anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * apds9802als.c - apds9802 ALS Driver |
| 3 | * |
| 4 | * Copyright (C) 2009 Intel Corp |
| 5 | * |
| 6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 7 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 21 | * |
| 22 | */ |
| 23 | |
| 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> |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 32 | #include <linux/pm_runtime.h> |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 33 | |
| 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 |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 38 | |
| 39 | #define DRIVER_NAME "apds9802als" |
| 40 | |
| 41 | struct als_data { |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 42 | struct mutex mutex; |
| 43 | }; |
| 44 | |
| 45 | static ssize_t als_sensing_range_show(struct device *dev, |
| 46 | struct device_attribute *attr, char *buf) |
| 47 | { |
| 48 | struct i2c_client *client = to_i2c_client(dev); |
| 49 | int val; |
| 50 | |
| 51 | val = i2c_smbus_read_byte_data(client, 0x81); |
| 52 | if (val < 0) |
| 53 | return val; |
| 54 | if (val & 1) |
| 55 | return sprintf(buf, "4095\n"); |
| 56 | else |
| 57 | return sprintf(buf, "65535\n"); |
| 58 | } |
| 59 | |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 60 | static int als_wait_for_data_ready(struct device *dev) |
| 61 | { |
| 62 | struct i2c_client *client = to_i2c_client(dev); |
| 63 | int ret; |
| 64 | int retry = 10; |
| 65 | |
| 66 | do { |
| 67 | msleep(30); |
| 68 | ret = i2c_smbus_read_byte_data(client, 0x86); |
| 69 | } while (!(ret & 0x80) && retry--); |
| 70 | |
Axel Lin | 644a9d3 | 2012-11-16 17:17:50 +0800 | [diff] [blame] | 71 | if (retry < 0) { |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 72 | dev_warn(dev, "timeout waiting for data ready\n"); |
| 73 | return -ETIMEDOUT; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 79 | static ssize_t als_lux0_input_data_show(struct device *dev, |
| 80 | struct device_attribute *attr, char *buf) |
| 81 | { |
| 82 | struct i2c_client *client = to_i2c_client(dev); |
| 83 | struct als_data *data = i2c_get_clientdata(client); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 84 | int ret_val; |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 85 | int temp; |
| 86 | |
| 87 | /* Protect against parallel reads */ |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 88 | pm_runtime_get_sync(dev); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 89 | mutex_lock(&data->mutex); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 90 | |
| 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); |
| 96 | |
| 97 | ret_val = als_wait_for_data_ready(dev); |
| 98 | if (ret_val < 0) |
| 99 | goto failed; |
| 100 | |
| 101 | temp = i2c_smbus_read_byte_data(client, 0x8C); /* LSB data */ |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 102 | if (temp < 0) { |
| 103 | ret_val = temp; |
| 104 | goto failed; |
| 105 | } |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 106 | ret_val = i2c_smbus_read_byte_data(client, 0x8D); /* MSB data */ |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 107 | if (ret_val < 0) |
| 108 | goto failed; |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 109 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 110 | mutex_unlock(&data->mutex); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 111 | pm_runtime_put_sync(dev); |
| 112 | |
| 113 | temp = (ret_val << 8) | temp; |
| 114 | return sprintf(buf, "%d\n", temp); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 115 | failed: |
| 116 | mutex_unlock(&data->mutex); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 117 | pm_runtime_put_sync(dev); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 118 | return ret_val; |
| 119 | } |
| 120 | |
| 121 | static ssize_t als_sensing_range_store(struct device *dev, |
| 122 | struct device_attribute *attr, const char *buf, size_t count) |
| 123 | { |
| 124 | struct i2c_client *client = to_i2c_client(dev); |
| 125 | struct als_data *data = i2c_get_clientdata(client); |
Vasiliy Kulikov | 1093736 | 2010-11-11 14:05:11 -0800 | [diff] [blame] | 126 | int ret_val; |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 127 | unsigned long val; |
| 128 | |
Jingoo Han | f7b4127 | 2013-06-04 13:15:16 +0900 | [diff] [blame] | 129 | ret_val = kstrtoul(buf, 10, &val); |
| 130 | if (ret_val) |
| 131 | return ret_val; |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 132 | |
| 133 | if (val < 4096) |
| 134 | val = 1; |
| 135 | else if (val < 65536) |
| 136 | val = 2; |
| 137 | else |
| 138 | return -ERANGE; |
| 139 | |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 140 | pm_runtime_get_sync(dev); |
| 141 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 142 | /* Make sure nobody else reads/modifies/writes 0x81 while we |
| 143 | are active */ |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 144 | mutex_lock(&data->mutex); |
| 145 | |
| 146 | ret_val = i2c_smbus_read_byte_data(client, 0x81); |
| 147 | if (ret_val < 0) |
| 148 | goto fail; |
| 149 | |
| 150 | /* Reset the bits before setting them */ |
| 151 | ret_val = ret_val & 0xFA; |
| 152 | |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 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); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 157 | |
| 158 | ret_val = i2c_smbus_write_byte_data(client, 0x81, ret_val); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 159 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 160 | if (ret_val >= 0) { |
| 161 | /* All OK */ |
| 162 | mutex_unlock(&data->mutex); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 163 | pm_runtime_put_sync(dev); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 164 | return count; |
| 165 | } |
| 166 | fail: |
| 167 | mutex_unlock(&data->mutex); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 168 | pm_runtime_put_sync(dev); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 169 | return ret_val; |
| 170 | } |
| 171 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 172 | static int als_set_power_state(struct i2c_client *client, bool on_off) |
| 173 | { |
| 174 | int ret_val; |
| 175 | struct als_data *data = i2c_get_clientdata(client); |
| 176 | |
| 177 | mutex_lock(&data->mutex); |
| 178 | ret_val = i2c_smbus_read_byte_data(client, 0x80); |
| 179 | if (ret_val < 0) |
| 180 | goto fail; |
| 181 | if (on_off) |
| 182 | ret_val = ret_val | 0x01; |
| 183 | else |
| 184 | ret_val = ret_val & 0xFE; |
| 185 | ret_val = i2c_smbus_write_byte_data(client, 0x80, ret_val); |
| 186 | fail: |
| 187 | mutex_unlock(&data->mutex); |
| 188 | return ret_val; |
| 189 | } |
| 190 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 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); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 194 | |
| 195 | static struct attribute *mid_att_als[] = { |
| 196 | &dev_attr_lux0_sensor_range.attr, |
| 197 | &dev_attr_lux0_input.attr, |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 198 | NULL |
| 199 | }; |
| 200 | |
| 201 | static struct attribute_group m_als_gr = { |
| 202 | .name = "apds9802als", |
| 203 | .attrs = mid_att_als |
| 204 | }; |
| 205 | |
| 206 | static int als_set_default_config(struct i2c_client *client) |
| 207 | { |
| 208 | int ret_val; |
| 209 | /* Write the command and then switch on */ |
| 210 | ret_val = i2c_smbus_write_byte_data(client, 0x80, 0x01); |
| 211 | if (ret_val < 0) { |
| 212 | dev_err(&client->dev, "failed default switch on write\n"); |
| 213 | return ret_val; |
| 214 | } |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 215 | /* detection range: 1~64K Lux, maunal measurement */ |
| 216 | ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x08); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 217 | if (ret_val < 0) |
| 218 | dev_err(&client->dev, "failed default LUX on write\n"); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 219 | |
| 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. |
| 222 | */ |
| 223 | als_wait_for_data_ready(&client->dev); |
| 224 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 225 | return ret_val; |
| 226 | } |
| 227 | |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 228 | static int apds9802als_probe(struct i2c_client *client, |
| 229 | const struct i2c_device_id *id) |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 230 | { |
| 231 | int res; |
| 232 | struct als_data *data; |
| 233 | |
| 234 | data = kzalloc(sizeof(struct als_data), GFP_KERNEL); |
| 235 | if (data == NULL) { |
| 236 | dev_err(&client->dev, "Memory allocation failed\n"); |
| 237 | return -ENOMEM; |
| 238 | } |
| 239 | i2c_set_clientdata(client, data); |
| 240 | res = sysfs_create_group(&client->dev.kobj, &m_als_gr); |
| 241 | if (res) { |
| 242 | dev_err(&client->dev, "device create file failed\n"); |
| 243 | goto als_error1; |
| 244 | } |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 245 | dev_info(&client->dev, "ALS chip found\n"); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 246 | als_set_default_config(client); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 247 | mutex_init(&data->mutex); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 248 | |
Hong Liu | 4e67359 | 2011-03-22 16:33:59 -0700 | [diff] [blame] | 249 | pm_runtime_set_active(&client->dev); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 250 | pm_runtime_enable(&client->dev); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 251 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 252 | return res; |
| 253 | als_error1: |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 254 | kfree(data); |
| 255 | return res; |
| 256 | } |
| 257 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 258 | static int apds9802als_remove(struct i2c_client *client) |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 259 | { |
| 260 | struct als_data *data = i2c_get_clientdata(client); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 261 | |
Hong Liu | 4e67359 | 2011-03-22 16:33:59 -0700 | [diff] [blame] | 262 | pm_runtime_get_sync(&client->dev); |
| 263 | |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 264 | als_set_power_state(client, false); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 265 | sysfs_remove_group(&client->dev.kobj, &m_als_gr); |
Hong Liu | 4e67359 | 2011-03-22 16:33:59 -0700 | [diff] [blame] | 266 | |
| 267 | pm_runtime_disable(&client->dev); |
| 268 | pm_runtime_set_suspended(&client->dev); |
| 269 | pm_runtime_put_noidle(&client->dev); |
| 270 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 271 | kfree(data); |
| 272 | return 0; |
| 273 | } |
| 274 | |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 275 | #ifdef CONFIG_PM |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 276 | |
Lars-Peter Clausen | 1c9354b | 2013-04-11 11:24:39 +0200 | [diff] [blame] | 277 | static int apds9802als_suspend(struct device *dev) |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 278 | { |
| 279 | struct i2c_client *client = to_i2c_client(dev); |
| 280 | |
| 281 | als_set_power_state(client, false); |
| 282 | return 0; |
| 283 | } |
| 284 | |
Lars-Peter Clausen | 1c9354b | 2013-04-11 11:24:39 +0200 | [diff] [blame] | 285 | static int apds9802als_resume(struct device *dev) |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 286 | { |
| 287 | struct i2c_client *client = to_i2c_client(dev); |
| 288 | |
| 289 | als_set_power_state(client, true); |
| 290 | return 0; |
| 291 | } |
| 292 | |
Lars-Peter Clausen | 1c9354b | 2013-04-11 11:24:39 +0200 | [diff] [blame] | 293 | static UNIVERSAL_DEV_PM_OPS(apds9802als_pm_ops, apds9802als_suspend, |
| 294 | apds9802als_resume, NULL); |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 295 | |
| 296 | #define APDS9802ALS_PM_OPS (&apds9802als_pm_ops) |
| 297 | |
| 298 | #else /* CONFIG_PM */ |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 299 | #define APDS9802ALS_PM_OPS NULL |
| 300 | #endif /* CONFIG_PM */ |
| 301 | |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 302 | static struct i2c_device_id apds9802als_id[] = { |
| 303 | { DRIVER_NAME, 0 }, |
| 304 | { } |
| 305 | }; |
| 306 | |
| 307 | MODULE_DEVICE_TABLE(i2c, apds9802als_id); |
| 308 | |
| 309 | static struct i2c_driver apds9802als_driver = { |
| 310 | .driver = { |
Hong Liu | f0cfec1 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 311 | .name = DRIVER_NAME, |
| 312 | .pm = APDS9802ALS_PM_OPS, |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 313 | }, |
| 314 | .probe = apds9802als_probe, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 315 | .remove = apds9802als_remove, |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 316 | .id_table = apds9802als_id, |
| 317 | }; |
| 318 | |
Axel Lin | a64fe2e | 2012-01-22 15:36:45 +0800 | [diff] [blame] | 319 | module_i2c_driver(apds9802als_driver); |
anantha | 22d96aa | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 320 | |
| 321 | MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com"); |
| 322 | MODULE_DESCRIPTION("Avago apds9802als ALS Driver"); |
| 323 | MODULE_LICENSE("GPL v2"); |