GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / iio / light / tsl2563.c
blob98f8b78f5d8c1b2288964043849d8e3d007bce3e
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 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 iio_dev *indio_dev;
119 struct delayed_work poweroff_work;
121 struct work_struct work_thresh;
122 s64 event_timestamp;
123 /* Remember state for suspend and resume functions */
124 pm_message_t state;
126 struct tsl2563_gainlevel_coeff *gainlevel;
128 u16 low_thres;
129 u16 high_thres;
130 u8 intr;
131 bool int_enabled;
133 /* Calibration coefficients */
134 u32 calib0;
135 u32 calib1;
136 int cover_comp_gain;
138 /* Cache current values, to be returned while suspended */
139 u32 data0;
140 u32 data1;
143 static int tsl2563_write(struct i2c_client *client, u8 reg, u8 value)
145 int ret;
146 u8 buf[2];
148 buf[0] = TSL2563_CMD | reg;
149 buf[1] = value;
151 ret = i2c_master_send(client, buf, sizeof(buf));
152 return (ret == sizeof(buf)) ? 0 : ret;
155 static int tsl2563_read(struct i2c_client *client, u8 reg, void *buf, int len)
157 int ret;
158 u8 cmd = TSL2563_CMD | reg;
160 ret = i2c_master_send(client, &cmd, sizeof(cmd));
161 if (ret != sizeof(cmd))
162 return ret;
164 return i2c_master_recv(client, buf, len);
167 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
169 struct i2c_client *client = chip->client;
170 u8 cmd;
172 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
173 return tsl2563_write(client, TSL2563_REG_CTRL, cmd);
177 * Return value is 0 for off, 1 for on, or a negative error
178 * code if reading failed.
180 static int tsl2563_get_power(struct tsl2563_chip *chip)
182 struct i2c_client *client = chip->client;
183 int ret;
184 u8 val;
186 ret = tsl2563_read(client, TSL2563_REG_CTRL, &val, sizeof(val));
187 if (ret != sizeof(val))
188 return ret;
190 return (val & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
193 static int tsl2563_configure(struct tsl2563_chip *chip)
195 int ret;
197 ret = tsl2563_write(chip->client, TSL2563_REG_TIMING,
198 chip->gainlevel->gaintime);
199 if (ret)
200 goto error_ret;
201 ret = tsl2563_write(chip->client, TSL2563_REG_HIGHLOW,
202 chip->high_thres & 0xFF);
203 if (ret)
204 goto error_ret;
205 ret = tsl2563_write(chip->client, TSL2563_REG_HIGHHIGH,
206 (chip->high_thres >> 8) & 0xFF);
207 if (ret)
208 goto error_ret;
209 ret = tsl2563_write(chip->client, TSL2563_REG_LOWLOW,
210 chip->low_thres & 0xFF);
211 if (ret)
212 goto error_ret;
213 ret = tsl2563_write(chip->client, TSL2563_REG_LOWHIGH,
214 (chip->low_thres >> 8) & 0xFF);
215 /* Interrupt register is automatically written anyway if it is relevant
216 so is not here */
217 error_ret:
218 return ret;
221 static void tsl2563_poweroff_work(struct work_struct *work)
223 struct tsl2563_chip *chip =
224 container_of(work, struct tsl2563_chip, poweroff_work.work);
225 tsl2563_set_power(chip, 0);
228 static int tsl2563_detect(struct tsl2563_chip *chip)
230 int ret;
232 ret = tsl2563_set_power(chip, 1);
233 if (ret)
234 return ret;
236 ret = tsl2563_get_power(chip);
237 if (ret < 0)
238 return ret;
240 return ret ? 0 : -ENODEV;
243 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
245 struct i2c_client *client = chip->client;
246 int ret;
248 ret = tsl2563_read(client, TSL2563_REG_ID, id, sizeof(*id));
249 if (ret != sizeof(*id))
250 return ret;
252 return 0;
256 * "Normalized" ADC value is one obtained with 400ms of integration time and
257 * 16x gain. This function returns the number of bits of shift needed to
258 * convert between normalized values and HW values obtained using given
259 * timing and gain settings.
261 static int adc_shiftbits(u8 timing)
263 int shift = 0;
265 switch (timing & TSL2563_TIMING_MASK) {
266 case TSL2563_TIMING_13MS:
267 shift += 5;
268 break;
269 case TSL2563_TIMING_100MS:
270 shift += 2;
271 break;
272 case TSL2563_TIMING_400MS:
273 /* no-op */
274 break;
277 if (!(timing & TSL2563_TIMING_GAIN16))
278 shift += 4;
280 return shift;
283 /* Convert a HW ADC value to normalized scale. */
284 static u32 normalize_adc(u16 adc, u8 timing)
286 return adc << adc_shiftbits(timing);
289 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
291 unsigned int delay;
293 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
294 case TSL2563_TIMING_13MS:
295 delay = 14;
296 break;
297 case TSL2563_TIMING_100MS:
298 delay = 101;
299 break;
300 default:
301 delay = 402;
304 * TODO: Make sure that we wait at least required delay but why we
305 * have to extend it one tick more?
307 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
310 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
312 struct i2c_client *client = chip->client;
314 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
316 (adc > chip->gainlevel->max) ?
317 chip->gainlevel++ : chip->gainlevel--;
319 tsl2563_write(client, TSL2563_REG_TIMING,
320 chip->gainlevel->gaintime);
322 tsl2563_wait_adc(chip);
323 tsl2563_wait_adc(chip);
325 return 1;
326 } else
327 return 0;
330 static int tsl2563_get_adc(struct tsl2563_chip *chip)
332 struct i2c_client *client = chip->client;
333 u8 buf0[2], buf1[2];
334 u16 adc0, adc1;
335 int retry = 1;
336 int ret = 0;
338 if (chip->state.event != PM_EVENT_ON)
339 goto out;
341 if (!chip->int_enabled) {
342 cancel_delayed_work(&chip->poweroff_work);
344 if (!tsl2563_get_power(chip)) {
345 ret = tsl2563_set_power(chip, 1);
346 if (ret)
347 goto out;
348 ret = tsl2563_configure(chip);
349 if (ret)
350 goto out;
351 tsl2563_wait_adc(chip);
355 while (retry) {
356 ret = tsl2563_read(client,
357 TSL2563_REG_DATA0LOW,
358 buf0, sizeof(buf0));
359 if (ret != sizeof(buf0))
360 goto out;
362 ret = tsl2563_read(client, TSL2563_REG_DATA1LOW,
363 buf1, sizeof(buf1));
364 if (ret != sizeof(buf1))
365 goto out;
367 adc0 = (buf0[1] << 8) + buf0[0];
368 adc1 = (buf1[1] << 8) + buf1[0];
370 retry = tsl2563_adjust_gainlevel(chip, adc0);
373 chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
374 chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
376 if (!chip->int_enabled)
377 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
379 ret = 0;
380 out:
381 return ret;
384 static inline int calib_to_sysfs(u32 calib)
386 return (int) (((calib * CALIB_BASE_SYSFS) +
387 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
390 static inline u32 calib_from_sysfs(int value)
392 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
396 * Conversions between lux and ADC values.
398 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
399 * appropriate constants. Different constants are needed for different
400 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
401 * of the intensities in infrared and visible wavelengths). lux_table below
402 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
403 * constants.
406 struct tsl2563_lux_coeff {
407 unsigned long ch_ratio;
408 unsigned long ch0_coeff;
409 unsigned long ch1_coeff;
412 static const struct tsl2563_lux_coeff lux_table[] = {
414 .ch_ratio = FRAC10K(1300),
415 .ch0_coeff = FRAC10K(315),
416 .ch1_coeff = FRAC10K(262),
417 }, {
418 .ch_ratio = FRAC10K(2600),
419 .ch0_coeff = FRAC10K(337),
420 .ch1_coeff = FRAC10K(430),
421 }, {
422 .ch_ratio = FRAC10K(3900),
423 .ch0_coeff = FRAC10K(363),
424 .ch1_coeff = FRAC10K(529),
425 }, {
426 .ch_ratio = FRAC10K(5200),
427 .ch0_coeff = FRAC10K(392),
428 .ch1_coeff = FRAC10K(605),
429 }, {
430 .ch_ratio = FRAC10K(6500),
431 .ch0_coeff = FRAC10K(229),
432 .ch1_coeff = FRAC10K(291),
433 }, {
434 .ch_ratio = FRAC10K(8000),
435 .ch0_coeff = FRAC10K(157),
436 .ch1_coeff = FRAC10K(180),
437 }, {
438 .ch_ratio = FRAC10K(13000),
439 .ch0_coeff = FRAC10K(34),
440 .ch1_coeff = FRAC10K(26),
441 }, {
442 .ch_ratio = ULONG_MAX,
443 .ch0_coeff = 0,
444 .ch1_coeff = 0,
449 * Convert normalized, scaled ADC values to lux.
451 static unsigned int adc_to_lux(u32 adc0, u32 adc1)
453 const struct tsl2563_lux_coeff *lp = lux_table;
454 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
456 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
458 while (lp->ch_ratio < ratio)
459 lp++;
461 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
463 return (unsigned int) (lux >> ADC_FRAC_BITS);
466 /*--------------------------------------------------------------*/
467 /* Sysfs interface */
468 /*--------------------------------------------------------------*/
470 static ssize_t tsl2563_adc_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
473 struct iio_dev *indio_dev = dev_get_drvdata(dev);
474 struct tsl2563_chip *chip = indio_dev->dev_data;
475 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
476 int ret;
478 mutex_lock(&chip->lock);
480 ret = tsl2563_get_adc(chip);
481 if (ret)
482 goto out;
484 switch (this_attr->address) {
485 case 0:
486 ret = snprintf(buf, PAGE_SIZE, "%d\n", chip->data0);
487 break;
488 case 1:
489 ret = snprintf(buf, PAGE_SIZE, "%d\n", chip->data1);
490 break;
492 out:
493 mutex_unlock(&chip->lock);
494 return ret;
497 /* Apply calibration coefficient to ADC count. */
498 static u32 calib_adc(u32 adc, u32 calib)
500 unsigned long scaled = adc;
502 scaled *= calib;
503 scaled >>= CALIB_FRAC_BITS;
505 return (u32) scaled;
508 static ssize_t tsl2563_lux_show(struct device *dev,
509 struct device_attribute *attr, char *buf)
511 struct iio_dev *indio_dev = dev_get_drvdata(dev);
512 struct tsl2563_chip *chip = indio_dev->dev_data;
513 u32 calib0, calib1;
514 int ret;
516 mutex_lock(&chip->lock);
518 ret = tsl2563_get_adc(chip);
519 if (ret)
520 goto out;
522 calib0 = calib_adc(chip->data0, chip->calib0) * chip->cover_comp_gain;
523 calib1 = calib_adc(chip->data1, chip->calib1) * chip->cover_comp_gain;
525 ret = snprintf(buf, PAGE_SIZE, "%d\n", adc_to_lux(calib0, calib1));
527 out:
528 mutex_unlock(&chip->lock);
529 return ret;
532 static ssize_t format_calib(char *buf, int len, u32 calib)
534 return snprintf(buf, PAGE_SIZE, "%d\n", calib_to_sysfs(calib));
537 static ssize_t tsl2563_calib_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
540 struct iio_dev *indio_dev = dev_get_drvdata(dev);
541 struct tsl2563_chip *chip = indio_dev->dev_data;
542 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
543 int ret;
545 mutex_lock(&chip->lock);
546 switch (this_attr->address) {
547 case 0:
548 ret = format_calib(buf, PAGE_SIZE, chip->calib0);
549 break;
550 case 1:
551 ret = format_calib(buf, PAGE_SIZE, chip->calib1);
552 break;
553 default:
554 ret = -ENODEV;
556 mutex_unlock(&chip->lock);
557 return ret;
560 static ssize_t tsl2563_calib_store(struct device *dev,
561 struct device_attribute *attr,
562 const char *buf, size_t len)
564 struct iio_dev *indio_dev = dev_get_drvdata(dev);
565 struct tsl2563_chip *chip = indio_dev->dev_data;
566 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
567 int value;
568 u32 calib;
570 if (1 != sscanf(buf, "%d", &value))
571 return -EINVAL;
573 calib = calib_from_sysfs(value);
575 switch (this_attr->address) {
576 case 0:
577 chip->calib0 = calib;
578 break;
579 case 1:
580 chip->calib1 = calib;
581 break;
584 return len;
587 static IIO_DEVICE_ATTR(intensity_both_raw, S_IRUGO,
588 tsl2563_adc_show, NULL, 0);
589 static IIO_DEVICE_ATTR(intensity_ir_raw, S_IRUGO,
590 tsl2563_adc_show, NULL, 1);
591 static DEVICE_ATTR(illuminance0_input, S_IRUGO, tsl2563_lux_show, NULL);
592 static IIO_DEVICE_ATTR(intensity_both_calibgain, S_IRUGO | S_IWUSR,
593 tsl2563_calib_show, tsl2563_calib_store, 0);
594 static IIO_DEVICE_ATTR(intensity_ir_calibgain, S_IRUGO | S_IWUSR,
595 tsl2563_calib_show, tsl2563_calib_store, 1);
597 static ssize_t tsl2563_show_name(struct device *dev,
598 struct device_attribute *attr,
599 char *buf)
601 struct iio_dev *indio_dev = dev_get_drvdata(dev);
602 struct tsl2563_chip *chip = indio_dev->dev_data;
603 return sprintf(buf, "%s\n", chip->client->name);
606 static DEVICE_ATTR(name, S_IRUGO, tsl2563_show_name, NULL);
608 static struct attribute *tsl2563_attributes[] = {
609 &iio_dev_attr_intensity_both_raw.dev_attr.attr,
610 &iio_dev_attr_intensity_ir_raw.dev_attr.attr,
611 &dev_attr_illuminance0_input.attr,
612 &iio_dev_attr_intensity_both_calibgain.dev_attr.attr,
613 &iio_dev_attr_intensity_ir_calibgain.dev_attr.attr,
614 &dev_attr_name.attr,
615 NULL
618 static const struct attribute_group tsl2563_group = {
619 .attrs = tsl2563_attributes,
622 static ssize_t tsl2563_read_thresh(struct device *dev,
623 struct device_attribute *attr,
624 char *buf)
626 struct iio_dev *indio_dev = dev_get_drvdata(dev);
627 struct tsl2563_chip *chip = indio_dev->dev_data;
628 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
629 u16 val = 0;
630 switch (this_attr->address) {
631 case TSL2563_REG_HIGHLOW:
632 val = chip->high_thres;
633 break;
634 case TSL2563_REG_LOWLOW:
635 val = chip->low_thres;
636 break;
638 return snprintf(buf, PAGE_SIZE, "%d\n", val);
641 static ssize_t tsl2563_write_thresh(struct device *dev,
642 struct device_attribute *attr,
643 const char *buf,
644 size_t len)
646 struct iio_dev *indio_dev = dev_get_drvdata(dev);
647 struct tsl2563_chip *chip = indio_dev->dev_data;
648 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
649 unsigned long val;
650 int ret;
652 ret = strict_strtoul(buf, 10, &val);
653 if (ret)
654 return ret;
655 mutex_lock(&chip->lock);
656 ret = tsl2563_write(chip->client, this_attr->address, val & 0xFF);
657 if (ret)
658 goto error_ret;
659 ret = tsl2563_write(chip->client, this_attr->address + 1,
660 (val >> 8) & 0xFF);
661 switch (this_attr->address) {
662 case TSL2563_REG_HIGHLOW:
663 chip->high_thres = val;
664 break;
665 case TSL2563_REG_LOWLOW:
666 chip->low_thres = val;
667 break;
670 error_ret:
671 mutex_unlock(&chip->lock);
673 return ret < 0 ? ret : len;
676 static IIO_DEVICE_ATTR(intensity_both_thresh_high_value,
677 S_IRUGO | S_IWUSR,
678 tsl2563_read_thresh,
679 tsl2563_write_thresh,
680 TSL2563_REG_HIGHLOW);
682 static IIO_DEVICE_ATTR(intensity_both_thresh_low_value,
683 S_IRUGO | S_IWUSR,
684 tsl2563_read_thresh,
685 tsl2563_write_thresh,
686 TSL2563_REG_LOWLOW);
688 static int tsl2563_int_th(struct iio_dev *dev_info,
689 int index,
690 s64 timestamp,
691 int not_test)
693 struct tsl2563_chip *chip = dev_info->dev_data;
695 chip->event_timestamp = timestamp;
696 schedule_work(&chip->work_thresh);
698 return 0;
701 static void tsl2563_int_bh(struct work_struct *work_s)
703 struct tsl2563_chip *chip
704 = container_of(work_s,
705 struct tsl2563_chip, work_thresh);
706 u8 cmd = TSL2563_CMD | TSL2563_CLEARINT;
708 iio_push_event(chip->indio_dev, 0,
709 IIO_EVENT_CODE_LIGHT_BASE,
710 chip->event_timestamp);
712 /* reenable_irq */
713 enable_irq(chip->client->irq);
714 /* clear the interrupt and push the event */
715 i2c_master_send(chip->client, &cmd, sizeof(cmd));
719 static ssize_t tsl2563_write_interrupt_config(struct device *dev,
720 struct device_attribute *attr,
721 const char *buf,
722 size_t len)
724 struct iio_dev *indio_dev = dev_get_drvdata(dev);
725 struct tsl2563_chip *chip = indio_dev->dev_data;
726 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
727 int input, ret = 0;
729 ret = sscanf(buf, "%d", &input);
730 if (ret != 1)
731 return -EINVAL;
732 mutex_lock(&chip->lock);
733 if (input && !(chip->intr & 0x30)) {
734 iio_add_event_to_list(this_attr->listel,
735 &indio_dev->interrupts[0]->ev_list);
736 chip->intr &= ~0x30;
737 chip->intr |= 0x10;
738 /* ensure the chip is actually on */
739 cancel_delayed_work(&chip->poweroff_work);
740 if (!tsl2563_get_power(chip)) {
741 ret = tsl2563_set_power(chip, 1);
742 if (ret)
743 goto out;
744 ret = tsl2563_configure(chip);
745 if (ret)
746 goto out;
748 ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
749 chip->int_enabled = true;
752 if (!input && (chip->intr & 0x30)) {
753 chip->intr |= ~0x30;
754 ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
755 iio_remove_event_from_list(this_attr->listel,
756 &indio_dev->interrupts[0]->ev_list);
757 chip->int_enabled = false;
758 /* now the interrupt is not enabled, we can go to sleep */
759 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
761 out:
762 mutex_unlock(&chip->lock);
764 return (ret < 0) ? ret : len;
767 static ssize_t tsl2563_read_interrupt_config(struct device *dev,
768 struct device_attribute *attr,
769 char *buf)
771 struct iio_dev *indio_dev = dev_get_drvdata(dev);
772 struct tsl2563_chip *chip = indio_dev->dev_data;
773 int ret;
774 u8 rxbuf;
775 ssize_t len;
777 mutex_lock(&chip->lock);
778 ret = tsl2563_read(chip->client,
779 TSL2563_REG_INT,
780 &rxbuf,
781 sizeof(rxbuf));
782 mutex_unlock(&chip->lock);
783 if (ret < 0)
784 goto error_ret;
785 len = snprintf(buf, PAGE_SIZE, "%d\n", !!(rxbuf & 0x30));
786 error_ret:
788 return (ret < 0) ? ret : len;
791 IIO_EVENT_ATTR(intensity_both_thresh_both_en,
792 tsl2563_read_interrupt_config,
793 tsl2563_write_interrupt_config,
795 tsl2563_int_th);
797 static struct attribute *tsl2563_event_attributes[] = {
798 &iio_event_attr_intensity_both_thresh_both_en.dev_attr.attr,
799 &iio_dev_attr_intensity_both_thresh_high_value.dev_attr.attr,
800 &iio_dev_attr_intensity_both_thresh_low_value.dev_attr.attr,
801 NULL,
804 static struct attribute_group tsl2563_event_attribute_group = {
805 .attrs = tsl2563_event_attributes,
808 /*--------------------------------------------------------------*/
809 /* Probe, Attach, Remove */
810 /*--------------------------------------------------------------*/
811 static struct i2c_driver tsl2563_i2c_driver;
813 static int __devinit tsl2563_probe(struct i2c_client *client,
814 const struct i2c_device_id *device_id)
816 struct tsl2563_chip *chip;
817 struct tsl2563_platform_data *pdata = client->dev.platform_data;
818 int err = 0;
819 int ret;
820 u8 id;
822 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
823 if (!chip)
824 return -ENOMEM;
826 INIT_WORK(&chip->work_thresh, tsl2563_int_bh);
827 i2c_set_clientdata(client, chip);
828 chip->client = client;
830 err = tsl2563_detect(chip);
831 if (err) {
832 dev_err(&client->dev, "device not found, error %d\n", -err);
833 goto fail1;
836 err = tsl2563_read_id(chip, &id);
837 if (err)
838 goto fail1;
840 mutex_init(&chip->lock);
842 /* Default values used until userspace says otherwise */
843 chip->low_thres = 0x0;
844 chip->high_thres = 0xffff;
845 chip->gainlevel = tsl2563_gainlevel_table;
846 chip->intr = TSL2563_INT_PERSIST(4);
847 chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
848 chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
850 if (pdata)
851 chip->cover_comp_gain = pdata->cover_comp_gain;
852 else
853 chip->cover_comp_gain = 1;
855 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
857 chip->indio_dev = iio_allocate_device();
858 if (!chip->indio_dev)
859 goto fail1;
860 chip->indio_dev->attrs = &tsl2563_group;
861 chip->indio_dev->dev.parent = &client->dev;
862 chip->indio_dev->dev_data = (void *)(chip);
863 chip->indio_dev->driver_module = THIS_MODULE;
864 chip->indio_dev->modes = INDIO_DIRECT_MODE;
865 if (client->irq) {
866 chip->indio_dev->num_interrupt_lines = 1;
867 chip->indio_dev->event_attrs
868 = &tsl2563_event_attribute_group;
870 ret = iio_device_register(chip->indio_dev);
871 if (ret)
872 goto fail1;
874 if (client->irq) {
875 ret = iio_register_interrupt_line(client->irq,
876 chip->indio_dev,
878 IRQF_TRIGGER_RISING,
879 client->name);
880 if (ret)
881 goto fail2;
883 err = tsl2563_configure(chip);
884 if (err)
885 goto fail3;
887 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
888 /* The interrupt cannot yet be enabled so this is fine without lock */
889 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
891 return 0;
892 fail3:
893 if (client->irq)
894 iio_unregister_interrupt_line(chip->indio_dev, 0);
895 fail2:
896 iio_device_unregister(chip->indio_dev);
897 fail1:
898 kfree(chip);
899 return err;
902 static int tsl2563_remove(struct i2c_client *client)
904 struct tsl2563_chip *chip = i2c_get_clientdata(client);
905 if (!chip->int_enabled)
906 cancel_delayed_work(&chip->poweroff_work);
907 /* Ensure that interrupts are disabled - then flush any bottom halves */
908 chip->intr |= ~0x30;
909 tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr);
910 flush_scheduled_work();
911 tsl2563_set_power(chip, 0);
912 if (client->irq)
913 iio_unregister_interrupt_line(chip->indio_dev, 0);
914 iio_device_unregister(chip->indio_dev);
916 kfree(chip);
917 return 0;
920 static int tsl2563_suspend(struct i2c_client *client, pm_message_t state)
922 struct tsl2563_chip *chip = i2c_get_clientdata(client);
923 int ret;
925 mutex_lock(&chip->lock);
927 ret = tsl2563_set_power(chip, 0);
928 if (ret)
929 goto out;
931 chip->state = state;
933 out:
934 mutex_unlock(&chip->lock);
935 return ret;
938 static int tsl2563_resume(struct i2c_client *client)
940 struct tsl2563_chip *chip = i2c_get_clientdata(client);
941 int ret;
943 mutex_lock(&chip->lock);
945 ret = tsl2563_set_power(chip, 1);
946 if (ret)
947 goto out;
949 ret = tsl2563_configure(chip);
950 if (ret)
951 goto out;
953 chip->state.event = PM_EVENT_ON;
955 out:
956 mutex_unlock(&chip->lock);
957 return ret;
960 static const struct i2c_device_id tsl2563_id[] = {
961 { "tsl2560", 0 },
962 { "tsl2561", 1 },
963 { "tsl2562", 2 },
964 { "tsl2563", 3 },
967 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
969 static struct i2c_driver tsl2563_i2c_driver = {
970 .driver = {
971 .name = "tsl2563",
973 .suspend = tsl2563_suspend,
974 .resume = tsl2563_resume,
975 .probe = tsl2563_probe,
976 .remove = __devexit_p(tsl2563_remove),
977 .id_table = tsl2563_id,
980 static int __init tsl2563_init(void)
982 return i2c_add_driver(&tsl2563_i2c_driver);
985 static void __exit tsl2563_exit(void)
987 i2c_del_driver(&tsl2563_i2c_driver);
990 MODULE_AUTHOR("Nokia Corporation");
991 MODULE_DESCRIPTION("tsl2563 light sensor driver");
992 MODULE_LICENSE("GPL");
994 module_init(tsl2563_init);
995 module_exit(tsl2563_exit);