xref: /linux/drivers/iio/light/apds9960.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
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/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>
27 
28 #define APDS9960_REGMAP_NAME	"apds9960_regmap"
29 #define APDS9960_DRV_NAME	"apds9960"
30 
31 #define APDS9960_REG_RAM_START	0x00
32 #define APDS9960_REG_RAM_END	0x7f
33 
34 #define APDS9960_REG_ENABLE	0x80
35 #define APDS9960_REG_ATIME	0x81
36 #define APDS9960_REG_WTIME	0x83
37 
38 #define APDS9960_REG_AILTL	0x84
39 #define APDS9960_REG_AILTH	0x85
40 #define APDS9960_REG_AIHTL	0x86
41 #define APDS9960_REG_AIHTH	0x87
42 
43 #define APDS9960_REG_PILT	0x89
44 #define APDS9960_REG_PIHT	0x8b
45 #define APDS9960_REG_PERS	0x8c
46 
47 #define APDS9960_REG_CONFIG_1	0x8d
48 #define APDS9960_REG_PPULSE	0x8e
49 
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
55 
56 #define APDS9960_REG_CONFIG_2	0x90
57 #define APDS9960_REG_ID		0x92
58 
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)
63 
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
68 
69 #define APDS9960_REG_GPENTH	0xa0
70 #define APDS9960_REG_GEXTH	0xa1
71 
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
75 
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
79 
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
86 
87 #define APDS9960_REG_GCONF_4	0xab
88 #define APDS9960_REG_GFLVL	0xae
89 #define APDS9960_REG_GSTATUS	0xaf
90 
91 #define APDS9960_REG_IFORCE	0xe4
92 #define APDS9960_REG_PICLEAR	0xe5
93 #define APDS9960_REG_CICLEAR	0xe6
94 #define APDS9960_REG_AICLEAR	0xe7
95 
96 #define APDS9960_DEFAULT_PERS	0x33
97 #define APDS9960_DEFAULT_GPENTH	0x50
98 #define APDS9960_DEFAULT_GEXTH	0x40
99 
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
103 
104 enum apds9960_als_channel_idx {
105 	IDX_ALS_CLEAR, IDX_ALS_RED, IDX_ALS_GREEN, IDX_ALS_BLUE,
106 };
107 
108 #define APDS9960_REG_ALS_BASE	0x94
109 #define APDS9960_REG_ALS_CHANNEL(_colour) \
110 	(APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2))
111 
112 enum apds9960_gesture_channel_idx {
113 	IDX_DIR_UP, IDX_DIR_DOWN, IDX_DIR_LEFT, IDX_DIR_RIGHT,
114 };
115 
116 #define APDS9960_REG_GFIFO_BASE	0xfc
117 #define APDS9960_REG_GFIFO_DIR(_dir) \
118 	(APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir)
119 
120 struct apds9960_data {
121 	struct i2c_client *client;
122 	struct iio_dev *indio_dev;
123 	struct mutex lock;
124 
125 	/* regmap fields */
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;
130 
131 	struct regmap_field *reg_enable_als;
132 	struct regmap_field *reg_enable_ges;
133 	struct regmap_field *reg_enable_pxs;
134 
135 	/* state */
136 	int als_int;
137 	int pxs_int;
138 	int gesture_mode_running;
139 
140 	/* gain values */
141 	int als_gain;
142 	int pxs_gain;
143 
144 	/* integration time value in us */
145 	int als_adc_int_us;
146 
147 	/* gesture buffer */
148 	u8 buffer[4]; /* 4 8-bit channels */
149 };
150 
151 static const struct reg_default apds9960_reg_defaults[] = {
152 	/* Default ALS integration time = 2.48ms */
153 	{ APDS9960_REG_ATIME, 0xff },
154 };
155 
156 static const struct regmap_range apds9960_volatile_ranges[] = {
157 	regmap_reg_range(APDS9960_REG_STATUS,
158 				APDS9960_REG_PDATA),
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),
165 };
166 
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),
170 };
171 
172 static const struct regmap_range apds9960_precious_ranges[] = {
173 	regmap_reg_range(APDS9960_REG_RAM_START, APDS9960_REG_RAM_END),
174 };
175 
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),
179 };
180 
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)),
186 };
187 
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),
191 };
192 
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),
197 };
198 
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),
202 };
203 
204 static const struct regmap_config apds9960_regmap_config = {
205 	.name = APDS9960_REGMAP_NAME,
206 	.reg_bits = 8,
207 	.val_bits = 8,
208 	.use_single_read = true,
209 	.use_single_write = true,
210 
211 	.volatile_table = &apds9960_volatile_table,
212 	.precious_table = &apds9960_precious_table,
213 	.rd_table = &apds9960_readable_table,
214 	.wr_table = &apds9960_writeable_table,
215 
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,
220 };
221 
222 static const struct iio_event_spec apds9960_pxs_event_spec[] = {
223 	{
224 		.type = IIO_EV_TYPE_THRESH,
225 		.dir = IIO_EV_DIR_RISING,
226 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
227 	},
228 	{
229 		.type = IIO_EV_TYPE_THRESH,
230 		.dir = IIO_EV_DIR_FALLING,
231 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
232 	},
233 	{
234 		.type = IIO_EV_TYPE_THRESH,
235 		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
236 	},
237 };
238 
239 static const struct iio_event_spec apds9960_als_event_spec[] = {
240 	{
241 		.type = IIO_EV_TYPE_THRESH,
242 		.dir = IIO_EV_DIR_RISING,
243 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
244 	},
245 	{
246 		.type = IIO_EV_TYPE_THRESH,
247 		.dir = IIO_EV_DIR_FALLING,
248 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
249 	},
250 	{
251 		.type = IIO_EV_TYPE_THRESH,
252 		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
253 	},
254 };
255 
256 #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \
257 	.type = IIO_PROXIMITY, \
258 	.channel = _si + 1, \
259 	.scan_index = _si, \
260 	.indexed = 1, \
261 	.scan_type = { \
262 		.sign = 'u', \
263 		.realbits = 8, \
264 		.storagebits = 8, \
265 	}, \
266 }
267 
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), \
275 	.modified = 1, \
276 	.scan_index = -1, \
277 }
278 
279 static const unsigned long apds9960_scan_masks[] = {0xf, 0};
280 
281 static const struct iio_chan_spec apds9960_channels[] = {
282 	{
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),
287 		.channel = 0,
288 		.indexed = 0,
289 		.scan_index = -1,
290 
291 		.event_spec = apds9960_pxs_event_spec,
292 		.num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
293 	},
294 	/* Gesture Sensor */
295 	APDS9960_GESTURE_CHANNEL(UP, 0),
296 	APDS9960_GESTURE_CHANNEL(DOWN, 1),
297 	APDS9960_GESTURE_CHANNEL(LEFT, 2),
298 	APDS9960_GESTURE_CHANNEL(RIGHT, 3),
299 	/* ALS */
300 	{
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),
307 		.modified = 1,
308 		.scan_index = -1,
309 
310 		.event_spec = apds9960_als_event_spec,
311 		.num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
312 	},
313 	/* RGB Sensor */
314 	APDS9960_INTENSITY_CHANNEL(RED),
315 	APDS9960_INTENSITY_CHANNEL(GREEN),
316 	APDS9960_INTENSITY_CHANNEL(BLUE),
317 };
318 
319 /* integration time in us */
320 static const int apds9960_int_time[][2] = {
321 	{ 28000, 246},
322 	{100000, 219},
323 	{200000, 182},
324 	{700000,   0}
325 };
326 
327 /* gain mapping */
328 static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8};
329 static const int apds9960_als_gain_map[] = {1, 4, 16, 64};
330 
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");
334 
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,
339 	NULL,
340 };
341 
342 static const struct attribute_group apds9960_attribute_group = {
343 	.attrs = apds9960_attributes,
344 };
345 
346 static const struct reg_field apds9960_reg_field_int_als =
347 				REG_FIELD(APDS9960_REG_ENABLE, 4, 4);
348 
349 static const struct reg_field apds9960_reg_field_int_ges =
350 				REG_FIELD(APDS9960_REG_GCONF_4, 1, 1);
351 
352 static const struct reg_field apds9960_reg_field_int_pxs =
353 				REG_FIELD(APDS9960_REG_ENABLE, 5, 5);
354 
355 static const struct reg_field apds9960_reg_field_enable_als =
356 				REG_FIELD(APDS9960_REG_ENABLE, 1, 1);
357 
358 static const struct reg_field apds9960_reg_field_enable_ges =
359 				REG_FIELD(APDS9960_REG_ENABLE, 6, 6);
360 
361 static const struct reg_field apds9960_reg_field_enable_pxs =
362 				REG_FIELD(APDS9960_REG_ENABLE, 2, 2);
363 
364 static int apds9960_set_it_time(struct apds9960_data *data, int val2)
365 {
366 	int ret = -EINVAL;
367 	int idx;
368 
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]);
374 			if (!ret)
375 				data->als_adc_int_us = val2;
376 			mutex_unlock(&data->lock);
377 			break;
378 		}
379 	}
380 
381 	return ret;
382 }
383 
384 static int apds9960_set_pxs_gain(struct apds9960_data *data, int val)
385 {
386 	int ret = -EINVAL;
387 	int idx;
388 
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);
397 			if (ret) {
398 				mutex_unlock(&data->lock);
399 				break;
400 			}
401 
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);
406 			if (!ret)
407 				data->pxs_gain = idx;
408 			mutex_unlock(&data->lock);
409 			break;
410 		}
411 	}
412 
413 	return ret;
414 }
415 
416 static int apds9960_set_als_gain(struct apds9960_data *data, int val)
417 {
418 	int ret = -EINVAL;
419 	int idx;
420 
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);
427 			if (!ret)
428 				data->als_gain = idx;
429 			mutex_unlock(&data->lock);
430 			break;
431 		}
432 	}
433 
434 	return ret;
435 }
436 
437 #ifdef CONFIG_PM
438 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
439 {
440 	struct device *dev = &data->client->dev;
441 	int ret = 0;
442 
443 	mutex_lock(&data->lock);
444 
445 	if (on) {
446 		int suspended;
447 
448 		suspended = pm_runtime_suspended(dev);
449 		ret = pm_runtime_get_sync(dev);
450 
451 		/* Allow one integration cycle before allowing a reading */
452 		if (suspended)
453 			usleep_range(data->als_adc_int_us,
454 				     APDS9960_MAX_INT_TIME_IN_US);
455 	} else {
456 		pm_runtime_mark_last_busy(dev);
457 		ret = pm_runtime_put_autosuspend(dev);
458 	}
459 
460 	mutex_unlock(&data->lock);
461 
462 	return ret;
463 }
464 #else
465 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
466 {
467 	return 0;
468 }
469 #endif
470 
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)
474 {
475 	struct apds9960_data *data = iio_priv(indio_dev);
476 	__le16 buf;
477 	int ret = -EINVAL;
478 
479 	if (data->gesture_mode_running)
480 		return -EBUSY;
481 
482 	switch (mask) {
483 	case IIO_CHAN_INFO_RAW:
484 		apds9960_set_power_state(data, true);
485 		switch (chan->type) {
486 		case IIO_PROXIMITY:
487 			ret = regmap_read(data->regmap, chan->address, val);
488 			if (!ret)
489 				ret = IIO_VAL_INT;
490 			break;
491 		case IIO_INTENSITY:
492 			ret = regmap_bulk_read(data->regmap, chan->address,
493 					       &buf, 2);
494 			if (!ret) {
495 				ret = IIO_VAL_INT;
496 				*val = le16_to_cpu(buf);
497 			}
498 			break;
499 		default:
500 			ret = -EINVAL;
501 		}
502 		apds9960_set_power_state(data, false);
503 		break;
504 	case IIO_CHAN_INFO_INT_TIME:
505 		/* RGB + ALS sensors only have integration time */
506 		mutex_lock(&data->lock);
507 		switch (chan->type) {
508 		case IIO_INTENSITY:
509 			*val = 0;
510 			*val2 = data->als_adc_int_us;
511 			ret = IIO_VAL_INT_PLUS_MICRO;
512 			break;
513 		default:
514 			ret = -EINVAL;
515 		}
516 		mutex_unlock(&data->lock);
517 		break;
518 	case IIO_CHAN_INFO_SCALE:
519 		mutex_lock(&data->lock);
520 		switch (chan->type) {
521 		case IIO_PROXIMITY:
522 			*val = apds9960_pxs_gain_map[data->pxs_gain];
523 			ret = IIO_VAL_INT;
524 			break;
525 		case IIO_INTENSITY:
526 			*val = apds9960_als_gain_map[data->als_gain];
527 			ret = IIO_VAL_INT;
528 			break;
529 		default:
530 			ret = -EINVAL;
531 		}
532 		mutex_unlock(&data->lock);
533 		break;
534 	}
535 
536 	return ret;
537 };
538 
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)
542 {
543 	struct apds9960_data *data = iio_priv(indio_dev);
544 
545 	switch (mask) {
546 	case IIO_CHAN_INFO_INT_TIME:
547 		/* RGB + ALS sensors only have int time */
548 		switch (chan->type) {
549 		case IIO_INTENSITY:
550 			if (val != 0)
551 				return -EINVAL;
552 			return apds9960_set_it_time(data, val2);
553 		default:
554 			return -EINVAL;
555 		}
556 	case IIO_CHAN_INFO_SCALE:
557 		if (val2 != 0)
558 			return -EINVAL;
559 		switch (chan->type) {
560 		case IIO_PROXIMITY:
561 			return apds9960_set_pxs_gain(data, val);
562 		case IIO_INTENSITY:
563 			return apds9960_set_als_gain(data, val);
564 		default:
565 			return -EINVAL;
566 		}
567 	default:
568 		return -EINVAL;
569 	}
570 
571 	return 0;
572 }
573 
574 static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
575 					 enum iio_event_direction dir,
576 					 u8 *reg)
577 {
578 	switch (dir) {
579 	case IIO_EV_DIR_RISING:
580 		switch (chan->type) {
581 		case IIO_PROXIMITY:
582 			*reg = APDS9960_REG_PIHT;
583 			break;
584 		case IIO_INTENSITY:
585 			*reg = APDS9960_REG_AIHTL;
586 			break;
587 		default:
588 			return -EINVAL;
589 		}
590 		break;
591 	case IIO_EV_DIR_FALLING:
592 		switch (chan->type) {
593 		case IIO_PROXIMITY:
594 			*reg = APDS9960_REG_PILT;
595 			break;
596 		case IIO_INTENSITY:
597 			*reg = APDS9960_REG_AILTL;
598 			break;
599 		default:
600 			return -EINVAL;
601 		}
602 		break;
603 	default:
604 		return -EINVAL;
605 	}
606 
607 	return 0;
608 }
609 
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,
615 			       int *val, int *val2)
616 {
617 	u8 reg;
618 	__le16 buf;
619 	int ret = 0;
620 	struct apds9960_data *data = iio_priv(indio_dev);
621 
622 	if (info != IIO_EV_INFO_VALUE)
623 		return -EINVAL;
624 
625 	ret = apds9960_get_thres_reg(chan, dir, &reg);
626 	if (ret < 0)
627 		return ret;
628 
629 	if (chan->type == IIO_PROXIMITY) {
630 		ret = regmap_read(data->regmap, reg, val);
631 		if (ret < 0)
632 			return ret;
633 	} else if (chan->type == IIO_INTENSITY) {
634 		ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
635 		if (ret < 0)
636 			return ret;
637 		*val = le16_to_cpu(buf);
638 	} else
639 		return -EINVAL;
640 
641 	*val2 = 0;
642 
643 	return IIO_VAL_INT;
644 }
645 
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,
651 				int val, int val2)
652 {
653 	u8 reg;
654 	__le16 buf;
655 	int ret = 0;
656 	struct apds9960_data *data = iio_priv(indio_dev);
657 
658 	if (info != IIO_EV_INFO_VALUE)
659 		return -EINVAL;
660 
661 	ret = apds9960_get_thres_reg(chan, dir, &reg);
662 	if (ret < 0)
663 		return ret;
664 
665 	if (chan->type == IIO_PROXIMITY) {
666 		if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL)
667 			return -EINVAL;
668 		ret = regmap_write(data->regmap, reg, val);
669 		if (ret < 0)
670 			return ret;
671 	} else if (chan->type == IIO_INTENSITY) {
672 		if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL)
673 			return -EINVAL;
674 		buf = cpu_to_le16(val);
675 		ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
676 		if (ret < 0)
677 			return ret;
678 	} else
679 		return -EINVAL;
680 
681 	return 0;
682 }
683 
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)
688 {
689 	struct apds9960_data *data = iio_priv(indio_dev);
690 
691 	switch (chan->type) {
692 	case IIO_PROXIMITY:
693 		return data->pxs_int;
694 	case IIO_INTENSITY:
695 		return data->als_int;
696 	default:
697 		return -EINVAL;
698 	}
699 
700 	return 0;
701 }
702 
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,
707 				       int state)
708 {
709 	struct apds9960_data *data = iio_priv(indio_dev);
710 	int ret;
711 
712 	state = !!state;
713 
714 	switch (chan->type) {
715 	case IIO_PROXIMITY:
716 		if (data->pxs_int == state)
717 			return -EINVAL;
718 
719 		ret = regmap_field_write(data->reg_int_pxs, state);
720 		if (ret)
721 			return ret;
722 		data->pxs_int = state;
723 		apds9960_set_power_state(data, state);
724 		break;
725 	case IIO_INTENSITY:
726 		if (data->als_int == state)
727 			return -EINVAL;
728 
729 		ret = regmap_field_write(data->reg_int_als, state);
730 		if (ret)
731 			return ret;
732 		data->als_int = state;
733 		apds9960_set_power_state(data, state);
734 		break;
735 	default:
736 		return -EINVAL;
737 	}
738 
739 	return 0;
740 }
741 
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,
750 
751 };
752 
753 static inline int apds9660_fifo_is_empty(struct apds9960_data *data)
754 {
755 	int cnt;
756 	int ret;
757 
758 	ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt);
759 	if (ret)
760 		return ret;
761 
762 	return cnt;
763 }
764 
765 static void apds9960_read_gesture_fifo(struct apds9960_data *data)
766 {
767 	int ret, cnt = 0;
768 
769 	mutex_lock(&data->lock);
770 	data->gesture_mode_running = 1;
771 
772 	while (cnt || (cnt = apds9660_fifo_is_empty(data) > 0)) {
773 		ret = regmap_bulk_read(data->regmap, APDS9960_REG_GFIFO_BASE,
774 				      &data->buffer, 4);
775 
776 		if (ret)
777 			goto err_read;
778 
779 		iio_push_to_buffers(data->indio_dev, data->buffer);
780 		cnt--;
781 	}
782 
783 err_read:
784 	data->gesture_mode_running = 0;
785 	mutex_unlock(&data->lock);
786 }
787 
788 static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
789 {
790 	struct iio_dev *indio_dev = private;
791 	struct apds9960_data *data = iio_priv(indio_dev);
792 	int ret, status;
793 
794 	ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status);
795 	if (ret < 0) {
796 		dev_err(&data->client->dev, "irq status reg read failed\n");
797 		return IRQ_HANDLED;
798 	}
799 
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,
803 						    IIO_EV_TYPE_THRESH,
804 						    IIO_EV_DIR_EITHER),
805 			       iio_get_time_ns(indio_dev));
806 		regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1);
807 	}
808 
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,
812 						    IIO_EV_TYPE_THRESH,
813 						    IIO_EV_DIR_EITHER),
814 			       iio_get_time_ns(indio_dev));
815 		regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1);
816 	}
817 
818 	if (status & APDS9960_REG_STATUS_GINT)
819 		apds9960_read_gesture_fifo(data);
820 
821 	return IRQ_HANDLED;
822 }
823 
824 static int apds9960_set_powermode(struct apds9960_data *data, bool state)
825 {
826 	return regmap_update_bits(data->regmap, APDS9960_REG_ENABLE, 1, state);
827 }
828 
829 static int apds9960_buffer_postenable(struct iio_dev *indio_dev)
830 {
831 	struct apds9960_data *data = iio_priv(indio_dev);
832 	int ret;
833 
834 	ret = regmap_field_write(data->reg_int_ges, 1);
835 	if (ret)
836 		return ret;
837 
838 	ret = regmap_field_write(data->reg_enable_ges, 1);
839 	if (ret)
840 		return ret;
841 
842 	pm_runtime_get_sync(&data->client->dev);
843 
844 	return 0;
845 }
846 
847 static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
848 {
849 	struct apds9960_data *data = iio_priv(indio_dev);
850 	int ret;
851 
852 	ret = regmap_field_write(data->reg_enable_ges, 0);
853 	if (ret)
854 		return ret;
855 
856 	ret = regmap_field_write(data->reg_int_ges, 0);
857 	if (ret)
858 		return ret;
859 
860 	pm_runtime_put_autosuspend(&data->client->dev);
861 
862 	return 0;
863 }
864 
865 static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops = {
866 	.postenable = apds9960_buffer_postenable,
867 	.predisable = apds9960_buffer_predisable,
868 };
869 
870 static int apds9960_regfield_init(struct apds9960_data *data)
871 {
872 	struct device *dev = &data->client->dev;
873 	struct regmap *regmap = data->regmap;
874 
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);
880 	}
881 
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);
887 	}
888 
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);
894 	}
895 
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);
901 	}
902 
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);
908 	}
909 
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);
915 	}
916 
917 	return 0;
918 }
919 
920 static int apds9960_chip_init(struct apds9960_data *data)
921 {
922 	int ret;
923 
924 	/* Default IT for ALS of 28 ms */
925 	ret = apds9960_set_it_time(data, 28000);
926 	if (ret)
927 		return ret;
928 
929 	/* Ensure gesture interrupt is OFF */
930 	ret = regmap_field_write(data->reg_int_ges, 0);
931 	if (ret)
932 		return ret;
933 
934 	/* Disable gesture sensor, since polling is useless from user-space */
935 	ret = regmap_field_write(data->reg_enable_ges, 0);
936 	if (ret)
937 		return ret;
938 
939 	/* Ensure proximity interrupt is OFF */
940 	ret = regmap_field_write(data->reg_int_pxs, 0);
941 	if (ret)
942 		return ret;
943 
944 	/* Enable proximity sensor for polling */
945 	ret = regmap_field_write(data->reg_enable_pxs, 1);
946 	if (ret)
947 		return ret;
948 
949 	/* Ensure ALS interrupt is OFF */
950 	ret = regmap_field_write(data->reg_int_als, 0);
951 	if (ret)
952 		return ret;
953 
954 	/* Enable ALS sensor for polling */
955 	ret = regmap_field_write(data->reg_enable_als, 1);
956 	if (ret)
957 		return ret;
958 	/*
959 	 * When enabled trigger an interrupt after 3 readings
960 	 * outside threshold for ALS + PXS
961 	 */
962 	ret = regmap_write(data->regmap, APDS9960_REG_PERS,
963 			   APDS9960_DEFAULT_PERS);
964 	if (ret)
965 		return ret;
966 
967 	/*
968 	 * Wait for 4 event outside gesture threshold to prevent interrupt
969 	 * flooding.
970 	 */
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);
974 	if (ret)
975 		return ret;
976 
977 	/* Default ENTER and EXIT thresholds for the GESTURE engine. */
978 	ret = regmap_write(data->regmap, APDS9960_REG_GPENTH,
979 			   APDS9960_DEFAULT_GPENTH);
980 	if (ret)
981 		return ret;
982 
983 	ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
984 			   APDS9960_DEFAULT_GEXTH);
985 	if (ret)
986 		return ret;
987 
988 	return apds9960_set_powermode(data, 1);
989 }
990 
991 static int apds9960_probe(struct i2c_client *client)
992 {
993 	struct apds9960_data *data;
994 	struct iio_dev *indio_dev;
995 	int ret;
996 
997 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
998 	if (!indio_dev)
999 		return -ENOMEM;
1000 
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;
1007 
1008 	ret = devm_iio_kfifo_buffer_setup(&client->dev, indio_dev,
1009 					  &apds9960_buffer_setup_ops);
1010 	if (ret)
1011 		return ret;
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 void 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 
1084 #ifdef CONFIG_PM
1085 static int apds9960_runtime_suspend(struct device *dev)
1086 {
1087 	struct apds9960_data *data =
1088 			iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1089 
1090 	return apds9960_set_powermode(data, 0);
1091 }
1092 
1093 static int apds9960_runtime_resume(struct device *dev)
1094 {
1095 	struct apds9960_data *data =
1096 			iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1097 
1098 	return apds9960_set_powermode(data, 1);
1099 }
1100 #endif
1101 
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)
1107 };
1108 
1109 static const struct i2c_device_id apds9960_id[] = {
1110 	{ "apds9960" },
1111 	{}
1112 };
1113 MODULE_DEVICE_TABLE(i2c, apds9960_id);
1114 
1115 static const struct acpi_device_id apds9960_acpi_match[] = {
1116 	{ "MSHW0184" },
1117 	{ }
1118 };
1119 MODULE_DEVICE_TABLE(acpi, apds9960_acpi_match);
1120 
1121 static const struct of_device_id apds9960_of_match[] = {
1122 	{ .compatible = "avago,apds9960" },
1123 	{ }
1124 };
1125 MODULE_DEVICE_TABLE(of, apds9960_of_match);
1126 
1127 static struct i2c_driver apds9960_driver = {
1128 	.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,
1133 	},
1134 	.probe		= apds9960_probe,
1135 	.remove		= apds9960_remove,
1136 	.id_table	= apds9960_id,
1137 };
1138 module_i2c_driver(apds9960_driver);
1139 
1140 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
1141 MODULE_DESCRIPTION("APDS9960 Gesture/RGB/ALS/Proximity sensor");
1142 MODULE_LICENSE("GPL");
1143