2 * Freescale MPC8610HPCD ALSA SoC Fabric driver
4 * Author: Timur Tabi <timur@freescale.com>
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
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/of_device.h>
16 #include <linux/of_platform.h>
17 #include <sound/soc.h>
18 #include <asm/immap_86xx.h>
20 #include "../codecs/cs4270.h"
25 * mpc8610_hpcd_data: fabric-specific ASoC device data
27 * This structure contains data for a single sound platform device on an
28 * MPC8610 HPCD. Some of the data is taken from the device tree.
30 struct mpc8610_hpcd_data
{
31 struct snd_soc_device sound_devdata
;
32 struct snd_soc_dai_link dai
;
33 struct snd_soc_card machine
;
34 unsigned int dai_format
;
35 unsigned int codec_clk_direction
;
36 unsigned int cpu_clk_direction
;
37 unsigned int clk_frequency
;
38 struct ccsr_guts __iomem
*guts
;
39 struct ccsr_ssi __iomem
*ssi
;
40 unsigned int ssi_id
; /* 0 = SSI1, 1 = SSI2, etc */
42 unsigned int dma_id
; /* 0 = DMA1, 1 = DMA2, etc */
43 unsigned int dma_irq
[2];
44 struct ccsr_dma_channel __iomem
*dma
[2];
45 unsigned int dma_channel_id
[2]; /* 0 = ch 0, 1 = ch 1, etc*/
49 * mpc8610_hpcd_machine_probe: initalize the board
51 * This function is called when platform_device_add() is called. It is used
52 * to initialize the board-specific hardware.
54 * Here we program the DMACR and PMUXCR registers.
56 static int mpc8610_hpcd_machine_probe(struct platform_device
*sound_device
)
58 struct mpc8610_hpcd_data
*machine_data
=
59 sound_device
->dev
.platform_data
;
61 /* Program the signal routing between the SSI and the DMA */
62 guts_set_dmacr(machine_data
->guts
, machine_data
->dma_id
,
63 machine_data
->dma_channel_id
[0], CCSR_GUTS_DMACR_DEV_SSI
);
64 guts_set_dmacr(machine_data
->guts
, machine_data
->dma_id
,
65 machine_data
->dma_channel_id
[1], CCSR_GUTS_DMACR_DEV_SSI
);
67 guts_set_pmuxcr_dma(machine_data
->guts
, machine_data
->dma_id
,
68 machine_data
->dma_channel_id
[0], 0);
69 guts_set_pmuxcr_dma(machine_data
->guts
, machine_data
->dma_id
,
70 machine_data
->dma_channel_id
[1], 0);
72 switch (machine_data
->ssi_id
) {
74 clrsetbits_be32(&machine_data
->guts
->pmuxcr
,
75 CCSR_GUTS_PMUXCR_SSI1_MASK
, CCSR_GUTS_PMUXCR_SSI1_SSI
);
78 clrsetbits_be32(&machine_data
->guts
->pmuxcr
,
79 CCSR_GUTS_PMUXCR_SSI2_MASK
, CCSR_GUTS_PMUXCR_SSI2_SSI
);
87 * mpc8610_hpcd_startup: program the board with various hardware parameters
89 * This function takes board-specific information, like clock frequencies
90 * and serial data formats, and passes that information to the codec and
93 static int mpc8610_hpcd_startup(struct snd_pcm_substream
*substream
)
95 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
96 struct snd_soc_dai
*codec_dai
= rtd
->dai
->codec_dai
;
97 struct snd_soc_dai
*cpu_dai
= rtd
->dai
->cpu_dai
;
98 struct mpc8610_hpcd_data
*machine_data
=
99 rtd
->socdev
->dev
->platform_data
;
102 /* Tell the CPU driver what the serial protocol is. */
103 ret
= snd_soc_dai_set_fmt(cpu_dai
, machine_data
->dai_format
);
105 dev_err(substream
->pcm
->card
->dev
,
106 "could not set CPU driver audio format\n");
110 /* Tell the codec driver what the serial protocol is. */
111 ret
= snd_soc_dai_set_fmt(codec_dai
, machine_data
->dai_format
);
113 dev_err(substream
->pcm
->card
->dev
,
114 "could not set codec driver audio format\n");
119 * Tell the CPU driver what the clock frequency is, and whether it's a
122 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0,
123 machine_data
->clk_frequency
,
124 machine_data
->cpu_clk_direction
);
126 dev_err(substream
->pcm
->card
->dev
,
127 "could not set CPU driver clock parameters\n");
132 * Tell the codec driver what the MCLK frequency is, and whether it's
135 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0,
136 machine_data
->clk_frequency
,
137 machine_data
->codec_clk_direction
);
139 dev_err(substream
->pcm
->card
->dev
,
140 "could not set codec driver clock params\n");
148 * mpc8610_hpcd_machine_remove: Remove the sound device
150 * This function is called to remove the sound device for one SSI. We
151 * de-program the DMACR and PMUXCR register.
153 int mpc8610_hpcd_machine_remove(struct platform_device
*sound_device
)
155 struct mpc8610_hpcd_data
*machine_data
=
156 sound_device
->dev
.platform_data
;
158 /* Restore the signal routing */
160 guts_set_dmacr(machine_data
->guts
, machine_data
->dma_id
,
161 machine_data
->dma_channel_id
[0], 0);
162 guts_set_dmacr(machine_data
->guts
, machine_data
->dma_id
,
163 machine_data
->dma_channel_id
[1], 0);
165 switch (machine_data
->ssi_id
) {
167 clrsetbits_be32(&machine_data
->guts
->pmuxcr
,
168 CCSR_GUTS_PMUXCR_SSI1_MASK
, CCSR_GUTS_PMUXCR_SSI1_LA
);
171 clrsetbits_be32(&machine_data
->guts
->pmuxcr
,
172 CCSR_GUTS_PMUXCR_SSI2_MASK
, CCSR_GUTS_PMUXCR_SSI2_LA
);
180 * mpc8610_hpcd_ops: ASoC fabric driver operations
182 static struct snd_soc_ops mpc8610_hpcd_ops
= {
183 .startup
= mpc8610_hpcd_startup
,
187 * mpc8610_hpcd_probe: OF probe function for the fabric driver
189 * This function gets called when an SSI node is found in the device tree.
191 * Although this is a fabric driver, the SSI node is the "master" node with
192 * respect to audio hardware connections. Therefore, we create a new ASoC
193 * device for each new SSI node that has a codec attached.
195 * FIXME: Currently, we only support one DMA controller, so if there are
196 * multiple SSI nodes with codecs, only the first will be supported.
198 * FIXME: Even if we did support multiple DMA controllers, we have no
199 * mechanism for assigning DMA controllers and channels to the individual
200 * SSI devices. We also probably aren't compatible with the generic Elo DMA
203 static int mpc8610_hpcd_probe(struct of_device
*ofdev
,
204 const struct of_device_id
*match
)
206 struct device_node
*np
= ofdev
->dev
.of_node
;
207 struct device_node
*codec_np
= NULL
;
208 struct device_node
*guts_np
= NULL
;
209 struct device_node
*dma_np
= NULL
;
210 struct device_node
*dma_channel_np
= NULL
;
211 const phandle
*codec_ph
;
215 struct platform_device
*sound_device
= NULL
;
216 struct mpc8610_hpcd_data
*machine_data
;
217 struct fsl_ssi_info ssi_info
;
218 struct fsl_dma_info dma_info
;
220 unsigned int playback_dma_channel
;
221 unsigned int capture_dma_channel
;
223 machine_data
= kzalloc(sizeof(struct mpc8610_hpcd_data
), GFP_KERNEL
);
227 memset(&ssi_info
, 0, sizeof(ssi_info
));
228 memset(&dma_info
, 0, sizeof(dma_info
));
230 ssi_info
.dev
= &ofdev
->dev
;
233 * We are only interested in SSIs with a codec phandle in them, so let's
234 * make sure this SSI has one.
236 codec_ph
= of_get_property(np
, "codec-handle", NULL
);
240 codec_np
= of_find_node_by_phandle(*codec_ph
);
244 /* The MPC8610 HPCD only knows about the CS4270 codec, so reject
246 if (!of_device_is_compatible(codec_np
, "cirrus,cs4270"))
249 /* Get the device ID */
250 iprop
= of_get_property(np
, "cell-index", NULL
);
252 dev_err(&ofdev
->dev
, "cell-index property not found\n");
256 machine_data
->ssi_id
= *iprop
;
257 ssi_info
.id
= *iprop
;
259 /* Get the serial format and clock direction. */
260 sprop
= of_get_property(np
, "fsl,mode", NULL
);
262 dev_err(&ofdev
->dev
, "fsl,mode property not found\n");
267 if (strcasecmp(sprop
, "i2s-slave") == 0) {
268 machine_data
->dai_format
= SND_SOC_DAIFMT_I2S
;
269 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
270 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
273 * In i2s-slave mode, the codec has its own clock source, so we
274 * need to get the frequency from the device tree and pass it to
277 iprop
= of_get_property(codec_np
, "clock-frequency", NULL
);
278 if (!iprop
|| !*iprop
) {
279 dev_err(&ofdev
->dev
, "codec bus-frequency property "
280 "is missing or invalid\n");
284 machine_data
->clk_frequency
= *iprop
;
285 } else if (strcasecmp(sprop
, "i2s-master") == 0) {
286 machine_data
->dai_format
= SND_SOC_DAIFMT_I2S
;
287 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
288 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
289 } else if (strcasecmp(sprop
, "lj-slave") == 0) {
290 machine_data
->dai_format
= SND_SOC_DAIFMT_LEFT_J
;
291 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
292 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
293 } else if (strcasecmp(sprop
, "lj-master") == 0) {
294 machine_data
->dai_format
= SND_SOC_DAIFMT_LEFT_J
;
295 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
296 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
297 } else if (strcasecmp(sprop
, "rj-slave") == 0) {
298 machine_data
->dai_format
= SND_SOC_DAIFMT_RIGHT_J
;
299 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
300 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
301 } else if (strcasecmp(sprop
, "rj-master") == 0) {
302 machine_data
->dai_format
= SND_SOC_DAIFMT_RIGHT_J
;
303 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
304 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
305 } else if (strcasecmp(sprop
, "ac97-slave") == 0) {
306 machine_data
->dai_format
= SND_SOC_DAIFMT_AC97
;
307 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
308 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
309 } else if (strcasecmp(sprop
, "ac97-master") == 0) {
310 machine_data
->dai_format
= SND_SOC_DAIFMT_AC97
;
311 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
312 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
315 "unrecognized fsl,mode property \"%s\"\n", sprop
);
320 if (!machine_data
->clk_frequency
) {
321 dev_err(&ofdev
->dev
, "unknown clock frequency\n");
326 /* Read the SSI information from the device tree */
327 ret
= of_address_to_resource(np
, 0, &res
);
329 dev_err(&ofdev
->dev
, "could not obtain SSI address\n");
333 dev_err(&ofdev
->dev
, "invalid SSI address\n");
336 ssi_info
.ssi_phys
= res
.start
;
338 machine_data
->ssi
= ioremap(ssi_info
.ssi_phys
, sizeof(struct ccsr_ssi
));
339 if (!machine_data
->ssi
) {
340 dev_err(&ofdev
->dev
, "could not map SSI address %x\n",
345 ssi_info
.ssi
= machine_data
->ssi
;
348 /* Get the IRQ of the SSI */
349 machine_data
->ssi_irq
= irq_of_parse_and_map(np
, 0);
350 if (!machine_data
->ssi_irq
) {
351 dev_err(&ofdev
->dev
, "could not get SSI IRQ\n");
355 ssi_info
.irq
= machine_data
->ssi_irq
;
357 /* Do we want to use asynchronous mode? */
358 ssi_info
.asynchronous
=
359 of_find_property(np
, "fsl,ssi-asynchronous", NULL
) ? 1 : 0;
360 if (ssi_info
.asynchronous
)
361 dev_info(&ofdev
->dev
, "using asynchronous mode\n");
363 /* Map the global utilities registers. */
364 guts_np
= of_find_compatible_node(NULL
, NULL
, "fsl,mpc8610-guts");
366 dev_err(&ofdev
->dev
, "could not obtain address of GUTS\n");
370 machine_data
->guts
= of_iomap(guts_np
, 0);
371 of_node_put(guts_np
);
372 if (!machine_data
->guts
) {
373 dev_err(&ofdev
->dev
, "could not map GUTS\n");
378 /* Find the DMA channels to use. Both SSIs need to use the same DMA
379 * controller, so let's use DMA#1.
381 for_each_compatible_node(dma_np
, NULL
, "fsl,mpc8610-dma") {
382 iprop
= of_get_property(dma_np
, "cell-index", NULL
);
383 if (iprop
&& (*iprop
== 0)) {
389 dev_err(&ofdev
->dev
, "could not find DMA node\n");
393 machine_data
->dma_id
= *iprop
;
395 /* SSI1 needs to use DMA Channels 0 and 1, and SSI2 needs to use DMA
396 * channels 2 and 3. This is just how the MPC8610 is wired
399 playback_dma_channel
= (machine_data
->ssi_id
== 0) ? 0 : 2;
400 capture_dma_channel
= (machine_data
->ssi_id
== 0) ? 1 : 3;
403 * Find the DMA channels to use.
405 while ((dma_channel_np
= of_get_next_child(dma_np
, dma_channel_np
))) {
406 iprop
= of_get_property(dma_channel_np
, "cell-index", NULL
);
407 if (iprop
&& (*iprop
== playback_dma_channel
)) {
408 /* dma_channel[0] and dma_irq[0] are for playback */
409 dma_info
.dma_channel
[0] = of_iomap(dma_channel_np
, 0);
410 dma_info
.dma_irq
[0] =
411 irq_of_parse_and_map(dma_channel_np
, 0);
412 machine_data
->dma_channel_id
[0] = *iprop
;
415 if (iprop
&& (*iprop
== capture_dma_channel
)) {
416 /* dma_channel[1] and dma_irq[1] are for capture */
417 dma_info
.dma_channel
[1] = of_iomap(dma_channel_np
, 0);
418 dma_info
.dma_irq
[1] =
419 irq_of_parse_and_map(dma_channel_np
, 0);
420 machine_data
->dma_channel_id
[1] = *iprop
;
424 if (!dma_info
.dma_channel
[0] || !dma_info
.dma_channel
[1] ||
425 !dma_info
.dma_irq
[0] || !dma_info
.dma_irq
[1]) {
426 dev_err(&ofdev
->dev
, "could not find DMA channels\n");
431 dma_info
.ssi_stx_phys
= ssi_info
.ssi_phys
+
432 offsetof(struct ccsr_ssi
, stx0
);
433 dma_info
.ssi_srx_phys
= ssi_info
.ssi_phys
+
434 offsetof(struct ccsr_ssi
, srx0
);
436 /* We have the DMA information, so tell the DMA driver what it is */
437 if (!fsl_dma_configure(&dma_info
)) {
438 dev_err(&ofdev
->dev
, "could not instantiate DMA device\n");
444 * Initialize our DAI data structure. We should probably get this
445 * information from the device tree.
447 machine_data
->dai
.name
= "CS4270";
448 machine_data
->dai
.stream_name
= "CS4270";
450 machine_data
->dai
.cpu_dai
= fsl_ssi_create_dai(&ssi_info
);
451 machine_data
->dai
.codec_dai
= &cs4270_dai
; /* The codec_dai we want */
452 machine_data
->dai
.ops
= &mpc8610_hpcd_ops
;
454 machine_data
->machine
.probe
= mpc8610_hpcd_machine_probe
;
455 machine_data
->machine
.remove
= mpc8610_hpcd_machine_remove
;
456 machine_data
->machine
.name
= "MPC8610 HPCD";
457 machine_data
->machine
.num_links
= 1;
458 machine_data
->machine
.dai_link
= &machine_data
->dai
;
460 /* Allocate a new audio platform device structure */
461 sound_device
= platform_device_alloc("soc-audio", -1);
463 dev_err(&ofdev
->dev
, "platform device allocation failed\n");
468 machine_data
->sound_devdata
.card
= &machine_data
->machine
;
469 machine_data
->sound_devdata
.codec_dev
= &soc_codec_device_cs4270
;
470 machine_data
->machine
.platform
= &fsl_soc_platform
;
472 sound_device
->dev
.platform_data
= machine_data
;
475 /* Set the platform device and ASoC device to point to each other */
476 platform_set_drvdata(sound_device
, &machine_data
->sound_devdata
);
478 machine_data
->sound_devdata
.dev
= &sound_device
->dev
;
481 /* Tell ASoC to probe us. This will call mpc8610_hpcd_machine.probe(),
483 ret
= platform_device_add(sound_device
);
486 dev_err(&ofdev
->dev
, "platform device add failed\n");
490 dev_set_drvdata(&ofdev
->dev
, sound_device
);
495 of_node_put(codec_np
);
496 of_node_put(guts_np
);
498 of_node_put(dma_channel_np
);
501 platform_device_unregister(sound_device
);
503 if (machine_data
->dai
.cpu_dai
)
504 fsl_ssi_destroy_dai(machine_data
->dai
.cpu_dai
);
507 iounmap(ssi_info
.ssi
);
510 irq_dispose_mapping(ssi_info
.irq
);
512 if (dma_info
.dma_channel
[0])
513 iounmap(dma_info
.dma_channel
[0]);
515 if (dma_info
.dma_channel
[1])
516 iounmap(dma_info
.dma_channel
[1]);
518 if (dma_info
.dma_irq
[0])
519 irq_dispose_mapping(dma_info
.dma_irq
[0]);
521 if (dma_info
.dma_irq
[1])
522 irq_dispose_mapping(dma_info
.dma_irq
[1]);
524 if (machine_data
->guts
)
525 iounmap(machine_data
->guts
);
533 * mpc8610_hpcd_remove: remove the OF device
535 * This function is called when the OF device is removed.
537 static int mpc8610_hpcd_remove(struct of_device
*ofdev
)
539 struct platform_device
*sound_device
= dev_get_drvdata(&ofdev
->dev
);
540 struct mpc8610_hpcd_data
*machine_data
=
541 sound_device
->dev
.platform_data
;
543 platform_device_unregister(sound_device
);
545 if (machine_data
->dai
.cpu_dai
)
546 fsl_ssi_destroy_dai(machine_data
->dai
.cpu_dai
);
548 if (machine_data
->ssi
)
549 iounmap(machine_data
->ssi
);
551 if (machine_data
->dma
[0])
552 iounmap(machine_data
->dma
[0]);
554 if (machine_data
->dma
[1])
555 iounmap(machine_data
->dma
[1]);
557 if (machine_data
->dma_irq
[0])
558 irq_dispose_mapping(machine_data
->dma_irq
[0]);
560 if (machine_data
->dma_irq
[1])
561 irq_dispose_mapping(machine_data
->dma_irq
[1]);
563 if (machine_data
->guts
)
564 iounmap(machine_data
->guts
);
567 sound_device
->dev
.platform_data
= NULL
;
569 dev_set_drvdata(&ofdev
->dev
, NULL
);
574 static struct of_device_id mpc8610_hpcd_match
[] = {
576 .compatible
= "fsl,mpc8610-ssi",
580 MODULE_DEVICE_TABLE(of
, mpc8610_hpcd_match
);
582 static struct of_platform_driver mpc8610_hpcd_of_driver
= {
584 .name
= "mpc8610_hpcd",
585 .owner
= THIS_MODULE
,
586 .of_match_table
= mpc8610_hpcd_match
,
588 .probe
= mpc8610_hpcd_probe
,
589 .remove
= mpc8610_hpcd_remove
,
593 * mpc8610_hpcd_init: fabric driver initialization.
595 * This function is called when this module is loaded.
597 static int __init
mpc8610_hpcd_init(void)
601 printk(KERN_INFO
"Freescale MPC8610 HPCD ALSA SoC fabric driver\n");
603 ret
= of_register_platform_driver(&mpc8610_hpcd_of_driver
);
607 "mpc8610-hpcd: failed to register platform driver\n");
613 * mpc8610_hpcd_exit: fabric driver exit
615 * This function is called when this driver is unloaded.
617 static void __exit
mpc8610_hpcd_exit(void)
619 of_unregister_platform_driver(&mpc8610_hpcd_of_driver
);
622 module_init(mpc8610_hpcd_init
);
623 module_exit(mpc8610_hpcd_exit
);
625 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
626 MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC fabric driver");
627 MODULE_LICENSE("GPL");