ad7606.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AD7606 SPI ADC driver
  4. *
  5. * Copyright 2011 Analog Devices Inc.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/property.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/util_macros.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/trigger.h>
  24. #include <linux/iio/triggered_buffer.h>
  25. #include <linux/iio/trigger_consumer.h>
  26. #include "ad7606.h"
  27. /*
  28. * Scales are computed as 5000/32768 and 10000/32768 respectively,
  29. * so that when applied to the raw values they provide mV values
  30. */
  31. static const unsigned int ad7606_scale_avail[2] = {
  32. 152588, 305176
  33. };
  34. static const unsigned int ad7616_sw_scale_avail[3] = {
  35. 76293, 152588, 305176
  36. };
  37. static const unsigned int ad7606_oversampling_avail[7] = {
  38. 1, 2, 4, 8, 16, 32, 64,
  39. };
  40. static const unsigned int ad7616_oversampling_avail[8] = {
  41. 1, 2, 4, 8, 16, 32, 64, 128,
  42. };
  43. static int ad7606_reset(struct ad7606_state *st)
  44. {
  45. if (st->gpio_reset) {
  46. gpiod_set_value(st->gpio_reset, 1);
  47. ndelay(100); /* t_reset >= 100ns */
  48. gpiod_set_value(st->gpio_reset, 0);
  49. return 0;
  50. }
  51. return -ENODEV;
  52. }
  53. static int ad7606_reg_access(struct iio_dev *indio_dev,
  54. unsigned int reg,
  55. unsigned int writeval,
  56. unsigned int *readval)
  57. {
  58. struct ad7606_state *st = iio_priv(indio_dev);
  59. int ret;
  60. mutex_lock(&st->lock);
  61. if (readval) {
  62. ret = st->bops->reg_read(st, reg);
  63. if (ret < 0)
  64. goto err_unlock;
  65. *readval = ret;
  66. ret = 0;
  67. } else {
  68. ret = st->bops->reg_write(st, reg, writeval);
  69. }
  70. err_unlock:
  71. mutex_unlock(&st->lock);
  72. return ret;
  73. }
  74. static int ad7606_read_samples(struct ad7606_state *st)
  75. {
  76. unsigned int num = st->chip_info->num_channels - 1;
  77. u16 *data = st->data;
  78. int ret;
  79. /*
  80. * The frstdata signal is set to high while and after reading the sample
  81. * of the first channel and low for all other channels. This can be used
  82. * to check that the incoming data is correctly aligned. During normal
  83. * operation the data should never become unaligned, but some glitch or
  84. * electrostatic discharge might cause an extra read or clock cycle.
  85. * Monitoring the frstdata signal allows to recover from such failure
  86. * situations.
  87. */
  88. if (st->gpio_frstdata) {
  89. ret = st->bops->read_block(st->dev, 1, data);
  90. if (ret)
  91. return ret;
  92. if (!gpiod_get_value(st->gpio_frstdata)) {
  93. ad7606_reset(st);
  94. return -EIO;
  95. }
  96. data++;
  97. num--;
  98. }
  99. return st->bops->read_block(st->dev, num, data);
  100. }
  101. static irqreturn_t ad7606_trigger_handler(int irq, void *p)
  102. {
  103. struct iio_poll_func *pf = p;
  104. struct iio_dev *indio_dev = pf->indio_dev;
  105. struct ad7606_state *st = iio_priv(indio_dev);
  106. int ret;
  107. mutex_lock(&st->lock);
  108. ret = ad7606_read_samples(st);
  109. if (ret == 0)
  110. iio_push_to_buffers_with_timestamp(indio_dev, st->data,
  111. iio_get_time_ns(indio_dev));
  112. iio_trigger_notify_done(indio_dev->trig);
  113. /* The rising edge of the CONVST signal starts a new conversion. */
  114. gpiod_set_value(st->gpio_convst, 1);
  115. mutex_unlock(&st->lock);
  116. return IRQ_HANDLED;
  117. }
  118. static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
  119. {
  120. struct ad7606_state *st = iio_priv(indio_dev);
  121. int ret;
  122. gpiod_set_value(st->gpio_convst, 1);
  123. ret = wait_for_completion_timeout(&st->completion,
  124. msecs_to_jiffies(1000));
  125. if (!ret) {
  126. ret = -ETIMEDOUT;
  127. goto error_ret;
  128. }
  129. ret = ad7606_read_samples(st);
  130. if (ret == 0)
  131. ret = st->data[ch];
  132. error_ret:
  133. gpiod_set_value(st->gpio_convst, 0);
  134. return ret;
  135. }
  136. static int ad7606_read_raw(struct iio_dev *indio_dev,
  137. struct iio_chan_spec const *chan,
  138. int *val,
  139. int *val2,
  140. long m)
  141. {
  142. int ret, ch = 0;
  143. struct ad7606_state *st = iio_priv(indio_dev);
  144. switch (m) {
  145. case IIO_CHAN_INFO_RAW:
  146. ret = iio_device_claim_direct_mode(indio_dev);
  147. if (ret)
  148. return ret;
  149. ret = ad7606_scan_direct(indio_dev, chan->address);
  150. iio_device_release_direct_mode(indio_dev);
  151. if (ret < 0)
  152. return ret;
  153. *val = (short)ret;
  154. return IIO_VAL_INT;
  155. case IIO_CHAN_INFO_SCALE:
  156. if (st->sw_mode_en)
  157. ch = chan->address;
  158. *val = 0;
  159. *val2 = st->scale_avail[st->range[ch]];
  160. return IIO_VAL_INT_PLUS_MICRO;
  161. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  162. *val = st->oversampling;
  163. return IIO_VAL_INT;
  164. }
  165. return -EINVAL;
  166. }
  167. static ssize_t ad7606_show_avail(char *buf, const unsigned int *vals,
  168. unsigned int n, bool micros)
  169. {
  170. size_t len = 0;
  171. int i;
  172. for (i = 0; i < n; i++) {
  173. len += scnprintf(buf + len, PAGE_SIZE - len,
  174. micros ? "0.%06u " : "%u ", vals[i]);
  175. }
  176. buf[len - 1] = '\n';
  177. return len;
  178. }
  179. static ssize_t in_voltage_scale_available_show(struct device *dev,
  180. struct device_attribute *attr,
  181. char *buf)
  182. {
  183. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  184. struct ad7606_state *st = iio_priv(indio_dev);
  185. return ad7606_show_avail(buf, st->scale_avail, st->num_scales, true);
  186. }
  187. static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
  188. static int ad7606_write_scale_hw(struct iio_dev *indio_dev, int ch, int val)
  189. {
  190. struct ad7606_state *st = iio_priv(indio_dev);
  191. gpiod_set_value(st->gpio_range, val);
  192. return 0;
  193. }
  194. static int ad7606_write_os_hw(struct iio_dev *indio_dev, int val)
  195. {
  196. struct ad7606_state *st = iio_priv(indio_dev);
  197. DECLARE_BITMAP(values, 3);
  198. values[0] = val;
  199. gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
  200. st->gpio_os->info, values);
  201. /* AD7616 requires a reset to update value */
  202. if (st->chip_info->os_req_reset)
  203. ad7606_reset(st);
  204. return 0;
  205. }
  206. static int ad7606_write_raw(struct iio_dev *indio_dev,
  207. struct iio_chan_spec const *chan,
  208. int val,
  209. int val2,
  210. long mask)
  211. {
  212. struct ad7606_state *st = iio_priv(indio_dev);
  213. int i, ret, ch = 0;
  214. switch (mask) {
  215. case IIO_CHAN_INFO_SCALE:
  216. mutex_lock(&st->lock);
  217. i = find_closest(val2, st->scale_avail, st->num_scales);
  218. if (st->sw_mode_en)
  219. ch = chan->address;
  220. ret = st->write_scale(indio_dev, ch, i);
  221. if (ret < 0) {
  222. mutex_unlock(&st->lock);
  223. return ret;
  224. }
  225. st->range[ch] = i;
  226. mutex_unlock(&st->lock);
  227. return 0;
  228. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  229. if (val2)
  230. return -EINVAL;
  231. i = find_closest(val, st->oversampling_avail,
  232. st->num_os_ratios);
  233. mutex_lock(&st->lock);
  234. ret = st->write_os(indio_dev, i);
  235. if (ret < 0) {
  236. mutex_unlock(&st->lock);
  237. return ret;
  238. }
  239. st->oversampling = st->oversampling_avail[i];
  240. mutex_unlock(&st->lock);
  241. return 0;
  242. default:
  243. return -EINVAL;
  244. }
  245. }
  246. static ssize_t ad7606_oversampling_ratio_avail(struct device *dev,
  247. struct device_attribute *attr,
  248. char *buf)
  249. {
  250. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  251. struct ad7606_state *st = iio_priv(indio_dev);
  252. return ad7606_show_avail(buf, st->oversampling_avail,
  253. st->num_os_ratios, false);
  254. }
  255. static IIO_DEVICE_ATTR(oversampling_ratio_available, 0444,
  256. ad7606_oversampling_ratio_avail, NULL, 0);
  257. static struct attribute *ad7606_attributes_os_and_range[] = {
  258. &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
  259. &iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
  260. NULL,
  261. };
  262. static const struct attribute_group ad7606_attribute_group_os_and_range = {
  263. .attrs = ad7606_attributes_os_and_range,
  264. };
  265. static struct attribute *ad7606_attributes_os[] = {
  266. &iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
  267. NULL,
  268. };
  269. static const struct attribute_group ad7606_attribute_group_os = {
  270. .attrs = ad7606_attributes_os,
  271. };
  272. static struct attribute *ad7606_attributes_range[] = {
  273. &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
  274. NULL,
  275. };
  276. static const struct attribute_group ad7606_attribute_group_range = {
  277. .attrs = ad7606_attributes_range,
  278. };
  279. static const struct iio_chan_spec ad7605_channels[] = {
  280. IIO_CHAN_SOFT_TIMESTAMP(4),
  281. AD7605_CHANNEL(0),
  282. AD7605_CHANNEL(1),
  283. AD7605_CHANNEL(2),
  284. AD7605_CHANNEL(3),
  285. };
  286. static const struct iio_chan_spec ad7606_channels[] = {
  287. IIO_CHAN_SOFT_TIMESTAMP(8),
  288. AD7606_CHANNEL(0),
  289. AD7606_CHANNEL(1),
  290. AD7606_CHANNEL(2),
  291. AD7606_CHANNEL(3),
  292. AD7606_CHANNEL(4),
  293. AD7606_CHANNEL(5),
  294. AD7606_CHANNEL(6),
  295. AD7606_CHANNEL(7),
  296. };
  297. /*
  298. * The current assumption that this driver makes for AD7616, is that it's
  299. * working in Hardware Mode with Serial, Burst and Sequencer modes activated.
  300. * To activate them, following pins must be pulled high:
  301. * -SER/PAR
  302. * -SEQEN
  303. * And following pins must be pulled low:
  304. * -WR/BURST
  305. * -DB4/SER1W
  306. */
  307. static const struct iio_chan_spec ad7616_channels[] = {
  308. IIO_CHAN_SOFT_TIMESTAMP(16),
  309. AD7606_CHANNEL(0),
  310. AD7606_CHANNEL(1),
  311. AD7606_CHANNEL(2),
  312. AD7606_CHANNEL(3),
  313. AD7606_CHANNEL(4),
  314. AD7606_CHANNEL(5),
  315. AD7606_CHANNEL(6),
  316. AD7606_CHANNEL(7),
  317. AD7606_CHANNEL(8),
  318. AD7606_CHANNEL(9),
  319. AD7606_CHANNEL(10),
  320. AD7606_CHANNEL(11),
  321. AD7606_CHANNEL(12),
  322. AD7606_CHANNEL(13),
  323. AD7606_CHANNEL(14),
  324. AD7606_CHANNEL(15),
  325. };
  326. static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
  327. /* More devices added in future */
  328. [ID_AD7605_4] = {
  329. .channels = ad7605_channels,
  330. .num_channels = 5,
  331. },
  332. [ID_AD7606_8] = {
  333. .channels = ad7606_channels,
  334. .num_channels = 9,
  335. .oversampling_avail = ad7606_oversampling_avail,
  336. .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
  337. },
  338. [ID_AD7606_6] = {
  339. .channels = ad7606_channels,
  340. .num_channels = 7,
  341. .oversampling_avail = ad7606_oversampling_avail,
  342. .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
  343. },
  344. [ID_AD7606_4] = {
  345. .channels = ad7606_channels,
  346. .num_channels = 5,
  347. .oversampling_avail = ad7606_oversampling_avail,
  348. .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
  349. },
  350. [ID_AD7606B] = {
  351. .channels = ad7606_channels,
  352. .num_channels = 9,
  353. .oversampling_avail = ad7606_oversampling_avail,
  354. .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
  355. },
  356. [ID_AD7616] = {
  357. .channels = ad7616_channels,
  358. .num_channels = 17,
  359. .oversampling_avail = ad7616_oversampling_avail,
  360. .oversampling_num = ARRAY_SIZE(ad7616_oversampling_avail),
  361. .os_req_reset = true,
  362. .init_delay_ms = 15,
  363. },
  364. };
  365. static int ad7606_request_gpios(struct ad7606_state *st)
  366. {
  367. struct device *dev = st->dev;
  368. st->gpio_convst = devm_gpiod_get(dev, "adi,conversion-start",
  369. GPIOD_OUT_LOW);
  370. if (IS_ERR(st->gpio_convst))
  371. return PTR_ERR(st->gpio_convst);
  372. st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  373. if (IS_ERR(st->gpio_reset))
  374. return PTR_ERR(st->gpio_reset);
  375. st->gpio_range = devm_gpiod_get_optional(dev, "adi,range",
  376. GPIOD_OUT_LOW);
  377. if (IS_ERR(st->gpio_range))
  378. return PTR_ERR(st->gpio_range);
  379. st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
  380. GPIOD_OUT_HIGH);
  381. if (IS_ERR(st->gpio_standby))
  382. return PTR_ERR(st->gpio_standby);
  383. st->gpio_frstdata = devm_gpiod_get_optional(dev, "adi,first-data",
  384. GPIOD_IN);
  385. if (IS_ERR(st->gpio_frstdata))
  386. return PTR_ERR(st->gpio_frstdata);
  387. if (!st->chip_info->oversampling_num)
  388. return 0;
  389. st->gpio_os = devm_gpiod_get_array_optional(dev,
  390. "adi,oversampling-ratio",
  391. GPIOD_OUT_LOW);
  392. return PTR_ERR_OR_ZERO(st->gpio_os);
  393. }
  394. /*
  395. * The BUSY signal indicates when conversions are in progress, so when a rising
  396. * edge of CONVST is applied, BUSY goes logic high and transitions low at the
  397. * end of the entire conversion process. The falling edge of the BUSY signal
  398. * triggers this interrupt.
  399. */
  400. static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
  401. {
  402. struct iio_dev *indio_dev = dev_id;
  403. struct ad7606_state *st = iio_priv(indio_dev);
  404. if (iio_buffer_enabled(indio_dev)) {
  405. gpiod_set_value(st->gpio_convst, 0);
  406. iio_trigger_poll_chained(st->trig);
  407. } else {
  408. complete(&st->completion);
  409. }
  410. return IRQ_HANDLED;
  411. };
  412. static int ad7606_validate_trigger(struct iio_dev *indio_dev,
  413. struct iio_trigger *trig)
  414. {
  415. struct ad7606_state *st = iio_priv(indio_dev);
  416. if (st->trig != trig)
  417. return -EINVAL;
  418. return 0;
  419. }
  420. static int ad7606_buffer_postenable(struct iio_dev *indio_dev)
  421. {
  422. struct ad7606_state *st = iio_priv(indio_dev);
  423. gpiod_set_value(st->gpio_convst, 1);
  424. return 0;
  425. }
  426. static int ad7606_buffer_predisable(struct iio_dev *indio_dev)
  427. {
  428. struct ad7606_state *st = iio_priv(indio_dev);
  429. gpiod_set_value(st->gpio_convst, 0);
  430. return 0;
  431. }
  432. static const struct iio_buffer_setup_ops ad7606_buffer_ops = {
  433. .postenable = &ad7606_buffer_postenable,
  434. .predisable = &ad7606_buffer_predisable,
  435. };
  436. static const struct iio_info ad7606_info_no_os_or_range = {
  437. .read_raw = &ad7606_read_raw,
  438. .validate_trigger = &ad7606_validate_trigger,
  439. };
  440. static const struct iio_info ad7606_info_os_and_range = {
  441. .read_raw = &ad7606_read_raw,
  442. .write_raw = &ad7606_write_raw,
  443. .attrs = &ad7606_attribute_group_os_and_range,
  444. .validate_trigger = &ad7606_validate_trigger,
  445. };
  446. static const struct iio_info ad7606_info_os_range_and_debug = {
  447. .read_raw = &ad7606_read_raw,
  448. .write_raw = &ad7606_write_raw,
  449. .debugfs_reg_access = &ad7606_reg_access,
  450. .attrs = &ad7606_attribute_group_os_and_range,
  451. .validate_trigger = &ad7606_validate_trigger,
  452. };
  453. static const struct iio_info ad7606_info_os = {
  454. .read_raw = &ad7606_read_raw,
  455. .write_raw = &ad7606_write_raw,
  456. .attrs = &ad7606_attribute_group_os,
  457. .validate_trigger = &ad7606_validate_trigger,
  458. };
  459. static const struct iio_info ad7606_info_range = {
  460. .read_raw = &ad7606_read_raw,
  461. .write_raw = &ad7606_write_raw,
  462. .attrs = &ad7606_attribute_group_range,
  463. .validate_trigger = &ad7606_validate_trigger,
  464. };
  465. static const struct iio_trigger_ops ad7606_trigger_ops = {
  466. .validate_device = iio_trigger_validate_own_device,
  467. };
  468. static void ad7606_regulator_disable(void *data)
  469. {
  470. struct ad7606_state *st = data;
  471. regulator_disable(st->reg);
  472. }
  473. int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
  474. const char *name, unsigned int id,
  475. const struct ad7606_bus_ops *bops)
  476. {
  477. struct ad7606_state *st;
  478. int ret;
  479. struct iio_dev *indio_dev;
  480. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  481. if (!indio_dev)
  482. return -ENOMEM;
  483. st = iio_priv(indio_dev);
  484. dev_set_drvdata(dev, indio_dev);
  485. st->dev = dev;
  486. mutex_init(&st->lock);
  487. st->bops = bops;
  488. st->base_address = base_address;
  489. /* tied to logic low, analog input range is +/- 5V */
  490. st->range[0] = 0;
  491. st->oversampling = 1;
  492. st->scale_avail = ad7606_scale_avail;
  493. st->num_scales = ARRAY_SIZE(ad7606_scale_avail);
  494. st->reg = devm_regulator_get(dev, "avcc");
  495. if (IS_ERR(st->reg))
  496. return PTR_ERR(st->reg);
  497. ret = regulator_enable(st->reg);
  498. if (ret) {
  499. dev_err(dev, "Failed to enable specified AVcc supply\n");
  500. return ret;
  501. }
  502. ret = devm_add_action_or_reset(dev, ad7606_regulator_disable, st);
  503. if (ret)
  504. return ret;
  505. st->chip_info = &ad7606_chip_info_tbl[id];
  506. if (st->chip_info->oversampling_num) {
  507. st->oversampling_avail = st->chip_info->oversampling_avail;
  508. st->num_os_ratios = st->chip_info->oversampling_num;
  509. }
  510. ret = ad7606_request_gpios(st);
  511. if (ret)
  512. return ret;
  513. if (st->gpio_os) {
  514. if (st->gpio_range)
  515. indio_dev->info = &ad7606_info_os_and_range;
  516. else
  517. indio_dev->info = &ad7606_info_os;
  518. } else {
  519. if (st->gpio_range)
  520. indio_dev->info = &ad7606_info_range;
  521. else
  522. indio_dev->info = &ad7606_info_no_os_or_range;
  523. }
  524. indio_dev->modes = INDIO_DIRECT_MODE;
  525. indio_dev->name = name;
  526. indio_dev->channels = st->chip_info->channels;
  527. indio_dev->num_channels = st->chip_info->num_channels;
  528. init_completion(&st->completion);
  529. ret = ad7606_reset(st);
  530. if (ret)
  531. dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
  532. /* AD7616 requires al least 15ms to reconfigure after a reset */
  533. if (st->chip_info->init_delay_ms) {
  534. if (msleep_interruptible(st->chip_info->init_delay_ms))
  535. return -ERESTARTSYS;
  536. }
  537. st->write_scale = ad7606_write_scale_hw;
  538. st->write_os = ad7606_write_os_hw;
  539. if (st->bops->sw_mode_config)
  540. st->sw_mode_en = device_property_present(st->dev,
  541. "adi,sw-mode");
  542. if (st->sw_mode_en) {
  543. /* Scale of 0.076293 is only available in sw mode */
  544. st->scale_avail = ad7616_sw_scale_avail;
  545. st->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail);
  546. /* After reset, in software mode, ±10 V is set by default */
  547. memset32(st->range, 2, ARRAY_SIZE(st->range));
  548. indio_dev->info = &ad7606_info_os_range_and_debug;
  549. ret = st->bops->sw_mode_config(indio_dev);
  550. if (ret < 0)
  551. return ret;
  552. }
  553. st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
  554. indio_dev->name,
  555. iio_device_id(indio_dev));
  556. if (!st->trig)
  557. return -ENOMEM;
  558. st->trig->ops = &ad7606_trigger_ops;
  559. iio_trigger_set_drvdata(st->trig, indio_dev);
  560. ret = devm_iio_trigger_register(dev, st->trig);
  561. if (ret)
  562. return ret;
  563. indio_dev->trig = iio_trigger_get(st->trig);
  564. ret = devm_request_threaded_irq(dev, irq,
  565. NULL,
  566. &ad7606_interrupt,
  567. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  568. name, indio_dev);
  569. if (ret)
  570. return ret;
  571. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  572. &iio_pollfunc_store_time,
  573. &ad7606_trigger_handler,
  574. &ad7606_buffer_ops);
  575. if (ret)
  576. return ret;
  577. return devm_iio_device_register(dev, indio_dev);
  578. }
  579. EXPORT_SYMBOL_NS_GPL(ad7606_probe, IIO_AD7606);
  580. #ifdef CONFIG_PM_SLEEP
  581. static int ad7606_suspend(struct device *dev)
  582. {
  583. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  584. struct ad7606_state *st = iio_priv(indio_dev);
  585. if (st->gpio_standby) {
  586. gpiod_set_value(st->gpio_range, 1);
  587. gpiod_set_value(st->gpio_standby, 0);
  588. }
  589. return 0;
  590. }
  591. static int ad7606_resume(struct device *dev)
  592. {
  593. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  594. struct ad7606_state *st = iio_priv(indio_dev);
  595. if (st->gpio_standby) {
  596. gpiod_set_value(st->gpio_range, st->range[0]);
  597. gpiod_set_value(st->gpio_standby, 1);
  598. ad7606_reset(st);
  599. }
  600. return 0;
  601. }
  602. SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
  603. EXPORT_SYMBOL_NS_GPL(ad7606_pm_ops, IIO_AD7606);
  604. #endif
  605. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  606. MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
  607. MODULE_LICENSE("GPL v2");