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
;
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
)) {
43 int values
[reset_gpios
->ndescs
];
45 for (i
= 0; i
< reset_gpios
->ndescs
; i
++)
48 gpiod_set_array_value_cansleep(
49 reset_gpios
->ndescs
, reset_gpios
->desc
, values
);
53 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host
*host
)
55 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
57 if (!IS_ERR(pwrseq
->ext_clk
) && !pwrseq
->clk_enabled
) {
58 clk_prepare_enable(pwrseq
->ext_clk
);
59 pwrseq
->clk_enabled
= true;
62 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
65 static void mmc_pwrseq_simple_post_power_on(struct mmc_host
*host
)
67 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
69 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 0);
71 if (pwrseq
->post_power_on_delay_ms
)
72 msleep(pwrseq
->post_power_on_delay_ms
);
75 static void mmc_pwrseq_simple_power_off(struct mmc_host
*host
)
77 struct mmc_pwrseq_simple
*pwrseq
= to_pwrseq_simple(host
->pwrseq
);
79 mmc_pwrseq_simple_set_gpios_value(pwrseq
, 1);
81 if (!IS_ERR(pwrseq
->ext_clk
) && pwrseq
->clk_enabled
) {
82 clk_disable_unprepare(pwrseq
->ext_clk
);
83 pwrseq
->clk_enabled
= false;
87 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops
= {
88 .pre_power_on
= mmc_pwrseq_simple_pre_power_on
,
89 .post_power_on
= mmc_pwrseq_simple_post_power_on
,
90 .power_off
= mmc_pwrseq_simple_power_off
,
93 static const struct of_device_id mmc_pwrseq_simple_of_match
[] = {
94 { .compatible
= "mmc-pwrseq-simple",},
97 MODULE_DEVICE_TABLE(of
, mmc_pwrseq_simple_of_match
);
99 static int mmc_pwrseq_simple_probe(struct platform_device
*pdev
)
101 struct mmc_pwrseq_simple
*pwrseq
;
102 struct device
*dev
= &pdev
->dev
;
104 pwrseq
= devm_kzalloc(dev
, sizeof(*pwrseq
), GFP_KERNEL
);
108 pwrseq
->ext_clk
= devm_clk_get(dev
, "ext_clock");
109 if (IS_ERR(pwrseq
->ext_clk
) && PTR_ERR(pwrseq
->ext_clk
) != -ENOENT
)
110 return PTR_ERR(pwrseq
->ext_clk
);
112 pwrseq
->reset_gpios
= devm_gpiod_get_array(dev
, "reset",
114 if (IS_ERR(pwrseq
->reset_gpios
) &&
115 PTR_ERR(pwrseq
->reset_gpios
) != -ENOENT
&&
116 PTR_ERR(pwrseq
->reset_gpios
) != -ENOSYS
) {
117 return PTR_ERR(pwrseq
->reset_gpios
);
120 device_property_read_u32(dev
, "post-power-on-delay-ms",
121 &pwrseq
->post_power_on_delay_ms
);
123 pwrseq
->pwrseq
.dev
= dev
;
124 pwrseq
->pwrseq
.ops
= &mmc_pwrseq_simple_ops
;
125 pwrseq
->pwrseq
.owner
= THIS_MODULE
;
126 platform_set_drvdata(pdev
, pwrseq
);
128 return mmc_pwrseq_register(&pwrseq
->pwrseq
);
131 static int mmc_pwrseq_simple_remove(struct platform_device
*pdev
)
133 struct mmc_pwrseq_simple
*pwrseq
= platform_get_drvdata(pdev
);
135 mmc_pwrseq_unregister(&pwrseq
->pwrseq
);
140 static struct platform_driver mmc_pwrseq_simple_driver
= {
141 .probe
= mmc_pwrseq_simple_probe
,
142 .remove
= mmc_pwrseq_simple_remove
,
144 .name
= "pwrseq_simple",
145 .of_match_table
= mmc_pwrseq_simple_of_match
,
149 module_platform_driver(mmc_pwrseq_simple_driver
);
150 MODULE_LICENSE("GPL v2");