Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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