1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 Intel Corporation
5 * Driver for Semtech's SX9500 capacitive proximity/button solution.
6 * Datasheet available at
7 * <http://www.semtech.com/images/datasheet/sx9500.pdf>.
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>
19 #include <linux/delay.h>
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>
29 #define SX9500_DRIVER_NAME "sx9500"
30 #define SX9500_IRQ_NAME "sx9500_event"
32 /* Register definitions. */
33 #define SX9500_REG_IRQ_SRC 0x00
34 #define SX9500_REG_STAT 0x01
35 #define SX9500_REG_IRQ_MSK 0x03
37 #define SX9500_REG_PROX_CTRL0 0x06
38 #define SX9500_REG_PROX_CTRL1 0x07
39 #define SX9500_REG_PROX_CTRL2 0x08
40 #define SX9500_REG_PROX_CTRL3 0x09
41 #define SX9500_REG_PROX_CTRL4 0x0a
42 #define SX9500_REG_PROX_CTRL5 0x0b
43 #define SX9500_REG_PROX_CTRL6 0x0c
44 #define SX9500_REG_PROX_CTRL7 0x0d
45 #define SX9500_REG_PROX_CTRL8 0x0e
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
57 #define SX9500_REG_RESET 0x7f
59 /* Write this to REG_RESET to do a soft reset. */
60 #define SX9500_SOFT_RESET 0xde
62 #define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4)
63 #define SX9500_SCAN_PERIOD_SHIFT 4
66 * These serve for identifying IRQ source in the IRQ_SRC register, and
67 * also for masking the IRQs in the IRQ_MSK register.
69 #define SX9500_CLOSE_IRQ BIT(6)
70 #define SX9500_FAR_IRQ BIT(5)
71 #define SX9500_CONVDONE_IRQ BIT(3)
73 #define SX9500_PROXSTAT_SHIFT 4
74 #define SX9500_COMPSTAT_MASK GENMASK(3, 0)
76 #define SX9500_NUM_CHANNELS 4
77 #define SX9500_CHAN_MASK GENMASK(SX9500_NUM_CHANNELS - 1, 0)
81 struct i2c_client
*client
;
82 struct iio_trigger
*trig
;
83 struct regmap
*regmap
;
84 struct gpio_desc
*gpiod_rst
;
86 * Last reading of the proximity status for each channel. We
87 * only send an event to user space when this changes.
89 bool prox_stat
[SX9500_NUM_CHANNELS
];
90 bool event_enabled
[SX9500_NUM_CHANNELS
];
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
];
100 static const struct iio_event_spec sx9500_events
[] = {
102 .type
= IIO_EV_TYPE_THRESH
,
103 .dir
= IIO_EV_DIR_EITHER
,
104 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
108 #define SX9500_CHANNEL(idx) \
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), \
115 .event_spec = sx9500_events, \
116 .num_event_specs = ARRAY_SIZE(sx9500_events), \
126 static const struct iio_chan_spec sx9500_channels
[] = {
131 IIO_CHAN_SOFT_TIMESTAMP(4),
134 static const struct {
137 } sx9500_samp_freq_table
[] = {
148 static const unsigned int sx9500_scan_period_table
[] = {
149 30, 60, 90, 120, 150, 200, 300, 400,
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
),
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
),
166 * All allocated registers are readable, so we just list unallocated
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),
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
),
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
),
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
),
192 static const struct regmap_config sx9500_regmap_config
= {
196 .max_register
= SX9500_REG_RESET
,
197 .cache_type
= REGCACHE_RBTREE
,
199 .wr_table
= &sx9500_writeable_regs
,
200 .rd_table
= &sx9500_readable_regs
,
201 .volatile_table
= &sx9500_volatile_regs
,
204 static int sx9500_inc_users(struct sx9500_data
*data
, int *counter
,
205 unsigned int reg
, unsigned int bitmask
)
209 /* Bit is already active, nothing to do. */
212 return regmap_update_bits(data
->regmap
, reg
, bitmask
, bitmask
);
215 static int sx9500_dec_users(struct sx9500_data
*data
, int *counter
,
216 unsigned int reg
, unsigned int bitmask
)
220 /* There are more users, do not deactivate. */
223 return regmap_update_bits(data
->regmap
, reg
, bitmask
, 0);
226 static int sx9500_inc_chan_users(struct sx9500_data
*data
, int chan
)
228 return sx9500_inc_users(data
, &data
->channel_users
[chan
],
229 SX9500_REG_PROX_CTRL0
, BIT(chan
));
232 static int sx9500_dec_chan_users(struct sx9500_data
*data
, int chan
)
234 return sx9500_dec_users(data
, &data
->channel_users
[chan
],
235 SX9500_REG_PROX_CTRL0
, BIT(chan
));
238 static int sx9500_inc_data_rdy_users(struct sx9500_data
*data
)
240 return sx9500_inc_users(data
, &data
->data_rdy_users
,
241 SX9500_REG_IRQ_MSK
, SX9500_CONVDONE_IRQ
);
244 static int sx9500_dec_data_rdy_users(struct sx9500_data
*data
)
246 return sx9500_dec_users(data
, &data
->data_rdy_users
,
247 SX9500_REG_IRQ_MSK
, SX9500_CONVDONE_IRQ
);
250 static int sx9500_inc_close_far_users(struct sx9500_data
*data
)
252 return sx9500_inc_users(data
, &data
->close_far_users
,
254 SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
);
257 static int sx9500_dec_close_far_users(struct sx9500_data
*data
)
259 return sx9500_dec_users(data
, &data
->close_far_users
,
261 SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
);
264 static int sx9500_read_prox_data(struct sx9500_data
*data
,
265 const struct iio_chan_spec
*chan
,
271 ret
= regmap_write(data
->regmap
, SX9500_REG_SENSOR_SEL
, chan
->channel
);
275 ret
= regmap_bulk_read(data
->regmap
, SX9500_REG_USE_MSB
, ®val
, 2);
279 *val
= be16_to_cpu(regval
);
285 * If we have no interrupt support, we have to wait for a scan period
286 * after enabling a channel to get a result.
288 static int sx9500_wait_for_sample(struct sx9500_data
*data
)
293 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
, &val
);
297 val
= (val
& SX9500_SCAN_PERIOD_MASK
) >> SX9500_SCAN_PERIOD_SHIFT
;
299 msleep(sx9500_scan_period_table
[val
]);
304 static int sx9500_read_proximity(struct sx9500_data
*data
,
305 const struct iio_chan_spec
*chan
,
310 mutex_lock(&data
->mutex
);
312 ret
= sx9500_inc_chan_users(data
, chan
->channel
);
316 ret
= sx9500_inc_data_rdy_users(data
);
320 mutex_unlock(&data
->mutex
);
322 if (data
->client
->irq
> 0)
323 ret
= wait_for_completion_interruptible(&data
->completion
);
325 ret
= sx9500_wait_for_sample(data
);
327 mutex_lock(&data
->mutex
);
330 goto out_dec_data_rdy
;
332 ret
= sx9500_read_prox_data(data
, chan
, val
);
334 goto out_dec_data_rdy
;
336 ret
= sx9500_dec_data_rdy_users(data
);
340 ret
= sx9500_dec_chan_users(data
, chan
->channel
);
349 sx9500_dec_data_rdy_users(data
);
351 sx9500_dec_chan_users(data
, chan
->channel
);
353 mutex_unlock(&data
->mutex
);
354 reinit_completion(&data
->completion
);
359 static int sx9500_read_samp_freq(struct sx9500_data
*data
,
365 mutex_lock(&data
->mutex
);
366 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
, ®val
);
367 mutex_unlock(&data
->mutex
);
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
;
376 return IIO_VAL_INT_PLUS_MICRO
;
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
)
383 struct sx9500_data
*data
= iio_priv(indio_dev
);
386 switch (chan
->type
) {
389 case IIO_CHAN_INFO_RAW
:
390 ret
= iio_device_claim_direct_mode(indio_dev
);
393 ret
= sx9500_read_proximity(data
, chan
, val
);
394 iio_device_release_direct_mode(indio_dev
);
396 case IIO_CHAN_INFO_SAMP_FREQ
:
397 return sx9500_read_samp_freq(data
, val
, val2
);
406 static int sx9500_set_samp_freq(struct sx9500_data
*data
,
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
)
416 if (i
== ARRAY_SIZE(sx9500_samp_freq_table
))
419 mutex_lock(&data
->mutex
);
421 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
422 SX9500_SCAN_PERIOD_MASK
,
423 i
<< SX9500_SCAN_PERIOD_SHIFT
);
425 mutex_unlock(&data
->mutex
);
430 static int sx9500_write_raw(struct iio_dev
*indio_dev
,
431 const struct iio_chan_spec
*chan
,
432 int val
, int val2
, long mask
)
434 struct sx9500_data
*data
= iio_priv(indio_dev
);
436 switch (chan
->type
) {
439 case IIO_CHAN_INFO_SAMP_FREQ
:
440 return sx9500_set_samp_freq(data
, val
, val2
);
449 static irqreturn_t
sx9500_irq_handler(int irq
, void *private)
451 struct iio_dev
*indio_dev
= private;
452 struct sx9500_data
*data
= iio_priv(indio_dev
);
454 if (data
->trigger_enabled
)
455 iio_trigger_poll(data
->trig
);
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
463 return IRQ_WAKE_THREAD
;
466 static void sx9500_push_events(struct iio_dev
*indio_dev
)
469 unsigned int val
, chan
;
470 struct sx9500_data
*data
= iio_priv(indio_dev
);
472 ret
= regmap_read(data
->regmap
, SX9500_REG_STAT
, &val
);
474 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
478 val
>>= SX9500_PROXSTAT_SHIFT
;
479 for (chan
= 0; chan
< SX9500_NUM_CHANNELS
; chan
++) {
482 bool new_prox
= val
& BIT(chan
);
484 if (!data
->event_enabled
[chan
])
486 if (new_prox
== data
->prox_stat
[chan
])
487 /* No change on this channel. */
490 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
);
493 iio_push_event(indio_dev
, ev
, iio_get_time_ns(indio_dev
));
494 data
->prox_stat
[chan
] = new_prox
;
498 static irqreturn_t
sx9500_irq_thread_handler(int irq
, void *private)
500 struct iio_dev
*indio_dev
= private;
501 struct sx9500_data
*data
= iio_priv(indio_dev
);
505 mutex_lock(&data
->mutex
);
507 ret
= regmap_read(data
->regmap
, SX9500_REG_IRQ_SRC
, &val
);
509 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
513 if (val
& (SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
))
514 sx9500_push_events(indio_dev
);
516 if (val
& SX9500_CONVDONE_IRQ
)
517 complete(&data
->completion
);
520 mutex_unlock(&data
->mutex
);
525 static 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
)
530 struct sx9500_data
*data
= iio_priv(indio_dev
);
532 if (chan
->type
!= IIO_PROXIMITY
|| type
!= IIO_EV_TYPE_THRESH
||
533 dir
!= IIO_EV_DIR_EITHER
)
536 return data
->event_enabled
[chan
->channel
];
539 static 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
,
545 struct sx9500_data
*data
= iio_priv(indio_dev
);
548 if (chan
->type
!= IIO_PROXIMITY
|| type
!= IIO_EV_TYPE_THRESH
||
549 dir
!= IIO_EV_DIR_EITHER
)
552 mutex_lock(&data
->mutex
);
555 ret
= sx9500_inc_chan_users(data
, chan
->channel
);
558 ret
= sx9500_inc_close_far_users(data
);
562 ret
= sx9500_dec_chan_users(data
, chan
->channel
);
565 ret
= sx9500_dec_close_far_users(data
);
570 data
->event_enabled
[chan
->channel
] = state
;
575 sx9500_dec_chan_users(data
, chan
->channel
);
577 sx9500_inc_chan_users(data
, chan
->channel
);
579 mutex_unlock(&data
->mutex
);
583 static int sx9500_update_scan_mode(struct iio_dev
*indio_dev
,
584 const unsigned long *scan_mask
)
586 struct sx9500_data
*data
= iio_priv(indio_dev
);
588 mutex_lock(&data
->mutex
);
590 data
->buffer
= kzalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
591 mutex_unlock(&data
->mutex
);
593 if (data
->buffer
== NULL
)
599 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
600 "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
602 static struct attribute
*sx9500_attributes
[] = {
603 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
607 static const struct attribute_group sx9500_attribute_group
= {
608 .attrs
= sx9500_attributes
,
611 static const struct iio_info sx9500_info
= {
612 .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
,
620 static int sx9500_set_trigger_state(struct iio_trigger
*trig
,
623 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
624 struct sx9500_data
*data
= iio_priv(indio_dev
);
627 mutex_lock(&data
->mutex
);
630 ret
= sx9500_inc_data_rdy_users(data
);
632 ret
= sx9500_dec_data_rdy_users(data
);
636 data
->trigger_enabled
= state
;
639 mutex_unlock(&data
->mutex
);
644 static const struct iio_trigger_ops sx9500_trigger_ops
= {
645 .set_trigger_state
= sx9500_set_trigger_state
,
648 static irqreturn_t
sx9500_trigger_handler(int irq
, void *private)
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;
655 mutex_lock(&data
->mutex
);
657 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
658 indio_dev
->masklength
) {
659 ret
= sx9500_read_prox_data(data
, &indio_dev
->channels
[bit
],
664 data
->buffer
[i
++] = val
;
667 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
668 iio_get_time_ns(indio_dev
));
671 mutex_unlock(&data
->mutex
);
673 iio_trigger_notify_done(indio_dev
->trig
);
678 static int sx9500_buffer_preenable(struct iio_dev
*indio_dev
)
680 struct sx9500_data
*data
= iio_priv(indio_dev
);
683 mutex_lock(&data
->mutex
);
685 for (i
= 0; i
< SX9500_NUM_CHANNELS
; i
++)
686 if (test_bit(i
, indio_dev
->active_scan_mask
)) {
687 ret
= sx9500_inc_chan_users(data
, i
);
693 for (i
= i
- 1; i
>= 0; i
--)
694 if (test_bit(i
, indio_dev
->active_scan_mask
))
695 sx9500_dec_chan_users(data
, i
);
697 mutex_unlock(&data
->mutex
);
702 static int sx9500_buffer_predisable(struct iio_dev
*indio_dev
)
704 struct sx9500_data
*data
= iio_priv(indio_dev
);
707 iio_triggered_buffer_predisable(indio_dev
);
709 mutex_lock(&data
->mutex
);
711 for (i
= 0; i
< SX9500_NUM_CHANNELS
; i
++)
712 if (test_bit(i
, indio_dev
->active_scan_mask
)) {
713 ret
= sx9500_dec_chan_users(data
, i
);
719 for (i
= i
- 1; i
>= 0; i
--)
720 if (test_bit(i
, indio_dev
->active_scan_mask
))
721 sx9500_inc_chan_users(data
, i
);
723 mutex_unlock(&data
->mutex
);
728 static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops
= {
729 .preenable
= sx9500_buffer_preenable
,
730 .postenable
= iio_triggered_buffer_postenable
,
731 .predisable
= sx9500_buffer_predisable
,
734 struct sx9500_reg_default
{
739 static const struct sx9500_reg_default sx9500_default_regs
[] = {
741 .reg
= SX9500_REG_PROX_CTRL1
,
742 /* Shield enabled, small range. */
746 .reg
= SX9500_REG_PROX_CTRL2
,
747 /* x8 gain, 167kHz frequency, finest resolution. */
751 .reg
= SX9500_REG_PROX_CTRL3
,
752 /* Doze enabled, 2x scan period doze, no raw filter. */
756 .reg
= SX9500_REG_PROX_CTRL4
,
757 /* Average threshold. */
761 .reg
= SX9500_REG_PROX_CTRL5
,
763 * Debouncer off, lowest average negative filter,
764 * highest average postive filter.
769 .reg
= SX9500_REG_PROX_CTRL6
,
770 /* Proximity detection threshold: 280 */
774 .reg
= SX9500_REG_PROX_CTRL7
,
776 * No automatic compensation, compensate each pin
777 * independently, proximity hysteresis: 32, close
778 * debouncer off, far debouncer off.
783 .reg
= SX9500_REG_PROX_CTRL8
,
784 /* No stuck timeout, no periodic compensation. */
788 .reg
= SX9500_REG_PROX_CTRL0
,
789 /* Scan period: 30ms, all sensors disabled. */
794 /* Activate all channels and perform an initial compensation. */
795 static int sx9500_init_compensation(struct iio_dev
*indio_dev
)
797 struct sx9500_data
*data
= iio_priv(indio_dev
);
801 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
802 SX9500_CHAN_MASK
, SX9500_CHAN_MASK
);
806 for (i
= 10; i
>= 0; i
--) {
807 usleep_range(10000, 20000);
808 ret
= regmap_read(data
->regmap
, SX9500_REG_STAT
, &val
);
811 if (!(val
& SX9500_COMPSTAT_MASK
))
816 dev_err(&data
->client
->dev
, "initial compensation timed out");
821 regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
822 SX9500_CHAN_MASK
, 0);
826 static int sx9500_init_device(struct iio_dev
*indio_dev
)
828 struct sx9500_data
*data
= iio_priv(indio_dev
);
832 if (data
->gpiod_rst
) {
833 gpiod_set_value_cansleep(data
->gpiod_rst
, 0);
834 usleep_range(1000, 2000);
835 gpiod_set_value_cansleep(data
->gpiod_rst
, 1);
836 usleep_range(1000, 2000);
839 ret
= regmap_write(data
->regmap
, SX9500_REG_IRQ_MSK
, 0);
843 ret
= regmap_write(data
->regmap
, SX9500_REG_RESET
,
848 ret
= regmap_read(data
->regmap
, SX9500_REG_IRQ_SRC
, &val
);
852 for (i
= 0; i
< ARRAY_SIZE(sx9500_default_regs
); i
++) {
853 ret
= regmap_write(data
->regmap
,
854 sx9500_default_regs
[i
].reg
,
855 sx9500_default_regs
[i
].def
);
860 return sx9500_init_compensation(indio_dev
);
863 static const struct acpi_gpio_params reset_gpios
= { 0, 0, false };
864 static const struct acpi_gpio_params interrupt_gpios
= { 2, 0, false };
866 static const struct acpi_gpio_mapping acpi_sx9500_gpios
[] = {
867 { "reset-gpios", &reset_gpios
, 1 },
869 * Some platforms have a bug in ACPI GPIO description making IRQ
870 * GPIO to be output only. Ask the GPIO core to ignore this limit.
872 { "interrupt-gpios", &interrupt_gpios
, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION
},
876 static void sx9500_gpio_probe(struct i2c_client
*client
,
877 struct sx9500_data
*data
)
879 struct gpio_desc
*gpiod_int
;
888 ret
= devm_acpi_dev_add_driver_gpios(dev
, acpi_sx9500_gpios
);
890 dev_dbg(dev
, "Unable to add GPIO mapping table\n");
892 if (client
->irq
<= 0) {
893 gpiod_int
= devm_gpiod_get(dev
, "interrupt", GPIOD_IN
);
894 if (IS_ERR(gpiod_int
))
895 dev_err(dev
, "gpio get irq failed\n");
897 client
->irq
= gpiod_to_irq(gpiod_int
);
900 data
->gpiod_rst
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
901 if (IS_ERR(data
->gpiod_rst
)) {
902 dev_warn(dev
, "gpio get reset pin failed\n");
903 data
->gpiod_rst
= NULL
;
907 static int sx9500_probe(struct i2c_client
*client
,
908 const struct i2c_device_id
*id
)
911 struct iio_dev
*indio_dev
;
912 struct sx9500_data
*data
;
914 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
915 if (indio_dev
== NULL
)
918 data
= iio_priv(indio_dev
);
919 data
->client
= client
;
920 mutex_init(&data
->mutex
);
921 init_completion(&data
->completion
);
922 data
->trigger_enabled
= false;
924 data
->regmap
= devm_regmap_init_i2c(client
, &sx9500_regmap_config
);
925 if (IS_ERR(data
->regmap
))
926 return PTR_ERR(data
->regmap
);
928 indio_dev
->dev
.parent
= &client
->dev
;
929 indio_dev
->name
= SX9500_DRIVER_NAME
;
930 indio_dev
->channels
= sx9500_channels
;
931 indio_dev
->num_channels
= ARRAY_SIZE(sx9500_channels
);
932 indio_dev
->info
= &sx9500_info
;
933 indio_dev
->modes
= INDIO_DIRECT_MODE
;
934 i2c_set_clientdata(client
, indio_dev
);
936 sx9500_gpio_probe(client
, data
);
938 ret
= sx9500_init_device(indio_dev
);
942 if (client
->irq
<= 0)
943 dev_warn(&client
->dev
, "no valid irq found\n");
945 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
946 sx9500_irq_handler
, sx9500_irq_thread_handler
,
947 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
948 SX9500_IRQ_NAME
, indio_dev
);
952 data
->trig
= devm_iio_trigger_alloc(&client
->dev
,
953 "%s-dev%d", indio_dev
->name
, indio_dev
->id
);
957 data
->trig
->dev
.parent
= &client
->dev
;
958 data
->trig
->ops
= &sx9500_trigger_ops
;
959 iio_trigger_set_drvdata(data
->trig
, indio_dev
);
961 ret
= iio_trigger_register(data
->trig
);
966 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
967 sx9500_trigger_handler
,
968 &sx9500_buffer_setup_ops
);
970 goto out_trigger_unregister
;
972 ret
= iio_device_register(indio_dev
);
974 goto out_buffer_cleanup
;
979 iio_triggered_buffer_cleanup(indio_dev
);
980 out_trigger_unregister
:
982 iio_trigger_unregister(data
->trig
);
987 static int sx9500_remove(struct i2c_client
*client
)
989 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
990 struct sx9500_data
*data
= iio_priv(indio_dev
);
992 iio_device_unregister(indio_dev
);
993 iio_triggered_buffer_cleanup(indio_dev
);
995 iio_trigger_unregister(data
->trig
);
1001 #ifdef CONFIG_PM_SLEEP
1002 static int sx9500_suspend(struct device
*dev
)
1004 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1005 struct sx9500_data
*data
= iio_priv(indio_dev
);
1008 mutex_lock(&data
->mutex
);
1009 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
,
1010 &data
->suspend_ctrl0
);
1015 * Scan period doesn't matter because when all the sensors are
1016 * deactivated the device is in sleep mode.
1018 ret
= regmap_write(data
->regmap
, SX9500_REG_PROX_CTRL0
, 0);
1021 mutex_unlock(&data
->mutex
);
1025 static int sx9500_resume(struct device
*dev
)
1027 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1028 struct sx9500_data
*data
= iio_priv(indio_dev
);
1031 mutex_lock(&data
->mutex
);
1032 ret
= regmap_write(data
->regmap
, SX9500_REG_PROX_CTRL0
,
1033 data
->suspend_ctrl0
);
1034 mutex_unlock(&data
->mutex
);
1038 #endif /* CONFIG_PM_SLEEP */
1040 static const struct dev_pm_ops sx9500_pm_ops
= {
1041 SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend
, sx9500_resume
)
1044 static const struct acpi_device_id sx9500_acpi_match
[] = {
1049 MODULE_DEVICE_TABLE(acpi
, sx9500_acpi_match
);
1051 static const struct of_device_id sx9500_of_match
[] = {
1052 { .compatible
= "semtech,sx9500", },
1055 MODULE_DEVICE_TABLE(of
, sx9500_of_match
);
1057 static const struct i2c_device_id sx9500_id
[] = {
1061 MODULE_DEVICE_TABLE(i2c
, sx9500_id
);
1063 static struct i2c_driver sx9500_driver
= {
1065 .name
= SX9500_DRIVER_NAME
,
1066 .acpi_match_table
= ACPI_PTR(sx9500_acpi_match
),
1067 .of_match_table
= of_match_ptr(sx9500_of_match
),
1068 .pm
= &sx9500_pm_ops
,
1070 .probe
= sx9500_probe
,
1071 .remove
= sx9500_remove
,
1072 .id_table
= sx9500_id
,
1074 module_i2c_driver(sx9500_driver
);
1076 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1077 MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
1078 MODULE_LICENSE("GPL v2");