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