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/acpi.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
18 #include <linux/irq.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>
28 #define APDS9960_REGMAP_NAME "apds9960_regmap"
29 #define APDS9960_DRV_NAME "apds9960"
31 #define APDS9960_REG_RAM_START 0x00
32 #define APDS9960_REG_RAM_END 0x7f
34 #define APDS9960_REG_ENABLE 0x80
35 #define APDS9960_REG_ATIME 0x81
36 #define APDS9960_REG_WTIME 0x83
38 #define APDS9960_REG_AILTL 0x84
39 #define APDS9960_REG_AILTH 0x85
40 #define APDS9960_REG_AIHTL 0x86
41 #define APDS9960_REG_AIHTH 0x87
43 #define APDS9960_REG_PILT 0x89
44 #define APDS9960_REG_PIHT 0x8b
45 #define APDS9960_REG_PERS 0x8c
47 #define APDS9960_REG_CONFIG_1 0x8d
48 #define APDS9960_REG_PPULSE 0x8e
50 #define APDS9960_REG_CONTROL 0x8f
51 #define APDS9960_REG_CONTROL_AGAIN_MASK 0x03
52 #define APDS9960_REG_CONTROL_PGAIN_MASK 0x0c
53 #define APDS9960_REG_CONTROL_AGAIN_MASK_SHIFT 0
54 #define APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT 2
56 #define APDS9960_REG_CONFIG_2 0x90
57 #define APDS9960_REG_ID 0x92
59 #define APDS9960_REG_STATUS 0x93
60 #define APDS9960_REG_STATUS_PS_INT BIT(5)
61 #define APDS9960_REG_STATUS_ALS_INT BIT(4)
62 #define APDS9960_REG_STATUS_GINT BIT(2)
64 #define APDS9960_REG_PDATA 0x9c
65 #define APDS9960_REG_POFFSET_UR 0x9d
66 #define APDS9960_REG_POFFSET_DL 0x9e
67 #define APDS9960_REG_CONFIG_3 0x9f
69 #define APDS9960_REG_GPENTH 0xa0
70 #define APDS9960_REG_GEXTH 0xa1
72 #define APDS9960_REG_GCONF_1 0xa2
73 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK 0xc0
74 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT 6
76 #define APDS9960_REG_GCONF_2 0xa3
77 #define APDS9960_REG_GCONF_2_GGAIN_MASK 0x60
78 #define APDS9960_REG_GCONF_2_GGAIN_MASK_SHIFT 5
80 #define APDS9960_REG_GOFFSET_U 0xa4
81 #define APDS9960_REG_GOFFSET_D 0xa5
82 #define APDS9960_REG_GPULSE 0xa6
83 #define APDS9960_REG_GOFFSET_L 0xa7
84 #define APDS9960_REG_GOFFSET_R 0xa9
85 #define APDS9960_REG_GCONF_3 0xaa
87 #define APDS9960_REG_GCONF_4 0xab
88 #define APDS9960_REG_GFLVL 0xae
89 #define APDS9960_REG_GSTATUS 0xaf
91 #define APDS9960_REG_IFORCE 0xe4
92 #define APDS9960_REG_PICLEAR 0xe5
93 #define APDS9960_REG_CICLEAR 0xe6
94 #define APDS9960_REG_AICLEAR 0xe7
96 #define APDS9960_DEFAULT_PERS 0x33
97 #define APDS9960_DEFAULT_GPENTH 0x50
98 #define APDS9960_DEFAULT_GEXTH 0x40
100 #define APDS9960_MAX_PXS_THRES_VAL 255
101 #define APDS9960_MAX_ALS_THRES_VAL 0xffff
102 #define APDS9960_MAX_INT_TIME_IN_US 1000000
104 enum apds9960_als_channel_idx {
105 IDX_ALS_CLEAR, IDX_ALS_RED, IDX_ALS_GREEN, IDX_ALS_BLUE,
108 #define APDS9960_REG_ALS_BASE 0x94
109 #define APDS9960_REG_ALS_CHANNEL(_colour) \
110 (APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2))
112 enum apds9960_gesture_channel_idx {
113 IDX_DIR_UP, IDX_DIR_DOWN, IDX_DIR_LEFT, IDX_DIR_RIGHT,
116 #define APDS9960_REG_GFIFO_BASE 0xfc
117 #define APDS9960_REG_GFIFO_DIR(_dir) \
118 (APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir)
120 struct apds9960_data {
121 struct i2c_client *client;
122 struct iio_dev *indio_dev;
126 struct regmap *regmap;
127 struct regmap_field *reg_int_als;
128 struct regmap_field *reg_int_ges;
129 struct regmap_field *reg_int_pxs;
131 struct regmap_field *reg_enable_als;
132 struct regmap_field *reg_enable_ges;
133 struct regmap_field *reg_enable_pxs;
138 int gesture_mode_running;
144 /* integration time value in us */
148 u8 buffer[4]; /* 4 8-bit channels */
151 static const struct reg_default apds9960_reg_defaults[] = {
152 /* Default ALS integration time = 2.48ms */
153 { APDS9960_REG_ATIME, 0xff },
156 static const struct regmap_range apds9960_volatile_ranges[] = {
157 regmap_reg_range(APDS9960_REG_STATUS,
159 regmap_reg_range(APDS9960_REG_GFLVL,
160 APDS9960_REG_GSTATUS),
161 regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
162 APDS9960_REG_GFIFO_DIR(RIGHT)),
163 regmap_reg_range(APDS9960_REG_IFORCE,
164 APDS9960_REG_AICLEAR),
167 static const struct regmap_access_table apds9960_volatile_table = {
168 .yes_ranges = apds9960_volatile_ranges,
169 .n_yes_ranges = ARRAY_SIZE(apds9960_volatile_ranges),
172 static const struct regmap_range apds9960_precious_ranges[] = {
173 regmap_reg_range(APDS9960_REG_RAM_START, APDS9960_REG_RAM_END),
176 static const struct regmap_access_table apds9960_precious_table = {
177 .yes_ranges = apds9960_precious_ranges,
178 .n_yes_ranges = ARRAY_SIZE(apds9960_precious_ranges),
181 static const struct regmap_range apds9960_readable_ranges[] = {
182 regmap_reg_range(APDS9960_REG_ENABLE,
183 APDS9960_REG_GSTATUS),
184 regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
185 APDS9960_REG_GFIFO_DIR(RIGHT)),
188 static const struct regmap_access_table apds9960_readable_table = {
189 .yes_ranges = apds9960_readable_ranges,
190 .n_yes_ranges = ARRAY_SIZE(apds9960_readable_ranges),
193 static const struct regmap_range apds9960_writeable_ranges[] = {
194 regmap_reg_range(APDS9960_REG_ENABLE, APDS9960_REG_CONFIG_2),
195 regmap_reg_range(APDS9960_REG_POFFSET_UR, APDS9960_REG_GCONF_4),
196 regmap_reg_range(APDS9960_REG_IFORCE, APDS9960_REG_AICLEAR),
199 static const struct regmap_access_table apds9960_writeable_table = {
200 .yes_ranges = apds9960_writeable_ranges,
201 .n_yes_ranges = ARRAY_SIZE(apds9960_writeable_ranges),
204 static const struct regmap_config apds9960_regmap_config = {
205 .name = APDS9960_REGMAP_NAME,
208 .use_single_read = true,
209 .use_single_write = true,
211 .volatile_table = &apds9960_volatile_table,
212 .precious_table = &apds9960_precious_table,
213 .rd_table = &apds9960_readable_table,
214 .wr_table = &apds9960_writeable_table,
216 .reg_defaults = apds9960_reg_defaults,
217 .num_reg_defaults = ARRAY_SIZE(apds9960_reg_defaults),
218 .max_register = APDS9960_REG_GFIFO_DIR(RIGHT),
219 .cache_type = REGCACHE_RBTREE,
222 static const struct iio_event_spec apds9960_pxs_event_spec[] = {
224 .type = IIO_EV_TYPE_THRESH,
225 .dir = IIO_EV_DIR_RISING,
226 .mask_separate = BIT(IIO_EV_INFO_VALUE),
229 .type = IIO_EV_TYPE_THRESH,
230 .dir = IIO_EV_DIR_FALLING,
231 .mask_separate = BIT(IIO_EV_INFO_VALUE),
234 .type = IIO_EV_TYPE_THRESH,
235 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
239 static const struct iio_event_spec apds9960_als_event_spec[] = {
241 .type = IIO_EV_TYPE_THRESH,
242 .dir = IIO_EV_DIR_RISING,
243 .mask_separate = BIT(IIO_EV_INFO_VALUE),
246 .type = IIO_EV_TYPE_THRESH,
247 .dir = IIO_EV_DIR_FALLING,
248 .mask_separate = BIT(IIO_EV_INFO_VALUE),
251 .type = IIO_EV_TYPE_THRESH,
252 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
256 #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \
257 .type = IIO_PROXIMITY, \
258 .channel = _si + 1, \
268 #define APDS9960_INTENSITY_CHANNEL(_colour) { \
269 .type = IIO_INTENSITY, \
270 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
271 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
272 BIT(IIO_CHAN_INFO_INT_TIME), \
273 .channel2 = IIO_MOD_LIGHT_##_colour, \
274 .address = APDS9960_REG_ALS_CHANNEL(_colour), \
279 static const unsigned long apds9960_scan_masks[] = {0xf, 0};
281 static const struct iio_chan_spec apds9960_channels[] = {
283 .type = IIO_PROXIMITY,
284 .address = APDS9960_REG_PDATA,
285 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
286 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
291 .event_spec = apds9960_pxs_event_spec,
292 .num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
295 APDS9960_GESTURE_CHANNEL(UP, 0),
296 APDS9960_GESTURE_CHANNEL(DOWN, 1),
297 APDS9960_GESTURE_CHANNEL(LEFT, 2),
298 APDS9960_GESTURE_CHANNEL(RIGHT, 3),
301 .type = IIO_INTENSITY,
302 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
303 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
304 BIT(IIO_CHAN_INFO_INT_TIME),
305 .channel2 = IIO_MOD_LIGHT_CLEAR,
306 .address = APDS9960_REG_ALS_CHANNEL(CLEAR),
310 .event_spec = apds9960_als_event_spec,
311 .num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
314 APDS9960_INTENSITY_CHANNEL(RED),
315 APDS9960_INTENSITY_CHANNEL(GREEN),
316 APDS9960_INTENSITY_CHANNEL(BLUE),
319 /* integration time in us */
320 static const int apds9960_int_time[][2] = {
328 static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8};
329 static const int apds9960_als_gain_map[] = {1, 4, 16, 64};
331 static IIO_CONST_ATTR(proximity_scale_available, "1 2 4 8");
332 static IIO_CONST_ATTR(intensity_scale_available, "1 4 16 64");
333 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.028 0.1 0.2 0.7");
335 static struct attribute *apds9960_attributes[] = {
336 &iio_const_attr_proximity_scale_available.dev_attr.attr,
337 &iio_const_attr_intensity_scale_available.dev_attr.attr,
338 &iio_const_attr_integration_time_available.dev_attr.attr,
342 static const struct attribute_group apds9960_attribute_group = {
343 .attrs = apds9960_attributes,
346 static const struct reg_field apds9960_reg_field_int_als =
347 REG_FIELD(APDS9960_REG_ENABLE, 4, 4);
349 static const struct reg_field apds9960_reg_field_int_ges =
350 REG_FIELD(APDS9960_REG_GCONF_4, 1, 1);
352 static const struct reg_field apds9960_reg_field_int_pxs =
353 REG_FIELD(APDS9960_REG_ENABLE, 5, 5);
355 static const struct reg_field apds9960_reg_field_enable_als =
356 REG_FIELD(APDS9960_REG_ENABLE, 1, 1);
358 static const struct reg_field apds9960_reg_field_enable_ges =
359 REG_FIELD(APDS9960_REG_ENABLE, 6, 6);
361 static const struct reg_field apds9960_reg_field_enable_pxs =
362 REG_FIELD(APDS9960_REG_ENABLE, 2, 2);
364 static int apds9960_set_it_time(struct apds9960_data *data, int val2)
369 for (idx = 0; idx < ARRAY_SIZE(apds9960_int_time); idx++) {
370 if (apds9960_int_time[idx][0] == val2) {
371 mutex_lock(&data->lock);
372 ret = regmap_write(data->regmap, APDS9960_REG_ATIME,
373 apds9960_int_time[idx][1]);
375 data->als_adc_int_us = val2;
376 mutex_unlock(&data->lock);
384 static int apds9960_set_pxs_gain(struct apds9960_data *data, int val)
389 for (idx = 0; idx < ARRAY_SIZE(apds9960_pxs_gain_map); idx++) {
390 if (apds9960_pxs_gain_map[idx] == val) {
391 /* pxs + gesture gains are mirrored */
392 mutex_lock(&data->lock);
393 ret = regmap_update_bits(data->regmap,
394 APDS9960_REG_CONTROL,
395 APDS9960_REG_CONTROL_PGAIN_MASK,
396 idx << APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT);
398 mutex_unlock(&data->lock);
402 ret = regmap_update_bits(data->regmap,
403 APDS9960_REG_GCONF_2,
404 APDS9960_REG_GCONF_2_GGAIN_MASK,
405 idx << APDS9960_REG_GCONF_2_GGAIN_MASK_SHIFT);
407 data->pxs_gain = idx;
408 mutex_unlock(&data->lock);
416 static int apds9960_set_als_gain(struct apds9960_data *data, int val)
421 for (idx = 0; idx < ARRAY_SIZE(apds9960_als_gain_map); idx++) {
422 if (apds9960_als_gain_map[idx] == val) {
423 mutex_lock(&data->lock);
424 ret = regmap_update_bits(data->regmap,
425 APDS9960_REG_CONTROL,
426 APDS9960_REG_CONTROL_AGAIN_MASK, idx);
428 data->als_gain = idx;
429 mutex_unlock(&data->lock);
438 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
440 struct device *dev = &data->client->dev;
443 mutex_lock(&data->lock);
448 suspended = pm_runtime_suspended(dev);
449 ret = pm_runtime_get_sync(dev);
451 /* Allow one integration cycle before allowing a reading */
453 usleep_range(data->als_adc_int_us,
454 APDS9960_MAX_INT_TIME_IN_US);
456 pm_runtime_mark_last_busy(dev);
457 ret = pm_runtime_put_autosuspend(dev);
460 mutex_unlock(&data->lock);
465 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
471 static int apds9960_read_raw(struct iio_dev *indio_dev,
472 struct iio_chan_spec const *chan,
473 int *val, int *val2, long mask)
475 struct apds9960_data *data = iio_priv(indio_dev);
479 if (data->gesture_mode_running)
483 case IIO_CHAN_INFO_RAW:
484 apds9960_set_power_state(data, true);
485 switch (chan->type) {
487 ret = regmap_read(data->regmap, chan->address, val);
492 ret = regmap_bulk_read(data->regmap, chan->address,
496 *val = le16_to_cpu(buf);
502 apds9960_set_power_state(data, false);
504 case IIO_CHAN_INFO_INT_TIME:
505 /* RGB + ALS sensors only have integration time */
506 mutex_lock(&data->lock);
507 switch (chan->type) {
510 *val2 = data->als_adc_int_us;
511 ret = IIO_VAL_INT_PLUS_MICRO;
516 mutex_unlock(&data->lock);
518 case IIO_CHAN_INFO_SCALE:
519 mutex_lock(&data->lock);
520 switch (chan->type) {
522 *val = apds9960_pxs_gain_map[data->pxs_gain];
526 *val = apds9960_als_gain_map[data->als_gain];
532 mutex_unlock(&data->lock);
539 static int apds9960_write_raw(struct iio_dev *indio_dev,
540 struct iio_chan_spec const *chan,
541 int val, int val2, long mask)
543 struct apds9960_data *data = iio_priv(indio_dev);
546 case IIO_CHAN_INFO_INT_TIME:
547 /* RGB + ALS sensors only have int time */
548 switch (chan->type) {
552 return apds9960_set_it_time(data, val2);
556 case IIO_CHAN_INFO_SCALE:
559 switch (chan->type) {
561 return apds9960_set_pxs_gain(data, val);
563 return apds9960_set_als_gain(data, val);
574 static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
575 enum iio_event_direction dir,
579 case IIO_EV_DIR_RISING:
580 switch (chan->type) {
582 *reg = APDS9960_REG_PIHT;
585 *reg = APDS9960_REG_AIHTL;
591 case IIO_EV_DIR_FALLING:
592 switch (chan->type) {
594 *reg = APDS9960_REG_PILT;
597 *reg = APDS9960_REG_AILTL;
610 static int apds9960_read_event(struct iio_dev *indio_dev,
611 const struct iio_chan_spec *chan,
612 enum iio_event_type type,
613 enum iio_event_direction dir,
614 enum iio_event_info info,
620 struct apds9960_data *data = iio_priv(indio_dev);
622 if (info != IIO_EV_INFO_VALUE)
625 ret = apds9960_get_thres_reg(chan, dir, ®);
629 if (chan->type == IIO_PROXIMITY) {
630 ret = regmap_read(data->regmap, reg, val);
633 } else if (chan->type == IIO_INTENSITY) {
634 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
637 *val = le16_to_cpu(buf);
646 static int apds9960_write_event(struct iio_dev *indio_dev,
647 const struct iio_chan_spec *chan,
648 enum iio_event_type type,
649 enum iio_event_direction dir,
650 enum iio_event_info info,
656 struct apds9960_data *data = iio_priv(indio_dev);
658 if (info != IIO_EV_INFO_VALUE)
661 ret = apds9960_get_thres_reg(chan, dir, ®);
665 if (chan->type == IIO_PROXIMITY) {
666 if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL)
668 ret = regmap_write(data->regmap, reg, val);
671 } else if (chan->type == IIO_INTENSITY) {
672 if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL)
674 buf = cpu_to_le16(val);
675 ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
684 static int apds9960_read_event_config(struct iio_dev *indio_dev,
685 const struct iio_chan_spec *chan,
686 enum iio_event_type type,
687 enum iio_event_direction dir)
689 struct apds9960_data *data = iio_priv(indio_dev);
691 switch (chan->type) {
693 return data->pxs_int;
695 return data->als_int;
703 static int apds9960_write_event_config(struct iio_dev *indio_dev,
704 const struct iio_chan_spec *chan,
705 enum iio_event_type type,
706 enum iio_event_direction dir,
709 struct apds9960_data *data = iio_priv(indio_dev);
714 switch (chan->type) {
716 if (data->pxs_int == state)
719 ret = regmap_field_write(data->reg_int_pxs, state);
722 data->pxs_int = state;
723 apds9960_set_power_state(data, state);
726 if (data->als_int == state)
729 ret = regmap_field_write(data->reg_int_als, state);
732 data->als_int = state;
733 apds9960_set_power_state(data, state);
742 static const struct iio_info apds9960_info = {
743 .attrs = &apds9960_attribute_group,
744 .read_raw = apds9960_read_raw,
745 .write_raw = apds9960_write_raw,
746 .read_event_value = apds9960_read_event,
747 .write_event_value = apds9960_write_event,
748 .read_event_config = apds9960_read_event_config,
749 .write_event_config = apds9960_write_event_config,
753 static inline int apds9660_fifo_is_empty(struct apds9960_data *data)
758 ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt);
765 static void apds9960_read_gesture_fifo(struct apds9960_data *data)
769 mutex_lock(&data->lock);
770 data->gesture_mode_running = 1;
772 while (cnt || (cnt = apds9660_fifo_is_empty(data) > 0)) {
773 ret = regmap_bulk_read(data->regmap, APDS9960_REG_GFIFO_BASE,
779 iio_push_to_buffers(data->indio_dev, data->buffer);
784 data->gesture_mode_running = 0;
785 mutex_unlock(&data->lock);
788 static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
790 struct iio_dev *indio_dev = private;
791 struct apds9960_data *data = iio_priv(indio_dev);
794 ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status);
796 dev_err(&data->client->dev, "irq status reg read failed\n");
800 if ((status & APDS9960_REG_STATUS_ALS_INT) && data->als_int) {
801 iio_push_event(indio_dev,
802 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
805 iio_get_time_ns(indio_dev));
806 regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1);
809 if ((status & APDS9960_REG_STATUS_PS_INT) && data->pxs_int) {
810 iio_push_event(indio_dev,
811 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
814 iio_get_time_ns(indio_dev));
815 regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1);
818 if (status & APDS9960_REG_STATUS_GINT)
819 apds9960_read_gesture_fifo(data);
824 static int apds9960_set_powermode(struct apds9960_data *data, bool state)
826 return regmap_update_bits(data->regmap, APDS9960_REG_ENABLE, 1, state);
829 static int apds9960_buffer_postenable(struct iio_dev *indio_dev)
831 struct apds9960_data *data = iio_priv(indio_dev);
834 ret = regmap_field_write(data->reg_int_ges, 1);
838 ret = regmap_field_write(data->reg_enable_ges, 1);
842 pm_runtime_get_sync(&data->client->dev);
847 static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
849 struct apds9960_data *data = iio_priv(indio_dev);
852 ret = regmap_field_write(data->reg_enable_ges, 0);
856 ret = regmap_field_write(data->reg_int_ges, 0);
860 pm_runtime_put_autosuspend(&data->client->dev);
865 static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops = {
866 .postenable = apds9960_buffer_postenable,
867 .predisable = apds9960_buffer_predisable,
870 static int apds9960_regfield_init(struct apds9960_data *data)
872 struct device *dev = &data->client->dev;
873 struct regmap *regmap = data->regmap;
875 data->reg_int_als = devm_regmap_field_alloc(dev, regmap,
876 apds9960_reg_field_int_als);
877 if (IS_ERR(data->reg_int_als)) {
878 dev_err(dev, "INT ALS reg field init failed\n");
879 return PTR_ERR(data->reg_int_als);
882 data->reg_int_ges = devm_regmap_field_alloc(dev, regmap,
883 apds9960_reg_field_int_ges);
884 if (IS_ERR(data->reg_int_ges)) {
885 dev_err(dev, "INT gesture reg field init failed\n");
886 return PTR_ERR(data->reg_int_ges);
889 data->reg_int_pxs = devm_regmap_field_alloc(dev, regmap,
890 apds9960_reg_field_int_pxs);
891 if (IS_ERR(data->reg_int_pxs)) {
892 dev_err(dev, "INT pxs reg field init failed\n");
893 return PTR_ERR(data->reg_int_pxs);
896 data->reg_enable_als = devm_regmap_field_alloc(dev, regmap,
897 apds9960_reg_field_enable_als);
898 if (IS_ERR(data->reg_enable_als)) {
899 dev_err(dev, "Enable ALS reg field init failed\n");
900 return PTR_ERR(data->reg_enable_als);
903 data->reg_enable_ges = devm_regmap_field_alloc(dev, regmap,
904 apds9960_reg_field_enable_ges);
905 if (IS_ERR(data->reg_enable_ges)) {
906 dev_err(dev, "Enable gesture reg field init failed\n");
907 return PTR_ERR(data->reg_enable_ges);
910 data->reg_enable_pxs = devm_regmap_field_alloc(dev, regmap,
911 apds9960_reg_field_enable_pxs);
912 if (IS_ERR(data->reg_enable_pxs)) {
913 dev_err(dev, "Enable PXS reg field init failed\n");
914 return PTR_ERR(data->reg_enable_pxs);
920 static int apds9960_chip_init(struct apds9960_data *data)
924 /* Default IT for ALS of 28 ms */
925 ret = apds9960_set_it_time(data, 28000);
929 /* Ensure gesture interrupt is OFF */
930 ret = regmap_field_write(data->reg_int_ges, 0);
934 /* Disable gesture sensor, since polling is useless from user-space */
935 ret = regmap_field_write(data->reg_enable_ges, 0);
939 /* Ensure proximity interrupt is OFF */
940 ret = regmap_field_write(data->reg_int_pxs, 0);
944 /* Enable proximity sensor for polling */
945 ret = regmap_field_write(data->reg_enable_pxs, 1);
949 /* Ensure ALS interrupt is OFF */
950 ret = regmap_field_write(data->reg_int_als, 0);
954 /* Enable ALS sensor for polling */
955 ret = regmap_field_write(data->reg_enable_als, 1);
959 * When enabled trigger an interrupt after 3 readings
960 * outside threshold for ALS + PXS
962 ret = regmap_write(data->regmap, APDS9960_REG_PERS,
963 APDS9960_DEFAULT_PERS);
968 * Wait for 4 event outside gesture threshold to prevent interrupt
971 ret = regmap_update_bits(data->regmap, APDS9960_REG_GCONF_1,
972 APDS9960_REG_GCONF_1_GFIFO_THRES_MASK,
973 BIT(0) << APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT);
977 /* Default ENTER and EXIT thresholds for the GESTURE engine. */
978 ret = regmap_write(data->regmap, APDS9960_REG_GPENTH,
979 APDS9960_DEFAULT_GPENTH);
983 ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
984 APDS9960_DEFAULT_GEXTH);
988 return apds9960_set_powermode(data, 1);
991 static int apds9960_probe(struct i2c_client *client)
993 struct apds9960_data *data;
994 struct iio_dev *indio_dev;
997 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1001 indio_dev->info = &apds9960_info;
1002 indio_dev->name = APDS9960_DRV_NAME;
1003 indio_dev->channels = apds9960_channels;
1004 indio_dev->num_channels = ARRAY_SIZE(apds9960_channels);
1005 indio_dev->available_scan_masks = apds9960_scan_masks;
1006 indio_dev->modes = INDIO_DIRECT_MODE;
1008 ret = devm_iio_kfifo_buffer_setup(&client->dev, indio_dev,
1009 &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 void 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);
1085 static int apds9960_runtime_suspend(struct device *dev)
1087 struct apds9960_data *data =
1088 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1090 return apds9960_set_powermode(data, 0);
1093 static int apds9960_runtime_resume(struct device *dev)
1095 struct apds9960_data *data =
1096 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1098 return apds9960_set_powermode(data, 1);
1102 static const struct dev_pm_ops apds9960_pm_ops = {
1103 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1104 pm_runtime_force_resume)
1105 SET_RUNTIME_PM_OPS(apds9960_runtime_suspend,
1106 apds9960_runtime_resume, NULL)
1109 static const struct i2c_device_id apds9960_id[] = {
1113 MODULE_DEVICE_TABLE(i2c, apds9960_id);
1115 static const struct acpi_device_id apds9960_acpi_match[] = {
1119 MODULE_DEVICE_TABLE(acpi, apds9960_acpi_match);
1121 static const struct of_device_id apds9960_of_match[] = {
1122 { .compatible = "avago,apds9960" },
1125 MODULE_DEVICE_TABLE(of, apds9960_of_match);
1127 static struct i2c_driver apds9960_driver = {
1129 .name = APDS9960_DRV_NAME,
1130 .of_match_table = apds9960_of_match,
1131 .pm = &apds9960_pm_ops,
1132 .acpi_match_table = apds9960_acpi_match,
1134 .probe = apds9960_probe,
1135 .remove = apds9960_remove,
1136 .id_table = apds9960_id,
1138 module_i2c_driver(apds9960_driver);
1140 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
1141 MODULE_DESCRIPTION("APDS9960 Gesture/RGB/ALS/Proximity sensor");
1142 MODULE_LICENSE("GPL");