ad799x.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * iio/adc/ad799x.c
  4. * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc.
  5. *
  6. * based on iio/adc/max1363
  7. * Copyright (C) 2008-2010 Jonathan Cameron
  8. *
  9. * based on linux/drivers/i2c/chips/max123x
  10. * Copyright (C) 2002-2004 Stefan Eletzhofer
  11. *
  12. * based on linux/drivers/acron/char/pcf8583.c
  13. * Copyright (C) 2000 Russell King
  14. *
  15. * ad799x.c
  16. *
  17. * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
  18. * ad7998 and similar chips.
  19. */
  20. #include <linux/interrupt.h>
  21. #include <linux/device.h>
  22. #include <linux/kernel.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/i2c.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #include <linux/err.h>
  29. #include <linux/module.h>
  30. #include <linux/mutex.h>
  31. #include <linux/bitops.h>
  32. #include <linux/iio/iio.h>
  33. #include <linux/iio/sysfs.h>
  34. #include <linux/iio/events.h>
  35. #include <linux/iio/buffer.h>
  36. #include <linux/iio/trigger_consumer.h>
  37. #include <linux/iio/triggered_buffer.h>
  38. #define AD799X_CHANNEL_SHIFT 4
  39. /*
  40. * AD7991, AD7995 and AD7999 defines
  41. */
  42. #define AD7991_REF_SEL 0x08
  43. #define AD7991_FLTR 0x04
  44. #define AD7991_BIT_TRIAL_DELAY 0x02
  45. #define AD7991_SAMPLE_DELAY 0x01
  46. /*
  47. * AD7992, AD7993, AD7994, AD7997 and AD7998 defines
  48. */
  49. #define AD7998_FLTR BIT(3)
  50. #define AD7998_ALERT_EN BIT(2)
  51. #define AD7998_BUSY_ALERT BIT(1)
  52. #define AD7998_BUSY_ALERT_POL BIT(0)
  53. #define AD7998_CONV_RES_REG 0x0
  54. #define AD7998_ALERT_STAT_REG 0x1
  55. #define AD7998_CONF_REG 0x2
  56. #define AD7998_CYCLE_TMR_REG 0x3
  57. #define AD7998_DATALOW_REG(x) ((x) * 3 + 0x4)
  58. #define AD7998_DATAHIGH_REG(x) ((x) * 3 + 0x5)
  59. #define AD7998_HYST_REG(x) ((x) * 3 + 0x6)
  60. #define AD7998_CYC_MASK GENMASK(2, 0)
  61. #define AD7998_CYC_DIS 0x0
  62. #define AD7998_CYC_TCONF_32 0x1
  63. #define AD7998_CYC_TCONF_64 0x2
  64. #define AD7998_CYC_TCONF_128 0x3
  65. #define AD7998_CYC_TCONF_256 0x4
  66. #define AD7998_CYC_TCONF_512 0x5
  67. #define AD7998_CYC_TCONF_1024 0x6
  68. #define AD7998_CYC_TCONF_2048 0x7
  69. #define AD7998_ALERT_STAT_CLEAR 0xFF
  70. /*
  71. * AD7997 and AD7997 defines
  72. */
  73. #define AD7997_8_READ_SINGLE BIT(7)
  74. #define AD7997_8_READ_SEQUENCE (BIT(6) | BIT(5) | BIT(4))
  75. enum {
  76. ad7991,
  77. ad7995,
  78. ad7999,
  79. ad7992,
  80. ad7993,
  81. ad7994,
  82. ad7997,
  83. ad7998
  84. };
  85. /**
  86. * struct ad799x_chip_config - chip specific information
  87. * @channel: channel specification
  88. * @default_config: device default configuration
  89. * @info: pointer to iio_info struct
  90. */
  91. struct ad799x_chip_config {
  92. const struct iio_chan_spec channel[9];
  93. u16 default_config;
  94. const struct iio_info *info;
  95. };
  96. /**
  97. * struct ad799x_chip_info - chip specific information
  98. * @num_channels: number of channels
  99. * @noirq_config: device configuration w/o IRQ
  100. * @irq_config: device configuration w/IRQ
  101. * @has_vref: device supports external reference voltage
  102. */
  103. struct ad799x_chip_info {
  104. int num_channels;
  105. const struct ad799x_chip_config noirq_config;
  106. const struct ad799x_chip_config irq_config;
  107. bool has_vref;
  108. };
  109. struct ad799x_state {
  110. struct i2c_client *client;
  111. const struct ad799x_chip_config *chip_config;
  112. struct regulator *reg;
  113. struct regulator *vref;
  114. /* lock to protect against multiple access to the device */
  115. struct mutex lock;
  116. unsigned int id;
  117. u16 config;
  118. u8 *rx_buf;
  119. unsigned int transfer_size;
  120. };
  121. static int ad799x_write_config(struct ad799x_state *st, u16 val)
  122. {
  123. switch (st->id) {
  124. case ad7997:
  125. case ad7998:
  126. return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG,
  127. val);
  128. case ad7992:
  129. case ad7993:
  130. case ad7994:
  131. return i2c_smbus_write_byte_data(st->client, AD7998_CONF_REG,
  132. val);
  133. default:
  134. /* Will be written when doing a conversion */
  135. st->config = val;
  136. return 0;
  137. }
  138. }
  139. static int ad799x_read_config(struct ad799x_state *st)
  140. {
  141. switch (st->id) {
  142. case ad7997:
  143. case ad7998:
  144. return i2c_smbus_read_word_swapped(st->client, AD7998_CONF_REG);
  145. case ad7992:
  146. case ad7993:
  147. case ad7994:
  148. return i2c_smbus_read_byte_data(st->client, AD7998_CONF_REG);
  149. default:
  150. /* No readback support */
  151. return st->config;
  152. }
  153. }
  154. static int ad799x_update_config(struct ad799x_state *st, u16 config)
  155. {
  156. int ret;
  157. ret = ad799x_write_config(st, config);
  158. if (ret < 0)
  159. return ret;
  160. ret = ad799x_read_config(st);
  161. if (ret < 0)
  162. return ret;
  163. st->config = ret;
  164. return 0;
  165. }
  166. static irqreturn_t ad799x_trigger_handler(int irq, void *p)
  167. {
  168. struct iio_poll_func *pf = p;
  169. struct iio_dev *indio_dev = pf->indio_dev;
  170. struct ad799x_state *st = iio_priv(indio_dev);
  171. int b_sent;
  172. u8 cmd;
  173. switch (st->id) {
  174. case ad7991:
  175. case ad7995:
  176. case ad7999:
  177. cmd = st->config |
  178. (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
  179. break;
  180. case ad7992:
  181. case ad7993:
  182. case ad7994:
  183. cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
  184. AD7998_CONV_RES_REG;
  185. break;
  186. case ad7997:
  187. case ad7998:
  188. cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
  189. break;
  190. default:
  191. cmd = 0;
  192. }
  193. b_sent = i2c_smbus_read_i2c_block_data(st->client,
  194. cmd, st->transfer_size, st->rx_buf);
  195. if (b_sent < 0)
  196. goto out;
  197. iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
  198. iio_get_time_ns(indio_dev));
  199. out:
  200. iio_trigger_notify_done(indio_dev->trig);
  201. return IRQ_HANDLED;
  202. }
  203. static int ad799x_update_scan_mode(struct iio_dev *indio_dev,
  204. const unsigned long *scan_mask)
  205. {
  206. struct ad799x_state *st = iio_priv(indio_dev);
  207. kfree(st->rx_buf);
  208. st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
  209. if (!st->rx_buf)
  210. return -ENOMEM;
  211. st->transfer_size = bitmap_weight(scan_mask,
  212. iio_get_masklength(indio_dev)) * 2;
  213. switch (st->id) {
  214. case ad7992:
  215. case ad7993:
  216. case ad7994:
  217. case ad7997:
  218. case ad7998:
  219. st->config &= ~(GENMASK(7, 0) << AD799X_CHANNEL_SHIFT);
  220. st->config |= (*scan_mask << AD799X_CHANNEL_SHIFT);
  221. return ad799x_write_config(st, st->config);
  222. default:
  223. return 0;
  224. }
  225. }
  226. static int ad799x_scan_direct(struct ad799x_state *st, unsigned int ch)
  227. {
  228. u8 cmd;
  229. switch (st->id) {
  230. case ad7991:
  231. case ad7995:
  232. case ad7999:
  233. cmd = st->config | (BIT(ch) << AD799X_CHANNEL_SHIFT);
  234. break;
  235. case ad7992:
  236. case ad7993:
  237. case ad7994:
  238. cmd = BIT(ch) << AD799X_CHANNEL_SHIFT;
  239. break;
  240. case ad7997:
  241. case ad7998:
  242. cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. return i2c_smbus_read_word_swapped(st->client, cmd);
  248. }
  249. static int ad799x_read_raw(struct iio_dev *indio_dev,
  250. struct iio_chan_spec const *chan,
  251. int *val,
  252. int *val2,
  253. long m)
  254. {
  255. int ret;
  256. struct ad799x_state *st = iio_priv(indio_dev);
  257. switch (m) {
  258. case IIO_CHAN_INFO_RAW:
  259. if (!iio_device_claim_direct(indio_dev))
  260. return -EBUSY;
  261. mutex_lock(&st->lock);
  262. ret = ad799x_scan_direct(st, chan->scan_index);
  263. mutex_unlock(&st->lock);
  264. iio_device_release_direct(indio_dev);
  265. if (ret < 0)
  266. return ret;
  267. *val = (ret >> chan->scan_type.shift) &
  268. GENMASK(chan->scan_type.realbits - 1, 0);
  269. return IIO_VAL_INT;
  270. case IIO_CHAN_INFO_SCALE:
  271. if (st->vref)
  272. ret = regulator_get_voltage(st->vref);
  273. else
  274. ret = regulator_get_voltage(st->reg);
  275. if (ret < 0)
  276. return ret;
  277. *val = ret / 1000;
  278. *val2 = chan->scan_type.realbits;
  279. return IIO_VAL_FRACTIONAL_LOG2;
  280. }
  281. return -EINVAL;
  282. }
  283. static const unsigned int ad7998_frequencies[] = {
  284. [AD7998_CYC_DIS] = 0,
  285. [AD7998_CYC_TCONF_32] = 15625,
  286. [AD7998_CYC_TCONF_64] = 7812,
  287. [AD7998_CYC_TCONF_128] = 3906,
  288. [AD7998_CYC_TCONF_512] = 976,
  289. [AD7998_CYC_TCONF_1024] = 488,
  290. [AD7998_CYC_TCONF_2048] = 244,
  291. };
  292. static ssize_t ad799x_read_frequency(struct device *dev,
  293. struct device_attribute *attr,
  294. char *buf)
  295. {
  296. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  297. struct ad799x_state *st = iio_priv(indio_dev);
  298. int ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
  299. if (ret < 0)
  300. return ret;
  301. return sprintf(buf, "%u\n", ad7998_frequencies[ret & AD7998_CYC_MASK]);
  302. }
  303. static ssize_t ad799x_write_frequency(struct device *dev,
  304. struct device_attribute *attr,
  305. const char *buf,
  306. size_t len)
  307. {
  308. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  309. struct ad799x_state *st = iio_priv(indio_dev);
  310. long val;
  311. int ret, i;
  312. ret = kstrtol(buf, 10, &val);
  313. if (ret)
  314. return ret;
  315. mutex_lock(&st->lock);
  316. ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
  317. if (ret < 0)
  318. goto error_ret_mutex;
  319. /* Wipe the bits clean */
  320. ret &= ~AD7998_CYC_MASK;
  321. for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
  322. if (val == ad7998_frequencies[i])
  323. break;
  324. if (i == ARRAY_SIZE(ad7998_frequencies)) {
  325. ret = -EINVAL;
  326. goto error_ret_mutex;
  327. }
  328. ret = i2c_smbus_write_byte_data(st->client, AD7998_CYCLE_TMR_REG,
  329. ret | i);
  330. if (ret < 0)
  331. goto error_ret_mutex;
  332. ret = len;
  333. error_ret_mutex:
  334. mutex_unlock(&st->lock);
  335. return ret;
  336. }
  337. static int ad799x_read_event_config(struct iio_dev *indio_dev,
  338. const struct iio_chan_spec *chan,
  339. enum iio_event_type type,
  340. enum iio_event_direction dir)
  341. {
  342. struct ad799x_state *st = iio_priv(indio_dev);
  343. if (!(st->config & AD7998_ALERT_EN))
  344. return 0;
  345. if ((st->config >> AD799X_CHANNEL_SHIFT) & BIT(chan->scan_index))
  346. return 1;
  347. return 0;
  348. }
  349. static int ad799x_write_event_config(struct iio_dev *indio_dev,
  350. const struct iio_chan_spec *chan,
  351. enum iio_event_type type,
  352. enum iio_event_direction dir,
  353. bool state)
  354. {
  355. struct ad799x_state *st = iio_priv(indio_dev);
  356. int ret;
  357. if (!iio_device_claim_direct(indio_dev))
  358. return -EBUSY;
  359. mutex_lock(&st->lock);
  360. if (state)
  361. st->config |= BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT;
  362. else
  363. st->config &= ~(BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT);
  364. if (st->config >> AD799X_CHANNEL_SHIFT)
  365. st->config |= AD7998_ALERT_EN;
  366. else
  367. st->config &= ~AD7998_ALERT_EN;
  368. ret = ad799x_write_config(st, st->config);
  369. mutex_unlock(&st->lock);
  370. iio_device_release_direct(indio_dev);
  371. return ret;
  372. }
  373. static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
  374. enum iio_event_direction dir,
  375. enum iio_event_info info)
  376. {
  377. switch (info) {
  378. case IIO_EV_INFO_VALUE:
  379. if (dir == IIO_EV_DIR_FALLING)
  380. return AD7998_DATALOW_REG(chan->channel);
  381. else
  382. return AD7998_DATAHIGH_REG(chan->channel);
  383. case IIO_EV_INFO_HYSTERESIS:
  384. return AD7998_HYST_REG(chan->channel);
  385. default:
  386. return -EINVAL;
  387. }
  388. return 0;
  389. }
  390. static int ad799x_write_event_value(struct iio_dev *indio_dev,
  391. const struct iio_chan_spec *chan,
  392. enum iio_event_type type,
  393. enum iio_event_direction dir,
  394. enum iio_event_info info,
  395. int val, int val2)
  396. {
  397. int ret;
  398. struct ad799x_state *st = iio_priv(indio_dev);
  399. if (val < 0 || val > GENMASK(chan->scan_type.realbits - 1, 0))
  400. return -EINVAL;
  401. ret = i2c_smbus_write_word_swapped(st->client,
  402. ad799x_threshold_reg(chan, dir, info),
  403. val << chan->scan_type.shift);
  404. return ret;
  405. }
  406. static int ad799x_read_event_value(struct iio_dev *indio_dev,
  407. const struct iio_chan_spec *chan,
  408. enum iio_event_type type,
  409. enum iio_event_direction dir,
  410. enum iio_event_info info,
  411. int *val, int *val2)
  412. {
  413. int ret;
  414. struct ad799x_state *st = iio_priv(indio_dev);
  415. ret = i2c_smbus_read_word_swapped(st->client,
  416. ad799x_threshold_reg(chan, dir, info));
  417. if (ret < 0)
  418. return ret;
  419. *val = (ret >> chan->scan_type.shift) &
  420. GENMASK(chan->scan_type.realbits - 1, 0);
  421. return IIO_VAL_INT;
  422. }
  423. static irqreturn_t ad799x_event_handler(int irq, void *private)
  424. {
  425. struct iio_dev *indio_dev = private;
  426. struct ad799x_state *st = iio_priv(private);
  427. int i, ret;
  428. ret = i2c_smbus_read_byte_data(st->client, AD7998_ALERT_STAT_REG);
  429. if (ret <= 0)
  430. goto done;
  431. if (i2c_smbus_write_byte_data(st->client, AD7998_ALERT_STAT_REG,
  432. AD7998_ALERT_STAT_CLEAR) < 0)
  433. goto done;
  434. for (i = 0; i < 8; i++) {
  435. if (ret & BIT(i))
  436. iio_push_event(indio_dev,
  437. i & 0x1 ?
  438. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
  439. (i >> 1),
  440. IIO_EV_TYPE_THRESH,
  441. IIO_EV_DIR_RISING) :
  442. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
  443. (i >> 1),
  444. IIO_EV_TYPE_THRESH,
  445. IIO_EV_DIR_FALLING),
  446. iio_get_time_ns(indio_dev));
  447. }
  448. done:
  449. return IRQ_HANDLED;
  450. }
  451. static IIO_DEV_ATTR_SAMP_FREQ(0644,
  452. ad799x_read_frequency,
  453. ad799x_write_frequency);
  454. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
  455. static struct attribute *ad799x_event_attributes[] = {
  456. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  457. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  458. NULL,
  459. };
  460. static const struct attribute_group ad799x_event_attrs_group = {
  461. .attrs = ad799x_event_attributes,
  462. };
  463. static const struct iio_info ad7991_info = {
  464. .read_raw = &ad799x_read_raw,
  465. .update_scan_mode = ad799x_update_scan_mode,
  466. };
  467. static const struct iio_info ad7993_4_7_8_noirq_info = {
  468. .read_raw = &ad799x_read_raw,
  469. .update_scan_mode = ad799x_update_scan_mode,
  470. };
  471. static const struct iio_info ad7993_4_7_8_irq_info = {
  472. .read_raw = &ad799x_read_raw,
  473. .event_attrs = &ad799x_event_attrs_group,
  474. .read_event_config = &ad799x_read_event_config,
  475. .write_event_config = &ad799x_write_event_config,
  476. .read_event_value = &ad799x_read_event_value,
  477. .write_event_value = &ad799x_write_event_value,
  478. .update_scan_mode = ad799x_update_scan_mode,
  479. };
  480. static const struct iio_event_spec ad799x_events[] = {
  481. {
  482. .type = IIO_EV_TYPE_THRESH,
  483. .dir = IIO_EV_DIR_RISING,
  484. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  485. BIT(IIO_EV_INFO_ENABLE),
  486. }, {
  487. .type = IIO_EV_TYPE_THRESH,
  488. .dir = IIO_EV_DIR_FALLING,
  489. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  490. BIT(IIO_EV_INFO_ENABLE),
  491. }, {
  492. .type = IIO_EV_TYPE_THRESH,
  493. .dir = IIO_EV_DIR_EITHER,
  494. .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
  495. },
  496. };
  497. #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
  498. .type = IIO_VOLTAGE, \
  499. .indexed = 1, \
  500. .channel = (_index), \
  501. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  502. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  503. .scan_index = (_index), \
  504. .scan_type = { \
  505. .sign = 'u', \
  506. .realbits = (_realbits), \
  507. .storagebits = 16, \
  508. .shift = 12 - (_realbits), \
  509. .endianness = IIO_BE, \
  510. }, \
  511. .event_spec = _ev_spec, \
  512. .num_event_specs = _num_ev_spec, \
  513. }
  514. #define AD799X_CHANNEL(_index, _realbits) \
  515. _AD799X_CHANNEL(_index, _realbits, NULL, 0)
  516. #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
  517. _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
  518. ARRAY_SIZE(ad799x_events))
  519. static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
  520. [ad7991] = {
  521. .num_channels = 5,
  522. .has_vref = true,
  523. .noirq_config = {
  524. .channel = {
  525. AD799X_CHANNEL(0, 12),
  526. AD799X_CHANNEL(1, 12),
  527. AD799X_CHANNEL(2, 12),
  528. AD799X_CHANNEL(3, 12),
  529. IIO_CHAN_SOFT_TIMESTAMP(4),
  530. },
  531. .info = &ad7991_info,
  532. },
  533. },
  534. [ad7995] = {
  535. .num_channels = 5,
  536. .has_vref = true,
  537. .noirq_config = {
  538. .channel = {
  539. AD799X_CHANNEL(0, 10),
  540. AD799X_CHANNEL(1, 10),
  541. AD799X_CHANNEL(2, 10),
  542. AD799X_CHANNEL(3, 10),
  543. IIO_CHAN_SOFT_TIMESTAMP(4),
  544. },
  545. .info = &ad7991_info,
  546. },
  547. },
  548. [ad7999] = {
  549. .num_channels = 5,
  550. .has_vref = true,
  551. .noirq_config = {
  552. .channel = {
  553. AD799X_CHANNEL(0, 8),
  554. AD799X_CHANNEL(1, 8),
  555. AD799X_CHANNEL(2, 8),
  556. AD799X_CHANNEL(3, 8),
  557. IIO_CHAN_SOFT_TIMESTAMP(4),
  558. },
  559. .info = &ad7991_info,
  560. },
  561. },
  562. [ad7992] = {
  563. .num_channels = 3,
  564. .noirq_config = {
  565. .channel = {
  566. AD799X_CHANNEL(0, 12),
  567. AD799X_CHANNEL(1, 12),
  568. IIO_CHAN_SOFT_TIMESTAMP(3),
  569. },
  570. .info = &ad7993_4_7_8_noirq_info,
  571. },
  572. .irq_config = {
  573. .channel = {
  574. AD799X_CHANNEL_WITH_EVENTS(0, 12),
  575. AD799X_CHANNEL_WITH_EVENTS(1, 12),
  576. IIO_CHAN_SOFT_TIMESTAMP(3),
  577. },
  578. .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
  579. .info = &ad7993_4_7_8_irq_info,
  580. },
  581. },
  582. [ad7993] = {
  583. .num_channels = 5,
  584. .noirq_config = {
  585. .channel = {
  586. AD799X_CHANNEL(0, 10),
  587. AD799X_CHANNEL(1, 10),
  588. AD799X_CHANNEL(2, 10),
  589. AD799X_CHANNEL(3, 10),
  590. IIO_CHAN_SOFT_TIMESTAMP(4),
  591. },
  592. .info = &ad7993_4_7_8_noirq_info,
  593. },
  594. .irq_config = {
  595. .channel = {
  596. AD799X_CHANNEL_WITH_EVENTS(0, 10),
  597. AD799X_CHANNEL_WITH_EVENTS(1, 10),
  598. AD799X_CHANNEL_WITH_EVENTS(2, 10),
  599. AD799X_CHANNEL_WITH_EVENTS(3, 10),
  600. IIO_CHAN_SOFT_TIMESTAMP(4),
  601. },
  602. .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
  603. .info = &ad7993_4_7_8_irq_info,
  604. },
  605. },
  606. [ad7994] = {
  607. .num_channels = 5,
  608. .has_vref = true,
  609. .noirq_config = {
  610. .channel = {
  611. AD799X_CHANNEL(0, 12),
  612. AD799X_CHANNEL(1, 12),
  613. AD799X_CHANNEL(2, 12),
  614. AD799X_CHANNEL(3, 12),
  615. IIO_CHAN_SOFT_TIMESTAMP(4),
  616. },
  617. .info = &ad7993_4_7_8_noirq_info,
  618. },
  619. .irq_config = {
  620. .channel = {
  621. AD799X_CHANNEL_WITH_EVENTS(0, 12),
  622. AD799X_CHANNEL_WITH_EVENTS(1, 12),
  623. AD799X_CHANNEL_WITH_EVENTS(2, 12),
  624. AD799X_CHANNEL_WITH_EVENTS(3, 12),
  625. IIO_CHAN_SOFT_TIMESTAMP(4),
  626. },
  627. .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
  628. .info = &ad7993_4_7_8_irq_info,
  629. },
  630. },
  631. [ad7997] = {
  632. .num_channels = 9,
  633. .noirq_config = {
  634. .channel = {
  635. AD799X_CHANNEL(0, 10),
  636. AD799X_CHANNEL(1, 10),
  637. AD799X_CHANNEL(2, 10),
  638. AD799X_CHANNEL(3, 10),
  639. AD799X_CHANNEL(4, 10),
  640. AD799X_CHANNEL(5, 10),
  641. AD799X_CHANNEL(6, 10),
  642. AD799X_CHANNEL(7, 10),
  643. IIO_CHAN_SOFT_TIMESTAMP(8),
  644. },
  645. .info = &ad7993_4_7_8_noirq_info,
  646. },
  647. .irq_config = {
  648. .channel = {
  649. AD799X_CHANNEL_WITH_EVENTS(0, 10),
  650. AD799X_CHANNEL_WITH_EVENTS(1, 10),
  651. AD799X_CHANNEL_WITH_EVENTS(2, 10),
  652. AD799X_CHANNEL_WITH_EVENTS(3, 10),
  653. AD799X_CHANNEL(4, 10),
  654. AD799X_CHANNEL(5, 10),
  655. AD799X_CHANNEL(6, 10),
  656. AD799X_CHANNEL(7, 10),
  657. IIO_CHAN_SOFT_TIMESTAMP(8),
  658. },
  659. .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
  660. .info = &ad7993_4_7_8_irq_info,
  661. },
  662. },
  663. [ad7998] = {
  664. .num_channels = 9,
  665. .noirq_config = {
  666. .channel = {
  667. AD799X_CHANNEL(0, 12),
  668. AD799X_CHANNEL(1, 12),
  669. AD799X_CHANNEL(2, 12),
  670. AD799X_CHANNEL(3, 12),
  671. AD799X_CHANNEL(4, 12),
  672. AD799X_CHANNEL(5, 12),
  673. AD799X_CHANNEL(6, 12),
  674. AD799X_CHANNEL(7, 12),
  675. IIO_CHAN_SOFT_TIMESTAMP(8),
  676. },
  677. .info = &ad7993_4_7_8_noirq_info,
  678. },
  679. .irq_config = {
  680. .channel = {
  681. AD799X_CHANNEL_WITH_EVENTS(0, 12),
  682. AD799X_CHANNEL_WITH_EVENTS(1, 12),
  683. AD799X_CHANNEL_WITH_EVENTS(2, 12),
  684. AD799X_CHANNEL_WITH_EVENTS(3, 12),
  685. AD799X_CHANNEL(4, 12),
  686. AD799X_CHANNEL(5, 12),
  687. AD799X_CHANNEL(6, 12),
  688. AD799X_CHANNEL(7, 12),
  689. IIO_CHAN_SOFT_TIMESTAMP(8),
  690. },
  691. .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
  692. .info = &ad7993_4_7_8_irq_info,
  693. },
  694. },
  695. };
  696. static int ad799x_probe(struct i2c_client *client)
  697. {
  698. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  699. int ret;
  700. int extra_config = 0;
  701. struct ad799x_state *st;
  702. struct iio_dev *indio_dev;
  703. const struct ad799x_chip_info *chip_info =
  704. &ad799x_chip_info_tbl[id->driver_data];
  705. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
  706. if (indio_dev == NULL)
  707. return -ENOMEM;
  708. st = iio_priv(indio_dev);
  709. /* this is only used for device removal purposes */
  710. i2c_set_clientdata(client, indio_dev);
  711. st->id = id->driver_data;
  712. if (client->irq > 0 && chip_info->irq_config.info)
  713. st->chip_config = &chip_info->irq_config;
  714. else
  715. st->chip_config = &chip_info->noirq_config;
  716. /* TODO: Add pdata options for filtering and bit delay */
  717. st->reg = devm_regulator_get(&client->dev, "vcc");
  718. if (IS_ERR(st->reg))
  719. return PTR_ERR(st->reg);
  720. ret = regulator_enable(st->reg);
  721. if (ret)
  722. return ret;
  723. /* check if an external reference is supplied */
  724. if (chip_info->has_vref) {
  725. st->vref = devm_regulator_get_optional(&client->dev, "vref");
  726. ret = PTR_ERR_OR_ZERO(st->vref);
  727. if (ret) {
  728. if (ret != -ENODEV)
  729. goto error_disable_reg;
  730. st->vref = NULL;
  731. dev_info(&client->dev, "Using VCC reference voltage\n");
  732. }
  733. if (st->vref) {
  734. dev_info(&client->dev, "Using external reference voltage\n");
  735. extra_config |= AD7991_REF_SEL;
  736. ret = regulator_enable(st->vref);
  737. if (ret)
  738. goto error_disable_reg;
  739. }
  740. }
  741. st->client = client;
  742. indio_dev->name = id->name;
  743. indio_dev->info = st->chip_config->info;
  744. indio_dev->modes = INDIO_DIRECT_MODE;
  745. indio_dev->channels = st->chip_config->channel;
  746. indio_dev->num_channels = chip_info->num_channels;
  747. ret = ad799x_update_config(st, st->chip_config->default_config | extra_config);
  748. if (ret)
  749. goto error_disable_vref;
  750. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  751. &ad799x_trigger_handler, NULL);
  752. if (ret)
  753. goto error_disable_vref;
  754. if (client->irq > 0) {
  755. ret = devm_request_threaded_irq(&client->dev,
  756. client->irq,
  757. NULL,
  758. ad799x_event_handler,
  759. IRQF_TRIGGER_FALLING |
  760. IRQF_ONESHOT,
  761. client->name,
  762. indio_dev);
  763. if (ret)
  764. goto error_cleanup_ring;
  765. }
  766. mutex_init(&st->lock);
  767. ret = iio_device_register(indio_dev);
  768. if (ret)
  769. goto error_cleanup_ring;
  770. return 0;
  771. error_cleanup_ring:
  772. iio_triggered_buffer_cleanup(indio_dev);
  773. error_disable_vref:
  774. if (st->vref)
  775. regulator_disable(st->vref);
  776. error_disable_reg:
  777. regulator_disable(st->reg);
  778. return ret;
  779. }
  780. static void ad799x_remove(struct i2c_client *client)
  781. {
  782. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  783. struct ad799x_state *st = iio_priv(indio_dev);
  784. iio_device_unregister(indio_dev);
  785. iio_triggered_buffer_cleanup(indio_dev);
  786. if (st->vref)
  787. regulator_disable(st->vref);
  788. regulator_disable(st->reg);
  789. kfree(st->rx_buf);
  790. }
  791. static int ad799x_suspend(struct device *dev)
  792. {
  793. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  794. struct ad799x_state *st = iio_priv(indio_dev);
  795. if (st->vref)
  796. regulator_disable(st->vref);
  797. regulator_disable(st->reg);
  798. return 0;
  799. }
  800. static int ad799x_resume(struct device *dev)
  801. {
  802. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  803. struct ad799x_state *st = iio_priv(indio_dev);
  804. int ret;
  805. ret = regulator_enable(st->reg);
  806. if (ret) {
  807. dev_err(dev, "Unable to enable vcc regulator\n");
  808. return ret;
  809. }
  810. if (st->vref) {
  811. ret = regulator_enable(st->vref);
  812. if (ret) {
  813. regulator_disable(st->reg);
  814. dev_err(dev, "Unable to enable vref regulator\n");
  815. return ret;
  816. }
  817. }
  818. /* resync config */
  819. ret = ad799x_update_config(st, st->config);
  820. if (ret) {
  821. if (st->vref)
  822. regulator_disable(st->vref);
  823. regulator_disable(st->reg);
  824. return ret;
  825. }
  826. return 0;
  827. }
  828. static DEFINE_SIMPLE_DEV_PM_OPS(ad799x_pm_ops, ad799x_suspend, ad799x_resume);
  829. static const struct i2c_device_id ad799x_id[] = {
  830. { "ad7991", ad7991 },
  831. { "ad7995", ad7995 },
  832. { "ad7999", ad7999 },
  833. { "ad7992", ad7992 },
  834. { "ad7993", ad7993 },
  835. { "ad7994", ad7994 },
  836. { "ad7997", ad7997 },
  837. { "ad7998", ad7998 },
  838. { }
  839. };
  840. MODULE_DEVICE_TABLE(i2c, ad799x_id);
  841. static struct i2c_driver ad799x_driver = {
  842. .driver = {
  843. .name = "ad799x",
  844. .pm = pm_sleep_ptr(&ad799x_pm_ops),
  845. },
  846. .probe = ad799x_probe,
  847. .remove = ad799x_remove,
  848. .id_table = ad799x_id,
  849. };
  850. module_i2c_driver(ad799x_driver);
  851. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  852. MODULE_DESCRIPTION("Analog Devices AD799x ADC");
  853. MODULE_LICENSE("GPL v2");