diff mbox series

[v2] media: i2c: adv7511: remove open coded version of SMBus block read

Message ID 20210127103357.5045-1-wsa+renesas@sang-engineering.com (mailing list archive)
State Mainlined
Commit eea62d6d471a08fe1a18c6c4ddccfde24a005beb
Delegated to: Kieran Bingham
Headers show
Series [v2] media: i2c: adv7511: remove open coded version of SMBus block read | expand

Commit Message

Wolfram Sang Jan. 27, 2021, 10:33 a.m. UTC
The open coded version differs from the one in the core in one way: the
buffer will be always copied back, even when the transfer failed. Be
more robust: use the block read from the I2C core and propagate a
potential errno further to the sanity checks.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---

Changes since v1:
* respect 'err' in more code paths
* updated comment

 drivers/media/i2c/adv7511-v4l2.c | 58 ++++++++++++++------------------
 1 file changed, 26 insertions(+), 32 deletions(-)

Comments

Hans Verkuil Feb. 23, 2021, 12:01 p.m. UTC | #1
Hi Wolfram,

On 27/01/2021 11:33, Wolfram Sang wrote:
> The open coded version differs from the one in the core in one way: the
> buffer will be always copied back, even when the transfer failed. Be
> more robust: use the block read from the I2C core and propagate a
> potential errno further to the sanity checks.
> 
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

This looks good.

If you want to merge this, then you can add my:

Acked-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>

If you want me to take it, then just let me know and I'll queue it up for 5.13.

Regards,

	Hans

