1/*
2 * A iio driver for the light sensor ISL 29018/29023/29035.
3 *
4 * IIO driver for monitoring ambient light intensity in luxi, proximity
5 * sensing and infrared sensing.
6 *
7 * Copyright (c) 2010, NVIDIA Corporation.
8 *
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.
13 *
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.
18 *
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.
22 */
23
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>
33#include <linux/acpi.h>
34
35#define CONVERSION_TIME_MS		100
36
37#define ISL29018_REG_ADD_COMMAND1	0x00
38#define COMMMAND1_OPMODE_SHIFT		5
39#define COMMMAND1_OPMODE_MASK		(7 << COMMMAND1_OPMODE_SHIFT)
40#define COMMMAND1_OPMODE_POWER_DOWN	0
41#define COMMMAND1_OPMODE_ALS_ONCE	1
42#define COMMMAND1_OPMODE_IR_ONCE	2
43#define COMMMAND1_OPMODE_PROX_ONCE	3
44
45#define ISL29018_REG_ADD_COMMANDII	0x01
46#define COMMANDII_RESOLUTION_SHIFT	2
47#define COMMANDII_RESOLUTION_MASK	(0x3 << COMMANDII_RESOLUTION_SHIFT)
48
49#define COMMANDII_RANGE_SHIFT		0
50#define COMMANDII_RANGE_MASK		(0x3 << COMMANDII_RANGE_SHIFT)
51
52#define COMMANDII_SCHEME_SHIFT		7
53#define COMMANDII_SCHEME_MASK		(0x1 << COMMANDII_SCHEME_SHIFT)
54
55#define ISL29018_REG_ADD_DATA_LSB	0x02
56#define ISL29018_REG_ADD_DATA_MSB	0x03
57
58#define ISL29018_REG_TEST		0x08
59#define ISL29018_TEST_SHIFT		0
60#define ISL29018_TEST_MASK		(0xFF << ISL29018_TEST_SHIFT)
61
62#define ISL29035_REG_DEVICE_ID		0x0F
63#define ISL29035_DEVICE_ID_SHIFT	0x03
64#define ISL29035_DEVICE_ID_MASK		(0x7 << ISL29035_DEVICE_ID_SHIFT)
65#define ISL29035_DEVICE_ID		0x5
66#define ISL29035_BOUT_SHIFT		0x07
67#define ISL29035_BOUT_MASK		(0x01 << ISL29035_BOUT_SHIFT)
68
69#define ISL29018_INT_TIME_AVAIL		"0.090000 0.005630 0.000351 0.000021"
70#define ISL29023_INT_TIME_AVAIL		"0.090000 0.005600 0.000352 0.000022"
71#define ISL29035_INT_TIME_AVAIL		"0.105000 0.006500 0.000410 0.000025"
72
73static const char * const int_time_avail[] = {
74	ISL29018_INT_TIME_AVAIL,
75	ISL29023_INT_TIME_AVAIL,
76	ISL29035_INT_TIME_AVAIL,
77};
78
79enum isl29018_int_time {
80	ISL29018_INT_TIME_16,
81	ISL29018_INT_TIME_12,
82	ISL29018_INT_TIME_8,
83	ISL29018_INT_TIME_4,
84};
85
86static const unsigned int isl29018_int_utimes[3][4] = {
87	{90000, 5630, 351, 21},
88	{90000, 5600, 352, 22},
89	{105000, 6500, 410, 25},
90};
91
92static const struct isl29018_scale {
93	unsigned int scale;
94	unsigned int uscale;
95} isl29018_scales[4][4] = {
96	{ {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
97	{ {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
98	{ {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
99	{ {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
100};
101
102struct isl29018_chip {
103	struct device		*dev;
104	struct regmap		*regmap;
105	struct mutex		lock;
106	int			type;
107	unsigned int		calibscale;
108	unsigned int		ucalibscale;
109	unsigned int		int_time;
110	struct isl29018_scale	scale;
111	int			prox_scheme;
112	bool			suspended;
113};
114
115static int isl29018_set_integration_time(struct isl29018_chip *chip,
116					 unsigned int utime)
117{
118	int i, ret;
119	unsigned int int_time, new_int_time;
120
121	for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
122		if (utime == isl29018_int_utimes[chip->type][i]) {
123			new_int_time = i;
124			break;
125		}
126	}
127
128	if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
129		return -EINVAL;
130
131	ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
132				 COMMANDII_RESOLUTION_MASK,
133				 i << COMMANDII_RESOLUTION_SHIFT);
134	if (ret < 0)
135		return ret;
136
137	/* keep the same range when integration time changes */
138	int_time = chip->int_time;
139	for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
140		if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
141		    chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
142			chip->scale = isl29018_scales[new_int_time][i];
143			break;
144		}
145	}
146	chip->int_time = new_int_time;
147
148	return 0;
149}
150
151static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
152{
153	int i, ret;
154	struct isl29018_scale new_scale;
155
156	for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
157		if (scale == isl29018_scales[chip->int_time][i].scale &&
158		    uscale == isl29018_scales[chip->int_time][i].uscale) {
159			new_scale = isl29018_scales[chip->int_time][i];
160			break;
161		}
162	}
163
164	if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
165		return -EINVAL;
166
167	ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
168				 COMMANDII_RANGE_MASK,
169				 i << COMMANDII_RANGE_SHIFT);
170	if (ret < 0)
171		return ret;
172
173	chip->scale = new_scale;
174
175	return 0;
176}
177
178static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
179{
180	int status;
181	unsigned int lsb;
182	unsigned int msb;
183
184	/* Set mode */
185	status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
186			mode << COMMMAND1_OPMODE_SHIFT);
187	if (status) {
188		dev_err(chip->dev,
189			"Error in setting operating mode err %d\n", status);
190		return status;
191	}
192	msleep(CONVERSION_TIME_MS);
193	status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
194	if (status < 0) {
195		dev_err(chip->dev,
196			"Error in reading LSB DATA with err %d\n", status);
197		return status;
198	}
199
200	status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
201	if (status < 0) {
202		dev_err(chip->dev,
203			"Error in reading MSB DATA with error %d\n", status);
204		return status;
205	}
206	dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
207
208	return (msb << 8) | lsb;
209}
210
211static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
212{
213	int lux_data;
214	unsigned int data_x_range;
215
216	lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
217
218	if (lux_data < 0)
219		return lux_data;
220
221	data_x_range = lux_data * chip->scale.scale +
222		       lux_data * chip->scale.uscale / 1000000;
223	*lux = data_x_range * chip->calibscale +
224	       data_x_range * chip->ucalibscale / 1000000;
225
226	return 0;
227}
228
229static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
230{
231	int ir_data;
232
233	ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
234
235	if (ir_data < 0)
236		return ir_data;
237
238	*ir = ir_data;
239
240	return 0;
241}
242
243static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
244		int *near_ir)
245{
246	int status;
247	int prox_data = -1;
248	int ir_data = -1;
249
250	/* Do proximity sensing with required scheme */
251	status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
252			COMMANDII_SCHEME_MASK,
253			scheme << COMMANDII_SCHEME_SHIFT);
254	if (status) {
255		dev_err(chip->dev, "Error in setting operating mode\n");
256		return status;
257	}
258
259	prox_data = isl29018_read_sensor_input(chip,
260					COMMMAND1_OPMODE_PROX_ONCE);
261	if (prox_data < 0)
262		return prox_data;
263
264	if (scheme == 1) {
265		*near_ir = prox_data;
266		return 0;
267	}
268
269	ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
270
271	if (ir_data < 0)
272		return ir_data;
273
274	if (prox_data >= ir_data)
275		*near_ir = prox_data - ir_data;
276	else
277		*near_ir = 0;
278
279	return 0;
280}
281
282static ssize_t show_scale_available(struct device *dev,
283			struct device_attribute *attr, char *buf)
284{
285	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
286	struct isl29018_chip *chip = iio_priv(indio_dev);
287	int i, len = 0;
288
289	for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
290		len += sprintf(buf + len, "%d.%06d ",
291			       isl29018_scales[chip->int_time][i].scale,
292			       isl29018_scales[chip->int_time][i].uscale);
293
294	buf[len - 1] = '\n';
295
296	return len;
297}
298
299static ssize_t show_int_time_available(struct device *dev,
300			struct device_attribute *attr, char *buf)
301{
302	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
303	struct isl29018_chip *chip = iio_priv(indio_dev);
304	int i, len = 0;
305
306	for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
307		len += sprintf(buf + len, "0.%06d ",
308			       isl29018_int_utimes[chip->type][i]);
309
310	buf[len - 1] = '\n';
311
312	return len;
313}
314
315/* proximity scheme */
316static ssize_t show_prox_infrared_suppression(struct device *dev,
317			struct device_attribute *attr, char *buf)
318{
319	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
320	struct isl29018_chip *chip = iio_priv(indio_dev);
321
322	/* return the "proximity scheme" i.e. if the chip does on chip
323	infrared suppression (1 means perform on chip suppression) */
324	return sprintf(buf, "%d\n", chip->prox_scheme);
325}
326
327static ssize_t store_prox_infrared_suppression(struct device *dev,
328		struct device_attribute *attr, const char *buf, size_t count)
329{
330	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
331	struct isl29018_chip *chip = iio_priv(indio_dev);
332	int val;
333
334	if (kstrtoint(buf, 10, &val))
335		return -EINVAL;
336	if (!(val == 0 || val == 1)) {
337		dev_err(dev, "The mode is not supported\n");
338		return -EINVAL;
339	}
340
341	/* get the  "proximity scheme" i.e. if the chip does on chip
342	infrared suppression (1 means perform on chip suppression) */
343	mutex_lock(&chip->lock);
344	chip->prox_scheme = val;
345	mutex_unlock(&chip->lock);
346
347	return count;
348}
349
350/* Channel IO */
351static int isl29018_write_raw(struct iio_dev *indio_dev,
352			      struct iio_chan_spec const *chan,
353			      int val,
354			      int val2,
355			      long mask)
356{
357	struct isl29018_chip *chip = iio_priv(indio_dev);
358	int ret = -EINVAL;
359
360	mutex_lock(&chip->lock);
361	switch (mask) {
362	case IIO_CHAN_INFO_CALIBSCALE:
363		if (chan->type == IIO_LIGHT) {
364			chip->calibscale = val;
365			chip->ucalibscale = val2;
366			ret = 0;
367		}
368		break;
369	case IIO_CHAN_INFO_INT_TIME:
370		if (chan->type == IIO_LIGHT) {
371			if (val) {
372				mutex_unlock(&chip->lock);
373				return -EINVAL;
374			}
375			ret = isl29018_set_integration_time(chip, val2);
376		}
377		break;
378	case IIO_CHAN_INFO_SCALE:
379		if (chan->type == IIO_LIGHT)
380			ret = isl29018_set_scale(chip, val, val2);
381		break;
382	default:
383		break;
384	}
385	mutex_unlock(&chip->lock);
386
387	return ret;
388}
389
390static int isl29018_read_raw(struct iio_dev *indio_dev,
391			     struct iio_chan_spec const *chan,
392			     int *val,
393			     int *val2,
394			     long mask)
395{
396	int ret = -EINVAL;
397	struct isl29018_chip *chip = iio_priv(indio_dev);
398
399	mutex_lock(&chip->lock);
400	if (chip->suspended) {
401		mutex_unlock(&chip->lock);
402		return -EBUSY;
403	}
404	switch (mask) {
405	case IIO_CHAN_INFO_RAW:
406	case IIO_CHAN_INFO_PROCESSED:
407		switch (chan->type) {
408		case IIO_LIGHT:
409			ret = isl29018_read_lux(chip, val);
410			break;
411		case IIO_INTENSITY:
412			ret = isl29018_read_ir(chip, val);
413			break;
414		case IIO_PROXIMITY:
415			ret = isl29018_read_proximity_ir(chip,
416					chip->prox_scheme, val);
417			break;
418		default:
419			break;
420		}
421		if (!ret)
422			ret = IIO_VAL_INT;
423		break;
424	case IIO_CHAN_INFO_INT_TIME:
425		if (chan->type == IIO_LIGHT) {
426			*val = 0;
427			*val2 = isl29018_int_utimes[chip->type][chip->int_time];
428			ret = IIO_VAL_INT_PLUS_MICRO;
429		}
430		break;
431	case IIO_CHAN_INFO_SCALE:
432		if (chan->type == IIO_LIGHT) {
433			*val = chip->scale.scale;
434			*val2 = chip->scale.uscale;
435			ret = IIO_VAL_INT_PLUS_MICRO;
436		}
437		break;
438	case IIO_CHAN_INFO_CALIBSCALE:
439		if (chan->type == IIO_LIGHT) {
440			*val = chip->calibscale;
441			*val2 = chip->ucalibscale;
442			ret = IIO_VAL_INT_PLUS_MICRO;
443		}
444		break;
445	default:
446		break;
447	}
448	mutex_unlock(&chip->lock);
449	return ret;
450}
451
452#define ISL29018_LIGHT_CHANNEL {					\
453	.type = IIO_LIGHT,						\
454	.indexed = 1,							\
455	.channel = 0,							\
456	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |		\
457	BIT(IIO_CHAN_INFO_CALIBSCALE) |					\
458	BIT(IIO_CHAN_INFO_SCALE) |					\
459	BIT(IIO_CHAN_INFO_INT_TIME),					\
460}
461
462#define ISL29018_IR_CHANNEL {						\
463	.type = IIO_INTENSITY,						\
464	.modified = 1,							\
465	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
466	.channel2 = IIO_MOD_LIGHT_IR,					\
467}
468
469#define ISL29018_PROXIMITY_CHANNEL {					\
470	.type = IIO_PROXIMITY,						\
471	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
472}
473
474static const struct iio_chan_spec isl29018_channels[] = {
475	ISL29018_LIGHT_CHANNEL,
476	ISL29018_IR_CHANNEL,
477	ISL29018_PROXIMITY_CHANNEL,
478};
479
480static const struct iio_chan_spec isl29023_channels[] = {
481	ISL29018_LIGHT_CHANNEL,
482	ISL29018_IR_CHANNEL,
483};
484
485static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, S_IRUGO,
486		       show_int_time_available, NULL, 0);
487static IIO_DEVICE_ATTR(in_illuminance_scale_available, S_IRUGO,
488		      show_scale_available, NULL, 0);
489static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
490					S_IRUGO | S_IWUSR,
491					show_prox_infrared_suppression,
492					store_prox_infrared_suppression, 0);
493
494#define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
495
496static struct attribute *isl29018_attributes[] = {
497	ISL29018_DEV_ATTR(in_illuminance_scale_available),
498	ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
499	ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
500	NULL
501};
502
503static struct attribute *isl29023_attributes[] = {
504	ISL29018_DEV_ATTR(in_illuminance_scale_available),
505	ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
506	NULL
507};
508
509static const struct attribute_group isl29018_group = {
510	.attrs = isl29018_attributes,
511};
512
513static const struct attribute_group isl29023_group = {
514	.attrs = isl29023_attributes,
515};
516
517static int isl29035_detect(struct isl29018_chip *chip)
518{
519	int status;
520	unsigned int id;
521
522	status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
523	if (status < 0) {
524		dev_err(chip->dev,
525			"Error reading ID register with error %d\n",
526			status);
527		return status;
528	}
529
530	id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
531
532	if (id != ISL29035_DEVICE_ID)
533		return -ENODEV;
534
535	/* clear out brownout bit */
536	return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
537				  ISL29035_BOUT_MASK, 0);
538}
539
540enum {
541	isl29018,
542	isl29023,
543	isl29035,
544};
545
546static int isl29018_chip_init(struct isl29018_chip *chip)
547{
548	int status;
549
550	if (chip->type == isl29035) {
551		status = isl29035_detect(chip);
552		if (status < 0)
553			return status;
554	}
555
556	/* Code added per Intersil Application Note 1534:
557	 *     When VDD sinks to approximately 1.8V or below, some of
558	 * the part's registers may change their state. When VDD
559	 * recovers to 2.25V (or greater), the part may thus be in an
560	 * unknown mode of operation. The user can return the part to
561	 * a known mode of operation either by (a) setting VDD = 0V for
562	 * 1 second or more and then powering back up with a slew rate
563	 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
564	 * conversions, clear the test registers, and then rewrite all
565	 * registers to the desired values.
566	 * ...
567	 * FOR ISL29011, ISL29018, ISL29021, ISL29023
568	 * 1. Write 0x00 to register 0x08 (TEST)
569	 * 2. Write 0x00 to register 0x00 (CMD1)
570	 * 3. Rewrite all registers to the desired values
571	 *
572	 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
573	 * the same thing EXCEPT the data sheet asks for a 1ms delay after
574	 * writing the CMD1 register.
575	 */
576	status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
577	if (status < 0) {
578		dev_err(chip->dev, "Failed to clear isl29018 TEST reg.(%d)\n",
579			status);
580		return status;
581	}
582
583	/* See Intersil AN1534 comments above.
584	 * "Operating Mode" (COMMAND1) register is reprogrammed when
585	 * data is read from the device.
586	 */
587	status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
588	if (status < 0) {
589		dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
590			status);
591		return status;
592	}
593
594	usleep_range(1000, 2000);	/* per data sheet, page 10 */
595
596	/* set defaults */
597	status = isl29018_set_scale(chip, chip->scale.scale,
598				    chip->scale.uscale);
599	if (status < 0) {
600		dev_err(chip->dev, "Init of isl29018 fails\n");
601		return status;
602	}
603
604	status = isl29018_set_integration_time(chip,
605			isl29018_int_utimes[chip->type][chip->int_time]);
606	if (status < 0) {
607		dev_err(chip->dev, "Init of isl29018 fails\n");
608		return status;
609	}
610
611	return 0;
612}
613
614static const struct iio_info isl29018_info = {
615	.attrs = &isl29018_group,
616	.driver_module = THIS_MODULE,
617	.read_raw = &isl29018_read_raw,
618	.write_raw = &isl29018_write_raw,
619};
620
621static const struct iio_info isl29023_info = {
622	.attrs = &isl29023_group,
623	.driver_module = THIS_MODULE,
624	.read_raw = &isl29018_read_raw,
625	.write_raw = &isl29018_write_raw,
626};
627
628static bool is_volatile_reg(struct device *dev, unsigned int reg)
629{
630	switch (reg) {
631	case ISL29018_REG_ADD_DATA_LSB:
632	case ISL29018_REG_ADD_DATA_MSB:
633	case ISL29018_REG_ADD_COMMAND1:
634	case ISL29018_REG_TEST:
635	case ISL29035_REG_DEVICE_ID:
636		return true;
637	default:
638		return false;
639	}
640}
641
642/*
643 * isl29018_regmap_config: regmap configuration.
644 * Use RBTREE mechanism for caching.
645 */
646static const struct regmap_config isl29018_regmap_config = {
647	.reg_bits = 8,
648	.val_bits = 8,
649	.volatile_reg = is_volatile_reg,
650	.max_register = ISL29018_REG_TEST,
651	.num_reg_defaults_raw = ISL29018_REG_TEST + 1,
652	.cache_type = REGCACHE_RBTREE,
653};
654
655/* isl29035_regmap_config: regmap configuration for ISL29035 */
656static const struct regmap_config isl29035_regmap_config = {
657	.reg_bits = 8,
658	.val_bits = 8,
659	.volatile_reg = is_volatile_reg,
660	.max_register = ISL29035_REG_DEVICE_ID,
661	.num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
662	.cache_type = REGCACHE_RBTREE,
663};
664
665struct chip_info {
666	const struct iio_chan_spec *channels;
667	int num_channels;
668	const struct iio_info *indio_info;
669	const struct regmap_config *regmap_cfg;
670};
671
672static const struct chip_info chip_info_tbl[] = {
673	[isl29018] = {
674		.channels = isl29018_channels,
675		.num_channels = ARRAY_SIZE(isl29018_channels),
676		.indio_info = &isl29018_info,
677		.regmap_cfg = &isl29018_regmap_config,
678	},
679	[isl29023] = {
680		.channels = isl29023_channels,
681		.num_channels = ARRAY_SIZE(isl29023_channels),
682		.indio_info = &isl29023_info,
683		.regmap_cfg = &isl29018_regmap_config,
684	},
685	[isl29035] = {
686		.channels = isl29023_channels,
687		.num_channels = ARRAY_SIZE(isl29023_channels),
688		.indio_info = &isl29023_info,
689		.regmap_cfg = &isl29035_regmap_config,
690	},
691};
692
693static const char *isl29018_match_acpi_device(struct device *dev, int *data)
694{
695	const struct acpi_device_id *id;
696
697	id = acpi_match_device(dev->driver->acpi_match_table, dev);
698
699	if (!id)
700		return NULL;
701
702	*data = (int) id->driver_data;
703
704	return dev_name(dev);
705}
706
707static int isl29018_probe(struct i2c_client *client,
708			 const struct i2c_device_id *id)
709{
710	struct isl29018_chip *chip;
711	struct iio_dev *indio_dev;
712	int err;
713	const char *name = NULL;
714	int dev_id = 0;
715
716	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
717	if (!indio_dev) {
718		dev_err(&client->dev, "iio allocation fails\n");
719		return -ENOMEM;
720	}
721	chip = iio_priv(indio_dev);
722
723	i2c_set_clientdata(client, indio_dev);
724	chip->dev = &client->dev;
725
726	if (id) {
727		name = id->name;
728		dev_id = id->driver_data;
729	}
730
731	if (ACPI_HANDLE(&client->dev))
732		name = isl29018_match_acpi_device(&client->dev, &dev_id);
733
734	mutex_init(&chip->lock);
735
736	chip->type = dev_id;
737	chip->calibscale = 1;
738	chip->ucalibscale = 0;
739	chip->int_time = ISL29018_INT_TIME_16;
740	chip->scale = isl29018_scales[chip->int_time][0];
741	chip->suspended = false;
742
743	chip->regmap = devm_regmap_init_i2c(client,
744				chip_info_tbl[dev_id].regmap_cfg);
745	if (IS_ERR(chip->regmap)) {
746		err = PTR_ERR(chip->regmap);
747		dev_err(chip->dev, "regmap initialization failed: %d\n", err);
748		return err;
749	}
750
751	err = isl29018_chip_init(chip);
752	if (err)
753		return err;
754
755	indio_dev->info = chip_info_tbl[dev_id].indio_info;
756	indio_dev->channels = chip_info_tbl[dev_id].channels;
757	indio_dev->num_channels = chip_info_tbl[dev_id].num_channels;
758	indio_dev->name = name;
759	indio_dev->dev.parent = &client->dev;
760	indio_dev->modes = INDIO_DIRECT_MODE;
761	err = devm_iio_device_register(&client->dev, indio_dev);
762	if (err) {
763		dev_err(&client->dev, "iio registration fails\n");
764		return err;
765	}
766
767	return 0;
768}
769
770#ifdef CONFIG_PM_SLEEP
771static int isl29018_suspend(struct device *dev)
772{
773	struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
774
775	mutex_lock(&chip->lock);
776
777	/* Since this driver uses only polling commands, we are by default in
778	 * auto shutdown (ie, power-down) mode.
779	 * So we do not have much to do here.
780	 */
781	chip->suspended = true;
782
783	mutex_unlock(&chip->lock);
784	return 0;
785}
786
787static int isl29018_resume(struct device *dev)
788{
789	struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
790	int err;
791
792	mutex_lock(&chip->lock);
793
794	err = isl29018_chip_init(chip);
795	if (!err)
796		chip->suspended = false;
797
798	mutex_unlock(&chip->lock);
799	return err;
800}
801
802static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
803#define ISL29018_PM_OPS (&isl29018_pm_ops)
804#else
805#define ISL29018_PM_OPS NULL
806#endif
807
808static const struct acpi_device_id isl29018_acpi_match[] = {
809	{"ISL29018", isl29018},
810	{"ISL29023", isl29023},
811	{"ISL29035", isl29035},
812	{},
813};
814MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
815
816static const struct i2c_device_id isl29018_id[] = {
817	{"isl29018", isl29018},
818	{"isl29023", isl29023},
819	{"isl29035", isl29035},
820	{}
821};
822
823MODULE_DEVICE_TABLE(i2c, isl29018_id);
824
825static const struct of_device_id isl29018_of_match[] = {
826	{ .compatible = "isil,isl29018", },
827	{ .compatible = "isil,isl29023", },
828	{ .compatible = "isil,isl29035", },
829	{ },
830};
831MODULE_DEVICE_TABLE(of, isl29018_of_match);
832
833static struct i2c_driver isl29018_driver = {
834	.class	= I2C_CLASS_HWMON,
835	.driver	 = {
836			.name = "isl29018",
837			.acpi_match_table = ACPI_PTR(isl29018_acpi_match),
838			.pm = ISL29018_PM_OPS,
839			.of_match_table = isl29018_of_match,
840		    },
841	.probe	 = isl29018_probe,
842	.id_table = isl29018_id,
843};
844module_i2c_driver(isl29018_driver);
845
846MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
847MODULE_LICENSE("GPL");
848