regmap-spi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/module.h>
  15. #include "internal.h"
  16. struct regmap_async_spi {
  17. struct regmap_async core;
  18. struct spi_message m;
  19. struct spi_transfer t[2];
  20. };
  21. static void regmap_spi_complete(void *data)
  22. {
  23. struct regmap_async_spi *async = data;
  24. regmap_async_complete_cb(&async->core, async->m.status);
  25. }
  26. static int regmap_spi_write(void *context, const void *data, size_t count)
  27. {
  28. struct device *dev = context;
  29. struct spi_device *spi = to_spi_device(dev);
  30. return spi_write(spi, data, count);
  31. }
  32. static int regmap_spi_gather_write(void *context,
  33. const void *reg, size_t reg_len,
  34. const void *val, size_t val_len)
  35. {
  36. struct device *dev = context;
  37. struct spi_device *spi = to_spi_device(dev);
  38. struct spi_message m;
  39. struct spi_transfer t[2] = { { .tx_buf = reg, .len = reg_len, },
  40. { .tx_buf = val, .len = val_len, }, };
  41. spi_message_init(&m);
  42. spi_message_add_tail(&t[0], &m);
  43. spi_message_add_tail(&t[1], &m);
  44. return spi_sync(spi, &m);
  45. }
  46. static int regmap_spi_async_write(void *context,
  47. const void *reg, size_t reg_len,
  48. const void *val, size_t val_len,
  49. struct regmap_async *a)
  50. {
  51. struct regmap_async_spi *async = container_of(a,
  52. struct regmap_async_spi,
  53. core);
  54. struct device *dev = context;
  55. struct spi_device *spi = to_spi_device(dev);
  56. async->t[0].tx_buf = reg;
  57. async->t[0].len = reg_len;
  58. async->t[1].tx_buf = val;
  59. async->t[1].len = val_len;
  60. spi_message_init(&async->m);
  61. spi_message_add_tail(&async->t[0], &async->m);
  62. if (val)
  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. .reg_format_endian_default = REGMAP_ENDIAN_BIG,
  92. .val_format_endian_default = REGMAP_ENDIAN_BIG,
  93. };
  94. struct regmap *__regmap_init_spi(struct spi_device *spi,
  95. const struct regmap_config *config,
  96. struct lock_class_key *lock_key,
  97. const char *lock_name)
  98. {
  99. return __regmap_init(&spi->dev, &regmap_spi, &spi->dev, config,
  100. lock_key, lock_name);
  101. }
  102. EXPORT_SYMBOL_GPL(__regmap_init_spi);
  103. struct regmap *__devm_regmap_init_spi(struct spi_device *spi,
  104. const struct regmap_config *config,
  105. struct lock_class_key *lock_key,
  106. const char *lock_name)
  107. {
  108. return __devm_regmap_init(&spi->dev, &regmap_spi, &spi->dev, config,
  109. lock_key, lock_name);
  110. }
  111. EXPORT_SYMBOL_GPL(__devm_regmap_init_spi);
  112. MODULE_LICENSE("GPL");