CHROMIUM: iio: isl29018: make read delay depend on resolution
[cascardo/linux.git] / drivers / staging / iio / light / isl29018.c
1 /*
2  * A iio driver for the light sensor ISL 29018.
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/slab.h>
30 #include "../iio.h"
31 #include "../sysfs.h"
32
33 #define ISL29018_REG_ADD_COMMAND1       0x00
34 #define COMMMAND1_OPMODE_SHIFT          5
35 #define COMMMAND1_OPMODE_MASK           (7 << COMMMAND1_OPMODE_SHIFT)
36 #define COMMMAND1_OPMODE_POWER_DOWN     0
37 #define COMMMAND1_OPMODE_ALS_ONCE       1
38 #define COMMMAND1_OPMODE_IR_ONCE        2
39 #define COMMMAND1_OPMODE_PROX_ONCE      3
40
41 #define ISL29018_REG_ADD_COMMANDII      0x01
42 #define COMMANDII_RESOLUTION_SHIFT      2
43 #define COMMANDII_RESOLUTION_MASK       (0x3 << COMMANDII_RESOLUTION_SHIFT)
44
45 #define COMMANDII_RANGE_SHIFT           0
46 #define COMMANDII_RANGE_MASK            (0x3 << COMMANDII_RANGE_SHIFT)
47
48 #define COMMANDII_SCHEME_SHIFT          7
49 #define COMMANDII_SCHEME_MASK           (0x1 << COMMANDII_SCHEME_SHIFT)
50
51 #define ISL29018_REG_ADD_DATA_LSB       0x02
52 #define ISL29018_REG_ADD_DATA_MSB       0x03
53 #define ISL29018_MAX_REGS               (ISL29018_REG_ADD_DATA_MSB+1)
54
55 #define ISL29018_REG_TEST               0x08
56 #define ISL29018_TEST_SHIFT             0
57 #define ISL29018_TEST_MASK              (0xFF << ISL29018_TEST_SHIFT)
58
59 #define ISL29018_REG_TEST               0x08
60 #define ISL29018_TEST_SHIFT             0
61 #define ISL29018_TEST_MASK              (0xFF << ISL29018_TEST_SHIFT)
62
63 struct isl29018_chip {
64         struct i2c_client       *client;
65         struct mutex            lock;
66         unsigned int            lux_scale;
67         unsigned int            range;
68         unsigned int            adc_bit;
69         int                     prox_scheme;
70         u8                      reg_cache[ISL29018_MAX_REGS];
71 };
72
73 static int isl29018_write_data(struct i2c_client *client, u8 reg,
74                         u8 val, u8 mask, u8 shift)
75 {
76         u8 regval = val;
77         int ret;
78         struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
79
80         /* don't cache or mask REG_TEST */
81         if (reg < ISL29018_MAX_REGS) {
82                 regval = chip->reg_cache[reg];
83                 regval &= ~mask;
84                 regval |= val << shift;
85         }
86
87         ret = i2c_smbus_write_byte_data(client, reg, regval);
88         if (ret) {
89                 dev_err(&client->dev, "Write to device fails status %x\n", ret);
90         } else {
91                 /* don't update cache on err */
92                 if (reg < ISL29018_MAX_REGS)
93                         chip->reg_cache[reg] = regval;
94         }
95
96         return ret;
97 }
98
99 static int isl29018_set_range(struct i2c_client *client, unsigned long range,
100                 unsigned int *new_range)
101 {
102         static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
103         int i;
104
105         for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
106                 if (range <= supp_ranges[i]) {
107                         *new_range = (unsigned int)supp_ranges[i];
108                         break;
109                 }
110         }
111
112         if (i >= ARRAY_SIZE(supp_ranges))
113                 return -EINVAL;
114
115         return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
116                         i, COMMANDII_RANGE_MASK, COMMANDII_RANGE_SHIFT);
117 }
118
119 static int isl29018_set_resolution(struct i2c_client *client,
120                         unsigned long adcbit, unsigned int *conf_adc_bit)
121 {
122         static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
123         int i;
124
125         for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
126                 if (adcbit >= supp_adcbit[i]) {
127                         *conf_adc_bit = (unsigned int)supp_adcbit[i];
128                         break;
129                 }
130         }
131
132         if (i >= ARRAY_SIZE(supp_adcbit))
133                 return -EINVAL;
134
135         return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
136                         i, COMMANDII_RESOLUTION_MASK,
137                         COMMANDII_RESOLUTION_SHIFT);
138 }
139
140 static void integration_wait(struct isl29018_chip *chip)
141 {
142         int wait_time;
143
144         /* Integration and conversion time is described in the data sheet
145          * as 2^n x (constant), and for 16 bits it's 90ms. So calculate
146          * from there.
147          *
148          * Since the time taken depends on a resistor value, leave some
149          * margin above the 90ms timeout, otherwise we risk reading a
150          * stale value.
151          */
152         wait_time = DIV_ROUND_UP(100, 1 << (16 - chip->adc_bit));
153         msleep(wait_time);
154 }
155
156 static int isl29018_read_sensor_input(struct i2c_client *client, int mode)
157 {
158         int status;
159         int lux;
160
161         /* Set mode */
162         status = isl29018_write_data(client, ISL29018_REG_ADD_COMMAND1,
163                         mode, COMMMAND1_OPMODE_MASK, COMMMAND1_OPMODE_SHIFT);
164         if (status) {
165                 dev_err(&client->dev, "Error in setting operating mode\n");
166                 return status;
167         }
168         integration_wait(iio_priv(i2c_get_clientdata(client)));
169         status = i2c_smbus_read_word_data(client, ISL29018_REG_ADD_DATA_LSB);
170         if (status < 0) {
171                 dev_err(&client->dev, "Error in reading Lux DATA\n");
172                 return status;
173         }
174
175         lux = le16_to_cpu(status);
176         dev_vdbg(&client->dev, "lux = %u\n", lux);
177
178         return lux;
179 }
180
181 static int isl29018_read_lux(struct i2c_client *client, int *lux)
182 {
183         int lux_data;
184         struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
185
186         lux_data = isl29018_read_sensor_input(client,
187                                 COMMMAND1_OPMODE_ALS_ONCE);
188
189         if (lux_data < 0)
190                 return lux_data;
191
192         *lux = (lux_data * chip->range * chip->lux_scale) >> chip->adc_bit;
193
194         return 0;
195 }
196
197 static int isl29018_read_ir(struct i2c_client *client, int *ir)
198 {
199         int ir_data;
200
201         ir_data = isl29018_read_sensor_input(client, COMMMAND1_OPMODE_IR_ONCE);
202
203         if (ir_data < 0)
204                 return ir_data;
205
206         *ir = ir_data;
207
208         return 0;
209 }
210
211 static int isl29018_read_proximity_ir(struct i2c_client *client, int scheme,
212                 int *near_ir)
213 {
214         int status;
215         int prox_data = -1;
216         int ir_data = -1;
217
218         /* Do proximity sensing with required scheme */
219         status = isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
220                         scheme, COMMANDII_SCHEME_MASK, COMMANDII_SCHEME_SHIFT);
221         if (status) {
222                 dev_err(&client->dev, "Error in setting operating mode\n");
223                 return status;
224         }
225
226         prox_data = isl29018_read_sensor_input(client,
227                                         COMMMAND1_OPMODE_PROX_ONCE);
228         if (prox_data < 0)
229                 return prox_data;
230
231         if (scheme == 1) {
232                 *near_ir = prox_data;
233                 return 0;
234         }
235
236         ir_data = isl29018_read_sensor_input(client,
237                                 COMMMAND1_OPMODE_IR_ONCE);
238
239         if (ir_data < 0)
240                 return ir_data;
241
242         if (prox_data >= ir_data)
243                 *near_ir = prox_data - ir_data;
244         else
245                 *near_ir = 0;
246
247         return 0;
248 }
249
250 /* Sysfs interface */
251 /* lux_scale */
252 static ssize_t show_lux_scale(struct device *dev,
253                         struct device_attribute *attr, char *buf)
254 {
255         struct iio_dev *indio_dev = dev_get_drvdata(dev);
256         struct isl29018_chip *chip = iio_priv(indio_dev);
257
258         return sprintf(buf, "%d\n", chip->lux_scale);
259 }
260
261 static ssize_t store_lux_scale(struct device *dev,
262                 struct device_attribute *attr, const char *buf, size_t count)
263 {
264         struct iio_dev *indio_dev = dev_get_drvdata(dev);
265         struct isl29018_chip *chip = iio_priv(indio_dev);
266         unsigned long lval;
267
268         lval = simple_strtoul(buf, NULL, 10);
269         if (lval == 0)
270                 return -EINVAL;
271
272         mutex_lock(&chip->lock);
273         chip->lux_scale = lval;
274         mutex_unlock(&chip->lock);
275
276         return count;
277 }
278
279 static ssize_t get_sensor_data(struct device *dev, char *buf, int mode)
280 {
281         struct iio_dev *indio_dev = dev_get_drvdata(dev);
282         struct isl29018_chip *chip = iio_priv(indio_dev);
283         struct i2c_client *client = chip->client;
284         int value = 0;
285         int status;
286
287         mutex_lock(&chip->lock);
288         switch (mode) {
289         case COMMMAND1_OPMODE_PROX_ONCE:
290                 status = isl29018_read_proximity_ir(client,
291                                                 chip->prox_scheme, &value);
292         break;
293
294         case COMMMAND1_OPMODE_ALS_ONCE:
295                 status = isl29018_read_lux(client, &value);
296                 break;
297
298         case COMMMAND1_OPMODE_IR_ONCE:
299                 status = isl29018_read_ir(client, &value);
300                 break;
301
302         default:
303                 dev_err(&client->dev, "Mode %d is not supported\n", mode);
304                 mutex_unlock(&chip->lock);
305                 return -EBUSY;
306         }
307
308         if (status < 0) {
309                 dev_err(&client->dev, "Error in Reading data");
310                 mutex_unlock(&chip->lock);
311                 return status;
312         }
313
314         mutex_unlock(&chip->lock);
315
316         return sprintf(buf, "%d\n", value);
317 }
318
319
320 /* Read lux */
321 static ssize_t show_lux(struct device *dev,
322                 struct device_attribute *devattr, char *buf)
323 {
324         return get_sensor_data(dev, buf, COMMMAND1_OPMODE_ALS_ONCE);
325 }
326
327 /* Read ir */
328 static ssize_t show_ir(struct device *dev,
329                 struct device_attribute *devattr, char *buf)
330 {
331         return get_sensor_data(dev, buf, COMMMAND1_OPMODE_IR_ONCE);
332 }
333
334 /* Read nearest ir */
335 static ssize_t show_proxim_ir(struct device *dev,
336                 struct device_attribute *devattr, char *buf)
337 {
338         return get_sensor_data(dev, buf, COMMMAND1_OPMODE_PROX_ONCE);
339 }
340
341 /* range */
342 static ssize_t show_range(struct device *dev,
343                         struct device_attribute *attr, char *buf)
344 {
345         struct iio_dev *indio_dev = dev_get_drvdata(dev);
346         struct isl29018_chip *chip = iio_priv(indio_dev);
347
348         return sprintf(buf, "%u\n", chip->range);
349 }
350
351 static ssize_t store_range(struct device *dev,
352                 struct device_attribute *attr, const char *buf, size_t count)
353 {
354         struct iio_dev *indio_dev = dev_get_drvdata(dev);
355         struct isl29018_chip *chip = iio_priv(indio_dev);
356         struct i2c_client *client = chip->client;
357         int status;
358         unsigned long lval;
359         unsigned int new_range;
360
361         if (strict_strtoul(buf, 10, &lval))
362                 return -EINVAL;
363
364         if (!(lval == 1000UL || lval == 4000UL ||
365                         lval == 16000UL || lval == 64000UL)) {
366                 dev_err(dev, "The range is not supported\n");
367                 return -EINVAL;
368         }
369
370         mutex_lock(&chip->lock);
371         status = isl29018_set_range(client, lval, &new_range);
372         if (status < 0) {
373                 mutex_unlock(&chip->lock);
374                 dev_err(dev, "Error in setting max range\n");
375                 return status;
376         }
377         chip->range = new_range;
378         mutex_unlock(&chip->lock);
379
380         return count;
381 }
382
383 /* resolution */
384 static ssize_t show_resolution(struct device *dev,
385                         struct device_attribute *attr, char *buf)
386 {
387         struct iio_dev *indio_dev = dev_get_drvdata(dev);
388         struct isl29018_chip *chip = iio_priv(indio_dev);
389
390         return sprintf(buf, "%u\n", chip->adc_bit);
391 }
392
393 static ssize_t store_resolution(struct device *dev,
394                 struct device_attribute *attr, const char *buf, size_t count)
395 {
396         struct iio_dev *indio_dev = dev_get_drvdata(dev);
397         struct isl29018_chip *chip = iio_priv(indio_dev);
398         struct i2c_client *client = chip->client;
399         int status;
400         unsigned long lval;
401         unsigned int new_adc_bit;
402
403         if (strict_strtoul(buf, 10, &lval))
404                 return -EINVAL;
405         if (!(lval == 4 || lval == 8 || lval == 12 || lval == 16)) {
406                 dev_err(dev, "The resolution is not supported\n");
407                 return -EINVAL;
408         }
409
410         mutex_lock(&chip->lock);
411         status = isl29018_set_resolution(client, lval, &new_adc_bit);
412         if (status < 0) {
413                 mutex_unlock(&chip->lock);
414                 dev_err(dev, "Error in setting resolution\n");
415                 return status;
416         }
417         chip->adc_bit = new_adc_bit;
418         mutex_unlock(&chip->lock);
419
420         return count;
421 }
422
423 /* proximity scheme */
424 static ssize_t show_prox_infrared_supression(struct device *dev,
425                         struct device_attribute *attr, char *buf)
426 {
427         struct iio_dev *indio_dev = dev_get_drvdata(dev);
428         struct isl29018_chip *chip = iio_priv(indio_dev);
429
430         /* return the "proximity scheme" i.e. if the chip does on chip
431         infrared supression (1 means perform on chip supression) */
432         return sprintf(buf, "%d\n", chip->prox_scheme);
433 }
434
435 static ssize_t store_prox_infrared_supression(struct device *dev,
436                 struct device_attribute *attr, const char *buf, size_t count)
437 {
438         struct iio_dev *indio_dev = dev_get_drvdata(dev);
439         struct isl29018_chip *chip = iio_priv(indio_dev);
440         unsigned long lval;
441
442         if (strict_strtoul(buf, 10, &lval))
443                 return -EINVAL;
444         if (!(lval == 0UL || lval == 1UL)) {
445                 dev_err(dev, "The mode is not supported\n");
446                 return -EINVAL;
447         }
448
449         /* get the  "proximity scheme" i.e. if the chip does on chip
450         infrared supression (1 means perform on chip supression) */
451         mutex_lock(&chip->lock);
452         chip->prox_scheme = (int)lval;
453         mutex_unlock(&chip->lock);
454
455         return count;
456 }
457
458 /* Channel IO */
459 static int isl29018_write_raw(struct iio_dev *indio_dev,
460                               struct iio_chan_spec const *chan,
461                               int val,
462                               int val2,
463                               long mask)
464 {
465         struct isl29018_chip *chip = iio_priv(indio_dev);
466         int ret = -EINVAL;
467
468         mutex_lock(&chip->lock);
469         if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
470                 chip->lux_scale = val;
471                 ret = 0;
472         }
473         mutex_unlock(&chip->lock);
474
475         return 0;
476 }
477
478 static int isl29018_read_raw(struct iio_dev *indio_dev,
479                              struct iio_chan_spec const *chan,
480                              int *val,
481                              int *val2,
482                              long mask)
483 {
484         int ret = -EINVAL;
485         struct isl29018_chip *chip = iio_priv(indio_dev);
486         struct i2c_client *client = chip->client;
487
488         mutex_lock(&chip->lock);
489         switch (mask) {
490         case 0:
491                 switch (chan->type) {
492                 case IIO_LIGHT:
493                         ret = isl29018_read_lux(client, val);
494                         break;
495                 case IIO_INTENSITY:
496                         ret = isl29018_read_ir(client, val);
497                         break;
498                 case IIO_PROXIMITY:
499                         ret = isl29018_read_proximity_ir(client,
500                                         chip->prox_scheme, val);
501                         break;
502                 default:
503                         break;
504                 }
505                 if (!ret)
506                         ret = IIO_VAL_INT;
507                 break;
508         case IIO_CHAN_INFO_CALIBSCALE:
509                 if (chan->type == IIO_LIGHT) {
510                         *val = chip->lux_scale;
511                         ret = IIO_VAL_INT;
512                 }
513                 break;
514         default:
515                 break;
516         }
517         mutex_unlock(&chip->lock);
518         return ret;
519 }
520
521 static const struct iio_chan_spec isl29018_channels[] = {
522         {
523                 .type = IIO_LIGHT,
524                 .indexed = 1,
525                 .channel = 0,
526                 .processed_val = IIO_PROCESSED,
527                 .info_mask = IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
528         }, {
529                 .type = IIO_INTENSITY,
530                 .modified = 1,
531                 .channel2 = IIO_MOD_LIGHT_IR,
532         }, {
533                 /* Unindexed in current ABI.  But perhaps it should be. */
534                 .type = IIO_PROXIMITY,
535         }
536 };
537
538 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
539 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
540 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
541 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
542                                         show_resolution, store_resolution, 0);
543 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_supression,
544                                         S_IRUGO | S_IWUSR,
545                                         show_prox_infrared_supression,
546                                         store_prox_infrared_supression, 0);
547 static IIO_DEVICE_ATTR(illuminance0_input, S_IRUGO, show_lux, NULL, 0);
548 static IIO_DEVICE_ATTR(illuminance0_calibscale, S_IRUGO | S_IWUSR,
549                                         show_lux_scale, store_lux_scale, 0);
550 static IIO_DEVICE_ATTR(intensity_infrared_raw, S_IRUGO, show_ir, NULL, 0);
551 static IIO_DEVICE_ATTR(proximity_raw, S_IRUGO, show_proxim_ir, NULL, 0);
552
553 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
554 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
555 static struct attribute *isl29018_attributes[] = {
556         ISL29018_DEV_ATTR(range),
557         ISL29018_CONST_ATTR(range_available),
558         ISL29018_DEV_ATTR(adc_resolution),
559         ISL29018_CONST_ATTR(adc_resolution_available),
560         ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_supression),
561         ISL29018_DEV_ATTR(illuminance0_input),
562         ISL29018_DEV_ATTR(illuminance0_calibscale),
563         ISL29018_DEV_ATTR(intensity_infrared_raw),
564         ISL29018_DEV_ATTR(proximity_raw),
565         NULL
566 };
567
568 static const struct attribute_group isl29108_group = {
569         .attrs = isl29018_attributes,
570 };
571
572 static int isl29018_chip_init(struct i2c_client *client)
573 {
574         struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
575         int status;
576         int new_adc_bit;
577         unsigned int new_range;
578
579         memset(chip->reg_cache, 0, sizeof(chip->reg_cache));
580
581         /* Code added per Intersil Application Note 1534:
582          *     When VDD sinks to approximately 1.8V or below, some of
583          * the part's registers may change their state. When VDD
584          * recovers to 2.25V (or greater), the part may thus be in an
585          * unknown mode of operation. The user can return the part to
586          * a known mode of operation either by (a) setting VDD = 0V for
587          * 1 second or more and then powering back up with a slew rate
588          * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
589          * conversions, clear the test registers, and then rewrite all
590          * registers to the desired values.
591          * ...
592          * FOR ISL29011, ISL29018, ISL29021, ISL29023
593          * 1. Write 0x00 to register 0x08 (TEST)
594          * 2. Write 0x00 to register 0x00 (CMD1)
595          * 3. Rewrite all registers to the desired values
596          *
597          * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
598          * the same thing EXCEPT the data sheet asks for a 1ms delay after
599          * writing the CMD1 register.
600          */
601         status = isl29018_write_data(client, ISL29018_REG_TEST, 0,
602                                 ISL29018_TEST_MASK, ISL29018_TEST_SHIFT);
603         if (status < 0) {
604                 dev_err(&client->dev, "Failed to clear isl29018 TEST reg."
605                                         "(%d)\n", status);
606                 return status;
607         }
608
609         /* See Intersil AN1534 comments above.
610          * "Operating Mode" (COMMAND1) register is reprogrammed when
611          * data is read from the device.
612          */
613         status = isl29018_write_data(client, ISL29018_REG_ADD_COMMAND1, 0,
614                                 0xff, 0);
615         if (status < 0) {
616                 dev_err(&client->dev, "Failed to clear isl29018 CMD1 reg."
617                                         "(%d)\n", status);
618                 return status;
619         }
620
621         msleep(1);      /* per data sheet, page 10 */
622
623         /* set defaults */
624         status = isl29018_set_range(client, chip->range, &new_range);
625         if (status < 0) {
626                 dev_err(&client->dev, "Init of isl29018 fails\n");
627                 return status;
628         }
629
630         status = isl29018_set_resolution(client, chip->adc_bit,
631                                                 &new_adc_bit);
632
633         return 0;
634 }
635
636 static const struct iio_info isl29108_info = {
637         .attrs = &isl29108_group,
638         .driver_module = THIS_MODULE,
639         .read_raw = &isl29018_read_raw,
640         .write_raw = &isl29018_write_raw,
641 };
642
643 static int __devinit isl29018_probe(struct i2c_client *client,
644                          const struct i2c_device_id *id)
645 {
646         struct isl29018_chip *chip;
647         struct iio_dev *indio_dev;
648         int err;
649
650         indio_dev = iio_allocate_device(sizeof(*chip));
651         if (indio_dev == NULL) {
652                 dev_err(&client->dev, "iio allocation fails\n");
653                 err = -ENOMEM;
654                 goto exit;
655         }
656         chip = iio_priv(indio_dev);
657
658         i2c_set_clientdata(client, indio_dev);
659         chip->client = client;
660
661         mutex_init(&chip->lock);
662
663         chip->lux_scale = 1;
664         chip->range = 1000;
665         chip->adc_bit = 16;
666
667         err = isl29018_chip_init(client);
668         if (err)
669                 goto exit_iio_free;
670
671         indio_dev->info = &isl29108_info;
672         indio_dev->channels = isl29018_channels;
673         indio_dev->num_channels = ARRAY_SIZE(isl29018_channels);
674         indio_dev->name = id->name;
675         indio_dev->dev.parent = &client->dev;
676         indio_dev->modes = INDIO_DIRECT_MODE;
677         err = iio_device_register(indio_dev);
678         if (err) {
679                 dev_err(&client->dev, "iio registration fails\n");
680                 goto exit_iio_free;
681         }
682
683         return 0;
684 exit_iio_free:
685         iio_free_device(indio_dev);
686 exit:
687         return err;
688 }
689
690 static int __devexit isl29018_remove(struct i2c_client *client)
691 {
692         struct iio_dev *indio_dev = i2c_get_clientdata(client);
693
694         dev_dbg(&client->dev, "%s()\n", __func__);
695         iio_device_unregister(indio_dev);
696         iio_free_device(indio_dev);
697
698         return 0;
699 }
700
701 static const struct i2c_device_id isl29018_id[] = {
702         {"isl29018", 0},
703         {}
704 };
705
706 MODULE_DEVICE_TABLE(i2c, isl29018_id);
707
708 static const struct of_device_id isl29018_of_match[] = {
709         { .compatible = "invn,isl29018", },
710         { },
711 };
712 MODULE_DEVICE_TABLE(of, isl29018_of_match);
713
714 static struct i2c_driver isl29018_driver = {
715         .class  = I2C_CLASS_HWMON,
716         .driver  = {
717                         .name = "isl29018",
718                         .owner = THIS_MODULE,
719                         .of_match_table = isl29018_of_match,
720                     },
721         .probe   = isl29018_probe,
722         .remove  = __devexit_p(isl29018_remove),
723         .id_table = isl29018_id,
724 };
725 module_i2c_driver(isl29018_driver);
726
727 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
728 MODULE_LICENSE("GPL");