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
;
27 struct gpio_desc
*reset_gpios
[0];
30 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple
*pwrseq
,
35 for (i
= 0; i
< pwrseq
->nr_gpios
; i
++)
36 if (!IS_ERR(pwrseq
->reset_gpios
[i
]))
37 gpiod_set_value_cansleep(pwrseq
->reset_gpios
[i
], value
);
40 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host
*host
)
42 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
43 struct mmc_pwrseq_simple
, pwrseq
);
45 if (!IS_ERR(pwrseq
->ext_clk
) && !pwrseq
->clk_enabled
) {
46 clk_prepare_enable(pwrseq
->ext_clk
);
47 pwrseq
->clk_enabled
= true;
50 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
53 static void mmc_pwrseq_simple_post_power_on(struct mmc_host
*host
)
55 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
56 struct mmc_pwrseq_simple
, pwrseq
);
58 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 0);
61 static void mmc_pwrseq_simple_power_off(struct mmc_host
*host
)
63 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
64 struct mmc_pwrseq_simple
, pwrseq
);
66 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
68 if (!IS_ERR(pwrseq
->ext_clk
) && pwrseq
->clk_enabled
) {
69 clk_disable_unprepare(pwrseq
->ext_clk
);
70 pwrseq
->clk_enabled
= false;
74 static void mmc_pwrseq_simple_free(struct mmc_host
*host
)
76 struct mmc_pwrseq_simple
*pwrseq
= container_of(host
->pwrseq
,
77 struct mmc_pwrseq_simple
, pwrseq
);
80 for (i
= 0; i
< pwrseq
->nr_gpios
; i
++)
81 if (!IS_ERR(pwrseq
->reset_gpios
[i
]))
82 gpiod_put(pwrseq
->reset_gpios
[i
]);
84 if (!IS_ERR(pwrseq
->ext_clk
))
85 clk_put(pwrseq
->ext_clk
);
91 static 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 int mmc_pwrseq_simple_alloc(struct mmc_host
*host
, struct device
*dev
)
100 struct mmc_pwrseq_simple
*pwrseq
;
101 int i
, nr_gpios
, ret
= 0;
103 nr_gpios
= of_gpio_named_count(dev
->of_node
, "reset-gpios");
107 pwrseq
= kzalloc(sizeof(struct mmc_pwrseq_simple
) + nr_gpios
*
108 sizeof(struct gpio_desc
*), GFP_KERNEL
);
112 pwrseq
->ext_clk
= clk_get(dev
, "ext_clock");
113 if (IS_ERR(pwrseq
->ext_clk
) &&
114 PTR_ERR(pwrseq
->ext_clk
) != -ENOENT
) {
115 ret
= PTR_ERR(pwrseq
->ext_clk
);
119 for (i
= 0; i
< nr_gpios
; i
++) {
120 pwrseq
->reset_gpios
[i
] = gpiod_get_index(dev
, "reset", i
,
122 if (IS_ERR(pwrseq
->reset_gpios
[i
]) &&
123 PTR_ERR(pwrseq
->reset_gpios
[i
]) != -ENOENT
&&
124 PTR_ERR(pwrseq
->reset_gpios
[i
]) != -ENOSYS
) {
125 ret
= PTR_ERR(pwrseq
->reset_gpios
[i
]);
128 gpiod_put(pwrseq
->reset_gpios
[i
]);
134 pwrseq
->nr_gpios
= nr_gpios
;
135 pwrseq
->pwrseq
.ops
= &mmc_pwrseq_simple_ops
;
136 host
->pwrseq
= &pwrseq
->pwrseq
;
140 if (!IS_ERR(pwrseq
->ext_clk
))
141 clk_put(pwrseq
->ext_clk
);