ltc2632.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * LTC2632 Digital to analog convertors spi driver
  3. *
  4. * Copyright 2017 Maxime Roussin-Bélanger
  5. * expanded by Silvan Murer <silvan.murer@gmail.com>
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #include <linux/device.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/module.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/regulator/consumer.h>
  14. #define LTC2632_DAC_CHANNELS 2
  15. #define LTC2632_ADDR_DAC0 0x0
  16. #define LTC2632_ADDR_DAC1 0x1
  17. #define LTC2632_CMD_WRITE_INPUT_N 0x0
  18. #define LTC2632_CMD_UPDATE_DAC_N 0x1
  19. #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2
  20. #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_N 0x3
  21. #define LTC2632_CMD_POWERDOWN_DAC_N 0x4
  22. #define LTC2632_CMD_POWERDOWN_CHIP 0x5
  23. #define LTC2632_CMD_INTERNAL_REFER 0x6
  24. #define LTC2632_CMD_EXTERNAL_REFER 0x7
  25. /**
  26. * struct ltc2632_chip_info - chip specific information
  27. * @channels: channel spec for the DAC
  28. * @vref_mv: internal reference voltage
  29. */
  30. struct ltc2632_chip_info {
  31. const struct iio_chan_spec *channels;
  32. const int vref_mv;
  33. };
  34. /**
  35. * struct ltc2632_state - driver instance specific data
  36. * @spi_dev: pointer to the spi_device struct
  37. * @powerdown_cache_mask used to show current channel powerdown state
  38. * @vref_mv used reference voltage (internal or external)
  39. * @vref_reg regulator for the reference voltage
  40. */
  41. struct ltc2632_state {
  42. struct spi_device *spi_dev;
  43. unsigned int powerdown_cache_mask;
  44. int vref_mv;
  45. struct regulator *vref_reg;
  46. };
  47. enum ltc2632_supported_device_ids {
  48. ID_LTC2632L12,
  49. ID_LTC2632L10,
  50. ID_LTC2632L8,
  51. ID_LTC2632H12,
  52. ID_LTC2632H10,
  53. ID_LTC2632H8,
  54. };
  55. static int ltc2632_spi_write(struct spi_device *spi,
  56. u8 cmd, u8 addr, u16 val, u8 shift)
  57. {
  58. u32 data;
  59. u8 msg[3];
  60. /*
  61. * The input shift register is 24 bits wide.
  62. * The next four are the command bits, C3 to C0,
  63. * followed by the 4-bit DAC address, A3 to A0, and then the
  64. * 12-, 10-, 8-bit data-word. The data-word comprises the 12-,
  65. * 10-, 8-bit input code followed by 4, 6, or 8 don't care bits.
  66. */
  67. data = (cmd << 20) | (addr << 16) | (val << shift);
  68. msg[0] = data >> 16;
  69. msg[1] = data >> 8;
  70. msg[2] = data;
  71. return spi_write(spi, msg, sizeof(msg));
  72. }
  73. static int ltc2632_read_raw(struct iio_dev *indio_dev,
  74. struct iio_chan_spec const *chan,
  75. int *val,
  76. int *val2,
  77. long m)
  78. {
  79. const struct ltc2632_state *st = iio_priv(indio_dev);
  80. switch (m) {
  81. case IIO_CHAN_INFO_SCALE:
  82. *val = st->vref_mv;
  83. *val2 = chan->scan_type.realbits;
  84. return IIO_VAL_FRACTIONAL_LOG2;
  85. }
  86. return -EINVAL;
  87. }
  88. static int ltc2632_write_raw(struct iio_dev *indio_dev,
  89. struct iio_chan_spec const *chan,
  90. int val,
  91. int val2,
  92. long mask)
  93. {
  94. struct ltc2632_state *st = iio_priv(indio_dev);
  95. switch (mask) {
  96. case IIO_CHAN_INFO_RAW:
  97. if (val >= (1 << chan->scan_type.realbits) || val < 0)
  98. return -EINVAL;
  99. return ltc2632_spi_write(st->spi_dev,
  100. LTC2632_CMD_WRITE_INPUT_N_UPDATE_N,
  101. chan->address, val,
  102. chan->scan_type.shift);
  103. default:
  104. return -EINVAL;
  105. }
  106. }
  107. static ssize_t ltc2632_read_dac_powerdown(struct iio_dev *indio_dev,
  108. uintptr_t private,
  109. const struct iio_chan_spec *chan,
  110. char *buf)
  111. {
  112. struct ltc2632_state *st = iio_priv(indio_dev);
  113. return sprintf(buf, "%d\n",
  114. !!(st->powerdown_cache_mask & (1 << chan->channel)));
  115. }
  116. static ssize_t ltc2632_write_dac_powerdown(struct iio_dev *indio_dev,
  117. uintptr_t private,
  118. const struct iio_chan_spec *chan,
  119. const char *buf,
  120. size_t len)
  121. {
  122. bool pwr_down;
  123. int ret;
  124. struct ltc2632_state *st = iio_priv(indio_dev);
  125. ret = strtobool(buf, &pwr_down);
  126. if (ret)
  127. return ret;
  128. if (pwr_down)
  129. st->powerdown_cache_mask |= (1 << chan->channel);
  130. else
  131. st->powerdown_cache_mask &= ~(1 << chan->channel);
  132. ret = ltc2632_spi_write(st->spi_dev,
  133. LTC2632_CMD_POWERDOWN_DAC_N,
  134. chan->channel, 0, 0);
  135. return ret ? ret : len;
  136. }
  137. static const struct iio_info ltc2632_info = {
  138. .write_raw = ltc2632_write_raw,
  139. .read_raw = ltc2632_read_raw,
  140. };
  141. static const struct iio_chan_spec_ext_info ltc2632_ext_info[] = {
  142. {
  143. .name = "powerdown",
  144. .read = ltc2632_read_dac_powerdown,
  145. .write = ltc2632_write_dac_powerdown,
  146. .shared = IIO_SEPARATE,
  147. },
  148. { },
  149. };
  150. #define LTC2632_CHANNEL(_chan, _bits) { \
  151. .type = IIO_VOLTAGE, \
  152. .indexed = 1, \
  153. .output = 1, \
  154. .channel = (_chan), \
  155. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  156. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  157. .address = (_chan), \
  158. .scan_type = { \
  159. .realbits = (_bits), \
  160. .shift = 16 - (_bits), \
  161. }, \
  162. .ext_info = ltc2632_ext_info, \
  163. }
  164. #define DECLARE_LTC2632_CHANNELS(_name, _bits) \
  165. const struct iio_chan_spec _name ## _channels[] = { \
  166. LTC2632_CHANNEL(0, _bits), \
  167. LTC2632_CHANNEL(1, _bits), \
  168. }
  169. static DECLARE_LTC2632_CHANNELS(ltc2632l12, 12);
  170. static DECLARE_LTC2632_CHANNELS(ltc2632l10, 10);
  171. static DECLARE_LTC2632_CHANNELS(ltc2632l8, 8);
  172. static DECLARE_LTC2632_CHANNELS(ltc2632h12, 12);
  173. static DECLARE_LTC2632_CHANNELS(ltc2632h10, 10);
  174. static DECLARE_LTC2632_CHANNELS(ltc2632h8, 8);
  175. static const struct ltc2632_chip_info ltc2632_chip_info_tbl[] = {
  176. [ID_LTC2632L12] = {
  177. .channels = ltc2632l12_channels,
  178. .vref_mv = 2500,
  179. },
  180. [ID_LTC2632L10] = {
  181. .channels = ltc2632l10_channels,
  182. .vref_mv = 2500,
  183. },
  184. [ID_LTC2632L8] = {
  185. .channels = ltc2632l8_channels,
  186. .vref_mv = 2500,
  187. },
  188. [ID_LTC2632H12] = {
  189. .channels = ltc2632h12_channels,
  190. .vref_mv = 4096,
  191. },
  192. [ID_LTC2632H10] = {
  193. .channels = ltc2632h10_channels,
  194. .vref_mv = 4096,
  195. },
  196. [ID_LTC2632H8] = {
  197. .channels = ltc2632h8_channels,
  198. .vref_mv = 4096,
  199. },
  200. };
  201. static int ltc2632_probe(struct spi_device *spi)
  202. {
  203. struct ltc2632_state *st;
  204. struct iio_dev *indio_dev;
  205. struct ltc2632_chip_info *chip_info;
  206. int ret;
  207. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  208. if (!indio_dev)
  209. return -ENOMEM;
  210. st = iio_priv(indio_dev);
  211. spi_set_drvdata(spi, indio_dev);
  212. st->spi_dev = spi;
  213. chip_info = (struct ltc2632_chip_info *)
  214. spi_get_device_id(spi)->driver_data;
  215. st->vref_reg = devm_regulator_get_optional(&spi->dev, "vref");
  216. if (PTR_ERR(st->vref_reg) == -ENODEV) {
  217. /* use internal reference voltage */
  218. st->vref_reg = NULL;
  219. st->vref_mv = chip_info->vref_mv;
  220. ret = ltc2632_spi_write(spi, LTC2632_CMD_INTERNAL_REFER,
  221. 0, 0, 0);
  222. if (ret) {
  223. dev_err(&spi->dev,
  224. "Set internal reference command failed, %d\n",
  225. ret);
  226. return ret;
  227. }
  228. } else if (IS_ERR(st->vref_reg)) {
  229. dev_err(&spi->dev,
  230. "Error getting voltage reference regulator\n");
  231. return PTR_ERR(st->vref_reg);
  232. } else {
  233. /* use external reference voltage */
  234. ret = regulator_enable(st->vref_reg);
  235. if (ret) {
  236. dev_err(&spi->dev,
  237. "enable reference regulator failed, %d\n",
  238. ret);
  239. return ret;
  240. }
  241. st->vref_mv = regulator_get_voltage(st->vref_reg) / 1000;
  242. ret = ltc2632_spi_write(spi, LTC2632_CMD_EXTERNAL_REFER,
  243. 0, 0, 0);
  244. if (ret) {
  245. dev_err(&spi->dev,
  246. "Set external reference command failed, %d\n",
  247. ret);
  248. return ret;
  249. }
  250. }
  251. indio_dev->dev.parent = &spi->dev;
  252. indio_dev->name = dev_of_node(&spi->dev) ? dev_of_node(&spi->dev)->name
  253. : spi_get_device_id(spi)->name;
  254. indio_dev->info = &ltc2632_info;
  255. indio_dev->modes = INDIO_DIRECT_MODE;
  256. indio_dev->channels = chip_info->channels;
  257. indio_dev->num_channels = LTC2632_DAC_CHANNELS;
  258. return iio_device_register(indio_dev);
  259. }
  260. static int ltc2632_remove(struct spi_device *spi)
  261. {
  262. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  263. struct ltc2632_state *st = iio_priv(indio_dev);
  264. iio_device_unregister(indio_dev);
  265. if (st->vref_reg)
  266. regulator_disable(st->vref_reg);
  267. return 0;
  268. }
  269. static const struct spi_device_id ltc2632_id[] = {
  270. { "ltc2632-l12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632L12] },
  271. { "ltc2632-l10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632L10] },
  272. { "ltc2632-l8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632L8] },
  273. { "ltc2632-h12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H12] },
  274. { "ltc2632-h10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H10] },
  275. { "ltc2632-h8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H8] },
  276. {}
  277. };
  278. MODULE_DEVICE_TABLE(spi, ltc2632_id);
  279. static const struct of_device_id ltc2632_of_match[] = {
  280. {
  281. .compatible = "lltc,ltc2632-l12",
  282. .data = &ltc2632_chip_info_tbl[ID_LTC2632L12]
  283. }, {
  284. .compatible = "lltc,ltc2632-l10",
  285. .data = &ltc2632_chip_info_tbl[ID_LTC2632L10]
  286. }, {
  287. .compatible = "lltc,ltc2632-l8",
  288. .data = &ltc2632_chip_info_tbl[ID_LTC2632L8]
  289. }, {
  290. .compatible = "lltc,ltc2632-h12",
  291. .data = &ltc2632_chip_info_tbl[ID_LTC2632H12]
  292. }, {
  293. .compatible = "lltc,ltc2632-h10",
  294. .data = &ltc2632_chip_info_tbl[ID_LTC2632H10]
  295. }, {
  296. .compatible = "lltc,ltc2632-h8",
  297. .data = &ltc2632_chip_info_tbl[ID_LTC2632H8]
  298. },
  299. {}
  300. };
  301. MODULE_DEVICE_TABLE(of, ltc2632_of_match);
  302. static struct spi_driver ltc2632_driver = {
  303. .driver = {
  304. .name = "ltc2632",
  305. .of_match_table = of_match_ptr(ltc2632_of_match),
  306. },
  307. .probe = ltc2632_probe,
  308. .remove = ltc2632_remove,
  309. .id_table = ltc2632_id,
  310. };
  311. module_spi_driver(ltc2632_driver);
  312. MODULE_AUTHOR("Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>");
  313. MODULE_DESCRIPTION("LTC2632 DAC SPI driver");
  314. MODULE_LICENSE("GPL v2");