Lines Matching +full:light +full:- +full:to +full:- +full:digital

1 // SPDX-License-Identifier: GPL-2.0-or-later
7 * Device driver for the TAOS TSL2591. This is a very-high sensitivity
8 * light-to-digital converter that transforms light intensity into a digital
30 /* ADC integration time, field value to time in ms */
32 /* ADC integration time, field value to time in seconds */
34 /* ADC integration time, time in seconds to field value */
35 #define TSL2591_SEC_TO_FVAL(x) ((x) - 1)
102 #define TSL2591_PRST_ALS_INT_CYCLE_MAX (BIT(4) - 1)
141 #define TSL2591_ALS_MAX_VALUE (BIT(16) - 1)
212 return -EINVAL; in tsl2591_gain_to_multiplier()
228 return -EINVAL; in tsl2591_multiplier_to_gain()
266 return -EINVAL; in tsl2591_persist_cycle_to_lit()
304 return -EINVAL; in tsl2591_persist_lit_to_cycle()
320 return -EINVAL; in tsl2591_compatible_int_time()
333 return -EINVAL; in tsl2591_als_time_to_fval()
345 return -EINVAL; in tsl2591_compatible_gain()
370 return -EINVAL; in tsl2591_compatible_als_persist_cycle()
380 dev_err(&client->dev, "Failed to read register\n"); in tsl2591_check_als_valid()
381 return -EINVAL; in tsl2591_check_als_valid()
389 struct tsl2591_als_settings settings = chip->als_settings; in tsl2591_wait_adc_complete()
390 struct i2c_client *client = chip->client; in tsl2591_wait_adc_complete()
397 return -EINVAL; in tsl2591_wait_adc_complete()
400 * Sleep for ALS integration time to allow enough time or an ADC read in tsl2591_wait_adc_complete()
401 * cycle to complete. Check status after delay for ALS valid. in tsl2591_wait_adc_complete()
405 /* Check for status ALS valid flag for up to 100ms */ in tsl2591_wait_adc_complete()
411 dev_err(&client->dev, "Timed out waiting for valid ALS data\n"); in tsl2591_wait_adc_complete()
417 * tsl2591_read_channel_data - Reads raw channel data and calculates lux
423 * lux = ((C0DATA - C1DATA) * (1 - (C1DATA / C0DATA))) / CPL
425 * Scale values to get more representative value of lux i.e.
426 * lux = ((C0DATA - C1DATA) * (1000 - ((C1DATA * 1000) / C0DATA))) / CPL
436 struct tsl2591_als_settings *settings = &chip->als_settings; in tsl2591_read_channel_data()
437 struct i2c_client *client = chip->client; in tsl2591_read_channel_data()
445 dev_err(&client->dev, "No data available. Err: %d\n", ret); in tsl2591_read_channel_data()
453 dev_err(&client->dev, "Failed to read data bytes"); in tsl2591_read_channel_data()
460 switch (chan->type) { in tsl2591_read_channel_data()
462 if (chan->channel2 == IIO_MOD_LIGHT_BOTH) in tsl2591_read_channel_data()
464 else if (chan->channel2 == IIO_MOD_LIGHT_IR) in tsl2591_read_channel_data()
467 return -EINVAL; in tsl2591_read_channel_data()
470 gain_multi = tsl2591_gain_to_multiplier(settings->als_gain); in tsl2591_read_channel_data()
472 dev_err(&client->dev, "Invalid multiplier"); in tsl2591_read_channel_data()
476 int_time_fval = TSL2591_FVAL_TO_MSEC(settings->als_int_time); in tsl2591_read_channel_data()
480 dev_dbg(&client->dev, "Counts Per Lux: %d\n", counts_per_lux); in tsl2591_read_channel_data()
483 lux = ((als_ch0 - als_ch1) * in tsl2591_read_channel_data()
484 (1000 - ((als_ch1 * 1000) / als_ch0))) / counts_per_lux; in tsl2591_read_channel_data()
486 dev_dbg(&client->dev, "Raw lux calculation: %d\n", lux); in tsl2591_read_channel_data()
488 /* Divide by 1000 to get real lux value before scaling */ in tsl2591_read_channel_data()
492 *val2 = (lux - (*val * 1000)) * 1000; in tsl2591_read_channel_data()
496 return -EINVAL; in tsl2591_read_channel_data()
504 struct tsl2591_als_settings als_settings = chip->als_settings; in tsl2591_set_als_gain_int_time()
505 struct i2c_client *client = chip->client; in tsl2591_set_als_gain_int_time()
512 dev_err(&client->dev, "Failed to set als gain & int time\n"); in tsl2591_set_als_gain_int_time()
520 struct tsl2591_als_settings als_settings = chip->als_settings; in tsl2591_set_als_lower_threshold()
521 struct i2c_client *client = chip->client; in tsl2591_set_als_lower_threshold()
527 chip->als_settings.als_lower_thresh = als_lower_threshold; in tsl2591_set_als_lower_threshold()
530 * Lower threshold should not be greater or equal to upper. in tsl2591_set_als_lower_threshold()
531 * If this is the case, then assert upper threshold to new lower in tsl2591_set_als_lower_threshold()
532 * threshold + 1 to avoid ordering issues when setting thresholds. in tsl2591_set_als_lower_threshold()
546 dev_err(&client->dev, "Failed to set als lower threshold\n"); in tsl2591_set_als_lower_threshold()
554 dev_err(&client->dev, "Failed to set als lower threshold\n"); in tsl2591_set_als_lower_threshold()
564 struct tsl2591_als_settings als_settings = chip->als_settings; in tsl2591_set_als_upper_threshold()
565 struct i2c_client *client = chip->client; in tsl2591_set_als_upper_threshold()
572 return -EINVAL; in tsl2591_set_als_upper_threshold()
574 chip->als_settings.als_upper_thresh = als_upper_threshold; in tsl2591_set_als_upper_threshold()
578 * is the case, then assert lower threshold to new upper in tsl2591_set_als_upper_threshold()
579 * threshold - 1 to avoid ordering issues when setting thresholds. in tsl2591_set_als_upper_threshold()
582 als_lower_threshold = als_upper_threshold - 1; in tsl2591_set_als_upper_threshold()
593 dev_err(&client->dev, "Failed to set als upper threshold\n"); in tsl2591_set_als_upper_threshold()
601 dev_err(&client->dev, "Failed to set als upper threshold\n"); in tsl2591_set_als_upper_threshold()
611 struct i2c_client *client = chip->client; in tsl2591_set_als_persist_cycle()
618 dev_err(&client->dev, "Failed to set als persist cycle\n"); in tsl2591_set_als_persist_cycle()
620 chip->als_settings.als_persist = als_persist; in tsl2591_set_als_persist_cycle()
627 struct i2c_client *client = chip->client; in tsl2591_set_power_state()
634 dev_err(&client->dev, in tsl2591_set_power_state()
635 "Failed to set the power state to %#04x\n", state); in tsl2591_set_power_state()
648 tsl2591_als_period_list[chip->als_settings.als_int_time]); in tsl2591_in_illuminance_period_available_show()
717 struct i2c_client *client = chip->client; in tsl2591_read_raw()
720 pm_runtime_get_sync(&client->dev); in tsl2591_read_raw()
722 mutex_lock(&chip->als_mutex); in tsl2591_read_raw()
726 if (chan->type != IIO_INTENSITY) { in tsl2591_read_raw()
727 ret = -EINVAL; in tsl2591_read_raw()
738 if (chan->type != IIO_LIGHT) { in tsl2591_read_raw()
739 ret = -EINVAL; in tsl2591_read_raw()
750 if (chan->type != IIO_INTENSITY) { in tsl2591_read_raw()
751 ret = -EINVAL; in tsl2591_read_raw()
755 *val = TSL2591_FVAL_TO_SEC(chip->als_settings.als_int_time); in tsl2591_read_raw()
759 if (chan->type != IIO_INTENSITY) { in tsl2591_read_raw()
760 ret = -EINVAL; in tsl2591_read_raw()
764 *val = tsl2591_gain_to_multiplier(chip->als_settings.als_gain); in tsl2591_read_raw()
768 ret = -EINVAL; in tsl2591_read_raw()
773 mutex_unlock(&chip->als_mutex); in tsl2591_read_raw()
775 pm_runtime_mark_last_busy(&client->dev); in tsl2591_read_raw()
776 pm_runtime_put_autosuspend(&client->dev); in tsl2591_read_raw()
790 mutex_lock(&chip->als_mutex); in tsl2591_write_raw()
803 chip->als_settings.als_int_time = int_time; in tsl2591_write_raw()
815 chip->als_settings.als_gain = gain; in tsl2591_write_raw()
818 ret = -EINVAL; in tsl2591_write_raw()
825 mutex_unlock(&chip->als_mutex); in tsl2591_write_raw()
847 return -EINVAL; in tsl2591_read_available()
859 struct i2c_client *client = chip->client; in tsl2591_read_event_value()
863 mutex_lock(&chip->als_mutex); in tsl2591_read_event_value()
869 *val = chip->als_settings.als_upper_thresh; in tsl2591_read_event_value()
872 *val = chip->als_settings.als_lower_thresh; in tsl2591_read_event_value()
875 ret = -EINVAL; in tsl2591_read_event_value()
887 int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time); in tsl2591_read_event_value()
896 ret = -EINVAL; in tsl2591_read_event_value()
901 mutex_unlock(&chip->als_mutex); in tsl2591_read_event_value()
917 return -EINVAL; in tsl2591_write_event_value()
919 mutex_lock(&chip->als_mutex); in tsl2591_write_event_value()
924 ret = -EINVAL; in tsl2591_write_event_value()
940 ret = -EINVAL; in tsl2591_write_event_value()
945 int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time); in tsl2591_write_event_value()
952 ret = -EINVAL; in tsl2591_write_event_value()
965 ret = -EINVAL; in tsl2591_write_event_value()
970 mutex_unlock(&chip->als_mutex); in tsl2591_write_event_value()
981 return chip->events_enabled; in tsl2591_read_event_config()
991 struct i2c_client *client = chip->client; in tsl2591_write_event_config()
993 if (state && !chip->events_enabled) { in tsl2591_write_event_config()
994 chip->events_enabled = true; in tsl2591_write_event_config()
995 pm_runtime_get_sync(&client->dev); in tsl2591_write_event_config()
996 } else if (!state && chip->events_enabled) { in tsl2591_write_event_config()
997 chip->events_enabled = false; in tsl2591_write_event_config()
998 pm_runtime_mark_last_busy(&client->dev); in tsl2591_write_event_config()
999 pm_runtime_put_autosuspend(&client->dev); in tsl2591_write_event_config()
1028 mutex_lock(&chip->als_mutex); in tsl2591_suspend()
1030 mutex_unlock(&chip->als_mutex); in tsl2591_suspend()
1042 if (chip->events_enabled) in tsl2591_resume()
1045 mutex_lock(&chip->als_mutex); in tsl2591_resume()
1047 mutex_unlock(&chip->als_mutex); in tsl2591_resume()
1059 struct i2c_client *client = chip->client; in tsl2591_event_handler()
1061 if (!chip->events_enabled) in tsl2591_event_handler()
1080 chip->als_settings.als_int_time = TSL2591_DEFAULT_ALS_INT_TIME; in tsl2591_load_defaults()
1081 chip->als_settings.als_gain = TSL2591_DEFAULT_ALS_GAIN; in tsl2591_load_defaults()
1082 chip->als_settings.als_lower_thresh = TSL2591_DEFAULT_ALS_LOWER_THRESH; in tsl2591_load_defaults()
1083 chip->als_settings.als_upper_thresh = TSL2591_DEFAULT_ALS_UPPER_THRESH; in tsl2591_load_defaults()
1108 struct i2c_client *client = chip->client; in tsl2591_chip_off()
1110 pm_runtime_disable(&client->dev); in tsl2591_chip_off()
1111 pm_runtime_set_suspended(&client->dev); in tsl2591_chip_off()
1112 pm_runtime_put_noidle(&client->dev); in tsl2591_chip_off()
1123 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { in tsl2591_probe()
1124 dev_err(&client->dev, in tsl2591_probe()
1126 return -EOPNOTSUPP; in tsl2591_probe()
1129 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); in tsl2591_probe()
1131 return -ENOMEM; in tsl2591_probe()
1134 chip->client = client; in tsl2591_probe()
1137 if (client->irq) { in tsl2591_probe()
1138 ret = devm_request_threaded_irq(&client->dev, client->irq, in tsl2591_probe()
1143 dev_err_probe(&client->dev, ret, "IRQ request error\n"); in tsl2591_probe()
1144 return -EINVAL; in tsl2591_probe()
1146 indio_dev->info = &tsl2591_info; in tsl2591_probe()
1148 indio_dev->info = &tsl2591_info_no_irq; in tsl2591_probe()
1151 mutex_init(&chip->als_mutex); in tsl2591_probe()
1156 dev_err(&client->dev, in tsl2591_probe()
1157 "Failed to read the device ID register\n"); in tsl2591_probe()
1162 dev_err(&client->dev, "Device ID: %#04x unknown\n", ret); in tsl2591_probe()
1163 return -EINVAL; in tsl2591_probe()
1166 indio_dev->channels = tsl2591_channels; in tsl2591_probe()
1167 indio_dev->num_channels = ARRAY_SIZE(tsl2591_channels); in tsl2591_probe()
1168 indio_dev->modes = INDIO_DIRECT_MODE; in tsl2591_probe()
1169 indio_dev->name = chip->client->name; in tsl2591_probe()
1170 chip->events_enabled = false; in tsl2591_probe()
1172 pm_runtime_enable(&client->dev); in tsl2591_probe()
1173 pm_runtime_set_autosuspend_delay(&client->dev, in tsl2591_probe()
1175 pm_runtime_use_autosuspend(&client->dev); in tsl2591_probe()
1178 * Add chip off to automatically managed path and disable runtime in tsl2591_probe()
1181 * added to the managed path after pm runtime is enabled and before in tsl2591_probe()
1182 * any error exit paths are met to ensure we're not left in a state in tsl2591_probe()
1185 ret = devm_add_action_or_reset(&client->dev, tsl2591_chip_off, in tsl2591_probe()
1188 return -EINVAL; in tsl2591_probe()
1192 dev_err(&client->dev, "Failed to load sensor defaults\n"); in tsl2591_probe()
1193 return -EINVAL; in tsl2591_probe()
1198 dev_err(&client->dev, "Failed to clear als irq\n"); in tsl2591_probe()
1199 return -EINVAL; in tsl2591_probe()
1202 return devm_iio_device_register(&client->dev, indio_dev); in tsl2591_probe()
1222 MODULE_DESCRIPTION("TAOS tsl2591 ambient light sensor driver");