ad5791.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * AD5760, AD5780, AD5781, AD5790, AD5791 Voltage Output Digital to Analog
  3. * Converter
  4. *
  5. * Copyright 2011 Analog Devices Inc.
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/fs.h>
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/slab.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/module.h>
  18. #include <linux/bitops.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/dac/ad5791.h>
  22. #define AD5791_DAC_MASK GENMASK(19, 0)
  23. #define AD5791_CMD_READ BIT(23)
  24. #define AD5791_CMD_WRITE 0
  25. #define AD5791_ADDR(addr) ((addr) << 20)
  26. /* Registers */
  27. #define AD5791_ADDR_NOOP 0
  28. #define AD5791_ADDR_DAC0 1
  29. #define AD5791_ADDR_CTRL 2
  30. #define AD5791_ADDR_CLRCODE 3
  31. #define AD5791_ADDR_SW_CTRL 4
  32. /* Control Register */
  33. #define AD5791_CTRL_RBUF BIT(1)
  34. #define AD5791_CTRL_OPGND BIT(2)
  35. #define AD5791_CTRL_DACTRI BIT(3)
  36. #define AD5791_CTRL_BIN2SC BIT(4)
  37. #define AD5791_CTRL_SDODIS BIT(5)
  38. #define AD5761_CTRL_LINCOMP(x) ((x) << 6)
  39. #define AD5791_LINCOMP_0_10 0
  40. #define AD5791_LINCOMP_10_12 1
  41. #define AD5791_LINCOMP_12_16 2
  42. #define AD5791_LINCOMP_16_19 3
  43. #define AD5791_LINCOMP_19_20 12
  44. #define AD5780_LINCOMP_0_10 0
  45. #define AD5780_LINCOMP_10_20 12
  46. /* Software Control Register */
  47. #define AD5791_SWCTRL_LDAC BIT(0)
  48. #define AD5791_SWCTRL_CLR BIT(1)
  49. #define AD5791_SWCTRL_RESET BIT(2)
  50. #define AD5791_DAC_PWRDN_6K 0
  51. #define AD5791_DAC_PWRDN_3STATE 1
  52. /**
  53. * struct ad5791_chip_info - chip specific information
  54. * @get_lin_comp: function pointer to the device specific function
  55. */
  56. struct ad5791_chip_info {
  57. int (*get_lin_comp) (unsigned int span);
  58. };
  59. /**
  60. * struct ad5791_state - driver instance specific data
  61. * @spi: spi_device
  62. * @reg_vdd: positive supply regulator
  63. * @reg_vss: negative supply regulator
  64. * @chip_info: chip model specific constants
  65. * @vref_mv: actual reference voltage used
  66. * @vref_neg_mv: voltage of the negative supply
  67. * @pwr_down_mode current power down mode
  68. */
  69. struct ad5791_state {
  70. struct spi_device *spi;
  71. struct regulator *reg_vdd;
  72. struct regulator *reg_vss;
  73. const struct ad5791_chip_info *chip_info;
  74. unsigned short vref_mv;
  75. unsigned int vref_neg_mv;
  76. unsigned ctrl;
  77. unsigned pwr_down_mode;
  78. bool pwr_down;
  79. union {
  80. __be32 d32;
  81. u8 d8[4];
  82. } data[3] ____cacheline_aligned;
  83. };
  84. /**
  85. * ad5791_supported_device_ids:
  86. */
  87. enum ad5791_supported_device_ids {
  88. ID_AD5760,
  89. ID_AD5780,
  90. ID_AD5781,
  91. ID_AD5791,
  92. };
  93. static int ad5791_spi_write(struct ad5791_state *st, u8 addr, u32 val)
  94. {
  95. st->data[0].d32 = cpu_to_be32(AD5791_CMD_WRITE |
  96. AD5791_ADDR(addr) |
  97. (val & AD5791_DAC_MASK));
  98. return spi_write(st->spi, &st->data[0].d8[1], 3);
  99. }
  100. static int ad5791_spi_read(struct ad5791_state *st, u8 addr, u32 *val)
  101. {
  102. int ret;
  103. struct spi_transfer xfers[] = {
  104. {
  105. .tx_buf = &st->data[0].d8[1],
  106. .bits_per_word = 8,
  107. .len = 3,
  108. .cs_change = 1,
  109. }, {
  110. .tx_buf = &st->data[1].d8[1],
  111. .rx_buf = &st->data[2].d8[1],
  112. .bits_per_word = 8,
  113. .len = 3,
  114. },
  115. };
  116. st->data[0].d32 = cpu_to_be32(AD5791_CMD_READ |
  117. AD5791_ADDR(addr));
  118. st->data[1].d32 = cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP));
  119. ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
  120. *val = be32_to_cpu(st->data[2].d32);
  121. return ret;
  122. }
  123. static const char * const ad5791_powerdown_modes[] = {
  124. "6kohm_to_gnd",
  125. "three_state",
  126. };
  127. static int ad5791_get_powerdown_mode(struct iio_dev *indio_dev,
  128. const struct iio_chan_spec *chan)
  129. {
  130. struct ad5791_state *st = iio_priv(indio_dev);
  131. return st->pwr_down_mode;
  132. }
  133. static int ad5791_set_powerdown_mode(struct iio_dev *indio_dev,
  134. const struct iio_chan_spec *chan, unsigned int mode)
  135. {
  136. struct ad5791_state *st = iio_priv(indio_dev);
  137. st->pwr_down_mode = mode;
  138. return 0;
  139. }
  140. static const struct iio_enum ad5791_powerdown_mode_enum = {
  141. .items = ad5791_powerdown_modes,
  142. .num_items = ARRAY_SIZE(ad5791_powerdown_modes),
  143. .get = ad5791_get_powerdown_mode,
  144. .set = ad5791_set_powerdown_mode,
  145. };
  146. static ssize_t ad5791_read_dac_powerdown(struct iio_dev *indio_dev,
  147. uintptr_t private, const struct iio_chan_spec *chan, char *buf)
  148. {
  149. struct ad5791_state *st = iio_priv(indio_dev);
  150. return sprintf(buf, "%d\n", st->pwr_down);
  151. }
  152. static ssize_t ad5791_write_dac_powerdown(struct iio_dev *indio_dev,
  153. uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
  154. size_t len)
  155. {
  156. bool pwr_down;
  157. int ret;
  158. struct ad5791_state *st = iio_priv(indio_dev);
  159. ret = strtobool(buf, &pwr_down);
  160. if (ret)
  161. return ret;
  162. if (!pwr_down) {
  163. st->ctrl &= ~(AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI);
  164. } else {
  165. if (st->pwr_down_mode == AD5791_DAC_PWRDN_6K)
  166. st->ctrl |= AD5791_CTRL_OPGND;
  167. else if (st->pwr_down_mode == AD5791_DAC_PWRDN_3STATE)
  168. st->ctrl |= AD5791_CTRL_DACTRI;
  169. }
  170. st->pwr_down = pwr_down;
  171. ret = ad5791_spi_write(st, AD5791_ADDR_CTRL, st->ctrl);
  172. return ret ? ret : len;
  173. }
  174. static int ad5791_get_lin_comp(unsigned int span)
  175. {
  176. if (span <= 10000)
  177. return AD5791_LINCOMP_0_10;
  178. else if (span <= 12000)
  179. return AD5791_LINCOMP_10_12;
  180. else if (span <= 16000)
  181. return AD5791_LINCOMP_12_16;
  182. else if (span <= 19000)
  183. return AD5791_LINCOMP_16_19;
  184. else
  185. return AD5791_LINCOMP_19_20;
  186. }
  187. static int ad5780_get_lin_comp(unsigned int span)
  188. {
  189. if (span <= 10000)
  190. return AD5780_LINCOMP_0_10;
  191. else
  192. return AD5780_LINCOMP_10_20;
  193. }
  194. static const struct ad5791_chip_info ad5791_chip_info_tbl[] = {
  195. [ID_AD5760] = {
  196. .get_lin_comp = ad5780_get_lin_comp,
  197. },
  198. [ID_AD5780] = {
  199. .get_lin_comp = ad5780_get_lin_comp,
  200. },
  201. [ID_AD5781] = {
  202. .get_lin_comp = ad5791_get_lin_comp,
  203. },
  204. [ID_AD5791] = {
  205. .get_lin_comp = ad5791_get_lin_comp,
  206. },
  207. };
  208. static int ad5791_read_raw(struct iio_dev *indio_dev,
  209. struct iio_chan_spec const *chan,
  210. int *val,
  211. int *val2,
  212. long m)
  213. {
  214. struct ad5791_state *st = iio_priv(indio_dev);
  215. u64 val64;
  216. int ret;
  217. switch (m) {
  218. case IIO_CHAN_INFO_RAW:
  219. ret = ad5791_spi_read(st, chan->address, val);
  220. if (ret)
  221. return ret;
  222. *val &= AD5791_DAC_MASK;
  223. *val >>= chan->scan_type.shift;
  224. return IIO_VAL_INT;
  225. case IIO_CHAN_INFO_SCALE:
  226. *val = st->vref_mv;
  227. *val2 = (1 << chan->scan_type.realbits) - 1;
  228. return IIO_VAL_FRACTIONAL;
  229. case IIO_CHAN_INFO_OFFSET:
  230. val64 = (((u64)st->vref_neg_mv) << chan->scan_type.realbits);
  231. do_div(val64, st->vref_mv);
  232. *val = -val64;
  233. return IIO_VAL_INT;
  234. default:
  235. return -EINVAL;
  236. }
  237. };
  238. static const struct iio_chan_spec_ext_info ad5791_ext_info[] = {
  239. {
  240. .name = "powerdown",
  241. .shared = IIO_SHARED_BY_TYPE,
  242. .read = ad5791_read_dac_powerdown,
  243. .write = ad5791_write_dac_powerdown,
  244. },
  245. IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
  246. &ad5791_powerdown_mode_enum),
  247. IIO_ENUM_AVAILABLE("powerdown_mode", &ad5791_powerdown_mode_enum),
  248. { },
  249. };
  250. #define AD5791_CHAN(bits, _shift) { \
  251. .type = IIO_VOLTAGE, \
  252. .output = 1, \
  253. .indexed = 1, \
  254. .address = AD5791_ADDR_DAC0, \
  255. .channel = 0, \
  256. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  257. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  258. BIT(IIO_CHAN_INFO_OFFSET), \
  259. .scan_type = { \
  260. .sign = 'u', \
  261. .realbits = (bits), \
  262. .storagebits = 24, \
  263. .shift = (_shift), \
  264. }, \
  265. .ext_info = ad5791_ext_info, \
  266. }
  267. static const struct iio_chan_spec ad5791_channels[] = {
  268. [ID_AD5760] = AD5791_CHAN(16, 4),
  269. [ID_AD5780] = AD5791_CHAN(18, 2),
  270. [ID_AD5781] = AD5791_CHAN(18, 2),
  271. [ID_AD5791] = AD5791_CHAN(20, 0)
  272. };
  273. static int ad5791_write_raw(struct iio_dev *indio_dev,
  274. struct iio_chan_spec const *chan,
  275. int val,
  276. int val2,
  277. long mask)
  278. {
  279. struct ad5791_state *st = iio_priv(indio_dev);
  280. switch (mask) {
  281. case IIO_CHAN_INFO_RAW:
  282. val &= GENMASK(chan->scan_type.realbits - 1, 0);
  283. val <<= chan->scan_type.shift;
  284. return ad5791_spi_write(st, chan->address, val);
  285. default:
  286. return -EINVAL;
  287. }
  288. }
  289. static const struct iio_info ad5791_info = {
  290. .read_raw = &ad5791_read_raw,
  291. .write_raw = &ad5791_write_raw,
  292. .driver_module = THIS_MODULE,
  293. };
  294. static int ad5791_probe(struct spi_device *spi)
  295. {
  296. struct ad5791_platform_data *pdata = spi->dev.platform_data;
  297. struct iio_dev *indio_dev;
  298. struct ad5791_state *st;
  299. int ret, pos_voltage_uv = 0, neg_voltage_uv = 0;
  300. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  301. if (!indio_dev)
  302. return -ENOMEM;
  303. st = iio_priv(indio_dev);
  304. st->reg_vdd = devm_regulator_get(&spi->dev, "vdd");
  305. if (!IS_ERR(st->reg_vdd)) {
  306. ret = regulator_enable(st->reg_vdd);
  307. if (ret)
  308. return ret;
  309. ret = regulator_get_voltage(st->reg_vdd);
  310. if (ret < 0)
  311. goto error_disable_reg_pos;
  312. pos_voltage_uv = ret;
  313. }
  314. st->reg_vss = devm_regulator_get(&spi->dev, "vss");
  315. if (!IS_ERR(st->reg_vss)) {
  316. ret = regulator_enable(st->reg_vss);
  317. if (ret)
  318. goto error_disable_reg_pos;
  319. ret = regulator_get_voltage(st->reg_vss);
  320. if (ret < 0)
  321. goto error_disable_reg_neg;
  322. neg_voltage_uv = ret;
  323. }
  324. st->pwr_down = true;
  325. st->spi = spi;
  326. if (!IS_ERR(st->reg_vss) && !IS_ERR(st->reg_vdd)) {
  327. st->vref_mv = (pos_voltage_uv + neg_voltage_uv) / 1000;
  328. st->vref_neg_mv = neg_voltage_uv / 1000;
  329. } else if (pdata) {
  330. st->vref_mv = pdata->vref_pos_mv + pdata->vref_neg_mv;
  331. st->vref_neg_mv = pdata->vref_neg_mv;
  332. } else {
  333. dev_warn(&spi->dev, "reference voltage unspecified\n");
  334. }
  335. ret = ad5791_spi_write(st, AD5791_ADDR_SW_CTRL, AD5791_SWCTRL_RESET);
  336. if (ret)
  337. goto error_disable_reg_neg;
  338. st->chip_info = &ad5791_chip_info_tbl[spi_get_device_id(spi)
  339. ->driver_data];
  340. st->ctrl = AD5761_CTRL_LINCOMP(st->chip_info->get_lin_comp(st->vref_mv))
  341. | ((pdata && pdata->use_rbuf_gain2) ? 0 : AD5791_CTRL_RBUF) |
  342. AD5791_CTRL_BIN2SC;
  343. ret = ad5791_spi_write(st, AD5791_ADDR_CTRL, st->ctrl |
  344. AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI);
  345. if (ret)
  346. goto error_disable_reg_neg;
  347. spi_set_drvdata(spi, indio_dev);
  348. indio_dev->dev.parent = &spi->dev;
  349. indio_dev->info = &ad5791_info;
  350. indio_dev->modes = INDIO_DIRECT_MODE;
  351. indio_dev->channels
  352. = &ad5791_channels[spi_get_device_id(spi)->driver_data];
  353. indio_dev->num_channels = 1;
  354. indio_dev->name = spi_get_device_id(st->spi)->name;
  355. ret = iio_device_register(indio_dev);
  356. if (ret)
  357. goto error_disable_reg_neg;
  358. return 0;
  359. error_disable_reg_neg:
  360. if (!IS_ERR(st->reg_vss))
  361. regulator_disable(st->reg_vss);
  362. error_disable_reg_pos:
  363. if (!IS_ERR(st->reg_vdd))
  364. regulator_disable(st->reg_vdd);
  365. return ret;
  366. }
  367. static int ad5791_remove(struct spi_device *spi)
  368. {
  369. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  370. struct ad5791_state *st = iio_priv(indio_dev);
  371. iio_device_unregister(indio_dev);
  372. if (!IS_ERR(st->reg_vdd))
  373. regulator_disable(st->reg_vdd);
  374. if (!IS_ERR(st->reg_vss))
  375. regulator_disable(st->reg_vss);
  376. return 0;
  377. }
  378. static const struct spi_device_id ad5791_id[] = {
  379. {"ad5760", ID_AD5760},
  380. {"ad5780", ID_AD5780},
  381. {"ad5781", ID_AD5781},
  382. {"ad5790", ID_AD5791},
  383. {"ad5791", ID_AD5791},
  384. {}
  385. };
  386. MODULE_DEVICE_TABLE(spi, ad5791_id);
  387. static struct spi_driver ad5791_driver = {
  388. .driver = {
  389. .name = "ad5791",
  390. },
  391. .probe = ad5791_probe,
  392. .remove = ad5791_remove,
  393. .id_table = ad5791_id,
  394. };
  395. module_spi_driver(ad5791_driver);
  396. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  397. MODULE_DESCRIPTION("Analog Devices AD5760/AD5780/AD5781/AD5790/AD5791 DAC");
  398. MODULE_LICENSE("GPL v2");