1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/interrupt.h>
15#include <linux/of_device.h>
16#include <linux/slab.h>
17#include <sound/soc.h>
18#include <asm/fsl_guts.h>
19
20#include "fsl_dma.h"
21#include "fsl_ssi.h"
22
23
24static phys_addr_t guts_phys;
25
26#define DAI_NAME_SIZE 32
27
28
29
30
31
32
33
34struct mpc8610_hpcd_data {
35 struct snd_soc_dai_link dai[2];
36 struct snd_soc_card card;
37 unsigned int dai_format;
38 unsigned int codec_clk_direction;
39 unsigned int cpu_clk_direction;
40 unsigned int clk_frequency;
41 unsigned int ssi_id;
42 unsigned int dma_id[2];
43 unsigned int dma_channel_id[2];
44 char codec_dai_name[DAI_NAME_SIZE];
45 char codec_name[DAI_NAME_SIZE];
46 char platform_name[2][DAI_NAME_SIZE];
47};
48
49
50
51
52
53
54
55
56static int mpc8610_hpcd_machine_probe(struct platform_device *sound_device)
57{
58 struct snd_soc_card *card = platform_get_drvdata(sound_device);
59 struct mpc8610_hpcd_data *machine_data =
60 container_of(card, struct mpc8610_hpcd_data, card);
61 struct ccsr_guts_86xx __iomem *guts;
62
63 guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
64 if (!guts) {
65 dev_err(card->dev, "could not map global utilities\n");
66 return -ENOMEM;
67 }
68
69
70 guts_set_dmacr(guts, machine_data->dma_id[0],
71 machine_data->dma_channel_id[0],
72 CCSR_GUTS_DMACR_DEV_SSI);
73 guts_set_dmacr(guts, machine_data->dma_id[1],
74 machine_data->dma_channel_id[1],
75 CCSR_GUTS_DMACR_DEV_SSI);
76
77 guts_set_pmuxcr_dma(guts, machine_data->dma_id[0],
78 machine_data->dma_channel_id[0], 0);
79 guts_set_pmuxcr_dma(guts, machine_data->dma_id[1],
80 machine_data->dma_channel_id[1], 0);
81
82 switch (machine_data->ssi_id) {
83 case 0:
84 clrsetbits_be32(&guts->pmuxcr,
85 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_SSI);
86 break;
87 case 1:
88 clrsetbits_be32(&guts->pmuxcr,
89 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_SSI);
90 break;
91 }
92
93 iounmap(guts);
94
95 return 0;
96}
97
98
99
100
101
102
103
104
105static int mpc8610_hpcd_startup(struct snd_pcm_substream *substream)
106{
107 struct snd_soc_pcm_runtime *rtd = substream->private_data;
108 struct mpc8610_hpcd_data *machine_data =
109 container_of(rtd->card, struct mpc8610_hpcd_data, card);
110 struct device *dev = rtd->card->dev;
111 int ret = 0;
112
113
114 ret = snd_soc_dai_set_fmt(rtd->codec_dai, machine_data->dai_format);
115 if (ret < 0) {
116 dev_err(dev, "could not set codec driver audio format\n");
117 return ret;
118 }
119
120
121
122
123
124 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0,
125 machine_data->clk_frequency,
126 machine_data->codec_clk_direction);
127 if (ret < 0) {
128 dev_err(dev, "could not set codec driver clock params\n");
129 return ret;
130 }
131
132 return 0;
133}
134
135
136
137
138
139
140
141static int mpc8610_hpcd_machine_remove(struct platform_device *sound_device)
142{
143 struct snd_soc_card *card = platform_get_drvdata(sound_device);
144 struct mpc8610_hpcd_data *machine_data =
145 container_of(card, struct mpc8610_hpcd_data, card);
146 struct ccsr_guts_86xx __iomem *guts;
147
148 guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
149 if (!guts) {
150 dev_err(card->dev, "could not map global utilities\n");
151 return -ENOMEM;
152 }
153
154
155
156 guts_set_dmacr(guts, machine_data->dma_id[0],
157 machine_data->dma_channel_id[0], 0);
158 guts_set_dmacr(guts, machine_data->dma_id[1],
159 machine_data->dma_channel_id[1], 0);
160
161 switch (machine_data->ssi_id) {
162 case 0:
163 clrsetbits_be32(&guts->pmuxcr,
164 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
165 break;
166 case 1:
167 clrsetbits_be32(&guts->pmuxcr,
168 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_LA);
169 break;
170 }
171
172 iounmap(guts);
173
174 return 0;
175}
176
177
178
179
180static struct snd_soc_ops mpc8610_hpcd_ops = {
181 .startup = mpc8610_hpcd_startup,
182};
183
184
185
186
187
188
189
190
191
192
193
194static struct device_node *get_node_by_phandle_name(struct device_node *np,
195 const char *name,
196 const char *compatible)
197{
198 const phandle *ph;
199 int len;
200
201 ph = of_get_property(np, name, &len);
202 if (!ph || (len != sizeof(phandle)))
203 return NULL;
204
205 np = of_find_node_by_phandle(*ph);
206 if (!np)
207 return NULL;
208
209 if (compatible && !of_device_is_compatible(np, compatible)) {
210 of_node_put(np);
211 return NULL;
212 }
213
214 return np;
215}
216
217
218
219
220
221
222
223
224static int get_parent_cell_index(struct device_node *np)
225{
226 struct device_node *parent = of_get_parent(np);
227 const u32 *iprop;
228
229 if (!parent)
230 return -1;
231
232 iprop = of_get_property(parent, "cell-index", NULL);
233 of_node_put(parent);
234
235 if (!iprop)
236 return -1;
237
238 return *iprop;
239}
240
241
242
243
244
245
246
247
248
249
250
251static int codec_node_dev_name(struct device_node *np, char *buf, size_t len)
252{
253 const u32 *iprop;
254 int bus, addr;
255 char temp[DAI_NAME_SIZE];
256
257 of_modalias_node(np, temp, DAI_NAME_SIZE);
258
259 iprop = of_get_property(np, "reg", NULL);
260 if (!iprop)
261 return -EINVAL;
262
263 addr = *iprop;
264
265 bus = get_parent_cell_index(np);
266 if (bus < 0)
267 return bus;
268
269 snprintf(buf, len, "%s-codec.%u-%04x", temp, bus, addr);
270
271 return 0;
272}
273
274static int get_dma_channel(struct device_node *ssi_np,
275 const char *compatible,
276 struct snd_soc_dai_link *dai,
277 unsigned int *dma_channel_id,
278 unsigned int *dma_id)
279{
280 struct resource res;
281 struct device_node *dma_channel_np;
282 const u32 *iprop;
283 int ret;
284
285 dma_channel_np = get_node_by_phandle_name(ssi_np, compatible,
286 "fsl,ssi-dma-channel");
287 if (!dma_channel_np)
288 return -EINVAL;
289
290
291
292
293
294
295
296
297
298 ret = of_address_to_resource(dma_channel_np, 0, &res);
299 if (ret)
300 return ret;
301 snprintf((char *)dai->platform_name, DAI_NAME_SIZE, "%llx.%s",
302 (unsigned long long) res.start, dma_channel_np->name);
303
304 iprop = of_get_property(dma_channel_np, "cell-index", NULL);
305 if (!iprop) {
306 of_node_put(dma_channel_np);
307 return -EINVAL;
308 }
309
310 *dma_channel_id = *iprop;
311 *dma_id = get_parent_cell_index(dma_channel_np);
312 of_node_put(dma_channel_np);
313
314 return 0;
315}
316
317
318
319
320
321
322
323
324static int mpc8610_hpcd_probe(struct platform_device *pdev)
325{
326 struct device *dev = pdev->dev.parent;
327
328 struct platform_device *ssi_pdev =
329 container_of(dev, struct platform_device, dev);
330 struct device_node *np = ssi_pdev->dev.of_node;
331 struct device_node *codec_np = NULL;
332 struct platform_device *sound_device = NULL;
333 struct mpc8610_hpcd_data *machine_data;
334 int ret = -ENODEV;
335 const char *sprop;
336 const u32 *iprop;
337
338
339
340
341
342 codec_np = get_node_by_phandle_name(np, "codec-handle",
343 "cirrus,cs4270");
344 if (!codec_np) {
345 dev_err(dev, "invalid codec node\n");
346 return -EINVAL;
347 }
348
349 machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
350 if (!machine_data)
351 return -ENOMEM;
352
353 machine_data->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
354 machine_data->dai[0].ops = &mpc8610_hpcd_ops;
355
356
357 ret = codec_node_dev_name(codec_np, machine_data->codec_name,
358 DAI_NAME_SIZE);
359 if (ret) {
360 dev_err(&pdev->dev, "invalid codec node %s\n",
361 codec_np->full_name);
362 ret = -EINVAL;
363 goto error;
364 }
365 machine_data->dai[0].codec_name = machine_data->codec_name;
366
367
368 machine_data->dai[0].codec_dai_name = "cs4270-hifi";
369
370
371
372
373
374 memcpy(&machine_data->dai[1], &machine_data->dai[0],
375 sizeof(struct snd_soc_dai_link));
376
377
378 iprop = of_get_property(np, "cell-index", NULL);
379 if (!iprop) {
380 dev_err(&pdev->dev, "cell-index property not found\n");
381 ret = -EINVAL;
382 goto error;
383 }
384 machine_data->ssi_id = *iprop;
385
386
387 sprop = of_get_property(np, "fsl,mode", NULL);
388 if (!sprop) {
389 dev_err(&pdev->dev, "fsl,mode property not found\n");
390 ret = -EINVAL;
391 goto error;
392 }
393
394 if (strcasecmp(sprop, "i2s-slave") == 0) {
395 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
396 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
397 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
398
399
400
401
402
403 iprop = of_get_property(codec_np, "clock-frequency", NULL);
404 if (!iprop || !*iprop) {
405 dev_err(&pdev->dev, "codec bus-frequency "
406 "property is missing or invalid\n");
407 ret = -EINVAL;
408 goto error;
409 }
410 machine_data->clk_frequency = *iprop;
411 } else if (strcasecmp(sprop, "i2s-master") == 0) {
412 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
413 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
414 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
415 } else if (strcasecmp(sprop, "lj-slave") == 0) {
416 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
417 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
418 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
419 } else if (strcasecmp(sprop, "lj-master") == 0) {
420 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
421 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
422 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
423 } else if (strcasecmp(sprop, "rj-slave") == 0) {
424 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
425 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
426 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
427 } else if (strcasecmp(sprop, "rj-master") == 0) {
428 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
429 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
430 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
431 } else if (strcasecmp(sprop, "ac97-slave") == 0) {
432 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
433 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
434 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
435 } else if (strcasecmp(sprop, "ac97-master") == 0) {
436 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
437 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
438 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
439 } else {
440 dev_err(&pdev->dev,
441 "unrecognized fsl,mode property '%s'\n", sprop);
442 ret = -EINVAL;
443 goto error;
444 }
445
446 if (!machine_data->clk_frequency) {
447 dev_err(&pdev->dev, "unknown clock frequency\n");
448 ret = -EINVAL;
449 goto error;
450 }
451
452
453 machine_data->dai[0].platform_name = machine_data->platform_name[0];
454 ret = get_dma_channel(np, "fsl,playback-dma", &machine_data->dai[0],
455 &machine_data->dma_channel_id[0],
456 &machine_data->dma_id[0]);
457 if (ret) {
458 dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
459 goto error;
460 }
461
462
463 machine_data->dai[1].platform_name = machine_data->platform_name[1];
464 ret = get_dma_channel(np, "fsl,capture-dma", &machine_data->dai[1],
465 &machine_data->dma_channel_id[1],
466 &machine_data->dma_id[1]);
467 if (ret) {
468 dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
469 goto error;
470 }
471
472
473 machine_data->dai[0].stream_name = "playback";
474 machine_data->dai[1].stream_name = "capture";
475 machine_data->dai[0].name = machine_data->dai[0].stream_name;
476 machine_data->dai[1].name = machine_data->dai[1].stream_name;
477
478 machine_data->card.probe = mpc8610_hpcd_machine_probe;
479 machine_data->card.remove = mpc8610_hpcd_machine_remove;
480 machine_data->card.name = pdev->name;
481 machine_data->card.num_links = 2;
482 machine_data->card.dai_link = machine_data->dai;
483
484
485 sound_device = platform_device_alloc("soc-audio", -1);
486 if (!sound_device) {
487 dev_err(&pdev->dev, "platform device alloc failed\n");
488 ret = -ENOMEM;
489 goto error;
490 }
491
492
493 platform_set_drvdata(sound_device, &machine_data->card);
494
495
496 ret = platform_device_add(sound_device);
497 if (ret) {
498 dev_err(&pdev->dev, "platform device add failed\n");
499 goto error;
500 }
501 dev_set_drvdata(&pdev->dev, sound_device);
502
503 of_node_put(codec_np);
504
505 return 0;
506
507error:
508 of_node_put(codec_np);
509
510 if (sound_device)
511 platform_device_unregister(sound_device);
512
513 kfree(machine_data);
514
515 return ret;
516}
517
518
519
520
521
522
523static int __devexit mpc8610_hpcd_remove(struct platform_device *pdev)
524{
525 struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
526 struct snd_soc_card *card = platform_get_drvdata(sound_device);
527 struct mpc8610_hpcd_data *machine_data =
528 container_of(card, struct mpc8610_hpcd_data, card);
529
530 platform_device_unregister(sound_device);
531
532 kfree(machine_data);
533 sound_device->dev.platform_data = NULL;
534
535 dev_set_drvdata(&pdev->dev, NULL);
536
537 return 0;
538}
539
540static struct platform_driver mpc8610_hpcd_driver = {
541 .probe = mpc8610_hpcd_probe,
542 .remove = __devexit_p(mpc8610_hpcd_remove),
543 .driver = {
544
545
546
547 .name = "snd-soc-mpc8610hpcd",
548 .owner = THIS_MODULE,
549 },
550};
551
552
553
554
555
556
557static int __init mpc8610_hpcd_init(void)
558{
559 struct device_node *guts_np;
560 struct resource res;
561
562 pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
563
564
565 guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
566 if (of_address_to_resource(guts_np, 0, &res)) {
567 pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
568 return -EINVAL;
569 }
570 guts_phys = res.start;
571
572 return platform_driver_register(&mpc8610_hpcd_driver);
573}
574
575
576
577
578
579
580static void __exit mpc8610_hpcd_exit(void)
581{
582 platform_driver_unregister(&mpc8610_hpcd_driver);
583}
584
585module_init(mpc8610_hpcd_init);
586module_exit(mpc8610_hpcd_exit);
587
588MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
589MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
590MODULE_LICENSE("GPL v2");
591