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
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>
36 #include <linux/hwmon.h>
37 #include <linux/err.h>
38 #include <linux/slab.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
{
95 static struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table
[] = {
97 .gaintime
= TSL2563_TIMING_400MS
| TSL2563_TIMING_GAIN16
,
101 .gaintime
= TSL2563_TIMING_400MS
| TSL2563_TIMING_GAIN1
,
105 .gaintime
= TSL2563_TIMING_100MS
| TSL2563_TIMING_GAIN1
,
109 .gaintime
= TSL2563_TIMING_13MS
| TSL2563_TIMING_GAIN1
,
115 struct tsl2563_chip
{
117 struct i2c_client
*client
;
118 struct iio_dev
*indio_dev
;
119 struct delayed_work poweroff_work
;
121 struct work_struct work_thresh
;
123 /* Remember state for suspend and resume functions */
126 struct tsl2563_gainlevel_coeff
*gainlevel
;
133 /* Calibration coefficients */
138 /* Cache current values, to be returned while suspended */
143 static int tsl2563_write(struct i2c_client
*client
, u8 reg
, u8 value
)
148 buf
[0] = TSL2563_CMD
| reg
;
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
)
158 u8 cmd
= TSL2563_CMD
| reg
;
160 ret
= i2c_master_send(client
, &cmd
, sizeof(cmd
));
161 if (ret
!= sizeof(cmd
))
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
;
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
;
186 ret
= tsl2563_read(client
, TSL2563_REG_CTRL
, &val
, sizeof(val
));
187 if (ret
!= sizeof(val
))
190 return (val
& TSL2563_CTRL_POWER_MASK
) == TSL2563_CMD_POWER_ON
;
193 static int tsl2563_configure(struct tsl2563_chip
*chip
)
197 ret
= tsl2563_write(chip
->client
, TSL2563_REG_TIMING
,
198 chip
->gainlevel
->gaintime
);
201 ret
= tsl2563_write(chip
->client
, TSL2563_REG_HIGHLOW
,
202 chip
->high_thres
& 0xFF);
205 ret
= tsl2563_write(chip
->client
, TSL2563_REG_HIGHHIGH
,
206 (chip
->high_thres
>> 8) & 0xFF);
209 ret
= tsl2563_write(chip
->client
, TSL2563_REG_LOWLOW
,
210 chip
->low_thres
& 0xFF);
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
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
)
232 ret
= tsl2563_set_power(chip
, 1);
236 ret
= tsl2563_get_power(chip
);
240 return ret
? 0 : -ENODEV
;
243 static int tsl2563_read_id(struct tsl2563_chip
*chip
, u8
*id
)
245 struct i2c_client
*client
= chip
->client
;
248 ret
= tsl2563_read(client
, TSL2563_REG_ID
, id
, sizeof(*id
));
249 if (ret
!= sizeof(*id
))
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
)
265 switch (timing
& TSL2563_TIMING_MASK
) {
266 case TSL2563_TIMING_13MS
:
269 case TSL2563_TIMING_100MS
:
272 case TSL2563_TIMING_400MS
:
277 if (!(timing
& TSL2563_TIMING_GAIN16
))
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
)
293 switch (chip
->gainlevel
->gaintime
& TSL2563_TIMING_MASK
) {
294 case TSL2563_TIMING_13MS
:
297 case TSL2563_TIMING_100MS
:
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
);
330 static int tsl2563_get_adc(struct tsl2563_chip
*chip
)
332 struct i2c_client
*client
= chip
->client
;
338 if (chip
->state
.event
!= PM_EVENT_ON
)
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);
348 ret
= tsl2563_configure(chip
);
351 tsl2563_wait_adc(chip
);
356 ret
= tsl2563_read(client
,
357 TSL2563_REG_DATA0LOW
,
359 if (ret
!= sizeof(buf0
))
362 ret
= tsl2563_read(client
, TSL2563_REG_DATA1LOW
,
364 if (ret
!= sizeof(buf1
))
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
);
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
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),
418 .ch_ratio
= FRAC10K(2600),
419 .ch0_coeff
= FRAC10K(337),
420 .ch1_coeff
= FRAC10K(430),
422 .ch_ratio
= FRAC10K(3900),
423 .ch0_coeff
= FRAC10K(363),
424 .ch1_coeff
= FRAC10K(529),
426 .ch_ratio
= FRAC10K(5200),
427 .ch0_coeff
= FRAC10K(392),
428 .ch1_coeff
= FRAC10K(605),
430 .ch_ratio
= FRAC10K(6500),
431 .ch0_coeff
= FRAC10K(229),
432 .ch1_coeff
= FRAC10K(291),
434 .ch_ratio
= FRAC10K(8000),
435 .ch0_coeff
= FRAC10K(157),
436 .ch1_coeff
= FRAC10K(180),
438 .ch_ratio
= FRAC10K(13000),
439 .ch0_coeff
= FRAC10K(34),
440 .ch1_coeff
= FRAC10K(26),
442 .ch_ratio
= ULONG_MAX
,
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
)
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
);
478 mutex_lock(&chip
->lock
);
480 ret
= tsl2563_get_adc(chip
);
484 switch (this_attr
->address
) {
486 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n", chip
->data0
);
489 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n", chip
->data1
);
493 mutex_unlock(&chip
->lock
);
497 /* Apply calibration coefficient to ADC count. */
498 static u32
calib_adc(u32 adc
, u32 calib
)
500 unsigned long scaled
= adc
;
503 scaled
>>= CALIB_FRAC_BITS
;
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
;
516 mutex_lock(&chip
->lock
);
518 ret
= tsl2563_get_adc(chip
);
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
));
528 mutex_unlock(&chip
->lock
);
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
);
545 mutex_lock(&chip
->lock
);
546 switch (this_attr
->address
) {
548 ret
= format_calib(buf
, PAGE_SIZE
, chip
->calib0
);
551 ret
= format_calib(buf
, PAGE_SIZE
, chip
->calib1
);
556 mutex_unlock(&chip
->lock
);
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
);
570 if (1 != sscanf(buf
, "%d", &value
))
573 calib
= calib_from_sysfs(value
);
575 switch (this_attr
->address
) {
577 chip
->calib0
= calib
;
580 chip
->calib1
= calib
;
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
,
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
,
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
,
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
);
630 switch (this_attr
->address
) {
631 case TSL2563_REG_HIGHLOW
:
632 val
= chip
->high_thres
;
634 case TSL2563_REG_LOWLOW
:
635 val
= chip
->low_thres
;
638 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
641 static ssize_t
tsl2563_write_thresh(struct device
*dev
,
642 struct device_attribute
*attr
,
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
);
652 ret
= strict_strtoul(buf
, 10, &val
);
655 mutex_lock(&chip
->lock
);
656 ret
= tsl2563_write(chip
->client
, this_attr
->address
, val
& 0xFF);
659 ret
= tsl2563_write(chip
->client
, this_attr
->address
+ 1,
661 switch (this_attr
->address
) {
662 case TSL2563_REG_HIGHLOW
:
663 chip
->high_thres
= val
;
665 case TSL2563_REG_LOWLOW
:
666 chip
->low_thres
= val
;
671 mutex_unlock(&chip
->lock
);
673 return ret
< 0 ? ret
: len
;
676 static IIO_DEVICE_ATTR(intensity_both_thresh_high_value
,
679 tsl2563_write_thresh
,
680 TSL2563_REG_HIGHLOW
);
682 static IIO_DEVICE_ATTR(intensity_both_thresh_low_value
,
685 tsl2563_write_thresh
,
688 static int tsl2563_int_th(struct iio_dev
*dev_info
,
693 struct tsl2563_chip
*chip
= dev_info
->dev_data
;
695 chip
->event_timestamp
= timestamp
;
696 schedule_work(&chip
->work_thresh
);
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
);
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
,
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
);
729 ret
= sscanf(buf
, "%d", &input
);
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
);
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);
744 ret
= tsl2563_configure(chip
);
748 ret
= tsl2563_write(chip
->client
, TSL2563_REG_INT
, chip
->intr
);
749 chip
->int_enabled
= true;
752 if (!input
&& (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
);
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
,
771 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
772 struct tsl2563_chip
*chip
= indio_dev
->dev_data
;
777 mutex_lock(&chip
->lock
);
778 ret
= tsl2563_read(chip
->client
,
782 mutex_unlock(&chip
->lock
);
785 len
= snprintf(buf
, PAGE_SIZE
, "%d\n", !!(rxbuf
& 0x30));
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
,
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
,
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
;
822 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
826 INIT_WORK(&chip
->work_thresh
, tsl2563_int_bh
);
827 i2c_set_clientdata(client
, chip
);
828 chip
->client
= client
;
830 err
= tsl2563_detect(chip
);
832 dev_err(&client
->dev
, "device not found, error %d\n", -err
);
836 err
= tsl2563_read_id(chip
, &id
);
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
);
851 chip
->cover_comp_gain
= pdata
->cover_comp_gain
;
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
)
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
;
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
);
875 ret
= iio_register_interrupt_line(client
->irq
,
883 err
= tsl2563_configure(chip
);
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
);
894 iio_unregister_interrupt_line(chip
->indio_dev
, 0);
896 iio_device_unregister(chip
->indio_dev
);
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 */
909 tsl2563_write(chip
->client
, TSL2563_REG_INT
, chip
->intr
);
910 flush_scheduled_work();
911 tsl2563_set_power(chip
, 0);
913 iio_unregister_interrupt_line(chip
->indio_dev
, 0);
914 iio_device_unregister(chip
->indio_dev
);
920 static int tsl2563_suspend(struct i2c_client
*client
, pm_message_t state
)
922 struct tsl2563_chip
*chip
= i2c_get_clientdata(client
);
925 mutex_lock(&chip
->lock
);
927 ret
= tsl2563_set_power(chip
, 0);
934 mutex_unlock(&chip
->lock
);
938 static int tsl2563_resume(struct i2c_client
*client
)
940 struct tsl2563_chip
*chip
= i2c_get_clientdata(client
);
943 mutex_lock(&chip
->lock
);
945 ret
= tsl2563_set_power(chip
, 1);
949 ret
= tsl2563_configure(chip
);
953 chip
->state
.event
= PM_EVENT_ON
;
956 mutex_unlock(&chip
->lock
);
960 static const struct i2c_device_id tsl2563_id
[] = {
967 MODULE_DEVICE_TABLE(i2c
, tsl2563_id
);
969 static struct i2c_driver tsl2563_i2c_driver
= {
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
);