1 // SPDX-License-Identifier: GPL-2.0
3 // Freescale P1022DS ALSA SoC Machine driver
5 // Author: Timur Tabi <timur@freescale.com>
7 // Copyright 2010 Freescale Semiconductor, Inc.
9 #include <linux/module.h>
10 #include <linux/fsl/guts.h>
11 #include <linux/interrupt.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 /* P1022-specific PMUXCR and DMUXCR bit definitions */
23 #define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000
24 #define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000
25 #define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000
27 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00
28 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000
30 #define CCSR_GUTS_DMUXCR_PAD 1 /* DMA controller/channel set to pad */
31 #define CCSR_GUTS_DMUXCR_SSI 2 /* DMA controller/channel set to SSI */
34 * Set the DMACR register in the GUTS
36 * The DMACR register determines the source of initiated transfers for each
37 * channel on each DMA controller. Rather than have a bunch of repetitive
38 * macros for the bit patterns, we just have a function that calculates
41 * guts: Pointer to GUTS structure
42 * co: The DMA controller (0 or 1)
43 * ch: The channel on the DMA controller (0, 1, 2, or 3)
44 * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx)
46 static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
47 unsigned int co, unsigned int ch, unsigned int device)
49 unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
51 clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
54 /* There's only one global utilities register */
55 static phys_addr_t guts_phys;
58 * machine_data: machine-specific ASoC device data
60 * This structure contains data for a single sound platform device on an
61 * P1022 DS. Some of the data is taken from the device tree.
64 struct snd_soc_dai_link dai[2];
65 struct snd_soc_card card;
66 unsigned int dai_format;
67 unsigned int codec_clk_direction;
68 unsigned int cpu_clk_direction;
69 unsigned int clk_frequency;
70 unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
71 unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
72 unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
73 char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
77 * p1022_ds_machine_probe: initialize the board
79 * This function is used to initialize the board-specific hardware.
81 * Here we program the DMACR and PMUXCR registers.
83 static int p1022_ds_machine_probe(struct snd_soc_card *card)
85 struct machine_data *mdata =
86 container_of(card, struct machine_data, card);
87 struct ccsr_guts __iomem *guts;
89 guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
91 dev_err(card->dev, "could not map global utilities\n");
95 /* Enable SSI Tx signal */
96 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
97 CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
99 /* Enable SSI Rx signal */
100 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
101 CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
103 /* Enable DMA Channel for SSI */
104 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
105 CCSR_GUTS_DMUXCR_SSI);
107 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
108 CCSR_GUTS_DMUXCR_SSI);
116 * p1022_ds_startup: program the board with various hardware parameters
118 * This function takes board-specific information, like clock frequencies
119 * and serial data formats, and passes that information to the codec and
122 static int p1022_ds_startup(struct snd_pcm_substream *substream)
124 struct snd_soc_pcm_runtime *rtd = substream->private_data;
125 struct machine_data *mdata =
126 container_of(rtd->card, struct machine_data, card);
127 struct device *dev = rtd->card->dev;
130 /* Tell the codec driver what the serial protocol is. */
131 ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format);
133 dev_err(dev, "could not set codec driver audio format\n");
138 * Tell the codec driver what the MCLK frequency is, and whether it's
141 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0, mdata->clk_frequency,
142 mdata->codec_clk_direction);
144 dev_err(dev, "could not set codec driver clock params\n");
152 * p1022_ds_machine_remove: Remove the sound device
154 * This function is called to remove the sound device for one SSI. We
155 * de-program the DMACR and PMUXCR register.
157 static int p1022_ds_machine_remove(struct snd_soc_card *card)
159 struct machine_data *mdata =
160 container_of(card, struct machine_data, card);
161 struct ccsr_guts __iomem *guts;
163 guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
165 dev_err(card->dev, "could not map global utilities\n");
169 /* Restore the signal routing */
170 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
171 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
172 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
173 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
181 * p1022_ds_ops: ASoC machine driver operations
183 static const struct snd_soc_ops p1022_ds_ops = {
184 .startup = p1022_ds_startup,
188 * p1022_ds_probe: platform probe function for the machine driver
190 * Although this is a machine driver, the SSI node is the "master" node with
191 * respect to audio hardware connections. Therefore, we create a new ASoC
192 * device for each new SSI node that has a codec attached.
194 static int p1022_ds_probe(struct platform_device *pdev)
196 struct device *dev = pdev->dev.parent;
197 /* ssi_pdev is the platform device for the SSI node that probed us */
198 struct platform_device *ssi_pdev = to_platform_device(dev);
199 struct device_node *np = ssi_pdev->dev.of_node;
200 struct device_node *codec_np = NULL;
201 struct machine_data *mdata;
206 /* Find the codec node for this SSI. */
207 codec_np = of_parse_phandle(np, "codec-handle", 0);
209 dev_err(dev, "could not find codec node\n");
213 mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
219 mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
220 mdata->dai[0].ops = &p1022_ds_ops;
222 /* ASoC core can match codec with device node */
223 mdata->dai[0].codec_of_node = codec_np;
225 /* We register two DAIs per SSI, one for playback and the other for
226 * capture. We support codecs that have separate DAIs for both playback
229 memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
231 /* The DAI names from the codec (snd_soc_dai_driver.name) */
232 mdata->dai[0].codec_dai_name = "wm8776-hifi-playback";
233 mdata->dai[1].codec_dai_name = "wm8776-hifi-capture";
235 /* Get the device ID */
236 iprop = of_get_property(np, "cell-index", NULL);
238 dev_err(&pdev->dev, "cell-index property not found\n");
242 mdata->ssi_id = be32_to_cpup(iprop);
244 /* Get the serial format and clock direction. */
245 sprop = of_get_property(np, "fsl,mode", NULL);
247 dev_err(&pdev->dev, "fsl,mode property not found\n");
252 if (strcasecmp(sprop, "i2s-slave") == 0) {
253 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
254 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
255 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
256 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
258 /* In i2s-slave mode, the codec has its own clock source, so we
259 * need to get the frequency from the device tree and pass it to
262 iprop = of_get_property(codec_np, "clock-frequency", NULL);
263 if (!iprop || !*iprop) {
264 dev_err(&pdev->dev, "codec bus-frequency "
265 "property is missing or invalid\n");
269 mdata->clk_frequency = be32_to_cpup(iprop);
270 } else if (strcasecmp(sprop, "i2s-master") == 0) {
271 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
272 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
273 mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
274 mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
275 } else if (strcasecmp(sprop, "lj-slave") == 0) {
276 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
277 SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
278 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
279 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
280 } else if (strcasecmp(sprop, "lj-master") == 0) {
281 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
282 SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
283 mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
284 mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
285 } else if (strcasecmp(sprop, "rj-slave") == 0) {
286 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
287 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
288 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
289 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
290 } else if (strcasecmp(sprop, "rj-master") == 0) {
291 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
292 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
293 mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
294 mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
295 } else if (strcasecmp(sprop, "ac97-slave") == 0) {
296 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
297 SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
298 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
299 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
300 } else if (strcasecmp(sprop, "ac97-master") == 0) {
301 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
302 SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
303 mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
304 mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
307 "unrecognized fsl,mode property '%s'\n", sprop);
312 if (!mdata->clk_frequency) {
313 dev_err(&pdev->dev, "unknown clock frequency\n");
318 /* Find the playback DMA channel to use. */
319 mdata->dai[0].platform_name = mdata->platform_name[0];
320 ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
321 &mdata->dma_channel_id[0],
324 dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
328 /* Find the capture DMA channel to use. */
329 mdata->dai[1].platform_name = mdata->platform_name[1];
330 ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
331 &mdata->dma_channel_id[1],
334 dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
338 /* Initialize our DAI data structure. */
339 mdata->dai[0].stream_name = "playback";
340 mdata->dai[1].stream_name = "capture";
341 mdata->dai[0].name = mdata->dai[0].stream_name;
342 mdata->dai[1].name = mdata->dai[1].stream_name;
344 mdata->card.probe = p1022_ds_machine_probe;
345 mdata->card.remove = p1022_ds_machine_remove;
346 mdata->card.name = pdev->name; /* The platform driver name */
347 mdata->card.owner = THIS_MODULE;
348 mdata->card.dev = &pdev->dev;
349 mdata->card.num_links = 2;
350 mdata->card.dai_link = mdata->dai;
352 /* Register with ASoC */
353 ret = snd_soc_register_card(&mdata->card);
355 dev_err(&pdev->dev, "could not register card\n");
359 of_node_put(codec_np);
366 of_node_put(codec_np);
371 * p1022_ds_remove: remove the platform device
373 * This function is called when the platform device is removed.
375 static int p1022_ds_remove(struct platform_device *pdev)
377 struct snd_soc_card *card = platform_get_drvdata(pdev);
378 struct machine_data *mdata =
379 container_of(card, struct machine_data, card);
381 snd_soc_unregister_card(card);
387 static struct platform_driver p1022_ds_driver = {
388 .probe = p1022_ds_probe,
389 .remove = p1022_ds_remove,
392 * The name must match 'compatible' property in the device tree,
393 * in lowercase letters.
395 .name = "snd-soc-p1022ds",
400 * p1022_ds_init: machine driver initialization.
402 * This function is called when this module is loaded.
404 static int __init p1022_ds_init(void)
406 struct device_node *guts_np;
409 /* Get the physical address of the global utilities registers */
410 guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
411 if (of_address_to_resource(guts_np, 0, &res)) {
412 pr_err("snd-soc-p1022ds: missing/invalid global utils node\n");
413 of_node_put(guts_np);
416 guts_phys = res.start;
417 of_node_put(guts_np);
419 return platform_driver_register(&p1022_ds_driver);
423 * p1022_ds_exit: machine driver exit
425 * This function is called when this driver is unloaded.
427 static void __exit p1022_ds_exit(void)
429 platform_driver_unregister(&p1022_ds_driver);
432 module_init(p1022_ds_init);
433 module_exit(p1022_ds_exit);
435 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
436 MODULE_DESCRIPTION("Freescale P1022 DS ALSA SoC machine driver");
437 MODULE_LICENSE("GPL v2");