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 int i; 44 int values[reset_gpios->ndescs]; 45 46 for (i = 0; i < reset_gpios->ndescs; i++) 47 values[i] = value; 48 49 gpiod_set_array_value_cansleep( 50 reset_gpios->ndescs, reset_gpios->desc, values); 51 } 52 } 53 54 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host) 55 { 56 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq); 57 58 if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) { 59 clk_prepare_enable(pwrseq->ext_clk); 60 pwrseq->clk_enabled = true; 61 } 62 63 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1); 64 } 65 66 static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host) 67 { 68 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq); 69 70 mmc_pwrseq_simple_set_gpios_value(pwrseq, 0); 71 72 if (pwrseq->post_power_on_delay_ms) 73 msleep(pwrseq->post_power_on_delay_ms); 74 } 75 76 static void mmc_pwrseq_simple_power_off(struct mmc_host *host) 77 { 78 struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq); 79 80 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1); 81 82 if (pwrseq->power_off_delay_us) 83 usleep_range(pwrseq->power_off_delay_us, 84 2 * pwrseq->power_off_delay_us); 85 86 if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) { 87 clk_disable_unprepare(pwrseq->ext_clk); 88 pwrseq->clk_enabled = false; 89 } 90 } 91 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, 96 }; 97 98 static const struct of_device_id mmc_pwrseq_simple_of_match[] = { 99 { .compatible = "mmc-pwrseq-simple",}, 100 {/* sentinel */}, 101 }; 102 MODULE_DEVICE_TABLE(of, mmc_pwrseq_simple_of_match); 103 104 static int mmc_pwrseq_simple_probe(struct platform_device *pdev) 105 { 106 struct mmc_pwrseq_simple *pwrseq; 107 struct device *dev = &pdev->dev; 108 109 pwrseq = devm_kzalloc(dev, sizeof(*pwrseq), GFP_KERNEL); 110 if (!pwrseq) 111 return -ENOMEM; 112 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); 116 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); 123 } 124 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); 129 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); 134 135 return mmc_pwrseq_register(&pwrseq->pwrseq); 136 } 137 138 static int mmc_pwrseq_simple_remove(struct platform_device *pdev) 139 { 140 struct mmc_pwrseq_simple *pwrseq = platform_get_drvdata(pdev); 141 142 mmc_pwrseq_unregister(&pwrseq->pwrseq); 143 144 return 0; 145 } 146 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, 153 }, 154 }; 155 156 module_platform_driver(mmc_pwrseq_simple_driver); 157 MODULE_LICENSE("GPL v2"); 158