1 // SPDX-License-Identifier: GPL-2.0-only
3 * isl29125.c - Support for Intersil ISL29125 RGB light sensor
5 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
7 * RGB light sensor with 16-bit channels for red, green, blue);
8 * 7-bit I2C slave address 0x44
10 * TODO: interrupt support, IR compensation, thresholds, 12bit
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/delay.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/trigger_consumer.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/triggered_buffer.h>
24 #define ISL29125_DRV_NAME "isl29125"
26 #define ISL29125_DEVICE_ID 0x00
27 #define ISL29125_CONF1 0x01
28 #define ISL29125_CONF2 0x02
29 #define ISL29125_CONF3 0x03
30 #define ISL29125_STATUS 0x08
31 #define ISL29125_GREEN_DATA 0x09
32 #define ISL29125_RED_DATA 0x0b
33 #define ISL29125_BLUE_DATA 0x0d
35 #define ISL29125_ID 0x7d
37 #define ISL29125_MODE_MASK GENMASK(2, 0)
38 #define ISL29125_MODE_PD 0x0
39 #define ISL29125_MODE_G 0x1
40 #define ISL29125_MODE_R 0x2
41 #define ISL29125_MODE_B 0x3
42 #define ISL29125_MODE_RGB 0x5
44 #define ISL29125_SENSING_RANGE_0 5722 /* 375 lux full range */
45 #define ISL29125_SENSING_RANGE_1 152590 /* 10k lux full range */
47 #define ISL29125_MODE_RANGE BIT(3)
49 #define ISL29125_STATUS_CONV BIT(1)
51 struct isl29125_data
{
52 struct i2c_client
*client
;
54 u16 buffer
[8]; /* 3x 16-bit, padding, 8 bytes timestamp */
57 #define ISL29125_CHANNEL(_color, _si) { \
58 .type = IIO_INTENSITY, \
60 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
61 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
62 .channel2 = IIO_MOD_LIGHT_##_color, \
68 .endianness = IIO_CPU, \
72 static const struct iio_chan_spec isl29125_channels
[] = {
73 ISL29125_CHANNEL(GREEN
, 0),
74 ISL29125_CHANNEL(RED
, 1),
75 ISL29125_CHANNEL(BLUE
, 2),
76 IIO_CHAN_SOFT_TIMESTAMP(3),
82 {ISL29125_MODE_G
, ISL29125_GREEN_DATA
},
83 {ISL29125_MODE_R
, ISL29125_RED_DATA
},
84 {ISL29125_MODE_B
, ISL29125_BLUE_DATA
},
87 static int isl29125_read_data(struct isl29125_data
*data
, int si
)
92 ret
= i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
93 data
->conf1
| isl29125_regs
[si
].mode
);
100 ret
= i2c_smbus_read_byte_data(data
->client
, ISL29125_STATUS
);
103 if (ret
& ISL29125_STATUS_CONV
)
109 dev_err(&data
->client
->dev
, "data not ready\n");
114 ret
= i2c_smbus_read_word_data(data
->client
, isl29125_regs
[si
].data
);
117 i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
, data
->conf1
);
121 static int isl29125_read_raw(struct iio_dev
*indio_dev
,
122 struct iio_chan_spec
const *chan
,
123 int *val
, int *val2
, long mask
)
125 struct isl29125_data
*data
= iio_priv(indio_dev
);
129 case IIO_CHAN_INFO_RAW
:
130 ret
= iio_device_claim_direct_mode(indio_dev
);
133 ret
= isl29125_read_data(data
, chan
->scan_index
);
134 iio_device_release_direct_mode(indio_dev
);
139 case IIO_CHAN_INFO_SCALE
:
141 if (data
->conf1
& ISL29125_MODE_RANGE
)
142 *val2
= ISL29125_SENSING_RANGE_1
; /*10k lux full range*/
144 *val2
= ISL29125_SENSING_RANGE_0
; /*375 lux full range*/
145 return IIO_VAL_INT_PLUS_MICRO
;
150 static int isl29125_write_raw(struct iio_dev
*indio_dev
,
151 struct iio_chan_spec
const *chan
,
152 int val
, int val2
, long mask
)
154 struct isl29125_data
*data
= iio_priv(indio_dev
);
157 case IIO_CHAN_INFO_SCALE
:
160 if (val2
== ISL29125_SENSING_RANGE_1
)
161 data
->conf1
|= ISL29125_MODE_RANGE
;
162 else if (val2
== ISL29125_SENSING_RANGE_0
)
163 data
->conf1
&= ~ISL29125_MODE_RANGE
;
166 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
173 static irqreturn_t
isl29125_trigger_handler(int irq
, void *p
)
175 struct iio_poll_func
*pf
= p
;
176 struct iio_dev
*indio_dev
= pf
->indio_dev
;
177 struct isl29125_data
*data
= iio_priv(indio_dev
);
180 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
181 indio_dev
->masklength
) {
182 int ret
= i2c_smbus_read_word_data(data
->client
,
183 isl29125_regs
[i
].data
);
187 data
->buffer
[j
++] = ret
;
190 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
191 iio_get_time_ns(indio_dev
));
194 iio_trigger_notify_done(indio_dev
->trig
);
199 static IIO_CONST_ATTR(scale_available
, "0.005722 0.152590");
201 static struct attribute
*isl29125_attributes
[] = {
202 &iio_const_attr_scale_available
.dev_attr
.attr
,
206 static const struct attribute_group isl29125_attribute_group
= {
207 .attrs
= isl29125_attributes
,
210 static const struct iio_info isl29125_info
= {
211 .read_raw
= isl29125_read_raw
,
212 .write_raw
= isl29125_write_raw
,
213 .attrs
= &isl29125_attribute_group
,
216 static int isl29125_buffer_preenable(struct iio_dev
*indio_dev
)
218 struct isl29125_data
*data
= iio_priv(indio_dev
);
220 data
->conf1
|= ISL29125_MODE_RGB
;
221 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
225 static int isl29125_buffer_predisable(struct iio_dev
*indio_dev
)
227 struct isl29125_data
*data
= iio_priv(indio_dev
);
230 ret
= iio_triggered_buffer_predisable(indio_dev
);
234 data
->conf1
&= ~ISL29125_MODE_MASK
;
235 data
->conf1
|= ISL29125_MODE_PD
;
236 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
240 static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops
= {
241 .preenable
= isl29125_buffer_preenable
,
242 .postenable
= &iio_triggered_buffer_postenable
,
243 .predisable
= isl29125_buffer_predisable
,
246 static int isl29125_probe(struct i2c_client
*client
,
247 const struct i2c_device_id
*id
)
249 struct isl29125_data
*data
;
250 struct iio_dev
*indio_dev
;
253 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
254 if (indio_dev
== NULL
)
257 data
= iio_priv(indio_dev
);
258 i2c_set_clientdata(client
, indio_dev
);
259 data
->client
= client
;
261 indio_dev
->dev
.parent
= &client
->dev
;
262 indio_dev
->info
= &isl29125_info
;
263 indio_dev
->name
= ISL29125_DRV_NAME
;
264 indio_dev
->channels
= isl29125_channels
;
265 indio_dev
->num_channels
= ARRAY_SIZE(isl29125_channels
);
266 indio_dev
->modes
= INDIO_DIRECT_MODE
;
268 ret
= i2c_smbus_read_byte_data(data
->client
, ISL29125_DEVICE_ID
);
271 if (ret
!= ISL29125_ID
)
274 data
->conf1
= ISL29125_MODE_PD
| ISL29125_MODE_RANGE
;
275 ret
= i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
280 ret
= i2c_smbus_write_byte_data(data
->client
, ISL29125_STATUS
, 0);
284 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
285 isl29125_trigger_handler
, &isl29125_buffer_setup_ops
);
289 ret
= iio_device_register(indio_dev
);
296 iio_triggered_buffer_cleanup(indio_dev
);
300 static int isl29125_powerdown(struct isl29125_data
*data
)
302 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
303 (data
->conf1
& ~ISL29125_MODE_MASK
) | ISL29125_MODE_PD
);
306 static int isl29125_remove(struct i2c_client
*client
)
308 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
310 iio_device_unregister(indio_dev
);
311 iio_triggered_buffer_cleanup(indio_dev
);
312 isl29125_powerdown(iio_priv(indio_dev
));
317 #ifdef CONFIG_PM_SLEEP
318 static int isl29125_suspend(struct device
*dev
)
320 struct isl29125_data
*data
= iio_priv(i2c_get_clientdata(
321 to_i2c_client(dev
)));
322 return isl29125_powerdown(data
);
325 static int isl29125_resume(struct device
*dev
)
327 struct isl29125_data
*data
= iio_priv(i2c_get_clientdata(
328 to_i2c_client(dev
)));
329 return i2c_smbus_write_byte_data(data
->client
, ISL29125_CONF1
,
334 static SIMPLE_DEV_PM_OPS(isl29125_pm_ops
, isl29125_suspend
, isl29125_resume
);
336 static const struct i2c_device_id isl29125_id
[] = {
340 MODULE_DEVICE_TABLE(i2c
, isl29125_id
);
342 static struct i2c_driver isl29125_driver
= {
344 .name
= ISL29125_DRV_NAME
,
345 .pm
= &isl29125_pm_ops
,
347 .probe
= isl29125_probe
,
348 .remove
= isl29125_remove
,
349 .id_table
= isl29125_id
,
351 module_i2c_driver(isl29125_driver
);
353 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
354 MODULE_DESCRIPTION("ISL29125 RGB light sensor driver");
355 MODULE_LICENSE("GPL");