1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Linaro Ltd
5 * Author: Ulf Hansson <ulf.hansson@linaro.org>
7 * Simple MMC power sequence management
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/delay.h>
19 #include <linux/property.h>
21 #include <linux/mmc/host.h>
25 struct mmc_pwrseq_simple
{
26 struct mmc_pwrseq pwrseq
;
28 u32 post_power_on_delay_ms
;
29 u32 power_off_delay_us
;
31 struct gpio_descs
*reset_gpios
;
34 #define to_pwrseq_simple(p) container_of(p, struct mmc_pwrseq_simple, pwrseq)
36 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple
*pwrseq
,
39 struct gpio_descs
*reset_gpios
= pwrseq
->reset_gpios
;
41 if (!IS_ERR(reset_gpios
)) {
42 unsigned long *values
;
43 int nvalues
= reset_gpios
->ndescs
;
45 values
= bitmap_alloc(nvalues
, GFP_KERNEL
);
50 bitmap_fill(values
, nvalues
);
52 bitmap_zero(values
, nvalues
);
54 gpiod_set_array_value_cansleep(nvalues
, reset_gpios
->desc
,
55 reset_gpios
->info
, values
);
61 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host
*host
)
63 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
65 if (!IS_ERR(pwrseq
->ext_clk
) && !pwrseq
->clk_enabled
) {
66 clk_prepare_enable(pwrseq
->ext_clk
);
67 pwrseq
->clk_enabled
= true;
70 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
73 static void mmc_pwrseq_simple_post_power_on(struct mmc_host
*host
)
75 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
77 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 0);
79 if (pwrseq
->post_power_on_delay_ms
)
80 msleep(pwrseq
->post_power_on_delay_ms
);
83 static void mmc_pwrseq_simple_power_off(struct mmc_host
*host
)
85 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
87 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
89 if (pwrseq
->power_off_delay_us
)
90 usleep_range(pwrseq
->power_off_delay_us
,
91 2 * pwrseq
->power_off_delay_us
);
93 if (!IS_ERR(pwrseq
->ext_clk
) && pwrseq
->clk_enabled
) {
94 clk_disable_unprepare(pwrseq
->ext_clk
);
95 pwrseq
->clk_enabled
= false;
99 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops
= {
100 .pre_power_on
= mmc_pwrseq_simple_pre_power_on
,
101 .post_power_on
= mmc_pwrseq_simple_post_power_on
,
102 .power_off
= mmc_pwrseq_simple_power_off
,
105 static const struct of_device_id mmc_pwrseq_simple_of_match
[] = {
106 { .compatible
= "mmc-pwrseq-simple",},
109 MODULE_DEVICE_TABLE(of
, mmc_pwrseq_simple_of_match
);
111 static int mmc_pwrseq_simple_probe(struct platform_device
*pdev
)
113 struct mmc_pwrseq_simple
*pwrseq
;
114 struct device
*dev
= &pdev
->dev
;
116 pwrseq
= devm_kzalloc(dev
, sizeof(*pwrseq
), GFP_KERNEL
);
120 pwrseq
->ext_clk
= devm_clk_get(dev
, "ext_clock");
121 if (IS_ERR(pwrseq
->ext_clk
) && PTR_ERR(pwrseq
->ext_clk
) != -ENOENT
)
122 return PTR_ERR(pwrseq
->ext_clk
);
124 pwrseq
->reset_gpios
= devm_gpiod_get_array(dev
, "reset",
126 if (IS_ERR(pwrseq
->reset_gpios
) &&
127 PTR_ERR(pwrseq
->reset_gpios
) != -ENOENT
&&
128 PTR_ERR(pwrseq
->reset_gpios
) != -ENOSYS
) {
129 return PTR_ERR(pwrseq
->reset_gpios
);
132 device_property_read_u32(dev
, "post-power-on-delay-ms",
133 &pwrseq
->post_power_on_delay_ms
);
134 device_property_read_u32(dev
, "power-off-delay-us",
135 &pwrseq
->power_off_delay_us
);
137 pwrseq
->pwrseq
.dev
= dev
;
138 pwrseq
->pwrseq
.ops
= &mmc_pwrseq_simple_ops
;
139 pwrseq
->pwrseq
.owner
= THIS_MODULE
;
140 platform_set_drvdata(pdev
, pwrseq
);
142 return mmc_pwrseq_register(&pwrseq
->pwrseq
);
145 static int mmc_pwrseq_simple_remove(struct platform_device
*pdev
)
147 struct mmc_pwrseq_simple
*pwrseq
= platform_get_drvdata(pdev
);
149 mmc_pwrseq_unregister(&pwrseq
->pwrseq
);
154 static struct platform_driver mmc_pwrseq_simple_driver
= {
155 .probe
= mmc_pwrseq_simple_probe
,
156 .remove
= mmc_pwrseq_simple_remove
,
158 .name
= "pwrseq_simple",
159 .of_match_table
= mmc_pwrseq_simple_of_match
,
163 module_platform_driver(mmc_pwrseq_simple_driver
);
164 MODULE_LICENSE("GPL v2");