2 * Copyright (c) 2014 Intel Corporation
4 * Driver for Semtech's SX9500 capacitive proximity/button solution.
5 * Datasheet available at
6 * <http://www.semtech.com/images/datasheet/sx9500.pdf>.
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.
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/irq.h>
18 #include <linux/acpi.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/regmap.h>
22 #include <linux/delay.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/events.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/iio/trigger_consumer.h>
32 #define SX9500_DRIVER_NAME "sx9500"
33 #define SX9500_IRQ_NAME "sx9500_event"
35 /* Register definitions. */
36 #define SX9500_REG_IRQ_SRC 0x00
37 #define SX9500_REG_STAT 0x01
38 #define SX9500_REG_IRQ_MSK 0x03
40 #define SX9500_REG_PROX_CTRL0 0x06
41 #define SX9500_REG_PROX_CTRL1 0x07
42 #define SX9500_REG_PROX_CTRL2 0x08
43 #define SX9500_REG_PROX_CTRL3 0x09
44 #define SX9500_REG_PROX_CTRL4 0x0a
45 #define SX9500_REG_PROX_CTRL5 0x0b
46 #define SX9500_REG_PROX_CTRL6 0x0c
47 #define SX9500_REG_PROX_CTRL7 0x0d
48 #define SX9500_REG_PROX_CTRL8 0x0e
50 #define SX9500_REG_SENSOR_SEL 0x20
51 #define SX9500_REG_USE_MSB 0x21
52 #define SX9500_REG_USE_LSB 0x22
53 #define SX9500_REG_AVG_MSB 0x23
54 #define SX9500_REG_AVG_LSB 0x24
55 #define SX9500_REG_DIFF_MSB 0x25
56 #define SX9500_REG_DIFF_LSB 0x26
57 #define SX9500_REG_OFFSET_MSB 0x27
58 #define SX9500_REG_OFFSET_LSB 0x28
60 #define SX9500_REG_RESET 0x7f
62 /* Write this to REG_RESET to do a soft reset. */
63 #define SX9500_SOFT_RESET 0xde
65 #define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4)
66 #define SX9500_SCAN_PERIOD_SHIFT 4
69 * These serve for identifying IRQ source in the IRQ_SRC register, and
70 * also for masking the IRQs in the IRQ_MSK register.
72 #define SX9500_CLOSE_IRQ BIT(6)
73 #define SX9500_FAR_IRQ BIT(5)
74 #define SX9500_CONVDONE_IRQ BIT(3)
76 #define SX9500_PROXSTAT_SHIFT 4
77 #define SX9500_COMPSTAT_MASK GENMASK(3, 0)
79 #define SX9500_NUM_CHANNELS 4
80 #define SX9500_CHAN_MASK GENMASK(SX9500_NUM_CHANNELS - 1, 0)
84 struct i2c_client
*client
;
85 struct iio_trigger
*trig
;
86 struct regmap
*regmap
;
87 struct gpio_desc
*gpiod_rst
;
89 * Last reading of the proximity status for each channel. We
90 * only send an event to user space when this changes.
92 bool prox_stat
[SX9500_NUM_CHANNELS
];
93 bool event_enabled
[SX9500_NUM_CHANNELS
];
96 /* Remember enabled channels and sample rate during suspend. */
97 unsigned int suspend_ctrl0
;
98 struct completion completion
;
99 int data_rdy_users
, close_far_users
;
100 int channel_users
[SX9500_NUM_CHANNELS
];
103 static const struct iio_event_spec sx9500_events
[] = {
105 .type
= IIO_EV_TYPE_THRESH
,
106 .dir
= IIO_EV_DIR_EITHER
,
107 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
111 #define SX9500_CHANNEL(idx) \
113 .type = IIO_PROXIMITY, \
114 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
115 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
118 .event_spec = sx9500_events, \
119 .num_event_specs = ARRAY_SIZE(sx9500_events), \
129 static const struct iio_chan_spec sx9500_channels
[] = {
134 IIO_CHAN_SOFT_TIMESTAMP(4),
137 static const struct {
140 } sx9500_samp_freq_table
[] = {
151 static const unsigned int sx9500_scan_period_table
[] = {
152 30, 60, 90, 120, 150, 200, 300, 400,
155 static const struct regmap_range sx9500_writable_reg_ranges
[] = {
156 regmap_reg_range(SX9500_REG_IRQ_MSK
, SX9500_REG_IRQ_MSK
),
157 regmap_reg_range(SX9500_REG_PROX_CTRL0
, SX9500_REG_PROX_CTRL8
),
158 regmap_reg_range(SX9500_REG_SENSOR_SEL
, SX9500_REG_SENSOR_SEL
),
159 regmap_reg_range(SX9500_REG_OFFSET_MSB
, SX9500_REG_OFFSET_LSB
),
160 regmap_reg_range(SX9500_REG_RESET
, SX9500_REG_RESET
),
163 static const struct regmap_access_table sx9500_writeable_regs
= {
164 .yes_ranges
= sx9500_writable_reg_ranges
,
165 .n_yes_ranges
= ARRAY_SIZE(sx9500_writable_reg_ranges
),
169 * All allocated registers are readable, so we just list unallocated
172 static const struct regmap_range sx9500_non_readable_reg_ranges
[] = {
173 regmap_reg_range(SX9500_REG_STAT
+ 1, SX9500_REG_STAT
+ 1),
174 regmap_reg_range(SX9500_REG_IRQ_MSK
+ 1, SX9500_REG_PROX_CTRL0
- 1),
175 regmap_reg_range(SX9500_REG_PROX_CTRL8
+ 1, SX9500_REG_SENSOR_SEL
- 1),
176 regmap_reg_range(SX9500_REG_OFFSET_LSB
+ 1, SX9500_REG_RESET
- 1),
179 static const struct regmap_access_table sx9500_readable_regs
= {
180 .no_ranges
= sx9500_non_readable_reg_ranges
,
181 .n_no_ranges
= ARRAY_SIZE(sx9500_non_readable_reg_ranges
),
184 static const struct regmap_range sx9500_volatile_reg_ranges
[] = {
185 regmap_reg_range(SX9500_REG_IRQ_SRC
, SX9500_REG_STAT
),
186 regmap_reg_range(SX9500_REG_USE_MSB
, SX9500_REG_OFFSET_LSB
),
187 regmap_reg_range(SX9500_REG_RESET
, SX9500_REG_RESET
),
190 static const struct regmap_access_table sx9500_volatile_regs
= {
191 .yes_ranges
= sx9500_volatile_reg_ranges
,
192 .n_yes_ranges
= ARRAY_SIZE(sx9500_volatile_reg_ranges
),
195 static const struct regmap_config sx9500_regmap_config
= {
199 .max_register
= SX9500_REG_RESET
,
200 .cache_type
= REGCACHE_RBTREE
,
202 .wr_table
= &sx9500_writeable_regs
,
203 .rd_table
= &sx9500_readable_regs
,
204 .volatile_table
= &sx9500_volatile_regs
,
207 static int sx9500_inc_users(struct sx9500_data
*data
, int *counter
,
208 unsigned int reg
, unsigned int bitmask
)
212 /* Bit is already active, nothing to do. */
215 return regmap_update_bits(data
->regmap
, reg
, bitmask
, bitmask
);
218 static int sx9500_dec_users(struct sx9500_data
*data
, int *counter
,
219 unsigned int reg
, unsigned int bitmask
)
223 /* There are more users, do not deactivate. */
226 return regmap_update_bits(data
->regmap
, reg
, bitmask
, 0);
229 static int sx9500_inc_chan_users(struct sx9500_data
*data
, int chan
)
231 return sx9500_inc_users(data
, &data
->channel_users
[chan
],
232 SX9500_REG_PROX_CTRL0
, BIT(chan
));
235 static int sx9500_dec_chan_users(struct sx9500_data
*data
, int chan
)
237 return sx9500_dec_users(data
, &data
->channel_users
[chan
],
238 SX9500_REG_PROX_CTRL0
, BIT(chan
));
241 static int sx9500_inc_data_rdy_users(struct sx9500_data
*data
)
243 return sx9500_inc_users(data
, &data
->data_rdy_users
,
244 SX9500_REG_IRQ_MSK
, SX9500_CONVDONE_IRQ
);
247 static int sx9500_dec_data_rdy_users(struct sx9500_data
*data
)
249 return sx9500_dec_users(data
, &data
->data_rdy_users
,
250 SX9500_REG_IRQ_MSK
, SX9500_CONVDONE_IRQ
);
253 static int sx9500_inc_close_far_users(struct sx9500_data
*data
)
255 return sx9500_inc_users(data
, &data
->close_far_users
,
257 SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
);
260 static int sx9500_dec_close_far_users(struct sx9500_data
*data
)
262 return sx9500_dec_users(data
, &data
->close_far_users
,
264 SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
);
267 static int sx9500_read_prox_data(struct sx9500_data
*data
,
268 const struct iio_chan_spec
*chan
,
274 ret
= regmap_write(data
->regmap
, SX9500_REG_SENSOR_SEL
, chan
->channel
);
278 ret
= regmap_bulk_read(data
->regmap
, SX9500_REG_USE_MSB
, ®val
, 2);
282 *val
= be16_to_cpu(regval
);
288 * If we have no interrupt support, we have to wait for a scan period
289 * after enabling a channel to get a result.
291 static int sx9500_wait_for_sample(struct sx9500_data
*data
)
296 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
, &val
);
300 val
= (val
& SX9500_SCAN_PERIOD_MASK
) >> SX9500_SCAN_PERIOD_SHIFT
;
302 msleep(sx9500_scan_period_table
[val
]);
307 static int sx9500_read_proximity(struct sx9500_data
*data
,
308 const struct iio_chan_spec
*chan
,
313 mutex_lock(&data
->mutex
);
315 ret
= sx9500_inc_chan_users(data
, chan
->channel
);
319 ret
= sx9500_inc_data_rdy_users(data
);
323 mutex_unlock(&data
->mutex
);
325 if (data
->client
->irq
> 0)
326 ret
= wait_for_completion_interruptible(&data
->completion
);
328 ret
= sx9500_wait_for_sample(data
);
330 mutex_lock(&data
->mutex
);
333 goto out_dec_data_rdy
;
335 ret
= sx9500_read_prox_data(data
, chan
, val
);
337 goto out_dec_data_rdy
;
339 ret
= sx9500_dec_data_rdy_users(data
);
343 ret
= sx9500_dec_chan_users(data
, chan
->channel
);
352 sx9500_dec_data_rdy_users(data
);
354 sx9500_dec_chan_users(data
, chan
->channel
);
356 mutex_unlock(&data
->mutex
);
357 reinit_completion(&data
->completion
);
362 static int sx9500_read_samp_freq(struct sx9500_data
*data
,
368 mutex_lock(&data
->mutex
);
369 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
, ®val
);
370 mutex_unlock(&data
->mutex
);
375 regval
= (regval
& SX9500_SCAN_PERIOD_MASK
) >> SX9500_SCAN_PERIOD_SHIFT
;
376 *val
= sx9500_samp_freq_table
[regval
].val
;
377 *val2
= sx9500_samp_freq_table
[regval
].val2
;
379 return IIO_VAL_INT_PLUS_MICRO
;
382 static int sx9500_read_raw(struct iio_dev
*indio_dev
,
383 const struct iio_chan_spec
*chan
,
384 int *val
, int *val2
, long mask
)
386 struct sx9500_data
*data
= iio_priv(indio_dev
);
389 switch (chan
->type
) {
392 case IIO_CHAN_INFO_RAW
:
393 ret
= iio_device_claim_direct_mode(indio_dev
);
396 ret
= sx9500_read_proximity(data
, chan
, val
);
397 iio_device_release_direct_mode(indio_dev
);
399 case IIO_CHAN_INFO_SAMP_FREQ
:
400 return sx9500_read_samp_freq(data
, val
, val2
);
409 static int sx9500_set_samp_freq(struct sx9500_data
*data
,
414 for (i
= 0; i
< ARRAY_SIZE(sx9500_samp_freq_table
); i
++)
415 if (val
== sx9500_samp_freq_table
[i
].val
&&
416 val2
== sx9500_samp_freq_table
[i
].val2
)
419 if (i
== ARRAY_SIZE(sx9500_samp_freq_table
))
422 mutex_lock(&data
->mutex
);
424 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
425 SX9500_SCAN_PERIOD_MASK
,
426 i
<< SX9500_SCAN_PERIOD_SHIFT
);
428 mutex_unlock(&data
->mutex
);
433 static int sx9500_write_raw(struct iio_dev
*indio_dev
,
434 const struct iio_chan_spec
*chan
,
435 int val
, int val2
, long mask
)
437 struct sx9500_data
*data
= iio_priv(indio_dev
);
439 switch (chan
->type
) {
442 case IIO_CHAN_INFO_SAMP_FREQ
:
443 return sx9500_set_samp_freq(data
, val
, val2
);
452 static irqreturn_t
sx9500_irq_handler(int irq
, void *private)
454 struct iio_dev
*indio_dev
= private;
455 struct sx9500_data
*data
= iio_priv(indio_dev
);
457 if (data
->trigger_enabled
)
458 iio_trigger_poll(data
->trig
);
461 * Even if no event is enabled, we need to wake the thread to
462 * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It
463 * is not possible to do that here because regmap_read takes a
466 return IRQ_WAKE_THREAD
;
469 static void sx9500_push_events(struct iio_dev
*indio_dev
)
472 unsigned int val
, chan
;
473 struct sx9500_data
*data
= iio_priv(indio_dev
);
475 ret
= regmap_read(data
->regmap
, SX9500_REG_STAT
, &val
);
477 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
481 val
>>= SX9500_PROXSTAT_SHIFT
;
482 for (chan
= 0; chan
< SX9500_NUM_CHANNELS
; chan
++) {
485 bool new_prox
= val
& BIT(chan
);
487 if (!data
->event_enabled
[chan
])
489 if (new_prox
== data
->prox_stat
[chan
])
490 /* No change on this channel. */
493 dir
= new_prox
? IIO_EV_DIR_FALLING
: IIO_EV_DIR_RISING
;
494 ev
= IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, chan
,
495 IIO_EV_TYPE_THRESH
, dir
);
496 iio_push_event(indio_dev
, ev
, iio_get_time_ns(indio_dev
));
497 data
->prox_stat
[chan
] = new_prox
;
501 static irqreturn_t
sx9500_irq_thread_handler(int irq
, void *private)
503 struct iio_dev
*indio_dev
= private;
504 struct sx9500_data
*data
= iio_priv(indio_dev
);
508 mutex_lock(&data
->mutex
);
510 ret
= regmap_read(data
->regmap
, SX9500_REG_IRQ_SRC
, &val
);
512 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
516 if (val
& (SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
))
517 sx9500_push_events(indio_dev
);
519 if (val
& SX9500_CONVDONE_IRQ
)
520 complete(&data
->completion
);
523 mutex_unlock(&data
->mutex
);
528 static int sx9500_read_event_config(struct iio_dev
*indio_dev
,
529 const struct iio_chan_spec
*chan
,
530 enum iio_event_type type
,
531 enum iio_event_direction dir
)
533 struct sx9500_data
*data
= iio_priv(indio_dev
);
535 if (chan
->type
!= IIO_PROXIMITY
|| type
!= IIO_EV_TYPE_THRESH
||
536 dir
!= IIO_EV_DIR_EITHER
)
539 return data
->event_enabled
[chan
->channel
];
542 static int sx9500_write_event_config(struct iio_dev
*indio_dev
,
543 const struct iio_chan_spec
*chan
,
544 enum iio_event_type type
,
545 enum iio_event_direction dir
,
548 struct sx9500_data
*data
= iio_priv(indio_dev
);
551 if (chan
->type
!= IIO_PROXIMITY
|| type
!= IIO_EV_TYPE_THRESH
||
552 dir
!= IIO_EV_DIR_EITHER
)
555 mutex_lock(&data
->mutex
);
558 ret
= sx9500_inc_chan_users(data
, chan
->channel
);
561 ret
= sx9500_inc_close_far_users(data
);
565 ret
= sx9500_dec_chan_users(data
, chan
->channel
);
568 ret
= sx9500_dec_close_far_users(data
);
573 data
->event_enabled
[chan
->channel
] = state
;
578 sx9500_dec_chan_users(data
, chan
->channel
);
580 sx9500_inc_chan_users(data
, chan
->channel
);
582 mutex_unlock(&data
->mutex
);
586 static int sx9500_update_scan_mode(struct iio_dev
*indio_dev
,
587 const unsigned long *scan_mask
)
589 struct sx9500_data
*data
= iio_priv(indio_dev
);
591 mutex_lock(&data
->mutex
);
593 data
->buffer
= kzalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
594 mutex_unlock(&data
->mutex
);
596 if (data
->buffer
== NULL
)
602 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
603 "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
605 static struct attribute
*sx9500_attributes
[] = {
606 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
610 static const struct attribute_group sx9500_attribute_group
= {
611 .attrs
= sx9500_attributes
,
614 static const struct iio_info sx9500_info
= {
615 .attrs
= &sx9500_attribute_group
,
616 .read_raw
= &sx9500_read_raw
,
617 .write_raw
= &sx9500_write_raw
,
618 .read_event_config
= &sx9500_read_event_config
,
619 .write_event_config
= &sx9500_write_event_config
,
620 .update_scan_mode
= &sx9500_update_scan_mode
,
623 static int sx9500_set_trigger_state(struct iio_trigger
*trig
,
626 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
627 struct sx9500_data
*data
= iio_priv(indio_dev
);
630 mutex_lock(&data
->mutex
);
633 ret
= sx9500_inc_data_rdy_users(data
);
635 ret
= sx9500_dec_data_rdy_users(data
);
639 data
->trigger_enabled
= state
;
642 mutex_unlock(&data
->mutex
);
647 static const struct iio_trigger_ops sx9500_trigger_ops
= {
648 .set_trigger_state
= sx9500_set_trigger_state
,
651 static irqreturn_t
sx9500_trigger_handler(int irq
, void *private)
653 struct iio_poll_func
*pf
= private;
654 struct iio_dev
*indio_dev
= pf
->indio_dev
;
655 struct sx9500_data
*data
= iio_priv(indio_dev
);
656 int val
, bit
, ret
, i
= 0;
658 mutex_lock(&data
->mutex
);
660 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
661 indio_dev
->masklength
) {
662 ret
= sx9500_read_prox_data(data
, &indio_dev
->channels
[bit
],
667 data
->buffer
[i
++] = val
;
670 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
671 iio_get_time_ns(indio_dev
));
674 mutex_unlock(&data
->mutex
);
676 iio_trigger_notify_done(indio_dev
->trig
);
681 static int sx9500_buffer_preenable(struct iio_dev
*indio_dev
)
683 struct sx9500_data
*data
= iio_priv(indio_dev
);
686 mutex_lock(&data
->mutex
);
688 for (i
= 0; i
< SX9500_NUM_CHANNELS
; i
++)
689 if (test_bit(i
, indio_dev
->active_scan_mask
)) {
690 ret
= sx9500_inc_chan_users(data
, i
);
696 for (i
= i
- 1; i
>= 0; i
--)
697 if (test_bit(i
, indio_dev
->active_scan_mask
))
698 sx9500_dec_chan_users(data
, i
);
700 mutex_unlock(&data
->mutex
);
705 static int sx9500_buffer_predisable(struct iio_dev
*indio_dev
)
707 struct sx9500_data
*data
= iio_priv(indio_dev
);
710 iio_triggered_buffer_predisable(indio_dev
);
712 mutex_lock(&data
->mutex
);
714 for (i
= 0; i
< SX9500_NUM_CHANNELS
; i
++)
715 if (test_bit(i
, indio_dev
->active_scan_mask
)) {
716 ret
= sx9500_dec_chan_users(data
, i
);
722 for (i
= i
- 1; i
>= 0; i
--)
723 if (test_bit(i
, indio_dev
->active_scan_mask
))
724 sx9500_inc_chan_users(data
, i
);
726 mutex_unlock(&data
->mutex
);
731 static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops
= {
732 .preenable
= sx9500_buffer_preenable
,
733 .postenable
= iio_triggered_buffer_postenable
,
734 .predisable
= sx9500_buffer_predisable
,
737 struct sx9500_reg_default
{
742 static const struct sx9500_reg_default sx9500_default_regs
[] = {
744 .reg
= SX9500_REG_PROX_CTRL1
,
745 /* Shield enabled, small range. */
749 .reg
= SX9500_REG_PROX_CTRL2
,
750 /* x8 gain, 167kHz frequency, finest resolution. */
754 .reg
= SX9500_REG_PROX_CTRL3
,
755 /* Doze enabled, 2x scan period doze, no raw filter. */
759 .reg
= SX9500_REG_PROX_CTRL4
,
760 /* Average threshold. */
764 .reg
= SX9500_REG_PROX_CTRL5
,
766 * Debouncer off, lowest average negative filter,
767 * highest average postive filter.
772 .reg
= SX9500_REG_PROX_CTRL6
,
773 /* Proximity detection threshold: 280 */
777 .reg
= SX9500_REG_PROX_CTRL7
,
779 * No automatic compensation, compensate each pin
780 * independently, proximity hysteresis: 32, close
781 * debouncer off, far debouncer off.
786 .reg
= SX9500_REG_PROX_CTRL8
,
787 /* No stuck timeout, no periodic compensation. */
791 .reg
= SX9500_REG_PROX_CTRL0
,
792 /* Scan period: 30ms, all sensors disabled. */
797 /* Activate all channels and perform an initial compensation. */
798 static int sx9500_init_compensation(struct iio_dev
*indio_dev
)
800 struct sx9500_data
*data
= iio_priv(indio_dev
);
804 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
805 SX9500_CHAN_MASK
, SX9500_CHAN_MASK
);
809 for (i
= 10; i
>= 0; i
--) {
810 usleep_range(10000, 20000);
811 ret
= regmap_read(data
->regmap
, SX9500_REG_STAT
, &val
);
814 if (!(val
& SX9500_COMPSTAT_MASK
))
819 dev_err(&data
->client
->dev
, "initial compensation timed out");
824 regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
825 SX9500_CHAN_MASK
, 0);
829 static int sx9500_init_device(struct iio_dev
*indio_dev
)
831 struct sx9500_data
*data
= iio_priv(indio_dev
);
835 if (data
->gpiod_rst
) {
836 gpiod_set_value_cansleep(data
->gpiod_rst
, 0);
837 usleep_range(1000, 2000);
838 gpiod_set_value_cansleep(data
->gpiod_rst
, 1);
839 usleep_range(1000, 2000);
842 ret
= regmap_write(data
->regmap
, SX9500_REG_IRQ_MSK
, 0);
846 ret
= regmap_write(data
->regmap
, SX9500_REG_RESET
,
851 ret
= regmap_read(data
->regmap
, SX9500_REG_IRQ_SRC
, &val
);
855 for (i
= 0; i
< ARRAY_SIZE(sx9500_default_regs
); i
++) {
856 ret
= regmap_write(data
->regmap
,
857 sx9500_default_regs
[i
].reg
,
858 sx9500_default_regs
[i
].def
);
863 return sx9500_init_compensation(indio_dev
);
866 static const struct acpi_gpio_params reset_gpios
= { 0, 0, false };
867 static const struct acpi_gpio_params interrupt_gpios
= { 2, 0, false };
869 static const struct acpi_gpio_mapping acpi_sx9500_gpios
[] = {
870 { "reset-gpios", &reset_gpios
, 1 },
872 * Some platforms have a bug in ACPI GPIO description making IRQ
873 * GPIO to be output only. Ask the GPIO core to ignore this limit.
875 { "interrupt-gpios", &interrupt_gpios
, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION
},
879 static void sx9500_gpio_probe(struct i2c_client
*client
,
880 struct sx9500_data
*data
)
882 struct gpio_desc
*gpiod_int
;
891 ret
= devm_acpi_dev_add_driver_gpios(dev
, acpi_sx9500_gpios
);
893 dev_dbg(dev
, "Unable to add GPIO mapping table\n");
895 if (client
->irq
<= 0) {
896 gpiod_int
= devm_gpiod_get(dev
, "interrupt", GPIOD_IN
);
897 if (IS_ERR(gpiod_int
))
898 dev_err(dev
, "gpio get irq failed\n");
900 client
->irq
= gpiod_to_irq(gpiod_int
);
903 data
->gpiod_rst
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
904 if (IS_ERR(data
->gpiod_rst
)) {
905 dev_warn(dev
, "gpio get reset pin failed\n");
906 data
->gpiod_rst
= NULL
;
910 static int sx9500_probe(struct i2c_client
*client
,
911 const struct i2c_device_id
*id
)
914 struct iio_dev
*indio_dev
;
915 struct sx9500_data
*data
;
917 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
918 if (indio_dev
== NULL
)
921 data
= iio_priv(indio_dev
);
922 data
->client
= client
;
923 mutex_init(&data
->mutex
);
924 init_completion(&data
->completion
);
925 data
->trigger_enabled
= false;
927 data
->regmap
= devm_regmap_init_i2c(client
, &sx9500_regmap_config
);
928 if (IS_ERR(data
->regmap
))
929 return PTR_ERR(data
->regmap
);
931 indio_dev
->dev
.parent
= &client
->dev
;
932 indio_dev
->name
= SX9500_DRIVER_NAME
;
933 indio_dev
->channels
= sx9500_channels
;
934 indio_dev
->num_channels
= ARRAY_SIZE(sx9500_channels
);
935 indio_dev
->info
= &sx9500_info
;
936 indio_dev
->modes
= INDIO_DIRECT_MODE
;
937 i2c_set_clientdata(client
, indio_dev
);
939 sx9500_gpio_probe(client
, data
);
941 ret
= sx9500_init_device(indio_dev
);
945 if (client
->irq
<= 0)
946 dev_warn(&client
->dev
, "no valid irq found\n");
948 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
949 sx9500_irq_handler
, sx9500_irq_thread_handler
,
950 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
951 SX9500_IRQ_NAME
, indio_dev
);
955 data
->trig
= devm_iio_trigger_alloc(&client
->dev
,
956 "%s-dev%d", indio_dev
->name
, indio_dev
->id
);
960 data
->trig
->dev
.parent
= &client
->dev
;
961 data
->trig
->ops
= &sx9500_trigger_ops
;
962 iio_trigger_set_drvdata(data
->trig
, indio_dev
);
964 ret
= iio_trigger_register(data
->trig
);
969 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
970 sx9500_trigger_handler
,
971 &sx9500_buffer_setup_ops
);
973 goto out_trigger_unregister
;
975 ret
= iio_device_register(indio_dev
);
977 goto out_buffer_cleanup
;
982 iio_triggered_buffer_cleanup(indio_dev
);
983 out_trigger_unregister
:
985 iio_trigger_unregister(data
->trig
);
990 static int sx9500_remove(struct i2c_client
*client
)
992 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
993 struct sx9500_data
*data
= iio_priv(indio_dev
);
995 iio_device_unregister(indio_dev
);
996 iio_triggered_buffer_cleanup(indio_dev
);
998 iio_trigger_unregister(data
->trig
);
1004 #ifdef CONFIG_PM_SLEEP
1005 static int sx9500_suspend(struct device
*dev
)
1007 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1008 struct sx9500_data
*data
= iio_priv(indio_dev
);
1011 mutex_lock(&data
->mutex
);
1012 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
,
1013 &data
->suspend_ctrl0
);
1018 * Scan period doesn't matter because when all the sensors are
1019 * deactivated the device is in sleep mode.
1021 ret
= regmap_write(data
->regmap
, SX9500_REG_PROX_CTRL0
, 0);
1024 mutex_unlock(&data
->mutex
);
1028 static int sx9500_resume(struct device
*dev
)
1030 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1031 struct sx9500_data
*data
= iio_priv(indio_dev
);
1034 mutex_lock(&data
->mutex
);
1035 ret
= regmap_write(data
->regmap
, SX9500_REG_PROX_CTRL0
,
1036 data
->suspend_ctrl0
);
1037 mutex_unlock(&data
->mutex
);
1041 #endif /* CONFIG_PM_SLEEP */
1043 static const struct dev_pm_ops sx9500_pm_ops
= {
1044 SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend
, sx9500_resume
)
1047 static const struct acpi_device_id sx9500_acpi_match
[] = {
1052 MODULE_DEVICE_TABLE(acpi
, sx9500_acpi_match
);
1054 static const struct of_device_id sx9500_of_match
[] = {
1055 { .compatible
= "semtech,sx9500", },
1058 MODULE_DEVICE_TABLE(of
, sx9500_of_match
);
1060 static const struct i2c_device_id sx9500_id
[] = {
1064 MODULE_DEVICE_TABLE(i2c
, sx9500_id
);
1066 static struct i2c_driver sx9500_driver
= {
1068 .name
= SX9500_DRIVER_NAME
,
1069 .acpi_match_table
= ACPI_PTR(sx9500_acpi_match
),
1070 .of_match_table
= of_match_ptr(sx9500_of_match
),
1071 .pm
= &sx9500_pm_ops
,
1073 .probe
= sx9500_probe
,
1074 .remove
= sx9500_remove
,
1075 .id_table
= sx9500_id
,
1077 module_i2c_driver(sx9500_driver
);
1079 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1080 MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
1081 MODULE_LICENSE("GPL v2");