9b8503f2d68cef2289cca82565b93e80d6330b28
[firefly-linux-kernel-4.4.55.git] / sound / soc / fsl / mpc5200_psc_ac97.c
1 /*
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>
15
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19
20 #include <asm/time.h>
21 #include <asm/delay.h>
22 #include <asm/mpc52xx_psc.h>
23
24 #include "mpc5200_dma.h"
25 #include "mpc5200_psc_ac97.h"
26
27 #define DRV_NAME "mpc5200-psc-ac97"
28
29 /* ALSA only supports a single AC97 device so static is recommend here */
30 static struct psc_dma *psc_dma;
31
32 static unsigned short psc_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
33 {
34         int status;
35         unsigned int val;
36
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);
40         if (status == 0) {
41                 pr_err("timeout on ac97 bus (rdy)\n");
42                 return -ENODEV;
43         }
44
45         /* Force clear the data valid bit */
46         in_be32(&psc_dma->psc_regs->ac97_data);
47
48         /* Send the read */
49         out_be32(&psc_dma->psc_regs->ac97_cmd, (1<<31) | ((reg & 0x7f) << 24));
50
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);
54         if (status == 0) {
55                 pr_err("timeout on ac97 read (val) %x\n",
56                                 in_be16(&psc_dma->psc_regs->sr_csr.status));
57                 return -ENODEV;
58         }
59         /* Get the data */
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");
63                 return -ENODEV;
64         }
65         val = (val >> 8) & 0xffff;
66
67         return (unsigned short) val;
68 }
69
70 static void psc_ac97_write(struct snd_ac97 *ac97,
71                                 unsigned short reg, unsigned short val)
72 {
73         int status;
74
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);
78         if (status == 0) {
79                 pr_err("timeout on ac97 bus (write)\n");
80                 return;
81         }
82         /* Write data */
83         out_be32(&psc_dma->psc_regs->ac97_cmd,
84                         ((reg & 0x7f) << 24) | (val << 8));
85 }
86
87 static void psc_ac97_warm_reset(struct snd_ac97 *ac97)
88 {
89         struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
90
91         out_be32(&regs->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);
92         udelay(3);
93         out_be32(&regs->sicr, psc_dma->sicr);
94 }
95
96 static void psc_ac97_cold_reset(struct snd_ac97 *ac97)
97 {
98         struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
99
100         /* Do a cold reset */
101         out_8(&regs->op1, MPC52xx_PSC_OP_RES);
102         udelay(10);
103         out_8(&regs->op0, MPC52xx_PSC_OP_RES);
104         udelay(50);
105         psc_ac97_warm_reset(ac97);
106 }
107
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,
113 };
114 EXPORT_SYMBOL_GPL(soc_ac97_ops);
115
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)
119 {
120         struct psc_dma *psc_dma = cpu_dai->private_data;
121
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));
130
131
132         if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
133                 if (params_channels(params) == 1)
134                         psc_dma->slots |= 0x00000100;
135                 else
136                         psc_dma->slots |= 0x00000300;
137         } else {
138                 if (params_channels(params) == 1)
139                         psc_dma->slots |= 0x01000000;
140                 else
141                         psc_dma->slots |= 0x03000000;
142         }
143         out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
144
145         return 0;
146 }
147
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)
151 {
152         struct psc_dma *psc_dma = cpu_dai->private_data;
153
154         if (params_channels(params) == 1)
155                 out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
156         else
157                 out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);
158
159         return 0;
160 }
161
162 static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
163                                                         struct snd_soc_dai *dai)
164 {
165         struct snd_soc_pcm_runtime *rtd = substream->private_data;
166         struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
167
168         switch (cmd) {
169         case SNDRV_PCM_TRIGGER_STOP:
170                 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
171                         psc_dma->slots &= 0xFFFF0000;
172                 else
173                         psc_dma->slots &= 0x0000FFFF;
174
175                 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
176                 break;
177         }
178         return 0;
179 }
180
181 static int psc_ac97_probe(struct platform_device *pdev,
182                                         struct snd_soc_dai *cpu_dai)
183 {
184         struct psc_dma *psc_dma = cpu_dai->private_data;
185         struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
186
187         /* Go */
188         out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
189         return 0;
190 }
191
192 /* ---------------------------------------------------------------------
193  * ALSA SoC Bindings
194  *
195  * - Digital Audio Interface (DAI) template
196  * - create/destroy dai hooks
197  */
198
199 /**
200  * psc_ac97_dai_template: template CPU Digital Audio Interface
201  */
202 static struct snd_soc_dai_ops psc_ac97_analog_ops = {
203         .hw_params      = psc_ac97_hw_analog_params,
204         .trigger        = psc_ac97_trigger,
205 };
206
207 static struct snd_soc_dai_ops psc_ac97_digital_ops = {
208         .hw_params      = psc_ac97_hw_digital_params,
209 };
210
211 struct snd_soc_dai psc_ac97_dai[] = {
212 {
213         .name   = "AC97",
214         .ac97_control = 1,
215         .probe  = psc_ac97_probe,
216         .playback = {
217                 .channels_min   = 1,
218                 .channels_max   = 6,
219                 .rates          = SNDRV_PCM_RATE_8000_48000,
220                 .formats = SNDRV_PCM_FMTBIT_S32_BE,
221         },
222         .capture = {
223                 .channels_min   = 1,
224                 .channels_max   = 2,
225                 .rates          = SNDRV_PCM_RATE_8000_48000,
226                 .formats = SNDRV_PCM_FMTBIT_S32_BE,
227         },
228         .ops = &psc_ac97_analog_ops,
229 },
230 {
231         .name   = "SPDIF",
232         .ac97_control = 1,
233         .playback = {
234                 .channels_min   = 1,
235                 .channels_max   = 2,
236                 .rates          = SNDRV_PCM_RATE_32000 | \
237                         SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
238                 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
239         },
240         .ops = &psc_ac97_digital_ops,
241 } };
242 EXPORT_SYMBOL_GPL(psc_ac97_dai);
243
244
245
246 /* ---------------------------------------------------------------------
247  * OF platform bus binding code:
248  * - Probe/remove operations
249  * - OF device match table
250  */
251 static int __devinit psc_ac97_of_probe(struct of_device *op,
252                                       const struct of_device_id *match)
253 {
254         int rc, i;
255         struct snd_ac97 ac97;
256         struct mpc52xx_psc __iomem *regs;
257
258         rc = mpc5200_audio_dma_create(op);
259         if (rc != 0)
260                 return rc;
261
262         for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
263                 psc_ac97_dai[i].dev = &op->dev;
264
265         rc = snd_soc_register_dais(psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
266         if (rc != 0) {
267                 dev_err(&op->dev, "Failed to register DAI\n");
268                 return rc;
269         }
270
271         psc_dma = dev_get_drvdata(&op->dev);
272         regs = psc_dma->psc_regs;
273         ac97.private_data = psc_dma;
274
275         for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
276                 psc_ac97_dai[i].private_data = psc_dma;
277
278         psc_dma->imr = 0;
279         out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
280
281         /* Configure the serial interface mode to AC97 */
282         psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
283         out_be32(&regs->sicr, psc_dma->sicr);
284
285         /* No slots active */
286         out_be32(&regs->ac97_slots, 0x00000000);
287
288         return 0;
289 }
290
291 static int __devexit psc_ac97_of_remove(struct of_device *op)
292 {
293         return mpc5200_audio_dma_destroy(op);
294 }
295
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", },
300         {}
301 };
302 MODULE_DEVICE_TABLE(of, psc_ac97_match);
303
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),
308         .driver = {
309                 .name = "mpc5200-psc-ac97",
310                 .owner = THIS_MODULE,
311         },
312 };
313
314 /* ---------------------------------------------------------------------
315  * Module setup and teardown; simply register the of_platform driver
316  * for the PSC in AC97 mode.
317  */
318 static int __init psc_ac97_init(void)
319 {
320         return of_register_platform_driver(&psc_ac97_driver);
321 }
322 module_init(psc_ac97_init);
323
324 static void __exit psc_ac97_exit(void)
325 {
326         of_unregister_platform_driver(&psc_ac97_driver);
327 }
328 module_exit(psc_ac97_exit);
329
330 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
331 MODULE_DESCRIPTION("mpc5200 AC97 module");
332 MODULE_LICENSE("GPL");
333