1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2021 Joe Sandom <joe.g.sandom@gmail.com>
4 *
5 * Datasheet: https://ams.com/tsl25911#tab/documents
6 *
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
9 * signal.
10 */
11
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>
23
24 #include <linux/unaligned.h>
25
26 #include <linux/iio/events.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29
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)
36
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
56
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
63
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
71
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
84
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)
103
104 /* TSL2591 PID register mask */
105 #define TSL2591_PACKAGE_ID_MASK GENMASK(5, 4)
106
107 /* TSL2591 ID register mask */
108 #define TSL2591_DEVICE_ID_MASK GENMASK(7, 0)
109
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)
115
116 /* TSL2591 constant values */
117 #define TSL2591_PACKAGE_ID_VAL 0x00
118 #define TSL2591_DEVICE_ID_VAL 0x50
119
120 /* Power off suspend delay time MS */
121 #define TSL2591_POWER_OFF_DELAY_MS 2000
122
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
129
130 /* TSL2591 number of data registers */
131 #define TSL2591_NUM_DATA_REGISTERS 4
132
133 /* TSL2591 number of valid status reads on ADC complete */
134 #define TSL2591_ALS_STS_VALID_COUNT 10
135
136 /* TSL2591 delay period between polls when checking for ALS valid flag */
137 #define TSL2591_DELAY_PERIOD_US 10000
138
139 /* TSL2591 maximum values */
140 #define TSL2591_MAX_ALS_INT_TIME_MS 600
141 #define TSL2591_ALS_MAX_VALUE (BIT(16) - 1)
142
143 /*
144 * LUX calculations;
145 * AGAIN values from Adafruit's TSL2591 Arduino library
146 * https://github.com/adafruit/Adafruit_TSL2591_Library
147 */
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
153
154 struct tsl2591_als_settings {
155 u16 als_lower_thresh;
156 u16 als_upper_thresh;
157 u8 als_int_time;
158 u8 als_persist;
159 u8 als_gain;
160 };
161
162 struct tsl2591_chip {
163 struct tsl2591_als_settings als_settings;
164 struct i2c_client *client;
165 /*
166 * Keep als_settings in sync with hardware state
167 * and ensure multiple readers are serialized.
168 */
169 struct mutex als_mutex;
170 bool events_enabled;
171 };
172
173 /*
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
177 */
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",
185 };
186
187 static const int tsl2591_int_time_available[] = {
188 1, 2, 3, 4, 5, 6,
189 };
190
191 static const int tsl2591_calibscale_available[] = {
192 1, 25, 428, 9876,
193 };
194
195 static const u8 tsl2591_persist_table[] = {
196 [TSL2591_PRST_ALS_INT_CYCLE_ANY] = 1,
197 [TSL2591_PRST_ALS_INT_CYCLE_2] = 2,
198 [TSL2591_PRST_ALS_INT_CYCLE_3] = 3,
199 [TSL2591_PRST_ALS_INT_CYCLE_5] = 5,
200 [TSL2591_PRST_ALS_INT_CYCLE_10] = 10,
201 [TSL2591_PRST_ALS_INT_CYCLE_15] = 15,
202 [TSL2591_PRST_ALS_INT_CYCLE_20] = 20,
203 [TSL2591_PRST_ALS_INT_CYCLE_25] = 25,
204 [TSL2591_PRST_ALS_INT_CYCLE_30] = 30,
205 [TSL2591_PRST_ALS_INT_CYCLE_35] = 35,
206 [TSL2591_PRST_ALS_INT_CYCLE_40] = 40,
207 [TSL2591_PRST_ALS_INT_CYCLE_45] = 45,
208 [TSL2591_PRST_ALS_INT_CYCLE_50] = 50,
209 [TSL2591_PRST_ALS_INT_CYCLE_55] = 55,
210 [TSL2591_PRST_ALS_INT_CYCLE_60] = 60,
211 };
212
213 static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
214 u16 als_lower_threshold);
215 static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
216 u16 als_upper_threshold);
217
tsl2591_gain_to_multiplier(const u8 als_gain)218 static int tsl2591_gain_to_multiplier(const u8 als_gain)
219 {
220 switch (als_gain) {
221 case TSL2591_CTRL_ALS_LOW_GAIN:
222 return TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER;
223 case TSL2591_CTRL_ALS_MED_GAIN:
224 return TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER;
225 case TSL2591_CTRL_ALS_HIGH_GAIN:
226 return TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER;
227 case TSL2591_CTRL_ALS_MAX_GAIN:
228 return TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER;
229 default:
230 return -EINVAL;
231 }
232 }
233
tsl2591_multiplier_to_gain(const u32 multiplier)234 static int tsl2591_multiplier_to_gain(const u32 multiplier)
235 {
236 switch (multiplier) {
237 case TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER:
238 return TSL2591_CTRL_ALS_LOW_GAIN;
239 case TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER:
240 return TSL2591_CTRL_ALS_MED_GAIN;
241 case TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER:
242 return TSL2591_CTRL_ALS_HIGH_GAIN;
243 case TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER:
244 return TSL2591_CTRL_ALS_MAX_GAIN;
245 default:
246 return -EINVAL;
247 }
248 }
249
tsl2591_persist_cycle_to_lit(const u8 als_persist)250 static int tsl2591_persist_cycle_to_lit(const u8 als_persist)
251 {
252 if (als_persist >= ARRAY_SIZE(tsl2591_persist_table) ||
253 !tsl2591_persist_table[als_persist])
254 return -EINVAL;
255
256 return tsl2591_persist_table[als_persist];
257 }
258
tsl2591_persist_lit_to_cycle(const u8 als_persist)259 static int tsl2591_persist_lit_to_cycle(const u8 als_persist)
260 {
261 for (unsigned int i = TSL2591_PRST_ALS_INT_CYCLE_ANY;
262 i < ARRAY_SIZE(tsl2591_persist_table); i++) {
263 if (als_persist == tsl2591_persist_table[i])
264 return i;
265 }
266
267 return -EINVAL;
268 }
269
tsl2591_compatible_int_time(struct tsl2591_chip * chip,const u32 als_integration_time)270 static int tsl2591_compatible_int_time(struct tsl2591_chip *chip,
271 const u32 als_integration_time)
272 {
273 switch (als_integration_time) {
274 case TSL2591_CTRL_ALS_INTEGRATION_100MS:
275 case TSL2591_CTRL_ALS_INTEGRATION_200MS:
276 case TSL2591_CTRL_ALS_INTEGRATION_300MS:
277 case TSL2591_CTRL_ALS_INTEGRATION_400MS:
278 case TSL2591_CTRL_ALS_INTEGRATION_500MS:
279 case TSL2591_CTRL_ALS_INTEGRATION_600MS:
280 return 0;
281 default:
282 return -EINVAL;
283 }
284 }
285
tsl2591_als_time_to_fval(const u32 als_integration_time)286 static int tsl2591_als_time_to_fval(const u32 als_integration_time)
287 {
288 int i;
289
290 for (i = 0; i < ARRAY_SIZE(tsl2591_int_time_available); i++) {
291 if (als_integration_time == tsl2591_int_time_available[i])
292 return TSL2591_SEC_TO_FVAL(als_integration_time);
293 }
294
295 return -EINVAL;
296 }
297
tsl2591_compatible_gain(struct tsl2591_chip * chip,const u8 als_gain)298 static int tsl2591_compatible_gain(struct tsl2591_chip *chip, const u8 als_gain)
299 {
300 switch (als_gain) {
301 case TSL2591_CTRL_ALS_LOW_GAIN:
302 case TSL2591_CTRL_ALS_MED_GAIN:
303 case TSL2591_CTRL_ALS_HIGH_GAIN:
304 case TSL2591_CTRL_ALS_MAX_GAIN:
305 return 0;
306 default:
307 return -EINVAL;
308 }
309 }
310
tsl2591_check_als_valid(struct i2c_client * client)311 static int tsl2591_check_als_valid(struct i2c_client *client)
312 {
313 int ret;
314
315 ret = i2c_smbus_read_byte_data(client, TSL2591_CMD_NOP | TSL2591_STATUS);
316 if (ret < 0) {
317 dev_err(&client->dev, "Failed to read register\n");
318 return -EINVAL;
319 }
320
321 return FIELD_GET(TSL2591_STS_ALS_VALID_MASK, ret);
322 }
323
tsl2591_wait_adc_complete(struct tsl2591_chip * chip)324 static int tsl2591_wait_adc_complete(struct tsl2591_chip *chip)
325 {
326 struct tsl2591_als_settings settings = chip->als_settings;
327 struct i2c_client *client = chip->client;
328 int delay;
329 int val;
330 int ret;
331
332 delay = TSL2591_FVAL_TO_MSEC(settings.als_int_time);
333 if (!delay)
334 return -EINVAL;
335
336 /*
337 * Sleep for ALS integration time to allow enough time or an ADC read
338 * cycle to complete. Check status after delay for ALS valid.
339 */
340 msleep(delay);
341
342 /* Check for status ALS valid flag for up to 100ms */
343 ret = readx_poll_timeout(tsl2591_check_als_valid, client,
344 val, val == TSL2591_STS_VAL_HIGH_MASK,
345 TSL2591_DELAY_PERIOD_US,
346 TSL2591_DELAY_PERIOD_US * TSL2591_ALS_STS_VALID_COUNT);
347 if (ret)
348 dev_err(&client->dev, "Timed out waiting for valid ALS data\n");
349
350 return ret;
351 }
352
353 /*
354 * tsl2591_read_channel_data - Reads raw channel data and calculates lux
355 *
356 * Formula for lux calculation;
357 * Derived from Adafruit's TSL2591 library
358 * Link: https://github.com/adafruit/Adafruit_TSL2591_Library
359 * Counts Per Lux (CPL) = (ATIME_ms * AGAIN) / LUX DF
360 * lux = ((C0DATA - C1DATA) * (1 - (C1DATA / C0DATA))) / CPL
361 *
362 * Scale values to get more representative value of lux i.e.
363 * lux = ((C0DATA - C1DATA) * (1000 - ((C1DATA * 1000) / C0DATA))) / CPL
364 *
365 * Channel 0 = IR + Visible
366 * Channel 1 = IR only
367 */
tsl2591_read_channel_data(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2)368 static int tsl2591_read_channel_data(struct iio_dev *indio_dev,
369 struct iio_chan_spec const *chan,
370 int *val, int *val2)
371 {
372 struct tsl2591_chip *chip = iio_priv(indio_dev);
373 struct tsl2591_als_settings *settings = &chip->als_settings;
374 struct i2c_client *client = chip->client;
375 u8 als_data[TSL2591_NUM_DATA_REGISTERS];
376 int counts_per_lux, int_time_fval, gain_multi, lux;
377 u16 als_ch0, als_ch1;
378 int ret;
379
380 ret = tsl2591_wait_adc_complete(chip);
381 if (ret < 0) {
382 dev_err(&client->dev, "No data available. Err: %d\n", ret);
383 return ret;
384 }
385
386 ret = i2c_smbus_read_i2c_block_data(client,
387 TSL2591_CMD_NOP | TSL2591_C0_DATAL,
388 sizeof(als_data), als_data);
389 if (ret < 0) {
390 dev_err(&client->dev, "Failed to read data bytes");
391 return ret;
392 }
393
394 als_ch0 = get_unaligned_le16(&als_data[0]);
395 als_ch1 = get_unaligned_le16(&als_data[2]);
396
397 switch (chan->type) {
398 case IIO_INTENSITY:
399 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
400 *val = als_ch0;
401 else if (chan->channel2 == IIO_MOD_LIGHT_IR)
402 *val = als_ch1;
403 else
404 return -EINVAL;
405 break;
406 case IIO_LIGHT:
407 gain_multi = tsl2591_gain_to_multiplier(settings->als_gain);
408 if (gain_multi < 0) {
409 dev_err(&client->dev, "Invalid multiplier");
410 return gain_multi;
411 }
412
413 int_time_fval = TSL2591_FVAL_TO_MSEC(settings->als_int_time);
414 /* Calculate counts per lux value */
415 counts_per_lux = (int_time_fval * gain_multi) / TSL2591_LUX_COEFFICIENT;
416
417 dev_dbg(&client->dev, "Counts Per Lux: %d\n", counts_per_lux);
418
419 /* Calculate lux value */
420 lux = ((als_ch0 - als_ch1) *
421 (1000 - ((als_ch1 * 1000) / als_ch0))) / counts_per_lux;
422
423 dev_dbg(&client->dev, "Raw lux calculation: %d\n", lux);
424
425 /* Divide by 1000 to get real lux value before scaling */
426 *val = lux / 1000;
427
428 /* Get the decimal part of lux reading */
429 *val2 = (lux - (*val * 1000)) * 1000;
430
431 break;
432 default:
433 return -EINVAL;
434 }
435
436 return 0;
437 }
438
tsl2591_set_als_gain_int_time(struct tsl2591_chip * chip)439 static int tsl2591_set_als_gain_int_time(struct tsl2591_chip *chip)
440 {
441 struct tsl2591_als_settings als_settings = chip->als_settings;
442 struct i2c_client *client = chip->client;
443 int ret;
444
445 ret = i2c_smbus_write_byte_data(client,
446 TSL2591_CMD_NOP | TSL2591_CONTROL,
447 als_settings.als_int_time | als_settings.als_gain);
448 if (ret)
449 dev_err(&client->dev, "Failed to set als gain & int time\n");
450
451 return ret;
452 }
453
tsl2591_set_als_lower_threshold(struct tsl2591_chip * chip,u16 als_lower_threshold)454 static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
455 u16 als_lower_threshold)
456 {
457 struct tsl2591_als_settings als_settings = chip->als_settings;
458 struct i2c_client *client = chip->client;
459 u16 als_upper_threshold;
460 u8 als_lower_l;
461 u8 als_lower_h;
462 int ret;
463
464 chip->als_settings.als_lower_thresh = als_lower_threshold;
465
466 /*
467 * Lower threshold should not be greater or equal to upper.
468 * If this is the case, then assert upper threshold to new lower
469 * threshold + 1 to avoid ordering issues when setting thresholds.
470 */
471 if (als_lower_threshold >= als_settings.als_upper_thresh) {
472 als_upper_threshold = als_lower_threshold + 1;
473 tsl2591_set_als_upper_threshold(chip, als_upper_threshold);
474 }
475
476 als_lower_l = als_lower_threshold;
477 als_lower_h = als_lower_threshold >> 8;
478
479 ret = i2c_smbus_write_byte_data(client,
480 TSL2591_CMD_NOP | TSL2591_AILTL,
481 als_lower_l);
482 if (ret) {
483 dev_err(&client->dev, "Failed to set als lower threshold\n");
484 return ret;
485 }
486
487 ret = i2c_smbus_write_byte_data(client,
488 TSL2591_CMD_NOP | TSL2591_AILTH,
489 als_lower_h);
490 if (ret) {
491 dev_err(&client->dev, "Failed to set als lower threshold\n");
492 return ret;
493 }
494
495 return 0;
496 }
497
tsl2591_set_als_upper_threshold(struct tsl2591_chip * chip,u16 als_upper_threshold)498 static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
499 u16 als_upper_threshold)
500 {
501 struct tsl2591_als_settings als_settings = chip->als_settings;
502 struct i2c_client *client = chip->client;
503 u16 als_lower_threshold;
504 u8 als_upper_l;
505 u8 als_upper_h;
506 int ret;
507
508 if (als_upper_threshold > TSL2591_ALS_MAX_VALUE)
509 return -EINVAL;
510
511 chip->als_settings.als_upper_thresh = als_upper_threshold;
512
513 /*
514 * Upper threshold should not be less than lower. If this
515 * is the case, then assert lower threshold to new upper
516 * threshold - 1 to avoid ordering issues when setting thresholds.
517 */
518 if (als_upper_threshold < als_settings.als_lower_thresh) {
519 als_lower_threshold = als_upper_threshold - 1;
520 tsl2591_set_als_lower_threshold(chip, als_lower_threshold);
521 }
522
523 als_upper_l = als_upper_threshold;
524 als_upper_h = als_upper_threshold >> 8;
525
526 ret = i2c_smbus_write_byte_data(client,
527 TSL2591_CMD_NOP | TSL2591_AIHTL,
528 als_upper_l);
529 if (ret) {
530 dev_err(&client->dev, "Failed to set als upper threshold\n");
531 return ret;
532 }
533
534 ret = i2c_smbus_write_byte_data(client,
535 TSL2591_CMD_NOP | TSL2591_AIHTH,
536 als_upper_h);
537 if (ret) {
538 dev_err(&client->dev, "Failed to set als upper threshold\n");
539 return ret;
540 }
541
542 return 0;
543 }
544
tsl2591_set_als_persist_cycle(struct tsl2591_chip * chip,u8 als_persist)545 static int tsl2591_set_als_persist_cycle(struct tsl2591_chip *chip,
546 u8 als_persist)
547 {
548 struct i2c_client *client = chip->client;
549 int ret;
550
551 ret = i2c_smbus_write_byte_data(client,
552 TSL2591_CMD_NOP | TSL2591_PERSIST,
553 als_persist);
554 if (ret)
555 dev_err(&client->dev, "Failed to set als persist cycle\n");
556
557 chip->als_settings.als_persist = als_persist;
558
559 return ret;
560 }
561
tsl2591_set_power_state(struct tsl2591_chip * chip,u8 state)562 static int tsl2591_set_power_state(struct tsl2591_chip *chip, u8 state)
563 {
564 struct i2c_client *client = chip->client;
565 int ret;
566
567 ret = i2c_smbus_write_byte_data(client,
568 TSL2591_CMD_NOP | TSL2591_ENABLE,
569 state);
570 if (ret)
571 dev_err(&client->dev,
572 "Failed to set the power state to %#04x\n", state);
573
574 return ret;
575 }
576
tsl2591_in_illuminance_period_available_show(struct device * dev,struct device_attribute * attr,char * buf)577 static ssize_t tsl2591_in_illuminance_period_available_show(struct device *dev,
578 struct device_attribute *attr,
579 char *buf)
580 {
581 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
582 struct tsl2591_chip *chip = iio_priv(indio_dev);
583
584 return sysfs_emit(buf, "%s\n",
585 tsl2591_als_period_list[chip->als_settings.als_int_time]);
586 }
587
588 static IIO_DEVICE_ATTR_RO(tsl2591_in_illuminance_period_available, 0);
589
590 static struct attribute *tsl2591_event_attrs_ctrl[] = {
591 &iio_dev_attr_tsl2591_in_illuminance_period_available.dev_attr.attr,
592 NULL
593 };
594
595 static const struct attribute_group tsl2591_event_attribute_group = {
596 .attrs = tsl2591_event_attrs_ctrl,
597 };
598
599 static const struct iio_event_spec tsl2591_events[] = {
600 {
601 .type = IIO_EV_TYPE_THRESH,
602 .dir = IIO_EV_DIR_RISING,
603 .mask_separate = BIT(IIO_EV_INFO_VALUE),
604 }, {
605 .type = IIO_EV_TYPE_THRESH,
606 .dir = IIO_EV_DIR_FALLING,
607 .mask_separate = BIT(IIO_EV_INFO_VALUE),
608 }, {
609 .type = IIO_EV_TYPE_THRESH,
610 .dir = IIO_EV_DIR_EITHER,
611 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
612 BIT(IIO_EV_INFO_ENABLE),
613 },
614 };
615
616 static const struct iio_chan_spec tsl2591_channels[] = {
617 {
618 .type = IIO_INTENSITY,
619 .modified = 1,
620 .channel2 = IIO_MOD_LIGHT_IR,
621 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
622 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
623 BIT(IIO_CHAN_INFO_CALIBSCALE),
624 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
625 BIT(IIO_CHAN_INFO_CALIBSCALE)
626 },
627 {
628 .type = IIO_INTENSITY,
629 .modified = 1,
630 .channel2 = IIO_MOD_LIGHT_BOTH,
631 .event_spec = tsl2591_events,
632 .num_event_specs = ARRAY_SIZE(tsl2591_events),
633 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
634 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
635 BIT(IIO_CHAN_INFO_CALIBSCALE),
636 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
637 BIT(IIO_CHAN_INFO_CALIBSCALE)
638 },
639 {
640 .type = IIO_LIGHT,
641 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
642 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
643 BIT(IIO_CHAN_INFO_CALIBSCALE),
644 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
645 BIT(IIO_CHAN_INFO_CALIBSCALE)
646 },
647 };
648
tsl2591_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)649 static int tsl2591_read_raw(struct iio_dev *indio_dev,
650 struct iio_chan_spec const *chan,
651 int *val, int *val2, long mask)
652 {
653 struct tsl2591_chip *chip = iio_priv(indio_dev);
654 struct i2c_client *client = chip->client;
655 int ret;
656
657 pm_runtime_get_sync(&client->dev);
658
659 mutex_lock(&chip->als_mutex);
660
661 switch (mask) {
662 case IIO_CHAN_INFO_RAW:
663 if (chan->type != IIO_INTENSITY) {
664 ret = -EINVAL;
665 goto err_unlock;
666 }
667
668 ret = tsl2591_read_channel_data(indio_dev, chan, val, val2);
669 if (ret < 0)
670 goto err_unlock;
671
672 ret = IIO_VAL_INT;
673 break;
674 case IIO_CHAN_INFO_PROCESSED:
675 if (chan->type != IIO_LIGHT) {
676 ret = -EINVAL;
677 goto err_unlock;
678 }
679
680 ret = tsl2591_read_channel_data(indio_dev, chan, val, val2);
681 if (ret < 0)
682 break;
683
684 ret = IIO_VAL_INT_PLUS_MICRO;
685 break;
686 case IIO_CHAN_INFO_INT_TIME:
687 if (chan->type != IIO_INTENSITY) {
688 ret = -EINVAL;
689 goto err_unlock;
690 }
691
692 *val = TSL2591_FVAL_TO_SEC(chip->als_settings.als_int_time);
693 ret = IIO_VAL_INT;
694 break;
695 case IIO_CHAN_INFO_CALIBSCALE:
696 if (chan->type != IIO_INTENSITY) {
697 ret = -EINVAL;
698 goto err_unlock;
699 }
700
701 *val = tsl2591_gain_to_multiplier(chip->als_settings.als_gain);
702 ret = IIO_VAL_INT;
703 break;
704 default:
705 ret = -EINVAL;
706 break;
707 }
708
709 err_unlock:
710 mutex_unlock(&chip->als_mutex);
711
712 pm_runtime_put_autosuspend(&client->dev);
713
714 return ret;
715 }
716
tsl2591_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)717 static int tsl2591_write_raw(struct iio_dev *indio_dev,
718 struct iio_chan_spec const *chan,
719 int val, int val2, long mask)
720 {
721 struct tsl2591_chip *chip = iio_priv(indio_dev);
722 int int_time;
723 int gain;
724 int ret;
725
726 mutex_lock(&chip->als_mutex);
727
728 switch (mask) {
729 case IIO_CHAN_INFO_INT_TIME:
730 int_time = tsl2591_als_time_to_fval(val);
731 if (int_time < 0) {
732 ret = int_time;
733 goto err_unlock;
734 }
735 ret = tsl2591_compatible_int_time(chip, int_time);
736 if (ret < 0)
737 goto err_unlock;
738
739 chip->als_settings.als_int_time = int_time;
740 break;
741 case IIO_CHAN_INFO_CALIBSCALE:
742 gain = tsl2591_multiplier_to_gain(val);
743 if (gain < 0) {
744 ret = gain;
745 goto err_unlock;
746 }
747 ret = tsl2591_compatible_gain(chip, gain);
748 if (ret < 0)
749 goto err_unlock;
750
751 chip->als_settings.als_gain = gain;
752 break;
753 default:
754 ret = -EINVAL;
755 goto err_unlock;
756 }
757
758 ret = tsl2591_set_als_gain_int_time(chip);
759
760 err_unlock:
761 mutex_unlock(&chip->als_mutex);
762 return ret;
763 }
764
tsl2591_read_available(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)765 static int tsl2591_read_available(struct iio_dev *indio_dev,
766 struct iio_chan_spec const *chan,
767 const int **vals, int *type, int *length,
768 long mask)
769 {
770 switch (mask) {
771 case IIO_CHAN_INFO_INT_TIME:
772 *length = ARRAY_SIZE(tsl2591_int_time_available);
773 *vals = tsl2591_int_time_available;
774 *type = IIO_VAL_INT;
775 return IIO_AVAIL_LIST;
776
777 case IIO_CHAN_INFO_CALIBSCALE:
778 *length = ARRAY_SIZE(tsl2591_calibscale_available);
779 *vals = tsl2591_calibscale_available;
780 *type = IIO_VAL_INT;
781 return IIO_AVAIL_LIST;
782 default:
783 return -EINVAL;
784 }
785 }
786
tsl2591_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)787 static int tsl2591_read_event_value(struct iio_dev *indio_dev,
788 const struct iio_chan_spec *chan,
789 enum iio_event_type type,
790 enum iio_event_direction dir,
791 enum iio_event_info info, int *val,
792 int *val2)
793 {
794 struct tsl2591_chip *chip = iio_priv(indio_dev);
795 struct i2c_client *client = chip->client;
796 int als_persist, int_time, period;
797 int ret;
798
799 mutex_lock(&chip->als_mutex);
800
801 switch (info) {
802 case IIO_EV_INFO_VALUE:
803 switch (dir) {
804 case IIO_EV_DIR_RISING:
805 *val = chip->als_settings.als_upper_thresh;
806 break;
807 case IIO_EV_DIR_FALLING:
808 *val = chip->als_settings.als_lower_thresh;
809 break;
810 default:
811 ret = -EINVAL;
812 goto err_unlock;
813 }
814 ret = IIO_VAL_INT;
815 break;
816 case IIO_EV_INFO_PERIOD:
817 ret = i2c_smbus_read_byte_data(client,
818 TSL2591_CMD_NOP | TSL2591_PERSIST);
819 if (ret <= 0 || ret > TSL2591_PRST_ALS_INT_CYCLE_MAX)
820 goto err_unlock;
821
822 als_persist = tsl2591_persist_cycle_to_lit(ret);
823 int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time);
824 period = als_persist * (int_time * MSEC_PER_SEC);
825
826 *val = period / USEC_PER_SEC;
827 *val2 = period % USEC_PER_SEC;
828
829 ret = IIO_VAL_INT_PLUS_MICRO;
830 break;
831 default:
832 ret = -EINVAL;
833 break;
834 }
835
836 err_unlock:
837 mutex_unlock(&chip->als_mutex);
838 return ret;
839 }
840
tsl2591_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)841 static int tsl2591_write_event_value(struct iio_dev *indio_dev,
842 const struct iio_chan_spec *chan,
843 enum iio_event_type type,
844 enum iio_event_direction dir,
845 enum iio_event_info info, int val,
846 int val2)
847 {
848 struct tsl2591_chip *chip = iio_priv(indio_dev);
849 int period, int_time, als_persist;
850 int ret;
851
852 if (val < 0 || val2 < 0)
853 return -EINVAL;
854
855 mutex_lock(&chip->als_mutex);
856
857 switch (info) {
858 case IIO_EV_INFO_VALUE:
859 if (val > TSL2591_ALS_MAX_VALUE) {
860 ret = -EINVAL;
861 goto err_unlock;
862 }
863
864 switch (dir) {
865 case IIO_EV_DIR_RISING:
866 ret = tsl2591_set_als_upper_threshold(chip, val);
867 if (ret < 0)
868 goto err_unlock;
869 break;
870 case IIO_EV_DIR_FALLING:
871 ret = tsl2591_set_als_lower_threshold(chip, val);
872 if (ret < 0)
873 goto err_unlock;
874 break;
875 default:
876 ret = -EINVAL;
877 goto err_unlock;
878 }
879 break;
880 case IIO_EV_INFO_PERIOD:
881 int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time);
882
883 period = ((val * MSEC_PER_SEC) +
884 (val2 / MSEC_PER_SEC)) / int_time;
885
886 als_persist = tsl2591_persist_lit_to_cycle(period);
887 if (als_persist < 0) {
888 ret = -EINVAL;
889 goto err_unlock;
890 }
891
892 ret = tsl2591_set_als_persist_cycle(chip, als_persist);
893 if (ret < 0)
894 goto err_unlock;
895 break;
896 default:
897 ret = -EINVAL;
898 break;
899 }
900
901 err_unlock:
902 mutex_unlock(&chip->als_mutex);
903 return ret;
904 }
905
tsl2591_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)906 static int tsl2591_read_event_config(struct iio_dev *indio_dev,
907 const struct iio_chan_spec *chan,
908 enum iio_event_type type,
909 enum iio_event_direction dir)
910 {
911 struct tsl2591_chip *chip = iio_priv(indio_dev);
912
913 return chip->events_enabled;
914 }
915
tsl2591_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,bool state)916 static int tsl2591_write_event_config(struct iio_dev *indio_dev,
917 const struct iio_chan_spec *chan,
918 enum iio_event_type type,
919 enum iio_event_direction dir,
920 bool state)
921 {
922 struct tsl2591_chip *chip = iio_priv(indio_dev);
923 struct i2c_client *client = chip->client;
924
925 if (state && !chip->events_enabled) {
926 chip->events_enabled = true;
927 pm_runtime_get_sync(&client->dev);
928 } else if (!state && chip->events_enabled) {
929 chip->events_enabled = false;
930 pm_runtime_put_autosuspend(&client->dev);
931 }
932
933 return 0;
934 }
935
936 static const struct iio_info tsl2591_info = {
937 .event_attrs = &tsl2591_event_attribute_group,
938 .read_raw = tsl2591_read_raw,
939 .write_raw = tsl2591_write_raw,
940 .read_avail = tsl2591_read_available,
941 .read_event_value = tsl2591_read_event_value,
942 .write_event_value = tsl2591_write_event_value,
943 .read_event_config = tsl2591_read_event_config,
944 .write_event_config = tsl2591_write_event_config,
945 };
946
947 static const struct iio_info tsl2591_info_no_irq = {
948 .read_raw = tsl2591_read_raw,
949 .write_raw = tsl2591_write_raw,
950 .read_avail = tsl2591_read_available,
951 };
952
tsl2591_suspend(struct device * dev)953 static int tsl2591_suspend(struct device *dev)
954 {
955 struct iio_dev *indio_dev = dev_get_drvdata(dev);
956 struct tsl2591_chip *chip = iio_priv(indio_dev);
957 int ret;
958
959 mutex_lock(&chip->als_mutex);
960 ret = tsl2591_set_power_state(chip, TSL2591_PWR_OFF);
961 mutex_unlock(&chip->als_mutex);
962
963 return ret;
964 }
965
tsl2591_resume(struct device * dev)966 static int tsl2591_resume(struct device *dev)
967 {
968 int power_state = TSL2591_PWR_ON | TSL2591_ENABLE_ALS;
969 struct iio_dev *indio_dev = dev_get_drvdata(dev);
970 struct tsl2591_chip *chip = iio_priv(indio_dev);
971 int ret;
972
973 if (chip->events_enabled)
974 power_state |= TSL2591_ENABLE_ALS_INT;
975
976 mutex_lock(&chip->als_mutex);
977 ret = tsl2591_set_power_state(chip, power_state);
978 mutex_unlock(&chip->als_mutex);
979
980 return ret;
981 }
982
983 static DEFINE_RUNTIME_DEV_PM_OPS(tsl2591_pm_ops, tsl2591_suspend,
984 tsl2591_resume, NULL);
985
tsl2591_event_handler(int irq,void * private)986 static irqreturn_t tsl2591_event_handler(int irq, void *private)
987 {
988 struct iio_dev *dev_info = private;
989 struct tsl2591_chip *chip = iio_priv(dev_info);
990 struct i2c_client *client = chip->client;
991
992 if (!chip->events_enabled)
993 return IRQ_NONE;
994
995 iio_push_event(dev_info,
996 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
997 IIO_EV_TYPE_THRESH,
998 IIO_EV_DIR_EITHER),
999 iio_get_time_ns(dev_info));
1000
1001 /* Clear ALS irq */
1002 i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI);
1003
1004 return IRQ_HANDLED;
1005 }
1006
tsl2591_load_defaults(struct tsl2591_chip * chip)1007 static int tsl2591_load_defaults(struct tsl2591_chip *chip)
1008 {
1009 int ret;
1010
1011 chip->als_settings.als_int_time = TSL2591_DEFAULT_ALS_INT_TIME;
1012 chip->als_settings.als_gain = TSL2591_DEFAULT_ALS_GAIN;
1013 chip->als_settings.als_lower_thresh = TSL2591_DEFAULT_ALS_LOWER_THRESH;
1014 chip->als_settings.als_upper_thresh = TSL2591_DEFAULT_ALS_UPPER_THRESH;
1015
1016 ret = tsl2591_set_als_gain_int_time(chip);
1017 if (ret < 0)
1018 return ret;
1019
1020 ret = tsl2591_set_als_persist_cycle(chip, TSL2591_DEFAULT_ALS_PERSIST);
1021 if (ret < 0)
1022 return ret;
1023
1024 ret = tsl2591_set_als_lower_threshold(chip, TSL2591_DEFAULT_ALS_LOWER_THRESH);
1025 if (ret < 0)
1026 return ret;
1027
1028 ret = tsl2591_set_als_upper_threshold(chip, TSL2591_DEFAULT_ALS_UPPER_THRESH);
1029 if (ret < 0)
1030 return ret;
1031
1032 return 0;
1033 }
1034
tsl2591_chip_off(void * data)1035 static void tsl2591_chip_off(void *data)
1036 {
1037 struct iio_dev *indio_dev = data;
1038 struct tsl2591_chip *chip = iio_priv(indio_dev);
1039 struct i2c_client *client = chip->client;
1040
1041 pm_runtime_disable(&client->dev);
1042 pm_runtime_set_suspended(&client->dev);
1043 pm_runtime_put_noidle(&client->dev);
1044
1045 tsl2591_set_power_state(chip, TSL2591_PWR_OFF);
1046 }
1047
tsl2591_probe(struct i2c_client * client)1048 static int tsl2591_probe(struct i2c_client *client)
1049 {
1050 struct tsl2591_chip *chip;
1051 struct iio_dev *indio_dev;
1052 int ret;
1053
1054 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1055 dev_err(&client->dev,
1056 "I2C smbus byte data functionality is not supported\n");
1057 return -EOPNOTSUPP;
1058 }
1059
1060 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
1061 if (!indio_dev)
1062 return -ENOMEM;
1063
1064 chip = iio_priv(indio_dev);
1065 chip->client = client;
1066 i2c_set_clientdata(client, indio_dev);
1067
1068 if (client->irq) {
1069 ret = devm_request_threaded_irq(&client->dev, client->irq,
1070 NULL, tsl2591_event_handler,
1071 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1072 "tsl2591_irq", indio_dev);
1073 if (ret)
1074 return ret;
1075 indio_dev->info = &tsl2591_info;
1076 } else {
1077 indio_dev->info = &tsl2591_info_no_irq;
1078 }
1079
1080 mutex_init(&chip->als_mutex);
1081
1082 ret = i2c_smbus_read_byte_data(client,
1083 TSL2591_CMD_NOP | TSL2591_DEVICE_ID);
1084 if (ret < 0) {
1085 dev_err(&client->dev,
1086 "Failed to read the device ID register\n");
1087 return ret;
1088 }
1089 ret = FIELD_GET(TSL2591_DEVICE_ID_MASK, ret);
1090 if (ret != TSL2591_DEVICE_ID_VAL) {
1091 dev_err(&client->dev, "Device ID: %#04x unknown\n", ret);
1092 return -EINVAL;
1093 }
1094
1095 indio_dev->channels = tsl2591_channels;
1096 indio_dev->num_channels = ARRAY_SIZE(tsl2591_channels);
1097 indio_dev->modes = INDIO_DIRECT_MODE;
1098 indio_dev->name = chip->client->name;
1099 chip->events_enabled = false;
1100
1101 pm_runtime_enable(&client->dev);
1102 pm_runtime_set_autosuspend_delay(&client->dev,
1103 TSL2591_POWER_OFF_DELAY_MS);
1104 pm_runtime_use_autosuspend(&client->dev);
1105
1106 /*
1107 * Add chip off to automatically managed path and disable runtime
1108 * power management. This ensures that the chip power management
1109 * is handled correctly on driver remove. tsl2591_chip_off() must be
1110 * added to the managed path after pm runtime is enabled and before
1111 * any error exit paths are met to ensure we're not left in a state
1112 * of pm runtime not being disabled properly.
1113 */
1114 ret = devm_add_action_or_reset(&client->dev, tsl2591_chip_off,
1115 indio_dev);
1116 if (ret < 0)
1117 return -EINVAL;
1118
1119 ret = tsl2591_load_defaults(chip);
1120 if (ret < 0) {
1121 dev_err(&client->dev, "Failed to load sensor defaults\n");
1122 return -EINVAL;
1123 }
1124
1125 ret = i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI);
1126 if (ret < 0) {
1127 dev_err(&client->dev, "Failed to clear als irq\n");
1128 return -EINVAL;
1129 }
1130
1131 return devm_iio_device_register(&client->dev, indio_dev);
1132 }
1133
1134 static const struct of_device_id tsl2591_of_match[] = {
1135 { .compatible = "amstaos,tsl2591"},
1136 { }
1137 };
1138 MODULE_DEVICE_TABLE(of, tsl2591_of_match);
1139
1140 static struct i2c_driver tsl2591_driver = {
1141 .driver = {
1142 .name = "tsl2591",
1143 .pm = pm_ptr(&tsl2591_pm_ops),
1144 .of_match_table = tsl2591_of_match,
1145 },
1146 .probe = tsl2591_probe
1147 };
1148 module_i2c_driver(tsl2591_driver);
1149
1150 MODULE_AUTHOR("Joe Sandom <joe.g.sandom@gmail.com>");
1151 MODULE_DESCRIPTION("TAOS tsl2591 ambient light sensor driver");
1152 MODULE_LICENSE("GPL");
1153