sx9500.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. int ret;
  322. switch (chan->type) {
  323. case IIO_PROXIMITY:
  324. switch (mask) {
  325. case IIO_CHAN_INFO_RAW:
  326. ret = iio_device_claim_direct_mode(indio_dev);
  327. if (ret)
  328. return ret;
  329. ret = sx9500_read_proximity(data, chan, val);
  330. iio_device_release_direct_mode(indio_dev);
  331. return ret;
  332. case IIO_CHAN_INFO_SAMP_FREQ:
  333. return sx9500_read_samp_freq(data, val, val2);
  334. default:
  335. return -EINVAL;
  336. }
  337. default:
  338. return -EINVAL;
  339. }
  340. }
  341. static int sx9500_set_samp_freq(struct sx9500_data *data,
  342. int val, int val2)
  343. {
  344. int i, ret;
  345. for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++)
  346. if (val == sx9500_samp_freq_table[i].val &&
  347. val2 == sx9500_samp_freq_table[i].val2)
  348. break;
  349. if (i == ARRAY_SIZE(sx9500_samp_freq_table))
  350. return -EINVAL;
  351. mutex_lock(&data->mutex);
  352. ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
  353. SX9500_SCAN_PERIOD_MASK,
  354. i << SX9500_SCAN_PERIOD_SHIFT);
  355. mutex_unlock(&data->mutex);
  356. return ret;
  357. }
  358. static int sx9500_write_raw(struct iio_dev *indio_dev,
  359. const struct iio_chan_spec *chan,
  360. int val, int val2, long mask)
  361. {
  362. struct sx9500_data *data = iio_priv(indio_dev);
  363. switch (chan->type) {
  364. case IIO_PROXIMITY:
  365. switch (mask) {
  366. case IIO_CHAN_INFO_SAMP_FREQ:
  367. return sx9500_set_samp_freq(data, val, val2);
  368. default:
  369. return -EINVAL;
  370. }
  371. default:
  372. return -EINVAL;
  373. }
  374. }
  375. static irqreturn_t sx9500_irq_handler(int irq, void *private)
  376. {
  377. struct iio_dev *indio_dev = private;
  378. struct sx9500_data *data = iio_priv(indio_dev);
  379. if (data->trigger_enabled)
  380. iio_trigger_poll(data->trig);
  381. /*
  382. * Even if no event is enabled, we need to wake the thread to
  383. * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It
  384. * is not possible to do that here because regmap_read takes a
  385. * mutex.
  386. */
  387. return IRQ_WAKE_THREAD;
  388. }
  389. static void sx9500_push_events(struct iio_dev *indio_dev)
  390. {
  391. int ret;
  392. unsigned int val, chan;
  393. struct sx9500_data *data = iio_priv(indio_dev);
  394. ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
  395. if (ret < 0) {
  396. dev_err(&data->client->dev, "i2c transfer error in irq\n");
  397. return;
  398. }
  399. val >>= SX9500_PROXSTAT_SHIFT;
  400. for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) {
  401. int dir;
  402. u64 ev;
  403. bool new_prox = val & BIT(chan);
  404. if (!data->event_enabled[chan])
  405. continue;
  406. if (new_prox == data->prox_stat[chan])
  407. /* No change on this channel. */
  408. continue;
  409. dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
  410. ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
  411. IIO_EV_TYPE_THRESH, dir);
  412. iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
  413. data->prox_stat[chan] = new_prox;
  414. }
  415. }
  416. static irqreturn_t sx9500_irq_thread_handler(int irq, void *private)
  417. {
  418. struct iio_dev *indio_dev = private;
  419. struct sx9500_data *data = iio_priv(indio_dev);
  420. int ret;
  421. unsigned int val;
  422. mutex_lock(&data->mutex);
  423. ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
  424. if (ret < 0) {
  425. dev_err(&data->client->dev, "i2c transfer error in irq\n");
  426. goto out;
  427. }
  428. if (val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ))
  429. sx9500_push_events(indio_dev);
  430. if (val & SX9500_CONVDONE_IRQ)
  431. complete(&data->completion);
  432. out:
  433. mutex_unlock(&data->mutex);
  434. return IRQ_HANDLED;
  435. }
  436. static int sx9500_read_event_config(struct iio_dev *indio_dev,
  437. const struct iio_chan_spec *chan,
  438. enum iio_event_type type,
  439. enum iio_event_direction dir)
  440. {
  441. struct sx9500_data *data = iio_priv(indio_dev);
  442. if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
  443. dir != IIO_EV_DIR_EITHER)
  444. return -EINVAL;
  445. return data->event_enabled[chan->channel];
  446. }
  447. static int sx9500_write_event_config(struct iio_dev *indio_dev,
  448. const struct iio_chan_spec *chan,
  449. enum iio_event_type type,
  450. enum iio_event_direction dir,
  451. int state)
  452. {
  453. struct sx9500_data *data = iio_priv(indio_dev);
  454. int ret;
  455. if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
  456. dir != IIO_EV_DIR_EITHER)
  457. return -EINVAL;
  458. mutex_lock(&data->mutex);
  459. if (state == 1) {
  460. ret = sx9500_inc_chan_users(data, chan->channel);
  461. if (ret < 0)
  462. goto out_unlock;
  463. ret = sx9500_inc_close_far_users(data);
  464. if (ret < 0)
  465. goto out_undo_chan;
  466. } else {
  467. ret = sx9500_dec_chan_users(data, chan->channel);
  468. if (ret < 0)
  469. goto out_unlock;
  470. ret = sx9500_dec_close_far_users(data);
  471. if (ret < 0)
  472. goto out_undo_chan;
  473. }
  474. data->event_enabled[chan->channel] = state;
  475. goto out_unlock;
  476. out_undo_chan:
  477. if (state == 1)
  478. sx9500_dec_chan_users(data, chan->channel);
  479. else
  480. sx9500_inc_chan_users(data, chan->channel);
  481. out_unlock:
  482. mutex_unlock(&data->mutex);
  483. return ret;
  484. }
  485. static int sx9500_update_scan_mode(struct iio_dev *indio_dev,
  486. const unsigned long *scan_mask)
  487. {
  488. struct sx9500_data *data = iio_priv(indio_dev);
  489. mutex_lock(&data->mutex);
  490. kfree(data->buffer);
  491. data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
  492. mutex_unlock(&data->mutex);
  493. if (data->buffer == NULL)
  494. return -ENOMEM;
  495. return 0;
  496. }
  497. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
  498. "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
  499. static struct attribute *sx9500_attributes[] = {
  500. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  501. NULL,
  502. };
  503. static const struct attribute_group sx9500_attribute_group = {
  504. .attrs = sx9500_attributes,
  505. };
  506. static const struct iio_info sx9500_info = {
  507. .driver_module = THIS_MODULE,
  508. .attrs = &sx9500_attribute_group,
  509. .read_raw = &sx9500_read_raw,
  510. .write_raw = &sx9500_write_raw,
  511. .read_event_config = &sx9500_read_event_config,
  512. .write_event_config = &sx9500_write_event_config,
  513. .update_scan_mode = &sx9500_update_scan_mode,
  514. };
  515. static int sx9500_set_trigger_state(struct iio_trigger *trig,
  516. bool state)
  517. {
  518. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  519. struct sx9500_data *data = iio_priv(indio_dev);
  520. int ret;
  521. mutex_lock(&data->mutex);
  522. if (state)
  523. ret = sx9500_inc_data_rdy_users(data);
  524. else
  525. ret = sx9500_dec_data_rdy_users(data);
  526. if (ret < 0)
  527. goto out;
  528. data->trigger_enabled = state;
  529. out:
  530. mutex_unlock(&data->mutex);
  531. return ret;
  532. }
  533. static const struct iio_trigger_ops sx9500_trigger_ops = {
  534. .set_trigger_state = sx9500_set_trigger_state,
  535. .owner = THIS_MODULE,
  536. };
  537. static irqreturn_t sx9500_trigger_handler(int irq, void *private)
  538. {
  539. struct iio_poll_func *pf = private;
  540. struct iio_dev *indio_dev = pf->indio_dev;
  541. struct sx9500_data *data = iio_priv(indio_dev);
  542. int val, bit, ret, i = 0;
  543. mutex_lock(&data->mutex);
  544. for_each_set_bit(bit, indio_dev->active_scan_mask,
  545. indio_dev->masklength) {
  546. ret = sx9500_read_prox_data(data, &indio_dev->channels[bit],
  547. &val);
  548. if (ret < 0)
  549. goto out;
  550. data->buffer[i++] = val;
  551. }
  552. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  553. iio_get_time_ns(indio_dev));
  554. out:
  555. mutex_unlock(&data->mutex);
  556. iio_trigger_notify_done(indio_dev->trig);
  557. return IRQ_HANDLED;
  558. }
  559. static int sx9500_buffer_preenable(struct iio_dev *indio_dev)
  560. {
  561. struct sx9500_data *data = iio_priv(indio_dev);
  562. int ret = 0, i;
  563. mutex_lock(&data->mutex);
  564. for (i = 0; i < SX9500_NUM_CHANNELS; i++)
  565. if (test_bit(i, indio_dev->active_scan_mask)) {
  566. ret = sx9500_inc_chan_users(data, i);
  567. if (ret)
  568. break;
  569. }
  570. if (ret)
  571. for (i = i - 1; i >= 0; i--)
  572. if (test_bit(i, indio_dev->active_scan_mask))
  573. sx9500_dec_chan_users(data, i);
  574. mutex_unlock(&data->mutex);
  575. return ret;
  576. }
  577. static int sx9500_buffer_predisable(struct iio_dev *indio_dev)
  578. {
  579. struct sx9500_data *data = iio_priv(indio_dev);
  580. int ret = 0, i;
  581. iio_triggered_buffer_predisable(indio_dev);
  582. mutex_lock(&data->mutex);
  583. for (i = 0; i < SX9500_NUM_CHANNELS; i++)
  584. if (test_bit(i, indio_dev->active_scan_mask)) {
  585. ret = sx9500_dec_chan_users(data, i);
  586. if (ret)
  587. break;
  588. }
  589. if (ret)
  590. for (i = i - 1; i >= 0; i--)
  591. if (test_bit(i, indio_dev->active_scan_mask))
  592. sx9500_inc_chan_users(data, i);
  593. mutex_unlock(&data->mutex);
  594. return ret;
  595. }
  596. static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops = {
  597. .preenable = sx9500_buffer_preenable,
  598. .postenable = iio_triggered_buffer_postenable,
  599. .predisable = sx9500_buffer_predisable,
  600. };
  601. struct sx9500_reg_default {
  602. u8 reg;
  603. u8 def;
  604. };
  605. static const struct sx9500_reg_default sx9500_default_regs[] = {
  606. {
  607. .reg = SX9500_REG_PROX_CTRL1,
  608. /* Shield enabled, small range. */
  609. .def = 0x43,
  610. },
  611. {
  612. .reg = SX9500_REG_PROX_CTRL2,
  613. /* x8 gain, 167kHz frequency, finest resolution. */
  614. .def = 0x77,
  615. },
  616. {
  617. .reg = SX9500_REG_PROX_CTRL3,
  618. /* Doze enabled, 2x scan period doze, no raw filter. */
  619. .def = 0x40,
  620. },
  621. {
  622. .reg = SX9500_REG_PROX_CTRL4,
  623. /* Average threshold. */
  624. .def = 0x30,
  625. },
  626. {
  627. .reg = SX9500_REG_PROX_CTRL5,
  628. /*
  629. * Debouncer off, lowest average negative filter,
  630. * highest average postive filter.
  631. */
  632. .def = 0x0f,
  633. },
  634. {
  635. .reg = SX9500_REG_PROX_CTRL6,
  636. /* Proximity detection threshold: 280 */
  637. .def = 0x0e,
  638. },
  639. {
  640. .reg = SX9500_REG_PROX_CTRL7,
  641. /*
  642. * No automatic compensation, compensate each pin
  643. * independently, proximity hysteresis: 32, close
  644. * debouncer off, far debouncer off.
  645. */
  646. .def = 0x00,
  647. },
  648. {
  649. .reg = SX9500_REG_PROX_CTRL8,
  650. /* No stuck timeout, no periodic compensation. */
  651. .def = 0x00,
  652. },
  653. {
  654. .reg = SX9500_REG_PROX_CTRL0,
  655. /* Scan period: 30ms, all sensors disabled. */
  656. .def = 0x00,
  657. },
  658. };
  659. /* Activate all channels and perform an initial compensation. */
  660. static int sx9500_init_compensation(struct iio_dev *indio_dev)
  661. {
  662. struct sx9500_data *data = iio_priv(indio_dev);
  663. int i, ret;
  664. unsigned int val;
  665. ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
  666. SX9500_CHAN_MASK, SX9500_CHAN_MASK);
  667. if (ret < 0)
  668. return ret;
  669. for (i = 10; i >= 0; i--) {
  670. usleep_range(10000, 20000);
  671. ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
  672. if (ret < 0)
  673. goto out;
  674. if (!(val & SX9500_COMPSTAT_MASK))
  675. break;
  676. }
  677. if (i < 0) {
  678. dev_err(&data->client->dev, "initial compensation timed out");
  679. ret = -ETIMEDOUT;
  680. }
  681. out:
  682. regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
  683. SX9500_CHAN_MASK, 0);
  684. return ret;
  685. }
  686. static int sx9500_init_device(struct iio_dev *indio_dev)
  687. {
  688. struct sx9500_data *data = iio_priv(indio_dev);
  689. int ret, i;
  690. unsigned int val;
  691. if (data->gpiod_rst) {
  692. gpiod_set_value_cansleep(data->gpiod_rst, 0);
  693. usleep_range(1000, 2000);
  694. gpiod_set_value_cansleep(data->gpiod_rst, 1);
  695. usleep_range(1000, 2000);
  696. }
  697. ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0);
  698. if (ret < 0)
  699. return ret;
  700. ret = regmap_write(data->regmap, SX9500_REG_RESET,
  701. SX9500_SOFT_RESET);
  702. if (ret < 0)
  703. return ret;
  704. ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
  705. if (ret < 0)
  706. return ret;
  707. for (i = 0; i < ARRAY_SIZE(sx9500_default_regs); i++) {
  708. ret = regmap_write(data->regmap,
  709. sx9500_default_regs[i].reg,
  710. sx9500_default_regs[i].def);
  711. if (ret < 0)
  712. return ret;
  713. }
  714. return sx9500_init_compensation(indio_dev);
  715. }
  716. static void sx9500_gpio_probe(struct i2c_client *client,
  717. struct sx9500_data *data)
  718. {
  719. struct device *dev;
  720. if (!client)
  721. return;
  722. dev = &client->dev;
  723. data->gpiod_rst = devm_gpiod_get_index(dev, SX9500_GPIO_RESET,
  724. 0, GPIOD_OUT_HIGH);
  725. if (IS_ERR(data->gpiod_rst)) {
  726. dev_warn(dev, "gpio get reset pin failed\n");
  727. data->gpiod_rst = NULL;
  728. }
  729. }
  730. static int sx9500_probe(struct i2c_client *client,
  731. const struct i2c_device_id *id)
  732. {
  733. int ret;
  734. struct iio_dev *indio_dev;
  735. struct sx9500_data *data;
  736. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  737. if (indio_dev == NULL)
  738. return -ENOMEM;
  739. data = iio_priv(indio_dev);
  740. data->client = client;
  741. mutex_init(&data->mutex);
  742. init_completion(&data->completion);
  743. data->trigger_enabled = false;
  744. data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config);
  745. if (IS_ERR(data->regmap))
  746. return PTR_ERR(data->regmap);
  747. indio_dev->dev.parent = &client->dev;
  748. indio_dev->name = SX9500_DRIVER_NAME;
  749. indio_dev->channels = sx9500_channels;
  750. indio_dev->num_channels = ARRAY_SIZE(sx9500_channels);
  751. indio_dev->info = &sx9500_info;
  752. indio_dev->modes = INDIO_DIRECT_MODE;
  753. i2c_set_clientdata(client, indio_dev);
  754. sx9500_gpio_probe(client, data);
  755. ret = sx9500_init_device(indio_dev);
  756. if (ret < 0)
  757. return ret;
  758. if (client->irq <= 0)
  759. dev_warn(&client->dev, "no valid irq found\n");
  760. else {
  761. ret = devm_request_threaded_irq(&client->dev, client->irq,
  762. sx9500_irq_handler, sx9500_irq_thread_handler,
  763. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  764. SX9500_IRQ_NAME, indio_dev);
  765. if (ret < 0)
  766. return ret;
  767. data->trig = devm_iio_trigger_alloc(&client->dev,
  768. "%s-dev%d", indio_dev->name, indio_dev->id);
  769. if (!data->trig)
  770. return -ENOMEM;
  771. data->trig->dev.parent = &client->dev;
  772. data->trig->ops = &sx9500_trigger_ops;
  773. iio_trigger_set_drvdata(data->trig, indio_dev);
  774. ret = iio_trigger_register(data->trig);
  775. if (ret)
  776. return ret;
  777. }
  778. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  779. sx9500_trigger_handler,
  780. &sx9500_buffer_setup_ops);
  781. if (ret < 0)
  782. goto out_trigger_unregister;
  783. ret = iio_device_register(indio_dev);
  784. if (ret < 0)
  785. goto out_buffer_cleanup;
  786. return 0;
  787. out_buffer_cleanup:
  788. iio_triggered_buffer_cleanup(indio_dev);
  789. out_trigger_unregister:
  790. if (client->irq > 0)
  791. iio_trigger_unregister(data->trig);
  792. return ret;
  793. }
  794. static int sx9500_remove(struct i2c_client *client)
  795. {
  796. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  797. struct sx9500_data *data = iio_priv(indio_dev);
  798. iio_device_unregister(indio_dev);
  799. iio_triggered_buffer_cleanup(indio_dev);
  800. if (client->irq > 0)
  801. iio_trigger_unregister(data->trig);
  802. kfree(data->buffer);
  803. return 0;
  804. }
  805. #ifdef CONFIG_PM_SLEEP
  806. static int sx9500_suspend(struct device *dev)
  807. {
  808. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  809. struct sx9500_data *data = iio_priv(indio_dev);
  810. int ret;
  811. mutex_lock(&data->mutex);
  812. ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0,
  813. &data->suspend_ctrl0);
  814. if (ret < 0)
  815. goto out;
  816. /*
  817. * Scan period doesn't matter because when all the sensors are
  818. * deactivated the device is in sleep mode.
  819. */
  820. ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0);
  821. out:
  822. mutex_unlock(&data->mutex);
  823. return ret;
  824. }
  825. static int sx9500_resume(struct device *dev)
  826. {
  827. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  828. struct sx9500_data *data = iio_priv(indio_dev);
  829. int ret;
  830. mutex_lock(&data->mutex);
  831. ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0,
  832. data->suspend_ctrl0);
  833. mutex_unlock(&data->mutex);
  834. return ret;
  835. }
  836. #endif /* CONFIG_PM_SLEEP */
  837. static const struct dev_pm_ops sx9500_pm_ops = {
  838. SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend, sx9500_resume)
  839. };
  840. static const struct acpi_device_id sx9500_acpi_match[] = {
  841. {"SSX9500", 0},
  842. { },
  843. };
  844. MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match);
  845. static const struct of_device_id sx9500_of_match[] = {
  846. { .compatible = "semtech,sx9500", },
  847. { }
  848. };
  849. MODULE_DEVICE_TABLE(of, sx9500_of_match);
  850. static const struct i2c_device_id sx9500_id[] = {
  851. {"sx9500", 0},
  852. { },
  853. };
  854. MODULE_DEVICE_TABLE(i2c, sx9500_id);
  855. static struct i2c_driver sx9500_driver = {
  856. .driver = {
  857. .name = SX9500_DRIVER_NAME,
  858. .acpi_match_table = ACPI_PTR(sx9500_acpi_match),
  859. .of_match_table = of_match_ptr(sx9500_of_match),
  860. .pm = &sx9500_pm_ops,
  861. },
  862. .probe = sx9500_probe,
  863. .remove = sx9500_remove,
  864. .id_table = sx9500_id,
  865. };
  866. module_i2c_driver(sx9500_driver);
  867. MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
  868. MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
  869. MODULE_LICENSE("GPL v2");