1 // SPDX-License-Identifier: GPL-2.0-only
3 * apds9802als.c - apds9802 ALS Driver
5 * Copyright (C) 2009 Intel Corp
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <linux/mutex.h>
18 #include <linux/sysfs.h>
19 #include <linux/pm_runtime.h>
21 #define ALS_MIN_RANGE_VAL 1
22 #define ALS_MAX_RANGE_VAL 2
23 #define POWER_STA_ENABLE 1
24 #define POWER_STA_DISABLE 0
26 #define DRIVER_NAME "apds9802als"
32 static ssize_t
als_sensing_range_show(struct device
*dev
,
33 struct device_attribute
*attr
, char *buf
)
35 struct i2c_client
*client
= to_i2c_client(dev
);
38 val
= i2c_smbus_read_byte_data(client
, 0x81);
42 return sprintf(buf
, "4095\n");
44 return sprintf(buf
, "65535\n");
47 static int als_wait_for_data_ready(struct device
*dev
)
49 struct i2c_client
*client
= to_i2c_client(dev
);
55 ret
= i2c_smbus_read_byte_data(client
, 0x86);
56 } while (!(ret
& 0x80) && retry
--);
59 dev_warn(dev
, "timeout waiting for data ready\n");
66 static ssize_t
als_lux0_input_data_show(struct device
*dev
,
67 struct device_attribute
*attr
, char *buf
)
69 struct i2c_client
*client
= to_i2c_client(dev
);
70 struct als_data
*data
= i2c_get_clientdata(client
);
74 /* Protect against parallel reads */
75 pm_runtime_get_sync(dev
);
76 mutex_lock(&data
->mutex
);
78 /* clear EOC interrupt status */
79 i2c_smbus_write_byte(client
, 0x40);
80 /* start measurement */
81 temp
= i2c_smbus_read_byte_data(client
, 0x81);
82 i2c_smbus_write_byte_data(client
, 0x81, temp
| 0x08);
84 ret_val
= als_wait_for_data_ready(dev
);
88 temp
= i2c_smbus_read_byte_data(client
, 0x8C); /* LSB data */
93 ret_val
= i2c_smbus_read_byte_data(client
, 0x8D); /* MSB data */
97 mutex_unlock(&data
->mutex
);
98 pm_runtime_put_sync(dev
);
100 temp
= (ret_val
<< 8) | temp
;
101 return sprintf(buf
, "%d\n", temp
);
103 mutex_unlock(&data
->mutex
);
104 pm_runtime_put_sync(dev
);
108 static ssize_t
als_sensing_range_store(struct device
*dev
,
109 struct device_attribute
*attr
, const char *buf
, size_t count
)
111 struct i2c_client
*client
= to_i2c_client(dev
);
112 struct als_data
*data
= i2c_get_clientdata(client
);
116 ret_val
= kstrtoul(buf
, 10, &val
);
122 else if (val
< 65536)
127 pm_runtime_get_sync(dev
);
129 /* Make sure nobody else reads/modifies/writes 0x81 while we
131 mutex_lock(&data
->mutex
);
133 ret_val
= i2c_smbus_read_byte_data(client
, 0x81);
137 /* Reset the bits before setting them */
138 ret_val
= ret_val
& 0xFA;
140 if (val
== 1) /* Setting detection range up to 4k LUX */
141 ret_val
= (ret_val
| 0x01);
142 else /* Setting detection range up to 64k LUX*/
143 ret_val
= (ret_val
| 0x00);
145 ret_val
= i2c_smbus_write_byte_data(client
, 0x81, ret_val
);
149 mutex_unlock(&data
->mutex
);
150 pm_runtime_put_sync(dev
);
154 mutex_unlock(&data
->mutex
);
155 pm_runtime_put_sync(dev
);
159 static int als_set_power_state(struct i2c_client
*client
, bool on_off
)
162 struct als_data
*data
= i2c_get_clientdata(client
);
164 mutex_lock(&data
->mutex
);
165 ret_val
= i2c_smbus_read_byte_data(client
, 0x80);
169 ret_val
= ret_val
| 0x01;
171 ret_val
= ret_val
& 0xFE;
172 ret_val
= i2c_smbus_write_byte_data(client
, 0x80, ret_val
);
174 mutex_unlock(&data
->mutex
);
178 static DEVICE_ATTR(lux0_sensor_range
, S_IRUGO
| S_IWUSR
,
179 als_sensing_range_show
, als_sensing_range_store
);
180 static DEVICE_ATTR(lux0_input
, S_IRUGO
, als_lux0_input_data_show
, NULL
);
182 static struct attribute
*mid_att_als
[] = {
183 &dev_attr_lux0_sensor_range
.attr
,
184 &dev_attr_lux0_input
.attr
,
188 static const struct attribute_group m_als_gr
= {
189 .name
= "apds9802als",
193 static int als_set_default_config(struct i2c_client
*client
)
196 /* Write the command and then switch on */
197 ret_val
= i2c_smbus_write_byte_data(client
, 0x80, 0x01);
199 dev_err(&client
->dev
, "failed default switch on write\n");
202 /* detection range: 1~64K Lux, maunal measurement */
203 ret_val
= i2c_smbus_write_byte_data(client
, 0x81, 0x08);
205 dev_err(&client
->dev
, "failed default LUX on write\n");
207 /* We always get 0 for the 1st measurement after system power on,
208 * so make sure it is finished before user asks for data.
210 als_wait_for_data_ready(&client
->dev
);
215 static int apds9802als_probe(struct i2c_client
*client
,
216 const struct i2c_device_id
*id
)
219 struct als_data
*data
;
221 data
= kzalloc(sizeof(struct als_data
), GFP_KERNEL
);
223 dev_err(&client
->dev
, "Memory allocation failed\n");
226 i2c_set_clientdata(client
, data
);
227 res
= sysfs_create_group(&client
->dev
.kobj
, &m_als_gr
);
229 dev_err(&client
->dev
, "device create file failed\n");
232 dev_info(&client
->dev
, "ALS chip found\n");
233 als_set_default_config(client
);
234 mutex_init(&data
->mutex
);
236 pm_runtime_set_active(&client
->dev
);
237 pm_runtime_enable(&client
->dev
);
245 static int apds9802als_remove(struct i2c_client
*client
)
247 struct als_data
*data
= i2c_get_clientdata(client
);
249 pm_runtime_get_sync(&client
->dev
);
251 als_set_power_state(client
, false);
252 sysfs_remove_group(&client
->dev
.kobj
, &m_als_gr
);
254 pm_runtime_disable(&client
->dev
);
255 pm_runtime_set_suspended(&client
->dev
);
256 pm_runtime_put_noidle(&client
->dev
);
264 static int apds9802als_suspend(struct device
*dev
)
266 struct i2c_client
*client
= to_i2c_client(dev
);
268 als_set_power_state(client
, false);
272 static int apds9802als_resume(struct device
*dev
)
274 struct i2c_client
*client
= to_i2c_client(dev
);
276 als_set_power_state(client
, true);
280 static UNIVERSAL_DEV_PM_OPS(apds9802als_pm_ops
, apds9802als_suspend
,
281 apds9802als_resume
, NULL
);
283 #define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
285 #else /* CONFIG_PM */
286 #define APDS9802ALS_PM_OPS NULL
287 #endif /* CONFIG_PM */
289 static const struct i2c_device_id apds9802als_id
[] = {
294 MODULE_DEVICE_TABLE(i2c
, apds9802als_id
);
296 static struct i2c_driver apds9802als_driver
= {
299 .pm
= APDS9802ALS_PM_OPS
,
301 .probe
= apds9802als_probe
,
302 .remove
= apds9802als_remove
,
303 .id_table
= apds9802als_id
,
306 module_i2c_driver(apds9802als_driver
);
308 MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
309 MODULE_DESCRIPTION("Avago apds9802als ALS Driver");
310 MODULE_LICENSE("GPL v2");