Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of the ROHM BH1770GLC / OSRAM SFH7770 sensor driver.
0004  * Chip is combined proximity and ambient light sensor.
0005  *
0006  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
0007  *
0008  * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/i2c.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/mutex.h>
0016 #include <linux/platform_data/bh1770glc.h>
0017 #include <linux/regulator/consumer.h>
0018 #include <linux/pm_runtime.h>
0019 #include <linux/workqueue.h>
0020 #include <linux/delay.h>
0021 #include <linux/wait.h>
0022 #include <linux/slab.h>
0023 
0024 #define BH1770_ALS_CONTROL  0x80 /* ALS operation mode control */
0025 #define BH1770_PS_CONTROL   0x81 /* PS operation mode control */
0026 #define BH1770_I_LED        0x82 /* active LED and LED1, LED2 current */
0027 #define BH1770_I_LED3       0x83 /* LED3 current setting */
0028 #define BH1770_ALS_PS_MEAS  0x84 /* Forced mode trigger */
0029 #define BH1770_PS_MEAS_RATE 0x85 /* PS meas. rate at stand alone mode */
0030 #define BH1770_ALS_MEAS_RATE    0x86 /* ALS meas. rate at stand alone mode */
0031 #define BH1770_PART_ID      0x8a /* Part number and revision ID */
0032 #define BH1770_MANUFACT_ID  0x8b /* Manufacturerer ID */
0033 #define BH1770_ALS_DATA_0   0x8c /* ALS DATA low byte */
0034 #define BH1770_ALS_DATA_1   0x8d /* ALS DATA high byte */
0035 #define BH1770_ALS_PS_STATUS    0x8e /* Measurement data and int status */
0036 #define BH1770_PS_DATA_LED1 0x8f /* PS data from LED1 */
0037 #define BH1770_PS_DATA_LED2 0x90 /* PS data from LED2 */
0038 #define BH1770_PS_DATA_LED3 0x91 /* PS data from LED3 */
0039 #define BH1770_INTERRUPT    0x92 /* Interrupt setting */
0040 #define BH1770_PS_TH_LED1   0x93 /* PS interrupt threshold for LED1 */
0041 #define BH1770_PS_TH_LED2   0x94 /* PS interrupt threshold for LED2 */
0042 #define BH1770_PS_TH_LED3   0x95 /* PS interrupt threshold for LED3 */
0043 #define BH1770_ALS_TH_UP_0  0x96 /* ALS upper threshold low byte */
0044 #define BH1770_ALS_TH_UP_1  0x97 /* ALS upper threshold high byte */
0045 #define BH1770_ALS_TH_LOW_0 0x98 /* ALS lower threshold low byte */
0046 #define BH1770_ALS_TH_LOW_1 0x99 /* ALS lower threshold high byte */
0047 
0048 /* MANUFACT_ID */
0049 #define BH1770_MANUFACT_ROHM    0x01
0050 #define BH1770_MANUFACT_OSRAM   0x03
0051 
0052 /* PART_ID */
0053 #define BH1770_PART     0x90
0054 #define BH1770_PART_MASK    0xf0
0055 #define BH1770_REV_MASK     0x0f
0056 #define BH1770_REV_SHIFT    0
0057 #define BH1770_REV_0        0x00
0058 #define BH1770_REV_1        0x01
0059 
0060 /* Operating modes for both */
0061 #define BH1770_STANDBY      0x00
0062 #define BH1770_FORCED       0x02
0063 #define BH1770_STANDALONE   0x03
0064 #define BH1770_SWRESET      (0x01 << 2)
0065 
0066 #define BH1770_PS_TRIG_MEAS (1 << 0)
0067 #define BH1770_ALS_TRIG_MEAS    (1 << 1)
0068 
0069 /* Interrupt control */
0070 #define BH1770_INT_OUTPUT_MODE  (1 << 3) /* 0 = latched */
0071 #define BH1770_INT_POLARITY (1 << 2) /* 1 = active high */
0072 #define BH1770_INT_ALS_ENA  (1 << 1)
0073 #define BH1770_INT_PS_ENA   (1 << 0)
0074 
0075 /* Interrupt status */
0076 #define BH1770_INT_LED1_DATA    (1 << 0)
0077 #define BH1770_INT_LED1_INT (1 << 1)
0078 #define BH1770_INT_LED2_DATA    (1 << 2)
0079 #define BH1770_INT_LED2_INT (1 << 3)
0080 #define BH1770_INT_LED3_DATA    (1 << 4)
0081 #define BH1770_INT_LED3_INT (1 << 5)
0082 #define BH1770_INT_LEDS_INT ((1 << 1) | (1 << 3) | (1 << 5))
0083 #define BH1770_INT_ALS_DATA (1 << 6)
0084 #define BH1770_INT_ALS_INT  (1 << 7)
0085 
0086 /* Led channels */
0087 #define BH1770_LED1     0x00
0088 
0089 #define BH1770_DISABLE      0
0090 #define BH1770_ENABLE       1
0091 #define BH1770_PROX_CHANNELS    1
0092 
0093 #define BH1770_LUX_DEFAULT_RATE 1 /* Index to lux rate table */
0094 #define BH1770_PROX_DEFAULT_RATE 1 /* Direct HW value =~ 50Hz */
0095 #define BH1770_PROX_DEF_RATE_THRESH 6 /* Direct HW value =~ 5 Hz */
0096 #define BH1770_STARTUP_DELAY    50
0097 #define BH1770_RESET_TIME   10
0098 #define BH1770_TIMEOUT      2100 /* Timeout in 2.1 seconds */
0099 
0100 #define BH1770_LUX_RANGE    65535
0101 #define BH1770_PROX_RANGE   255
0102 #define BH1770_COEF_SCALER  1024
0103 #define BH1770_CALIB_SCALER 8192
0104 #define BH1770_LUX_NEUTRAL_CALIB_VALUE (1 * BH1770_CALIB_SCALER)
0105 #define BH1770_LUX_DEF_THRES    1000
0106 #define BH1770_PROX_DEF_THRES   70
0107 #define BH1770_PROX_DEF_ABS_THRES   100
0108 #define BH1770_DEFAULT_PERSISTENCE  10
0109 #define BH1770_PROX_MAX_PERSISTENCE 50
0110 #define BH1770_LUX_GA_SCALE 16384
0111 #define BH1770_LUX_CF_SCALE 2048 /* CF ChipFactor */
0112 #define BH1770_NEUTRAL_CF   BH1770_LUX_CF_SCALE
0113 #define BH1770_LUX_CORR_SCALE   4096
0114 
0115 #define PROX_ABOVE_THRESHOLD    1
0116 #define PROX_BELOW_THRESHOLD    0
0117 
0118 #define PROX_IGNORE_LUX_LIMIT   500
0119 
0120 struct bh1770_chip {
0121     struct bh1770_platform_data *pdata;
0122     char                chipname[10];
0123     u8              revision;
0124     struct i2c_client       *client;
0125     struct regulator_bulk_data  regs[2];
0126     struct mutex            mutex; /* avoid parallel access */
0127     wait_queue_head_t       wait;
0128 
0129     bool            int_mode_prox;
0130     bool            int_mode_lux;
0131     struct delayed_work prox_work;
0132     u32 lux_cf; /* Chip specific factor */
0133     u32 lux_ga;
0134     u32 lux_calib;
0135     int lux_rate_index;
0136     u32 lux_corr;
0137     u16 lux_data_raw;
0138     u16 lux_threshold_hi;
0139     u16 lux_threshold_lo;
0140     u16 lux_thres_hi_onchip;
0141     u16 lux_thres_lo_onchip;
0142     bool    lux_wait_result;
0143 
0144     int prox_enable_count;
0145     u16 prox_coef;
0146     u16 prox_const;
0147     int prox_rate;
0148     int prox_rate_threshold;
0149     u8  prox_persistence;
0150     u8  prox_persistence_counter;
0151     u8  prox_data;
0152     u8  prox_threshold;
0153     u8  prox_threshold_hw;
0154     bool    prox_force_update;
0155     u8  prox_abs_thres;
0156     u8  prox_led;
0157 };
0158 
0159 static const char reg_vcc[] = "Vcc";
0160 static const char reg_vleds[] = "Vleds";
0161 
0162 /*
0163  * Supported stand alone rates in ms from chip data sheet
0164  * {10, 20, 30, 40, 70, 100, 200, 500, 1000, 2000};
0165  */
0166 static const s16 prox_rates_hz[] = {100, 50, 33, 25, 14, 10, 5, 2};
0167 static const s16 prox_rates_ms[] = {10, 20, 30, 40, 70, 100, 200, 500};
0168 
0169 /*
0170  * Supported stand alone rates in ms from chip data sheet
0171  * {100, 200, 500, 1000, 2000};
0172  */
0173 static const s16 lux_rates_hz[] = {10, 5, 2, 1, 0};
0174 
0175 /*
0176  * interrupt control functions are called while keeping chip->mutex
0177  * excluding module probe / remove
0178  */
0179 static inline int bh1770_lux_interrupt_control(struct bh1770_chip *chip,
0180                     int lux)
0181 {
0182     chip->int_mode_lux = lux;
0183     /* Set interrupt modes, interrupt active low, latched */
0184     return i2c_smbus_write_byte_data(chip->client,
0185                     BH1770_INTERRUPT,
0186                     (lux << 1) | chip->int_mode_prox);
0187 }
0188 
0189 static inline int bh1770_prox_interrupt_control(struct bh1770_chip *chip,
0190                     int ps)
0191 {
0192     chip->int_mode_prox = ps;
0193     return i2c_smbus_write_byte_data(chip->client,
0194                     BH1770_INTERRUPT,
0195                     (chip->int_mode_lux << 1) | (ps << 0));
0196 }
0197 
0198 /* chip->mutex is always kept here */
0199 static int bh1770_lux_rate(struct bh1770_chip *chip, int rate_index)
0200 {
0201     /* sysfs may call this when the chip is powered off */
0202     if (pm_runtime_suspended(&chip->client->dev))
0203         return 0;
0204 
0205     /* Proper proximity response needs fastest lux rate (100ms) */
0206     if (chip->prox_enable_count)
0207         rate_index = 0;
0208 
0209     return i2c_smbus_write_byte_data(chip->client,
0210                     BH1770_ALS_MEAS_RATE,
0211                     rate_index);
0212 }
0213 
0214 static int bh1770_prox_rate(struct bh1770_chip *chip, int mode)
0215 {
0216     int rate;
0217 
0218     rate = (mode == PROX_ABOVE_THRESHOLD) ?
0219         chip->prox_rate_threshold : chip->prox_rate;
0220 
0221     return i2c_smbus_write_byte_data(chip->client,
0222                     BH1770_PS_MEAS_RATE,
0223                     rate);
0224 }
0225 
0226 /* InfraredLED is controlled by the chip during proximity scanning */
0227 static inline int bh1770_led_cfg(struct bh1770_chip *chip)
0228 {
0229     /* LED cfg, current for leds 1 and 2 */
0230     return i2c_smbus_write_byte_data(chip->client,
0231                     BH1770_I_LED,
0232                     (BH1770_LED1 << 6) |
0233                     (BH1770_LED_5mA << 3) |
0234                     chip->prox_led);
0235 }
0236 
0237 /*
0238  * Following two functions converts raw ps values from HW to normalized
0239  * values. Purpose is to compensate differences between different sensor
0240  * versions and variants so that result means about the same between
0241  * versions.
0242  */
0243 static inline u8 bh1770_psraw_to_adjusted(struct bh1770_chip *chip, u8 psraw)
0244 {
0245     u16 adjusted;
0246     adjusted = (u16)(((u32)(psraw + chip->prox_const) * chip->prox_coef) /
0247         BH1770_COEF_SCALER);
0248     if (adjusted > BH1770_PROX_RANGE)
0249         adjusted = BH1770_PROX_RANGE;
0250     return adjusted;
0251 }
0252 
0253 static inline u8 bh1770_psadjusted_to_raw(struct bh1770_chip *chip, u8 ps)
0254 {
0255     u16 raw;
0256 
0257     raw = (((u32)ps * BH1770_COEF_SCALER) / chip->prox_coef);
0258     if (raw > chip->prox_const)
0259         raw = raw - chip->prox_const;
0260     else
0261         raw = 0;
0262     return raw;
0263 }
0264 
0265 /*
0266  * Following two functions converts raw lux values from HW to normalized
0267  * values. Purpose is to compensate differences between different sensor
0268  * versions and variants so that result means about the same between
0269  * versions. Chip->mutex is kept when this is called.
0270  */
0271 static int bh1770_prox_set_threshold(struct bh1770_chip *chip)
0272 {
0273     u8 tmp = 0;
0274 
0275     /* sysfs may call this when the chip is powered off */
0276     if (pm_runtime_suspended(&chip->client->dev))
0277         return 0;
0278 
0279     tmp = bh1770_psadjusted_to_raw(chip, chip->prox_threshold);
0280     chip->prox_threshold_hw = tmp;
0281 
0282     return  i2c_smbus_write_byte_data(chip->client, BH1770_PS_TH_LED1,
0283                     tmp);
0284 }
0285 
0286 static inline u16 bh1770_lux_raw_to_adjusted(struct bh1770_chip *chip, u16 raw)
0287 {
0288     u32 lux;
0289     lux = ((u32)raw * chip->lux_corr) / BH1770_LUX_CORR_SCALE;
0290     return min(lux, (u32)BH1770_LUX_RANGE);
0291 }
0292 
0293 static inline u16 bh1770_lux_adjusted_to_raw(struct bh1770_chip *chip,
0294                     u16 adjusted)
0295 {
0296     return (u32)adjusted * BH1770_LUX_CORR_SCALE / chip->lux_corr;
0297 }
0298 
0299 /* chip->mutex is kept when this is called */
0300 static int bh1770_lux_update_thresholds(struct bh1770_chip *chip,
0301                     u16 threshold_hi, u16 threshold_lo)
0302 {
0303     u8 data[4];
0304     int ret;
0305 
0306     /* sysfs may call this when the chip is powered off */
0307     if (pm_runtime_suspended(&chip->client->dev))
0308         return 0;
0309 
0310     /*
0311      * Compensate threshold values with the correction factors if not
0312      * set to minimum or maximum.
0313      * Min & max values disables interrupts.
0314      */
0315     if (threshold_hi != BH1770_LUX_RANGE && threshold_hi != 0)
0316         threshold_hi = bh1770_lux_adjusted_to_raw(chip, threshold_hi);
0317 
0318     if (threshold_lo != BH1770_LUX_RANGE && threshold_lo != 0)
0319         threshold_lo = bh1770_lux_adjusted_to_raw(chip, threshold_lo);
0320 
0321     if (chip->lux_thres_hi_onchip == threshold_hi &&
0322         chip->lux_thres_lo_onchip == threshold_lo)
0323         return 0;
0324 
0325     chip->lux_thres_hi_onchip = threshold_hi;
0326     chip->lux_thres_lo_onchip = threshold_lo;
0327 
0328     data[0] = threshold_hi;
0329     data[1] = threshold_hi >> 8;
0330     data[2] = threshold_lo;
0331     data[3] = threshold_lo >> 8;
0332 
0333     ret = i2c_smbus_write_i2c_block_data(chip->client,
0334                     BH1770_ALS_TH_UP_0,
0335                     ARRAY_SIZE(data),
0336                     data);
0337     return ret;
0338 }
0339 
0340 static int bh1770_lux_get_result(struct bh1770_chip *chip)
0341 {
0342     u16 data;
0343     int ret;
0344 
0345     ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_0);
0346     if (ret < 0)
0347         return ret;
0348 
0349     data = ret & 0xff;
0350     ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_1);
0351     if (ret < 0)
0352         return ret;
0353 
0354     chip->lux_data_raw = data | ((ret & 0xff) << 8);
0355 
0356     return 0;
0357 }
0358 
0359 /* Calculate correction value which contains chip and device specific parts */
0360 static u32 bh1770_get_corr_value(struct bh1770_chip *chip)
0361 {
0362     u32 tmp;
0363     /* Impact of glass attenuation correction */
0364     tmp = (BH1770_LUX_CORR_SCALE * chip->lux_ga) / BH1770_LUX_GA_SCALE;
0365     /* Impact of chip factor correction */
0366     tmp = (tmp * chip->lux_cf) / BH1770_LUX_CF_SCALE;
0367     /* Impact of Device specific calibration correction */
0368     tmp = (tmp * chip->lux_calib) / BH1770_CALIB_SCALER;
0369     return tmp;
0370 }
0371 
0372 static int bh1770_lux_read_result(struct bh1770_chip *chip)
0373 {
0374     bh1770_lux_get_result(chip);
0375     return bh1770_lux_raw_to_adjusted(chip, chip->lux_data_raw);
0376 }
0377 
0378 /*
0379  * Chip on / off functions are called while keeping mutex except probe
0380  * or remove phase
0381  */
0382 static int bh1770_chip_on(struct bh1770_chip *chip)
0383 {
0384     int ret = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
0385                     chip->regs);
0386     if (ret < 0)
0387         return ret;
0388 
0389     usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
0390 
0391     /* Reset the chip */
0392     i2c_smbus_write_byte_data(chip->client, BH1770_ALS_CONTROL,
0393                 BH1770_SWRESET);
0394     usleep_range(BH1770_RESET_TIME, BH1770_RESET_TIME * 2);
0395 
0396     /*
0397      * ALS is started always since proximity needs als results
0398      * for realibility estimation.
0399      * Let's assume dark until the first ALS measurement is ready.
0400      */
0401     chip->lux_data_raw = 0;
0402     chip->prox_data = 0;
0403     ret = i2c_smbus_write_byte_data(chip->client,
0404                     BH1770_ALS_CONTROL, BH1770_STANDALONE);
0405 
0406     /* Assume reset defaults */
0407     chip->lux_thres_hi_onchip = BH1770_LUX_RANGE;
0408     chip->lux_thres_lo_onchip = 0;
0409 
0410     return ret;
0411 }
0412 
0413 static void bh1770_chip_off(struct bh1770_chip *chip)
0414 {
0415     i2c_smbus_write_byte_data(chip->client,
0416                     BH1770_INTERRUPT, BH1770_DISABLE);
0417     i2c_smbus_write_byte_data(chip->client,
0418                 BH1770_ALS_CONTROL, BH1770_STANDBY);
0419     i2c_smbus_write_byte_data(chip->client,
0420                 BH1770_PS_CONTROL, BH1770_STANDBY);
0421     regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
0422 }
0423 
0424 /* chip->mutex is kept when this is called */
0425 static int bh1770_prox_mode_control(struct bh1770_chip *chip)
0426 {
0427     if (chip->prox_enable_count) {
0428         chip->prox_force_update = true; /* Force immediate update */
0429 
0430         bh1770_lux_rate(chip, chip->lux_rate_index);
0431         bh1770_prox_set_threshold(chip);
0432         bh1770_led_cfg(chip);
0433         bh1770_prox_rate(chip, PROX_BELOW_THRESHOLD);
0434         bh1770_prox_interrupt_control(chip, BH1770_ENABLE);
0435         i2c_smbus_write_byte_data(chip->client,
0436                     BH1770_PS_CONTROL, BH1770_STANDALONE);
0437     } else {
0438         chip->prox_data = 0;
0439         bh1770_lux_rate(chip, chip->lux_rate_index);
0440         bh1770_prox_interrupt_control(chip, BH1770_DISABLE);
0441         i2c_smbus_write_byte_data(chip->client,
0442                     BH1770_PS_CONTROL, BH1770_STANDBY);
0443     }
0444     return 0;
0445 }
0446 
0447 /* chip->mutex is kept when this is called */
0448 static int bh1770_prox_read_result(struct bh1770_chip *chip)
0449 {
0450     int ret;
0451     bool above;
0452     u8 mode;
0453 
0454     ret = i2c_smbus_read_byte_data(chip->client, BH1770_PS_DATA_LED1);
0455     if (ret < 0)
0456         goto out;
0457 
0458     if (ret > chip->prox_threshold_hw)
0459         above = true;
0460     else
0461         above = false;
0462 
0463     /*
0464      * when ALS levels goes above limit, proximity result may be
0465      * false proximity. Thus ignore the result. With real proximity
0466      * there is a shadow causing low als levels.
0467      */
0468     if (chip->lux_data_raw > PROX_IGNORE_LUX_LIMIT)
0469         ret = 0;
0470 
0471     chip->prox_data = bh1770_psraw_to_adjusted(chip, ret);
0472 
0473     /* Strong proximity level or force mode requires immediate response */
0474     if (chip->prox_data >= chip->prox_abs_thres ||
0475         chip->prox_force_update)
0476         chip->prox_persistence_counter = chip->prox_persistence;
0477 
0478     chip->prox_force_update = false;
0479 
0480     /* Persistence filttering to reduce false proximity events */
0481     if (likely(above)) {
0482         if (chip->prox_persistence_counter < chip->prox_persistence) {
0483             chip->prox_persistence_counter++;
0484             ret = -ENODATA;
0485         } else {
0486             mode = PROX_ABOVE_THRESHOLD;
0487             ret = 0;
0488         }
0489     } else {
0490         chip->prox_persistence_counter = 0;
0491         mode = PROX_BELOW_THRESHOLD;
0492         chip->prox_data = 0;
0493         ret = 0;
0494     }
0495 
0496     /* Set proximity detection rate based on above or below value */
0497     if (ret == 0) {
0498         bh1770_prox_rate(chip, mode);
0499         sysfs_notify(&chip->client->dev.kobj, NULL, "prox0_raw");
0500     }
0501 out:
0502     return ret;
0503 }
0504 
0505 static int bh1770_detect(struct bh1770_chip *chip)
0506 {
0507     struct i2c_client *client = chip->client;
0508     s32 ret;
0509     u8 manu, part;
0510 
0511     ret = i2c_smbus_read_byte_data(client, BH1770_MANUFACT_ID);
0512     if (ret < 0)
0513         goto error;
0514     manu = (u8)ret;
0515 
0516     ret = i2c_smbus_read_byte_data(client, BH1770_PART_ID);
0517     if (ret < 0)
0518         goto error;
0519     part = (u8)ret;
0520 
0521     chip->revision = (part & BH1770_REV_MASK) >> BH1770_REV_SHIFT;
0522     chip->prox_coef = BH1770_COEF_SCALER;
0523     chip->prox_const = 0;
0524     chip->lux_cf = BH1770_NEUTRAL_CF;
0525 
0526     if ((manu == BH1770_MANUFACT_ROHM) &&
0527         ((part & BH1770_PART_MASK) == BH1770_PART)) {
0528         snprintf(chip->chipname, sizeof(chip->chipname), "BH1770GLC");
0529         return 0;
0530     }
0531 
0532     if ((manu == BH1770_MANUFACT_OSRAM) &&
0533         ((part & BH1770_PART_MASK) == BH1770_PART)) {
0534         snprintf(chip->chipname, sizeof(chip->chipname), "SFH7770");
0535         /* Values selected by comparing different versions */
0536         chip->prox_coef = 819; /* 0.8 * BH1770_COEF_SCALER */
0537         chip->prox_const = 40;
0538         return 0;
0539     }
0540 
0541     ret = -ENODEV;
0542 error:
0543     dev_dbg(&client->dev, "BH1770 or SFH7770 not found\n");
0544 
0545     return ret;
0546 }
0547 
0548 /*
0549  * This work is re-scheduled at every proximity interrupt.
0550  * If this work is running, it means that there hasn't been any
0551  * proximity interrupt in time. Situation is handled as no-proximity.
0552  * It would be nice to have low-threshold interrupt or interrupt
0553  * when measurement and hi-threshold are both 0. But neither of those exists.
0554  * This is a workaroud for missing HW feature.
0555  */
0556 
0557 static void bh1770_prox_work(struct work_struct *work)
0558 {
0559     struct bh1770_chip *chip =
0560         container_of(work, struct bh1770_chip, prox_work.work);
0561 
0562     mutex_lock(&chip->mutex);
0563     bh1770_prox_read_result(chip);
0564     mutex_unlock(&chip->mutex);
0565 }
0566 
0567 /* This is threaded irq handler */
0568 static irqreturn_t bh1770_irq(int irq, void *data)
0569 {
0570     struct bh1770_chip *chip = data;
0571     int status;
0572     int rate = 0;
0573 
0574     mutex_lock(&chip->mutex);
0575     status = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_PS_STATUS);
0576 
0577     /* Acknowledge interrupt by reading this register */
0578     i2c_smbus_read_byte_data(chip->client, BH1770_INTERRUPT);
0579 
0580     /*
0581      * Check if there is fresh data available for als.
0582      * If this is the very first data, update thresholds after that.
0583      */
0584     if (status & BH1770_INT_ALS_DATA) {
0585         bh1770_lux_get_result(chip);
0586         if (unlikely(chip->lux_wait_result)) {
0587             chip->lux_wait_result = false;
0588             wake_up(&chip->wait);
0589             bh1770_lux_update_thresholds(chip,
0590                         chip->lux_threshold_hi,
0591                         chip->lux_threshold_lo);
0592         }
0593     }
0594 
0595     /* Disable interrupt logic to guarantee acknowledgement */
0596     i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
0597                   (0 << 1) | (0 << 0));
0598 
0599     if ((status & BH1770_INT_ALS_INT))
0600         sysfs_notify(&chip->client->dev.kobj, NULL, "lux0_input");
0601 
0602     if (chip->int_mode_prox && (status & BH1770_INT_LEDS_INT)) {
0603         rate = prox_rates_ms[chip->prox_rate_threshold];
0604         bh1770_prox_read_result(chip);
0605     }
0606 
0607     /* Re-enable interrupt logic */
0608     i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
0609                   (chip->int_mode_lux << 1) |
0610                   (chip->int_mode_prox << 0));
0611     mutex_unlock(&chip->mutex);
0612 
0613     /*
0614      * Can't cancel work while keeping mutex since the work uses the
0615      * same mutex.
0616      */
0617     if (rate) {
0618         /*
0619          * Simulate missing no-proximity interrupt 50ms after the
0620          * next expected interrupt time.
0621          */
0622         cancel_delayed_work_sync(&chip->prox_work);
0623         schedule_delayed_work(&chip->prox_work,
0624                 msecs_to_jiffies(rate + 50));
0625     }
0626     return IRQ_HANDLED;
0627 }
0628 
0629 static ssize_t bh1770_power_state_store(struct device *dev,
0630                       struct device_attribute *attr,
0631                       const char *buf, size_t count)
0632 {
0633     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0634     unsigned long value;
0635     ssize_t ret;
0636 
0637     ret = kstrtoul(buf, 0, &value);
0638     if (ret)
0639         return ret;
0640 
0641     mutex_lock(&chip->mutex);
0642     if (value) {
0643         pm_runtime_get_sync(dev);
0644 
0645         ret = bh1770_lux_rate(chip, chip->lux_rate_index);
0646         if (ret < 0) {
0647             pm_runtime_put(dev);
0648             goto leave;
0649         }
0650 
0651         ret = bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
0652         if (ret < 0) {
0653             pm_runtime_put(dev);
0654             goto leave;
0655         }
0656 
0657         /* This causes interrupt after the next measurement cycle */
0658         bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
0659                     BH1770_LUX_DEF_THRES);
0660         /* Inform that we are waiting for a result from ALS */
0661         chip->lux_wait_result = true;
0662         bh1770_prox_mode_control(chip);
0663     } else if (!pm_runtime_suspended(dev)) {
0664         pm_runtime_put(dev);
0665     }
0666     ret = count;
0667 leave:
0668     mutex_unlock(&chip->mutex);
0669     return ret;
0670 }
0671 
0672 static ssize_t bh1770_power_state_show(struct device *dev,
0673                    struct device_attribute *attr, char *buf)
0674 {
0675     return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
0676 }
0677 
0678 static ssize_t bh1770_lux_result_show(struct device *dev,
0679                    struct device_attribute *attr, char *buf)
0680 {
0681     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0682     ssize_t ret;
0683     long timeout;
0684 
0685     if (pm_runtime_suspended(dev))
0686         return -EIO; /* Chip is not enabled at all */
0687 
0688     timeout = wait_event_interruptible_timeout(chip->wait,
0689                     !chip->lux_wait_result,
0690                     msecs_to_jiffies(BH1770_TIMEOUT));
0691     if (!timeout)
0692         return -EIO;
0693 
0694     mutex_lock(&chip->mutex);
0695     ret = sprintf(buf, "%d\n", bh1770_lux_read_result(chip));
0696     mutex_unlock(&chip->mutex);
0697 
0698     return ret;
0699 }
0700 
0701 static ssize_t bh1770_lux_range_show(struct device *dev,
0702                    struct device_attribute *attr, char *buf)
0703 {
0704     return sprintf(buf, "%d\n", BH1770_LUX_RANGE);
0705 }
0706 
0707 static ssize_t bh1770_prox_enable_store(struct device *dev,
0708                       struct device_attribute *attr,
0709                       const char *buf, size_t count)
0710 {
0711     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0712     unsigned long value;
0713     int ret;
0714 
0715     ret = kstrtoul(buf, 0, &value);
0716     if (ret)
0717         return ret;
0718 
0719     mutex_lock(&chip->mutex);
0720     /* Assume no proximity. Sensor will tell real state soon */
0721     if (!chip->prox_enable_count)
0722         chip->prox_data = 0;
0723 
0724     if (value)
0725         chip->prox_enable_count++;
0726     else if (chip->prox_enable_count > 0)
0727         chip->prox_enable_count--;
0728     else
0729         goto leave;
0730 
0731     /* Run control only when chip is powered on */
0732     if (!pm_runtime_suspended(dev))
0733         bh1770_prox_mode_control(chip);
0734 leave:
0735     mutex_unlock(&chip->mutex);
0736     return count;
0737 }
0738 
0739 static ssize_t bh1770_prox_enable_show(struct device *dev,
0740                    struct device_attribute *attr, char *buf)
0741 {
0742     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0743     ssize_t len;
0744 
0745     mutex_lock(&chip->mutex);
0746     len = sprintf(buf, "%d\n", chip->prox_enable_count);
0747     mutex_unlock(&chip->mutex);
0748     return len;
0749 }
0750 
0751 static ssize_t bh1770_prox_result_show(struct device *dev,
0752                    struct device_attribute *attr, char *buf)
0753 {
0754     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0755     ssize_t ret;
0756 
0757     mutex_lock(&chip->mutex);
0758     if (chip->prox_enable_count && !pm_runtime_suspended(dev))
0759         ret = sprintf(buf, "%d\n", chip->prox_data);
0760     else
0761         ret = -EIO;
0762     mutex_unlock(&chip->mutex);
0763     return ret;
0764 }
0765 
0766 static ssize_t bh1770_prox_range_show(struct device *dev,
0767                    struct device_attribute *attr, char *buf)
0768 {
0769     return sprintf(buf, "%d\n", BH1770_PROX_RANGE);
0770 }
0771 
0772 static ssize_t bh1770_get_prox_rate_avail(struct device *dev,
0773                    struct device_attribute *attr, char *buf)
0774 {
0775     int i;
0776     int pos = 0;
0777     for (i = 0; i < ARRAY_SIZE(prox_rates_hz); i++)
0778         pos += sprintf(buf + pos, "%d ", prox_rates_hz[i]);
0779     sprintf(buf + pos - 1, "\n");
0780     return pos;
0781 }
0782 
0783 static ssize_t bh1770_get_prox_rate_above(struct device *dev,
0784                    struct device_attribute *attr, char *buf)
0785 {
0786     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0787     return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate_threshold]);
0788 }
0789 
0790 static ssize_t bh1770_get_prox_rate_below(struct device *dev,
0791                    struct device_attribute *attr, char *buf)
0792 {
0793     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0794     return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate]);
0795 }
0796 
0797 static int bh1770_prox_rate_validate(int rate)
0798 {
0799     int i;
0800 
0801     for (i = 0; i < ARRAY_SIZE(prox_rates_hz) - 1; i++)
0802         if (rate >= prox_rates_hz[i])
0803             break;
0804     return i;
0805 }
0806 
0807 static ssize_t bh1770_set_prox_rate_above(struct device *dev,
0808                     struct device_attribute *attr,
0809                     const char *buf, size_t count)
0810 {
0811     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0812     unsigned long value;
0813     int ret;
0814 
0815     ret = kstrtoul(buf, 0, &value);
0816     if (ret)
0817         return ret;
0818 
0819     mutex_lock(&chip->mutex);
0820     chip->prox_rate_threshold = bh1770_prox_rate_validate(value);
0821     mutex_unlock(&chip->mutex);
0822     return count;
0823 }
0824 
0825 static ssize_t bh1770_set_prox_rate_below(struct device *dev,
0826                     struct device_attribute *attr,
0827                     const char *buf, size_t count)
0828 {
0829     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0830     unsigned long value;
0831     int ret;
0832 
0833     ret = kstrtoul(buf, 0, &value);
0834     if (ret)
0835         return ret;
0836 
0837     mutex_lock(&chip->mutex);
0838     chip->prox_rate = bh1770_prox_rate_validate(value);
0839     mutex_unlock(&chip->mutex);
0840     return count;
0841 }
0842 
0843 static ssize_t bh1770_get_prox_thres(struct device *dev,
0844                    struct device_attribute *attr, char *buf)
0845 {
0846     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0847     return sprintf(buf, "%d\n", chip->prox_threshold);
0848 }
0849 
0850 static ssize_t bh1770_set_prox_thres(struct device *dev,
0851                       struct device_attribute *attr,
0852                       const char *buf, size_t count)
0853 {
0854     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0855     unsigned long value;
0856     int ret;
0857 
0858     ret = kstrtoul(buf, 0, &value);
0859     if (ret)
0860         return ret;
0861 
0862     if (value > BH1770_PROX_RANGE)
0863         return -EINVAL;
0864 
0865     mutex_lock(&chip->mutex);
0866     chip->prox_threshold = value;
0867     ret = bh1770_prox_set_threshold(chip);
0868     mutex_unlock(&chip->mutex);
0869     if (ret < 0)
0870         return ret;
0871     return count;
0872 }
0873 
0874 static ssize_t bh1770_prox_persistence_show(struct device *dev,
0875                  struct device_attribute *attr, char *buf)
0876 {
0877     struct bh1770_chip *chip = dev_get_drvdata(dev);
0878 
0879     return sprintf(buf, "%u\n", chip->prox_persistence);
0880 }
0881 
0882 static ssize_t bh1770_prox_persistence_store(struct device *dev,
0883                 struct device_attribute *attr,
0884                 const char *buf, size_t len)
0885 {
0886     struct bh1770_chip *chip = dev_get_drvdata(dev);
0887     unsigned long value;
0888     int ret;
0889 
0890     ret = kstrtoul(buf, 0, &value);
0891     if (ret)
0892         return ret;
0893 
0894     if (value > BH1770_PROX_MAX_PERSISTENCE)
0895         return -EINVAL;
0896 
0897     chip->prox_persistence = value;
0898 
0899     return len;
0900 }
0901 
0902 static ssize_t bh1770_prox_abs_thres_show(struct device *dev,
0903                  struct device_attribute *attr, char *buf)
0904 {
0905     struct bh1770_chip *chip = dev_get_drvdata(dev);
0906     return sprintf(buf, "%u\n", chip->prox_abs_thres);
0907 }
0908 
0909 static ssize_t bh1770_prox_abs_thres_store(struct device *dev,
0910                 struct device_attribute *attr,
0911                 const char *buf, size_t len)
0912 {
0913     struct bh1770_chip *chip = dev_get_drvdata(dev);
0914     unsigned long value;
0915     int ret;
0916 
0917     ret = kstrtoul(buf, 0, &value);
0918     if (ret)
0919         return ret;
0920 
0921     if (value > BH1770_PROX_RANGE)
0922         return -EINVAL;
0923 
0924     chip->prox_abs_thres = value;
0925 
0926     return len;
0927 }
0928 
0929 static ssize_t bh1770_chip_id_show(struct device *dev,
0930                    struct device_attribute *attr, char *buf)
0931 {
0932     struct bh1770_chip *chip =  dev_get_drvdata(dev);
0933     return sprintf(buf, "%s rev %d\n", chip->chipname, chip->revision);
0934 }
0935 
0936 static ssize_t bh1770_lux_calib_default_show(struct device *dev,
0937                  struct device_attribute *attr, char *buf)
0938 {
0939     return sprintf(buf, "%u\n", BH1770_CALIB_SCALER);
0940 }
0941 
0942 static ssize_t bh1770_lux_calib_show(struct device *dev,
0943                  struct device_attribute *attr, char *buf)
0944 {
0945     struct bh1770_chip *chip = dev_get_drvdata(dev);
0946     ssize_t len;
0947 
0948     mutex_lock(&chip->mutex);
0949     len = sprintf(buf, "%u\n", chip->lux_calib);
0950     mutex_unlock(&chip->mutex);
0951     return len;
0952 }
0953 
0954 static ssize_t bh1770_lux_calib_store(struct device *dev,
0955                   struct device_attribute *attr,
0956                   const char *buf, size_t len)
0957 {
0958     struct bh1770_chip *chip = dev_get_drvdata(dev);
0959     unsigned long value;
0960     u32 old_calib;
0961     u32 new_corr;
0962     int ret;
0963 
0964     ret = kstrtoul(buf, 0, &value);
0965     if (ret)
0966         return ret;
0967 
0968     mutex_lock(&chip->mutex);
0969     old_calib = chip->lux_calib;
0970     chip->lux_calib = value;
0971     new_corr = bh1770_get_corr_value(chip);
0972     if (new_corr == 0) {
0973         chip->lux_calib = old_calib;
0974         mutex_unlock(&chip->mutex);
0975         return -EINVAL;
0976     }
0977     chip->lux_corr = new_corr;
0978     /* Refresh thresholds on HW after changing correction value */
0979     bh1770_lux_update_thresholds(chip, chip->lux_threshold_hi,
0980                 chip->lux_threshold_lo);
0981 
0982     mutex_unlock(&chip->mutex);
0983 
0984     return len;
0985 }
0986 
0987 static ssize_t bh1770_get_lux_rate_avail(struct device *dev,
0988                    struct device_attribute *attr, char *buf)
0989 {
0990     int i;
0991     int pos = 0;
0992     for (i = 0; i < ARRAY_SIZE(lux_rates_hz); i++)
0993         pos += sprintf(buf + pos, "%d ", lux_rates_hz[i]);
0994     sprintf(buf + pos - 1, "\n");
0995     return pos;
0996 }
0997 
0998 static ssize_t bh1770_get_lux_rate(struct device *dev,
0999                    struct device_attribute *attr, char *buf)
1000 {
1001     struct bh1770_chip *chip =  dev_get_drvdata(dev);
1002     return sprintf(buf, "%d\n", lux_rates_hz[chip->lux_rate_index]);
1003 }
1004 
1005 static ssize_t bh1770_set_lux_rate(struct device *dev,
1006                       struct device_attribute *attr,
1007                       const char *buf, size_t count)
1008 {
1009     struct bh1770_chip *chip =  dev_get_drvdata(dev);
1010     unsigned long rate_hz;
1011     int ret, i;
1012 
1013     ret = kstrtoul(buf, 0, &rate_hz);
1014     if (ret)
1015         return ret;
1016 
1017     for (i = 0; i < ARRAY_SIZE(lux_rates_hz) - 1; i++)
1018         if (rate_hz >= lux_rates_hz[i])
1019             break;
1020 
1021     mutex_lock(&chip->mutex);
1022     chip->lux_rate_index = i;
1023     ret = bh1770_lux_rate(chip, i);
1024     mutex_unlock(&chip->mutex);
1025 
1026     if (ret < 0)
1027         return ret;
1028 
1029     return count;
1030 }
1031 
1032 static ssize_t bh1770_get_lux_thresh_above(struct device *dev,
1033                    struct device_attribute *attr, char *buf)
1034 {
1035     struct bh1770_chip *chip =  dev_get_drvdata(dev);
1036     return sprintf(buf, "%d\n", chip->lux_threshold_hi);
1037 }
1038 
1039 static ssize_t bh1770_get_lux_thresh_below(struct device *dev,
1040                    struct device_attribute *attr, char *buf)
1041 {
1042     struct bh1770_chip *chip =  dev_get_drvdata(dev);
1043     return sprintf(buf, "%d\n", chip->lux_threshold_lo);
1044 }
1045 
1046 static ssize_t bh1770_set_lux_thresh(struct bh1770_chip *chip, u16 *target,
1047                 const char *buf)
1048 {
1049     unsigned long thresh;
1050     int ret;
1051 
1052     ret = kstrtoul(buf, 0, &thresh);
1053     if (ret)
1054         return ret;
1055 
1056     if (thresh > BH1770_LUX_RANGE)
1057         return -EINVAL;
1058 
1059     mutex_lock(&chip->mutex);
1060     *target = thresh;
1061     /*
1062      * Don't update values in HW if we are still waiting for
1063      * first interrupt to come after device handle open call.
1064      */
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);
1070     return ret;
1071 
1072 }
1073 
1074 static ssize_t bh1770_set_lux_thresh_above(struct device *dev,
1075                   struct device_attribute *attr,
1076                   const char *buf, size_t len)
1077 {
1078     struct bh1770_chip *chip =  dev_get_drvdata(dev);
1079     int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_hi, buf);
1080     if (ret < 0)
1081         return ret;
1082     return len;
1083 }
1084 
1085 static ssize_t bh1770_set_lux_thresh_below(struct device *dev,
1086                   struct device_attribute *attr,
1087                   const char *buf, size_t len)
1088 {
1089     struct bh1770_chip *chip =  dev_get_drvdata(dev);
1090     int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_lo, buf);
1091     if (ret < 0)
1092         return ret;
1093     return len;
1094 }
1095 
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);
1116 
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,
1121                         NULL);
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);
1136 
1137 
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,
1158     NULL
1159 };
1160 
1161 static const struct attribute_group bh1770_attribute_group = {
1162     .attrs = sysfs_attrs
1163 };
1164 
1165 static int bh1770_probe(struct i2c_client *client,
1166                 const struct i2c_device_id *id)
1167 {
1168     struct bh1770_chip *chip;
1169     int err;
1170 
1171     chip = devm_kzalloc(&client->dev, sizeof *chip, GFP_KERNEL);
1172     if (!chip)
1173         return -ENOMEM;
1174 
1175     i2c_set_clientdata(client, chip);
1176     chip->client  = client;
1177 
1178     mutex_init(&chip->mutex);
1179     init_waitqueue_head(&chip->wait);
1180     INIT_DELAYED_WORK(&chip->prox_work, bh1770_prox_work);
1181 
1182     if (client->dev.platform_data == NULL) {
1183         dev_err(&client->dev, "platform data is mandatory\n");
1184         return -EINVAL;
1185     }
1186 
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;
1192 
1193     if (chip->pdata->glass_attenuation == 0)
1194         chip->lux_ga = BH1770_NEUTRAL_GA;
1195     else
1196         chip->lux_ga = chip->pdata->glass_attenuation;
1197 
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;
1205 
1206     chip->regs[0].supply = reg_vcc;
1207     chip->regs[1].supply = reg_vleds;
1208 
1209     err = devm_regulator_bulk_get(&client->dev,
1210                       ARRAY_SIZE(chip->regs), chip->regs);
1211     if (err < 0) {
1212         dev_err(&client->dev, "Cannot get regulators\n");
1213         return err;
1214     }
1215 
1216     err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
1217                 chip->regs);
1218     if (err < 0) {
1219         dev_err(&client->dev, "Cannot enable regulators\n");
1220         return err;
1221     }
1222 
1223     usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
1224     err = bh1770_detect(chip);
1225     if (err < 0)
1226         goto fail0;
1227 
1228     /* Start chip */
1229     bh1770_chip_on(chip);
1230     pm_runtime_set_active(&client->dev);
1231     pm_runtime_enable(&client->dev);
1232 
1233     chip->lux_corr = bh1770_get_corr_value(chip);
1234     if (chip->lux_corr == 0) {
1235         dev_err(&client->dev, "Improper correction values\n");
1236         err = -EINVAL;
1237         goto fail0;
1238     }
1239 
1240     if (chip->pdata->setup_resources) {
1241         err = chip->pdata->setup_resources();
1242         if (err) {
1243             err = -EINVAL;
1244             goto fail0;
1245         }
1246     }
1247 
1248     err = sysfs_create_group(&chip->client->dev.kobj,
1249                 &bh1770_attribute_group);
1250     if (err < 0) {
1251         dev_err(&chip->client->dev, "Sysfs registration failed\n");
1252         goto fail1;
1253     }
1254 
1255     /*
1256      * Chip needs level triggered interrupt to work. However,
1257      * level triggering doesn't work always correctly with power
1258      * management. Select both
1259      */
1260     err = request_threaded_irq(client->irq, NULL,
1261                 bh1770_irq,
1262                 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
1263                 IRQF_TRIGGER_LOW,
1264                 "bh1770", chip);
1265     if (err) {
1266         dev_err(&client->dev, "could not get IRQ %d\n",
1267             client->irq);
1268         goto fail2;
1269     }
1270     regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1271     return err;
1272 fail2:
1273     sysfs_remove_group(&chip->client->dev.kobj,
1274             &bh1770_attribute_group);
1275 fail1:
1276     if (chip->pdata->release_resources)
1277         chip->pdata->release_resources();
1278 fail0:
1279     regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1280     return err;
1281 }
1282 
1283 static int bh1770_remove(struct i2c_client *client)
1284 {
1285     struct bh1770_chip *chip = i2c_get_clientdata(client);
1286 
1287     free_irq(client->irq, chip);
1288 
1289     sysfs_remove_group(&chip->client->dev.kobj,
1290             &bh1770_attribute_group);
1291 
1292     if (chip->pdata->release_resources)
1293         chip->pdata->release_resources();
1294 
1295     cancel_delayed_work_sync(&chip->prox_work);
1296 
1297     if (!pm_runtime_suspended(&client->dev))
1298         bh1770_chip_off(chip);
1299 
1300     pm_runtime_disable(&client->dev);
1301     pm_runtime_set_suspended(&client->dev);
1302 
1303     return 0;
1304 }
1305 
1306 #ifdef CONFIG_PM_SLEEP
1307 static int bh1770_suspend(struct device *dev)
1308 {
1309     struct i2c_client *client = to_i2c_client(dev);
1310     struct bh1770_chip *chip = i2c_get_clientdata(client);
1311 
1312     bh1770_chip_off(chip);
1313 
1314     return 0;
1315 }
1316 
1317 static int bh1770_resume(struct device *dev)
1318 {
1319     struct i2c_client *client = to_i2c_client(dev);
1320     struct bh1770_chip *chip = i2c_get_clientdata(client);
1321     int ret = 0;
1322 
1323     bh1770_chip_on(chip);
1324 
1325     if (!pm_runtime_suspended(dev)) {
1326         /*
1327          * If we were enabled at suspend time, it is expected
1328          * everything works nice and smoothly
1329          */
1330         ret = bh1770_lux_rate(chip, chip->lux_rate_index);
1331         ret |= bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
1332 
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);
1339     }
1340     return ret;
1341 }
1342 #endif
1343 
1344 #ifdef CONFIG_PM
1345 static int bh1770_runtime_suspend(struct device *dev)
1346 {
1347     struct i2c_client *client = to_i2c_client(dev);
1348     struct bh1770_chip *chip = i2c_get_clientdata(client);
1349 
1350     bh1770_chip_off(chip);
1351 
1352     return 0;
1353 }
1354 
1355 static int bh1770_runtime_resume(struct device *dev)
1356 {
1357     struct i2c_client *client = to_i2c_client(dev);
1358     struct bh1770_chip *chip = i2c_get_clientdata(client);
1359 
1360     bh1770_chip_on(chip);
1361 
1362     return 0;
1363 }
1364 #endif
1365 
1366 static const struct i2c_device_id bh1770_id[] = {
1367     {"bh1770glc", 0 },
1368     {"sfh7770", 0 },
1369     {}
1370 };
1371 
1372 MODULE_DEVICE_TABLE(i2c, bh1770_id);
1373 
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)
1377 };
1378 
1379 static struct i2c_driver bh1770_driver = {
1380     .driver  = {
1381         .name   = "bh1770glc",
1382         .pm = &bh1770_pm_ops,
1383     },
1384     .probe    = bh1770_probe,
1385     .remove   = bh1770_remove,
1386     .id_table = bh1770_id,
1387 };
1388 
1389 module_i2c_driver(bh1770_driver);
1390 
1391 MODULE_DESCRIPTION("BH1770GLC / SFH7770 combined ALS and proximity sensor");
1392 MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1393 MODULE_LICENSE("GPL v2");