xref: /linux/drivers/iio/light/gp2ap002.c (revision ca64d84e93762f4e587e040a44ad9f6089afc777)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * These are the two Sharp GP2AP002 variants supported by this driver:
4  * GP2AP002A00F Ambient Light and Proximity Sensor
5  * GP2AP002S00F Proximity Sensor
6  *
7  * Copyright (C) 2020 Linaro Ltd.
8  * Author: Linus Walleij <linus.walleij@linaro.org>
9  *
10  * Based partly on the code in Sony Ericssons GP2AP00200F driver by
11  * Courtney Cavin and Oskar Andero in drivers/input/misc/gp2ap002a00f.c
12  * Based partly on a Samsung misc driver submitted by
13  * Donggeun Kim & Minkyu Kang in 2011:
14  * https://lore.kernel.org/lkml/1315556546-7445-1-git-send-email-dg77.kim@samsung.com/
15  * Based partly on a submission by
16  * Jonathan Bakker and Paweł Chmiel in january 2019:
17  * https://lore.kernel.org/linux-input/20190125175045.22576-1-pawel.mikolaj.chmiel@gmail.com/
18  * Based partly on code from the Samsung GT-S7710 by <mjchen@sta.samsung.com>
19  * Based partly on the code in LG Electronics GP2AP00200F driver by
20  * Kenobi Lee <sungyoung.lee@lge.com> and EunYoung Cho <ey.cho@lge.com>
21  */
22 #include <linux/module.h>
23 #include <linux/i2c.h>
24 #include <linux/regmap.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/events.h>
28 #include <linux/iio/consumer.h> /* To get our ADC channel */
29 #include <linux/iio/types.h> /* To deal with our ADC channel */
30 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/interrupt.h>
35 #include <linux/bits.h>
36 #include <linux/math64.h>
37 #include <linux/pm.h>
38 
39 #define GP2AP002_PROX_CHANNEL 0
40 #define GP2AP002_ALS_CHANNEL 1
41 
42 /* ------------------------------------------------------------------------ */
43 /* ADDRESS SYMBOL             DATA                                 Init R/W */
44 /*                   D7    D6    D5    D4    D3    D2    D1    D0           */
45 /* ------------------------------------------------------------------------ */
46 /*    0      PROX     X     X     X     X     X     X     X    VO  H'00   R */
47 /*    1      GAIN     X     X     X     X  LED0     X     X     X  H'00   W */
48 /*    2       HYS  HYSD HYSC1 HYSC0     X HYSF3 HYSF2 HYSF1 HYSF0  H'00   W */
49 /*    3     CYCLE     X     X CYCL2 CYCL1 CYCL0  OSC2     X     X  H'00   W */
50 /*    4     OPMOD     X     X     X   ASD     X     X  VCON   SSD  H'00   W */
51 /*    6       CON     X     X     X OCON1 OCON0     X     X     X  H'00   W */
52 /* ------------------------------------------------------------------------ */
53 /* VO   :Proximity sensing result(0: no detection, 1: detection)            */
54 /* LED0 :Select switch for LED driver's On-registence(0:2x higher, 1:normal)*/
55 /* HYSD/HYSF :Adjusts the receiver sensitivity                              */
56 /* OSC  :Select switch internal clocl frequency hoppling(0:effective)       */
57 /* CYCL :Determine the detection cycle(typically 8ms, up to 128x)           */
58 /* SSD  :Software Shutdown function(0:shutdown, 1:operating)                */
59 /* VCON :VOUT output method control(0:normal, 1:interrupt)                  */
60 /* ASD  :Select switch for analog sleep function(0:ineffective, 1:effective)*/
61 /* OCON :Select switch for enabling/disabling VOUT (00:enable, 11:disable)  */
62 
63 #define GP2AP002_PROX				0x00
64 #define GP2AP002_GAIN				0x01
65 #define GP2AP002_HYS				0x02
66 #define GP2AP002_CYCLE				0x03
67 #define GP2AP002_OPMOD				0x04
68 #define GP2AP002_CON				0x06
69 
70 #define GP2AP002_PROX_VO_DETECT			BIT(0)
71 
72 /* Setting this bit to 0 means 2x higher LED resistance */
73 #define GP2AP002_GAIN_LED_NORMAL		BIT(3)
74 
75 /*
76  * These bits adjusts the proximity sensitivity, determining characteristics
77  * of the detection distance and its hysteresis.
78  */
79 #define GP2AP002_HYS_HYSD_SHIFT		7
80 #define GP2AP002_HYS_HYSD_MASK		BIT(7)
81 #define GP2AP002_HYS_HYSC_SHIFT		5
82 #define GP2AP002_HYS_HYSC_MASK		GENMASK(6, 5)
83 #define GP2AP002_HYS_HYSF_SHIFT		0
84 #define GP2AP002_HYS_HYSF_MASK		GENMASK(3, 0)
85 #define GP2AP002_HYS_MASK		(GP2AP002_HYS_HYSD_MASK | \
86 					 GP2AP002_HYS_HYSC_MASK | \
87 					 GP2AP002_HYS_HYSF_MASK)
88 
89 /*
90  * These values determine the detection cycle response time
91  * 0: 8ms, 1: 16ms, 2: 32ms, 3: 64ms, 4: 128ms,
92  * 5: 256ms, 6: 512ms, 7: 1024ms
93  */
94 #define GP2AP002_CYCLE_CYCL_SHIFT	3
95 #define GP2AP002_CYCLE_CYCL_MASK	GENMASK(5, 3)
96 
97 /*
98  * Select switch for internal clock frequency hopping
99  *	0: effective,
100  *	1: ineffective
101  */
102 #define GP2AP002_CYCLE_OSC_EFFECTIVE	0
103 #define GP2AP002_CYCLE_OSC_INEFFECTIVE	BIT(2)
104 #define GP2AP002_CYCLE_OSC_MASK		BIT(2)
105 
106 /* Analog sleep effective */
107 #define GP2AP002_OPMOD_ASD		BIT(4)
108 /* Enable chip */
109 #define GP2AP002_OPMOD_SSD_OPERATING	BIT(0)
110 /* IRQ mode */
111 #define GP2AP002_OPMOD_VCON_IRQ		BIT(1)
112 #define GP2AP002_OPMOD_MASK		(BIT(0) | BIT(1) | BIT(4))
113 
114 /*
115  * Select switch for enabling/disabling Vout pin
116  * 0: enable
117  * 2: force to go Low
118  * 3: force to go High
119  */
120 #define GP2AP002_CON_OCON_SHIFT		3
121 #define GP2AP002_CON_OCON_ENABLE	(0x0 << GP2AP002_CON_OCON_SHIFT)
122 #define GP2AP002_CON_OCON_LOW		(0x2 << GP2AP002_CON_OCON_SHIFT)
123 #define GP2AP002_CON_OCON_HIGH		(0x3 << GP2AP002_CON_OCON_SHIFT)
124 #define GP2AP002_CON_OCON_MASK		(0x3 << GP2AP002_CON_OCON_SHIFT)
125 
126 /**
127  * struct gp2ap002 - GP2AP002 state
128  * @map: regmap pointer for the i2c regmap
129  * @dev: pointer to parent device
130  * @vdd: regulator controlling VDD
131  * @vio: regulator controlling VIO
132  * @alsout: IIO ADC channel to convert the ALSOUT signal
133  * @hys_far: hysteresis control from device tree
134  * @hys_close: hysteresis control from device tree
135  * @is_gp2ap002s00f: this is the GP2AP002F variant of the chip
136  * @irq: the IRQ line used by this device
137  * @enabled: we cannot read the status of the hardware so we need to
138  * keep track of whether the event is enabled using this state variable
139  */
140 struct gp2ap002 {
141 	struct regmap *map;
142 	struct device *dev;
143 	struct regulator *vdd;
144 	struct regulator *vio;
145 	struct iio_channel *alsout;
146 	u8 hys_far;
147 	u8 hys_close;
148 	bool is_gp2ap002s00f;
149 	int irq;
150 	bool enabled;
151 };
152 
153 static irqreturn_t gp2ap002_prox_irq(int irq, void *d)
154 {
155 	struct iio_dev *indio_dev = d;
156 	struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
157 	u64 ev;
158 	int val;
159 	int ret;
160 
161 	ret = regmap_read(gp2ap002->map, GP2AP002_PROX, &val);
162 	if (ret) {
163 		dev_err(gp2ap002->dev, "error reading proximity\n");
164 		goto err_retrig;
165 	}
166 
167 	if (val & GP2AP002_PROX_VO_DETECT) {
168 		/* Close */
169 		dev_dbg(gp2ap002->dev, "close\n");
170 		ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
171 				   gp2ap002->hys_far);
172 		if (ret)
173 			dev_err(gp2ap002->dev,
174 				"error setting up proximity hysteresis\n");
175 		ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
176 					IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING);
177 	} else {
178 		/* Far */
179 		dev_dbg(gp2ap002->dev, "far\n");
180 		ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
181 				   gp2ap002->hys_close);
182 		if (ret)
183 			dev_err(gp2ap002->dev,
184 				"error setting up proximity hysteresis\n");
185 		ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
186 					IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING);
187 	}
188 	iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
189 
190 	/*
191 	 * After changing hysteresis, we need to wait for one detection
192 	 * cycle to see if anything changed, or we will just trigger the
193 	 * previous interrupt again. A detection cycle depends on the CYCLE
194 	 * register, we are hard-coding ~8 ms in probe() so wait some more
195 	 * than this, 20-30 ms.
196 	 */
197 	usleep_range(20000, 30000);
198 
199 err_retrig:
200 	ret = regmap_write(gp2ap002->map, GP2AP002_CON,
201 			   GP2AP002_CON_OCON_ENABLE);
202 	if (ret)
203 		dev_err(gp2ap002->dev, "error setting up VOUT control\n");
204 
205 	return IRQ_HANDLED;
206 }
207 
208 /*
209  * This array maps current and lux.
210  *
211  * Ambient light sensing range is 3 to 55000 lux.
212  *
213  * This mapping is based on the following formula.
214  * illuminance = 10 ^ (current[mA] / 10)
215  *
216  * When the ADC measures 0, return 0 lux.
217  */
218 static const u16 gp2ap002_illuminance_table[] = {
219 	0, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 63, 79,
220 	100, 126, 158, 200, 251, 316, 398, 501, 631, 794, 1000, 1259, 1585,
221 	1995, 2512, 3162, 3981, 5012, 6310, 7943, 10000, 12589, 15849, 19953,
222 	25119, 31623, 39811, 50119,
223 };
224 
225 static int gp2ap002_get_lux(struct gp2ap002 *gp2ap002)
226 {
227 	int ret, res;
228 	u16 lux;
229 
230 	ret = iio_read_channel_processed(gp2ap002->alsout, &res);
231 	if (ret < 0)
232 		return ret;
233 
234 	dev_dbg(gp2ap002->dev, "read %d mA from ADC\n", res);
235 
236 	/* ensure we don't under/overflow */
237 	res = clamp(res, 0, (int)ARRAY_SIZE(gp2ap002_illuminance_table) - 1);
238 	lux = gp2ap002_illuminance_table[res];
239 
240 	return (int)lux;
241 }
242 
243 static int gp2ap002_read_raw(struct iio_dev *indio_dev,
244 			   struct iio_chan_spec const *chan,
245 			   int *val, int *val2, long mask)
246 {
247 	struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
248 	int ret;
249 
250 	switch (mask) {
251 	case IIO_CHAN_INFO_RAW:
252 		switch (chan->type) {
253 		case IIO_LIGHT:
254 			ret = gp2ap002_get_lux(gp2ap002);
255 			if (ret < 0)
256 				return ret;
257 			*val = ret;
258 			return IIO_VAL_INT;
259 		default:
260 			return -EINVAL;
261 		}
262 	default:
263 		return -EINVAL;
264 	}
265 }
266 
267 static int gp2ap002_init(struct gp2ap002 *gp2ap002)
268 {
269 	int ret;
270 
271 	/* Set up the IR LED resistance */
272 	ret = regmap_write(gp2ap002->map, GP2AP002_GAIN,
273 			   GP2AP002_GAIN_LED_NORMAL);
274 	if (ret) {
275 		dev_err(gp2ap002->dev, "error setting up LED gain\n");
276 		return ret;
277 	}
278 	ret = regmap_write(gp2ap002->map, GP2AP002_HYS, gp2ap002->hys_far);
279 	if (ret) {
280 		dev_err(gp2ap002->dev,
281 			"error setting up proximity hysteresis\n");
282 		return ret;
283 	}
284 
285 	/* Disable internal frequency hopping */
286 	ret = regmap_write(gp2ap002->map, GP2AP002_CYCLE,
287 			   GP2AP002_CYCLE_OSC_INEFFECTIVE);
288 	if (ret) {
289 		dev_err(gp2ap002->dev,
290 			"error setting up internal frequency hopping\n");
291 		return ret;
292 	}
293 
294 	/* Enable chip and IRQ, disable analog sleep */
295 	ret = regmap_write(gp2ap002->map, GP2AP002_OPMOD,
296 			   GP2AP002_OPMOD_SSD_OPERATING |
297 			   GP2AP002_OPMOD_VCON_IRQ);
298 	if (ret) {
299 		dev_err(gp2ap002->dev, "error setting up operation mode\n");
300 		return ret;
301 	}
302 
303 	/* Interrupt on VOUT enabled */
304 	ret = regmap_write(gp2ap002->map, GP2AP002_CON,
305 			   GP2AP002_CON_OCON_ENABLE);
306 	if (ret)
307 		dev_err(gp2ap002->dev, "error setting up VOUT control\n");
308 
309 	return ret;
310 }
311 
312 static int gp2ap002_read_event_config(struct iio_dev *indio_dev,
313 				      const struct iio_chan_spec *chan,
314 				      enum iio_event_type type,
315 				      enum iio_event_direction dir)
316 {
317 	struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
318 
319 	/*
320 	 * We just keep track of this internally, as it is not possible to
321 	 * query the hardware.
322 	 */
323 	return gp2ap002->enabled;
324 }
325 
326 static int gp2ap002_write_event_config(struct iio_dev *indio_dev,
327 				       const struct iio_chan_spec *chan,
328 				       enum iio_event_type type,
329 				       enum iio_event_direction dir,
330 				       int state)
331 {
332 	struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
333 
334 	if (state) {
335 		/*
336 		 * This will bring the regulators up (unless they are on
337 		 * already) and reintialize the sensor by using runtime_pm
338 		 * callbacks.
339 		 */
340 		pm_runtime_get_sync(gp2ap002->dev);
341 		gp2ap002->enabled = true;
342 	} else {
343 		pm_runtime_mark_last_busy(gp2ap002->dev);
344 		pm_runtime_put_autosuspend(gp2ap002->dev);
345 		gp2ap002->enabled = false;
346 	}
347 
348 	return 0;
349 }
350 
351 static const struct iio_info gp2ap002_info = {
352 	.read_raw = gp2ap002_read_raw,
353 	.read_event_config = gp2ap002_read_event_config,
354 	.write_event_config = gp2ap002_write_event_config,
355 };
356 
357 static const struct iio_event_spec gp2ap002_events[] = {
358 	{
359 		.type = IIO_EV_TYPE_THRESH,
360 		.dir = IIO_EV_DIR_EITHER,
361 		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
362 	},
363 };
364 
365 static const struct iio_chan_spec gp2ap002_channels[] = {
366 	{
367 		.type = IIO_PROXIMITY,
368 		.event_spec = gp2ap002_events,
369 		.num_event_specs = ARRAY_SIZE(gp2ap002_events),
370 	},
371 	{
372 		.type = IIO_LIGHT,
373 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
374 		.channel = GP2AP002_ALS_CHANNEL,
375 	},
376 };
377 
378 /*
379  * We need a special regmap because this hardware expects to
380  * write single bytes to registers but read a 16bit word on some
381  * variants and discard the lower 8 bits so combine
382  * i2c_smbus_read_word_data() with i2c_smbus_write_byte_data()
383  * selectively like this.
384  */
385 static int gp2ap002_regmap_i2c_read(void *context, unsigned int reg,
386 				    unsigned int *val)
387 {
388 	struct device *dev = context;
389 	struct i2c_client *i2c = to_i2c_client(dev);
390 	int ret;
391 
392 	ret = i2c_smbus_read_word_data(i2c, reg);
393 	if (ret < 0)
394 		return ret;
395 
396 	*val = (ret >> 8) & 0xFF;
397 
398 	return 0;
399 }
400 
401 static int gp2ap002_regmap_i2c_write(void *context, unsigned int reg,
402 				     unsigned int val)
403 {
404 	struct device *dev = context;
405 	struct i2c_client *i2c = to_i2c_client(dev);
406 
407 	return i2c_smbus_write_byte_data(i2c, reg, val);
408 }
409 
410 static struct regmap_bus gp2ap002_regmap_bus = {
411 	.reg_read = gp2ap002_regmap_i2c_read,
412 	.reg_write = gp2ap002_regmap_i2c_write,
413 };
414 
415 static int gp2ap002_probe(struct i2c_client *client,
416 			  const struct i2c_device_id *id)
417 {
418 	struct gp2ap002 *gp2ap002;
419 	struct iio_dev *indio_dev;
420 	struct device *dev = &client->dev;
421 	enum iio_chan_type ch_type;
422 	static const struct regmap_config config = {
423 		.reg_bits = 8,
424 		.val_bits = 8,
425 		.max_register = GP2AP002_CON,
426 	};
427 	struct regmap *regmap;
428 	int num_chan;
429 	const char *compat;
430 	u8 val;
431 	int ret;
432 
433 	indio_dev = devm_iio_device_alloc(dev, sizeof(*gp2ap002));
434 	if (!indio_dev)
435 		return -ENOMEM;
436 	i2c_set_clientdata(client, indio_dev);
437 
438 	gp2ap002 = iio_priv(indio_dev);
439 	gp2ap002->dev = dev;
440 
441 	/*
442 	 * Check the device compatible like this makes it possible to use
443 	 * ACPI PRP0001 for registering the sensor using device tree
444 	 * properties.
445 	 */
446 	ret = device_property_read_string(dev, "compatible", &compat);
447 	if (ret) {
448 		dev_err(dev, "cannot check compatible\n");
449 		return ret;
450 	}
451 	gp2ap002->is_gp2ap002s00f = !strcmp(compat, "sharp,gp2ap002s00f");
452 
453 	regmap = devm_regmap_init(dev, &gp2ap002_regmap_bus, dev, &config);
454 	if (IS_ERR(regmap)) {
455 		dev_err(dev, "Failed to register i2c regmap %d\n",
456 			(int)PTR_ERR(regmap));
457 		return PTR_ERR(regmap);
458 	}
459 	gp2ap002->map = regmap;
460 
461 	/*
462 	 * The hysteresis settings are coded into the device tree as values
463 	 * to be written into the hysteresis register. The datasheet defines
464 	 * modes "A", "B1" and "B2" with fixed values to be use but vendor
465 	 * code trees for actual devices are tweaking these values and refer to
466 	 * modes named things like "B1.5". To be able to support any devices,
467 	 * we allow passing an arbitrary hysteresis setting for "near" and
468 	 * "far".
469 	 */
470 
471 	/* Check the device tree for the IR LED hysteresis */
472 	ret = device_property_read_u8(dev, "sharp,proximity-far-hysteresis",
473 				      &val);
474 	if (ret) {
475 		dev_err(dev, "failed to obtain proximity far setting\n");
476 		return ret;
477 	}
478 	dev_dbg(dev, "proximity far setting %02x\n", val);
479 	gp2ap002->hys_far = val;
480 
481 	ret = device_property_read_u8(dev, "sharp,proximity-close-hysteresis",
482 				      &val);
483 	if (ret) {
484 		dev_err(dev, "failed to obtain proximity close setting\n");
485 		return ret;
486 	}
487 	dev_dbg(dev, "proximity close setting %02x\n", val);
488 	gp2ap002->hys_close = val;
489 
490 	/* The GP2AP002A00F has a light sensor too */
491 	if (!gp2ap002->is_gp2ap002s00f) {
492 		gp2ap002->alsout = devm_iio_channel_get(dev, "alsout");
493 		if (IS_ERR(gp2ap002->alsout)) {
494 			if (PTR_ERR(gp2ap002->alsout) == -ENODEV) {
495 				dev_err(dev, "no ADC, deferring...\n");
496 				return -EPROBE_DEFER;
497 			}
498 			dev_err(dev, "failed to get ALSOUT ADC channel\n");
499 			return PTR_ERR(gp2ap002->alsout);
500 		}
501 		ret = iio_get_channel_type(gp2ap002->alsout, &ch_type);
502 		if (ret < 0)
503 			return ret;
504 		if (ch_type != IIO_CURRENT) {
505 			dev_err(dev,
506 				"wrong type of IIO channel specified for ALSOUT\n");
507 			return -EINVAL;
508 		}
509 	}
510 
511 	gp2ap002->vdd = devm_regulator_get(dev, "vdd");
512 	if (IS_ERR(gp2ap002->vdd)) {
513 		dev_err(dev, "failed to get VDD regulator\n");
514 		return PTR_ERR(gp2ap002->vdd);
515 	}
516 	gp2ap002->vio = devm_regulator_get(dev, "vio");
517 	if (IS_ERR(gp2ap002->vio)) {
518 		dev_err(dev, "failed to get VIO regulator\n");
519 		return PTR_ERR(gp2ap002->vio);
520 	}
521 
522 	/* Operating voltage 2.4V .. 3.6V according to datasheet */
523 	ret = regulator_set_voltage(gp2ap002->vdd, 2400000, 3600000);
524 	if (ret) {
525 		dev_err(dev, "failed to sett VDD voltage\n");
526 		return ret;
527 	}
528 
529 	/* VIO should be between 1.65V and VDD */
530 	ret = regulator_get_voltage(gp2ap002->vdd);
531 	if (ret < 0) {
532 		dev_err(dev, "failed to get VDD voltage\n");
533 		return ret;
534 	}
535 	ret = regulator_set_voltage(gp2ap002->vio, 1650000, ret);
536 	if (ret) {
537 		dev_err(dev, "failed to set VIO voltage\n");
538 		return ret;
539 	}
540 
541 	ret = regulator_enable(gp2ap002->vdd);
542 	if (ret) {
543 		dev_err(dev, "failed to enable VDD regulator\n");
544 		return ret;
545 	}
546 	ret = regulator_enable(gp2ap002->vio);
547 	if (ret) {
548 		dev_err(dev, "failed to enable VIO regulator\n");
549 		goto out_disable_vdd;
550 	}
551 
552 	msleep(20);
553 
554 	/*
555 	 * Initialize the device and signal to runtime PM that now we are
556 	 * definately up and using power.
557 	 */
558 	ret = gp2ap002_init(gp2ap002);
559 	if (ret) {
560 		dev_err(dev, "initialization failed\n");
561 		goto out_disable_vio;
562 	}
563 	pm_runtime_get_noresume(dev);
564 	pm_runtime_set_active(dev);
565 	pm_runtime_enable(dev);
566 	gp2ap002->enabled = false;
567 
568 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
569 					gp2ap002_prox_irq, IRQF_ONESHOT,
570 					"gp2ap002", indio_dev);
571 	if (ret) {
572 		dev_err(dev, "unable to request IRQ\n");
573 		goto out_disable_vio;
574 	}
575 	gp2ap002->irq = client->irq;
576 
577 	/*
578 	 * As the device takes 20 ms + regulator delay to come up with a fresh
579 	 * measurement after power-on, do not shut it down unnecessarily.
580 	 * Set autosuspend to a one second.
581 	 */
582 	pm_runtime_set_autosuspend_delay(dev, 1000);
583 	pm_runtime_use_autosuspend(dev);
584 	pm_runtime_put(dev);
585 
586 	indio_dev->dev.parent = dev;
587 	indio_dev->info = &gp2ap002_info;
588 	indio_dev->name = "gp2ap002";
589 	indio_dev->channels = gp2ap002_channels;
590 	/* Skip light channel for the proximity-only sensor */
591 	num_chan = ARRAY_SIZE(gp2ap002_channels);
592 	if (gp2ap002->is_gp2ap002s00f)
593 		num_chan--;
594 	indio_dev->num_channels = num_chan;
595 	indio_dev->modes = INDIO_DIRECT_MODE;
596 
597 	ret = iio_device_register(indio_dev);
598 	if (ret)
599 		goto out_disable_pm;
600 	dev_dbg(dev, "Sharp GP2AP002 probed successfully\n");
601 
602 	return 0;
603 
604 out_disable_pm:
605 	pm_runtime_put_noidle(dev);
606 	pm_runtime_disable(dev);
607 out_disable_vio:
608 	regulator_disable(gp2ap002->vio);
609 out_disable_vdd:
610 	regulator_disable(gp2ap002->vdd);
611 	return ret;
612 }
613 
614 static int gp2ap002_remove(struct i2c_client *client)
615 {
616 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
617 	struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
618 	struct device *dev = &client->dev;
619 
620 	pm_runtime_get_sync(dev);
621 	pm_runtime_put_noidle(dev);
622 	pm_runtime_disable(dev);
623 	iio_device_unregister(indio_dev);
624 	regulator_disable(gp2ap002->vio);
625 	regulator_disable(gp2ap002->vdd);
626 
627 	return 0;
628 }
629 
630 static int __maybe_unused gp2ap002_runtime_suspend(struct device *dev)
631 {
632 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
633 	struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
634 	int ret;
635 
636 	/* Deactivate the IRQ */
637 	disable_irq(gp2ap002->irq);
638 
639 	/* Disable chip and IRQ, everything off */
640 	ret = regmap_write(gp2ap002->map, GP2AP002_OPMOD, 0x00);
641 	if (ret) {
642 		dev_err(gp2ap002->dev, "error setting up operation mode\n");
643 		return ret;
644 	}
645 	/*
646 	 * As these regulators may be shared, at least we are now in
647 	 * sleep even if the regulators aren't really turned off.
648 	 */
649 	regulator_disable(gp2ap002->vio);
650 	regulator_disable(gp2ap002->vdd);
651 
652 	return 0;
653 }
654 
655 static int __maybe_unused gp2ap002_runtime_resume(struct device *dev)
656 {
657 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
658 	struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
659 	int ret;
660 
661 	ret = regulator_enable(gp2ap002->vdd);
662 	if (ret) {
663 		dev_err(dev, "failed to enable VDD regulator in resume path\n");
664 		return ret;
665 	}
666 	ret = regulator_enable(gp2ap002->vio);
667 	if (ret) {
668 		dev_err(dev, "failed to enable VIO regulator in resume path\n");
669 		return ret;
670 	}
671 
672 	msleep(20);
673 
674 	ret = gp2ap002_init(gp2ap002);
675 	if (ret) {
676 		dev_err(dev, "re-initialization failed\n");
677 		return ret;
678 	}
679 
680 	/* Re-activate the IRQ */
681 	enable_irq(gp2ap002->irq);
682 
683 	return 0;
684 }
685 
686 static const struct dev_pm_ops gp2ap002_dev_pm_ops = {
687 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
688 				pm_runtime_force_resume)
689 	SET_RUNTIME_PM_OPS(gp2ap002_runtime_suspend,
690 			   gp2ap002_runtime_resume, NULL)
691 };
692 
693 static const struct i2c_device_id gp2ap002_id_table[] = {
694 	{ "gp2ap002", 0 },
695 	{ },
696 };
697 MODULE_DEVICE_TABLE(i2c, gp2ap002_id_table);
698 
699 static const struct of_device_id gp2ap002_of_match[] = {
700 	{ .compatible = "sharp,gp2ap002a00f" },
701 	{ .compatible = "sharp,gp2ap002s00f" },
702 	{ },
703 };
704 MODULE_DEVICE_TABLE(of, gp2ap002_of_match);
705 
706 static struct i2c_driver gp2ap002_driver = {
707 	.driver = {
708 		.name = "gp2ap002",
709 		.of_match_table = gp2ap002_of_match,
710 		.pm = &gp2ap002_dev_pm_ops,
711 	},
712 	.probe = gp2ap002_probe,
713 	.remove = gp2ap002_remove,
714 	.id_table = gp2ap002_id_table,
715 };
716 module_i2c_driver(gp2ap002_driver);
717 
718 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
719 MODULE_DESCRIPTION("GP2AP002 ambient light and proximity sensor driver");
720 MODULE_LICENSE("GPL v2");
721