2023-10-13 07:23:58

by Bartosz Golaszewski

[permalink] [raw]
Subject: [PATCH v2] hte: tegra194: don't access struct gpio_chip

From: Bartosz Golaszewski <[email protected]>

Using struct gpio_chip is not safe as it will disappear if the
underlying driver is unbound for any reason. Switch to using reference
counted struct gpio_device and its dedicated accessors.

Signed-off-by: Bartosz Golaszewski <[email protected]>
Tested-by: Dipen Patel <[email protected]>
Reviewed-by: Linus Walleij <[email protected]>
[andy: used gpio_device_find_by_fwnode()]
Reviewed-by: Dipen Patel <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Andy Shevchenko <[email protected]>
---
Dipen,

Can you retest this patch now rebased on top of current GPIO for-next?

v1 -> v2:
- rebase on top of current gpio/for-next

drivers/hte/hte-tegra194.c | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/drivers/hte/hte-tegra194.c b/drivers/hte/hte-tegra194.c
index 6fe6897047ac..341a134cb7d0 100644
--- a/drivers/hte/hte-tegra194.c
+++ b/drivers/hte/hte-tegra194.c
@@ -132,7 +132,7 @@ struct tegra_hte_soc {
const struct tegra_hte_data *prov_data;
struct tegra_hte_line_data *line_data;
struct hte_chip *chip;
- struct gpio_chip *c;
+ struct gpio_device *gdev;
void __iomem *regs;
};

