1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/iio/light/tsl2563.c
5 * Copyright (C) 2008 Nokia Corporation
10 * Converted to IIO driver
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/property.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/sched.h>
21 #include <linux/mutex.h>
22 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/events.h>
30 #include <linux/platform_data/tsl2563.h>
32 /* Use this many bits for fraction part. */
33 #define ADC_FRAC_BITS 14
35 /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
36 #define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
38 /* Bits used for fraction in calibration coefficients.*/
39 #define CALIB_FRAC_BITS 10
40 /* 0.5 in CALIB_FRAC_BITS precision */
41 #define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1))
42 /* Make a fraction from a number n that was multiplied with b. */
43 #define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b))
44 /* Decimal 10^(digits in sysfs presentation) */
45 #define CALIB_BASE_SYSFS 1000
47 #define TSL2563_CMD 0x80
48 #define TSL2563_CLEARINT 0x40
50 #define TSL2563_REG_CTRL 0x00
51 #define TSL2563_REG_TIMING 0x01
52 #define TSL2563_REG_LOWLOW 0x02 /* data0 low threshold, 2 bytes */
53 #define TSL2563_REG_LOWHIGH 0x03
54 #define TSL2563_REG_HIGHLOW 0x04 /* data0 high threshold, 2 bytes */
55 #define TSL2563_REG_HIGHHIGH 0x05
56 #define TSL2563_REG_INT 0x06
57 #define TSL2563_REG_ID 0x0a
58 #define TSL2563_REG_DATA0LOW 0x0c /* broadband sensor value, 2 bytes */
59 #define TSL2563_REG_DATA0HIGH 0x0d
60 #define TSL2563_REG_DATA1LOW 0x0e /* infrared sensor value, 2 bytes */
61 #define TSL2563_REG_DATA1HIGH 0x0f
63 #define TSL2563_CMD_POWER_ON 0x03
64 #define TSL2563_CMD_POWER_OFF 0x00
65 #define TSL2563_CTRL_POWER_MASK 0x03
67 #define TSL2563_TIMING_13MS 0x00
68 #define TSL2563_TIMING_100MS 0x01
69 #define TSL2563_TIMING_400MS 0x02
70 #define TSL2563_TIMING_MASK 0x03
71 #define TSL2563_TIMING_GAIN16 0x10
72 #define TSL2563_TIMING_GAIN1 0x00
74 #define TSL2563_INT_DISABLED 0x00
75 #define TSL2563_INT_LEVEL 0x10
76 #define TSL2563_INT_PERSIST(n) ((n) & 0x0F)
78 struct tsl2563_gainlevel_coeff {
84 static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
86 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
90 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
94 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
98 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
104 struct tsl2563_chip {
106 struct i2c_client *client;
107 struct delayed_work poweroff_work;
109 /* Remember state for suspend and resume functions */
112 struct tsl2563_gainlevel_coeff const *gainlevel;
119 /* Calibration coefficients */
124 /* Cache current values, to be returned while suspended */
129 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
131 struct i2c_client *client = chip->client;
134 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
135 return i2c_smbus_write_byte_data(client,
136 TSL2563_CMD | TSL2563_REG_CTRL, cmd);
140 * Return value is 0 for off, 1 for on, or a negative error
141 * code if reading failed.
143 static int tsl2563_get_power(struct tsl2563_chip *chip)
145 struct i2c_client *client = chip->client;
148 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
152 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
155 static int tsl2563_configure(struct tsl2563_chip *chip)
159 ret = i2c_smbus_write_byte_data(chip->client,
160 TSL2563_CMD | TSL2563_REG_TIMING,
161 chip->gainlevel->gaintime);
164 ret = i2c_smbus_write_byte_data(chip->client,
165 TSL2563_CMD | TSL2563_REG_HIGHLOW,
166 chip->high_thres & 0xFF);
169 ret = i2c_smbus_write_byte_data(chip->client,
170 TSL2563_CMD | TSL2563_REG_HIGHHIGH,
171 (chip->high_thres >> 8) & 0xFF);
174 ret = i2c_smbus_write_byte_data(chip->client,
175 TSL2563_CMD | TSL2563_REG_LOWLOW,
176 chip->low_thres & 0xFF);
179 ret = i2c_smbus_write_byte_data(chip->client,
180 TSL2563_CMD | TSL2563_REG_LOWHIGH,
181 (chip->low_thres >> 8) & 0xFF);
183 * Interrupt register is automatically written anyway if it is relevant
190 static void tsl2563_poweroff_work(struct work_struct *work)
192 struct tsl2563_chip *chip =
193 container_of(work, struct tsl2563_chip, poweroff_work.work);
194 tsl2563_set_power(chip, 0);
197 static int tsl2563_detect(struct tsl2563_chip *chip)
201 ret = tsl2563_set_power(chip, 1);
205 ret = tsl2563_get_power(chip);
209 return ret ? 0 : -ENODEV;
212 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
214 struct i2c_client *client = chip->client;
217 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
227 * "Normalized" ADC value is one obtained with 400ms of integration time and
228 * 16x gain. This function returns the number of bits of shift needed to
229 * convert between normalized values and HW values obtained using given
230 * timing and gain settings.
232 static int tsl2563_adc_shiftbits(u8 timing)
236 switch (timing & TSL2563_TIMING_MASK) {
237 case TSL2563_TIMING_13MS:
240 case TSL2563_TIMING_100MS:
243 case TSL2563_TIMING_400MS:
248 if (!(timing & TSL2563_TIMING_GAIN16))
254 /* Convert a HW ADC value to normalized scale. */
255 static u32 tsl2563_normalize_adc(u16 adc, u8 timing)
257 return adc << tsl2563_adc_shiftbits(timing);
260 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
264 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
265 case TSL2563_TIMING_13MS:
268 case TSL2563_TIMING_100MS:
275 * TODO: Make sure that we wait at least required delay but why we
276 * have to extend it one tick more?
278 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
281 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
283 struct i2c_client *client = chip->client;
285 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
287 (adc > chip->gainlevel->max) ?
288 chip->gainlevel++ : chip->gainlevel--;
290 i2c_smbus_write_byte_data(client,
291 TSL2563_CMD | TSL2563_REG_TIMING,
292 chip->gainlevel->gaintime);
294 tsl2563_wait_adc(chip);
295 tsl2563_wait_adc(chip);
302 static int tsl2563_get_adc(struct tsl2563_chip *chip)
304 struct i2c_client *client = chip->client;
312 if (!chip->int_enabled) {
313 cancel_delayed_work(&chip->poweroff_work);
315 if (!tsl2563_get_power(chip)) {
316 ret = tsl2563_set_power(chip, 1);
319 ret = tsl2563_configure(chip);
322 tsl2563_wait_adc(chip);
327 ret = i2c_smbus_read_word_data(client,
328 TSL2563_CMD | TSL2563_REG_DATA0LOW);
333 ret = i2c_smbus_read_word_data(client,
334 TSL2563_CMD | TSL2563_REG_DATA1LOW);
339 retry = tsl2563_adjust_gainlevel(chip, adc0);
342 chip->data0 = tsl2563_normalize_adc(adc0, chip->gainlevel->gaintime);
343 chip->data1 = tsl2563_normalize_adc(adc1, chip->gainlevel->gaintime);
345 if (!chip->int_enabled)
346 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
353 static inline int tsl2563_calib_to_sysfs(u32 calib)
355 return (int) (((calib * CALIB_BASE_SYSFS) +
356 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
359 static inline u32 tsl2563_calib_from_sysfs(int value)
361 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
365 * Conversions between lux and ADC values.
367 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
368 * appropriate constants. Different constants are needed for different
369 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
370 * of the intensities in infrared and visible wavelengths). lux_table below
371 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
375 struct tsl2563_lux_coeff {
376 unsigned long ch_ratio;
377 unsigned long ch0_coeff;
378 unsigned long ch1_coeff;
381 static const struct tsl2563_lux_coeff lux_table[] = {
383 .ch_ratio = FRAC10K(1300),
384 .ch0_coeff = FRAC10K(315),
385 .ch1_coeff = FRAC10K(262),
387 .ch_ratio = FRAC10K(2600),
388 .ch0_coeff = FRAC10K(337),
389 .ch1_coeff = FRAC10K(430),
391 .ch_ratio = FRAC10K(3900),
392 .ch0_coeff = FRAC10K(363),
393 .ch1_coeff = FRAC10K(529),
395 .ch_ratio = FRAC10K(5200),
396 .ch0_coeff = FRAC10K(392),
397 .ch1_coeff = FRAC10K(605),
399 .ch_ratio = FRAC10K(6500),
400 .ch0_coeff = FRAC10K(229),
401 .ch1_coeff = FRAC10K(291),
403 .ch_ratio = FRAC10K(8000),
404 .ch0_coeff = FRAC10K(157),
405 .ch1_coeff = FRAC10K(180),
407 .ch_ratio = FRAC10K(13000),
408 .ch0_coeff = FRAC10K(34),
409 .ch1_coeff = FRAC10K(26),
411 .ch_ratio = ULONG_MAX,
417 /* Convert normalized, scaled ADC values to lux. */
418 static unsigned int tsl2563_adc_to_lux(u32 adc0, u32 adc1)
420 const struct tsl2563_lux_coeff *lp = lux_table;
421 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
423 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
425 while (lp->ch_ratio < ratio)
428 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
430 return (unsigned int) (lux >> ADC_FRAC_BITS);
433 /* Apply calibration coefficient to ADC count. */
434 static u32 tsl2563_calib_adc(u32 adc, u32 calib)
436 unsigned long scaled = adc;
439 scaled >>= CALIB_FRAC_BITS;
444 static int tsl2563_write_raw(struct iio_dev *indio_dev,
445 struct iio_chan_spec const *chan,
450 struct tsl2563_chip *chip = iio_priv(indio_dev);
452 if (mask != IIO_CHAN_INFO_CALIBSCALE)
454 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
455 chip->calib0 = tsl2563_calib_from_sysfs(val);
456 else if (chan->channel2 == IIO_MOD_LIGHT_IR)
457 chip->calib1 = tsl2563_calib_from_sysfs(val);
464 static int tsl2563_read_raw(struct iio_dev *indio_dev,
465 struct iio_chan_spec const *chan,
472 struct tsl2563_chip *chip = iio_priv(indio_dev);
474 mutex_lock(&chip->lock);
476 case IIO_CHAN_INFO_RAW:
477 case IIO_CHAN_INFO_PROCESSED:
478 switch (chan->type) {
480 ret = tsl2563_get_adc(chip);
483 calib0 = tsl2563_calib_adc(chip->data0, chip->calib0) *
484 chip->cover_comp_gain;
485 calib1 = tsl2563_calib_adc(chip->data1, chip->calib1) *
486 chip->cover_comp_gain;
487 *val = tsl2563_adc_to_lux(calib0, calib1);
491 ret = tsl2563_get_adc(chip);
494 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
505 case IIO_CHAN_INFO_CALIBSCALE:
506 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
507 *val = tsl2563_calib_to_sysfs(chip->calib0);
509 *val = tsl2563_calib_to_sysfs(chip->calib1);
518 mutex_unlock(&chip->lock);
522 static const struct iio_event_spec tsl2563_events[] = {
524 .type = IIO_EV_TYPE_THRESH,
525 .dir = IIO_EV_DIR_RISING,
526 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
527 BIT(IIO_EV_INFO_ENABLE),
529 .type = IIO_EV_TYPE_THRESH,
530 .dir = IIO_EV_DIR_FALLING,
531 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
532 BIT(IIO_EV_INFO_ENABLE),
536 static const struct iio_chan_spec tsl2563_channels[] = {
540 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
543 .type = IIO_INTENSITY,
545 .channel2 = IIO_MOD_LIGHT_BOTH,
546 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
547 BIT(IIO_CHAN_INFO_CALIBSCALE),
548 .event_spec = tsl2563_events,
549 .num_event_specs = ARRAY_SIZE(tsl2563_events),
551 .type = IIO_INTENSITY,
553 .channel2 = IIO_MOD_LIGHT_IR,
554 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
555 BIT(IIO_CHAN_INFO_CALIBSCALE),
559 static int tsl2563_read_thresh(struct iio_dev *indio_dev,
560 const struct iio_chan_spec *chan, enum iio_event_type type,
561 enum iio_event_direction dir, enum iio_event_info info, int *val,
564 struct tsl2563_chip *chip = iio_priv(indio_dev);
567 case IIO_EV_DIR_RISING:
568 *val = chip->high_thres;
570 case IIO_EV_DIR_FALLING:
571 *val = chip->low_thres;
580 static int tsl2563_write_thresh(struct iio_dev *indio_dev,
581 const struct iio_chan_spec *chan, enum iio_event_type type,
582 enum iio_event_direction dir, enum iio_event_info info, int val,
585 struct tsl2563_chip *chip = iio_priv(indio_dev);
589 if (dir == IIO_EV_DIR_RISING)
590 address = TSL2563_REG_HIGHLOW;
592 address = TSL2563_REG_LOWLOW;
593 mutex_lock(&chip->lock);
594 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
598 ret = i2c_smbus_write_byte_data(chip->client,
599 TSL2563_CMD | (address + 1),
601 if (dir == IIO_EV_DIR_RISING)
602 chip->high_thres = val;
604 chip->low_thres = val;
607 mutex_unlock(&chip->lock);
612 static irqreturn_t tsl2563_event_handler(int irq, void *private)
614 struct iio_dev *dev_info = private;
615 struct tsl2563_chip *chip = iio_priv(dev_info);
617 iio_push_event(dev_info,
618 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY,
622 iio_get_time_ns(dev_info));
624 /* clear the interrupt and push the event */
625 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
629 static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
630 const struct iio_chan_spec *chan, enum iio_event_type type,
631 enum iio_event_direction dir, int state)
633 struct tsl2563_chip *chip = iio_priv(indio_dev);
636 mutex_lock(&chip->lock);
637 if (state && !(chip->intr & 0x30)) {
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);
646 ret = tsl2563_configure(chip);
650 ret = i2c_smbus_write_byte_data(chip->client,
651 TSL2563_CMD | TSL2563_REG_INT,
653 chip->int_enabled = true;
656 if (!state && (chip->intr & 0x30)) {
658 ret = i2c_smbus_write_byte_data(chip->client,
659 TSL2563_CMD | TSL2563_REG_INT,
661 chip->int_enabled = false;
662 /* now the interrupt is not enabled, we can go to sleep */
663 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
666 mutex_unlock(&chip->lock);
671 static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
672 const struct iio_chan_spec *chan, enum iio_event_type type,
673 enum iio_event_direction dir)
675 struct tsl2563_chip *chip = iio_priv(indio_dev);
678 mutex_lock(&chip->lock);
679 ret = i2c_smbus_read_byte_data(chip->client,
680 TSL2563_CMD | TSL2563_REG_INT);
681 mutex_unlock(&chip->lock);
685 return !!(ret & 0x30);
688 static const struct iio_info tsl2563_info_no_irq = {
689 .read_raw = &tsl2563_read_raw,
690 .write_raw = &tsl2563_write_raw,
693 static const struct iio_info tsl2563_info = {
694 .read_raw = &tsl2563_read_raw,
695 .write_raw = &tsl2563_write_raw,
696 .read_event_value = &tsl2563_read_thresh,
697 .write_event_value = &tsl2563_write_thresh,
698 .read_event_config = &tsl2563_read_interrupt_config,
699 .write_event_config = &tsl2563_write_interrupt_config,
702 static int tsl2563_probe(struct i2c_client *client,
703 const struct i2c_device_id *device_id)
705 struct iio_dev *indio_dev;
706 struct tsl2563_chip *chip;
707 struct tsl2563_platform_data *pdata = client->dev.platform_data;
711 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
715 chip = iio_priv(indio_dev);
717 i2c_set_clientdata(client, indio_dev);
718 chip->client = client;
720 err = tsl2563_detect(chip);
722 dev_err(&client->dev, "detect error %d\n", -err);
726 err = tsl2563_read_id(chip, &id);
728 dev_err(&client->dev, "read id error %d\n", -err);
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 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
740 chip->calib1 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
743 chip->cover_comp_gain = pdata->cover_comp_gain;
745 err = device_property_read_u32(&client->dev, "amstaos,cover-comp-gain",
746 &chip->cover_comp_gain);
748 chip->cover_comp_gain = 1;
751 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
752 indio_dev->name = client->name;
753 indio_dev->channels = tsl2563_channels;
754 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
755 indio_dev->modes = INDIO_DIRECT_MODE;
758 indio_dev->info = &tsl2563_info;
760 indio_dev->info = &tsl2563_info_no_irq;
763 err = devm_request_threaded_irq(&client->dev, client->irq,
765 &tsl2563_event_handler,
766 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
770 dev_err(&client->dev, "irq request error %d\n", -err);
775 err = tsl2563_configure(chip);
777 dev_err(&client->dev, "configure error %d\n", -err);
781 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
783 /* The interrupt cannot yet be enabled so this is fine without lock */
784 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
786 err = iio_device_register(indio_dev);
788 dev_err(&client->dev, "iio registration error %d\n", -err);
795 cancel_delayed_work_sync(&chip->poweroff_work);
799 static int tsl2563_remove(struct i2c_client *client)
801 struct iio_dev *indio_dev = i2c_get_clientdata(client);
802 struct tsl2563_chip *chip = iio_priv(indio_dev);
804 iio_device_unregister(indio_dev);
805 if (!chip->int_enabled)
806 cancel_delayed_work(&chip->poweroff_work);
807 /* Ensure that interrupts are disabled - then flush any bottom halves */
809 i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
811 flush_scheduled_work();
812 tsl2563_set_power(chip, 0);
817 #ifdef CONFIG_PM_SLEEP
818 static int tsl2563_suspend(struct device *dev)
820 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
821 struct tsl2563_chip *chip = iio_priv(indio_dev);
824 mutex_lock(&chip->lock);
826 ret = tsl2563_set_power(chip, 0);
830 chip->suspended = true;
833 mutex_unlock(&chip->lock);
837 static int tsl2563_resume(struct device *dev)
839 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
840 struct tsl2563_chip *chip = iio_priv(indio_dev);
843 mutex_lock(&chip->lock);
845 ret = tsl2563_set_power(chip, 1);
849 ret = tsl2563_configure(chip);
853 chip->suspended = false;
856 mutex_unlock(&chip->lock);
860 static SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, tsl2563_resume);
861 #define TSL2563_PM_OPS (&tsl2563_pm_ops)
863 #define TSL2563_PM_OPS NULL
866 static const struct i2c_device_id tsl2563_id[] = {
873 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
875 static const struct of_device_id tsl2563_of_match[] = {
876 { .compatible = "amstaos,tsl2560" },
877 { .compatible = "amstaos,tsl2561" },
878 { .compatible = "amstaos,tsl2562" },
879 { .compatible = "amstaos,tsl2563" },
882 MODULE_DEVICE_TABLE(of, tsl2563_of_match);
884 static struct i2c_driver tsl2563_i2c_driver = {
887 .of_match_table = tsl2563_of_match,
888 .pm = TSL2563_PM_OPS,
890 .probe = tsl2563_probe,
891 .remove = tsl2563_remove,
892 .id_table = tsl2563_id,
894 module_i2c_driver(tsl2563_i2c_driver);
896 MODULE_AUTHOR("Nokia Corporation");
897 MODULE_DESCRIPTION("tsl2563 light sensor driver");
898 MODULE_LICENSE("GPL");