ad5446.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * AD5446 SPI DAC driver
  3. *
  4. * Copyright 2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/list.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/err.h>
  19. #include <linux/module.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #define MODE_PWRDWN_1k 0x1
  23. #define MODE_PWRDWN_100k 0x2
  24. #define MODE_PWRDWN_TRISTATE 0x3
  25. /**
  26. * struct ad5446_state - driver instance specific data
  27. * @spi: spi_device
  28. * @chip_info: chip model specific constants, available modes etc
  29. * @reg: supply regulator
  30. * @vref_mv: actual reference voltage used
  31. */
  32. struct ad5446_state {
  33. struct device *dev;
  34. const struct ad5446_chip_info *chip_info;
  35. struct regulator *reg;
  36. unsigned short vref_mv;
  37. unsigned cached_val;
  38. unsigned pwr_down_mode;
  39. unsigned pwr_down;
  40. };
  41. /**
  42. * struct ad5446_chip_info - chip specific information
  43. * @channel: channel spec for the DAC
  44. * @int_vref_mv: AD5620/40/60: the internal reference voltage
  45. * @write: chip specific helper function to write to the register
  46. */
  47. struct ad5446_chip_info {
  48. struct iio_chan_spec channel;
  49. u16 int_vref_mv;
  50. int (*write)(struct ad5446_state *st, unsigned val);
  51. };
  52. static const char * const ad5446_powerdown_modes[] = {
  53. "1kohm_to_gnd", "100kohm_to_gnd", "three_state"
  54. };
  55. static int ad5446_set_powerdown_mode(struct iio_dev *indio_dev,
  56. const struct iio_chan_spec *chan, unsigned int mode)
  57. {
  58. struct ad5446_state *st = iio_priv(indio_dev);
  59. st->pwr_down_mode = mode + 1;
  60. return 0;
  61. }
  62. static int ad5446_get_powerdown_mode(struct iio_dev *indio_dev,
  63. const struct iio_chan_spec *chan)
  64. {
  65. struct ad5446_state *st = iio_priv(indio_dev);
  66. return st->pwr_down_mode - 1;
  67. }
  68. static const struct iio_enum ad5446_powerdown_mode_enum = {
  69. .items = ad5446_powerdown_modes,
  70. .num_items = ARRAY_SIZE(ad5446_powerdown_modes),
  71. .get = ad5446_get_powerdown_mode,
  72. .set = ad5446_set_powerdown_mode,
  73. };
  74. static ssize_t ad5446_read_dac_powerdown(struct iio_dev *indio_dev,
  75. uintptr_t private,
  76. const struct iio_chan_spec *chan,
  77. char *buf)
  78. {
  79. struct ad5446_state *st = iio_priv(indio_dev);
  80. return sprintf(buf, "%d\n", st->pwr_down);
  81. }
  82. static ssize_t ad5446_write_dac_powerdown(struct iio_dev *indio_dev,
  83. uintptr_t private,
  84. const struct iio_chan_spec *chan,
  85. const char *buf, size_t len)
  86. {
  87. struct ad5446_state *st = iio_priv(indio_dev);
  88. unsigned int shift;
  89. unsigned int val;
  90. bool powerdown;
  91. int ret;
  92. ret = strtobool(buf, &powerdown);
  93. if (ret)
  94. return ret;
  95. mutex_lock(&indio_dev->mlock);
  96. st->pwr_down = powerdown;
  97. if (st->pwr_down) {
  98. shift = chan->scan_type.realbits + chan->scan_type.shift;
  99. val = st->pwr_down_mode << shift;
  100. } else {
  101. val = st->cached_val;
  102. }
  103. ret = st->chip_info->write(st, val);
  104. mutex_unlock(&indio_dev->mlock);
  105. return ret ? ret : len;
  106. }
  107. static const struct iio_chan_spec_ext_info ad5446_ext_info_powerdown[] = {
  108. {
  109. .name = "powerdown",
  110. .read = ad5446_read_dac_powerdown,
  111. .write = ad5446_write_dac_powerdown,
  112. .shared = IIO_SEPARATE,
  113. },
  114. IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5446_powerdown_mode_enum),
  115. IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum),
  116. { },
  117. };
  118. #define _AD5446_CHANNEL(bits, storage, _shift, ext) { \
  119. .type = IIO_VOLTAGE, \
  120. .indexed = 1, \
  121. .output = 1, \
  122. .channel = 0, \
  123. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  124. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  125. .scan_type = { \
  126. .sign = 'u', \
  127. .realbits = (bits), \
  128. .storagebits = (storage), \
  129. .shift = (_shift), \
  130. }, \
  131. .ext_info = (ext), \
  132. }
  133. #define AD5446_CHANNEL(bits, storage, shift) \
  134. _AD5446_CHANNEL(bits, storage, shift, NULL)
  135. #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \
  136. _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown)
  137. static int ad5446_read_raw(struct iio_dev *indio_dev,
  138. struct iio_chan_spec const *chan,
  139. int *val,
  140. int *val2,
  141. long m)
  142. {
  143. struct ad5446_state *st = iio_priv(indio_dev);
  144. switch (m) {
  145. case IIO_CHAN_INFO_RAW:
  146. *val = st->cached_val;
  147. return IIO_VAL_INT;
  148. case IIO_CHAN_INFO_SCALE:
  149. *val = st->vref_mv;
  150. *val2 = chan->scan_type.realbits;
  151. return IIO_VAL_FRACTIONAL_LOG2;
  152. }
  153. return -EINVAL;
  154. }
  155. static int ad5446_write_raw(struct iio_dev *indio_dev,
  156. struct iio_chan_spec const *chan,
  157. int val,
  158. int val2,
  159. long mask)
  160. {
  161. struct ad5446_state *st = iio_priv(indio_dev);
  162. int ret = 0;
  163. switch (mask) {
  164. case IIO_CHAN_INFO_RAW:
  165. if (val >= (1 << chan->scan_type.realbits) || val < 0)
  166. return -EINVAL;
  167. val <<= chan->scan_type.shift;
  168. mutex_lock(&indio_dev->mlock);
  169. st->cached_val = val;
  170. if (!st->pwr_down)
  171. ret = st->chip_info->write(st, val);
  172. mutex_unlock(&indio_dev->mlock);
  173. break;
  174. default:
  175. ret = -EINVAL;
  176. }
  177. return ret;
  178. }
  179. static const struct iio_info ad5446_info = {
  180. .read_raw = ad5446_read_raw,
  181. .write_raw = ad5446_write_raw,
  182. .driver_module = THIS_MODULE,
  183. };
  184. static int ad5446_probe(struct device *dev, const char *name,
  185. const struct ad5446_chip_info *chip_info)
  186. {
  187. struct ad5446_state *st;
  188. struct iio_dev *indio_dev;
  189. struct regulator *reg;
  190. int ret, voltage_uv = 0;
  191. reg = devm_regulator_get(dev, "vcc");
  192. if (!IS_ERR(reg)) {
  193. ret = regulator_enable(reg);
  194. if (ret)
  195. return ret;
  196. ret = regulator_get_voltage(reg);
  197. if (ret < 0)
  198. goto error_disable_reg;
  199. voltage_uv = ret;
  200. }
  201. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  202. if (indio_dev == NULL) {
  203. ret = -ENOMEM;
  204. goto error_disable_reg;
  205. }
  206. st = iio_priv(indio_dev);
  207. st->chip_info = chip_info;
  208. dev_set_drvdata(dev, indio_dev);
  209. st->reg = reg;
  210. st->dev = dev;
  211. /* Establish that the iio_dev is a child of the device */
  212. indio_dev->dev.parent = dev;
  213. indio_dev->name = name;
  214. indio_dev->info = &ad5446_info;
  215. indio_dev->modes = INDIO_DIRECT_MODE;
  216. indio_dev->channels = &st->chip_info->channel;
  217. indio_dev->num_channels = 1;
  218. st->pwr_down_mode = MODE_PWRDWN_1k;
  219. if (st->chip_info->int_vref_mv)
  220. st->vref_mv = st->chip_info->int_vref_mv;
  221. else if (voltage_uv)
  222. st->vref_mv = voltage_uv / 1000;
  223. else
  224. dev_warn(dev, "reference voltage unspecified\n");
  225. ret = iio_device_register(indio_dev);
  226. if (ret)
  227. goto error_disable_reg;
  228. return 0;
  229. error_disable_reg:
  230. if (!IS_ERR(reg))
  231. regulator_disable(reg);
  232. return ret;
  233. }
  234. static int ad5446_remove(struct device *dev)
  235. {
  236. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  237. struct ad5446_state *st = iio_priv(indio_dev);
  238. iio_device_unregister(indio_dev);
  239. if (!IS_ERR(st->reg))
  240. regulator_disable(st->reg);
  241. return 0;
  242. }
  243. #if IS_ENABLED(CONFIG_SPI_MASTER)
  244. static int ad5446_write(struct ad5446_state *st, unsigned val)
  245. {
  246. struct spi_device *spi = to_spi_device(st->dev);
  247. __be16 data = cpu_to_be16(val);
  248. return spi_write(spi, &data, sizeof(data));
  249. }
  250. static int ad5660_write(struct ad5446_state *st, unsigned val)
  251. {
  252. struct spi_device *spi = to_spi_device(st->dev);
  253. uint8_t data[3];
  254. data[0] = (val >> 16) & 0xFF;
  255. data[1] = (val >> 8) & 0xFF;
  256. data[2] = val & 0xFF;
  257. return spi_write(spi, data, sizeof(data));
  258. }
  259. /**
  260. * ad5446_supported_spi_device_ids:
  261. * The AD5620/40/60 parts are available in different fixed internal reference
  262. * voltage options. The actual part numbers may look differently
  263. * (and a bit cryptic), however this style is used to make clear which
  264. * parts are supported here.
  265. */
  266. enum ad5446_supported_spi_device_ids {
  267. ID_AD5300,
  268. ID_AD5310,
  269. ID_AD5320,
  270. ID_AD5444,
  271. ID_AD5446,
  272. ID_AD5450,
  273. ID_AD5451,
  274. ID_AD5541A,
  275. ID_AD5512A,
  276. ID_AD5553,
  277. ID_AD5601,
  278. ID_AD5611,
  279. ID_AD5621,
  280. ID_AD5641,
  281. ID_AD5620_2500,
  282. ID_AD5620_1250,
  283. ID_AD5640_2500,
  284. ID_AD5640_1250,
  285. ID_AD5660_2500,
  286. ID_AD5660_1250,
  287. ID_AD5662,
  288. };
  289. static const struct ad5446_chip_info ad5446_spi_chip_info[] = {
  290. [ID_AD5300] = {
  291. .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
  292. .write = ad5446_write,
  293. },
  294. [ID_AD5310] = {
  295. .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
  296. .write = ad5446_write,
  297. },
  298. [ID_AD5320] = {
  299. .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
  300. .write = ad5446_write,
  301. },
  302. [ID_AD5444] = {
  303. .channel = AD5446_CHANNEL(12, 16, 2),
  304. .write = ad5446_write,
  305. },
  306. [ID_AD5446] = {
  307. .channel = AD5446_CHANNEL(14, 16, 0),
  308. .write = ad5446_write,
  309. },
  310. [ID_AD5450] = {
  311. .channel = AD5446_CHANNEL(8, 16, 6),
  312. .write = ad5446_write,
  313. },
  314. [ID_AD5451] = {
  315. .channel = AD5446_CHANNEL(10, 16, 4),
  316. .write = ad5446_write,
  317. },
  318. [ID_AD5541A] = {
  319. .channel = AD5446_CHANNEL(16, 16, 0),
  320. .write = ad5446_write,
  321. },
  322. [ID_AD5512A] = {
  323. .channel = AD5446_CHANNEL(12, 16, 4),
  324. .write = ad5446_write,
  325. },
  326. [ID_AD5553] = {
  327. .channel = AD5446_CHANNEL(14, 16, 0),
  328. .write = ad5446_write,
  329. },
  330. [ID_AD5601] = {
  331. .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6),
  332. .write = ad5446_write,
  333. },
  334. [ID_AD5611] = {
  335. .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4),
  336. .write = ad5446_write,
  337. },
  338. [ID_AD5621] = {
  339. .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
  340. .write = ad5446_write,
  341. },
  342. [ID_AD5641] = {
  343. .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
  344. .write = ad5446_write,
  345. },
  346. [ID_AD5620_2500] = {
  347. .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
  348. .int_vref_mv = 2500,
  349. .write = ad5446_write,
  350. },
  351. [ID_AD5620_1250] = {
  352. .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
  353. .int_vref_mv = 1250,
  354. .write = ad5446_write,
  355. },
  356. [ID_AD5640_2500] = {
  357. .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
  358. .int_vref_mv = 2500,
  359. .write = ad5446_write,
  360. },
  361. [ID_AD5640_1250] = {
  362. .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
  363. .int_vref_mv = 1250,
  364. .write = ad5446_write,
  365. },
  366. [ID_AD5660_2500] = {
  367. .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
  368. .int_vref_mv = 2500,
  369. .write = ad5660_write,
  370. },
  371. [ID_AD5660_1250] = {
  372. .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
  373. .int_vref_mv = 1250,
  374. .write = ad5660_write,
  375. },
  376. [ID_AD5662] = {
  377. .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
  378. .write = ad5660_write,
  379. },
  380. };
  381. static const struct spi_device_id ad5446_spi_ids[] = {
  382. {"ad5300", ID_AD5300},
  383. {"ad5310", ID_AD5310},
  384. {"ad5320", ID_AD5320},
  385. {"ad5444", ID_AD5444},
  386. {"ad5446", ID_AD5446},
  387. {"ad5450", ID_AD5450},
  388. {"ad5451", ID_AD5451},
  389. {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */
  390. {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */
  391. {"ad5512a", ID_AD5512A},
  392. {"ad5541a", ID_AD5541A},
  393. {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */
  394. {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */
  395. {"ad5553", ID_AD5553},
  396. {"ad5601", ID_AD5601},
  397. {"ad5611", ID_AD5611},
  398. {"ad5621", ID_AD5621},
  399. {"ad5641", ID_AD5641},
  400. {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
  401. {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
  402. {"ad5640-2500", ID_AD5640_2500},
  403. {"ad5640-1250", ID_AD5640_1250},
  404. {"ad5660-2500", ID_AD5660_2500},
  405. {"ad5660-1250", ID_AD5660_1250},
  406. {"ad5662", ID_AD5662},
  407. {}
  408. };
  409. MODULE_DEVICE_TABLE(spi, ad5446_spi_ids);
  410. static int ad5446_spi_probe(struct spi_device *spi)
  411. {
  412. const struct spi_device_id *id = spi_get_device_id(spi);
  413. return ad5446_probe(&spi->dev, id->name,
  414. &ad5446_spi_chip_info[id->driver_data]);
  415. }
  416. static int ad5446_spi_remove(struct spi_device *spi)
  417. {
  418. return ad5446_remove(&spi->dev);
  419. }
  420. static struct spi_driver ad5446_spi_driver = {
  421. .driver = {
  422. .name = "ad5446",
  423. },
  424. .probe = ad5446_spi_probe,
  425. .remove = ad5446_spi_remove,
  426. .id_table = ad5446_spi_ids,
  427. };
  428. static int __init ad5446_spi_register_driver(void)
  429. {
  430. return spi_register_driver(&ad5446_spi_driver);
  431. }
  432. static void ad5446_spi_unregister_driver(void)
  433. {
  434. spi_unregister_driver(&ad5446_spi_driver);
  435. }
  436. #else
  437. static inline int ad5446_spi_register_driver(void) { return 0; }
  438. static inline void ad5446_spi_unregister_driver(void) { }
  439. #endif
  440. #if IS_ENABLED(CONFIG_I2C)
  441. static int ad5622_write(struct ad5446_state *st, unsigned val)
  442. {
  443. struct i2c_client *client = to_i2c_client(st->dev);
  444. __be16 data = cpu_to_be16(val);
  445. return i2c_master_send(client, (char *)&data, sizeof(data));
  446. }
  447. /**
  448. * ad5446_supported_i2c_device_ids:
  449. * The AD5620/40/60 parts are available in different fixed internal reference
  450. * voltage options. The actual part numbers may look differently
  451. * (and a bit cryptic), however this style is used to make clear which
  452. * parts are supported here.
  453. */
  454. enum ad5446_supported_i2c_device_ids {
  455. ID_AD5602,
  456. ID_AD5612,
  457. ID_AD5622,
  458. };
  459. static const struct ad5446_chip_info ad5446_i2c_chip_info[] = {
  460. [ID_AD5602] = {
  461. .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
  462. .write = ad5622_write,
  463. },
  464. [ID_AD5612] = {
  465. .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
  466. .write = ad5622_write,
  467. },
  468. [ID_AD5622] = {
  469. .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
  470. .write = ad5622_write,
  471. },
  472. };
  473. static int ad5446_i2c_probe(struct i2c_client *i2c,
  474. const struct i2c_device_id *id)
  475. {
  476. return ad5446_probe(&i2c->dev, id->name,
  477. &ad5446_i2c_chip_info[id->driver_data]);
  478. }
  479. static int ad5446_i2c_remove(struct i2c_client *i2c)
  480. {
  481. return ad5446_remove(&i2c->dev);
  482. }
  483. static const struct i2c_device_id ad5446_i2c_ids[] = {
  484. {"ad5301", ID_AD5602},
  485. {"ad5311", ID_AD5612},
  486. {"ad5321", ID_AD5622},
  487. {"ad5602", ID_AD5602},
  488. {"ad5612", ID_AD5612},
  489. {"ad5622", ID_AD5622},
  490. {}
  491. };
  492. MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids);
  493. static struct i2c_driver ad5446_i2c_driver = {
  494. .driver = {
  495. .name = "ad5446",
  496. },
  497. .probe = ad5446_i2c_probe,
  498. .remove = ad5446_i2c_remove,
  499. .id_table = ad5446_i2c_ids,
  500. };
  501. static int __init ad5446_i2c_register_driver(void)
  502. {
  503. return i2c_add_driver(&ad5446_i2c_driver);
  504. }
  505. static void __exit ad5446_i2c_unregister_driver(void)
  506. {
  507. i2c_del_driver(&ad5446_i2c_driver);
  508. }
  509. #else
  510. static inline int ad5446_i2c_register_driver(void) { return 0; }
  511. static inline void ad5446_i2c_unregister_driver(void) { }
  512. #endif
  513. static int __init ad5446_init(void)
  514. {
  515. int ret;
  516. ret = ad5446_spi_register_driver();
  517. if (ret)
  518. return ret;
  519. ret = ad5446_i2c_register_driver();
  520. if (ret) {
  521. ad5446_spi_unregister_driver();
  522. return ret;
  523. }
  524. return 0;
  525. }
  526. module_init(ad5446_init);
  527. static void __exit ad5446_exit(void)
  528. {
  529. ad5446_i2c_unregister_driver();
  530. ad5446_spi_unregister_driver();
  531. }
  532. module_exit(ad5446_exit);
  533. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  534. MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
  535. MODULE_LICENSE("GPL v2");