2 * bf5xx-ac97.c -- AC97 support for the ADI blackfin chip.
5 * Created: 11th. June 2007
6 * Copyright: Analog Device Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/wait.h>
18 #include <linux/delay.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/ac97_codec.h>
23 #include <sound/initval.h>
24 #include <sound/soc.h>
27 #include <asm/portmux.h>
28 #include <linux/mutex.h>
29 #include <linux/gpio.h>
31 #include "bf5xx-sport.h"
32 #include "bf5xx-ac97.h"
34 #if defined(CONFIG_BF54x)
35 #define PIN_REQ_SPORT_0 {P_SPORT0_TFS, P_SPORT0_DTPRI, P_SPORT0_TSCLK, \
36 P_SPORT0_RFS, P_SPORT0_DRPRI, P_SPORT0_RSCLK, 0}
38 #define PIN_REQ_SPORT_1 {P_SPORT1_TFS, P_SPORT1_DTPRI, P_SPORT1_TSCLK, \
39 P_SPORT1_RFS, P_SPORT1_DRPRI, P_SPORT1_RSCLK, 0}
41 #define PIN_REQ_SPORT_2 {P_SPORT2_TFS, P_SPORT2_DTPRI, P_SPORT2_TSCLK, \
42 P_SPORT2_RFS, P_SPORT2_DRPRI, P_SPORT2_RSCLK, 0}
44 #define PIN_REQ_SPORT_3 {P_SPORT3_TFS, P_SPORT3_DTPRI, P_SPORT3_TSCLK, \
45 P_SPORT3_RFS, P_SPORT3_DRPRI, P_SPORT3_RSCLK, 0}
47 #define PIN_REQ_SPORT_0 {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS, \
48 P_SPORT0_DRPRI, P_SPORT0_RSCLK, 0}
50 #define PIN_REQ_SPORT_1 {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, \
51 P_SPORT1_DRPRI, P_SPORT1_RSCLK, 0}
54 static int *cmd_count;
55 static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
57 static u16 sport_req[][7] = {
59 #ifdef PIN_REQ_SPORT_1
62 #ifdef PIN_REQ_SPORT_2
65 #ifdef PIN_REQ_SPORT_3
70 static struct sport_param sport_params[4] = {
72 .dma_rx_chan = CH_SPORT0_RX,
73 .dma_tx_chan = CH_SPORT0_TX,
74 .err_irq = IRQ_SPORT0_ERROR,
75 .regs = (struct sport_register *)SPORT0_TCR1,
77 #ifdef PIN_REQ_SPORT_1
79 .dma_rx_chan = CH_SPORT1_RX,
80 .dma_tx_chan = CH_SPORT1_TX,
81 .err_irq = IRQ_SPORT1_ERROR,
82 .regs = (struct sport_register *)SPORT1_TCR1,
85 #ifdef PIN_REQ_SPORT_2
87 .dma_rx_chan = CH_SPORT2_RX,
88 .dma_tx_chan = CH_SPORT2_TX,
89 .err_irq = IRQ_SPORT2_ERROR,
90 .regs = (struct sport_register *)SPORT2_TCR1,
93 #ifdef PIN_REQ_SPORT_3
95 .dma_rx_chan = CH_SPORT3_RX,
96 .dma_tx_chan = CH_SPORT3_TX,
97 .err_irq = IRQ_SPORT3_ERROR,
98 .regs = (struct sport_register *)SPORT3_TCR1,
103 void bf5xx_pcm_to_ac97(struct ac97_frame *dst, const __u16 *src,
104 size_t count, unsigned int chan_mask)
107 dst->ac97_tag = TAG_VALID;
108 if (chan_mask & SP_FL) {
109 dst->ac97_pcm_r = *src++;
110 dst->ac97_tag |= TAG_PCM_RIGHT;
112 if (chan_mask & SP_FR) {
113 dst->ac97_pcm_l = *src++;
114 dst->ac97_tag |= TAG_PCM_LEFT;
117 #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
118 if (chan_mask & SP_SR) {
119 dst->ac97_sl = *src++;
120 dst->ac97_tag |= TAG_PCM_SL;
122 if (chan_mask & SP_SL) {
123 dst->ac97_sr = *src++;
124 dst->ac97_tag |= TAG_PCM_SR;
126 if (chan_mask & SP_LFE) {
127 dst->ac97_lfe = *src++;
128 dst->ac97_tag |= TAG_PCM_LFE;
130 if (chan_mask & SP_FC) {
131 dst->ac97_center = *src++;
132 dst->ac97_tag |= TAG_PCM_CENTER;
138 EXPORT_SYMBOL(bf5xx_pcm_to_ac97);
140 void bf5xx_ac97_to_pcm(const struct ac97_frame *src, __u16 *dst,
144 *(dst++) = src->ac97_pcm_l;
145 *(dst++) = src->ac97_pcm_r;
149 EXPORT_SYMBOL(bf5xx_ac97_to_pcm);
151 static unsigned int sport_tx_curr_frag(struct sport_device *sport)
153 return sport->tx_curr_frag = sport_curr_offset_tx(sport) /
157 static void enqueue_cmd(struct snd_ac97 *ac97, __u16 addr, __u16 data)
159 struct sport_device *sport = sport_handle;
160 int nextfrag = sport_tx_curr_frag(sport);
161 struct ac97_frame *nextwrite;
163 sport_incfrag(sport, &nextfrag, 1);
165 nextwrite = (struct ac97_frame *)(sport->tx_buf +
166 nextfrag * sport->tx_fragsize);
167 pr_debug("sport->tx_buf:%p, nextfrag:0x%x nextwrite:%p, cmd_count:%d\n",
168 sport->tx_buf, nextfrag, nextwrite, cmd_count[nextfrag]);
169 nextwrite[cmd_count[nextfrag]].ac97_tag |= TAG_CMD;
170 nextwrite[cmd_count[nextfrag]].ac97_addr = addr;
171 nextwrite[cmd_count[nextfrag]].ac97_data = data;
172 ++cmd_count[nextfrag];
173 pr_debug("ac97_sport: Inserting %02x/%04x into fragment %d\n",
174 addr >> 8, data, nextfrag);
177 static unsigned short bf5xx_ac97_read(struct snd_ac97 *ac97,
180 struct ac97_frame out_frame[2], in_frame[2];
182 pr_debug("%s enter 0x%x\n", __func__, reg);
184 /* When dma descriptor is enabled, the register should not be read */
185 if (sport_handle->tx_run || sport_handle->rx_run) {
186 pr_err("Could you send a mail to cliff.cai@analog.com "
187 "to report this?\n");
191 memset(&out_frame, 0, 2 * sizeof(struct ac97_frame));
192 memset(&in_frame, 0, 2 * sizeof(struct ac97_frame));
193 out_frame[0].ac97_tag = TAG_VALID | TAG_CMD;
194 out_frame[0].ac97_addr = ((reg << 8) | 0x8000);
195 sport_send_and_recv(sport_handle, (unsigned char *)&out_frame,
196 (unsigned char *)&in_frame,
197 2 * sizeof(struct ac97_frame));
198 return in_frame[1].ac97_data;
201 void bf5xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
204 pr_debug("%s enter 0x%x:0x%04x\n", __func__, reg, val);
206 if (sport_handle->tx_run) {
207 enqueue_cmd(ac97, (reg << 8), val); /* write */
208 enqueue_cmd(ac97, (reg << 8) | 0x8000, 0); /* read back */
210 struct ac97_frame frame;
211 memset(&frame, 0, sizeof(struct ac97_frame));
212 frame.ac97_tag = TAG_VALID | TAG_CMD;
213 frame.ac97_addr = (reg << 8);
214 frame.ac97_data = val;
215 sport_send_and_recv(sport_handle, (unsigned char *)&frame, \
216 NULL, sizeof(struct ac97_frame));
220 static void bf5xx_ac97_warm_reset(struct snd_ac97 *ac97)
222 #if defined(CONFIG_BF54x) || defined(CONFIG_BF561) || \
223 (defined(BF537_FAMILY) && (CONFIG_SND_BF5XX_SPORT_NUM == 1))
225 #define CONCAT(a, b, c) a ## b ## c
226 #define BFIN_SPORT_RFS(x) CONCAT(P_SPORT, x, _RFS)
228 u16 per = BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM);
229 u16 gpio = P_IDENT(BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM));
231 pr_debug("%s enter\n", __func__);
233 peripheral_free(per);
234 gpio_request(gpio, "bf5xx-ac97");
235 gpio_direction_output(gpio, 1);
237 gpio_set_value(gpio, 0);
240 peripheral_request(per, "soc-audio");
242 pr_info("%s: Not implemented\n", __func__);
246 static void bf5xx_ac97_cold_reset(struct snd_ac97 *ac97)
248 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
249 pr_debug("%s enter\n", __func__);
251 /* It is specified for bf548-ezkit */
252 gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 0);
253 /* Keep reset pin low for 1 ms */
255 gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 1);
256 /* Wait for bit clock recover */
259 pr_info("%s: Not implemented\n", __func__);
263 struct snd_ac97_bus_ops soc_ac97_ops = {
264 .read = bf5xx_ac97_read,
265 .write = bf5xx_ac97_write,
266 .warm_reset = bf5xx_ac97_warm_reset,
267 .reset = bf5xx_ac97_cold_reset,
269 EXPORT_SYMBOL_GPL(soc_ac97_ops);
272 static int bf5xx_ac97_suspend(struct platform_device *pdev,
273 struct snd_soc_dai *dai)
275 struct sport_device *sport =
276 (struct sport_device *)dai->private_data;
278 pr_debug("%s : sport %d\n", __func__, dai->id);
281 if (dai->capture.active)
282 sport_rx_stop(sport);
283 if (dai->playback.active)
284 sport_tx_stop(sport);
288 static int bf5xx_ac97_resume(struct platform_device *pdev,
289 struct snd_soc_dai *dai)
292 struct sport_device *sport =
293 (struct sport_device *)dai->private_data;
295 pr_debug("%s : sport %d\n", __func__, dai->id);
299 ret = sport_set_multichannel(sport_handle, 16, 0x1F, 1);
301 pr_err("SPORT is busy!\n");
305 ret = sport_config_rx(sport_handle, IRFS, 0xF, 0, (16*16-1));
307 pr_err("SPORT is busy!\n");
311 ret = sport_config_tx(sport_handle, ITFS, 0xF, 0, (16*16-1));
313 pr_err("SPORT is busy!\n");
317 if (dai->capture.active)
318 sport_rx_start(sport);
319 if (dai->playback.active)
320 sport_tx_start(sport);
325 #define bf5xx_ac97_suspend NULL
326 #define bf5xx_ac97_resume NULL
329 static int bf5xx_ac97_probe(struct platform_device *pdev,
330 struct snd_soc_dai *dai)
333 cmd_count = (int *)get_zeroed_page(GFP_KERNEL);
334 if (cmd_count == NULL)
337 if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
338 pr_err("Requesting Peripherals failed\n");
343 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
344 /* Request PB3 as reset pin */
345 if (gpio_request(CONFIG_SND_BF5XX_RESET_GPIO_NUM, "SND_AD198x RESET")) {
346 pr_err("Failed to request GPIO_%d for reset\n",
347 CONFIG_SND_BF5XX_RESET_GPIO_NUM);
351 gpio_direction_output(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 1);
353 sport_handle = sport_init(&sport_params[sport_num], 2, \
354 sizeof(struct ac97_frame), NULL);
359 /*SPORT works in TDM mode to simulate AC97 transfers*/
360 ret = sport_set_multichannel(sport_handle, 16, 0x1F, 1);
362 pr_err("SPORT is busy!\n");
364 goto sport_config_err;
367 ret = sport_config_rx(sport_handle, IRFS, 0xF, 0, (16*16-1));
369 pr_err("SPORT is busy!\n");
371 goto sport_config_err;
374 ret = sport_config_tx(sport_handle, ITFS, 0xF, 0, (16*16-1));
376 pr_err("SPORT is busy!\n");
378 goto sport_config_err;
386 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
387 gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM);
390 peripheral_free_list(&sport_req[sport_num][0]);
392 free_page((unsigned long)cmd_count);
398 static void bf5xx_ac97_remove(struct platform_device *pdev,
399 struct snd_soc_dai *dai)
401 free_page((unsigned long)cmd_count);
403 peripheral_free_list(&sport_req[sport_num][0]);
404 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
405 gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM);
409 struct snd_soc_dai bfin_ac97_dai = {
410 .name = "bf5xx-ac97",
412 .type = SND_SOC_DAI_AC97,
413 .probe = bf5xx_ac97_probe,
414 .remove = bf5xx_ac97_remove,
415 .suspend = bf5xx_ac97_suspend,
416 .resume = bf5xx_ac97_resume,
418 .stream_name = "AC97 Playback",
420 #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
425 .rates = SNDRV_PCM_RATE_48000,
426 .formats = SNDRV_PCM_FMTBIT_S16_LE, },
428 .stream_name = "AC97 Capture",
431 .rates = SNDRV_PCM_RATE_48000,
432 .formats = SNDRV_PCM_FMTBIT_S16_LE, },
434 EXPORT_SYMBOL_GPL(bfin_ac97_dai);
436 MODULE_AUTHOR("Roy Huang");
437 MODULE_DESCRIPTION("AC97 driver for ADI Blackfin");
438 MODULE_LICENSE("GPL");