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