regmap-spi.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. static int regmap_spi_write(void *context, const void *data, size_t count)
  17. {
  18. struct device *dev = context;
  19. struct spi_device *spi = to_spi_device(dev);
  20. return spi_write(spi, data, count);
  21. }
  22. static int regmap_spi_gather_write(void *context,
  23. const void *reg, size_t reg_len,
  24. const void *val, size_t val_len)
  25. {
  26. struct device *dev = context;
  27. struct spi_device *spi = to_spi_device(dev);
  28. struct spi_message m;
  29. struct spi_transfer t[2] = { { .tx_buf = reg, .len = reg_len, },
  30. { .tx_buf = val, .len = val_len, }, };
  31. spi_message_init(&m);
  32. spi_message_add_tail(&t[0], &m);
  33. spi_message_add_tail(&t[1], &m);
  34. return spi_sync(spi, &m);
  35. }
  36. static int regmap_spi_read(void *context,
  37. const void *reg, size_t reg_size,
  38. void *val, size_t val_size)
  39. {
  40. struct device *dev = context;
  41. struct spi_device *spi = to_spi_device(dev);
  42. return spi_write_then_read(spi, reg, reg_size, val, val_size);
  43. }
  44. static struct regmap_bus regmap_spi = {
  45. .write = regmap_spi_write,
  46. .gather_write = regmap_spi_gather_write,
  47. .read = regmap_spi_read,
  48. .read_flag_mask = 0x80,
  49. };
  50. /**
  51. * regmap_init_spi(): Initialise register map
  52. *
  53. * @spi: Device that will be interacted with
  54. * @config: Configuration for register map
  55. *
  56. * The return value will be an ERR_PTR() on error or a valid pointer to
  57. * a struct regmap.
  58. */
  59. struct regmap *regmap_init_spi(struct spi_device *spi,
  60. const struct regmap_config *config)
  61. {
  62. return regmap_init(&spi->dev, &regmap_spi, &spi->dev, config);
  63. }
  64. EXPORT_SYMBOL_GPL(regmap_init_spi);
  65. /**
  66. * devm_regmap_init_spi(): Initialise register map
  67. *
  68. * @spi: Device that will be interacted with
  69. * @config: Configuration for register map
  70. *
  71. * The return value will be an ERR_PTR() on error or a valid pointer
  72. * to a struct regmap. The map will be automatically freed by the
  73. * device management code.
  74. */
  75. struct regmap *devm_regmap_init_spi(struct spi_device *spi,
  76. const struct regmap_config *config)
  77. {
  78. return devm_regmap_init(&spi->dev, &regmap_spi, &spi->dev, config);
  79. }
  80. EXPORT_SYMBOL_GPL(devm_regmap_init_spi);
  81. MODULE_LICENSE("GPL");