1 // SPDX-License-Identifier: GPL-2.0-only
5 // Based on TI DMA Crossbar driver by:
6 // Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
10 #include <linux/init.h>
11 #include <linux/mfd/syscon.h>
13 #include <linux/of_dma.h>
14 #include <linux/of_platform.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/spinlock.h>
19 #define LPC32XX_SSP_CLK_CTRL 0x78
20 #define LPC32XX_I2S_CLK_CTRL 0x7c
22 struct lpc32xx_dmamux {
32 struct lpc32xx_dmamux_data {
33 struct dma_router dmarouter;
35 spinlock_t lock; /* protects busy status flag */
38 /* From LPC32x0 User manual "3.2.1 DMA request signals" */
39 static struct lpc32xx_dmamux lpc32xx_muxes[] = {
42 .name_sel0 = "spi2-rx-tx",
43 .name_sel1 = "ssp1-rx",
44 .muxreg = LPC32XX_SSP_CLK_CTRL,
49 .name_sel0 = "uart7-rx",
50 .name_sel1 = "i2s1-dma1",
51 .muxreg = LPC32XX_I2S_CLK_CTRL,
56 .name_sel0 = "spi1-rx-tx",
57 .name_sel1 = "ssp1-tx",
58 .muxreg = LPC32XX_SSP_CLK_CTRL,
64 .name_sel1 = "ssp0-rx",
65 .muxreg = LPC32XX_SSP_CLK_CTRL,
71 .name_sel1 = "ssp0-tx",
72 .muxreg = LPC32XX_SSP_CLK_CTRL,
77 static void lpc32xx_dmamux_release(struct device *dev, void *route_data)
79 struct lpc32xx_dmamux_data *dmamux = dev_get_drvdata(dev);
80 struct lpc32xx_dmamux *mux = route_data;
82 dev_dbg(dev, "releasing dma request signal %d routed to %s\n",
83 mux->signal, mux->muxval ? mux->name_sel1 : mux->name_sel1);
85 guard(spinlock)(&dmamux->lock);
90 static void *lpc32xx_dmamux_reserve(struct of_phandle_args *dma_spec,
93 struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
94 struct device *dev = &pdev->dev;
95 struct lpc32xx_dmamux_data *dmamux = platform_get_drvdata(pdev);
97 struct lpc32xx_dmamux *mux = NULL;
100 if (dma_spec->args_count != 3) {
101 dev_err(&pdev->dev, "invalid number of dma mux args\n");
102 return ERR_PTR(-EINVAL);
105 for (i = 0; i < ARRAY_SIZE(lpc32xx_muxes); i++) {
106 if (lpc32xx_muxes[i].signal == dma_spec->args[0]) {
107 mux = &lpc32xx_muxes[i];
112 dev_err(&pdev->dev, "invalid mux request number: %d\n",
114 return ERR_PTR(-EINVAL);
117 if (dma_spec->args[2] > 1) {
118 dev_err(&pdev->dev, "invalid dma mux value: %d\n",
120 return ERR_PTR(-EINVAL);
123 /* The of_node_put() will be done in the core for the node */
124 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
126 dev_err(&pdev->dev, "can't get dma master\n");
127 return ERR_PTR(-EINVAL);
130 spin_lock_irqsave(&dmamux->lock, flags);
132 spin_unlock_irqrestore(&dmamux->lock, flags);
133 dev_err(dev, "dma request signal %d busy, routed to %s\n",
134 mux->signal, mux->muxval ? mux->name_sel1 : mux->name_sel1);
135 of_node_put(dma_spec->np);
136 return ERR_PTR(-EBUSY);
140 mux->muxval = dma_spec->args[2] ? BIT(mux->bit) : 0;
142 regmap_update_bits(dmamux->reg, mux->muxreg, BIT(mux->bit), mux->muxval);
143 spin_unlock_irqrestore(&dmamux->lock, flags);
145 dma_spec->args[2] = 0;
146 dma_spec->args_count = 2;
148 dev_dbg(dev, "dma request signal %d routed to %s\n",
149 mux->signal, mux->muxval ? mux->name_sel1 : mux->name_sel1);
154 static int lpc32xx_dmamux_probe(struct platform_device *pdev)
156 struct device_node *np = pdev->dev.of_node;
157 struct lpc32xx_dmamux_data *dmamux;
159 dmamux = devm_kzalloc(&pdev->dev, sizeof(*dmamux), GFP_KERNEL);
163 dmamux->reg = syscon_node_to_regmap(np->parent);
164 if (IS_ERR(dmamux->reg)) {
165 dev_err(&pdev->dev, "syscon lookup failed\n");
166 return PTR_ERR(dmamux->reg);
169 spin_lock_init(&dmamux->lock);
170 platform_set_drvdata(pdev, dmamux);
171 dmamux->dmarouter.dev = &pdev->dev;
172 dmamux->dmarouter.route_free = lpc32xx_dmamux_release;
174 return of_dma_router_register(np, lpc32xx_dmamux_reserve,
178 static const struct of_device_id lpc32xx_dmamux_match[] = {
179 { .compatible = "nxp,lpc3220-dmamux" },
183 static struct platform_driver lpc32xx_dmamux_driver = {
184 .probe = lpc32xx_dmamux_probe,
186 .name = "lpc32xx-dmamux",
187 .of_match_table = lpc32xx_dmamux_match,
191 static int __init lpc32xx_dmamux_init(void)
193 return platform_driver_register(&lpc32xx_dmamux_driver);
195 arch_initcall(lpc32xx_dmamux_init);