blob: c4ae3e096bb9154376a3a3760e3cf4897cad65a3 [file] [log] [blame]
Jon Smirl20d0e152009-05-26 08:34:10 -04001/*
2 * linux/sound/mpc5200-ac97.c -- AC97 support for the Freescale MPC52xx chip.
3 *
4 * Copyright (C) 2009 Jon Smirl, Digispeaker
5 * Author: Jon Smirl <jonsmirl@gmail.com>
6 *
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.
10 */
11
12#include <linux/module.h>
13#include <linux/of_device.h>
14#include <linux/of_platform.h>
John Bonesioed0f19b2009-07-21 13:15:40 -070015#include <linux/delay.h>
Jon Smirl20d0e152009-05-26 08:34:10 -040016
17#include <sound/pcm.h>
18#include <sound/pcm_params.h>
19#include <sound/soc.h>
20
21#include <asm/time.h>
22#include <asm/delay.h>
23#include <asm/mpc52xx_psc.h>
24
25#include "mpc5200_dma.h"
26#include "mpc5200_psc_ac97.h"
27
28#define DRV_NAME "mpc5200-psc-ac97"
29
30/* ALSA only supports a single AC97 device so static is recommend here */
31static struct psc_dma *psc_dma;
32
33static unsigned short psc_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
34{
Jon Smirlea8b27a2009-05-27 01:06:19 -040035 int status;
Jon Smirl20d0e152009-05-26 08:34:10 -040036 unsigned int val;
37
Grant Likely0827d6b2009-07-02 11:57:25 -060038 mutex_lock(&psc_dma->mutex);
39
Jon Smirl20d0e152009-05-26 08:34:10 -040040 /* Wait for command send status zero = ready */
Jon Smirlea8b27a2009-05-27 01:06:19 -040041 status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
42 MPC52xx_PSC_SR_CMDSEND), 100, 0);
43 if (status == 0) {
Jon Smirl20d0e152009-05-26 08:34:10 -040044 pr_err("timeout on ac97 bus (rdy)\n");
Grant Likely0827d6b2009-07-02 11:57:25 -060045 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040046 return -ENODEV;
47 }
Grant Likely07573532009-07-02 11:57:19 -060048
49 /* Force clear the data valid bit */
50 in_be32(&psc_dma->psc_regs->ac97_data);
51
Jon Smirl20d0e152009-05-26 08:34:10 -040052 /* Send the read */
53 out_be32(&psc_dma->psc_regs->ac97_cmd, (1<<31) | ((reg & 0x7f) << 24));
54
55 /* Wait for the answer */
Jon Smirlea8b27a2009-05-27 01:06:19 -040056 status = spin_event_timeout((in_be16(&psc_dma->psc_regs->sr_csr.status) &
57 MPC52xx_PSC_SR_DATA_VAL), 100, 0);
58 if (status == 0) {
Jon Smirl20d0e152009-05-26 08:34:10 -040059 pr_err("timeout on ac97 read (val) %x\n",
60 in_be16(&psc_dma->psc_regs->sr_csr.status));
Grant Likely0827d6b2009-07-02 11:57:25 -060061 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040062 return -ENODEV;
63 }
64 /* Get the data */
65 val = in_be32(&psc_dma->psc_regs->ac97_data);
66 if (((val >> 24) & 0x7f) != reg) {
67 pr_err("reg echo error on ac97 read\n");
Grant Likely0827d6b2009-07-02 11:57:25 -060068 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040069 return -ENODEV;
70 }
71 val = (val >> 8) & 0xffff;
72
Grant Likely0827d6b2009-07-02 11:57:25 -060073 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040074 return (unsigned short) val;
75}
76
77static void psc_ac97_write(struct snd_ac97 *ac97,
78 unsigned short reg, unsigned short val)
79{
Jon Smirlea8b27a2009-05-27 01:06:19 -040080 int status;
Jon Smirl20d0e152009-05-26 08:34:10 -040081
Grant Likely0827d6b2009-07-02 11:57:25 -060082 mutex_lock(&psc_dma->mutex);
83
Jon Smirl20d0e152009-05-26 08:34:10 -040084 /* Wait for command status zero = ready */
Jon Smirlea8b27a2009-05-27 01:06:19 -040085 status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
86 MPC52xx_PSC_SR_CMDSEND), 100, 0);
87 if (status == 0) {
Jon Smirl20d0e152009-05-26 08:34:10 -040088 pr_err("timeout on ac97 bus (write)\n");
Grant Likely0827d6b2009-07-02 11:57:25 -060089 goto out;
Jon Smirl20d0e152009-05-26 08:34:10 -040090 }
91 /* Write data */
92 out_be32(&psc_dma->psc_regs->ac97_cmd,
93 ((reg & 0x7f) << 24) | (val << 8));
Grant Likely0827d6b2009-07-02 11:57:25 -060094
95 out:
96 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040097}
98
99static void psc_ac97_warm_reset(struct snd_ac97 *ac97)
100{
Jon Smirl20d0e152009-05-26 08:34:10 -0400101 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
102
103 out_be32(&regs->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);
Jon Smirlea8b27a2009-05-27 01:06:19 -0400104 udelay(3);
Jon Smirl20d0e152009-05-26 08:34:10 -0400105 out_be32(&regs->sicr, psc_dma->sicr);
106}
107
108static void psc_ac97_cold_reset(struct snd_ac97 *ac97)
109{
Jon Smirl20d0e152009-05-26 08:34:10 -0400110 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
111
112 /* Do a cold reset */
113 out_8(&regs->op1, MPC52xx_PSC_OP_RES);
Jon Smirlea8b27a2009-05-27 01:06:19 -0400114 udelay(10);
Jon Smirl20d0e152009-05-26 08:34:10 -0400115 out_8(&regs->op0, MPC52xx_PSC_OP_RES);
John Bonesioed0f19b2009-07-21 13:15:40 -0700116 msleep(1);
Jon Smirl20d0e152009-05-26 08:34:10 -0400117 psc_ac97_warm_reset(ac97);
118}
119
120struct snd_ac97_bus_ops soc_ac97_ops = {
121 .read = psc_ac97_read,
122 .write = psc_ac97_write,
123 .reset = psc_ac97_cold_reset,
124 .warm_reset = psc_ac97_warm_reset,
125};
126EXPORT_SYMBOL_GPL(soc_ac97_ops);
127
128static int psc_ac97_hw_analog_params(struct snd_pcm_substream *substream,
129 struct snd_pcm_hw_params *params,
130 struct snd_soc_dai *cpu_dai)
131{
132 struct psc_dma *psc_dma = cpu_dai->private_data;
133
134 dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
135 " periods=%i buffer_size=%i buffer_bytes=%i channels=%i"
136 " rate=%i format=%i\n",
137 __func__, substream, params_period_size(params),
138 params_period_bytes(params), params_periods(params),
139 params_buffer_size(params), params_buffer_bytes(params),
140 params_channels(params), params_rate(params),
141 params_format(params));
142
143
144 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
145 if (params_channels(params) == 1)
146 psc_dma->slots |= 0x00000100;
147 else
148 psc_dma->slots |= 0x00000300;
149 } else {
150 if (params_channels(params) == 1)
151 psc_dma->slots |= 0x01000000;
152 else
153 psc_dma->slots |= 0x03000000;
154 }
155 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
156
157 return 0;
158}
159
160static int psc_ac97_hw_digital_params(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params,
162 struct snd_soc_dai *cpu_dai)
163{
164 struct psc_dma *psc_dma = cpu_dai->private_data;
165
166 if (params_channels(params) == 1)
167 out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
168 else
169 out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);
170
171 return 0;
172}
173
174static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
175 struct snd_soc_dai *dai)
176{
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
178 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
179
180 switch (cmd) {
181 case SNDRV_PCM_TRIGGER_STOP:
182 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
183 psc_dma->slots &= 0xFFFF0000;
184 else
185 psc_dma->slots &= 0x0000FFFF;
186
187 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
188 break;
189 }
190 return 0;
191}
192
193static int psc_ac97_probe(struct platform_device *pdev,
194 struct snd_soc_dai *cpu_dai)
195{
196 struct psc_dma *psc_dma = cpu_dai->private_data;
197 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
198
199 /* Go */
200 out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
201 return 0;
202}
203
204/* ---------------------------------------------------------------------
205 * ALSA SoC Bindings
206 *
207 * - Digital Audio Interface (DAI) template
208 * - create/destroy dai hooks
209 */
210
211/**
212 * psc_ac97_dai_template: template CPU Digital Audio Interface
213 */
214static struct snd_soc_dai_ops psc_ac97_analog_ops = {
215 .hw_params = psc_ac97_hw_analog_params,
216 .trigger = psc_ac97_trigger,
217};
218
219static struct snd_soc_dai_ops psc_ac97_digital_ops = {
220 .hw_params = psc_ac97_hw_digital_params,
221};
222
223struct snd_soc_dai psc_ac97_dai[] = {
224{
225 .name = "AC97",
226 .ac97_control = 1,
227 .probe = psc_ac97_probe,
228 .playback = {
229 .channels_min = 1,
230 .channels_max = 6,
231 .rates = SNDRV_PCM_RATE_8000_48000,
232 .formats = SNDRV_PCM_FMTBIT_S32_BE,
233 },
234 .capture = {
235 .channels_min = 1,
236 .channels_max = 2,
237 .rates = SNDRV_PCM_RATE_8000_48000,
238 .formats = SNDRV_PCM_FMTBIT_S32_BE,
239 },
240 .ops = &psc_ac97_analog_ops,
241},
242{
243 .name = "SPDIF",
244 .ac97_control = 1,
245 .playback = {
246 .channels_min = 1,
247 .channels_max = 2,
248 .rates = SNDRV_PCM_RATE_32000 | \
249 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
250 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
251 },
252 .ops = &psc_ac97_digital_ops,
253} };
254EXPORT_SYMBOL_GPL(psc_ac97_dai);
255
256
257
258/* ---------------------------------------------------------------------
259 * OF platform bus binding code:
260 * - Probe/remove operations
261 * - OF device match table
262 */
263static int __devinit psc_ac97_of_probe(struct of_device *op,
264 const struct of_device_id *match)
265{
266 int rc, i;
267 struct snd_ac97 ac97;
268 struct mpc52xx_psc __iomem *regs;
269
270 rc = mpc5200_audio_dma_create(op);
271 if (rc != 0)
272 return rc;
273
274 for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
275 psc_ac97_dai[i].dev = &op->dev;
276
277 rc = snd_soc_register_dais(psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
278 if (rc != 0) {
279 dev_err(&op->dev, "Failed to register DAI\n");
280 return rc;
281 }
282
283 psc_dma = dev_get_drvdata(&op->dev);
284 regs = psc_dma->psc_regs;
285 ac97.private_data = psc_dma;
286
287 for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
288 psc_ac97_dai[i].private_data = psc_dma;
289
290 psc_dma->imr = 0;
291 out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
292
293 /* Configure the serial interface mode to AC97 */
294 psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
295 out_be32(&regs->sicr, psc_dma->sicr);
296
297 /* No slots active */
298 out_be32(&regs->ac97_slots, 0x00000000);
299
300 return 0;
301}
302
303static int __devexit psc_ac97_of_remove(struct of_device *op)
304{
305 return mpc5200_audio_dma_destroy(op);
306}
307
308/* Match table for of_platform binding */
309static struct of_device_id psc_ac97_match[] __devinitdata = {
310 { .compatible = "fsl,mpc5200-psc-ac97", },
311 { .compatible = "fsl,mpc5200b-psc-ac97", },
312 {}
313};
314MODULE_DEVICE_TABLE(of, psc_ac97_match);
315
316static struct of_platform_driver psc_ac97_driver = {
317 .match_table = psc_ac97_match,
318 .probe = psc_ac97_of_probe,
319 .remove = __devexit_p(psc_ac97_of_remove),
320 .driver = {
321 .name = "mpc5200-psc-ac97",
322 .owner = THIS_MODULE,
323 },
324};
325
326/* ---------------------------------------------------------------------
327 * Module setup and teardown; simply register the of_platform driver
328 * for the PSC in AC97 mode.
329 */
330static int __init psc_ac97_init(void)
331{
332 return of_register_platform_driver(&psc_ac97_driver);
333}
334module_init(psc_ac97_init);
335
336static void __exit psc_ac97_exit(void)
337{
338 of_unregister_platform_driver(&psc_ac97_driver);
339}
340module_exit(psc_ac97_exit);
341
342MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
343MODULE_DESCRIPTION("mpc5200 AC97 module");
344MODULE_LICENSE("GPL");
345