ad5380.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * Analog devices AD5380, AD5381, AD5382, AD5383, AD5390, AD5391, AD5392
  3. * multi-channel Digital to Analog Converters driver
  4. *
  5. * Copyright 2011 Analog Devices Inc.
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/i2c.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/regmap.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #define AD5380_REG_DATA(x) (((x) << 2) | 3)
  22. #define AD5380_REG_OFFSET(x) (((x) << 2) | 2)
  23. #define AD5380_REG_GAIN(x) (((x) << 2) | 1)
  24. #define AD5380_REG_SF_PWR_DOWN (8 << 2)
  25. #define AD5380_REG_SF_PWR_UP (9 << 2)
  26. #define AD5380_REG_SF_CTRL (12 << 2)
  27. #define AD5380_CTRL_PWR_DOWN_MODE_OFFSET 13
  28. #define AD5380_CTRL_INT_VREF_2V5 BIT(12)
  29. #define AD5380_CTRL_INT_VREF_EN BIT(10)
  30. /**
  31. * struct ad5380_chip_info - chip specific information
  32. * @channel_template: channel specification template
  33. * @num_channels: number of channels
  34. * @int_vref: internal vref in uV
  35. */
  36. struct ad5380_chip_info {
  37. struct iio_chan_spec channel_template;
  38. unsigned int num_channels;
  39. unsigned int int_vref;
  40. };
  41. /**
  42. * struct ad5380_state - driver instance specific data
  43. * @regmap: regmap instance used by the device
  44. * @chip_info: chip model specific constants, available modes etc
  45. * @vref_reg: vref supply regulator
  46. * @vref: actual reference voltage used in uA
  47. * @pwr_down: whether the chip is currently in power down mode
  48. */
  49. struct ad5380_state {
  50. struct regmap *regmap;
  51. const struct ad5380_chip_info *chip_info;
  52. struct regulator *vref_reg;
  53. int vref;
  54. bool pwr_down;
  55. };
  56. enum ad5380_type {
  57. ID_AD5380_3,
  58. ID_AD5380_5,
  59. ID_AD5381_3,
  60. ID_AD5381_5,
  61. ID_AD5382_3,
  62. ID_AD5382_5,
  63. ID_AD5383_3,
  64. ID_AD5383_5,
  65. ID_AD5390_3,
  66. ID_AD5390_5,
  67. ID_AD5391_3,
  68. ID_AD5391_5,
  69. ID_AD5392_3,
  70. ID_AD5392_5,
  71. };
  72. static ssize_t ad5380_read_dac_powerdown(struct iio_dev *indio_dev,
  73. uintptr_t private, const struct iio_chan_spec *chan, char *buf)
  74. {
  75. struct ad5380_state *st = iio_priv(indio_dev);
  76. return sprintf(buf, "%d\n", st->pwr_down);
  77. }
  78. static ssize_t ad5380_write_dac_powerdown(struct iio_dev *indio_dev,
  79. uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
  80. size_t len)
  81. {
  82. struct ad5380_state *st = iio_priv(indio_dev);
  83. bool pwr_down;
  84. int ret;
  85. ret = strtobool(buf, &pwr_down);
  86. if (ret)
  87. return ret;
  88. mutex_lock(&indio_dev->mlock);
  89. if (pwr_down)
  90. ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_DOWN, 0);
  91. else
  92. ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_UP, 0);
  93. st->pwr_down = pwr_down;
  94. mutex_unlock(&indio_dev->mlock);
  95. return ret ? ret : len;
  96. }
  97. static const char * const ad5380_powerdown_modes[] = {
  98. "100kohm_to_gnd",
  99. "three_state",
  100. };
  101. static int ad5380_get_powerdown_mode(struct iio_dev *indio_dev,
  102. const struct iio_chan_spec *chan)
  103. {
  104. struct ad5380_state *st = iio_priv(indio_dev);
  105. unsigned int mode;
  106. int ret;
  107. ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode);
  108. if (ret)
  109. return ret;
  110. mode = (mode >> AD5380_CTRL_PWR_DOWN_MODE_OFFSET) & 1;
  111. return mode;
  112. }
  113. static int ad5380_set_powerdown_mode(struct iio_dev *indio_dev,
  114. const struct iio_chan_spec *chan, unsigned int mode)
  115. {
  116. struct ad5380_state *st = iio_priv(indio_dev);
  117. int ret;
  118. ret = regmap_update_bits(st->regmap, AD5380_REG_SF_CTRL,
  119. 1 << AD5380_CTRL_PWR_DOWN_MODE_OFFSET,
  120. mode << AD5380_CTRL_PWR_DOWN_MODE_OFFSET);
  121. return ret;
  122. }
  123. static const struct iio_enum ad5380_powerdown_mode_enum = {
  124. .items = ad5380_powerdown_modes,
  125. .num_items = ARRAY_SIZE(ad5380_powerdown_modes),
  126. .get = ad5380_get_powerdown_mode,
  127. .set = ad5380_set_powerdown_mode,
  128. };
  129. static unsigned int ad5380_info_to_reg(struct iio_chan_spec const *chan,
  130. long info)
  131. {
  132. switch (info) {
  133. case 0:
  134. return AD5380_REG_DATA(chan->address);
  135. case IIO_CHAN_INFO_CALIBBIAS:
  136. return AD5380_REG_OFFSET(chan->address);
  137. case IIO_CHAN_INFO_CALIBSCALE:
  138. return AD5380_REG_GAIN(chan->address);
  139. default:
  140. break;
  141. }
  142. return 0;
  143. }
  144. static int ad5380_write_raw(struct iio_dev *indio_dev,
  145. struct iio_chan_spec const *chan, int val, int val2, long info)
  146. {
  147. const unsigned int max_val = (1 << chan->scan_type.realbits);
  148. struct ad5380_state *st = iio_priv(indio_dev);
  149. switch (info) {
  150. case IIO_CHAN_INFO_RAW:
  151. case IIO_CHAN_INFO_CALIBSCALE:
  152. if (val >= max_val || val < 0)
  153. return -EINVAL;
  154. return regmap_write(st->regmap,
  155. ad5380_info_to_reg(chan, info),
  156. val << chan->scan_type.shift);
  157. case IIO_CHAN_INFO_CALIBBIAS:
  158. val += (1 << chan->scan_type.realbits) / 2;
  159. if (val >= max_val || val < 0)
  160. return -EINVAL;
  161. return regmap_write(st->regmap,
  162. AD5380_REG_OFFSET(chan->address),
  163. val << chan->scan_type.shift);
  164. default:
  165. break;
  166. }
  167. return -EINVAL;
  168. }
  169. static int ad5380_read_raw(struct iio_dev *indio_dev,
  170. struct iio_chan_spec const *chan, int *val, int *val2, long info)
  171. {
  172. struct ad5380_state *st = iio_priv(indio_dev);
  173. int ret;
  174. switch (info) {
  175. case IIO_CHAN_INFO_RAW:
  176. case IIO_CHAN_INFO_CALIBSCALE:
  177. ret = regmap_read(st->regmap, ad5380_info_to_reg(chan, info),
  178. val);
  179. if (ret)
  180. return ret;
  181. *val >>= chan->scan_type.shift;
  182. return IIO_VAL_INT;
  183. case IIO_CHAN_INFO_CALIBBIAS:
  184. ret = regmap_read(st->regmap, AD5380_REG_OFFSET(chan->address),
  185. val);
  186. if (ret)
  187. return ret;
  188. *val >>= chan->scan_type.shift;
  189. val -= (1 << chan->scan_type.realbits) / 2;
  190. return IIO_VAL_INT;
  191. case IIO_CHAN_INFO_SCALE:
  192. *val = 2 * st->vref;
  193. *val2 = chan->scan_type.realbits;
  194. return IIO_VAL_FRACTIONAL_LOG2;
  195. default:
  196. break;
  197. }
  198. return -EINVAL;
  199. }
  200. static const struct iio_info ad5380_info = {
  201. .read_raw = ad5380_read_raw,
  202. .write_raw = ad5380_write_raw,
  203. .driver_module = THIS_MODULE,
  204. };
  205. static struct iio_chan_spec_ext_info ad5380_ext_info[] = {
  206. {
  207. .name = "powerdown",
  208. .read = ad5380_read_dac_powerdown,
  209. .write = ad5380_write_dac_powerdown,
  210. .shared = IIO_SEPARATE,
  211. },
  212. IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
  213. &ad5380_powerdown_mode_enum),
  214. IIO_ENUM_AVAILABLE("powerdown_mode", &ad5380_powerdown_mode_enum),
  215. { },
  216. };
  217. #define AD5380_CHANNEL(_bits) { \
  218. .type = IIO_VOLTAGE, \
  219. .indexed = 1, \
  220. .output = 1, \
  221. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  222. BIT(IIO_CHAN_INFO_CALIBSCALE) | \
  223. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  224. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  225. .scan_type = { \
  226. .sign = 'u', \
  227. .realbits = (_bits), \
  228. .storagebits = 16, \
  229. .shift = 14 - (_bits), \
  230. }, \
  231. .ext_info = ad5380_ext_info, \
  232. }
  233. static const struct ad5380_chip_info ad5380_chip_info_tbl[] = {
  234. [ID_AD5380_3] = {
  235. .channel_template = AD5380_CHANNEL(14),
  236. .num_channels = 40,
  237. .int_vref = 1250,
  238. },
  239. [ID_AD5380_5] = {
  240. .channel_template = AD5380_CHANNEL(14),
  241. .num_channels = 40,
  242. .int_vref = 2500,
  243. },
  244. [ID_AD5381_3] = {
  245. .channel_template = AD5380_CHANNEL(12),
  246. .num_channels = 16,
  247. .int_vref = 1250,
  248. },
  249. [ID_AD5381_5] = {
  250. .channel_template = AD5380_CHANNEL(12),
  251. .num_channels = 16,
  252. .int_vref = 2500,
  253. },
  254. [ID_AD5382_3] = {
  255. .channel_template = AD5380_CHANNEL(14),
  256. .num_channels = 32,
  257. .int_vref = 1250,
  258. },
  259. [ID_AD5382_5] = {
  260. .channel_template = AD5380_CHANNEL(14),
  261. .num_channels = 32,
  262. .int_vref = 2500,
  263. },
  264. [ID_AD5383_3] = {
  265. .channel_template = AD5380_CHANNEL(12),
  266. .num_channels = 32,
  267. .int_vref = 1250,
  268. },
  269. [ID_AD5383_5] = {
  270. .channel_template = AD5380_CHANNEL(12),
  271. .num_channels = 32,
  272. .int_vref = 2500,
  273. },
  274. [ID_AD5390_3] = {
  275. .channel_template = AD5380_CHANNEL(14),
  276. .num_channels = 16,
  277. .int_vref = 1250,
  278. },
  279. [ID_AD5390_5] = {
  280. .channel_template = AD5380_CHANNEL(14),
  281. .num_channels = 16,
  282. .int_vref = 2500,
  283. },
  284. [ID_AD5391_3] = {
  285. .channel_template = AD5380_CHANNEL(12),
  286. .num_channels = 16,
  287. .int_vref = 1250,
  288. },
  289. [ID_AD5391_5] = {
  290. .channel_template = AD5380_CHANNEL(12),
  291. .num_channels = 16,
  292. .int_vref = 2500,
  293. },
  294. [ID_AD5392_3] = {
  295. .channel_template = AD5380_CHANNEL(14),
  296. .num_channels = 8,
  297. .int_vref = 1250,
  298. },
  299. [ID_AD5392_5] = {
  300. .channel_template = AD5380_CHANNEL(14),
  301. .num_channels = 8,
  302. .int_vref = 2500,
  303. },
  304. };
  305. static int ad5380_alloc_channels(struct iio_dev *indio_dev)
  306. {
  307. struct ad5380_state *st = iio_priv(indio_dev);
  308. struct iio_chan_spec *channels;
  309. unsigned int i;
  310. channels = kcalloc(st->chip_info->num_channels,
  311. sizeof(struct iio_chan_spec), GFP_KERNEL);
  312. if (!channels)
  313. return -ENOMEM;
  314. for (i = 0; i < st->chip_info->num_channels; ++i) {
  315. channels[i] = st->chip_info->channel_template;
  316. channels[i].channel = i;
  317. channels[i].address = i;
  318. }
  319. indio_dev->channels = channels;
  320. return 0;
  321. }
  322. static int ad5380_probe(struct device *dev, struct regmap *regmap,
  323. enum ad5380_type type, const char *name)
  324. {
  325. struct iio_dev *indio_dev;
  326. struct ad5380_state *st;
  327. unsigned int ctrl = 0;
  328. int ret;
  329. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  330. if (indio_dev == NULL) {
  331. dev_err(dev, "Failed to allocate iio device\n");
  332. return -ENOMEM;
  333. }
  334. st = iio_priv(indio_dev);
  335. dev_set_drvdata(dev, indio_dev);
  336. st->chip_info = &ad5380_chip_info_tbl[type];
  337. st->regmap = regmap;
  338. indio_dev->dev.parent = dev;
  339. indio_dev->name = name;
  340. indio_dev->info = &ad5380_info;
  341. indio_dev->modes = INDIO_DIRECT_MODE;
  342. indio_dev->num_channels = st->chip_info->num_channels;
  343. ret = ad5380_alloc_channels(indio_dev);
  344. if (ret) {
  345. dev_err(dev, "Failed to allocate channel spec: %d\n", ret);
  346. return ret;
  347. }
  348. if (st->chip_info->int_vref == 2500)
  349. ctrl |= AD5380_CTRL_INT_VREF_2V5;
  350. st->vref_reg = devm_regulator_get(dev, "vref");
  351. if (!IS_ERR(st->vref_reg)) {
  352. ret = regulator_enable(st->vref_reg);
  353. if (ret) {
  354. dev_err(dev, "Failed to enable vref regulators: %d\n",
  355. ret);
  356. goto error_free_reg;
  357. }
  358. ret = regulator_get_voltage(st->vref_reg);
  359. if (ret < 0)
  360. goto error_disable_reg;
  361. st->vref = ret / 1000;
  362. } else {
  363. st->vref = st->chip_info->int_vref;
  364. ctrl |= AD5380_CTRL_INT_VREF_EN;
  365. }
  366. ret = regmap_write(st->regmap, AD5380_REG_SF_CTRL, ctrl);
  367. if (ret) {
  368. dev_err(dev, "Failed to write to device: %d\n", ret);
  369. goto error_disable_reg;
  370. }
  371. ret = iio_device_register(indio_dev);
  372. if (ret) {
  373. dev_err(dev, "Failed to register iio device: %d\n", ret);
  374. goto error_disable_reg;
  375. }
  376. return 0;
  377. error_disable_reg:
  378. if (!IS_ERR(st->vref_reg))
  379. regulator_disable(st->vref_reg);
  380. error_free_reg:
  381. kfree(indio_dev->channels);
  382. return ret;
  383. }
  384. static int ad5380_remove(struct device *dev)
  385. {
  386. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  387. struct ad5380_state *st = iio_priv(indio_dev);
  388. iio_device_unregister(indio_dev);
  389. kfree(indio_dev->channels);
  390. if (!IS_ERR(st->vref_reg)) {
  391. regulator_disable(st->vref_reg);
  392. }
  393. return 0;
  394. }
  395. static bool ad5380_reg_false(struct device *dev, unsigned int reg)
  396. {
  397. return false;
  398. }
  399. static const struct regmap_config ad5380_regmap_config = {
  400. .reg_bits = 10,
  401. .val_bits = 14,
  402. .max_register = AD5380_REG_DATA(40),
  403. .cache_type = REGCACHE_RBTREE,
  404. .volatile_reg = ad5380_reg_false,
  405. .readable_reg = ad5380_reg_false,
  406. };
  407. #if IS_ENABLED(CONFIG_SPI_MASTER)
  408. static int ad5380_spi_probe(struct spi_device *spi)
  409. {
  410. const struct spi_device_id *id = spi_get_device_id(spi);
  411. struct regmap *regmap;
  412. regmap = devm_regmap_init_spi(spi, &ad5380_regmap_config);
  413. if (IS_ERR(regmap))
  414. return PTR_ERR(regmap);
  415. return ad5380_probe(&spi->dev, regmap, id->driver_data, id->name);
  416. }
  417. static int ad5380_spi_remove(struct spi_device *spi)
  418. {
  419. return ad5380_remove(&spi->dev);
  420. }
  421. static const struct spi_device_id ad5380_spi_ids[] = {
  422. { "ad5380-3", ID_AD5380_3 },
  423. { "ad5380-5", ID_AD5380_5 },
  424. { "ad5381-3", ID_AD5381_3 },
  425. { "ad5381-5", ID_AD5381_5 },
  426. { "ad5382-3", ID_AD5382_3 },
  427. { "ad5382-5", ID_AD5382_5 },
  428. { "ad5383-3", ID_AD5383_3 },
  429. { "ad5383-5", ID_AD5383_5 },
  430. { "ad5384-3", ID_AD5380_3 },
  431. { "ad5384-5", ID_AD5380_5 },
  432. { "ad5390-3", ID_AD5390_3 },
  433. { "ad5390-5", ID_AD5390_5 },
  434. { "ad5391-3", ID_AD5391_3 },
  435. { "ad5391-5", ID_AD5391_5 },
  436. { "ad5392-3", ID_AD5392_3 },
  437. { "ad5392-5", ID_AD5392_5 },
  438. { }
  439. };
  440. MODULE_DEVICE_TABLE(spi, ad5380_spi_ids);
  441. static struct spi_driver ad5380_spi_driver = {
  442. .driver = {
  443. .name = "ad5380",
  444. },
  445. .probe = ad5380_spi_probe,
  446. .remove = ad5380_spi_remove,
  447. .id_table = ad5380_spi_ids,
  448. };
  449. static inline int ad5380_spi_register_driver(void)
  450. {
  451. return spi_register_driver(&ad5380_spi_driver);
  452. }
  453. static inline void ad5380_spi_unregister_driver(void)
  454. {
  455. spi_unregister_driver(&ad5380_spi_driver);
  456. }
  457. #else
  458. static inline int ad5380_spi_register_driver(void)
  459. {
  460. return 0;
  461. }
  462. static inline void ad5380_spi_unregister_driver(void)
  463. {
  464. }
  465. #endif
  466. #if IS_ENABLED(CONFIG_I2C)
  467. static int ad5380_i2c_probe(struct i2c_client *i2c,
  468. const struct i2c_device_id *id)
  469. {
  470. struct regmap *regmap;
  471. regmap = devm_regmap_init_i2c(i2c, &ad5380_regmap_config);
  472. if (IS_ERR(regmap))
  473. return PTR_ERR(regmap);
  474. return ad5380_probe(&i2c->dev, regmap, id->driver_data, id->name);
  475. }
  476. static int ad5380_i2c_remove(struct i2c_client *i2c)
  477. {
  478. return ad5380_remove(&i2c->dev);
  479. }
  480. static const struct i2c_device_id ad5380_i2c_ids[] = {
  481. { "ad5380-3", ID_AD5380_3 },
  482. { "ad5380-5", ID_AD5380_5 },
  483. { "ad5381-3", ID_AD5381_3 },
  484. { "ad5381-5", ID_AD5381_5 },
  485. { "ad5382-3", ID_AD5382_3 },
  486. { "ad5382-5", ID_AD5382_5 },
  487. { "ad5383-3", ID_AD5383_3 },
  488. { "ad5383-5", ID_AD5383_5 },
  489. { "ad5384-3", ID_AD5380_3 },
  490. { "ad5384-5", ID_AD5380_5 },
  491. { "ad5390-3", ID_AD5390_3 },
  492. { "ad5390-5", ID_AD5390_5 },
  493. { "ad5391-3", ID_AD5391_3 },
  494. { "ad5391-5", ID_AD5391_5 },
  495. { "ad5392-3", ID_AD5392_3 },
  496. { "ad5392-5", ID_AD5392_5 },
  497. { }
  498. };
  499. MODULE_DEVICE_TABLE(i2c, ad5380_i2c_ids);
  500. static struct i2c_driver ad5380_i2c_driver = {
  501. .driver = {
  502. .name = "ad5380",
  503. },
  504. .probe = ad5380_i2c_probe,
  505. .remove = ad5380_i2c_remove,
  506. .id_table = ad5380_i2c_ids,
  507. };
  508. static inline int ad5380_i2c_register_driver(void)
  509. {
  510. return i2c_add_driver(&ad5380_i2c_driver);
  511. }
  512. static inline void ad5380_i2c_unregister_driver(void)
  513. {
  514. i2c_del_driver(&ad5380_i2c_driver);
  515. }
  516. #else
  517. static inline int ad5380_i2c_register_driver(void)
  518. {
  519. return 0;
  520. }
  521. static inline void ad5380_i2c_unregister_driver(void)
  522. {
  523. }
  524. #endif
  525. static int __init ad5380_spi_init(void)
  526. {
  527. int ret;
  528. ret = ad5380_spi_register_driver();
  529. if (ret)
  530. return ret;
  531. ret = ad5380_i2c_register_driver();
  532. if (ret) {
  533. ad5380_spi_unregister_driver();
  534. return ret;
  535. }
  536. return 0;
  537. }
  538. module_init(ad5380_spi_init);
  539. static void __exit ad5380_spi_exit(void)
  540. {
  541. ad5380_i2c_unregister_driver();
  542. ad5380_spi_unregister_driver();
  543. }
  544. module_exit(ad5380_spi_exit);
  545. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  546. MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC");
  547. MODULE_LICENSE("GPL v2");