27 #include <linux/module.h>
28 #include <linux/i2c.h>
31 #include <linux/sched.h>
36 #include <linux/slab.h>
44 #define ADC_FRAC_BITS (14)
47 #define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
50 #define CALIB_FRAC_BITS (10)
52 #define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1))
54 #define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b))
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)
64 #define TSL2563_REG_LOWHIGH (0x03)
65 #define TSL2563_REG_HIGHLOW (0x04)
66 #define TSL2563_REG_HIGHHIGH (0x05)
67 #define TSL2563_REG_INT (0x06)
68 #define TSL2563_REG_ID (0x0a)
69 #define TSL2563_REG_DATA0LOW (0x0c)
70 #define TSL2563_REG_DATA0HIGH (0x0d)
71 #define TSL2563_REG_DATA1LOW (0x0e)
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)
203 tsl2563_set_power(chip, 0);
210 ret = tsl2563_set_power(chip, 1);
214 ret = tsl2563_get_power(chip);
266 return adc << adc_shiftbits(timing);
273 switch (chip->
gainlevel->gaintime & TSL2563_TIMING_MASK) {
294 if (adc > chip->
gainlevel->max || adc < chip->gainlevel->min) {
303 tsl2563_wait_adc(chip);
304 tsl2563_wait_adc(chip);
324 if (!tsl2563_get_power(chip)) {
325 ret = tsl2563_set_power(chip, 1);
328 ret = tsl2563_configure(chip);
331 tsl2563_wait_adc(chip);
348 retry = tsl2563_adjust_gainlevel(chip, adc0);
362 static inline int calib_to_sysfs(
u32 calib)
368 static inline u32 calib_from_sysfs(
int value)
429 static unsigned int adc_to_lux(
u32 adc0,
u32 adc1)
432 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
460 static int tsl2563_write_raw(
struct iio_dev *indio_dev,
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,
490 switch (chan->
type) {
492 ret = tsl2563_get_adc(chip);
499 *val = adc_to_lux(calib0, calib1);
503 ret = tsl2563_get_adc(chip);
519 *val = calib_to_sysfs(chip->
calib0);
521 *val = calib_to_sysfs(chip->
calib1);
559 static int tsl2563_read_thresh(
struct iio_dev *indio_dev,
579 static int tsl2563_write_thresh(
struct iio_dev *indio_dev,
610 static irqreturn_t tsl2563_event_handler(
int irq,
void *
private)
627 static int tsl2563_write_interrupt_config(
struct iio_dev *indio_dev,
635 if (state && !(chip->
intr & 0x30)) {
640 if (!tsl2563_get_power(chip)) {
641 ret = tsl2563_set_power(chip, 1);
644 ret = tsl2563_configure(chip);
654 if (!state && (chip->
intr & 0x30)) {
669 static int tsl2563_read_interrupt_config(
struct iio_dev *indio_dev,
681 ret = !!(ret & 0x30);
692 static const struct iio_info tsl2563_info_no_irq = {
694 .read_raw = &tsl2563_read_raw,
695 .write_raw = &tsl2563_write_raw,
698 static const struct iio_info tsl2563_info = {
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,
721 chip = iio_priv(indio_dev);
723 i2c_set_clientdata(client, chip);
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);
743 chip->
gainlevel = tsl2563_gainlevel_table;
753 dev_info(&client->
dev,
"model %d, rev. %d\n",
id >> 4,
id & 0x0f);
755 indio_dev->
channels = tsl2563_channels;
757 indio_dev->
dev.parent = &client->
dev;
761 indio_dev->
info = &tsl2563_info;
763 indio_dev->
info = &tsl2563_info_no_irq;
768 &tsl2563_event_handler,
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);
791 dev_err(&client->
dev,
"iio registration error %d\n", -err);
811 struct iio_dev *indio_dev = iio_priv_to_dev(chip);
821 tsl2563_set_power(chip, 0);
830 #ifdef CONFIG_PM_SLEEP
831 static int tsl2563_suspend(
struct device *
dev)
838 ret = tsl2563_set_power(chip, 0);
849 static int tsl2563_resume(
struct device *dev)
856 ret = tsl2563_set_power(chip, 1);
860 ret = tsl2563_configure(chip);
872 #define TSL2563_PM_OPS (&tsl2563_pm_ops)
874 #define TSL2563_PM_OPS NULL
886 static struct i2c_driver tsl2563_i2c_driver = {
891 .probe = tsl2563_probe,
893 .id_table = tsl2563_id,