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/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 /* Cal defs */
25 #define PROX_STAT_CAL 0
26 #define PROX_STAT_SAMP 1
27 #define MAX_SAMPLES_CAL 200
28
29 /* TSL2772 Device ID */
30 #define TRITON_ID 0x00
31 #define SWORDFISH_ID 0x30
32 #define HALIBUT_ID 0x20
33
34 /* Lux calculation constants */
35 #define TSL2772_LUX_CALC_OVER_FLOW 65535
36
37 /*
38 * TAOS Register definitions - Note: depending on device, some of these register
39 * are not used and the register address is benign.
40 */
41
42 /* Register offsets */
43 #define TSL2772_MAX_CONFIG_REG 16
44
45 /* Device Registers and Masks */
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 /* tsl2772 cmd reg masks */
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 /* tsl2772 cntrl reg masks */
84 #define TSL2772_CNTL_ADC_ENBL 0x02
85 #define TSL2772_CNTL_PWR_ON 0x01
86
87 /* tsl2772 status reg masks */
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 /* tsl2772 cntrl reg masks */
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 /* Device family members */
111 enum {
112 tsl2571,
113 tsl2671,
114 tmd2671,
115 tsl2771,
116 tmd2771,
117 tsl2572,
118 tsl2672,
119 tmd2672,
120 tsl2772,
121 tmd2772
122 };
123
124 enum {
125 TSL2772_CHIP_UNKNOWN = 0,
126 TSL2772_CHIP_WORKING = 1,
127 TSL2772_CHIP_SUSPENDED = 2
128 };
129
130 /* Per-device data */
131 struct tsl2772_als_info {
132 u16 als_ch0;
133 u16 als_ch1;
134 u16 lux;
135 };
136
137 struct 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
144 struct 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 * This structure is intentionally large to accommodate
162 * updates via sysfs.
163 * Sized to 9 = max 8 segments + 1 termination segment
164 */
165 struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE];
166 };
167
168 /*
169 * Different devices require different coefficents, and these numbers were
170 * derived from the 'Lux Equation' section of the various device datasheets.
171 * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
172 * The coefficients are multiplied by 1000 to avoid floating point operations.
173 * The two rows in each table correspond to the Lux1 and Lux2 equations from
174 * the datasheets.
175 */
176 static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
177 { 53000, 106000 },
178 { 31800, 53000 },
179 { 0, 0 },
180 };
181
182 static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
183 { 24000, 48000 },
184 { 14400, 24000 },
185 { 0, 0 },
186 };
187
188 static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
189 { 60000, 112200 },
190 { 37800, 60000 },
191 { 0, 0 },
192 };
193
194 static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
195 { 20000, 35000 },
196 { 12600, 20000 },
197 { 0, 0 },
198 };
199
200 static 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
213 static const struct tsl2772_settings tsl2772_default_settings = {
214 .als_time = 255, /* 2.72 / 2.73 ms */
215 .als_gain = 0,
216 .prox_time = 255, /* 2.72 / 2.73 ms */
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
236 static const s16 tsl2772_als_gain[] = {
237 1,
238 8,
239 16,
240 120
241 };
242
243 static const s16 tsl2772_prox_gain[] = {
244 1,
245 2,
246 4,
247 8
248 };
249
250 static 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
263 static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 };
264
265 static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 };
266
267 /* Channel variations */
268 enum {
269 ALS,
270 PRX,
271 ALSPRX,
272 PRX2,
273 ALSPRX2,
274 };
275
276 static 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
tsl2772_read_status(struct tsl2772_chip * chip)289 static 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
tsl2772_write_control_reg(struct tsl2772_chip * chip,u8 data)303 static 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
tsl2772_read_autoinc_regs(struct tsl2772_chip * chip,int lower_reg,int upper_reg)318 static 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 * tsl2772_get_lux() - Reads and calculates current lux value.
369 * @indio_dev: pointer to IIO device
370 *
371 * The raw ch0 and ch1 values of the ambient light sensed in the last
372 * integration cycle are read from the device. The raw values are multiplied
373 * by a device-specific scale factor, and divided by the integration time and
374 * device gain. The code supports multiple lux equations through the lux table
375 * coefficients. A lux gain trim is applied to each lux equation, and then the
376 * maximum lux within the interval 0..65535 is selected.
377 */
tsl2772_get_lux(struct iio_dev * indio_dev)378 static 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; /* return LAST VALUE */
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 /* have no data, so return LAST VALUE */
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 * The als_gain_trim can have a value within the range 250..4000
440 * and is a multiplier for the lux. A trim of 1000 makes no
441 * changes to the lux, less than 1000 scales it down, and
442 * greater than 1000 scales it up.
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
457 update_struct_with_max_lux:
458 chip->als_cur_info.lux = max_lux;
459 ret = max_lux;
460
461 out_unlock:
462 mutex_unlock(&chip->als_mutex);
463
464 return ret;
465 }
466
467 /**
468 * tsl2772_get_prox() - Reads proximity data registers and updates
469 * chip->prox_data.
470 *
471 * @indio_dev: pointer to IIO device
472 */
tsl2772_get_prox(struct iio_dev * indio_dev)473 static 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
512 prox_poll_err:
513 mutex_unlock(&chip->prox_mutex);
514
515 return ret;
516 }
517
518 /**
519 * tsl2772_defaults() - Populates the device nominal operating parameters
520 * with those provided by a 'platform' data struct or
521 * with prefined defaults.
522 *
523 * @chip: pointer to device structure.
524 */
tsl2772_defaults(struct tsl2772_chip * chip)525 static void tsl2772_defaults(struct tsl2772_chip *chip)
526 {
527 /* If Operational settings defined elsewhere.. */
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 /* Load up the proper lux table. */
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 * tsl2772_als_calibrate() - Obtain single reading and calculate
548 * the als_gain_trim.
549 *
550 * @indio_dev: pointer to IIO device
551 */
tsl2772_als_calibrate(struct iio_dev * indio_dev)552 static 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
tsl2772_chip_on(struct iio_dev * indio_dev)598 static 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 /* Non calculated parameters */
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 /* and make sure we're not already on */
635 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
636 /* if forcing a register update - turn off, then on */
637 dev_info(&chip->client->dev, "device is already enabled\n");
638 return -EINVAL;
639 }
640
641 /* Set the gain based on tsl2772_settings struct */
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 /* set chip time scaling and saturation */
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; /* 75% of full scale */
652 chip->als_gain_time_scale = als_time_us *
653 tsl2772_als_gain[chip->settings.als_gain];
654
655 /*
656 * TSL2772 Specific power-on / adc enable sequence
657 * Power on the device 1st.
658 */
659 ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
660 if (ret < 0)
661 return ret;
662
663 /*
664 * Use the following shadow copy for our delay before enabling ADC.
665 * Write all the registers.
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 /* Power-on settling time */
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
tsl2772_chip_off(struct iio_dev * indio_dev)710 static int tsl2772_chip_off(struct iio_dev *indio_dev)
711 {
712 struct tsl2772_chip *chip = iio_priv(indio_dev);
713
714 /* turn device off */
715 chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
716 return tsl2772_write_control_reg(chip, 0x00);
717 }
718
tsl2772_chip_off_action(void * data)719 static void tsl2772_chip_off_action(void *data)
720 {
721 struct iio_dev *indio_dev = data;
722
723 tsl2772_chip_off(indio_dev);
724 }
725
726 /**
727 * tsl2772_invoke_change - power cycle the device to implement the user
728 * parameters
729 * @indio_dev: pointer to IIO device
730 *
731 * Obtain and lock both ALS and PROX resources, determine and save device state
732 * (On/Off), cycle device to implement updated parameter, put device back into
733 * proper state, and unlock resource.
734 */
tsl2772_invoke_change(struct iio_dev * indio_dev)735 static int tsl2772_invoke_change(struct iio_dev *indio_dev)
736 {
737 struct tsl2772_chip *chip = iio_priv(indio_dev);
738 int device_status = chip->tsl2772_chip_status;
739 int ret;
740
741 mutex_lock(&chip->als_mutex);
742 mutex_lock(&chip->prox_mutex);
743
744 if (device_status == TSL2772_CHIP_WORKING) {
745 ret = tsl2772_chip_off(indio_dev);
746 if (ret < 0)
747 goto unlock;
748 }
749
750 ret = tsl2772_chip_on(indio_dev);
751
752 unlock:
753 mutex_unlock(&chip->prox_mutex);
754 mutex_unlock(&chip->als_mutex);
755
756 return ret;
757 }
758
tsl2772_prox_cal(struct iio_dev * indio_dev)759 static int tsl2772_prox_cal(struct iio_dev *indio_dev)
760 {
761 struct tsl2772_chip *chip = iio_priv(indio_dev);
762 int prox_history[MAX_SAMPLES_CAL + 1];
763 int i, ret, mean, max, sample_sum;
764
765 if (chip->settings.prox_max_samples_cal < 1 ||
766 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
767 return -EINVAL;
768
769 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
770 usleep_range(15000, 17500);
771 ret = tsl2772_get_prox(indio_dev);
772 if (ret < 0)
773 return ret;
774
775 prox_history[i] = chip->prox_data;
776 }
777
778 sample_sum = 0;
779 max = INT_MIN;
780 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
781 sample_sum += prox_history[i];
782 max = max(max, prox_history[i]);
783 }
784 mean = sample_sum / chip->settings.prox_max_samples_cal;
785
786 chip->settings.prox_thres_high = (max << 1) - mean;
787
788 return tsl2772_invoke_change(indio_dev);
789 }
790
tsl2772_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)791 static int tsl2772_read_avail(struct iio_dev *indio_dev,
792 struct iio_chan_spec const *chan,
793 const int **vals, int *type, int *length,
794 long mask)
795 {
796 struct tsl2772_chip *chip = iio_priv(indio_dev);
797
798 switch (mask) {
799 case IIO_CHAN_INFO_CALIBSCALE:
800 if (chan->type == IIO_INTENSITY) {
801 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
802 *vals = tsl2772_int_calibscale_avail;
803 } else {
804 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
805 *vals = tsl2772_prox_calibscale_avail;
806 }
807 *type = IIO_VAL_INT;
808 return IIO_AVAIL_LIST;
809 case IIO_CHAN_INFO_INT_TIME:
810 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
811 *vals = tsl2772_int_time_avail[chip->id];
812 *type = IIO_VAL_INT_PLUS_MICRO;
813 return IIO_AVAIL_RANGE;
814 }
815
816 return -EINVAL;
817 }
818
in_illuminance0_target_input_show(struct device * dev,struct device_attribute * attr,char * buf)819 static ssize_t in_illuminance0_target_input_show(struct device *dev,
820 struct device_attribute *attr,
821 char *buf)
822 {
823 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
824
825 return snprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
826 }
827
in_illuminance0_target_input_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)828 static ssize_t in_illuminance0_target_input_store(struct device *dev,
829 struct device_attribute *attr,
830 const char *buf, size_t len)
831 {
832 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
833 struct tsl2772_chip *chip = iio_priv(indio_dev);
834 u16 value;
835 int ret;
836
837 if (kstrtou16(buf, 0, &value))
838 return -EINVAL;
839
840 chip->settings.als_cal_target = value;
841 ret = tsl2772_invoke_change(indio_dev);
842 if (ret < 0)
843 return ret;
844
845 return len;
846 }
847
in_illuminance0_calibrate_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)848 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
849 struct device_attribute *attr,
850 const char *buf, size_t len)
851 {
852 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
853 bool value;
854 int ret;
855
856 if (kstrtobool(buf, &value) || !value)
857 return -EINVAL;
858
859 ret = tsl2772_als_calibrate(indio_dev);
860 if (ret < 0)
861 return ret;
862
863 ret = tsl2772_invoke_change(indio_dev);
864 if (ret < 0)
865 return ret;
866
867 return len;
868 }
869
in_illuminance0_lux_table_show(struct device * dev,struct device_attribute * attr,char * buf)870 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
871 struct device_attribute *attr,
872 char *buf)
873 {
874 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
875 int i = 0;
876 int offset = 0;
877
878 while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
879 offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,",
880 chip->tsl2772_device_lux[i].ch0,
881 chip->tsl2772_device_lux[i].ch1);
882 if (chip->tsl2772_device_lux[i].ch0 == 0) {
883 /*
884 * We just printed the first "0" entry.
885 * Now get rid of the extra "," and break.
886 */
887 offset--;
888 break;
889 }
890 i++;
891 }
892
893 offset += snprintf(buf + offset, PAGE_SIZE, "\n");
894 return offset;
895 }
896
in_illuminance0_lux_table_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)897 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
898 struct device_attribute *attr,
899 const char *buf, size_t len)
900 {
901 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
902 struct tsl2772_chip *chip = iio_priv(indio_dev);
903 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
904 int n, ret;
905
906 get_options(buf, ARRAY_SIZE(value), value);
907
908 /*
909 * We now have an array of ints starting at value[1], and
910 * enumerated by value[0].
911 * We expect each group of two ints to be one table entry,
912 * and the last table entry is all 0.
913 */
914 n = value[0];
915 if ((n % 2) || n < 4 ||
916 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
917 return -EINVAL;
918
919 if ((value[(n - 1)] | value[n]) != 0)
920 return -EINVAL;
921
922 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
923 ret = tsl2772_chip_off(indio_dev);
924 if (ret < 0)
925 return ret;
926 }
927
928 /* Zero out the table */
929 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
930 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
931
932 ret = tsl2772_invoke_change(indio_dev);
933 if (ret < 0)
934 return ret;
935
936 return len;
937 }
938
in_proximity0_calibrate_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)939 static ssize_t in_proximity0_calibrate_store(struct device *dev,
940 struct device_attribute *attr,
941 const char *buf, size_t len)
942 {
943 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
944 bool value;
945 int ret;
946
947 if (kstrtobool(buf, &value) || !value)
948 return -EINVAL;
949
950 ret = tsl2772_prox_cal(indio_dev);
951 if (ret < 0)
952 return ret;
953
954 ret = tsl2772_invoke_change(indio_dev);
955 if (ret < 0)
956 return ret;
957
958 return len;
959 }
960
tsl2772_read_interrupt_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)961 static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
962 const struct iio_chan_spec *chan,
963 enum iio_event_type type,
964 enum iio_event_direction dir)
965 {
966 struct tsl2772_chip *chip = iio_priv(indio_dev);
967
968 if (chan->type == IIO_INTENSITY)
969 return chip->settings.als_interrupt_en;
970 else
971 return chip->settings.prox_interrupt_en;
972 }
973
tsl2772_write_interrupt_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int val)974 static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
975 const struct iio_chan_spec *chan,
976 enum iio_event_type type,
977 enum iio_event_direction dir,
978 int val)
979 {
980 struct tsl2772_chip *chip = iio_priv(indio_dev);
981
982 if (chan->type == IIO_INTENSITY)
983 chip->settings.als_interrupt_en = val ? true : false;
984 else
985 chip->settings.prox_interrupt_en = val ? true : false;
986
987 return tsl2772_invoke_change(indio_dev);
988 }
989
tsl2772_write_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)990 static int tsl2772_write_event_value(struct iio_dev *indio_dev,
991 const struct iio_chan_spec *chan,
992 enum iio_event_type type,
993 enum iio_event_direction dir,
994 enum iio_event_info info,
995 int val, int val2)
996 {
997 struct tsl2772_chip *chip = iio_priv(indio_dev);
998 int ret = -EINVAL, count, persistence;
999 u8 time;
1000
1001 switch (info) {
1002 case IIO_EV_INFO_VALUE:
1003 if (chan->type == IIO_INTENSITY) {
1004 switch (dir) {
1005 case IIO_EV_DIR_RISING:
1006 chip->settings.als_thresh_high = val;
1007 ret = 0;
1008 break;
1009 case IIO_EV_DIR_FALLING:
1010 chip->settings.als_thresh_low = val;
1011 ret = 0;
1012 break;
1013 default:
1014 break;
1015 }
1016 } else {
1017 switch (dir) {
1018 case IIO_EV_DIR_RISING:
1019 chip->settings.prox_thres_high = val;
1020 ret = 0;
1021 break;
1022 case IIO_EV_DIR_FALLING:
1023 chip->settings.prox_thres_low = val;
1024 ret = 0;
1025 break;
1026 default:
1027 break;
1028 }
1029 }
1030 break;
1031 case IIO_EV_INFO_PERIOD:
1032 if (chan->type == IIO_INTENSITY)
1033 time = chip->settings.als_time;
1034 else
1035 time = chip->settings.prox_time;
1036
1037 count = 256 - time;
1038 persistence = ((val * 1000000) + val2) /
1039 (count * tsl2772_int_time_avail[chip->id][3]);
1040
1041 if (chan->type == IIO_INTENSITY) {
1042 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1043 if (persistence > 3)
1044 persistence = (persistence / 5) + 3;
1045
1046 chip->settings.als_persistence = persistence;
1047 } else {
1048 chip->settings.prox_persistence = persistence;
1049 }
1050
1051 ret = 0;
1052 break;
1053 default:
1054 break;
1055 }
1056
1057 if (ret < 0)
1058 return ret;
1059
1060 return tsl2772_invoke_change(indio_dev);
1061 }
1062
tsl2772_read_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)1063 static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1064 const struct iio_chan_spec *chan,
1065 enum iio_event_type type,
1066 enum iio_event_direction dir,
1067 enum iio_event_info info,
1068 int *val, int *val2)
1069 {
1070 struct tsl2772_chip *chip = iio_priv(indio_dev);
1071 int filter_delay, persistence;
1072 u8 time;
1073
1074 switch (info) {
1075 case IIO_EV_INFO_VALUE:
1076 if (chan->type == IIO_INTENSITY) {
1077 switch (dir) {
1078 case IIO_EV_DIR_RISING:
1079 *val = chip->settings.als_thresh_high;
1080 return IIO_VAL_INT;
1081 case IIO_EV_DIR_FALLING:
1082 *val = chip->settings.als_thresh_low;
1083 return IIO_VAL_INT;
1084 default:
1085 return -EINVAL;
1086 }
1087 } else {
1088 switch (dir) {
1089 case IIO_EV_DIR_RISING:
1090 *val = chip->settings.prox_thres_high;
1091 return IIO_VAL_INT;
1092 case IIO_EV_DIR_FALLING:
1093 *val = chip->settings.prox_thres_low;
1094 return IIO_VAL_INT;
1095 default:
1096 return -EINVAL;
1097 }
1098 }
1099 break;
1100 case IIO_EV_INFO_PERIOD:
1101 if (chan->type == IIO_INTENSITY) {
1102 time = chip->settings.als_time;
1103 persistence = chip->settings.als_persistence;
1104
1105 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1106 if (persistence > 3)
1107 persistence = (persistence - 3) * 5;
1108 } else {
1109 time = chip->settings.prox_time;
1110 persistence = chip->settings.prox_persistence;
1111 }
1112
1113 filter_delay = persistence * (256 - time) *
1114 tsl2772_int_time_avail[chip->id][3];
1115
1116 *val = filter_delay / 1000000;
1117 *val2 = filter_delay % 1000000;
1118 return IIO_VAL_INT_PLUS_MICRO;
1119 default:
1120 return -EINVAL;
1121 }
1122 }
1123
tsl2772_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)1124 static int tsl2772_read_raw(struct iio_dev *indio_dev,
1125 struct iio_chan_spec const *chan,
1126 int *val,
1127 int *val2,
1128 long mask)
1129 {
1130 struct tsl2772_chip *chip = iio_priv(indio_dev);
1131
1132 switch (mask) {
1133 case IIO_CHAN_INFO_PROCESSED:
1134 switch (chan->type) {
1135 case IIO_LIGHT:
1136 tsl2772_get_lux(indio_dev);
1137 *val = chip->als_cur_info.lux;
1138 return IIO_VAL_INT;
1139 default:
1140 return -EINVAL;
1141 }
1142 case IIO_CHAN_INFO_RAW:
1143 switch (chan->type) {
1144 case IIO_INTENSITY:
1145 tsl2772_get_lux(indio_dev);
1146 if (chan->channel == 0)
1147 *val = chip->als_cur_info.als_ch0;
1148 else
1149 *val = chip->als_cur_info.als_ch1;
1150 return IIO_VAL_INT;
1151 case IIO_PROXIMITY:
1152 tsl2772_get_prox(indio_dev);
1153 *val = chip->prox_data;
1154 return IIO_VAL_INT;
1155 default:
1156 return -EINVAL;
1157 }
1158 break;
1159 case IIO_CHAN_INFO_CALIBSCALE:
1160 if (chan->type == IIO_LIGHT)
1161 *val = tsl2772_als_gain[chip->settings.als_gain];
1162 else
1163 *val = tsl2772_prox_gain[chip->settings.prox_gain];
1164 return IIO_VAL_INT;
1165 case IIO_CHAN_INFO_CALIBBIAS:
1166 *val = chip->settings.als_gain_trim;
1167 return IIO_VAL_INT;
1168 case IIO_CHAN_INFO_INT_TIME:
1169 *val = 0;
1170 *val2 = (256 - chip->settings.als_time) *
1171 tsl2772_int_time_avail[chip->id][3];
1172 return IIO_VAL_INT_PLUS_MICRO;
1173 default:
1174 return -EINVAL;
1175 }
1176 }
1177
tsl2772_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)1178 static int tsl2772_write_raw(struct iio_dev *indio_dev,
1179 struct iio_chan_spec const *chan,
1180 int val,
1181 int val2,
1182 long mask)
1183 {
1184 struct tsl2772_chip *chip = iio_priv(indio_dev);
1185
1186 switch (mask) {
1187 case IIO_CHAN_INFO_CALIBSCALE:
1188 if (chan->type == IIO_INTENSITY) {
1189 switch (val) {
1190 case 1:
1191 chip->settings.als_gain = 0;
1192 break;
1193 case 8:
1194 chip->settings.als_gain = 1;
1195 break;
1196 case 16:
1197 chip->settings.als_gain = 2;
1198 break;
1199 case 120:
1200 chip->settings.als_gain = 3;
1201 break;
1202 default:
1203 return -EINVAL;
1204 }
1205 } else {
1206 switch (val) {
1207 case 1:
1208 chip->settings.prox_gain = 0;
1209 break;
1210 case 2:
1211 chip->settings.prox_gain = 1;
1212 break;
1213 case 4:
1214 chip->settings.prox_gain = 2;
1215 break;
1216 case 8:
1217 chip->settings.prox_gain = 3;
1218 break;
1219 default:
1220 return -EINVAL;
1221 }
1222 }
1223 break;
1224 case IIO_CHAN_INFO_CALIBBIAS:
1225 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1226 val > TSL2772_ALS_GAIN_TRIM_MAX)
1227 return -EINVAL;
1228
1229 chip->settings.als_gain_trim = val;
1230 break;
1231 case IIO_CHAN_INFO_INT_TIME:
1232 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1233 val2 > tsl2772_int_time_avail[chip->id][5])
1234 return -EINVAL;
1235
1236 chip->settings.als_time = 256 -
1237 (val2 / tsl2772_int_time_avail[chip->id][3]);
1238 break;
1239 default:
1240 return -EINVAL;
1241 }
1242
1243 return tsl2772_invoke_change(indio_dev);
1244 }
1245
1246 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1247
1248 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1249
1250 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1251
1252 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1253
1254 /* Use the default register values to identify the Taos device */
tsl2772_device_id_verif(int id,int target)1255 static int tsl2772_device_id_verif(int id, int target)
1256 {
1257 switch (target) {
1258 case tsl2571:
1259 case tsl2671:
1260 case tsl2771:
1261 return (id & 0xf0) == TRITON_ID;
1262 case tmd2671:
1263 case tmd2771:
1264 return (id & 0xf0) == HALIBUT_ID;
1265 case tsl2572:
1266 case tsl2672:
1267 case tmd2672:
1268 case tsl2772:
1269 case tmd2772:
1270 return (id & 0xf0) == SWORDFISH_ID;
1271 }
1272
1273 return -EINVAL;
1274 }
1275
tsl2772_event_handler(int irq,void * private)1276 static irqreturn_t tsl2772_event_handler(int irq, void *private)
1277 {
1278 struct iio_dev *indio_dev = private;
1279 struct tsl2772_chip *chip = iio_priv(indio_dev);
1280 s64 timestamp = iio_get_time_ns(indio_dev);
1281 int ret;
1282
1283 ret = tsl2772_read_status(chip);
1284 if (ret < 0)
1285 return IRQ_HANDLED;
1286
1287 /* What type of interrupt do we need to process */
1288 if (ret & TSL2772_STA_PRX_INTR) {
1289 iio_push_event(indio_dev,
1290 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1291 0,
1292 IIO_EV_TYPE_THRESH,
1293 IIO_EV_DIR_EITHER),
1294 timestamp);
1295 }
1296
1297 if (ret & TSL2772_STA_ALS_INTR) {
1298 iio_push_event(indio_dev,
1299 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1300 0,
1301 IIO_EV_TYPE_THRESH,
1302 IIO_EV_DIR_EITHER),
1303 timestamp);
1304 }
1305
1306 ret = i2c_smbus_write_byte(chip->client,
1307 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1308 TSL2772_CMD_PROXALS_INT_CLR);
1309 if (ret < 0)
1310 dev_err(&chip->client->dev,
1311 "%s: failed to clear interrupt status: %d\n",
1312 __func__, ret);
1313
1314 return IRQ_HANDLED;
1315 }
1316
1317 static struct attribute *tsl2772_ALS_device_attrs[] = {
1318 &dev_attr_in_illuminance0_target_input.attr,
1319 &dev_attr_in_illuminance0_calibrate.attr,
1320 &dev_attr_in_illuminance0_lux_table.attr,
1321 NULL
1322 };
1323
1324 static struct attribute *tsl2772_PRX_device_attrs[] = {
1325 &dev_attr_in_proximity0_calibrate.attr,
1326 NULL
1327 };
1328
1329 static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1330 &dev_attr_in_illuminance0_target_input.attr,
1331 &dev_attr_in_illuminance0_calibrate.attr,
1332 &dev_attr_in_illuminance0_lux_table.attr,
1333 NULL
1334 };
1335
1336 static struct attribute *tsl2772_PRX2_device_attrs[] = {
1337 &dev_attr_in_proximity0_calibrate.attr,
1338 NULL
1339 };
1340
1341 static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1342 &dev_attr_in_illuminance0_target_input.attr,
1343 &dev_attr_in_illuminance0_calibrate.attr,
1344 &dev_attr_in_illuminance0_lux_table.attr,
1345 &dev_attr_in_proximity0_calibrate.attr,
1346 NULL
1347 };
1348
1349 static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1350 [ALS] = {
1351 .attrs = tsl2772_ALS_device_attrs,
1352 },
1353 [PRX] = {
1354 .attrs = tsl2772_PRX_device_attrs,
1355 },
1356 [ALSPRX] = {
1357 .attrs = tsl2772_ALSPRX_device_attrs,
1358 },
1359 [PRX2] = {
1360 .attrs = tsl2772_PRX2_device_attrs,
1361 },
1362 [ALSPRX2] = {
1363 .attrs = tsl2772_ALSPRX2_device_attrs,
1364 },
1365 };
1366
1367 #define TSL2772_DEVICE_INFO(type)[type] = \
1368 { \
1369 .attrs = &tsl2772_device_attr_group_tbl[type], \
1370 .read_raw = &tsl2772_read_raw, \
1371 .read_avail = &tsl2772_read_avail, \
1372 .write_raw = &tsl2772_write_raw, \
1373 .read_event_value = &tsl2772_read_event_value, \
1374 .write_event_value = &tsl2772_write_event_value, \
1375 .read_event_config = &tsl2772_read_interrupt_config, \
1376 .write_event_config = &tsl2772_write_interrupt_config, \
1377 }
1378
1379 static const struct iio_info tsl2772_device_info[] = {
1380 TSL2772_DEVICE_INFO(ALS),
1381 TSL2772_DEVICE_INFO(PRX),
1382 TSL2772_DEVICE_INFO(ALSPRX),
1383 TSL2772_DEVICE_INFO(PRX2),
1384 TSL2772_DEVICE_INFO(ALSPRX2),
1385 };
1386
1387 static const struct iio_event_spec tsl2772_events[] = {
1388 {
1389 .type = IIO_EV_TYPE_THRESH,
1390 .dir = IIO_EV_DIR_RISING,
1391 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1392 }, {
1393 .type = IIO_EV_TYPE_THRESH,
1394 .dir = IIO_EV_DIR_FALLING,
1395 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1396 }, {
1397 .type = IIO_EV_TYPE_THRESH,
1398 .dir = IIO_EV_DIR_EITHER,
1399 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1400 BIT(IIO_EV_INFO_ENABLE),
1401 },
1402 };
1403
1404 static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1405 [ALS] = {
1406 .channel_with_events = {
1407 {
1408 .type = IIO_LIGHT,
1409 .indexed = 1,
1410 .channel = 0,
1411 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1412 }, {
1413 .type = IIO_INTENSITY,
1414 .indexed = 1,
1415 .channel = 0,
1416 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1417 BIT(IIO_CHAN_INFO_INT_TIME) |
1418 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1419 BIT(IIO_CHAN_INFO_CALIBBIAS),
1420 .info_mask_separate_available =
1421 BIT(IIO_CHAN_INFO_INT_TIME) |
1422 BIT(IIO_CHAN_INFO_CALIBSCALE),
1423 .event_spec = tsl2772_events,
1424 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1425 }, {
1426 .type = IIO_INTENSITY,
1427 .indexed = 1,
1428 .channel = 1,
1429 },
1430 },
1431 .channel_without_events = {
1432 {
1433 .type = IIO_LIGHT,
1434 .indexed = 1,
1435 .channel = 0,
1436 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1437 }, {
1438 .type = IIO_INTENSITY,
1439 .indexed = 1,
1440 .channel = 0,
1441 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1442 BIT(IIO_CHAN_INFO_INT_TIME) |
1443 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1444 BIT(IIO_CHAN_INFO_CALIBBIAS),
1445 .info_mask_separate_available =
1446 BIT(IIO_CHAN_INFO_INT_TIME) |
1447 BIT(IIO_CHAN_INFO_CALIBSCALE),
1448 }, {
1449 .type = IIO_INTENSITY,
1450 .indexed = 1,
1451 .channel = 1,
1452 },
1453 },
1454 .chan_table_elements = 3,
1455 .info = &tsl2772_device_info[ALS],
1456 },
1457 [PRX] = {
1458 .channel_with_events = {
1459 {
1460 .type = IIO_PROXIMITY,
1461 .indexed = 1,
1462 .channel = 0,
1463 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1464 .event_spec = tsl2772_events,
1465 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1466 },
1467 },
1468 .channel_without_events = {
1469 {
1470 .type = IIO_PROXIMITY,
1471 .indexed = 1,
1472 .channel = 0,
1473 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1474 },
1475 },
1476 .chan_table_elements = 1,
1477 .info = &tsl2772_device_info[PRX],
1478 },
1479 [ALSPRX] = {
1480 .channel_with_events = {
1481 {
1482 .type = IIO_LIGHT,
1483 .indexed = 1,
1484 .channel = 0,
1485 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1486 }, {
1487 .type = IIO_INTENSITY,
1488 .indexed = 1,
1489 .channel = 0,
1490 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1491 BIT(IIO_CHAN_INFO_INT_TIME) |
1492 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1493 BIT(IIO_CHAN_INFO_CALIBBIAS),
1494 .info_mask_separate_available =
1495 BIT(IIO_CHAN_INFO_INT_TIME) |
1496 BIT(IIO_CHAN_INFO_CALIBSCALE),
1497 .event_spec = tsl2772_events,
1498 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1499 }, {
1500 .type = IIO_INTENSITY,
1501 .indexed = 1,
1502 .channel = 1,
1503 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1504 }, {
1505 .type = IIO_PROXIMITY,
1506 .indexed = 1,
1507 .channel = 0,
1508 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1509 .event_spec = tsl2772_events,
1510 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1511 },
1512 },
1513 .channel_without_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 }, {
1531 .type = IIO_INTENSITY,
1532 .indexed = 1,
1533 .channel = 1,
1534 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1535 }, {
1536 .type = IIO_PROXIMITY,
1537 .indexed = 1,
1538 .channel = 0,
1539 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1540 },
1541 },
1542 .chan_table_elements = 4,
1543 .info = &tsl2772_device_info[ALSPRX],
1544 },
1545 [PRX2] = {
1546 .channel_with_events = {
1547 {
1548 .type = IIO_PROXIMITY,
1549 .indexed = 1,
1550 .channel = 0,
1551 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1552 BIT(IIO_CHAN_INFO_CALIBSCALE),
1553 .info_mask_separate_available =
1554 BIT(IIO_CHAN_INFO_CALIBSCALE),
1555 .event_spec = tsl2772_events,
1556 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1557 },
1558 },
1559 .channel_without_events = {
1560 {
1561 .type = IIO_PROXIMITY,
1562 .indexed = 1,
1563 .channel = 0,
1564 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1565 BIT(IIO_CHAN_INFO_CALIBSCALE),
1566 .info_mask_separate_available =
1567 BIT(IIO_CHAN_INFO_CALIBSCALE),
1568 },
1569 },
1570 .chan_table_elements = 1,
1571 .info = &tsl2772_device_info[PRX2],
1572 },
1573 [ALSPRX2] = {
1574 .channel_with_events = {
1575 {
1576 .type = IIO_LIGHT,
1577 .indexed = 1,
1578 .channel = 0,
1579 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1580 }, {
1581 .type = IIO_INTENSITY,
1582 .indexed = 1,
1583 .channel = 0,
1584 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1585 BIT(IIO_CHAN_INFO_INT_TIME) |
1586 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1587 BIT(IIO_CHAN_INFO_CALIBBIAS),
1588 .info_mask_separate_available =
1589 BIT(IIO_CHAN_INFO_INT_TIME) |
1590 BIT(IIO_CHAN_INFO_CALIBSCALE),
1591 .event_spec = tsl2772_events,
1592 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1593 }, {
1594 .type = IIO_INTENSITY,
1595 .indexed = 1,
1596 .channel = 1,
1597 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1598 }, {
1599 .type = IIO_PROXIMITY,
1600 .indexed = 1,
1601 .channel = 0,
1602 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1603 BIT(IIO_CHAN_INFO_CALIBSCALE),
1604 .info_mask_separate_available =
1605 BIT(IIO_CHAN_INFO_CALIBSCALE),
1606 .event_spec = tsl2772_events,
1607 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1608 },
1609 },
1610 .channel_without_events = {
1611 {
1612 .type = IIO_LIGHT,
1613 .indexed = 1,
1614 .channel = 0,
1615 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1616 }, {
1617 .type = IIO_INTENSITY,
1618 .indexed = 1,
1619 .channel = 0,
1620 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1621 BIT(IIO_CHAN_INFO_INT_TIME) |
1622 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1623 BIT(IIO_CHAN_INFO_CALIBBIAS),
1624 .info_mask_separate_available =
1625 BIT(IIO_CHAN_INFO_INT_TIME) |
1626 BIT(IIO_CHAN_INFO_CALIBSCALE),
1627 }, {
1628 .type = IIO_INTENSITY,
1629 .indexed = 1,
1630 .channel = 1,
1631 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1632 }, {
1633 .type = IIO_PROXIMITY,
1634 .indexed = 1,
1635 .channel = 0,
1636 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1637 BIT(IIO_CHAN_INFO_CALIBSCALE),
1638 .info_mask_separate_available =
1639 BIT(IIO_CHAN_INFO_CALIBSCALE),
1640 },
1641 },
1642 .chan_table_elements = 4,
1643 .info = &tsl2772_device_info[ALSPRX2],
1644 },
1645 };
1646
tsl2772_probe(struct i2c_client * clientp,const struct i2c_device_id * id)1647 static int tsl2772_probe(struct i2c_client *clientp,
1648 const struct i2c_device_id *id)
1649 {
1650 struct iio_dev *indio_dev;
1651 struct tsl2772_chip *chip;
1652 int ret;
1653
1654 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1655 if (!indio_dev)
1656 return -ENOMEM;
1657
1658 chip = iio_priv(indio_dev);
1659 chip->client = clientp;
1660 i2c_set_clientdata(clientp, indio_dev);
1661
1662 ret = i2c_smbus_read_byte_data(chip->client,
1663 TSL2772_CMD_REG | TSL2772_CHIPID);
1664 if (ret < 0)
1665 return ret;
1666
1667 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1668 dev_info(&chip->client->dev,
1669 "%s: i2c device found does not match expected id\n",
1670 __func__);
1671 return -EINVAL;
1672 }
1673
1674 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1675 if (ret < 0) {
1676 dev_err(&clientp->dev,
1677 "%s: Failed to write to CMD register: %d\n",
1678 __func__, ret);
1679 return ret;
1680 }
1681
1682 mutex_init(&chip->als_mutex);
1683 mutex_init(&chip->prox_mutex);
1684
1685 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1686 chip->pdata = dev_get_platdata(&clientp->dev);
1687 chip->id = id->driver_data;
1688 chip->chip_info =
1689 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1690
1691 indio_dev->info = chip->chip_info->info;
1692 indio_dev->dev.parent = &clientp->dev;
1693 indio_dev->modes = INDIO_DIRECT_MODE;
1694 indio_dev->name = chip->client->name;
1695 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1696
1697 if (clientp->irq) {
1698 indio_dev->channels = chip->chip_info->channel_with_events;
1699
1700 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1701 NULL,
1702 &tsl2772_event_handler,
1703 IRQF_TRIGGER_FALLING |
1704 IRQF_ONESHOT,
1705 "TSL2772_event",
1706 indio_dev);
1707 if (ret) {
1708 dev_err(&clientp->dev,
1709 "%s: irq request failed\n", __func__);
1710 return ret;
1711 }
1712 } else {
1713 indio_dev->channels = chip->chip_info->channel_without_events;
1714 }
1715
1716 tsl2772_defaults(chip);
1717 ret = tsl2772_chip_on(indio_dev);
1718 if (ret < 0)
1719 return ret;
1720
1721 ret = devm_add_action_or_reset(&clientp->dev,
1722 tsl2772_chip_off_action,
1723 indio_dev);
1724 if (ret < 0)
1725 return ret;
1726
1727 ret = iio_device_register(indio_dev);
1728 if (ret) {
1729 dev_err(&clientp->dev,
1730 "%s: iio registration failed\n", __func__);
1731 return ret;
1732 }
1733
1734 return 0;
1735 }
1736
tsl2772_suspend(struct device * dev)1737 static int tsl2772_suspend(struct device *dev)
1738 {
1739 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1740
1741 return tsl2772_chip_off(indio_dev);
1742 }
1743
tsl2772_resume(struct device * dev)1744 static int tsl2772_resume(struct device *dev)
1745 {
1746 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1747
1748 return tsl2772_chip_on(indio_dev);
1749 }
1750
tsl2772_remove(struct i2c_client * client)1751 static int tsl2772_remove(struct i2c_client *client)
1752 {
1753 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1754
1755 iio_device_unregister(indio_dev);
1756
1757 return 0;
1758 }
1759
1760 static const struct i2c_device_id tsl2772_idtable[] = {
1761 { "tsl2571", tsl2571 },
1762 { "tsl2671", tsl2671 },
1763 { "tmd2671", tmd2671 },
1764 { "tsl2771", tsl2771 },
1765 { "tmd2771", tmd2771 },
1766 { "tsl2572", tsl2572 },
1767 { "tsl2672", tsl2672 },
1768 { "tmd2672", tmd2672 },
1769 { "tsl2772", tsl2772 },
1770 { "tmd2772", tmd2772 },
1771 {}
1772 };
1773
1774 MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1775
1776 static const struct of_device_id tsl2772_of_match[] = {
1777 { .compatible = "amstaos,tsl2571" },
1778 { .compatible = "amstaos,tsl2671" },
1779 { .compatible = "amstaos,tmd2671" },
1780 { .compatible = "amstaos,tsl2771" },
1781 { .compatible = "amstaos,tmd2771" },
1782 { .compatible = "amstaos,tsl2572" },
1783 { .compatible = "amstaos,tsl2672" },
1784 { .compatible = "amstaos,tmd2672" },
1785 { .compatible = "amstaos,tsl2772" },
1786 { .compatible = "amstaos,tmd2772" },
1787 {}
1788 };
1789 MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1790
1791 static const struct dev_pm_ops tsl2772_pm_ops = {
1792 .suspend = tsl2772_suspend,
1793 .resume = tsl2772_resume,
1794 };
1795
1796 static struct i2c_driver tsl2772_driver = {
1797 .driver = {
1798 .name = "tsl2772",
1799 .of_match_table = tsl2772_of_match,
1800 .pm = &tsl2772_pm_ops,
1801 },
1802 .id_table = tsl2772_idtable,
1803 .probe = tsl2772_probe,
1804 .remove = tsl2772_remove,
1805 };
1806
1807 module_i2c_driver(tsl2772_driver);
1808
1809 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1810 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1811 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1812 MODULE_LICENSE("GPL");
1813