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 out_be32(&psc_dma->psc_regs->ac97_cmd, (1<<31) | ((reg & 0x7f) << 24));
47 /* Wait for the answer */
48 status = spin_event_timeout((in_be16(&psc_dma->psc_regs->sr_csr.status) &
49 MPC52xx_PSC_SR_DATA_VAL), 100, 0);
51 pr_err("timeout on ac97 read (val) %x\n",
52 in_be16(&psc_dma->psc_regs->sr_csr.status));
56 val = in_be32(&psc_dma->psc_regs->ac97_data);
57 if (((val >> 24) & 0x7f) != reg) {
58 pr_err("reg echo error on ac97 read\n");
61 val = (val >> 8) & 0xffff;
63 return (unsigned short) val;
66 static void psc_ac97_write(struct snd_ac97 *ac97,
67 unsigned short reg, unsigned short val)
71 /* Wait for command status zero = ready */
72 status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
73 MPC52xx_PSC_SR_CMDSEND), 100, 0);
75 pr_err("timeout on ac97 bus (write)\n");
79 out_be32(&psc_dma->psc_regs->ac97_cmd,
80 ((reg & 0x7f) << 24) | (val << 8));
83 static void psc_ac97_warm_reset(struct snd_ac97 *ac97)
85 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
87 out_be32(®s->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);
89 out_be32(®s->sicr, psc_dma->sicr);
92 static void psc_ac97_cold_reset(struct snd_ac97 *ac97)
94 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
97 out_8(®s->op1, MPC52xx_PSC_OP_RES);
99 out_8(®s->op0, MPC52xx_PSC_OP_RES);
101 psc_ac97_warm_reset(ac97);
104 struct snd_ac97_bus_ops soc_ac97_ops = {
105 .read = psc_ac97_read,
106 .write = psc_ac97_write,
107 .reset = psc_ac97_cold_reset,
108 .warm_reset = psc_ac97_warm_reset,
110 EXPORT_SYMBOL_GPL(soc_ac97_ops);
112 static int psc_ac97_hw_analog_params(struct snd_pcm_substream *substream,
113 struct snd_pcm_hw_params *params,
114 struct snd_soc_dai *cpu_dai)
116 struct psc_dma *psc_dma = cpu_dai->private_data;
118 dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
119 " periods=%i buffer_size=%i buffer_bytes=%i channels=%i"
120 " rate=%i format=%i\n",
121 __func__, substream, params_period_size(params),
122 params_period_bytes(params), params_periods(params),
123 params_buffer_size(params), params_buffer_bytes(params),
124 params_channels(params), params_rate(params),
125 params_format(params));
128 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
129 if (params_channels(params) == 1)
130 psc_dma->slots |= 0x00000100;
132 psc_dma->slots |= 0x00000300;
134 if (params_channels(params) == 1)
135 psc_dma->slots |= 0x01000000;
137 psc_dma->slots |= 0x03000000;
139 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
144 static int psc_ac97_hw_digital_params(struct snd_pcm_substream *substream,
145 struct snd_pcm_hw_params *params,
146 struct snd_soc_dai *cpu_dai)
148 struct psc_dma *psc_dma = cpu_dai->private_data;
150 if (params_channels(params) == 1)
151 out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
153 out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);
158 static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
159 struct snd_soc_dai *dai)
161 struct snd_soc_pcm_runtime *rtd = substream->private_data;
162 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
165 case SNDRV_PCM_TRIGGER_STOP:
166 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
167 psc_dma->slots &= 0xFFFF0000;
169 psc_dma->slots &= 0x0000FFFF;
171 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
177 static int psc_ac97_probe(struct platform_device *pdev,
178 struct snd_soc_dai *cpu_dai)
180 struct psc_dma *psc_dma = cpu_dai->private_data;
181 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
184 out_8(®s->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
188 /* ---------------------------------------------------------------------
191 * - Digital Audio Interface (DAI) template
192 * - create/destroy dai hooks
196 * psc_ac97_dai_template: template CPU Digital Audio Interface
198 static struct snd_soc_dai_ops psc_ac97_analog_ops = {
199 .hw_params = psc_ac97_hw_analog_params,
200 .trigger = psc_ac97_trigger,
203 static struct snd_soc_dai_ops psc_ac97_digital_ops = {
204 .hw_params = psc_ac97_hw_digital_params,
207 struct snd_soc_dai psc_ac97_dai[] = {
211 .probe = psc_ac97_probe,
215 .rates = SNDRV_PCM_RATE_8000_48000,
216 .formats = SNDRV_PCM_FMTBIT_S32_BE,
221 .rates = SNDRV_PCM_RATE_8000_48000,
222 .formats = SNDRV_PCM_FMTBIT_S32_BE,
224 .ops = &psc_ac97_analog_ops,
232 .rates = SNDRV_PCM_RATE_32000 | \
233 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
234 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
236 .ops = &psc_ac97_digital_ops,
238 EXPORT_SYMBOL_GPL(psc_ac97_dai);
242 /* ---------------------------------------------------------------------
243 * OF platform bus binding code:
244 * - Probe/remove operations
245 * - OF device match table
247 static int __devinit psc_ac97_of_probe(struct of_device *op,
248 const struct of_device_id *match)
251 struct snd_ac97 ac97;
252 struct mpc52xx_psc __iomem *regs;
254 rc = mpc5200_audio_dma_create(op);
258 for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
259 psc_ac97_dai[i].dev = &op->dev;
261 rc = snd_soc_register_dais(psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
263 dev_err(&op->dev, "Failed to register DAI\n");
267 psc_dma = dev_get_drvdata(&op->dev);
268 regs = psc_dma->psc_regs;
269 ac97.private_data = psc_dma;
271 for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
272 psc_ac97_dai[i].private_data = psc_dma;
275 out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
277 /* Configure the serial interface mode to AC97 */
278 psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
279 out_be32(®s->sicr, psc_dma->sicr);
281 /* No slots active */
282 out_be32(®s->ac97_slots, 0x00000000);
287 static int __devexit psc_ac97_of_remove(struct of_device *op)
289 return mpc5200_audio_dma_destroy(op);
292 /* Match table for of_platform binding */
293 static struct of_device_id psc_ac97_match[] __devinitdata = {
294 { .compatible = "fsl,mpc5200-psc-ac97", },
295 { .compatible = "fsl,mpc5200b-psc-ac97", },
298 MODULE_DEVICE_TABLE(of, psc_ac97_match);
300 static struct of_platform_driver psc_ac97_driver = {
301 .match_table = psc_ac97_match,
302 .probe = psc_ac97_of_probe,
303 .remove = __devexit_p(psc_ac97_of_remove),
305 .name = "mpc5200-psc-ac97",
306 .owner = THIS_MODULE,
310 /* ---------------------------------------------------------------------
311 * Module setup and teardown; simply register the of_platform driver
312 * for the PSC in AC97 mode.
314 static int __init psc_ac97_init(void)
316 return of_register_platform_driver(&psc_ac97_driver);
318 module_init(psc_ac97_init);
320 static void __exit psc_ac97_exit(void)
322 of_unregister_platform_driver(&psc_ac97_driver);
324 module_exit(psc_ac97_exit);
326 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
327 MODULE_DESCRIPTION("mpc5200 AC97 module");
328 MODULE_LICENSE("GPL");