Linux-libre 5.4.49-gnu
[librecmc/linux-libre.git] / drivers / iio / light / apds9960.c
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/gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/events.h>
25 #include <linux/iio/kfifo_buf.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/of_gpio.h>
28
29 #define APDS9960_REGMAP_NAME    "apds9960_regmap"
30 #define APDS9960_DRV_NAME       "apds9960"
31
32 #define APDS9960_REG_RAM_START  0x00
33 #define APDS9960_REG_RAM_END    0x7f
34
35 #define APDS9960_REG_ENABLE     0x80
36 #define APDS9960_REG_ATIME      0x81
37 #define APDS9960_REG_WTIME      0x83
38
39 #define APDS9960_REG_AILTL      0x84
40 #define APDS9960_REG_AILTH      0x85
41 #define APDS9960_REG_AIHTL      0x86
42 #define APDS9960_REG_AIHTH      0x87
43
44 #define APDS9960_REG_PILT       0x89
45 #define APDS9960_REG_PIHT       0x8b
46 #define APDS9960_REG_PERS       0x8c
47
48 #define APDS9960_REG_CONFIG_1   0x8d
49 #define APDS9960_REG_PPULSE     0x8e
50
51 #define APDS9960_REG_CONTROL    0x8f
52 #define APDS9960_REG_CONTROL_AGAIN_MASK         0x03
53 #define APDS9960_REG_CONTROL_PGAIN_MASK         0x0c
54 #define APDS9960_REG_CONTROL_AGAIN_MASK_SHIFT   0
55 #define APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT   2
56
57 #define APDS9960_REG_CONFIG_2   0x90
58 #define APDS9960_REG_CONFIG_2_GGAIN_MASK        0x60
59 #define APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT  5
60
61 #define APDS9960_REG_ID         0x92
62
63 #define APDS9960_REG_STATUS     0x93
64 #define APDS9960_REG_STATUS_PS_INT      BIT(5)
65 #define APDS9960_REG_STATUS_ALS_INT     BIT(4)
66 #define APDS9960_REG_STATUS_GINT        BIT(2)
67
68 #define APDS9960_REG_PDATA      0x9c
69 #define APDS9960_REG_POFFSET_UR 0x9d
70 #define APDS9960_REG_POFFSET_DL 0x9e
71 #define APDS9960_REG_CONFIG_3   0x9f
72
73 #define APDS9960_REG_GPENTH     0xa0
74 #define APDS9960_REG_GEXTH      0xa1
75
76 #define APDS9960_REG_GCONF_1    0xa2
77 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK           0xc0
78 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT     6
79
80 #define APDS9960_REG_GCONF_2    0xa3
81 #define APDS9960_REG_GOFFSET_U  0xa4
82 #define APDS9960_REG_GOFFSET_D  0xa5
83 #define APDS9960_REG_GPULSE     0xa6
84 #define APDS9960_REG_GOFFSET_L  0xa7
85 #define APDS9960_REG_GOFFSET_R  0xa9
86 #define APDS9960_REG_GCONF_3    0xaa
87
88 #define APDS9960_REG_GCONF_4    0xab
89 #define APDS9960_REG_GFLVL      0xae
90 #define APDS9960_REG_GSTATUS    0xaf
91
92 #define APDS9960_REG_IFORCE     0xe4
93 #define APDS9960_REG_PICLEAR    0xe5
94 #define APDS9960_REG_CICLEAR    0xe6
95 #define APDS9960_REG_AICLEAR    0xe7
96
97 #define APDS9960_DEFAULT_PERS   0x33
98 #define APDS9960_DEFAULT_GPENTH 0x50
99 #define APDS9960_DEFAULT_GEXTH  0x40
100
101 #define APDS9960_MAX_PXS_THRES_VAL      255
102 #define APDS9960_MAX_ALS_THRES_VAL      0xffff
103 #define APDS9960_MAX_INT_TIME_IN_US     1000000
104
105 enum apds9960_als_channel_idx {
106         IDX_ALS_CLEAR, IDX_ALS_RED, IDX_ALS_GREEN, IDX_ALS_BLUE,
107 };
108
109 #define APDS9960_REG_ALS_BASE   0x94
110 #define APDS9960_REG_ALS_CHANNEL(_colour) \
111         (APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2))
112
113 enum apds9960_gesture_channel_idx {
114         IDX_DIR_UP, IDX_DIR_DOWN, IDX_DIR_LEFT, IDX_DIR_RIGHT,
115 };
116
117 #define APDS9960_REG_GFIFO_BASE 0xfc
118 #define APDS9960_REG_GFIFO_DIR(_dir) \
119         (APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir)
120
121 struct apds9960_data {
122         struct i2c_client *client;
123         struct iio_dev *indio_dev;
124         struct mutex lock;
125
126         /* regmap fields */
127         struct regmap *regmap;
128         struct regmap_field *reg_int_als;
129         struct regmap_field *reg_int_ges;
130         struct regmap_field *reg_int_pxs;
131
132         struct regmap_field *reg_enable_als;
133         struct regmap_field *reg_enable_ges;
134         struct regmap_field *reg_enable_pxs;
135
136         /* state */
137         int als_int;
138         int pxs_int;
139         int gesture_mode_running;
140
141         /* gain values */
142         int als_gain;
143         int pxs_gain;
144
145         /* integration time value in us */
146         int als_adc_int_us;
147
148         /* gesture buffer */
149         u8 buffer[4]; /* 4 8-bit channels */
150 };
151
152 static const struct reg_default apds9960_reg_defaults[] = {
153         /* Default ALS integration time = 2.48ms */
154         { APDS9960_REG_ATIME, 0xff },
155 };
156
157 static const struct regmap_range apds9960_volatile_ranges[] = {
158         regmap_reg_range(APDS9960_REG_STATUS,
159                                 APDS9960_REG_PDATA),
160         regmap_reg_range(APDS9960_REG_GFLVL,
161                                 APDS9960_REG_GSTATUS),
162         regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
163                                 APDS9960_REG_GFIFO_DIR(RIGHT)),
164         regmap_reg_range(APDS9960_REG_IFORCE,
165                                 APDS9960_REG_AICLEAR),
166 };
167
168 static const struct regmap_access_table apds9960_volatile_table = {
169         .yes_ranges     = apds9960_volatile_ranges,
170         .n_yes_ranges   = ARRAY_SIZE(apds9960_volatile_ranges),
171 };
172
173 static const struct regmap_range apds9960_precious_ranges[] = {
174         regmap_reg_range(APDS9960_REG_RAM_START, APDS9960_REG_RAM_END),
175 };
176
177 static const struct regmap_access_table apds9960_precious_table = {
178         .yes_ranges     = apds9960_precious_ranges,
179         .n_yes_ranges   = ARRAY_SIZE(apds9960_precious_ranges),
180 };
181
182 static const struct regmap_range apds9960_readable_ranges[] = {
183         regmap_reg_range(APDS9960_REG_ENABLE,
184                                 APDS9960_REG_GSTATUS),
185         regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
186                                 APDS9960_REG_GFIFO_DIR(RIGHT)),
187 };
188
189 static const struct regmap_access_table apds9960_readable_table = {
190         .yes_ranges     = apds9960_readable_ranges,
191         .n_yes_ranges   = ARRAY_SIZE(apds9960_readable_ranges),
192 };
193
194 static const struct regmap_range apds9960_writeable_ranges[] = {
195         regmap_reg_range(APDS9960_REG_ENABLE, APDS9960_REG_CONFIG_2),
196         regmap_reg_range(APDS9960_REG_POFFSET_UR, APDS9960_REG_GCONF_4),
197         regmap_reg_range(APDS9960_REG_IFORCE, APDS9960_REG_AICLEAR),
198 };
199
200 static const struct regmap_access_table apds9960_writeable_table = {
201         .yes_ranges     = apds9960_writeable_ranges,
202         .n_yes_ranges   = ARRAY_SIZE(apds9960_writeable_ranges),
203 };
204
205 static const struct regmap_config apds9960_regmap_config = {
206         .name = APDS9960_REGMAP_NAME,
207         .reg_bits = 8,
208         .val_bits = 8,
209         .use_single_read = true,
210         .use_single_write = true,
211
212         .volatile_table = &apds9960_volatile_table,
213         .precious_table = &apds9960_precious_table,
214         .rd_table = &apds9960_readable_table,
215         .wr_table = &apds9960_writeable_table,
216
217         .reg_defaults = apds9960_reg_defaults,
218         .num_reg_defaults = ARRAY_SIZE(apds9960_reg_defaults),
219         .max_register = APDS9960_REG_GFIFO_DIR(RIGHT),
220         .cache_type = REGCACHE_RBTREE,
221 };
222
223 static const struct iio_event_spec apds9960_pxs_event_spec[] = {
224         {
225                 .type = IIO_EV_TYPE_THRESH,
226                 .dir = IIO_EV_DIR_RISING,
227                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
228                         BIT(IIO_EV_INFO_ENABLE),
229         },
230         {
231                 .type = IIO_EV_TYPE_THRESH,
232                 .dir = IIO_EV_DIR_FALLING,
233                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
234                         BIT(IIO_EV_INFO_ENABLE),
235         },
236 };
237
238 static const struct iio_event_spec apds9960_als_event_spec[] = {
239         {
240                 .type = IIO_EV_TYPE_THRESH,
241                 .dir = IIO_EV_DIR_RISING,
242                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
243                         BIT(IIO_EV_INFO_ENABLE),
244         },
245         {
246                 .type = IIO_EV_TYPE_THRESH,
247                 .dir = IIO_EV_DIR_FALLING,
248                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
249                         BIT(IIO_EV_INFO_ENABLE),
250         },
251 };
252
253 #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \
254         .type = IIO_PROXIMITY, \
255         .channel = _si + 1, \
256         .scan_index = _si, \
257         .indexed = 1, \
258         .scan_type = { \
259                 .sign = 'u', \
260                 .realbits = 8, \
261                 .storagebits = 8, \
262         }, \
263 }
264
265 #define APDS9960_INTENSITY_CHANNEL(_colour) { \
266         .type = IIO_INTENSITY, \
267         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
268         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
269                         BIT(IIO_CHAN_INFO_INT_TIME), \
270         .channel2 = IIO_MOD_LIGHT_##_colour, \
271         .address = APDS9960_REG_ALS_CHANNEL(_colour), \
272         .modified = 1, \
273         .scan_index = -1, \
274 }
275
276 static const unsigned long apds9960_scan_masks[] = {0xf, 0};
277
278 static const struct iio_chan_spec apds9960_channels[] = {
279         {
280                 .type = IIO_PROXIMITY,
281                 .address = APDS9960_REG_PDATA,
282                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
283                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
284                 .channel = 0,
285                 .indexed = 0,
286                 .scan_index = -1,
287
288                 .event_spec = apds9960_pxs_event_spec,
289                 .num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
290         },
291         /* Gesture Sensor */
292         APDS9960_GESTURE_CHANNEL(UP, 0),
293         APDS9960_GESTURE_CHANNEL(DOWN, 1),
294         APDS9960_GESTURE_CHANNEL(LEFT, 2),
295         APDS9960_GESTURE_CHANNEL(RIGHT, 3),
296         /* ALS */
297         {
298                 .type = IIO_INTENSITY,
299                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
300                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
301                         BIT(IIO_CHAN_INFO_INT_TIME),
302                 .channel2 = IIO_MOD_LIGHT_CLEAR,
303                 .address = APDS9960_REG_ALS_CHANNEL(CLEAR),
304                 .modified = 1,
305                 .scan_index = -1,
306
307                 .event_spec = apds9960_als_event_spec,
308                 .num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
309         },
310         /* RGB Sensor */
311         APDS9960_INTENSITY_CHANNEL(RED),
312         APDS9960_INTENSITY_CHANNEL(GREEN),
313         APDS9960_INTENSITY_CHANNEL(BLUE),
314 };
315
316 /* integration time in us */
317 static const int apds9960_int_time[][2] = {
318         { 28000, 246},
319         {100000, 219},
320         {200000, 182},
321         {700000,   0}
322 };
323
324 /* gain mapping */
325 static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8};
326 static const int apds9960_als_gain_map[] = {1, 4, 16, 64};
327
328 static IIO_CONST_ATTR(proximity_scale_available, "1 2 4 8");
329 static IIO_CONST_ATTR(intensity_scale_available, "1 4 16 64");
330 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.028 0.1 0.2 0.7");
331
332 static struct attribute *apds9960_attributes[] = {
333         &iio_const_attr_proximity_scale_available.dev_attr.attr,
334         &iio_const_attr_intensity_scale_available.dev_attr.attr,
335         &iio_const_attr_integration_time_available.dev_attr.attr,
336         NULL,
337 };
338
339 static const struct attribute_group apds9960_attribute_group = {
340         .attrs = apds9960_attributes,
341 };
342
343 static const struct reg_field apds9960_reg_field_int_als =
344                                 REG_FIELD(APDS9960_REG_ENABLE, 4, 4);
345
346 static const struct reg_field apds9960_reg_field_int_ges =
347                                 REG_FIELD(APDS9960_REG_GCONF_4, 1, 1);
348
349 static const struct reg_field apds9960_reg_field_int_pxs =
350                                 REG_FIELD(APDS9960_REG_ENABLE, 5, 5);
351
352 static const struct reg_field apds9960_reg_field_enable_als =
353                                 REG_FIELD(APDS9960_REG_ENABLE, 1, 1);
354
355 static const struct reg_field apds9960_reg_field_enable_ges =
356                                 REG_FIELD(APDS9960_REG_ENABLE, 6, 6);
357
358 static const struct reg_field apds9960_reg_field_enable_pxs =
359                                 REG_FIELD(APDS9960_REG_ENABLE, 2, 2);
360
361 static int apds9960_set_it_time(struct apds9960_data *data, int val2)
362 {
363         int ret = -EINVAL;
364         int idx;
365
366         for (idx = 0; idx < ARRAY_SIZE(apds9960_int_time); idx++) {
367                 if (apds9960_int_time[idx][0] == val2) {
368                         mutex_lock(&data->lock);
369                         ret = regmap_write(data->regmap, APDS9960_REG_ATIME,
370                                                  apds9960_int_time[idx][1]);
371                         if (!ret)
372                                 data->als_adc_int_us = val2;
373                         mutex_unlock(&data->lock);
374                         break;
375                 }
376         }
377
378         return ret;
379 }
380
381 static int apds9960_set_pxs_gain(struct apds9960_data *data, int val)
382 {
383         int ret = -EINVAL;
384         int idx;
385
386         for (idx = 0; idx < ARRAY_SIZE(apds9960_pxs_gain_map); idx++) {
387                 if (apds9960_pxs_gain_map[idx] == val) {
388                         /* pxs + gesture gains are mirrored */
389                         mutex_lock(&data->lock);
390                         ret = regmap_update_bits(data->regmap,
391                                 APDS9960_REG_CONTROL,
392                                 APDS9960_REG_CONTROL_PGAIN_MASK,
393                                 idx << APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT);
394                         if (ret) {
395                                 mutex_unlock(&data->lock);
396                                 break;
397                         }
398
399                         ret = regmap_update_bits(data->regmap,
400                                 APDS9960_REG_CONFIG_2,
401                                 APDS9960_REG_CONFIG_2_GGAIN_MASK,
402                                 idx << APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT);
403                         if (!ret)
404                                 data->pxs_gain = idx;
405                         mutex_unlock(&data->lock);
406                         break;
407                 }
408         }
409
410         return ret;
411 }
412
413 static int apds9960_set_als_gain(struct apds9960_data *data, int val)
414 {
415         int ret = -EINVAL;
416         int idx;
417
418         for (idx = 0; idx < ARRAY_SIZE(apds9960_als_gain_map); idx++) {
419                 if (apds9960_als_gain_map[idx] == val) {
420                         mutex_lock(&data->lock);
421                         ret = regmap_update_bits(data->regmap,
422                                         APDS9960_REG_CONTROL,
423                                         APDS9960_REG_CONTROL_AGAIN_MASK, idx);
424                         if (!ret)
425                                 data->als_gain = idx;
426                         mutex_unlock(&data->lock);
427                         break;
428                 }
429         }
430
431         return ret;
432 }
433
434 #ifdef CONFIG_PM
435 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
436 {
437         struct device *dev = &data->client->dev;
438         int ret = 0;
439
440         mutex_lock(&data->lock);
441
442         if (on) {
443                 int suspended;
444
445                 suspended = pm_runtime_suspended(dev);
446                 ret = pm_runtime_get_sync(dev);
447
448                 /* Allow one integration cycle before allowing a reading */
449                 if (suspended)
450                         usleep_range(data->als_adc_int_us,
451                                      APDS9960_MAX_INT_TIME_IN_US);
452         } else {
453                 pm_runtime_mark_last_busy(dev);
454                 ret = pm_runtime_put_autosuspend(dev);
455         }
456
457         mutex_unlock(&data->lock);
458
459         return ret;
460 }
461 #else
462 static int apds9960_set_power_state(struct apds9960_data *data, bool on)
463 {
464         return 0;
465 }
466 #endif
467
468 static int apds9960_read_raw(struct iio_dev *indio_dev,
469                              struct iio_chan_spec const *chan,
470                              int *val, int *val2, long mask)
471 {
472         struct apds9960_data *data = iio_priv(indio_dev);
473         __le16 buf;
474         int ret = -EINVAL;
475
476         if (data->gesture_mode_running)
477                 return -EBUSY;
478
479         switch (mask) {
480         case IIO_CHAN_INFO_RAW:
481                 apds9960_set_power_state(data, true);
482                 switch (chan->type) {
483                 case IIO_PROXIMITY:
484                         ret = regmap_read(data->regmap, chan->address, val);
485                         if (!ret)
486                                 ret = IIO_VAL_INT;
487                         break;
488                 case IIO_INTENSITY:
489                         ret = regmap_bulk_read(data->regmap, chan->address,
490                                                &buf, 2);
491                         if (!ret) {
492                                 ret = IIO_VAL_INT;
493                                 *val = le16_to_cpu(buf);
494                         }
495                         break;
496                 default:
497                         ret = -EINVAL;
498                 }
499                 apds9960_set_power_state(data, false);
500                 break;
501         case IIO_CHAN_INFO_INT_TIME:
502                 /* RGB + ALS sensors only have integration time */
503                 mutex_lock(&data->lock);
504                 switch (chan->type) {
505                 case IIO_INTENSITY:
506                         *val = 0;
507                         *val2 = data->als_adc_int_us;
508                         ret = IIO_VAL_INT_PLUS_MICRO;
509                         break;
510                 default:
511                         ret = -EINVAL;
512                 }
513                 mutex_unlock(&data->lock);
514                 break;
515         case IIO_CHAN_INFO_SCALE:
516                 mutex_lock(&data->lock);
517                 switch (chan->type) {
518                 case IIO_PROXIMITY:
519                         *val = apds9960_pxs_gain_map[data->pxs_gain];
520                         ret = IIO_VAL_INT;
521                         break;
522                 case IIO_INTENSITY:
523                         *val = apds9960_als_gain_map[data->als_gain];
524                         ret = IIO_VAL_INT;
525                         break;
526                 default:
527                         ret = -EINVAL;
528                 }
529                 mutex_unlock(&data->lock);
530                 break;
531         }
532
533         return ret;
534 };
535
536 static int apds9960_write_raw(struct iio_dev *indio_dev,
537                              struct iio_chan_spec const *chan,
538                              int val, int val2, long mask)
539 {
540         struct apds9960_data *data = iio_priv(indio_dev);
541
542         switch (mask) {
543         case IIO_CHAN_INFO_INT_TIME:
544                 /* RGB + ALS sensors only have int time */
545                 switch (chan->type) {
546                 case IIO_INTENSITY:
547                         if (val != 0)
548                                 return -EINVAL;
549                         return apds9960_set_it_time(data, val2);
550                 default:
551                         return -EINVAL;
552                 }
553         case IIO_CHAN_INFO_SCALE:
554                 if (val2 != 0)
555                         return -EINVAL;
556                 switch (chan->type) {
557                 case IIO_PROXIMITY:
558                         return apds9960_set_pxs_gain(data, val);
559                 case IIO_INTENSITY:
560                         return apds9960_set_als_gain(data, val);
561                 default:
562                         return -EINVAL;
563                 }
564         default:
565                 return -EINVAL;
566         };
567
568         return 0;
569 }
570
571 static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
572                                          enum iio_event_direction dir,
573                                          u8 *reg)
574 {
575         switch (dir) {
576         case IIO_EV_DIR_RISING:
577                 switch (chan->type) {
578                 case IIO_PROXIMITY:
579                         *reg = APDS9960_REG_PIHT;
580                         break;
581                 case IIO_INTENSITY:
582                         *reg = APDS9960_REG_AIHTL;
583                         break;
584                 default:
585                         return -EINVAL;
586                 }
587                 break;
588         case IIO_EV_DIR_FALLING:
589                 switch (chan->type) {
590                 case IIO_PROXIMITY:
591                         *reg = APDS9960_REG_PILT;
592                         break;
593                 case IIO_INTENSITY:
594                         *reg = APDS9960_REG_AILTL;
595                         break;
596                 default:
597                         return -EINVAL;
598                 }
599                 break;
600         default:
601                 return -EINVAL;
602         }
603
604         return 0;
605 }
606
607 static int apds9960_read_event(struct iio_dev *indio_dev,
608                                const struct iio_chan_spec *chan,
609                                enum iio_event_type type,
610                                enum iio_event_direction dir,
611                                enum iio_event_info info,
612                                int *val, int *val2)
613 {
614         u8 reg;
615         __le16 buf;
616         int ret = 0;
617         struct apds9960_data *data = iio_priv(indio_dev);
618
619         if (info != IIO_EV_INFO_VALUE)
620                 return -EINVAL;
621
622         ret = apds9960_get_thres_reg(chan, dir, &reg);
623         if (ret < 0)
624                 return ret;
625
626         if (chan->type == IIO_PROXIMITY) {
627                 ret = regmap_read(data->regmap, reg, val);
628                 if (ret < 0)
629                         return ret;
630         } else if (chan->type == IIO_INTENSITY) {
631                 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
632                 if (ret < 0)
633                         return ret;
634                 *val = le16_to_cpu(buf);
635         } else
636                 return -EINVAL;
637
638         *val2 = 0;
639
640         return IIO_VAL_INT;
641 }
642
643 static int apds9960_write_event(struct iio_dev *indio_dev,
644                                 const struct iio_chan_spec *chan,
645                                 enum iio_event_type type,
646                                 enum iio_event_direction dir,
647                                 enum iio_event_info info,
648                                 int val, int val2)
649 {
650         u8 reg;
651         __le16 buf;
652         int ret = 0;
653         struct apds9960_data *data = iio_priv(indio_dev);
654
655         if (info != IIO_EV_INFO_VALUE)
656                 return -EINVAL;
657
658         ret = apds9960_get_thres_reg(chan, dir, &reg);
659         if (ret < 0)
660                 return ret;
661
662         if (chan->type == IIO_PROXIMITY) {
663                 if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL)
664                         return -EINVAL;
665                 ret = regmap_write(data->regmap, reg, val);
666                 if (ret < 0)
667                         return ret;
668         } else if (chan->type == IIO_INTENSITY) {
669                 if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL)
670                         return -EINVAL;
671                 buf = cpu_to_le16(val);
672                 ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
673                 if (ret < 0)
674                         return ret;
675         } else
676                 return -EINVAL;
677
678         return 0;
679 }
680
681 static int apds9960_read_event_config(struct iio_dev *indio_dev,
682                                       const struct iio_chan_spec *chan,
683                                       enum iio_event_type type,
684                                       enum iio_event_direction dir)
685 {
686         struct apds9960_data *data = iio_priv(indio_dev);
687
688         switch (chan->type) {
689         case IIO_PROXIMITY:
690                 return data->pxs_int;
691         case IIO_INTENSITY:
692                 return data->als_int;
693         default:
694                 return -EINVAL;
695         }
696
697         return 0;
698 }
699
700 static int apds9960_write_event_config(struct iio_dev *indio_dev,
701                                        const struct iio_chan_spec *chan,
702                                        enum iio_event_type type,
703                                        enum iio_event_direction dir,
704                                        int state)
705 {
706         struct apds9960_data *data = iio_priv(indio_dev);
707         int ret;
708
709         state = !!state;
710
711         switch (chan->type) {
712         case IIO_PROXIMITY:
713                 if (data->pxs_int == state)
714                         return -EINVAL;
715
716                 ret = regmap_field_write(data->reg_int_pxs, state);
717                 if (ret)
718                         return ret;
719                 data->pxs_int = state;
720                 apds9960_set_power_state(data, state);
721                 break;
722         case IIO_INTENSITY:
723                 if (data->als_int == state)
724                         return -EINVAL;
725
726                 ret = regmap_field_write(data->reg_int_als, state);
727                 if (ret)
728                         return ret;
729                 data->als_int = state;
730                 apds9960_set_power_state(data, state);
731                 break;
732         default:
733                 return -EINVAL;
734         }
735
736         return 0;
737 }
738
739 static const struct iio_info apds9960_info = {
740         .attrs = &apds9960_attribute_group,
741         .read_raw = apds9960_read_raw,
742         .write_raw = apds9960_write_raw,
743         .read_event_value = apds9960_read_event,
744         .write_event_value = apds9960_write_event,
745         .read_event_config = apds9960_read_event_config,
746         .write_event_config = apds9960_write_event_config,
747
748 };
749
750 static inline int apds9660_fifo_is_empty(struct apds9960_data *data)
751 {
752         int cnt;
753         int ret;
754
755         ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt);
756         if (ret)
757                 return ret;
758
759         return cnt;
760 }
761
762 static void apds9960_read_gesture_fifo(struct apds9960_data *data)
763 {
764         int ret, cnt = 0;
765
766         mutex_lock(&data->lock);
767         data->gesture_mode_running = 1;
768
769         while (cnt || (cnt = apds9660_fifo_is_empty(data) > 0)) {
770                 ret = regmap_bulk_read(data->regmap, APDS9960_REG_GFIFO_BASE,
771                                       &data->buffer, 4);
772
773                 if (ret)
774                         goto err_read;
775
776                 iio_push_to_buffers(data->indio_dev, data->buffer);
777                 cnt--;
778         }
779
780 err_read:
781         data->gesture_mode_running = 0;
782         mutex_unlock(&data->lock);
783 }
784
785 static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
786 {
787         struct iio_dev *indio_dev = private;
788         struct apds9960_data *data = iio_priv(indio_dev);
789         int ret, status;
790
791         ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status);
792         if (ret < 0) {
793                 dev_err(&data->client->dev, "irq status reg read failed\n");
794                 return IRQ_HANDLED;
795         }
796
797         if ((status & APDS9960_REG_STATUS_ALS_INT) && data->als_int) {
798                 iio_push_event(indio_dev,
799                                IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
800                                                     IIO_EV_TYPE_THRESH,
801                                                     IIO_EV_DIR_EITHER),
802                                iio_get_time_ns(indio_dev));
803                 regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1);
804         }
805
806         if ((status & APDS9960_REG_STATUS_PS_INT) && data->pxs_int) {
807                 iio_push_event(indio_dev,
808                                IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
809                                                     IIO_EV_TYPE_THRESH,
810                                                     IIO_EV_DIR_EITHER),
811                                iio_get_time_ns(indio_dev));
812                 regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1);
813         }
814
815         if (status & APDS9960_REG_STATUS_GINT)
816                 apds9960_read_gesture_fifo(data);
817
818         return IRQ_HANDLED;
819 }
820
821 static int apds9960_set_powermode(struct apds9960_data *data, bool state)
822 {
823         return regmap_update_bits(data->regmap, APDS9960_REG_ENABLE, 1, state);
824 }
825
826 static int apds9960_buffer_postenable(struct iio_dev *indio_dev)
827 {
828         struct apds9960_data *data = iio_priv(indio_dev);
829         int ret;
830
831         ret = regmap_field_write(data->reg_int_ges, 1);
832         if (ret)
833                 return ret;
834
835         ret = regmap_field_write(data->reg_enable_ges, 1);
836         if (ret)
837                 return ret;
838
839         pm_runtime_get_sync(&data->client->dev);
840
841         return 0;
842 }
843
844 static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
845 {
846         struct apds9960_data *data = iio_priv(indio_dev);
847         int ret;
848
849         ret = regmap_field_write(data->reg_enable_ges, 0);
850         if (ret)
851                 return ret;
852
853         ret = regmap_field_write(data->reg_int_ges, 0);
854         if (ret)
855                 return ret;
856
857         pm_runtime_put_autosuspend(&data->client->dev);
858
859         return 0;
860 }
861
862 static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops = {
863         .postenable = apds9960_buffer_postenable,
864         .predisable = apds9960_buffer_predisable,
865 };
866
867 static int apds9960_regfield_init(struct apds9960_data *data)
868 {
869         struct device *dev = &data->client->dev;
870         struct regmap *regmap = data->regmap;
871
872         data->reg_int_als = devm_regmap_field_alloc(dev, regmap,
873                                                 apds9960_reg_field_int_als);
874         if (IS_ERR(data->reg_int_als)) {
875                 dev_err(dev, "INT ALS reg field init failed\n");
876                 return PTR_ERR(data->reg_int_als);
877         }
878
879         data->reg_int_ges = devm_regmap_field_alloc(dev, regmap,
880                                                 apds9960_reg_field_int_ges);
881         if (IS_ERR(data->reg_int_ges)) {
882                 dev_err(dev, "INT gesture reg field init failed\n");
883                 return PTR_ERR(data->reg_int_ges);
884         }
885
886         data->reg_int_pxs = devm_regmap_field_alloc(dev, regmap,
887                                                 apds9960_reg_field_int_pxs);
888         if (IS_ERR(data->reg_int_pxs)) {
889                 dev_err(dev, "INT pxs reg field init failed\n");
890                 return PTR_ERR(data->reg_int_pxs);
891         }
892
893         data->reg_enable_als = devm_regmap_field_alloc(dev, regmap,
894                                                 apds9960_reg_field_enable_als);
895         if (IS_ERR(data->reg_enable_als)) {
896                 dev_err(dev, "Enable ALS reg field init failed\n");
897                 return PTR_ERR(data->reg_enable_als);
898         }
899
900         data->reg_enable_ges = devm_regmap_field_alloc(dev, regmap,
901                                                 apds9960_reg_field_enable_ges);
902         if (IS_ERR(data->reg_enable_ges)) {
903                 dev_err(dev, "Enable gesture reg field init failed\n");
904                 return PTR_ERR(data->reg_enable_ges);
905         }
906
907         data->reg_enable_pxs = devm_regmap_field_alloc(dev, regmap,
908                                                 apds9960_reg_field_enable_pxs);
909         if (IS_ERR(data->reg_enable_pxs)) {
910                 dev_err(dev, "Enable PXS reg field init failed\n");
911                 return PTR_ERR(data->reg_enable_pxs);
912         }
913
914         return 0;
915 }
916
917 static int apds9960_chip_init(struct apds9960_data *data)
918 {
919         int ret;
920
921         /* Default IT for ALS of 28 ms */
922         ret = apds9960_set_it_time(data, 28000);
923         if (ret)
924                 return ret;
925
926         /* Ensure gesture interrupt is OFF */
927         ret = regmap_field_write(data->reg_int_ges, 0);
928         if (ret)
929                 return ret;
930
931         /* Disable gesture sensor, since polling is useless from user-space */
932         ret = regmap_field_write(data->reg_enable_ges, 0);
933         if (ret)
934                 return ret;
935
936         /* Ensure proximity interrupt is OFF */
937         ret = regmap_field_write(data->reg_int_pxs, 0);
938         if (ret)
939                 return ret;
940
941         /* Enable proximity sensor for polling */
942         ret = regmap_field_write(data->reg_enable_pxs, 1);
943         if (ret)
944                 return ret;
945
946         /* Ensure ALS interrupt is OFF */
947         ret = regmap_field_write(data->reg_int_als, 0);
948         if (ret)
949                 return ret;
950
951         /* Enable ALS sensor for polling */
952         ret = regmap_field_write(data->reg_enable_als, 1);
953         if (ret)
954                 return ret;
955         /*
956          * When enabled trigger an interrupt after 3 readings
957          * outside threshold for ALS + PXS
958          */
959         ret = regmap_write(data->regmap, APDS9960_REG_PERS,
960                            APDS9960_DEFAULT_PERS);
961         if (ret)
962                 return ret;
963
964         /*
965          * Wait for 4 event outside gesture threshold to prevent interrupt
966          * flooding.
967          */
968         ret = regmap_update_bits(data->regmap, APDS9960_REG_GCONF_1,
969                         APDS9960_REG_GCONF_1_GFIFO_THRES_MASK,
970                         BIT(0) << APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT);
971         if (ret)
972                 return ret;
973
974         /* Default ENTER and EXIT thresholds for the GESTURE engine. */
975         ret = regmap_write(data->regmap, APDS9960_REG_GPENTH,
976                            APDS9960_DEFAULT_GPENTH);
977         if (ret)
978                 return ret;
979
980         ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
981                            APDS9960_DEFAULT_GEXTH);
982         if (ret)
983                 return ret;
984
985         return apds9960_set_powermode(data, 1);
986 }
987
988 static int apds9960_probe(struct i2c_client *client,
989                           const struct i2c_device_id *id)
990 {
991         struct apds9960_data *data;
992         struct iio_buffer *buffer;
993         struct iio_dev *indio_dev;
994         int ret;
995
996         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
997         if (!indio_dev)
998                 return -ENOMEM;
999
1000         buffer = devm_iio_kfifo_allocate(&client->dev);
1001         if (!buffer)
1002                 return -ENOMEM;
1003
1004         iio_device_attach_buffer(indio_dev, buffer);
1005
1006         indio_dev->dev.parent = &client->dev;
1007         indio_dev->info = &apds9960_info;
1008         indio_dev->name = APDS9960_DRV_NAME;
1009         indio_dev->channels = apds9960_channels;
1010         indio_dev->num_channels = ARRAY_SIZE(apds9960_channels);
1011         indio_dev->available_scan_masks = apds9960_scan_masks;
1012         indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
1013         indio_dev->setup_ops = &apds9960_buffer_setup_ops;
1014
1015         data = iio_priv(indio_dev);
1016         i2c_set_clientdata(client, indio_dev);
1017
1018         data->regmap = devm_regmap_init_i2c(client, &apds9960_regmap_config);
1019         if (IS_ERR(data->regmap)) {
1020                 dev_err(&client->dev, "regmap initialization failed.\n");
1021                 return PTR_ERR(data->regmap);
1022         }
1023
1024         data->client = client;
1025         data->indio_dev = indio_dev;
1026         mutex_init(&data->lock);
1027
1028         ret = pm_runtime_set_active(&client->dev);
1029         if (ret)
1030                 goto error_power_down;
1031
1032         pm_runtime_enable(&client->dev);
1033         pm_runtime_set_autosuspend_delay(&client->dev, 5000);
1034         pm_runtime_use_autosuspend(&client->dev);
1035
1036         apds9960_set_power_state(data, true);
1037
1038         ret = apds9960_regfield_init(data);
1039         if (ret)
1040                 goto error_power_down;
1041
1042         ret = apds9960_chip_init(data);
1043         if (ret)
1044                 goto error_power_down;
1045
1046         if (client->irq <= 0) {
1047                 dev_err(&client->dev, "no valid irq defined\n");
1048                 ret = -EINVAL;
1049                 goto error_power_down;
1050         }
1051         ret = devm_request_threaded_irq(&client->dev, client->irq,
1052                                         NULL, apds9960_interrupt_handler,
1053                                         IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1054                                         "apds9960_event",
1055                                         indio_dev);
1056         if (ret) {
1057                 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
1058                 goto error_power_down;
1059         }
1060
1061         ret = iio_device_register(indio_dev);
1062         if (ret)
1063                 goto error_power_down;
1064
1065         apds9960_set_power_state(data, false);
1066
1067         return 0;
1068
1069 error_power_down:
1070         apds9960_set_power_state(data, false);
1071
1072         return ret;
1073 }
1074
1075 static int apds9960_remove(struct i2c_client *client)
1076 {
1077         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1078         struct apds9960_data *data = iio_priv(indio_dev);
1079
1080         iio_device_unregister(indio_dev);
1081         pm_runtime_disable(&client->dev);
1082         pm_runtime_set_suspended(&client->dev);
1083         apds9960_set_powermode(data, 0);
1084
1085         return 0;
1086 }
1087
1088 #ifdef CONFIG_PM
1089 static int apds9960_runtime_suspend(struct device *dev)
1090 {
1091         struct apds9960_data *data =
1092                         iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1093
1094         return apds9960_set_powermode(data, 0);
1095 }
1096
1097 static int apds9960_runtime_resume(struct device *dev)
1098 {
1099         struct apds9960_data *data =
1100                         iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1101
1102         return apds9960_set_powermode(data, 1);
1103 }
1104 #endif
1105
1106 static const struct dev_pm_ops apds9960_pm_ops = {
1107         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1108                                 pm_runtime_force_resume)
1109         SET_RUNTIME_PM_OPS(apds9960_runtime_suspend,
1110                            apds9960_runtime_resume, NULL)
1111 };
1112
1113 static const struct i2c_device_id apds9960_id[] = {
1114         { "apds9960", 0 },
1115         {}
1116 };
1117 MODULE_DEVICE_TABLE(i2c, apds9960_id);
1118
1119 static const struct of_device_id apds9960_of_match[] = {
1120         { .compatible = "avago,apds9960" },
1121         { }
1122 };
1123 MODULE_DEVICE_TABLE(of, apds9960_of_match);
1124
1125 static struct i2c_driver apds9960_driver = {
1126         .driver = {
1127                 .name   = APDS9960_DRV_NAME,
1128                 .of_match_table = apds9960_of_match,
1129                 .pm     = &apds9960_pm_ops,
1130         },
1131         .probe          = apds9960_probe,
1132         .remove         = apds9960_remove,
1133         .id_table       = apds9960_id,
1134 };
1135 module_i2c_driver(apds9960_driver);
1136
1137 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
1138 MODULE_DESCRIPTION("APDS9960 Gesture/RGB/ALS/Proximity sensor");
1139 MODULE_LICENSE("GPL");