]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - sound/soc/fsl/mpc8610_hpcd.c
Merge branch 'topic/cs5535audio' into to-push
[mirror_ubuntu-jammy-kernel.git] / sound / soc / fsl / mpc8610_hpcd.c
1 /**
2 * Freescale MPC8610HPCD ALSA SoC Fabric driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
6 * Copyright 2007-2008 Freescale Semiconductor, Inc. This file is licensed
7 * under the terms of the GNU General Public License version 2. This
8 * program is licensed "as is" without any warranty of any kind, whether
9 * express or implied.
10 */
11
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/of_device.h>
15 #include <linux/of_platform.h>
16 #include <sound/soc.h>
17 #include <asm/immap_86xx.h>
18
19 #include "../codecs/cs4270.h"
20 #include "fsl_dma.h"
21 #include "fsl_ssi.h"
22
23 /**
24 * mpc8610_hpcd_data: fabric-specific ASoC device data
25 *
26 * This structure contains data for a single sound platform device on an
27 * MPC8610 HPCD. Some of the data is taken from the device tree.
28 */
29 struct mpc8610_hpcd_data {
30 struct snd_soc_device sound_devdata;
31 struct snd_soc_dai_link dai;
32 struct snd_soc_card machine;
33 unsigned int dai_format;
34 unsigned int codec_clk_direction;
35 unsigned int cpu_clk_direction;
36 unsigned int clk_frequency;
37 struct ccsr_guts __iomem *guts;
38 struct ccsr_ssi __iomem *ssi;
39 unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
40 unsigned int ssi_irq;
41 unsigned int dma_id; /* 0 = DMA1, 1 = DMA2, etc */
42 unsigned int dma_irq[2];
43 struct ccsr_dma_channel __iomem *dma[2];
44 unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
45 };
46
47 /**
48 * mpc8610_hpcd_machine_probe: initalize the board
49 *
50 * This function is called when platform_device_add() is called. It is used
51 * to initialize the board-specific hardware.
52 *
53 * Here we program the DMACR and PMUXCR registers.
54 */
55 static int mpc8610_hpcd_machine_probe(struct platform_device *sound_device)
56 {
57 struct mpc8610_hpcd_data *machine_data =
58 sound_device->dev.platform_data;
59
60 /* Program the signal routing between the SSI and the DMA */
61 guts_set_dmacr(machine_data->guts, machine_data->dma_id,
62 machine_data->dma_channel_id[0], CCSR_GUTS_DMACR_DEV_SSI);
63 guts_set_dmacr(machine_data->guts, machine_data->dma_id,
64 machine_data->dma_channel_id[1], CCSR_GUTS_DMACR_DEV_SSI);
65
66 guts_set_pmuxcr_dma(machine_data->guts, machine_data->dma_id,
67 machine_data->dma_channel_id[0], 0);
68 guts_set_pmuxcr_dma(machine_data->guts, machine_data->dma_id,
69 machine_data->dma_channel_id[1], 0);
70
71 switch (machine_data->ssi_id) {
72 case 0:
73 clrsetbits_be32(&machine_data->guts->pmuxcr,
74 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_SSI);
75 break;
76 case 1:
77 clrsetbits_be32(&machine_data->guts->pmuxcr,
78 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_SSI);
79 break;
80 }
81
82 return 0;
83 }
84
85 /**
86 * mpc8610_hpcd_startup: program the board with various hardware parameters
87 *
88 * This function takes board-specific information, like clock frequencies
89 * and serial data formats, and passes that information to the codec and
90 * transport drivers.
91 */
92 static int mpc8610_hpcd_startup(struct snd_pcm_substream *substream)
93 {
94 struct snd_soc_pcm_runtime *rtd = substream->private_data;
95 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
96 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
97 struct mpc8610_hpcd_data *machine_data =
98 rtd->socdev->dev->platform_data;
99 int ret = 0;
100
101 /* Tell the CPU driver what the serial protocol is. */
102 ret = snd_soc_dai_set_fmt(cpu_dai, machine_data->dai_format);
103 if (ret < 0) {
104 dev_err(substream->pcm->card->dev,
105 "could not set CPU driver audio format\n");
106 return ret;
107 }
108
109 /* Tell the codec driver what the serial protocol is. */
110 ret = snd_soc_dai_set_fmt(codec_dai, machine_data->dai_format);
111 if (ret < 0) {
112 dev_err(substream->pcm->card->dev,
113 "could not set codec driver audio format\n");
114 return ret;
115 }
116
117 /*
118 * Tell the CPU driver what the clock frequency is, and whether it's a
119 * slave or master.
120 */
121 ret = snd_soc_dai_set_sysclk(cpu_dai, 0,
122 machine_data->clk_frequency,
123 machine_data->cpu_clk_direction);
124 if (ret < 0) {
125 dev_err(substream->pcm->card->dev,
126 "could not set CPU driver clock parameters\n");
127 return ret;
128 }
129
130 /*
131 * Tell the codec driver what the MCLK frequency is, and whether it's
132 * a slave or master.
133 */
134 ret = snd_soc_dai_set_sysclk(codec_dai, 0,
135 machine_data->clk_frequency,
136 machine_data->codec_clk_direction);
137 if (ret < 0) {
138 dev_err(substream->pcm->card->dev,
139 "could not set codec driver clock params\n");
140 return ret;
141 }
142
143 return 0;
144 }
145
146 /**
147 * mpc8610_hpcd_machine_remove: Remove the sound device
148 *
149 * This function is called to remove the sound device for one SSI. We
150 * de-program the DMACR and PMUXCR register.
151 */
152 int mpc8610_hpcd_machine_remove(struct platform_device *sound_device)
153 {
154 struct mpc8610_hpcd_data *machine_data =
155 sound_device->dev.platform_data;
156
157 /* Restore the signal routing */
158
159 guts_set_dmacr(machine_data->guts, machine_data->dma_id,
160 machine_data->dma_channel_id[0], 0);
161 guts_set_dmacr(machine_data->guts, machine_data->dma_id,
162 machine_data->dma_channel_id[1], 0);
163
164 switch (machine_data->ssi_id) {
165 case 0:
166 clrsetbits_be32(&machine_data->guts->pmuxcr,
167 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
168 break;
169 case 1:
170 clrsetbits_be32(&machine_data->guts->pmuxcr,
171 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_LA);
172 break;
173 }
174
175 return 0;
176 }
177
178 /**
179 * mpc8610_hpcd_ops: ASoC fabric driver operations
180 */
181 static struct snd_soc_ops mpc8610_hpcd_ops = {
182 .startup = mpc8610_hpcd_startup,
183 };
184
185 /**
186 * mpc8610_hpcd_machine: ASoC machine data
187 */
188 static struct snd_soc_card mpc8610_hpcd_machine = {
189 .probe = mpc8610_hpcd_machine_probe,
190 .remove = mpc8610_hpcd_machine_remove,
191 .name = "MPC8610 HPCD",
192 .num_links = 1,
193 };
194
195 /**
196 * mpc8610_hpcd_probe: OF probe function for the fabric driver
197 *
198 * This function gets called when an SSI node is found in the device tree.
199 *
200 * Although this is a fabric driver, the SSI node is the "master" node with
201 * respect to audio hardware connections. Therefore, we create a new ASoC
202 * device for each new SSI node that has a codec attached.
203 *
204 * FIXME: Currently, we only support one DMA controller, so if there are
205 * multiple SSI nodes with codecs, only the first will be supported.
206 *
207 * FIXME: Even if we did support multiple DMA controllers, we have no
208 * mechanism for assigning DMA controllers and channels to the individual
209 * SSI devices. We also probably aren't compatible with the generic Elo DMA
210 * device driver.
211 */
212 static int mpc8610_hpcd_probe(struct of_device *ofdev,
213 const struct of_device_id *match)
214 {
215 struct device_node *np = ofdev->node;
216 struct device_node *codec_np = NULL;
217 struct device_node *guts_np = NULL;
218 struct device_node *dma_np = NULL;
219 struct device_node *dma_channel_np = NULL;
220 const phandle *codec_ph;
221 const char *sprop;
222 const u32 *iprop;
223 struct resource res;
224 struct platform_device *sound_device = NULL;
225 struct mpc8610_hpcd_data *machine_data;
226 struct fsl_ssi_info ssi_info;
227 struct fsl_dma_info dma_info;
228 int ret = -ENODEV;
229 unsigned int playback_dma_channel;
230 unsigned int capture_dma_channel;
231
232 machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
233 if (!machine_data)
234 return -ENOMEM;
235
236 memset(&ssi_info, 0, sizeof(ssi_info));
237 memset(&dma_info, 0, sizeof(dma_info));
238
239 ssi_info.dev = &ofdev->dev;
240
241 /*
242 * We are only interested in SSIs with a codec phandle in them, so let's
243 * make sure this SSI has one.
244 */
245 codec_ph = of_get_property(np, "codec-handle", NULL);
246 if (!codec_ph)
247 goto error;
248
249 codec_np = of_find_node_by_phandle(*codec_ph);
250 if (!codec_np)
251 goto error;
252
253 /* The MPC8610 HPCD only knows about the CS4270 codec, so reject
254 anything else. */
255 if (!of_device_is_compatible(codec_np, "cirrus,cs4270"))
256 goto error;
257
258 /* Get the device ID */
259 iprop = of_get_property(np, "cell-index", NULL);
260 if (!iprop) {
261 dev_err(&ofdev->dev, "cell-index property not found\n");
262 ret = -EINVAL;
263 goto error;
264 }
265 machine_data->ssi_id = *iprop;
266 ssi_info.id = *iprop;
267
268 /* Get the serial format and clock direction. */
269 sprop = of_get_property(np, "fsl,mode", NULL);
270 if (!sprop) {
271 dev_err(&ofdev->dev, "fsl,mode property not found\n");
272 ret = -EINVAL;
273 goto error;
274 }
275
276 if (strcasecmp(sprop, "i2s-slave") == 0) {
277 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
278 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
279 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
280
281 /*
282 * In i2s-slave mode, the codec has its own clock source, so we
283 * need to get the frequency from the device tree and pass it to
284 * the codec driver.
285 */
286 iprop = of_get_property(codec_np, "clock-frequency", NULL);
287 if (!iprop || !*iprop) {
288 dev_err(&ofdev->dev, "codec bus-frequency property "
289 "is missing or invalid\n");
290 ret = -EINVAL;
291 goto error;
292 }
293 machine_data->clk_frequency = *iprop;
294 } else if (strcasecmp(sprop, "i2s-master") == 0) {
295 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
296 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
297 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
298 } else if (strcasecmp(sprop, "lj-slave") == 0) {
299 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
300 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
301 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
302 } else if (strcasecmp(sprop, "lj-master") == 0) {
303 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
304 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
305 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
306 } else if (strcasecmp(sprop, "rj-slave") == 0) {
307 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
308 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
309 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
310 } else if (strcasecmp(sprop, "rj-master") == 0) {
311 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
312 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
313 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
314 } else if (strcasecmp(sprop, "ac97-slave") == 0) {
315 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
316 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
317 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
318 } else if (strcasecmp(sprop, "ac97-master") == 0) {
319 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
320 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
321 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
322 } else {
323 dev_err(&ofdev->dev,
324 "unrecognized fsl,mode property \"%s\"\n", sprop);
325 ret = -EINVAL;
326 goto error;
327 }
328
329 if (!machine_data->clk_frequency) {
330 dev_err(&ofdev->dev, "unknown clock frequency\n");
331 ret = -EINVAL;
332 goto error;
333 }
334
335 /* Read the SSI information from the device tree */
336 ret = of_address_to_resource(np, 0, &res);
337 if (ret) {
338 dev_err(&ofdev->dev, "could not obtain SSI address\n");
339 goto error;
340 }
341 if (!res.start) {
342 dev_err(&ofdev->dev, "invalid SSI address\n");
343 goto error;
344 }
345 ssi_info.ssi_phys = res.start;
346
347 machine_data->ssi = ioremap(ssi_info.ssi_phys, sizeof(struct ccsr_ssi));
348 if (!machine_data->ssi) {
349 dev_err(&ofdev->dev, "could not map SSI address %x\n",
350 ssi_info.ssi_phys);
351 ret = -EINVAL;
352 goto error;
353 }
354 ssi_info.ssi = machine_data->ssi;
355
356
357 /* Get the IRQ of the SSI */
358 machine_data->ssi_irq = irq_of_parse_and_map(np, 0);
359 if (!machine_data->ssi_irq) {
360 dev_err(&ofdev->dev, "could not get SSI IRQ\n");
361 ret = -EINVAL;
362 goto error;
363 }
364 ssi_info.irq = machine_data->ssi_irq;
365
366
367 /* Map the global utilities registers. */
368 guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
369 if (!guts_np) {
370 dev_err(&ofdev->dev, "could not obtain address of GUTS\n");
371 ret = -EINVAL;
372 goto error;
373 }
374 machine_data->guts = of_iomap(guts_np, 0);
375 of_node_put(guts_np);
376 if (!machine_data->guts) {
377 dev_err(&ofdev->dev, "could not map GUTS\n");
378 ret = -EINVAL;
379 goto error;
380 }
381
382 /* Find the DMA channels to use. Both SSIs need to use the same DMA
383 * controller, so let's use DMA#1.
384 */
385 for_each_compatible_node(dma_np, NULL, "fsl,mpc8610-dma") {
386 iprop = of_get_property(dma_np, "cell-index", NULL);
387 if (iprop && (*iprop == 0)) {
388 of_node_put(dma_np);
389 break;
390 }
391 }
392 if (!dma_np) {
393 dev_err(&ofdev->dev, "could not find DMA node\n");
394 ret = -EINVAL;
395 goto error;
396 }
397 machine_data->dma_id = *iprop;
398
399 /* SSI1 needs to use DMA Channels 0 and 1, and SSI2 needs to use DMA
400 * channels 2 and 3. This is just how the MPC8610 is wired
401 * internally.
402 */
403 playback_dma_channel = (machine_data->ssi_id == 0) ? 0 : 2;
404 capture_dma_channel = (machine_data->ssi_id == 0) ? 1 : 3;
405
406 /*
407 * Find the DMA channels to use.
408 */
409 while ((dma_channel_np = of_get_next_child(dma_np, dma_channel_np))) {
410 iprop = of_get_property(dma_channel_np, "cell-index", NULL);
411 if (iprop && (*iprop == playback_dma_channel)) {
412 /* dma_channel[0] and dma_irq[0] are for playback */
413 dma_info.dma_channel[0] = of_iomap(dma_channel_np, 0);
414 dma_info.dma_irq[0] =
415 irq_of_parse_and_map(dma_channel_np, 0);
416 machine_data->dma_channel_id[0] = *iprop;
417 continue;
418 }
419 if (iprop && (*iprop == capture_dma_channel)) {
420 /* dma_channel[1] and dma_irq[1] are for capture */
421 dma_info.dma_channel[1] = of_iomap(dma_channel_np, 0);
422 dma_info.dma_irq[1] =
423 irq_of_parse_and_map(dma_channel_np, 0);
424 machine_data->dma_channel_id[1] = *iprop;
425 continue;
426 }
427 }
428 if (!dma_info.dma_channel[0] || !dma_info.dma_channel[1] ||
429 !dma_info.dma_irq[0] || !dma_info.dma_irq[1]) {
430 dev_err(&ofdev->dev, "could not find DMA channels\n");
431 ret = -EINVAL;
432 goto error;
433 }
434
435 dma_info.ssi_stx_phys = ssi_info.ssi_phys +
436 offsetof(struct ccsr_ssi, stx0);
437 dma_info.ssi_srx_phys = ssi_info.ssi_phys +
438 offsetof(struct ccsr_ssi, srx0);
439
440 /* We have the DMA information, so tell the DMA driver what it is */
441 if (!fsl_dma_configure(&dma_info)) {
442 dev_err(&ofdev->dev, "could not instantiate DMA device\n");
443 ret = -EBUSY;
444 goto error;
445 }
446
447 /*
448 * Initialize our DAI data structure. We should probably get this
449 * information from the device tree.
450 */
451 machine_data->dai.name = "CS4270";
452 machine_data->dai.stream_name = "CS4270";
453
454 machine_data->dai.cpu_dai = fsl_ssi_create_dai(&ssi_info);
455 machine_data->dai.codec_dai = &cs4270_dai; /* The codec_dai we want */
456 machine_data->dai.ops = &mpc8610_hpcd_ops;
457
458 mpc8610_hpcd_machine.dai_link = &machine_data->dai;
459
460 /* Allocate a new audio platform device structure */
461 sound_device = platform_device_alloc("soc-audio", -1);
462 if (!sound_device) {
463 dev_err(&ofdev->dev, "platform device allocation failed\n");
464 ret = -ENOMEM;
465 goto error;
466 }
467
468 machine_data->sound_devdata.card = &mpc8610_hpcd_machine;
469 machine_data->sound_devdata.codec_dev = &soc_codec_device_cs4270;
470 machine_data->machine.platform = &fsl_soc_platform;
471
472 sound_device->dev.platform_data = machine_data;
473
474
475 /* Set the platform device and ASoC device to point to each other */
476 platform_set_drvdata(sound_device, &machine_data->sound_devdata);
477
478 machine_data->sound_devdata.dev = &sound_device->dev;
479
480
481 /* Tell ASoC to probe us. This will call mpc8610_hpcd_machine.probe(),
482 if it exists. */
483 ret = platform_device_add(sound_device);
484
485 if (ret) {
486 dev_err(&ofdev->dev, "platform device add failed\n");
487 goto error;
488 }
489
490 dev_set_drvdata(&ofdev->dev, sound_device);
491
492 return 0;
493
494 error:
495 of_node_put(codec_np);
496 of_node_put(guts_np);
497 of_node_put(dma_np);
498 of_node_put(dma_channel_np);
499
500 if (sound_device)
501 platform_device_unregister(sound_device);
502
503 if (machine_data->dai.cpu_dai)
504 fsl_ssi_destroy_dai(machine_data->dai.cpu_dai);
505
506 if (ssi_info.ssi)
507 iounmap(ssi_info.ssi);
508
509 if (ssi_info.irq)
510 irq_dispose_mapping(ssi_info.irq);
511
512 if (dma_info.dma_channel[0])
513 iounmap(dma_info.dma_channel[0]);
514
515 if (dma_info.dma_channel[1])
516 iounmap(dma_info.dma_channel[1]);
517
518 if (dma_info.dma_irq[0])
519 irq_dispose_mapping(dma_info.dma_irq[0]);
520
521 if (dma_info.dma_irq[1])
522 irq_dispose_mapping(dma_info.dma_irq[1]);
523
524 if (machine_data->guts)
525 iounmap(machine_data->guts);
526
527 kfree(machine_data);
528
529 return ret;
530 }
531
532 /**
533 * mpc8610_hpcd_remove: remove the OF device
534 *
535 * This function is called when the OF device is removed.
536 */
537 static int mpc8610_hpcd_remove(struct of_device *ofdev)
538 {
539 struct platform_device *sound_device = dev_get_drvdata(&ofdev->dev);
540 struct mpc8610_hpcd_data *machine_data =
541 sound_device->dev.platform_data;
542
543 platform_device_unregister(sound_device);
544
545 if (machine_data->dai.cpu_dai)
546 fsl_ssi_destroy_dai(machine_data->dai.cpu_dai);
547
548 if (machine_data->ssi)
549 iounmap(machine_data->ssi);
550
551 if (machine_data->dma[0])
552 iounmap(machine_data->dma[0]);
553
554 if (machine_data->dma[1])
555 iounmap(machine_data->dma[1]);
556
557 if (machine_data->dma_irq[0])
558 irq_dispose_mapping(machine_data->dma_irq[0]);
559
560 if (machine_data->dma_irq[1])
561 irq_dispose_mapping(machine_data->dma_irq[1]);
562
563 if (machine_data->guts)
564 iounmap(machine_data->guts);
565
566 kfree(machine_data);
567 sound_device->dev.platform_data = NULL;
568
569 dev_set_drvdata(&ofdev->dev, NULL);
570
571 return 0;
572 }
573
574 static struct of_device_id mpc8610_hpcd_match[] = {
575 {
576 .compatible = "fsl,mpc8610-ssi",
577 },
578 {}
579 };
580 MODULE_DEVICE_TABLE(of, mpc8610_hpcd_match);
581
582 static struct of_platform_driver mpc8610_hpcd_of_driver = {
583 .owner = THIS_MODULE,
584 .name = "mpc8610_hpcd",
585 .match_table = mpc8610_hpcd_match,
586 .probe = mpc8610_hpcd_probe,
587 .remove = mpc8610_hpcd_remove,
588 };
589
590 /**
591 * mpc8610_hpcd_init: fabric driver initialization.
592 *
593 * This function is called when this module is loaded.
594 */
595 static int __init mpc8610_hpcd_init(void)
596 {
597 int ret;
598
599 printk(KERN_INFO "Freescale MPC8610 HPCD ALSA SoC fabric driver\n");
600
601 ret = of_register_platform_driver(&mpc8610_hpcd_of_driver);
602
603 if (ret)
604 printk(KERN_ERR
605 "mpc8610-hpcd: failed to register platform driver\n");
606
607 return ret;
608 }
609
610 /**
611 * mpc8610_hpcd_exit: fabric driver exit
612 *
613 * This function is called when this driver is unloaded.
614 */
615 static void __exit mpc8610_hpcd_exit(void)
616 {
617 of_unregister_platform_driver(&mpc8610_hpcd_of_driver);
618 }
619
620 module_init(mpc8610_hpcd_init);
621 module_exit(mpc8610_hpcd_exit);
622
623 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
624 MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC fabric driver");
625 MODULE_LICENSE("GPL");