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 spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
39 MPC52xx_PSC_SR_CMDSEND), 100, 0, rc);
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 spin_event_timeout((in_be16(&psc_dma->psc_regs->sr_csr.status) &
49 MPC52xx_PSC_SR_DATA_VAL), 100, 0, rc);
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 spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
73 MPC52xx_PSC_SR_CMDSEND), 100, 0, rc);
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)
86 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
88 out_be32(®s->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);
89 spin_event_timeout(0, 3, 0, rc);
90 out_be32(®s->sicr, psc_dma->sicr);
93 static void psc_ac97_cold_reset(struct snd_ac97 *ac97)
96 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
99 out_8(®s->op1, MPC52xx_PSC_OP_RES);
100 spin_event_timeout(0, 10, 0, rc);
101 out_8(®s->op0, MPC52xx_PSC_OP_RES);
102 spin_event_timeout(0, 50, 0, rc);
103 psc_ac97_warm_reset(ac97);
106 struct snd_ac97_bus_ops soc_ac97_ops = {
107 .read = psc_ac97_read,
108 .write = psc_ac97_write,
109 .reset = psc_ac97_cold_reset,
110 .warm_reset = psc_ac97_warm_reset,
112 EXPORT_SYMBOL_GPL(soc_ac97_ops);
114 static int psc_ac97_hw_analog_params(struct snd_pcm_substream *substream,
115 struct snd_pcm_hw_params *params,
116 struct snd_soc_dai *cpu_dai)
118 struct psc_dma *psc_dma = cpu_dai->private_data;
120 dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
121 " periods=%i buffer_size=%i buffer_bytes=%i channels=%i"
122 " rate=%i format=%i\n",
123 __func__, substream, params_period_size(params),
124 params_period_bytes(params), params_periods(params),
125 params_buffer_size(params), params_buffer_bytes(params),
126 params_channels(params), params_rate(params),
127 params_format(params));
130 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
131 if (params_channels(params) == 1)
132 psc_dma->slots |= 0x00000100;
134 psc_dma->slots |= 0x00000300;
136 if (params_channels(params) == 1)
137 psc_dma->slots |= 0x01000000;
139 psc_dma->slots |= 0x03000000;
141 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
146 static int psc_ac97_hw_digital_params(struct snd_pcm_substream *substream,
147 struct snd_pcm_hw_params *params,
148 struct snd_soc_dai *cpu_dai)
150 struct psc_dma *psc_dma = cpu_dai->private_data;
152 if (params_channels(params) == 1)
153 out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
155 out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);
160 static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
161 struct snd_soc_dai *dai)
163 struct snd_soc_pcm_runtime *rtd = substream->private_data;
164 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
167 case SNDRV_PCM_TRIGGER_STOP:
168 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
169 psc_dma->slots &= 0xFFFF0000;
171 psc_dma->slots &= 0x0000FFFF;
173 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
179 static int psc_ac97_probe(struct platform_device *pdev,
180 struct snd_soc_dai *cpu_dai)
182 struct psc_dma *psc_dma = cpu_dai->private_data;
183 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
186 out_8(®s->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
190 /* ---------------------------------------------------------------------
193 * - Digital Audio Interface (DAI) template
194 * - create/destroy dai hooks
198 * psc_ac97_dai_template: template CPU Digital Audio Interface
200 static struct snd_soc_dai_ops psc_ac97_analog_ops = {
201 .hw_params = psc_ac97_hw_analog_params,
202 .trigger = psc_ac97_trigger,
205 static struct snd_soc_dai_ops psc_ac97_digital_ops = {
206 .hw_params = psc_ac97_hw_digital_params,
209 struct snd_soc_dai psc_ac97_dai[] = {
213 .probe = psc_ac97_probe,
217 .rates = SNDRV_PCM_RATE_8000_48000,
218 .formats = SNDRV_PCM_FMTBIT_S32_BE,
223 .rates = SNDRV_PCM_RATE_8000_48000,
224 .formats = SNDRV_PCM_FMTBIT_S32_BE,
226 .ops = &psc_ac97_analog_ops,
234 .rates = SNDRV_PCM_RATE_32000 | \
235 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
236 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
238 .ops = &psc_ac97_digital_ops,
240 EXPORT_SYMBOL_GPL(psc_ac97_dai);
244 /* ---------------------------------------------------------------------
245 * OF platform bus binding code:
246 * - Probe/remove operations
247 * - OF device match table
249 static int __devinit psc_ac97_of_probe(struct of_device *op,
250 const struct of_device_id *match)
253 struct snd_ac97 ac97;
254 struct mpc52xx_psc __iomem *regs;
256 rc = mpc5200_audio_dma_create(op);
260 for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
261 psc_ac97_dai[i].dev = &op->dev;
263 rc = snd_soc_register_dais(psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
265 dev_err(&op->dev, "Failed to register DAI\n");
269 psc_dma = dev_get_drvdata(&op->dev);
270 regs = psc_dma->psc_regs;
271 ac97.private_data = psc_dma;
273 for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
274 psc_ac97_dai[i].private_data = psc_dma;
277 out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
279 /* Configure the serial interface mode to AC97 */
280 psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
281 out_be32(®s->sicr, psc_dma->sicr);
283 /* No slots active */
284 out_be32(®s->ac97_slots, 0x00000000);
289 static int __devexit psc_ac97_of_remove(struct of_device *op)
291 return mpc5200_audio_dma_destroy(op);
294 /* Match table for of_platform binding */
295 static struct of_device_id psc_ac97_match[] __devinitdata = {
296 { .compatible = "fsl,mpc5200-psc-ac97", },
297 { .compatible = "fsl,mpc5200b-psc-ac97", },
300 MODULE_DEVICE_TABLE(of, psc_ac97_match);
302 static struct of_platform_driver psc_ac97_driver = {
303 .match_table = psc_ac97_match,
304 .probe = psc_ac97_of_probe,
305 .remove = __devexit_p(psc_ac97_of_remove),
307 .name = "mpc5200-psc-ac97",
308 .owner = THIS_MODULE,
312 /* ---------------------------------------------------------------------
313 * Module setup and teardown; simply register the of_platform driver
314 * for the PSC in AC97 mode.
316 static int __init psc_ac97_init(void)
318 return of_register_platform_driver(&psc_ac97_driver);
320 module_init(psc_ac97_init);
322 static void __exit psc_ac97_exit(void)
324 of_unregister_platform_driver(&psc_ac97_driver);
326 module_exit(psc_ac97_exit);
328 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
329 MODULE_DESCRIPTION("mpc5200 AC97 module");
330 MODULE_LICENSE("GPL");