1 // SPDX-License-Identifier: GPL-2.0
3 // Freescale MPC8610HPCD ALSA SoC Machine driver
5 // Author: Timur Tabi <timur@freescale.com>
7 // Copyright 2007-2010 Freescale Semiconductor, Inc.
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/fsl/guts.h>
12 #include <linux/of_address.h>
13 #include <linux/of_device.h>
14 #include <linux/slab.h>
15 #include <sound/soc.h>
19 #include "fsl_utils.h"
21 /* There's only one global utilities register */
22 static phys_addr_t guts_phys
;
25 * mpc8610_hpcd_data: machine-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_dai_link dai
[2];
32 struct snd_soc_card card
;
33 unsigned int dai_format
;
34 unsigned int codec_clk_direction
;
35 unsigned int cpu_clk_direction
;
36 unsigned int clk_frequency
;
37 unsigned int ssi_id
; /* 0 = SSI1, 1 = SSI2, etc */
38 unsigned int dma_id
[2]; /* 0 = DMA1, 1 = DMA2, etc */
39 unsigned int dma_channel_id
[2]; /* 0 = ch 0, 1 = ch 1, etc*/
40 char codec_dai_name
[DAI_NAME_SIZE
];
41 char platform_name
[2][DAI_NAME_SIZE
]; /* One for each DMA channel */
45 * mpc8610_hpcd_machine_probe: initialize the board
47 * This function is used to initialize the board-specific hardware.
49 * Here we program the DMACR and PMUXCR registers.
51 static int mpc8610_hpcd_machine_probe(struct snd_soc_card
*card
)
53 struct mpc8610_hpcd_data
*machine_data
=
54 container_of(card
, struct mpc8610_hpcd_data
, card
);
55 struct ccsr_guts __iomem
*guts
;
57 guts
= ioremap(guts_phys
, sizeof(struct ccsr_guts
));
59 dev_err(card
->dev
, "could not map global utilities\n");
63 /* Program the signal routing between the SSI and the DMA */
64 guts_set_dmacr(guts
, machine_data
->dma_id
[0],
65 machine_data
->dma_channel_id
[0],
66 CCSR_GUTS_DMACR_DEV_SSI
);
67 guts_set_dmacr(guts
, machine_data
->dma_id
[1],
68 machine_data
->dma_channel_id
[1],
69 CCSR_GUTS_DMACR_DEV_SSI
);
71 guts_set_pmuxcr_dma(guts
, machine_data
->dma_id
[0],
72 machine_data
->dma_channel_id
[0], 0);
73 guts_set_pmuxcr_dma(guts
, machine_data
->dma_id
[1],
74 machine_data
->dma_channel_id
[1], 0);
76 switch (machine_data
->ssi_id
) {
78 clrsetbits_be32(&guts
->pmuxcr
,
79 CCSR_GUTS_PMUXCR_SSI1_MASK
, CCSR_GUTS_PMUXCR_SSI1_SSI
);
82 clrsetbits_be32(&guts
->pmuxcr
,
83 CCSR_GUTS_PMUXCR_SSI2_MASK
, CCSR_GUTS_PMUXCR_SSI2_SSI
);
93 * mpc8610_hpcd_startup: program the board with various hardware parameters
95 * This function takes board-specific information, like clock frequencies
96 * and serial data formats, and passes that information to the codec and
99 static int mpc8610_hpcd_startup(struct snd_pcm_substream
*substream
)
101 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
102 struct mpc8610_hpcd_data
*machine_data
=
103 container_of(rtd
->card
, struct mpc8610_hpcd_data
, card
);
104 struct device
*dev
= rtd
->card
->dev
;
107 /* Tell the codec driver what the serial protocol is. */
108 ret
= snd_soc_dai_set_fmt(rtd
->codec_dai
, machine_data
->dai_format
);
110 dev_err(dev
, "could not set codec driver audio format\n");
115 * Tell the codec driver what the MCLK frequency is, and whether it's
118 ret
= snd_soc_dai_set_sysclk(rtd
->codec_dai
, 0,
119 machine_data
->clk_frequency
,
120 machine_data
->codec_clk_direction
);
122 dev_err(dev
, "could not set codec driver clock params\n");
130 * mpc8610_hpcd_machine_remove: Remove the sound device
132 * This function is called to remove the sound device for one SSI. We
133 * de-program the DMACR and PMUXCR register.
135 static int mpc8610_hpcd_machine_remove(struct snd_soc_card
*card
)
137 struct mpc8610_hpcd_data
*machine_data
=
138 container_of(card
, struct mpc8610_hpcd_data
, card
);
139 struct ccsr_guts __iomem
*guts
;
141 guts
= ioremap(guts_phys
, sizeof(struct ccsr_guts
));
143 dev_err(card
->dev
, "could not map global utilities\n");
147 /* Restore the signal routing */
149 guts_set_dmacr(guts
, machine_data
->dma_id
[0],
150 machine_data
->dma_channel_id
[0], 0);
151 guts_set_dmacr(guts
, machine_data
->dma_id
[1],
152 machine_data
->dma_channel_id
[1], 0);
154 switch (machine_data
->ssi_id
) {
156 clrsetbits_be32(&guts
->pmuxcr
,
157 CCSR_GUTS_PMUXCR_SSI1_MASK
, CCSR_GUTS_PMUXCR_SSI1_LA
);
160 clrsetbits_be32(&guts
->pmuxcr
,
161 CCSR_GUTS_PMUXCR_SSI2_MASK
, CCSR_GUTS_PMUXCR_SSI2_LA
);
171 * mpc8610_hpcd_ops: ASoC machine driver operations
173 static const struct snd_soc_ops mpc8610_hpcd_ops
= {
174 .startup
= mpc8610_hpcd_startup
,
178 * mpc8610_hpcd_probe: platform probe function for the machine driver
180 * Although this is a machine driver, the SSI node is the "master" node with
181 * respect to audio hardware connections. Therefore, we create a new ASoC
182 * device for each new SSI node that has a codec attached.
184 static int mpc8610_hpcd_probe(struct platform_device
*pdev
)
186 struct device
*dev
= pdev
->dev
.parent
;
187 /* ssi_pdev is the platform device for the SSI node that probed us */
188 struct platform_device
*ssi_pdev
= to_platform_device(dev
);
189 struct device_node
*np
= ssi_pdev
->dev
.of_node
;
190 struct device_node
*codec_np
= NULL
;
191 struct mpc8610_hpcd_data
*machine_data
;
196 /* Find the codec node for this SSI. */
197 codec_np
= of_parse_phandle(np
, "codec-handle", 0);
199 dev_err(dev
, "invalid codec node\n");
203 machine_data
= kzalloc(sizeof(struct mpc8610_hpcd_data
), GFP_KERNEL
);
209 machine_data
->dai
[0].cpu_dai_name
= dev_name(&ssi_pdev
->dev
);
210 machine_data
->dai
[0].ops
= &mpc8610_hpcd_ops
;
212 /* ASoC core can match codec with device node */
213 machine_data
->dai
[0].codec_of_node
= codec_np
;
215 /* The DAI name from the codec (snd_soc_dai_driver.name) */
216 machine_data
->dai
[0].codec_dai_name
= "cs4270-hifi";
218 /* We register two DAIs per SSI, one for playback and the other for
219 * capture. Currently, we only support codecs that have one DAI for
220 * both playback and capture.
222 memcpy(&machine_data
->dai
[1], &machine_data
->dai
[0],
223 sizeof(struct snd_soc_dai_link
));
225 /* Get the device ID */
226 iprop
= of_get_property(np
, "cell-index", NULL
);
228 dev_err(&pdev
->dev
, "cell-index property not found\n");
232 machine_data
->ssi_id
= be32_to_cpup(iprop
);
234 /* Get the serial format and clock direction. */
235 sprop
= of_get_property(np
, "fsl,mode", NULL
);
237 dev_err(&pdev
->dev
, "fsl,mode property not found\n");
242 if (strcasecmp(sprop
, "i2s-slave") == 0) {
243 machine_data
->dai_format
=
244 SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBM_CFM
;
245 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
246 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
248 /* In i2s-slave mode, the codec has its own clock source, so we
249 * need to get the frequency from the device tree and pass it to
252 iprop
= of_get_property(codec_np
, "clock-frequency", NULL
);
253 if (!iprop
|| !*iprop
) {
254 dev_err(&pdev
->dev
, "codec bus-frequency "
255 "property is missing or invalid\n");
259 machine_data
->clk_frequency
= be32_to_cpup(iprop
);
260 } else if (strcasecmp(sprop
, "i2s-master") == 0) {
261 machine_data
->dai_format
=
262 SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBS_CFS
;
263 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
264 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
265 } else if (strcasecmp(sprop
, "lj-slave") == 0) {
266 machine_data
->dai_format
=
267 SND_SOC_DAIFMT_LEFT_J
| SND_SOC_DAIFMT_CBM_CFM
;
268 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
269 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
270 } else if (strcasecmp(sprop
, "lj-master") == 0) {
271 machine_data
->dai_format
=
272 SND_SOC_DAIFMT_LEFT_J
| SND_SOC_DAIFMT_CBS_CFS
;
273 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
274 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
275 } else if (strcasecmp(sprop
, "rj-slave") == 0) {
276 machine_data
->dai_format
=
277 SND_SOC_DAIFMT_RIGHT_J
| SND_SOC_DAIFMT_CBM_CFM
;
278 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
279 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
280 } else if (strcasecmp(sprop
, "rj-master") == 0) {
281 machine_data
->dai_format
=
282 SND_SOC_DAIFMT_RIGHT_J
| SND_SOC_DAIFMT_CBS_CFS
;
283 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
284 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
285 } else if (strcasecmp(sprop
, "ac97-slave") == 0) {
286 machine_data
->dai_format
=
287 SND_SOC_DAIFMT_AC97
| SND_SOC_DAIFMT_CBM_CFM
;
288 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_OUT
;
289 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_IN
;
290 } else if (strcasecmp(sprop
, "ac97-master") == 0) {
291 machine_data
->dai_format
=
292 SND_SOC_DAIFMT_AC97
| SND_SOC_DAIFMT_CBS_CFS
;
293 machine_data
->codec_clk_direction
= SND_SOC_CLOCK_IN
;
294 machine_data
->cpu_clk_direction
= SND_SOC_CLOCK_OUT
;
297 "unrecognized fsl,mode property '%s'\n", sprop
);
302 if (!machine_data
->clk_frequency
) {
303 dev_err(&pdev
->dev
, "unknown clock frequency\n");
308 /* Find the playback DMA channel to use. */
309 machine_data
->dai
[0].platform_name
= machine_data
->platform_name
[0];
310 ret
= fsl_asoc_get_dma_channel(np
, "fsl,playback-dma",
311 &machine_data
->dai
[0],
312 &machine_data
->dma_channel_id
[0],
313 &machine_data
->dma_id
[0]);
315 dev_err(&pdev
->dev
, "missing/invalid playback DMA phandle\n");
319 /* Find the capture DMA channel to use. */
320 machine_data
->dai
[1].platform_name
= machine_data
->platform_name
[1];
321 ret
= fsl_asoc_get_dma_channel(np
, "fsl,capture-dma",
322 &machine_data
->dai
[1],
323 &machine_data
->dma_channel_id
[1],
324 &machine_data
->dma_id
[1]);
326 dev_err(&pdev
->dev
, "missing/invalid capture DMA phandle\n");
330 /* Initialize our DAI data structure. */
331 machine_data
->dai
[0].stream_name
= "playback";
332 machine_data
->dai
[1].stream_name
= "capture";
333 machine_data
->dai
[0].name
= machine_data
->dai
[0].stream_name
;
334 machine_data
->dai
[1].name
= machine_data
->dai
[1].stream_name
;
336 machine_data
->card
.probe
= mpc8610_hpcd_machine_probe
;
337 machine_data
->card
.remove
= mpc8610_hpcd_machine_remove
;
338 machine_data
->card
.name
= pdev
->name
; /* The platform driver name */
339 machine_data
->card
.owner
= THIS_MODULE
;
340 machine_data
->card
.dev
= &pdev
->dev
;
341 machine_data
->card
.num_links
= 2;
342 machine_data
->card
.dai_link
= machine_data
->dai
;
344 /* Register with ASoC */
345 ret
= snd_soc_register_card(&machine_data
->card
);
347 dev_err(&pdev
->dev
, "could not register card\n");
351 of_node_put(codec_np
);
358 of_node_put(codec_np
);
363 * mpc8610_hpcd_remove: remove the platform device
365 * This function is called when the platform device is removed.
367 static int mpc8610_hpcd_remove(struct platform_device
*pdev
)
369 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
370 struct mpc8610_hpcd_data
*machine_data
=
371 container_of(card
, struct mpc8610_hpcd_data
, card
);
373 snd_soc_unregister_card(card
);
379 static struct platform_driver mpc8610_hpcd_driver
= {
380 .probe
= mpc8610_hpcd_probe
,
381 .remove
= mpc8610_hpcd_remove
,
383 /* The name must match 'compatible' property in the device tree,
384 * in lowercase letters.
386 .name
= "snd-soc-mpc8610hpcd",
391 * mpc8610_hpcd_init: machine driver initialization.
393 * This function is called when this module is loaded.
395 static int __init
mpc8610_hpcd_init(void)
397 struct device_node
*guts_np
;
400 pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
402 /* Get the physical address of the global utilities registers */
403 guts_np
= of_find_compatible_node(NULL
, NULL
, "fsl,mpc8610-guts");
404 if (of_address_to_resource(guts_np
, 0, &res
)) {
405 pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
408 guts_phys
= res
.start
;
410 return platform_driver_register(&mpc8610_hpcd_driver
);
414 * mpc8610_hpcd_exit: machine driver exit
416 * This function is called when this driver is unloaded.
418 static void __exit
mpc8610_hpcd_exit(void)
420 platform_driver_unregister(&mpc8610_hpcd_driver
);
423 module_init(mpc8610_hpcd_init
);
424 module_exit(mpc8610_hpcd_exit
);
426 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
427 MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
428 MODULE_LICENSE("GPL v2");