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