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 #define SX9500_GPIO_INT "interrupt"
36 #define SX9500_GPIO_RESET "reset"
38 /* Register definitions. */
39 #define SX9500_REG_IRQ_SRC 0x00
40 #define SX9500_REG_STAT 0x01
41 #define SX9500_REG_IRQ_MSK 0x03
43 #define SX9500_REG_PROX_CTRL0 0x06
44 #define SX9500_REG_PROX_CTRL1 0x07
45 #define SX9500_REG_PROX_CTRL2 0x08
46 #define SX9500_REG_PROX_CTRL3 0x09
47 #define SX9500_REG_PROX_CTRL4 0x0a
48 #define SX9500_REG_PROX_CTRL5 0x0b
49 #define SX9500_REG_PROX_CTRL6 0x0c
50 #define SX9500_REG_PROX_CTRL7 0x0d
51 #define SX9500_REG_PROX_CTRL8 0x0e
53 #define SX9500_REG_SENSOR_SEL 0x20
54 #define SX9500_REG_USE_MSB 0x21
55 #define SX9500_REG_USE_LSB 0x22
56 #define SX9500_REG_AVG_MSB 0x23
57 #define SX9500_REG_AVG_LSB 0x24
58 #define SX9500_REG_DIFF_MSB 0x25
59 #define SX9500_REG_DIFF_LSB 0x26
60 #define SX9500_REG_OFFSET_MSB 0x27
61 #define SX9500_REG_OFFSET_LSB 0x28
63 #define SX9500_REG_RESET 0x7f
65 /* Write this to REG_RESET to do a soft reset. */
66 #define SX9500_SOFT_RESET 0xde
68 #define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4)
69 #define SX9500_SCAN_PERIOD_SHIFT 4
72 * These serve for identifying IRQ source in the IRQ_SRC register, and
73 * also for masking the IRQs in the IRQ_MSK register.
75 #define SX9500_CLOSE_IRQ BIT(6)
76 #define SX9500_FAR_IRQ BIT(5)
77 #define SX9500_CONVDONE_IRQ BIT(3)
79 #define SX9500_PROXSTAT_SHIFT 4
80 #define SX9500_COMPSTAT_MASK GENMASK(3, 0)
82 #define SX9500_NUM_CHANNELS 4
83 #define SX9500_CHAN_MASK GENMASK(SX9500_NUM_CHANNELS - 1, 0)
87 struct i2c_client
*client
;
88 struct iio_trigger
*trig
;
89 struct regmap
*regmap
;
90 struct gpio_desc
*gpiod_rst
;
92 * Last reading of the proximity status for each channel. We
93 * only send an event to user space when this changes.
95 bool prox_stat
[SX9500_NUM_CHANNELS
];
96 bool event_enabled
[SX9500_NUM_CHANNELS
];
99 /* Remember enabled channels and sample rate during suspend. */
100 unsigned int suspend_ctrl0
;
101 struct completion completion
;
102 int data_rdy_users
, close_far_users
;
103 int channel_users
[SX9500_NUM_CHANNELS
];
106 static const struct iio_event_spec sx9500_events
[] = {
108 .type
= IIO_EV_TYPE_THRESH
,
109 .dir
= IIO_EV_DIR_EITHER
,
110 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
114 #define SX9500_CHANNEL(idx) \
116 .type = IIO_PROXIMITY, \
117 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
118 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
121 .event_spec = sx9500_events, \
122 .num_event_specs = ARRAY_SIZE(sx9500_events), \
132 static const struct iio_chan_spec sx9500_channels
[] = {
137 IIO_CHAN_SOFT_TIMESTAMP(4),
140 static const struct {
143 } sx9500_samp_freq_table
[] = {
154 static const unsigned int sx9500_scan_period_table
[] = {
155 30, 60, 90, 120, 150, 200, 300, 400,
158 static const struct regmap_range sx9500_writable_reg_ranges
[] = {
159 regmap_reg_range(SX9500_REG_IRQ_MSK
, SX9500_REG_IRQ_MSK
),
160 regmap_reg_range(SX9500_REG_PROX_CTRL0
, SX9500_REG_PROX_CTRL8
),
161 regmap_reg_range(SX9500_REG_SENSOR_SEL
, SX9500_REG_SENSOR_SEL
),
162 regmap_reg_range(SX9500_REG_OFFSET_MSB
, SX9500_REG_OFFSET_LSB
),
163 regmap_reg_range(SX9500_REG_RESET
, SX9500_REG_RESET
),
166 static const struct regmap_access_table sx9500_writeable_regs
= {
167 .yes_ranges
= sx9500_writable_reg_ranges
,
168 .n_yes_ranges
= ARRAY_SIZE(sx9500_writable_reg_ranges
),
172 * All allocated registers are readable, so we just list unallocated
175 static const struct regmap_range sx9500_non_readable_reg_ranges
[] = {
176 regmap_reg_range(SX9500_REG_STAT
+ 1, SX9500_REG_STAT
+ 1),
177 regmap_reg_range(SX9500_REG_IRQ_MSK
+ 1, SX9500_REG_PROX_CTRL0
- 1),
178 regmap_reg_range(SX9500_REG_PROX_CTRL8
+ 1, SX9500_REG_SENSOR_SEL
- 1),
179 regmap_reg_range(SX9500_REG_OFFSET_LSB
+ 1, SX9500_REG_RESET
- 1),
182 static const struct regmap_access_table sx9500_readable_regs
= {
183 .no_ranges
= sx9500_non_readable_reg_ranges
,
184 .n_no_ranges
= ARRAY_SIZE(sx9500_non_readable_reg_ranges
),
187 static const struct regmap_range sx9500_volatile_reg_ranges
[] = {
188 regmap_reg_range(SX9500_REG_IRQ_SRC
, SX9500_REG_STAT
),
189 regmap_reg_range(SX9500_REG_USE_MSB
, SX9500_REG_OFFSET_LSB
),
190 regmap_reg_range(SX9500_REG_RESET
, SX9500_REG_RESET
),
193 static const struct regmap_access_table sx9500_volatile_regs
= {
194 .yes_ranges
= sx9500_volatile_reg_ranges
,
195 .n_yes_ranges
= ARRAY_SIZE(sx9500_volatile_reg_ranges
),
198 static const struct regmap_config sx9500_regmap_config
= {
202 .max_register
= SX9500_REG_RESET
,
203 .cache_type
= REGCACHE_RBTREE
,
205 .wr_table
= &sx9500_writeable_regs
,
206 .rd_table
= &sx9500_readable_regs
,
207 .volatile_table
= &sx9500_volatile_regs
,
210 static int sx9500_inc_users(struct sx9500_data
*data
, int *counter
,
211 unsigned int reg
, unsigned int bitmask
)
215 /* Bit is already active, nothing to do. */
218 return regmap_update_bits(data
->regmap
, reg
, bitmask
, bitmask
);
221 static int sx9500_dec_users(struct sx9500_data
*data
, int *counter
,
222 unsigned int reg
, unsigned int bitmask
)
226 /* There are more users, do not deactivate. */
229 return regmap_update_bits(data
->regmap
, reg
, bitmask
, 0);
232 static int sx9500_inc_chan_users(struct sx9500_data
*data
, int chan
)
234 return sx9500_inc_users(data
, &data
->channel_users
[chan
],
235 SX9500_REG_PROX_CTRL0
, BIT(chan
));
238 static int sx9500_dec_chan_users(struct sx9500_data
*data
, int chan
)
240 return sx9500_dec_users(data
, &data
->channel_users
[chan
],
241 SX9500_REG_PROX_CTRL0
, BIT(chan
));
244 static int sx9500_inc_data_rdy_users(struct sx9500_data
*data
)
246 return sx9500_inc_users(data
, &data
->data_rdy_users
,
247 SX9500_REG_IRQ_MSK
, SX9500_CONVDONE_IRQ
);
250 static int sx9500_dec_data_rdy_users(struct sx9500_data
*data
)
252 return sx9500_dec_users(data
, &data
->data_rdy_users
,
253 SX9500_REG_IRQ_MSK
, SX9500_CONVDONE_IRQ
);
256 static int sx9500_inc_close_far_users(struct sx9500_data
*data
)
258 return sx9500_inc_users(data
, &data
->close_far_users
,
260 SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
);
263 static int sx9500_dec_close_far_users(struct sx9500_data
*data
)
265 return sx9500_dec_users(data
, &data
->close_far_users
,
267 SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
);
270 static int sx9500_read_prox_data(struct sx9500_data
*data
,
271 const struct iio_chan_spec
*chan
,
277 ret
= regmap_write(data
->regmap
, SX9500_REG_SENSOR_SEL
, chan
->channel
);
281 ret
= regmap_bulk_read(data
->regmap
, SX9500_REG_USE_MSB
, ®val
, 2);
285 *val
= be16_to_cpu(regval
);
291 * If we have no interrupt support, we have to wait for a scan period
292 * after enabling a channel to get a result.
294 static int sx9500_wait_for_sample(struct sx9500_data
*data
)
299 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
, &val
);
303 val
= (val
& SX9500_SCAN_PERIOD_MASK
) >> SX9500_SCAN_PERIOD_SHIFT
;
305 msleep(sx9500_scan_period_table
[val
]);
310 static int sx9500_read_proximity(struct sx9500_data
*data
,
311 const struct iio_chan_spec
*chan
,
316 mutex_lock(&data
->mutex
);
318 ret
= sx9500_inc_chan_users(data
, chan
->channel
);
322 ret
= sx9500_inc_data_rdy_users(data
);
326 mutex_unlock(&data
->mutex
);
328 if (data
->client
->irq
> 0)
329 ret
= wait_for_completion_interruptible(&data
->completion
);
331 ret
= sx9500_wait_for_sample(data
);
333 mutex_lock(&data
->mutex
);
336 goto out_dec_data_rdy
;
338 ret
= sx9500_read_prox_data(data
, chan
, val
);
340 goto out_dec_data_rdy
;
342 ret
= sx9500_dec_data_rdy_users(data
);
346 ret
= sx9500_dec_chan_users(data
, chan
->channel
);
355 sx9500_dec_data_rdy_users(data
);
357 sx9500_dec_chan_users(data
, chan
->channel
);
359 mutex_unlock(&data
->mutex
);
360 reinit_completion(&data
->completion
);
365 static int sx9500_read_samp_freq(struct sx9500_data
*data
,
371 mutex_lock(&data
->mutex
);
372 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
, ®val
);
373 mutex_unlock(&data
->mutex
);
378 regval
= (regval
& SX9500_SCAN_PERIOD_MASK
) >> SX9500_SCAN_PERIOD_SHIFT
;
379 *val
= sx9500_samp_freq_table
[regval
].val
;
380 *val2
= sx9500_samp_freq_table
[regval
].val2
;
382 return IIO_VAL_INT_PLUS_MICRO
;
385 static int sx9500_read_raw(struct iio_dev
*indio_dev
,
386 const struct iio_chan_spec
*chan
,
387 int *val
, int *val2
, long mask
)
389 struct sx9500_data
*data
= iio_priv(indio_dev
);
392 switch (chan
->type
) {
395 case IIO_CHAN_INFO_RAW
:
396 ret
= iio_device_claim_direct_mode(indio_dev
);
399 ret
= sx9500_read_proximity(data
, chan
, val
);
400 iio_device_release_direct_mode(indio_dev
);
402 case IIO_CHAN_INFO_SAMP_FREQ
:
403 return sx9500_read_samp_freq(data
, val
, val2
);
412 static int sx9500_set_samp_freq(struct sx9500_data
*data
,
417 for (i
= 0; i
< ARRAY_SIZE(sx9500_samp_freq_table
); i
++)
418 if (val
== sx9500_samp_freq_table
[i
].val
&&
419 val2
== sx9500_samp_freq_table
[i
].val2
)
422 if (i
== ARRAY_SIZE(sx9500_samp_freq_table
))
425 mutex_lock(&data
->mutex
);
427 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
428 SX9500_SCAN_PERIOD_MASK
,
429 i
<< SX9500_SCAN_PERIOD_SHIFT
);
431 mutex_unlock(&data
->mutex
);
436 static int sx9500_write_raw(struct iio_dev
*indio_dev
,
437 const struct iio_chan_spec
*chan
,
438 int val
, int val2
, long mask
)
440 struct sx9500_data
*data
= iio_priv(indio_dev
);
442 switch (chan
->type
) {
445 case IIO_CHAN_INFO_SAMP_FREQ
:
446 return sx9500_set_samp_freq(data
, val
, val2
);
455 static irqreturn_t
sx9500_irq_handler(int irq
, void *private)
457 struct iio_dev
*indio_dev
= private;
458 struct sx9500_data
*data
= iio_priv(indio_dev
);
460 if (data
->trigger_enabled
)
461 iio_trigger_poll(data
->trig
);
464 * Even if no event is enabled, we need to wake the thread to
465 * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It
466 * is not possible to do that here because regmap_read takes a
469 return IRQ_WAKE_THREAD
;
472 static void sx9500_push_events(struct iio_dev
*indio_dev
)
475 unsigned int val
, chan
;
476 struct sx9500_data
*data
= iio_priv(indio_dev
);
478 ret
= regmap_read(data
->regmap
, SX9500_REG_STAT
, &val
);
480 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
484 val
>>= SX9500_PROXSTAT_SHIFT
;
485 for (chan
= 0; chan
< SX9500_NUM_CHANNELS
; chan
++) {
488 bool new_prox
= val
& BIT(chan
);
490 if (!data
->event_enabled
[chan
])
492 if (new_prox
== data
->prox_stat
[chan
])
493 /* No change on this channel. */
496 dir
= new_prox
? IIO_EV_DIR_FALLING
: IIO_EV_DIR_RISING
;
497 ev
= IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, chan
,
498 IIO_EV_TYPE_THRESH
, dir
);
499 iio_push_event(indio_dev
, ev
, iio_get_time_ns(indio_dev
));
500 data
->prox_stat
[chan
] = new_prox
;
504 static irqreturn_t
sx9500_irq_thread_handler(int irq
, void *private)
506 struct iio_dev
*indio_dev
= private;
507 struct sx9500_data
*data
= iio_priv(indio_dev
);
511 mutex_lock(&data
->mutex
);
513 ret
= regmap_read(data
->regmap
, SX9500_REG_IRQ_SRC
, &val
);
515 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
519 if (val
& (SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
))
520 sx9500_push_events(indio_dev
);
522 if (val
& SX9500_CONVDONE_IRQ
)
523 complete(&data
->completion
);
526 mutex_unlock(&data
->mutex
);
531 static int sx9500_read_event_config(struct iio_dev
*indio_dev
,
532 const struct iio_chan_spec
*chan
,
533 enum iio_event_type type
,
534 enum iio_event_direction dir
)
536 struct sx9500_data
*data
= iio_priv(indio_dev
);
538 if (chan
->type
!= IIO_PROXIMITY
|| type
!= IIO_EV_TYPE_THRESH
||
539 dir
!= IIO_EV_DIR_EITHER
)
542 return data
->event_enabled
[chan
->channel
];
545 static int sx9500_write_event_config(struct iio_dev
*indio_dev
,
546 const struct iio_chan_spec
*chan
,
547 enum iio_event_type type
,
548 enum iio_event_direction dir
,
551 struct sx9500_data
*data
= iio_priv(indio_dev
);
554 if (chan
->type
!= IIO_PROXIMITY
|| type
!= IIO_EV_TYPE_THRESH
||
555 dir
!= IIO_EV_DIR_EITHER
)
558 mutex_lock(&data
->mutex
);
561 ret
= sx9500_inc_chan_users(data
, chan
->channel
);
564 ret
= sx9500_inc_close_far_users(data
);
568 ret
= sx9500_dec_chan_users(data
, chan
->channel
);
571 ret
= sx9500_dec_close_far_users(data
);
576 data
->event_enabled
[chan
->channel
] = state
;
581 sx9500_dec_chan_users(data
, chan
->channel
);
583 sx9500_inc_chan_users(data
, chan
->channel
);
585 mutex_unlock(&data
->mutex
);
589 static int sx9500_update_scan_mode(struct iio_dev
*indio_dev
,
590 const unsigned long *scan_mask
)
592 struct sx9500_data
*data
= iio_priv(indio_dev
);
594 mutex_lock(&data
->mutex
);
596 data
->buffer
= kzalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
597 mutex_unlock(&data
->mutex
);
599 if (data
->buffer
== NULL
)
605 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
606 "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
608 static struct attribute
*sx9500_attributes
[] = {
609 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
613 static const struct attribute_group sx9500_attribute_group
= {
614 .attrs
= sx9500_attributes
,
617 static const struct iio_info sx9500_info
= {
618 .driver_module
= THIS_MODULE
,
619 .attrs
= &sx9500_attribute_group
,
620 .read_raw
= &sx9500_read_raw
,
621 .write_raw
= &sx9500_write_raw
,
622 .read_event_config
= &sx9500_read_event_config
,
623 .write_event_config
= &sx9500_write_event_config
,
624 .update_scan_mode
= &sx9500_update_scan_mode
,
627 static int sx9500_set_trigger_state(struct iio_trigger
*trig
,
630 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
631 struct sx9500_data
*data
= iio_priv(indio_dev
);
634 mutex_lock(&data
->mutex
);
637 ret
= sx9500_inc_data_rdy_users(data
);
639 ret
= sx9500_dec_data_rdy_users(data
);
643 data
->trigger_enabled
= state
;
646 mutex_unlock(&data
->mutex
);
651 static const struct iio_trigger_ops sx9500_trigger_ops
= {
652 .set_trigger_state
= sx9500_set_trigger_state
,
653 .owner
= THIS_MODULE
,
656 static irqreturn_t
sx9500_trigger_handler(int irq
, void *private)
658 struct iio_poll_func
*pf
= private;
659 struct iio_dev
*indio_dev
= pf
->indio_dev
;
660 struct sx9500_data
*data
= iio_priv(indio_dev
);
661 int val
, bit
, ret
, i
= 0;
663 mutex_lock(&data
->mutex
);
665 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
666 indio_dev
->masklength
) {
667 ret
= sx9500_read_prox_data(data
, &indio_dev
->channels
[bit
],
672 data
->buffer
[i
++] = val
;
675 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
676 iio_get_time_ns(indio_dev
));
679 mutex_unlock(&data
->mutex
);
681 iio_trigger_notify_done(indio_dev
->trig
);
686 static int sx9500_buffer_preenable(struct iio_dev
*indio_dev
)
688 struct sx9500_data
*data
= iio_priv(indio_dev
);
691 mutex_lock(&data
->mutex
);
693 for (i
= 0; i
< SX9500_NUM_CHANNELS
; i
++)
694 if (test_bit(i
, indio_dev
->active_scan_mask
)) {
695 ret
= sx9500_inc_chan_users(data
, i
);
701 for (i
= i
- 1; i
>= 0; i
--)
702 if (test_bit(i
, indio_dev
->active_scan_mask
))
703 sx9500_dec_chan_users(data
, i
);
705 mutex_unlock(&data
->mutex
);
710 static int sx9500_buffer_predisable(struct iio_dev
*indio_dev
)
712 struct sx9500_data
*data
= iio_priv(indio_dev
);
715 iio_triggered_buffer_predisable(indio_dev
);
717 mutex_lock(&data
->mutex
);
719 for (i
= 0; i
< SX9500_NUM_CHANNELS
; i
++)
720 if (test_bit(i
, indio_dev
->active_scan_mask
)) {
721 ret
= sx9500_dec_chan_users(data
, i
);
727 for (i
= i
- 1; i
>= 0; i
--)
728 if (test_bit(i
, indio_dev
->active_scan_mask
))
729 sx9500_inc_chan_users(data
, i
);
731 mutex_unlock(&data
->mutex
);
736 static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops
= {
737 .preenable
= sx9500_buffer_preenable
,
738 .postenable
= iio_triggered_buffer_postenable
,
739 .predisable
= sx9500_buffer_predisable
,
742 struct sx9500_reg_default
{
747 static const struct sx9500_reg_default sx9500_default_regs
[] = {
749 .reg
= SX9500_REG_PROX_CTRL1
,
750 /* Shield enabled, small range. */
754 .reg
= SX9500_REG_PROX_CTRL2
,
755 /* x8 gain, 167kHz frequency, finest resolution. */
759 .reg
= SX9500_REG_PROX_CTRL3
,
760 /* Doze enabled, 2x scan period doze, no raw filter. */
764 .reg
= SX9500_REG_PROX_CTRL4
,
765 /* Average threshold. */
769 .reg
= SX9500_REG_PROX_CTRL5
,
771 * Debouncer off, lowest average negative filter,
772 * highest average postive filter.
777 .reg
= SX9500_REG_PROX_CTRL6
,
778 /* Proximity detection threshold: 280 */
782 .reg
= SX9500_REG_PROX_CTRL7
,
784 * No automatic compensation, compensate each pin
785 * independently, proximity hysteresis: 32, close
786 * debouncer off, far debouncer off.
791 .reg
= SX9500_REG_PROX_CTRL8
,
792 /* No stuck timeout, no periodic compensation. */
796 .reg
= SX9500_REG_PROX_CTRL0
,
797 /* Scan period: 30ms, all sensors disabled. */
802 /* Activate all channels and perform an initial compensation. */
803 static int sx9500_init_compensation(struct iio_dev
*indio_dev
)
805 struct sx9500_data
*data
= iio_priv(indio_dev
);
809 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
810 SX9500_CHAN_MASK
, SX9500_CHAN_MASK
);
814 for (i
= 10; i
>= 0; i
--) {
815 usleep_range(10000, 20000);
816 ret
= regmap_read(data
->regmap
, SX9500_REG_STAT
, &val
);
819 if (!(val
& SX9500_COMPSTAT_MASK
))
824 dev_err(&data
->client
->dev
, "initial compensation timed out");
829 regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
830 SX9500_CHAN_MASK
, 0);
834 static int sx9500_init_device(struct iio_dev
*indio_dev
)
836 struct sx9500_data
*data
= iio_priv(indio_dev
);
840 if (data
->gpiod_rst
) {
841 gpiod_set_value_cansleep(data
->gpiod_rst
, 0);
842 usleep_range(1000, 2000);
843 gpiod_set_value_cansleep(data
->gpiod_rst
, 1);
844 usleep_range(1000, 2000);
847 ret
= regmap_write(data
->regmap
, SX9500_REG_IRQ_MSK
, 0);
851 ret
= regmap_write(data
->regmap
, SX9500_REG_RESET
,
856 ret
= regmap_read(data
->regmap
, SX9500_REG_IRQ_SRC
, &val
);
860 for (i
= 0; i
< ARRAY_SIZE(sx9500_default_regs
); i
++) {
861 ret
= regmap_write(data
->regmap
,
862 sx9500_default_regs
[i
].reg
,
863 sx9500_default_regs
[i
].def
);
868 return sx9500_init_compensation(indio_dev
);
871 static void sx9500_gpio_probe(struct i2c_client
*client
,
872 struct sx9500_data
*data
)
881 data
->gpiod_rst
= devm_gpiod_get_index(dev
, SX9500_GPIO_RESET
,
883 if (IS_ERR(data
->gpiod_rst
)) {
884 dev_warn(dev
, "gpio get reset pin failed\n");
885 data
->gpiod_rst
= NULL
;
889 static int sx9500_probe(struct i2c_client
*client
,
890 const struct i2c_device_id
*id
)
893 struct iio_dev
*indio_dev
;
894 struct sx9500_data
*data
;
896 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
897 if (indio_dev
== NULL
)
900 data
= iio_priv(indio_dev
);
901 data
->client
= client
;
902 mutex_init(&data
->mutex
);
903 init_completion(&data
->completion
);
904 data
->trigger_enabled
= false;
906 data
->regmap
= devm_regmap_init_i2c(client
, &sx9500_regmap_config
);
907 if (IS_ERR(data
->regmap
))
908 return PTR_ERR(data
->regmap
);
910 indio_dev
->dev
.parent
= &client
->dev
;
911 indio_dev
->name
= SX9500_DRIVER_NAME
;
912 indio_dev
->channels
= sx9500_channels
;
913 indio_dev
->num_channels
= ARRAY_SIZE(sx9500_channels
);
914 indio_dev
->info
= &sx9500_info
;
915 indio_dev
->modes
= INDIO_DIRECT_MODE
;
916 i2c_set_clientdata(client
, indio_dev
);
918 sx9500_gpio_probe(client
, data
);
920 ret
= sx9500_init_device(indio_dev
);
924 if (client
->irq
<= 0)
925 dev_warn(&client
->dev
, "no valid irq found\n");
927 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
928 sx9500_irq_handler
, sx9500_irq_thread_handler
,
929 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
930 SX9500_IRQ_NAME
, indio_dev
);
934 data
->trig
= devm_iio_trigger_alloc(&client
->dev
,
935 "%s-dev%d", indio_dev
->name
, indio_dev
->id
);
939 data
->trig
->dev
.parent
= &client
->dev
;
940 data
->trig
->ops
= &sx9500_trigger_ops
;
941 iio_trigger_set_drvdata(data
->trig
, indio_dev
);
943 ret
= iio_trigger_register(data
->trig
);
948 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
949 sx9500_trigger_handler
,
950 &sx9500_buffer_setup_ops
);
952 goto out_trigger_unregister
;
954 ret
= iio_device_register(indio_dev
);
956 goto out_buffer_cleanup
;
961 iio_triggered_buffer_cleanup(indio_dev
);
962 out_trigger_unregister
:
964 iio_trigger_unregister(data
->trig
);
969 static int sx9500_remove(struct i2c_client
*client
)
971 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
972 struct sx9500_data
*data
= iio_priv(indio_dev
);
974 iio_device_unregister(indio_dev
);
975 iio_triggered_buffer_cleanup(indio_dev
);
977 iio_trigger_unregister(data
->trig
);
983 #ifdef CONFIG_PM_SLEEP
984 static int sx9500_suspend(struct device
*dev
)
986 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
987 struct sx9500_data
*data
= iio_priv(indio_dev
);
990 mutex_lock(&data
->mutex
);
991 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
,
992 &data
->suspend_ctrl0
);
997 * Scan period doesn't matter because when all the sensors are
998 * deactivated the device is in sleep mode.
1000 ret
= regmap_write(data
->regmap
, SX9500_REG_PROX_CTRL0
, 0);
1003 mutex_unlock(&data
->mutex
);
1007 static int sx9500_resume(struct device
*dev
)
1009 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1010 struct sx9500_data
*data
= iio_priv(indio_dev
);
1013 mutex_lock(&data
->mutex
);
1014 ret
= regmap_write(data
->regmap
, SX9500_REG_PROX_CTRL0
,
1015 data
->suspend_ctrl0
);
1016 mutex_unlock(&data
->mutex
);
1020 #endif /* CONFIG_PM_SLEEP */
1022 static const struct dev_pm_ops sx9500_pm_ops
= {
1023 SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend
, sx9500_resume
)
1026 static const struct acpi_device_id sx9500_acpi_match
[] = {
1030 MODULE_DEVICE_TABLE(acpi
, sx9500_acpi_match
);
1032 static const struct of_device_id sx9500_of_match
[] = {
1033 { .compatible
= "semtech,sx9500", },
1036 MODULE_DEVICE_TABLE(of
, sx9500_of_match
);
1038 static const struct i2c_device_id sx9500_id
[] = {
1042 MODULE_DEVICE_TABLE(i2c
, sx9500_id
);
1044 static struct i2c_driver sx9500_driver
= {
1046 .name
= SX9500_DRIVER_NAME
,
1047 .acpi_match_table
= ACPI_PTR(sx9500_acpi_match
),
1048 .of_match_table
= of_match_ptr(sx9500_of_match
),
1049 .pm
= &sx9500_pm_ops
,
1051 .probe
= sx9500_probe
,
1052 .remove
= sx9500_remove
,
1053 .id_table
= sx9500_id
,
1055 module_i2c_driver(sx9500_driver
);
1057 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1058 MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
1059 MODULE_LICENSE("GPL v2");