arm-sdk

os build toolkit for various embedded devices
git clone https://git.parazyd.org/arm-sdk
Log | Files | Refs | Submodules | README | LICENSE

0009-misc-apds990x-convert-to-iio.patch (35111B)


      1 From b8d6cddfec87f35e49d4bc54020e59a4c71dc31e Mon Sep 17 00:00:00 2001
      2 From: =?UTF-8?q?Filip=20Matijevi=C4=87?= <filip.matijevic.pz@gmail.com>
      3 Date: Sat, 10 Feb 2018 21:28:19 +0100
      4 Subject: [PATCH 09/11] misc: apds990x: convert to iio
      5 MIME-Version: 1.0
      6 Content-Type: text/plain; charset=UTF-8
      7 Content-Transfer-Encoding: 8bit
      8 
      9 Signed-off-by: Filip Matijević <filip.matijevic.pz@gmail.com>
     10 ---
     11  drivers/misc/apds990x.c | 959 +++++++++++++++++++++++++-----------------------
     12  1 file changed, 493 insertions(+), 466 deletions(-)
     13 
     14 diff --git a/drivers/misc/apds990x.c b/drivers/misc/apds990x.c
     15 index 7bb9cd76110a..363a6bf85a6c 100644
     16 --- a/drivers/misc/apds990x.c
     17 +++ b/drivers/misc/apds990x.c
     18 @@ -1,4 +1,5 @@
     19  /*
     20 + *
     21   * This file is part of the APDS990x sensor driver.
     22   * Chip is combined proximity and ambient light sensor.
     23   *
     24 @@ -35,6 +36,12 @@
     25  #include <linux/platform_data/apds990x.h>
     26  #include <linux/gpio.h>
     27  #include <linux/of_gpio.h>
     28 +#include <linux/iio/iio.h>
     29 +#include <linux/iio/sysfs.h>
     30 +#include <linux/iio/events.h>
     31 +
     32 +#define APDS990X_DRV_NAME	"apds990x"
     33 +#define APDS990X_SLEEP_DELAY_MS 3000
     34  
     35  /* Register map */
     36  #define APDS990X_ENABLE	 0x00 /* Enable of states and interrupts */
     37 @@ -130,10 +137,12 @@ struct apds990x_chip {
     38  	struct i2c_client		*client;
     39  	struct mutex			mutex; /* avoid parallel access */
     40  	struct regulator_bulk_data	regs[2];
     41 -	wait_queue_head_t		wait;
     42 +	wait_queue_head_t		wait_lux;
     43 +	wait_queue_head_t		wait_prox;
     44  
     45 -	int	prox_en;
     46 -	bool	prox_continuous_mode;
     47 +	bool    lux_en;
     48 +	bool    prox_en;
     49 +	bool	prox_wait_fresh_res;
     50  	bool	lux_wait_fresh_res;
     51  
     52  	/* Chip parameters */
     53 @@ -207,7 +216,6 @@ static int apds990x_read_byte(struct apds990x_chip *chip, u8 reg, u8 *data)
     54  
     55  	reg &= ~APDS990x_CMD_TYPE_MASK;
     56  	reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
     57 -
     58  	ret = i2c_smbus_read_byte_data(client, reg);
     59  	*data = ret;
     60  	return (int)ret;
     61 @@ -233,7 +241,6 @@ static int apds990x_write_byte(struct apds990x_chip *chip, u8 reg, u8 data)
     62  
     63  	reg &= ~APDS990x_CMD_TYPE_MASK;
     64  	reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
     65 -
     66  	ret = i2c_smbus_write_byte_data(client, reg, data);
     67  	return (int)ret;
     68  }
     69 @@ -250,18 +257,6 @@ static int apds990x_write_word(struct apds990x_chip *chip, u8 reg, u16 data)
     70  	return (int)ret;
     71  }
     72  
     73 -static int apds990x_mode_on(struct apds990x_chip *chip)
     74 -{
     75 -	/* ALS is mandatory, proximity optional */
     76 -	u8 reg = APDS990X_EN_AIEN | APDS990X_EN_PON | APDS990X_EN_AEN |
     77 -		APDS990X_EN_WEN;
     78 -
     79 -	if (chip->prox_en)
     80 -		reg |= APDS990X_EN_PIEN | APDS990X_EN_PEN;
     81 -
     82 -	return apds990x_write_byte(chip, APDS990X_ENABLE, reg);
     83 -}
     84 -
     85  static u16 apds990x_lux_to_threshold(struct apds990x_chip *chip, u32 lux)
     86  {
     87  	u32 thres;
     88 @@ -346,10 +341,7 @@ static int apds990x_refresh_pthres(struct apds990x_chip *chip, int data)
     89  		hi = chip->prox_thres;
     90  	} else {
     91  		lo = chip->prox_thres - APDS_PROX_HYSTERESIS;
     92 -		if (chip->prox_continuous_mode)
     93 -			hi = chip->prox_thres;
     94 -		else
     95 -			hi = APDS_RANGE;
     96 +		hi = APDS_RANGE;
     97  	}
     98  
     99  	ret = apds990x_write_word(chip, APDS990X_PILTL, lo);
    100 @@ -490,8 +482,10 @@ static int apds990x_ack_int(struct apds990x_chip *chip, u8 mode)
    101  
    102  static irqreturn_t apds990x_irq(int irq, void *data)
    103  {
    104 -	struct apds990x_chip *chip = data;
    105 +	struct iio_dev *indio_dev = data;
    106 +	struct apds990x_chip *chip = iio_priv(indio_dev);
    107  	u8 status;
    108 +	int ev_dir = IIO_EV_DIR_EITHER;
    109  
    110  	apds990x_read_byte(chip, APDS990X_STATUS, &status);
    111  	apds990x_ack_int(chip, status);
    112 @@ -514,14 +508,25 @@ static irqreturn_t apds990x_irq(int irq, void *data)
    113  				/* Result is valid */
    114  				chip->lux = chip->lux_raw;
    115  				chip->lux_wait_fresh_res = false;
    116 -				wake_up(&chip->wait);
    117 -				sysfs_notify(&chip->client->dev.kobj,
    118 -					NULL, "lux0_input");
    119 +				wake_up(&chip->wait_lux);
    120 +				if (chip->lux_en) {
    121 +					if (chip->lux < chip->lux_thres_lo) {
    122 +						ev_dir = IIO_EV_DIR_FALLING;
    123 +					} else if (chip->lux > chip->lux_thres_hi) {
    124 +						ev_dir = IIO_EV_DIR_RISING;
    125 +					}
    126 +					iio_push_event(indio_dev,
    127 +								IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
    128 +													IIO_EV_TYPE_THRESH,
    129 +													ev_dir),
    130 +								iio_get_time_ns(indio_dev));
    131 +				}
    132  			}
    133  		}
    134  
    135 -		if ((status & APDS990X_ST_PINT) && chip->prox_en) {
    136 +		if (status & APDS990X_ST_PINT) {
    137  			u16 clr_ch;
    138 +			bool prox_ok = false;
    139  
    140  			apds990x_read_word(chip, APDS990X_CDATAL, &clr_ch);
    141  			/*
    142 @@ -529,27 +534,58 @@ static irqreturn_t apds990x_irq(int irq, void *data)
    143  			 * proximity gives false posivite values.
    144  			 * Just ignore them.
    145  			 */
    146 -			if (chip->again_meas == 0 &&
    147 -				clr_ch == chip->a_max_result)
    148 +			if ((chip->again_meas == 0)
    149 +					&& (clr_ch == chip->a_max_result)) {
    150  				chip->prox_data = 0;
    151 -			else
    152 +			} else {
    153 +				prox_ok = true;
    154  				apds990x_read_word(chip,
    155  						APDS990X_PDATAL,
    156  						&chip->prox_data);
    157 +				if (chip->prox_data < chip->prox_thres) {
    158 +					chip->prox_data = 0;
    159 +					ev_dir = IIO_EV_DIR_FALLING;
    160 +				} else {
    161 +					chip->prox_data = APDS_PROX_RANGE;
    162 +					ev_dir = IIO_EV_DIR_RISING;
    163 +				}
    164 +				chip->prox_wait_fresh_res = false;
    165 +				wake_up(&chip->wait_prox);
    166 +			}
    167  
    168  			apds990x_refresh_pthres(chip, chip->prox_data);
    169 -			if (chip->prox_data < chip->prox_thres)
    170 -				chip->prox_data = 0;
    171 -			else if (!chip->prox_continuous_mode)
    172 -				chip->prox_data = APDS_PROX_RANGE;
    173 -			sysfs_notify(&chip->client->dev.kobj,
    174 -				NULL, "prox0_raw");
    175 +			if (prox_ok && chip->prox_en) {
    176 +				iio_push_event(indio_dev,
    177 +							IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
    178 +												IIO_EV_TYPE_THRESH,
    179 +												ev_dir),
    180 +							iio_get_time_ns(indio_dev));
    181 +			}
    182  		}
    183  	}
    184  	mutex_unlock(&chip->mutex);
    185  	return IRQ_HANDLED;
    186  }
    187  
    188 +static int apds990x_set_mode(struct apds990x_chip *chip)
    189 +{
    190 +	u8 reg = 0;
    191 +
    192 +	if (pm_runtime_suspended(&chip->client->dev))
    193 +		return 0;
    194 +
    195 +	if (chip->lux_en || chip->lux_wait_fresh_res
    196 +	    || chip->prox_en || chip->prox_wait_fresh_res) {
    197 +		reg = APDS990X_EN_PON | APDS990X_EN_WEN;
    198 +		if (chip->lux_en || chip->lux_wait_fresh_res)
    199 +			reg |= APDS990X_EN_AIEN | APDS990X_EN_AEN;
    200 +
    201 +		if (chip->prox_en || chip->prox_wait_fresh_res)
    202 +			reg |= APDS990X_EN_PIEN | APDS990X_EN_PEN;
    203 +	}
    204 +	return apds990x_write_byte(chip, APDS990X_ENABLE, reg);
    205 +}
    206 +
    207  static int apds990x_configure(struct apds990x_chip *chip)
    208  {
    209  	/* It is recommended to use disabled mode during these operations */
    210 @@ -577,6 +613,7 @@ static int apds990x_configure(struct apds990x_chip *chip)
    211  			(chip->pdiode << 4) |
    212  			(chip->pgain << 2) |
    213  			(chip->again_next << 0));
    214 +
    215  	return 0;
    216  }
    217  
    218 @@ -611,106 +648,8 @@ static int apds990x_detect(struct apds990x_chip *chip)
    219  	return ret;
    220  }
    221  
    222 -#ifdef CONFIG_PM
    223 -static int apds990x_chip_on(struct apds990x_chip *chip)
    224 -{
    225 -	int err	 = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
    226 -					chip->regs);
    227 -	if (err < 0)
    228 -		return err;
    229 -
    230 -	usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
    231 -
    232 -	/* Refresh all configs in case of regulators were off */
    233 -	chip->prox_data = 0;
    234 -	apds990x_configure(chip);
    235 -	apds990x_mode_on(chip);
    236 -	return 0;
    237 -}
    238 -#endif
    239 -
    240 -static int apds990x_chip_off(struct apds990x_chip *chip)
    241 -{
    242 -	apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
    243 -	regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
    244 -	return 0;
    245 -}
    246 -
    247 -static ssize_t apds990x_lux_show(struct device *dev,
    248 -				 struct device_attribute *attr, char *buf)
    249 -{
    250 -	struct apds990x_chip *chip = dev_get_drvdata(dev);
    251 -	ssize_t ret;
    252 -	u32 result;
    253 -	long timeout;
    254 -
    255 -	if (pm_runtime_suspended(dev))
    256 -		return -EIO;
    257 -
    258 -	timeout = wait_event_interruptible_timeout(chip->wait,
    259 -						!chip->lux_wait_fresh_res,
    260 -						msecs_to_jiffies(APDS_TIMEOUT));
    261 -	if (!timeout)
    262 -		return -EIO;
    263 -
    264 -	mutex_lock(&chip->mutex);
    265 -	result = (chip->lux * chip->lux_calib) / APDS_CALIB_SCALER;
    266 -	if (result > (APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE))
    267 -		result = APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE;
    268 -
    269 -	ret = sprintf(buf, "%d.%d\n",
    270 -		result / APDS990X_LUX_OUTPUT_SCALE,
    271 -		result % APDS990X_LUX_OUTPUT_SCALE);
    272 -	mutex_unlock(&chip->mutex);
    273 -	return ret;
    274 -}
    275 -
    276 -static DEVICE_ATTR(lux0_input, S_IRUGO, apds990x_lux_show, NULL);
    277 -
    278 -static ssize_t apds990x_lux_range_show(struct device *dev,
    279 -				 struct device_attribute *attr, char *buf)
    280 -{
    281 -	return sprintf(buf, "%u\n", APDS_RANGE);
    282 -}
    283 -
    284 -static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, apds990x_lux_range_show, NULL);
    285 -
    286 -static ssize_t apds990x_lux_calib_format_show(struct device *dev,
    287 -				 struct device_attribute *attr, char *buf)
    288 -{
    289 -	return sprintf(buf, "%u\n", APDS_CALIB_SCALER);
    290 -}
    291 -
    292 -static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
    293 -		apds990x_lux_calib_format_show, NULL);
    294 -
    295 -static ssize_t apds990x_lux_calib_show(struct device *dev,
    296 -				 struct device_attribute *attr, char *buf)
    297 -{
    298 -	struct apds990x_chip *chip = dev_get_drvdata(dev);
    299 -
    300 -	return sprintf(buf, "%u\n", chip->lux_calib);
    301 -}
    302 -
    303 -static ssize_t apds990x_lux_calib_store(struct device *dev,
    304 -				  struct device_attribute *attr,
    305 -				  const char *buf, size_t len)
    306 -{
    307 -	struct apds990x_chip *chip = dev_get_drvdata(dev);
    308 -	unsigned long value;
    309 -	int ret;
    310 -
    311 -	ret = kstrtoul(buf, 0, &value);
    312 -	if (ret)
    313 -		return ret;
    314 -
    315 -	chip->lux_calib = value;
    316 -
    317 -	return len;
    318 -}
    319 -
    320 -static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, apds990x_lux_calib_show,
    321 -		apds990x_lux_calib_store);
    322 +static IIO_CONST_ATTR(intensity_calibscale_default,
    323 +						__stringify(APDS_CALIB_SCALER));
    324  
    325  static ssize_t apds990x_rate_avail(struct device *dev,
    326  				   struct device_attribute *attr, char *buf)
    327 @@ -724,13 +663,8 @@ static ssize_t apds990x_rate_avail(struct device *dev,
    328  	return pos;
    329  }
    330  
    331 -static ssize_t apds990x_rate_show(struct device *dev,
    332 -				   struct device_attribute *attr, char *buf)
    333 -{
    334 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    335 -
    336 -	return sprintf(buf, "%d\n", chip->arate);
    337 -}
    338 +static IIO_DEVICE_ATTR(intensity_rate_avail, S_IRUGO,
    339 +						apds990x_rate_avail, NULL, 0);
    340  
    341  static int apds990x_set_arate(struct apds990x_chip *chip, int rate)
    342  {
    343 @@ -757,315 +691,420 @@ static int apds990x_set_arate(struct apds990x_chip *chip, int rate)
    344  			(chip->prox_persistence << APDS990X_PPERS_SHIFT));
    345  }
    346  
    347 -static ssize_t apds990x_rate_store(struct device *dev,
    348 -				  struct device_attribute *attr,
    349 -				  const char *buf, size_t len)
    350 +static ssize_t apds990x_set_lux_thresh(struct apds990x_chip *chip,
    351 +				u32 *target, unsigned long thresh)
    352  {
    353 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    354 -	unsigned long value;
    355 -	int ret;
    356  
    357 -	ret = kstrtoul(buf, 0, &value);
    358 -	if (ret)
    359 -		return ret;
    360 +	if (thresh > APDS_RANGE)
    361 +		return -EINVAL;
    362  
    363  	mutex_lock(&chip->mutex);
    364 -	ret = apds990x_set_arate(chip, value);
    365 +	*target = thresh;
    366 +	/*
    367 +	 * Don't update values in HW if we are still waiting for
    368 +	 * first interrupt to come after device handle open call.
    369 +	 */
    370 +	if (!chip->lux_wait_fresh_res)
    371 +		apds990x_refresh_athres(chip);
    372  	mutex_unlock(&chip->mutex);
    373 -
    374 -	if (ret < 0)
    375 -		return ret;
    376 -	return len;
    377 +	return 0;
    378  }
    379  
    380 -static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, apds990x_rate_avail, NULL);
    381 -
    382 -static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, apds990x_rate_show,
    383 -						 apds990x_rate_store);
    384 -
    385 -static ssize_t apds990x_prox_show(struct device *dev,
    386 -				 struct device_attribute *attr, char *buf)
    387 +static int apds990x_set_prox_thresh(struct apds990x_chip *chip,
    388 +									u32 thresh)
    389  {
    390 -	ssize_t ret;
    391 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    392  
    393 -	if (pm_runtime_suspended(dev) || !chip->prox_en)
    394 -		return -EIO;
    395 +	if ((thresh > APDS_RANGE) || (thresh == 0) ||
    396 +		(thresh < APDS_PROX_HYSTERESIS))
    397 +		return -EINVAL;
    398  
    399  	mutex_lock(&chip->mutex);
    400 -	ret = sprintf(buf, "%d\n", chip->prox_data);
    401 -	mutex_unlock(&chip->mutex);
    402 -	return ret;
    403 -}
    404 -
    405 -static DEVICE_ATTR(prox0_raw, S_IRUGO, apds990x_prox_show, NULL);
    406 +	chip->prox_thres = thresh;
    407  
    408 -static ssize_t apds990x_prox_range_show(struct device *dev,
    409 -				 struct device_attribute *attr, char *buf)
    410 -{
    411 -	return sprintf(buf, "%u\n", APDS_PROX_RANGE);
    412 +	apds990x_force_p_refresh(chip);
    413 +	mutex_unlock(&chip->mutex);
    414 +	return 0;
    415  }
    416  
    417 -static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, apds990x_prox_range_show, NULL);
    418 -
    419 -static ssize_t apds990x_prox_enable_show(struct device *dev,
    420 +static ssize_t apds990x_chip_id_show(struct device *dev,
    421  				   struct device_attribute *attr, char *buf)
    422  {
    423 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    424 +	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
    425 +	struct apds990x_chip *chip = iio_priv(indio_dev);
    426  
    427 -	return sprintf(buf, "%d\n", chip->prox_en);
    428 +	return sprintf(buf, "%s %d\n", chip->chipname, chip->revision);
    429  }
    430  
    431 -static ssize_t apds990x_prox_enable_store(struct device *dev,
    432 -				  struct device_attribute *attr,
    433 -				  const char *buf, size_t len)
    434 -{
    435 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    436 -	unsigned long value;
    437 -	int ret;
    438 -
    439 -	ret = kstrtoul(buf, 0, &value);
    440 -	if (ret)
    441 -		return ret;
    442 +static IIO_DEVICE_ATTR(chip_id, S_IRUGO,
    443 +						apds990x_chip_id_show, NULL, 0);
    444  
    445 -	mutex_lock(&chip->mutex);
    446 +#define APDS990X_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
    447 +#define APDS990X_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
    448  
    449 -	if (!chip->prox_en)
    450 -		chip->prox_data = 0;
    451 +static struct attribute *sysfs_attrs_ctrl[] = {
    452 +	APDS990X_CONST_ATTR(intensity_calibscale_default),
    453 +	APDS990X_DEV_ATTR(intensity_rate_avail),
    454 +	APDS990X_DEV_ATTR(chip_id),
    455 +	NULL,
    456 +};
    457  
    458 -	if (value)
    459 -		chip->prox_en++;
    460 -	else if (chip->prox_en > 0)
    461 -		chip->prox_en--;
    462 +static struct attribute_group apds990x_attribute_group = {
    463 +	.attrs = sysfs_attrs_ctrl,
    464 +};
    465  
    466 -	if (!pm_runtime_suspended(dev))
    467 -		apds990x_mode_on(chip);
    468 -	mutex_unlock(&chip->mutex);
    469 -	return len;
    470 -}
    471 +static const struct iio_event_spec apds990x_lux_event_spec[] = {
    472 +	{
    473 +		.type = IIO_EV_TYPE_THRESH,
    474 +		.dir = IIO_EV_DIR_RISING,
    475 +		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
    476 +						 BIT(IIO_EV_INFO_ENABLE),
    477 +	},
    478 +	{
    479 +		.type = IIO_EV_TYPE_THRESH,
    480 +		.dir = IIO_EV_DIR_FALLING,
    481 +		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
    482 +						 BIT(IIO_EV_INFO_ENABLE),
    483 +	},
    484 +};
    485  
    486 -static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, apds990x_prox_enable_show,
    487 -						   apds990x_prox_enable_store);
    488 +static const struct iio_event_spec apds990x_prox_event_spec[] = {
    489 +	{
    490 +		.type = IIO_EV_TYPE_THRESH,
    491 +		.dir = IIO_EV_DIR_EITHER,
    492 +		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
    493 +						 BIT(IIO_EV_INFO_ENABLE),
    494 +	},
    495 +};
    496  
    497 -static const char *reporting_modes[] = {"trigger", "periodic"};
    498 +static const struct iio_chan_spec apds990x_channels[] = {
    499 +	{
    500 +		.type = IIO_INTENSITY,
    501 +		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
    502 +							  BIT(IIO_CHAN_INFO_SCALE) |
    503 +							  BIT(IIO_CHAN_INFO_SAMP_FREQ) |
    504 +							  BIT(IIO_CHAN_INFO_CALIBSCALE),
    505 +		.event_spec = apds990x_lux_event_spec,
    506 +		.num_event_specs = ARRAY_SIZE(apds990x_lux_event_spec),
    507 +	},
    508 +	{
    509 +		.type = IIO_PROXIMITY,
    510 +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
    511 +							  BIT(IIO_CHAN_INFO_SCALE),
    512 +		.event_spec = apds990x_prox_event_spec,
    513 +		.num_event_specs = ARRAY_SIZE(apds990x_prox_event_spec),
    514 +	},
    515 +};
    516  
    517 -static ssize_t apds990x_prox_reporting_mode_show(struct device *dev,
    518 -				   struct device_attribute *attr, char *buf)
    519 +static int apds990x_set_power_state(struct apds990x_chip *chip, bool on)
    520  {
    521 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    522 +	struct device *dev = &chip->client->dev;
    523 +	int ret = 0;
    524  
    525 -	return sprintf(buf, "%s\n",
    526 -		reporting_modes[!!chip->prox_continuous_mode]);
    527 -}
    528 +	mutex_lock(&chip->mutex);
    529 +	apds990x_set_mode(chip);
    530  
    531 -static ssize_t apds990x_prox_reporting_mode_store(struct device *dev,
    532 -				  struct device_attribute *attr,
    533 -				  const char *buf, size_t len)
    534 -{
    535 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    536 -	int ret;
    537 +	if (on) {
    538 +		ret = pm_runtime_get_sync(dev);
    539 +		if (ret < 0)
    540 +			pm_runtime_put_noidle(dev);
    541 +	} else {
    542 +		pm_runtime_mark_last_busy(dev);
    543 +		ret = pm_runtime_put_autosuspend(dev);
    544 +	}
    545  
    546 -	ret = sysfs_match_string(reporting_modes, buf);
    547 -	if (ret < 0)
    548 -		return ret;
    549 +	mutex_unlock(&chip->mutex);
    550  
    551 -	chip->prox_continuous_mode = ret;
    552 -	return len;
    553 +	return ret;
    554  }
    555  
    556 -static DEVICE_ATTR(prox0_reporting_mode, S_IRUGO | S_IWUSR,
    557 -		apds990x_prox_reporting_mode_show,
    558 -		apds990x_prox_reporting_mode_store);
    559 -
    560 -static ssize_t apds990x_prox_reporting_avail_show(struct device *dev,
    561 -				   struct device_attribute *attr, char *buf)
    562 +static int apds990x_read_raw(struct iio_dev *indio_dev,
    563 +							 struct iio_chan_spec const *chan,
    564 +							 int *val, int *val2, long mask)
    565  {
    566 -	return sprintf(buf, "%s %s\n", reporting_modes[0], reporting_modes[1]);
    567 -}
    568 -
    569 -static DEVICE_ATTR(prox0_reporting_mode_avail, S_IRUGO | S_IWUSR,
    570 -		apds990x_prox_reporting_avail_show, NULL);
    571 +	struct apds990x_chip *chip = iio_priv(indio_dev);
    572 +	long timeout;
    573 +	u32 result;
    574 +	int ret;
    575  
    576 +	switch (mask) {
    577 +	case IIO_CHAN_INFO_PROCESSED:
    578 +		switch (chan->type) {
    579 +		case IIO_INTENSITY:
    580 +			if (!chip->lux_en)
    581 +				chip->lux_wait_fresh_res = true;
    582 +			apds990x_set_power_state(chip, true);
    583 +			if (chip->lux_wait_fresh_res) {
    584 +				apds990x_force_a_refresh(chip);
    585 +				timeout = wait_event_interruptible_timeout(chip->wait_lux,
    586 +							!chip->lux_wait_fresh_res,
    587 +							msecs_to_jiffies(APDS_TIMEOUT));
    588 +				if (!timeout)
    589 +					return -EIO;
    590 +			}
    591  
    592 -static ssize_t apds990x_lux_thresh_above_show(struct device *dev,
    593 -				   struct device_attribute *attr, char *buf)
    594 -{
    595 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    596 +			mutex_lock(&chip->mutex);
    597 +			result = (chip->lux * chip->lux_calib) / APDS_CALIB_SCALER;
    598 +			if (result > (APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE))
    599 +				result = APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE;
    600  
    601 -	return sprintf(buf, "%d\n", chip->lux_thres_hi);
    602 -}
    603 +			*val = result;
    604 +			*val2 = APDS990X_LUX_OUTPUT_SCALE;
    605 +			ret = IIO_VAL_FRACTIONAL;
    606 +			mutex_unlock(&chip->mutex);
    607 +			apds990x_set_power_state(chip, false);
    608 +			break;
    609 +		default:
    610 +			return -EINVAL;
    611 +		}
    612 +		break;
    613 +	case IIO_CHAN_INFO_RAW:
    614 +		switch (chan->type) {
    615 +		case IIO_PROXIMITY:
    616 +			if (!chip->prox_en)
    617 +				chip->prox_wait_fresh_res = true;
    618 +			apds990x_set_power_state(chip, true);
    619 +			if (chip->prox_wait_fresh_res) {
    620 +				apds990x_force_p_refresh(chip);
    621 +				timeout = wait_event_interruptible_timeout(chip->wait_prox,
    622 +							!chip->prox_wait_fresh_res,
    623 +							msecs_to_jiffies(APDS_TIMEOUT));
    624 +				if (!timeout)
    625 +					return -EIO;
    626 +			}
    627  
    628 -static ssize_t apds990x_lux_thresh_below_show(struct device *dev,
    629 -				   struct device_attribute *attr, char *buf)
    630 -{
    631 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    632 +			mutex_lock(&chip->mutex);
    633 +			*val = chip->prox_data;
    634 +			mutex_unlock(&chip->mutex);
    635 +			ret = IIO_VAL_INT;
    636 +			apds990x_set_power_state(chip, false);
    637 +			break;
    638 +		default:
    639 +			return -EINVAL;
    640 +		}
    641 +		break;
    642 +	case IIO_CHAN_INFO_SCALE:
    643 +		mutex_lock(&chip->mutex);
    644 +		switch (chan->type) {
    645 +		case IIO_INTENSITY:
    646 +			*val = APDS_RANGE;
    647 +			ret = IIO_VAL_INT;
    648 +			break;
    649 +		case IIO_PROXIMITY:
    650 +			*val = APDS_PROX_RANGE;
    651 +			ret = IIO_VAL_INT;
    652 +			break;
    653 +		default:
    654 +			return -EINVAL;
    655 +		}
    656 +		mutex_unlock(&chip->mutex);
    657 +		break;
    658 +	case IIO_CHAN_INFO_SAMP_FREQ:
    659 +		switch (chan->type) {
    660 +		case IIO_INTENSITY:
    661 +			*val = chip->arate;
    662 +			ret = IIO_VAL_INT;
    663 +			break;
    664 +		default:
    665 +			return -EINVAL;
    666 +		}
    667 +		break;
    668 +	case IIO_CHAN_INFO_CALIBSCALE:
    669 +		switch (chan->type) {
    670 +		case IIO_INTENSITY:
    671 +			*val = chip->lux_calib;
    672 +			ret = IIO_VAL_INT;
    673 +			break;
    674 +		default:
    675 +			return -EINVAL;
    676 +		}
    677 +		break;
    678 +	default:
    679 +		return -EINVAL;
    680 +	}
    681  
    682 -	return sprintf(buf, "%d\n", chip->lux_thres_lo);
    683 +	return ret;
    684  }
    685  
    686 -static ssize_t apds990x_set_lux_thresh(struct apds990x_chip *chip, u32 *target,
    687 -				const char *buf)
    688 +static int apds990x_write_raw(struct iio_dev *indio_dev,
    689 +							  struct iio_chan_spec const *chan,
    690 +							  int val, int val2, long mask)
    691  {
    692 -	unsigned long thresh;
    693 +	struct apds990x_chip *chip = iio_priv(indio_dev);
    694  	int ret;
    695  
    696 -	ret = kstrtoul(buf, 0, &thresh);
    697 -	if (ret)
    698 -		return ret;
    699 -
    700 -	if (thresh > APDS_RANGE)
    701 -		return -EINVAL;
    702 -
    703  	mutex_lock(&chip->mutex);
    704 -	*target = thresh;
    705 -	/*
    706 -	 * Don't update values in HW if we are still waiting for
    707 -	 * first interrupt to come after device handle open call.
    708 -	 */
    709 -	if (!chip->lux_wait_fresh_res)
    710 -		apds990x_refresh_athres(chip);
    711 -	mutex_unlock(&chip->mutex);
    712 -	return ret;
    713  
    714 -}
    715 +	switch (mask) {
    716 +	case IIO_CHAN_INFO_SAMP_FREQ:
    717 +		switch (chan->type) {
    718 +		case IIO_INTENSITY:
    719 +			if (val != 0) {
    720 +				ret = -EINVAL;
    721 +				break;
    722 +			}
    723 +			ret = apds990x_set_arate(chip, val2);
    724 +		default:
    725 +			ret = -EINVAL;
    726 +			break;
    727 +		}
    728 +		break;
    729 +	case IIO_CHAN_INFO_CALIBSCALE:
    730 +		switch (chan->type) {
    731 +		case IIO_INTENSITY:
    732 +			if (val < 0 || val > USHRT_MAX || val2 != 0){
    733 +				ret = -EINVAL;
    734 +				break;
    735 +			}
    736 +			chip->lux_calib = val;
    737 +			ret = 0;
    738 +			break;
    739 +		default:
    740 +			ret = -EINVAL;
    741 +			break;
    742 +		}
    743 +		break;
    744 +	default:
    745 +		ret = -EINVAL;
    746 +		break;
    747 +	}
    748  
    749 -static ssize_t apds990x_lux_thresh_above_store(struct device *dev,
    750 -				  struct device_attribute *attr,
    751 -				  const char *buf, size_t len)
    752 -{
    753 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    754 -	int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_hi, buf);
    755 +	mutex_unlock(&chip->mutex);
    756  
    757 -	if (ret < 0)
    758 -		return ret;
    759 -	return len;
    760 +	return ret;
    761  }
    762  
    763 -static ssize_t apds990x_lux_thresh_below_store(struct device *dev,
    764 -				  struct device_attribute *attr,
    765 -				  const char *buf, size_t len)
    766 +static int apds990x_read_event(struct iio_dev *indio_dev,
    767 +		const struct iio_chan_spec *chan, enum iio_event_type type,
    768 +		enum iio_event_direction dir, enum iio_event_info info,
    769 +		int *val, int *val2)
    770  {
    771 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    772 -	int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_lo, buf);
    773 -
    774 -	if (ret < 0)
    775 -		return ret;
    776 -	return len;
    777 -}
    778 +	struct apds990x_chip *chip = iio_priv(indio_dev);
    779  
    780 -static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
    781 -		apds990x_lux_thresh_above_show,
    782 -		apds990x_lux_thresh_above_store);
    783 -
    784 -static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
    785 -		apds990x_lux_thresh_below_show,
    786 -		apds990x_lux_thresh_below_store);
    787 -
    788 -static ssize_t apds990x_prox_threshold_show(struct device *dev,
    789 -				   struct device_attribute *attr, char *buf)
    790 -{
    791 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    792 +	switch (chan->type) {
    793 +	case IIO_INTENSITY:
    794 +		switch (dir) {
    795 +		case IIO_EV_DIR_RISING:
    796 +			*val = chip->lux_thres_hi;
    797 +			break;
    798 +		case IIO_EV_DIR_FALLING:
    799 +			*val = chip->lux_thres_lo;
    800 +			break;
    801 +		default:
    802 +			return -EINVAL;
    803 +		}
    804 +	case IIO_PROXIMITY:
    805 +		*val = chip->prox_thres;
    806 +		break;
    807 +	default:
    808 +		return -EINVAL;
    809 +	}
    810  
    811 -	return sprintf(buf, "%d\n", chip->prox_thres);
    812 +	return IIO_VAL_INT;
    813  }
    814  
    815 -static ssize_t apds990x_prox_threshold_store(struct device *dev,
    816 -				  struct device_attribute *attr,
    817 -				  const char *buf, size_t len)
    818 +static int apds990x_write_event(struct iio_dev *indio_dev,
    819 +		const struct iio_chan_spec *chan, enum iio_event_type type,
    820 +		enum iio_event_direction dir, enum iio_event_info info, int val,
    821 +		int val2)
    822  {
    823 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    824 -	unsigned long value;
    825 +	struct apds990x_chip *chip = iio_priv(indio_dev);
    826 +	u32 *thresh;
    827  	int ret;
    828  
    829 -	ret = kstrtoul(buf, 0, &value);
    830 -	if (ret)
    831 -		return ret;
    832 -
    833 -	if ((value > APDS_RANGE) || (value == 0) ||
    834 -		(value < APDS_PROX_HYSTERESIS))
    835 +	switch (chan->type) {
    836 +	case IIO_INTENSITY:
    837 +		switch (dir) {
    838 +		case IIO_EV_DIR_RISING:
    839 +			thresh = &chip->lux_thres_hi;
    840 +			break;
    841 +		case IIO_EV_DIR_FALLING:
    842 +			thresh = &chip->lux_thres_lo;
    843 +			break;
    844 +		default:
    845 +			return -EINVAL;
    846 +		}
    847 +		mutex_lock(&chip->mutex);
    848 +		ret = apds990x_set_lux_thresh(chip, thresh, val);
    849 +		mutex_unlock(&chip->mutex);
    850 +		break;
    851 +	case IIO_PROXIMITY:
    852 +		switch (dir) {
    853 +		case IIO_EV_DIR_RISING:
    854 +			mutex_lock(&chip->mutex);
    855 +			ret = apds990x_set_prox_thresh(chip, val);
    856 +			mutex_unlock(&chip->mutex);
    857 +			break;
    858 +		default:
    859 +			return -EINVAL;
    860 +		}
    861 +		break;
    862 +	default:
    863  		return -EINVAL;
    864 +	}
    865  
    866 -	mutex_lock(&chip->mutex);
    867 -	chip->prox_thres = value;
    868 -
    869 -	apds990x_force_p_refresh(chip);
    870 -	mutex_unlock(&chip->mutex);
    871 -	return len;
    872 +	return ret;
    873  }
    874  
    875 -static DEVICE_ATTR(prox0_thresh_above_value, S_IRUGO | S_IWUSR,
    876 -		apds990x_prox_threshold_show,
    877 -		apds990x_prox_threshold_store);
    878 -
    879 -static ssize_t apds990x_power_state_show(struct device *dev,
    880 -				   struct device_attribute *attr, char *buf)
    881 +static int apds990x_read_event_config(struct iio_dev *indio_dev,
    882 +					 const struct iio_chan_spec *chan,
    883 +					 enum iio_event_type type,
    884 +					 enum iio_event_direction dir)
    885  {
    886 -	return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
    887 +	struct apds990x_chip *chip = iio_priv(indio_dev);
    888 +
    889 +	switch (chan->type) {
    890 +	case IIO_INTENSITY:
    891 +		return chip->lux_en;
    892 +	case IIO_PROXIMITY:
    893 +		return chip->prox_en;
    894 +	default:
    895 +		return -EINVAL;
    896 +	}
    897 +
    898  	return 0;
    899  }
    900  
    901 -static ssize_t apds990x_power_state_store(struct device *dev,
    902 -				  struct device_attribute *attr,
    903 -				  const char *buf, size_t len)
    904 +static int apds990x_write_event_config(struct iio_dev *indio_dev,
    905 +					 const struct iio_chan_spec *chan,
    906 +					 enum iio_event_type type,
    907 +					 enum iio_event_direction dir, int state)
    908  {
    909 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    910 -	unsigned long value;
    911 -	int ret;
    912 +	struct apds990x_chip *chip = iio_priv(indio_dev);
    913  
    914 -	ret = kstrtoul(buf, 0, &value);
    915 -	if (ret)
    916 -		return ret;
    917 +	state = !!state;
    918  
    919 -	if (value) {
    920 -		pm_runtime_get_sync(dev);
    921 -		mutex_lock(&chip->mutex);
    922 -		chip->lux_wait_fresh_res = true;
    923 -		apds990x_force_a_refresh(chip);
    924 -		apds990x_force_p_refresh(chip);
    925 -		mutex_unlock(&chip->mutex);
    926 -	} else {
    927 -		if (!pm_runtime_suspended(dev))
    928 -			pm_runtime_put(dev);
    929 -	}
    930 -	return len;
    931 -}
    932 +	switch (chan->type) {
    933 +	case IIO_INTENSITY:
    934 +		if (chip->lux_en == state)
    935 +			return -EINVAL;
    936  
    937 -static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
    938 -		apds990x_power_state_show,
    939 -		apds990x_power_state_store);
    940 +		chip->lux_en = state;
    941 +		chip->lux_wait_fresh_res = true;
    942 +		apds990x_set_mode(chip);
    943 +		break;
    944 +	case IIO_PROXIMITY:
    945 +		if (chip->prox_en == state)
    946 +			return -EINVAL;
    947  
    948 -static ssize_t apds990x_chip_id_show(struct device *dev,
    949 -				   struct device_attribute *attr, char *buf)
    950 -{
    951 -	struct apds990x_chip *chip =  dev_get_drvdata(dev);
    952 +		chip->prox_en = state;
    953 +		chip->prox_wait_fresh_res = true;
    954 +		apds990x_set_mode(chip);
    955 +		break;
    956 +	default:
    957 +		return -EINVAL;
    958 +	}
    959  
    960 -	return sprintf(buf, "%s %d\n", chip->chipname, chip->revision);
    961 +	return 0;
    962  }
    963  
    964 -static DEVICE_ATTR(chip_id, S_IRUGO, apds990x_chip_id_show, NULL);
    965 -
    966 -static struct attribute *sysfs_attrs_ctrl[] = {
    967 -	&dev_attr_lux0_calibscale.attr,
    968 -	&dev_attr_lux0_calibscale_default.attr,
    969 -	&dev_attr_lux0_input.attr,
    970 -	&dev_attr_lux0_sensor_range.attr,
    971 -	&dev_attr_lux0_rate.attr,
    972 -	&dev_attr_lux0_rate_avail.attr,
    973 -	&dev_attr_lux0_thresh_above_value.attr,
    974 -	&dev_attr_lux0_thresh_below_value.attr,
    975 -	&dev_attr_prox0_raw_en.attr,
    976 -	&dev_attr_prox0_raw.attr,
    977 -	&dev_attr_prox0_sensor_range.attr,
    978 -	&dev_attr_prox0_thresh_above_value.attr,
    979 -	&dev_attr_prox0_reporting_mode.attr,
    980 -	&dev_attr_prox0_reporting_mode_avail.attr,
    981 -	&dev_attr_chip_id.attr,
    982 -	&dev_attr_power_state.attr,
    983 -	NULL
    984 -};
    985 -
    986 -static const struct attribute_group apds990x_attribute_group[] = {
    987 -	{.attrs = sysfs_attrs_ctrl },
    988 +static const struct iio_info apds990x_info = {
    989 +	.read_raw = apds990x_read_raw,
    990 +	.write_raw = apds990x_write_raw,
    991 +	.read_event_value = apds990x_read_event,
    992 +	.write_event_value = apds990x_write_event,
    993 +	.read_event_config = apds990x_read_event_config,
    994 +	.write_event_config = apds990x_write_event_config,
    995 +	.attrs = &apds990x_attribute_group,
    996  };
    997  
    998  static const int apds990x_parse_dt(struct device *dev,
    999 @@ -1132,16 +1171,27 @@ static int apds990x_probe(struct i2c_client *client,
   1000  				const struct i2c_device_id *id)
   1001  {
   1002  	struct apds990x_chip *chip;
   1003 +	struct iio_dev *indio_dev;
   1004  	int err = 0;
   1005  
   1006 -	chip = kzalloc(sizeof *chip, GFP_KERNEL);
   1007 -	if (!chip)
   1008 +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
   1009 +	if (!indio_dev)
   1010  		return -ENOMEM;
   1011  
   1012 -	i2c_set_clientdata(client, chip);
   1013 -	chip->client  = client;
   1014 +	indio_dev->info = &apds990x_info;
   1015 +	indio_dev->name = id->name;
   1016 +	indio_dev->dev.parent = &client->dev;
   1017 +	indio_dev->channels = apds990x_channels;
   1018 +	indio_dev->num_channels = ARRAY_SIZE(apds990x_channels);
   1019 +	indio_dev->modes = INDIO_DIRECT_MODE;
   1020  
   1021 -	init_waitqueue_head(&chip->wait);
   1022 +	chip = iio_priv(indio_dev);
   1023 +	i2c_set_clientdata(client, indio_dev);
   1024 +
   1025 +	chip->client = client;
   1026 +
   1027 +	init_waitqueue_head(&chip->wait_lux);
   1028 +	init_waitqueue_head(&chip->wait_prox);
   1029  	mutex_init(&chip->mutex);
   1030  	chip->pdata	= client->dev.platform_data;
   1031  
   1032 @@ -1157,8 +1207,7 @@ static int apds990x_probe(struct i2c_client *client,
   1033  
   1034  	if (chip->pdata == NULL) {
   1035  		dev_err(&client->dev, "platform data is mandatory\n");
   1036 -		err = -EINVAL;
   1037 -		goto fail1;
   1038 +		return -EINVAL;
   1039  	}
   1040  
   1041  	if (chip->pdata->cf.ga == 0) {
   1042 @@ -1197,22 +1246,23 @@ static int apds990x_probe(struct i2c_client *client,
   1043  	chip->pgain = APDS_PGAIN_1X;
   1044  	chip->prox_calib = APDS_PROX_NEUTRAL_CALIB_VALUE;
   1045  	chip->prox_persistence = APDS_DEFAULT_PROX_PERS;
   1046 -	chip->prox_continuous_mode = false;
   1047  
   1048  	chip->regs[0].supply = reg_vcc;
   1049  	chip->regs[1].supply = reg_vled;
   1050  
   1051 -	err = regulator_bulk_get(&client->dev,
   1052 +	chip->lux_en = true;
   1053 +
   1054 +	err = devm_regulator_bulk_get(&client->dev,
   1055  				 ARRAY_SIZE(chip->regs), chip->regs);
   1056  	if (err < 0) {
   1057  		dev_err(&client->dev, "Cannot get regulators\n");
   1058 -		goto fail1;
   1059 +		return err;
   1060  	}
   1061  
   1062  	err = regulator_bulk_enable(ARRAY_SIZE(chip->regs), chip->regs);
   1063  	if (err < 0) {
   1064  		dev_err(&client->dev, "Cannot enable regulators\n");
   1065 -		goto fail2;
   1066 +		return err;
   1067  	}
   1068  
   1069  	usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
   1070 @@ -1220,141 +1270,119 @@ static int apds990x_probe(struct i2c_client *client,
   1071  	err = apds990x_detect(chip);
   1072  	if (err < 0) {
   1073  		dev_err(&client->dev, "APDS990X not found\n");
   1074 -		goto fail3;
   1075 +		goto fail1;
   1076  	}
   1077  
   1078 -	pm_runtime_set_active(&client->dev);
   1079 -
   1080 -	apds990x_configure(chip);
   1081 -	apds990x_set_arate(chip, APDS_LUX_DEFAULT_RATE);
   1082 -	apds990x_mode_on(chip);
   1083 -
   1084  	pm_runtime_enable(&client->dev);
   1085 +	pm_runtime_set_autosuspend_delay(&client->dev,
   1086 +									APDS990X_SLEEP_DELAY_MS);
   1087 +	pm_runtime_use_autosuspend(&client->dev);
   1088  
   1089  	if (chip->pdata->setup_resources) {
   1090  		err = chip->pdata->setup_resources();
   1091  		if (err) {
   1092  			err = -EINVAL;
   1093 -			goto fail3;
   1094 +			goto fail1;
   1095  		}
   1096  	}
   1097  
   1098 -	err = sysfs_create_group(&chip->client->dev.kobj,
   1099 -				apds990x_attribute_group);
   1100 -	if (err < 0) {
   1101 -		dev_err(&chip->client->dev, "Sysfs registration failed\n");
   1102 -		goto fail4;
   1103 -	}
   1104 -
   1105 -	err = request_threaded_irq(client->irq, NULL,
   1106 -				apds990x_irq,
   1107 -				IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW |
   1108 -				IRQF_ONESHOT,
   1109 -				"apds990x", chip);
   1110 +	err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
   1111 +				apds990x_irq, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW |
   1112 +				IRQF_ONESHOT, "apds990x", indio_dev);
   1113  	if (err) {
   1114  		dev_err(&client->dev, "could not get IRQ %d\n",
   1115  			client->irq);
   1116 -		goto fail5;
   1117 +		goto fail2;
   1118  	}
   1119 -	return err;
   1120 -fail5:
   1121 -	sysfs_remove_group(&chip->client->dev.kobj,
   1122 -			&apds990x_attribute_group[0]);
   1123 -fail4:
   1124 +
   1125 +	err = devm_iio_device_register(&client->dev, indio_dev);
   1126 +	if (err)
   1127 +		goto fail2;
   1128 +
   1129 +	return 0;
   1130 +fail2:
   1131  	if (chip->pdata && chip->pdata->release_resources)
   1132  		chip->pdata->release_resources();
   1133 -fail3:
   1134 -	regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
   1135 -fail2:
   1136 -	regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
   1137  fail1:
   1138 -	kfree(chip);
   1139 +	regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
   1140 +
   1141  	return err;
   1142  }
   1143  
   1144  static int apds990x_remove(struct i2c_client *client)
   1145  {
   1146 -	struct apds990x_chip *chip = i2c_get_clientdata(client);
   1147 -
   1148 -	free_irq(client->irq, chip);
   1149 -	sysfs_remove_group(&chip->client->dev.kobj,
   1150 -			apds990x_attribute_group);
   1151 +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
   1152 +	struct apds990x_chip *chip = iio_priv(indio_dev);
   1153  
   1154  	if (chip->pdata && chip->pdata->release_resources)
   1155  		chip->pdata->release_resources();
   1156  
   1157 -	if (!pm_runtime_suspended(&client->dev))
   1158 -		apds990x_chip_off(chip);
   1159 -
   1160  	pm_runtime_disable(&client->dev);
   1161  	pm_runtime_set_suspended(&client->dev);
   1162 +	regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
   1163  
   1164 -	regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
   1165 -
   1166 -	kfree(chip);
   1167  	return 0;
   1168  }
   1169  
   1170 -#ifdef CONFIG_PM_SLEEP
   1171 -static int apds990x_suspend(struct device *dev)
   1172 +static int apds990x_set_power(struct apds990x_chip *chip, bool on)
   1173  {
   1174 -	struct i2c_client *client = to_i2c_client(dev);
   1175 -	struct apds990x_chip *chip = i2c_get_clientdata(client);
   1176 +	int ret = 0;
   1177  
   1178 -	apds990x_chip_off(chip);
   1179 -	return 0;
   1180 -}
   1181 +	if (on) {
   1182 +		ret = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
   1183 +					chip->regs);
   1184 +		if (ret < 0)
   1185 +			return ret;
   1186  
   1187 -static int apds990x_resume(struct device *dev)
   1188 -{
   1189 -	struct i2c_client *client = to_i2c_client(dev);
   1190 -	struct apds990x_chip *chip = i2c_get_clientdata(client);
   1191 +		usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
   1192  
   1193 -	/*
   1194 -	 * If we were enabled at suspend time, it is expected
   1195 -	 * everything works nice and smoothly. Chip_on is enough
   1196 -	 */
   1197 -	apds990x_chip_on(chip);
   1198 +		apds990x_configure(chip);
   1199 +		apds990x_set_arate(chip, APDS_LUX_DEFAULT_RATE);
   1200  
   1201 -	return 0;
   1202 +		chip->lux_wait_fresh_res = true;
   1203 +		chip->prox_wait_fresh_res = true;
   1204 +
   1205 +		apds990x_set_mode(chip);
   1206 +
   1207 +	} else {
   1208 +		ret = regulator_bulk_disable(ARRAY_SIZE(chip->regs),
   1209 +					chip->regs);
   1210 +	}
   1211 +
   1212 +	return ret;
   1213  }
   1214 -#endif
   1215  
   1216 -#ifdef CONFIG_PM
   1217  static int apds990x_runtime_suspend(struct device *dev)
   1218  {
   1219 -	struct i2c_client *client = to_i2c_client(dev);
   1220 -	struct apds990x_chip *chip = i2c_get_clientdata(client);
   1221 +	struct apds990x_chip *chip =
   1222 +					iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
   1223  
   1224 -	apds990x_chip_off(chip);
   1225 +	apds990x_set_power(chip, false);
   1226  	return 0;
   1227  }
   1228  
   1229  static int apds990x_runtime_resume(struct device *dev)
   1230  {
   1231 -	struct i2c_client *client = to_i2c_client(dev);
   1232 -	struct apds990x_chip *chip = i2c_get_clientdata(client);
   1233 +	struct apds990x_chip *chip =
   1234 +					iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
   1235  
   1236 -	apds990x_chip_on(chip);
   1237 +	apds990x_set_power(chip, true);
   1238  	return 0;
   1239  }
   1240  
   1241 -#endif
   1242 +static const struct dev_pm_ops apds990x_pm_ops = {
   1243 +	SET_SYSTEM_SLEEP_PM_OPS(apds990x_runtime_suspend,
   1244 +							apds990x_runtime_resume)
   1245 +	SET_RUNTIME_PM_OPS(apds990x_runtime_suspend,
   1246 +					   apds990x_runtime_resume, NULL)
   1247 +};
   1248  
   1249  static const struct i2c_device_id apds990x_id[] = {
   1250  	{"apds990x", 0 },
   1251  	{}
   1252  };
   1253 -
   1254  MODULE_DEVICE_TABLE(i2c, apds990x_id);
   1255  
   1256 -static const struct dev_pm_ops apds990x_pm_ops = {
   1257 -	SET_SYSTEM_SLEEP_PM_OPS(apds990x_suspend, apds990x_resume)
   1258 -	SET_RUNTIME_PM_OPS(apds990x_runtime_suspend,
   1259 -			apds990x_runtime_resume,
   1260 -			NULL)
   1261 -};
   1262 -
   1263  static const struct of_device_id apds990x_of_match[] = {
   1264  	{.compatible = "avago,apds990x" },
   1265  	{}
   1266 @@ -1363,7 +1391,7 @@ MODULE_DEVICE_TABLE(of, apds990x_of_match);
   1267  
   1268  static struct i2c_driver apds990x_driver = {
   1269  	.driver	 = {
   1270 -		.name	= "apds990x",
   1271 +		.name	= APDS990X_DRV_NAME,
   1272  		.of_match_table	= apds990x_of_match,
   1273  		.pm	= &apds990x_pm_ops,
   1274  	},
   1275 @@ -1371,7 +1399,6 @@ static struct i2c_driver apds990x_driver = {
   1276  	.remove	  = apds990x_remove,
   1277  	.id_table = apds990x_id,
   1278  };
   1279 -
   1280  module_i2c_driver(apds990x_driver);
   1281  
   1282  MODULE_DESCRIPTION("APDS990X combined ALS and proximity sensor");
   1283 -- 
   1284 2.14.1
   1285