25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/i2c.h>
33 #include <linux/wait.h>
34 #include <linux/slab.h>
38 #define APDS990X_ENABLE 0x00
39 #define APDS990X_ATIME 0x01
40 #define APDS990X_PTIME 0x02
41 #define APDS990X_WTIME 0x03
42 #define APDS990X_AILTL 0x04
43 #define APDS990X_AILTH 0x05
44 #define APDS990X_AIHTL 0x06
45 #define APDS990X_AIHTH 0x07
46 #define APDS990X_PILTL 0x08
47 #define APDS990X_PILTH 0x09
48 #define APDS990X_PIHTL 0x0a
49 #define APDS990X_PIHTH 0x0b
50 #define APDS990X_PERS 0x0c
51 #define APDS990X_CONFIG 0x0d
52 #define APDS990X_PPCOUNT 0x0e
53 #define APDS990X_CONTROL 0x0f
54 #define APDS990X_REV 0x11
55 #define APDS990X_ID 0x12
56 #define APDS990X_STATUS 0x13
57 #define APDS990X_CDATAL 0x14
58 #define APDS990X_CDATAH 0x15
59 #define APDS990X_IRDATAL 0x16
60 #define APDS990X_IRDATAH 0x17
61 #define APDS990X_PDATAL 0x18
62 #define APDS990X_PDATAH 0x19
65 #define APDS990X_MAX_AGAIN 3
68 #define APDS990X_EN_PIEN (0x1 << 5)
69 #define APDS990X_EN_AIEN (0x1 << 4)
70 #define APDS990X_EN_WEN (0x1 << 3)
71 #define APDS990X_EN_PEN (0x1 << 2)
72 #define APDS990X_EN_AEN (0x1 << 1)
73 #define APDS990X_EN_PON (0x1 << 0)
74 #define APDS990X_EN_DISABLE_ALL 0
77 #define APDS990X_ST_PINT (0x1 << 5)
78 #define APDS990X_ST_AINT (0x1 << 4)
81 #define APDS990x_CMD_TYPE_MASK (0x03 << 5)
82 #define APDS990x_CMD_TYPE_RB (0x00 << 5)
83 #define APDS990x_CMD_TYPE_INC (0x01 << 5)
84 #define APDS990x_CMD_TYPE_SPE (0x03 << 5)
86 #define APDS990x_ADDR_SHIFT 0
87 #define APDS990x_CMD 0x80
90 #define APDS990X_INT_ACK_ALS 0x6
91 #define APDS990X_INT_ACK_PS 0x5
92 #define APDS990X_INT_ACK_BOTH 0x7
95 #define APDS990X_PTIME_DEFAULT 0xff
98 #define APDS990X_WTIME_DEFAULT 0xee
100 #define APDS990X_TIME_TO_ADC 1024
103 #define APDS990X_APERS_SHIFT 0
104 #define APDS990X_PPERS_SHIFT 4
107 #define APDS990X_ID_0 0x0
108 #define APDS990X_ID_4 0x4
109 #define APDS990X_ID_29 0x29
112 #define APDS_PGAIN_1X 0x0
113 #define APDS_PDIODE_IR 0x2
115 #define APDS990X_LUX_OUTPUT_SCALE 10
167 #define APDS_CALIB_SCALER 8192
168 #define APDS_LUX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
169 #define APDS_PROX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
171 #define APDS_PROX_DEF_THRES 600
172 #define APDS_PROX_HYSTERESIS 50
173 #define APDS_LUX_DEF_THRES_HI 101
174 #define APDS_LUX_DEF_THRES_LO 100
175 #define APDS_DEFAULT_PROX_PERS 1
177 #define APDS_TIMEOUT 2000
178 #define APDS_STARTUP_DELAY 25000
179 #define APDS_RANGE 65535
180 #define APDS_PROX_RANGE 1023
181 #define APDS_LUX_GAIN_LO_LIMIT 100
182 #define APDS_LUX_GAIN_LO_LIMIT_STRICT 25
185 #define TIME_STEP_SCALER 32
187 #define APDS_LUX_AVERAGING_TIME 50
188 #define APDS_LUX_DEFAULT_RATE 200
190 static const u8 again[] = {1, 8, 16, 120};
191 static const u8 ir_currents[] = {100, 50, 25, 12};
194 static const u16 arates_hz[] = {10, 5, 2, 1};
195 static const u8 apersis[] = {1, 2, 4, 5};
198 static const char reg_vcc[] =
"Vdd";
199 static const char reg_vled[] =
"Vled";
295 thres = lux * cpl / 64;
310 thres = (chip->
rcf.cf1 * thres + chip->
rcf.irf1 *
ir) /
313 thres = (chip->
rcf.cf2 * thres + chip->
rcf.irf2 *
ir) /
321 static inline int apds990x_set_atime(
struct apds990x_chip *chip,
u32 time_ms)
325 chip->
atime = time_ms;
334 static int apds990x_refresh_pthres(
struct apds990x_chip *chip,
int data)
339 if (pm_runtime_suspended(&chip->
client->dev))
359 static int apds990x_refresh_athres(
struct apds990x_chip *chip)
363 if (pm_runtime_suspended(&chip->
client->dev))
375 static void apds990x_force_a_refresh(
struct apds990x_chip *chip)
383 static void apds990x_force_p_refresh(
struct apds990x_chip *chip)
417 else if (next_again != curr_again &&
438 apds990x_force_a_refresh(chip);
440 apds990x_refresh_athres(chip);
458 iac =
max(iac1, iac2);
495 apds990x_ack_int(chip, status);
498 if (!pm_runtime_suspended(&chip->
client->dev)) {
507 chip->
lux_raw = apds990x_get_lux(chip,
511 if (apds990x_calc_again(chip) == 0) {
534 apds990x_read_word(chip,
538 apds990x_refresh_pthres(chip, chip->
prox_data);
612 #if defined(CONFIG_PM) || defined(CONFIG_PM_RUNTIME)
624 apds990x_configure(chip);
625 apds990x_mode_on(chip);
645 if (pm_runtime_suspended(dev))
657 result =
APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE;
660 result / APDS990X_LUX_OUTPUT_SCALE,
661 result % APDS990X_LUX_OUTPUT_SCALE);
676 static ssize_t apds990x_lux_calib_format_show(
struct device *dev,
683 apds990x_lux_calib_format_show,
NULL);
695 const char *buf,
size_t len)
712 apds990x_lux_calib_store);
720 pos +=
sprintf(buf + pos,
"%d ", arates_hz[i]);
737 if (rate >= arates_hz[i])
745 chip->
arate = arates_hz[
i];
748 if (pm_runtime_suspended(&chip->
client->dev))
759 const char *buf,
size_t len)
769 ret = apds990x_set_arate(chip, value);
780 apds990x_rate_store);
787 if (pm_runtime_suspended(dev) || !chip->
prox_en)
813 static ssize_t apds990x_prox_enable_store(
struct device *dev,
815 const char *buf,
size_t len)
833 if (!pm_runtime_suspended(dev))
834 apds990x_mode_on(chip);
840 apds990x_prox_enable_store);
842 static const char reporting_modes[][9] = {
"trigger",
"periodic"};
844 static ssize_t apds990x_prox_reporting_mode_show(
struct device *dev,
852 static ssize_t apds990x_prox_reporting_mode_store(
struct device *dev,
854 const char *buf,
size_t len)
868 apds990x_prox_reporting_mode_show,
869 apds990x_prox_reporting_mode_store);
871 static ssize_t apds990x_prox_reporting_avail_show(
struct device *dev,
874 return sprintf(buf,
"%s %s\n", reporting_modes[0], reporting_modes[1]);
878 apds990x_prox_reporting_avail_show,
NULL);
881 static ssize_t apds990x_lux_thresh_above_show(
struct device *dev,
888 static ssize_t apds990x_lux_thresh_below_show(
struct device *dev,
899 unsigned long thresh;
914 apds990x_refresh_athres(chip);
920 static ssize_t apds990x_lux_thresh_above_store(
struct device *dev,
922 const char *buf,
size_t len)
925 int ret = apds990x_set_lux_thresh(chip, &chip->
lux_thres_hi, buf);
931 static ssize_t apds990x_lux_thresh_below_store(
struct device *dev,
933 const char *buf,
size_t len)
936 int ret = apds990x_set_lux_thresh(chip, &chip->
lux_thres_lo, buf);
943 apds990x_lux_thresh_above_show,
944 apds990x_lux_thresh_above_store);
947 apds990x_lux_thresh_below_show,
948 apds990x_lux_thresh_below_store);
950 static ssize_t apds990x_prox_threshold_show(
struct device *dev,
957 static ssize_t apds990x_prox_threshold_store(
struct device *dev,
959 const char *buf,
size_t len)
974 apds990x_force_p_refresh(chip);
980 apds990x_prox_threshold_show,
981 apds990x_prox_threshold_store);
986 return sprintf(buf,
"%d\n", !pm_runtime_suspended(dev));
990 static ssize_t apds990x_power_state_store(
struct device *dev,
992 const char *buf,
size_t len)
1000 pm_runtime_get_sync(dev);
1003 apds990x_force_a_refresh(chip);
1004 apds990x_force_p_refresh(chip);
1007 if (!pm_runtime_suspended(dev))
1008 pm_runtime_put(dev);
1014 apds990x_power_state_show,
1015 apds990x_power_state_store);
1026 static struct attribute *sysfs_attrs_ctrl[] = {
1027 &dev_attr_lux0_calibscale.attr,
1028 &dev_attr_lux0_calibscale_default.attr,
1029 &dev_attr_lux0_input.attr,
1030 &dev_attr_lux0_sensor_range.attr,
1031 &dev_attr_lux0_rate.attr,
1032 &dev_attr_lux0_rate_avail.attr,
1033 &dev_attr_lux0_thresh_above_value.attr,
1034 &dev_attr_lux0_thresh_below_value.attr,
1035 &dev_attr_prox0_raw_en.attr,
1036 &dev_attr_prox0_raw.attr,
1037 &dev_attr_prox0_sensor_range.attr,
1038 &dev_attr_prox0_thresh_above_value.attr,
1039 &dev_attr_prox0_reporting_mode.attr,
1040 &dev_attr_prox0_reporting_mode_avail.attr,
1041 &dev_attr_chip_id.attr,
1042 &dev_attr_power_state.attr,
1047 {.attrs = sysfs_attrs_ctrl },
1060 i2c_set_clientdata(client, chip);
1065 chip->
pdata = client->
dev.platform_data;
1068 dev_err(&client->
dev,
"platform data is mandatory\n");
1073 if (chip->
pdata->cf.ga == 0) {
1076 chip->
cf.cf1 = 4096;
1077 chip->
cf.irf1 = 9134;
1078 chip->
cf.cf2 = 2867;
1079 chip->
cf.irf2 = 5816;
1088 (chip->
cf.cf1 - chip->
cf.cf2);
1111 chip->
regs[0].supply = reg_vcc;
1112 chip->
regs[1].supply = reg_vled;
1117 dev_err(&client->
dev,
"Cannot get regulators\n");
1123 dev_err(&client->
dev,
"Cannot enable regulators\n");
1129 err = apds990x_detect(chip);
1131 dev_err(&client->
dev,
"APDS990X not found\n");
1135 pm_runtime_set_active(&client->
dev);
1137 apds990x_configure(chip);
1139 apds990x_mode_on(chip);
1143 if (chip->
pdata->setup_resources) {
1144 err = chip->
pdata->setup_resources();
1152 apds990x_attribute_group);
1164 dev_err(&client->
dev,
"could not get IRQ %d\n",
1171 &apds990x_attribute_group[0]);
1173 if (chip->
pdata && chip->
pdata->release_resources)
1174 chip->
pdata->release_resources();
1190 apds990x_attribute_group);
1192 if (chip->
pdata && chip->
pdata->release_resources)
1193 chip->
pdata->release_resources();
1195 if (!pm_runtime_suspended(&client->
dev))
1196 apds990x_chip_off(chip);
1198 pm_runtime_disable(&client->
dev);
1199 pm_runtime_set_suspended(&client->
dev);
1213 apds990x_chip_off(chip);
1226 apds990x_chip_on(chip);
1231 #define apds990x_suspend NULL
1232 #define apds990x_resume NULL
1233 #define apds990x_shutdown NULL
1236 #ifdef CONFIG_PM_RUNTIME
1237 static int apds990x_runtime_suspend(
struct device *dev)
1242 apds990x_chip_off(chip);
1246 static int apds990x_runtime_resume(
struct device *dev)
1251 apds990x_chip_on(chip);
1264 static const struct dev_pm_ops apds990x_pm_ops = {
1267 apds990x_runtime_resume,
1275 .pm = &apds990x_pm_ops,
1277 .probe = apds990x_probe,
1279 .id_table = apds990x_id,