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