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/kernel.h> 12 #include <linux/slab.h> 13 #include <linux/device.h> 14 #include <linux/err.h> 15 #include <linux/of_gpio.h> 16 #include <linux/gpio/consumer.h> 17 18 #include <linux/mmc/host.h> 19 20 #include "pwrseq.h" 21 22 struct mmc_pwrseq_simple { 23 struct mmc_pwrseq pwrseq; 24 bool clk_enabled; 25 struct clk *ext_clk; 26 struct gpio_descs *reset_gpios; 27 }; 28 29 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple *pwrseq, 30 int value) 31 { 32 struct gpio_descs *reset_gpios = pwrseq->reset_gpios; 33 34 if (!IS_ERR(reset_gpios)) { 35 int i; 36 int values[reset_gpios->ndescs]; 37 38 for (i = 0; i < reset_gpios->ndescs; i++) 39 values[i] = value; 40 41 gpiod_set_array_value_cansleep( 42 reset_gpios->ndescs, reset_gpios->desc, values); 43 } 44 } 45 46 static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host) 47 { 48 struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq, 49 struct mmc_pwrseq_simple, pwrseq); 50 51 if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) { 52 clk_prepare_enable(pwrseq->ext_clk); 53 pwrseq->clk_enabled = true; 54 } 55 56 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1); 57 } 58 59 static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host) 60 { 61 struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq, 62 struct mmc_pwrseq_simple, pwrseq); 63 64 mmc_pwrseq_simple_set_gpios_value(pwrseq, 0); 65 } 66 67 static void mmc_pwrseq_simple_power_off(struct mmc_host *host) 68 { 69 struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq, 70 struct mmc_pwrseq_simple, pwrseq); 71 72 mmc_pwrseq_simple_set_gpios_value(pwrseq, 1); 73 74 if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) { 75 clk_disable_unprepare(pwrseq->ext_clk); 76 pwrseq->clk_enabled = false; 77 } 78 } 79 80 static void mmc_pwrseq_simple_free(struct mmc_host *host) 81 { 82 struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq, 83 struct mmc_pwrseq_simple, pwrseq); 84 85 if (!IS_ERR(pwrseq->reset_gpios)) 86 gpiod_put_array(pwrseq->reset_gpios); 87 88 if (!IS_ERR(pwrseq->ext_clk)) 89 clk_put(pwrseq->ext_clk); 90 91 kfree(pwrseq); 92 } 93 94 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = { 95 .pre_power_on = mmc_pwrseq_simple_pre_power_on, 96 .post_power_on = mmc_pwrseq_simple_post_power_on, 97 .power_off = mmc_pwrseq_simple_power_off, 98 .free = mmc_pwrseq_simple_free, 99 }; 100 101 struct mmc_pwrseq *mmc_pwrseq_simple_alloc(struct mmc_host *host, 102 struct device *dev) 103 { 104 struct mmc_pwrseq_simple *pwrseq; 105 int ret = 0; 106 107 pwrseq = kzalloc(sizeof(*pwrseq), GFP_KERNEL); 108 if (!pwrseq) 109 return ERR_PTR(-ENOMEM); 110 111 pwrseq->ext_clk = clk_get(dev, "ext_clock"); 112 if (IS_ERR(pwrseq->ext_clk) && 113 PTR_ERR(pwrseq->ext_clk) != -ENOENT) { 114 ret = PTR_ERR(pwrseq->ext_clk); 115 goto free; 116 } 117 118 pwrseq->reset_gpios = gpiod_get_array(dev, "reset", 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 ret = PTR_ERR(pwrseq->reset_gpios); 123 goto clk_put; 124 } 125 126 pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops; 127 128 return &pwrseq->pwrseq; 129 clk_put: 130 if (!IS_ERR(pwrseq->ext_clk)) 131 clk_put(pwrseq->ext_clk); 132 free: 133 kfree(pwrseq); 134 return ERR_PTR(ret); 135 } 136