2 * adjd_s311.c - Support for ADJD-S311-CR999 digital color sensor
4 * Copyright (C) 2012 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 * driver for ADJD-S311-CR999 digital color sensor (10-bit channels for
11 * red, green, blue, clear); 7-bit I2C slave address 0x74
13 * limitations: no calibration, no offset mode, no sleep mode
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/bitmap.h>
23 #include <linux/err.h>
24 #include <linux/irq.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/triggered_buffer.h>
32 #define ADJD_S311_DRV_NAME "adjd_s311"
34 #define ADJD_S311_CTRL 0x00
35 #define ADJD_S311_CONFIG 0x01
36 #define ADJD_S311_CAP_RED 0x06
37 #define ADJD_S311_CAP_GREEN 0x07
38 #define ADJD_S311_CAP_BLUE 0x08
39 #define ADJD_S311_CAP_CLEAR 0x09
40 #define ADJD_S311_INT_RED 0x0a
41 #define ADJD_S311_INT_GREEN 0x0c
42 #define ADJD_S311_INT_BLUE 0x0e
43 #define ADJD_S311_INT_CLEAR 0x10
44 #define ADJD_S311_DATA_RED 0x40
45 #define ADJD_S311_DATA_GREEN 0x42
46 #define ADJD_S311_DATA_BLUE 0x44
47 #define ADJD_S311_DATA_CLEAR 0x46
48 #define ADJD_S311_OFFSET_RED 0x48
49 #define ADJD_S311_OFFSET_GREEN 0x49
50 #define ADJD_S311_OFFSET_BLUE 0x4a
51 #define ADJD_S311_OFFSET_CLEAR 0x4b
53 #define ADJD_S311_CTRL_GOFS 0x02
54 #define ADJD_S311_CTRL_GSSR 0x01
55 #define ADJD_S311_CAP_MASK 0x0f
56 #define ADJD_S311_INT_MASK 0x0fff
57 #define ADJD_S311_DATA_MASK 0x03ff
59 struct adjd_s311_data
{
60 struct i2c_client
*client
;
64 enum adjd_s311_channel_idx
{
65 IDX_RED
, IDX_GREEN
, IDX_BLUE
, IDX_CLEAR
68 #define ADJD_S311_DATA_REG(chan) (ADJD_S311_DATA_RED + (chan) * 2)
69 #define ADJD_S311_INT_REG(chan) (ADJD_S311_INT_RED + (chan) * 2)
70 #define ADJD_S311_CAP_REG(chan) (ADJD_S311_CAP_RED + (chan))
72 static int adjd_s311_req_data(struct iio_dev
*indio_dev
)
74 struct adjd_s311_data
*data
= iio_priv(indio_dev
);
77 int ret
= i2c_smbus_write_byte_data(data
->client
, ADJD_S311_CTRL
,
83 ret
= i2c_smbus_read_byte_data(data
->client
, ADJD_S311_CTRL
);
86 if (!(ret
& ADJD_S311_CTRL_GSSR
))
92 dev_err(&data
->client
->dev
,
93 "adjd_s311_req_data() failed, data not ready\n");
100 static int adjd_s311_read_data(struct iio_dev
*indio_dev
, u8 reg
, int *val
)
102 struct adjd_s311_data
*data
= iio_priv(indio_dev
);
104 int ret
= adjd_s311_req_data(indio_dev
);
108 ret
= i2c_smbus_read_word_data(data
->client
, reg
);
112 *val
= ret
& ADJD_S311_DATA_MASK
;
117 static irqreturn_t
adjd_s311_trigger_handler(int irq
, void *p
)
119 struct iio_poll_func
*pf
= p
;
120 struct iio_dev
*indio_dev
= pf
->indio_dev
;
121 struct adjd_s311_data
*data
= iio_priv(indio_dev
);
122 s64 time_ns
= iio_get_time_ns();
126 int ret
= adjd_s311_req_data(indio_dev
);
130 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
131 indio_dev
->masklength
) {
132 ret
= i2c_smbus_read_word_data(data
->client
,
133 ADJD_S311_DATA_REG(i
));
137 data
->buffer
[j
++] = ret
& ADJD_S311_DATA_MASK
;
141 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
, time_ns
);
144 iio_trigger_notify_done(indio_dev
->trig
);
149 #define ADJD_S311_CHANNEL(_color, _scan_idx) { \
150 .type = IIO_INTENSITY, \
152 .address = (IDX_##_color), \
153 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
154 BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
155 BIT(IIO_CHAN_INFO_INT_TIME), \
156 .channel2 = (IIO_MOD_LIGHT_##_color), \
157 .scan_index = (_scan_idx), \
162 .endianness = IIO_CPU, \
166 static const struct iio_chan_spec adjd_s311_channels
[] = {
167 ADJD_S311_CHANNEL(RED
, 0),
168 ADJD_S311_CHANNEL(GREEN
, 1),
169 ADJD_S311_CHANNEL(BLUE
, 2),
170 ADJD_S311_CHANNEL(CLEAR
, 3),
171 IIO_CHAN_SOFT_TIMESTAMP(4),
174 static int adjd_s311_read_raw(struct iio_dev
*indio_dev
,
175 struct iio_chan_spec
const *chan
,
176 int *val
, int *val2
, long mask
)
178 struct adjd_s311_data
*data
= iio_priv(indio_dev
);
182 case IIO_CHAN_INFO_RAW
:
183 ret
= adjd_s311_read_data(indio_dev
,
184 ADJD_S311_DATA_REG(chan
->address
), val
);
188 case IIO_CHAN_INFO_HARDWAREGAIN
:
189 ret
= i2c_smbus_read_byte_data(data
->client
,
190 ADJD_S311_CAP_REG(chan
->address
));
193 *val
= ret
& ADJD_S311_CAP_MASK
;
195 case IIO_CHAN_INFO_INT_TIME
:
196 ret
= i2c_smbus_read_word_data(data
->client
,
197 ADJD_S311_INT_REG(chan
->address
));
202 * not documented, based on measurement:
203 * 4095 LSBs correspond to roughly 4 ms
205 *val2
= ret
& ADJD_S311_INT_MASK
;
206 return IIO_VAL_INT_PLUS_MICRO
;
211 static int adjd_s311_write_raw(struct iio_dev
*indio_dev
,
212 struct iio_chan_spec
const *chan
,
213 int val
, int val2
, long mask
)
215 struct adjd_s311_data
*data
= iio_priv(indio_dev
);
218 case IIO_CHAN_INFO_HARDWAREGAIN
:
219 if (val
< 0 || val
> ADJD_S311_CAP_MASK
)
222 return i2c_smbus_write_byte_data(data
->client
,
223 ADJD_S311_CAP_REG(chan
->address
), val
);
224 case IIO_CHAN_INFO_INT_TIME
:
225 if (val
!= 0 || val2
< 0 || val2
> ADJD_S311_INT_MASK
)
228 return i2c_smbus_write_word_data(data
->client
,
229 ADJD_S311_INT_REG(chan
->address
), val2
);
234 static int adjd_s311_update_scan_mode(struct iio_dev
*indio_dev
,
235 const unsigned long *scan_mask
)
237 struct adjd_s311_data
*data
= iio_priv(indio_dev
);
240 data
->buffer
= kmalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
241 if (data
->buffer
== NULL
)
247 static const struct iio_info adjd_s311_info
= {
248 .read_raw
= adjd_s311_read_raw
,
249 .write_raw
= adjd_s311_write_raw
,
250 .update_scan_mode
= adjd_s311_update_scan_mode
,
251 .driver_module
= THIS_MODULE
,
254 static int adjd_s311_probe(struct i2c_client
*client
,
255 const struct i2c_device_id
*id
)
257 struct adjd_s311_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
= &adjd_s311_info
;
271 indio_dev
->name
= ADJD_S311_DRV_NAME
;
272 indio_dev
->channels
= adjd_s311_channels
;
273 indio_dev
->num_channels
= ARRAY_SIZE(adjd_s311_channels
);
274 indio_dev
->modes
= INDIO_DIRECT_MODE
;
276 err
= iio_triggered_buffer_setup(indio_dev
, NULL
,
277 adjd_s311_trigger_handler
, NULL
);
281 err
= iio_device_register(indio_dev
);
283 goto exit_unreg_buffer
;
285 dev_info(&client
->dev
, "ADJD-S311 color sensor registered\n");
290 iio_triggered_buffer_cleanup(indio_dev
);
294 static int adjd_s311_remove(struct i2c_client
*client
)
296 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
297 struct adjd_s311_data
*data
= iio_priv(indio_dev
);
299 iio_device_unregister(indio_dev
);
300 iio_triggered_buffer_cleanup(indio_dev
);
306 static const struct i2c_device_id adjd_s311_id
[] = {
310 MODULE_DEVICE_TABLE(i2c
, adjd_s311_id
);
312 static struct i2c_driver adjd_s311_driver
= {
314 .name
= ADJD_S311_DRV_NAME
,
316 .probe
= adjd_s311_probe
,
317 .remove
= adjd_s311_remove
,
318 .id_table
= adjd_s311_id
,
320 module_i2c_driver(adjd_s311_driver
);
322 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
323 MODULE_DESCRIPTION("ADJD-S311 color sensor");
324 MODULE_LICENSE("GPL");