@@ -418,7 +418,7 @@ static int tegra_hte_line_xlate(struct hte_chip *gc,
* HTE/GTE namespace.
*/
if (gs->prov_data->type == HTE_TEGRA_TYPE_GPIO && !args) {
- line_id = desc->attr.line_id - gs->c->base;
+ line_id = desc->attr.line_id - gpio_device_get_base(gs->gdev);
map = gs->prov_data->map;
map_sz = gs->prov_data->map_sz;
} else if (gs->prov_data->type == HTE_TEGRA_TYPE_GPIO && args) {
@@ -645,7 +645,7 @@ static bool tegra_hte_match_from_linedata(const struct hte_chip *chip,
if (!hte_dev || (hte_dev->prov_data->type != HTE_TEGRA_TYPE_GPIO))
return false;

- return hte_dev->c == gpiod_to_chip(hdesc->attr.line_data);
+ return hte_dev->gdev == gpiod_to_gpio_device(hdesc->attr.line_data);
}

static const struct of_device_id tegra_hte_of_match[] = {
@@ -673,14 +673,11 @@ static void tegra_gte_disable(void *data)
tegra_hte_writel(gs, HTE_TECTRL, 0);
}

-static int tegra_get_gpiochip_from_name(struct gpio_chip *chip, void *data)
+static void tegra_hte_put_gpio_device(void *data)
{
- return !strcmp(chip->label, data);
-}
+ struct gpio_device *gdev = data;

-static int tegra_gpiochip_match(struct gpio_chip *chip, void *data)
-{
- return chip->fwnode == of_node_to_fwnode(data);
+ gpio_device_put(gdev);
}

static int tegra_hte_probe(struct platform_device *pdev)
@@ -760,8 +757,8 @@ static int tegra_hte_probe(struct platform_device *pdev)

if (of_device_is_compatible(dev->of_node,
"nvidia,tegra194-gte-aon")) {
- hte_dev->c = gpiochip_find("tegra194-gpio-aon",
- tegra_get_gpiochip_from_name);
+ hte_dev->gdev =
+ gpio_device_find_by_label("tegra194-gpio-aon");
} else {
gpio_ctrl = of_parse_phandle(dev->of_node,
"nvidia,gpio-controller",
@@ -772,14 +769,19 @@ static int tegra_hte_probe(struct platform_device *pdev)
return -ENODEV;
}

- hte_dev->c = gpiochip_find(gpio_ctrl,
- tegra_gpiochip_match);
+ hte_dev->gdev =
+ gpio_device_find_by_fwnode(of_fwnode_handle(gpio_ctrl));
of_node_put(gpio_ctrl);
}

- if (!hte_dev->c)
+ if (!hte_dev->gdev)
return dev_err_probe(dev, -EPROBE_DEFER,
"wait for gpio controller\n");
+
+ ret = devm_add_action_or_reset(dev, tegra_hte_put_gpio_device,
+ hte_dev->gdev);
+ if (ret)
+ return ret;
}

hte_dev->chip = gc;
--
2.39.2


2023-10-17 18:24:14

by Dipen Patel

[permalink] [raw]
Subject: Re: [PATCH v2] hte: tegra194: don't access struct gpio_chip

On 10/13/23 12:23 AM, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <[email protected]>
>
> Using struct gpio_chip is not safe as it will disappear if the
> underlying driver is unbound for any reason. Switch to using reference
> counted struct gpio_device and its dedicated accessors.
>
> Signed-off-by: Bartosz Golaszewski <[email protected]>
> Tested-by: Dipen Patel <[email protected]>
> Reviewed-by: Linus Walleij <[email protected]>
> [andy: used gpio_device_find_by_fwnode()]
> Reviewed-by: Dipen Patel <[email protected]>
> Link: https://lore.kernel.org/r/[email protected]
> Signed-off-by: Andy Shevchenko <[email protected]>
> ---
> Dipen,
>
> Can you retest this patch now rebased on top of current GPIO for-next?
Tested-by: Dipen Patel <[email protected]>
>
> v1 -> v2:
> - rebase on top of current gpio/for-next
>
> drivers/hte/hte-tegra194.c | 30 ++++++++++++++++--------------
> 1 file changed, 16 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/hte/hte-tegra194.c b/drivers/hte/hte-tegra194.c
> index 6fe6897047ac..341a134cb7d0 100644
> --- a/drivers/hte/hte-tegra194.c
> +++ b/drivers/hte/hte-tegra194.c
> @@ -132,7 +132,7 @@ struct tegra_hte_soc {
> const struct tegra_hte_data *prov_data;
> struct tegra_hte_line_data *line_data;
> struct hte_chip *chip;
> - struct gpio_chip *c;
> + struct gpio_device *gdev;
> void __iomem *regs;
> };
>
> @@ -418,7 +418,7 @@ static int tegra_hte_line_xlate(struct hte_chip *gc,
> * HTE/GTE namespace.
> */
> if (gs->prov_data->type == HTE_TEGRA_TYPE_GPIO && !args) {
> - line_id = desc->attr.line_id - gs->c->base;
> + line_id = desc->attr.line_id - gpio_device_get_base(gs->gdev);
> map = gs->prov_data->map;
> map_sz = gs->prov_data->map_sz;
> } else if (gs->prov_data->type == HTE_TEGRA_TYPE_GPIO && args) {
> @@ -645,7 +645,7 @@ static bool tegra_hte_match_from_linedata(const struct hte_chip *chip,
> if (!hte_dev || (hte_dev->prov_data->type != HTE_TEGRA_TYPE_GPIO))
> return false;
>
> - return hte_dev->c == gpiod_to_chip(hdesc->attr.line_data);
> + return hte_dev->gdev == gpiod_to_gpio_device(hdesc->attr.line_data);
> }
>
> static const struct of_device_id tegra_hte_of_match[] = {
> @@ -673,14 +673,11 @@ static void tegra_gte_disable(void *data)
> tegra_hte_writel(gs, HTE_TECTRL, 0);
> }
>
> -static int tegra_get_gpiochip_from_name(struct gpio_chip *chip, void *data)
> +static void tegra_hte_put_gpio_device(void *data)
> {
> - return !strcmp(chip->label, data);
> -}
> + struct gpio_device *gdev = data;
>
> -static int tegra_gpiochip_match(struct gpio_chip *chip, void *data)
> -{
> - return chip->fwnode == of_node_to_fwnode(data);
> + gpio_device_put(gdev);
> }
>
> static int tegra_hte_probe(struct platform_device *pdev)
> @@ -760,8 +757,8 @@ static int tegra_hte_probe(struct platform_device *pdev)
>
> if (of_device_is_compatible(dev->of_node,
> "nvidia,tegra194-gte-aon")) {
> - hte_dev->c = gpiochip_find("tegra194-gpio-aon",
> - tegra_get_gpiochip_from_name);
> + hte_dev->gdev =
> + gpio_device_find_by_label("tegra194-gpio-aon");
> } else {
> gpio_ctrl = of_parse_phandle(dev->of_node,
> "nvidia,gpio-controller",
> @@ -772,14 +769,19 @@ static int tegra_hte_probe(struct platform_device *pdev)
> return -ENODEV;
> }
>
> - hte_dev->c = gpiochip_find(gpio_ctrl,
> - tegra_gpiochip_match);
> + hte_dev->gdev =
> + gpio_device_find_by_fwnode(of_fwnode_handle(gpio_ctrl));
> of_node_put(gpio_ctrl);
> }
>
> - if (!hte_dev->c)
> + if (!hte_dev->gdev)
> return dev_err_probe(dev, -EPROBE_DEFER,
> "wait for gpio controller\n");
> +
> + ret = devm_add_action_or_reset(dev, tegra_hte_put_gpio_device,
> + hte_dev->gdev);
> + if (ret)
> + return ret;
> }
>
> hte_dev->chip = gc;