1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/module.h>
17#include <linux/fsl/guts.h>
18#include <linux/interrupt.h>
19#include <linux/of_address.h>
20#include <linux/of_device.h>
21#include <linux/slab.h>
22#include <sound/soc.h>
23
24#include "fsl_dma.h"
25#include "fsl_ssi.h"
26#include "fsl_utils.h"
27
28
29
30#define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000
31#define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000
32#define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000
33
34#define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00
35#define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000
36
37#define CCSR_GUTS_DMUXCR_PAD 1
38#define CCSR_GUTS_DMUXCR_SSI 2
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
54 unsigned int co, unsigned int ch, unsigned int device)
55{
56 unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
57
58 clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
59}
60
61
62static phys_addr_t guts_phys;
63
64
65
66
67
68
69
70struct machine_data {
71 struct snd_soc_dai_link dai[2];
72 struct snd_soc_card card;
73 unsigned int dai_format;
74 unsigned int codec_clk_direction;
75 unsigned int cpu_clk_direction;
76 unsigned int clk_frequency;
77 unsigned int dma_id[2];
78 unsigned int dma_channel_id[2];
79 char platform_name[2][DAI_NAME_SIZE];
80};
81
82
83
84
85
86
87
88
89static int p1022_rdk_machine_probe(struct snd_soc_card *card)
90{
91 struct machine_data *mdata =
92 container_of(card, struct machine_data, card);
93 struct ccsr_guts __iomem *guts;
94
95 guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
96 if (!guts) {
97 dev_err(card->dev, "could not map global utilities\n");
98 return -ENOMEM;
99 }
100
101
102 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
103 CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
104
105
106 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
107 CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
108
109
110 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
111 CCSR_GUTS_DMUXCR_SSI);
112
113 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
114 CCSR_GUTS_DMUXCR_SSI);
115
116 iounmap(guts);
117
118 return 0;
119}
120
121
122
123
124
125
126
127
128static int p1022_rdk_startup(struct snd_pcm_substream *substream)
129{
130 struct snd_soc_pcm_runtime *rtd = substream->private_data;
131 struct machine_data *mdata =
132 container_of(rtd->card, struct machine_data, card);
133 struct device *dev = rtd->card->dev;
134 int ret = 0;
135
136
137 ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format);
138 if (ret < 0) {
139 dev_err(dev, "could not set codec driver audio format (ret=%i)\n",
140 ret);
141 return ret;
142 }
143
144 ret = snd_soc_dai_set_pll(rtd->codec_dai, 0, 0, mdata->clk_frequency,
145 mdata->clk_frequency);
146 if (ret < 0) {
147 dev_err(dev, "could not set codec PLL frequency (ret=%i)\n",
148 ret);
149 return ret;
150 }
151
152 return 0;
153}
154
155
156
157
158
159
160
161static int p1022_rdk_machine_remove(struct snd_soc_card *card)
162{
163 struct machine_data *mdata =
164 container_of(card, struct machine_data, card);
165 struct ccsr_guts __iomem *guts;
166
167 guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
168 if (!guts) {
169 dev_err(card->dev, "could not map global utilities\n");
170 return -ENOMEM;
171 }
172
173
174 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
175 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
176 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
177 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
178
179 iounmap(guts);
180
181 return 0;
182}
183
184
185
186
187static const struct snd_soc_ops p1022_rdk_ops = {
188 .startup = p1022_rdk_startup,
189};
190
191
192
193
194
195
196
197
198static int p1022_rdk_probe(struct platform_device *pdev)
199{
200 struct device *dev = pdev->dev.parent;
201
202 struct platform_device *ssi_pdev = to_platform_device(dev);
203 struct device_node *np = ssi_pdev->dev.of_node;
204 struct device_node *codec_np = NULL;
205 struct machine_data *mdata;
206 struct snd_soc_dai_link_component *comp;
207 const u32 *iprop;
208 int ret;
209
210
211 codec_np = of_parse_phandle(np, "codec-handle", 0);
212 if (!codec_np) {
213 dev_err(dev, "could not find codec node\n");
214 return -EINVAL;
215 }
216
217 mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
218 if (!mdata) {
219 ret = -ENOMEM;
220 goto error_put;
221 }
222
223 comp = devm_kzalloc(&pdev->dev, 6 * sizeof(*comp), GFP_KERNEL);
224 if (!comp) {
225 ret = -ENOMEM;
226 goto error_put;
227 }
228
229 mdata->dai[0].cpus = &comp[0];
230 mdata->dai[0].codecs = &comp[1];
231 mdata->dai[0].platforms = &comp[2];
232
233 mdata->dai[0].num_cpus = 1;
234 mdata->dai[0].num_codecs = 1;
235 mdata->dai[0].num_platforms = 1;
236
237 mdata->dai[1].cpus = &comp[3];
238 mdata->dai[1].codecs = &comp[4];
239 mdata->dai[1].platforms = &comp[5];
240
241 mdata->dai[1].num_cpus = 1;
242 mdata->dai[1].num_codecs = 1;
243 mdata->dai[1].num_platforms = 1;
244
245 mdata->dai[0].cpus->dai_name = dev_name(&ssi_pdev->dev);
246 mdata->dai[0].ops = &p1022_rdk_ops;
247
248
249 mdata->dai[0].codecs->of_node = codec_np;
250
251
252
253
254
255
256 memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
257
258
259 mdata->dai[0].codecs->dai_name = "wm8960-hifi";
260 mdata->dai[1].codecs->dai_name = mdata->dai[0].codecs->dai_name;
261
262
263
264
265
266
267 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
268 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
269 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
270 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
271
272
273
274
275
276
277 iprop = of_get_property(codec_np, "clock-frequency", NULL);
278 if (!iprop || !*iprop) {
279 dev_err(&pdev->dev, "codec bus-frequency property is missing or invalid\n");
280 ret = -EINVAL;
281 goto error;
282 }
283 mdata->clk_frequency = be32_to_cpup(iprop);
284
285 if (!mdata->clk_frequency) {
286 dev_err(&pdev->dev, "unknown clock frequency\n");
287 ret = -EINVAL;
288 goto error;
289 }
290
291
292 mdata->dai[0].platforms->name = mdata->platform_name[0];
293 ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
294 &mdata->dma_channel_id[0],
295 &mdata->dma_id[0]);
296 if (ret) {
297 dev_err(&pdev->dev, "missing/invalid playback DMA phandle (ret=%i)\n",
298 ret);
299 goto error;
300 }
301
302
303 mdata->dai[1].platforms->name = mdata->platform_name[1];
304 ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
305 &mdata->dma_channel_id[1],
306 &mdata->dma_id[1]);
307 if (ret) {
308 dev_err(&pdev->dev, "missing/invalid capture DMA phandle (ret=%i)\n",
309 ret);
310 goto error;
311 }
312
313
314 mdata->dai[0].stream_name = "playback";
315 mdata->dai[1].stream_name = "capture";
316 mdata->dai[0].name = mdata->dai[0].stream_name;
317 mdata->dai[1].name = mdata->dai[1].stream_name;
318
319 mdata->card.probe = p1022_rdk_machine_probe;
320 mdata->card.remove = p1022_rdk_machine_remove;
321 mdata->card.name = pdev->name;
322 mdata->card.owner = THIS_MODULE;
323 mdata->card.dev = &pdev->dev;
324 mdata->card.num_links = 2;
325 mdata->card.dai_link = mdata->dai;
326
327
328 ret = snd_soc_register_card(&mdata->card);
329 if (ret) {
330 dev_err(&pdev->dev, "could not register card (ret=%i)\n", ret);
331 goto error;
332 }
333
334 return 0;
335
336error:
337 kfree(mdata);
338error_put:
339 of_node_put(codec_np);
340 return ret;
341}
342
343
344
345
346
347
348static int p1022_rdk_remove(struct platform_device *pdev)
349{
350 struct snd_soc_card *card = platform_get_drvdata(pdev);
351 struct machine_data *mdata =
352 container_of(card, struct machine_data, card);
353
354 snd_soc_unregister_card(card);
355 kfree(mdata);
356
357 return 0;
358}
359
360static struct platform_driver p1022_rdk_driver = {
361 .probe = p1022_rdk_probe,
362 .remove = p1022_rdk_remove,
363 .driver = {
364
365
366
367
368 .name = "snd-soc-p1022rdk",
369 },
370};
371
372
373
374
375
376
377static int __init p1022_rdk_init(void)
378{
379 struct device_node *guts_np;
380 struct resource res;
381
382
383 guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
384 if (of_address_to_resource(guts_np, 0, &res)) {
385 pr_err("snd-soc-p1022rdk: missing/invalid global utils node\n");
386 of_node_put(guts_np);
387 return -EINVAL;
388 }
389 guts_phys = res.start;
390 of_node_put(guts_np);
391
392 return platform_driver_register(&p1022_rdk_driver);
393}
394
395
396
397
398
399
400static void __exit p1022_rdk_exit(void)
401{
402 platform_driver_unregister(&p1022_rdk_driver);
403}
404
405late_initcall(p1022_rdk_init);
406module_exit(p1022_rdk_exit);
407
408MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
409MODULE_DESCRIPTION("Freescale / iVeia P1022 RDK ALSA SoC machine driver");
410MODULE_LICENSE("GPL v2");
411