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_MODE_RANGE BIT(3)
49 #define ISL29125_STATUS_CONV BIT(1)
51 struct isl29125_data
{
52 struct i2c_client
*client
;
55 u16 buffer
[8]; /* 3x 16-bit, padding, 8 bytes timestamp */
58 #define ISL29125_CHANNEL(_color, _si) { \
59 .type = IIO_INTENSITY, \
61 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
62 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
63 .channel2 = IIO_MOD_LIGHT_##_color, \
69 .endianness = IIO_CPU, \
73 static const struct iio_chan_spec isl29125_channels
[] = {
74 ISL29125_CHANNEL(GREEN
, 0),
75 ISL29125_CHANNEL(RED
, 1),
76 ISL29125_CHANNEL(BLUE
, 2),
77 IIO_CHAN_SOFT_TIMESTAMP(3),
83 {ISL29125_MODE_G
, ISL29125_GREEN_DATA
},
84 {ISL29125_MODE_R
, ISL29125_RED_DATA
},
85 {ISL29125_MODE_B
, ISL29125_BLUE_DATA
},
88 static int isl29125_read_data(struct isl29125_data
*data
, int si
)
93 ret
= i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
94 data
->conf1
| isl29125_regs
[si
].mode
);
101 ret
= i2c_smbus_read_byte_data(data
->client
, ISL29125_STATUS
);
104 if (ret
& ISL29125_STATUS_CONV
)
110 dev_err(&data
->client
->dev
, "data not ready\n");
115 ret
= i2c_smbus_read_word_data(data
->client
, isl29125_regs
[si
].data
);
118 i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
, data
->conf1
);
122 static int isl29125_read_raw(struct iio_dev
*indio_dev
,
123 struct iio_chan_spec
const *chan
,
124 int *val
, int *val2
, long mask
)
126 struct isl29125_data
*data
= iio_priv(indio_dev
);
130 case IIO_CHAN_INFO_RAW
:
131 if (iio_buffer_enabled(indio_dev
))
133 mutex_lock(&data
->lock
);
134 ret
= isl29125_read_data(data
, chan
->scan_index
);
135 mutex_unlock(&data
->lock
);
140 case IIO_CHAN_INFO_SCALE
:
142 if (data
->conf1
& ISL29125_MODE_RANGE
)
143 *val2
= 152590; /* 10k lux full range */
145 *val2
= 5722; /* 375 lux full range */
146 return IIO_VAL_INT_PLUS_MICRO
;
151 static int isl29125_write_raw(struct iio_dev
*indio_dev
,
152 struct iio_chan_spec
const *chan
,
153 int val
, int val2
, long mask
)
155 struct isl29125_data
*data
= iio_priv(indio_dev
);
158 case IIO_CHAN_INFO_SCALE
:
162 data
->conf1
|= ISL29125_MODE_RANGE
;
163 else if (val2
== 5722)
164 data
->conf1
&= ~ISL29125_MODE_RANGE
;
167 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
174 static irqreturn_t
isl29125_trigger_handler(int irq
, void *p
)
176 struct iio_poll_func
*pf
= p
;
177 struct iio_dev
*indio_dev
= pf
->indio_dev
;
178 struct isl29125_data
*data
= iio_priv(indio_dev
);
181 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
182 indio_dev
->masklength
) {
183 int ret
= i2c_smbus_read_word_data(data
->client
,
184 isl29125_regs
[i
].data
);
188 data
->buffer
[j
++] = ret
;
191 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
195 iio_trigger_notify_done(indio_dev
->trig
);
200 static IIO_CONST_ATTR(scale_available
, "0.005722 0.152590");
202 static struct attribute
*isl29125_attributes
[] = {
203 &iio_const_attr_scale_available
.dev_attr
.attr
,
207 static const struct attribute_group isl29125_attribute_group
= {
208 .attrs
= isl29125_attributes
,
211 static const struct iio_info isl29125_info
= {
212 .read_raw
= isl29125_read_raw
,
213 .write_raw
= isl29125_write_raw
,
214 .attrs
= &isl29125_attribute_group
,
215 .driver_module
= THIS_MODULE
,
218 static int isl29125_buffer_preenable(struct iio_dev
*indio_dev
)
220 struct isl29125_data
*data
= iio_priv(indio_dev
);
222 data
->conf1
|= ISL29125_MODE_RGB
;
223 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
227 static int isl29125_buffer_predisable(struct iio_dev
*indio_dev
)
229 struct isl29125_data
*data
= iio_priv(indio_dev
);
232 ret
= iio_triggered_buffer_predisable(indio_dev
);
236 data
->conf1
&= ~ISL29125_MODE_MASK
;
237 data
->conf1
|= ISL29125_MODE_PD
;
238 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
242 static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops
= {
243 .preenable
= isl29125_buffer_preenable
,
244 .postenable
= &iio_triggered_buffer_postenable
,
245 .predisable
= isl29125_buffer_predisable
,
248 static int isl29125_probe(struct i2c_client
*client
,
249 const struct i2c_device_id
*id
)
251 struct isl29125_data
*data
;
252 struct iio_dev
*indio_dev
;
255 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
256 if (indio_dev
== NULL
)
259 data
= iio_priv(indio_dev
);
260 i2c_set_clientdata(client
, indio_dev
);
261 data
->client
= client
;
262 mutex_init(&data
->lock
);
264 indio_dev
->dev
.parent
= &client
->dev
;
265 indio_dev
->info
= &isl29125_info
;
266 indio_dev
->name
= ISL29125_DRV_NAME
;
267 indio_dev
->channels
= isl29125_channels
;
268 indio_dev
->num_channels
= ARRAY_SIZE(isl29125_channels
);
269 indio_dev
->modes
= INDIO_DIRECT_MODE
;
271 ret
= i2c_smbus_read_byte_data(data
->client
, ISL29125_DEVICE_ID
);
274 if (ret
!= ISL29125_ID
)
277 data
->conf1
= ISL29125_MODE_PD
| ISL29125_MODE_RANGE
;
278 ret
= i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
283 ret
= i2c_smbus_write_byte_data(data
->client
, ISL29125_STATUS
, 0);
287 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
288 isl29125_trigger_handler
, &isl29125_buffer_setup_ops
);
292 ret
= iio_device_register(indio_dev
);
299 iio_triggered_buffer_cleanup(indio_dev
);
303 static int isl29125_powerdown(struct isl29125_data
*data
)
305 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
306 (data
->conf1
& ~ISL29125_MODE_MASK
) | ISL29125_MODE_PD
);
309 static int isl29125_remove(struct i2c_client
*client
)
311 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
313 iio_device_unregister(indio_dev
);
314 iio_triggered_buffer_cleanup(indio_dev
);
315 isl29125_powerdown(iio_priv(indio_dev
));
320 #ifdef CONFIG_PM_SLEEP
321 static int isl29125_suspend(struct device
*dev
)
323 struct isl29125_data
*data
= iio_priv(i2c_get_clientdata(
324 to_i2c_client(dev
)));
325 return isl29125_powerdown(data
);
328 static int isl29125_resume(struct device
*dev
)
330 struct isl29125_data
*data
= iio_priv(i2c_get_clientdata(
331 to_i2c_client(dev
)));
332 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
337 static SIMPLE_DEV_PM_OPS(isl29125_pm_ops
, isl29125_suspend
, isl29125_resume
);
339 static const struct i2c_device_id isl29125_id
[] = {
343 MODULE_DEVICE_TABLE(i2c
, isl29125_id
);
345 static struct i2c_driver isl29125_driver
= {
347 .name
= ISL29125_DRV_NAME
,
348 .pm
= &isl29125_pm_ops
,
350 .probe
= isl29125_probe
,
351 .remove
= isl29125_remove
,
352 .id_table
= isl29125_id
,
354 module_i2c_driver(isl29125_driver
);
356 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
357 MODULE_DESCRIPTION("ISL29125 RGB light sensor driver");
358 MODULE_LICENSE("GPL");