xref: /openbmc/linux/drivers/iio/light/adjd_s311.c (revision 12eb4683)
1 /*
2  * adjd_s311.c - Support for ADJD-S311-CR999 digital color sensor
3  *
4  * Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net>
5  *
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.
9  *
10  * driver for ADJD-S311-CR999 digital color sensor (10-bit channels for
11  * red, green, blue, clear); 7-bit I2C slave address 0x74
12  *
13  * limitations: no calibration, no offset mode, no sleep mode
14  */
15 
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>
25 
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>
31 
32 #define ADJD_S311_DRV_NAME "adjd_s311"
33 
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
52 
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
58 
59 struct adjd_s311_data {
60 	struct i2c_client *client;
61 	u16 *buffer;
62 };
63 
64 enum adjd_s311_channel_idx {
65 	IDX_RED, IDX_GREEN, IDX_BLUE, IDX_CLEAR
66 };
67 
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))
71 
72 static int adjd_s311_req_data(struct iio_dev *indio_dev)
73 {
74 	struct adjd_s311_data *data = iio_priv(indio_dev);
75 	int tries = 10;
76 
77 	int ret = i2c_smbus_write_byte_data(data->client, ADJD_S311_CTRL,
78 		ADJD_S311_CTRL_GSSR);
79 	if (ret < 0)
80 		return ret;
81 
82 	while (tries--) {
83 		ret = i2c_smbus_read_byte_data(data->client, ADJD_S311_CTRL);
84 		if (ret < 0)
85 			return ret;
86 		if (!(ret & ADJD_S311_CTRL_GSSR))
87 			break;
88 		msleep(20);
89 	}
90 
91 	if (tries < 0) {
92 		dev_err(&data->client->dev,
93 			"adjd_s311_req_data() failed, data not ready\n");
94 		return -EIO;
95 	}
96 
97 	return 0;
98 }
99 
100 static int adjd_s311_read_data(struct iio_dev *indio_dev, u8 reg, int *val)
101 {
102 	struct adjd_s311_data *data = iio_priv(indio_dev);
103 
104 	int ret = adjd_s311_req_data(indio_dev);
105 	if (ret < 0)
106 		return ret;
107 
108 	ret = i2c_smbus_read_word_data(data->client, reg);
109 	if (ret < 0)
110 		return ret;
111 
112 	*val = ret & ADJD_S311_DATA_MASK;
113 
114 	return 0;
115 }
116 
117 static irqreturn_t adjd_s311_trigger_handler(int irq, void *p)
118 {
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();
123 	int len = 0;
124 	int i, j = 0;
125 
126 	int ret = adjd_s311_req_data(indio_dev);
127 	if (ret < 0)
128 		goto done;
129 
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));
134 		if (ret < 0)
135 			goto done;
136 
137 		data->buffer[j++] = ret & ADJD_S311_DATA_MASK;
138 		len += 2;
139 	}
140 
141 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, time_ns);
142 
143 done:
144 	iio_trigger_notify_done(indio_dev->trig);
145 
146 	return IRQ_HANDLED;
147 }
148 
149 #define ADJD_S311_CHANNEL(_color, _scan_idx) { \
150 	.type = IIO_INTENSITY, \
151 	.modified = 1, \
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), \
158 	.scan_type = IIO_ST('u', 10, 16, 0), \
159 }
160 
161 static const struct iio_chan_spec adjd_s311_channels[] = {
162 	ADJD_S311_CHANNEL(RED, 0),
163 	ADJD_S311_CHANNEL(GREEN, 1),
164 	ADJD_S311_CHANNEL(BLUE, 2),
165 	ADJD_S311_CHANNEL(CLEAR, 3),
166 	IIO_CHAN_SOFT_TIMESTAMP(4),
167 };
168 
169 static int adjd_s311_read_raw(struct iio_dev *indio_dev,
170 			   struct iio_chan_spec const *chan,
171 			   int *val, int *val2, long mask)
172 {
173 	struct adjd_s311_data *data = iio_priv(indio_dev);
174 	int ret;
175 
176 	switch (mask) {
177 	case IIO_CHAN_INFO_RAW:
178 		ret = adjd_s311_read_data(indio_dev,
179 			ADJD_S311_DATA_REG(chan->address), val);
180 		if (ret < 0)
181 			return ret;
182 		return IIO_VAL_INT;
183 	case IIO_CHAN_INFO_HARDWAREGAIN:
184 		ret = i2c_smbus_read_byte_data(data->client,
185 			ADJD_S311_CAP_REG(chan->address));
186 		if (ret < 0)
187 			return ret;
188 		*val = ret & ADJD_S311_CAP_MASK;
189 		return IIO_VAL_INT;
190 	case IIO_CHAN_INFO_INT_TIME:
191 		ret = i2c_smbus_read_word_data(data->client,
192 			ADJD_S311_INT_REG(chan->address));
193 		if (ret < 0)
194 			return ret;
195 		*val = 0;
196 		/*
197 		 * not documented, based on measurement:
198 		 * 4095 LSBs correspond to roughly 4 ms
199 		 */
200 		*val2 = ret & ADJD_S311_INT_MASK;
201 		return IIO_VAL_INT_PLUS_MICRO;
202 	}
203 	return -EINVAL;
204 }
205 
206 static int adjd_s311_write_raw(struct iio_dev *indio_dev,
207 			       struct iio_chan_spec const *chan,
208 			       int val, int val2, long mask)
209 {
210 	struct adjd_s311_data *data = iio_priv(indio_dev);
211 
212 	switch (mask) {
213 	case IIO_CHAN_INFO_HARDWAREGAIN:
214 		if (val < 0 || val > ADJD_S311_CAP_MASK)
215 			return -EINVAL;
216 
217 		return i2c_smbus_write_byte_data(data->client,
218 			ADJD_S311_CAP_REG(chan->address), val);
219 	case IIO_CHAN_INFO_INT_TIME:
220 		if (val != 0 || val2 < 0 || val2 > ADJD_S311_INT_MASK)
221 			return -EINVAL;
222 
223 		return i2c_smbus_write_word_data(data->client,
224 			ADJD_S311_INT_REG(chan->address), val2);
225 	}
226 	return -EINVAL;
227 }
228 
229 static int adjd_s311_update_scan_mode(struct iio_dev *indio_dev,
230 	const unsigned long *scan_mask)
231 {
232 	struct adjd_s311_data *data = iio_priv(indio_dev);
233 
234 	kfree(data->buffer);
235 	data->buffer = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
236 	if (data->buffer == NULL)
237 		return -ENOMEM;
238 
239 	return 0;
240 }
241 
242 static const struct iio_info adjd_s311_info = {
243 	.read_raw = adjd_s311_read_raw,
244 	.write_raw = adjd_s311_write_raw,
245 	.update_scan_mode = adjd_s311_update_scan_mode,
246 	.driver_module = THIS_MODULE,
247 };
248 
249 static int adjd_s311_probe(struct i2c_client *client,
250 			   const struct i2c_device_id *id)
251 {
252 	struct adjd_s311_data *data;
253 	struct iio_dev *indio_dev;
254 	int err;
255 
256 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
257 	if (indio_dev == NULL)
258 		return -ENOMEM;
259 
260 	data = iio_priv(indio_dev);
261 	i2c_set_clientdata(client, indio_dev);
262 	data->client = client;
263 
264 	indio_dev->dev.parent = &client->dev;
265 	indio_dev->info = &adjd_s311_info;
266 	indio_dev->name = ADJD_S311_DRV_NAME;
267 	indio_dev->channels = adjd_s311_channels;
268 	indio_dev->num_channels = ARRAY_SIZE(adjd_s311_channels);
269 	indio_dev->modes = INDIO_DIRECT_MODE;
270 
271 	err = iio_triggered_buffer_setup(indio_dev, NULL,
272 		adjd_s311_trigger_handler, NULL);
273 	if (err < 0)
274 		return err;
275 
276 	err = iio_device_register(indio_dev);
277 	if (err)
278 		goto exit_unreg_buffer;
279 
280 	dev_info(&client->dev, "ADJD-S311 color sensor registered\n");
281 
282 	return 0;
283 
284 exit_unreg_buffer:
285 	iio_triggered_buffer_cleanup(indio_dev);
286 	return err;
287 }
288 
289 static int adjd_s311_remove(struct i2c_client *client)
290 {
291 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
292 	struct adjd_s311_data *data = iio_priv(indio_dev);
293 
294 	iio_device_unregister(indio_dev);
295 	iio_triggered_buffer_cleanup(indio_dev);
296 	kfree(data->buffer);
297 
298 	return 0;
299 }
300 
301 static const struct i2c_device_id adjd_s311_id[] = {
302 	{ "adjd_s311", 0 },
303 	{ }
304 };
305 MODULE_DEVICE_TABLE(i2c, adjd_s311_id);
306 
307 static struct i2c_driver adjd_s311_driver = {
308 	.driver = {
309 		.name	= ADJD_S311_DRV_NAME,
310 	},
311 	.probe		= adjd_s311_probe,
312 	.remove		= adjd_s311_remove,
313 	.id_table	= adjd_s311_id,
314 };
315 module_i2c_driver(adjd_s311_driver);
316 
317 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
318 MODULE_DESCRIPTION("ADJD-S311 color sensor");
319 MODULE_LICENSE("GPL");
320