ad5360.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Analog devices AD5360, AD5361, AD5362, AD5363, AD5370, AD5371, AD5373
  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/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 AD5360_CMD(x) ((x) << 22)
  20. #define AD5360_ADDR(x) ((x) << 16)
  21. #define AD5360_READBACK_TYPE(x) ((x) << 13)
  22. #define AD5360_READBACK_ADDR(x) ((x) << 7)
  23. #define AD5360_CHAN_ADDR(chan) ((chan) + 0x8)
  24. #define AD5360_CMD_WRITE_DATA 0x3
  25. #define AD5360_CMD_WRITE_OFFSET 0x2
  26. #define AD5360_CMD_WRITE_GAIN 0x1
  27. #define AD5360_CMD_SPECIAL_FUNCTION 0x0
  28. /* Special function register addresses */
  29. #define AD5360_REG_SF_NOP 0x0
  30. #define AD5360_REG_SF_CTRL 0x1
  31. #define AD5360_REG_SF_OFS(x) (0x2 + (x))
  32. #define AD5360_REG_SF_READBACK 0x5
  33. #define AD5360_SF_CTRL_PWR_DOWN BIT(0)
  34. #define AD5360_READBACK_X1A 0x0
  35. #define AD5360_READBACK_X1B 0x1
  36. #define AD5360_READBACK_OFFSET 0x2
  37. #define AD5360_READBACK_GAIN 0x3
  38. #define AD5360_READBACK_SF 0x4
  39. /**
  40. * struct ad5360_chip_info - chip specific information
  41. * @channel_template: channel specification template
  42. * @num_channels: number of channels
  43. * @channels_per_group: number of channels per group
  44. * @num_vrefs: number of vref supplies for the chip
  45. */
  46. struct ad5360_chip_info {
  47. struct iio_chan_spec channel_template;
  48. unsigned int num_channels;
  49. unsigned int channels_per_group;
  50. unsigned int num_vrefs;
  51. };
  52. /**
  53. * struct ad5360_state - driver instance specific data
  54. * @spi: spi_device
  55. * @chip_info: chip model specific constants, available modes etc
  56. * @vref_reg: vref supply regulators
  57. * @ctrl: control register cache
  58. * @data: spi transfer buffers
  59. */
  60. struct ad5360_state {
  61. struct spi_device *spi;
  62. const struct ad5360_chip_info *chip_info;
  63. struct regulator_bulk_data vref_reg[3];
  64. unsigned int ctrl;
  65. /*
  66. * DMA (thus cache coherency maintenance) requires the
  67. * transfer buffers to live in their own cache lines.
  68. */
  69. union {
  70. __be32 d32;
  71. u8 d8[4];
  72. } data[2] ____cacheline_aligned;
  73. };
  74. enum ad5360_type {
  75. ID_AD5360,
  76. ID_AD5361,
  77. ID_AD5362,
  78. ID_AD5363,
  79. ID_AD5370,
  80. ID_AD5371,
  81. ID_AD5372,
  82. ID_AD5373,
  83. };
  84. #define AD5360_CHANNEL(bits) { \
  85. .type = IIO_VOLTAGE, \
  86. .indexed = 1, \
  87. .output = 1, \
  88. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  89. BIT(IIO_CHAN_INFO_SCALE) | \
  90. BIT(IIO_CHAN_INFO_OFFSET) | \
  91. BIT(IIO_CHAN_INFO_CALIBSCALE) | \
  92. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  93. .scan_type = { \
  94. .sign = 'u', \
  95. .realbits = (bits), \
  96. .storagebits = 16, \
  97. .shift = 16 - (bits), \
  98. }, \
  99. }
  100. static const struct ad5360_chip_info ad5360_chip_info_tbl[] = {
  101. [ID_AD5360] = {
  102. .channel_template = AD5360_CHANNEL(16),
  103. .num_channels = 16,
  104. .channels_per_group = 8,
  105. .num_vrefs = 2,
  106. },
  107. [ID_AD5361] = {
  108. .channel_template = AD5360_CHANNEL(14),
  109. .num_channels = 16,
  110. .channels_per_group = 8,
  111. .num_vrefs = 2,
  112. },
  113. [ID_AD5362] = {
  114. .channel_template = AD5360_CHANNEL(16),
  115. .num_channels = 8,
  116. .channels_per_group = 4,
  117. .num_vrefs = 2,
  118. },
  119. [ID_AD5363] = {
  120. .channel_template = AD5360_CHANNEL(14),
  121. .num_channels = 8,
  122. .channels_per_group = 4,
  123. .num_vrefs = 2,
  124. },
  125. [ID_AD5370] = {
  126. .channel_template = AD5360_CHANNEL(16),
  127. .num_channels = 40,
  128. .channels_per_group = 8,
  129. .num_vrefs = 2,
  130. },
  131. [ID_AD5371] = {
  132. .channel_template = AD5360_CHANNEL(14),
  133. .num_channels = 40,
  134. .channels_per_group = 8,
  135. .num_vrefs = 3,
  136. },
  137. [ID_AD5372] = {
  138. .channel_template = AD5360_CHANNEL(16),
  139. .num_channels = 32,
  140. .channels_per_group = 8,
  141. .num_vrefs = 2,
  142. },
  143. [ID_AD5373] = {
  144. .channel_template = AD5360_CHANNEL(14),
  145. .num_channels = 32,
  146. .channels_per_group = 8,
  147. .num_vrefs = 2,
  148. },
  149. };
  150. static unsigned int ad5360_get_channel_vref_index(struct ad5360_state *st,
  151. unsigned int channel)
  152. {
  153. unsigned int i;
  154. /* The first groups have their own vref, while the remaining groups
  155. * share the last vref */
  156. i = channel / st->chip_info->channels_per_group;
  157. if (i >= st->chip_info->num_vrefs)
  158. i = st->chip_info->num_vrefs - 1;
  159. return i;
  160. }
  161. static int ad5360_get_channel_vref(struct ad5360_state *st,
  162. unsigned int channel)
  163. {
  164. unsigned int i = ad5360_get_channel_vref_index(st, channel);
  165. return regulator_get_voltage(st->vref_reg[i].consumer);
  166. }
  167. static int ad5360_write_unlocked(struct iio_dev *indio_dev,
  168. unsigned int cmd, unsigned int addr, unsigned int val,
  169. unsigned int shift)
  170. {
  171. struct ad5360_state *st = iio_priv(indio_dev);
  172. val <<= shift;
  173. val |= AD5360_CMD(cmd) | AD5360_ADDR(addr);
  174. st->data[0].d32 = cpu_to_be32(val);
  175. return spi_write(st->spi, &st->data[0].d8[1], 3);
  176. }
  177. static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd,
  178. unsigned int addr, unsigned int val, unsigned int shift)
  179. {
  180. int ret;
  181. mutex_lock(&indio_dev->mlock);
  182. ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
  183. mutex_unlock(&indio_dev->mlock);
  184. return ret;
  185. }
  186. static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
  187. unsigned int addr)
  188. {
  189. struct ad5360_state *st = iio_priv(indio_dev);
  190. int ret;
  191. struct spi_transfer t[] = {
  192. {
  193. .tx_buf = &st->data[0].d8[1],
  194. .len = 3,
  195. .cs_change = 1,
  196. }, {
  197. .rx_buf = &st->data[1].d8[1],
  198. .len = 3,
  199. },
  200. };
  201. mutex_lock(&indio_dev->mlock);
  202. st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
  203. AD5360_ADDR(AD5360_REG_SF_READBACK) |
  204. AD5360_READBACK_TYPE(type) |
  205. AD5360_READBACK_ADDR(addr));
  206. ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
  207. if (ret >= 0)
  208. ret = be32_to_cpu(st->data[1].d32) & 0xffff;
  209. mutex_unlock(&indio_dev->mlock);
  210. return ret;
  211. }
  212. static ssize_t ad5360_read_dac_powerdown(struct device *dev,
  213. struct device_attribute *attr,
  214. char *buf)
  215. {
  216. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  217. struct ad5360_state *st = iio_priv(indio_dev);
  218. return sprintf(buf, "%d\n", (bool)(st->ctrl & AD5360_SF_CTRL_PWR_DOWN));
  219. }
  220. static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
  221. unsigned int clr)
  222. {
  223. struct ad5360_state *st = iio_priv(indio_dev);
  224. unsigned int ret;
  225. mutex_lock(&indio_dev->mlock);
  226. st->ctrl |= set;
  227. st->ctrl &= ~clr;
  228. ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
  229. AD5360_REG_SF_CTRL, st->ctrl, 0);
  230. mutex_unlock(&indio_dev->mlock);
  231. return ret;
  232. }
  233. static ssize_t ad5360_write_dac_powerdown(struct device *dev,
  234. struct device_attribute *attr, const char *buf, size_t len)
  235. {
  236. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  237. bool pwr_down;
  238. int ret;
  239. ret = strtobool(buf, &pwr_down);
  240. if (ret)
  241. return ret;
  242. if (pwr_down)
  243. ret = ad5360_update_ctrl(indio_dev, AD5360_SF_CTRL_PWR_DOWN, 0);
  244. else
  245. ret = ad5360_update_ctrl(indio_dev, 0, AD5360_SF_CTRL_PWR_DOWN);
  246. return ret ? ret : len;
  247. }
  248. static IIO_DEVICE_ATTR(out_voltage_powerdown,
  249. S_IRUGO | S_IWUSR,
  250. ad5360_read_dac_powerdown,
  251. ad5360_write_dac_powerdown, 0);
  252. static struct attribute *ad5360_attributes[] = {
  253. &iio_dev_attr_out_voltage_powerdown.dev_attr.attr,
  254. NULL,
  255. };
  256. static const struct attribute_group ad5360_attribute_group = {
  257. .attrs = ad5360_attributes,
  258. };
  259. static int ad5360_write_raw(struct iio_dev *indio_dev,
  260. struct iio_chan_spec const *chan,
  261. int val,
  262. int val2,
  263. long mask)
  264. {
  265. struct ad5360_state *st = iio_priv(indio_dev);
  266. int max_val = (1 << chan->scan_type.realbits);
  267. unsigned int ofs_index;
  268. switch (mask) {
  269. case IIO_CHAN_INFO_RAW:
  270. if (val >= max_val || val < 0)
  271. return -EINVAL;
  272. return ad5360_write(indio_dev, AD5360_CMD_WRITE_DATA,
  273. chan->address, val, chan->scan_type.shift);
  274. case IIO_CHAN_INFO_CALIBBIAS:
  275. if (val >= max_val || val < 0)
  276. return -EINVAL;
  277. return ad5360_write(indio_dev, AD5360_CMD_WRITE_OFFSET,
  278. chan->address, val, chan->scan_type.shift);
  279. case IIO_CHAN_INFO_CALIBSCALE:
  280. if (val >= max_val || val < 0)
  281. return -EINVAL;
  282. return ad5360_write(indio_dev, AD5360_CMD_WRITE_GAIN,
  283. chan->address, val, chan->scan_type.shift);
  284. case IIO_CHAN_INFO_OFFSET:
  285. if (val <= -max_val || val > 0)
  286. return -EINVAL;
  287. val = -val;
  288. /* offset is supposed to have the same scale as raw, but it
  289. * is always 14bits wide, so on a chip where the raw value has
  290. * more bits, we need to shift offset. */
  291. val >>= (chan->scan_type.realbits - 14);
  292. /* There is one DAC offset register per vref. Changing one
  293. * channels offset will also change the offset for all other
  294. * channels which share the same vref supply. */
  295. ofs_index = ad5360_get_channel_vref_index(st, chan->channel);
  296. return ad5360_write(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
  297. AD5360_REG_SF_OFS(ofs_index), val, 0);
  298. default:
  299. break;
  300. }
  301. return -EINVAL;
  302. }
  303. static int ad5360_read_raw(struct iio_dev *indio_dev,
  304. struct iio_chan_spec const *chan,
  305. int *val,
  306. int *val2,
  307. long m)
  308. {
  309. struct ad5360_state *st = iio_priv(indio_dev);
  310. unsigned int ofs_index;
  311. int scale_uv;
  312. int ret;
  313. switch (m) {
  314. case IIO_CHAN_INFO_RAW:
  315. ret = ad5360_read(indio_dev, AD5360_READBACK_X1A,
  316. chan->address);
  317. if (ret < 0)
  318. return ret;
  319. *val = ret >> chan->scan_type.shift;
  320. return IIO_VAL_INT;
  321. case IIO_CHAN_INFO_SCALE:
  322. scale_uv = ad5360_get_channel_vref(st, chan->channel);
  323. if (scale_uv < 0)
  324. return scale_uv;
  325. /* vout = 4 * vref * dac_code */
  326. *val = scale_uv * 4 / 1000;
  327. *val2 = chan->scan_type.realbits;
  328. return IIO_VAL_FRACTIONAL_LOG2;
  329. case IIO_CHAN_INFO_CALIBBIAS:
  330. ret = ad5360_read(indio_dev, AD5360_READBACK_OFFSET,
  331. chan->address);
  332. if (ret < 0)
  333. return ret;
  334. *val = ret;
  335. return IIO_VAL_INT;
  336. case IIO_CHAN_INFO_CALIBSCALE:
  337. ret = ad5360_read(indio_dev, AD5360_READBACK_GAIN,
  338. chan->address);
  339. if (ret < 0)
  340. return ret;
  341. *val = ret;
  342. return IIO_VAL_INT;
  343. case IIO_CHAN_INFO_OFFSET:
  344. ofs_index = ad5360_get_channel_vref_index(st, chan->channel);
  345. ret = ad5360_read(indio_dev, AD5360_READBACK_SF,
  346. AD5360_REG_SF_OFS(ofs_index));
  347. if (ret < 0)
  348. return ret;
  349. ret <<= (chan->scan_type.realbits - 14);
  350. *val = -ret;
  351. return IIO_VAL_INT;
  352. }
  353. return -EINVAL;
  354. }
  355. static const struct iio_info ad5360_info = {
  356. .read_raw = ad5360_read_raw,
  357. .write_raw = ad5360_write_raw,
  358. .attrs = &ad5360_attribute_group,
  359. .driver_module = THIS_MODULE,
  360. };
  361. static const char * const ad5360_vref_name[] = {
  362. "vref0", "vref1", "vref2"
  363. };
  364. static int ad5360_alloc_channels(struct iio_dev *indio_dev)
  365. {
  366. struct ad5360_state *st = iio_priv(indio_dev);
  367. struct iio_chan_spec *channels;
  368. unsigned int i;
  369. channels = kcalloc(st->chip_info->num_channels,
  370. sizeof(struct iio_chan_spec), GFP_KERNEL);
  371. if (!channels)
  372. return -ENOMEM;
  373. for (i = 0; i < st->chip_info->num_channels; ++i) {
  374. channels[i] = st->chip_info->channel_template;
  375. channels[i].channel = i;
  376. channels[i].address = AD5360_CHAN_ADDR(i);
  377. }
  378. indio_dev->channels = channels;
  379. return 0;
  380. }
  381. static int ad5360_probe(struct spi_device *spi)
  382. {
  383. enum ad5360_type type = spi_get_device_id(spi)->driver_data;
  384. struct iio_dev *indio_dev;
  385. struct ad5360_state *st;
  386. unsigned int i;
  387. int ret;
  388. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  389. if (indio_dev == NULL) {
  390. dev_err(&spi->dev, "Failed to allocate iio device\n");
  391. return -ENOMEM;
  392. }
  393. st = iio_priv(indio_dev);
  394. spi_set_drvdata(spi, indio_dev);
  395. st->chip_info = &ad5360_chip_info_tbl[type];
  396. st->spi = spi;
  397. indio_dev->dev.parent = &spi->dev;
  398. indio_dev->name = spi_get_device_id(spi)->name;
  399. indio_dev->info = &ad5360_info;
  400. indio_dev->modes = INDIO_DIRECT_MODE;
  401. indio_dev->num_channels = st->chip_info->num_channels;
  402. ret = ad5360_alloc_channels(indio_dev);
  403. if (ret) {
  404. dev_err(&spi->dev, "Failed to allocate channel spec: %d\n", ret);
  405. return ret;
  406. }
  407. for (i = 0; i < st->chip_info->num_vrefs; ++i)
  408. st->vref_reg[i].supply = ad5360_vref_name[i];
  409. ret = devm_regulator_bulk_get(&st->spi->dev, st->chip_info->num_vrefs,
  410. st->vref_reg);
  411. if (ret) {
  412. dev_err(&spi->dev, "Failed to request vref regulators: %d\n", ret);
  413. goto error_free_channels;
  414. }
  415. ret = regulator_bulk_enable(st->chip_info->num_vrefs, st->vref_reg);
  416. if (ret) {
  417. dev_err(&spi->dev, "Failed to enable vref regulators: %d\n", ret);
  418. goto error_free_channels;
  419. }
  420. ret = iio_device_register(indio_dev);
  421. if (ret) {
  422. dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
  423. goto error_disable_reg;
  424. }
  425. return 0;
  426. error_disable_reg:
  427. regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
  428. error_free_channels:
  429. kfree(indio_dev->channels);
  430. return ret;
  431. }
  432. static int ad5360_remove(struct spi_device *spi)
  433. {
  434. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  435. struct ad5360_state *st = iio_priv(indio_dev);
  436. iio_device_unregister(indio_dev);
  437. kfree(indio_dev->channels);
  438. regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
  439. return 0;
  440. }
  441. static const struct spi_device_id ad5360_ids[] = {
  442. { "ad5360", ID_AD5360 },
  443. { "ad5361", ID_AD5361 },
  444. { "ad5362", ID_AD5362 },
  445. { "ad5363", ID_AD5363 },
  446. { "ad5370", ID_AD5370 },
  447. { "ad5371", ID_AD5371 },
  448. { "ad5372", ID_AD5372 },
  449. { "ad5373", ID_AD5373 },
  450. {}
  451. };
  452. MODULE_DEVICE_TABLE(spi, ad5360_ids);
  453. static struct spi_driver ad5360_driver = {
  454. .driver = {
  455. .name = "ad5360",
  456. },
  457. .probe = ad5360_probe,
  458. .remove = ad5360_remove,
  459. .id_table = ad5360_ids,
  460. };
  461. module_spi_driver(ad5360_driver);
  462. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  463. MODULE_DESCRIPTION("Analog Devices AD5360/61/62/63/70/71/72/73 DAC");
  464. MODULE_LICENSE("GPL v2");