eb52eea64e64130f31abe3cf32aa078ae9d1c46a
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0156-media-tc358743-Check-I2C-succeeded-during-probe.patch
1 From f83a4396237e96da7a2aa3bfc2de1c928b128e6c Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.org>
3 Date: Wed, 31 Oct 2018 14:57:34 +0000
4 Subject: [PATCH] media: tc358743: Check I2C succeeded during probe.
5
6 The probe for the TC358743 reads the CHIPID register from
7 the device and compares it to the expected value of 0.
8 If the I2C request fails then that also returns 0, so
9 the driver loads thinking that the device is there.
10
11 Generally I2C communications are reliable so there is
12 limited need to check the return value on every transfer,
13 therefore only amend the one read during probe to check
14 for I2C errors.
15
16 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
17 ---
18 drivers/media/i2c/tc358743.c | 27 +++++++++++++++++++++++----
19 1 file changed, 23 insertions(+), 4 deletions(-)
20
21 --- a/drivers/media/i2c/tc358743.c
22 +++ b/drivers/media/i2c/tc358743.c
23 @@ -110,7 +110,7 @@ static inline struct tc358743_state *to_
24
25 /* --------------- I2C --------------- */
26
27 -static void i2c_rd(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
28 +static int i2c_rd(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
29 {
30 struct tc358743_state *state = to_state(sd);
31 struct i2c_client *client = state->i2c_client;
32 @@ -136,6 +136,7 @@ static void i2c_rd(struct v4l2_subdev *s
33 v4l2_err(sd, "%s: reading register 0x%x from 0x%x failed\n",
34 __func__, reg, client->addr);
35 }
36 + return err != ARRAY_SIZE(msgs);
37 }
38
39 static void i2c_wr(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
40 @@ -192,15 +193,24 @@ static void i2c_wr(struct v4l2_subdev *s
41 }
42 }
43
44 -static noinline u32 i2c_rdreg(struct v4l2_subdev *sd, u16 reg, u32 n)
45 +static noinline u32 i2c_rdreg_err(struct v4l2_subdev *sd, u16 reg, u32 n,
46 + int *err)
47 {
48 + int error;
49 __le32 val = 0;
50
51 - i2c_rd(sd, reg, (u8 __force *)&val, n);
52 + error = i2c_rd(sd, reg, (u8 __force *)&val, n);
53 + if (err)
54 + *err = error;
55
56 return le32_to_cpu(val);
57 }
58
59 +static inline u32 i2c_rdreg(struct v4l2_subdev *sd, u16 reg, u32 n)
60 +{
61 + return i2c_rdreg_err(sd, reg, n, NULL);
62 +}
63 +
64 static noinline void i2c_wrreg(struct v4l2_subdev *sd, u16 reg, u32 val, u32 n)
65 {
66 __le32 raw = cpu_to_le32(val);
67 @@ -229,6 +239,13 @@ static u16 i2c_rd16(struct v4l2_subdev *
68 return i2c_rdreg(sd, reg, 2);
69 }
70
71 +static int i2c_rd16_err(struct v4l2_subdev *sd, u16 reg, u16 *value)
72 +{
73 + int err;
74 + *value = i2c_rdreg_err(sd, reg, 2, &err);
75 + return err;
76 +}
77 +
78 static void i2c_wr16(struct v4l2_subdev *sd, u16 reg, u16 val)
79 {
80 i2c_wrreg(sd, reg, val, 2);
81 @@ -2062,6 +2079,7 @@ static int tc358743_probe(struct i2c_cli
82 struct tc358743_platform_data *pdata = client->dev.platform_data;
83 struct v4l2_subdev *sd;
84 u16 irq_mask = MASK_HDMI_MSK | MASK_CSI_MSK;
85 + u16 chipid;
86 int err;
87
88 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
89 @@ -2094,7 +2112,8 @@ static int tc358743_probe(struct i2c_cli
90 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
91
92 /* i2c access */
93 - if ((i2c_rd16(sd, CHIPID) & MASK_CHIPID) != 0) {
94 + if (i2c_rd16_err(sd, CHIPID, &chipid) ||
95 + (chipid & MASK_CHIPID) != 0) {
96 v4l2_info(sd, "not a TC358743 on address 0x%x\n",
97 client->addr << 1);
98 return -ENODEV;