Lines Matching +full:light +full:- +full:to +full:- +full:digital
1 // SPDX-License-Identifier: GPL-2.0-only
3 * tcs3472.c - Support for TAOS TCS3472 color light-to-digital converter
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,
125 while (tries--) { in tcs3472_req_data()
126 ret = i2c_smbus_read_byte_data(data->client, TCS3472_STATUS); in tcs3472_req_data()
135 dev_err(&data->client->dev, "data not ready\n"); in tcs3472_req_data()
136 return -EIO; in tcs3472_req_data()
159 ret = i2c_smbus_read_word_data(data->client, chan->address); in tcs3472_read_raw()
166 *val = tcs3472_agains[data->control & in tcs3472_read_raw()
171 *val2 = (256 - data->atime) * 2400; in tcs3472_read_raw()
174 return -EINVAL; in tcs3472_read_raw()
187 return -EINVAL; in tcs3472_write_raw()
190 data->control &= ~TCS3472_CONTROL_AGAIN_MASK; in tcs3472_write_raw()
191 data->control |= i; in tcs3472_write_raw()
193 data->client, TCS3472_CONTROL, in tcs3472_write_raw()
194 data->control); in tcs3472_write_raw()
197 return -EINVAL; in tcs3472_write_raw()
200 return -EINVAL; in tcs3472_write_raw()
202 if (val2 == (256 - i) * 2400) { in tcs3472_write_raw()
203 data->atime = i; in tcs3472_write_raw()
205 data->client, TCS3472_ATIME, in tcs3472_write_raw()
206 data->atime); in tcs3472_write_raw()
210 return -EINVAL; in tcs3472_write_raw()
212 return -EINVAL; in tcs3472_write_raw()
216 * Translation from APERS field value to the number of consecutive out-of-range
232 mutex_lock(&data->lock); in tcs3472_read_event()
237 data->high_thresh : data->low_thresh; in tcs3472_read_event()
241 period = (256 - data->atime) * 2400 * in tcs3472_read_event()
242 tcs3472_intr_pers[data->apers]; in tcs3472_read_event()
248 ret = -EINVAL; in tcs3472_read_event()
252 mutex_unlock(&data->lock); in tcs3472_read_event()
268 mutex_lock(&data->lock); in tcs3472_write_event()
279 ret = -EINVAL; in tcs3472_write_event()
282 ret = i2c_smbus_write_word_data(data->client, command, val); in tcs3472_write_event()
287 data->high_thresh = val; in tcs3472_write_event()
289 data->low_thresh = val; in tcs3472_write_event()
293 for (i = 1; i < ARRAY_SIZE(tcs3472_intr_pers) - 1; i++) { in tcs3472_write_event()
294 if (period <= (256 - data->atime) * 2400 * in tcs3472_write_event()
298 ret = i2c_smbus_write_byte_data(data->client, TCS3472_PERS, i); in tcs3472_write_event()
302 data->apers = i; in tcs3472_write_event()
305 ret = -EINVAL; in tcs3472_write_event()
309 mutex_unlock(&data->lock); in tcs3472_write_event()
321 mutex_lock(&data->lock); in tcs3472_read_event_config()
322 ret = !!(data->enable & TCS3472_ENABLE_AIEN); in tcs3472_read_event_config()
323 mutex_unlock(&data->lock); in tcs3472_read_event_config()
336 mutex_lock(&data->lock); in tcs3472_write_event_config()
338 enable_old = data->enable; in tcs3472_write_event_config()
341 data->enable |= TCS3472_ENABLE_AIEN; in tcs3472_write_event_config()
343 data->enable &= ~TCS3472_ENABLE_AIEN; in tcs3472_write_event_config()
345 if (enable_old != data->enable) { in tcs3472_write_event_config()
346 ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE, in tcs3472_write_event_config()
347 data->enable); in tcs3472_write_event_config()
349 data->enable = enable_old; in tcs3472_write_event_config()
351 mutex_unlock(&data->lock); in tcs3472_write_event_config()
362 ret = i2c_smbus_read_byte_data(data->client, TCS3472_STATUS); in tcs3472_event_handler()
369 i2c_smbus_read_byte_data(data->client, TCS3472_INTR_CLEAR); in tcs3472_event_handler()
378 struct iio_dev *indio_dev = pf->indio_dev; in tcs3472_trigger_handler()
387 ret = i2c_smbus_read_word_data(data->client, in tcs3472_trigger_handler()
392 data->scan.chans[j++] = ret; in tcs3472_trigger_handler()
395 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, in tcs3472_trigger_handler()
399 iio_trigger_notify_done(indio_dev->trig); in tcs3472_trigger_handler()
412 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06d ", in tcs3472_show_int_time_available()
416 buf[len - 1] = '\n'; in tcs3472_show_int_time_available()
450 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in tcs3472_probe()
452 return -ENOMEM; in tcs3472_probe()
456 data->client = client; in tcs3472_probe()
457 mutex_init(&data->lock); in tcs3472_probe()
459 indio_dev->info = &tcs3472_info; in tcs3472_probe()
460 indio_dev->name = TCS3472_DRV_NAME; in tcs3472_probe()
461 indio_dev->channels = tcs3472_channels; in tcs3472_probe()
462 indio_dev->num_channels = ARRAY_SIZE(tcs3472_channels); in tcs3472_probe()
463 indio_dev->modes = INDIO_DIRECT_MODE; in tcs3472_probe()
465 ret = i2c_smbus_read_byte_data(data->client, TCS3472_ID); in tcs3472_probe()
470 dev_info(&client->dev, "TCS34721/34725 found\n"); in tcs3472_probe()
472 dev_info(&client->dev, "TCS34723/34727 found\n"); in tcs3472_probe()
474 return -ENODEV; in tcs3472_probe()
476 ret = i2c_smbus_read_byte_data(data->client, TCS3472_CONTROL); in tcs3472_probe()
479 data->control = ret; in tcs3472_probe()
481 ret = i2c_smbus_read_byte_data(data->client, TCS3472_ATIME); in tcs3472_probe()
484 data->atime = ret; in tcs3472_probe()
486 ret = i2c_smbus_read_word_data(data->client, TCS3472_AILT); in tcs3472_probe()
489 data->low_thresh = ret; in tcs3472_probe()
491 ret = i2c_smbus_read_word_data(data->client, TCS3472_AIHT); in tcs3472_probe()
494 data->high_thresh = ret; in tcs3472_probe()
496 data->apers = 1; in tcs3472_probe()
497 ret = i2c_smbus_write_byte_data(data->client, TCS3472_PERS, in tcs3472_probe()
498 data->apers); in tcs3472_probe()
502 ret = i2c_smbus_read_byte_data(data->client, TCS3472_ENABLE); in tcs3472_probe()
507 data->enable = ret | TCS3472_ENABLE_PON | TCS3472_ENABLE_AEN; in tcs3472_probe()
508 data->enable &= ~TCS3472_ENABLE_AIEN; in tcs3472_probe()
509 ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE, in tcs3472_probe()
510 data->enable); in tcs3472_probe()
519 if (client->irq) { in tcs3472_probe()
520 ret = request_threaded_irq(client->irq, NULL, in tcs3472_probe()
524 client->name, indio_dev); in tcs3472_probe()
536 if (client->irq) in tcs3472_probe()
537 free_irq(client->irq, indio_dev); in tcs3472_probe()
548 mutex_lock(&data->lock); in tcs3472_powerdown()
550 ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE, in tcs3472_powerdown()
551 data->enable & ~enable_mask); in tcs3472_powerdown()
553 data->enable &= ~enable_mask; in tcs3472_powerdown()
555 mutex_unlock(&data->lock); in tcs3472_powerdown()
565 if (client->irq) in tcs3472_remove()
566 free_irq(client->irq, indio_dev); in tcs3472_remove()
585 mutex_lock(&data->lock); in tcs3472_resume()
587 ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE, in tcs3472_resume()
588 data->enable | enable_mask); in tcs3472_resume()
590 data->enable |= enable_mask; in tcs3472_resume()
592 mutex_unlock(&data->lock); in tcs3472_resume()
618 MODULE_DESCRIPTION("TCS3472 color light sensors driver");