1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/slab.h>
18#include <linux/of.h>
19#include <linux/of_dma.h>
20
21static LIST_HEAD(of_dma_list);
22static DEFINE_MUTEX(of_dma_lock);
23
24
25
26
27
28
29
30
31
32
33static struct of_dma *of_dma_find_controller(struct of_phandle_args *dma_spec)
34{
35 struct of_dma *ofdma;
36
37 list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
38 if (ofdma->of_node == dma_spec->np)
39 return ofdma;
40
41 pr_debug("%s: can't find DMA controller %s\n", __func__,
42 dma_spec->np->full_name);
43
44 return NULL;
45}
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60int of_dma_controller_register(struct device_node *np,
61 struct dma_chan *(*of_dma_xlate)
62 (struct of_phandle_args *, struct of_dma *),
63 void *data)
64{
65 struct of_dma *ofdma;
66
67 if (!np || !of_dma_xlate) {
68 pr_err("%s: not enough information provided\n", __func__);
69 return -EINVAL;
70 }
71
72 ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
73 if (!ofdma)
74 return -ENOMEM;
75
76 ofdma->of_node = np;
77 ofdma->of_dma_xlate = of_dma_xlate;
78 ofdma->of_dma_data = data;
79
80
81 mutex_lock(&of_dma_lock);
82 list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
83 mutex_unlock(&of_dma_lock);
84
85 return 0;
86}
87EXPORT_SYMBOL_GPL(of_dma_controller_register);
88
89
90
91
92
93
94
95void of_dma_controller_free(struct device_node *np)
96{
97 struct of_dma *ofdma;
98
99 mutex_lock(&of_dma_lock);
100
101 list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
102 if (ofdma->of_node == np) {
103 list_del(&ofdma->of_dma_controllers);
104 kfree(ofdma);
105 break;
106 }
107
108 mutex_unlock(&of_dma_lock);
109}
110EXPORT_SYMBOL_GPL(of_dma_controller_free);
111
112
113
114
115
116
117
118
119
120
121
122
123static int of_dma_match_channel(struct device_node *np, const char *name,
124 int index, struct of_phandle_args *dma_spec)
125{
126 const char *s;
127
128 if (of_property_read_string_index(np, "dma-names", index, &s))
129 return -ENODEV;
130
131 if (strcmp(name, s))
132 return -ENODEV;
133
134 if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
135 dma_spec))
136 return -ENODEV;
137
138 return 0;
139}
140
141
142
143
144
145
146
147
148struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
149 const char *name)
150{
151 struct of_phandle_args dma_spec;
152 struct of_dma *ofdma;
153 struct dma_chan *chan;
154 int count, i;
155 int ret_no_channel = -ENODEV;
156
157 if (!np || !name) {
158 pr_err("%s: not enough information provided\n", __func__);
159 return ERR_PTR(-ENODEV);
160 }
161
162 count = of_property_count_strings(np, "dma-names");
163 if (count < 0) {
164 pr_err("%s: dma-names property of node '%s' missing or empty\n",
165 __func__, np->full_name);
166 return ERR_PTR(-ENODEV);
167 }
168
169 for (i = 0; i < count; i++) {
170 if (of_dma_match_channel(np, name, i, &dma_spec))
171 continue;
172
173 mutex_lock(&of_dma_lock);
174 ofdma = of_dma_find_controller(&dma_spec);
175
176 if (ofdma) {
177 chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
178 } else {
179 ret_no_channel = -EPROBE_DEFER;
180 chan = NULL;
181 }
182
183 mutex_unlock(&of_dma_lock);
184
185 of_node_put(dma_spec.np);
186
187 if (chan)
188 return chan;
189 }
190
191 return ERR_PTR(ret_no_channel);
192}
193
194
195
196
197
198
199
200
201
202
203
204
205struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
206 struct of_dma *ofdma)
207{
208 int count = dma_spec->args_count;
209 struct of_dma_filter_info *info = ofdma->of_dma_data;
210
211 if (!info || !info->filter_fn)
212 return NULL;
213
214 if (count != 1)
215 return NULL;
216
217 return dma_request_channel(info->dma_cap, info->filter_fn,
218 &dma_spec->args[0]);
219}
220EXPORT_SYMBOL_GPL(of_dma_simple_xlate);
221