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/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/events.h>
26 #include <linux/iio/trigger.h>
27 #include <linux/iio/triggered_buffer.h>
28 #include <linux/iio/trigger_consumer.h>
30 #define SX9500_DRIVER_NAME "sx9500"
31 #define SX9500_IRQ_NAME "sx9500_event"
32 #define SX9500_GPIO_NAME "sx9500_gpio"
34 /* Register definitions. */
35 #define SX9500_REG_IRQ_SRC 0x00
36 #define SX9500_REG_STAT 0x01
37 #define SX9500_REG_IRQ_MSK 0x03
39 #define SX9500_REG_PROX_CTRL0 0x06
40 #define SX9500_REG_PROX_CTRL1 0x07
41 #define SX9500_REG_PROX_CTRL2 0x08
42 #define SX9500_REG_PROX_CTRL3 0x09
43 #define SX9500_REG_PROX_CTRL4 0x0a
44 #define SX9500_REG_PROX_CTRL5 0x0b
45 #define SX9500_REG_PROX_CTRL6 0x0c
46 #define SX9500_REG_PROX_CTRL7 0x0d
47 #define SX9500_REG_PROX_CTRL8 0x0e
49 #define SX9500_REG_SENSOR_SEL 0x20
50 #define SX9500_REG_USE_MSB 0x21
51 #define SX9500_REG_USE_LSB 0x22
52 #define SX9500_REG_AVG_MSB 0x23
53 #define SX9500_REG_AVG_LSB 0x24
54 #define SX9500_REG_DIFF_MSB 0x25
55 #define SX9500_REG_DIFF_LSB 0x26
56 #define SX9500_REG_OFFSET_MSB 0x27
57 #define SX9500_REG_OFFSET_LSB 0x28
59 #define SX9500_REG_RESET 0x7f
61 /* Write this to REG_RESET to do a soft reset. */
62 #define SX9500_SOFT_RESET 0xde
64 #define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4)
65 #define SX9500_SCAN_PERIOD_SHIFT 4
68 * These serve for identifying IRQ source in the IRQ_SRC register, and
69 * also for masking the IRQs in the IRQ_MSK register.
71 #define SX9500_CLOSE_IRQ BIT(6)
72 #define SX9500_FAR_IRQ BIT(5)
73 #define SX9500_CONVDONE_IRQ BIT(3)
75 #define SX9500_PROXSTAT_SHIFT 4
77 #define SX9500_NUM_CHANNELS 4
81 struct i2c_client
*client
;
82 struct iio_trigger
*trig
;
83 struct regmap
*regmap
;
85 * Last reading of the proximity status for each channel. We
86 * only send an event to user space when this changes.
88 bool prox_stat
[SX9500_NUM_CHANNELS
];
89 bool event_enabled
[SX9500_NUM_CHANNELS
];
94 static const struct iio_event_spec sx9500_events
[] = {
96 .type
= IIO_EV_TYPE_THRESH
,
97 .dir
= IIO_EV_DIR_EITHER
,
98 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
102 #define SX9500_CHANNEL(idx) \
104 .type = IIO_PROXIMITY, \
105 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
106 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
109 .event_spec = sx9500_events, \
110 .num_event_specs = ARRAY_SIZE(sx9500_events), \
120 static const struct iio_chan_spec sx9500_channels
[] = {
125 IIO_CHAN_SOFT_TIMESTAMP(4),
128 static const struct {
131 } sx9500_samp_freq_table
[] = {
142 static const struct regmap_range sx9500_writable_reg_ranges
[] = {
143 regmap_reg_range(SX9500_REG_IRQ_MSK
, SX9500_REG_IRQ_MSK
),
144 regmap_reg_range(SX9500_REG_PROX_CTRL0
, SX9500_REG_PROX_CTRL8
),
145 regmap_reg_range(SX9500_REG_SENSOR_SEL
, SX9500_REG_SENSOR_SEL
),
146 regmap_reg_range(SX9500_REG_OFFSET_MSB
, SX9500_REG_OFFSET_LSB
),
147 regmap_reg_range(SX9500_REG_RESET
, SX9500_REG_RESET
),
150 static const struct regmap_access_table sx9500_writeable_regs
= {
151 .yes_ranges
= sx9500_writable_reg_ranges
,
152 .n_yes_ranges
= ARRAY_SIZE(sx9500_writable_reg_ranges
),
156 * All allocated registers are readable, so we just list unallocated
159 static const struct regmap_range sx9500_non_readable_reg_ranges
[] = {
160 regmap_reg_range(SX9500_REG_STAT
+ 1, SX9500_REG_STAT
+ 1),
161 regmap_reg_range(SX9500_REG_IRQ_MSK
+ 1, SX9500_REG_PROX_CTRL0
- 1),
162 regmap_reg_range(SX9500_REG_PROX_CTRL8
+ 1, SX9500_REG_SENSOR_SEL
- 1),
163 regmap_reg_range(SX9500_REG_OFFSET_LSB
+ 1, SX9500_REG_RESET
- 1),
166 static const struct regmap_access_table sx9500_readable_regs
= {
167 .no_ranges
= sx9500_non_readable_reg_ranges
,
168 .n_no_ranges
= ARRAY_SIZE(sx9500_non_readable_reg_ranges
),
171 static const struct regmap_range sx9500_volatile_reg_ranges
[] = {
172 regmap_reg_range(SX9500_REG_IRQ_SRC
, SX9500_REG_STAT
),
173 regmap_reg_range(SX9500_REG_USE_MSB
, SX9500_REG_OFFSET_LSB
),
174 regmap_reg_range(SX9500_REG_RESET
, SX9500_REG_RESET
),
177 static const struct regmap_access_table sx9500_volatile_regs
= {
178 .yes_ranges
= sx9500_volatile_reg_ranges
,
179 .n_yes_ranges
= ARRAY_SIZE(sx9500_volatile_reg_ranges
),
182 static const struct regmap_config sx9500_regmap_config
= {
186 .max_register
= SX9500_REG_RESET
,
187 .cache_type
= REGCACHE_RBTREE
,
189 .wr_table
= &sx9500_writeable_regs
,
190 .rd_table
= &sx9500_readable_regs
,
191 .volatile_table
= &sx9500_volatile_regs
,
194 static int sx9500_read_proximity(struct sx9500_data
*data
,
195 const struct iio_chan_spec
*chan
,
201 ret
= regmap_write(data
->regmap
, SX9500_REG_SENSOR_SEL
, chan
->channel
);
205 ret
= regmap_bulk_read(data
->regmap
, SX9500_REG_USE_MSB
, ®val
, 2);
209 *val
= 32767 - (s16
)be16_to_cpu(regval
);
214 static int sx9500_read_samp_freq(struct sx9500_data
*data
,
220 mutex_lock(&data
->mutex
);
221 ret
= regmap_read(data
->regmap
, SX9500_REG_PROX_CTRL0
, ®val
);
222 mutex_unlock(&data
->mutex
);
227 regval
= (regval
& SX9500_SCAN_PERIOD_MASK
) >> SX9500_SCAN_PERIOD_SHIFT
;
228 *val
= sx9500_samp_freq_table
[regval
].val
;
229 *val2
= sx9500_samp_freq_table
[regval
].val2
;
231 return IIO_VAL_INT_PLUS_MICRO
;
234 static int sx9500_read_raw(struct iio_dev
*indio_dev
,
235 const struct iio_chan_spec
*chan
,
236 int *val
, int *val2
, long mask
)
238 struct sx9500_data
*data
= iio_priv(indio_dev
);
241 switch (chan
->type
) {
244 case IIO_CHAN_INFO_RAW
:
245 if (iio_buffer_enabled(indio_dev
))
247 mutex_lock(&data
->mutex
);
248 ret
= sx9500_read_proximity(data
, chan
, val
);
249 mutex_unlock(&data
->mutex
);
251 case IIO_CHAN_INFO_SAMP_FREQ
:
252 return sx9500_read_samp_freq(data
, val
, val2
);
261 static int sx9500_set_samp_freq(struct sx9500_data
*data
,
266 for (i
= 0; i
< ARRAY_SIZE(sx9500_samp_freq_table
); i
++)
267 if (val
== sx9500_samp_freq_table
[i
].val
&&
268 val2
== sx9500_samp_freq_table
[i
].val2
)
271 if (i
== ARRAY_SIZE(sx9500_samp_freq_table
))
274 mutex_lock(&data
->mutex
);
276 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_PROX_CTRL0
,
277 SX9500_SCAN_PERIOD_MASK
,
278 i
<< SX9500_SCAN_PERIOD_SHIFT
);
280 mutex_unlock(&data
->mutex
);
285 static int sx9500_write_raw(struct iio_dev
*indio_dev
,
286 const struct iio_chan_spec
*chan
,
287 int val
, int val2
, long mask
)
289 struct sx9500_data
*data
= iio_priv(indio_dev
);
291 switch (chan
->type
) {
294 case IIO_CHAN_INFO_SAMP_FREQ
:
295 return sx9500_set_samp_freq(data
, val
, val2
);
304 static irqreturn_t
sx9500_irq_handler(int irq
, void *private)
306 struct iio_dev
*indio_dev
= private;
307 struct sx9500_data
*data
= iio_priv(indio_dev
);
309 if (data
->trigger_enabled
)
310 iio_trigger_poll(data
->trig
);
313 * Even if no event is enabled, we need to wake the thread to
314 * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It
315 * is not possible to do that here because regmap_read takes a
318 return IRQ_WAKE_THREAD
;
321 static irqreturn_t
sx9500_irq_thread_handler(int irq
, void *private)
323 struct iio_dev
*indio_dev
= private;
324 struct sx9500_data
*data
= iio_priv(indio_dev
);
326 unsigned int val
, chan
;
328 mutex_lock(&data
->mutex
);
330 ret
= regmap_read(data
->regmap
, SX9500_REG_IRQ_SRC
, &val
);
332 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
336 if (!(val
& (SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
)))
339 ret
= regmap_read(data
->regmap
, SX9500_REG_STAT
, &val
);
341 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
345 val
>>= SX9500_PROXSTAT_SHIFT
;
346 for (chan
= 0; chan
< SX9500_NUM_CHANNELS
; chan
++) {
349 bool new_prox
= val
& BIT(chan
);
351 if (!data
->event_enabled
[chan
])
353 if (new_prox
== data
->prox_stat
[chan
])
354 /* No change on this channel. */
357 dir
= new_prox
? IIO_EV_DIR_FALLING
:
359 ev
= IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
,
363 iio_push_event(indio_dev
, ev
, iio_get_time_ns());
364 data
->prox_stat
[chan
] = new_prox
;
368 mutex_unlock(&data
->mutex
);
373 static int sx9500_read_event_config(struct iio_dev
*indio_dev
,
374 const struct iio_chan_spec
*chan
,
375 enum iio_event_type type
,
376 enum iio_event_direction dir
)
378 struct sx9500_data
*data
= iio_priv(indio_dev
);
380 if (chan
->type
!= IIO_PROXIMITY
|| type
!= IIO_EV_TYPE_THRESH
||
381 dir
!= IIO_EV_DIR_EITHER
)
384 return data
->event_enabled
[chan
->channel
];
387 static int sx9500_write_event_config(struct iio_dev
*indio_dev
,
388 const struct iio_chan_spec
*chan
,
389 enum iio_event_type type
,
390 enum iio_event_direction dir
,
393 struct sx9500_data
*data
= iio_priv(indio_dev
);
395 bool any_active
= false;
396 unsigned int irqmask
;
398 if (chan
->type
!= IIO_PROXIMITY
|| type
!= IIO_EV_TYPE_THRESH
||
399 dir
!= IIO_EV_DIR_EITHER
)
402 mutex_lock(&data
->mutex
);
404 data
->event_enabled
[chan
->channel
] = state
;
406 for (i
= 0; i
< SX9500_NUM_CHANNELS
; i
++)
407 if (data
->event_enabled
[i
]) {
412 irqmask
= SX9500_CLOSE_IRQ
| SX9500_FAR_IRQ
;
414 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_IRQ_MSK
,
417 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_IRQ_MSK
,
420 mutex_unlock(&data
->mutex
);
425 static int sx9500_update_scan_mode(struct iio_dev
*indio_dev
,
426 const unsigned long *scan_mask
)
428 struct sx9500_data
*data
= iio_priv(indio_dev
);
430 mutex_lock(&data
->mutex
);
432 data
->buffer
= kzalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
433 mutex_unlock(&data
->mutex
);
435 if (data
->buffer
== NULL
)
441 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
442 "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
444 static struct attribute
*sx9500_attributes
[] = {
445 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
449 static const struct attribute_group sx9500_attribute_group
= {
450 .attrs
= sx9500_attributes
,
453 static const struct iio_info sx9500_info
= {
454 .driver_module
= THIS_MODULE
,
455 .attrs
= &sx9500_attribute_group
,
456 .read_raw
= &sx9500_read_raw
,
457 .write_raw
= &sx9500_write_raw
,
458 .read_event_config
= &sx9500_read_event_config
,
459 .write_event_config
= &sx9500_write_event_config
,
460 .update_scan_mode
= &sx9500_update_scan_mode
,
463 static int sx9500_set_trigger_state(struct iio_trigger
*trig
,
466 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
467 struct sx9500_data
*data
= iio_priv(indio_dev
);
470 mutex_lock(&data
->mutex
);
472 ret
= regmap_update_bits(data
->regmap
, SX9500_REG_IRQ_MSK
,
474 state
? SX9500_CONVDONE_IRQ
: 0);
476 data
->trigger_enabled
= state
;
478 mutex_unlock(&data
->mutex
);
483 static const struct iio_trigger_ops sx9500_trigger_ops
= {
484 .set_trigger_state
= sx9500_set_trigger_state
,
485 .owner
= THIS_MODULE
,
488 static irqreturn_t
sx9500_trigger_handler(int irq
, void *private)
490 struct iio_poll_func
*pf
= private;
491 struct iio_dev
*indio_dev
= pf
->indio_dev
;
492 struct sx9500_data
*data
= iio_priv(indio_dev
);
493 int val
, bit
, ret
, i
= 0;
495 mutex_lock(&data
->mutex
);
497 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
498 indio_dev
->masklength
) {
499 ret
= sx9500_read_proximity(data
, &indio_dev
->channels
[bit
],
504 data
->buffer
[i
++] = val
;
507 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
511 mutex_unlock(&data
->mutex
);
513 iio_trigger_notify_done(indio_dev
->trig
);
518 struct sx9500_reg_default
{
523 static const struct sx9500_reg_default sx9500_default_regs
[] = {
525 .reg
= SX9500_REG_PROX_CTRL1
,
526 /* Shield enabled, small range. */
530 .reg
= SX9500_REG_PROX_CTRL2
,
531 /* x8 gain, 167kHz frequency, finest resolution. */
535 .reg
= SX9500_REG_PROX_CTRL3
,
536 /* Doze enabled, 2x scan period doze, no raw filter. */
540 .reg
= SX9500_REG_PROX_CTRL4
,
541 /* Average threshold. */
545 .reg
= SX9500_REG_PROX_CTRL5
,
547 * Debouncer off, lowest average negative filter,
548 * highest average postive filter.
553 .reg
= SX9500_REG_PROX_CTRL6
,
554 /* Proximity detection threshold: 280 */
558 .reg
= SX9500_REG_PROX_CTRL7
,
560 * No automatic compensation, compensate each pin
561 * independently, proximity hysteresis: 32, close
562 * debouncer off, far debouncer off.
567 .reg
= SX9500_REG_PROX_CTRL8
,
568 /* No stuck timeout, no periodic compensation. */
572 .reg
= SX9500_REG_PROX_CTRL0
,
573 /* Scan period: 30ms, all sensors enabled. */
578 static int sx9500_init_device(struct iio_dev
*indio_dev
)
580 struct sx9500_data
*data
= iio_priv(indio_dev
);
584 ret
= regmap_write(data
->regmap
, SX9500_REG_IRQ_MSK
, 0);
588 ret
= regmap_write(data
->regmap
, SX9500_REG_RESET
,
593 ret
= regmap_read(data
->regmap
, SX9500_REG_IRQ_SRC
, &val
);
597 for (i
= 0; i
< ARRAY_SIZE(sx9500_default_regs
); i
++) {
598 ret
= regmap_write(data
->regmap
,
599 sx9500_default_regs
[i
].reg
,
600 sx9500_default_regs
[i
].def
);
608 static int sx9500_gpio_probe(struct i2c_client
*client
,
609 struct sx9500_data
*data
)
612 struct gpio_desc
*gpio
;
620 /* data ready gpio interrupt pin */
621 gpio
= devm_gpiod_get_index(dev
, SX9500_GPIO_NAME
, 0, GPIOD_IN
);
623 dev_err(dev
, "acpi gpio get index failed\n");
624 return PTR_ERR(gpio
);
627 ret
= gpiod_to_irq(gpio
);
629 dev_dbg(dev
, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio
), ret
);
634 static int sx9500_probe(struct i2c_client
*client
,
635 const struct i2c_device_id
*id
)
638 struct iio_dev
*indio_dev
;
639 struct sx9500_data
*data
;
641 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
642 if (indio_dev
== NULL
)
645 data
= iio_priv(indio_dev
);
646 data
->client
= client
;
647 mutex_init(&data
->mutex
);
648 data
->trigger_enabled
= false;
650 data
->regmap
= devm_regmap_init_i2c(client
, &sx9500_regmap_config
);
651 if (IS_ERR(data
->regmap
))
652 return PTR_ERR(data
->regmap
);
654 sx9500_init_device(indio_dev
);
656 indio_dev
->dev
.parent
= &client
->dev
;
657 indio_dev
->name
= SX9500_DRIVER_NAME
;
658 indio_dev
->channels
= sx9500_channels
;
659 indio_dev
->num_channels
= ARRAY_SIZE(sx9500_channels
);
660 indio_dev
->info
= &sx9500_info
;
661 indio_dev
->modes
= INDIO_DIRECT_MODE
;
662 i2c_set_clientdata(client
, indio_dev
);
664 if (client
->irq
<= 0)
665 client
->irq
= sx9500_gpio_probe(client
, data
);
667 if (client
->irq
> 0) {
668 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
669 sx9500_irq_handler
, sx9500_irq_thread_handler
,
670 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
671 SX9500_IRQ_NAME
, indio_dev
);
675 data
->trig
= devm_iio_trigger_alloc(&client
->dev
,
676 "%s-dev%d", indio_dev
->name
, indio_dev
->id
);
680 data
->trig
->dev
.parent
= &client
->dev
;
681 data
->trig
->ops
= &sx9500_trigger_ops
;
682 iio_trigger_set_drvdata(data
->trig
, indio_dev
);
684 ret
= iio_trigger_register(data
->trig
);
689 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
690 sx9500_trigger_handler
, NULL
);
692 goto out_trigger_unregister
;
694 ret
= iio_device_register(indio_dev
);
696 goto out_buffer_cleanup
;
701 iio_triggered_buffer_cleanup(indio_dev
);
702 out_trigger_unregister
:
704 iio_trigger_unregister(data
->trig
);
709 static int sx9500_remove(struct i2c_client
*client
)
711 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
712 struct sx9500_data
*data
= iio_priv(indio_dev
);
714 iio_device_unregister(indio_dev
);
715 iio_triggered_buffer_cleanup(indio_dev
);
717 iio_trigger_unregister(data
->trig
);
723 static const struct acpi_device_id sx9500_acpi_match
[] = {
727 MODULE_DEVICE_TABLE(acpi
, sx9500_acpi_match
);
729 static const struct i2c_device_id sx9500_id
[] = {
733 MODULE_DEVICE_TABLE(i2c
, sx9500_id
);
735 static struct i2c_driver sx9500_driver
= {
737 .name
= SX9500_DRIVER_NAME
,
738 .acpi_match_table
= ACPI_PTR(sx9500_acpi_match
),
740 .probe
= sx9500_probe
,
741 .remove
= sx9500_remove
,
742 .id_table
= sx9500_id
,
744 module_i2c_driver(sx9500_driver
);
746 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
747 MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
748 MODULE_LICENSE("GPL v2");