1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2021 Joe Sandom <joe.g.sandom@gmail.com>
5 * Datasheet: https://ams.com/tsl25911#tab/documents
7 * Device driver for the TAOS TSL2591. This is a very-high sensitivity
8 * light-to-digital converter that transforms light intensity into a digital
12 #include <linux/bitfield.h>
13 #include <linux/debugfs.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/iopoll.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/sysfs.h>
24 #include <linux/unaligned.h>
26 #include <linux/iio/events.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
30 /* ADC integration time, field value to time in ms */
31 #define TSL2591_FVAL_TO_MSEC(x) (((x) + 1) * 100)
32 /* ADC integration time, field value to time in seconds */
33 #define TSL2591_FVAL_TO_SEC(x) ((x) + 1)
34 /* ADC integration time, time in seconds to field value */
35 #define TSL2591_SEC_TO_FVAL(x) ((x) - 1)
37 /* TSL2591 register set */
38 #define TSL2591_ENABLE 0x00
39 #define TSL2591_CONTROL 0x01
40 #define TSL2591_AILTL 0x04
41 #define TSL2591_AILTH 0x05
42 #define TSL2591_AIHTL 0x06
43 #define TSL2591_AIHTH 0x07
44 #define TSL2591_NP_AILTL 0x08
45 #define TSL2591_NP_AILTH 0x09
46 #define TSL2591_NP_AIHTL 0x0A
47 #define TSL2591_NP_AIHTH 0x0B
48 #define TSL2591_PERSIST 0x0C
49 #define TSL2591_PACKAGE_ID 0x11
50 #define TSL2591_DEVICE_ID 0x12
51 #define TSL2591_STATUS 0x13
52 #define TSL2591_C0_DATAL 0x14
53 #define TSL2591_C0_DATAH 0x15
54 #define TSL2591_C1_DATAL 0x16
55 #define TSL2591_C1_DATAH 0x17
57 /* TSL2591 command register definitions */
58 #define TSL2591_CMD_NOP 0xA0
59 #define TSL2591_CMD_SF_INTSET 0xE4
60 #define TSL2591_CMD_SF_CALS_I 0xE5
61 #define TSL2591_CMD_SF_CALS_NPI 0xE7
62 #define TSL2591_CMD_SF_CNP_ALSI 0xEA
64 /* TSL2591 enable register definitions */
65 #define TSL2591_PWR_ON 0x01
66 #define TSL2591_PWR_OFF 0x00
67 #define TSL2591_ENABLE_ALS 0x02
68 #define TSL2591_ENABLE_ALS_INT 0x10
69 #define TSL2591_ENABLE_SLEEP_INT 0x40
70 #define TSL2591_ENABLE_NP_INT 0x80
72 /* TSL2591 control register definitions */
73 #define TSL2591_CTRL_ALS_INTEGRATION_100MS 0x00
74 #define TSL2591_CTRL_ALS_INTEGRATION_200MS 0x01
75 #define TSL2591_CTRL_ALS_INTEGRATION_300MS 0x02
76 #define TSL2591_CTRL_ALS_INTEGRATION_400MS 0x03
77 #define TSL2591_CTRL_ALS_INTEGRATION_500MS 0x04
78 #define TSL2591_CTRL_ALS_INTEGRATION_600MS 0x05
79 #define TSL2591_CTRL_ALS_LOW_GAIN 0x00
80 #define TSL2591_CTRL_ALS_MED_GAIN 0x10
81 #define TSL2591_CTRL_ALS_HIGH_GAIN 0x20
82 #define TSL2591_CTRL_ALS_MAX_GAIN 0x30
83 #define TSL2591_CTRL_SYS_RESET 0x80
85 /* TSL2591 persist register definitions */
86 #define TSL2591_PRST_ALS_INT_CYCLE_0 0x00
87 #define TSL2591_PRST_ALS_INT_CYCLE_ANY 0x01
88 #define TSL2591_PRST_ALS_INT_CYCLE_2 0x02
89 #define TSL2591_PRST_ALS_INT_CYCLE_3 0x03
90 #define TSL2591_PRST_ALS_INT_CYCLE_5 0x04
91 #define TSL2591_PRST_ALS_INT_CYCLE_10 0x05
92 #define TSL2591_PRST_ALS_INT_CYCLE_15 0x06
93 #define TSL2591_PRST_ALS_INT_CYCLE_20 0x07
94 #define TSL2591_PRST_ALS_INT_CYCLE_25 0x08
95 #define TSL2591_PRST_ALS_INT_CYCLE_30 0x09
96 #define TSL2591_PRST_ALS_INT_CYCLE_35 0x0A
97 #define TSL2591_PRST_ALS_INT_CYCLE_40 0x0B
98 #define TSL2591_PRST_ALS_INT_CYCLE_45 0x0C
99 #define TSL2591_PRST_ALS_INT_CYCLE_50 0x0D
100 #define TSL2591_PRST_ALS_INT_CYCLE_55 0x0E
101 #define TSL2591_PRST_ALS_INT_CYCLE_60 0x0F
102 #define TSL2591_PRST_ALS_INT_CYCLE_MAX (BIT(4) - 1)
104 /* TSL2591 PID register mask */
105 #define TSL2591_PACKAGE_ID_MASK GENMASK(5, 4)
107 /* TSL2591 ID register mask */
108 #define TSL2591_DEVICE_ID_MASK GENMASK(7, 0)
110 /* TSL2591 status register masks */
111 #define TSL2591_STS_ALS_VALID_MASK BIT(0)
112 #define TSL2591_STS_ALS_INT_MASK BIT(4)
113 #define TSL2591_STS_NPERS_INT_MASK BIT(5)
114 #define TSL2591_STS_VAL_HIGH_MASK BIT(0)
116 /* TSL2591 constant values */
117 #define TSL2591_PACKAGE_ID_VAL 0x00
118 #define TSL2591_DEVICE_ID_VAL 0x50
120 /* Power off suspend delay time MS */
121 #define TSL2591_POWER_OFF_DELAY_MS 2000
123 /* TSL2591 default values */
124 #define TSL2591_DEFAULT_ALS_INT_TIME TSL2591_CTRL_ALS_INTEGRATION_300MS
125 #define TSL2591_DEFAULT_ALS_GAIN TSL2591_CTRL_ALS_MED_GAIN
126 #define TSL2591_DEFAULT_ALS_PERSIST TSL2591_PRST_ALS_INT_CYCLE_ANY
127 #define TSL2591_DEFAULT_ALS_LOWER_THRESH 100
128 #define TSL2591_DEFAULT_ALS_UPPER_THRESH 1500
130 /* TSL2591 number of data registers */
131 #define TSL2591_NUM_DATA_REGISTERS 4
133 /* TSL2591 number of valid status reads on ADC complete */
134 #define TSL2591_ALS_STS_VALID_COUNT 10
136 /* TSL2591 delay period between polls when checking for ALS valid flag */
137 #define TSL2591_DELAY_PERIOD_US 10000
139 /* TSL2591 maximum values */
140 #define TSL2591_MAX_ALS_INT_TIME_MS 600
141 #define TSL2591_ALS_MAX_VALUE (BIT(16) - 1)
145 * AGAIN values from Adafruit's TSL2591 Arduino library
146 * https://github.com/adafruit/Adafruit_TSL2591_Library
148 #define TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER 1
149 #define TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER 25
150 #define TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER 428
151 #define TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER 9876
152 #define TSL2591_LUX_COEFFICIENT 408
154 struct tsl2591_als_settings
{
155 u16 als_lower_thresh
;
156 u16 als_upper_thresh
;
162 struct tsl2591_chip
{
163 struct tsl2591_als_settings als_settings
;
164 struct i2c_client
*client
;
166 * Keep als_settings in sync with hardware state
167 * and ensure multiple readers are serialized.
169 struct mutex als_mutex
;
174 * Period table is ALS persist cycle x integration time setting
175 * Integration times: 100ms, 200ms, 300ms, 400ms, 500ms, 600ms
176 * ALS cycles: 1, 2, 3, 5, 10, 20, 25, 30, 35, 40, 45, 50, 55, 60
178 static const char * const tsl2591_als_period_list
[] = {
179 "0.1 0.2 0.3 0.5 1.0 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0",
180 "0.2 0.4 0.6 1.0 2.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0",
181 "0.3 0.6 0.9 1.5 3.0 6.0 7.5 9.0 10.5 12.0 13.5 15.0 16.5 18.0",
182 "0.4 0.8 1.2 2.0 4.0 8.0 10.0 12.0 14.0 16.0 18.0 20.0 22.0 24.0",
183 "0.5 1.0 1.5 2.5 5.0 10.0 12.5 15.0 17.5 20.0 22.5 25.0 27.5 30.0",
184 "0.6 1.2 1.8 3.0 6.0 12.0 15.0 18.0 21.0 24.0 27.0 30.0 33.0 36.0",
187 static const int tsl2591_int_time_available
[] = {
191 static const int tsl2591_calibscale_available
[] = {
195 static int tsl2591_set_als_lower_threshold(struct tsl2591_chip
*chip
,
196 u16 als_lower_threshold
);
197 static int tsl2591_set_als_upper_threshold(struct tsl2591_chip
*chip
,
198 u16 als_upper_threshold
);
200 static int tsl2591_gain_to_multiplier(const u8 als_gain
)
203 case TSL2591_CTRL_ALS_LOW_GAIN
:
204 return TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER
;
205 case TSL2591_CTRL_ALS_MED_GAIN
:
206 return TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER
;
207 case TSL2591_CTRL_ALS_HIGH_GAIN
:
208 return TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER
;
209 case TSL2591_CTRL_ALS_MAX_GAIN
:
210 return TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER
;
216 static int tsl2591_multiplier_to_gain(const u32 multiplier
)
218 switch (multiplier
) {
219 case TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER
:
220 return TSL2591_CTRL_ALS_LOW_GAIN
;
221 case TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER
:
222 return TSL2591_CTRL_ALS_MED_GAIN
;
223 case TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER
:
224 return TSL2591_CTRL_ALS_HIGH_GAIN
;
225 case TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER
:
226 return TSL2591_CTRL_ALS_MAX_GAIN
;
232 static int tsl2591_persist_cycle_to_lit(const u8 als_persist
)
234 switch (als_persist
) {
235 case TSL2591_PRST_ALS_INT_CYCLE_ANY
:
237 case TSL2591_PRST_ALS_INT_CYCLE_2
:
239 case TSL2591_PRST_ALS_INT_CYCLE_3
:
241 case TSL2591_PRST_ALS_INT_CYCLE_5
:
243 case TSL2591_PRST_ALS_INT_CYCLE_10
:
245 case TSL2591_PRST_ALS_INT_CYCLE_15
:
247 case TSL2591_PRST_ALS_INT_CYCLE_20
:
249 case TSL2591_PRST_ALS_INT_CYCLE_25
:
251 case TSL2591_PRST_ALS_INT_CYCLE_30
:
253 case TSL2591_PRST_ALS_INT_CYCLE_35
:
255 case TSL2591_PRST_ALS_INT_CYCLE_40
:
257 case TSL2591_PRST_ALS_INT_CYCLE_45
:
259 case TSL2591_PRST_ALS_INT_CYCLE_50
:
261 case TSL2591_PRST_ALS_INT_CYCLE_55
:
263 case TSL2591_PRST_ALS_INT_CYCLE_60
:
270 static int tsl2591_persist_lit_to_cycle(const u8 als_persist
)
272 switch (als_persist
) {
274 return TSL2591_PRST_ALS_INT_CYCLE_ANY
;
276 return TSL2591_PRST_ALS_INT_CYCLE_2
;
278 return TSL2591_PRST_ALS_INT_CYCLE_3
;
280 return TSL2591_PRST_ALS_INT_CYCLE_5
;
282 return TSL2591_PRST_ALS_INT_CYCLE_10
;
284 return TSL2591_PRST_ALS_INT_CYCLE_15
;
286 return TSL2591_PRST_ALS_INT_CYCLE_20
;
288 return TSL2591_PRST_ALS_INT_CYCLE_25
;
290 return TSL2591_PRST_ALS_INT_CYCLE_30
;
292 return TSL2591_PRST_ALS_INT_CYCLE_35
;
294 return TSL2591_PRST_ALS_INT_CYCLE_40
;
296 return TSL2591_PRST_ALS_INT_CYCLE_45
;
298 return TSL2591_PRST_ALS_INT_CYCLE_50
;
300 return TSL2591_PRST_ALS_INT_CYCLE_55
;
302 return TSL2591_PRST_ALS_INT_CYCLE_60
;
308 static int tsl2591_compatible_int_time(struct tsl2591_chip
*chip
,
309 const u32 als_integration_time
)
311 switch (als_integration_time
) {
312 case TSL2591_CTRL_ALS_INTEGRATION_100MS
:
313 case TSL2591_CTRL_ALS_INTEGRATION_200MS
:
314 case TSL2591_CTRL_ALS_INTEGRATION_300MS
:
315 case TSL2591_CTRL_ALS_INTEGRATION_400MS
:
316 case TSL2591_CTRL_ALS_INTEGRATION_500MS
:
317 case TSL2591_CTRL_ALS_INTEGRATION_600MS
:
324 static int tsl2591_als_time_to_fval(const u32 als_integration_time
)
328 for (i
= 0; i
< ARRAY_SIZE(tsl2591_int_time_available
); i
++) {
329 if (als_integration_time
== tsl2591_int_time_available
[i
])
330 return TSL2591_SEC_TO_FVAL(als_integration_time
);
336 static int tsl2591_compatible_gain(struct tsl2591_chip
*chip
, const u8 als_gain
)
339 case TSL2591_CTRL_ALS_LOW_GAIN
:
340 case TSL2591_CTRL_ALS_MED_GAIN
:
341 case TSL2591_CTRL_ALS_HIGH_GAIN
:
342 case TSL2591_CTRL_ALS_MAX_GAIN
:
349 static int tsl2591_compatible_als_persist_cycle(struct tsl2591_chip
*chip
,
350 const u32 als_persist
)
352 switch (als_persist
) {
353 case TSL2591_PRST_ALS_INT_CYCLE_ANY
:
354 case TSL2591_PRST_ALS_INT_CYCLE_2
:
355 case TSL2591_PRST_ALS_INT_CYCLE_3
:
356 case TSL2591_PRST_ALS_INT_CYCLE_5
:
357 case TSL2591_PRST_ALS_INT_CYCLE_10
:
358 case TSL2591_PRST_ALS_INT_CYCLE_15
:
359 case TSL2591_PRST_ALS_INT_CYCLE_20
:
360 case TSL2591_PRST_ALS_INT_CYCLE_25
:
361 case TSL2591_PRST_ALS_INT_CYCLE_30
:
362 case TSL2591_PRST_ALS_INT_CYCLE_35
:
363 case TSL2591_PRST_ALS_INT_CYCLE_40
:
364 case TSL2591_PRST_ALS_INT_CYCLE_45
:
365 case TSL2591_PRST_ALS_INT_CYCLE_50
:
366 case TSL2591_PRST_ALS_INT_CYCLE_55
:
367 case TSL2591_PRST_ALS_INT_CYCLE_60
:
374 static int tsl2591_check_als_valid(struct i2c_client
*client
)
378 ret
= i2c_smbus_read_byte_data(client
, TSL2591_CMD_NOP
| TSL2591_STATUS
);
380 dev_err(&client
->dev
, "Failed to read register\n");
384 return FIELD_GET(TSL2591_STS_ALS_VALID_MASK
, ret
);
387 static int tsl2591_wait_adc_complete(struct tsl2591_chip
*chip
)
389 struct tsl2591_als_settings settings
= chip
->als_settings
;
390 struct i2c_client
*client
= chip
->client
;
395 delay
= TSL2591_FVAL_TO_MSEC(settings
.als_int_time
);
400 * Sleep for ALS integration time to allow enough time or an ADC read
401 * cycle to complete. Check status after delay for ALS valid.
405 /* Check for status ALS valid flag for up to 100ms */
406 ret
= readx_poll_timeout(tsl2591_check_als_valid
, client
,
407 val
, val
== TSL2591_STS_VAL_HIGH_MASK
,
408 TSL2591_DELAY_PERIOD_US
,
409 TSL2591_DELAY_PERIOD_US
* TSL2591_ALS_STS_VALID_COUNT
);
411 dev_err(&client
->dev
, "Timed out waiting for valid ALS data\n");
417 * tsl2591_read_channel_data - Reads raw channel data and calculates lux
419 * Formula for lux calculation;
420 * Derived from Adafruit's TSL2591 library
421 * Link: https://github.com/adafruit/Adafruit_TSL2591_Library
422 * Counts Per Lux (CPL) = (ATIME_ms * AGAIN) / LUX DF
423 * lux = ((C0DATA - C1DATA) * (1 - (C1DATA / C0DATA))) / CPL
425 * Scale values to get more representative value of lux i.e.
426 * lux = ((C0DATA - C1DATA) * (1000 - ((C1DATA * 1000) / C0DATA))) / CPL
428 * Channel 0 = IR + Visible
429 * Channel 1 = IR only
431 static int tsl2591_read_channel_data(struct iio_dev
*indio_dev
,
432 struct iio_chan_spec
const *chan
,
435 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
436 struct tsl2591_als_settings
*settings
= &chip
->als_settings
;
437 struct i2c_client
*client
= chip
->client
;
438 u8 als_data
[TSL2591_NUM_DATA_REGISTERS
];
439 int counts_per_lux
, int_time_fval
, gain_multi
, lux
;
440 u16 als_ch0
, als_ch1
;
443 ret
= tsl2591_wait_adc_complete(chip
);
445 dev_err(&client
->dev
, "No data available. Err: %d\n", ret
);
449 ret
= i2c_smbus_read_i2c_block_data(client
,
450 TSL2591_CMD_NOP
| TSL2591_C0_DATAL
,
451 sizeof(als_data
), als_data
);
453 dev_err(&client
->dev
, "Failed to read data bytes");
457 als_ch0
= get_unaligned_le16(&als_data
[0]);
458 als_ch1
= get_unaligned_le16(&als_data
[2]);
460 switch (chan
->type
) {
462 if (chan
->channel2
== IIO_MOD_LIGHT_BOTH
)
464 else if (chan
->channel2
== IIO_MOD_LIGHT_IR
)
470 gain_multi
= tsl2591_gain_to_multiplier(settings
->als_gain
);
471 if (gain_multi
< 0) {
472 dev_err(&client
->dev
, "Invalid multiplier");
476 int_time_fval
= TSL2591_FVAL_TO_MSEC(settings
->als_int_time
);
477 /* Calculate counts per lux value */
478 counts_per_lux
= (int_time_fval
* gain_multi
) / TSL2591_LUX_COEFFICIENT
;
480 dev_dbg(&client
->dev
, "Counts Per Lux: %d\n", counts_per_lux
);
482 /* Calculate lux value */
483 lux
= ((als_ch0
- als_ch1
) *
484 (1000 - ((als_ch1
* 1000) / als_ch0
))) / counts_per_lux
;
486 dev_dbg(&client
->dev
, "Raw lux calculation: %d\n", lux
);
488 /* Divide by 1000 to get real lux value before scaling */
491 /* Get the decimal part of lux reading */
492 *val2
= (lux
- (*val
* 1000)) * 1000;
502 static int tsl2591_set_als_gain_int_time(struct tsl2591_chip
*chip
)
504 struct tsl2591_als_settings als_settings
= chip
->als_settings
;
505 struct i2c_client
*client
= chip
->client
;
508 ret
= i2c_smbus_write_byte_data(client
,
509 TSL2591_CMD_NOP
| TSL2591_CONTROL
,
510 als_settings
.als_int_time
| als_settings
.als_gain
);
512 dev_err(&client
->dev
, "Failed to set als gain & int time\n");
517 static int tsl2591_set_als_lower_threshold(struct tsl2591_chip
*chip
,
518 u16 als_lower_threshold
)
520 struct tsl2591_als_settings als_settings
= chip
->als_settings
;
521 struct i2c_client
*client
= chip
->client
;
522 u16 als_upper_threshold
;
527 chip
->als_settings
.als_lower_thresh
= als_lower_threshold
;
530 * Lower threshold should not be greater or equal to upper.
531 * If this is the case, then assert upper threshold to new lower
532 * threshold + 1 to avoid ordering issues when setting thresholds.
534 if (als_lower_threshold
>= als_settings
.als_upper_thresh
) {
535 als_upper_threshold
= als_lower_threshold
+ 1;
536 tsl2591_set_als_upper_threshold(chip
, als_upper_threshold
);
539 als_lower_l
= als_lower_threshold
;
540 als_lower_h
= als_lower_threshold
>> 8;
542 ret
= i2c_smbus_write_byte_data(client
,
543 TSL2591_CMD_NOP
| TSL2591_AILTL
,
546 dev_err(&client
->dev
, "Failed to set als lower threshold\n");
550 ret
= i2c_smbus_write_byte_data(client
,
551 TSL2591_CMD_NOP
| TSL2591_AILTH
,
554 dev_err(&client
->dev
, "Failed to set als lower threshold\n");
561 static int tsl2591_set_als_upper_threshold(struct tsl2591_chip
*chip
,
562 u16 als_upper_threshold
)
564 struct tsl2591_als_settings als_settings
= chip
->als_settings
;
565 struct i2c_client
*client
= chip
->client
;
566 u16 als_lower_threshold
;
571 if (als_upper_threshold
> TSL2591_ALS_MAX_VALUE
)
574 chip
->als_settings
.als_upper_thresh
= als_upper_threshold
;
577 * Upper threshold should not be less than lower. If this
578 * is the case, then assert lower threshold to new upper
579 * threshold - 1 to avoid ordering issues when setting thresholds.
581 if (als_upper_threshold
< als_settings
.als_lower_thresh
) {
582 als_lower_threshold
= als_upper_threshold
- 1;
583 tsl2591_set_als_lower_threshold(chip
, als_lower_threshold
);
586 als_upper_l
= als_upper_threshold
;
587 als_upper_h
= als_upper_threshold
>> 8;
589 ret
= i2c_smbus_write_byte_data(client
,
590 TSL2591_CMD_NOP
| TSL2591_AIHTL
,
593 dev_err(&client
->dev
, "Failed to set als upper threshold\n");
597 ret
= i2c_smbus_write_byte_data(client
,
598 TSL2591_CMD_NOP
| TSL2591_AIHTH
,
601 dev_err(&client
->dev
, "Failed to set als upper threshold\n");
608 static int tsl2591_set_als_persist_cycle(struct tsl2591_chip
*chip
,
611 struct i2c_client
*client
= chip
->client
;
614 ret
= i2c_smbus_write_byte_data(client
,
615 TSL2591_CMD_NOP
| TSL2591_PERSIST
,
618 dev_err(&client
->dev
, "Failed to set als persist cycle\n");
620 chip
->als_settings
.als_persist
= als_persist
;
625 static int tsl2591_set_power_state(struct tsl2591_chip
*chip
, u8 state
)
627 struct i2c_client
*client
= chip
->client
;
630 ret
= i2c_smbus_write_byte_data(client
,
631 TSL2591_CMD_NOP
| TSL2591_ENABLE
,
634 dev_err(&client
->dev
,
635 "Failed to set the power state to %#04x\n", state
);
640 static ssize_t
tsl2591_in_illuminance_period_available_show(struct device
*dev
,
641 struct device_attribute
*attr
,
644 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
645 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
647 return sysfs_emit(buf
, "%s\n",
648 tsl2591_als_period_list
[chip
->als_settings
.als_int_time
]);
651 static IIO_DEVICE_ATTR_RO(tsl2591_in_illuminance_period_available
, 0);
653 static struct attribute
*tsl2591_event_attrs_ctrl
[] = {
654 &iio_dev_attr_tsl2591_in_illuminance_period_available
.dev_attr
.attr
,
658 static const struct attribute_group tsl2591_event_attribute_group
= {
659 .attrs
= tsl2591_event_attrs_ctrl
,
662 static const struct iio_event_spec tsl2591_events
[] = {
664 .type
= IIO_EV_TYPE_THRESH
,
665 .dir
= IIO_EV_DIR_RISING
,
666 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
668 .type
= IIO_EV_TYPE_THRESH
,
669 .dir
= IIO_EV_DIR_FALLING
,
670 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
672 .type
= IIO_EV_TYPE_THRESH
,
673 .dir
= IIO_EV_DIR_EITHER
,
674 .mask_separate
= BIT(IIO_EV_INFO_PERIOD
) |
675 BIT(IIO_EV_INFO_ENABLE
),
679 static const struct iio_chan_spec tsl2591_channels
[] = {
681 .type
= IIO_INTENSITY
,
683 .channel2
= IIO_MOD_LIGHT_IR
,
684 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
685 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_INT_TIME
) |
686 BIT(IIO_CHAN_INFO_CALIBSCALE
),
687 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_INT_TIME
) |
688 BIT(IIO_CHAN_INFO_CALIBSCALE
)
691 .type
= IIO_INTENSITY
,
693 .channel2
= IIO_MOD_LIGHT_BOTH
,
694 .event_spec
= tsl2591_events
,
695 .num_event_specs
= ARRAY_SIZE(tsl2591_events
),
696 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
697 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_INT_TIME
) |
698 BIT(IIO_CHAN_INFO_CALIBSCALE
),
699 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_INT_TIME
) |
700 BIT(IIO_CHAN_INFO_CALIBSCALE
)
704 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
705 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_INT_TIME
) |
706 BIT(IIO_CHAN_INFO_CALIBSCALE
),
707 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_INT_TIME
) |
708 BIT(IIO_CHAN_INFO_CALIBSCALE
)
712 static int tsl2591_read_raw(struct iio_dev
*indio_dev
,
713 struct iio_chan_spec
const *chan
,
714 int *val
, int *val2
, long mask
)
716 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
717 struct i2c_client
*client
= chip
->client
;
720 pm_runtime_get_sync(&client
->dev
);
722 mutex_lock(&chip
->als_mutex
);
725 case IIO_CHAN_INFO_RAW
:
726 if (chan
->type
!= IIO_INTENSITY
) {
731 ret
= tsl2591_read_channel_data(indio_dev
, chan
, val
, val2
);
737 case IIO_CHAN_INFO_PROCESSED
:
738 if (chan
->type
!= IIO_LIGHT
) {
743 ret
= tsl2591_read_channel_data(indio_dev
, chan
, val
, val2
);
747 ret
= IIO_VAL_INT_PLUS_MICRO
;
749 case IIO_CHAN_INFO_INT_TIME
:
750 if (chan
->type
!= IIO_INTENSITY
) {
755 *val
= TSL2591_FVAL_TO_SEC(chip
->als_settings
.als_int_time
);
758 case IIO_CHAN_INFO_CALIBSCALE
:
759 if (chan
->type
!= IIO_INTENSITY
) {
764 *val
= tsl2591_gain_to_multiplier(chip
->als_settings
.als_gain
);
773 mutex_unlock(&chip
->als_mutex
);
775 pm_runtime_mark_last_busy(&client
->dev
);
776 pm_runtime_put_autosuspend(&client
->dev
);
781 static int tsl2591_write_raw(struct iio_dev
*indio_dev
,
782 struct iio_chan_spec
const *chan
,
783 int val
, int val2
, long mask
)
785 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
790 mutex_lock(&chip
->als_mutex
);
793 case IIO_CHAN_INFO_INT_TIME
:
794 int_time
= tsl2591_als_time_to_fval(val
);
799 ret
= tsl2591_compatible_int_time(chip
, int_time
);
803 chip
->als_settings
.als_int_time
= int_time
;
805 case IIO_CHAN_INFO_CALIBSCALE
:
806 gain
= tsl2591_multiplier_to_gain(val
);
811 ret
= tsl2591_compatible_gain(chip
, gain
);
815 chip
->als_settings
.als_gain
= gain
;
822 ret
= tsl2591_set_als_gain_int_time(chip
);
825 mutex_unlock(&chip
->als_mutex
);
829 static int tsl2591_read_available(struct iio_dev
*indio_dev
,
830 struct iio_chan_spec
const *chan
,
831 const int **vals
, int *type
, int *length
,
835 case IIO_CHAN_INFO_INT_TIME
:
836 *length
= ARRAY_SIZE(tsl2591_int_time_available
);
837 *vals
= tsl2591_int_time_available
;
839 return IIO_AVAIL_LIST
;
841 case IIO_CHAN_INFO_CALIBSCALE
:
842 *length
= ARRAY_SIZE(tsl2591_calibscale_available
);
843 *vals
= tsl2591_calibscale_available
;
845 return IIO_AVAIL_LIST
;
851 static int tsl2591_read_event_value(struct iio_dev
*indio_dev
,
852 const struct iio_chan_spec
*chan
,
853 enum iio_event_type type
,
854 enum iio_event_direction dir
,
855 enum iio_event_info info
, int *val
,
858 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
859 struct i2c_client
*client
= chip
->client
;
860 int als_persist
, int_time
, period
;
863 mutex_lock(&chip
->als_mutex
);
866 case IIO_EV_INFO_VALUE
:
868 case IIO_EV_DIR_RISING
:
869 *val
= chip
->als_settings
.als_upper_thresh
;
871 case IIO_EV_DIR_FALLING
:
872 *val
= chip
->als_settings
.als_lower_thresh
;
880 case IIO_EV_INFO_PERIOD
:
881 ret
= i2c_smbus_read_byte_data(client
,
882 TSL2591_CMD_NOP
| TSL2591_PERSIST
);
883 if (ret
<= 0 || ret
> TSL2591_PRST_ALS_INT_CYCLE_MAX
)
886 als_persist
= tsl2591_persist_cycle_to_lit(ret
);
887 int_time
= TSL2591_FVAL_TO_MSEC(chip
->als_settings
.als_int_time
);
888 period
= als_persist
* (int_time
* MSEC_PER_SEC
);
890 *val
= period
/ USEC_PER_SEC
;
891 *val2
= period
% USEC_PER_SEC
;
893 ret
= IIO_VAL_INT_PLUS_MICRO
;
901 mutex_unlock(&chip
->als_mutex
);
905 static int tsl2591_write_event_value(struct iio_dev
*indio_dev
,
906 const struct iio_chan_spec
*chan
,
907 enum iio_event_type type
,
908 enum iio_event_direction dir
,
909 enum iio_event_info info
, int val
,
912 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
913 int period
, int_time
, als_persist
;
916 if (val
< 0 || val2
< 0)
919 mutex_lock(&chip
->als_mutex
);
922 case IIO_EV_INFO_VALUE
:
923 if (val
> TSL2591_ALS_MAX_VALUE
) {
929 case IIO_EV_DIR_RISING
:
930 ret
= tsl2591_set_als_upper_threshold(chip
, val
);
934 case IIO_EV_DIR_FALLING
:
935 ret
= tsl2591_set_als_lower_threshold(chip
, val
);
944 case IIO_EV_INFO_PERIOD
:
945 int_time
= TSL2591_FVAL_TO_MSEC(chip
->als_settings
.als_int_time
);
947 period
= ((val
* MSEC_PER_SEC
) +
948 (val2
/ MSEC_PER_SEC
)) / int_time
;
950 als_persist
= tsl2591_persist_lit_to_cycle(period
);
951 if (als_persist
< 0) {
956 ret
= tsl2591_compatible_als_persist_cycle(chip
, als_persist
);
960 ret
= tsl2591_set_als_persist_cycle(chip
, als_persist
);
970 mutex_unlock(&chip
->als_mutex
);
974 static int tsl2591_read_event_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
)
979 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
981 return chip
->events_enabled
;
984 static int tsl2591_write_event_config(struct iio_dev
*indio_dev
,
985 const struct iio_chan_spec
*chan
,
986 enum iio_event_type type
,
987 enum iio_event_direction dir
,
990 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
991 struct i2c_client
*client
= chip
->client
;
993 if (state
&& !chip
->events_enabled
) {
994 chip
->events_enabled
= true;
995 pm_runtime_get_sync(&client
->dev
);
996 } else if (!state
&& chip
->events_enabled
) {
997 chip
->events_enabled
= false;
998 pm_runtime_mark_last_busy(&client
->dev
);
999 pm_runtime_put_autosuspend(&client
->dev
);
1005 static const struct iio_info tsl2591_info
= {
1006 .event_attrs
= &tsl2591_event_attribute_group
,
1007 .read_raw
= tsl2591_read_raw
,
1008 .write_raw
= tsl2591_write_raw
,
1009 .read_avail
= tsl2591_read_available
,
1010 .read_event_value
= tsl2591_read_event_value
,
1011 .write_event_value
= tsl2591_write_event_value
,
1012 .read_event_config
= tsl2591_read_event_config
,
1013 .write_event_config
= tsl2591_write_event_config
,
1016 static const struct iio_info tsl2591_info_no_irq
= {
1017 .read_raw
= tsl2591_read_raw
,
1018 .write_raw
= tsl2591_write_raw
,
1019 .read_avail
= tsl2591_read_available
,
1022 static int tsl2591_suspend(struct device
*dev
)
1024 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1025 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
1028 mutex_lock(&chip
->als_mutex
);
1029 ret
= tsl2591_set_power_state(chip
, TSL2591_PWR_OFF
);
1030 mutex_unlock(&chip
->als_mutex
);
1035 static int tsl2591_resume(struct device
*dev
)
1037 int power_state
= TSL2591_PWR_ON
| TSL2591_ENABLE_ALS
;
1038 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1039 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
1042 if (chip
->events_enabled
)
1043 power_state
|= TSL2591_ENABLE_ALS_INT
;
1045 mutex_lock(&chip
->als_mutex
);
1046 ret
= tsl2591_set_power_state(chip
, power_state
);
1047 mutex_unlock(&chip
->als_mutex
);
1052 static DEFINE_RUNTIME_DEV_PM_OPS(tsl2591_pm_ops
, tsl2591_suspend
,
1053 tsl2591_resume
, NULL
);
1055 static irqreturn_t
tsl2591_event_handler(int irq
, void *private)
1057 struct iio_dev
*dev_info
= private;
1058 struct tsl2591_chip
*chip
= iio_priv(dev_info
);
1059 struct i2c_client
*client
= chip
->client
;
1061 if (!chip
->events_enabled
)
1064 iio_push_event(dev_info
,
1065 IIO_UNMOD_EVENT_CODE(IIO_LIGHT
, 0,
1068 iio_get_time_ns(dev_info
));
1071 i2c_smbus_write_byte(client
, TSL2591_CMD_SF_CALS_NPI
);
1076 static int tsl2591_load_defaults(struct tsl2591_chip
*chip
)
1080 chip
->als_settings
.als_int_time
= TSL2591_DEFAULT_ALS_INT_TIME
;
1081 chip
->als_settings
.als_gain
= TSL2591_DEFAULT_ALS_GAIN
;
1082 chip
->als_settings
.als_lower_thresh
= TSL2591_DEFAULT_ALS_LOWER_THRESH
;
1083 chip
->als_settings
.als_upper_thresh
= TSL2591_DEFAULT_ALS_UPPER_THRESH
;
1085 ret
= tsl2591_set_als_gain_int_time(chip
);
1089 ret
= tsl2591_set_als_persist_cycle(chip
, TSL2591_DEFAULT_ALS_PERSIST
);
1093 ret
= tsl2591_set_als_lower_threshold(chip
, TSL2591_DEFAULT_ALS_LOWER_THRESH
);
1097 ret
= tsl2591_set_als_upper_threshold(chip
, TSL2591_DEFAULT_ALS_UPPER_THRESH
);
1104 static void tsl2591_chip_off(void *data
)
1106 struct iio_dev
*indio_dev
= data
;
1107 struct tsl2591_chip
*chip
= iio_priv(indio_dev
);
1108 struct i2c_client
*client
= chip
->client
;
1110 pm_runtime_disable(&client
->dev
);
1111 pm_runtime_set_suspended(&client
->dev
);
1112 pm_runtime_put_noidle(&client
->dev
);
1114 tsl2591_set_power_state(chip
, TSL2591_PWR_OFF
);
1117 static int tsl2591_probe(struct i2c_client
*client
)
1119 struct tsl2591_chip
*chip
;
1120 struct iio_dev
*indio_dev
;
1123 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
1124 dev_err(&client
->dev
,
1125 "I2C smbus byte data functionality is not supported\n");
1129 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*chip
));
1133 chip
= iio_priv(indio_dev
);
1134 chip
->client
= client
;
1135 i2c_set_clientdata(client
, indio_dev
);
1138 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1139 NULL
, tsl2591_event_handler
,
1140 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1141 "tsl2591_irq", indio_dev
);
1143 dev_err_probe(&client
->dev
, ret
, "IRQ request error\n");
1146 indio_dev
->info
= &tsl2591_info
;
1148 indio_dev
->info
= &tsl2591_info_no_irq
;
1151 mutex_init(&chip
->als_mutex
);
1153 ret
= i2c_smbus_read_byte_data(client
,
1154 TSL2591_CMD_NOP
| TSL2591_DEVICE_ID
);
1156 dev_err(&client
->dev
,
1157 "Failed to read the device ID register\n");
1160 ret
= FIELD_GET(TSL2591_DEVICE_ID_MASK
, ret
);
1161 if (ret
!= TSL2591_DEVICE_ID_VAL
) {
1162 dev_err(&client
->dev
, "Device ID: %#04x unknown\n", ret
);
1166 indio_dev
->channels
= tsl2591_channels
;
1167 indio_dev
->num_channels
= ARRAY_SIZE(tsl2591_channels
);
1168 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1169 indio_dev
->name
= chip
->client
->name
;
1170 chip
->events_enabled
= false;
1172 pm_runtime_enable(&client
->dev
);
1173 pm_runtime_set_autosuspend_delay(&client
->dev
,
1174 TSL2591_POWER_OFF_DELAY_MS
);
1175 pm_runtime_use_autosuspend(&client
->dev
);
1178 * Add chip off to automatically managed path and disable runtime
1179 * power management. This ensures that the chip power management
1180 * is handled correctly on driver remove. tsl2591_chip_off() must be
1181 * added to the managed path after pm runtime is enabled and before
1182 * any error exit paths are met to ensure we're not left in a state
1183 * of pm runtime not being disabled properly.
1185 ret
= devm_add_action_or_reset(&client
->dev
, tsl2591_chip_off
,
1190 ret
= tsl2591_load_defaults(chip
);
1192 dev_err(&client
->dev
, "Failed to load sensor defaults\n");
1196 ret
= i2c_smbus_write_byte(client
, TSL2591_CMD_SF_CALS_NPI
);
1198 dev_err(&client
->dev
, "Failed to clear als irq\n");
1202 return devm_iio_device_register(&client
->dev
, indio_dev
);
1205 static const struct of_device_id tsl2591_of_match
[] = {
1206 { .compatible
= "amstaos,tsl2591"},
1209 MODULE_DEVICE_TABLE(of
, tsl2591_of_match
);
1211 static struct i2c_driver tsl2591_driver
= {
1214 .pm
= pm_ptr(&tsl2591_pm_ops
),
1215 .of_match_table
= tsl2591_of_match
,
1217 .probe
= tsl2591_probe
1219 module_i2c_driver(tsl2591_driver
);
1221 MODULE_AUTHOR("Joe Sandom <joe.g.sandom@gmail.com>");
1222 MODULE_DESCRIPTION("TAOS tsl2591 ambient light sensor driver");
1223 MODULE_LICENSE("GPL");