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
,
33 struct gpio_descs
*reset_gpios
= pwrseq
->reset_gpios
;
34 int values
[reset_gpios
->ndescs
];
36 for (i
= 0; i
< reset_gpios
->ndescs
; i
++)
39 gpiod_set_array_value_cansleep(reset_gpios
->ndescs
, reset_gpios
->desc
,
43 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host
*host
)
45 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
46 struct mmc_pwrseq_simple
, pwrseq
);
48 if (!IS_ERR(pwrseq
->ext_clk
) && !pwrseq
->clk_enabled
) {
49 clk_prepare_enable(pwrseq
->ext_clk
);
50 pwrseq
->clk_enabled
= true;
53 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
56 static void mmc_pwrseq_simple_post_power_on(struct mmc_host
*host
)
58 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
59 struct mmc_pwrseq_simple
, pwrseq
);
61 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 0);
64 static void mmc_pwrseq_simple_power_off(struct mmc_host
*host
)
66 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
67 struct mmc_pwrseq_simple
, pwrseq
);
69 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
71 if (!IS_ERR(pwrseq
->ext_clk
) && pwrseq
->clk_enabled
) {
72 clk_disable_unprepare(pwrseq
->ext_clk
);
73 pwrseq
->clk_enabled
= false;
77 static void mmc_pwrseq_simple_free(struct mmc_host
*host
)
79 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
80 struct mmc_pwrseq_simple
, pwrseq
);
82 gpiod_put_array(pwrseq
->reset_gpios
);
84 if (!IS_ERR(pwrseq
->ext_clk
))
85 clk_put(pwrseq
->ext_clk
);
90 static struct mmc_pwrseq_ops mmc_pwrseq_simple_ops
= {
91 .pre_power_on
= mmc_pwrseq_simple_pre_power_on
,
92 .post_power_on
= mmc_pwrseq_simple_post_power_on
,
93 .power_off
= mmc_pwrseq_simple_power_off
,
94 .free
= mmc_pwrseq_simple_free
,
97 struct mmc_pwrseq
*mmc_pwrseq_simple_alloc(struct mmc_host
*host
,
100 struct mmc_pwrseq_simple
*pwrseq
;
103 pwrseq
= kzalloc(sizeof(*pwrseq
), GFP_KERNEL
);
105 return ERR_PTR(-ENOMEM
);
107 pwrseq
->ext_clk
= clk_get(dev
, "ext_clock");
108 if (IS_ERR(pwrseq
->ext_clk
) &&
109 PTR_ERR(pwrseq
->ext_clk
) != -ENOENT
) {
110 ret
= PTR_ERR(pwrseq
->ext_clk
);
114 pwrseq
->reset_gpios
= gpiod_get_array(dev
, "reset", GPIOD_OUT_HIGH
);
115 if (IS_ERR(pwrseq
->reset_gpios
)) {
116 ret
= PTR_ERR(pwrseq
->reset_gpios
);
120 pwrseq
->pwrseq
.ops
= &mmc_pwrseq_simple_ops
;
122 return &pwrseq
->pwrseq
;
124 if (!IS_ERR(pwrseq
->ext_clk
))
125 clk_put(pwrseq
->ext_clk
);