GNU Linux-libre 4.4.294-gnu1
[releases.git] / drivers / iio / light / apds9960.c
1 /*
2  * apds9960.c - Support for Avago APDS9960 gesture/RGB/ALS/proximity sensor
3  *
4  * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * TODO: gesture + proximity calib offsets
17  */
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/mutex.h>
24 #include <linux/err.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <linux/i2c.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/regmap.h>
30 #include <linux/iio/iio.h>
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/events.h>
33 #include <linux/iio/kfifo_buf.h>
34 #include <linux/iio/sysfs.h>
35 #include <linux/of_gpio.h>
36
37 #define APDS9960_REGMAP_NAME    "apds9960_regmap"
38 #define APDS9960_DRV_NAME       "apds9960"
39
40 #define APDS9960_REG_RAM_START  0x00
41 #define APDS9960_REG_RAM_END    0x7f
42
43 #define APDS9960_REG_ENABLE     0x80
44 #define APDS9960_REG_ATIME      0x81
45 #define APDS9960_REG_WTIME      0x83
46
47 #define APDS9960_REG_AILTL      0x84
48 #define APDS9960_REG_AILTH      0x85
49 #define APDS9960_REG_AIHTL      0x86
50 #define APDS9960_REG_AIHTH      0x87
51
52 #define APDS9960_REG_PILT       0x89
53 #define APDS9960_REG_PIHT       0x8b
54 #define APDS9960_REG_PERS       0x8c
55
56 #define APDS9960_REG_CONFIG_1   0x8d
57 #define APDS9960_REG_PPULSE     0x8e
58
59 #define APDS9960_REG_CONTROL    0x8f
60 #define APDS9960_REG_CONTROL_AGAIN_MASK         0x03
61 #define APDS9960_REG_CONTROL_PGAIN_MASK         0x0c
62 #define APDS9960_REG_CONTROL_AGAIN_MASK_SHIFT   0
63 #define APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT   2
64
65 #define APDS9960_REG_CONFIG_2   0x90
66 #define APDS9960_REG_CONFIG_2_GGAIN_MASK        0x60
67 #define APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT  5
68
69 #define APDS9960_REG_ID         0x92
70
71 #define APDS9960_REG_STATUS     0x93
72 #define APDS9960_REG_STATUS_PS_INT      BIT(5)
73 #define APDS9960_REG_STATUS_ALS_INT     BIT(4)
74 #define APDS9960_REG_STATUS_GINT        BIT(2)
75
76 #define APDS9960_REG_PDATA      0x9c
77 #define APDS9960_REG_POFFSET_UR 0x9d
78 #define APDS9960_REG_POFFSET_DL 0x9e
79 #define APDS9960_REG_CONFIG_3   0x9f
80
81 #define APDS9960_REG_GPENTH     0xa0
82 #define APDS9960_REG_GEXTH      0xa1
83
84 #define APDS9960_REG_GCONF_1    0xa2
85 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK           0xc0
86 #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT     6
87
88 #define APDS9960_REG_GCONF_2    0xa3
89 #define APDS9960_REG_GOFFSET_U  0xa4
90 #define APDS9960_REG_GOFFSET_D  0xa5
91 #define APDS9960_REG_GPULSE     0xa6
92 #define APDS9960_REG_GOFFSET_L  0xa7
93 #define APDS9960_REG_GOFFSET_R  0xa9
94 #define APDS9960_REG_GCONF_3    0xaa
95
96 #define APDS9960_REG_GCONF_4    0xab
97 #define APDS9960_REG_GFLVL      0xae
98 #define APDS9960_REG_GSTATUS    0xaf
99
100 #define APDS9960_REG_IFORCE     0xe4
101 #define APDS9960_REG_PICLEAR    0xe5
102 #define APDS9960_REG_CICLEAR    0xe6
103 #define APDS9960_REG_AICLEAR    0xe7
104
105 #define APDS9960_DEFAULT_PERS   0x33
106 #define APDS9960_DEFAULT_GPENTH 0x50
107 #define APDS9960_DEFAULT_GEXTH  0x40
108
109 #define APDS9960_MAX_PXS_THRES_VAL      255
110 #define APDS9960_MAX_ALS_THRES_VAL      0xffff
111 #define APDS9960_MAX_INT_TIME_IN_US     1000000
112
113 enum apds9960_als_channel_idx {
114         IDX_ALS_CLEAR, IDX_ALS_RED, IDX_ALS_GREEN, IDX_ALS_BLUE,
115 };
116
117 #define APDS9960_REG_ALS_BASE   0x94
118 #define APDS9960_REG_ALS_CHANNEL(_colour) \
119         (APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2))
120
121 enum apds9960_gesture_channel_idx {
122         IDX_DIR_UP, IDX_DIR_DOWN, IDX_DIR_LEFT, IDX_DIR_RIGHT,
123 };
124
125 #define APDS9960_REG_GFIFO_BASE 0xfc
126 #define APDS9960_REG_GFIFO_DIR(_dir) \
127         (APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir)
128
129 struct apds9960_data {
130         struct i2c_client *client;
131         struct iio_dev *indio_dev;
132         struct mutex lock;
133
134         /* regmap fields */
135         struct regmap *regmap;
136         struct regmap_field *reg_int_als;
137         struct regmap_field *reg_int_ges;
138         struct regmap_field *reg_int_pxs;
139
140         struct regmap_field *reg_enable_als;
141         struct regmap_field *reg_enable_ges;
142         struct regmap_field *reg_enable_pxs;
143
144         /* state */
145         int als_int;
146         int pxs_int;
147         int gesture_mode_running;
148
149         /* gain values */
150         int als_gain;
151         int pxs_gain;
152
153         /* integration time value in us */
154         int als_adc_int_us;
155
156         /* gesture buffer */
157         u8 buffer[4]; /* 4 8-bit channels */
158 };
159
160 static const struct reg_default apds9960_reg_defaults[] = {
161         /* Default ALS integration time = 2.48ms */
162         { APDS9960_REG_ATIME, 0xff },
163 };
164
165 static const struct regmap_range apds9960_volatile_ranges[] = {
166         regmap_reg_range(APDS9960_REG_STATUS,
167                                 APDS9960_REG_PDATA),
168         regmap_reg_range(APDS9960_REG_GFLVL,
169                                 APDS9960_REG_GSTATUS),
170         regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
171                                 APDS9960_REG_GFIFO_DIR(RIGHT)),
172         regmap_reg_range(APDS9960_REG_IFORCE,
173                                 APDS9960_REG_AICLEAR),
174 };
175
176 static const struct regmap_access_table apds9960_volatile_table = {
177         .yes_ranges     = apds9960_volatile_ranges,
178         .n_yes_ranges   = ARRAY_SIZE(apds9960_volatile_ranges),
179 };
180
181 static const struct regmap_range apds9960_precious_ranges[] = {
182         regmap_reg_range(APDS9960_REG_RAM_START, APDS9960_REG_RAM_END),
183 };
184
185 static const struct regmap_access_table apds9960_precious_table = {
186         .yes_ranges     = apds9960_precious_ranges,
187         .n_yes_ranges   = ARRAY_SIZE(apds9960_precious_ranges),
188 };
189
190 static const struct regmap_range apds9960_readable_ranges[] = {
191         regmap_reg_range(APDS9960_REG_ENABLE,
192                                 APDS9960_REG_GSTATUS),
193         regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
194                                 APDS9960_REG_GFIFO_DIR(RIGHT)),
195 };
196
197 static const struct regmap_access_table apds9960_readable_table = {
198         .yes_ranges     = apds9960_readable_ranges,
199         .n_yes_ranges   = ARRAY_SIZE(apds9960_readable_ranges),
200 };
201
202 static const struct regmap_range apds9960_writeable_ranges[] = {
203         regmap_reg_range(APDS9960_REG_ENABLE, APDS9960_REG_CONFIG_2),
204         regmap_reg_range(APDS9960_REG_POFFSET_UR, APDS9960_REG_GCONF_4),
205         regmap_reg_range(APDS9960_REG_IFORCE, APDS9960_REG_AICLEAR),
206 };
207
208 static const struct regmap_access_table apds9960_writeable_table = {
209         .yes_ranges     = apds9960_writeable_ranges,
210         .n_yes_ranges   = ARRAY_SIZE(apds9960_writeable_ranges),
211 };
212
213 static const struct regmap_config apds9960_regmap_config = {
214         .name = APDS9960_REGMAP_NAME,
215         .reg_bits = 8,
216         .val_bits = 8,
217         .use_single_rw = 1,
218
219         .volatile_table = &apds9960_volatile_table,
220         .precious_table = &apds9960_precious_table,
221         .rd_table = &apds9960_readable_table,
222         .wr_table = &apds9960_writeable_table,
223
224         .reg_defaults = apds9960_reg_defaults,
225         .num_reg_defaults = ARRAY_SIZE(apds9960_reg_defaults),
226         .max_register = APDS9960_REG_GFIFO_DIR(RIGHT),
227         .cache_type = REGCACHE_RBTREE,
228 };
229
230 static const struct iio_event_spec apds9960_pxs_event_spec[] = {
231         {
232                 .type = IIO_EV_TYPE_THRESH,
233                 .dir = IIO_EV_DIR_RISING,
234                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
235                         BIT(IIO_EV_INFO_ENABLE),
236         },
237         {
238                 .type = IIO_EV_TYPE_THRESH,
239                 .dir = IIO_EV_DIR_FALLING,
240                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
241                         BIT(IIO_EV_INFO_ENABLE),
242         },
243 };
244
245 static const struct iio_event_spec apds9960_als_event_spec[] = {
246         {
247                 .type = IIO_EV_TYPE_THRESH,
248                 .dir = IIO_EV_DIR_RISING,
249                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
250                         BIT(IIO_EV_INFO_ENABLE),
251         },
252         {
253                 .type = IIO_EV_TYPE_THRESH,
254                 .dir = IIO_EV_DIR_FALLING,
255                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
256                         BIT(IIO_EV_INFO_ENABLE),
257         },
258 };
259
260 #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \
261         .type = IIO_PROXIMITY, \
262         .channel = _si + 1, \
263         .scan_index = _si, \
264         .indexed = 1, \
265         .scan_type = { \
266                 .sign = 'u', \
267                 .realbits = 8, \
268                 .storagebits = 8, \
269         }, \
270 }
271
272 #define APDS9960_INTENSITY_CHANNEL(_colour) { \
273         .type = IIO_INTENSITY, \
274         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
275         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
276                         BIT(IIO_CHAN_INFO_INT_TIME), \
277         .channel2 = IIO_MOD_LIGHT_##_colour, \
278         .address = APDS9960_REG_ALS_CHANNEL(_colour), \
279         .modified = 1, \
280         .scan_index = -1, \
281 }
282
283 static const unsigned long apds9960_scan_masks[] = {0xf, 0};
284
285 static const struct iio_chan_spec apds9960_channels[] = {
286         {
287                 .type = IIO_PROXIMITY,
288                 .address = APDS9960_REG_PDATA,
289                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
290                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
291                 .channel = 0,
292                 .indexed = 0,
293                 .scan_index = -1,
294
295                 .event_spec = apds9960_pxs_event_spec,
296                 .num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
297         },
298         /* Gesture Sensor */
299         APDS9960_GESTURE_CHANNEL(UP, 0),
300         APDS9960_GESTURE_CHANNEL(DOWN, 1),
301         APDS9960_GESTURE_CHANNEL(LEFT, 2),
302         APDS9960_GESTURE_CHANNEL(RIGHT, 3),
303         /* ALS */
304         {
305                 .type = IIO_INTENSITY,
306                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
307                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
308                         BIT(IIO_CHAN_INFO_INT_TIME),
309                 .channel2 = IIO_MOD_LIGHT_CLEAR,
310                 .address = APDS9960_REG_ALS_CHANNEL(CLEAR),
311                 .modified = 1,
312                 .scan_index = -1,
313
314                 .event_spec = apds9960_als_event_spec,
315                 .num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
316         },
317         /* RGB Sensor */
318         APDS9960_INTENSITY_CHANNEL(RED),
319         APDS9960_INTENSITY_CHANNEL(GREEN),
320         APDS9960_INTENSITY_CHANNEL(BLUE),
321 };
322
323 /* integration time in us */
324 static const int apds9960_int_time[][2] =
325         { {28000, 246}, {100000, 219}, {200000, 182}, {700000, 0} };
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 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_CONFIG_2,
404                                 APDS9960_REG_CONFIG_2_GGAIN_MASK,
405                                 idx << APDS9960_REG_CONFIG_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                         break;
498                 default:
499                         ret = -EINVAL;
500                 }
501                 apds9960_set_power_state(data, false);
502                 break;
503         case IIO_CHAN_INFO_INT_TIME:
504                 /* RGB + ALS sensors only have integration time */
505                 mutex_lock(&data->lock);
506                 switch (chan->type) {
507                 case IIO_INTENSITY:
508                         *val = 0;
509                         *val2 = data->als_adc_int_us;
510                         ret = IIO_VAL_INT_PLUS_MICRO;
511                         break;
512                 default:
513                         ret = -EINVAL;
514                 }
515                 mutex_unlock(&data->lock);
516                 break;
517         case IIO_CHAN_INFO_SCALE:
518                 mutex_lock(&data->lock);
519                 switch (chan->type) {
520                 case IIO_PROXIMITY:
521                         *val = apds9960_pxs_gain_map[data->pxs_gain];
522                         ret = IIO_VAL_INT;
523                         break;
524                 case IIO_INTENSITY:
525                         *val = apds9960_als_gain_map[data->als_gain];
526                         ret = IIO_VAL_INT;
527                         break;
528                 default:
529                         ret = -EINVAL;
530                 }
531                 mutex_unlock(&data->lock);
532                 break;
533         }
534
535         return ret;
536 };
537
538 static int apds9960_write_raw(struct iio_dev *indio_dev,
539                              struct iio_chan_spec const *chan,
540                              int val, int val2, long mask)
541 {
542         struct apds9960_data *data = iio_priv(indio_dev);
543
544         switch (mask) {
545         case IIO_CHAN_INFO_INT_TIME:
546                 /* RGB + ALS sensors only have int time */
547                 switch (chan->type) {
548                 case IIO_INTENSITY:
549                         if (val != 0)
550                                 return -EINVAL;
551                         return apds9960_set_it_time(data, val2);
552                 default:
553                         return -EINVAL;
554                 }
555         case IIO_CHAN_INFO_SCALE:
556                 if (val2 != 0)
557                         return -EINVAL;
558                 switch (chan->type) {
559                 case IIO_PROXIMITY:
560                         return apds9960_set_pxs_gain(data, val);
561                 case IIO_INTENSITY:
562                         return apds9960_set_als_gain(data, val);
563                 default:
564                         return -EINVAL;
565                 }
566         default:
567                 return -EINVAL;
568         };
569
570         return 0;
571 }
572
573 static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
574                                          enum iio_event_direction dir,
575                                          u8 *reg)
576 {
577         switch (dir) {
578         case IIO_EV_DIR_RISING:
579                 switch (chan->type) {
580                 case IIO_PROXIMITY:
581                         *reg = APDS9960_REG_PIHT;
582                         break;
583                 case IIO_INTENSITY:
584                         *reg = APDS9960_REG_AIHTL;
585                         break;
586                 default:
587                         return -EINVAL;
588                 }
589                 break;
590         case IIO_EV_DIR_FALLING:
591                 switch (chan->type) {
592                 case IIO_PROXIMITY:
593                         *reg = APDS9960_REG_PILT;
594                         break;
595                 case IIO_INTENSITY:
596                         *reg = APDS9960_REG_AILTL;
597                         break;
598                 default:
599                         return -EINVAL;
600                 }
601                 break;
602         default:
603                 return -EINVAL;
604         }
605
606         return 0;
607 }
608
609 static int apds9960_read_event(struct iio_dev *indio_dev,
610                                const struct iio_chan_spec *chan,
611                                enum iio_event_type type,
612                                enum iio_event_direction dir,
613                                enum iio_event_info info,
614                                int *val, int *val2)
615 {
616         u8 reg;
617         __le16 buf;
618         int ret = 0;
619         struct apds9960_data *data = iio_priv(indio_dev);
620
621         if (info != IIO_EV_INFO_VALUE)
622                 return -EINVAL;
623
624         ret = apds9960_get_thres_reg(chan, dir, &reg);
625         if (ret < 0)
626                 return ret;
627
628         if (chan->type == IIO_PROXIMITY) {
629                 ret = regmap_read(data->regmap, reg, val);
630                 if (ret < 0)
631                         return ret;
632         } else if (chan->type == IIO_INTENSITY) {
633                 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
634                 if (ret < 0)
635                         return ret;
636                 *val = le16_to_cpu(buf);
637         } else
638                 return -EINVAL;
639
640         *val2 = 0;
641
642         return IIO_VAL_INT;
643 }
644
645 static int apds9960_write_event(struct iio_dev *indio_dev,
646                                 const struct iio_chan_spec *chan,
647                                 enum iio_event_type type,
648                                 enum iio_event_direction dir,
649                                 enum iio_event_info info,
650                                 int val, int val2)
651 {
652         u8 reg;
653         __le16 buf;
654         int ret = 0;
655         struct apds9960_data *data = iio_priv(indio_dev);
656
657         if (info != IIO_EV_INFO_VALUE)
658                 return -EINVAL;
659
660         ret = apds9960_get_thres_reg(chan, dir, &reg);
661         if (ret < 0)
662                 return ret;
663
664         if (chan->type == IIO_PROXIMITY) {
665                 if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL)
666                         return -EINVAL;
667                 ret = regmap_write(data->regmap, reg, val);
668                 if (ret < 0)
669                         return ret;
670         } else if (chan->type == IIO_INTENSITY) {
671                 if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL)
672                         return -EINVAL;
673                 buf = cpu_to_le16(val);
674                 ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
675                 if (ret < 0)
676                         return ret;
677         } else
678                 return -EINVAL;
679
680         return 0;
681 }
682
683 static int apds9960_read_event_config(struct iio_dev *indio_dev,
684                                       const struct iio_chan_spec *chan,
685                                       enum iio_event_type type,
686                                       enum iio_event_direction dir)
687 {
688         struct apds9960_data *data = iio_priv(indio_dev);
689
690         switch (chan->type) {
691         case IIO_PROXIMITY:
692                 return data->pxs_int;
693         case IIO_INTENSITY:
694                 return data->als_int;
695         default:
696                 return -EINVAL;
697         }
698
699         return 0;
700 }
701
702 static int apds9960_write_event_config(struct iio_dev *indio_dev,
703                                        const struct iio_chan_spec *chan,
704                                        enum iio_event_type type,
705                                        enum iio_event_direction dir,
706                                        int state)
707 {
708         struct apds9960_data *data = iio_priv(indio_dev);
709         int ret;
710
711         state = !!state;
712
713         switch (chan->type) {
714         case IIO_PROXIMITY:
715                 if (data->pxs_int == state)
716                         return -EINVAL;
717
718                 ret = regmap_field_write(data->reg_int_pxs, state);
719                 if (ret)
720                         return ret;
721                 data->pxs_int = state;
722                 apds9960_set_power_state(data, state);
723                 break;
724         case IIO_INTENSITY:
725                 if (data->als_int == state)
726                         return -EINVAL;
727
728                 ret = regmap_field_write(data->reg_int_als, state);
729                 if (ret)
730                         return ret;
731                 data->als_int = state;
732                 apds9960_set_power_state(data, state);
733                 break;
734         default:
735                 return -EINVAL;
736         }
737
738         return 0;
739 }
740
741 static const struct iio_info apds9960_info = {
742         .driver_module = THIS_MODULE,
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         }
781
782 err_read:
783         data->gesture_mode_running = 0;
784         mutex_unlock(&data->lock);
785 }
786
787 static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
788 {
789         struct iio_dev *indio_dev = private;
790         struct apds9960_data *data = iio_priv(indio_dev);
791         int ret, status;
792
793         ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status);
794         if (ret < 0) {
795                 dev_err(&data->client->dev, "irq status reg read failed\n");
796                 return IRQ_HANDLED;
797         }
798
799         if ((status & APDS9960_REG_STATUS_ALS_INT) && data->als_int) {
800                 iio_push_event(indio_dev,
801                                IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
802                                                     IIO_EV_TYPE_THRESH,
803                                                     IIO_EV_DIR_EITHER),
804                                iio_get_time_ns());
805                 regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1);
806         }
807
808         if ((status & APDS9960_REG_STATUS_PS_INT) && data->pxs_int) {
809                 iio_push_event(indio_dev,
810                                IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
811                                                     IIO_EV_TYPE_THRESH,
812                                                     IIO_EV_DIR_EITHER),
813                                iio_get_time_ns());
814                 regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1);
815         }
816
817         if (status & APDS9960_REG_STATUS_GINT)
818                 apds9960_read_gesture_fifo(data);
819
820         return IRQ_HANDLED;
821 }
822
823 static int apds9960_set_powermode(struct apds9960_data *data, bool state)
824 {
825         return regmap_update_bits(data->regmap, APDS9960_REG_ENABLE, 1, state);
826 }
827
828 static int apds9960_buffer_postenable(struct iio_dev *indio_dev)
829 {
830         struct apds9960_data *data = iio_priv(indio_dev);
831         int ret;
832
833         ret = regmap_field_write(data->reg_int_ges, 1);
834         if (ret)
835                 return ret;
836
837         ret = regmap_field_write(data->reg_enable_ges, 1);
838         if (ret)
839                 return ret;
840
841         pm_runtime_get_sync(&data->client->dev);
842
843         return 0;
844 }
845
846 static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
847 {
848         struct apds9960_data *data = iio_priv(indio_dev);
849         int ret;
850
851         ret = regmap_field_write(data->reg_enable_ges, 0);
852         if (ret)
853                 return ret;
854
855         ret = regmap_field_write(data->reg_int_ges, 0);
856         if (ret)
857                 return ret;
858
859         pm_runtime_put_autosuspend(&data->client->dev);
860
861         return 0;
862 }
863
864 static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops = {
865         .postenable = apds9960_buffer_postenable,
866         .predisable = apds9960_buffer_predisable,
867 };
868
869 static int apds9960_regfield_init(struct apds9960_data *data)
870 {
871         struct device *dev = &data->client->dev;
872         struct regmap *regmap = data->regmap;
873
874         data->reg_int_als = devm_regmap_field_alloc(dev, regmap,
875                                                 apds9960_reg_field_int_als);
876         if (IS_ERR(data->reg_int_als)) {
877                 dev_err(dev, "INT ALS reg field init failed\n");
878                 return PTR_ERR(data->reg_int_als);
879         }
880
881         data->reg_int_ges = devm_regmap_field_alloc(dev, regmap,
882                                                 apds9960_reg_field_int_ges);
883         if (IS_ERR(data->reg_int_ges)) {
884                 dev_err(dev, "INT gesture reg field init failed\n");
885                 return PTR_ERR(data->reg_int_ges);
886         }
887
888         data->reg_int_pxs = devm_regmap_field_alloc(dev, regmap,
889                                                 apds9960_reg_field_int_pxs);
890         if (IS_ERR(data->reg_int_pxs)) {
891                 dev_err(dev, "INT pxs reg field init failed\n");
892                 return PTR_ERR(data->reg_int_pxs);
893         }
894
895         data->reg_enable_als = devm_regmap_field_alloc(dev, regmap,
896                                                 apds9960_reg_field_enable_als);
897         if (IS_ERR(data->reg_enable_als)) {
898                 dev_err(dev, "Enable ALS reg field init failed\n");
899                 return PTR_ERR(data->reg_enable_als);
900         }
901
902         data->reg_enable_ges = devm_regmap_field_alloc(dev, regmap,
903                                                 apds9960_reg_field_enable_ges);
904         if (IS_ERR(data->reg_enable_ges)) {
905                 dev_err(dev, "Enable gesture reg field init failed\n");
906                 return PTR_ERR(data->reg_enable_ges);
907         }
908
909         data->reg_enable_pxs = devm_regmap_field_alloc(dev, regmap,
910                                                 apds9960_reg_field_enable_pxs);
911         if (IS_ERR(data->reg_enable_pxs)) {
912                 dev_err(dev, "Enable PXS reg field init failed\n");
913                 return PTR_ERR(data->reg_enable_pxs);
914         }
915
916         return 0;
917 }
918
919 static int apds9960_chip_init(struct apds9960_data *data)
920 {
921         int ret;
922
923         /* Default IT for ALS of 28 ms */
924         ret = apds9960_set_it_time(data, 28000);
925         if (ret)
926                 return ret;
927
928         /* Ensure gesture interrupt is OFF */
929         ret = regmap_field_write(data->reg_int_ges, 0);
930         if (ret)
931                 return ret;
932
933         /* Disable gesture sensor, since polling is useless from user-space */
934         ret = regmap_field_write(data->reg_enable_ges, 0);
935         if (ret)
936                 return ret;
937
938         /* Ensure proximity interrupt is OFF */
939         ret = regmap_field_write(data->reg_int_pxs, 0);
940         if (ret)
941                 return ret;
942
943         /* Enable proximity sensor for polling */
944         ret = regmap_field_write(data->reg_enable_pxs, 1);
945         if (ret)
946                 return ret;
947
948         /* Ensure ALS interrupt is OFF */
949         ret = regmap_field_write(data->reg_int_als, 0);
950         if (ret)
951                 return ret;
952
953         /* Enable ALS sensor for polling */
954         ret = regmap_field_write(data->reg_enable_als, 1);
955         if (ret)
956                 return ret;
957         /*
958          * When enabled trigger an interrupt after 3 readings
959          * outside threshold for ALS + PXS
960          */
961         ret = regmap_write(data->regmap, APDS9960_REG_PERS,
962                            APDS9960_DEFAULT_PERS);
963         if (ret)
964                 return ret;
965
966         /*
967          * Wait for 4 event outside gesture threshold to prevent interrupt
968          * flooding.
969          */
970         ret = regmap_update_bits(data->regmap, APDS9960_REG_GCONF_1,
971                         APDS9960_REG_GCONF_1_GFIFO_THRES_MASK,
972                         BIT(0) << APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT);
973         if (ret)
974                 return ret;
975
976         /* Default ENTER and EXIT thresholds for the GESTURE engine. */
977         ret = regmap_write(data->regmap, APDS9960_REG_GPENTH,
978                            APDS9960_DEFAULT_GPENTH);
979         if (ret)
980                 return ret;
981
982         ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
983                            APDS9960_DEFAULT_GEXTH);
984         if (ret)
985                 return ret;
986
987         return apds9960_set_powermode(data, 1);
988 }
989
990 static int apds9960_probe(struct i2c_client *client,
991                           const struct i2c_device_id *id)
992 {
993         struct apds9960_data *data;
994         struct iio_buffer *buffer;
995         struct iio_dev *indio_dev;
996         int ret;
997
998         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
999         if (!indio_dev)
1000                 return -ENOMEM;
1001
1002         buffer = devm_iio_kfifo_allocate(&client->dev);
1003         if (!buffer)
1004                 return -ENOMEM;
1005
1006         iio_device_attach_buffer(indio_dev, buffer);
1007
1008         indio_dev->dev.parent = &client->dev;
1009         indio_dev->info = &apds9960_info;
1010         indio_dev->name = APDS9960_DRV_NAME;
1011         indio_dev->channels = apds9960_channels;
1012         indio_dev->num_channels = ARRAY_SIZE(apds9960_channels);
1013         indio_dev->available_scan_masks = apds9960_scan_masks;
1014         indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
1015         indio_dev->setup_ops = &apds9960_buffer_setup_ops;
1016
1017         data = iio_priv(indio_dev);
1018         i2c_set_clientdata(client, indio_dev);
1019
1020         data->regmap = devm_regmap_init_i2c(client, &apds9960_regmap_config);
1021         if (IS_ERR(data->regmap)) {
1022                 dev_err(&client->dev, "regmap initialization failed.\n");
1023                 return PTR_ERR(data->regmap);
1024         }
1025
1026         data->client = client;
1027         data->indio_dev = indio_dev;
1028         mutex_init(&data->lock);
1029
1030         ret = pm_runtime_set_active(&client->dev);
1031         if (ret)
1032                 goto error_power_down;
1033
1034         pm_runtime_enable(&client->dev);
1035         pm_runtime_set_autosuspend_delay(&client->dev, 5000);
1036         pm_runtime_use_autosuspend(&client->dev);
1037
1038         apds9960_set_power_state(data, true);
1039
1040         ret = apds9960_regfield_init(data);
1041         if (ret)
1042                 goto error_power_down;
1043
1044         ret = apds9960_chip_init(data);
1045         if (ret)
1046                 goto error_power_down;
1047
1048         if (client->irq <= 0) {
1049                 dev_err(&client->dev, "no valid irq defined\n");
1050                 ret = -EINVAL;
1051                 goto error_power_down;
1052         }
1053         ret = devm_request_threaded_irq(&client->dev, client->irq,
1054                                         NULL, apds9960_interrupt_handler,
1055                                         IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1056                                         "apds9960_event",
1057                                         indio_dev);
1058         if (ret) {
1059                 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
1060                 goto error_power_down;
1061         }
1062
1063         ret = iio_device_register(indio_dev);
1064         if (ret)
1065                 goto error_power_down;
1066
1067         apds9960_set_power_state(data, false);
1068
1069         return 0;
1070
1071 error_power_down:
1072         apds9960_set_power_state(data, false);
1073
1074         return ret;
1075 }
1076
1077 static int apds9960_remove(struct i2c_client *client)
1078 {
1079         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1080         struct apds9960_data *data = iio_priv(indio_dev);
1081
1082         iio_device_unregister(indio_dev);
1083         pm_runtime_disable(&client->dev);
1084         pm_runtime_set_suspended(&client->dev);
1085         apds9960_set_powermode(data, 0);
1086
1087         return 0;
1088 }
1089
1090 #ifdef CONFIG_PM
1091 static int apds9960_runtime_suspend(struct device *dev)
1092 {
1093         struct apds9960_data *data =
1094                         iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1095
1096         return apds9960_set_powermode(data, 0);
1097 }
1098
1099 static int apds9960_runtime_resume(struct device *dev)
1100 {
1101         struct apds9960_data *data =
1102                         iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1103
1104         return apds9960_set_powermode(data, 1);
1105 }
1106 #endif
1107
1108 static const struct dev_pm_ops apds9960_pm_ops = {
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 struct i2c_driver apds9960_driver = {
1120         .driver = {
1121                 .name   = APDS9960_DRV_NAME,
1122                 .pm     = &apds9960_pm_ops,
1123         },
1124         .probe          = apds9960_probe,
1125         .remove         = apds9960_remove,
1126         .id_table       = apds9960_id,
1127 };
1128 module_i2c_driver(apds9960_driver);
1129
1130 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
1131 MODULE_DESCRIPTION("ADPS9960 Gesture/RGB/ALS/Proximity sensor");
1132 MODULE_LICENSE("GPL");