2 * Platform driver for the Synopsys DesignWare DMA Controller
4 * Copyright (C) 2007-2008 Atmel Corporation
5 * Copyright (C) 2010-2011 ST Microelectronics
6 * Copyright (C) 2013 Intel Corporation
8 * Some parts of this driver are derived from the original dw_dmac.
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.
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/clk.h>
18 #include <linux/platform_device.h>
19 #include <linux/dmaengine.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/of_dma.h>
23 #include <linux/acpi.h>
24 #include <linux/acpi_dma.h>
28 struct dw_dma_of_filter_args {
35 static bool dw_dma_of_filter(struct dma_chan *chan, void *param)
37 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
38 struct dw_dma_of_filter_args *fargs = param;
40 /* Ensure the device matches our channel */
41 if (chan->device != &fargs->dw->dma)
44 dwc->src_id = fargs->req;
45 dwc->dst_id = fargs->req;
46 dwc->src_master = fargs->src;
47 dwc->dst_master = fargs->dst;
52 static struct dma_chan *dw_dma_of_xlate(struct of_phandle_args *dma_spec,
55 struct dw_dma *dw = ofdma->of_dma_data;
56 struct dw_dma_of_filter_args fargs = {
61 if (dma_spec->args_count != 3)
64 fargs.req = dma_spec->args[0];
65 fargs.src = dma_spec->args[1];
66 fargs.dst = dma_spec->args[2];
68 if (WARN_ON(fargs.req >= DW_DMA_MAX_NR_REQUESTS ||
69 fargs.src >= dw->nr_masters ||
70 fargs.dst >= dw->nr_masters))
74 dma_cap_set(DMA_SLAVE, cap);
76 /* TODO: there should be a simpler way to do this */
77 return dma_request_channel(cap, dw_dma_of_filter, &fargs);
81 static bool dw_dma_acpi_filter(struct dma_chan *chan, void *param)
83 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
84 struct acpi_dma_spec *dma_spec = param;
86 if (chan->device->dev != dma_spec->dev ||
87 chan->chan_id != dma_spec->chan_id)
90 dwc->src_id = dma_spec->slave_id;
91 dwc->dst_id = dma_spec->slave_id;
92 dwc->src_master = dwc_get_sms(NULL);
93 dwc->dst_master = dwc_get_dms(NULL);
98 static void dw_dma_acpi_controller_register(struct dw_dma *dw)
100 struct device *dev = dw->dma.dev;
101 struct acpi_dma_filter_info *info;
104 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
108 dma_cap_zero(info->dma_cap);
109 dma_cap_set(DMA_SLAVE, info->dma_cap);
110 info->filter_fn = dw_dma_acpi_filter;
112 ret = devm_acpi_dma_controller_register(dev, acpi_dma_simple_xlate,
115 dev_err(dev, "could not register acpi_dma_controller\n");
117 #else /* !CONFIG_ACPI */
118 static inline void dw_dma_acpi_controller_register(struct dw_dma *dw) {}
119 #endif /* !CONFIG_ACPI */
122 static struct dw_dma_platform_data *
123 dw_dma_parse_dt(struct platform_device *pdev)
125 struct device_node *np = pdev->dev.of_node;
126 struct dw_dma_platform_data *pdata;
130 dev_err(&pdev->dev, "Missing DT data\n");
134 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
138 if (of_property_read_u32(np, "dma-channels", &pdata->nr_channels))
141 if (of_property_read_bool(np, "is_private"))
142 pdata->is_private = true;
144 if (!of_property_read_u32(np, "chan_allocation_order", &tmp))
145 pdata->chan_allocation_order = (unsigned char)tmp;
147 if (!of_property_read_u32(np, "chan_priority", &tmp))
148 pdata->chan_priority = tmp;
150 if (!of_property_read_u32(np, "block_size", &tmp))
151 pdata->block_size = tmp;
153 if (!of_property_read_u32(np, "dma-masters", &tmp)) {
157 pdata->nr_masters = tmp;
160 if (!of_property_read_u32_array(np, "data_width", arr,
162 for (tmp = 0; tmp < pdata->nr_masters; tmp++)
163 pdata->data_width[tmp] = arr[tmp];
168 static inline struct dw_dma_platform_data *
169 dw_dma_parse_dt(struct platform_device *pdev)
175 static int dw_probe(struct platform_device *pdev)
177 struct dw_dma_chip *chip;
178 struct device *dev = &pdev->dev;
179 struct resource *mem;
180 struct dw_dma_platform_data *pdata;
183 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
187 chip->irq = platform_get_irq(pdev, 0);
191 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
192 chip->regs = devm_ioremap_resource(dev, mem);
193 if (IS_ERR(chip->regs))
194 return PTR_ERR(chip->regs);
196 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
200 pdata = dev_get_platdata(dev);
202 pdata = dw_dma_parse_dt(pdev);
206 err = dw_dma_probe(chip, pdata);
210 platform_set_drvdata(pdev, chip);
212 if (pdev->dev.of_node) {
213 err = of_dma_controller_register(pdev->dev.of_node,
214 dw_dma_of_xlate, chip->dw);
217 "could not register of_dma_controller\n");
220 if (ACPI_HANDLE(&pdev->dev))
221 dw_dma_acpi_controller_register(chip->dw);
226 static int dw_remove(struct platform_device *pdev)
228 struct dw_dma_chip *chip = platform_get_drvdata(pdev);
230 if (pdev->dev.of_node)
231 of_dma_controller_free(pdev->dev.of_node);
233 return dw_dma_remove(chip);
236 static void dw_shutdown(struct platform_device *pdev)
238 struct dw_dma_chip *chip = platform_get_drvdata(pdev);
240 dw_dma_shutdown(chip);
244 static const struct of_device_id dw_dma_of_id_table[] = {
245 { .compatible = "snps,dma-spear1340" },
248 MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
252 static const struct acpi_device_id dw_dma_acpi_id_table[] = {
256 MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table);
259 #ifdef CONFIG_PM_SLEEP
261 static int dw_suspend_late(struct device *dev)
263 struct platform_device *pdev = to_platform_device(dev);
264 struct dw_dma_chip *chip = platform_get_drvdata(pdev);
266 return dw_dma_suspend(chip);
269 static int dw_resume_early(struct device *dev)
271 struct platform_device *pdev = to_platform_device(dev);
272 struct dw_dma_chip *chip = platform_get_drvdata(pdev);
274 return dw_dma_resume(chip);
277 #endif /* CONFIG_PM_SLEEP */
279 static const struct dev_pm_ops dw_dev_pm_ops = {
280 SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early)
283 static struct platform_driver dw_driver = {
286 .shutdown = dw_shutdown,
289 .pm = &dw_dev_pm_ops,
290 .of_match_table = of_match_ptr(dw_dma_of_id_table),
291 .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
295 static int __init dw_init(void)
297 return platform_driver_register(&dw_driver);
299 subsys_initcall(dw_init);
301 static void __exit dw_exit(void)
303 platform_driver_unregister(&dw_driver);
305 module_exit(dw_exit);
307 MODULE_LICENSE("GPL v2");
308 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver");