2 * This file is part of the APDS990x sensor driver.
3 * Chip is combined proximity and ambient light sensor.
5 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/i2c.h>
28 #include <linux/interrupt.h>
29 #include <linux/mutex.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/delay.h>
33 #include <linux/wait.h>
34 #include <linux/slab.h>
35 #include <linux/platform_data/apds990x.h>
38 #define APDS990X_ENABLE 0x00 /* Enable of states and interrupts */
39 #define APDS990X_ATIME 0x01 /* ALS ADC time */
40 #define APDS990X_PTIME 0x02 /* Proximity ADC time */
41 #define APDS990X_WTIME 0x03 /* Wait time */
42 #define APDS990X_AILTL 0x04 /* ALS interrupt low threshold low byte */
43 #define APDS990X_AILTH 0x05 /* ALS interrupt low threshold hi byte */
44 #define APDS990X_AIHTL 0x06 /* ALS interrupt hi threshold low byte */
45 #define APDS990X_AIHTH 0x07 /* ALS interrupt hi threshold hi byte */
46 #define APDS990X_PILTL 0x08 /* Proximity interrupt low threshold low byte */
47 #define APDS990X_PILTH 0x09 /* Proximity interrupt low threshold hi byte */
48 #define APDS990X_PIHTL 0x0a /* Proximity interrupt hi threshold low byte */
49 #define APDS990X_PIHTH 0x0b /* Proximity interrupt hi threshold hi byte */
50 #define APDS990X_PERS 0x0c /* Interrupt persistence filters */
51 #define APDS990X_CONFIG 0x0d /* Configuration */
52 #define APDS990X_PPCOUNT 0x0e /* Proximity pulse count */
53 #define APDS990X_CONTROL 0x0f /* Gain control register */
54 #define APDS990X_REV 0x11 /* Revision Number */
55 #define APDS990X_ID 0x12 /* Device ID */
56 #define APDS990X_STATUS 0x13 /* Device status */
57 #define APDS990X_CDATAL 0x14 /* Clear ADC low data register */
58 #define APDS990X_CDATAH 0x15 /* Clear ADC high data register */
59 #define APDS990X_IRDATAL 0x16 /* IR ADC low data register */
60 #define APDS990X_IRDATAH 0x17 /* IR ADC high data register */
61 #define APDS990X_PDATAL 0x18 /* Proximity ADC low data register */
62 #define APDS990X_PDATAH 0x19 /* Proximity ADC high data register */
65 #define APDS990X_MAX_AGAIN 3
68 #define APDS990X_EN_PIEN (0x1 << 5)
69 #define APDS990X_EN_AIEN (0x1 << 4)
70 #define APDS990X_EN_WEN (0x1 << 3)
71 #define APDS990X_EN_PEN (0x1 << 2)
72 #define APDS990X_EN_AEN (0x1 << 1)
73 #define APDS990X_EN_PON (0x1 << 0)
74 #define APDS990X_EN_DISABLE_ALL 0
77 #define APDS990X_ST_PINT (0x1 << 5)
78 #define APDS990X_ST_AINT (0x1 << 4)
80 /* I2C access types */
81 #define APDS990x_CMD_TYPE_MASK (0x03 << 5)
82 #define APDS990x_CMD_TYPE_RB (0x00 << 5) /* Repeated byte */
83 #define APDS990x_CMD_TYPE_INC (0x01 << 5) /* Auto increment */
84 #define APDS990x_CMD_TYPE_SPE (0x03 << 5) /* Special function */
86 #define APDS990x_ADDR_SHIFT 0
87 #define APDS990x_CMD 0x80
89 /* Interrupt ack commands */
90 #define APDS990X_INT_ACK_ALS 0x6
91 #define APDS990X_INT_ACK_PS 0x5
92 #define APDS990X_INT_ACK_BOTH 0x7
95 #define APDS990X_PTIME_DEFAULT 0xff /* Recommended conversion time 2.7ms*/
98 #define APDS990X_WTIME_DEFAULT 0xee /* ~50ms wait time */
100 #define APDS990X_TIME_TO_ADC 1024 /* One timetick as ADC count value */
103 #define APDS990X_APERS_SHIFT 0
104 #define APDS990X_PPERS_SHIFT 4
107 #define APDS990X_ID_0 0x0
108 #define APDS990X_ID_4 0x4
109 #define APDS990X_ID_29 0x29
111 /* pgain and pdiode settings */
112 #define APDS_PGAIN_1X 0x0
113 #define APDS_PDIODE_IR 0x2
115 #define APDS990X_LUX_OUTPUT_SCALE 10
117 /* Reverse chip factors for threshold calculation */
118 struct reverse_factors
{
126 struct apds990x_chip
{
127 struct apds990x_platform_data
*pdata
;
128 struct i2c_client
*client
;
129 struct mutex mutex
; /* avoid parallel access */
130 struct regulator_bulk_data regs
[2];
131 wait_queue_head_t wait
;
134 bool prox_continuous_mode
;
135 bool lux_wait_fresh_res
;
137 /* Chip parameters */
138 struct apds990x_chip_factors cf
;
139 struct reverse_factors rcf
;
140 u16 atime
; /* als integration time */
141 u16 arate
; /* als reporting rate */
142 u16 a_max_result
; /* Max possible ADC value with current atime */
143 u8 again_meas
; /* Gain used in last measurement */
144 u8 again_next
; /* Next calculated gain */
167 #define APDS_CALIB_SCALER 8192
168 #define APDS_LUX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
169 #define APDS_PROX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
171 #define APDS_PROX_DEF_THRES 600
172 #define APDS_PROX_HYSTERESIS 50
173 #define APDS_LUX_DEF_THRES_HI 101
174 #define APDS_LUX_DEF_THRES_LO 100
175 #define APDS_DEFAULT_PROX_PERS 1
177 #define APDS_TIMEOUT 2000
178 #define APDS_STARTUP_DELAY 25000 /* us */
179 #define APDS_RANGE 65535
180 #define APDS_PROX_RANGE 1023
181 #define APDS_LUX_GAIN_LO_LIMIT 100
182 #define APDS_LUX_GAIN_LO_LIMIT_STRICT 25
184 #define TIMESTEP 87 /* 2.7ms is about 87 / 32 */
185 #define TIME_STEP_SCALER 32
187 #define APDS_LUX_AVERAGING_TIME 50 /* tolerates 50/60Hz ripple */
188 #define APDS_LUX_DEFAULT_RATE 200
190 static const u8 again
[] = {1, 8, 16, 120}; /* ALS gain steps */
192 /* Following two tables must match i.e 10Hz rate means 1 as persistence value */
193 static const u16 arates_hz
[] = {10, 5, 2, 1};
194 static const u8 apersis
[] = {1, 2, 4, 5};
197 static const char reg_vcc
[] = "Vdd";
198 static const char reg_vled
[] = "Vled";
200 static int apds990x_read_byte(struct apds990x_chip
*chip
, u8 reg
, u8
*data
)
202 struct i2c_client
*client
= chip
->client
;
205 reg
&= ~APDS990x_CMD_TYPE_MASK
;
206 reg
|= APDS990x_CMD
| APDS990x_CMD_TYPE_RB
;
208 ret
= i2c_smbus_read_byte_data(client
, reg
);
213 static int apds990x_read_word(struct apds990x_chip
*chip
, u8 reg
, u16
*data
)
215 struct i2c_client
*client
= chip
->client
;
218 reg
&= ~APDS990x_CMD_TYPE_MASK
;
219 reg
|= APDS990x_CMD
| APDS990x_CMD_TYPE_INC
;
221 ret
= i2c_smbus_read_word_data(client
, reg
);
226 static int apds990x_write_byte(struct apds990x_chip
*chip
, u8 reg
, u8 data
)
228 struct i2c_client
*client
= chip
->client
;
231 reg
&= ~APDS990x_CMD_TYPE_MASK
;
232 reg
|= APDS990x_CMD
| APDS990x_CMD_TYPE_RB
;
234 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
238 static int apds990x_write_word(struct apds990x_chip
*chip
, u8 reg
, u16 data
)
240 struct i2c_client
*client
= chip
->client
;
243 reg
&= ~APDS990x_CMD_TYPE_MASK
;
244 reg
|= APDS990x_CMD
| APDS990x_CMD_TYPE_INC
;
246 ret
= i2c_smbus_write_word_data(client
, reg
, data
);
250 static int apds990x_mode_on(struct apds990x_chip
*chip
)
252 /* ALS is mandatory, proximity optional */
253 u8 reg
= APDS990X_EN_AIEN
| APDS990X_EN_PON
| APDS990X_EN_AEN
|
257 reg
|= APDS990X_EN_PIEN
| APDS990X_EN_PEN
;
259 return apds990x_write_byte(chip
, APDS990X_ENABLE
, reg
);
262 static u16
apds990x_lux_to_threshold(struct apds990x_chip
*chip
, u32 lux
)
270 else if (lux
== APDS_RANGE
)
274 * Reported LUX value is a combination of the IR and CLEAR channel
275 * values. However, interrupt threshold is only for clear channel.
276 * This function approximates needed HW threshold value for a given
277 * LUX value in the current lightning type.
278 * IR level compared to visible light varies heavily depending on the
279 * source of the light
281 * Calculate threshold value for the next measurement period.
282 * Math: threshold = lux * cpl where
283 * cpl = atime * again / (glass_attenuation * device_factor)
286 * First remove calibration. Division by four is to avoid overflow
288 lux
= lux
* (APDS_CALIB_SCALER
/ 4) / (chip
->lux_calib
/ 4);
290 /* Multiplication by 64 is to increase accuracy */
291 cpl
= ((u32
)chip
->atime
* (u32
)again
[chip
->again_next
] *
292 APDS_PARAM_SCALE
* 64) / (chip
->cf
.ga
* chip
->cf
.df
);
294 thres
= lux
* cpl
/ 64;
296 * Convert IR light from the latest result to match with
297 * new gain step. This helps to adapt with the current
300 ir
= (u32
)chip
->lux_ir
* (u32
)again
[chip
->again_next
] /
301 (u32
)again
[chip
->again_meas
];
304 * Compensate count with IR light impact
305 * IAC1 > IAC2 (see apds990x_get_lux for formulas)
307 if (chip
->lux_clear
* APDS_PARAM_SCALE
>=
308 chip
->rcf
.afactor
* chip
->lux_ir
)
309 thres
= (chip
->rcf
.cf1
* thres
+ chip
->rcf
.irf1
* ir
) /
312 thres
= (chip
->rcf
.cf2
* thres
+ chip
->rcf
.irf2
* ir
) /
315 if (thres
>= chip
->a_max_result
)
316 thres
= chip
->a_max_result
- 1;
320 static inline int apds990x_set_atime(struct apds990x_chip
*chip
, u32 time_ms
)
324 chip
->atime
= time_ms
;
325 /* Formula is specified in the data sheet */
326 reg_value
= 256 - ((time_ms
* TIME_STEP_SCALER
) / TIMESTEP
);
327 /* Calculate max ADC value for given integration time */
328 chip
->a_max_result
= (u16
)(256 - reg_value
) * APDS990X_TIME_TO_ADC
;
329 return apds990x_write_byte(chip
, APDS990X_ATIME
, reg_value
);
332 /* Called always with mutex locked */
333 static int apds990x_refresh_pthres(struct apds990x_chip
*chip
, int data
)
337 /* If the chip is not in use, don't try to access it */
338 if (pm_runtime_suspended(&chip
->client
->dev
))
341 if (data
< chip
->prox_thres
) {
343 hi
= chip
->prox_thres
;
345 lo
= chip
->prox_thres
- APDS_PROX_HYSTERESIS
;
346 if (chip
->prox_continuous_mode
)
347 hi
= chip
->prox_thres
;
352 ret
= apds990x_write_word(chip
, APDS990X_PILTL
, lo
);
353 ret
|= apds990x_write_word(chip
, APDS990X_PIHTL
, hi
);
357 /* Called always with mutex locked */
358 static int apds990x_refresh_athres(struct apds990x_chip
*chip
)
361 /* If the chip is not in use, don't try to access it */
362 if (pm_runtime_suspended(&chip
->client
->dev
))
365 ret
= apds990x_write_word(chip
, APDS990X_AILTL
,
366 apds990x_lux_to_threshold(chip
, chip
->lux_thres_lo
));
367 ret
|= apds990x_write_word(chip
, APDS990X_AIHTL
,
368 apds990x_lux_to_threshold(chip
, chip
->lux_thres_hi
));
373 /* Called always with mutex locked */
374 static void apds990x_force_a_refresh(struct apds990x_chip
*chip
)
376 /* This will force ALS interrupt after the next measurement. */
377 apds990x_write_word(chip
, APDS990X_AILTL
, APDS_LUX_DEF_THRES_LO
);
378 apds990x_write_word(chip
, APDS990X_AIHTL
, APDS_LUX_DEF_THRES_HI
);
381 /* Called always with mutex locked */
382 static void apds990x_force_p_refresh(struct apds990x_chip
*chip
)
384 /* This will force proximity interrupt after the next measurement. */
385 apds990x_write_word(chip
, APDS990X_PILTL
, APDS_PROX_DEF_THRES
- 1);
386 apds990x_write_word(chip
, APDS990X_PIHTL
, APDS_PROX_DEF_THRES
);
389 /* Called always with mutex locked */
390 static int apds990x_calc_again(struct apds990x_chip
*chip
)
392 int curr_again
= chip
->again_meas
;
393 int next_again
= chip
->again_meas
;
396 /* Calculate suitable als gain */
397 if (chip
->lux_clear
== chip
->a_max_result
)
398 next_again
-= 2; /* ALS saturated. Decrease gain by 2 steps */
399 else if (chip
->lux_clear
> chip
->a_max_result
/ 2)
401 else if (chip
->lux_clear
< APDS_LUX_GAIN_LO_LIMIT_STRICT
)
402 next_again
+= 2; /* Too dark. Increase gain by 2 steps */
403 else if (chip
->lux_clear
< APDS_LUX_GAIN_LO_LIMIT
)
406 /* Limit gain to available range */
409 else if (next_again
> APDS990X_MAX_AGAIN
)
410 next_again
= APDS990X_MAX_AGAIN
;
412 /* Let's check can we trust the measured result */
413 if (chip
->lux_clear
== chip
->a_max_result
)
414 /* Result can be totally garbage due to saturation */
416 else if (next_again
!= curr_again
&&
417 chip
->lux_clear
< APDS_LUX_GAIN_LO_LIMIT_STRICT
)
419 * Gain is changed and measurement result is very small.
420 * Result can be totally garbage due to underflow
424 chip
->again_next
= next_again
;
425 apds990x_write_byte(chip
, APDS990X_CONTROL
,
426 (chip
->pdrive
<< 6) |
427 (chip
->pdiode
<< 4) |
429 (chip
->again_next
<< 0));
432 * Error means bad result -> re-measurement is needed. The forced
433 * refresh uses fastest possible persistence setting to get result
434 * as soon as possible.
437 apds990x_force_a_refresh(chip
);
439 apds990x_refresh_athres(chip
);
444 /* Called always with mutex locked */
445 static int apds990x_get_lux(struct apds990x_chip
*chip
, int clear
, int ir
)
447 int iac
, iac1
, iac2
; /* IR adjusted counts */
448 u32 lpc
; /* Lux per count */
451 * iac1 = CF1 * CLEAR_CH - IRF1 * IR_CH
452 * iac2 = CF2 * CLEAR_CH - IRF2 * IR_CH
454 iac1
= (chip
->cf
.cf1
* clear
- chip
->cf
.irf1
* ir
) / APDS_PARAM_SCALE
;
455 iac2
= (chip
->cf
.cf2
* clear
- chip
->cf
.irf2
* ir
) / APDS_PARAM_SCALE
;
457 iac
= max(iac1
, iac2
);
460 lpc
= APDS990X_LUX_OUTPUT_SCALE
* (chip
->cf
.df
* chip
->cf
.ga
) /
461 (u32
)(again
[chip
->again_meas
] * (u32
)chip
->atime
);
463 return (iac
* lpc
) / APDS_PARAM_SCALE
;
466 static int apds990x_ack_int(struct apds990x_chip
*chip
, u8 mode
)
468 struct i2c_client
*client
= chip
->client
;
470 u8 reg
= APDS990x_CMD
| APDS990x_CMD_TYPE_SPE
;
472 switch (mode
& (APDS990X_ST_AINT
| APDS990X_ST_PINT
)) {
473 case APDS990X_ST_AINT
:
474 reg
|= APDS990X_INT_ACK_ALS
;
476 case APDS990X_ST_PINT
:
477 reg
|= APDS990X_INT_ACK_PS
;
480 reg
|= APDS990X_INT_ACK_BOTH
;
484 ret
= i2c_smbus_read_byte_data(client
, reg
);
488 static irqreturn_t
apds990x_irq(int irq
, void *data
)
490 struct apds990x_chip
*chip
= data
;
493 apds990x_read_byte(chip
, APDS990X_STATUS
, &status
);
494 apds990x_ack_int(chip
, status
);
496 mutex_lock(&chip
->mutex
);
497 if (!pm_runtime_suspended(&chip
->client
->dev
)) {
498 if (status
& APDS990X_ST_AINT
) {
499 apds990x_read_word(chip
, APDS990X_CDATAL
,
501 apds990x_read_word(chip
, APDS990X_IRDATAL
,
503 /* Store used gain for calculations */
504 chip
->again_meas
= chip
->again_next
;
506 chip
->lux_raw
= apds990x_get_lux(chip
,
510 if (apds990x_calc_again(chip
) == 0) {
511 /* Result is valid */
512 chip
->lux
= chip
->lux_raw
;
513 chip
->lux_wait_fresh_res
= false;
514 wake_up(&chip
->wait
);
515 sysfs_notify(&chip
->client
->dev
.kobj
,
520 if ((status
& APDS990X_ST_PINT
) && chip
->prox_en
) {
523 apds990x_read_word(chip
, APDS990X_CDATAL
, &clr_ch
);
525 * If ALS channel is saturated at min gain,
526 * proximity gives false posivite values.
529 if (chip
->again_meas
== 0 &&
530 clr_ch
== chip
->a_max_result
)
533 apds990x_read_word(chip
,
537 apds990x_refresh_pthres(chip
, chip
->prox_data
);
538 if (chip
->prox_data
< chip
->prox_thres
)
540 else if (!chip
->prox_continuous_mode
)
541 chip
->prox_data
= APDS_PROX_RANGE
;
542 sysfs_notify(&chip
->client
->dev
.kobj
,
546 mutex_unlock(&chip
->mutex
);
550 static int apds990x_configure(struct apds990x_chip
*chip
)
552 /* It is recommended to use disabled mode during these operations */
553 apds990x_write_byte(chip
, APDS990X_ENABLE
, APDS990X_EN_DISABLE_ALL
);
555 /* conversion and wait times for different state machince states */
556 apds990x_write_byte(chip
, APDS990X_PTIME
, APDS990X_PTIME_DEFAULT
);
557 apds990x_write_byte(chip
, APDS990X_WTIME
, APDS990X_WTIME_DEFAULT
);
558 apds990x_set_atime(chip
, APDS_LUX_AVERAGING_TIME
);
560 apds990x_write_byte(chip
, APDS990X_CONFIG
, 0);
562 /* Persistence levels */
563 apds990x_write_byte(chip
, APDS990X_PERS
,
564 (chip
->lux_persistence
<< APDS990X_APERS_SHIFT
) |
565 (chip
->prox_persistence
<< APDS990X_PPERS_SHIFT
));
567 apds990x_write_byte(chip
, APDS990X_PPCOUNT
, chip
->pdata
->ppcount
);
569 /* Start with relatively small gain */
570 chip
->again_meas
= 1;
571 chip
->again_next
= 1;
572 apds990x_write_byte(chip
, APDS990X_CONTROL
,
573 (chip
->pdrive
<< 6) |
574 (chip
->pdiode
<< 4) |
576 (chip
->again_next
<< 0));
580 static int apds990x_detect(struct apds990x_chip
*chip
)
582 struct i2c_client
*client
= chip
->client
;
586 ret
= apds990x_read_byte(chip
, APDS990X_ID
, &id
);
588 dev_err(&client
->dev
, "ID read failed\n");
592 ret
= apds990x_read_byte(chip
, APDS990X_REV
, &chip
->revision
);
594 dev_err(&client
->dev
, "REV read failed\n");
602 snprintf(chip
->chipname
, sizeof(chip
->chipname
), "APDS-990x");
612 static int apds990x_chip_on(struct apds990x_chip
*chip
)
614 int err
= regulator_bulk_enable(ARRAY_SIZE(chip
->regs
),
619 usleep_range(APDS_STARTUP_DELAY
, 2 * APDS_STARTUP_DELAY
);
621 /* Refresh all configs in case of regulators were off */
623 apds990x_configure(chip
);
624 apds990x_mode_on(chip
);
629 static int apds990x_chip_off(struct apds990x_chip
*chip
)
631 apds990x_write_byte(chip
, APDS990X_ENABLE
, APDS990X_EN_DISABLE_ALL
);
632 regulator_bulk_disable(ARRAY_SIZE(chip
->regs
), chip
->regs
);
636 static ssize_t
apds990x_lux_show(struct device
*dev
,
637 struct device_attribute
*attr
, char *buf
)
639 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
644 if (pm_runtime_suspended(dev
))
647 timeout
= wait_event_interruptible_timeout(chip
->wait
,
648 !chip
->lux_wait_fresh_res
,
649 msecs_to_jiffies(APDS_TIMEOUT
));
653 mutex_lock(&chip
->mutex
);
654 result
= (chip
->lux
* chip
->lux_calib
) / APDS_CALIB_SCALER
;
655 if (result
> (APDS_RANGE
* APDS990X_LUX_OUTPUT_SCALE
))
656 result
= APDS_RANGE
* APDS990X_LUX_OUTPUT_SCALE
;
658 ret
= sprintf(buf
, "%d.%d\n",
659 result
/ APDS990X_LUX_OUTPUT_SCALE
,
660 result
% APDS990X_LUX_OUTPUT_SCALE
);
661 mutex_unlock(&chip
->mutex
);
665 static DEVICE_ATTR(lux0_input
, S_IRUGO
, apds990x_lux_show
, NULL
);
667 static ssize_t
apds990x_lux_range_show(struct device
*dev
,
668 struct device_attribute
*attr
, char *buf
)
670 return sprintf(buf
, "%u\n", APDS_RANGE
);
673 static DEVICE_ATTR(lux0_sensor_range
, S_IRUGO
, apds990x_lux_range_show
, NULL
);
675 static ssize_t
apds990x_lux_calib_format_show(struct device
*dev
,
676 struct device_attribute
*attr
, char *buf
)
678 return sprintf(buf
, "%u\n", APDS_CALIB_SCALER
);
681 static DEVICE_ATTR(lux0_calibscale_default
, S_IRUGO
,
682 apds990x_lux_calib_format_show
, NULL
);
684 static ssize_t
apds990x_lux_calib_show(struct device
*dev
,
685 struct device_attribute
*attr
, char *buf
)
687 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
689 return sprintf(buf
, "%u\n", chip
->lux_calib
);
692 static ssize_t
apds990x_lux_calib_store(struct device
*dev
,
693 struct device_attribute
*attr
,
694 const char *buf
, size_t len
)
696 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
700 ret
= kstrtoul(buf
, 0, &value
);
704 chip
->lux_calib
= value
;
709 static DEVICE_ATTR(lux0_calibscale
, S_IRUGO
| S_IWUSR
, apds990x_lux_calib_show
,
710 apds990x_lux_calib_store
);
712 static ssize_t
apds990x_rate_avail(struct device
*dev
,
713 struct device_attribute
*attr
, char *buf
)
718 for (i
= 0; i
< ARRAY_SIZE(arates_hz
); i
++)
719 pos
+= sprintf(buf
+ pos
, "%d ", arates_hz
[i
]);
720 sprintf(buf
+ pos
- 1, "\n");
724 static ssize_t
apds990x_rate_show(struct device
*dev
,
725 struct device_attribute
*attr
, char *buf
)
727 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
729 return sprintf(buf
, "%d\n", chip
->arate
);
732 static int apds990x_set_arate(struct apds990x_chip
*chip
, int rate
)
736 for (i
= 0; i
< ARRAY_SIZE(arates_hz
); i
++)
737 if (rate
>= arates_hz
[i
])
740 if (i
== ARRAY_SIZE(arates_hz
))
743 /* Pick up corresponding persistence value */
744 chip
->lux_persistence
= apersis
[i
];
745 chip
->arate
= arates_hz
[i
];
747 /* If the chip is not in use, don't try to access it */
748 if (pm_runtime_suspended(&chip
->client
->dev
))
751 /* Persistence levels */
752 return apds990x_write_byte(chip
, APDS990X_PERS
,
753 (chip
->lux_persistence
<< APDS990X_APERS_SHIFT
) |
754 (chip
->prox_persistence
<< APDS990X_PPERS_SHIFT
));
757 static ssize_t
apds990x_rate_store(struct device
*dev
,
758 struct device_attribute
*attr
,
759 const char *buf
, size_t len
)
761 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
765 ret
= kstrtoul(buf
, 0, &value
);
769 mutex_lock(&chip
->mutex
);
770 ret
= apds990x_set_arate(chip
, value
);
771 mutex_unlock(&chip
->mutex
);
778 static DEVICE_ATTR(lux0_rate_avail
, S_IRUGO
, apds990x_rate_avail
, NULL
);
780 static DEVICE_ATTR(lux0_rate
, S_IRUGO
| S_IWUSR
, apds990x_rate_show
,
781 apds990x_rate_store
);
783 static ssize_t
apds990x_prox_show(struct device
*dev
,
784 struct device_attribute
*attr
, char *buf
)
787 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
789 if (pm_runtime_suspended(dev
) || !chip
->prox_en
)
792 mutex_lock(&chip
->mutex
);
793 ret
= sprintf(buf
, "%d\n", chip
->prox_data
);
794 mutex_unlock(&chip
->mutex
);
798 static DEVICE_ATTR(prox0_raw
, S_IRUGO
, apds990x_prox_show
, NULL
);
800 static ssize_t
apds990x_prox_range_show(struct device
*dev
,
801 struct device_attribute
*attr
, char *buf
)
803 return sprintf(buf
, "%u\n", APDS_PROX_RANGE
);
806 static DEVICE_ATTR(prox0_sensor_range
, S_IRUGO
, apds990x_prox_range_show
, NULL
);
808 static ssize_t
apds990x_prox_enable_show(struct device
*dev
,
809 struct device_attribute
*attr
, char *buf
)
811 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
813 return sprintf(buf
, "%d\n", chip
->prox_en
);
816 static ssize_t
apds990x_prox_enable_store(struct device
*dev
,
817 struct device_attribute
*attr
,
818 const char *buf
, size_t len
)
820 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
824 ret
= kstrtoul(buf
, 0, &value
);
828 mutex_lock(&chip
->mutex
);
835 else if (chip
->prox_en
> 0)
838 if (!pm_runtime_suspended(dev
))
839 apds990x_mode_on(chip
);
840 mutex_unlock(&chip
->mutex
);
844 static DEVICE_ATTR(prox0_raw_en
, S_IRUGO
| S_IWUSR
, apds990x_prox_enable_show
,
845 apds990x_prox_enable_store
);
847 static const char *reporting_modes
[] = {"trigger", "periodic"};
849 static ssize_t
apds990x_prox_reporting_mode_show(struct device
*dev
,
850 struct device_attribute
*attr
, char *buf
)
852 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
854 return sprintf(buf
, "%s\n",
855 reporting_modes
[!!chip
->prox_continuous_mode
]);
858 static ssize_t
apds990x_prox_reporting_mode_store(struct device
*dev
,
859 struct device_attribute
*attr
,
860 const char *buf
, size_t len
)
862 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
865 ret
= sysfs_match_string(reporting_modes
, buf
);
869 chip
->prox_continuous_mode
= ret
;
873 static DEVICE_ATTR(prox0_reporting_mode
, S_IRUGO
| S_IWUSR
,
874 apds990x_prox_reporting_mode_show
,
875 apds990x_prox_reporting_mode_store
);
877 static ssize_t
apds990x_prox_reporting_avail_show(struct device
*dev
,
878 struct device_attribute
*attr
, char *buf
)
880 return sprintf(buf
, "%s %s\n", reporting_modes
[0], reporting_modes
[1]);
883 static DEVICE_ATTR(prox0_reporting_mode_avail
, S_IRUGO
| S_IWUSR
,
884 apds990x_prox_reporting_avail_show
, NULL
);
887 static ssize_t
apds990x_lux_thresh_above_show(struct device
*dev
,
888 struct device_attribute
*attr
, char *buf
)
890 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
892 return sprintf(buf
, "%d\n", chip
->lux_thres_hi
);
895 static ssize_t
apds990x_lux_thresh_below_show(struct device
*dev
,
896 struct device_attribute
*attr
, char *buf
)
898 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
900 return sprintf(buf
, "%d\n", chip
->lux_thres_lo
);
903 static ssize_t
apds990x_set_lux_thresh(struct apds990x_chip
*chip
, u32
*target
,
906 unsigned long thresh
;
909 ret
= kstrtoul(buf
, 0, &thresh
);
913 if (thresh
> APDS_RANGE
)
916 mutex_lock(&chip
->mutex
);
919 * Don't update values in HW if we are still waiting for
920 * first interrupt to come after device handle open call.
922 if (!chip
->lux_wait_fresh_res
)
923 apds990x_refresh_athres(chip
);
924 mutex_unlock(&chip
->mutex
);
929 static ssize_t
apds990x_lux_thresh_above_store(struct device
*dev
,
930 struct device_attribute
*attr
,
931 const char *buf
, size_t len
)
933 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
934 int ret
= apds990x_set_lux_thresh(chip
, &chip
->lux_thres_hi
, buf
);
941 static ssize_t
apds990x_lux_thresh_below_store(struct device
*dev
,
942 struct device_attribute
*attr
,
943 const char *buf
, size_t len
)
945 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
946 int ret
= apds990x_set_lux_thresh(chip
, &chip
->lux_thres_lo
, buf
);
953 static DEVICE_ATTR(lux0_thresh_above_value
, S_IRUGO
| S_IWUSR
,
954 apds990x_lux_thresh_above_show
,
955 apds990x_lux_thresh_above_store
);
957 static DEVICE_ATTR(lux0_thresh_below_value
, S_IRUGO
| S_IWUSR
,
958 apds990x_lux_thresh_below_show
,
959 apds990x_lux_thresh_below_store
);
961 static ssize_t
apds990x_prox_threshold_show(struct device
*dev
,
962 struct device_attribute
*attr
, char *buf
)
964 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
966 return sprintf(buf
, "%d\n", chip
->prox_thres
);
969 static ssize_t
apds990x_prox_threshold_store(struct device
*dev
,
970 struct device_attribute
*attr
,
971 const char *buf
, size_t len
)
973 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
977 ret
= kstrtoul(buf
, 0, &value
);
981 if ((value
> APDS_RANGE
) || (value
== 0) ||
982 (value
< APDS_PROX_HYSTERESIS
))
985 mutex_lock(&chip
->mutex
);
986 chip
->prox_thres
= value
;
988 apds990x_force_p_refresh(chip
);
989 mutex_unlock(&chip
->mutex
);
993 static DEVICE_ATTR(prox0_thresh_above_value
, S_IRUGO
| S_IWUSR
,
994 apds990x_prox_threshold_show
,
995 apds990x_prox_threshold_store
);
997 static ssize_t
apds990x_power_state_show(struct device
*dev
,
998 struct device_attribute
*attr
, char *buf
)
1000 return sprintf(buf
, "%d\n", !pm_runtime_suspended(dev
));
1004 static ssize_t
apds990x_power_state_store(struct device
*dev
,
1005 struct device_attribute
*attr
,
1006 const char *buf
, size_t len
)
1008 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
1009 unsigned long value
;
1012 ret
= kstrtoul(buf
, 0, &value
);
1017 pm_runtime_get_sync(dev
);
1018 mutex_lock(&chip
->mutex
);
1019 chip
->lux_wait_fresh_res
= true;
1020 apds990x_force_a_refresh(chip
);
1021 apds990x_force_p_refresh(chip
);
1022 mutex_unlock(&chip
->mutex
);
1024 if (!pm_runtime_suspended(dev
))
1025 pm_runtime_put(dev
);
1030 static DEVICE_ATTR(power_state
, S_IRUGO
| S_IWUSR
,
1031 apds990x_power_state_show
,
1032 apds990x_power_state_store
);
1034 static ssize_t
apds990x_chip_id_show(struct device
*dev
,
1035 struct device_attribute
*attr
, char *buf
)
1037 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
1039 return sprintf(buf
, "%s %d\n", chip
->chipname
, chip
->revision
);
1042 static DEVICE_ATTR(chip_id
, S_IRUGO
, apds990x_chip_id_show
, NULL
);
1044 static struct attribute
*sysfs_attrs_ctrl
[] = {
1045 &dev_attr_lux0_calibscale
.attr
,
1046 &dev_attr_lux0_calibscale_default
.attr
,
1047 &dev_attr_lux0_input
.attr
,
1048 &dev_attr_lux0_sensor_range
.attr
,
1049 &dev_attr_lux0_rate
.attr
,
1050 &dev_attr_lux0_rate_avail
.attr
,
1051 &dev_attr_lux0_thresh_above_value
.attr
,
1052 &dev_attr_lux0_thresh_below_value
.attr
,
1053 &dev_attr_prox0_raw_en
.attr
,
1054 &dev_attr_prox0_raw
.attr
,
1055 &dev_attr_prox0_sensor_range
.attr
,
1056 &dev_attr_prox0_thresh_above_value
.attr
,
1057 &dev_attr_prox0_reporting_mode
.attr
,
1058 &dev_attr_prox0_reporting_mode_avail
.attr
,
1059 &dev_attr_chip_id
.attr
,
1060 &dev_attr_power_state
.attr
,
1064 static const struct attribute_group apds990x_attribute_group
[] = {
1065 {.attrs
= sysfs_attrs_ctrl
},
1068 static int apds990x_probe(struct i2c_client
*client
,
1069 const struct i2c_device_id
*id
)
1071 struct apds990x_chip
*chip
;
1074 chip
= kzalloc(sizeof *chip
, GFP_KERNEL
);
1078 i2c_set_clientdata(client
, chip
);
1079 chip
->client
= client
;
1081 init_waitqueue_head(&chip
->wait
);
1082 mutex_init(&chip
->mutex
);
1083 chip
->pdata
= client
->dev
.platform_data
;
1085 if (chip
->pdata
== NULL
) {
1086 dev_err(&client
->dev
, "platform data is mandatory\n");
1091 if (chip
->pdata
->cf
.ga
== 0) {
1092 /* set uncovered sensor default parameters */
1093 chip
->cf
.ga
= 1966; /* 0.48 * APDS_PARAM_SCALE */
1094 chip
->cf
.cf1
= 4096; /* 1.00 * APDS_PARAM_SCALE */
1095 chip
->cf
.irf1
= 9134; /* 2.23 * APDS_PARAM_SCALE */
1096 chip
->cf
.cf2
= 2867; /* 0.70 * APDS_PARAM_SCALE */
1097 chip
->cf
.irf2
= 5816; /* 1.42 * APDS_PARAM_SCALE */
1100 chip
->cf
= chip
->pdata
->cf
;
1103 /* precalculate inverse chip factors for threshold control */
1105 (chip
->cf
.irf1
- chip
->cf
.irf2
) * APDS_PARAM_SCALE
/
1106 (chip
->cf
.cf1
- chip
->cf
.cf2
);
1107 chip
->rcf
.cf1
= APDS_PARAM_SCALE
* APDS_PARAM_SCALE
/
1109 chip
->rcf
.irf1
= chip
->cf
.irf1
* APDS_PARAM_SCALE
/
1111 chip
->rcf
.cf2
= APDS_PARAM_SCALE
* APDS_PARAM_SCALE
/
1113 chip
->rcf
.irf2
= chip
->cf
.irf2
* APDS_PARAM_SCALE
/
1116 /* Set something to start with */
1117 chip
->lux_thres_hi
= APDS_LUX_DEF_THRES_HI
;
1118 chip
->lux_thres_lo
= APDS_LUX_DEF_THRES_LO
;
1119 chip
->lux_calib
= APDS_LUX_NEUTRAL_CALIB_VALUE
;
1121 chip
->prox_thres
= APDS_PROX_DEF_THRES
;
1122 chip
->pdrive
= chip
->pdata
->pdrive
;
1123 chip
->pdiode
= APDS_PDIODE_IR
;
1124 chip
->pgain
= APDS_PGAIN_1X
;
1125 chip
->prox_calib
= APDS_PROX_NEUTRAL_CALIB_VALUE
;
1126 chip
->prox_persistence
= APDS_DEFAULT_PROX_PERS
;
1127 chip
->prox_continuous_mode
= false;
1129 chip
->regs
[0].supply
= reg_vcc
;
1130 chip
->regs
[1].supply
= reg_vled
;
1132 err
= regulator_bulk_get(&client
->dev
,
1133 ARRAY_SIZE(chip
->regs
), chip
->regs
);
1135 dev_err(&client
->dev
, "Cannot get regulators\n");
1139 err
= regulator_bulk_enable(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1141 dev_err(&client
->dev
, "Cannot enable regulators\n");
1145 usleep_range(APDS_STARTUP_DELAY
, 2 * APDS_STARTUP_DELAY
);
1147 err
= apds990x_detect(chip
);
1149 dev_err(&client
->dev
, "APDS990X not found\n");
1153 pm_runtime_set_active(&client
->dev
);
1155 apds990x_configure(chip
);
1156 apds990x_set_arate(chip
, APDS_LUX_DEFAULT_RATE
);
1157 apds990x_mode_on(chip
);
1159 pm_runtime_enable(&client
->dev
);
1161 if (chip
->pdata
->setup_resources
) {
1162 err
= chip
->pdata
->setup_resources();
1169 err
= sysfs_create_group(&chip
->client
->dev
.kobj
,
1170 apds990x_attribute_group
);
1172 dev_err(&chip
->client
->dev
, "Sysfs registration failed\n");
1176 err
= request_threaded_irq(client
->irq
, NULL
,
1178 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_LOW
|
1182 dev_err(&client
->dev
, "could not get IRQ %d\n",
1188 sysfs_remove_group(&chip
->client
->dev
.kobj
,
1189 &apds990x_attribute_group
[0]);
1191 if (chip
->pdata
&& chip
->pdata
->release_resources
)
1192 chip
->pdata
->release_resources();
1194 regulator_bulk_disable(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1196 regulator_bulk_free(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1202 static int apds990x_remove(struct i2c_client
*client
)
1204 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1206 free_irq(client
->irq
, chip
);
1207 sysfs_remove_group(&chip
->client
->dev
.kobj
,
1208 apds990x_attribute_group
);
1210 if (chip
->pdata
&& chip
->pdata
->release_resources
)
1211 chip
->pdata
->release_resources();
1213 if (!pm_runtime_suspended(&client
->dev
))
1214 apds990x_chip_off(chip
);
1216 pm_runtime_disable(&client
->dev
);
1217 pm_runtime_set_suspended(&client
->dev
);
1219 regulator_bulk_free(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1225 #ifdef CONFIG_PM_SLEEP
1226 static int apds990x_suspend(struct device
*dev
)
1228 struct i2c_client
*client
= to_i2c_client(dev
);
1229 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1231 apds990x_chip_off(chip
);
1235 static int apds990x_resume(struct device
*dev
)
1237 struct i2c_client
*client
= to_i2c_client(dev
);
1238 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1241 * If we were enabled at suspend time, it is expected
1242 * everything works nice and smoothly. Chip_on is enough
1244 apds990x_chip_on(chip
);
1251 static int apds990x_runtime_suspend(struct device
*dev
)
1253 struct i2c_client
*client
= to_i2c_client(dev
);
1254 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1256 apds990x_chip_off(chip
);
1260 static int apds990x_runtime_resume(struct device
*dev
)
1262 struct i2c_client
*client
= to_i2c_client(dev
);
1263 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1265 apds990x_chip_on(chip
);
1271 static const struct i2c_device_id apds990x_id
[] = {
1276 MODULE_DEVICE_TABLE(i2c
, apds990x_id
);
1278 static const struct dev_pm_ops apds990x_pm_ops
= {
1279 SET_SYSTEM_SLEEP_PM_OPS(apds990x_suspend
, apds990x_resume
)
1280 SET_RUNTIME_PM_OPS(apds990x_runtime_suspend
,
1281 apds990x_runtime_resume
,
1285 static struct i2c_driver apds990x_driver
= {
1288 .pm
= &apds990x_pm_ops
,
1290 .probe
= apds990x_probe
,
1291 .remove
= apds990x_remove
,
1292 .id_table
= apds990x_id
,
1295 module_i2c_driver(apds990x_driver
);
1297 MODULE_DESCRIPTION("APDS990X combined ALS and proximity sensor");
1298 MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1299 MODULE_LICENSE("GPL v2");