regmap-spi.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. if (val)
  64. spi_message_add_tail(&async->t[1], &async->m);
  65. async->m.complete = regmap_spi_complete;
  66. async->m.context = async;
  67. return spi_async(spi, &async->m);
  68. }
  69. static struct regmap_async *regmap_spi_async_alloc(void)
  70. {
  71. struct regmap_async_spi *async_spi;
  72. async_spi = kzalloc(sizeof(*async_spi), GFP_KERNEL);
  73. if (!async_spi)
  74. return NULL;
  75. return &async_spi->core;
  76. }
  77. static int regmap_spi_read(void *context,
  78. const void *reg, size_t reg_size,
  79. void *val, size_t val_size)
  80. {
  81. struct device *dev = context;
  82. struct spi_device *spi = to_spi_device(dev);
  83. return spi_write_then_read(spi, reg, reg_size, val, val_size);
  84. }
  85. static struct regmap_bus regmap_spi = {
  86. .write = regmap_spi_write,
  87. .gather_write = regmap_spi_gather_write,
  88. .async_write = regmap_spi_async_write,
  89. .async_alloc = regmap_spi_async_alloc,
  90. .read = regmap_spi_read,
  91. .read_flag_mask = 0x80,
  92. };
  93. /**
  94. * regmap_init_spi(): Initialise register map
  95. *
  96. * @spi: Device that will be interacted with
  97. * @config: Configuration for register map
  98. *
  99. * The return value will be an ERR_PTR() on error or a valid pointer to
  100. * a struct regmap.
  101. */
  102. struct regmap *regmap_init_spi(struct spi_device *spi,
  103. const struct regmap_config *config)
  104. {
  105. return regmap_init(&spi->dev, &regmap_spi, &spi->dev, config);
  106. }
  107. EXPORT_SYMBOL_GPL(regmap_init_spi);
  108. /**
  109. * devm_regmap_init_spi(): Initialise register map
  110. *
  111. * @spi: Device that will be interacted with
  112. * @config: Configuration for register map
  113. *
  114. * The return value will be an ERR_PTR() on error or a valid pointer
  115. * to a struct regmap. The map will be automatically freed by the
  116. * device management code.
  117. */
  118. struct regmap *devm_regmap_init_spi(struct spi_device *spi,
  119. const struct regmap_config *config)
  120. {
  121. return devm_regmap_init(&spi->dev, &regmap_spi, &spi->dev, config);
  122. }
  123. EXPORT_SYMBOL_GPL(devm_regmap_init_spi);
  124. MODULE_LICENSE("GPL");