ad5449.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * AD5415, AD5426, AD5429, AD5432, AD5439, AD5443, AD5449 Digital to Analog
  3. * Converter driver.
  4. *
  5. * Copyright 2012 Analog Devices Inc.
  6. * Author: Lars-Peter Clausen <lars@metafoo.de>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <asm/unaligned.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/platform_data/ad5449.h>
  22. #define AD5449_MAX_CHANNELS 2
  23. #define AD5449_MAX_VREFS 2
  24. #define AD5449_CMD_NOOP 0x0
  25. #define AD5449_CMD_LOAD_AND_UPDATE(x) (0x1 + (x) * 3)
  26. #define AD5449_CMD_READ(x) (0x2 + (x) * 3)
  27. #define AD5449_CMD_LOAD(x) (0x3 + (x) * 3)
  28. #define AD5449_CMD_CTRL 13
  29. #define AD5449_CTRL_SDO_OFFSET 10
  30. #define AD5449_CTRL_DAISY_CHAIN BIT(9)
  31. #define AD5449_CTRL_HCLR_TO_MIDSCALE BIT(8)
  32. #define AD5449_CTRL_SAMPLE_RISING BIT(7)
  33. /**
  34. * struct ad5449_chip_info - chip specific information
  35. * @channels: Channel specification
  36. * @num_channels: Number of channels
  37. * @has_ctrl: Chip has a control register
  38. */
  39. struct ad5449_chip_info {
  40. const struct iio_chan_spec *channels;
  41. unsigned int num_channels;
  42. bool has_ctrl;
  43. };
  44. /**
  45. * struct ad5449 - driver instance specific data
  46. * @spi: the SPI device for this driver instance
  47. * @chip_info: chip model specific constants, available modes etc
  48. * @vref_reg: vref supply regulators
  49. * @has_sdo: whether the SDO line is connected
  50. * @dac_cache: Cache for the DAC values
  51. * @data: spi transfer buffers
  52. */
  53. struct ad5449 {
  54. struct spi_device *spi;
  55. const struct ad5449_chip_info *chip_info;
  56. struct regulator_bulk_data vref_reg[AD5449_MAX_VREFS];
  57. bool has_sdo;
  58. uint16_t dac_cache[AD5449_MAX_CHANNELS];
  59. /*
  60. * DMA (thus cache coherency maintenance) requires the
  61. * transfer buffers to live in their own cache lines.
  62. */
  63. __be16 data[2] ____cacheline_aligned;
  64. };
  65. enum ad5449_type {
  66. ID_AD5426,
  67. ID_AD5429,
  68. ID_AD5432,
  69. ID_AD5439,
  70. ID_AD5443,
  71. ID_AD5449,
  72. };
  73. static int ad5449_write(struct iio_dev *indio_dev, unsigned int addr,
  74. unsigned int val)
  75. {
  76. struct ad5449 *st = iio_priv(indio_dev);
  77. int ret;
  78. mutex_lock(&indio_dev->mlock);
  79. st->data[0] = cpu_to_be16((addr << 12) | val);
  80. ret = spi_write(st->spi, st->data, 2);
  81. mutex_unlock(&indio_dev->mlock);
  82. return ret;
  83. }
  84. static int ad5449_read(struct iio_dev *indio_dev, unsigned int addr,
  85. unsigned int *val)
  86. {
  87. struct ad5449 *st = iio_priv(indio_dev);
  88. int ret;
  89. struct spi_message msg;
  90. struct spi_transfer t[] = {
  91. {
  92. .tx_buf = &st->data[0],
  93. .len = 2,
  94. .cs_change = 1,
  95. }, {
  96. .tx_buf = &st->data[1],
  97. .rx_buf = &st->data[1],
  98. .len = 2,
  99. },
  100. };
  101. spi_message_init(&msg);
  102. spi_message_add_tail(&t[0], &msg);
  103. spi_message_add_tail(&t[1], &msg);
  104. mutex_lock(&indio_dev->mlock);
  105. st->data[0] = cpu_to_be16(addr << 12);
  106. st->data[1] = cpu_to_be16(AD5449_CMD_NOOP);
  107. ret = spi_sync(st->spi, &msg);
  108. if (ret < 0)
  109. return ret;
  110. *val = be16_to_cpu(st->data[1]);
  111. mutex_unlock(&indio_dev->mlock);
  112. return 0;
  113. }
  114. static int ad5449_read_raw(struct iio_dev *indio_dev,
  115. struct iio_chan_spec const *chan, int *val, int *val2, long info)
  116. {
  117. struct ad5449 *st = iio_priv(indio_dev);
  118. struct regulator_bulk_data *reg;
  119. int scale_uv;
  120. int ret;
  121. switch (info) {
  122. case IIO_CHAN_INFO_RAW:
  123. if (st->has_sdo) {
  124. ret = ad5449_read(indio_dev,
  125. AD5449_CMD_READ(chan->address), val);
  126. if (ret)
  127. return ret;
  128. *val &= 0xfff;
  129. } else {
  130. *val = st->dac_cache[chan->address];
  131. }
  132. return IIO_VAL_INT;
  133. case IIO_CHAN_INFO_SCALE:
  134. reg = &st->vref_reg[chan->channel];
  135. scale_uv = regulator_get_voltage(reg->consumer);
  136. if (scale_uv < 0)
  137. return scale_uv;
  138. *val = scale_uv / 1000;
  139. *val2 = chan->scan_type.realbits;
  140. return IIO_VAL_FRACTIONAL_LOG2;
  141. default:
  142. break;
  143. }
  144. return -EINVAL;
  145. }
  146. static int ad5449_write_raw(struct iio_dev *indio_dev,
  147. struct iio_chan_spec const *chan, int val, int val2, long info)
  148. {
  149. struct ad5449 *st = iio_priv(indio_dev);
  150. int ret;
  151. switch (info) {
  152. case IIO_CHAN_INFO_RAW:
  153. if (val < 0 || val >= (1 << chan->scan_type.realbits))
  154. return -EINVAL;
  155. ret = ad5449_write(indio_dev,
  156. AD5449_CMD_LOAD_AND_UPDATE(chan->address),
  157. val << chan->scan_type.shift);
  158. if (ret == 0)
  159. st->dac_cache[chan->address] = val;
  160. break;
  161. default:
  162. ret = -EINVAL;
  163. }
  164. return ret;
  165. }
  166. static const struct iio_info ad5449_info = {
  167. .read_raw = ad5449_read_raw,
  168. .write_raw = ad5449_write_raw,
  169. .driver_module = THIS_MODULE,
  170. };
  171. #define AD5449_CHANNEL(chan, bits) { \
  172. .type = IIO_VOLTAGE, \
  173. .indexed = 1, \
  174. .output = 1, \
  175. .channel = (chan), \
  176. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
  177. IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
  178. .address = (chan), \
  179. .scan_type = IIO_ST('u', (bits), 16, 12 - (bits)), \
  180. }
  181. #define DECLARE_AD5449_CHANNELS(name, bits) \
  182. const struct iio_chan_spec name[] = { \
  183. AD5449_CHANNEL(0, bits), \
  184. AD5449_CHANNEL(1, bits), \
  185. }
  186. static DECLARE_AD5449_CHANNELS(ad5429_channels, 8);
  187. static DECLARE_AD5449_CHANNELS(ad5439_channels, 10);
  188. static DECLARE_AD5449_CHANNELS(ad5449_channels, 12);
  189. static const struct ad5449_chip_info ad5449_chip_info[] = {
  190. [ID_AD5426] = {
  191. .channels = ad5429_channels,
  192. .num_channels = 1,
  193. .has_ctrl = false,
  194. },
  195. [ID_AD5429] = {
  196. .channels = ad5429_channels,
  197. .num_channels = 2,
  198. .has_ctrl = true,
  199. },
  200. [ID_AD5432] = {
  201. .channels = ad5439_channels,
  202. .num_channels = 1,
  203. .has_ctrl = false,
  204. },
  205. [ID_AD5439] = {
  206. .channels = ad5439_channels,
  207. .num_channels = 2,
  208. .has_ctrl = true,
  209. },
  210. [ID_AD5443] = {
  211. .channels = ad5449_channels,
  212. .num_channels = 1,
  213. .has_ctrl = false,
  214. },
  215. [ID_AD5449] = {
  216. .channels = ad5449_channels,
  217. .num_channels = 2,
  218. .has_ctrl = true,
  219. },
  220. };
  221. static const char *ad5449_vref_name(struct ad5449 *st, int n)
  222. {
  223. if (st->chip_info->num_channels == 1)
  224. return "VREF";
  225. if (n == 0)
  226. return "VREFA";
  227. else
  228. return "VREFB";
  229. }
  230. static int __devinit ad5449_spi_probe(struct spi_device *spi)
  231. {
  232. struct ad5449_platform_data *pdata = spi->dev.platform_data;
  233. const struct spi_device_id *id = spi_get_device_id(spi);
  234. struct iio_dev *indio_dev;
  235. struct ad5449 *st;
  236. unsigned int i;
  237. int ret;
  238. indio_dev = iio_device_alloc(sizeof(*st));
  239. if (indio_dev == NULL)
  240. return -ENOMEM;
  241. st = iio_priv(indio_dev);
  242. spi_set_drvdata(spi, indio_dev);
  243. st->chip_info = &ad5449_chip_info[id->driver_data];
  244. st->spi = spi;
  245. for (i = 0; i < st->chip_info->num_channels; ++i)
  246. st->vref_reg[i].supply = ad5449_vref_name(st, i);
  247. ret = regulator_bulk_get(&spi->dev, st->chip_info->num_channels,
  248. st->vref_reg);
  249. if (ret)
  250. goto error_free;
  251. ret = regulator_bulk_enable(st->chip_info->num_channels, st->vref_reg);
  252. if (ret)
  253. goto error_free_reg;
  254. indio_dev->dev.parent = &spi->dev;
  255. indio_dev->name = id->name;
  256. indio_dev->info = &ad5449_info;
  257. indio_dev->modes = INDIO_DIRECT_MODE;
  258. indio_dev->channels = st->chip_info->channels;
  259. indio_dev->num_channels = st->chip_info->num_channels;
  260. if (st->chip_info->has_ctrl) {
  261. unsigned int ctrl = 0x00;
  262. if (pdata) {
  263. if (pdata->hardware_clear_to_midscale)
  264. ctrl |= AD5449_CTRL_HCLR_TO_MIDSCALE;
  265. ctrl |= pdata->sdo_mode << AD5449_CTRL_SDO_OFFSET;
  266. st->has_sdo = pdata->sdo_mode != AD5449_SDO_DISABLED;
  267. } else {
  268. st->has_sdo = true;
  269. }
  270. ad5449_write(indio_dev, AD5449_CMD_CTRL, ctrl);
  271. }
  272. ret = iio_device_register(indio_dev);
  273. if (ret)
  274. goto error_disable_reg;
  275. return 0;
  276. error_disable_reg:
  277. regulator_bulk_disable(st->chip_info->num_channels, st->vref_reg);
  278. error_free_reg:
  279. regulator_bulk_free(st->chip_info->num_channels, st->vref_reg);
  280. error_free:
  281. iio_device_free(indio_dev);
  282. return ret;
  283. }
  284. static int __devexit ad5449_spi_remove(struct spi_device *spi)
  285. {
  286. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  287. struct ad5449 *st = iio_priv(indio_dev);
  288. iio_device_unregister(indio_dev);
  289. regulator_bulk_disable(st->chip_info->num_channels, st->vref_reg);
  290. regulator_bulk_free(st->chip_info->num_channels, st->vref_reg);
  291. iio_device_free(indio_dev);
  292. return 0;
  293. }
  294. static const struct spi_device_id ad5449_spi_ids[] = {
  295. { "ad5415", ID_AD5449 },
  296. { "ad5426", ID_AD5426 },
  297. { "ad5429", ID_AD5429 },
  298. { "ad5432", ID_AD5432 },
  299. { "ad5439", ID_AD5439 },
  300. { "ad5443", ID_AD5443 },
  301. { "ad5449", ID_AD5449 },
  302. {}
  303. };
  304. MODULE_DEVICE_TABLE(spi, ad5449_spi_ids);
  305. static struct spi_driver ad5449_spi_driver = {
  306. .driver = {
  307. .name = "ad5449",
  308. .owner = THIS_MODULE,
  309. },
  310. .probe = ad5449_spi_probe,
  311. .remove = __devexit_p(ad5449_spi_remove),
  312. .id_table = ad5449_spi_ids,
  313. };
  314. module_spi_driver(ad5449_spi_driver);
  315. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  316. MODULE_DESCRIPTION("Analog Devices AD5449 and similar DACs");
  317. MODULE_LICENSE("GPL v2");