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