dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / iio / light / apds9960.c
blobb09b8b60bd83b6afe443086c373e841c23cae948
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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
9 */
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;
124 struct mutex lock;
126 /* regmap fields */
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;
136 /* state */
137 int als_int;
138 int pxs_int;
139 int gesture_mode_running;
141 /* gain values */
142 int als_gain;
143 int pxs_gain;
145 /* integration time value in us */
146 int als_adc_int_us;
148 /* gesture buffer */
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,
159 APDS9960_REG_PDATA),
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,
207 .reg_bits = 8,
208 .val_bits = 8,
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, \
256 .scan_index = _si, \
257 .indexed = 1, \
258 .scan_type = { \
259 .sign = 'u', \
260 .realbits = 8, \
261 .storagebits = 8, \
262 }, \
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), \
272 .modified = 1, \
273 .scan_index = -1, \
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),
284 .channel = 0,
285 .indexed = 0,
286 .scan_index = -1,
288 .event_spec = apds9960_pxs_event_spec,
289 .num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
291 /* Gesture Sensor */
292 APDS9960_GESTURE_CHANNEL(UP, 0),
293 APDS9960_GESTURE_CHANNEL(DOWN, 1),
294 APDS9960_GESTURE_CHANNEL(LEFT, 2),
295 APDS9960_GESTURE_CHANNEL(RIGHT, 3),
296 /* ALS */
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),
304 .modified = 1,
305 .scan_index = -1,
307 .event_spec = apds9960_als_event_spec,
308 .num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
310 /* RGB Sensor */
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] = {
318 { 28000, 246},
319 {100000, 219},
320 {200000, 182},
321 {700000, 0}
324 /* gain mapping */
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,
336 NULL,
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)
363 int ret = -EINVAL;
364 int idx;
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]);
371 if (!ret)
372 data->als_adc_int_us = val2;
373 mutex_unlock(&data->lock);
374 break;
378 return ret;
381 static int apds9960_set_pxs_gain(struct apds9960_data *data, int val)
383 int ret = -EINVAL;
384 int idx;
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);
394 if (ret) {
395 mutex_unlock(&data->lock);
396 break;
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);
403 if (!ret)
404 data->pxs_gain = idx;
405 mutex_unlock(&data->lock);
406 break;
410 return ret;
413 static int apds9960_set_als_gain(struct apds9960_data *data, int val)
415 int ret = -EINVAL;
416 int idx;
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);
424 if (!ret)
425 data->als_gain = idx;
426 mutex_unlock(&data->lock);
427 break;
431 return ret;
434 #ifdef CONFIG_PM
435 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
437 struct device *dev = &data->client->dev;
438 int ret = 0;
440 mutex_lock(&data->lock);
442 if (on) {
443 int suspended;
445 suspended = pm_runtime_suspended(dev);
446 ret = pm_runtime_get_sync(dev);
448 /* Allow one integration cycle before allowing a reading */
449 if (suspended)
450 usleep_range(data->als_adc_int_us,
451 APDS9960_MAX_INT_TIME_IN_US);
452 } else {
453 pm_runtime_mark_last_busy(dev);
454 ret = pm_runtime_put_autosuspend(dev);
457 mutex_unlock(&data->lock);
459 return ret;
461 #else
462 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
464 return 0;
466 #endif
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);
473 __le16 buf;
474 int ret = -EINVAL;
476 if (data->gesture_mode_running)
477 return -EBUSY;
479 switch (mask) {
480 case IIO_CHAN_INFO_RAW:
481 apds9960_set_power_state(data, true);
482 switch (chan->type) {
483 case IIO_PROXIMITY:
484 ret = regmap_read(data->regmap, chan->address, val);
485 if (!ret)
486 ret = IIO_VAL_INT;
487 break;
488 case IIO_INTENSITY:
489 ret = regmap_bulk_read(data->regmap, chan->address,
490 &buf, 2);
491 if (!ret) {
492 ret = IIO_VAL_INT;
493 *val = le16_to_cpu(buf);
495 break;
496 default:
497 ret = -EINVAL;
499 apds9960_set_power_state(data, false);
500 break;
501 case IIO_CHAN_INFO_INT_TIME:
502 /* RGB + ALS sensors only have integration time */
503 mutex_lock(&data->lock);
504 switch (chan->type) {
505 case IIO_INTENSITY:
506 *val = 0;
507 *val2 = data->als_adc_int_us;
508 ret = IIO_VAL_INT_PLUS_MICRO;
509 break;
510 default:
511 ret = -EINVAL;
513 mutex_unlock(&data->lock);
514 break;
515 case IIO_CHAN_INFO_SCALE:
516 mutex_lock(&data->lock);
517 switch (chan->type) {
518 case IIO_PROXIMITY:
519 *val = apds9960_pxs_gain_map[data->pxs_gain];
520 ret = IIO_VAL_INT;
521 break;
522 case IIO_INTENSITY:
523 *val = apds9960_als_gain_map[data->als_gain];
524 ret = IIO_VAL_INT;
525 break;
526 default:
527 ret = -EINVAL;
529 mutex_unlock(&data->lock);
530 break;
533 return ret;
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);
542 switch (mask) {
543 case IIO_CHAN_INFO_INT_TIME:
544 /* RGB + ALS sensors only have int time */
545 switch (chan->type) {
546 case IIO_INTENSITY:
547 if (val != 0)
548 return -EINVAL;
549 return apds9960_set_it_time(data, val2);
550 default:
551 return -EINVAL;
553 case IIO_CHAN_INFO_SCALE:
554 if (val2 != 0)
555 return -EINVAL;
556 switch (chan->type) {
557 case IIO_PROXIMITY:
558 return apds9960_set_pxs_gain(data, val);
559 case IIO_INTENSITY:
560 return apds9960_set_als_gain(data, val);
561 default:
562 return -EINVAL;
564 default:
565 return -EINVAL;
568 return 0;
571 static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
572 enum iio_event_direction dir,
573 u8 *reg)
575 switch (dir) {
576 case IIO_EV_DIR_RISING:
577 switch (chan->type) {
578 case IIO_PROXIMITY:
579 *reg = APDS9960_REG_PIHT;
580 break;
581 case IIO_INTENSITY:
582 *reg = APDS9960_REG_AIHTL;
583 break;
584 default:
585 return -EINVAL;
587 break;
588 case IIO_EV_DIR_FALLING:
589 switch (chan->type) {
590 case IIO_PROXIMITY:
591 *reg = APDS9960_REG_PILT;
592 break;
593 case IIO_INTENSITY:
594 *reg = APDS9960_REG_AILTL;
595 break;
596 default:
597 return -EINVAL;
599 break;
600 default:
601 return -EINVAL;
604 return 0;
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,
612 int *val, int *val2)
614 u8 reg;
615 __le16 buf;
616 int ret = 0;
617 struct apds9960_data *data = iio_priv(indio_dev);
619 if (info != IIO_EV_INFO_VALUE)
620 return -EINVAL;
622 ret = apds9960_get_thres_reg(chan, dir, &reg);
623 if (ret < 0)
624 return ret;
626 if (chan->type == IIO_PROXIMITY) {
627 ret = regmap_read(data->regmap, reg, val);
628 if (ret < 0)
629 return ret;
630 } else if (chan->type == IIO_INTENSITY) {
631 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
632 if (ret < 0)
633 return ret;
634 *val = le16_to_cpu(buf);
635 } else
636 return -EINVAL;
638 *val2 = 0;
640 return IIO_VAL_INT;
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,
648 int val, int val2)
650 u8 reg;
651 __le16 buf;
652 int ret = 0;
653 struct apds9960_data *data = iio_priv(indio_dev);
655 if (info != IIO_EV_INFO_VALUE)
656 return -EINVAL;
658 ret = apds9960_get_thres_reg(chan, dir, &reg);
659 if (ret < 0)
660 return ret;
662 if (chan->type == IIO_PROXIMITY) {
663 if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL)
664 return -EINVAL;
665 ret = regmap_write(data->regmap, reg, val);
666 if (ret < 0)
667 return ret;
668 } else if (chan->type == IIO_INTENSITY) {
669 if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL)
670 return -EINVAL;
671 buf = cpu_to_le16(val);
672 ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
673 if (ret < 0)
674 return ret;
675 } else
676 return -EINVAL;
678 return 0;
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) {
689 case IIO_PROXIMITY:
690 return data->pxs_int;
691 case IIO_INTENSITY:
692 return data->als_int;
693 default:
694 return -EINVAL;
697 return 0;
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,
704 int state)
706 struct apds9960_data *data = iio_priv(indio_dev);
707 int ret;
709 state = !!state;
711 switch (chan->type) {
712 case IIO_PROXIMITY:
713 if (data->pxs_int == state)
714 return -EINVAL;
716 ret = regmap_field_write(data->reg_int_pxs, state);
717 if (ret)
718 return ret;
719 data->pxs_int = state;
720 apds9960_set_power_state(data, state);
721 break;
722 case IIO_INTENSITY:
723 if (data->als_int == state)
724 return -EINVAL;
726 ret = regmap_field_write(data->reg_int_als, state);
727 if (ret)
728 return ret;
729 data->als_int = state;
730 apds9960_set_power_state(data, state);
731 break;
732 default:
733 return -EINVAL;
736 return 0;
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)
752 int cnt;
753 int ret;
755 ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt);
756 if (ret)
757 return ret;
759 return cnt;
762 static void apds9960_read_gesture_fifo(struct apds9960_data *data)
764 int ret, cnt = 0;
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,
771 &data->buffer, 4);
773 if (ret)
774 goto err_read;
776 iio_push_to_buffers(data->indio_dev, data->buffer);
777 cnt--;
780 err_read:
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);
789 int ret, status;
791 ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status);
792 if (ret < 0) {
793 dev_err(&data->client->dev, "irq status reg read failed\n");
794 return IRQ_HANDLED;
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,
800 IIO_EV_TYPE_THRESH,
801 IIO_EV_DIR_EITHER),
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,
809 IIO_EV_TYPE_THRESH,
810 IIO_EV_DIR_EITHER),
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);
818 return IRQ_HANDLED;
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);
829 int ret;
831 ret = regmap_field_write(data->reg_int_ges, 1);
832 if (ret)
833 return ret;
835 ret = regmap_field_write(data->reg_enable_ges, 1);
836 if (ret)
837 return ret;
839 pm_runtime_get_sync(&data->client->dev);
841 return 0;
844 static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
846 struct apds9960_data *data = iio_priv(indio_dev);
847 int ret;
849 ret = regmap_field_write(data->reg_enable_ges, 0);
850 if (ret)
851 return ret;
853 ret = regmap_field_write(data->reg_int_ges, 0);
854 if (ret)
855 return ret;
857 pm_runtime_put_autosuspend(&data->client->dev);
859 return 0;
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);
914 return 0;
917 static int apds9960_chip_init(struct apds9960_data *data)
919 int ret;
921 /* Default IT for ALS of 28 ms */
922 ret = apds9960_set_it_time(data, 28000);
923 if (ret)
924 return ret;
926 /* Ensure gesture interrupt is OFF */
927 ret = regmap_field_write(data->reg_int_ges, 0);
928 if (ret)
929 return ret;
931 /* Disable gesture sensor, since polling is useless from user-space */
932 ret = regmap_field_write(data->reg_enable_ges, 0);
933 if (ret)
934 return ret;
936 /* Ensure proximity interrupt is OFF */
937 ret = regmap_field_write(data->reg_int_pxs, 0);
938 if (ret)
939 return ret;
941 /* Enable proximity sensor for polling */
942 ret = regmap_field_write(data->reg_enable_pxs, 1);
943 if (ret)
944 return ret;
946 /* Ensure ALS interrupt is OFF */
947 ret = regmap_field_write(data->reg_int_als, 0);
948 if (ret)
949 return ret;
951 /* Enable ALS sensor for polling */
952 ret = regmap_field_write(data->reg_enable_als, 1);
953 if (ret)
954 return ret;
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);
961 if (ret)
962 return ret;
965 * Wait for 4 event outside gesture threshold to prevent interrupt
966 * flooding.
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);
971 if (ret)
972 return ret;
974 /* Default ENTER and EXIT thresholds for the GESTURE engine. */
975 ret = regmap_write(data->regmap, APDS9960_REG_GPENTH,
976 APDS9960_DEFAULT_GPENTH);
977 if (ret)
978 return ret;
980 ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
981 APDS9960_DEFAULT_GEXTH);
982 if (ret)
983 return ret;
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;
994 int ret;
996 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
997 if (!indio_dev)
998 return -ENOMEM;
1000 buffer = devm_iio_kfifo_allocate(&client->dev);
1001 if (!buffer)
1002 return -ENOMEM;
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);
1029 if (ret)
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);
1039 if (ret)
1040 goto error_power_down;
1042 ret = apds9960_chip_init(data);
1043 if (ret)
1044 goto error_power_down;
1046 if (client->irq <= 0) {
1047 dev_err(&client->dev, "no valid irq defined\n");
1048 ret = -EINVAL;
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,
1054 "apds9960_event",
1055 indio_dev);
1056 if (ret) {
1057 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
1058 goto error_power_down;
1061 ret = iio_device_register(indio_dev);
1062 if (ret)
1063 goto error_power_down;
1065 apds9960_set_power_state(data, false);
1067 return 0;
1069 error_power_down:
1070 apds9960_set_power_state(data, false);
1072 return ret;
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);
1085 return 0;
1088 #ifdef CONFIG_PM
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);
1104 #endif
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[] = {
1114 { "apds9960", 0 },
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 = {
1126 .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");