1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/iio/light/tsl2563.c
4 *
5 * Copyright (C) 2008 Nokia Corporation
6 *
7 * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
8 * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
9 *
10 * Converted to IIO driver
11 * Amit Kucheria <amit.kucheria@verdurent.com>
12 */
13
14 #include <linux/bits.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/math.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/pm.h>
24 #include <linux/property.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27
28 #include <linux/iio/events.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/sysfs.h>
31
32 /* Use this many bits for fraction part. */
33 #define ADC_FRAC_BITS 14
34
35 /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
36 #define FRAC10K(f) (((f) * BIT(ADC_FRAC_BITS)) / (10000))
37
38 /* Bits used for fraction in calibration coefficients.*/
39 #define CALIB_FRAC_BITS 10
40 /* Decimal 10^(digits in sysfs presentation) */
41 #define CALIB_BASE_SYSFS 1000
42
43 #define TSL2563_CMD BIT(7)
44 #define TSL2563_CLEARINT BIT(6)
45
46 #define TSL2563_REG_CTRL 0x00
47 #define TSL2563_REG_TIMING 0x01
48 #define TSL2563_REG_LOW 0x02 /* data0 low threshold, 2 bytes */
49 #define TSL2563_REG_HIGH 0x04 /* data0 high threshold, 2 bytes */
50 #define TSL2563_REG_INT 0x06
51 #define TSL2563_REG_ID 0x0a
52 #define TSL2563_REG_DATA0 0x0c /* broadband sensor value, 2 bytes */
53 #define TSL2563_REG_DATA1 0x0e /* infrared sensor value, 2 bytes */
54
55 #define TSL2563_CMD_POWER_ON 0x03
56 #define TSL2563_CMD_POWER_OFF 0x00
57 #define TSL2563_CTRL_POWER_MASK GENMASK(1, 0)
58
59 #define TSL2563_TIMING_13MS 0x00
60 #define TSL2563_TIMING_100MS 0x01
61 #define TSL2563_TIMING_400MS 0x02
62 #define TSL2563_TIMING_MASK GENMASK(1, 0)
63 #define TSL2563_TIMING_GAIN16 0x10
64 #define TSL2563_TIMING_GAIN1 0x00
65
66 #define TSL2563_INT_DISABLED 0x00
67 #define TSL2563_INT_LEVEL 0x10
68 #define TSL2563_INT_MASK GENMASK(5, 4)
69 #define TSL2563_INT_PERSIST(n) ((n) & GENMASK(3, 0))
70
71 struct tsl2563_gainlevel_coeff {
72 u8 gaintime;
73 u16 min;
74 u16 max;
75 };
76
77 static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
78 {
79 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
80 .min = 0,
81 .max = 65534,
82 }, {
83 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
84 .min = 2048,
85 .max = 65534,
86 }, {
87 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
88 .min = 4095,
89 .max = 37177,
90 }, {
91 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
92 .min = 3000,
93 .max = 65535,
94 },
95 };
96
97 struct tsl2563_chip {
98 struct mutex lock;
99 struct i2c_client *client;
100 struct delayed_work poweroff_work;
101
102 /* Remember state for suspend and resume functions */
103 bool suspended;
104
105 struct tsl2563_gainlevel_coeff const *gainlevel;
106
107 u16 low_thres;
108 u16 high_thres;
109 u8 intr;
110 bool int_enabled;
111
112 /* Calibration coefficients */
113 u32 calib0;
114 u32 calib1;
115 int cover_comp_gain;
116
117 /* Cache current values, to be returned while suspended */
118 u32 data0;
119 u32 data1;
120 };
121
tsl2563_set_power(struct tsl2563_chip * chip,int on)122 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
123 {
124 struct i2c_client *client = chip->client;
125 u8 cmd;
126
127 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
128 return i2c_smbus_write_byte_data(client,
129 TSL2563_CMD | TSL2563_REG_CTRL, cmd);
130 }
131
132 /*
133 * Return value is 0 for off, 1 for on, or a negative error
134 * code if reading failed.
135 */
tsl2563_get_power(struct tsl2563_chip * chip)136 static int tsl2563_get_power(struct tsl2563_chip *chip)
137 {
138 struct i2c_client *client = chip->client;
139 int ret;
140
141 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
142 if (ret < 0)
143 return ret;
144
145 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
146 }
147
tsl2563_configure(struct tsl2563_chip * chip)148 static int tsl2563_configure(struct tsl2563_chip *chip)
149 {
150 int ret;
151
152 ret = i2c_smbus_write_byte_data(chip->client,
153 TSL2563_CMD | TSL2563_REG_TIMING,
154 chip->gainlevel->gaintime);
155 if (ret)
156 goto error_ret;
157 ret = i2c_smbus_write_word_data(chip->client,
158 TSL2563_CMD | TSL2563_REG_HIGH,
159 chip->high_thres);
160 if (ret)
161 goto error_ret;
162 ret = i2c_smbus_write_word_data(chip->client,
163 TSL2563_CMD | TSL2563_REG_LOW,
164 chip->low_thres);
165 if (ret)
166 goto error_ret;
167 /*
168 * Interrupt register is automatically written anyway if it is relevant
169 * so is not here.
170 */
171 error_ret:
172 return ret;
173 }
174
tsl2563_poweroff_work(struct work_struct * work)175 static void tsl2563_poweroff_work(struct work_struct *work)
176 {
177 struct tsl2563_chip *chip =
178 container_of(work, struct tsl2563_chip, poweroff_work.work);
179 tsl2563_set_power(chip, 0);
180 }
181
tsl2563_detect(struct tsl2563_chip * chip)182 static int tsl2563_detect(struct tsl2563_chip *chip)
183 {
184 int ret;
185
186 ret = tsl2563_set_power(chip, 1);
187 if (ret)
188 return ret;
189
190 ret = tsl2563_get_power(chip);
191 if (ret < 0)
192 return ret;
193
194 return ret ? 0 : -ENODEV;
195 }
196
tsl2563_read_id(struct tsl2563_chip * chip,u8 * id)197 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
198 {
199 struct i2c_client *client = chip->client;
200 int ret;
201
202 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
203 if (ret < 0)
204 return ret;
205
206 *id = ret;
207
208 return 0;
209 }
210
tsl2563_configure_irq(struct tsl2563_chip * chip,bool enable)211 static int tsl2563_configure_irq(struct tsl2563_chip *chip, bool enable)
212 {
213 int ret;
214
215 chip->intr &= ~TSL2563_INT_MASK;
216 if (enable)
217 chip->intr |= TSL2563_INT_LEVEL;
218
219 ret = i2c_smbus_write_byte_data(chip->client,
220 TSL2563_CMD | TSL2563_REG_INT,
221 chip->intr);
222 if (ret < 0)
223 return ret;
224
225 chip->int_enabled = enable;
226 return 0;
227 }
228
229 /*
230 * "Normalized" ADC value is one obtained with 400ms of integration time and
231 * 16x gain. This function returns the number of bits of shift needed to
232 * convert between normalized values and HW values obtained using given
233 * timing and gain settings.
234 */
tsl2563_adc_shiftbits(u8 timing)235 static int tsl2563_adc_shiftbits(u8 timing)
236 {
237 int shift = 0;
238
239 switch (timing & TSL2563_TIMING_MASK) {
240 case TSL2563_TIMING_13MS:
241 shift += 5;
242 break;
243 case TSL2563_TIMING_100MS:
244 shift += 2;
245 break;
246 case TSL2563_TIMING_400MS:
247 /* no-op */
248 break;
249 }
250
251 if (!(timing & TSL2563_TIMING_GAIN16))
252 shift += 4;
253
254 return shift;
255 }
256
257 /* Convert a HW ADC value to normalized scale. */
tsl2563_normalize_adc(u16 adc,u8 timing)258 static u32 tsl2563_normalize_adc(u16 adc, u8 timing)
259 {
260 return adc << tsl2563_adc_shiftbits(timing);
261 }
262
tsl2563_wait_adc(struct tsl2563_chip * chip)263 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
264 {
265 unsigned int delay;
266
267 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
268 case TSL2563_TIMING_13MS:
269 delay = 14;
270 break;
271 case TSL2563_TIMING_100MS:
272 delay = 101;
273 break;
274 default:
275 delay = 402;
276 }
277 /*
278 * TODO: Make sure that we wait at least required delay but why we
279 * have to extend it one tick more?
280 */
281 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
282 }
283
tsl2563_adjust_gainlevel(struct tsl2563_chip * chip,u16 adc)284 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
285 {
286 struct i2c_client *client = chip->client;
287
288 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
289
290 (adc > chip->gainlevel->max) ?
291 chip->gainlevel++ : chip->gainlevel--;
292
293 i2c_smbus_write_byte_data(client,
294 TSL2563_CMD | TSL2563_REG_TIMING,
295 chip->gainlevel->gaintime);
296
297 tsl2563_wait_adc(chip);
298 tsl2563_wait_adc(chip);
299
300 return 1;
301 } else
302 return 0;
303 }
304
tsl2563_get_adc(struct tsl2563_chip * chip)305 static int tsl2563_get_adc(struct tsl2563_chip *chip)
306 {
307 struct i2c_client *client = chip->client;
308 u16 adc0, adc1;
309 int retry = 1;
310 int ret = 0;
311
312 if (chip->suspended)
313 goto out;
314
315 if (!chip->int_enabled) {
316 cancel_delayed_work_sync(&chip->poweroff_work);
317
318 if (!tsl2563_get_power(chip)) {
319 ret = tsl2563_set_power(chip, 1);
320 if (ret)
321 goto out;
322 ret = tsl2563_configure(chip);
323 if (ret)
324 goto out;
325 tsl2563_wait_adc(chip);
326 }
327 }
328
329 while (retry) {
330 ret = i2c_smbus_read_word_data(client,
331 TSL2563_CMD | TSL2563_REG_DATA0);
332 if (ret < 0)
333 goto out;
334 adc0 = ret;
335
336 ret = i2c_smbus_read_word_data(client,
337 TSL2563_CMD | TSL2563_REG_DATA1);
338 if (ret < 0)
339 goto out;
340 adc1 = ret;
341
342 retry = tsl2563_adjust_gainlevel(chip, adc0);
343 }
344
345 chip->data0 = tsl2563_normalize_adc(adc0, chip->gainlevel->gaintime);
346 chip->data1 = tsl2563_normalize_adc(adc1, chip->gainlevel->gaintime);
347
348 if (!chip->int_enabled)
349 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
350
351 ret = 0;
352 out:
353 return ret;
354 }
355
tsl2563_calib_to_sysfs(u32 calib)356 static inline int tsl2563_calib_to_sysfs(u32 calib)
357 {
358 return (int)DIV_ROUND_CLOSEST(calib * CALIB_BASE_SYSFS, BIT(CALIB_FRAC_BITS));
359 }
360
tsl2563_calib_from_sysfs(int value)361 static inline u32 tsl2563_calib_from_sysfs(int value)
362 {
363 /* Make a fraction from a number n that was multiplied with b. */
364 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
365 }
366
367 /*
368 * Conversions between lux and ADC values.
369 *
370 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
371 * appropriate constants. Different constants are needed for different
372 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
373 * of the intensities in infrared and visible wavelengths). lux_table below
374 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
375 * constants.
376 */
377
378 struct tsl2563_lux_coeff {
379 unsigned long ch_ratio;
380 unsigned long ch0_coeff;
381 unsigned long ch1_coeff;
382 };
383
384 static const struct tsl2563_lux_coeff lux_table[] = {
385 {
386 .ch_ratio = FRAC10K(1300),
387 .ch0_coeff = FRAC10K(315),
388 .ch1_coeff = FRAC10K(262),
389 }, {
390 .ch_ratio = FRAC10K(2600),
391 .ch0_coeff = FRAC10K(337),
392 .ch1_coeff = FRAC10K(430),
393 }, {
394 .ch_ratio = FRAC10K(3900),
395 .ch0_coeff = FRAC10K(363),
396 .ch1_coeff = FRAC10K(529),
397 }, {
398 .ch_ratio = FRAC10K(5200),
399 .ch0_coeff = FRAC10K(392),
400 .ch1_coeff = FRAC10K(605),
401 }, {
402 .ch_ratio = FRAC10K(6500),
403 .ch0_coeff = FRAC10K(229),
404 .ch1_coeff = FRAC10K(291),
405 }, {
406 .ch_ratio = FRAC10K(8000),
407 .ch0_coeff = FRAC10K(157),
408 .ch1_coeff = FRAC10K(180),
409 }, {
410 .ch_ratio = FRAC10K(13000),
411 .ch0_coeff = FRAC10K(34),
412 .ch1_coeff = FRAC10K(26),
413 }, {
414 .ch_ratio = ULONG_MAX,
415 .ch0_coeff = 0,
416 .ch1_coeff = 0,
417 },
418 };
419
420 /* Convert normalized, scaled ADC values to lux. */
tsl2563_adc_to_lux(u32 adc0,u32 adc1)421 static unsigned int tsl2563_adc_to_lux(u32 adc0, u32 adc1)
422 {
423 const struct tsl2563_lux_coeff *lp = lux_table;
424 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
425
426 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
427
428 while (lp->ch_ratio < ratio)
429 lp++;
430
431 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
432
433 return (unsigned int) (lux >> ADC_FRAC_BITS);
434 }
435
436 /* Apply calibration coefficient to ADC count. */
tsl2563_calib_adc(u32 adc,u32 calib)437 static u32 tsl2563_calib_adc(u32 adc, u32 calib)
438 {
439 unsigned long scaled = adc;
440
441 scaled *= calib;
442 scaled >>= CALIB_FRAC_BITS;
443
444 return (u32) scaled;
445 }
446
tsl2563_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)447 static int tsl2563_write_raw(struct iio_dev *indio_dev,
448 struct iio_chan_spec const *chan,
449 int val,
450 int val2,
451 long mask)
452 {
453 struct tsl2563_chip *chip = iio_priv(indio_dev);
454
455 if (mask != IIO_CHAN_INFO_CALIBSCALE)
456 return -EINVAL;
457 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
458 chip->calib0 = tsl2563_calib_from_sysfs(val);
459 else if (chan->channel2 == IIO_MOD_LIGHT_IR)
460 chip->calib1 = tsl2563_calib_from_sysfs(val);
461 else
462 return -EINVAL;
463
464 return 0;
465 }
466
tsl2563_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)467 static int tsl2563_read_raw(struct iio_dev *indio_dev,
468 struct iio_chan_spec const *chan,
469 int *val,
470 int *val2,
471 long mask)
472 {
473 int ret = -EINVAL;
474 u32 calib0, calib1;
475 struct tsl2563_chip *chip = iio_priv(indio_dev);
476
477 mutex_lock(&chip->lock);
478 switch (mask) {
479 case IIO_CHAN_INFO_RAW:
480 case IIO_CHAN_INFO_PROCESSED:
481 switch (chan->type) {
482 case IIO_LIGHT:
483 ret = tsl2563_get_adc(chip);
484 if (ret)
485 goto error_ret;
486 calib0 = tsl2563_calib_adc(chip->data0, chip->calib0) *
487 chip->cover_comp_gain;
488 calib1 = tsl2563_calib_adc(chip->data1, chip->calib1) *
489 chip->cover_comp_gain;
490 *val = tsl2563_adc_to_lux(calib0, calib1);
491 ret = IIO_VAL_INT;
492 break;
493 case IIO_INTENSITY:
494 ret = tsl2563_get_adc(chip);
495 if (ret)
496 goto error_ret;
497 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
498 *val = chip->data0;
499 else
500 *val = chip->data1;
501 ret = IIO_VAL_INT;
502 break;
503 default:
504 break;
505 }
506 break;
507
508 case IIO_CHAN_INFO_CALIBSCALE:
509 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
510 *val = tsl2563_calib_to_sysfs(chip->calib0);
511 else
512 *val = tsl2563_calib_to_sysfs(chip->calib1);
513 ret = IIO_VAL_INT;
514 break;
515 default:
516 ret = -EINVAL;
517 goto error_ret;
518 }
519
520 error_ret:
521 mutex_unlock(&chip->lock);
522 return ret;
523 }
524
525 static const struct iio_event_spec tsl2563_events[] = {
526 {
527 .type = IIO_EV_TYPE_THRESH,
528 .dir = IIO_EV_DIR_RISING,
529 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
530 BIT(IIO_EV_INFO_ENABLE),
531 }, {
532 .type = IIO_EV_TYPE_THRESH,
533 .dir = IIO_EV_DIR_FALLING,
534 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
535 BIT(IIO_EV_INFO_ENABLE),
536 },
537 };
538
539 static const struct iio_chan_spec tsl2563_channels[] = {
540 {
541 .type = IIO_LIGHT,
542 .indexed = 1,
543 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
544 .channel = 0,
545 }, {
546 .type = IIO_INTENSITY,
547 .modified = 1,
548 .channel2 = IIO_MOD_LIGHT_BOTH,
549 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
550 BIT(IIO_CHAN_INFO_CALIBSCALE),
551 .event_spec = tsl2563_events,
552 .num_event_specs = ARRAY_SIZE(tsl2563_events),
553 }, {
554 .type = IIO_INTENSITY,
555 .modified = 1,
556 .channel2 = IIO_MOD_LIGHT_IR,
557 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
558 BIT(IIO_CHAN_INFO_CALIBSCALE),
559 }
560 };
561
tsl2563_read_thresh(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)562 static int tsl2563_read_thresh(struct iio_dev *indio_dev,
563 const struct iio_chan_spec *chan, enum iio_event_type type,
564 enum iio_event_direction dir, enum iio_event_info info, int *val,
565 int *val2)
566 {
567 struct tsl2563_chip *chip = iio_priv(indio_dev);
568
569 switch (dir) {
570 case IIO_EV_DIR_RISING:
571 *val = chip->high_thres;
572 break;
573 case IIO_EV_DIR_FALLING:
574 *val = chip->low_thres;
575 break;
576 default:
577 return -EINVAL;
578 }
579
580 return IIO_VAL_INT;
581 }
582
tsl2563_write_thresh(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)583 static int tsl2563_write_thresh(struct iio_dev *indio_dev,
584 const struct iio_chan_spec *chan, enum iio_event_type type,
585 enum iio_event_direction dir, enum iio_event_info info, int val,
586 int val2)
587 {
588 struct tsl2563_chip *chip = iio_priv(indio_dev);
589 int ret;
590
591 mutex_lock(&chip->lock);
592
593 if (dir == IIO_EV_DIR_RISING)
594 ret = i2c_smbus_write_word_data(chip->client,
595 TSL2563_CMD | TSL2563_REG_HIGH, val);
596 else
597 ret = i2c_smbus_write_word_data(chip->client,
598 TSL2563_CMD | TSL2563_REG_LOW, val);
599 if (ret)
600 goto error_ret;
601
602 if (dir == IIO_EV_DIR_RISING)
603 chip->high_thres = val;
604 else
605 chip->low_thres = val;
606
607 error_ret:
608 mutex_unlock(&chip->lock);
609
610 return ret;
611 }
612
tsl2563_event_handler(int irq,void * private)613 static irqreturn_t tsl2563_event_handler(int irq, void *private)
614 {
615 struct iio_dev *dev_info = private;
616 struct tsl2563_chip *chip = iio_priv(dev_info);
617
618 iio_push_event(dev_info,
619 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY,
620 0,
621 IIO_EV_TYPE_THRESH,
622 IIO_EV_DIR_EITHER),
623 iio_get_time_ns(dev_info));
624
625 /* clear the interrupt and push the event */
626 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
627 return IRQ_HANDLED;
628 }
629
tsl2563_write_interrupt_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,bool state)630 static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
631 const struct iio_chan_spec *chan, enum iio_event_type type,
632 enum iio_event_direction dir, bool state)
633 {
634 struct tsl2563_chip *chip = iio_priv(indio_dev);
635 int ret = 0;
636
637 mutex_lock(&chip->lock);
638 if (state && !(chip->intr & TSL2563_INT_MASK)) {
639 /* ensure the chip is actually on */
640 cancel_delayed_work_sync(&chip->poweroff_work);
641 if (!tsl2563_get_power(chip)) {
642 ret = tsl2563_set_power(chip, 1);
643 if (ret)
644 goto out;
645 ret = tsl2563_configure(chip);
646 if (ret)
647 goto out;
648 }
649 ret = tsl2563_configure_irq(chip, true);
650 }
651
652 if (!state && (chip->intr & TSL2563_INT_MASK)) {
653 ret = tsl2563_configure_irq(chip, false);
654 /* now the interrupt is not enabled, we can go to sleep */
655 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
656 }
657 out:
658 mutex_unlock(&chip->lock);
659
660 return ret;
661 }
662
tsl2563_read_interrupt_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)663 static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
664 const struct iio_chan_spec *chan, enum iio_event_type type,
665 enum iio_event_direction dir)
666 {
667 struct tsl2563_chip *chip = iio_priv(indio_dev);
668 int ret;
669
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);
674 if (ret < 0)
675 return ret;
676
677 return !!(ret & TSL2563_INT_MASK);
678 }
679
680 static const struct iio_info tsl2563_info_no_irq = {
681 .read_raw = &tsl2563_read_raw,
682 .write_raw = &tsl2563_write_raw,
683 };
684
685 static const struct iio_info tsl2563_info = {
686 .read_raw = &tsl2563_read_raw,
687 .write_raw = &tsl2563_write_raw,
688 .read_event_value = &tsl2563_read_thresh,
689 .write_event_value = &tsl2563_write_thresh,
690 .read_event_config = &tsl2563_read_interrupt_config,
691 .write_event_config = &tsl2563_write_interrupt_config,
692 };
693
tsl2563_probe(struct i2c_client * client)694 static int tsl2563_probe(struct i2c_client *client)
695 {
696 struct device *dev = &client->dev;
697 struct iio_dev *indio_dev;
698 struct tsl2563_chip *chip;
699 unsigned long irq_flags;
700 u8 id = 0;
701 int err;
702
703 indio_dev = devm_iio_device_alloc(dev, sizeof(*chip));
704 if (!indio_dev)
705 return -ENOMEM;
706
707 chip = iio_priv(indio_dev);
708
709 i2c_set_clientdata(client, indio_dev);
710 chip->client = client;
711
712 err = tsl2563_detect(chip);
713 if (err)
714 return dev_err_probe(dev, err, "detect error\n");
715
716 err = tsl2563_read_id(chip, &id);
717 if (err)
718 return dev_err_probe(dev, err, "read id error\n");
719
720 mutex_init(&chip->lock);
721
722 /* Default values used until userspace says otherwise */
723 chip->low_thres = 0x0;
724 chip->high_thres = 0xffff;
725 chip->gainlevel = tsl2563_gainlevel_table;
726 chip->intr = TSL2563_INT_PERSIST(4);
727 chip->calib0 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
728 chip->calib1 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
729
730 chip->cover_comp_gain = 1;
731 device_property_read_u32(dev, "amstaos,cover-comp-gain", &chip->cover_comp_gain);
732
733 dev_info(dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
734 indio_dev->name = client->name;
735 indio_dev->channels = tsl2563_channels;
736 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
737 indio_dev->modes = INDIO_DIRECT_MODE;
738
739 if (client->irq)
740 indio_dev->info = &tsl2563_info;
741 else
742 indio_dev->info = &tsl2563_info_no_irq;
743
744 if (client->irq) {
745 irq_flags = irq_get_trigger_type(client->irq);
746 if (irq_flags == IRQF_TRIGGER_NONE)
747 irq_flags = IRQF_TRIGGER_RISING;
748 irq_flags |= IRQF_ONESHOT;
749
750 err = devm_request_threaded_irq(dev, client->irq,
751 NULL,
752 &tsl2563_event_handler,
753 irq_flags,
754 "tsl2563_event",
755 indio_dev);
756 if (err)
757 return err;
758 }
759
760 err = tsl2563_configure(chip);
761 if (err)
762 return dev_err_probe(dev, err, "configure error\n");
763
764 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
765
766 /* The interrupt cannot yet be enabled so this is fine without lock */
767 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
768
769 err = iio_device_register(indio_dev);
770 if (err) {
771 dev_err_probe(dev, err, "iio registration error\n");
772 goto fail;
773 }
774
775 return 0;
776
777 fail:
778 cancel_delayed_work_sync(&chip->poweroff_work);
779 return err;
780 }
781
tsl2563_remove(struct i2c_client * client)782 static void tsl2563_remove(struct i2c_client *client)
783 {
784 struct iio_dev *indio_dev = i2c_get_clientdata(client);
785 struct tsl2563_chip *chip = iio_priv(indio_dev);
786
787 iio_device_unregister(indio_dev);
788 if (!chip->int_enabled)
789 cancel_delayed_work_sync(&chip->poweroff_work);
790 /* Ensure that interrupts are disabled - then flush any bottom halves */
791 tsl2563_configure_irq(chip, false);
792 tsl2563_set_power(chip, 0);
793 }
794
tsl2563_suspend(struct device * dev)795 static int tsl2563_suspend(struct device *dev)
796 {
797 struct iio_dev *indio_dev = dev_get_drvdata(dev);
798 struct tsl2563_chip *chip = iio_priv(indio_dev);
799 int ret;
800
801 mutex_lock(&chip->lock);
802
803 ret = tsl2563_set_power(chip, 0);
804 if (ret)
805 goto out;
806
807 chip->suspended = true;
808
809 out:
810 mutex_unlock(&chip->lock);
811 return ret;
812 }
813
tsl2563_resume(struct device * dev)814 static int tsl2563_resume(struct device *dev)
815 {
816 struct iio_dev *indio_dev = dev_get_drvdata(dev);
817 struct tsl2563_chip *chip = iio_priv(indio_dev);
818 int ret;
819
820 mutex_lock(&chip->lock);
821
822 ret = tsl2563_set_power(chip, 1);
823 if (ret)
824 goto out;
825
826 ret = tsl2563_configure(chip);
827 if (ret)
828 goto out;
829
830 chip->suspended = false;
831
832 out:
833 mutex_unlock(&chip->lock);
834 return ret;
835 }
836
837 static DEFINE_SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend,
838 tsl2563_resume);
839
840 static const struct i2c_device_id tsl2563_id[] = {
841 { .name = "tsl2560" },
842 { .name = "tsl2561" },
843 { .name = "tsl2562" },
844 { .name = "tsl2563" },
845 { }
846 };
847 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
848
849 static const struct of_device_id tsl2563_of_match[] = {
850 { .compatible = "amstaos,tsl2560" },
851 { .compatible = "amstaos,tsl2561" },
852 { .compatible = "amstaos,tsl2562" },
853 { .compatible = "amstaos,tsl2563" },
854 { }
855 };
856 MODULE_DEVICE_TABLE(of, tsl2563_of_match);
857
858 static struct i2c_driver tsl2563_i2c_driver = {
859 .driver = {
860 .name = "tsl2563",
861 .of_match_table = tsl2563_of_match,
862 .pm = pm_sleep_ptr(&tsl2563_pm_ops),
863 },
864 .probe = tsl2563_probe,
865 .remove = tsl2563_remove,
866 .id_table = tsl2563_id,
867 };
868 module_i2c_driver(tsl2563_i2c_driver);
869
870 MODULE_AUTHOR("Nokia Corporation");
871 MODULE_DESCRIPTION("tsl2563 light sensor driver");
872 MODULE_LICENSE("GPL");
873