Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / light / isl29018.c
1 /*
2  * A iio driver for the light sensor ISL 29018/29023/29035.
3  *
4  * IIO driver for monitoring ambient light intensity in luxi, proximity
5  * sensing and infrared sensing.
6  *
7  * Copyright (c) 2010, NVIDIA Corporation.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/i2c.h>
26 #include <linux/err.h>
27 #include <linux/mutex.h>
28 #include <linux/delay.h>
29 #include <linux/regmap.h>
30 #include <linux/slab.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/sysfs.h>
33 #include <linux/acpi.h>
34
35 #define CONVERSION_TIME_MS              100
36
37 #define ISL29018_REG_ADD_COMMAND1       0x00
38 #define COMMMAND1_OPMODE_SHIFT          5
39 #define COMMMAND1_OPMODE_MASK           (7 << COMMMAND1_OPMODE_SHIFT)
40 #define COMMMAND1_OPMODE_POWER_DOWN     0
41 #define COMMMAND1_OPMODE_ALS_ONCE       1
42 #define COMMMAND1_OPMODE_IR_ONCE        2
43 #define COMMMAND1_OPMODE_PROX_ONCE      3
44
45 #define ISL29018_REG_ADD_COMMANDII      0x01
46 #define COMMANDII_RESOLUTION_SHIFT      2
47 #define COMMANDII_RESOLUTION_MASK       (0x3 << COMMANDII_RESOLUTION_SHIFT)
48
49 #define COMMANDII_RANGE_SHIFT           0
50 #define COMMANDII_RANGE_MASK            (0x3 << COMMANDII_RANGE_SHIFT)
51
52 #define COMMANDII_SCHEME_SHIFT          7
53 #define COMMANDII_SCHEME_MASK           (0x1 << COMMANDII_SCHEME_SHIFT)
54
55 #define ISL29018_REG_ADD_DATA_LSB       0x02
56 #define ISL29018_REG_ADD_DATA_MSB       0x03
57
58 #define ISL29018_REG_TEST               0x08
59 #define ISL29018_TEST_SHIFT             0
60 #define ISL29018_TEST_MASK              (0xFF << ISL29018_TEST_SHIFT)
61
62 #define ISL29035_REG_DEVICE_ID          0x0F
63 #define ISL29035_DEVICE_ID_SHIFT        0x03
64 #define ISL29035_DEVICE_ID_MASK         (0x7 << ISL29035_DEVICE_ID_SHIFT)
65 #define ISL29035_DEVICE_ID              0x5
66 #define ISL29035_BOUT_SHIFT             0x07
67 #define ISL29035_BOUT_MASK              (0x01 << ISL29035_BOUT_SHIFT)
68
69 #define ISL29018_INT_TIME_AVAIL         "0.090000 0.005630 0.000351 0.000021"
70 #define ISL29023_INT_TIME_AVAIL         "0.090000 0.005600 0.000352 0.000022"
71 #define ISL29035_INT_TIME_AVAIL         "0.105000 0.006500 0.000410 0.000025"
72
73 static const char * const int_time_avail[] = {
74         ISL29018_INT_TIME_AVAIL,
75         ISL29023_INT_TIME_AVAIL,
76         ISL29035_INT_TIME_AVAIL,
77 };
78
79 enum isl29018_int_time {
80         ISL29018_INT_TIME_16,
81         ISL29018_INT_TIME_12,
82         ISL29018_INT_TIME_8,
83         ISL29018_INT_TIME_4,
84 };
85
86 static const unsigned int isl29018_int_utimes[3][4] = {
87         {90000, 5630, 351, 21},
88         {90000, 5600, 352, 22},
89         {105000, 6500, 410, 25},
90 };
91
92 static const struct isl29018_scale {
93         unsigned int scale;
94         unsigned int uscale;
95 } isl29018_scales[4][4] = {
96         { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
97         { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
98         { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
99         { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
100 };
101
102 struct isl29018_chip {
103         struct device           *dev;
104         struct regmap           *regmap;
105         struct mutex            lock;
106         int                     type;
107         unsigned int            calibscale;
108         unsigned int            ucalibscale;
109         unsigned int            int_time;
110         struct isl29018_scale   scale;
111         int                     prox_scheme;
112         bool                    suspended;
113 };
114
115 static int isl29018_set_integration_time(struct isl29018_chip *chip,
116                                          unsigned int utime)
117 {
118         int i, ret;
119         unsigned int int_time, new_int_time;
120         struct isl29018_scale new_scale;
121
122         for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
123                 if (utime == isl29018_int_utimes[chip->type][i]) {
124                         new_int_time = i;
125                         new_scale = isl29018_scales[new_int_time][0];
126                         break;
127                 }
128         }
129
130         if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
131                 return -EINVAL;
132
133         ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
134                                  COMMANDII_RESOLUTION_MASK,
135                                  i << COMMANDII_RESOLUTION_SHIFT);
136         if (ret < 0)
137                 return ret;
138
139         /* keep the same range when integration time changes */
140         int_time = chip->int_time;
141         for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
142                 if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
143                     chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
144                         chip->scale = isl29018_scales[new_int_time][i];
145                         break;
146                 }
147         }
148         chip->int_time = new_int_time;
149
150         return 0;
151 }
152
153 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
154 {
155         int i, ret;
156         struct isl29018_scale new_scale;
157
158         for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
159                 if (scale == isl29018_scales[chip->int_time][i].scale &&
160                     uscale == isl29018_scales[chip->int_time][i].uscale) {
161                         new_scale = isl29018_scales[chip->int_time][i];
162                         break;
163                 }
164         }
165
166         if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
167                 return -EINVAL;
168
169         ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
170                                  COMMANDII_RANGE_MASK,
171                                  i << COMMANDII_RANGE_SHIFT);
172         if (ret < 0)
173                 return ret;
174
175         chip->scale = new_scale;
176
177         return 0;
178 }
179
180 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
181 {
182         int status;
183         unsigned int lsb;
184         unsigned int msb;
185
186         /* Set mode */
187         status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
188                         mode << COMMMAND1_OPMODE_SHIFT);
189         if (status) {
190                 dev_err(chip->dev,
191                         "Error in setting operating mode err %d\n", status);
192                 return status;
193         }
194         msleep(CONVERSION_TIME_MS);
195         status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
196         if (status < 0) {
197                 dev_err(chip->dev,
198                         "Error in reading LSB DATA with err %d\n", status);
199                 return status;
200         }
201
202         status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
203         if (status < 0) {
204                 dev_err(chip->dev,
205                         "Error in reading MSB DATA with error %d\n", status);
206                 return status;
207         }
208         dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
209
210         return (msb << 8) | lsb;
211 }
212
213 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
214 {
215         int lux_data;
216         unsigned int data_x_range;
217
218         lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
219
220         if (lux_data < 0)
221                 return lux_data;
222
223         data_x_range = lux_data * chip->scale.scale +
224                        lux_data * chip->scale.uscale / 1000000;
225         *lux = data_x_range * chip->calibscale +
226                data_x_range * chip->ucalibscale / 1000000;
227
228         return 0;
229 }
230
231 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
232 {
233         int ir_data;
234
235         ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
236
237         if (ir_data < 0)
238                 return ir_data;
239
240         *ir = ir_data;
241
242         return 0;
243 }
244
245 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
246                 int *near_ir)
247 {
248         int status;
249         int prox_data = -1;
250         int ir_data = -1;
251
252         /* Do proximity sensing with required scheme */
253         status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
254                         COMMANDII_SCHEME_MASK,
255                         scheme << COMMANDII_SCHEME_SHIFT);
256         if (status) {
257                 dev_err(chip->dev, "Error in setting operating mode\n");
258                 return status;
259         }
260
261         prox_data = isl29018_read_sensor_input(chip,
262                                         COMMMAND1_OPMODE_PROX_ONCE);
263         if (prox_data < 0)
264                 return prox_data;
265
266         if (scheme == 1) {
267                 *near_ir = prox_data;
268                 return 0;
269         }
270
271         ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
272
273         if (ir_data < 0)
274                 return ir_data;
275
276         if (prox_data >= ir_data)
277                 *near_ir = prox_data - ir_data;
278         else
279                 *near_ir = 0;
280
281         return 0;
282 }
283
284 static ssize_t show_scale_available(struct device *dev,
285                         struct device_attribute *attr, char *buf)
286 {
287         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
288         struct isl29018_chip *chip = iio_priv(indio_dev);
289         int i, len = 0;
290
291         for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
292                 len += sprintf(buf + len, "%d.%06d ",
293                                isl29018_scales[chip->int_time][i].scale,
294                                isl29018_scales[chip->int_time][i].uscale);
295
296         buf[len - 1] = '\n';
297
298         return len;
299 }
300
301 static ssize_t show_int_time_available(struct device *dev,
302                         struct device_attribute *attr, char *buf)
303 {
304         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
305         struct isl29018_chip *chip = iio_priv(indio_dev);
306         int i, len = 0;
307
308         for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
309                 len += sprintf(buf + len, "0.%06d ",
310                                isl29018_int_utimes[chip->type][i]);
311
312         buf[len - 1] = '\n';
313
314         return len;
315 }
316
317 /* proximity scheme */
318 static ssize_t show_prox_infrared_suppression(struct device *dev,
319                         struct device_attribute *attr, char *buf)
320 {
321         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
322         struct isl29018_chip *chip = iio_priv(indio_dev);
323
324         /* return the "proximity scheme" i.e. if the chip does on chip
325         infrared suppression (1 means perform on chip suppression) */
326         return sprintf(buf, "%d\n", chip->prox_scheme);
327 }
328
329 static ssize_t store_prox_infrared_suppression(struct device *dev,
330                 struct device_attribute *attr, const char *buf, size_t count)
331 {
332         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
333         struct isl29018_chip *chip = iio_priv(indio_dev);
334         int val;
335
336         if (kstrtoint(buf, 10, &val))
337                 return -EINVAL;
338         if (!(val == 0 || val == 1)) {
339                 dev_err(dev, "The mode is not supported\n");
340                 return -EINVAL;
341         }
342
343         /* get the  "proximity scheme" i.e. if the chip does on chip
344         infrared suppression (1 means perform on chip suppression) */
345         mutex_lock(&chip->lock);
346         chip->prox_scheme = val;
347         mutex_unlock(&chip->lock);
348
349         return count;
350 }
351
352 /* Channel IO */
353 static int isl29018_write_raw(struct iio_dev *indio_dev,
354                               struct iio_chan_spec const *chan,
355                               int val,
356                               int val2,
357                               long mask)
358 {
359         struct isl29018_chip *chip = iio_priv(indio_dev);
360         int ret = -EINVAL;
361
362         mutex_lock(&chip->lock);
363         switch (mask) {
364         case IIO_CHAN_INFO_CALIBSCALE:
365                 if (chan->type == IIO_LIGHT) {
366                         chip->calibscale = val;
367                         chip->ucalibscale = val2;
368                         ret = 0;
369                 }
370                 break;
371         case IIO_CHAN_INFO_INT_TIME:
372                 if (chan->type == IIO_LIGHT) {
373                         if (val != 0) {
374                                 mutex_unlock(&chip->lock);
375                                 return -EINVAL;
376                         }
377                         ret = isl29018_set_integration_time(chip, val2);
378                 }
379                 break;
380         case IIO_CHAN_INFO_SCALE:
381                 if (chan->type == IIO_LIGHT)
382                         ret = isl29018_set_scale(chip, val, val2);
383                 break;
384         default:
385                 break;
386         }
387         mutex_unlock(&chip->lock);
388
389         return ret;
390 }
391
392 static int isl29018_read_raw(struct iio_dev *indio_dev,
393                              struct iio_chan_spec const *chan,
394                              int *val,
395                              int *val2,
396                              long mask)
397 {
398         int ret = -EINVAL;
399         struct isl29018_chip *chip = iio_priv(indio_dev);
400
401         mutex_lock(&chip->lock);
402         if (chip->suspended) {
403                 mutex_unlock(&chip->lock);
404                 return -EBUSY;
405         }
406         switch (mask) {
407         case IIO_CHAN_INFO_RAW:
408         case IIO_CHAN_INFO_PROCESSED:
409                 switch (chan->type) {
410                 case IIO_LIGHT:
411                         ret = isl29018_read_lux(chip, val);
412                         break;
413                 case IIO_INTENSITY:
414                         ret = isl29018_read_ir(chip, val);
415                         break;
416                 case IIO_PROXIMITY:
417                         ret = isl29018_read_proximity_ir(chip,
418                                         chip->prox_scheme, val);
419                         break;
420                 default:
421                         break;
422                 }
423                 if (!ret)
424                         ret = IIO_VAL_INT;
425                 break;
426         case IIO_CHAN_INFO_INT_TIME:
427                 if (chan->type == IIO_LIGHT) {
428                         *val = 0;
429                         *val2 = isl29018_int_utimes[chip->type][chip->int_time];
430                         ret = IIO_VAL_INT_PLUS_MICRO;
431                 }
432                 break;
433         case IIO_CHAN_INFO_SCALE:
434                 if (chan->type == IIO_LIGHT) {
435                         *val = chip->scale.scale;
436                         *val2 = chip->scale.uscale;
437                         ret = IIO_VAL_INT_PLUS_MICRO;
438                 }
439                 break;
440         case IIO_CHAN_INFO_CALIBSCALE:
441                 if (chan->type == IIO_LIGHT) {
442                         *val = chip->calibscale;
443                         *val2 = chip->ucalibscale;
444                         ret = IIO_VAL_INT_PLUS_MICRO;
445                 }
446                 break;
447         default:
448                 break;
449         }
450         mutex_unlock(&chip->lock);
451         return ret;
452 }
453
454 #define ISL29018_LIGHT_CHANNEL {                                        \
455         .type = IIO_LIGHT,                                              \
456         .indexed = 1,                                                   \
457         .channel = 0,                                                   \
458         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |            \
459         BIT(IIO_CHAN_INFO_CALIBSCALE) |                                 \
460         BIT(IIO_CHAN_INFO_SCALE) |                                      \
461         BIT(IIO_CHAN_INFO_INT_TIME),                                    \
462 }
463
464 #define ISL29018_IR_CHANNEL {                                           \
465         .type = IIO_INTENSITY,                                          \
466         .modified = 1,                                                  \
467         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
468         .channel2 = IIO_MOD_LIGHT_IR,                                   \
469 }
470
471 #define ISL29018_PROXIMITY_CHANNEL {                                    \
472         .type = IIO_PROXIMITY,                                          \
473         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
474 }
475
476 static const struct iio_chan_spec isl29018_channels[] = {
477         ISL29018_LIGHT_CHANNEL,
478         ISL29018_IR_CHANNEL,
479         ISL29018_PROXIMITY_CHANNEL,
480 };
481
482 static const struct iio_chan_spec isl29023_channels[] = {
483         ISL29018_LIGHT_CHANNEL,
484         ISL29018_IR_CHANNEL,
485 };
486
487 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, S_IRUGO,
488                        show_int_time_available, NULL, 0);
489 static IIO_DEVICE_ATTR(in_illuminance_scale_available, S_IRUGO,
490                       show_scale_available, NULL, 0);
491 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
492                                         S_IRUGO | S_IWUSR,
493                                         show_prox_infrared_suppression,
494                                         store_prox_infrared_suppression, 0);
495
496 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
497
498 static struct attribute *isl29018_attributes[] = {
499         ISL29018_DEV_ATTR(in_illuminance_scale_available),
500         ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
501         ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
502         NULL
503 };
504
505 static struct attribute *isl29023_attributes[] = {
506         ISL29018_DEV_ATTR(in_illuminance_scale_available),
507         ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
508         NULL
509 };
510
511 static const struct attribute_group isl29018_group = {
512         .attrs = isl29018_attributes,
513 };
514
515 static const struct attribute_group isl29023_group = {
516         .attrs = isl29023_attributes,
517 };
518
519 static int isl29035_detect(struct isl29018_chip *chip)
520 {
521         int status;
522         unsigned int id;
523
524         status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
525         if (status < 0) {
526                 dev_err(chip->dev,
527                         "Error reading ID register with error %d\n",
528                         status);
529                 return status;
530         }
531
532         id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
533
534         if (id != ISL29035_DEVICE_ID)
535                 return -ENODEV;
536
537         /* clear out brownout bit */
538         return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
539                                   ISL29035_BOUT_MASK, 0);
540 }
541
542 enum {
543         isl29018,
544         isl29023,
545         isl29035,
546 };
547
548 static int isl29018_chip_init(struct isl29018_chip *chip)
549 {
550         int status;
551
552         if (chip->type == isl29035) {
553                 status = isl29035_detect(chip);
554                 if (status < 0)
555                         return status;
556         }
557
558         /* Code added per Intersil Application Note 1534:
559          *     When VDD sinks to approximately 1.8V or below, some of
560          * the part's registers may change their state. When VDD
561          * recovers to 2.25V (or greater), the part may thus be in an
562          * unknown mode of operation. The user can return the part to
563          * a known mode of operation either by (a) setting VDD = 0V for
564          * 1 second or more and then powering back up with a slew rate
565          * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
566          * conversions, clear the test registers, and then rewrite all
567          * registers to the desired values.
568          * ...
569          * FOR ISL29011, ISL29018, ISL29021, ISL29023
570          * 1. Write 0x00 to register 0x08 (TEST)
571          * 2. Write 0x00 to register 0x00 (CMD1)
572          * 3. Rewrite all registers to the desired values
573          *
574          * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
575          * the same thing EXCEPT the data sheet asks for a 1ms delay after
576          * writing the CMD1 register.
577          */
578         status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
579         if (status < 0) {
580                 dev_err(chip->dev, "Failed to clear isl29018 TEST reg.(%d)\n",
581                         status);
582                 return status;
583         }
584
585         /* See Intersil AN1534 comments above.
586          * "Operating Mode" (COMMAND1) register is reprogrammed when
587          * data is read from the device.
588          */
589         status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
590         if (status < 0) {
591                 dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
592                         status);
593                 return status;
594         }
595
596         usleep_range(1000, 2000);       /* per data sheet, page 10 */
597
598         /* set defaults */
599         status = isl29018_set_scale(chip, chip->scale.scale,
600                                     chip->scale.uscale);
601         if (status < 0) {
602                 dev_err(chip->dev, "Init of isl29018 fails\n");
603                 return status;
604         }
605
606         status = isl29018_set_integration_time(chip,
607                         isl29018_int_utimes[chip->type][chip->int_time]);
608         if (status < 0) {
609                 dev_err(chip->dev, "Init of isl29018 fails\n");
610                 return status;
611         }
612
613         return 0;
614 }
615
616 static const struct iio_info isl29018_info = {
617         .attrs = &isl29018_group,
618         .driver_module = THIS_MODULE,
619         .read_raw = &isl29018_read_raw,
620         .write_raw = &isl29018_write_raw,
621 };
622
623 static const struct iio_info isl29023_info = {
624         .attrs = &isl29023_group,
625         .driver_module = THIS_MODULE,
626         .read_raw = &isl29018_read_raw,
627         .write_raw = &isl29018_write_raw,
628 };
629
630 static bool is_volatile_reg(struct device *dev, unsigned int reg)
631 {
632         switch (reg) {
633         case ISL29018_REG_ADD_DATA_LSB:
634         case ISL29018_REG_ADD_DATA_MSB:
635         case ISL29018_REG_ADD_COMMAND1:
636         case ISL29018_REG_TEST:
637         case ISL29035_REG_DEVICE_ID:
638                 return true;
639         default:
640                 return false;
641         }
642 }
643
644 /*
645  * isl29018_regmap_config: regmap configuration.
646  * Use RBTREE mechanism for caching.
647  */
648 static const struct regmap_config isl29018_regmap_config = {
649         .reg_bits = 8,
650         .val_bits = 8,
651         .volatile_reg = is_volatile_reg,
652         .max_register = ISL29018_REG_TEST,
653         .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
654         .cache_type = REGCACHE_RBTREE,
655 };
656
657 /* isl29035_regmap_config: regmap configuration for ISL29035 */
658 static const struct regmap_config isl29035_regmap_config = {
659         .reg_bits = 8,
660         .val_bits = 8,
661         .volatile_reg = is_volatile_reg,
662         .max_register = ISL29035_REG_DEVICE_ID,
663         .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
664         .cache_type = REGCACHE_RBTREE,
665 };
666
667 struct chip_info {
668         const struct iio_chan_spec *channels;
669         int num_channels;
670         const struct iio_info *indio_info;
671         const struct regmap_config *regmap_cfg;
672 };
673
674 static const struct chip_info chip_info_tbl[] = {
675         [isl29018] = {
676                 .channels = isl29018_channels,
677                 .num_channels = ARRAY_SIZE(isl29018_channels),
678                 .indio_info = &isl29018_info,
679                 .regmap_cfg = &isl29018_regmap_config,
680         },
681         [isl29023] = {
682                 .channels = isl29023_channels,
683                 .num_channels = ARRAY_SIZE(isl29023_channels),
684                 .indio_info = &isl29023_info,
685                 .regmap_cfg = &isl29018_regmap_config,
686         },
687         [isl29035] = {
688                 .channels = isl29023_channels,
689                 .num_channels = ARRAY_SIZE(isl29023_channels),
690                 .indio_info = &isl29023_info,
691                 .regmap_cfg = &isl29035_regmap_config,
692         },
693 };
694
695 static const char *isl29018_match_acpi_device(struct device *dev, int *data)
696 {
697         const struct acpi_device_id *id;
698
699         id = acpi_match_device(dev->driver->acpi_match_table, dev);
700
701         if (!id)
702                 return NULL;
703
704         *data = (int) id->driver_data;
705
706         return dev_name(dev);
707 }
708
709 static int isl29018_probe(struct i2c_client *client,
710                          const struct i2c_device_id *id)
711 {
712         struct isl29018_chip *chip;
713         struct iio_dev *indio_dev;
714         int err;
715         const char *name = NULL;
716         int dev_id = 0;
717
718         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
719         if (indio_dev == NULL) {
720                 dev_err(&client->dev, "iio allocation fails\n");
721                 return -ENOMEM;
722         }
723         chip = iio_priv(indio_dev);
724
725         i2c_set_clientdata(client, indio_dev);
726         chip->dev = &client->dev;
727
728         if (id) {
729                 name = id->name;
730                 dev_id = id->driver_data;
731         }
732
733         if (ACPI_HANDLE(&client->dev))
734                 name = isl29018_match_acpi_device(&client->dev, &dev_id);
735
736         mutex_init(&chip->lock);
737
738         chip->type = dev_id;
739         chip->calibscale = 1;
740         chip->ucalibscale = 0;
741         chip->int_time = ISL29018_INT_TIME_16;
742         chip->scale = isl29018_scales[chip->int_time][0];
743         chip->suspended = false;
744
745         chip->regmap = devm_regmap_init_i2c(client,
746                                 chip_info_tbl[dev_id].regmap_cfg);
747         if (IS_ERR(chip->regmap)) {
748                 err = PTR_ERR(chip->regmap);
749                 dev_err(chip->dev, "regmap initialization failed: %d\n", err);
750                 return err;
751         }
752
753         err = isl29018_chip_init(chip);
754         if (err)
755                 return err;
756
757         indio_dev->info = chip_info_tbl[dev_id].indio_info;
758         indio_dev->channels = chip_info_tbl[dev_id].channels;
759         indio_dev->num_channels = chip_info_tbl[dev_id].num_channels;
760         indio_dev->name = name;
761         indio_dev->dev.parent = &client->dev;
762         indio_dev->modes = INDIO_DIRECT_MODE;
763         err = devm_iio_device_register(&client->dev, indio_dev);
764         if (err) {
765                 dev_err(&client->dev, "iio registration fails\n");
766                 return err;
767         }
768
769         return 0;
770 }
771
772 #ifdef CONFIG_PM_SLEEP
773 static int isl29018_suspend(struct device *dev)
774 {
775         struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
776
777         mutex_lock(&chip->lock);
778
779         /* Since this driver uses only polling commands, we are by default in
780          * auto shutdown (ie, power-down) mode.
781          * So we do not have much to do here.
782          */
783         chip->suspended = true;
784
785         mutex_unlock(&chip->lock);
786         return 0;
787 }
788
789 static int isl29018_resume(struct device *dev)
790 {
791         struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
792         int err;
793
794         mutex_lock(&chip->lock);
795
796         err = isl29018_chip_init(chip);
797         if (!err)
798                 chip->suspended = false;
799
800         mutex_unlock(&chip->lock);
801         return err;
802 }
803
804 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
805 #define ISL29018_PM_OPS (&isl29018_pm_ops)
806 #else
807 #define ISL29018_PM_OPS NULL
808 #endif
809
810 static const struct acpi_device_id isl29018_acpi_match[] = {
811         {"ISL29018", isl29018},
812         {"ISL29023", isl29023},
813         {"ISL29035", isl29035},
814         {},
815 };
816 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
817
818 static const struct i2c_device_id isl29018_id[] = {
819         {"isl29018", isl29018},
820         {"isl29023", isl29023},
821         {"isl29035", isl29035},
822         {}
823 };
824
825 MODULE_DEVICE_TABLE(i2c, isl29018_id);
826
827 static const struct of_device_id isl29018_of_match[] = {
828         { .compatible = "isil,isl29018", },
829         { .compatible = "isil,isl29023", },
830         { .compatible = "isil,isl29035", },
831         { },
832 };
833 MODULE_DEVICE_TABLE(of, isl29018_of_match);
834
835 static struct i2c_driver isl29018_driver = {
836         .class  = I2C_CLASS_HWMON,
837         .driver  = {
838                         .name = "isl29018",
839                         .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
840                         .pm = ISL29018_PM_OPS,
841                         .of_match_table = isl29018_of_match,
842                     },
843         .probe   = isl29018_probe,
844         .id_table = isl29018_id,
845 };
846 module_i2c_driver(isl29018_driver);
847
848 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
849 MODULE_LICENSE("GPL");