xref: /openbmc/linux/drivers/iio/light/tcs3414.c (revision 4eb5928d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tcs3414.c - Support for TAOS TCS3414 digital color sensor
4  *
5  * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
6  *
7  * Digital color sensor with 16-bit channels for red, green, blue, clear);
8  * 7-bit I2C slave address 0x39 (TCS3414) or 0x29, 0x49, 0x59 (TCS3413,
9  * TCS3415, TCS3416, resp.)
10  *
11  * TODO: sync, interrupt support, thresholds, prescaler
12  */
13 
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/delay.h>
17 #include <linux/pm.h>
18 
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/triggered_buffer.h>
24 
25 #define TCS3414_DRV_NAME "tcs3414"
26 
27 #define TCS3414_COMMAND BIT(7)
28 #define TCS3414_COMMAND_WORD (TCS3414_COMMAND | BIT(5))
29 
30 #define TCS3414_CONTROL (TCS3414_COMMAND | 0x00)
31 #define TCS3414_TIMING (TCS3414_COMMAND | 0x01)
32 #define TCS3414_ID (TCS3414_COMMAND | 0x04)
33 #define TCS3414_GAIN (TCS3414_COMMAND | 0x07)
34 #define TCS3414_DATA_GREEN (TCS3414_COMMAND_WORD | 0x10)
35 #define TCS3414_DATA_RED (TCS3414_COMMAND_WORD | 0x12)
36 #define TCS3414_DATA_BLUE (TCS3414_COMMAND_WORD | 0x14)
37 #define TCS3414_DATA_CLEAR (TCS3414_COMMAND_WORD | 0x16)
38 
39 #define TCS3414_CONTROL_ADC_VALID BIT(4)
40 #define TCS3414_CONTROL_ADC_EN BIT(1)
41 #define TCS3414_CONTROL_POWER BIT(0)
42 
43 #define TCS3414_INTEG_MASK GENMASK(1, 0)
44 #define TCS3414_INTEG_12MS 0x0
45 #define TCS3414_INTEG_100MS 0x1
46 #define TCS3414_INTEG_400MS 0x2
47 
48 #define TCS3414_GAIN_MASK GENMASK(5, 4)
49 #define TCS3414_GAIN_SHIFT 4
50 
51 struct tcs3414_data {
52 	struct i2c_client *client;
53 	u8 control;
54 	u8 gain;
55 	u8 timing;
56 	u16 buffer[8]; /* 4x 16-bit + 8 bytes timestamp */
57 };
58 
59 #define TCS3414_CHANNEL(_color, _si, _addr) { \
60 	.type = IIO_INTENSITY, \
61 	.modified = 1, \
62 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
63 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
64 		BIT(IIO_CHAN_INFO_INT_TIME), \
65 	.channel2 = IIO_MOD_LIGHT_##_color, \
66 	.address = _addr, \
67 	.scan_index = _si, \
68 	.scan_type = { \
69 		.sign = 'u', \
70 		.realbits = 16, \
71 		.storagebits = 16, \
72 		.endianness = IIO_CPU, \
73 	}, \
74 }
75 
76 /* scale factors: 1/gain */
77 static const int tcs3414_scales[][2] = {
78 	{1, 0}, {0, 250000}, {0, 62500}, {0, 15625}
79 };
80 
81 /* integration time in ms */
82 static const int tcs3414_times[] = { 12, 100, 400 };
83 
84 static const struct iio_chan_spec tcs3414_channels[] = {
85 	TCS3414_CHANNEL(GREEN, 0, TCS3414_DATA_GREEN),
86 	TCS3414_CHANNEL(RED, 1, TCS3414_DATA_RED),
87 	TCS3414_CHANNEL(BLUE, 2, TCS3414_DATA_BLUE),
88 	TCS3414_CHANNEL(CLEAR, 3, TCS3414_DATA_CLEAR),
89 	IIO_CHAN_SOFT_TIMESTAMP(4),
90 };
91 
92 static int tcs3414_req_data(struct tcs3414_data *data)
93 {
94 	int tries = 25;
95 	int ret;
96 
97 	ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
98 		data->control | TCS3414_CONTROL_ADC_EN);
99 	if (ret < 0)
100 		return ret;
101 
102 	while (tries--) {
103 		ret = i2c_smbus_read_byte_data(data->client, TCS3414_CONTROL);
104 		if (ret < 0)
105 			return ret;
106 		if (ret & TCS3414_CONTROL_ADC_VALID)
107 			break;
108 		msleep(20);
109 	}
110 
111 	ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
112 		data->control);
113 	if (ret < 0)
114 		return ret;
115 
116 	if (tries < 0) {
117 		dev_err(&data->client->dev, "data not ready\n");
118 		return -EIO;
119 	}
120 
121 	return 0;
122 }
123 
124 static int tcs3414_read_raw(struct iio_dev *indio_dev,
125 			   struct iio_chan_spec const *chan,
126 			   int *val, int *val2, long mask)
127 {
128 	struct tcs3414_data *data = iio_priv(indio_dev);
129 	int i, ret;
130 
131 	switch (mask) {
132 	case IIO_CHAN_INFO_RAW:
133 		ret = iio_device_claim_direct_mode(indio_dev);
134 		if (ret)
135 			return ret;
136 		ret = tcs3414_req_data(data);
137 		if (ret < 0) {
138 			iio_device_release_direct_mode(indio_dev);
139 			return ret;
140 		}
141 		ret = i2c_smbus_read_word_data(data->client, chan->address);
142 		iio_device_release_direct_mode(indio_dev);
143 		if (ret < 0)
144 			return ret;
145 		*val = ret;
146 		return IIO_VAL_INT;
147 	case IIO_CHAN_INFO_SCALE:
148 		i = (data->gain & TCS3414_GAIN_MASK) >> TCS3414_GAIN_SHIFT;
149 		*val = tcs3414_scales[i][0];
150 		*val2 = tcs3414_scales[i][1];
151 		return IIO_VAL_INT_PLUS_MICRO;
152 	case IIO_CHAN_INFO_INT_TIME:
153 		*val = 0;
154 		*val2 = tcs3414_times[data->timing & TCS3414_INTEG_MASK] * 1000;
155 		return IIO_VAL_INT_PLUS_MICRO;
156 	}
157 	return -EINVAL;
158 }
159 
160 static int tcs3414_write_raw(struct iio_dev *indio_dev,
161 			       struct iio_chan_spec const *chan,
162 			       int val, int val2, long mask)
163 {
164 	struct tcs3414_data *data = iio_priv(indio_dev);
165 	int i;
166 
167 	switch (mask) {
168 	case IIO_CHAN_INFO_SCALE:
169 		for (i = 0; i < ARRAY_SIZE(tcs3414_scales); i++) {
170 			if (val == tcs3414_scales[i][0] &&
171 				val2 == tcs3414_scales[i][1]) {
172 				data->gain &= ~TCS3414_GAIN_MASK;
173 				data->gain |= i << TCS3414_GAIN_SHIFT;
174 				return i2c_smbus_write_byte_data(
175 					data->client, TCS3414_GAIN,
176 					data->gain);
177 			}
178 		}
179 		return -EINVAL;
180 	case IIO_CHAN_INFO_INT_TIME:
181 		if (val != 0)
182 			return -EINVAL;
183 		for (i = 0; i < ARRAY_SIZE(tcs3414_times); i++) {
184 			if (val2 == tcs3414_times[i] * 1000) {
185 				data->timing &= ~TCS3414_INTEG_MASK;
186 				data->timing |= i;
187 				return i2c_smbus_write_byte_data(
188 					data->client, TCS3414_TIMING,
189 					data->timing);
190 			}
191 		}
192 		return -EINVAL;
193 	default:
194 		return -EINVAL;
195 	}
196 }
197 
198 static irqreturn_t tcs3414_trigger_handler(int irq, void *p)
199 {
200 	struct iio_poll_func *pf = p;
201 	struct iio_dev *indio_dev = pf->indio_dev;
202 	struct tcs3414_data *data = iio_priv(indio_dev);
203 	int i, j = 0;
204 
205 	for_each_set_bit(i, indio_dev->active_scan_mask,
206 		indio_dev->masklength) {
207 		int ret = i2c_smbus_read_word_data(data->client,
208 			TCS3414_DATA_GREEN + 2*i);
209 		if (ret < 0)
210 			goto done;
211 
212 		data->buffer[j++] = ret;
213 	}
214 
215 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
216 		iio_get_time_ns(indio_dev));
217 
218 done:
219 	iio_trigger_notify_done(indio_dev->trig);
220 
221 	return IRQ_HANDLED;
222 }
223 
224 static IIO_CONST_ATTR(scale_available, "1 0.25 0.0625 0.015625");
225 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.012 0.1 0.4");
226 
227 static struct attribute *tcs3414_attributes[] = {
228 	&iio_const_attr_scale_available.dev_attr.attr,
229 	&iio_const_attr_integration_time_available.dev_attr.attr,
230 	NULL
231 };
232 
233 static const struct attribute_group tcs3414_attribute_group = {
234 	.attrs = tcs3414_attributes,
235 };
236 
237 static const struct iio_info tcs3414_info = {
238 	.read_raw = tcs3414_read_raw,
239 	.write_raw = tcs3414_write_raw,
240 	.attrs = &tcs3414_attribute_group,
241 };
242 
243 static int tcs3414_buffer_postenable(struct iio_dev *indio_dev)
244 {
245 	struct tcs3414_data *data = iio_priv(indio_dev);
246 
247 	data->control |= TCS3414_CONTROL_ADC_EN;
248 	return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
249 		data->control);
250 }
251 
252 static int tcs3414_buffer_predisable(struct iio_dev *indio_dev)
253 {
254 	struct tcs3414_data *data = iio_priv(indio_dev);
255 
256 	data->control &= ~TCS3414_CONTROL_ADC_EN;
257 	return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
258 		data->control);
259 }
260 
261 static const struct iio_buffer_setup_ops tcs3414_buffer_setup_ops = {
262 	.postenable = tcs3414_buffer_postenable,
263 	.predisable = tcs3414_buffer_predisable,
264 };
265 
266 static int tcs3414_probe(struct i2c_client *client,
267 			   const struct i2c_device_id *id)
268 {
269 	struct tcs3414_data *data;
270 	struct iio_dev *indio_dev;
271 	int ret;
272 
273 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
274 	if (indio_dev == NULL)
275 		return -ENOMEM;
276 
277 	data = iio_priv(indio_dev);
278 	i2c_set_clientdata(client, indio_dev);
279 	data->client = client;
280 
281 	indio_dev->info = &tcs3414_info;
282 	indio_dev->name = TCS3414_DRV_NAME;
283 	indio_dev->channels = tcs3414_channels;
284 	indio_dev->num_channels = ARRAY_SIZE(tcs3414_channels);
285 	indio_dev->modes = INDIO_DIRECT_MODE;
286 
287 	ret = i2c_smbus_read_byte_data(data->client, TCS3414_ID);
288 	if (ret < 0)
289 		return ret;
290 
291 	switch (ret & 0xf0) {
292 	case 0x00:
293 		dev_info(&client->dev, "TCS3404 found\n");
294 		break;
295 	case 0x10:
296 		dev_info(&client->dev, "TCS3413/14/15/16 found\n");
297 		break;
298 	default:
299 		return -ENODEV;
300 	}
301 
302 	data->control = TCS3414_CONTROL_POWER;
303 	ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
304 		data->control);
305 	if (ret < 0)
306 		return ret;
307 
308 	data->timing = TCS3414_INTEG_12MS; /* free running */
309 	ret = i2c_smbus_write_byte_data(data->client, TCS3414_TIMING,
310 		data->timing);
311 	if (ret < 0)
312 		return ret;
313 
314 	ret = i2c_smbus_read_byte_data(data->client, TCS3414_GAIN);
315 	if (ret < 0)
316 		return ret;
317 	data->gain = ret;
318 
319 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
320 		tcs3414_trigger_handler, &tcs3414_buffer_setup_ops);
321 	if (ret < 0)
322 		return ret;
323 
324 	ret = iio_device_register(indio_dev);
325 	if (ret < 0)
326 		goto buffer_cleanup;
327 
328 	return 0;
329 
330 buffer_cleanup:
331 	iio_triggered_buffer_cleanup(indio_dev);
332 	return ret;
333 }
334 
335 static int tcs3414_powerdown(struct tcs3414_data *data)
336 {
337 	return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
338 		data->control & ~(TCS3414_CONTROL_POWER |
339 		TCS3414_CONTROL_ADC_EN));
340 }
341 
342 static int tcs3414_remove(struct i2c_client *client)
343 {
344 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
345 
346 	iio_device_unregister(indio_dev);
347 	iio_triggered_buffer_cleanup(indio_dev);
348 	tcs3414_powerdown(iio_priv(indio_dev));
349 
350 	return 0;
351 }
352 
353 #ifdef CONFIG_PM_SLEEP
354 static int tcs3414_suspend(struct device *dev)
355 {
356 	struct tcs3414_data *data = iio_priv(i2c_get_clientdata(
357 		to_i2c_client(dev)));
358 	return tcs3414_powerdown(data);
359 }
360 
361 static int tcs3414_resume(struct device *dev)
362 {
363 	struct tcs3414_data *data = iio_priv(i2c_get_clientdata(
364 		to_i2c_client(dev)));
365 	return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
366 		data->control);
367 }
368 #endif
369 
370 static SIMPLE_DEV_PM_OPS(tcs3414_pm_ops, tcs3414_suspend, tcs3414_resume);
371 
372 static const struct i2c_device_id tcs3414_id[] = {
373 	{ "tcs3414", 0 },
374 	{ }
375 };
376 MODULE_DEVICE_TABLE(i2c, tcs3414_id);
377 
378 static struct i2c_driver tcs3414_driver = {
379 	.driver = {
380 		.name	= TCS3414_DRV_NAME,
381 		.pm	= &tcs3414_pm_ops,
382 	},
383 	.probe		= tcs3414_probe,
384 	.remove		= tcs3414_remove,
385 	.id_table	= tcs3414_id,
386 };
387 module_i2c_driver(tcs3414_driver);
388 
389 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
390 MODULE_DESCRIPTION("TCS3414 digital color sensors driver");
391 MODULE_LICENSE("GPL");
392