Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / light / isl29018.c
blob426b6af708073c03d85d63cf81fcb326f4f00ec9
1 /*
2 * A iio driver for the light sensor ISL 29018.
4 * IIO driver for monitoring ambient light intensity in luxi, proximity
5 * sensing and infrared sensing.
7 * Copyright (c) 2010, NVIDIA Corporation.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <linux/module.h>
25 #include <linux/i2c.h>
26 #include <linux/err.h>
27 #include <linux/mutex.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include "../iio.h"
32 #define CONVERSION_TIME_MS 100
34 #define ISL29018_REG_ADD_COMMAND1 0x00
35 #define COMMMAND1_OPMODE_SHIFT 5
36 #define COMMMAND1_OPMODE_MASK (7 << COMMMAND1_OPMODE_SHIFT)
37 #define COMMMAND1_OPMODE_POWER_DOWN 0
38 #define COMMMAND1_OPMODE_ALS_ONCE 1
39 #define COMMMAND1_OPMODE_IR_ONCE 2
40 #define COMMMAND1_OPMODE_PROX_ONCE 3
42 #define ISL29018_REG_ADD_COMMANDII 0x01
43 #define COMMANDII_RESOLUTION_SHIFT 2
44 #define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT)
46 #define COMMANDII_RANGE_SHIFT 0
47 #define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT)
49 #define COMMANDII_SCHEME_SHIFT 7
50 #define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT)
52 #define ISL29018_REG_ADD_DATA_LSB 0x02
53 #define ISL29018_REG_ADD_DATA_MSB 0x03
54 #define ISL29018_MAX_REGS ISL29018_REG_ADD_DATA_MSB
56 struct isl29018_chip {
57 struct i2c_client *client;
58 struct mutex lock;
59 unsigned int lux_scale;
60 unsigned int range;
61 unsigned int adc_bit;
62 int prox_scheme;
63 u8 reg_cache[ISL29018_MAX_REGS];
66 static int isl29018_write_data(struct i2c_client *client, u8 reg,
67 u8 val, u8 mask, u8 shift)
69 u8 regval;
70 int ret = 0;
71 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
73 regval = chip->reg_cache[reg];
74 regval &= ~mask;
75 regval |= val << shift;
77 ret = i2c_smbus_write_byte_data(client, reg, regval);
78 if (ret) {
79 dev_err(&client->dev, "Write to device fails status %x\n", ret);
80 return ret;
82 chip->reg_cache[reg] = regval;
84 return 0;
87 static int isl29018_set_range(struct i2c_client *client, unsigned long range,
88 unsigned int *new_range)
90 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
91 int i;
93 for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
94 if (range <= supp_ranges[i]) {
95 *new_range = (unsigned int)supp_ranges[i];
96 break;
100 if (i >= ARRAY_SIZE(supp_ranges))
101 return -EINVAL;
103 return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
104 i, COMMANDII_RANGE_MASK, COMMANDII_RANGE_SHIFT);
107 static int isl29018_set_resolution(struct i2c_client *client,
108 unsigned long adcbit, unsigned int *conf_adc_bit)
110 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
111 int i;
113 for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
114 if (adcbit >= supp_adcbit[i]) {
115 *conf_adc_bit = (unsigned int)supp_adcbit[i];
116 break;
120 if (i >= ARRAY_SIZE(supp_adcbit))
121 return -EINVAL;
123 return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
124 i, COMMANDII_RESOLUTION_MASK,
125 COMMANDII_RESOLUTION_SHIFT);
128 static int isl29018_read_sensor_input(struct i2c_client *client, int mode)
130 int status;
131 int lsb;
132 int msb;
134 /* Set mode */
135 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMAND1,
136 mode, COMMMAND1_OPMODE_MASK, COMMMAND1_OPMODE_SHIFT);
137 if (status) {
138 dev_err(&client->dev, "Error in setting operating mode\n");
139 return status;
141 msleep(CONVERSION_TIME_MS);
142 lsb = i2c_smbus_read_byte_data(client, ISL29018_REG_ADD_DATA_LSB);
143 if (lsb < 0) {
144 dev_err(&client->dev, "Error in reading LSB DATA\n");
145 return lsb;
148 msb = i2c_smbus_read_byte_data(client, ISL29018_REG_ADD_DATA_MSB);
149 if (msb < 0) {
150 dev_err(&client->dev, "Error in reading MSB DATA\n");
151 return msb;
153 dev_vdbg(&client->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
155 return (msb << 8) | lsb;
158 static int isl29018_read_lux(struct i2c_client *client, int *lux)
160 int lux_data;
161 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
163 lux_data = isl29018_read_sensor_input(client,
164 COMMMAND1_OPMODE_ALS_ONCE);
166 if (lux_data < 0)
167 return lux_data;
169 *lux = (lux_data * chip->range * chip->lux_scale) >> chip->adc_bit;
171 return 0;
174 static int isl29018_read_ir(struct i2c_client *client, int *ir)
176 int ir_data;
178 ir_data = isl29018_read_sensor_input(client, COMMMAND1_OPMODE_IR_ONCE);
180 if (ir_data < 0)
181 return ir_data;
183 *ir = ir_data;
185 return 0;
188 static int isl29018_read_proximity_ir(struct i2c_client *client, int scheme,
189 int *near_ir)
191 int status;
192 int prox_data = -1;
193 int ir_data = -1;
195 /* Do proximity sensing with required scheme */
196 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
197 scheme, COMMANDII_SCHEME_MASK, COMMANDII_SCHEME_SHIFT);
198 if (status) {
199 dev_err(&client->dev, "Error in setting operating mode\n");
200 return status;
203 prox_data = isl29018_read_sensor_input(client,
204 COMMMAND1_OPMODE_PROX_ONCE);
205 if (prox_data < 0)
206 return prox_data;
208 if (scheme == 1) {
209 *near_ir = prox_data;
210 return 0;
213 ir_data = isl29018_read_sensor_input(client,
214 COMMMAND1_OPMODE_IR_ONCE);
216 if (ir_data < 0)
217 return ir_data;
219 if (prox_data >= ir_data)
220 *near_ir = prox_data - ir_data;
221 else
222 *near_ir = 0;
224 return 0;
227 /* Sysfs interface */
228 /* range */
229 static ssize_t show_range(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct iio_dev *indio_dev = dev_get_drvdata(dev);
233 struct isl29018_chip *chip = iio_priv(indio_dev);
235 return sprintf(buf, "%u\n", chip->range);
238 static ssize_t store_range(struct device *dev,
239 struct device_attribute *attr, const char *buf, size_t count)
241 struct iio_dev *indio_dev = dev_get_drvdata(dev);
242 struct isl29018_chip *chip = iio_priv(indio_dev);
243 struct i2c_client *client = chip->client;
244 int status;
245 unsigned long lval;
246 unsigned int new_range;
248 if (strict_strtoul(buf, 10, &lval))
249 return -EINVAL;
251 if (!(lval == 1000UL || lval == 4000UL ||
252 lval == 16000UL || lval == 64000UL)) {
253 dev_err(dev, "The range is not supported\n");
254 return -EINVAL;
257 mutex_lock(&chip->lock);
258 status = isl29018_set_range(client, lval, &new_range);
259 if (status < 0) {
260 mutex_unlock(&chip->lock);
261 dev_err(dev, "Error in setting max range\n");
262 return status;
264 chip->range = new_range;
265 mutex_unlock(&chip->lock);
267 return count;
270 /* resolution */
271 static ssize_t show_resolution(struct device *dev,
272 struct device_attribute *attr, char *buf)
274 struct iio_dev *indio_dev = dev_get_drvdata(dev);
275 struct isl29018_chip *chip = iio_priv(indio_dev);
277 return sprintf(buf, "%u\n", chip->adc_bit);
280 static ssize_t store_resolution(struct device *dev,
281 struct device_attribute *attr, const char *buf, size_t count)
283 struct iio_dev *indio_dev = dev_get_drvdata(dev);
284 struct isl29018_chip *chip = iio_priv(indio_dev);
285 struct i2c_client *client = chip->client;
286 int status;
287 unsigned long lval;
288 unsigned int new_adc_bit;
290 if (strict_strtoul(buf, 10, &lval))
291 return -EINVAL;
292 if (!(lval == 4 || lval == 8 || lval == 12 || lval == 16)) {
293 dev_err(dev, "The resolution is not supported\n");
294 return -EINVAL;
297 mutex_lock(&chip->lock);
298 status = isl29018_set_resolution(client, lval, &new_adc_bit);
299 if (status < 0) {
300 mutex_unlock(&chip->lock);
301 dev_err(dev, "Error in setting resolution\n");
302 return status;
304 chip->adc_bit = new_adc_bit;
305 mutex_unlock(&chip->lock);
307 return count;
310 /* proximity scheme */
311 static ssize_t show_prox_infrared_supression(struct device *dev,
312 struct device_attribute *attr, char *buf)
314 struct iio_dev *indio_dev = dev_get_drvdata(dev);
315 struct isl29018_chip *chip = iio_priv(indio_dev);
317 /* return the "proximity scheme" i.e. if the chip does on chip
318 infrared supression (1 means perform on chip supression) */
319 return sprintf(buf, "%d\n", chip->prox_scheme);
322 static ssize_t store_prox_infrared_supression(struct device *dev,
323 struct device_attribute *attr, const char *buf, size_t count)
325 struct iio_dev *indio_dev = dev_get_drvdata(dev);
326 struct isl29018_chip *chip = iio_priv(indio_dev);
327 unsigned long lval;
329 if (strict_strtoul(buf, 10, &lval))
330 return -EINVAL;
331 if (!(lval == 0UL || lval == 1UL)) {
332 dev_err(dev, "The mode is not supported\n");
333 return -EINVAL;
336 /* get the "proximity scheme" i.e. if the chip does on chip
337 infrared supression (1 means perform on chip supression) */
338 mutex_lock(&chip->lock);
339 chip->prox_scheme = (int)lval;
340 mutex_unlock(&chip->lock);
342 return count;
345 /* Channel IO */
346 static int isl29018_write_raw(struct iio_dev *indio_dev,
347 struct iio_chan_spec const *chan,
348 int val,
349 int val2,
350 long mask)
352 struct isl29018_chip *chip = iio_priv(indio_dev);
353 int ret = -EINVAL;
355 mutex_lock(&chip->lock);
356 if (mask == (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) &&
357 chan->type == IIO_LIGHT) {
358 chip->lux_scale = val;
359 ret = 0;
361 mutex_unlock(&chip->lock);
363 return 0;
366 static int isl29018_read_raw(struct iio_dev *indio_dev,
367 struct iio_chan_spec const *chan,
368 int *val,
369 int *val2,
370 long mask)
372 int ret = -EINVAL;
373 struct isl29018_chip *chip = iio_priv(indio_dev);
374 struct i2c_client *client = chip->client;
376 mutex_lock(&chip->lock);
377 switch (mask) {
378 case 0:
379 switch (chan->type) {
380 case IIO_LIGHT:
381 ret = isl29018_read_lux(client, val);
382 break;
383 case IIO_INTENSITY:
384 ret = isl29018_read_ir(client, val);
385 break;
386 case IIO_PROXIMITY:
387 ret = isl29018_read_proximity_ir(client,
388 chip->prox_scheme, val);
389 break;
390 default:
391 break;
393 if (!ret)
394 ret = IIO_VAL_INT;
395 break;
396 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
397 if (chan->type == IIO_LIGHT) {
398 *val = chip->lux_scale;
399 ret = IIO_VAL_INT;
401 break;
402 default:
403 break;
405 mutex_unlock(&chip->lock);
406 return ret;
409 static const struct iio_chan_spec isl29018_channels[] = {
411 .type = IIO_LIGHT,
412 .indexed = 1,
413 .channel = 0,
414 .processed_val = 1,
415 .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE),
416 }, {
417 .type = IIO_INTENSITY,
418 .modified = 1,
419 .channel2 = IIO_MOD_LIGHT_IR,
420 }, {
421 /* Unindexed in current ABI. But perhaps it should be. */
422 .type = IIO_PROXIMITY,
426 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
427 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
428 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
429 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
430 show_resolution, store_resolution, 0);
431 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_supression,
432 S_IRUGO | S_IWUSR,
433 show_prox_infrared_supression,
434 store_prox_infrared_supression, 0);
436 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
437 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
438 static struct attribute *isl29018_attributes[] = {
439 ISL29018_DEV_ATTR(range),
440 ISL29018_CONST_ATTR(range_available),
441 ISL29018_DEV_ATTR(adc_resolution),
442 ISL29018_CONST_ATTR(adc_resolution_available),
443 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_supression),
444 NULL
447 static const struct attribute_group isl29108_group = {
448 .attrs = isl29018_attributes,
451 static int isl29018_chip_init(struct i2c_client *client)
453 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
454 int status;
455 int new_adc_bit;
456 unsigned int new_range;
458 memset(chip->reg_cache, 0, sizeof(chip->reg_cache));
460 /* set defaults */
461 status = isl29018_set_range(client, chip->range, &new_range);
462 if (status < 0) {
463 dev_err(&client->dev, "Init of isl29018 fails\n");
464 return status;
467 status = isl29018_set_resolution(client, chip->adc_bit,
468 &new_adc_bit);
470 return 0;
473 static const struct iio_info isl29108_info = {
474 .attrs = &isl29108_group,
475 .driver_module = THIS_MODULE,
476 .read_raw = &isl29018_read_raw,
477 .write_raw = &isl29018_write_raw,
480 static int __devinit isl29018_probe(struct i2c_client *client,
481 const struct i2c_device_id *id)
483 struct isl29018_chip *chip;
484 struct iio_dev *indio_dev;
485 int err;
487 indio_dev = iio_allocate_device(sizeof(*chip));
488 if (indio_dev == NULL) {
489 dev_err(&client->dev, "iio allocation fails\n");
490 err = -ENOMEM;
491 goto exit;
493 chip = iio_priv(indio_dev);
495 i2c_set_clientdata(client, indio_dev);
496 chip->client = client;
498 mutex_init(&chip->lock);
500 chip->lux_scale = 1;
501 chip->range = 1000;
502 chip->adc_bit = 16;
504 err = isl29018_chip_init(client);
505 if (err)
506 goto exit_iio_free;
508 indio_dev->info = &isl29108_info;
509 indio_dev->channels = isl29018_channels;
510 indio_dev->num_channels = ARRAY_SIZE(isl29018_channels);
511 indio_dev->name = id->name;
512 indio_dev->dev.parent = &client->dev;
513 indio_dev->modes = INDIO_DIRECT_MODE;
514 err = iio_device_register(indio_dev);
515 if (err) {
516 dev_err(&client->dev, "iio registration fails\n");
517 goto exit_iio_free;
520 return 0;
521 exit_iio_free:
522 iio_free_device(indio_dev);
523 exit:
524 return err;
527 static int __devexit isl29018_remove(struct i2c_client *client)
529 struct iio_dev *indio_dev = i2c_get_clientdata(client);
531 dev_dbg(&client->dev, "%s()\n", __func__);
532 iio_device_unregister(indio_dev);
534 return 0;
537 static const struct i2c_device_id isl29018_id[] = {
538 {"isl29018", 0},
542 MODULE_DEVICE_TABLE(i2c, isl29018_id);
544 static struct i2c_driver isl29018_driver = {
545 .class = I2C_CLASS_HWMON,
546 .driver = {
547 .name = "isl29018",
548 .owner = THIS_MODULE,
550 .probe = isl29018_probe,
551 .remove = __devexit_p(isl29018_remove),
552 .id_table = isl29018_id,
555 static int __init isl29018_init(void)
557 return i2c_add_driver(&isl29018_driver);
560 static void __exit isl29018_exit(void)
562 i2c_del_driver(&isl29018_driver);
565 module_init(isl29018_init);
566 module_exit(isl29018_exit);
568 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
569 MODULE_LICENSE("GPL");