1 // SPDX-License-Identifier: GPL-2.0+
3 * apds9960.c - Support for Avago APDS9960 gesture/RGB/ALS/proximity sensor
5 * Copyright (C) 2015, 2018
6 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
8 * TODO: gesture + proximity calib offsets
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/delay.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
17 #include <linux/irq.h>
18 #include <linux/gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/events.h>
25 #include <linux/iio/kfifo_buf.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/of_gpio.h>
29 #define APDS9960_REGMAP_NAME "apds9960_regmap"
30 #define APDS9960_DRV_NAME "apds9960"
32 #define APDS9960_REG_RAM_START 0x00
33 #define APDS9960_REG_RAM_END 0x7f
35 #define APDS9960_REG_ENABLE 0x80
36 #define APDS9960_REG_ATIME 0x81
37 #define APDS9960_REG_WTIME 0x83
39 #define APDS9960_REG_AILTL 0x84
40 #define APDS9960_REG_AILTH 0x85
41 #define APDS9960_REG_AIHTL 0x86
42 #define APDS9960_REG_AIHTH 0x87
44 #define APDS9960_REG_PILT 0x89
45 #define APDS9960_REG_PIHT 0x8b
46 #define APDS9960_REG_PERS 0x8c
48 #define APDS9960_REG_CONFIG_1 0x8d
49 #define APDS9960_REG_PPULSE 0x8e
51 #define APDS9960_REG_CONTROL 0x8f
52 #define APDS9960_REG_CONTROL_AGAIN_MASK 0x03
53 #define APDS9960_REG_CONTROL_PGAIN_MASK 0x0c
54 #define APDS9960_REG_CONTROL_AGAIN_MASK_SHIFT 0
55 #define APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT 2
57 #define APDS9960_REG_CONFIG_2 0x90
58 #define APDS9960_REG_CONFIG_2_GGAIN_MASK 0x60
59 #define APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT 5
61 #define APDS9960_REG_ID 0x92
63 #define APDS9960_REG_STATUS 0x93
64 #define APDS9960_REG_STATUS_PS_INT BIT(5)
65 #define APDS9960_REG_STATUS_ALS_INT BIT(4)
66 #define APDS9960_REG_STATUS_GINT BIT(2)
68 #define APDS9960_REG_PDATA 0x9c
69 #define APDS9960_REG_POFFSET_UR 0x9d
70 #define APDS9960_REG_POFFSET_DL 0x9e
71 #define APDS9960_REG_CONFIG_3 0x9f
73 #define APDS9960_REG_GPENTH 0xa0
74 #define APDS9960_REG_GEXTH 0xa1
76 #define APDS9960_REG_GCONF_1 0xa2
77 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK 0xc0
78 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT 6
80 #define APDS9960_REG_GCONF_2 0xa3
81 #define APDS9960_REG_GOFFSET_U 0xa4
82 #define APDS9960_REG_GOFFSET_D 0xa5
83 #define APDS9960_REG_GPULSE 0xa6
84 #define APDS9960_REG_GOFFSET_L 0xa7
85 #define APDS9960_REG_GOFFSET_R 0xa9
86 #define APDS9960_REG_GCONF_3 0xaa
88 #define APDS9960_REG_GCONF_4 0xab
89 #define APDS9960_REG_GFLVL 0xae
90 #define APDS9960_REG_GSTATUS 0xaf
92 #define APDS9960_REG_IFORCE 0xe4
93 #define APDS9960_REG_PICLEAR 0xe5
94 #define APDS9960_REG_CICLEAR 0xe6
95 #define APDS9960_REG_AICLEAR 0xe7
97 #define APDS9960_DEFAULT_PERS 0x33
98 #define APDS9960_DEFAULT_GPENTH 0x50
99 #define APDS9960_DEFAULT_GEXTH 0x40
101 #define APDS9960_MAX_PXS_THRES_VAL 255
102 #define APDS9960_MAX_ALS_THRES_VAL 0xffff
103 #define APDS9960_MAX_INT_TIME_IN_US 1000000
105 enum apds9960_als_channel_idx
{
106 IDX_ALS_CLEAR
, IDX_ALS_RED
, IDX_ALS_GREEN
, IDX_ALS_BLUE
,
109 #define APDS9960_REG_ALS_BASE 0x94
110 #define APDS9960_REG_ALS_CHANNEL(_colour) \
111 (APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2))
113 enum apds9960_gesture_channel_idx
{
114 IDX_DIR_UP
, IDX_DIR_DOWN
, IDX_DIR_LEFT
, IDX_DIR_RIGHT
,
117 #define APDS9960_REG_GFIFO_BASE 0xfc
118 #define APDS9960_REG_GFIFO_DIR(_dir) \
119 (APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir)
121 struct apds9960_data
{
122 struct i2c_client
*client
;
123 struct iio_dev
*indio_dev
;
127 struct regmap
*regmap
;
128 struct regmap_field
*reg_int_als
;
129 struct regmap_field
*reg_int_ges
;
130 struct regmap_field
*reg_int_pxs
;
132 struct regmap_field
*reg_enable_als
;
133 struct regmap_field
*reg_enable_ges
;
134 struct regmap_field
*reg_enable_pxs
;
139 int gesture_mode_running
;
145 /* integration time value in us */
149 u8 buffer
[4]; /* 4 8-bit channels */
152 static const struct reg_default apds9960_reg_defaults
[] = {
153 /* Default ALS integration time = 2.48ms */
154 { APDS9960_REG_ATIME
, 0xff },
157 static const struct regmap_range apds9960_volatile_ranges
[] = {
158 regmap_reg_range(APDS9960_REG_STATUS
,
160 regmap_reg_range(APDS9960_REG_GFLVL
,
161 APDS9960_REG_GSTATUS
),
162 regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP
),
163 APDS9960_REG_GFIFO_DIR(RIGHT
)),
164 regmap_reg_range(APDS9960_REG_IFORCE
,
165 APDS9960_REG_AICLEAR
),
168 static const struct regmap_access_table apds9960_volatile_table
= {
169 .yes_ranges
= apds9960_volatile_ranges
,
170 .n_yes_ranges
= ARRAY_SIZE(apds9960_volatile_ranges
),
173 static const struct regmap_range apds9960_precious_ranges
[] = {
174 regmap_reg_range(APDS9960_REG_RAM_START
, APDS9960_REG_RAM_END
),
177 static const struct regmap_access_table apds9960_precious_table
= {
178 .yes_ranges
= apds9960_precious_ranges
,
179 .n_yes_ranges
= ARRAY_SIZE(apds9960_precious_ranges
),
182 static const struct regmap_range apds9960_readable_ranges
[] = {
183 regmap_reg_range(APDS9960_REG_ENABLE
,
184 APDS9960_REG_GSTATUS
),
185 regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP
),
186 APDS9960_REG_GFIFO_DIR(RIGHT
)),
189 static const struct regmap_access_table apds9960_readable_table
= {
190 .yes_ranges
= apds9960_readable_ranges
,
191 .n_yes_ranges
= ARRAY_SIZE(apds9960_readable_ranges
),
194 static const struct regmap_range apds9960_writeable_ranges
[] = {
195 regmap_reg_range(APDS9960_REG_ENABLE
, APDS9960_REG_CONFIG_2
),
196 regmap_reg_range(APDS9960_REG_POFFSET_UR
, APDS9960_REG_GCONF_4
),
197 regmap_reg_range(APDS9960_REG_IFORCE
, APDS9960_REG_AICLEAR
),
200 static const struct regmap_access_table apds9960_writeable_table
= {
201 .yes_ranges
= apds9960_writeable_ranges
,
202 .n_yes_ranges
= ARRAY_SIZE(apds9960_writeable_ranges
),
205 static const struct regmap_config apds9960_regmap_config
= {
206 .name
= APDS9960_REGMAP_NAME
,
209 .use_single_read
= true,
210 .use_single_write
= true,
212 .volatile_table
= &apds9960_volatile_table
,
213 .precious_table
= &apds9960_precious_table
,
214 .rd_table
= &apds9960_readable_table
,
215 .wr_table
= &apds9960_writeable_table
,
217 .reg_defaults
= apds9960_reg_defaults
,
218 .num_reg_defaults
= ARRAY_SIZE(apds9960_reg_defaults
),
219 .max_register
= APDS9960_REG_GFIFO_DIR(RIGHT
),
220 .cache_type
= REGCACHE_RBTREE
,
223 static const struct iio_event_spec apds9960_pxs_event_spec
[] = {
225 .type
= IIO_EV_TYPE_THRESH
,
226 .dir
= IIO_EV_DIR_RISING
,
227 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
228 BIT(IIO_EV_INFO_ENABLE
),
231 .type
= IIO_EV_TYPE_THRESH
,
232 .dir
= IIO_EV_DIR_FALLING
,
233 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
234 BIT(IIO_EV_INFO_ENABLE
),
238 static const struct iio_event_spec apds9960_als_event_spec
[] = {
240 .type
= IIO_EV_TYPE_THRESH
,
241 .dir
= IIO_EV_DIR_RISING
,
242 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
243 BIT(IIO_EV_INFO_ENABLE
),
246 .type
= IIO_EV_TYPE_THRESH
,
247 .dir
= IIO_EV_DIR_FALLING
,
248 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
249 BIT(IIO_EV_INFO_ENABLE
),
253 #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \
254 .type = IIO_PROXIMITY, \
255 .channel = _si + 1, \
265 #define APDS9960_INTENSITY_CHANNEL(_colour) { \
266 .type = IIO_INTENSITY, \
267 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
268 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
269 BIT(IIO_CHAN_INFO_INT_TIME), \
270 .channel2 = IIO_MOD_LIGHT_##_colour, \
271 .address = APDS9960_REG_ALS_CHANNEL(_colour), \
276 static const unsigned long apds9960_scan_masks
[] = {0xf, 0};
278 static const struct iio_chan_spec apds9960_channels
[] = {
280 .type
= IIO_PROXIMITY
,
281 .address
= APDS9960_REG_PDATA
,
282 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
283 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
288 .event_spec
= apds9960_pxs_event_spec
,
289 .num_event_specs
= ARRAY_SIZE(apds9960_pxs_event_spec
),
292 APDS9960_GESTURE_CHANNEL(UP
, 0),
293 APDS9960_GESTURE_CHANNEL(DOWN
, 1),
294 APDS9960_GESTURE_CHANNEL(LEFT
, 2),
295 APDS9960_GESTURE_CHANNEL(RIGHT
, 3),
298 .type
= IIO_INTENSITY
,
299 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
300 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
) |
301 BIT(IIO_CHAN_INFO_INT_TIME
),
302 .channel2
= IIO_MOD_LIGHT_CLEAR
,
303 .address
= APDS9960_REG_ALS_CHANNEL(CLEAR
),
307 .event_spec
= apds9960_als_event_spec
,
308 .num_event_specs
= ARRAY_SIZE(apds9960_als_event_spec
),
311 APDS9960_INTENSITY_CHANNEL(RED
),
312 APDS9960_INTENSITY_CHANNEL(GREEN
),
313 APDS9960_INTENSITY_CHANNEL(BLUE
),
316 /* integration time in us */
317 static const int apds9960_int_time
[][2] = {
325 static const int apds9960_pxs_gain_map
[] = {1, 2, 4, 8};
326 static const int apds9960_als_gain_map
[] = {1, 4, 16, 64};
328 static IIO_CONST_ATTR(proximity_scale_available
, "1 2 4 8");
329 static IIO_CONST_ATTR(intensity_scale_available
, "1 4 16 64");
330 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.028 0.1 0.2 0.7");
332 static struct attribute
*apds9960_attributes
[] = {
333 &iio_const_attr_proximity_scale_available
.dev_attr
.attr
,
334 &iio_const_attr_intensity_scale_available
.dev_attr
.attr
,
335 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
339 static const struct attribute_group apds9960_attribute_group
= {
340 .attrs
= apds9960_attributes
,
343 static const struct reg_field apds9960_reg_field_int_als
=
344 REG_FIELD(APDS9960_REG_ENABLE
, 4, 4);
346 static const struct reg_field apds9960_reg_field_int_ges
=
347 REG_FIELD(APDS9960_REG_GCONF_4
, 1, 1);
349 static const struct reg_field apds9960_reg_field_int_pxs
=
350 REG_FIELD(APDS9960_REG_ENABLE
, 5, 5);
352 static const struct reg_field apds9960_reg_field_enable_als
=
353 REG_FIELD(APDS9960_REG_ENABLE
, 1, 1);
355 static const struct reg_field apds9960_reg_field_enable_ges
=
356 REG_FIELD(APDS9960_REG_ENABLE
, 6, 6);
358 static const struct reg_field apds9960_reg_field_enable_pxs
=
359 REG_FIELD(APDS9960_REG_ENABLE
, 2, 2);
361 static int apds9960_set_it_time(struct apds9960_data
*data
, int val2
)
366 for (idx
= 0; idx
< ARRAY_SIZE(apds9960_int_time
); idx
++) {
367 if (apds9960_int_time
[idx
][0] == val2
) {
368 mutex_lock(&data
->lock
);
369 ret
= regmap_write(data
->regmap
, APDS9960_REG_ATIME
,
370 apds9960_int_time
[idx
][1]);
372 data
->als_adc_int_us
= val2
;
373 mutex_unlock(&data
->lock
);
381 static int apds9960_set_pxs_gain(struct apds9960_data
*data
, int val
)
386 for (idx
= 0; idx
< ARRAY_SIZE(apds9960_pxs_gain_map
); idx
++) {
387 if (apds9960_pxs_gain_map
[idx
] == val
) {
388 /* pxs + gesture gains are mirrored */
389 mutex_lock(&data
->lock
);
390 ret
= regmap_update_bits(data
->regmap
,
391 APDS9960_REG_CONTROL
,
392 APDS9960_REG_CONTROL_PGAIN_MASK
,
393 idx
<< APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT
);
395 mutex_unlock(&data
->lock
);
399 ret
= regmap_update_bits(data
->regmap
,
400 APDS9960_REG_CONFIG_2
,
401 APDS9960_REG_CONFIG_2_GGAIN_MASK
,
402 idx
<< APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT
);
404 data
->pxs_gain
= idx
;
405 mutex_unlock(&data
->lock
);
413 static int apds9960_set_als_gain(struct apds9960_data
*data
, int val
)
418 for (idx
= 0; idx
< ARRAY_SIZE(apds9960_als_gain_map
); idx
++) {
419 if (apds9960_als_gain_map
[idx
] == val
) {
420 mutex_lock(&data
->lock
);
421 ret
= regmap_update_bits(data
->regmap
,
422 APDS9960_REG_CONTROL
,
423 APDS9960_REG_CONTROL_AGAIN_MASK
, idx
);
425 data
->als_gain
= idx
;
426 mutex_unlock(&data
->lock
);
435 static int apds9960_set_power_state(struct apds9960_data
*data
, bool on
)
437 struct device
*dev
= &data
->client
->dev
;
440 mutex_lock(&data
->lock
);
445 suspended
= pm_runtime_suspended(dev
);
446 ret
= pm_runtime_get_sync(dev
);
448 /* Allow one integration cycle before allowing a reading */
450 usleep_range(data
->als_adc_int_us
,
451 APDS9960_MAX_INT_TIME_IN_US
);
453 pm_runtime_mark_last_busy(dev
);
454 ret
= pm_runtime_put_autosuspend(dev
);
457 mutex_unlock(&data
->lock
);
462 static int apds9960_set_power_state(struct apds9960_data
*data
, bool on
)
468 static int apds9960_read_raw(struct iio_dev
*indio_dev
,
469 struct iio_chan_spec
const *chan
,
470 int *val
, int *val2
, long mask
)
472 struct apds9960_data
*data
= iio_priv(indio_dev
);
476 if (data
->gesture_mode_running
)
480 case IIO_CHAN_INFO_RAW
:
481 apds9960_set_power_state(data
, true);
482 switch (chan
->type
) {
484 ret
= regmap_read(data
->regmap
, chan
->address
, val
);
489 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
493 *val
= le16_to_cpu(buf
);
499 apds9960_set_power_state(data
, false);
501 case IIO_CHAN_INFO_INT_TIME
:
502 /* RGB + ALS sensors only have integration time */
503 mutex_lock(&data
->lock
);
504 switch (chan
->type
) {
507 *val2
= data
->als_adc_int_us
;
508 ret
= IIO_VAL_INT_PLUS_MICRO
;
513 mutex_unlock(&data
->lock
);
515 case IIO_CHAN_INFO_SCALE
:
516 mutex_lock(&data
->lock
);
517 switch (chan
->type
) {
519 *val
= apds9960_pxs_gain_map
[data
->pxs_gain
];
523 *val
= apds9960_als_gain_map
[data
->als_gain
];
529 mutex_unlock(&data
->lock
);
536 static int apds9960_write_raw(struct iio_dev
*indio_dev
,
537 struct iio_chan_spec
const *chan
,
538 int val
, int val2
, long mask
)
540 struct apds9960_data
*data
= iio_priv(indio_dev
);
543 case IIO_CHAN_INFO_INT_TIME
:
544 /* RGB + ALS sensors only have int time */
545 switch (chan
->type
) {
549 return apds9960_set_it_time(data
, val2
);
553 case IIO_CHAN_INFO_SCALE
:
556 switch (chan
->type
) {
558 return apds9960_set_pxs_gain(data
, val
);
560 return apds9960_set_als_gain(data
, val
);
571 static inline int apds9960_get_thres_reg(const struct iio_chan_spec
*chan
,
572 enum iio_event_direction dir
,
576 case IIO_EV_DIR_RISING
:
577 switch (chan
->type
) {
579 *reg
= APDS9960_REG_PIHT
;
582 *reg
= APDS9960_REG_AIHTL
;
588 case IIO_EV_DIR_FALLING
:
589 switch (chan
->type
) {
591 *reg
= APDS9960_REG_PILT
;
594 *reg
= APDS9960_REG_AILTL
;
607 static int apds9960_read_event(struct iio_dev
*indio_dev
,
608 const struct iio_chan_spec
*chan
,
609 enum iio_event_type type
,
610 enum iio_event_direction dir
,
611 enum iio_event_info info
,
617 struct apds9960_data
*data
= iio_priv(indio_dev
);
619 if (info
!= IIO_EV_INFO_VALUE
)
622 ret
= apds9960_get_thres_reg(chan
, dir
, ®
);
626 if (chan
->type
== IIO_PROXIMITY
) {
627 ret
= regmap_read(data
->regmap
, reg
, val
);
630 } else if (chan
->type
== IIO_INTENSITY
) {
631 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
634 *val
= le16_to_cpu(buf
);
643 static int apds9960_write_event(struct iio_dev
*indio_dev
,
644 const struct iio_chan_spec
*chan
,
645 enum iio_event_type type
,
646 enum iio_event_direction dir
,
647 enum iio_event_info info
,
653 struct apds9960_data
*data
= iio_priv(indio_dev
);
655 if (info
!= IIO_EV_INFO_VALUE
)
658 ret
= apds9960_get_thres_reg(chan
, dir
, ®
);
662 if (chan
->type
== IIO_PROXIMITY
) {
663 if (val
< 0 || val
> APDS9960_MAX_PXS_THRES_VAL
)
665 ret
= regmap_write(data
->regmap
, reg
, val
);
668 } else if (chan
->type
== IIO_INTENSITY
) {
669 if (val
< 0 || val
> APDS9960_MAX_ALS_THRES_VAL
)
671 buf
= cpu_to_le16(val
);
672 ret
= regmap_bulk_write(data
->regmap
, reg
, &buf
, 2);
681 static int apds9960_read_event_config(struct iio_dev
*indio_dev
,
682 const struct iio_chan_spec
*chan
,
683 enum iio_event_type type
,
684 enum iio_event_direction dir
)
686 struct apds9960_data
*data
= iio_priv(indio_dev
);
688 switch (chan
->type
) {
690 return data
->pxs_int
;
692 return data
->als_int
;
700 static int apds9960_write_event_config(struct iio_dev
*indio_dev
,
701 const struct iio_chan_spec
*chan
,
702 enum iio_event_type type
,
703 enum iio_event_direction dir
,
706 struct apds9960_data
*data
= iio_priv(indio_dev
);
711 switch (chan
->type
) {
713 if (data
->pxs_int
== state
)
716 ret
= regmap_field_write(data
->reg_int_pxs
, state
);
719 data
->pxs_int
= state
;
720 apds9960_set_power_state(data
, state
);
723 if (data
->als_int
== state
)
726 ret
= regmap_field_write(data
->reg_int_als
, state
);
729 data
->als_int
= state
;
730 apds9960_set_power_state(data
, state
);
739 static const struct iio_info apds9960_info
= {
740 .attrs
= &apds9960_attribute_group
,
741 .read_raw
= apds9960_read_raw
,
742 .write_raw
= apds9960_write_raw
,
743 .read_event_value
= apds9960_read_event
,
744 .write_event_value
= apds9960_write_event
,
745 .read_event_config
= apds9960_read_event_config
,
746 .write_event_config
= apds9960_write_event_config
,
750 static inline int apds9660_fifo_is_empty(struct apds9960_data
*data
)
755 ret
= regmap_read(data
->regmap
, APDS9960_REG_GFLVL
, &cnt
);
762 static void apds9960_read_gesture_fifo(struct apds9960_data
*data
)
766 mutex_lock(&data
->lock
);
767 data
->gesture_mode_running
= 1;
769 while (cnt
|| (cnt
= apds9660_fifo_is_empty(data
) > 0)) {
770 ret
= regmap_bulk_read(data
->regmap
, APDS9960_REG_GFIFO_BASE
,
776 iio_push_to_buffers(data
->indio_dev
, data
->buffer
);
781 data
->gesture_mode_running
= 0;
782 mutex_unlock(&data
->lock
);
785 static irqreturn_t
apds9960_interrupt_handler(int irq
, void *private)
787 struct iio_dev
*indio_dev
= private;
788 struct apds9960_data
*data
= iio_priv(indio_dev
);
791 ret
= regmap_read(data
->regmap
, APDS9960_REG_STATUS
, &status
);
793 dev_err(&data
->client
->dev
, "irq status reg read failed\n");
797 if ((status
& APDS9960_REG_STATUS_ALS_INT
) && data
->als_int
) {
798 iio_push_event(indio_dev
,
799 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY
, 0,
802 iio_get_time_ns(indio_dev
));
803 regmap_write(data
->regmap
, APDS9960_REG_CICLEAR
, 1);
806 if ((status
& APDS9960_REG_STATUS_PS_INT
) && data
->pxs_int
) {
807 iio_push_event(indio_dev
,
808 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, 0,
811 iio_get_time_ns(indio_dev
));
812 regmap_write(data
->regmap
, APDS9960_REG_PICLEAR
, 1);
815 if (status
& APDS9960_REG_STATUS_GINT
)
816 apds9960_read_gesture_fifo(data
);
821 static int apds9960_set_powermode(struct apds9960_data
*data
, bool state
)
823 return regmap_update_bits(data
->regmap
, APDS9960_REG_ENABLE
, 1, state
);
826 static int apds9960_buffer_postenable(struct iio_dev
*indio_dev
)
828 struct apds9960_data
*data
= iio_priv(indio_dev
);
831 ret
= regmap_field_write(data
->reg_int_ges
, 1);
835 ret
= regmap_field_write(data
->reg_enable_ges
, 1);
839 pm_runtime_get_sync(&data
->client
->dev
);
844 static int apds9960_buffer_predisable(struct iio_dev
*indio_dev
)
846 struct apds9960_data
*data
= iio_priv(indio_dev
);
849 ret
= regmap_field_write(data
->reg_enable_ges
, 0);
853 ret
= regmap_field_write(data
->reg_int_ges
, 0);
857 pm_runtime_put_autosuspend(&data
->client
->dev
);
862 static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops
= {
863 .postenable
= apds9960_buffer_postenable
,
864 .predisable
= apds9960_buffer_predisable
,
867 static int apds9960_regfield_init(struct apds9960_data
*data
)
869 struct device
*dev
= &data
->client
->dev
;
870 struct regmap
*regmap
= data
->regmap
;
872 data
->reg_int_als
= devm_regmap_field_alloc(dev
, regmap
,
873 apds9960_reg_field_int_als
);
874 if (IS_ERR(data
->reg_int_als
)) {
875 dev_err(dev
, "INT ALS reg field init failed\n");
876 return PTR_ERR(data
->reg_int_als
);
879 data
->reg_int_ges
= devm_regmap_field_alloc(dev
, regmap
,
880 apds9960_reg_field_int_ges
);
881 if (IS_ERR(data
->reg_int_ges
)) {
882 dev_err(dev
, "INT gesture reg field init failed\n");
883 return PTR_ERR(data
->reg_int_ges
);
886 data
->reg_int_pxs
= devm_regmap_field_alloc(dev
, regmap
,
887 apds9960_reg_field_int_pxs
);
888 if (IS_ERR(data
->reg_int_pxs
)) {
889 dev_err(dev
, "INT pxs reg field init failed\n");
890 return PTR_ERR(data
->reg_int_pxs
);
893 data
->reg_enable_als
= devm_regmap_field_alloc(dev
, regmap
,
894 apds9960_reg_field_enable_als
);
895 if (IS_ERR(data
->reg_enable_als
)) {
896 dev_err(dev
, "Enable ALS reg field init failed\n");
897 return PTR_ERR(data
->reg_enable_als
);
900 data
->reg_enable_ges
= devm_regmap_field_alloc(dev
, regmap
,
901 apds9960_reg_field_enable_ges
);
902 if (IS_ERR(data
->reg_enable_ges
)) {
903 dev_err(dev
, "Enable gesture reg field init failed\n");
904 return PTR_ERR(data
->reg_enable_ges
);
907 data
->reg_enable_pxs
= devm_regmap_field_alloc(dev
, regmap
,
908 apds9960_reg_field_enable_pxs
);
909 if (IS_ERR(data
->reg_enable_pxs
)) {
910 dev_err(dev
, "Enable PXS reg field init failed\n");
911 return PTR_ERR(data
->reg_enable_pxs
);
917 static int apds9960_chip_init(struct apds9960_data
*data
)
921 /* Default IT for ALS of 28 ms */
922 ret
= apds9960_set_it_time(data
, 28000);
926 /* Ensure gesture interrupt is OFF */
927 ret
= regmap_field_write(data
->reg_int_ges
, 0);
931 /* Disable gesture sensor, since polling is useless from user-space */
932 ret
= regmap_field_write(data
->reg_enable_ges
, 0);
936 /* Ensure proximity interrupt is OFF */
937 ret
= regmap_field_write(data
->reg_int_pxs
, 0);
941 /* Enable proximity sensor for polling */
942 ret
= regmap_field_write(data
->reg_enable_pxs
, 1);
946 /* Ensure ALS interrupt is OFF */
947 ret
= regmap_field_write(data
->reg_int_als
, 0);
951 /* Enable ALS sensor for polling */
952 ret
= regmap_field_write(data
->reg_enable_als
, 1);
956 * When enabled trigger an interrupt after 3 readings
957 * outside threshold for ALS + PXS
959 ret
= regmap_write(data
->regmap
, APDS9960_REG_PERS
,
960 APDS9960_DEFAULT_PERS
);
965 * Wait for 4 event outside gesture threshold to prevent interrupt
968 ret
= regmap_update_bits(data
->regmap
, APDS9960_REG_GCONF_1
,
969 APDS9960_REG_GCONF_1_GFIFO_THRES_MASK
,
970 BIT(0) << APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT
);
974 /* Default ENTER and EXIT thresholds for the GESTURE engine. */
975 ret
= regmap_write(data
->regmap
, APDS9960_REG_GPENTH
,
976 APDS9960_DEFAULT_GPENTH
);
980 ret
= regmap_write(data
->regmap
, APDS9960_REG_GEXTH
,
981 APDS9960_DEFAULT_GEXTH
);
985 return apds9960_set_powermode(data
, 1);
988 static int apds9960_probe(struct i2c_client
*client
,
989 const struct i2c_device_id
*id
)
991 struct apds9960_data
*data
;
992 struct iio_buffer
*buffer
;
993 struct iio_dev
*indio_dev
;
996 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1000 buffer
= devm_iio_kfifo_allocate(&client
->dev
);
1004 iio_device_attach_buffer(indio_dev
, buffer
);
1006 indio_dev
->dev
.parent
= &client
->dev
;
1007 indio_dev
->info
= &apds9960_info
;
1008 indio_dev
->name
= APDS9960_DRV_NAME
;
1009 indio_dev
->channels
= apds9960_channels
;
1010 indio_dev
->num_channels
= ARRAY_SIZE(apds9960_channels
);
1011 indio_dev
->available_scan_masks
= apds9960_scan_masks
;
1012 indio_dev
->modes
= (INDIO_BUFFER_SOFTWARE
| INDIO_DIRECT_MODE
);
1013 indio_dev
->setup_ops
= &apds9960_buffer_setup_ops
;
1015 data
= iio_priv(indio_dev
);
1016 i2c_set_clientdata(client
, indio_dev
);
1018 data
->regmap
= devm_regmap_init_i2c(client
, &apds9960_regmap_config
);
1019 if (IS_ERR(data
->regmap
)) {
1020 dev_err(&client
->dev
, "regmap initialization failed.\n");
1021 return PTR_ERR(data
->regmap
);
1024 data
->client
= client
;
1025 data
->indio_dev
= indio_dev
;
1026 mutex_init(&data
->lock
);
1028 ret
= pm_runtime_set_active(&client
->dev
);
1030 goto error_power_down
;
1032 pm_runtime_enable(&client
->dev
);
1033 pm_runtime_set_autosuspend_delay(&client
->dev
, 5000);
1034 pm_runtime_use_autosuspend(&client
->dev
);
1036 apds9960_set_power_state(data
, true);
1038 ret
= apds9960_regfield_init(data
);
1040 goto error_power_down
;
1042 ret
= apds9960_chip_init(data
);
1044 goto error_power_down
;
1046 if (client
->irq
<= 0) {
1047 dev_err(&client
->dev
, "no valid irq defined\n");
1049 goto error_power_down
;
1051 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1052 NULL
, apds9960_interrupt_handler
,
1053 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1057 dev_err(&client
->dev
, "request irq (%d) failed\n", client
->irq
);
1058 goto error_power_down
;
1061 ret
= iio_device_register(indio_dev
);
1063 goto error_power_down
;
1065 apds9960_set_power_state(data
, false);
1070 apds9960_set_power_state(data
, false);
1075 static int apds9960_remove(struct i2c_client
*client
)
1077 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1078 struct apds9960_data
*data
= iio_priv(indio_dev
);
1080 iio_device_unregister(indio_dev
);
1081 pm_runtime_disable(&client
->dev
);
1082 pm_runtime_set_suspended(&client
->dev
);
1083 apds9960_set_powermode(data
, 0);
1089 static int apds9960_runtime_suspend(struct device
*dev
)
1091 struct apds9960_data
*data
=
1092 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
1094 return apds9960_set_powermode(data
, 0);
1097 static int apds9960_runtime_resume(struct device
*dev
)
1099 struct apds9960_data
*data
=
1100 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
1102 return apds9960_set_powermode(data
, 1);
1106 static const struct dev_pm_ops apds9960_pm_ops
= {
1107 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1108 pm_runtime_force_resume
)
1109 SET_RUNTIME_PM_OPS(apds9960_runtime_suspend
,
1110 apds9960_runtime_resume
, NULL
)
1113 static const struct i2c_device_id apds9960_id
[] = {
1117 MODULE_DEVICE_TABLE(i2c
, apds9960_id
);
1119 static const struct of_device_id apds9960_of_match
[] = {
1120 { .compatible
= "avago,apds9960" },
1123 MODULE_DEVICE_TABLE(of
, apds9960_of_match
);
1125 static struct i2c_driver apds9960_driver
= {
1127 .name
= APDS9960_DRV_NAME
,
1128 .of_match_table
= apds9960_of_match
,
1129 .pm
= &apds9960_pm_ops
,
1131 .probe
= apds9960_probe
,
1132 .remove
= apds9960_remove
,
1133 .id_table
= apds9960_id
,
1135 module_i2c_driver(apds9960_driver
);
1137 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
1138 MODULE_DESCRIPTION("ADPS9960 Gesture/RGB/ALS/Proximity sensor");
1139 MODULE_LICENSE("GPL");