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/init.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/delay.h>
20 #include <linux/property.h>
22 #include <linux/mmc/host.h>
26 struct mmc_pwrseq_simple
{
27 struct mmc_pwrseq pwrseq
;
29 u32 post_power_on_delay_ms
;
30 u32 power_off_delay_us
;
32 struct gpio_descs
*reset_gpios
;
35 #define to_pwrseq_simple(p) container_of(p, struct mmc_pwrseq_simple, pwrseq)
37 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple
*pwrseq
,
40 struct gpio_descs
*reset_gpios
= pwrseq
->reset_gpios
;
42 if (!IS_ERR(reset_gpios
)) {
44 int nvalues
= reset_gpios
->ndescs
;
46 values
= kmalloc_array(nvalues
, sizeof(int), GFP_KERNEL
);
50 for (i
= 0; i
< nvalues
; i
++)
53 gpiod_set_array_value_cansleep(nvalues
, reset_gpios
->desc
, values
);
58 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host
*host
)
60 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
62 if (!IS_ERR(pwrseq
->ext_clk
) && !pwrseq
->clk_enabled
) {
63 clk_prepare_enable(pwrseq
->ext_clk
);
64 pwrseq
->clk_enabled
= true;
67 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
70 static void mmc_pwrseq_simple_post_power_on(struct mmc_host
*host
)
72 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
74 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 0);
76 if (pwrseq
->post_power_on_delay_ms
)
77 msleep(pwrseq
->post_power_on_delay_ms
);
80 static void mmc_pwrseq_simple_power_off(struct mmc_host
*host
)
82 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
84 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
86 if (pwrseq
->power_off_delay_us
)
87 usleep_range(pwrseq
->power_off_delay_us
,
88 2 * pwrseq
->power_off_delay_us
);
90 if (!IS_ERR(pwrseq
->ext_clk
) && pwrseq
->clk_enabled
) {
91 clk_disable_unprepare(pwrseq
->ext_clk
);
92 pwrseq
->clk_enabled
= false;
96 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops
= {
97 .pre_power_on
= mmc_pwrseq_simple_pre_power_on
,
98 .post_power_on
= mmc_pwrseq_simple_post_power_on
,
99 .power_off
= mmc_pwrseq_simple_power_off
,
102 static const struct of_device_id mmc_pwrseq_simple_of_match
[] = {
103 { .compatible
= "mmc-pwrseq-simple",},
106 MODULE_DEVICE_TABLE(of
, mmc_pwrseq_simple_of_match
);
108 static int mmc_pwrseq_simple_probe(struct platform_device
*pdev
)
110 struct mmc_pwrseq_simple
*pwrseq
;
111 struct device
*dev
= &pdev
->dev
;
113 pwrseq
= devm_kzalloc(dev
, sizeof(*pwrseq
), GFP_KERNEL
);
117 pwrseq
->ext_clk
= devm_clk_get(dev
, "ext_clock");
118 if (IS_ERR(pwrseq
->ext_clk
) && PTR_ERR(pwrseq
->ext_clk
) != -ENOENT
)
119 return PTR_ERR(pwrseq
->ext_clk
);
121 pwrseq
->reset_gpios
= devm_gpiod_get_array(dev
, "reset",
123 if (IS_ERR(pwrseq
->reset_gpios
) &&
124 PTR_ERR(pwrseq
->reset_gpios
) != -ENOENT
&&
125 PTR_ERR(pwrseq
->reset_gpios
) != -ENOSYS
) {
126 return PTR_ERR(pwrseq
->reset_gpios
);
129 device_property_read_u32(dev
, "post-power-on-delay-ms",
130 &pwrseq
->post_power_on_delay_ms
);
131 device_property_read_u32(dev
, "power-off-delay-us",
132 &pwrseq
->power_off_delay_us
);
134 pwrseq
->pwrseq
.dev
= dev
;
135 pwrseq
->pwrseq
.ops
= &mmc_pwrseq_simple_ops
;
136 pwrseq
->pwrseq
.owner
= THIS_MODULE
;
137 platform_set_drvdata(pdev
, pwrseq
);
139 return mmc_pwrseq_register(&pwrseq
->pwrseq
);
142 static int mmc_pwrseq_simple_remove(struct platform_device
*pdev
)
144 struct mmc_pwrseq_simple
*pwrseq
= platform_get_drvdata(pdev
);
146 mmc_pwrseq_unregister(&pwrseq
->pwrseq
);
151 static struct platform_driver mmc_pwrseq_simple_driver
= {
152 .probe
= mmc_pwrseq_simple_probe
,
153 .remove
= mmc_pwrseq_simple_remove
,
155 .name
= "pwrseq_simple",
156 .of_match_table
= mmc_pwrseq_simple_of_match
,
160 module_platform_driver(mmc_pwrseq_simple_driver
);
161 MODULE_LICENSE("GPL v2");