x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / iio / light / tcs3472.c
blob3aa71e34ae28b39350033616805eb6f2d5480714
1 /*
2 * tcs3472.c - Support for TAOS TCS3472 color light-to-digital converter
4 * Copyright (c) 2013 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 * 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)
14 * TODO: interrupt support, thresholds, wait time
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/delay.h>
20 #include <linux/pm.h>
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>
28 #define TCS3472_DRV_NAME "tcs3472"
30 #define TCS3472_COMMAND BIT(7)
31 #define TCS3472_AUTO_INCR BIT(5)
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)
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))
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 */
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 = { \
71 .sign = 'u', \
72 .realbits = 16, \
73 .storagebits = 16, \
74 .endianness = IIO_CPU, \
75 }, \
78 static const int tcs3472_agains[] = { 1, 4, 16, 60 };
80 static const struct iio_chan_spec tcs3472_channels[] = {
81 TCS3472_CHANNEL(CLEAR, 0, TCS3472_CDATA),
82 TCS3472_CHANNEL(RED, 1, TCS3472_RDATA),
83 TCS3472_CHANNEL(GREEN, 2, TCS3472_GDATA),
84 TCS3472_CHANNEL(BLUE, 3, TCS3472_BDATA),
85 IIO_CHAN_SOFT_TIMESTAMP(4),
88 static int tcs3472_req_data(struct tcs3472_data *data)
90 int tries = 50;
91 int ret;
93 while (tries--) {
94 ret = i2c_smbus_read_byte_data(data->client, TCS3472_STATUS);
95 if (ret < 0)
96 return ret;
97 if (ret & TCS3472_STATUS_AVALID)
98 break;
99 msleep(20);
102 if (tries < 0) {
103 dev_err(&data->client->dev, "data not ready\n");
104 return -EIO;
107 return 0;
110 static int tcs3472_read_raw(struct iio_dev *indio_dev,
111 struct iio_chan_spec const *chan,
112 int *val, int *val2, long mask)
114 struct tcs3472_data *data = iio_priv(indio_dev);
115 int ret;
117 switch (mask) {
118 case IIO_CHAN_INFO_RAW:
119 ret = iio_device_claim_direct_mode(indio_dev);
120 if (ret)
121 return ret;
122 ret = tcs3472_req_data(data);
123 if (ret < 0) {
124 iio_device_release_direct_mode(indio_dev);
125 return ret;
127 ret = i2c_smbus_read_word_data(data->client, chan->address);
128 iio_device_release_direct_mode(indio_dev);
129 if (ret < 0)
130 return ret;
131 *val = ret;
132 return IIO_VAL_INT;
133 case IIO_CHAN_INFO_CALIBSCALE:
134 *val = tcs3472_agains[data->control &
135 TCS3472_CONTROL_AGAIN_MASK];
136 return IIO_VAL_INT;
137 case IIO_CHAN_INFO_INT_TIME:
138 *val = 0;
139 *val2 = (256 - data->atime) * 2400;
140 return IIO_VAL_INT_PLUS_MICRO;
142 return -EINVAL;
145 static int tcs3472_write_raw(struct iio_dev *indio_dev,
146 struct iio_chan_spec const *chan,
147 int val, int val2, long mask)
149 struct tcs3472_data *data = iio_priv(indio_dev);
150 int i;
152 switch (mask) {
153 case IIO_CHAN_INFO_CALIBSCALE:
154 if (val2 != 0)
155 return -EINVAL;
156 for (i = 0; i < ARRAY_SIZE(tcs3472_agains); i++) {
157 if (val == tcs3472_agains[i]) {
158 data->control &= ~TCS3472_CONTROL_AGAIN_MASK;
159 data->control |= i;
160 return i2c_smbus_write_byte_data(
161 data->client, TCS3472_CONTROL,
162 data->control);
165 return -EINVAL;
166 case IIO_CHAN_INFO_INT_TIME:
167 if (val != 0)
168 return -EINVAL;
169 for (i = 0; i < 256; i++) {
170 if (val2 == (256 - i) * 2400) {
171 data->atime = i;
172 return i2c_smbus_write_word_data(
173 data->client, TCS3472_ATIME,
174 data->atime);
178 return -EINVAL;
180 return -EINVAL;
183 static irqreturn_t tcs3472_trigger_handler(int irq, void *p)
185 struct iio_poll_func *pf = p;
186 struct iio_dev *indio_dev = pf->indio_dev;
187 struct tcs3472_data *data = iio_priv(indio_dev);
188 int i, j = 0;
190 int ret = tcs3472_req_data(data);
191 if (ret < 0)
192 goto done;
194 for_each_set_bit(i, indio_dev->active_scan_mask,
195 indio_dev->masklength) {
196 ret = i2c_smbus_read_word_data(data->client,
197 TCS3472_CDATA + 2*i);
198 if (ret < 0)
199 goto done;
201 data->buffer[j++] = ret;
204 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
205 iio_get_time_ns(indio_dev));
207 done:
208 iio_trigger_notify_done(indio_dev->trig);
210 return IRQ_HANDLED;
213 static ssize_t tcs3472_show_int_time_available(struct device *dev,
214 struct device_attribute *attr,
215 char *buf)
217 size_t len = 0;
218 int i;
220 for (i = 1; i <= 256; i++)
221 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06d ",
222 2400 * i);
224 /* replace trailing space by newline */
225 buf[len - 1] = '\n';
227 return len;
230 static IIO_CONST_ATTR(calibscale_available, "1 4 16 60");
231 static IIO_DEV_ATTR_INT_TIME_AVAIL(tcs3472_show_int_time_available);
233 static struct attribute *tcs3472_attributes[] = {
234 &iio_const_attr_calibscale_available.dev_attr.attr,
235 &iio_dev_attr_integration_time_available.dev_attr.attr,
236 NULL
239 static const struct attribute_group tcs3472_attribute_group = {
240 .attrs = tcs3472_attributes,
243 static const struct iio_info tcs3472_info = {
244 .read_raw = tcs3472_read_raw,
245 .write_raw = tcs3472_write_raw,
246 .attrs = &tcs3472_attribute_group,
247 .driver_module = THIS_MODULE,
250 static int tcs3472_probe(struct i2c_client *client,
251 const struct i2c_device_id *id)
253 struct tcs3472_data *data;
254 struct iio_dev *indio_dev;
255 int ret;
257 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
258 if (indio_dev == NULL)
259 return -ENOMEM;
261 data = iio_priv(indio_dev);
262 i2c_set_clientdata(client, indio_dev);
263 data->client = client;
265 indio_dev->dev.parent = &client->dev;
266 indio_dev->info = &tcs3472_info;
267 indio_dev->name = TCS3472_DRV_NAME;
268 indio_dev->channels = tcs3472_channels;
269 indio_dev->num_channels = ARRAY_SIZE(tcs3472_channels);
270 indio_dev->modes = INDIO_DIRECT_MODE;
272 ret = i2c_smbus_read_byte_data(data->client, TCS3472_ID);
273 if (ret < 0)
274 return ret;
276 if (ret == 0x44)
277 dev_info(&client->dev, "TCS34721/34725 found\n");
278 else if (ret == 0x4d)
279 dev_info(&client->dev, "TCS34723/34727 found\n");
280 else
281 return -ENODEV;
283 ret = i2c_smbus_read_byte_data(data->client, TCS3472_CONTROL);
284 if (ret < 0)
285 return ret;
286 data->control = ret;
288 ret = i2c_smbus_read_byte_data(data->client, TCS3472_ATIME);
289 if (ret < 0)
290 return ret;
291 data->atime = ret;
293 ret = i2c_smbus_read_byte_data(data->client, TCS3472_ENABLE);
294 if (ret < 0)
295 return ret;
297 /* enable device */
298 data->enable = ret | TCS3472_ENABLE_PON | TCS3472_ENABLE_AEN;
299 ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
300 data->enable);
301 if (ret < 0)
302 return ret;
304 ret = iio_triggered_buffer_setup(indio_dev, NULL,
305 tcs3472_trigger_handler, NULL);
306 if (ret < 0)
307 return ret;
309 ret = iio_device_register(indio_dev);
310 if (ret < 0)
311 goto buffer_cleanup;
313 return 0;
315 buffer_cleanup:
316 iio_triggered_buffer_cleanup(indio_dev);
317 return ret;
320 static int tcs3472_powerdown(struct tcs3472_data *data)
322 return i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
323 data->enable & ~(TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON));
326 static int tcs3472_remove(struct i2c_client *client)
328 struct iio_dev *indio_dev = i2c_get_clientdata(client);
330 iio_device_unregister(indio_dev);
331 iio_triggered_buffer_cleanup(indio_dev);
332 tcs3472_powerdown(iio_priv(indio_dev));
334 return 0;
337 #ifdef CONFIG_PM_SLEEP
338 static int tcs3472_suspend(struct device *dev)
340 struct tcs3472_data *data = iio_priv(i2c_get_clientdata(
341 to_i2c_client(dev)));
342 return tcs3472_powerdown(data);
345 static int tcs3472_resume(struct device *dev)
347 struct tcs3472_data *data = iio_priv(i2c_get_clientdata(
348 to_i2c_client(dev)));
349 return i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
350 data->enable | (TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON));
352 #endif
354 static SIMPLE_DEV_PM_OPS(tcs3472_pm_ops, tcs3472_suspend, tcs3472_resume);
356 static const struct i2c_device_id tcs3472_id[] = {
357 { "tcs3472", 0 },
360 MODULE_DEVICE_TABLE(i2c, tcs3472_id);
362 static struct i2c_driver tcs3472_driver = {
363 .driver = {
364 .name = TCS3472_DRV_NAME,
365 .pm = &tcs3472_pm_ops,
367 .probe = tcs3472_probe,
368 .remove = tcs3472_remove,
369 .id_table = tcs3472_id,
371 module_i2c_driver(tcs3472_driver);
373 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
374 MODULE_DESCRIPTION("TCS3472 color light sensors driver");
375 MODULE_LICENSE("GPL");