Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright 2024 Timesys Corporation <piotr.wojtaszczyk@timesys.com>
4//
5// Based on TI DMA Crossbar driver by:
6// Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
7// Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
8
9#include <linux/err.h>
10#include <linux/init.h>
11#include <linux/mfd/syscon.h>
12#include <linux/of.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>
18
19#define LPC32XX_SSP_CLK_CTRL 0x78
20#define LPC32XX_I2S_CLK_CTRL 0x7c
21
22struct lpc32xx_dmamux {
23 int signal;
24 char *name_sel0;
25 char *name_sel1;
26 int muxval;
27 int muxreg;
28 int bit;
29 bool busy;
30};
31
32struct lpc32xx_dmamux_data {
33 struct dma_router dmarouter;
34 struct regmap *reg;
35 spinlock_t lock; /* protects busy status flag */
36};
37
38/* From LPC32x0 User manual "3.2.1 DMA request signals" */
39static struct lpc32xx_dmamux lpc32xx_muxes[] = {
40 {
41 .signal = 3,
42 .name_sel0 = "spi2-rx-tx",
43 .name_sel1 = "ssp1-rx",
44 .muxreg = LPC32XX_SSP_CLK_CTRL,
45 .bit = 5,
46 },
47 {
48 .signal = 10,
49 .name_sel0 = "uart7-rx",
50 .name_sel1 = "i2s1-dma1",
51 .muxreg = LPC32XX_I2S_CLK_CTRL,
52 .bit = 4,
53 },
54 {
55 .signal = 11,
56 .name_sel0 = "spi1-rx-tx",
57 .name_sel1 = "ssp1-tx",
58 .muxreg = LPC32XX_SSP_CLK_CTRL,
59 .bit = 4,
60 },
61 {
62 .signal = 14,
63 .name_sel0 = "none",
64 .name_sel1 = "ssp0-rx",
65 .muxreg = LPC32XX_SSP_CLK_CTRL,
66 .bit = 3,
67 },
68 {
69 .signal = 15,
70 .name_sel0 = "none",
71 .name_sel1 = "ssp0-tx",
72 .muxreg = LPC32XX_SSP_CLK_CTRL,
73 .bit = 2,
74 },
75};
76
77static void lpc32xx_dmamux_release(struct device *dev, void *route_data)
78{
79 struct lpc32xx_dmamux_data *dmamux = dev_get_drvdata(dev);
80 struct lpc32xx_dmamux *mux = route_data;
81
82 dev_dbg(dev, "releasing dma request signal %d routed to %s\n",
83 mux->signal, mux->muxval ? mux->name_sel1 : mux->name_sel1);
84
85 guard(spinlock)(&dmamux->lock);
86
87 mux->busy = false;
88}
89
90static void *lpc32xx_dmamux_reserve(struct of_phandle_args *dma_spec,
91 struct of_dma *ofdma)
92{
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);
96 unsigned long flags;
97 struct lpc32xx_dmamux *mux = NULL;
98 int ret = -EINVAL;
99 int i;
100
101 if (dma_spec->args_count != 3) {
102 dev_err(&pdev->dev, "invalid number of dma mux args\n");
103 goto err_put_pdev;
104 }
105
106 for (i = 0; i < ARRAY_SIZE(lpc32xx_muxes); i++) {
107 if (lpc32xx_muxes[i].signal == dma_spec->args[0]) {
108 mux = &lpc32xx_muxes[i];
109 break;
110 }
111 }
112 if (!mux) {
113 dev_err(&pdev->dev, "invalid mux request number: %d\n",
114 dma_spec->args[0]);
115 goto err_put_pdev;
116 }
117
118 if (dma_spec->args[2] > 1) {
119 dev_err(&pdev->dev, "invalid dma mux value: %d\n",
120 dma_spec->args[1]);
121 goto err_put_pdev;
122 }
123
124 /* The of_node_put() will be done in the core for the node */
125 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
126 if (!dma_spec->np) {
127 dev_err(&pdev->dev, "can't get dma master\n");
128 goto err_put_pdev;
129 }
130
131 spin_lock_irqsave(&dmamux->lock, flags);
132 if (mux->busy) {
133 spin_unlock_irqrestore(&dmamux->lock, flags);
134 dev_err(dev, "dma request signal %d busy, routed to %s\n",
135 mux->signal, mux->muxval ? mux->name_sel1 : mux->name_sel1);
136 of_node_put(dma_spec->np);
137 ret = -EBUSY;
138 goto err_put_pdev;
139 }
140
141 mux->busy = true;
142 mux->muxval = dma_spec->args[2] ? BIT(mux->bit) : 0;
143
144 regmap_update_bits(dmamux->reg, mux->muxreg, BIT(mux->bit), mux->muxval);
145 spin_unlock_irqrestore(&dmamux->lock, flags);
146
147 dma_spec->args[2] = 0;
148 dma_spec->args_count = 2;
149
150 dev_dbg(dev, "dma request signal %d routed to %s\n",
151 mux->signal, mux->muxval ? mux->name_sel1 : mux->name_sel1);
152
153 put_device(&pdev->dev);
154
155 return mux;
156
157err_put_pdev:
158 put_device(&pdev->dev);
159
160 return ERR_PTR(ret);
161}
162
163static int lpc32xx_dmamux_probe(struct platform_device *pdev)
164{
165 struct device_node *np = pdev->dev.of_node;
166 struct lpc32xx_dmamux_data *dmamux;
167
168 dmamux = devm_kzalloc(&pdev->dev, sizeof(*dmamux), GFP_KERNEL);
169 if (!dmamux)
170 return -ENOMEM;
171
172 dmamux->reg = syscon_node_to_regmap(np->parent);
173 if (IS_ERR(dmamux->reg)) {
174 dev_err(&pdev->dev, "syscon lookup failed\n");
175 return PTR_ERR(dmamux->reg);
176 }
177
178 spin_lock_init(&dmamux->lock);
179 platform_set_drvdata(pdev, dmamux);
180 dmamux->dmarouter.dev = &pdev->dev;
181 dmamux->dmarouter.route_free = lpc32xx_dmamux_release;
182
183 return of_dma_router_register(np, lpc32xx_dmamux_reserve,
184 &dmamux->dmarouter);
185}
186
187static const struct of_device_id lpc32xx_dmamux_match[] = {
188 { .compatible = "nxp,lpc3220-dmamux" },
189 {},
190};
191
192static struct platform_driver lpc32xx_dmamux_driver = {
193 .probe = lpc32xx_dmamux_probe,
194 .driver = {
195 .name = "lpc32xx-dmamux",
196 .of_match_table = lpc32xx_dmamux_match,
197 },
198};
199
200static int __init lpc32xx_dmamux_init(void)
201{
202 return platform_driver_register(&lpc32xx_dmamux_driver);
203}
204arch_initcall(lpc32xx_dmamux_init);