1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of the ROHM BH1770GLC / OSRAM SFH7770 sensor driver.
4 * Chip is combined proximity and ambient light sensor.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
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>
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 */
49 #define BH1770_MANUFACT_ROHM 0x01
50 #define BH1770_MANUFACT_OSRAM 0x03
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
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)
66 #define BH1770_PS_TRIG_MEAS (1 << 0)
67 #define BH1770_ALS_TRIG_MEAS (1 << 1)
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)
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)
87 #define BH1770_LED1 0x00
89 #define BH1770_DISABLE 0
90 #define BH1770_ENABLE 1
91 #define BH1770_PROX_CHANNELS 1
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 */
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
115 #define PROX_ABOVE_THRESHOLD 1
116 #define PROX_BELOW_THRESHOLD 0
118 #define PROX_IGNORE_LUX_LIMIT 500
121 struct bh1770_platform_data *pdata;
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;
131 struct delayed_work prox_work;
132 u32 lux_cf; /* Chip specific factor */
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;
144 int prox_enable_count;
148 int prox_rate_threshold;
150 u8 prox_persistence_counter;
153 u8 prox_threshold_hw;
154 bool prox_force_update;
159 static const char reg_vcc[] = "Vcc";
160 static const char reg_vleds[] = "Vleds";
163 * Supported stand alone rates in ms from chip data sheet
164 * {10, 20, 30, 40, 70, 100, 200, 500, 1000, 2000};
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};
170 * Supported stand alone rates in ms from chip data sheet
171 * {100, 200, 500, 1000, 2000};
173 static const s16 lux_rates_hz[] = {10, 5, 2, 1, 0};
176 * interrupt control functions are called while keeping chip->mutex
177 * excluding module probe / remove
179 static inline int bh1770_lux_interrupt_control(struct bh1770_chip *chip,
182 chip->int_mode_lux = lux;
183 /* Set interrupt modes, interrupt active low, latched */
184 return i2c_smbus_write_byte_data(chip->client,
186 (lux << 1) | chip->int_mode_prox);
189 static inline int bh1770_prox_interrupt_control(struct bh1770_chip *chip,
192 chip->int_mode_prox = ps;
193 return i2c_smbus_write_byte_data(chip->client,
195 (chip->int_mode_lux << 1) | (ps << 0));
198 /* chip->mutex is always kept here */
199 static int bh1770_lux_rate(struct bh1770_chip *chip, int rate_index)
201 /* sysfs may call this when the chip is powered off */
202 if (pm_runtime_suspended(&chip->client->dev))
205 /* Proper proximity response needs fastest lux rate (100ms) */
206 if (chip->prox_enable_count)
209 return i2c_smbus_write_byte_data(chip->client,
210 BH1770_ALS_MEAS_RATE,
214 static int bh1770_prox_rate(struct bh1770_chip *chip, int mode)
218 rate = (mode == PROX_ABOVE_THRESHOLD) ?
219 chip->prox_rate_threshold : chip->prox_rate;
221 return i2c_smbus_write_byte_data(chip->client,
226 /* InfraredLED is controlled by the chip during proximity scanning */
227 static inline int bh1770_led_cfg(struct bh1770_chip *chip)
229 /* LED cfg, current for leds 1 and 2 */
230 return i2c_smbus_write_byte_data(chip->client,
233 (BH1770_LED_5mA << 3) |
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
243 static inline u8 bh1770_psraw_to_adjusted(struct bh1770_chip *chip, u8 psraw)
246 adjusted = (u16)(((u32)(psraw + chip->prox_const) * chip->prox_coef) /
248 if (adjusted > BH1770_PROX_RANGE)
249 adjusted = BH1770_PROX_RANGE;
253 static inline u8 bh1770_psadjusted_to_raw(struct bh1770_chip *chip, u8 ps)
257 raw = (((u32)ps * BH1770_COEF_SCALER) / chip->prox_coef);
258 if (raw > chip->prox_const)
259 raw = raw - chip->prox_const;
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.
271 static int bh1770_prox_set_threshold(struct bh1770_chip *chip)
275 /* sysfs may call this when the chip is powered off */
276 if (pm_runtime_suspended(&chip->client->dev))
279 tmp = bh1770_psadjusted_to_raw(chip, chip->prox_threshold);
280 chip->prox_threshold_hw = tmp;
282 return i2c_smbus_write_byte_data(chip->client, BH1770_PS_TH_LED1,
286 static inline u16 bh1770_lux_raw_to_adjusted(struct bh1770_chip *chip, u16 raw)
289 lux = ((u32)raw * chip->lux_corr) / BH1770_LUX_CORR_SCALE;
290 return min(lux, (u32)BH1770_LUX_RANGE);
293 static inline u16 bh1770_lux_adjusted_to_raw(struct bh1770_chip *chip,
296 return (u32)adjusted * BH1770_LUX_CORR_SCALE / chip->lux_corr;
299 /* chip->mutex is kept when this is called */
300 static int bh1770_lux_update_thresholds(struct bh1770_chip *chip,
301 u16 threshold_hi, u16 threshold_lo)
306 /* sysfs may call this when the chip is powered off */
307 if (pm_runtime_suspended(&chip->client->dev))
311 * Compensate threshold values with the correction factors if not
312 * set to minimum or maximum.
313 * Min & max values disables interrupts.
315 if (threshold_hi != BH1770_LUX_RANGE && threshold_hi != 0)
316 threshold_hi = bh1770_lux_adjusted_to_raw(chip, threshold_hi);
318 if (threshold_lo != BH1770_LUX_RANGE && threshold_lo != 0)
319 threshold_lo = bh1770_lux_adjusted_to_raw(chip, threshold_lo);
321 if (chip->lux_thres_hi_onchip == threshold_hi &&
322 chip->lux_thres_lo_onchip == threshold_lo)
325 chip->lux_thres_hi_onchip = threshold_hi;
326 chip->lux_thres_lo_onchip = threshold_lo;
328 data[0] = threshold_hi;
329 data[1] = threshold_hi >> 8;
330 data[2] = threshold_lo;
331 data[3] = threshold_lo >> 8;
333 ret = i2c_smbus_write_i2c_block_data(chip->client,
340 static int bh1770_lux_get_result(struct bh1770_chip *chip)
345 ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_0);
350 ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_1);
354 chip->lux_data_raw = data | ((ret & 0xff) << 8);
359 /* Calculate correction value which contains chip and device specific parts */
360 static u32 bh1770_get_corr_value(struct bh1770_chip *chip)
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;
372 static int bh1770_lux_read_result(struct bh1770_chip *chip)
374 bh1770_lux_get_result(chip);
375 return bh1770_lux_raw_to_adjusted(chip, chip->lux_data_raw);
379 * Chip on / off functions are called while keeping mutex except probe
382 static int bh1770_chip_on(struct bh1770_chip *chip)
384 int ret = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
389 usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
392 i2c_smbus_write_byte_data(chip->client, BH1770_ALS_CONTROL,
394 usleep_range(BH1770_RESET_TIME, BH1770_RESET_TIME * 2);
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.
401 chip->lux_data_raw = 0;
403 ret = i2c_smbus_write_byte_data(chip->client,
404 BH1770_ALS_CONTROL, BH1770_STANDALONE);
406 /* Assume reset defaults */
407 chip->lux_thres_hi_onchip = BH1770_LUX_RANGE;
408 chip->lux_thres_lo_onchip = 0;
413 static void bh1770_chip_off(struct bh1770_chip *chip)
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);
424 /* chip->mutex is kept when this is called */
425 static int bh1770_prox_mode_control(struct bh1770_chip *chip)
427 if (chip->prox_enable_count) {
428 chip->prox_force_update = true; /* Force immediate update */
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);
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);
447 /* chip->mutex is kept when this is called */
448 static int bh1770_prox_read_result(struct bh1770_chip *chip)
454 ret = i2c_smbus_read_byte_data(chip->client, BH1770_PS_DATA_LED1);
458 if (ret > chip->prox_threshold_hw)
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.
468 if (chip->lux_data_raw > PROX_IGNORE_LUX_LIMIT)
471 chip->prox_data = bh1770_psraw_to_adjusted(chip, ret);
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;
478 chip->prox_force_update = false;
480 /* Persistence filttering to reduce false proximity events */
482 if (chip->prox_persistence_counter < chip->prox_persistence) {
483 chip->prox_persistence_counter++;
486 mode = PROX_ABOVE_THRESHOLD;
490 chip->prox_persistence_counter = 0;
491 mode = PROX_BELOW_THRESHOLD;
496 /* Set proximity detection rate based on above or below value */
498 bh1770_prox_rate(chip, mode);
499 sysfs_notify(&chip->client->dev.kobj, NULL, "prox0_raw");
505 static int bh1770_detect(struct bh1770_chip *chip)
507 struct i2c_client *client = chip->client;
511 ret = i2c_smbus_read_byte_data(client, BH1770_MANUFACT_ID);
516 ret = i2c_smbus_read_byte_data(client, BH1770_PART_ID);
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;
526 if ((manu == BH1770_MANUFACT_ROHM) &&
527 ((part & BH1770_PART_MASK) == BH1770_PART)) {
528 snprintf(chip->chipname, sizeof(chip->chipname), "BH1770GLC");
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;
543 dev_dbg(&client->dev, "BH1770 or SFH7770 not found\n");
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.
557 static void bh1770_prox_work(struct work_struct *work)
559 struct bh1770_chip *chip =
560 container_of(work, struct bh1770_chip, prox_work.work);
562 mutex_lock(&chip->mutex);
563 bh1770_prox_read_result(chip);
564 mutex_unlock(&chip->mutex);
567 /* This is threaded irq handler */
568 static irqreturn_t bh1770_irq(int irq, void *data)
570 struct bh1770_chip *chip = data;
574 mutex_lock(&chip->mutex);
575 status = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_PS_STATUS);
577 /* Acknowledge interrupt by reading this register */
578 i2c_smbus_read_byte_data(chip->client, BH1770_INTERRUPT);
581 * Check if there is fresh data available for als.
582 * If this is the very first data, update thresholds after that.
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);
595 /* Disable interrupt logic to guarantee acknowledgement */
596 i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
597 (0 << 1) | (0 << 0));
599 if ((status & BH1770_INT_ALS_INT))
600 sysfs_notify(&chip->client->dev.kobj, NULL, "lux0_input");
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);
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);
614 * Can't cancel work while keeping mutex since the work uses the
619 * Simulate missing no-proximity interrupt 50ms after the
620 * next expected interrupt time.
622 cancel_delayed_work_sync(&chip->prox_work);
623 schedule_delayed_work(&chip->prox_work,
624 msecs_to_jiffies(rate + 50));
629 static ssize_t bh1770_power_state_store(struct device *dev,
630 struct device_attribute *attr,
631 const char *buf, size_t count)
633 struct bh1770_chip *chip = dev_get_drvdata(dev);
637 ret = kstrtoul(buf, 0, &value);
641 mutex_lock(&chip->mutex);
643 pm_runtime_get_sync(dev);
645 ret = bh1770_lux_rate(chip, chip->lux_rate_index);
651 ret = bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
657 /* This causes interrupt after the next measurement cycle */
658 bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
659 BH1770_LUX_DEF_THRES);
660 /* Inform that we are waiting for a result from ALS */
661 chip->lux_wait_result = true;
662 bh1770_prox_mode_control(chip);
663 } else if (!pm_runtime_suspended(dev)) {
668 mutex_unlock(&chip->mutex);
672 static ssize_t bh1770_power_state_show(struct device *dev,
673 struct device_attribute *attr, char *buf)
675 return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
678 static ssize_t bh1770_lux_result_show(struct device *dev,
679 struct device_attribute *attr, char *buf)
681 struct bh1770_chip *chip = dev_get_drvdata(dev);
685 if (pm_runtime_suspended(dev))
686 return -EIO; /* Chip is not enabled at all */
688 timeout = wait_event_interruptible_timeout(chip->wait,
689 !chip->lux_wait_result,
690 msecs_to_jiffies(BH1770_TIMEOUT));
694 mutex_lock(&chip->mutex);
695 ret = sprintf(buf, "%d\n", bh1770_lux_read_result(chip));
696 mutex_unlock(&chip->mutex);
701 static ssize_t bh1770_lux_range_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
704 return sprintf(buf, "%d\n", BH1770_LUX_RANGE);
707 static ssize_t bh1770_prox_enable_store(struct device *dev,
708 struct device_attribute *attr,
709 const char *buf, size_t count)
711 struct bh1770_chip *chip = dev_get_drvdata(dev);
715 ret = kstrtoul(buf, 0, &value);
719 mutex_lock(&chip->mutex);
720 /* Assume no proximity. Sensor will tell real state soon */
721 if (!chip->prox_enable_count)
725 chip->prox_enable_count++;
726 else if (chip->prox_enable_count > 0)
727 chip->prox_enable_count--;
731 /* Run control only when chip is powered on */
732 if (!pm_runtime_suspended(dev))
733 bh1770_prox_mode_control(chip);
735 mutex_unlock(&chip->mutex);
739 static ssize_t bh1770_prox_enable_show(struct device *dev,
740 struct device_attribute *attr, char *buf)
742 struct bh1770_chip *chip = dev_get_drvdata(dev);
745 mutex_lock(&chip->mutex);
746 len = sprintf(buf, "%d\n", chip->prox_enable_count);
747 mutex_unlock(&chip->mutex);
751 static ssize_t bh1770_prox_result_show(struct device *dev,
752 struct device_attribute *attr, char *buf)
754 struct bh1770_chip *chip = dev_get_drvdata(dev);
757 mutex_lock(&chip->mutex);
758 if (chip->prox_enable_count && !pm_runtime_suspended(dev))
759 ret = sprintf(buf, "%d\n", chip->prox_data);
762 mutex_unlock(&chip->mutex);
766 static ssize_t bh1770_prox_range_show(struct device *dev,
767 struct device_attribute *attr, char *buf)
769 return sprintf(buf, "%d\n", BH1770_PROX_RANGE);
772 static ssize_t bh1770_get_prox_rate_avail(struct device *dev,
773 struct device_attribute *attr, char *buf)
777 for (i = 0; i < ARRAY_SIZE(prox_rates_hz); i++)
778 pos += sprintf(buf + pos, "%d ", prox_rates_hz[i]);
779 sprintf(buf + pos - 1, "\n");
783 static ssize_t bh1770_get_prox_rate_above(struct device *dev,
784 struct device_attribute *attr, char *buf)
786 struct bh1770_chip *chip = dev_get_drvdata(dev);
787 return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate_threshold]);
790 static ssize_t bh1770_get_prox_rate_below(struct device *dev,
791 struct device_attribute *attr, char *buf)
793 struct bh1770_chip *chip = dev_get_drvdata(dev);
794 return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate]);
797 static int bh1770_prox_rate_validate(int rate)
801 for (i = 0; i < ARRAY_SIZE(prox_rates_hz) - 1; i++)
802 if (rate >= prox_rates_hz[i])
807 static ssize_t bh1770_set_prox_rate_above(struct device *dev,
808 struct device_attribute *attr,
809 const char *buf, size_t count)
811 struct bh1770_chip *chip = dev_get_drvdata(dev);
815 ret = kstrtoul(buf, 0, &value);
819 mutex_lock(&chip->mutex);
820 chip->prox_rate_threshold = bh1770_prox_rate_validate(value);
821 mutex_unlock(&chip->mutex);
825 static ssize_t bh1770_set_prox_rate_below(struct device *dev,
826 struct device_attribute *attr,
827 const char *buf, size_t count)
829 struct bh1770_chip *chip = dev_get_drvdata(dev);
833 ret = kstrtoul(buf, 0, &value);
837 mutex_lock(&chip->mutex);
838 chip->prox_rate = bh1770_prox_rate_validate(value);
839 mutex_unlock(&chip->mutex);
843 static ssize_t bh1770_get_prox_thres(struct device *dev,
844 struct device_attribute *attr, char *buf)
846 struct bh1770_chip *chip = dev_get_drvdata(dev);
847 return sprintf(buf, "%d\n", chip->prox_threshold);
850 static ssize_t bh1770_set_prox_thres(struct device *dev,
851 struct device_attribute *attr,
852 const char *buf, size_t count)
854 struct bh1770_chip *chip = dev_get_drvdata(dev);
858 ret = kstrtoul(buf, 0, &value);
862 if (value > BH1770_PROX_RANGE)
865 mutex_lock(&chip->mutex);
866 chip->prox_threshold = value;
867 ret = bh1770_prox_set_threshold(chip);
868 mutex_unlock(&chip->mutex);
874 static ssize_t bh1770_prox_persistence_show(struct device *dev,
875 struct device_attribute *attr, char *buf)
877 struct bh1770_chip *chip = dev_get_drvdata(dev);
879 return sprintf(buf, "%u\n", chip->prox_persistence);
882 static ssize_t bh1770_prox_persistence_store(struct device *dev,
883 struct device_attribute *attr,
884 const char *buf, size_t len)
886 struct bh1770_chip *chip = dev_get_drvdata(dev);
890 ret = kstrtoul(buf, 0, &value);
894 if (value > BH1770_PROX_MAX_PERSISTENCE)
897 chip->prox_persistence = value;
902 static ssize_t bh1770_prox_abs_thres_show(struct device *dev,
903 struct device_attribute *attr, char *buf)
905 struct bh1770_chip *chip = dev_get_drvdata(dev);
906 return sprintf(buf, "%u\n", chip->prox_abs_thres);
909 static ssize_t bh1770_prox_abs_thres_store(struct device *dev,
910 struct device_attribute *attr,
911 const char *buf, size_t len)
913 struct bh1770_chip *chip = dev_get_drvdata(dev);
917 ret = kstrtoul(buf, 0, &value);
921 if (value > BH1770_PROX_RANGE)
924 chip->prox_abs_thres = value;
929 static ssize_t bh1770_chip_id_show(struct device *dev,
930 struct device_attribute *attr, char *buf)
932 struct bh1770_chip *chip = dev_get_drvdata(dev);
933 return sprintf(buf, "%s rev %d\n", chip->chipname, chip->revision);
936 static ssize_t bh1770_lux_calib_default_show(struct device *dev,
937 struct device_attribute *attr, char *buf)
939 return sprintf(buf, "%u\n", BH1770_CALIB_SCALER);
942 static ssize_t bh1770_lux_calib_show(struct device *dev,
943 struct device_attribute *attr, char *buf)
945 struct bh1770_chip *chip = dev_get_drvdata(dev);
948 mutex_lock(&chip->mutex);
949 len = sprintf(buf, "%u\n", chip->lux_calib);
950 mutex_unlock(&chip->mutex);
954 static ssize_t bh1770_lux_calib_store(struct device *dev,
955 struct device_attribute *attr,
956 const char *buf, size_t len)
958 struct bh1770_chip *chip = dev_get_drvdata(dev);
964 ret = kstrtoul(buf, 0, &value);
968 mutex_lock(&chip->mutex);
969 old_calib = chip->lux_calib;
970 chip->lux_calib = value;
971 new_corr = bh1770_get_corr_value(chip);
973 chip->lux_calib = old_calib;
974 mutex_unlock(&chip->mutex);
977 chip->lux_corr = new_corr;
978 /* Refresh thresholds on HW after changing correction value */
979 bh1770_lux_update_thresholds(chip, chip->lux_threshold_hi,
980 chip->lux_threshold_lo);
982 mutex_unlock(&chip->mutex);
987 static ssize_t bh1770_get_lux_rate_avail(struct device *dev,
988 struct device_attribute *attr, char *buf)
992 for (i = 0; i < ARRAY_SIZE(lux_rates_hz); i++)
993 pos += sprintf(buf + pos, "%d ", lux_rates_hz[i]);
994 sprintf(buf + pos - 1, "\n");
998 static ssize_t bh1770_get_lux_rate(struct device *dev,
999 struct device_attribute *attr, char *buf)
1001 struct bh1770_chip *chip = dev_get_drvdata(dev);
1002 return sprintf(buf, "%d\n", lux_rates_hz[chip->lux_rate_index]);
1005 static ssize_t bh1770_set_lux_rate(struct device *dev,
1006 struct device_attribute *attr,
1007 const char *buf, size_t count)
1009 struct bh1770_chip *chip = dev_get_drvdata(dev);
1010 unsigned long rate_hz;
1013 ret = kstrtoul(buf, 0, &rate_hz);
1017 for (i = 0; i < ARRAY_SIZE(lux_rates_hz) - 1; i++)
1018 if (rate_hz >= lux_rates_hz[i])
1021 mutex_lock(&chip->mutex);
1022 chip->lux_rate_index = i;
1023 ret = bh1770_lux_rate(chip, i);
1024 mutex_unlock(&chip->mutex);
1032 static ssize_t bh1770_get_lux_thresh_above(struct device *dev,
1033 struct device_attribute *attr, char *buf)
1035 struct bh1770_chip *chip = dev_get_drvdata(dev);
1036 return sprintf(buf, "%d\n", chip->lux_threshold_hi);
1039 static ssize_t bh1770_get_lux_thresh_below(struct device *dev,
1040 struct device_attribute *attr, char *buf)
1042 struct bh1770_chip *chip = dev_get_drvdata(dev);
1043 return sprintf(buf, "%d\n", chip->lux_threshold_lo);
1046 static ssize_t bh1770_set_lux_thresh(struct bh1770_chip *chip, u16 *target,
1049 unsigned long thresh;
1052 ret = kstrtoul(buf, 0, &thresh);
1056 if (thresh > BH1770_LUX_RANGE)
1059 mutex_lock(&chip->mutex);
1062 * Don't update values in HW if we are still waiting for
1063 * first interrupt to come after device handle open call.
1065 if (!chip->lux_wait_result)
1066 ret = bh1770_lux_update_thresholds(chip,
1067 chip->lux_threshold_hi,
1068 chip->lux_threshold_lo);
1069 mutex_unlock(&chip->mutex);
1074 static ssize_t bh1770_set_lux_thresh_above(struct device *dev,
1075 struct device_attribute *attr,
1076 const char *buf, size_t len)
1078 struct bh1770_chip *chip = dev_get_drvdata(dev);
1079 int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_hi, buf);
1085 static ssize_t bh1770_set_lux_thresh_below(struct device *dev,
1086 struct device_attribute *attr,
1087 const char *buf, size_t len)
1089 struct bh1770_chip *chip = dev_get_drvdata(dev);
1090 int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_lo, buf);
1096 static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, bh1770_prox_enable_show,
1097 bh1770_prox_enable_store);
1098 static DEVICE_ATTR(prox0_thresh_above1_value, S_IRUGO | S_IWUSR,
1099 bh1770_prox_abs_thres_show,
1100 bh1770_prox_abs_thres_store);
1101 static DEVICE_ATTR(prox0_thresh_above0_value, S_IRUGO | S_IWUSR,
1102 bh1770_get_prox_thres,
1103 bh1770_set_prox_thres);
1104 static DEVICE_ATTR(prox0_raw, S_IRUGO, bh1770_prox_result_show, NULL);
1105 static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, bh1770_prox_range_show, NULL);
1106 static DEVICE_ATTR(prox0_thresh_above_count, S_IRUGO | S_IWUSR,
1107 bh1770_prox_persistence_show,
1108 bh1770_prox_persistence_store);
1109 static DEVICE_ATTR(prox0_rate_above, S_IRUGO | S_IWUSR,
1110 bh1770_get_prox_rate_above,
1111 bh1770_set_prox_rate_above);
1112 static DEVICE_ATTR(prox0_rate_below, S_IRUGO | S_IWUSR,
1113 bh1770_get_prox_rate_below,
1114 bh1770_set_prox_rate_below);
1115 static DEVICE_ATTR(prox0_rate_avail, S_IRUGO, bh1770_get_prox_rate_avail, NULL);
1117 static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, bh1770_lux_calib_show,
1118 bh1770_lux_calib_store);
1119 static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
1120 bh1770_lux_calib_default_show,
1122 static DEVICE_ATTR(lux0_input, S_IRUGO, bh1770_lux_result_show, NULL);
1123 static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, bh1770_lux_range_show, NULL);
1124 static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, bh1770_get_lux_rate,
1125 bh1770_set_lux_rate);
1126 static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, bh1770_get_lux_rate_avail, NULL);
1127 static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
1128 bh1770_get_lux_thresh_above,
1129 bh1770_set_lux_thresh_above);
1130 static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
1131 bh1770_get_lux_thresh_below,
1132 bh1770_set_lux_thresh_below);
1133 static DEVICE_ATTR(chip_id, S_IRUGO, bh1770_chip_id_show, NULL);
1134 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, bh1770_power_state_show,
1135 bh1770_power_state_store);
1138 static struct attribute *sysfs_attrs[] = {
1139 &dev_attr_lux0_calibscale.attr,
1140 &dev_attr_lux0_calibscale_default.attr,
1141 &dev_attr_lux0_input.attr,
1142 &dev_attr_lux0_sensor_range.attr,
1143 &dev_attr_lux0_rate.attr,
1144 &dev_attr_lux0_rate_avail.attr,
1145 &dev_attr_lux0_thresh_above_value.attr,
1146 &dev_attr_lux0_thresh_below_value.attr,
1147 &dev_attr_prox0_raw.attr,
1148 &dev_attr_prox0_sensor_range.attr,
1149 &dev_attr_prox0_raw_en.attr,
1150 &dev_attr_prox0_thresh_above_count.attr,
1151 &dev_attr_prox0_rate_above.attr,
1152 &dev_attr_prox0_rate_below.attr,
1153 &dev_attr_prox0_rate_avail.attr,
1154 &dev_attr_prox0_thresh_above0_value.attr,
1155 &dev_attr_prox0_thresh_above1_value.attr,
1156 &dev_attr_chip_id.attr,
1157 &dev_attr_power_state.attr,
1161 static const struct attribute_group bh1770_attribute_group = {
1162 .attrs = sysfs_attrs
1165 static int bh1770_probe(struct i2c_client *client,
1166 const struct i2c_device_id *id)
1168 struct bh1770_chip *chip;
1171 chip = devm_kzalloc(&client->dev, sizeof *chip, GFP_KERNEL);
1175 i2c_set_clientdata(client, chip);
1176 chip->client = client;
1178 mutex_init(&chip->mutex);
1179 init_waitqueue_head(&chip->wait);
1180 INIT_DELAYED_WORK(&chip->prox_work, bh1770_prox_work);
1182 if (client->dev.platform_data == NULL) {
1183 dev_err(&client->dev, "platform data is mandatory\n");
1187 chip->pdata = client->dev.platform_data;
1188 chip->lux_calib = BH1770_LUX_NEUTRAL_CALIB_VALUE;
1189 chip->lux_rate_index = BH1770_LUX_DEFAULT_RATE;
1190 chip->lux_threshold_lo = BH1770_LUX_DEF_THRES;
1191 chip->lux_threshold_hi = BH1770_LUX_DEF_THRES;
1193 if (chip->pdata->glass_attenuation == 0)
1194 chip->lux_ga = BH1770_NEUTRAL_GA;
1196 chip->lux_ga = chip->pdata->glass_attenuation;
1198 chip->prox_threshold = BH1770_PROX_DEF_THRES;
1199 chip->prox_led = chip->pdata->led_def_curr;
1200 chip->prox_abs_thres = BH1770_PROX_DEF_ABS_THRES;
1201 chip->prox_persistence = BH1770_DEFAULT_PERSISTENCE;
1202 chip->prox_rate_threshold = BH1770_PROX_DEF_RATE_THRESH;
1203 chip->prox_rate = BH1770_PROX_DEFAULT_RATE;
1204 chip->prox_data = 0;
1206 chip->regs[0].supply = reg_vcc;
1207 chip->regs[1].supply = reg_vleds;
1209 err = devm_regulator_bulk_get(&client->dev,
1210 ARRAY_SIZE(chip->regs), chip->regs);
1212 dev_err(&client->dev, "Cannot get regulators\n");
1216 err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
1219 dev_err(&client->dev, "Cannot enable regulators\n");
1223 usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
1224 err = bh1770_detect(chip);
1229 bh1770_chip_on(chip);
1230 pm_runtime_set_active(&client->dev);
1231 pm_runtime_enable(&client->dev);
1233 chip->lux_corr = bh1770_get_corr_value(chip);
1234 if (chip->lux_corr == 0) {
1235 dev_err(&client->dev, "Improper correction values\n");
1240 if (chip->pdata->setup_resources) {
1241 err = chip->pdata->setup_resources();
1248 err = sysfs_create_group(&chip->client->dev.kobj,
1249 &bh1770_attribute_group);
1251 dev_err(&chip->client->dev, "Sysfs registration failed\n");
1256 * Chip needs level triggered interrupt to work. However,
1257 * level triggering doesn't work always correctly with power
1258 * management. Select both
1260 err = request_threaded_irq(client->irq, NULL,
1262 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
1266 dev_err(&client->dev, "could not get IRQ %d\n",
1270 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1273 sysfs_remove_group(&chip->client->dev.kobj,
1274 &bh1770_attribute_group);
1276 if (chip->pdata->release_resources)
1277 chip->pdata->release_resources();
1279 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1283 static int bh1770_remove(struct i2c_client *client)
1285 struct bh1770_chip *chip = i2c_get_clientdata(client);
1287 free_irq(client->irq, chip);
1289 sysfs_remove_group(&chip->client->dev.kobj,
1290 &bh1770_attribute_group);
1292 if (chip->pdata->release_resources)
1293 chip->pdata->release_resources();
1295 cancel_delayed_work_sync(&chip->prox_work);
1297 if (!pm_runtime_suspended(&client->dev))
1298 bh1770_chip_off(chip);
1300 pm_runtime_disable(&client->dev);
1301 pm_runtime_set_suspended(&client->dev);
1306 #ifdef CONFIG_PM_SLEEP
1307 static int bh1770_suspend(struct device *dev)
1309 struct i2c_client *client = to_i2c_client(dev);
1310 struct bh1770_chip *chip = i2c_get_clientdata(client);
1312 bh1770_chip_off(chip);
1317 static int bh1770_resume(struct device *dev)
1319 struct i2c_client *client = to_i2c_client(dev);
1320 struct bh1770_chip *chip = i2c_get_clientdata(client);
1323 bh1770_chip_on(chip);
1325 if (!pm_runtime_suspended(dev)) {
1327 * If we were enabled at suspend time, it is expected
1328 * everything works nice and smoothly
1330 ret = bh1770_lux_rate(chip, chip->lux_rate_index);
1331 ret |= bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
1333 /* This causes interrupt after the next measurement cycle */
1334 bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
1335 BH1770_LUX_DEF_THRES);
1336 /* Inform that we are waiting for a result from ALS */
1337 chip->lux_wait_result = true;
1338 bh1770_prox_mode_control(chip);
1345 static int bh1770_runtime_suspend(struct device *dev)
1347 struct i2c_client *client = to_i2c_client(dev);
1348 struct bh1770_chip *chip = i2c_get_clientdata(client);
1350 bh1770_chip_off(chip);
1355 static int bh1770_runtime_resume(struct device *dev)
1357 struct i2c_client *client = to_i2c_client(dev);
1358 struct bh1770_chip *chip = i2c_get_clientdata(client);
1360 bh1770_chip_on(chip);
1366 static const struct i2c_device_id bh1770_id[] = {
1372 MODULE_DEVICE_TABLE(i2c, bh1770_id);
1374 static const struct dev_pm_ops bh1770_pm_ops = {
1375 SET_SYSTEM_SLEEP_PM_OPS(bh1770_suspend, bh1770_resume)
1376 SET_RUNTIME_PM_OPS(bh1770_runtime_suspend, bh1770_runtime_resume, NULL)
1379 static struct i2c_driver bh1770_driver = {
1381 .name = "bh1770glc",
1382 .pm = &bh1770_pm_ops,
1384 .probe = bh1770_probe,
1385 .remove = bh1770_remove,
1386 .id_table = bh1770_id,
1389 module_i2c_driver(bh1770_driver);
1391 MODULE_DESCRIPTION("BH1770GLC / SFH7770 combined ALS and proximity sensor");
1392 MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1393 MODULE_LICENSE("GPL v2");