xref: /linux/drivers/dma/lpc18xx-dmamux.c (revision d30c1683aaecb93d2ab95685dc4300a33d3cea7a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * DMA Router driver for LPC18xx/43xx DMA MUX
4  *
5  * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
6  *
7  * Based on TI DMA Crossbar driver by:
8  *   Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
9  *   Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
10  */
11 
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/of.h>
16 #include <linux/of_dma.h>
17 #include <linux/of_platform.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/spinlock.h>
21 
22 /* CREG register offset and macros for mux manipulation */
23 #define LPC18XX_CREG_DMAMUX		0x11c
24 #define LPC18XX_DMAMUX_VAL(v, n)	((v) << (n * 2))
25 #define LPC18XX_DMAMUX_MASK(n)		(0x3 << (n * 2))
26 #define LPC18XX_DMAMUX_MAX_VAL		0x3
27 
28 struct lpc18xx_dmamux {
29 	u32 value;
30 	bool busy;
31 };
32 
33 struct lpc18xx_dmamux_data {
34 	struct dma_router dmarouter;
35 	struct lpc18xx_dmamux *muxes;
36 	u32 dma_master_requests;
37 	u32 dma_mux_requests;
38 	struct regmap *reg;
39 	spinlock_t lock;
40 };
41 
42 static void lpc18xx_dmamux_free(struct device *dev, void *route_data)
43 {
44 	struct lpc18xx_dmamux_data *dmamux = dev_get_drvdata(dev);
45 	struct lpc18xx_dmamux *mux = route_data;
46 	unsigned long flags;
47 
48 	spin_lock_irqsave(&dmamux->lock, flags);
49 	mux->busy = false;
50 	spin_unlock_irqrestore(&dmamux->lock, flags);
51 }
52 
53 static void *lpc18xx_dmamux_reserve(struct of_phandle_args *dma_spec,
54 				    struct of_dma *ofdma)
55 {
56 	struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
57 	struct lpc18xx_dmamux_data *dmamux = platform_get_drvdata(pdev);
58 	unsigned long flags;
59 	unsigned mux;
60 	int ret = -EINVAL;
61 
62 	if (dma_spec->args_count != 3) {
63 		dev_err(&pdev->dev, "invalid number of dma mux args\n");
64 		goto err_put_pdev;
65 	}
66 
67 	mux = dma_spec->args[0];
68 	if (mux >= dmamux->dma_master_requests) {
69 		dev_err(&pdev->dev, "invalid mux number: %d\n",
70 			dma_spec->args[0]);
71 		goto err_put_pdev;
72 	}
73 
74 	if (dma_spec->args[1] > LPC18XX_DMAMUX_MAX_VAL) {
75 		dev_err(&pdev->dev, "invalid dma mux value: %d\n",
76 			dma_spec->args[1]);
77 		goto err_put_pdev;
78 	}
79 
80 	/* The of_node_put() will be done in the core for the node */
81 	dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
82 	if (!dma_spec->np) {
83 		dev_err(&pdev->dev, "can't get dma master\n");
84 		goto err_put_pdev;
85 	}
86 
87 	spin_lock_irqsave(&dmamux->lock, flags);
88 	if (dmamux->muxes[mux].busy) {
89 		spin_unlock_irqrestore(&dmamux->lock, flags);
90 		dev_err(&pdev->dev, "dma request %u busy with %u.%u\n",
91 			mux, mux, dmamux->muxes[mux].value);
92 		of_node_put(dma_spec->np);
93 		ret = -EBUSY;
94 		goto err_put_pdev;
95 	}
96 
97 	dmamux->muxes[mux].busy = true;
98 	dmamux->muxes[mux].value = dma_spec->args[1];
99 
100 	regmap_update_bits(dmamux->reg, LPC18XX_CREG_DMAMUX,
101 			   LPC18XX_DMAMUX_MASK(mux),
102 			   LPC18XX_DMAMUX_VAL(dmamux->muxes[mux].value, mux));
103 	spin_unlock_irqrestore(&dmamux->lock, flags);
104 
105 	dma_spec->args[1] = dma_spec->args[2];
106 	dma_spec->args_count = 2;
107 
108 	dev_dbg(&pdev->dev, "mapping dmamux %u.%u to dma request %u\n", mux,
109 		dmamux->muxes[mux].value, mux);
110 
111 	put_device(&pdev->dev);
112 
113 	return &dmamux->muxes[mux];
114 
115 err_put_pdev:
116 	put_device(&pdev->dev);
117 
118 	return ERR_PTR(ret);
119 }
120 
121 static int lpc18xx_dmamux_probe(struct platform_device *pdev)
122 {
123 	struct device_node *dma_np, *np = pdev->dev.of_node;
124 	struct lpc18xx_dmamux_data *dmamux;
125 	int ret;
126 
127 	dmamux = devm_kzalloc(&pdev->dev, sizeof(*dmamux), GFP_KERNEL);
128 	if (!dmamux)
129 		return -ENOMEM;
130 
131 	dmamux->reg = syscon_regmap_lookup_by_compatible("nxp,lpc1850-creg");
132 	if (IS_ERR(dmamux->reg)) {
133 		dev_err(&pdev->dev, "syscon lookup failed\n");
134 		return PTR_ERR(dmamux->reg);
135 	}
136 
137 	ret = of_property_read_u32(np, "dma-requests",
138 				   &dmamux->dma_mux_requests);
139 	if (ret) {
140 		dev_err(&pdev->dev, "missing dma-requests property\n");
141 		return ret;
142 	}
143 
144 	dma_np = of_parse_phandle(np, "dma-masters", 0);
145 	if (!dma_np) {
146 		dev_err(&pdev->dev, "can't get dma master\n");
147 		return -ENODEV;
148 	}
149 
150 	ret = of_property_read_u32(dma_np, "dma-requests",
151 				   &dmamux->dma_master_requests);
152 	of_node_put(dma_np);
153 	if (ret) {
154 		dev_err(&pdev->dev, "missing master dma-requests property\n");
155 		return ret;
156 	}
157 
158 	dmamux->muxes = devm_kcalloc(&pdev->dev, dmamux->dma_master_requests,
159 				     sizeof(struct lpc18xx_dmamux),
160 				     GFP_KERNEL);
161 	if (!dmamux->muxes)
162 		return -ENOMEM;
163 
164 	spin_lock_init(&dmamux->lock);
165 	platform_set_drvdata(pdev, dmamux);
166 	dmamux->dmarouter.dev = &pdev->dev;
167 	dmamux->dmarouter.route_free = lpc18xx_dmamux_free;
168 
169 	return of_dma_router_register(np, lpc18xx_dmamux_reserve,
170 				      &dmamux->dmarouter);
171 }
172 
173 static const struct of_device_id lpc18xx_dmamux_match[] = {
174 	{ .compatible = "nxp,lpc1850-dmamux" },
175 	{},
176 };
177 
178 static struct platform_driver lpc18xx_dmamux_driver = {
179 	.probe	= lpc18xx_dmamux_probe,
180 	.driver = {
181 		.name = "lpc18xx-dmamux",
182 		.of_match_table = lpc18xx_dmamux_match,
183 	},
184 };
185 
186 static int __init lpc18xx_dmamux_init(void)
187 {
188 	return platform_driver_register(&lpc18xx_dmamux_driver);
189 }
190 arch_initcall(lpc18xx_dmamux_init);
191