Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / iio / light / isl29018.c
blob201eae1c45892b5941265593b0a76caffe8b0c77
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * A iio driver for the light sensor ISL 29018/29023/29035.
5 * IIO driver for monitoring ambient light intensity in luxi, proximity
6 * sensing and infrared sensing.
8 * Copyright (c) 2010, NVIDIA Corporation.
9 */
11 #include <linux/i2c.h>
12 #include <linux/err.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/delay.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 #define ISL29018_CONV_TIME_MS 100
26 #define ISL29018_REG_ADD_COMMAND1 0x00
27 #define ISL29018_CMD1_OPMODE_SHIFT 5
28 #define ISL29018_CMD1_OPMODE_MASK (7 << ISL29018_CMD1_OPMODE_SHIFT)
29 #define ISL29018_CMD1_OPMODE_POWER_DOWN 0
30 #define ISL29018_CMD1_OPMODE_ALS_ONCE 1
31 #define ISL29018_CMD1_OPMODE_IR_ONCE 2
32 #define ISL29018_CMD1_OPMODE_PROX_ONCE 3
34 #define ISL29018_REG_ADD_COMMAND2 0x01
35 #define ISL29018_CMD2_RESOLUTION_SHIFT 2
36 #define ISL29018_CMD2_RESOLUTION_MASK (0x3 << ISL29018_CMD2_RESOLUTION_SHIFT)
38 #define ISL29018_CMD2_RANGE_SHIFT 0
39 #define ISL29018_CMD2_RANGE_MASK (0x3 << ISL29018_CMD2_RANGE_SHIFT)
41 #define ISL29018_CMD2_SCHEME_SHIFT 7
42 #define ISL29018_CMD2_SCHEME_MASK (0x1 << ISL29018_CMD2_SCHEME_SHIFT)
44 #define ISL29018_REG_ADD_DATA_LSB 0x02
45 #define ISL29018_REG_ADD_DATA_MSB 0x03
47 #define ISL29018_REG_TEST 0x08
48 #define ISL29018_TEST_SHIFT 0
49 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
51 #define ISL29035_REG_DEVICE_ID 0x0F
52 #define ISL29035_DEVICE_ID_SHIFT 0x03
53 #define ISL29035_DEVICE_ID_MASK (0x7 << ISL29035_DEVICE_ID_SHIFT)
54 #define ISL29035_DEVICE_ID 0x5
55 #define ISL29035_BOUT_SHIFT 0x07
56 #define ISL29035_BOUT_MASK (0x01 << ISL29035_BOUT_SHIFT)
58 enum isl29018_int_time {
59 ISL29018_INT_TIME_16,
60 ISL29018_INT_TIME_12,
61 ISL29018_INT_TIME_8,
62 ISL29018_INT_TIME_4,
65 static const unsigned int isl29018_int_utimes[3][4] = {
66 {90000, 5630, 351, 21},
67 {90000, 5600, 352, 22},
68 {105000, 6500, 410, 25},
71 static const struct isl29018_scale {
72 unsigned int scale;
73 unsigned int uscale;
74 } isl29018_scales[4][4] = {
75 { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
76 { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
77 { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
78 { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
81 struct isl29018_chip {
82 struct regmap *regmap;
83 struct mutex lock;
84 int type;
85 unsigned int calibscale;
86 unsigned int ucalibscale;
87 unsigned int int_time;
88 struct isl29018_scale scale;
89 int prox_scheme;
90 bool suspended;
91 struct regulator *vcc_reg;
94 static int isl29018_set_integration_time(struct isl29018_chip *chip,
95 unsigned int utime)
97 unsigned int i;
98 int ret;
99 unsigned int int_time, new_int_time;
101 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
102 if (utime == isl29018_int_utimes[chip->type][i]) {
103 new_int_time = i;
104 break;
108 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
109 return -EINVAL;
111 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
112 ISL29018_CMD2_RESOLUTION_MASK,
113 i << ISL29018_CMD2_RESOLUTION_SHIFT);
114 if (ret < 0)
115 return ret;
117 /* Keep the same range when integration time changes */
118 int_time = chip->int_time;
119 for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
120 if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
121 chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
122 chip->scale = isl29018_scales[new_int_time][i];
123 break;
126 chip->int_time = new_int_time;
128 return 0;
131 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
133 unsigned int i;
134 int ret;
135 struct isl29018_scale new_scale;
137 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
138 if (scale == isl29018_scales[chip->int_time][i].scale &&
139 uscale == isl29018_scales[chip->int_time][i].uscale) {
140 new_scale = isl29018_scales[chip->int_time][i];
141 break;
145 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
146 return -EINVAL;
148 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
149 ISL29018_CMD2_RANGE_MASK,
150 i << ISL29018_CMD2_RANGE_SHIFT);
151 if (ret < 0)
152 return ret;
154 chip->scale = new_scale;
156 return 0;
159 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
161 int status;
162 unsigned int lsb;
163 unsigned int msb;
164 struct device *dev = regmap_get_device(chip->regmap);
166 /* Set mode */
167 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
168 mode << ISL29018_CMD1_OPMODE_SHIFT);
169 if (status) {
170 dev_err(dev,
171 "Error in setting operating mode err %d\n", status);
172 return status;
174 msleep(ISL29018_CONV_TIME_MS);
175 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
176 if (status < 0) {
177 dev_err(dev,
178 "Error in reading LSB DATA with err %d\n", status);
179 return status;
182 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
183 if (status < 0) {
184 dev_err(dev,
185 "Error in reading MSB DATA with error %d\n", status);
186 return status;
188 dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
190 return (msb << 8) | lsb;
193 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
195 int lux_data;
196 unsigned int data_x_range;
198 lux_data = isl29018_read_sensor_input(chip,
199 ISL29018_CMD1_OPMODE_ALS_ONCE);
200 if (lux_data < 0)
201 return lux_data;
203 data_x_range = lux_data * chip->scale.scale +
204 lux_data * chip->scale.uscale / 1000000;
205 *lux = data_x_range * chip->calibscale +
206 data_x_range * chip->ucalibscale / 1000000;
208 return 0;
211 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
213 int ir_data;
215 ir_data = isl29018_read_sensor_input(chip,
216 ISL29018_CMD1_OPMODE_IR_ONCE);
217 if (ir_data < 0)
218 return ir_data;
220 *ir = ir_data;
222 return 0;
225 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
226 int *near_ir)
228 int status;
229 int prox_data = -1;
230 int ir_data = -1;
231 struct device *dev = regmap_get_device(chip->regmap);
233 /* Do proximity sensing with required scheme */
234 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
235 ISL29018_CMD2_SCHEME_MASK,
236 scheme << ISL29018_CMD2_SCHEME_SHIFT);
237 if (status) {
238 dev_err(dev, "Error in setting operating mode\n");
239 return status;
242 prox_data = isl29018_read_sensor_input(chip,
243 ISL29018_CMD1_OPMODE_PROX_ONCE);
244 if (prox_data < 0)
245 return prox_data;
247 if (scheme == 1) {
248 *near_ir = prox_data;
249 return 0;
252 ir_data = isl29018_read_sensor_input(chip,
253 ISL29018_CMD1_OPMODE_IR_ONCE);
254 if (ir_data < 0)
255 return ir_data;
257 if (prox_data >= ir_data)
258 *near_ir = prox_data - ir_data;
259 else
260 *near_ir = 0;
262 return 0;
265 static ssize_t in_illuminance_scale_available_show
266 (struct device *dev, struct device_attribute *attr,
267 char *buf)
269 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
270 struct isl29018_chip *chip = iio_priv(indio_dev);
271 unsigned int i;
272 int len = 0;
274 mutex_lock(&chip->lock);
275 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
276 len += sprintf(buf + len, "%d.%06d ",
277 isl29018_scales[chip->int_time][i].scale,
278 isl29018_scales[chip->int_time][i].uscale);
279 mutex_unlock(&chip->lock);
281 buf[len - 1] = '\n';
283 return len;
286 static ssize_t in_illuminance_integration_time_available_show
287 (struct device *dev, struct device_attribute *attr,
288 char *buf)
290 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
291 struct isl29018_chip *chip = iio_priv(indio_dev);
292 unsigned int i;
293 int len = 0;
295 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
296 len += sprintf(buf + len, "0.%06d ",
297 isl29018_int_utimes[chip->type][i]);
299 buf[len - 1] = '\n';
301 return len;
305 * From ISL29018 Data Sheet (FN6619.4, Oct 8, 2012) regarding the
306 * infrared suppression:
308 * Proximity Sensing Scheme: Bit 7. This bit programs the function
309 * of the proximity detection. Logic 0 of this bit, Scheme 0, makes
310 * full n (4, 8, 12, 16) bits (unsigned) proximity detection. The range
311 * of Scheme 0 proximity count is from 0 to 2^n. Logic 1 of this bit,
312 * Scheme 1, makes n-1 (3, 7, 11, 15) bits (2's complementary)
313 * proximity_less_ambient detection. The range of Scheme 1
314 * proximity count is from -2^(n-1) to 2^(n-1) . The sign bit is extended
315 * for resolutions less than 16. While Scheme 0 has wider dynamic
316 * range, Scheme 1 proximity detection is less affected by the
317 * ambient IR noise variation.
319 * 0 Sensing IR from LED and ambient
320 * 1 Sensing IR from LED with ambient IR rejection
322 static ssize_t proximity_on_chip_ambient_infrared_suppression_show
323 (struct device *dev, struct device_attribute *attr,
324 char *buf)
326 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
327 struct isl29018_chip *chip = iio_priv(indio_dev);
330 * Return the "proximity scheme" i.e. if the chip does on chip
331 * infrared suppression (1 means perform on chip suppression)
333 return sprintf(buf, "%d\n", chip->prox_scheme);
336 static ssize_t proximity_on_chip_ambient_infrared_suppression_store
337 (struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
340 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
341 struct isl29018_chip *chip = iio_priv(indio_dev);
342 int val;
344 if (kstrtoint(buf, 10, &val))
345 return -EINVAL;
346 if (!(val == 0 || val == 1))
347 return -EINVAL;
350 * Get the "proximity scheme" i.e. if the chip does on chip
351 * infrared suppression (1 means perform on chip suppression)
353 mutex_lock(&chip->lock);
354 chip->prox_scheme = val;
355 mutex_unlock(&chip->lock);
357 return count;
360 static int isl29018_write_raw(struct iio_dev *indio_dev,
361 struct iio_chan_spec const *chan,
362 int val,
363 int val2,
364 long mask)
366 struct isl29018_chip *chip = iio_priv(indio_dev);
367 int ret = -EINVAL;
369 mutex_lock(&chip->lock);
370 if (chip->suspended) {
371 ret = -EBUSY;
372 goto write_done;
374 switch (mask) {
375 case IIO_CHAN_INFO_CALIBSCALE:
376 if (chan->type == IIO_LIGHT) {
377 chip->calibscale = val;
378 chip->ucalibscale = val2;
379 ret = 0;
381 break;
382 case IIO_CHAN_INFO_INT_TIME:
383 if (chan->type == IIO_LIGHT && !val)
384 ret = isl29018_set_integration_time(chip, val2);
385 break;
386 case IIO_CHAN_INFO_SCALE:
387 if (chan->type == IIO_LIGHT)
388 ret = isl29018_set_scale(chip, val, val2);
389 break;
390 default:
391 break;
394 write_done:
395 mutex_unlock(&chip->lock);
397 return ret;
400 static int isl29018_read_raw(struct iio_dev *indio_dev,
401 struct iio_chan_spec const *chan,
402 int *val,
403 int *val2,
404 long mask)
406 int ret = -EINVAL;
407 struct isl29018_chip *chip = iio_priv(indio_dev);
409 mutex_lock(&chip->lock);
410 if (chip->suspended) {
411 ret = -EBUSY;
412 goto read_done;
414 switch (mask) {
415 case IIO_CHAN_INFO_RAW:
416 case IIO_CHAN_INFO_PROCESSED:
417 switch (chan->type) {
418 case IIO_LIGHT:
419 ret = isl29018_read_lux(chip, val);
420 break;
421 case IIO_INTENSITY:
422 ret = isl29018_read_ir(chip, val);
423 break;
424 case IIO_PROXIMITY:
425 ret = isl29018_read_proximity_ir(chip,
426 chip->prox_scheme,
427 val);
428 break;
429 default:
430 break;
432 if (!ret)
433 ret = IIO_VAL_INT;
434 break;
435 case IIO_CHAN_INFO_INT_TIME:
436 if (chan->type == IIO_LIGHT) {
437 *val = 0;
438 *val2 = isl29018_int_utimes[chip->type][chip->int_time];
439 ret = IIO_VAL_INT_PLUS_MICRO;
441 break;
442 case IIO_CHAN_INFO_SCALE:
443 if (chan->type == IIO_LIGHT) {
444 *val = chip->scale.scale;
445 *val2 = chip->scale.uscale;
446 ret = IIO_VAL_INT_PLUS_MICRO;
448 break;
449 case IIO_CHAN_INFO_CALIBSCALE:
450 if (chan->type == IIO_LIGHT) {
451 *val = chip->calibscale;
452 *val2 = chip->ucalibscale;
453 ret = IIO_VAL_INT_PLUS_MICRO;
455 break;
456 default:
457 break;
460 read_done:
461 mutex_unlock(&chip->lock);
463 return ret;
466 #define ISL29018_LIGHT_CHANNEL { \
467 .type = IIO_LIGHT, \
468 .indexed = 1, \
469 .channel = 0, \
470 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
471 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
472 BIT(IIO_CHAN_INFO_SCALE) | \
473 BIT(IIO_CHAN_INFO_INT_TIME), \
476 #define ISL29018_IR_CHANNEL { \
477 .type = IIO_INTENSITY, \
478 .modified = 1, \
479 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
480 .channel2 = IIO_MOD_LIGHT_IR, \
483 #define ISL29018_PROXIMITY_CHANNEL { \
484 .type = IIO_PROXIMITY, \
485 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
488 static const struct iio_chan_spec isl29018_channels[] = {
489 ISL29018_LIGHT_CHANNEL,
490 ISL29018_IR_CHANNEL,
491 ISL29018_PROXIMITY_CHANNEL,
494 static const struct iio_chan_spec isl29023_channels[] = {
495 ISL29018_LIGHT_CHANNEL,
496 ISL29018_IR_CHANNEL,
499 static IIO_DEVICE_ATTR_RO(in_illuminance_integration_time_available, 0);
500 static IIO_DEVICE_ATTR_RO(in_illuminance_scale_available, 0);
501 static IIO_DEVICE_ATTR_RW(proximity_on_chip_ambient_infrared_suppression, 0);
503 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
505 static struct attribute *isl29018_attributes[] = {
506 ISL29018_DEV_ATTR(in_illuminance_scale_available),
507 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
508 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
509 NULL
512 static struct attribute *isl29023_attributes[] = {
513 ISL29018_DEV_ATTR(in_illuminance_scale_available),
514 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
515 NULL
518 static const struct attribute_group isl29018_group = {
519 .attrs = isl29018_attributes,
522 static const struct attribute_group isl29023_group = {
523 .attrs = isl29023_attributes,
526 enum {
527 isl29018,
528 isl29023,
529 isl29035,
532 static int isl29018_chip_init(struct isl29018_chip *chip)
534 int status;
535 struct device *dev = regmap_get_device(chip->regmap);
537 if (chip->type == isl29035) {
538 unsigned int id;
540 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
541 if (status < 0) {
542 dev_err(dev,
543 "Error reading ID register with error %d\n",
544 status);
545 return status;
548 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
550 if (id != ISL29035_DEVICE_ID)
551 return -ENODEV;
553 /* Clear brownout bit */
554 status = regmap_clear_bits(chip->regmap,
555 ISL29035_REG_DEVICE_ID,
556 ISL29035_BOUT_MASK);
557 if (status < 0)
558 return status;
562 * Code added per Intersil Application Note 1534:
563 * When VDD sinks to approximately 1.8V or below, some of
564 * the part's registers may change their state. When VDD
565 * recovers to 2.25V (or greater), the part may thus be in an
566 * unknown mode of operation. The user can return the part to
567 * a known mode of operation either by (a) setting VDD = 0V for
568 * 1 second or more and then powering back up with a slew rate
569 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
570 * conversions, clear the test registers, and then rewrite all
571 * registers to the desired values.
572 * ...
573 * For ISL29011, ISL29018, ISL29021, ISL29023
574 * 1. Write 0x00 to register 0x08 (TEST)
575 * 2. Write 0x00 to register 0x00 (CMD1)
576 * 3. Rewrite all registers to the desired values
578 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
579 * the same thing EXCEPT the data sheet asks for a 1ms delay after
580 * writing the CMD1 register.
582 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
583 if (status < 0) {
584 dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
585 status);
586 return status;
590 * See Intersil AN1534 comments above.
591 * "Operating Mode" (COMMAND1) register is reprogrammed when
592 * data is read from the device.
594 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
595 if (status < 0) {
596 dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
597 status);
598 return status;
601 usleep_range(1000, 2000); /* per data sheet, page 10 */
603 /* Set defaults */
604 status = isl29018_set_scale(chip, chip->scale.scale,
605 chip->scale.uscale);
606 if (status < 0) {
607 dev_err(dev, "Init of isl29018 fails\n");
608 return status;
611 status = isl29018_set_integration_time(chip,
612 isl29018_int_utimes[chip->type][chip->int_time]);
613 if (status < 0)
614 dev_err(dev, "Init of isl29018 fails\n");
616 return status;
619 static const struct iio_info isl29018_info = {
620 .attrs = &isl29018_group,
621 .read_raw = isl29018_read_raw,
622 .write_raw = isl29018_write_raw,
625 static const struct iio_info isl29023_info = {
626 .attrs = &isl29023_group,
627 .read_raw = isl29018_read_raw,
628 .write_raw = isl29018_write_raw,
631 static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
633 switch (reg) {
634 case ISL29018_REG_ADD_DATA_LSB:
635 case ISL29018_REG_ADD_DATA_MSB:
636 case ISL29018_REG_ADD_COMMAND1:
637 case ISL29018_REG_TEST:
638 case ISL29035_REG_DEVICE_ID:
639 return true;
640 default:
641 return false;
645 static const struct regmap_config isl29018_regmap_config = {
646 .reg_bits = 8,
647 .val_bits = 8,
648 .volatile_reg = isl29018_is_volatile_reg,
649 .max_register = ISL29018_REG_TEST,
650 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
651 .cache_type = REGCACHE_RBTREE,
654 static const struct regmap_config isl29035_regmap_config = {
655 .reg_bits = 8,
656 .val_bits = 8,
657 .volatile_reg = isl29018_is_volatile_reg,
658 .max_register = ISL29035_REG_DEVICE_ID,
659 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
660 .cache_type = REGCACHE_RBTREE,
663 struct isl29018_chip_info {
664 const struct iio_chan_spec *channels;
665 int num_channels;
666 const struct iio_info *indio_info;
667 const struct regmap_config *regmap_cfg;
670 static const struct isl29018_chip_info isl29018_chip_info_tbl[] = {
671 [isl29018] = {
672 .channels = isl29018_channels,
673 .num_channels = ARRAY_SIZE(isl29018_channels),
674 .indio_info = &isl29018_info,
675 .regmap_cfg = &isl29018_regmap_config,
677 [isl29023] = {
678 .channels = isl29023_channels,
679 .num_channels = ARRAY_SIZE(isl29023_channels),
680 .indio_info = &isl29023_info,
681 .regmap_cfg = &isl29018_regmap_config,
683 [isl29035] = {
684 .channels = isl29023_channels,
685 .num_channels = ARRAY_SIZE(isl29023_channels),
686 .indio_info = &isl29023_info,
687 .regmap_cfg = &isl29035_regmap_config,
691 static void isl29018_disable_regulator_action(void *_data)
693 struct isl29018_chip *chip = _data;
694 int err;
696 err = regulator_disable(chip->vcc_reg);
697 if (err)
698 pr_err("failed to disable isl29018's VCC regulator!\n");
701 static int isl29018_probe(struct i2c_client *client)
703 const struct i2c_device_id *id = i2c_client_get_device_id(client);
704 struct isl29018_chip *chip;
705 struct iio_dev *indio_dev;
706 const void *ddata = NULL;
707 const char *name;
708 int dev_id;
709 int err;
711 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
712 if (!indio_dev)
713 return -ENOMEM;
715 chip = iio_priv(indio_dev);
717 i2c_set_clientdata(client, indio_dev);
719 if (id) {
720 name = id->name;
721 dev_id = id->driver_data;
722 } else {
723 name = iio_get_acpi_device_name_and_data(&client->dev, &ddata);
724 dev_id = (intptr_t)ddata;
727 mutex_init(&chip->lock);
729 chip->type = dev_id;
730 chip->calibscale = 1;
731 chip->ucalibscale = 0;
732 chip->int_time = ISL29018_INT_TIME_16;
733 chip->scale = isl29018_scales[chip->int_time][0];
734 chip->suspended = false;
736 chip->vcc_reg = devm_regulator_get(&client->dev, "vcc");
737 if (IS_ERR(chip->vcc_reg))
738 return dev_err_probe(&client->dev, PTR_ERR(chip->vcc_reg),
739 "failed to get VCC regulator!\n");
741 err = regulator_enable(chip->vcc_reg);
742 if (err) {
743 dev_err(&client->dev, "failed to enable VCC regulator!\n");
744 return err;
747 err = devm_add_action_or_reset(&client->dev, isl29018_disable_regulator_action,
748 chip);
749 if (err) {
750 dev_err(&client->dev, "failed to setup regulator cleanup action!\n");
751 return err;
754 chip->regmap = devm_regmap_init_i2c(client,
755 isl29018_chip_info_tbl[dev_id].regmap_cfg);
756 if (IS_ERR(chip->regmap)) {
757 err = PTR_ERR(chip->regmap);
758 dev_err(&client->dev, "regmap initialization fails: %d\n", err);
759 return err;
762 err = isl29018_chip_init(chip);
763 if (err)
764 return err;
766 indio_dev->info = isl29018_chip_info_tbl[dev_id].indio_info;
767 indio_dev->channels = isl29018_chip_info_tbl[dev_id].channels;
768 indio_dev->num_channels = isl29018_chip_info_tbl[dev_id].num_channels;
769 indio_dev->name = name;
770 indio_dev->modes = INDIO_DIRECT_MODE;
772 return devm_iio_device_register(&client->dev, indio_dev);
775 static int isl29018_suspend(struct device *dev)
777 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
778 int ret;
780 mutex_lock(&chip->lock);
783 * Since this driver uses only polling commands, we are by default in
784 * auto shutdown (ie, power-down) mode.
785 * So we do not have much to do here.
787 chip->suspended = true;
788 ret = regulator_disable(chip->vcc_reg);
789 if (ret)
790 dev_err(dev, "failed to disable VCC regulator\n");
792 mutex_unlock(&chip->lock);
794 return ret;
797 static int isl29018_resume(struct device *dev)
799 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
800 int err;
802 mutex_lock(&chip->lock);
804 err = regulator_enable(chip->vcc_reg);
805 if (err) {
806 dev_err(dev, "failed to enable VCC regulator\n");
807 mutex_unlock(&chip->lock);
808 return err;
811 err = isl29018_chip_init(chip);
812 if (!err)
813 chip->suspended = false;
815 mutex_unlock(&chip->lock);
817 return err;
820 static DEFINE_SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend,
821 isl29018_resume);
823 static const struct acpi_device_id isl29018_acpi_match[] = {
824 {"ISL29018", isl29018},
825 {"ISL29023", isl29023},
826 {"ISL29035", isl29035},
829 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
831 static const struct i2c_device_id isl29018_id[] = {
832 {"isl29018", isl29018},
833 {"isl29023", isl29023},
834 {"isl29035", isl29035},
837 MODULE_DEVICE_TABLE(i2c, isl29018_id);
839 static const struct of_device_id isl29018_of_match[] = {
840 { .compatible = "isil,isl29018", },
841 { .compatible = "isil,isl29023", },
842 { .compatible = "isil,isl29035", },
845 MODULE_DEVICE_TABLE(of, isl29018_of_match);
847 static struct i2c_driver isl29018_driver = {
848 .driver = {
849 .name = "isl29018",
850 .acpi_match_table = isl29018_acpi_match,
851 .pm = pm_sleep_ptr(&isl29018_pm_ops),
852 .of_match_table = isl29018_of_match,
854 .probe = isl29018_probe,
855 .id_table = isl29018_id,
857 module_i2c_driver(isl29018_driver);
859 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
860 MODULE_LICENSE("GPL");