2 * apds9802als.c - apds9802 ALS Driver
4 * Copyright (C) 2009 Intel Corp
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
28 #include <linux/err.h>
29 #include <linux/delay.h>
30 #include <linux/mutex.h>
31 #include <linux/sysfs.h>
32 #include <linux/pm_runtime.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"
45 static ssize_t als_sensing_range_show(struct device *dev,
46 struct device_attribute *attr, char *buf)
48 struct i2c_client *client = to_i2c_client(dev);
51 val = i2c_smbus_read_byte_data(client, 0x81);
55 return sprintf(buf, "4095\n");
57 return sprintf(buf, "65535\n");
60 static int als_wait_for_data_ready(struct device *dev)
62 struct i2c_client *client = to_i2c_client(dev);
68 ret = i2c_smbus_read_byte_data(client, 0x86);
69 } while (!(ret & 0x80) && retry--);
72 dev_warn(dev, "timeout waiting for data ready\n");
79 static ssize_t als_lux0_input_data_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
82 struct i2c_client *client = to_i2c_client(dev);
83 struct als_data *data = i2c_get_clientdata(client);
87 /* Protect against parallel reads */
88 pm_runtime_get_sync(dev);
89 mutex_lock(&data->mutex);
91 /* clear EOC interrupt status */
92 i2c_smbus_write_byte(client, 0x40);
93 /* start measurement */
94 temp = i2c_smbus_read_byte_data(client, 0x81);
95 i2c_smbus_write_byte_data(client, 0x81, temp | 0x08);
97 ret_val = als_wait_for_data_ready(dev);
101 temp = i2c_smbus_read_byte_data(client, 0x8C); /* LSB data */
106 ret_val = i2c_smbus_read_byte_data(client, 0x8D); /* MSB data */
110 mutex_unlock(&data->mutex);
111 pm_runtime_put_sync(dev);
113 temp = (ret_val << 8) | temp;
114 return sprintf(buf, "%d\n", temp);
116 mutex_unlock(&data->mutex);
117 pm_runtime_put_sync(dev);
121 static ssize_t als_sensing_range_store(struct device *dev,
122 struct device_attribute *attr, const char *buf, size_t count)
124 struct i2c_client *client = to_i2c_client(dev);
125 struct als_data *data = i2c_get_clientdata(client);
129 ret_val = kstrtoul(buf, 10, &val);
135 else if (val < 65536)
140 pm_runtime_get_sync(dev);
142 /* Make sure nobody else reads/modifies/writes 0x81 while we
144 mutex_lock(&data->mutex);
146 ret_val = i2c_smbus_read_byte_data(client, 0x81);
150 /* Reset the bits before setting them */
151 ret_val = ret_val & 0xFA;
153 if (val == 1) /* Setting detection range up to 4k LUX */
154 ret_val = (ret_val | 0x01);
155 else /* Setting detection range up to 64k LUX*/
156 ret_val = (ret_val | 0x00);
158 ret_val = i2c_smbus_write_byte_data(client, 0x81, ret_val);
162 mutex_unlock(&data->mutex);
163 pm_runtime_put_sync(dev);
167 mutex_unlock(&data->mutex);
168 pm_runtime_put_sync(dev);
172 static int als_set_power_state(struct i2c_client *client, bool on_off)
175 struct als_data *data = i2c_get_clientdata(client);
177 mutex_lock(&data->mutex);
178 ret_val = i2c_smbus_read_byte_data(client, 0x80);
182 ret_val = ret_val | 0x01;
184 ret_val = ret_val & 0xFE;
185 ret_val = i2c_smbus_write_byte_data(client, 0x80, ret_val);
187 mutex_unlock(&data->mutex);
191 static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
192 als_sensing_range_show, als_sensing_range_store);
193 static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux0_input_data_show, NULL);
195 static struct attribute *mid_att_als[] = {
196 &dev_attr_lux0_sensor_range.attr,
197 &dev_attr_lux0_input.attr,
201 static struct attribute_group m_als_gr = {
202 .name = "apds9802als",
206 static int als_set_default_config(struct i2c_client *client)
209 /* Write the command and then switch on */
210 ret_val = i2c_smbus_write_byte_data(client, 0x80, 0x01);
212 dev_err(&client->dev, "failed default switch on write\n");
215 /* detection range: 1~64K Lux, maunal measurement */
216 ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x08);
218 dev_err(&client->dev, "failed default LUX on write\n");
220 /* We always get 0 for the 1st measurement after system power on,
221 * so make sure it is finished before user asks for data.
223 als_wait_for_data_ready(&client->dev);
228 static int apds9802als_probe(struct i2c_client *client,
229 const struct i2c_device_id *id)
232 struct als_data *data;
234 data = kzalloc(sizeof(struct als_data), GFP_KERNEL);
236 dev_err(&client->dev, "Memory allocation failed\n");
239 i2c_set_clientdata(client, data);
240 res = sysfs_create_group(&client->dev.kobj, &m_als_gr);
242 dev_err(&client->dev, "device create file failed\n");
245 dev_info(&client->dev, "ALS chip found\n");
246 als_set_default_config(client);
247 mutex_init(&data->mutex);
249 pm_runtime_set_active(&client->dev);
250 pm_runtime_enable(&client->dev);
258 static int apds9802als_remove(struct i2c_client *client)
260 struct als_data *data = i2c_get_clientdata(client);
262 pm_runtime_get_sync(&client->dev);
264 als_set_power_state(client, false);
265 sysfs_remove_group(&client->dev.kobj, &m_als_gr);
267 pm_runtime_disable(&client->dev);
268 pm_runtime_set_suspended(&client->dev);
269 pm_runtime_put_noidle(&client->dev);
277 static int apds9802als_suspend(struct device *dev)
279 struct i2c_client *client = to_i2c_client(dev);
281 als_set_power_state(client, false);
285 static int apds9802als_resume(struct device *dev)
287 struct i2c_client *client = to_i2c_client(dev);
289 als_set_power_state(client, true);
293 static UNIVERSAL_DEV_PM_OPS(apds9802als_pm_ops, apds9802als_suspend,
294 apds9802als_resume, NULL);
296 #define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
298 #else /* CONFIG_PM */
299 #define APDS9802ALS_PM_OPS NULL
300 #endif /* CONFIG_PM */
302 static struct i2c_device_id apds9802als_id[] = {
307 MODULE_DEVICE_TABLE(i2c, apds9802als_id);
309 static struct i2c_driver apds9802als_driver = {
312 .pm = APDS9802ALS_PM_OPS,
314 .probe = apds9802als_probe,
315 .remove = apds9802als_remove,
316 .id_table = apds9802als_id,
319 module_i2c_driver(apds9802als_driver);
321 MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
322 MODULE_DESCRIPTION("Avago apds9802als ALS Driver");
323 MODULE_LICENSE("GPL v2");