ad5360.c 13 KB

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