ltc2632.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * LTC2632 Digital to analog convertors spi driver
  4. *
  5. * Copyright 2017 Maxime Roussin-Bélanger
  6. * expanded by Silvan Murer <[email protected]>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/spi/spi.h>
  10. #include <linux/module.h>
  11. #include <linux/iio/iio.h>
  12. #include <linux/property.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <asm/unaligned.h>
  15. #define LTC2632_CMD_WRITE_INPUT_N 0x0
  16. #define LTC2632_CMD_UPDATE_DAC_N 0x1
  17. #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2
  18. #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_N 0x3
  19. #define LTC2632_CMD_POWERDOWN_DAC_N 0x4
  20. #define LTC2632_CMD_POWERDOWN_CHIP 0x5
  21. #define LTC2632_CMD_INTERNAL_REFER 0x6
  22. #define LTC2632_CMD_EXTERNAL_REFER 0x7
  23. /**
  24. * struct ltc2632_chip_info - chip specific information
  25. * @channels: channel spec for the DAC
  26. * @num_channels: DAC channel count of the chip
  27. * @vref_mv: internal reference voltage
  28. */
  29. struct ltc2632_chip_info {
  30. const struct iio_chan_spec *channels;
  31. const size_t num_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. ID_LTC2634L12,
  55. ID_LTC2634L10,
  56. ID_LTC2634L8,
  57. ID_LTC2634H12,
  58. ID_LTC2634H10,
  59. ID_LTC2634H8,
  60. ID_LTC2636L12,
  61. ID_LTC2636L10,
  62. ID_LTC2636L8,
  63. ID_LTC2636H12,
  64. ID_LTC2636H10,
  65. ID_LTC2636H8,
  66. };
  67. static int ltc2632_spi_write(struct spi_device *spi,
  68. u8 cmd, u8 addr, u16 val, u8 shift)
  69. {
  70. u32 data;
  71. u8 msg[3];
  72. /*
  73. * The input shift register is 24 bits wide.
  74. * The next four are the command bits, C3 to C0,
  75. * followed by the 4-bit DAC address, A3 to A0, and then the
  76. * 12-, 10-, 8-bit data-word. The data-word comprises the 12-,
  77. * 10-, 8-bit input code followed by 4, 6, or 8 don't care bits.
  78. */
  79. data = (cmd << 20) | (addr << 16) | (val << shift);
  80. put_unaligned_be24(data, &msg[0]);
  81. return spi_write(spi, msg, sizeof(msg));
  82. }
  83. static int ltc2632_read_raw(struct iio_dev *indio_dev,
  84. struct iio_chan_spec const *chan,
  85. int *val,
  86. int *val2,
  87. long m)
  88. {
  89. const struct ltc2632_state *st = iio_priv(indio_dev);
  90. switch (m) {
  91. case IIO_CHAN_INFO_SCALE:
  92. *val = st->vref_mv;
  93. *val2 = chan->scan_type.realbits;
  94. return IIO_VAL_FRACTIONAL_LOG2;
  95. }
  96. return -EINVAL;
  97. }
  98. static int ltc2632_write_raw(struct iio_dev *indio_dev,
  99. struct iio_chan_spec const *chan,
  100. int val,
  101. int val2,
  102. long mask)
  103. {
  104. struct ltc2632_state *st = iio_priv(indio_dev);
  105. switch (mask) {
  106. case IIO_CHAN_INFO_RAW:
  107. if (val >= (1 << chan->scan_type.realbits) || val < 0)
  108. return -EINVAL;
  109. return ltc2632_spi_write(st->spi_dev,
  110. LTC2632_CMD_WRITE_INPUT_N_UPDATE_N,
  111. chan->address, val,
  112. chan->scan_type.shift);
  113. default:
  114. return -EINVAL;
  115. }
  116. }
  117. static ssize_t ltc2632_read_dac_powerdown(struct iio_dev *indio_dev,
  118. uintptr_t private,
  119. const struct iio_chan_spec *chan,
  120. char *buf)
  121. {
  122. struct ltc2632_state *st = iio_priv(indio_dev);
  123. return sysfs_emit(buf, "%d\n",
  124. !!(st->powerdown_cache_mask & (1 << chan->channel)));
  125. }
  126. static ssize_t ltc2632_write_dac_powerdown(struct iio_dev *indio_dev,
  127. uintptr_t private,
  128. const struct iio_chan_spec *chan,
  129. const char *buf,
  130. size_t len)
  131. {
  132. bool pwr_down;
  133. int ret;
  134. struct ltc2632_state *st = iio_priv(indio_dev);
  135. ret = kstrtobool(buf, &pwr_down);
  136. if (ret)
  137. return ret;
  138. if (pwr_down)
  139. st->powerdown_cache_mask |= (1 << chan->channel);
  140. else
  141. st->powerdown_cache_mask &= ~(1 << chan->channel);
  142. ret = ltc2632_spi_write(st->spi_dev,
  143. LTC2632_CMD_POWERDOWN_DAC_N,
  144. chan->channel, 0, 0);
  145. return ret ? ret : len;
  146. }
  147. static const struct iio_info ltc2632_info = {
  148. .write_raw = ltc2632_write_raw,
  149. .read_raw = ltc2632_read_raw,
  150. };
  151. static const struct iio_chan_spec_ext_info ltc2632_ext_info[] = {
  152. {
  153. .name = "powerdown",
  154. .read = ltc2632_read_dac_powerdown,
  155. .write = ltc2632_write_dac_powerdown,
  156. .shared = IIO_SEPARATE,
  157. },
  158. { },
  159. };
  160. #define LTC2632_CHANNEL(_chan, _bits) { \
  161. .type = IIO_VOLTAGE, \
  162. .indexed = 1, \
  163. .output = 1, \
  164. .channel = (_chan), \
  165. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  166. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  167. .address = (_chan), \
  168. .scan_type = { \
  169. .realbits = (_bits), \
  170. .shift = 16 - (_bits), \
  171. }, \
  172. .ext_info = ltc2632_ext_info, \
  173. }
  174. #define DECLARE_LTC2632_CHANNELS(_name, _bits) \
  175. const struct iio_chan_spec _name ## _channels[] = { \
  176. LTC2632_CHANNEL(0, _bits), \
  177. LTC2632_CHANNEL(1, _bits), \
  178. LTC2632_CHANNEL(2, _bits), \
  179. LTC2632_CHANNEL(3, _bits), \
  180. LTC2632_CHANNEL(4, _bits), \
  181. LTC2632_CHANNEL(5, _bits), \
  182. LTC2632_CHANNEL(6, _bits), \
  183. LTC2632_CHANNEL(7, _bits), \
  184. }
  185. static DECLARE_LTC2632_CHANNELS(ltc2632x12, 12);
  186. static DECLARE_LTC2632_CHANNELS(ltc2632x10, 10);
  187. static DECLARE_LTC2632_CHANNELS(ltc2632x8, 8);
  188. static const struct ltc2632_chip_info ltc2632_chip_info_tbl[] = {
  189. [ID_LTC2632L12] = {
  190. .channels = ltc2632x12_channels,
  191. .num_channels = 2,
  192. .vref_mv = 2500,
  193. },
  194. [ID_LTC2632L10] = {
  195. .channels = ltc2632x10_channels,
  196. .num_channels = 2,
  197. .vref_mv = 2500,
  198. },
  199. [ID_LTC2632L8] = {
  200. .channels = ltc2632x8_channels,
  201. .num_channels = 2,
  202. .vref_mv = 2500,
  203. },
  204. [ID_LTC2632H12] = {
  205. .channels = ltc2632x12_channels,
  206. .num_channels = 2,
  207. .vref_mv = 4096,
  208. },
  209. [ID_LTC2632H10] = {
  210. .channels = ltc2632x10_channels,
  211. .num_channels = 2,
  212. .vref_mv = 4096,
  213. },
  214. [ID_LTC2632H8] = {
  215. .channels = ltc2632x8_channels,
  216. .num_channels = 2,
  217. .vref_mv = 4096,
  218. },
  219. [ID_LTC2634L12] = {
  220. .channels = ltc2632x12_channels,
  221. .num_channels = 4,
  222. .vref_mv = 2500,
  223. },
  224. [ID_LTC2634L10] = {
  225. .channels = ltc2632x10_channels,
  226. .num_channels = 4,
  227. .vref_mv = 2500,
  228. },
  229. [ID_LTC2634L8] = {
  230. .channels = ltc2632x8_channels,
  231. .num_channels = 4,
  232. .vref_mv = 2500,
  233. },
  234. [ID_LTC2634H12] = {
  235. .channels = ltc2632x12_channels,
  236. .num_channels = 4,
  237. .vref_mv = 4096,
  238. },
  239. [ID_LTC2634H10] = {
  240. .channels = ltc2632x10_channels,
  241. .num_channels = 4,
  242. .vref_mv = 4096,
  243. },
  244. [ID_LTC2634H8] = {
  245. .channels = ltc2632x8_channels,
  246. .num_channels = 4,
  247. .vref_mv = 4096,
  248. },
  249. [ID_LTC2636L12] = {
  250. .channels = ltc2632x12_channels,
  251. .num_channels = 8,
  252. .vref_mv = 2500,
  253. },
  254. [ID_LTC2636L10] = {
  255. .channels = ltc2632x10_channels,
  256. .num_channels = 8,
  257. .vref_mv = 2500,
  258. },
  259. [ID_LTC2636L8] = {
  260. .channels = ltc2632x8_channels,
  261. .num_channels = 8,
  262. .vref_mv = 2500,
  263. },
  264. [ID_LTC2636H12] = {
  265. .channels = ltc2632x12_channels,
  266. .num_channels = 8,
  267. .vref_mv = 4096,
  268. },
  269. [ID_LTC2636H10] = {
  270. .channels = ltc2632x10_channels,
  271. .num_channels = 8,
  272. .vref_mv = 4096,
  273. },
  274. [ID_LTC2636H8] = {
  275. .channels = ltc2632x8_channels,
  276. .num_channels = 8,
  277. .vref_mv = 4096,
  278. },
  279. };
  280. static int ltc2632_probe(struct spi_device *spi)
  281. {
  282. struct ltc2632_state *st;
  283. struct iio_dev *indio_dev;
  284. struct ltc2632_chip_info *chip_info;
  285. int ret;
  286. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  287. if (!indio_dev)
  288. return -ENOMEM;
  289. st = iio_priv(indio_dev);
  290. spi_set_drvdata(spi, indio_dev);
  291. st->spi_dev = spi;
  292. chip_info = (struct ltc2632_chip_info *)
  293. spi_get_device_id(spi)->driver_data;
  294. st->vref_reg = devm_regulator_get_optional(&spi->dev, "vref");
  295. if (PTR_ERR(st->vref_reg) == -ENODEV) {
  296. /* use internal reference voltage */
  297. st->vref_reg = NULL;
  298. st->vref_mv = chip_info->vref_mv;
  299. ret = ltc2632_spi_write(spi, LTC2632_CMD_INTERNAL_REFER,
  300. 0, 0, 0);
  301. if (ret) {
  302. dev_err(&spi->dev,
  303. "Set internal reference command failed, %d\n",
  304. ret);
  305. return ret;
  306. }
  307. } else if (IS_ERR(st->vref_reg)) {
  308. dev_err(&spi->dev,
  309. "Error getting voltage reference regulator\n");
  310. return PTR_ERR(st->vref_reg);
  311. } else {
  312. /* use external reference voltage */
  313. ret = regulator_enable(st->vref_reg);
  314. if (ret) {
  315. dev_err(&spi->dev,
  316. "enable reference regulator failed, %d\n",
  317. ret);
  318. return ret;
  319. }
  320. st->vref_mv = regulator_get_voltage(st->vref_reg) / 1000;
  321. ret = ltc2632_spi_write(spi, LTC2632_CMD_EXTERNAL_REFER,
  322. 0, 0, 0);
  323. if (ret) {
  324. dev_err(&spi->dev,
  325. "Set external reference command failed, %d\n",
  326. ret);
  327. return ret;
  328. }
  329. }
  330. indio_dev->name = fwnode_get_name(dev_fwnode(&spi->dev)) ?: spi_get_device_id(spi)->name;
  331. indio_dev->info = &ltc2632_info;
  332. indio_dev->modes = INDIO_DIRECT_MODE;
  333. indio_dev->channels = chip_info->channels;
  334. indio_dev->num_channels = chip_info->num_channels;
  335. return iio_device_register(indio_dev);
  336. }
  337. static void ltc2632_remove(struct spi_device *spi)
  338. {
  339. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  340. struct ltc2632_state *st = iio_priv(indio_dev);
  341. iio_device_unregister(indio_dev);
  342. if (st->vref_reg)
  343. regulator_disable(st->vref_reg);
  344. }
  345. static const struct spi_device_id ltc2632_id[] = {
  346. { "ltc2632-l12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632L12] },
  347. { "ltc2632-l10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632L10] },
  348. { "ltc2632-l8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632L8] },
  349. { "ltc2632-h12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H12] },
  350. { "ltc2632-h10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H10] },
  351. { "ltc2632-h8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H8] },
  352. { "ltc2634-l12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634L12] },
  353. { "ltc2634-l10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634L10] },
  354. { "ltc2634-l8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634L8] },
  355. { "ltc2634-h12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634H12] },
  356. { "ltc2634-h10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634H10] },
  357. { "ltc2634-h8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634H8] },
  358. { "ltc2636-l12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636L12] },
  359. { "ltc2636-l10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636L10] },
  360. { "ltc2636-l8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636L8] },
  361. { "ltc2636-h12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636H12] },
  362. { "ltc2636-h10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636H10] },
  363. { "ltc2636-h8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636H8] },
  364. {}
  365. };
  366. MODULE_DEVICE_TABLE(spi, ltc2632_id);
  367. static const struct of_device_id ltc2632_of_match[] = {
  368. {
  369. .compatible = "lltc,ltc2632-l12",
  370. .data = &ltc2632_chip_info_tbl[ID_LTC2632L12]
  371. }, {
  372. .compatible = "lltc,ltc2632-l10",
  373. .data = &ltc2632_chip_info_tbl[ID_LTC2632L10]
  374. }, {
  375. .compatible = "lltc,ltc2632-l8",
  376. .data = &ltc2632_chip_info_tbl[ID_LTC2632L8]
  377. }, {
  378. .compatible = "lltc,ltc2632-h12",
  379. .data = &ltc2632_chip_info_tbl[ID_LTC2632H12]
  380. }, {
  381. .compatible = "lltc,ltc2632-h10",
  382. .data = &ltc2632_chip_info_tbl[ID_LTC2632H10]
  383. }, {
  384. .compatible = "lltc,ltc2632-h8",
  385. .data = &ltc2632_chip_info_tbl[ID_LTC2632H8]
  386. }, {
  387. .compatible = "lltc,ltc2634-l12",
  388. .data = &ltc2632_chip_info_tbl[ID_LTC2634L12]
  389. }, {
  390. .compatible = "lltc,ltc2634-l10",
  391. .data = &ltc2632_chip_info_tbl[ID_LTC2634L10]
  392. }, {
  393. .compatible = "lltc,ltc2634-l8",
  394. .data = &ltc2632_chip_info_tbl[ID_LTC2634L8]
  395. }, {
  396. .compatible = "lltc,ltc2634-h12",
  397. .data = &ltc2632_chip_info_tbl[ID_LTC2634H12]
  398. }, {
  399. .compatible = "lltc,ltc2634-h10",
  400. .data = &ltc2632_chip_info_tbl[ID_LTC2634H10]
  401. }, {
  402. .compatible = "lltc,ltc2634-h8",
  403. .data = &ltc2632_chip_info_tbl[ID_LTC2634H8]
  404. }, {
  405. .compatible = "lltc,ltc2636-l12",
  406. .data = &ltc2632_chip_info_tbl[ID_LTC2636L12]
  407. }, {
  408. .compatible = "lltc,ltc2636-l10",
  409. .data = &ltc2632_chip_info_tbl[ID_LTC2636L10]
  410. }, {
  411. .compatible = "lltc,ltc2636-l8",
  412. .data = &ltc2632_chip_info_tbl[ID_LTC2636L8]
  413. }, {
  414. .compatible = "lltc,ltc2636-h12",
  415. .data = &ltc2632_chip_info_tbl[ID_LTC2636H12]
  416. }, {
  417. .compatible = "lltc,ltc2636-h10",
  418. .data = &ltc2632_chip_info_tbl[ID_LTC2636H10]
  419. }, {
  420. .compatible = "lltc,ltc2636-h8",
  421. .data = &ltc2632_chip_info_tbl[ID_LTC2636H8]
  422. },
  423. {}
  424. };
  425. MODULE_DEVICE_TABLE(of, ltc2632_of_match);
  426. static struct spi_driver ltc2632_driver = {
  427. .driver = {
  428. .name = "ltc2632",
  429. .of_match_table = ltc2632_of_match,
  430. },
  431. .probe = ltc2632_probe,
  432. .remove = ltc2632_remove,
  433. .id_table = ltc2632_id,
  434. };
  435. module_spi_driver(ltc2632_driver);
  436. MODULE_AUTHOR("Maxime Roussin-Belanger <[email protected]>");
  437. MODULE_DESCRIPTION("LTC2632 DAC SPI driver");
  438. MODULE_LICENSE("GPL v2");