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