ad7150.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * AD7150 capacitive sensor driver supporting AD7150/1/6
  4. *
  5. * Copyright 2010-2011 Analog Devices Inc.
  6. * Copyright 2021 Jonathan Cameron <[email protected]>
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/device.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/i2c.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/events.h>
  21. #define AD7150_STATUS_REG 0
  22. #define AD7150_STATUS_OUT1 BIT(3)
  23. #define AD7150_STATUS_OUT2 BIT(5)
  24. #define AD7150_CH1_DATA_HIGH_REG 1
  25. #define AD7150_CH2_DATA_HIGH_REG 3
  26. #define AD7150_CH1_AVG_HIGH_REG 5
  27. #define AD7150_CH2_AVG_HIGH_REG 7
  28. #define AD7150_CH1_SENSITIVITY_REG 9
  29. #define AD7150_CH1_THR_HOLD_H_REG 9
  30. #define AD7150_CH1_TIMEOUT_REG 10
  31. #define AD7150_CH_TIMEOUT_RECEDING GENMASK(3, 0)
  32. #define AD7150_CH_TIMEOUT_APPROACHING GENMASK(7, 4)
  33. #define AD7150_CH1_SETUP_REG 11
  34. #define AD7150_CH2_SENSITIVITY_REG 12
  35. #define AD7150_CH2_THR_HOLD_H_REG 12
  36. #define AD7150_CH2_TIMEOUT_REG 13
  37. #define AD7150_CH2_SETUP_REG 14
  38. #define AD7150_CFG_REG 15
  39. #define AD7150_CFG_FIX BIT(7)
  40. #define AD7150_CFG_THRESHTYPE_MSK GENMASK(6, 5)
  41. #define AD7150_CFG_TT_NEG 0x0
  42. #define AD7150_CFG_TT_POS 0x1
  43. #define AD7150_CFG_TT_IN_WINDOW 0x2
  44. #define AD7150_CFG_TT_OUT_WINDOW 0x3
  45. #define AD7150_PD_TIMER_REG 16
  46. #define AD7150_CH1_CAPDAC_REG 17
  47. #define AD7150_CH2_CAPDAC_REG 18
  48. #define AD7150_SN3_REG 19
  49. #define AD7150_SN2_REG 20
  50. #define AD7150_SN1_REG 21
  51. #define AD7150_SN0_REG 22
  52. #define AD7150_ID_REG 23
  53. enum {
  54. AD7150,
  55. AD7151,
  56. };
  57. /**
  58. * struct ad7150_chip_info - instance specific chip data
  59. * @client: i2c client for this device
  60. * @threshold: thresholds for simple capacitance value events
  61. * @thresh_sensitivity: threshold for simple capacitance offset
  62. * from 'average' value.
  63. * @thresh_timeout: a timeout, in samples from the moment an
  64. * adaptive threshold event occurs to when the average
  65. * value jumps to current value. Note made up of two fields,
  66. * 3:0 are for timeout receding - applies if below lower threshold
  67. * 7:4 are for timeout approaching - applies if above upper threshold
  68. * @state_lock: ensure consistent state of this structure wrt the
  69. * hardware.
  70. * @interrupts: one or two interrupt numbers depending on device type.
  71. * @int_enabled: is a given interrupt currently enabled.
  72. * @type: threshold type
  73. * @dir: threshold direction
  74. */
  75. struct ad7150_chip_info {
  76. struct i2c_client *client;
  77. u16 threshold[2][2];
  78. u8 thresh_sensitivity[2][2];
  79. u8 thresh_timeout[2][2];
  80. struct mutex state_lock;
  81. int interrupts[2];
  82. bool int_enabled[2];
  83. enum iio_event_type type;
  84. enum iio_event_direction dir;
  85. };
  86. static const u8 ad7150_addresses[][6] = {
  87. { AD7150_CH1_DATA_HIGH_REG, AD7150_CH1_AVG_HIGH_REG,
  88. AD7150_CH1_SETUP_REG, AD7150_CH1_THR_HOLD_H_REG,
  89. AD7150_CH1_SENSITIVITY_REG, AD7150_CH1_TIMEOUT_REG },
  90. { AD7150_CH2_DATA_HIGH_REG, AD7150_CH2_AVG_HIGH_REG,
  91. AD7150_CH2_SETUP_REG, AD7150_CH2_THR_HOLD_H_REG,
  92. AD7150_CH2_SENSITIVITY_REG, AD7150_CH2_TIMEOUT_REG },
  93. };
  94. static int ad7150_read_raw(struct iio_dev *indio_dev,
  95. struct iio_chan_spec const *chan,
  96. int *val,
  97. int *val2,
  98. long mask)
  99. {
  100. struct ad7150_chip_info *chip = iio_priv(indio_dev);
  101. int channel = chan->channel;
  102. int ret;
  103. switch (mask) {
  104. case IIO_CHAN_INFO_RAW:
  105. ret = i2c_smbus_read_word_swapped(chip->client,
  106. ad7150_addresses[channel][0]);
  107. if (ret < 0)
  108. return ret;
  109. *val = ret >> 4;
  110. return IIO_VAL_INT;
  111. case IIO_CHAN_INFO_AVERAGE_RAW:
  112. ret = i2c_smbus_read_word_swapped(chip->client,
  113. ad7150_addresses[channel][1]);
  114. if (ret < 0)
  115. return ret;
  116. *val = ret;
  117. return IIO_VAL_INT;
  118. case IIO_CHAN_INFO_SCALE:
  119. /*
  120. * Base units for capacitance are nano farads and the value
  121. * calculated from the datasheet formula is in picofarad
  122. * so multiply by 1000
  123. */
  124. *val = 1000;
  125. *val2 = 40944 >> 4; /* To match shift in _RAW */
  126. return IIO_VAL_FRACTIONAL;
  127. case IIO_CHAN_INFO_OFFSET:
  128. *val = -(12288 >> 4); /* To match shift in _RAW */
  129. return IIO_VAL_INT;
  130. case IIO_CHAN_INFO_SAMP_FREQ:
  131. /* Strangely same for both 1 and 2 chan parts */
  132. *val = 100;
  133. return IIO_VAL_INT;
  134. default:
  135. return -EINVAL;
  136. }
  137. }
  138. static int ad7150_read_event_config(struct iio_dev *indio_dev,
  139. const struct iio_chan_spec *chan,
  140. enum iio_event_type type,
  141. enum iio_event_direction dir)
  142. {
  143. struct ad7150_chip_info *chip = iio_priv(indio_dev);
  144. u8 threshtype;
  145. bool thrfixed;
  146. int ret;
  147. ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG_REG);
  148. if (ret < 0)
  149. return ret;
  150. threshtype = FIELD_GET(AD7150_CFG_THRESHTYPE_MSK, ret);
  151. /*check if threshold mode is fixed or adaptive*/
  152. thrfixed = FIELD_GET(AD7150_CFG_FIX, ret);
  153. switch (type) {
  154. case IIO_EV_TYPE_THRESH_ADAPTIVE:
  155. if (dir == IIO_EV_DIR_RISING)
  156. return !thrfixed && (threshtype == AD7150_CFG_TT_POS);
  157. return !thrfixed && (threshtype == AD7150_CFG_TT_NEG);
  158. case IIO_EV_TYPE_THRESH:
  159. if (dir == IIO_EV_DIR_RISING)
  160. return thrfixed && (threshtype == AD7150_CFG_TT_POS);
  161. return thrfixed && (threshtype == AD7150_CFG_TT_NEG);
  162. default:
  163. break;
  164. }
  165. return -EINVAL;
  166. }
  167. /* state_lock should be held to ensure consistent state */
  168. static int ad7150_write_event_params(struct iio_dev *indio_dev,
  169. unsigned int chan,
  170. enum iio_event_type type,
  171. enum iio_event_direction dir)
  172. {
  173. struct ad7150_chip_info *chip = iio_priv(indio_dev);
  174. int rising = (dir == IIO_EV_DIR_RISING);
  175. /* Only update value live, if parameter is in use */
  176. if ((type != chip->type) || (dir != chip->dir))
  177. return 0;
  178. switch (type) {
  179. /* Note completely different from the adaptive versions */
  180. case IIO_EV_TYPE_THRESH: {
  181. u16 value = chip->threshold[rising][chan];
  182. return i2c_smbus_write_word_swapped(chip->client,
  183. ad7150_addresses[chan][3],
  184. value);
  185. }
  186. case IIO_EV_TYPE_THRESH_ADAPTIVE: {
  187. int ret;
  188. u8 sens, timeout;
  189. sens = chip->thresh_sensitivity[rising][chan];
  190. ret = i2c_smbus_write_byte_data(chip->client,
  191. ad7150_addresses[chan][4],
  192. sens);
  193. if (ret)
  194. return ret;
  195. /*
  196. * Single timeout register contains timeouts for both
  197. * directions.
  198. */
  199. timeout = FIELD_PREP(AD7150_CH_TIMEOUT_APPROACHING,
  200. chip->thresh_timeout[1][chan]);
  201. timeout |= FIELD_PREP(AD7150_CH_TIMEOUT_RECEDING,
  202. chip->thresh_timeout[0][chan]);
  203. return i2c_smbus_write_byte_data(chip->client,
  204. ad7150_addresses[chan][5],
  205. timeout);
  206. }
  207. default:
  208. return -EINVAL;
  209. }
  210. }
  211. static int ad7150_write_event_config(struct iio_dev *indio_dev,
  212. const struct iio_chan_spec *chan,
  213. enum iio_event_type type,
  214. enum iio_event_direction dir, int state)
  215. {
  216. struct ad7150_chip_info *chip = iio_priv(indio_dev);
  217. int ret = 0;
  218. /*
  219. * There is only a single shared control and no on chip
  220. * interrupt disables for the two interrupt lines.
  221. * So, enabling will switch the events configured to enable
  222. * whatever was most recently requested and if necessary enable_irq()
  223. * the interrupt and any disable will disable_irq() for that
  224. * channels interrupt.
  225. */
  226. if (!state) {
  227. if ((chip->int_enabled[chan->channel]) &&
  228. (type == chip->type) && (dir == chip->dir)) {
  229. disable_irq(chip->interrupts[chan->channel]);
  230. chip->int_enabled[chan->channel] = false;
  231. }
  232. return 0;
  233. }
  234. mutex_lock(&chip->state_lock);
  235. if ((type != chip->type) || (dir != chip->dir)) {
  236. int rising = (dir == IIO_EV_DIR_RISING);
  237. u8 thresh_type, cfg, fixed;
  238. /*
  239. * Need to temporarily disable both interrupts if
  240. * enabled - this is to avoid races around changing
  241. * config and thresholds.
  242. * Note enable/disable_irq() are reference counted so
  243. * no need to check if already enabled.
  244. */
  245. disable_irq(chip->interrupts[0]);
  246. disable_irq(chip->interrupts[1]);
  247. ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG_REG);
  248. if (ret < 0)
  249. goto error_ret;
  250. cfg = ret & ~(AD7150_CFG_THRESHTYPE_MSK | AD7150_CFG_FIX);
  251. if (type == IIO_EV_TYPE_THRESH_ADAPTIVE)
  252. fixed = 0;
  253. else
  254. fixed = 1;
  255. if (rising)
  256. thresh_type = AD7150_CFG_TT_POS;
  257. else
  258. thresh_type = AD7150_CFG_TT_NEG;
  259. cfg |= FIELD_PREP(AD7150_CFG_FIX, fixed) |
  260. FIELD_PREP(AD7150_CFG_THRESHTYPE_MSK, thresh_type);
  261. ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG_REG,
  262. cfg);
  263. if (ret < 0)
  264. goto error_ret;
  265. /*
  266. * There is a potential race condition here, but not easy
  267. * to close given we can't disable the interrupt at the
  268. * chip side of things. Rely on the status bit.
  269. */
  270. chip->type = type;
  271. chip->dir = dir;
  272. /* update control attributes */
  273. ret = ad7150_write_event_params(indio_dev, chan->channel, type,
  274. dir);
  275. if (ret)
  276. goto error_ret;
  277. /* reenable any irq's we disabled whilst changing mode */
  278. enable_irq(chip->interrupts[0]);
  279. enable_irq(chip->interrupts[1]);
  280. }
  281. if (!chip->int_enabled[chan->channel]) {
  282. enable_irq(chip->interrupts[chan->channel]);
  283. chip->int_enabled[chan->channel] = true;
  284. }
  285. error_ret:
  286. mutex_unlock(&chip->state_lock);
  287. return ret;
  288. }
  289. static int ad7150_read_event_value(struct iio_dev *indio_dev,
  290. const struct iio_chan_spec *chan,
  291. enum iio_event_type type,
  292. enum iio_event_direction dir,
  293. enum iio_event_info info,
  294. int *val, int *val2)
  295. {
  296. struct ad7150_chip_info *chip = iio_priv(indio_dev);
  297. int rising = (dir == IIO_EV_DIR_RISING);
  298. /* Complex register sharing going on here */
  299. switch (info) {
  300. case IIO_EV_INFO_VALUE:
  301. switch (type) {
  302. case IIO_EV_TYPE_THRESH_ADAPTIVE:
  303. *val = chip->thresh_sensitivity[rising][chan->channel];
  304. return IIO_VAL_INT;
  305. case IIO_EV_TYPE_THRESH:
  306. *val = chip->threshold[rising][chan->channel];
  307. return IIO_VAL_INT;
  308. default:
  309. return -EINVAL;
  310. }
  311. case IIO_EV_INFO_TIMEOUT:
  312. *val = 0;
  313. *val2 = chip->thresh_timeout[rising][chan->channel] * 10000;
  314. return IIO_VAL_INT_PLUS_MICRO;
  315. default:
  316. return -EINVAL;
  317. }
  318. }
  319. static int ad7150_write_event_value(struct iio_dev *indio_dev,
  320. const struct iio_chan_spec *chan,
  321. enum iio_event_type type,
  322. enum iio_event_direction dir,
  323. enum iio_event_info info,
  324. int val, int val2)
  325. {
  326. int ret;
  327. struct ad7150_chip_info *chip = iio_priv(indio_dev);
  328. int rising = (dir == IIO_EV_DIR_RISING);
  329. mutex_lock(&chip->state_lock);
  330. switch (info) {
  331. case IIO_EV_INFO_VALUE:
  332. switch (type) {
  333. case IIO_EV_TYPE_THRESH_ADAPTIVE:
  334. chip->thresh_sensitivity[rising][chan->channel] = val;
  335. break;
  336. case IIO_EV_TYPE_THRESH:
  337. chip->threshold[rising][chan->channel] = val;
  338. break;
  339. default:
  340. ret = -EINVAL;
  341. goto error_ret;
  342. }
  343. break;
  344. case IIO_EV_INFO_TIMEOUT: {
  345. /*
  346. * Raw timeout is in cycles of 10 msecs as long as both
  347. * channels are enabled.
  348. * In terms of INT_PLUS_MICRO, that is in units of 10,000
  349. */
  350. int timeout = val2 / 10000;
  351. if (val != 0 || timeout < 0 || timeout > 15 || val2 % 10000) {
  352. ret = -EINVAL;
  353. goto error_ret;
  354. }
  355. chip->thresh_timeout[rising][chan->channel] = timeout;
  356. break;
  357. }
  358. default:
  359. ret = -EINVAL;
  360. goto error_ret;
  361. }
  362. /* write back if active */
  363. ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
  364. error_ret:
  365. mutex_unlock(&chip->state_lock);
  366. return ret;
  367. }
  368. static const struct iio_event_spec ad7150_events[] = {
  369. {
  370. .type = IIO_EV_TYPE_THRESH,
  371. .dir = IIO_EV_DIR_RISING,
  372. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  373. BIT(IIO_EV_INFO_ENABLE),
  374. }, {
  375. .type = IIO_EV_TYPE_THRESH,
  376. .dir = IIO_EV_DIR_FALLING,
  377. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  378. BIT(IIO_EV_INFO_ENABLE),
  379. }, {
  380. .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
  381. .dir = IIO_EV_DIR_RISING,
  382. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  383. BIT(IIO_EV_INFO_ENABLE) |
  384. BIT(IIO_EV_INFO_TIMEOUT),
  385. }, {
  386. .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
  387. .dir = IIO_EV_DIR_FALLING,
  388. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  389. BIT(IIO_EV_INFO_ENABLE) |
  390. BIT(IIO_EV_INFO_TIMEOUT),
  391. },
  392. };
  393. #define AD7150_CAPACITANCE_CHAN(_chan) { \
  394. .type = IIO_CAPACITANCE, \
  395. .indexed = 1, \
  396. .channel = _chan, \
  397. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  398. BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
  399. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  400. BIT(IIO_CHAN_INFO_OFFSET), \
  401. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
  402. .event_spec = ad7150_events, \
  403. .num_event_specs = ARRAY_SIZE(ad7150_events), \
  404. }
  405. #define AD7150_CAPACITANCE_CHAN_NO_IRQ(_chan) { \
  406. .type = IIO_CAPACITANCE, \
  407. .indexed = 1, \
  408. .channel = _chan, \
  409. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  410. BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
  411. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  412. BIT(IIO_CHAN_INFO_OFFSET), \
  413. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
  414. }
  415. static const struct iio_chan_spec ad7150_channels[] = {
  416. AD7150_CAPACITANCE_CHAN(0),
  417. AD7150_CAPACITANCE_CHAN(1),
  418. };
  419. static const struct iio_chan_spec ad7150_channels_no_irq[] = {
  420. AD7150_CAPACITANCE_CHAN_NO_IRQ(0),
  421. AD7150_CAPACITANCE_CHAN_NO_IRQ(1),
  422. };
  423. static const struct iio_chan_spec ad7151_channels[] = {
  424. AD7150_CAPACITANCE_CHAN(0),
  425. };
  426. static const struct iio_chan_spec ad7151_channels_no_irq[] = {
  427. AD7150_CAPACITANCE_CHAN_NO_IRQ(0),
  428. };
  429. static irqreturn_t __ad7150_event_handler(void *private, u8 status_mask,
  430. int channel)
  431. {
  432. struct iio_dev *indio_dev = private;
  433. struct ad7150_chip_info *chip = iio_priv(indio_dev);
  434. s64 timestamp = iio_get_time_ns(indio_dev);
  435. int int_status;
  436. int_status = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS_REG);
  437. if (int_status < 0)
  438. return IRQ_HANDLED;
  439. if (!(int_status & status_mask))
  440. return IRQ_HANDLED;
  441. iio_push_event(indio_dev,
  442. IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, channel,
  443. chip->type, chip->dir),
  444. timestamp);
  445. return IRQ_HANDLED;
  446. }
  447. static irqreturn_t ad7150_event_handler_ch1(int irq, void *private)
  448. {
  449. return __ad7150_event_handler(private, AD7150_STATUS_OUT1, 0);
  450. }
  451. static irqreturn_t ad7150_event_handler_ch2(int irq, void *private)
  452. {
  453. return __ad7150_event_handler(private, AD7150_STATUS_OUT2, 1);
  454. }
  455. static IIO_CONST_ATTR(in_capacitance_thresh_adaptive_timeout_available,
  456. "[0 0.01 0.15]");
  457. static struct attribute *ad7150_event_attributes[] = {
  458. &iio_const_attr_in_capacitance_thresh_adaptive_timeout_available
  459. .dev_attr.attr,
  460. NULL,
  461. };
  462. static const struct attribute_group ad7150_event_attribute_group = {
  463. .attrs = ad7150_event_attributes,
  464. .name = "events",
  465. };
  466. static const struct iio_info ad7150_info = {
  467. .event_attrs = &ad7150_event_attribute_group,
  468. .read_raw = &ad7150_read_raw,
  469. .read_event_config = &ad7150_read_event_config,
  470. .write_event_config = &ad7150_write_event_config,
  471. .read_event_value = &ad7150_read_event_value,
  472. .write_event_value = &ad7150_write_event_value,
  473. };
  474. static const struct iio_info ad7150_info_no_irq = {
  475. .read_raw = &ad7150_read_raw,
  476. };
  477. static void ad7150_reg_disable(void *data)
  478. {
  479. struct regulator *reg = data;
  480. regulator_disable(reg);
  481. }
  482. static int ad7150_probe(struct i2c_client *client,
  483. const struct i2c_device_id *id)
  484. {
  485. struct ad7150_chip_info *chip;
  486. struct iio_dev *indio_dev;
  487. struct regulator *reg;
  488. int ret;
  489. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  490. if (!indio_dev)
  491. return -ENOMEM;
  492. chip = iio_priv(indio_dev);
  493. mutex_init(&chip->state_lock);
  494. chip->client = client;
  495. indio_dev->name = id->name;
  496. indio_dev->modes = INDIO_DIRECT_MODE;
  497. reg = devm_regulator_get(&client->dev, "vdd");
  498. if (IS_ERR(reg))
  499. return PTR_ERR(reg);
  500. ret = regulator_enable(reg);
  501. if (ret)
  502. return ret;
  503. ret = devm_add_action_or_reset(&client->dev, ad7150_reg_disable, reg);
  504. if (ret)
  505. return ret;
  506. chip->interrupts[0] = fwnode_irq_get(dev_fwnode(&client->dev), 0);
  507. if (chip->interrupts[0] < 0)
  508. return chip->interrupts[0];
  509. if (id->driver_data == AD7150) {
  510. chip->interrupts[1] = fwnode_irq_get(dev_fwnode(&client->dev), 1);
  511. if (chip->interrupts[1] < 0)
  512. return chip->interrupts[1];
  513. }
  514. if (chip->interrupts[0] &&
  515. (id->driver_data == AD7151 || chip->interrupts[1])) {
  516. irq_set_status_flags(chip->interrupts[0], IRQ_NOAUTOEN);
  517. ret = devm_request_threaded_irq(&client->dev,
  518. chip->interrupts[0],
  519. NULL,
  520. &ad7150_event_handler_ch1,
  521. IRQF_TRIGGER_RISING |
  522. IRQF_ONESHOT,
  523. "ad7150_irq1",
  524. indio_dev);
  525. if (ret)
  526. return ret;
  527. indio_dev->info = &ad7150_info;
  528. switch (id->driver_data) {
  529. case AD7150:
  530. indio_dev->channels = ad7150_channels;
  531. indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
  532. irq_set_status_flags(chip->interrupts[1], IRQ_NOAUTOEN);
  533. ret = devm_request_threaded_irq(&client->dev,
  534. chip->interrupts[1],
  535. NULL,
  536. &ad7150_event_handler_ch2,
  537. IRQF_TRIGGER_RISING |
  538. IRQF_ONESHOT,
  539. "ad7150_irq2",
  540. indio_dev);
  541. if (ret)
  542. return ret;
  543. break;
  544. case AD7151:
  545. indio_dev->channels = ad7151_channels;
  546. indio_dev->num_channels = ARRAY_SIZE(ad7151_channels);
  547. break;
  548. default:
  549. return -EINVAL;
  550. }
  551. } else {
  552. indio_dev->info = &ad7150_info_no_irq;
  553. switch (id->driver_data) {
  554. case AD7150:
  555. indio_dev->channels = ad7150_channels_no_irq;
  556. indio_dev->num_channels =
  557. ARRAY_SIZE(ad7150_channels_no_irq);
  558. break;
  559. case AD7151:
  560. indio_dev->channels = ad7151_channels_no_irq;
  561. indio_dev->num_channels =
  562. ARRAY_SIZE(ad7151_channels_no_irq);
  563. break;
  564. default:
  565. return -EINVAL;
  566. }
  567. }
  568. return devm_iio_device_register(indio_dev->dev.parent, indio_dev);
  569. }
  570. static const struct i2c_device_id ad7150_id[] = {
  571. { "ad7150", AD7150 },
  572. { "ad7151", AD7151 },
  573. { "ad7156", AD7150 },
  574. {}
  575. };
  576. MODULE_DEVICE_TABLE(i2c, ad7150_id);
  577. static const struct of_device_id ad7150_of_match[] = {
  578. { "adi,ad7150" },
  579. { "adi,ad7151" },
  580. { "adi,ad7156" },
  581. {}
  582. };
  583. static struct i2c_driver ad7150_driver = {
  584. .driver = {
  585. .name = "ad7150",
  586. .of_match_table = ad7150_of_match,
  587. },
  588. .probe = ad7150_probe,
  589. .id_table = ad7150_id,
  590. };
  591. module_i2c_driver(ad7150_driver);
  592. MODULE_AUTHOR("Barry Song <[email protected]>");
  593. MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
  594. MODULE_LICENSE("GPL v2");