xref: /openbmc/linux/drivers/iio/light/vcnl4035.c (revision 66c98360)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
4  *
5  * Copyright (c) 2018, DENX Software Engineering GmbH
6  * Author: Parthiban Nallathambi <pn@denx.de>
7  *
8  * TODO: Proximity
9  */
10 #include <linux/bitops.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/events.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
23 
24 #define VCNL4035_DRV_NAME	"vcnl4035"
25 #define VCNL4035_IRQ_NAME	"vcnl4035_event"
26 #define VCNL4035_REGMAP_NAME	"vcnl4035_regmap"
27 
28 /* Device registers */
29 #define VCNL4035_ALS_CONF	0x00
30 #define VCNL4035_ALS_THDH	0x01
31 #define VCNL4035_ALS_THDL	0x02
32 #define VCNL4035_ALS_DATA	0x0B
33 #define VCNL4035_WHITE_DATA	0x0C
34 #define VCNL4035_INT_FLAG	0x0D
35 #define VCNL4035_DEV_ID		0x0E
36 
37 /* Register masks */
38 #define VCNL4035_MODE_ALS_MASK		BIT(0)
39 #define VCNL4035_MODE_ALS_WHITE_CHAN	BIT(8)
40 #define VCNL4035_MODE_ALS_INT_MASK	BIT(1)
41 #define VCNL4035_ALS_IT_MASK		GENMASK(7, 5)
42 #define VCNL4035_ALS_PERS_MASK		GENMASK(3, 2)
43 #define VCNL4035_INT_ALS_IF_H_MASK	BIT(12)
44 #define VCNL4035_INT_ALS_IF_L_MASK	BIT(13)
45 
46 /* Default values */
47 #define VCNL4035_MODE_ALS_ENABLE	BIT(0)
48 #define VCNL4035_MODE_ALS_DISABLE	0x00
49 #define VCNL4035_MODE_ALS_INT_ENABLE	BIT(1)
50 #define VCNL4035_MODE_ALS_INT_DISABLE	0
51 #define VCNL4035_DEV_ID_VAL		0x80
52 #define VCNL4035_ALS_IT_DEFAULT		0x01
53 #define VCNL4035_ALS_PERS_DEFAULT	0x00
54 #define VCNL4035_ALS_THDH_DEFAULT	5000
55 #define VCNL4035_ALS_THDL_DEFAULT	100
56 #define VCNL4035_SLEEP_DELAY_MS		2000
57 
58 struct vcnl4035_data {
59 	struct i2c_client *client;
60 	struct regmap *regmap;
61 	unsigned int als_it_val;
62 	unsigned int als_persistence;
63 	unsigned int als_thresh_low;
64 	unsigned int als_thresh_high;
65 	struct iio_trigger *drdy_trigger0;
66 };
67 
68 static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data)
69 {
70 	int ret;
71 	int reg;
72 
73 	ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, &reg);
74 	if (ret < 0)
75 		return false;
76 
77 	return !!(reg &
78 		(VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK));
79 }
80 
81 static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private)
82 {
83 	struct iio_dev *indio_dev = private;
84 	struct vcnl4035_data *data = iio_priv(indio_dev);
85 
86 	if (vcnl4035_is_triggered(data)) {
87 		iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
88 							0,
89 							IIO_EV_TYPE_THRESH,
90 							IIO_EV_DIR_EITHER),
91 				iio_get_time_ns(indio_dev));
92 		iio_trigger_poll_nested(data->drdy_trigger0);
93 		return IRQ_HANDLED;
94 	}
95 
96 	return IRQ_NONE;
97 }
98 
99 /* Triggered buffer */
100 static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
101 {
102 	struct iio_poll_func *pf = p;
103 	struct iio_dev *indio_dev = pf->indio_dev;
104 	struct vcnl4035_data *data = iio_priv(indio_dev);
105 	/* Ensure naturally aligned timestamp */
106 	u8 buffer[ALIGN(sizeof(u16), sizeof(s64)) + sizeof(s64)]  __aligned(8);
107 	int ret;
108 
109 	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, (int *)buffer);
110 	if (ret < 0) {
111 		dev_err(&data->client->dev,
112 			"Trigger consumer can't read from sensor.\n");
113 		goto fail_read;
114 	}
115 	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
116 					iio_get_time_ns(indio_dev));
117 
118 fail_read:
119 	iio_trigger_notify_done(indio_dev->trig);
120 
121 	return IRQ_HANDLED;
122 }
123 
124 static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger,
125 					bool enable_drdy)
126 {
127 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
128 	struct vcnl4035_data *data = iio_priv(indio_dev);
129 	int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE :
130 					VCNL4035_MODE_ALS_INT_DISABLE;
131 
132 	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
133 				 VCNL4035_MODE_ALS_INT_MASK,
134 				 val);
135 }
136 
137 static const struct iio_trigger_ops vcnl4035_trigger_ops = {
138 	.validate_device = iio_trigger_validate_own_device,
139 	.set_trigger_state = vcnl4035_als_drdy_set_state,
140 };
141 
142 static int vcnl4035_set_pm_runtime_state(struct vcnl4035_data *data, bool on)
143 {
144 	int ret;
145 	struct device *dev = &data->client->dev;
146 
147 	if (on) {
148 		ret = pm_runtime_resume_and_get(dev);
149 	} else {
150 		pm_runtime_mark_last_busy(dev);
151 		ret = pm_runtime_put_autosuspend(dev);
152 	}
153 
154 	return ret;
155 }
156 
157 /*
158  *	Device IT	INT Time (ms)	Scale (lux/step)
159  *	000		50		0.064
160  *	001		100		0.032
161  *	010		200		0.016
162  *	100		400		0.008
163  *	101 - 111	800		0.004
164  * Values are proportional, so ALS INT is selected for input due to
165  * simplicity reason. Integration time value and scaling is
166  * calculated based on device INT value
167  *
168  * Raw value needs to be scaled using ALS steps
169  */
170 static int vcnl4035_read_raw(struct iio_dev *indio_dev,
171 			    struct iio_chan_spec const *chan, int *val,
172 			    int *val2, long mask)
173 {
174 	struct vcnl4035_data *data = iio_priv(indio_dev);
175 	int ret;
176 	int raw_data;
177 	unsigned int reg;
178 
179 	switch (mask) {
180 	case IIO_CHAN_INFO_RAW:
181 		ret = vcnl4035_set_pm_runtime_state(data, true);
182 		if  (ret < 0)
183 			return ret;
184 
185 		ret = iio_device_claim_direct_mode(indio_dev);
186 		if (!ret) {
187 			if (chan->channel)
188 				reg = VCNL4035_ALS_DATA;
189 			else
190 				reg = VCNL4035_WHITE_DATA;
191 			ret = regmap_read(data->regmap, reg, &raw_data);
192 			iio_device_release_direct_mode(indio_dev);
193 			if (!ret) {
194 				*val = raw_data;
195 				ret = IIO_VAL_INT;
196 			}
197 		}
198 		vcnl4035_set_pm_runtime_state(data, false);
199 		return ret;
200 	case IIO_CHAN_INFO_INT_TIME:
201 		*val = 50;
202 		if (data->als_it_val)
203 			*val = data->als_it_val * 100;
204 		return IIO_VAL_INT;
205 	case IIO_CHAN_INFO_SCALE:
206 		*val = 64;
207 		if (!data->als_it_val)
208 			*val2 = 1000;
209 		else
210 			*val2 = data->als_it_val * 2 * 1000;
211 		return IIO_VAL_FRACTIONAL;
212 	default:
213 		return -EINVAL;
214 	}
215 }
216 
217 static int vcnl4035_write_raw(struct iio_dev *indio_dev,
218 				struct iio_chan_spec const *chan,
219 				int val, int val2, long mask)
220 {
221 	int ret;
222 	struct vcnl4035_data *data = iio_priv(indio_dev);
223 
224 	switch (mask) {
225 	case IIO_CHAN_INFO_INT_TIME:
226 		if (val <= 0 || val > 800)
227 			return -EINVAL;
228 
229 		ret = vcnl4035_set_pm_runtime_state(data, true);
230 		if  (ret < 0)
231 			return ret;
232 
233 		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
234 					 VCNL4035_ALS_IT_MASK,
235 					 val / 100);
236 		if (!ret)
237 			data->als_it_val = val / 100;
238 
239 		vcnl4035_set_pm_runtime_state(data, false);
240 		return ret;
241 	default:
242 		return -EINVAL;
243 	}
244 }
245 
246 /* No direct ABI for persistence and threshold, so eventing */
247 static int vcnl4035_read_thresh(struct iio_dev *indio_dev,
248 		const struct iio_chan_spec *chan, enum iio_event_type type,
249 		enum iio_event_direction dir, enum iio_event_info info,
250 		int *val, int *val2)
251 {
252 	struct vcnl4035_data *data = iio_priv(indio_dev);
253 
254 	switch (info) {
255 	case IIO_EV_INFO_VALUE:
256 		switch (dir) {
257 		case IIO_EV_DIR_RISING:
258 			*val = data->als_thresh_high;
259 			return IIO_VAL_INT;
260 		case IIO_EV_DIR_FALLING:
261 			*val = data->als_thresh_low;
262 			return IIO_VAL_INT;
263 		default:
264 			return -EINVAL;
265 		}
266 		break;
267 	case IIO_EV_INFO_PERIOD:
268 		*val = data->als_persistence;
269 		return IIO_VAL_INT;
270 	default:
271 		return -EINVAL;
272 	}
273 
274 }
275 
276 static int vcnl4035_write_thresh(struct iio_dev *indio_dev,
277 		const struct iio_chan_spec *chan, enum iio_event_type type,
278 		enum iio_event_direction dir, enum iio_event_info info, int val,
279 		int val2)
280 {
281 	struct vcnl4035_data *data = iio_priv(indio_dev);
282 	int ret;
283 
284 	switch (info) {
285 	case IIO_EV_INFO_VALUE:
286 		/* 16 bit threshold range 0 - 65535 */
287 		if (val < 0 || val > 65535)
288 			return -EINVAL;
289 		if (dir == IIO_EV_DIR_RISING) {
290 			if (val < data->als_thresh_low)
291 				return -EINVAL;
292 			ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
293 					   val);
294 			if (ret)
295 				return ret;
296 			data->als_thresh_high = val;
297 		} else {
298 			if (val > data->als_thresh_high)
299 				return -EINVAL;
300 			ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
301 					   val);
302 			if (ret)
303 				return ret;
304 			data->als_thresh_low = val;
305 		}
306 		return ret;
307 	case IIO_EV_INFO_PERIOD:
308 		/* allow only 1 2 4 8 as persistence value */
309 		if (val < 0 || val > 8 || hweight8(val) != 1)
310 			return -EINVAL;
311 		ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
312 					 VCNL4035_ALS_PERS_MASK, val);
313 		if (!ret)
314 			data->als_persistence = val;
315 		return ret;
316 	default:
317 		return -EINVAL;
318 	}
319 }
320 
321 static IIO_CONST_ATTR_INT_TIME_AVAIL("50 100 200 400 800");
322 
323 static struct attribute *vcnl4035_attributes[] = {
324 	&iio_const_attr_integration_time_available.dev_attr.attr,
325 	NULL,
326 };
327 
328 static const struct attribute_group vcnl4035_attribute_group = {
329 	.attrs = vcnl4035_attributes,
330 };
331 
332 static const struct iio_info vcnl4035_info = {
333 	.read_raw		= vcnl4035_read_raw,
334 	.write_raw		= vcnl4035_write_raw,
335 	.read_event_value	= vcnl4035_read_thresh,
336 	.write_event_value	= vcnl4035_write_thresh,
337 	.attrs			= &vcnl4035_attribute_group,
338 };
339 
340 static const struct iio_event_spec vcnl4035_event_spec[] = {
341 	{
342 		.type = IIO_EV_TYPE_THRESH,
343 		.dir = IIO_EV_DIR_RISING,
344 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
345 	}, {
346 		.type = IIO_EV_TYPE_THRESH,
347 		.dir = IIO_EV_DIR_FALLING,
348 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
349 	}, {
350 		.type = IIO_EV_TYPE_THRESH,
351 		.dir = IIO_EV_DIR_EITHER,
352 		.mask_separate = BIT(IIO_EV_INFO_PERIOD),
353 	},
354 };
355 
356 enum vcnl4035_scan_index_order {
357 	VCNL4035_CHAN_INDEX_LIGHT,
358 	VCNL4035_CHAN_INDEX_WHITE_LED,
359 };
360 
361 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
362 	.validate_scan_mask = &iio_validate_scan_mask_onehot,
363 };
364 
365 static const struct iio_chan_spec vcnl4035_channels[] = {
366 	{
367 		.type = IIO_LIGHT,
368 		.channel = 0,
369 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
370 				BIT(IIO_CHAN_INFO_INT_TIME) |
371 				BIT(IIO_CHAN_INFO_SCALE),
372 		.event_spec = vcnl4035_event_spec,
373 		.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec),
374 		.scan_index = VCNL4035_CHAN_INDEX_LIGHT,
375 		.scan_type = {
376 			.sign = 'u',
377 			.realbits = 16,
378 			.storagebits = 16,
379 			.endianness = IIO_LE,
380 		},
381 	},
382 	{
383 		.type = IIO_INTENSITY,
384 		.channel = 1,
385 		.modified = 1,
386 		.channel2 = IIO_MOD_LIGHT_BOTH,
387 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
388 		.scan_index = VCNL4035_CHAN_INDEX_WHITE_LED,
389 		.scan_type = {
390 			.sign = 'u',
391 			.realbits = 16,
392 			.storagebits = 16,
393 			.endianness = IIO_LE,
394 		},
395 	},
396 };
397 
398 static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status)
399 {
400 	return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
401 					VCNL4035_MODE_ALS_MASK,
402 					status);
403 }
404 
405 static int vcnl4035_init(struct vcnl4035_data *data)
406 {
407 	int ret;
408 	int id;
409 
410 	ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id);
411 	if (ret < 0) {
412 		dev_err(&data->client->dev, "Failed to read DEV_ID register\n");
413 		return ret;
414 	}
415 
416 	if (id != VCNL4035_DEV_ID_VAL) {
417 		dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
418 			id, VCNL4035_DEV_ID_VAL);
419 		return -ENODEV;
420 	}
421 
422 	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
423 	if (ret < 0)
424 		return ret;
425 
426 	/* ALS white channel enable */
427 	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
428 				 VCNL4035_MODE_ALS_WHITE_CHAN,
429 				 1);
430 	if (ret) {
431 		dev_err(&data->client->dev, "set white channel enable %d\n",
432 			ret);
433 		return ret;
434 	}
435 
436 	/* set default integration time - 100 ms for ALS */
437 	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
438 				 VCNL4035_ALS_IT_MASK,
439 				 VCNL4035_ALS_IT_DEFAULT);
440 	if (ret) {
441 		dev_err(&data->client->dev, "set default ALS IT returned %d\n",
442 			ret);
443 		return ret;
444 	}
445 	data->als_it_val = VCNL4035_ALS_IT_DEFAULT;
446 
447 	/* set default persistence time - 1 for ALS */
448 	ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF,
449 				 VCNL4035_ALS_PERS_MASK,
450 				 VCNL4035_ALS_PERS_DEFAULT);
451 	if (ret) {
452 		dev_err(&data->client->dev, "set default PERS returned %d\n",
453 			ret);
454 		return ret;
455 	}
456 	data->als_persistence = VCNL4035_ALS_PERS_DEFAULT;
457 
458 	/* set default HIGH threshold for ALS */
459 	ret = regmap_write(data->regmap, VCNL4035_ALS_THDH,
460 				VCNL4035_ALS_THDH_DEFAULT);
461 	if (ret) {
462 		dev_err(&data->client->dev, "set default THDH returned %d\n",
463 			ret);
464 		return ret;
465 	}
466 	data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT;
467 
468 	/* set default LOW threshold for ALS */
469 	ret = regmap_write(data->regmap, VCNL4035_ALS_THDL,
470 				VCNL4035_ALS_THDL_DEFAULT);
471 	if (ret) {
472 		dev_err(&data->client->dev, "set default THDL returned %d\n",
473 			ret);
474 		return ret;
475 	}
476 	data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT;
477 
478 	return 0;
479 }
480 
481 static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg)
482 {
483 	switch (reg) {
484 	case VCNL4035_ALS_CONF:
485 	case VCNL4035_DEV_ID:
486 		return false;
487 	default:
488 		return true;
489 	}
490 }
491 
492 static const struct regmap_config vcnl4035_regmap_config = {
493 	.name		= VCNL4035_REGMAP_NAME,
494 	.reg_bits	= 8,
495 	.val_bits	= 16,
496 	.max_register	= VCNL4035_DEV_ID,
497 	.cache_type	= REGCACHE_RBTREE,
498 	.volatile_reg	= vcnl4035_is_volatile_reg,
499 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
500 };
501 
502 static int vcnl4035_probe_trigger(struct iio_dev *indio_dev)
503 {
504 	int ret;
505 	struct vcnl4035_data *data = iio_priv(indio_dev);
506 
507 	data->drdy_trigger0 = devm_iio_trigger_alloc(
508 			indio_dev->dev.parent,
509 			"%s-dev%d", indio_dev->name, iio_device_id(indio_dev));
510 	if (!data->drdy_trigger0)
511 		return -ENOMEM;
512 
513 	data->drdy_trigger0->ops = &vcnl4035_trigger_ops;
514 	iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
515 	ret = devm_iio_trigger_register(indio_dev->dev.parent,
516 					data->drdy_trigger0);
517 	if (ret) {
518 		dev_err(&data->client->dev, "iio trigger register failed\n");
519 		return ret;
520 	}
521 
522 	/* Trigger setup */
523 	ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent, indio_dev,
524 					NULL, vcnl4035_trigger_consumer_handler,
525 					&iio_triggered_buffer_setup_ops);
526 	if (ret < 0) {
527 		dev_err(&data->client->dev, "iio triggered buffer setup failed\n");
528 		return ret;
529 	}
530 
531 	/* IRQ to trigger mapping */
532 	ret = devm_request_threaded_irq(&data->client->dev, data->client->irq,
533 			NULL, vcnl4035_drdy_irq_thread,
534 			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
535 			VCNL4035_IRQ_NAME, indio_dev);
536 	if (ret < 0)
537 		dev_err(&data->client->dev, "request irq %d for trigger0 failed\n",
538 				data->client->irq);
539 	return ret;
540 }
541 
542 static int vcnl4035_probe(struct i2c_client *client)
543 {
544 	struct vcnl4035_data *data;
545 	struct iio_dev *indio_dev;
546 	struct regmap *regmap;
547 	int ret;
548 
549 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
550 	if (!indio_dev)
551 		return -ENOMEM;
552 
553 	regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config);
554 	if (IS_ERR(regmap)) {
555 		dev_err(&client->dev, "regmap_init failed!\n");
556 		return PTR_ERR(regmap);
557 	}
558 
559 	data = iio_priv(indio_dev);
560 	i2c_set_clientdata(client, indio_dev);
561 	data->client = client;
562 	data->regmap = regmap;
563 
564 	indio_dev->info = &vcnl4035_info;
565 	indio_dev->name = VCNL4035_DRV_NAME;
566 	indio_dev->channels = vcnl4035_channels;
567 	indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels);
568 	indio_dev->modes = INDIO_DIRECT_MODE;
569 
570 	ret = vcnl4035_init(data);
571 	if (ret < 0) {
572 		dev_err(&client->dev, "vcnl4035 chip init failed\n");
573 		return ret;
574 	}
575 
576 	if (client->irq > 0) {
577 		ret = vcnl4035_probe_trigger(indio_dev);
578 		if (ret < 0) {
579 			dev_err(&client->dev, "vcnl4035 unable init trigger\n");
580 			goto fail_poweroff;
581 		}
582 	}
583 
584 	ret = pm_runtime_set_active(&client->dev);
585 	if (ret < 0)
586 		goto fail_poweroff;
587 
588 	ret = iio_device_register(indio_dev);
589 	if (ret < 0)
590 		goto fail_poweroff;
591 
592 	pm_runtime_enable(&client->dev);
593 	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS);
594 	pm_runtime_use_autosuspend(&client->dev);
595 
596 	return 0;
597 
598 fail_poweroff:
599 	vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
600 	return ret;
601 }
602 
603 static void vcnl4035_remove(struct i2c_client *client)
604 {
605 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
606 	int ret;
607 
608 	pm_runtime_dont_use_autosuspend(&client->dev);
609 	pm_runtime_disable(&client->dev);
610 	iio_device_unregister(indio_dev);
611 	pm_runtime_set_suspended(&client->dev);
612 
613 	ret = vcnl4035_set_als_power_state(iio_priv(indio_dev),
614 					   VCNL4035_MODE_ALS_DISABLE);
615 	if (ret)
616 		dev_warn(&client->dev, "Failed to put device into standby (%pe)\n",
617 			 ERR_PTR(ret));
618 }
619 
620 static int vcnl4035_runtime_suspend(struct device *dev)
621 {
622 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
623 	struct vcnl4035_data *data = iio_priv(indio_dev);
624 	int ret;
625 
626 	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE);
627 	regcache_mark_dirty(data->regmap);
628 
629 	return ret;
630 }
631 
632 static int vcnl4035_runtime_resume(struct device *dev)
633 {
634 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
635 	struct vcnl4035_data *data = iio_priv(indio_dev);
636 	int ret;
637 
638 	regcache_sync(data->regmap);
639 	ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE);
640 	if (ret < 0)
641 		return ret;
642 
643 	/* wait for 1 ALS integration cycle */
644 	msleep(data->als_it_val * 100);
645 
646 	return 0;
647 }
648 
649 static DEFINE_RUNTIME_DEV_PM_OPS(vcnl4035_pm_ops, vcnl4035_runtime_suspend,
650 				 vcnl4035_runtime_resume, NULL);
651 
652 static const struct i2c_device_id vcnl4035_id[] = {
653 	{ "vcnl4035", 0 },
654 	{ }
655 };
656 MODULE_DEVICE_TABLE(i2c, vcnl4035_id);
657 
658 static const struct of_device_id vcnl4035_of_match[] = {
659 	{ .compatible = "vishay,vcnl4035", },
660 	{ }
661 };
662 MODULE_DEVICE_TABLE(of, vcnl4035_of_match);
663 
664 static struct i2c_driver vcnl4035_driver = {
665 	.driver = {
666 		.name   = VCNL4035_DRV_NAME,
667 		.pm	= pm_ptr(&vcnl4035_pm_ops),
668 		.of_match_table = vcnl4035_of_match,
669 	},
670 	.probe_new = vcnl4035_probe,
671 	.remove	= vcnl4035_remove,
672 	.id_table = vcnl4035_id,
673 };
674 
675 module_i2c_driver(vcnl4035_driver);
676 
677 MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
678 MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
679 MODULE_LICENSE("GPL v2");
680