16 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
22 #include <linux/bitmap.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_LO 0x0a
41 #define ADJD_S311_INT_RED_HI 0x0b
42 #define ADJD_S311_INT_GREEN_LO 0x0c
43 #define ADJD_S311_INT_GREEN_HI 0x0d
44 #define ADJD_S311_INT_BLUE_LO 0x0e
45 #define ADJD_S311_INT_BLUE_HI 0x0f
46 #define ADJD_S311_INT_CLEAR_LO 0x10
47 #define ADJD_S311_INT_CLEAR_HI 0x11
48 #define ADJD_S311_DATA_RED_LO 0x40
49 #define ADJD_S311_DATA_RED_HI 0x41
50 #define ADJD_S311_DATA_GREEN_LO 0x42
51 #define ADJD_S311_DATA_GREEN_HI 0x43
52 #define ADJD_S311_DATA_BLUE_LO 0x44
53 #define ADJD_S311_DATA_BLUE_HI 0x45
54 #define ADJD_S311_DATA_CLEAR_LO 0x46
55 #define ADJD_S311_DATA_CLEAR_HI 0x47
56 #define ADJD_S311_OFFSET_RED 0x48
57 #define ADJD_S311_OFFSET_GREEN 0x49
58 #define ADJD_S311_OFFSET_BLUE 0x4a
59 #define ADJD_S311_OFFSET_CLEAR 0x4b
61 #define ADJD_S311_CTRL_GOFS 0x02
62 #define ADJD_S311_CTRL_GSSR 0x01
63 #define ADJD_S311_CAP_MASK 0x0f
64 #define ADJD_S311_INT_MASK 0x0fff
65 #define ADJD_S311_DATA_MASK 0x03ff
76 #define ADJD_S311_DATA_REG(chan) (ADJD_S311_DATA_RED_LO + (chan) * 2)
77 #define ADJD_S311_INT_REG(chan) (ADJD_S311_INT_RED_LO + (chan) * 2)
78 #define ADJD_S311_CAP_REG(chan) (ADJD_S311_CAP_RED + (chan))
80 static int adjd_s311_req_data(
struct iio_dev *indio_dev)
101 "adjd_s311_req_data() failed, data not ready\n");
108 static int adjd_s311_read_data(
struct iio_dev *indio_dev,
u8 reg,
int *
val)
112 int ret = adjd_s311_req_data(indio_dev);
139 static ssize_t adjd_s311_write_int_time(
struct iio_dev *indio_dev,
144 unsigned long int_time;
147 ret = kstrtoul(buf, 10, &int_time);
162 static irqreturn_t adjd_s311_trigger_handler(
int irq,
void *
p)
168 s64 time_ns = iio_get_time_ns();
172 int ret = adjd_s311_req_data(indio_dev);
200 .name =
"integration_time",
201 .read = adjd_s311_read_int_time,
202 .write = adjd_s311_write_int_time,
207 #define ADJD_S311_CHANNEL(_color, _scan_idx) { \
208 .type = IIO_INTENSITY, \
210 .address = (IDX_##_color), \
211 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
212 IIO_CHAN_INFO_HARDWAREGAIN_SEPARATE_BIT, \
213 .channel2 = (IIO_MOD_LIGHT_##_color), \
214 .scan_index = (_scan_idx), \
215 .scan_type = IIO_ST('u', 10, 16, 0), \
216 .ext_info = adjd_s311_ext_info, \
227 static int adjd_s311_read_raw(
struct iio_dev *indio_dev,
229 int *val,
int *val2,
long mask)
236 ret = adjd_s311_read_data(indio_dev, chan->
address, val);
251 static int adjd_s311_write_raw(
struct iio_dev *indio_dev,
253 int val,
int val2,
long mask)
270 static int adjd_s311_update_scan_mode(
struct iio_dev *indio_dev,
271 const unsigned long *scan_mask)
283 static const struct iio_info adjd_s311_info = {
284 .read_raw = adjd_s311_read_raw,
285 .write_raw = adjd_s311_write_raw,
286 .update_scan_mode = adjd_s311_update_scan_mode,
298 if (indio_dev ==
NULL) {
302 data = iio_priv(indio_dev);
303 i2c_set_clientdata(client, indio_dev);
306 indio_dev->
dev.parent = &client->
dev;
307 indio_dev->
info = &adjd_s311_info;
309 indio_dev->
channels = adjd_s311_channels;
314 adjd_s311_trigger_handler,
NULL);
316 goto exit_free_device;
320 goto exit_unreg_buffer;
322 dev_info(&client->
dev,
"ADJD-S311 color sensor registered\n");
336 struct iio_dev *indio_dev = i2c_get_clientdata(client);
357 .probe = adjd_s311_probe,
359 .id_table = adjd_s311_id,