Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * sdhci-pltfm.c Support for SDHCI platform devices |
| 3 | * Copyright (c) 2009 Intel Corporation |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
| 19 | /* Supports: |
| 20 | * SDHCI platform devices |
| 21 | * |
| 22 | * Inspired by sdhci-pci.c, by Pierre Ossman |
| 23 | */ |
| 24 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 25 | #include <linux/err.h> |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 26 | |
| 27 | #include "sdhci.h" |
Anton Vorontsov | 515033f | 2010-08-10 18:01:47 -0700 | [diff] [blame] | 28 | #include "sdhci-pltfm.h" |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 29 | |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 30 | static struct sdhci_ops sdhci_pltfm_ops = { |
| 31 | }; |
| 32 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 33 | struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev, |
| 34 | struct sdhci_pltfm_data *pdata) |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 35 | { |
| 36 | struct sdhci_host *host; |
Wolfram Sang | 4b711cb | 2010-10-15 12:20:59 +0200 | [diff] [blame] | 37 | struct sdhci_pltfm_host *pltfm_host; |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 38 | struct resource *iomem; |
| 39 | int ret; |
| 40 | |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 41 | iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 42 | if (!iomem) { |
| 43 | ret = -ENOMEM; |
| 44 | goto err; |
| 45 | } |
| 46 | |
Anton Vorontsov | e632c45 | 2010-05-26 14:41:56 -0700 | [diff] [blame] | 47 | if (resource_size(iomem) < 0x100) |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 48 | dev_err(&pdev->dev, "Invalid iomem size!\n"); |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 49 | |
Wolfram Sang | 4b711cb | 2010-10-15 12:20:59 +0200 | [diff] [blame] | 50 | /* Some PCI-based MFD need the parent here */ |
| 51 | if (pdev->dev.parent != &platform_bus) |
| 52 | host = sdhci_alloc_host(pdev->dev.parent, sizeof(*pltfm_host)); |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 53 | else |
Wolfram Sang | 4b711cb | 2010-10-15 12:20:59 +0200 | [diff] [blame] | 54 | host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host)); |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 55 | |
| 56 | if (IS_ERR(host)) { |
| 57 | ret = PTR_ERR(host); |
| 58 | goto err; |
| 59 | } |
| 60 | |
Wolfram Sang | 4b711cb | 2010-10-15 12:20:59 +0200 | [diff] [blame] | 61 | pltfm_host = sdhci_priv(host); |
| 62 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 63 | host->hw_name = dev_name(&pdev->dev); |
Anton Vorontsov | a7626b7 | 2010-05-26 14:41:55 -0700 | [diff] [blame] | 64 | if (pdata && pdata->ops) |
| 65 | host->ops = pdata->ops; |
| 66 | else |
| 67 | host->ops = &sdhci_pltfm_ops; |
| 68 | if (pdata) |
| 69 | host->quirks = pdata->quirks; |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 70 | host->irq = platform_get_irq(pdev, 0); |
| 71 | |
| 72 | if (!request_mem_region(iomem->start, resource_size(iomem), |
| 73 | mmc_hostname(host->mmc))) { |
| 74 | dev_err(&pdev->dev, "cannot request region\n"); |
| 75 | ret = -EBUSY; |
| 76 | goto err_request; |
| 77 | } |
| 78 | |
| 79 | host->ioaddr = ioremap(iomem->start, resource_size(iomem)); |
| 80 | if (!host->ioaddr) { |
| 81 | dev_err(&pdev->dev, "failed to remap registers\n"); |
| 82 | ret = -ENOMEM; |
| 83 | goto err_remap; |
| 84 | } |
| 85 | |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 86 | platform_set_drvdata(pdev, host); |
| 87 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 88 | return host; |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 89 | |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 90 | err_remap: |
| 91 | release_mem_region(iomem->start, resource_size(iomem)); |
| 92 | err_request: |
| 93 | sdhci_free_host(host); |
| 94 | err: |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 95 | dev_err(&pdev->dev, "%s failed %d\n", __func__, ret); |
| 96 | return ERR_PTR(ret); |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 99 | void sdhci_pltfm_free(struct platform_device *pdev) |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 100 | { |
| 101 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 102 | struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 103 | |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 104 | iounmap(host->ioaddr); |
| 105 | release_mem_region(iomem->start, resource_size(iomem)); |
| 106 | sdhci_free_host(host); |
| 107 | platform_set_drvdata(pdev, NULL); |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 108 | } |
| 109 | |
| 110 | int sdhci_pltfm_register(struct platform_device *pdev, |
| 111 | struct sdhci_pltfm_data *pdata) |
| 112 | { |
| 113 | struct sdhci_host *host; |
| 114 | int ret = 0; |
| 115 | |
| 116 | host = sdhci_pltfm_init(pdev, pdata); |
| 117 | if (IS_ERR(host)) |
| 118 | return PTR_ERR(host); |
| 119 | |
| 120 | ret = sdhci_add_host(host); |
| 121 | if (ret) |
| 122 | sdhci_pltfm_free(pdev); |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | int sdhci_pltfm_unregister(struct platform_device *pdev) |
| 128 | { |
| 129 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 130 | int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff); |
| 131 | |
| 132 | sdhci_remove_host(host, dead); |
| 133 | sdhci_pltfm_free(pdev); |
Richard Röjfors | a3456a2 | 2009-06-04 13:57:29 +0200 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Giuseppe Cavallaro | be8ae09 | 2010-09-28 10:41:27 +0200 | [diff] [blame] | 138 | #ifdef CONFIG_PM |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 139 | int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state) |
Giuseppe Cavallaro | be8ae09 | 2010-09-28 10:41:27 +0200 | [diff] [blame] | 140 | { |
| 141 | struct sdhci_host *host = platform_get_drvdata(dev); |
| 142 | |
| 143 | return sdhci_suspend_host(host, state); |
| 144 | } |
| 145 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame^] | 146 | int sdhci_pltfm_resume(struct platform_device *dev) |
Giuseppe Cavallaro | be8ae09 | 2010-09-28 10:41:27 +0200 | [diff] [blame] | 147 | { |
| 148 | struct sdhci_host *host = platform_get_drvdata(dev); |
| 149 | |
| 150 | return sdhci_resume_host(host); |
| 151 | } |
Giuseppe Cavallaro | be8ae09 | 2010-09-28 10:41:27 +0200 | [diff] [blame] | 152 | #endif /* CONFIG_PM */ |