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/i2c.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/events.h>
24 #include <linux/iio/kfifo_buf.h>
25 #include <linux/iio/sysfs.h>
27 #define APDS9960_REGMAP_NAME "apds9960_regmap"
28 #define APDS9960_DRV_NAME "apds9960"
30 #define APDS9960_REG_RAM_START 0x00
31 #define APDS9960_REG_RAM_END 0x7f
33 #define APDS9960_REG_ENABLE 0x80
34 #define APDS9960_REG_ATIME 0x81
35 #define APDS9960_REG_WTIME 0x83
37 #define APDS9960_REG_AILTL 0x84
38 #define APDS9960_REG_AILTH 0x85
39 #define APDS9960_REG_AIHTL 0x86
40 #define APDS9960_REG_AIHTH 0x87
42 #define APDS9960_REG_PILT 0x89
43 #define APDS9960_REG_PIHT 0x8b
44 #define APDS9960_REG_PERS 0x8c
46 #define APDS9960_REG_CONFIG_1 0x8d
47 #define APDS9960_REG_PPULSE 0x8e
49 #define APDS9960_REG_CONTROL 0x8f
50 #define APDS9960_REG_CONTROL_AGAIN_MASK 0x03
51 #define APDS9960_REG_CONTROL_PGAIN_MASK 0x0c
52 #define APDS9960_REG_CONTROL_AGAIN_MASK_SHIFT 0
53 #define APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT 2
55 #define APDS9960_REG_CONFIG_2 0x90
56 #define APDS9960_REG_CONFIG_2_GGAIN_MASK 0x60
57 #define APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT 5
59 #define APDS9960_REG_ID 0x92
61 #define APDS9960_REG_STATUS 0x93
62 #define APDS9960_REG_STATUS_PS_INT BIT(5)
63 #define APDS9960_REG_STATUS_ALS_INT BIT(4)
64 #define APDS9960_REG_STATUS_GINT BIT(2)
66 #define APDS9960_REG_PDATA 0x9c
67 #define APDS9960_REG_POFFSET_UR 0x9d
68 #define APDS9960_REG_POFFSET_DL 0x9e
69 #define APDS9960_REG_CONFIG_3 0x9f
71 #define APDS9960_REG_GPENTH 0xa0
72 #define APDS9960_REG_GEXTH 0xa1
74 #define APDS9960_REG_GCONF_1 0xa2
75 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK 0xc0
76 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT 6
78 #define APDS9960_REG_GCONF_2 0xa3
79 #define APDS9960_REG_GOFFSET_U 0xa4
80 #define APDS9960_REG_GOFFSET_D 0xa5
81 #define APDS9960_REG_GPULSE 0xa6
82 #define APDS9960_REG_GOFFSET_L 0xa7
83 #define APDS9960_REG_GOFFSET_R 0xa9
84 #define APDS9960_REG_GCONF_3 0xaa
86 #define APDS9960_REG_GCONF_4 0xab
87 #define APDS9960_REG_GFLVL 0xae
88 #define APDS9960_REG_GSTATUS 0xaf
90 #define APDS9960_REG_IFORCE 0xe4
91 #define APDS9960_REG_PICLEAR 0xe5
92 #define APDS9960_REG_CICLEAR 0xe6
93 #define APDS9960_REG_AICLEAR 0xe7
95 #define APDS9960_DEFAULT_PERS 0x33
96 #define APDS9960_DEFAULT_GPENTH 0x50
97 #define APDS9960_DEFAULT_GEXTH 0x40
99 #define APDS9960_MAX_PXS_THRES_VAL 255
100 #define APDS9960_MAX_ALS_THRES_VAL 0xffff
101 #define APDS9960_MAX_INT_TIME_IN_US 1000000
103 enum apds9960_als_channel_idx
{
104 IDX_ALS_CLEAR
, IDX_ALS_RED
, IDX_ALS_GREEN
, IDX_ALS_BLUE
,
107 #define APDS9960_REG_ALS_BASE 0x94
108 #define APDS9960_REG_ALS_CHANNEL(_colour) \
109 (APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2))
111 enum apds9960_gesture_channel_idx
{
112 IDX_DIR_UP
, IDX_DIR_DOWN
, IDX_DIR_LEFT
, IDX_DIR_RIGHT
,
115 #define APDS9960_REG_GFIFO_BASE 0xfc
116 #define APDS9960_REG_GFIFO_DIR(_dir) \
117 (APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir)
119 struct apds9960_data
{
120 struct i2c_client
*client
;
121 struct iio_dev
*indio_dev
;
125 struct regmap
*regmap
;
126 struct regmap_field
*reg_int_als
;
127 struct regmap_field
*reg_int_ges
;
128 struct regmap_field
*reg_int_pxs
;
130 struct regmap_field
*reg_enable_als
;
131 struct regmap_field
*reg_enable_ges
;
132 struct regmap_field
*reg_enable_pxs
;
137 int gesture_mode_running
;
143 /* integration time value in us */
147 u8 buffer
[4]; /* 4 8-bit channels */
150 static const struct reg_default apds9960_reg_defaults
[] = {
151 /* Default ALS integration time = 2.48ms */
152 { APDS9960_REG_ATIME
, 0xff },
155 static const struct regmap_range apds9960_volatile_ranges
[] = {
156 regmap_reg_range(APDS9960_REG_STATUS
,
158 regmap_reg_range(APDS9960_REG_GFLVL
,
159 APDS9960_REG_GSTATUS
),
160 regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP
),
161 APDS9960_REG_GFIFO_DIR(RIGHT
)),
162 regmap_reg_range(APDS9960_REG_IFORCE
,
163 APDS9960_REG_AICLEAR
),
166 static const struct regmap_access_table apds9960_volatile_table
= {
167 .yes_ranges
= apds9960_volatile_ranges
,
168 .n_yes_ranges
= ARRAY_SIZE(apds9960_volatile_ranges
),
171 static const struct regmap_range apds9960_precious_ranges
[] = {
172 regmap_reg_range(APDS9960_REG_RAM_START
, APDS9960_REG_RAM_END
),
175 static const struct regmap_access_table apds9960_precious_table
= {
176 .yes_ranges
= apds9960_precious_ranges
,
177 .n_yes_ranges
= ARRAY_SIZE(apds9960_precious_ranges
),
180 static const struct regmap_range apds9960_readable_ranges
[] = {
181 regmap_reg_range(APDS9960_REG_ENABLE
,
182 APDS9960_REG_GSTATUS
),
183 regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP
),
184 APDS9960_REG_GFIFO_DIR(RIGHT
)),
187 static const struct regmap_access_table apds9960_readable_table
= {
188 .yes_ranges
= apds9960_readable_ranges
,
189 .n_yes_ranges
= ARRAY_SIZE(apds9960_readable_ranges
),
192 static const struct regmap_range apds9960_writeable_ranges
[] = {
193 regmap_reg_range(APDS9960_REG_ENABLE
, APDS9960_REG_CONFIG_2
),
194 regmap_reg_range(APDS9960_REG_POFFSET_UR
, APDS9960_REG_GCONF_4
),
195 regmap_reg_range(APDS9960_REG_IFORCE
, APDS9960_REG_AICLEAR
),
198 static const struct regmap_access_table apds9960_writeable_table
= {
199 .yes_ranges
= apds9960_writeable_ranges
,
200 .n_yes_ranges
= ARRAY_SIZE(apds9960_writeable_ranges
),
203 static const struct regmap_config apds9960_regmap_config
= {
204 .name
= APDS9960_REGMAP_NAME
,
207 .use_single_read
= true,
208 .use_single_write
= true,
210 .volatile_table
= &apds9960_volatile_table
,
211 .precious_table
= &apds9960_precious_table
,
212 .rd_table
= &apds9960_readable_table
,
213 .wr_table
= &apds9960_writeable_table
,
215 .reg_defaults
= apds9960_reg_defaults
,
216 .num_reg_defaults
= ARRAY_SIZE(apds9960_reg_defaults
),
217 .max_register
= APDS9960_REG_GFIFO_DIR(RIGHT
),
218 .cache_type
= REGCACHE_RBTREE
,
221 static const struct iio_event_spec apds9960_pxs_event_spec
[] = {
223 .type
= IIO_EV_TYPE_THRESH
,
224 .dir
= IIO_EV_DIR_RISING
,
225 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
226 BIT(IIO_EV_INFO_ENABLE
),
229 .type
= IIO_EV_TYPE_THRESH
,
230 .dir
= IIO_EV_DIR_FALLING
,
231 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
232 BIT(IIO_EV_INFO_ENABLE
),
236 static const struct iio_event_spec apds9960_als_event_spec
[] = {
238 .type
= IIO_EV_TYPE_THRESH
,
239 .dir
= IIO_EV_DIR_RISING
,
240 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
241 BIT(IIO_EV_INFO_ENABLE
),
244 .type
= IIO_EV_TYPE_THRESH
,
245 .dir
= IIO_EV_DIR_FALLING
,
246 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
247 BIT(IIO_EV_INFO_ENABLE
),
251 #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \
252 .type = IIO_PROXIMITY, \
253 .channel = _si + 1, \
263 #define APDS9960_INTENSITY_CHANNEL(_colour) { \
264 .type = IIO_INTENSITY, \
265 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
266 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
267 BIT(IIO_CHAN_INFO_INT_TIME), \
268 .channel2 = IIO_MOD_LIGHT_##_colour, \
269 .address = APDS9960_REG_ALS_CHANNEL(_colour), \
274 static const unsigned long apds9960_scan_masks
[] = {0xf, 0};
276 static const struct iio_chan_spec apds9960_channels
[] = {
278 .type
= IIO_PROXIMITY
,
279 .address
= APDS9960_REG_PDATA
,
280 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
281 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
286 .event_spec
= apds9960_pxs_event_spec
,
287 .num_event_specs
= ARRAY_SIZE(apds9960_pxs_event_spec
),
290 APDS9960_GESTURE_CHANNEL(UP
, 0),
291 APDS9960_GESTURE_CHANNEL(DOWN
, 1),
292 APDS9960_GESTURE_CHANNEL(LEFT
, 2),
293 APDS9960_GESTURE_CHANNEL(RIGHT
, 3),
296 .type
= IIO_INTENSITY
,
297 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
298 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
) |
299 BIT(IIO_CHAN_INFO_INT_TIME
),
300 .channel2
= IIO_MOD_LIGHT_CLEAR
,
301 .address
= APDS9960_REG_ALS_CHANNEL(CLEAR
),
305 .event_spec
= apds9960_als_event_spec
,
306 .num_event_specs
= ARRAY_SIZE(apds9960_als_event_spec
),
309 APDS9960_INTENSITY_CHANNEL(RED
),
310 APDS9960_INTENSITY_CHANNEL(GREEN
),
311 APDS9960_INTENSITY_CHANNEL(BLUE
),
314 /* integration time in us */
315 static const int apds9960_int_time
[][2] = {
323 static const int apds9960_pxs_gain_map
[] = {1, 2, 4, 8};
324 static const int apds9960_als_gain_map
[] = {1, 4, 16, 64};
326 static IIO_CONST_ATTR(proximity_scale_available
, "1 2 4 8");
327 static IIO_CONST_ATTR(intensity_scale_available
, "1 4 16 64");
328 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.028 0.1 0.2 0.7");
330 static struct attribute
*apds9960_attributes
[] = {
331 &iio_const_attr_proximity_scale_available
.dev_attr
.attr
,
332 &iio_const_attr_intensity_scale_available
.dev_attr
.attr
,
333 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
337 static const struct attribute_group apds9960_attribute_group
= {
338 .attrs
= apds9960_attributes
,
341 static const struct reg_field apds9960_reg_field_int_als
=
342 REG_FIELD(APDS9960_REG_ENABLE
, 4, 4);
344 static const struct reg_field apds9960_reg_field_int_ges
=
345 REG_FIELD(APDS9960_REG_GCONF_4
, 1, 1);
347 static const struct reg_field apds9960_reg_field_int_pxs
=
348 REG_FIELD(APDS9960_REG_ENABLE
, 5, 5);
350 static const struct reg_field apds9960_reg_field_enable_als
=
351 REG_FIELD(APDS9960_REG_ENABLE
, 1, 1);
353 static const struct reg_field apds9960_reg_field_enable_ges
=
354 REG_FIELD(APDS9960_REG_ENABLE
, 6, 6);
356 static const struct reg_field apds9960_reg_field_enable_pxs
=
357 REG_FIELD(APDS9960_REG_ENABLE
, 2, 2);
359 static int apds9960_set_it_time(struct apds9960_data
*data
, int val2
)
364 for (idx
= 0; idx
< ARRAY_SIZE(apds9960_int_time
); idx
++) {
365 if (apds9960_int_time
[idx
][0] == val2
) {
366 mutex_lock(&data
->lock
);
367 ret
= regmap_write(data
->regmap
, APDS9960_REG_ATIME
,
368 apds9960_int_time
[idx
][1]);
370 data
->als_adc_int_us
= val2
;
371 mutex_unlock(&data
->lock
);
379 static int apds9960_set_pxs_gain(struct apds9960_data
*data
, int val
)
384 for (idx
= 0; idx
< ARRAY_SIZE(apds9960_pxs_gain_map
); idx
++) {
385 if (apds9960_pxs_gain_map
[idx
] == val
) {
386 /* pxs + gesture gains are mirrored */
387 mutex_lock(&data
->lock
);
388 ret
= regmap_update_bits(data
->regmap
,
389 APDS9960_REG_CONTROL
,
390 APDS9960_REG_CONTROL_PGAIN_MASK
,
391 idx
<< APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT
);
393 mutex_unlock(&data
->lock
);
397 ret
= regmap_update_bits(data
->regmap
,
398 APDS9960_REG_CONFIG_2
,
399 APDS9960_REG_CONFIG_2_GGAIN_MASK
,
400 idx
<< APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT
);
402 data
->pxs_gain
= idx
;
403 mutex_unlock(&data
->lock
);
411 static int apds9960_set_als_gain(struct apds9960_data
*data
, int val
)
416 for (idx
= 0; idx
< ARRAY_SIZE(apds9960_als_gain_map
); idx
++) {
417 if (apds9960_als_gain_map
[idx
] == val
) {
418 mutex_lock(&data
->lock
);
419 ret
= regmap_update_bits(data
->regmap
,
420 APDS9960_REG_CONTROL
,
421 APDS9960_REG_CONTROL_AGAIN_MASK
, idx
);
423 data
->als_gain
= idx
;
424 mutex_unlock(&data
->lock
);
433 static int apds9960_set_power_state(struct apds9960_data
*data
, bool on
)
435 struct device
*dev
= &data
->client
->dev
;
438 mutex_lock(&data
->lock
);
443 suspended
= pm_runtime_suspended(dev
);
444 ret
= pm_runtime_get_sync(dev
);
446 /* Allow one integration cycle before allowing a reading */
448 usleep_range(data
->als_adc_int_us
,
449 APDS9960_MAX_INT_TIME_IN_US
);
451 pm_runtime_mark_last_busy(dev
);
452 ret
= pm_runtime_put_autosuspend(dev
);
455 mutex_unlock(&data
->lock
);
460 static int apds9960_set_power_state(struct apds9960_data
*data
, bool on
)
466 static int apds9960_read_raw(struct iio_dev
*indio_dev
,
467 struct iio_chan_spec
const *chan
,
468 int *val
, int *val2
, long mask
)
470 struct apds9960_data
*data
= iio_priv(indio_dev
);
474 if (data
->gesture_mode_running
)
478 case IIO_CHAN_INFO_RAW
:
479 apds9960_set_power_state(data
, true);
480 switch (chan
->type
) {
482 ret
= regmap_read(data
->regmap
, chan
->address
, val
);
487 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
491 *val
= le16_to_cpu(buf
);
497 apds9960_set_power_state(data
, false);
499 case IIO_CHAN_INFO_INT_TIME
:
500 /* RGB + ALS sensors only have integration time */
501 mutex_lock(&data
->lock
);
502 switch (chan
->type
) {
505 *val2
= data
->als_adc_int_us
;
506 ret
= IIO_VAL_INT_PLUS_MICRO
;
511 mutex_unlock(&data
->lock
);
513 case IIO_CHAN_INFO_SCALE
:
514 mutex_lock(&data
->lock
);
515 switch (chan
->type
) {
517 *val
= apds9960_pxs_gain_map
[data
->pxs_gain
];
521 *val
= apds9960_als_gain_map
[data
->als_gain
];
527 mutex_unlock(&data
->lock
);
534 static int apds9960_write_raw(struct iio_dev
*indio_dev
,
535 struct iio_chan_spec
const *chan
,
536 int val
, int val2
, long mask
)
538 struct apds9960_data
*data
= iio_priv(indio_dev
);
541 case IIO_CHAN_INFO_INT_TIME
:
542 /* RGB + ALS sensors only have int time */
543 switch (chan
->type
) {
547 return apds9960_set_it_time(data
, val2
);
551 case IIO_CHAN_INFO_SCALE
:
554 switch (chan
->type
) {
556 return apds9960_set_pxs_gain(data
, val
);
558 return apds9960_set_als_gain(data
, val
);
569 static inline int apds9960_get_thres_reg(const struct iio_chan_spec
*chan
,
570 enum iio_event_direction dir
,
574 case IIO_EV_DIR_RISING
:
575 switch (chan
->type
) {
577 *reg
= APDS9960_REG_PIHT
;
580 *reg
= APDS9960_REG_AIHTL
;
586 case IIO_EV_DIR_FALLING
:
587 switch (chan
->type
) {
589 *reg
= APDS9960_REG_PILT
;
592 *reg
= APDS9960_REG_AILTL
;
605 static int apds9960_read_event(struct iio_dev
*indio_dev
,
606 const struct iio_chan_spec
*chan
,
607 enum iio_event_type type
,
608 enum iio_event_direction dir
,
609 enum iio_event_info info
,
615 struct apds9960_data
*data
= iio_priv(indio_dev
);
617 if (info
!= IIO_EV_INFO_VALUE
)
620 ret
= apds9960_get_thres_reg(chan
, dir
, ®
);
624 if (chan
->type
== IIO_PROXIMITY
) {
625 ret
= regmap_read(data
->regmap
, reg
, val
);
628 } else if (chan
->type
== IIO_INTENSITY
) {
629 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
632 *val
= le16_to_cpu(buf
);
641 static int apds9960_write_event(struct iio_dev
*indio_dev
,
642 const struct iio_chan_spec
*chan
,
643 enum iio_event_type type
,
644 enum iio_event_direction dir
,
645 enum iio_event_info info
,
651 struct apds9960_data
*data
= iio_priv(indio_dev
);
653 if (info
!= IIO_EV_INFO_VALUE
)
656 ret
= apds9960_get_thres_reg(chan
, dir
, ®
);
660 if (chan
->type
== IIO_PROXIMITY
) {
661 if (val
< 0 || val
> APDS9960_MAX_PXS_THRES_VAL
)
663 ret
= regmap_write(data
->regmap
, reg
, val
);
666 } else if (chan
->type
== IIO_INTENSITY
) {
667 if (val
< 0 || val
> APDS9960_MAX_ALS_THRES_VAL
)
669 buf
= cpu_to_le16(val
);
670 ret
= regmap_bulk_write(data
->regmap
, reg
, &buf
, 2);
679 static int apds9960_read_event_config(struct iio_dev
*indio_dev
,
680 const struct iio_chan_spec
*chan
,
681 enum iio_event_type type
,
682 enum iio_event_direction dir
)
684 struct apds9960_data
*data
= iio_priv(indio_dev
);
686 switch (chan
->type
) {
688 return data
->pxs_int
;
690 return data
->als_int
;
698 static int apds9960_write_event_config(struct iio_dev
*indio_dev
,
699 const struct iio_chan_spec
*chan
,
700 enum iio_event_type type
,
701 enum iio_event_direction dir
,
704 struct apds9960_data
*data
= iio_priv(indio_dev
);
709 switch (chan
->type
) {
711 if (data
->pxs_int
== state
)
714 ret
= regmap_field_write(data
->reg_int_pxs
, state
);
717 data
->pxs_int
= state
;
718 apds9960_set_power_state(data
, state
);
721 if (data
->als_int
== state
)
724 ret
= regmap_field_write(data
->reg_int_als
, state
);
727 data
->als_int
= state
;
728 apds9960_set_power_state(data
, state
);
737 static const struct iio_info apds9960_info
= {
738 .attrs
= &apds9960_attribute_group
,
739 .read_raw
= apds9960_read_raw
,
740 .write_raw
= apds9960_write_raw
,
741 .read_event_value
= apds9960_read_event
,
742 .write_event_value
= apds9960_write_event
,
743 .read_event_config
= apds9960_read_event_config
,
744 .write_event_config
= apds9960_write_event_config
,
748 static inline int apds9660_fifo_is_empty(struct apds9960_data
*data
)
753 ret
= regmap_read(data
->regmap
, APDS9960_REG_GFLVL
, &cnt
);
760 static void apds9960_read_gesture_fifo(struct apds9960_data
*data
)
764 mutex_lock(&data
->lock
);
765 data
->gesture_mode_running
= 1;
767 while (cnt
|| (cnt
= apds9660_fifo_is_empty(data
) > 0)) {
768 ret
= regmap_bulk_read(data
->regmap
, APDS9960_REG_GFIFO_BASE
,
774 iio_push_to_buffers(data
->indio_dev
, data
->buffer
);
779 data
->gesture_mode_running
= 0;
780 mutex_unlock(&data
->lock
);
783 static irqreturn_t
apds9960_interrupt_handler(int irq
, void *private)
785 struct iio_dev
*indio_dev
= private;
786 struct apds9960_data
*data
= iio_priv(indio_dev
);
789 ret
= regmap_read(data
->regmap
, APDS9960_REG_STATUS
, &status
);
791 dev_err(&data
->client
->dev
, "irq status reg read failed\n");
795 if ((status
& APDS9960_REG_STATUS_ALS_INT
) && data
->als_int
) {
796 iio_push_event(indio_dev
,
797 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY
, 0,
800 iio_get_time_ns(indio_dev
));
801 regmap_write(data
->regmap
, APDS9960_REG_CICLEAR
, 1);
804 if ((status
& APDS9960_REG_STATUS_PS_INT
) && data
->pxs_int
) {
805 iio_push_event(indio_dev
,
806 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, 0,
809 iio_get_time_ns(indio_dev
));
810 regmap_write(data
->regmap
, APDS9960_REG_PICLEAR
, 1);
813 if (status
& APDS9960_REG_STATUS_GINT
)
814 apds9960_read_gesture_fifo(data
);
819 static int apds9960_set_powermode(struct apds9960_data
*data
, bool state
)
821 return regmap_update_bits(data
->regmap
, APDS9960_REG_ENABLE
, 1, state
);
824 static int apds9960_buffer_postenable(struct iio_dev
*indio_dev
)
826 struct apds9960_data
*data
= iio_priv(indio_dev
);
829 ret
= regmap_field_write(data
->reg_int_ges
, 1);
833 ret
= regmap_field_write(data
->reg_enable_ges
, 1);
837 pm_runtime_get_sync(&data
->client
->dev
);
842 static int apds9960_buffer_predisable(struct iio_dev
*indio_dev
)
844 struct apds9960_data
*data
= iio_priv(indio_dev
);
847 ret
= regmap_field_write(data
->reg_enable_ges
, 0);
851 ret
= regmap_field_write(data
->reg_int_ges
, 0);
855 pm_runtime_put_autosuspend(&data
->client
->dev
);
860 static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops
= {
861 .postenable
= apds9960_buffer_postenable
,
862 .predisable
= apds9960_buffer_predisable
,
865 static int apds9960_regfield_init(struct apds9960_data
*data
)
867 struct device
*dev
= &data
->client
->dev
;
868 struct regmap
*regmap
= data
->regmap
;
870 data
->reg_int_als
= devm_regmap_field_alloc(dev
, regmap
,
871 apds9960_reg_field_int_als
);
872 if (IS_ERR(data
->reg_int_als
)) {
873 dev_err(dev
, "INT ALS reg field init failed\n");
874 return PTR_ERR(data
->reg_int_als
);
877 data
->reg_int_ges
= devm_regmap_field_alloc(dev
, regmap
,
878 apds9960_reg_field_int_ges
);
879 if (IS_ERR(data
->reg_int_ges
)) {
880 dev_err(dev
, "INT gesture reg field init failed\n");
881 return PTR_ERR(data
->reg_int_ges
);
884 data
->reg_int_pxs
= devm_regmap_field_alloc(dev
, regmap
,
885 apds9960_reg_field_int_pxs
);
886 if (IS_ERR(data
->reg_int_pxs
)) {
887 dev_err(dev
, "INT pxs reg field init failed\n");
888 return PTR_ERR(data
->reg_int_pxs
);
891 data
->reg_enable_als
= devm_regmap_field_alloc(dev
, regmap
,
892 apds9960_reg_field_enable_als
);
893 if (IS_ERR(data
->reg_enable_als
)) {
894 dev_err(dev
, "Enable ALS reg field init failed\n");
895 return PTR_ERR(data
->reg_enable_als
);
898 data
->reg_enable_ges
= devm_regmap_field_alloc(dev
, regmap
,
899 apds9960_reg_field_enable_ges
);
900 if (IS_ERR(data
->reg_enable_ges
)) {
901 dev_err(dev
, "Enable gesture reg field init failed\n");
902 return PTR_ERR(data
->reg_enable_ges
);
905 data
->reg_enable_pxs
= devm_regmap_field_alloc(dev
, regmap
,
906 apds9960_reg_field_enable_pxs
);
907 if (IS_ERR(data
->reg_enable_pxs
)) {
908 dev_err(dev
, "Enable PXS reg field init failed\n");
909 return PTR_ERR(data
->reg_enable_pxs
);
915 static int apds9960_chip_init(struct apds9960_data
*data
)
919 /* Default IT for ALS of 28 ms */
920 ret
= apds9960_set_it_time(data
, 28000);
924 /* Ensure gesture interrupt is OFF */
925 ret
= regmap_field_write(data
->reg_int_ges
, 0);
929 /* Disable gesture sensor, since polling is useless from user-space */
930 ret
= regmap_field_write(data
->reg_enable_ges
, 0);
934 /* Ensure proximity interrupt is OFF */
935 ret
= regmap_field_write(data
->reg_int_pxs
, 0);
939 /* Enable proximity sensor for polling */
940 ret
= regmap_field_write(data
->reg_enable_pxs
, 1);
944 /* Ensure ALS interrupt is OFF */
945 ret
= regmap_field_write(data
->reg_int_als
, 0);
949 /* Enable ALS sensor for polling */
950 ret
= regmap_field_write(data
->reg_enable_als
, 1);
954 * When enabled trigger an interrupt after 3 readings
955 * outside threshold for ALS + PXS
957 ret
= regmap_write(data
->regmap
, APDS9960_REG_PERS
,
958 APDS9960_DEFAULT_PERS
);
963 * Wait for 4 event outside gesture threshold to prevent interrupt
966 ret
= regmap_update_bits(data
->regmap
, APDS9960_REG_GCONF_1
,
967 APDS9960_REG_GCONF_1_GFIFO_THRES_MASK
,
968 BIT(0) << APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT
);
972 /* Default ENTER and EXIT thresholds for the GESTURE engine. */
973 ret
= regmap_write(data
->regmap
, APDS9960_REG_GPENTH
,
974 APDS9960_DEFAULT_GPENTH
);
978 ret
= regmap_write(data
->regmap
, APDS9960_REG_GEXTH
,
979 APDS9960_DEFAULT_GEXTH
);
983 return apds9960_set_powermode(data
, 1);
986 static int apds9960_probe(struct i2c_client
*client
,
987 const struct i2c_device_id
*id
)
989 struct apds9960_data
*data
;
990 struct iio_buffer
*buffer
;
991 struct iio_dev
*indio_dev
;
994 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
998 buffer
= devm_iio_kfifo_allocate(&client
->dev
);
1002 iio_device_attach_buffer(indio_dev
, buffer
);
1004 indio_dev
->dev
.parent
= &client
->dev
;
1005 indio_dev
->info
= &apds9960_info
;
1006 indio_dev
->name
= APDS9960_DRV_NAME
;
1007 indio_dev
->channels
= apds9960_channels
;
1008 indio_dev
->num_channels
= ARRAY_SIZE(apds9960_channels
);
1009 indio_dev
->available_scan_masks
= apds9960_scan_masks
;
1010 indio_dev
->modes
= (INDIO_BUFFER_SOFTWARE
| INDIO_DIRECT_MODE
);
1011 indio_dev
->setup_ops
= &apds9960_buffer_setup_ops
;
1013 data
= iio_priv(indio_dev
);
1014 i2c_set_clientdata(client
, indio_dev
);
1016 data
->regmap
= devm_regmap_init_i2c(client
, &apds9960_regmap_config
);
1017 if (IS_ERR(data
->regmap
)) {
1018 dev_err(&client
->dev
, "regmap initialization failed.\n");
1019 return PTR_ERR(data
->regmap
);
1022 data
->client
= client
;
1023 data
->indio_dev
= indio_dev
;
1024 mutex_init(&data
->lock
);
1026 ret
= pm_runtime_set_active(&client
->dev
);
1028 goto error_power_down
;
1030 pm_runtime_enable(&client
->dev
);
1031 pm_runtime_set_autosuspend_delay(&client
->dev
, 5000);
1032 pm_runtime_use_autosuspend(&client
->dev
);
1034 apds9960_set_power_state(data
, true);
1036 ret
= apds9960_regfield_init(data
);
1038 goto error_power_down
;
1040 ret
= apds9960_chip_init(data
);
1042 goto error_power_down
;
1044 if (client
->irq
<= 0) {
1045 dev_err(&client
->dev
, "no valid irq defined\n");
1047 goto error_power_down
;
1049 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1050 NULL
, apds9960_interrupt_handler
,
1051 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1055 dev_err(&client
->dev
, "request irq (%d) failed\n", client
->irq
);
1056 goto error_power_down
;
1059 ret
= iio_device_register(indio_dev
);
1061 goto error_power_down
;
1063 apds9960_set_power_state(data
, false);
1068 apds9960_set_power_state(data
, false);
1073 static int apds9960_remove(struct i2c_client
*client
)
1075 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1076 struct apds9960_data
*data
= iio_priv(indio_dev
);
1078 iio_device_unregister(indio_dev
);
1079 pm_runtime_disable(&client
->dev
);
1080 pm_runtime_set_suspended(&client
->dev
);
1081 apds9960_set_powermode(data
, 0);
1087 static int apds9960_runtime_suspend(struct device
*dev
)
1089 struct apds9960_data
*data
=
1090 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
1092 return apds9960_set_powermode(data
, 0);
1095 static int apds9960_runtime_resume(struct device
*dev
)
1097 struct apds9960_data
*data
=
1098 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
1100 return apds9960_set_powermode(data
, 1);
1104 static const struct dev_pm_ops apds9960_pm_ops
= {
1105 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1106 pm_runtime_force_resume
)
1107 SET_RUNTIME_PM_OPS(apds9960_runtime_suspend
,
1108 apds9960_runtime_resume
, NULL
)
1111 static const struct i2c_device_id apds9960_id
[] = {
1115 MODULE_DEVICE_TABLE(i2c
, apds9960_id
);
1117 static const struct of_device_id apds9960_of_match
[] = {
1118 { .compatible
= "avago,apds9960" },
1121 MODULE_DEVICE_TABLE(of
, apds9960_of_match
);
1123 static struct i2c_driver apds9960_driver
= {
1125 .name
= APDS9960_DRV_NAME
,
1126 .of_match_table
= apds9960_of_match
,
1127 .pm
= &apds9960_pm_ops
,
1129 .probe
= apds9960_probe
,
1130 .remove
= apds9960_remove
,
1131 .id_table
= apds9960_id
,
1133 module_i2c_driver(apds9960_driver
);
1135 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
1136 MODULE_DESCRIPTION("APDS9960 Gesture/RGB/ALS/Proximity sensor");
1137 MODULE_LICENSE("GPL");