xref: /linux/sound/soc/fsl/p1022_ds.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Freescale P1022DS ALSA SoC Machine driver
4 //
5 // Author: Timur Tabi <timur@freescale.com>
6 //
7 // Copyright 2010 Freescale Semiconductor, Inc.
8 
9 #include <linux/module.h>
10 #include <linux/fsl/guts.h>
11 #include <linux/interrupt.h>
12 #include <linux/of.h>
13 #include <linux/of_address.h>
14 #include <linux/slab.h>
15 #include <sound/soc.h>
16 
17 #include "fsl_dma.h"
18 #include "fsl_ssi.h"
19 #include "fsl_utils.h"
20 
21 /* P1022-specific PMUXCR and DMUXCR bit definitions */
22 
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
26 
27 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK	0x00000c00
28 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI	0x00000000
29 
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 */
32 
33 /*
34  * Set the DMACR register in the GUTS
35  *
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
39  * them.
40  *
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)
45  */
46 static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
47 	unsigned int co, unsigned int ch, unsigned int device)
48 {
49 	unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
50 
51 	clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
52 }
53 
54 /* There's only one global utilities register */
55 static phys_addr_t guts_phys;
56 
57 /**
58  * machine_data: machine-specific ASoC device data
59  *
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.
62  */
63 struct machine_data {
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 */
74 };
75 
76 #define card_to_mdata(_card) container_of(_card, struct machine_data, card)
77 
78 /**
79  * p1022_ds_machine_probe: initialize the board
80  *
81  * This function is used to initialize the board-specific hardware.
82  *
83  * Here we program the DMACR and PMUXCR registers.
84  */
85 static int p1022_ds_machine_probe(struct snd_soc_card *card)
86 {
87 	struct machine_data *mdata = card_to_mdata(card);
88 	struct ccsr_guts __iomem *guts;
89 
90 	guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
91 	if (!guts) {
92 		dev_err(card->dev, "could not map global utilities\n");
93 		return -ENOMEM;
94 	}
95 
96 	/* Enable SSI Tx signal */
97 	clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
98 			CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
99 
100 	/* Enable SSI Rx signal */
101 	clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
102 			CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
103 
104 	/* Enable DMA Channel for SSI */
105 	guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
106 			CCSR_GUTS_DMUXCR_SSI);
107 
108 	guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
109 			CCSR_GUTS_DMUXCR_SSI);
110 
111 	iounmap(guts);
112 
113 	return 0;
114 }
115 
116 /**
117  * p1022_ds_startup: program the board with various hardware parameters
118  *
119  * This function takes board-specific information, like clock frequencies
120  * and serial data formats, and passes that information to the codec and
121  * transport drivers.
122  */
123 static int p1022_ds_startup(struct snd_pcm_substream *substream)
124 {
125 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
126 	struct machine_data *mdata = card_to_mdata(rtd->card);
127 	struct device *dev = rtd->card->dev;
128 	int ret = 0;
129 
130 	/* Tell the codec driver what the serial protocol is. */
131 	ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_codec(rtd, 0), mdata->dai_format);
132 	if (ret < 0) {
133 		dev_err(dev, "could not set codec driver audio format\n");
134 		return ret;
135 	}
136 
137 	/*
138 	 * Tell the codec driver what the MCLK frequency is, and whether it's
139 	 * a slave or master.
140 	 */
141 	ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_codec(rtd, 0), 0, mdata->clk_frequency,
142 				     mdata->codec_clk_direction);
143 	if (ret < 0) {
144 		dev_err(dev, "could not set codec driver clock params\n");
145 		return ret;
146 	}
147 
148 	return 0;
149 }
150 
151 /**
152  * p1022_ds_machine_remove: Remove the sound device
153  *
154  * This function is called to remove the sound device for one SSI.  We
155  * de-program the DMACR and PMUXCR register.
156  */
157 static int p1022_ds_machine_remove(struct snd_soc_card *card)
158 {
159 	struct machine_data *mdata = card_to_mdata(card);
160 	struct ccsr_guts __iomem *guts;
161 
162 	guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
163 	if (!guts) {
164 		dev_err(card->dev, "could not map global utilities\n");
165 		return -ENOMEM;
166 	}
167 
168 	/* Restore the signal routing */
169 	clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
170 	clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
171 	guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
172 	guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
173 
174 	iounmap(guts);
175 
176 	return 0;
177 }
178 
179 /**
180  * p1022_ds_ops: ASoC machine driver operations
181  */
182 static const struct snd_soc_ops p1022_ds_ops = {
183 	.startup = p1022_ds_startup,
184 };
185 
186 /**
187  * p1022_ds_probe: platform probe function for the machine driver
188  *
189  * Although this is a machine driver, the SSI node is the "master" node with
190  * respect to audio hardware connections.  Therefore, we create a new ASoC
191  * device for each new SSI node that has a codec attached.
192  */
193 static int p1022_ds_probe(struct platform_device *pdev)
194 {
195 	struct device *dev = pdev->dev.parent;
196 	/* ssi_pdev is the platform device for the SSI node that probed us */
197 	struct platform_device *ssi_pdev = to_platform_device(dev);
198 	struct device_node *np = ssi_pdev->dev.of_node;
199 	struct device_node *codec_np = NULL;
200 	struct machine_data *mdata;
201 	struct snd_soc_dai_link_component *comp;
202 	int ret;
203 	const char *sprop;
204 	const u32 *iprop;
205 
206 	/* Find the codec node for this SSI. */
207 	codec_np = of_parse_phandle(np, "codec-handle", 0);
208 	if (!codec_np) {
209 		dev_err(dev, "could not find codec node\n");
210 		return -EINVAL;
211 	}
212 
213 	mdata = kzalloc_obj(struct machine_data);
214 	if (!mdata) {
215 		ret = -ENOMEM;
216 		goto error_put;
217 	}
218 
219 	comp = devm_kzalloc(&pdev->dev, 6 * sizeof(*comp), GFP_KERNEL);
220 	if (!comp) {
221 		ret = -ENOMEM;
222 		goto error_put;
223 	}
224 
225 	mdata->dai[0].cpus	= &comp[0];
226 	mdata->dai[0].codecs	= &comp[1];
227 	mdata->dai[0].platforms	= &comp[2];
228 
229 	mdata->dai[0].num_cpus		= 1;
230 	mdata->dai[0].num_codecs	= 1;
231 	mdata->dai[0].num_platforms	= 1;
232 
233 	mdata->dai[1].cpus	= &comp[3];
234 	mdata->dai[1].codecs	= &comp[4];
235 	mdata->dai[1].platforms	= &comp[5];
236 
237 	mdata->dai[1].num_cpus		= 1;
238 	mdata->dai[1].num_codecs	= 1;
239 	mdata->dai[1].num_platforms	= 1;
240 
241 
242 	mdata->dai[0].cpus->dai_name = dev_name(&ssi_pdev->dev);
243 	mdata->dai[0].ops = &p1022_ds_ops;
244 
245 	/* ASoC core can match codec with device node */
246 	mdata->dai[0].codecs->of_node = codec_np;
247 
248 	/* We register two DAIs per SSI, one for playback and the other for
249 	 * capture.  We support codecs that have separate DAIs for both playback
250 	 * and capture.
251 	 */
252 	memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
253 
254 	/* The DAI names from the codec (snd_soc_dai_driver.name) */
255 	mdata->dai[0].codecs->dai_name = "wm8776-hifi-playback";
256 	mdata->dai[1].codecs->dai_name = "wm8776-hifi-capture";
257 
258 	/* Get the device ID */
259 	iprop = of_get_property(np, "cell-index", NULL);
260 	if (!iprop) {
261 		dev_err(&pdev->dev, "cell-index property not found\n");
262 		ret = -EINVAL;
263 		goto error;
264 	}
265 	mdata->ssi_id = be32_to_cpup(iprop);
266 
267 	/* Get the serial format and clock direction. */
268 	sprop = of_get_property(np, "fsl,mode", NULL);
269 	if (!sprop) {
270 		dev_err(&pdev->dev, "fsl,mode property not found\n");
271 		ret = -EINVAL;
272 		goto error;
273 	}
274 
275 	if (strcasecmp(sprop, "i2s-slave") == 0) {
276 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
277 			SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBP_CFP;
278 		mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
279 		mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
280 
281 		/* In i2s-slave mode, the codec has its own clock source, so we
282 		 * need to get the frequency from the device tree and pass it to
283 		 * the codec driver.
284 		 */
285 		iprop = of_get_property(codec_np, "clock-frequency", NULL);
286 		if (!iprop || !*iprop) {
287 			dev_err(&pdev->dev, "codec bus-frequency "
288 				"property is missing or invalid\n");
289 			ret = -EINVAL;
290 			goto error;
291 		}
292 		mdata->clk_frequency = be32_to_cpup(iprop);
293 	} else if (strcasecmp(sprop, "i2s-master") == 0) {
294 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
295 			SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBC_CFC;
296 		mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
297 		mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
298 	} else if (strcasecmp(sprop, "lj-slave") == 0) {
299 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
300 			SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBP_CFP;
301 		mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
302 		mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
303 	} else if (strcasecmp(sprop, "lj-master") == 0) {
304 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
305 			SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBC_CFC;
306 		mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
307 		mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
308 	} else if (strcasecmp(sprop, "rj-slave") == 0) {
309 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
310 			SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBP_CFP;
311 		mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
312 		mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
313 	} else if (strcasecmp(sprop, "rj-master") == 0) {
314 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
315 			SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBC_CFC;
316 		mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
317 		mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
318 	} else if (strcasecmp(sprop, "ac97-slave") == 0) {
319 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
320 			SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBP_CFP;
321 		mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
322 		mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
323 	} else if (strcasecmp(sprop, "ac97-master") == 0) {
324 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
325 			SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBC_CFC;
326 		mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
327 		mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
328 	} else {
329 		dev_err(&pdev->dev,
330 			"unrecognized fsl,mode property '%s'\n", sprop);
331 		ret = -EINVAL;
332 		goto error;
333 	}
334 
335 	if (!mdata->clk_frequency) {
336 		dev_err(&pdev->dev, "unknown clock frequency\n");
337 		ret = -EINVAL;
338 		goto error;
339 	}
340 
341 	/* Find the playback DMA channel to use. */
342 	mdata->dai[0].platforms->name = mdata->platform_name[0];
343 	ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
344 				       &mdata->dma_channel_id[0],
345 				       &mdata->dma_id[0]);
346 	if (ret) {
347 		dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
348 		goto error;
349 	}
350 
351 	/* Find the capture DMA channel to use. */
352 	mdata->dai[1].platforms->name = mdata->platform_name[1];
353 	ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
354 				       &mdata->dma_channel_id[1],
355 				       &mdata->dma_id[1]);
356 	if (ret) {
357 		dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
358 		goto error;
359 	}
360 
361 	/* Initialize our DAI data structure.  */
362 	mdata->dai[0].stream_name = "playback";
363 	mdata->dai[1].stream_name = "capture";
364 	mdata->dai[0].name = mdata->dai[0].stream_name;
365 	mdata->dai[1].name = mdata->dai[1].stream_name;
366 
367 	mdata->card.probe = p1022_ds_machine_probe;
368 	mdata->card.remove = p1022_ds_machine_remove;
369 	mdata->card.name = pdev->name; /* The platform driver name */
370 	mdata->card.owner = THIS_MODULE;
371 	mdata->card.dev = &pdev->dev;
372 	mdata->card.num_links = 2;
373 	mdata->card.dai_link = mdata->dai;
374 
375 	/* Register with ASoC */
376 	ret = snd_soc_register_card(&mdata->card);
377 	if (ret) {
378 		dev_err(&pdev->dev, "could not register card\n");
379 		goto error;
380 	}
381 
382 	of_node_put(codec_np);
383 
384 	return 0;
385 
386 error:
387 	kfree(mdata);
388 error_put:
389 	of_node_put(codec_np);
390 	return ret;
391 }
392 
393 /**
394  * p1022_ds_remove: remove the platform device
395  *
396  * This function is called when the platform device is removed.
397  */
398 static void p1022_ds_remove(struct platform_device *pdev)
399 {
400 	struct snd_soc_card *card = platform_get_drvdata(pdev);
401 	struct machine_data *mdata = card_to_mdata(card);
402 
403 	snd_soc_unregister_card(card);
404 	kfree(mdata);
405 }
406 
407 static struct platform_driver p1022_ds_driver = {
408 	.probe = p1022_ds_probe,
409 	.remove = p1022_ds_remove,
410 	.driver = {
411 		/*
412 		 * The name must match 'compatible' property in the device tree,
413 		 * in lowercase letters.
414 		 */
415 		.name = "snd-soc-p1022ds",
416 	},
417 };
418 
419 /**
420  * p1022_ds_init: machine driver initialization.
421  *
422  * This function is called when this module is loaded.
423  */
424 static int __init p1022_ds_init(void)
425 {
426 	struct device_node *guts_np;
427 	struct resource res;
428 
429 	/* Get the physical address of the global utilities registers */
430 	guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
431 	if (of_address_to_resource(guts_np, 0, &res)) {
432 		pr_err("snd-soc-p1022ds: missing/invalid global utils node\n");
433 		of_node_put(guts_np);
434 		return -EINVAL;
435 	}
436 	guts_phys = res.start;
437 	of_node_put(guts_np);
438 
439 	return platform_driver_register(&p1022_ds_driver);
440 }
441 
442 /**
443  * p1022_ds_exit: machine driver exit
444  *
445  * This function is called when this driver is unloaded.
446  */
447 static void __exit p1022_ds_exit(void)
448 {
449 	platform_driver_unregister(&p1022_ds_driver);
450 }
451 
452 module_init(p1022_ds_init);
453 module_exit(p1022_ds_exit);
454 
455 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
456 MODULE_DESCRIPTION("Freescale P1022 DS ALSA SoC machine driver");
457 MODULE_LICENSE("GPL v2");
458