xref: /openbmc/linux/drivers/mmc/core/pwrseq_simple.c (revision e3d786a3)
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 
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 		unsigned long *values;
44 		int nvalues = reset_gpios->ndescs;
45 
46 		values = bitmap_alloc(nvalues, GFP_KERNEL);
47 		if (!values)
48 			return;
49 
50 		if (value)
51 			bitmap_fill(values, nvalues);
52 		else
53 			bitmap_zero(values, nvalues);
54 
55 		gpiod_set_array_value_cansleep(nvalues, reset_gpios->desc,
56 					       reset_gpios->info, values);
57 
58 		kfree(values);
59 	}
60 }
61 
62 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host)
63 {
64 	struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
65 
66 	if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) {
67 		clk_prepare_enable(pwrseq->ext_clk);
68 		pwrseq->clk_enabled = true;
69 	}
70 
71 	mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
72 }
73 
74 static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host)
75 {
76 	struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
77 
78 	mmc_pwrseq_simple_set_gpios_value(pwrseq, 0);
79 
80 	if (pwrseq->post_power_on_delay_ms)
81 		msleep(pwrseq->post_power_on_delay_ms);
82 }
83 
84 static void mmc_pwrseq_simple_power_off(struct mmc_host *host)
85 {
86 	struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
87 
88 	mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
89 
90 	if (pwrseq->power_off_delay_us)
91 		usleep_range(pwrseq->power_off_delay_us,
92 			2 * pwrseq->power_off_delay_us);
93 
94 	if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) {
95 		clk_disable_unprepare(pwrseq->ext_clk);
96 		pwrseq->clk_enabled = false;
97 	}
98 }
99 
100 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {
101 	.pre_power_on = mmc_pwrseq_simple_pre_power_on,
102 	.post_power_on = mmc_pwrseq_simple_post_power_on,
103 	.power_off = mmc_pwrseq_simple_power_off,
104 };
105 
106 static const struct of_device_id mmc_pwrseq_simple_of_match[] = {
107 	{ .compatible = "mmc-pwrseq-simple",},
108 	{/* sentinel */},
109 };
110 MODULE_DEVICE_TABLE(of, mmc_pwrseq_simple_of_match);
111 
112 static int mmc_pwrseq_simple_probe(struct platform_device *pdev)
113 {
114 	struct mmc_pwrseq_simple *pwrseq;
115 	struct device *dev = &pdev->dev;
116 
117 	pwrseq = devm_kzalloc(dev, sizeof(*pwrseq), GFP_KERNEL);
118 	if (!pwrseq)
119 		return -ENOMEM;
120 
121 	pwrseq->ext_clk = devm_clk_get(dev, "ext_clock");
122 	if (IS_ERR(pwrseq->ext_clk) && PTR_ERR(pwrseq->ext_clk) != -ENOENT)
123 		return PTR_ERR(pwrseq->ext_clk);
124 
125 	pwrseq->reset_gpios = devm_gpiod_get_array(dev, "reset",
126 							GPIOD_OUT_HIGH);
127 	if (IS_ERR(pwrseq->reset_gpios) &&
128 	    PTR_ERR(pwrseq->reset_gpios) != -ENOENT &&
129 	    PTR_ERR(pwrseq->reset_gpios) != -ENOSYS) {
130 		return PTR_ERR(pwrseq->reset_gpios);
131 	}
132 
133 	device_property_read_u32(dev, "post-power-on-delay-ms",
134 				 &pwrseq->post_power_on_delay_ms);
135 	device_property_read_u32(dev, "power-off-delay-us",
136 				 &pwrseq->power_off_delay_us);
137 
138 	pwrseq->pwrseq.dev = dev;
139 	pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops;
140 	pwrseq->pwrseq.owner = THIS_MODULE;
141 	platform_set_drvdata(pdev, pwrseq);
142 
143 	return mmc_pwrseq_register(&pwrseq->pwrseq);
144 }
145 
146 static int mmc_pwrseq_simple_remove(struct platform_device *pdev)
147 {
148 	struct mmc_pwrseq_simple *pwrseq = platform_get_drvdata(pdev);
149 
150 	mmc_pwrseq_unregister(&pwrseq->pwrseq);
151 
152 	return 0;
153 }
154 
155 static struct platform_driver mmc_pwrseq_simple_driver = {
156 	.probe = mmc_pwrseq_simple_probe,
157 	.remove = mmc_pwrseq_simple_remove,
158 	.driver = {
159 		.name = "pwrseq_simple",
160 		.of_match_table = mmc_pwrseq_simple_of_match,
161 	},
162 };
163 
164 module_platform_driver(mmc_pwrseq_simple_driver);
165 MODULE_LICENSE("GPL v2");
166