1 // SPDX-License-Identifier: GPL-2.0-only
3 * apds9300.c - IIO driver for Avago APDS9300 ambient light sensor
8 #include <linux/module.h>
9 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/err.h>
13 #include <linux/mutex.h>
14 #include <linux/interrupt.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/events.h>
19 #define APDS9300_DRV_NAME "apds9300"
20 #define APDS9300_IRQ_NAME "apds9300_event"
22 /* Command register bits */
23 #define APDS9300_CMD BIT(7) /* Select command register. Must write as 1 */
24 #define APDS9300_WORD BIT(5) /* I2C write/read: if 1 word, if 0 byte */
25 #define APDS9300_CLEAR BIT(6) /* Interrupt clear. Clears pending interrupt */
28 #define APDS9300_CONTROL 0x00 /* Control of basic functions */
29 #define APDS9300_THRESHLOWLOW 0x02 /* Low byte of low interrupt threshold */
30 #define APDS9300_THRESHHIGHLOW 0x04 /* Low byte of high interrupt threshold */
31 #define APDS9300_INTERRUPT 0x06 /* Interrupt control */
32 #define APDS9300_DATA0LOW 0x0c /* Low byte of ADC channel 0 */
33 #define APDS9300_DATA1LOW 0x0e /* Low byte of ADC channel 1 */
35 /* Power on/off value for APDS9300_CONTROL register */
36 #define APDS9300_POWER_ON 0x03
37 #define APDS9300_POWER_OFF 0x00
40 #define APDS9300_INTR_ENABLE 0x10
41 /* Interrupt Persist Function: Any value outside of threshold range */
42 #define APDS9300_THRESH_INTR 0x01
44 #define APDS9300_THRESH_MAX 0xffff /* Max threshold value */
46 struct apds9300_data {
47 struct i2c_client *client;
57 /* Calculated values 1000 * (CH1/CH0)^1.4 for CH1/CH0 from 0 to 0.52 */
58 static const u16 apds9300_lux_ratio[] = {
59 0, 2, 4, 7, 11, 15, 19, 24, 29, 34, 40, 45, 51, 57, 64, 70, 77, 84, 91,
60 98, 105, 112, 120, 128, 136, 144, 152, 160, 168, 177, 185, 194, 203,
61 212, 221, 230, 239, 249, 258, 268, 277, 287, 297, 307, 317, 327, 337,
62 347, 358, 368, 379, 390, 400,
65 static unsigned long apds9300_calculate_lux(u16 ch0, u16 ch1)
67 unsigned long lux, tmp;
69 /* avoid division by zero */
73 tmp = DIV_ROUND_UP(ch1 * 100, ch0);
75 lux = 3150 * ch0 - (unsigned long)DIV_ROUND_UP_ULL(ch0
76 * apds9300_lux_ratio[tmp] * 5930ull, 1000);
77 } else if (tmp <= 65) {
78 lux = 2290 * ch0 - 2910 * ch1;
79 } else if (tmp <= 80) {
80 lux = 1570 * ch0 - 1800 * ch1;
81 } else if (tmp <= 130) {
82 lux = 338 * ch0 - 260 * ch1;
90 static int apds9300_get_adc_val(struct apds9300_data *data, int adc_number)
93 u8 flags = APDS9300_CMD | APDS9300_WORD;
95 if (!data->power_state)
98 /* Select ADC0 or ADC1 data register */
99 flags |= adc_number ? APDS9300_DATA1LOW : APDS9300_DATA0LOW;
101 ret = i2c_smbus_read_word_data(data->client, flags);
103 dev_err(&data->client->dev,
104 "failed to read ADC%d value\n", adc_number);
109 static int apds9300_set_thresh_low(struct apds9300_data *data, int value)
113 if (!data->power_state)
116 if (value > APDS9300_THRESH_MAX)
119 ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHLOWLOW
120 | APDS9300_CMD | APDS9300_WORD, value);
122 dev_err(&data->client->dev, "failed to set thresh_low\n");
125 data->thresh_low = value;
130 static int apds9300_set_thresh_hi(struct apds9300_data *data, int value)
134 if (!data->power_state)
137 if (value > APDS9300_THRESH_MAX)
140 ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHHIGHLOW
141 | APDS9300_CMD | APDS9300_WORD, value);
143 dev_err(&data->client->dev, "failed to set thresh_hi\n");
146 data->thresh_hi = value;
151 static int apds9300_set_intr_state(struct apds9300_data *data, int state)
156 if (!data->power_state)
159 cmd = state ? APDS9300_INTR_ENABLE | APDS9300_THRESH_INTR : 0x00;
160 ret = i2c_smbus_write_byte_data(data->client,
161 APDS9300_INTERRUPT | APDS9300_CMD, cmd);
163 dev_err(&data->client->dev,
164 "failed to set interrupt state %d\n", state);
167 data->intr_en = state;
172 static int apds9300_set_power_state(struct apds9300_data *data, int state)
177 cmd = state ? APDS9300_POWER_ON : APDS9300_POWER_OFF;
178 ret = i2c_smbus_write_byte_data(data->client,
179 APDS9300_CONTROL | APDS9300_CMD, cmd);
181 dev_err(&data->client->dev,
182 "failed to set power state %d\n", state);
185 data->power_state = state;
190 static void apds9300_clear_intr(struct apds9300_data *data)
194 ret = i2c_smbus_write_byte(data->client, APDS9300_CLEAR | APDS9300_CMD);
196 dev_err(&data->client->dev, "failed to clear interrupt\n");
199 static int apds9300_chip_init(struct apds9300_data *data)
203 /* Need to set power off to ensure that the chip is off */
204 ret = apds9300_set_power_state(data, 0);
208 * Probe the chip. To do so we try to power up the device and then to
209 * read back the 0x03 code
211 ret = apds9300_set_power_state(data, 1);
214 ret = i2c_smbus_read_byte_data(data->client,
215 APDS9300_CONTROL | APDS9300_CMD);
216 if (ret != APDS9300_POWER_ON) {
221 * Disable interrupt to ensure thai it is doesn't enable
222 * i.e. after device soft reset
224 ret = apds9300_set_intr_state(data, 0);
231 dev_err(&data->client->dev, "failed to init the chip\n");
235 static int apds9300_read_raw(struct iio_dev *indio_dev,
236 struct iio_chan_spec const *chan, int *val, int *val2,
239 int ch0, ch1, ret = -EINVAL;
240 struct apds9300_data *data = iio_priv(indio_dev);
242 mutex_lock(&data->mutex);
243 switch (chan->type) {
245 ch0 = apds9300_get_adc_val(data, 0);
250 ch1 = apds9300_get_adc_val(data, 1);
255 *val = apds9300_calculate_lux(ch0, ch1);
259 ret = apds9300_get_adc_val(data, chan->channel);
268 mutex_unlock(&data->mutex);
273 static int apds9300_read_thresh(struct iio_dev *indio_dev,
274 const struct iio_chan_spec *chan, enum iio_event_type type,
275 enum iio_event_direction dir, enum iio_event_info info,
278 struct apds9300_data *data = iio_priv(indio_dev);
281 case IIO_EV_DIR_RISING:
282 *val = data->thresh_hi;
284 case IIO_EV_DIR_FALLING:
285 *val = data->thresh_low;
294 static int apds9300_write_thresh(struct iio_dev *indio_dev,
295 const struct iio_chan_spec *chan, enum iio_event_type type,
296 enum iio_event_direction dir, enum iio_event_info info, int val,
299 struct apds9300_data *data = iio_priv(indio_dev);
302 mutex_lock(&data->mutex);
303 if (dir == IIO_EV_DIR_RISING)
304 ret = apds9300_set_thresh_hi(data, val);
306 ret = apds9300_set_thresh_low(data, val);
307 mutex_unlock(&data->mutex);
312 static int apds9300_read_interrupt_config(struct iio_dev *indio_dev,
313 const struct iio_chan_spec *chan,
314 enum iio_event_type type,
315 enum iio_event_direction dir)
317 struct apds9300_data *data = iio_priv(indio_dev);
319 return data->intr_en;
322 static int apds9300_write_interrupt_config(struct iio_dev *indio_dev,
323 const struct iio_chan_spec *chan, enum iio_event_type type,
324 enum iio_event_direction dir, int state)
326 struct apds9300_data *data = iio_priv(indio_dev);
329 mutex_lock(&data->mutex);
330 ret = apds9300_set_intr_state(data, state);
331 mutex_unlock(&data->mutex);
336 static const struct iio_info apds9300_info_no_irq = {
337 .read_raw = apds9300_read_raw,
340 static const struct iio_info apds9300_info = {
341 .read_raw = apds9300_read_raw,
342 .read_event_value = apds9300_read_thresh,
343 .write_event_value = apds9300_write_thresh,
344 .read_event_config = apds9300_read_interrupt_config,
345 .write_event_config = apds9300_write_interrupt_config,
348 static const struct iio_event_spec apds9300_event_spec[] = {
350 .type = IIO_EV_TYPE_THRESH,
351 .dir = IIO_EV_DIR_RISING,
352 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
353 BIT(IIO_EV_INFO_ENABLE),
355 .type = IIO_EV_TYPE_THRESH,
356 .dir = IIO_EV_DIR_FALLING,
357 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
358 BIT(IIO_EV_INFO_ENABLE),
362 static const struct iio_chan_spec apds9300_channels[] = {
367 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
369 .type = IIO_INTENSITY,
371 .channel2 = IIO_MOD_LIGHT_BOTH,
373 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
374 .event_spec = apds9300_event_spec,
375 .num_event_specs = ARRAY_SIZE(apds9300_event_spec),
377 .type = IIO_INTENSITY,
379 .channel2 = IIO_MOD_LIGHT_IR,
381 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
385 static irqreturn_t apds9300_interrupt_handler(int irq, void *private)
387 struct iio_dev *dev_info = private;
388 struct apds9300_data *data = iio_priv(dev_info);
390 iio_push_event(dev_info,
391 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
394 iio_get_time_ns(dev_info));
396 apds9300_clear_intr(data);
401 static int apds9300_probe(struct i2c_client *client)
403 struct apds9300_data *data;
404 struct iio_dev *indio_dev;
407 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
411 data = iio_priv(indio_dev);
412 i2c_set_clientdata(client, indio_dev);
413 data->client = client;
415 ret = apds9300_chip_init(data);
419 mutex_init(&data->mutex);
421 indio_dev->channels = apds9300_channels;
422 indio_dev->num_channels = ARRAY_SIZE(apds9300_channels);
423 indio_dev->name = APDS9300_DRV_NAME;
424 indio_dev->modes = INDIO_DIRECT_MODE;
427 indio_dev->info = &apds9300_info;
429 indio_dev->info = &apds9300_info_no_irq;
432 ret = devm_request_threaded_irq(&client->dev, client->irq,
433 NULL, apds9300_interrupt_handler,
434 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
435 APDS9300_IRQ_NAME, indio_dev);
437 dev_err(&client->dev, "irq request error %d\n", -ret);
442 ret = iio_device_register(indio_dev);
449 /* Ensure that power off in case of error */
450 apds9300_set_power_state(data, 0);
454 static void apds9300_remove(struct i2c_client *client)
456 struct iio_dev *indio_dev = i2c_get_clientdata(client);
457 struct apds9300_data *data = iio_priv(indio_dev);
459 iio_device_unregister(indio_dev);
461 /* Ensure that power off and interrupts are disabled */
462 apds9300_set_intr_state(data, 0);
463 apds9300_set_power_state(data, 0);
466 static int apds9300_suspend(struct device *dev)
468 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
469 struct apds9300_data *data = iio_priv(indio_dev);
472 mutex_lock(&data->mutex);
473 ret = apds9300_set_power_state(data, 0);
474 mutex_unlock(&data->mutex);
479 static int apds9300_resume(struct device *dev)
481 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
482 struct apds9300_data *data = iio_priv(indio_dev);
485 mutex_lock(&data->mutex);
486 ret = apds9300_set_power_state(data, 1);
487 mutex_unlock(&data->mutex);
492 static DEFINE_SIMPLE_DEV_PM_OPS(apds9300_pm_ops, apds9300_suspend,
495 static const struct i2c_device_id apds9300_id[] = {
496 { APDS9300_DRV_NAME, 0 },
500 MODULE_DEVICE_TABLE(i2c, apds9300_id);
502 static struct i2c_driver apds9300_driver = {
504 .name = APDS9300_DRV_NAME,
505 .pm = pm_sleep_ptr(&apds9300_pm_ops),
507 .probe = apds9300_probe,
508 .remove = apds9300_remove,
509 .id_table = apds9300_id,
512 module_i2c_driver(apds9300_driver);
515 MODULE_AUTHOR("GlobalLogic inc.");
516 MODULE_DESCRIPTION("APDS9300 ambient light photo sensor driver");
517 MODULE_LICENSE("GPL");