1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Device driver for monitoring ambient light intensity (lux)
4 * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
6 * Copyright (c) 2011, TAOS Corporation.
7 * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
10 #include <linux/kernel.h>
11 #include <linux/i2c.h>
12 #include <linux/errno.h>
13 #include <linux/delay.h>
14 #include <linux/string.h>
15 #include <linux/mutex.h>
16 #include <linux/unistd.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/pm_runtime.h>
23 /* Device Registers and Masks */
24 #define TSL2583_CNTRL 0x00
25 #define TSL2583_ALS_TIME 0X01
26 #define TSL2583_INTERRUPT 0x02
27 #define TSL2583_GAIN 0x07
28 #define TSL2583_REVID 0x11
29 #define TSL2583_CHIPID 0x12
30 #define TSL2583_ALS_CHAN0LO 0x14
31 #define TSL2583_ALS_CHAN0HI 0x15
32 #define TSL2583_ALS_CHAN1LO 0x16
33 #define TSL2583_ALS_CHAN1HI 0x17
34 #define TSL2583_TMR_LO 0x18
35 #define TSL2583_TMR_HI 0x19
37 /* tsl2583 cmd reg masks */
38 #define TSL2583_CMD_REG 0x80
39 #define TSL2583_CMD_SPL_FN 0x60
40 #define TSL2583_CMD_ALS_INT_CLR 0x01
42 /* tsl2583 cntrl reg masks */
43 #define TSL2583_CNTL_ADC_ENBL 0x02
44 #define TSL2583_CNTL_PWR_OFF 0x00
45 #define TSL2583_CNTL_PWR_ON 0x01
47 /* tsl2583 status reg masks */
48 #define TSL2583_STA_ADC_VALID 0x01
49 #define TSL2583_STA_ADC_INTR 0x10
51 /* Lux calculation constants */
52 #define TSL2583_LUX_CALC_OVER_FLOW 65535
54 #define TSL2583_INTERRUPT_DISABLED 0x00
56 #define TSL2583_CHIP_ID 0x90
57 #define TSL2583_CHIP_ID_MASK 0xf0
59 #define TSL2583_POWER_OFF_DELAY_MS 2000
62 struct tsl2583_als_info
{
74 static const struct tsl2583_lux tsl2583_default_lux
[] = {
75 { 9830, 8520, 15729 },
76 { 12452, 10807, 23344 },
77 { 14746, 6383, 11705 },
78 { 17695, 4063, 6554 },
79 { 0, 0, 0 } /* Termination segment */
82 #define TSL2583_MAX_LUX_TABLE_ENTRIES 11
84 struct tsl2583_settings
{
91 * This structure is intentionally large to accommodate updates via
92 * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
93 * Assumption is that one and only one type of glass used.
95 struct tsl2583_lux als_device_lux
[TSL2583_MAX_LUX_TABLE_ENTRIES
];
99 struct mutex als_mutex
;
100 struct i2c_client
*client
;
101 struct tsl2583_als_info als_cur_info
;
102 struct tsl2583_settings als_settings
;
113 /* Index = (0 - 3) Used to validate the gain selection index */
114 static const struct gainadj gainadj
[] = {
122 * Provides initial operational parameter defaults.
123 * These defaults may be changed through the device's sysfs files.
125 static void tsl2583_defaults(struct tsl2583_chip
*chip
)
128 * The integration time must be a multiple of 50ms and within the
129 * range [50, 600] ms.
131 chip
->als_settings
.als_time
= 100;
134 * This is an index into the gainadj table. Assume clear glass as the
137 chip
->als_settings
.als_gain
= 0;
139 /* Default gain trim to account for aperture effects */
140 chip
->als_settings
.als_gain_trim
= 1000;
142 /* Known external ALS reading used for calibration */
143 chip
->als_settings
.als_cal_target
= 130;
145 /* Default lux table. */
146 memcpy(chip
->als_settings
.als_device_lux
, tsl2583_default_lux
,
147 sizeof(tsl2583_default_lux
));
151 * Reads and calculates current lux value.
152 * The raw ch0 and ch1 values of the ambient light sensed in the last
153 * integration cycle are read from the device.
154 * Time scale factor array values are adjusted based on the integration time.
155 * The raw values are multiplied by a scale factor, and device gain is obtained
156 * using gain index. Limit checks are done next, then the ratio of a multiple
157 * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
158 * declared above is then scanned to find the first ratio value that is just
159 * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
160 * the array are then used along with the time scale factor array values, to
163 static int tsl2583_get_lux(struct iio_dev
*indio_dev
)
165 u16 ch0
, ch1
; /* separated ch0/ch1 data from device */
166 u32 lux
; /* raw lux calculated from device data */
170 struct tsl2583_lux
*p
;
171 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
174 ret
= i2c_smbus_read_byte_data(chip
->client
, TSL2583_CMD_REG
);
176 dev_err(&chip
->client
->dev
, "%s: failed to read CMD_REG register\n",
181 /* is data new & valid */
182 if (!(ret
& TSL2583_STA_ADC_INTR
)) {
183 dev_err(&chip
->client
->dev
, "%s: data not valid; returning last value\n",
185 ret
= chip
->als_cur_info
.lux
; /* return LAST VALUE */
189 for (i
= 0; i
< 4; i
++) {
190 int reg
= TSL2583_CMD_REG
| (TSL2583_ALS_CHAN0LO
+ i
);
192 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
194 dev_err(&chip
->client
->dev
, "%s: failed to read register %x\n",
202 * Clear the pending interrupt status bit on the chip to allow the next
203 * integration cycle to start. This has to be done even though this
204 * driver currently does not support interrupts.
206 ret
= i2c_smbus_write_byte(chip
->client
,
207 (TSL2583_CMD_REG
| TSL2583_CMD_SPL_FN
|
208 TSL2583_CMD_ALS_INT_CLR
));
210 dev_err(&chip
->client
->dev
, "%s: failed to clear the interrupt bit\n",
212 goto done
; /* have no data, so return failure */
215 /* extract ALS/lux data */
216 ch0
= le16_to_cpup((const __le16
*)&buf
[0]);
217 ch1
= le16_to_cpup((const __le16
*)&buf
[2]);
219 chip
->als_cur_info
.als_ch0
= ch0
;
220 chip
->als_cur_info
.als_ch1
= ch1
;
222 if ((ch0
>= chip
->als_saturation
) || (ch1
>= chip
->als_saturation
))
227 * The sensor appears to be in total darkness so set the
228 * calculated lux to 0 and return early to avoid a division by
229 * zero below when calculating the ratio.
232 chip
->als_cur_info
.lux
= 0;
236 /* calculate ratio */
237 ratio
= (ch1
<< 15) / ch0
;
239 /* convert to unscaled lux using the pointer to the table */
240 for (p
= (struct tsl2583_lux
*)chip
->als_settings
.als_device_lux
;
241 p
->ratio
!= 0 && p
->ratio
< ratio
; p
++)
249 ch0lux
= ((ch0
* p
->ch0
) +
250 (gainadj
[chip
->als_settings
.als_gain
].ch0
>> 1))
251 / gainadj
[chip
->als_settings
.als_gain
].ch0
;
252 ch1lux
= ((ch1
* p
->ch1
) +
253 (gainadj
[chip
->als_settings
.als_gain
].ch1
>> 1))
254 / gainadj
[chip
->als_settings
.als_gain
].ch1
;
256 /* note: lux is 31 bit max at this point */
257 if (ch1lux
> ch0lux
) {
258 dev_dbg(&chip
->client
->dev
, "%s: No Data - Returning 0\n",
261 chip
->als_cur_info
.lux
= 0;
265 lux
= ch0lux
- ch1lux
;
268 /* adjust for active time scale */
269 if (chip
->als_time_scale
== 0)
272 lux
= (lux
+ (chip
->als_time_scale
>> 1)) /
273 chip
->als_time_scale
;
276 * Adjust for active gain scale.
277 * The tsl2583_default_lux tables above have a factor of 8192 built in,
278 * so we need to shift right.
279 * User-specified gain provides a multiplier.
280 * Apply user-specified gain before shifting right to retain precision.
281 * Use 64 bits to avoid overflow on multiplication.
282 * Then go back to 32 bits before division to avoid using div_u64().
285 lux64
= lux64
* chip
->als_settings
.als_gain_trim
;
288 lux
= (lux
+ 500) / 1000;
290 if (lux
> TSL2583_LUX_CALC_OVER_FLOW
) { /* check for overflow */
292 lux
= TSL2583_LUX_CALC_OVER_FLOW
;
295 /* Update the structure with the latest VALID lux. */
296 chip
->als_cur_info
.lux
= lux
;
304 * Obtain single reading and calculate the als_gain_trim (later used
305 * to derive actual lux).
306 * Return updated gain_trim value.
308 static int tsl2583_als_calibrate(struct iio_dev
*indio_dev
)
310 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
311 unsigned int gain_trim_val
;
315 ret
= i2c_smbus_read_byte_data(chip
->client
,
316 TSL2583_CMD_REG
| TSL2583_CNTRL
);
318 dev_err(&chip
->client
->dev
,
319 "%s: failed to read from the CNTRL register\n",
324 if ((ret
& (TSL2583_CNTL_ADC_ENBL
| TSL2583_CNTL_PWR_ON
))
325 != (TSL2583_CNTL_ADC_ENBL
| TSL2583_CNTL_PWR_ON
)) {
326 dev_err(&chip
->client
->dev
,
327 "%s: Device is not powered on and/or ADC is not enabled\n",
330 } else if ((ret
& TSL2583_STA_ADC_VALID
) != TSL2583_STA_ADC_VALID
) {
331 dev_err(&chip
->client
->dev
,
332 "%s: The two ADC channels have not completed an integration cycle\n",
337 lux_val
= tsl2583_get_lux(indio_dev
);
339 dev_err(&chip
->client
->dev
, "%s: failed to get lux\n",
344 gain_trim_val
= (unsigned int)(((chip
->als_settings
.als_cal_target
)
345 * chip
->als_settings
.als_gain_trim
) / lux_val
);
346 if ((gain_trim_val
< 250) || (gain_trim_val
> 4000)) {
347 dev_err(&chip
->client
->dev
,
348 "%s: trim_val of %d is not within the range [250, 4000]\n",
349 __func__
, gain_trim_val
);
353 chip
->als_settings
.als_gain_trim
= (int)gain_trim_val
;
358 static int tsl2583_set_als_time(struct tsl2583_chip
*chip
)
360 int als_count
, als_time
, ret
;
363 /* determine als integration register */
364 als_count
= (chip
->als_settings
.als_time
* 100 + 135) / 270;
366 als_count
= 1; /* ensure at least one cycle */
368 /* convert back to time (encompasses overrides) */
369 als_time
= (als_count
* 27 + 5) / 10;
371 val
= 256 - als_count
;
372 ret
= i2c_smbus_write_byte_data(chip
->client
,
373 TSL2583_CMD_REG
| TSL2583_ALS_TIME
,
376 dev_err(&chip
->client
->dev
, "%s: failed to set the als time to %d\n",
381 /* set chip struct re scaling and saturation */
382 chip
->als_saturation
= als_count
* 922; /* 90% of full scale */
383 chip
->als_time_scale
= (als_time
+ 25) / 50;
388 static int tsl2583_set_als_gain(struct tsl2583_chip
*chip
)
392 /* Set the gain based on als_settings struct */
393 ret
= i2c_smbus_write_byte_data(chip
->client
,
394 TSL2583_CMD_REG
| TSL2583_GAIN
,
395 chip
->als_settings
.als_gain
);
397 dev_err(&chip
->client
->dev
,
398 "%s: failed to set the gain to %d\n", __func__
,
399 chip
->als_settings
.als_gain
);
404 static int tsl2583_set_power_state(struct tsl2583_chip
*chip
, u8 state
)
408 ret
= i2c_smbus_write_byte_data(chip
->client
,
409 TSL2583_CMD_REG
| TSL2583_CNTRL
, state
);
411 dev_err(&chip
->client
->dev
,
412 "%s: failed to set the power state to %d\n", __func__
,
419 * Turn the device on.
420 * Configuration must be set before calling this function.
422 static int tsl2583_chip_init_and_power_on(struct iio_dev
*indio_dev
)
424 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
427 /* Power on the device; ADC off. */
428 ret
= tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_ON
);
432 ret
= i2c_smbus_write_byte_data(chip
->client
,
433 TSL2583_CMD_REG
| TSL2583_INTERRUPT
,
434 TSL2583_INTERRUPT_DISABLED
);
436 dev_err(&chip
->client
->dev
,
437 "%s: failed to disable interrupts\n", __func__
);
441 ret
= tsl2583_set_als_time(chip
);
445 ret
= tsl2583_set_als_gain(chip
);
449 usleep_range(3000, 3500);
451 ret
= tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_ON
|
452 TSL2583_CNTL_ADC_ENBL
);
459 /* Sysfs Interface Functions */
461 static ssize_t
in_illuminance_input_target_show(struct device
*dev
,
462 struct device_attribute
*attr
,
465 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
466 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
469 mutex_lock(&chip
->als_mutex
);
470 ret
= sprintf(buf
, "%d\n", chip
->als_settings
.als_cal_target
);
471 mutex_unlock(&chip
->als_mutex
);
476 static ssize_t
in_illuminance_input_target_store(struct device
*dev
,
477 struct device_attribute
*attr
,
478 const char *buf
, size_t len
)
480 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
481 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
484 if (kstrtoint(buf
, 0, &value
) || !value
)
487 mutex_lock(&chip
->als_mutex
);
488 chip
->als_settings
.als_cal_target
= value
;
489 mutex_unlock(&chip
->als_mutex
);
494 static ssize_t
in_illuminance_calibrate_store(struct device
*dev
,
495 struct device_attribute
*attr
,
496 const char *buf
, size_t len
)
498 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
499 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
502 if (kstrtoint(buf
, 0, &value
) || value
!= 1)
505 mutex_lock(&chip
->als_mutex
);
507 ret
= tsl2583_als_calibrate(indio_dev
);
513 mutex_unlock(&chip
->als_mutex
);
518 static ssize_t
in_illuminance_lux_table_show(struct device
*dev
,
519 struct device_attribute
*attr
,
522 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
523 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
527 for (i
= 0; i
< ARRAY_SIZE(chip
->als_settings
.als_device_lux
); i
++) {
528 offset
+= sprintf(buf
+ offset
, "%u,%u,%u,",
529 chip
->als_settings
.als_device_lux
[i
].ratio
,
530 chip
->als_settings
.als_device_lux
[i
].ch0
,
531 chip
->als_settings
.als_device_lux
[i
].ch1
);
532 if (chip
->als_settings
.als_device_lux
[i
].ratio
== 0) {
534 * We just printed the first "0" entry.
535 * Now get rid of the extra "," and break.
542 offset
+= sprintf(buf
+ offset
, "\n");
547 static ssize_t
in_illuminance_lux_table_store(struct device
*dev
,
548 struct device_attribute
*attr
,
549 const char *buf
, size_t len
)
551 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
552 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
553 const unsigned int max_ints
= TSL2583_MAX_LUX_TABLE_ENTRIES
* 3;
554 int value
[TSL2583_MAX_LUX_TABLE_ENTRIES
* 3 + 1];
558 mutex_lock(&chip
->als_mutex
);
560 get_options(buf
, ARRAY_SIZE(value
), value
);
563 * We now have an array of ints starting at value[1], and
564 * enumerated by value[0].
565 * We expect each group of three ints is one table entry,
566 * and the last table entry is all 0.
569 if ((n
% 3) || n
< 6 || n
> max_ints
) {
571 "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
575 if ((value
[n
- 2] | value
[n
- 1] | value
[n
]) != 0) {
576 dev_err(dev
, "%s: The last 3 entries in the lux table must be zeros.\n",
581 memcpy(chip
->als_settings
.als_device_lux
, &value
[1],
582 value
[0] * sizeof(value
[1]));
587 mutex_unlock(&chip
->als_mutex
);
592 static IIO_CONST_ATTR(in_illuminance_calibscale_available
, "1 8 16 111");
593 static IIO_CONST_ATTR(in_illuminance_integration_time_available
,
594 "0.050 0.100 0.150 0.200 0.250 0.300 0.350 0.400 0.450 0.500 0.550 0.600 0.650");
595 static IIO_DEVICE_ATTR_RW(in_illuminance_input_target
, 0);
596 static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate
, 0);
597 static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table
, 0);
599 static struct attribute
*sysfs_attrs_ctrl
[] = {
600 &iio_const_attr_in_illuminance_calibscale_available
.dev_attr
.attr
,
601 &iio_const_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
602 &iio_dev_attr_in_illuminance_input_target
.dev_attr
.attr
,
603 &iio_dev_attr_in_illuminance_calibrate
.dev_attr
.attr
,
604 &iio_dev_attr_in_illuminance_lux_table
.dev_attr
.attr
,
608 static const struct attribute_group tsl2583_attribute_group
= {
609 .attrs
= sysfs_attrs_ctrl
,
612 static const struct iio_chan_spec tsl2583_channels
[] = {
616 .channel2
= IIO_MOD_LIGHT_IR
,
617 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
622 .channel2
= IIO_MOD_LIGHT_BOTH
,
623 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
627 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
628 BIT(IIO_CHAN_INFO_CALIBBIAS
) |
629 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
630 BIT(IIO_CHAN_INFO_INT_TIME
),
634 static int tsl2583_set_pm_runtime_busy(struct tsl2583_chip
*chip
, bool on
)
639 ret
= pm_runtime_get_sync(&chip
->client
->dev
);
641 pm_runtime_put_noidle(&chip
->client
->dev
);
643 pm_runtime_mark_last_busy(&chip
->client
->dev
);
644 ret
= pm_runtime_put_autosuspend(&chip
->client
->dev
);
650 static int tsl2583_read_raw(struct iio_dev
*indio_dev
,
651 struct iio_chan_spec
const *chan
,
652 int *val
, int *val2
, long mask
)
654 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
657 ret
= tsl2583_set_pm_runtime_busy(chip
, true);
661 mutex_lock(&chip
->als_mutex
);
665 case IIO_CHAN_INFO_RAW
:
666 if (chan
->type
== IIO_LIGHT
) {
667 ret
= tsl2583_get_lux(indio_dev
);
672 * From page 20 of the TSL2581, TSL2583 data
673 * sheet (TAOS134 − MARCH 2011):
675 * One of the photodiodes (channel 0) is
676 * sensitive to both visible and infrared light,
677 * while the second photodiode (channel 1) is
678 * sensitive primarily to infrared light.
680 if (chan
->channel2
== IIO_MOD_LIGHT_BOTH
)
681 *val
= chip
->als_cur_info
.als_ch0
;
683 *val
= chip
->als_cur_info
.als_ch1
;
688 case IIO_CHAN_INFO_PROCESSED
:
689 if (chan
->type
== IIO_LIGHT
) {
690 ret
= tsl2583_get_lux(indio_dev
);
698 case IIO_CHAN_INFO_CALIBBIAS
:
699 if (chan
->type
== IIO_LIGHT
) {
700 *val
= chip
->als_settings
.als_gain_trim
;
704 case IIO_CHAN_INFO_CALIBSCALE
:
705 if (chan
->type
== IIO_LIGHT
) {
706 *val
= gainadj
[chip
->als_settings
.als_gain
].mean
;
710 case IIO_CHAN_INFO_INT_TIME
:
711 if (chan
->type
== IIO_LIGHT
) {
713 *val2
= chip
->als_settings
.als_time
;
714 ret
= IIO_VAL_INT_PLUS_MICRO
;
722 mutex_unlock(&chip
->als_mutex
);
728 * Preserve the ret variable if the call to
729 * tsl2583_set_pm_runtime_busy() is successful so the reading
730 * (if applicable) is returned to user space.
732 pm_ret
= tsl2583_set_pm_runtime_busy(chip
, false);
739 static int tsl2583_write_raw(struct iio_dev
*indio_dev
,
740 struct iio_chan_spec
const *chan
,
741 int val
, int val2
, long mask
)
743 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
746 ret
= tsl2583_set_pm_runtime_busy(chip
, true);
750 mutex_lock(&chip
->als_mutex
);
754 case IIO_CHAN_INFO_CALIBBIAS
:
755 if (chan
->type
== IIO_LIGHT
) {
756 chip
->als_settings
.als_gain_trim
= val
;
760 case IIO_CHAN_INFO_CALIBSCALE
:
761 if (chan
->type
== IIO_LIGHT
) {
764 for (i
= 0; i
< ARRAY_SIZE(gainadj
); i
++) {
765 if (gainadj
[i
].mean
== val
) {
766 chip
->als_settings
.als_gain
= i
;
767 ret
= tsl2583_set_als_gain(chip
);
773 case IIO_CHAN_INFO_INT_TIME
:
774 if (chan
->type
== IIO_LIGHT
&& !val
&& val2
>= 50 &&
775 val2
<= 650 && !(val2
% 50)) {
776 chip
->als_settings
.als_time
= val2
;
777 ret
= tsl2583_set_als_time(chip
);
784 mutex_unlock(&chip
->als_mutex
);
789 ret
= tsl2583_set_pm_runtime_busy(chip
, false);
796 static const struct iio_info tsl2583_info
= {
797 .attrs
= &tsl2583_attribute_group
,
798 .read_raw
= tsl2583_read_raw
,
799 .write_raw
= tsl2583_write_raw
,
802 static int tsl2583_probe(struct i2c_client
*clientp
,
803 const struct i2c_device_id
*idp
)
806 struct tsl2583_chip
*chip
;
807 struct iio_dev
*indio_dev
;
809 if (!i2c_check_functionality(clientp
->adapter
,
810 I2C_FUNC_SMBUS_BYTE_DATA
)) {
811 dev_err(&clientp
->dev
, "%s: i2c smbus byte data functionality is unsupported\n",
816 indio_dev
= devm_iio_device_alloc(&clientp
->dev
, sizeof(*chip
));
820 chip
= iio_priv(indio_dev
);
821 chip
->client
= clientp
;
822 i2c_set_clientdata(clientp
, indio_dev
);
824 mutex_init(&chip
->als_mutex
);
826 ret
= i2c_smbus_read_byte_data(clientp
,
827 TSL2583_CMD_REG
| TSL2583_CHIPID
);
829 dev_err(&clientp
->dev
,
830 "%s: failed to read the chip ID register\n", __func__
);
834 if ((ret
& TSL2583_CHIP_ID_MASK
) != TSL2583_CHIP_ID
) {
835 dev_err(&clientp
->dev
, "%s: received an unknown chip ID %x\n",
840 indio_dev
->info
= &tsl2583_info
;
841 indio_dev
->channels
= tsl2583_channels
;
842 indio_dev
->num_channels
= ARRAY_SIZE(tsl2583_channels
);
843 indio_dev
->modes
= INDIO_DIRECT_MODE
;
844 indio_dev
->name
= chip
->client
->name
;
846 pm_runtime_enable(&clientp
->dev
);
847 pm_runtime_set_autosuspend_delay(&clientp
->dev
,
848 TSL2583_POWER_OFF_DELAY_MS
);
849 pm_runtime_use_autosuspend(&clientp
->dev
);
851 ret
= devm_iio_device_register(indio_dev
->dev
.parent
, indio_dev
);
853 dev_err(&clientp
->dev
, "%s: iio registration failed\n",
858 /* Load up the V2 defaults (these are hard coded defaults for now) */
859 tsl2583_defaults(chip
);
861 dev_info(&clientp
->dev
, "Light sensor found.\n");
866 static int tsl2583_remove(struct i2c_client
*client
)
868 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
869 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
871 iio_device_unregister(indio_dev
);
873 pm_runtime_disable(&client
->dev
);
874 pm_runtime_set_suspended(&client
->dev
);
875 pm_runtime_put_noidle(&client
->dev
);
877 return tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_OFF
);
880 static int __maybe_unused
tsl2583_suspend(struct device
*dev
)
882 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
883 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
886 mutex_lock(&chip
->als_mutex
);
888 ret
= tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_OFF
);
890 mutex_unlock(&chip
->als_mutex
);
895 static int __maybe_unused
tsl2583_resume(struct device
*dev
)
897 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
898 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
901 mutex_lock(&chip
->als_mutex
);
903 ret
= tsl2583_chip_init_and_power_on(indio_dev
);
905 mutex_unlock(&chip
->als_mutex
);
910 static const struct dev_pm_ops tsl2583_pm_ops
= {
911 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
912 pm_runtime_force_resume
)
913 SET_RUNTIME_PM_OPS(tsl2583_suspend
, tsl2583_resume
, NULL
)
916 static const struct i2c_device_id tsl2583_idtable
[] = {
922 MODULE_DEVICE_TABLE(i2c
, tsl2583_idtable
);
924 static const struct of_device_id tsl2583_of_match
[] = {
925 { .compatible
= "amstaos,tsl2580", },
926 { .compatible
= "amstaos,tsl2581", },
927 { .compatible
= "amstaos,tsl2583", },
930 MODULE_DEVICE_TABLE(of
, tsl2583_of_match
);
932 /* Driver definition */
933 static struct i2c_driver tsl2583_driver
= {
936 .pm
= &tsl2583_pm_ops
,
937 .of_match_table
= tsl2583_of_match
,
939 .id_table
= tsl2583_idtable
,
940 .probe
= tsl2583_probe
,
941 .remove
= tsl2583_remove
,
943 module_i2c_driver(tsl2583_driver
);
945 MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
946 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
947 MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
948 MODULE_LICENSE("GPL");