Commit | Line | Data |
---|---|---|
a3456a2d RR |
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 | ||
85d6509d | 25 | #include <linux/err.h> |
a3456a2d RR |
26 | |
27 | #include "sdhci.h" | |
515033f9 | 28 | #include "sdhci-pltfm.h" |
a3456a2d | 29 | |
a3456a2d RR |
30 | static struct sdhci_ops sdhci_pltfm_ops = { |
31 | }; | |
32 | ||
85d6509d SG |
33 | struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev, |
34 | struct sdhci_pltfm_data *pdata) | |
a3456a2d RR |
35 | { |
36 | struct sdhci_host *host; | |
4b711cb1 | 37 | struct sdhci_pltfm_host *pltfm_host; |
a3456a2d RR |
38 | struct resource *iomem; |
39 | int ret; | |
40 | ||
a3456a2d RR |
41 | iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
42 | if (!iomem) { | |
43 | ret = -ENOMEM; | |
44 | goto err; | |
45 | } | |
46 | ||
e632c45a | 47 | if (resource_size(iomem) < 0x100) |
85d6509d | 48 | dev_err(&pdev->dev, "Invalid iomem size!\n"); |
a3456a2d | 49 | |
4b711cb1 WS |
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)); | |
a3456a2d | 53 | else |
4b711cb1 | 54 | host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host)); |
a3456a2d RR |
55 | |
56 | if (IS_ERR(host)) { | |
57 | ret = PTR_ERR(host); | |
58 | goto err; | |
59 | } | |
60 | ||
4b711cb1 WS |
61 | pltfm_host = sdhci_priv(host); |
62 | ||
85d6509d | 63 | host->hw_name = dev_name(&pdev->dev); |
a7626b7a AV |
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; | |
a3456a2d RR |
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 | ||
a3456a2d RR |
86 | platform_set_drvdata(pdev, host); |
87 | ||
85d6509d | 88 | return host; |
a3456a2d | 89 | |
a3456a2d RR |
90 | err_remap: |
91 | release_mem_region(iomem->start, resource_size(iomem)); | |
92 | err_request: | |
93 | sdhci_free_host(host); | |
94 | err: | |
85d6509d SG |
95 | dev_err(&pdev->dev, "%s failed %d\n", __func__, ret); |
96 | return ERR_PTR(ret); | |
a3456a2d RR |
97 | } |
98 | ||
85d6509d | 99 | void sdhci_pltfm_free(struct platform_device *pdev) |
a3456a2d RR |
100 | { |
101 | struct sdhci_host *host = platform_get_drvdata(pdev); | |
102 | struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
a3456a2d | 103 | |
a3456a2d RR |
104 | iounmap(host->ioaddr); |
105 | release_mem_region(iomem->start, resource_size(iomem)); | |
106 | sdhci_free_host(host); | |
107 | platform_set_drvdata(pdev, NULL); | |
85d6509d | 108 | } |
a3456a2d | 109 | |
85d6509d SG |
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; | |
a3456a2d RR |
125 | } |
126 | ||
85d6509d SG |
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); | |
134 | ||
135 | return 0; | |
136 | } | |
515033f9 | 137 | |
be8ae09d | 138 | #ifdef CONFIG_PM |
85d6509d | 139 | int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state) |
be8ae09d GC |
140 | { |
141 | struct sdhci_host *host = platform_get_drvdata(dev); | |
142 | ||
143 | return sdhci_suspend_host(host, state); | |
144 | } | |
145 | ||
85d6509d | 146 | int sdhci_pltfm_resume(struct platform_device *dev) |
be8ae09d GC |
147 | { |
148 | struct sdhci_host *host = platform_get_drvdata(dev); | |
149 | ||
150 | return sdhci_resume_host(host); | |
151 | } | |
be8ae09d | 152 | #endif /* CONFIG_PM */ |