Merge tag 'riscv-for-linus-4.15-rc2_cleanups' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / mmc / core / pwrseq_simple.c
blob13ef162cf066a63363106e40a513f3317205d10d
1 /*
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
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>
22 #include <linux/mmc/host.h>
24 #include "pwrseq.h"
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;
35 #define to_pwrseq_simple(p) container_of(p, struct mmc_pwrseq_simple, pwrseq)
37 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple *pwrseq,
38 int value)
40 struct gpio_descs *reset_gpios = pwrseq->reset_gpios;
42 if (!IS_ERR(reset_gpios)) {
43 int i;
44 int values[reset_gpios->ndescs];
46 for (i = 0; i < reset_gpios->ndescs; i++)
47 values[i] = value;
49 gpiod_set_array_value_cansleep(
50 reset_gpios->ndescs, reset_gpios->desc, values);
54 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host)
56 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
58 if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) {
59 clk_prepare_enable(pwrseq->ext_clk);
60 pwrseq->clk_enabled = true;
63 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
66 static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host)
68 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
70 mmc_pwrseq_simple_set_gpios_value(pwrseq, 0);
72 if (pwrseq->post_power_on_delay_ms)
73 msleep(pwrseq->post_power_on_delay_ms);
76 static void mmc_pwrseq_simple_power_off(struct mmc_host *host)
78 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
80 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
82 if (pwrseq->power_off_delay_us)
83 usleep_range(pwrseq->power_off_delay_us,
84 2 * pwrseq->power_off_delay_us);
86 if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) {
87 clk_disable_unprepare(pwrseq->ext_clk);
88 pwrseq->clk_enabled = false;
92 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {
93 .pre_power_on = mmc_pwrseq_simple_pre_power_on,
94 .post_power_on = mmc_pwrseq_simple_post_power_on,
95 .power_off = mmc_pwrseq_simple_power_off,
98 static const struct of_device_id mmc_pwrseq_simple_of_match[] = {
99 { .compatible = "mmc-pwrseq-simple",},
100 {/* sentinel */},
102 MODULE_DEVICE_TABLE(of, mmc_pwrseq_simple_of_match);
104 static int mmc_pwrseq_simple_probe(struct platform_device *pdev)
106 struct mmc_pwrseq_simple *pwrseq;
107 struct device *dev = &pdev->dev;
109 pwrseq = devm_kzalloc(dev, sizeof(*pwrseq), GFP_KERNEL);
110 if (!pwrseq)
111 return -ENOMEM;
113 pwrseq->ext_clk = devm_clk_get(dev, "ext_clock");
114 if (IS_ERR(pwrseq->ext_clk) && PTR_ERR(pwrseq->ext_clk) != -ENOENT)
115 return PTR_ERR(pwrseq->ext_clk);
117 pwrseq->reset_gpios = devm_gpiod_get_array(dev, "reset",
118 GPIOD_OUT_HIGH);
119 if (IS_ERR(pwrseq->reset_gpios) &&
120 PTR_ERR(pwrseq->reset_gpios) != -ENOENT &&
121 PTR_ERR(pwrseq->reset_gpios) != -ENOSYS) {
122 return PTR_ERR(pwrseq->reset_gpios);
125 device_property_read_u32(dev, "post-power-on-delay-ms",
126 &pwrseq->post_power_on_delay_ms);
127 device_property_read_u32(dev, "power-off-delay-us",
128 &pwrseq->power_off_delay_us);
130 pwrseq->pwrseq.dev = dev;
131 pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops;
132 pwrseq->pwrseq.owner = THIS_MODULE;
133 platform_set_drvdata(pdev, pwrseq);
135 return mmc_pwrseq_register(&pwrseq->pwrseq);
138 static int mmc_pwrseq_simple_remove(struct platform_device *pdev)
140 struct mmc_pwrseq_simple *pwrseq = platform_get_drvdata(pdev);
142 mmc_pwrseq_unregister(&pwrseq->pwrseq);
144 return 0;
147 static struct platform_driver mmc_pwrseq_simple_driver = {
148 .probe = mmc_pwrseq_simple_probe,
149 .remove = mmc_pwrseq_simple_remove,
150 .driver = {
151 .name = "pwrseq_simple",
152 .of_match_table = mmc_pwrseq_simple_of_match,
156 module_platform_driver(mmc_pwrseq_simple_driver);
157 MODULE_LICENSE("GPL v2");