Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / light / tsl2563.c
blob5bf2bfbc5379eb013ef373351ace384afcecb317
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/iio/light/tsl2563.c
5 * Copyright (C) 2008 Nokia Corporation
7 * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
8 * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
10 * Converted to IIO driver
11 * Amit Kucheria <amit.kucheria@verdurent.com>
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>
23 #include <linux/pm.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 {
79 u8 gaintime;
80 u16 min;
81 u16 max;
84 static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
86 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
87 .min = 0,
88 .max = 65534,
89 }, {
90 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
91 .min = 2048,
92 .max = 65534,
93 }, {
94 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
95 .min = 4095,
96 .max = 37177,
97 }, {
98 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
99 .min = 3000,
100 .max = 65535,
104 struct tsl2563_chip {
105 struct mutex lock;
106 struct i2c_client *client;
107 struct delayed_work poweroff_work;
109 /* Remember state for suspend and resume functions */
110 bool suspended;
112 struct tsl2563_gainlevel_coeff const *gainlevel;
114 u16 low_thres;
115 u16 high_thres;
116 u8 intr;
117 bool int_enabled;
119 /* Calibration coefficients */
120 u32 calib0;
121 u32 calib1;
122 int cover_comp_gain;
124 /* Cache current values, to be returned while suspended */
125 u32 data0;
126 u32 data1;
129 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
131 struct i2c_client *client = chip->client;
132 u8 cmd;
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;
146 int ret;
148 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
149 if (ret < 0)
150 return ret;
152 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
155 static int tsl2563_configure(struct tsl2563_chip *chip)
157 int ret;
159 ret = i2c_smbus_write_byte_data(chip->client,
160 TSL2563_CMD | TSL2563_REG_TIMING,
161 chip->gainlevel->gaintime);
162 if (ret)
163 goto error_ret;
164 ret = i2c_smbus_write_byte_data(chip->client,
165 TSL2563_CMD | TSL2563_REG_HIGHLOW,
166 chip->high_thres & 0xFF);
167 if (ret)
168 goto error_ret;
169 ret = i2c_smbus_write_byte_data(chip->client,
170 TSL2563_CMD | TSL2563_REG_HIGHHIGH,
171 (chip->high_thres >> 8) & 0xFF);
172 if (ret)
173 goto error_ret;
174 ret = i2c_smbus_write_byte_data(chip->client,
175 TSL2563_CMD | TSL2563_REG_LOWLOW,
176 chip->low_thres & 0xFF);
177 if (ret)
178 goto error_ret;
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
184 * so is not here.
186 error_ret:
187 return ret;
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)
199 int ret;
201 ret = tsl2563_set_power(chip, 1);
202 if (ret)
203 return ret;
205 ret = tsl2563_get_power(chip);
206 if (ret < 0)
207 return ret;
209 return ret ? 0 : -ENODEV;
212 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
214 struct i2c_client *client = chip->client;
215 int ret;
217 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
218 if (ret < 0)
219 return ret;
221 *id = ret;
223 return 0;
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)
234 int shift = 0;
236 switch (timing & TSL2563_TIMING_MASK) {
237 case TSL2563_TIMING_13MS:
238 shift += 5;
239 break;
240 case TSL2563_TIMING_100MS:
241 shift += 2;
242 break;
243 case TSL2563_TIMING_400MS:
244 /* no-op */
245 break;
248 if (!(timing & TSL2563_TIMING_GAIN16))
249 shift += 4;
251 return shift;
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)
262 unsigned int delay;
264 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
265 case TSL2563_TIMING_13MS:
266 delay = 14;
267 break;
268 case TSL2563_TIMING_100MS:
269 delay = 101;
270 break;
271 default:
272 delay = 402;
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);
297 return 1;
298 } else
299 return 0;
302 static int tsl2563_get_adc(struct tsl2563_chip *chip)
304 struct i2c_client *client = chip->client;
305 u16 adc0, adc1;
306 int retry = 1;
307 int ret = 0;
309 if (chip->suspended)
310 goto out;
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);
317 if (ret)
318 goto out;
319 ret = tsl2563_configure(chip);
320 if (ret)
321 goto out;
322 tsl2563_wait_adc(chip);
326 while (retry) {
327 ret = i2c_smbus_read_word_data(client,
328 TSL2563_CMD | TSL2563_REG_DATA0LOW);
329 if (ret < 0)
330 goto out;
331 adc0 = ret;
333 ret = i2c_smbus_read_word_data(client,
334 TSL2563_CMD | TSL2563_REG_DATA1LOW);
335 if (ret < 0)
336 goto out;
337 adc1 = ret;
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);
348 ret = 0;
349 out:
350 return ret;
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
372 * constants.
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),
386 }, {
387 .ch_ratio = FRAC10K(2600),
388 .ch0_coeff = FRAC10K(337),
389 .ch1_coeff = FRAC10K(430),
390 }, {
391 .ch_ratio = FRAC10K(3900),
392 .ch0_coeff = FRAC10K(363),
393 .ch1_coeff = FRAC10K(529),
394 }, {
395 .ch_ratio = FRAC10K(5200),
396 .ch0_coeff = FRAC10K(392),
397 .ch1_coeff = FRAC10K(605),
398 }, {
399 .ch_ratio = FRAC10K(6500),
400 .ch0_coeff = FRAC10K(229),
401 .ch1_coeff = FRAC10K(291),
402 }, {
403 .ch_ratio = FRAC10K(8000),
404 .ch0_coeff = FRAC10K(157),
405 .ch1_coeff = FRAC10K(180),
406 }, {
407 .ch_ratio = FRAC10K(13000),
408 .ch0_coeff = FRAC10K(34),
409 .ch1_coeff = FRAC10K(26),
410 }, {
411 .ch_ratio = ULONG_MAX,
412 .ch0_coeff = 0,
413 .ch1_coeff = 0,
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)
426 lp++;
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;
438 scaled *= calib;
439 scaled >>= CALIB_FRAC_BITS;
441 return (u32) scaled;
444 static int tsl2563_write_raw(struct iio_dev *indio_dev,
445 struct iio_chan_spec const *chan,
446 int val,
447 int val2,
448 long mask)
450 struct tsl2563_chip *chip = iio_priv(indio_dev);
452 if (mask != IIO_CHAN_INFO_CALIBSCALE)
453 return -EINVAL;
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);
458 else
459 return -EINVAL;
461 return 0;
464 static int tsl2563_read_raw(struct iio_dev *indio_dev,
465 struct iio_chan_spec const *chan,
466 int *val,
467 int *val2,
468 long mask)
470 int ret = -EINVAL;
471 u32 calib0, calib1;
472 struct tsl2563_chip *chip = iio_priv(indio_dev);
474 mutex_lock(&chip->lock);
475 switch (mask) {
476 case IIO_CHAN_INFO_RAW:
477 case IIO_CHAN_INFO_PROCESSED:
478 switch (chan->type) {
479 case IIO_LIGHT:
480 ret = tsl2563_get_adc(chip);
481 if (ret)
482 goto error_ret;
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);
488 ret = IIO_VAL_INT;
489 break;
490 case IIO_INTENSITY:
491 ret = tsl2563_get_adc(chip);
492 if (ret)
493 goto error_ret;
494 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
495 *val = chip->data0;
496 else
497 *val = chip->data1;
498 ret = IIO_VAL_INT;
499 break;
500 default:
501 break;
503 break;
505 case IIO_CHAN_INFO_CALIBSCALE:
506 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
507 *val = tsl2563_calib_to_sysfs(chip->calib0);
508 else
509 *val = tsl2563_calib_to_sysfs(chip->calib1);
510 ret = IIO_VAL_INT;
511 break;
512 default:
513 ret = -EINVAL;
514 goto error_ret;
517 error_ret:
518 mutex_unlock(&chip->lock);
519 return ret;
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),
528 }, {
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[] = {
538 .type = IIO_LIGHT,
539 .indexed = 1,
540 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
541 .channel = 0,
542 }, {
543 .type = IIO_INTENSITY,
544 .modified = 1,
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),
550 }, {
551 .type = IIO_INTENSITY,
552 .modified = 1,
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,
562 int *val2)
564 struct tsl2563_chip *chip = iio_priv(indio_dev);
566 switch (dir) {
567 case IIO_EV_DIR_RISING:
568 *val = chip->high_thres;
569 break;
570 case IIO_EV_DIR_FALLING:
571 *val = chip->low_thres;
572 break;
573 default:
574 return -EINVAL;
577 return IIO_VAL_INT;
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,
583 int val2)
585 struct tsl2563_chip *chip = iio_priv(indio_dev);
586 int ret;
587 u8 address;
589 if (dir == IIO_EV_DIR_RISING)
590 address = TSL2563_REG_HIGHLOW;
591 else
592 address = TSL2563_REG_LOWLOW;
593 mutex_lock(&chip->lock);
594 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
595 val & 0xFF);
596 if (ret)
597 goto error_ret;
598 ret = i2c_smbus_write_byte_data(chip->client,
599 TSL2563_CMD | (address + 1),
600 (val >> 8) & 0xFF);
601 if (dir == IIO_EV_DIR_RISING)
602 chip->high_thres = val;
603 else
604 chip->low_thres = val;
606 error_ret:
607 mutex_unlock(&chip->lock);
609 return ret;
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,
620 IIO_EV_TYPE_THRESH,
621 IIO_EV_DIR_EITHER),
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);
626 return IRQ_HANDLED;
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);
634 int ret = 0;
636 mutex_lock(&chip->lock);
637 if (state && !(chip->intr & 0x30)) {
638 chip->intr &= ~0x30;
639 chip->intr |= 0x10;
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);
644 if (ret)
645 goto out;
646 ret = tsl2563_configure(chip);
647 if (ret)
648 goto out;
650 ret = i2c_smbus_write_byte_data(chip->client,
651 TSL2563_CMD | TSL2563_REG_INT,
652 chip->intr);
653 chip->int_enabled = true;
656 if (!state && (chip->intr & 0x30)) {
657 chip->intr &= ~0x30;
658 ret = i2c_smbus_write_byte_data(chip->client,
659 TSL2563_CMD | TSL2563_REG_INT,
660 chip->intr);
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);
665 out:
666 mutex_unlock(&chip->lock);
668 return ret;
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);
676 int ret;
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);
682 if (ret < 0)
683 return ret;
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;
708 int err = 0;
709 u8 id = 0;
711 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
712 if (!indio_dev)
713 return -ENOMEM;
715 chip = iio_priv(indio_dev);
717 i2c_set_clientdata(client, indio_dev);
718 chip->client = client;
720 err = tsl2563_detect(chip);
721 if (err) {
722 dev_err(&client->dev, "detect error %d\n", -err);
723 return err;
726 err = tsl2563_read_id(chip, &id);
727 if (err) {
728 dev_err(&client->dev, "read id error %d\n", -err);
729 return 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);
742 if (pdata) {
743 chip->cover_comp_gain = pdata->cover_comp_gain;
744 } else {
745 err = device_property_read_u32(&client->dev, "amstaos,cover-comp-gain",
746 &chip->cover_comp_gain);
747 if (err)
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;
757 if (client->irq)
758 indio_dev->info = &tsl2563_info;
759 else
760 indio_dev->info = &tsl2563_info_no_irq;
762 if (client->irq) {
763 err = devm_request_threaded_irq(&client->dev, client->irq,
764 NULL,
765 &tsl2563_event_handler,
766 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
767 "tsl2563_event",
768 indio_dev);
769 if (err) {
770 dev_err(&client->dev, "irq request error %d\n", -err);
771 return err;
775 err = tsl2563_configure(chip);
776 if (err) {
777 dev_err(&client->dev, "configure error %d\n", -err);
778 return 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);
787 if (err) {
788 dev_err(&client->dev, "iio registration error %d\n", -err);
789 goto fail;
792 return 0;
794 fail:
795 cancel_delayed_work_sync(&chip->poweroff_work);
796 return err;
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 */
808 chip->intr &= ~0x30;
809 i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
810 chip->intr);
811 flush_scheduled_work();
812 tsl2563_set_power(chip, 0);
814 return 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);
822 int ret;
824 mutex_lock(&chip->lock);
826 ret = tsl2563_set_power(chip, 0);
827 if (ret)
828 goto out;
830 chip->suspended = true;
832 out:
833 mutex_unlock(&chip->lock);
834 return ret;
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);
841 int ret;
843 mutex_lock(&chip->lock);
845 ret = tsl2563_set_power(chip, 1);
846 if (ret)
847 goto out;
849 ret = tsl2563_configure(chip);
850 if (ret)
851 goto out;
853 chip->suspended = false;
855 out:
856 mutex_unlock(&chip->lock);
857 return ret;
860 static SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, tsl2563_resume);
861 #define TSL2563_PM_OPS (&tsl2563_pm_ops)
862 #else
863 #define TSL2563_PM_OPS NULL
864 #endif
866 static const struct i2c_device_id tsl2563_id[] = {
867 { "tsl2560", 0 },
868 { "tsl2561", 1 },
869 { "tsl2562", 2 },
870 { "tsl2563", 3 },
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 = {
885 .driver = {
886 .name = "tsl2563",
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");