ad5764.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Analog devices AD5764, AD5764R, AD5744, AD5744R quad-channel
  3. * 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/module.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/iio/iio.h>
  18. #include <linux/iio/sysfs.h>
  19. #define AD5764_REG_SF_NOP 0x0
  20. #define AD5764_REG_SF_CONFIG 0x1
  21. #define AD5764_REG_SF_CLEAR 0x4
  22. #define AD5764_REG_SF_LOAD 0x5
  23. #define AD5764_REG_DATA(x) ((2 << 3) | (x))
  24. #define AD5764_REG_COARSE_GAIN(x) ((3 << 3) | (x))
  25. #define AD5764_REG_FINE_GAIN(x) ((4 << 3) | (x))
  26. #define AD5764_REG_OFFSET(x) ((5 << 3) | (x))
  27. #define AD5764_NUM_CHANNELS 4
  28. /**
  29. * struct ad5764_chip_info - chip specific information
  30. * @int_vref: Value of the internal reference voltage in uV - 0 if external
  31. * reference voltage is used
  32. * @channel channel specification
  33. */
  34. struct ad5764_chip_info {
  35. unsigned long int_vref;
  36. const struct iio_chan_spec *channels;
  37. };
  38. /**
  39. * struct ad5764_state - driver instance specific data
  40. * @spi: spi_device
  41. * @chip_info: chip info
  42. * @vref_reg: vref supply regulators
  43. * @data: spi transfer buffers
  44. */
  45. struct ad5764_state {
  46. struct spi_device *spi;
  47. const struct ad5764_chip_info *chip_info;
  48. struct regulator_bulk_data vref_reg[2];
  49. /*
  50. * DMA (thus cache coherency maintenance) requires the
  51. * transfer buffers to live in their own cache lines.
  52. */
  53. union {
  54. __be32 d32;
  55. u8 d8[4];
  56. } data[2] ____cacheline_aligned;
  57. };
  58. enum ad5764_type {
  59. ID_AD5744,
  60. ID_AD5744R,
  61. ID_AD5764,
  62. ID_AD5764R,
  63. };
  64. #define AD5764_CHANNEL(_chan, _bits) { \
  65. .type = IIO_VOLTAGE, \
  66. .indexed = 1, \
  67. .output = 1, \
  68. .channel = (_chan), \
  69. .address = (_chan), \
  70. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  71. BIT(IIO_CHAN_INFO_SCALE) | \
  72. BIT(IIO_CHAN_INFO_CALIBSCALE) | \
  73. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  74. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET), \
  75. .scan_type = { \
  76. .sign = 'u', \
  77. .realbits = (_bits), \
  78. .storagebits = 16, \
  79. .shift = 16 - (_bits), \
  80. }, \
  81. }
  82. #define DECLARE_AD5764_CHANNELS(_name, _bits) \
  83. const struct iio_chan_spec _name##_channels[] = { \
  84. AD5764_CHANNEL(0, (_bits)), \
  85. AD5764_CHANNEL(1, (_bits)), \
  86. AD5764_CHANNEL(2, (_bits)), \
  87. AD5764_CHANNEL(3, (_bits)), \
  88. };
  89. static DECLARE_AD5764_CHANNELS(ad5764, 16);
  90. static DECLARE_AD5764_CHANNELS(ad5744, 14);
  91. static const struct ad5764_chip_info ad5764_chip_infos[] = {
  92. [ID_AD5744] = {
  93. .int_vref = 0,
  94. .channels = ad5744_channels,
  95. },
  96. [ID_AD5744R] = {
  97. .int_vref = 5000000,
  98. .channels = ad5744_channels,
  99. },
  100. [ID_AD5764] = {
  101. .int_vref = 0,
  102. .channels = ad5764_channels,
  103. },
  104. [ID_AD5764R] = {
  105. .int_vref = 5000000,
  106. .channels = ad5764_channels,
  107. },
  108. };
  109. static int ad5764_write(struct iio_dev *indio_dev, unsigned int reg,
  110. unsigned int val)
  111. {
  112. struct ad5764_state *st = iio_priv(indio_dev);
  113. int ret;
  114. mutex_lock(&indio_dev->mlock);
  115. st->data[0].d32 = cpu_to_be32((reg << 16) | val);
  116. ret = spi_write(st->spi, &st->data[0].d8[1], 3);
  117. mutex_unlock(&indio_dev->mlock);
  118. return ret;
  119. }
  120. static int ad5764_read(struct iio_dev *indio_dev, unsigned int reg,
  121. unsigned int *val)
  122. {
  123. struct ad5764_state *st = iio_priv(indio_dev);
  124. int ret;
  125. struct spi_transfer t[] = {
  126. {
  127. .tx_buf = &st->data[0].d8[1],
  128. .len = 3,
  129. .cs_change = 1,
  130. }, {
  131. .rx_buf = &st->data[1].d8[1],
  132. .len = 3,
  133. },
  134. };
  135. mutex_lock(&indio_dev->mlock);
  136. st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16));
  137. ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
  138. if (ret >= 0)
  139. *val = be32_to_cpu(st->data[1].d32) & 0xffff;
  140. mutex_unlock(&indio_dev->mlock);
  141. return ret;
  142. }
  143. static int ad5764_chan_info_to_reg(struct iio_chan_spec const *chan, long info)
  144. {
  145. switch (info) {
  146. case 0:
  147. return AD5764_REG_DATA(chan->address);
  148. case IIO_CHAN_INFO_CALIBBIAS:
  149. return AD5764_REG_OFFSET(chan->address);
  150. case IIO_CHAN_INFO_CALIBSCALE:
  151. return AD5764_REG_FINE_GAIN(chan->address);
  152. default:
  153. break;
  154. }
  155. return 0;
  156. }
  157. static int ad5764_write_raw(struct iio_dev *indio_dev,
  158. struct iio_chan_spec const *chan, int val, int val2, long info)
  159. {
  160. const int max_val = (1 << chan->scan_type.realbits);
  161. unsigned int reg;
  162. switch (info) {
  163. case IIO_CHAN_INFO_RAW:
  164. if (val >= max_val || val < 0)
  165. return -EINVAL;
  166. val <<= chan->scan_type.shift;
  167. break;
  168. case IIO_CHAN_INFO_CALIBBIAS:
  169. if (val >= 128 || val < -128)
  170. return -EINVAL;
  171. break;
  172. case IIO_CHAN_INFO_CALIBSCALE:
  173. if (val >= 32 || val < -32)
  174. return -EINVAL;
  175. break;
  176. default:
  177. return -EINVAL;
  178. }
  179. reg = ad5764_chan_info_to_reg(chan, info);
  180. return ad5764_write(indio_dev, reg, (u16)val);
  181. }
  182. static int ad5764_get_channel_vref(struct ad5764_state *st,
  183. unsigned int channel)
  184. {
  185. if (st->chip_info->int_vref)
  186. return st->chip_info->int_vref;
  187. else
  188. return regulator_get_voltage(st->vref_reg[channel / 2].consumer);
  189. }
  190. static int ad5764_read_raw(struct iio_dev *indio_dev,
  191. struct iio_chan_spec const *chan, int *val, int *val2, long info)
  192. {
  193. struct ad5764_state *st = iio_priv(indio_dev);
  194. unsigned int reg;
  195. int vref;
  196. int ret;
  197. switch (info) {
  198. case IIO_CHAN_INFO_RAW:
  199. reg = AD5764_REG_DATA(chan->address);
  200. ret = ad5764_read(indio_dev, reg, val);
  201. if (ret < 0)
  202. return ret;
  203. *val >>= chan->scan_type.shift;
  204. return IIO_VAL_INT;
  205. case IIO_CHAN_INFO_CALIBBIAS:
  206. reg = AD5764_REG_OFFSET(chan->address);
  207. ret = ad5764_read(indio_dev, reg, val);
  208. if (ret < 0)
  209. return ret;
  210. *val = sign_extend32(*val, 7);
  211. return IIO_VAL_INT;
  212. case IIO_CHAN_INFO_CALIBSCALE:
  213. reg = AD5764_REG_FINE_GAIN(chan->address);
  214. ret = ad5764_read(indio_dev, reg, val);
  215. if (ret < 0)
  216. return ret;
  217. *val = sign_extend32(*val, 5);
  218. return IIO_VAL_INT;
  219. case IIO_CHAN_INFO_SCALE:
  220. /* vout = 4 * vref + ((dac_code / 65536) - 0.5) */
  221. vref = ad5764_get_channel_vref(st, chan->channel);
  222. if (vref < 0)
  223. return vref;
  224. *val = vref * 4 / 1000;
  225. *val2 = chan->scan_type.realbits;
  226. return IIO_VAL_FRACTIONAL_LOG2;
  227. case IIO_CHAN_INFO_OFFSET:
  228. *val = -(1 << chan->scan_type.realbits) / 2;
  229. return IIO_VAL_INT;
  230. }
  231. return -EINVAL;
  232. }
  233. static const struct iio_info ad5764_info = {
  234. .read_raw = ad5764_read_raw,
  235. .write_raw = ad5764_write_raw,
  236. .driver_module = THIS_MODULE,
  237. };
  238. static int ad5764_probe(struct spi_device *spi)
  239. {
  240. enum ad5764_type type = spi_get_device_id(spi)->driver_data;
  241. struct iio_dev *indio_dev;
  242. struct ad5764_state *st;
  243. int ret;
  244. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  245. if (indio_dev == NULL) {
  246. dev_err(&spi->dev, "Failed to allocate iio device\n");
  247. return -ENOMEM;
  248. }
  249. st = iio_priv(indio_dev);
  250. spi_set_drvdata(spi, indio_dev);
  251. st->spi = spi;
  252. st->chip_info = &ad5764_chip_infos[type];
  253. indio_dev->dev.parent = &spi->dev;
  254. indio_dev->name = spi_get_device_id(spi)->name;
  255. indio_dev->info = &ad5764_info;
  256. indio_dev->modes = INDIO_DIRECT_MODE;
  257. indio_dev->num_channels = AD5764_NUM_CHANNELS;
  258. indio_dev->channels = st->chip_info->channels;
  259. if (st->chip_info->int_vref == 0) {
  260. st->vref_reg[0].supply = "vrefAB";
  261. st->vref_reg[1].supply = "vrefCD";
  262. ret = devm_regulator_bulk_get(&st->spi->dev,
  263. ARRAY_SIZE(st->vref_reg), st->vref_reg);
  264. if (ret) {
  265. dev_err(&spi->dev, "Failed to request vref regulators: %d\n",
  266. ret);
  267. return ret;
  268. }
  269. ret = regulator_bulk_enable(ARRAY_SIZE(st->vref_reg),
  270. st->vref_reg);
  271. if (ret) {
  272. dev_err(&spi->dev, "Failed to enable vref regulators: %d\n",
  273. ret);
  274. return ret;
  275. }
  276. }
  277. ret = iio_device_register(indio_dev);
  278. if (ret) {
  279. dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
  280. goto error_disable_reg;
  281. }
  282. return 0;
  283. error_disable_reg:
  284. if (st->chip_info->int_vref == 0)
  285. regulator_bulk_disable(ARRAY_SIZE(st->vref_reg), st->vref_reg);
  286. return ret;
  287. }
  288. static int ad5764_remove(struct spi_device *spi)
  289. {
  290. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  291. struct ad5764_state *st = iio_priv(indio_dev);
  292. iio_device_unregister(indio_dev);
  293. if (st->chip_info->int_vref == 0)
  294. regulator_bulk_disable(ARRAY_SIZE(st->vref_reg), st->vref_reg);
  295. return 0;
  296. }
  297. static const struct spi_device_id ad5764_ids[] = {
  298. { "ad5744", ID_AD5744 },
  299. { "ad5744r", ID_AD5744R },
  300. { "ad5764", ID_AD5764 },
  301. { "ad5764r", ID_AD5764R },
  302. { }
  303. };
  304. MODULE_DEVICE_TABLE(spi, ad5764_ids);
  305. static struct spi_driver ad5764_driver = {
  306. .driver = {
  307. .name = "ad5764",
  308. },
  309. .probe = ad5764_probe,
  310. .remove = ad5764_remove,
  311. .id_table = ad5764_ids,
  312. };
  313. module_spi_driver(ad5764_driver);
  314. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  315. MODULE_DESCRIPTION("Analog Devices AD5744/AD5744R/AD5764/AD5764R DAC");
  316. MODULE_LICENSE("GPL v2");