1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of the APDS990x sensor driver.
4 * Chip is combined proximity and ambient light sensor.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/mutex.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/delay.h>
19 #include <linux/wait.h>
20 #include <linux/slab.h>
21 #include <linux/platform_data/apds990x.h>
24 #define APDS990X_ENABLE 0x00 /* Enable of states and interrupts */
25 #define APDS990X_ATIME 0x01 /* ALS ADC time */
26 #define APDS990X_PTIME 0x02 /* Proximity ADC time */
27 #define APDS990X_WTIME 0x03 /* Wait time */
28 #define APDS990X_AILTL 0x04 /* ALS interrupt low threshold low byte */
29 #define APDS990X_AILTH 0x05 /* ALS interrupt low threshold hi byte */
30 #define APDS990X_AIHTL 0x06 /* ALS interrupt hi threshold low byte */
31 #define APDS990X_AIHTH 0x07 /* ALS interrupt hi threshold hi byte */
32 #define APDS990X_PILTL 0x08 /* Proximity interrupt low threshold low byte */
33 #define APDS990X_PILTH 0x09 /* Proximity interrupt low threshold hi byte */
34 #define APDS990X_PIHTL 0x0a /* Proximity interrupt hi threshold low byte */
35 #define APDS990X_PIHTH 0x0b /* Proximity interrupt hi threshold hi byte */
36 #define APDS990X_PERS 0x0c /* Interrupt persistence filters */
37 #define APDS990X_CONFIG 0x0d /* Configuration */
38 #define APDS990X_PPCOUNT 0x0e /* Proximity pulse count */
39 #define APDS990X_CONTROL 0x0f /* Gain control register */
40 #define APDS990X_REV 0x11 /* Revision Number */
41 #define APDS990X_ID 0x12 /* Device ID */
42 #define APDS990X_STATUS 0x13 /* Device status */
43 #define APDS990X_CDATAL 0x14 /* Clear ADC low data register */
44 #define APDS990X_CDATAH 0x15 /* Clear ADC high data register */
45 #define APDS990X_IRDATAL 0x16 /* IR ADC low data register */
46 #define APDS990X_IRDATAH 0x17 /* IR ADC high data register */
47 #define APDS990X_PDATAL 0x18 /* Proximity ADC low data register */
48 #define APDS990X_PDATAH 0x19 /* Proximity ADC high data register */
51 #define APDS990X_MAX_AGAIN 3
54 #define APDS990X_EN_PIEN (0x1 << 5)
55 #define APDS990X_EN_AIEN (0x1 << 4)
56 #define APDS990X_EN_WEN (0x1 << 3)
57 #define APDS990X_EN_PEN (0x1 << 2)
58 #define APDS990X_EN_AEN (0x1 << 1)
59 #define APDS990X_EN_PON (0x1 << 0)
60 #define APDS990X_EN_DISABLE_ALL 0
63 #define APDS990X_ST_PINT (0x1 << 5)
64 #define APDS990X_ST_AINT (0x1 << 4)
66 /* I2C access types */
67 #define APDS990x_CMD_TYPE_MASK (0x03 << 5)
68 #define APDS990x_CMD_TYPE_RB (0x00 << 5) /* Repeated byte */
69 #define APDS990x_CMD_TYPE_INC (0x01 << 5) /* Auto increment */
70 #define APDS990x_CMD_TYPE_SPE (0x03 << 5) /* Special function */
72 #define APDS990x_ADDR_SHIFT 0
73 #define APDS990x_CMD 0x80
75 /* Interrupt ack commands */
76 #define APDS990X_INT_ACK_ALS 0x6
77 #define APDS990X_INT_ACK_PS 0x5
78 #define APDS990X_INT_ACK_BOTH 0x7
81 #define APDS990X_PTIME_DEFAULT 0xff /* Recommended conversion time 2.7ms*/
84 #define APDS990X_WTIME_DEFAULT 0xee /* ~50ms wait time */
86 #define APDS990X_TIME_TO_ADC 1024 /* One timetick as ADC count value */
89 #define APDS990X_APERS_SHIFT 0
90 #define APDS990X_PPERS_SHIFT 4
93 #define APDS990X_ID_0 0x0
94 #define APDS990X_ID_4 0x4
95 #define APDS990X_ID_29 0x29
97 /* pgain and pdiode settings */
98 #define APDS_PGAIN_1X 0x0
99 #define APDS_PDIODE_IR 0x2
101 #define APDS990X_LUX_OUTPUT_SCALE 10
103 /* Reverse chip factors for threshold calculation */
104 struct reverse_factors {
112 struct apds990x_chip {
113 struct apds990x_platform_data *pdata;
114 struct i2c_client *client;
115 struct mutex mutex; /* avoid parallel access */
116 struct regulator_bulk_data regs[2];
117 wait_queue_head_t wait;
120 bool prox_continuous_mode;
121 bool lux_wait_fresh_res;
123 /* Chip parameters */
124 struct apds990x_chip_factors cf;
125 struct reverse_factors rcf;
126 u16 atime; /* als integration time */
127 u16 arate; /* als reporting rate */
128 u16 a_max_result; /* Max possible ADC value with current atime */
129 u8 again_meas; /* Gain used in last measurement */
130 u8 again_next; /* Next calculated gain */
153 #define APDS_CALIB_SCALER 8192
154 #define APDS_LUX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
155 #define APDS_PROX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
157 #define APDS_PROX_DEF_THRES 600
158 #define APDS_PROX_HYSTERESIS 50
159 #define APDS_LUX_DEF_THRES_HI 101
160 #define APDS_LUX_DEF_THRES_LO 100
161 #define APDS_DEFAULT_PROX_PERS 1
163 #define APDS_TIMEOUT 2000
164 #define APDS_STARTUP_DELAY 25000 /* us */
165 #define APDS_RANGE 65535
166 #define APDS_PROX_RANGE 1023
167 #define APDS_LUX_GAIN_LO_LIMIT 100
168 #define APDS_LUX_GAIN_LO_LIMIT_STRICT 25
170 #define TIMESTEP 87 /* 2.7ms is about 87 / 32 */
171 #define TIME_STEP_SCALER 32
173 #define APDS_LUX_AVERAGING_TIME 50 /* tolerates 50/60Hz ripple */
174 #define APDS_LUX_DEFAULT_RATE 200
176 static const u8 again[] = {1, 8, 16, 120}; /* ALS gain steps */
178 /* Following two tables must match i.e 10Hz rate means 1 as persistence value */
179 static const u16 arates_hz[] = {10, 5, 2, 1};
180 static const u8 apersis[] = {1, 2, 4, 5};
183 static const char reg_vcc[] = "Vdd";
184 static const char reg_vled[] = "Vled";
186 static int apds990x_read_byte(struct apds990x_chip *chip, u8 reg, u8 *data)
188 struct i2c_client *client = chip->client;
191 reg &= ~APDS990x_CMD_TYPE_MASK;
192 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
194 ret = i2c_smbus_read_byte_data(client, reg);
199 static int apds990x_read_word(struct apds990x_chip *chip, u8 reg, u16 *data)
201 struct i2c_client *client = chip->client;
204 reg &= ~APDS990x_CMD_TYPE_MASK;
205 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
207 ret = i2c_smbus_read_word_data(client, reg);
212 static int apds990x_write_byte(struct apds990x_chip *chip, u8 reg, u8 data)
214 struct i2c_client *client = chip->client;
217 reg &= ~APDS990x_CMD_TYPE_MASK;
218 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
220 ret = i2c_smbus_write_byte_data(client, reg, data);
224 static int apds990x_write_word(struct apds990x_chip *chip, u8 reg, u16 data)
226 struct i2c_client *client = chip->client;
229 reg &= ~APDS990x_CMD_TYPE_MASK;
230 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
232 ret = i2c_smbus_write_word_data(client, reg, data);
236 static int apds990x_mode_on(struct apds990x_chip *chip)
238 /* ALS is mandatory, proximity optional */
239 u8 reg = APDS990X_EN_AIEN | APDS990X_EN_PON | APDS990X_EN_AEN |
243 reg |= APDS990X_EN_PIEN | APDS990X_EN_PEN;
245 return apds990x_write_byte(chip, APDS990X_ENABLE, reg);
248 static u16 apds990x_lux_to_threshold(struct apds990x_chip *chip, u32 lux)
256 else if (lux == APDS_RANGE)
260 * Reported LUX value is a combination of the IR and CLEAR channel
261 * values. However, interrupt threshold is only for clear channel.
262 * This function approximates needed HW threshold value for a given
263 * LUX value in the current lightning type.
264 * IR level compared to visible light varies heavily depending on the
265 * source of the light
267 * Calculate threshold value for the next measurement period.
268 * Math: threshold = lux * cpl where
269 * cpl = atime * again / (glass_attenuation * device_factor)
272 * First remove calibration. Division by four is to avoid overflow
274 lux = lux * (APDS_CALIB_SCALER / 4) / (chip->lux_calib / 4);
276 /* Multiplication by 64 is to increase accuracy */
277 cpl = ((u32)chip->atime * (u32)again[chip->again_next] *
278 APDS_PARAM_SCALE * 64) / (chip->cf.ga * chip->cf.df);
280 thres = lux * cpl / 64;
282 * Convert IR light from the latest result to match with
283 * new gain step. This helps to adapt with the current
286 ir = (u32)chip->lux_ir * (u32)again[chip->again_next] /
287 (u32)again[chip->again_meas];
290 * Compensate count with IR light impact
291 * IAC1 > IAC2 (see apds990x_get_lux for formulas)
293 if (chip->lux_clear * APDS_PARAM_SCALE >=
294 chip->rcf.afactor * chip->lux_ir)
295 thres = (chip->rcf.cf1 * thres + chip->rcf.irf1 * ir) /
298 thres = (chip->rcf.cf2 * thres + chip->rcf.irf2 * ir) /
301 if (thres >= chip->a_max_result)
302 thres = chip->a_max_result - 1;
306 static inline int apds990x_set_atime(struct apds990x_chip *chip, u32 time_ms)
310 chip->atime = time_ms;
311 /* Formula is specified in the data sheet */
312 reg_value = 256 - ((time_ms * TIME_STEP_SCALER) / TIMESTEP);
313 /* Calculate max ADC value for given integration time */
314 chip->a_max_result = (u16)(256 - reg_value) * APDS990X_TIME_TO_ADC;
315 return apds990x_write_byte(chip, APDS990X_ATIME, reg_value);
318 /* Called always with mutex locked */
319 static int apds990x_refresh_pthres(struct apds990x_chip *chip, int data)
323 /* If the chip is not in use, don't try to access it */
324 if (pm_runtime_suspended(&chip->client->dev))
327 if (data < chip->prox_thres) {
329 hi = chip->prox_thres;
331 lo = chip->prox_thres - APDS_PROX_HYSTERESIS;
332 if (chip->prox_continuous_mode)
333 hi = chip->prox_thres;
338 ret = apds990x_write_word(chip, APDS990X_PILTL, lo);
339 ret |= apds990x_write_word(chip, APDS990X_PIHTL, hi);
343 /* Called always with mutex locked */
344 static int apds990x_refresh_athres(struct apds990x_chip *chip)
347 /* If the chip is not in use, don't try to access it */
348 if (pm_runtime_suspended(&chip->client->dev))
351 ret = apds990x_write_word(chip, APDS990X_AILTL,
352 apds990x_lux_to_threshold(chip, chip->lux_thres_lo));
353 ret |= apds990x_write_word(chip, APDS990X_AIHTL,
354 apds990x_lux_to_threshold(chip, chip->lux_thres_hi));
359 /* Called always with mutex locked */
360 static void apds990x_force_a_refresh(struct apds990x_chip *chip)
362 /* This will force ALS interrupt after the next measurement. */
363 apds990x_write_word(chip, APDS990X_AILTL, APDS_LUX_DEF_THRES_LO);
364 apds990x_write_word(chip, APDS990X_AIHTL, APDS_LUX_DEF_THRES_HI);
367 /* Called always with mutex locked */
368 static void apds990x_force_p_refresh(struct apds990x_chip *chip)
370 /* This will force proximity interrupt after the next measurement. */
371 apds990x_write_word(chip, APDS990X_PILTL, APDS_PROX_DEF_THRES - 1);
372 apds990x_write_word(chip, APDS990X_PIHTL, APDS_PROX_DEF_THRES);
375 /* Called always with mutex locked */
376 static int apds990x_calc_again(struct apds990x_chip *chip)
378 int curr_again = chip->again_meas;
379 int next_again = chip->again_meas;
382 /* Calculate suitable als gain */
383 if (chip->lux_clear == chip->a_max_result)
384 next_again -= 2; /* ALS saturated. Decrease gain by 2 steps */
385 else if (chip->lux_clear > chip->a_max_result / 2)
387 else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
388 next_again += 2; /* Too dark. Increase gain by 2 steps */
389 else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT)
392 /* Limit gain to available range */
395 else if (next_again > APDS990X_MAX_AGAIN)
396 next_again = APDS990X_MAX_AGAIN;
398 /* Let's check can we trust the measured result */
399 if (chip->lux_clear == chip->a_max_result)
400 /* Result can be totally garbage due to saturation */
402 else if (next_again != curr_again &&
403 chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
405 * Gain is changed and measurement result is very small.
406 * Result can be totally garbage due to underflow
410 chip->again_next = next_again;
411 apds990x_write_byte(chip, APDS990X_CONTROL,
412 (chip->pdrive << 6) |
413 (chip->pdiode << 4) |
415 (chip->again_next << 0));
418 * Error means bad result -> re-measurement is needed. The forced
419 * refresh uses fastest possible persistence setting to get result
420 * as soon as possible.
423 apds990x_force_a_refresh(chip);
425 apds990x_refresh_athres(chip);
430 /* Called always with mutex locked */
431 static int apds990x_get_lux(struct apds990x_chip *chip, int clear, int ir)
433 int iac, iac1, iac2; /* IR adjusted counts */
434 u32 lpc; /* Lux per count */
437 * iac1 = CF1 * CLEAR_CH - IRF1 * IR_CH
438 * iac2 = CF2 * CLEAR_CH - IRF2 * IR_CH
440 iac1 = (chip->cf.cf1 * clear - chip->cf.irf1 * ir) / APDS_PARAM_SCALE;
441 iac2 = (chip->cf.cf2 * clear - chip->cf.irf2 * ir) / APDS_PARAM_SCALE;
443 iac = max(iac1, iac2);
446 lpc = APDS990X_LUX_OUTPUT_SCALE * (chip->cf.df * chip->cf.ga) /
447 (u32)(again[chip->again_meas] * (u32)chip->atime);
449 return (iac * lpc) / APDS_PARAM_SCALE;
452 static int apds990x_ack_int(struct apds990x_chip *chip, u8 mode)
454 struct i2c_client *client = chip->client;
456 u8 reg = APDS990x_CMD | APDS990x_CMD_TYPE_SPE;
458 switch (mode & (APDS990X_ST_AINT | APDS990X_ST_PINT)) {
459 case APDS990X_ST_AINT:
460 reg |= APDS990X_INT_ACK_ALS;
462 case APDS990X_ST_PINT:
463 reg |= APDS990X_INT_ACK_PS;
466 reg |= APDS990X_INT_ACK_BOTH;
470 ret = i2c_smbus_read_byte_data(client, reg);
474 static irqreturn_t apds990x_irq(int irq, void *data)
476 struct apds990x_chip *chip = data;
479 apds990x_read_byte(chip, APDS990X_STATUS, &status);
480 apds990x_ack_int(chip, status);
482 mutex_lock(&chip->mutex);
483 if (!pm_runtime_suspended(&chip->client->dev)) {
484 if (status & APDS990X_ST_AINT) {
485 apds990x_read_word(chip, APDS990X_CDATAL,
487 apds990x_read_word(chip, APDS990X_IRDATAL,
489 /* Store used gain for calculations */
490 chip->again_meas = chip->again_next;
492 chip->lux_raw = apds990x_get_lux(chip,
496 if (apds990x_calc_again(chip) == 0) {
497 /* Result is valid */
498 chip->lux = chip->lux_raw;
499 chip->lux_wait_fresh_res = false;
500 wake_up(&chip->wait);
501 sysfs_notify(&chip->client->dev.kobj,
506 if ((status & APDS990X_ST_PINT) && chip->prox_en) {
509 apds990x_read_word(chip, APDS990X_CDATAL, &clr_ch);
511 * If ALS channel is saturated at min gain,
512 * proximity gives false posivite values.
515 if (chip->again_meas == 0 &&
516 clr_ch == chip->a_max_result)
519 apds990x_read_word(chip,
523 apds990x_refresh_pthres(chip, chip->prox_data);
524 if (chip->prox_data < chip->prox_thres)
526 else if (!chip->prox_continuous_mode)
527 chip->prox_data = APDS_PROX_RANGE;
528 sysfs_notify(&chip->client->dev.kobj,
532 mutex_unlock(&chip->mutex);
536 static int apds990x_configure(struct apds990x_chip *chip)
538 /* It is recommended to use disabled mode during these operations */
539 apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
541 /* conversion and wait times for different state machince states */
542 apds990x_write_byte(chip, APDS990X_PTIME, APDS990X_PTIME_DEFAULT);
543 apds990x_write_byte(chip, APDS990X_WTIME, APDS990X_WTIME_DEFAULT);
544 apds990x_set_atime(chip, APDS_LUX_AVERAGING_TIME);
546 apds990x_write_byte(chip, APDS990X_CONFIG, 0);
548 /* Persistence levels */
549 apds990x_write_byte(chip, APDS990X_PERS,
550 (chip->lux_persistence << APDS990X_APERS_SHIFT) |
551 (chip->prox_persistence << APDS990X_PPERS_SHIFT));
553 apds990x_write_byte(chip, APDS990X_PPCOUNT, chip->pdata->ppcount);
555 /* Start with relatively small gain */
556 chip->again_meas = 1;
557 chip->again_next = 1;
558 apds990x_write_byte(chip, APDS990X_CONTROL,
559 (chip->pdrive << 6) |
560 (chip->pdiode << 4) |
562 (chip->again_next << 0));
566 static int apds990x_detect(struct apds990x_chip *chip)
568 struct i2c_client *client = chip->client;
572 ret = apds990x_read_byte(chip, APDS990X_ID, &id);
574 dev_err(&client->dev, "ID read failed\n");
578 ret = apds990x_read_byte(chip, APDS990X_REV, &chip->revision);
580 dev_err(&client->dev, "REV read failed\n");
588 snprintf(chip->chipname, sizeof(chip->chipname), "APDS-990x");
598 static int apds990x_chip_on(struct apds990x_chip *chip)
600 int err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
605 usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
607 /* Refresh all configs in case of regulators were off */
609 apds990x_configure(chip);
610 apds990x_mode_on(chip);
615 static int apds990x_chip_off(struct apds990x_chip *chip)
617 apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
618 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
622 static ssize_t apds990x_lux_show(struct device *dev,
623 struct device_attribute *attr, char *buf)
625 struct apds990x_chip *chip = dev_get_drvdata(dev);
630 if (pm_runtime_suspended(dev))
633 timeout = wait_event_interruptible_timeout(chip->wait,
634 !chip->lux_wait_fresh_res,
635 msecs_to_jiffies(APDS_TIMEOUT));
639 mutex_lock(&chip->mutex);
640 result = (chip->lux * chip->lux_calib) / APDS_CALIB_SCALER;
641 if (result > (APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE))
642 result = APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE;
644 ret = sprintf(buf, "%d.%d\n",
645 result / APDS990X_LUX_OUTPUT_SCALE,
646 result % APDS990X_LUX_OUTPUT_SCALE);
647 mutex_unlock(&chip->mutex);
651 static DEVICE_ATTR(lux0_input, S_IRUGO, apds990x_lux_show, NULL);
653 static ssize_t apds990x_lux_range_show(struct device *dev,
654 struct device_attribute *attr, char *buf)
656 return sprintf(buf, "%u\n", APDS_RANGE);
659 static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, apds990x_lux_range_show, NULL);
661 static ssize_t apds990x_lux_calib_format_show(struct device *dev,
662 struct device_attribute *attr, char *buf)
664 return sprintf(buf, "%u\n", APDS_CALIB_SCALER);
667 static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
668 apds990x_lux_calib_format_show, NULL);
670 static ssize_t apds990x_lux_calib_show(struct device *dev,
671 struct device_attribute *attr, char *buf)
673 struct apds990x_chip *chip = dev_get_drvdata(dev);
675 return sprintf(buf, "%u\n", chip->lux_calib);
678 static ssize_t apds990x_lux_calib_store(struct device *dev,
679 struct device_attribute *attr,
680 const char *buf, size_t len)
682 struct apds990x_chip *chip = dev_get_drvdata(dev);
686 ret = kstrtoul(buf, 0, &value);
690 chip->lux_calib = value;
695 static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, apds990x_lux_calib_show,
696 apds990x_lux_calib_store);
698 static ssize_t apds990x_rate_avail(struct device *dev,
699 struct device_attribute *attr, char *buf)
704 for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
705 pos += sprintf(buf + pos, "%d ", arates_hz[i]);
706 sprintf(buf + pos - 1, "\n");
710 static ssize_t apds990x_rate_show(struct device *dev,
711 struct device_attribute *attr, char *buf)
713 struct apds990x_chip *chip = dev_get_drvdata(dev);
715 return sprintf(buf, "%d\n", chip->arate);
718 static int apds990x_set_arate(struct apds990x_chip *chip, int rate)
722 for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
723 if (rate >= arates_hz[i])
726 if (i == ARRAY_SIZE(arates_hz))
729 /* Pick up corresponding persistence value */
730 chip->lux_persistence = apersis[i];
731 chip->arate = arates_hz[i];
733 /* If the chip is not in use, don't try to access it */
734 if (pm_runtime_suspended(&chip->client->dev))
737 /* Persistence levels */
738 return apds990x_write_byte(chip, APDS990X_PERS,
739 (chip->lux_persistence << APDS990X_APERS_SHIFT) |
740 (chip->prox_persistence << APDS990X_PPERS_SHIFT));
743 static ssize_t apds990x_rate_store(struct device *dev,
744 struct device_attribute *attr,
745 const char *buf, size_t len)
747 struct apds990x_chip *chip = dev_get_drvdata(dev);
751 ret = kstrtoul(buf, 0, &value);
755 mutex_lock(&chip->mutex);
756 ret = apds990x_set_arate(chip, value);
757 mutex_unlock(&chip->mutex);
764 static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, apds990x_rate_avail, NULL);
766 static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, apds990x_rate_show,
767 apds990x_rate_store);
769 static ssize_t apds990x_prox_show(struct device *dev,
770 struct device_attribute *attr, char *buf)
773 struct apds990x_chip *chip = dev_get_drvdata(dev);
775 if (pm_runtime_suspended(dev) || !chip->prox_en)
778 mutex_lock(&chip->mutex);
779 ret = sprintf(buf, "%d\n", chip->prox_data);
780 mutex_unlock(&chip->mutex);
784 static DEVICE_ATTR(prox0_raw, S_IRUGO, apds990x_prox_show, NULL);
786 static ssize_t apds990x_prox_range_show(struct device *dev,
787 struct device_attribute *attr, char *buf)
789 return sprintf(buf, "%u\n", APDS_PROX_RANGE);
792 static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, apds990x_prox_range_show, NULL);
794 static ssize_t apds990x_prox_enable_show(struct device *dev,
795 struct device_attribute *attr, char *buf)
797 struct apds990x_chip *chip = dev_get_drvdata(dev);
799 return sprintf(buf, "%d\n", chip->prox_en);
802 static ssize_t apds990x_prox_enable_store(struct device *dev,
803 struct device_attribute *attr,
804 const char *buf, size_t len)
806 struct apds990x_chip *chip = dev_get_drvdata(dev);
810 ret = kstrtoul(buf, 0, &value);
814 mutex_lock(&chip->mutex);
821 else if (chip->prox_en > 0)
824 if (!pm_runtime_suspended(dev))
825 apds990x_mode_on(chip);
826 mutex_unlock(&chip->mutex);
830 static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, apds990x_prox_enable_show,
831 apds990x_prox_enable_store);
833 static const char *reporting_modes[] = {"trigger", "periodic"};
835 static ssize_t apds990x_prox_reporting_mode_show(struct device *dev,
836 struct device_attribute *attr, char *buf)
838 struct apds990x_chip *chip = dev_get_drvdata(dev);
840 return sprintf(buf, "%s\n",
841 reporting_modes[!!chip->prox_continuous_mode]);
844 static ssize_t apds990x_prox_reporting_mode_store(struct device *dev,
845 struct device_attribute *attr,
846 const char *buf, size_t len)
848 struct apds990x_chip *chip = dev_get_drvdata(dev);
851 ret = sysfs_match_string(reporting_modes, buf);
855 chip->prox_continuous_mode = ret;
859 static DEVICE_ATTR(prox0_reporting_mode, S_IRUGO | S_IWUSR,
860 apds990x_prox_reporting_mode_show,
861 apds990x_prox_reporting_mode_store);
863 static ssize_t apds990x_prox_reporting_avail_show(struct device *dev,
864 struct device_attribute *attr, char *buf)
866 return sprintf(buf, "%s %s\n", reporting_modes[0], reporting_modes[1]);
869 static DEVICE_ATTR(prox0_reporting_mode_avail, S_IRUGO | S_IWUSR,
870 apds990x_prox_reporting_avail_show, NULL);
873 static ssize_t apds990x_lux_thresh_above_show(struct device *dev,
874 struct device_attribute *attr, char *buf)
876 struct apds990x_chip *chip = dev_get_drvdata(dev);
878 return sprintf(buf, "%d\n", chip->lux_thres_hi);
881 static ssize_t apds990x_lux_thresh_below_show(struct device *dev,
882 struct device_attribute *attr, char *buf)
884 struct apds990x_chip *chip = dev_get_drvdata(dev);
886 return sprintf(buf, "%d\n", chip->lux_thres_lo);
889 static ssize_t apds990x_set_lux_thresh(struct apds990x_chip *chip, u32 *target,
892 unsigned long thresh;
895 ret = kstrtoul(buf, 0, &thresh);
899 if (thresh > APDS_RANGE)
902 mutex_lock(&chip->mutex);
905 * Don't update values in HW if we are still waiting for
906 * first interrupt to come after device handle open call.
908 if (!chip->lux_wait_fresh_res)
909 apds990x_refresh_athres(chip);
910 mutex_unlock(&chip->mutex);
915 static ssize_t apds990x_lux_thresh_above_store(struct device *dev,
916 struct device_attribute *attr,
917 const char *buf, size_t len)
919 struct apds990x_chip *chip = dev_get_drvdata(dev);
920 int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_hi, buf);
927 static ssize_t apds990x_lux_thresh_below_store(struct device *dev,
928 struct device_attribute *attr,
929 const char *buf, size_t len)
931 struct apds990x_chip *chip = dev_get_drvdata(dev);
932 int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_lo, buf);
939 static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
940 apds990x_lux_thresh_above_show,
941 apds990x_lux_thresh_above_store);
943 static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
944 apds990x_lux_thresh_below_show,
945 apds990x_lux_thresh_below_store);
947 static ssize_t apds990x_prox_threshold_show(struct device *dev,
948 struct device_attribute *attr, char *buf)
950 struct apds990x_chip *chip = dev_get_drvdata(dev);
952 return sprintf(buf, "%d\n", chip->prox_thres);
955 static ssize_t apds990x_prox_threshold_store(struct device *dev,
956 struct device_attribute *attr,
957 const char *buf, size_t len)
959 struct apds990x_chip *chip = dev_get_drvdata(dev);
963 ret = kstrtoul(buf, 0, &value);
967 if ((value > APDS_RANGE) || (value == 0) ||
968 (value < APDS_PROX_HYSTERESIS))
971 mutex_lock(&chip->mutex);
972 chip->prox_thres = value;
974 apds990x_force_p_refresh(chip);
975 mutex_unlock(&chip->mutex);
979 static DEVICE_ATTR(prox0_thresh_above_value, S_IRUGO | S_IWUSR,
980 apds990x_prox_threshold_show,
981 apds990x_prox_threshold_store);
983 static ssize_t apds990x_power_state_show(struct device *dev,
984 struct device_attribute *attr, char *buf)
986 return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
990 static ssize_t apds990x_power_state_store(struct device *dev,
991 struct device_attribute *attr,
992 const char *buf, size_t len)
994 struct apds990x_chip *chip = dev_get_drvdata(dev);
998 ret = kstrtoul(buf, 0, &value);
1003 pm_runtime_get_sync(dev);
1004 mutex_lock(&chip->mutex);
1005 chip->lux_wait_fresh_res = true;
1006 apds990x_force_a_refresh(chip);
1007 apds990x_force_p_refresh(chip);
1008 mutex_unlock(&chip->mutex);
1010 if (!pm_runtime_suspended(dev))
1011 pm_runtime_put(dev);
1016 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
1017 apds990x_power_state_show,
1018 apds990x_power_state_store);
1020 static ssize_t apds990x_chip_id_show(struct device *dev,
1021 struct device_attribute *attr, char *buf)
1023 struct apds990x_chip *chip = dev_get_drvdata(dev);
1025 return sprintf(buf, "%s %d\n", chip->chipname, chip->revision);
1028 static DEVICE_ATTR(chip_id, S_IRUGO, apds990x_chip_id_show, NULL);
1030 static struct attribute *sysfs_attrs_ctrl[] = {
1031 &dev_attr_lux0_calibscale.attr,
1032 &dev_attr_lux0_calibscale_default.attr,
1033 &dev_attr_lux0_input.attr,
1034 &dev_attr_lux0_sensor_range.attr,
1035 &dev_attr_lux0_rate.attr,
1036 &dev_attr_lux0_rate_avail.attr,
1037 &dev_attr_lux0_thresh_above_value.attr,
1038 &dev_attr_lux0_thresh_below_value.attr,
1039 &dev_attr_prox0_raw_en.attr,
1040 &dev_attr_prox0_raw.attr,
1041 &dev_attr_prox0_sensor_range.attr,
1042 &dev_attr_prox0_thresh_above_value.attr,
1043 &dev_attr_prox0_reporting_mode.attr,
1044 &dev_attr_prox0_reporting_mode_avail.attr,
1045 &dev_attr_chip_id.attr,
1046 &dev_attr_power_state.attr,
1050 static const struct attribute_group apds990x_attribute_group[] = {
1051 {.attrs = sysfs_attrs_ctrl },
1054 static int apds990x_probe(struct i2c_client *client,
1055 const struct i2c_device_id *id)
1057 struct apds990x_chip *chip;
1060 chip = kzalloc(sizeof *chip, GFP_KERNEL);
1064 i2c_set_clientdata(client, chip);
1065 chip->client = client;
1067 init_waitqueue_head(&chip->wait);
1068 mutex_init(&chip->mutex);
1069 chip->pdata = client->dev.platform_data;
1071 if (chip->pdata == NULL) {
1072 dev_err(&client->dev, "platform data is mandatory\n");
1077 if (chip->pdata->cf.ga == 0) {
1078 /* set uncovered sensor default parameters */
1079 chip->cf.ga = 1966; /* 0.48 * APDS_PARAM_SCALE */
1080 chip->cf.cf1 = 4096; /* 1.00 * APDS_PARAM_SCALE */
1081 chip->cf.irf1 = 9134; /* 2.23 * APDS_PARAM_SCALE */
1082 chip->cf.cf2 = 2867; /* 0.70 * APDS_PARAM_SCALE */
1083 chip->cf.irf2 = 5816; /* 1.42 * APDS_PARAM_SCALE */
1086 chip->cf = chip->pdata->cf;
1089 /* precalculate inverse chip factors for threshold control */
1091 (chip->cf.irf1 - chip->cf.irf2) * APDS_PARAM_SCALE /
1092 (chip->cf.cf1 - chip->cf.cf2);
1093 chip->rcf.cf1 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
1095 chip->rcf.irf1 = chip->cf.irf1 * APDS_PARAM_SCALE /
1097 chip->rcf.cf2 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
1099 chip->rcf.irf2 = chip->cf.irf2 * APDS_PARAM_SCALE /
1102 /* Set something to start with */
1103 chip->lux_thres_hi = APDS_LUX_DEF_THRES_HI;
1104 chip->lux_thres_lo = APDS_LUX_DEF_THRES_LO;
1105 chip->lux_calib = APDS_LUX_NEUTRAL_CALIB_VALUE;
1107 chip->prox_thres = APDS_PROX_DEF_THRES;
1108 chip->pdrive = chip->pdata->pdrive;
1109 chip->pdiode = APDS_PDIODE_IR;
1110 chip->pgain = APDS_PGAIN_1X;
1111 chip->prox_calib = APDS_PROX_NEUTRAL_CALIB_VALUE;
1112 chip->prox_persistence = APDS_DEFAULT_PROX_PERS;
1113 chip->prox_continuous_mode = false;
1115 chip->regs[0].supply = reg_vcc;
1116 chip->regs[1].supply = reg_vled;
1118 err = regulator_bulk_get(&client->dev,
1119 ARRAY_SIZE(chip->regs), chip->regs);
1121 dev_err(&client->dev, "Cannot get regulators\n");
1125 err = regulator_bulk_enable(ARRAY_SIZE(chip->regs), chip->regs);
1127 dev_err(&client->dev, "Cannot enable regulators\n");
1131 usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
1133 err = apds990x_detect(chip);
1135 dev_err(&client->dev, "APDS990X not found\n");
1139 pm_runtime_set_active(&client->dev);
1141 apds990x_configure(chip);
1142 apds990x_set_arate(chip, APDS_LUX_DEFAULT_RATE);
1143 apds990x_mode_on(chip);
1145 pm_runtime_enable(&client->dev);
1147 if (chip->pdata->setup_resources) {
1148 err = chip->pdata->setup_resources();
1155 err = sysfs_create_group(&chip->client->dev.kobj,
1156 apds990x_attribute_group);
1158 dev_err(&chip->client->dev, "Sysfs registration failed\n");
1162 err = request_threaded_irq(client->irq, NULL,
1164 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW |
1168 dev_err(&client->dev, "could not get IRQ %d\n",
1174 sysfs_remove_group(&chip->client->dev.kobj,
1175 &apds990x_attribute_group[0]);
1177 if (chip->pdata && chip->pdata->release_resources)
1178 chip->pdata->release_resources();
1180 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1182 regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
1188 static void apds990x_remove(struct i2c_client *client)
1190 struct apds990x_chip *chip = i2c_get_clientdata(client);
1192 free_irq(client->irq, chip);
1193 sysfs_remove_group(&chip->client->dev.kobj,
1194 apds990x_attribute_group);
1196 if (chip->pdata && chip->pdata->release_resources)
1197 chip->pdata->release_resources();
1199 if (!pm_runtime_suspended(&client->dev))
1200 apds990x_chip_off(chip);
1202 pm_runtime_disable(&client->dev);
1203 pm_runtime_set_suspended(&client->dev);
1205 regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
1210 #ifdef CONFIG_PM_SLEEP
1211 static int apds990x_suspend(struct device *dev)
1213 struct i2c_client *client = to_i2c_client(dev);
1214 struct apds990x_chip *chip = i2c_get_clientdata(client);
1216 apds990x_chip_off(chip);
1220 static int apds990x_resume(struct device *dev)
1222 struct i2c_client *client = to_i2c_client(dev);
1223 struct apds990x_chip *chip = i2c_get_clientdata(client);
1226 * If we were enabled at suspend time, it is expected
1227 * everything works nice and smoothly. Chip_on is enough
1229 apds990x_chip_on(chip);
1236 static int apds990x_runtime_suspend(struct device *dev)
1238 struct i2c_client *client = to_i2c_client(dev);
1239 struct apds990x_chip *chip = i2c_get_clientdata(client);
1241 apds990x_chip_off(chip);
1245 static int apds990x_runtime_resume(struct device *dev)
1247 struct i2c_client *client = to_i2c_client(dev);
1248 struct apds990x_chip *chip = i2c_get_clientdata(client);
1250 apds990x_chip_on(chip);
1256 static const struct i2c_device_id apds990x_id[] = {
1261 MODULE_DEVICE_TABLE(i2c, apds990x_id);
1263 static const struct dev_pm_ops apds990x_pm_ops = {
1264 SET_SYSTEM_SLEEP_PM_OPS(apds990x_suspend, apds990x_resume)
1265 SET_RUNTIME_PM_OPS(apds990x_runtime_suspend,
1266 apds990x_runtime_resume,
1270 static struct i2c_driver apds990x_driver = {
1273 .pm = &apds990x_pm_ops,
1275 .probe = apds990x_probe,
1276 .remove = apds990x_remove,
1277 .id_table = apds990x_id,
1280 module_i2c_driver(apds990x_driver);
1282 MODULE_DESCRIPTION("APDS990X combined ALS and proximity sensor");
1283 MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1284 MODULE_LICENSE("GPL v2");