Lines Matching +full:stand +full:- +full:alone
1 // SPDX-License-Identifier: GPL-2.0-only
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
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 */
163 * Supported stand alone rates in ms from chip data sheet
170 * Supported stand alone rates in ms from chip data sheet
176 * interrupt control functions are called while keeping chip->mutex
182 chip->int_mode_lux = lux; in bh1770_lux_interrupt_control()
184 return i2c_smbus_write_byte_data(chip->client, in bh1770_lux_interrupt_control()
186 (lux << 1) | chip->int_mode_prox); in bh1770_lux_interrupt_control()
192 chip->int_mode_prox = ps; in bh1770_prox_interrupt_control()
193 return i2c_smbus_write_byte_data(chip->client, in bh1770_prox_interrupt_control()
195 (chip->int_mode_lux << 1) | (ps << 0)); in bh1770_prox_interrupt_control()
198 /* chip->mutex is always kept here */
202 if (pm_runtime_suspended(&chip->client->dev)) in bh1770_lux_rate()
206 if (chip->prox_enable_count) in bh1770_lux_rate()
209 return i2c_smbus_write_byte_data(chip->client, in bh1770_lux_rate()
219 chip->prox_rate_threshold : chip->prox_rate; in bh1770_prox_rate()
221 return i2c_smbus_write_byte_data(chip->client, in bh1770_prox_rate()
230 return i2c_smbus_write_byte_data(chip->client, in bh1770_led_cfg()
234 chip->prox_led); in bh1770_led_cfg()
246 adjusted = (u16)(((u32)(psraw + chip->prox_const) * chip->prox_coef) / in bh1770_psraw_to_adjusted()
257 raw = (((u32)ps * BH1770_COEF_SCALER) / chip->prox_coef); in bh1770_psadjusted_to_raw()
258 if (raw > chip->prox_const) in bh1770_psadjusted_to_raw()
259 raw = raw - chip->prox_const; in bh1770_psadjusted_to_raw()
269 * versions. Chip->mutex is kept when this is called.
276 if (pm_runtime_suspended(&chip->client->dev)) in bh1770_prox_set_threshold()
279 tmp = bh1770_psadjusted_to_raw(chip, chip->prox_threshold); in bh1770_prox_set_threshold()
280 chip->prox_threshold_hw = tmp; in bh1770_prox_set_threshold()
282 return i2c_smbus_write_byte_data(chip->client, BH1770_PS_TH_LED1, in bh1770_prox_set_threshold()
289 lux = ((u32)raw * chip->lux_corr) / BH1770_LUX_CORR_SCALE; in bh1770_lux_raw_to_adjusted()
296 return (u32)adjusted * BH1770_LUX_CORR_SCALE / chip->lux_corr; in bh1770_lux_adjusted_to_raw()
299 /* chip->mutex is kept when this is called */
307 if (pm_runtime_suspended(&chip->client->dev)) in bh1770_lux_update_thresholds()
321 if (chip->lux_thres_hi_onchip == threshold_hi && in bh1770_lux_update_thresholds()
322 chip->lux_thres_lo_onchip == threshold_lo) in bh1770_lux_update_thresholds()
325 chip->lux_thres_hi_onchip = threshold_hi; in bh1770_lux_update_thresholds()
326 chip->lux_thres_lo_onchip = threshold_lo; in bh1770_lux_update_thresholds()
333 ret = i2c_smbus_write_i2c_block_data(chip->client, in bh1770_lux_update_thresholds()
345 ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_0); in bh1770_lux_get_result()
350 ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_1); in bh1770_lux_get_result()
354 chip->lux_data_raw = data | ((ret & 0xff) << 8); in bh1770_lux_get_result()
364 tmp = (BH1770_LUX_CORR_SCALE * chip->lux_ga) / BH1770_LUX_GA_SCALE; in bh1770_get_corr_value()
366 tmp = (tmp * chip->lux_cf) / BH1770_LUX_CF_SCALE; in bh1770_get_corr_value()
368 tmp = (tmp * chip->lux_calib) / BH1770_CALIB_SCALER; in bh1770_get_corr_value()
375 return bh1770_lux_raw_to_adjusted(chip, chip->lux_data_raw); in bh1770_lux_read_result()
384 int ret = regulator_bulk_enable(ARRAY_SIZE(chip->regs), in bh1770_chip_on()
385 chip->regs); in bh1770_chip_on()
392 i2c_smbus_write_byte_data(chip->client, BH1770_ALS_CONTROL, in bh1770_chip_on()
401 chip->lux_data_raw = 0; in bh1770_chip_on()
402 chip->prox_data = 0; in bh1770_chip_on()
403 ret = i2c_smbus_write_byte_data(chip->client, in bh1770_chip_on()
407 chip->lux_thres_hi_onchip = BH1770_LUX_RANGE; in bh1770_chip_on()
408 chip->lux_thres_lo_onchip = 0; in bh1770_chip_on()
415 i2c_smbus_write_byte_data(chip->client, in bh1770_chip_off()
417 i2c_smbus_write_byte_data(chip->client, in bh1770_chip_off()
419 i2c_smbus_write_byte_data(chip->client, in bh1770_chip_off()
421 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs); in bh1770_chip_off()
424 /* chip->mutex is kept when this is called */
427 if (chip->prox_enable_count) { in bh1770_prox_mode_control()
428 chip->prox_force_update = true; /* Force immediate update */ in bh1770_prox_mode_control()
430 bh1770_lux_rate(chip, chip->lux_rate_index); in bh1770_prox_mode_control()
435 i2c_smbus_write_byte_data(chip->client, in bh1770_prox_mode_control()
438 chip->prox_data = 0; in bh1770_prox_mode_control()
439 bh1770_lux_rate(chip, chip->lux_rate_index); in bh1770_prox_mode_control()
441 i2c_smbus_write_byte_data(chip->client, in bh1770_prox_mode_control()
447 /* chip->mutex is kept when this is called */
454 ret = i2c_smbus_read_byte_data(chip->client, BH1770_PS_DATA_LED1); in bh1770_prox_read_result()
458 if (ret > chip->prox_threshold_hw) in bh1770_prox_read_result()
468 if (chip->lux_data_raw > PROX_IGNORE_LUX_LIMIT) in bh1770_prox_read_result()
471 chip->prox_data = bh1770_psraw_to_adjusted(chip, ret); in bh1770_prox_read_result()
474 if (chip->prox_data >= chip->prox_abs_thres || in bh1770_prox_read_result()
475 chip->prox_force_update) in bh1770_prox_read_result()
476 chip->prox_persistence_counter = chip->prox_persistence; in bh1770_prox_read_result()
478 chip->prox_force_update = false; in bh1770_prox_read_result()
482 if (chip->prox_persistence_counter < chip->prox_persistence) { in bh1770_prox_read_result()
483 chip->prox_persistence_counter++; in bh1770_prox_read_result()
484 ret = -ENODATA; in bh1770_prox_read_result()
490 chip->prox_persistence_counter = 0; in bh1770_prox_read_result()
492 chip->prox_data = 0; in bh1770_prox_read_result()
499 sysfs_notify(&chip->client->dev.kobj, NULL, "prox0_raw"); in bh1770_prox_read_result()
507 struct i2c_client *client = chip->client; in bh1770_detect()
521 chip->revision = (part & BH1770_REV_MASK) >> BH1770_REV_SHIFT; in bh1770_detect()
522 chip->prox_coef = BH1770_COEF_SCALER; in bh1770_detect()
523 chip->prox_const = 0; in bh1770_detect()
524 chip->lux_cf = BH1770_NEUTRAL_CF; in bh1770_detect()
528 snprintf(chip->chipname, sizeof(chip->chipname), "BH1770GLC"); in bh1770_detect()
534 snprintf(chip->chipname, sizeof(chip->chipname), "SFH7770"); in bh1770_detect()
536 chip->prox_coef = 819; /* 0.8 * BH1770_COEF_SCALER */ in bh1770_detect()
537 chip->prox_const = 40; in bh1770_detect()
541 ret = -ENODEV; in bh1770_detect()
543 dev_dbg(&client->dev, "BH1770 or SFH7770 not found\n"); in bh1770_detect()
549 * This work is re-scheduled at every proximity interrupt.
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.
562 mutex_lock(&chip->mutex); in bh1770_prox_work()
564 mutex_unlock(&chip->mutex); in bh1770_prox_work()
574 mutex_lock(&chip->mutex); in bh1770_irq()
575 status = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_PS_STATUS); in bh1770_irq()
578 i2c_smbus_read_byte_data(chip->client, BH1770_INTERRUPT); in bh1770_irq()
586 if (unlikely(chip->lux_wait_result)) { in bh1770_irq()
587 chip->lux_wait_result = false; in bh1770_irq()
588 wake_up(&chip->wait); in bh1770_irq()
590 chip->lux_threshold_hi, in bh1770_irq()
591 chip->lux_threshold_lo); in bh1770_irq()
596 i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT, in bh1770_irq()
600 sysfs_notify(&chip->client->dev.kobj, NULL, "lux0_input"); in bh1770_irq()
602 if (chip->int_mode_prox && (status & BH1770_INT_LEDS_INT)) { in bh1770_irq()
603 rate = prox_rates_ms[chip->prox_rate_threshold]; in bh1770_irq()
607 /* Re-enable interrupt logic */ in bh1770_irq()
608 i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT, in bh1770_irq()
609 (chip->int_mode_lux << 1) | in bh1770_irq()
610 (chip->int_mode_prox << 0)); in bh1770_irq()
611 mutex_unlock(&chip->mutex); in bh1770_irq()
619 * Simulate missing no-proximity interrupt 50ms after the in bh1770_irq()
622 cancel_delayed_work_sync(&chip->prox_work); in bh1770_irq()
623 schedule_delayed_work(&chip->prox_work, in bh1770_irq()
641 mutex_lock(&chip->mutex); in bh1770_power_state_store()
645 ret = bh1770_lux_rate(chip, chip->lux_rate_index); in bh1770_power_state_store()
661 chip->lux_wait_result = true; in bh1770_power_state_store()
668 mutex_unlock(&chip->mutex); in bh1770_power_state_store()
686 return -EIO; /* Chip is not enabled at all */ in bh1770_lux_result_show()
688 timeout = wait_event_interruptible_timeout(chip->wait, in bh1770_lux_result_show()
689 !chip->lux_wait_result, in bh1770_lux_result_show()
692 return -EIO; in bh1770_lux_result_show()
694 mutex_lock(&chip->mutex); in bh1770_lux_result_show()
696 mutex_unlock(&chip->mutex); in bh1770_lux_result_show()
719 mutex_lock(&chip->mutex); in bh1770_prox_enable_store()
721 if (!chip->prox_enable_count) in bh1770_prox_enable_store()
722 chip->prox_data = 0; in bh1770_prox_enable_store()
725 chip->prox_enable_count++; in bh1770_prox_enable_store()
726 else if (chip->prox_enable_count > 0) in bh1770_prox_enable_store()
727 chip->prox_enable_count--; in bh1770_prox_enable_store()
735 mutex_unlock(&chip->mutex); in bh1770_prox_enable_store()
745 mutex_lock(&chip->mutex); in bh1770_prox_enable_show()
746 len = sprintf(buf, "%d\n", chip->prox_enable_count); in bh1770_prox_enable_show()
747 mutex_unlock(&chip->mutex); in bh1770_prox_enable_show()
757 mutex_lock(&chip->mutex); in bh1770_prox_result_show()
758 if (chip->prox_enable_count && !pm_runtime_suspended(dev)) in bh1770_prox_result_show()
759 ret = sprintf(buf, "%d\n", chip->prox_data); in bh1770_prox_result_show()
761 ret = -EIO; in bh1770_prox_result_show()
762 mutex_unlock(&chip->mutex); in bh1770_prox_result_show()
779 sprintf(buf + pos - 1, "\n"); in bh1770_get_prox_rate_avail()
787 return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate_threshold]); in bh1770_get_prox_rate_above()
794 return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate]); in bh1770_get_prox_rate_below()
801 for (i = 0; i < ARRAY_SIZE(prox_rates_hz) - 1; i++) in bh1770_prox_rate_validate()
819 mutex_lock(&chip->mutex); in bh1770_set_prox_rate_above()
820 chip->prox_rate_threshold = bh1770_prox_rate_validate(value); in bh1770_set_prox_rate_above()
821 mutex_unlock(&chip->mutex); in bh1770_set_prox_rate_above()
837 mutex_lock(&chip->mutex); in bh1770_set_prox_rate_below()
838 chip->prox_rate = bh1770_prox_rate_validate(value); in bh1770_set_prox_rate_below()
839 mutex_unlock(&chip->mutex); in bh1770_set_prox_rate_below()
847 return sprintf(buf, "%d\n", chip->prox_threshold); in bh1770_get_prox_thres()
863 return -EINVAL; in bh1770_set_prox_thres()
865 mutex_lock(&chip->mutex); in bh1770_set_prox_thres()
866 chip->prox_threshold = value; in bh1770_set_prox_thres()
868 mutex_unlock(&chip->mutex); in bh1770_set_prox_thres()
879 return sprintf(buf, "%u\n", chip->prox_persistence); in bh1770_prox_persistence_show()
895 return -EINVAL; in bh1770_prox_persistence_store()
897 chip->prox_persistence = value; in bh1770_prox_persistence_store()
906 return sprintf(buf, "%u\n", chip->prox_abs_thres); in bh1770_prox_abs_thres_show()
922 return -EINVAL; in bh1770_prox_abs_thres_store()
924 chip->prox_abs_thres = value; in bh1770_prox_abs_thres_store()
933 return sprintf(buf, "%s rev %d\n", chip->chipname, chip->revision); in bh1770_chip_id_show()
948 mutex_lock(&chip->mutex); in bh1770_lux_calib_show()
949 len = sprintf(buf, "%u\n", chip->lux_calib); in bh1770_lux_calib_show()
950 mutex_unlock(&chip->mutex); in bh1770_lux_calib_show()
968 mutex_lock(&chip->mutex); in bh1770_lux_calib_store()
969 old_calib = chip->lux_calib; in bh1770_lux_calib_store()
970 chip->lux_calib = value; in bh1770_lux_calib_store()
973 chip->lux_calib = old_calib; in bh1770_lux_calib_store()
974 mutex_unlock(&chip->mutex); in bh1770_lux_calib_store()
975 return -EINVAL; in bh1770_lux_calib_store()
977 chip->lux_corr = new_corr; in bh1770_lux_calib_store()
979 bh1770_lux_update_thresholds(chip, chip->lux_threshold_hi, in bh1770_lux_calib_store()
980 chip->lux_threshold_lo); in bh1770_lux_calib_store()
982 mutex_unlock(&chip->mutex); in bh1770_lux_calib_store()
994 sprintf(buf + pos - 1, "\n"); in bh1770_get_lux_rate_avail()
1002 return sprintf(buf, "%d\n", lux_rates_hz[chip->lux_rate_index]); in bh1770_get_lux_rate()
1017 for (i = 0; i < ARRAY_SIZE(lux_rates_hz) - 1; i++) in bh1770_set_lux_rate()
1021 mutex_lock(&chip->mutex); in bh1770_set_lux_rate()
1022 chip->lux_rate_index = i; in bh1770_set_lux_rate()
1024 mutex_unlock(&chip->mutex); in bh1770_set_lux_rate()
1036 return sprintf(buf, "%d\n", chip->lux_threshold_hi); in bh1770_get_lux_thresh_above()
1043 return sprintf(buf, "%d\n", chip->lux_threshold_lo); in bh1770_get_lux_thresh_below()
1057 return -EINVAL; in bh1770_set_lux_thresh()
1059 mutex_lock(&chip->mutex); in bh1770_set_lux_thresh()
1065 if (!chip->lux_wait_result) in bh1770_set_lux_thresh()
1067 chip->lux_threshold_hi, in bh1770_set_lux_thresh()
1068 chip->lux_threshold_lo); in bh1770_set_lux_thresh()
1069 mutex_unlock(&chip->mutex); in bh1770_set_lux_thresh()
1079 int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_hi, buf); in bh1770_set_lux_thresh_above()
1090 int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_lo, buf); in bh1770_set_lux_thresh_below()
1171 chip = devm_kzalloc(&client->dev, sizeof *chip, GFP_KERNEL); in bh1770_probe()
1173 return -ENOMEM; in bh1770_probe()
1176 chip->client = client; in bh1770_probe()
1178 mutex_init(&chip->mutex); in bh1770_probe()
1179 init_waitqueue_head(&chip->wait); in bh1770_probe()
1180 INIT_DELAYED_WORK(&chip->prox_work, bh1770_prox_work); in bh1770_probe()
1182 if (client->dev.platform_data == NULL) { in bh1770_probe()
1183 dev_err(&client->dev, "platform data is mandatory\n"); in bh1770_probe()
1184 return -EINVAL; in bh1770_probe()
1187 chip->pdata = client->dev.platform_data; in bh1770_probe()
1188 chip->lux_calib = BH1770_LUX_NEUTRAL_CALIB_VALUE; in bh1770_probe()
1189 chip->lux_rate_index = BH1770_LUX_DEFAULT_RATE; in bh1770_probe()
1190 chip->lux_threshold_lo = BH1770_LUX_DEF_THRES; in bh1770_probe()
1191 chip->lux_threshold_hi = BH1770_LUX_DEF_THRES; in bh1770_probe()
1193 if (chip->pdata->glass_attenuation == 0) in bh1770_probe()
1194 chip->lux_ga = BH1770_NEUTRAL_GA; in bh1770_probe()
1196 chip->lux_ga = chip->pdata->glass_attenuation; in bh1770_probe()
1198 chip->prox_threshold = BH1770_PROX_DEF_THRES; in bh1770_probe()
1199 chip->prox_led = chip->pdata->led_def_curr; in bh1770_probe()
1200 chip->prox_abs_thres = BH1770_PROX_DEF_ABS_THRES; in bh1770_probe()
1201 chip->prox_persistence = BH1770_DEFAULT_PERSISTENCE; in bh1770_probe()
1202 chip->prox_rate_threshold = BH1770_PROX_DEF_RATE_THRESH; in bh1770_probe()
1203 chip->prox_rate = BH1770_PROX_DEFAULT_RATE; in bh1770_probe()
1204 chip->prox_data = 0; in bh1770_probe()
1206 chip->regs[0].supply = reg_vcc; in bh1770_probe()
1207 chip->regs[1].supply = reg_vleds; in bh1770_probe()
1209 err = devm_regulator_bulk_get(&client->dev, in bh1770_probe()
1210 ARRAY_SIZE(chip->regs), chip->regs); in bh1770_probe()
1212 dev_err(&client->dev, "Cannot get regulators\n"); in bh1770_probe()
1216 err = regulator_bulk_enable(ARRAY_SIZE(chip->regs), in bh1770_probe()
1217 chip->regs); in bh1770_probe()
1219 dev_err(&client->dev, "Cannot enable regulators\n"); in bh1770_probe()
1230 pm_runtime_set_active(&client->dev); in bh1770_probe()
1231 pm_runtime_enable(&client->dev); in bh1770_probe()
1233 chip->lux_corr = bh1770_get_corr_value(chip); in bh1770_probe()
1234 if (chip->lux_corr == 0) { in bh1770_probe()
1235 dev_err(&client->dev, "Improper correction values\n"); in bh1770_probe()
1236 err = -EINVAL; in bh1770_probe()
1240 if (chip->pdata->setup_resources) { in bh1770_probe()
1241 err = chip->pdata->setup_resources(); in bh1770_probe()
1243 err = -EINVAL; in bh1770_probe()
1248 err = sysfs_create_group(&chip->client->dev.kobj, in bh1770_probe()
1251 dev_err(&chip->client->dev, "Sysfs registration failed\n"); in bh1770_probe()
1260 err = request_threaded_irq(client->irq, NULL, in bh1770_probe()
1266 dev_err(&client->dev, "could not get IRQ %d\n", in bh1770_probe()
1267 client->irq); in bh1770_probe()
1270 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs); in bh1770_probe()
1273 sysfs_remove_group(&chip->client->dev.kobj, in bh1770_probe()
1276 if (chip->pdata->release_resources) in bh1770_probe()
1277 chip->pdata->release_resources(); in bh1770_probe()
1279 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs); in bh1770_probe()
1287 free_irq(client->irq, chip); in bh1770_remove()
1289 sysfs_remove_group(&chip->client->dev.kobj, in bh1770_remove()
1292 if (chip->pdata->release_resources) in bh1770_remove()
1293 chip->pdata->release_resources(); in bh1770_remove()
1295 cancel_delayed_work_sync(&chip->prox_work); in bh1770_remove()
1297 if (!pm_runtime_suspended(&client->dev)) in bh1770_remove()
1300 pm_runtime_disable(&client->dev); in bh1770_remove()
1301 pm_runtime_set_suspended(&client->dev); in bh1770_remove()
1330 ret = bh1770_lux_rate(chip, chip->lux_rate_index); in bh1770_resume()
1337 chip->lux_wait_result = true; in bh1770_resume()