Subject: [PATCH v3 1/2] mmc: atmel-mci: Convert to gpio descriptors

Replace the legacy GPIO APIs with gpio descriptor consumer interface.

To maintain backward compatibility, we rely on the "cd-inverted"
property to manage the invertion flag instead of GPIO property.

Signed-off-by: Balamanikandan Gunasundar <[email protected]>
---
drivers/mmc/host/atmel-mci.c | 78 +++++++++++++++++-------------------
include/linux/atmel-mci.h | 4 +-
2 files changed, 39 insertions(+), 43 deletions(-)

diff --git a/drivers/mmc/host/atmel-mci.c b/drivers/mmc/host/atmel-mci.c
index 67b2cd166e56..6ab43733ba9f 100644
--- a/drivers/mmc/host/atmel-mci.c
+++ b/drivers/mmc/host/atmel-mci.c
@@ -11,7 +11,6 @@
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
-#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
@@ -19,7 +18,8 @@
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
-#include <linux/of_gpio.h>
+#include <linux/irq.h>
+#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/scatterlist.h>
#include <linux/seq_file.h>
@@ -389,8 +389,8 @@ struct atmel_mci_slot {
#define ATMCI_CARD_NEED_INIT 1
#define ATMCI_SHUTDOWN 2

- int detect_pin;
- int wp_pin;
+ struct gpio_desc *detect_pin;
+ struct gpio_desc *wp_pin;
bool detect_is_active_high;

struct timer_list detect_timer;
@@ -638,7 +638,10 @@ atmci_of_init(struct platform_device *pdev)
pdata->slot[slot_id].bus_width = 1;

pdata->slot[slot_id].detect_pin =
- of_get_named_gpio(cnp, "cd-gpios", 0);
+ devm_fwnode_gpiod_get(&pdev->dev, of_fwnode_handle(cnp),
+ "cd", GPIOD_IN, "cd-gpios");
+ if (IS_ERR(pdata->slot[slot_id].detect_pin))
+ pdata->slot[slot_id].detect_pin = NULL;

pdata->slot[slot_id].detect_is_active_high =
of_property_read_bool(cnp, "cd-inverted");
@@ -647,7 +650,10 @@ atmci_of_init(struct platform_device *pdev)
of_property_read_bool(cnp, "non-removable");

pdata->slot[slot_id].wp_pin =
- of_get_named_gpio(cnp, "wp-gpios", 0);
+ devm_fwnode_gpiod_get(&pdev->dev, of_fwnode_handle(cnp),
+ "wp", GPIOD_IN, "wp-gpios");
+ if (IS_ERR(pdata->slot[slot_id].wp_pin))
+ pdata->slot[slot_id].wp_pin = NULL;
}

return pdata;
@@ -1511,8 +1517,8 @@ static int atmci_get_ro(struct mmc_host *mmc)
int read_only = -ENOSYS;
struct atmel_mci_slot *slot = mmc_priv(mmc);

