xref: /openbmc/linux/sound/soc/fsl/p1022_ds.c (revision 63dc02bd)
1 /**
2  * Freescale P1022DS ALSA SoC Machine driver
3  *
4  * Author: Timur Tabi <timur@freescale.com>
5  *
6  * Copyright 2010 Freescale Semiconductor, Inc.
7  *
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.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/of_device.h>
16 #include <linux/slab.h>
17 #include <linux/of_i2c.h>
18 #include <sound/soc.h>
19 #include <asm/fsl_guts.h>
20 
21 #include "fsl_dma.h"
22 #include "fsl_ssi.h"
23 
24 /* P1022-specific PMUXCR and DMUXCR bit definitions */
25 
26 #define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK	0x0001c000
27 #define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI	0x00010000
28 #define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI		0x00018000
29 
30 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK	0x00000c00
31 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI	0x00000000
32 
33 #define CCSR_GUTS_DMUXCR_PAD	1	/* DMA controller/channel set to pad */
34 #define CCSR_GUTS_DMUXCR_SSI	2	/* DMA controller/channel set to SSI */
35 
36 /*
37  * Set the DMACR register in the GUTS
38  *
39  * The DMACR register determines the source of initiated transfers for each
40  * channel on each DMA controller.  Rather than have a bunch of repetitive
41  * macros for the bit patterns, we just have a function that calculates
42  * them.
43  *
44  * guts: Pointer to GUTS structure
45  * co: The DMA controller (0 or 1)
46  * ch: The channel on the DMA controller (0, 1, 2, or 3)
47  * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx)
48  */
49 static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
50 	unsigned int co, unsigned int ch, unsigned int device)
51 {
52 	unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
53 
54 	clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
55 }
56 
57 /* There's only one global utilities register */
58 static phys_addr_t guts_phys;
59 
60 #define DAI_NAME_SIZE	32
61 
62 /**
63  * machine_data: machine-specific ASoC device data
64  *
65  * This structure contains data for a single sound platform device on an
66  * P1022 DS.  Some of the data is taken from the device tree.
67  */
68 struct machine_data {
69 	struct snd_soc_dai_link dai[2];
70 	struct snd_soc_card card;
71 	unsigned int dai_format;
72 	unsigned int codec_clk_direction;
73 	unsigned int cpu_clk_direction;
74 	unsigned int clk_frequency;
75 	unsigned int ssi_id;		/* 0 = SSI1, 1 = SSI2, etc */
76 	unsigned int dma_id[2];		/* 0 = DMA1, 1 = DMA2, etc */
77 	unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
78 	char codec_name[DAI_NAME_SIZE];
79 	char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
80 };
81 
82 /**
83  * p1022_ds_machine_probe: initialize the board
84  *
85  * This function is used to initialize the board-specific hardware.
86  *
87  * Here we program the DMACR and PMUXCR registers.
88  */
89 static int p1022_ds_machine_probe(struct snd_soc_card *card)
90 {
91 	struct machine_data *mdata =
92 		container_of(card, struct machine_data, card);
93 	struct ccsr_guts __iomem *guts;
94 
95 	guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
96 	if (!guts) {
97 		dev_err(card->dev, "could not map global utilities\n");
98 		return -ENOMEM;
99 	}
100 
101 	/* Enable SSI Tx signal */
102 	clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
103 			CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
104 
105 	/* Enable SSI Rx signal */
106 	clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
107 			CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
108 
109 	/* Enable DMA Channel for SSI */
110 	guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
111 			CCSR_GUTS_DMUXCR_SSI);
112 
113 	guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
114 			CCSR_GUTS_DMUXCR_SSI);
115 
116 	iounmap(guts);
117 
118 	return 0;
119 }
120 
121 /**
122  * p1022_ds_startup: program the board with various hardware parameters
123  *
124  * This function takes board-specific information, like clock frequencies
125  * and serial data formats, and passes that information to the codec and
126  * transport drivers.
127  */
128 static int p1022_ds_startup(struct snd_pcm_substream *substream)
129 {
130 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
131 	struct machine_data *mdata =
132 		container_of(rtd->card, struct machine_data, card);
133 	struct device *dev = rtd->card->dev;
134 	int ret = 0;
135 
136 	/* Tell the codec driver what the serial protocol is. */
137 	ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format);
138 	if (ret < 0) {
139 		dev_err(dev, "could not set codec driver audio format\n");
140 		return ret;
141 	}
142 
143 	/*
144 	 * Tell the codec driver what the MCLK frequency is, and whether it's
145 	 * a slave or master.
146 	 */
147 	ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0, mdata->clk_frequency,
148 				     mdata->codec_clk_direction);
149 	if (ret < 0) {
150 		dev_err(dev, "could not set codec driver clock params\n");
151 		return ret;
152 	}
153 
154 	return 0;
155 }
156 
157 /**
158  * p1022_ds_machine_remove: Remove the sound device
159  *
160  * This function is called to remove the sound device for one SSI.  We
161  * de-program the DMACR and PMUXCR register.
162  */
163 static int p1022_ds_machine_remove(struct snd_soc_card *card)
164 {
165 	struct machine_data *mdata =
166 		container_of(card, struct machine_data, card);
167 	struct ccsr_guts __iomem *guts;
168 
169 	guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
170 	if (!guts) {
171 		dev_err(card->dev, "could not map global utilities\n");
172 		return -ENOMEM;
173 	}
174 
175 	/* Restore the signal routing */
176 	clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
177 	clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
178 	guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
179 	guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
180 
181 	iounmap(guts);
182 
183 	return 0;
184 }
185 
186 /**
187  * p1022_ds_ops: ASoC machine driver operations
188  */
189 static struct snd_soc_ops p1022_ds_ops = {
190 	.startup = p1022_ds_startup,
191 };
192 
193 /**
194  * get_node_by_phandle_name - get a node by its phandle name
195  *
196  * This function takes a node, the name of a property in that node, and a
197  * compatible string.  Assuming the property is a phandle to another node,
198  * it returns that node, (optionally) if that node is compatible.
199  *
200  * If the property is not a phandle, or the node it points to is not compatible
201  * with the specific string, then NULL is returned.
202  */
203 static struct device_node *get_node_by_phandle_name(struct device_node *np,
204 	const char *name, const char *compatible)
205 {
206 	np = of_parse_phandle(np, name, 0);
207 	if (!np)
208 		return NULL;
209 
210 	if (!of_device_is_compatible(np, compatible)) {
211 		of_node_put(np);
212 		return NULL;
213 	}
214 
215 	return np;
216 }
217 
218 /**
219  * get_parent_cell_index -- return the cell-index of the parent of a node
220  *
221  * Return the value of the cell-index property of the parent of the given
222  * node.  This is used for DMA channel nodes that need to know the DMA ID
223  * of the controller they are on.
224  */
225 static int get_parent_cell_index(struct device_node *np)
226 {
227 	struct device_node *parent = of_get_parent(np);
228 	const u32 *iprop;
229 	int ret = -1;
230 
231 	if (!parent)
232 		return -1;
233 
234 	iprop = of_get_property(parent, "cell-index", NULL);
235 	if (iprop)
236 		ret = be32_to_cpup(iprop);
237 
238 	of_node_put(parent);
239 
240 	return ret;
241 }
242 
243 /**
244  * codec_node_dev_name - determine the dev_name for a codec node
245  *
246  * This function determines the dev_name for an I2C node.  This is the name
247  * that would be returned by dev_name() if this device_node were part of a
248  * 'struct device'  It's ugly and hackish, but it works.
249  *
250  * The dev_name for such devices include the bus number and I2C address. For
251  * example, "cs4270-codec.0-004f".
252  */
253 static int codec_node_dev_name(struct device_node *np, char *buf, size_t len)
254 {
255 	const u32 *iprop;
256 	int addr;
257 	char temp[DAI_NAME_SIZE];
258 	struct i2c_client *i2c;
259 
260 	of_modalias_node(np, temp, DAI_NAME_SIZE);
261 
262 	iprop = of_get_property(np, "reg", NULL);
263 	if (!iprop)
264 		return -EINVAL;
265 
266 	addr = be32_to_cpup(iprop);
267 
268 	/* We need the adapter number */
269 	i2c = of_find_i2c_device_by_node(np);
270 	if (!i2c)
271 		return -ENODEV;
272 
273 	snprintf(buf, len, "%s.%u-%04x", temp, i2c->adapter->nr, addr);
274 
275 	return 0;
276 }
277 
278 static int get_dma_channel(struct device_node *ssi_np,
279 			   const char *name,
280 			   struct snd_soc_dai_link *dai,
281 			   unsigned int *dma_channel_id,
282 			   unsigned int *dma_id)
283 {
284 	struct resource res;
285 	struct device_node *dma_channel_np;
286 	const u32 *iprop;
287 	int ret;
288 
289 	dma_channel_np = get_node_by_phandle_name(ssi_np, name,
290 						  "fsl,ssi-dma-channel");
291 	if (!dma_channel_np)
292 		return -EINVAL;
293 
294 	/* Determine the dev_name for the device_node.  This code mimics the
295 	 * behavior of of_device_make_bus_id(). We need this because ASoC uses
296 	 * the dev_name() of the device to match the platform (DMA) device with
297 	 * the CPU (SSI) device.  It's all ugly and hackish, but it works (for
298 	 * now).
299 	 *
300 	 * dai->platform name should already point to an allocated buffer.
301 	 */
302 	ret = of_address_to_resource(dma_channel_np, 0, &res);
303 	if (ret) {
304 		of_node_put(dma_channel_np);
305 		return ret;
306 	}
307 	snprintf((char *)dai->platform_name, DAI_NAME_SIZE, "%llx.%s",
308 		 (unsigned long long) res.start, dma_channel_np->name);
309 
310 	iprop = of_get_property(dma_channel_np, "cell-index", NULL);
311 	if (!iprop) {
312 		of_node_put(dma_channel_np);
313 		return -EINVAL;
314 	}
315 
316 	*dma_channel_id = be32_to_cpup(iprop);
317 	*dma_id = get_parent_cell_index(dma_channel_np);
318 	of_node_put(dma_channel_np);
319 
320 	return 0;
321 }
322 
323 /**
324  * p1022_ds_probe: platform probe function for the machine driver
325  *
326  * Although this is a machine driver, the SSI node is the "master" node with
327  * respect to audio hardware connections.  Therefore, we create a new ASoC
328  * device for each new SSI node that has a codec attached.
329  */
330 static int p1022_ds_probe(struct platform_device *pdev)
331 {
332 	struct device *dev = pdev->dev.parent;
333 	/* ssi_pdev is the platform device for the SSI node that probed us */
334 	struct platform_device *ssi_pdev =
335 		container_of(dev, struct platform_device, dev);
336 	struct device_node *np = ssi_pdev->dev.of_node;
337 	struct device_node *codec_np = NULL;
338 	struct platform_device *sound_device = NULL;
339 	struct machine_data *mdata;
340 	int ret = -ENODEV;
341 	const char *sprop;
342 	const u32 *iprop;
343 
344 	/* Find the codec node for this SSI. */
345 	codec_np = of_parse_phandle(np, "codec-handle", 0);
346 	if (!codec_np) {
347 		dev_err(dev, "could not find codec node\n");
348 		return -EINVAL;
349 	}
350 
351 	mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
352 	if (!mdata) {
353 		ret = -ENOMEM;
354 		goto error_put;
355 	}
356 
357 	mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
358 	mdata->dai[0].ops = &p1022_ds_ops;
359 
360 	/* Determine the codec name, it will be used as the codec DAI name */
361 	ret = codec_node_dev_name(codec_np, mdata->codec_name, DAI_NAME_SIZE);
362 	if (ret) {
363 		dev_err(&pdev->dev, "invalid codec node %s\n",
364 			codec_np->full_name);
365 		ret = -EINVAL;
366 		goto error;
367 	}
368 	mdata->dai[0].codec_name = mdata->codec_name;
369 
370 	/* We register two DAIs per SSI, one for playback and the other for
371 	 * capture.  We support codecs that have separate DAIs for both playback
372 	 * and capture.
373 	 */
374 	memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
375 
376 	/* The DAI names from the codec (snd_soc_dai_driver.name) */
377 	mdata->dai[0].codec_dai_name = "wm8776-hifi-playback";
378 	mdata->dai[1].codec_dai_name = "wm8776-hifi-capture";
379 
380 	/* Get the device ID */
381 	iprop = of_get_property(np, "cell-index", NULL);
382 	if (!iprop) {
383 		dev_err(&pdev->dev, "cell-index property not found\n");
384 		ret = -EINVAL;
385 		goto error;
386 	}
387 	mdata->ssi_id = be32_to_cpup(iprop);
388 
389 	/* Get the serial format and clock direction. */
390 	sprop = of_get_property(np, "fsl,mode", NULL);
391 	if (!sprop) {
392 		dev_err(&pdev->dev, "fsl,mode property not found\n");
393 		ret = -EINVAL;
394 		goto error;
395 	}
396 
397 	if (strcasecmp(sprop, "i2s-slave") == 0) {
398 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
399 			SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
400 		mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
401 		mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
402 
403 		/* In i2s-slave mode, the codec has its own clock source, so we
404 		 * need to get the frequency from the device tree and pass it to
405 		 * the codec driver.
406 		 */
407 		iprop = of_get_property(codec_np, "clock-frequency", NULL);
408 		if (!iprop || !*iprop) {
409 			dev_err(&pdev->dev, "codec bus-frequency "
410 				"property is missing or invalid\n");
411 			ret = -EINVAL;
412 			goto error;
413 		}
414 		mdata->clk_frequency = be32_to_cpup(iprop);
415 	} else if (strcasecmp(sprop, "i2s-master") == 0) {
416 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
417 			SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
418 		mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
419 		mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
420 	} else if (strcasecmp(sprop, "lj-slave") == 0) {
421 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
422 			SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
423 		mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
424 		mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
425 	} else if (strcasecmp(sprop, "lj-master") == 0) {
426 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
427 			SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
428 		mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
429 		mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
430 	} else if (strcasecmp(sprop, "rj-slave") == 0) {
431 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
432 			SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
433 		mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
434 		mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
435 	} else if (strcasecmp(sprop, "rj-master") == 0) {
436 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
437 			SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
438 		mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
439 		mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
440 	} else if (strcasecmp(sprop, "ac97-slave") == 0) {
441 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
442 			SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
443 		mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
444 		mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
445 	} else if (strcasecmp(sprop, "ac97-master") == 0) {
446 		mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
447 			SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
448 		mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
449 		mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
450 	} else {
451 		dev_err(&pdev->dev,
452 			"unrecognized fsl,mode property '%s'\n", sprop);
453 		ret = -EINVAL;
454 		goto error;
455 	}
456 
457 	if (!mdata->clk_frequency) {
458 		dev_err(&pdev->dev, "unknown clock frequency\n");
459 		ret = -EINVAL;
460 		goto error;
461 	}
462 
463 	/* Find the playback DMA channel to use. */
464 	mdata->dai[0].platform_name = mdata->platform_name[0];
465 	ret = get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
466 			      &mdata->dma_channel_id[0],
467 			      &mdata->dma_id[0]);
468 	if (ret) {
469 		dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
470 		goto error;
471 	}
472 
473 	/* Find the capture DMA channel to use. */
474 	mdata->dai[1].platform_name = mdata->platform_name[1];
475 	ret = get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
476 			      &mdata->dma_channel_id[1],
477 			      &mdata->dma_id[1]);
478 	if (ret) {
479 		dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
480 		goto error;
481 	}
482 
483 	/* Initialize our DAI data structure.  */
484 	mdata->dai[0].stream_name = "playback";
485 	mdata->dai[1].stream_name = "capture";
486 	mdata->dai[0].name = mdata->dai[0].stream_name;
487 	mdata->dai[1].name = mdata->dai[1].stream_name;
488 
489 	mdata->card.probe = p1022_ds_machine_probe;
490 	mdata->card.remove = p1022_ds_machine_remove;
491 	mdata->card.name = pdev->name; /* The platform driver name */
492 	mdata->card.num_links = 2;
493 	mdata->card.dai_link = mdata->dai;
494 
495 	/* Allocate a new audio platform device structure */
496 	sound_device = platform_device_alloc("soc-audio", -1);
497 	if (!sound_device) {
498 		dev_err(&pdev->dev, "platform device alloc failed\n");
499 		ret = -ENOMEM;
500 		goto error;
501 	}
502 
503 	/* Associate the card data with the sound device */
504 	platform_set_drvdata(sound_device, &mdata->card);
505 
506 	/* Register with ASoC */
507 	ret = platform_device_add(sound_device);
508 	if (ret) {
509 		dev_err(&pdev->dev, "platform device add failed\n");
510 		goto error;
511 	}
512 	dev_set_drvdata(&pdev->dev, sound_device);
513 
514 	of_node_put(codec_np);
515 
516 	return 0;
517 
518 error:
519 	if (sound_device)
520 		platform_device_put(sound_device);
521 
522 	kfree(mdata);
523 error_put:
524 	of_node_put(codec_np);
525 	return ret;
526 }
527 
528 /**
529  * p1022_ds_remove: remove the platform device
530  *
531  * This function is called when the platform device is removed.
532  */
533 static int __devexit p1022_ds_remove(struct platform_device *pdev)
534 {
535 	struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
536 	struct snd_soc_card *card = platform_get_drvdata(sound_device);
537 	struct machine_data *mdata =
538 		container_of(card, struct machine_data, card);
539 
540 	platform_device_unregister(sound_device);
541 
542 	kfree(mdata);
543 	sound_device->dev.platform_data = NULL;
544 
545 	dev_set_drvdata(&pdev->dev, NULL);
546 
547 	return 0;
548 }
549 
550 static struct platform_driver p1022_ds_driver = {
551 	.probe = p1022_ds_probe,
552 	.remove = __devexit_p(p1022_ds_remove),
553 	.driver = {
554 		/*
555 		 * The name must match 'compatible' property in the device tree,
556 		 * in lowercase letters.
557 		 */
558 		.name = "snd-soc-p1022ds",
559 		.owner = THIS_MODULE,
560 	},
561 };
562 
563 /**
564  * p1022_ds_init: machine driver initialization.
565  *
566  * This function is called when this module is loaded.
567  */
568 static int __init p1022_ds_init(void)
569 {
570 	struct device_node *guts_np;
571 	struct resource res;
572 
573 	/* Get the physical address of the global utilities registers */
574 	guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
575 	if (of_address_to_resource(guts_np, 0, &res)) {
576 		pr_err("snd-soc-p1022ds: missing/invalid global utils node\n");
577 		of_node_put(guts_np);
578 		return -EINVAL;
579 	}
580 	guts_phys = res.start;
581 	of_node_put(guts_np);
582 
583 	return platform_driver_register(&p1022_ds_driver);
584 }
585 
586 /**
587  * p1022_ds_exit: machine driver exit
588  *
589  * This function is called when this driver is unloaded.
590  */
591 static void __exit p1022_ds_exit(void)
592 {
593 	platform_driver_unregister(&p1022_ds_driver);
594 }
595 
596 module_init(p1022_ds_init);
597 module_exit(p1022_ds_exit);
598 
599 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
600 MODULE_DESCRIPTION("Freescale P1022 DS ALSA SoC machine driver");
601 MODULE_LICENSE("GPL v2");
602