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>
38 #include <linux/iio/iio.h>
39 #include <linux/iio/sysfs.h>
40 #include <linux/iio/events.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 const 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 delayed_work poweroff_work;
120 /* Remember state for suspend and resume functions */
123 struct tsl2563_gainlevel_coeff const *gainlevel;
130 /* Calibration coefficients */
135 /* Cache current values, to be returned while suspended */
140 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
142 struct i2c_client *client = chip->client;
145 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
146 return i2c_smbus_write_byte_data(client,
147 TSL2563_CMD | TSL2563_REG_CTRL, cmd);
151 * Return value is 0 for off, 1 for on, or a negative error
152 * code if reading failed.
154 static int tsl2563_get_power(struct tsl2563_chip *chip)
156 struct i2c_client *client = chip->client;
159 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
163 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
166 static int tsl2563_configure(struct tsl2563_chip *chip)
170 ret = i2c_smbus_write_byte_data(chip->client,
171 TSL2563_CMD | TSL2563_REG_TIMING,
172 chip->gainlevel->gaintime);
175 ret = i2c_smbus_write_byte_data(chip->client,
176 TSL2563_CMD | TSL2563_REG_HIGHLOW,
177 chip->high_thres & 0xFF);
180 ret = i2c_smbus_write_byte_data(chip->client,
181 TSL2563_CMD | TSL2563_REG_HIGHHIGH,
182 (chip->high_thres >> 8) & 0xFF);
185 ret = i2c_smbus_write_byte_data(chip->client,
186 TSL2563_CMD | TSL2563_REG_LOWLOW,
187 chip->low_thres & 0xFF);
190 ret = i2c_smbus_write_byte_data(chip->client,
191 TSL2563_CMD | TSL2563_REG_LOWHIGH,
192 (chip->low_thres >> 8) & 0xFF);
193 /* Interrupt register is automatically written anyway if it is relevant
199 static void tsl2563_poweroff_work(struct work_struct *work)
201 struct tsl2563_chip *chip =
202 container_of(work, struct tsl2563_chip, poweroff_work.work);
203 tsl2563_set_power(chip, 0);
206 static int tsl2563_detect(struct tsl2563_chip *chip)
210 ret = tsl2563_set_power(chip, 1);
214 ret = tsl2563_get_power(chip);
218 return ret ? 0 : -ENODEV;
221 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
223 struct i2c_client *client = chip->client;
226 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
236 * "Normalized" ADC value is one obtained with 400ms of integration time and
237 * 16x gain. This function returns the number of bits of shift needed to
238 * convert between normalized values and HW values obtained using given
239 * timing and gain settings.
241 static int adc_shiftbits(u8 timing)
245 switch (timing & TSL2563_TIMING_MASK) {
246 case TSL2563_TIMING_13MS:
249 case TSL2563_TIMING_100MS:
252 case TSL2563_TIMING_400MS:
257 if (!(timing & TSL2563_TIMING_GAIN16))
263 /* Convert a HW ADC value to normalized scale. */
264 static u32 normalize_adc(u16 adc, u8 timing)
266 return adc << adc_shiftbits(timing);
269 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
273 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
274 case TSL2563_TIMING_13MS:
277 case TSL2563_TIMING_100MS:
284 * TODO: Make sure that we wait at least required delay but why we
285 * have to extend it one tick more?
287 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
290 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
292 struct i2c_client *client = chip->client;
294 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
296 (adc > chip->gainlevel->max) ?
297 chip->gainlevel++ : chip->gainlevel--;
299 i2c_smbus_write_byte_data(client,
300 TSL2563_CMD | TSL2563_REG_TIMING,
301 chip->gainlevel->gaintime);
303 tsl2563_wait_adc(chip);
304 tsl2563_wait_adc(chip);
311 static int tsl2563_get_adc(struct tsl2563_chip *chip)
313 struct i2c_client *client = chip->client;
321 if (!chip->int_enabled) {
322 cancel_delayed_work(&chip->poweroff_work);
324 if (!tsl2563_get_power(chip)) {
325 ret = tsl2563_set_power(chip, 1);
328 ret = tsl2563_configure(chip);
331 tsl2563_wait_adc(chip);
336 ret = i2c_smbus_read_word_data(client,
337 TSL2563_CMD | TSL2563_REG_DATA0LOW);
342 ret = i2c_smbus_read_word_data(client,
343 TSL2563_CMD | TSL2563_REG_DATA1LOW);
348 retry = tsl2563_adjust_gainlevel(chip, adc0);
351 chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
352 chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
354 if (!chip->int_enabled)
355 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
362 static inline int calib_to_sysfs(u32 calib)
364 return (int) (((calib * CALIB_BASE_SYSFS) +
365 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
368 static inline u32 calib_from_sysfs(int value)
370 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
374 * Conversions between lux and ADC values.
376 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
377 * appropriate constants. Different constants are needed for different
378 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
379 * of the intensities in infrared and visible wavelengths). lux_table below
380 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
384 struct tsl2563_lux_coeff {
385 unsigned long ch_ratio;
386 unsigned long ch0_coeff;
387 unsigned long ch1_coeff;
390 static const struct tsl2563_lux_coeff lux_table[] = {
392 .ch_ratio = FRAC10K(1300),
393 .ch0_coeff = FRAC10K(315),
394 .ch1_coeff = FRAC10K(262),
396 .ch_ratio = FRAC10K(2600),
397 .ch0_coeff = FRAC10K(337),
398 .ch1_coeff = FRAC10K(430),
400 .ch_ratio = FRAC10K(3900),
401 .ch0_coeff = FRAC10K(363),
402 .ch1_coeff = FRAC10K(529),
404 .ch_ratio = FRAC10K(5200),
405 .ch0_coeff = FRAC10K(392),
406 .ch1_coeff = FRAC10K(605),
408 .ch_ratio = FRAC10K(6500),
409 .ch0_coeff = FRAC10K(229),
410 .ch1_coeff = FRAC10K(291),
412 .ch_ratio = FRAC10K(8000),
413 .ch0_coeff = FRAC10K(157),
414 .ch1_coeff = FRAC10K(180),
416 .ch_ratio = FRAC10K(13000),
417 .ch0_coeff = FRAC10K(34),
418 .ch1_coeff = FRAC10K(26),
420 .ch_ratio = ULONG_MAX,
427 * Convert normalized, scaled ADC values to lux.
429 static unsigned int adc_to_lux(u32 adc0, u32 adc1)
431 const struct tsl2563_lux_coeff *lp = lux_table;
432 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
434 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
436 while (lp->ch_ratio < ratio)
439 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
441 return (unsigned int) (lux >> ADC_FRAC_BITS);
444 /*--------------------------------------------------------------*/
445 /* Sysfs interface */
446 /*--------------------------------------------------------------*/
449 /* Apply calibration coefficient to ADC count. */
450 static u32 calib_adc(u32 adc, u32 calib)
452 unsigned long scaled = adc;
455 scaled >>= CALIB_FRAC_BITS;
460 static int tsl2563_write_raw(struct iio_dev *indio_dev,
461 struct iio_chan_spec const *chan,
466 struct tsl2563_chip *chip = iio_priv(indio_dev);
468 if (chan->channel == IIO_MOD_LIGHT_BOTH)
469 chip->calib0 = calib_from_sysfs(val);
471 chip->calib1 = calib_from_sysfs(val);
476 static int tsl2563_read_raw(struct iio_dev *indio_dev,
477 struct iio_chan_spec const *chan,
484 struct tsl2563_chip *chip = iio_priv(indio_dev);
486 mutex_lock(&chip->lock);
488 case IIO_CHAN_INFO_RAW:
489 case IIO_CHAN_INFO_PROCESSED:
490 switch (chan->type) {
492 ret = tsl2563_get_adc(chip);
495 calib0 = calib_adc(chip->data0, chip->calib0) *
496 chip->cover_comp_gain;
497 calib1 = calib_adc(chip->data1, chip->calib1) *
498 chip->cover_comp_gain;
499 *val = adc_to_lux(calib0, calib1);
503 ret = tsl2563_get_adc(chip);
506 if (chan->channel == 0)
517 case IIO_CHAN_INFO_CALIBSCALE:
518 if (chan->channel == 0)
519 *val = calib_to_sysfs(chip->calib0);
521 *val = calib_to_sysfs(chip->calib1);
530 mutex_unlock(&chip->lock);
534 static const struct iio_chan_spec tsl2563_channels[] = {
538 .info_mask = IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT,
541 .type = IIO_INTENSITY,
543 .channel2 = IIO_MOD_LIGHT_BOTH,
544 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
545 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
546 .event_mask = (IIO_EV_BIT(IIO_EV_TYPE_THRESH,
548 IIO_EV_BIT(IIO_EV_TYPE_THRESH,
549 IIO_EV_DIR_FALLING)),
551 .type = IIO_INTENSITY,
553 .channel2 = IIO_MOD_LIGHT_IR,
554 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
555 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
559 static int tsl2563_read_thresh(struct iio_dev *indio_dev,
563 struct tsl2563_chip *chip = iio_priv(indio_dev);
565 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
566 case IIO_EV_DIR_RISING:
567 *val = chip->high_thres;
569 case IIO_EV_DIR_FALLING:
570 *val = chip->low_thres;
579 static int tsl2563_write_thresh(struct iio_dev *indio_dev,
583 struct tsl2563_chip *chip = iio_priv(indio_dev);
587 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
588 address = TSL2563_REG_HIGHLOW;
590 address = TSL2563_REG_LOWLOW;
591 mutex_lock(&chip->lock);
592 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
596 ret = i2c_smbus_write_byte_data(chip->client,
597 TSL2563_CMD | (address + 1),
599 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
600 chip->high_thres = val;
602 chip->low_thres = val;
605 mutex_unlock(&chip->lock);
610 static irqreturn_t tsl2563_event_handler(int irq, void *private)
612 struct iio_dev *dev_info = private;
613 struct tsl2563_chip *chip = iio_priv(dev_info);
615 iio_push_event(dev_info,
616 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
622 /* clear the interrupt and push the event */
623 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
627 static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
631 struct tsl2563_chip *chip = iio_priv(indio_dev);
634 mutex_lock(&chip->lock);
635 if (state && !(chip->intr & 0x30)) {
638 /* ensure the chip is actually on */
639 cancel_delayed_work(&chip->poweroff_work);
640 if (!tsl2563_get_power(chip)) {
641 ret = tsl2563_set_power(chip, 1);
644 ret = tsl2563_configure(chip);
648 ret = i2c_smbus_write_byte_data(chip->client,
649 TSL2563_CMD | TSL2563_REG_INT,
651 chip->int_enabled = true;
654 if (!state && (chip->intr & 0x30)) {
656 ret = i2c_smbus_write_byte_data(chip->client,
657 TSL2563_CMD | TSL2563_REG_INT,
659 chip->int_enabled = false;
660 /* now the interrupt is not enabled, we can go to sleep */
661 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
664 mutex_unlock(&chip->lock);
669 static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
672 struct tsl2563_chip *chip = iio_priv(indio_dev);
675 mutex_lock(&chip->lock);
676 ret = i2c_smbus_read_byte_data(chip->client,
677 TSL2563_CMD | TSL2563_REG_INT);
678 mutex_unlock(&chip->lock);
681 ret = !!(ret & 0x30);
687 /*--------------------------------------------------------------*/
688 /* Probe, Attach, Remove */
689 /*--------------------------------------------------------------*/
690 static struct i2c_driver tsl2563_i2c_driver;
692 static const struct iio_info tsl2563_info_no_irq = {
693 .driver_module = THIS_MODULE,
694 .read_raw = &tsl2563_read_raw,
695 .write_raw = &tsl2563_write_raw,
698 static const struct iio_info tsl2563_info = {
699 .driver_module = THIS_MODULE,
700 .read_raw = &tsl2563_read_raw,
701 .write_raw = &tsl2563_write_raw,
702 .read_event_value = &tsl2563_read_thresh,
703 .write_event_value = &tsl2563_write_thresh,
704 .read_event_config = &tsl2563_read_interrupt_config,
705 .write_event_config = &tsl2563_write_interrupt_config,
708 static int tsl2563_probe(struct i2c_client *client,
709 const struct i2c_device_id *device_id)
711 struct iio_dev *indio_dev;
712 struct tsl2563_chip *chip;
713 struct tsl2563_platform_data *pdata = client->dev.platform_data;
717 indio_dev = iio_device_alloc(sizeof(*chip));
721 chip = iio_priv(indio_dev);
723 i2c_set_clientdata(client, chip);
724 chip->client = client;
726 err = tsl2563_detect(chip);
728 dev_err(&client->dev, "detect error %d\n", -err);
732 err = tsl2563_read_id(chip, &id);
734 dev_err(&client->dev, "read id error %d\n", -err);
738 mutex_init(&chip->lock);
740 /* Default values used until userspace says otherwise */
741 chip->low_thres = 0x0;
742 chip->high_thres = 0xffff;
743 chip->gainlevel = tsl2563_gainlevel_table;
744 chip->intr = TSL2563_INT_PERSIST(4);
745 chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
746 chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
749 chip->cover_comp_gain = pdata->cover_comp_gain;
751 chip->cover_comp_gain = 1;
753 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
754 indio_dev->name = client->name;
755 indio_dev->channels = tsl2563_channels;
756 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
757 indio_dev->dev.parent = &client->dev;
758 indio_dev->modes = INDIO_DIRECT_MODE;
761 indio_dev->info = &tsl2563_info;
763 indio_dev->info = &tsl2563_info_no_irq;
766 err = request_threaded_irq(client->irq,
768 &tsl2563_event_handler,
769 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
773 dev_err(&client->dev, "irq request error %d\n", -err);
778 err = tsl2563_configure(chip);
780 dev_err(&client->dev, "configure error %d\n", -err);
784 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
786 /* The interrupt cannot yet be enabled so this is fine without lock */
787 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
789 err = iio_device_register(indio_dev);
791 dev_err(&client->dev, "iio registration error %d\n", -err);
798 cancel_delayed_work(&chip->poweroff_work);
799 flush_scheduled_work();
802 free_irq(client->irq, indio_dev);
804 iio_device_free(indio_dev);
808 static int tsl2563_remove(struct i2c_client *client)
810 struct tsl2563_chip *chip = i2c_get_clientdata(client);
811 struct iio_dev *indio_dev = iio_priv_to_dev(chip);
813 iio_device_unregister(indio_dev);
814 if (!chip->int_enabled)
815 cancel_delayed_work(&chip->poweroff_work);
816 /* Ensure that interrupts are disabled - then flush any bottom halves */
818 i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
820 flush_scheduled_work();
821 tsl2563_set_power(chip, 0);
823 free_irq(client->irq, indio_dev);
825 iio_device_free(indio_dev);
830 #ifdef CONFIG_PM_SLEEP
831 static int tsl2563_suspend(struct device *dev)
833 struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
836 mutex_lock(&chip->lock);
838 ret = tsl2563_set_power(chip, 0);
842 chip->suspended = true;
845 mutex_unlock(&chip->lock);
849 static int tsl2563_resume(struct device *dev)
851 struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
854 mutex_lock(&chip->lock);
856 ret = tsl2563_set_power(chip, 1);
860 ret = tsl2563_configure(chip);
864 chip->suspended = false;
867 mutex_unlock(&chip->lock);
871 static SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, tsl2563_resume);
872 #define TSL2563_PM_OPS (&tsl2563_pm_ops)
874 #define TSL2563_PM_OPS NULL
877 static const struct i2c_device_id tsl2563_id[] = {
884 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
886 static struct i2c_driver tsl2563_i2c_driver = {
889 .pm = TSL2563_PM_OPS,
891 .probe = tsl2563_probe,
892 .remove = tsl2563_remove,
893 .id_table = tsl2563_id,
895 module_i2c_driver(tsl2563_i2c_driver);
897 MODULE_AUTHOR("Nokia Corporation");
898 MODULE_DESCRIPTION("tsl2563 light sensor driver");
899 MODULE_LICENSE("GPL");