Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / sound / soc / fsl / mpc8610_hpcd.c
blob7df43f66cb3d851a858c8b7c3d76daa819e06500
1 /**
2 * Freescale MPC8610HPCD ALSA SoC Fabric driver
4 * Author: Timur Tabi <timur@freescale.com>
6 * Copyright 2007-2008 Freescale Semiconductor, Inc. This file is licensed
7 * under the terms of the GNU General Public License version 2. This
8 * program is licensed "as is" without any warranty of any kind, whether
9 * express or implied.
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/of_device.h>
15 #include <linux/of_platform.h>
16 #include <sound/soc.h>
17 #include <asm/immap_86xx.h>
19 #include "../codecs/cs4270.h"
20 #include "fsl_dma.h"
21 #include "fsl_ssi.h"
23 /**
24 * mpc8610_hpcd_data: fabric-specific ASoC device data
26 * This structure contains data for a single sound platform device on an
27 * MPC8610 HPCD. Some of the data is taken from the device tree.
29 struct mpc8610_hpcd_data {
30 struct snd_soc_device sound_devdata;
31 struct snd_soc_dai_link dai;
32 struct snd_soc_machine machine;
33 unsigned int dai_format;
34 unsigned int codec_clk_direction;
35 unsigned int cpu_clk_direction;
36 unsigned int clk_frequency;
37 struct ccsr_guts __iomem *guts;
38 struct ccsr_ssi __iomem *ssi;
39 unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
40 unsigned int ssi_irq;
41 unsigned int dma_id; /* 0 = DMA1, 1 = DMA2, etc */
42 unsigned int dma_irq[2];
43 struct ccsr_dma_channel __iomem *dma[2];
44 unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
47 /**
48 * mpc8610_hpcd_machine_probe: initalize the board
50 * This function is called when platform_device_add() is called. It is used
51 * to initialize the board-specific hardware.
53 * Here we program the DMACR and PMUXCR registers.
55 static int mpc8610_hpcd_machine_probe(struct platform_device *sound_device)
57 struct mpc8610_hpcd_data *machine_data =
58 sound_device->dev.platform_data;
60 /* Program the signal routing between the SSI and the DMA */
61 guts_set_dmacr(machine_data->guts, machine_data->dma_id + 1,
62 machine_data->dma_channel_id[0], CCSR_GUTS_DMACR_DEV_SSI);
63 guts_set_dmacr(machine_data->guts, machine_data->dma_id + 1,
64 machine_data->dma_channel_id[1], CCSR_GUTS_DMACR_DEV_SSI);
66 guts_set_pmuxcr_dma(machine_data->guts, machine_data->dma_id,
67 machine_data->dma_channel_id[0], 0);
68 guts_set_pmuxcr_dma(machine_data->guts, machine_data->dma_id,
69 machine_data->dma_channel_id[1], 0);
71 guts_set_pmuxcr_dma(machine_data->guts, 1, 0, 0);
72 guts_set_pmuxcr_dma(machine_data->guts, 1, 3, 0);
73 guts_set_pmuxcr_dma(machine_data->guts, 0, 3, 0);
75 switch (machine_data->ssi_id) {
76 case 0:
77 clrsetbits_be32(&machine_data->guts->pmuxcr,
78 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_SSI);
79 break;
80 case 1:
81 clrsetbits_be32(&machine_data->guts->pmuxcr,
82 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_SSI);
83 break;
86 return 0;
89 /**
90 * mpc8610_hpcd_startup: program the board with various hardware parameters
92 * This function takes board-specific information, like clock frequencies
93 * and serial data formats, and passes that information to the codec and
94 * transport drivers.
96 static int mpc8610_hpcd_startup(struct snd_pcm_substream *substream)
98 struct snd_soc_pcm_runtime *rtd = substream->private_data;
99 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
100 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
101 struct mpc8610_hpcd_data *machine_data =
102 rtd->socdev->dev->platform_data;
103 int ret = 0;
105 /* Tell the CPU driver what the serial protocol is. */
106 if (cpu_dai->dai_ops.set_fmt) {
107 ret = cpu_dai->dai_ops.set_fmt(cpu_dai,
108 machine_data->dai_format);
109 if (ret < 0) {
110 dev_err(substream->pcm->card->dev,
111 "could not set CPU driver audio format\n");
112 return ret;
116 /* Tell the codec driver what the serial protocol is. */
117 if (codec_dai->dai_ops.set_fmt) {
118 ret = codec_dai->dai_ops.set_fmt(codec_dai,
119 machine_data->dai_format);
120 if (ret < 0) {
121 dev_err(substream->pcm->card->dev,
122 "could not set codec driver audio format\n");
123 return ret;
128 * Tell the CPU driver what the clock frequency is, and whether it's a
129 * slave or master.
131 if (cpu_dai->dai_ops.set_sysclk) {
132 ret = cpu_dai->dai_ops.set_sysclk(cpu_dai, 0,
133 machine_data->clk_frequency,
134 machine_data->cpu_clk_direction);
135 if (ret < 0) {
136 dev_err(substream->pcm->card->dev,
137 "could not set CPU driver clock parameters\n");
138 return ret;
143 * Tell the codec driver what the MCLK frequency is, and whether it's
144 * a slave or master.
146 if (codec_dai->dai_ops.set_sysclk) {
147 ret = codec_dai->dai_ops.set_sysclk(codec_dai, 0,
148 machine_data->clk_frequency,
149 machine_data->codec_clk_direction);
150 if (ret < 0) {
151 dev_err(substream->pcm->card->dev,
152 "could not set codec driver clock params\n");
153 return ret;
157 return 0;
161 * mpc8610_hpcd_machine_remove: Remove the sound device
163 * This function is called to remove the sound device for one SSI. We
164 * de-program the DMACR and PMUXCR register.
166 int mpc8610_hpcd_machine_remove(struct platform_device *sound_device)
168 struct mpc8610_hpcd_data *machine_data =
169 sound_device->dev.platform_data;
171 /* Restore the signal routing */
173 guts_set_dmacr(machine_data->guts, machine_data->dma_id + 1,
174 machine_data->dma_channel_id[0], 0);
175 guts_set_dmacr(machine_data->guts, machine_data->dma_id + 1,
176 machine_data->dma_channel_id[1], 0);
178 switch (machine_data->ssi_id) {
179 case 0:
180 clrsetbits_be32(&machine_data->guts->pmuxcr,
181 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
182 break;
183 case 1:
184 clrsetbits_be32(&machine_data->guts->pmuxcr,
185 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
186 break;
189 return 0;
193 * mpc8610_hpcd_ops: ASoC fabric driver operations
195 static struct snd_soc_ops mpc8610_hpcd_ops = {
196 .startup = mpc8610_hpcd_startup,
200 * mpc8610_hpcd_machine: ASoC machine data
202 static struct snd_soc_machine mpc8610_hpcd_machine = {
203 .probe = mpc8610_hpcd_machine_probe,
204 .remove = mpc8610_hpcd_machine_remove,
205 .name = "MPC8610 HPCD",
206 .num_links = 1,
210 * mpc8610_hpcd_probe: OF probe function for the fabric driver
212 * This function gets called when an SSI node is found in the device tree.
214 * Although this is a fabric driver, the SSI node is the "master" node with
215 * respect to audio hardware connections. Therefore, we create a new ASoC
216 * device for each new SSI node that has a codec attached.
218 * FIXME: Currently, we only support one DMA controller, so if there are
219 * multiple SSI nodes with codecs, only the first will be supported.
221 * FIXME: Even if we did support multiple DMA controllers, we have no
222 * mechanism for assigning DMA controllers and channels to the individual
223 * SSI devices. We also probably aren't compatible with the generic Elo DMA
224 * device driver.
226 static int mpc8610_hpcd_probe(struct of_device *ofdev,
227 const struct of_device_id *match)
229 struct device_node *np = ofdev->node;
230 struct device_node *codec_np = NULL;
231 struct device_node *guts_np = NULL;
232 struct device_node *dma_np = NULL;
233 struct device_node *dma_channel_np = NULL;
234 const phandle *codec_ph;
235 const char *sprop;
236 const u32 *iprop;
237 struct resource res;
238 struct platform_device *sound_device = NULL;
239 struct mpc8610_hpcd_data *machine_data;
240 struct fsl_ssi_info ssi_info;
241 struct fsl_dma_info dma_info;
242 int ret = -ENODEV;
244 machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
245 if (!machine_data)
246 return -ENOMEM;
248 memset(&ssi_info, 0, sizeof(ssi_info));
249 memset(&dma_info, 0, sizeof(dma_info));
251 ssi_info.dev = &ofdev->dev;
254 * We are only interested in SSIs with a codec phandle in them, so let's
255 * make sure this SSI has one.
257 codec_ph = of_get_property(np, "codec-handle", NULL);
258 if (!codec_ph)
259 goto error;
261 codec_np = of_find_node_by_phandle(*codec_ph);
262 if (!codec_np)
263 goto error;
265 /* The MPC8610 HPCD only knows about the CS4270 codec, so reject
266 anything else. */
267 if (!of_device_is_compatible(codec_np, "cirrus,cs4270"))
268 goto error;
270 /* Get the device ID */
271 iprop = of_get_property(np, "cell-index", NULL);
272 if (!iprop) {
273 dev_err(&ofdev->dev, "cell-index property not found\n");
274 ret = -EINVAL;
275 goto error;
277 machine_data->ssi_id = *iprop;
278 ssi_info.id = *iprop;
280 /* Get the serial format and clock direction. */
281 sprop = of_get_property(np, "fsl,mode", NULL);
282 if (!sprop) {
283 dev_err(&ofdev->dev, "fsl,mode property not found\n");
284 ret = -EINVAL;
285 goto error;
288 if (strcasecmp(sprop, "i2s-slave") == 0) {
289 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
290 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
291 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
294 * In i2s-slave mode, the codec has its own clock source, so we
295 * need to get the frequency from the device tree and pass it to
296 * the codec driver.
298 iprop = of_get_property(codec_np, "clock-frequency", NULL);
299 if (!iprop || !*iprop) {
300 dev_err(&ofdev->dev, "codec bus-frequency property "
301 "is missing or invalid\n");
302 ret = -EINVAL;
303 goto error;
305 machine_data->clk_frequency = *iprop;
306 } else if (strcasecmp(sprop, "i2s-master") == 0) {
307 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
308 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
309 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
310 } else if (strcasecmp(sprop, "lj-slave") == 0) {
311 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
312 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
313 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
314 } else if (strcasecmp(sprop, "lj-master") == 0) {
315 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
316 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
317 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
318 <<<<<<< HEAD:sound/soc/fsl/mpc8610_hpcd.c
319 } else if (strcasecmp(sprop, "rj-master") == 0) {
320 =======
321 } else if (strcasecmp(sprop, "rj-slave") == 0) {
322 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:sound/soc/fsl/mpc8610_hpcd.c
323 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
324 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
325 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
326 } else if (strcasecmp(sprop, "rj-master") == 0) {
327 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
328 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
329 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
330 } else if (strcasecmp(sprop, "ac97-slave") == 0) {
331 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
332 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
333 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
334 } else if (strcasecmp(sprop, "ac97-master") == 0) {
335 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
336 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
337 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
338 } else {
339 dev_err(&ofdev->dev,
340 "unrecognized fsl,mode property \"%s\"\n", sprop);
341 ret = -EINVAL;
342 goto error;
345 if (!machine_data->clk_frequency) {
346 dev_err(&ofdev->dev, "unknown clock frequency\n");
347 ret = -EINVAL;
348 goto error;
351 /* Read the SSI information from the device tree */
352 ret = of_address_to_resource(np, 0, &res);
353 if (ret) {
354 dev_err(&ofdev->dev, "could not obtain SSI address\n");
355 goto error;
357 if (!res.start) {
358 dev_err(&ofdev->dev, "invalid SSI address\n");
359 goto error;
361 ssi_info.ssi_phys = res.start;
363 machine_data->ssi = ioremap(ssi_info.ssi_phys, sizeof(struct ccsr_ssi));
364 if (!machine_data->ssi) {
365 dev_err(&ofdev->dev, "could not map SSI address %x\n",
366 ssi_info.ssi_phys);
367 ret = -EINVAL;
368 goto error;
370 ssi_info.ssi = machine_data->ssi;
373 /* Get the IRQ of the SSI */
374 machine_data->ssi_irq = irq_of_parse_and_map(np, 0);
375 if (!machine_data->ssi_irq) {
376 dev_err(&ofdev->dev, "could not get SSI IRQ\n");
377 ret = -EINVAL;
378 goto error;
380 ssi_info.irq = machine_data->ssi_irq;
383 /* Map the global utilities registers. */
384 guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
385 if (!guts_np) {
386 dev_err(&ofdev->dev, "could not obtain address of GUTS\n");
387 ret = -EINVAL;
388 goto error;
390 machine_data->guts = of_iomap(guts_np, 0);
391 of_node_put(guts_np);
392 if (!machine_data->guts) {
393 dev_err(&ofdev->dev, "could not map GUTS\n");
394 ret = -EINVAL;
395 goto error;
398 /* Find the DMA channels to use. For now, we always use the first DMA
399 controller. */
400 for_each_compatible_node(dma_np, NULL, "fsl,mpc8610-dma") {
401 iprop = of_get_property(dma_np, "cell-index", NULL);
402 if (iprop && (*iprop == 0)) {
403 of_node_put(dma_np);
404 break;
407 if (!dma_np) {
408 dev_err(&ofdev->dev, "could not find DMA node\n");
409 ret = -EINVAL;
410 goto error;
412 machine_data->dma_id = *iprop;
415 * Find the DMA channels to use. For now, we always use DMA channel 0
416 * for playback, and DMA channel 1 for capture.
418 while ((dma_channel_np = of_get_next_child(dma_np, dma_channel_np))) {
419 iprop = of_get_property(dma_channel_np, "cell-index", NULL);
420 /* Is it DMA channel 0? */
421 if (iprop && (*iprop == 0)) {
422 /* dma_channel[0] and dma_irq[0] are for playback */
423 dma_info.dma_channel[0] = of_iomap(dma_channel_np, 0);
424 dma_info.dma_irq[0] =
425 irq_of_parse_and_map(dma_channel_np, 0);
426 machine_data->dma_channel_id[0] = *iprop;
427 continue;
429 if (iprop && (*iprop == 1)) {
430 /* dma_channel[1] and dma_irq[1] are for capture */
431 dma_info.dma_channel[1] = of_iomap(dma_channel_np, 0);
432 dma_info.dma_irq[1] =
433 irq_of_parse_and_map(dma_channel_np, 0);
434 machine_data->dma_channel_id[1] = *iprop;
435 continue;
438 if (!dma_info.dma_channel[0] || !dma_info.dma_channel[1] ||
439 !dma_info.dma_irq[0] || !dma_info.dma_irq[1]) {
440 dev_err(&ofdev->dev, "could not find DMA channels\n");
441 ret = -EINVAL;
442 goto error;
445 dma_info.ssi_stx_phys = ssi_info.ssi_phys +
446 offsetof(struct ccsr_ssi, stx0);
447 dma_info.ssi_srx_phys = ssi_info.ssi_phys +
448 offsetof(struct ccsr_ssi, srx0);
450 /* We have the DMA information, so tell the DMA driver what it is */
451 if (!fsl_dma_configure(&dma_info)) {
452 dev_err(&ofdev->dev, "could not instantiate DMA device\n");
453 ret = -EBUSY;
454 goto error;
458 * Initialize our DAI data structure. We should probably get this
459 * information from the device tree.
461 machine_data->dai.name = "CS4270";
462 machine_data->dai.stream_name = "CS4270";
464 machine_data->dai.cpu_dai = fsl_ssi_create_dai(&ssi_info);
465 machine_data->dai.codec_dai = &cs4270_dai; /* The codec_dai we want */
466 machine_data->dai.ops = &mpc8610_hpcd_ops;
468 mpc8610_hpcd_machine.dai_link = &machine_data->dai;
470 /* Allocate a new audio platform device structure */
471 sound_device = platform_device_alloc("soc-audio", -1);
472 if (!sound_device) {
473 dev_err(&ofdev->dev, "platform device allocation failed\n");
474 ret = -ENOMEM;
475 goto error;
478 machine_data->sound_devdata.machine = &mpc8610_hpcd_machine;
479 machine_data->sound_devdata.codec_dev = &soc_codec_device_cs4270;
480 machine_data->sound_devdata.platform = &fsl_soc_platform;
482 sound_device->dev.platform_data = machine_data;
485 /* Set the platform device and ASoC device to point to each other */
486 platform_set_drvdata(sound_device, &machine_data->sound_devdata);
488 machine_data->sound_devdata.dev = &sound_device->dev;
491 /* Tell ASoC to probe us. This will call mpc8610_hpcd_machine.probe(),
492 if it exists. */
493 ret = platform_device_add(sound_device);
495 if (ret) {
496 dev_err(&ofdev->dev, "platform device add failed\n");
497 goto error;
500 dev_set_drvdata(&ofdev->dev, sound_device);
502 return 0;
504 error:
505 of_node_put(codec_np);
506 of_node_put(guts_np);
507 of_node_put(dma_np);
508 of_node_put(dma_channel_np);
510 if (sound_device)
511 platform_device_unregister(sound_device);
513 if (machine_data->dai.cpu_dai)
514 fsl_ssi_destroy_dai(machine_data->dai.cpu_dai);
516 if (ssi_info.ssi)
517 iounmap(ssi_info.ssi);
519 if (ssi_info.irq)
520 irq_dispose_mapping(ssi_info.irq);
522 if (dma_info.dma_channel[0])
523 iounmap(dma_info.dma_channel[0]);
525 if (dma_info.dma_channel[1])
526 iounmap(dma_info.dma_channel[1]);
528 if (dma_info.dma_irq[0])
529 irq_dispose_mapping(dma_info.dma_irq[0]);
531 if (dma_info.dma_irq[1])
532 irq_dispose_mapping(dma_info.dma_irq[1]);
534 if (machine_data->guts)
535 iounmap(machine_data->guts);
537 kfree(machine_data);
539 return ret;
543 * mpc8610_hpcd_remove: remove the OF device
545 * This function is called when the OF device is removed.
547 static int mpc8610_hpcd_remove(struct of_device *ofdev)
549 struct platform_device *sound_device = dev_get_drvdata(&ofdev->dev);
550 struct mpc8610_hpcd_data *machine_data =
551 sound_device->dev.platform_data;
553 platform_device_unregister(sound_device);
555 if (machine_data->dai.cpu_dai)
556 fsl_ssi_destroy_dai(machine_data->dai.cpu_dai);
558 if (machine_data->ssi)
559 iounmap(machine_data->ssi);
561 if (machine_data->dma[0])
562 iounmap(machine_data->dma[0]);
564 if (machine_data->dma[1])
565 iounmap(machine_data->dma[1]);
567 if (machine_data->dma_irq[0])
568 irq_dispose_mapping(machine_data->dma_irq[0]);
570 if (machine_data->dma_irq[1])
571 irq_dispose_mapping(machine_data->dma_irq[1]);
573 if (machine_data->guts)
574 iounmap(machine_data->guts);
576 kfree(machine_data);
577 sound_device->dev.platform_data = NULL;
579 dev_set_drvdata(&ofdev->dev, NULL);
581 return 0;
584 static struct of_device_id mpc8610_hpcd_match[] = {
586 .compatible = "fsl,mpc8610-ssi",
590 MODULE_DEVICE_TABLE(of, mpc8610_hpcd_match);
592 static struct of_platform_driver mpc8610_hpcd_of_driver = {
593 .owner = THIS_MODULE,
594 .name = "mpc8610_hpcd",
595 .match_table = mpc8610_hpcd_match,
596 .probe = mpc8610_hpcd_probe,
597 .remove = mpc8610_hpcd_remove,
601 * mpc8610_hpcd_init: fabric driver initialization.
603 * This function is called when this module is loaded.
605 static int __init mpc8610_hpcd_init(void)
607 int ret;
609 printk(KERN_INFO "Freescale MPC8610 HPCD ALSA SoC fabric driver\n");
611 ret = of_register_platform_driver(&mpc8610_hpcd_of_driver);
613 if (ret)
614 printk(KERN_ERR
615 "mpc8610-hpcd: failed to register platform driver\n");
617 return ret;
621 * mpc8610_hpcd_exit: fabric driver exit
623 * This function is called when this driver is unloaded.
625 static void __exit mpc8610_hpcd_exit(void)
627 of_unregister_platform_driver(&mpc8610_hpcd_of_driver);
630 module_init(mpc8610_hpcd_init);
631 module_exit(mpc8610_hpcd_exit);
633 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
634 MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC fabric driver");
635 MODULE_LICENSE("GPL");