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,
14 * Datasheet: http://ams.com/eng/content/download/319364/1117183/file/TCS3472_Datasheet_EN_v2.pdf
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/delay.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/events.h>
27 #include <linux/iio/trigger_consumer.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/triggered_buffer.h>
31 #define TCS3472_DRV_NAME "tcs3472"
33 #define TCS3472_COMMAND BIT(7)
34 #define TCS3472_AUTO_INCR BIT(5)
35 #define TCS3472_SPECIAL_FUNC (BIT(5) | BIT(6))
37 #define TCS3472_INTR_CLEAR (TCS3472_COMMAND | TCS3472_SPECIAL_FUNC | 0x06)
39 #define TCS3472_ENABLE (TCS3472_COMMAND | 0x00)
40 #define TCS3472_ATIME (TCS3472_COMMAND | 0x01)
41 #define TCS3472_WTIME (TCS3472_COMMAND | 0x03)
42 #define TCS3472_AILT (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x04)
43 #define TCS3472_AIHT (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x06)
44 #define TCS3472_PERS (TCS3472_COMMAND | 0x0c)
45 #define TCS3472_CONFIG (TCS3472_COMMAND | 0x0d)
46 #define TCS3472_CONTROL (TCS3472_COMMAND | 0x0f)
47 #define TCS3472_ID (TCS3472_COMMAND | 0x12)
48 #define TCS3472_STATUS (TCS3472_COMMAND | 0x13)
49 #define TCS3472_CDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x14)
50 #define TCS3472_RDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x16)
51 #define TCS3472_GDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x18)
52 #define TCS3472_BDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x1a)
54 #define TCS3472_STATUS_AINT BIT(4)
55 #define TCS3472_STATUS_AVALID BIT(0)
56 #define TCS3472_ENABLE_AIEN BIT(4)
57 #define TCS3472_ENABLE_AEN BIT(1)
58 #define TCS3472_ENABLE_PON BIT(0)
59 #define TCS3472_CONTROL_AGAIN_MASK (BIT(0) | BIT(1))
62 struct i2c_client
*client
;
70 u16 buffer
[8]; /* 4 16-bit channels + 64-bit timestamp */
73 static const struct iio_event_spec tcs3472_events
[] = {
75 .type
= IIO_EV_TYPE_THRESH
,
76 .dir
= IIO_EV_DIR_RISING
,
77 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
79 .type
= IIO_EV_TYPE_THRESH
,
80 .dir
= IIO_EV_DIR_FALLING
,
81 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
83 .type
= IIO_EV_TYPE_THRESH
,
84 .dir
= IIO_EV_DIR_EITHER
,
85 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
86 BIT(IIO_EV_INFO_PERIOD
),
90 #define TCS3472_CHANNEL(_color, _si, _addr) { \
91 .type = IIO_INTENSITY, \
93 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
94 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBSCALE) | \
95 BIT(IIO_CHAN_INFO_INT_TIME), \
96 .channel2 = IIO_MOD_LIGHT_##_color, \
103 .endianness = IIO_CPU, \
105 .event_spec = _si ? NULL : tcs3472_events, \
106 .num_event_specs = _si ? 0 : ARRAY_SIZE(tcs3472_events), \
109 static const int tcs3472_agains
[] = { 1, 4, 16, 60 };
111 static const struct iio_chan_spec tcs3472_channels
[] = {
112 TCS3472_CHANNEL(CLEAR
, 0, TCS3472_CDATA
),
113 TCS3472_CHANNEL(RED
, 1, TCS3472_RDATA
),
114 TCS3472_CHANNEL(GREEN
, 2, TCS3472_GDATA
),
115 TCS3472_CHANNEL(BLUE
, 3, TCS3472_BDATA
),
116 IIO_CHAN_SOFT_TIMESTAMP(4),
119 static int tcs3472_req_data(struct tcs3472_data
*data
)
125 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_STATUS
);
128 if (ret
& TCS3472_STATUS_AVALID
)
134 dev_err(&data
->client
->dev
, "data not ready\n");
141 static int tcs3472_read_raw(struct iio_dev
*indio_dev
,
142 struct iio_chan_spec
const *chan
,
143 int *val
, int *val2
, long mask
)
145 struct tcs3472_data
*data
= iio_priv(indio_dev
);
149 case IIO_CHAN_INFO_RAW
:
150 ret
= iio_device_claim_direct_mode(indio_dev
);
153 ret
= tcs3472_req_data(data
);
155 iio_device_release_direct_mode(indio_dev
);
158 ret
= i2c_smbus_read_word_data(data
->client
, chan
->address
);
159 iio_device_release_direct_mode(indio_dev
);
164 case IIO_CHAN_INFO_CALIBSCALE
:
165 *val
= tcs3472_agains
[data
->control
&
166 TCS3472_CONTROL_AGAIN_MASK
];
168 case IIO_CHAN_INFO_INT_TIME
:
170 *val2
= (256 - data
->atime
) * 2400;
171 return IIO_VAL_INT_PLUS_MICRO
;
176 static int tcs3472_write_raw(struct iio_dev
*indio_dev
,
177 struct iio_chan_spec
const *chan
,
178 int val
, int val2
, long mask
)
180 struct tcs3472_data
*data
= iio_priv(indio_dev
);
184 case IIO_CHAN_INFO_CALIBSCALE
:
187 for (i
= 0; i
< ARRAY_SIZE(tcs3472_agains
); i
++) {
188 if (val
== tcs3472_agains
[i
]) {
189 data
->control
&= ~TCS3472_CONTROL_AGAIN_MASK
;
191 return i2c_smbus_write_byte_data(
192 data
->client
, TCS3472_CONTROL
,
197 case IIO_CHAN_INFO_INT_TIME
:
200 for (i
= 0; i
< 256; i
++) {
201 if (val2
== (256 - i
) * 2400) {
203 return i2c_smbus_write_byte_data(
204 data
->client
, TCS3472_ATIME
,
215 * Translation from APERS field value to the number of consecutive out-of-range
216 * clear channel values before an interrupt is generated
218 static const int tcs3472_intr_pers
[] = {
219 0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
222 static int tcs3472_read_event(struct iio_dev
*indio_dev
,
223 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
224 enum iio_event_direction dir
, enum iio_event_info info
, int *val
,
227 struct tcs3472_data
*data
= iio_priv(indio_dev
);
231 mutex_lock(&data
->lock
);
234 case IIO_EV_INFO_VALUE
:
235 *val
= (dir
== IIO_EV_DIR_RISING
) ?
236 data
->high_thresh
: data
->low_thresh
;
239 case IIO_EV_INFO_PERIOD
:
240 period
= (256 - data
->atime
) * 2400 *
241 tcs3472_intr_pers
[data
->apers
];
242 *val
= period
/ USEC_PER_SEC
;
243 *val2
= period
% USEC_PER_SEC
;
244 ret
= IIO_VAL_INT_PLUS_MICRO
;
251 mutex_unlock(&data
->lock
);
256 static int tcs3472_write_event(struct iio_dev
*indio_dev
,
257 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
258 enum iio_event_direction dir
, enum iio_event_info info
, int val
,
261 struct tcs3472_data
*data
= iio_priv(indio_dev
);
267 mutex_lock(&data
->lock
);
269 case IIO_EV_INFO_VALUE
:
271 case IIO_EV_DIR_RISING
:
272 command
= TCS3472_AIHT
;
274 case IIO_EV_DIR_FALLING
:
275 command
= TCS3472_AILT
;
281 ret
= i2c_smbus_write_word_data(data
->client
, command
, val
);
285 if (dir
== IIO_EV_DIR_RISING
)
286 data
->high_thresh
= val
;
288 data
->low_thresh
= val
;
290 case IIO_EV_INFO_PERIOD
:
291 period
= val
* USEC_PER_SEC
+ val2
;
292 for (i
= 1; i
< ARRAY_SIZE(tcs3472_intr_pers
) - 1; i
++) {
293 if (period
<= (256 - data
->atime
) * 2400 *
294 tcs3472_intr_pers
[i
])
297 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_PERS
, i
);
308 mutex_unlock(&data
->lock
);
313 static int tcs3472_read_event_config(struct iio_dev
*indio_dev
,
314 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
315 enum iio_event_direction dir
)
317 struct tcs3472_data
*data
= iio_priv(indio_dev
);
320 mutex_lock(&data
->lock
);
321 ret
= !!(data
->enable
& TCS3472_ENABLE_AIEN
);
322 mutex_unlock(&data
->lock
);
327 static int tcs3472_write_event_config(struct iio_dev
*indio_dev
,
328 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
329 enum iio_event_direction dir
, int state
)
331 struct tcs3472_data
*data
= iio_priv(indio_dev
);
335 mutex_lock(&data
->lock
);
337 enable_old
= data
->enable
;
340 data
->enable
|= TCS3472_ENABLE_AIEN
;
342 data
->enable
&= ~TCS3472_ENABLE_AIEN
;
344 if (enable_old
!= data
->enable
) {
345 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_ENABLE
,
348 data
->enable
= enable_old
;
350 mutex_unlock(&data
->lock
);
355 static irqreturn_t
tcs3472_event_handler(int irq
, void *priv
)
357 struct iio_dev
*indio_dev
= priv
;
358 struct tcs3472_data
*data
= iio_priv(indio_dev
);
361 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_STATUS
);
362 if (ret
>= 0 && (ret
& TCS3472_STATUS_AINT
)) {
363 iio_push_event(indio_dev
, IIO_UNMOD_EVENT_CODE(IIO_INTENSITY
, 0,
366 iio_get_time_ns(indio_dev
));
368 i2c_smbus_read_byte_data(data
->client
, TCS3472_INTR_CLEAR
);
374 static irqreturn_t
tcs3472_trigger_handler(int irq
, void *p
)
376 struct iio_poll_func
*pf
= p
;
377 struct iio_dev
*indio_dev
= pf
->indio_dev
;
378 struct tcs3472_data
*data
= iio_priv(indio_dev
);
381 int ret
= tcs3472_req_data(data
);
385 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
386 indio_dev
->masklength
) {
387 ret
= i2c_smbus_read_word_data(data
->client
,
388 TCS3472_CDATA
+ 2*i
);
392 data
->buffer
[j
++] = ret
;
395 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
396 iio_get_time_ns(indio_dev
));
399 iio_trigger_notify_done(indio_dev
->trig
);
404 static ssize_t
tcs3472_show_int_time_available(struct device
*dev
,
405 struct device_attribute
*attr
,
411 for (i
= 1; i
<= 256; i
++)
412 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%06d ",
415 /* replace trailing space by newline */
421 static IIO_CONST_ATTR(calibscale_available
, "1 4 16 60");
422 static IIO_DEV_ATTR_INT_TIME_AVAIL(tcs3472_show_int_time_available
);
424 static struct attribute
*tcs3472_attributes
[] = {
425 &iio_const_attr_calibscale_available
.dev_attr
.attr
,
426 &iio_dev_attr_integration_time_available
.dev_attr
.attr
,
430 static const struct attribute_group tcs3472_attribute_group
= {
431 .attrs
= tcs3472_attributes
,
434 static const struct iio_info tcs3472_info
= {
435 .read_raw
= tcs3472_read_raw
,
436 .write_raw
= tcs3472_write_raw
,
437 .read_event_value
= tcs3472_read_event
,
438 .write_event_value
= tcs3472_write_event
,
439 .read_event_config
= tcs3472_read_event_config
,
440 .write_event_config
= tcs3472_write_event_config
,
441 .attrs
= &tcs3472_attribute_group
,
444 static int tcs3472_probe(struct i2c_client
*client
,
445 const struct i2c_device_id
*id
)
447 struct tcs3472_data
*data
;
448 struct iio_dev
*indio_dev
;
451 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
452 if (indio_dev
== NULL
)
455 data
= iio_priv(indio_dev
);
456 i2c_set_clientdata(client
, indio_dev
);
457 data
->client
= client
;
458 mutex_init(&data
->lock
);
460 indio_dev
->dev
.parent
= &client
->dev
;
461 indio_dev
->info
= &tcs3472_info
;
462 indio_dev
->name
= TCS3472_DRV_NAME
;
463 indio_dev
->channels
= tcs3472_channels
;
464 indio_dev
->num_channels
= ARRAY_SIZE(tcs3472_channels
);
465 indio_dev
->modes
= INDIO_DIRECT_MODE
;
467 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_ID
);
472 dev_info(&client
->dev
, "TCS34721/34725 found\n");
473 else if (ret
== 0x4d)
474 dev_info(&client
->dev
, "TCS34723/34727 found\n");
478 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_CONTROL
);
483 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_ATIME
);
488 ret
= i2c_smbus_read_word_data(data
->client
, TCS3472_AILT
);
491 data
->low_thresh
= ret
;
493 ret
= i2c_smbus_read_word_data(data
->client
, TCS3472_AIHT
);
496 data
->high_thresh
= ret
;
499 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_PERS
,
504 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_ENABLE
);
509 data
->enable
= ret
| TCS3472_ENABLE_PON
| TCS3472_ENABLE_AEN
;
510 data
->enable
&= ~TCS3472_ENABLE_AIEN
;
511 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_ENABLE
,
516 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
517 tcs3472_trigger_handler
, NULL
);
522 ret
= request_threaded_irq(client
->irq
, NULL
,
523 tcs3472_event_handler
,
524 IRQF_TRIGGER_FALLING
| IRQF_SHARED
|
526 client
->name
, indio_dev
);
531 ret
= iio_device_register(indio_dev
);
538 free_irq(client
->irq
, indio_dev
);
540 iio_triggered_buffer_cleanup(indio_dev
);
544 static int tcs3472_powerdown(struct tcs3472_data
*data
)
547 u8 enable_mask
= TCS3472_ENABLE_AEN
| TCS3472_ENABLE_PON
;
549 mutex_lock(&data
->lock
);
551 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_ENABLE
,
552 data
->enable
& ~enable_mask
);
554 data
->enable
&= ~enable_mask
;
556 mutex_unlock(&data
->lock
);
561 static int tcs3472_remove(struct i2c_client
*client
)
563 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
565 iio_device_unregister(indio_dev
);
566 free_irq(client
->irq
, indio_dev
);
567 iio_triggered_buffer_cleanup(indio_dev
);
568 tcs3472_powerdown(iio_priv(indio_dev
));
573 #ifdef CONFIG_PM_SLEEP
574 static int tcs3472_suspend(struct device
*dev
)
576 struct tcs3472_data
*data
= iio_priv(i2c_get_clientdata(
577 to_i2c_client(dev
)));
578 return tcs3472_powerdown(data
);
581 static int tcs3472_resume(struct device
*dev
)
583 struct tcs3472_data
*data
= iio_priv(i2c_get_clientdata(
584 to_i2c_client(dev
)));
586 u8 enable_mask
= TCS3472_ENABLE_AEN
| TCS3472_ENABLE_PON
;
588 mutex_lock(&data
->lock
);
590 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_ENABLE
,
591 data
->enable
| enable_mask
);
593 data
->enable
|= enable_mask
;
595 mutex_unlock(&data
->lock
);
601 static SIMPLE_DEV_PM_OPS(tcs3472_pm_ops
, tcs3472_suspend
, tcs3472_resume
);
603 static const struct i2c_device_id tcs3472_id
[] = {
607 MODULE_DEVICE_TABLE(i2c
, tcs3472_id
);
609 static struct i2c_driver tcs3472_driver
= {
611 .name
= TCS3472_DRV_NAME
,
612 .pm
= &tcs3472_pm_ops
,
614 .probe
= tcs3472_probe
,
615 .remove
= tcs3472_remove
,
616 .id_table
= tcs3472_id
,
618 module_i2c_driver(tcs3472_driver
);
620 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
621 MODULE_DESCRIPTION("TCS3472 color light sensors driver");
622 MODULE_LICENSE("GPL");