xref: /linux/drivers/iio/light/isl29018.c (revision 2eff01ee2881becc9daaa0d53477ec202136b1f4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * A iio driver for the light sensor ISL 29018/29023/29035.
4  *
5  * IIO driver for monitoring ambient light intensity in luxi, proximity
6  * sensing and infrared sensing.
7  *
8  * Copyright (c) 2010, NVIDIA Corporation.
9  */
10 
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>
20 
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 
24 #define ISL29018_CONV_TIME_MS		100
25 
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
33 
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)
37 
38 #define ISL29018_CMD2_RANGE_SHIFT	0
39 #define ISL29018_CMD2_RANGE_MASK	(0x3 << ISL29018_CMD2_RANGE_SHIFT)
40 
41 #define ISL29018_CMD2_SCHEME_SHIFT	7
42 #define ISL29018_CMD2_SCHEME_MASK	(0x1 << ISL29018_CMD2_SCHEME_SHIFT)
43 
44 #define ISL29018_REG_ADD_DATA_LSB	0x02
45 #define ISL29018_REG_ADD_DATA_MSB	0x03
46 
47 #define ISL29018_REG_TEST		0x08
48 #define ISL29018_TEST_SHIFT		0
49 #define ISL29018_TEST_MASK		(0xFF << ISL29018_TEST_SHIFT)
50 
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)
57 
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,
63 };
64 
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},
69 };
70 
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} }
79 };
80 
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;
92 };
93 
94 static int isl29018_set_integration_time(struct isl29018_chip *chip,
95 					 unsigned int utime)
96 {
97 	unsigned int i;
98 	int ret;
99 	unsigned int int_time, new_int_time;
100 
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;
105 		}
106 	}
107 
108 	if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
109 		return -EINVAL;
110 
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;
116 
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;
124 		}
125 	}
126 	chip->int_time = new_int_time;
127 
128 	return 0;
129 }
130 
131 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
132 {
133 	unsigned int i;
134 	int ret;
135 	struct isl29018_scale new_scale;
136 
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;
142 		}
143 	}
144 
145 	if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
146 		return -EINVAL;
147 
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;
153 
154 	chip->scale = new_scale;
155 
156 	return 0;
157 }
158 
159 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
160 {
161 	int status;
162 	unsigned int lsb;
163 	unsigned int msb;
164 	struct device *dev = regmap_get_device(chip->regmap);
165 
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;
173 	}
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;
180 	}
181 
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;
187 	}
188 	dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
189 
190 	return (msb << 8) | lsb;
191 }
192 
193 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
194 {
195 	int lux_data;
196 	unsigned int data_x_range;
197 
198 	lux_data = isl29018_read_sensor_input(chip,
199 					      ISL29018_CMD1_OPMODE_ALS_ONCE);
200 	if (lux_data < 0)
201 		return lux_data;
202 
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;
207 
208 	return 0;
209 }
210 
211 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
212 {
213 	int ir_data;
214 
215 	ir_data = isl29018_read_sensor_input(chip,
216 					     ISL29018_CMD1_OPMODE_IR_ONCE);
217 	if (ir_data < 0)
218 		return ir_data;
219 
220 	*ir = ir_data;
221 
222 	return 0;
223 }
224 
225 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
226 				      int *near_ir)
227 {
228 	int status;
229 	int prox_data = -1;
230 	int ir_data = -1;
231 	struct device *dev = regmap_get_device(chip->regmap);
232 
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;
240 	}
241 
242 	prox_data = isl29018_read_sensor_input(chip,
243 					       ISL29018_CMD1_OPMODE_PROX_ONCE);
244 	if (prox_data < 0)
245 		return prox_data;
246 
247 	if (scheme == 1) {
248 		*near_ir = prox_data;
249 		return 0;
250 	}
251 
252 	ir_data = isl29018_read_sensor_input(chip,
253 					     ISL29018_CMD1_OPMODE_IR_ONCE);
254 	if (ir_data < 0)
255 		return ir_data;
256 
257 	if (prox_data >= ir_data)
258 		*near_ir = prox_data - ir_data;
259 	else
260 		*near_ir = 0;
261 
262 	return 0;
263 }
264 
265 static ssize_t in_illuminance_scale_available_show
266 			(struct device *dev, struct device_attribute *attr,
267 			 char *buf)
268 {
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;
273 
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);
280 
281 	buf[len - 1] = '\n';
282 
283 	return len;
284 }
285 
286 static ssize_t in_illuminance_integration_time_available_show
287 			(struct device *dev, struct device_attribute *attr,
288 			 char *buf)
289 {
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;
294 
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]);
298 
299 	buf[len - 1] = '\n';
300 
301 	return len;
302 }
303 
304 /*
305  * From ISL29018 Data Sheet (FN6619.4, Oct 8, 2012) regarding the
306  * infrared suppression:
307  *
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.
318  *
319  * 0 Sensing IR from LED and ambient
320  * 1 Sensing IR from LED with ambient IR rejection
321  */
322 static ssize_t proximity_on_chip_ambient_infrared_suppression_show
323 			(struct device *dev, struct device_attribute *attr,
324 			 char *buf)
325 {
326 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
327 	struct isl29018_chip *chip = iio_priv(indio_dev);
328 
329 	/*
330 	 * Return the "proximity scheme" i.e. if the chip does on chip
331 	 * infrared suppression (1 means perform on chip suppression)
332 	 */
333 	return sprintf(buf, "%d\n", chip->prox_scheme);
334 }
335 
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)
339 {
340 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
341 	struct isl29018_chip *chip = iio_priv(indio_dev);
342 	int val;
343 
344 	if (kstrtoint(buf, 10, &val))
345 		return -EINVAL;
346 	if (!(val == 0 || val == 1))
347 		return -EINVAL;
348 
349 	/*
350 	 * Get the "proximity scheme" i.e. if the chip does on chip
351 	 * infrared suppression (1 means perform on chip suppression)
352 	 */
353 	mutex_lock(&chip->lock);
354 	chip->prox_scheme = val;
355 	mutex_unlock(&chip->lock);
356 
357 	return count;
358 }
359 
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)
365 {
366 	struct isl29018_chip *chip = iio_priv(indio_dev);
367 	int ret = -EINVAL;
368 
369 	mutex_lock(&chip->lock);
370 	if (chip->suspended) {
371 		ret = -EBUSY;
372 		goto write_done;
373 	}
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;
380 		}
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;
392 	}
393 
394 write_done:
395 	mutex_unlock(&chip->lock);
396 
397 	return ret;
398 }
399 
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)
405 {
406 	int ret = -EINVAL;
407 	struct isl29018_chip *chip = iio_priv(indio_dev);
408 
409 	mutex_lock(&chip->lock);
410 	if (chip->suspended) {
411 		ret = -EBUSY;
412 		goto read_done;
413 	}
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;
431 		}
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;
440 		}
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;
447 		}
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;
454 		}
455 		break;
456 	default:
457 		break;
458 	}
459 
460 read_done:
461 	mutex_unlock(&chip->lock);
462 
463 	return ret;
464 }
465 
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),					\
474 }
475 
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,					\
481 }
482 
483 #define ISL29018_PROXIMITY_CHANNEL {					\
484 	.type = IIO_PROXIMITY,						\
485 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
486 }
487 
488 static const struct iio_chan_spec isl29018_channels[] = {
489 	ISL29018_LIGHT_CHANNEL,
490 	ISL29018_IR_CHANNEL,
491 	ISL29018_PROXIMITY_CHANNEL,
492 };
493 
494 static const struct iio_chan_spec isl29023_channels[] = {
495 	ISL29018_LIGHT_CHANNEL,
496 	ISL29018_IR_CHANNEL,
497 };
498 
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);
502 
503 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
504 
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
510 };
511 
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
516 };
517 
518 static const struct attribute_group isl29018_group = {
519 	.attrs = isl29018_attributes,
520 };
521 
522 static const struct attribute_group isl29023_group = {
523 	.attrs = isl29023_attributes,
524 };
525 
526 enum {
527 	isl29018,
528 	isl29023,
529 	isl29035,
530 };
531 
532 static int isl29018_chip_init(struct isl29018_chip *chip)
533 {
534 	int status;
535 	struct device *dev = regmap_get_device(chip->regmap);
536 
537 	if (chip->type == isl29035) {
538 		unsigned int id;
539 
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;
546 		}
547 
548 		id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
549 
550 		if (id != ISL29035_DEVICE_ID)
551 			return -ENODEV;
552 
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;
559 	}
560 
561 	/*
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
577 	 *
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.
581 	 */
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;
587 	}
588 
589 	/*
590 	 * See Intersil AN1534 comments above.
591 	 * "Operating Mode" (COMMAND1) register is reprogrammed when
592 	 * data is read from the device.
593 	 */
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;
599 	}
600 
601 	usleep_range(1000, 2000);	/* per data sheet, page 10 */
602 
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;
609 	}
610 
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");
615 
616 	return status;
617 }
618 
619 static const struct iio_info isl29018_info = {
620 	.attrs = &isl29018_group,
621 	.read_raw = isl29018_read_raw,
622 	.write_raw = isl29018_write_raw,
623 };
624 
625 static const struct iio_info isl29023_info = {
626 	.attrs = &isl29023_group,
627 	.read_raw = isl29018_read_raw,
628 	.write_raw = isl29018_write_raw,
629 };
630 
631 static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
632 {
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;
642 	}
643 }
644 
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,
652 };
653 
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,
661 };
662 
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;
668 };
669 
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,
676 	},
677 	[isl29023] = {
678 		.channels = isl29023_channels,
679 		.num_channels = ARRAY_SIZE(isl29023_channels),
680 		.indio_info = &isl29023_info,
681 		.regmap_cfg = &isl29018_regmap_config,
682 	},
683 	[isl29035] = {
684 		.channels = isl29023_channels,
685 		.num_channels = ARRAY_SIZE(isl29023_channels),
686 		.indio_info = &isl29023_info,
687 		.regmap_cfg = &isl29035_regmap_config,
688 	},
689 };
690 
691 static void isl29018_disable_regulator_action(void *_data)
692 {
693 	struct isl29018_chip *chip = _data;
694 	int err;
695 
696 	err = regulator_disable(chip->vcc_reg);
697 	if (err)
698 		pr_err("failed to disable isl29018's VCC regulator!\n");
699 }
700 
701 static int isl29018_probe(struct i2c_client *client)
702 {
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;
710 
711 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
712 	if (!indio_dev)
713 		return -ENOMEM;
714 
715 	chip = iio_priv(indio_dev);
716 
717 	i2c_set_clientdata(client, indio_dev);
718 
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;
725 	}
726 
727 	mutex_init(&chip->lock);
728 
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;
735 
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");
740 
741 	err = regulator_enable(chip->vcc_reg);
742 	if (err) {
743 		dev_err(&client->dev, "failed to enable VCC regulator!\n");
744 		return err;
745 	}
746 
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;
752 	}
753 
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;
760 	}
761 
762 	err = isl29018_chip_init(chip);
763 	if (err)
764 		return err;
765 
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;
771 
772 	return devm_iio_device_register(&client->dev, indio_dev);
773 }
774 
775 static int isl29018_suspend(struct device *dev)
776 {
777 	struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
778 	int ret;
779 
780 	mutex_lock(&chip->lock);
781 
782 	/*
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.
786 	 */
787 	chip->suspended = true;
788 	ret = regulator_disable(chip->vcc_reg);
789 	if (ret)
790 		dev_err(dev, "failed to disable VCC regulator\n");
791 
792 	mutex_unlock(&chip->lock);
793 
794 	return ret;
795 }
796 
797 static int isl29018_resume(struct device *dev)
798 {
799 	struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
800 	int err;
801 
802 	mutex_lock(&chip->lock);
803 
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;
809 	}
810 
811 	err = isl29018_chip_init(chip);
812 	if (!err)
813 		chip->suspended = false;
814 
815 	mutex_unlock(&chip->lock);
816 
817 	return err;
818 }
819 
820 static DEFINE_SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend,
821 				isl29018_resume);
822 
823 static const struct acpi_device_id isl29018_acpi_match[] = {
824 	{"ISL29018", isl29018},
825 	{"ISL29023", isl29023},
826 	{"ISL29035", isl29035},
827 	{}
828 };
829 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
830 
831 static const struct i2c_device_id isl29018_id[] = {
832 	{"isl29018", isl29018},
833 	{"isl29023", isl29023},
834 	{"isl29035", isl29035},
835 	{}
836 };
837 MODULE_DEVICE_TABLE(i2c, isl29018_id);
838 
839 static const struct of_device_id isl29018_of_match[] = {
840 	{ .compatible = "isil,isl29018", },
841 	{ .compatible = "isil,isl29023", },
842 	{ .compatible = "isil,isl29035", },
843 	{ }
844 };
845 MODULE_DEVICE_TABLE(of, isl29018_of_match);
846 
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,
853 		    },
854 	.probe = isl29018_probe,
855 	.id_table = isl29018_id,
856 };
857 module_i2c_driver(isl29018_driver);
858 
859 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
860 MODULE_LICENSE("GPL");
861