mmc: pwrseq: simplify alloc/free hooks
PATCH 1/1 mmc: pwrseq: simplify alloc/free hooks
The alloc() and free() hooks required each pwrseq implementation to set host->pwrseq themselves. This is error-prone and could be done at a higher level if alloc() was changed to return a pointer to a struct mmc_pwrseq instead of an error code.
This patch performs this change and moves the burden of maintaining host->pwrseq from the power sequence hooks to the pwrseq code.
---
It occured to me that after this change, none of the alloc() implementations ever uses the host parameter. I have kept it in case it could be useful for other purposes, but can remove it in a v2 if this is deemed better by the MMC maintainers.
drivers/mmc/core/pwrseq.c | 16 ++++++++++++---- drivers/mmc/core/pwrseq.h | 6 ++++-- drivers/mmc/core/pwrseq_emmc.c | 11 +++++------ drivers/mmc/core/pwrseq_simple.c | 11 +++++------ 4 files changed, 26 insertions(+), 18 deletions(-)
drivers/mmc/core/pwrseq.c +12 -4
@@ -19,7 +19,7 @@struct mmc_pwrseq_match {const char *compatible; -int (*alloc)(struct mmc_host *host,struct device *dev); +struct mmc_pwrseq *(*alloc)(struct mmc_host *host,struct device *dev); };static struct mmc_pwrseq_match pwrseq_match[] = {
@@ -52,6 +52,7 @@ int mmc_pwrseq_alloc(struct mmc_host *host)struct platform_device *pdev;struct device_node *np;struct mmc_pwrseq_match *match; +struct mmc_pwrseq *pwrseq;int ret =0 ; np =of_parse_phandle (host->parent->of_node,"mmc-pwrseq" ,0 );
@@ -70,9 +71,14 @@ int mmc_pwrseq_alloc(struct mmc_host *host) goto err; } - ret = match->alloc (host, &pdev->dev); -if (!ret) -dev_info (host->parent,"allocated mmc-pwrseq\n" ); + pwrseq = match->alloc (host, &pdev->dev); +if (IS_ERR (pwrseq)) { + ret =PTR_ERR (host->pwrseq); + goto err; + } + + host->pwrseq = pwrseq; +dev_info (host->parent,"allocated mmc-pwrseq\n" ); err:of_node_put (np);
@@ -109,4 +115,6 @@ void mmc_pwrseq_free(struct mmc_host *host)if (pwrseq && pwrseq->ops && pwrseq->ops->free) pwrseq->ops->free (host); + + host->pwrseq =NULL ; }
drivers/mmc/core/pwrseq.h +4 -2
@@ -27,8 +27,10 @@ void mmc_pwrseq_post_power_on(struct mmc_host *host);void mmc_pwrseq_power_off (struct mmc_host *host);void mmc_pwrseq_free (struct mmc_host *host); -int mmc_pwrseq_simple_alloc (struct mmc_host *host,struct device *dev); -int mmc_pwrseq_emmc_alloc (struct mmc_host *host,struct device *dev); +struct mmc_pwrseq *mmc_pwrseq_simple_alloc (struct mmc_host *host, +struct device *dev); +struct mmc_pwrseq *mmc_pwrseq_emmc_alloc (struct mmc_host *host, +struct device *dev);#else
drivers/mmc/core/pwrseq_emmc.c +5 -6
@@ -49,7 +49,6 @@ static void mmc_pwrseq_emmc_free(struct mmc_host *host)unregister_restart_handler (&pwrseq->reset_nb);gpiod_put (pwrseq->reset_gpio);kfree (pwrseq); - host->pwrseq =NULL ; }static struct mmc_pwrseq_ops mmc_pwrseq_emmc_ops = {
@@ -67,14 +66,15 @@ static int mmc_pwrseq_emmc_reset_nb(struct notifier_block *this,return NOTIFY_DONE ; } -int mmc_pwrseq_emmc_alloc (struct mmc_host *host,struct device *dev) +struct mmc_pwrseq *mmc_pwrseq_emmc_alloc (struct mmc_host *host, +struct device *dev) {struct mmc_pwrseq_emmc *pwrseq;int ret =0 ; pwrseq =kzalloc (sizeof (struct mmc_pwrseq_emmc ),GFP_KERNEL );if (!pwrseq) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); pwrseq->reset_gpio =gpiod_get_index (dev,"reset" ,0 ,GPIOD_OUT_LOW );if (IS_ERR (pwrseq->reset_gpio)) {
@@ -92,10 +92,9 @@ int mmc_pwrseq_emmc_alloc(struct mmc_host *host, struct device *dev)register_restart_handler (&pwrseq->reset_nb); pwrseq->pwrseq.ops = &mmc_pwrseq_emmc_ops; - host->pwrseq = &pwrseq->pwrseq; -return 0 ; +return &pwrseq->pwrseq; free:kfree (pwrseq); -return ret; +return ERR_PTR (ret); }
drivers/mmc/core/pwrseq_simple.c +5 -6
@@ -85,7 +85,6 @@ static void mmc_pwrseq_simple_free(struct mmc_host *host)clk_put (pwrseq->ext_clk);kfree (pwrseq); - host->pwrseq =NULL ; }static struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {
@@ -95,7 +94,8 @@ static struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = { .free = mmc_pwrseq_simple_free, }; -int mmc_pwrseq_simple_alloc (struct mmc_host *host,struct device *dev) +struct mmc_pwrseq *mmc_pwrseq_simple_alloc (struct mmc_host *host, +struct device *dev) {struct mmc_pwrseq_simple *pwrseq;int i, nr_gpios, ret =0 ;
@@ -107,7 +107,7 @@ int mmc_pwrseq_simple_alloc(struct mmc_host *host, struct device *dev) pwrseq =kzalloc (sizeof (struct mmc_pwrseq_simple ) + nr_gpios *sizeof (struct gpio_desc *),GFP_KERNEL );if (!pwrseq) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); pwrseq->ext_clk =clk_get (dev,"ext_clock" ); if (IS_ERR(pwrseq->ext_clk) &&
@@ -133,13 +133,12 @@ int mmc_pwrseq_simple_alloc(struct mmc_host *host, struct device *dev) pwrseq->nr_gpios = nr_gpios; pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops; - host->pwrseq = &pwrseq->pwrseq; -return 0 ; +return &pwrseq->pwrseq; clk_put:if (!IS_ERR (pwrseq->ext_clk))clk_put (pwrseq->ext_clk); free:kfree (pwrseq); -return ret; +return ERR_PTR (ret); }
Signature · 1 line
2.3.0
Chris, Ulf, gentle ping?
Quoted 177 lines · Alexandre Courbot
The
alloc()andfree()hooks required each pwrseq implementation to sethost->pwrseqthemselves. This is error-prone and could be done at a higher level ifalloc()was changed to return a pointer to a structmmc_pwrseqinstead of an error code.This patch performs this change and moves the burden of maintaining
host->pwrseqfrom the power sequence hooks to the pwrseq code.Signed-off-by Alexandre Courbot <[email protected]>---
It occured to me that after this change, none of thealloc()implementations ever uses the host parameter. I have kept it in case it could be useful for other purposes, but can remove it in a v2 if this is deemed better by the MMC maintainers.drivers/mmc/core/pwrseq.c | 16 ++++++++++++---- drivers/mmc/core/pwrseq.h | 6 ++++-- drivers/mmc/core/pwrseq_emmc.c | 11 +++++------ drivers/mmc/core/pwrseq_simple.c | 11 +++++------ 4 files changed, 26 insertions(+), 18 deletions(-)drivers/mmc/core/pwrseq.c +12
-4@@ -19,7 +19,7 @@struct mmc_pwrseq_match {const char *compatible; -int (*alloc)(struct mmc_host *host,struct device *dev); +struct mmc_pwrseq *(*alloc)(struct mmc_host *host,struct device *dev); };static struct mmc_pwrseq_match pwrseq_match[] = {@@ -52,6 +52,7 @@ int mmc_pwrseq_alloc(struct mmc_host *host)struct platform_device *pdev;struct device_node *np;struct mmc_pwrseq_match *match; +struct mmc_pwrseq *pwrseq;int ret =0 ; np =of_parse_phandle (host->parent->of_node,"mmc-pwrseq" ,0 );@@ -70,9 +71,14 @@ int mmc_pwrseq_alloc(struct mmc_host *host) goto err; } - ret = match->alloc (host, &pdev->dev); -if (!ret) -dev_info (host->parent,"allocated mmc-pwrseq\n" ); + pwrseq = match->alloc (host, &pdev->dev); +if (IS_ERR (pwrseq)) { + ret =PTR_ERR (host->pwrseq); + goto err; + } + + host->pwrseq = pwrseq; +dev_info (host->parent,"allocated mmc-pwrseq\n" ); err:of_node_put (np);@@ -109,4 +115,6 @@ void mmc_pwrseq_free(struct mmc_host *host)if (pwrseq && pwrseq->ops && pwrseq->ops->free) pwrseq->ops->free (host); + + host->pwrseq =NULL ; }drivers/mmc/core/pwrseq.h +4
-2@@ -27,8 +27,10 @@ void mmc_pwrseq_post_power_on(struct mmc_host *host);void mmc_pwrseq_power_off (struct mmc_host *host);void mmc_pwrseq_free (struct mmc_host *host); -int mmc_pwrseq_simple_alloc (struct mmc_host *host,struct device *dev); -int mmc_pwrseq_emmc_alloc (struct mmc_host *host,struct device *dev); +struct mmc_pwrseq *mmc_pwrseq_simple_alloc (struct mmc_host *host, +struct device *dev); +struct mmc_pwrseq *mmc_pwrseq_emmc_alloc (struct mmc_host *host, +struct device *dev);#else drivers/mmc/core/pwrseq_emmc.c +5
-6@@ -49,7 +49,6 @@ static void mmc_pwrseq_emmc_free(struct mmc_host *host)unregister_restart_handler (&pwrseq->reset_nb);gpiod_put (pwrseq->reset_gpio);kfree (pwrseq); - host->pwrseq =NULL ; }static struct mmc_pwrseq_ops mmc_pwrseq_emmc_ops = {@@ -67,14 +66,15 @@ static int mmc_pwrseq_emmc_reset_nb(struct notifier_block *this,return NOTIFY_DONE ; } -int mmc_pwrseq_emmc_alloc (struct mmc_host *host,struct device *dev) +struct mmc_pwrseq *mmc_pwrseq_emmc_alloc (struct mmc_host *host, +struct device *dev) {struct mmc_pwrseq_emmc *pwrseq;int ret =0 ; pwrseq =kzalloc (sizeof (struct mmc_pwrseq_emmc ),GFP_KERNEL );if (!pwrseq) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); pwrseq->reset_gpio =gpiod_get_index (dev,"reset" ,0 ,GPIOD_OUT_LOW );if (IS_ERR (pwrseq->reset_gpio)) {@@ -92,10 +92,9 @@ int mmc_pwrseq_emmc_alloc(struct mmc_host *host, struct device *dev)register_restart_handler (&pwrseq->reset_nb); pwrseq->pwrseq.ops = &mmc_pwrseq_emmc_ops; - host->pwrseq = &pwrseq->pwrseq; -return 0 ; +return &pwrseq->pwrseq; free:kfree (pwrseq); -return ret; +return ERR_PTR (ret); }drivers/mmc/core/pwrseq_simple.c +5
-7@@ -85,7 +85,6 @@ static void mmc_pwrseq_simple_free(struct mmc_host *host)clk_put (pwrseq->ext_clk);kfree (pwrseq); - host->pwrseq =NULL ; }static struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {@@ -95,7 +94,8 @@ static struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = { .free = mmc_pwrseq_simple_free, }; -int mmc_pwrseq_simple_alloc (struct mmc_host *host,struct device *dev) +struct mmc_pwrseq *mmc_pwrseq_simple_alloc (struct mmc_host *host, +struct device *dev) {struct mmc_pwrseq_simple *pwrseq;int i, nr_gpios, ret =0 ;@@ -107,7 +107,7 @@ int mmc_pwrseq_simple_alloc(struct mmc_host *host, struct device *dev) pwrseq =kzalloc (sizeof (struct mmc_pwrseq_simple ) + nr_gpios *sizeof (struct gpio_desc *),GFP_KERNEL );if (!pwrseq) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); pwrseq->ext_clk =clk_get (dev,"ext_clock" ); if (IS_ERR(pwrseq->ext_clk) &&@@ -133,13 +133,12 @@ int mmc_pwrseq_simple_alloc(struct mmc_host *host, struct device *dev) pwrseq->nr_gpios = nr_gpios; pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops; - host->pwrseq = &pwrseq->pwrseq; -return 0 ; +return &pwrseq->pwrseq; clk_put:if (!IS_ERR (pwrseq->ext_clk))clk_put (pwrseq->ext_clk); free:kfree (pwrseq); -return ret; +return ERR_PTR (ret); } --2.3.0
Ulf Hansson
AppliedQuoted 9 lines · Alexandre Courbot
The
alloc()andfree()hooks required each pwrseq implementation to sethost->pwrseqthemselves. This is error-prone and could be done at a higher level ifalloc()was changed to return a pointer to a structmmc_pwrseqinstead of an error code.This patch performs this change and moves the burden of maintaining
host->pwrseqfrom the power sequence hooks to the pwrseq code.Signed-off-by Alexandre Courbot <[email protected]>
Applied, thanks!
Kind regards
Uffe
Quoted 168 lines
---
It occured to me that after this change, none of thealloc()implementations ever uses the host parameter. I have kept it in case it could be useful for other purposes, but can remove it in a v2 if this is deemed better by the MMC maintainers.drivers/mmc/core/pwrseq.c | 16 ++++++++++++---- drivers/mmc/core/pwrseq.h | 6 ++++-- drivers/mmc/core/pwrseq_emmc.c | 11 +++++------ drivers/mmc/core/pwrseq_simple.c | 11 +++++------ 4 files changed, 26 insertions(+), 18 deletions(-)drivers/mmc/core/pwrseq.c +12
-4@@ -19,7 +19,7 @@struct mmc_pwrseq_match {const char *compatible; -int (*alloc)(struct mmc_host *host,struct device *dev); +struct mmc_pwrseq *(*alloc)(struct mmc_host *host,struct device *dev); };static struct mmc_pwrseq_match pwrseq_match[] = {@@ -52,6 +52,7 @@ int mmc_pwrseq_alloc(struct mmc_host *host)struct platform_device *pdev;struct device_node *np;struct mmc_pwrseq_match *match; +struct mmc_pwrseq *pwrseq;int ret =0 ; np =of_parse_phandle (host->parent->of_node,"mmc-pwrseq" ,0 );@@ -70,9 +71,14 @@ int mmc_pwrseq_alloc(struct mmc_host *host) goto err; } - ret = match->alloc (host, &pdev->dev); -if (!ret) -dev_info (host->parent,"allocated mmc-pwrseq\n" ); + pwrseq = match->alloc (host, &pdev->dev); +if (IS_ERR (pwrseq)) { + ret =PTR_ERR (host->pwrseq); + goto err; + } + + host->pwrseq = pwrseq; +dev_info (host->parent,"allocated mmc-pwrseq\n" ); err:of_node_put (np);@@ -109,4 +115,6 @@ void mmc_pwrseq_free(struct mmc_host *host)if (pwrseq && pwrseq->ops && pwrseq->ops->free) pwrseq->ops->free (host); + + host->pwrseq =NULL ; }drivers/mmc/core/pwrseq.h +4
-2@@ -27,8 +27,10 @@ void mmc_pwrseq_post_power_on(struct mmc_host *host);void mmc_pwrseq_power_off (struct mmc_host *host);void mmc_pwrseq_free (struct mmc_host *host); -int mmc_pwrseq_simple_alloc (struct mmc_host *host,struct device *dev); -int mmc_pwrseq_emmc_alloc (struct mmc_host *host,struct device *dev); +struct mmc_pwrseq *mmc_pwrseq_simple_alloc (struct mmc_host *host, +struct device *dev); +struct mmc_pwrseq *mmc_pwrseq_emmc_alloc (struct mmc_host *host, +struct device *dev);#else drivers/mmc/core/pwrseq_emmc.c +5
-6@@ -49,7 +49,6 @@ static void mmc_pwrseq_emmc_free(struct mmc_host *host)unregister_restart_handler (&pwrseq->reset_nb);gpiod_put (pwrseq->reset_gpio);kfree (pwrseq); - host->pwrseq =NULL ; }static struct mmc_pwrseq_ops mmc_pwrseq_emmc_ops = {@@ -67,14 +66,15 @@ static int mmc_pwrseq_emmc_reset_nb(struct notifier_block *this,return NOTIFY_DONE ; } -int mmc_pwrseq_emmc_alloc (struct mmc_host *host,struct device *dev) +struct mmc_pwrseq *mmc_pwrseq_emmc_alloc (struct mmc_host *host, +struct device *dev) {struct mmc_pwrseq_emmc *pwrseq;int ret =0 ; pwrseq =kzalloc (sizeof (struct mmc_pwrseq_emmc ),GFP_KERNEL );if (!pwrseq) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); pwrseq->reset_gpio =gpiod_get_index (dev,"reset" ,0 ,GPIOD_OUT_LOW );if (IS_ERR (pwrseq->reset_gpio)) {@@ -92,10 +92,9 @@ int mmc_pwrseq_emmc_alloc(struct mmc_host *host, struct device *dev)register_restart_handler (&pwrseq->reset_nb); pwrseq->pwrseq.ops = &mmc_pwrseq_emmc_ops; - host->pwrseq = &pwrseq->pwrseq; -return 0 ; +return &pwrseq->pwrseq; free:kfree (pwrseq); -return ret; +return ERR_PTR (ret); }drivers/mmc/core/pwrseq_simple.c +5
-7@@ -85,7 +85,6 @@ static void mmc_pwrseq_simple_free(struct mmc_host *host)clk_put (pwrseq->ext_clk);kfree (pwrseq); - host->pwrseq =NULL ; }static struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {@@ -95,7 +94,8 @@ static struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = { .free = mmc_pwrseq_simple_free, }; -int mmc_pwrseq_simple_alloc (struct mmc_host *host,struct device *dev) +struct mmc_pwrseq *mmc_pwrseq_simple_alloc (struct mmc_host *host, +struct device *dev) {struct mmc_pwrseq_simple *pwrseq;int i, nr_gpios, ret =0 ;@@ -107,7 +107,7 @@ int mmc_pwrseq_simple_alloc(struct mmc_host *host, struct device *dev) pwrseq =kzalloc (sizeof (struct mmc_pwrseq_simple ) + nr_gpios *sizeof (struct gpio_desc *),GFP_KERNEL );if (!pwrseq) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); pwrseq->ext_clk =clk_get (dev,"ext_clock" ); if (IS_ERR(pwrseq->ext_clk) &&@@ -133,13 +133,12 @@ int mmc_pwrseq_simple_alloc(struct mmc_host *host, struct device *dev) pwrseq->nr_gpios = nr_gpios; pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops; - host->pwrseq = &pwrseq->pwrseq; -return 0 ; +return &pwrseq->pwrseq; clk_put:if (!IS_ERR (pwrseq->ext_clk))clk_put (pwrseq->ext_clk); free:kfree (pwrseq); -return ret; +return ERR_PTR (ret); } --2.3.0