xref: /openbmc/linux/drivers/iio/light/tcs3472.c (revision 79f08d9e)
1 /*
2  * tcs3472.c - Support for TAOS TCS3472 color light-to-digital converter
3  *
4  * Copyright (c) 2013 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  * Color light sensor with 16-bit channels for red, green, blue, clear);
11  * 7-bit I2C slave address 0x39 (TCS34721, TCS34723) or 0x29 (TCS34725,
12  * TCS34727)
13  *
14  * TODO: interrupt support, thresholds, wait time
15  */
16 
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/delay.h>
20 #include <linux/pm.h>
21 
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/triggered_buffer.h>
27 
28 #define TCS3472_DRV_NAME "tcs3472"
29 
30 #define TCS3472_COMMAND BIT(7)
31 #define TCS3472_AUTO_INCR BIT(5)
32 
33 #define TCS3472_ENABLE (TCS3472_COMMAND | 0x00)
34 #define TCS3472_ATIME (TCS3472_COMMAND | 0x01)
35 #define TCS3472_WTIME (TCS3472_COMMAND | 0x03)
36 #define TCS3472_AILT (TCS3472_COMMAND | 0x04)
37 #define TCS3472_AIHT (TCS3472_COMMAND | 0x06)
38 #define TCS3472_PERS (TCS3472_COMMAND | 0x0c)
39 #define TCS3472_CONFIG (TCS3472_COMMAND | 0x0d)
40 #define TCS3472_CONTROL (TCS3472_COMMAND | 0x0f)
41 #define TCS3472_ID (TCS3472_COMMAND | 0x12)
42 #define TCS3472_STATUS (TCS3472_COMMAND | 0x13)
43 #define TCS3472_CDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x14)
44 #define TCS3472_RDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x16)
45 #define TCS3472_GDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x18)
46 #define TCS3472_BDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x1a)
47 
48 #define TCS3472_STATUS_AVALID BIT(0)
49 #define TCS3472_ENABLE_AEN BIT(1)
50 #define TCS3472_ENABLE_PON BIT(0)
51 #define TCS3472_CONTROL_AGAIN_MASK (BIT(0) | BIT(1))
52 
53 struct tcs3472_data {
54 	struct i2c_client *client;
55 	u8 enable;
56 	u8 control;
57 	u8 atime;
58 	u16 buffer[8]; /* 4 16-bit channels + 64-bit timestamp */
59 };
60 
61 #define TCS3472_CHANNEL(_color, _si, _addr) { \
62 	.type = IIO_INTENSITY, \
63 	.modified = 1, \
64 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
65 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBSCALE) | \
66 		BIT(IIO_CHAN_INFO_INT_TIME), \
67 	.channel2 = IIO_MOD_LIGHT_##_color, \
68 	.address = _addr, \
69 	.scan_index = _si, \
70 	.scan_type = IIO_ST('u', 16, 16, 0), \
71 }
72 
73 static const int tcs3472_agains[] = { 1, 4, 16, 60 };
74 
75 static const struct iio_chan_spec tcs3472_channels[] = {
76 	TCS3472_CHANNEL(CLEAR, 0, TCS3472_CDATA),
77 	TCS3472_CHANNEL(RED, 1, TCS3472_RDATA),
78 	TCS3472_CHANNEL(GREEN, 2, TCS3472_GDATA),
79 	TCS3472_CHANNEL(BLUE, 3, TCS3472_BDATA),
80 	IIO_CHAN_SOFT_TIMESTAMP(4),
81 };
82 
83 static int tcs3472_req_data(struct tcs3472_data *data)
84 {
85 	int tries = 50;
86 	int ret;
87 
88 	while (tries--) {
89 		ret = i2c_smbus_read_byte_data(data->client, TCS3472_STATUS);
90 		if (ret < 0)
91 			return ret;
92 		if (ret & TCS3472_STATUS_AVALID)
93 			break;
94 		msleep(20);
95 	}
96 
97 	if (tries < 0) {
98 		dev_err(&data->client->dev, "data not ready\n");
99 		return -EIO;
100 	}
101 
102 	return 0;
103 }
104 
105 static int tcs3472_read_raw(struct iio_dev *indio_dev,
106 			   struct iio_chan_spec const *chan,
107 			   int *val, int *val2, long mask)
108 {
109 	struct tcs3472_data *data = iio_priv(indio_dev);
110 	int ret;
111 
112 	switch (mask) {
113 	case IIO_CHAN_INFO_RAW:
114 		ret = tcs3472_req_data(data);
115 		if (ret < 0)
116 			return ret;
117 		ret = i2c_smbus_read_word_data(data->client, chan->address);
118 		if (ret < 0)
119 			return ret;
120 		*val = ret;
121 		return IIO_VAL_INT;
122 	case IIO_CHAN_INFO_CALIBSCALE:
123 		*val = tcs3472_agains[data->control &
124 			TCS3472_CONTROL_AGAIN_MASK];
125 		return IIO_VAL_INT;
126 	case IIO_CHAN_INFO_INT_TIME:
127 		*val = 0;
128 		*val2 = (256 - data->atime) * 2400;
129 		return IIO_VAL_INT_PLUS_MICRO;
130 	}
131 	return -EINVAL;
132 }
133 
134 static int tcs3472_write_raw(struct iio_dev *indio_dev,
135 			       struct iio_chan_spec const *chan,
136 			       int val, int val2, long mask)
137 {
138 	struct tcs3472_data *data = iio_priv(indio_dev);
139 	int i;
140 
141 	switch (mask) {
142 	case IIO_CHAN_INFO_CALIBSCALE:
143 		if (val2 != 0)
144 			return -EINVAL;
145 		for (i = 0; i < ARRAY_SIZE(tcs3472_agains); i++) {
146 			if (val == tcs3472_agains[i]) {
147 				data->control &= ~TCS3472_CONTROL_AGAIN_MASK;
148 				data->control |= i;
149 				return i2c_smbus_write_byte_data(
150 					data->client, TCS3472_CONTROL,
151 					data->control);
152 			}
153 		}
154 		return -EINVAL;
155 	case IIO_CHAN_INFO_INT_TIME:
156 		if (val != 0)
157 			return -EINVAL;
158 		for (i = 0; i < 256; i++) {
159 			if (val2 == (256 - i) * 2400) {
160 				data->atime = i;
161 				return i2c_smbus_write_word_data(
162 					data->client, TCS3472_ATIME,
163 					data->atime);
164 			}
165 
166 		}
167 		return -EINVAL;
168 	}
169 	return -EINVAL;
170 }
171 
172 static irqreturn_t tcs3472_trigger_handler(int irq, void *p)
173 {
174 	struct iio_poll_func *pf = p;
175 	struct iio_dev *indio_dev = pf->indio_dev;
176 	struct tcs3472_data *data = iio_priv(indio_dev);
177 	int len = 0;
178 	int i, j = 0;
179 
180 	int ret = tcs3472_req_data(data);
181 	if (ret < 0)
182 		goto done;
183 
184 	for_each_set_bit(i, indio_dev->active_scan_mask,
185 		indio_dev->masklength) {
186 		ret = i2c_smbus_read_word_data(data->client,
187 			TCS3472_CDATA + 2*i);
188 		if (ret < 0)
189 			goto done;
190 
191 		data->buffer[j++] = ret;
192 		len += 2;
193 	}
194 
195 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
196 		iio_get_time_ns());
197 
198 done:
199 	iio_trigger_notify_done(indio_dev->trig);
200 
201 	return IRQ_HANDLED;
202 }
203 
204 static ssize_t tcs3472_show_int_time_available(struct device *dev,
205 					struct device_attribute *attr,
206 					char *buf)
207 {
208 	size_t len = 0;
209 	int i;
210 
211 	for (i = 1; i <= 256; i++)
212 		len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06d ",
213 			2400 * i);
214 
215 	/* replace trailing space by newline */
216 	buf[len - 1] = '\n';
217 
218 	return len;
219 }
220 
221 static IIO_CONST_ATTR(calibscale_available, "1 4 16 60");
222 static IIO_DEV_ATTR_INT_TIME_AVAIL(tcs3472_show_int_time_available);
223 
224 static struct attribute *tcs3472_attributes[] = {
225 	&iio_const_attr_calibscale_available.dev_attr.attr,
226 	&iio_dev_attr_integration_time_available.dev_attr.attr,
227 	NULL
228 };
229 
230 static const struct attribute_group tcs3472_attribute_group = {
231 	.attrs = tcs3472_attributes,
232 };
233 
234 static const struct iio_info tcs3472_info = {
235 	.read_raw = tcs3472_read_raw,
236 	.write_raw = tcs3472_write_raw,
237 	.attrs = &tcs3472_attribute_group,
238 	.driver_module = THIS_MODULE,
239 };
240 
241 static int tcs3472_probe(struct i2c_client *client,
242 			   const struct i2c_device_id *id)
243 {
244 	struct tcs3472_data *data;
245 	struct iio_dev *indio_dev;
246 	int ret;
247 
248 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
249 	if (indio_dev == NULL)
250 		return -ENOMEM;
251 
252 	data = iio_priv(indio_dev);
253 	i2c_set_clientdata(client, indio_dev);
254 	data->client = client;
255 
256 	indio_dev->dev.parent = &client->dev;
257 	indio_dev->info = &tcs3472_info;
258 	indio_dev->name = TCS3472_DRV_NAME;
259 	indio_dev->channels = tcs3472_channels;
260 	indio_dev->num_channels = ARRAY_SIZE(tcs3472_channels);
261 	indio_dev->modes = INDIO_DIRECT_MODE;
262 
263 	ret = i2c_smbus_read_byte_data(data->client, TCS3472_ID);
264 	if (ret < 0)
265 		return ret;
266 
267 	if (ret == 0x44)
268 		dev_info(&client->dev, "TCS34721/34725 found\n");
269 	else if (ret == 0x4d)
270 		dev_info(&client->dev, "TCS34723/34727 found\n");
271 	else
272 		return -ENODEV;
273 
274 	ret = i2c_smbus_read_byte_data(data->client, TCS3472_CONTROL);
275 	if (ret < 0)
276 		return ret;
277 	data->control = ret;
278 
279 	ret = i2c_smbus_read_byte_data(data->client, TCS3472_ATIME);
280 	if (ret < 0)
281 		return ret;
282 	data->atime = ret;
283 
284 	ret = i2c_smbus_read_byte_data(data->client, TCS3472_ENABLE);
285 	if (ret < 0)
286 		return ret;
287 
288 	/* enable device */
289 	data->enable = ret | TCS3472_ENABLE_PON | TCS3472_ENABLE_AEN;
290 	ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
291 		data->enable);
292 	if (ret < 0)
293 		return ret;
294 
295 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
296 		tcs3472_trigger_handler, NULL);
297 	if (ret < 0)
298 		return ret;
299 
300 	ret = iio_device_register(indio_dev);
301 	if (ret < 0)
302 		goto buffer_cleanup;
303 
304 	return 0;
305 
306 buffer_cleanup:
307 	iio_triggered_buffer_cleanup(indio_dev);
308 	return ret;
309 }
310 
311 static int tcs3472_powerdown(struct tcs3472_data *data)
312 {
313 	return i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
314 		data->enable & ~(TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON));
315 }
316 
317 static int tcs3472_remove(struct i2c_client *client)
318 {
319 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
320 
321 	iio_device_unregister(indio_dev);
322 	iio_triggered_buffer_cleanup(indio_dev);
323 	tcs3472_powerdown(iio_priv(indio_dev));
324 
325 	return 0;
326 }
327 
328 #ifdef CONFIG_PM_SLEEP
329 static int tcs3472_suspend(struct device *dev)
330 {
331 	struct tcs3472_data *data = iio_priv(i2c_get_clientdata(
332 		to_i2c_client(dev)));
333 	return tcs3472_powerdown(data);
334 }
335 
336 static int tcs3472_resume(struct device *dev)
337 {
338 	struct tcs3472_data *data = iio_priv(i2c_get_clientdata(
339 		to_i2c_client(dev)));
340 	return i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
341 		data->enable | (TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON));
342 }
343 #endif
344 
345 static SIMPLE_DEV_PM_OPS(tcs3472_pm_ops, tcs3472_suspend, tcs3472_resume);
346 
347 static const struct i2c_device_id tcs3472_id[] = {
348 	{ "tcs3472", 0 },
349 	{ }
350 };
351 MODULE_DEVICE_TABLE(i2c, tcs3472_id);
352 
353 static struct i2c_driver tcs3472_driver = {
354 	.driver = {
355 		.name	= TCS3472_DRV_NAME,
356 		.pm	= &tcs3472_pm_ops,
357 		.owner	= THIS_MODULE,
358 	},
359 	.probe		= tcs3472_probe,
360 	.remove		= tcs3472_remove,
361 	.id_table	= tcs3472_id,
362 };
363 module_i2c_driver(tcs3472_driver);
364 
365 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
366 MODULE_DESCRIPTION("TCS3472 color light sensors driver");
367 MODULE_LICENSE("GPL");
368