sx9500.c 25 KB

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