Skip to content

Commit 1f3c85c

Browse files
mwalleLinus Walleij
authored andcommitted
pinctrl: microchip-sgpio: wait until output is actually set
Right now, when a gpio value is set, the actual hardware pin gets set asynchronously. When linux write the output register, it takes some time until it is actually propagated to the output shift registers. If that output port is connected to an I2C mux for example, the linux driver assumes the I2C bus is already switched although it is not. Fortunately, there is a single shot mode with a feedback: you can trigger the single shot and the hardware will clear that bit once it has finished the clocking and strobed the load signal of the shift registers. This can take a considerable amount of time though. Measuremens have shown that it takes up to a whole burst cycle gap which is about 50ms on the largest setting. Therefore, we have to mark the output bank as sleepable. To avoid unnecessary waiting, just trigger the single shot if the value was actually changed. Signed-off-by: Michael Walle <michael@walle.cc> Link: https://lore.kernel.org/r/20220226204507.2511633-6-michael@walle.cc Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 2560c68 commit 1f3c85c

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

drivers/pinctrl/pinctrl-microchip-sgpio.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ struct sgpio_properties {
6464
#define SGPIO_LUTON_BIT_SOURCE GENMASK(11, 0)
6565

6666
#define SGPIO_OCELOT_AUTO_REPEAT BIT(10)
67+
#define SGPIO_OCELOT_SINGLE_SHOT BIT(11)
6768
#define SGPIO_OCELOT_PORT_WIDTH GENMASK(8, 7)
6869
#define SGPIO_OCELOT_CLK_FREQ GENMASK(19, 8)
6970
#define SGPIO_OCELOT_BIT_SOURCE GENMASK(23, 12)
7071

7172
#define SGPIO_SPARX5_AUTO_REPEAT BIT(6)
73+
#define SGPIO_SPARX5_SINGLE_SHOT BIT(7)
7274
#define SGPIO_SPARX5_PORT_WIDTH GENMASK(4, 3)
7375
#define SGPIO_SPARX5_CLK_FREQ GENMASK(19, 8)
7476
#define SGPIO_SPARX5_BIT_SOURCE GENMASK(23, 12)
@@ -118,6 +120,8 @@ struct sgpio_priv {
118120
struct regmap *regs;
119121
const struct sgpio_properties *properties;
120122
spinlock_t lock;
123+
/* protects the config register and single shot mode */
124+
struct mutex poll_lock;
121125
};
122126

123127
struct sgpio_port_addr {
@@ -224,12 +228,64 @@ static inline void sgpio_configure_clock(struct sgpio_priv *priv, u32 clkfrq)
224228
sgpio_clrsetbits(priv, REG_SIO_CLOCK, 0, clr, set);
225229
}
226230

231+
static int sgpio_single_shot(struct sgpio_priv *priv)
232+
{
233+
u32 addr = sgpio_get_addr(priv, REG_SIO_CONFIG, 0);
234+
int ret, ret2;
235+
u32 ctrl;
236+
unsigned int single_shot;
237+
unsigned int auto_repeat;
238+
239+
switch (priv->properties->arch) {
240+
case SGPIO_ARCH_LUTON:
241+
/* not supported for now */
242+
return 0;
243+
case SGPIO_ARCH_OCELOT:
244+
single_shot = SGPIO_OCELOT_SINGLE_SHOT;
245+
auto_repeat = SGPIO_OCELOT_AUTO_REPEAT;
246+
break;
247+
case SGPIO_ARCH_SPARX5:
248+
single_shot = SGPIO_SPARX5_SINGLE_SHOT;
249+
auto_repeat = SGPIO_SPARX5_AUTO_REPEAT;
250+
break;
251+
default:
252+
return -EINVAL;
253+
}
254+
255+
/*
256+
* Trigger immediate burst. This only works when auto repeat is turned
257+
* off. Otherwise, the single shot bit will never be cleared by the
258+
* hardware. Measurements showed that an update might take as long as
259+
* the burst gap. On a LAN9668 this is about 50ms for the largest
260+
* setting.
261+
* After the manual burst, reenable the auto repeat mode again.
262+
*/
263+
mutex_lock(&priv->poll_lock);
264+
ret = regmap_update_bits(priv->regs, addr, single_shot | auto_repeat,
265+
single_shot);
266+
if (ret)
267+
goto out;
268+
269+
ret = regmap_read_poll_timeout(priv->regs, addr, ctrl,
270+
!(ctrl & single_shot), 100, 60000);
271+
272+
/* reenable auto repeat mode even if there was an error */
273+
ret2 = regmap_update_bits(priv->regs, addr, auto_repeat, auto_repeat);
274+
out:
275+
mutex_unlock(&priv->poll_lock);
276+
277+
return ret ?: ret2;
278+
}
279+
227280
static int sgpio_output_set(struct sgpio_priv *priv,
228281
struct sgpio_port_addr *addr,
229282
int value)
230283
{
231284
unsigned int bit = SGPIO_SRC_BITS * addr->bit;
285+
u32 reg = sgpio_get_addr(priv, REG_PORT_CONFIG, addr->port);
286+
bool changed;
232287
u32 clr, set;
288+
int ret;
233289

234290
switch (priv->properties->arch) {
235291
case SGPIO_ARCH_LUTON:
@@ -248,7 +304,16 @@ static int sgpio_output_set(struct sgpio_priv *priv,
248304
return -EINVAL;
249305
}
250306

251-
sgpio_clrsetbits(priv, REG_PORT_CONFIG, addr->port, clr, set);
307+
ret = regmap_update_bits_check(priv->regs, reg, clr | set, set,
308+
&changed);
309+
if (ret)
310+
return ret;
311+
312+
if (changed) {
313+
ret = sgpio_single_shot(priv);
314+
if (ret)
315+
return ret;
316+
}
252317

253318
return 0;
254319
}
@@ -787,6 +852,7 @@ static int microchip_sgpio_register_bank(struct device *dev,
787852
gc->of_gpio_n_cells = 3;
788853
gc->base = -1;
789854
gc->ngpio = ngpios;
855+
gc->can_sleep = !bank->is_input;
790856

791857
if (bank->is_input && priv->properties->flags & SGPIO_FLAGS_HAS_IRQ) {
792858
int irq = fwnode_irq_get(fwnode, 0);
@@ -847,6 +913,7 @@ static int microchip_sgpio_probe(struct platform_device *pdev)
847913

848914
priv->dev = dev;
849915
spin_lock_init(&priv->lock);
916+
mutex_init(&priv->poll_lock);
850917

851918
reset = devm_reset_control_get_optional_shared(&pdev->dev, "switch");
852919
if (IS_ERR(reset))

0 commit comments

Comments
 (0)