1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * This file is part of the ROHM BH1770GLC / OSRAM SFH7770 sensor driver.
4 * Chip is combined proximity and ambient light sensor.
5 *
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 *
8 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/mutex.h>
16 #include <linux/platform_data/bh1770glc.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/workqueue.h>
20 #include <linux/delay.h>
21 #include <linux/wait.h>
22 #include <linux/slab.h>
23
24 #define BH1770_ALS_CONTROL 0x80 /* ALS operation mode control */
25 #define BH1770_PS_CONTROL 0x81 /* PS operation mode control */
26 #define BH1770_I_LED 0x82 /* active LED and LED1, LED2 current */
27 #define BH1770_I_LED3 0x83 /* LED3 current setting */
28 #define BH1770_ALS_PS_MEAS 0x84 /* Forced mode trigger */
29 #define BH1770_PS_MEAS_RATE 0x85 /* PS meas. rate at stand alone mode */
30 #define BH1770_ALS_MEAS_RATE 0x86 /* ALS meas. rate at stand alone mode */
31 #define BH1770_PART_ID 0x8a /* Part number and revision ID */
32 #define BH1770_MANUFACT_ID 0x8b /* Manufacturerer ID */
33 #define BH1770_ALS_DATA_0 0x8c /* ALS DATA low byte */
34 #define BH1770_ALS_DATA_1 0x8d /* ALS DATA high byte */
35 #define BH1770_ALS_PS_STATUS 0x8e /* Measurement data and int status */
36 #define BH1770_PS_DATA_LED1 0x8f /* PS data from LED1 */
37 #define BH1770_PS_DATA_LED2 0x90 /* PS data from LED2 */
38 #define BH1770_PS_DATA_LED3 0x91 /* PS data from LED3 */
39 #define BH1770_INTERRUPT 0x92 /* Interrupt setting */
40 #define BH1770_PS_TH_LED1 0x93 /* PS interrupt threshold for LED1 */
41 #define BH1770_PS_TH_LED2 0x94 /* PS interrupt threshold for LED2 */
42 #define BH1770_PS_TH_LED3 0x95 /* PS interrupt threshold for LED3 */
43 #define BH1770_ALS_TH_UP_0 0x96 /* ALS upper threshold low byte */
44 #define BH1770_ALS_TH_UP_1 0x97 /* ALS upper threshold high byte */
45 #define BH1770_ALS_TH_LOW_0 0x98 /* ALS lower threshold low byte */
46 #define BH1770_ALS_TH_LOW_1 0x99 /* ALS lower threshold high byte */
47
48 /* MANUFACT_ID */
49 #define BH1770_MANUFACT_ROHM 0x01
50 #define BH1770_MANUFACT_OSRAM 0x03
51
52 /* PART_ID */
53 #define BH1770_PART 0x90
54 #define BH1770_PART_MASK 0xf0
55 #define BH1770_REV_MASK 0x0f
56 #define BH1770_REV_SHIFT 0
57 #define BH1770_REV_0 0x00
58 #define BH1770_REV_1 0x01
59
60 /* Operating modes for both */
61 #define BH1770_STANDBY 0x00
62 #define BH1770_FORCED 0x02
63 #define BH1770_STANDALONE 0x03
64 #define BH1770_SWRESET (0x01 << 2)
65
66 #define BH1770_PS_TRIG_MEAS (1 << 0)
67 #define BH1770_ALS_TRIG_MEAS (1 << 1)
68
69 /* Interrupt control */
70 #define BH1770_INT_OUTPUT_MODE (1 << 3) /* 0 = latched */
71 #define BH1770_INT_POLARITY (1 << 2) /* 1 = active high */
72 #define BH1770_INT_ALS_ENA (1 << 1)
73 #define BH1770_INT_PS_ENA (1 << 0)
74
75 /* Interrupt status */
76 #define BH1770_INT_LED1_DATA (1 << 0)
77 #define BH1770_INT_LED1_INT (1 << 1)
78 #define BH1770_INT_LED2_DATA (1 << 2)
79 #define BH1770_INT_LED2_INT (1 << 3)
80 #define BH1770_INT_LED3_DATA (1 << 4)
81 #define BH1770_INT_LED3_INT (1 << 5)
82 #define BH1770_INT_LEDS_INT ((1 << 1) | (1 << 3) | (1 << 5))
83 #define BH1770_INT_ALS_DATA (1 << 6)
84 #define BH1770_INT_ALS_INT (1 << 7)
85
86 /* Led channels */
87 #define BH1770_LED1 0x00
88
89 #define BH1770_DISABLE 0
90 #define BH1770_ENABLE 1
91 #define BH1770_PROX_CHANNELS 1
92
93 #define BH1770_LUX_DEFAULT_RATE 1 /* Index to lux rate table */
94 #define BH1770_PROX_DEFAULT_RATE 1 /* Direct HW value =~ 50Hz */
95 #define BH1770_PROX_DEF_RATE_THRESH 6 /* Direct HW value =~ 5 Hz */
96 #define BH1770_STARTUP_DELAY 50
97 #define BH1770_RESET_TIME 10
98 #define BH1770_TIMEOUT 2100 /* Timeout in 2.1 seconds */
99
100 #define BH1770_LUX_RANGE 65535
101 #define BH1770_PROX_RANGE 255
102 #define BH1770_COEF_SCALER 1024
103 #define BH1770_CALIB_SCALER 8192
104 #define BH1770_LUX_NEUTRAL_CALIB_VALUE (1 * BH1770_CALIB_SCALER)
105 #define BH1770_LUX_DEF_THRES 1000
106 #define BH1770_PROX_DEF_THRES 70
107 #define BH1770_PROX_DEF_ABS_THRES 100
108 #define BH1770_DEFAULT_PERSISTENCE 10
109 #define BH1770_PROX_MAX_PERSISTENCE 50
110 #define BH1770_LUX_GA_SCALE 16384
111 #define BH1770_LUX_CF_SCALE 2048 /* CF ChipFactor */
112 #define BH1770_NEUTRAL_CF BH1770_LUX_CF_SCALE
113 #define BH1770_LUX_CORR_SCALE 4096
114
115 #define PROX_ABOVE_THRESHOLD 1
116 #define PROX_BELOW_THRESHOLD 0
117
118 #define PROX_IGNORE_LUX_LIMIT 500
119
120 struct bh1770_chip {
121 struct bh1770_platform_data *pdata;
122 char chipname[10];
123 u8 revision;
124 struct i2c_client *client;
125 struct regulator_bulk_data regs[2];
126 struct mutex mutex; /* avoid parallel access */
127 wait_queue_head_t wait;
128
129 bool int_mode_prox;
130 bool int_mode_lux;
131 struct delayed_work prox_work;
132 u32 lux_cf; /* Chip specific factor */
133 u32 lux_ga;
134 u32 lux_calib;
135 int lux_rate_index;
136 u32 lux_corr;
137 u16 lux_data_raw;
138 u16 lux_threshold_hi;
139 u16 lux_threshold_lo;
140 u16 lux_thres_hi_onchip;
141 u16 lux_thres_lo_onchip;
142 bool lux_wait_result;
143
144 int prox_enable_count;
145 u16 prox_coef;
146 u16 prox_const;
147 int prox_rate;
148 int prox_rate_threshold;
149 u8 prox_persistence;
150 u8 prox_persistence_counter;
151 u8 prox_data;
152 u8 prox_threshold;
153 u8 prox_threshold_hw;
154 bool prox_force_update;
155 u8 prox_abs_thres;
156 u8 prox_led;
157 };
158
159 static const char reg_vcc[] = "Vcc";
160 static const char reg_vleds[] = "Vleds";
161
162 /*
163 * Supported stand alone rates in ms from chip data sheet
164 * {10, 20, 30, 40, 70, 100, 200, 500, 1000, 2000};
165 */
166 static const s16 prox_rates_hz[] = {100, 50, 33, 25, 14, 10, 5, 2};
167 static const s16 prox_rates_ms[] = {10, 20, 30, 40, 70, 100, 200, 500};
168
169 /*
170 * Supported stand alone rates in ms from chip data sheet
171 * {100, 200, 500, 1000, 2000};
172 */
173 static const s16 lux_rates_hz[] = {10, 5, 2, 1, 0};
174
175 /*
176 * interrupt control functions are called while keeping chip->mutex
177 * excluding module probe / remove
178 */
bh1770_lux_interrupt_control(struct bh1770_chip * chip,int lux)179 static inline int bh1770_lux_interrupt_control(struct bh1770_chip *chip,
180 int lux)
181 {
182 chip->int_mode_lux = lux;
183 /* Set interrupt modes, interrupt active low, latched */
184 return i2c_smbus_write_byte_data(chip->client,
185 BH1770_INTERRUPT,
186 (lux << 1) | chip->int_mode_prox);
187 }
188
bh1770_prox_interrupt_control(struct bh1770_chip * chip,int ps)189 static inline int bh1770_prox_interrupt_control(struct bh1770_chip *chip,
190 int ps)
191 {
192 chip->int_mode_prox = ps;
193 return i2c_smbus_write_byte_data(chip->client,
194 BH1770_INTERRUPT,
195 (chip->int_mode_lux << 1) | (ps << 0));
196 }
197
198 /* chip->mutex is always kept here */
bh1770_lux_rate(struct bh1770_chip * chip,int rate_index)199 static int bh1770_lux_rate(struct bh1770_chip *chip, int rate_index)
200 {
201 /* sysfs may call this when the chip is powered off */
202 if (pm_runtime_suspended(&chip->client->dev))
203 return 0;
204
205 /* Proper proximity response needs fastest lux rate (100ms) */
206 if (chip->prox_enable_count)
207 rate_index = 0;
208
209 return i2c_smbus_write_byte_data(chip->client,
210 BH1770_ALS_MEAS_RATE,
211 rate_index);
212 }
213
bh1770_prox_rate(struct bh1770_chip * chip,int mode)214 static int bh1770_prox_rate(struct bh1770_chip *chip, int mode)
215 {
216 int rate;
217
218 rate = (mode == PROX_ABOVE_THRESHOLD) ?
219 chip->prox_rate_threshold : chip->prox_rate;
220
221 return i2c_smbus_write_byte_data(chip->client,
222 BH1770_PS_MEAS_RATE,
223 rate);
224 }
225
226 /* InfraredLED is controlled by the chip during proximity scanning */
bh1770_led_cfg(struct bh1770_chip * chip)227 static inline int bh1770_led_cfg(struct bh1770_chip *chip)
228 {
229 /* LED cfg, current for leds 1 and 2 */
230 return i2c_smbus_write_byte_data(chip->client,
231 BH1770_I_LED,
232 (BH1770_LED1 << 6) |
233 (BH1770_LED_5mA << 3) |
234 chip->prox_led);
235 }
236
237 /*
238 * Following two functions converts raw ps values from HW to normalized
239 * values. Purpose is to compensate differences between different sensor
240 * versions and variants so that result means about the same between
241 * versions.
242 */
bh1770_psraw_to_adjusted(struct bh1770_chip * chip,u8 psraw)243 static inline u8 bh1770_psraw_to_adjusted(struct bh1770_chip *chip, u8 psraw)
244 {
245 u16 adjusted;
246 adjusted = (u16)(((u32)(psraw + chip->prox_const) * chip->prox_coef) /
247 BH1770_COEF_SCALER);
248 if (adjusted > BH1770_PROX_RANGE)
249 adjusted = BH1770_PROX_RANGE;
250 return adjusted;
251 }
252
bh1770_psadjusted_to_raw(struct bh1770_chip * chip,u8 ps)253 static inline u8 bh1770_psadjusted_to_raw(struct bh1770_chip *chip, u8 ps)
254 {
255 u16 raw;
256
257 raw = (((u32)ps * BH1770_COEF_SCALER) / chip->prox_coef);
258 if (raw > chip->prox_const)
259 raw = raw - chip->prox_const;
260 else
261 raw = 0;
262 return raw;
263 }
264
265 /*
266 * Following two functions converts raw lux values from HW to normalized
267 * values. Purpose is to compensate differences between different sensor
268 * versions and variants so that result means about the same between
269 * versions. Chip->mutex is kept when this is called.
270 */
bh1770_prox_set_threshold(struct bh1770_chip * chip)271 static int bh1770_prox_set_threshold(struct bh1770_chip *chip)
272 {
273 u8 tmp = 0;
274
275 /* sysfs may call this when the chip is powered off */
276 if (pm_runtime_suspended(&chip->client->dev))
277 return 0;
278
279 tmp = bh1770_psadjusted_to_raw(chip, chip->prox_threshold);
280 chip->prox_threshold_hw = tmp;
281
282 return i2c_smbus_write_byte_data(chip->client, BH1770_PS_TH_LED1,
283 tmp);
284 }
285
bh1770_lux_raw_to_adjusted(struct bh1770_chip * chip,u16 raw)286 static inline u16 bh1770_lux_raw_to_adjusted(struct bh1770_chip *chip, u16 raw)
287 {
288 u32 lux;
289 lux = ((u32)raw * chip->lux_corr) / BH1770_LUX_CORR_SCALE;
290 return min(lux, (u32)BH1770_LUX_RANGE);
291 }
292
bh1770_lux_adjusted_to_raw(struct bh1770_chip * chip,u16 adjusted)293 static inline u16 bh1770_lux_adjusted_to_raw(struct bh1770_chip *chip,
294 u16 adjusted)
295 {
296 return (u32)adjusted * BH1770_LUX_CORR_SCALE / chip->lux_corr;
297 }
298
299 /* chip->mutex is kept when this is called */
bh1770_lux_update_thresholds(struct bh1770_chip * chip,u16 threshold_hi,u16 threshold_lo)300 static int bh1770_lux_update_thresholds(struct bh1770_chip *chip,
301 u16 threshold_hi, u16 threshold_lo)
302 {
303 u8 data[4];
304 int ret;
305
306 /* sysfs may call this when the chip is powered off */
307 if (pm_runtime_suspended(&chip->client->dev))
308 return 0;
309
310 /*
311 * Compensate threshold values with the correction factors if not
312 * set to minimum or maximum.
313 * Min & max values disables interrupts.
314 */
315 if (threshold_hi != BH1770_LUX_RANGE && threshold_hi != 0)
316 threshold_hi = bh1770_lux_adjusted_to_raw(chip, threshold_hi);
317
318 if (threshold_lo != BH1770_LUX_RANGE && threshold_lo != 0)
319 threshold_lo = bh1770_lux_adjusted_to_raw(chip, threshold_lo);
320
321 if (chip->lux_thres_hi_onchip == threshold_hi &&
322 chip->lux_thres_lo_onchip == threshold_lo)
323 return 0;
324
325 chip->lux_thres_hi_onchip = threshold_hi;
326 chip->lux_thres_lo_onchip = threshold_lo;
327
328 data[0] = threshold_hi;
329 data[1] = threshold_hi >> 8;
330 data[2] = threshold_lo;
331 data[3] = threshold_lo >> 8;
332
333 ret = i2c_smbus_write_i2c_block_data(chip->client,
334 BH1770_ALS_TH_UP_0,
335 ARRAY_SIZE(data),
336 data);
337 return ret;
338 }
339
bh1770_lux_get_result(struct bh1770_chip * chip)340 static int bh1770_lux_get_result(struct bh1770_chip *chip)
341 {
342 u16 data;
343 int ret;
344
345 ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_0);
346 if (ret < 0)
347 return ret;
348
349 data = ret & 0xff;
350 ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_1);
351 if (ret < 0)
352 return ret;
353
354 chip->lux_data_raw = data | ((ret & 0xff) << 8);
355
356 return 0;
357 }
358
359 /* Calculate correction value which contains chip and device specific parts */
bh1770_get_corr_value(struct bh1770_chip * chip)360 static u32 bh1770_get_corr_value(struct bh1770_chip *chip)
361 {
362 u32 tmp;
363 /* Impact of glass attenuation correction */
364 tmp = (BH1770_LUX_CORR_SCALE * chip->lux_ga) / BH1770_LUX_GA_SCALE;
365 /* Impact of chip factor correction */
366 tmp = (tmp * chip->lux_cf) / BH1770_LUX_CF_SCALE;
367 /* Impact of Device specific calibration correction */
368 tmp = (tmp * chip->lux_calib) / BH1770_CALIB_SCALER;
369 return tmp;
370 }
371
bh1770_lux_read_result(struct bh1770_chip * chip)372 static int bh1770_lux_read_result(struct bh1770_chip *chip)
373 {
374 bh1770_lux_get_result(chip);
375 return bh1770_lux_raw_to_adjusted(chip, chip->lux_data_raw);
376 }
377
378 /*
379 * Chip on / off functions are called while keeping mutex except probe
380 * or remove phase
381 */
bh1770_chip_on(struct bh1770_chip * chip)382 static int bh1770_chip_on(struct bh1770_chip *chip)
383 {
384 int ret = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
385 chip->regs);
386 if (ret < 0)
387 return ret;
388
389 usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
390
391 /* Reset the chip */
392 i2c_smbus_write_byte_data(chip->client, BH1770_ALS_CONTROL,
393 BH1770_SWRESET);
394 usleep_range(BH1770_RESET_TIME, BH1770_RESET_TIME * 2);
395
396 /*
397 * ALS is started always since proximity needs als results
398 * for realibility estimation.
399 * Let's assume dark until the first ALS measurement is ready.
400 */
401 chip->lux_data_raw = 0;
402 chip->prox_data = 0;
403 ret = i2c_smbus_write_byte_data(chip->client,
404 BH1770_ALS_CONTROL, BH1770_STANDALONE);
405
406 /* Assume reset defaults */
407 chip->lux_thres_hi_onchip = BH1770_LUX_RANGE;
408 chip->lux_thres_lo_onchip = 0;
409
410 return ret;
411 }
412
bh1770_chip_off(struct bh1770_chip * chip)413 static void bh1770_chip_off(struct bh1770_chip *chip)
414 {
415 i2c_smbus_write_byte_data(chip->client,
416 BH1770_INTERRUPT, BH1770_DISABLE);
417 i2c_smbus_write_byte_data(chip->client,
418 BH1770_ALS_CONTROL, BH1770_STANDBY);
419 i2c_smbus_write_byte_data(chip->client,
420 BH1770_PS_CONTROL, BH1770_STANDBY);
421 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
422 }
423
424 /* chip->mutex is kept when this is called */
bh1770_prox_mode_control(struct bh1770_chip * chip)425 static int bh1770_prox_mode_control(struct bh1770_chip *chip)
426 {
427 if (chip->prox_enable_count) {
428 chip->prox_force_update = true; /* Force immediate update */
429
430 bh1770_lux_rate(chip, chip->lux_rate_index);
431 bh1770_prox_set_threshold(chip);
432 bh1770_led_cfg(chip);
433 bh1770_prox_rate(chip, PROX_BELOW_THRESHOLD);
434 bh1770_prox_interrupt_control(chip, BH1770_ENABLE);
435 i2c_smbus_write_byte_data(chip->client,
436 BH1770_PS_CONTROL, BH1770_STANDALONE);
437 } else {
438 chip->prox_data = 0;
439 bh1770_lux_rate(chip, chip->lux_rate_index);
440 bh1770_prox_interrupt_control(chip, BH1770_DISABLE);
441 i2c_smbus_write_byte_data(chip->client,
442 BH1770_PS_CONTROL, BH1770_STANDBY);
443 }
444 return 0;
445 }
446
447 /* chip->mutex is kept when this is called */
bh1770_prox_read_result(struct bh1770_chip * chip)448 static int bh1770_prox_read_result(struct bh1770_chip *chip)
449 {
450 int ret;
451 bool above;
452 u8 mode;
453
454 ret = i2c_smbus_read_byte_data(chip->client, BH1770_PS_DATA_LED1);
455 if (ret < 0)
456 goto out;
457
458 if (ret > chip->prox_threshold_hw)
459 above = true;
460 else
461 above = false;
462
463 /*
464 * when ALS levels goes above limit, proximity result may be
465 * false proximity. Thus ignore the result. With real proximity
466 * there is a shadow causing low als levels.
467 */
468 if (chip->lux_data_raw > PROX_IGNORE_LUX_LIMIT)
469 ret = 0;
470
471 chip->prox_data = bh1770_psraw_to_adjusted(chip, ret);
472
473 /* Strong proximity level or force mode requires immediate response */
474 if (chip->prox_data >= chip->prox_abs_thres ||
475 chip->prox_force_update)
476 chip->prox_persistence_counter = chip->prox_persistence;
477
478 chip->prox_force_update = false;
479
480 /* Persistence filttering to reduce false proximity events */
481 if (likely(above)) {
482 if (chip->prox_persistence_counter < chip->prox_persistence) {
483 chip->prox_persistence_counter++;
484 ret = -ENODATA;
485 } else {
486 mode = PROX_ABOVE_THRESHOLD;
487 ret = 0;
488 }
489 } else {
490 chip->prox_persistence_counter = 0;
491 mode = PROX_BELOW_THRESHOLD;
492 chip->prox_data = 0;
493 ret = 0;
494 }
495
496 /* Set proximity detection rate based on above or below value */
497 if (ret == 0) {
498 bh1770_prox_rate(chip, mode);
499 sysfs_notify(&chip->client->dev.kobj, NULL, "prox0_raw");
500 }
501 out:
502 return ret;
503 }
504
bh1770_detect(struct bh1770_chip * chip)505 static int bh1770_detect(struct bh1770_chip *chip)
506 {
507 struct i2c_client *client = chip->client;
508 s32 ret;
509 u8 manu, part;
510
511 ret = i2c_smbus_read_byte_data(client, BH1770_MANUFACT_ID);
512 if (ret < 0)
513 goto error;
514 manu = (u8)ret;
515
516 ret = i2c_smbus_read_byte_data(client, BH1770_PART_ID);
517 if (ret < 0)
518 goto error;
519 part = (u8)ret;
520
521 chip->revision = (part & BH1770_REV_MASK) >> BH1770_REV_SHIFT;
522 chip->prox_coef = BH1770_COEF_SCALER;
523 chip->prox_const = 0;
524 chip->lux_cf = BH1770_NEUTRAL_CF;
525
526 if ((manu == BH1770_MANUFACT_ROHM) &&
527 ((part & BH1770_PART_MASK) == BH1770_PART)) {
528 snprintf(chip->chipname, sizeof(chip->chipname), "BH1770GLC");
529 return 0;
530 }
531
532 if ((manu == BH1770_MANUFACT_OSRAM) &&
533 ((part & BH1770_PART_MASK) == BH1770_PART)) {
534 snprintf(chip->chipname, sizeof(chip->chipname), "SFH7770");
535 /* Values selected by comparing different versions */
536 chip->prox_coef = 819; /* 0.8 * BH1770_COEF_SCALER */
537 chip->prox_const = 40;
538 return 0;
539 }
540
541 ret = -ENODEV;
542 error:
543 dev_dbg(&client->dev, "BH1770 or SFH7770 not found\n");
544
545 return ret;
546 }
547
548 /*
549 * This work is re-scheduled at every proximity interrupt.
550 * If this work is running, it means that there hasn't been any
551 * proximity interrupt in time. Situation is handled as no-proximity.
552 * It would be nice to have low-threshold interrupt or interrupt
553 * when measurement and hi-threshold are both 0. But neither of those exists.
554 * This is a workaroud for missing HW feature.
555 */
556
bh1770_prox_work(struct work_struct * work)557 static void bh1770_prox_work(struct work_struct *work)
558 {
559 struct bh1770_chip *chip =
560 container_of(work, struct bh1770_chip, prox_work.work);
561
562 mutex_lock(&chip->mutex);
563 bh1770_prox_read_result(chip);
564 mutex_unlock(&chip->mutex);
565 }
566
567 /* This is threaded irq handler */
bh1770_irq(int irq,void * data)568 static irqreturn_t bh1770_irq(int irq, void *data)
569 {
570 struct bh1770_chip *chip = data;
571 int status;
572 int rate = 0;
573
574 mutex_lock(&chip->mutex);
575 status = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_PS_STATUS);
576
577 /* Acknowledge interrupt by reading this register */
578 i2c_smbus_read_byte_data(chip->client, BH1770_INTERRUPT);
579
580 /*
581 * Check if there is fresh data available for als.
582 * If this is the very first data, update thresholds after that.
583 */
584 if (status & BH1770_INT_ALS_DATA) {
585 bh1770_lux_get_result(chip);
586 if (unlikely(chip->lux_wait_result)) {
587 chip->lux_wait_result = false;
588 wake_up(&chip->wait);
589 bh1770_lux_update_thresholds(chip,
590 chip->lux_threshold_hi,
591 chip->lux_threshold_lo);
592 }
593 }
594
595 /* Disable interrupt logic to guarantee acknowledgement */
596 i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
597 (0 << 1) | (0 << 0));
598
599 if ((status & BH1770_INT_ALS_INT))
600 sysfs_notify(&chip->client->dev.kobj, NULL, "lux0_input");
601
602 if (chip->int_mode_prox && (status & BH1770_INT_LEDS_INT)) {
603 rate = prox_rates_ms[chip->prox_rate_threshold];
604 bh1770_prox_read_result(chip);
605 }
606
607 /* Re-enable interrupt logic */
608 i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
609 (chip->int_mode_lux << 1) |
610 (chip->int_mode_prox << 0));
611 mutex_unlock(&chip->mutex);
612
613 /*
614 * Can't cancel work while keeping mutex since the work uses the
615 * same mutex.
616 */
617 if (rate) {
618 /*
619 * Simulate missing no-proximity interrupt 50ms after the
620 * next expected interrupt time.
621 */
622 cancel_delayed_work_sync(&chip->prox_work);
623 schedule_delayed_work(&chip->prox_work,
624 msecs_to_jiffies(rate + 50));
625 }
626 return IRQ_HANDLED;
627 }
628
bh1770_power_state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)629 static ssize_t bh1770_power_state_store(struct device *dev,
630 struct device_attribute *attr,
631 const char *buf, size_t count)
632 {
633 struct bh1770_chip *chip = dev_get_drvdata(dev);
634 unsigned long value;
635 ssize_t ret;
636
637 ret = kstrtoul(buf, 0, &value);
638 if (ret)
639 return ret;
640
641 mutex_lock(&chip->mutex);
642 if (value) {
643 ret = pm_runtime_resume_and_get(dev);
644 if (ret < 0)
645 goto leave;
646
647 ret = bh1770_lux_rate(chip, chip->lux_rate_index);
648 if (ret < 0) {
649 pm_runtime_put(dev);
650 goto leave;
651 }
652
653 ret = bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
654 if (ret < 0) {
655 pm_runtime_put(dev);
656 goto leave;
657 }
658
659 /* This causes interrupt after the next measurement cycle */
660 bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
661 BH1770_LUX_DEF_THRES);
662 /* Inform that we are waiting for a result from ALS */
663 chip->lux_wait_result = true;
664 bh1770_prox_mode_control(chip);
665 } else if (!pm_runtime_suspended(dev)) {
666 pm_runtime_put(dev);
667 }
668 ret = count;
669 leave:
670 mutex_unlock(&chip->mutex);
671 return ret;
672 }
673
bh1770_power_state_show(struct device * dev,struct device_attribute * attr,char * buf)674 static ssize_t bh1770_power_state_show(struct device *dev,
675 struct device_attribute *attr, char *buf)
676 {
677 return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
678 }
679
bh1770_lux_result_show(struct device * dev,struct device_attribute * attr,char * buf)680 static ssize_t bh1770_lux_result_show(struct device *dev,
681 struct device_attribute *attr, char *buf)
682 {
683 struct bh1770_chip *chip = dev_get_drvdata(dev);
684 ssize_t ret;
685 long time_left;
686
687 if (pm_runtime_suspended(dev))
688 return -EIO; /* Chip is not enabled at all */
689
690 time_left = wait_event_interruptible_timeout(chip->wait,
691 !chip->lux_wait_result,
692 msecs_to_jiffies(BH1770_TIMEOUT));
693 if (!time_left)
694 return -EIO;
695
696 mutex_lock(&chip->mutex);
697 ret = sprintf(buf, "%d\n", bh1770_lux_read_result(chip));
698 mutex_unlock(&chip->mutex);
699
700 return ret;
701 }
702
bh1770_lux_range_show(struct device * dev,struct device_attribute * attr,char * buf)703 static ssize_t bh1770_lux_range_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
705 {
706 return sprintf(buf, "%d\n", BH1770_LUX_RANGE);
707 }
708
bh1770_prox_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)709 static ssize_t bh1770_prox_enable_store(struct device *dev,
710 struct device_attribute *attr,
711 const char *buf, size_t count)
712 {
713 struct bh1770_chip *chip = dev_get_drvdata(dev);
714 unsigned long value;
715 int ret;
716
717 ret = kstrtoul(buf, 0, &value);
718 if (ret)
719 return ret;
720
721 mutex_lock(&chip->mutex);
722 /* Assume no proximity. Sensor will tell real state soon */
723 if (!chip->prox_enable_count)
724 chip->prox_data = 0;
725
726 if (value)
727 chip->prox_enable_count++;
728 else if (chip->prox_enable_count > 0)
729 chip->prox_enable_count--;
730 else
731 goto leave;
732
733 /* Run control only when chip is powered on */
734 if (!pm_runtime_suspended(dev))
735 bh1770_prox_mode_control(chip);
736 leave:
737 mutex_unlock(&chip->mutex);
738 return count;
739 }
740
bh1770_prox_enable_show(struct device * dev,struct device_attribute * attr,char * buf)741 static ssize_t bh1770_prox_enable_show(struct device *dev,
742 struct device_attribute *attr, char *buf)
743 {
744 struct bh1770_chip *chip = dev_get_drvdata(dev);
745 ssize_t len;
746
747 mutex_lock(&chip->mutex);
748 len = sprintf(buf, "%d\n", chip->prox_enable_count);
749 mutex_unlock(&chip->mutex);
750 return len;
751 }
752
bh1770_prox_result_show(struct device * dev,struct device_attribute * attr,char * buf)753 static ssize_t bh1770_prox_result_show(struct device *dev,
754 struct device_attribute *attr, char *buf)
755 {
756 struct bh1770_chip *chip = dev_get_drvdata(dev);
757 ssize_t ret;
758
759 mutex_lock(&chip->mutex);
760 if (chip->prox_enable_count && !pm_runtime_suspended(dev))
761 ret = sprintf(buf, "%d\n", chip->prox_data);
762 else
763 ret = -EIO;
764 mutex_unlock(&chip->mutex);
765 return ret;
766 }
767
bh1770_prox_range_show(struct device * dev,struct device_attribute * attr,char * buf)768 static ssize_t bh1770_prox_range_show(struct device *dev,
769 struct device_attribute *attr, char *buf)
770 {
771 return sprintf(buf, "%d\n", BH1770_PROX_RANGE);
772 }
773
bh1770_get_prox_rate_avail(struct device * dev,struct device_attribute * attr,char * buf)774 static ssize_t bh1770_get_prox_rate_avail(struct device *dev,
775 struct device_attribute *attr, char *buf)
776 {
777 int i;
778 int pos = 0;
779 for (i = 0; i < ARRAY_SIZE(prox_rates_hz); i++)
780 pos += sprintf(buf + pos, "%d ", prox_rates_hz[i]);
781 sprintf(buf + pos - 1, "\n");
782 return pos;
783 }
784
bh1770_get_prox_rate_above(struct device * dev,struct device_attribute * attr,char * buf)785 static ssize_t bh1770_get_prox_rate_above(struct device *dev,
786 struct device_attribute *attr, char *buf)
787 {
788 struct bh1770_chip *chip = dev_get_drvdata(dev);
789 return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate_threshold]);
790 }
791
bh1770_get_prox_rate_below(struct device * dev,struct device_attribute * attr,char * buf)792 static ssize_t bh1770_get_prox_rate_below(struct device *dev,
793 struct device_attribute *attr, char *buf)
794 {
795 struct bh1770_chip *chip = dev_get_drvdata(dev);
796 return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate]);
797 }
798
bh1770_prox_rate_validate(int rate)799 static int bh1770_prox_rate_validate(int rate)
800 {
801 int i;
802
803 for (i = 0; i < ARRAY_SIZE(prox_rates_hz) - 1; i++)
804 if (rate >= prox_rates_hz[i])
805 break;
806 return i;
807 }
808
bh1770_set_prox_rate_above(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)809 static ssize_t bh1770_set_prox_rate_above(struct device *dev,
810 struct device_attribute *attr,
811 const char *buf, size_t count)
812 {
813 struct bh1770_chip *chip = dev_get_drvdata(dev);
814 unsigned long value;
815 int ret;
816
817 ret = kstrtoul(buf, 0, &value);
818 if (ret)
819 return ret;
820
821 mutex_lock(&chip->mutex);
822 chip->prox_rate_threshold = bh1770_prox_rate_validate(value);
823 mutex_unlock(&chip->mutex);
824 return count;
825 }
826
bh1770_set_prox_rate_below(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)827 static ssize_t bh1770_set_prox_rate_below(struct device *dev,
828 struct device_attribute *attr,
829 const char *buf, size_t count)
830 {
831 struct bh1770_chip *chip = dev_get_drvdata(dev);
832 unsigned long value;
833 int ret;
834
835 ret = kstrtoul(buf, 0, &value);
836 if (ret)
837 return ret;
838
839 mutex_lock(&chip->mutex);
840 chip->prox_rate = bh1770_prox_rate_validate(value);
841 mutex_unlock(&chip->mutex);
842 return count;
843 }
844
bh1770_get_prox_thres(struct device * dev,struct device_attribute * attr,char * buf)845 static ssize_t bh1770_get_prox_thres(struct device *dev,
846 struct device_attribute *attr, char *buf)
847 {
848 struct bh1770_chip *chip = dev_get_drvdata(dev);
849 return sprintf(buf, "%d\n", chip->prox_threshold);
850 }
851
bh1770_set_prox_thres(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)852 static ssize_t bh1770_set_prox_thres(struct device *dev,
853 struct device_attribute *attr,
854 const char *buf, size_t count)
855 {
856 struct bh1770_chip *chip = dev_get_drvdata(dev);
857 unsigned long value;
858 int ret;
859
860 ret = kstrtoul(buf, 0, &value);
861 if (ret)
862 return ret;
863
864 if (value > BH1770_PROX_RANGE)
865 return -EINVAL;
866
867 mutex_lock(&chip->mutex);
868 chip->prox_threshold = value;
869 ret = bh1770_prox_set_threshold(chip);
870 mutex_unlock(&chip->mutex);
871 if (ret < 0)
872 return ret;
873 return count;
874 }
875
bh1770_prox_persistence_show(struct device * dev,struct device_attribute * attr,char * buf)876 static ssize_t bh1770_prox_persistence_show(struct device *dev,
877 struct device_attribute *attr, char *buf)
878 {
879 struct bh1770_chip *chip = dev_get_drvdata(dev);
880
881 return sprintf(buf, "%u\n", chip->prox_persistence);
882 }
883
bh1770_prox_persistence_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)884 static ssize_t bh1770_prox_persistence_store(struct device *dev,
885 struct device_attribute *attr,
886 const char *buf, size_t len)
887 {
888 struct bh1770_chip *chip = dev_get_drvdata(dev);
889 unsigned long value;
890 int ret;
891
892 ret = kstrtoul(buf, 0, &value);
893 if (ret)
894 return ret;
895
896 if (value > BH1770_PROX_MAX_PERSISTENCE)
897 return -EINVAL;
898
899 chip->prox_persistence = value;
900
901 return len;
902 }
903
bh1770_prox_abs_thres_show(struct device * dev,struct device_attribute * attr,char * buf)904 static ssize_t bh1770_prox_abs_thres_show(struct device *dev,
905 struct device_attribute *attr, char *buf)
906 {
907 struct bh1770_chip *chip = dev_get_drvdata(dev);
908 return sprintf(buf, "%u\n", chip->prox_abs_thres);
909 }
910
bh1770_prox_abs_thres_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)911 static ssize_t bh1770_prox_abs_thres_store(struct device *dev,
912 struct device_attribute *attr,
913 const char *buf, size_t len)
914 {
915 struct bh1770_chip *chip = dev_get_drvdata(dev);
916 unsigned long value;
917 int ret;
918
919 ret = kstrtoul(buf, 0, &value);
920 if (ret)
921 return ret;
922
923 if (value > BH1770_PROX_RANGE)
924 return -EINVAL;
925
926 chip->prox_abs_thres = value;
927
928 return len;
929 }
930
bh1770_chip_id_show(struct device * dev,struct device_attribute * attr,char * buf)931 static ssize_t bh1770_chip_id_show(struct device *dev,
932 struct device_attribute *attr, char *buf)
933 {
934 struct bh1770_chip *chip = dev_get_drvdata(dev);
935 return sprintf(buf, "%s rev %d\n", chip->chipname, chip->revision);
936 }
937
bh1770_lux_calib_default_show(struct device * dev,struct device_attribute * attr,char * buf)938 static ssize_t bh1770_lux_calib_default_show(struct device *dev,
939 struct device_attribute *attr, char *buf)
940 {
941 return sprintf(buf, "%u\n", BH1770_CALIB_SCALER);
942 }
943
bh1770_lux_calib_show(struct device * dev,struct device_attribute * attr,char * buf)944 static ssize_t bh1770_lux_calib_show(struct device *dev,
945 struct device_attribute *attr, char *buf)
946 {
947 struct bh1770_chip *chip = dev_get_drvdata(dev);
948 ssize_t len;
949
950 mutex_lock(&chip->mutex);
951 len = sprintf(buf, "%u\n", chip->lux_calib);
952 mutex_unlock(&chip->mutex);
953 return len;
954 }
955
bh1770_lux_calib_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)956 static ssize_t bh1770_lux_calib_store(struct device *dev,
957 struct device_attribute *attr,
958 const char *buf, size_t len)
959 {
960 struct bh1770_chip *chip = dev_get_drvdata(dev);
961 unsigned long value;
962 u32 old_calib;
963 u32 new_corr;
964 int ret;
965
966 ret = kstrtoul(buf, 0, &value);
967 if (ret)
968 return ret;
969
970 mutex_lock(&chip->mutex);
971 old_calib = chip->lux_calib;
972 chip->lux_calib = value;
973 new_corr = bh1770_get_corr_value(chip);
974 if (new_corr == 0) {
975 chip->lux_calib = old_calib;
976 mutex_unlock(&chip->mutex);
977 return -EINVAL;
978 }
979 chip->lux_corr = new_corr;
980 /* Refresh thresholds on HW after changing correction value */
981 bh1770_lux_update_thresholds(chip, chip->lux_threshold_hi,
982 chip->lux_threshold_lo);
983
984 mutex_unlock(&chip->mutex);
985
986 return len;
987 }
988
bh1770_get_lux_rate_avail(struct device * dev,struct device_attribute * attr,char * buf)989 static ssize_t bh1770_get_lux_rate_avail(struct device *dev,
990 struct device_attribute *attr, char *buf)
991 {
992 int i;
993 int pos = 0;
994 for (i = 0; i < ARRAY_SIZE(lux_rates_hz); i++)
995 pos += sprintf(buf + pos, "%d ", lux_rates_hz[i]);
996 sprintf(buf + pos - 1, "\n");
997 return pos;
998 }
999
bh1770_get_lux_rate(struct device * dev,struct device_attribute * attr,char * buf)1000 static ssize_t bh1770_get_lux_rate(struct device *dev,
1001 struct device_attribute *attr, char *buf)
1002 {
1003 struct bh1770_chip *chip = dev_get_drvdata(dev);
1004 return sprintf(buf, "%d\n", lux_rates_hz[chip->lux_rate_index]);
1005 }
1006
bh1770_set_lux_rate(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1007 static ssize_t bh1770_set_lux_rate(struct device *dev,
1008 struct device_attribute *attr,
1009 const char *buf, size_t count)
1010 {
1011 struct bh1770_chip *chip = dev_get_drvdata(dev);
1012 unsigned long rate_hz;
1013 int ret, i;
1014
1015 ret = kstrtoul(buf, 0, &rate_hz);
1016 if (ret)
1017 return ret;
1018
1019 for (i = 0; i < ARRAY_SIZE(lux_rates_hz) - 1; i++)
1020 if (rate_hz >= lux_rates_hz[i])
1021 break;
1022
1023 mutex_lock(&chip->mutex);
1024 chip->lux_rate_index = i;
1025 ret = bh1770_lux_rate(chip, i);
1026 mutex_unlock(&chip->mutex);
1027
1028 if (ret < 0)
1029 return ret;
1030
1031 return count;
1032 }
1033
bh1770_get_lux_thresh_above(struct device * dev,struct device_attribute * attr,char * buf)1034 static ssize_t bh1770_get_lux_thresh_above(struct device *dev,
1035 struct device_attribute *attr, char *buf)
1036 {
1037 struct bh1770_chip *chip = dev_get_drvdata(dev);
1038 return sprintf(buf, "%d\n", chip->lux_threshold_hi);
1039 }
1040
bh1770_get_lux_thresh_below(struct device * dev,struct device_attribute * attr,char * buf)1041 static ssize_t bh1770_get_lux_thresh_below(struct device *dev,
1042 struct device_attribute *attr, char *buf)
1043 {
1044 struct bh1770_chip *chip = dev_get_drvdata(dev);
1045 return sprintf(buf, "%d\n", chip->lux_threshold_lo);
1046 }
1047
bh1770_set_lux_thresh(struct bh1770_chip * chip,u16 * target,const char * buf)1048 static ssize_t bh1770_set_lux_thresh(struct bh1770_chip *chip, u16 *target,
1049 const char *buf)
1050 {
1051 unsigned long thresh;
1052 int ret;
1053
1054 ret = kstrtoul(buf, 0, &thresh);
1055 if (ret)
1056 return ret;
1057
1058 if (thresh > BH1770_LUX_RANGE)
1059 return -EINVAL;
1060
1061 mutex_lock(&chip->mutex);
1062 *target = thresh;
1063 /*
1064 * Don't update values in HW if we are still waiting for
1065 * first interrupt to come after device handle open call.
1066 */
1067 if (!chip->lux_wait_result)
1068 ret = bh1770_lux_update_thresholds(chip,
1069 chip->lux_threshold_hi,
1070 chip->lux_threshold_lo);
1071 mutex_unlock(&chip->mutex);
1072 return ret;
1073
1074 }
1075
bh1770_set_lux_thresh_above(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)1076 static ssize_t bh1770_set_lux_thresh_above(struct device *dev,
1077 struct device_attribute *attr,
1078 const char *buf, size_t len)
1079 {
1080 struct bh1770_chip *chip = dev_get_drvdata(dev);
1081 int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_hi, buf);
1082 if (ret < 0)
1083 return ret;
1084 return len;
1085 }
1086
bh1770_set_lux_thresh_below(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)1087 static ssize_t bh1770_set_lux_thresh_below(struct device *dev,
1088 struct device_attribute *attr,
1089 const char *buf, size_t len)
1090 {
1091 struct bh1770_chip *chip = dev_get_drvdata(dev);
1092 int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_lo, buf);
1093 if (ret < 0)
1094 return ret;
1095 return len;
1096 }
1097
1098 static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, bh1770_prox_enable_show,
1099 bh1770_prox_enable_store);
1100 static DEVICE_ATTR(prox0_thresh_above1_value, S_IRUGO | S_IWUSR,
1101 bh1770_prox_abs_thres_show,
1102 bh1770_prox_abs_thres_store);
1103 static DEVICE_ATTR(prox0_thresh_above0_value, S_IRUGO | S_IWUSR,
1104 bh1770_get_prox_thres,
1105 bh1770_set_prox_thres);
1106 static DEVICE_ATTR(prox0_raw, S_IRUGO, bh1770_prox_result_show, NULL);
1107 static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, bh1770_prox_range_show, NULL);
1108 static DEVICE_ATTR(prox0_thresh_above_count, S_IRUGO | S_IWUSR,
1109 bh1770_prox_persistence_show,
1110 bh1770_prox_persistence_store);
1111 static DEVICE_ATTR(prox0_rate_above, S_IRUGO | S_IWUSR,
1112 bh1770_get_prox_rate_above,
1113 bh1770_set_prox_rate_above);
1114 static DEVICE_ATTR(prox0_rate_below, S_IRUGO | S_IWUSR,
1115 bh1770_get_prox_rate_below,
1116 bh1770_set_prox_rate_below);
1117 static DEVICE_ATTR(prox0_rate_avail, S_IRUGO, bh1770_get_prox_rate_avail, NULL);
1118
1119 static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, bh1770_lux_calib_show,
1120 bh1770_lux_calib_store);
1121 static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
1122 bh1770_lux_calib_default_show,
1123 NULL);
1124 static DEVICE_ATTR(lux0_input, S_IRUGO, bh1770_lux_result_show, NULL);
1125 static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, bh1770_lux_range_show, NULL);
1126 static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, bh1770_get_lux_rate,
1127 bh1770_set_lux_rate);
1128 static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, bh1770_get_lux_rate_avail, NULL);
1129 static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
1130 bh1770_get_lux_thresh_above,
1131 bh1770_set_lux_thresh_above);
1132 static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
1133 bh1770_get_lux_thresh_below,
1134 bh1770_set_lux_thresh_below);
1135 static DEVICE_ATTR(chip_id, S_IRUGO, bh1770_chip_id_show, NULL);
1136 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, bh1770_power_state_show,
1137 bh1770_power_state_store);
1138
1139
1140 static struct attribute *sysfs_attrs[] = {
1141 &dev_attr_lux0_calibscale.attr,
1142 &dev_attr_lux0_calibscale_default.attr,
1143 &dev_attr_lux0_input.attr,
1144 &dev_attr_lux0_sensor_range.attr,
1145 &dev_attr_lux0_rate.attr,
1146 &dev_attr_lux0_rate_avail.attr,
1147 &dev_attr_lux0_thresh_above_value.attr,
1148 &dev_attr_lux0_thresh_below_value.attr,
1149 &dev_attr_prox0_raw.attr,
1150 &dev_attr_prox0_sensor_range.attr,
1151 &dev_attr_prox0_raw_en.attr,
1152 &dev_attr_prox0_thresh_above_count.attr,
1153 &dev_attr_prox0_rate_above.attr,
1154 &dev_attr_prox0_rate_below.attr,
1155 &dev_attr_prox0_rate_avail.attr,
1156 &dev_attr_prox0_thresh_above0_value.attr,
1157 &dev_attr_prox0_thresh_above1_value.attr,
1158 &dev_attr_chip_id.attr,
1159 &dev_attr_power_state.attr,
1160 NULL
1161 };
1162
1163 static const struct attribute_group bh1770_attribute_group = {
1164 .attrs = sysfs_attrs
1165 };
1166
bh1770_probe(struct i2c_client * client)1167 static int bh1770_probe(struct i2c_client *client)
1168 {
1169 struct bh1770_chip *chip;
1170 int err;
1171
1172 chip = devm_kzalloc(&client->dev, sizeof *chip, GFP_KERNEL);
1173 if (!chip)
1174 return -ENOMEM;
1175
1176 i2c_set_clientdata(client, chip);
1177 chip->client = client;
1178
1179 mutex_init(&chip->mutex);
1180 init_waitqueue_head(&chip->wait);
1181 INIT_DELAYED_WORK(&chip->prox_work, bh1770_prox_work);
1182
1183 if (client->dev.platform_data == NULL) {
1184 dev_err(&client->dev, "platform data is mandatory\n");
1185 return -EINVAL;
1186 }
1187
1188 chip->pdata = client->dev.platform_data;
1189 chip->lux_calib = BH1770_LUX_NEUTRAL_CALIB_VALUE;
1190 chip->lux_rate_index = BH1770_LUX_DEFAULT_RATE;
1191 chip->lux_threshold_lo = BH1770_LUX_DEF_THRES;
1192 chip->lux_threshold_hi = BH1770_LUX_DEF_THRES;
1193
1194 if (chip->pdata->glass_attenuation == 0)
1195 chip->lux_ga = BH1770_NEUTRAL_GA;
1196 else
1197 chip->lux_ga = chip->pdata->glass_attenuation;
1198
1199 chip->prox_threshold = BH1770_PROX_DEF_THRES;
1200 chip->prox_led = chip->pdata->led_def_curr;
1201 chip->prox_abs_thres = BH1770_PROX_DEF_ABS_THRES;
1202 chip->prox_persistence = BH1770_DEFAULT_PERSISTENCE;
1203 chip->prox_rate_threshold = BH1770_PROX_DEF_RATE_THRESH;
1204 chip->prox_rate = BH1770_PROX_DEFAULT_RATE;
1205 chip->prox_data = 0;
1206
1207 chip->regs[0].supply = reg_vcc;
1208 chip->regs[1].supply = reg_vleds;
1209
1210 err = devm_regulator_bulk_get(&client->dev,
1211 ARRAY_SIZE(chip->regs), chip->regs);
1212 if (err < 0) {
1213 dev_err(&client->dev, "Cannot get regulators\n");
1214 return err;
1215 }
1216
1217 err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
1218 chip->regs);
1219 if (err < 0) {
1220 dev_err(&client->dev, "Cannot enable regulators\n");
1221 return err;
1222 }
1223
1224 usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
1225 err = bh1770_detect(chip);
1226 if (err < 0)
1227 goto fail0;
1228
1229 /* Start chip */
1230 bh1770_chip_on(chip);
1231 pm_runtime_set_active(&client->dev);
1232 pm_runtime_enable(&client->dev);
1233
1234 chip->lux_corr = bh1770_get_corr_value(chip);
1235 if (chip->lux_corr == 0) {
1236 dev_err(&client->dev, "Improper correction values\n");
1237 err = -EINVAL;
1238 goto fail0;
1239 }
1240
1241 if (chip->pdata->setup_resources) {
1242 err = chip->pdata->setup_resources();
1243 if (err) {
1244 err = -EINVAL;
1245 goto fail0;
1246 }
1247 }
1248
1249 err = sysfs_create_group(&chip->client->dev.kobj,
1250 &bh1770_attribute_group);
1251 if (err < 0) {
1252 dev_err(&chip->client->dev, "Sysfs registration failed\n");
1253 goto fail1;
1254 }
1255
1256 /*
1257 * Chip needs level triggered interrupt to work. However,
1258 * level triggering doesn't work always correctly with power
1259 * management. Select both
1260 */
1261 err = request_threaded_irq(client->irq, NULL,
1262 bh1770_irq,
1263 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
1264 IRQF_TRIGGER_LOW,
1265 "bh1770", chip);
1266 if (err) {
1267 dev_err(&client->dev, "could not get IRQ %d\n",
1268 client->irq);
1269 goto fail2;
1270 }
1271 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1272 return err;
1273 fail2:
1274 sysfs_remove_group(&chip->client->dev.kobj,
1275 &bh1770_attribute_group);
1276 fail1:
1277 if (chip->pdata->release_resources)
1278 chip->pdata->release_resources();
1279 fail0:
1280 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1281 return err;
1282 }
1283
bh1770_remove(struct i2c_client * client)1284 static void bh1770_remove(struct i2c_client *client)
1285 {
1286 struct bh1770_chip *chip = i2c_get_clientdata(client);
1287
1288 free_irq(client->irq, chip);
1289
1290 sysfs_remove_group(&chip->client->dev.kobj,
1291 &bh1770_attribute_group);
1292
1293 if (chip->pdata->release_resources)
1294 chip->pdata->release_resources();
1295
1296 cancel_delayed_work_sync(&chip->prox_work);
1297
1298 if (!pm_runtime_suspended(&client->dev))
1299 bh1770_chip_off(chip);
1300
1301 pm_runtime_disable(&client->dev);
1302 pm_runtime_set_suspended(&client->dev);
1303 }
1304
1305 #ifdef CONFIG_PM_SLEEP
bh1770_suspend(struct device * dev)1306 static int bh1770_suspend(struct device *dev)
1307 {
1308 struct i2c_client *client = to_i2c_client(dev);
1309 struct bh1770_chip *chip = i2c_get_clientdata(client);
1310
1311 bh1770_chip_off(chip);
1312
1313 return 0;
1314 }
1315
bh1770_resume(struct device * dev)1316 static int bh1770_resume(struct device *dev)
1317 {
1318 struct i2c_client *client = to_i2c_client(dev);
1319 struct bh1770_chip *chip = i2c_get_clientdata(client);
1320 int ret = 0;
1321
1322 bh1770_chip_on(chip);
1323
1324 if (!pm_runtime_suspended(dev)) {
1325 /*
1326 * If we were enabled at suspend time, it is expected
1327 * everything works nice and smoothly
1328 */
1329 ret = bh1770_lux_rate(chip, chip->lux_rate_index);
1330 ret |= bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
1331
1332 /* This causes interrupt after the next measurement cycle */
1333 bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
1334 BH1770_LUX_DEF_THRES);
1335 /* Inform that we are waiting for a result from ALS */
1336 chip->lux_wait_result = true;
1337 bh1770_prox_mode_control(chip);
1338 }
1339 return ret;
1340 }
1341 #endif
1342
1343 #ifdef CONFIG_PM
bh1770_runtime_suspend(struct device * dev)1344 static int bh1770_runtime_suspend(struct device *dev)
1345 {
1346 struct i2c_client *client = to_i2c_client(dev);
1347 struct bh1770_chip *chip = i2c_get_clientdata(client);
1348
1349 bh1770_chip_off(chip);
1350
1351 return 0;
1352 }
1353
bh1770_runtime_resume(struct device * dev)1354 static int bh1770_runtime_resume(struct device *dev)
1355 {
1356 struct i2c_client *client = to_i2c_client(dev);
1357 struct bh1770_chip *chip = i2c_get_clientdata(client);
1358
1359 bh1770_chip_on(chip);
1360
1361 return 0;
1362 }
1363 #endif
1364
1365 static const struct i2c_device_id bh1770_id[] = {
1366 { "bh1770glc" },
1367 { "sfh7770" },
1368 {}
1369 };
1370
1371 MODULE_DEVICE_TABLE(i2c, bh1770_id);
1372
1373 static const struct dev_pm_ops bh1770_pm_ops = {
1374 SET_SYSTEM_SLEEP_PM_OPS(bh1770_suspend, bh1770_resume)
1375 SET_RUNTIME_PM_OPS(bh1770_runtime_suspend, bh1770_runtime_resume, NULL)
1376 };
1377
1378 static struct i2c_driver bh1770_driver = {
1379 .driver = {
1380 .name = "bh1770glc",
1381 .pm = &bh1770_pm_ops,
1382 },
1383 .probe = bh1770_probe,
1384 .remove = bh1770_remove,
1385 .id_table = bh1770_id,
1386 };
1387
1388 module_i2c_driver(bh1770_driver);
1389
1390 MODULE_DESCRIPTION("BH1770GLC / SFH7770 combined ALS and proximity sensor");
1391 MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1392 MODULE_LICENSE("GPL v2");
1393