blob: ffa00a2eb7704439341debdcf7cafe2310e4ce94 [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>
Eric Millbrandt949ad0a2010-08-06 20:49:19 -060023#include <asm/mpc52xx.h>
Jon Smirl20d0e152009-05-26 08:34:10 -040024#include <asm/mpc52xx_psc.h>
25
26#include "mpc5200_dma.h"
27#include "mpc5200_psc_ac97.h"
28
29#define DRV_NAME "mpc5200-psc-ac97"
30
31/* ALSA only supports a single AC97 device so static is recommend here */
32static struct psc_dma *psc_dma;
33
34static unsigned short psc_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
35{
Jon Smirlea8b27a2009-05-27 01:06:19 -040036 int status;
Jon Smirl20d0e152009-05-26 08:34:10 -040037 unsigned int val;
38
Grant Likely0827d6b2009-07-02 11:57:25 -060039 mutex_lock(&psc_dma->mutex);
40
Jon Smirl20d0e152009-05-26 08:34:10 -040041 /* Wait for command send status zero = ready */
Jon Smirlea8b27a2009-05-27 01:06:19 -040042 status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
43 MPC52xx_PSC_SR_CMDSEND), 100, 0);
44 if (status == 0) {
Jon Smirl20d0e152009-05-26 08:34:10 -040045 pr_err("timeout on ac97 bus (rdy)\n");
Grant Likely0827d6b2009-07-02 11:57:25 -060046 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040047 return -ENODEV;
48 }
Grant Likely07573532009-07-02 11:57:19 -060049
50 /* Force clear the data valid bit */
51 in_be32(&psc_dma->psc_regs->ac97_data);
52
Jon Smirl20d0e152009-05-26 08:34:10 -040053 /* Send the read */
54 out_be32(&psc_dma->psc_regs->ac97_cmd, (1<<31) | ((reg & 0x7f) << 24));
55
56 /* Wait for the answer */
Jon Smirlea8b27a2009-05-27 01:06:19 -040057 status = spin_event_timeout((in_be16(&psc_dma->psc_regs->sr_csr.status) &
58 MPC52xx_PSC_SR_DATA_VAL), 100, 0);
59 if (status == 0) {
Jon Smirl20d0e152009-05-26 08:34:10 -040060 pr_err("timeout on ac97 read (val) %x\n",
61 in_be16(&psc_dma->psc_regs->sr_csr.status));
Grant Likely0827d6b2009-07-02 11:57:25 -060062 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040063 return -ENODEV;
64 }
65 /* Get the data */
66 val = in_be32(&psc_dma->psc_regs->ac97_data);
67 if (((val >> 24) & 0x7f) != reg) {
68 pr_err("reg echo error on ac97 read\n");
Grant Likely0827d6b2009-07-02 11:57:25 -060069 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040070 return -ENODEV;
71 }
72 val = (val >> 8) & 0xffff;
73
Grant Likely0827d6b2009-07-02 11:57:25 -060074 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040075 return (unsigned short) val;
76}
77
78static void psc_ac97_write(struct snd_ac97 *ac97,
79 unsigned short reg, unsigned short val)
80{
Jon Smirlea8b27a2009-05-27 01:06:19 -040081 int status;
Jon Smirl20d0e152009-05-26 08:34:10 -040082
Grant Likely0827d6b2009-07-02 11:57:25 -060083 mutex_lock(&psc_dma->mutex);
84
Jon Smirl20d0e152009-05-26 08:34:10 -040085 /* Wait for command status zero = ready */
Jon Smirlea8b27a2009-05-27 01:06:19 -040086 status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
87 MPC52xx_PSC_SR_CMDSEND), 100, 0);
88 if (status == 0) {
Jon Smirl20d0e152009-05-26 08:34:10 -040089 pr_err("timeout on ac97 bus (write)\n");
Grant Likely0827d6b2009-07-02 11:57:25 -060090 goto out;
Jon Smirl20d0e152009-05-26 08:34:10 -040091 }
92 /* Write data */
93 out_be32(&psc_dma->psc_regs->ac97_cmd,
94 ((reg & 0x7f) << 24) | (val << 8));
Grant Likely0827d6b2009-07-02 11:57:25 -060095
96 out:
97 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -040098}
99
100static void psc_ac97_warm_reset(struct snd_ac97 *ac97)
101{
Jon Smirl20d0e152009-05-26 08:34:10 -0400102 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
103
Eric Millbrandt949ad0a2010-08-06 20:49:19 -0600104 mutex_lock(&psc_dma->mutex);
105
Jon Smirl20d0e152009-05-26 08:34:10 -0400106 out_be32(&regs->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);
Jon Smirlea8b27a2009-05-27 01:06:19 -0400107 udelay(3);
Jon Smirl20d0e152009-05-26 08:34:10 -0400108 out_be32(&regs->sicr, psc_dma->sicr);
Eric Millbrandt949ad0a2010-08-06 20:49:19 -0600109
110 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e152009-05-26 08:34:10 -0400111}
112
113static void psc_ac97_cold_reset(struct snd_ac97 *ac97)
114{
Jon Smirl20d0e152009-05-26 08:34:10 -0400115 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
116
Eric Millbrandt949ad0a2010-08-06 20:49:19 -0600117 mutex_lock(&psc_dma->mutex);
118 dev_dbg(psc_dma->dev, "cold reset\n");
119
120 mpc5200_psc_ac97_gpio_reset(psc_dma->id);
121
122 /* Notify the PSC that a reset has occurred */
123 out_be32(&regs->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_ACRB);
124
125 /* Re-enable RX and TX */
126 out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
127
128 mutex_unlock(&psc_dma->mutex);
129
John Bonesioed0f19b2009-07-21 13:15:40 -0700130 msleep(1);
Jon Smirl20d0e152009-05-26 08:34:10 -0400131 psc_ac97_warm_reset(ac97);
132}
133
134struct snd_ac97_bus_ops soc_ac97_ops = {
135 .read = psc_ac97_read,
136 .write = psc_ac97_write,
137 .reset = psc_ac97_cold_reset,
138 .warm_reset = psc_ac97_warm_reset,
139};
140EXPORT_SYMBOL_GPL(soc_ac97_ops);
141
142static int psc_ac97_hw_analog_params(struct snd_pcm_substream *substream,
143 struct snd_pcm_hw_params *params,
144 struct snd_soc_dai *cpu_dai)
145{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000146 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
Grant Likelyc939e5c2009-11-07 01:34:43 -0700147 struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
Jon Smirl20d0e152009-05-26 08:34:10 -0400148
149 dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
150 " periods=%i buffer_size=%i buffer_bytes=%i channels=%i"
151 " rate=%i format=%i\n",
152 __func__, substream, params_period_size(params),
153 params_period_bytes(params), params_periods(params),
154 params_buffer_size(params), params_buffer_bytes(params),
155 params_channels(params), params_rate(params),
156 params_format(params));
157
Grant Likelyc939e5c2009-11-07 01:34:43 -0700158 /* Determine the set of enable bits to turn on */
159 s->ac97_slot_bits = (params_channels(params) == 1) ? 0x100 : 0x300;
160 if (substream->pstr->stream != SNDRV_PCM_STREAM_CAPTURE)
161 s->ac97_slot_bits <<= 16;
Jon Smirl20d0e152009-05-26 08:34:10 -0400162 return 0;
163}
164
165static int psc_ac97_hw_digital_params(struct snd_pcm_substream *substream,
166 struct snd_pcm_hw_params *params,
167 struct snd_soc_dai *cpu_dai)
168{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000169 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
Jon Smirl20d0e152009-05-26 08:34:10 -0400170
Grant Likelyc939e5c2009-11-07 01:34:43 -0700171 dev_dbg(psc_dma->dev, "%s(substream=%p)\n", __func__, substream);
172
Jon Smirl20d0e152009-05-26 08:34:10 -0400173 if (params_channels(params) == 1)
174 out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
175 else
176 out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);
177
178 return 0;
179}
180
181static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
182 struct snd_soc_dai *dai)
183{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(dai);
Grant Likelyc939e5c2009-11-07 01:34:43 -0700185 struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
Jon Smirl20d0e152009-05-26 08:34:10 -0400186
187 switch (cmd) {
Grant Likelyc939e5c2009-11-07 01:34:43 -0700188 case SNDRV_PCM_TRIGGER_START:
189 dev_dbg(psc_dma->dev, "AC97 START: stream=%i\n",
190 substream->pstr->stream);
Jon Smirl20d0e152009-05-26 08:34:10 -0400191
Grant Likelyc939e5c2009-11-07 01:34:43 -0700192 /* Set the slot enable bits */
193 psc_dma->slots |= s->ac97_slot_bits;
194 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
195 break;
196
197 case SNDRV_PCM_TRIGGER_STOP:
198 dev_dbg(psc_dma->dev, "AC97 STOP: stream=%i\n",
199 substream->pstr->stream);
200
201 /* Clear the slot enable bits */
202 psc_dma->slots &= ~(s->ac97_slot_bits);
Jon Smirl20d0e152009-05-26 08:34:10 -0400203 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
204 break;
205 }
206 return 0;
207}
208
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000209static int psc_ac97_probe(struct snd_soc_dai *cpu_dai)
Jon Smirl20d0e152009-05-26 08:34:10 -0400210{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000211 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
Jon Smirl20d0e152009-05-26 08:34:10 -0400212 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
213
214 /* Go */
215 out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
216 return 0;
217}
218
219/* ---------------------------------------------------------------------
220 * ALSA SoC Bindings
221 *
222 * - Digital Audio Interface (DAI) template
223 * - create/destroy dai hooks
224 */
225
226/**
227 * psc_ac97_dai_template: template CPU Digital Audio Interface
228 */
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100229static const struct snd_soc_dai_ops psc_ac97_analog_ops = {
Jon Smirl20d0e152009-05-26 08:34:10 -0400230 .hw_params = psc_ac97_hw_analog_params,
231 .trigger = psc_ac97_trigger,
232};
233
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100234static const struct snd_soc_dai_ops psc_ac97_digital_ops = {
Jon Smirl20d0e152009-05-26 08:34:10 -0400235 .hw_params = psc_ac97_hw_digital_params,
236};
237
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000238static struct snd_soc_dai_driver psc_ac97_dai[] = {
Jon Smirl20d0e152009-05-26 08:34:10 -0400239{
Jon Smirl20d0e152009-05-26 08:34:10 -0400240 .ac97_control = 1,
241 .probe = psc_ac97_probe,
242 .playback = {
243 .channels_min = 1,
244 .channels_max = 6,
245 .rates = SNDRV_PCM_RATE_8000_48000,
246 .formats = SNDRV_PCM_FMTBIT_S32_BE,
247 },
248 .capture = {
249 .channels_min = 1,
250 .channels_max = 2,
251 .rates = SNDRV_PCM_RATE_8000_48000,
252 .formats = SNDRV_PCM_FMTBIT_S32_BE,
253 },
254 .ops = &psc_ac97_analog_ops,
255},
256{
Jon Smirl20d0e152009-05-26 08:34:10 -0400257 .ac97_control = 1,
258 .playback = {
259 .channels_min = 1,
260 .channels_max = 2,
261 .rates = SNDRV_PCM_RATE_32000 | \
262 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
263 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
264 },
265 .ops = &psc_ac97_digital_ops,
266} };
Jon Smirl20d0e152009-05-26 08:34:10 -0400267
268
269
270/* ---------------------------------------------------------------------
271 * OF platform bus binding code:
272 * - Probe/remove operations
273 * - OF device match table
274 */
Grant Likelyf07eb222011-02-22 21:05:04 -0700275static int __devinit psc_ac97_of_probe(struct platform_device *op)
Jon Smirl20d0e152009-05-26 08:34:10 -0400276{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000277 int rc;
Jon Smirl20d0e152009-05-26 08:34:10 -0400278 struct snd_ac97 ac97;
279 struct mpc52xx_psc __iomem *regs;
280
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000281 rc = snd_soc_register_dais(&op->dev, psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
Jon Smirl20d0e152009-05-26 08:34:10 -0400282 if (rc != 0) {
283 dev_err(&op->dev, "Failed to register DAI\n");
284 return rc;
285 }
286
287 psc_dma = dev_get_drvdata(&op->dev);
288 regs = psc_dma->psc_regs;
289 ac97.private_data = psc_dma;
290
Jon Smirl20d0e152009-05-26 08:34:10 -0400291 psc_dma->imr = 0;
292 out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
293
294 /* Configure the serial interface mode to AC97 */
295 psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
296 out_be32(&regs->sicr, psc_dma->sicr);
297
298 /* No slots active */
299 out_be32(&regs->ac97_slots, 0x00000000);
300
301 return 0;
302}
303
Grant Likely2dc11582010-08-06 09:25:50 -0600304static int __devexit psc_ac97_of_remove(struct platform_device *op)
Jon Smirl20d0e152009-05-26 08:34:10 -0400305{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000306 snd_soc_unregister_dais(&op->dev, ARRAY_SIZE(psc_ac97_dai));
307 return 0;
Jon Smirl20d0e152009-05-26 08:34:10 -0400308}
309
310/* Match table for of_platform binding */
311static struct of_device_id psc_ac97_match[] __devinitdata = {
312 { .compatible = "fsl,mpc5200-psc-ac97", },
313 { .compatible = "fsl,mpc5200b-psc-ac97", },
314 {}
315};
316MODULE_DEVICE_TABLE(of, psc_ac97_match);
317
Grant Likelyf07eb222011-02-22 21:05:04 -0700318static struct platform_driver psc_ac97_driver = {
Jon Smirl20d0e152009-05-26 08:34:10 -0400319 .probe = psc_ac97_of_probe,
320 .remove = __devexit_p(psc_ac97_of_remove),
321 .driver = {
322 .name = "mpc5200-psc-ac97",
323 .owner = THIS_MODULE,
Grant Likely40182942010-04-13 16:13:02 -0700324 .of_match_table = psc_ac97_match,
Jon Smirl20d0e152009-05-26 08:34:10 -0400325 },
326};
327
Axel Linba0a7e02011-11-25 10:10:55 +0800328module_platform_driver(psc_ac97_driver);
Jon Smirl20d0e152009-05-26 08:34:10 -0400329
330MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
331MODULE_DESCRIPTION("mpc5200 AC97 module");
332MODULE_LICENSE("GPL");
333