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

1 // SPDX-License-Identifier: GPL-2.0-only
3 * apds9300.c - IIO driver for Avago APDS9300 ambient light sensor
56 /* Calculated values 1000 * (CH1/CH0)^1.4 for CH1/CH0 from 0 to 0.52 */
58 0, 2, 4, 7, 11, 15, 19, 24, 29, 34, 40, 45, 51, 57, 64, 70, 77, 84, 91,
64 static unsigned long apds9300_calculate_lux(u16 ch0, u16 ch1) in apds9300_calculate_lux() argument
69 if (ch0 == 0) in apds9300_calculate_lux()
72 tmp = DIV_ROUND_UP(ch1 * 100, ch0); in apds9300_calculate_lux()
74 lux = 3150 * ch0 - (unsigned long)DIV_ROUND_UP_ULL(ch0 in apds9300_calculate_lux()
77 lux = 2290 * ch0 - 2910 * ch1; in apds9300_calculate_lux()
79 lux = 1570 * ch0 - 1800 * ch1; in apds9300_calculate_lux()
81 lux = 338 * ch0 - 260 * ch1; in apds9300_calculate_lux()
94 if (!data->power_state) in apds9300_get_adc_val()
95 return -EBUSY; in apds9300_get_adc_val()
100 ret = i2c_smbus_read_word_data(data->client, flags); in apds9300_get_adc_val()
102 dev_err(&data->client->dev, in apds9300_get_adc_val()
112 if (!data->power_state) in apds9300_set_thresh_low()
113 return -EBUSY; in apds9300_set_thresh_low()
116 return -EINVAL; in apds9300_set_thresh_low()
118 ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHLOWLOW in apds9300_set_thresh_low()
121 dev_err(&data->client->dev, "failed to set thresh_low\n"); in apds9300_set_thresh_low()
124 data->thresh_low = value; in apds9300_set_thresh_low()
133 if (!data->power_state) in apds9300_set_thresh_hi()
134 return -EBUSY; in apds9300_set_thresh_hi()
137 return -EINVAL; in apds9300_set_thresh_hi()
139 ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHHIGHLOW in apds9300_set_thresh_hi()
142 dev_err(&data->client->dev, "failed to set thresh_hi\n"); in apds9300_set_thresh_hi()
145 data->thresh_hi = value; in apds9300_set_thresh_hi()
155 if (!data->power_state) in apds9300_set_intr_state()
156 return -EBUSY; in apds9300_set_intr_state()
159 ret = i2c_smbus_write_byte_data(data->client, in apds9300_set_intr_state()
162 dev_err(&data->client->dev, in apds9300_set_intr_state()
166 data->intr_en = state; in apds9300_set_intr_state()
177 ret = i2c_smbus_write_byte_data(data->client, in apds9300_set_power_state()
180 dev_err(&data->client->dev, in apds9300_set_power_state()
184 data->power_state = state; in apds9300_set_power_state()
193 ret = i2c_smbus_write_byte(data->client, APDS9300_CLEAR | APDS9300_CMD); in apds9300_clear_intr()
195 dev_err(&data->client->dev, "failed to clear interrupt\n"); in apds9300_clear_intr()
213 ret = i2c_smbus_read_byte_data(data->client, in apds9300_chip_init()
216 ret = -ENODEV; in apds9300_chip_init()
230 dev_err(&data->client->dev, "failed to init the chip\n"); in apds9300_chip_init()
238 int ch0, ch1, ret = -EINVAL; in apds9300_read_raw() local
241 mutex_lock(&data->mutex); in apds9300_read_raw()
242 switch (chan->type) { in apds9300_read_raw()
244 ch0 = apds9300_get_adc_val(data, 0); in apds9300_read_raw()
245 if (ch0 < 0) { in apds9300_read_raw()
246 ret = ch0; in apds9300_read_raw()
254 *val = apds9300_calculate_lux(ch0, ch1); in apds9300_read_raw()
258 ret = apds9300_get_adc_val(data, chan->channel); in apds9300_read_raw()
267 mutex_unlock(&data->mutex); in apds9300_read_raw()
281 *val = data->thresh_hi; in apds9300_read_thresh()
284 *val = data->thresh_low; in apds9300_read_thresh()
287 return -EINVAL; in apds9300_read_thresh()
301 mutex_lock(&data->mutex); in apds9300_write_thresh()
306 mutex_unlock(&data->mutex); in apds9300_write_thresh()
318 return data->intr_en; in apds9300_read_interrupt_config()
328 mutex_lock(&data->mutex); in apds9300_write_interrupt_config()
330 mutex_unlock(&data->mutex); in apds9300_write_interrupt_config()
406 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in apds9300_probe()
408 return -ENOMEM; in apds9300_probe()
412 data->client = client; in apds9300_probe()
418 mutex_init(&data->mutex); in apds9300_probe()
420 indio_dev->channels = apds9300_channels; in apds9300_probe()
421 indio_dev->num_channels = ARRAY_SIZE(apds9300_channels); in apds9300_probe()
422 indio_dev->name = APDS9300_DRV_NAME; in apds9300_probe()
423 indio_dev->modes = INDIO_DIRECT_MODE; in apds9300_probe()
425 if (client->irq) in apds9300_probe()
426 indio_dev->info = &apds9300_info; in apds9300_probe()
428 indio_dev->info = &apds9300_info_no_irq; in apds9300_probe()
430 if (client->irq) { in apds9300_probe()
431 ret = devm_request_threaded_irq(&client->dev, client->irq, in apds9300_probe()
436 dev_err(&client->dev, "irq request error %d\n", -ret); in apds9300_probe()
471 mutex_lock(&data->mutex); in apds9300_suspend()
473 mutex_unlock(&data->mutex); in apds9300_suspend()
484 mutex_lock(&data->mutex); in apds9300_resume()
486 mutex_unlock(&data->mutex); in apds9300_resume()