sx9500.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. /* Register definitions. */
  32. #define SX9500_REG_IRQ_SRC 0x00
  33. #define SX9500_REG_STAT 0x01
  34. #define SX9500_REG_IRQ_MSK 0x03
  35. #define SX9500_REG_PROX_CTRL0 0x06
  36. #define SX9500_REG_PROX_CTRL1 0x07
  37. #define SX9500_REG_PROX_CTRL2 0x08
  38. #define SX9500_REG_PROX_CTRL3 0x09
  39. #define SX9500_REG_PROX_CTRL4 0x0a
  40. #define SX9500_REG_PROX_CTRL5 0x0b
  41. #define SX9500_REG_PROX_CTRL6 0x0c
  42. #define SX9500_REG_PROX_CTRL7 0x0d
  43. #define SX9500_REG_PROX_CTRL8 0x0e
  44. #define SX9500_REG_SENSOR_SEL 0x20
  45. #define SX9500_REG_USE_MSB 0x21
  46. #define SX9500_REG_USE_LSB 0x22
  47. #define SX9500_REG_AVG_MSB 0x23
  48. #define SX9500_REG_AVG_LSB 0x24
  49. #define SX9500_REG_DIFF_MSB 0x25
  50. #define SX9500_REG_DIFF_LSB 0x26
  51. #define SX9500_REG_OFFSET_MSB 0x27
  52. #define SX9500_REG_OFFSET_LSB 0x28
  53. #define SX9500_REG_RESET 0x7f
  54. /* Write this to REG_RESET to do a soft reset. */
  55. #define SX9500_SOFT_RESET 0xde
  56. #define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4)
  57. #define SX9500_SCAN_PERIOD_SHIFT 4
  58. /*
  59. * These serve for identifying IRQ source in the IRQ_SRC register, and
  60. * also for masking the IRQs in the IRQ_MSK register.
  61. */
  62. #define SX9500_CLOSE_IRQ BIT(6)
  63. #define SX9500_FAR_IRQ BIT(5)
  64. #define SX9500_CONVDONE_IRQ BIT(3)
  65. #define SX9500_PROXSTAT_SHIFT 4
  66. #define SX9500_COMPSTAT_MASK GENMASK(3, 0)
  67. #define SX9500_NUM_CHANNELS 4
  68. #define SX9500_CHAN_MASK GENMASK(SX9500_NUM_CHANNELS - 1, 0)
  69. struct sx9500_data {
  70. struct mutex mutex;
  71. struct i2c_client *client;
  72. struct iio_trigger *trig;
  73. struct regmap *regmap;
  74. struct gpio_desc *gpiod_rst;
  75. /*
  76. * Last reading of the proximity status for each channel. We
  77. * only send an event to user space when this changes.
  78. */
  79. bool prox_stat[SX9500_NUM_CHANNELS];
  80. bool event_enabled[SX9500_NUM_CHANNELS];
  81. bool trigger_enabled;
  82. u16 *buffer;
  83. /* Remember enabled channels and sample rate during suspend. */
  84. unsigned int suspend_ctrl0;
  85. struct completion completion;
  86. int data_rdy_users, close_far_users;
  87. int channel_users[SX9500_NUM_CHANNELS];
  88. };
  89. static const struct iio_event_spec sx9500_events[] = {
  90. {
  91. .type = IIO_EV_TYPE_THRESH,
  92. .dir = IIO_EV_DIR_EITHER,
  93. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  94. },
  95. };
  96. #define SX9500_CHANNEL(idx) \
  97. { \
  98. .type = IIO_PROXIMITY, \
  99. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  100. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  101. .indexed = 1, \
  102. .channel = idx, \
  103. .event_spec = sx9500_events, \
  104. .num_event_specs = ARRAY_SIZE(sx9500_events), \
  105. .scan_index = idx, \
  106. .scan_type = { \
  107. .sign = 'u', \
  108. .realbits = 16, \
  109. .storagebits = 16, \
  110. .shift = 0, \
  111. }, \
  112. }
  113. static const struct iio_chan_spec sx9500_channels[] = {
  114. SX9500_CHANNEL(0),
  115. SX9500_CHANNEL(1),
  116. SX9500_CHANNEL(2),
  117. SX9500_CHANNEL(3),
  118. IIO_CHAN_SOFT_TIMESTAMP(4),
  119. };
  120. static const struct {
  121. int val;
  122. int val2;
  123. } sx9500_samp_freq_table[] = {
  124. {33, 333333},
  125. {16, 666666},
  126. {11, 111111},
  127. {8, 333333},
  128. {6, 666666},
  129. {5, 0},
  130. {3, 333333},
  131. {2, 500000},
  132. };
  133. static const unsigned int sx9500_scan_period_table[] = {
  134. 30, 60, 90, 120, 150, 200, 300, 400,
  135. };
  136. static const struct regmap_range sx9500_writable_reg_ranges[] = {
  137. regmap_reg_range(SX9500_REG_IRQ_MSK, SX9500_REG_IRQ_MSK),
  138. regmap_reg_range(SX9500_REG_PROX_CTRL0, SX9500_REG_PROX_CTRL8),
  139. regmap_reg_range(SX9500_REG_SENSOR_SEL, SX9500_REG_SENSOR_SEL),
  140. regmap_reg_range(SX9500_REG_OFFSET_MSB, SX9500_REG_OFFSET_LSB),
  141. regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
  142. };
  143. static const struct regmap_access_table sx9500_writeable_regs = {
  144. .yes_ranges = sx9500_writable_reg_ranges,
  145. .n_yes_ranges = ARRAY_SIZE(sx9500_writable_reg_ranges),
  146. };
  147. /*
  148. * All allocated registers are readable, so we just list unallocated
  149. * ones.
  150. */
  151. static const struct regmap_range sx9500_non_readable_reg_ranges[] = {
  152. regmap_reg_range(SX9500_REG_STAT + 1, SX9500_REG_STAT + 1),
  153. regmap_reg_range(SX9500_REG_IRQ_MSK + 1, SX9500_REG_PROX_CTRL0 - 1),
  154. regmap_reg_range(SX9500_REG_PROX_CTRL8 + 1, SX9500_REG_SENSOR_SEL - 1),
  155. regmap_reg_range(SX9500_REG_OFFSET_LSB + 1, SX9500_REG_RESET - 1),
  156. };
  157. static const struct regmap_access_table sx9500_readable_regs = {
  158. .no_ranges = sx9500_non_readable_reg_ranges,
  159. .n_no_ranges = ARRAY_SIZE(sx9500_non_readable_reg_ranges),
  160. };
  161. static const struct regmap_range sx9500_volatile_reg_ranges[] = {
  162. regmap_reg_range(SX9500_REG_IRQ_SRC, SX9500_REG_STAT),
  163. regmap_reg_range(SX9500_REG_USE_MSB, SX9500_REG_OFFSET_LSB),
  164. regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
  165. };
  166. static const struct regmap_access_table sx9500_volatile_regs = {
  167. .yes_ranges = sx9500_volatile_reg_ranges,
  168. .n_yes_ranges = ARRAY_SIZE(sx9500_volatile_reg_ranges),
  169. };
  170. static const struct regmap_config sx9500_regmap_config = {
  171. .reg_bits = 8,
  172. .val_bits = 8,
  173. .max_register = SX9500_REG_RESET,
  174. .cache_type = REGCACHE_RBTREE,
  175. .wr_table = &sx9500_writeable_regs,
  176. .rd_table = &sx9500_readable_regs,
  177. .volatile_table = &sx9500_volatile_regs,
  178. };
  179. static int sx9500_inc_users(struct sx9500_data *data, int *counter,
  180. unsigned int reg, unsigned int bitmask)
  181. {
  182. (*counter)++;
  183. if (*counter != 1)
  184. /* Bit is already active, nothing to do. */
  185. return 0;
  186. return regmap_update_bits(data->regmap, reg, bitmask, bitmask);
  187. }
  188. static int sx9500_dec_users(struct sx9500_data *data, int *counter,
  189. unsigned int reg, unsigned int bitmask)
  190. {
  191. (*counter)--;
  192. if (*counter != 0)
  193. /* There are more users, do not deactivate. */
  194. return 0;
  195. return regmap_update_bits(data->regmap, reg, bitmask, 0);
  196. }
  197. static int sx9500_inc_chan_users(struct sx9500_data *data, int chan)
  198. {
  199. return sx9500_inc_users(data, &data->channel_users[chan],
  200. SX9500_REG_PROX_CTRL0, BIT(chan));
  201. }
  202. static int sx9500_dec_chan_users(struct sx9500_data *data, int chan)
  203. {
  204. return sx9500_dec_users(data, &data->channel_users[chan],
  205. SX9500_REG_PROX_CTRL0, BIT(chan));
  206. }
  207. static int sx9500_inc_data_rdy_users(struct sx9500_data *data)
  208. {
  209. return sx9500_inc_users(data, &data->data_rdy_users,
  210. SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ);
  211. }
  212. static int sx9500_dec_data_rdy_users(struct sx9500_data *data)
  213. {
  214. return sx9500_dec_users(data, &data->data_rdy_users,
  215. SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ);
  216. }
  217. static int sx9500_inc_close_far_users(struct sx9500_data *data)
  218. {
  219. return sx9500_inc_users(data, &data->close_far_users,
  220. SX9500_REG_IRQ_MSK,
  221. SX9500_CLOSE_IRQ | SX9500_FAR_IRQ);
  222. }
  223. static int sx9500_dec_close_far_users(struct sx9500_data *data)
  224. {
  225. return sx9500_dec_users(data, &data->close_far_users,
  226. SX9500_REG_IRQ_MSK,
  227. SX9500_CLOSE_IRQ | SX9500_FAR_IRQ);
  228. }
  229. static int sx9500_read_prox_data(struct sx9500_data *data,
  230. const struct iio_chan_spec *chan,
  231. int *val)
  232. {
  233. int ret;
  234. __be16 regval;
  235. ret = regmap_write(data->regmap, SX9500_REG_SENSOR_SEL, chan->channel);
  236. if (ret < 0)
  237. return ret;
  238. ret = regmap_bulk_read(data->regmap, SX9500_REG_USE_MSB, &regval, 2);
  239. if (ret < 0)
  240. return ret;
  241. *val = be16_to_cpu(regval);
  242. return IIO_VAL_INT;
  243. }
  244. /*
  245. * If we have no interrupt support, we have to wait for a scan period
  246. * after enabling a channel to get a result.
  247. */
  248. static int sx9500_wait_for_sample(struct sx9500_data *data)
  249. {
  250. int ret;
  251. unsigned int val;
  252. ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &val);
  253. if (ret < 0)
  254. return ret;
  255. val = (val & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT;
  256. msleep(sx9500_scan_period_table[val]);
  257. return 0;
  258. }
  259. static int sx9500_read_proximity(struct sx9500_data *data,
  260. const struct iio_chan_spec *chan,
  261. int *val)
  262. {
  263. int ret;
  264. mutex_lock(&data->mutex);
  265. ret = sx9500_inc_chan_users(data, chan->channel);
  266. if (ret < 0)
  267. goto out;
  268. ret = sx9500_inc_data_rdy_users(data);
  269. if (ret < 0)
  270. goto out_dec_chan;
  271. mutex_unlock(&data->mutex);
  272. if (data->client->irq > 0)
  273. ret = wait_for_completion_interruptible(&data->completion);
  274. else
  275. ret = sx9500_wait_for_sample(data);
  276. mutex_lock(&data->mutex);
  277. if (ret < 0)
  278. goto out_dec_data_rdy;
  279. ret = sx9500_read_prox_data(data, chan, val);
  280. if (ret < 0)
  281. goto out_dec_data_rdy;
  282. ret = sx9500_dec_data_rdy_users(data);
  283. if (ret < 0)
  284. goto out_dec_chan;
  285. ret = sx9500_dec_chan_users(data, chan->channel);
  286. if (ret < 0)
  287. goto out;
  288. ret = IIO_VAL_INT;
  289. goto out;
  290. out_dec_data_rdy:
  291. sx9500_dec_data_rdy_users(data);
  292. out_dec_chan:
  293. sx9500_dec_chan_users(data, chan->channel);
  294. out:
  295. mutex_unlock(&data->mutex);
  296. reinit_completion(&data->completion);
  297. return ret;
  298. }
  299. static int sx9500_read_samp_freq(struct sx9500_data *data,
  300. int *val, int *val2)
  301. {
  302. int ret;
  303. unsigned int regval;
  304. mutex_lock(&data->mutex);
  305. ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &regval);
  306. mutex_unlock(&data->mutex);
  307. if (ret < 0)
  308. return ret;
  309. regval = (regval & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT;
  310. *val = sx9500_samp_freq_table[regval].val;
  311. *val2 = sx9500_samp_freq_table[regval].val2;
  312. return IIO_VAL_INT_PLUS_MICRO;
  313. }
  314. static int sx9500_read_raw(struct iio_dev *indio_dev,
  315. const struct iio_chan_spec *chan,
  316. int *val, int *val2, long mask)
  317. {
  318. struct sx9500_data *data = iio_priv(indio_dev);
  319. int ret;
  320. switch (chan->type) {
  321. case IIO_PROXIMITY:
  322. switch (mask) {
  323. case IIO_CHAN_INFO_RAW:
  324. ret = iio_device_claim_direct_mode(indio_dev);
  325. if (ret)
  326. return ret;
  327. ret = sx9500_read_proximity(data, chan, val);
  328. iio_device_release_direct_mode(indio_dev);
  329. return ret;
  330. case IIO_CHAN_INFO_SAMP_FREQ:
  331. return sx9500_read_samp_freq(data, val, val2);
  332. default:
  333. return -EINVAL;
  334. }
  335. default:
  336. return -EINVAL;
  337. }
  338. }
  339. static int sx9500_set_samp_freq(struct sx9500_data *data,
  340. int val, int val2)
  341. {
  342. int i, ret;
  343. for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++)
  344. if (val == sx9500_samp_freq_table[i].val &&
  345. val2 == sx9500_samp_freq_table[i].val2)
  346. break;
  347. if (i == ARRAY_SIZE(sx9500_samp_freq_table))
  348. return -EINVAL;
  349. mutex_lock(&data->mutex);
  350. ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
  351. SX9500_SCAN_PERIOD_MASK,
  352. i << SX9500_SCAN_PERIOD_SHIFT);
  353. mutex_unlock(&data->mutex);
  354. return ret;
  355. }
  356. static int sx9500_write_raw(struct iio_dev *indio_dev,
  357. const struct iio_chan_spec *chan,
  358. int val, int val2, long mask)
  359. {
  360. struct sx9500_data *data = iio_priv(indio_dev);
  361. switch (chan->type) {
  362. case IIO_PROXIMITY:
  363. switch (mask) {
  364. case IIO_CHAN_INFO_SAMP_FREQ:
  365. return sx9500_set_samp_freq(data, val, val2);
  366. default:
  367. return -EINVAL;
  368. }
  369. default:
  370. return -EINVAL;
  371. }
  372. }
  373. static irqreturn_t sx9500_irq_handler(int irq, void *private)
  374. {
  375. struct iio_dev *indio_dev = private;
  376. struct sx9500_data *data = iio_priv(indio_dev);
  377. if (data->trigger_enabled)
  378. iio_trigger_poll(data->trig);
  379. /*
  380. * Even if no event is enabled, we need to wake the thread to
  381. * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It
  382. * is not possible to do that here because regmap_read takes a
  383. * mutex.
  384. */
  385. return IRQ_WAKE_THREAD;
  386. }
  387. static void sx9500_push_events(struct iio_dev *indio_dev)
  388. {
  389. int ret;
  390. unsigned int val, chan;
  391. struct sx9500_data *data = iio_priv(indio_dev);
  392. ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
  393. if (ret < 0) {
  394. dev_err(&data->client->dev, "i2c transfer error in irq\n");
  395. return;
  396. }
  397. val >>= SX9500_PROXSTAT_SHIFT;
  398. for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) {
  399. int dir;
  400. u64 ev;
  401. bool new_prox = val & BIT(chan);
  402. if (!data->event_enabled[chan])
  403. continue;
  404. if (new_prox == data->prox_stat[chan])
  405. /* No change on this channel. */
  406. continue;
  407. dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
  408. ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
  409. IIO_EV_TYPE_THRESH, dir);
  410. iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
  411. data->prox_stat[chan] = new_prox;
  412. }
  413. }
  414. static irqreturn_t sx9500_irq_thread_handler(int irq, void *private)
  415. {
  416. struct iio_dev *indio_dev = private;
  417. struct sx9500_data *data = iio_priv(indio_dev);
  418. int ret;
  419. unsigned int val;
  420. mutex_lock(&data->mutex);
  421. ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
  422. if (ret < 0) {
  423. dev_err(&data->client->dev, "i2c transfer error in irq\n");
  424. goto out;
  425. }
  426. if (val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ))
  427. sx9500_push_events(indio_dev);
  428. if (val & SX9500_CONVDONE_IRQ)
  429. complete(&data->completion);
  430. out:
  431. mutex_unlock(&data->mutex);
  432. return IRQ_HANDLED;
  433. }
  434. static int sx9500_read_event_config(struct iio_dev *indio_dev,
  435. const struct iio_chan_spec *chan,
  436. enum iio_event_type type,
  437. enum iio_event_direction dir)
  438. {
  439. struct sx9500_data *data = iio_priv(indio_dev);
  440. if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
  441. dir != IIO_EV_DIR_EITHER)
  442. return -EINVAL;
  443. return data->event_enabled[chan->channel];
  444. }
  445. static int sx9500_write_event_config(struct iio_dev *indio_dev,
  446. const struct iio_chan_spec *chan,
  447. enum iio_event_type type,
  448. enum iio_event_direction dir,
  449. int state)
  450. {
  451. struct sx9500_data *data = iio_priv(indio_dev);
  452. int ret;
  453. if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
  454. dir != IIO_EV_DIR_EITHER)
  455. return -EINVAL;
  456. mutex_lock(&data->mutex);
  457. if (state == 1) {
  458. ret = sx9500_inc_chan_users(data, chan->channel);
  459. if (ret < 0)
  460. goto out_unlock;
  461. ret = sx9500_inc_close_far_users(data);
  462. if (ret < 0)
  463. goto out_undo_chan;
  464. } else {
  465. ret = sx9500_dec_chan_users(data, chan->channel);
  466. if (ret < 0)
  467. goto out_unlock;
  468. ret = sx9500_dec_close_far_users(data);
  469. if (ret < 0)
  470. goto out_undo_chan;
  471. }
  472. data->event_enabled[chan->channel] = state;
  473. goto out_unlock;
  474. out_undo_chan:
  475. if (state == 1)
  476. sx9500_dec_chan_users(data, chan->channel);
  477. else
  478. sx9500_inc_chan_users(data, chan->channel);
  479. out_unlock:
  480. mutex_unlock(&data->mutex);
  481. return ret;
  482. }
  483. static int sx9500_update_scan_mode(struct iio_dev *indio_dev,
  484. const unsigned long *scan_mask)
  485. {
  486. struct sx9500_data *data = iio_priv(indio_dev);
  487. mutex_lock(&data->mutex);
  488. kfree(data->buffer);
  489. data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
  490. mutex_unlock(&data->mutex);
  491. if (data->buffer == NULL)
  492. return -ENOMEM;
  493. return 0;
  494. }
  495. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
  496. "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
  497. static struct attribute *sx9500_attributes[] = {
  498. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  499. NULL,
  500. };
  501. static const struct attribute_group sx9500_attribute_group = {
  502. .attrs = sx9500_attributes,
  503. };
  504. static const struct iio_info sx9500_info = {
  505. .attrs = &sx9500_attribute_group,
  506. .read_raw = &sx9500_read_raw,
  507. .write_raw = &sx9500_write_raw,
  508. .read_event_config = &sx9500_read_event_config,
  509. .write_event_config = &sx9500_write_event_config,
  510. .update_scan_mode = &sx9500_update_scan_mode,
  511. };
  512. static int sx9500_set_trigger_state(struct iio_trigger *trig,
  513. bool state)
  514. {
  515. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  516. struct sx9500_data *data = iio_priv(indio_dev);
  517. int ret;
  518. mutex_lock(&data->mutex);
  519. if (state)
  520. ret = sx9500_inc_data_rdy_users(data);
  521. else
  522. ret = sx9500_dec_data_rdy_users(data);
  523. if (ret < 0)
  524. goto out;
  525. data->trigger_enabled = state;
  526. out:
  527. mutex_unlock(&data->mutex);
  528. return ret;
  529. }
  530. static const struct iio_trigger_ops sx9500_trigger_ops = {
  531. .set_trigger_state = sx9500_set_trigger_state,
  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(indio_dev));
  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 const struct acpi_gpio_params reset_gpios = { 0, 0, false };
  713. static const struct acpi_gpio_params interrupt_gpios = { 2, 0, false };
  714. static const struct acpi_gpio_mapping acpi_sx9500_gpios[] = {
  715. { "reset-gpios", &reset_gpios, 1 },
  716. /*
  717. * Some platforms have a bug in ACPI GPIO description making IRQ
  718. * GPIO to be output only. Ask the GPIO core to ignore this limit.
  719. */
  720. { "interrupt-gpios", &interrupt_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
  721. { },
  722. };
  723. static void sx9500_gpio_probe(struct i2c_client *client,
  724. struct sx9500_data *data)
  725. {
  726. struct gpio_desc *gpiod_int;
  727. struct device *dev;
  728. int ret;
  729. if (!client)
  730. return;
  731. dev = &client->dev;
  732. ret = devm_acpi_dev_add_driver_gpios(dev, acpi_sx9500_gpios);
  733. if (ret)
  734. dev_dbg(dev, "Unable to add GPIO mapping table\n");
  735. if (client->irq <= 0) {
  736. gpiod_int = devm_gpiod_get(dev, "interrupt", GPIOD_IN);
  737. if (IS_ERR(gpiod_int))
  738. dev_err(dev, "gpio get irq failed\n");
  739. else
  740. client->irq = gpiod_to_irq(gpiod_int);
  741. }
  742. data->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  743. if (IS_ERR(data->gpiod_rst)) {
  744. dev_warn(dev, "gpio get reset pin failed\n");
  745. data->gpiod_rst = NULL;
  746. }
  747. }
  748. static int sx9500_probe(struct i2c_client *client,
  749. const struct i2c_device_id *id)
  750. {
  751. int ret;
  752. struct iio_dev *indio_dev;
  753. struct sx9500_data *data;
  754. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  755. if (indio_dev == NULL)
  756. return -ENOMEM;
  757. data = iio_priv(indio_dev);
  758. data->client = client;
  759. mutex_init(&data->mutex);
  760. init_completion(&data->completion);
  761. data->trigger_enabled = false;
  762. data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config);
  763. if (IS_ERR(data->regmap))
  764. return PTR_ERR(data->regmap);
  765. indio_dev->dev.parent = &client->dev;
  766. indio_dev->name = SX9500_DRIVER_NAME;
  767. indio_dev->channels = sx9500_channels;
  768. indio_dev->num_channels = ARRAY_SIZE(sx9500_channels);
  769. indio_dev->info = &sx9500_info;
  770. indio_dev->modes = INDIO_DIRECT_MODE;
  771. i2c_set_clientdata(client, indio_dev);
  772. sx9500_gpio_probe(client, data);
  773. ret = sx9500_init_device(indio_dev);
  774. if (ret < 0)
  775. return ret;
  776. if (client->irq <= 0)
  777. dev_warn(&client->dev, "no valid irq found\n");
  778. else {
  779. ret = devm_request_threaded_irq(&client->dev, client->irq,
  780. sx9500_irq_handler, sx9500_irq_thread_handler,
  781. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  782. SX9500_IRQ_NAME, indio_dev);
  783. if (ret < 0)
  784. return ret;
  785. data->trig = devm_iio_trigger_alloc(&client->dev,
  786. "%s-dev%d", indio_dev->name, indio_dev->id);
  787. if (!data->trig)
  788. return -ENOMEM;
  789. data->trig->dev.parent = &client->dev;
  790. data->trig->ops = &sx9500_trigger_ops;
  791. iio_trigger_set_drvdata(data->trig, indio_dev);
  792. ret = iio_trigger_register(data->trig);
  793. if (ret)
  794. return ret;
  795. }
  796. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  797. sx9500_trigger_handler,
  798. &sx9500_buffer_setup_ops);
  799. if (ret < 0)
  800. goto out_trigger_unregister;
  801. ret = iio_device_register(indio_dev);
  802. if (ret < 0)
  803. goto out_buffer_cleanup;
  804. return 0;
  805. out_buffer_cleanup:
  806. iio_triggered_buffer_cleanup(indio_dev);
  807. out_trigger_unregister:
  808. if (client->irq > 0)
  809. iio_trigger_unregister(data->trig);
  810. return ret;
  811. }
  812. static int sx9500_remove(struct i2c_client *client)
  813. {
  814. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  815. struct sx9500_data *data = iio_priv(indio_dev);
  816. iio_device_unregister(indio_dev);
  817. iio_triggered_buffer_cleanup(indio_dev);
  818. if (client->irq > 0)
  819. iio_trigger_unregister(data->trig);
  820. kfree(data->buffer);
  821. return 0;
  822. }
  823. #ifdef CONFIG_PM_SLEEP
  824. static int sx9500_suspend(struct device *dev)
  825. {
  826. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  827. struct sx9500_data *data = iio_priv(indio_dev);
  828. int ret;
  829. mutex_lock(&data->mutex);
  830. ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0,
  831. &data->suspend_ctrl0);
  832. if (ret < 0)
  833. goto out;
  834. /*
  835. * Scan period doesn't matter because when all the sensors are
  836. * deactivated the device is in sleep mode.
  837. */
  838. ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0);
  839. out:
  840. mutex_unlock(&data->mutex);
  841. return ret;
  842. }
  843. static int sx9500_resume(struct device *dev)
  844. {
  845. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  846. struct sx9500_data *data = iio_priv(indio_dev);
  847. int ret;
  848. mutex_lock(&data->mutex);
  849. ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0,
  850. data->suspend_ctrl0);
  851. mutex_unlock(&data->mutex);
  852. return ret;
  853. }
  854. #endif /* CONFIG_PM_SLEEP */
  855. static const struct dev_pm_ops sx9500_pm_ops = {
  856. SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend, sx9500_resume)
  857. };
  858. static const struct acpi_device_id sx9500_acpi_match[] = {
  859. {"SSX9500", 0},
  860. {"SASX9500", 0},
  861. { },
  862. };
  863. MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match);
  864. static const struct of_device_id sx9500_of_match[] = {
  865. { .compatible = "semtech,sx9500", },
  866. { }
  867. };
  868. MODULE_DEVICE_TABLE(of, sx9500_of_match);
  869. static const struct i2c_device_id sx9500_id[] = {
  870. {"sx9500", 0},
  871. { },
  872. };
  873. MODULE_DEVICE_TABLE(i2c, sx9500_id);
  874. static struct i2c_driver sx9500_driver = {
  875. .driver = {
  876. .name = SX9500_DRIVER_NAME,
  877. .acpi_match_table = ACPI_PTR(sx9500_acpi_match),
  878. .of_match_table = of_match_ptr(sx9500_of_match),
  879. .pm = &sx9500_pm_ops,
  880. },
  881. .probe = sx9500_probe,
  882. .remove = sx9500_remove,
  883. .id_table = sx9500_id,
  884. };
  885. module_i2c_driver(sx9500_driver);
  886. MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
  887. MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
  888. MODULE_LICENSE("GPL v2");