25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/i2c.h>
35 #include <linux/wait.h>
36 #include <linux/slab.h>
38 #define BH1770_ALS_CONTROL 0x80
39 #define BH1770_PS_CONTROL 0x81
40 #define BH1770_I_LED 0x82
41 #define BH1770_I_LED3 0x83
42 #define BH1770_ALS_PS_MEAS 0x84
43 #define BH1770_PS_MEAS_RATE 0x85
44 #define BH1770_ALS_MEAS_RATE 0x86
45 #define BH1770_PART_ID 0x8a
46 #define BH1770_MANUFACT_ID 0x8b
47 #define BH1770_ALS_DATA_0 0x8c
48 #define BH1770_ALS_DATA_1 0x8d
49 #define BH1770_ALS_PS_STATUS 0x8e
50 #define BH1770_PS_DATA_LED1 0x8f
51 #define BH1770_PS_DATA_LED2 0x90
52 #define BH1770_PS_DATA_LED3 0x91
53 #define BH1770_INTERRUPT 0x92
54 #define BH1770_PS_TH_LED1 0x93
55 #define BH1770_PS_TH_LED2 0x94
56 #define BH1770_PS_TH_LED3 0x95
57 #define BH1770_ALS_TH_UP_0 0x96
58 #define BH1770_ALS_TH_UP_1 0x97
59 #define BH1770_ALS_TH_LOW_0 0x98
60 #define BH1770_ALS_TH_LOW_1 0x99
63 #define BH1770_MANUFACT_ROHM 0x01
64 #define BH1770_MANUFACT_OSRAM 0x03
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
75 #define BH1770_STANDBY 0x00
76 #define BH1770_FORCED 0x02
77 #define BH1770_STANDALONE 0x03
78 #define BH1770_SWRESET (0x01 << 2)
80 #define BH1770_PS_TRIG_MEAS (1 << 0)
81 #define BH1770_ALS_TRIG_MEAS (1 << 1)
84 #define BH1770_INT_OUTPUT_MODE (1 << 3)
85 #define BH1770_INT_POLARITY (1 << 2)
86 #define BH1770_INT_ALS_ENA (1 << 1)
87 #define BH1770_INT_PS_ENA (1 << 0)
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)
101 #define BH1770_LED1 0x00
103 #define BH1770_DISABLE 0
104 #define BH1770_ENABLE 1
105 #define BH1770_PROX_CHANNELS 1
107 #define BH1770_LUX_DEFAULT_RATE 1
108 #define BH1770_PROX_DEFAULT_RATE 1
109 #define BH1770_PROX_DEF_RATE_THRESH 6
110 #define BH1770_STARTUP_DELAY 50
111 #define BH1770_RESET_TIME 10
112 #define BH1770_TIMEOUT 2100
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
126 #define BH1770_NEUTRAL_CF BH1770_LUX_CF_SCALE
127 #define BH1770_LUX_CORR_SCALE 4096
129 #define PROX_ABOVE_THRESHOLD 1
130 #define PROX_BELOW_THRESHOLD 0
132 #define PROX_IGNORE_LUX_LIMIT 500
173 static const char reg_vcc[] =
"Vcc";
174 static const char reg_vleds[] =
"Vleds";
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};
184 static const u8 prox_curr_ma[] = {5, 10, 20, 50, 100, 150, 200};
190 static const s16 lux_rates_hz[] = {10, 5, 2, 1, 0};
196 static inline int bh1770_lux_interrupt_control(
struct bh1770_chip *
chip,
206 static inline int bh1770_prox_interrupt_control(
struct bh1770_chip *
chip,
219 if (pm_runtime_suspended(&chip->
client->dev))
244 static inline int bh1770_led_cfg(
struct bh1770_chip *chip)
260 static inline u8 bh1770_psraw_to_adjusted(
struct bh1770_chip *chip,
u8 psraw)
288 static int bh1770_prox_set_threshold(
struct bh1770_chip *chip)
293 if (pm_runtime_suspended(&chip->
client->dev))
310 static inline u16 bh1770_lux_adjusted_to_raw(
struct bh1770_chip *chip,
317 static int bh1770_lux_update_thresholds(
struct bh1770_chip *chip,
318 u16 threshold_hi,
u16 threshold_lo)
324 if (pm_runtime_suspended(&chip->
client->dev))
333 threshold_hi = bh1770_lux_adjusted_to_raw(chip, threshold_hi);
336 threshold_lo = bh1770_lux_adjusted_to_raw(chip, threshold_lo);
345 data[0] = threshold_hi;
346 data[1] = threshold_hi >> 8;
347 data[2] = threshold_lo;
348 data[3] = threshold_lo >> 8;
357 static int bh1770_lux_get_result(
struct bh1770_chip *chip)
389 static int bh1770_lux_read_result(
struct bh1770_chip *chip)
391 bh1770_lux_get_result(chip);
392 return bh1770_lux_raw_to_adjusted(chip, chip->
lux_data_raw);
399 static int bh1770_chip_on(
struct bh1770_chip *chip)
430 static void bh1770_chip_off(
struct bh1770_chip *chip)
442 static int bh1770_prox_mode_control(
struct bh1770_chip *chip)
448 bh1770_prox_set_threshold(chip);
449 bh1770_led_cfg(chip);
465 static int bh1770_prox_read_result(
struct bh1770_chip *chip)
488 chip->
prox_data = bh1770_psraw_to_adjusted(chip, ret);
515 bh1770_prox_rate(chip, mode);
560 dev_dbg(&client->
dev,
"BH1770 or SFH7770 not found\n");
580 bh1770_prox_read_result(chip);
602 bh1770_lux_get_result(chip);
606 bh1770_lux_update_thresholds(chip,
614 (0 << 1) | (0 << 0));
621 bh1770_prox_read_result(chip);
659 pm_runtime_get_sync(dev);
678 bh1770_prox_mode_control(chip);
679 }
else if (!pm_runtime_suspended(dev)) {
691 return sprintf(buf,
"%d\n", !pm_runtime_suspended(dev));
701 if (pm_runtime_suspended(dev))
711 ret =
sprintf(buf,
"%d\n", bh1770_lux_read_result(chip));
725 const char *buf,
size_t count)
746 if (!pm_runtime_suspended(dev))
747 bh1770_prox_mode_control(chip);
786 static ssize_t bh1770_get_prox_rate_avail(
struct device *dev,
791 for (i = 0; i <
ARRAY_SIZE(prox_rates_hz); i++)
792 pos +=
sprintf(buf + pos,
"%d ", prox_rates_hz[i]);
797 static ssize_t bh1770_get_prox_rate_above(
struct device *dev,
804 static ssize_t bh1770_get_prox_rate_below(
struct device *dev,
811 static int bh1770_prox_rate_validate(
int rate)
815 for (i = 0; i <
ARRAY_SIZE(prox_rates_hz) - 1; i++)
816 if (rate >= prox_rates_hz[i])
821 static ssize_t bh1770_set_prox_rate_above(
struct device *dev,
823 const char *buf,
size_t count)
837 static ssize_t bh1770_set_prox_rate_below(
struct device *dev,
839 const char *buf,
size_t count)
848 chip->
prox_rate = bh1770_prox_rate_validate(value);
862 const char *buf,
size_t count)
875 ret = bh1770_prox_set_threshold(chip);
882 static ssize_t bh1770_prox_persistence_show(
struct device *dev,
890 static ssize_t bh1770_prox_persistence_store(
struct device *dev,
892 const char *buf,
size_t len)
908 static ssize_t bh1770_prox_abs_thres_show(
struct device *dev,
915 static ssize_t bh1770_prox_abs_thres_store(
struct device *dev,
917 const char *buf,
size_t len)
940 static ssize_t bh1770_lux_calib_default_show(
struct device *dev,
960 const char *buf,
size_t len)
973 new_corr = bh1770_get_corr_value(chip);
994 for (i = 0; i <
ARRAY_SIZE(lux_rates_hz); i++)
995 pos +=
sprintf(buf + pos,
"%d ", lux_rates_hz[i]);
1009 const char *buf,
size_t count)
1012 unsigned long rate_hz;
1018 for (i = 0; i <
ARRAY_SIZE(lux_rates_hz) - 1; i++)
1019 if (rate_hz >= lux_rates_hz[i])
1024 ret = bh1770_lux_rate(chip, i);
1033 static ssize_t bh1770_get_lux_thresh_above(
struct device *dev,
1040 static ssize_t bh1770_get_lux_thresh_below(
struct device *dev,
1051 unsigned long thresh;
1066 ret = bh1770_lux_update_thresholds(chip,
1074 static ssize_t bh1770_set_lux_thresh_above(
struct device *dev,
1076 const char *buf,
size_t len)
1085 static ssize_t bh1770_set_lux_thresh_below(
struct device *dev,
1087 const char *buf,
size_t len)
1097 bh1770_prox_enable_store);
1099 bh1770_prox_abs_thres_show,
1100 bh1770_prox_abs_thres_store);
1102 bh1770_get_prox_thres,
1103 bh1770_set_prox_thres);
1107 bh1770_prox_persistence_show,
1108 bh1770_prox_persistence_store);
1110 bh1770_get_prox_rate_above,
1111 bh1770_set_prox_rate_above);
1113 bh1770_get_prox_rate_below,
1114 bh1770_set_prox_rate_below);
1118 bh1770_lux_calib_store);
1120 bh1770_lux_calib_default_show,
1125 bh1770_set_lux_rate);
1128 bh1770_get_lux_thresh_above,
1129 bh1770_set_lux_thresh_above);
1131 bh1770_get_lux_thresh_below,
1132 bh1770_set_lux_thresh_below);
1135 bh1770_power_state_store);
1138 static struct attribute *sysfs_attrs[] = {
1139 &dev_attr_lux0_calibscale.attr,
1140 &dev_attr_lux0_calibscale_default.attr,
1141 &dev_attr_lux0_input.attr,
1142 &dev_attr_lux0_sensor_range.attr,
1143 &dev_attr_lux0_rate.attr,
1144 &dev_attr_lux0_rate_avail.attr,
1145 &dev_attr_lux0_thresh_above_value.attr,
1146 &dev_attr_lux0_thresh_below_value.attr,
1147 &dev_attr_prox0_raw.attr,
1148 &dev_attr_prox0_sensor_range.attr,
1149 &dev_attr_prox0_raw_en.attr,
1150 &dev_attr_prox0_thresh_above_count.attr,
1151 &dev_attr_prox0_rate_above.attr,
1152 &dev_attr_prox0_rate_below.attr,
1153 &dev_attr_prox0_rate_avail.attr,
1154 &dev_attr_prox0_thresh_above0_value.attr,
1155 &dev_attr_prox0_thresh_above1_value.attr,
1156 &dev_attr_chip_id.attr,
1157 &dev_attr_power_state.attr,
1162 .attrs = sysfs_attrs
1175 i2c_set_clientdata(client, chip);
1182 if (client->
dev.platform_data ==
NULL) {
1183 dev_err(&client->
dev,
"platform data is mandatory\n");
1188 chip->
pdata = client->
dev.platform_data;
1194 if (chip->
pdata->glass_attenuation == 0)
1207 chip->
regs[0].supply = reg_vcc;
1208 chip->
regs[1].supply = reg_vleds;
1213 dev_err(&client->
dev,
"Cannot get regulators\n");
1220 dev_err(&client->
dev,
"Cannot enable regulators\n");
1225 err = bh1770_detect(chip);
1230 bh1770_chip_on(chip);
1231 pm_runtime_set_active(&client->
dev);
1234 chip->
lux_corr = bh1770_get_corr_value(chip);
1236 dev_err(&client->
dev,
"Improper correction values\n");
1241 if (chip->
pdata->setup_resources) {
1242 err = chip->
pdata->setup_resources();
1250 &bh1770_attribute_group);
1267 dev_err(&client->
dev,
"could not get IRQ %d\n",
1275 &bh1770_attribute_group);
1277 if (chip->
pdata->release_resources)
1278 chip->
pdata->release_resources();
1290 struct bh1770_chip *chip = i2c_get_clientdata(client);
1295 &bh1770_attribute_group);
1297 if (chip->
pdata->release_resources)
1298 chip->
pdata->release_resources();
1302 if (!pm_runtime_suspended(&client->
dev))
1303 bh1770_chip_off(chip);
1305 pm_runtime_disable(&client->
dev);
1306 pm_runtime_set_suspended(&client->
dev);
1317 struct bh1770_chip *chip = i2c_get_clientdata(client);
1319 bh1770_chip_off(chip);
1327 struct bh1770_chip *chip = i2c_get_clientdata(client);
1330 bh1770_chip_on(chip);
1332 if (!pm_runtime_suspended(dev)) {
1345 bh1770_prox_mode_control(chip);
1351 #define bh1770_suspend NULL
1352 #define bh1770_shutdown NULL
1353 #define bh1770_resume NULL
1356 #ifdef CONFIG_PM_RUNTIME
1357 static int bh1770_runtime_suspend(
struct device *dev)
1360 struct bh1770_chip *chip = i2c_get_clientdata(client);
1362 bh1770_chip_off(chip);
1367 static int bh1770_runtime_resume(
struct device *dev)
1370 struct bh1770_chip *chip = i2c_get_clientdata(client);
1372 bh1770_chip_on(chip);
1386 static const struct dev_pm_ops bh1770_pm_ops = {
1393 .name =
"bh1770glc",
1395 .pm = &bh1770_pm_ops,
1397 .probe = bh1770_probe,
1399 .id_table = bh1770_id,