ad7606_spi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AD7606 SPI ADC driver
  4. *
  5. * Copyright 2011 Analog Devices Inc.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/spi/spi.h>
  9. #include <linux/types.h>
  10. #include <linux/err.h>
  11. #include <linux/iio/iio.h>
  12. #include "ad7606.h"
  13. #define MAX_SPI_FREQ_HZ 23500000 /* VDRIVE above 4.75 V */
  14. #define AD7616_CONFIGURATION_REGISTER 0x02
  15. #define AD7616_OS_MASK GENMASK(4, 2)
  16. #define AD7616_BURST_MODE BIT(6)
  17. #define AD7616_SEQEN_MODE BIT(5)
  18. #define AD7616_RANGE_CH_A_ADDR_OFF 0x04
  19. #define AD7616_RANGE_CH_B_ADDR_OFF 0x06
  20. /*
  21. * Range of channels from a group are stored in 2 registers.
  22. * 0, 1, 2, 3 in a register followed by 4, 5, 6, 7 in second register.
  23. * For channels from second group(8-15) the order is the same, only with
  24. * an offset of 2 for register address.
  25. */
  26. #define AD7616_RANGE_CH_ADDR(ch) ((ch) >> 2)
  27. /* The range of the channel is stored in 2 bits */
  28. #define AD7616_RANGE_CH_MSK(ch) (0b11 << (((ch) & 0b11) * 2))
  29. #define AD7616_RANGE_CH_MODE(ch, mode) ((mode) << ((((ch) & 0b11)) * 2))
  30. #define AD7606_CONFIGURATION_REGISTER 0x02
  31. #define AD7606_SINGLE_DOUT 0x00
  32. /*
  33. * Range for AD7606B channels are stored in registers starting with address 0x3.
  34. * Each register stores range for 2 channels(4 bits per channel).
  35. */
  36. #define AD7606_RANGE_CH_MSK(ch) (GENMASK(3, 0) << (4 * ((ch) & 0x1)))
  37. #define AD7606_RANGE_CH_MODE(ch, mode) \
  38. ((GENMASK(3, 0) & mode) << (4 * ((ch) & 0x1)))
  39. #define AD7606_RANGE_CH_ADDR(ch) (0x03 + ((ch) >> 1))
  40. #define AD7606_OS_MODE 0x08
  41. static const struct iio_chan_spec ad7616_sw_channels[] = {
  42. IIO_CHAN_SOFT_TIMESTAMP(16),
  43. AD7616_CHANNEL(0),
  44. AD7616_CHANNEL(1),
  45. AD7616_CHANNEL(2),
  46. AD7616_CHANNEL(3),
  47. AD7616_CHANNEL(4),
  48. AD7616_CHANNEL(5),
  49. AD7616_CHANNEL(6),
  50. AD7616_CHANNEL(7),
  51. AD7616_CHANNEL(8),
  52. AD7616_CHANNEL(9),
  53. AD7616_CHANNEL(10),
  54. AD7616_CHANNEL(11),
  55. AD7616_CHANNEL(12),
  56. AD7616_CHANNEL(13),
  57. AD7616_CHANNEL(14),
  58. AD7616_CHANNEL(15),
  59. };
  60. static const struct iio_chan_spec ad7606b_sw_channels[] = {
  61. IIO_CHAN_SOFT_TIMESTAMP(8),
  62. AD7616_CHANNEL(0),
  63. AD7616_CHANNEL(1),
  64. AD7616_CHANNEL(2),
  65. AD7616_CHANNEL(3),
  66. AD7616_CHANNEL(4),
  67. AD7616_CHANNEL(5),
  68. AD7616_CHANNEL(6),
  69. AD7616_CHANNEL(7),
  70. };
  71. static const unsigned int ad7606B_oversampling_avail[9] = {
  72. 1, 2, 4, 8, 16, 32, 64, 128, 256
  73. };
  74. static u16 ad7616_spi_rd_wr_cmd(int addr, char isWriteOp)
  75. {
  76. /*
  77. * The address of register consist of one w/r bit
  78. * 6 bits of address followed by one reserved bit.
  79. */
  80. return ((addr & 0x7F) << 1) | ((isWriteOp & 0x1) << 7);
  81. }
  82. static u16 ad7606B_spi_rd_wr_cmd(int addr, char is_write_op)
  83. {
  84. /*
  85. * The address of register consists of one bit which
  86. * specifies a read command placed in bit 6, followed by
  87. * 6 bits of address.
  88. */
  89. return (addr & 0x3F) | (((~is_write_op) & 0x1) << 6);
  90. }
  91. static int ad7606_spi_read_block(struct device *dev,
  92. int count, void *buf)
  93. {
  94. struct spi_device *spi = to_spi_device(dev);
  95. int i, ret;
  96. unsigned short *data = buf;
  97. __be16 *bdata = buf;
  98. ret = spi_read(spi, buf, count * 2);
  99. if (ret < 0) {
  100. dev_err(&spi->dev, "SPI read error\n");
  101. return ret;
  102. }
  103. for (i = 0; i < count; i++)
  104. data[i] = be16_to_cpu(bdata[i]);
  105. return 0;
  106. }
  107. static int ad7606_spi_reg_read(struct ad7606_state *st, unsigned int addr)
  108. {
  109. struct spi_device *spi = to_spi_device(st->dev);
  110. struct spi_transfer t[] = {
  111. {
  112. .tx_buf = &st->d16[0],
  113. .len = 2,
  114. .cs_change = 0,
  115. }, {
  116. .rx_buf = &st->d16[1],
  117. .len = 2,
  118. },
  119. };
  120. int ret;
  121. st->d16[0] = cpu_to_be16(st->bops->rd_wr_cmd(addr, 0) << 8);
  122. ret = spi_sync_transfer(spi, t, ARRAY_SIZE(t));
  123. if (ret < 0)
  124. return ret;
  125. return be16_to_cpu(st->d16[1]);
  126. }
  127. static int ad7606_spi_reg_write(struct ad7606_state *st,
  128. unsigned int addr,
  129. unsigned int val)
  130. {
  131. struct spi_device *spi = to_spi_device(st->dev);
  132. st->d16[0] = cpu_to_be16((st->bops->rd_wr_cmd(addr, 1) << 8) |
  133. (val & 0x1FF));
  134. return spi_write(spi, &st->d16[0], sizeof(st->d16[0]));
  135. }
  136. static int ad7606_spi_write_mask(struct ad7606_state *st,
  137. unsigned int addr,
  138. unsigned long mask,
  139. unsigned int val)
  140. {
  141. int readval;
  142. readval = st->bops->reg_read(st, addr);
  143. if (readval < 0)
  144. return readval;
  145. readval &= ~mask;
  146. readval |= val;
  147. return st->bops->reg_write(st, addr, readval);
  148. }
  149. static int ad7616_write_scale_sw(struct iio_dev *indio_dev, int ch, int val)
  150. {
  151. struct ad7606_state *st = iio_priv(indio_dev);
  152. unsigned int ch_addr, mode, ch_index;
  153. /*
  154. * Ad7616 has 16 channels divided in group A and group B.
  155. * The range of channels from A are stored in registers with address 4
  156. * while channels from B are stored in register with address 6.
  157. * The last bit from channels determines if it is from group A or B
  158. * because the order of channels in iio is 0A, 0B, 1A, 1B...
  159. */
  160. ch_index = ch >> 1;
  161. ch_addr = AD7616_RANGE_CH_ADDR(ch_index);
  162. if ((ch & 0x1) == 0) /* channel A */
  163. ch_addr += AD7616_RANGE_CH_A_ADDR_OFF;
  164. else /* channel B */
  165. ch_addr += AD7616_RANGE_CH_B_ADDR_OFF;
  166. /* 0b01 for 2.5v, 0b10 for 5v and 0b11 for 10v */
  167. mode = AD7616_RANGE_CH_MODE(ch_index, ((val + 1) & 0b11));
  168. return st->bops->write_mask(st, ch_addr, AD7616_RANGE_CH_MSK(ch_index),
  169. mode);
  170. }
  171. static int ad7616_write_os_sw(struct iio_dev *indio_dev, int val)
  172. {
  173. struct ad7606_state *st = iio_priv(indio_dev);
  174. return st->bops->write_mask(st, AD7616_CONFIGURATION_REGISTER,
  175. AD7616_OS_MASK, val << 2);
  176. }
  177. static int ad7606_write_scale_sw(struct iio_dev *indio_dev, int ch, int val)
  178. {
  179. struct ad7606_state *st = iio_priv(indio_dev);
  180. return ad7606_spi_write_mask(st,
  181. AD7606_RANGE_CH_ADDR(ch),
  182. AD7606_RANGE_CH_MSK(ch),
  183. AD7606_RANGE_CH_MODE(ch, val));
  184. }
  185. static int ad7606_write_os_sw(struct iio_dev *indio_dev, int val)
  186. {
  187. struct ad7606_state *st = iio_priv(indio_dev);
  188. return ad7606_spi_reg_write(st, AD7606_OS_MODE, val);
  189. }
  190. static int ad7616_sw_mode_config(struct iio_dev *indio_dev)
  191. {
  192. struct ad7606_state *st = iio_priv(indio_dev);
  193. /*
  194. * Scale can be configured individually for each channel
  195. * in software mode.
  196. */
  197. indio_dev->channels = ad7616_sw_channels;
  198. st->write_scale = ad7616_write_scale_sw;
  199. st->write_os = &ad7616_write_os_sw;
  200. /* Activate Burst mode and SEQEN MODE */
  201. return st->bops->write_mask(st,
  202. AD7616_CONFIGURATION_REGISTER,
  203. AD7616_BURST_MODE | AD7616_SEQEN_MODE,
  204. AD7616_BURST_MODE | AD7616_SEQEN_MODE);
  205. }
  206. static int ad7606B_sw_mode_config(struct iio_dev *indio_dev)
  207. {
  208. struct ad7606_state *st = iio_priv(indio_dev);
  209. unsigned long os[3] = {1};
  210. /*
  211. * Software mode is enabled when all three oversampling
  212. * pins are set to high. If oversampling gpios are defined
  213. * in the device tree, then they need to be set to high,
  214. * otherwise, they must be hardwired to VDD
  215. */
  216. if (st->gpio_os) {
  217. gpiod_set_array_value(ARRAY_SIZE(os),
  218. st->gpio_os->desc, st->gpio_os->info, os);
  219. }
  220. /* OS of 128 and 256 are available only in software mode */
  221. st->oversampling_avail = ad7606B_oversampling_avail;
  222. st->num_os_ratios = ARRAY_SIZE(ad7606B_oversampling_avail);
  223. st->write_scale = ad7606_write_scale_sw;
  224. st->write_os = &ad7606_write_os_sw;
  225. /* Configure device spi to output on a single channel */
  226. st->bops->reg_write(st,
  227. AD7606_CONFIGURATION_REGISTER,
  228. AD7606_SINGLE_DOUT);
  229. /*
  230. * Scale can be configured individually for each channel
  231. * in software mode.
  232. */
  233. indio_dev->channels = ad7606b_sw_channels;
  234. return 0;
  235. }
  236. static const struct ad7606_bus_ops ad7606_spi_bops = {
  237. .read_block = ad7606_spi_read_block,
  238. };
  239. static const struct ad7606_bus_ops ad7616_spi_bops = {
  240. .read_block = ad7606_spi_read_block,
  241. .reg_read = ad7606_spi_reg_read,
  242. .reg_write = ad7606_spi_reg_write,
  243. .write_mask = ad7606_spi_write_mask,
  244. .rd_wr_cmd = ad7616_spi_rd_wr_cmd,
  245. .sw_mode_config = ad7616_sw_mode_config,
  246. };
  247. static const struct ad7606_bus_ops ad7606B_spi_bops = {
  248. .read_block = ad7606_spi_read_block,
  249. .reg_read = ad7606_spi_reg_read,
  250. .reg_write = ad7606_spi_reg_write,
  251. .write_mask = ad7606_spi_write_mask,
  252. .rd_wr_cmd = ad7606B_spi_rd_wr_cmd,
  253. .sw_mode_config = ad7606B_sw_mode_config,
  254. };
  255. static int ad7606_spi_probe(struct spi_device *spi)
  256. {
  257. const struct spi_device_id *id = spi_get_device_id(spi);
  258. const struct ad7606_bus_ops *bops;
  259. switch (id->driver_data) {
  260. case ID_AD7616:
  261. bops = &ad7616_spi_bops;
  262. break;
  263. case ID_AD7606B:
  264. bops = &ad7606B_spi_bops;
  265. break;
  266. default:
  267. bops = &ad7606_spi_bops;
  268. break;
  269. }
  270. return ad7606_probe(&spi->dev, spi->irq, NULL,
  271. id->name, id->driver_data,
  272. bops);
  273. }
  274. static const struct spi_device_id ad7606_id_table[] = {
  275. { "ad7605-4", ID_AD7605_4 },
  276. { "ad7606-4", ID_AD7606_4 },
  277. { "ad7606-6", ID_AD7606_6 },
  278. { "ad7606-8", ID_AD7606_8 },
  279. { "ad7606b", ID_AD7606B },
  280. { "ad7616", ID_AD7616 },
  281. {}
  282. };
  283. MODULE_DEVICE_TABLE(spi, ad7606_id_table);
  284. static const struct of_device_id ad7606_of_match[] = {
  285. { .compatible = "adi,ad7605-4" },
  286. { .compatible = "adi,ad7606-4" },
  287. { .compatible = "adi,ad7606-6" },
  288. { .compatible = "adi,ad7606-8" },
  289. { .compatible = "adi,ad7606b" },
  290. { .compatible = "adi,ad7616" },
  291. { },
  292. };
  293. MODULE_DEVICE_TABLE(of, ad7606_of_match);
  294. static struct spi_driver ad7606_driver = {
  295. .driver = {
  296. .name = "ad7606",
  297. .of_match_table = ad7606_of_match,
  298. .pm = AD7606_PM_OPS,
  299. },
  300. .probe = ad7606_spi_probe,
  301. .id_table = ad7606_id_table,
  302. };
  303. module_spi_driver(ad7606_driver);
  304. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  305. MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
  306. MODULE_LICENSE("GPL v2");
  307. MODULE_IMPORT_NS(IIO_AD7606);