1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2014 Linaro Ltd 4 * 5 * Author: Ulf Hansson <ulf.hansson@linaro.org> 6 * 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> 22 23 #include <linux/mmc/host.h> 24 25 #include "pwrseq.h" 26 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; 35 }; 36 37 #define to_pwrseq_simple(p) container_of(p, struct mmc_pwrseq_simple, pwrseq) 38 39 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple *pwrseq, 40 int value) 41 { 42 struct gpio_descs *reset_gpios = pwrseq->reset_gpios; 43 44 if (!IS_ERR(reset_gpios)) { 45 unsigned long *values; 46 int nvalues = reset_gpios->ndescs; 47 48 values = bitmap_alloc(nvalues, GFP_KERNEL); 49 if (!values) 50 return; 51 52 if (value) 53 bitmap_fill(values, nvalues); 54 else 55 bitmap_zero(values, nvalues); 56 57 gpiod_set_array_value_cansleep(nvalues, reset_gpios->desc, 58 reset_gpios->info, values); 59 60 bitmap_free(values); 61 } 62 } 63 64 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host) 65 { 66 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq); 67 68 if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) { 69 clk_prepare_enable(pwrseq->ext_clk); 70 pwrseq->clk_enabled = true; 71 } 72 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); 78 } 79 80 static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host) 81 { 82 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq); 83 84 if (pwrseq->reset_ctrl) 85 reset_control_deassert(pwrseq->reset_ctrl); 86 else 87 mmc_pwrseq_simple_set_gpios_value(pwrseq, 0); 88 89 if (pwrseq->post_power_on_delay_ms) 90 msleep(pwrseq->post_power_on_delay_ms); 91 } 92 93 static void mmc_pwrseq_simple_power_off(struct mmc_host *host) 94 { 95 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq); 96 97 if (pwrseq->reset_ctrl) 98 reset_control_assert(pwrseq->reset_ctrl); 99 else 100 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1); 101 102 if (pwrseq->power_off_delay_us) 103 usleep_range(pwrseq->power_off_delay_us, 104 2 * pwrseq->power_off_delay_us); 105 106 if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) { 107 clk_disable_unprepare(pwrseq->ext_clk); 108 pwrseq->clk_enabled = false; 109 } 110 } 111 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, 116 }; 117 118 static const struct of_device_id mmc_pwrseq_simple_of_match[] = { 119 { .compatible = "mmc-pwrseq-simple",}, 120 {/* sentinel */}, 121 }; 122 MODULE_DEVICE_TABLE(of, mmc_pwrseq_simple_of_match); 123 124 static int mmc_pwrseq_simple_probe(struct platform_device *pdev) 125 { 126 struct mmc_pwrseq_simple *pwrseq; 127 struct device *dev = &pdev->dev; 128 int ngpio; 129 130 pwrseq = devm_kzalloc(dev, sizeof(*pwrseq), GFP_KERNEL); 131 if (!pwrseq) 132 return -ENOMEM; 133 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"); 137 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"); 144 } 145 146 /* 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. 149 */ 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"); 157 } 158 } 159 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); 164 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); 169 170 return mmc_pwrseq_register(&pwrseq->pwrseq); 171 } 172 173 static void mmc_pwrseq_simple_remove(struct platform_device *pdev) 174 { 175 struct mmc_pwrseq_simple *pwrseq = platform_get_drvdata(pdev); 176 177 mmc_pwrseq_unregister(&pwrseq->pwrseq); 178 } 179 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, 186 }, 187 }; 188 189 module_platform_driver(mmc_pwrseq_simple_driver); 190 MODULE_DESCRIPTION("Simple power sequence management for MMC"); 191 MODULE_LICENSE("GPL v2"); 192