xref: /openbmc/linux/drivers/iio/light/apds9960.c (revision dc6a81c3)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * apds9960.c - Support for Avago APDS9960 gesture/RGB/ALS/proximity sensor
4  *
5  * Copyright (C) 2015, 2018
6  * Author: Matt Ranostay <matt.ranostay@konsulko.com>
7  *
8  * TODO: gesture + proximity calib offsets
9  */
10 
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>
26 
27 #define APDS9960_REGMAP_NAME	"apds9960_regmap"
28 #define APDS9960_DRV_NAME	"apds9960"
29 
30 #define APDS9960_REG_RAM_START	0x00
31 #define APDS9960_REG_RAM_END	0x7f
32 
33 #define APDS9960_REG_ENABLE	0x80
34 #define APDS9960_REG_ATIME	0x81
35 #define APDS9960_REG_WTIME	0x83
36 
37 #define APDS9960_REG_AILTL	0x84
38 #define APDS9960_REG_AILTH	0x85
39 #define APDS9960_REG_AIHTL	0x86
40 #define APDS9960_REG_AIHTH	0x87
41 
42 #define APDS9960_REG_PILT	0x89
43 #define APDS9960_REG_PIHT	0x8b
44 #define APDS9960_REG_PERS	0x8c
45 
46 #define APDS9960_REG_CONFIG_1	0x8d
47 #define APDS9960_REG_PPULSE	0x8e
48 
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
54 
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
58 
59 #define APDS9960_REG_ID		0x92
60 
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)
65 
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
70 
71 #define APDS9960_REG_GPENTH	0xa0
72 #define APDS9960_REG_GEXTH	0xa1
73 
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
77 
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
85 
86 #define APDS9960_REG_GCONF_4	0xab
87 #define APDS9960_REG_GFLVL	0xae
88 #define APDS9960_REG_GSTATUS	0xaf
89 
90 #define APDS9960_REG_IFORCE	0xe4
91 #define APDS9960_REG_PICLEAR	0xe5
92 #define APDS9960_REG_CICLEAR	0xe6
93 #define APDS9960_REG_AICLEAR	0xe7
94 
95 #define APDS9960_DEFAULT_PERS	0x33
96 #define APDS9960_DEFAULT_GPENTH	0x50
97 #define APDS9960_DEFAULT_GEXTH	0x40
98 
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
102 
103 enum apds9960_als_channel_idx {
104 	IDX_ALS_CLEAR, IDX_ALS_RED, IDX_ALS_GREEN, IDX_ALS_BLUE,
105 };
106 
107 #define APDS9960_REG_ALS_BASE	0x94
108 #define APDS9960_REG_ALS_CHANNEL(_colour) \
109 	(APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2))
110 
111 enum apds9960_gesture_channel_idx {
112 	IDX_DIR_UP, IDX_DIR_DOWN, IDX_DIR_LEFT, IDX_DIR_RIGHT,
113 };
114 
115 #define APDS9960_REG_GFIFO_BASE	0xfc
116 #define APDS9960_REG_GFIFO_DIR(_dir) \
117 	(APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir)
118 
119 struct apds9960_data {
120 	struct i2c_client *client;
121 	struct iio_dev *indio_dev;
122 	struct mutex lock;
123 
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;
129 
130 	struct regmap_field *reg_enable_als;
131 	struct regmap_field *reg_enable_ges;
132 	struct regmap_field *reg_enable_pxs;
133 
134 	/* state */
135 	int als_int;
136 	int pxs_int;
137 	int gesture_mode_running;
138 
139 	/* gain values */
140 	int als_gain;
141 	int pxs_gain;
142 
143 	/* integration time value in us */
144 	int als_adc_int_us;
145 
146 	/* gesture buffer */
147 	u8 buffer[4]; /* 4 8-bit channels */
148 };
149 
150 static const struct reg_default apds9960_reg_defaults[] = {
151 	/* Default ALS integration time = 2.48ms */
152 	{ APDS9960_REG_ATIME, 0xff },
153 };
154 
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),
164 };
165 
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),
169 };
170 
171 static const struct regmap_range apds9960_precious_ranges[] = {
172 	regmap_reg_range(APDS9960_REG_RAM_START, APDS9960_REG_RAM_END),
173 };
174 
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),
178 };
179 
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)),
185 };
186 
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),
190 };
191 
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),
196 };
197 
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),
201 };
202 
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,
209 
210 	.volatile_table = &apds9960_volatile_table,
211 	.precious_table = &apds9960_precious_table,
212 	.rd_table = &apds9960_readable_table,
213 	.wr_table = &apds9960_writeable_table,
214 
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,
219 };
220 
221 static const struct iio_event_spec apds9960_pxs_event_spec[] = {
222 	{
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),
227 	},
228 	{
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),
233 	},
234 };
235 
236 static const struct iio_event_spec apds9960_als_event_spec[] = {
237 	{
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),
242 	},
243 	{
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),
248 	},
249 };
250 
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 	}, \
261 }
262 
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, \
272 }
273 
274 static const unsigned long apds9960_scan_masks[] = {0xf, 0};
275 
276 static const struct iio_chan_spec apds9960_channels[] = {
277 	{
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,
285 
286 		.event_spec = apds9960_pxs_event_spec,
287 		.num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
288 	},
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 */
295 	{
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,
304 
305 		.event_spec = apds9960_als_event_spec,
306 		.num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
307 	},
308 	/* RGB Sensor */
309 	APDS9960_INTENSITY_CHANNEL(RED),
310 	APDS9960_INTENSITY_CHANNEL(GREEN),
311 	APDS9960_INTENSITY_CHANNEL(BLUE),
312 };
313 
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}
320 };
321 
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};
325 
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");
329 
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,
335 };
336 
337 static const struct attribute_group apds9960_attribute_group = {
338 	.attrs = apds9960_attributes,
339 };
340 
341 static const struct reg_field apds9960_reg_field_int_als =
342 				REG_FIELD(APDS9960_REG_ENABLE, 4, 4);
343 
344 static const struct reg_field apds9960_reg_field_int_ges =
345 				REG_FIELD(APDS9960_REG_GCONF_4, 1, 1);
346 
347 static const struct reg_field apds9960_reg_field_int_pxs =
348 				REG_FIELD(APDS9960_REG_ENABLE, 5, 5);
349 
350 static const struct reg_field apds9960_reg_field_enable_als =
351 				REG_FIELD(APDS9960_REG_ENABLE, 1, 1);
352 
353 static const struct reg_field apds9960_reg_field_enable_ges =
354 				REG_FIELD(APDS9960_REG_ENABLE, 6, 6);
355 
356 static const struct reg_field apds9960_reg_field_enable_pxs =
357 				REG_FIELD(APDS9960_REG_ENABLE, 2, 2);
358 
359 static int apds9960_set_it_time(struct apds9960_data *data, int val2)
360 {
361 	int ret = -EINVAL;
362 	int idx;
363 
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;
373 		}
374 	}
375 
376 	return ret;
377 }
378 
379 static int apds9960_set_pxs_gain(struct apds9960_data *data, int val)
380 {
381 	int ret = -EINVAL;
382 	int idx;
383 
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;
395 			}
396 
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;
405 		}
406 	}
407 
408 	return ret;
409 }
410 
411 static int apds9960_set_als_gain(struct apds9960_data *data, int val)
412 {
413 	int ret = -EINVAL;
414 	int idx;
415 
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;
426 		}
427 	}
428 
429 	return ret;
430 }
431 
432 #ifdef CONFIG_PM
433 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
434 {
435 	struct device *dev = &data->client->dev;
436 	int ret = 0;
437 
438 	mutex_lock(&data->lock);
439 
440 	if (on) {
441 		int suspended;
442 
443 		suspended = pm_runtime_suspended(dev);
444 		ret = pm_runtime_get_sync(dev);
445 
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);
453 	}
454 
455 	mutex_unlock(&data->lock);
456 
457 	return ret;
458 }
459 #else
460 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
461 {
462 	return 0;
463 }
464 #endif
465 
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)
469 {
470 	struct apds9960_data *data = iio_priv(indio_dev);
471 	__le16 buf;
472 	int ret = -EINVAL;
473 
474 	if (data->gesture_mode_running)
475 		return -EBUSY;
476 
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);
492 			}
493 			break;
494 		default:
495 			ret = -EINVAL;
496 		}
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;
510 		}
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;
526 		}
527 		mutex_unlock(&data->lock);
528 		break;
529 	}
530 
531 	return ret;
532 };
533 
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)
537 {
538 	struct apds9960_data *data = iio_priv(indio_dev);
539 
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;
550 		}
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;
561 		}
562 	default:
563 		return -EINVAL;
564 	};
565 
566 	return 0;
567 }
568 
569 static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
570 					 enum iio_event_direction dir,
571 					 u8 *reg)
572 {
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;
584 		}
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;
596 		}
597 		break;
598 	default:
599 		return -EINVAL;
600 	}
601 
602 	return 0;
603 }
604 
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)
611 {
612 	u8 reg;
613 	__le16 buf;
614 	int ret = 0;
615 	struct apds9960_data *data = iio_priv(indio_dev);
616 
617 	if (info != IIO_EV_INFO_VALUE)
618 		return -EINVAL;
619 
620 	ret = apds9960_get_thres_reg(chan, dir, &reg);
621 	if (ret < 0)
622 		return ret;
623 
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;
635 
636 	*val2 = 0;
637 
638 	return IIO_VAL_INT;
639 }
640 
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)
647 {
648 	u8 reg;
649 	__le16 buf;
650 	int ret = 0;
651 	struct apds9960_data *data = iio_priv(indio_dev);
652 
653 	if (info != IIO_EV_INFO_VALUE)
654 		return -EINVAL;
655 
656 	ret = apds9960_get_thres_reg(chan, dir, &reg);
657 	if (ret < 0)
658 		return ret;
659 
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;
675 
676 	return 0;
677 }
678 
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)
683 {
684 	struct apds9960_data *data = iio_priv(indio_dev);
685 
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;
693 	}
694 
695 	return 0;
696 }
697 
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)
703 {
704 	struct apds9960_data *data = iio_priv(indio_dev);
705 	int ret;
706 
707 	state = !!state;
708 
709 	switch (chan->type) {
710 	case IIO_PROXIMITY:
711 		if (data->pxs_int == state)
712 			return -EINVAL;
713 
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;
723 
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;
732 	}
733 
734 	return 0;
735 }
736 
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,
745 
746 };
747 
748 static inline int apds9660_fifo_is_empty(struct apds9960_data *data)
749 {
750 	int cnt;
751 	int ret;
752 
753 	ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt);
754 	if (ret)
755 		return ret;
756 
757 	return cnt;
758 }
759 
760 static void apds9960_read_gesture_fifo(struct apds9960_data *data)
761 {
762 	int ret, cnt = 0;
763 
764 	mutex_lock(&data->lock);
765 	data->gesture_mode_running = 1;
766 
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);
770 
771 		if (ret)
772 			goto err_read;
773 
774 		iio_push_to_buffers(data->indio_dev, data->buffer);
775 		cnt--;
776 	}
777 
778 err_read:
779 	data->gesture_mode_running = 0;
780 	mutex_unlock(&data->lock);
781 }
782 
783 static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
784 {
785 	struct iio_dev *indio_dev = private;
786 	struct apds9960_data *data = iio_priv(indio_dev);
787 	int ret, status;
788 
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;
793 	}
794 
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);
802 	}
803 
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);
811 	}
812 
813 	if (status & APDS9960_REG_STATUS_GINT)
814 		apds9960_read_gesture_fifo(data);
815 
816 	return IRQ_HANDLED;
817 }
818 
819 static int apds9960_set_powermode(struct apds9960_data *data, bool state)
820 {
821 	return regmap_update_bits(data->regmap, APDS9960_REG_ENABLE, 1, state);
822 }
823 
824 static int apds9960_buffer_postenable(struct iio_dev *indio_dev)
825 {
826 	struct apds9960_data *data = iio_priv(indio_dev);
827 	int ret;
828 
829 	ret = regmap_field_write(data->reg_int_ges, 1);
830 	if (ret)
831 		return ret;
832 
833 	ret = regmap_field_write(data->reg_enable_ges, 1);
834 	if (ret)
835 		return ret;
836 
837 	pm_runtime_get_sync(&data->client->dev);
838 
839 	return 0;
840 }
841 
842 static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
843 {
844 	struct apds9960_data *data = iio_priv(indio_dev);
845 	int ret;
846 
847 	ret = regmap_field_write(data->reg_enable_ges, 0);
848 	if (ret)
849 		return ret;
850 
851 	ret = regmap_field_write(data->reg_int_ges, 0);
852 	if (ret)
853 		return ret;
854 
855 	pm_runtime_put_autosuspend(&data->client->dev);
856 
857 	return 0;
858 }
859 
860 static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops = {
861 	.postenable = apds9960_buffer_postenable,
862 	.predisable = apds9960_buffer_predisable,
863 };
864 
865 static int apds9960_regfield_init(struct apds9960_data *data)
866 {
867 	struct device *dev = &data->client->dev;
868 	struct regmap *regmap = data->regmap;
869 
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);
875 	}
876 
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);
882 	}
883 
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);
889 	}
890 
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);
896 	}
897 
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);
903 	}
904 
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);
910 	}
911 
912 	return 0;
913 }
914 
915 static int apds9960_chip_init(struct apds9960_data *data)
916 {
917 	int ret;
918 
919 	/* Default IT for ALS of 28 ms */
920 	ret = apds9960_set_it_time(data, 28000);
921 	if (ret)
922 		return ret;
923 
924 	/* Ensure gesture interrupt is OFF */
925 	ret = regmap_field_write(data->reg_int_ges, 0);
926 	if (ret)
927 		return ret;
928 
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;
933 
934 	/* Ensure proximity interrupt is OFF */
935 	ret = regmap_field_write(data->reg_int_pxs, 0);
936 	if (ret)
937 		return ret;
938 
939 	/* Enable proximity sensor for polling */
940 	ret = regmap_field_write(data->reg_enable_pxs, 1);
941 	if (ret)
942 		return ret;
943 
944 	/* Ensure ALS interrupt is OFF */
945 	ret = regmap_field_write(data->reg_int_als, 0);
946 	if (ret)
947 		return ret;
948 
949 	/* Enable ALS sensor for polling */
950 	ret = regmap_field_write(data->reg_enable_als, 1);
951 	if (ret)
952 		return ret;
953 	/*
954 	 * When enabled trigger an interrupt after 3 readings
955 	 * outside threshold for ALS + PXS
956 	 */
957 	ret = regmap_write(data->regmap, APDS9960_REG_PERS,
958 			   APDS9960_DEFAULT_PERS);
959 	if (ret)
960 		return ret;
961 
962 	/*
963 	 * Wait for 4 event outside gesture threshold to prevent interrupt
964 	 * flooding.
965 	 */
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;
971 
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;
977 
978 	ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
979 			   APDS9960_DEFAULT_GEXTH);
980 	if (ret)
981 		return ret;
982 
983 	return apds9960_set_powermode(data, 1);
984 }
985 
986 static int apds9960_probe(struct i2c_client *client,
987 			  const struct i2c_device_id *id)
988 {
989 	struct apds9960_data *data;
990 	struct iio_buffer *buffer;
991 	struct iio_dev *indio_dev;
992 	int ret;
993 
994 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
995 	if (!indio_dev)
996 		return -ENOMEM;
997 
998 	buffer = devm_iio_kfifo_allocate(&client->dev);
999 	if (!buffer)
1000 		return -ENOMEM;
1001 
1002 	iio_device_attach_buffer(indio_dev, buffer);
1003 
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;
1012 
1013 	data = iio_priv(indio_dev);
1014 	i2c_set_clientdata(client, indio_dev);
1015 
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);
1020 	}
1021 
1022 	data->client = client;
1023 	data->indio_dev = indio_dev;
1024 	mutex_init(&data->lock);
1025 
1026 	ret = pm_runtime_set_active(&client->dev);
1027 	if (ret)
1028 		goto error_power_down;
1029 
1030 	pm_runtime_enable(&client->dev);
1031 	pm_runtime_set_autosuspend_delay(&client->dev, 5000);
1032 	pm_runtime_use_autosuspend(&client->dev);
1033 
1034 	apds9960_set_power_state(data, true);
1035 
1036 	ret = apds9960_regfield_init(data);
1037 	if (ret)
1038 		goto error_power_down;
1039 
1040 	ret = apds9960_chip_init(data);
1041 	if (ret)
1042 		goto error_power_down;
1043 
1044 	if (client->irq <= 0) {
1045 		dev_err(&client->dev, "no valid irq defined\n");
1046 		ret = -EINVAL;
1047 		goto error_power_down;
1048 	}
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;
1057 	}
1058 
1059 	ret = iio_device_register(indio_dev);
1060 	if (ret)
1061 		goto error_power_down;
1062 
1063 	apds9960_set_power_state(data, false);
1064 
1065 	return 0;
1066 
1067 error_power_down:
1068 	apds9960_set_power_state(data, false);
1069 
1070 	return ret;
1071 }
1072 
1073 static int apds9960_remove(struct i2c_client *client)
1074 {
1075 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1076 	struct apds9960_data *data = iio_priv(indio_dev);
1077 
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);
1082 
1083 	return 0;
1084 }
1085 
1086 #ifdef CONFIG_PM
1087 static int apds9960_runtime_suspend(struct device *dev)
1088 {
1089 	struct apds9960_data *data =
1090 			iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1091 
1092 	return apds9960_set_powermode(data, 0);
1093 }
1094 
1095 static int apds9960_runtime_resume(struct device *dev)
1096 {
1097 	struct apds9960_data *data =
1098 			iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1099 
1100 	return apds9960_set_powermode(data, 1);
1101 }
1102 #endif
1103 
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)
1109 };
1110 
1111 static const struct i2c_device_id apds9960_id[] = {
1112 	{ "apds9960", 0 },
1113 	{}
1114 };
1115 MODULE_DEVICE_TABLE(i2c, apds9960_id);
1116 
1117 static const struct of_device_id apds9960_of_match[] = {
1118 	{ .compatible = "avago,apds9960" },
1119 	{ }
1120 };
1121 MODULE_DEVICE_TABLE(of, apds9960_of_match);
1122 
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,
1128 	},
1129 	.probe		= apds9960_probe,
1130 	.remove		= apds9960_remove,
1131 	.id_table	= apds9960_id,
1132 };
1133 module_i2c_driver(apds9960_driver);
1134 
1135 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
1136 MODULE_DESCRIPTION("APDS9960 Gesture/RGB/ALS/Proximity sensor");
1137 MODULE_LICENSE("GPL");
1138