mediatek: add kmod-usb3 to default package set of the ZBT-Z8102AX
[openwrt/staging/stintel.git] / target / linux / mvebu / patches-6.1 / 106-Revert-i2c-pxa-move-to-generic-GPIO-recovery.patch
1 From ea8444b6fa5955c16b713dc83310882b93b44e62 Mon Sep 17 00:00:00 2001
2 From: Robert Marko <robert.marko@sartura.hr>
3 Date: Fri, 10 Nov 2023 10:10:29 +0100
4 Subject: [PATCH] Revert "i2c: pxa: move to generic GPIO recovery"
5
6 This reverts commit 0b01392c18b9993a584f36ace1d61118772ad0ca.
7
8 Conversion of PXA to generic I2C recovery, makes the I2C bus completely
9 lock up if recovery pinctrl is present in the DT and I2C recovery is
10 enabled.
11
12 So, until the generic I2C recovery can also work with PXA lets revert
13 to have working I2C and I2C recovery again.
14
15 Signed-off-by: Robert Marko <robert.marko@sartura.hr>
16 Cc: stable@vger.kernel.org # 5.11+
17 ---
18 drivers/i2c/busses/i2c-pxa.c | 76 ++++++++++++++++++++++++++++++++----
19 1 file changed, 68 insertions(+), 8 deletions(-)
20
21 --- a/drivers/i2c/busses/i2c-pxa.c
22 +++ b/drivers/i2c/busses/i2c-pxa.c
23 @@ -264,6 +264,9 @@ struct pxa_i2c {
24 u32 hs_mask;
25
26 struct i2c_bus_recovery_info recovery;
27 + struct pinctrl *pinctrl;
28 + struct pinctrl_state *pinctrl_default;
29 + struct pinctrl_state *pinctrl_recovery;
30 };
31
32 #define _IBMR(i2c) ((i2c)->reg_ibmr)
33 @@ -1302,12 +1305,13 @@ static void i2c_pxa_prepare_recovery(str
34 */
35 gpiod_set_value(i2c->recovery.scl_gpiod, ibmr & IBMR_SCLS);
36 gpiod_set_value(i2c->recovery.sda_gpiod, ibmr & IBMR_SDAS);
37 +
38 + WARN_ON(pinctrl_select_state(i2c->pinctrl, i2c->pinctrl_recovery));
39 }
40
41 static void i2c_pxa_unprepare_recovery(struct i2c_adapter *adap)
42 {
43 struct pxa_i2c *i2c = adap->algo_data;
44 - struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
45 u32 isr;
46
47 /*
48 @@ -1321,7 +1325,7 @@ static void i2c_pxa_unprepare_recovery(s
49 i2c_pxa_do_reset(i2c);
50 }
51
52 - WARN_ON(pinctrl_select_state(bri->pinctrl, bri->pins_default));
53 + WARN_ON(pinctrl_select_state(i2c->pinctrl, i2c->pinctrl_default));
54
55 dev_dbg(&i2c->adap.dev, "recovery: IBMR 0x%08x ISR 0x%08x\n",
56 readl(_IBMR(i2c)), readl(_ISR(i2c)));
57 @@ -1343,20 +1347,76 @@ static int i2c_pxa_init_recovery(struct
58 if (IS_ENABLED(CONFIG_I2C_PXA_SLAVE))
59 return 0;
60
61 - bri->pinctrl = devm_pinctrl_get(dev);
62 - if (PTR_ERR(bri->pinctrl) == -ENODEV) {
63 - bri->pinctrl = NULL;
64 + i2c->pinctrl = devm_pinctrl_get(dev);
65 + if (PTR_ERR(i2c->pinctrl) == -ENODEV)
66 + i2c->pinctrl = NULL;
67 + if (IS_ERR(i2c->pinctrl))
68 + return PTR_ERR(i2c->pinctrl);
69 +
70 + if (!i2c->pinctrl)
71 + return 0;
72 +
73 + i2c->pinctrl_default = pinctrl_lookup_state(i2c->pinctrl,
74 + PINCTRL_STATE_DEFAULT);
75 + i2c->pinctrl_recovery = pinctrl_lookup_state(i2c->pinctrl, "recovery");
76 +
77 + if (IS_ERR(i2c->pinctrl_default) || IS_ERR(i2c->pinctrl_recovery)) {
78 + dev_info(dev, "missing pinmux recovery information: %ld %ld\n",
79 + PTR_ERR(i2c->pinctrl_default),
80 + PTR_ERR(i2c->pinctrl_recovery));
81 + return 0;
82 + }
83 +
84 + /*
85 + * Claiming GPIOs can influence the pinmux state, and may glitch the
86 + * I2C bus. Do this carefully.
87 + */
88 + bri->scl_gpiod = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
89 + if (bri->scl_gpiod == ERR_PTR(-EPROBE_DEFER))
90 + return -EPROBE_DEFER;
91 + if (IS_ERR(bri->scl_gpiod)) {
92 + dev_info(dev, "missing scl gpio recovery information: %pe\n",
93 + bri->scl_gpiod);
94 + return 0;
95 + }
96 +
97 + /*
98 + * We have SCL. Pull SCL low and wait a bit so that SDA glitches
99 + * have no effect.
100 + */
101 + gpiod_direction_output(bri->scl_gpiod, 0);
102 + udelay(10);
103 + bri->sda_gpiod = devm_gpiod_get(dev, "sda", GPIOD_OUT_HIGH_OPEN_DRAIN);
104 +
105 + /* Wait a bit in case of a SDA glitch, and then release SCL. */
106 + udelay(10);
107 + gpiod_direction_output(bri->scl_gpiod, 1);
108 +
109 + if (bri->sda_gpiod == ERR_PTR(-EPROBE_DEFER))
110 + return -EPROBE_DEFER;
111 +
112 + if (IS_ERR(bri->sda_gpiod)) {
113 + dev_info(dev, "missing sda gpio recovery information: %pe\n",
114 + bri->sda_gpiod);
115 return 0;
116 }
117 - if (IS_ERR(bri->pinctrl))
118 - return PTR_ERR(bri->pinctrl);
119
120 bri->prepare_recovery = i2c_pxa_prepare_recovery;
121 bri->unprepare_recovery = i2c_pxa_unprepare_recovery;
122 + bri->recover_bus = i2c_generic_scl_recovery;
123
124 i2c->adap.bus_recovery_info = bri;
125
126 - return 0;
127 + /*
128 + * Claiming GPIOs can change the pinmux state, which confuses the
129 + * pinctrl since pinctrl's idea of the current setting is unaffected
130 + * by the pinmux change caused by claiming the GPIO. Work around that
131 + * by switching pinctrl to the GPIO state here. We do it this way to
132 + * avoid glitching the I2C bus.
133 + */
134 + pinctrl_select_state(i2c->pinctrl, i2c->pinctrl_recovery);
135 +
136 + return pinctrl_select_state(i2c->pinctrl, i2c->pinctrl_default);
137 }
138
139 static int i2c_pxa_probe(struct platform_device *dev)