2 * Device driver for monitoring ambient light intensity (lux)
3 * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
5 * Copyright (c) 2011, TAOS Corporation.
6 * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
21 #include <linux/errno.h>
22 #include <linux/delay.h>
23 #include <linux/string.h>
24 #include <linux/mutex.h>
25 #include <linux/unistd.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/pm_runtime.h>
32 /* Device Registers and Masks */
33 #define TSL2583_CNTRL 0x00
34 #define TSL2583_ALS_TIME 0X01
35 #define TSL2583_INTERRUPT 0x02
36 #define TSL2583_GAIN 0x07
37 #define TSL2583_REVID 0x11
38 #define TSL2583_CHIPID 0x12
39 #define TSL2583_ALS_CHAN0LO 0x14
40 #define TSL2583_ALS_CHAN0HI 0x15
41 #define TSL2583_ALS_CHAN1LO 0x16
42 #define TSL2583_ALS_CHAN1HI 0x17
43 #define TSL2583_TMR_LO 0x18
44 #define TSL2583_TMR_HI 0x19
46 /* tsl2583 cmd reg masks */
47 #define TSL2583_CMD_REG 0x80
48 #define TSL2583_CMD_SPL_FN 0x60
49 #define TSL2583_CMD_ALS_INT_CLR 0x01
51 /* tsl2583 cntrl reg masks */
52 #define TSL2583_CNTL_ADC_ENBL 0x02
53 #define TSL2583_CNTL_PWR_OFF 0x00
54 #define TSL2583_CNTL_PWR_ON 0x01
56 /* tsl2583 status reg masks */
57 #define TSL2583_STA_ADC_VALID 0x01
58 #define TSL2583_STA_ADC_INTR 0x10
60 /* Lux calculation constants */
61 #define TSL2583_LUX_CALC_OVER_FLOW 65535
63 #define TSL2583_INTERRUPT_DISABLED 0x00
65 #define TSL2583_CHIP_ID 0x90
66 #define TSL2583_CHIP_ID_MASK 0xf0
68 #define TSL2583_POWER_OFF_DELAY_MS 2000
71 struct tsl2583_als_info
{
83 static const struct tsl2583_lux tsl2583_default_lux
[] = {
84 { 9830, 8520, 15729 },
85 { 12452, 10807, 23344 },
86 { 14746, 6383, 11705 },
87 { 17695, 4063, 6554 },
88 { 0, 0, 0 } /* Termination segment */
91 #define TSL2583_MAX_LUX_TABLE_ENTRIES 11
93 struct tsl2583_settings
{
100 * This structure is intentionally large to accommodate updates via
101 * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
102 * Assumption is that one and only one type of glass used.
104 struct tsl2583_lux als_device_lux
[TSL2583_MAX_LUX_TABLE_ENTRIES
];
107 struct tsl2583_chip
{
108 struct mutex als_mutex
;
109 struct i2c_client
*client
;
110 struct tsl2583_als_info als_cur_info
;
111 struct tsl2583_settings als_settings
;
122 /* Index = (0 - 3) Used to validate the gain selection index */
123 static const struct gainadj gainadj
[] = {
131 * Provides initial operational parameter defaults.
132 * These defaults may be changed through the device's sysfs files.
134 static void tsl2583_defaults(struct tsl2583_chip
*chip
)
137 * The integration time must be a multiple of 50ms and within the
138 * range [50, 600] ms.
140 chip
->als_settings
.als_time
= 100;
143 * This is an index into the gainadj table. Assume clear glass as the
146 chip
->als_settings
.als_gain
= 0;
148 /* Default gain trim to account for aperture effects */
149 chip
->als_settings
.als_gain_trim
= 1000;
151 /* Known external ALS reading used for calibration */
152 chip
->als_settings
.als_cal_target
= 130;
154 /* Default lux table. */
155 memcpy(chip
->als_settings
.als_device_lux
, tsl2583_default_lux
,
156 sizeof(tsl2583_default_lux
));
160 * Reads and calculates current lux value.
161 * The raw ch0 and ch1 values of the ambient light sensed in the last
162 * integration cycle are read from the device.
163 * Time scale factor array values are adjusted based on the integration time.
164 * The raw values are multiplied by a scale factor, and device gain is obtained
165 * using gain index. Limit checks are done next, then the ratio of a multiple
166 * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
167 * declared above is then scanned to find the first ratio value that is just
168 * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
169 * the array are then used along with the time scale factor array values, to
172 static int tsl2583_get_lux(struct iio_dev
*indio_dev
)
174 u16 ch0
, ch1
; /* separated ch0/ch1 data from device */
175 u32 lux
; /* raw lux calculated from device data */
179 struct tsl2583_lux
*p
;
180 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
183 ret
= i2c_smbus_read_byte_data(chip
->client
, TSL2583_CMD_REG
);
185 dev_err(&chip
->client
->dev
, "%s: failed to read CMD_REG register\n",
190 /* is data new & valid */
191 if (!(ret
& TSL2583_STA_ADC_INTR
)) {
192 dev_err(&chip
->client
->dev
, "%s: data not valid; returning last value\n",
194 ret
= chip
->als_cur_info
.lux
; /* return LAST VALUE */
198 for (i
= 0; i
< 4; i
++) {
199 int reg
= TSL2583_CMD_REG
| (TSL2583_ALS_CHAN0LO
+ i
);
201 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
203 dev_err(&chip
->client
->dev
, "%s: failed to read register %x\n",
211 * Clear the pending interrupt status bit on the chip to allow the next
212 * integration cycle to start. This has to be done even though this
213 * driver currently does not support interrupts.
215 ret
= i2c_smbus_write_byte(chip
->client
,
216 (TSL2583_CMD_REG
| TSL2583_CMD_SPL_FN
|
217 TSL2583_CMD_ALS_INT_CLR
));
219 dev_err(&chip
->client
->dev
, "%s: failed to clear the interrupt bit\n",
221 goto done
; /* have no data, so return failure */
224 /* extract ALS/lux data */
225 ch0
= le16_to_cpup((const __le16
*)&buf
[0]);
226 ch1
= le16_to_cpup((const __le16
*)&buf
[2]);
228 chip
->als_cur_info
.als_ch0
= ch0
;
229 chip
->als_cur_info
.als_ch1
= ch1
;
231 if ((ch0
>= chip
->als_saturation
) || (ch1
>= chip
->als_saturation
))
236 * The sensor appears to be in total darkness so set the
237 * calculated lux to 0 and return early to avoid a division by
238 * zero below when calculating the ratio.
241 chip
->als_cur_info
.lux
= 0;
245 /* calculate ratio */
246 ratio
= (ch1
<< 15) / ch0
;
248 /* convert to unscaled lux using the pointer to the table */
249 for (p
= (struct tsl2583_lux
*)chip
->als_settings
.als_device_lux
;
250 p
->ratio
!= 0 && p
->ratio
< ratio
; p
++)
258 ch0lux
= ((ch0
* p
->ch0
) +
259 (gainadj
[chip
->als_settings
.als_gain
].ch0
>> 1))
260 / gainadj
[chip
->als_settings
.als_gain
].ch0
;
261 ch1lux
= ((ch1
* p
->ch1
) +
262 (gainadj
[chip
->als_settings
.als_gain
].ch1
>> 1))
263 / gainadj
[chip
->als_settings
.als_gain
].ch1
;
265 /* note: lux is 31 bit max at this point */
266 if (ch1lux
> ch0lux
) {
267 dev_dbg(&chip
->client
->dev
, "%s: No Data - Returning 0\n",
270 chip
->als_cur_info
.lux
= 0;
274 lux
= ch0lux
- ch1lux
;
277 /* adjust for active time scale */
278 if (chip
->als_time_scale
== 0)
281 lux
= (lux
+ (chip
->als_time_scale
>> 1)) /
282 chip
->als_time_scale
;
285 * Adjust for active gain scale.
286 * The tsl2583_default_lux tables above have a factor of 8192 built in,
287 * so we need to shift right.
288 * User-specified gain provides a multiplier.
289 * Apply user-specified gain before shifting right to retain precision.
290 * Use 64 bits to avoid overflow on multiplication.
291 * Then go back to 32 bits before division to avoid using div_u64().
294 lux64
= lux64
* chip
->als_settings
.als_gain_trim
;
297 lux
= (lux
+ 500) / 1000;
299 if (lux
> TSL2583_LUX_CALC_OVER_FLOW
) { /* check for overflow */
301 lux
= TSL2583_LUX_CALC_OVER_FLOW
;
304 /* Update the structure with the latest VALID lux. */
305 chip
->als_cur_info
.lux
= lux
;
313 * Obtain single reading and calculate the als_gain_trim (later used
314 * to derive actual lux).
315 * Return updated gain_trim value.
317 static int tsl2583_als_calibrate(struct iio_dev
*indio_dev
)
319 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
320 unsigned int gain_trim_val
;
324 ret
= i2c_smbus_read_byte_data(chip
->client
,
325 TSL2583_CMD_REG
| TSL2583_CNTRL
);
327 dev_err(&chip
->client
->dev
,
328 "%s: failed to read from the CNTRL register\n",
333 if ((ret
& (TSL2583_CNTL_ADC_ENBL
| TSL2583_CNTL_PWR_ON
))
334 != (TSL2583_CNTL_ADC_ENBL
| TSL2583_CNTL_PWR_ON
)) {
335 dev_err(&chip
->client
->dev
,
336 "%s: Device is not powered on and/or ADC is not enabled\n",
339 } else if ((ret
& TSL2583_STA_ADC_VALID
) != TSL2583_STA_ADC_VALID
) {
340 dev_err(&chip
->client
->dev
,
341 "%s: The two ADC channels have not completed an integration cycle\n",
346 lux_val
= tsl2583_get_lux(indio_dev
);
348 dev_err(&chip
->client
->dev
, "%s: failed to get lux\n",
353 gain_trim_val
= (unsigned int)(((chip
->als_settings
.als_cal_target
)
354 * chip
->als_settings
.als_gain_trim
) / lux_val
);
355 if ((gain_trim_val
< 250) || (gain_trim_val
> 4000)) {
356 dev_err(&chip
->client
->dev
,
357 "%s: trim_val of %d is not within the range [250, 4000]\n",
358 __func__
, gain_trim_val
);
362 chip
->als_settings
.als_gain_trim
= (int)gain_trim_val
;
367 static int tsl2583_set_als_time(struct tsl2583_chip
*chip
)
369 int als_count
, als_time
, ret
;
372 /* determine als integration register */
373 als_count
= (chip
->als_settings
.als_time
* 100 + 135) / 270;
375 als_count
= 1; /* ensure at least one cycle */
377 /* convert back to time (encompasses overrides) */
378 als_time
= (als_count
* 27 + 5) / 10;
380 val
= 256 - als_count
;
381 ret
= i2c_smbus_write_byte_data(chip
->client
,
382 TSL2583_CMD_REG
| TSL2583_ALS_TIME
,
385 dev_err(&chip
->client
->dev
, "%s: failed to set the als time to %d\n",
390 /* set chip struct re scaling and saturation */
391 chip
->als_saturation
= als_count
* 922; /* 90% of full scale */
392 chip
->als_time_scale
= (als_time
+ 25) / 50;
397 static int tsl2583_set_als_gain(struct tsl2583_chip
*chip
)
401 /* Set the gain based on als_settings struct */
402 ret
= i2c_smbus_write_byte_data(chip
->client
,
403 TSL2583_CMD_REG
| TSL2583_GAIN
,
404 chip
->als_settings
.als_gain
);
406 dev_err(&chip
->client
->dev
,
407 "%s: failed to set the gain to %d\n", __func__
,
408 chip
->als_settings
.als_gain
);
413 static int tsl2583_set_power_state(struct tsl2583_chip
*chip
, u8 state
)
417 ret
= i2c_smbus_write_byte_data(chip
->client
,
418 TSL2583_CMD_REG
| TSL2583_CNTRL
, state
);
420 dev_err(&chip
->client
->dev
,
421 "%s: failed to set the power state to %d\n", __func__
,
428 * Turn the device on.
429 * Configuration must be set before calling this function.
431 static int tsl2583_chip_init_and_power_on(struct iio_dev
*indio_dev
)
433 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
436 /* Power on the device; ADC off. */
437 ret
= tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_ON
);
441 ret
= i2c_smbus_write_byte_data(chip
->client
,
442 TSL2583_CMD_REG
| TSL2583_INTERRUPT
,
443 TSL2583_INTERRUPT_DISABLED
);
445 dev_err(&chip
->client
->dev
,
446 "%s: failed to disable interrupts\n", __func__
);
450 ret
= tsl2583_set_als_time(chip
);
454 ret
= tsl2583_set_als_gain(chip
);
458 usleep_range(3000, 3500);
460 ret
= tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_ON
|
461 TSL2583_CNTL_ADC_ENBL
);
468 /* Sysfs Interface Functions */
470 static ssize_t
in_illuminance_input_target_show(struct device
*dev
,
471 struct device_attribute
*attr
,
474 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
475 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
478 mutex_lock(&chip
->als_mutex
);
479 ret
= sprintf(buf
, "%d\n", chip
->als_settings
.als_cal_target
);
480 mutex_unlock(&chip
->als_mutex
);
485 static ssize_t
in_illuminance_input_target_store(struct device
*dev
,
486 struct device_attribute
*attr
,
487 const char *buf
, size_t len
)
489 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
490 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
493 if (kstrtoint(buf
, 0, &value
) || !value
)
496 mutex_lock(&chip
->als_mutex
);
497 chip
->als_settings
.als_cal_target
= value
;
498 mutex_unlock(&chip
->als_mutex
);
503 static ssize_t
in_illuminance_calibrate_store(struct device
*dev
,
504 struct device_attribute
*attr
,
505 const char *buf
, size_t len
)
507 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
508 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
511 if (kstrtoint(buf
, 0, &value
) || value
!= 1)
514 mutex_lock(&chip
->als_mutex
);
516 ret
= tsl2583_als_calibrate(indio_dev
);
522 mutex_unlock(&chip
->als_mutex
);
527 static ssize_t
in_illuminance_lux_table_show(struct device
*dev
,
528 struct device_attribute
*attr
,
531 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
532 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
536 for (i
= 0; i
< ARRAY_SIZE(chip
->als_settings
.als_device_lux
); i
++) {
537 offset
+= sprintf(buf
+ offset
, "%u,%u,%u,",
538 chip
->als_settings
.als_device_lux
[i
].ratio
,
539 chip
->als_settings
.als_device_lux
[i
].ch0
,
540 chip
->als_settings
.als_device_lux
[i
].ch1
);
541 if (chip
->als_settings
.als_device_lux
[i
].ratio
== 0) {
543 * We just printed the first "0" entry.
544 * Now get rid of the extra "," and break.
551 offset
+= sprintf(buf
+ offset
, "\n");
556 static ssize_t
in_illuminance_lux_table_store(struct device
*dev
,
557 struct device_attribute
*attr
,
558 const char *buf
, size_t len
)
560 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
561 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
562 const unsigned int max_ints
= TSL2583_MAX_LUX_TABLE_ENTRIES
* 3;
563 int value
[TSL2583_MAX_LUX_TABLE_ENTRIES
* 3 + 1];
567 mutex_lock(&chip
->als_mutex
);
569 get_options(buf
, ARRAY_SIZE(value
), value
);
572 * We now have an array of ints starting at value[1], and
573 * enumerated by value[0].
574 * We expect each group of three ints is one table entry,
575 * and the last table entry is all 0.
578 if ((n
% 3) || n
< 6 || n
> max_ints
) {
580 "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
584 if ((value
[n
- 2] | value
[n
- 1] | value
[n
]) != 0) {
585 dev_err(dev
, "%s: The last 3 entries in the lux table must be zeros.\n",
590 memcpy(chip
->als_settings
.als_device_lux
, &value
[1],
591 value
[0] * sizeof(value
[1]));
596 mutex_unlock(&chip
->als_mutex
);
601 static IIO_CONST_ATTR(in_illuminance_calibscale_available
, "1 8 16 111");
602 static IIO_CONST_ATTR(in_illuminance_integration_time_available
,
603 "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");
604 static IIO_DEVICE_ATTR_RW(in_illuminance_input_target
, 0);
605 static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate
, 0);
606 static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table
, 0);
608 static struct attribute
*sysfs_attrs_ctrl
[] = {
609 &iio_const_attr_in_illuminance_calibscale_available
.dev_attr
.attr
,
610 &iio_const_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
611 &iio_dev_attr_in_illuminance_input_target
.dev_attr
.attr
,
612 &iio_dev_attr_in_illuminance_calibrate
.dev_attr
.attr
,
613 &iio_dev_attr_in_illuminance_lux_table
.dev_attr
.attr
,
617 static const struct attribute_group tsl2583_attribute_group
= {
618 .attrs
= sysfs_attrs_ctrl
,
621 static const struct iio_chan_spec tsl2583_channels
[] = {
625 .channel2
= IIO_MOD_LIGHT_IR
,
626 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
631 .channel2
= IIO_MOD_LIGHT_BOTH
,
632 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
636 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
637 BIT(IIO_CHAN_INFO_CALIBBIAS
) |
638 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
639 BIT(IIO_CHAN_INFO_INT_TIME
),
643 static int tsl2583_set_pm_runtime_busy(struct tsl2583_chip
*chip
, bool on
)
648 ret
= pm_runtime_get_sync(&chip
->client
->dev
);
650 pm_runtime_put_noidle(&chip
->client
->dev
);
652 pm_runtime_mark_last_busy(&chip
->client
->dev
);
653 ret
= pm_runtime_put_autosuspend(&chip
->client
->dev
);
659 static int tsl2583_read_raw(struct iio_dev
*indio_dev
,
660 struct iio_chan_spec
const *chan
,
661 int *val
, int *val2
, long mask
)
663 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
666 ret
= tsl2583_set_pm_runtime_busy(chip
, true);
670 mutex_lock(&chip
->als_mutex
);
674 case IIO_CHAN_INFO_RAW
:
675 if (chan
->type
== IIO_LIGHT
) {
676 ret
= tsl2583_get_lux(indio_dev
);
681 * From page 20 of the TSL2581, TSL2583 data
682 * sheet (TAOS134 − MARCH 2011):
684 * One of the photodiodes (channel 0) is
685 * sensitive to both visible and infrared light,
686 * while the second photodiode (channel 1) is
687 * sensitive primarily to infrared light.
689 if (chan
->channel2
== IIO_MOD_LIGHT_BOTH
)
690 *val
= chip
->als_cur_info
.als_ch0
;
692 *val
= chip
->als_cur_info
.als_ch1
;
697 case IIO_CHAN_INFO_PROCESSED
:
698 if (chan
->type
== IIO_LIGHT
) {
699 ret
= tsl2583_get_lux(indio_dev
);
707 case IIO_CHAN_INFO_CALIBBIAS
:
708 if (chan
->type
== IIO_LIGHT
) {
709 *val
= chip
->als_settings
.als_gain_trim
;
713 case IIO_CHAN_INFO_CALIBSCALE
:
714 if (chan
->type
== IIO_LIGHT
) {
715 *val
= gainadj
[chip
->als_settings
.als_gain
].mean
;
719 case IIO_CHAN_INFO_INT_TIME
:
720 if (chan
->type
== IIO_LIGHT
) {
722 *val2
= chip
->als_settings
.als_time
;
723 ret
= IIO_VAL_INT_PLUS_MICRO
;
731 mutex_unlock(&chip
->als_mutex
);
737 * Preserve the ret variable if the call to
738 * tsl2583_set_pm_runtime_busy() is successful so the reading
739 * (if applicable) is returned to user space.
741 pm_ret
= tsl2583_set_pm_runtime_busy(chip
, false);
748 static int tsl2583_write_raw(struct iio_dev
*indio_dev
,
749 struct iio_chan_spec
const *chan
,
750 int val
, int val2
, long mask
)
752 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
755 ret
= tsl2583_set_pm_runtime_busy(chip
, true);
759 mutex_lock(&chip
->als_mutex
);
763 case IIO_CHAN_INFO_CALIBBIAS
:
764 if (chan
->type
== IIO_LIGHT
) {
765 chip
->als_settings
.als_gain_trim
= val
;
769 case IIO_CHAN_INFO_CALIBSCALE
:
770 if (chan
->type
== IIO_LIGHT
) {
773 for (i
= 0; i
< ARRAY_SIZE(gainadj
); i
++) {
774 if (gainadj
[i
].mean
== val
) {
775 chip
->als_settings
.als_gain
= i
;
776 ret
= tsl2583_set_als_gain(chip
);
782 case IIO_CHAN_INFO_INT_TIME
:
783 if (chan
->type
== IIO_LIGHT
&& !val
&& val2
>= 50 &&
784 val2
<= 650 && !(val2
% 50)) {
785 chip
->als_settings
.als_time
= val2
;
786 ret
= tsl2583_set_als_time(chip
);
793 mutex_unlock(&chip
->als_mutex
);
798 ret
= tsl2583_set_pm_runtime_busy(chip
, false);
805 static const struct iio_info tsl2583_info
= {
806 .attrs
= &tsl2583_attribute_group
,
807 .read_raw
= tsl2583_read_raw
,
808 .write_raw
= tsl2583_write_raw
,
811 static int tsl2583_probe(struct i2c_client
*clientp
,
812 const struct i2c_device_id
*idp
)
815 struct tsl2583_chip
*chip
;
816 struct iio_dev
*indio_dev
;
818 if (!i2c_check_functionality(clientp
->adapter
,
819 I2C_FUNC_SMBUS_BYTE_DATA
)) {
820 dev_err(&clientp
->dev
, "%s: i2c smbus byte data functionality is unsupported\n",
825 indio_dev
= devm_iio_device_alloc(&clientp
->dev
, sizeof(*chip
));
829 chip
= iio_priv(indio_dev
);
830 chip
->client
= clientp
;
831 i2c_set_clientdata(clientp
, indio_dev
);
833 mutex_init(&chip
->als_mutex
);
835 ret
= i2c_smbus_read_byte_data(clientp
,
836 TSL2583_CMD_REG
| TSL2583_CHIPID
);
838 dev_err(&clientp
->dev
,
839 "%s: failed to read the chip ID register\n", __func__
);
843 if ((ret
& TSL2583_CHIP_ID_MASK
) != TSL2583_CHIP_ID
) {
844 dev_err(&clientp
->dev
, "%s: received an unknown chip ID %x\n",
849 indio_dev
->info
= &tsl2583_info
;
850 indio_dev
->channels
= tsl2583_channels
;
851 indio_dev
->num_channels
= ARRAY_SIZE(tsl2583_channels
);
852 indio_dev
->dev
.parent
= &clientp
->dev
;
853 indio_dev
->modes
= INDIO_DIRECT_MODE
;
854 indio_dev
->name
= chip
->client
->name
;
856 pm_runtime_enable(&clientp
->dev
);
857 pm_runtime_set_autosuspend_delay(&clientp
->dev
,
858 TSL2583_POWER_OFF_DELAY_MS
);
859 pm_runtime_use_autosuspend(&clientp
->dev
);
861 ret
= devm_iio_device_register(indio_dev
->dev
.parent
, indio_dev
);
863 dev_err(&clientp
->dev
, "%s: iio registration failed\n",
868 /* Load up the V2 defaults (these are hard coded defaults for now) */
869 tsl2583_defaults(chip
);
871 dev_info(&clientp
->dev
, "Light sensor found.\n");
876 static int tsl2583_remove(struct i2c_client
*client
)
878 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
879 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
881 iio_device_unregister(indio_dev
);
883 pm_runtime_disable(&client
->dev
);
884 pm_runtime_set_suspended(&client
->dev
);
885 pm_runtime_put_noidle(&client
->dev
);
887 return tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_OFF
);
890 static int __maybe_unused
tsl2583_suspend(struct device
*dev
)
892 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
893 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
896 mutex_lock(&chip
->als_mutex
);
898 ret
= tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_OFF
);
900 mutex_unlock(&chip
->als_mutex
);
905 static int __maybe_unused
tsl2583_resume(struct device
*dev
)
907 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
908 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
911 mutex_lock(&chip
->als_mutex
);
913 ret
= tsl2583_chip_init_and_power_on(indio_dev
);
915 mutex_unlock(&chip
->als_mutex
);
920 static const struct dev_pm_ops tsl2583_pm_ops
= {
921 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
922 pm_runtime_force_resume
)
923 SET_RUNTIME_PM_OPS(tsl2583_suspend
, tsl2583_resume
, NULL
)
926 static const struct i2c_device_id tsl2583_idtable
[] = {
932 MODULE_DEVICE_TABLE(i2c
, tsl2583_idtable
);
934 static const struct of_device_id tsl2583_of_match
[] = {
935 { .compatible
= "amstaos,tsl2580", },
936 { .compatible
= "amstaos,tsl2581", },
937 { .compatible
= "amstaos,tsl2583", },
940 MODULE_DEVICE_TABLE(of
, tsl2583_of_match
);
942 /* Driver definition */
943 static struct i2c_driver tsl2583_driver
= {
946 .pm
= &tsl2583_pm_ops
,
947 .of_match_table
= tsl2583_of_match
,
949 .id_table
= tsl2583_idtable
,
950 .probe
= tsl2583_probe
,
951 .remove
= tsl2583_remove
,
953 module_i2c_driver(tsl2583_driver
);
955 MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
956 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
957 MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
958 MODULE_LICENSE("GPL");