regmap-spi.c 3.7 KB

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