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