2 * linux/sound/mpc5200-ac97.c -- AC97 support for the Freescale MPC52xx chip.
4 * Copyright (C) 2009 Jon Smirl, Digispeaker
5 * Author: Jon Smirl <jonsmirl@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/of_platform.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
21 #include <asm/delay.h>
22 #include <asm/mpc52xx_psc.h>
24 #include "mpc5200_dma.h"
25 #include "mpc5200_psc_ac97.h"
27 #define DRV_NAME "mpc5200-psc-ac97"
29 /* ALSA only supports a single AC97 device so static is recommend here */
30 static struct psc_dma *psc_dma;
32 static unsigned short psc_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
37 /* Wait for command send status zero = ready */
38 status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
39 MPC52xx_PSC_SR_CMDSEND), 100, 0);
41 pr_err("timeout on ac97 bus (rdy)\n");
45 /* Force clear the data valid bit */
46 in_be32(&psc_dma->psc_regs->ac97_data);
49 out_be32(&psc_dma->psc_regs->ac97_cmd, (1<<31) | ((reg & 0x7f) << 24));
51 /* Wait for the answer */
52 status = spin_event_timeout((in_be16(&psc_dma->psc_regs->sr_csr.status) &
53 MPC52xx_PSC_SR_DATA_VAL), 100, 0);
55 pr_err("timeout on ac97 read (val) %x\n",
56 in_be16(&psc_dma->psc_regs->sr_csr.status));
60 val = in_be32(&psc_dma->psc_regs->ac97_data);
61 if (((val >> 24) & 0x7f) != reg) {
62 pr_err("reg echo error on ac97 read\n");
65 val = (val >> 8) & 0xffff;
67 return (unsigned short) val;
70 static void psc_ac97_write(struct snd_ac97 *ac97,
71 unsigned short reg, unsigned short val)
75 /* Wait for command status zero = ready */
76 status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
77 MPC52xx_PSC_SR_CMDSEND), 100, 0);
79 pr_err("timeout on ac97 bus (write)\n");
83 out_be32(&psc_dma->psc_regs->ac97_cmd,
84 ((reg & 0x7f) << 24) | (val << 8));
87 static void psc_ac97_warm_reset(struct snd_ac97 *ac97)
89 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
91 out_be32(®s->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);
93 out_be32(®s->sicr, psc_dma->sicr);
96 static void psc_ac97_cold_reset(struct snd_ac97 *ac97)
98 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
100 /* Do a cold reset */
101 out_8(®s->op1, MPC52xx_PSC_OP_RES);
103 out_8(®s->op0, MPC52xx_PSC_OP_RES);
105 psc_ac97_warm_reset(ac97);
108 struct snd_ac97_bus_ops soc_ac97_ops = {
109 .read = psc_ac97_read,
110 .write = psc_ac97_write,
111 .reset = psc_ac97_cold_reset,
112 .warm_reset = psc_ac97_warm_reset,
114 EXPORT_SYMBOL_GPL(soc_ac97_ops);
116 static int psc_ac97_hw_analog_params(struct snd_pcm_substream *substream,
117 struct snd_pcm_hw_params *params,
118 struct snd_soc_dai *cpu_dai)
120 struct psc_dma *psc_dma = cpu_dai->private_data;
122 dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
123 " periods=%i buffer_size=%i buffer_bytes=%i channels=%i"
124 " rate=%i format=%i\n",
125 __func__, substream, params_period_size(params),
126 params_period_bytes(params), params_periods(params),
127 params_buffer_size(params), params_buffer_bytes(params),
128 params_channels(params), params_rate(params),
129 params_format(params));
132 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
133 if (params_channels(params) == 1)
134 psc_dma->slots |= 0x00000100;
136 psc_dma->slots |= 0x00000300;
138 if (params_channels(params) == 1)
139 psc_dma->slots |= 0x01000000;
141 psc_dma->slots |= 0x03000000;
143 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
148 static int psc_ac97_hw_digital_params(struct snd_pcm_substream *substream,
149 struct snd_pcm_hw_params *params,
150 struct snd_soc_dai *cpu_dai)
152 struct psc_dma *psc_dma = cpu_dai->private_data;
154 if (params_channels(params) == 1)
155 out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
157 out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);
162 static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
163 struct snd_soc_dai *dai)
165 struct snd_soc_pcm_runtime *rtd = substream->private_data;
166 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
169 case SNDRV_PCM_TRIGGER_STOP:
170 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
171 psc_dma->slots &= 0xFFFF0000;
173 psc_dma->slots &= 0x0000FFFF;
175 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
181 static int psc_ac97_probe(struct platform_device *pdev,
182 struct snd_soc_dai *cpu_dai)
184 struct psc_dma *psc_dma = cpu_dai->private_data;
185 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
188 out_8(®s->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
192 /* ---------------------------------------------------------------------
195 * - Digital Audio Interface (DAI) template
196 * - create/destroy dai hooks
200 * psc_ac97_dai_template: template CPU Digital Audio Interface
202 static struct snd_soc_dai_ops psc_ac97_analog_ops = {
203 .hw_params = psc_ac97_hw_analog_params,
204 .trigger = psc_ac97_trigger,
207 static struct snd_soc_dai_ops psc_ac97_digital_ops = {
208 .hw_params = psc_ac97_hw_digital_params,
211 struct snd_soc_dai psc_ac97_dai[] = {
215 .probe = psc_ac97_probe,
219 .rates = SNDRV_PCM_RATE_8000_48000,
220 .formats = SNDRV_PCM_FMTBIT_S32_BE,
225 .rates = SNDRV_PCM_RATE_8000_48000,
226 .formats = SNDRV_PCM_FMTBIT_S32_BE,
228 .ops = &psc_ac97_analog_ops,
236 .rates = SNDRV_PCM_RATE_32000 | \
237 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
238 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
240 .ops = &psc_ac97_digital_ops,
242 EXPORT_SYMBOL_GPL(psc_ac97_dai);
246 /* ---------------------------------------------------------------------
247 * OF platform bus binding code:
248 * - Probe/remove operations
249 * - OF device match table
251 static int __devinit psc_ac97_of_probe(struct of_device *op,
252 const struct of_device_id *match)
255 struct snd_ac97 ac97;
256 struct mpc52xx_psc __iomem *regs;
258 rc = mpc5200_audio_dma_create(op);
262 for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
263 psc_ac97_dai[i].dev = &op->dev;
265 rc = snd_soc_register_dais(psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
267 dev_err(&op->dev, "Failed to register DAI\n");
271 psc_dma = dev_get_drvdata(&op->dev);
272 regs = psc_dma->psc_regs;
273 ac97.private_data = psc_dma;
275 for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
276 psc_ac97_dai[i].private_data = psc_dma;
279 out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
281 /* Configure the serial interface mode to AC97 */
282 psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
283 out_be32(®s->sicr, psc_dma->sicr);
285 /* No slots active */
286 out_be32(®s->ac97_slots, 0x00000000);
291 static int __devexit psc_ac97_of_remove(struct of_device *op)
293 return mpc5200_audio_dma_destroy(op);
296 /* Match table for of_platform binding */
297 static struct of_device_id psc_ac97_match[] __devinitdata = {
298 { .compatible = "fsl,mpc5200-psc-ac97", },
299 { .compatible = "fsl,mpc5200b-psc-ac97", },
302 MODULE_DEVICE_TABLE(of, psc_ac97_match);
304 static struct of_platform_driver psc_ac97_driver = {
305 .match_table = psc_ac97_match,
306 .probe = psc_ac97_of_probe,
307 .remove = __devexit_p(psc_ac97_of_remove),
309 .name = "mpc5200-psc-ac97",
310 .owner = THIS_MODULE,
314 /* ---------------------------------------------------------------------
315 * Module setup and teardown; simply register the of_platform driver
316 * for the PSC in AC97 mode.
318 static int __init psc_ac97_init(void)
320 return of_register_platform_driver(&psc_ac97_driver);
322 module_init(psc_ac97_init);
324 static void __exit psc_ac97_exit(void)
326 of_unregister_platform_driver(&psc_ac97_driver);
328 module_exit(psc_ac97_exit);
330 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
331 MODULE_DESCRIPTION("mpc5200 AC97 module");
332 MODULE_LICENSE("GPL");