Lines Matching +full:ch0 +full:- +full:2

1 // SPDX-License-Identifier: GPL-2.0-or-later
7 * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
61 /* Per-device data */
70 unsigned int ch0; member
108 s16 ch0; member
113 /* Index = (0 - 3) Used to validate the gain selection index */
131 chip->als_settings.als_time = 100; in tsl2583_defaults()
137 chip->als_settings.als_gain = 0; in tsl2583_defaults()
140 chip->als_settings.als_gain_trim = 1000; in tsl2583_defaults()
143 chip->als_settings.als_cal_target = 130; in tsl2583_defaults()
146 memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux, in tsl2583_defaults()
152 * The raw ch0 and ch1 values of the ambient light sensed in the last
157 * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
159 * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
165 u16 ch0, ch1; /* separated ch0/ch1 data from device */ in tsl2583_get_lux() local
174 ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG); in tsl2583_get_lux()
176 dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n", in tsl2583_get_lux()
183 dev_err(&chip->client->dev, "%s: data not valid; returning last value\n", in tsl2583_get_lux()
185 ret = chip->als_cur_info.lux; /* return LAST VALUE */ in tsl2583_get_lux()
192 ret = i2c_smbus_read_byte_data(chip->client, reg); in tsl2583_get_lux()
194 dev_err(&chip->client->dev, "%s: failed to read register %x\n", in tsl2583_get_lux()
206 ret = i2c_smbus_write_byte(chip->client, in tsl2583_get_lux()
210 dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n", in tsl2583_get_lux()
216 ch0 = le16_to_cpup((const __le16 *)&buf[0]); in tsl2583_get_lux()
217 ch1 = le16_to_cpup((const __le16 *)&buf[2]); in tsl2583_get_lux()
219 chip->als_cur_info.als_ch0 = ch0; in tsl2583_get_lux()
220 chip->als_cur_info.als_ch1 = ch1; in tsl2583_get_lux()
222 if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation)) in tsl2583_get_lux()
225 if (!ch0) { in tsl2583_get_lux()
232 chip->als_cur_info.lux = 0; in tsl2583_get_lux()
237 ratio = (ch1 << 15) / ch0; in tsl2583_get_lux()
240 for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux; in tsl2583_get_lux()
241 p->ratio != 0 && p->ratio < ratio; p++) in tsl2583_get_lux()
244 if (p->ratio == 0) { in tsl2583_get_lux()
249 ch0lux = ((ch0 * p->ch0) + in tsl2583_get_lux()
250 (gainadj[chip->als_settings.als_gain].ch0 >> 1)) in tsl2583_get_lux()
251 / gainadj[chip->als_settings.als_gain].ch0; in tsl2583_get_lux()
252 ch1lux = ((ch1 * p->ch1) + in tsl2583_get_lux()
253 (gainadj[chip->als_settings.als_gain].ch1 >> 1)) in tsl2583_get_lux()
254 / gainadj[chip->als_settings.als_gain].ch1; in tsl2583_get_lux()
258 dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n", in tsl2583_get_lux()
261 chip->als_cur_info.lux = 0; in tsl2583_get_lux()
265 lux = ch0lux - ch1lux; in tsl2583_get_lux()
269 if (chip->als_time_scale == 0) in tsl2583_get_lux()
272 lux = (lux + (chip->als_time_scale >> 1)) / in tsl2583_get_lux()
273 chip->als_time_scale; in tsl2583_get_lux()
279 * User-specified gain provides a multiplier. in tsl2583_get_lux()
280 * Apply user-specified gain before shifting right to retain precision. in tsl2583_get_lux()
285 lux64 = lux64 * chip->als_settings.als_gain_trim; in tsl2583_get_lux()
296 chip->als_cur_info.lux = lux; in tsl2583_get_lux()
315 ret = i2c_smbus_read_byte_data(chip->client, in tsl2583_als_calibrate()
318 dev_err(&chip->client->dev, in tsl2583_als_calibrate()
326 dev_err(&chip->client->dev, in tsl2583_als_calibrate()
329 return -EINVAL; in tsl2583_als_calibrate()
331 dev_err(&chip->client->dev, in tsl2583_als_calibrate()
334 return -ENODATA; in tsl2583_als_calibrate()
339 dev_err(&chip->client->dev, "%s: failed to get lux\n", in tsl2583_als_calibrate()
346 dev_err(&chip->client->dev, in tsl2583_als_calibrate()
349 return -ENODATA; in tsl2583_als_calibrate()
352 gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target) in tsl2583_als_calibrate()
353 * chip->als_settings.als_gain_trim) / lux_val); in tsl2583_als_calibrate()
355 dev_err(&chip->client->dev, in tsl2583_als_calibrate()
358 return -ENODATA; in tsl2583_als_calibrate()
361 chip->als_settings.als_gain_trim = (int)gain_trim_val; in tsl2583_als_calibrate()
372 als_count = DIV_ROUND_CLOSEST(chip->als_settings.als_time * 100, 270); in tsl2583_set_als_time()
379 val = 256 - als_count; in tsl2583_set_als_time()
380 ret = i2c_smbus_write_byte_data(chip->client, in tsl2583_set_als_time()
384 dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n", in tsl2583_set_als_time()
390 chip->als_saturation = als_count * 922; /* 90% of full scale */ in tsl2583_set_als_time()
391 chip->als_time_scale = DIV_ROUND_CLOSEST(als_time, 50); in tsl2583_set_als_time()
401 ret = i2c_smbus_write_byte_data(chip->client, in tsl2583_set_als_gain()
403 chip->als_settings.als_gain); in tsl2583_set_als_gain()
405 dev_err(&chip->client->dev, in tsl2583_set_als_gain()
407 chip->als_settings.als_gain); in tsl2583_set_als_gain()
416 ret = i2c_smbus_write_byte_data(chip->client, in tsl2583_set_power_state()
419 dev_err(&chip->client->dev, in tsl2583_set_power_state()
440 ret = i2c_smbus_write_byte_data(chip->client, in tsl2583_chip_init_and_power_on()
444 dev_err(&chip->client->dev, in tsl2583_chip_init_and_power_on()
477 mutex_lock(&chip->als_mutex); in in_illuminance_input_target_show()
478 ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target); in in_illuminance_input_target_show()
479 mutex_unlock(&chip->als_mutex); in in_illuminance_input_target_show()
493 return -EINVAL; in in_illuminance_input_target_store()
495 mutex_lock(&chip->als_mutex); in in_illuminance_input_target_store()
496 chip->als_settings.als_cal_target = value; in in_illuminance_input_target_store()
497 mutex_unlock(&chip->als_mutex); in in_illuminance_input_target_store()
511 return -EINVAL; in in_illuminance_calibrate_store()
513 mutex_lock(&chip->als_mutex); in in_illuminance_calibrate_store()
521 mutex_unlock(&chip->als_mutex); in in_illuminance_calibrate_store()
535 for (i = 0; i < ARRAY_SIZE(chip->als_settings.als_device_lux); i++) { in in_illuminance_lux_table_show()
537 chip->als_settings.als_device_lux[i].ratio, in in_illuminance_lux_table_show()
538 chip->als_settings.als_device_lux[i].ch0, in in_illuminance_lux_table_show()
539 chip->als_settings.als_device_lux[i].ch1); in in_illuminance_lux_table_show()
540 if (chip->als_settings.als_device_lux[i].ratio == 0) { in in_illuminance_lux_table_show()
545 offset--; in in_illuminance_lux_table_show()
563 int ret = -EINVAL; in in_illuminance_lux_table_store()
566 mutex_lock(&chip->als_mutex); in in_illuminance_lux_table_store()
583 if ((value[n - 2] | value[n - 1] | value[n]) != 0) { in in_illuminance_lux_table_store()
589 memcpy(chip->als_settings.als_device_lux, &value[1], in in_illuminance_lux_table_store()
595 mutex_unlock(&chip->als_mutex); in in_illuminance_lux_table_store()
647 ret = pm_runtime_resume_and_get(&chip->client->dev); in tsl2583_set_pm_runtime_busy()
649 pm_runtime_mark_last_busy(&chip->client->dev); in tsl2583_set_pm_runtime_busy()
650 ret = pm_runtime_put_autosuspend(&chip->client->dev); in tsl2583_set_pm_runtime_busy()
667 mutex_lock(&chip->als_mutex); in tsl2583_read_raw()
669 ret = -EINVAL; in tsl2583_read_raw()
672 if (chan->type == IIO_LIGHT) { in tsl2583_read_raw()
686 if (chan->channel2 == IIO_MOD_LIGHT_BOTH) in tsl2583_read_raw()
687 *val = chip->als_cur_info.als_ch0; in tsl2583_read_raw()
689 *val = chip->als_cur_info.als_ch1; in tsl2583_read_raw()
695 if (chan->type == IIO_LIGHT) { in tsl2583_read_raw()
705 if (chan->type == IIO_LIGHT) { in tsl2583_read_raw()
706 *val = chip->als_settings.als_gain_trim; in tsl2583_read_raw()
711 if (chan->type == IIO_LIGHT) { in tsl2583_read_raw()
712 *val = gainadj[chip->als_settings.als_gain].mean; in tsl2583_read_raw()
717 if (chan->type == IIO_LIGHT) { in tsl2583_read_raw()
719 *val2 = chip->als_settings.als_time; in tsl2583_read_raw()
728 mutex_unlock(&chip->als_mutex); in tsl2583_read_raw()
758 mutex_lock(&chip->als_mutex); in tsl2583_write_raw()
760 ret = -EINVAL; in tsl2583_write_raw()
763 if (chan->type == IIO_LIGHT) { in tsl2583_write_raw()
764 chip->als_settings.als_gain_trim = val; in tsl2583_write_raw()
769 if (chan->type == IIO_LIGHT) { in tsl2583_write_raw()
774 chip->als_settings.als_gain = i; in tsl2583_write_raw()
782 if (chan->type == IIO_LIGHT && !val && val2 >= 50 && in tsl2583_write_raw()
784 chip->als_settings.als_time = val2; in tsl2583_write_raw()
792 mutex_unlock(&chip->als_mutex); in tsl2583_write_raw()
818 if (!i2c_check_functionality(clientp->adapter, in tsl2583_probe()
820 dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n", in tsl2583_probe()
822 return -EOPNOTSUPP; in tsl2583_probe()
825 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip)); in tsl2583_probe()
827 return -ENOMEM; in tsl2583_probe()
830 chip->client = clientp; in tsl2583_probe()
833 mutex_init(&chip->als_mutex); in tsl2583_probe()
838 dev_err(&clientp->dev, in tsl2583_probe()
844 dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n", in tsl2583_probe()
846 return -EINVAL; in tsl2583_probe()
849 indio_dev->info = &tsl2583_info; in tsl2583_probe()
850 indio_dev->channels = tsl2583_channels; in tsl2583_probe()
851 indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels); in tsl2583_probe()
852 indio_dev->modes = INDIO_DIRECT_MODE; in tsl2583_probe()
853 indio_dev->name = chip->client->name; in tsl2583_probe()
855 pm_runtime_enable(&clientp->dev); in tsl2583_probe()
856 pm_runtime_set_autosuspend_delay(&clientp->dev, in tsl2583_probe()
858 pm_runtime_use_autosuspend(&clientp->dev); in tsl2583_probe()
862 dev_err(&clientp->dev, "%s: iio registration failed\n", in tsl2583_probe()
870 dev_info(&clientp->dev, "Light sensor found.\n"); in tsl2583_probe()
882 pm_runtime_disable(&client->dev); in tsl2583_remove()
883 pm_runtime_set_suspended(&client->dev); in tsl2583_remove()
894 mutex_lock(&chip->als_mutex); in tsl2583_suspend()
898 mutex_unlock(&chip->als_mutex); in tsl2583_suspend()
909 mutex_lock(&chip->als_mutex); in tsl2583_resume()
913 mutex_unlock(&chip->als_mutex); in tsl2583_resume()
924 { "tsl2583", 2 },