gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / iio / light / apds9960.c
blob52f86bc777ddf65435de3dbf1edd8630a6db651e
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/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;
122 struct mutex lock;
124 /* regmap fields */
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;
134 /* state */
135 int als_int;
136 int pxs_int;
137 int gesture_mode_running;
139 /* gain values */
140 int als_gain;
141 int pxs_gain;
143 /* integration time value in us */
144 int als_adc_int_us;
146 /* gesture buffer */
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,
157 APDS9960_REG_PDATA),
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,
205 .reg_bits = 8,
206 .val_bits = 8,
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, \
254 .scan_index = _si, \
255 .indexed = 1, \
256 .scan_type = { \
257 .sign = 'u', \
258 .realbits = 8, \
259 .storagebits = 8, \
260 }, \
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), \
270 .modified = 1, \
271 .scan_index = -1, \
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),
282 .channel = 0,
283 .indexed = 0,
284 .scan_index = -1,
286 .event_spec = apds9960_pxs_event_spec,
287 .num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
289 /* Gesture Sensor */
290 APDS9960_GESTURE_CHANNEL(UP, 0),
291 APDS9960_GESTURE_CHANNEL(DOWN, 1),
292 APDS9960_GESTURE_CHANNEL(LEFT, 2),
293 APDS9960_GESTURE_CHANNEL(RIGHT, 3),
294 /* ALS */
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),
302 .modified = 1,
303 .scan_index = -1,
305 .event_spec = apds9960_als_event_spec,
306 .num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
308 /* RGB Sensor */
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] = {
316 { 28000, 246},
317 {100000, 219},
318 {200000, 182},
319 {700000, 0}
322 /* gain mapping */
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,
334 NULL,
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)
361 int ret = -EINVAL;
362 int idx;
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]);
369 if (!ret)
370 data->als_adc_int_us = val2;
371 mutex_unlock(&data->lock);
372 break;
376 return ret;
379 static int apds9960_set_pxs_gain(struct apds9960_data *data, int val)
381 int ret = -EINVAL;
382 int idx;
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);
392 if (ret) {
393 mutex_unlock(&data->lock);
394 break;
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);
401 if (!ret)
402 data->pxs_gain = idx;
403 mutex_unlock(&data->lock);
404 break;
408 return ret;
411 static int apds9960_set_als_gain(struct apds9960_data *data, int val)
413 int ret = -EINVAL;
414 int idx;
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);
422 if (!ret)
423 data->als_gain = idx;
424 mutex_unlock(&data->lock);
425 break;
429 return ret;
432 #ifdef CONFIG_PM
433 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
435 struct device *dev = &data->client->dev;
436 int ret = 0;
438 mutex_lock(&data->lock);
440 if (on) {
441 int suspended;
443 suspended = pm_runtime_suspended(dev);
444 ret = pm_runtime_get_sync(dev);
446 /* Allow one integration cycle before allowing a reading */
447 if (suspended)
448 usleep_range(data->als_adc_int_us,
449 APDS9960_MAX_INT_TIME_IN_US);
450 } else {
451 pm_runtime_mark_last_busy(dev);
452 ret = pm_runtime_put_autosuspend(dev);
455 mutex_unlock(&data->lock);
457 return ret;
459 #else
460 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
462 return 0;
464 #endif
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);
471 __le16 buf;
472 int ret = -EINVAL;
474 if (data->gesture_mode_running)
475 return -EBUSY;
477 switch (mask) {
478 case IIO_CHAN_INFO_RAW:
479 apds9960_set_power_state(data, true);
480 switch (chan->type) {
481 case IIO_PROXIMITY:
482 ret = regmap_read(data->regmap, chan->address, val);
483 if (!ret)
484 ret = IIO_VAL_INT;
485 break;
486 case IIO_INTENSITY:
487 ret = regmap_bulk_read(data->regmap, chan->address,
488 &buf, 2);
489 if (!ret) {
490 ret = IIO_VAL_INT;
491 *val = le16_to_cpu(buf);
493 break;
494 default:
495 ret = -EINVAL;
497 apds9960_set_power_state(data, false);
498 break;
499 case IIO_CHAN_INFO_INT_TIME:
500 /* RGB + ALS sensors only have integration time */
501 mutex_lock(&data->lock);
502 switch (chan->type) {
503 case IIO_INTENSITY:
504 *val = 0;
505 *val2 = data->als_adc_int_us;
506 ret = IIO_VAL_INT_PLUS_MICRO;
507 break;
508 default:
509 ret = -EINVAL;
511 mutex_unlock(&data->lock);
512 break;
513 case IIO_CHAN_INFO_SCALE:
514 mutex_lock(&data->lock);
515 switch (chan->type) {
516 case IIO_PROXIMITY:
517 *val = apds9960_pxs_gain_map[data->pxs_gain];
518 ret = IIO_VAL_INT;
519 break;
520 case IIO_INTENSITY:
521 *val = apds9960_als_gain_map[data->als_gain];
522 ret = IIO_VAL_INT;
523 break;
524 default:
525 ret = -EINVAL;
527 mutex_unlock(&data->lock);
528 break;
531 return ret;
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);
540 switch (mask) {
541 case IIO_CHAN_INFO_INT_TIME:
542 /* RGB + ALS sensors only have int time */
543 switch (chan->type) {
544 case IIO_INTENSITY:
545 if (val != 0)
546 return -EINVAL;
547 return apds9960_set_it_time(data, val2);
548 default:
549 return -EINVAL;
551 case IIO_CHAN_INFO_SCALE:
552 if (val2 != 0)
553 return -EINVAL;
554 switch (chan->type) {
555 case IIO_PROXIMITY:
556 return apds9960_set_pxs_gain(data, val);
557 case IIO_INTENSITY:
558 return apds9960_set_als_gain(data, val);
559 default:
560 return -EINVAL;
562 default:
563 return -EINVAL;
566 return 0;
569 static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
570 enum iio_event_direction dir,
571 u8 *reg)
573 switch (dir) {
574 case IIO_EV_DIR_RISING:
575 switch (chan->type) {
576 case IIO_PROXIMITY:
577 *reg = APDS9960_REG_PIHT;
578 break;
579 case IIO_INTENSITY:
580 *reg = APDS9960_REG_AIHTL;
581 break;
582 default:
583 return -EINVAL;
585 break;
586 case IIO_EV_DIR_FALLING:
587 switch (chan->type) {
588 case IIO_PROXIMITY:
589 *reg = APDS9960_REG_PILT;
590 break;
591 case IIO_INTENSITY:
592 *reg = APDS9960_REG_AILTL;
593 break;
594 default:
595 return -EINVAL;
597 break;
598 default:
599 return -EINVAL;
602 return 0;
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,
610 int *val, int *val2)
612 u8 reg;
613 __le16 buf;
614 int ret = 0;
615 struct apds9960_data *data = iio_priv(indio_dev);
617 if (info != IIO_EV_INFO_VALUE)
618 return -EINVAL;
620 ret = apds9960_get_thres_reg(chan, dir, &reg);
621 if (ret < 0)
622 return ret;
624 if (chan->type == IIO_PROXIMITY) {
625 ret = regmap_read(data->regmap, reg, val);
626 if (ret < 0)
627 return ret;
628 } else if (chan->type == IIO_INTENSITY) {
629 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
630 if (ret < 0)
631 return ret;
632 *val = le16_to_cpu(buf);
633 } else
634 return -EINVAL;
636 *val2 = 0;
638 return IIO_VAL_INT;
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,
646 int val, int val2)
648 u8 reg;
649 __le16 buf;
650 int ret = 0;
651 struct apds9960_data *data = iio_priv(indio_dev);
653 if (info != IIO_EV_INFO_VALUE)
654 return -EINVAL;
656 ret = apds9960_get_thres_reg(chan, dir, &reg);
657 if (ret < 0)
658 return ret;
660 if (chan->type == IIO_PROXIMITY) {
661 if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL)
662 return -EINVAL;
663 ret = regmap_write(data->regmap, reg, val);
664 if (ret < 0)
665 return ret;
666 } else if (chan->type == IIO_INTENSITY) {
667 if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL)
668 return -EINVAL;
669 buf = cpu_to_le16(val);
670 ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
671 if (ret < 0)
672 return ret;
673 } else
674 return -EINVAL;
676 return 0;
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) {
687 case IIO_PROXIMITY:
688 return data->pxs_int;
689 case IIO_INTENSITY:
690 return data->als_int;
691 default:
692 return -EINVAL;
695 return 0;
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,
702 int state)
704 struct apds9960_data *data = iio_priv(indio_dev);
705 int ret;
707 state = !!state;
709 switch (chan->type) {
710 case IIO_PROXIMITY:
711 if (data->pxs_int == state)
712 return -EINVAL;
714 ret = regmap_field_write(data->reg_int_pxs, state);
715 if (ret)
716 return ret;
717 data->pxs_int = state;
718 apds9960_set_power_state(data, state);
719 break;
720 case IIO_INTENSITY:
721 if (data->als_int == state)
722 return -EINVAL;
724 ret = regmap_field_write(data->reg_int_als, state);
725 if (ret)
726 return ret;
727 data->als_int = state;
728 apds9960_set_power_state(data, state);
729 break;
730 default:
731 return -EINVAL;
734 return 0;
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)
750 int cnt;
751 int ret;
753 ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt);
754 if (ret)
755 return ret;
757 return cnt;
760 static void apds9960_read_gesture_fifo(struct apds9960_data *data)
762 int ret, cnt = 0;
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,
769 &data->buffer, 4);
771 if (ret)
772 goto err_read;
774 iio_push_to_buffers(data->indio_dev, data->buffer);
775 cnt--;
778 err_read:
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);
787 int ret, status;
789 ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status);
790 if (ret < 0) {
791 dev_err(&data->client->dev, "irq status reg read failed\n");
792 return IRQ_HANDLED;
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,
798 IIO_EV_TYPE_THRESH,
799 IIO_EV_DIR_EITHER),
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,
807 IIO_EV_TYPE_THRESH,
808 IIO_EV_DIR_EITHER),
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);
816 return IRQ_HANDLED;
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);
827 int ret;
829 ret = regmap_field_write(data->reg_int_ges, 1);
830 if (ret)
831 return ret;
833 ret = regmap_field_write(data->reg_enable_ges, 1);
834 if (ret)
835 return ret;
837 pm_runtime_get_sync(&data->client->dev);
839 return 0;
842 static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
844 struct apds9960_data *data = iio_priv(indio_dev);
845 int ret;
847 ret = regmap_field_write(data->reg_enable_ges, 0);
848 if (ret)
849 return ret;
851 ret = regmap_field_write(data->reg_int_ges, 0);
852 if (ret)
853 return ret;
855 pm_runtime_put_autosuspend(&data->client->dev);
857 return 0;
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);
912 return 0;
915 static int apds9960_chip_init(struct apds9960_data *data)
917 int ret;
919 /* Default IT for ALS of 28 ms */
920 ret = apds9960_set_it_time(data, 28000);
921 if (ret)
922 return ret;
924 /* Ensure gesture interrupt is OFF */
925 ret = regmap_field_write(data->reg_int_ges, 0);
926 if (ret)
927 return ret;
929 /* Disable gesture sensor, since polling is useless from user-space */
930 ret = regmap_field_write(data->reg_enable_ges, 0);
931 if (ret)
932 return ret;
934 /* Ensure proximity interrupt is OFF */
935 ret = regmap_field_write(data->reg_int_pxs, 0);
936 if (ret)
937 return ret;
939 /* Enable proximity sensor for polling */
940 ret = regmap_field_write(data->reg_enable_pxs, 1);
941 if (ret)
942 return ret;
944 /* Ensure ALS interrupt is OFF */
945 ret = regmap_field_write(data->reg_int_als, 0);
946 if (ret)
947 return ret;
949 /* Enable ALS sensor for polling */
950 ret = regmap_field_write(data->reg_enable_als, 1);
951 if (ret)
952 return ret;
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);
959 if (ret)
960 return ret;
963 * Wait for 4 event outside gesture threshold to prevent interrupt
964 * flooding.
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);
969 if (ret)
970 return ret;
972 /* Default ENTER and EXIT thresholds for the GESTURE engine. */
973 ret = regmap_write(data->regmap, APDS9960_REG_GPENTH,
974 APDS9960_DEFAULT_GPENTH);
975 if (ret)
976 return ret;
978 ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
979 APDS9960_DEFAULT_GEXTH);
980 if (ret)
981 return ret;
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;
992 int ret;
994 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
995 if (!indio_dev)
996 return -ENOMEM;
998 buffer = devm_iio_kfifo_allocate(&client->dev);
999 if (!buffer)
1000 return -ENOMEM;
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);
1027 if (ret)
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);
1037 if (ret)
1038 goto error_power_down;
1040 ret = apds9960_chip_init(data);
1041 if (ret)
1042 goto error_power_down;
1044 if (client->irq <= 0) {
1045 dev_err(&client->dev, "no valid irq defined\n");
1046 ret = -EINVAL;
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,
1052 "apds9960_event",
1053 indio_dev);
1054 if (ret) {
1055 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
1056 goto error_power_down;
1059 ret = iio_device_register(indio_dev);
1060 if (ret)
1061 goto error_power_down;
1063 apds9960_set_power_state(data, false);
1065 return 0;
1067 error_power_down:
1068 apds9960_set_power_state(data, false);
1070 return ret;
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);
1083 return 0;
1086 #ifdef CONFIG_PM
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);
1102 #endif
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[] = {
1112 { "apds9960", 0 },
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 = {
1124 .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");