ad5360.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. struct spi_message m;
  186. int ret;
  187. struct spi_transfer t[] = {
  188. {
  189. .tx_buf = &st->data[0].d8[1],
  190. .len = 3,
  191. .cs_change = 1,
  192. }, {
  193. .rx_buf = &st->data[1].d8[1],
  194. .len = 3,
  195. },
  196. };
  197. spi_message_init(&m);
  198. spi_message_add_tail(&t[0], &m);
  199. spi_message_add_tail(&t[1], &m);
  200. mutex_lock(&indio_dev->mlock);
  201. st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
  202. AD5360_ADDR(AD5360_REG_SF_READBACK) |
  203. AD5360_READBACK_TYPE(type) |
  204. AD5360_READBACK_ADDR(addr));
  205. ret = spi_sync(st->spi, &m);
  206. if (ret >= 0)
  207. ret = be32_to_cpu(st->data[1].d32) & 0xffff;
  208. mutex_unlock(&indio_dev->mlock);
  209. return ret;
  210. }
  211. static ssize_t ad5360_read_dac_powerdown(struct device *dev,
  212. struct device_attribute *attr,
  213. char *buf)
  214. {
  215. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  216. struct ad5360_state *st = iio_priv(indio_dev);
  217. return sprintf(buf, "%d\n", (bool)(st->ctrl & AD5360_SF_CTRL_PWR_DOWN));
  218. }
  219. static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
  220. unsigned int clr)
  221. {
  222. struct ad5360_state *st = iio_priv(indio_dev);
  223. unsigned int ret;
  224. mutex_lock(&indio_dev->mlock);
  225. st->ctrl |= set;
  226. st->ctrl &= ~clr;
  227. ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
  228. AD5360_REG_SF_CTRL, st->ctrl, 0);
  229. mutex_unlock(&indio_dev->mlock);
  230. return ret;
  231. }
  232. static ssize_t ad5360_write_dac_powerdown(struct device *dev,
  233. struct device_attribute *attr, const char *buf, size_t len)
  234. {
  235. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  236. bool pwr_down;
  237. int ret;
  238. ret = strtobool(buf, &pwr_down);
  239. if (ret)
  240. return ret;
  241. if (pwr_down)
  242. ret = ad5360_update_ctrl(indio_dev, AD5360_SF_CTRL_PWR_DOWN, 0);
  243. else
  244. ret = ad5360_update_ctrl(indio_dev, 0, AD5360_SF_CTRL_PWR_DOWN);
  245. return ret ? ret : len;
  246. }
  247. static IIO_DEVICE_ATTR(out_voltage_powerdown,
  248. S_IRUGO | S_IWUSR,
  249. ad5360_read_dac_powerdown,
  250. ad5360_write_dac_powerdown, 0);
  251. static struct attribute *ad5360_attributes[] = {
  252. &iio_dev_attr_out_voltage_powerdown.dev_attr.attr,
  253. NULL,
  254. };
  255. static const struct attribute_group ad5360_attribute_group = {
  256. .attrs = ad5360_attributes,
  257. };
  258. static int ad5360_write_raw(struct iio_dev *indio_dev,
  259. struct iio_chan_spec const *chan,
  260. int val,
  261. int val2,
  262. long mask)
  263. {
  264. struct ad5360_state *st = iio_priv(indio_dev);
  265. int max_val = (1 << chan->scan_type.realbits);
  266. unsigned int ofs_index;
  267. switch (mask) {
  268. case IIO_CHAN_INFO_RAW:
  269. if (val >= max_val || val < 0)
  270. return -EINVAL;
  271. return ad5360_write(indio_dev, AD5360_CMD_WRITE_DATA,
  272. chan->address, val, chan->scan_type.shift);
  273. case IIO_CHAN_INFO_CALIBBIAS:
  274. if (val >= max_val || val < 0)
  275. return -EINVAL;
  276. return ad5360_write(indio_dev, AD5360_CMD_WRITE_OFFSET,
  277. chan->address, val, chan->scan_type.shift);
  278. case IIO_CHAN_INFO_CALIBSCALE:
  279. if (val >= max_val || val < 0)
  280. return -EINVAL;
  281. return ad5360_write(indio_dev, AD5360_CMD_WRITE_GAIN,
  282. chan->address, val, chan->scan_type.shift);
  283. case IIO_CHAN_INFO_OFFSET:
  284. if (val <= -max_val || val > 0)
  285. return -EINVAL;
  286. val = -val;
  287. /* offset is supposed to have the same scale as raw, but it
  288. * is always 14bits wide, so on a chip where the raw value has
  289. * more bits, we need to shift offset. */
  290. val >>= (chan->scan_type.realbits - 14);
  291. /* There is one DAC offset register per vref. Changing one
  292. * channels offset will also change the offset for all other
  293. * channels which share the same vref supply. */
  294. ofs_index = ad5360_get_channel_vref_index(st, chan->channel);
  295. return ad5360_write(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
  296. AD5360_REG_SF_OFS(ofs_index), val, 0);
  297. default:
  298. break;
  299. }
  300. return -EINVAL;
  301. }
  302. static int ad5360_read_raw(struct iio_dev *indio_dev,
  303. struct iio_chan_spec const *chan,
  304. int *val,
  305. int *val2,
  306. long m)
  307. {
  308. struct ad5360_state *st = iio_priv(indio_dev);
  309. unsigned int ofs_index;
  310. int scale_uv;
  311. int ret;
  312. switch (m) {
  313. case IIO_CHAN_INFO_RAW:
  314. ret = ad5360_read(indio_dev, AD5360_READBACK_X1A,
  315. chan->address);
  316. if (ret < 0)
  317. return ret;
  318. *val = ret >> chan->scan_type.shift;
  319. return IIO_VAL_INT;
  320. case IIO_CHAN_INFO_SCALE:
  321. /* vout = 4 * vref * dac_code */
  322. scale_uv = ad5360_get_channel_vref(st, chan->channel) * 4 * 100;
  323. if (scale_uv < 0)
  324. return scale_uv;
  325. scale_uv >>= (chan->scan_type.realbits);
  326. *val = scale_uv / 100000;
  327. *val2 = (scale_uv % 100000) * 10;
  328. return IIO_VAL_INT_PLUS_MICRO;
  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 = iio_device_alloc(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. goto error_free;
  406. }
  407. for (i = 0; i < st->chip_info->num_vrefs; ++i)
  408. st->vref_reg[i].supply = ad5360_vref_name[i];
  409. ret = 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_reg;
  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_reg:
  429. regulator_bulk_free(st->chip_info->num_vrefs, st->vref_reg);
  430. error_free_channels:
  431. kfree(indio_dev->channels);
  432. error_free:
  433. iio_device_free(indio_dev);
  434. return ret;
  435. }
  436. static int ad5360_remove(struct spi_device *spi)
  437. {
  438. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  439. struct ad5360_state *st = iio_priv(indio_dev);
  440. iio_device_unregister(indio_dev);
  441. kfree(indio_dev->channels);
  442. regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
  443. regulator_bulk_free(st->chip_info->num_vrefs, st->vref_reg);
  444. iio_device_free(indio_dev);
  445. return 0;
  446. }
  447. static const struct spi_device_id ad5360_ids[] = {
  448. { "ad5360", ID_AD5360 },
  449. { "ad5361", ID_AD5361 },
  450. { "ad5362", ID_AD5362 },
  451. { "ad5363", ID_AD5363 },
  452. { "ad5370", ID_AD5370 },
  453. { "ad5371", ID_AD5371 },
  454. { "ad5372", ID_AD5372 },
  455. { "ad5373", ID_AD5373 },
  456. {}
  457. };
  458. MODULE_DEVICE_TABLE(spi, ad5360_ids);
  459. static struct spi_driver ad5360_driver = {
  460. .driver = {
  461. .name = "ad5360",
  462. .owner = THIS_MODULE,
  463. },
  464. .probe = ad5360_probe,
  465. .remove = ad5360_remove,
  466. .id_table = ad5360_ids,
  467. };
  468. module_spi_driver(ad5360_driver);
  469. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  470. MODULE_DESCRIPTION("Analog Devices AD5360/61/62/63/70/71/72/73 DAC");
  471. MODULE_LICENSE("GPL v2");