xref: /openbmc/linux/sound/soc/fsl/p1022_ds.c (revision b664e06d)
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_address.h>
13 #include <linux/of_device.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 /**
77  * p1022_ds_machine_probe: initialize the board
78  *
79  * This function is used to initialize the board-specific hardware.
80  *
81  * Here we program the DMACR and PMUXCR registers.
82  */
83 static int p1022_ds_machine_probe(struct snd_soc_card *card)
84 {
85 	struct machine_data *mdata =
86 		container_of(card, struct machine_data, card);
87 	struct ccsr_guts __iomem *guts;
88 
89 	guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
90 	if (!guts) {
91 		dev_err(card->dev, "could not map global utilities\n");
92 		return -ENOMEM;
93 	}
94 
95 	/* Enable SSI Tx signal */
96 	clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
97 			CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
98 
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);
102 
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);
106 
107 	guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
108 			CCSR_GUTS_DMUXCR_SSI);
109 
110 	iounmap(guts);
111 
112 	return 0;
113 }
114 
115 /**
116  * p1022_ds_startup: program the board with various hardware parameters
117  *
118  * This function takes board-specific information, like clock frequencies
119  * and serial data formats, and passes that information to the codec and
120  * transport drivers.
121  */
122 static int p1022_ds_startup(struct snd_pcm_substream *substream)
123 {
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;
128 	int ret = 0;
129 
130 	/* Tell the codec driver what the serial protocol is. */
131 	ret = snd_soc_dai_set_fmt(rtd->codec_dai, 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(rtd->codec_dai, 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 =
160 		container_of(card, struct machine_data, card);
161 	struct ccsr_guts __iomem *guts;
162 
163 	guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
164 	if (!guts) {
165 		dev_err(card->dev, "could not map global utilities\n");
166 		return -ENOMEM;
167 	}
168 
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);
174 
175 	iounmap(guts);
176 
177 	return 0;
178 }
179 
180 /**
181  * p1022_ds_ops: ASoC machine driver operations
182  */
183 static const struct snd_soc_ops p1022_ds_ops = {
184 	.startup = p1022_ds_startup,
185 };
186 
187 /**
188  * p1022_ds_probe: platform probe function for the machine driver
189  *
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.
193  */
194 static int p1022_ds_probe(struct platform_device *pdev)
195 {
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;
202 	int ret = -ENODEV;
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(sizeof(struct machine_data), GFP_KERNEL);
214 	if (!mdata) {
215 		ret = -ENOMEM;
216 		goto error_put;
217 	}
218 
219 	mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
220 	mdata->dai[0].ops = &p1022_ds_ops;
221 
222 	/* ASoC core can match codec with device node */
223 	mdata->dai[0].codec_of_node = codec_np;
224 
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
227 	 * and capture.
228 	 */
229 	memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
230 
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";
234 
235 	/* Get the device ID */
236 	iprop = of_get_property(np, "cell-index", NULL);
237 	if (!iprop) {
238 		dev_err(&pdev->dev, "cell-index property not found\n");
239 		ret = -EINVAL;
240 		goto error;
241 	}
242 	mdata->ssi_id = be32_to_cpup(iprop);
243 
244 	/* Get the serial format and clock direction. */
245 	sprop = of_get_property(np, "fsl,mode", NULL);
246 	if (!sprop) {
247 		dev_err(&pdev->dev, "fsl,mode property not found\n");
248 		ret = -EINVAL;
249 		goto error;
250 	}
251 
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;
257 
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
260 		 * the codec driver.
261 		 */
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");
266 			ret = -EINVAL;
267 			goto error;
268 		}
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;
305 	} else {
306 		dev_err(&pdev->dev,
307 			"unrecognized fsl,mode property '%s'\n", sprop);
308 		ret = -EINVAL;
309 		goto error;
310 	}
311 
312 	if (!mdata->clk_frequency) {
313 		dev_err(&pdev->dev, "unknown clock frequency\n");
314 		ret = -EINVAL;
315 		goto error;
316 	}
317 
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],
322 				       &mdata->dma_id[0]);
323 	if (ret) {
324 		dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
325 		goto error;
326 	}
327 
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],
332 				       &mdata->dma_id[1]);
333 	if (ret) {
334 		dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
335 		goto error;
336 	}
337 
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;
343 
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;
351 
352 	/* Register with ASoC */
353 	ret = snd_soc_register_card(&mdata->card);
354 	if (ret) {
355 		dev_err(&pdev->dev, "could not register card\n");
356 		goto error;
357 	}
358 
359 	of_node_put(codec_np);
360 
361 	return 0;
362 
363 error:
364 	kfree(mdata);
365 error_put:
366 	of_node_put(codec_np);
367 	return ret;
368 }
369 
370 /**
371  * p1022_ds_remove: remove the platform device
372  *
373  * This function is called when the platform device is removed.
374  */
375 static int p1022_ds_remove(struct platform_device *pdev)
376 {
377 	struct snd_soc_card *card = platform_get_drvdata(pdev);
378 	struct machine_data *mdata =
379 		container_of(card, struct machine_data, card);
380 
381 	snd_soc_unregister_card(card);
382 	kfree(mdata);
383 
384 	return 0;
385 }
386 
387 static struct platform_driver p1022_ds_driver = {
388 	.probe = p1022_ds_probe,
389 	.remove = p1022_ds_remove,
390 	.driver = {
391 		/*
392 		 * The name must match 'compatible' property in the device tree,
393 		 * in lowercase letters.
394 		 */
395 		.name = "snd-soc-p1022ds",
396 	},
397 };
398 
399 /**
400  * p1022_ds_init: machine driver initialization.
401  *
402  * This function is called when this module is loaded.
403  */
404 static int __init p1022_ds_init(void)
405 {
406 	struct device_node *guts_np;
407 	struct resource res;
408 
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);
414 		return -EINVAL;
415 	}
416 	guts_phys = res.start;
417 	of_node_put(guts_np);
418 
419 	return platform_driver_register(&p1022_ds_driver);
420 }
421 
422 /**
423  * p1022_ds_exit: machine driver exit
424  *
425  * This function is called when this driver is unloaded.
426  */
427 static void __exit p1022_ds_exit(void)
428 {
429 	platform_driver_unregister(&p1022_ds_driver);
430 }
431 
432 module_init(p1022_ds_init);
433 module_exit(p1022_ds_exit);
434 
435 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
436 MODULE_DESCRIPTION("Freescale P1022 DS ALSA SoC machine driver");
437 MODULE_LICENSE("GPL v2");
438