1 // SPDX-License-Identifier: GPL-2.0-only
3 * tsl4531.c - Support for TAOS TSL4531 ambient light sensor
5 * Copyright 2013 Peter Meerwald <pmeerw@pmeerw.net>
7 * IIO driver for the TSL4531x family
8 * TSL45311/TSL45313: 7-bit I2C slave address 0x39
9 * TSL45315/TSL45317: 7-bit I2C slave address 0x29
11 * TODO: single cycle measurement
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/err.h>
17 #include <linux/delay.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
22 #define TSL4531_DRV_NAME "tsl4531"
24 #define TSL4531_COMMAND BIT(7)
26 #define TSL4531_CONTROL (TSL4531_COMMAND | 0x00)
27 #define TSL4531_CONFIG (TSL4531_COMMAND | 0x01)
28 #define TSL4531_DATA (TSL4531_COMMAND | 0x04)
29 #define TSL4531_ID (TSL4531_COMMAND | 0x0a)
31 /* operating modes in control register */
32 #define TSL4531_MODE_POWERDOWN 0x00
33 #define TSL4531_MODE_SINGLE_ADC 0x02
34 #define TSL4531_MODE_NORMAL 0x03
36 /* integration time control in config register */
37 #define TSL4531_TCNTRL_400MS 0x00
38 #define TSL4531_TCNTRL_200MS 0x01
39 #define TSL4531_TCNTRL_100MS 0x02
41 /* part number in id register */
42 #define TSL45311_ID 0x8
43 #define TSL45313_ID 0x9
44 #define TSL45315_ID 0xa
45 #define TSL45317_ID 0xb
46 #define TSL4531_ID_SHIFT 4
49 struct i2c_client
*client
;
54 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.2 0.4");
56 static struct attribute
*tsl4531_attributes
[] = {
57 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
61 static const struct attribute_group tsl4531_attribute_group
= {
62 .attrs
= tsl4531_attributes
,
65 static const struct iio_chan_spec tsl4531_channels
[] = {
68 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
69 BIT(IIO_CHAN_INFO_SCALE
) |
70 BIT(IIO_CHAN_INFO_INT_TIME
)
74 static int tsl4531_read_raw(struct iio_dev
*indio_dev
,
75 struct iio_chan_spec
const *chan
,
76 int *val
, int *val2
, long mask
)
78 struct tsl4531_data
*data
= iio_priv(indio_dev
);
82 case IIO_CHAN_INFO_RAW
:
83 ret
= i2c_smbus_read_word_data(data
->client
,
89 case IIO_CHAN_INFO_SCALE
:
90 /* 0.. 1x, 1 .. 2x, 2 .. 4x */
91 *val
= 1 << data
->int_time
;
93 case IIO_CHAN_INFO_INT_TIME
:
94 if (data
->int_time
== 0)
96 else if (data
->int_time
== 1)
98 else if (data
->int_time
== 2)
103 return IIO_VAL_INT_PLUS_MICRO
;
109 static int tsl4531_write_raw(struct iio_dev
*indio_dev
,
110 struct iio_chan_spec
const *chan
,
111 int val
, int val2
, long mask
)
113 struct tsl4531_data
*data
= iio_priv(indio_dev
);
117 case IIO_CHAN_INFO_INT_TIME
:
122 else if (val2
== 200000)
124 else if (val2
== 100000)
128 mutex_lock(&data
->lock
);
129 ret
= i2c_smbus_write_byte_data(data
->client
,
130 TSL4531_CONFIG
, int_time
);
132 data
->int_time
= int_time
;
133 mutex_unlock(&data
->lock
);
140 static const struct iio_info tsl4531_info
= {
141 .read_raw
= tsl4531_read_raw
,
142 .write_raw
= tsl4531_write_raw
,
143 .attrs
= &tsl4531_attribute_group
,
146 static int tsl4531_check_id(struct i2c_client
*client
)
148 int ret
= i2c_smbus_read_byte_data(client
, TSL4531_ID
);
152 switch (ret
>> TSL4531_ID_SHIFT
) {
163 static int tsl4531_probe(struct i2c_client
*client
,
164 const struct i2c_device_id
*id
)
166 struct tsl4531_data
*data
;
167 struct iio_dev
*indio_dev
;
170 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
174 data
= iio_priv(indio_dev
);
175 i2c_set_clientdata(client
, indio_dev
);
176 data
->client
= client
;
177 mutex_init(&data
->lock
);
179 ret
= tsl4531_check_id(client
);
181 dev_err(&client
->dev
, "no TSL4531 sensor\n");
185 ret
= i2c_smbus_write_byte_data(data
->client
, TSL4531_CONTROL
,
186 TSL4531_MODE_NORMAL
);
190 ret
= i2c_smbus_write_byte_data(data
->client
, TSL4531_CONFIG
,
191 TSL4531_TCNTRL_400MS
);
195 indio_dev
->info
= &tsl4531_info
;
196 indio_dev
->channels
= tsl4531_channels
;
197 indio_dev
->num_channels
= ARRAY_SIZE(tsl4531_channels
);
198 indio_dev
->name
= TSL4531_DRV_NAME
;
199 indio_dev
->modes
= INDIO_DIRECT_MODE
;
201 return iio_device_register(indio_dev
);
204 static int tsl4531_powerdown(struct i2c_client
*client
)
206 return i2c_smbus_write_byte_data(client
, TSL4531_CONTROL
,
207 TSL4531_MODE_POWERDOWN
);
210 static int tsl4531_remove(struct i2c_client
*client
)
212 iio_device_unregister(i2c_get_clientdata(client
));
213 tsl4531_powerdown(client
);
218 #ifdef CONFIG_PM_SLEEP
219 static int tsl4531_suspend(struct device
*dev
)
221 return tsl4531_powerdown(to_i2c_client(dev
));
224 static int tsl4531_resume(struct device
*dev
)
226 return i2c_smbus_write_byte_data(to_i2c_client(dev
), TSL4531_CONTROL
,
227 TSL4531_MODE_NORMAL
);
230 static SIMPLE_DEV_PM_OPS(tsl4531_pm_ops
, tsl4531_suspend
, tsl4531_resume
);
231 #define TSL4531_PM_OPS (&tsl4531_pm_ops)
233 #define TSL4531_PM_OPS NULL
236 static const struct i2c_device_id tsl4531_id
[] = {
240 MODULE_DEVICE_TABLE(i2c
, tsl4531_id
);
242 static struct i2c_driver tsl4531_driver
= {
244 .name
= TSL4531_DRV_NAME
,
245 .pm
= TSL4531_PM_OPS
,
247 .probe
= tsl4531_probe
,
248 .remove
= tsl4531_remove
,
249 .id_table
= tsl4531_id
,
252 module_i2c_driver(tsl4531_driver
);
254 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
255 MODULE_DESCRIPTION("TAOS TSL4531 ambient light sensors driver");
256 MODULE_LICENSE("GPL");