Merge tag 'sound-3.11' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
[linux-2.6.git] / drivers / staging / iio / light / isl29018.c
blob82478a59e42e719588adc20533824a7f31e47271
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/regmap.h>
30 #include <linux/slab.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/sysfs.h>
34 #define CONVERSION_TIME_MS 100
36 #define ISL29018_REG_ADD_COMMAND1 0x00
37 #define COMMMAND1_OPMODE_SHIFT 5
38 #define COMMMAND1_OPMODE_MASK (7 << COMMMAND1_OPMODE_SHIFT)
39 #define COMMMAND1_OPMODE_POWER_DOWN 0
40 #define COMMMAND1_OPMODE_ALS_ONCE 1
41 #define COMMMAND1_OPMODE_IR_ONCE 2
42 #define COMMMAND1_OPMODE_PROX_ONCE 3
44 #define ISL29018_REG_ADD_COMMANDII 0x01
45 #define COMMANDII_RESOLUTION_SHIFT 2
46 #define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT)
48 #define COMMANDII_RANGE_SHIFT 0
49 #define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT)
51 #define COMMANDII_SCHEME_SHIFT 7
52 #define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT)
54 #define ISL29018_REG_ADD_DATA_LSB 0x02
55 #define ISL29018_REG_ADD_DATA_MSB 0x03
57 #define ISL29018_REG_TEST 0x08
58 #define ISL29018_TEST_SHIFT 0
59 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
61 struct isl29018_chip {
62 struct device *dev;
63 struct regmap *regmap;
64 struct mutex lock;
65 unsigned int lux_scale;
66 unsigned int lux_uscale;
67 unsigned int range;
68 unsigned int adc_bit;
69 int prox_scheme;
70 bool suspended;
73 static int isl29018_set_range(struct isl29018_chip *chip, unsigned long range,
74 unsigned int *new_range)
76 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
77 int i;
79 for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
80 if (range <= supp_ranges[i]) {
81 *new_range = (unsigned int)supp_ranges[i];
82 break;
86 if (i >= ARRAY_SIZE(supp_ranges))
87 return -EINVAL;
89 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
90 COMMANDII_RANGE_MASK, i << COMMANDII_RANGE_SHIFT);
93 static int isl29018_set_resolution(struct isl29018_chip *chip,
94 unsigned long adcbit, unsigned int *conf_adc_bit)
96 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
97 int i;
99 for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
100 if (adcbit >= supp_adcbit[i]) {
101 *conf_adc_bit = (unsigned int)supp_adcbit[i];
102 break;
106 if (i >= ARRAY_SIZE(supp_adcbit))
107 return -EINVAL;
109 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
110 COMMANDII_RESOLUTION_MASK,
111 i << COMMANDII_RESOLUTION_SHIFT);
114 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
116 int status;
117 unsigned int lsb;
118 unsigned int msb;
120 /* Set mode */
121 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
122 mode << COMMMAND1_OPMODE_SHIFT);
123 if (status) {
124 dev_err(chip->dev,
125 "Error in setting operating mode err %d\n", status);
126 return status;
128 msleep(CONVERSION_TIME_MS);
129 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
130 if (status < 0) {
131 dev_err(chip->dev,
132 "Error in reading LSB DATA with err %d\n", status);
133 return status;
136 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
137 if (status < 0) {
138 dev_err(chip->dev,
139 "Error in reading MSB DATA with error %d\n", status);
140 return status;
142 dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
144 return (msb << 8) | lsb;
147 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
149 int lux_data;
150 unsigned int data_x_range, lux_unshifted;
152 lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
154 if (lux_data < 0)
155 return lux_data;
157 /* To support fractional scaling, separate the unshifted lux
158 * into two calculations: int scaling and micro-scaling.
159 * lux_uscale ranges from 0-999999, so about 20 bits. Split
160 * the /1,000,000 in two to reduce the risk of over/underflow.
162 data_x_range = lux_data * chip->range;
163 lux_unshifted = data_x_range * chip->lux_scale;
164 lux_unshifted += data_x_range / 1000 * chip->lux_uscale / 1000;
165 *lux = lux_unshifted >> chip->adc_bit;
167 return 0;
170 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
172 int ir_data;
174 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
176 if (ir_data < 0)
177 return ir_data;
179 *ir = ir_data;
181 return 0;
184 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
185 int *near_ir)
187 int status;
188 int prox_data = -1;
189 int ir_data = -1;
191 /* Do proximity sensing with required scheme */
192 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
193 COMMANDII_SCHEME_MASK,
194 scheme << COMMANDII_SCHEME_SHIFT);
195 if (status) {
196 dev_err(chip->dev, "Error in setting operating mode\n");
197 return status;
200 prox_data = isl29018_read_sensor_input(chip,
201 COMMMAND1_OPMODE_PROX_ONCE);
202 if (prox_data < 0)
203 return prox_data;
205 if (scheme == 1) {
206 *near_ir = prox_data;
207 return 0;
210 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
212 if (ir_data < 0)
213 return ir_data;
215 if (prox_data >= ir_data)
216 *near_ir = prox_data - ir_data;
217 else
218 *near_ir = 0;
220 return 0;
223 /* Sysfs interface */
224 /* range */
225 static ssize_t show_range(struct device *dev,
226 struct device_attribute *attr, char *buf)
228 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
229 struct isl29018_chip *chip = iio_priv(indio_dev);
231 return sprintf(buf, "%u\n", chip->range);
234 static ssize_t store_range(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t count)
237 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
238 struct isl29018_chip *chip = iio_priv(indio_dev);
239 int status;
240 unsigned long lval;
241 unsigned int new_range;
243 if (strict_strtoul(buf, 10, &lval))
244 return -EINVAL;
246 if (!(lval == 1000UL || lval == 4000UL ||
247 lval == 16000UL || lval == 64000UL)) {
248 dev_err(dev, "The range is not supported\n");
249 return -EINVAL;
252 mutex_lock(&chip->lock);
253 status = isl29018_set_range(chip, lval, &new_range);
254 if (status < 0) {
255 mutex_unlock(&chip->lock);
256 dev_err(dev,
257 "Error in setting max range with err %d\n", status);
258 return status;
260 chip->range = new_range;
261 mutex_unlock(&chip->lock);
263 return count;
266 /* resolution */
267 static ssize_t show_resolution(struct device *dev,
268 struct device_attribute *attr, char *buf)
270 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
271 struct isl29018_chip *chip = iio_priv(indio_dev);
273 return sprintf(buf, "%u\n", chip->adc_bit);
276 static ssize_t store_resolution(struct device *dev,
277 struct device_attribute *attr, const char *buf, size_t count)
279 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
280 struct isl29018_chip *chip = iio_priv(indio_dev);
281 int status;
282 unsigned long lval;
283 unsigned int new_adc_bit;
285 if (strict_strtoul(buf, 10, &lval))
286 return -EINVAL;
287 if (!(lval == 4 || lval == 8 || lval == 12 || lval == 16)) {
288 dev_err(dev, "The resolution is not supported\n");
289 return -EINVAL;
292 mutex_lock(&chip->lock);
293 status = isl29018_set_resolution(chip, lval, &new_adc_bit);
294 if (status < 0) {
295 mutex_unlock(&chip->lock);
296 dev_err(dev, "Error in setting resolution\n");
297 return status;
299 chip->adc_bit = new_adc_bit;
300 mutex_unlock(&chip->lock);
302 return count;
305 /* proximity scheme */
306 static ssize_t show_prox_infrared_suppression(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
310 struct isl29018_chip *chip = iio_priv(indio_dev);
312 /* return the "proximity scheme" i.e. if the chip does on chip
313 infrared suppression (1 means perform on chip suppression) */
314 return sprintf(buf, "%d\n", chip->prox_scheme);
317 static ssize_t store_prox_infrared_suppression(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t count)
320 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
321 struct isl29018_chip *chip = iio_priv(indio_dev);
322 unsigned long lval;
324 if (strict_strtoul(buf, 10, &lval))
325 return -EINVAL;
326 if (!(lval == 0UL || lval == 1UL)) {
327 dev_err(dev, "The mode is not supported\n");
328 return -EINVAL;
331 /* get the "proximity scheme" i.e. if the chip does on chip
332 infrared suppression (1 means perform on chip suppression) */
333 mutex_lock(&chip->lock);
334 chip->prox_scheme = (int)lval;
335 mutex_unlock(&chip->lock);
337 return count;
340 /* Channel IO */
341 static int isl29018_write_raw(struct iio_dev *indio_dev,
342 struct iio_chan_spec const *chan,
343 int val,
344 int val2,
345 long mask)
347 struct isl29018_chip *chip = iio_priv(indio_dev);
348 int ret = -EINVAL;
350 mutex_lock(&chip->lock);
351 if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
352 chip->lux_scale = val;
353 /* With no write_raw_get_fmt(), val2 is a MICRO fraction. */
354 chip->lux_uscale = val2;
355 ret = 0;
357 mutex_unlock(&chip->lock);
359 return ret;
362 static int isl29018_read_raw(struct iio_dev *indio_dev,
363 struct iio_chan_spec const *chan,
364 int *val,
365 int *val2,
366 long mask)
368 int ret = -EINVAL;
369 struct isl29018_chip *chip = iio_priv(indio_dev);
371 mutex_lock(&chip->lock);
372 if (chip->suspended) {
373 mutex_unlock(&chip->lock);
374 return -EBUSY;
376 switch (mask) {
377 case IIO_CHAN_INFO_RAW:
378 case IIO_CHAN_INFO_PROCESSED:
379 switch (chan->type) {
380 case IIO_LIGHT:
381 ret = isl29018_read_lux(chip, val);
382 break;
383 case IIO_INTENSITY:
384 ret = isl29018_read_ir(chip, val);
385 break;
386 case IIO_PROXIMITY:
387 ret = isl29018_read_proximity_ir(chip,
388 chip->prox_scheme, val);
389 break;
390 default:
391 break;
393 if (!ret)
394 ret = IIO_VAL_INT;
395 break;
396 case IIO_CHAN_INFO_CALIBSCALE:
397 if (chan->type == IIO_LIGHT) {
398 *val = chip->lux_scale;
399 *val2 = chip->lux_uscale;
400 ret = IIO_VAL_INT_PLUS_MICRO;
402 break;
403 default:
404 break;
406 mutex_unlock(&chip->lock);
407 return ret;
410 static const struct iio_chan_spec isl29018_channels[] = {
412 .type = IIO_LIGHT,
413 .indexed = 1,
414 .channel = 0,
415 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
416 BIT(IIO_CHAN_INFO_CALIBSCALE),
417 }, {
418 .type = IIO_INTENSITY,
419 .modified = 1,
420 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
421 .channel2 = IIO_MOD_LIGHT_IR,
422 }, {
423 /* Unindexed in current ABI. But perhaps it should be. */
424 .type = IIO_PROXIMITY,
425 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
429 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
430 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
431 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
432 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
433 show_resolution, store_resolution, 0);
434 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
435 S_IRUGO | S_IWUSR,
436 show_prox_infrared_suppression,
437 store_prox_infrared_suppression, 0);
439 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
440 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
441 static struct attribute *isl29018_attributes[] = {
442 ISL29018_DEV_ATTR(range),
443 ISL29018_CONST_ATTR(range_available),
444 ISL29018_DEV_ATTR(adc_resolution),
445 ISL29018_CONST_ATTR(adc_resolution_available),
446 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
447 NULL
450 static const struct attribute_group isl29108_group = {
451 .attrs = isl29018_attributes,
454 static int isl29018_chip_init(struct isl29018_chip *chip)
456 int status;
457 int new_adc_bit;
458 unsigned int new_range;
460 /* Code added per Intersil Application Note 1534:
461 * When VDD sinks to approximately 1.8V or below, some of
462 * the part's registers may change their state. When VDD
463 * recovers to 2.25V (or greater), the part may thus be in an
464 * unknown mode of operation. The user can return the part to
465 * a known mode of operation either by (a) setting VDD = 0V for
466 * 1 second or more and then powering back up with a slew rate
467 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
468 * conversions, clear the test registers, and then rewrite all
469 * registers to the desired values.
470 * ...
471 * FOR ISL29011, ISL29018, ISL29021, ISL29023
472 * 1. Write 0x00 to register 0x08 (TEST)
473 * 2. Write 0x00 to register 0x00 (CMD1)
474 * 3. Rewrite all registers to the desired values
476 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
477 * the same thing EXCEPT the data sheet asks for a 1ms delay after
478 * writing the CMD1 register.
480 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
481 if (status < 0) {
482 dev_err(chip->dev, "Failed to clear isl29018 TEST reg."
483 "(%d)\n", status);
484 return status;
487 /* See Intersil AN1534 comments above.
488 * "Operating Mode" (COMMAND1) register is reprogrammed when
489 * data is read from the device.
491 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
492 if (status < 0) {
493 dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg."
494 "(%d)\n", status);
495 return status;
498 msleep(1); /* per data sheet, page 10 */
500 /* set defaults */
501 status = isl29018_set_range(chip, chip->range, &new_range);
502 if (status < 0) {
503 dev_err(chip->dev, "Init of isl29018 fails\n");
504 return status;
507 status = isl29018_set_resolution(chip, chip->adc_bit,
508 &new_adc_bit);
510 return 0;
513 static const struct iio_info isl29108_info = {
514 .attrs = &isl29108_group,
515 .driver_module = THIS_MODULE,
516 .read_raw = &isl29018_read_raw,
517 .write_raw = &isl29018_write_raw,
520 static bool is_volatile_reg(struct device *dev, unsigned int reg)
522 switch (reg) {
523 case ISL29018_REG_ADD_DATA_LSB:
524 case ISL29018_REG_ADD_DATA_MSB:
525 case ISL29018_REG_ADD_COMMAND1:
526 case ISL29018_REG_TEST:
527 return true;
528 default:
529 return false;
534 * isl29018_regmap_config: regmap configuration.
535 * Use RBTREE mechanism for caching.
537 static const struct regmap_config isl29018_regmap_config = {
538 .reg_bits = 8,
539 .val_bits = 8,
540 .volatile_reg = is_volatile_reg,
541 .max_register = ISL29018_REG_TEST,
542 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
543 .cache_type = REGCACHE_RBTREE,
546 static int isl29018_probe(struct i2c_client *client,
547 const struct i2c_device_id *id)
549 struct isl29018_chip *chip;
550 struct iio_dev *indio_dev;
551 int err;
553 indio_dev = iio_device_alloc(sizeof(*chip));
554 if (indio_dev == NULL) {
555 dev_err(&client->dev, "iio allocation fails\n");
556 err = -ENOMEM;
557 goto exit;
559 chip = iio_priv(indio_dev);
561 i2c_set_clientdata(client, indio_dev);
562 chip->dev = &client->dev;
564 mutex_init(&chip->lock);
566 chip->lux_scale = 1;
567 chip->range = 1000;
568 chip->adc_bit = 16;
569 chip->suspended = false;
571 chip->regmap = devm_regmap_init_i2c(client, &isl29018_regmap_config);
572 if (IS_ERR(chip->regmap)) {
573 err = PTR_ERR(chip->regmap);
574 dev_err(chip->dev, "regmap initialization failed: %d\n", err);
575 goto exit;
578 err = isl29018_chip_init(chip);
579 if (err)
580 goto exit_iio_free;
582 indio_dev->info = &isl29108_info;
583 indio_dev->channels = isl29018_channels;
584 indio_dev->num_channels = ARRAY_SIZE(isl29018_channels);
585 indio_dev->name = id->name;
586 indio_dev->dev.parent = &client->dev;
587 indio_dev->modes = INDIO_DIRECT_MODE;
588 err = iio_device_register(indio_dev);
589 if (err) {
590 dev_err(&client->dev, "iio registration fails\n");
591 goto exit_iio_free;
594 return 0;
595 exit_iio_free:
596 iio_device_free(indio_dev);
597 exit:
598 return err;
601 static int isl29018_remove(struct i2c_client *client)
603 struct iio_dev *indio_dev = i2c_get_clientdata(client);
605 dev_dbg(&client->dev, "%s()\n", __func__);
606 iio_device_unregister(indio_dev);
607 iio_device_free(indio_dev);
609 return 0;
612 #ifdef CONFIG_PM_SLEEP
613 static int isl29018_suspend(struct device *dev)
615 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
617 mutex_lock(&chip->lock);
619 /* Since this driver uses only polling commands, we are by default in
620 * auto shutdown (ie, power-down) mode.
621 * So we do not have much to do here.
623 chip->suspended = true;
625 mutex_unlock(&chip->lock);
626 return 0;
629 static int isl29018_resume(struct device *dev)
631 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
632 int err;
634 mutex_lock(&chip->lock);
636 err = isl29018_chip_init(chip);
637 if (!err)
638 chip->suspended = false;
640 mutex_unlock(&chip->lock);
641 return err;
644 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
645 #define ISL29018_PM_OPS (&isl29018_pm_ops)
646 #else
647 #define ISL29018_PM_OPS NULL
648 #endif
650 static const struct i2c_device_id isl29018_id[] = {
651 {"isl29018", 0},
655 MODULE_DEVICE_TABLE(i2c, isl29018_id);
657 static const struct of_device_id isl29018_of_match[] = {
658 { .compatible = "isil,isl29018", },
659 { },
661 MODULE_DEVICE_TABLE(of, isl29018_of_match);
663 static struct i2c_driver isl29018_driver = {
664 .class = I2C_CLASS_HWMON,
665 .driver = {
666 .name = "isl29018",
667 .pm = ISL29018_PM_OPS,
668 .owner = THIS_MODULE,
669 .of_match_table = isl29018_of_match,
671 .probe = isl29018_probe,
672 .remove = isl29018_remove,
673 .id_table = isl29018_id,
675 module_i2c_driver(isl29018_driver);
677 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
678 MODULE_LICENSE("GPL");