blob: e5b9c04d1565134ad546119d8139284e35760902 [file] [log] [blame]
Andra Danciu1af301d2019-04-05 14:50:02 +03001// SPDX-License-Identifier: GPL-2.0
2//
3// linux/sound/mpc5200-ac97.c -- AC97 support for the Freescale MPC52xx chip.
4//
5// Copyright (C) 2009 Jon Smirl, Digispeaker
6// Author: Jon Smirl <jonsmirl@gmail.com>
Jon Smirl20d0e1522009-05-26 08:34:10 -04007
8#include <linux/module.h>
9#include <linux/of_device.h>
10#include <linux/of_platform.h>
John Bonesioed0f19b2009-07-21 13:15:40 -070011#include <linux/delay.h>
Xiubo Li0d55ad42016-01-15 19:35:25 +080012#include <linux/time.h>
Jon Smirl20d0e1522009-05-26 08:34:10 -040013
14#include <sound/pcm.h>
15#include <sound/pcm_params.h>
16#include <sound/soc.h>
17
18#include <asm/time.h>
19#include <asm/delay.h>
Eric Millbrandt949ad0a2010-08-06 20:49:19 -060020#include <asm/mpc52xx.h>
Jon Smirl20d0e1522009-05-26 08:34:10 -040021#include <asm/mpc52xx_psc.h>
22
23#include "mpc5200_dma.h"
Jon Smirl20d0e1522009-05-26 08:34:10 -040024
25#define DRV_NAME "mpc5200-psc-ac97"
26
27/* ALSA only supports a single AC97 device so static is recommend here */
28static struct psc_dma *psc_dma;
29
30static unsigned short psc_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
31{
Jon Smirlea8b27a2009-05-27 01:06:19 -040032 int status;
Jon Smirl20d0e1522009-05-26 08:34:10 -040033 unsigned int val;
34
Grant Likely0827d6b2009-07-02 11:57:25 -060035 mutex_lock(&psc_dma->mutex);
36
Jon Smirl20d0e1522009-05-26 08:34:10 -040037 /* Wait for command send status zero = ready */
Jon Smirlea8b27a2009-05-27 01:06:19 -040038 status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
39 MPC52xx_PSC_SR_CMDSEND), 100, 0);
40 if (status == 0) {
Jon Smirl20d0e1522009-05-26 08:34:10 -040041 pr_err("timeout on ac97 bus (rdy)\n");
Grant Likely0827d6b2009-07-02 11:57:25 -060042 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e1522009-05-26 08:34:10 -040043 return -ENODEV;
44 }
Grant Likely07573532009-07-02 11:57:19 -060045
46 /* Force clear the data valid bit */
47 in_be32(&psc_dma->psc_regs->ac97_data);
48
Jon Smirl20d0e1522009-05-26 08:34:10 -040049 /* Send the read */
50 out_be32(&psc_dma->psc_regs->ac97_cmd, (1<<31) | ((reg & 0x7f) << 24));
51
52 /* Wait for the answer */
Jon Smirlea8b27a2009-05-27 01:06:19 -040053 status = spin_event_timeout((in_be16(&psc_dma->psc_regs->sr_csr.status) &
54 MPC52xx_PSC_SR_DATA_VAL), 100, 0);
55 if (status == 0) {
Jon Smirl20d0e1522009-05-26 08:34:10 -040056 pr_err("timeout on ac97 read (val) %x\n",
57 in_be16(&psc_dma->psc_regs->sr_csr.status));
Grant Likely0827d6b2009-07-02 11:57:25 -060058 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e1522009-05-26 08:34:10 -040059 return -ENODEV;
60 }
61 /* Get the data */
62 val = in_be32(&psc_dma->psc_regs->ac97_data);
63 if (((val >> 24) & 0x7f) != reg) {
64 pr_err("reg echo error on ac97 read\n");
Grant Likely0827d6b2009-07-02 11:57:25 -060065 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e1522009-05-26 08:34:10 -040066 return -ENODEV;
67 }
68 val = (val >> 8) & 0xffff;
69
Grant Likely0827d6b2009-07-02 11:57:25 -060070 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e1522009-05-26 08:34:10 -040071 return (unsigned short) val;
72}
73
74static void psc_ac97_write(struct snd_ac97 *ac97,
75 unsigned short reg, unsigned short val)
76{
Jon Smirlea8b27a2009-05-27 01:06:19 -040077 int status;
Jon Smirl20d0e1522009-05-26 08:34:10 -040078
Grant Likely0827d6b2009-07-02 11:57:25 -060079 mutex_lock(&psc_dma->mutex);
80
Jon Smirl20d0e1522009-05-26 08:34:10 -040081 /* Wait for command status zero = ready */
Jon Smirlea8b27a2009-05-27 01:06:19 -040082 status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
83 MPC52xx_PSC_SR_CMDSEND), 100, 0);
84 if (status == 0) {
Jon Smirl20d0e1522009-05-26 08:34:10 -040085 pr_err("timeout on ac97 bus (write)\n");
Grant Likely0827d6b2009-07-02 11:57:25 -060086 goto out;
Jon Smirl20d0e1522009-05-26 08:34:10 -040087 }
88 /* Write data */
89 out_be32(&psc_dma->psc_regs->ac97_cmd,
90 ((reg & 0x7f) << 24) | (val << 8));
Grant Likely0827d6b2009-07-02 11:57:25 -060091
92 out:
93 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e1522009-05-26 08:34:10 -040094}
95
96static void psc_ac97_warm_reset(struct snd_ac97 *ac97)
97{
Jon Smirl20d0e1522009-05-26 08:34:10 -040098 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
99
Eric Millbrandt949ad0a2010-08-06 20:49:19 -0600100 mutex_lock(&psc_dma->mutex);
101
Jon Smirl20d0e1522009-05-26 08:34:10 -0400102 out_be32(&regs->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);
Jon Smirlea8b27a2009-05-27 01:06:19 -0400103 udelay(3);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400104 out_be32(&regs->sicr, psc_dma->sicr);
Eric Millbrandt949ad0a2010-08-06 20:49:19 -0600105
106 mutex_unlock(&psc_dma->mutex);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400107}
108
109static void psc_ac97_cold_reset(struct snd_ac97 *ac97)
110{
Jon Smirl20d0e1522009-05-26 08:34:10 -0400111 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
112
Eric Millbrandt949ad0a2010-08-06 20:49:19 -0600113 mutex_lock(&psc_dma->mutex);
114 dev_dbg(psc_dma->dev, "cold reset\n");
115
116 mpc5200_psc_ac97_gpio_reset(psc_dma->id);
117
118 /* Notify the PSC that a reset has occurred */
119 out_be32(&regs->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_ACRB);
120
121 /* Re-enable RX and TX */
122 out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
123
124 mutex_unlock(&psc_dma->mutex);
125
Xiubo Li0d55ad42016-01-15 19:35:25 +0800126 usleep_range(1000, 2000);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400127 psc_ac97_warm_reset(ac97);
128}
129
Mark Brownb047e1c2013-06-26 12:45:59 +0100130static struct snd_ac97_bus_ops psc_ac97_ops = {
Jon Smirl20d0e1522009-05-26 08:34:10 -0400131 .read = psc_ac97_read,
132 .write = psc_ac97_write,
133 .reset = psc_ac97_cold_reset,
134 .warm_reset = psc_ac97_warm_reset,
135};
Jon Smirl20d0e1522009-05-26 08:34:10 -0400136
137static int psc_ac97_hw_analog_params(struct snd_pcm_substream *substream,
138 struct snd_pcm_hw_params *params,
139 struct snd_soc_dai *cpu_dai)
140{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000141 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
Grant Likelyc939e5c2009-11-07 01:34:43 -0700142 struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400143
144 dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
145 " periods=%i buffer_size=%i buffer_bytes=%i channels=%i"
146 " rate=%i format=%i\n",
147 __func__, substream, params_period_size(params),
148 params_period_bytes(params), params_periods(params),
149 params_buffer_size(params), params_buffer_bytes(params),
150 params_channels(params), params_rate(params),
151 params_format(params));
152
Grant Likelyc939e5c2009-11-07 01:34:43 -0700153 /* Determine the set of enable bits to turn on */
154 s->ac97_slot_bits = (params_channels(params) == 1) ? 0x100 : 0x300;
155 if (substream->pstr->stream != SNDRV_PCM_STREAM_CAPTURE)
156 s->ac97_slot_bits <<= 16;
Jon Smirl20d0e1522009-05-26 08:34:10 -0400157 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{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000164 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400165
Grant Likelyc939e5c2009-11-07 01:34:43 -0700166 dev_dbg(psc_dma->dev, "%s(substream=%p)\n", __func__, substream);
167
Jon Smirl20d0e1522009-05-26 08:34:10 -0400168 if (params_channels(params) == 1)
169 out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
170 else
171 out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);
172
173 return 0;
174}
175
176static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
177 struct snd_soc_dai *dai)
178{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000179 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(dai);
Grant Likelyc939e5c2009-11-07 01:34:43 -0700180 struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400181
182 switch (cmd) {
Grant Likelyc939e5c2009-11-07 01:34:43 -0700183 case SNDRV_PCM_TRIGGER_START:
184 dev_dbg(psc_dma->dev, "AC97 START: stream=%i\n",
185 substream->pstr->stream);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400186
Grant Likelyc939e5c2009-11-07 01:34:43 -0700187 /* Set the slot enable bits */
188 psc_dma->slots |= s->ac97_slot_bits;
189 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
190 break;
191
192 case SNDRV_PCM_TRIGGER_STOP:
193 dev_dbg(psc_dma->dev, "AC97 STOP: stream=%i\n",
194 substream->pstr->stream);
195
196 /* Clear the slot enable bits */
197 psc_dma->slots &= ~(s->ac97_slot_bits);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400198 out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
199 break;
200 }
201 return 0;
202}
203
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000204static int psc_ac97_probe(struct snd_soc_dai *cpu_dai)
Jon Smirl20d0e1522009-05-26 08:34:10 -0400205{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000206 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400207 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
208
209 /* Go */
210 out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
211 return 0;
212}
213
214/* ---------------------------------------------------------------------
215 * ALSA SoC Bindings
216 *
217 * - Digital Audio Interface (DAI) template
218 * - create/destroy dai hooks
219 */
220
221/**
222 * psc_ac97_dai_template: template CPU Digital Audio Interface
223 */
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100224static const struct snd_soc_dai_ops psc_ac97_analog_ops = {
Jon Smirl20d0e1522009-05-26 08:34:10 -0400225 .hw_params = psc_ac97_hw_analog_params,
226 .trigger = psc_ac97_trigger,
227};
228
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100229static const struct snd_soc_dai_ops psc_ac97_digital_ops = {
Jon Smirl20d0e1522009-05-26 08:34:10 -0400230 .hw_params = psc_ac97_hw_digital_params,
231};
232
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000233static struct snd_soc_dai_driver psc_ac97_dai[] = {
Jon Smirl20d0e1522009-05-26 08:34:10 -0400234{
Eric Millbrandta4f7b702012-09-13 17:43:12 -0400235 .name = "mpc5200-psc-ac97.0",
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100236 .bus_control = true,
Jon Smirl20d0e1522009-05-26 08:34:10 -0400237 .probe = psc_ac97_probe,
238 .playback = {
Eric Millbrandta4f7b702012-09-13 17:43:12 -0400239 .stream_name = "AC97 Playback",
Jon Smirl20d0e1522009-05-26 08:34:10 -0400240 .channels_min = 1,
241 .channels_max = 6,
242 .rates = SNDRV_PCM_RATE_8000_48000,
243 .formats = SNDRV_PCM_FMTBIT_S32_BE,
244 },
245 .capture = {
Eric Millbrandta4f7b702012-09-13 17:43:12 -0400246 .stream_name = "AC97 Capture",
Jon Smirl20d0e1522009-05-26 08:34:10 -0400247 .channels_min = 1,
248 .channels_max = 2,
249 .rates = SNDRV_PCM_RATE_8000_48000,
250 .formats = SNDRV_PCM_FMTBIT_S32_BE,
251 },
252 .ops = &psc_ac97_analog_ops,
253},
254{
Eric Millbrandta4f7b702012-09-13 17:43:12 -0400255 .name = "mpc5200-psc-ac97.1",
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100256 .bus_control = true,
Jon Smirl20d0e1522009-05-26 08:34:10 -0400257 .playback = {
Eric Millbrandta4f7b702012-09-13 17:43:12 -0400258 .stream_name = "AC97 SPDIF",
Jon Smirl20d0e1522009-05-26 08:34:10 -0400259 .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 Smirl20d0e1522009-05-26 08:34:10 -0400267
Kuninori Morimotof298a0f2013-03-21 03:31:41 -0700268static const struct snd_soc_component_driver psc_ac97_component = {
269 .name = DRV_NAME,
270};
Jon Smirl20d0e1522009-05-26 08:34:10 -0400271
272
273/* ---------------------------------------------------------------------
274 * OF platform bus binding code:
275 * - Probe/remove operations
276 * - OF device match table
277 */
Bill Pembertona0a3d512012-12-07 09:26:16 -0500278static int psc_ac97_of_probe(struct platform_device *op)
Jon Smirl20d0e1522009-05-26 08:34:10 -0400279{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000280 int rc;
Jon Smirl20d0e1522009-05-26 08:34:10 -0400281 struct mpc52xx_psc __iomem *regs;
282
Eric Millbrandtf515b672012-09-13 17:43:11 -0400283 rc = mpc5200_audio_dma_create(op);
284 if (rc != 0)
285 return rc;
286
Mark Brownb047e1c2013-06-26 12:45:59 +0100287 rc = snd_soc_set_ac97_ops(&psc_ac97_ops);
288 if (rc != 0) {
Rob Herring5af50732013-09-17 14:28:33 -0500289 dev_err(&op->dev, "Failed to set AC'97 ops: %d\n", rc);
Mark Brownb047e1c2013-06-26 12:45:59 +0100290 return rc;
291 }
292
Kuninori Morimotof298a0f2013-03-21 03:31:41 -0700293 rc = snd_soc_register_component(&op->dev, &psc_ac97_component,
294 psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
Jon Smirl20d0e1522009-05-26 08:34:10 -0400295 if (rc != 0) {
296 dev_err(&op->dev, "Failed to register DAI\n");
297 return rc;
298 }
299
300 psc_dma = dev_get_drvdata(&op->dev);
301 regs = psc_dma->psc_regs;
Jon Smirl20d0e1522009-05-26 08:34:10 -0400302
Jon Smirl20d0e1522009-05-26 08:34:10 -0400303 psc_dma->imr = 0;
304 out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
305
306 /* Configure the serial interface mode to AC97 */
307 psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
308 out_be32(&regs->sicr, psc_dma->sicr);
309
310 /* No slots active */
311 out_be32(&regs->ac97_slots, 0x00000000);
312
313 return 0;
314}
315
Bill Pembertona0a3d512012-12-07 09:26:16 -0500316static int psc_ac97_of_remove(struct platform_device *op)
Jon Smirl20d0e1522009-05-26 08:34:10 -0400317{
Eric Millbrandtf515b672012-09-13 17:43:11 -0400318 mpc5200_audio_dma_destroy(op);
Kuninori Morimotof298a0f2013-03-21 03:31:41 -0700319 snd_soc_unregister_component(&op->dev);
Mark Brownb047e1c2013-06-26 12:45:59 +0100320 snd_soc_set_ac97_ops(NULL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000321 return 0;
Jon Smirl20d0e1522009-05-26 08:34:10 -0400322}
323
324/* Match table for of_platform binding */
Fabian Frederick6ffa84d2015-03-18 17:48:57 +0100325static const struct of_device_id psc_ac97_match[] = {
Jon Smirl20d0e1522009-05-26 08:34:10 -0400326 { .compatible = "fsl,mpc5200-psc-ac97", },
327 { .compatible = "fsl,mpc5200b-psc-ac97", },
328 {}
329};
330MODULE_DEVICE_TABLE(of, psc_ac97_match);
331
Grant Likelyf07eb222011-02-22 21:05:04 -0700332static struct platform_driver psc_ac97_driver = {
Jon Smirl20d0e1522009-05-26 08:34:10 -0400333 .probe = psc_ac97_of_probe,
Bill Pembertona0a3d512012-12-07 09:26:16 -0500334 .remove = psc_ac97_of_remove,
Jon Smirl20d0e1522009-05-26 08:34:10 -0400335 .driver = {
336 .name = "mpc5200-psc-ac97",
Grant Likely40182942010-04-13 16:13:02 -0700337 .of_match_table = psc_ac97_match,
Jon Smirl20d0e1522009-05-26 08:34:10 -0400338 },
339};
340
Axel Linba0a7e02011-11-25 10:10:55 +0800341module_platform_driver(psc_ac97_driver);
Jon Smirl20d0e1522009-05-26 08:34:10 -0400342
343MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
344MODULE_DESCRIPTION("mpc5200 AC97 module");
345MODULE_LICENSE("GPL");
346