2 * isl29125.c - Support for Intersil ISL29125 RGB light sensor
4 * Copyright (c) 2014 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 * RGB light sensor with 16-bit channels for red, green, blue);
11 * 7-bit I2C slave address 0x44
13 * TODO: interrupt support, IR compensation, thresholds, 12bit
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/triggered_buffer.h>
27 #define ISL29125_DRV_NAME "isl29125"
29 #define ISL29125_DEVICE_ID 0x00
30 #define ISL29125_CONF1 0x01
31 #define ISL29125_CONF2 0x02
32 #define ISL29125_CONF3 0x03
33 #define ISL29125_STATUS 0x08
34 #define ISL29125_GREEN_DATA 0x09
35 #define ISL29125_RED_DATA 0x0b
36 #define ISL29125_BLUE_DATA 0x0d
38 #define ISL29125_ID 0x7d
40 #define ISL29125_MODE_MASK GENMASK(2, 0)
41 #define ISL29125_MODE_PD 0x0
42 #define ISL29125_MODE_G 0x1
43 #define ISL29125_MODE_R 0x2
44 #define ISL29125_MODE_B 0x3
45 #define ISL29125_MODE_RGB 0x5
47 #define ISL29125_SENSING_RANGE_0 5722 /* 375 lux full range */
48 #define ISL29125_SENSING_RANGE_1 152590 /* 10k lux full range */
50 #define ISL29125_MODE_RANGE BIT(3)
52 #define ISL29125_STATUS_CONV BIT(1)
54 struct isl29125_data {
55 struct i2c_client *client;
57 /* Ensure timestamp is naturally aligned */
60 s64 timestamp __aligned(8);
64 #define ISL29125_CHANNEL(_color, _si) { \
65 .type = IIO_INTENSITY, \
67 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
68 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
69 .channel2 = IIO_MOD_LIGHT_##_color, \
75 .endianness = IIO_CPU, \
79 static const struct iio_chan_spec isl29125_channels[] = {
80 ISL29125_CHANNEL(GREEN, 0),
81 ISL29125_CHANNEL(RED, 1),
82 ISL29125_CHANNEL(BLUE, 2),
83 IIO_CHAN_SOFT_TIMESTAMP(3),
89 {ISL29125_MODE_G, ISL29125_GREEN_DATA},
90 {ISL29125_MODE_R, ISL29125_RED_DATA},
91 {ISL29125_MODE_B, ISL29125_BLUE_DATA},
94 static int isl29125_read_data(struct isl29125_data *data, int si)
99 ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
100 data->conf1 | isl29125_regs[si].mode);
107 ret = i2c_smbus_read_byte_data(data->client, ISL29125_STATUS);
110 if (ret & ISL29125_STATUS_CONV)
116 dev_err(&data->client->dev, "data not ready\n");
121 ret = i2c_smbus_read_word_data(data->client, isl29125_regs[si].data);
124 i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, data->conf1);
128 static int isl29125_read_raw(struct iio_dev *indio_dev,
129 struct iio_chan_spec const *chan,
130 int *val, int *val2, long mask)
132 struct isl29125_data *data = iio_priv(indio_dev);
136 case IIO_CHAN_INFO_RAW:
137 ret = iio_device_claim_direct_mode(indio_dev);
140 ret = isl29125_read_data(data, chan->scan_index);
141 iio_device_release_direct_mode(indio_dev);
146 case IIO_CHAN_INFO_SCALE:
148 if (data->conf1 & ISL29125_MODE_RANGE)
149 *val2 = ISL29125_SENSING_RANGE_1; /*10k lux full range*/
151 *val2 = ISL29125_SENSING_RANGE_0; /*375 lux full range*/
152 return IIO_VAL_INT_PLUS_MICRO;
157 static int isl29125_write_raw(struct iio_dev *indio_dev,
158 struct iio_chan_spec const *chan,
159 int val, int val2, long mask)
161 struct isl29125_data *data = iio_priv(indio_dev);
164 case IIO_CHAN_INFO_SCALE:
167 if (val2 == ISL29125_SENSING_RANGE_1)
168 data->conf1 |= ISL29125_MODE_RANGE;
169 else if (val2 == ISL29125_SENSING_RANGE_0)
170 data->conf1 &= ~ISL29125_MODE_RANGE;
173 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
180 static irqreturn_t isl29125_trigger_handler(int irq, void *p)
182 struct iio_poll_func *pf = p;
183 struct iio_dev *indio_dev = pf->indio_dev;
184 struct isl29125_data *data = iio_priv(indio_dev);
187 for_each_set_bit(i, indio_dev->active_scan_mask,
188 indio_dev->masklength) {
189 int ret = i2c_smbus_read_word_data(data->client,
190 isl29125_regs[i].data);
194 data->scan.chans[j++] = ret;
197 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
198 iio_get_time_ns(indio_dev));
201 iio_trigger_notify_done(indio_dev->trig);
206 static IIO_CONST_ATTR(scale_available, "0.005722 0.152590");
208 static struct attribute *isl29125_attributes[] = {
209 &iio_const_attr_scale_available.dev_attr.attr,
213 static const struct attribute_group isl29125_attribute_group = {
214 .attrs = isl29125_attributes,
217 static const struct iio_info isl29125_info = {
218 .read_raw = isl29125_read_raw,
219 .write_raw = isl29125_write_raw,
220 .attrs = &isl29125_attribute_group,
221 .driver_module = THIS_MODULE,
224 static int isl29125_buffer_preenable(struct iio_dev *indio_dev)
226 struct isl29125_data *data = iio_priv(indio_dev);
228 data->conf1 |= ISL29125_MODE_RGB;
229 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
233 static int isl29125_buffer_predisable(struct iio_dev *indio_dev)
235 struct isl29125_data *data = iio_priv(indio_dev);
238 ret = iio_triggered_buffer_predisable(indio_dev);
242 data->conf1 &= ~ISL29125_MODE_MASK;
243 data->conf1 |= ISL29125_MODE_PD;
244 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
248 static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops = {
249 .preenable = isl29125_buffer_preenable,
250 .postenable = &iio_triggered_buffer_postenable,
251 .predisable = isl29125_buffer_predisable,
254 static int isl29125_probe(struct i2c_client *client,
255 const struct i2c_device_id *id)
257 struct isl29125_data *data;
258 struct iio_dev *indio_dev;
261 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
262 if (indio_dev == NULL)
265 data = iio_priv(indio_dev);
266 i2c_set_clientdata(client, indio_dev);
267 data->client = client;
269 indio_dev->dev.parent = &client->dev;
270 indio_dev->info = &isl29125_info;
271 indio_dev->name = ISL29125_DRV_NAME;
272 indio_dev->channels = isl29125_channels;
273 indio_dev->num_channels = ARRAY_SIZE(isl29125_channels);
274 indio_dev->modes = INDIO_DIRECT_MODE;
276 ret = i2c_smbus_read_byte_data(data->client, ISL29125_DEVICE_ID);
279 if (ret != ISL29125_ID)
282 data->conf1 = ISL29125_MODE_PD | ISL29125_MODE_RANGE;
283 ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
288 ret = i2c_smbus_write_byte_data(data->client, ISL29125_STATUS, 0);
292 ret = iio_triggered_buffer_setup(indio_dev, NULL,
293 isl29125_trigger_handler, &isl29125_buffer_setup_ops);
297 ret = iio_device_register(indio_dev);
304 iio_triggered_buffer_cleanup(indio_dev);
308 static int isl29125_powerdown(struct isl29125_data *data)
310 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
311 (data->conf1 & ~ISL29125_MODE_MASK) | ISL29125_MODE_PD);
314 static int isl29125_remove(struct i2c_client *client)
316 struct iio_dev *indio_dev = i2c_get_clientdata(client);
318 iio_device_unregister(indio_dev);
319 iio_triggered_buffer_cleanup(indio_dev);
320 isl29125_powerdown(iio_priv(indio_dev));
325 #ifdef CONFIG_PM_SLEEP
326 static int isl29125_suspend(struct device *dev)
328 struct isl29125_data *data = iio_priv(i2c_get_clientdata(
329 to_i2c_client(dev)));
330 return isl29125_powerdown(data);
333 static int isl29125_resume(struct device *dev)
335 struct isl29125_data *data = iio_priv(i2c_get_clientdata(
336 to_i2c_client(dev)));
337 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
342 static SIMPLE_DEV_PM_OPS(isl29125_pm_ops, isl29125_suspend, isl29125_resume);
344 static const struct i2c_device_id isl29125_id[] = {
348 MODULE_DEVICE_TABLE(i2c, isl29125_id);
350 static struct i2c_driver isl29125_driver = {
352 .name = ISL29125_DRV_NAME,
353 .pm = &isl29125_pm_ops,
355 .probe = isl29125_probe,
356 .remove = isl29125_remove,
357 .id_table = isl29125_id,
359 module_i2c_driver(isl29125_driver);
361 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
362 MODULE_DESCRIPTION("ISL29125 RGB light sensor driver");
363 MODULE_LICENSE("GPL");