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