ad7768-1.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Analog Devices AD7768-1 SPI ADC driver
  4. *
  5. * Copyright 2017 Analog Devices Inc.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/iio/buffer.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/trigger.h>
  22. #include <linux/iio/triggered_buffer.h>
  23. #include <linux/iio/trigger_consumer.h>
  24. /* AD7768 registers definition */
  25. #define AD7768_REG_CHIP_TYPE 0x3
  26. #define AD7768_REG_PROD_ID_L 0x4
  27. #define AD7768_REG_PROD_ID_H 0x5
  28. #define AD7768_REG_CHIP_GRADE 0x6
  29. #define AD7768_REG_SCRATCH_PAD 0x0A
  30. #define AD7768_REG_VENDOR_L 0x0C
  31. #define AD7768_REG_VENDOR_H 0x0D
  32. #define AD7768_REG_INTERFACE_FORMAT 0x14
  33. #define AD7768_REG_POWER_CLOCK 0x15
  34. #define AD7768_REG_ANALOG 0x16
  35. #define AD7768_REG_ANALOG2 0x17
  36. #define AD7768_REG_CONVERSION 0x18
  37. #define AD7768_REG_DIGITAL_FILTER 0x19
  38. #define AD7768_REG_SINC3_DEC_RATE_MSB 0x1A
  39. #define AD7768_REG_SINC3_DEC_RATE_LSB 0x1B
  40. #define AD7768_REG_DUTY_CYCLE_RATIO 0x1C
  41. #define AD7768_REG_SYNC_RESET 0x1D
  42. #define AD7768_REG_GPIO_CONTROL 0x1E
  43. #define AD7768_REG_GPIO_WRITE 0x1F
  44. #define AD7768_REG_GPIO_READ 0x20
  45. #define AD7768_REG_OFFSET_HI 0x21
  46. #define AD7768_REG_OFFSET_MID 0x22
  47. #define AD7768_REG_OFFSET_LO 0x23
  48. #define AD7768_REG_GAIN_HI 0x24
  49. #define AD7768_REG_GAIN_MID 0x25
  50. #define AD7768_REG_GAIN_LO 0x26
  51. #define AD7768_REG_SPI_DIAG_ENABLE 0x28
  52. #define AD7768_REG_ADC_DIAG_ENABLE 0x29
  53. #define AD7768_REG_DIG_DIAG_ENABLE 0x2A
  54. #define AD7768_REG_ADC_DATA 0x2C
  55. #define AD7768_REG_MASTER_STATUS 0x2D
  56. #define AD7768_REG_SPI_DIAG_STATUS 0x2E
  57. #define AD7768_REG_ADC_DIAG_STATUS 0x2F
  58. #define AD7768_REG_DIG_DIAG_STATUS 0x30
  59. #define AD7768_REG_MCLK_COUNTER 0x31
  60. /* AD7768_REG_POWER_CLOCK */
  61. #define AD7768_PWR_MCLK_DIV_MSK GENMASK(5, 4)
  62. #define AD7768_PWR_MCLK_DIV(x) FIELD_PREP(AD7768_PWR_MCLK_DIV_MSK, x)
  63. #define AD7768_PWR_PWRMODE_MSK GENMASK(1, 0)
  64. #define AD7768_PWR_PWRMODE(x) FIELD_PREP(AD7768_PWR_PWRMODE_MSK, x)
  65. /* AD7768_REG_DIGITAL_FILTER */
  66. #define AD7768_DIG_FIL_FIL_MSK GENMASK(6, 4)
  67. #define AD7768_DIG_FIL_FIL(x) FIELD_PREP(AD7768_DIG_FIL_FIL_MSK, x)
  68. #define AD7768_DIG_FIL_DEC_MSK GENMASK(2, 0)
  69. #define AD7768_DIG_FIL_DEC_RATE(x) FIELD_PREP(AD7768_DIG_FIL_DEC_MSK, x)
  70. /* AD7768_REG_CONVERSION */
  71. #define AD7768_CONV_MODE_MSK GENMASK(2, 0)
  72. #define AD7768_CONV_MODE(x) FIELD_PREP(AD7768_CONV_MODE_MSK, x)
  73. #define AD7768_RD_FLAG_MSK(x) (BIT(6) | ((x) & 0x3F))
  74. #define AD7768_WR_FLAG_MSK(x) ((x) & 0x3F)
  75. enum ad7768_conv_mode {
  76. AD7768_CONTINUOUS,
  77. AD7768_ONE_SHOT,
  78. AD7768_SINGLE,
  79. AD7768_PERIODIC,
  80. AD7768_STANDBY
  81. };
  82. enum ad7768_pwrmode {
  83. AD7768_ECO_MODE = 0,
  84. AD7768_MED_MODE = 2,
  85. AD7768_FAST_MODE = 3
  86. };
  87. enum ad7768_mclk_div {
  88. AD7768_MCLK_DIV_16,
  89. AD7768_MCLK_DIV_8,
  90. AD7768_MCLK_DIV_4,
  91. AD7768_MCLK_DIV_2
  92. };
  93. enum ad7768_dec_rate {
  94. AD7768_DEC_RATE_32 = 0,
  95. AD7768_DEC_RATE_64 = 1,
  96. AD7768_DEC_RATE_128 = 2,
  97. AD7768_DEC_RATE_256 = 3,
  98. AD7768_DEC_RATE_512 = 4,
  99. AD7768_DEC_RATE_1024 = 5,
  100. AD7768_DEC_RATE_8 = 9,
  101. AD7768_DEC_RATE_16 = 10
  102. };
  103. struct ad7768_clk_configuration {
  104. enum ad7768_mclk_div mclk_div;
  105. enum ad7768_dec_rate dec_rate;
  106. unsigned int clk_div;
  107. enum ad7768_pwrmode pwrmode;
  108. };
  109. static const struct ad7768_clk_configuration ad7768_clk_config[] = {
  110. { AD7768_MCLK_DIV_2, AD7768_DEC_RATE_8, 16, AD7768_FAST_MODE },
  111. { AD7768_MCLK_DIV_2, AD7768_DEC_RATE_16, 32, AD7768_FAST_MODE },
  112. { AD7768_MCLK_DIV_2, AD7768_DEC_RATE_32, 64, AD7768_FAST_MODE },
  113. { AD7768_MCLK_DIV_2, AD7768_DEC_RATE_64, 128, AD7768_FAST_MODE },
  114. { AD7768_MCLK_DIV_2, AD7768_DEC_RATE_128, 256, AD7768_FAST_MODE },
  115. { AD7768_MCLK_DIV_4, AD7768_DEC_RATE_128, 512, AD7768_MED_MODE },
  116. { AD7768_MCLK_DIV_4, AD7768_DEC_RATE_256, 1024, AD7768_MED_MODE },
  117. { AD7768_MCLK_DIV_4, AD7768_DEC_RATE_512, 2048, AD7768_MED_MODE },
  118. { AD7768_MCLK_DIV_4, AD7768_DEC_RATE_1024, 4096, AD7768_MED_MODE },
  119. { AD7768_MCLK_DIV_8, AD7768_DEC_RATE_1024, 8192, AD7768_MED_MODE },
  120. { AD7768_MCLK_DIV_16, AD7768_DEC_RATE_1024, 16384, AD7768_ECO_MODE },
  121. };
  122. static const struct iio_chan_spec ad7768_channels[] = {
  123. {
  124. .type = IIO_VOLTAGE,
  125. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  126. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  127. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  128. .indexed = 1,
  129. .channel = 0,
  130. .scan_index = 0,
  131. .scan_type = {
  132. .sign = 'u',
  133. .realbits = 24,
  134. .storagebits = 32,
  135. .shift = 8,
  136. .endianness = IIO_BE,
  137. },
  138. },
  139. };
  140. struct ad7768_state {
  141. struct spi_device *spi;
  142. struct regulator *vref;
  143. struct mutex lock;
  144. struct clk *mclk;
  145. unsigned int mclk_freq;
  146. unsigned int samp_freq;
  147. struct completion completion;
  148. struct iio_trigger *trig;
  149. struct gpio_desc *gpio_sync_in;
  150. const char *labels[ARRAY_SIZE(ad7768_channels)];
  151. /*
  152. * DMA (thus cache coherency maintenance) may require the
  153. * transfer buffers to live in their own cache lines.
  154. */
  155. union {
  156. struct {
  157. __be32 chan;
  158. s64 timestamp;
  159. } scan;
  160. __be32 d32;
  161. u8 d8[2];
  162. } data __aligned(IIO_DMA_MINALIGN);
  163. };
  164. static int ad7768_spi_reg_read(struct ad7768_state *st, unsigned int addr,
  165. unsigned int len)
  166. {
  167. unsigned int shift;
  168. int ret;
  169. shift = 32 - (8 * len);
  170. st->data.d8[0] = AD7768_RD_FLAG_MSK(addr);
  171. ret = spi_write_then_read(st->spi, st->data.d8, 1,
  172. &st->data.d32, len);
  173. if (ret < 0)
  174. return ret;
  175. return (be32_to_cpu(st->data.d32) >> shift);
  176. }
  177. static int ad7768_spi_reg_write(struct ad7768_state *st,
  178. unsigned int addr,
  179. unsigned int val)
  180. {
  181. st->data.d8[0] = AD7768_WR_FLAG_MSK(addr);
  182. st->data.d8[1] = val & 0xFF;
  183. return spi_write(st->spi, st->data.d8, 2);
  184. }
  185. static int ad7768_set_mode(struct ad7768_state *st,
  186. enum ad7768_conv_mode mode)
  187. {
  188. int regval;
  189. regval = ad7768_spi_reg_read(st, AD7768_REG_CONVERSION, 1);
  190. if (regval < 0)
  191. return regval;
  192. regval &= ~AD7768_CONV_MODE_MSK;
  193. regval |= AD7768_CONV_MODE(mode);
  194. return ad7768_spi_reg_write(st, AD7768_REG_CONVERSION, regval);
  195. }
  196. static int ad7768_scan_direct(struct iio_dev *indio_dev)
  197. {
  198. struct ad7768_state *st = iio_priv(indio_dev);
  199. int readval, ret;
  200. reinit_completion(&st->completion);
  201. ret = ad7768_set_mode(st, AD7768_ONE_SHOT);
  202. if (ret < 0)
  203. return ret;
  204. ret = wait_for_completion_timeout(&st->completion,
  205. msecs_to_jiffies(1000));
  206. if (!ret)
  207. return -ETIMEDOUT;
  208. readval = ad7768_spi_reg_read(st, AD7768_REG_ADC_DATA, 3);
  209. if (readval < 0)
  210. return readval;
  211. /*
  212. * Any SPI configuration of the AD7768-1 can only be
  213. * performed in continuous conversion mode.
  214. */
  215. ret = ad7768_set_mode(st, AD7768_CONTINUOUS);
  216. if (ret < 0)
  217. return ret;
  218. return readval;
  219. }
  220. static int ad7768_reg_access(struct iio_dev *indio_dev,
  221. unsigned int reg,
  222. unsigned int writeval,
  223. unsigned int *readval)
  224. {
  225. struct ad7768_state *st = iio_priv(indio_dev);
  226. int ret;
  227. mutex_lock(&st->lock);
  228. if (readval) {
  229. ret = ad7768_spi_reg_read(st, reg, 1);
  230. if (ret < 0)
  231. goto err_unlock;
  232. *readval = ret;
  233. ret = 0;
  234. } else {
  235. ret = ad7768_spi_reg_write(st, reg, writeval);
  236. }
  237. err_unlock:
  238. mutex_unlock(&st->lock);
  239. return ret;
  240. }
  241. static int ad7768_set_dig_fil(struct ad7768_state *st,
  242. enum ad7768_dec_rate dec_rate)
  243. {
  244. unsigned int mode;
  245. int ret;
  246. if (dec_rate == AD7768_DEC_RATE_8 || dec_rate == AD7768_DEC_RATE_16)
  247. mode = AD7768_DIG_FIL_FIL(dec_rate);
  248. else
  249. mode = AD7768_DIG_FIL_DEC_RATE(dec_rate);
  250. ret = ad7768_spi_reg_write(st, AD7768_REG_DIGITAL_FILTER, mode);
  251. if (ret < 0)
  252. return ret;
  253. /* A sync-in pulse is required every time the filter dec rate changes */
  254. gpiod_set_value(st->gpio_sync_in, 1);
  255. gpiod_set_value(st->gpio_sync_in, 0);
  256. return 0;
  257. }
  258. static int ad7768_set_freq(struct ad7768_state *st,
  259. unsigned int freq)
  260. {
  261. unsigned int diff_new, diff_old, pwr_mode, i, idx;
  262. int res, ret;
  263. diff_old = U32_MAX;
  264. idx = 0;
  265. res = DIV_ROUND_CLOSEST(st->mclk_freq, freq);
  266. /* Find the closest match for the desired sampling frequency */
  267. for (i = 0; i < ARRAY_SIZE(ad7768_clk_config); i++) {
  268. diff_new = abs(res - ad7768_clk_config[i].clk_div);
  269. if (diff_new < diff_old) {
  270. diff_old = diff_new;
  271. idx = i;
  272. }
  273. }
  274. /*
  275. * Set both the mclk_div and pwrmode with a single write to the
  276. * POWER_CLOCK register
  277. */
  278. pwr_mode = AD7768_PWR_MCLK_DIV(ad7768_clk_config[idx].mclk_div) |
  279. AD7768_PWR_PWRMODE(ad7768_clk_config[idx].pwrmode);
  280. ret = ad7768_spi_reg_write(st, AD7768_REG_POWER_CLOCK, pwr_mode);
  281. if (ret < 0)
  282. return ret;
  283. ret = ad7768_set_dig_fil(st, ad7768_clk_config[idx].dec_rate);
  284. if (ret < 0)
  285. return ret;
  286. st->samp_freq = DIV_ROUND_CLOSEST(st->mclk_freq,
  287. ad7768_clk_config[idx].clk_div);
  288. return 0;
  289. }
  290. static ssize_t ad7768_sampling_freq_avail(struct device *dev,
  291. struct device_attribute *attr,
  292. char *buf)
  293. {
  294. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  295. struct ad7768_state *st = iio_priv(indio_dev);
  296. unsigned int freq;
  297. int i, len = 0;
  298. for (i = 0; i < ARRAY_SIZE(ad7768_clk_config); i++) {
  299. freq = DIV_ROUND_CLOSEST(st->mclk_freq,
  300. ad7768_clk_config[i].clk_div);
  301. len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", freq);
  302. }
  303. buf[len - 1] = '\n';
  304. return len;
  305. }
  306. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(ad7768_sampling_freq_avail);
  307. static int ad7768_read_raw(struct iio_dev *indio_dev,
  308. struct iio_chan_spec const *chan,
  309. int *val, int *val2, long info)
  310. {
  311. struct ad7768_state *st = iio_priv(indio_dev);
  312. int scale_uv, ret;
  313. switch (info) {
  314. case IIO_CHAN_INFO_RAW:
  315. ret = iio_device_claim_direct_mode(indio_dev);
  316. if (ret)
  317. return ret;
  318. ret = ad7768_scan_direct(indio_dev);
  319. if (ret >= 0)
  320. *val = ret;
  321. iio_device_release_direct_mode(indio_dev);
  322. if (ret < 0)
  323. return ret;
  324. return IIO_VAL_INT;
  325. case IIO_CHAN_INFO_SCALE:
  326. scale_uv = regulator_get_voltage(st->vref);
  327. if (scale_uv < 0)
  328. return scale_uv;
  329. *val = (scale_uv * 2) / 1000;
  330. *val2 = chan->scan_type.realbits;
  331. return IIO_VAL_FRACTIONAL_LOG2;
  332. case IIO_CHAN_INFO_SAMP_FREQ:
  333. *val = st->samp_freq;
  334. return IIO_VAL_INT;
  335. }
  336. return -EINVAL;
  337. }
  338. static int ad7768_write_raw(struct iio_dev *indio_dev,
  339. struct iio_chan_spec const *chan,
  340. int val, int val2, long info)
  341. {
  342. struct ad7768_state *st = iio_priv(indio_dev);
  343. switch (info) {
  344. case IIO_CHAN_INFO_SAMP_FREQ:
  345. return ad7768_set_freq(st, val);
  346. default:
  347. return -EINVAL;
  348. }
  349. }
  350. static int ad7768_read_label(struct iio_dev *indio_dev,
  351. const struct iio_chan_spec *chan, char *label)
  352. {
  353. struct ad7768_state *st = iio_priv(indio_dev);
  354. return sprintf(label, "%s\n", st->labels[chan->channel]);
  355. }
  356. static struct attribute *ad7768_attributes[] = {
  357. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  358. NULL
  359. };
  360. static const struct attribute_group ad7768_group = {
  361. .attrs = ad7768_attributes,
  362. };
  363. static const struct iio_info ad7768_info = {
  364. .attrs = &ad7768_group,
  365. .read_raw = &ad7768_read_raw,
  366. .write_raw = &ad7768_write_raw,
  367. .read_label = ad7768_read_label,
  368. .debugfs_reg_access = &ad7768_reg_access,
  369. };
  370. static int ad7768_setup(struct ad7768_state *st)
  371. {
  372. int ret;
  373. /*
  374. * Two writes to the SPI_RESET[1:0] bits are required to initiate
  375. * a software reset. The bits must first be set to 11, and then
  376. * to 10. When the sequence is detected, the reset occurs.
  377. * See the datasheet, page 70.
  378. */
  379. ret = ad7768_spi_reg_write(st, AD7768_REG_SYNC_RESET, 0x3);
  380. if (ret)
  381. return ret;
  382. ret = ad7768_spi_reg_write(st, AD7768_REG_SYNC_RESET, 0x2);
  383. if (ret)
  384. return ret;
  385. st->gpio_sync_in = devm_gpiod_get(&st->spi->dev, "adi,sync-in",
  386. GPIOD_OUT_LOW);
  387. if (IS_ERR(st->gpio_sync_in))
  388. return PTR_ERR(st->gpio_sync_in);
  389. /* Set the default sampling frequency to 32000 kSPS */
  390. return ad7768_set_freq(st, 32000);
  391. }
  392. static irqreturn_t ad7768_trigger_handler(int irq, void *p)
  393. {
  394. struct iio_poll_func *pf = p;
  395. struct iio_dev *indio_dev = pf->indio_dev;
  396. struct ad7768_state *st = iio_priv(indio_dev);
  397. int ret;
  398. mutex_lock(&st->lock);
  399. ret = spi_read(st->spi, &st->data.scan.chan, 3);
  400. if (ret < 0)
  401. goto err_unlock;
  402. iio_push_to_buffers_with_timestamp(indio_dev, &st->data.scan,
  403. iio_get_time_ns(indio_dev));
  404. err_unlock:
  405. iio_trigger_notify_done(indio_dev->trig);
  406. mutex_unlock(&st->lock);
  407. return IRQ_HANDLED;
  408. }
  409. static irqreturn_t ad7768_interrupt(int irq, void *dev_id)
  410. {
  411. struct iio_dev *indio_dev = dev_id;
  412. struct ad7768_state *st = iio_priv(indio_dev);
  413. if (iio_buffer_enabled(indio_dev))
  414. iio_trigger_poll(st->trig);
  415. else
  416. complete(&st->completion);
  417. return IRQ_HANDLED;
  418. };
  419. static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
  420. {
  421. struct ad7768_state *st = iio_priv(indio_dev);
  422. /*
  423. * Write a 1 to the LSB of the INTERFACE_FORMAT register to enter
  424. * continuous read mode. Subsequent data reads do not require an
  425. * initial 8-bit write to query the ADC_DATA register.
  426. */
  427. return ad7768_spi_reg_write(st, AD7768_REG_INTERFACE_FORMAT, 0x01);
  428. }
  429. static int ad7768_buffer_predisable(struct iio_dev *indio_dev)
  430. {
  431. struct ad7768_state *st = iio_priv(indio_dev);
  432. /*
  433. * To exit continuous read mode, perform a single read of the ADC_DATA
  434. * reg (0x2C), which allows further configuration of the device.
  435. */
  436. return ad7768_spi_reg_read(st, AD7768_REG_ADC_DATA, 3);
  437. }
  438. static const struct iio_buffer_setup_ops ad7768_buffer_ops = {
  439. .postenable = &ad7768_buffer_postenable,
  440. .predisable = &ad7768_buffer_predisable,
  441. };
  442. static const struct iio_trigger_ops ad7768_trigger_ops = {
  443. .validate_device = iio_trigger_validate_own_device,
  444. };
  445. static void ad7768_regulator_disable(void *data)
  446. {
  447. struct ad7768_state *st = data;
  448. regulator_disable(st->vref);
  449. }
  450. static int ad7768_set_channel_label(struct iio_dev *indio_dev,
  451. int num_channels)
  452. {
  453. struct ad7768_state *st = iio_priv(indio_dev);
  454. struct device *device = indio_dev->dev.parent;
  455. struct fwnode_handle *fwnode;
  456. struct fwnode_handle *child;
  457. const char *label;
  458. int crt_ch = 0;
  459. fwnode = dev_fwnode(device);
  460. fwnode_for_each_child_node(fwnode, child) {
  461. if (fwnode_property_read_u32(child, "reg", &crt_ch))
  462. continue;
  463. if (crt_ch >= num_channels)
  464. continue;
  465. if (fwnode_property_read_string(child, "label", &label))
  466. continue;
  467. st->labels[crt_ch] = label;
  468. }
  469. return 0;
  470. }
  471. static int ad7768_probe(struct spi_device *spi)
  472. {
  473. struct ad7768_state *st;
  474. struct iio_dev *indio_dev;
  475. int ret;
  476. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  477. if (!indio_dev)
  478. return -ENOMEM;
  479. st = iio_priv(indio_dev);
  480. st->spi = spi;
  481. st->vref = devm_regulator_get(&spi->dev, "vref");
  482. if (IS_ERR(st->vref))
  483. return PTR_ERR(st->vref);
  484. ret = regulator_enable(st->vref);
  485. if (ret) {
  486. dev_err(&spi->dev, "Failed to enable specified vref supply\n");
  487. return ret;
  488. }
  489. ret = devm_add_action_or_reset(&spi->dev, ad7768_regulator_disable, st);
  490. if (ret)
  491. return ret;
  492. st->mclk = devm_clk_get_enabled(&spi->dev, "mclk");
  493. if (IS_ERR(st->mclk))
  494. return PTR_ERR(st->mclk);
  495. st->mclk_freq = clk_get_rate(st->mclk);
  496. mutex_init(&st->lock);
  497. indio_dev->channels = ad7768_channels;
  498. indio_dev->num_channels = ARRAY_SIZE(ad7768_channels);
  499. indio_dev->name = spi_get_device_id(spi)->name;
  500. indio_dev->info = &ad7768_info;
  501. indio_dev->modes = INDIO_DIRECT_MODE;
  502. ret = ad7768_setup(st);
  503. if (ret < 0) {
  504. dev_err(&spi->dev, "AD7768 setup failed\n");
  505. return ret;
  506. }
  507. st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
  508. indio_dev->name,
  509. iio_device_id(indio_dev));
  510. if (!st->trig)
  511. return -ENOMEM;
  512. st->trig->ops = &ad7768_trigger_ops;
  513. iio_trigger_set_drvdata(st->trig, indio_dev);
  514. ret = devm_iio_trigger_register(&spi->dev, st->trig);
  515. if (ret)
  516. return ret;
  517. indio_dev->trig = iio_trigger_get(st->trig);
  518. init_completion(&st->completion);
  519. ret = ad7768_set_channel_label(indio_dev, ARRAY_SIZE(ad7768_channels));
  520. if (ret)
  521. return ret;
  522. ret = devm_request_irq(&spi->dev, spi->irq,
  523. &ad7768_interrupt,
  524. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  525. indio_dev->name, indio_dev);
  526. if (ret)
  527. return ret;
  528. ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
  529. &iio_pollfunc_store_time,
  530. &ad7768_trigger_handler,
  531. &ad7768_buffer_ops);
  532. if (ret)
  533. return ret;
  534. return devm_iio_device_register(&spi->dev, indio_dev);
  535. }
  536. static const struct spi_device_id ad7768_id_table[] = {
  537. { "ad7768-1", 0 },
  538. {}
  539. };
  540. MODULE_DEVICE_TABLE(spi, ad7768_id_table);
  541. static const struct of_device_id ad7768_of_match[] = {
  542. { .compatible = "adi,ad7768-1" },
  543. { },
  544. };
  545. MODULE_DEVICE_TABLE(of, ad7768_of_match);
  546. static struct spi_driver ad7768_driver = {
  547. .driver = {
  548. .name = "ad7768-1",
  549. .of_match_table = ad7768_of_match,
  550. },
  551. .probe = ad7768_probe,
  552. .id_table = ad7768_id_table,
  553. };
  554. module_spi_driver(ad7768_driver);
  555. MODULE_AUTHOR("Stefan Popa <[email protected]>");
  556. MODULE_DESCRIPTION("Analog Devices AD7768-1 ADC driver");
  557. MODULE_LICENSE("GPL v2");