regmap-spi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Register map access API - SPI support
  4. //
  5. // Copyright 2011 Wolfson Microelectronics plc
  6. //
  7. // Author: Mark Brown <[email protected]>
  8. #include <linux/regmap.h>
  9. #include <linux/spi/spi.h>
  10. #include <linux/module.h>
  11. #include "internal.h"
  12. struct regmap_async_spi {
  13. struct regmap_async core;
  14. struct spi_message m;
  15. struct spi_transfer t[2];
  16. };
  17. static void regmap_spi_complete(void *data)
  18. {
  19. struct regmap_async_spi *async = data;
  20. regmap_async_complete_cb(&async->core, async->m.status);
  21. }
  22. static int regmap_spi_write(void *context, const void *data, size_t count)
  23. {
  24. struct device *dev = context;
  25. struct spi_device *spi = to_spi_device(dev);
  26. return spi_write(spi, data, count);
  27. }
  28. static int regmap_spi_gather_write(void *context,
  29. const void *reg, size_t reg_len,
  30. const void *val, size_t val_len)
  31. {
  32. struct device *dev = context;
  33. struct spi_device *spi = to_spi_device(dev);
  34. struct spi_message m;
  35. struct spi_transfer t[2] = { { .tx_buf = reg, .len = reg_len, },
  36. { .tx_buf = val, .len = val_len, }, };
  37. spi_message_init(&m);
  38. spi_message_add_tail(&t[0], &m);
  39. spi_message_add_tail(&t[1], &m);
  40. return spi_sync(spi, &m);
  41. }
  42. static int regmap_spi_async_write(void *context,
  43. const void *reg, size_t reg_len,
  44. const void *val, size_t val_len,
  45. struct regmap_async *a)
  46. {
  47. struct regmap_async_spi *async = container_of(a,
  48. struct regmap_async_spi,
  49. core);
  50. struct device *dev = context;
  51. struct spi_device *spi = to_spi_device(dev);
  52. async->t[0].tx_buf = reg;
  53. async->t[0].len = reg_len;
  54. async->t[1].tx_buf = val;
  55. async->t[1].len = val_len;
  56. spi_message_init(&async->m);
  57. spi_message_add_tail(&async->t[0], &async->m);
  58. if (val)
  59. spi_message_add_tail(&async->t[1], &async->m);
  60. async->m.complete = regmap_spi_complete;
  61. async->m.context = async;
  62. return spi_async(spi, &async->m);
  63. }
  64. static struct regmap_async *regmap_spi_async_alloc(void)
  65. {
  66. struct regmap_async_spi *async_spi;
  67. async_spi = kzalloc(sizeof(*async_spi), GFP_KERNEL);
  68. if (!async_spi)
  69. return NULL;
  70. return &async_spi->core;
  71. }
  72. static int regmap_spi_read(void *context,
  73. const void *reg, size_t reg_size,
  74. void *val, size_t val_size)
  75. {
  76. struct device *dev = context;
  77. struct spi_device *spi = to_spi_device(dev);
  78. return spi_write_then_read(spi, reg, reg_size, val, val_size);
  79. }
  80. static const struct regmap_bus regmap_spi = {
  81. .write = regmap_spi_write,
  82. .gather_write = regmap_spi_gather_write,
  83. .async_write = regmap_spi_async_write,
  84. .async_alloc = regmap_spi_async_alloc,
  85. .read = regmap_spi_read,
  86. .read_flag_mask = 0x80,
  87. .reg_format_endian_default = REGMAP_ENDIAN_BIG,
  88. .val_format_endian_default = REGMAP_ENDIAN_BIG,
  89. };
  90. static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
  91. const struct regmap_config *config)
  92. {
  93. size_t max_size = spi_max_transfer_size(spi);
  94. size_t max_msg_size, reg_reserve_size;
  95. struct regmap_bus *bus;
  96. if (max_size != SIZE_MAX) {
  97. bus = kmemdup(&regmap_spi, sizeof(*bus), GFP_KERNEL);
  98. if (!bus)
  99. return ERR_PTR(-ENOMEM);
  100. max_msg_size = spi_max_message_size(spi);
  101. reg_reserve_size = config->reg_bits / BITS_PER_BYTE
  102. + config->pad_bits / BITS_PER_BYTE;
  103. if (max_size + reg_reserve_size > max_msg_size)
  104. max_size -= reg_reserve_size;
  105. bus->free_on_exit = true;
  106. bus->max_raw_read = max_size;
  107. bus->max_raw_write = max_size;
  108. return bus;
  109. }
  110. return &regmap_spi;
  111. }
  112. struct regmap *__regmap_init_spi(struct spi_device *spi,
  113. const struct regmap_config *config,
  114. struct lock_class_key *lock_key,
  115. const char *lock_name)
  116. {
  117. const struct regmap_bus *bus = regmap_get_spi_bus(spi, config);
  118. if (IS_ERR(bus))
  119. return ERR_CAST(bus);
  120. return __regmap_init(&spi->dev, bus, &spi->dev, config, lock_key, lock_name);
  121. }
  122. EXPORT_SYMBOL_GPL(__regmap_init_spi);
  123. struct regmap *__devm_regmap_init_spi(struct spi_device *spi,
  124. const struct regmap_config *config,
  125. struct lock_class_key *lock_key,
  126. const char *lock_name)
  127. {
  128. const struct regmap_bus *bus = regmap_get_spi_bus(spi, config);
  129. if (IS_ERR(bus))
  130. return ERR_CAST(bus);
  131. return __devm_regmap_init(&spi->dev, bus, &spi->dev, config, lock_key, lock_name);
  132. }
  133. EXPORT_SYMBOL_GPL(__devm_regmap_init_spi);
  134. MODULE_LICENSE("GPL");