b50f126f3908ed0c35f02d4e5e152bc1f685a5ee
[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
34 #define CONVERSION_TIME_MS              100
35
36 #define ISL29018_REG_ADD_COMMAND1       0x00
37 #define COMMMAND1_OPMODE_SHIFT          5
38 #define COMMMAND1_OPMODE_MASK           (7 << COMMMAND1_OPMODE_SHIFT)
39 #define COMMMAND1_OPMODE_POWER_DOWN     0
40 #define COMMMAND1_OPMODE_ALS_ONCE       1
41 #define COMMMAND1_OPMODE_IR_ONCE        2
42 #define COMMMAND1_OPMODE_PROX_ONCE      3
43
44 #define ISL29018_REG_ADD_COMMANDII      0x01
45 #define COMMANDII_RESOLUTION_SHIFT      2
46 #define COMMANDII_RESOLUTION_MASK       (0x3 << COMMANDII_RESOLUTION_SHIFT)
47
48 #define COMMANDII_RANGE_SHIFT           0
49 #define COMMANDII_RANGE_MASK            (0x3 << COMMANDII_RANGE_SHIFT)
50
51 #define COMMANDII_SCHEME_SHIFT          7
52 #define COMMANDII_SCHEME_MASK           (0x1 << COMMANDII_SCHEME_SHIFT)
53
54 #define ISL29018_REG_ADD_DATA_LSB       0x02
55 #define ISL29018_REG_ADD_DATA_MSB       0x03
56
57 #define ISL29018_REG_TEST               0x08
58 #define ISL29018_TEST_SHIFT             0
59 #define ISL29018_TEST_MASK              (0xFF << ISL29018_TEST_SHIFT)
60
61 #define ISL29035_REG_DEVICE_ID          0x0F
62 #define ISL29035_DEVICE_ID_SHIFT        0x03
63 #define ISL29035_DEVICE_ID_MASK         (0x7 << ISL29035_DEVICE_ID_SHIFT)
64 #define ISL29035_DEVICE_ID              0x5
65 #define ISL29035_BOUT_SHIFT             0x07
66 #define ISL29035_BOUT_MASK              (0x01 << ISL29035_BOUT_SHIFT)
67
68 struct isl29018_chip {
69         struct device           *dev;
70         struct regmap           *regmap;
71         struct mutex            lock;
72         int                     type;
73         unsigned int            lux_scale;
74         unsigned int            lux_uscale;
75         unsigned int            range;
76         unsigned int            adc_bit;
77         int                     prox_scheme;
78         bool                    suspended;
79 };
80
81 static int isl29018_set_range(struct isl29018_chip *chip, unsigned long range,
82                 unsigned int *new_range)
83 {
84         static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
85         int i;
86
87         for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
88                 if (range <= supp_ranges[i]) {
89                         *new_range = (unsigned int)supp_ranges[i];
90                         break;
91                 }
92         }
93
94         if (i >= ARRAY_SIZE(supp_ranges))
95                 return -EINVAL;
96
97         return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
98                         COMMANDII_RANGE_MASK, i << COMMANDII_RANGE_SHIFT);
99 }
100
101 static int isl29018_set_resolution(struct isl29018_chip *chip,
102                         unsigned long adcbit, unsigned int *conf_adc_bit)
103 {
104         static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
105         int i;
106
107         for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
108                 if (adcbit >= supp_adcbit[i]) {
109                         *conf_adc_bit = (unsigned int)supp_adcbit[i];
110                         break;
111                 }
112         }
113
114         if (i >= ARRAY_SIZE(supp_adcbit))
115                 return -EINVAL;
116
117         return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
118                         COMMANDII_RESOLUTION_MASK,
119                         i << COMMANDII_RESOLUTION_SHIFT);
120 }
121
122 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
123 {
124         int status;
125         unsigned int lsb;
126         unsigned int msb;
127
128         /* Set mode */
129         status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
130                         mode << COMMMAND1_OPMODE_SHIFT);
131         if (status) {
132                 dev_err(chip->dev,
133                         "Error in setting operating mode err %d\n", status);
134                 return status;
135         }
136         msleep(CONVERSION_TIME_MS);
137         status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
138         if (status < 0) {
139                 dev_err(chip->dev,
140                         "Error in reading LSB DATA with err %d\n", status);
141                 return status;
142         }
143
144         status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
145         if (status < 0) {
146                 dev_err(chip->dev,
147                         "Error in reading MSB DATA with error %d\n", status);
148                 return status;
149         }
150         dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
151
152         return (msb << 8) | lsb;
153 }
154
155 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
156 {
157         int lux_data;
158         unsigned int data_x_range, lux_unshifted;
159
160         lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
161
162         if (lux_data < 0)
163                 return lux_data;
164
165         /* To support fractional scaling, separate the unshifted lux
166          * into two calculations: int scaling and micro-scaling.
167          * lux_uscale ranges from 0-999999, so about 20 bits.  Split
168          * the /1,000,000 in two to reduce the risk of over/underflow.
169          */
170         data_x_range = lux_data * chip->range;
171         lux_unshifted = data_x_range * chip->lux_scale;
172         lux_unshifted += data_x_range / 1000 * chip->lux_uscale / 1000;
173         *lux = lux_unshifted >> chip->adc_bit;
174
175         return 0;
176 }
177
178 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
179 {
180         int ir_data;
181
182         ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
183
184         if (ir_data < 0)
185                 return ir_data;
186
187         *ir = ir_data;
188
189         return 0;
190 }
191
192 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
193                 int *near_ir)
194 {
195         int status;
196         int prox_data = -1;
197         int ir_data = -1;
198
199         /* Do proximity sensing with required scheme */
200         status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
201                         COMMANDII_SCHEME_MASK,
202                         scheme << COMMANDII_SCHEME_SHIFT);
203         if (status) {
204                 dev_err(chip->dev, "Error in setting operating mode\n");
205                 return status;
206         }
207
208         prox_data = isl29018_read_sensor_input(chip,
209                                         COMMMAND1_OPMODE_PROX_ONCE);
210         if (prox_data < 0)
211                 return prox_data;
212
213         if (scheme == 1) {
214                 *near_ir = prox_data;
215                 return 0;
216         }
217
218         ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
219
220         if (ir_data < 0)
221                 return ir_data;
222
223         if (prox_data >= ir_data)
224                 *near_ir = prox_data - ir_data;
225         else
226                 *near_ir = 0;
227
228         return 0;
229 }
230
231 /* Sysfs interface */
232 /* range */
233 static ssize_t show_range(struct device *dev,
234                         struct device_attribute *attr, char *buf)
235 {
236         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
237         struct isl29018_chip *chip = iio_priv(indio_dev);
238
239         return sprintf(buf, "%u\n", chip->range);
240 }
241
242 static ssize_t store_range(struct device *dev,
243                 struct device_attribute *attr, const char *buf, size_t count)
244 {
245         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
246         struct isl29018_chip *chip = iio_priv(indio_dev);
247         int status;
248         unsigned long lval;
249         unsigned int new_range;
250
251         if (kstrtoul(buf, 10, &lval))
252                 return -EINVAL;
253
254         if (!(lval == 1000UL || lval == 4000UL ||
255                         lval == 16000UL || lval == 64000UL)) {
256                 dev_err(dev, "The range is not supported\n");
257                 return -EINVAL;
258         }
259
260         mutex_lock(&chip->lock);
261         status = isl29018_set_range(chip, lval, &new_range);
262         if (status < 0) {
263                 mutex_unlock(&chip->lock);
264                 dev_err(dev,
265                         "Error in setting max range with err %d\n", status);
266                 return status;
267         }
268         chip->range = new_range;
269         mutex_unlock(&chip->lock);
270
271         return count;
272 }
273
274 /* resolution */
275 static ssize_t show_resolution(struct device *dev,
276                         struct device_attribute *attr, char *buf)
277 {
278         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
279         struct isl29018_chip *chip = iio_priv(indio_dev);
280
281         return sprintf(buf, "%u\n", chip->adc_bit);
282 }
283
284 static ssize_t store_resolution(struct device *dev,
285                 struct device_attribute *attr, const char *buf, size_t count)
286 {
287         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
288         struct isl29018_chip *chip = iio_priv(indio_dev);
289         int status;
290         unsigned int val;
291         unsigned int new_adc_bit;
292
293         if (kstrtouint(buf, 10, &val))
294                 return -EINVAL;
295         if (!(val == 4 || val == 8 || val == 12 || val == 16)) {
296                 dev_err(dev, "The resolution is not supported\n");
297                 return -EINVAL;
298         }
299
300         mutex_lock(&chip->lock);
301         status = isl29018_set_resolution(chip, val, &new_adc_bit);
302         if (status < 0) {
303                 mutex_unlock(&chip->lock);
304                 dev_err(dev, "Error in setting resolution\n");
305                 return status;
306         }
307         chip->adc_bit = new_adc_bit;
308         mutex_unlock(&chip->lock);
309
310         return count;
311 }
312
313 /* proximity scheme */
314 static ssize_t show_prox_infrared_suppression(struct device *dev,
315                         struct device_attribute *attr, char *buf)
316 {
317         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
318         struct isl29018_chip *chip = iio_priv(indio_dev);
319
320         /* return the "proximity scheme" i.e. if the chip does on chip
321         infrared suppression (1 means perform on chip suppression) */
322         return sprintf(buf, "%d\n", chip->prox_scheme);
323 }
324
325 static ssize_t store_prox_infrared_suppression(struct device *dev,
326                 struct device_attribute *attr, const char *buf, size_t count)
327 {
328         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
329         struct isl29018_chip *chip = iio_priv(indio_dev);
330         int val;
331
332         if (kstrtoint(buf, 10, &val))
333                 return -EINVAL;
334         if (!(val == 0 || val == 1)) {
335                 dev_err(dev, "The mode is not supported\n");
336                 return -EINVAL;
337         }
338
339         /* get the  "proximity scheme" i.e. if the chip does on chip
340         infrared suppression (1 means perform on chip suppression) */
341         mutex_lock(&chip->lock);
342         chip->prox_scheme = val;
343         mutex_unlock(&chip->lock);
344
345         return count;
346 }
347
348 /* Channel IO */
349 static int isl29018_write_raw(struct iio_dev *indio_dev,
350                               struct iio_chan_spec const *chan,
351                               int val,
352                               int val2,
353                               long mask)
354 {
355         struct isl29018_chip *chip = iio_priv(indio_dev);
356         int ret = -EINVAL;
357
358         mutex_lock(&chip->lock);
359         if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
360                 chip->lux_scale = val;
361                 /* With no write_raw_get_fmt(), val2 is a MICRO fraction. */
362                 chip->lux_uscale = val2;
363                 ret = 0;
364         }
365         mutex_unlock(&chip->lock);
366
367         return ret;
368 }
369
370 static int isl29018_read_raw(struct iio_dev *indio_dev,
371                              struct iio_chan_spec const *chan,
372                              int *val,
373                              int *val2,
374                              long mask)
375 {
376         int ret = -EINVAL;
377         struct isl29018_chip *chip = iio_priv(indio_dev);
378
379         mutex_lock(&chip->lock);
380         if (chip->suspended) {
381                 mutex_unlock(&chip->lock);
382                 return -EBUSY;
383         }
384         switch (mask) {
385         case IIO_CHAN_INFO_RAW:
386         case IIO_CHAN_INFO_PROCESSED:
387                 switch (chan->type) {
388                 case IIO_LIGHT:
389                         ret = isl29018_read_lux(chip, val);
390                         break;
391                 case IIO_INTENSITY:
392                         ret = isl29018_read_ir(chip, val);
393                         break;
394                 case IIO_PROXIMITY:
395                         ret = isl29018_read_proximity_ir(chip,
396                                         chip->prox_scheme, val);
397                         break;
398                 default:
399                         break;
400                 }
401                 if (!ret)
402                         ret = IIO_VAL_INT;
403                 break;
404         case IIO_CHAN_INFO_CALIBSCALE:
405                 if (chan->type == IIO_LIGHT) {
406                         *val = chip->lux_scale;
407                         *val2 = chip->lux_uscale;
408                         ret = IIO_VAL_INT_PLUS_MICRO;
409                 }
410                 break;
411         default:
412                 break;
413         }
414         mutex_unlock(&chip->lock);
415         return ret;
416 }
417
418 #define ISL29018_LIGHT_CHANNEL {                                        \
419         .type = IIO_LIGHT,                                              \
420         .indexed = 1,                                                   \
421         .channel = 0,                                                   \
422         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |            \
423         BIT(IIO_CHAN_INFO_CALIBSCALE),                                  \
424 }
425
426 #define ISL29018_IR_CHANNEL {                                           \
427         .type = IIO_INTENSITY,                                          \
428         .modified = 1,                                                  \
429         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
430         .channel2 = IIO_MOD_LIGHT_IR,                                   \
431 }
432
433 #define ISL29018_PROXIMITY_CHANNEL {                                    \
434         .type = IIO_PROXIMITY,                                          \
435         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
436 }
437
438 static const struct iio_chan_spec isl29018_channels[] = {
439         ISL29018_LIGHT_CHANNEL,
440         ISL29018_IR_CHANNEL,
441         ISL29018_PROXIMITY_CHANNEL,
442 };
443
444 static const struct iio_chan_spec isl29023_channels[] = {
445         ISL29018_LIGHT_CHANNEL,
446         ISL29018_IR_CHANNEL,
447 };
448
449 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
450 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
451 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
452 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
453                                         show_resolution, store_resolution, 0);
454 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
455                                         S_IRUGO | S_IWUSR,
456                                         show_prox_infrared_suppression,
457                                         store_prox_infrared_suppression, 0);
458
459 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
460 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
461 static struct attribute *isl29018_attributes[] = {
462         ISL29018_DEV_ATTR(range),
463         ISL29018_CONST_ATTR(range_available),
464         ISL29018_DEV_ATTR(adc_resolution),
465         ISL29018_CONST_ATTR(adc_resolution_available),
466         ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
467         NULL
468 };
469
470 static struct attribute *isl29023_attributes[] = {
471         ISL29018_DEV_ATTR(range),
472         ISL29018_CONST_ATTR(range_available),
473         ISL29018_DEV_ATTR(adc_resolution),
474         ISL29018_CONST_ATTR(adc_resolution_available),
475         NULL
476 };
477
478 static const struct attribute_group isl29018_group = {
479         .attrs = isl29018_attributes,
480 };
481
482 static const struct attribute_group isl29023_group = {
483         .attrs = isl29023_attributes,
484 };
485
486 static int isl29035_detect(struct isl29018_chip *chip)
487 {
488         int status;
489         unsigned int id;
490
491         status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
492         if (status < 0) {
493                 dev_err(chip->dev,
494                         "Error reading ID register with error %d\n",
495                         status);
496                 return status;
497         }
498
499         id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
500
501         if (id != ISL29035_DEVICE_ID)
502                 return -ENODEV;
503
504         /* clear out brownout bit */
505         return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
506                                   ISL29035_BOUT_MASK, 0);
507 }
508
509 enum {
510         isl29018,
511         isl29023,
512         isl29035,
513 };
514
515 static int isl29018_chip_init(struct isl29018_chip *chip)
516 {
517         int status;
518         unsigned int new_adc_bit;
519         unsigned int new_range;
520
521         if (chip->type == isl29035) {
522                 status = isl29035_detect(chip);
523                 if (status < 0)
524                         return status;
525         }
526
527         /* Code added per Intersil Application Note 1534:
528          *     When VDD sinks to approximately 1.8V or below, some of
529          * the part's registers may change their state. When VDD
530          * recovers to 2.25V (or greater), the part may thus be in an
531          * unknown mode of operation. The user can return the part to
532          * a known mode of operation either by (a) setting VDD = 0V for
533          * 1 second or more and then powering back up with a slew rate
534          * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
535          * conversions, clear the test registers, and then rewrite all
536          * registers to the desired values.
537          * ...
538          * FOR ISL29011, ISL29018, ISL29021, ISL29023
539          * 1. Write 0x00 to register 0x08 (TEST)
540          * 2. Write 0x00 to register 0x00 (CMD1)
541          * 3. Rewrite all registers to the desired values
542          *
543          * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
544          * the same thing EXCEPT the data sheet asks for a 1ms delay after
545          * writing the CMD1 register.
546          */
547         status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
548         if (status < 0) {
549                 dev_err(chip->dev, "Failed to clear isl29018 TEST reg."
550                                         "(%d)\n", status);
551                 return status;
552         }
553
554         /* See Intersil AN1534 comments above.
555          * "Operating Mode" (COMMAND1) register is reprogrammed when
556          * data is read from the device.
557          */
558         status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
559         if (status < 0) {
560                 dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg."
561                                         "(%d)\n", status);
562                 return status;
563         }
564
565         msleep(1);      /* per data sheet, page 10 */
566
567         /* set defaults */
568         status = isl29018_set_range(chip, chip->range, &new_range);
569         if (status < 0) {
570                 dev_err(chip->dev, "Init of isl29018 fails\n");
571                 return status;
572         }
573
574         status = isl29018_set_resolution(chip, chip->adc_bit,
575                                                 &new_adc_bit);
576
577         return 0;
578 }
579
580 static const struct iio_info isl29018_info = {
581         .attrs = &isl29018_group,
582         .driver_module = THIS_MODULE,
583         .read_raw = &isl29018_read_raw,
584         .write_raw = &isl29018_write_raw,
585 };
586
587 static const struct iio_info isl29023_info = {
588         .attrs = &isl29023_group,
589         .driver_module = THIS_MODULE,
590         .read_raw = &isl29018_read_raw,
591         .write_raw = &isl29018_write_raw,
592 };
593
594 static bool is_volatile_reg(struct device *dev, unsigned int reg)
595 {
596         switch (reg) {
597         case ISL29018_REG_ADD_DATA_LSB:
598         case ISL29018_REG_ADD_DATA_MSB:
599         case ISL29018_REG_ADD_COMMAND1:
600         case ISL29018_REG_TEST:
601         case ISL29035_REG_DEVICE_ID:
602                 return true;
603         default:
604                 return false;
605         }
606 }
607
608 /*
609  * isl29018_regmap_config: regmap configuration.
610  * Use RBTREE mechanism for caching.
611  */
612 static const struct regmap_config isl29018_regmap_config = {
613         .reg_bits = 8,
614         .val_bits = 8,
615         .volatile_reg = is_volatile_reg,
616         .max_register = ISL29018_REG_TEST,
617         .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
618         .cache_type = REGCACHE_RBTREE,
619 };
620
621 /* isl29035_regmap_config: regmap configuration for ISL29035 */
622 static const struct regmap_config isl29035_regmap_config = {
623         .reg_bits = 8,
624         .val_bits = 8,
625         .volatile_reg = is_volatile_reg,
626         .max_register = ISL29035_REG_DEVICE_ID,
627         .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
628         .cache_type = REGCACHE_RBTREE,
629 };
630
631 struct chip_info {
632         const struct iio_chan_spec *channels;
633         int num_channels;
634         const struct iio_info *indio_info;
635         const struct regmap_config *regmap_cfg;
636 };
637
638 static const struct chip_info chip_info_tbl[] = {
639         [isl29018] = {
640                 .channels = isl29018_channels,
641                 .num_channels = ARRAY_SIZE(isl29018_channels),
642                 .indio_info = &isl29018_info,
643                 .regmap_cfg = &isl29018_regmap_config,
644         },
645         [isl29023] = {
646                 .channels = isl29023_channels,
647                 .num_channels = ARRAY_SIZE(isl29023_channels),
648                 .indio_info = &isl29023_info,
649                 .regmap_cfg = &isl29018_regmap_config,
650         },
651         [isl29035] = {
652                 .channels = isl29023_channels,
653                 .num_channels = ARRAY_SIZE(isl29023_channels),
654                 .indio_info = &isl29023_info,
655                 .regmap_cfg = &isl29035_regmap_config,
656         },
657 };
658
659 static int isl29018_probe(struct i2c_client *client,
660                          const struct i2c_device_id *id)
661 {
662         struct isl29018_chip *chip;
663         struct iio_dev *indio_dev;
664         int err;
665
666         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
667         if (indio_dev == NULL) {
668                 dev_err(&client->dev, "iio allocation fails\n");
669                 return -ENOMEM;
670         }
671         chip = iio_priv(indio_dev);
672
673         i2c_set_clientdata(client, indio_dev);
674         chip->dev = &client->dev;
675
676         mutex_init(&chip->lock);
677
678         chip->type = id->driver_data;
679         chip->lux_scale = 1;
680         chip->lux_uscale = 0;
681         chip->range = 1000;
682         chip->adc_bit = 16;
683         chip->suspended = false;
684
685         chip->regmap = devm_regmap_init_i2c(client,
686                                 chip_info_tbl[id->driver_data].regmap_cfg);
687         if (IS_ERR(chip->regmap)) {
688                 err = PTR_ERR(chip->regmap);
689                 dev_err(chip->dev, "regmap initialization failed: %d\n", err);
690                 return err;
691         }
692
693         err = isl29018_chip_init(chip);
694         if (err)
695                 return err;
696
697         indio_dev->info = chip_info_tbl[id->driver_data].indio_info;
698         indio_dev->channels = chip_info_tbl[id->driver_data].channels;
699         indio_dev->num_channels = chip_info_tbl[id->driver_data].num_channels;
700         indio_dev->name = id->name;
701         indio_dev->dev.parent = &client->dev;
702         indio_dev->modes = INDIO_DIRECT_MODE;
703         err = devm_iio_device_register(&client->dev, indio_dev);
704         if (err) {
705                 dev_err(&client->dev, "iio registration fails\n");
706                 return err;
707         }
708
709         return 0;
710 }
711
712 #ifdef CONFIG_PM_SLEEP
713 static int isl29018_suspend(struct device *dev)
714 {
715         struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
716
717         mutex_lock(&chip->lock);
718
719         /* Since this driver uses only polling commands, we are by default in
720          * auto shutdown (ie, power-down) mode.
721          * So we do not have much to do here.
722          */
723         chip->suspended = true;
724
725         mutex_unlock(&chip->lock);
726         return 0;
727 }
728
729 static int isl29018_resume(struct device *dev)
730 {
731         struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
732         int err;
733
734         mutex_lock(&chip->lock);
735
736         err = isl29018_chip_init(chip);
737         if (!err)
738                 chip->suspended = false;
739
740         mutex_unlock(&chip->lock);
741         return err;
742 }
743
744 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
745 #define ISL29018_PM_OPS (&isl29018_pm_ops)
746 #else
747 #define ISL29018_PM_OPS NULL
748 #endif
749
750 static const struct i2c_device_id isl29018_id[] = {
751         {"isl29018", isl29018},
752         {"isl29023", isl29023},
753         {"isl29035", isl29035},
754         {}
755 };
756
757 MODULE_DEVICE_TABLE(i2c, isl29018_id);
758
759 static const struct of_device_id isl29018_of_match[] = {
760         { .compatible = "isil,isl29018", },
761         { .compatible = "isil,isl29023", },
762         { .compatible = "isil,isl29035", },
763         { },
764 };
765 MODULE_DEVICE_TABLE(of, isl29018_of_match);
766
767 static struct i2c_driver isl29018_driver = {
768         .class  = I2C_CLASS_HWMON,
769         .driver  = {
770                         .name = "isl29018",
771                         .pm = ISL29018_PM_OPS,
772                         .owner = THIS_MODULE,
773                         .of_match_table = isl29018_of_match,
774                     },
775         .probe   = isl29018_probe,
776         .id_table = isl29018_id,
777 };
778 module_i2c_driver(isl29018_driver);
779
780 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
781 MODULE_LICENSE("GPL");