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 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple
*pwrseq
,
31 struct gpio_descs
*reset_gpios
= pwrseq
->reset_gpios
;
33 if (!IS_ERR(reset_gpios
)) {
35 int values
[reset_gpios
->ndescs
];
37 for (i
= 0; i
< reset_gpios
->ndescs
; i
++)
40 gpiod_set_array_value_cansleep(
41 reset_gpios
->ndescs
, reset_gpios
->desc
, values
);
45 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host
*host
)
47 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
48 struct mmc_pwrseq_simple
, pwrseq
);
50 if (!IS_ERR(pwrseq
->ext_clk
) && !pwrseq
->clk_enabled
) {
51 clk_prepare_enable(pwrseq
->ext_clk
);
52 pwrseq
->clk_enabled
= true;
55 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
58 static void mmc_pwrseq_simple_post_power_on(struct mmc_host
*host
)
60 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
61 struct mmc_pwrseq_simple
, 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
= container_of(host
->pwrseq
,
69 struct mmc_pwrseq_simple
, pwrseq
);
71 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
73 if (!IS_ERR(pwrseq
->ext_clk
) && pwrseq
->clk_enabled
) {
74 clk_disable_unprepare(pwrseq
->ext_clk
);
75 pwrseq
->clk_enabled
= false;
79 static void mmc_pwrseq_simple_free(struct mmc_host
*host
)
81 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
82 struct mmc_pwrseq_simple
, pwrseq
);
84 if (!IS_ERR(pwrseq
->reset_gpios
))
85 gpiod_put_array(pwrseq
->reset_gpios
);
87 if (!IS_ERR(pwrseq
->ext_clk
))
88 clk_put(pwrseq
->ext_clk
);
93 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops
= {
94 .pre_power_on
= mmc_pwrseq_simple_pre_power_on
,
95 .post_power_on
= mmc_pwrseq_simple_post_power_on
,
96 .power_off
= mmc_pwrseq_simple_power_off
,
97 .free
= mmc_pwrseq_simple_free
,
100 struct mmc_pwrseq
*mmc_pwrseq_simple_alloc(struct mmc_host
*host
,
103 struct mmc_pwrseq_simple
*pwrseq
;
106 pwrseq
= kzalloc(sizeof(*pwrseq
), GFP_KERNEL
);
108 return ERR_PTR(-ENOMEM
);
110 pwrseq
->ext_clk
= clk_get(dev
, "ext_clock");
111 if (IS_ERR(pwrseq
->ext_clk
) &&
112 PTR_ERR(pwrseq
->ext_clk
) != -ENOENT
) {
113 ret
= PTR_ERR(pwrseq
->ext_clk
);
117 pwrseq
->reset_gpios
= gpiod_get_array(dev
, "reset", GPIOD_OUT_HIGH
);
118 if (IS_ERR(pwrseq
->reset_gpios
) &&
119 PTR_ERR(pwrseq
->reset_gpios
) != -ENOENT
&&
120 PTR_ERR(pwrseq
->reset_gpios
) != -ENOSYS
) {
121 ret
= PTR_ERR(pwrseq
->reset_gpios
);
125 pwrseq
->pwrseq
.ops
= &mmc_pwrseq_simple_ops
;
127 return &pwrseq
->pwrseq
;
129 if (!IS_ERR(pwrseq
->ext_clk
))
130 clk_put(pwrseq
->ext_clk
);