2 * Freescale MPC8610HPCD ALSA SoC Machine driver
4 * Author: Timur Tabi <timur@freescale.com>
6 * Copyright 2007-2010 Freescale Semiconductor, Inc.
8 * This file is licensed under the terms of the GNU General Public License
9 * version 2. This program is licensed "as is" without any warranty of any
10 * kind, whether express or implied.
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>
23 /* There's only one global utilities register */
24 static phys_addr_t guts_phys
;
26 #define DAI_NAME_SIZE 32
29 * mpc8610_hpcd_data: machine-specific ASoC device data
31 * This structure contains data for a single sound platform device on an
32 * MPC8610 HPCD. Some of the data is taken from the device tree.
34 struct 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
; /* 0 = SSI1, 1 = SSI2, etc */
42 unsigned int dma_id
[2]; /* 0 = DMA1, 1 = DMA2, etc */
43 unsigned int dma_channel_id
[2]; /* 0 = ch 0, 1 = ch 1, etc*/
44 char codec_dai_name
[DAI_NAME_SIZE
];
45 char codec_name
[DAI_NAME_SIZE
];
46 char platform_name
[2][DAI_NAME_SIZE
]; /* One for each DMA channel */
50 * mpc8610_hpcd_machine_probe: initialize the board
52 * This function is used to initialize the board-specific hardware.
54 * Here we program the DMACR and PMUXCR registers.
56 static int mpc8610_hpcd_machine_probe(struct snd_soc_card
*card
)
58 struct mpc8610_hpcd_data
*machine_data
=
59 container_of(card
, struct mpc8610_hpcd_data
, card
);
60 struct ccsr_guts_86xx __iomem
*guts
;
62 guts
= ioremap(guts_phys
, sizeof(struct ccsr_guts_86xx
));
64 dev_err(card
->dev
, "could not map global utilities\n");
68 /* Program the signal routing between the SSI and the DMA */
69 guts_set_dmacr(guts
, machine_data
->dma_id
[0],
70 machine_data
->dma_channel_id
[0],
71 CCSR_GUTS_DMACR_DEV_SSI
);
72 guts_set_dmacr(guts
, machine_data
->dma_id
[1],
73 machine_data
->dma_channel_id
[1],
74 CCSR_GUTS_DMACR_DEV_SSI
);
76 guts_set_pmuxcr_dma(guts
, machine_data
->dma_id
[0],
77 machine_data
->dma_channel_id
[0], 0);
78 guts_set_pmuxcr_dma(guts
, machine_data
->dma_id
[1],
79 machine_data
->dma_channel_id
[1], 0);
81 switch (machine_data
->ssi_id
) {
83 clrsetbits_be32(&guts
->pmuxcr
,
84 CCSR_GUTS_PMUXCR_SSI1_MASK
, CCSR_GUTS_PMUXCR_SSI1_SSI
);
87 clrsetbits_be32(&guts
->pmuxcr
,
88 CCSR_GUTS_PMUXCR_SSI2_MASK
, CCSR_GUTS_PMUXCR_SSI2_SSI
);
98 * mpc8610_hpcd_startup: program the board with various hardware parameters
100 * This function takes board-specific information, like clock frequencies
101 * and serial data formats, and passes that information to the codec and
104 static int mpc8610_hpcd_startup(struct snd_pcm_substream
*substream
)
106 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
107 struct mpc8610_hpcd_data
*machine_data
=
108 container_of(rtd
->card
, struct mpc8610_hpcd_data
, card
);
109 struct device
*dev
= rtd
->card
->dev
;
112 /* Tell the codec driver what the serial protocol is. */
113 ret
= snd_soc_dai_set_fmt(rtd
->codec_dai
, machine_data
->dai_format
);
115 dev_err(dev
, "could not set codec driver audio format\n");
120 * Tell the codec driver what the MCLK frequency is, and whether it's
123 ret
= snd_soc_dai_set_sysclk(rtd
->codec_dai
, 0,
124 machine_data
->clk_frequency
,
125 machine_data
->codec_clk_direction
);
127 dev_err(dev
, "could not set codec driver clock params\n");
135 * mpc8610_hpcd_machine_remove: Remove the sound device
137 * This function is called to remove the sound device for one SSI. We
138 * de-program the DMACR and PMUXCR register.
140 static int mpc8610_hpcd_machine_remove(struct snd_soc_card
*card
)
142 struct mpc8610_hpcd_data
*machine_data
=
143 container_of(card
, struct mpc8610_hpcd_data
, card
);
144 struct ccsr_guts_86xx __iomem
*guts
;
146 guts
= ioremap(guts_phys
, sizeof(struct ccsr_guts_86xx
));
148 dev_err(card
->dev
, "could not map global utilities\n");
152 /* Restore the signal routing */
154 guts_set_dmacr(guts
, machine_data
->dma_id
[0],
155 machine_data
->dma_channel_id
[0], 0);
156 guts_set_dmacr(guts
, machine_data
->dma_id
[1],
157 machine_data
->dma_channel_id
[1], 0);
159 switch (machine_data
->ssi_id
) {
161 clrsetbits_be32(&guts
->pmuxcr
,
162 CCSR_GUTS_PMUXCR_SSI1_MASK
, CCSR_GUTS_PMUXCR_SSI1_LA
);
165 clrsetbits_be32(&guts
->pmuxcr
,
166 CCSR_GUTS_PMUXCR_SSI2_MASK
, CCSR_GUTS_PMUXCR_SSI2_LA
);
176 * mpc8610_hpcd_ops: ASoC machine driver operations
178 static struct snd_soc_ops mpc8610_hpcd_ops
= {
179 .startup
= mpc8610_hpcd_startup
,
183 * get_node_by_phandle_name - get a node by its phandle name
185 * This function takes a node, the name of a property in that node, and a
186 * compatible string. Assuming the property is a phandle to another node,
187 * it returns that node, (optionally) if that node is compatible.
189 * If the property is not a phandle, or the node it points to is not compatible
190 * with the specific string, then NULL is returned.
192 static struct device_node
*get_node_by_phandle_name(struct device_node
*np
,
194 const char *compatible
)
199 ph
= of_get_property(np
, name
, &len
);
200 if (!ph
|| (len
!= sizeof(phandle
)))
203 np
= of_find_node_by_phandle(*ph
);
207 if (compatible
&& !of_device_is_compatible(np
, compatible
)) {
216 * get_parent_cell_index -- return the cell-index of the parent of a node
218 * Return the value of the cell-index property of the parent of the given
219 * node. This is used for DMA channel nodes that need to know the DMA ID
220 * of the controller they are on.
222 static int get_parent_cell_index(struct device_node
*np
)
224 struct device_node
*parent
= of_get_parent(np
);
230 iprop
= of_get_property(parent
, "cell-index", NULL
);
240 * codec_node_dev_name - determine the dev_name for a codec node
242 * This function determines the dev_name for an I2C node. This is the name
243 * that would be returned by dev_name() if this device_node were part of a
244 * 'struct device' It's ugly and hackish, but it works.
246 * The dev_name for such devices include the bus number and I2C address. For
247 * example, "cs4270-codec.0-004f".
249 static int codec_node_dev_name(struct device_node
*np
, char *buf
, size_t len
)
253 char temp
[DAI_NAME_SIZE
];
255 of_modalias_node(np
, temp
, DAI_NAME_SIZE
);
257 iprop
= of_get_property(np
, "reg", NULL
);
263 bus
= get_parent_cell_index(np
);
267 snprintf(buf
, len
, "%s-codec.%u-%04x", temp
, bus
, addr
);
272 static int get_dma_channel(struct device_node
*ssi_np
,
273 const char *compatible
,
274 struct snd_soc_dai_link
*dai
,
275 unsigned int *dma_channel_id
,
276 unsigned int *dma_id
)
279 struct device_node
*dma_channel_np
;
283 dma_channel_np
= get_node_by_phandle_name(ssi_np
, compatible
,
284 "fsl,ssi-dma-channel");
288 /* Determine the dev_name for the device_node. This code mimics the
289 * behavior of of_device_make_bus_id(). We need this because ASoC uses
290 * the dev_name() of the device to match the platform (DMA) device with
291 * the CPU (SSI) device. It's all ugly and hackish, but it works (for
294 * dai->platform name should already point to an allocated buffer.
296 ret
= of_address_to_resource(dma_channel_np
, 0, &res
);
299 snprintf((char *)dai
->platform_name
, DAI_NAME_SIZE
, "%llx.%s",
300 (unsigned long long) res
.start
, dma_channel_np
->name
);
302 iprop
= of_get_property(dma_channel_np
, "cell-index", NULL
);
304 of_node_put(dma_channel_np
);
308 *dma_channel_id
= *iprop
;
309 *dma_id
= get_parent_cell_index(dma_channel_np
);
310 of_node_put(dma_channel_np
);
316 * mpc8610_hpcd_probe: platform probe function for the machine driver
318 * Although this is a machine driver, the SSI node is the "master" node with
319 * respect to audio hardware connections. Therefore, we create a new ASoC
320 * device for each new SSI node that has a codec attached.
322 static int mpc8610_hpcd_probe(struct platform_device
*pdev
)
324 struct device
*dev
= pdev
->dev
.parent
;
325 /* ssi_pdev is the platform device for the SSI node that probed us */
326 struct platform_device
*ssi_pdev
=
327 container_of(dev
, struct platform_device
, dev
);
328 struct device_node
*np
= ssi_pdev
->dev
.of_node
;
329 struct device_node
*codec_np
= NULL
;
330 struct platform_device
*sound_device
= NULL
;
331 struct mpc8610_hpcd_data
*machine_data
;
336 /* We are only interested in SSIs with a codec phandle in them,
337 * so let's make sure this SSI has one. The MPC8610 HPCD only
338 * knows about the CS4270 codec, so reject anything else.
340 codec_np
= get_node_by_phandle_name(np
, "codec-handle",
343 dev_err(dev
, "invalid codec node\n");
347 machine_data
= kzalloc(sizeof(struct mpc8610_hpcd_data
), GFP_KERNEL
);
351 machine_data
->dai
[0].cpu_dai_name
= dev_name(&ssi_pdev
->dev
);
352 machine_data
->dai
[0].ops
= &mpc8610_hpcd_ops
;
354 /* Determine the codec name, it will be used as the codec DAI name */
355 ret
= codec_node_dev_name(codec_np
, machine_data
->codec_name
,
358 dev_err(&pdev
->dev
, "invalid codec node %s\n",
359 codec_np
->full_name
);
363 machine_data
->dai
[0].codec_name
= machine_data
->codec_name
;
365 /* The DAI name from the codec (snd_soc_dai_driver.name) */
366 machine_data
->dai
[0].codec_dai_name
= "cs4270-hifi";
368 /* We register two DAIs per SSI, one for playback and the other for
369 * capture. Currently, we only support codecs that have one DAI for
370 * both playback and capture.
372 memcpy(&machine_data
->dai
[1], &machine_data
->dai
[0],
373 sizeof(struct snd_soc_dai_link
));
375 /* Get the device ID */
376 iprop
= of_get_property(np
, "cell-index", NULL
);
378 dev_err(&pdev
->dev
, "cell-index property not found\n");
382 machine_data
->ssi_id
= *iprop
;
384 /* Get the serial format and clock direction. */
385 sprop
= of_get_property(np
, "fsl,mode", NULL
);
387 dev_err(&pdev
->dev
, "fsl,mode property not found\n");
392 if (strcasecmp(sprop
, "i2s-slave") == 0) {
393 machine_data
->dai_format
= SND_SOC_DAIFMT_I2S
;
394 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
395 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
397 /* In i2s-slave mode, the codec has its own clock source, so we
398 * need to get the frequency from the device tree and pass it to
401 iprop
= of_get_property(codec_np
, "clock-frequency", NULL
);
402 if (!iprop
|| !*iprop
) {
403 dev_err(&pdev
->dev
, "codec bus-frequency "
404 "property is missing or invalid\n");
408 machine_data
->clk_frequency
= *iprop
;
409 } else if (strcasecmp(sprop
, "i2s-master") == 0) {
410 machine_data
->dai_format
= SND_SOC_DAIFMT_I2S
;
411 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
412 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
413 } else if (strcasecmp(sprop
, "lj-slave") == 0) {
414 machine_data
->dai_format
= SND_SOC_DAIFMT_LEFT_J
;
415 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
416 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
417 } else if (strcasecmp(sprop
, "lj-master") == 0) {
418 machine_data
->dai_format
= SND_SOC_DAIFMT_LEFT_J
;
419 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
420 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
421 } else if (strcasecmp(sprop
, "rj-slave") == 0) {
422 machine_data
->dai_format
= SND_SOC_DAIFMT_RIGHT_J
;
423 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
424 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
425 } else if (strcasecmp(sprop
, "rj-master") == 0) {
426 machine_data
->dai_format
= SND_SOC_DAIFMT_RIGHT_J
;
427 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
428 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
429 } else if (strcasecmp(sprop
, "ac97-slave") == 0) {
430 machine_data
->dai_format
= SND_SOC_DAIFMT_AC97
;
431 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
432 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
433 } else if (strcasecmp(sprop
, "ac97-master") == 0) {
434 machine_data
->dai_format
= SND_SOC_DAIFMT_AC97
;
435 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
436 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
439 "unrecognized fsl,mode property '%s'\n", sprop
);
444 if (!machine_data
->clk_frequency
) {
445 dev_err(&pdev
->dev
, "unknown clock frequency\n");
450 /* Find the playback DMA channel to use. */
451 machine_data
->dai
[0].platform_name
= machine_data
->platform_name
[0];
452 ret
= get_dma_channel(np
, "fsl,playback-dma", &machine_data
->dai
[0],
453 &machine_data
->dma_channel_id
[0],
454 &machine_data
->dma_id
[0]);
456 dev_err(&pdev
->dev
, "missing/invalid playback DMA phandle\n");
460 /* Find the capture DMA channel to use. */
461 machine_data
->dai
[1].platform_name
= machine_data
->platform_name
[1];
462 ret
= get_dma_channel(np
, "fsl,capture-dma", &machine_data
->dai
[1],
463 &machine_data
->dma_channel_id
[1],
464 &machine_data
->dma_id
[1]);
466 dev_err(&pdev
->dev
, "missing/invalid capture DMA phandle\n");
470 /* Initialize our DAI data structure. */
471 machine_data
->dai
[0].stream_name
= "playback";
472 machine_data
->dai
[1].stream_name
= "capture";
473 machine_data
->dai
[0].name
= machine_data
->dai
[0].stream_name
;
474 machine_data
->dai
[1].name
= machine_data
->dai
[1].stream_name
;
476 machine_data
->card
.probe
= mpc8610_hpcd_machine_probe
;
477 machine_data
->card
.remove
= mpc8610_hpcd_machine_remove
;
478 machine_data
->card
.name
= pdev
->name
; /* The platform driver name */
479 machine_data
->card
.num_links
= 2;
480 machine_data
->card
.dai_link
= machine_data
->dai
;
482 /* Allocate a new audio platform device structure */
483 sound_device
= platform_device_alloc("soc-audio", -1);
485 dev_err(&pdev
->dev
, "platform device alloc failed\n");
490 /* Associate the card data with the sound device */
491 platform_set_drvdata(sound_device
, &machine_data
->card
);
493 /* Register with ASoC */
494 ret
= platform_device_add(sound_device
);
496 dev_err(&pdev
->dev
, "platform device add failed\n");
499 dev_set_drvdata(&pdev
->dev
, sound_device
);
501 of_node_put(codec_np
);
506 of_node_put(codec_np
);
509 platform_device_unregister(sound_device
);
517 * mpc8610_hpcd_remove: remove the platform device
519 * This function is called when the platform device is removed.
521 static int __devexit
mpc8610_hpcd_remove(struct platform_device
*pdev
)
523 struct platform_device
*sound_device
= dev_get_drvdata(&pdev
->dev
);
524 struct snd_soc_card
*card
= platform_get_drvdata(sound_device
);
525 struct mpc8610_hpcd_data
*machine_data
=
526 container_of(card
, struct mpc8610_hpcd_data
, card
);
528 platform_device_unregister(sound_device
);
531 sound_device
->dev
.platform_data
= NULL
;
533 dev_set_drvdata(&pdev
->dev
, NULL
);
538 static struct platform_driver mpc8610_hpcd_driver
= {
539 .probe
= mpc8610_hpcd_probe
,
540 .remove
= __devexit_p(mpc8610_hpcd_remove
),
542 /* The name must match the 'model' property in the device tree,
543 * in lowercase letters.
545 .name
= "snd-soc-mpc8610hpcd",
546 .owner
= THIS_MODULE
,
551 * mpc8610_hpcd_init: machine driver initialization.
553 * This function is called when this module is loaded.
555 static int __init
mpc8610_hpcd_init(void)
557 struct device_node
*guts_np
;
560 pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
562 /* Get the physical address of the global utilities registers */
563 guts_np
= of_find_compatible_node(NULL
, NULL
, "fsl,mpc8610-guts");
564 if (of_address_to_resource(guts_np
, 0, &res
)) {
565 pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
568 guts_phys
= res
.start
;
570 return platform_driver_register(&mpc8610_hpcd_driver
);
574 * mpc8610_hpcd_exit: machine driver exit
576 * This function is called when this driver is unloaded.
578 static void __exit
mpc8610_hpcd_exit(void)
580 platform_driver_unregister(&mpc8610_hpcd_driver
);
583 module_init(mpc8610_hpcd_init
);
584 module_exit(mpc8610_hpcd_exit
);
586 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
587 MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
588 MODULE_LICENSE("GPL v2");