1 /*
2 * Copyright (C) 2014 Linaro Ltd
3 *
4 * Author: Ulf Hansson <ulf.hansson@linaro.org>
5 *
6 * License terms: GNU General Public License (GPL) version 2
7 *
8 * Simple MMC power sequence management
9 */
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>
21
22 #include <linux/mmc/host.h>
23
24 #include "pwrseq.h"
25
26 struct mmc_pwrseq_simple {
27 struct mmc_pwrseq pwrseq;
28 bool clk_enabled;
29 u32 post_power_on_delay_ms;
30 u32 power_off_delay_us;
31 struct clk *ext_clk;
32 struct gpio_descs *reset_gpios;
33 };
34
35 #define to_pwrseq_simple(p) container_of(p, struct mmc_pwrseq_simple, pwrseq)
36
mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple * pwrseq,int value)37 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple *pwrseq,
38 int value)
39 {
40 struct gpio_descs *reset_gpios = pwrseq->reset_gpios;
41
42 if (!IS_ERR(reset_gpios)) {
43 int i, *values;
44 int nvalues = reset_gpios->ndescs;
45
46 values = kmalloc_array(nvalues, sizeof(int), GFP_KERNEL);
47 if (!values)
48 return;
49
50 for (i = 0; i < nvalues; i++)
51 values[i] = value;
52
53 gpiod_set_array_value_cansleep(nvalues, reset_gpios->desc, values);
54 kfree(values);
55 }
56 }
57
mmc_pwrseq_simple_pre_power_on(struct mmc_host * host)58 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host)
59 {
60 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
61
62 if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) {
63 clk_prepare_enable(pwrseq->ext_clk);
64 pwrseq->clk_enabled = true;
65 }
66
67 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
68 }
69
mmc_pwrseq_simple_post_power_on(struct mmc_host * host)70 static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host)
71 {
72 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
73
74 mmc_pwrseq_simple_set_gpios_value(pwrseq, 0);
75
76 if (pwrseq->post_power_on_delay_ms)
77 msleep(pwrseq->post_power_on_delay_ms);
78 }
79
mmc_pwrseq_simple_power_off(struct mmc_host * host)80 static void mmc_pwrseq_simple_power_off(struct mmc_host *host)
81 {
82 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
83
84 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
85
86 if (pwrseq->power_off_delay_us)
87 usleep_range(pwrseq->power_off_delay_us,
88 2 * pwrseq->power_off_delay_us);
89
90 if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) {
91 clk_disable_unprepare(pwrseq->ext_clk);
92 pwrseq->clk_enabled = false;
93 }
94 }
95
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,
100 };
101
102 static const struct of_device_id mmc_pwrseq_simple_of_match[] = {
103 { .compatible = "mmc-pwrseq-simple",},
104 {/* sentinel */},
105 };
106 MODULE_DEVICE_TABLE(of, mmc_pwrseq_simple_of_match);
107
mmc_pwrseq_simple_probe(struct platform_device * pdev)108 static int mmc_pwrseq_simple_probe(struct platform_device *pdev)
109 {
110 struct mmc_pwrseq_simple *pwrseq;
111 struct device *dev = &pdev->dev;
112
113 pwrseq = devm_kzalloc(dev, sizeof(*pwrseq), GFP_KERNEL);
114 if (!pwrseq)
115 return -ENOMEM;
116
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);
120
121 pwrseq->reset_gpios = devm_gpiod_get_array(dev, "reset",
122 GPIOD_OUT_HIGH);
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);
127 }
128
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);
133
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);
138
139 return mmc_pwrseq_register(&pwrseq->pwrseq);
140 }
141
mmc_pwrseq_simple_remove(struct platform_device * pdev)142 static int mmc_pwrseq_simple_remove(struct platform_device *pdev)
143 {
144 struct mmc_pwrseq_simple *pwrseq = platform_get_drvdata(pdev);
145
146 mmc_pwrseq_unregister(&pwrseq->pwrseq);
147
148 return 0;
149 }
150
151 static struct platform_driver mmc_pwrseq_simple_driver = {
152 .probe = mmc_pwrseq_simple_probe,
153 .remove = mmc_pwrseq_simple_remove,
154 .driver = {
155 .name = "pwrseq_simple",
156 .of_match_table = mmc_pwrseq_simple_of_match,
157 },
158 };
159
160 module_platform_driver(mmc_pwrseq_simple_driver);
161 MODULE_LICENSE("GPL v2");
162