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 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>
31 /* Device Registers and Masks */
32 #define TSL2583_CNTRL 0x00
33 #define TSL2583_ALS_TIME 0X01
34 #define TSL2583_INTERRUPT 0x02
35 #define TSL2583_GAIN 0x07
36 #define TSL2583_REVID 0x11
37 #define TSL2583_CHIPID 0x12
38 #define TSL2583_ALS_CHAN0LO 0x14
39 #define TSL2583_ALS_CHAN0HI 0x15
40 #define TSL2583_ALS_CHAN1LO 0x16
41 #define TSL2583_ALS_CHAN1HI 0x17
42 #define TSL2583_TMR_LO 0x18
43 #define TSL2583_TMR_HI 0x19
45 /* tsl2583 cmd reg masks */
46 #define TSL2583_CMD_REG 0x80
47 #define TSL2583_CMD_SPL_FN 0x60
48 #define TSL2583_CMD_ALS_INT_CLR 0x01
50 /* tsl2583 cntrl reg masks */
51 #define TSL2583_CNTL_ADC_ENBL 0x02
52 #define TSL2583_CNTL_PWR_OFF 0x00
53 #define TSL2583_CNTL_PWR_ON 0x01
55 /* tsl2583 status reg masks */
56 #define TSL2583_STA_ADC_VALID 0x01
57 #define TSL2583_STA_ADC_INTR 0x10
59 /* Lux calculation constants */
60 #define TSL2583_LUX_CALC_OVER_FLOW 65535
62 #define TSL2583_INTERRUPT_DISABLED 0x00
64 #define TSL2583_CHIP_ID 0x90
65 #define TSL2583_CHIP_ID_MASK 0xf0
68 struct tsl2583_als_info
{
80 static const struct tsl2583_lux tsl2583_default_lux
[] = {
81 { 9830, 8520, 15729 },
82 { 12452, 10807, 23344 },
83 { 14746, 6383, 11705 },
84 { 17695, 4063, 6554 },
85 { 0, 0, 0 } /* Termination segment */
88 #define TSL2583_MAX_LUX_TABLE_ENTRIES 11
90 struct tsl2583_settings
{
97 * This structure is intentionally large to accommodate updates via
98 * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
99 * Assumption is that one and only one type of glass used.
101 struct tsl2583_lux als_device_lux
[TSL2583_MAX_LUX_TABLE_ENTRIES
];
104 struct tsl2583_chip
{
105 struct mutex als_mutex
;
106 struct i2c_client
*client
;
107 struct tsl2583_als_info als_cur_info
;
108 struct tsl2583_settings als_settings
;
120 /* Index = (0 - 3) Used to validate the gain selection index */
121 static const struct gainadj gainadj
[] = {
129 * Provides initial operational parameter defaults.
130 * These defaults may be changed through the device's sysfs files.
132 static void tsl2583_defaults(struct tsl2583_chip
*chip
)
135 * The integration time must be a multiple of 50ms and within the
136 * range [50, 600] ms.
138 chip
->als_settings
.als_time
= 100;
141 * This is an index into the gainadj table. Assume clear glass as the
144 chip
->als_settings
.als_gain
= 0;
146 /* Default gain trim to account for aperture effects */
147 chip
->als_settings
.als_gain_trim
= 1000;
149 /* Known external ALS reading used for calibration */
150 chip
->als_settings
.als_cal_target
= 130;
152 /* Default lux table. */
153 memcpy(chip
->als_settings
.als_device_lux
, tsl2583_default_lux
,
154 sizeof(tsl2583_default_lux
));
158 * Reads and calculates current lux value.
159 * The raw ch0 and ch1 values of the ambient light sensed in the last
160 * integration cycle are read from the device.
161 * Time scale factor array values are adjusted based on the integration time.
162 * The raw values are multiplied by a scale factor, and device gain is obtained
163 * using gain index. Limit checks are done next, then the ratio of a multiple
164 * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
165 * declared above is then scanned to find the first ratio value that is just
166 * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
167 * the array are then used along with the time scale factor array values, to
170 static int tsl2583_get_lux(struct iio_dev
*indio_dev
)
172 u16 ch0
, ch1
; /* separated ch0/ch1 data from device */
173 u32 lux
; /* raw lux calculated from device data */
177 struct tsl2583_lux
*p
;
178 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
181 ret
= i2c_smbus_read_byte_data(chip
->client
, TSL2583_CMD_REG
);
183 dev_err(&chip
->client
->dev
, "%s: failed to read CMD_REG register\n",
188 /* is data new & valid */
189 if (!(ret
& TSL2583_STA_ADC_INTR
)) {
190 dev_err(&chip
->client
->dev
, "%s: data not valid; returning last value\n",
192 ret
= chip
->als_cur_info
.lux
; /* return LAST VALUE */
196 for (i
= 0; i
< 4; i
++) {
197 int reg
= TSL2583_CMD_REG
| (TSL2583_ALS_CHAN0LO
+ i
);
199 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
201 dev_err(&chip
->client
->dev
, "%s: failed to read register %x\n",
209 * Clear the pending interrupt status bit on the chip to allow the next
210 * integration cycle to start. This has to be done even though this
211 * driver currently does not support interrupts.
213 ret
= i2c_smbus_write_byte(chip
->client
,
214 (TSL2583_CMD_REG
| TSL2583_CMD_SPL_FN
|
215 TSL2583_CMD_ALS_INT_CLR
));
217 dev_err(&chip
->client
->dev
, "%s: failed to clear the interrupt bit\n",
219 goto done
; /* have no data, so return failure */
222 /* extract ALS/lux data */
223 ch0
= le16_to_cpup((const __le16
*)&buf
[0]);
224 ch1
= le16_to_cpup((const __le16
*)&buf
[2]);
226 chip
->als_cur_info
.als_ch0
= ch0
;
227 chip
->als_cur_info
.als_ch1
= ch1
;
229 if ((ch0
>= chip
->als_saturation
) || (ch1
>= chip
->als_saturation
))
234 * The sensor appears to be in total darkness so set the
235 * calculated lux to 0 and return early to avoid a division by
236 * zero below when calculating the ratio.
239 chip
->als_cur_info
.lux
= 0;
243 /* calculate ratio */
244 ratio
= (ch1
<< 15) / ch0
;
246 /* convert to unscaled lux using the pointer to the table */
247 for (p
= (struct tsl2583_lux
*)chip
->als_settings
.als_device_lux
;
248 p
->ratio
!= 0 && p
->ratio
< ratio
; p
++)
256 ch0lux
= ((ch0
* p
->ch0
) +
257 (gainadj
[chip
->als_settings
.als_gain
].ch0
>> 1))
258 / gainadj
[chip
->als_settings
.als_gain
].ch0
;
259 ch1lux
= ((ch1
* p
->ch1
) +
260 (gainadj
[chip
->als_settings
.als_gain
].ch1
>> 1))
261 / gainadj
[chip
->als_settings
.als_gain
].ch1
;
263 /* note: lux is 31 bit max at this point */
264 if (ch1lux
> ch0lux
) {
265 dev_dbg(&chip
->client
->dev
, "%s: No Data - Returning 0\n",
268 chip
->als_cur_info
.lux
= 0;
272 lux
= ch0lux
- ch1lux
;
275 /* adjust for active time scale */
276 if (chip
->als_time_scale
== 0)
279 lux
= (lux
+ (chip
->als_time_scale
>> 1)) /
280 chip
->als_time_scale
;
283 * Adjust for active gain scale.
284 * The tsl2583_default_lux tables above have a factor of 8192 built in,
285 * so we need to shift right.
286 * User-specified gain provides a multiplier.
287 * Apply user-specified gain before shifting right to retain precision.
288 * Use 64 bits to avoid overflow on multiplication.
289 * Then go back to 32 bits before division to avoid using div_u64().
292 lux64
= lux64
* chip
->als_settings
.als_gain_trim
;
295 lux
= (lux
+ 500) / 1000;
297 if (lux
> TSL2583_LUX_CALC_OVER_FLOW
) { /* check for overflow */
299 lux
= TSL2583_LUX_CALC_OVER_FLOW
;
302 /* Update the structure with the latest VALID lux. */
303 chip
->als_cur_info
.lux
= lux
;
311 * Obtain single reading and calculate the als_gain_trim (later used
312 * to derive actual lux).
313 * Return updated gain_trim value.
315 static int tsl2583_als_calibrate(struct iio_dev
*indio_dev
)
317 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
318 unsigned int gain_trim_val
;
322 ret
= i2c_smbus_read_byte_data(chip
->client
,
323 TSL2583_CMD_REG
| TSL2583_CNTRL
);
325 dev_err(&chip
->client
->dev
,
326 "%s: failed to read from the CNTRL register\n",
331 if ((ret
& (TSL2583_CNTL_ADC_ENBL
| TSL2583_CNTL_PWR_ON
))
332 != (TSL2583_CNTL_ADC_ENBL
| TSL2583_CNTL_PWR_ON
)) {
333 dev_err(&chip
->client
->dev
,
334 "%s: Device is not powered on and/or ADC is not enabled\n",
337 } else if ((ret
& TSL2583_STA_ADC_VALID
) != TSL2583_STA_ADC_VALID
) {
338 dev_err(&chip
->client
->dev
,
339 "%s: The two ADC channels have not completed an integration cycle\n",
344 lux_val
= tsl2583_get_lux(indio_dev
);
346 dev_err(&chip
->client
->dev
, "%s: failed to get lux\n",
351 gain_trim_val
= (unsigned int)(((chip
->als_settings
.als_cal_target
)
352 * chip
->als_settings
.als_gain_trim
) / lux_val
);
353 if ((gain_trim_val
< 250) || (gain_trim_val
> 4000)) {
354 dev_err(&chip
->client
->dev
,
355 "%s: trim_val of %d is not within the range [250, 4000]\n",
356 __func__
, gain_trim_val
);
360 chip
->als_settings
.als_gain_trim
= (int)gain_trim_val
;
365 static int tsl2583_set_als_time(struct tsl2583_chip
*chip
)
367 int als_count
, als_time
, ret
;
370 /* determine als integration register */
371 als_count
= (chip
->als_settings
.als_time
* 100 + 135) / 270;
373 als_count
= 1; /* ensure at least one cycle */
375 /* convert back to time (encompasses overrides) */
376 als_time
= (als_count
* 27 + 5) / 10;
378 val
= 256 - als_count
;
379 ret
= i2c_smbus_write_byte_data(chip
->client
,
380 TSL2583_CMD_REG
| TSL2583_ALS_TIME
,
383 dev_err(&chip
->client
->dev
, "%s: failed to set the als time to %d\n",
388 /* set chip struct re scaling and saturation */
389 chip
->als_saturation
= als_count
* 922; /* 90% of full scale */
390 chip
->als_time_scale
= (als_time
+ 25) / 50;
395 static int tsl2583_set_als_gain(struct tsl2583_chip
*chip
)
399 /* Set the gain based on als_settings struct */
400 ret
= i2c_smbus_write_byte_data(chip
->client
,
401 TSL2583_CMD_REG
| TSL2583_GAIN
,
402 chip
->als_settings
.als_gain
);
404 dev_err(&chip
->client
->dev
,
405 "%s: failed to set the gain to %d\n", __func__
,
406 chip
->als_settings
.als_gain
);
411 static int tsl2583_set_power_state(struct tsl2583_chip
*chip
, u8 state
)
415 ret
= i2c_smbus_write_byte_data(chip
->client
,
416 TSL2583_CMD_REG
| TSL2583_CNTRL
, state
);
418 dev_err(&chip
->client
->dev
,
419 "%s: failed to set the power state to %d\n", __func__
,
426 * Turn the device on.
427 * Configuration must be set before calling this function.
429 static int tsl2583_chip_init_and_power_on(struct iio_dev
*indio_dev
)
431 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
434 /* Power on the device; ADC off. */
435 ret
= tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_ON
);
439 ret
= i2c_smbus_write_byte_data(chip
->client
,
440 TSL2583_CMD_REG
| TSL2583_INTERRUPT
,
441 TSL2583_INTERRUPT_DISABLED
);
443 dev_err(&chip
->client
->dev
,
444 "%s: failed to disable interrupts\n", __func__
);
448 ret
= tsl2583_set_als_time(chip
);
452 ret
= tsl2583_set_als_gain(chip
);
456 usleep_range(3000, 3500);
458 ret
= tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_ON
|
459 TSL2583_CNTL_ADC_ENBL
);
463 chip
->suspended
= false;
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 if (chip
->suspended
) {
521 ret
= tsl2583_als_calibrate(indio_dev
);
527 mutex_unlock(&chip
->als_mutex
);
532 static ssize_t
in_illuminance_lux_table_show(struct device
*dev
,
533 struct device_attribute
*attr
,
536 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
537 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
541 for (i
= 0; i
< ARRAY_SIZE(chip
->als_settings
.als_device_lux
); i
++) {
542 offset
+= sprintf(buf
+ offset
, "%u,%u,%u,",
543 chip
->als_settings
.als_device_lux
[i
].ratio
,
544 chip
->als_settings
.als_device_lux
[i
].ch0
,
545 chip
->als_settings
.als_device_lux
[i
].ch1
);
546 if (chip
->als_settings
.als_device_lux
[i
].ratio
== 0) {
548 * We just printed the first "0" entry.
549 * Now get rid of the extra "," and break.
556 offset
+= sprintf(buf
+ offset
, "\n");
561 static ssize_t
in_illuminance_lux_table_store(struct device
*dev
,
562 struct device_attribute
*attr
,
563 const char *buf
, size_t len
)
565 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
566 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
567 const unsigned int max_ints
= TSL2583_MAX_LUX_TABLE_ENTRIES
* 3;
568 int value
[TSL2583_MAX_LUX_TABLE_ENTRIES
* 3 + 1];
572 mutex_lock(&chip
->als_mutex
);
574 get_options(buf
, ARRAY_SIZE(value
), value
);
577 * We now have an array of ints starting at value[1], and
578 * enumerated by value[0].
579 * We expect each group of three ints is one table entry,
580 * and the last table entry is all 0.
583 if ((n
% 3) || n
< 6 || n
> max_ints
) {
585 "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
589 if ((value
[n
- 2] | value
[n
- 1] | value
[n
]) != 0) {
590 dev_err(dev
, "%s: The last 3 entries in the lux table must be zeros.\n",
595 memcpy(chip
->als_settings
.als_device_lux
, &value
[1],
596 value
[0] * sizeof(value
[1]));
601 mutex_unlock(&chip
->als_mutex
);
606 static IIO_CONST_ATTR(in_illuminance_calibscale_available
, "1 8 16 111");
607 static IIO_CONST_ATTR(in_illuminance_integration_time_available
,
608 "0.000050 0.000100 0.000150 0.000200 0.000250 0.000300 0.000350 0.000400 0.000450 0.000500 0.000550 0.000600 0.000650");
609 static IIO_DEVICE_ATTR_RW(in_illuminance_input_target
, 0);
610 static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate
, 0);
611 static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table
, 0);
613 static struct attribute
*sysfs_attrs_ctrl
[] = {
614 &iio_const_attr_in_illuminance_calibscale_available
.dev_attr
.attr
,
615 &iio_const_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
616 &iio_dev_attr_in_illuminance_input_target
.dev_attr
.attr
,
617 &iio_dev_attr_in_illuminance_calibrate
.dev_attr
.attr
,
618 &iio_dev_attr_in_illuminance_lux_table
.dev_attr
.attr
,
622 static const struct attribute_group tsl2583_attribute_group
= {
623 .attrs
= sysfs_attrs_ctrl
,
626 static const struct iio_chan_spec tsl2583_channels
[] = {
630 .channel2
= IIO_MOD_LIGHT_IR
,
631 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
636 .channel2
= IIO_MOD_LIGHT_BOTH
,
637 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
641 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
642 BIT(IIO_CHAN_INFO_CALIBBIAS
) |
643 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
644 BIT(IIO_CHAN_INFO_INT_TIME
),
648 static int tsl2583_read_raw(struct iio_dev
*indio_dev
,
649 struct iio_chan_spec
const *chan
,
650 int *val
, int *val2
, long mask
)
652 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
655 mutex_lock(&chip
->als_mutex
);
657 if (chip
->suspended
) {
663 case IIO_CHAN_INFO_RAW
:
664 if (chan
->type
== IIO_LIGHT
) {
665 ret
= tsl2583_get_lux(indio_dev
);
670 * From page 20 of the TSL2581, TSL2583 data
671 * sheet (TAOS134 − MARCH 2011):
673 * One of the photodiodes (channel 0) is
674 * sensitive to both visible and infrared light,
675 * while the second photodiode (channel 1) is
676 * sensitive primarily to infrared light.
678 if (chan
->channel2
== IIO_MOD_LIGHT_BOTH
)
679 *val
= chip
->als_cur_info
.als_ch0
;
681 *val
= chip
->als_cur_info
.als_ch1
;
686 case IIO_CHAN_INFO_PROCESSED
:
687 if (chan
->type
== IIO_LIGHT
) {
688 ret
= tsl2583_get_lux(indio_dev
);
696 case IIO_CHAN_INFO_CALIBBIAS
:
697 if (chan
->type
== IIO_LIGHT
) {
698 *val
= chip
->als_settings
.als_gain_trim
;
702 case IIO_CHAN_INFO_CALIBSCALE
:
703 if (chan
->type
== IIO_LIGHT
) {
704 *val
= gainadj
[chip
->als_settings
.als_gain
].mean
;
708 case IIO_CHAN_INFO_INT_TIME
:
709 if (chan
->type
== IIO_LIGHT
) {
711 *val2
= chip
->als_settings
.als_time
;
712 ret
= IIO_VAL_INT_PLUS_MICRO
;
720 mutex_unlock(&chip
->als_mutex
);
725 static int tsl2583_write_raw(struct iio_dev
*indio_dev
,
726 struct iio_chan_spec
const *chan
,
727 int val
, int val2
, long mask
)
729 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
732 mutex_lock(&chip
->als_mutex
);
734 if (chip
->suspended
) {
740 case IIO_CHAN_INFO_CALIBBIAS
:
741 if (chan
->type
== IIO_LIGHT
) {
742 chip
->als_settings
.als_gain_trim
= val
;
746 case IIO_CHAN_INFO_CALIBSCALE
:
747 if (chan
->type
== IIO_LIGHT
) {
750 for (i
= 0; i
< ARRAY_SIZE(gainadj
); i
++) {
751 if (gainadj
[i
].mean
== val
) {
752 chip
->als_settings
.als_gain
= i
;
753 ret
= tsl2583_set_als_gain(chip
);
759 case IIO_CHAN_INFO_INT_TIME
:
760 if (chan
->type
== IIO_LIGHT
&& !val
&& val2
>= 50 &&
761 val2
<= 650 && !(val2
% 50)) {
762 chip
->als_settings
.als_time
= val2
;
763 ret
= tsl2583_set_als_time(chip
);
771 mutex_unlock(&chip
->als_mutex
);
776 static const struct iio_info tsl2583_info
= {
777 .attrs
= &tsl2583_attribute_group
,
778 .driver_module
= THIS_MODULE
,
779 .read_raw
= tsl2583_read_raw
,
780 .write_raw
= tsl2583_write_raw
,
783 static int tsl2583_probe(struct i2c_client
*clientp
,
784 const struct i2c_device_id
*idp
)
787 struct tsl2583_chip
*chip
;
788 struct iio_dev
*indio_dev
;
790 if (!i2c_check_functionality(clientp
->adapter
,
791 I2C_FUNC_SMBUS_BYTE_DATA
)) {
792 dev_err(&clientp
->dev
, "%s: i2c smbus byte data functionality is unsupported\n",
797 indio_dev
= devm_iio_device_alloc(&clientp
->dev
, sizeof(*chip
));
801 chip
= iio_priv(indio_dev
);
802 chip
->client
= clientp
;
803 i2c_set_clientdata(clientp
, indio_dev
);
805 mutex_init(&chip
->als_mutex
);
806 chip
->suspended
= true;
808 ret
= i2c_smbus_read_byte_data(clientp
,
809 TSL2583_CMD_REG
| TSL2583_CHIPID
);
811 dev_err(&clientp
->dev
,
812 "%s: failed to read the chip ID register\n", __func__
);
816 if ((ret
& TSL2583_CHIP_ID_MASK
) != TSL2583_CHIP_ID
) {
817 dev_err(&clientp
->dev
, "%s: received an unknown chip ID %x\n",
822 indio_dev
->info
= &tsl2583_info
;
823 indio_dev
->channels
= tsl2583_channels
;
824 indio_dev
->num_channels
= ARRAY_SIZE(tsl2583_channels
);
825 indio_dev
->dev
.parent
= &clientp
->dev
;
826 indio_dev
->modes
= INDIO_DIRECT_MODE
;
827 indio_dev
->name
= chip
->client
->name
;
829 ret
= devm_iio_device_register(indio_dev
->dev
.parent
, indio_dev
);
831 dev_err(&clientp
->dev
, "%s: iio registration failed\n",
836 /* Load up the V2 defaults (these are hard coded defaults for now) */
837 tsl2583_defaults(chip
);
839 /* Make sure the chip is on */
840 ret
= tsl2583_chip_init_and_power_on(indio_dev
);
844 dev_info(&clientp
->dev
, "Light sensor found.\n");
849 static int __maybe_unused
tsl2583_suspend(struct device
*dev
)
851 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
852 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
855 mutex_lock(&chip
->als_mutex
);
857 ret
= tsl2583_set_power_state(chip
, TSL2583_CNTL_PWR_OFF
);
858 chip
->suspended
= true;
860 mutex_unlock(&chip
->als_mutex
);
865 static int __maybe_unused
tsl2583_resume(struct device
*dev
)
867 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
868 struct tsl2583_chip
*chip
= iio_priv(indio_dev
);
871 mutex_lock(&chip
->als_mutex
);
873 ret
= tsl2583_chip_init_and_power_on(indio_dev
);
875 mutex_unlock(&chip
->als_mutex
);
880 static SIMPLE_DEV_PM_OPS(tsl2583_pm_ops
, tsl2583_suspend
, tsl2583_resume
);
882 static struct i2c_device_id tsl2583_idtable
[] = {
888 MODULE_DEVICE_TABLE(i2c
, tsl2583_idtable
);
890 static const struct of_device_id tsl2583_of_match
[] = {
891 { .compatible
= "amstaos,tsl2580", },
892 { .compatible
= "amstaos,tsl2581", },
893 { .compatible
= "amstaos,tsl2583", },
896 MODULE_DEVICE_TABLE(of
, tsl2583_of_match
);
898 /* Driver definition */
899 static struct i2c_driver tsl2583_driver
= {
902 .pm
= &tsl2583_pm_ops
,
903 .of_match_table
= tsl2583_of_match
,
905 .id_table
= tsl2583_idtable
,
906 .probe
= tsl2583_probe
,
908 module_i2c_driver(tsl2583_driver
);
910 MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
911 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
912 MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
913 MODULE_LICENSE("GPL");