regmap-spi.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Register map access API - SPI support
  3. *
  4. * Copyright 2011 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/regmap.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include "internal.h"
  17. struct regmap_async_spi {
  18. struct regmap_async core;
  19. struct spi_message m;
  20. struct spi_transfer t[2];
  21. };
  22. static void regmap_spi_complete(void *data)
  23. {
  24. struct regmap_async_spi *async = data;
  25. regmap_async_complete_cb(&async->core, async->m.status);
  26. }
  27. static int regmap_spi_write(void *context, const void *data, size_t count)
  28. {
  29. struct device *dev = context;
  30. struct spi_device *spi = to_spi_device(dev);
  31. return spi_write(spi, data, count);
  32. }
  33. static int regmap_spi_gather_write(void *context,
  34. const void *reg, size_t reg_len,
  35. const void *val, size_t val_len)
  36. {
  37. struct device *dev = context;
  38. struct spi_device *spi = to_spi_device(dev);
  39. struct spi_message m;
  40. struct spi_transfer t[2] = { { .tx_buf = reg, .len = reg_len, },
  41. { .tx_buf = val, .len = val_len, }, };
  42. spi_message_init(&m);
  43. spi_message_add_tail(&t[0], &m);
  44. spi_message_add_tail(&t[1], &m);
  45. return spi_sync(spi, &m);
  46. }
  47. static int regmap_spi_async_write(void *context,
  48. const void *reg, size_t reg_len,
  49. const void *val, size_t val_len,
  50. struct regmap_async *a)
  51. {
  52. struct regmap_async_spi *async = container_of(a,
  53. struct regmap_async_spi,
  54. core);
  55. struct device *dev = context;
  56. struct spi_device *spi = to_spi_device(dev);
  57. async->t[0].tx_buf = reg;
  58. async->t[0].len = reg_len;
  59. async->t[1].tx_buf = val;
  60. async->t[1].len = val_len;
  61. spi_message_init(&async->m);
  62. spi_message_add_tail(&async->t[0], &async->m);
  63. spi_message_add_tail(&async->t[1], &async->m);
  64. async->m.complete = regmap_spi_complete;
  65. async->m.context = async;
  66. return spi_async(spi, &async->m);
  67. }
  68. static struct regmap_async *regmap_spi_async_alloc(void)
  69. {
  70. struct regmap_async_spi *async_spi;
  71. async_spi = kzalloc(sizeof(*async_spi), GFP_KERNEL);
  72. if (!async_spi)
  73. return NULL;
  74. return &async_spi->core;
  75. }
  76. static int regmap_spi_read(void *context,
  77. const void *reg, size_t reg_size,
  78. void *val, size_t val_size)
  79. {
  80. struct device *dev = context;
  81. struct spi_device *spi = to_spi_device(dev);
  82. return spi_write_then_read(spi, reg, reg_size, val, val_size);
  83. }
  84. static struct regmap_bus regmap_spi = {
  85. .write = regmap_spi_write,
  86. .gather_write = regmap_spi_gather_write,
  87. .async_write = regmap_spi_async_write,
  88. .async_alloc = regmap_spi_async_alloc,
  89. .read = regmap_spi_read,
  90. .read_flag_mask = 0x80,
  91. };
  92. /**
  93. * regmap_init_spi(): Initialise register map
  94. *
  95. * @spi: Device that will be interacted with
  96. * @config: Configuration for register map
  97. *
  98. * The return value will be an ERR_PTR() on error or a valid pointer to
  99. * a struct regmap.
  100. */
  101. struct regmap *regmap_init_spi(struct spi_device *spi,
  102. const struct regmap_config *config)
  103. {
  104. return regmap_init(&spi->dev, &regmap_spi, &spi->dev, config);
  105. }
  106. EXPORT_SYMBOL_GPL(regmap_init_spi);
  107. /**
  108. * devm_regmap_init_spi(): Initialise register map
  109. *
  110. * @spi: Device that will be interacted with
  111. * @config: Configuration for register map
  112. *
  113. * The return value will be an ERR_PTR() on error or a valid pointer
  114. * to a struct regmap. The map will be automatically freed by the
  115. * device management code.
  116. */
  117. struct regmap *devm_regmap_init_spi(struct spi_device *spi,
  118. const struct regmap_config *config)
  119. {
  120. return devm_regmap_init(&spi->dev, &regmap_spi, &spi->dev, config);
  121. }
  122. EXPORT_SYMBOL_GPL(devm_regmap_init_spi);
  123. MODULE_LICENSE("GPL");