- if (gpio_is_valid(slot->wp_pin)) {
- read_only = gpio_get_value(slot->wp_pin);
+ if (slot->wp_pin) {
+ read_only = gpiod_get_value(slot->wp_pin);
dev_dbg(&mmc->class_dev, "card is %s\n",
read_only ? "read-only" : "read-write");
}
@@ -1525,8 +1531,8 @@ static int atmci_get_cd(struct mmc_host *mmc)
int present = -ENOSYS;
struct atmel_mci_slot *slot = mmc_priv(mmc);

- if (gpio_is_valid(slot->detect_pin)) {
- present = !(gpio_get_value(slot->detect_pin) ^
+ if (slot->detect_pin) {
+ present = !(gpiod_get_raw_value(slot->detect_pin) ^
slot->detect_is_active_high);
dev_dbg(&mmc->class_dev, "card is %spresent\n",
present ? "" : "not ");
@@ -1639,8 +1645,8 @@ static void atmci_detect_change(struct timer_list *t)
if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
return;

- enable_irq(gpio_to_irq(slot->detect_pin));
- present = !(gpio_get_value(slot->detect_pin) ^
+ enable_irq(gpiod_to_irq(slot->detect_pin));
+ present = !(gpiod_get_raw_value(slot->detect_pin) ^
slot->detect_is_active_high);
present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);

@@ -2241,9 +2247,9 @@ static int atmci_init_slot(struct atmel_mci *host,
dev_dbg(&mmc->class_dev,
"slot[%u]: bus_width=%u, detect_pin=%d, "
"detect_is_active_high=%s, wp_pin=%d\n",
- id, slot_data->bus_width, slot_data->detect_pin,
+ id, slot_data->bus_width, desc_to_gpio(slot_data->detect_pin),
slot_data->detect_is_active_high ? "true" : "false",
- slot_data->wp_pin);
+ desc_to_gpio(slot_data->wp_pin));

mmc->ops = &atmci_ops;
mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
@@ -2279,51 +2285,43 @@ static int atmci_init_slot(struct atmel_mci *host,

/* Assume card is present initially */
set_bit(ATMCI_CARD_PRESENT, &slot->flags);
- if (gpio_is_valid(slot->detect_pin)) {
- if (devm_gpio_request(&host->pdev->dev, slot->detect_pin,
- "mmc_detect")) {
- dev_dbg(&mmc->class_dev, "no detect pin available\n");
- slot->detect_pin = -EBUSY;
- } else if (gpio_get_value(slot->detect_pin) ^
- slot->detect_is_active_high) {
+ if (slot->detect_pin) {
+ if (gpiod_get_raw_value(slot->detect_pin) ^
+ slot->detect_is_active_high) {
clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
}
+ } else {
+ dev_dbg(&mmc->class_dev, "no detect pin available\n");
}

- if (!gpio_is_valid(slot->detect_pin)) {
+ if (!slot->detect_pin) {
if (slot_data->non_removable)
mmc->caps |= MMC_CAP_NONREMOVABLE;
else
mmc->caps |= MMC_CAP_NEEDS_POLL;
}

- if (gpio_is_valid(slot->wp_pin)) {
- if (devm_gpio_request(&host->pdev->dev, slot->wp_pin,
- "mmc_wp")) {
- dev_dbg(&mmc->class_dev, "no WP pin available\n");
- slot->wp_pin = -EBUSY;
- }
- }
+ if (!slot->wp_pin)
+ dev_dbg(&mmc->class_dev, "no WP pin available\n");

host->slot[id] = slot;
mmc_regulator_get_supply(mmc);
- mmc_pwrseq_alloc(slot->mmc);
mmc_add_host(mmc);

- if (gpio_is_valid(slot->detect_pin)) {
+ if (slot->detect_pin) {
int ret;

timer_setup(&slot->detect_timer, atmci_detect_change, 0);

- ret = request_irq(gpio_to_irq(slot->detect_pin),
- atmci_detect_interrupt,
- IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
- "mmc-detect", slot);
+ ret = request_irq(gpiod_to_irq(slot->detect_pin),
+ atmci_detect_interrupt,
+ IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
+ "mmc-detect", slot);
if (ret) {
dev_dbg(&mmc->class_dev,
"could not request IRQ %d for detect pin\n",
- gpio_to_irq(slot->detect_pin));
- slot->detect_pin = -EBUSY;
+ gpiod_to_irq(slot->detect_pin));
+ slot->detect_pin = NULL;
}
}

@@ -2342,10 +2340,8 @@ static void atmci_cleanup_slot(struct atmel_mci_slot *slot,

mmc_remove_host(slot->mmc);

- if (gpio_is_valid(slot->detect_pin)) {
- int pin = slot->detect_pin;
-
- free_irq(gpio_to_irq(pin), slot);
+ if (slot->detect_pin) {
+ free_irq(gpiod_to_irq(slot->detect_pin), slot);
del_timer_sync(&slot->detect_timer);
}

diff --git a/include/linux/atmel-mci.h b/include/linux/atmel-mci.h
index 1491af38cc6e..017e7d8f6126 100644
--- a/include/linux/atmel-mci.h
+++ b/include/linux/atmel-mci.h
@@ -26,8 +26,8 @@
*/
struct mci_slot_pdata {
unsigned int bus_width;
- int detect_pin;
- int wp_pin;
+ struct gpio_desc *detect_pin;
+ struct gpio_desc *wp_pin;
bool detect_is_active_high;
bool non_removable;
};
--
2.25.1


2022-12-28 13:43:16

by Ludovic Desroches

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] mmc: atmel-mci: Convert to gpio descriptors

On 26/12/2022 08:39, Balamanikandan Gunasundar wrote:
> Replace the legacy GPIO APIs with gpio descriptor consumer interface.
>
> To maintain backward compatibility, we rely on the "cd-inverted"
> property to manage the invertion flag instead of GPIO property.
>
> Signed-off-by: Balamanikandan Gunasundar <[email protected]>
Reviewed-by: Ludovic Desroches <[email protected]>

> ---
> drivers/mmc/host/atmel-mci.c | 78 +++++++++++++++++-------------------
> include/linux/atmel-mci.h | 4 +-
> 2 files changed, 39 insertions(+), 43 deletions(-)
>
> diff --git a/drivers/mmc/host/atmel-mci.c b/drivers/mmc/host/atmel-mci.c
> index 67b2cd166e56..6ab43733ba9f 100644
> --- a/drivers/mmc/host/atmel-mci.c
> +++ b/drivers/mmc/host/atmel-mci.c
> @@ -11,7 +11,6 @@
> #include <linux/dmaengine.h>
> #include <linux/dma-mapping.h>
> #include <linux/err.h>
> -#include <linux/gpio.h>
> #include <linux/init.h>
> #include <linux/interrupt.h>
> #include <linux/io.h>
> @@ -19,7 +18,8 @@
> #include <linux/module.h>
> #include <linux/of.h>
> #include <linux/of_device.h>
> -#include <linux/of_gpio.h>
> +#include <linux/irq.h>
> +#include <linux/gpio/consumer.h>
> #include <linux/platform_device.h>
> #include <linux/scatterlist.h>
> #include <linux/seq_file.h>
> @@ -389,8 +389,8 @@ struct atmel_mci_slot {
> #define ATMCI_CARD_NEED_INIT 1
> #define ATMCI_SHUTDOWN 2
>
> - int detect_pin;
> - int wp_pin;
> + struct gpio_desc *detect_pin;
> + struct gpio_desc *wp_pin;
> bool detect_is_active_high;
>
> struct timer_list detect_timer;
> @@ -638,7 +638,10 @@ atmci_of_init(struct platform_device *pdev)
> pdata->slot[slot_id].bus_width = 1;
>
> pdata->slot[slot_id].detect_pin =
> - of_get_named_gpio(cnp, "cd-gpios", 0);
> + devm_fwnode_gpiod_get(&pdev->dev, of_fwnode_handle(cnp),
> + "cd", GPIOD_IN, "cd-gpios");
> + if (IS_ERR(pdata->slot[slot_id].detect_pin))
> + pdata->slot[slot_id].detect_pin = NULL;
>
> pdata->slot[slot_id].detect_is_active_high =
> of_property_read_bool(cnp, "cd-inverted");
> @@ -647,7 +650,10 @@ atmci_of_init(struct platform_device *pdev)
> of_property_read_bool(cnp, "non-removable");
>
> pdata->slot[slot_id].wp_pin =
> - of_get_named_gpio(cnp, "wp-gpios", 0);
> + devm_fwnode_gpiod_get(&pdev->dev, of_fwnode_handle(cnp),
> + "wp", GPIOD_IN, "wp-gpios");
> + if (IS_ERR(pdata->slot[slot_id].wp_pin))
> + pdata->slot[slot_id].wp_pin = NULL;
> }
>
> return pdata;
> @@ -1511,8 +1517,8 @@ static int atmci_get_ro(struct mmc_host *mmc)
> int read_only = -ENOSYS;
> struct atmel_mci_slot *slot = mmc_priv(mmc);
>
> - if (gpio_is_valid(slot->wp_pin)) {
> - read_only = gpio_get_value(slot->wp_pin);
> + if (slot->wp_pin) {
> + read_only = gpiod_get_value(slot->wp_pin);
> dev_dbg(&mmc->class_dev, "card is %s\n",
> read_only ? "read-only" : "read-write");
> }
> @@ -1525,8 +1531,8 @@ static int atmci_get_cd(struct mmc_host *mmc)
> int present = -ENOSYS;
> struct atmel_mci_slot *slot = mmc_priv(mmc);
>
> - if (gpio_is_valid(slot->detect_pin)) {
> - present = !(gpio_get_value(slot->detect_pin) ^
> + if (slot->detect_pin) {
> + present = !(gpiod_get_raw_value(slot->detect_pin) ^
> slot->detect_is_active_high);
> dev_dbg(&mmc->class_dev, "card is %spresent\n",
> present ? "" : "not ");
> @@ -1639,8 +1645,8 @@ static void atmci_detect_change(struct timer_list *t)
> if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
> return;
>
> - enable_irq(gpio_to_irq(slot->detect_pin));
> - present = !(gpio_get_value(slot->detect_pin) ^
> + enable_irq(gpiod_to_irq(slot->detect_pin));
> + present = !(gpiod_get_raw_value(slot->detect_pin) ^
> slot->detect_is_active_high);
> present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
>
> @@ -2241,9 +2247,9 @@ static int atmci_init_slot(struct atmel_mci *host,
> dev_dbg(&mmc->class_dev,
> "slot[%u]: bus_width=%u, detect_pin=%d, "
> "detect_is_active_high=%s, wp_pin=%d\n",
> - id, slot_data->bus_width, slot_data->detect_pin,
> + id, slot_data->bus_width, desc_to_gpio(slot_data->detect_pin),
> slot_data->detect_is_active_high ? "true" : "false",
> - slot_data->wp_pin);
> + desc_to_gpio(slot_data->wp_pin));
>
> mmc->ops = &atmci_ops;
> mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
> @@ -2279,51 +2285,43 @@ static int atmci_init_slot(struct atmel_mci *host,
>
> /* Assume card is present initially */
> set_bit(ATMCI_CARD_PRESENT, &slot->flags);
> - if (gpio_is_valid(slot->detect_pin)) {
> - if (devm_gpio_request(&host->pdev->dev, slot->detect_pin,
> - "mmc_detect")) {
> - dev_dbg(&mmc->class_dev, "no detect pin available\n");
> - slot->detect_pin = -EBUSY;
> - } else if (gpio_get_value(slot->detect_pin) ^
> - slot->detect_is_active_high) {
> + if (slot->detect_pin) {
> + if (gpiod_get_raw_value(slot->detect_pin) ^
> + slot->detect_is_active_high) {
> clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
> }
> + } else {
> + dev_dbg(&mmc->class_dev, "no detect pin available\n");
> }
>
> - if (!gpio_is_valid(slot->detect_pin)) {
> + if (!slot->detect_pin) {
> if (slot_data->non_removable)
> mmc->caps |= MMC_CAP_NONREMOVABLE;
> else
> mmc->caps |= MMC_CAP_NEEDS_POLL;
> }
>
> - if (gpio_is_valid(slot->wp_pin)) {
> - if (devm_gpio_request(&host->pdev->dev, slot->wp_pin,
> - "mmc_wp")) {
> - dev_dbg(&mmc->class_dev, "no WP pin available\n");
> - slot->wp_pin = -EBUSY;
> - }
> - }
> + if (!slot->wp_pin)
> + dev_dbg(&mmc->class_dev, "no WP pin available\n");
>
> host->slot[id] = slot;
> mmc_regulator_get_supply(mmc);
> - mmc_pwrseq_alloc(slot->mmc);
> mmc_add_host(mmc);
>
> - if (gpio_is_valid(slot->detect_pin)) {
> + if (slot->detect_pin) {
> int ret;
>
> timer_setup(&slot->detect_timer, atmci_detect_change, 0);
>
> - ret = request_irq(gpio_to_irq(slot->detect_pin),
> - atmci_detect_interrupt,
> - IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
> - "mmc-detect", slot);
> + ret = request_irq(gpiod_to_irq(slot->detect_pin),
> + atmci_detect_interrupt,
> + IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
> + "mmc-detect", slot);
> if (ret) {
> dev_dbg(&mmc->class_dev,
> "could not request IRQ %d for detect pin\n",
> - gpio_to_irq(slot->detect_pin));
> - slot->detect_pin = -EBUSY;
> + gpiod_to_irq(slot->detect_pin));
> + slot->detect_pin = NULL;
> }
> }
>
> @@ -2342,10 +2340,8 @@ static void atmci_cleanup_slot(struct atmel_mci_slot *slot,
>
> mmc_remove_host(slot->mmc);
>
> - if (gpio_is_valid(slot->detect_pin)) {
> - int pin = slot->detect_pin;
> -
> - free_irq(gpio_to_irq(pin), slot);
> + if (slot->detect_pin) {
> + free_irq(gpiod_to_irq(slot->detect_pin), slot);
> del_timer_sync(&slot->detect_timer);
> }
>
> diff --git a/include/linux/atmel-mci.h b/include/linux/atmel-mci.h
> index 1491af38cc6e..017e7d8f6126 100644
> --- a/include/linux/atmel-mci.h
> +++ b/include/linux/atmel-mci.h
> @@ -26,8 +26,8 @@
> */
> struct mci_slot_pdata {
> unsigned int bus_width;
> - int detect_pin;
> - int wp_pin;
> + struct gpio_desc *detect_pin;
> + struct gpio_desc *wp_pin;
> bool detect_is_active_high;
> bool non_removable;
> };

2022-12-29 00:52:11

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] mmc: atmel-mci: Convert to gpio descriptors

Hi Balamanikandan,

thanks for your patch!

On Mon, Dec 26, 2022 at 8:39 AM Balamanikandan Gunasundar
<[email protected]> wrote:

> Replace the legacy GPIO APIs with gpio descriptor consumer interface.
>
> To maintain backward compatibility, we rely on the "cd-inverted"
> property to manage the invertion flag instead of GPIO property.
>
> Signed-off-by: Balamanikandan Gunasundar <[email protected]>

Overall this looks really nice! I thought about converting this driver
but was a bit afraid of doing mistakes since it was a bit elaborate.
Nice that you use fwnode accessors!

Acked-by: Linus Walleij <[email protected]>

Improvement to consider (can also be a separate patch):

> + if (slot->detect_pin) {
> + present = !(gpiod_get_raw_value(slot->detect_pin) ^
> slot->detect_is_active_high);

Normally we contain all this active low/high mess in mmc_of_parse(),
but I guess this doesn't work here because the of node is different
from the device :P

Since the code says *explicitly* "active high" not "inverted" as the
core code parses it, could you try just push this to gpiolib like
we usually do, by deleting the detect_is_active_high stuff and
just apply a patch like this?

diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index 4fff7258ee41..5979c9a75cf9 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -199,7 +199,9 @@ static void of_gpio_set_polarity_by_property(const
struct device_node *np,
{ "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" },
{ "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" },
#endif
-
+#if IS_ENABLED(CONFIG_MMC_ATMELMCI)
+ { "atmel,hsmci", "cd-gpios", "cd-inverted" },
+#endif
/*
* The regulator GPIO handles are specified such that the
* presence or absence of "enable-active-high" solely controls

(It's fine to include in your MMC patch if some GPIO maintainer like
me ACK it.)

Yours,
Linus Walleij

2023-01-02 15:19:42

by Ulf Hansson

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] mmc: atmel-mci: Convert to gpio descriptors

On Thu, 29 Dec 2022 at 01:23, Linus Walleij <[email protected]> wrote:
>
> Hi Balamanikandan,
>
> thanks for your patch!
>
> On Mon, Dec 26, 2022 at 8:39 AM Balamanikandan Gunasundar
> <[email protected]> wrote:
>
> > Replace the legacy GPIO APIs with gpio descriptor consumer interface.
> >
> > To maintain backward compatibility, we rely on the "cd-inverted"
> > property to manage the invertion flag instead of GPIO property.
> >
> > Signed-off-by: Balamanikandan Gunasundar <[email protected]>
>
> Overall this looks really nice! I thought about converting this driver
> but was a bit afraid of doing mistakes since it was a bit elaborate.
> Nice that you use fwnode accessors!
>
> Acked-by: Linus Walleij <[email protected]>

Linus, thanks for helping out by reviewing this!

>
> Improvement to consider (can also be a separate patch):

I tried to apply the $subject patch as is, but it failed. It needs to
be rebased on top of my latest next branch. Please re-spin and if
possible, consider incorporating Linus's suggestions too as part of
the series.

Kind regards
Uffe



>
> > + if (slot->detect_pin) {
> > + present = !(gpiod_get_raw_value(slot->detect_pin) ^
> > slot->detect_is_active_high);
>
> Normally we contain all this active low/high mess in mmc_of_parse(),
> but I guess this doesn't work here because the of node is different
> from the device :P
>
> Since the code says *explicitly* "active high" not "inverted" as the
> core code parses it, could you try just push this to gpiolib like
> we usually do, by deleting the detect_is_active_high stuff and
> just apply a patch like this?
>
> diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
> index 4fff7258ee41..5979c9a75cf9 100644
> --- a/drivers/gpio/gpiolib-of.c
> +++ b/drivers/gpio/gpiolib-of.c
> @@ -199,7 +199,9 @@ static void of_gpio_set_polarity_by_property(const
> struct device_node *np,
> { "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" },
> { "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" },
> #endif
> -
> +#if IS_ENABLED(CONFIG_MMC_ATMELMCI)
> + { "atmel,hsmci", "cd-gpios", "cd-inverted" },
> +#endif
> /*
> * The regulator GPIO handles are specified such that the
> * presence or absence of "enable-active-high" solely controls
>
> (It's fine to include in your MMC patch if some GPIO maintainer like
> me ACK it.)
>
> Yours,
> Linus Walleij