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