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 */
191 static const u8 ir_currents
[] = {100, 50, 25, 12}; /* IRled currents in mA */
193 /* Following two tables must match i.e 10Hz rate means 1 as persistence value */
194 static const u16 arates_hz
[] = {10, 5, 2, 1};
195 static const u8 apersis
[] = {1, 2, 4, 5};
198 static const char reg_vcc
[] = "Vdd";
199 static const char reg_vled
[] = "Vled";
201 static int apds990x_read_byte(struct apds990x_chip
*chip
, u8 reg
, u8
*data
)
203 struct i2c_client
*client
= chip
->client
;
206 reg
&= ~APDS990x_CMD_TYPE_MASK
;
207 reg
|= APDS990x_CMD
| APDS990x_CMD_TYPE_RB
;
209 ret
= i2c_smbus_read_byte_data(client
, reg
);
214 static int apds990x_read_word(struct apds990x_chip
*chip
, u8 reg
, u16
*data
)
216 struct i2c_client
*client
= chip
->client
;
219 reg
&= ~APDS990x_CMD_TYPE_MASK
;
220 reg
|= APDS990x_CMD
| APDS990x_CMD_TYPE_INC
;
222 ret
= i2c_smbus_read_word_data(client
, reg
);
227 static int apds990x_write_byte(struct apds990x_chip
*chip
, u8 reg
, u8 data
)
229 struct i2c_client
*client
= chip
->client
;
232 reg
&= ~APDS990x_CMD_TYPE_MASK
;
233 reg
|= APDS990x_CMD
| APDS990x_CMD_TYPE_RB
;
235 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
239 static int apds990x_write_word(struct apds990x_chip
*chip
, u8 reg
, u16 data
)
241 struct i2c_client
*client
= chip
->client
;
244 reg
&= ~APDS990x_CMD_TYPE_MASK
;
245 reg
|= APDS990x_CMD
| APDS990x_CMD_TYPE_INC
;
247 ret
= i2c_smbus_write_word_data(client
, reg
, data
);
251 static int apds990x_mode_on(struct apds990x_chip
*chip
)
253 /* ALS is mandatory, proximity optional */
254 u8 reg
= APDS990X_EN_AIEN
| APDS990X_EN_PON
| APDS990X_EN_AEN
|
258 reg
|= APDS990X_EN_PIEN
| APDS990X_EN_PEN
;
260 return apds990x_write_byte(chip
, APDS990X_ENABLE
, reg
);
263 static u16
apds990x_lux_to_threshold(struct apds990x_chip
*chip
, u32 lux
)
271 else if (lux
== APDS_RANGE
)
275 * Reported LUX value is a combination of the IR and CLEAR channel
276 * values. However, interrupt threshold is only for clear channel.
277 * This function approximates needed HW threshold value for a given
278 * LUX value in the current lightning type.
279 * IR level compared to visible light varies heavily depending on the
280 * source of the light
282 * Calculate threshold value for the next measurement period.
283 * Math: threshold = lux * cpl where
284 * cpl = atime * again / (glass_attenuation * device_factor)
287 * First remove calibration. Division by four is to avoid overflow
289 lux
= lux
* (APDS_CALIB_SCALER
/ 4) / (chip
->lux_calib
/ 4);
291 /* Multiplication by 64 is to increase accuracy */
292 cpl
= ((u32
)chip
->atime
* (u32
)again
[chip
->again_next
] *
293 APDS_PARAM_SCALE
* 64) / (chip
->cf
.ga
* chip
->cf
.df
);
295 thres
= lux
* cpl
/ 64;
297 * Convert IR light from the latest result to match with
298 * new gain step. This helps to adapt with the current
301 ir
= (u32
)chip
->lux_ir
* (u32
)again
[chip
->again_next
] /
302 (u32
)again
[chip
->again_meas
];
305 * Compensate count with IR light impact
306 * IAC1 > IAC2 (see apds990x_get_lux for formulas)
308 if (chip
->lux_clear
* APDS_PARAM_SCALE
>=
309 chip
->rcf
.afactor
* chip
->lux_ir
)
310 thres
= (chip
->rcf
.cf1
* thres
+ chip
->rcf
.irf1
* ir
) /
313 thres
= (chip
->rcf
.cf2
* thres
+ chip
->rcf
.irf2
* ir
) /
316 if (thres
>= chip
->a_max_result
)
317 thres
= chip
->a_max_result
- 1;
321 static inline int apds990x_set_atime(struct apds990x_chip
*chip
, u32 time_ms
)
325 chip
->atime
= time_ms
;
326 /* Formula is specified in the data sheet */
327 reg_value
= 256 - ((time_ms
* TIME_STEP_SCALER
) / TIMESTEP
);
328 /* Calculate max ADC value for given integration time */
329 chip
->a_max_result
= (u16
)(256 - reg_value
) * APDS990X_TIME_TO_ADC
;
330 return apds990x_write_byte(chip
, APDS990X_ATIME
, reg_value
);
333 /* Called always with mutex locked */
334 static int apds990x_refresh_pthres(struct apds990x_chip
*chip
, int data
)
338 /* If the chip is not in use, don't try to access it */
339 if (pm_runtime_suspended(&chip
->client
->dev
))
342 if (data
< chip
->prox_thres
) {
344 hi
= chip
->prox_thres
;
346 lo
= chip
->prox_thres
- APDS_PROX_HYSTERESIS
;
347 if (chip
->prox_continuous_mode
)
348 hi
= chip
->prox_thres
;
353 ret
= apds990x_write_word(chip
, APDS990X_PILTL
, lo
);
354 ret
|= apds990x_write_word(chip
, APDS990X_PIHTL
, hi
);
358 /* Called always with mutex locked */
359 static int apds990x_refresh_athres(struct apds990x_chip
*chip
)
362 /* If the chip is not in use, don't try to access it */
363 if (pm_runtime_suspended(&chip
->client
->dev
))
366 ret
= apds990x_write_word(chip
, APDS990X_AILTL
,
367 apds990x_lux_to_threshold(chip
, chip
->lux_thres_lo
));
368 ret
|= apds990x_write_word(chip
, APDS990X_AIHTL
,
369 apds990x_lux_to_threshold(chip
, chip
->lux_thres_hi
));
374 /* Called always with mutex locked */
375 static void apds990x_force_a_refresh(struct apds990x_chip
*chip
)
377 /* This will force ALS interrupt after the next measurement. */
378 apds990x_write_word(chip
, APDS990X_AILTL
, APDS_LUX_DEF_THRES_LO
);
379 apds990x_write_word(chip
, APDS990X_AIHTL
, APDS_LUX_DEF_THRES_HI
);
382 /* Called always with mutex locked */
383 static void apds990x_force_p_refresh(struct apds990x_chip
*chip
)
385 /* This will force proximity interrupt after the next measurement. */
386 apds990x_write_word(chip
, APDS990X_PILTL
, APDS_PROX_DEF_THRES
- 1);
387 apds990x_write_word(chip
, APDS990X_PIHTL
, APDS_PROX_DEF_THRES
);
390 /* Called always with mutex locked */
391 static int apds990x_calc_again(struct apds990x_chip
*chip
)
393 int curr_again
= chip
->again_meas
;
394 int next_again
= chip
->again_meas
;
397 /* Calculate suitable als gain */
398 if (chip
->lux_clear
== chip
->a_max_result
)
399 next_again
-= 2; /* ALS saturated. Decrease gain by 2 steps */
400 else if (chip
->lux_clear
> chip
->a_max_result
/ 2)
402 else if (chip
->lux_clear
< APDS_LUX_GAIN_LO_LIMIT_STRICT
)
403 next_again
+= 2; /* Too dark. Increase gain by 2 steps */
404 else if (chip
->lux_clear
< APDS_LUX_GAIN_LO_LIMIT
)
407 /* Limit gain to available range */
410 else if (next_again
> APDS990X_MAX_AGAIN
)
411 next_again
= APDS990X_MAX_AGAIN
;
413 /* Let's check can we trust the measured result */
414 if (chip
->lux_clear
== chip
->a_max_result
)
415 /* Result can be totally garbage due to saturation */
417 else if (next_again
!= curr_again
&&
418 chip
->lux_clear
< APDS_LUX_GAIN_LO_LIMIT_STRICT
)
420 * Gain is changed and measurement result is very small.
421 * Result can be totally garbage due to underflow
425 chip
->again_next
= next_again
;
426 apds990x_write_byte(chip
, APDS990X_CONTROL
,
427 (chip
->pdrive
<< 6) |
428 (chip
->pdiode
<< 4) |
430 (chip
->again_next
<< 0));
433 * Error means bad result -> re-measurement is needed. The forced
434 * refresh uses fastest possible persistence setting to get result
435 * as soon as possible.
438 apds990x_force_a_refresh(chip
);
440 apds990x_refresh_athres(chip
);
445 /* Called always with mutex locked */
446 static int apds990x_get_lux(struct apds990x_chip
*chip
, int clear
, int ir
)
448 int iac
, iac1
, iac2
; /* IR adjusted counts */
449 u32 lpc
; /* Lux per count */
452 * iac1 = CF1 * CLEAR_CH - IRF1 * IR_CH
453 * iac2 = CF2 * CLEAR_CH - IRF2 * IR_CH
455 iac1
= (chip
->cf
.cf1
* clear
- chip
->cf
.irf1
* ir
) / APDS_PARAM_SCALE
;
456 iac2
= (chip
->cf
.cf2
* clear
- chip
->cf
.irf2
* ir
) / APDS_PARAM_SCALE
;
458 iac
= max(iac1
, iac2
);
461 lpc
= APDS990X_LUX_OUTPUT_SCALE
* (chip
->cf
.df
* chip
->cf
.ga
) /
462 (u32
)(again
[chip
->again_meas
] * (u32
)chip
->atime
);
464 return (iac
* lpc
) / APDS_PARAM_SCALE
;
467 static int apds990x_ack_int(struct apds990x_chip
*chip
, u8 mode
)
469 struct i2c_client
*client
= chip
->client
;
471 u8 reg
= APDS990x_CMD
| APDS990x_CMD_TYPE_SPE
;
473 switch (mode
& (APDS990X_ST_AINT
| APDS990X_ST_PINT
)) {
474 case APDS990X_ST_AINT
:
475 reg
|= APDS990X_INT_ACK_ALS
;
477 case APDS990X_ST_PINT
:
478 reg
|= APDS990X_INT_ACK_PS
;
481 reg
|= APDS990X_INT_ACK_BOTH
;
485 ret
= i2c_smbus_read_byte_data(client
, reg
);
489 static irqreturn_t
apds990x_irq(int irq
, void *data
)
491 struct apds990x_chip
*chip
= data
;
494 apds990x_read_byte(chip
, APDS990X_STATUS
, &status
);
495 apds990x_ack_int(chip
, status
);
497 mutex_lock(&chip
->mutex
);
498 if (!pm_runtime_suspended(&chip
->client
->dev
)) {
499 if (status
& APDS990X_ST_AINT
) {
500 apds990x_read_word(chip
, APDS990X_CDATAL
,
502 apds990x_read_word(chip
, APDS990X_IRDATAL
,
504 /* Store used gain for calculations */
505 chip
->again_meas
= chip
->again_next
;
507 chip
->lux_raw
= apds990x_get_lux(chip
,
511 if (apds990x_calc_again(chip
) == 0) {
512 /* Result is valid */
513 chip
->lux
= chip
->lux_raw
;
514 chip
->lux_wait_fresh_res
= false;
515 wake_up(&chip
->wait
);
516 sysfs_notify(&chip
->client
->dev
.kobj
,
521 if ((status
& APDS990X_ST_PINT
) && chip
->prox_en
) {
524 apds990x_read_word(chip
, APDS990X_CDATAL
, &clr_ch
);
526 * If ALS channel is saturated at min gain,
527 * proximity gives false posivite values.
530 if (chip
->again_meas
== 0 &&
531 clr_ch
== chip
->a_max_result
)
534 apds990x_read_word(chip
,
538 apds990x_refresh_pthres(chip
, chip
->prox_data
);
539 if (chip
->prox_data
< chip
->prox_thres
)
541 else if (!chip
->prox_continuous_mode
)
542 chip
->prox_data
= APDS_PROX_RANGE
;
543 sysfs_notify(&chip
->client
->dev
.kobj
,
547 mutex_unlock(&chip
->mutex
);
551 static int apds990x_configure(struct apds990x_chip
*chip
)
553 /* It is recommended to use disabled mode during these operations */
554 apds990x_write_byte(chip
, APDS990X_ENABLE
, APDS990X_EN_DISABLE_ALL
);
556 /* conversion and wait times for different state machince states */
557 apds990x_write_byte(chip
, APDS990X_PTIME
, APDS990X_PTIME_DEFAULT
);
558 apds990x_write_byte(chip
, APDS990X_WTIME
, APDS990X_WTIME_DEFAULT
);
559 apds990x_set_atime(chip
, APDS_LUX_AVERAGING_TIME
);
561 apds990x_write_byte(chip
, APDS990X_CONFIG
, 0);
563 /* Persistence levels */
564 apds990x_write_byte(chip
, APDS990X_PERS
,
565 (chip
->lux_persistence
<< APDS990X_APERS_SHIFT
) |
566 (chip
->prox_persistence
<< APDS990X_PPERS_SHIFT
));
568 apds990x_write_byte(chip
, APDS990X_PPCOUNT
, chip
->pdata
->ppcount
);
570 /* Start with relatively small gain */
571 chip
->again_meas
= 1;
572 chip
->again_next
= 1;
573 apds990x_write_byte(chip
, APDS990X_CONTROL
,
574 (chip
->pdrive
<< 6) |
575 (chip
->pdiode
<< 4) |
577 (chip
->again_next
<< 0));
581 static int apds990x_detect(struct apds990x_chip
*chip
)
583 struct i2c_client
*client
= chip
->client
;
587 ret
= apds990x_read_byte(chip
, APDS990X_ID
, &id
);
589 dev_err(&client
->dev
, "ID read failed\n");
593 ret
= apds990x_read_byte(chip
, APDS990X_REV
, &chip
->revision
);
595 dev_err(&client
->dev
, "REV read failed\n");
603 snprintf(chip
->chipname
, sizeof(chip
->chipname
), "APDS-990x");
613 static int apds990x_chip_on(struct apds990x_chip
*chip
)
615 int err
= regulator_bulk_enable(ARRAY_SIZE(chip
->regs
),
620 usleep_range(APDS_STARTUP_DELAY
, 2 * APDS_STARTUP_DELAY
);
622 /* Refresh all configs in case of regulators were off */
624 apds990x_configure(chip
);
625 apds990x_mode_on(chip
);
630 static int apds990x_chip_off(struct apds990x_chip
*chip
)
632 apds990x_write_byte(chip
, APDS990X_ENABLE
, APDS990X_EN_DISABLE_ALL
);
633 regulator_bulk_disable(ARRAY_SIZE(chip
->regs
), chip
->regs
);
637 static ssize_t
apds990x_lux_show(struct device
*dev
,
638 struct device_attribute
*attr
, char *buf
)
640 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
645 if (pm_runtime_suspended(dev
))
648 timeout
= wait_event_interruptible_timeout(chip
->wait
,
649 !chip
->lux_wait_fresh_res
,
650 msecs_to_jiffies(APDS_TIMEOUT
));
654 mutex_lock(&chip
->mutex
);
655 result
= (chip
->lux
* chip
->lux_calib
) / APDS_CALIB_SCALER
;
656 if (result
> (APDS_RANGE
* APDS990X_LUX_OUTPUT_SCALE
))
657 result
= APDS_RANGE
* APDS990X_LUX_OUTPUT_SCALE
;
659 ret
= sprintf(buf
, "%d.%d\n",
660 result
/ APDS990X_LUX_OUTPUT_SCALE
,
661 result
% APDS990X_LUX_OUTPUT_SCALE
);
662 mutex_unlock(&chip
->mutex
);
666 static DEVICE_ATTR(lux0_input
, S_IRUGO
, apds990x_lux_show
, NULL
);
668 static ssize_t
apds990x_lux_range_show(struct device
*dev
,
669 struct device_attribute
*attr
, char *buf
)
671 return sprintf(buf
, "%u\n", APDS_RANGE
);
674 static DEVICE_ATTR(lux0_sensor_range
, S_IRUGO
, apds990x_lux_range_show
, NULL
);
676 static ssize_t
apds990x_lux_calib_format_show(struct device
*dev
,
677 struct device_attribute
*attr
, char *buf
)
679 return sprintf(buf
, "%u\n", APDS_CALIB_SCALER
);
682 static DEVICE_ATTR(lux0_calibscale_default
, S_IRUGO
,
683 apds990x_lux_calib_format_show
, NULL
);
685 static ssize_t
apds990x_lux_calib_show(struct device
*dev
,
686 struct device_attribute
*attr
, char *buf
)
688 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
690 return sprintf(buf
, "%u\n", chip
->lux_calib
);
693 static ssize_t
apds990x_lux_calib_store(struct device
*dev
,
694 struct device_attribute
*attr
,
695 const char *buf
, size_t len
)
697 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
701 ret
= kstrtoul(buf
, 0, &value
);
705 chip
->lux_calib
= value
;
710 static DEVICE_ATTR(lux0_calibscale
, S_IRUGO
| S_IWUSR
, apds990x_lux_calib_show
,
711 apds990x_lux_calib_store
);
713 static ssize_t
apds990x_rate_avail(struct device
*dev
,
714 struct device_attribute
*attr
, char *buf
)
719 for (i
= 0; i
< ARRAY_SIZE(arates_hz
); i
++)
720 pos
+= sprintf(buf
+ pos
, "%d ", arates_hz
[i
]);
721 sprintf(buf
+ pos
- 1, "\n");
725 static ssize_t
apds990x_rate_show(struct device
*dev
,
726 struct device_attribute
*attr
, char *buf
)
728 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
730 return sprintf(buf
, "%d\n", chip
->arate
);
733 static int apds990x_set_arate(struct apds990x_chip
*chip
, int rate
)
737 for (i
= 0; i
< ARRAY_SIZE(arates_hz
); i
++)
738 if (rate
>= arates_hz
[i
])
741 if (i
== ARRAY_SIZE(arates_hz
))
744 /* Pick up corresponding persistence value */
745 chip
->lux_persistence
= apersis
[i
];
746 chip
->arate
= arates_hz
[i
];
748 /* If the chip is not in use, don't try to access it */
749 if (pm_runtime_suspended(&chip
->client
->dev
))
752 /* Persistence levels */
753 return apds990x_write_byte(chip
, APDS990X_PERS
,
754 (chip
->lux_persistence
<< APDS990X_APERS_SHIFT
) |
755 (chip
->prox_persistence
<< APDS990X_PPERS_SHIFT
));
758 static ssize_t
apds990x_rate_store(struct device
*dev
,
759 struct device_attribute
*attr
,
760 const char *buf
, size_t len
)
762 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
766 ret
= kstrtoul(buf
, 0, &value
);
770 mutex_lock(&chip
->mutex
);
771 ret
= apds990x_set_arate(chip
, value
);
772 mutex_unlock(&chip
->mutex
);
779 static DEVICE_ATTR(lux0_rate_avail
, S_IRUGO
, apds990x_rate_avail
, NULL
);
781 static DEVICE_ATTR(lux0_rate
, S_IRUGO
| S_IWUSR
, apds990x_rate_show
,
782 apds990x_rate_store
);
784 static ssize_t
apds990x_prox_show(struct device
*dev
,
785 struct device_attribute
*attr
, char *buf
)
788 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
790 if (pm_runtime_suspended(dev
) || !chip
->prox_en
)
793 mutex_lock(&chip
->mutex
);
794 ret
= sprintf(buf
, "%d\n", chip
->prox_data
);
795 mutex_unlock(&chip
->mutex
);
799 static DEVICE_ATTR(prox0_raw
, S_IRUGO
, apds990x_prox_show
, NULL
);
801 static ssize_t
apds990x_prox_range_show(struct device
*dev
,
802 struct device_attribute
*attr
, char *buf
)
804 return sprintf(buf
, "%u\n", APDS_PROX_RANGE
);
807 static DEVICE_ATTR(prox0_sensor_range
, S_IRUGO
, apds990x_prox_range_show
, NULL
);
809 static ssize_t
apds990x_prox_enable_show(struct device
*dev
,
810 struct device_attribute
*attr
, char *buf
)
812 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
814 return sprintf(buf
, "%d\n", chip
->prox_en
);
817 static ssize_t
apds990x_prox_enable_store(struct device
*dev
,
818 struct device_attribute
*attr
,
819 const char *buf
, size_t len
)
821 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
825 ret
= kstrtoul(buf
, 0, &value
);
829 mutex_lock(&chip
->mutex
);
836 else if (chip
->prox_en
> 0)
839 if (!pm_runtime_suspended(dev
))
840 apds990x_mode_on(chip
);
841 mutex_unlock(&chip
->mutex
);
845 static DEVICE_ATTR(prox0_raw_en
, S_IRUGO
| S_IWUSR
, apds990x_prox_enable_show
,
846 apds990x_prox_enable_store
);
848 static const char *reporting_modes
[] = {"trigger", "periodic"};
850 static ssize_t
apds990x_prox_reporting_mode_show(struct device
*dev
,
851 struct device_attribute
*attr
, char *buf
)
853 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
855 return sprintf(buf
, "%s\n",
856 reporting_modes
[!!chip
->prox_continuous_mode
]);
859 static ssize_t
apds990x_prox_reporting_mode_store(struct device
*dev
,
860 struct device_attribute
*attr
,
861 const char *buf
, size_t len
)
863 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
866 ret
= sysfs_match_string(reporting_modes
, buf
);
870 chip
->prox_continuous_mode
= ret
;
874 static DEVICE_ATTR(prox0_reporting_mode
, S_IRUGO
| S_IWUSR
,
875 apds990x_prox_reporting_mode_show
,
876 apds990x_prox_reporting_mode_store
);
878 static ssize_t
apds990x_prox_reporting_avail_show(struct device
*dev
,
879 struct device_attribute
*attr
, char *buf
)
881 return sprintf(buf
, "%s %s\n", reporting_modes
[0], reporting_modes
[1]);
884 static DEVICE_ATTR(prox0_reporting_mode_avail
, S_IRUGO
| S_IWUSR
,
885 apds990x_prox_reporting_avail_show
, NULL
);
888 static ssize_t
apds990x_lux_thresh_above_show(struct device
*dev
,
889 struct device_attribute
*attr
, char *buf
)
891 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
893 return sprintf(buf
, "%d\n", chip
->lux_thres_hi
);
896 static ssize_t
apds990x_lux_thresh_below_show(struct device
*dev
,
897 struct device_attribute
*attr
, char *buf
)
899 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
901 return sprintf(buf
, "%d\n", chip
->lux_thres_lo
);
904 static ssize_t
apds990x_set_lux_thresh(struct apds990x_chip
*chip
, u32
*target
,
907 unsigned long thresh
;
910 ret
= kstrtoul(buf
, 0, &thresh
);
914 if (thresh
> APDS_RANGE
)
917 mutex_lock(&chip
->mutex
);
920 * Don't update values in HW if we are still waiting for
921 * first interrupt to come after device handle open call.
923 if (!chip
->lux_wait_fresh_res
)
924 apds990x_refresh_athres(chip
);
925 mutex_unlock(&chip
->mutex
);
930 static ssize_t
apds990x_lux_thresh_above_store(struct device
*dev
,
931 struct device_attribute
*attr
,
932 const char *buf
, size_t len
)
934 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
935 int ret
= apds990x_set_lux_thresh(chip
, &chip
->lux_thres_hi
, buf
);
942 static ssize_t
apds990x_lux_thresh_below_store(struct device
*dev
,
943 struct device_attribute
*attr
,
944 const char *buf
, size_t len
)
946 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
947 int ret
= apds990x_set_lux_thresh(chip
, &chip
->lux_thres_lo
, buf
);
954 static DEVICE_ATTR(lux0_thresh_above_value
, S_IRUGO
| S_IWUSR
,
955 apds990x_lux_thresh_above_show
,
956 apds990x_lux_thresh_above_store
);
958 static DEVICE_ATTR(lux0_thresh_below_value
, S_IRUGO
| S_IWUSR
,
959 apds990x_lux_thresh_below_show
,
960 apds990x_lux_thresh_below_store
);
962 static ssize_t
apds990x_prox_threshold_show(struct device
*dev
,
963 struct device_attribute
*attr
, char *buf
)
965 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
967 return sprintf(buf
, "%d\n", chip
->prox_thres
);
970 static ssize_t
apds990x_prox_threshold_store(struct device
*dev
,
971 struct device_attribute
*attr
,
972 const char *buf
, size_t len
)
974 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
978 ret
= kstrtoul(buf
, 0, &value
);
982 if ((value
> APDS_RANGE
) || (value
== 0) ||
983 (value
< APDS_PROX_HYSTERESIS
))
986 mutex_lock(&chip
->mutex
);
987 chip
->prox_thres
= value
;
989 apds990x_force_p_refresh(chip
);
990 mutex_unlock(&chip
->mutex
);
994 static DEVICE_ATTR(prox0_thresh_above_value
, S_IRUGO
| S_IWUSR
,
995 apds990x_prox_threshold_show
,
996 apds990x_prox_threshold_store
);
998 static ssize_t
apds990x_power_state_show(struct device
*dev
,
999 struct device_attribute
*attr
, char *buf
)
1001 return sprintf(buf
, "%d\n", !pm_runtime_suspended(dev
));
1005 static ssize_t
apds990x_power_state_store(struct device
*dev
,
1006 struct device_attribute
*attr
,
1007 const char *buf
, size_t len
)
1009 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
1010 unsigned long value
;
1013 ret
= kstrtoul(buf
, 0, &value
);
1018 pm_runtime_get_sync(dev
);
1019 mutex_lock(&chip
->mutex
);
1020 chip
->lux_wait_fresh_res
= true;
1021 apds990x_force_a_refresh(chip
);
1022 apds990x_force_p_refresh(chip
);
1023 mutex_unlock(&chip
->mutex
);
1025 if (!pm_runtime_suspended(dev
))
1026 pm_runtime_put(dev
);
1031 static DEVICE_ATTR(power_state
, S_IRUGO
| S_IWUSR
,
1032 apds990x_power_state_show
,
1033 apds990x_power_state_store
);
1035 static ssize_t
apds990x_chip_id_show(struct device
*dev
,
1036 struct device_attribute
*attr
, char *buf
)
1038 struct apds990x_chip
*chip
= dev_get_drvdata(dev
);
1040 return sprintf(buf
, "%s %d\n", chip
->chipname
, chip
->revision
);
1043 static DEVICE_ATTR(chip_id
, S_IRUGO
, apds990x_chip_id_show
, NULL
);
1045 static struct attribute
*sysfs_attrs_ctrl
[] = {
1046 &dev_attr_lux0_calibscale
.attr
,
1047 &dev_attr_lux0_calibscale_default
.attr
,
1048 &dev_attr_lux0_input
.attr
,
1049 &dev_attr_lux0_sensor_range
.attr
,
1050 &dev_attr_lux0_rate
.attr
,
1051 &dev_attr_lux0_rate_avail
.attr
,
1052 &dev_attr_lux0_thresh_above_value
.attr
,
1053 &dev_attr_lux0_thresh_below_value
.attr
,
1054 &dev_attr_prox0_raw_en
.attr
,
1055 &dev_attr_prox0_raw
.attr
,
1056 &dev_attr_prox0_sensor_range
.attr
,
1057 &dev_attr_prox0_thresh_above_value
.attr
,
1058 &dev_attr_prox0_reporting_mode
.attr
,
1059 &dev_attr_prox0_reporting_mode_avail
.attr
,
1060 &dev_attr_chip_id
.attr
,
1061 &dev_attr_power_state
.attr
,
1065 static const struct attribute_group apds990x_attribute_group
[] = {
1066 {.attrs
= sysfs_attrs_ctrl
},
1069 static int apds990x_probe(struct i2c_client
*client
,
1070 const struct i2c_device_id
*id
)
1072 struct apds990x_chip
*chip
;
1075 chip
= kzalloc(sizeof *chip
, GFP_KERNEL
);
1079 i2c_set_clientdata(client
, chip
);
1080 chip
->client
= client
;
1082 init_waitqueue_head(&chip
->wait
);
1083 mutex_init(&chip
->mutex
);
1084 chip
->pdata
= client
->dev
.platform_data
;
1086 if (chip
->pdata
== NULL
) {
1087 dev_err(&client
->dev
, "platform data is mandatory\n");
1092 if (chip
->pdata
->cf
.ga
== 0) {
1093 /* set uncovered sensor default parameters */
1094 chip
->cf
.ga
= 1966; /* 0.48 * APDS_PARAM_SCALE */
1095 chip
->cf
.cf1
= 4096; /* 1.00 * APDS_PARAM_SCALE */
1096 chip
->cf
.irf1
= 9134; /* 2.23 * APDS_PARAM_SCALE */
1097 chip
->cf
.cf2
= 2867; /* 0.70 * APDS_PARAM_SCALE */
1098 chip
->cf
.irf2
= 5816; /* 1.42 * APDS_PARAM_SCALE */
1101 chip
->cf
= chip
->pdata
->cf
;
1104 /* precalculate inverse chip factors for threshold control */
1106 (chip
->cf
.irf1
- chip
->cf
.irf2
) * APDS_PARAM_SCALE
/
1107 (chip
->cf
.cf1
- chip
->cf
.cf2
);
1108 chip
->rcf
.cf1
= APDS_PARAM_SCALE
* APDS_PARAM_SCALE
/
1110 chip
->rcf
.irf1
= chip
->cf
.irf1
* APDS_PARAM_SCALE
/
1112 chip
->rcf
.cf2
= APDS_PARAM_SCALE
* APDS_PARAM_SCALE
/
1114 chip
->rcf
.irf2
= chip
->cf
.irf2
* APDS_PARAM_SCALE
/
1117 /* Set something to start with */
1118 chip
->lux_thres_hi
= APDS_LUX_DEF_THRES_HI
;
1119 chip
->lux_thres_lo
= APDS_LUX_DEF_THRES_LO
;
1120 chip
->lux_calib
= APDS_LUX_NEUTRAL_CALIB_VALUE
;
1122 chip
->prox_thres
= APDS_PROX_DEF_THRES
;
1123 chip
->pdrive
= chip
->pdata
->pdrive
;
1124 chip
->pdiode
= APDS_PDIODE_IR
;
1125 chip
->pgain
= APDS_PGAIN_1X
;
1126 chip
->prox_calib
= APDS_PROX_NEUTRAL_CALIB_VALUE
;
1127 chip
->prox_persistence
= APDS_DEFAULT_PROX_PERS
;
1128 chip
->prox_continuous_mode
= false;
1130 chip
->regs
[0].supply
= reg_vcc
;
1131 chip
->regs
[1].supply
= reg_vled
;
1133 err
= regulator_bulk_get(&client
->dev
,
1134 ARRAY_SIZE(chip
->regs
), chip
->regs
);
1136 dev_err(&client
->dev
, "Cannot get regulators\n");
1140 err
= regulator_bulk_enable(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1142 dev_err(&client
->dev
, "Cannot enable regulators\n");
1146 usleep_range(APDS_STARTUP_DELAY
, 2 * APDS_STARTUP_DELAY
);
1148 err
= apds990x_detect(chip
);
1150 dev_err(&client
->dev
, "APDS990X not found\n");
1154 pm_runtime_set_active(&client
->dev
);
1156 apds990x_configure(chip
);
1157 apds990x_set_arate(chip
, APDS_LUX_DEFAULT_RATE
);
1158 apds990x_mode_on(chip
);
1160 pm_runtime_enable(&client
->dev
);
1162 if (chip
->pdata
->setup_resources
) {
1163 err
= chip
->pdata
->setup_resources();
1170 err
= sysfs_create_group(&chip
->client
->dev
.kobj
,
1171 apds990x_attribute_group
);
1173 dev_err(&chip
->client
->dev
, "Sysfs registration failed\n");
1177 err
= request_threaded_irq(client
->irq
, NULL
,
1179 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_LOW
|
1183 dev_err(&client
->dev
, "could not get IRQ %d\n",
1189 sysfs_remove_group(&chip
->client
->dev
.kobj
,
1190 &apds990x_attribute_group
[0]);
1192 if (chip
->pdata
&& chip
->pdata
->release_resources
)
1193 chip
->pdata
->release_resources();
1195 regulator_bulk_disable(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1197 regulator_bulk_free(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1203 static int apds990x_remove(struct i2c_client
*client
)
1205 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1207 free_irq(client
->irq
, chip
);
1208 sysfs_remove_group(&chip
->client
->dev
.kobj
,
1209 apds990x_attribute_group
);
1211 if (chip
->pdata
&& chip
->pdata
->release_resources
)
1212 chip
->pdata
->release_resources();
1214 if (!pm_runtime_suspended(&client
->dev
))
1215 apds990x_chip_off(chip
);
1217 pm_runtime_disable(&client
->dev
);
1218 pm_runtime_set_suspended(&client
->dev
);
1220 regulator_bulk_free(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1226 #ifdef CONFIG_PM_SLEEP
1227 static int apds990x_suspend(struct device
*dev
)
1229 struct i2c_client
*client
= to_i2c_client(dev
);
1230 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1232 apds990x_chip_off(chip
);
1236 static int apds990x_resume(struct device
*dev
)
1238 struct i2c_client
*client
= to_i2c_client(dev
);
1239 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1242 * If we were enabled at suspend time, it is expected
1243 * everything works nice and smoothly. Chip_on is enough
1245 apds990x_chip_on(chip
);
1252 static int apds990x_runtime_suspend(struct device
*dev
)
1254 struct i2c_client
*client
= to_i2c_client(dev
);
1255 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1257 apds990x_chip_off(chip
);
1261 static int apds990x_runtime_resume(struct device
*dev
)
1263 struct i2c_client
*client
= to_i2c_client(dev
);
1264 struct apds990x_chip
*chip
= i2c_get_clientdata(client
);
1266 apds990x_chip_on(chip
);
1272 static const struct i2c_device_id apds990x_id
[] = {
1277 MODULE_DEVICE_TABLE(i2c
, apds990x_id
);
1279 static const struct dev_pm_ops apds990x_pm_ops
= {
1280 SET_SYSTEM_SLEEP_PM_OPS(apds990x_suspend
, apds990x_resume
)
1281 SET_RUNTIME_PM_OPS(apds990x_runtime_suspend
,
1282 apds990x_runtime_resume
,
1286 static struct i2c_driver apds990x_driver
= {
1289 .pm
= &apds990x_pm_ops
,
1291 .probe
= apds990x_probe
,
1292 .remove
= apds990x_remove
,
1293 .id_table
= apds990x_id
,
1296 module_i2c_driver(apds990x_driver
);
1298 MODULE_DESCRIPTION("APDS990X combined ALS and proximity sensor");
1299 MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1300 MODULE_LICENSE("GPL v2");