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