Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[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/irq.h>
31 #include <linux/sched.h>
32 #include <linux/mutex.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/pm.h>
36 #include <linux/hwmon.h>
37 #include <linux/err.h>
38 #include <linux/slab.h>
39
40 #include "../iio.h"
41 #include "tsl2563.h"
42
43 /* Use this many bits for fraction part. */
44 #define ADC_FRAC_BITS           (14)
45
46 /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
47 #define FRAC10K(f)              (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
48
49 /* Bits used for fraction in calibration coefficients.*/
50 #define CALIB_FRAC_BITS         (10)
51 /* 0.5 in CALIB_FRAC_BITS precision */
52 #define CALIB_FRAC_HALF         (1 << (CALIB_FRAC_BITS - 1))
53 /* Make a fraction from a number n that was multiplied with b. */
54 #define CALIB_FRAC(n, b)        (((n) << CALIB_FRAC_BITS) / (b))
55 /* Decimal 10^(digits in sysfs presentation) */
56 #define CALIB_BASE_SYSFS        (1000)
57
58 #define TSL2563_CMD             (0x80)
59 #define TSL2563_CLEARINT        (0x40)
60
61 #define TSL2563_REG_CTRL        (0x00)
62 #define TSL2563_REG_TIMING      (0x01)
63 #define TSL2563_REG_LOWLOW      (0x02) /* data0 low threshold, 2 bytes */
64 #define TSL2563_REG_LOWHIGH     (0x03)
65 #define TSL2563_REG_HIGHLOW     (0x04) /* data0 high threshold, 2 bytes */
66 #define TSL2563_REG_HIGHHIGH    (0x05)
67 #define TSL2563_REG_INT         (0x06)
68 #define TSL2563_REG_ID          (0x0a)
69 #define TSL2563_REG_DATA0LOW    (0x0c) /* broadband sensor value, 2 bytes */
70 #define TSL2563_REG_DATA0HIGH   (0x0d)
71 #define TSL2563_REG_DATA1LOW    (0x0e) /* infrared sensor value, 2 bytes */
72 #define TSL2563_REG_DATA1HIGH   (0x0f)
73
74 #define TSL2563_CMD_POWER_ON    (0x03)
75 #define TSL2563_CMD_POWER_OFF   (0x00)
76 #define TSL2563_CTRL_POWER_MASK (0x03)
77
78 #define TSL2563_TIMING_13MS     (0x00)
79 #define TSL2563_TIMING_100MS    (0x01)
80 #define TSL2563_TIMING_400MS    (0x02)
81 #define TSL2563_TIMING_MASK     (0x03)
82 #define TSL2563_TIMING_GAIN16   (0x10)
83 #define TSL2563_TIMING_GAIN1    (0x00)
84
85 #define TSL2563_INT_DISBLED     (0x00)
86 #define TSL2563_INT_LEVEL       (0x10)
87 #define TSL2563_INT_PERSIST(n)  ((n) & 0x0F)
88
89 struct tsl2563_gainlevel_coeff {
90         u8 gaintime;
91         u16 min;
92         u16 max;
93 };
94
95 static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
96         {
97                 .gaintime       = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
98                 .min            = 0,
99                 .max            = 65534,
100         }, {
101                 .gaintime       = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
102                 .min            = 2048,
103                 .max            = 65534,
104         }, {
105                 .gaintime       = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
106                 .min            = 4095,
107                 .max            = 37177,
108         }, {
109                 .gaintime       = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
110                 .min            = 3000,
111                 .max            = 65535,
112         },
113 };
114
115 struct tsl2563_chip {
116         struct mutex            lock;
117         struct i2c_client       *client;
118         struct delayed_work     poweroff_work;
119
120         /* Remember state for suspend and resume functions */
121         pm_message_t            state;
122
123         struct tsl2563_gainlevel_coeff const *gainlevel;
124
125         u16                     low_thres;
126         u16                     high_thres;
127         u8                      intr;
128         bool                    int_enabled;
129
130         /* Calibration coefficients */
131         u32                     calib0;
132         u32                     calib1;
133         int                     cover_comp_gain;
134
135         /* Cache current values, to be returned while suspended */
136         u32                     data0;
137         u32                     data1;
138 };
139
140 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
141 {
142         struct i2c_client *client = chip->client;
143         u8 cmd;
144
145         cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
146         return i2c_smbus_write_byte_data(client,
147                                          TSL2563_CMD | TSL2563_REG_CTRL, cmd);
148 }
149
150 /*
151  * Return value is 0 for off, 1 for on, or a negative error
152  * code if reading failed.
153  */
154 static int tsl2563_get_power(struct tsl2563_chip *chip)
155 {
156         struct i2c_client *client = chip->client;
157         int ret;
158
159         ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
160         if (ret < 0)
161                 return ret;
162
163         return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
164 }
165
166 static int tsl2563_configure(struct tsl2563_chip *chip)
167 {
168         int ret;
169
170         ret = i2c_smbus_write_byte_data(chip->client,
171                         TSL2563_CMD | TSL2563_REG_TIMING,
172                         chip->gainlevel->gaintime);
173         if (ret)
174                 goto error_ret;
175         ret = i2c_smbus_write_byte_data(chip->client,
176                         TSL2563_CMD | TSL2563_REG_HIGHLOW,
177                         chip->high_thres & 0xFF);
178         if (ret)
179                 goto error_ret;
180         ret = i2c_smbus_write_byte_data(chip->client,
181                         TSL2563_CMD | TSL2563_REG_HIGHHIGH,
182                         (chip->high_thres >> 8) & 0xFF);
183         if (ret)
184                 goto error_ret;
185         ret = i2c_smbus_write_byte_data(chip->client,
186                         TSL2563_CMD | TSL2563_REG_LOWLOW,
187                         chip->low_thres & 0xFF);
188         if (ret)
189                 goto error_ret;
190         ret = i2c_smbus_write_byte_data(chip->client,
191                         TSL2563_CMD | TSL2563_REG_LOWHIGH,
192                         (chip->low_thres >> 8) & 0xFF);
193 /* Interrupt register is automatically written anyway if it is relevant
194    so is not here */
195 error_ret:
196         return ret;
197 }
198
199 static void tsl2563_poweroff_work(struct work_struct *work)
200 {
201         struct tsl2563_chip *chip =
202                 container_of(work, struct tsl2563_chip, poweroff_work.work);
203         tsl2563_set_power(chip, 0);
204 }
205
206 static int tsl2563_detect(struct tsl2563_chip *chip)
207 {
208         int ret;
209
210         ret = tsl2563_set_power(chip, 1);
211         if (ret)
212                 return ret;
213
214         ret = tsl2563_get_power(chip);
215         if (ret < 0)
216                 return ret;
217
218         return ret ? 0 : -ENODEV;
219 }
220
221 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
222 {
223         struct i2c_client *client = chip->client;
224         int ret;
225
226         ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
227         if (ret < 0)
228                 return ret;
229
230         return 0;
231 }
232
233 /*
234  * "Normalized" ADC value is one obtained with 400ms of integration time and
235  * 16x gain. This function returns the number of bits of shift needed to
236  * convert between normalized values and HW values obtained using given
237  * timing and gain settings.
238  */
239 static int adc_shiftbits(u8 timing)
240 {
241         int shift = 0;
242
243         switch (timing & TSL2563_TIMING_MASK) {
244         case TSL2563_TIMING_13MS:
245                 shift += 5;
246                 break;
247         case TSL2563_TIMING_100MS:
248                 shift += 2;
249                 break;
250         case TSL2563_TIMING_400MS:
251                 /* no-op */
252                 break;
253         }
254
255         if (!(timing & TSL2563_TIMING_GAIN16))
256                 shift += 4;
257
258         return shift;
259 }
260
261 /* Convert a HW ADC value to normalized scale. */
262 static u32 normalize_adc(u16 adc, u8 timing)
263 {
264         return adc << adc_shiftbits(timing);
265 }
266
267 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
268 {
269         unsigned int delay;
270
271         switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
272         case TSL2563_TIMING_13MS:
273                 delay = 14;
274                 break;
275         case TSL2563_TIMING_100MS:
276                 delay = 101;
277                 break;
278         default:
279                 delay = 402;
280         }
281         /*
282          * TODO: Make sure that we wait at least required delay but why we
283          * have to extend it one tick more?
284          */
285         schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
286 }
287
288 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
289 {
290         struct i2c_client *client = chip->client;
291
292         if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
293
294                 (adc > chip->gainlevel->max) ?
295                         chip->gainlevel++ : chip->gainlevel--;
296
297                 i2c_smbus_write_byte_data(client,
298                                           TSL2563_CMD | TSL2563_REG_TIMING,
299                                           chip->gainlevel->gaintime);
300
301                 tsl2563_wait_adc(chip);
302                 tsl2563_wait_adc(chip);
303
304                 return 1;
305         } else
306                 return 0;
307 }
308
309 static int tsl2563_get_adc(struct tsl2563_chip *chip)
310 {
311         struct i2c_client *client = chip->client;
312         u16 adc0, adc1;
313         int retry = 1;
314         int ret = 0;
315
316         if (chip->state.event != PM_EVENT_ON)
317                 goto out;
318
319         if (!chip->int_enabled) {
320                 cancel_delayed_work(&chip->poweroff_work);
321
322                 if (!tsl2563_get_power(chip)) {
323                         ret = tsl2563_set_power(chip, 1);
324                         if (ret)
325                                 goto out;
326                         ret = tsl2563_configure(chip);
327                         if (ret)
328                                 goto out;
329                         tsl2563_wait_adc(chip);
330                 }
331         }
332
333         while (retry) {
334                 ret = i2c_smbus_read_word_data(client,
335                                 TSL2563_CMD | TSL2563_REG_DATA0LOW);
336                 if (ret < 0)
337                         goto out;
338                 adc0 = ret;
339
340                 ret = i2c_smbus_read_word_data(client,
341                                 TSL2563_CMD | TSL2563_REG_DATA1LOW);
342                 if (ret < 0)
343                         goto out;
344                 adc1 = ret;
345
346                 retry = tsl2563_adjust_gainlevel(chip, adc0);
347         }
348
349         chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
350         chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
351
352         if (!chip->int_enabled)
353                 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
354
355         ret = 0;
356 out:
357         return ret;
358 }
359
360 static inline int calib_to_sysfs(u32 calib)
361 {
362         return (int) (((calib * CALIB_BASE_SYSFS) +
363                        CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
364 }
365
366 static inline u32 calib_from_sysfs(int value)
367 {
368         return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
369 }
370
371 /*
372  * Conversions between lux and ADC values.
373  *
374  * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
375  * appropriate constants. Different constants are needed for different
376  * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
377  * of the intensities in infrared and visible wavelengths). lux_table below
378  * lists the upper threshold of the adc1/adc0 ratio and the corresponding
379  * constants.
380  */
381
382 struct tsl2563_lux_coeff {
383         unsigned long ch_ratio;
384         unsigned long ch0_coeff;
385         unsigned long ch1_coeff;
386 };
387
388 static const struct tsl2563_lux_coeff lux_table[] = {
389         {
390                 .ch_ratio       = FRAC10K(1300),
391                 .ch0_coeff      = FRAC10K(315),
392                 .ch1_coeff      = FRAC10K(262),
393         }, {
394                 .ch_ratio       = FRAC10K(2600),
395                 .ch0_coeff      = FRAC10K(337),
396                 .ch1_coeff      = FRAC10K(430),
397         }, {
398                 .ch_ratio       = FRAC10K(3900),
399                 .ch0_coeff      = FRAC10K(363),
400                 .ch1_coeff      = FRAC10K(529),
401         }, {
402                 .ch_ratio       = FRAC10K(5200),
403                 .ch0_coeff      = FRAC10K(392),
404                 .ch1_coeff      = FRAC10K(605),
405         }, {
406                 .ch_ratio       = FRAC10K(6500),
407                 .ch0_coeff      = FRAC10K(229),
408                 .ch1_coeff      = FRAC10K(291),
409         }, {
410                 .ch_ratio       = FRAC10K(8000),
411                 .ch0_coeff      = FRAC10K(157),
412                 .ch1_coeff      = FRAC10K(180),
413         }, {
414                 .ch_ratio       = FRAC10K(13000),
415                 .ch0_coeff      = FRAC10K(34),
416                 .ch1_coeff      = FRAC10K(26),
417         }, {
418                 .ch_ratio       = ULONG_MAX,
419                 .ch0_coeff      = 0,
420                 .ch1_coeff      = 0,
421         },
422 };
423
424 /*
425  * Convert normalized, scaled ADC values to lux.
426  */
427 static unsigned int adc_to_lux(u32 adc0, u32 adc1)
428 {
429         const struct tsl2563_lux_coeff *lp = lux_table;
430         unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
431
432         ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
433
434         while (lp->ch_ratio < ratio)
435                 lp++;
436
437         lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
438
439         return (unsigned int) (lux >> ADC_FRAC_BITS);
440 }
441
442 /*--------------------------------------------------------------*/
443 /*                      Sysfs interface                         */
444 /*--------------------------------------------------------------*/
445
446
447 /* Apply calibration coefficient to ADC count. */
448 static u32 calib_adc(u32 adc, u32 calib)
449 {
450         unsigned long scaled = adc;
451
452         scaled *= calib;
453         scaled >>= CALIB_FRAC_BITS;
454
455         return (u32) scaled;
456 }
457
458 static int tsl2563_write_raw(struct iio_dev *indio_dev,
459                                struct iio_chan_spec const *chan,
460                                int val,
461                                int val2,
462                                long mask)
463 {
464         struct tsl2563_chip *chip = iio_priv(indio_dev);
465
466         if (chan->channel == 0)
467                 chip->calib0 = calib_from_sysfs(val);
468         else
469                 chip->calib1 = calib_from_sysfs(val);
470
471         return 0;
472 }
473
474 static int tsl2563_read_raw(struct iio_dev *indio_dev,
475                             struct iio_chan_spec const *chan,
476                             int *val,
477                             int *val2,
478                             long m)
479 {
480         int ret = -EINVAL;
481         u32 calib0, calib1;
482         struct tsl2563_chip *chip = iio_priv(indio_dev);
483
484         mutex_lock(&chip->lock);
485         switch (m) {
486         case 0:
487                 switch (chan->type) {
488                 case IIO_LIGHT:
489                         ret = tsl2563_get_adc(chip);
490                         if (ret)
491                                 goto error_ret;
492                         calib0 = calib_adc(chip->data0, chip->calib0) *
493                                 chip->cover_comp_gain;
494                         calib1 = calib_adc(chip->data1, chip->calib1) *
495                                 chip->cover_comp_gain;
496                         *val = adc_to_lux(calib0, calib1);
497                         ret = IIO_VAL_INT;
498                         break;
499                 case IIO_INTENSITY:
500                         ret = tsl2563_get_adc(chip);
501                         if (ret)
502                                 goto error_ret;
503                         if (chan->channel == 0)
504                                 *val = chip->data0;
505                         else
506                                 *val = chip->data1;
507                         ret = IIO_VAL_INT;
508                         break;
509                 default:
510                         break;
511                 }
512                 break;
513
514         case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
515                 if (chan->channel == 0)
516                         *val = calib_to_sysfs(chip->calib0);
517                 else
518                         *val = calib_to_sysfs(chip->calib1);
519                 ret = IIO_VAL_INT;
520                 break;
521         default:
522                 return -EINVAL;
523         }
524
525 error_ret:
526         mutex_unlock(&chip->lock);
527         return ret;
528 }
529
530 #define INFO_MASK (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE)
531 #define EVENT_MASK (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
532                     IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
533 #define IIO_CHAN_2563(type, mod, proc, chan, imask, emask) \
534         IIO_CHAN(type, mod, 1, proc, NULL, chan, 0, imask, 0, 0, {}, emask)
535
536 static const struct iio_chan_spec tsl2563_channels[] = {
537         IIO_CHAN_2563(IIO_LIGHT,     0, 1, 0, 0, 0),
538         IIO_CHAN_2563(IIO_INTENSITY, 1, 0, 0, INFO_MASK, EVENT_MASK),
539         IIO_CHAN_2563(IIO_INTENSITY, 1, 0, 1, INFO_MASK, 0),
540 };
541
542 static int tsl2563_read_thresh(struct iio_dev *indio_dev,
543                                 int event_code,
544                                 int *val)
545 {
546         struct tsl2563_chip *chip = iio_priv(indio_dev);
547
548         switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
549         case IIO_EV_DIR_RISING:
550                 *val = chip->high_thres;
551                 break;
552         case IIO_EV_DIR_FALLING:
553                 *val = chip->low_thres;
554                 break;
555         default:
556                 return -EINVAL;
557         }
558
559         return 0;
560 }
561
562 static ssize_t tsl2563_write_thresh(struct iio_dev *indio_dev,
563                                   int event_code,
564                                   int val)
565 {
566         struct tsl2563_chip *chip = iio_priv(indio_dev);
567         int ret;
568         u8 address;
569
570         if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
571                 address = TSL2563_REG_HIGHLOW;
572         else
573                 address = TSL2563_REG_LOWLOW;
574         mutex_lock(&chip->lock);
575         ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
576                                         val & 0xFF);
577         if (ret)
578                 goto error_ret;
579         ret = i2c_smbus_write_byte_data(chip->client,
580                                         TSL2563_CMD | (address + 1),
581                                         (val >> 8) & 0xFF);
582         if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
583                 chip->high_thres = val;
584         else
585                 chip->low_thres = val;
586
587 error_ret:
588         mutex_unlock(&chip->lock);
589
590         return ret;
591 }
592
593 static irqreturn_t tsl2563_event_handler(int irq, void *private)
594 {
595         struct iio_dev *dev_info = private;
596         struct tsl2563_chip *chip = iio_priv(dev_info);
597
598         iio_push_event(dev_info, 0,
599                        IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_LIGHT,
600                                             0,
601                                             IIO_EV_TYPE_THRESH,
602                                             IIO_EV_DIR_EITHER),
603                        iio_get_time_ns());
604
605         /* clear the interrupt and push the event */
606         i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
607         return IRQ_HANDLED;
608 }
609
610 static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
611                                         int event_code,
612                                         int state)
613 {
614         struct tsl2563_chip *chip = iio_priv(indio_dev);
615         int ret = 0;
616
617         mutex_lock(&chip->lock);
618         if (state && !(chip->intr & 0x30)) {
619                 chip->intr &= ~0x30;
620                 chip->intr |= 0x10;
621                 /* ensure the chip is actually on */
622                 cancel_delayed_work(&chip->poweroff_work);
623                 if (!tsl2563_get_power(chip)) {
624                         ret = tsl2563_set_power(chip, 1);
625                         if (ret)
626                                 goto out;
627                         ret = tsl2563_configure(chip);
628                         if (ret)
629                                 goto out;
630                 }
631                 ret = i2c_smbus_write_byte_data(chip->client,
632                                                 TSL2563_CMD | TSL2563_REG_INT,
633                                                 chip->intr);
634                 chip->int_enabled = true;
635         }
636
637         if (!state && (chip->intr & 0x30)) {
638                 chip->intr |= ~0x30;
639                 ret = i2c_smbus_write_byte_data(chip->client,
640                                                 TSL2563_CMD | TSL2563_REG_INT,
641                                                 chip->intr);
642                 chip->int_enabled = false;
643                 /* now the interrupt is not enabled, we can go to sleep */
644                 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
645         }
646 out:
647         mutex_unlock(&chip->lock);
648
649         return ret;
650 }
651
652 static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
653                                            int event_code)
654 {
655         struct tsl2563_chip *chip = iio_priv(indio_dev);
656         int ret;
657
658         mutex_lock(&chip->lock);
659         ret = i2c_smbus_read_byte_data(chip->client,
660                                        TSL2563_CMD | TSL2563_REG_INT);
661         mutex_unlock(&chip->lock);
662         if (ret < 0)
663                 goto error_ret;
664         ret = !!(ret & 0x30);
665 error_ret:
666
667         return ret;
668 }
669
670 /*--------------------------------------------------------------*/
671 /*                      Probe, Attach, Remove                   */
672 /*--------------------------------------------------------------*/
673 static struct i2c_driver tsl2563_i2c_driver;
674
675 static const struct iio_info tsl2563_info_no_irq = {
676         .driver_module = THIS_MODULE,
677         .read_raw = &tsl2563_read_raw,
678         .write_raw = &tsl2563_write_raw,
679 };
680
681 static const struct iio_info tsl2563_info = {
682         .driver_module = THIS_MODULE,
683         .num_interrupt_lines = 1,
684         .read_raw = &tsl2563_read_raw,
685         .write_raw = &tsl2563_write_raw,
686         .read_event_value = &tsl2563_read_thresh,
687         .write_event_value = &tsl2563_write_thresh,
688         .read_event_config = &tsl2563_read_interrupt_config,
689         .write_event_config = &tsl2563_write_interrupt_config,
690 };
691
692 static int __devinit tsl2563_probe(struct i2c_client *client,
693                                 const struct i2c_device_id *device_id)
694 {
695         struct iio_dev *indio_dev;
696         struct tsl2563_chip *chip;
697         struct tsl2563_platform_data *pdata = client->dev.platform_data;
698         int err = 0;
699         int ret;
700         u8 id;
701
702         indio_dev = iio_allocate_device(sizeof(*chip));
703         if (!indio_dev)
704                 return -ENOMEM;
705
706         chip = iio_priv(indio_dev);
707
708         i2c_set_clientdata(client, chip);
709         chip->client = client;
710
711         err = tsl2563_detect(chip);
712         if (err) {
713                 dev_err(&client->dev, "device not found, error %d\n", -err);
714                 goto fail1;
715         }
716
717         err = tsl2563_read_id(chip, &id);
718         if (err)
719                 goto fail1;
720
721         mutex_init(&chip->lock);
722
723         /* Default values used until userspace says otherwise */
724         chip->low_thres = 0x0;
725         chip->high_thres = 0xffff;
726         chip->gainlevel = tsl2563_gainlevel_table;
727         chip->intr = TSL2563_INT_PERSIST(4);
728         chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
729         chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
730
731         if (pdata)
732                 chip->cover_comp_gain = pdata->cover_comp_gain;
733         else
734                 chip->cover_comp_gain = 1;
735
736         dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
737         indio_dev->name = client->name;
738         indio_dev->channels = tsl2563_channels;
739         indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
740         indio_dev->dev.parent = &client->dev;
741         indio_dev->modes = INDIO_DIRECT_MODE;
742         if (client->irq)
743                 indio_dev->info = &tsl2563_info;
744         else
745                 indio_dev->info = &tsl2563_info_no_irq;
746         ret = iio_device_register(indio_dev);
747         if (ret)
748                 goto fail1;
749         if (client->irq) {
750                 ret = request_threaded_irq(client->irq,
751                                            NULL,
752                                            &tsl2563_event_handler,
753                                            IRQF_TRIGGER_RISING | IRQF_ONESHOT,
754                                            "tsl2563_event",
755                                            indio_dev);
756                 if (ret)
757                         goto fail2;
758         }
759         err = tsl2563_configure(chip);
760         if (err)
761                 goto fail3;
762
763         INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
764         /* The interrupt cannot yet be enabled so this is fine without lock */
765         schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
766
767         return 0;
768 fail3:
769         if (client->irq)
770                 free_irq(client->irq, indio_dev);
771 fail2:
772         iio_device_unregister(indio_dev);
773 fail1:
774         kfree(chip);
775         return err;
776 }
777
778 static int tsl2563_remove(struct i2c_client *client)
779 {
780         struct tsl2563_chip *chip = i2c_get_clientdata(client);
781         struct iio_dev *indio_dev = iio_priv_to_dev(chip);
782         if (!chip->int_enabled)
783                 cancel_delayed_work(&chip->poweroff_work);
784         /* Ensure that interrupts are disabled - then flush any bottom halves */
785         chip->intr |= ~0x30;
786         i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
787                                   chip->intr);
788         flush_scheduled_work();
789         tsl2563_set_power(chip, 0);
790         if (client->irq)
791                 free_irq(client->irq, indio_dev);
792         iio_device_unregister(indio_dev);
793
794         return 0;
795 }
796
797 static int tsl2563_suspend(struct i2c_client *client, pm_message_t state)
798 {
799         struct tsl2563_chip *chip = i2c_get_clientdata(client);
800         int ret;
801
802         mutex_lock(&chip->lock);
803
804         ret = tsl2563_set_power(chip, 0);
805         if (ret)
806                 goto out;
807
808         chip->state = state;
809
810 out:
811         mutex_unlock(&chip->lock);
812         return ret;
813 }
814
815 static int tsl2563_resume(struct i2c_client *client)
816 {
817         struct tsl2563_chip *chip = i2c_get_clientdata(client);
818         int ret;
819
820         mutex_lock(&chip->lock);
821
822         ret = tsl2563_set_power(chip, 1);
823         if (ret)
824                 goto out;
825
826         ret = tsl2563_configure(chip);
827         if (ret)
828                 goto out;
829
830         chip->state.event = PM_EVENT_ON;
831
832 out:
833         mutex_unlock(&chip->lock);
834         return ret;
835 }
836
837 static const struct i2c_device_id tsl2563_id[] = {
838         { "tsl2560", 0 },
839         { "tsl2561", 1 },
840         { "tsl2562", 2 },
841         { "tsl2563", 3 },
842         {}
843 };
844 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
845
846 static struct i2c_driver tsl2563_i2c_driver = {
847         .driver = {
848                 .name    = "tsl2563",
849         },
850         .suspend        = tsl2563_suspend,
851         .resume         = tsl2563_resume,
852         .probe          = tsl2563_probe,
853         .remove         = __devexit_p(tsl2563_remove),
854         .id_table       = tsl2563_id,
855 };
856
857 static int __init tsl2563_init(void)
858 {
859         return i2c_add_driver(&tsl2563_i2c_driver);
860 }
861
862 static void __exit tsl2563_exit(void)
863 {
864         i2c_del_driver(&tsl2563_i2c_driver);
865 }
866
867 MODULE_AUTHOR("Nokia Corporation");
868 MODULE_DESCRIPTION("tsl2563 light sensor driver");
869 MODULE_LICENSE("GPL");
870
871 module_init(tsl2563_init);
872 module_exit(tsl2563_exit);