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/gpio/consumer.h>
17 #include <linux/mmc/host.h>
21 struct mmc_pwrseq_simple
{
22 struct mmc_pwrseq pwrseq
;
25 struct gpio_descs
*reset_gpios
;
28 #define to_pwrseq_simple(p) container_of(p, struct mmc_pwrseq_simple, pwrseq)
30 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple
*pwrseq
,
33 struct gpio_descs
*reset_gpios
= pwrseq
->reset_gpios
;
35 if (!IS_ERR(reset_gpios
)) {
37 int values
[reset_gpios
->ndescs
];
39 for (i
= 0; i
< reset_gpios
->ndescs
; i
++)
42 gpiod_set_array_value_cansleep(
43 reset_gpios
->ndescs
, reset_gpios
->desc
, values
);
47 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host
*host
)
49 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->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
= to_pwrseq_simple(host
->pwrseq
);
63 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 0);
66 static void mmc_pwrseq_simple_power_off(struct mmc_host
*host
)
68 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
70 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
72 if (!IS_ERR(pwrseq
->ext_clk
) && pwrseq
->clk_enabled
) {
73 clk_disable_unprepare(pwrseq
->ext_clk
);
74 pwrseq
->clk_enabled
= false;
78 static void mmc_pwrseq_simple_free(struct mmc_host
*host
)
80 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
82 if (!IS_ERR(pwrseq
->reset_gpios
))
83 gpiod_put_array(pwrseq
->reset_gpios
);
85 if (!IS_ERR(pwrseq
->ext_clk
))
86 clk_put(pwrseq
->ext_clk
);
91 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops
= {
92 .pre_power_on
= mmc_pwrseq_simple_pre_power_on
,
93 .post_power_on
= mmc_pwrseq_simple_post_power_on
,
94 .power_off
= mmc_pwrseq_simple_power_off
,
95 .free
= mmc_pwrseq_simple_free
,
98 struct mmc_pwrseq
*mmc_pwrseq_simple_alloc(struct mmc_host
*host
,
101 struct mmc_pwrseq_simple
*pwrseq
;
104 pwrseq
= kzalloc(sizeof(*pwrseq
), GFP_KERNEL
);
106 return ERR_PTR(-ENOMEM
);
108 pwrseq
->ext_clk
= clk_get(dev
, "ext_clock");
109 if (IS_ERR(pwrseq
->ext_clk
) &&
110 PTR_ERR(pwrseq
->ext_clk
) != -ENOENT
) {
111 ret
= PTR_ERR(pwrseq
->ext_clk
);
115 pwrseq
->reset_gpios
= gpiod_get_array(dev
, "reset", GPIOD_OUT_HIGH
);
116 if (IS_ERR(pwrseq
->reset_gpios
) &&
117 PTR_ERR(pwrseq
->reset_gpios
) != -ENOENT
&&
118 PTR_ERR(pwrseq
->reset_gpios
) != -ENOSYS
) {
119 ret
= PTR_ERR(pwrseq
->reset_gpios
);
123 pwrseq
->pwrseq
.ops
= &mmc_pwrseq_simple_ops
;
125 return &pwrseq
->pwrseq
;
127 if (!IS_ERR(pwrseq
->ext_clk
))
128 clk_put(pwrseq
->ext_clk
);