Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[linux-2.6.git] / drivers / misc / isl29003.c
blobe3183f26216b876cee627c6a980bd1ed6ee35ec5
1 /*
2 * isl29003.c - Linux kernel module for
3 * Intersil ISL29003 ambient light sensor
5 * See file:Documentation/misc-devices/isl29003
7 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
9 * Based on code written by
10 * Rodolfo Giometti <giometti@linux.it>
11 * Eurotech S.p.A. <info@eurotech.it>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/i2c.h>
32 #include <linux/mutex.h>
33 #include <linux/delay.h>
35 #define ISL29003_DRV_NAME "isl29003"
36 #define DRIVER_VERSION "1.0"
38 #define ISL29003_REG_COMMAND 0x00
39 #define ISL29003_ADC_ENABLED (1 << 7)
40 #define ISL29003_ADC_PD (1 << 6)
41 #define ISL29003_TIMING_INT (1 << 5)
42 #define ISL29003_MODE_SHIFT (2)
43 #define ISL29003_MODE_MASK (0x3 << ISL29003_MODE_SHIFT)
44 #define ISL29003_RES_SHIFT (0)
45 #define ISL29003_RES_MASK (0x3 << ISL29003_RES_SHIFT)
47 #define ISL29003_REG_CONTROL 0x01
48 #define ISL29003_INT_FLG (1 << 5)
49 #define ISL29003_RANGE_SHIFT (2)
50 #define ISL29003_RANGE_MASK (0x3 << ISL29003_RANGE_SHIFT)
51 #define ISL29003_INT_PERSISTS_SHIFT (0)
52 #define ISL29003_INT_PERSISTS_MASK (0xf << ISL29003_INT_PERSISTS_SHIFT)
54 #define ISL29003_REG_IRQ_THRESH_HI 0x02
55 #define ISL29003_REG_IRQ_THRESH_LO 0x03
56 #define ISL29003_REG_LSB_SENSOR 0x04
57 #define ISL29003_REG_MSB_SENSOR 0x05
58 #define ISL29003_REG_LSB_TIMER 0x06
59 #define ISL29003_REG_MSB_TIMER 0x07
61 #define ISL29003_NUM_CACHABLE_REGS 4
63 struct isl29003_data {
64 struct i2c_client *client;
65 struct mutex lock;
66 u8 reg_cache[ISL29003_NUM_CACHABLE_REGS];
67 u8 power_state_before_suspend;
70 static int gain_range[] = {
71 1000, 4000, 16000, 64000
75 * register access helpers
78 static int __isl29003_read_reg(struct i2c_client *client,
79 u32 reg, u8 mask, u8 shift)
81 struct isl29003_data *data = i2c_get_clientdata(client);
82 return (data->reg_cache[reg] & mask) >> shift;
85 static int __isl29003_write_reg(struct i2c_client *client,
86 u32 reg, u8 mask, u8 shift, u8 val)
88 struct isl29003_data *data = i2c_get_clientdata(client);
89 int ret = 0;
90 u8 tmp;
92 if (reg >= ISL29003_NUM_CACHABLE_REGS)
93 return -EINVAL;
95 mutex_lock(&data->lock);
97 tmp = data->reg_cache[reg];
98 tmp &= ~mask;
99 tmp |= val << shift;
101 ret = i2c_smbus_write_byte_data(client, reg, tmp);
102 if (!ret)
103 data->reg_cache[reg] = tmp;
105 mutex_unlock(&data->lock);
106 return ret;
110 * internally used functions
113 /* range */
114 static int isl29003_get_range(struct i2c_client *client)
116 return __isl29003_read_reg(client, ISL29003_REG_CONTROL,
117 ISL29003_RANGE_MASK, ISL29003_RANGE_SHIFT);
120 static int isl29003_set_range(struct i2c_client *client, int range)
122 return __isl29003_write_reg(client, ISL29003_REG_CONTROL,
123 ISL29003_RANGE_MASK, ISL29003_RANGE_SHIFT, range);
126 /* resolution */
127 static int isl29003_get_resolution(struct i2c_client *client)
129 return __isl29003_read_reg(client, ISL29003_REG_COMMAND,
130 ISL29003_RES_MASK, ISL29003_RES_SHIFT);
133 static int isl29003_set_resolution(struct i2c_client *client, int res)
135 return __isl29003_write_reg(client, ISL29003_REG_COMMAND,
136 ISL29003_RES_MASK, ISL29003_RES_SHIFT, res);
139 /* mode */
140 static int isl29003_get_mode(struct i2c_client *client)
142 return __isl29003_read_reg(client, ISL29003_REG_COMMAND,
143 ISL29003_RES_MASK, ISL29003_RES_SHIFT);
146 static int isl29003_set_mode(struct i2c_client *client, int mode)
148 return __isl29003_write_reg(client, ISL29003_REG_COMMAND,
149 ISL29003_RES_MASK, ISL29003_RES_SHIFT, mode);
152 /* power_state */
153 static int isl29003_set_power_state(struct i2c_client *client, int state)
155 return __isl29003_write_reg(client, ISL29003_REG_COMMAND,
156 ISL29003_ADC_ENABLED | ISL29003_ADC_PD, 0,
157 state ? ISL29003_ADC_ENABLED : ISL29003_ADC_PD);
160 static int isl29003_get_power_state(struct i2c_client *client)
162 struct isl29003_data *data = i2c_get_clientdata(client);
163 u8 cmdreg = data->reg_cache[ISL29003_REG_COMMAND];
164 return ~cmdreg & ISL29003_ADC_PD;
167 static int isl29003_get_adc_value(struct i2c_client *client)
169 struct isl29003_data *data = i2c_get_clientdata(client);
170 int lsb, msb, range, bitdepth;
172 mutex_lock(&data->lock);
173 lsb = i2c_smbus_read_byte_data(client, ISL29003_REG_LSB_SENSOR);
175 if (lsb < 0) {
176 mutex_unlock(&data->lock);
177 return lsb;
180 msb = i2c_smbus_read_byte_data(client, ISL29003_REG_MSB_SENSOR);
181 mutex_unlock(&data->lock);
183 if (msb < 0)
184 return msb;
186 range = isl29003_get_range(client);
187 bitdepth = (4 - isl29003_get_resolution(client)) * 4;
188 return (((msb << 8) | lsb) * gain_range[range]) >> bitdepth;
192 * sysfs layer
195 /* range */
196 static ssize_t isl29003_show_range(struct device *dev,
197 struct device_attribute *attr, char *buf)
199 struct i2c_client *client = to_i2c_client(dev);
200 return sprintf(buf, "%i\n", isl29003_get_range(client));
203 static ssize_t isl29003_store_range(struct device *dev,
204 struct device_attribute *attr,
205 const char *buf, size_t count)
207 struct i2c_client *client = to_i2c_client(dev);
208 unsigned long val;
209 int ret;
211 ret = kstrtoul(buf, 10, &val);
212 if (ret)
213 return ret;
215 if (val > 3)
216 return -EINVAL;
218 ret = isl29003_set_range(client, val);
219 if (ret < 0)
220 return ret;
222 return count;
225 static DEVICE_ATTR(range, S_IWUSR | S_IRUGO,
226 isl29003_show_range, isl29003_store_range);
229 /* resolution */
230 static ssize_t isl29003_show_resolution(struct device *dev,
231 struct device_attribute *attr,
232 char *buf)
234 struct i2c_client *client = to_i2c_client(dev);
235 return sprintf(buf, "%d\n", isl29003_get_resolution(client));
238 static ssize_t isl29003_store_resolution(struct device *dev,
239 struct device_attribute *attr,
240 const char *buf, size_t count)
242 struct i2c_client *client = to_i2c_client(dev);
243 unsigned long val;
244 int ret;
246 ret = kstrtoul(buf, 10, &val);
247 if (ret)
248 return ret;
250 if (val > 3)
251 return -EINVAL;
253 ret = isl29003_set_resolution(client, val);
254 if (ret < 0)
255 return ret;
257 return count;
260 static DEVICE_ATTR(resolution, S_IWUSR | S_IRUGO,
261 isl29003_show_resolution, isl29003_store_resolution);
263 /* mode */
264 static ssize_t isl29003_show_mode(struct device *dev,
265 struct device_attribute *attr, char *buf)
267 struct i2c_client *client = to_i2c_client(dev);
268 return sprintf(buf, "%d\n", isl29003_get_mode(client));
271 static ssize_t isl29003_store_mode(struct device *dev,
272 struct device_attribute *attr, const char *buf, size_t count)
274 struct i2c_client *client = to_i2c_client(dev);
275 unsigned long val;
276 int ret;
278 ret = kstrtoul(buf, 10, &val);
279 if (ret)
280 return ret;
282 if (val > 2)
283 return -EINVAL;
285 ret = isl29003_set_mode(client, val);
286 if (ret < 0)
287 return ret;
289 return count;
292 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
293 isl29003_show_mode, isl29003_store_mode);
296 /* power state */
297 static ssize_t isl29003_show_power_state(struct device *dev,
298 struct device_attribute *attr,
299 char *buf)
301 struct i2c_client *client = to_i2c_client(dev);
302 return sprintf(buf, "%d\n", isl29003_get_power_state(client));
305 static ssize_t isl29003_store_power_state(struct device *dev,
306 struct device_attribute *attr,
307 const char *buf, size_t count)
309 struct i2c_client *client = to_i2c_client(dev);
310 unsigned long val;
311 int ret;
313 ret = kstrtoul(buf, 10, &val);
314 if (ret)
315 return ret;
317 if (val > 1)
318 return -EINVAL;
320 ret = isl29003_set_power_state(client, val);
321 return ret ? ret : count;
324 static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
325 isl29003_show_power_state, isl29003_store_power_state);
328 /* lux */
329 static ssize_t isl29003_show_lux(struct device *dev,
330 struct device_attribute *attr, char *buf)
332 struct i2c_client *client = to_i2c_client(dev);
334 /* No LUX data if not operational */
335 if (!isl29003_get_power_state(client))
336 return -EBUSY;
338 return sprintf(buf, "%d\n", isl29003_get_adc_value(client));
341 static DEVICE_ATTR(lux, S_IRUGO, isl29003_show_lux, NULL);
343 static struct attribute *isl29003_attributes[] = {
344 &dev_attr_range.attr,
345 &dev_attr_resolution.attr,
346 &dev_attr_mode.attr,
347 &dev_attr_power_state.attr,
348 &dev_attr_lux.attr,
349 NULL
352 static const struct attribute_group isl29003_attr_group = {
353 .attrs = isl29003_attributes,
356 static int isl29003_init_client(struct i2c_client *client)
358 struct isl29003_data *data = i2c_get_clientdata(client);
359 int i;
361 /* read all the registers once to fill the cache.
362 * if one of the reads fails, we consider the init failed */
363 for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++) {
364 int v = i2c_smbus_read_byte_data(client, i);
365 if (v < 0)
366 return -ENODEV;
368 data->reg_cache[i] = v;
371 /* set defaults */
372 isl29003_set_range(client, 0);
373 isl29003_set_resolution(client, 0);
374 isl29003_set_mode(client, 0);
375 isl29003_set_power_state(client, 0);
377 return 0;
381 * I2C layer
384 static int isl29003_probe(struct i2c_client *client,
385 const struct i2c_device_id *id)
387 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
388 struct isl29003_data *data;
389 int err = 0;
391 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
392 return -EIO;
394 data = kzalloc(sizeof(struct isl29003_data), GFP_KERNEL);
395 if (!data)
396 return -ENOMEM;
398 data->client = client;
399 i2c_set_clientdata(client, data);
400 mutex_init(&data->lock);
402 /* initialize the ISL29003 chip */
403 err = isl29003_init_client(client);
404 if (err)
405 goto exit_kfree;
407 /* register sysfs hooks */
408 err = sysfs_create_group(&client->dev.kobj, &isl29003_attr_group);
409 if (err)
410 goto exit_kfree;
412 dev_info(&client->dev, "driver version %s enabled\n", DRIVER_VERSION);
413 return 0;
415 exit_kfree:
416 kfree(data);
417 return err;
420 static int isl29003_remove(struct i2c_client *client)
422 sysfs_remove_group(&client->dev.kobj, &isl29003_attr_group);
423 isl29003_set_power_state(client, 0);
424 kfree(i2c_get_clientdata(client));
425 return 0;
428 #ifdef CONFIG_PM_SLEEP
429 static int isl29003_suspend(struct device *dev)
431 struct i2c_client *client = to_i2c_client(dev);
432 struct isl29003_data *data = i2c_get_clientdata(client);
434 data->power_state_before_suspend = isl29003_get_power_state(client);
435 return isl29003_set_power_state(client, 0);
438 static int isl29003_resume(struct device *dev)
440 int i;
441 struct i2c_client *client = to_i2c_client(dev);
442 struct isl29003_data *data = i2c_get_clientdata(client);
444 /* restore registers from cache */
445 for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++)
446 if (i2c_smbus_write_byte_data(client, i, data->reg_cache[i]))
447 return -EIO;
449 return isl29003_set_power_state(client,
450 data->power_state_before_suspend);
453 static SIMPLE_DEV_PM_OPS(isl29003_pm_ops, isl29003_suspend, isl29003_resume);
454 #define ISL29003_PM_OPS (&isl29003_pm_ops)
456 #else
457 #define ISL29003_PM_OPS NULL
458 #endif /* CONFIG_PM_SLEEP */
460 static const struct i2c_device_id isl29003_id[] = {
461 { "isl29003", 0 },
464 MODULE_DEVICE_TABLE(i2c, isl29003_id);
466 static struct i2c_driver isl29003_driver = {
467 .driver = {
468 .name = ISL29003_DRV_NAME,
469 .owner = THIS_MODULE,
470 .pm = ISL29003_PM_OPS,
472 .probe = isl29003_probe,
473 .remove = isl29003_remove,
474 .id_table = isl29003_id,
477 module_i2c_driver(isl29003_driver);
479 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
480 MODULE_DESCRIPTION("ISL29003 ambient light sensor driver");
481 MODULE_LICENSE("GPL v2");
482 MODULE_VERSION(DRIVER_VERSION);