2 * This file is part of the ROHM BH1770GLC / OSRAM SFH7770 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/platform_data/bh1770glc.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/workqueue.h>
34 #include <linux/delay.h>
35 #include <linux/wait.h>
36 #include <linux/slab.h>
38 #define BH1770_ALS_CONTROL 0x80 /* ALS operation mode control */
39 #define BH1770_PS_CONTROL 0x81 /* PS operation mode control */
40 #define BH1770_I_LED 0x82 /* active LED and LED1, LED2 current */
41 #define BH1770_I_LED3 0x83 /* LED3 current setting */
42 #define BH1770_ALS_PS_MEAS 0x84 /* Forced mode trigger */
43 #define BH1770_PS_MEAS_RATE 0x85 /* PS meas. rate at stand alone mode */
44 #define BH1770_ALS_MEAS_RATE 0x86 /* ALS meas. rate at stand alone mode */
45 #define BH1770_PART_ID 0x8a /* Part number and revision ID */
46 #define BH1770_MANUFACT_ID 0x8b /* Manufacturerer ID */
47 #define BH1770_ALS_DATA_0 0x8c /* ALS DATA low byte */
48 #define BH1770_ALS_DATA_1 0x8d /* ALS DATA high byte */
49 #define BH1770_ALS_PS_STATUS 0x8e /* Measurement data and int status */
50 #define BH1770_PS_DATA_LED1 0x8f /* PS data from LED1 */
51 #define BH1770_PS_DATA_LED2 0x90 /* PS data from LED2 */
52 #define BH1770_PS_DATA_LED3 0x91 /* PS data from LED3 */
53 #define BH1770_INTERRUPT 0x92 /* Interrupt setting */
54 #define BH1770_PS_TH_LED1 0x93 /* PS interrupt threshold for LED1 */
55 #define BH1770_PS_TH_LED2 0x94 /* PS interrupt threshold for LED2 */
56 #define BH1770_PS_TH_LED3 0x95 /* PS interrupt threshold for LED3 */
57 #define BH1770_ALS_TH_UP_0 0x96 /* ALS upper threshold low byte */
58 #define BH1770_ALS_TH_UP_1 0x97 /* ALS upper threshold high byte */
59 #define BH1770_ALS_TH_LOW_0 0x98 /* ALS lower threshold low byte */
60 #define BH1770_ALS_TH_LOW_1 0x99 /* ALS lower threshold high byte */
63 #define BH1770_MANUFACT_ROHM 0x01
64 #define BH1770_MANUFACT_OSRAM 0x03
67 #define BH1770_PART 0x90
68 #define BH1770_PART_MASK 0xf0
69 #define BH1770_REV_MASK 0x0f
70 #define BH1770_REV_SHIFT 0
71 #define BH1770_REV_0 0x00
72 #define BH1770_REV_1 0x01
74 /* Operating modes for both */
75 #define BH1770_STANDBY 0x00
76 #define BH1770_FORCED 0x02
77 #define BH1770_STANDALONE 0x03
78 #define BH1770_SWRESET (0x01 << 2)
80 #define BH1770_PS_TRIG_MEAS (1 << 0)
81 #define BH1770_ALS_TRIG_MEAS (1 << 1)
83 /* Interrupt control */
84 #define BH1770_INT_OUTPUT_MODE (1 << 3) /* 0 = latched */
85 #define BH1770_INT_POLARITY (1 << 2) /* 1 = active high */
86 #define BH1770_INT_ALS_ENA (1 << 1)
87 #define BH1770_INT_PS_ENA (1 << 0)
89 /* Interrupt status */
90 #define BH1770_INT_LED1_DATA (1 << 0)
91 #define BH1770_INT_LED1_INT (1 << 1)
92 #define BH1770_INT_LED2_DATA (1 << 2)
93 #define BH1770_INT_LED2_INT (1 << 3)
94 #define BH1770_INT_LED3_DATA (1 << 4)
95 #define BH1770_INT_LED3_INT (1 << 5)
96 #define BH1770_INT_LEDS_INT ((1 << 1) | (1 << 3) | (1 << 5))
97 #define BH1770_INT_ALS_DATA (1 << 6)
98 #define BH1770_INT_ALS_INT (1 << 7)
101 #define BH1770_LED1 0x00
103 #define BH1770_DISABLE 0
104 #define BH1770_ENABLE 1
105 #define BH1770_PROX_CHANNELS 1
107 #define BH1770_LUX_DEFAULT_RATE 1 /* Index to lux rate table */
108 #define BH1770_PROX_DEFAULT_RATE 1 /* Direct HW value =~ 50Hz */
109 #define BH1770_PROX_DEF_RATE_THRESH 6 /* Direct HW value =~ 5 Hz */
110 #define BH1770_STARTUP_DELAY 50
111 #define BH1770_RESET_TIME 10
112 #define BH1770_TIMEOUT 2100 /* Timeout in 2.1 seconds */
114 #define BH1770_LUX_RANGE 65535
115 #define BH1770_PROX_RANGE 255
116 #define BH1770_COEF_SCALER 1024
117 #define BH1770_CALIB_SCALER 8192
118 #define BH1770_LUX_NEUTRAL_CALIB_VALUE (1 * BH1770_CALIB_SCALER)
119 #define BH1770_LUX_DEF_THRES 1000
120 #define BH1770_PROX_DEF_THRES 70
121 #define BH1770_PROX_DEF_ABS_THRES 100
122 #define BH1770_DEFAULT_PERSISTENCE 10
123 #define BH1770_PROX_MAX_PERSISTENCE 50
124 #define BH1770_LUX_GA_SCALE 16384
125 #define BH1770_LUX_CF_SCALE 2048 /* CF ChipFactor */
126 #define BH1770_NEUTRAL_CF BH1770_LUX_CF_SCALE
127 #define BH1770_LUX_CORR_SCALE 4096
129 #define PROX_ABOVE_THRESHOLD 1
130 #define PROX_BELOW_THRESHOLD 0
132 #define PROX_IGNORE_LUX_LIMIT 500
135 struct bh1770_platform_data
*pdata
;
138 struct i2c_client
*client
;
139 struct regulator_bulk_data regs
[2];
140 struct mutex mutex
; /* avoid parallel access */
141 wait_queue_head_t wait
;
145 struct delayed_work prox_work
;
146 u32 lux_cf
; /* Chip specific factor */
152 u16 lux_threshold_hi
;
153 u16 lux_threshold_lo
;
154 u16 lux_thres_hi_onchip
;
155 u16 lux_thres_lo_onchip
;
156 bool lux_wait_result
;
158 int prox_enable_count
;
162 int prox_rate_threshold
;
164 u8 prox_persistence_counter
;
167 u8 prox_threshold_hw
;
168 bool prox_force_update
;
173 static const char reg_vcc
[] = "Vcc";
174 static const char reg_vleds
[] = "Vleds";
177 * Supported stand alone rates in ms from chip data sheet
178 * {10, 20, 30, 40, 70, 100, 200, 500, 1000, 2000};
180 static const s16 prox_rates_hz
[] = {100, 50, 33, 25, 14, 10, 5, 2};
181 static const s16 prox_rates_ms
[] = {10, 20, 30, 40, 70, 100, 200, 500};
184 * Supported stand alone rates in ms from chip data sheet
185 * {100, 200, 500, 1000, 2000};
187 static const s16 lux_rates_hz
[] = {10, 5, 2, 1, 0};
190 * interrupt control functions are called while keeping chip->mutex
191 * excluding module probe / remove
193 static inline int bh1770_lux_interrupt_control(struct bh1770_chip
*chip
,
196 chip
->int_mode_lux
= lux
;
197 /* Set interrupt modes, interrupt active low, latched */
198 return i2c_smbus_write_byte_data(chip
->client
,
200 (lux
<< 1) | chip
->int_mode_prox
);
203 static inline int bh1770_prox_interrupt_control(struct bh1770_chip
*chip
,
206 chip
->int_mode_prox
= ps
;
207 return i2c_smbus_write_byte_data(chip
->client
,
209 (chip
->int_mode_lux
<< 1) | (ps
<< 0));
212 /* chip->mutex is always kept here */
213 static int bh1770_lux_rate(struct bh1770_chip
*chip
, int rate_index
)
215 /* sysfs may call this when the chip is powered off */
216 if (pm_runtime_suspended(&chip
->client
->dev
))
219 /* Proper proximity response needs fastest lux rate (100ms) */
220 if (chip
->prox_enable_count
)
223 return i2c_smbus_write_byte_data(chip
->client
,
224 BH1770_ALS_MEAS_RATE
,
228 static int bh1770_prox_rate(struct bh1770_chip
*chip
, int mode
)
232 rate
= (mode
== PROX_ABOVE_THRESHOLD
) ?
233 chip
->prox_rate_threshold
: chip
->prox_rate
;
235 return i2c_smbus_write_byte_data(chip
->client
,
240 /* InfraredLED is controlled by the chip during proximity scanning */
241 static inline int bh1770_led_cfg(struct bh1770_chip
*chip
)
243 /* LED cfg, current for leds 1 and 2 */
244 return i2c_smbus_write_byte_data(chip
->client
,
247 (BH1770_LED_5mA
<< 3) |
252 * Following two functions converts raw ps values from HW to normalized
253 * values. Purpose is to compensate differences between different sensor
254 * versions and variants so that result means about the same between
257 static inline u8
bh1770_psraw_to_adjusted(struct bh1770_chip
*chip
, u8 psraw
)
260 adjusted
= (u16
)(((u32
)(psraw
+ chip
->prox_const
) * chip
->prox_coef
) /
262 if (adjusted
> BH1770_PROX_RANGE
)
263 adjusted
= BH1770_PROX_RANGE
;
267 static inline u8
bh1770_psadjusted_to_raw(struct bh1770_chip
*chip
, u8 ps
)
271 raw
= (((u32
)ps
* BH1770_COEF_SCALER
) / chip
->prox_coef
);
272 if (raw
> chip
->prox_const
)
273 raw
= raw
- chip
->prox_const
;
280 * Following two functions converts raw lux values from HW to normalized
281 * values. Purpose is to compensate differences between different sensor
282 * versions and variants so that result means about the same between
283 * versions. Chip->mutex is kept when this is called.
285 static int bh1770_prox_set_threshold(struct bh1770_chip
*chip
)
289 /* sysfs may call this when the chip is powered off */
290 if (pm_runtime_suspended(&chip
->client
->dev
))
293 tmp
= bh1770_psadjusted_to_raw(chip
, chip
->prox_threshold
);
294 chip
->prox_threshold_hw
= tmp
;
296 return i2c_smbus_write_byte_data(chip
->client
, BH1770_PS_TH_LED1
,
300 static inline u16
bh1770_lux_raw_to_adjusted(struct bh1770_chip
*chip
, u16 raw
)
303 lux
= ((u32
)raw
* chip
->lux_corr
) / BH1770_LUX_CORR_SCALE
;
304 return min(lux
, (u32
)BH1770_LUX_RANGE
);
307 static inline u16
bh1770_lux_adjusted_to_raw(struct bh1770_chip
*chip
,
310 return (u32
)adjusted
* BH1770_LUX_CORR_SCALE
/ chip
->lux_corr
;
313 /* chip->mutex is kept when this is called */
314 static int bh1770_lux_update_thresholds(struct bh1770_chip
*chip
,
315 u16 threshold_hi
, u16 threshold_lo
)
320 /* sysfs may call this when the chip is powered off */
321 if (pm_runtime_suspended(&chip
->client
->dev
))
325 * Compensate threshold values with the correction factors if not
326 * set to minimum or maximum.
327 * Min & max values disables interrupts.
329 if (threshold_hi
!= BH1770_LUX_RANGE
&& threshold_hi
!= 0)
330 threshold_hi
= bh1770_lux_adjusted_to_raw(chip
, threshold_hi
);
332 if (threshold_lo
!= BH1770_LUX_RANGE
&& threshold_lo
!= 0)
333 threshold_lo
= bh1770_lux_adjusted_to_raw(chip
, threshold_lo
);
335 if (chip
->lux_thres_hi_onchip
== threshold_hi
&&
336 chip
->lux_thres_lo_onchip
== threshold_lo
)
339 chip
->lux_thres_hi_onchip
= threshold_hi
;
340 chip
->lux_thres_lo_onchip
= threshold_lo
;
342 data
[0] = threshold_hi
;
343 data
[1] = threshold_hi
>> 8;
344 data
[2] = threshold_lo
;
345 data
[3] = threshold_lo
>> 8;
347 ret
= i2c_smbus_write_i2c_block_data(chip
->client
,
354 static int bh1770_lux_get_result(struct bh1770_chip
*chip
)
359 ret
= i2c_smbus_read_byte_data(chip
->client
, BH1770_ALS_DATA_0
);
364 ret
= i2c_smbus_read_byte_data(chip
->client
, BH1770_ALS_DATA_1
);
368 chip
->lux_data_raw
= data
| ((ret
& 0xff) << 8);
373 /* Calculate correction value which contains chip and device specific parts */
374 static u32
bh1770_get_corr_value(struct bh1770_chip
*chip
)
377 /* Impact of glass attenuation correction */
378 tmp
= (BH1770_LUX_CORR_SCALE
* chip
->lux_ga
) / BH1770_LUX_GA_SCALE
;
379 /* Impact of chip factor correction */
380 tmp
= (tmp
* chip
->lux_cf
) / BH1770_LUX_CF_SCALE
;
381 /* Impact of Device specific calibration correction */
382 tmp
= (tmp
* chip
->lux_calib
) / BH1770_CALIB_SCALER
;
386 static int bh1770_lux_read_result(struct bh1770_chip
*chip
)
388 bh1770_lux_get_result(chip
);
389 return bh1770_lux_raw_to_adjusted(chip
, chip
->lux_data_raw
);
393 * Chip on / off functions are called while keeping mutex except probe
396 static int bh1770_chip_on(struct bh1770_chip
*chip
)
398 int ret
= regulator_bulk_enable(ARRAY_SIZE(chip
->regs
),
403 usleep_range(BH1770_STARTUP_DELAY
, BH1770_STARTUP_DELAY
* 2);
406 i2c_smbus_write_byte_data(chip
->client
, BH1770_ALS_CONTROL
,
408 usleep_range(BH1770_RESET_TIME
, BH1770_RESET_TIME
* 2);
411 * ALS is started always since proximity needs als results
412 * for realibility estimation.
413 * Let's assume dark until the first ALS measurement is ready.
415 chip
->lux_data_raw
= 0;
417 ret
= i2c_smbus_write_byte_data(chip
->client
,
418 BH1770_ALS_CONTROL
, BH1770_STANDALONE
);
420 /* Assume reset defaults */
421 chip
->lux_thres_hi_onchip
= BH1770_LUX_RANGE
;
422 chip
->lux_thres_lo_onchip
= 0;
427 static void bh1770_chip_off(struct bh1770_chip
*chip
)
429 i2c_smbus_write_byte_data(chip
->client
,
430 BH1770_INTERRUPT
, BH1770_DISABLE
);
431 i2c_smbus_write_byte_data(chip
->client
,
432 BH1770_ALS_CONTROL
, BH1770_STANDBY
);
433 i2c_smbus_write_byte_data(chip
->client
,
434 BH1770_PS_CONTROL
, BH1770_STANDBY
);
435 regulator_bulk_disable(ARRAY_SIZE(chip
->regs
), chip
->regs
);
438 /* chip->mutex is kept when this is called */
439 static int bh1770_prox_mode_control(struct bh1770_chip
*chip
)
441 if (chip
->prox_enable_count
) {
442 chip
->prox_force_update
= true; /* Force immediate update */
444 bh1770_lux_rate(chip
, chip
->lux_rate_index
);
445 bh1770_prox_set_threshold(chip
);
446 bh1770_led_cfg(chip
);
447 bh1770_prox_rate(chip
, PROX_BELOW_THRESHOLD
);
448 bh1770_prox_interrupt_control(chip
, BH1770_ENABLE
);
449 i2c_smbus_write_byte_data(chip
->client
,
450 BH1770_PS_CONTROL
, BH1770_STANDALONE
);
453 bh1770_lux_rate(chip
, chip
->lux_rate_index
);
454 bh1770_prox_interrupt_control(chip
, BH1770_DISABLE
);
455 i2c_smbus_write_byte_data(chip
->client
,
456 BH1770_PS_CONTROL
, BH1770_STANDBY
);
461 /* chip->mutex is kept when this is called */
462 static int bh1770_prox_read_result(struct bh1770_chip
*chip
)
468 ret
= i2c_smbus_read_byte_data(chip
->client
, BH1770_PS_DATA_LED1
);
472 if (ret
> chip
->prox_threshold_hw
)
478 * when ALS levels goes above limit, proximity result may be
479 * false proximity. Thus ignore the result. With real proximity
480 * there is a shadow causing low als levels.
482 if (chip
->lux_data_raw
> PROX_IGNORE_LUX_LIMIT
)
485 chip
->prox_data
= bh1770_psraw_to_adjusted(chip
, ret
);
487 /* Strong proximity level or force mode requires immediate response */
488 if (chip
->prox_data
>= chip
->prox_abs_thres
||
489 chip
->prox_force_update
)
490 chip
->prox_persistence_counter
= chip
->prox_persistence
;
492 chip
->prox_force_update
= false;
494 /* Persistence filttering to reduce false proximity events */
496 if (chip
->prox_persistence_counter
< chip
->prox_persistence
) {
497 chip
->prox_persistence_counter
++;
500 mode
= PROX_ABOVE_THRESHOLD
;
504 chip
->prox_persistence_counter
= 0;
505 mode
= PROX_BELOW_THRESHOLD
;
510 /* Set proximity detection rate based on above or below value */
512 bh1770_prox_rate(chip
, mode
);
513 sysfs_notify(&chip
->client
->dev
.kobj
, NULL
, "prox0_raw");
519 static int bh1770_detect(struct bh1770_chip
*chip
)
521 struct i2c_client
*client
= chip
->client
;
525 ret
= i2c_smbus_read_byte_data(client
, BH1770_MANUFACT_ID
);
530 ret
= i2c_smbus_read_byte_data(client
, BH1770_PART_ID
);
535 chip
->revision
= (part
& BH1770_REV_MASK
) >> BH1770_REV_SHIFT
;
536 chip
->prox_coef
= BH1770_COEF_SCALER
;
537 chip
->prox_const
= 0;
538 chip
->lux_cf
= BH1770_NEUTRAL_CF
;
540 if ((manu
== BH1770_MANUFACT_ROHM
) &&
541 ((part
& BH1770_PART_MASK
) == BH1770_PART
)) {
542 snprintf(chip
->chipname
, sizeof(chip
->chipname
), "BH1770GLC");
546 if ((manu
== BH1770_MANUFACT_OSRAM
) &&
547 ((part
& BH1770_PART_MASK
) == BH1770_PART
)) {
548 snprintf(chip
->chipname
, sizeof(chip
->chipname
), "SFH7770");
549 /* Values selected by comparing different versions */
550 chip
->prox_coef
= 819; /* 0.8 * BH1770_COEF_SCALER */
551 chip
->prox_const
= 40;
557 dev_dbg(&client
->dev
, "BH1770 or SFH7770 not found\n");
563 * This work is re-scheduled at every proximity interrupt.
564 * If this work is running, it means that there hasn't been any
565 * proximity interrupt in time. Situation is handled as no-proximity.
566 * It would be nice to have low-threshold interrupt or interrupt
567 * when measurement and hi-threshold are both 0. But neither of those exists.
568 * This is a workaroud for missing HW feature.
571 static void bh1770_prox_work(struct work_struct
*work
)
573 struct bh1770_chip
*chip
=
574 container_of(work
, struct bh1770_chip
, prox_work
.work
);
576 mutex_lock(&chip
->mutex
);
577 bh1770_prox_read_result(chip
);
578 mutex_unlock(&chip
->mutex
);
581 /* This is threaded irq handler */
582 static irqreturn_t
bh1770_irq(int irq
, void *data
)
584 struct bh1770_chip
*chip
= data
;
588 mutex_lock(&chip
->mutex
);
589 status
= i2c_smbus_read_byte_data(chip
->client
, BH1770_ALS_PS_STATUS
);
591 /* Acknowledge interrupt by reading this register */
592 i2c_smbus_read_byte_data(chip
->client
, BH1770_INTERRUPT
);
595 * Check if there is fresh data available for als.
596 * If this is the very first data, update thresholds after that.
598 if (status
& BH1770_INT_ALS_DATA
) {
599 bh1770_lux_get_result(chip
);
600 if (unlikely(chip
->lux_wait_result
)) {
601 chip
->lux_wait_result
= false;
602 wake_up(&chip
->wait
);
603 bh1770_lux_update_thresholds(chip
,
604 chip
->lux_threshold_hi
,
605 chip
->lux_threshold_lo
);
609 /* Disable interrupt logic to guarantee acknowledgement */
610 i2c_smbus_write_byte_data(chip
->client
, BH1770_INTERRUPT
,
611 (0 << 1) | (0 << 0));
613 if ((status
& BH1770_INT_ALS_INT
))
614 sysfs_notify(&chip
->client
->dev
.kobj
, NULL
, "lux0_input");
616 if (chip
->int_mode_prox
&& (status
& BH1770_INT_LEDS_INT
)) {
617 rate
= prox_rates_ms
[chip
->prox_rate_threshold
];
618 bh1770_prox_read_result(chip
);
621 /* Re-enable interrupt logic */
622 i2c_smbus_write_byte_data(chip
->client
, BH1770_INTERRUPT
,
623 (chip
->int_mode_lux
<< 1) |
624 (chip
->int_mode_prox
<< 0));
625 mutex_unlock(&chip
->mutex
);
628 * Can't cancel work while keeping mutex since the work uses the
633 * Simulate missing no-proximity interrupt 50ms after the
634 * next expected interrupt time.
636 cancel_delayed_work_sync(&chip
->prox_work
);
637 schedule_delayed_work(&chip
->prox_work
,
638 msecs_to_jiffies(rate
+ 50));
643 static ssize_t
bh1770_power_state_store(struct device
*dev
,
644 struct device_attribute
*attr
,
645 const char *buf
, size_t count
)
647 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
651 ret
= kstrtoul(buf
, 0, &value
);
655 mutex_lock(&chip
->mutex
);
657 pm_runtime_get_sync(dev
);
659 ret
= bh1770_lux_rate(chip
, chip
->lux_rate_index
);
665 ret
= bh1770_lux_interrupt_control(chip
, BH1770_ENABLE
);
671 /* This causes interrupt after the next measurement cycle */
672 bh1770_lux_update_thresholds(chip
, BH1770_LUX_DEF_THRES
,
673 BH1770_LUX_DEF_THRES
);
674 /* Inform that we are waiting for a result from ALS */
675 chip
->lux_wait_result
= true;
676 bh1770_prox_mode_control(chip
);
677 } else if (!pm_runtime_suspended(dev
)) {
682 mutex_unlock(&chip
->mutex
);
686 static ssize_t
bh1770_power_state_show(struct device
*dev
,
687 struct device_attribute
*attr
, char *buf
)
689 return sprintf(buf
, "%d\n", !pm_runtime_suspended(dev
));
692 static ssize_t
bh1770_lux_result_show(struct device
*dev
,
693 struct device_attribute
*attr
, char *buf
)
695 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
699 if (pm_runtime_suspended(dev
))
700 return -EIO
; /* Chip is not enabled at all */
702 timeout
= wait_event_interruptible_timeout(chip
->wait
,
703 !chip
->lux_wait_result
,
704 msecs_to_jiffies(BH1770_TIMEOUT
));
708 mutex_lock(&chip
->mutex
);
709 ret
= sprintf(buf
, "%d\n", bh1770_lux_read_result(chip
));
710 mutex_unlock(&chip
->mutex
);
715 static ssize_t
bh1770_lux_range_show(struct device
*dev
,
716 struct device_attribute
*attr
, char *buf
)
718 return sprintf(buf
, "%d\n", BH1770_LUX_RANGE
);
721 static ssize_t
bh1770_prox_enable_store(struct device
*dev
,
722 struct device_attribute
*attr
,
723 const char *buf
, size_t count
)
725 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
729 ret
= kstrtoul(buf
, 0, &value
);
733 mutex_lock(&chip
->mutex
);
734 /* Assume no proximity. Sensor will tell real state soon */
735 if (!chip
->prox_enable_count
)
739 chip
->prox_enable_count
++;
740 else if (chip
->prox_enable_count
> 0)
741 chip
->prox_enable_count
--;
745 /* Run control only when chip is powered on */
746 if (!pm_runtime_suspended(dev
))
747 bh1770_prox_mode_control(chip
);
749 mutex_unlock(&chip
->mutex
);
753 static ssize_t
bh1770_prox_enable_show(struct device
*dev
,
754 struct device_attribute
*attr
, char *buf
)
756 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
759 mutex_lock(&chip
->mutex
);
760 len
= sprintf(buf
, "%d\n", chip
->prox_enable_count
);
761 mutex_unlock(&chip
->mutex
);
765 static ssize_t
bh1770_prox_result_show(struct device
*dev
,
766 struct device_attribute
*attr
, char *buf
)
768 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
771 mutex_lock(&chip
->mutex
);
772 if (chip
->prox_enable_count
&& !pm_runtime_suspended(dev
))
773 ret
= sprintf(buf
, "%d\n", chip
->prox_data
);
776 mutex_unlock(&chip
->mutex
);
780 static ssize_t
bh1770_prox_range_show(struct device
*dev
,
781 struct device_attribute
*attr
, char *buf
)
783 return sprintf(buf
, "%d\n", BH1770_PROX_RANGE
);
786 static ssize_t
bh1770_get_prox_rate_avail(struct device
*dev
,
787 struct device_attribute
*attr
, char *buf
)
791 for (i
= 0; i
< ARRAY_SIZE(prox_rates_hz
); i
++)
792 pos
+= sprintf(buf
+ pos
, "%d ", prox_rates_hz
[i
]);
793 sprintf(buf
+ pos
- 1, "\n");
797 static ssize_t
bh1770_get_prox_rate_above(struct device
*dev
,
798 struct device_attribute
*attr
, char *buf
)
800 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
801 return sprintf(buf
, "%d\n", prox_rates_hz
[chip
->prox_rate_threshold
]);
804 static ssize_t
bh1770_get_prox_rate_below(struct device
*dev
,
805 struct device_attribute
*attr
, char *buf
)
807 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
808 return sprintf(buf
, "%d\n", prox_rates_hz
[chip
->prox_rate
]);
811 static int bh1770_prox_rate_validate(int rate
)
815 for (i
= 0; i
< ARRAY_SIZE(prox_rates_hz
) - 1; i
++)
816 if (rate
>= prox_rates_hz
[i
])
821 static ssize_t
bh1770_set_prox_rate_above(struct device
*dev
,
822 struct device_attribute
*attr
,
823 const char *buf
, size_t count
)
825 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
829 ret
= kstrtoul(buf
, 0, &value
);
833 mutex_lock(&chip
->mutex
);
834 chip
->prox_rate_threshold
= bh1770_prox_rate_validate(value
);
835 mutex_unlock(&chip
->mutex
);
839 static ssize_t
bh1770_set_prox_rate_below(struct device
*dev
,
840 struct device_attribute
*attr
,
841 const char *buf
, size_t count
)
843 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
847 ret
= kstrtoul(buf
, 0, &value
);
851 mutex_lock(&chip
->mutex
);
852 chip
->prox_rate
= bh1770_prox_rate_validate(value
);
853 mutex_unlock(&chip
->mutex
);
857 static ssize_t
bh1770_get_prox_thres(struct device
*dev
,
858 struct device_attribute
*attr
, char *buf
)
860 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
861 return sprintf(buf
, "%d\n", chip
->prox_threshold
);
864 static ssize_t
bh1770_set_prox_thres(struct device
*dev
,
865 struct device_attribute
*attr
,
866 const char *buf
, size_t count
)
868 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
872 ret
= kstrtoul(buf
, 0, &value
);
876 if (value
> BH1770_PROX_RANGE
)
879 mutex_lock(&chip
->mutex
);
880 chip
->prox_threshold
= value
;
881 ret
= bh1770_prox_set_threshold(chip
);
882 mutex_unlock(&chip
->mutex
);
888 static ssize_t
bh1770_prox_persistence_show(struct device
*dev
,
889 struct device_attribute
*attr
, char *buf
)
891 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
893 return sprintf(buf
, "%u\n", chip
->prox_persistence
);
896 static ssize_t
bh1770_prox_persistence_store(struct device
*dev
,
897 struct device_attribute
*attr
,
898 const char *buf
, size_t len
)
900 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
904 ret
= kstrtoul(buf
, 0, &value
);
908 if (value
> BH1770_PROX_MAX_PERSISTENCE
)
911 chip
->prox_persistence
= value
;
916 static ssize_t
bh1770_prox_abs_thres_show(struct device
*dev
,
917 struct device_attribute
*attr
, char *buf
)
919 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
920 return sprintf(buf
, "%u\n", chip
->prox_abs_thres
);
923 static ssize_t
bh1770_prox_abs_thres_store(struct device
*dev
,
924 struct device_attribute
*attr
,
925 const char *buf
, size_t len
)
927 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
931 ret
= kstrtoul(buf
, 0, &value
);
935 if (value
> BH1770_PROX_RANGE
)
938 chip
->prox_abs_thres
= value
;
943 static ssize_t
bh1770_chip_id_show(struct device
*dev
,
944 struct device_attribute
*attr
, char *buf
)
946 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
947 return sprintf(buf
, "%s rev %d\n", chip
->chipname
, chip
->revision
);
950 static ssize_t
bh1770_lux_calib_default_show(struct device
*dev
,
951 struct device_attribute
*attr
, char *buf
)
953 return sprintf(buf
, "%u\n", BH1770_CALIB_SCALER
);
956 static ssize_t
bh1770_lux_calib_show(struct device
*dev
,
957 struct device_attribute
*attr
, char *buf
)
959 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
962 mutex_lock(&chip
->mutex
);
963 len
= sprintf(buf
, "%u\n", chip
->lux_calib
);
964 mutex_unlock(&chip
->mutex
);
968 static ssize_t
bh1770_lux_calib_store(struct device
*dev
,
969 struct device_attribute
*attr
,
970 const char *buf
, size_t len
)
972 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
978 ret
= kstrtoul(buf
, 0, &value
);
982 mutex_lock(&chip
->mutex
);
983 old_calib
= chip
->lux_calib
;
984 chip
->lux_calib
= value
;
985 new_corr
= bh1770_get_corr_value(chip
);
987 chip
->lux_calib
= old_calib
;
988 mutex_unlock(&chip
->mutex
);
991 chip
->lux_corr
= new_corr
;
992 /* Refresh thresholds on HW after changing correction value */
993 bh1770_lux_update_thresholds(chip
, chip
->lux_threshold_hi
,
994 chip
->lux_threshold_lo
);
996 mutex_unlock(&chip
->mutex
);
1001 static ssize_t
bh1770_get_lux_rate_avail(struct device
*dev
,
1002 struct device_attribute
*attr
, char *buf
)
1006 for (i
= 0; i
< ARRAY_SIZE(lux_rates_hz
); i
++)
1007 pos
+= sprintf(buf
+ pos
, "%d ", lux_rates_hz
[i
]);
1008 sprintf(buf
+ pos
- 1, "\n");
1012 static ssize_t
bh1770_get_lux_rate(struct device
*dev
,
1013 struct device_attribute
*attr
, char *buf
)
1015 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
1016 return sprintf(buf
, "%d\n", lux_rates_hz
[chip
->lux_rate_index
]);
1019 static ssize_t
bh1770_set_lux_rate(struct device
*dev
,
1020 struct device_attribute
*attr
,
1021 const char *buf
, size_t count
)
1023 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
1024 unsigned long rate_hz
;
1027 ret
= kstrtoul(buf
, 0, &rate_hz
);
1031 for (i
= 0; i
< ARRAY_SIZE(lux_rates_hz
) - 1; i
++)
1032 if (rate_hz
>= lux_rates_hz
[i
])
1035 mutex_lock(&chip
->mutex
);
1036 chip
->lux_rate_index
= i
;
1037 ret
= bh1770_lux_rate(chip
, i
);
1038 mutex_unlock(&chip
->mutex
);
1046 static ssize_t
bh1770_get_lux_thresh_above(struct device
*dev
,
1047 struct device_attribute
*attr
, char *buf
)
1049 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
1050 return sprintf(buf
, "%d\n", chip
->lux_threshold_hi
);
1053 static ssize_t
bh1770_get_lux_thresh_below(struct device
*dev
,
1054 struct device_attribute
*attr
, char *buf
)
1056 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
1057 return sprintf(buf
, "%d\n", chip
->lux_threshold_lo
);
1060 static ssize_t
bh1770_set_lux_thresh(struct bh1770_chip
*chip
, u16
*target
,
1063 unsigned long thresh
;
1066 ret
= kstrtoul(buf
, 0, &thresh
);
1070 if (thresh
> BH1770_LUX_RANGE
)
1073 mutex_lock(&chip
->mutex
);
1076 * Don't update values in HW if we are still waiting for
1077 * first interrupt to come after device handle open call.
1079 if (!chip
->lux_wait_result
)
1080 ret
= bh1770_lux_update_thresholds(chip
,
1081 chip
->lux_threshold_hi
,
1082 chip
->lux_threshold_lo
);
1083 mutex_unlock(&chip
->mutex
);
1088 static ssize_t
bh1770_set_lux_thresh_above(struct device
*dev
,
1089 struct device_attribute
*attr
,
1090 const char *buf
, size_t len
)
1092 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
1093 int ret
= bh1770_set_lux_thresh(chip
, &chip
->lux_threshold_hi
, buf
);
1099 static ssize_t
bh1770_set_lux_thresh_below(struct device
*dev
,
1100 struct device_attribute
*attr
,
1101 const char *buf
, size_t len
)
1103 struct bh1770_chip
*chip
= dev_get_drvdata(dev
);
1104 int ret
= bh1770_set_lux_thresh(chip
, &chip
->lux_threshold_lo
, buf
);
1110 static DEVICE_ATTR(prox0_raw_en
, S_IRUGO
| S_IWUSR
, bh1770_prox_enable_show
,
1111 bh1770_prox_enable_store
);
1112 static DEVICE_ATTR(prox0_thresh_above1_value
, S_IRUGO
| S_IWUSR
,
1113 bh1770_prox_abs_thres_show
,
1114 bh1770_prox_abs_thres_store
);
1115 static DEVICE_ATTR(prox0_thresh_above0_value
, S_IRUGO
| S_IWUSR
,
1116 bh1770_get_prox_thres
,
1117 bh1770_set_prox_thres
);
1118 static DEVICE_ATTR(prox0_raw
, S_IRUGO
, bh1770_prox_result_show
, NULL
);
1119 static DEVICE_ATTR(prox0_sensor_range
, S_IRUGO
, bh1770_prox_range_show
, NULL
);
1120 static DEVICE_ATTR(prox0_thresh_above_count
, S_IRUGO
| S_IWUSR
,
1121 bh1770_prox_persistence_show
,
1122 bh1770_prox_persistence_store
);
1123 static DEVICE_ATTR(prox0_rate_above
, S_IRUGO
| S_IWUSR
,
1124 bh1770_get_prox_rate_above
,
1125 bh1770_set_prox_rate_above
);
1126 static DEVICE_ATTR(prox0_rate_below
, S_IRUGO
| S_IWUSR
,
1127 bh1770_get_prox_rate_below
,
1128 bh1770_set_prox_rate_below
);
1129 static DEVICE_ATTR(prox0_rate_avail
, S_IRUGO
, bh1770_get_prox_rate_avail
, NULL
);
1131 static DEVICE_ATTR(lux0_calibscale
, S_IRUGO
| S_IWUSR
, bh1770_lux_calib_show
,
1132 bh1770_lux_calib_store
);
1133 static DEVICE_ATTR(lux0_calibscale_default
, S_IRUGO
,
1134 bh1770_lux_calib_default_show
,
1136 static DEVICE_ATTR(lux0_input
, S_IRUGO
, bh1770_lux_result_show
, NULL
);
1137 static DEVICE_ATTR(lux0_sensor_range
, S_IRUGO
, bh1770_lux_range_show
, NULL
);
1138 static DEVICE_ATTR(lux0_rate
, S_IRUGO
| S_IWUSR
, bh1770_get_lux_rate
,
1139 bh1770_set_lux_rate
);
1140 static DEVICE_ATTR(lux0_rate_avail
, S_IRUGO
, bh1770_get_lux_rate_avail
, NULL
);
1141 static DEVICE_ATTR(lux0_thresh_above_value
, S_IRUGO
| S_IWUSR
,
1142 bh1770_get_lux_thresh_above
,
1143 bh1770_set_lux_thresh_above
);
1144 static DEVICE_ATTR(lux0_thresh_below_value
, S_IRUGO
| S_IWUSR
,
1145 bh1770_get_lux_thresh_below
,
1146 bh1770_set_lux_thresh_below
);
1147 static DEVICE_ATTR(chip_id
, S_IRUGO
, bh1770_chip_id_show
, NULL
);
1148 static DEVICE_ATTR(power_state
, S_IRUGO
| S_IWUSR
, bh1770_power_state_show
,
1149 bh1770_power_state_store
);
1152 static struct attribute
*sysfs_attrs
[] = {
1153 &dev_attr_lux0_calibscale
.attr
,
1154 &dev_attr_lux0_calibscale_default
.attr
,
1155 &dev_attr_lux0_input
.attr
,
1156 &dev_attr_lux0_sensor_range
.attr
,
1157 &dev_attr_lux0_rate
.attr
,
1158 &dev_attr_lux0_rate_avail
.attr
,
1159 &dev_attr_lux0_thresh_above_value
.attr
,
1160 &dev_attr_lux0_thresh_below_value
.attr
,
1161 &dev_attr_prox0_raw
.attr
,
1162 &dev_attr_prox0_sensor_range
.attr
,
1163 &dev_attr_prox0_raw_en
.attr
,
1164 &dev_attr_prox0_thresh_above_count
.attr
,
1165 &dev_attr_prox0_rate_above
.attr
,
1166 &dev_attr_prox0_rate_below
.attr
,
1167 &dev_attr_prox0_rate_avail
.attr
,
1168 &dev_attr_prox0_thresh_above0_value
.attr
,
1169 &dev_attr_prox0_thresh_above1_value
.attr
,
1170 &dev_attr_chip_id
.attr
,
1171 &dev_attr_power_state
.attr
,
1175 static const struct attribute_group bh1770_attribute_group
= {
1176 .attrs
= sysfs_attrs
1179 static int bh1770_probe(struct i2c_client
*client
,
1180 const struct i2c_device_id
*id
)
1182 struct bh1770_chip
*chip
;
1185 chip
= devm_kzalloc(&client
->dev
, sizeof *chip
, GFP_KERNEL
);
1189 i2c_set_clientdata(client
, chip
);
1190 chip
->client
= client
;
1192 mutex_init(&chip
->mutex
);
1193 init_waitqueue_head(&chip
->wait
);
1194 INIT_DELAYED_WORK(&chip
->prox_work
, bh1770_prox_work
);
1196 if (client
->dev
.platform_data
== NULL
) {
1197 dev_err(&client
->dev
, "platform data is mandatory\n");
1201 chip
->pdata
= client
->dev
.platform_data
;
1202 chip
->lux_calib
= BH1770_LUX_NEUTRAL_CALIB_VALUE
;
1203 chip
->lux_rate_index
= BH1770_LUX_DEFAULT_RATE
;
1204 chip
->lux_threshold_lo
= BH1770_LUX_DEF_THRES
;
1205 chip
->lux_threshold_hi
= BH1770_LUX_DEF_THRES
;
1207 if (chip
->pdata
->glass_attenuation
== 0)
1208 chip
->lux_ga
= BH1770_NEUTRAL_GA
;
1210 chip
->lux_ga
= chip
->pdata
->glass_attenuation
;
1212 chip
->prox_threshold
= BH1770_PROX_DEF_THRES
;
1213 chip
->prox_led
= chip
->pdata
->led_def_curr
;
1214 chip
->prox_abs_thres
= BH1770_PROX_DEF_ABS_THRES
;
1215 chip
->prox_persistence
= BH1770_DEFAULT_PERSISTENCE
;
1216 chip
->prox_rate_threshold
= BH1770_PROX_DEF_RATE_THRESH
;
1217 chip
->prox_rate
= BH1770_PROX_DEFAULT_RATE
;
1218 chip
->prox_data
= 0;
1220 chip
->regs
[0].supply
= reg_vcc
;
1221 chip
->regs
[1].supply
= reg_vleds
;
1223 err
= devm_regulator_bulk_get(&client
->dev
,
1224 ARRAY_SIZE(chip
->regs
), chip
->regs
);
1226 dev_err(&client
->dev
, "Cannot get regulators\n");
1230 err
= regulator_bulk_enable(ARRAY_SIZE(chip
->regs
),
1233 dev_err(&client
->dev
, "Cannot enable regulators\n");
1237 usleep_range(BH1770_STARTUP_DELAY
, BH1770_STARTUP_DELAY
* 2);
1238 err
= bh1770_detect(chip
);
1243 bh1770_chip_on(chip
);
1244 pm_runtime_set_active(&client
->dev
);
1245 pm_runtime_enable(&client
->dev
);
1247 chip
->lux_corr
= bh1770_get_corr_value(chip
);
1248 if (chip
->lux_corr
== 0) {
1249 dev_err(&client
->dev
, "Improper correction values\n");
1254 if (chip
->pdata
->setup_resources
) {
1255 err
= chip
->pdata
->setup_resources();
1262 err
= sysfs_create_group(&chip
->client
->dev
.kobj
,
1263 &bh1770_attribute_group
);
1265 dev_err(&chip
->client
->dev
, "Sysfs registration failed\n");
1270 * Chip needs level triggered interrupt to work. However,
1271 * level triggering doesn't work always correctly with power
1272 * management. Select both
1274 err
= request_threaded_irq(client
->irq
, NULL
,
1276 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
|
1280 dev_err(&client
->dev
, "could not get IRQ %d\n",
1284 regulator_bulk_disable(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1287 sysfs_remove_group(&chip
->client
->dev
.kobj
,
1288 &bh1770_attribute_group
);
1290 if (chip
->pdata
->release_resources
)
1291 chip
->pdata
->release_resources();
1293 regulator_bulk_disable(ARRAY_SIZE(chip
->regs
), chip
->regs
);
1297 static int bh1770_remove(struct i2c_client
*client
)
1299 struct bh1770_chip
*chip
= i2c_get_clientdata(client
);
1301 free_irq(client
->irq
, chip
);
1303 sysfs_remove_group(&chip
->client
->dev
.kobj
,
1304 &bh1770_attribute_group
);
1306 if (chip
->pdata
->release_resources
)
1307 chip
->pdata
->release_resources();
1309 cancel_delayed_work_sync(&chip
->prox_work
);
1311 if (!pm_runtime_suspended(&client
->dev
))
1312 bh1770_chip_off(chip
);
1314 pm_runtime_disable(&client
->dev
);
1315 pm_runtime_set_suspended(&client
->dev
);
1320 #ifdef CONFIG_PM_SLEEP
1321 static int bh1770_suspend(struct device
*dev
)
1323 struct i2c_client
*client
= to_i2c_client(dev
);
1324 struct bh1770_chip
*chip
= i2c_get_clientdata(client
);
1326 bh1770_chip_off(chip
);
1331 static int bh1770_resume(struct device
*dev
)
1333 struct i2c_client
*client
= to_i2c_client(dev
);
1334 struct bh1770_chip
*chip
= i2c_get_clientdata(client
);
1337 bh1770_chip_on(chip
);
1339 if (!pm_runtime_suspended(dev
)) {
1341 * If we were enabled at suspend time, it is expected
1342 * everything works nice and smoothly
1344 ret
= bh1770_lux_rate(chip
, chip
->lux_rate_index
);
1345 ret
|= bh1770_lux_interrupt_control(chip
, BH1770_ENABLE
);
1347 /* This causes interrupt after the next measurement cycle */
1348 bh1770_lux_update_thresholds(chip
, BH1770_LUX_DEF_THRES
,
1349 BH1770_LUX_DEF_THRES
);
1350 /* Inform that we are waiting for a result from ALS */
1351 chip
->lux_wait_result
= true;
1352 bh1770_prox_mode_control(chip
);
1359 static int bh1770_runtime_suspend(struct device
*dev
)
1361 struct i2c_client
*client
= to_i2c_client(dev
);
1362 struct bh1770_chip
*chip
= i2c_get_clientdata(client
);
1364 bh1770_chip_off(chip
);
1369 static int bh1770_runtime_resume(struct device
*dev
)
1371 struct i2c_client
*client
= to_i2c_client(dev
);
1372 struct bh1770_chip
*chip
= i2c_get_clientdata(client
);
1374 bh1770_chip_on(chip
);
1380 static const struct i2c_device_id bh1770_id
[] = {
1386 MODULE_DEVICE_TABLE(i2c
, bh1770_id
);
1388 static const struct dev_pm_ops bh1770_pm_ops
= {
1389 SET_SYSTEM_SLEEP_PM_OPS(bh1770_suspend
, bh1770_resume
)
1390 SET_RUNTIME_PM_OPS(bh1770_runtime_suspend
, bh1770_runtime_resume
, NULL
)
1393 static struct i2c_driver bh1770_driver
= {
1395 .name
= "bh1770glc",
1396 .pm
= &bh1770_pm_ops
,
1398 .probe
= bh1770_probe
,
1399 .remove
= bh1770_remove
,
1400 .id_table
= bh1770_id
,
1403 module_i2c_driver(bh1770_driver
);
1405 MODULE_DESCRIPTION("BH1770GLC / SFH7770 combined ALS and proximity sensor");
1406 MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1407 MODULE_LICENSE("GPL v2");