Skip to content

Commit

Permalink
mmc: pwrseq: simplify alloc/free hooks
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
  • Loading branch information
Gnurou authored and storulf committed Mar 23, 2015
1 parent d34712d commit 0f12a0c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
16 changes: 12 additions & 4 deletions drivers/mmc/core/pwrseq.c
Expand Up @@ -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[] = {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
6 changes: 4 additions & 2 deletions drivers/mmc/core/pwrseq.h
Expand Up @@ -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

Expand Down
11 changes: 5 additions & 6 deletions drivers/mmc/core/pwrseq_emmc.c
Expand Up @@ -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 = {
Expand All @@ -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)) {
Expand All @@ -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);
}
11 changes: 5 additions & 6 deletions drivers/mmc/core/pwrseq_simple.c
Expand Up @@ -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 = {
Expand All @@ -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;
Expand All @@ -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) &&
Expand All @@ -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);
}

0 comments on commit 0f12a0c

Please sign in to comment.