Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1515677

Re: [PATCH 1/9] staging: iio: tsl2583: i2c_smbus_write_byte() / i2c_smbus_read_byte() migration

From Jonathan Cameron <jic23@kernel.org>
Newsgroups linux.kernel
Subject Re: [PATCH 1/9] staging: iio: tsl2583: i2c_smbus_write_byte() / i2c_smbus_read_byte() migration
Date 2016-11-06 12:40 +0100
Message-ID <sAtXQ-4Qy-29@gated-at.bofh.it> (permalink)
References <szpMC-3XC-3@gated-at.bofh.it> <szpMC-3XC-29@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On 03/11/16 12:56, Brian Masney wrote:
> There were several places where the driver would first call
> i2c_smbus_write_byte() to select the register on the device, and then
> call i2c_smbus_read_byte() to get the contents of that register. The
> code would look roughly like:
> 
> /* Select register */
> i2c_smbus_write_byte(client, REGISTER);
> 
> /* Read the the last register that was written to */
> int data = i2c_smbus_read_byte(client);
> 
> Rewrite this to use i2c_smbus_read_byte_data() to combine the two
> calls into one:
> 
> int data = i2c_smbus_read_byte_data(chip->client, REGISTER);
> 
> Verified that the driver still functions correctly using a TSL2581
> hooked up to a Raspberry Pi 2.
> 
> This fixes the following warnings that were found by the
> kbuild test robot that were introduced by commit 8ba355cce3c6 ("staging:
> iio: tsl2583: check for error code from i2c_smbus_read_byte()").
> 
> drivers/staging/iio/light/tsl2583.c:365:5-12: WARNING: Unsigned
> expression compared with zero: reg_val < 0
> 
> drivers/staging/iio/light/tsl2583.c:388:5-12: WARNING: Unsigned
> expression compared with zero: reg_val < 0
> 
> This also removes the need for the taos_i2c_read() function since all
> callers were only calling the function with a length of 1.
> 
> Signed-off-by: Brian Masney <masneyb@onstation.org>
> Cc: Julia Lawall <julia.lawall@lip6.fr>
Forgot to say - applied to the togreg branch of iio.git and pushed
out as testing to see what we've missed!

Jonathan
> ---
>  drivers/staging/iio/light/tsl2583.c | 85 +++++++------------------------------
>  1 file changed, 16 insertions(+), 69 deletions(-)
> 
> diff --git a/drivers/staging/iio/light/tsl2583.c b/drivers/staging/iio/light/tsl2583.c
> index 49b19f5..a3a9095 100644
> --- a/drivers/staging/iio/light/tsl2583.c
> +++ b/drivers/staging/iio/light/tsl2583.c
> @@ -155,39 +155,6 @@ static void taos_defaults(struct tsl2583_chip *chip)
>  }
>  
>  /*
> - * Read a number of bytes starting at register (reg) location.
> - * Return 0, or i2c_smbus_write_byte ERROR code.
> - */
> -static int
> -taos_i2c_read(struct i2c_client *client, u8 reg, u8 *val, unsigned int len)
> -{
> -	int i, ret;
> -
> -	for (i = 0; i < len; i++) {
> -		/* select register to write */
> -		ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | reg));
> -		if (ret < 0) {
> -			dev_err(&client->dev,
> -				"taos_i2c_read failed to write register %x\n",
> -				reg);
> -			return ret;
> -		}
> -		/* read the data */
> -		ret = i2c_smbus_read_byte(client);
> -		if (ret < 0) {
> -			dev_err(&client->dev,
> -				"%s failed to read byte after writing to register %x\n",
> -				__func__, reg);
> -			return ret;
> -		}
> -		*val = ret;
> -		val++;
> -		reg++;
> -	}
> -	return 0;
> -}
> -
> -/*
>   * Reads and calculates current lux value.
>   * The raw ch0 and ch1 values of the ambient light sensed in the last
>   * integration cycle are read from the device.
> @@ -220,13 +187,14 @@ static int taos_get_lux(struct iio_dev *indio_dev)
>  		goto done;
>  	}
>  
> -	ret = taos_i2c_read(chip->client, (TSL258X_CMD_REG), &buf[0], 1);
> +	ret = i2c_smbus_read_byte_data(chip->client, TSL258X_CMD_REG);
>  	if (ret < 0) {
>  		dev_err(&chip->client->dev, "taos_get_lux failed to read CMD_REG\n");
>  		goto done;
>  	}
> +
>  	/* is data new & valid */
> -	if (!(buf[0] & TSL258X_STA_ADC_INTR)) {
> +	if (!(ret & TSL258X_STA_ADC_INTR)) {
>  		dev_err(&chip->client->dev, "taos_get_lux data not valid\n");
>  		ret = chip->als_cur_info.lux; /* return LAST VALUE */
>  		goto done;
> @@ -235,13 +203,14 @@ static int taos_get_lux(struct iio_dev *indio_dev)
>  	for (i = 0; i < 4; i++) {
>  		int reg = TSL258X_CMD_REG | (TSL258X_ALS_CHAN0LO + i);
>  
> -		ret = taos_i2c_read(chip->client, reg, &buf[i], 1);
> +		ret = i2c_smbus_read_byte_data(chip->client, reg);
>  		if (ret < 0) {
>  			dev_err(&chip->client->dev,
>  				"taos_get_lux failed to read register %x\n",
>  				reg);
>  			goto done;
>  		}
> +		buf[i] = ret;
>  	}
>  
>  	/*
> @@ -343,52 +312,36 @@ static int taos_get_lux(struct iio_dev *indio_dev)
>  static int taos_als_calibrate(struct iio_dev *indio_dev)
>  {
>  	struct tsl2583_chip *chip = iio_priv(indio_dev);
> -	u8 reg_val;
>  	unsigned int gain_trim_val;
>  	int ret;
>  	int lux_val;
>  
> -	ret = i2c_smbus_write_byte(chip->client,
> -				   (TSL258X_CMD_REG | TSL258X_CNTRL));
> +	ret = i2c_smbus_read_byte_data(chip->client,
> +				       TSL258X_CMD_REG | TSL258X_CNTRL);
>  	if (ret < 0) {
>  		dev_err(&chip->client->dev,
> -			"taos_als_calibrate failed to reach the CNTRL register, ret=%d\n",
> -			ret);
> -		return ret;
> -	}
> -
> -	reg_val = i2c_smbus_read_byte(chip->client);
> -	if (reg_val < 0) {
> -		dev_err(&chip->client->dev,
> -			"%s failed to read after writing to the CNTRL register\n",
> +			"%s failed to read from the CNTRL register\n",
>  			__func__);
>  		return ret;
>  	}
>  
> -	if ((reg_val & (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON))
> +	if ((ret & (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON))
>  			!= (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON)) {
>  		dev_err(&chip->client->dev,
>  			"taos_als_calibrate failed: device not powered on with ADC enabled\n");
>  		return -EINVAL;
>  	}
>  
> -	ret = i2c_smbus_write_byte(chip->client,
> -				   (TSL258X_CMD_REG | TSL258X_CNTRL));
> +	ret = i2c_smbus_read_byte_data(chip->client,
> +				       TSL258X_CMD_REG | TSL258X_CNTRL);
>  	if (ret < 0) {
>  		dev_err(&chip->client->dev,
> -			"taos_als_calibrate failed to reach the STATUS register, ret=%d\n",
> -			ret);
> -		return ret;
> -	}
> -	reg_val = i2c_smbus_read_byte(chip->client);
> -	if (reg_val < 0) {
> -		dev_err(&chip->client->dev,
> -			"%s failed to read after writing to the STATUS register\n",
> +			"%s failed to read from the CNTRL register\n",
>  			__func__);
>  		return ret;
>  	}
>  
> -	if ((reg_val & TSL258X_STA_ADC_VALID) != TSL258X_STA_ADC_VALID) {
> +	if ((ret & TSL258X_STA_ADC_VALID) != TSL258X_STA_ADC_VALID) {
>  		dev_err(&chip->client->dev,
>  			"taos_als_calibrate failed: STATUS - ADC not valid.\n");
>  		return -ENODATA;
> @@ -847,15 +800,9 @@ static int taos_probe(struct i2c_client *clientp,
>  	memcpy(chip->taos_config, taos_config, sizeof(chip->taos_config));
>  
>  	for (i = 0; i < TSL258X_MAX_DEVICE_REGS; i++) {
> -		ret = i2c_smbus_write_byte(clientp,
> -				(TSL258X_CMD_REG | (TSL258X_CNTRL + i)));
> -		if (ret < 0) {
> -			dev_err(&clientp->dev,
> -				"i2c_smbus_write_byte to cmd reg failed in taos_probe(), err = %d\n",
> -				ret);
> -			return ret;
> -		}
> -		ret = i2c_smbus_read_byte(clientp);
> +		ret = i2c_smbus_read_byte_data(clientp,
> +					       (TSL258X_CMD_REG |
> +						(TSL258X_CNTRL + i)));
>  		if (ret < 0) {
>  			dev_err(&clientp->dev,
>  				"i2c_smbus_read_byte from reg failed in taos_probe(), err = %d\n",
> 

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 0/9] staging: iio: tsl2583: i2c cleanups Brian Masney <masneyb@onstation.org> - 2016-11-03 14:00 +0100
  [PATCH 8/9] staging: iio: tsl2583: check if chip is suspended in in_illuminance_calibrate_store Brian Masney <masneyb@onstation.org> - 2016-11-03 14:00 +0100
    Re: [PATCH 8/9] staging: iio: tsl2583: check if chip is suspended in  in_illuminance_calibrate_store Jonathan Cameron <jic23@kernel.org> - 2016-11-06 13:10 +0100
  [PATCH 2/9] staging: iio: tsl2583: removed unused code from device probing Brian Masney <masneyb@onstation.org> - 2016-11-03 14:00 +0100
    Re: [PATCH 2/9] staging: iio: tsl2583: removed unused code from  device probing Jonathan Cameron <jic23@kernel.org> - 2016-11-06 12:50 +0100
  [PATCH 6/9] staging: iio: tsl2583: remove redudant i2c call in taos_als_calibrate() Brian Masney <masneyb@onstation.org> - 2016-11-03 14:00 +0100
    Re: [PATCH 6/9] staging: iio: tsl2583: remove redudant i2c call in  taos_als_calibrate() Jonathan Cameron <jic23@kernel.org> - 2016-11-06 13:00 +0100
  [PATCH 7/9] staging: iio: tsl2583: fix issue with changes to calibscale and int_time not being set on the chip Brian Masney <masneyb@onstation.org> - 2016-11-03 14:00 +0100
    Re: [PATCH 7/9] staging: iio: tsl2583: fix issue with changes to  calibscale and int_time not being set on the chip Jonathan Cameron <jic23@kernel.org> - 2016-11-06 13:20 +0100
      Re: [PATCH 7/9] staging: iio: tsl2583: fix issue with changes to  calibscale and int_time not being set on the chip Brian Masney <masneyb@onstation.org> - 2016-11-06 15:30 +0100
        Re: [PATCH 7/9] staging: iio: tsl2583: fix issue with changes to  calibscale and int_time not being set on the chip Jonathan Cameron <jic23@kernel.org> - 2016-11-06 19:00 +0100
  [PATCH 1/9] staging: iio: tsl2583: i2c_smbus_write_byte() / i2c_smbus_read_byte() migration Brian Masney <masneyb@onstation.org> - 2016-11-03 14:00 +0100
    Re: [PATCH 1/9] staging: iio: tsl2583: i2c_smbus_write_byte() /  i2c_smbus_read_byte() migration Jonathan Cameron <jic23@kernel.org> - 2016-11-06 12:40 +0100
    Re: [PATCH 1/9] staging: iio: tsl2583: i2c_smbus_write_byte() /  i2c_smbus_read_byte() migration Jonathan Cameron <jic23@kernel.org> - 2016-11-06 12:40 +0100
  [PATCH 4/9] staging: iio: tsl2583: remove redundant power off sequence in taos_chip_on() Brian Masney <masneyb@onstation.org> - 2016-11-03 14:00 +0100
    Re: [PATCH 4/9] staging: iio: tsl2583: remove redundant power off  sequence in taos_chip_on() Jonathan Cameron <jic23@kernel.org> - 2016-11-06 13:00 +0100
  [PATCH 5/9] staging: iio: tsl2583: don't shutdown chip when updating the lux table Brian Masney <masneyb@onstation.org> - 2016-11-03 14:00 +0100
    Re: [PATCH 5/9] staging: iio: tsl2583: don't shutdown chip when  updating the lux table Jonathan Cameron <jic23@kernel.org> - 2016-11-06 13:00 +0100
  [PATCH 9/9] staging: iio: tsl2583: remove redundant write to the control register in taos_probe() Brian Masney <masneyb@onstation.org> - 2016-11-03 14:00 +0100
    Re: [PATCH 9/9] staging: iio: tsl2583: remove redundant write to the  control register in taos_probe() Jonathan Cameron <jic23@kernel.org> - 2016-11-06 13:10 +0100

csiph-web