GNU Linux-libre 4.9.326-gnu1
[releases.git] / drivers / dma / dw / platform.c
1 /*
2  * Platform driver for the Synopsys DesignWare DMA Controller
3  *
4  * Copyright (C) 2007-2008 Atmel Corporation
5  * Copyright (C) 2010-2011 ST Microelectronics
6  * Copyright (C) 2013 Intel Corporation
7  *
8  * Some parts of this driver are derived from the original dw_dmac.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/clk.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/platform_device.h>
20 #include <linux/dmaengine.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/of.h>
23 #include <linux/of_dma.h>
24 #include <linux/acpi.h>
25 #include <linux/acpi_dma.h>
26
27 #include "internal.h"
28
29 #define DRV_NAME        "dw_dmac"
30
31 static struct dma_chan *dw_dma_of_xlate(struct of_phandle_args *dma_spec,
32                                         struct of_dma *ofdma)
33 {
34         struct dw_dma *dw = ofdma->of_dma_data;
35         struct dw_dma_slave slave = {
36                 .dma_dev = dw->dma.dev,
37         };
38         dma_cap_mask_t cap;
39
40         if (dma_spec->args_count != 3)
41                 return NULL;
42
43         slave.src_id = dma_spec->args[0];
44         slave.dst_id = dma_spec->args[0];
45         slave.m_master = dma_spec->args[1];
46         slave.p_master = dma_spec->args[2];
47
48         if (WARN_ON(slave.src_id >= DW_DMA_MAX_NR_REQUESTS ||
49                     slave.dst_id >= DW_DMA_MAX_NR_REQUESTS ||
50                     slave.m_master >= dw->pdata->nr_masters ||
51                     slave.p_master >= dw->pdata->nr_masters))
52                 return NULL;
53
54         dma_cap_zero(cap);
55         dma_cap_set(DMA_SLAVE, cap);
56
57         /* TODO: there should be a simpler way to do this */
58         return dma_request_channel(cap, dw_dma_filter, &slave);
59 }
60
61 #ifdef CONFIG_ACPI
62 static bool dw_dma_acpi_filter(struct dma_chan *chan, void *param)
63 {
64         struct acpi_dma_spec *dma_spec = param;
65         struct dw_dma_slave slave = {
66                 .dma_dev = dma_spec->dev,
67                 .src_id = dma_spec->slave_id,
68                 .dst_id = dma_spec->slave_id,
69                 .m_master = 0,
70                 .p_master = 1,
71         };
72
73         return dw_dma_filter(chan, &slave);
74 }
75
76 static void dw_dma_acpi_controller_register(struct dw_dma *dw)
77 {
78         struct device *dev = dw->dma.dev;
79         struct acpi_dma_filter_info *info;
80         int ret;
81
82         info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
83         if (!info)
84                 return;
85
86         dma_cap_zero(info->dma_cap);
87         dma_cap_set(DMA_SLAVE, info->dma_cap);
88         info->filter_fn = dw_dma_acpi_filter;
89
90         ret = acpi_dma_controller_register(dev, acpi_dma_simple_xlate, info);
91         if (ret)
92                 dev_err(dev, "could not register acpi_dma_controller\n");
93 }
94
95 static void dw_dma_acpi_controller_free(struct dw_dma *dw)
96 {
97         struct device *dev = dw->dma.dev;
98
99         acpi_dma_controller_free(dev);
100 }
101 #else /* !CONFIG_ACPI */
102 static inline void dw_dma_acpi_controller_register(struct dw_dma *dw) {}
103 static inline void dw_dma_acpi_controller_free(struct dw_dma *dw) {}
104 #endif /* !CONFIG_ACPI */
105
106 #ifdef CONFIG_OF
107 static struct dw_dma_platform_data *
108 dw_dma_parse_dt(struct platform_device *pdev)
109 {
110         struct device_node *np = pdev->dev.of_node;
111         struct dw_dma_platform_data *pdata;
112         u32 tmp, arr[DW_DMA_MAX_NR_MASTERS];
113         u32 nr_masters;
114         u32 nr_channels;
115
116         if (!np) {
117                 dev_err(&pdev->dev, "Missing DT data\n");
118                 return NULL;
119         }
120
121         if (of_property_read_u32(np, "dma-masters", &nr_masters))
122                 return NULL;
123         if (nr_masters < 1 || nr_masters > DW_DMA_MAX_NR_MASTERS)
124                 return NULL;
125
126         if (of_property_read_u32(np, "dma-channels", &nr_channels))
127                 return NULL;
128
129         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
130         if (!pdata)
131                 return NULL;
132
133         pdata->nr_masters = nr_masters;
134         pdata->nr_channels = nr_channels;
135
136         if (of_property_read_bool(np, "is_private"))
137                 pdata->is_private = true;
138
139         if (!of_property_read_u32(np, "chan_allocation_order", &tmp))
140                 pdata->chan_allocation_order = (unsigned char)tmp;
141
142         if (!of_property_read_u32(np, "chan_priority", &tmp))
143                 pdata->chan_priority = tmp;
144
145         if (!of_property_read_u32(np, "block_size", &tmp))
146                 pdata->block_size = tmp;
147
148         if (!of_property_read_u32_array(np, "data-width", arr, nr_masters)) {
149                 for (tmp = 0; tmp < nr_masters; tmp++)
150                         pdata->data_width[tmp] = arr[tmp];
151         } else if (!of_property_read_u32_array(np, "data_width", arr, nr_masters)) {
152                 for (tmp = 0; tmp < nr_masters; tmp++)
153                         pdata->data_width[tmp] = BIT(arr[tmp] & 0x07);
154         }
155
156         return pdata;
157 }
158 #else
159 static inline struct dw_dma_platform_data *
160 dw_dma_parse_dt(struct platform_device *pdev)
161 {
162         return NULL;
163 }
164 #endif
165
166 static int dw_probe(struct platform_device *pdev)
167 {
168         struct dw_dma_chip *chip;
169         struct device *dev = &pdev->dev;
170         struct resource *mem;
171         const struct dw_dma_platform_data *pdata;
172         int err;
173
174         chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
175         if (!chip)
176                 return -ENOMEM;
177
178         chip->irq = platform_get_irq(pdev, 0);
179         if (chip->irq < 0)
180                 return chip->irq;
181
182         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
183         chip->regs = devm_ioremap_resource(dev, mem);
184         if (IS_ERR(chip->regs))
185                 return PTR_ERR(chip->regs);
186
187         err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
188         if (err)
189                 return err;
190
191         pdata = dev_get_platdata(dev);
192         if (!pdata)
193                 pdata = dw_dma_parse_dt(pdev);
194
195         chip->dev = dev;
196         chip->pdata = pdata;
197
198         chip->clk = devm_clk_get(chip->dev, "hclk");
199         if (IS_ERR(chip->clk))
200                 return PTR_ERR(chip->clk);
201         err = clk_prepare_enable(chip->clk);
202         if (err)
203                 return err;
204
205         pm_runtime_enable(&pdev->dev);
206
207         err = dw_dma_probe(chip);
208         if (err)
209                 goto err_dw_dma_probe;
210
211         platform_set_drvdata(pdev, chip);
212
213         if (pdev->dev.of_node) {
214                 err = of_dma_controller_register(pdev->dev.of_node,
215                                                  dw_dma_of_xlate, chip->dw);
216                 if (err)
217                         dev_err(&pdev->dev,
218                                 "could not register of_dma_controller\n");
219         }
220
221         if (ACPI_HANDLE(&pdev->dev))
222                 dw_dma_acpi_controller_register(chip->dw);
223
224         return 0;
225
226 err_dw_dma_probe:
227         pm_runtime_disable(&pdev->dev);
228         clk_disable_unprepare(chip->clk);
229         return err;
230 }
231
232 static int dw_remove(struct platform_device *pdev)
233 {
234         struct dw_dma_chip *chip = platform_get_drvdata(pdev);
235
236         if (ACPI_HANDLE(&pdev->dev))
237                 dw_dma_acpi_controller_free(chip->dw);
238
239         if (pdev->dev.of_node)
240                 of_dma_controller_free(pdev->dev.of_node);
241
242         dw_dma_remove(chip);
243         pm_runtime_disable(&pdev->dev);
244         clk_disable_unprepare(chip->clk);
245
246         return 0;
247 }
248
249 static void dw_shutdown(struct platform_device *pdev)
250 {
251         struct dw_dma_chip *chip = platform_get_drvdata(pdev);
252
253         /*
254          * We have to call dw_dma_disable() to stop any ongoing transfer. On
255          * some platforms we can't do that since DMA device is powered off.
256          * Moreover we have no possibility to check if the platform is affected
257          * or not. That's why we call pm_runtime_get_sync() / pm_runtime_put()
258          * unconditionally. On the other hand we can't use
259          * pm_runtime_suspended() because runtime PM framework is not fully
260          * used by the driver.
261          */
262         pm_runtime_get_sync(chip->dev);
263         dw_dma_disable(chip);
264         pm_runtime_put_sync_suspend(chip->dev);
265
266         clk_disable_unprepare(chip->clk);
267 }
268
269 #ifdef CONFIG_OF
270 static const struct of_device_id dw_dma_of_id_table[] = {
271         { .compatible = "snps,dma-spear1340" },
272         {}
273 };
274 MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
275 #endif
276
277 #ifdef CONFIG_ACPI
278 static const struct acpi_device_id dw_dma_acpi_id_table[] = {
279         { "INTL9C60", 0 },
280         { }
281 };
282 MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table);
283 #endif
284
285 #ifdef CONFIG_PM_SLEEP
286
287 static int dw_suspend_late(struct device *dev)
288 {
289         struct platform_device *pdev = to_platform_device(dev);
290         struct dw_dma_chip *chip = platform_get_drvdata(pdev);
291
292         dw_dma_disable(chip);
293         clk_disable_unprepare(chip->clk);
294
295         return 0;
296 }
297
298 static int dw_resume_early(struct device *dev)
299 {
300         struct platform_device *pdev = to_platform_device(dev);
301         struct dw_dma_chip *chip = platform_get_drvdata(pdev);
302
303         clk_prepare_enable(chip->clk);
304         return dw_dma_enable(chip);
305 }
306
307 #endif /* CONFIG_PM_SLEEP */
308
309 static const struct dev_pm_ops dw_dev_pm_ops = {
310         SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early)
311 };
312
313 static struct platform_driver dw_driver = {
314         .probe          = dw_probe,
315         .remove         = dw_remove,
316         .shutdown       = dw_shutdown,
317         .driver = {
318                 .name   = DRV_NAME,
319                 .pm     = &dw_dev_pm_ops,
320                 .of_match_table = of_match_ptr(dw_dma_of_id_table),
321                 .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
322         },
323 };
324
325 static int __init dw_init(void)
326 {
327         return platform_driver_register(&dw_driver);
328 }
329 subsys_initcall(dw_init);
330
331 static void __exit dw_exit(void)
332 {
333         platform_driver_unregister(&dw_driver);
334 }
335 module_exit(dw_exit);
336
337 MODULE_LICENSE("GPL v2");
338 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver");
339 MODULE_ALIAS("platform:" DRV_NAME);