sx9500.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. * Copyright (c) 2014 Intel Corporation
  3. *
  4. * Driver for Semtech's SX9500 capacitive proximity/button solution.
  5. * Datasheet available at
  6. * <http://www.semtech.com/images/datasheet/sx9500.pdf>.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/irq.h>
  17. #include <linux/acpi.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/regmap.h>
  20. #include <linux/pm.h>
  21. #include <linux/delay.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/iio/buffer.h>
  24. #include <linux/iio/sysfs.h>
  25. #include <linux/iio/events.h>
  26. #include <linux/iio/trigger.h>
  27. #include <linux/iio/triggered_buffer.h>
  28. #include <linux/iio/trigger_consumer.h>
  29. #define SX9500_DRIVER_NAME "sx9500"
  30. #define SX9500_IRQ_NAME "sx9500_event"
  31. #define SX9500_GPIO_INT "interrupt"
  32. #define SX9500_GPIO_RESET "reset"
  33. /* Register definitions. */
  34. #define SX9500_REG_IRQ_SRC 0x00
  35. #define SX9500_REG_STAT 0x01
  36. #define SX9500_REG_IRQ_MSK 0x03
  37. #define SX9500_REG_PROX_CTRL0 0x06
  38. #define SX9500_REG_PROX_CTRL1 0x07
  39. #define SX9500_REG_PROX_CTRL2 0x08
  40. #define SX9500_REG_PROX_CTRL3 0x09
  41. #define SX9500_REG_PROX_CTRL4 0x0a
  42. #define SX9500_REG_PROX_CTRL5 0x0b
  43. #define SX9500_REG_PROX_CTRL6 0x0c
  44. #define SX9500_REG_PROX_CTRL7 0x0d
  45. #define SX9500_REG_PROX_CTRL8 0x0e
  46. #define SX9500_REG_SENSOR_SEL 0x20
  47. #define SX9500_REG_USE_MSB 0x21
  48. #define SX9500_REG_USE_LSB 0x22
  49. #define SX9500_REG_AVG_MSB 0x23
  50. #define SX9500_REG_AVG_LSB 0x24
  51. #define SX9500_REG_DIFF_MSB 0x25
  52. #define SX9500_REG_DIFF_LSB 0x26
  53. #define SX9500_REG_OFFSET_MSB 0x27
  54. #define SX9500_REG_OFFSET_LSB 0x28
  55. #define SX9500_REG_RESET 0x7f
  56. /* Write this to REG_RESET to do a soft reset. */
  57. #define SX9500_SOFT_RESET 0xde
  58. #define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4)
  59. #define SX9500_SCAN_PERIOD_SHIFT 4
  60. /*
  61. * These serve for identifying IRQ source in the IRQ_SRC register, and
  62. * also for masking the IRQs in the IRQ_MSK register.
  63. */
  64. #define SX9500_CLOSE_IRQ BIT(6)
  65. #define SX9500_FAR_IRQ BIT(5)
  66. #define SX9500_CONVDONE_IRQ BIT(3)
  67. #define SX9500_PROXSTAT_SHIFT 4
  68. #define SX9500_COMPSTAT_MASK GENMASK(3, 0)
  69. #define SX9500_NUM_CHANNELS 4
  70. #define SX9500_CHAN_MASK GENMASK(SX9500_NUM_CHANNELS - 1, 0)
  71. struct sx9500_data {
  72. struct mutex mutex;
  73. struct i2c_client *client;
  74. struct iio_trigger *trig;
  75. struct regmap *regmap;
  76. struct gpio_desc *gpiod_rst;
  77. /*
  78. * Last reading of the proximity status for each channel. We
  79. * only send an event to user space when this changes.
  80. */
  81. bool prox_stat[SX9500_NUM_CHANNELS];
  82. bool event_enabled[SX9500_NUM_CHANNELS];
  83. bool trigger_enabled;
  84. u16 *buffer;
  85. /* Remember enabled channels and sample rate during suspend. */
  86. unsigned int suspend_ctrl0;
  87. struct completion completion;
  88. int data_rdy_users, close_far_users;
  89. int channel_users[SX9500_NUM_CHANNELS];
  90. };
  91. static const struct iio_event_spec sx9500_events[] = {
  92. {
  93. .type = IIO_EV_TYPE_THRESH,
  94. .dir = IIO_EV_DIR_EITHER,
  95. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  96. },
  97. };
  98. #define SX9500_CHANNEL(idx) \
  99. { \
  100. .type = IIO_PROXIMITY, \
  101. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  102. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  103. .indexed = 1, \
  104. .channel = idx, \
  105. .event_spec = sx9500_events, \
  106. .num_event_specs = ARRAY_SIZE(sx9500_events), \
  107. .scan_index = idx, \
  108. .scan_type = { \
  109. .sign = 'u', \
  110. .realbits = 16, \
  111. .storagebits = 16, \
  112. .shift = 0, \
  113. }, \
  114. }
  115. static const struct iio_chan_spec sx9500_channels[] = {
  116. SX9500_CHANNEL(0),
  117. SX9500_CHANNEL(1),
  118. SX9500_CHANNEL(2),
  119. SX9500_CHANNEL(3),
  120. IIO_CHAN_SOFT_TIMESTAMP(4),
  121. };
  122. static const struct {
  123. int val;
  124. int val2;
  125. } sx9500_samp_freq_table[] = {
  126. {33, 333333},
  127. {16, 666666},
  128. {11, 111111},
  129. {8, 333333},
  130. {6, 666666},
  131. {5, 0},
  132. {3, 333333},
  133. {2, 500000},
  134. };
  135. static const unsigned int sx9500_scan_period_table[] = {
  136. 30, 60, 90, 120, 150, 200, 300, 400,
  137. };
  138. static const struct regmap_range sx9500_writable_reg_ranges[] = {
  139. regmap_reg_range(SX9500_REG_IRQ_MSK, SX9500_REG_IRQ_MSK),
  140. regmap_reg_range(SX9500_REG_PROX_CTRL0, SX9500_REG_PROX_CTRL8),
  141. regmap_reg_range(SX9500_REG_SENSOR_SEL, SX9500_REG_SENSOR_SEL),
  142. regmap_reg_range(SX9500_REG_OFFSET_MSB, SX9500_REG_OFFSET_LSB),
  143. regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
  144. };
  145. static const struct regmap_access_table sx9500_writeable_regs = {
  146. .yes_ranges = sx9500_writable_reg_ranges,
  147. .n_yes_ranges = ARRAY_SIZE(sx9500_writable_reg_ranges),
  148. };
  149. /*
  150. * All allocated registers are readable, so we just list unallocated
  151. * ones.
  152. */
  153. static const struct regmap_range sx9500_non_readable_reg_ranges[] = {
  154. regmap_reg_range(SX9500_REG_STAT + 1, SX9500_REG_STAT + 1),
  155. regmap_reg_range(SX9500_REG_IRQ_MSK + 1, SX9500_REG_PROX_CTRL0 - 1),
  156. regmap_reg_range(SX9500_REG_PROX_CTRL8 + 1, SX9500_REG_SENSOR_SEL - 1),
  157. regmap_reg_range(SX9500_REG_OFFSET_LSB + 1, SX9500_REG_RESET - 1),
  158. };
  159. static const struct regmap_access_table sx9500_readable_regs = {
  160. .no_ranges = sx9500_non_readable_reg_ranges,
  161. .n_no_ranges = ARRAY_SIZE(sx9500_non_readable_reg_ranges),
  162. };
  163. static const struct regmap_range sx9500_volatile_reg_ranges[] = {
  164. regmap_reg_range(SX9500_REG_IRQ_SRC, SX9500_REG_STAT),
  165. regmap_reg_range(SX9500_REG_USE_MSB, SX9500_REG_OFFSET_LSB),
  166. regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
  167. };
  168. static const struct regmap_access_table sx9500_volatile_regs = {
  169. .yes_ranges = sx9500_volatile_reg_ranges,
  170. .n_yes_ranges = ARRAY_SIZE(sx9500_volatile_reg_ranges),
  171. };
  172. static const struct regmap_config sx9500_regmap_config = {
  173. .reg_bits = 8,
  174. .val_bits = 8,
  175. .max_register = SX9500_REG_RESET,
  176. .cache_type = REGCACHE_RBTREE,
  177. .wr_table = &sx9500_writeable_regs,
  178. .rd_table = &sx9500_readable_regs,
  179. .volatile_table = &sx9500_volatile_regs,
  180. };
  181. static int sx9500_inc_users(struct sx9500_data *data, int *counter,
  182. unsigned int reg, unsigned int bitmask)
  183. {
  184. (*counter)++;
  185. if (*counter != 1)
  186. /* Bit is already active, nothing to do. */
  187. return 0;
  188. return regmap_update_bits(data->regmap, reg, bitmask, bitmask);
  189. }
  190. static int sx9500_dec_users(struct sx9500_data *data, int *counter,
  191. unsigned int reg, unsigned int bitmask)
  192. {
  193. (*counter)--;
  194. if (*counter != 0)
  195. /* There are more users, do not deactivate. */
  196. return 0;
  197. return regmap_update_bits(data->regmap, reg, bitmask, 0);
  198. }
  199. static int sx9500_inc_chan_users(struct sx9500_data *data, int chan)
  200. {
  201. return sx9500_inc_users(data, &data->channel_users[chan],
  202. SX9500_REG_PROX_CTRL0, BIT(chan));
  203. }
  204. static int sx9500_dec_chan_users(struct sx9500_data *data, int chan)
  205. {
  206. return sx9500_dec_users(data, &data->channel_users[chan],
  207. SX9500_REG_PROX_CTRL0, BIT(chan));
  208. }
  209. static int sx9500_inc_data_rdy_users(struct sx9500_data *data)
  210. {
  211. return sx9500_inc_users(data, &data->data_rdy_users,
  212. SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ);
  213. }
  214. static int sx9500_dec_data_rdy_users(struct sx9500_data *data)
  215. {
  216. return sx9500_dec_users(data, &data->data_rdy_users,
  217. SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ);
  218. }
  219. static int sx9500_inc_close_far_users(struct sx9500_data *data)
  220. {
  221. return sx9500_inc_users(data, &data->close_far_users,
  222. SX9500_REG_IRQ_MSK,
  223. SX9500_CLOSE_IRQ | SX9500_FAR_IRQ);
  224. }
  225. static int sx9500_dec_close_far_users(struct sx9500_data *data)
  226. {
  227. return sx9500_dec_users(data, &data->close_far_users,
  228. SX9500_REG_IRQ_MSK,
  229. SX9500_CLOSE_IRQ | SX9500_FAR_IRQ);
  230. }
  231. static int sx9500_read_prox_data(struct sx9500_data *data,
  232. const struct iio_chan_spec *chan,
  233. int *val)
  234. {
  235. int ret;
  236. __be16 regval;
  237. ret = regmap_write(data->regmap, SX9500_REG_SENSOR_SEL, chan->channel);
  238. if (ret < 0)
  239. return ret;
  240. ret = regmap_bulk_read(data->regmap, SX9500_REG_USE_MSB, &regval, 2);
  241. if (ret < 0)
  242. return ret;
  243. *val = be16_to_cpu(regval);
  244. return IIO_VAL_INT;
  245. }
  246. /*
  247. * If we have no interrupt support, we have to wait for a scan period
  248. * after enabling a channel to get a result.
  249. */
  250. static int sx9500_wait_for_sample(struct sx9500_data *data)
  251. {
  252. int ret;
  253. unsigned int val;
  254. ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &val);
  255. if (ret < 0)
  256. return ret;
  257. val = (val & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT;
  258. msleep(sx9500_scan_period_table[val]);
  259. return 0;
  260. }
  261. static int sx9500_read_proximity(struct sx9500_data *data,
  262. const struct iio_chan_spec *chan,
  263. int *val)
  264. {
  265. int ret;
  266. mutex_lock(&data->mutex);
  267. ret = sx9500_inc_chan_users(data, chan->channel);
  268. if (ret < 0)
  269. goto out;
  270. ret = sx9500_inc_data_rdy_users(data);
  271. if (ret < 0)
  272. goto out_dec_chan;
  273. mutex_unlock(&data->mutex);
  274. if (data->client->irq > 0)
  275. ret = wait_for_completion_interruptible(&data->completion);
  276. else
  277. ret = sx9500_wait_for_sample(data);
  278. mutex_lock(&data->mutex);
  279. if (ret < 0)
  280. goto out_dec_data_rdy;
  281. ret = sx9500_read_prox_data(data, chan, val);
  282. if (ret < 0)
  283. goto out_dec_data_rdy;
  284. ret = sx9500_dec_data_rdy_users(data);
  285. if (ret < 0)
  286. goto out_dec_chan;
  287. ret = sx9500_dec_chan_users(data, chan->channel);
  288. if (ret < 0)
  289. goto out;
  290. ret = IIO_VAL_INT;
  291. goto out;
  292. out_dec_data_rdy:
  293. sx9500_dec_data_rdy_users(data);
  294. out_dec_chan:
  295. sx9500_dec_chan_users(data, chan->channel);
  296. out:
  297. mutex_unlock(&data->mutex);
  298. reinit_completion(&data->completion);
  299. return ret;
  300. }
  301. static int sx9500_read_samp_freq(struct sx9500_data *data,
  302. int *val, int *val2)
  303. {
  304. int ret;
  305. unsigned int regval;
  306. mutex_lock(&data->mutex);
  307. ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &regval);
  308. mutex_unlock(&data->mutex);
  309. if (ret < 0)
  310. return ret;
  311. regval = (regval & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT;
  312. *val = sx9500_samp_freq_table[regval].val;
  313. *val2 = sx9500_samp_freq_table[regval].val2;
  314. return IIO_VAL_INT_PLUS_MICRO;
  315. }
  316. static int sx9500_read_raw(struct iio_dev *indio_dev,
  317. const struct iio_chan_spec *chan,
  318. int *val, int *val2, long mask)
  319. {
  320. struct sx9500_data *data = iio_priv(indio_dev);
  321. switch (chan->type) {
  322. case IIO_PROXIMITY:
  323. switch (mask) {
  324. case IIO_CHAN_INFO_RAW:
  325. if (iio_buffer_enabled(indio_dev))
  326. return -EBUSY;
  327. return sx9500_read_proximity(data, chan, val);
  328. case IIO_CHAN_INFO_SAMP_FREQ:
  329. return sx9500_read_samp_freq(data, val, val2);
  330. default:
  331. return -EINVAL;
  332. }
  333. default:
  334. return -EINVAL;
  335. }
  336. }
  337. static int sx9500_set_samp_freq(struct sx9500_data *data,
  338. int val, int val2)
  339. {
  340. int i, ret;
  341. for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++)
  342. if (val == sx9500_samp_freq_table[i].val &&
  343. val2 == sx9500_samp_freq_table[i].val2)
  344. break;
  345. if (i == ARRAY_SIZE(sx9500_samp_freq_table))
  346. return -EINVAL;
  347. mutex_lock(&data->mutex);
  348. ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
  349. SX9500_SCAN_PERIOD_MASK,
  350. i << SX9500_SCAN_PERIOD_SHIFT);
  351. mutex_unlock(&data->mutex);
  352. return ret;
  353. }
  354. static int sx9500_write_raw(struct iio_dev *indio_dev,
  355. const struct iio_chan_spec *chan,
  356. int val, int val2, long mask)
  357. {
  358. struct sx9500_data *data = iio_priv(indio_dev);
  359. switch (chan->type) {
  360. case IIO_PROXIMITY:
  361. switch (mask) {
  362. case IIO_CHAN_INFO_SAMP_FREQ:
  363. return sx9500_set_samp_freq(data, val, val2);
  364. default:
  365. return -EINVAL;
  366. }
  367. default:
  368. return -EINVAL;
  369. }
  370. }
  371. static irqreturn_t sx9500_irq_handler(int irq, void *private)
  372. {
  373. struct iio_dev *indio_dev = private;
  374. struct sx9500_data *data = iio_priv(indio_dev);
  375. if (data->trigger_enabled)
  376. iio_trigger_poll(data->trig);
  377. /*
  378. * Even if no event is enabled, we need to wake the thread to
  379. * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It
  380. * is not possible to do that here because regmap_read takes a
  381. * mutex.
  382. */
  383. return IRQ_WAKE_THREAD;
  384. }
  385. static void sx9500_push_events(struct iio_dev *indio_dev)
  386. {
  387. int ret;
  388. unsigned int val, chan;
  389. struct sx9500_data *data = iio_priv(indio_dev);
  390. ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
  391. if (ret < 0) {
  392. dev_err(&data->client->dev, "i2c transfer error in irq\n");
  393. return;
  394. }
  395. val >>= SX9500_PROXSTAT_SHIFT;
  396. for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) {
  397. int dir;
  398. u64 ev;
  399. bool new_prox = val & BIT(chan);
  400. if (!data->event_enabled[chan])
  401. continue;
  402. if (new_prox == data->prox_stat[chan])
  403. /* No change on this channel. */
  404. continue;
  405. dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
  406. ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
  407. IIO_EV_TYPE_THRESH, dir);
  408. iio_push_event(indio_dev, ev, iio_get_time_ns());
  409. data->prox_stat[chan] = new_prox;
  410. }
  411. }
  412. static irqreturn_t sx9500_irq_thread_handler(int irq, void *private)
  413. {
  414. struct iio_dev *indio_dev = private;
  415. struct sx9500_data *data = iio_priv(indio_dev);
  416. int ret;
  417. unsigned int val;
  418. mutex_lock(&data->mutex);
  419. ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
  420. if (ret < 0) {
  421. dev_err(&data->client->dev, "i2c transfer error in irq\n");
  422. goto out;
  423. }
  424. if (val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ))
  425. sx9500_push_events(indio_dev);
  426. if (val & SX9500_CONVDONE_IRQ)
  427. complete_all(&data->completion);
  428. out:
  429. mutex_unlock(&data->mutex);
  430. return IRQ_HANDLED;
  431. }
  432. static int sx9500_read_event_config(struct iio_dev *indio_dev,
  433. const struct iio_chan_spec *chan,
  434. enum iio_event_type type,
  435. enum iio_event_direction dir)
  436. {
  437. struct sx9500_data *data = iio_priv(indio_dev);
  438. if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
  439. dir != IIO_EV_DIR_EITHER)
  440. return -EINVAL;
  441. return data->event_enabled[chan->channel];
  442. }
  443. static int sx9500_write_event_config(struct iio_dev *indio_dev,
  444. const struct iio_chan_spec *chan,
  445. enum iio_event_type type,
  446. enum iio_event_direction dir,
  447. int state)
  448. {
  449. struct sx9500_data *data = iio_priv(indio_dev);
  450. int ret;
  451. if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
  452. dir != IIO_EV_DIR_EITHER)
  453. return -EINVAL;
  454. mutex_lock(&data->mutex);
  455. if (state == 1) {
  456. ret = sx9500_inc_chan_users(data, chan->channel);
  457. if (ret < 0)
  458. goto out_unlock;
  459. ret = sx9500_inc_close_far_users(data);
  460. if (ret < 0)
  461. goto out_undo_chan;
  462. } else {
  463. ret = sx9500_dec_chan_users(data, chan->channel);
  464. if (ret < 0)
  465. goto out_unlock;
  466. ret = sx9500_dec_close_far_users(data);
  467. if (ret < 0)
  468. goto out_undo_chan;
  469. }
  470. data->event_enabled[chan->channel] = state;
  471. goto out_unlock;
  472. out_undo_chan:
  473. if (state == 1)
  474. sx9500_dec_chan_users(data, chan->channel);
  475. else
  476. sx9500_inc_chan_users(data, chan->channel);
  477. out_unlock:
  478. mutex_unlock(&data->mutex);
  479. return ret;
  480. }
  481. static int sx9500_update_scan_mode(struct iio_dev *indio_dev,
  482. const unsigned long *scan_mask)
  483. {
  484. struct sx9500_data *data = iio_priv(indio_dev);
  485. mutex_lock(&data->mutex);
  486. kfree(data->buffer);
  487. data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
  488. mutex_unlock(&data->mutex);
  489. if (data->buffer == NULL)
  490. return -ENOMEM;
  491. return 0;
  492. }
  493. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
  494. "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
  495. static struct attribute *sx9500_attributes[] = {
  496. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  497. NULL,
  498. };
  499. static const struct attribute_group sx9500_attribute_group = {
  500. .attrs = sx9500_attributes,
  501. };
  502. static const struct iio_info sx9500_info = {
  503. .driver_module = THIS_MODULE,
  504. .attrs = &sx9500_attribute_group,
  505. .read_raw = &sx9500_read_raw,
  506. .write_raw = &sx9500_write_raw,
  507. .read_event_config = &sx9500_read_event_config,
  508. .write_event_config = &sx9500_write_event_config,
  509. .update_scan_mode = &sx9500_update_scan_mode,
  510. };
  511. static int sx9500_set_trigger_state(struct iio_trigger *trig,
  512. bool state)
  513. {
  514. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  515. struct sx9500_data *data = iio_priv(indio_dev);
  516. int ret;
  517. mutex_lock(&data->mutex);
  518. if (state)
  519. ret = sx9500_inc_data_rdy_users(data);
  520. else
  521. ret = sx9500_dec_data_rdy_users(data);
  522. if (ret < 0)
  523. goto out;
  524. data->trigger_enabled = state;
  525. out:
  526. mutex_unlock(&data->mutex);
  527. return ret;
  528. }
  529. static const struct iio_trigger_ops sx9500_trigger_ops = {
  530. .set_trigger_state = sx9500_set_trigger_state,
  531. .owner = THIS_MODULE,
  532. };
  533. static irqreturn_t sx9500_trigger_handler(int irq, void *private)
  534. {
  535. struct iio_poll_func *pf = private;
  536. struct iio_dev *indio_dev = pf->indio_dev;
  537. struct sx9500_data *data = iio_priv(indio_dev);
  538. int val, bit, ret, i = 0;
  539. mutex_lock(&data->mutex);
  540. for_each_set_bit(bit, indio_dev->active_scan_mask,
  541. indio_dev->masklength) {
  542. ret = sx9500_read_prox_data(data, &indio_dev->channels[bit],
  543. &val);
  544. if (ret < 0)
  545. goto out;
  546. data->buffer[i++] = val;
  547. }
  548. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  549. iio_get_time_ns());
  550. out:
  551. mutex_unlock(&data->mutex);
  552. iio_trigger_notify_done(indio_dev->trig);
  553. return IRQ_HANDLED;
  554. }
  555. static int sx9500_buffer_preenable(struct iio_dev *indio_dev)
  556. {
  557. struct sx9500_data *data = iio_priv(indio_dev);
  558. int ret = 0, i;
  559. mutex_lock(&data->mutex);
  560. for (i = 0; i < SX9500_NUM_CHANNELS; i++)
  561. if (test_bit(i, indio_dev->active_scan_mask)) {
  562. ret = sx9500_inc_chan_users(data, i);
  563. if (ret)
  564. break;
  565. }
  566. if (ret)
  567. for (i = i - 1; i >= 0; i--)
  568. if (test_bit(i, indio_dev->active_scan_mask))
  569. sx9500_dec_chan_users(data, i);
  570. mutex_unlock(&data->mutex);
  571. return ret;
  572. }
  573. static int sx9500_buffer_predisable(struct iio_dev *indio_dev)
  574. {
  575. struct sx9500_data *data = iio_priv(indio_dev);
  576. int ret = 0, i;
  577. iio_triggered_buffer_predisable(indio_dev);
  578. mutex_lock(&data->mutex);
  579. for (i = 0; i < SX9500_NUM_CHANNELS; i++)
  580. if (test_bit(i, indio_dev->active_scan_mask)) {
  581. ret = sx9500_dec_chan_users(data, i);
  582. if (ret)
  583. break;
  584. }
  585. if (ret)
  586. for (i = i - 1; i >= 0; i--)
  587. if (test_bit(i, indio_dev->active_scan_mask))
  588. sx9500_inc_chan_users(data, i);
  589. mutex_unlock(&data->mutex);
  590. return ret;
  591. }
  592. static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops = {
  593. .preenable = sx9500_buffer_preenable,
  594. .postenable = iio_triggered_buffer_postenable,
  595. .predisable = sx9500_buffer_predisable,
  596. };
  597. struct sx9500_reg_default {
  598. u8 reg;
  599. u8 def;
  600. };
  601. static const struct sx9500_reg_default sx9500_default_regs[] = {
  602. {
  603. .reg = SX9500_REG_PROX_CTRL1,
  604. /* Shield enabled, small range. */
  605. .def = 0x43,
  606. },
  607. {
  608. .reg = SX9500_REG_PROX_CTRL2,
  609. /* x8 gain, 167kHz frequency, finest resolution. */
  610. .def = 0x77,
  611. },
  612. {
  613. .reg = SX9500_REG_PROX_CTRL3,
  614. /* Doze enabled, 2x scan period doze, no raw filter. */
  615. .def = 0x40,
  616. },
  617. {
  618. .reg = SX9500_REG_PROX_CTRL4,
  619. /* Average threshold. */
  620. .def = 0x30,
  621. },
  622. {
  623. .reg = SX9500_REG_PROX_CTRL5,
  624. /*
  625. * Debouncer off, lowest average negative filter,
  626. * highest average postive filter.
  627. */
  628. .def = 0x0f,
  629. },
  630. {
  631. .reg = SX9500_REG_PROX_CTRL6,
  632. /* Proximity detection threshold: 280 */
  633. .def = 0x0e,
  634. },
  635. {
  636. .reg = SX9500_REG_PROX_CTRL7,
  637. /*
  638. * No automatic compensation, compensate each pin
  639. * independently, proximity hysteresis: 32, close
  640. * debouncer off, far debouncer off.
  641. */
  642. .def = 0x00,
  643. },
  644. {
  645. .reg = SX9500_REG_PROX_CTRL8,
  646. /* No stuck timeout, no periodic compensation. */
  647. .def = 0x00,
  648. },
  649. {
  650. .reg = SX9500_REG_PROX_CTRL0,
  651. /* Scan period: 30ms, all sensors disabled. */
  652. .def = 0x00,
  653. },
  654. };
  655. /* Activate all channels and perform an initial compensation. */
  656. static int sx9500_init_compensation(struct iio_dev *indio_dev)
  657. {
  658. struct sx9500_data *data = iio_priv(indio_dev);
  659. int i, ret;
  660. unsigned int val;
  661. ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
  662. SX9500_CHAN_MASK, SX9500_CHAN_MASK);
  663. if (ret < 0)
  664. return ret;
  665. for (i = 10; i >= 0; i--) {
  666. usleep_range(10000, 20000);
  667. ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
  668. if (ret < 0)
  669. goto out;
  670. if (!(val & SX9500_COMPSTAT_MASK))
  671. break;
  672. }
  673. if (i < 0) {
  674. dev_err(&data->client->dev, "initial compensation timed out");
  675. ret = -ETIMEDOUT;
  676. }
  677. out:
  678. regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
  679. SX9500_CHAN_MASK, 0);
  680. return ret;
  681. }
  682. static int sx9500_init_device(struct iio_dev *indio_dev)
  683. {
  684. struct sx9500_data *data = iio_priv(indio_dev);
  685. int ret, i;
  686. unsigned int val;
  687. if (data->gpiod_rst) {
  688. gpiod_set_value_cansleep(data->gpiod_rst, 0);
  689. usleep_range(1000, 2000);
  690. gpiod_set_value_cansleep(data->gpiod_rst, 1);
  691. usleep_range(1000, 2000);
  692. }
  693. ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0);
  694. if (ret < 0)
  695. return ret;
  696. ret = regmap_write(data->regmap, SX9500_REG_RESET,
  697. SX9500_SOFT_RESET);
  698. if (ret < 0)
  699. return ret;
  700. ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
  701. if (ret < 0)
  702. return ret;
  703. for (i = 0; i < ARRAY_SIZE(sx9500_default_regs); i++) {
  704. ret = regmap_write(data->regmap,
  705. sx9500_default_regs[i].reg,
  706. sx9500_default_regs[i].def);
  707. if (ret < 0)
  708. return ret;
  709. }
  710. return sx9500_init_compensation(indio_dev);
  711. }
  712. static void sx9500_gpio_probe(struct i2c_client *client,
  713. struct sx9500_data *data)
  714. {
  715. struct device *dev;
  716. if (!client)
  717. return;
  718. dev = &client->dev;
  719. data->gpiod_rst = devm_gpiod_get_index(dev, SX9500_GPIO_RESET,
  720. 0, GPIOD_OUT_HIGH);
  721. if (IS_ERR(data->gpiod_rst)) {
  722. dev_warn(dev, "gpio get reset pin failed\n");
  723. data->gpiod_rst = NULL;
  724. }
  725. }
  726. static int sx9500_probe(struct i2c_client *client,
  727. const struct i2c_device_id *id)
  728. {
  729. int ret;
  730. struct iio_dev *indio_dev;
  731. struct sx9500_data *data;
  732. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  733. if (indio_dev == NULL)
  734. return -ENOMEM;
  735. data = iio_priv(indio_dev);
  736. data->client = client;
  737. mutex_init(&data->mutex);
  738. init_completion(&data->completion);
  739. data->trigger_enabled = false;
  740. data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config);
  741. if (IS_ERR(data->regmap))
  742. return PTR_ERR(data->regmap);
  743. indio_dev->dev.parent = &client->dev;
  744. indio_dev->name = SX9500_DRIVER_NAME;
  745. indio_dev->channels = sx9500_channels;
  746. indio_dev->num_channels = ARRAY_SIZE(sx9500_channels);
  747. indio_dev->info = &sx9500_info;
  748. indio_dev->modes = INDIO_DIRECT_MODE;
  749. i2c_set_clientdata(client, indio_dev);
  750. sx9500_gpio_probe(client, data);
  751. ret = sx9500_init_device(indio_dev);
  752. if (ret < 0)
  753. return ret;
  754. if (client->irq <= 0)
  755. dev_warn(&client->dev, "no valid irq found\n");
  756. else {
  757. ret = devm_request_threaded_irq(&client->dev, client->irq,
  758. sx9500_irq_handler, sx9500_irq_thread_handler,
  759. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  760. SX9500_IRQ_NAME, indio_dev);
  761. if (ret < 0)
  762. return ret;
  763. data->trig = devm_iio_trigger_alloc(&client->dev,
  764. "%s-dev%d", indio_dev->name, indio_dev->id);
  765. if (!data->trig)
  766. return -ENOMEM;
  767. data->trig->dev.parent = &client->dev;
  768. data->trig->ops = &sx9500_trigger_ops;
  769. iio_trigger_set_drvdata(data->trig, indio_dev);
  770. ret = iio_trigger_register(data->trig);
  771. if (ret)
  772. return ret;
  773. }
  774. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  775. sx9500_trigger_handler,
  776. &sx9500_buffer_setup_ops);
  777. if (ret < 0)
  778. goto out_trigger_unregister;
  779. ret = iio_device_register(indio_dev);
  780. if (ret < 0)
  781. goto out_buffer_cleanup;
  782. return 0;
  783. out_buffer_cleanup:
  784. iio_triggered_buffer_cleanup(indio_dev);
  785. out_trigger_unregister:
  786. if (client->irq > 0)
  787. iio_trigger_unregister(data->trig);
  788. return ret;
  789. }
  790. static int sx9500_remove(struct i2c_client *client)
  791. {
  792. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  793. struct sx9500_data *data = iio_priv(indio_dev);
  794. iio_device_unregister(indio_dev);
  795. iio_triggered_buffer_cleanup(indio_dev);
  796. if (client->irq > 0)
  797. iio_trigger_unregister(data->trig);
  798. kfree(data->buffer);
  799. return 0;
  800. }
  801. #ifdef CONFIG_PM_SLEEP
  802. static int sx9500_suspend(struct device *dev)
  803. {
  804. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  805. struct sx9500_data *data = iio_priv(indio_dev);
  806. int ret;
  807. mutex_lock(&data->mutex);
  808. ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0,
  809. &data->suspend_ctrl0);
  810. if (ret < 0)
  811. goto out;
  812. /*
  813. * Scan period doesn't matter because when all the sensors are
  814. * deactivated the device is in sleep mode.
  815. */
  816. ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0);
  817. out:
  818. mutex_unlock(&data->mutex);
  819. return ret;
  820. }
  821. static int sx9500_resume(struct device *dev)
  822. {
  823. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  824. struct sx9500_data *data = iio_priv(indio_dev);
  825. int ret;
  826. mutex_lock(&data->mutex);
  827. ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0,
  828. data->suspend_ctrl0);
  829. mutex_unlock(&data->mutex);
  830. return ret;
  831. }
  832. #endif /* CONFIG_PM_SLEEP */
  833. static const struct dev_pm_ops sx9500_pm_ops = {
  834. SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend, sx9500_resume)
  835. };
  836. static const struct acpi_device_id sx9500_acpi_match[] = {
  837. {"SSX9500", 0},
  838. { },
  839. };
  840. MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match);
  841. static const struct i2c_device_id sx9500_id[] = {
  842. {"sx9500", 0},
  843. { },
  844. };
  845. MODULE_DEVICE_TABLE(i2c, sx9500_id);
  846. static struct i2c_driver sx9500_driver = {
  847. .driver = {
  848. .name = SX9500_DRIVER_NAME,
  849. .acpi_match_table = ACPI_PTR(sx9500_acpi_match),
  850. .pm = &sx9500_pm_ops,
  851. },
  852. .probe = sx9500_probe,
  853. .remove = sx9500_remove,
  854. .id_table = sx9500_id,
  855. };
  856. module_i2c_driver(sx9500_driver);
  857. MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
  858. MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
  859. MODULE_LICENSE("GPL v2");