xref: /openbmc/linux/drivers/misc/apds990x.c (revision 2fa49589)
1 /*
2  * This file is part of the APDS990x sensor driver.
3  * Chip is combined proximity and ambient light sensor.
4  *
5  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6  *
7  * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/i2c.h>
28 #include <linux/interrupt.h>
29 #include <linux/mutex.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/delay.h>
33 #include <linux/wait.h>
34 #include <linux/slab.h>
35 #include <linux/platform_data/apds990x.h>
36 
37 /* Register map */
38 #define APDS990X_ENABLE	 0x00 /* Enable of states and interrupts */
39 #define APDS990X_ATIME	 0x01 /* ALS ADC time  */
40 #define APDS990X_PTIME	 0x02 /* Proximity ADC time  */
41 #define APDS990X_WTIME	 0x03 /* Wait time  */
42 #define APDS990X_AILTL	 0x04 /* ALS interrupt low threshold low byte */
43 #define APDS990X_AILTH	 0x05 /* ALS interrupt low threshold hi byte */
44 #define APDS990X_AIHTL	 0x06 /* ALS interrupt hi threshold low byte */
45 #define APDS990X_AIHTH	 0x07 /* ALS interrupt hi threshold hi byte */
46 #define APDS990X_PILTL	 0x08 /* Proximity interrupt low threshold low byte */
47 #define APDS990X_PILTH	 0x09 /* Proximity interrupt low threshold hi byte */
48 #define APDS990X_PIHTL	 0x0a /* Proximity interrupt hi threshold low byte */
49 #define APDS990X_PIHTH	 0x0b /* Proximity interrupt hi threshold hi byte */
50 #define APDS990X_PERS	 0x0c /* Interrupt persistence filters */
51 #define APDS990X_CONFIG	 0x0d /* Configuration */
52 #define APDS990X_PPCOUNT 0x0e /* Proximity pulse count */
53 #define APDS990X_CONTROL 0x0f /* Gain control register */
54 #define APDS990X_REV	 0x11 /* Revision Number */
55 #define APDS990X_ID	 0x12 /* Device ID */
56 #define APDS990X_STATUS	 0x13 /* Device status */
57 #define APDS990X_CDATAL	 0x14 /* Clear ADC low data register */
58 #define APDS990X_CDATAH	 0x15 /* Clear ADC high data register */
59 #define APDS990X_IRDATAL 0x16 /* IR ADC low data register */
60 #define APDS990X_IRDATAH 0x17 /* IR ADC high data register */
61 #define APDS990X_PDATAL	 0x18 /* Proximity ADC low data register */
62 #define APDS990X_PDATAH	 0x19 /* Proximity ADC high data register */
63 
64 /* Control */
65 #define APDS990X_MAX_AGAIN	3
66 
67 /* Enable register */
68 #define APDS990X_EN_PIEN	(0x1 << 5)
69 #define APDS990X_EN_AIEN	(0x1 << 4)
70 #define APDS990X_EN_WEN		(0x1 << 3)
71 #define APDS990X_EN_PEN		(0x1 << 2)
72 #define APDS990X_EN_AEN		(0x1 << 1)
73 #define APDS990X_EN_PON		(0x1 << 0)
74 #define APDS990X_EN_DISABLE_ALL 0
75 
76 /* Status register */
77 #define APDS990X_ST_PINT	(0x1 << 5)
78 #define APDS990X_ST_AINT	(0x1 << 4)
79 
80 /* I2C access types */
81 #define APDS990x_CMD_TYPE_MASK	(0x03 << 5)
82 #define APDS990x_CMD_TYPE_RB	(0x00 << 5) /* Repeated byte */
83 #define APDS990x_CMD_TYPE_INC	(0x01 << 5) /* Auto increment */
84 #define APDS990x_CMD_TYPE_SPE	(0x03 << 5) /* Special function */
85 
86 #define APDS990x_ADDR_SHIFT	0
87 #define APDS990x_CMD		0x80
88 
89 /* Interrupt ack commands */
90 #define APDS990X_INT_ACK_ALS	0x6
91 #define APDS990X_INT_ACK_PS	0x5
92 #define APDS990X_INT_ACK_BOTH	0x7
93 
94 /* ptime */
95 #define APDS990X_PTIME_DEFAULT	0xff /* Recommended conversion time 2.7ms*/
96 
97 /* wtime */
98 #define APDS990X_WTIME_DEFAULT	0xee /* ~50ms wait time */
99 
100 #define APDS990X_TIME_TO_ADC	1024 /* One timetick as ADC count value */
101 
102 /* Persistence */
103 #define APDS990X_APERS_SHIFT	0
104 #define APDS990X_PPERS_SHIFT	4
105 
106 /* Supported ID:s */
107 #define APDS990X_ID_0		0x0
108 #define APDS990X_ID_4		0x4
109 #define APDS990X_ID_29		0x29
110 
111 /* pgain and pdiode settings */
112 #define APDS_PGAIN_1X	       0x0
113 #define APDS_PDIODE_IR	       0x2
114 
115 #define APDS990X_LUX_OUTPUT_SCALE 10
116 
117 /* Reverse chip factors for threshold calculation */
118 struct reverse_factors {
119 	u32 afactor;
120 	int cf1;
121 	int irf1;
122 	int cf2;
123 	int irf2;
124 };
125 
126 struct apds990x_chip {
127 	struct apds990x_platform_data	*pdata;
128 	struct i2c_client		*client;
129 	struct mutex			mutex; /* avoid parallel access */
130 	struct regulator_bulk_data	regs[2];
131 	wait_queue_head_t		wait;
132 
133 	int	prox_en;
134 	bool	prox_continuous_mode;
135 	bool	lux_wait_fresh_res;
136 
137 	/* Chip parameters */
138 	struct	apds990x_chip_factors	cf;
139 	struct	reverse_factors		rcf;
140 	u16	atime;		/* als integration time */
141 	u16	arate;		/* als reporting rate */
142 	u16	a_max_result;	/* Max possible ADC value with current atime */
143 	u8	again_meas;	/* Gain used in last measurement */
144 	u8	again_next;	/* Next calculated gain */
145 	u8	pgain;
146 	u8	pdiode;
147 	u8	pdrive;
148 	u8	lux_persistence;
149 	u8	prox_persistence;
150 
151 	u32	lux_raw;
152 	u32	lux;
153 	u16	lux_clear;
154 	u16	lux_ir;
155 	u16	lux_calib;
156 	u32	lux_thres_hi;
157 	u32	lux_thres_lo;
158 
159 	u32	prox_thres;
160 	u16	prox_data;
161 	u16	prox_calib;
162 
163 	char	chipname[10];
164 	u8	revision;
165 };
166 
167 #define APDS_CALIB_SCALER		8192
168 #define APDS_LUX_NEUTRAL_CALIB_VALUE	(1 * APDS_CALIB_SCALER)
169 #define APDS_PROX_NEUTRAL_CALIB_VALUE	(1 * APDS_CALIB_SCALER)
170 
171 #define APDS_PROX_DEF_THRES		600
172 #define APDS_PROX_HYSTERESIS		50
173 #define APDS_LUX_DEF_THRES_HI		101
174 #define APDS_LUX_DEF_THRES_LO		100
175 #define APDS_DEFAULT_PROX_PERS		1
176 
177 #define APDS_TIMEOUT			2000
178 #define APDS_STARTUP_DELAY		25000 /* us */
179 #define APDS_RANGE			65535
180 #define APDS_PROX_RANGE			1023
181 #define APDS_LUX_GAIN_LO_LIMIT		100
182 #define APDS_LUX_GAIN_LO_LIMIT_STRICT	25
183 
184 #define TIMESTEP			87 /* 2.7ms is about 87 / 32 */
185 #define TIME_STEP_SCALER		32
186 
187 #define APDS_LUX_AVERAGING_TIME		50 /* tolerates 50/60Hz ripple */
188 #define APDS_LUX_DEFAULT_RATE		200
189 
190 static const u8 again[]	= {1, 8, 16, 120}; /* ALS gain steps */
191 
192 /* Following two tables must match i.e 10Hz rate means 1 as persistence value */
193 static const u16 arates_hz[] = {10, 5, 2, 1};
194 static const u8 apersis[] = {1, 2, 4, 5};
195 
196 /* Regulators */
197 static const char reg_vcc[] = "Vdd";
198 static const char reg_vled[] = "Vled";
199 
200 static int apds990x_read_byte(struct apds990x_chip *chip, u8 reg, u8 *data)
201 {
202 	struct i2c_client *client = chip->client;
203 	s32 ret;
204 
205 	reg &= ~APDS990x_CMD_TYPE_MASK;
206 	reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
207 
208 	ret = i2c_smbus_read_byte_data(client, reg);
209 	*data = ret;
210 	return (int)ret;
211 }
212 
213 static int apds990x_read_word(struct apds990x_chip *chip, u8 reg, u16 *data)
214 {
215 	struct i2c_client *client = chip->client;
216 	s32 ret;
217 
218 	reg &= ~APDS990x_CMD_TYPE_MASK;
219 	reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
220 
221 	ret = i2c_smbus_read_word_data(client, reg);
222 	*data = ret;
223 	return (int)ret;
224 }
225 
226 static int apds990x_write_byte(struct apds990x_chip *chip, u8 reg, u8 data)
227 {
228 	struct i2c_client *client = chip->client;
229 	s32 ret;
230 
231 	reg &= ~APDS990x_CMD_TYPE_MASK;
232 	reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
233 
234 	ret = i2c_smbus_write_byte_data(client, reg, data);
235 	return (int)ret;
236 }
237 
238 static int apds990x_write_word(struct apds990x_chip *chip, u8 reg, u16 data)
239 {
240 	struct i2c_client *client = chip->client;
241 	s32 ret;
242 
243 	reg &= ~APDS990x_CMD_TYPE_MASK;
244 	reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
245 
246 	ret = i2c_smbus_write_word_data(client, reg, data);
247 	return (int)ret;
248 }
249 
250 static int apds990x_mode_on(struct apds990x_chip *chip)
251 {
252 	/* ALS is mandatory, proximity optional */
253 	u8 reg = APDS990X_EN_AIEN | APDS990X_EN_PON | APDS990X_EN_AEN |
254 		APDS990X_EN_WEN;
255 
256 	if (chip->prox_en)
257 		reg |= APDS990X_EN_PIEN | APDS990X_EN_PEN;
258 
259 	return apds990x_write_byte(chip, APDS990X_ENABLE, reg);
260 }
261 
262 static u16 apds990x_lux_to_threshold(struct apds990x_chip *chip, u32 lux)
263 {
264 	u32 thres;
265 	u32 cpl;
266 	u32 ir;
267 
268 	if (lux == 0)
269 		return 0;
270 	else if (lux == APDS_RANGE)
271 		return APDS_RANGE;
272 
273 	/*
274 	 * Reported LUX value is a combination of the IR and CLEAR channel
275 	 * values. However, interrupt threshold is only for clear channel.
276 	 * This function approximates needed HW threshold value for a given
277 	 * LUX value in the current lightning type.
278 	 * IR level compared to visible light varies heavily depending on the
279 	 * source of the light
280 	 *
281 	 * Calculate threshold value for the next measurement period.
282 	 * Math: threshold = lux * cpl where
283 	 * cpl = atime * again / (glass_attenuation * device_factor)
284 	 * (count-per-lux)
285 	 *
286 	 * First remove calibration. Division by four is to avoid overflow
287 	 */
288 	lux = lux * (APDS_CALIB_SCALER / 4) / (chip->lux_calib / 4);
289 
290 	/* Multiplication by 64 is to increase accuracy */
291 	cpl = ((u32)chip->atime * (u32)again[chip->again_next] *
292 		APDS_PARAM_SCALE * 64) / (chip->cf.ga * chip->cf.df);
293 
294 	thres = lux * cpl / 64;
295 	/*
296 	 * Convert IR light from the latest result to match with
297 	 * new gain step. This helps to adapt with the current
298 	 * source of light.
299 	 */
300 	ir = (u32)chip->lux_ir * (u32)again[chip->again_next] /
301 		(u32)again[chip->again_meas];
302 
303 	/*
304 	 * Compensate count with IR light impact
305 	 * IAC1 > IAC2 (see apds990x_get_lux for formulas)
306 	 */
307 	if (chip->lux_clear * APDS_PARAM_SCALE >=
308 		chip->rcf.afactor * chip->lux_ir)
309 		thres = (chip->rcf.cf1 * thres + chip->rcf.irf1 * ir) /
310 			APDS_PARAM_SCALE;
311 	else
312 		thres = (chip->rcf.cf2 * thres + chip->rcf.irf2 * ir) /
313 			APDS_PARAM_SCALE;
314 
315 	if (thres >= chip->a_max_result)
316 		thres = chip->a_max_result - 1;
317 	return thres;
318 }
319 
320 static inline int apds990x_set_atime(struct apds990x_chip *chip, u32 time_ms)
321 {
322 	u8 reg_value;
323 
324 	chip->atime = time_ms;
325 	/* Formula is specified in the data sheet */
326 	reg_value = 256 - ((time_ms * TIME_STEP_SCALER) / TIMESTEP);
327 	/* Calculate max ADC value for given integration time */
328 	chip->a_max_result = (u16)(256 - reg_value) * APDS990X_TIME_TO_ADC;
329 	return apds990x_write_byte(chip, APDS990X_ATIME, reg_value);
330 }
331 
332 /* Called always with mutex locked */
333 static int apds990x_refresh_pthres(struct apds990x_chip *chip, int data)
334 {
335 	int ret, lo, hi;
336 
337 	/* If the chip is not in use, don't try to access it */
338 	if (pm_runtime_suspended(&chip->client->dev))
339 		return 0;
340 
341 	if (data < chip->prox_thres) {
342 		lo = 0;
343 		hi = chip->prox_thres;
344 	} else {
345 		lo = chip->prox_thres - APDS_PROX_HYSTERESIS;
346 		if (chip->prox_continuous_mode)
347 			hi = chip->prox_thres;
348 		else
349 			hi = APDS_RANGE;
350 	}
351 
352 	ret = apds990x_write_word(chip, APDS990X_PILTL, lo);
353 	ret |= apds990x_write_word(chip, APDS990X_PIHTL, hi);
354 	return ret;
355 }
356 
357 /* Called always with mutex locked */
358 static int apds990x_refresh_athres(struct apds990x_chip *chip)
359 {
360 	int ret;
361 	/* If the chip is not in use, don't try to access it */
362 	if (pm_runtime_suspended(&chip->client->dev))
363 		return 0;
364 
365 	ret = apds990x_write_word(chip, APDS990X_AILTL,
366 			apds990x_lux_to_threshold(chip, chip->lux_thres_lo));
367 	ret |= apds990x_write_word(chip, APDS990X_AIHTL,
368 			apds990x_lux_to_threshold(chip, chip->lux_thres_hi));
369 
370 	return ret;
371 }
372 
373 /* Called always with mutex locked */
374 static void apds990x_force_a_refresh(struct apds990x_chip *chip)
375 {
376 	/* This will force ALS interrupt after the next measurement. */
377 	apds990x_write_word(chip, APDS990X_AILTL, APDS_LUX_DEF_THRES_LO);
378 	apds990x_write_word(chip, APDS990X_AIHTL, APDS_LUX_DEF_THRES_HI);
379 }
380 
381 /* Called always with mutex locked */
382 static void apds990x_force_p_refresh(struct apds990x_chip *chip)
383 {
384 	/* This will force proximity interrupt after the next measurement. */
385 	apds990x_write_word(chip, APDS990X_PILTL, APDS_PROX_DEF_THRES - 1);
386 	apds990x_write_word(chip, APDS990X_PIHTL, APDS_PROX_DEF_THRES);
387 }
388 
389 /* Called always with mutex locked */
390 static int apds990x_calc_again(struct apds990x_chip *chip)
391 {
392 	int curr_again = chip->again_meas;
393 	int next_again = chip->again_meas;
394 	int ret = 0;
395 
396 	/* Calculate suitable als gain */
397 	if (chip->lux_clear == chip->a_max_result)
398 		next_again -= 2; /* ALS saturated. Decrease gain by 2 steps */
399 	else if (chip->lux_clear > chip->a_max_result / 2)
400 		next_again--;
401 	else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
402 		next_again += 2; /* Too dark. Increase gain by 2 steps */
403 	else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT)
404 		next_again++;
405 
406 	/* Limit gain to available range */
407 	if (next_again < 0)
408 		next_again = 0;
409 	else if (next_again > APDS990X_MAX_AGAIN)
410 		next_again = APDS990X_MAX_AGAIN;
411 
412 	/* Let's check can we trust the measured result */
413 	if (chip->lux_clear == chip->a_max_result)
414 		/* Result can be totally garbage due to saturation */
415 		ret = -ERANGE;
416 	else if (next_again != curr_again &&
417 		chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
418 		/*
419 		 * Gain is changed and measurement result is very small.
420 		 * Result can be totally garbage due to underflow
421 		 */
422 		ret = -ERANGE;
423 
424 	chip->again_next = next_again;
425 	apds990x_write_byte(chip, APDS990X_CONTROL,
426 			(chip->pdrive << 6) |
427 			(chip->pdiode << 4) |
428 			(chip->pgain << 2) |
429 			(chip->again_next << 0));
430 
431 	/*
432 	 * Error means bad result -> re-measurement is needed. The forced
433 	 * refresh uses fastest possible persistence setting to get result
434 	 * as soon as possible.
435 	 */
436 	if (ret < 0)
437 		apds990x_force_a_refresh(chip);
438 	else
439 		apds990x_refresh_athres(chip);
440 
441 	return ret;
442 }
443 
444 /* Called always with mutex locked */
445 static int apds990x_get_lux(struct apds990x_chip *chip, int clear, int ir)
446 {
447 	int iac, iac1, iac2; /* IR adjusted counts */
448 	u32 lpc; /* Lux per count */
449 
450 	/* Formulas:
451 	 * iac1 = CF1 * CLEAR_CH - IRF1 * IR_CH
452 	 * iac2 = CF2 * CLEAR_CH - IRF2 * IR_CH
453 	 */
454 	iac1 = (chip->cf.cf1 * clear - chip->cf.irf1 * ir) / APDS_PARAM_SCALE;
455 	iac2 = (chip->cf.cf2 * clear - chip->cf.irf2 * ir) / APDS_PARAM_SCALE;
456 
457 	iac = max(iac1, iac2);
458 	iac = max(iac, 0);
459 
460 	lpc = APDS990X_LUX_OUTPUT_SCALE * (chip->cf.df * chip->cf.ga) /
461 		(u32)(again[chip->again_meas] * (u32)chip->atime);
462 
463 	return (iac * lpc) / APDS_PARAM_SCALE;
464 }
465 
466 static int apds990x_ack_int(struct apds990x_chip *chip, u8 mode)
467 {
468 	struct i2c_client *client = chip->client;
469 	s32 ret;
470 	u8 reg = APDS990x_CMD | APDS990x_CMD_TYPE_SPE;
471 
472 	switch (mode & (APDS990X_ST_AINT | APDS990X_ST_PINT)) {
473 	case APDS990X_ST_AINT:
474 		reg |= APDS990X_INT_ACK_ALS;
475 		break;
476 	case APDS990X_ST_PINT:
477 		reg |= APDS990X_INT_ACK_PS;
478 		break;
479 	default:
480 		reg |= APDS990X_INT_ACK_BOTH;
481 		break;
482 	}
483 
484 	ret = i2c_smbus_read_byte_data(client, reg);
485 	return (int)ret;
486 }
487 
488 static irqreturn_t apds990x_irq(int irq, void *data)
489 {
490 	struct apds990x_chip *chip = data;
491 	u8 status;
492 
493 	apds990x_read_byte(chip, APDS990X_STATUS, &status);
494 	apds990x_ack_int(chip, status);
495 
496 	mutex_lock(&chip->mutex);
497 	if (!pm_runtime_suspended(&chip->client->dev)) {
498 		if (status & APDS990X_ST_AINT) {
499 			apds990x_read_word(chip, APDS990X_CDATAL,
500 					&chip->lux_clear);
501 			apds990x_read_word(chip, APDS990X_IRDATAL,
502 					&chip->lux_ir);
503 			/* Store used gain for calculations */
504 			chip->again_meas = chip->again_next;
505 
506 			chip->lux_raw = apds990x_get_lux(chip,
507 							chip->lux_clear,
508 							chip->lux_ir);
509 
510 			if (apds990x_calc_again(chip) == 0) {
511 				/* Result is valid */
512 				chip->lux = chip->lux_raw;
513 				chip->lux_wait_fresh_res = false;
514 				wake_up(&chip->wait);
515 				sysfs_notify(&chip->client->dev.kobj,
516 					NULL, "lux0_input");
517 			}
518 		}
519 
520 		if ((status & APDS990X_ST_PINT) && chip->prox_en) {
521 			u16 clr_ch;
522 
523 			apds990x_read_word(chip, APDS990X_CDATAL, &clr_ch);
524 			/*
525 			 * If ALS channel is saturated at min gain,
526 			 * proximity gives false posivite values.
527 			 * Just ignore them.
528 			 */
529 			if (chip->again_meas == 0 &&
530 				clr_ch == chip->a_max_result)
531 				chip->prox_data = 0;
532 			else
533 				apds990x_read_word(chip,
534 						APDS990X_PDATAL,
535 						&chip->prox_data);
536 
537 			apds990x_refresh_pthres(chip, chip->prox_data);
538 			if (chip->prox_data < chip->prox_thres)
539 				chip->prox_data = 0;
540 			else if (!chip->prox_continuous_mode)
541 				chip->prox_data = APDS_PROX_RANGE;
542 			sysfs_notify(&chip->client->dev.kobj,
543 				NULL, "prox0_raw");
544 		}
545 	}
546 	mutex_unlock(&chip->mutex);
547 	return IRQ_HANDLED;
548 }
549 
550 static int apds990x_configure(struct apds990x_chip *chip)
551 {
552 	/* It is recommended to use disabled mode during these operations */
553 	apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
554 
555 	/* conversion and wait times for different state machince states */
556 	apds990x_write_byte(chip, APDS990X_PTIME, APDS990X_PTIME_DEFAULT);
557 	apds990x_write_byte(chip, APDS990X_WTIME, APDS990X_WTIME_DEFAULT);
558 	apds990x_set_atime(chip, APDS_LUX_AVERAGING_TIME);
559 
560 	apds990x_write_byte(chip, APDS990X_CONFIG, 0);
561 
562 	/* Persistence levels */
563 	apds990x_write_byte(chip, APDS990X_PERS,
564 			(chip->lux_persistence << APDS990X_APERS_SHIFT) |
565 			(chip->prox_persistence << APDS990X_PPERS_SHIFT));
566 
567 	apds990x_write_byte(chip, APDS990X_PPCOUNT, chip->pdata->ppcount);
568 
569 	/* Start with relatively small gain */
570 	chip->again_meas = 1;
571 	chip->again_next = 1;
572 	apds990x_write_byte(chip, APDS990X_CONTROL,
573 			(chip->pdrive << 6) |
574 			(chip->pdiode << 4) |
575 			(chip->pgain << 2) |
576 			(chip->again_next << 0));
577 	return 0;
578 }
579 
580 static int apds990x_detect(struct apds990x_chip *chip)
581 {
582 	struct i2c_client *client = chip->client;
583 	int ret;
584 	u8 id;
585 
586 	ret = apds990x_read_byte(chip, APDS990X_ID, &id);
587 	if (ret < 0) {
588 		dev_err(&client->dev, "ID read failed\n");
589 		return ret;
590 	}
591 
592 	ret = apds990x_read_byte(chip, APDS990X_REV, &chip->revision);
593 	if (ret < 0) {
594 		dev_err(&client->dev, "REV read failed\n");
595 		return ret;
596 	}
597 
598 	switch (id) {
599 	case APDS990X_ID_0:
600 	case APDS990X_ID_4:
601 	case APDS990X_ID_29:
602 		snprintf(chip->chipname, sizeof(chip->chipname), "APDS-990x");
603 		break;
604 	default:
605 		ret = -ENODEV;
606 		break;
607 	}
608 	return ret;
609 }
610 
611 #ifdef CONFIG_PM
612 static int apds990x_chip_on(struct apds990x_chip *chip)
613 {
614 	int err	 = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
615 					chip->regs);
616 	if (err < 0)
617 		return err;
618 
619 	usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
620 
621 	/* Refresh all configs in case of regulators were off */
622 	chip->prox_data = 0;
623 	apds990x_configure(chip);
624 	apds990x_mode_on(chip);
625 	return 0;
626 }
627 #endif
628 
629 static int apds990x_chip_off(struct apds990x_chip *chip)
630 {
631 	apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
632 	regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
633 	return 0;
634 }
635 
636 static ssize_t apds990x_lux_show(struct device *dev,
637 				 struct device_attribute *attr, char *buf)
638 {
639 	struct apds990x_chip *chip = dev_get_drvdata(dev);
640 	ssize_t ret;
641 	u32 result;
642 	long timeout;
643 
644 	if (pm_runtime_suspended(dev))
645 		return -EIO;
646 
647 	timeout = wait_event_interruptible_timeout(chip->wait,
648 						!chip->lux_wait_fresh_res,
649 						msecs_to_jiffies(APDS_TIMEOUT));
650 	if (!timeout)
651 		return -EIO;
652 
653 	mutex_lock(&chip->mutex);
654 	result = (chip->lux * chip->lux_calib) / APDS_CALIB_SCALER;
655 	if (result > (APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE))
656 		result = APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE;
657 
658 	ret = sprintf(buf, "%d.%d\n",
659 		result / APDS990X_LUX_OUTPUT_SCALE,
660 		result % APDS990X_LUX_OUTPUT_SCALE);
661 	mutex_unlock(&chip->mutex);
662 	return ret;
663 }
664 
665 static DEVICE_ATTR(lux0_input, S_IRUGO, apds990x_lux_show, NULL);
666 
667 static ssize_t apds990x_lux_range_show(struct device *dev,
668 				 struct device_attribute *attr, char *buf)
669 {
670 	return sprintf(buf, "%u\n", APDS_RANGE);
671 }
672 
673 static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, apds990x_lux_range_show, NULL);
674 
675 static ssize_t apds990x_lux_calib_format_show(struct device *dev,
676 				 struct device_attribute *attr, char *buf)
677 {
678 	return sprintf(buf, "%u\n", APDS_CALIB_SCALER);
679 }
680 
681 static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
682 		apds990x_lux_calib_format_show, NULL);
683 
684 static ssize_t apds990x_lux_calib_show(struct device *dev,
685 				 struct device_attribute *attr, char *buf)
686 {
687 	struct apds990x_chip *chip = dev_get_drvdata(dev);
688 
689 	return sprintf(buf, "%u\n", chip->lux_calib);
690 }
691 
692 static ssize_t apds990x_lux_calib_store(struct device *dev,
693 				  struct device_attribute *attr,
694 				  const char *buf, size_t len)
695 {
696 	struct apds990x_chip *chip = dev_get_drvdata(dev);
697 	unsigned long value;
698 	int ret;
699 
700 	ret = kstrtoul(buf, 0, &value);
701 	if (ret)
702 		return ret;
703 
704 	chip->lux_calib = value;
705 
706 	return len;
707 }
708 
709 static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, apds990x_lux_calib_show,
710 		apds990x_lux_calib_store);
711 
712 static ssize_t apds990x_rate_avail(struct device *dev,
713 				   struct device_attribute *attr, char *buf)
714 {
715 	int i;
716 	int pos = 0;
717 
718 	for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
719 		pos += sprintf(buf + pos, "%d ", arates_hz[i]);
720 	sprintf(buf + pos - 1, "\n");
721 	return pos;
722 }
723 
724 static ssize_t apds990x_rate_show(struct device *dev,
725 				   struct device_attribute *attr, char *buf)
726 {
727 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
728 
729 	return sprintf(buf, "%d\n", chip->arate);
730 }
731 
732 static int apds990x_set_arate(struct apds990x_chip *chip, int rate)
733 {
734 	int i;
735 
736 	for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
737 		if (rate >= arates_hz[i])
738 			break;
739 
740 	if (i == ARRAY_SIZE(arates_hz))
741 		return -EINVAL;
742 
743 	/* Pick up corresponding persistence value */
744 	chip->lux_persistence = apersis[i];
745 	chip->arate = arates_hz[i];
746 
747 	/* If the chip is not in use, don't try to access it */
748 	if (pm_runtime_suspended(&chip->client->dev))
749 		return 0;
750 
751 	/* Persistence levels */
752 	return apds990x_write_byte(chip, APDS990X_PERS,
753 			(chip->lux_persistence << APDS990X_APERS_SHIFT) |
754 			(chip->prox_persistence << APDS990X_PPERS_SHIFT));
755 }
756 
757 static ssize_t apds990x_rate_store(struct device *dev,
758 				  struct device_attribute *attr,
759 				  const char *buf, size_t len)
760 {
761 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
762 	unsigned long value;
763 	int ret;
764 
765 	ret = kstrtoul(buf, 0, &value);
766 	if (ret)
767 		return ret;
768 
769 	mutex_lock(&chip->mutex);
770 	ret = apds990x_set_arate(chip, value);
771 	mutex_unlock(&chip->mutex);
772 
773 	if (ret < 0)
774 		return ret;
775 	return len;
776 }
777 
778 static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, apds990x_rate_avail, NULL);
779 
780 static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, apds990x_rate_show,
781 						 apds990x_rate_store);
782 
783 static ssize_t apds990x_prox_show(struct device *dev,
784 				 struct device_attribute *attr, char *buf)
785 {
786 	ssize_t ret;
787 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
788 
789 	if (pm_runtime_suspended(dev) || !chip->prox_en)
790 		return -EIO;
791 
792 	mutex_lock(&chip->mutex);
793 	ret = sprintf(buf, "%d\n", chip->prox_data);
794 	mutex_unlock(&chip->mutex);
795 	return ret;
796 }
797 
798 static DEVICE_ATTR(prox0_raw, S_IRUGO, apds990x_prox_show, NULL);
799 
800 static ssize_t apds990x_prox_range_show(struct device *dev,
801 				 struct device_attribute *attr, char *buf)
802 {
803 	return sprintf(buf, "%u\n", APDS_PROX_RANGE);
804 }
805 
806 static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, apds990x_prox_range_show, NULL);
807 
808 static ssize_t apds990x_prox_enable_show(struct device *dev,
809 				   struct device_attribute *attr, char *buf)
810 {
811 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
812 
813 	return sprintf(buf, "%d\n", chip->prox_en);
814 }
815 
816 static ssize_t apds990x_prox_enable_store(struct device *dev,
817 				  struct device_attribute *attr,
818 				  const char *buf, size_t len)
819 {
820 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
821 	unsigned long value;
822 	int ret;
823 
824 	ret = kstrtoul(buf, 0, &value);
825 	if (ret)
826 		return ret;
827 
828 	mutex_lock(&chip->mutex);
829 
830 	if (!chip->prox_en)
831 		chip->prox_data = 0;
832 
833 	if (value)
834 		chip->prox_en++;
835 	else if (chip->prox_en > 0)
836 		chip->prox_en--;
837 
838 	if (!pm_runtime_suspended(dev))
839 		apds990x_mode_on(chip);
840 	mutex_unlock(&chip->mutex);
841 	return len;
842 }
843 
844 static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, apds990x_prox_enable_show,
845 						   apds990x_prox_enable_store);
846 
847 static const char *reporting_modes[] = {"trigger", "periodic"};
848 
849 static ssize_t apds990x_prox_reporting_mode_show(struct device *dev,
850 				   struct device_attribute *attr, char *buf)
851 {
852 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
853 
854 	return sprintf(buf, "%s\n",
855 		reporting_modes[!!chip->prox_continuous_mode]);
856 }
857 
858 static ssize_t apds990x_prox_reporting_mode_store(struct device *dev,
859 				  struct device_attribute *attr,
860 				  const char *buf, size_t len)
861 {
862 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
863 	int ret;
864 
865 	ret = sysfs_match_string(reporting_modes, buf);
866 	if (ret < 0)
867 		return ret;
868 
869 	chip->prox_continuous_mode = ret;
870 	return len;
871 }
872 
873 static DEVICE_ATTR(prox0_reporting_mode, S_IRUGO | S_IWUSR,
874 		apds990x_prox_reporting_mode_show,
875 		apds990x_prox_reporting_mode_store);
876 
877 static ssize_t apds990x_prox_reporting_avail_show(struct device *dev,
878 				   struct device_attribute *attr, char *buf)
879 {
880 	return sprintf(buf, "%s %s\n", reporting_modes[0], reporting_modes[1]);
881 }
882 
883 static DEVICE_ATTR(prox0_reporting_mode_avail, S_IRUGO | S_IWUSR,
884 		apds990x_prox_reporting_avail_show, NULL);
885 
886 
887 static ssize_t apds990x_lux_thresh_above_show(struct device *dev,
888 				   struct device_attribute *attr, char *buf)
889 {
890 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
891 
892 	return sprintf(buf, "%d\n", chip->lux_thres_hi);
893 }
894 
895 static ssize_t apds990x_lux_thresh_below_show(struct device *dev,
896 				   struct device_attribute *attr, char *buf)
897 {
898 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
899 
900 	return sprintf(buf, "%d\n", chip->lux_thres_lo);
901 }
902 
903 static ssize_t apds990x_set_lux_thresh(struct apds990x_chip *chip, u32 *target,
904 				const char *buf)
905 {
906 	unsigned long thresh;
907 	int ret;
908 
909 	ret = kstrtoul(buf, 0, &thresh);
910 	if (ret)
911 		return ret;
912 
913 	if (thresh > APDS_RANGE)
914 		return -EINVAL;
915 
916 	mutex_lock(&chip->mutex);
917 	*target = thresh;
918 	/*
919 	 * Don't update values in HW if we are still waiting for
920 	 * first interrupt to come after device handle open call.
921 	 */
922 	if (!chip->lux_wait_fresh_res)
923 		apds990x_refresh_athres(chip);
924 	mutex_unlock(&chip->mutex);
925 	return ret;
926 
927 }
928 
929 static ssize_t apds990x_lux_thresh_above_store(struct device *dev,
930 				  struct device_attribute *attr,
931 				  const char *buf, size_t len)
932 {
933 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
934 	int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_hi, buf);
935 
936 	if (ret < 0)
937 		return ret;
938 	return len;
939 }
940 
941 static ssize_t apds990x_lux_thresh_below_store(struct device *dev,
942 				  struct device_attribute *attr,
943 				  const char *buf, size_t len)
944 {
945 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
946 	int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_lo, buf);
947 
948 	if (ret < 0)
949 		return ret;
950 	return len;
951 }
952 
953 static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
954 		apds990x_lux_thresh_above_show,
955 		apds990x_lux_thresh_above_store);
956 
957 static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
958 		apds990x_lux_thresh_below_show,
959 		apds990x_lux_thresh_below_store);
960 
961 static ssize_t apds990x_prox_threshold_show(struct device *dev,
962 				   struct device_attribute *attr, char *buf)
963 {
964 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
965 
966 	return sprintf(buf, "%d\n", chip->prox_thres);
967 }
968 
969 static ssize_t apds990x_prox_threshold_store(struct device *dev,
970 				  struct device_attribute *attr,
971 				  const char *buf, size_t len)
972 {
973 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
974 	unsigned long value;
975 	int ret;
976 
977 	ret = kstrtoul(buf, 0, &value);
978 	if (ret)
979 		return ret;
980 
981 	if ((value > APDS_RANGE) || (value == 0) ||
982 		(value < APDS_PROX_HYSTERESIS))
983 		return -EINVAL;
984 
985 	mutex_lock(&chip->mutex);
986 	chip->prox_thres = value;
987 
988 	apds990x_force_p_refresh(chip);
989 	mutex_unlock(&chip->mutex);
990 	return len;
991 }
992 
993 static DEVICE_ATTR(prox0_thresh_above_value, S_IRUGO | S_IWUSR,
994 		apds990x_prox_threshold_show,
995 		apds990x_prox_threshold_store);
996 
997 static ssize_t apds990x_power_state_show(struct device *dev,
998 				   struct device_attribute *attr, char *buf)
999 {
1000 	return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
1001 	return 0;
1002 }
1003 
1004 static ssize_t apds990x_power_state_store(struct device *dev,
1005 				  struct device_attribute *attr,
1006 				  const char *buf, size_t len)
1007 {
1008 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
1009 	unsigned long value;
1010 	int ret;
1011 
1012 	ret = kstrtoul(buf, 0, &value);
1013 	if (ret)
1014 		return ret;
1015 
1016 	if (value) {
1017 		pm_runtime_get_sync(dev);
1018 		mutex_lock(&chip->mutex);
1019 		chip->lux_wait_fresh_res = true;
1020 		apds990x_force_a_refresh(chip);
1021 		apds990x_force_p_refresh(chip);
1022 		mutex_unlock(&chip->mutex);
1023 	} else {
1024 		if (!pm_runtime_suspended(dev))
1025 			pm_runtime_put(dev);
1026 	}
1027 	return len;
1028 }
1029 
1030 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
1031 		apds990x_power_state_show,
1032 		apds990x_power_state_store);
1033 
1034 static ssize_t apds990x_chip_id_show(struct device *dev,
1035 				   struct device_attribute *attr, char *buf)
1036 {
1037 	struct apds990x_chip *chip =  dev_get_drvdata(dev);
1038 
1039 	return sprintf(buf, "%s %d\n", chip->chipname, chip->revision);
1040 }
1041 
1042 static DEVICE_ATTR(chip_id, S_IRUGO, apds990x_chip_id_show, NULL);
1043 
1044 static struct attribute *sysfs_attrs_ctrl[] = {
1045 	&dev_attr_lux0_calibscale.attr,
1046 	&dev_attr_lux0_calibscale_default.attr,
1047 	&dev_attr_lux0_input.attr,
1048 	&dev_attr_lux0_sensor_range.attr,
1049 	&dev_attr_lux0_rate.attr,
1050 	&dev_attr_lux0_rate_avail.attr,
1051 	&dev_attr_lux0_thresh_above_value.attr,
1052 	&dev_attr_lux0_thresh_below_value.attr,
1053 	&dev_attr_prox0_raw_en.attr,
1054 	&dev_attr_prox0_raw.attr,
1055 	&dev_attr_prox0_sensor_range.attr,
1056 	&dev_attr_prox0_thresh_above_value.attr,
1057 	&dev_attr_prox0_reporting_mode.attr,
1058 	&dev_attr_prox0_reporting_mode_avail.attr,
1059 	&dev_attr_chip_id.attr,
1060 	&dev_attr_power_state.attr,
1061 	NULL
1062 };
1063 
1064 static const struct attribute_group apds990x_attribute_group[] = {
1065 	{.attrs = sysfs_attrs_ctrl },
1066 };
1067 
1068 static int apds990x_probe(struct i2c_client *client,
1069 				const struct i2c_device_id *id)
1070 {
1071 	struct apds990x_chip *chip;
1072 	int err;
1073 
1074 	chip = kzalloc(sizeof *chip, GFP_KERNEL);
1075 	if (!chip)
1076 		return -ENOMEM;
1077 
1078 	i2c_set_clientdata(client, chip);
1079 	chip->client  = client;
1080 
1081 	init_waitqueue_head(&chip->wait);
1082 	mutex_init(&chip->mutex);
1083 	chip->pdata	= client->dev.platform_data;
1084 
1085 	if (chip->pdata == NULL) {
1086 		dev_err(&client->dev, "platform data is mandatory\n");
1087 		err = -EINVAL;
1088 		goto fail1;
1089 	}
1090 
1091 	if (chip->pdata->cf.ga == 0) {
1092 		/* set uncovered sensor default parameters */
1093 		chip->cf.ga = 1966; /* 0.48 * APDS_PARAM_SCALE */
1094 		chip->cf.cf1 = 4096; /* 1.00 * APDS_PARAM_SCALE */
1095 		chip->cf.irf1 = 9134; /* 2.23 * APDS_PARAM_SCALE */
1096 		chip->cf.cf2 = 2867; /* 0.70 * APDS_PARAM_SCALE */
1097 		chip->cf.irf2 = 5816; /* 1.42 * APDS_PARAM_SCALE */
1098 		chip->cf.df = 52;
1099 	} else {
1100 		chip->cf = chip->pdata->cf;
1101 	}
1102 
1103 	/* precalculate inverse chip factors for threshold control */
1104 	chip->rcf.afactor =
1105 		(chip->cf.irf1 - chip->cf.irf2) * APDS_PARAM_SCALE /
1106 		(chip->cf.cf1 - chip->cf.cf2);
1107 	chip->rcf.cf1 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
1108 		chip->cf.cf1;
1109 	chip->rcf.irf1 = chip->cf.irf1 * APDS_PARAM_SCALE /
1110 		chip->cf.cf1;
1111 	chip->rcf.cf2 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
1112 		chip->cf.cf2;
1113 	chip->rcf.irf2 = chip->cf.irf2 * APDS_PARAM_SCALE /
1114 		chip->cf.cf2;
1115 
1116 	/* Set something to start with */
1117 	chip->lux_thres_hi = APDS_LUX_DEF_THRES_HI;
1118 	chip->lux_thres_lo = APDS_LUX_DEF_THRES_LO;
1119 	chip->lux_calib = APDS_LUX_NEUTRAL_CALIB_VALUE;
1120 
1121 	chip->prox_thres = APDS_PROX_DEF_THRES;
1122 	chip->pdrive = chip->pdata->pdrive;
1123 	chip->pdiode = APDS_PDIODE_IR;
1124 	chip->pgain = APDS_PGAIN_1X;
1125 	chip->prox_calib = APDS_PROX_NEUTRAL_CALIB_VALUE;
1126 	chip->prox_persistence = APDS_DEFAULT_PROX_PERS;
1127 	chip->prox_continuous_mode = false;
1128 
1129 	chip->regs[0].supply = reg_vcc;
1130 	chip->regs[1].supply = reg_vled;
1131 
1132 	err = regulator_bulk_get(&client->dev,
1133 				 ARRAY_SIZE(chip->regs), chip->regs);
1134 	if (err < 0) {
1135 		dev_err(&client->dev, "Cannot get regulators\n");
1136 		goto fail1;
1137 	}
1138 
1139 	err = regulator_bulk_enable(ARRAY_SIZE(chip->regs), chip->regs);
1140 	if (err < 0) {
1141 		dev_err(&client->dev, "Cannot enable regulators\n");
1142 		goto fail2;
1143 	}
1144 
1145 	usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
1146 
1147 	err = apds990x_detect(chip);
1148 	if (err < 0) {
1149 		dev_err(&client->dev, "APDS990X not found\n");
1150 		goto fail3;
1151 	}
1152 
1153 	pm_runtime_set_active(&client->dev);
1154 
1155 	apds990x_configure(chip);
1156 	apds990x_set_arate(chip, APDS_LUX_DEFAULT_RATE);
1157 	apds990x_mode_on(chip);
1158 
1159 	pm_runtime_enable(&client->dev);
1160 
1161 	if (chip->pdata->setup_resources) {
1162 		err = chip->pdata->setup_resources();
1163 		if (err) {
1164 			err = -EINVAL;
1165 			goto fail3;
1166 		}
1167 	}
1168 
1169 	err = sysfs_create_group(&chip->client->dev.kobj,
1170 				apds990x_attribute_group);
1171 	if (err < 0) {
1172 		dev_err(&chip->client->dev, "Sysfs registration failed\n");
1173 		goto fail4;
1174 	}
1175 
1176 	err = request_threaded_irq(client->irq, NULL,
1177 				apds990x_irq,
1178 				IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW |
1179 				IRQF_ONESHOT,
1180 				"apds990x", chip);
1181 	if (err) {
1182 		dev_err(&client->dev, "could not get IRQ %d\n",
1183 			client->irq);
1184 		goto fail5;
1185 	}
1186 	return err;
1187 fail5:
1188 	sysfs_remove_group(&chip->client->dev.kobj,
1189 			&apds990x_attribute_group[0]);
1190 fail4:
1191 	if (chip->pdata && chip->pdata->release_resources)
1192 		chip->pdata->release_resources();
1193 fail3:
1194 	regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1195 fail2:
1196 	regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
1197 fail1:
1198 	kfree(chip);
1199 	return err;
1200 }
1201 
1202 static int apds990x_remove(struct i2c_client *client)
1203 {
1204 	struct apds990x_chip *chip = i2c_get_clientdata(client);
1205 
1206 	free_irq(client->irq, chip);
1207 	sysfs_remove_group(&chip->client->dev.kobj,
1208 			apds990x_attribute_group);
1209 
1210 	if (chip->pdata && chip->pdata->release_resources)
1211 		chip->pdata->release_resources();
1212 
1213 	if (!pm_runtime_suspended(&client->dev))
1214 		apds990x_chip_off(chip);
1215 
1216 	pm_runtime_disable(&client->dev);
1217 	pm_runtime_set_suspended(&client->dev);
1218 
1219 	regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
1220 
1221 	kfree(chip);
1222 	return 0;
1223 }
1224 
1225 #ifdef CONFIG_PM_SLEEP
1226 static int apds990x_suspend(struct device *dev)
1227 {
1228 	struct i2c_client *client = to_i2c_client(dev);
1229 	struct apds990x_chip *chip = i2c_get_clientdata(client);
1230 
1231 	apds990x_chip_off(chip);
1232 	return 0;
1233 }
1234 
1235 static int apds990x_resume(struct device *dev)
1236 {
1237 	struct i2c_client *client = to_i2c_client(dev);
1238 	struct apds990x_chip *chip = i2c_get_clientdata(client);
1239 
1240 	/*
1241 	 * If we were enabled at suspend time, it is expected
1242 	 * everything works nice and smoothly. Chip_on is enough
1243 	 */
1244 	apds990x_chip_on(chip);
1245 
1246 	return 0;
1247 }
1248 #endif
1249 
1250 #ifdef CONFIG_PM
1251 static int apds990x_runtime_suspend(struct device *dev)
1252 {
1253 	struct i2c_client *client = to_i2c_client(dev);
1254 	struct apds990x_chip *chip = i2c_get_clientdata(client);
1255 
1256 	apds990x_chip_off(chip);
1257 	return 0;
1258 }
1259 
1260 static int apds990x_runtime_resume(struct device *dev)
1261 {
1262 	struct i2c_client *client = to_i2c_client(dev);
1263 	struct apds990x_chip *chip = i2c_get_clientdata(client);
1264 
1265 	apds990x_chip_on(chip);
1266 	return 0;
1267 }
1268 
1269 #endif
1270 
1271 static const struct i2c_device_id apds990x_id[] = {
1272 	{"apds990x", 0 },
1273 	{}
1274 };
1275 
1276 MODULE_DEVICE_TABLE(i2c, apds990x_id);
1277 
1278 static const struct dev_pm_ops apds990x_pm_ops = {
1279 	SET_SYSTEM_SLEEP_PM_OPS(apds990x_suspend, apds990x_resume)
1280 	SET_RUNTIME_PM_OPS(apds990x_runtime_suspend,
1281 			apds990x_runtime_resume,
1282 			NULL)
1283 };
1284 
1285 static struct i2c_driver apds990x_driver = {
1286 	.driver	 = {
1287 		.name	= "apds990x",
1288 		.pm	= &apds990x_pm_ops,
1289 	},
1290 	.probe	  = apds990x_probe,
1291 	.remove	  = apds990x_remove,
1292 	.id_table = apds990x_id,
1293 };
1294 
1295 module_i2c_driver(apds990x_driver);
1296 
1297 MODULE_DESCRIPTION("APDS990X combined ALS and proximity sensor");
1298 MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1299 MODULE_LICENSE("GPL v2");
1300