Commit 001d7ef8 authored by Mukesh Ojha's avatar Mukesh Ojha Committed by Linus Walleij
Browse files

pinctrl: Fix the clean up on pinconf_apply_setting failure



When some client does devm_pinctrl_get() followed by
pinctrl_select_state() that does pinmux first successfully and later
during config setting it sets the wrong drive strenght to the pin due to
which pinconf_apply_setting fails. Currently, on failure during config
setting is implemented as if pinmux has failed for one of the pin but
that does not seem right and need to undo the pinmux for all the pin if
config setting fails.

Current commit does a bit refactor to reuse the code and tries to clean
up mux setting on config setting failure.

Signed-off-by: default avatarMukesh Ojha <mukesh.ojha@oss.qualcomm.com>
Link: https://lore.kernel.org/20241224084441.515870-1-mukesh.ojha@oss.qualcomm.com


Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent e4ee0acf
Loading
Loading
Loading
Loading
+30 −20
Original line number Diff line number Diff line
@@ -1256,6 +1256,20 @@ static void pinctrl_link_add(struct pinctrl_dev *pctldev,
				DL_FLAG_AUTOREMOVE_CONSUMER);
}

static void pinctrl_cond_disable_mux_setting(struct pinctrl_state *state,
					     struct pinctrl_setting *target_setting)
{
	struct pinctrl_setting *setting;

	list_for_each_entry(setting, &state->settings, node) {
		if (target_setting && (&setting->node == &target_setting->node))
			break;

		if (setting->type == PIN_MAP_TYPE_MUX_GROUP)
			pinmux_disable_setting(setting);
	}
}

/**
 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
 * @p: the pinctrl handle for the device that requests configuration
@@ -1263,7 +1277,7 @@ static void pinctrl_link_add(struct pinctrl_dev *pctldev,
 */
static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
{
	struct pinctrl_setting *setting, *setting2;
	struct pinctrl_setting *setting;
	struct pinctrl_state *old_state = READ_ONCE(p->state);
	int ret;

@@ -1274,11 +1288,7 @@ static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
		 * still owned by the new state will be re-acquired by the call
		 * to pinmux_enable_setting() in the loop below.
		 */
		list_for_each_entry(setting, &old_state->settings, node) {
			if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
				continue;
			pinmux_disable_setting(setting);
		}
		pinctrl_cond_disable_mux_setting(old_state, NULL);
	}

	p->state = NULL;
@@ -1322,7 +1332,7 @@ static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
		}

		if (ret < 0) {
			goto unapply_new_state;
			goto unapply_mux_setting;
		}

		/* Do not link hogs (circular dependency) */
@@ -1334,12 +1344,13 @@ static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)

	return 0;

unapply_mux_setting:
	pinctrl_cond_disable_mux_setting(state, NULL);
	goto restore_old_state;

unapply_new_state:
	dev_err(p->dev, "Error applying setting, reverse things back\n");

	list_for_each_entry(setting2, &state->settings, node) {
		if (&setting2->node == &setting->node)
			break;
	/*
	 * All we can do here is pinmux_disable_setting.
	 * That means that some pins are muxed differently now
@@ -1347,10 +1358,9 @@ static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
	 * "unmux a pin"!), but it's not a big deal since the pins
	 * are free to be muxed by another apply_setting.
	 */
		if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
			pinmux_disable_setting(setting2);
	}
	pinctrl_cond_disable_mux_setting(state, setting);

restore_old_state:
	/* There's no infinite recursive loop here because p->state is NULL */
	if (old_state)
		pinctrl_select_state(p, old_state);