12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2190420abSSamu Onkalo /*
3190420abSSamu Onkalo * This file is part of the ROHM BH1770GLC / OSRAM SFH7770 sensor driver.
4190420abSSamu Onkalo * Chip is combined proximity and ambient light sensor.
5190420abSSamu Onkalo *
6190420abSSamu Onkalo * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7190420abSSamu Onkalo *
8190420abSSamu Onkalo * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
9190420abSSamu Onkalo */
10190420abSSamu Onkalo
11190420abSSamu Onkalo #include <linux/kernel.h>
12190420abSSamu Onkalo #include <linux/module.h>
13190420abSSamu Onkalo #include <linux/i2c.h>
14190420abSSamu Onkalo #include <linux/interrupt.h>
15190420abSSamu Onkalo #include <linux/mutex.h>
167ae5f10aSWolfram Sang #include <linux/platform_data/bh1770glc.h>
17190420abSSamu Onkalo #include <linux/regulator/consumer.h>
18190420abSSamu Onkalo #include <linux/pm_runtime.h>
19190420abSSamu Onkalo #include <linux/workqueue.h>
20190420abSSamu Onkalo #include <linux/delay.h>
21190420abSSamu Onkalo #include <linux/wait.h>
22190420abSSamu Onkalo #include <linux/slab.h>
23190420abSSamu Onkalo
24190420abSSamu Onkalo #define BH1770_ALS_CONTROL 0x80 /* ALS operation mode control */
25190420abSSamu Onkalo #define BH1770_PS_CONTROL 0x81 /* PS operation mode control */
26190420abSSamu Onkalo #define BH1770_I_LED 0x82 /* active LED and LED1, LED2 current */
27190420abSSamu Onkalo #define BH1770_I_LED3 0x83 /* LED3 current setting */
28190420abSSamu Onkalo #define BH1770_ALS_PS_MEAS 0x84 /* Forced mode trigger */
29190420abSSamu Onkalo #define BH1770_PS_MEAS_RATE 0x85 /* PS meas. rate at stand alone mode */
30190420abSSamu Onkalo #define BH1770_ALS_MEAS_RATE 0x86 /* ALS meas. rate at stand alone mode */
31190420abSSamu Onkalo #define BH1770_PART_ID 0x8a /* Part number and revision ID */
32190420abSSamu Onkalo #define BH1770_MANUFACT_ID 0x8b /* Manufacturerer ID */
33190420abSSamu Onkalo #define BH1770_ALS_DATA_0 0x8c /* ALS DATA low byte */
34190420abSSamu Onkalo #define BH1770_ALS_DATA_1 0x8d /* ALS DATA high byte */
35190420abSSamu Onkalo #define BH1770_ALS_PS_STATUS 0x8e /* Measurement data and int status */
36190420abSSamu Onkalo #define BH1770_PS_DATA_LED1 0x8f /* PS data from LED1 */
37190420abSSamu Onkalo #define BH1770_PS_DATA_LED2 0x90 /* PS data from LED2 */
38190420abSSamu Onkalo #define BH1770_PS_DATA_LED3 0x91 /* PS data from LED3 */
39190420abSSamu Onkalo #define BH1770_INTERRUPT 0x92 /* Interrupt setting */
40190420abSSamu Onkalo #define BH1770_PS_TH_LED1 0x93 /* PS interrupt threshold for LED1 */
41190420abSSamu Onkalo #define BH1770_PS_TH_LED2 0x94 /* PS interrupt threshold for LED2 */
42190420abSSamu Onkalo #define BH1770_PS_TH_LED3 0x95 /* PS interrupt threshold for LED3 */
43190420abSSamu Onkalo #define BH1770_ALS_TH_UP_0 0x96 /* ALS upper threshold low byte */
44190420abSSamu Onkalo #define BH1770_ALS_TH_UP_1 0x97 /* ALS upper threshold high byte */
45190420abSSamu Onkalo #define BH1770_ALS_TH_LOW_0 0x98 /* ALS lower threshold low byte */
46190420abSSamu Onkalo #define BH1770_ALS_TH_LOW_1 0x99 /* ALS lower threshold high byte */
47190420abSSamu Onkalo
48190420abSSamu Onkalo /* MANUFACT_ID */
49190420abSSamu Onkalo #define BH1770_MANUFACT_ROHM 0x01
50190420abSSamu Onkalo #define BH1770_MANUFACT_OSRAM 0x03
51190420abSSamu Onkalo
52190420abSSamu Onkalo /* PART_ID */
53190420abSSamu Onkalo #define BH1770_PART 0x90
54190420abSSamu Onkalo #define BH1770_PART_MASK 0xf0
55190420abSSamu Onkalo #define BH1770_REV_MASK 0x0f
56190420abSSamu Onkalo #define BH1770_REV_SHIFT 0
57190420abSSamu Onkalo #define BH1770_REV_0 0x00
58190420abSSamu Onkalo #define BH1770_REV_1 0x01
59190420abSSamu Onkalo
60190420abSSamu Onkalo /* Operating modes for both */
61190420abSSamu Onkalo #define BH1770_STANDBY 0x00
62190420abSSamu Onkalo #define BH1770_FORCED 0x02
63190420abSSamu Onkalo #define BH1770_STANDALONE 0x03
64190420abSSamu Onkalo #define BH1770_SWRESET (0x01 << 2)
65190420abSSamu Onkalo
66190420abSSamu Onkalo #define BH1770_PS_TRIG_MEAS (1 << 0)
67190420abSSamu Onkalo #define BH1770_ALS_TRIG_MEAS (1 << 1)
68190420abSSamu Onkalo
69190420abSSamu Onkalo /* Interrupt control */
70190420abSSamu Onkalo #define BH1770_INT_OUTPUT_MODE (1 << 3) /* 0 = latched */
71190420abSSamu Onkalo #define BH1770_INT_POLARITY (1 << 2) /* 1 = active high */
72190420abSSamu Onkalo #define BH1770_INT_ALS_ENA (1 << 1)
73190420abSSamu Onkalo #define BH1770_INT_PS_ENA (1 << 0)
74190420abSSamu Onkalo
75190420abSSamu Onkalo /* Interrupt status */
76190420abSSamu Onkalo #define BH1770_INT_LED1_DATA (1 << 0)
77190420abSSamu Onkalo #define BH1770_INT_LED1_INT (1 << 1)
78190420abSSamu Onkalo #define BH1770_INT_LED2_DATA (1 << 2)
79190420abSSamu Onkalo #define BH1770_INT_LED2_INT (1 << 3)
80190420abSSamu Onkalo #define BH1770_INT_LED3_DATA (1 << 4)
81190420abSSamu Onkalo #define BH1770_INT_LED3_INT (1 << 5)
82190420abSSamu Onkalo #define BH1770_INT_LEDS_INT ((1 << 1) | (1 << 3) | (1 << 5))
83190420abSSamu Onkalo #define BH1770_INT_ALS_DATA (1 << 6)
84190420abSSamu Onkalo #define BH1770_INT_ALS_INT (1 << 7)
85190420abSSamu Onkalo
86190420abSSamu Onkalo /* Led channels */
87190420abSSamu Onkalo #define BH1770_LED1 0x00
88190420abSSamu Onkalo
89190420abSSamu Onkalo #define BH1770_DISABLE 0
90190420abSSamu Onkalo #define BH1770_ENABLE 1
91190420abSSamu Onkalo #define BH1770_PROX_CHANNELS 1
92190420abSSamu Onkalo
93190420abSSamu Onkalo #define BH1770_LUX_DEFAULT_RATE 1 /* Index to lux rate table */
94190420abSSamu Onkalo #define BH1770_PROX_DEFAULT_RATE 1 /* Direct HW value =~ 50Hz */
95190420abSSamu Onkalo #define BH1770_PROX_DEF_RATE_THRESH 6 /* Direct HW value =~ 5 Hz */
96190420abSSamu Onkalo #define BH1770_STARTUP_DELAY 50
97190420abSSamu Onkalo #define BH1770_RESET_TIME 10
98190420abSSamu Onkalo #define BH1770_TIMEOUT 2100 /* Timeout in 2.1 seconds */
99190420abSSamu Onkalo
100190420abSSamu Onkalo #define BH1770_LUX_RANGE 65535
101190420abSSamu Onkalo #define BH1770_PROX_RANGE 255
102190420abSSamu Onkalo #define BH1770_COEF_SCALER 1024
103190420abSSamu Onkalo #define BH1770_CALIB_SCALER 8192
104190420abSSamu Onkalo #define BH1770_LUX_NEUTRAL_CALIB_VALUE (1 * BH1770_CALIB_SCALER)
105190420abSSamu Onkalo #define BH1770_LUX_DEF_THRES 1000
106190420abSSamu Onkalo #define BH1770_PROX_DEF_THRES 70
107190420abSSamu Onkalo #define BH1770_PROX_DEF_ABS_THRES 100
108190420abSSamu Onkalo #define BH1770_DEFAULT_PERSISTENCE 10
109190420abSSamu Onkalo #define BH1770_PROX_MAX_PERSISTENCE 50
110190420abSSamu Onkalo #define BH1770_LUX_GA_SCALE 16384
111190420abSSamu Onkalo #define BH1770_LUX_CF_SCALE 2048 /* CF ChipFactor */
112190420abSSamu Onkalo #define BH1770_NEUTRAL_CF BH1770_LUX_CF_SCALE
113190420abSSamu Onkalo #define BH1770_LUX_CORR_SCALE 4096
114190420abSSamu Onkalo
115190420abSSamu Onkalo #define PROX_ABOVE_THRESHOLD 1
116190420abSSamu Onkalo #define PROX_BELOW_THRESHOLD 0
117190420abSSamu Onkalo
118190420abSSamu Onkalo #define PROX_IGNORE_LUX_LIMIT 500
119190420abSSamu Onkalo
120190420abSSamu Onkalo struct bh1770_chip {
121190420abSSamu Onkalo struct bh1770_platform_data *pdata;
122190420abSSamu Onkalo char chipname[10];
123190420abSSamu Onkalo u8 revision;
124190420abSSamu Onkalo struct i2c_client *client;
125190420abSSamu Onkalo struct regulator_bulk_data regs[2];
126190420abSSamu Onkalo struct mutex mutex; /* avoid parallel access */
127190420abSSamu Onkalo wait_queue_head_t wait;
128190420abSSamu Onkalo
129190420abSSamu Onkalo bool int_mode_prox;
130190420abSSamu Onkalo bool int_mode_lux;
131190420abSSamu Onkalo struct delayed_work prox_work;
132190420abSSamu Onkalo u32 lux_cf; /* Chip specific factor */
133190420abSSamu Onkalo u32 lux_ga;
134190420abSSamu Onkalo u32 lux_calib;
135190420abSSamu Onkalo int lux_rate_index;
136190420abSSamu Onkalo u32 lux_corr;
137190420abSSamu Onkalo u16 lux_data_raw;
138190420abSSamu Onkalo u16 lux_threshold_hi;
139190420abSSamu Onkalo u16 lux_threshold_lo;
140190420abSSamu Onkalo u16 lux_thres_hi_onchip;
141190420abSSamu Onkalo u16 lux_thres_lo_onchip;
142190420abSSamu Onkalo bool lux_wait_result;
143190420abSSamu Onkalo
144190420abSSamu Onkalo int prox_enable_count;
145190420abSSamu Onkalo u16 prox_coef;
146190420abSSamu Onkalo u16 prox_const;
147190420abSSamu Onkalo int prox_rate;
148190420abSSamu Onkalo int prox_rate_threshold;
149190420abSSamu Onkalo u8 prox_persistence;
150190420abSSamu Onkalo u8 prox_persistence_counter;
151190420abSSamu Onkalo u8 prox_data;
152190420abSSamu Onkalo u8 prox_threshold;
153190420abSSamu Onkalo u8 prox_threshold_hw;
154190420abSSamu Onkalo bool prox_force_update;
155190420abSSamu Onkalo u8 prox_abs_thres;
156190420abSSamu Onkalo u8 prox_led;
157190420abSSamu Onkalo };
158190420abSSamu Onkalo
159190420abSSamu Onkalo static const char reg_vcc[] = "Vcc";
160190420abSSamu Onkalo static const char reg_vleds[] = "Vleds";
161190420abSSamu Onkalo
162190420abSSamu Onkalo /*
163190420abSSamu Onkalo * Supported stand alone rates in ms from chip data sheet
164190420abSSamu Onkalo * {10, 20, 30, 40, 70, 100, 200, 500, 1000, 2000};
165190420abSSamu Onkalo */
166190420abSSamu Onkalo static const s16 prox_rates_hz[] = {100, 50, 33, 25, 14, 10, 5, 2};
167190420abSSamu Onkalo static const s16 prox_rates_ms[] = {10, 20, 30, 40, 70, 100, 200, 500};
168190420abSSamu Onkalo
169190420abSSamu Onkalo /*
170190420abSSamu Onkalo * Supported stand alone rates in ms from chip data sheet
171190420abSSamu Onkalo * {100, 200, 500, 1000, 2000};
172190420abSSamu Onkalo */
173190420abSSamu Onkalo static const s16 lux_rates_hz[] = {10, 5, 2, 1, 0};
174190420abSSamu Onkalo
175190420abSSamu Onkalo /*
176190420abSSamu Onkalo * interrupt control functions are called while keeping chip->mutex
177190420abSSamu Onkalo * excluding module probe / remove
178190420abSSamu Onkalo */
bh1770_lux_interrupt_control(struct bh1770_chip * chip,int lux)179190420abSSamu Onkalo static inline int bh1770_lux_interrupt_control(struct bh1770_chip *chip,
180190420abSSamu Onkalo int lux)
181190420abSSamu Onkalo {
182190420abSSamu Onkalo chip->int_mode_lux = lux;
183190420abSSamu Onkalo /* Set interrupt modes, interrupt active low, latched */
184190420abSSamu Onkalo return i2c_smbus_write_byte_data(chip->client,
185190420abSSamu Onkalo BH1770_INTERRUPT,
186190420abSSamu Onkalo (lux << 1) | chip->int_mode_prox);
187190420abSSamu Onkalo }
188190420abSSamu Onkalo
bh1770_prox_interrupt_control(struct bh1770_chip * chip,int ps)189190420abSSamu Onkalo static inline int bh1770_prox_interrupt_control(struct bh1770_chip *chip,
190190420abSSamu Onkalo int ps)
191190420abSSamu Onkalo {
192190420abSSamu Onkalo chip->int_mode_prox = ps;
193190420abSSamu Onkalo return i2c_smbus_write_byte_data(chip->client,
194190420abSSamu Onkalo BH1770_INTERRUPT,
195190420abSSamu Onkalo (chip->int_mode_lux << 1) | (ps << 0));
196190420abSSamu Onkalo }
197190420abSSamu Onkalo
198190420abSSamu Onkalo /* chip->mutex is always kept here */
bh1770_lux_rate(struct bh1770_chip * chip,int rate_index)199190420abSSamu Onkalo static int bh1770_lux_rate(struct bh1770_chip *chip, int rate_index)
200190420abSSamu Onkalo {
201190420abSSamu Onkalo /* sysfs may call this when the chip is powered off */
202190420abSSamu Onkalo if (pm_runtime_suspended(&chip->client->dev))
203190420abSSamu Onkalo return 0;
204190420abSSamu Onkalo
205190420abSSamu Onkalo /* Proper proximity response needs fastest lux rate (100ms) */
206190420abSSamu Onkalo if (chip->prox_enable_count)
207190420abSSamu Onkalo rate_index = 0;
208190420abSSamu Onkalo
209190420abSSamu Onkalo return i2c_smbus_write_byte_data(chip->client,
210190420abSSamu Onkalo BH1770_ALS_MEAS_RATE,
211190420abSSamu Onkalo rate_index);
212190420abSSamu Onkalo }
213190420abSSamu Onkalo
bh1770_prox_rate(struct bh1770_chip * chip,int mode)214190420abSSamu Onkalo static int bh1770_prox_rate(struct bh1770_chip *chip, int mode)
215190420abSSamu Onkalo {
216190420abSSamu Onkalo int rate;
217190420abSSamu Onkalo
218190420abSSamu Onkalo rate = (mode == PROX_ABOVE_THRESHOLD) ?
219190420abSSamu Onkalo chip->prox_rate_threshold : chip->prox_rate;
220190420abSSamu Onkalo
221190420abSSamu Onkalo return i2c_smbus_write_byte_data(chip->client,
222190420abSSamu Onkalo BH1770_PS_MEAS_RATE,
223190420abSSamu Onkalo rate);
224190420abSSamu Onkalo }
225190420abSSamu Onkalo
226190420abSSamu Onkalo /* InfraredLED is controlled by the chip during proximity scanning */
bh1770_led_cfg(struct bh1770_chip * chip)227190420abSSamu Onkalo static inline int bh1770_led_cfg(struct bh1770_chip *chip)
228190420abSSamu Onkalo {
229190420abSSamu Onkalo /* LED cfg, current for leds 1 and 2 */
230190420abSSamu Onkalo return i2c_smbus_write_byte_data(chip->client,
231190420abSSamu Onkalo BH1770_I_LED,
232190420abSSamu Onkalo (BH1770_LED1 << 6) |
233190420abSSamu Onkalo (BH1770_LED_5mA << 3) |
234190420abSSamu Onkalo chip->prox_led);
235190420abSSamu Onkalo }
236190420abSSamu Onkalo
237190420abSSamu Onkalo /*
238190420abSSamu Onkalo * Following two functions converts raw ps values from HW to normalized
239190420abSSamu Onkalo * values. Purpose is to compensate differences between different sensor
240190420abSSamu Onkalo * versions and variants so that result means about the same between
241190420abSSamu Onkalo * versions.
242190420abSSamu Onkalo */
bh1770_psraw_to_adjusted(struct bh1770_chip * chip,u8 psraw)243190420abSSamu Onkalo static inline u8 bh1770_psraw_to_adjusted(struct bh1770_chip *chip, u8 psraw)
244190420abSSamu Onkalo {
245190420abSSamu Onkalo u16 adjusted;
246190420abSSamu Onkalo adjusted = (u16)(((u32)(psraw + chip->prox_const) * chip->prox_coef) /
247190420abSSamu Onkalo BH1770_COEF_SCALER);
248190420abSSamu Onkalo if (adjusted > BH1770_PROX_RANGE)
249190420abSSamu Onkalo adjusted = BH1770_PROX_RANGE;
250190420abSSamu Onkalo return adjusted;
251190420abSSamu Onkalo }
252190420abSSamu Onkalo
bh1770_psadjusted_to_raw(struct bh1770_chip * chip,u8 ps)253190420abSSamu Onkalo static inline u8 bh1770_psadjusted_to_raw(struct bh1770_chip *chip, u8 ps)
254190420abSSamu Onkalo {
255190420abSSamu Onkalo u16 raw;
256190420abSSamu Onkalo
257190420abSSamu Onkalo raw = (((u32)ps * BH1770_COEF_SCALER) / chip->prox_coef);
258190420abSSamu Onkalo if (raw > chip->prox_const)
259190420abSSamu Onkalo raw = raw - chip->prox_const;
260190420abSSamu Onkalo else
261190420abSSamu Onkalo raw = 0;
262190420abSSamu Onkalo return raw;
263190420abSSamu Onkalo }
264190420abSSamu Onkalo
265190420abSSamu Onkalo /*
266190420abSSamu Onkalo * Following two functions converts raw lux values from HW to normalized
267190420abSSamu Onkalo * values. Purpose is to compensate differences between different sensor
268190420abSSamu Onkalo * versions and variants so that result means about the same between
269190420abSSamu Onkalo * versions. Chip->mutex is kept when this is called.
270190420abSSamu Onkalo */
bh1770_prox_set_threshold(struct bh1770_chip * chip)271190420abSSamu Onkalo static int bh1770_prox_set_threshold(struct bh1770_chip *chip)
272190420abSSamu Onkalo {
273190420abSSamu Onkalo u8 tmp = 0;
274190420abSSamu Onkalo
275190420abSSamu Onkalo /* sysfs may call this when the chip is powered off */
276190420abSSamu Onkalo if (pm_runtime_suspended(&chip->client->dev))
277190420abSSamu Onkalo return 0;
278190420abSSamu Onkalo
279190420abSSamu Onkalo tmp = bh1770_psadjusted_to_raw(chip, chip->prox_threshold);
280190420abSSamu Onkalo chip->prox_threshold_hw = tmp;
281190420abSSamu Onkalo
282190420abSSamu Onkalo return i2c_smbus_write_byte_data(chip->client, BH1770_PS_TH_LED1,
283190420abSSamu Onkalo tmp);
284190420abSSamu Onkalo }
285190420abSSamu Onkalo
bh1770_lux_raw_to_adjusted(struct bh1770_chip * chip,u16 raw)286190420abSSamu Onkalo static inline u16 bh1770_lux_raw_to_adjusted(struct bh1770_chip *chip, u16 raw)
287190420abSSamu Onkalo {
288190420abSSamu Onkalo u32 lux;
289190420abSSamu Onkalo lux = ((u32)raw * chip->lux_corr) / BH1770_LUX_CORR_SCALE;
290190420abSSamu Onkalo return min(lux, (u32)BH1770_LUX_RANGE);
291190420abSSamu Onkalo }
292190420abSSamu Onkalo
bh1770_lux_adjusted_to_raw(struct bh1770_chip * chip,u16 adjusted)293190420abSSamu Onkalo static inline u16 bh1770_lux_adjusted_to_raw(struct bh1770_chip *chip,
294190420abSSamu Onkalo u16 adjusted)
295190420abSSamu Onkalo {
296190420abSSamu Onkalo return (u32)adjusted * BH1770_LUX_CORR_SCALE / chip->lux_corr;
297190420abSSamu Onkalo }
298190420abSSamu Onkalo
299190420abSSamu Onkalo /* chip->mutex is kept when this is called */
bh1770_lux_update_thresholds(struct bh1770_chip * chip,u16 threshold_hi,u16 threshold_lo)300190420abSSamu Onkalo static int bh1770_lux_update_thresholds(struct bh1770_chip *chip,
301190420abSSamu Onkalo u16 threshold_hi, u16 threshold_lo)
302190420abSSamu Onkalo {
303190420abSSamu Onkalo u8 data[4];
304190420abSSamu Onkalo int ret;
305190420abSSamu Onkalo
306190420abSSamu Onkalo /* sysfs may call this when the chip is powered off */
307190420abSSamu Onkalo if (pm_runtime_suspended(&chip->client->dev))
308190420abSSamu Onkalo return 0;
309190420abSSamu Onkalo
310190420abSSamu Onkalo /*
311190420abSSamu Onkalo * Compensate threshold values with the correction factors if not
312190420abSSamu Onkalo * set to minimum or maximum.
313190420abSSamu Onkalo * Min & max values disables interrupts.
314190420abSSamu Onkalo */
315190420abSSamu Onkalo if (threshold_hi != BH1770_LUX_RANGE && threshold_hi != 0)
316190420abSSamu Onkalo threshold_hi = bh1770_lux_adjusted_to_raw(chip, threshold_hi);
317190420abSSamu Onkalo
318190420abSSamu Onkalo if (threshold_lo != BH1770_LUX_RANGE && threshold_lo != 0)
319190420abSSamu Onkalo threshold_lo = bh1770_lux_adjusted_to_raw(chip, threshold_lo);
320190420abSSamu Onkalo
321190420abSSamu Onkalo if (chip->lux_thres_hi_onchip == threshold_hi &&
322190420abSSamu Onkalo chip->lux_thres_lo_onchip == threshold_lo)
323190420abSSamu Onkalo return 0;
324190420abSSamu Onkalo
325190420abSSamu Onkalo chip->lux_thres_hi_onchip = threshold_hi;
326190420abSSamu Onkalo chip->lux_thres_lo_onchip = threshold_lo;
327190420abSSamu Onkalo
328190420abSSamu Onkalo data[0] = threshold_hi;
329190420abSSamu Onkalo data[1] = threshold_hi >> 8;
330190420abSSamu Onkalo data[2] = threshold_lo;
331190420abSSamu Onkalo data[3] = threshold_lo >> 8;
332190420abSSamu Onkalo
333190420abSSamu Onkalo ret = i2c_smbus_write_i2c_block_data(chip->client,
334190420abSSamu Onkalo BH1770_ALS_TH_UP_0,
335190420abSSamu Onkalo ARRAY_SIZE(data),
336190420abSSamu Onkalo data);
337190420abSSamu Onkalo return ret;
338190420abSSamu Onkalo }
339190420abSSamu Onkalo
bh1770_lux_get_result(struct bh1770_chip * chip)340190420abSSamu Onkalo static int bh1770_lux_get_result(struct bh1770_chip *chip)
341190420abSSamu Onkalo {
342190420abSSamu Onkalo u16 data;
343190420abSSamu Onkalo int ret;
344190420abSSamu Onkalo
345190420abSSamu Onkalo ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_0);
346190420abSSamu Onkalo if (ret < 0)
347190420abSSamu Onkalo return ret;
348190420abSSamu Onkalo
349190420abSSamu Onkalo data = ret & 0xff;
350190420abSSamu Onkalo ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_1);
351190420abSSamu Onkalo if (ret < 0)
352190420abSSamu Onkalo return ret;
353190420abSSamu Onkalo
354190420abSSamu Onkalo chip->lux_data_raw = data | ((ret & 0xff) << 8);
355190420abSSamu Onkalo
356190420abSSamu Onkalo return 0;
357190420abSSamu Onkalo }
358190420abSSamu Onkalo
359190420abSSamu Onkalo /* Calculate correction value which contains chip and device specific parts */
bh1770_get_corr_value(struct bh1770_chip * chip)360190420abSSamu Onkalo static u32 bh1770_get_corr_value(struct bh1770_chip *chip)
361190420abSSamu Onkalo {
362190420abSSamu Onkalo u32 tmp;
363190420abSSamu Onkalo /* Impact of glass attenuation correction */
364190420abSSamu Onkalo tmp = (BH1770_LUX_CORR_SCALE * chip->lux_ga) / BH1770_LUX_GA_SCALE;
365190420abSSamu Onkalo /* Impact of chip factor correction */
366190420abSSamu Onkalo tmp = (tmp * chip->lux_cf) / BH1770_LUX_CF_SCALE;
367190420abSSamu Onkalo /* Impact of Device specific calibration correction */
368190420abSSamu Onkalo tmp = (tmp * chip->lux_calib) / BH1770_CALIB_SCALER;
369190420abSSamu Onkalo return tmp;
370190420abSSamu Onkalo }
371190420abSSamu Onkalo
bh1770_lux_read_result(struct bh1770_chip * chip)372190420abSSamu Onkalo static int bh1770_lux_read_result(struct bh1770_chip *chip)
373190420abSSamu Onkalo {
374190420abSSamu Onkalo bh1770_lux_get_result(chip);
375190420abSSamu Onkalo return bh1770_lux_raw_to_adjusted(chip, chip->lux_data_raw);
376190420abSSamu Onkalo }
377190420abSSamu Onkalo
378190420abSSamu Onkalo /*
379190420abSSamu Onkalo * Chip on / off functions are called while keeping mutex except probe
380190420abSSamu Onkalo * or remove phase
381190420abSSamu Onkalo */
bh1770_chip_on(struct bh1770_chip * chip)382190420abSSamu Onkalo static int bh1770_chip_on(struct bh1770_chip *chip)
383190420abSSamu Onkalo {
384190420abSSamu Onkalo int ret = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
385190420abSSamu Onkalo chip->regs);
386190420abSSamu Onkalo if (ret < 0)
387190420abSSamu Onkalo return ret;
388190420abSSamu Onkalo
389190420abSSamu Onkalo usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
390190420abSSamu Onkalo
391190420abSSamu Onkalo /* Reset the chip */
392190420abSSamu Onkalo i2c_smbus_write_byte_data(chip->client, BH1770_ALS_CONTROL,
393190420abSSamu Onkalo BH1770_SWRESET);
394190420abSSamu Onkalo usleep_range(BH1770_RESET_TIME, BH1770_RESET_TIME * 2);
395190420abSSamu Onkalo
396190420abSSamu Onkalo /*
397190420abSSamu Onkalo * ALS is started always since proximity needs als results
398190420abSSamu Onkalo * for realibility estimation.
399190420abSSamu Onkalo * Let's assume dark until the first ALS measurement is ready.
400190420abSSamu Onkalo */
401190420abSSamu Onkalo chip->lux_data_raw = 0;
402190420abSSamu Onkalo chip->prox_data = 0;
403190420abSSamu Onkalo ret = i2c_smbus_write_byte_data(chip->client,
404190420abSSamu Onkalo BH1770_ALS_CONTROL, BH1770_STANDALONE);
405190420abSSamu Onkalo
406190420abSSamu Onkalo /* Assume reset defaults */
407190420abSSamu Onkalo chip->lux_thres_hi_onchip = BH1770_LUX_RANGE;
408190420abSSamu Onkalo chip->lux_thres_lo_onchip = 0;
409190420abSSamu Onkalo
410190420abSSamu Onkalo return ret;
411190420abSSamu Onkalo }
412190420abSSamu Onkalo
bh1770_chip_off(struct bh1770_chip * chip)413190420abSSamu Onkalo static void bh1770_chip_off(struct bh1770_chip *chip)
414190420abSSamu Onkalo {
415190420abSSamu Onkalo i2c_smbus_write_byte_data(chip->client,
416190420abSSamu Onkalo BH1770_INTERRUPT, BH1770_DISABLE);
417190420abSSamu Onkalo i2c_smbus_write_byte_data(chip->client,
418190420abSSamu Onkalo BH1770_ALS_CONTROL, BH1770_STANDBY);
419190420abSSamu Onkalo i2c_smbus_write_byte_data(chip->client,
420190420abSSamu Onkalo BH1770_PS_CONTROL, BH1770_STANDBY);
421190420abSSamu Onkalo regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
422190420abSSamu Onkalo }
423190420abSSamu Onkalo
424190420abSSamu Onkalo /* chip->mutex is kept when this is called */
bh1770_prox_mode_control(struct bh1770_chip * chip)425190420abSSamu Onkalo static int bh1770_prox_mode_control(struct bh1770_chip *chip)
426190420abSSamu Onkalo {
427190420abSSamu Onkalo if (chip->prox_enable_count) {
428190420abSSamu Onkalo chip->prox_force_update = true; /* Force immediate update */
429190420abSSamu Onkalo
430190420abSSamu Onkalo bh1770_lux_rate(chip, chip->lux_rate_index);
431190420abSSamu Onkalo bh1770_prox_set_threshold(chip);
432190420abSSamu Onkalo bh1770_led_cfg(chip);
433190420abSSamu Onkalo bh1770_prox_rate(chip, PROX_BELOW_THRESHOLD);
434190420abSSamu Onkalo bh1770_prox_interrupt_control(chip, BH1770_ENABLE);
435190420abSSamu Onkalo i2c_smbus_write_byte_data(chip->client,
436190420abSSamu Onkalo BH1770_PS_CONTROL, BH1770_STANDALONE);
437190420abSSamu Onkalo } else {
438190420abSSamu Onkalo chip->prox_data = 0;
439190420abSSamu Onkalo bh1770_lux_rate(chip, chip->lux_rate_index);
440190420abSSamu Onkalo bh1770_prox_interrupt_control(chip, BH1770_DISABLE);
441190420abSSamu Onkalo i2c_smbus_write_byte_data(chip->client,
442190420abSSamu Onkalo BH1770_PS_CONTROL, BH1770_STANDBY);
443190420abSSamu Onkalo }
444190420abSSamu Onkalo return 0;
445190420abSSamu Onkalo }
446190420abSSamu Onkalo
447190420abSSamu Onkalo /* chip->mutex is kept when this is called */
bh1770_prox_read_result(struct bh1770_chip * chip)448190420abSSamu Onkalo static int bh1770_prox_read_result(struct bh1770_chip *chip)
449190420abSSamu Onkalo {
450190420abSSamu Onkalo int ret;
451190420abSSamu Onkalo bool above;
452190420abSSamu Onkalo u8 mode;
453190420abSSamu Onkalo
454190420abSSamu Onkalo ret = i2c_smbus_read_byte_data(chip->client, BH1770_PS_DATA_LED1);
455190420abSSamu Onkalo if (ret < 0)
456190420abSSamu Onkalo goto out;
457190420abSSamu Onkalo
458190420abSSamu Onkalo if (ret > chip->prox_threshold_hw)
459190420abSSamu Onkalo above = true;
460190420abSSamu Onkalo else
461190420abSSamu Onkalo above = false;
462190420abSSamu Onkalo
463190420abSSamu Onkalo /*
464190420abSSamu Onkalo * when ALS levels goes above limit, proximity result may be
465190420abSSamu Onkalo * false proximity. Thus ignore the result. With real proximity
466190420abSSamu Onkalo * there is a shadow causing low als levels.
467190420abSSamu Onkalo */
468190420abSSamu Onkalo if (chip->lux_data_raw > PROX_IGNORE_LUX_LIMIT)
469190420abSSamu Onkalo ret = 0;
470190420abSSamu Onkalo
471190420abSSamu Onkalo chip->prox_data = bh1770_psraw_to_adjusted(chip, ret);
472190420abSSamu Onkalo
473190420abSSamu Onkalo /* Strong proximity level or force mode requires immediate response */
474190420abSSamu Onkalo if (chip->prox_data >= chip->prox_abs_thres ||
475190420abSSamu Onkalo chip->prox_force_update)
476190420abSSamu Onkalo chip->prox_persistence_counter = chip->prox_persistence;
477190420abSSamu Onkalo
478190420abSSamu Onkalo chip->prox_force_update = false;
479190420abSSamu Onkalo
480190420abSSamu Onkalo /* Persistence filttering to reduce false proximity events */
481190420abSSamu Onkalo if (likely(above)) {
482190420abSSamu Onkalo if (chip->prox_persistence_counter < chip->prox_persistence) {
483190420abSSamu Onkalo chip->prox_persistence_counter++;
484190420abSSamu Onkalo ret = -ENODATA;
485190420abSSamu Onkalo } else {
486190420abSSamu Onkalo mode = PROX_ABOVE_THRESHOLD;
487190420abSSamu Onkalo ret = 0;
488190420abSSamu Onkalo }
489190420abSSamu Onkalo } else {
490190420abSSamu Onkalo chip->prox_persistence_counter = 0;
491190420abSSamu Onkalo mode = PROX_BELOW_THRESHOLD;
492190420abSSamu Onkalo chip->prox_data = 0;
493190420abSSamu Onkalo ret = 0;
494190420abSSamu Onkalo }
495190420abSSamu Onkalo
496190420abSSamu Onkalo /* Set proximity detection rate based on above or below value */
497190420abSSamu Onkalo if (ret == 0) {
498190420abSSamu Onkalo bh1770_prox_rate(chip, mode);
499190420abSSamu Onkalo sysfs_notify(&chip->client->dev.kobj, NULL, "prox0_raw");
500190420abSSamu Onkalo }
501190420abSSamu Onkalo out:
502190420abSSamu Onkalo return ret;
503190420abSSamu Onkalo }
504190420abSSamu Onkalo
bh1770_detect(struct bh1770_chip * chip)505190420abSSamu Onkalo static int bh1770_detect(struct bh1770_chip *chip)
506190420abSSamu Onkalo {
507190420abSSamu Onkalo struct i2c_client *client = chip->client;
508190420abSSamu Onkalo s32 ret;
509190420abSSamu Onkalo u8 manu, part;
510190420abSSamu Onkalo
511190420abSSamu Onkalo ret = i2c_smbus_read_byte_data(client, BH1770_MANUFACT_ID);
512190420abSSamu Onkalo if (ret < 0)
513190420abSSamu Onkalo goto error;
514190420abSSamu Onkalo manu = (u8)ret;
515190420abSSamu Onkalo
516190420abSSamu Onkalo ret = i2c_smbus_read_byte_data(client, BH1770_PART_ID);
517190420abSSamu Onkalo if (ret < 0)
518190420abSSamu Onkalo goto error;
519190420abSSamu Onkalo part = (u8)ret;
520190420abSSamu Onkalo
521190420abSSamu Onkalo chip->revision = (part & BH1770_REV_MASK) >> BH1770_REV_SHIFT;
522190420abSSamu Onkalo chip->prox_coef = BH1770_COEF_SCALER;
523190420abSSamu Onkalo chip->prox_const = 0;
524190420abSSamu Onkalo chip->lux_cf = BH1770_NEUTRAL_CF;
525190420abSSamu Onkalo
526190420abSSamu Onkalo if ((manu == BH1770_MANUFACT_ROHM) &&
527190420abSSamu Onkalo ((part & BH1770_PART_MASK) == BH1770_PART)) {
528190420abSSamu Onkalo snprintf(chip->chipname, sizeof(chip->chipname), "BH1770GLC");
529190420abSSamu Onkalo return 0;
530190420abSSamu Onkalo }
531190420abSSamu Onkalo
532190420abSSamu Onkalo if ((manu == BH1770_MANUFACT_OSRAM) &&
533190420abSSamu Onkalo ((part & BH1770_PART_MASK) == BH1770_PART)) {
534190420abSSamu Onkalo snprintf(chip->chipname, sizeof(chip->chipname), "SFH7770");
535190420abSSamu Onkalo /* Values selected by comparing different versions */
536190420abSSamu Onkalo chip->prox_coef = 819; /* 0.8 * BH1770_COEF_SCALER */
537190420abSSamu Onkalo chip->prox_const = 40;
538190420abSSamu Onkalo return 0;
539190420abSSamu Onkalo }
540190420abSSamu Onkalo
541190420abSSamu Onkalo ret = -ENODEV;
542190420abSSamu Onkalo error:
543190420abSSamu Onkalo dev_dbg(&client->dev, "BH1770 or SFH7770 not found\n");
544190420abSSamu Onkalo
545190420abSSamu Onkalo return ret;
546190420abSSamu Onkalo }
547190420abSSamu Onkalo
548190420abSSamu Onkalo /*
549190420abSSamu Onkalo * This work is re-scheduled at every proximity interrupt.
550190420abSSamu Onkalo * If this work is running, it means that there hasn't been any
551190420abSSamu Onkalo * proximity interrupt in time. Situation is handled as no-proximity.
552190420abSSamu Onkalo * It would be nice to have low-threshold interrupt or interrupt
553190420abSSamu Onkalo * when measurement and hi-threshold are both 0. But neither of those exists.
554190420abSSamu Onkalo * This is a workaroud for missing HW feature.
555190420abSSamu Onkalo */
556190420abSSamu Onkalo
bh1770_prox_work(struct work_struct * work)557190420abSSamu Onkalo static void bh1770_prox_work(struct work_struct *work)
558190420abSSamu Onkalo {
559190420abSSamu Onkalo struct bh1770_chip *chip =
560190420abSSamu Onkalo container_of(work, struct bh1770_chip, prox_work.work);
561190420abSSamu Onkalo
562190420abSSamu Onkalo mutex_lock(&chip->mutex);
563190420abSSamu Onkalo bh1770_prox_read_result(chip);
564190420abSSamu Onkalo mutex_unlock(&chip->mutex);
565190420abSSamu Onkalo }
566190420abSSamu Onkalo
567190420abSSamu Onkalo /* This is threaded irq handler */
bh1770_irq(int irq,void * data)568190420abSSamu Onkalo static irqreturn_t bh1770_irq(int irq, void *data)
569190420abSSamu Onkalo {
570190420abSSamu Onkalo struct bh1770_chip *chip = data;
571190420abSSamu Onkalo int status;
572190420abSSamu Onkalo int rate = 0;
573190420abSSamu Onkalo
574190420abSSamu Onkalo mutex_lock(&chip->mutex);
575190420abSSamu Onkalo status = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_PS_STATUS);
576190420abSSamu Onkalo
577190420abSSamu Onkalo /* Acknowledge interrupt by reading this register */
578190420abSSamu Onkalo i2c_smbus_read_byte_data(chip->client, BH1770_INTERRUPT);
579190420abSSamu Onkalo
580190420abSSamu Onkalo /*
581190420abSSamu Onkalo * Check if there is fresh data available for als.
582190420abSSamu Onkalo * If this is the very first data, update thresholds after that.
583190420abSSamu Onkalo */
584190420abSSamu Onkalo if (status & BH1770_INT_ALS_DATA) {
585190420abSSamu Onkalo bh1770_lux_get_result(chip);
586190420abSSamu Onkalo if (unlikely(chip->lux_wait_result)) {
587190420abSSamu Onkalo chip->lux_wait_result = false;
588190420abSSamu Onkalo wake_up(&chip->wait);
589190420abSSamu Onkalo bh1770_lux_update_thresholds(chip,
590190420abSSamu Onkalo chip->lux_threshold_hi,
591190420abSSamu Onkalo chip->lux_threshold_lo);
592190420abSSamu Onkalo }
593190420abSSamu Onkalo }
594190420abSSamu Onkalo
595190420abSSamu Onkalo /* Disable interrupt logic to guarantee acknowledgement */
596190420abSSamu Onkalo i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
597190420abSSamu Onkalo (0 << 1) | (0 << 0));
598190420abSSamu Onkalo
599190420abSSamu Onkalo if ((status & BH1770_INT_ALS_INT))
600190420abSSamu Onkalo sysfs_notify(&chip->client->dev.kobj, NULL, "lux0_input");
601190420abSSamu Onkalo
602190420abSSamu Onkalo if (chip->int_mode_prox && (status & BH1770_INT_LEDS_INT)) {
603190420abSSamu Onkalo rate = prox_rates_ms[chip->prox_rate_threshold];
604190420abSSamu Onkalo bh1770_prox_read_result(chip);
605190420abSSamu Onkalo }
606190420abSSamu Onkalo
607190420abSSamu Onkalo /* Re-enable interrupt logic */
608190420abSSamu Onkalo i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
609190420abSSamu Onkalo (chip->int_mode_lux << 1) |
610190420abSSamu Onkalo (chip->int_mode_prox << 0));
611190420abSSamu Onkalo mutex_unlock(&chip->mutex);
612190420abSSamu Onkalo
613190420abSSamu Onkalo /*
614190420abSSamu Onkalo * Can't cancel work while keeping mutex since the work uses the
615190420abSSamu Onkalo * same mutex.
616190420abSSamu Onkalo */
617190420abSSamu Onkalo if (rate) {
618190420abSSamu Onkalo /*
619190420abSSamu Onkalo * Simulate missing no-proximity interrupt 50ms after the
620190420abSSamu Onkalo * next expected interrupt time.
621190420abSSamu Onkalo */
622190420abSSamu Onkalo cancel_delayed_work_sync(&chip->prox_work);
623190420abSSamu Onkalo schedule_delayed_work(&chip->prox_work,
624190420abSSamu Onkalo msecs_to_jiffies(rate + 50));
625190420abSSamu Onkalo }
626190420abSSamu Onkalo return IRQ_HANDLED;
627190420abSSamu Onkalo }
628190420abSSamu Onkalo
bh1770_power_state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)629190420abSSamu Onkalo static ssize_t bh1770_power_state_store(struct device *dev,
630190420abSSamu Onkalo struct device_attribute *attr,
631190420abSSamu Onkalo const char *buf, size_t count)
632190420abSSamu Onkalo {
633190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
634190420abSSamu Onkalo unsigned long value;
635aec04288SDan Carpenter ssize_t ret;
636190420abSSamu Onkalo
637f7b41276SJingoo Han ret = kstrtoul(buf, 0, &value);
638f7b41276SJingoo Han if (ret)
639f7b41276SJingoo Han return ret;
640190420abSSamu Onkalo
641190420abSSamu Onkalo mutex_lock(&chip->mutex);
642190420abSSamu Onkalo if (value) {
643190420abSSamu Onkalo pm_runtime_get_sync(dev);
644190420abSSamu Onkalo
645190420abSSamu Onkalo ret = bh1770_lux_rate(chip, chip->lux_rate_index);
646aec04288SDan Carpenter if (ret < 0) {
647aec04288SDan Carpenter pm_runtime_put(dev);
648aec04288SDan Carpenter goto leave;
649aec04288SDan Carpenter }
650190420abSSamu Onkalo
651aec04288SDan Carpenter ret = bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
652190420abSSamu Onkalo if (ret < 0) {
653190420abSSamu Onkalo pm_runtime_put(dev);
654190420abSSamu Onkalo goto leave;
655190420abSSamu Onkalo }
656190420abSSamu Onkalo
657190420abSSamu Onkalo /* This causes interrupt after the next measurement cycle */
658190420abSSamu Onkalo bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
659190420abSSamu Onkalo BH1770_LUX_DEF_THRES);
660190420abSSamu Onkalo /* Inform that we are waiting for a result from ALS */
661190420abSSamu Onkalo chip->lux_wait_result = true;
662190420abSSamu Onkalo bh1770_prox_mode_control(chip);
663190420abSSamu Onkalo } else if (!pm_runtime_suspended(dev)) {
664190420abSSamu Onkalo pm_runtime_put(dev);
665190420abSSamu Onkalo }
666190420abSSamu Onkalo ret = count;
667190420abSSamu Onkalo leave:
668190420abSSamu Onkalo mutex_unlock(&chip->mutex);
669190420abSSamu Onkalo return ret;
670190420abSSamu Onkalo }
671190420abSSamu Onkalo
bh1770_power_state_show(struct device * dev,struct device_attribute * attr,char * buf)672190420abSSamu Onkalo static ssize_t bh1770_power_state_show(struct device *dev,
673190420abSSamu Onkalo struct device_attribute *attr, char *buf)
674190420abSSamu Onkalo {
675190420abSSamu Onkalo return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
676190420abSSamu Onkalo }
677190420abSSamu Onkalo
bh1770_lux_result_show(struct device * dev,struct device_attribute * attr,char * buf)678190420abSSamu Onkalo static ssize_t bh1770_lux_result_show(struct device *dev,
679190420abSSamu Onkalo struct device_attribute *attr, char *buf)
680190420abSSamu Onkalo {
681190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
682190420abSSamu Onkalo ssize_t ret;
683190420abSSamu Onkalo long timeout;
684190420abSSamu Onkalo
685190420abSSamu Onkalo if (pm_runtime_suspended(dev))
686190420abSSamu Onkalo return -EIO; /* Chip is not enabled at all */
687190420abSSamu Onkalo
688190420abSSamu Onkalo timeout = wait_event_interruptible_timeout(chip->wait,
689190420abSSamu Onkalo !chip->lux_wait_result,
690190420abSSamu Onkalo msecs_to_jiffies(BH1770_TIMEOUT));
691190420abSSamu Onkalo if (!timeout)
692190420abSSamu Onkalo return -EIO;
693190420abSSamu Onkalo
694190420abSSamu Onkalo mutex_lock(&chip->mutex);
695190420abSSamu Onkalo ret = sprintf(buf, "%d\n", bh1770_lux_read_result(chip));
696190420abSSamu Onkalo mutex_unlock(&chip->mutex);
697190420abSSamu Onkalo
698190420abSSamu Onkalo return ret;
699190420abSSamu Onkalo }
700190420abSSamu Onkalo
bh1770_lux_range_show(struct device * dev,struct device_attribute * attr,char * buf)701190420abSSamu Onkalo static ssize_t bh1770_lux_range_show(struct device *dev,
702190420abSSamu Onkalo struct device_attribute *attr, char *buf)
703190420abSSamu Onkalo {
704190420abSSamu Onkalo return sprintf(buf, "%d\n", BH1770_LUX_RANGE);
705190420abSSamu Onkalo }
706190420abSSamu Onkalo
bh1770_prox_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)707190420abSSamu Onkalo static ssize_t bh1770_prox_enable_store(struct device *dev,
708190420abSSamu Onkalo struct device_attribute *attr,
709190420abSSamu Onkalo const char *buf, size_t count)
710190420abSSamu Onkalo {
711190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
712190420abSSamu Onkalo unsigned long value;
713f7b41276SJingoo Han int ret;
714190420abSSamu Onkalo
715f7b41276SJingoo Han ret = kstrtoul(buf, 0, &value);
716f7b41276SJingoo Han if (ret)
717f7b41276SJingoo Han return ret;
718190420abSSamu Onkalo
719190420abSSamu Onkalo mutex_lock(&chip->mutex);
720190420abSSamu Onkalo /* Assume no proximity. Sensor will tell real state soon */
721190420abSSamu Onkalo if (!chip->prox_enable_count)
722190420abSSamu Onkalo chip->prox_data = 0;
723190420abSSamu Onkalo
724190420abSSamu Onkalo if (value)
725190420abSSamu Onkalo chip->prox_enable_count++;
726190420abSSamu Onkalo else if (chip->prox_enable_count > 0)
727190420abSSamu Onkalo chip->prox_enable_count--;
728190420abSSamu Onkalo else
729190420abSSamu Onkalo goto leave;
730190420abSSamu Onkalo
731190420abSSamu Onkalo /* Run control only when chip is powered on */
732190420abSSamu Onkalo if (!pm_runtime_suspended(dev))
733190420abSSamu Onkalo bh1770_prox_mode_control(chip);
734190420abSSamu Onkalo leave:
735190420abSSamu Onkalo mutex_unlock(&chip->mutex);
736190420abSSamu Onkalo return count;
737190420abSSamu Onkalo }
738190420abSSamu Onkalo
bh1770_prox_enable_show(struct device * dev,struct device_attribute * attr,char * buf)739190420abSSamu Onkalo static ssize_t bh1770_prox_enable_show(struct device *dev,
740190420abSSamu Onkalo struct device_attribute *attr, char *buf)
741190420abSSamu Onkalo {
742190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
743190420abSSamu Onkalo ssize_t len;
744190420abSSamu Onkalo
745190420abSSamu Onkalo mutex_lock(&chip->mutex);
746190420abSSamu Onkalo len = sprintf(buf, "%d\n", chip->prox_enable_count);
747190420abSSamu Onkalo mutex_unlock(&chip->mutex);
748190420abSSamu Onkalo return len;
749190420abSSamu Onkalo }
750190420abSSamu Onkalo
bh1770_prox_result_show(struct device * dev,struct device_attribute * attr,char * buf)751190420abSSamu Onkalo static ssize_t bh1770_prox_result_show(struct device *dev,
752190420abSSamu Onkalo struct device_attribute *attr, char *buf)
753190420abSSamu Onkalo {
754190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
755190420abSSamu Onkalo ssize_t ret;
756190420abSSamu Onkalo
757190420abSSamu Onkalo mutex_lock(&chip->mutex);
758190420abSSamu Onkalo if (chip->prox_enable_count && !pm_runtime_suspended(dev))
759190420abSSamu Onkalo ret = sprintf(buf, "%d\n", chip->prox_data);
760190420abSSamu Onkalo else
761190420abSSamu Onkalo ret = -EIO;
762190420abSSamu Onkalo mutex_unlock(&chip->mutex);
763190420abSSamu Onkalo return ret;
764190420abSSamu Onkalo }
765190420abSSamu Onkalo
bh1770_prox_range_show(struct device * dev,struct device_attribute * attr,char * buf)766190420abSSamu Onkalo static ssize_t bh1770_prox_range_show(struct device *dev,
767190420abSSamu Onkalo struct device_attribute *attr, char *buf)
768190420abSSamu Onkalo {
769190420abSSamu Onkalo return sprintf(buf, "%d\n", BH1770_PROX_RANGE);
770190420abSSamu Onkalo }
771190420abSSamu Onkalo
bh1770_get_prox_rate_avail(struct device * dev,struct device_attribute * attr,char * buf)772190420abSSamu Onkalo static ssize_t bh1770_get_prox_rate_avail(struct device *dev,
773190420abSSamu Onkalo struct device_attribute *attr, char *buf)
774190420abSSamu Onkalo {
775190420abSSamu Onkalo int i;
776190420abSSamu Onkalo int pos = 0;
777190420abSSamu Onkalo for (i = 0; i < ARRAY_SIZE(prox_rates_hz); i++)
778190420abSSamu Onkalo pos += sprintf(buf + pos, "%d ", prox_rates_hz[i]);
779190420abSSamu Onkalo sprintf(buf + pos - 1, "\n");
780190420abSSamu Onkalo return pos;
781190420abSSamu Onkalo }
782190420abSSamu Onkalo
bh1770_get_prox_rate_above(struct device * dev,struct device_attribute * attr,char * buf)783190420abSSamu Onkalo static ssize_t bh1770_get_prox_rate_above(struct device *dev,
784190420abSSamu Onkalo struct device_attribute *attr, char *buf)
785190420abSSamu Onkalo {
786190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
787190420abSSamu Onkalo return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate_threshold]);
788190420abSSamu Onkalo }
789190420abSSamu Onkalo
bh1770_get_prox_rate_below(struct device * dev,struct device_attribute * attr,char * buf)790190420abSSamu Onkalo static ssize_t bh1770_get_prox_rate_below(struct device *dev,
791190420abSSamu Onkalo struct device_attribute *attr, char *buf)
792190420abSSamu Onkalo {
793190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
794190420abSSamu Onkalo return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate]);
795190420abSSamu Onkalo }
796190420abSSamu Onkalo
bh1770_prox_rate_validate(int rate)797190420abSSamu Onkalo static int bh1770_prox_rate_validate(int rate)
798190420abSSamu Onkalo {
799190420abSSamu Onkalo int i;
800190420abSSamu Onkalo
801190420abSSamu Onkalo for (i = 0; i < ARRAY_SIZE(prox_rates_hz) - 1; i++)
802190420abSSamu Onkalo if (rate >= prox_rates_hz[i])
803190420abSSamu Onkalo break;
804190420abSSamu Onkalo return i;
805190420abSSamu Onkalo }
806190420abSSamu Onkalo
bh1770_set_prox_rate_above(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)807190420abSSamu Onkalo static ssize_t bh1770_set_prox_rate_above(struct device *dev,
808190420abSSamu Onkalo struct device_attribute *attr,
809190420abSSamu Onkalo const char *buf, size_t count)
810190420abSSamu Onkalo {
811190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
812190420abSSamu Onkalo unsigned long value;
813f7b41276SJingoo Han int ret;
814190420abSSamu Onkalo
815f7b41276SJingoo Han ret = kstrtoul(buf, 0, &value);
816f7b41276SJingoo Han if (ret)
817f7b41276SJingoo Han return ret;
818190420abSSamu Onkalo
819190420abSSamu Onkalo mutex_lock(&chip->mutex);
820190420abSSamu Onkalo chip->prox_rate_threshold = bh1770_prox_rate_validate(value);
821190420abSSamu Onkalo mutex_unlock(&chip->mutex);
822190420abSSamu Onkalo return count;
823190420abSSamu Onkalo }
824190420abSSamu Onkalo
bh1770_set_prox_rate_below(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)825190420abSSamu Onkalo static ssize_t bh1770_set_prox_rate_below(struct device *dev,
826190420abSSamu Onkalo struct device_attribute *attr,
827190420abSSamu Onkalo const char *buf, size_t count)
828190420abSSamu Onkalo {
829190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
830190420abSSamu Onkalo unsigned long value;
831f7b41276SJingoo Han int ret;
832190420abSSamu Onkalo
833f7b41276SJingoo Han ret = kstrtoul(buf, 0, &value);
834f7b41276SJingoo Han if (ret)
835f7b41276SJingoo Han return ret;
836190420abSSamu Onkalo
837190420abSSamu Onkalo mutex_lock(&chip->mutex);
838190420abSSamu Onkalo chip->prox_rate = bh1770_prox_rate_validate(value);
839190420abSSamu Onkalo mutex_unlock(&chip->mutex);
840190420abSSamu Onkalo return count;
841190420abSSamu Onkalo }
842190420abSSamu Onkalo
bh1770_get_prox_thres(struct device * dev,struct device_attribute * attr,char * buf)843190420abSSamu Onkalo static ssize_t bh1770_get_prox_thres(struct device *dev,
844190420abSSamu Onkalo struct device_attribute *attr, char *buf)
845190420abSSamu Onkalo {
846190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
847190420abSSamu Onkalo return sprintf(buf, "%d\n", chip->prox_threshold);
848190420abSSamu Onkalo }
849190420abSSamu Onkalo
bh1770_set_prox_thres(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)850190420abSSamu Onkalo static ssize_t bh1770_set_prox_thres(struct device *dev,
851190420abSSamu Onkalo struct device_attribute *attr,
852190420abSSamu Onkalo const char *buf, size_t count)
853190420abSSamu Onkalo {
854190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
855190420abSSamu Onkalo unsigned long value;
856190420abSSamu Onkalo int ret;
857190420abSSamu Onkalo
858f7b41276SJingoo Han ret = kstrtoul(buf, 0, &value);
859f7b41276SJingoo Han if (ret)
860f7b41276SJingoo Han return ret;
861f7b41276SJingoo Han
862190420abSSamu Onkalo if (value > BH1770_PROX_RANGE)
863190420abSSamu Onkalo return -EINVAL;
864190420abSSamu Onkalo
865190420abSSamu Onkalo mutex_lock(&chip->mutex);
866190420abSSamu Onkalo chip->prox_threshold = value;
867190420abSSamu Onkalo ret = bh1770_prox_set_threshold(chip);
868190420abSSamu Onkalo mutex_unlock(&chip->mutex);
869190420abSSamu Onkalo if (ret < 0)
870190420abSSamu Onkalo return ret;
871190420abSSamu Onkalo return count;
872190420abSSamu Onkalo }
873190420abSSamu Onkalo
bh1770_prox_persistence_show(struct device * dev,struct device_attribute * attr,char * buf)874190420abSSamu Onkalo static ssize_t bh1770_prox_persistence_show(struct device *dev,
875190420abSSamu Onkalo struct device_attribute *attr, char *buf)
876190420abSSamu Onkalo {
877190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
878190420abSSamu Onkalo
879190420abSSamu Onkalo return sprintf(buf, "%u\n", chip->prox_persistence);
880190420abSSamu Onkalo }
881190420abSSamu Onkalo
bh1770_prox_persistence_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)882190420abSSamu Onkalo static ssize_t bh1770_prox_persistence_store(struct device *dev,
883190420abSSamu Onkalo struct device_attribute *attr,
884190420abSSamu Onkalo const char *buf, size_t len)
885190420abSSamu Onkalo {
886190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
887190420abSSamu Onkalo unsigned long value;
888f7b41276SJingoo Han int ret;
889190420abSSamu Onkalo
890f7b41276SJingoo Han ret = kstrtoul(buf, 0, &value);
891f7b41276SJingoo Han if (ret)
892f7b41276SJingoo Han return ret;
893190420abSSamu Onkalo
894190420abSSamu Onkalo if (value > BH1770_PROX_MAX_PERSISTENCE)
895190420abSSamu Onkalo return -EINVAL;
896190420abSSamu Onkalo
897190420abSSamu Onkalo chip->prox_persistence = value;
898190420abSSamu Onkalo
899190420abSSamu Onkalo return len;
900190420abSSamu Onkalo }
901190420abSSamu Onkalo
bh1770_prox_abs_thres_show(struct device * dev,struct device_attribute * attr,char * buf)902190420abSSamu Onkalo static ssize_t bh1770_prox_abs_thres_show(struct device *dev,
903190420abSSamu Onkalo struct device_attribute *attr, char *buf)
904190420abSSamu Onkalo {
905190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
906190420abSSamu Onkalo return sprintf(buf, "%u\n", chip->prox_abs_thres);
907190420abSSamu Onkalo }
908190420abSSamu Onkalo
bh1770_prox_abs_thres_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)909190420abSSamu Onkalo static ssize_t bh1770_prox_abs_thres_store(struct device *dev,
910190420abSSamu Onkalo struct device_attribute *attr,
911190420abSSamu Onkalo const char *buf, size_t len)
912190420abSSamu Onkalo {
913190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
914190420abSSamu Onkalo unsigned long value;
915f7b41276SJingoo Han int ret;
916190420abSSamu Onkalo
917f7b41276SJingoo Han ret = kstrtoul(buf, 0, &value);
918f7b41276SJingoo Han if (ret)
919f7b41276SJingoo Han return ret;
920190420abSSamu Onkalo
921190420abSSamu Onkalo if (value > BH1770_PROX_RANGE)
922190420abSSamu Onkalo return -EINVAL;
923190420abSSamu Onkalo
924190420abSSamu Onkalo chip->prox_abs_thres = value;
925190420abSSamu Onkalo
926190420abSSamu Onkalo return len;
927190420abSSamu Onkalo }
928190420abSSamu Onkalo
bh1770_chip_id_show(struct device * dev,struct device_attribute * attr,char * buf)929190420abSSamu Onkalo static ssize_t bh1770_chip_id_show(struct device *dev,
930190420abSSamu Onkalo struct device_attribute *attr, char *buf)
931190420abSSamu Onkalo {
932190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
933190420abSSamu Onkalo return sprintf(buf, "%s rev %d\n", chip->chipname, chip->revision);
934190420abSSamu Onkalo }
935190420abSSamu Onkalo
bh1770_lux_calib_default_show(struct device * dev,struct device_attribute * attr,char * buf)936190420abSSamu Onkalo static ssize_t bh1770_lux_calib_default_show(struct device *dev,
937190420abSSamu Onkalo struct device_attribute *attr, char *buf)
938190420abSSamu Onkalo {
939190420abSSamu Onkalo return sprintf(buf, "%u\n", BH1770_CALIB_SCALER);
940190420abSSamu Onkalo }
941190420abSSamu Onkalo
bh1770_lux_calib_show(struct device * dev,struct device_attribute * attr,char * buf)942190420abSSamu Onkalo static ssize_t bh1770_lux_calib_show(struct device *dev,
943190420abSSamu Onkalo struct device_attribute *attr, char *buf)
944190420abSSamu Onkalo {
945190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
946190420abSSamu Onkalo ssize_t len;
947190420abSSamu Onkalo
948190420abSSamu Onkalo mutex_lock(&chip->mutex);
949190420abSSamu Onkalo len = sprintf(buf, "%u\n", chip->lux_calib);
950190420abSSamu Onkalo mutex_unlock(&chip->mutex);
951190420abSSamu Onkalo return len;
952190420abSSamu Onkalo }
953190420abSSamu Onkalo
bh1770_lux_calib_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)954190420abSSamu Onkalo static ssize_t bh1770_lux_calib_store(struct device *dev,
955190420abSSamu Onkalo struct device_attribute *attr,
956190420abSSamu Onkalo const char *buf, size_t len)
957190420abSSamu Onkalo {
958190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
959190420abSSamu Onkalo unsigned long value;
960190420abSSamu Onkalo u32 old_calib;
961190420abSSamu Onkalo u32 new_corr;
962f7b41276SJingoo Han int ret;
963190420abSSamu Onkalo
964f7b41276SJingoo Han ret = kstrtoul(buf, 0, &value);
965f7b41276SJingoo Han if (ret)
966f7b41276SJingoo Han return ret;
967190420abSSamu Onkalo
968190420abSSamu Onkalo mutex_lock(&chip->mutex);
969190420abSSamu Onkalo old_calib = chip->lux_calib;
970190420abSSamu Onkalo chip->lux_calib = value;
971190420abSSamu Onkalo new_corr = bh1770_get_corr_value(chip);
972190420abSSamu Onkalo if (new_corr == 0) {
973190420abSSamu Onkalo chip->lux_calib = old_calib;
974190420abSSamu Onkalo mutex_unlock(&chip->mutex);
975190420abSSamu Onkalo return -EINVAL;
976190420abSSamu Onkalo }
977190420abSSamu Onkalo chip->lux_corr = new_corr;
978190420abSSamu Onkalo /* Refresh thresholds on HW after changing correction value */
979190420abSSamu Onkalo bh1770_lux_update_thresholds(chip, chip->lux_threshold_hi,
980190420abSSamu Onkalo chip->lux_threshold_lo);
981190420abSSamu Onkalo
982190420abSSamu Onkalo mutex_unlock(&chip->mutex);
983190420abSSamu Onkalo
984190420abSSamu Onkalo return len;
985190420abSSamu Onkalo }
986190420abSSamu Onkalo
bh1770_get_lux_rate_avail(struct device * dev,struct device_attribute * attr,char * buf)987190420abSSamu Onkalo static ssize_t bh1770_get_lux_rate_avail(struct device *dev,
988190420abSSamu Onkalo struct device_attribute *attr, char *buf)
989190420abSSamu Onkalo {
990190420abSSamu Onkalo int i;
991190420abSSamu Onkalo int pos = 0;
992190420abSSamu Onkalo for (i = 0; i < ARRAY_SIZE(lux_rates_hz); i++)
993190420abSSamu Onkalo pos += sprintf(buf + pos, "%d ", lux_rates_hz[i]);
994190420abSSamu Onkalo sprintf(buf + pos - 1, "\n");
995190420abSSamu Onkalo return pos;
996190420abSSamu Onkalo }
997190420abSSamu Onkalo
bh1770_get_lux_rate(struct device * dev,struct device_attribute * attr,char * buf)998190420abSSamu Onkalo static ssize_t bh1770_get_lux_rate(struct device *dev,
999190420abSSamu Onkalo struct device_attribute *attr, char *buf)
1000190420abSSamu Onkalo {
1001190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
1002190420abSSamu Onkalo return sprintf(buf, "%d\n", lux_rates_hz[chip->lux_rate_index]);
1003190420abSSamu Onkalo }
1004190420abSSamu Onkalo
bh1770_set_lux_rate(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1005190420abSSamu Onkalo static ssize_t bh1770_set_lux_rate(struct device *dev,
1006190420abSSamu Onkalo struct device_attribute *attr,
1007190420abSSamu Onkalo const char *buf, size_t count)
1008190420abSSamu Onkalo {
1009190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
1010190420abSSamu Onkalo unsigned long rate_hz;
1011190420abSSamu Onkalo int ret, i;
1012190420abSSamu Onkalo
1013f7b41276SJingoo Han ret = kstrtoul(buf, 0, &rate_hz);
1014f7b41276SJingoo Han if (ret)
1015f7b41276SJingoo Han return ret;
1016190420abSSamu Onkalo
1017190420abSSamu Onkalo for (i = 0; i < ARRAY_SIZE(lux_rates_hz) - 1; i++)
1018190420abSSamu Onkalo if (rate_hz >= lux_rates_hz[i])
1019190420abSSamu Onkalo break;
1020190420abSSamu Onkalo
1021190420abSSamu Onkalo mutex_lock(&chip->mutex);
1022190420abSSamu Onkalo chip->lux_rate_index = i;
1023190420abSSamu Onkalo ret = bh1770_lux_rate(chip, i);
1024190420abSSamu Onkalo mutex_unlock(&chip->mutex);
1025190420abSSamu Onkalo
1026190420abSSamu Onkalo if (ret < 0)
1027190420abSSamu Onkalo return ret;
1028190420abSSamu Onkalo
1029190420abSSamu Onkalo return count;
1030190420abSSamu Onkalo }
1031190420abSSamu Onkalo
bh1770_get_lux_thresh_above(struct device * dev,struct device_attribute * attr,char * buf)1032190420abSSamu Onkalo static ssize_t bh1770_get_lux_thresh_above(struct device *dev,
1033190420abSSamu Onkalo struct device_attribute *attr, char *buf)
1034190420abSSamu Onkalo {
1035190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
1036190420abSSamu Onkalo return sprintf(buf, "%d\n", chip->lux_threshold_hi);
1037190420abSSamu Onkalo }
1038190420abSSamu Onkalo
bh1770_get_lux_thresh_below(struct device * dev,struct device_attribute * attr,char * buf)1039190420abSSamu Onkalo static ssize_t bh1770_get_lux_thresh_below(struct device *dev,
1040190420abSSamu Onkalo struct device_attribute *attr, char *buf)
1041190420abSSamu Onkalo {
1042190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
1043190420abSSamu Onkalo return sprintf(buf, "%d\n", chip->lux_threshold_lo);
1044190420abSSamu Onkalo }
1045190420abSSamu Onkalo
bh1770_set_lux_thresh(struct bh1770_chip * chip,u16 * target,const char * buf)1046190420abSSamu Onkalo static ssize_t bh1770_set_lux_thresh(struct bh1770_chip *chip, u16 *target,
1047190420abSSamu Onkalo const char *buf)
1048190420abSSamu Onkalo {
1049190420abSSamu Onkalo unsigned long thresh;
1050f7b41276SJingoo Han int ret;
1051190420abSSamu Onkalo
1052f7b41276SJingoo Han ret = kstrtoul(buf, 0, &thresh);
1053f7b41276SJingoo Han if (ret)
1054f7b41276SJingoo Han return ret;
1055190420abSSamu Onkalo
1056190420abSSamu Onkalo if (thresh > BH1770_LUX_RANGE)
1057190420abSSamu Onkalo return -EINVAL;
1058190420abSSamu Onkalo
1059190420abSSamu Onkalo mutex_lock(&chip->mutex);
1060190420abSSamu Onkalo *target = thresh;
1061190420abSSamu Onkalo /*
1062190420abSSamu Onkalo * Don't update values in HW if we are still waiting for
1063190420abSSamu Onkalo * first interrupt to come after device handle open call.
1064190420abSSamu Onkalo */
1065190420abSSamu Onkalo if (!chip->lux_wait_result)
1066190420abSSamu Onkalo ret = bh1770_lux_update_thresholds(chip,
1067190420abSSamu Onkalo chip->lux_threshold_hi,
1068190420abSSamu Onkalo chip->lux_threshold_lo);
1069190420abSSamu Onkalo mutex_unlock(&chip->mutex);
1070190420abSSamu Onkalo return ret;
1071190420abSSamu Onkalo
1072190420abSSamu Onkalo }
1073190420abSSamu Onkalo
bh1770_set_lux_thresh_above(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)1074190420abSSamu Onkalo static ssize_t bh1770_set_lux_thresh_above(struct device *dev,
1075190420abSSamu Onkalo struct device_attribute *attr,
1076190420abSSamu Onkalo const char *buf, size_t len)
1077190420abSSamu Onkalo {
1078190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
1079190420abSSamu Onkalo int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_hi, buf);
1080190420abSSamu Onkalo if (ret < 0)
1081190420abSSamu Onkalo return ret;
1082190420abSSamu Onkalo return len;
1083190420abSSamu Onkalo }
1084190420abSSamu Onkalo
bh1770_set_lux_thresh_below(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)1085190420abSSamu Onkalo static ssize_t bh1770_set_lux_thresh_below(struct device *dev,
1086190420abSSamu Onkalo struct device_attribute *attr,
1087190420abSSamu Onkalo const char *buf, size_t len)
1088190420abSSamu Onkalo {
1089190420abSSamu Onkalo struct bh1770_chip *chip = dev_get_drvdata(dev);
1090190420abSSamu Onkalo int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_lo, buf);
1091190420abSSamu Onkalo if (ret < 0)
1092190420abSSamu Onkalo return ret;
1093190420abSSamu Onkalo return len;
1094190420abSSamu Onkalo }
1095190420abSSamu Onkalo
1096190420abSSamu Onkalo static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, bh1770_prox_enable_show,
1097190420abSSamu Onkalo bh1770_prox_enable_store);
1098190420abSSamu Onkalo static DEVICE_ATTR(prox0_thresh_above1_value, S_IRUGO | S_IWUSR,
1099190420abSSamu Onkalo bh1770_prox_abs_thres_show,
1100190420abSSamu Onkalo bh1770_prox_abs_thres_store);
1101190420abSSamu Onkalo static DEVICE_ATTR(prox0_thresh_above0_value, S_IRUGO | S_IWUSR,
1102190420abSSamu Onkalo bh1770_get_prox_thres,
1103190420abSSamu Onkalo bh1770_set_prox_thres);
1104190420abSSamu Onkalo static DEVICE_ATTR(prox0_raw, S_IRUGO, bh1770_prox_result_show, NULL);
1105190420abSSamu Onkalo static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, bh1770_prox_range_show, NULL);
1106190420abSSamu Onkalo static DEVICE_ATTR(prox0_thresh_above_count, S_IRUGO | S_IWUSR,
1107190420abSSamu Onkalo bh1770_prox_persistence_show,
1108190420abSSamu Onkalo bh1770_prox_persistence_store);
1109190420abSSamu Onkalo static DEVICE_ATTR(prox0_rate_above, S_IRUGO | S_IWUSR,
1110190420abSSamu Onkalo bh1770_get_prox_rate_above,
1111190420abSSamu Onkalo bh1770_set_prox_rate_above);
1112190420abSSamu Onkalo static DEVICE_ATTR(prox0_rate_below, S_IRUGO | S_IWUSR,
1113190420abSSamu Onkalo bh1770_get_prox_rate_below,
1114190420abSSamu Onkalo bh1770_set_prox_rate_below);
1115190420abSSamu Onkalo static DEVICE_ATTR(prox0_rate_avail, S_IRUGO, bh1770_get_prox_rate_avail, NULL);
1116190420abSSamu Onkalo
1117190420abSSamu Onkalo static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, bh1770_lux_calib_show,
1118190420abSSamu Onkalo bh1770_lux_calib_store);
1119190420abSSamu Onkalo static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
1120190420abSSamu Onkalo bh1770_lux_calib_default_show,
1121190420abSSamu Onkalo NULL);
1122190420abSSamu Onkalo static DEVICE_ATTR(lux0_input, S_IRUGO, bh1770_lux_result_show, NULL);
1123190420abSSamu Onkalo static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, bh1770_lux_range_show, NULL);
1124190420abSSamu Onkalo static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, bh1770_get_lux_rate,
1125190420abSSamu Onkalo bh1770_set_lux_rate);
1126190420abSSamu Onkalo static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, bh1770_get_lux_rate_avail, NULL);
1127190420abSSamu Onkalo static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
1128190420abSSamu Onkalo bh1770_get_lux_thresh_above,
1129190420abSSamu Onkalo bh1770_set_lux_thresh_above);
1130190420abSSamu Onkalo static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
1131190420abSSamu Onkalo bh1770_get_lux_thresh_below,
1132190420abSSamu Onkalo bh1770_set_lux_thresh_below);
1133190420abSSamu Onkalo static DEVICE_ATTR(chip_id, S_IRUGO, bh1770_chip_id_show, NULL);
1134190420abSSamu Onkalo static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, bh1770_power_state_show,
1135190420abSSamu Onkalo bh1770_power_state_store);
1136190420abSSamu Onkalo
1137190420abSSamu Onkalo
1138190420abSSamu Onkalo static struct attribute *sysfs_attrs[] = {
1139190420abSSamu Onkalo &dev_attr_lux0_calibscale.attr,
1140190420abSSamu Onkalo &dev_attr_lux0_calibscale_default.attr,
1141190420abSSamu Onkalo &dev_attr_lux0_input.attr,
1142190420abSSamu Onkalo &dev_attr_lux0_sensor_range.attr,
1143190420abSSamu Onkalo &dev_attr_lux0_rate.attr,
1144190420abSSamu Onkalo &dev_attr_lux0_rate_avail.attr,
1145190420abSSamu Onkalo &dev_attr_lux0_thresh_above_value.attr,
1146190420abSSamu Onkalo &dev_attr_lux0_thresh_below_value.attr,
1147190420abSSamu Onkalo &dev_attr_prox0_raw.attr,
1148190420abSSamu Onkalo &dev_attr_prox0_sensor_range.attr,
1149190420abSSamu Onkalo &dev_attr_prox0_raw_en.attr,
1150190420abSSamu Onkalo &dev_attr_prox0_thresh_above_count.attr,
1151190420abSSamu Onkalo &dev_attr_prox0_rate_above.attr,
1152190420abSSamu Onkalo &dev_attr_prox0_rate_below.attr,
1153190420abSSamu Onkalo &dev_attr_prox0_rate_avail.attr,
1154190420abSSamu Onkalo &dev_attr_prox0_thresh_above0_value.attr,
1155190420abSSamu Onkalo &dev_attr_prox0_thresh_above1_value.attr,
1156190420abSSamu Onkalo &dev_attr_chip_id.attr,
1157190420abSSamu Onkalo &dev_attr_power_state.attr,
1158190420abSSamu Onkalo NULL
1159190420abSSamu Onkalo };
1160190420abSSamu Onkalo
11610690ee2bSArvind Yadav static const struct attribute_group bh1770_attribute_group = {
1162190420abSSamu Onkalo .attrs = sysfs_attrs
1163190420abSSamu Onkalo };
1164190420abSSamu Onkalo
bh1770_probe(struct i2c_client * client)1165781edb05SUwe Kleine-König static int bh1770_probe(struct i2c_client *client)
1166190420abSSamu Onkalo {
1167190420abSSamu Onkalo struct bh1770_chip *chip;
1168190420abSSamu Onkalo int err;
1169190420abSSamu Onkalo
11704734e390SHimangi Saraogi chip = devm_kzalloc(&client->dev, sizeof *chip, GFP_KERNEL);
1171190420abSSamu Onkalo if (!chip)
1172190420abSSamu Onkalo return -ENOMEM;
1173190420abSSamu Onkalo
1174190420abSSamu Onkalo i2c_set_clientdata(client, chip);
1175190420abSSamu Onkalo chip->client = client;
1176190420abSSamu Onkalo
1177190420abSSamu Onkalo mutex_init(&chip->mutex);
1178190420abSSamu Onkalo init_waitqueue_head(&chip->wait);
1179190420abSSamu Onkalo INIT_DELAYED_WORK(&chip->prox_work, bh1770_prox_work);
1180190420abSSamu Onkalo
1181190420abSSamu Onkalo if (client->dev.platform_data == NULL) {
1182190420abSSamu Onkalo dev_err(&client->dev, "platform data is mandatory\n");
11834734e390SHimangi Saraogi return -EINVAL;
1184190420abSSamu Onkalo }
1185190420abSSamu Onkalo
1186190420abSSamu Onkalo chip->pdata = client->dev.platform_data;
1187190420abSSamu Onkalo chip->lux_calib = BH1770_LUX_NEUTRAL_CALIB_VALUE;
1188190420abSSamu Onkalo chip->lux_rate_index = BH1770_LUX_DEFAULT_RATE;
1189190420abSSamu Onkalo chip->lux_threshold_lo = BH1770_LUX_DEF_THRES;
1190190420abSSamu Onkalo chip->lux_threshold_hi = BH1770_LUX_DEF_THRES;
1191190420abSSamu Onkalo
1192190420abSSamu Onkalo if (chip->pdata->glass_attenuation == 0)
1193190420abSSamu Onkalo chip->lux_ga = BH1770_NEUTRAL_GA;
1194190420abSSamu Onkalo else
1195190420abSSamu Onkalo chip->lux_ga = chip->pdata->glass_attenuation;
1196190420abSSamu Onkalo
1197190420abSSamu Onkalo chip->prox_threshold = BH1770_PROX_DEF_THRES;
1198190420abSSamu Onkalo chip->prox_led = chip->pdata->led_def_curr;
1199190420abSSamu Onkalo chip->prox_abs_thres = BH1770_PROX_DEF_ABS_THRES;
1200190420abSSamu Onkalo chip->prox_persistence = BH1770_DEFAULT_PERSISTENCE;
1201190420abSSamu Onkalo chip->prox_rate_threshold = BH1770_PROX_DEF_RATE_THRESH;
1202190420abSSamu Onkalo chip->prox_rate = BH1770_PROX_DEFAULT_RATE;
1203190420abSSamu Onkalo chip->prox_data = 0;
1204190420abSSamu Onkalo
1205190420abSSamu Onkalo chip->regs[0].supply = reg_vcc;
1206190420abSSamu Onkalo chip->regs[1].supply = reg_vleds;
1207190420abSSamu Onkalo
12084734e390SHimangi Saraogi err = devm_regulator_bulk_get(&client->dev,
1209190420abSSamu Onkalo ARRAY_SIZE(chip->regs), chip->regs);
1210190420abSSamu Onkalo if (err < 0) {
1211190420abSSamu Onkalo dev_err(&client->dev, "Cannot get regulators\n");
12124734e390SHimangi Saraogi return err;
1213190420abSSamu Onkalo }
1214190420abSSamu Onkalo
1215190420abSSamu Onkalo err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
1216190420abSSamu Onkalo chip->regs);
1217190420abSSamu Onkalo if (err < 0) {
1218190420abSSamu Onkalo dev_err(&client->dev, "Cannot enable regulators\n");
12194734e390SHimangi Saraogi return err;
1220190420abSSamu Onkalo }
1221190420abSSamu Onkalo
1222190420abSSamu Onkalo usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
1223190420abSSamu Onkalo err = bh1770_detect(chip);
1224190420abSSamu Onkalo if (err < 0)
12254734e390SHimangi Saraogi goto fail0;
1226190420abSSamu Onkalo
1227190420abSSamu Onkalo /* Start chip */
1228190420abSSamu Onkalo bh1770_chip_on(chip);
1229190420abSSamu Onkalo pm_runtime_set_active(&client->dev);
1230190420abSSamu Onkalo pm_runtime_enable(&client->dev);
1231190420abSSamu Onkalo
1232190420abSSamu Onkalo chip->lux_corr = bh1770_get_corr_value(chip);
1233190420abSSamu Onkalo if (chip->lux_corr == 0) {
1234190420abSSamu Onkalo dev_err(&client->dev, "Improper correction values\n");
1235190420abSSamu Onkalo err = -EINVAL;
12364734e390SHimangi Saraogi goto fail0;
1237190420abSSamu Onkalo }
1238190420abSSamu Onkalo
1239190420abSSamu Onkalo if (chip->pdata->setup_resources) {
1240190420abSSamu Onkalo err = chip->pdata->setup_resources();
1241190420abSSamu Onkalo if (err) {
1242190420abSSamu Onkalo err = -EINVAL;
12434734e390SHimangi Saraogi goto fail0;
1244190420abSSamu Onkalo }
1245190420abSSamu Onkalo }
1246190420abSSamu Onkalo
1247190420abSSamu Onkalo err = sysfs_create_group(&chip->client->dev.kobj,
1248190420abSSamu Onkalo &bh1770_attribute_group);
1249190420abSSamu Onkalo if (err < 0) {
1250190420abSSamu Onkalo dev_err(&chip->client->dev, "Sysfs registration failed\n");
12514734e390SHimangi Saraogi goto fail1;
1252190420abSSamu Onkalo }
1253190420abSSamu Onkalo
1254190420abSSamu Onkalo /*
1255190420abSSamu Onkalo * Chip needs level triggered interrupt to work. However,
1256190420abSSamu Onkalo * level triggering doesn't work always correctly with power
1257190420abSSamu Onkalo * management. Select both
1258190420abSSamu Onkalo */
1259190420abSSamu Onkalo err = request_threaded_irq(client->irq, NULL,
1260190420abSSamu Onkalo bh1770_irq,
1261190420abSSamu Onkalo IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
1262190420abSSamu Onkalo IRQF_TRIGGER_LOW,
1263190420abSSamu Onkalo "bh1770", chip);
1264190420abSSamu Onkalo if (err) {
1265190420abSSamu Onkalo dev_err(&client->dev, "could not get IRQ %d\n",
1266190420abSSamu Onkalo client->irq);
12674734e390SHimangi Saraogi goto fail2;
1268190420abSSamu Onkalo }
1269190420abSSamu Onkalo regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1270190420abSSamu Onkalo return err;
12714734e390SHimangi Saraogi fail2:
1272190420abSSamu Onkalo sysfs_remove_group(&chip->client->dev.kobj,
1273190420abSSamu Onkalo &bh1770_attribute_group);
12744734e390SHimangi Saraogi fail1:
1275190420abSSamu Onkalo if (chip->pdata->release_resources)
1276190420abSSamu Onkalo chip->pdata->release_resources();
12774734e390SHimangi Saraogi fail0:
1278190420abSSamu Onkalo regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1279190420abSSamu Onkalo return err;
1280190420abSSamu Onkalo }
1281190420abSSamu Onkalo
bh1770_remove(struct i2c_client * client)1282ed5c2f5fSUwe Kleine-König static void bh1770_remove(struct i2c_client *client)
1283190420abSSamu Onkalo {
1284190420abSSamu Onkalo struct bh1770_chip *chip = i2c_get_clientdata(client);
1285190420abSSamu Onkalo
1286190420abSSamu Onkalo free_irq(client->irq, chip);
1287190420abSSamu Onkalo
1288190420abSSamu Onkalo sysfs_remove_group(&chip->client->dev.kobj,
1289190420abSSamu Onkalo &bh1770_attribute_group);
1290190420abSSamu Onkalo
1291190420abSSamu Onkalo if (chip->pdata->release_resources)
1292190420abSSamu Onkalo chip->pdata->release_resources();
1293190420abSSamu Onkalo
1294190420abSSamu Onkalo cancel_delayed_work_sync(&chip->prox_work);
1295190420abSSamu Onkalo
1296190420abSSamu Onkalo if (!pm_runtime_suspended(&client->dev))
1297190420abSSamu Onkalo bh1770_chip_off(chip);
1298190420abSSamu Onkalo
1299190420abSSamu Onkalo pm_runtime_disable(&client->dev);
1300190420abSSamu Onkalo pm_runtime_set_suspended(&client->dev);
1301190420abSSamu Onkalo }
1302190420abSSamu Onkalo
13030ca19d6fSJingoo Han #ifdef CONFIG_PM_SLEEP
bh1770_suspend(struct device * dev)1304190420abSSamu Onkalo static int bh1770_suspend(struct device *dev)
1305190420abSSamu Onkalo {
13068c99d8e6SGeliang Tang struct i2c_client *client = to_i2c_client(dev);
1307190420abSSamu Onkalo struct bh1770_chip *chip = i2c_get_clientdata(client);
1308190420abSSamu Onkalo
1309190420abSSamu Onkalo bh1770_chip_off(chip);
1310190420abSSamu Onkalo
1311190420abSSamu Onkalo return 0;
1312190420abSSamu Onkalo }
1313190420abSSamu Onkalo
bh1770_resume(struct device * dev)1314190420abSSamu Onkalo static int bh1770_resume(struct device *dev)
1315190420abSSamu Onkalo {
13168c99d8e6SGeliang Tang struct i2c_client *client = to_i2c_client(dev);
1317190420abSSamu Onkalo struct bh1770_chip *chip = i2c_get_clientdata(client);
1318190420abSSamu Onkalo int ret = 0;
1319190420abSSamu Onkalo
1320190420abSSamu Onkalo bh1770_chip_on(chip);
1321190420abSSamu Onkalo
1322190420abSSamu Onkalo if (!pm_runtime_suspended(dev)) {
1323190420abSSamu Onkalo /*
1324190420abSSamu Onkalo * If we were enabled at suspend time, it is expected
1325190420abSSamu Onkalo * everything works nice and smoothly
1326190420abSSamu Onkalo */
1327190420abSSamu Onkalo ret = bh1770_lux_rate(chip, chip->lux_rate_index);
1328190420abSSamu Onkalo ret |= bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
1329190420abSSamu Onkalo
1330190420abSSamu Onkalo /* This causes interrupt after the next measurement cycle */
1331190420abSSamu Onkalo bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
1332190420abSSamu Onkalo BH1770_LUX_DEF_THRES);
1333190420abSSamu Onkalo /* Inform that we are waiting for a result from ALS */
1334190420abSSamu Onkalo chip->lux_wait_result = true;
1335190420abSSamu Onkalo bh1770_prox_mode_control(chip);
1336190420abSSamu Onkalo }
1337190420abSSamu Onkalo return ret;
1338190420abSSamu Onkalo }
1339190420abSSamu Onkalo #endif
1340190420abSSamu Onkalo
1341bbd6d050SRafael J. Wysocki #ifdef CONFIG_PM
bh1770_runtime_suspend(struct device * dev)1342190420abSSamu Onkalo static int bh1770_runtime_suspend(struct device *dev)
1343190420abSSamu Onkalo {
13448c99d8e6SGeliang Tang struct i2c_client *client = to_i2c_client(dev);
1345190420abSSamu Onkalo struct bh1770_chip *chip = i2c_get_clientdata(client);
1346190420abSSamu Onkalo
1347190420abSSamu Onkalo bh1770_chip_off(chip);
1348190420abSSamu Onkalo
1349190420abSSamu Onkalo return 0;
1350190420abSSamu Onkalo }
1351190420abSSamu Onkalo
bh1770_runtime_resume(struct device * dev)1352190420abSSamu Onkalo static int bh1770_runtime_resume(struct device *dev)
1353190420abSSamu Onkalo {
13548c99d8e6SGeliang Tang struct i2c_client *client = to_i2c_client(dev);
1355190420abSSamu Onkalo struct bh1770_chip *chip = i2c_get_clientdata(client);
1356190420abSSamu Onkalo
1357190420abSSamu Onkalo bh1770_chip_on(chip);
1358190420abSSamu Onkalo
1359190420abSSamu Onkalo return 0;
1360190420abSSamu Onkalo }
1361190420abSSamu Onkalo #endif
1362190420abSSamu Onkalo
1363190420abSSamu Onkalo static const struct i2c_device_id bh1770_id[] = {
1364190420abSSamu Onkalo {"bh1770glc", 0 },
1365190420abSSamu Onkalo {"sfh7770", 0 },
1366190420abSSamu Onkalo {}
1367190420abSSamu Onkalo };
1368190420abSSamu Onkalo
1369190420abSSamu Onkalo MODULE_DEVICE_TABLE(i2c, bh1770_id);
1370190420abSSamu Onkalo
1371190420abSSamu Onkalo static const struct dev_pm_ops bh1770_pm_ops = {
1372190420abSSamu Onkalo SET_SYSTEM_SLEEP_PM_OPS(bh1770_suspend, bh1770_resume)
1373190420abSSamu Onkalo SET_RUNTIME_PM_OPS(bh1770_runtime_suspend, bh1770_runtime_resume, NULL)
1374190420abSSamu Onkalo };
1375190420abSSamu Onkalo
1376190420abSSamu Onkalo static struct i2c_driver bh1770_driver = {
1377190420abSSamu Onkalo .driver = {
1378190420abSSamu Onkalo .name = "bh1770glc",
1379190420abSSamu Onkalo .pm = &bh1770_pm_ops,
1380190420abSSamu Onkalo },
1381*f050bb8fSUwe Kleine-König .probe = bh1770_probe,
13822d6bed9cSBill Pemberton .remove = bh1770_remove,
1383190420abSSamu Onkalo .id_table = bh1770_id,
1384190420abSSamu Onkalo };
1385190420abSSamu Onkalo
1386a64fe2edSAxel Lin module_i2c_driver(bh1770_driver);
1387190420abSSamu Onkalo
1388190420abSSamu Onkalo MODULE_DESCRIPTION("BH1770GLC / SFH7770 combined ALS and proximity sensor");
1389190420abSSamu Onkalo MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1390190420abSSamu Onkalo MODULE_LICENSE("GPL v2");
1391