> ---
> 
> Changes since v1:
> * respect 'err' in more code paths
> * updated comment
> 
>  drivers/media/i2c/adv7511-v4l2.c | 58 ++++++++++++++------------------
>  1 file changed, 26 insertions(+), 32 deletions(-)
> 
> diff --git a/drivers/media/i2c/adv7511-v4l2.c b/drivers/media/i2c/adv7511-v4l2.c
> index a3161d709015..9183003ae22d 100644
> --- a/drivers/media/i2c/adv7511-v4l2.c
> +++ b/drivers/media/i2c/adv7511-v4l2.c
> @@ -214,36 +214,25 @@ static inline void adv7511_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask
>  	adv7511_wr(sd, reg, (adv7511_rd(sd, reg) & clr_mask) | val_mask);
>  }
>  
> -static int adv_smbus_read_i2c_block_data(struct i2c_client *client,
> -					 u8 command, unsigned length, u8 *values)
> -{
> -	union i2c_smbus_data data;
> -	int ret;
> -
> -	if (length > I2C_SMBUS_BLOCK_MAX)
> -		length = I2C_SMBUS_BLOCK_MAX;
> -	data.block[0] = length;
> -
> -	ret = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
> -			     I2C_SMBUS_READ, command,
> -			     I2C_SMBUS_I2C_BLOCK_DATA, &data);
> -	memcpy(values, data.block + 1, length);
> -	return ret;
> -}
> -
> -static void adv7511_edid_rd(struct v4l2_subdev *sd, uint16_t len, uint8_t *buf)
> +static int adv7511_edid_rd(struct v4l2_subdev *sd, uint16_t len, uint8_t *buf)
>  {
>  	struct adv7511_state *state = get_adv7511_state(sd);
>  	int i;
> -	int err = 0;
>  
>  	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
>  
> -	for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
> -		err = adv_smbus_read_i2c_block_data(state->i2c_edid, i,
> +	for (i = 0; i < len; i += I2C_SMBUS_BLOCK_MAX) {
> +		s32 ret;
> +
> +		ret = i2c_smbus_read_i2c_block_data(state->i2c_edid, i,
>  						    I2C_SMBUS_BLOCK_MAX, buf + i);
> -	if (err)
> -		v4l2_err(sd, "%s: i2c read error\n", __func__);
> +		if (ret < 0) {
> +			v4l2_err(sd, "%s: i2c read error\n", __func__);
> +			return ret;
> +		}
> +	}
> +
> +	return 0;
>  }
>  
>  static inline int adv7511_cec_read(struct v4l2_subdev *sd, u8 reg)
> @@ -1668,22 +1657,27 @@ static bool adv7511_check_edid_status(struct v4l2_subdev *sd)
>  	if (edidRdy & MASK_ADV7511_EDID_RDY) {
>  		int segment = adv7511_rd(sd, 0xc4);
>  		struct adv7511_edid_detect ed;
> +		int err;
>  
>  		if (segment >= EDID_MAX_SEGM) {
>  			v4l2_err(sd, "edid segment number too big\n");
>  			return false;
>  		}
>  		v4l2_dbg(1, debug, sd, "%s: got segment %d\n", __func__, segment);
> -		adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]);
> -		adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]);
> -		if (segment == 0) {
> -			state->edid.blocks = state->edid.data[0x7e] + 1;
> -			v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n", __func__, state->edid.blocks);
> +		err = adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]);
> +		if (!err) {
> +			adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]);
> +			if (segment == 0) {
> +				state->edid.blocks = state->edid.data[0x7e] + 1;
> +				v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n",
> +					 __func__, state->edid.blocks);
> +			}
>  		}
> -		if (!edid_verify_crc(sd, segment) ||
> -		    !edid_verify_header(sd, segment)) {
> -			/* edid crc error, force reread of edid segment */
> -			v4l2_err(sd, "%s: edid crc or header error\n", __func__);
> +
> +		if (err || !edid_verify_crc(sd, segment) || !edid_verify_header(sd, segment)) {
> +			/* Couldn't read EDID or EDID is invalid. Force retry! */
> +			if (!err)
> +				v4l2_err(sd, "%s: edid crc or header error\n", __func__);
>  			state->have_monitor = false;
>  			adv7511_s_power(sd, false);
>  			adv7511_s_power(sd, true);
>
Hans Verkuil March 4, 2021, 10:51 a.m. UTC | #2
Hi Wolfram,

On 23/02/2021 13:01, Hans Verkuil wrote:
> Hi Wolfram,
> 
> On 27/01/2021 11:33, Wolfram Sang wrote:
>> The open coded version differs from the one in the core in one way: the
>> buffer will be always copied back, even when the transfer failed. Be
>> more robust: use the block read from the I2C core and propagate a
>> potential errno further to the sanity checks.
>>
>> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
> 
> This looks good.
> 
> If you want to merge this, then you can add my:
> 
> Acked-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
> 
> If you want me to take it, then just let me know and I'll queue it up for 5.13.

I didn't hear back from you, so I'll pick it up for 5.13.

Regards,

	Hans

> 
> Regards,
> 
> 	Hans
> 
>> ---
>>
>> Changes since v1:
>> * respect 'err' in more code paths
>> * updated comment
>>
>>  drivers/media/i2c/adv7511-v4l2.c | 58 ++++++++++++++------------------
>>  1 file changed, 26 insertions(+), 32 deletions(-)
>>
>> diff --git a/drivers/media/i2c/adv7511-v4l2.c b/drivers/media/i2c/adv7511-v4l2.c
>> index a3161d709015..9183003ae22d 100644
>> --- a/drivers/media/i2c/adv7511-v4l2.c
>> +++ b/drivers/media/i2c/adv7511-v4l2.c
>> @@ -214,36 +214,25 @@ static inline void adv7511_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask
>>  	adv7511_wr(sd, reg, (adv7511_rd(sd, reg) & clr_mask) | val_mask);
>>  }
>>  
>> -static int adv_smbus_read_i2c_block_data(struct i2c_client *client,
>> -					 u8 command, unsigned length, u8 *values)
>> -{
>> -	union i2c_smbus_data data;
>> -	int ret;
>> -
>> -	if (length > I2C_SMBUS_BLOCK_MAX)
>> -		length = I2C_SMBUS_BLOCK_MAX;
>> -	data.block[0] = length;
>> -
>> -	ret = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
>> -			     I2C_SMBUS_READ, command,
>> -			     I2C_SMBUS_I2C_BLOCK_DATA, &data);
>> -	memcpy(values, data.block + 1, length);
>> -	return ret;
>> -}
>> -
>> -static void adv7511_edid_rd(struct v4l2_subdev *sd, uint16_t len, uint8_t *buf)
>> +static int adv7511_edid_rd(struct v4l2_subdev *sd, uint16_t len, uint8_t *buf)
>>  {
>>  	struct adv7511_state *state = get_adv7511_state(sd);
>>  	int i;
>> -	int err = 0;
>>  
>>  	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
>>  
>> -	for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
>> -		err = adv_smbus_read_i2c_block_data(state->i2c_edid, i,
>> +	for (i = 0; i < len; i += I2C_SMBUS_BLOCK_MAX) {
>> +		s32 ret;
>> +
>> +		ret = i2c_smbus_read_i2c_block_data(state->i2c_edid, i,
>>  						    I2C_SMBUS_BLOCK_MAX, buf + i);
>> -	if (err)
>> -		v4l2_err(sd, "%s: i2c read error\n", __func__);
>> +		if (ret < 0) {
>> +			v4l2_err(sd, "%s: i2c read error\n", __func__);
>> +			return ret;
>> +		}
>> +	}
>> +
>> +	return 0;
>>  }
>>  
>>  static inline int adv7511_cec_read(struct v4l2_subdev *sd, u8 reg)
>> @@ -1668,22 +1657,27 @@ static bool adv7511_check_edid_status(struct v4l2_subdev *sd)
>>  	if (edidRdy & MASK_ADV7511_EDID_RDY) {
>>  		int segment = adv7511_rd(sd, 0xc4);
>>  		struct adv7511_edid_detect ed;
>> +		int err;
>>  
>>  		if (segment >= EDID_MAX_SEGM) {
>>  			v4l2_err(sd, "edid segment number too big\n");
>>  			return false;
>>  		}
>>  		v4l2_dbg(1, debug, sd, "%s: got segment %d\n", __func__, segment);
>> -		adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]);
>> -		adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]);
>> -		if (segment == 0) {
>> -			state->edid.blocks = state->edid.data[0x7e] + 1;
>> -			v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n", __func__, state->edid.blocks);
>> +		err = adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]);
>> +		if (!err) {
>> +			adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]);
>> +			if (segment == 0) {
>> +				state->edid.blocks = state->edid.data[0x7e] + 1;
>> +				v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n",
>> +					 __func__, state->edid.blocks);
>> +			}
>>  		}
>> -		if (!edid_verify_crc(sd, segment) ||
>> -		    !edid_verify_header(sd, segment)) {
>> -			/* edid crc error, force reread of edid segment */
>> -			v4l2_err(sd, "%s: edid crc or header error\n", __func__);
>> +
>> +		if (err || !edid_verify_crc(sd, segment) || !edid_verify_header(sd, segment)) {
>> +			/* Couldn't read EDID or EDID is invalid. Force retry! */
>> +			if (!err)
>> +				v4l2_err(sd, "%s: edid crc or header error\n", __func__);
>>  			state->have_monitor = false;
>>  			adv7511_s_power(sd, false);
>>  			adv7511_s_power(sd, true);
>>
>
Wolfram Sang March 18, 2021, 10:43 a.m. UTC | #3
> I didn't hear back from you, so I'll pick it up for 5.13.

Thank you, Hans!

Can you pick up this one as well?

[PATCH 1/3] media: i2c: adv7842: remove open coded version of SMBus block write
Hans Verkuil March 18, 2021, 11:06 a.m. UTC | #4
On 18/03/2021 11:43, Wolfram Sang wrote:
> 
>> I didn't hear back from you, so I'll pick it up for 5.13.
> 
> Thank you, Hans!
> 
> Can you pick up this one as well?
> 
> [PATCH 1/3] media: i2c: adv7842: remove open coded version of SMBus block write
> 

Ah, I thought you had picked that one up. Miscommunication.

I've delegated it to myself in our patchwork system, so it should appear in a PR
for 5.13 next week or so.

Regards,

	Hans
Wolfram Sang March 18, 2021, 11:08 a.m. UTC | #5
> I've delegated it to myself in our patchwork system, so it should appear in a PR
> for 5.13 next week or so.

Thank you!
diff mbox series

Patch

diff --git a/drivers/media/i2c/adv7511-v4l2.c b/drivers/media/i2c/adv7511-v4l2.c
index a3161d709015..9183003ae22d 100644
--- a/drivers/media/i2c/adv7511-v4l2.c
+++ b/drivers/media/i2c/adv7511-v4l2.c
@@ -214,36 +214,25 @@  static inline void adv7511_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask
 	adv7511_wr(sd, reg, (adv7511_rd(sd, reg) & clr_mask) | val_mask);
 }
 
-static int adv_smbus_read_i2c_block_data(struct i2c_client *client,
-					 u8 command, unsigned length, u8 *values)
-{
-	union i2c_smbus_data data;
-	int ret;
-
-	if (length > I2C_SMBUS_BLOCK_MAX)
-		length = I2C_SMBUS_BLOCK_MAX;
-	data.block[0] = length;
-
-	ret = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
-			     I2C_SMBUS_READ, command,
-			     I2C_SMBUS_I2C_BLOCK_DATA, &data);
-	memcpy(values, data.block + 1, length);
-	return ret;
-}
-
-static void adv7511_edid_rd(struct v4l2_subdev *sd, uint16_t len, uint8_t *buf)
+static int adv7511_edid_rd(struct v4l2_subdev *sd, uint16_t len, uint8_t *buf)
 {
 	struct adv7511_state *state = get_adv7511_state(sd);
 	int i;
-	int err = 0;
 
 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
 
-	for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
-		err = adv_smbus_read_i2c_block_data(state->i2c_edid, i,
+	for (i = 0; i < len; i += I2C_SMBUS_BLOCK_MAX) {
+		s32 ret;
+
+		ret = i2c_smbus_read_i2c_block_data(state->i2c_edid, i,
 						    I2C_SMBUS_BLOCK_MAX, buf + i);
-	if (err)
-		v4l2_err(sd, "%s: i2c read error\n", __func__);
+		if (ret < 0) {
+			v4l2_err(sd, "%s: i2c read error\n", __func__);
+			return ret;
+		}
+	}
+
+	return 0;
 }
 
 static inline int adv7511_cec_read(struct v4l2_subdev *sd, u8 reg)
@@ -1668,22 +1657,27 @@  static bool adv7511_check_edid_status(struct v4l2_subdev *sd)
 	if (edidRdy & MASK_ADV7511_EDID_RDY) {
 		int segment = adv7511_rd(sd, 0xc4);
 		struct adv7511_edid_detect ed;
+		int err;
 
 		if (segment >= EDID_MAX_SEGM) {
 			v4l2_err(sd, "edid segment number too big\n");
 			return false;
 		}
 		v4l2_dbg(1, debug, sd, "%s: got segment %d\n", __func__, segment);
-		adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]);
-		adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]);
-		if (segment == 0) {
-			state->edid.blocks = state->edid.data[0x7e] + 1;
-			v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n", __func__, state->edid.blocks);
+		err = adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]);
+		if (!err) {
+			adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]);
+			if (segment == 0) {
+				state->edid.blocks = state->edid.data[0x7e] + 1;
+				v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n",
+					 __func__, state->edid.blocks);
+			}
 		}
-		if (!edid_verify_crc(sd, segment) ||
-		    !edid_verify_header(sd, segment)) {
-			/* edid crc error, force reread of edid segment */
-			v4l2_err(sd, "%s: edid crc or header error\n", __func__);
+
+		if (err || !edid_verify_crc(sd, segment) || !edid_verify_header(sd, segment)) {
+			/* Couldn't read EDID or EDID is invalid. Force retry! */
+			if (!err)
+				v4l2_err(sd, "%s: edid crc or header error\n", __func__);
 			state->have_monitor = false;
 			adv7511_s_power(sd, false);
 			adv7511_s_power(sd, true);