]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/staging/iio/light/isl29028.c
isl29028: Convert als data to lux units
[linux-2.6.git] / drivers / staging / iio / light / isl29028.c
1 /*
2  * A iio driver for the light sensor ISL 29028.
3  *
4  * IIO Light driver for monitoring ambient light intensity in lux and proximity
5  * ir.
6  *
7  * Copyright (c) 2011, 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 <linux/completion.h>
31 #include <linux/interrupt.h>
32 #include "../iio.h"
33
34 #define CONVERSION_TIME_MS              100
35
36 #define ISL29028_REG_ADD_CONFIGURE      0x01
37
38 #define CONFIGURE_PROX_EN_MASK          (1 << 7)
39 #define CONFIGURE_PROX_EN_SH            7
40
41 #define CONFIGURE_PROX_SLP_SH           4
42 #define CONFIGURE_PROX_SLP_MASK         (7 << CONFIGURE_PROX_SLP_SH)
43
44 #define CONFIGURE_PROX_DRIVE            (1 << 3)
45
46 #define CONFIGURE_ALS_EN                1
47 #define CONFIGURE_ALS_DIS               0
48 #define CONFIGURE_ALS_EN_SH             2
49 #define CONFIGURE_ALS_EN_MASK           (1 << CONFIGURE_ALS_EN_SH)
50
51
52 #define CONFIGURE_ALS_RANGE_LOW_LUX     0
53 #define CONFIGURE_ALS_RANGE_HIGH_LUX    1
54 #define CONFIGURE_ALS_RANGE_SH          1
55 #define CONFIGURE_ALS_RANGE_MASK        (1 << CONFIGURE_ALS_RANGE_SH)
56
57 #define CONFIGURE_ALS_IR_MODE_MASK      1
58 #define CONFIGURE_ALS_IR_MODE_SH        0
59 #define CONFIGURE_ALS_IR_MODE_IR        1
60 #define CONFIGURE_ALS_IR_MODE_ALS       0
61
62 #define ISL29028_REG_ADD_INTERRUPT      0x02
63 #define INTERRUPT_PROX_FLAG_MASK        (1 << 7)
64 #define INTERRUPT_PROX_FLAG_SH          7
65 #define INTERRUPT_PROX_FLAG_EN          1
66 #define INTERRUPT_PROX_FLAG_DIS         0
67
68 #define INTERRUPT_PROX_PERSIST_SH       5
69 #define INTERRUPT_PROX_PERSIST_MASK     (3 << 5)
70
71 #define INTERRUPT_ALS_FLAG_MASK         (1 << 3)
72 #define INTERRUPT_ALS_FLAG_SH           3
73 #define INTERRUPT_ALS_FLAG_EN           1
74 #define INTERRUPT_ALS_FLAG_DIS          0
75
76 #define INTERRUPT_ALS_PERSIST_SH        1
77 #define INTERRUPT_ALS_PERSIST_MASK      (3 << 1)
78
79 #define ISL29028_REG_ADD_PROX_LOW_THRES         0x03
80 #define ISL29028_REG_ADD_PROX_HIGH_THRES        0x04
81
82 #define ISL29028_REG_ADD_ALSIR_LOW_THRES        0x05
83 #define ISL29028_REG_ADD_ALSIR_LH_THRES         0x06
84 #define ISL29028_REG_ADD_ALSIR_LH_THRES_L_SH    0
85 #define ISL29028_REG_ADD_ALSIR_LH_THRES_H_SH    4
86 #define ISL29028_REG_ADD_ALSIR_HIGH_THRES       0x07
87
88 #define ISL29028_REG_ADD_PROX_DATA              0x08
89 #define ISL29028_REG_ADD_ALSIR_L                0x09
90 #define ISL29028_REG_ADD_ALSIR_U                0x0A
91
92 #define ISL29028_REG_ADD_TEST1_MODE             0x0E
93 #define ISL29028_REG_ADD_TEST2_MODE             0x0F
94
95 #define ISL29028_MAX_REGS               ISL29028_REG_ADD_TEST2_MODE
96
97 enum {
98         MODE_NONE = 0,
99         MODE_ALS,
100         MODE_IR
101 };
102
103 struct isl29028_chip {
104         struct iio_dev          *indio_dev;
105         struct i2c_client       *client;
106         struct mutex            lock;
107         int                     irq;
108
109         int                     prox_period;
110         int                     prox_low_thres;
111         int                     prox_high_thres;
112         int                     prox_persist;
113         bool                    is_prox_enable;
114         int                     prox_reading;
115
116         int                     als_high_thres;
117         int                     als_low_thres;
118         int                     als_persist;
119         int                     als_range;
120         int                     als_reading;
121         int                     als_ir_mode;
122
123         int                     ir_high_thres;
124         int                     ir_low_thres;
125         int                     ir_reading;
126
127         bool                    is_int_enable;
128         bool                    is_proxim_int_waiting;
129         bool                    is_als_int_waiting;
130         struct completion       prox_completion;
131         struct completion       als_completion;
132         u8                      reg_cache[ISL29028_MAX_REGS];
133 };
134
135 static bool isl29028_write_data(struct i2c_client *client, u8 reg,
136         u8 val, u8 mask, u8 shift)
137 {
138         u8 regval;
139         int ret = 0;
140         struct isl29028_chip *chip = i2c_get_clientdata(client);
141
142         regval = chip->reg_cache[reg];
143         regval &= ~mask;
144         regval |= val << shift;
145
146         ret = i2c_smbus_write_byte_data(client, reg, regval);
147         if (ret) {
148                 dev_err(&client->dev, "Write to device reg %d fails status "
149                                 "%x\n", reg, ret);
150                 return false;
151         }
152         chip->reg_cache[reg] = regval;
153         return true;
154 }
155
156 static bool isl29018_set_proxim_period(struct i2c_client *client,
157                 bool is_enable, int period)
158 {
159         int prox_period[] = {0, 12, 50, 75, 100, 200, 400, 800};
160         int i;
161         int sel;
162         bool st;
163         if (period < 12)
164                 sel = 7;
165         else {
166                 for (i = 1; i < ARRAY_SIZE(prox_period) - 1; ++i) {
167                         if ((prox_period[i] <= period) &&
168                                                 period < prox_period[i + 1])
169                                 break;
170                 }
171                 sel = 7 - i;
172         }
173
174         if (!is_enable) {
175                 dev_dbg(&client->dev, "Disabling proximity sensing\n");
176                 st = isl29028_write_data(client, ISL29028_REG_ADD_CONFIGURE,
177                         0, CONFIGURE_PROX_EN_MASK, CONFIGURE_PROX_EN_SH);
178         } else {
179                 dev_dbg(&client->dev, "Enabling proximity sensing with period "
180                         "of %d ms sel %d period %d\n", prox_period[7 - sel],
181                         sel, period);
182                 st = isl29028_write_data(client, ISL29028_REG_ADD_CONFIGURE,
183                         sel, CONFIGURE_PROX_SLP_MASK, CONFIGURE_PROX_SLP_SH);
184                 if (st)
185                         st = isl29028_write_data(client,
186                                 ISL29028_REG_ADD_CONFIGURE, 1,
187                                 CONFIGURE_PROX_EN_MASK, CONFIGURE_PROX_EN_SH);
188         }
189         return st;
190 }
191
192 static bool isl29018_set_proxim_persist(struct i2c_client *client,
193                 bool is_enable, int persist)
194 {
195         int prox_perstant[] = {1, 4, 8, 16};
196         int i;
197         int sel;
198         bool st;
199         if (is_enable) {
200                 for (i = 0; i < ARRAY_SIZE(prox_perstant) - 1; ++i) {
201                         if ((prox_perstant[i] <= persist) &&
202                                                 persist < prox_perstant[i+1])
203                                 break;
204                 }
205                 sel = i;
206         }
207
208         if (is_enable) {
209                 dev_dbg(&client->dev, "Enabling proximity threshold interrupt\n");
210                 st = isl29028_write_data(client, ISL29028_REG_ADD_INTERRUPT,
211                         sel, INTERRUPT_PROX_PERSIST_MASK,
212                         INTERRUPT_PROX_PERSIST_SH);
213                 if (st)
214                         st = isl29028_write_data(client,
215                                 ISL29028_REG_ADD_INTERRUPT,
216                                 INTERRUPT_PROX_FLAG_EN,
217                                 INTERRUPT_PROX_FLAG_MASK,
218                                 INTERRUPT_PROX_FLAG_SH);
219         } else {
220                 st = isl29028_write_data(client,
221                         ISL29028_REG_ADD_INTERRUPT, INTERRUPT_PROX_FLAG_DIS,
222                         INTERRUPT_PROX_FLAG_MASK, INTERRUPT_PROX_FLAG_SH);
223         }
224         return st;
225 }
226
227 static bool isl29018_set_als_persist(struct i2c_client *client, bool is_enable,
228                         int persist)
229 {
230         int prox_perstant[] = {1, 4, 8, 16};
231         int i;
232         int sel;
233         bool st;
234         if (is_enable) {
235                 for (i = 0; i < ARRAY_SIZE(prox_perstant) - 1; ++i) {
236                         if ((prox_perstant[i] <= persist) &&
237                                                 persist < prox_perstant[i+1])
238                                 break;
239                 }
240                 sel = i;
241         }
242
243         if (is_enable) {
244                 dev_dbg(&client->dev, "Enabling als threshold interrupt\n");
245                 st = isl29028_write_data(client, ISL29028_REG_ADD_INTERRUPT,
246                         sel, INTERRUPT_ALS_PERSIST_MASK,
247                         INTERRUPT_ALS_PERSIST_SH);
248                 if (st)
249                         st = isl29028_write_data(client,
250                                 ISL29028_REG_ADD_INTERRUPT,
251                                 INTERRUPT_ALS_FLAG_EN,
252                                 INTERRUPT_ALS_FLAG_MASK,
253                                 INTERRUPT_ALS_FLAG_SH);
254         } else {
255                 st = isl29028_write_data(client,
256                         ISL29028_REG_ADD_INTERRUPT, INTERRUPT_ALS_FLAG_DIS,
257                         INTERRUPT_ALS_FLAG_MASK, INTERRUPT_ALS_FLAG_SH);
258         }
259         return st;
260 }
261
262 static bool isl29018_set_proxim_high_threshold(struct i2c_client *client, u8 th)
263 {
264         return isl29028_write_data(client, ISL29028_REG_ADD_PROX_HIGH_THRES,
265                 th, 0xFF, 0);
266 }
267
268 static bool isl29018_set_proxim_low_threshold(struct i2c_client *client, u8 th)
269 {
270         return isl29028_write_data(client, ISL29028_REG_ADD_PROX_LOW_THRES,
271                 th, 0xFF, 0);
272 }
273
274 static bool isl29018_set_irals_high_threshold(struct i2c_client *client,
275                                 u32 als)
276 {
277         bool st;
278         st = isl29028_write_data(client, ISL29028_REG_ADD_ALSIR_HIGH_THRES,
279                 (als >> 4) & 0xFF, 0xFF, 0);
280         if (st)
281                 st = isl29028_write_data(client,
282                         ISL29028_REG_ADD_ALSIR_LH_THRES, als & 0xF,
283                         0xF << ISL29028_REG_ADD_ALSIR_LH_THRES_H_SH,
284                         ISL29028_REG_ADD_ALSIR_LH_THRES_H_SH);
285         return st;
286 }
287
288 static bool isl29018_set_irals_low_threshold(struct i2c_client *client, u32 als)
289 {
290         bool st;
291         st = isl29028_write_data(client,
292                 ISL29028_REG_ADD_ALSIR_LH_THRES, (als >> 8) & 0xF,
293                 0xF << ISL29028_REG_ADD_ALSIR_LH_THRES_L_SH,
294                 ISL29028_REG_ADD_ALSIR_LH_THRES_L_SH);
295         if (st)
296                 st = isl29028_write_data(client,
297                         ISL29028_REG_ADD_ALSIR_LOW_THRES,
298                         als & 0xFF, 0xFF, 0);
299         return st;
300 }
301
302 static bool isl29018_set_als_ir_mode(struct i2c_client *client, bool is_enable,
303         bool is_als)
304 {
305         struct isl29028_chip *chip = i2c_get_clientdata(client);
306         bool st;
307         if (is_enable) {
308                 if (is_als) {
309                         dev_dbg(&client->dev, "Enabling ALS mode\n");
310                         st = isl29028_write_data(client,
311                                 ISL29028_REG_ADD_CONFIGURE,
312                                 CONFIGURE_ALS_IR_MODE_ALS,
313                                 CONFIGURE_ALS_IR_MODE_MASK,
314                                 CONFIGURE_ALS_IR_MODE_SH);
315                         if (st)
316                                 st = isl29028_write_data(client,
317                                         ISL29028_REG_ADD_CONFIGURE,
318                                         CONFIGURE_ALS_RANGE_HIGH_LUX,
319                                         CONFIGURE_ALS_RANGE_MASK,
320                                         CONFIGURE_ALS_RANGE_SH);
321                         if (st)
322                                 st = isl29018_set_irals_high_threshold(client,
323                                         chip->als_high_thres);
324                         if (st)
325                                 st = isl29018_set_irals_low_threshold(client,
326                                         chip->als_low_thres);
327                 } else {
328                         dev_dbg(&client->dev, "Enabling IR mode\n");
329                         st = isl29028_write_data(client,
330                                 ISL29028_REG_ADD_CONFIGURE,
331                                 CONFIGURE_ALS_IR_MODE_IR,
332                                 CONFIGURE_ALS_IR_MODE_MASK,
333                                 CONFIGURE_ALS_IR_MODE_SH);
334                         if (st)
335                                 st = isl29018_set_irals_high_threshold(client,
336                                         chip->ir_high_thres);
337                         if (st)
338                                 st = isl29018_set_irals_low_threshold(client,
339                                         chip->ir_low_thres);
340                 }
341                 if (st)
342                         st = isl29028_write_data(client,
343                                 ISL29028_REG_ADD_CONFIGURE,
344                                 CONFIGURE_ALS_EN,
345                                 CONFIGURE_ALS_EN_MASK,
346                                 CONFIGURE_ALS_EN_SH);
347         } else {
348                 st = isl29028_write_data(client,
349                         ISL29028_REG_ADD_CONFIGURE,
350                         CONFIGURE_ALS_DIS,
351                         CONFIGURE_ALS_EN_MASK,
352                         CONFIGURE_ALS_EN_SH);
353         }
354         return st;
355 }
356
357 static bool isl29028_read_als_ir(struct i2c_client *client, int *als_ir)
358 {
359         s32 lsb;
360         s32 msb;
361
362         lsb = i2c_smbus_read_byte_data(client, ISL29028_REG_ADD_ALSIR_L);
363         if (lsb < 0) {
364                 dev_err(&client->dev, "Error in reading register %d, error %d\n",
365                                 ISL29028_REG_ADD_ALSIR_L, lsb);
366                 return false;
367         }
368
369         msb = i2c_smbus_read_byte_data(client, ISL29028_REG_ADD_ALSIR_U);
370         if (msb < 0) {
371                 dev_err(&client->dev, "Error in reading register %d, error %d\n",
372                                 ISL29028_REG_ADD_ALSIR_U, lsb);
373                 return false;
374         }
375         *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
376         return true;
377 }
378
379 static bool isl29028_read_proxim(struct i2c_client *client, int *prox)
380 {
381         s32 data;
382
383         data = i2c_smbus_read_byte_data(client, ISL29028_REG_ADD_PROX_DATA);
384         if (data < 0) {
385                 dev_err(&client->dev, "Error in reading register %d, error %d\n",
386                                 ISL29028_REG_ADD_PROX_DATA, data);
387                 return false;
388         }
389         *prox = (int)data;
390         return true;
391 }
392
393 /* Sysfs interface */
394 /* proximity period  */
395 static ssize_t show_prox_period(struct device *dev,
396         struct device_attribute *attr, char *buf)
397 {
398         struct iio_dev *indio_dev = dev_get_drvdata(dev);
399         struct isl29028_chip *chip = indio_dev->dev_data;
400
401         dev_vdbg(dev, "%s()\n", __func__);
402         return sprintf(buf, "%d\n", chip->prox_period);
403 }
404
405 static ssize_t store_prox_period(struct device *dev,
406                 struct device_attribute *attr, const char *buf, size_t count)
407 {
408         struct iio_dev *indio_dev = dev_get_drvdata(dev);
409         struct isl29028_chip *chip = indio_dev->dev_data;
410         struct i2c_client *client = chip->client;
411         bool st;
412         unsigned long lval;
413
414         dev_vdbg(dev, "%s()\n", __func__);
415
416         if (strict_strtoul(buf, 10, &lval))
417                 return -EINVAL;
418
419         mutex_lock(&chip->lock);
420         st = isl29018_set_proxim_period(client, chip->is_prox_enable,
421                                 (int)lval);
422         if (st)
423                 chip->prox_period = (int)lval;
424         else
425                 dev_err(dev, "Error in setting the proximity period\n");
426
427         mutex_unlock(&chip->lock);
428         return count;
429 }
430
431 /* proximity enable/disable  */
432 static ssize_t show_prox_enable(struct device *dev,
433         struct device_attribute *attr, char *buf)
434 {
435         struct iio_dev *indio_dev = dev_get_drvdata(dev);
436         struct isl29028_chip *chip = indio_dev->dev_data;
437
438         dev_vdbg(dev, "%s()\n", __func__);
439         if (chip->is_prox_enable)
440                 return sprintf(buf, "1\n");
441         else
442                 return sprintf(buf, "0\n");
443 }
444
445 static ssize_t store_prox_enable(struct device *dev,
446                 struct device_attribute *attr, const char *buf, size_t count)
447 {
448         struct iio_dev *indio_dev = dev_get_drvdata(dev);
449         struct isl29028_chip *chip = indio_dev->dev_data;
450         struct i2c_client *client = chip->client;
451         bool st;
452         unsigned long lval;
453
454         dev_vdbg(dev, "%s()\n", __func__);
455
456         if (strict_strtoul(buf, 10, &lval))
457                 return -EINVAL;
458         if ((lval != 1) && (lval != 0)) {
459                 dev_err(dev, "illegal value %lu\n", lval);
460                 return -EINVAL;
461         }
462
463         mutex_lock(&chip->lock);
464         if (lval == 1)
465                 st = isl29018_set_proxim_period(client, true,
466                                                         chip->prox_period);
467         else
468                 st = isl29018_set_proxim_period(client, false,
469                                                         chip->prox_period);
470         if (st)
471                 chip->is_prox_enable = (lval) ? true : false;
472         else
473                 dev_err(dev, "Error in enabling proximity\n");
474
475         mutex_unlock(&chip->lock);
476         return count;
477 }
478
479 /* als/ir enable/disable  */
480 static ssize_t show_als_ir_mode(struct device *dev,
481         struct device_attribute *attr, char *buf)
482 {
483         struct iio_dev *indio_dev = dev_get_drvdata(dev);
484         struct isl29028_chip *chip = indio_dev->dev_data;
485
486         dev_vdbg(dev, "%s()\n", __func__);
487         return sprintf(buf, "Current Mode: %d [0:None, 1:ALS, 2:IR]\n",
488                         chip->als_ir_mode);
489 }
490
491 static ssize_t store_als_ir_mode(struct device *dev,
492                 struct device_attribute *attr, const char *buf, size_t count)
493 {
494         struct iio_dev *indio_dev = dev_get_drvdata(dev);
495         struct isl29028_chip *chip = indio_dev->dev_data;
496         struct i2c_client *client = chip->client;
497         bool st;
498         unsigned long lval;
499
500         dev_vdbg(dev, "%s()\n", __func__);
501
502         if (strict_strtoul(buf, 10, &lval))
503                 return -EINVAL;
504         if (lval > 2) {
505                 dev_err(dev, "illegal value %lu\n", lval);
506                 return -EINVAL;
507         }
508
509         mutex_lock(&chip->lock);
510         if (lval == 0)
511                 st = isl29018_set_als_ir_mode(client, false, false);
512         else if (lval == 1)
513                 st = isl29018_set_als_ir_mode(client, true, true);
514         else
515                 st = isl29018_set_als_ir_mode(client, true, false);
516         if (st)
517                 chip->als_ir_mode = (int)lval;
518         else
519                 dev_err(dev, "Error in enabling als/ir mode\n");
520
521         mutex_unlock(&chip->lock);
522         return count;
523 }
524
525 /* Proximity low thresholds  */
526 static ssize_t show_proxim_low_threshold(struct device *dev,
527         struct device_attribute *attr, char *buf)
528 {
529         struct iio_dev *indio_dev = dev_get_drvdata(dev);
530         struct isl29028_chip *chip = indio_dev->dev_data;
531
532         dev_vdbg(dev, "%s()\n", __func__);
533         return sprintf(buf, "%d\n", chip->prox_low_thres);
534 }
535
536 static ssize_t store_proxim_low_threshold(struct device *dev,
537                 struct device_attribute *attr, const char *buf, size_t count)
538 {
539         struct iio_dev *indio_dev = dev_get_drvdata(dev);
540         struct isl29028_chip *chip = indio_dev->dev_data;
541         struct i2c_client *client = chip->client;
542         bool st;
543         unsigned long lval;
544
545         dev_vdbg(dev, "%s()\n", __func__);
546
547         if (strict_strtoul(buf, 10, &lval))
548                 return -EINVAL;
549
550         if ((lval > 0xFF) || (lval < 0x0)) {
551                 dev_err(dev, "The threshold is not supported\n");
552                 return -EINVAL;
553         }
554
555         mutex_lock(&chip->lock);
556         st = isl29018_set_proxim_low_threshold(client, (u8)lval);
557         if (st)
558                 chip->prox_low_thres = (int)lval;
559         else
560                 dev_err(dev, "Error in setting proximity low threshold\n");
561
562         mutex_unlock(&chip->lock);
563         return count;
564 }
565
566 /* Proximity high thresholds  */
567 static ssize_t show_proxim_high_threshold(struct device *dev,
568         struct device_attribute *attr, char *buf)
569 {
570         struct iio_dev *indio_dev = dev_get_drvdata(dev);
571         struct isl29028_chip *chip = indio_dev->dev_data;
572
573         dev_vdbg(dev, "%s()\n", __func__);
574         return sprintf(buf, "%d\n", chip->prox_high_thres);
575 }
576
577 static ssize_t store_proxim_high_threshold(struct device *dev,
578                 struct device_attribute *attr, const char *buf, size_t count)
579 {
580         struct iio_dev *indio_dev = dev_get_drvdata(dev);
581         struct isl29028_chip *chip = indio_dev->dev_data;
582         struct i2c_client *client = chip->client;
583         bool st;
584         unsigned long lval;
585
586         dev_vdbg(dev, "%s()\n", __func__);
587
588         if (strict_strtoul(buf, 10, &lval))
589                 return -EINVAL;
590
591         if ((lval > 0xFF) || (lval < 0x0)) {
592                 dev_err(dev, "The threshold is not supported\n");
593                 return -EINVAL;
594         }
595
596         mutex_lock(&chip->lock);
597         st = isl29018_set_proxim_high_threshold(client, (u8)lval);
598         if (st)
599                 chip->prox_high_thres = (int)lval;
600         else
601                 dev_err(dev, "Error in setting proximity high threshold\n");
602
603         mutex_unlock(&chip->lock);
604         return count;
605 }
606
607 /* als low thresholds  */
608 static ssize_t show_als_low_threshold(struct device *dev,
609         struct device_attribute *attr, char *buf)
610 {
611         struct iio_dev *indio_dev = dev_get_drvdata(dev);
612         struct isl29028_chip *chip = indio_dev->dev_data;
613
614         dev_vdbg(dev, "%s()\n", __func__);
615         return sprintf(buf, "%d\n", chip->als_low_thres);
616 }
617
618 static ssize_t store_als_low_threshold(struct device *dev,
619                 struct device_attribute *attr, const char *buf, size_t count)
620 {
621         struct iio_dev *indio_dev = dev_get_drvdata(dev);
622         struct isl29028_chip *chip = indio_dev->dev_data;
623         struct i2c_client *client = chip->client;
624         bool st;
625         unsigned long lval;
626
627         dev_vdbg(dev, "%s()\n", __func__);
628
629         if (strict_strtoul(buf, 10, &lval))
630                 return -EINVAL;
631
632         if ((lval > 0xFFFF) || (lval < 0x0)) {
633                 dev_err(dev, "The ALS threshold is not supported\n");
634                 return -EINVAL;
635         }
636
637         mutex_lock(&chip->lock);
638         if (chip->als_ir_mode == MODE_ALS) {
639                 st = isl29018_set_irals_low_threshold(client, (int)lval);
640                 if (st)
641                         chip->als_low_thres = (int)lval;
642                 else
643                         dev_err(dev, "Error in setting als low threshold\n");
644         } else
645                 chip->als_low_thres = (int)lval;
646         mutex_unlock(&chip->lock);
647         return count;
648 }
649
650 /* Als high thresholds  */
651 static ssize_t show_als_high_threshold(struct device *dev,
652         struct device_attribute *attr, char *buf)
653 {
654         struct iio_dev *indio_dev = dev_get_drvdata(dev);
655         struct isl29028_chip *chip = indio_dev->dev_data;
656
657         dev_vdbg(dev, "%s()\n", __func__);
658         return sprintf(buf, "%d\n", chip->als_high_thres);
659 }
660
661 static ssize_t store_als_high_threshold(struct device *dev,
662                 struct device_attribute *attr, const char *buf, size_t count)
663 {
664         struct iio_dev *indio_dev = dev_get_drvdata(dev);
665         struct isl29028_chip *chip = indio_dev->dev_data;
666         struct i2c_client *client = chip->client;
667         bool st;
668         unsigned long lval;
669
670         dev_vdbg(dev, "%s()\n", __func__);
671
672         if (strict_strtoul(buf, 10, &lval))
673                 return -EINVAL;
674
675         if ((lval > 0xFFFF) || (lval < 0x0)) {
676                 dev_err(dev, "The als threshold is not supported\n");
677                 return -EINVAL;
678         }
679
680         mutex_lock(&chip->lock);
681         if (chip->als_ir_mode == MODE_ALS) {
682                 st = isl29018_set_irals_high_threshold(client, (int)lval);
683                 if (st)
684                         chip->als_high_thres = (int)lval;
685                 else
686                         dev_err(dev, "Error in setting als high threshold\n");
687         } else
688                 chip->als_high_thres = (int)lval;
689         mutex_unlock(&chip->lock);
690         return count;
691 }
692
693 /* IR low thresholds  */
694 static ssize_t show_ir_low_threshold(struct device *dev,
695         struct device_attribute *attr, char *buf)
696 {
697         struct iio_dev *indio_dev = dev_get_drvdata(dev);
698         struct isl29028_chip *chip = indio_dev->dev_data;
699
700         dev_vdbg(dev, "%s()\n", __func__);
701         return sprintf(buf, "%d\n", chip->ir_low_thres);
702 }
703
704 static ssize_t store_ir_low_threshold(struct device *dev,
705                 struct device_attribute *attr, const char *buf, size_t count)
706 {
707         struct iio_dev *indio_dev = dev_get_drvdata(dev);
708         struct isl29028_chip *chip = indio_dev->dev_data;
709         struct i2c_client *client = chip->client;
710         bool st;
711         unsigned long lval;
712
713         dev_vdbg(dev, "%s()\n", __func__);
714
715         if (strict_strtoul(buf, 10, &lval))
716                 return -EINVAL;
717
718         if ((lval > 0xFFFF) || (lval < 0x0)) {
719                 dev_err(dev, "The IR threshold is not supported\n");
720                 return -EINVAL;
721         }
722
723         mutex_lock(&chip->lock);
724         if (chip->als_ir_mode == MODE_IR) {
725                 st = isl29018_set_irals_low_threshold(client, (int)lval);
726                 if (st)
727                         chip->ir_low_thres = (int)lval;
728                 else
729                         dev_err(dev, "Error in setting als low threshold\n");
730         } else
731                 chip->ir_low_thres = (int)lval;
732         mutex_unlock(&chip->lock);
733         return count;
734 }
735
736 /* IR high thresholds  */
737 static ssize_t show_ir_high_threshold(struct device *dev,
738         struct device_attribute *attr, char *buf)
739 {
740         struct iio_dev *indio_dev = dev_get_drvdata(dev);
741         struct isl29028_chip *chip = indio_dev->dev_data;
742
743         dev_vdbg(dev, "%s()\n", __func__);
744         return sprintf(buf, "%d\n", chip->ir_high_thres);
745 }
746
747 static ssize_t store_ir_high_threshold(struct device *dev,
748                 struct device_attribute *attr, const char *buf, size_t count)
749 {
750         struct iio_dev *indio_dev = dev_get_drvdata(dev);
751         struct isl29028_chip *chip = indio_dev->dev_data;
752         struct i2c_client *client = chip->client;
753         bool st;
754         unsigned long lval;
755
756         dev_vdbg(dev, "%s()\n", __func__);
757
758         if (strict_strtoul(buf, 10, &lval))
759                 return -EINVAL;
760
761         if ((lval > 0xFFFF) || (lval < 0x0)) {
762                 dev_err(dev, "The als threshold is not supported\n");
763                 return -EINVAL;
764         }
765
766         mutex_lock(&chip->lock);
767         if (chip->als_ir_mode == MODE_IR) {
768                 st = isl29018_set_irals_high_threshold(client, (int)lval);
769                 if (st)
770                         chip->ir_high_thres = (int)lval;
771                 else
772                         dev_err(dev, "Error in setting als high threshold\n");
773         } else
774                 chip->ir_high_thres = (int)lval;
775         mutex_unlock(&chip->lock);
776         return count;
777 }
778
779 /* Proximity persist  */
780 static ssize_t show_proxim_persist(struct device *dev,
781         struct device_attribute *attr, char *buf)
782 {
783         struct iio_dev *indio_dev = dev_get_drvdata(dev);
784         struct isl29028_chip *chip = indio_dev->dev_data;
785
786         dev_vdbg(dev, "%s()\n", __func__);
787         return sprintf(buf, "%d\n", chip->prox_persist);
788 }
789
790 static ssize_t store_proxim_persist(struct device *dev,
791                 struct device_attribute *attr, const char *buf, size_t count)
792 {
793         struct iio_dev *indio_dev = dev_get_drvdata(dev);
794         struct isl29028_chip *chip = indio_dev->dev_data;
795         unsigned long lval;
796
797         dev_vdbg(dev, "%s()\n", __func__);
798
799         if (strict_strtoul(buf, 10, &lval))
800                 return -EINVAL;
801
802         if ((lval > 16) || (lval < 0x0)) {
803                 dev_err(dev, "The proximity persist is not supported\n");
804                 return -EINVAL;
805         }
806
807         mutex_lock(&chip->lock);
808         chip->prox_persist = (int)lval;
809         mutex_unlock(&chip->lock);
810         return count;
811 }
812
813 /* als/ir persist  */
814 static ssize_t show_als_persist(struct device *dev,
815         struct device_attribute *attr, char *buf)
816 {
817         struct iio_dev *indio_dev = dev_get_drvdata(dev);
818         struct isl29028_chip *chip = indio_dev->dev_data;
819
820         dev_vdbg(dev, "%s()\n", __func__);
821         return sprintf(buf, "%d\n", chip->als_persist);
822 }
823
824 static ssize_t store_als_persist(struct device *dev,
825                 struct device_attribute *attr, const char *buf, size_t count)
826 {
827         struct iio_dev *indio_dev = dev_get_drvdata(dev);
828         struct isl29028_chip *chip = indio_dev->dev_data;
829         unsigned long lval;
830
831         dev_vdbg(dev, "%s()\n", __func__);
832
833         if (strict_strtoul(buf, 10, &lval))
834                 return -EINVAL;
835
836         if ((lval > 16) || (lval < 0x0)) {
837                 dev_err(dev, "The als persist is not supported\n");
838                 return -EINVAL;
839         }
840
841         mutex_lock(&chip->lock);
842         chip->als_persist = (int)lval;
843         mutex_unlock(&chip->lock);
844         return count;
845 }
846
847 /* Display proxim data  */
848 static ssize_t show_proxim_data(struct device *dev,
849         struct device_attribute *attr, char *buf)
850 {
851         struct iio_dev *indio_dev = dev_get_drvdata(dev);
852         struct isl29028_chip *chip = indio_dev->dev_data;
853         int prox_data;
854         bool st;
855         ssize_t buf_count = 0;
856
857         dev_vdbg(dev, "%s()\n", __func__);
858         mutex_lock(&chip->lock);
859
860         if (chip->is_prox_enable) {
861                 st = isl29028_read_proxim(chip->client, &prox_data);
862                 if (st) {
863                         buf_count = sprintf(buf, "%d\n", prox_data);
864                         chip->prox_reading = prox_data;
865                 }
866         } else
867                 buf_count = sprintf(buf, "%d\n", chip->prox_reading);
868
869         mutex_unlock(&chip->lock);
870         return buf_count;
871 }
872
873 /* Display als data  */
874 static ssize_t show_als_data(struct device *dev,
875         struct device_attribute *attr, char *buf)
876 {
877         struct iio_dev *indio_dev = dev_get_drvdata(dev);
878         struct isl29028_chip *chip = indio_dev->dev_data;
879         int als_ir_data;
880         bool st;
881         ssize_t buf_count = 0;
882
883         dev_vdbg(dev, "%s()\n", __func__);
884         mutex_lock(&chip->lock);
885
886         if (chip->als_ir_mode == MODE_ALS) {
887                 st = isl29028_read_als_ir(chip->client, &als_ir_data);
888                 if (st) {
889                         /* convert als data count to lux */
890                         /* if als_range = 0, lux = count * 0.0326 */
891                         /* if als_range = 1, lux = count * 0.522 */
892                         if (!chip->als_range)
893                                 als_ir_data = (als_ir_data * 326) / 10000;
894                         else
895                                 als_ir_data = (als_ir_data * 522) / 1000;
896
897                         buf_count = sprintf(buf, "%d\n", als_ir_data);
898                         chip->als_reading = als_ir_data;
899                 }
900         } else
901                 buf_count = sprintf(buf, "%d\n", chip->als_reading);
902         mutex_unlock(&chip->lock);
903         return buf_count;
904 }
905
906 /* Display IR data  */
907 static ssize_t show_ir_data(struct device *dev,
908         struct device_attribute *attr, char *buf)
909 {
910         struct iio_dev *indio_dev = dev_get_drvdata(dev);
911         struct isl29028_chip *chip = indio_dev->dev_data;
912         int als_ir_data;
913         bool st;
914         ssize_t buf_count = 0;
915
916         dev_vdbg(dev, "%s()\n", __func__);
917         mutex_lock(&chip->lock);
918
919         if (chip->als_ir_mode == MODE_IR) {
920                 st = isl29028_read_als_ir(chip->client, &als_ir_data);
921                 if (st) {
922                         buf_count = sprintf(buf, "%d\n", als_ir_data);
923                         chip->ir_reading = als_ir_data;
924                 }
925         } else
926                 buf_count = sprintf(buf, "%d\n", chip->ir_reading);
927         mutex_unlock(&chip->lock);
928         return buf_count;
929 }
930
931 /* Wait for the proximity threshold interrupt*/
932 static ssize_t show_wait_proxim_int(struct device *dev,
933         struct device_attribute *attr, char *buf)
934 {
935         struct iio_dev *indio_dev = dev_get_drvdata(dev);
936         struct isl29028_chip *chip = indio_dev->dev_data;
937         struct i2c_client *client = chip->client;
938         bool st;
939
940         dev_vdbg(dev, "%s()\n", __func__);
941         if (!chip->is_int_enable) {
942                 dev_err(dev, "%s() Interrupt mode not supported\n", __func__);
943                 return sprintf(buf, "error\n");
944         }
945
946         mutex_lock(&chip->lock);
947         st = isl29018_set_proxim_persist(client, true, chip->prox_persist);
948         if (!st) {
949                 dev_err(dev, "%s() Error in configuration\n", __func__);
950                 mutex_unlock(&chip->lock);
951                 return sprintf(buf, "error\n");
952         }
953
954         chip->is_proxim_int_waiting =  true;
955         mutex_unlock(&chip->lock);
956         wait_for_completion(&chip->prox_completion);
957         mutex_lock(&chip->lock);
958         chip->is_proxim_int_waiting =  false;
959         isl29018_set_proxim_persist(client, false, chip->prox_persist);
960         mutex_unlock(&chip->lock);
961         return sprintf(buf, "done\n");
962 }
963
964 /* Wait for the als/ir interrupt*/
965 static ssize_t show_wait_als_ir_int(struct device *dev,
966         struct device_attribute *attr, char *buf)
967 {
968         struct iio_dev *indio_dev = dev_get_drvdata(dev);
969         struct isl29028_chip *chip = indio_dev->dev_data;
970         struct i2c_client *client = chip->client;
971         bool st;
972
973         dev_vdbg(dev, "%s()\n", __func__);
974         if (!chip->is_int_enable) {
975                 dev_err(dev, "%s() Interrupt mode not supported\n", __func__);
976                 return sprintf(buf, "error\n");
977         }
978
979         mutex_lock(&chip->lock);
980
981         st = isl29018_set_als_persist(client, true, chip->als_persist);
982         if (!st) {
983                 dev_err(dev, "%s() Error in als ir int configuration\n",
984                                          __func__);
985                 mutex_unlock(&chip->lock);
986                 return sprintf(buf, "error\n");
987         }
988
989         chip->is_als_int_waiting =  true;
990         mutex_unlock(&chip->lock);
991         wait_for_completion(&chip->als_completion);
992         mutex_lock(&chip->lock);
993         chip->is_als_int_waiting =  false;
994         st = isl29018_set_als_persist(client, false, chip->als_persist);
995         mutex_unlock(&chip->lock);
996         return sprintf(buf, "done\n");
997 }
998
999 /* Read name */
1000 static ssize_t show_name(struct device *dev,
1001         struct device_attribute *attr, char *buf)
1002 {
1003         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1004         struct isl29028_chip *chip = indio_dev->dev_data;
1005         return sprintf(buf, "%s\n", chip->client->name);
1006 }
1007
1008 static IIO_DEVICE_ATTR(proximity_low_threshold, S_IRUGO | S_IWUSR,
1009                 show_proxim_low_threshold, store_proxim_low_threshold, 0);
1010 static IIO_DEVICE_ATTR(proximity_high_threshold, S_IRUGO | S_IWUSR,
1011                 show_proxim_high_threshold, store_proxim_high_threshold, 0);
1012 static IIO_DEVICE_ATTR(proximity_persist, S_IRUGO | S_IWUSR,
1013                 show_proxim_persist, store_proxim_persist, 0);
1014 static IIO_DEVICE_ATTR(proximity_period, S_IRUGO | S_IWUSR,
1015                 show_prox_period, store_prox_period, 0);
1016 static IIO_DEVICE_ATTR(proximity_enable, S_IRUGO | S_IWUSR,
1017                 show_prox_enable, store_prox_enable, 0);
1018 static IIO_DEVICE_ATTR(wait_proxim_thres, S_IRUGO,
1019                 show_wait_proxim_int, NULL, 0);
1020 static IIO_DEVICE_ATTR(proximity_value, S_IRUGO,
1021                 show_proxim_data, NULL, 0);
1022
1023 static IIO_DEVICE_ATTR(als_low_threshold, S_IRUGO | S_IWUSR,
1024                 show_als_low_threshold, store_als_low_threshold, 0);
1025 static IIO_DEVICE_ATTR(als_high_threshold, S_IRUGO | S_IWUSR,
1026                 show_als_high_threshold, store_als_high_threshold, 0);
1027 static IIO_DEVICE_ATTR(als_persist, S_IRUGO | S_IWUSR,
1028                 show_als_persist, store_als_persist, 0);
1029 static IIO_DEVICE_ATTR(als_ir_mode, S_IRUGO | S_IWUSR,
1030                 show_als_ir_mode, store_als_ir_mode, 0);
1031 static IIO_DEVICE_ATTR(als_value, S_IRUGO,
1032                 show_als_data, NULL, 0);
1033 static IIO_DEVICE_ATTR(wait_als_ir_thres, S_IRUGO,
1034                 show_wait_als_ir_int, NULL, 0);
1035
1036 static IIO_DEVICE_ATTR(ir_value, S_IRUGO,
1037                 show_ir_data, NULL, 0);
1038 static IIO_DEVICE_ATTR(ir_low_threshold, S_IRUGO | S_IWUSR,
1039                 show_ir_low_threshold, store_ir_low_threshold, 0);
1040 static IIO_DEVICE_ATTR(ir_high_threshold, S_IRUGO | S_IWUSR,
1041                 show_ir_high_threshold, store_ir_high_threshold, 0);
1042
1043 static IIO_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
1044
1045 static struct attribute *isl29028_attributes[] = {
1046         &iio_dev_attr_name.dev_attr.attr,
1047
1048         &iio_dev_attr_ir_value.dev_attr.attr,
1049
1050         &iio_dev_attr_als_low_threshold.dev_attr.attr,
1051         &iio_dev_attr_als_high_threshold.dev_attr.attr,
1052         &iio_dev_attr_als_persist.dev_attr.attr,
1053         &iio_dev_attr_als_ir_mode.dev_attr.attr,
1054         &iio_dev_attr_als_value.dev_attr.attr,
1055         &iio_dev_attr_wait_als_ir_thres.dev_attr.attr,
1056         &iio_dev_attr_ir_low_threshold.dev_attr.attr,
1057         &iio_dev_attr_ir_high_threshold.dev_attr.attr,
1058
1059         &iio_dev_attr_proximity_low_threshold.dev_attr.attr,
1060         &iio_dev_attr_proximity_high_threshold.dev_attr.attr,
1061         &iio_dev_attr_proximity_enable.dev_attr.attr,
1062         &iio_dev_attr_proximity_period.dev_attr.attr,
1063         &iio_dev_attr_proximity_persist.dev_attr.attr,
1064         &iio_dev_attr_proximity_value.dev_attr.attr,
1065         &iio_dev_attr_wait_proxim_thres.dev_attr.attr,
1066         NULL
1067 };
1068
1069 static const struct attribute_group isl29108_group = {
1070         .attrs = isl29028_attributes,
1071 };
1072
1073 static int isl29028_chip_init(struct i2c_client *client)
1074 {
1075         struct isl29028_chip *chip = i2c_get_clientdata(client);
1076         int i;
1077         bool st;
1078
1079         for (i = 0; i < ARRAY_SIZE(chip->reg_cache); i++)
1080                 chip->reg_cache[i] = 0;
1081
1082         chip->is_prox_enable  = 0;
1083         chip->prox_low_thres = 0;
1084         chip->prox_high_thres = 0xFF;
1085         chip->prox_period = 0;
1086         chip->prox_reading = 0;
1087
1088         chip->als_low_thres = 0;
1089         chip->als_high_thres = 0xFFF;
1090         chip->als_range = 1;
1091         chip->als_reading = 0;
1092         chip->als_ir_mode = 0;
1093
1094         chip->ir_high_thres = 0xFFF;
1095         chip->ir_low_thres = 0;
1096         chip->ir_reading = 0;
1097
1098         chip->is_int_enable = false;
1099         chip->prox_persist = 1;
1100         chip->als_persist = 1;
1101         chip->is_proxim_int_waiting = false;
1102         chip->is_als_int_waiting = false;
1103
1104         st = isl29028_write_data(client, ISL29028_REG_ADD_TEST1_MODE,
1105                                         0x0, 0xFF, 0);
1106         if (st)
1107                 st = isl29028_write_data(client, ISL29028_REG_ADD_TEST2_MODE,
1108                                         0x0, 0xFF, 0);
1109         if (st)
1110                 st = isl29028_write_data(client, ISL29028_REG_ADD_CONFIGURE,
1111                                         0x0, 0xFF, 0);
1112         if (st)
1113                 msleep(1);
1114         if (!st) {
1115                 dev_err(&client->dev, "%s(): fails\n", __func__);
1116                 return -ENODEV;
1117         }
1118         return 0;
1119 }
1120
1121 static irqreturn_t threshold_isr(int irq, void *irq_data)
1122 {
1123         struct isl29028_chip *chip = (struct isl29028_chip *)irq_data;
1124         s32 int_reg;
1125         struct i2c_client *client = chip->client;
1126
1127         int_reg = i2c_smbus_read_byte_data(client, ISL29028_REG_ADD_INTERRUPT);
1128         if (int_reg < 0) {
1129                 dev_err(&client->dev, "Error in reading register %d, error %d\n",
1130                                 ISL29028_REG_ADD_INTERRUPT, int_reg);
1131                 return IRQ_HANDLED;
1132         }
1133
1134         if (int_reg & INTERRUPT_PROX_FLAG_MASK) {
1135                 /* Write 0 to clear */
1136                 isl29028_write_data(client,
1137                         ISL29028_REG_ADD_INTERRUPT, INTERRUPT_PROX_FLAG_DIS,
1138                         INTERRUPT_PROX_FLAG_MASK, INTERRUPT_PROX_FLAG_SH);
1139                 if (chip->is_proxim_int_waiting)
1140                         complete(&chip->prox_completion);
1141         }
1142
1143         if (int_reg & INTERRUPT_ALS_FLAG_MASK) {
1144                 /* Write 0 to clear */
1145                 isl29028_write_data(client,
1146                         ISL29028_REG_ADD_INTERRUPT, INTERRUPT_ALS_FLAG_DIS,
1147                         INTERRUPT_ALS_FLAG_MASK, INTERRUPT_ALS_FLAG_SH);
1148                 if (chip->is_als_int_waiting)
1149                         complete(&chip->als_completion);
1150         }
1151
1152         return IRQ_HANDLED;
1153 }
1154
1155 static int __devinit isl29028_probe(struct i2c_client *client,
1156         const struct i2c_device_id *id)
1157 {
1158         struct isl29028_chip *chip;
1159         int err;
1160
1161         dev_dbg(&client->dev, "%s() called\n", __func__);
1162
1163         chip = kzalloc(sizeof(struct isl29028_chip), GFP_KERNEL);
1164         if (!chip) {
1165                 dev_err(&client->dev, "Memory allocation fails\n");
1166                 err = -ENOMEM;
1167                 goto exit;
1168         }
1169
1170         i2c_set_clientdata(client, chip);
1171         chip->client = client;
1172         chip->irq = client->irq;
1173
1174         mutex_init(&chip->lock);
1175
1176         err = isl29028_chip_init(client);
1177         if (err)
1178                 goto exit_free;
1179
1180         init_completion(&chip->prox_completion);
1181         init_completion(&chip->als_completion);
1182
1183         if (chip->irq > 0) {
1184                 err = request_threaded_irq(chip->irq, NULL, threshold_isr,
1185                                                 IRQF_SHARED, "ISL29028", chip);
1186                 if (err) {
1187                         dev_err(&client->dev, "Unable to register irq %d; "
1188                                 "error %d\n", chip->irq, err);
1189                         goto exit_free;
1190                 }
1191         }
1192
1193         chip->is_int_enable = true;
1194         chip->indio_dev = iio_allocate_device();
1195         if (!chip->indio_dev) {
1196                 dev_err(&client->dev, "iio allocation fails\n");
1197                 goto exit_irq;
1198         }
1199
1200         chip->indio_dev->attrs = &isl29108_group;
1201         chip->indio_dev->dev.parent = &client->dev;
1202         chip->indio_dev->dev_data = (void *)(chip);
1203         chip->indio_dev->driver_module = THIS_MODULE;
1204         chip->indio_dev->modes = INDIO_DIRECT_MODE;
1205         err = iio_device_register(chip->indio_dev);
1206         if (err) {
1207                 dev_err(&client->dev, "iio registration fails\n");
1208                 goto exit_iio_free;
1209         }
1210         dev_dbg(&client->dev, "%s() success\n", __func__);
1211         return 0;
1212
1213 exit_iio_free:
1214         iio_free_device(chip->indio_dev);
1215 exit_irq:
1216         if (chip->irq > 0)
1217                 free_irq(chip->irq, chip);
1218 exit_free:
1219         kfree(chip);
1220 exit:
1221         return err;
1222 }
1223
1224 static int __devexit isl29028_remove(struct i2c_client *client)
1225 {
1226         struct isl29028_chip *chip = i2c_get_clientdata(client);
1227
1228         dev_dbg(&client->dev, "%s()\n", __func__);
1229         iio_device_unregister(chip->indio_dev);
1230         if (chip->irq > 0)
1231                 free_irq(chip->irq, chip);
1232         kfree(chip);
1233         return 0;
1234 }
1235
1236 static const struct i2c_device_id isl29028_id[] = {
1237         {"isl29028", 0},
1238         {}
1239 };
1240
1241 MODULE_DEVICE_TABLE(i2c, isl29028_id);
1242
1243 static struct i2c_driver isl29028_driver = {
1244         .class  = I2C_CLASS_HWMON,
1245         .driver  = {
1246                 .name = "isl29028",
1247                 .owner = THIS_MODULE,
1248         },
1249         .probe   = isl29028_probe,
1250         .remove  = __devexit_p(isl29028_remove),
1251         .id_table = isl29028_id,
1252 };
1253
1254 static int __init isl29028_init(void)
1255 {
1256         return i2c_add_driver(&isl29028_driver);
1257 }
1258
1259 static void __exit isl29028_exit(void)
1260 {
1261         i2c_del_driver(&isl29028_driver);
1262 }
1263
1264 module_init(isl29028_init);
1265 module_exit(isl29028_exit);