1 // SPDX-License-Identifier: GPL-2.0-only
3 * tcs3472.c - Support for TAOS TCS3472 color light-to-digital converter
5 * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
7 * Color light sensor with 16-bit channels for red, green, blue, clear);
8 * 7-bit I2C slave address 0x39 (TCS34721, TCS34723) or 0x29 (TCS34725,
11 * Datasheet: http://ams.com/eng/content/download/319364/1117183/file/TCS3472_Datasheet_EN_v2.pdf
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/events.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)
32 #define TCS3472_SPECIAL_FUNC (BIT(5) | BIT(6))
34 #define TCS3472_INTR_CLEAR (TCS3472_COMMAND | TCS3472_SPECIAL_FUNC | 0x06)
36 #define TCS3472_ENABLE (TCS3472_COMMAND | 0x00)
37 #define TCS3472_ATIME (TCS3472_COMMAND | 0x01)
38 #define TCS3472_WTIME (TCS3472_COMMAND | 0x03)
39 #define TCS3472_AILT (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x04)
40 #define TCS3472_AIHT (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x06)
41 #define TCS3472_PERS (TCS3472_COMMAND | 0x0c)
42 #define TCS3472_CONFIG (TCS3472_COMMAND | 0x0d)
43 #define TCS3472_CONTROL (TCS3472_COMMAND | 0x0f)
44 #define TCS3472_ID (TCS3472_COMMAND | 0x12)
45 #define TCS3472_STATUS (TCS3472_COMMAND | 0x13)
46 #define TCS3472_CDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x14)
47 #define TCS3472_RDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x16)
48 #define TCS3472_GDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x18)
49 #define TCS3472_BDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x1a)
51 #define TCS3472_STATUS_AINT BIT(4)
52 #define TCS3472_STATUS_AVALID BIT(0)
53 #define TCS3472_ENABLE_AIEN BIT(4)
54 #define TCS3472_ENABLE_AEN BIT(1)
55 #define TCS3472_ENABLE_PON BIT(0)
56 #define TCS3472_CONTROL_AGAIN_MASK (BIT(0) | BIT(1))
59 struct i2c_client
*client
;
67 u16 buffer
[8]; /* 4 16-bit channels + 64-bit timestamp */
70 static const struct iio_event_spec tcs3472_events
[] = {
72 .type
= IIO_EV_TYPE_THRESH
,
73 .dir
= IIO_EV_DIR_RISING
,
74 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
76 .type
= IIO_EV_TYPE_THRESH
,
77 .dir
= IIO_EV_DIR_FALLING
,
78 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
80 .type
= IIO_EV_TYPE_THRESH
,
81 .dir
= IIO_EV_DIR_EITHER
,
82 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
83 BIT(IIO_EV_INFO_PERIOD
),
87 #define TCS3472_CHANNEL(_color, _si, _addr) { \
88 .type = IIO_INTENSITY, \
90 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
91 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBSCALE) | \
92 BIT(IIO_CHAN_INFO_INT_TIME), \
93 .channel2 = IIO_MOD_LIGHT_##_color, \
100 .endianness = IIO_CPU, \
102 .event_spec = _si ? NULL : tcs3472_events, \
103 .num_event_specs = _si ? 0 : ARRAY_SIZE(tcs3472_events), \
106 static const int tcs3472_agains
[] = { 1, 4, 16, 60 };
108 static const struct iio_chan_spec tcs3472_channels
[] = {
109 TCS3472_CHANNEL(CLEAR
, 0, TCS3472_CDATA
),
110 TCS3472_CHANNEL(RED
, 1, TCS3472_RDATA
),
111 TCS3472_CHANNEL(GREEN
, 2, TCS3472_GDATA
),
112 TCS3472_CHANNEL(BLUE
, 3, TCS3472_BDATA
),
113 IIO_CHAN_SOFT_TIMESTAMP(4),
116 static int tcs3472_req_data(struct tcs3472_data
*data
)
122 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_STATUS
);
125 if (ret
& TCS3472_STATUS_AVALID
)
131 dev_err(&data
->client
->dev
, "data not ready\n");
138 static int tcs3472_read_raw(struct iio_dev
*indio_dev
,
139 struct iio_chan_spec
const *chan
,
140 int *val
, int *val2
, long mask
)
142 struct tcs3472_data
*data
= iio_priv(indio_dev
);
146 case IIO_CHAN_INFO_RAW
:
147 ret
= iio_device_claim_direct_mode(indio_dev
);
150 ret
= tcs3472_req_data(data
);
152 iio_device_release_direct_mode(indio_dev
);
155 ret
= i2c_smbus_read_word_data(data
->client
, chan
->address
);
156 iio_device_release_direct_mode(indio_dev
);
161 case IIO_CHAN_INFO_CALIBSCALE
:
162 *val
= tcs3472_agains
[data
->control
&
163 TCS3472_CONTROL_AGAIN_MASK
];
165 case IIO_CHAN_INFO_INT_TIME
:
167 *val2
= (256 - data
->atime
) * 2400;
168 return IIO_VAL_INT_PLUS_MICRO
;
173 static int tcs3472_write_raw(struct iio_dev
*indio_dev
,
174 struct iio_chan_spec
const *chan
,
175 int val
, int val2
, long mask
)
177 struct tcs3472_data
*data
= iio_priv(indio_dev
);
181 case IIO_CHAN_INFO_CALIBSCALE
:
184 for (i
= 0; i
< ARRAY_SIZE(tcs3472_agains
); i
++) {
185 if (val
== tcs3472_agains
[i
]) {
186 data
->control
&= ~TCS3472_CONTROL_AGAIN_MASK
;
188 return i2c_smbus_write_byte_data(
189 data
->client
, TCS3472_CONTROL
,
194 case IIO_CHAN_INFO_INT_TIME
:
197 for (i
= 0; i
< 256; i
++) {
198 if (val2
== (256 - i
) * 2400) {
200 return i2c_smbus_write_byte_data(
201 data
->client
, TCS3472_ATIME
,
212 * Translation from APERS field value to the number of consecutive out-of-range
213 * clear channel values before an interrupt is generated
215 static const int tcs3472_intr_pers
[] = {
216 0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
219 static int tcs3472_read_event(struct iio_dev
*indio_dev
,
220 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
221 enum iio_event_direction dir
, enum iio_event_info info
, int *val
,
224 struct tcs3472_data
*data
= iio_priv(indio_dev
);
228 mutex_lock(&data
->lock
);
231 case IIO_EV_INFO_VALUE
:
232 *val
= (dir
== IIO_EV_DIR_RISING
) ?
233 data
->high_thresh
: data
->low_thresh
;
236 case IIO_EV_INFO_PERIOD
:
237 period
= (256 - data
->atime
) * 2400 *
238 tcs3472_intr_pers
[data
->apers
];
239 *val
= period
/ USEC_PER_SEC
;
240 *val2
= period
% USEC_PER_SEC
;
241 ret
= IIO_VAL_INT_PLUS_MICRO
;
248 mutex_unlock(&data
->lock
);
253 static int tcs3472_write_event(struct iio_dev
*indio_dev
,
254 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
255 enum iio_event_direction dir
, enum iio_event_info info
, int val
,
258 struct tcs3472_data
*data
= iio_priv(indio_dev
);
264 mutex_lock(&data
->lock
);
266 case IIO_EV_INFO_VALUE
:
268 case IIO_EV_DIR_RISING
:
269 command
= TCS3472_AIHT
;
271 case IIO_EV_DIR_FALLING
:
272 command
= TCS3472_AILT
;
278 ret
= i2c_smbus_write_word_data(data
->client
, command
, val
);
282 if (dir
== IIO_EV_DIR_RISING
)
283 data
->high_thresh
= val
;
285 data
->low_thresh
= val
;
287 case IIO_EV_INFO_PERIOD
:
288 period
= val
* USEC_PER_SEC
+ val2
;
289 for (i
= 1; i
< ARRAY_SIZE(tcs3472_intr_pers
) - 1; i
++) {
290 if (period
<= (256 - data
->atime
) * 2400 *
291 tcs3472_intr_pers
[i
])
294 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_PERS
, i
);
305 mutex_unlock(&data
->lock
);
310 static int tcs3472_read_event_config(struct iio_dev
*indio_dev
,
311 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
312 enum iio_event_direction dir
)
314 struct tcs3472_data
*data
= iio_priv(indio_dev
);
317 mutex_lock(&data
->lock
);
318 ret
= !!(data
->enable
& TCS3472_ENABLE_AIEN
);
319 mutex_unlock(&data
->lock
);
324 static int tcs3472_write_event_config(struct iio_dev
*indio_dev
,
325 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
326 enum iio_event_direction dir
, int state
)
328 struct tcs3472_data
*data
= iio_priv(indio_dev
);
332 mutex_lock(&data
->lock
);
334 enable_old
= data
->enable
;
337 data
->enable
|= TCS3472_ENABLE_AIEN
;
339 data
->enable
&= ~TCS3472_ENABLE_AIEN
;
341 if (enable_old
!= data
->enable
) {
342 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_ENABLE
,
345 data
->enable
= enable_old
;
347 mutex_unlock(&data
->lock
);
352 static irqreturn_t
tcs3472_event_handler(int irq
, void *priv
)
354 struct iio_dev
*indio_dev
= priv
;
355 struct tcs3472_data
*data
= iio_priv(indio_dev
);
358 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_STATUS
);
359 if (ret
>= 0 && (ret
& TCS3472_STATUS_AINT
)) {
360 iio_push_event(indio_dev
, IIO_UNMOD_EVENT_CODE(IIO_INTENSITY
, 0,
363 iio_get_time_ns(indio_dev
));
365 i2c_smbus_read_byte_data(data
->client
, TCS3472_INTR_CLEAR
);
371 static irqreturn_t
tcs3472_trigger_handler(int irq
, void *p
)
373 struct iio_poll_func
*pf
= p
;
374 struct iio_dev
*indio_dev
= pf
->indio_dev
;
375 struct tcs3472_data
*data
= iio_priv(indio_dev
);
378 int ret
= tcs3472_req_data(data
);
382 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
383 indio_dev
->masklength
) {
384 ret
= i2c_smbus_read_word_data(data
->client
,
385 TCS3472_CDATA
+ 2*i
);
389 data
->buffer
[j
++] = ret
;
392 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
393 iio_get_time_ns(indio_dev
));
396 iio_trigger_notify_done(indio_dev
->trig
);
401 static ssize_t
tcs3472_show_int_time_available(struct device
*dev
,
402 struct device_attribute
*attr
,
408 for (i
= 1; i
<= 256; i
++)
409 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%06d ",
412 /* replace trailing space by newline */
418 static IIO_CONST_ATTR(calibscale_available
, "1 4 16 60");
419 static IIO_DEV_ATTR_INT_TIME_AVAIL(tcs3472_show_int_time_available
);
421 static struct attribute
*tcs3472_attributes
[] = {
422 &iio_const_attr_calibscale_available
.dev_attr
.attr
,
423 &iio_dev_attr_integration_time_available
.dev_attr
.attr
,
427 static const struct attribute_group tcs3472_attribute_group
= {
428 .attrs
= tcs3472_attributes
,
431 static const struct iio_info tcs3472_info
= {
432 .read_raw
= tcs3472_read_raw
,
433 .write_raw
= tcs3472_write_raw
,
434 .read_event_value
= tcs3472_read_event
,
435 .write_event_value
= tcs3472_write_event
,
436 .read_event_config
= tcs3472_read_event_config
,
437 .write_event_config
= tcs3472_write_event_config
,
438 .attrs
= &tcs3472_attribute_group
,
441 static int tcs3472_probe(struct i2c_client
*client
,
442 const struct i2c_device_id
*id
)
444 struct tcs3472_data
*data
;
445 struct iio_dev
*indio_dev
;
448 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
449 if (indio_dev
== NULL
)
452 data
= iio_priv(indio_dev
);
453 i2c_set_clientdata(client
, indio_dev
);
454 data
->client
= client
;
455 mutex_init(&data
->lock
);
457 indio_dev
->dev
.parent
= &client
->dev
;
458 indio_dev
->info
= &tcs3472_info
;
459 indio_dev
->name
= TCS3472_DRV_NAME
;
460 indio_dev
->channels
= tcs3472_channels
;
461 indio_dev
->num_channels
= ARRAY_SIZE(tcs3472_channels
);
462 indio_dev
->modes
= INDIO_DIRECT_MODE
;
464 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_ID
);
469 dev_info(&client
->dev
, "TCS34721/34725 found\n");
470 else if (ret
== 0x4d)
471 dev_info(&client
->dev
, "TCS34723/34727 found\n");
475 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_CONTROL
);
480 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_ATIME
);
485 ret
= i2c_smbus_read_word_data(data
->client
, TCS3472_AILT
);
488 data
->low_thresh
= ret
;
490 ret
= i2c_smbus_read_word_data(data
->client
, TCS3472_AIHT
);
493 data
->high_thresh
= ret
;
496 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_PERS
,
501 ret
= i2c_smbus_read_byte_data(data
->client
, TCS3472_ENABLE
);
506 data
->enable
= ret
| TCS3472_ENABLE_PON
| TCS3472_ENABLE_AEN
;
507 data
->enable
&= ~TCS3472_ENABLE_AIEN
;
508 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_ENABLE
,
513 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
514 tcs3472_trigger_handler
, NULL
);
519 ret
= request_threaded_irq(client
->irq
, NULL
,
520 tcs3472_event_handler
,
521 IRQF_TRIGGER_FALLING
| IRQF_SHARED
|
523 client
->name
, indio_dev
);
528 ret
= iio_device_register(indio_dev
);
535 free_irq(client
->irq
, indio_dev
);
537 iio_triggered_buffer_cleanup(indio_dev
);
541 static int tcs3472_powerdown(struct tcs3472_data
*data
)
544 u8 enable_mask
= TCS3472_ENABLE_AEN
| TCS3472_ENABLE_PON
;
546 mutex_lock(&data
->lock
);
548 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_ENABLE
,
549 data
->enable
& ~enable_mask
);
551 data
->enable
&= ~enable_mask
;
553 mutex_unlock(&data
->lock
);
558 static int tcs3472_remove(struct i2c_client
*client
)
560 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
562 iio_device_unregister(indio_dev
);
563 free_irq(client
->irq
, indio_dev
);
564 iio_triggered_buffer_cleanup(indio_dev
);
565 tcs3472_powerdown(iio_priv(indio_dev
));
570 #ifdef CONFIG_PM_SLEEP
571 static int tcs3472_suspend(struct device
*dev
)
573 struct tcs3472_data
*data
= iio_priv(i2c_get_clientdata(
574 to_i2c_client(dev
)));
575 return tcs3472_powerdown(data
);
578 static int tcs3472_resume(struct device
*dev
)
580 struct tcs3472_data
*data
= iio_priv(i2c_get_clientdata(
581 to_i2c_client(dev
)));
583 u8 enable_mask
= TCS3472_ENABLE_AEN
| TCS3472_ENABLE_PON
;
585 mutex_lock(&data
->lock
);
587 ret
= i2c_smbus_write_byte_data(data
->client
, TCS3472_ENABLE
,
588 data
->enable
| enable_mask
);
590 data
->enable
|= enable_mask
;
592 mutex_unlock(&data
->lock
);
598 static SIMPLE_DEV_PM_OPS(tcs3472_pm_ops
, tcs3472_suspend
, tcs3472_resume
);
600 static const struct i2c_device_id tcs3472_id
[] = {
604 MODULE_DEVICE_TABLE(i2c
, tcs3472_id
);
606 static struct i2c_driver tcs3472_driver
= {
608 .name
= TCS3472_DRV_NAME
,
609 .pm
= &tcs3472_pm_ops
,
611 .probe
= tcs3472_probe
,
612 .remove
= tcs3472_remove
,
613 .id_table
= tcs3472_id
,
615 module_i2c_driver(tcs3472_driver
);
617 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
618 MODULE_DESCRIPTION("TCS3472 color light sensors driver");
619 MODULE_LICENSE("GPL");