ad7779.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * AD7770, AD7771, AD7779 ADC
  4. *
  5. * Copyright 2023-2024 Analog Devices Inc.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bitmap.h>
  9. #include <linux/clk.h>
  10. #include <linux/crc8.h>
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/math.h>
  17. #include <linux/module.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/string.h>
  22. #include <linux/types.h>
  23. #include <linux/unaligned.h>
  24. #include <linux/units.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/backend.h>
  27. #include <linux/iio/buffer.h>
  28. #include <linux/iio/sysfs.h>
  29. #include <linux/iio/trigger.h>
  30. #include <linux/iio/triggered_buffer.h>
  31. #include <linux/iio/trigger_consumer.h>
  32. #define AD7779_SPI_READ_CMD BIT(7)
  33. #define AD7779_DISABLE_SD BIT(7)
  34. #define AD7779_REG_CH_DISABLE 0x08
  35. #define AD7779_REG_CH_SYNC_OFFSET(ch) (0x09 + (ch))
  36. #define AD7779_REG_CH_CONFIG(ch) (0x00 + (ch))
  37. #define AD7779_REG_GENERAL_USER_CONFIG_1 0x11
  38. #define AD7779_REG_GENERAL_USER_CONFIG_2 0x12
  39. #define AD7779_REG_GENERAL_USER_CONFIG_3 0x13
  40. #define AD7779_REG_DOUT_FORMAT 0x14
  41. #define AD7779_REG_ADC_MUX_CONFIG 0x15
  42. #define AD7779_REG_GPIO_CONFIG 0x17
  43. #define AD7779_REG_BUFFER_CONFIG_1 0x19
  44. #define AD7779_REG_GLOBAL_MUX_CONFIG 0x16
  45. #define AD7779_REG_BUFFER_CONFIG_2 0x1A
  46. #define AD7779_REG_GPIO_DATA 0x18
  47. #define AD7779_REG_CH_OFFSET_UPPER_BYTE(ch) (0x1C + (ch) * 6)
  48. #define AD7779_REG_CH_OFFSET_LOWER_BYTE(ch) (0x1E + (ch) * 6)
  49. #define AD7779_REG_CH_GAIN_UPPER_BYTE(ch) (0x1F + (ch) * 6)
  50. #define AD7779_REG_CH_OFFSET_MID_BYTE(ch) (0x1D + (ch) * 6)
  51. #define AD7779_REG_CH_GAIN_MID_BYTE(ch) (0x20 + (ch) * 6)
  52. #define AD7779_REG_CH_ERR_REG(ch) (0x4C + (ch))
  53. #define AD7779_REG_CH0_1_SAT_ERR 0x54
  54. #define AD7779_REG_CH_GAIN_LOWER_BYTE(ch) (0x21 + (ch) * 6)
  55. #define AD7779_REG_CH2_3_SAT_ERR 0x55
  56. #define AD7779_REG_CH4_5_SAT_ERR 0x56
  57. #define AD7779_REG_CH6_7_SAT_ERR 0x57
  58. #define AD7779_REG_CHX_ERR_REG_EN 0x58
  59. #define AD7779_REG_GEN_ERR_REG_1 0x59
  60. #define AD7779_REG_GEN_ERR_REG_1_EN 0x5A
  61. #define AD7779_REG_GEN_ERR_REG_2 0x5B
  62. #define AD7779_REG_GEN_ERR_REG_2_EN 0x5C
  63. #define AD7779_REG_STATUS_REG_1 0x5D
  64. #define AD7779_REG_STATUS_REG_2 0x5E
  65. #define AD7779_REG_STATUS_REG_3 0x5F
  66. #define AD7779_REG_SRC_N_MSB 0x60
  67. #define AD7779_REG_SRC_N_LSB 0x61
  68. #define AD7779_REG_SRC_IF_MSB 0x62
  69. #define AD7779_REG_SRC_IF_LSB 0x63
  70. #define AD7779_REG_SRC_UPDATE 0x64
  71. #define AD7779_FILTER_MSK BIT(6)
  72. #define AD7779_MOD_POWERMODE_MSK BIT(6)
  73. #define AD7779_MOD_PDB_REFOUT_MSK BIT(4)
  74. #define AD7779_MOD_SPI_EN_MSK BIT(4)
  75. #define AD7779_USRMOD_INIT_MSK GENMASK(6, 4)
  76. /* AD7779_REG_DOUT_FORMAT */
  77. #define AD7779_DOUT_FORMAT_MSK GENMASK(7, 6)
  78. #define AD7779_DOUT_HEADER_FORMAT BIT(5)
  79. #define AD7779_DCLK_CLK_DIV_MSK GENMASK(3, 1)
  80. #define AD7779_REFMUX_CTRL_MSK GENMASK(7, 6)
  81. #define AD7779_SPI_CRC_EN_MSK BIT(0)
  82. #define AD7779_MAXCLK_LOWPOWER (4096 * HZ_PER_KHZ)
  83. #define AD7779_NUM_CHANNELS 8
  84. #define AD7779_RESET_BUF_SIZE 8
  85. #define AD7779_CHAN_DATA_SIZE 4
  86. #define AD7779_LOWPOWER_DIV 512
  87. #define AD7779_HIGHPOWER_DIV 2048
  88. #define AD7779_SINC3_MAXFREQ (16 * HZ_PER_KHZ)
  89. #define AD7779_SINC5_MAXFREQ (128 * HZ_PER_KHZ)
  90. #define AD7779_DEFAULT_SAMPLING_FREQ (8 * HZ_PER_KHZ)
  91. #define AD7779_DEFAULT_SAMPLING_2LINE (4 * HZ_PER_KHZ)
  92. #define AD7779_DEFAULT_SAMPLING_1LINE (2 * HZ_PER_KHZ)
  93. #define AD7779_SPIMODE_MAX_SAMP_FREQ (16 * HZ_PER_KHZ)
  94. #define GAIN_REL 0x555555
  95. #define AD7779_FREQ_MSB_MSK GENMASK(15, 8)
  96. #define AD7779_FREQ_LSB_MSK GENMASK(7, 0)
  97. #define AD7779_UPPER GENMASK(23, 16)
  98. #define AD7779_MID GENMASK(15, 8)
  99. #define AD7779_LOWER GENMASK(7, 0)
  100. #define AD7779_REG_MSK GENMASK(6, 0)
  101. #define AD7779_CRC8_POLY 0x07
  102. DECLARE_CRC8_TABLE(ad7779_crc8_table);
  103. enum ad7779_filter {
  104. AD7779_SINC3,
  105. AD7779_SINC5,
  106. };
  107. enum ad7779_variant {
  108. ad7770,
  109. ad7771,
  110. ad7779,
  111. };
  112. enum ad7779_power_mode {
  113. AD7779_LOW_POWER,
  114. AD7779_HIGH_POWER,
  115. };
  116. struct ad7779_chip_info {
  117. const char *name;
  118. struct iio_chan_spec const *channels;
  119. };
  120. struct ad7779_state {
  121. struct spi_device *spi;
  122. const struct ad7779_chip_info *chip_info;
  123. struct clk *mclk;
  124. struct iio_trigger *trig;
  125. struct completion completion;
  126. unsigned int sampling_freq;
  127. enum ad7779_filter filter_enabled;
  128. struct iio_backend *back;
  129. /*
  130. * DMA (thus cache coherency maintenance) requires the
  131. * transfer buffers to live in their own cache lines.
  132. */
  133. struct {
  134. u32 chans[8];
  135. aligned_s64 timestamp;
  136. } data __aligned(IIO_DMA_MINALIGN);
  137. u32 spidata_tx[8];
  138. u8 reg_rx_buf[3];
  139. u8 reg_tx_buf[3];
  140. u8 reset_buf[8];
  141. };
  142. static const char * const ad7779_filter_type[] = {
  143. [AD7779_SINC3] = "sinc3",
  144. [AD7779_SINC5] = "sinc5",
  145. };
  146. static const char * const ad7779_power_supplies[] = {
  147. "avdd1", "avdd2", "avdd4",
  148. };
  149. static int ad7779_spi_read(struct ad7779_state *st, u8 reg, u8 *rbuf)
  150. {
  151. int ret;
  152. u8 crc_buf[2];
  153. u8 exp_crc;
  154. struct spi_transfer t = {
  155. .tx_buf = st->reg_tx_buf,
  156. .rx_buf = st->reg_rx_buf,
  157. };
  158. st->reg_tx_buf[0] = AD7779_SPI_READ_CMD | FIELD_GET(AD7779_REG_MSK, reg);
  159. st->reg_tx_buf[1] = 0;
  160. if (reg == AD7779_REG_GEN_ERR_REG_1_EN) {
  161. t.len = 2;
  162. } else {
  163. t.len = 3;
  164. st->reg_tx_buf[2] = crc8(ad7779_crc8_table, st->reg_tx_buf,
  165. t.len - 1, 0);
  166. }
  167. ret = spi_sync_transfer(st->spi, &t, 1);
  168. if (ret)
  169. return ret;
  170. crc_buf[0] = AD7779_SPI_READ_CMD | FIELD_GET(AD7779_REG_MSK, reg);
  171. crc_buf[1] = st->reg_rx_buf[1];
  172. exp_crc = crc8(ad7779_crc8_table, crc_buf, ARRAY_SIZE(crc_buf), 0);
  173. if (reg != AD7779_REG_GEN_ERR_REG_1_EN && exp_crc != st->reg_rx_buf[2]) {
  174. dev_err(&st->spi->dev, "Bad CRC %x, expected %x",
  175. st->reg_rx_buf[2], exp_crc);
  176. return -EINVAL;
  177. }
  178. *rbuf = st->reg_rx_buf[1];
  179. return 0;
  180. }
  181. static int ad7779_spi_write(struct ad7779_state *st, u8 reg, u8 val)
  182. {
  183. u8 length = 3;
  184. st->reg_tx_buf[0] = FIELD_GET(AD7779_REG_MSK, reg);
  185. st->reg_tx_buf[1] = val;
  186. if (reg == AD7779_REG_GEN_ERR_REG_1_EN)
  187. length = 2;
  188. else
  189. st->reg_tx_buf[2] = crc8(ad7779_crc8_table, st->reg_tx_buf,
  190. length - 1, 0);
  191. return spi_write(st->spi, st->reg_tx_buf, length);
  192. }
  193. static int ad7779_spi_write_mask(struct ad7779_state *st, u8 reg, u8 mask,
  194. u8 val)
  195. {
  196. int ret;
  197. u8 regval, data;
  198. ret = ad7779_spi_read(st, reg, &data);
  199. if (ret)
  200. return ret;
  201. regval = (data & ~mask) | (val & mask);
  202. if (regval == data)
  203. return 0;
  204. return ad7779_spi_write(st, reg, regval);
  205. }
  206. static int ad7779_reg_access(struct iio_dev *indio_dev,
  207. unsigned int reg,
  208. unsigned int writeval,
  209. unsigned int *readval)
  210. {
  211. struct ad7779_state *st = iio_priv(indio_dev);
  212. u8 rval;
  213. int ret;
  214. if (readval) {
  215. ret = ad7779_spi_read(st, reg, &rval);
  216. *readval = rval;
  217. return ret;
  218. }
  219. return ad7779_spi_write(st, reg, writeval);
  220. }
  221. static int ad7779_set_sampling_frequency(struct ad7779_state *st,
  222. unsigned int sampling_freq)
  223. {
  224. int ret;
  225. unsigned int dec;
  226. unsigned int frac;
  227. unsigned int div;
  228. unsigned int decimal;
  229. unsigned int freq_khz;
  230. if (st->filter_enabled == AD7779_SINC3 &&
  231. sampling_freq > AD7779_SINC3_MAXFREQ)
  232. return -EINVAL;
  233. if (st->filter_enabled == AD7779_SINC5 &&
  234. sampling_freq > AD7779_SINC5_MAXFREQ)
  235. return -EINVAL;
  236. if (sampling_freq > AD7779_SPIMODE_MAX_SAMP_FREQ)
  237. return -EINVAL;
  238. div = AD7779_HIGHPOWER_DIV;
  239. freq_khz = sampling_freq / HZ_PER_KHZ;
  240. dec = div / freq_khz;
  241. frac = div % freq_khz;
  242. ret = ad7779_spi_write(st, AD7779_REG_SRC_N_MSB,
  243. FIELD_GET(AD7779_FREQ_MSB_MSK, dec));
  244. if (ret)
  245. return ret;
  246. ret = ad7779_spi_write(st, AD7779_REG_SRC_N_LSB,
  247. FIELD_GET(AD7779_FREQ_LSB_MSK, dec));
  248. if (ret)
  249. return ret;
  250. if (frac) {
  251. /*
  252. * In order to obtain the first three decimals of the decimation
  253. * the initial number is multiplied with 10^3 prior to the
  254. * division, then the original division result is subtracted and
  255. * the number is divided by 10^3.
  256. */
  257. decimal = ((mult_frac(div, KILO, freq_khz) - dec * KILO) << 16)
  258. / KILO;
  259. ret = ad7779_spi_write(st, AD7779_REG_SRC_N_MSB,
  260. FIELD_GET(AD7779_FREQ_MSB_MSK, decimal));
  261. if (ret)
  262. return ret;
  263. ret = ad7779_spi_write(st, AD7779_REG_SRC_N_LSB,
  264. FIELD_GET(AD7779_FREQ_LSB_MSK, decimal));
  265. if (ret)
  266. return ret;
  267. } else {
  268. ret = ad7779_spi_write(st, AD7779_REG_SRC_N_MSB,
  269. FIELD_GET(AD7779_FREQ_MSB_MSK, 0x0));
  270. if (ret)
  271. return ret;
  272. ret = ad7779_spi_write(st, AD7779_REG_SRC_N_LSB,
  273. FIELD_GET(AD7779_FREQ_LSB_MSK, 0x0));
  274. if (ret)
  275. return ret;
  276. }
  277. ret = ad7779_spi_write(st, AD7779_REG_SRC_UPDATE, BIT(0));
  278. if (ret)
  279. return ret;
  280. /* SRC update settling time */
  281. fsleep(15);
  282. ret = ad7779_spi_write(st, AD7779_REG_SRC_UPDATE, 0x0);
  283. if (ret)
  284. return ret;
  285. /* SRC update settling time */
  286. fsleep(15);
  287. st->sampling_freq = sampling_freq;
  288. return 0;
  289. }
  290. static int ad7779_get_filter(struct iio_dev *indio_dev,
  291. struct iio_chan_spec const *chan)
  292. {
  293. struct ad7779_state *st = iio_priv(indio_dev);
  294. u8 temp;
  295. int ret;
  296. ret = ad7779_spi_read(st, AD7779_REG_GENERAL_USER_CONFIG_2, &temp);
  297. if (ret)
  298. return ret;
  299. return FIELD_GET(AD7779_FILTER_MSK, temp);
  300. }
  301. static int ad7779_set_filter(struct iio_dev *indio_dev,
  302. struct iio_chan_spec const *chan,
  303. unsigned int mode)
  304. {
  305. struct ad7779_state *st = iio_priv(indio_dev);
  306. int ret;
  307. ret = ad7779_spi_write_mask(st,
  308. AD7779_REG_GENERAL_USER_CONFIG_2,
  309. AD7779_FILTER_MSK,
  310. FIELD_PREP(AD7779_FILTER_MSK, mode));
  311. if (ret)
  312. return ret;
  313. ret = ad7779_set_sampling_frequency(st, st->sampling_freq);
  314. if (ret)
  315. return ret;
  316. st->filter_enabled = mode;
  317. return 0;
  318. }
  319. static int ad7779_get_calibscale(struct ad7779_state *st, int channel)
  320. {
  321. int ret;
  322. u8 calibscale[3];
  323. ret = ad7779_spi_read(st, AD7779_REG_CH_GAIN_LOWER_BYTE(channel),
  324. &calibscale[0]);
  325. if (ret)
  326. return ret;
  327. ret = ad7779_spi_read(st, AD7779_REG_CH_GAIN_MID_BYTE(channel),
  328. &calibscale[1]);
  329. if (ret)
  330. return ret;
  331. ret = ad7779_spi_read(st, AD7779_REG_CH_GAIN_UPPER_BYTE(channel),
  332. &calibscale[2]);
  333. if (ret)
  334. return ret;
  335. return get_unaligned_be24(calibscale);
  336. }
  337. static int ad7779_set_calibscale(struct ad7779_state *st, int channel, int val)
  338. {
  339. int ret;
  340. unsigned int gain;
  341. u8 gain_bytes[3];
  342. /*
  343. * The gain value is relative to 0x555555, which represents a gain of 1
  344. */
  345. gain = DIV_ROUND_CLOSEST_ULL((u64)val * 5592405LL, MEGA);
  346. put_unaligned_be24(gain, gain_bytes);
  347. ret = ad7779_spi_write(st, AD7779_REG_CH_GAIN_UPPER_BYTE(channel),
  348. gain_bytes[0]);
  349. if (ret)
  350. return ret;
  351. ret = ad7779_spi_write(st, AD7779_REG_CH_GAIN_MID_BYTE(channel),
  352. gain_bytes[1]);
  353. if (ret)
  354. return ret;
  355. return ad7779_spi_write(st, AD7779_REG_CH_GAIN_LOWER_BYTE(channel),
  356. gain_bytes[2]);
  357. }
  358. static int ad7779_get_calibbias(struct ad7779_state *st, int channel)
  359. {
  360. int ret;
  361. u8 calibbias[3];
  362. ret = ad7779_spi_read(st, AD7779_REG_CH_OFFSET_LOWER_BYTE(channel),
  363. &calibbias[0]);
  364. if (ret)
  365. return ret;
  366. ret = ad7779_spi_read(st, AD7779_REG_CH_OFFSET_MID_BYTE(channel),
  367. &calibbias[1]);
  368. if (ret)
  369. return ret;
  370. ret = ad7779_spi_read(st, AD7779_REG_CH_OFFSET_UPPER_BYTE(channel),
  371. &calibbias[2]);
  372. if (ret)
  373. return ret;
  374. return get_unaligned_be24(calibbias);
  375. }
  376. static int ad7779_set_calibbias(struct ad7779_state *st, int channel, int val)
  377. {
  378. int ret;
  379. u8 calibbias[3];
  380. put_unaligned_be24(val, calibbias);
  381. ret = ad7779_spi_write(st, AD7779_REG_CH_OFFSET_UPPER_BYTE(channel),
  382. calibbias[0]);
  383. if (ret)
  384. return ret;
  385. ret = ad7779_spi_write(st, AD7779_REG_CH_OFFSET_MID_BYTE(channel),
  386. calibbias[1]);
  387. if (ret)
  388. return ret;
  389. return ad7779_spi_write(st, AD7779_REG_CH_OFFSET_LOWER_BYTE(channel),
  390. calibbias[2]);
  391. }
  392. static int __ad7779_read_raw(struct iio_dev *indio_dev,
  393. struct iio_chan_spec const *chan, int *val,
  394. int *val2, long mask)
  395. {
  396. struct ad7779_state *st = iio_priv(indio_dev);
  397. int ret;
  398. switch (mask) {
  399. case IIO_CHAN_INFO_CALIBSCALE:
  400. ret = ad7779_get_calibscale(st, chan->channel);
  401. if (ret < 0)
  402. return ret;
  403. *val = ret;
  404. *val2 = GAIN_REL;
  405. return IIO_VAL_FRACTIONAL;
  406. case IIO_CHAN_INFO_CALIBBIAS:
  407. ret = ad7779_get_calibbias(st, chan->channel);
  408. if (ret < 0)
  409. return ret;
  410. *val = ret;
  411. return IIO_VAL_INT;
  412. case IIO_CHAN_INFO_SAMP_FREQ:
  413. *val = st->sampling_freq;
  414. if (*val < 0)
  415. return -EINVAL;
  416. return IIO_VAL_INT;
  417. default:
  418. return -EINVAL;
  419. }
  420. }
  421. static int ad7779_read_raw(struct iio_dev *indio_dev,
  422. struct iio_chan_spec const *chan, int *val,
  423. int *val2, long mask)
  424. {
  425. int ret;
  426. if (!iio_device_claim_direct(indio_dev))
  427. return -EBUSY;
  428. ret = __ad7779_read_raw(indio_dev, chan, val, val2, mask);
  429. iio_device_release_direct(indio_dev);
  430. return ret;
  431. }
  432. static int __ad7779_write_raw(struct iio_dev *indio_dev,
  433. struct iio_chan_spec const *chan,
  434. int val, int val2,
  435. long mask)
  436. {
  437. struct ad7779_state *st = iio_priv(indio_dev);
  438. switch (mask) {
  439. case IIO_CHAN_INFO_CALIBSCALE:
  440. return ad7779_set_calibscale(st, chan->channel, val2);
  441. case IIO_CHAN_INFO_CALIBBIAS:
  442. return ad7779_set_calibbias(st, chan->channel, val);
  443. case IIO_CHAN_INFO_SAMP_FREQ:
  444. return ad7779_set_sampling_frequency(st, val);
  445. default:
  446. return -EINVAL;
  447. }
  448. }
  449. static int ad7779_write_raw(struct iio_dev *indio_dev,
  450. struct iio_chan_spec const *chan, int val, int val2,
  451. long mask)
  452. {
  453. int ret;
  454. if (!iio_device_claim_direct(indio_dev))
  455. return -EBUSY;
  456. ret = __ad7779_write_raw(indio_dev, chan, val, val2, mask);
  457. iio_device_release_direct(indio_dev);
  458. return ret;
  459. }
  460. static int ad7779_buffer_preenable(struct iio_dev *indio_dev)
  461. {
  462. int ret;
  463. struct ad7779_state *st = iio_priv(indio_dev);
  464. ret = ad7779_spi_write_mask(st,
  465. AD7779_REG_GENERAL_USER_CONFIG_3,
  466. AD7779_MOD_SPI_EN_MSK,
  467. FIELD_PREP(AD7779_MOD_SPI_EN_MSK, 1));
  468. if (ret)
  469. return ret;
  470. /*
  471. * DRDY output cannot be disabled at device level therefore we mask
  472. * the irq at host end.
  473. */
  474. enable_irq(st->spi->irq);
  475. return 0;
  476. }
  477. static int ad7779_buffer_postdisable(struct iio_dev *indio_dev)
  478. {
  479. struct ad7779_state *st = iio_priv(indio_dev);
  480. disable_irq(st->spi->irq);
  481. return ad7779_spi_write(st, AD7779_REG_GENERAL_USER_CONFIG_3,
  482. AD7779_DISABLE_SD);
  483. }
  484. static irqreturn_t ad7779_trigger_handler(int irq, void *p)
  485. {
  486. struct iio_poll_func *pf = p;
  487. struct iio_dev *indio_dev = pf->indio_dev;
  488. struct ad7779_state *st = iio_priv(indio_dev);
  489. int ret;
  490. struct spi_transfer t = {
  491. .rx_buf = st->data.chans,
  492. .tx_buf = st->spidata_tx,
  493. .len = AD7779_NUM_CHANNELS * AD7779_CHAN_DATA_SIZE,
  494. };
  495. st->spidata_tx[0] = AD7779_SPI_READ_CMD;
  496. ret = spi_sync_transfer(st->spi, &t, 1);
  497. if (ret) {
  498. dev_err(&st->spi->dev, "SPI transfer error in IRQ handler");
  499. goto exit_handler;
  500. }
  501. iio_push_to_buffers_with_ts(indio_dev, &st->data, sizeof(st->data),
  502. pf->timestamp);
  503. exit_handler:
  504. iio_trigger_notify_done(indio_dev->trig);
  505. return IRQ_HANDLED;
  506. }
  507. static int ad7779_reset(struct iio_dev *indio_dev, struct gpio_desc *reset_gpio)
  508. {
  509. struct ad7779_state *st = iio_priv(indio_dev);
  510. int ret;
  511. struct spi_transfer t = {
  512. .tx_buf = st->reset_buf,
  513. .len = 8,
  514. };
  515. if (reset_gpio) {
  516. gpiod_set_value(reset_gpio, 1);
  517. /* Delay for reset to occur is 225 microseconds */
  518. fsleep(230);
  519. ret = 0;
  520. } else {
  521. memset(st->reset_buf, 0xff, sizeof(st->reset_buf));
  522. ret = spi_sync_transfer(st->spi, &t, 1);
  523. if (ret)
  524. return ret;
  525. }
  526. /* Delay for reset to occur is 225 microseconds */
  527. fsleep(230);
  528. return ret;
  529. }
  530. static int ad7779_update_scan_mode(struct iio_dev *indio_dev,
  531. const unsigned long *scan_mask)
  532. {
  533. struct ad7779_state *st = iio_priv(indio_dev);
  534. unsigned int c;
  535. int ret;
  536. for (c = 0; c < AD7779_NUM_CHANNELS; c++) {
  537. if (test_bit(c, scan_mask))
  538. ret = iio_backend_chan_enable(st->back, c);
  539. else
  540. ret = iio_backend_chan_disable(st->back, c);
  541. if (ret)
  542. return ret;
  543. }
  544. return 0;
  545. }
  546. static const struct iio_info ad7779_info = {
  547. .read_raw = ad7779_read_raw,
  548. .write_raw = ad7779_write_raw,
  549. .debugfs_reg_access = &ad7779_reg_access,
  550. };
  551. static const struct iio_info ad7779_info_data = {
  552. .read_raw = ad7779_read_raw,
  553. .write_raw = ad7779_write_raw,
  554. .debugfs_reg_access = &ad7779_reg_access,
  555. .update_scan_mode = &ad7779_update_scan_mode,
  556. };
  557. static const struct iio_enum ad7779_filter_enum = {
  558. .items = ad7779_filter_type,
  559. .num_items = ARRAY_SIZE(ad7779_filter_type),
  560. .get = ad7779_get_filter,
  561. .set = ad7779_set_filter,
  562. };
  563. static const struct iio_chan_spec_ext_info ad7779_ext_filter[] = {
  564. IIO_ENUM("filter_type", IIO_SHARED_BY_ALL, &ad7779_filter_enum),
  565. IIO_ENUM_AVAILABLE("filter_type", IIO_SHARED_BY_ALL,
  566. &ad7779_filter_enum),
  567. { }
  568. };
  569. #define AD777x_CHAN_S(index, _ext_info) \
  570. { \
  571. .type = IIO_VOLTAGE, \
  572. .info_mask_separate = BIT(IIO_CHAN_INFO_CALIBSCALE) | \
  573. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  574. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
  575. .address = (index), \
  576. .indexed = 1, \
  577. .channel = (index), \
  578. .scan_index = (index), \
  579. .ext_info = (_ext_info), \
  580. .scan_type = { \
  581. .sign = 's', \
  582. .realbits = 24, \
  583. .storagebits = 32, \
  584. .endianness = IIO_BE, \
  585. }, \
  586. }
  587. #define AD777x_CHAN_NO_FILTER_S(index) \
  588. AD777x_CHAN_S(index, NULL)
  589. #define AD777x_CHAN_FILTER_S(index) \
  590. AD777x_CHAN_S(index, ad7779_ext_filter)
  591. static const struct iio_chan_spec ad7779_channels[] = {
  592. AD777x_CHAN_NO_FILTER_S(0),
  593. AD777x_CHAN_NO_FILTER_S(1),
  594. AD777x_CHAN_NO_FILTER_S(2),
  595. AD777x_CHAN_NO_FILTER_S(3),
  596. AD777x_CHAN_NO_FILTER_S(4),
  597. AD777x_CHAN_NO_FILTER_S(5),
  598. AD777x_CHAN_NO_FILTER_S(6),
  599. AD777x_CHAN_NO_FILTER_S(7),
  600. IIO_CHAN_SOFT_TIMESTAMP(8),
  601. };
  602. static const struct iio_chan_spec ad7779_channels_filter[] = {
  603. AD777x_CHAN_FILTER_S(0),
  604. AD777x_CHAN_FILTER_S(1),
  605. AD777x_CHAN_FILTER_S(2),
  606. AD777x_CHAN_FILTER_S(3),
  607. AD777x_CHAN_FILTER_S(4),
  608. AD777x_CHAN_FILTER_S(5),
  609. AD777x_CHAN_FILTER_S(6),
  610. AD777x_CHAN_FILTER_S(7),
  611. IIO_CHAN_SOFT_TIMESTAMP(8),
  612. };
  613. static const struct iio_buffer_setup_ops ad7779_buffer_setup_ops = {
  614. .preenable = ad7779_buffer_preenable,
  615. .postdisable = ad7779_buffer_postdisable,
  616. };
  617. static const struct iio_trigger_ops ad7779_trigger_ops = {
  618. .validate_device = iio_trigger_validate_own_device,
  619. };
  620. static int ad7779_conf(struct ad7779_state *st, struct gpio_desc *start_gpio)
  621. {
  622. int ret;
  623. ret = ad7779_spi_write_mask(st, AD7779_REG_GEN_ERR_REG_1_EN,
  624. AD7779_SPI_CRC_EN_MSK,
  625. FIELD_PREP(AD7779_SPI_CRC_EN_MSK, 1));
  626. if (ret)
  627. return ret;
  628. ret = ad7779_spi_write_mask(st, AD7779_REG_GENERAL_USER_CONFIG_1,
  629. AD7779_USRMOD_INIT_MSK,
  630. FIELD_PREP(AD7779_USRMOD_INIT_MSK, 5));
  631. if (ret)
  632. return ret;
  633. ret = ad7779_spi_write_mask(st, AD7779_REG_DOUT_FORMAT,
  634. AD7779_DCLK_CLK_DIV_MSK,
  635. FIELD_PREP(AD7779_DCLK_CLK_DIV_MSK, 1));
  636. if (ret)
  637. return ret;
  638. ret = ad7779_spi_write_mask(st, AD7779_REG_ADC_MUX_CONFIG,
  639. AD7779_REFMUX_CTRL_MSK,
  640. FIELD_PREP(AD7779_REFMUX_CTRL_MSK, 1));
  641. if (ret)
  642. return ret;
  643. ret = ad7779_set_sampling_frequency(st, AD7779_DEFAULT_SAMPLING_FREQ);
  644. if (ret)
  645. return ret;
  646. gpiod_set_value(start_gpio, 0);
  647. /* Start setup time */
  648. fsleep(15);
  649. gpiod_set_value(start_gpio, 1);
  650. /* Start setup time */
  651. fsleep(15);
  652. gpiod_set_value(start_gpio, 0);
  653. /* Start setup time */
  654. fsleep(15);
  655. return 0;
  656. }
  657. static int ad7779_set_data_lines(struct iio_dev *indio_dev, u32 num_lanes)
  658. {
  659. struct ad7779_state *st = iio_priv(indio_dev);
  660. int ret;
  661. if (num_lanes != 1 && num_lanes != 2 && num_lanes != 4)
  662. return -EINVAL;
  663. ret = ad7779_set_sampling_frequency(st, num_lanes * AD7779_DEFAULT_SAMPLING_1LINE);
  664. if (ret)
  665. return ret;
  666. ret = iio_backend_num_lanes_set(st->back, num_lanes);
  667. if (ret)
  668. return ret;
  669. return ad7779_spi_write_mask(st, AD7779_REG_DOUT_FORMAT,
  670. AD7779_DOUT_FORMAT_MSK,
  671. FIELD_PREP(AD7779_DOUT_FORMAT_MSK, 2 - ilog2(num_lanes)));
  672. }
  673. static int ad7779_setup_channels(struct iio_dev *indio_dev, const struct ad7779_state *st)
  674. {
  675. struct iio_chan_spec *channels;
  676. struct device *dev = &st->spi->dev;
  677. channels = devm_kmemdup_array(dev, st->chip_info->channels,
  678. ARRAY_SIZE(ad7779_channels),
  679. sizeof(*channels), GFP_KERNEL);
  680. if (!channels)
  681. return -ENOMEM;
  682. for (unsigned int i = 0; i < ARRAY_SIZE(ad7779_channels); i++)
  683. channels[i].scan_type.endianness = IIO_CPU;
  684. indio_dev->channels = channels;
  685. indio_dev->num_channels = ARRAY_SIZE(ad7779_channels);
  686. return 0;
  687. }
  688. static int ad7779_setup_without_backend(struct ad7779_state *st, struct iio_dev *indio_dev)
  689. {
  690. int ret;
  691. struct device *dev = &st->spi->dev;
  692. indio_dev->info = &ad7779_info;
  693. indio_dev->channels = st->chip_info->channels;
  694. indio_dev->num_channels = ARRAY_SIZE(ad7779_channels);
  695. st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
  696. iio_device_id(indio_dev));
  697. if (!st->trig)
  698. return -ENOMEM;
  699. st->trig->ops = &ad7779_trigger_ops;
  700. iio_trigger_set_drvdata(st->trig, st);
  701. ret = devm_request_irq(dev, st->spi->irq, iio_trigger_generic_data_rdy_poll,
  702. IRQF_NO_THREAD | IRQF_NO_AUTOEN, indio_dev->name,
  703. st->trig);
  704. if (ret)
  705. return dev_err_probe(dev, ret, "request IRQ %d failed\n",
  706. st->spi->irq);
  707. ret = devm_iio_trigger_register(dev, st->trig);
  708. if (ret)
  709. return ret;
  710. indio_dev->trig = iio_trigger_get(st->trig);
  711. init_completion(&st->completion);
  712. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  713. &iio_pollfunc_store_time,
  714. &ad7779_trigger_handler,
  715. &ad7779_buffer_setup_ops);
  716. if (ret)
  717. return ret;
  718. return ad7779_spi_write_mask(st, AD7779_REG_DOUT_FORMAT,
  719. AD7779_DCLK_CLK_DIV_MSK,
  720. FIELD_PREP(AD7779_DCLK_CLK_DIV_MSK, 7));
  721. }
  722. static int ad7779_setup_backend(struct ad7779_state *st, struct iio_dev *indio_dev)
  723. {
  724. struct device *dev = &st->spi->dev;
  725. int ret;
  726. u32 num_lanes;
  727. indio_dev->info = &ad7779_info_data;
  728. ret = ad7779_setup_channels(indio_dev, st);
  729. if (ret)
  730. return ret;
  731. st->back = devm_iio_backend_get(dev, NULL);
  732. if (IS_ERR(st->back))
  733. return dev_err_probe(dev, PTR_ERR(st->back),
  734. "failed to get iio backend");
  735. ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
  736. if (ret)
  737. return ret;
  738. ret = devm_iio_backend_enable(dev, st->back);
  739. if (ret)
  740. return ret;
  741. num_lanes = 4;
  742. ret = device_property_read_u32(dev, "adi,num-lanes", &num_lanes);
  743. if (ret && ret != -EINVAL)
  744. return ret;
  745. return ad7779_set_data_lines(indio_dev, num_lanes);
  746. }
  747. static int ad7779_probe(struct spi_device *spi)
  748. {
  749. struct iio_dev *indio_dev;
  750. struct ad7779_state *st;
  751. struct gpio_desc *reset_gpio, *start_gpio;
  752. struct device *dev = &spi->dev;
  753. int ret = -EINVAL;
  754. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  755. if (!indio_dev)
  756. return -ENOMEM;
  757. st = iio_priv(indio_dev);
  758. ret = devm_regulator_bulk_get_enable(dev,
  759. ARRAY_SIZE(ad7779_power_supplies),
  760. ad7779_power_supplies);
  761. if (ret)
  762. return dev_err_probe(dev, ret,
  763. "failed to get and enable supplies\n");
  764. st->mclk = devm_clk_get_enabled(dev, "mclk");
  765. if (IS_ERR(st->mclk))
  766. return PTR_ERR(st->mclk);
  767. reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  768. if (IS_ERR(reset_gpio))
  769. return PTR_ERR(reset_gpio);
  770. start_gpio = devm_gpiod_get(dev, "start", GPIOD_OUT_HIGH);
  771. if (IS_ERR(start_gpio))
  772. return PTR_ERR(start_gpio);
  773. crc8_populate_msb(ad7779_crc8_table, AD7779_CRC8_POLY);
  774. st->spi = spi;
  775. st->chip_info = spi_get_device_match_data(spi);
  776. if (!st->chip_info)
  777. return -ENODEV;
  778. ret = ad7779_reset(indio_dev, reset_gpio);
  779. if (ret)
  780. return ret;
  781. ret = ad7779_conf(st, start_gpio);
  782. if (ret)
  783. return ret;
  784. indio_dev->name = st->chip_info->name;
  785. indio_dev->modes = INDIO_DIRECT_MODE;
  786. if (device_property_present(dev, "io-backends"))
  787. ret = ad7779_setup_backend(st, indio_dev);
  788. else
  789. ret = ad7779_setup_without_backend(st, indio_dev);
  790. if (ret)
  791. return ret;
  792. return devm_iio_device_register(dev, indio_dev);
  793. }
  794. static int ad7779_suspend(struct device *dev)
  795. {
  796. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  797. struct ad7779_state *st = iio_priv(indio_dev);
  798. return ad7779_spi_write_mask(st, AD7779_REG_GENERAL_USER_CONFIG_1,
  799. AD7779_MOD_POWERMODE_MSK,
  800. FIELD_PREP(AD7779_MOD_POWERMODE_MSK,
  801. AD7779_LOW_POWER));
  802. }
  803. static int ad7779_resume(struct device *dev)
  804. {
  805. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  806. struct ad7779_state *st = iio_priv(indio_dev);
  807. return ad7779_spi_write_mask(st, AD7779_REG_GENERAL_USER_CONFIG_1,
  808. AD7779_MOD_POWERMODE_MSK,
  809. FIELD_PREP(AD7779_MOD_POWERMODE_MSK,
  810. AD7779_HIGH_POWER));
  811. }
  812. static DEFINE_SIMPLE_DEV_PM_OPS(ad7779_pm_ops, ad7779_suspend, ad7779_resume);
  813. static const struct ad7779_chip_info ad7770_chip_info = {
  814. .name = "ad7770",
  815. .channels = ad7779_channels,
  816. };
  817. static const struct ad7779_chip_info ad7771_chip_info = {
  818. .name = "ad7771",
  819. .channels = ad7779_channels_filter,
  820. };
  821. static const struct ad7779_chip_info ad7779_chip_info = {
  822. .name = "ad7779",
  823. .channels = ad7779_channels,
  824. };
  825. static const struct spi_device_id ad7779_id[] = {
  826. {
  827. .name = "ad7770",
  828. .driver_data = (kernel_ulong_t)&ad7770_chip_info,
  829. },
  830. {
  831. .name = "ad7771",
  832. .driver_data = (kernel_ulong_t)&ad7771_chip_info,
  833. },
  834. {
  835. .name = "ad7779",
  836. .driver_data = (kernel_ulong_t)&ad7779_chip_info,
  837. },
  838. { }
  839. };
  840. MODULE_DEVICE_TABLE(spi, ad7779_id);
  841. static const struct of_device_id ad7779_of_table[] = {
  842. {
  843. .compatible = "adi,ad7770",
  844. .data = &ad7770_chip_info,
  845. },
  846. {
  847. .compatible = "adi,ad7771",
  848. .data = &ad7771_chip_info,
  849. },
  850. {
  851. .compatible = "adi,ad7779",
  852. .data = &ad7779_chip_info,
  853. },
  854. { }
  855. };
  856. MODULE_DEVICE_TABLE(of, ad7779_of_table);
  857. static struct spi_driver ad7779_driver = {
  858. .driver = {
  859. .name = "ad7779",
  860. .pm = pm_sleep_ptr(&ad7779_pm_ops),
  861. .of_match_table = ad7779_of_table,
  862. },
  863. .probe = ad7779_probe,
  864. .id_table = ad7779_id,
  865. };
  866. module_spi_driver(ad7779_driver);
  867. MODULE_AUTHOR("Ramona Alexandra Nechita <ramona.nechita@analog.com>");
  868. MODULE_DESCRIPTION("Analog Devices AD7779 ADC");
  869. MODULE_LICENSE("GPL");
  870. MODULE_IMPORT_NS("IIO_BACKEND");