Merge remote branch 'nouveau/for-airlied' of nouveau-2.6
[pandora-kernel.git] / drivers / staging / iio / light / tsl2563.c
1 /*
2  * drivers/i2c/chips/tsl2563.c
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
7  * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
8  *
9  * Converted to IIO driver
10  * Amit Kucheria <amit.kucheria@verdurent.com>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * version 2 as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24  * 02110-1301 USA
25  */
26
27 #include <linux/module.h>
28 #include <linux/i2c.h>
29 #include <linux/interrupt.h>
30 #include <linux/sched.h>
31 #include <linux/mutex.h>
32 #include <linux/delay.h>
33 #include <linux/platform_device.h>
34 #include <linux/pm.h>
35 #include <linux/hwmon.h>
36 #include <linux/err.h>
37
38 #include "../iio.h"
39 #include "tsl2563.h"
40
41 /* Use this many bits for fraction part. */
42 #define ADC_FRAC_BITS           (14)
43
44 /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
45 #define FRAC10K(f)              (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
46
47 /* Bits used for fraction in calibration coefficients.*/
48 #define CALIB_FRAC_BITS         (10)
49 /* 0.5 in CALIB_FRAC_BITS precision */
50 #define CALIB_FRAC_HALF         (1 << (CALIB_FRAC_BITS - 1))
51 /* Make a fraction from a number n that was multiplied with b. */
52 #define CALIB_FRAC(n, b)        (((n) << CALIB_FRAC_BITS) / (b))
53 /* Decimal 10^(digits in sysfs presentation) */
54 #define CALIB_BASE_SYSFS        (1000)
55
56 #define TSL2563_CMD             (0x80)
57 #define TSL2563_CLEARINT        (0x40)
58
59 #define TSL2563_REG_CTRL        (0x00)
60 #define TSL2563_REG_TIMING      (0x01)
61 #define TSL2563_REG_LOWLOW      (0x02) /* data0 low threshold, 2 bytes */
62 #define TSL2563_REG_LOWHIGH     (0x03)
63 #define TSL2563_REG_HIGHLOW     (0x04) /* data0 high threshold, 2 bytes */
64 #define TSL2563_REG_HIGHHIGH    (0x05)
65 #define TSL2563_REG_INT         (0x06)
66 #define TSL2563_REG_ID          (0x0a)
67 #define TSL2563_REG_DATA0LOW    (0x0c) /* broadband sensor value, 2 bytes */
68 #define TSL2563_REG_DATA0HIGH   (0x0d)
69 #define TSL2563_REG_DATA1LOW    (0x0e) /* infrared sensor value, 2 bytes */
70 #define TSL2563_REG_DATA1HIGH   (0x0f)
71
72 #define TSL2563_CMD_POWER_ON    (0x03)
73 #define TSL2563_CMD_POWER_OFF   (0x00)
74 #define TSL2563_CTRL_POWER_MASK (0x03)
75
76 #define TSL2563_TIMING_13MS     (0x00)
77 #define TSL2563_TIMING_100MS    (0x01)
78 #define TSL2563_TIMING_400MS    (0x02)
79 #define TSL2563_TIMING_MASK     (0x03)
80 #define TSL2563_TIMING_GAIN16   (0x10)
81 #define TSL2563_TIMING_GAIN1    (0x00)
82
83 #define TSL2563_INT_DISBLED     (0x00)
84 #define TSL2563_INT_LEVEL       (0x10)
85 #define TSL2563_INT_PERSIST(n)  ((n) & 0x0F)
86
87 struct tsl2563_gainlevel_coeff {
88         u8 gaintime;
89         u16 min;
90         u16 max;
91 };
92
93 static struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
94         {
95                 .gaintime       = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
96                 .min            = 0,
97                 .max            = 65534,
98         }, {
99                 .gaintime       = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
100                 .min            = 2048,
101                 .max            = 65534,
102         }, {
103                 .gaintime       = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
104                 .min            = 4095,
105                 .max            = 37177,
106         }, {
107                 .gaintime       = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
108                 .min            = 3000,
109                 .max            = 65535,
110         },
111 };
112
113 struct tsl2563_chip {
114         struct mutex            lock;
115         struct i2c_client       *client;
116         struct iio_dev          *indio_dev;
117         struct delayed_work     poweroff_work;
118
119         /* Remember state for suspend and resume functions */
120         pm_message_t            state;
121
122         struct tsl2563_gainlevel_coeff *gainlevel;
123
124         /* Thresholds are in lux */
125         u16                     low_thres;
126         u16                     high_thres;
127         u8                      intr;
128
129         /* Calibration coefficients */
130         u32                     calib0;
131         u32                     calib1;
132         int                     cover_comp_gain;
133
134         /* Cache current values, to be returned while suspended */
135         u32                     data0;
136         u32                     data1;
137 };
138
139 static int tsl2563_write(struct i2c_client *client, u8 reg, u8 value)
140 {
141         int ret;
142         u8 buf[2];
143
144         buf[0] = TSL2563_CMD | reg;
145         buf[1] = value;
146
147         ret = i2c_master_send(client, buf, sizeof(buf));
148         return (ret == sizeof(buf)) ? 0 : ret;
149 }
150
151 static int tsl2563_read(struct i2c_client *client, u8 reg, void *buf, int len)
152 {
153         int ret;
154         u8 cmd = TSL2563_CMD | reg;
155
156         ret = i2c_master_send(client, &cmd, sizeof(cmd));
157         if (ret != sizeof(cmd))
158                 return ret;
159
160         return i2c_master_recv(client, buf, len);
161 }
162
163 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
164 {
165         struct i2c_client *client = chip->client;
166         u8 cmd;
167
168         cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
169         return tsl2563_write(client, TSL2563_REG_CTRL, cmd);
170 }
171
172 /*
173  * Return value is 0 for off, 1 for on, or a negative error
174  * code if reading failed.
175  */
176 static int tsl2563_get_power(struct tsl2563_chip *chip)
177 {
178         struct i2c_client *client = chip->client;
179         int ret;
180         u8 val;
181
182         ret = tsl2563_read(client, TSL2563_REG_CTRL, &val, sizeof(val));
183         if (ret != sizeof(val))
184                 return ret;
185
186         return (val & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
187 }
188
189 static int tsl2563_configure(struct tsl2563_chip *chip)
190 {
191         struct i2c_client *client = chip->client;
192         int ret;
193
194         ret = tsl2563_write(client, TSL2563_REG_TIMING,
195                         chip->gainlevel->gaintime);
196         if (ret)
197                 goto out;
198
199         ret = tsl2563_write(client, TSL2563_REG_INT, chip->intr);
200
201 out:
202         return ret;
203 }
204
205 static void tsl2563_poweroff_work(struct work_struct *work)
206 {
207         struct tsl2563_chip *chip =
208                 container_of(work, struct tsl2563_chip, poweroff_work.work);
209         tsl2563_set_power(chip, 0);
210 }
211
212 static int tsl2563_detect(struct tsl2563_chip *chip)
213 {
214         int ret;
215
216         ret = tsl2563_set_power(chip, 1);
217         if (ret)
218                 return ret;
219
220         ret = tsl2563_get_power(chip);
221         if (ret < 0)
222                 return ret;
223
224         return ret ? 0 : -ENODEV;
225 }
226
227 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
228 {
229         struct i2c_client *client = chip->client;
230         int ret;
231
232         ret = tsl2563_read(client, TSL2563_REG_ID, id, sizeof(*id));
233         if (ret != sizeof(*id))
234                 return ret;
235
236         return 0;
237 }
238
239 /*
240  * "Normalized" ADC value is one obtained with 400ms of integration time and
241  * 16x gain. This function returns the number of bits of shift needed to
242  * convert between normalized values and HW values obtained using given
243  * timing and gain settings.
244  */
245 static int adc_shiftbits(u8 timing)
246 {
247         int shift = 0;
248
249         switch (timing & TSL2563_TIMING_MASK) {
250         case TSL2563_TIMING_13MS:
251                 shift += 5;
252                 break;
253         case TSL2563_TIMING_100MS:
254                 shift += 2;
255                 break;
256         case TSL2563_TIMING_400MS:
257                 /* no-op */
258                 break;
259         }
260
261         if (!(timing & TSL2563_TIMING_GAIN16))
262                 shift += 4;
263
264         return shift;
265 }
266
267 /* Convert a HW ADC value to normalized scale. */
268 static u32 normalize_adc(u16 adc, u8 timing)
269 {
270         return adc << adc_shiftbits(timing);
271 }
272
273 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
274 {
275         unsigned int delay;
276
277         switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
278         case TSL2563_TIMING_13MS:
279                 delay = 14;
280                 break;
281         case TSL2563_TIMING_100MS:
282                 delay = 101;
283                 break;
284         default:
285                 delay = 402;
286         }
287         /*
288          * TODO: Make sure that we wait at least required delay but why we
289          * have to extend it one tick more?
290          */
291         schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
292 }
293
294 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
295 {
296         struct i2c_client *client = chip->client;
297
298         if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
299
300                 (adc > chip->gainlevel->max) ?
301                         chip->gainlevel++ : chip->gainlevel--;
302
303                 tsl2563_write(client, TSL2563_REG_TIMING,
304                               chip->gainlevel->gaintime);
305
306                 tsl2563_wait_adc(chip);
307                 tsl2563_wait_adc(chip);
308
309                 return 1;
310         } else
311                 return 0;
312 }
313
314 static int tsl2563_get_adc(struct tsl2563_chip *chip)
315 {
316         struct i2c_client *client = chip->client;
317         u8 buf0[2], buf1[2];
318         u16 adc0, adc1;
319         int retry = 1;
320         int ret = 0;
321
322         if (chip->state.event != PM_EVENT_ON)
323                 goto out;
324
325         cancel_delayed_work(&chip->poweroff_work);
326
327         if (!tsl2563_get_power(chip)) {
328                 ret = tsl2563_set_power(chip, 1);
329                 if (ret)
330                         goto out;
331                 ret = tsl2563_configure(chip);
332                 if (ret)
333                         goto out;
334                 tsl2563_wait_adc(chip);
335         }
336
337         while (retry) {
338                 ret = tsl2563_read(client,
339                                    TSL2563_REG_DATA0LOW | TSL2563_CLEARINT,
340                                    buf0, sizeof(buf0));
341                 if (ret != sizeof(buf0))
342                         goto out;
343
344                 ret = tsl2563_read(client, TSL2563_REG_DATA1LOW,
345                                    buf1, sizeof(buf1));
346                 if (ret != sizeof(buf1))
347                         goto out;
348
349                 adc0 = (buf0[1] << 8) + buf0[0];
350                 adc1 = (buf1[1] << 8) + buf1[0];
351
352                 retry = tsl2563_adjust_gainlevel(chip, adc0);
353         }
354
355         chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
356         chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
357
358         schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
359
360         ret = 0;
361 out:
362         return ret;
363 }
364
365 static inline int calib_to_sysfs(u32 calib)
366 {
367         return (int) (((calib * CALIB_BASE_SYSFS) +
368                        CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
369 }
370
371 static inline u32 calib_from_sysfs(int value)
372 {
373         return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
374 }
375
376 /*
377  * Conversions between lux and ADC values.
378  *
379  * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
380  * appropriate constants. Different constants are needed for different
381  * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
382  * of the intensities in infrared and visible wavelengths). lux_table below
383  * lists the upper threshold of the adc1/adc0 ratio and the corresponding
384  * constants.
385  */
386
387 struct tsl2563_lux_coeff {
388         unsigned long ch_ratio;
389         unsigned long ch0_coeff;
390         unsigned long ch1_coeff;
391 };
392
393 static const struct tsl2563_lux_coeff lux_table[] = {
394         {
395                 .ch_ratio       = FRAC10K(1300),
396                 .ch0_coeff      = FRAC10K(315),
397                 .ch1_coeff      = FRAC10K(262),
398         }, {
399                 .ch_ratio       = FRAC10K(2600),
400                 .ch0_coeff      = FRAC10K(337),
401                 .ch1_coeff      = FRAC10K(430),
402         }, {
403                 .ch_ratio       = FRAC10K(3900),
404                 .ch0_coeff      = FRAC10K(363),
405                 .ch1_coeff      = FRAC10K(529),
406         }, {
407                 .ch_ratio       = FRAC10K(5200),
408                 .ch0_coeff      = FRAC10K(392),
409                 .ch1_coeff      = FRAC10K(605),
410         }, {
411                 .ch_ratio       = FRAC10K(6500),
412                 .ch0_coeff      = FRAC10K(229),
413                 .ch1_coeff      = FRAC10K(291),
414         }, {
415                 .ch_ratio       = FRAC10K(8000),
416                 .ch0_coeff      = FRAC10K(157),
417                 .ch1_coeff      = FRAC10K(180),
418         }, {
419                 .ch_ratio       = FRAC10K(13000),
420                 .ch0_coeff      = FRAC10K(34),
421                 .ch1_coeff      = FRAC10K(26),
422         }, {
423                 .ch_ratio       = ULONG_MAX,
424                 .ch0_coeff      = 0,
425                 .ch1_coeff      = 0,
426         },
427 };
428
429 /*
430  * Convert normalized, scaled ADC values to lux.
431  */
432 static unsigned int adc_to_lux(u32 adc0, u32 adc1)
433 {
434         const struct tsl2563_lux_coeff *lp = lux_table;
435         unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
436
437         ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
438
439         while (lp->ch_ratio < ratio)
440                 lp++;
441
442         lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
443
444         return (unsigned int) (lux >> ADC_FRAC_BITS);
445 }
446
447 /*--------------------------------------------------------------*/
448 /*                      Sysfs interface                         */
449 /*--------------------------------------------------------------*/
450
451 static ssize_t tsl2563_adc0_show(struct device *dev,
452                                 struct device_attribute *attr, char *buf)
453 {
454         struct iio_dev *indio_dev = dev_get_drvdata(dev);
455         struct tsl2563_chip *chip = indio_dev->dev_data;
456         int ret;
457
458         mutex_lock(&chip->lock);
459
460         ret = tsl2563_get_adc(chip);
461         if (ret)
462                 goto out;
463
464         ret = snprintf(buf, PAGE_SIZE, "%d\n", chip->data0);
465 out:
466         mutex_unlock(&chip->lock);
467         return ret;
468 }
469
470 static ssize_t tsl2563_adc1_show(struct device *dev,
471                                 struct device_attribute *attr, char *buf)
472 {
473         struct iio_dev *indio_dev = dev_get_drvdata(dev);
474         struct tsl2563_chip *chip = indio_dev->dev_data;
475         int ret;
476
477         mutex_lock(&chip->lock);
478
479         ret = tsl2563_get_adc(chip);
480         if (ret)
481                 goto out;
482
483         ret = snprintf(buf, PAGE_SIZE, "%d\n", chip->data1);
484 out:
485         mutex_unlock(&chip->lock);
486         return ret;
487 }
488
489 /* Apply calibration coefficient to ADC count. */
490 static u32 calib_adc(u32 adc, u32 calib)
491 {
492         unsigned long scaled = adc;
493
494         scaled *= calib;
495         scaled >>= CALIB_FRAC_BITS;
496
497         return (u32) scaled;
498 }
499
500 static ssize_t tsl2563_lux_show(struct device *dev,
501                                 struct device_attribute *attr, char *buf)
502 {
503         struct iio_dev *indio_dev = dev_get_drvdata(dev);
504         struct tsl2563_chip *chip = indio_dev->dev_data;
505         u32 calib0, calib1;
506         int ret;
507
508         mutex_lock(&chip->lock);
509
510         ret = tsl2563_get_adc(chip);
511         if (ret)
512                 goto out;
513
514         calib0 = calib_adc(chip->data0, chip->calib0) * chip->cover_comp_gain;
515         calib1 = calib_adc(chip->data1, chip->calib1) * chip->cover_comp_gain;
516
517         ret = snprintf(buf, PAGE_SIZE, "%d\n", adc_to_lux(calib0, calib1));
518
519 out:
520         mutex_unlock(&chip->lock);
521         return ret;
522 }
523
524 static ssize_t format_calib(char *buf, int len, u32 calib)
525 {
526         return snprintf(buf, PAGE_SIZE, "%d\n", calib_to_sysfs(calib));
527 }
528
529 static ssize_t tsl2563_calib0_show(struct device *dev,
530                                    struct device_attribute *attr, char *buf)
531 {
532         struct iio_dev *indio_dev = dev_get_drvdata(dev);
533         struct tsl2563_chip *chip = indio_dev->dev_data;
534         int ret;
535
536         mutex_lock(&chip->lock);
537         ret = format_calib(buf, PAGE_SIZE, chip->calib0);
538         mutex_unlock(&chip->lock);
539         return ret;
540 }
541
542 static ssize_t tsl2563_calib1_show(struct device *dev,
543                                    struct device_attribute *attr, char *buf)
544 {
545         struct iio_dev *indio_dev = dev_get_drvdata(dev);
546         struct tsl2563_chip *chip = indio_dev->dev_data;
547         int ret;
548
549         mutex_lock(&chip->lock);
550         ret = format_calib(buf, PAGE_SIZE, chip->calib1);
551         mutex_unlock(&chip->lock);
552         return ret;
553 }
554
555 static int do_calib_store(struct device *dev, const char *buf, size_t len,
556                           int ch)
557 {
558         struct iio_dev *indio_dev = dev_get_drvdata(dev);
559         struct tsl2563_chip *chip = indio_dev->dev_data;
560         int value;
561         u32 calib;
562
563         if (1 != sscanf(buf, "%d", &value))
564                 return -EINVAL;
565
566         calib = calib_from_sysfs(value);
567
568         if (ch)
569                 chip->calib1 = calib;
570         else
571                 chip->calib0 = calib;
572
573         return len;
574 }
575
576 static ssize_t tsl2563_calib0_store(struct device *dev,
577                                     struct device_attribute *attr,
578                                     const char *buf, size_t len)
579 {
580         return do_calib_store(dev, buf, len, 0);
581 }
582
583 static ssize_t tsl2563_calib1_store(struct device *dev,
584                                     struct device_attribute *attr,
585                                     const char *buf, size_t len)
586 {
587         return do_calib_store(dev, buf, len, 1);
588 }
589
590 /* AmitXXXX: Convert to IIO_DEV_ATTR_LIGHT* as in tsl2561
591  * once I understand what they mean */
592 static DEVICE_ATTR(adc0, S_IRUGO, tsl2563_adc0_show, NULL);
593 static DEVICE_ATTR(adc1, S_IRUGO, tsl2563_adc1_show, NULL);
594 static DEVICE_ATTR(lux, S_IRUGO, tsl2563_lux_show, NULL);
595 static DEVICE_ATTR(calib0, S_IRUGO | S_IWUSR,
596                    tsl2563_calib0_show, tsl2563_calib0_store);
597 static DEVICE_ATTR(calib1, S_IRUGO | S_IWUSR,
598                    tsl2563_calib1_show, tsl2563_calib1_store);
599
600 static struct attribute *tsl2563_attributes[] = {
601         &dev_attr_adc0.attr,
602         &dev_attr_adc1.attr,
603         &dev_attr_lux.attr,
604         &dev_attr_calib0.attr,
605         &dev_attr_calib1.attr,
606         NULL
607 };
608
609 static const struct attribute_group tsl2563_group = {
610         .attrs = tsl2563_attributes,
611 };
612
613 /*--------------------------------------------------------------*/
614 /*                      Probe, Attach, Remove                   */
615 /*--------------------------------------------------------------*/
616 static struct i2c_driver tsl2563_i2c_driver;
617
618 static int __devinit tsl2563_probe(struct i2c_client *client,
619                                 const struct i2c_device_id *device_id)
620 {
621         struct tsl2563_chip *chip;
622         struct tsl2563_platform_data *pdata = client->dev.platform_data;
623         int err = 0;
624         int ret;
625         u8 id;
626
627         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
628         if (!chip)
629                 return -ENOMEM;
630
631         i2c_set_clientdata(client, chip);
632         chip->client = client;
633
634         err = tsl2563_detect(chip);
635         if (err) {
636                 dev_err(&client->dev, "device not found, error %d \n", -err);
637                 goto fail1;
638         }
639
640         err = tsl2563_read_id(chip, &id);
641         if (err)
642                 goto fail1;
643
644         mutex_init(&chip->lock);
645
646         /* Default values used until userspace says otherwise */
647         chip->low_thres = 0x0;
648         chip->high_thres = 0xffff;
649         chip->gainlevel = tsl2563_gainlevel_table;
650         chip->intr = TSL2563_INT_PERSIST(4);
651         chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
652         chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
653
654         if (pdata)
655                 chip->cover_comp_gain = pdata->cover_comp_gain;
656         else
657                 chip->cover_comp_gain = 1;
658
659         dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
660
661         chip->indio_dev = iio_allocate_device();
662         if (!chip->indio_dev)
663                 goto fail1;
664         chip->indio_dev->attrs = &tsl2563_group;
665         chip->indio_dev->dev.parent = &client->dev;
666         chip->indio_dev->dev_data = (void *)(chip);
667         chip->indio_dev->driver_module = THIS_MODULE;
668         chip->indio_dev->modes = INDIO_DIRECT_MODE;
669         ret = iio_device_register(chip->indio_dev);
670         if (ret)
671                 goto fail1;
672
673         err = tsl2563_configure(chip);
674         if (err)
675                 goto fail2;
676
677         INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
678         schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
679
680         return 0;
681 fail2:
682         iio_device_unregister(chip->indio_dev);
683 fail1:
684         kfree(chip);
685         return err;
686 }
687
688 static int tsl2563_remove(struct i2c_client *client)
689 {
690         struct tsl2563_chip *chip = i2c_get_clientdata(client);
691
692         iio_device_unregister(chip->indio_dev);
693
694         kfree(chip);
695         return 0;
696 }
697
698 static int tsl2563_suspend(struct i2c_client *client, pm_message_t state)
699 {
700         struct tsl2563_chip *chip = i2c_get_clientdata(client);
701         int ret;
702
703         mutex_lock(&chip->lock);
704
705         ret = tsl2563_set_power(chip, 0);
706         if (ret)
707                 goto out;
708
709         chip->state = state;
710
711 out:
712         mutex_unlock(&chip->lock);
713         return ret;
714 }
715
716 static int tsl2563_resume(struct i2c_client *client)
717 {
718         struct tsl2563_chip *chip = i2c_get_clientdata(client);
719         int ret;
720
721         mutex_lock(&chip->lock);
722
723         ret = tsl2563_set_power(chip, 1);
724         if (ret)
725                 goto out;
726
727         ret = tsl2563_configure(chip);
728         if (ret)
729                 goto out;
730
731         chip->state.event = PM_EVENT_ON;
732
733 out:
734         mutex_unlock(&chip->lock);
735         return ret;
736 }
737
738 static const struct i2c_device_id tsl2563_id[] = {
739         { "tsl2560", 0 },
740         { "tsl2561", 1 },
741         { "tsl2562", 2 },
742         { "tsl2563", 3 },
743         {}
744 };
745 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
746
747 static struct i2c_driver tsl2563_i2c_driver = {
748         .driver = {
749                 .name    = "tsl2563",
750         },
751         .suspend        = tsl2563_suspend,
752         .resume         = tsl2563_resume,
753         .probe          = tsl2563_probe,
754         .remove         = __devexit_p(tsl2563_remove),
755         .id_table       = tsl2563_id,
756 };
757
758 static int __init tsl2563_init(void)
759 {
760         return i2c_add_driver(&tsl2563_i2c_driver);
761 }
762
763 static void __exit tsl2563_exit(void)
764 {
765         i2c_del_driver(&tsl2563_i2c_driver);
766 }
767
768 MODULE_AUTHOR("Nokia Corporation");
769 MODULE_DESCRIPTION("tsl2563 light sensor driver");
770 MODULE_LICENSE("GPL");
771
772 module_init(tsl2563_init);
773 module_exit(tsl2563_exit);