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

source code of linux/drivers/iio/light/tsl2772.c