blob: 287d288e40c27b107e838762c4fd17a519c0c7cb [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Vlad Dogaru4193c0f2014-12-29 14:41:14 +02002/*
3 * Copyright (c) 2014 Intel Corporation
4 *
5 * Driver for Semtech's SX9500 capacitive proximity/button solution.
6 * Datasheet available at
7 * <http://www.semtech.com/images/datasheet/sx9500.pdf>.
Vlad Dogaru4193c0f2014-12-29 14:41:14 +02008 */
9
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/module.h>
13#include <linux/i2c.h>
14#include <linux/irq.h>
15#include <linux/acpi.h>
16#include <linux/gpio/consumer.h>
17#include <linux/regmap.h>
Vlad Dogaru7840ffe2015-04-03 15:47:29 +030018#include <linux/pm.h>
Vlad Dogaru59bd0422015-04-12 20:09:19 +030019#include <linux/delay.h>
Vlad Dogaru4193c0f2014-12-29 14:41:14 +020020
21#include <linux/iio/iio.h>
22#include <linux/iio/buffer.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/events.h>
25#include <linux/iio/trigger.h>
26#include <linux/iio/triggered_buffer.h>
27#include <linux/iio/trigger_consumer.h>
28
29#define SX9500_DRIVER_NAME "sx9500"
30#define SX9500_IRQ_NAME "sx9500_event"
Vlad Dogaru63de9f92015-04-03 15:47:31 +030031
Vlad Dogaru4193c0f2014-12-29 14:41:14 +020032/* Register definitions. */
33#define SX9500_REG_IRQ_SRC 0x00
34#define SX9500_REG_STAT 0x01
35#define SX9500_REG_IRQ_MSK 0x03
36
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
47#define SX9500_REG_SENSOR_SEL 0x20
48#define SX9500_REG_USE_MSB 0x21
49#define SX9500_REG_USE_LSB 0x22
50#define SX9500_REG_AVG_MSB 0x23
51#define SX9500_REG_AVG_LSB 0x24
52#define SX9500_REG_DIFF_MSB 0x25
53#define SX9500_REG_DIFF_LSB 0x26
54#define SX9500_REG_OFFSET_MSB 0x27
55#define SX9500_REG_OFFSET_LSB 0x28
56
57#define SX9500_REG_RESET 0x7f
58
59/* Write this to REG_RESET to do a soft reset. */
60#define SX9500_SOFT_RESET 0xde
61
62#define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4)
63#define SX9500_SCAN_PERIOD_SHIFT 4
64
65/*
66 * These serve for identifying IRQ source in the IRQ_SRC register, and
67 * also for masking the IRQs in the IRQ_MSK register.
68 */
69#define SX9500_CLOSE_IRQ BIT(6)
70#define SX9500_FAR_IRQ BIT(5)
71#define SX9500_CONVDONE_IRQ BIT(3)
72
73#define SX9500_PROXSTAT_SHIFT 4
Vlad Dogaru59bd0422015-04-12 20:09:19 +030074#define SX9500_COMPSTAT_MASK GENMASK(3, 0)
Vlad Dogaru4193c0f2014-12-29 14:41:14 +020075
76#define SX9500_NUM_CHANNELS 4
Vlad Dogaru68958bd2015-06-30 14:20:59 +030077#define SX9500_CHAN_MASK GENMASK(SX9500_NUM_CHANNELS - 1, 0)
Vlad Dogaru4193c0f2014-12-29 14:41:14 +020078
79struct sx9500_data {
80 struct mutex mutex;
81 struct i2c_client *client;
82 struct iio_trigger *trig;
83 struct regmap *regmap;
Vlad Dogaru45fd5f82015-04-12 20:09:21 +030084 struct gpio_desc *gpiod_rst;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +020085 /*
86 * Last reading of the proximity status for each channel. We
87 * only send an event to user space when this changes.
88 */
89 bool prox_stat[SX9500_NUM_CHANNELS];
90 bool event_enabled[SX9500_NUM_CHANNELS];
91 bool trigger_enabled;
92 u16 *buffer;
Vlad Dogaru7840ffe2015-04-03 15:47:29 +030093 /* Remember enabled channels and sample rate during suspend. */
94 unsigned int suspend_ctrl0;
Vlad Dogaru59bd0422015-04-12 20:09:19 +030095 struct completion completion;
96 int data_rdy_users, close_far_users;
97 int channel_users[SX9500_NUM_CHANNELS];
Vlad Dogaru4193c0f2014-12-29 14:41:14 +020098};
99
100static const struct iio_event_spec sx9500_events[] = {
101 {
102 .type = IIO_EV_TYPE_THRESH,
103 .dir = IIO_EV_DIR_EITHER,
104 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
105 },
106};
107
108#define SX9500_CHANNEL(idx) \
109 { \
110 .type = IIO_PROXIMITY, \
111 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
112 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
113 .indexed = 1, \
114 .channel = idx, \
115 .event_spec = sx9500_events, \
116 .num_event_specs = ARRAY_SIZE(sx9500_events), \
117 .scan_index = idx, \
118 .scan_type = { \
119 .sign = 'u', \
120 .realbits = 16, \
121 .storagebits = 16, \
122 .shift = 0, \
123 }, \
124 }
125
126static const struct iio_chan_spec sx9500_channels[] = {
127 SX9500_CHANNEL(0),
128 SX9500_CHANNEL(1),
129 SX9500_CHANNEL(2),
130 SX9500_CHANNEL(3),
131 IIO_CHAN_SOFT_TIMESTAMP(4),
132};
133
134static const struct {
135 int val;
136 int val2;
137} sx9500_samp_freq_table[] = {
138 {33, 333333},
139 {16, 666666},
140 {11, 111111},
141 {8, 333333},
142 {6, 666666},
143 {5, 0},
144 {3, 333333},
145 {2, 500000},
146};
147
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300148static const unsigned int sx9500_scan_period_table[] = {
149 30, 60, 90, 120, 150, 200, 300, 400,
150};
151
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200152static const struct regmap_range sx9500_writable_reg_ranges[] = {
153 regmap_reg_range(SX9500_REG_IRQ_MSK, SX9500_REG_IRQ_MSK),
154 regmap_reg_range(SX9500_REG_PROX_CTRL0, SX9500_REG_PROX_CTRL8),
155 regmap_reg_range(SX9500_REG_SENSOR_SEL, SX9500_REG_SENSOR_SEL),
156 regmap_reg_range(SX9500_REG_OFFSET_MSB, SX9500_REG_OFFSET_LSB),
157 regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
158};
159
160static const struct regmap_access_table sx9500_writeable_regs = {
161 .yes_ranges = sx9500_writable_reg_ranges,
162 .n_yes_ranges = ARRAY_SIZE(sx9500_writable_reg_ranges),
163};
164
165/*
166 * All allocated registers are readable, so we just list unallocated
167 * ones.
168 */
169static const struct regmap_range sx9500_non_readable_reg_ranges[] = {
170 regmap_reg_range(SX9500_REG_STAT + 1, SX9500_REG_STAT + 1),
171 regmap_reg_range(SX9500_REG_IRQ_MSK + 1, SX9500_REG_PROX_CTRL0 - 1),
172 regmap_reg_range(SX9500_REG_PROX_CTRL8 + 1, SX9500_REG_SENSOR_SEL - 1),
173 regmap_reg_range(SX9500_REG_OFFSET_LSB + 1, SX9500_REG_RESET - 1),
174};
175
176static const struct regmap_access_table sx9500_readable_regs = {
177 .no_ranges = sx9500_non_readable_reg_ranges,
178 .n_no_ranges = ARRAY_SIZE(sx9500_non_readable_reg_ranges),
179};
180
181static const struct regmap_range sx9500_volatile_reg_ranges[] = {
182 regmap_reg_range(SX9500_REG_IRQ_SRC, SX9500_REG_STAT),
183 regmap_reg_range(SX9500_REG_USE_MSB, SX9500_REG_OFFSET_LSB),
184 regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
185};
186
187static const struct regmap_access_table sx9500_volatile_regs = {
188 .yes_ranges = sx9500_volatile_reg_ranges,
189 .n_yes_ranges = ARRAY_SIZE(sx9500_volatile_reg_ranges),
190};
191
192static const struct regmap_config sx9500_regmap_config = {
193 .reg_bits = 8,
194 .val_bits = 8,
195
196 .max_register = SX9500_REG_RESET,
197 .cache_type = REGCACHE_RBTREE,
198
199 .wr_table = &sx9500_writeable_regs,
200 .rd_table = &sx9500_readable_regs,
201 .volatile_table = &sx9500_volatile_regs,
202};
203
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300204static int sx9500_inc_users(struct sx9500_data *data, int *counter,
205 unsigned int reg, unsigned int bitmask)
206{
207 (*counter)++;
208 if (*counter != 1)
209 /* Bit is already active, nothing to do. */
210 return 0;
211
212 return regmap_update_bits(data->regmap, reg, bitmask, bitmask);
213}
214
215static int sx9500_dec_users(struct sx9500_data *data, int *counter,
216 unsigned int reg, unsigned int bitmask)
217{
218 (*counter)--;
219 if (*counter != 0)
220 /* There are more users, do not deactivate. */
221 return 0;
222
223 return regmap_update_bits(data->regmap, reg, bitmask, 0);
224}
225
226static int sx9500_inc_chan_users(struct sx9500_data *data, int chan)
227{
228 return sx9500_inc_users(data, &data->channel_users[chan],
229 SX9500_REG_PROX_CTRL0, BIT(chan));
230}
231
232static int sx9500_dec_chan_users(struct sx9500_data *data, int chan)
233{
234 return sx9500_dec_users(data, &data->channel_users[chan],
235 SX9500_REG_PROX_CTRL0, BIT(chan));
236}
237
238static int sx9500_inc_data_rdy_users(struct sx9500_data *data)
239{
240 return sx9500_inc_users(data, &data->data_rdy_users,
241 SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ);
242}
243
244static int sx9500_dec_data_rdy_users(struct sx9500_data *data)
245{
246 return sx9500_dec_users(data, &data->data_rdy_users,
247 SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ);
248}
249
250static int sx9500_inc_close_far_users(struct sx9500_data *data)
251{
252 return sx9500_inc_users(data, &data->close_far_users,
253 SX9500_REG_IRQ_MSK,
254 SX9500_CLOSE_IRQ | SX9500_FAR_IRQ);
255}
256
257static int sx9500_dec_close_far_users(struct sx9500_data *data)
258{
259 return sx9500_dec_users(data, &data->close_far_users,
260 SX9500_REG_IRQ_MSK,
261 SX9500_CLOSE_IRQ | SX9500_FAR_IRQ);
262}
263
264static int sx9500_read_prox_data(struct sx9500_data *data,
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200265 const struct iio_chan_spec *chan,
266 int *val)
267{
268 int ret;
269 __be16 regval;
270
271 ret = regmap_write(data->regmap, SX9500_REG_SENSOR_SEL, chan->channel);
272 if (ret < 0)
273 return ret;
274
275 ret = regmap_bulk_read(data->regmap, SX9500_REG_USE_MSB, &regval, 2);
276 if (ret < 0)
277 return ret;
278
Daniel Balutafd1883f2015-06-11 18:49:34 +0300279 *val = be16_to_cpu(regval);
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200280
281 return IIO_VAL_INT;
282}
283
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300284/*
285 * If we have no interrupt support, we have to wait for a scan period
286 * after enabling a channel to get a result.
287 */
288static int sx9500_wait_for_sample(struct sx9500_data *data)
289{
290 int ret;
291 unsigned int val;
292
293 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &val);
294 if (ret < 0)
295 return ret;
296
297 val = (val & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT;
298
299 msleep(sx9500_scan_period_table[val]);
300
301 return 0;
302}
303
304static int sx9500_read_proximity(struct sx9500_data *data,
305 const struct iio_chan_spec *chan,
306 int *val)
307{
308 int ret;
309
310 mutex_lock(&data->mutex);
311
312 ret = sx9500_inc_chan_users(data, chan->channel);
313 if (ret < 0)
314 goto out;
315
316 ret = sx9500_inc_data_rdy_users(data);
317 if (ret < 0)
318 goto out_dec_chan;
319
320 mutex_unlock(&data->mutex);
321
322 if (data->client->irq > 0)
323 ret = wait_for_completion_interruptible(&data->completion);
324 else
325 ret = sx9500_wait_for_sample(data);
326
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300327 mutex_lock(&data->mutex);
328
Vlad Dogaru657c7ff2015-06-30 14:20:58 +0300329 if (ret < 0)
330 goto out_dec_data_rdy;
331
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300332 ret = sx9500_read_prox_data(data, chan, val);
333 if (ret < 0)
Vlad Dogaru657c7ff2015-06-30 14:20:58 +0300334 goto out_dec_data_rdy;
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300335
336 ret = sx9500_dec_data_rdy_users(data);
337 if (ret < 0)
Vlad Dogaru657c7ff2015-06-30 14:20:58 +0300338 goto out_dec_chan;
339
340 ret = sx9500_dec_chan_users(data, chan->channel);
341 if (ret < 0)
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300342 goto out;
343
344 ret = IIO_VAL_INT;
345
346 goto out;
347
Vlad Dogaru657c7ff2015-06-30 14:20:58 +0300348out_dec_data_rdy:
349 sx9500_dec_data_rdy_users(data);
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300350out_dec_chan:
351 sx9500_dec_chan_users(data, chan->channel);
352out:
353 mutex_unlock(&data->mutex);
354 reinit_completion(&data->completion);
355
356 return ret;
357}
358
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200359static int sx9500_read_samp_freq(struct sx9500_data *data,
360 int *val, int *val2)
361{
362 int ret;
363 unsigned int regval;
364
365 mutex_lock(&data->mutex);
366 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &regval);
367 mutex_unlock(&data->mutex);
368
369 if (ret < 0)
370 return ret;
371
372 regval = (regval & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT;
373 *val = sx9500_samp_freq_table[regval].val;
374 *val2 = sx9500_samp_freq_table[regval].val2;
375
376 return IIO_VAL_INT_PLUS_MICRO;
377}
378
379static int sx9500_read_raw(struct iio_dev *indio_dev,
380 const struct iio_chan_spec *chan,
381 int *val, int *val2, long mask)
382{
383 struct sx9500_data *data = iio_priv(indio_dev);
Alison Schofield6b2e7582017-01-20 14:11:30 -0800384 int ret;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200385
386 switch (chan->type) {
387 case IIO_PROXIMITY:
388 switch (mask) {
389 case IIO_CHAN_INFO_RAW:
Alison Schofield6b2e7582017-01-20 14:11:30 -0800390 ret = iio_device_claim_direct_mode(indio_dev);
391 if (ret)
392 return ret;
393 ret = sx9500_read_proximity(data, chan, val);
394 iio_device_release_direct_mode(indio_dev);
395 return ret;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200396 case IIO_CHAN_INFO_SAMP_FREQ:
397 return sx9500_read_samp_freq(data, val, val2);
398 default:
399 return -EINVAL;
400 }
401 default:
402 return -EINVAL;
403 }
404}
405
406static int sx9500_set_samp_freq(struct sx9500_data *data,
407 int val, int val2)
408{
409 int i, ret;
410
411 for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++)
412 if (val == sx9500_samp_freq_table[i].val &&
413 val2 == sx9500_samp_freq_table[i].val2)
414 break;
415
416 if (i == ARRAY_SIZE(sx9500_samp_freq_table))
417 return -EINVAL;
418
419 mutex_lock(&data->mutex);
420
421 ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
422 SX9500_SCAN_PERIOD_MASK,
423 i << SX9500_SCAN_PERIOD_SHIFT);
424
425 mutex_unlock(&data->mutex);
426
427 return ret;
428}
429
430static int sx9500_write_raw(struct iio_dev *indio_dev,
431 const struct iio_chan_spec *chan,
432 int val, int val2, long mask)
433{
434 struct sx9500_data *data = iio_priv(indio_dev);
435
436 switch (chan->type) {
437 case IIO_PROXIMITY:
438 switch (mask) {
439 case IIO_CHAN_INFO_SAMP_FREQ:
440 return sx9500_set_samp_freq(data, val, val2);
441 default:
442 return -EINVAL;
443 }
444 default:
445 return -EINVAL;
446 }
447}
448
449static irqreturn_t sx9500_irq_handler(int irq, void *private)
450{
451 struct iio_dev *indio_dev = private;
452 struct sx9500_data *data = iio_priv(indio_dev);
453
454 if (data->trigger_enabled)
455 iio_trigger_poll(data->trig);
456
457 /*
458 * Even if no event is enabled, we need to wake the thread to
459 * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It
460 * is not possible to do that here because regmap_read takes a
461 * mutex.
462 */
463 return IRQ_WAKE_THREAD;
464}
465
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300466static void sx9500_push_events(struct iio_dev *indio_dev)
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200467{
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200468 int ret;
469 unsigned int val, chan;
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300470 struct sx9500_data *data = iio_priv(indio_dev);
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200471
472 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
473 if (ret < 0) {
474 dev_err(&data->client->dev, "i2c transfer error in irq\n");
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300475 return;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200476 }
477
478 val >>= SX9500_PROXSTAT_SHIFT;
479 for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) {
480 int dir;
481 u64 ev;
482 bool new_prox = val & BIT(chan);
483
484 if (!data->event_enabled[chan])
485 continue;
486 if (new_prox == data->prox_stat[chan])
487 /* No change on this channel. */
488 continue;
489
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300490 dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
491 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
492 IIO_EV_TYPE_THRESH, dir);
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100493 iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200494 data->prox_stat[chan] = new_prox;
495 }
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300496}
497
498static irqreturn_t sx9500_irq_thread_handler(int irq, void *private)
499{
500 struct iio_dev *indio_dev = private;
501 struct sx9500_data *data = iio_priv(indio_dev);
502 int ret;
503 unsigned int val;
504
505 mutex_lock(&data->mutex);
506
507 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
508 if (ret < 0) {
509 dev_err(&data->client->dev, "i2c transfer error in irq\n");
510 goto out;
511 }
512
513 if (val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ))
514 sx9500_push_events(indio_dev);
515
516 if (val & SX9500_CONVDONE_IRQ)
Daniel Wagner8c11e162016-08-04 15:07:10 +0200517 complete(&data->completion);
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200518
519out:
520 mutex_unlock(&data->mutex);
521
522 return IRQ_HANDLED;
523}
524
525static int sx9500_read_event_config(struct iio_dev *indio_dev,
526 const struct iio_chan_spec *chan,
527 enum iio_event_type type,
528 enum iio_event_direction dir)
529{
530 struct sx9500_data *data = iio_priv(indio_dev);
531
532 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
533 dir != IIO_EV_DIR_EITHER)
534 return -EINVAL;
535
536 return data->event_enabled[chan->channel];
537}
538
539static int sx9500_write_event_config(struct iio_dev *indio_dev,
540 const struct iio_chan_spec *chan,
541 enum iio_event_type type,
542 enum iio_event_direction dir,
543 int state)
544{
545 struct sx9500_data *data = iio_priv(indio_dev);
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300546 int ret;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200547
548 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
549 dir != IIO_EV_DIR_EITHER)
550 return -EINVAL;
551
552 mutex_lock(&data->mutex);
553
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300554 if (state == 1) {
555 ret = sx9500_inc_chan_users(data, chan->channel);
556 if (ret < 0)
557 goto out_unlock;
558 ret = sx9500_inc_close_far_users(data);
559 if (ret < 0)
560 goto out_undo_chan;
561 } else {
562 ret = sx9500_dec_chan_users(data, chan->channel);
563 if (ret < 0)
564 goto out_unlock;
565 ret = sx9500_dec_close_far_users(data);
566 if (ret < 0)
567 goto out_undo_chan;
568 }
569
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200570 data->event_enabled[chan->channel] = state;
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300571 goto out_unlock;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200572
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300573out_undo_chan:
574 if (state == 1)
575 sx9500_dec_chan_users(data, chan->channel);
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200576 else
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300577 sx9500_inc_chan_users(data, chan->channel);
578out_unlock:
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200579 mutex_unlock(&data->mutex);
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200580 return ret;
581}
582
583static int sx9500_update_scan_mode(struct iio_dev *indio_dev,
584 const unsigned long *scan_mask)
585{
586 struct sx9500_data *data = iio_priv(indio_dev);
587
588 mutex_lock(&data->mutex);
589 kfree(data->buffer);
590 data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
591 mutex_unlock(&data->mutex);
592
593 if (data->buffer == NULL)
594 return -ENOMEM;
595
596 return 0;
597}
598
599static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
600 "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
601
602static struct attribute *sx9500_attributes[] = {
603 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
604 NULL,
605};
606
607static const struct attribute_group sx9500_attribute_group = {
608 .attrs = sx9500_attributes,
609};
610
611static const struct iio_info sx9500_info = {
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200612 .attrs = &sx9500_attribute_group,
613 .read_raw = &sx9500_read_raw,
614 .write_raw = &sx9500_write_raw,
615 .read_event_config = &sx9500_read_event_config,
616 .write_event_config = &sx9500_write_event_config,
617 .update_scan_mode = &sx9500_update_scan_mode,
618};
619
620static int sx9500_set_trigger_state(struct iio_trigger *trig,
621 bool state)
622{
623 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
624 struct sx9500_data *data = iio_priv(indio_dev);
625 int ret;
626
627 mutex_lock(&data->mutex);
628
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300629 if (state)
630 ret = sx9500_inc_data_rdy_users(data);
631 else
632 ret = sx9500_dec_data_rdy_users(data);
633 if (ret < 0)
634 goto out;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200635
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300636 data->trigger_enabled = state;
637
638out:
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200639 mutex_unlock(&data->mutex);
640
641 return ret;
642}
643
644static const struct iio_trigger_ops sx9500_trigger_ops = {
645 .set_trigger_state = sx9500_set_trigger_state,
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200646};
647
648static irqreturn_t sx9500_trigger_handler(int irq, void *private)
649{
650 struct iio_poll_func *pf = private;
651 struct iio_dev *indio_dev = pf->indio_dev;
652 struct sx9500_data *data = iio_priv(indio_dev);
653 int val, bit, ret, i = 0;
654
655 mutex_lock(&data->mutex);
656
Octavian Purdila70dddee2015-03-02 21:03:05 +0200657 for_each_set_bit(bit, indio_dev->active_scan_mask,
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200658 indio_dev->masklength) {
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300659 ret = sx9500_read_prox_data(data, &indio_dev->channels[bit],
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200660 &val);
661 if (ret < 0)
662 goto out;
663
664 data->buffer[i++] = val;
665 }
666
667 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100668 iio_get_time_ns(indio_dev));
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200669
670out:
671 mutex_unlock(&data->mutex);
672
673 iio_trigger_notify_done(indio_dev->trig);
674
675 return IRQ_HANDLED;
676}
677
Alexandru Ardelean3cfd6462019-09-20 11:35:13 +0300678static int sx9500_buffer_postenable(struct iio_dev *indio_dev)
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300679{
680 struct sx9500_data *data = iio_priv(indio_dev);
Geert Uytterhoeven897993f2015-06-29 09:14:33 +0200681 int ret = 0, i;
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300682
Alexandru Ardelean3cfd6462019-09-20 11:35:13 +0300683 ret = iio_triggered_buffer_postenable(indio_dev);
684 if (ret)
685 return ret;
686
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300687 mutex_lock(&data->mutex);
688
689 for (i = 0; i < SX9500_NUM_CHANNELS; i++)
690 if (test_bit(i, indio_dev->active_scan_mask)) {
691 ret = sx9500_inc_chan_users(data, i);
692 if (ret)
693 break;
694 }
695
696 if (ret)
697 for (i = i - 1; i >= 0; i--)
698 if (test_bit(i, indio_dev->active_scan_mask))
699 sx9500_dec_chan_users(data, i);
700
701 mutex_unlock(&data->mutex);
702
Alexandru Ardelean3cfd6462019-09-20 11:35:13 +0300703 if (ret)
704 iio_triggered_buffer_predisable(indio_dev);
705
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300706 return ret;
707}
708
709static int sx9500_buffer_predisable(struct iio_dev *indio_dev)
710{
711 struct sx9500_data *data = iio_priv(indio_dev);
Geert Uytterhoeven897993f2015-06-29 09:14:33 +0200712 int ret = 0, i;
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300713
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300714 mutex_lock(&data->mutex);
715
716 for (i = 0; i < SX9500_NUM_CHANNELS; i++)
717 if (test_bit(i, indio_dev->active_scan_mask)) {
718 ret = sx9500_dec_chan_users(data, i);
719 if (ret)
720 break;
721 }
722
723 if (ret)
724 for (i = i - 1; i >= 0; i--)
725 if (test_bit(i, indio_dev->active_scan_mask))
726 sx9500_inc_chan_users(data, i);
727
728 mutex_unlock(&data->mutex);
729
Alexandru Ardelean3cfd6462019-09-20 11:35:13 +0300730 iio_triggered_buffer_predisable(indio_dev);
731
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300732 return ret;
733}
734
735static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops = {
Alexandru Ardelean3cfd6462019-09-20 11:35:13 +0300736 .postenable = sx9500_buffer_postenable,
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300737 .predisable = sx9500_buffer_predisable,
738};
739
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200740struct sx9500_reg_default {
741 u8 reg;
742 u8 def;
743};
744
745static const struct sx9500_reg_default sx9500_default_regs[] = {
746 {
747 .reg = SX9500_REG_PROX_CTRL1,
748 /* Shield enabled, small range. */
749 .def = 0x43,
750 },
751 {
752 .reg = SX9500_REG_PROX_CTRL2,
753 /* x8 gain, 167kHz frequency, finest resolution. */
754 .def = 0x77,
755 },
756 {
757 .reg = SX9500_REG_PROX_CTRL3,
758 /* Doze enabled, 2x scan period doze, no raw filter. */
759 .def = 0x40,
760 },
761 {
762 .reg = SX9500_REG_PROX_CTRL4,
763 /* Average threshold. */
764 .def = 0x30,
765 },
766 {
767 .reg = SX9500_REG_PROX_CTRL5,
768 /*
769 * Debouncer off, lowest average negative filter,
770 * highest average postive filter.
771 */
772 .def = 0x0f,
773 },
774 {
775 .reg = SX9500_REG_PROX_CTRL6,
776 /* Proximity detection threshold: 280 */
777 .def = 0x0e,
778 },
779 {
780 .reg = SX9500_REG_PROX_CTRL7,
781 /*
782 * No automatic compensation, compensate each pin
783 * independently, proximity hysteresis: 32, close
784 * debouncer off, far debouncer off.
785 */
786 .def = 0x00,
787 },
788 {
789 .reg = SX9500_REG_PROX_CTRL8,
790 /* No stuck timeout, no periodic compensation. */
791 .def = 0x00,
792 },
793 {
794 .reg = SX9500_REG_PROX_CTRL0,
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300795 /* Scan period: 30ms, all sensors disabled. */
796 .def = 0x00,
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200797 },
798};
799
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300800/* Activate all channels and perform an initial compensation. */
801static int sx9500_init_compensation(struct iio_dev *indio_dev)
802{
803 struct sx9500_data *data = iio_priv(indio_dev);
804 int i, ret;
805 unsigned int val;
806
807 ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
Vlad Dogaru68958bd2015-06-30 14:20:59 +0300808 SX9500_CHAN_MASK, SX9500_CHAN_MASK);
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300809 if (ret < 0)
810 return ret;
811
812 for (i = 10; i >= 0; i--) {
813 usleep_range(10000, 20000);
814 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
815 if (ret < 0)
816 goto out;
817 if (!(val & SX9500_COMPSTAT_MASK))
818 break;
819 }
820
821 if (i < 0) {
822 dev_err(&data->client->dev, "initial compensation timed out");
823 ret = -ETIMEDOUT;
824 }
825
826out:
827 regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
Vlad Dogaru68958bd2015-06-30 14:20:59 +0300828 SX9500_CHAN_MASK, 0);
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300829 return ret;
830}
831
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200832static int sx9500_init_device(struct iio_dev *indio_dev)
833{
834 struct sx9500_data *data = iio_priv(indio_dev);
835 int ret, i;
836 unsigned int val;
837
Vlad Dogaru45fd5f82015-04-12 20:09:21 +0300838 if (data->gpiod_rst) {
839 gpiod_set_value_cansleep(data->gpiod_rst, 0);
840 usleep_range(1000, 2000);
841 gpiod_set_value_cansleep(data->gpiod_rst, 1);
842 usleep_range(1000, 2000);
843 }
844
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200845 ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0);
846 if (ret < 0)
847 return ret;
848
849 ret = regmap_write(data->regmap, SX9500_REG_RESET,
850 SX9500_SOFT_RESET);
851 if (ret < 0)
852 return ret;
853
854 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
855 if (ret < 0)
856 return ret;
857
858 for (i = 0; i < ARRAY_SIZE(sx9500_default_regs); i++) {
859 ret = regmap_write(data->regmap,
860 sx9500_default_regs[i].reg,
861 sx9500_default_regs[i].def);
862 if (ret < 0)
863 return ret;
864 }
865
Jonathan Cameron1a302952015-05-02 11:29:42 +0100866 return sx9500_init_compensation(indio_dev);
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200867}
868
Andy Shevchenkob61d8e62018-02-26 21:37:35 +0200869static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
870static const struct acpi_gpio_params interrupt_gpios = { 2, 0, false };
871
872static const struct acpi_gpio_mapping acpi_sx9500_gpios[] = {
873 { "reset-gpios", &reset_gpios, 1 },
874 /*
875 * Some platforms have a bug in ACPI GPIO description making IRQ
876 * GPIO to be output only. Ask the GPIO core to ignore this limit.
877 */
878 { "interrupt-gpios", &interrupt_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
879 { },
880};
881
Vlad Dogaru821ace22015-04-12 20:09:20 +0300882static void sx9500_gpio_probe(struct i2c_client *client,
883 struct sx9500_data *data)
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200884{
Andy Shevchenkoe53111a2017-11-03 15:03:36 +0200885 struct gpio_desc *gpiod_int;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200886 struct device *dev;
Andy Shevchenkob61d8e62018-02-26 21:37:35 +0200887 int ret;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200888
889 if (!client)
Vlad Dogaru821ace22015-04-12 20:09:20 +0300890 return;
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200891
892 dev = &client->dev;
893
Andy Shevchenkob61d8e62018-02-26 21:37:35 +0200894 ret = devm_acpi_dev_add_driver_gpios(dev, acpi_sx9500_gpios);
895 if (ret)
896 dev_dbg(dev, "Unable to add GPIO mapping table\n");
897
Andy Shevchenkoe53111a2017-11-03 15:03:36 +0200898 if (client->irq <= 0) {
Andy Shevchenkob61d8e62018-02-26 21:37:35 +0200899 gpiod_int = devm_gpiod_get(dev, "interrupt", GPIOD_IN);
Andy Shevchenkoe53111a2017-11-03 15:03:36 +0200900 if (IS_ERR(gpiod_int))
901 dev_err(dev, "gpio get irq failed\n");
902 else
903 client->irq = gpiod_to_irq(gpiod_int);
904 }
905
Andy Shevchenkob61d8e62018-02-26 21:37:35 +0200906 data->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
Vlad Dogaru45fd5f82015-04-12 20:09:21 +0300907 if (IS_ERR(data->gpiod_rst)) {
908 dev_warn(dev, "gpio get reset pin failed\n");
909 data->gpiod_rst = NULL;
910 }
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200911}
912
913static int sx9500_probe(struct i2c_client *client,
914 const struct i2c_device_id *id)
915{
916 int ret;
917 struct iio_dev *indio_dev;
918 struct sx9500_data *data;
919
920 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
921 if (indio_dev == NULL)
922 return -ENOMEM;
923
924 data = iio_priv(indio_dev);
925 data->client = client;
926 mutex_init(&data->mutex);
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300927 init_completion(&data->completion);
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200928 data->trigger_enabled = false;
929
930 data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config);
931 if (IS_ERR(data->regmap))
932 return PTR_ERR(data->regmap);
933
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200934 indio_dev->dev.parent = &client->dev;
935 indio_dev->name = SX9500_DRIVER_NAME;
936 indio_dev->channels = sx9500_channels;
937 indio_dev->num_channels = ARRAY_SIZE(sx9500_channels);
938 indio_dev->info = &sx9500_info;
939 indio_dev->modes = INDIO_DIRECT_MODE;
940 i2c_set_clientdata(client, indio_dev);
941
Vlad Dogaru821ace22015-04-12 20:09:20 +0300942 sx9500_gpio_probe(client, data);
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200943
Vlad Dogaru45fd5f82015-04-12 20:09:21 +0300944 ret = sx9500_init_device(indio_dev);
945 if (ret < 0)
946 return ret;
947
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300948 if (client->irq <= 0)
949 dev_warn(&client->dev, "no valid irq found\n");
950 else {
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200951 ret = devm_request_threaded_irq(&client->dev, client->irq,
952 sx9500_irq_handler, sx9500_irq_thread_handler,
953 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
954 SX9500_IRQ_NAME, indio_dev);
955 if (ret < 0)
956 return ret;
957
958 data->trig = devm_iio_trigger_alloc(&client->dev,
959 "%s-dev%d", indio_dev->name, indio_dev->id);
960 if (!data->trig)
961 return -ENOMEM;
962
963 data->trig->dev.parent = &client->dev;
964 data->trig->ops = &sx9500_trigger_ops;
965 iio_trigger_set_drvdata(data->trig, indio_dev);
966
967 ret = iio_trigger_register(data->trig);
968 if (ret)
969 return ret;
970 }
971
972 ret = iio_triggered_buffer_setup(indio_dev, NULL,
Vlad Dogaru59bd0422015-04-12 20:09:19 +0300973 sx9500_trigger_handler,
974 &sx9500_buffer_setup_ops);
Vlad Dogaru4193c0f2014-12-29 14:41:14 +0200975 if (ret < 0)
976 goto out_trigger_unregister;
977
978 ret = iio_device_register(indio_dev);
979 if (ret < 0)
980 goto out_buffer_cleanup;
981
982 return 0;
983
984out_buffer_cleanup:
985 iio_triggered_buffer_cleanup(indio_dev);
986out_trigger_unregister:
987 if (client->irq > 0)
988 iio_trigger_unregister(data->trig);
989
990 return ret;
991}
992
993static int sx9500_remove(struct i2c_client *client)
994{
995 struct iio_dev *indio_dev = i2c_get_clientdata(client);
996 struct sx9500_data *data = iio_priv(indio_dev);
997
998 iio_device_unregister(indio_dev);
999 iio_triggered_buffer_cleanup(indio_dev);
1000 if (client->irq > 0)
1001 iio_trigger_unregister(data->trig);
1002 kfree(data->buffer);
1003
1004 return 0;
1005}
1006
Vlad Dogaru7840ffe2015-04-03 15:47:29 +03001007#ifdef CONFIG_PM_SLEEP
1008static int sx9500_suspend(struct device *dev)
1009{
1010 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1011 struct sx9500_data *data = iio_priv(indio_dev);
1012 int ret;
1013
1014 mutex_lock(&data->mutex);
1015 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0,
1016 &data->suspend_ctrl0);
1017 if (ret < 0)
1018 goto out;
1019
1020 /*
1021 * Scan period doesn't matter because when all the sensors are
1022 * deactivated the device is in sleep mode.
1023 */
1024 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0);
1025
1026out:
1027 mutex_unlock(&data->mutex);
1028 return ret;
1029}
1030
1031static int sx9500_resume(struct device *dev)
1032{
1033 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1034 struct sx9500_data *data = iio_priv(indio_dev);
1035 int ret;
1036
1037 mutex_lock(&data->mutex);
1038 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0,
1039 data->suspend_ctrl0);
1040 mutex_unlock(&data->mutex);
1041
1042 return ret;
1043}
1044#endif /* CONFIG_PM_SLEEP */
1045
1046static const struct dev_pm_ops sx9500_pm_ops = {
1047 SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend, sx9500_resume)
1048};
1049
Vlad Dogaru4193c0f2014-12-29 14:41:14 +02001050static const struct acpi_device_id sx9500_acpi_match[] = {
1051 {"SSX9500", 0},
Andy Shevchenkob8793552017-11-03 15:03:39 +02001052 {"SASX9500", 0},
Vlad Dogaru4193c0f2014-12-29 14:41:14 +02001053 { },
1054};
1055MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match);
1056
Christoph Fritza5c8b112016-09-03 12:30:00 +02001057static const struct of_device_id sx9500_of_match[] = {
1058 { .compatible = "semtech,sx9500", },
1059 { }
1060};
1061MODULE_DEVICE_TABLE(of, sx9500_of_match);
1062
Vlad Dogaru4193c0f2014-12-29 14:41:14 +02001063static const struct i2c_device_id sx9500_id[] = {
1064 {"sx9500", 0},
Vlad Dogarua40c0ac2015-04-03 15:47:34 +03001065 { },
Vlad Dogaru4193c0f2014-12-29 14:41:14 +02001066};
1067MODULE_DEVICE_TABLE(i2c, sx9500_id);
1068
1069static struct i2c_driver sx9500_driver = {
1070 .driver = {
1071 .name = SX9500_DRIVER_NAME,
1072 .acpi_match_table = ACPI_PTR(sx9500_acpi_match),
Christoph Fritza5c8b112016-09-03 12:30:00 +02001073 .of_match_table = of_match_ptr(sx9500_of_match),
Vlad Dogaru7840ffe2015-04-03 15:47:29 +03001074 .pm = &sx9500_pm_ops,
Vlad Dogaru4193c0f2014-12-29 14:41:14 +02001075 },
1076 .probe = sx9500_probe,
1077 .remove = sx9500_remove,
1078 .id_table = sx9500_id,
1079};
1080module_i2c_driver(sx9500_driver);
1081
1082MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1083MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
1084MODULE_LICENSE("GPL v2");