1 /* 2 * sdhci-dove.c Support for SDHCI on Marvell's Dove SoC 3 * 4 * Author: Saeed Bishara <saeed@marvell.com> 5 * Mike Rapoport <mike@compulab.co.il> 6 * Based on sdhci-cns3xxx.c 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 */ 21 22 #include <linux/clk.h> 23 #include <linux/err.h> 24 #include <linux/gpio.h> 25 #include <linux/io.h> 26 #include <linux/mmc/host.h> 27 #include <linux/module.h> 28 #include <linux/of.h> 29 #include <linux/of_gpio.h> 30 31 #include "sdhci-pltfm.h" 32 33 struct sdhci_dove_priv { 34 struct clk *clk; 35 int gpio_cd; 36 }; 37 38 static irqreturn_t sdhci_dove_carddetect_irq(int irq, void *data) 39 { 40 struct sdhci_host *host = data; 41 42 tasklet_schedule(&host->card_tasklet); 43 return IRQ_HANDLED; 44 } 45 46 static u16 sdhci_dove_readw(struct sdhci_host *host, int reg) 47 { 48 u16 ret; 49 50 switch (reg) { 51 case SDHCI_HOST_VERSION: 52 case SDHCI_SLOT_INT_STATUS: 53 /* those registers don't exist */ 54 return 0; 55 default: 56 ret = readw(host->ioaddr + reg); 57 } 58 return ret; 59 } 60 61 static u32 sdhci_dove_readl(struct sdhci_host *host, int reg) 62 { 63 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 64 struct sdhci_dove_priv *priv = pltfm_host->priv; 65 u32 ret; 66 67 ret = readl(host->ioaddr + reg); 68 69 switch (reg) { 70 case SDHCI_CAPABILITIES: 71 /* Mask the support for 3.0V */ 72 ret &= ~SDHCI_CAN_VDD_300; 73 break; 74 case SDHCI_PRESENT_STATE: 75 if (gpio_is_valid(priv->gpio_cd)) { 76 if (gpio_get_value(priv->gpio_cd) == 0) 77 ret |= SDHCI_CARD_PRESENT; 78 else 79 ret &= ~SDHCI_CARD_PRESENT; 80 } 81 break; 82 } 83 return ret; 84 } 85 86 static const struct sdhci_ops sdhci_dove_ops = { 87 .read_w = sdhci_dove_readw, 88 .read_l = sdhci_dove_readl, 89 .set_clock = sdhci_set_clock, 90 .set_bus_width = sdhci_set_bus_width, 91 .reset = sdhci_reset, 92 }; 93 94 static const struct sdhci_pltfm_data sdhci_dove_pdata = { 95 .ops = &sdhci_dove_ops, 96 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER | 97 SDHCI_QUIRK_NO_BUSY_IRQ | 98 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | 99 SDHCI_QUIRK_FORCE_DMA | 100 SDHCI_QUIRK_NO_HISPD_BIT, 101 }; 102 103 static int sdhci_dove_probe(struct platform_device *pdev) 104 { 105 struct sdhci_host *host; 106 struct sdhci_pltfm_host *pltfm_host; 107 struct sdhci_dove_priv *priv; 108 int ret; 109 110 priv = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_dove_priv), 111 GFP_KERNEL); 112 if (!priv) { 113 dev_err(&pdev->dev, "unable to allocate private data"); 114 return -ENOMEM; 115 } 116 117 priv->clk = devm_clk_get(&pdev->dev, NULL); 118 119 if (pdev->dev.of_node) { 120 priv->gpio_cd = of_get_named_gpio(pdev->dev.of_node, 121 "cd-gpios", 0); 122 } else { 123 priv->gpio_cd = -EINVAL; 124 } 125 126 if (gpio_is_valid(priv->gpio_cd)) { 127 ret = gpio_request(priv->gpio_cd, "sdhci-cd"); 128 if (ret) { 129 dev_err(&pdev->dev, "card detect gpio request failed: %d\n", 130 ret); 131 return ret; 132 } 133 gpio_direction_input(priv->gpio_cd); 134 } 135 136 host = sdhci_pltfm_init(pdev, &sdhci_dove_pdata, 0); 137 if (IS_ERR(host)) { 138 ret = PTR_ERR(host); 139 goto err_sdhci_pltfm_init; 140 } 141 142 pltfm_host = sdhci_priv(host); 143 pltfm_host->priv = priv; 144 145 if (!IS_ERR(priv->clk)) 146 clk_prepare_enable(priv->clk); 147 148 sdhci_get_of_property(pdev); 149 150 ret = sdhci_add_host(host); 151 if (ret) 152 goto err_sdhci_add; 153 154 /* 155 * We must request the IRQ after sdhci_add_host(), as the tasklet only 156 * gets setup in sdhci_add_host() and we oops. 157 */ 158 if (gpio_is_valid(priv->gpio_cd)) { 159 ret = request_irq(gpio_to_irq(priv->gpio_cd), 160 sdhci_dove_carddetect_irq, 161 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 162 mmc_hostname(host->mmc), host); 163 if (ret) { 164 dev_err(&pdev->dev, "card detect irq request failed: %d\n", 165 ret); 166 goto err_request_irq; 167 } 168 } 169 170 return 0; 171 172 err_request_irq: 173 sdhci_remove_host(host, 0); 174 err_sdhci_add: 175 if (!IS_ERR(priv->clk)) 176 clk_disable_unprepare(priv->clk); 177 sdhci_pltfm_free(pdev); 178 err_sdhci_pltfm_init: 179 if (gpio_is_valid(priv->gpio_cd)) 180 gpio_free(priv->gpio_cd); 181 return ret; 182 } 183 184 static int sdhci_dove_remove(struct platform_device *pdev) 185 { 186 struct sdhci_host *host = platform_get_drvdata(pdev); 187 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 188 struct sdhci_dove_priv *priv = pltfm_host->priv; 189 190 sdhci_pltfm_unregister(pdev); 191 192 if (gpio_is_valid(priv->gpio_cd)) { 193 free_irq(gpio_to_irq(priv->gpio_cd), host); 194 gpio_free(priv->gpio_cd); 195 } 196 197 if (!IS_ERR(priv->clk)) 198 clk_disable_unprepare(priv->clk); 199 200 return 0; 201 } 202 203 static const struct of_device_id sdhci_dove_of_match_table[] = { 204 { .compatible = "marvell,dove-sdhci", }, 205 {} 206 }; 207 MODULE_DEVICE_TABLE(of, sdhci_dove_of_match_table); 208 209 static struct platform_driver sdhci_dove_driver = { 210 .driver = { 211 .name = "sdhci-dove", 212 .owner = THIS_MODULE, 213 .pm = SDHCI_PLTFM_PMOPS, 214 .of_match_table = sdhci_dove_of_match_table, 215 }, 216 .probe = sdhci_dove_probe, 217 .remove = sdhci_dove_remove, 218 }; 219 220 module_platform_driver(sdhci_dove_driver); 221 222 MODULE_DESCRIPTION("SDHCI driver for Dove"); 223 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>, " 224 "Mike Rapoport <mike@compulab.co.il>"); 225 MODULE_LICENSE("GPL v2"); 226