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>
35 #include <linux/err.h>
36 #include <linux/slab.h>
42 /* Use this many bits for fraction part. */
43 #define ADC_FRAC_BITS (14)
45 /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
46 #define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
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)
57 #define TSL2563_CMD (0x80)
58 #define TSL2563_CLEARINT (0x40)
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)
73 #define TSL2563_CMD_POWER_ON (0x03)
74 #define TSL2563_CMD_POWER_OFF (0x00)
75 #define TSL2563_CTRL_POWER_MASK (0x03)
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)
84 #define TSL2563_INT_DISBLED (0x00)
85 #define TSL2563_INT_LEVEL (0x10)
86 #define TSL2563_INT_PERSIST(n) ((n) & 0x0F)
88 struct tsl2563_gainlevel_coeff {
94 static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
96 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
100 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
104 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
108 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
114 struct tsl2563_chip {
116 struct i2c_client *client;
117 struct delayed_work poweroff_work;
119 /* Remember state for suspend and resume functions */
122 struct tsl2563_gainlevel_coeff const *gainlevel;
129 /* Calibration coefficients */
134 /* Cache current values, to be returned while suspended */
139 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
141 struct i2c_client *client = chip->client;
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);
150 * Return value is 0 for off, 1 for on, or a negative error
151 * code if reading failed.
153 static int tsl2563_get_power(struct tsl2563_chip *chip)
155 struct i2c_client *client = chip->client;
158 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
162 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
165 static int tsl2563_configure(struct tsl2563_chip *chip)
169 ret = i2c_smbus_write_byte_data(chip->client,
170 TSL2563_CMD | TSL2563_REG_TIMING,
171 chip->gainlevel->gaintime);
174 ret = i2c_smbus_write_byte_data(chip->client,
175 TSL2563_CMD | TSL2563_REG_HIGHLOW,
176 chip->high_thres & 0xFF);
179 ret = i2c_smbus_write_byte_data(chip->client,
180 TSL2563_CMD | TSL2563_REG_HIGHHIGH,
181 (chip->high_thres >> 8) & 0xFF);
184 ret = i2c_smbus_write_byte_data(chip->client,
185 TSL2563_CMD | TSL2563_REG_LOWLOW,
186 chip->low_thres & 0xFF);
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
198 static void tsl2563_poweroff_work(struct work_struct *work)
200 struct tsl2563_chip *chip =
201 container_of(work, struct tsl2563_chip, poweroff_work.work);
202 tsl2563_set_power(chip, 0);
205 static int tsl2563_detect(struct tsl2563_chip *chip)
209 ret = tsl2563_set_power(chip, 1);
213 ret = tsl2563_get_power(chip);
217 return ret ? 0 : -ENODEV;
220 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
222 struct i2c_client *client = chip->client;
225 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
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.
240 static int adc_shiftbits(u8 timing)
244 switch (timing & TSL2563_TIMING_MASK) {
245 case TSL2563_TIMING_13MS:
248 case TSL2563_TIMING_100MS:
251 case TSL2563_TIMING_400MS:
256 if (!(timing & TSL2563_TIMING_GAIN16))
262 /* Convert a HW ADC value to normalized scale. */
263 static u32 normalize_adc(u16 adc, u8 timing)
265 return adc << adc_shiftbits(timing);
268 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
272 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
273 case TSL2563_TIMING_13MS:
276 case TSL2563_TIMING_100MS:
283 * TODO: Make sure that we wait at least required delay but why we
284 * have to extend it one tick more?
286 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
289 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
291 struct i2c_client *client = chip->client;
293 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
295 (adc > chip->gainlevel->max) ?
296 chip->gainlevel++ : chip->gainlevel--;
298 i2c_smbus_write_byte_data(client,
299 TSL2563_CMD | TSL2563_REG_TIMING,
300 chip->gainlevel->gaintime);
302 tsl2563_wait_adc(chip);
303 tsl2563_wait_adc(chip);
310 static int tsl2563_get_adc(struct tsl2563_chip *chip)
312 struct i2c_client *client = chip->client;
317 if (chip->state.event != PM_EVENT_ON)
320 if (!chip->int_enabled) {
321 cancel_delayed_work(&chip->poweroff_work);
323 if (!tsl2563_get_power(chip)) {
324 ret = tsl2563_set_power(chip, 1);
327 ret = tsl2563_configure(chip);
330 tsl2563_wait_adc(chip);
335 ret = i2c_smbus_read_word_data(client,
336 TSL2563_CMD | TSL2563_REG_DATA0LOW);
341 ret = i2c_smbus_read_word_data(client,
342 TSL2563_CMD | TSL2563_REG_DATA1LOW);
347 retry = tsl2563_adjust_gainlevel(chip, adc0);
350 chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
351 chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
353 if (!chip->int_enabled)
354 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
361 static inline int calib_to_sysfs(u32 calib)
363 return (int) (((calib * CALIB_BASE_SYSFS) +
364 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
367 static inline u32 calib_from_sysfs(int value)
369 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
373 * Conversions between lux and ADC values.
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
383 struct tsl2563_lux_coeff {
384 unsigned long ch_ratio;
385 unsigned long ch0_coeff;
386 unsigned long ch1_coeff;
389 static const struct tsl2563_lux_coeff lux_table[] = {
391 .ch_ratio = FRAC10K(1300),
392 .ch0_coeff = FRAC10K(315),
393 .ch1_coeff = FRAC10K(262),
395 .ch_ratio = FRAC10K(2600),
396 .ch0_coeff = FRAC10K(337),
397 .ch1_coeff = FRAC10K(430),
399 .ch_ratio = FRAC10K(3900),
400 .ch0_coeff = FRAC10K(363),
401 .ch1_coeff = FRAC10K(529),
403 .ch_ratio = FRAC10K(5200),
404 .ch0_coeff = FRAC10K(392),
405 .ch1_coeff = FRAC10K(605),
407 .ch_ratio = FRAC10K(6500),
408 .ch0_coeff = FRAC10K(229),
409 .ch1_coeff = FRAC10K(291),
411 .ch_ratio = FRAC10K(8000),
412 .ch0_coeff = FRAC10K(157),
413 .ch1_coeff = FRAC10K(180),
415 .ch_ratio = FRAC10K(13000),
416 .ch0_coeff = FRAC10K(34),
417 .ch1_coeff = FRAC10K(26),
419 .ch_ratio = ULONG_MAX,
426 * Convert normalized, scaled ADC values to lux.
428 static unsigned int adc_to_lux(u32 adc0, u32 adc1)
430 const struct tsl2563_lux_coeff *lp = lux_table;
431 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
433 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
435 while (lp->ch_ratio < ratio)
438 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
440 return (unsigned int) (lux >> ADC_FRAC_BITS);
443 /*--------------------------------------------------------------*/
444 /* Sysfs interface */
445 /*--------------------------------------------------------------*/
448 /* Apply calibration coefficient to ADC count. */
449 static u32 calib_adc(u32 adc, u32 calib)
451 unsigned long scaled = adc;
454 scaled >>= CALIB_FRAC_BITS;
459 static int tsl2563_write_raw(struct iio_dev *indio_dev,
460 struct iio_chan_spec const *chan,
465 struct tsl2563_chip *chip = iio_priv(indio_dev);
467 if (chan->channel == 0)
468 chip->calib0 = calib_from_sysfs(val);
470 chip->calib1 = calib_from_sysfs(val);
475 static int tsl2563_read_raw(struct iio_dev *indio_dev,
476 struct iio_chan_spec const *chan,
483 struct tsl2563_chip *chip = iio_priv(indio_dev);
485 mutex_lock(&chip->lock);
488 switch (chan->type) {
490 ret = tsl2563_get_adc(chip);
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);
501 ret = tsl2563_get_adc(chip);
504 if (chan->channel == 0)
515 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
516 if (chan->channel == 0)
517 *val = calib_to_sysfs(chip->calib0);
519 *val = calib_to_sysfs(chip->calib1);
528 mutex_unlock(&chip->lock);
532 static const struct iio_chan_spec tsl2563_channels[] = {
538 .type = IIO_INTENSITY,
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,
544 IIO_EV_BIT(IIO_EV_TYPE_THRESH,
545 IIO_EV_DIR_FALLING)),
547 .type = IIO_INTENSITY,
549 .channel2 = IIO_MOD_LIGHT_IR,
550 .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE),
554 static int tsl2563_read_thresh(struct iio_dev *indio_dev,
558 struct tsl2563_chip *chip = iio_priv(indio_dev);
560 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
561 case IIO_EV_DIR_RISING:
562 *val = chip->high_thres;
564 case IIO_EV_DIR_FALLING:
565 *val = chip->low_thres;
574 static int tsl2563_write_thresh(struct iio_dev *indio_dev,
578 struct tsl2563_chip *chip = iio_priv(indio_dev);
582 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
583 address = TSL2563_REG_HIGHLOW;
585 address = TSL2563_REG_LOWLOW;
586 mutex_lock(&chip->lock);
587 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
591 ret = i2c_smbus_write_byte_data(chip->client,
592 TSL2563_CMD | (address + 1),
594 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
595 chip->high_thres = val;
597 chip->low_thres = val;
600 mutex_unlock(&chip->lock);
605 static irqreturn_t tsl2563_event_handler(int irq, void *private)
607 struct iio_dev *dev_info = private;
608 struct tsl2563_chip *chip = iio_priv(dev_info);
610 iio_push_event(dev_info,
611 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
617 /* clear the interrupt and push the event */
618 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
622 static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
626 struct tsl2563_chip *chip = iio_priv(indio_dev);
629 mutex_lock(&chip->lock);
630 if (state && !(chip->intr & 0x30)) {
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);
639 ret = tsl2563_configure(chip);
643 ret = i2c_smbus_write_byte_data(chip->client,
644 TSL2563_CMD | TSL2563_REG_INT,
646 chip->int_enabled = true;
649 if (!state && (chip->intr & 0x30)) {
651 ret = i2c_smbus_write_byte_data(chip->client,
652 TSL2563_CMD | TSL2563_REG_INT,
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);
659 mutex_unlock(&chip->lock);
664 static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
667 struct tsl2563_chip *chip = iio_priv(indio_dev);
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);
676 ret = !!(ret & 0x30);
682 /*--------------------------------------------------------------*/
683 /* Probe, Attach, Remove */
684 /*--------------------------------------------------------------*/
685 static struct i2c_driver tsl2563_i2c_driver;
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,
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,
703 static int __devinit tsl2563_probe(struct i2c_client *client,
704 const struct i2c_device_id *device_id)
706 struct iio_dev *indio_dev;
707 struct tsl2563_chip *chip;
708 struct tsl2563_platform_data *pdata = client->dev.platform_data;
713 indio_dev = iio_allocate_device(sizeof(*chip));
717 chip = iio_priv(indio_dev);
719 i2c_set_clientdata(client, chip);
720 chip->client = client;
722 err = tsl2563_detect(chip);
724 dev_err(&client->dev, "device not found, error %d\n", -err);
728 err = tsl2563_read_id(chip, &id);
732 mutex_init(&chip->lock);
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);
743 chip->cover_comp_gain = pdata->cover_comp_gain;
745 chip->cover_comp_gain = 1;
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;
754 indio_dev->info = &tsl2563_info;
756 indio_dev->info = &tsl2563_info_no_irq;
758 ret = request_threaded_irq(client->irq,
760 &tsl2563_event_handler,
761 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
767 err = tsl2563_configure(chip);
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);
775 ret = iio_device_register(indio_dev);
782 free_irq(client->irq, indio_dev);
784 iio_free_device(indio_dev);
790 static int tsl2563_remove(struct i2c_client *client)
792 struct tsl2563_chip *chip = i2c_get_clientdata(client);
793 struct iio_dev *indio_dev = iio_priv_to_dev(chip);
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 */
800 i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
802 flush_scheduled_work();
803 tsl2563_set_power(chip, 0);
805 free_irq(client->irq, indio_dev);
807 iio_free_device(indio_dev);
812 static int tsl2563_suspend(struct i2c_client *client, pm_message_t state)
814 struct tsl2563_chip *chip = i2c_get_clientdata(client);
817 mutex_lock(&chip->lock);
819 ret = tsl2563_set_power(chip, 0);
826 mutex_unlock(&chip->lock);
830 static int tsl2563_resume(struct i2c_client *client)
832 struct tsl2563_chip *chip = i2c_get_clientdata(client);
835 mutex_lock(&chip->lock);
837 ret = tsl2563_set_power(chip, 1);
841 ret = tsl2563_configure(chip);
845 chip->state.event = PM_EVENT_ON;
848 mutex_unlock(&chip->lock);
852 static const struct i2c_device_id tsl2563_id[] = {
859 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
861 static struct i2c_driver tsl2563_i2c_driver = {
865 .suspend = tsl2563_suspend,
866 .resume = tsl2563_resume,
867 .probe = tsl2563_probe,
868 .remove = __devexit_p(tsl2563_remove),
869 .id_table = tsl2563_id,
872 static int __init tsl2563_init(void)
874 return i2c_add_driver(&tsl2563_i2c_driver);
877 static void __exit tsl2563_exit(void)
879 i2c_del_driver(&tsl2563_i2c_driver);
882 MODULE_AUTHOR("Nokia Corporation");
883 MODULE_DESCRIPTION("tsl2563 light sensor driver");
884 MODULE_LICENSE("GPL");
886 module_init(tsl2563_init);
887 module_exit(tsl2563_exit);