2 * tsl4531.c - Support for TAOS TSL4531 ambient light sensor
4 * Copyright 2013 Peter Meerwald <pmeerw@pmeerw.net>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO driver for the TSL4531x family
11 * TSL45311/TSL45313: 7-bit I2C slave address 0x39
12 * TSL45315/TSL45317: 7-bit I2C slave address 0x29
14 * TODO: single cycle measurement
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/err.h>
20 #include <linux/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
25 #define TSL4531_DRV_NAME "tsl4531"
27 #define TSL4531_COMMAND BIT(7)
29 #define TSL4531_CONTROL (TSL4531_COMMAND | 0x00)
30 #define TSL4531_CONFIG (TSL4531_COMMAND | 0x01)
31 #define TSL4531_DATA (TSL4531_COMMAND | 0x04)
32 #define TSL4531_ID (TSL4531_COMMAND | 0x0a)
34 /* operating modes in control register */
35 #define TSL4531_MODE_POWERDOWN 0x00
36 #define TSL4531_MODE_SINGLE_ADC 0x02
37 #define TSL4531_MODE_NORMAL 0x03
39 /* integration time control in config register */
40 #define TSL4531_TCNTRL_400MS 0x00
41 #define TSL4531_TCNTRL_200MS 0x01
42 #define TSL4531_TCNTRL_100MS 0x02
44 /* part number in id register */
45 #define TSL45311_ID 0x8
46 #define TSL45313_ID 0x9
47 #define TSL45315_ID 0xa
48 #define TSL45317_ID 0xb
49 #define TSL4531_ID_SHIFT 4
52 struct i2c_client
*client
;
57 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.2 0.4");
59 static struct attribute
*tsl4531_attributes
[] = {
60 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
64 static const struct attribute_group tsl4531_attribute_group
= {
65 .attrs
= tsl4531_attributes
,
68 static const struct iio_chan_spec tsl4531_channels
[] = {
71 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
72 BIT(IIO_CHAN_INFO_SCALE
) |
73 BIT(IIO_CHAN_INFO_INT_TIME
)
77 static int tsl4531_read_raw(struct iio_dev
*indio_dev
,
78 struct iio_chan_spec
const *chan
,
79 int *val
, int *val2
, long mask
)
81 struct tsl4531_data
*data
= iio_priv(indio_dev
);
85 case IIO_CHAN_INFO_RAW
:
86 ret
= i2c_smbus_read_word_data(data
->client
,
92 case IIO_CHAN_INFO_SCALE
:
93 /* 0.. 1x, 1 .. 2x, 2 .. 4x */
94 *val
= 1 << data
->int_time
;
96 case IIO_CHAN_INFO_INT_TIME
:
97 if (data
->int_time
== 0)
99 else if (data
->int_time
== 1)
101 else if (data
->int_time
== 2)
106 return IIO_VAL_INT_PLUS_MICRO
;
112 static int tsl4531_write_raw(struct iio_dev
*indio_dev
,
113 struct iio_chan_spec
const *chan
,
114 int val
, int val2
, long mask
)
116 struct tsl4531_data
*data
= iio_priv(indio_dev
);
120 case IIO_CHAN_INFO_INT_TIME
:
125 else if (val2
== 200000)
127 else if (val2
== 100000)
131 mutex_lock(&data
->lock
);
132 ret
= i2c_smbus_write_byte_data(data
->client
,
133 TSL4531_CONFIG
, int_time
);
135 data
->int_time
= int_time
;
136 mutex_unlock(&data
->lock
);
143 static const struct iio_info tsl4531_info
= {
144 .read_raw
= tsl4531_read_raw
,
145 .write_raw
= tsl4531_write_raw
,
146 .attrs
= &tsl4531_attribute_group
,
147 .driver_module
= THIS_MODULE
,
150 static int tsl4531_check_id(struct i2c_client
*client
)
152 int ret
= i2c_smbus_read_byte_data(client
, TSL4531_ID
);
156 switch (ret
>> TSL4531_ID_SHIFT
) {
167 static int tsl4531_probe(struct i2c_client
*client
,
168 const struct i2c_device_id
*id
)
170 struct tsl4531_data
*data
;
171 struct iio_dev
*indio_dev
;
174 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
178 data
= iio_priv(indio_dev
);
179 i2c_set_clientdata(client
, indio_dev
);
180 data
->client
= client
;
181 mutex_init(&data
->lock
);
183 ret
= tsl4531_check_id(client
);
185 dev_err(&client
->dev
, "no TSL4531 sensor\n");
189 ret
= i2c_smbus_write_byte_data(data
->client
, TSL4531_CONTROL
,
190 TSL4531_MODE_NORMAL
);
194 ret
= i2c_smbus_write_byte_data(data
->client
, TSL4531_CONFIG
,
195 TSL4531_TCNTRL_400MS
);
199 indio_dev
->dev
.parent
= &client
->dev
;
200 indio_dev
->info
= &tsl4531_info
;
201 indio_dev
->channels
= tsl4531_channels
;
202 indio_dev
->num_channels
= ARRAY_SIZE(tsl4531_channels
);
203 indio_dev
->name
= TSL4531_DRV_NAME
;
204 indio_dev
->modes
= INDIO_DIRECT_MODE
;
206 return iio_device_register(indio_dev
);
209 static int tsl4531_powerdown(struct i2c_client
*client
)
211 return i2c_smbus_write_byte_data(client
, TSL4531_CONTROL
,
212 TSL4531_MODE_POWERDOWN
);
215 static int tsl4531_remove(struct i2c_client
*client
)
217 iio_device_unregister(i2c_get_clientdata(client
));
218 tsl4531_powerdown(client
);
223 #ifdef CONFIG_PM_SLEEP
224 static int tsl4531_suspend(struct device
*dev
)
226 return tsl4531_powerdown(to_i2c_client(dev
));
229 static int tsl4531_resume(struct device
*dev
)
231 return i2c_smbus_write_byte_data(to_i2c_client(dev
), TSL4531_CONTROL
,
232 TSL4531_MODE_NORMAL
);
235 static SIMPLE_DEV_PM_OPS(tsl4531_pm_ops
, tsl4531_suspend
, tsl4531_resume
);
236 #define TSL4531_PM_OPS (&tsl4531_pm_ops)
238 #define TSL4531_PM_OPS NULL
241 static const struct i2c_device_id tsl4531_id
[] = {
245 MODULE_DEVICE_TABLE(i2c
, tsl4531_id
);
247 static struct i2c_driver tsl4531_driver
= {
249 .name
= TSL4531_DRV_NAME
,
250 .pm
= TSL4531_PM_OPS
,
252 .probe
= tsl4531_probe
,
253 .remove
= tsl4531_remove
,
254 .id_table
= tsl4531_id
,
257 module_i2c_driver(tsl4531_driver
);
259 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
260 MODULE_DESCRIPTION("TAOS TSL4531 ambient light sensors driver");
261 MODULE_LICENSE("GPL");