Realtek cr: Add autosuspend function.
[zen-stable.git] / drivers / staging / iio / light / tsl2563.c
blob9cffa2ecb0ee52f2e9838493f1cc2240fed93ced
1 /*
2 * drivers/i2c/chips/tsl2563.c
4 * Copyright (C) 2008 Nokia Corporation
6 * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
7 * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
9 * Converted to IIO driver
10 * Amit Kucheria <amit.kucheria@verdurent.com>
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.
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.
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
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>
40 #include "../iio.h"
41 #include "tsl2563.h"
43 /* Use this many bits for fraction part. */
44 #define ADC_FRAC_BITS (14)
46 /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
47 #define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
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)
58 #define TSL2563_CMD (0x80)
59 #define TSL2563_CLEARINT (0x40)
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)
74 #define TSL2563_CMD_POWER_ON (0x03)
75 #define TSL2563_CMD_POWER_OFF (0x00)
76 #define TSL2563_CTRL_POWER_MASK (0x03)
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)
85 #define TSL2563_INT_DISBLED (0x00)
86 #define TSL2563_INT_LEVEL (0x10)
87 #define TSL2563_INT_PERSIST(n) ((n) & 0x0F)
89 struct tsl2563_gainlevel_coeff {
90 u8 gaintime;
91 u16 min;
92 u16 max;
95 static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
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,
115 struct tsl2563_chip {
116 struct mutex lock;
117 struct i2c_client *client;
118 struct delayed_work poweroff_work;
120 /* Remember state for suspend and resume functions */
121 pm_message_t state;
123 struct tsl2563_gainlevel_coeff const *gainlevel;
125 u16 low_thres;
126 u16 high_thres;
127 u8 intr;
128 bool int_enabled;
130 /* Calibration coefficients */
131 u32 calib0;
132 u32 calib1;
133 int cover_comp_gain;
135 /* Cache current values, to be returned while suspended */
136 u32 data0;
137 u32 data1;
140 static int tsl2563_write(struct i2c_client *client, u8 reg, u8 value)
142 int ret;
143 u8 buf[2];
145 buf[0] = TSL2563_CMD | reg;
146 buf[1] = value;
148 ret = i2c_master_send(client, buf, sizeof(buf));
149 return (ret == sizeof(buf)) ? 0 : ret;
152 static int tsl2563_read(struct i2c_client *client, u8 reg, void *buf, int len)
154 int ret;
155 u8 cmd = TSL2563_CMD | reg;
157 ret = i2c_master_send(client, &cmd, sizeof(cmd));
158 if (ret != sizeof(cmd))
159 return ret;
161 return i2c_master_recv(client, buf, len);
164 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
166 struct i2c_client *client = chip->client;
167 u8 cmd;
169 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
170 return tsl2563_write(client, TSL2563_REG_CTRL, cmd);
174 * Return value is 0 for off, 1 for on, or a negative error
175 * code if reading failed.
177 static int tsl2563_get_power(struct tsl2563_chip *chip)
179 struct i2c_client *client = chip->client;
180 int ret;
181 u8 val;
183 ret = tsl2563_read(client, TSL2563_REG_CTRL, &val, sizeof(val));
184 if (ret != sizeof(val))
185 return ret;
187 return (val & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
190 static int tsl2563_configure(struct tsl2563_chip *chip)
192 int ret;
194 ret = tsl2563_write(chip->client, TSL2563_REG_TIMING,
195 chip->gainlevel->gaintime);
196 if (ret)
197 goto error_ret;
198 ret = tsl2563_write(chip->client, TSL2563_REG_HIGHLOW,
199 chip->high_thres & 0xFF);
200 if (ret)
201 goto error_ret;
202 ret = tsl2563_write(chip->client, TSL2563_REG_HIGHHIGH,
203 (chip->high_thres >> 8) & 0xFF);
204 if (ret)
205 goto error_ret;
206 ret = tsl2563_write(chip->client, TSL2563_REG_LOWLOW,
207 chip->low_thres & 0xFF);
208 if (ret)
209 goto error_ret;
210 ret = tsl2563_write(chip->client, TSL2563_REG_LOWHIGH,
211 (chip->low_thres >> 8) & 0xFF);
212 /* Interrupt register is automatically written anyway if it is relevant
213 so is not here */
214 error_ret:
215 return ret;
218 static void tsl2563_poweroff_work(struct work_struct *work)
220 struct tsl2563_chip *chip =
221 container_of(work, struct tsl2563_chip, poweroff_work.work);
222 tsl2563_set_power(chip, 0);
225 static int tsl2563_detect(struct tsl2563_chip *chip)
227 int ret;
229 ret = tsl2563_set_power(chip, 1);
230 if (ret)
231 return ret;
233 ret = tsl2563_get_power(chip);
234 if (ret < 0)
235 return ret;
237 return ret ? 0 : -ENODEV;
240 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
242 struct i2c_client *client = chip->client;
243 int ret;
245 ret = tsl2563_read(client, TSL2563_REG_ID, id, sizeof(*id));
246 if (ret != sizeof(*id))
247 return ret;
249 return 0;
253 * "Normalized" ADC value is one obtained with 400ms of integration time and
254 * 16x gain. This function returns the number of bits of shift needed to
255 * convert between normalized values and HW values obtained using given
256 * timing and gain settings.
258 static int adc_shiftbits(u8 timing)
260 int shift = 0;
262 switch (timing & TSL2563_TIMING_MASK) {
263 case TSL2563_TIMING_13MS:
264 shift += 5;
265 break;
266 case TSL2563_TIMING_100MS:
267 shift += 2;
268 break;
269 case TSL2563_TIMING_400MS:
270 /* no-op */
271 break;
274 if (!(timing & TSL2563_TIMING_GAIN16))
275 shift += 4;
277 return shift;
280 /* Convert a HW ADC value to normalized scale. */
281 static u32 normalize_adc(u16 adc, u8 timing)
283 return adc << adc_shiftbits(timing);
286 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
288 unsigned int delay;
290 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
291 case TSL2563_TIMING_13MS:
292 delay = 14;
293 break;
294 case TSL2563_TIMING_100MS:
295 delay = 101;
296 break;
297 default:
298 delay = 402;
301 * TODO: Make sure that we wait at least required delay but why we
302 * have to extend it one tick more?
304 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
307 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
309 struct i2c_client *client = chip->client;
311 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
313 (adc > chip->gainlevel->max) ?
314 chip->gainlevel++ : chip->gainlevel--;
316 tsl2563_write(client, TSL2563_REG_TIMING,
317 chip->gainlevel->gaintime);
319 tsl2563_wait_adc(chip);
320 tsl2563_wait_adc(chip);
322 return 1;
323 } else
324 return 0;
327 static int tsl2563_get_adc(struct tsl2563_chip *chip)
329 struct i2c_client *client = chip->client;
330 u8 buf0[2], buf1[2];
331 u16 adc0, adc1;
332 int retry = 1;
333 int ret = 0;
335 if (chip->state.event != PM_EVENT_ON)
336 goto out;
338 if (!chip->int_enabled) {
339 cancel_delayed_work(&chip->poweroff_work);
341 if (!tsl2563_get_power(chip)) {
342 ret = tsl2563_set_power(chip, 1);
343 if (ret)
344 goto out;
345 ret = tsl2563_configure(chip);
346 if (ret)
347 goto out;
348 tsl2563_wait_adc(chip);
352 while (retry) {
353 ret = tsl2563_read(client,
354 TSL2563_REG_DATA0LOW,
355 buf0, sizeof(buf0));
356 if (ret != sizeof(buf0))
357 goto out;
359 ret = tsl2563_read(client, TSL2563_REG_DATA1LOW,
360 buf1, sizeof(buf1));
361 if (ret != sizeof(buf1))
362 goto out;
364 adc0 = (buf0[1] << 8) + buf0[0];
365 adc1 = (buf1[1] << 8) + buf1[0];
367 retry = tsl2563_adjust_gainlevel(chip, adc0);
370 chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
371 chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
373 if (!chip->int_enabled)
374 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
376 ret = 0;
377 out:
378 return ret;
381 static inline int calib_to_sysfs(u32 calib)
383 return (int) (((calib * CALIB_BASE_SYSFS) +
384 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
387 static inline u32 calib_from_sysfs(int value)
389 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
393 * Conversions between lux and ADC values.
395 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
396 * appropriate constants. Different constants are needed for different
397 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
398 * of the intensities in infrared and visible wavelengths). lux_table below
399 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
400 * constants.
403 struct tsl2563_lux_coeff {
404 unsigned long ch_ratio;
405 unsigned long ch0_coeff;
406 unsigned long ch1_coeff;
409 static const struct tsl2563_lux_coeff lux_table[] = {
411 .ch_ratio = FRAC10K(1300),
412 .ch0_coeff = FRAC10K(315),
413 .ch1_coeff = FRAC10K(262),
414 }, {
415 .ch_ratio = FRAC10K(2600),
416 .ch0_coeff = FRAC10K(337),
417 .ch1_coeff = FRAC10K(430),
418 }, {
419 .ch_ratio = FRAC10K(3900),
420 .ch0_coeff = FRAC10K(363),
421 .ch1_coeff = FRAC10K(529),
422 }, {
423 .ch_ratio = FRAC10K(5200),
424 .ch0_coeff = FRAC10K(392),
425 .ch1_coeff = FRAC10K(605),
426 }, {
427 .ch_ratio = FRAC10K(6500),
428 .ch0_coeff = FRAC10K(229),
429 .ch1_coeff = FRAC10K(291),
430 }, {
431 .ch_ratio = FRAC10K(8000),
432 .ch0_coeff = FRAC10K(157),
433 .ch1_coeff = FRAC10K(180),
434 }, {
435 .ch_ratio = FRAC10K(13000),
436 .ch0_coeff = FRAC10K(34),
437 .ch1_coeff = FRAC10K(26),
438 }, {
439 .ch_ratio = ULONG_MAX,
440 .ch0_coeff = 0,
441 .ch1_coeff = 0,
446 * Convert normalized, scaled ADC values to lux.
448 static unsigned int adc_to_lux(u32 adc0, u32 adc1)
450 const struct tsl2563_lux_coeff *lp = lux_table;
451 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
453 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
455 while (lp->ch_ratio < ratio)
456 lp++;
458 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
460 return (unsigned int) (lux >> ADC_FRAC_BITS);
463 /*--------------------------------------------------------------*/
464 /* Sysfs interface */
465 /*--------------------------------------------------------------*/
468 /* Apply calibration coefficient to ADC count. */
469 static u32 calib_adc(u32 adc, u32 calib)
471 unsigned long scaled = adc;
473 scaled *= calib;
474 scaled >>= CALIB_FRAC_BITS;
476 return (u32) scaled;
479 static int tsl2563_write_raw(struct iio_dev *indio_dev,
480 struct iio_chan_spec const *chan,
481 int val,
482 int val2,
483 long mask)
485 struct tsl2563_chip *chip = iio_priv(indio_dev);
487 if (chan->channel == 0)
488 chip->calib0 = calib_from_sysfs(val);
489 else
490 chip->calib1 = calib_from_sysfs(val);
492 return 0;
495 static int tsl2563_read_raw(struct iio_dev *indio_dev,
496 struct iio_chan_spec const *chan,
497 int *val,
498 int *val2,
499 long m)
501 int ret = -EINVAL;
502 u32 calib0, calib1;
503 struct tsl2563_chip *chip = iio_priv(indio_dev);
505 mutex_lock(&chip->lock);
506 switch (m) {
507 case 0:
508 switch (chan->type) {
509 case IIO_LIGHT:
510 ret = tsl2563_get_adc(chip);
511 if (ret)
512 goto error_ret;
513 calib0 = calib_adc(chip->data0, chip->calib0) *
514 chip->cover_comp_gain;
515 calib1 = calib_adc(chip->data1, chip->calib1) *
516 chip->cover_comp_gain;
517 *val = adc_to_lux(calib0, calib1);
518 ret = IIO_VAL_INT;
519 break;
520 case IIO_INTENSITY:
521 ret = tsl2563_get_adc(chip);
522 if (ret)
523 goto error_ret;
524 if (chan->channel == 0)
525 *val = chip->data0;
526 else
527 *val = chip->data1;
528 ret = IIO_VAL_INT;
529 break;
530 default:
531 break;
533 break;
535 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
536 if (chan->channel == 0)
537 *val = calib_to_sysfs(chip->calib0);
538 else
539 *val = calib_to_sysfs(chip->calib1);
540 ret = IIO_VAL_INT;
541 break;
542 default:
543 return -EINVAL;
546 error_ret:
547 mutex_unlock(&chip->lock);
548 return ret;
551 static const struct iio_chan_spec tsl2563_channels[] = {
552 IIO_CHAN(IIO_LIGHT, 0, 1, 1, NULL, 0, 0, 0, 0, 0, {}, 0),
553 IIO_CHAN(IIO_INTENSITY, 1, 1, 0, "both", 0,
554 (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE), 0, 0, 0, {},
555 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) |
556 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)),
557 IIO_CHAN(IIO_INTENSITY, 1, 1, 0, "ir", 1,
558 (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE), 0, 0, 0, {},
562 static int tsl2563_read_thresh(struct iio_dev *indio_dev,
563 int event_code,
564 int *val)
566 struct tsl2563_chip *chip = iio_priv(indio_dev);
568 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
569 case IIO_EV_DIR_RISING:
570 *val = chip->high_thres;
571 break;
572 case IIO_EV_DIR_FALLING:
573 *val = chip->low_thres;
574 break;
575 default:
576 return -EINVAL;
579 return 0;
582 static ssize_t tsl2563_write_thresh(struct iio_dev *indio_dev,
583 int event_code,
584 int val)
586 struct tsl2563_chip *chip = iio_priv(indio_dev);
587 int ret;
588 u8 address;
590 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
591 address = TSL2563_REG_HIGHLOW;
592 else
593 address = TSL2563_REG_LOWLOW;
594 mutex_lock(&chip->lock);
595 ret = tsl2563_write(chip->client, address, val & 0xFF);
596 if (ret)
597 goto error_ret;
598 ret = tsl2563_write(chip->client, address + 1,
599 (val >> 8) & 0xFF);
600 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
601 chip->high_thres = val;
602 else
603 chip->low_thres = val;
605 error_ret:
606 mutex_unlock(&chip->lock);
608 return ret;
611 static irqreturn_t tsl2563_event_handler(int irq, void *private)
613 struct iio_dev *dev_info = private;
614 struct tsl2563_chip *chip = iio_priv(dev_info);
615 u8 cmd = TSL2563_CMD | TSL2563_CLEARINT;
617 iio_push_event(dev_info, 0,
618 IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_LIGHT,
620 IIO_EV_TYPE_THRESH,
621 IIO_EV_DIR_EITHER),
622 iio_get_time_ns());
624 /* clear the interrupt and push the event */
625 i2c_master_send(chip->client, &cmd, sizeof(cmd));
626 return IRQ_HANDLED;
629 static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
630 int event_code,
631 int state)
633 struct tsl2563_chip *chip = iio_priv(indio_dev);
634 int ret = 0;
636 mutex_lock(&chip->lock);
637 if (state && !(chip->intr & 0x30)) {
638 chip->intr &= ~0x30;
639 chip->intr |= 0x10;
640 /* ensure the chip is actually on */
641 cancel_delayed_work(&chip->poweroff_work);
642 if (!tsl2563_get_power(chip)) {
643 ret = tsl2563_set_power(chip, 1);
644 if (ret)
645 goto out;
646 ret = tsl2563_configure(chip);
647 if (ret)
648 goto out;
650 ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
651 chip->int_enabled = true;
654 if (!state && (chip->intr & 0x30)) {
655 chip->intr |= ~0x30;
656 ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
657 chip->int_enabled = false;
658 /* now the interrupt is not enabled, we can go to sleep */
659 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
661 out:
662 mutex_unlock(&chip->lock);
664 return ret;
667 static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
668 int event_code)
670 struct tsl2563_chip *chip = iio_priv(indio_dev);
671 u8 rxbuf;
672 int ret;
674 mutex_lock(&chip->lock);
675 ret = tsl2563_read(chip->client, TSL2563_REG_INT,
676 &rxbuf, sizeof(rxbuf));
677 mutex_unlock(&chip->lock);
678 if (ret < 0)
679 goto error_ret;
680 ret = !!(rxbuf & 0x30);
681 error_ret:
683 return ret;
686 /*--------------------------------------------------------------*/
687 /* Probe, Attach, Remove */
688 /*--------------------------------------------------------------*/
689 static struct i2c_driver tsl2563_i2c_driver;
691 static const struct iio_info tsl2563_info_no_irq = {
692 .driver_module = THIS_MODULE,
695 static const struct iio_info tsl2563_info = {
696 .driver_module = THIS_MODULE,
697 .num_interrupt_lines = 1,
698 .read_raw = &tsl2563_read_raw,
699 .write_raw = &tsl2563_write_raw,
700 .read_event_value = &tsl2563_read_thresh,
701 .write_event_value = &tsl2563_write_thresh,
702 .read_event_config = &tsl2563_read_interrupt_config,
703 .write_event_config = &tsl2563_write_interrupt_config,
706 static int __devinit tsl2563_probe(struct i2c_client *client,
707 const struct i2c_device_id *device_id)
709 struct iio_dev *indio_dev;
710 struct tsl2563_chip *chip;
711 struct tsl2563_platform_data *pdata = client->dev.platform_data;
712 int err = 0;
713 int ret;
714 u8 id;
716 indio_dev = iio_allocate_device(sizeof(*chip));
717 if (!indio_dev)
718 return -ENOMEM;
720 chip = iio_priv(indio_dev);
722 i2c_set_clientdata(client, chip);
723 chip->client = client;
725 err = tsl2563_detect(chip);
726 if (err) {
727 dev_err(&client->dev, "device not found, error %d\n", -err);
728 goto fail1;
731 err = tsl2563_read_id(chip, &id);
732 if (err)
733 goto fail1;
735 mutex_init(&chip->lock);
737 /* Default values used until userspace says otherwise */
738 chip->low_thres = 0x0;
739 chip->high_thres = 0xffff;
740 chip->gainlevel = tsl2563_gainlevel_table;
741 chip->intr = TSL2563_INT_PERSIST(4);
742 chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
743 chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
745 if (pdata)
746 chip->cover_comp_gain = pdata->cover_comp_gain;
747 else
748 chip->cover_comp_gain = 1;
750 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
751 indio_dev->name = client->name;
752 indio_dev->channels = tsl2563_channels;
753 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
754 indio_dev->dev.parent = &client->dev;
755 indio_dev->modes = INDIO_DIRECT_MODE;
756 if (client->irq)
757 indio_dev->info = &tsl2563_info;
758 else
759 indio_dev->info = &tsl2563_info_no_irq;
760 ret = iio_device_register(indio_dev);
761 if (ret)
762 goto fail1;
763 if (client->irq) {
764 ret = request_threaded_irq(client->irq,
765 NULL,
766 &tsl2563_event_handler,
767 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
768 "tsl2563_event",
769 indio_dev);
770 if (ret)
771 goto fail2;
773 err = tsl2563_configure(chip);
774 if (err)
775 goto fail3;
777 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
778 /* The interrupt cannot yet be enabled so this is fine without lock */
779 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
781 return 0;
782 fail3:
783 if (client->irq)
784 free_irq(client->irq, indio_dev);
785 fail2:
786 iio_device_unregister(indio_dev);
787 fail1:
788 kfree(chip);
789 return err;
792 static int tsl2563_remove(struct i2c_client *client)
794 struct tsl2563_chip *chip = i2c_get_clientdata(client);
795 struct iio_dev *indio_dev = iio_priv_to_dev(chip);
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 tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
801 flush_scheduled_work();
802 tsl2563_set_power(chip, 0);
803 if (client->irq)
804 free_irq(client->irq, indio_dev);
805 iio_device_unregister(indio_dev);
807 return 0;
810 static int tsl2563_suspend(struct i2c_client *client, pm_message_t state)
812 struct tsl2563_chip *chip = i2c_get_clientdata(client);
813 int ret;
815 mutex_lock(&chip->lock);
817 ret = tsl2563_set_power(chip, 0);
818 if (ret)
819 goto out;
821 chip->state = state;
823 out:
824 mutex_unlock(&chip->lock);
825 return ret;
828 static int tsl2563_resume(struct i2c_client *client)
830 struct tsl2563_chip *chip = i2c_get_clientdata(client);
831 int ret;
833 mutex_lock(&chip->lock);
835 ret = tsl2563_set_power(chip, 1);
836 if (ret)
837 goto out;
839 ret = tsl2563_configure(chip);
840 if (ret)
841 goto out;
843 chip->state.event = PM_EVENT_ON;
845 out:
846 mutex_unlock(&chip->lock);
847 return ret;
850 static const struct i2c_device_id tsl2563_id[] = {
851 { "tsl2560", 0 },
852 { "tsl2561", 1 },
853 { "tsl2562", 2 },
854 { "tsl2563", 3 },
857 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
859 static struct i2c_driver tsl2563_i2c_driver = {
860 .driver = {
861 .name = "tsl2563",
863 .suspend = tsl2563_suspend,
864 .resume = tsl2563_resume,
865 .probe = tsl2563_probe,
866 .remove = __devexit_p(tsl2563_remove),
867 .id_table = tsl2563_id,
870 static int __init tsl2563_init(void)
872 return i2c_add_driver(&tsl2563_i2c_driver);
875 static void __exit tsl2563_exit(void)
877 i2c_del_driver(&tsl2563_i2c_driver);
880 MODULE_AUTHOR("Nokia Corporation");
881 MODULE_DESCRIPTION("tsl2563 light sensor driver");
882 MODULE_LICENSE("GPL");
884 module_init(tsl2563_init);
885 module_exit(tsl2563_exit);