24 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
34 #define ALS_MIN_RANGE_VAL 1
35 #define ALS_MAX_RANGE_VAL 2
36 #define POWER_STA_ENABLE 1
37 #define POWER_STA_DISABLE 0
39 #define DRIVER_NAME "apds9802als"
60 static int als_wait_for_data_ready(
struct device *dev)
69 }
while (!(ret & 0x80) && retry--);
72 dev_warn(dev,
"timeout waiting for data ready\n");
88 pm_runtime_get_sync(dev);
97 ret_val = als_wait_for_data_ready(dev);
111 pm_runtime_put_sync(dev);
113 temp = (ret_val << 8) | temp;
114 return sprintf(buf,
"%d\n", temp);
117 pm_runtime_put_sync(dev);
125 struct als_data *data = i2c_get_clientdata(client);
134 else if (val < 65536)
139 pm_runtime_get_sync(dev);
150 ret_val = ret_val & 0xFA;
153 ret_val = (ret_val | 0x01);
155 ret_val = (ret_val | 0x00);
162 pm_runtime_put_sync(dev);
167 pm_runtime_put_sync(dev);
171 static int als_set_power_state(
struct i2c_client *client,
bool on_off)
174 struct als_data *data = i2c_get_clientdata(client);
181 ret_val = ret_val | 0x01;
183 ret_val = ret_val & 0xFE;
191 als_sensing_range_show, als_sensing_range_store);
194 static struct attribute *mid_att_als[] = {
195 &dev_attr_lux0_sensor_range.attr,
196 &dev_attr_lux0_input.attr,
201 .name =
"apds9802als",
205 static int als_set_default_config(
struct i2c_client *client)
211 dev_err(&client->
dev,
"failed default switch on write\n");
217 dev_err(&client->
dev,
"failed default LUX on write\n");
222 als_wait_for_data_ready(&client->
dev);
227 static int apds9802als_probe(
struct i2c_client *client,
235 dev_err(&client->
dev,
"Memory allocation failed\n");
238 i2c_set_clientdata(client, data);
241 dev_err(&client->
dev,
"device create file failed\n");
245 als_set_default_config(client);
248 pm_runtime_set_active(&client->
dev);
259 struct als_data *data = i2c_get_clientdata(client);
261 pm_runtime_get_sync(&client->
dev);
263 als_set_power_state(client,
false);
266 pm_runtime_disable(&client->
dev);
267 pm_runtime_set_suspended(&client->
dev);
268 pm_runtime_put_noidle(&client->
dev);
277 als_set_power_state(client,
false);
283 als_set_default_config(client);
287 static int apds9802als_runtime_suspend(
struct device *dev)
291 als_set_power_state(client,
false);
295 static int apds9802als_runtime_resume(
struct device *dev)
299 als_set_power_state(client,
true);
303 static const struct dev_pm_ops apds9802als_pm_ops = {
305 .runtime_resume = apds9802als_runtime_resume,
308 #define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
311 #define apds9802als_suspend NULL
312 #define apds9802als_resume NULL
313 #define APDS9802ALS_PM_OPS NULL
323 static struct i2c_driver apds9802als_driver = {
328 .probe = apds9802als_probe,
332 .id_table = apds9802als_id,