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