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
57 /* Calculated values 1000 * (CH1/CH0)^1.4 for CH1/CH0 from 0 to 0.52 */
59 0, 2, 4, 7, 11, 15, 19, 24, 29, 34, 40, 45, 51, 57, 64, 70, 77, 84, 91,
65 static unsigned long apds9300_calculate_lux(u16 ch0, u16 ch1) in apds9300_calculate_lux() argument
70 if (ch0 == 0) in apds9300_calculate_lux()
73 tmp = DIV_ROUND_UP(ch1 * 100, ch0); in apds9300_calculate_lux()
75 lux = 3150 * ch0 - (unsigned long)DIV_ROUND_UP_ULL(ch0 in apds9300_calculate_lux()
78 lux = 2290 * ch0 - 2910 * ch1; in apds9300_calculate_lux()
80 lux = 1570 * ch0 - 1800 * ch1; in apds9300_calculate_lux()
82 lux = 338 * ch0 - 260 * ch1; in apds9300_calculate_lux()
95 if (!data->power_state) in apds9300_get_adc_val()
96 return -EBUSY; in apds9300_get_adc_val()
101 ret = i2c_smbus_read_word_data(data->client, flags); in apds9300_get_adc_val()
103 dev_err(&data->client->dev, in apds9300_get_adc_val()
113 if (!data->power_state) in apds9300_set_thresh_low()
114 return -EBUSY; in apds9300_set_thresh_low()
117 return -EINVAL; in apds9300_set_thresh_low()
119 ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHLOWLOW in apds9300_set_thresh_low()
122 dev_err(&data->client->dev, "failed to set thresh_low\n"); in apds9300_set_thresh_low()
125 data->thresh_low = value; in apds9300_set_thresh_low()
134 if (!data->power_state) in apds9300_set_thresh_hi()
135 return -EBUSY; in apds9300_set_thresh_hi()
138 return -EINVAL; in apds9300_set_thresh_hi()
140 ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHHIGHLOW in apds9300_set_thresh_hi()
143 dev_err(&data->client->dev, "failed to set thresh_hi\n"); in apds9300_set_thresh_hi()
146 data->thresh_hi = value; in apds9300_set_thresh_hi()
156 if (!data->power_state) in apds9300_set_intr_state()
157 return -EBUSY; in apds9300_set_intr_state()
160 ret = i2c_smbus_write_byte_data(data->client, in apds9300_set_intr_state()
163 dev_err(&data->client->dev, in apds9300_set_intr_state()
167 data->intr_en = state; in apds9300_set_intr_state()
178 ret = i2c_smbus_write_byte_data(data->client, in apds9300_set_power_state()
181 dev_err(&data->client->dev, in apds9300_set_power_state()
185 data->power_state = state; in apds9300_set_power_state()
194 ret = i2c_smbus_write_byte(data->client, APDS9300_CLEAR | APDS9300_CMD); in apds9300_clear_intr()
196 dev_err(&data->client->dev, "failed to clear interrupt\n"); in apds9300_clear_intr()
214 ret = i2c_smbus_read_byte_data(data->client, in apds9300_chip_init()
217 ret = -ENODEV; in apds9300_chip_init()
231 dev_err(&data->client->dev, "failed to init the chip\n"); in apds9300_chip_init()
239 int ch0, ch1, ret = -EINVAL; in apds9300_read_raw() local
242 mutex_lock(&data->mutex); in apds9300_read_raw()
243 switch (chan->type) { in apds9300_read_raw()
245 ch0 = apds9300_get_adc_val(data, 0); in apds9300_read_raw()
246 if (ch0 < 0) { in apds9300_read_raw()
247 ret = ch0; in apds9300_read_raw()
255 *val = apds9300_calculate_lux(ch0, ch1); in apds9300_read_raw()
259 ret = apds9300_get_adc_val(data, chan->channel); in apds9300_read_raw()
268 mutex_unlock(&data->mutex); in apds9300_read_raw()
282 *val = data->thresh_hi; in apds9300_read_thresh()
285 *val = data->thresh_low; in apds9300_read_thresh()
288 return -EINVAL; in apds9300_read_thresh()
302 mutex_lock(&data->mutex); in apds9300_write_thresh()
307 mutex_unlock(&data->mutex); in apds9300_write_thresh()
319 return data->intr_en; in apds9300_read_interrupt_config()
329 mutex_lock(&data->mutex); in apds9300_write_interrupt_config()
331 mutex_unlock(&data->mutex); in apds9300_write_interrupt_config()
407 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in apds9300_probe()
409 return -ENOMEM; in apds9300_probe()
413 data->client = client; in apds9300_probe()
419 mutex_init(&data->mutex); in apds9300_probe()
421 indio_dev->channels = apds9300_channels; in apds9300_probe()
422 indio_dev->num_channels = ARRAY_SIZE(apds9300_channels); in apds9300_probe()
423 indio_dev->name = APDS9300_DRV_NAME; in apds9300_probe()
424 indio_dev->modes = INDIO_DIRECT_MODE; in apds9300_probe()
426 if (client->irq) in apds9300_probe()
427 indio_dev->info = &apds9300_info; in apds9300_probe()
429 indio_dev->info = &apds9300_info_no_irq; in apds9300_probe()
431 if (client->irq) { in apds9300_probe()
432 ret = devm_request_threaded_irq(&client->dev, client->irq, in apds9300_probe()
437 dev_err(&client->dev, "irq request error %d\n", -ret); in apds9300_probe()
472 mutex_lock(&data->mutex); in apds9300_suspend()
474 mutex_unlock(&data->mutex); in apds9300_suspend()
485 mutex_lock(&data->mutex); in apds9300_resume()
487 mutex_unlock(&data->mutex); in apds9300_resume()