layerscape: add patches-5.4
[openwrt/staging/chunkeey.git] / target / linux / layerscape / patches-5.4 / 808-i2c-0014-i2c-mux-pca954x-support-property-idle-state.patch
1 From 5b51284db77e6383ff438b1dd21ee83bb6eaa895 Mon Sep 17 00:00:00 2001
2 From: Biwen Li <biwen.li@nxp.com>
3 Date: Tue, 22 Oct 2019 12:11:51 +0800
4 Subject: [PATCH] i2c: mux: pca954x: support property idle-state
5
6 This supports property idle-state,if present,
7 overrides i2c-mux-idle-disconnect.
8
9 My use cases:
10 - Use the property idle-state to fix
11 an errata on LS2085ARDB and LS2088ARDB.
12 - Errata id: E-00013(board LS2085ARDB and
13 LS2088ARDB revision on Rev.B, Rev.C and Rev.D).
14 - About E-00013:
15 - Description: I2C1 and I2C3 buses
16 are missing pull-up.
17 - Impact: When the PCA954x device is tri-stated, the I2C bus
18 will float. This makes the I2C bus and its associated
19 downstream devices inaccessible.
20 - Hardware fix: Populate resistors R189 and R190 for I2C1
21 and resistors R228 and R229 for I2C3.
22 - Software fix: Remove the tri-state option from the PCA954x
23 driver(PCA954x always on enable status, specify a
24 channel zero in dts to fix the errata E-00013).
25
26 Tested-by: Ioana Ciornei <ioana.ciornei@nxp.com>
27 Signed-off-by: Biwen Li <biwen.li@nxp.com>
28 ---
29 drivers/i2c/muxes/i2c-mux-pca954x.c | 69 ++++++++++++++++++++++++-------------
30 1 file changed, 46 insertions(+), 23 deletions(-)
31
32 --- a/drivers/i2c/muxes/i2c-mux-pca954x.c
33 +++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
34 @@ -87,7 +87,7 @@ struct pca954x {
35
36 u8 last_chan; /* last register value */
37 /* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */
38 - s8 idle_state;
39 + s32 idle_state;
40
41 struct i2c_client *client;
42
43 @@ -237,20 +237,23 @@ static int pca954x_reg_write(struct i2c_
44 I2C_SMBUS_BYTE, &dummy);
45 }
46
47 +static u8 pca954x_regval(struct pca954x *data, u8 chan)
48 +{
49 + /* We make switches look like muxes, not sure how to be smarter. */
50 + if (data->chip->muxtype == pca954x_ismux)
51 + return chan | data->chip->enable;
52 + else
53 + return 1 << chan;
54 +}
55 +
56 static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
57 {
58 struct pca954x *data = i2c_mux_priv(muxc);
59 struct i2c_client *client = data->client;
60 - const struct chip_desc *chip = data->chip;
61 u8 regval;
62 int ret = 0;
63
64 - /* we make switches look like muxes, not sure how to be smarter */
65 - if (chip->muxtype == pca954x_ismux)
66 - regval = chan | chip->enable;
67 - else
68 - regval = 1 << chan;
69 -
70 + regval = pca954x_regval(data, chan);
71 /* Only select the channel if its different from the last channel */
72 if (data->last_chan != regval) {
73 ret = pca954x_reg_write(muxc->parent, client, regval);
74 @@ -264,7 +267,7 @@ static int pca954x_deselect_mux(struct i
75 {
76 struct pca954x *data = i2c_mux_priv(muxc);
77 struct i2c_client *client = data->client;
78 - s8 idle_state;
79 + s32 idle_state;
80
81 idle_state = READ_ONCE(data->idle_state);
82 if (idle_state >= 0)
83 @@ -410,6 +413,22 @@ static void pca954x_cleanup(struct i2c_m
84 i2c_mux_del_adapters(muxc);
85 }
86
87 +static int pca954x_init(struct i2c_client *client, struct pca954x *data)
88 +{
89 + int ret;
90 + if (data->idle_state >= 0) {
91 + data->last_chan = pca954x_regval(data, data->idle_state);
92 + } else {
93 + /* Disconnect multiplexer */
94 + data->last_chan = 0;
95 + }
96 + ret = i2c_smbus_write_byte(client, data->last_chan);
97 + if (ret < 0)
98 + data->last_chan = 0;
99 +
100 + return ret;
101 +}
102 +
103 /*
104 * I2C init/probing/exit functions
105 */
106 @@ -419,7 +438,6 @@ static int pca954x_probe(struct i2c_clie
107 struct i2c_adapter *adap = client->adapter;
108 struct device *dev = &client->dev;
109 struct device_node *np = dev->of_node;
110 - bool idle_disconnect_dt;
111 struct gpio_desc *gpio;
112 struct i2c_mux_core *muxc;
113 struct pca954x *data;
114 @@ -470,23 +488,24 @@ static int pca954x_probe(struct i2c_clie
115 }
116 }
117
118 - /* Write the mux register at addr to verify
119 + data->idle_state = MUX_IDLE_AS_IS;
120 + if (of_property_read_u32(np, "idle-state", &data->idle_state)) {
121 + if (np && of_property_read_bool(np, "i2c-mux-idle-disconnect"))
122 + data->idle_state = MUX_IDLE_DISCONNECT;
123 + }
124 +
125 + /*
126 + * Write the mux register at addr to verify
127 * that the mux is in fact present. This also
128 - * initializes the mux to disconnected state.
129 + * initializes the mux to a channel
130 + * or disconnected state.
131 */
132 - if (i2c_smbus_write_byte(client, 0) < 0) {
133 + ret = pca954x_init(client, data);
134 + if (ret < 0) {
135 dev_warn(dev, "probe failed\n");
136 return -ENODEV;
137 }
138
139 - data->last_chan = 0; /* force the first selection */
140 - data->idle_state = MUX_IDLE_AS_IS;
141 -
142 - idle_disconnect_dt = np &&
143 - of_property_read_bool(np, "i2c-mux-idle-disconnect");
144 - if (idle_disconnect_dt)
145 - data->idle_state = MUX_IDLE_DISCONNECT;
146 -
147 ret = pca954x_irq_setup(muxc);
148 if (ret)
149 goto fail_cleanup;
150 @@ -538,9 +557,13 @@ static int pca954x_resume(struct device
151 struct i2c_client *client = to_i2c_client(dev);
152 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
153 struct pca954x *data = i2c_mux_priv(muxc);
154 + int ret;
155
156 - data->last_chan = 0;
157 - return i2c_smbus_write_byte(client, 0);
158 + ret = pca954x_init(client, data);
159 + if (ret < 0)
160 + dev_err(&client->dev, "failed to verify the mux, the mux maybe not present in fact\n");
161 +
162 + return ret;
163 }
164 #endif
165