1 From bdad8090b5efdb24fa9db00d8e2891927b68dcec Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Thu, 16 Jan 2014 14:32:35 +0100
4 Subject: [PATCH] ahci-platform: Add support for devices with more then 1 clock
6 The allwinner-sun4i AHCI controller needs 2 clocks to be enabled and the
7 imx AHCI controller needs 3 clocks to be enabled.
9 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 .../devicetree/bindings/ata/ahci-platform.txt | 1 +
12 drivers/ata/ahci.h | 3 +-
13 drivers/ata/ahci_platform.c | 119 ++++++++++++++++-----
14 include/linux/ahci_platform.h | 4 +
15 4 files changed, 99 insertions(+), 28 deletions(-)
17 diff --git a/Documentation/devicetree/bindings/ata/ahci-platform.txt b/Documentation/devicetree/bindings/ata/ahci-platform.txt
18 index 89de156..3ced07d 100644
19 --- a/Documentation/devicetree/bindings/ata/ahci-platform.txt
20 +++ b/Documentation/devicetree/bindings/ata/ahci-platform.txt
21 @@ -10,6 +10,7 @@ Required properties:
24 - dma-coherent : Present if dma operations are coherent
25 +- clocks : a list of phandle + clock specifier pairs
29 diff --git a/drivers/ata/ahci.h b/drivers/ata/ahci.h
30 index 64d1a99d..c12862b 100644
31 --- a/drivers/ata/ahci.h
32 +++ b/drivers/ata/ahci.h
38 AHCI_MAX_SG = 168, /* hardware max is 64K */
39 AHCI_DMA_BOUNDARY = 0xffffffff,
41 @@ -321,7 +322,7 @@ struct ahci_host_priv {
42 u32 em_loc; /* enclosure management location */
43 u32 em_buf_sz; /* EM buffer size in byte */
44 u32 em_msg_type; /* EM message type */
45 - struct clk *clk; /* Only for platforms supporting clk */
46 + struct clk *clks[AHCI_MAX_CLKS]; /* Optional */
47 void *plat_data; /* Other platform data */
49 * Optional ahci_start_engine override, if not set this gets set to the
50 diff --git a/drivers/ata/ahci_platform.c b/drivers/ata/ahci_platform.c
51 index 4b231ba..609975d 100644
52 --- a/drivers/ata/ahci_platform.c
53 +++ b/drivers/ata/ahci_platform.c
54 @@ -87,6 +87,66 @@ struct ata_port_operations ahci_platform_ops = {
55 AHCI_SHT("ahci_platform"),
59 + * ahci_platform_enable_clks - Enable platform clocks
60 + * @hpriv: host private area to store config values
62 + * This function enables all the clks found in hpriv->clks, starting
63 + * at index 0. If any clk fails to enable it disables all the clks
64 + * already enabled in reverse order, and then returns an error.
70 + * 0 on success otherwise a negative error code
72 +int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
76 + for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
77 + rc = clk_prepare_enable(hpriv->clks[c]);
79 + goto disable_unprepare_clk;
83 +disable_unprepare_clk:
85 + clk_disable_unprepare(hpriv->clks[c]);
88 +EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
91 + * ahci_platform_disable_clks - Disable platform clocks
92 + * @hpriv: host private area to store config values
94 + * This function disables all the clks found in hpriv->clks, in reverse
95 + * order of ahci_platform_enable_clks (starting at the end of the array).
100 +void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
104 + for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
105 + if (hpriv->clks[c])
106 + clk_disable_unprepare(hpriv->clks[c]);
108 +EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
110 +static void ahci_put_clks(struct ahci_host_priv *hpriv)
114 + for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
115 + clk_put(hpriv->clks[c]);
118 static int ahci_probe(struct platform_device *pdev)
120 struct device *dev = &pdev->dev;
121 @@ -97,6 +157,7 @@ static int ahci_probe(struct platform_device *pdev)
122 struct ahci_host_priv *hpriv;
123 struct ata_host *host;
124 struct resource *mem;
129 @@ -131,17 +192,31 @@ static int ahci_probe(struct platform_device *pdev)
133 - hpriv->clk = clk_get(dev, NULL);
134 - if (IS_ERR(hpriv->clk)) {
135 - dev_err(dev, "can't get clock\n");
137 - rc = clk_prepare_enable(hpriv->clk);
139 - dev_err(dev, "clock prepare enable failed");
141 + for (i = 0; i < AHCI_MAX_CLKS; i++) {
143 + * For now we must use clk_get(dev, NULL) for the first clock,
144 + * because some platforms (da850, spear13xx) are not yet
145 + * converted to use devicetree for clocks. For new platforms
146 + * this is equivalent to of_clk_get(dev->of_node, 0).
149 + clk = clk_get(dev, NULL);
151 + clk = of_clk_get(dev->of_node, i);
155 + if (rc == -EPROBE_DEFER)
159 + hpriv->clks[i] = clk;
162 + rc = ahci_enable_clks(dev, hpriv);
167 * Some platforms might need to prepare for mmio region access,
168 * which could be done in the following init call. So, the mmio
169 @@ -222,11 +297,9 @@ static int ahci_probe(struct platform_device *pdev)
170 if (pdata && pdata->exit)
172 disable_unprepare_clk:
173 - if (!IS_ERR(hpriv->clk))
174 - clk_disable_unprepare(hpriv->clk);
175 + ahci_disable_clks(hpriv);
177 - if (!IS_ERR(hpriv->clk))
178 - clk_put(hpriv->clk);
179 + ahci_put_clks(hpriv);
183 @@ -239,10 +312,8 @@ static void ahci_host_stop(struct ata_host *host)
184 if (pdata && pdata->exit)
187 - if (!IS_ERR(hpriv->clk)) {
188 - clk_disable_unprepare(hpriv->clk);
189 - clk_put(hpriv->clk);
191 + ahci_disable_clks(hpriv);
192 + ahci_put_clks(hpriv);
195 #ifdef CONFIG_PM_SLEEP
196 @@ -277,8 +348,7 @@ static int ahci_suspend(struct device *dev)
197 if (pdata && pdata->suspend)
198 return pdata->suspend(dev);
200 - if (!IS_ERR(hpriv->clk))
201 - clk_disable_unprepare(hpriv->clk);
202 + ahci_disable_clks(hpriv);
206 @@ -290,13 +360,9 @@ static int ahci_resume(struct device *dev)
207 struct ahci_host_priv *hpriv = host->private_data;
210 - if (!IS_ERR(hpriv->clk)) {
211 - rc = clk_prepare_enable(hpriv->clk);
213 - dev_err(dev, "clock prepare enable failed");
217 + rc = ahci_enable_clks(dev, hpriv);
221 if (pdata && pdata->resume) {
222 rc = pdata->resume(dev);
223 @@ -317,8 +383,7 @@ static int ahci_resume(struct device *dev)
226 disable_unprepare_clk:
227 - if (!IS_ERR(hpriv->clk))
228 - clk_disable_unprepare(hpriv->clk);
229 + ahci_disable_clks(hpriv);
233 diff --git a/include/linux/ahci_platform.h b/include/linux/ahci_platform.h
234 index 73a2500..769d065 100644
235 --- a/include/linux/ahci_platform.h
236 +++ b/include/linux/ahci_platform.h
240 struct ata_port_info;
241 +struct ahci_host_priv;
243 struct ahci_platform_data {
244 int (*init)(struct device *dev, void __iomem *addr);
245 @@ -30,4 +31,7 @@ struct ahci_platform_data {
246 unsigned int mask_port_map;
249 +int ahci_platform_enable_clks(struct ahci_host_priv *hpriv);
250 +void ahci_platform_disable_clks(struct ahci_host_priv *hpriv);
252 #endif /* _AHCI_PLATFORM_H */