1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>.
8 */
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>
18 #include <linux/pm.h>
19 #include <linux/delay.h>
20
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"
31
32 /* 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
74 #define SX9500_COMPSTAT_MASK GENMASK(3, 0)
75
76 #define SX9500_NUM_CHANNELS 4
77 #define SX9500_CHAN_MASK GENMASK(SX9500_NUM_CHANNELS - 1, 0)
78
79 struct sx9500_data {
80 struct mutex mutex;
81 struct i2c_client *client;
82 struct iio_trigger *trig;
83 struct regmap *regmap;
84 struct gpio_desc *gpiod_rst;
85 /*
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;
93 /* Remember enabled channels and sample rate during suspend. */
94 unsigned int suspend_ctrl0;
95 struct completion completion;
96 int data_rdy_users, close_far_users;
97 int channel_users[SX9500_NUM_CHANNELS];
98 };
99
100 static 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
126 static 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
134 static 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
148 static const unsigned int sx9500_scan_period_table[] = {
149 30, 60, 90, 120, 150, 200, 300, 400,
150 };
151
152 static 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
160 static 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 */
169 static 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
176 static 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
181 static 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
187 static 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
192 static 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
sx9500_inc_users(struct sx9500_data * data,int * counter,unsigned int reg,unsigned int bitmask)204 static 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_set_bits(data->regmap, reg, bitmask);
213 }
214
sx9500_dec_users(struct sx9500_data * data,int * counter,unsigned int reg,unsigned int bitmask)215 static 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_clear_bits(data->regmap, reg, bitmask);
224 }
225
sx9500_inc_chan_users(struct sx9500_data * data,int chan)226 static 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
sx9500_dec_chan_users(struct sx9500_data * data,int chan)232 static 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
sx9500_inc_data_rdy_users(struct sx9500_data * data)238 static 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
sx9500_dec_data_rdy_users(struct sx9500_data * data)244 static 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
sx9500_inc_close_far_users(struct sx9500_data * data)250 static 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
sx9500_dec_close_far_users(struct sx9500_data * data)257 static 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
sx9500_read_prox_data(struct sx9500_data * data,const struct iio_chan_spec * chan,int * val)264 static int sx9500_read_prox_data(struct sx9500_data *data,
265 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, ®val, 2);
276 if (ret < 0)
277 return ret;
278
279 *val = be16_to_cpu(regval);
280
281 return IIO_VAL_INT;
282 }
283
284 /*
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 */
sx9500_wait_for_sample(struct sx9500_data * data)288 static 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
sx9500_read_proximity(struct sx9500_data * data,const struct iio_chan_spec * chan,int * val)304 static 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
327 mutex_lock(&data->mutex);
328
329 if (ret < 0)
330 goto out_dec_data_rdy;
331
332 ret = sx9500_read_prox_data(data, chan, val);
333 if (ret < 0)
334 goto out_dec_data_rdy;
335
336 ret = sx9500_dec_data_rdy_users(data);
337 if (ret < 0)
338 goto out_dec_chan;
339
340 ret = sx9500_dec_chan_users(data, chan->channel);
341 if (ret < 0)
342 goto out;
343
344 ret = IIO_VAL_INT;
345
346 goto out;
347
348 out_dec_data_rdy:
349 sx9500_dec_data_rdy_users(data);
350 out_dec_chan:
351 sx9500_dec_chan_users(data, chan->channel);
352 out:
353 mutex_unlock(&data->mutex);
354 reinit_completion(&data->completion);
355
356 return ret;
357 }
358
sx9500_read_samp_freq(struct sx9500_data * data,int * val,int * val2)359 static 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, ®val);
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
sx9500_read_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long mask)379 static 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);
384 int ret;
385
386 switch (chan->type) {
387 case IIO_PROXIMITY:
388 switch (mask) {
389 case IIO_CHAN_INFO_RAW:
390 if (!iio_device_claim_direct(indio_dev))
391 return -EBUSY;
392 ret = sx9500_read_proximity(data, chan, val);
393 iio_device_release_direct(indio_dev);
394 return ret;
395 case IIO_CHAN_INFO_SAMP_FREQ:
396 return sx9500_read_samp_freq(data, val, val2);
397 default:
398 return -EINVAL;
399 }
400 default:
401 return -EINVAL;
402 }
403 }
404
sx9500_set_samp_freq(struct sx9500_data * data,int val,int val2)405 static int sx9500_set_samp_freq(struct sx9500_data *data,
406 int val, int val2)
407 {
408 int i, ret;
409
410 for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++)
411 if (val == sx9500_samp_freq_table[i].val &&
412 val2 == sx9500_samp_freq_table[i].val2)
413 break;
414
415 if (i == ARRAY_SIZE(sx9500_samp_freq_table))
416 return -EINVAL;
417
418 mutex_lock(&data->mutex);
419
420 ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
421 SX9500_SCAN_PERIOD_MASK,
422 i << SX9500_SCAN_PERIOD_SHIFT);
423
424 mutex_unlock(&data->mutex);
425
426 return ret;
427 }
428
sx9500_write_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int val,int val2,long mask)429 static int sx9500_write_raw(struct iio_dev *indio_dev,
430 const struct iio_chan_spec *chan,
431 int val, int val2, long mask)
432 {
433 struct sx9500_data *data = iio_priv(indio_dev);
434
435 switch (chan->type) {
436 case IIO_PROXIMITY:
437 switch (mask) {
438 case IIO_CHAN_INFO_SAMP_FREQ:
439 return sx9500_set_samp_freq(data, val, val2);
440 default:
441 return -EINVAL;
442 }
443 default:
444 return -EINVAL;
445 }
446 }
447
sx9500_irq_handler(int irq,void * private)448 static irqreturn_t sx9500_irq_handler(int irq, void *private)
449 {
450 struct iio_dev *indio_dev = private;
451 struct sx9500_data *data = iio_priv(indio_dev);
452
453 if (data->trigger_enabled)
454 iio_trigger_poll(data->trig);
455
456 /*
457 * Even if no event is enabled, we need to wake the thread to
458 * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It
459 * is not possible to do that here because regmap_read takes a
460 * mutex.
461 */
462 return IRQ_WAKE_THREAD;
463 }
464
sx9500_push_events(struct iio_dev * indio_dev)465 static void sx9500_push_events(struct iio_dev *indio_dev)
466 {
467 int ret;
468 unsigned int val, chan;
469 struct sx9500_data *data = iio_priv(indio_dev);
470
471 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
472 if (ret < 0) {
473 dev_err(&data->client->dev, "i2c transfer error in irq\n");
474 return;
475 }
476
477 val >>= SX9500_PROXSTAT_SHIFT;
478 for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) {
479 int dir;
480 u64 ev;
481 bool new_prox = val & BIT(chan);
482
483 if (!data->event_enabled[chan])
484 continue;
485 if (new_prox == data->prox_stat[chan])
486 /* No change on this channel. */
487 continue;
488
489 dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
490 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
491 IIO_EV_TYPE_THRESH, dir);
492 iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
493 data->prox_stat[chan] = new_prox;
494 }
495 }
496
sx9500_irq_thread_handler(int irq,void * private)497 static irqreturn_t sx9500_irq_thread_handler(int irq, void *private)
498 {
499 struct iio_dev *indio_dev = private;
500 struct sx9500_data *data = iio_priv(indio_dev);
501 int ret;
502 unsigned int val;
503
504 mutex_lock(&data->mutex);
505
506 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
507 if (ret < 0) {
508 dev_err(&data->client->dev, "i2c transfer error in irq\n");
509 goto out;
510 }
511
512 if (val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ))
513 sx9500_push_events(indio_dev);
514
515 if (val & SX9500_CONVDONE_IRQ)
516 complete(&data->completion);
517
518 out:
519 mutex_unlock(&data->mutex);
520
521 return IRQ_HANDLED;
522 }
523
sx9500_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)524 static int sx9500_read_event_config(struct iio_dev *indio_dev,
525 const struct iio_chan_spec *chan,
526 enum iio_event_type type,
527 enum iio_event_direction dir)
528 {
529 struct sx9500_data *data = iio_priv(indio_dev);
530
531 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
532 dir != IIO_EV_DIR_EITHER)
533 return -EINVAL;
534
535 return data->event_enabled[chan->channel];
536 }
537
sx9500_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,bool state)538 static int sx9500_write_event_config(struct iio_dev *indio_dev,
539 const struct iio_chan_spec *chan,
540 enum iio_event_type type,
541 enum iio_event_direction dir,
542 bool state)
543 {
544 struct sx9500_data *data = iio_priv(indio_dev);
545 int ret;
546
547 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
548 dir != IIO_EV_DIR_EITHER)
549 return -EINVAL;
550
551 mutex_lock(&data->mutex);
552
553 if (state) {
554 ret = sx9500_inc_chan_users(data, chan->channel);
555 if (ret < 0)
556 goto out_unlock;
557 ret = sx9500_inc_close_far_users(data);
558 if (ret < 0)
559 goto out_undo_chan;
560 } else {
561 ret = sx9500_dec_chan_users(data, chan->channel);
562 if (ret < 0)
563 goto out_unlock;
564 ret = sx9500_dec_close_far_users(data);
565 if (ret < 0)
566 goto out_undo_chan;
567 }
568
569 data->event_enabled[chan->channel] = state;
570 goto out_unlock;
571
572 out_undo_chan:
573 if (state)
574 sx9500_dec_chan_users(data, chan->channel);
575 else
576 sx9500_inc_chan_users(data, chan->channel);
577 out_unlock:
578 mutex_unlock(&data->mutex);
579 return ret;
580 }
581
sx9500_update_scan_mode(struct iio_dev * indio_dev,const unsigned long * scan_mask)582 static int sx9500_update_scan_mode(struct iio_dev *indio_dev,
583 const unsigned long *scan_mask)
584 {
585 struct sx9500_data *data = iio_priv(indio_dev);
586
587 mutex_lock(&data->mutex);
588 kfree(data->buffer);
589 data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
590 mutex_unlock(&data->mutex);
591
592 if (data->buffer == NULL)
593 return -ENOMEM;
594
595 return 0;
596 }
597
598 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
599 "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
600
601 static struct attribute *sx9500_attributes[] = {
602 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
603 NULL,
604 };
605
606 static const struct attribute_group sx9500_attribute_group = {
607 .attrs = sx9500_attributes,
608 };
609
610 static const struct iio_info sx9500_info = {
611 .attrs = &sx9500_attribute_group,
612 .read_raw = &sx9500_read_raw,
613 .write_raw = &sx9500_write_raw,
614 .read_event_config = &sx9500_read_event_config,
615 .write_event_config = &sx9500_write_event_config,
616 .update_scan_mode = &sx9500_update_scan_mode,
617 };
618
sx9500_set_trigger_state(struct iio_trigger * trig,bool state)619 static int sx9500_set_trigger_state(struct iio_trigger *trig,
620 bool state)
621 {
622 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
623 struct sx9500_data *data = iio_priv(indio_dev);
624 int ret;
625
626 mutex_lock(&data->mutex);
627
628 if (state)
629 ret = sx9500_inc_data_rdy_users(data);
630 else
631 ret = sx9500_dec_data_rdy_users(data);
632 if (ret < 0)
633 goto out;
634
635 data->trigger_enabled = state;
636
637 out:
638 mutex_unlock(&data->mutex);
639
640 return ret;
641 }
642
643 static const struct iio_trigger_ops sx9500_trigger_ops = {
644 .set_trigger_state = sx9500_set_trigger_state,
645 };
646
sx9500_trigger_handler(int irq,void * private)647 static irqreturn_t sx9500_trigger_handler(int irq, void *private)
648 {
649 struct iio_poll_func *pf = private;
650 struct iio_dev *indio_dev = pf->indio_dev;
651 struct sx9500_data *data = iio_priv(indio_dev);
652 int val, bit, ret, i = 0;
653
654 mutex_lock(&data->mutex);
655
656 iio_for_each_active_channel(indio_dev, bit) {
657 ret = sx9500_read_prox_data(data, &indio_dev->channels[bit],
658 &val);
659 if (ret < 0)
660 goto out;
661
662 data->buffer[i++] = val;
663 }
664
665 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
666 iio_get_time_ns(indio_dev));
667
668 out:
669 mutex_unlock(&data->mutex);
670
671 iio_trigger_notify_done(indio_dev->trig);
672
673 return IRQ_HANDLED;
674 }
675
sx9500_buffer_postenable(struct iio_dev * indio_dev)676 static int sx9500_buffer_postenable(struct iio_dev *indio_dev)
677 {
678 struct sx9500_data *data = iio_priv(indio_dev);
679 int ret = 0, i;
680
681 mutex_lock(&data->mutex);
682
683 for (i = 0; i < SX9500_NUM_CHANNELS; i++)
684 if (test_bit(i, indio_dev->active_scan_mask)) {
685 ret = sx9500_inc_chan_users(data, i);
686 if (ret)
687 break;
688 }
689
690 if (ret)
691 for (i = i - 1; i >= 0; i--)
692 if (test_bit(i, indio_dev->active_scan_mask))
693 sx9500_dec_chan_users(data, i);
694
695 mutex_unlock(&data->mutex);
696
697 return ret;
698 }
699
sx9500_buffer_predisable(struct iio_dev * indio_dev)700 static int sx9500_buffer_predisable(struct iio_dev *indio_dev)
701 {
702 struct sx9500_data *data = iio_priv(indio_dev);
703 int ret = 0, i;
704
705 mutex_lock(&data->mutex);
706
707 for (i = 0; i < SX9500_NUM_CHANNELS; i++)
708 if (test_bit(i, indio_dev->active_scan_mask)) {
709 ret = sx9500_dec_chan_users(data, i);
710 if (ret)
711 break;
712 }
713
714 if (ret)
715 for (i = i - 1; i >= 0; i--)
716 if (test_bit(i, indio_dev->active_scan_mask))
717 sx9500_inc_chan_users(data, i);
718
719 mutex_unlock(&data->mutex);
720
721 return ret;
722 }
723
724 static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops = {
725 .postenable = sx9500_buffer_postenable,
726 .predisable = sx9500_buffer_predisable,
727 };
728
729 struct sx9500_reg_default {
730 u8 reg;
731 u8 def;
732 };
733
734 static const struct sx9500_reg_default sx9500_default_regs[] = {
735 {
736 .reg = SX9500_REG_PROX_CTRL1,
737 /* Shield enabled, small range. */
738 .def = 0x43,
739 },
740 {
741 .reg = SX9500_REG_PROX_CTRL2,
742 /* x8 gain, 167kHz frequency, finest resolution. */
743 .def = 0x77,
744 },
745 {
746 .reg = SX9500_REG_PROX_CTRL3,
747 /* Doze enabled, 2x scan period doze, no raw filter. */
748 .def = 0x40,
749 },
750 {
751 .reg = SX9500_REG_PROX_CTRL4,
752 /* Average threshold. */
753 .def = 0x30,
754 },
755 {
756 .reg = SX9500_REG_PROX_CTRL5,
757 /*
758 * Debouncer off, lowest average negative filter,
759 * highest average positive filter.
760 */
761 .def = 0x0f,
762 },
763 {
764 .reg = SX9500_REG_PROX_CTRL6,
765 /* Proximity detection threshold: 280 */
766 .def = 0x0e,
767 },
768 {
769 .reg = SX9500_REG_PROX_CTRL7,
770 /*
771 * No automatic compensation, compensate each pin
772 * independently, proximity hysteresis: 32, close
773 * debouncer off, far debouncer off.
774 */
775 .def = 0x00,
776 },
777 {
778 .reg = SX9500_REG_PROX_CTRL8,
779 /* No stuck timeout, no periodic compensation. */
780 .def = 0x00,
781 },
782 {
783 .reg = SX9500_REG_PROX_CTRL0,
784 /* Scan period: 30ms, all sensors disabled. */
785 .def = 0x00,
786 },
787 };
788
789 /* Activate all channels and perform an initial compensation. */
sx9500_init_compensation(struct iio_dev * indio_dev)790 static int sx9500_init_compensation(struct iio_dev *indio_dev)
791 {
792 struct sx9500_data *data = iio_priv(indio_dev);
793 int i, ret;
794 unsigned int val;
795
796 ret = regmap_set_bits(data->regmap, SX9500_REG_PROX_CTRL0,
797 SX9500_CHAN_MASK);
798 if (ret < 0)
799 return ret;
800
801 for (i = 10; i >= 0; i--) {
802 usleep_range(10000, 20000);
803 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
804 if (ret < 0)
805 goto out;
806 if (!(val & SX9500_COMPSTAT_MASK))
807 break;
808 }
809
810 if (i < 0) {
811 dev_err(&data->client->dev, "initial compensation timed out");
812 ret = -ETIMEDOUT;
813 }
814
815 out:
816 regmap_clear_bits(data->regmap, SX9500_REG_PROX_CTRL0,
817 SX9500_CHAN_MASK);
818 return ret;
819 }
820
sx9500_init_device(struct iio_dev * indio_dev)821 static int sx9500_init_device(struct iio_dev *indio_dev)
822 {
823 struct sx9500_data *data = iio_priv(indio_dev);
824 int ret, i;
825 unsigned int val;
826
827 if (data->gpiod_rst) {
828 gpiod_set_value_cansleep(data->gpiod_rst, 0);
829 usleep_range(1000, 2000);
830 gpiod_set_value_cansleep(data->gpiod_rst, 1);
831 usleep_range(1000, 2000);
832 }
833
834 ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0);
835 if (ret < 0)
836 return ret;
837
838 ret = regmap_write(data->regmap, SX9500_REG_RESET,
839 SX9500_SOFT_RESET);
840 if (ret < 0)
841 return ret;
842
843 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
844 if (ret < 0)
845 return ret;
846
847 for (i = 0; i < ARRAY_SIZE(sx9500_default_regs); i++) {
848 ret = regmap_write(data->regmap,
849 sx9500_default_regs[i].reg,
850 sx9500_default_regs[i].def);
851 if (ret < 0)
852 return ret;
853 }
854
855 return sx9500_init_compensation(indio_dev);
856 }
857
858 static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
859 static const struct acpi_gpio_params interrupt_gpios = { 2, 0, false };
860
861 static const struct acpi_gpio_mapping acpi_sx9500_gpios[] = {
862 { "reset-gpios", &reset_gpios, 1 },
863 /*
864 * Some platforms have a bug in ACPI GPIO description making IRQ
865 * GPIO to be output only. Ask the GPIO core to ignore this limit.
866 */
867 { "interrupt-gpios", &interrupt_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
868 { }
869 };
870
sx9500_gpio_probe(struct i2c_client * client,struct sx9500_data * data)871 static void sx9500_gpio_probe(struct i2c_client *client,
872 struct sx9500_data *data)
873 {
874 struct gpio_desc *gpiod_int;
875 struct device *dev;
876 int ret;
877
878 if (!client)
879 return;
880
881 dev = &client->dev;
882
883 ret = devm_acpi_dev_add_driver_gpios(dev, acpi_sx9500_gpios);
884 if (ret)
885 dev_dbg(dev, "Unable to add GPIO mapping table\n");
886
887 if (client->irq <= 0) {
888 gpiod_int = devm_gpiod_get(dev, "interrupt", GPIOD_IN);
889 if (IS_ERR(gpiod_int))
890 dev_err(dev, "gpio get irq failed\n");
891 else
892 client->irq = gpiod_to_irq(gpiod_int);
893 }
894
895 data->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
896 if (IS_ERR(data->gpiod_rst)) {
897 dev_warn(dev, "gpio get reset pin failed\n");
898 data->gpiod_rst = NULL;
899 }
900 }
901
sx9500_probe(struct i2c_client * client)902 static int sx9500_probe(struct i2c_client *client)
903 {
904 int ret;
905 struct iio_dev *indio_dev;
906 struct sx9500_data *data;
907
908 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
909 if (indio_dev == NULL)
910 return -ENOMEM;
911
912 data = iio_priv(indio_dev);
913 data->client = client;
914 mutex_init(&data->mutex);
915 init_completion(&data->completion);
916 data->trigger_enabled = false;
917
918 data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config);
919 if (IS_ERR(data->regmap))
920 return PTR_ERR(data->regmap);
921
922 indio_dev->name = SX9500_DRIVER_NAME;
923 indio_dev->channels = sx9500_channels;
924 indio_dev->num_channels = ARRAY_SIZE(sx9500_channels);
925 indio_dev->info = &sx9500_info;
926 indio_dev->modes = INDIO_DIRECT_MODE;
927 i2c_set_clientdata(client, indio_dev);
928
929 sx9500_gpio_probe(client, data);
930
931 ret = sx9500_init_device(indio_dev);
932 if (ret < 0)
933 return ret;
934
935 if (client->irq <= 0)
936 dev_warn(&client->dev, "no valid irq found\n");
937 else {
938 ret = devm_request_threaded_irq(&client->dev, client->irq,
939 sx9500_irq_handler, sx9500_irq_thread_handler,
940 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
941 SX9500_IRQ_NAME, indio_dev);
942 if (ret < 0)
943 return ret;
944
945 data->trig = devm_iio_trigger_alloc(&client->dev,
946 "%s-dev%d", indio_dev->name, iio_device_id(indio_dev));
947 if (!data->trig)
948 return -ENOMEM;
949
950 data->trig->ops = &sx9500_trigger_ops;
951 iio_trigger_set_drvdata(data->trig, indio_dev);
952
953 ret = iio_trigger_register(data->trig);
954 if (ret)
955 return ret;
956 }
957
958 ret = iio_triggered_buffer_setup(indio_dev, NULL,
959 sx9500_trigger_handler,
960 &sx9500_buffer_setup_ops);
961 if (ret < 0)
962 goto out_trigger_unregister;
963
964 ret = iio_device_register(indio_dev);
965 if (ret < 0)
966 goto out_buffer_cleanup;
967
968 return 0;
969
970 out_buffer_cleanup:
971 iio_triggered_buffer_cleanup(indio_dev);
972 out_trigger_unregister:
973 if (client->irq > 0)
974 iio_trigger_unregister(data->trig);
975
976 return ret;
977 }
978
sx9500_remove(struct i2c_client * client)979 static void sx9500_remove(struct i2c_client *client)
980 {
981 struct iio_dev *indio_dev = i2c_get_clientdata(client);
982 struct sx9500_data *data = iio_priv(indio_dev);
983
984 iio_device_unregister(indio_dev);
985 iio_triggered_buffer_cleanup(indio_dev);
986 if (client->irq > 0)
987 iio_trigger_unregister(data->trig);
988 kfree(data->buffer);
989 }
990
sx9500_suspend(struct device * dev)991 static int sx9500_suspend(struct device *dev)
992 {
993 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
994 struct sx9500_data *data = iio_priv(indio_dev);
995 int ret;
996
997 mutex_lock(&data->mutex);
998 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0,
999 &data->suspend_ctrl0);
1000 if (ret < 0)
1001 goto out;
1002
1003 /*
1004 * Scan period doesn't matter because when all the sensors are
1005 * deactivated the device is in sleep mode.
1006 */
1007 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0);
1008
1009 out:
1010 mutex_unlock(&data->mutex);
1011 return ret;
1012 }
1013
sx9500_resume(struct device * dev)1014 static int sx9500_resume(struct device *dev)
1015 {
1016 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1017 struct sx9500_data *data = iio_priv(indio_dev);
1018 int ret;
1019
1020 mutex_lock(&data->mutex);
1021 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0,
1022 data->suspend_ctrl0);
1023 mutex_unlock(&data->mutex);
1024
1025 return ret;
1026 }
1027
1028 static DEFINE_SIMPLE_DEV_PM_OPS(sx9500_pm_ops, sx9500_suspend, sx9500_resume);
1029
1030 static const struct acpi_device_id sx9500_acpi_match[] = {
1031 {"SSX9500", 0},
1032 {"SASX9500", 0},
1033 { }
1034 };
1035 MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match);
1036
1037 static const struct of_device_id sx9500_of_match[] = {
1038 { .compatible = "semtech,sx9500", },
1039 { }
1040 };
1041 MODULE_DEVICE_TABLE(of, sx9500_of_match);
1042
1043 static const struct i2c_device_id sx9500_id[] = {
1044 { "sx9500" },
1045 { }
1046 };
1047 MODULE_DEVICE_TABLE(i2c, sx9500_id);
1048
1049 static struct i2c_driver sx9500_driver = {
1050 .driver = {
1051 .name = SX9500_DRIVER_NAME,
1052 .acpi_match_table = sx9500_acpi_match,
1053 .of_match_table = sx9500_of_match,
1054 .pm = pm_sleep_ptr(&sx9500_pm_ops),
1055 },
1056 .probe = sx9500_probe,
1057 .remove = sx9500_remove,
1058 .id_table = sx9500_id,
1059 };
1060 module_i2c_driver(sx9500_driver);
1061
1062 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1063 MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
1064 MODULE_LICENSE("GPL v2");
1065