2 * Copyright (C) 2014 Linaro Ltd
4 * Author: Ulf Hansson <ulf.hansson@linaro.org>
6 * License terms: GNU General Public License (GPL) version 2
8 * Simple MMC power sequence management
10 #include <linux/clk.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/of_gpio.h>
16 #include <linux/gpio/consumer.h>
18 #include <linux/mmc/host.h>
22 struct mmc_pwrseq_simple {
23 struct mmc_pwrseq pwrseq;
26 struct gpio_descs *reset_gpios;
29 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple *pwrseq,
32 struct gpio_descs *reset_gpios = pwrseq->reset_gpios;
34 if (!IS_ERR(reset_gpios)) {
36 int values[reset_gpios->ndescs];
38 for (i = 0; i < reset_gpios->ndescs; i++)
41 gpiod_set_array_value_cansleep(
42 reset_gpios->ndescs, reset_gpios->desc, values);
46 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host)
48 struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq,
49 struct mmc_pwrseq_simple, pwrseq);
51 if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) {
52 clk_prepare_enable(pwrseq->ext_clk);
53 pwrseq->clk_enabled = true;
56 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
59 static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host)
61 struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq,
62 struct mmc_pwrseq_simple, pwrseq);
64 mmc_pwrseq_simple_set_gpios_value(pwrseq, 0);
67 static void mmc_pwrseq_simple_power_off(struct mmc_host *host)
69 struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq,
70 struct mmc_pwrseq_simple, pwrseq);
72 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
74 if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) {
75 clk_disable_unprepare(pwrseq->ext_clk);
76 pwrseq->clk_enabled = false;
80 static void mmc_pwrseq_simple_free(struct mmc_host *host)
82 struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq,
83 struct mmc_pwrseq_simple, pwrseq);
85 if (!IS_ERR(pwrseq->reset_gpios))
86 gpiod_put_array(pwrseq->reset_gpios);
88 if (!IS_ERR(pwrseq->ext_clk))
89 clk_put(pwrseq->ext_clk);
94 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {
95 .pre_power_on = mmc_pwrseq_simple_pre_power_on,
96 .post_power_on = mmc_pwrseq_simple_post_power_on,
97 .power_off = mmc_pwrseq_simple_power_off,
98 .free = mmc_pwrseq_simple_free,
101 struct mmc_pwrseq *mmc_pwrseq_simple_alloc(struct mmc_host *host,
104 struct mmc_pwrseq_simple *pwrseq;
107 pwrseq = kzalloc(sizeof(*pwrseq), GFP_KERNEL);
109 return ERR_PTR(-ENOMEM);
111 pwrseq->ext_clk = clk_get(dev, "ext_clock");
112 if (IS_ERR(pwrseq->ext_clk) &&
113 PTR_ERR(pwrseq->ext_clk) != -ENOENT) {
114 ret = PTR_ERR(pwrseq->ext_clk);
118 pwrseq->reset_gpios = gpiod_get_array(dev, "reset", GPIOD_OUT_HIGH);
119 if (IS_ERR(pwrseq->reset_gpios) &&
120 PTR_ERR(pwrseq->reset_gpios) != -ENOENT &&
121 PTR_ERR(pwrseq->reset_gpios) != -ENOSYS) {
122 ret = PTR_ERR(pwrseq->reset_gpios);
126 pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops;
128 return &pwrseq->pwrseq;
130 if (!IS_ERR(pwrseq->ext_clk))
131 clk_put(pwrseq->ext_clk);