rohm-bd79703.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * BD79703 ROHM Digital to Analog converter
  4. *
  5. * Copyright (c) 2024, ROHM Semiconductor.
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/regmap.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/iio/iio.h>
  14. #define BD79703_MAX_REGISTER 0xf
  15. #define BD79703_DAC_BITS 8
  16. #define BD79703_REG_OUT_ALL GENMASK(2, 0)
  17. /*
  18. * The BD79703 uses 12-bit SPI commands. First four bits (high bits) define
  19. * channel(s) which are operated on, and also the mode. The mode can be to set
  20. * a DAC word only, or set DAC word and output. The data-sheet is not very
  21. * specific on how a previously set DAC word can be 'taken in to use'. Thus
  22. * this driver only uses the 'set DAC and output it' -mode.
  23. *
  24. * The BD79703 latches last 12-bits when the chip-select is toggled. Thus we
  25. * can use 16-bit transfers which should be widely supported. To simplify this
  26. * further, we treat the last 8 bits as a value, and first 8 bits as an
  27. * address. This allows us to separate channels/mode by address and treat the
  28. * 8-bit register value as DAC word. The highest 4 bits of address will be
  29. * discarded when the transfer is latched.
  30. */
  31. static const struct regmap_config bd79703_regmap_config = {
  32. .reg_bits = 8,
  33. .val_bits = 8,
  34. .max_register = BD79703_MAX_REGISTER,
  35. .cache_type = REGCACHE_MAPLE,
  36. };
  37. /* Dynamic driver private data */
  38. struct bd79703_data {
  39. struct regmap *regmap;
  40. int vfs;
  41. };
  42. /* Static, IC type specific data for different variants */
  43. struct bd7970x_chip_data {
  44. const char *name;
  45. const struct iio_chan_spec *channels;
  46. int num_channels;
  47. bool has_vfs;
  48. };
  49. static int bd79703_read_raw(struct iio_dev *idev,
  50. struct iio_chan_spec const *chan, int *val,
  51. int *val2, long mask)
  52. {
  53. struct bd79703_data *data = iio_priv(idev);
  54. if (mask != IIO_CHAN_INFO_SCALE)
  55. return -EINVAL;
  56. *val = data->vfs / 1000;
  57. *val2 = BD79703_DAC_BITS;
  58. return IIO_VAL_FRACTIONAL_LOG2;
  59. }
  60. static int bd79703_write_raw(struct iio_dev *idev,
  61. struct iio_chan_spec const *chan, int val,
  62. int val2, long mask)
  63. {
  64. struct bd79703_data *data = iio_priv(idev);
  65. if (val < 0 || val >= 1 << BD79703_DAC_BITS)
  66. return -EINVAL;
  67. return regmap_write(data->regmap, chan->address, val);
  68. };
  69. static const struct iio_info bd79703_info = {
  70. .read_raw = bd79703_read_raw,
  71. .write_raw = bd79703_write_raw,
  72. };
  73. #define BD79703_CHAN_ADDR(_chan, _addr) { \
  74. .type = IIO_VOLTAGE, \
  75. .indexed = 1, \
  76. .output = 1, \
  77. .channel = (_chan), \
  78. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  79. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  80. .address = (_addr), \
  81. }
  82. #define BD79703_CHAN(_chan) BD79703_CHAN_ADDR((_chan), (_chan) + 1)
  83. static const struct iio_chan_spec bd79700_channels[] = {
  84. BD79703_CHAN(0),
  85. BD79703_CHAN(1),
  86. };
  87. static const struct iio_chan_spec bd79701_channels[] = {
  88. BD79703_CHAN(0),
  89. BD79703_CHAN(1),
  90. BD79703_CHAN(2),
  91. };
  92. /*
  93. * The BD79702 has 4 channels. They aren't mapped to BD79703 channels 0, 1, 2
  94. * and 3, but to the channels 0, 1, 4, 5. So the addressing used with SPI
  95. * accesses is 1, 2, 5 and 6 for them. Thus, they're not constant offset to
  96. * the channel number as with other IC variants.
  97. */
  98. static const struct iio_chan_spec bd79702_channels[] = {
  99. BD79703_CHAN_ADDR(0, 1),
  100. BD79703_CHAN_ADDR(1, 2),
  101. BD79703_CHAN_ADDR(2, 5),
  102. BD79703_CHAN_ADDR(3, 6),
  103. };
  104. static const struct iio_chan_spec bd79703_channels[] = {
  105. BD79703_CHAN(0),
  106. BD79703_CHAN(1),
  107. BD79703_CHAN(2),
  108. BD79703_CHAN(3),
  109. BD79703_CHAN(4),
  110. BD79703_CHAN(5),
  111. };
  112. static const struct bd7970x_chip_data bd79700_chip_data = {
  113. .name = "bd79700",
  114. .channels = bd79700_channels,
  115. .num_channels = ARRAY_SIZE(bd79700_channels),
  116. .has_vfs = false,
  117. };
  118. static const struct bd7970x_chip_data bd79701_chip_data = {
  119. .name = "bd79701",
  120. .channels = bd79701_channels,
  121. .num_channels = ARRAY_SIZE(bd79701_channels),
  122. .has_vfs = false,
  123. };
  124. static const struct bd7970x_chip_data bd79702_chip_data = {
  125. .name = "bd79702",
  126. .channels = bd79702_channels,
  127. .num_channels = ARRAY_SIZE(bd79702_channels),
  128. .has_vfs = true,
  129. };
  130. static const struct bd7970x_chip_data bd79703_chip_data = {
  131. .name = "bd79703",
  132. .channels = bd79703_channels,
  133. .num_channels = ARRAY_SIZE(bd79703_channels),
  134. .has_vfs = true,
  135. };
  136. static int bd79703_probe(struct spi_device *spi)
  137. {
  138. const struct bd7970x_chip_data *cd;
  139. struct device *dev = &spi->dev;
  140. struct bd79703_data *data;
  141. struct iio_dev *idev;
  142. int ret;
  143. cd = spi_get_device_match_data(spi);
  144. if (!cd)
  145. return -ENODEV;
  146. idev = devm_iio_device_alloc(dev, sizeof(*data));
  147. if (!idev)
  148. return -ENOMEM;
  149. data = iio_priv(idev);
  150. data->regmap = devm_regmap_init_spi(spi, &bd79703_regmap_config);
  151. if (IS_ERR(data->regmap))
  152. return dev_err_probe(dev, PTR_ERR(data->regmap),
  153. "Failed to initialize Regmap\n");
  154. /*
  155. * BD79703 has a separate VFS pin, whereas the BD79700 and BD79701 use
  156. * VCC for their full-scale output voltage.
  157. */
  158. if (cd->has_vfs) {
  159. ret = devm_regulator_get_enable(dev, "vcc");
  160. if (ret)
  161. return dev_err_probe(dev, ret, "Failed to enable VCC\n");
  162. ret = devm_regulator_get_enable_read_voltage(dev, "vfs");
  163. if (ret < 0)
  164. return dev_err_probe(dev, ret, "Failed to get Vfs\n");
  165. } else {
  166. ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
  167. if (ret < 0)
  168. return dev_err_probe(dev, ret, "Failed to get VCC\n");
  169. }
  170. data->vfs = ret;
  171. idev->channels = cd->channels;
  172. idev->num_channels = cd->num_channels;
  173. idev->modes = INDIO_DIRECT_MODE;
  174. idev->info = &bd79703_info;
  175. idev->name = cd->name;
  176. /* Initialize all to output zero */
  177. ret = regmap_write(data->regmap, BD79703_REG_OUT_ALL, 0);
  178. if (ret)
  179. return ret;
  180. return devm_iio_device_register(dev, idev);
  181. }
  182. static const struct spi_device_id bd79703_id[] = {
  183. { "bd79700", (kernel_ulong_t)&bd79700_chip_data },
  184. { "bd79701", (kernel_ulong_t)&bd79701_chip_data },
  185. { "bd79702", (kernel_ulong_t)&bd79702_chip_data },
  186. { "bd79703", (kernel_ulong_t)&bd79703_chip_data },
  187. { }
  188. };
  189. MODULE_DEVICE_TABLE(spi, bd79703_id);
  190. static const struct of_device_id bd79703_of_match[] = {
  191. { .compatible = "rohm,bd79700", .data = &bd79700_chip_data },
  192. { .compatible = "rohm,bd79701", .data = &bd79701_chip_data },
  193. { .compatible = "rohm,bd79702", .data = &bd79702_chip_data },
  194. { .compatible = "rohm,bd79703", .data = &bd79703_chip_data },
  195. { }
  196. };
  197. MODULE_DEVICE_TABLE(of, bd79703_of_match);
  198. static struct spi_driver bd79703_driver = {
  199. .driver = {
  200. .name = "bd79703",
  201. .of_match_table = bd79703_of_match,
  202. },
  203. .probe = bd79703_probe,
  204. .id_table = bd79703_id,
  205. };
  206. module_spi_driver(bd79703_driver);
  207. MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>");
  208. MODULE_DESCRIPTION("ROHM BD79703 DAC driver");
  209. MODULE_LICENSE("GPL");