Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / mmc / core / pwrseq_simple.c
blob37cd858df0f4d7123683e1fe23a4c3fcd7817d13
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2014 Linaro Ltd
5 * Author: Ulf Hansson <ulf.hansson@linaro.org>
7 * Simple MMC power sequence management
8 */
9 #include <linux/clk.h>
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>
20 #include <linux/of.h>
21 #include <linux/reset.h>
23 #include <linux/mmc/host.h>
25 #include "pwrseq.h"
27 struct mmc_pwrseq_simple {
28 struct mmc_pwrseq pwrseq;
29 bool clk_enabled;
30 u32 post_power_on_delay_ms;
31 u32 power_off_delay_us;
32 struct clk *ext_clk;
33 struct gpio_descs *reset_gpios;
34 struct reset_control *reset_ctrl;
37 #define to_pwrseq_simple(p) container_of(p, struct mmc_pwrseq_simple, pwrseq)
39 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple *pwrseq,
40 int value)
42 struct gpio_descs *reset_gpios = pwrseq->reset_gpios;
44 if (!IS_ERR(reset_gpios)) {
45 unsigned long *values;
46 int nvalues = reset_gpios->ndescs;
48 values = bitmap_alloc(nvalues, GFP_KERNEL);
49 if (!values)
50 return;
52 if (value)
53 bitmap_fill(values, nvalues);
54 else
55 bitmap_zero(values, nvalues);
57 gpiod_set_array_value_cansleep(nvalues, reset_gpios->desc,
58 reset_gpios->info, values);
60 bitmap_free(values);
64 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host)
66 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
68 if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) {
69 clk_prepare_enable(pwrseq->ext_clk);
70 pwrseq->clk_enabled = true;
73 if (pwrseq->reset_ctrl) {
74 reset_control_deassert(pwrseq->reset_ctrl);
75 reset_control_assert(pwrseq->reset_ctrl);
76 } else
77 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
80 static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host)
82 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
84 if (pwrseq->reset_ctrl)
85 reset_control_deassert(pwrseq->reset_ctrl);
86 else
87 mmc_pwrseq_simple_set_gpios_value(pwrseq, 0);
89 if (pwrseq->post_power_on_delay_ms)
90 msleep(pwrseq->post_power_on_delay_ms);
93 static void mmc_pwrseq_simple_power_off(struct mmc_host *host)
95 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
97 if (pwrseq->reset_ctrl)
98 reset_control_assert(pwrseq->reset_ctrl);
99 else
100 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
102 if (pwrseq->power_off_delay_us)
103 usleep_range(pwrseq->power_off_delay_us,
104 2 * pwrseq->power_off_delay_us);
106 if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) {
107 clk_disable_unprepare(pwrseq->ext_clk);
108 pwrseq->clk_enabled = false;
112 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {
113 .pre_power_on = mmc_pwrseq_simple_pre_power_on,
114 .post_power_on = mmc_pwrseq_simple_post_power_on,
115 .power_off = mmc_pwrseq_simple_power_off,
118 static const struct of_device_id mmc_pwrseq_simple_of_match[] = {
119 { .compatible = "mmc-pwrseq-simple",},
120 {/* sentinel */},
122 MODULE_DEVICE_TABLE(of, mmc_pwrseq_simple_of_match);
124 static int mmc_pwrseq_simple_probe(struct platform_device *pdev)
126 struct mmc_pwrseq_simple *pwrseq;
127 struct device *dev = &pdev->dev;
128 int ngpio;
130 pwrseq = devm_kzalloc(dev, sizeof(*pwrseq), GFP_KERNEL);
131 if (!pwrseq)
132 return -ENOMEM;
134 pwrseq->ext_clk = devm_clk_get(dev, "ext_clock");
135 if (IS_ERR(pwrseq->ext_clk) && PTR_ERR(pwrseq->ext_clk) != -ENOENT)
136 return dev_err_probe(dev, PTR_ERR(pwrseq->ext_clk), "external clock not ready\n");
138 ngpio = of_count_phandle_with_args(dev->of_node, "reset-gpios", "#gpio-cells");
139 if (ngpio == 1) {
140 pwrseq->reset_ctrl = devm_reset_control_get_optional_shared(dev, NULL);
141 if (IS_ERR(pwrseq->reset_ctrl))
142 return dev_err_probe(dev, PTR_ERR(pwrseq->reset_ctrl),
143 "reset control not ready\n");
147 * Fallback to GPIO based reset control in case of multiple reset lines
148 * are specified or the platform doesn't have support for RESET at all.
150 if (!pwrseq->reset_ctrl) {
151 pwrseq->reset_gpios = devm_gpiod_get_array(dev, "reset", GPIOD_OUT_HIGH);
152 if (IS_ERR(pwrseq->reset_gpios) &&
153 PTR_ERR(pwrseq->reset_gpios) != -ENOENT &&
154 PTR_ERR(pwrseq->reset_gpios) != -ENOSYS) {
155 return dev_err_probe(dev, PTR_ERR(pwrseq->reset_gpios),
156 "reset GPIOs not ready\n");
160 device_property_read_u32(dev, "post-power-on-delay-ms",
161 &pwrseq->post_power_on_delay_ms);
162 device_property_read_u32(dev, "power-off-delay-us",
163 &pwrseq->power_off_delay_us);
165 pwrseq->pwrseq.dev = dev;
166 pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops;
167 pwrseq->pwrseq.owner = THIS_MODULE;
168 platform_set_drvdata(pdev, pwrseq);
170 return mmc_pwrseq_register(&pwrseq->pwrseq);
173 static void mmc_pwrseq_simple_remove(struct platform_device *pdev)
175 struct mmc_pwrseq_simple *pwrseq = platform_get_drvdata(pdev);
177 mmc_pwrseq_unregister(&pwrseq->pwrseq);
180 static struct platform_driver mmc_pwrseq_simple_driver = {
181 .probe = mmc_pwrseq_simple_probe,
182 .remove = mmc_pwrseq_simple_remove,
183 .driver = {
184 .name = "pwrseq_simple",
185 .of_match_table = mmc_pwrseq_simple_of_match,
189 module_platform_driver(mmc_pwrseq_simple_driver);
190 MODULE_DESCRIPTION("Simple power sequence management for MMC");
191 MODULE_LICENSE("GPL v2");