1
2
3
4
5
6
7
8
9
10
11#include <linux/delay.h>
12#include <linux/errno.h>
13#include <linux/i2c.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/iio/events.h>
20#include <linux/iio/iio.h>
21#include <linux/iio/sysfs.h>
22#include <linux/platform_data/tsl2772.h>
23
24
25#define PROX_STAT_CAL 0
26#define PROX_STAT_SAMP 1
27#define MAX_SAMPLES_CAL 200
28
29
30#define TRITON_ID 0x00
31#define SWORDFISH_ID 0x30
32#define HALIBUT_ID 0x20
33
34
35#define TSL2772_LUX_CALC_OVER_FLOW 65535
36
37
38
39
40
41
42
43#define TSL2772_MAX_CONFIG_REG 16
44
45
46#define TSL2772_CNTRL 0x00
47#define TSL2772_ALS_TIME 0X01
48#define TSL2772_PRX_TIME 0x02
49#define TSL2772_WAIT_TIME 0x03
50#define TSL2772_ALS_MINTHRESHLO 0X04
51#define TSL2772_ALS_MINTHRESHHI 0X05
52#define TSL2772_ALS_MAXTHRESHLO 0X06
53#define TSL2772_ALS_MAXTHRESHHI 0X07
54#define TSL2772_PRX_MINTHRESHLO 0X08
55#define TSL2772_PRX_MINTHRESHHI 0X09
56#define TSL2772_PRX_MAXTHRESHLO 0X0A
57#define TSL2772_PRX_MAXTHRESHHI 0X0B
58#define TSL2772_PERSISTENCE 0x0C
59#define TSL2772_ALS_PRX_CONFIG 0x0D
60#define TSL2772_PRX_COUNT 0x0E
61#define TSL2772_GAIN 0x0F
62#define TSL2772_NOTUSED 0x10
63#define TSL2772_REVID 0x11
64#define TSL2772_CHIPID 0x12
65#define TSL2772_STATUS 0x13
66#define TSL2772_ALS_CHAN0LO 0x14
67#define TSL2772_ALS_CHAN0HI 0x15
68#define TSL2772_ALS_CHAN1LO 0x16
69#define TSL2772_ALS_CHAN1HI 0x17
70#define TSL2772_PRX_LO 0x18
71#define TSL2772_PRX_HI 0x19
72
73
74#define TSL2772_CMD_REG 0x80
75#define TSL2772_CMD_SPL_FN 0x60
76#define TSL2772_CMD_REPEAT_PROTO 0x00
77#define TSL2772_CMD_AUTOINC_PROTO 0x20
78
79#define TSL2772_CMD_PROX_INT_CLR 0X05
80#define TSL2772_CMD_ALS_INT_CLR 0x06
81#define TSL2772_CMD_PROXALS_INT_CLR 0X07
82
83
84#define TSL2772_CNTL_ADC_ENBL 0x02
85#define TSL2772_CNTL_PWR_ON 0x01
86
87
88#define TSL2772_STA_ADC_VALID 0x01
89#define TSL2772_STA_PRX_VALID 0x02
90#define TSL2772_STA_ADC_PRX_VALID (TSL2772_STA_ADC_VALID | \
91 TSL2772_STA_PRX_VALID)
92#define TSL2772_STA_ALS_INTR 0x10
93#define TSL2772_STA_PRX_INTR 0x20
94
95
96#define TSL2772_CNTL_REG_CLEAR 0x00
97#define TSL2772_CNTL_PROX_INT_ENBL 0X20
98#define TSL2772_CNTL_ALS_INT_ENBL 0X10
99#define TSL2772_CNTL_WAIT_TMR_ENBL 0X08
100#define TSL2772_CNTL_PROX_DET_ENBL 0X04
101#define TSL2772_CNTL_PWRON 0x01
102#define TSL2772_CNTL_ALSPON_ENBL 0x03
103#define TSL2772_CNTL_INTALSPON_ENBL 0x13
104#define TSL2772_CNTL_PROXPON_ENBL 0x0F
105#define TSL2772_CNTL_INTPROXPON_ENBL 0x2F
106
107#define TSL2772_ALS_GAIN_TRIM_MIN 250
108#define TSL2772_ALS_GAIN_TRIM_MAX 4000
109
110
111enum {
112 tsl2571,
113 tsl2671,
114 tmd2671,
115 tsl2771,
116 tmd2771,
117 tsl2572,
118 tsl2672,
119 tmd2672,
120 tsl2772,
121 tmd2772
122};
123
124enum {
125 TSL2772_CHIP_UNKNOWN = 0,
126 TSL2772_CHIP_WORKING = 1,
127 TSL2772_CHIP_SUSPENDED = 2
128};
129
130
131struct tsl2772_als_info {
132 u16 als_ch0;
133 u16 als_ch1;
134 u16 lux;
135};
136
137struct tsl2772_chip_info {
138 int chan_table_elements;
139 struct iio_chan_spec channel_with_events[4];
140 struct iio_chan_spec channel_without_events[4];
141 const struct iio_info *info;
142};
143
144struct tsl2772_chip {
145 kernel_ulong_t id;
146 struct mutex prox_mutex;
147 struct mutex als_mutex;
148 struct i2c_client *client;
149 u16 prox_data;
150 struct tsl2772_als_info als_cur_info;
151 struct tsl2772_settings settings;
152 struct tsl2772_platform_data *pdata;
153 int als_gain_time_scale;
154 int als_saturation;
155 int tsl2772_chip_status;
156 u8 tsl2772_config[TSL2772_MAX_CONFIG_REG];
157 const struct tsl2772_chip_info *chip_info;
158 const struct iio_info *info;
159 s64 event_timestamp;
160
161
162
163
164
165 struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE];
166};
167
168
169
170
171
172
173
174
175
176static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
177 { 53000, 106000 },
178 { 31800, 53000 },
179 { 0, 0 },
180};
181
182static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
183 { 24000, 48000 },
184 { 14400, 24000 },
185 { 0, 0 },
186};
187
188static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
189 { 60000, 112200 },
190 { 37800, 60000 },
191 { 0, 0 },
192};
193
194static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
195 { 20000, 35000 },
196 { 12600, 20000 },
197 { 0, 0 },
198};
199
200static const struct tsl2772_lux *tsl2772_default_lux_table_group[] = {
201 [tsl2571] = tsl2x71_lux_table,
202 [tsl2671] = tsl2x71_lux_table,
203 [tmd2671] = tmd2x71_lux_table,
204 [tsl2771] = tsl2x71_lux_table,
205 [tmd2771] = tmd2x71_lux_table,
206 [tsl2572] = tsl2x72_lux_table,
207 [tsl2672] = tsl2x72_lux_table,
208 [tmd2672] = tmd2x72_lux_table,
209 [tsl2772] = tsl2x72_lux_table,
210 [tmd2772] = tmd2x72_lux_table,
211};
212
213static const struct tsl2772_settings tsl2772_default_settings = {
214 .als_time = 255,
215 .als_gain = 0,
216 .prox_time = 255,
217 .prox_gain = 0,
218 .wait_time = 255,
219 .als_prox_config = 0,
220 .als_gain_trim = 1000,
221 .als_cal_target = 150,
222 .als_persistence = 1,
223 .als_interrupt_en = false,
224 .als_thresh_low = 200,
225 .als_thresh_high = 256,
226 .prox_persistence = 1,
227 .prox_interrupt_en = false,
228 .prox_thres_low = 0,
229 .prox_thres_high = 512,
230 .prox_max_samples_cal = 30,
231 .prox_pulse_count = 8,
232 .prox_diode = TSL2772_DIODE1,
233 .prox_power = TSL2772_100_mA
234};
235
236static const s16 tsl2772_als_gain[] = {
237 1,
238 8,
239 16,
240 120
241};
242
243static const s16 tsl2772_prox_gain[] = {
244 1,
245 2,
246 4,
247 8
248};
249
250static const int tsl2772_int_time_avail[][6] = {
251 [tsl2571] = { 0, 2720, 0, 2720, 0, 696000 },
252 [tsl2671] = { 0, 2720, 0, 2720, 0, 696000 },
253 [tmd2671] = { 0, 2720, 0, 2720, 0, 696000 },
254 [tsl2771] = { 0, 2720, 0, 2720, 0, 696000 },
255 [tmd2771] = { 0, 2720, 0, 2720, 0, 696000 },
256 [tsl2572] = { 0, 2730, 0, 2730, 0, 699000 },
257 [tsl2672] = { 0, 2730, 0, 2730, 0, 699000 },
258 [tmd2672] = { 0, 2730, 0, 2730, 0, 699000 },
259 [tsl2772] = { 0, 2730, 0, 2730, 0, 699000 },
260 [tmd2772] = { 0, 2730, 0, 2730, 0, 699000 },
261};
262
263static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 };
264
265static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 };
266
267
268enum {
269 ALS,
270 PRX,
271 ALSPRX,
272 PRX2,
273 ALSPRX2,
274};
275
276static const u8 device_channel_config[] = {
277 [tsl2571] = ALS,
278 [tsl2671] = PRX,
279 [tmd2671] = PRX,
280 [tsl2771] = ALSPRX,
281 [tmd2771] = ALSPRX,
282 [tsl2572] = ALS,
283 [tsl2672] = PRX2,
284 [tmd2672] = PRX2,
285 [tsl2772] = ALSPRX2,
286 [tmd2772] = ALSPRX2
287};
288
289static int tsl2772_read_status(struct tsl2772_chip *chip)
290{
291 int ret;
292
293 ret = i2c_smbus_read_byte_data(chip->client,
294 TSL2772_CMD_REG | TSL2772_STATUS);
295 if (ret < 0)
296 dev_err(&chip->client->dev,
297 "%s: failed to read STATUS register: %d\n", __func__,
298 ret);
299
300 return ret;
301}
302
303static int tsl2772_write_control_reg(struct tsl2772_chip *chip, u8 data)
304{
305 int ret;
306
307 ret = i2c_smbus_write_byte_data(chip->client,
308 TSL2772_CMD_REG | TSL2772_CNTRL, data);
309 if (ret < 0) {
310 dev_err(&chip->client->dev,
311 "%s: failed to write to control register %x: %d\n",
312 __func__, data, ret);
313 }
314
315 return ret;
316}
317
318static int tsl2772_read_autoinc_regs(struct tsl2772_chip *chip, int lower_reg,
319 int upper_reg)
320{
321 u8 buf[2];
322 int ret;
323
324 ret = i2c_smbus_write_byte(chip->client,
325 TSL2772_CMD_REG | TSL2772_CMD_AUTOINC_PROTO |
326 lower_reg);
327 if (ret < 0) {
328 dev_err(&chip->client->dev,
329 "%s: failed to enable auto increment protocol: %d\n",
330 __func__, ret);
331 return ret;
332 }
333
334 ret = i2c_smbus_read_byte_data(chip->client,
335 TSL2772_CMD_REG | lower_reg);
336 if (ret < 0) {
337 dev_err(&chip->client->dev,
338 "%s: failed to read from register %x: %d\n", __func__,
339 lower_reg, ret);
340 return ret;
341 }
342 buf[0] = ret;
343
344 ret = i2c_smbus_read_byte_data(chip->client,
345 TSL2772_CMD_REG | upper_reg);
346 if (ret < 0) {
347 dev_err(&chip->client->dev,
348 "%s: failed to read from register %x: %d\n", __func__,
349 upper_reg, ret);
350 return ret;
351 }
352 buf[1] = ret;
353
354 ret = i2c_smbus_write_byte(chip->client,
355 TSL2772_CMD_REG | TSL2772_CMD_REPEAT_PROTO |
356 lower_reg);
357 if (ret < 0) {
358 dev_err(&chip->client->dev,
359 "%s: failed to enable repeated byte protocol: %d\n",
360 __func__, ret);
361 return ret;
362 }
363
364 return le16_to_cpup((const __le16 *)&buf[0]);
365}
366
367
368
369
370
371
372
373
374
375
376
377
378static int tsl2772_get_lux(struct iio_dev *indio_dev)
379{
380 struct tsl2772_chip *chip = iio_priv(indio_dev);
381 struct tsl2772_lux *p;
382 int max_lux, ret;
383 bool overflow;
384
385 mutex_lock(&chip->als_mutex);
386
387 if (chip->tsl2772_chip_status != TSL2772_CHIP_WORKING) {
388 dev_err(&chip->client->dev, "%s: device is not enabled\n",
389 __func__);
390 ret = -EBUSY;
391 goto out_unlock;
392 }
393
394 ret = tsl2772_read_status(chip);
395 if (ret < 0)
396 goto out_unlock;
397
398 if (!(ret & TSL2772_STA_ADC_VALID)) {
399 dev_err(&chip->client->dev,
400 "%s: data not valid yet\n", __func__);
401 ret = chip->als_cur_info.lux;
402 goto out_unlock;
403 }
404
405 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN0LO,
406 TSL2772_ALS_CHAN0HI);
407 if (ret < 0)
408 goto out_unlock;
409 chip->als_cur_info.als_ch0 = ret;
410
411 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN1LO,
412 TSL2772_ALS_CHAN1HI);
413 if (ret < 0)
414 goto out_unlock;
415 chip->als_cur_info.als_ch1 = ret;
416
417 if (chip->als_cur_info.als_ch0 >= chip->als_saturation) {
418 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
419 goto update_struct_with_max_lux;
420 }
421
422 if (!chip->als_cur_info.als_ch0) {
423
424 ret = chip->als_cur_info.lux;
425 goto out_unlock;
426 }
427
428 max_lux = 0;
429 overflow = false;
430 for (p = (struct tsl2772_lux *)chip->tsl2772_device_lux; p->ch0 != 0;
431 p++) {
432 int lux;
433
434 lux = ((chip->als_cur_info.als_ch0 * p->ch0) -
435 (chip->als_cur_info.als_ch1 * p->ch1)) /
436 chip->als_gain_time_scale;
437
438
439
440
441
442
443
444 lux = (lux * chip->settings.als_gain_trim) / 1000;
445
446 if (lux > TSL2772_LUX_CALC_OVER_FLOW) {
447 overflow = true;
448 continue;
449 }
450
451 max_lux = max(max_lux, lux);
452 }
453
454 if (overflow && max_lux == 0)
455 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
456
457update_struct_with_max_lux:
458 chip->als_cur_info.lux = max_lux;
459 ret = max_lux;
460
461out_unlock:
462 mutex_unlock(&chip->als_mutex);
463
464 return ret;
465}
466
467
468
469
470
471
472
473static int tsl2772_get_prox(struct iio_dev *indio_dev)
474{
475 struct tsl2772_chip *chip = iio_priv(indio_dev);
476 int ret;
477
478 mutex_lock(&chip->prox_mutex);
479
480 ret = tsl2772_read_status(chip);
481 if (ret < 0)
482 goto prox_poll_err;
483
484 switch (chip->id) {
485 case tsl2571:
486 case tsl2671:
487 case tmd2671:
488 case tsl2771:
489 case tmd2771:
490 if (!(ret & TSL2772_STA_ADC_VALID)) {
491 ret = -EINVAL;
492 goto prox_poll_err;
493 }
494 break;
495 case tsl2572:
496 case tsl2672:
497 case tmd2672:
498 case tsl2772:
499 case tmd2772:
500 if (!(ret & TSL2772_STA_PRX_VALID)) {
501 ret = -EINVAL;
502 goto prox_poll_err;
503 }
504 break;
505 }
506
507 ret = tsl2772_read_autoinc_regs(chip, TSL2772_PRX_LO, TSL2772_PRX_HI);
508 if (ret < 0)
509 goto prox_poll_err;
510 chip->prox_data = ret;
511
512prox_poll_err:
513 mutex_unlock(&chip->prox_mutex);
514
515 return ret;
516}
517
518
519
520
521
522
523
524
525static void tsl2772_defaults(struct tsl2772_chip *chip)
526{
527
528 if (chip->pdata && chip->pdata->platform_default_settings)
529 memcpy(&chip->settings, chip->pdata->platform_default_settings,
530 sizeof(tsl2772_default_settings));
531 else
532 memcpy(&chip->settings, &tsl2772_default_settings,
533 sizeof(tsl2772_default_settings));
534
535
536 if (chip->pdata && chip->pdata->platform_lux_table[0].ch0 != 0)
537 memcpy(chip->tsl2772_device_lux,
538 chip->pdata->platform_lux_table,
539 sizeof(chip->pdata->platform_lux_table));
540 else
541 memcpy(chip->tsl2772_device_lux,
542 tsl2772_default_lux_table_group[chip->id],
543 TSL2772_DEFAULT_TABLE_BYTES);
544}
545
546
547
548
549
550
551
552static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
553{
554 struct tsl2772_chip *chip = iio_priv(indio_dev);
555 int ret, lux_val;
556
557 ret = i2c_smbus_read_byte_data(chip->client,
558 TSL2772_CMD_REG | TSL2772_CNTRL);
559 if (ret < 0) {
560 dev_err(&chip->client->dev,
561 "%s: failed to read from the CNTRL register\n",
562 __func__);
563 return ret;
564 }
565
566 if ((ret & (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON))
567 != (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON)) {
568 dev_err(&chip->client->dev,
569 "%s: Device is not powered on and/or ADC is not enabled\n",
570 __func__);
571 return -EINVAL;
572 } else if ((ret & TSL2772_STA_ADC_VALID) != TSL2772_STA_ADC_VALID) {
573 dev_err(&chip->client->dev,
574 "%s: The two ADC channels have not completed an integration cycle\n",
575 __func__);
576 return -ENODATA;
577 }
578
579 lux_val = tsl2772_get_lux(indio_dev);
580 if (lux_val < 0) {
581 dev_err(&chip->client->dev,
582 "%s: failed to get lux\n", __func__);
583 return lux_val;
584 }
585 if (lux_val == 0)
586 return -ERANGE;
587
588 ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
589 lux_val;
590 if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX)
591 return -ERANGE;
592
593 chip->settings.als_gain_trim = ret;
594
595 return ret;
596}
597
598static int tsl2772_chip_on(struct iio_dev *indio_dev)
599{
600 struct tsl2772_chip *chip = iio_priv(indio_dev);
601 int ret, i, als_count, als_time_us;
602 u8 *dev_reg, reg_val;
603
604
605 chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
606 chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
607 chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
608 chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
609 chip->settings.als_prox_config;
610
611 chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
612 (chip->settings.als_thresh_low) & 0xFF;
613 chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
614 (chip->settings.als_thresh_low >> 8) & 0xFF;
615 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
616 (chip->settings.als_thresh_high) & 0xFF;
617 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
618 (chip->settings.als_thresh_high >> 8) & 0xFF;
619 chip->tsl2772_config[TSL2772_PERSISTENCE] =
620 (chip->settings.prox_persistence & 0xFF) << 4 |
621 (chip->settings.als_persistence & 0xFF);
622
623 chip->tsl2772_config[TSL2772_PRX_COUNT] =
624 chip->settings.prox_pulse_count;
625 chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
626 (chip->settings.prox_thres_low) & 0xFF;
627 chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
628 (chip->settings.prox_thres_low >> 8) & 0xFF;
629 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
630 (chip->settings.prox_thres_high) & 0xFF;
631 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
632 (chip->settings.prox_thres_high >> 8) & 0xFF;
633
634
635 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
636
637 dev_info(&chip->client->dev, "device is already enabled\n");
638 return -EINVAL;
639 }
640
641
642 chip->tsl2772_config[TSL2772_GAIN] =
643 (chip->settings.als_gain & 0xFF) |
644 ((chip->settings.prox_gain & 0xFF) << 2) |
645 (chip->settings.prox_diode << 4) |
646 (chip->settings.prox_power << 6);
647
648
649 als_count = 256 - chip->settings.als_time;
650 als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
651 chip->als_saturation = als_count * 768;
652 chip->als_gain_time_scale = als_time_us *
653 tsl2772_als_gain[chip->settings.als_gain];
654
655
656
657
658
659 ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
660 if (ret < 0)
661 return ret;
662
663
664
665
666
667 for (i = 0, dev_reg = chip->tsl2772_config;
668 i < TSL2772_MAX_CONFIG_REG; i++) {
669 int reg = TSL2772_CMD_REG + i;
670
671 ret = i2c_smbus_write_byte_data(chip->client, reg,
672 *dev_reg++);
673 if (ret < 0) {
674 dev_err(&chip->client->dev,
675 "%s: failed to write to register %x: %d\n",
676 __func__, reg, ret);
677 return ret;
678 }
679 }
680
681
682 usleep_range(3000, 3500);
683
684 reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
685 TSL2772_CNTL_PROX_DET_ENBL;
686 if (chip->settings.als_interrupt_en)
687 reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
688 if (chip->settings.prox_interrupt_en)
689 reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
690
691 ret = tsl2772_write_control_reg(chip, reg_val);
692 if (ret < 0)
693 return ret;
694
695 ret = i2c_smbus_write_byte(chip->client,
696 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
697 TSL2772_CMD_PROXALS_INT_CLR);
698 if (ret < 0) {
699 dev_err(&chip->client->dev,
700 "%s: failed to clear interrupt status: %d\n",
701 __func__, ret);
702 return ret;
703 }
704
705 chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
706
707 return ret;
708}
709
710static int tsl2772_chip_off(struct iio_dev *indio_dev)
711{
712 struct tsl2772_chip *chip = iio_priv(indio_dev);
713
714
715 chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
716 return tsl2772_write_control_reg(chip, 0x00);
717}
718
719
720
721
722
723
724
725
726
727
728static int tsl2772_invoke_change(struct iio_dev *indio_dev)
729{
730 struct tsl2772_chip *chip = iio_priv(indio_dev);
731 int device_status = chip->tsl2772_chip_status;
732 int ret;
733
734 mutex_lock(&chip->als_mutex);
735 mutex_lock(&chip->prox_mutex);
736
737 if (device_status == TSL2772_CHIP_WORKING) {
738 ret = tsl2772_chip_off(indio_dev);
739 if (ret < 0)
740 goto unlock;
741 }
742
743 ret = tsl2772_chip_on(indio_dev);
744
745unlock:
746 mutex_unlock(&chip->prox_mutex);
747 mutex_unlock(&chip->als_mutex);
748
749 return ret;
750}
751
752static int tsl2772_prox_cal(struct iio_dev *indio_dev)
753{
754 struct tsl2772_chip *chip = iio_priv(indio_dev);
755 int prox_history[MAX_SAMPLES_CAL + 1];
756 int i, ret, mean, max, sample_sum;
757
758 if (chip->settings.prox_max_samples_cal < 1 ||
759 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
760 return -EINVAL;
761
762 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
763 usleep_range(15000, 17500);
764 ret = tsl2772_get_prox(indio_dev);
765 if (ret < 0)
766 return ret;
767
768 prox_history[i] = chip->prox_data;
769 }
770
771 sample_sum = 0;
772 max = INT_MIN;
773 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
774 sample_sum += prox_history[i];
775 max = max(max, prox_history[i]);
776 }
777 mean = sample_sum / chip->settings.prox_max_samples_cal;
778
779 chip->settings.prox_thres_high = (max << 1) - mean;
780
781 return tsl2772_invoke_change(indio_dev);
782}
783
784static int tsl2772_read_avail(struct iio_dev *indio_dev,
785 struct iio_chan_spec const *chan,
786 const int **vals, int *type, int *length,
787 long mask)
788{
789 struct tsl2772_chip *chip = iio_priv(indio_dev);
790
791 switch (mask) {
792 case IIO_CHAN_INFO_CALIBSCALE:
793 if (chan->type == IIO_INTENSITY) {
794 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
795 *vals = tsl2772_int_calibscale_avail;
796 } else {
797 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
798 *vals = tsl2772_prox_calibscale_avail;
799 }
800 *type = IIO_VAL_INT;
801 return IIO_AVAIL_LIST;
802 case IIO_CHAN_INFO_INT_TIME:
803 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
804 *vals = tsl2772_int_time_avail[chip->id];
805 *type = IIO_VAL_INT_PLUS_MICRO;
806 return IIO_AVAIL_RANGE;
807 }
808
809 return -EINVAL;
810}
811
812static ssize_t in_illuminance0_target_input_show(struct device *dev,
813 struct device_attribute *attr,
814 char *buf)
815{
816 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
817
818 return snprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
819}
820
821static ssize_t in_illuminance0_target_input_store(struct device *dev,
822 struct device_attribute *attr,
823 const char *buf, size_t len)
824{
825 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
826 struct tsl2772_chip *chip = iio_priv(indio_dev);
827 u16 value;
828 int ret;
829
830 if (kstrtou16(buf, 0, &value))
831 return -EINVAL;
832
833 chip->settings.als_cal_target = value;
834 ret = tsl2772_invoke_change(indio_dev);
835 if (ret < 0)
836 return ret;
837
838 return len;
839}
840
841static ssize_t in_illuminance0_calibrate_store(struct device *dev,
842 struct device_attribute *attr,
843 const char *buf, size_t len)
844{
845 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
846 bool value;
847 int ret;
848
849 if (kstrtobool(buf, &value) || !value)
850 return -EINVAL;
851
852 ret = tsl2772_als_calibrate(indio_dev);
853 if (ret < 0)
854 return ret;
855
856 ret = tsl2772_invoke_change(indio_dev);
857 if (ret < 0)
858 return ret;
859
860 return len;
861}
862
863static ssize_t in_illuminance0_lux_table_show(struct device *dev,
864 struct device_attribute *attr,
865 char *buf)
866{
867 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
868 int i = 0;
869 int offset = 0;
870
871 while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
872 offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,",
873 chip->tsl2772_device_lux[i].ch0,
874 chip->tsl2772_device_lux[i].ch1);
875 if (chip->tsl2772_device_lux[i].ch0 == 0) {
876
877
878
879
880 offset--;
881 break;
882 }
883 i++;
884 }
885
886 offset += snprintf(buf + offset, PAGE_SIZE, "\n");
887 return offset;
888}
889
890static ssize_t in_illuminance0_lux_table_store(struct device *dev,
891 struct device_attribute *attr,
892 const char *buf, size_t len)
893{
894 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
895 struct tsl2772_chip *chip = iio_priv(indio_dev);
896 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
897 int n, ret;
898
899 get_options(buf, ARRAY_SIZE(value), value);
900
901
902
903
904
905
906
907 n = value[0];
908 if ((n % 2) || n < 4 ||
909 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
910 return -EINVAL;
911
912 if ((value[(n - 1)] | value[n]) != 0)
913 return -EINVAL;
914
915 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
916 ret = tsl2772_chip_off(indio_dev);
917 if (ret < 0)
918 return ret;
919 }
920
921
922 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
923 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
924
925 ret = tsl2772_invoke_change(indio_dev);
926 if (ret < 0)
927 return ret;
928
929 return len;
930}
931
932static ssize_t in_proximity0_calibrate_store(struct device *dev,
933 struct device_attribute *attr,
934 const char *buf, size_t len)
935{
936 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
937 bool value;
938 int ret;
939
940 if (kstrtobool(buf, &value) || !value)
941 return -EINVAL;
942
943 ret = tsl2772_prox_cal(indio_dev);
944 if (ret < 0)
945 return ret;
946
947 ret = tsl2772_invoke_change(indio_dev);
948 if (ret < 0)
949 return ret;
950
951 return len;
952}
953
954static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
955 const struct iio_chan_spec *chan,
956 enum iio_event_type type,
957 enum iio_event_direction dir)
958{
959 struct tsl2772_chip *chip = iio_priv(indio_dev);
960
961 if (chan->type == IIO_INTENSITY)
962 return chip->settings.als_interrupt_en;
963 else
964 return chip->settings.prox_interrupt_en;
965}
966
967static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
968 const struct iio_chan_spec *chan,
969 enum iio_event_type type,
970 enum iio_event_direction dir,
971 int val)
972{
973 struct tsl2772_chip *chip = iio_priv(indio_dev);
974
975 if (chan->type == IIO_INTENSITY)
976 chip->settings.als_interrupt_en = val ? true : false;
977 else
978 chip->settings.prox_interrupt_en = val ? true : false;
979
980 return tsl2772_invoke_change(indio_dev);
981}
982
983static int tsl2772_write_event_value(struct iio_dev *indio_dev,
984 const struct iio_chan_spec *chan,
985 enum iio_event_type type,
986 enum iio_event_direction dir,
987 enum iio_event_info info,
988 int val, int val2)
989{
990 struct tsl2772_chip *chip = iio_priv(indio_dev);
991 int ret = -EINVAL, count, persistence;
992 u8 time;
993
994 switch (info) {
995 case IIO_EV_INFO_VALUE:
996 if (chan->type == IIO_INTENSITY) {
997 switch (dir) {
998 case IIO_EV_DIR_RISING:
999 chip->settings.als_thresh_high = val;
1000 ret = 0;
1001 break;
1002 case IIO_EV_DIR_FALLING:
1003 chip->settings.als_thresh_low = val;
1004 ret = 0;
1005 break;
1006 default:
1007 break;
1008 }
1009 } else {
1010 switch (dir) {
1011 case IIO_EV_DIR_RISING:
1012 chip->settings.prox_thres_high = val;
1013 ret = 0;
1014 break;
1015 case IIO_EV_DIR_FALLING:
1016 chip->settings.prox_thres_low = val;
1017 ret = 0;
1018 break;
1019 default:
1020 break;
1021 }
1022 }
1023 break;
1024 case IIO_EV_INFO_PERIOD:
1025 if (chan->type == IIO_INTENSITY)
1026 time = chip->settings.als_time;
1027 else
1028 time = chip->settings.prox_time;
1029
1030 count = 256 - time;
1031 persistence = ((val * 1000000) + val2) /
1032 (count * tsl2772_int_time_avail[chip->id][3]);
1033
1034 if (chan->type == IIO_INTENSITY) {
1035
1036 if (persistence > 3)
1037 persistence = (persistence / 5) + 3;
1038
1039 chip->settings.als_persistence = persistence;
1040 } else {
1041 chip->settings.prox_persistence = persistence;
1042 }
1043
1044 ret = 0;
1045 break;
1046 default:
1047 break;
1048 }
1049
1050 if (ret < 0)
1051 return ret;
1052
1053 return tsl2772_invoke_change(indio_dev);
1054}
1055
1056static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1057 const struct iio_chan_spec *chan,
1058 enum iio_event_type type,
1059 enum iio_event_direction dir,
1060 enum iio_event_info info,
1061 int *val, int *val2)
1062{
1063 struct tsl2772_chip *chip = iio_priv(indio_dev);
1064 int filter_delay, persistence;
1065 u8 time;
1066
1067 switch (info) {
1068 case IIO_EV_INFO_VALUE:
1069 if (chan->type == IIO_INTENSITY) {
1070 switch (dir) {
1071 case IIO_EV_DIR_RISING:
1072 *val = chip->settings.als_thresh_high;
1073 return IIO_VAL_INT;
1074 case IIO_EV_DIR_FALLING:
1075 *val = chip->settings.als_thresh_low;
1076 return IIO_VAL_INT;
1077 default:
1078 return -EINVAL;
1079 }
1080 } else {
1081 switch (dir) {
1082 case IIO_EV_DIR_RISING:
1083 *val = chip->settings.prox_thres_high;
1084 return IIO_VAL_INT;
1085 case IIO_EV_DIR_FALLING:
1086 *val = chip->settings.prox_thres_low;
1087 return IIO_VAL_INT;
1088 default:
1089 return -EINVAL;
1090 }
1091 }
1092 break;
1093 case IIO_EV_INFO_PERIOD:
1094 if (chan->type == IIO_INTENSITY) {
1095 time = chip->settings.als_time;
1096 persistence = chip->settings.als_persistence;
1097
1098
1099 if (persistence > 3)
1100 persistence = (persistence - 3) * 5;
1101 } else {
1102 time = chip->settings.prox_time;
1103 persistence = chip->settings.prox_persistence;
1104 }
1105
1106 filter_delay = persistence * (256 - time) *
1107 tsl2772_int_time_avail[chip->id][3];
1108
1109 *val = filter_delay / 1000000;
1110 *val2 = filter_delay % 1000000;
1111 return IIO_VAL_INT_PLUS_MICRO;
1112 default:
1113 return -EINVAL;
1114 }
1115}
1116
1117static int tsl2772_read_raw(struct iio_dev *indio_dev,
1118 struct iio_chan_spec const *chan,
1119 int *val,
1120 int *val2,
1121 long mask)
1122{
1123 struct tsl2772_chip *chip = iio_priv(indio_dev);
1124
1125 switch (mask) {
1126 case IIO_CHAN_INFO_PROCESSED:
1127 switch (chan->type) {
1128 case IIO_LIGHT:
1129 tsl2772_get_lux(indio_dev);
1130 *val = chip->als_cur_info.lux;
1131 return IIO_VAL_INT;
1132 default:
1133 return -EINVAL;
1134 }
1135 case IIO_CHAN_INFO_RAW:
1136 switch (chan->type) {
1137 case IIO_INTENSITY:
1138 tsl2772_get_lux(indio_dev);
1139 if (chan->channel == 0)
1140 *val = chip->als_cur_info.als_ch0;
1141 else
1142 *val = chip->als_cur_info.als_ch1;
1143 return IIO_VAL_INT;
1144 case IIO_PROXIMITY:
1145 tsl2772_get_prox(indio_dev);
1146 *val = chip->prox_data;
1147 return IIO_VAL_INT;
1148 default:
1149 return -EINVAL;
1150 }
1151 break;
1152 case IIO_CHAN_INFO_CALIBSCALE:
1153 if (chan->type == IIO_LIGHT)
1154 *val = tsl2772_als_gain[chip->settings.als_gain];
1155 else
1156 *val = tsl2772_prox_gain[chip->settings.prox_gain];
1157 return IIO_VAL_INT;
1158 case IIO_CHAN_INFO_CALIBBIAS:
1159 *val = chip->settings.als_gain_trim;
1160 return IIO_VAL_INT;
1161 case IIO_CHAN_INFO_INT_TIME:
1162 *val = 0;
1163 *val2 = (256 - chip->settings.als_time) *
1164 tsl2772_int_time_avail[chip->id][3];
1165 return IIO_VAL_INT_PLUS_MICRO;
1166 default:
1167 return -EINVAL;
1168 }
1169}
1170
1171static int tsl2772_write_raw(struct iio_dev *indio_dev,
1172 struct iio_chan_spec const *chan,
1173 int val,
1174 int val2,
1175 long mask)
1176{
1177 struct tsl2772_chip *chip = iio_priv(indio_dev);
1178
1179 switch (mask) {
1180 case IIO_CHAN_INFO_CALIBSCALE:
1181 if (chan->type == IIO_INTENSITY) {
1182 switch (val) {
1183 case 1:
1184 chip->settings.als_gain = 0;
1185 break;
1186 case 8:
1187 chip->settings.als_gain = 1;
1188 break;
1189 case 16:
1190 chip->settings.als_gain = 2;
1191 break;
1192 case 120:
1193 chip->settings.als_gain = 3;
1194 break;
1195 default:
1196 return -EINVAL;
1197 }
1198 } else {
1199 switch (val) {
1200 case 1:
1201 chip->settings.prox_gain = 0;
1202 break;
1203 case 2:
1204 chip->settings.prox_gain = 1;
1205 break;
1206 case 4:
1207 chip->settings.prox_gain = 2;
1208 break;
1209 case 8:
1210 chip->settings.prox_gain = 3;
1211 break;
1212 default:
1213 return -EINVAL;
1214 }
1215 }
1216 break;
1217 case IIO_CHAN_INFO_CALIBBIAS:
1218 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1219 val > TSL2772_ALS_GAIN_TRIM_MAX)
1220 return -EINVAL;
1221
1222 chip->settings.als_gain_trim = val;
1223 break;
1224 case IIO_CHAN_INFO_INT_TIME:
1225 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1226 val2 > tsl2772_int_time_avail[chip->id][5])
1227 return -EINVAL;
1228
1229 chip->settings.als_time = 256 -
1230 (val2 / tsl2772_int_time_avail[chip->id][3]);
1231 break;
1232 default:
1233 return -EINVAL;
1234 }
1235
1236 return tsl2772_invoke_change(indio_dev);
1237}
1238
1239static DEVICE_ATTR_RW(in_illuminance0_target_input);
1240
1241static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1242
1243static DEVICE_ATTR_WO(in_proximity0_calibrate);
1244
1245static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1246
1247
1248static int tsl2772_device_id_verif(int id, int target)
1249{
1250 switch (target) {
1251 case tsl2571:
1252 case tsl2671:
1253 case tsl2771:
1254 return (id & 0xf0) == TRITON_ID;
1255 case tmd2671:
1256 case tmd2771:
1257 return (id & 0xf0) == HALIBUT_ID;
1258 case tsl2572:
1259 case tsl2672:
1260 case tmd2672:
1261 case tsl2772:
1262 case tmd2772:
1263 return (id & 0xf0) == SWORDFISH_ID;
1264 }
1265
1266 return -EINVAL;
1267}
1268
1269static irqreturn_t tsl2772_event_handler(int irq, void *private)
1270{
1271 struct iio_dev *indio_dev = private;
1272 struct tsl2772_chip *chip = iio_priv(indio_dev);
1273 s64 timestamp = iio_get_time_ns(indio_dev);
1274 int ret;
1275
1276 ret = tsl2772_read_status(chip);
1277 if (ret < 0)
1278 return IRQ_HANDLED;
1279
1280
1281 if (ret & TSL2772_STA_PRX_INTR) {
1282 iio_push_event(indio_dev,
1283 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1284 0,
1285 IIO_EV_TYPE_THRESH,
1286 IIO_EV_DIR_EITHER),
1287 timestamp);
1288 }
1289
1290 if (ret & TSL2772_STA_ALS_INTR) {
1291 iio_push_event(indio_dev,
1292 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1293 0,
1294 IIO_EV_TYPE_THRESH,
1295 IIO_EV_DIR_EITHER),
1296 timestamp);
1297 }
1298
1299 ret = i2c_smbus_write_byte(chip->client,
1300 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1301 TSL2772_CMD_PROXALS_INT_CLR);
1302 if (ret < 0)
1303 dev_err(&chip->client->dev,
1304 "%s: failed to clear interrupt status: %d\n",
1305 __func__, ret);
1306
1307 return IRQ_HANDLED;
1308}
1309
1310static struct attribute *tsl2772_ALS_device_attrs[] = {
1311 &dev_attr_in_illuminance0_target_input.attr,
1312 &dev_attr_in_illuminance0_calibrate.attr,
1313 &dev_attr_in_illuminance0_lux_table.attr,
1314 NULL
1315};
1316
1317static struct attribute *tsl2772_PRX_device_attrs[] = {
1318 &dev_attr_in_proximity0_calibrate.attr,
1319 NULL
1320};
1321
1322static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1323 &dev_attr_in_illuminance0_target_input.attr,
1324 &dev_attr_in_illuminance0_calibrate.attr,
1325 &dev_attr_in_illuminance0_lux_table.attr,
1326 NULL
1327};
1328
1329static struct attribute *tsl2772_PRX2_device_attrs[] = {
1330 &dev_attr_in_proximity0_calibrate.attr,
1331 NULL
1332};
1333
1334static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1335 &dev_attr_in_illuminance0_target_input.attr,
1336 &dev_attr_in_illuminance0_calibrate.attr,
1337 &dev_attr_in_illuminance0_lux_table.attr,
1338 &dev_attr_in_proximity0_calibrate.attr,
1339 NULL
1340};
1341
1342static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1343 [ALS] = {
1344 .attrs = tsl2772_ALS_device_attrs,
1345 },
1346 [PRX] = {
1347 .attrs = tsl2772_PRX_device_attrs,
1348 },
1349 [ALSPRX] = {
1350 .attrs = tsl2772_ALSPRX_device_attrs,
1351 },
1352 [PRX2] = {
1353 .attrs = tsl2772_PRX2_device_attrs,
1354 },
1355 [ALSPRX2] = {
1356 .attrs = tsl2772_ALSPRX2_device_attrs,
1357 },
1358};
1359
1360#define TSL2772_DEVICE_INFO(type)[type] = \
1361 { \
1362 .attrs = &tsl2772_device_attr_group_tbl[type], \
1363 .read_raw = &tsl2772_read_raw, \
1364 .read_avail = &tsl2772_read_avail, \
1365 .write_raw = &tsl2772_write_raw, \
1366 .read_event_value = &tsl2772_read_event_value, \
1367 .write_event_value = &tsl2772_write_event_value, \
1368 .read_event_config = &tsl2772_read_interrupt_config, \
1369 .write_event_config = &tsl2772_write_interrupt_config, \
1370 }
1371
1372static const struct iio_info tsl2772_device_info[] = {
1373 TSL2772_DEVICE_INFO(ALS),
1374 TSL2772_DEVICE_INFO(PRX),
1375 TSL2772_DEVICE_INFO(ALSPRX),
1376 TSL2772_DEVICE_INFO(PRX2),
1377 TSL2772_DEVICE_INFO(ALSPRX2),
1378};
1379
1380static const struct iio_event_spec tsl2772_events[] = {
1381 {
1382 .type = IIO_EV_TYPE_THRESH,
1383 .dir = IIO_EV_DIR_RISING,
1384 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1385 }, {
1386 .type = IIO_EV_TYPE_THRESH,
1387 .dir = IIO_EV_DIR_FALLING,
1388 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1389 }, {
1390 .type = IIO_EV_TYPE_THRESH,
1391 .dir = IIO_EV_DIR_EITHER,
1392 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1393 BIT(IIO_EV_INFO_ENABLE),
1394 },
1395};
1396
1397static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1398 [ALS] = {
1399 .channel_with_events = {
1400 {
1401 .type = IIO_LIGHT,
1402 .indexed = 1,
1403 .channel = 0,
1404 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1405 }, {
1406 .type = IIO_INTENSITY,
1407 .indexed = 1,
1408 .channel = 0,
1409 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1410 BIT(IIO_CHAN_INFO_INT_TIME) |
1411 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1412 BIT(IIO_CHAN_INFO_CALIBBIAS),
1413 .info_mask_separate_available =
1414 BIT(IIO_CHAN_INFO_INT_TIME) |
1415 BIT(IIO_CHAN_INFO_CALIBSCALE),
1416 .event_spec = tsl2772_events,
1417 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1418 }, {
1419 .type = IIO_INTENSITY,
1420 .indexed = 1,
1421 .channel = 1,
1422 },
1423 },
1424 .channel_without_events = {
1425 {
1426 .type = IIO_LIGHT,
1427 .indexed = 1,
1428 .channel = 0,
1429 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1430 }, {
1431 .type = IIO_INTENSITY,
1432 .indexed = 1,
1433 .channel = 0,
1434 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1435 BIT(IIO_CHAN_INFO_INT_TIME) |
1436 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1437 BIT(IIO_CHAN_INFO_CALIBBIAS),
1438 .info_mask_separate_available =
1439 BIT(IIO_CHAN_INFO_INT_TIME) |
1440 BIT(IIO_CHAN_INFO_CALIBSCALE),
1441 }, {
1442 .type = IIO_INTENSITY,
1443 .indexed = 1,
1444 .channel = 1,
1445 },
1446 },
1447 .chan_table_elements = 3,
1448 .info = &tsl2772_device_info[ALS],
1449 },
1450 [PRX] = {
1451 .channel_with_events = {
1452 {
1453 .type = IIO_PROXIMITY,
1454 .indexed = 1,
1455 .channel = 0,
1456 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1457 .event_spec = tsl2772_events,
1458 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1459 },
1460 },
1461 .channel_without_events = {
1462 {
1463 .type = IIO_PROXIMITY,
1464 .indexed = 1,
1465 .channel = 0,
1466 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1467 },
1468 },
1469 .chan_table_elements = 1,
1470 .info = &tsl2772_device_info[PRX],
1471 },
1472 [ALSPRX] = {
1473 .channel_with_events = {
1474 {
1475 .type = IIO_LIGHT,
1476 .indexed = 1,
1477 .channel = 0,
1478 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1479 }, {
1480 .type = IIO_INTENSITY,
1481 .indexed = 1,
1482 .channel = 0,
1483 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1484 BIT(IIO_CHAN_INFO_INT_TIME) |
1485 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1486 BIT(IIO_CHAN_INFO_CALIBBIAS),
1487 .info_mask_separate_available =
1488 BIT(IIO_CHAN_INFO_INT_TIME) |
1489 BIT(IIO_CHAN_INFO_CALIBSCALE),
1490 .event_spec = tsl2772_events,
1491 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1492 }, {
1493 .type = IIO_INTENSITY,
1494 .indexed = 1,
1495 .channel = 1,
1496 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1497 }, {
1498 .type = IIO_PROXIMITY,
1499 .indexed = 1,
1500 .channel = 0,
1501 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1502 .event_spec = tsl2772_events,
1503 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1504 },
1505 },
1506 .channel_without_events = {
1507 {
1508 .type = IIO_LIGHT,
1509 .indexed = 1,
1510 .channel = 0,
1511 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1512 }, {
1513 .type = IIO_INTENSITY,
1514 .indexed = 1,
1515 .channel = 0,
1516 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1517 BIT(IIO_CHAN_INFO_INT_TIME) |
1518 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1519 BIT(IIO_CHAN_INFO_CALIBBIAS),
1520 .info_mask_separate_available =
1521 BIT(IIO_CHAN_INFO_INT_TIME) |
1522 BIT(IIO_CHAN_INFO_CALIBSCALE),
1523 }, {
1524 .type = IIO_INTENSITY,
1525 .indexed = 1,
1526 .channel = 1,
1527 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1528 }, {
1529 .type = IIO_PROXIMITY,
1530 .indexed = 1,
1531 .channel = 0,
1532 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1533 },
1534 },
1535 .chan_table_elements = 4,
1536 .info = &tsl2772_device_info[ALSPRX],
1537 },
1538 [PRX2] = {
1539 .channel_with_events = {
1540 {
1541 .type = IIO_PROXIMITY,
1542 .indexed = 1,
1543 .channel = 0,
1544 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1545 BIT(IIO_CHAN_INFO_CALIBSCALE),
1546 .info_mask_separate_available =
1547 BIT(IIO_CHAN_INFO_CALIBSCALE),
1548 .event_spec = tsl2772_events,
1549 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1550 },
1551 },
1552 .channel_without_events = {
1553 {
1554 .type = IIO_PROXIMITY,
1555 .indexed = 1,
1556 .channel = 0,
1557 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1558 BIT(IIO_CHAN_INFO_CALIBSCALE),
1559 .info_mask_separate_available =
1560 BIT(IIO_CHAN_INFO_CALIBSCALE),
1561 },
1562 },
1563 .chan_table_elements = 1,
1564 .info = &tsl2772_device_info[PRX2],
1565 },
1566 [ALSPRX2] = {
1567 .channel_with_events = {
1568 {
1569 .type = IIO_LIGHT,
1570 .indexed = 1,
1571 .channel = 0,
1572 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1573 }, {
1574 .type = IIO_INTENSITY,
1575 .indexed = 1,
1576 .channel = 0,
1577 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1578 BIT(IIO_CHAN_INFO_INT_TIME) |
1579 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1580 BIT(IIO_CHAN_INFO_CALIBBIAS),
1581 .info_mask_separate_available =
1582 BIT(IIO_CHAN_INFO_INT_TIME) |
1583 BIT(IIO_CHAN_INFO_CALIBSCALE),
1584 .event_spec = tsl2772_events,
1585 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1586 }, {
1587 .type = IIO_INTENSITY,
1588 .indexed = 1,
1589 .channel = 1,
1590 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1591 }, {
1592 .type = IIO_PROXIMITY,
1593 .indexed = 1,
1594 .channel = 0,
1595 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1596 BIT(IIO_CHAN_INFO_CALIBSCALE),
1597 .info_mask_separate_available =
1598 BIT(IIO_CHAN_INFO_CALIBSCALE),
1599 .event_spec = tsl2772_events,
1600 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1601 },
1602 },
1603 .channel_without_events = {
1604 {
1605 .type = IIO_LIGHT,
1606 .indexed = 1,
1607 .channel = 0,
1608 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1609 }, {
1610 .type = IIO_INTENSITY,
1611 .indexed = 1,
1612 .channel = 0,
1613 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1614 BIT(IIO_CHAN_INFO_INT_TIME) |
1615 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1616 BIT(IIO_CHAN_INFO_CALIBBIAS),
1617 .info_mask_separate_available =
1618 BIT(IIO_CHAN_INFO_INT_TIME) |
1619 BIT(IIO_CHAN_INFO_CALIBSCALE),
1620 }, {
1621 .type = IIO_INTENSITY,
1622 .indexed = 1,
1623 .channel = 1,
1624 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1625 }, {
1626 .type = IIO_PROXIMITY,
1627 .indexed = 1,
1628 .channel = 0,
1629 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1630 BIT(IIO_CHAN_INFO_CALIBSCALE),
1631 .info_mask_separate_available =
1632 BIT(IIO_CHAN_INFO_CALIBSCALE),
1633 },
1634 },
1635 .chan_table_elements = 4,
1636 .info = &tsl2772_device_info[ALSPRX2],
1637 },
1638};
1639
1640static int tsl2772_probe(struct i2c_client *clientp,
1641 const struct i2c_device_id *id)
1642{
1643 struct iio_dev *indio_dev;
1644 struct tsl2772_chip *chip;
1645 int ret;
1646
1647 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1648 if (!indio_dev)
1649 return -ENOMEM;
1650
1651 chip = iio_priv(indio_dev);
1652 chip->client = clientp;
1653 i2c_set_clientdata(clientp, indio_dev);
1654
1655 ret = i2c_smbus_read_byte_data(chip->client,
1656 TSL2772_CMD_REG | TSL2772_CHIPID);
1657 if (ret < 0)
1658 return ret;
1659
1660 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1661 dev_info(&chip->client->dev,
1662 "%s: i2c device found does not match expected id\n",
1663 __func__);
1664 return -EINVAL;
1665 }
1666
1667 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1668 if (ret < 0) {
1669 dev_err(&clientp->dev,
1670 "%s: Failed to write to CMD register: %d\n",
1671 __func__, ret);
1672 return ret;
1673 }
1674
1675 mutex_init(&chip->als_mutex);
1676 mutex_init(&chip->prox_mutex);
1677
1678 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1679 chip->pdata = dev_get_platdata(&clientp->dev);
1680 chip->id = id->driver_data;
1681 chip->chip_info =
1682 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1683
1684 indio_dev->info = chip->chip_info->info;
1685 indio_dev->dev.parent = &clientp->dev;
1686 indio_dev->modes = INDIO_DIRECT_MODE;
1687 indio_dev->name = chip->client->name;
1688 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1689
1690 if (clientp->irq) {
1691 indio_dev->channels = chip->chip_info->channel_with_events;
1692
1693 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1694 NULL,
1695 &tsl2772_event_handler,
1696 IRQF_TRIGGER_FALLING |
1697 IRQF_ONESHOT,
1698 "TSL2772_event",
1699 indio_dev);
1700 if (ret) {
1701 dev_err(&clientp->dev,
1702 "%s: irq request failed\n", __func__);
1703 return ret;
1704 }
1705 } else {
1706 indio_dev->channels = chip->chip_info->channel_without_events;
1707 }
1708
1709 tsl2772_defaults(chip);
1710 ret = tsl2772_chip_on(indio_dev);
1711 if (ret < 0)
1712 return ret;
1713
1714 ret = iio_device_register(indio_dev);
1715 if (ret) {
1716 tsl2772_chip_off(indio_dev);
1717 dev_err(&clientp->dev,
1718 "%s: iio registration failed\n", __func__);
1719 return ret;
1720 }
1721
1722 return 0;
1723}
1724
1725static int tsl2772_suspend(struct device *dev)
1726{
1727 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1728
1729 return tsl2772_chip_off(indio_dev);
1730}
1731
1732static int tsl2772_resume(struct device *dev)
1733{
1734 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1735
1736 return tsl2772_chip_on(indio_dev);
1737}
1738
1739static int tsl2772_remove(struct i2c_client *client)
1740{
1741 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1742
1743 tsl2772_chip_off(indio_dev);
1744
1745 iio_device_unregister(indio_dev);
1746
1747 return 0;
1748}
1749
1750static const struct i2c_device_id tsl2772_idtable[] = {
1751 { "tsl2571", tsl2571 },
1752 { "tsl2671", tsl2671 },
1753 { "tmd2671", tmd2671 },
1754 { "tsl2771", tsl2771 },
1755 { "tmd2771", tmd2771 },
1756 { "tsl2572", tsl2572 },
1757 { "tsl2672", tsl2672 },
1758 { "tmd2672", tmd2672 },
1759 { "tsl2772", tsl2772 },
1760 { "tmd2772", tmd2772 },
1761 {}
1762};
1763
1764MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1765
1766static const struct of_device_id tsl2772_of_match[] = {
1767 { .compatible = "amstaos,tsl2571" },
1768 { .compatible = "amstaos,tsl2671" },
1769 { .compatible = "amstaos,tmd2671" },
1770 { .compatible = "amstaos,tsl2771" },
1771 { .compatible = "amstaos,tmd2771" },
1772 { .compatible = "amstaos,tsl2572" },
1773 { .compatible = "amstaos,tsl2672" },
1774 { .compatible = "amstaos,tmd2672" },
1775 { .compatible = "amstaos,tsl2772" },
1776 { .compatible = "amstaos,tmd2772" },
1777 {}
1778};
1779MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1780
1781static const struct dev_pm_ops tsl2772_pm_ops = {
1782 .suspend = tsl2772_suspend,
1783 .resume = tsl2772_resume,
1784};
1785
1786static struct i2c_driver tsl2772_driver = {
1787 .driver = {
1788 .name = "tsl2772",
1789 .of_match_table = tsl2772_of_match,
1790 .pm = &tsl2772_pm_ops,
1791 },
1792 .id_table = tsl2772_idtable,
1793 .probe = tsl2772_probe,
1794 .remove = tsl2772_remove,
1795};
1796
1797module_i2c_driver(tsl2772_driver);
1798
1799MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1800MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1801MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1802MODULE_LICENSE("GPL");
1803