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"
35 * 05000250 - AD1980 is running in TDM mode and RFS/TFS are generated by SPORT
36 * contrtoller. But, RFSDIV and TFSDIV are always set to 16*16-1,
37 * while the max AC97 data size is 13*16. The DIV is always larger
38 * than data size. AD73311 and ad2602 are not running in TDM mode.
39 * AD1836 and AD73322 depend on external RFS/TFS only. So, this
40 * anomaly does not affect blackfin sound drivers.
43 static int *cmd_count
;
44 static int sport_num
= CONFIG_SND_BF5XX_SPORT_NUM
;
46 #define SPORT_REQ(x) \
47 [x] = {P_SPORT##x##_TFS, P_SPORT##x##_DTPRI, P_SPORT##x##_TSCLK, \
48 P_SPORT##x##_RFS, P_SPORT##x##_DRPRI, P_SPORT##x##_RSCLK, 0}
49 static u16 sport_req
[][7] = {
64 #define SPORT_PARAMS(x) \
66 .dma_rx_chan = CH_SPORT##x##_RX, \
67 .dma_tx_chan = CH_SPORT##x##_TX, \
68 .err_irq = IRQ_SPORT##x##_ERROR, \
69 .regs = (struct sport_register *)SPORT##x##_TCR1, \
71 static struct sport_param sport_params
[4] = {
86 void bf5xx_pcm_to_ac97(struct ac97_frame
*dst
, const __u16
*src
,
87 size_t count
, unsigned int chan_mask
)
90 dst
->ac97_tag
= TAG_VALID
;
91 if (chan_mask
& SP_FL
) {
92 dst
->ac97_pcm_r
= *src
++;
93 dst
->ac97_tag
|= TAG_PCM_RIGHT
;
95 if (chan_mask
& SP_FR
) {
96 dst
->ac97_pcm_l
= *src
++;
97 dst
->ac97_tag
|= TAG_PCM_LEFT
;
100 #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
101 if (chan_mask
& SP_SR
) {
102 dst
->ac97_sl
= *src
++;
103 dst
->ac97_tag
|= TAG_PCM_SL
;
105 if (chan_mask
& SP_SL
) {
106 dst
->ac97_sr
= *src
++;
107 dst
->ac97_tag
|= TAG_PCM_SR
;
109 if (chan_mask
& SP_LFE
) {
110 dst
->ac97_lfe
= *src
++;
111 dst
->ac97_tag
|= TAG_PCM_LFE
;
113 if (chan_mask
& SP_FC
) {
114 dst
->ac97_center
= *src
++;
115 dst
->ac97_tag
|= TAG_PCM_CENTER
;
121 EXPORT_SYMBOL(bf5xx_pcm_to_ac97
);
123 void bf5xx_ac97_to_pcm(const struct ac97_frame
*src
, __u16
*dst
,
127 *(dst
++) = src
->ac97_pcm_l
;
128 *(dst
++) = src
->ac97_pcm_r
;
132 EXPORT_SYMBOL(bf5xx_ac97_to_pcm
);
134 static unsigned int sport_tx_curr_frag(struct sport_device
*sport
)
136 return sport
->tx_curr_frag
= sport_curr_offset_tx(sport
) /
140 static void enqueue_cmd(struct snd_ac97
*ac97
, __u16 addr
, __u16 data
)
142 struct sport_device
*sport
= sport_handle
;
143 int nextfrag
= sport_tx_curr_frag(sport
);
144 struct ac97_frame
*nextwrite
;
146 sport_incfrag(sport
, &nextfrag
, 1);
148 nextwrite
= (struct ac97_frame
*)(sport
->tx_buf
+
149 nextfrag
* sport
->tx_fragsize
);
150 pr_debug("sport->tx_buf:%p, nextfrag:0x%x nextwrite:%p, cmd_count:%d\n",
151 sport
->tx_buf
, nextfrag
, nextwrite
, cmd_count
[nextfrag
]);
152 nextwrite
[cmd_count
[nextfrag
]].ac97_tag
|= TAG_CMD
;
153 nextwrite
[cmd_count
[nextfrag
]].ac97_addr
= addr
;
154 nextwrite
[cmd_count
[nextfrag
]].ac97_data
= data
;
155 ++cmd_count
[nextfrag
];
156 pr_debug("ac97_sport: Inserting %02x/%04x into fragment %d\n",
157 addr
>> 8, data
, nextfrag
);
160 static unsigned short bf5xx_ac97_read(struct snd_ac97
*ac97
,
163 struct ac97_frame out_frame
[2], in_frame
[2];
165 pr_debug("%s enter 0x%x\n", __func__
, reg
);
167 /* When dma descriptor is enabled, the register should not be read */
168 if (sport_handle
->tx_run
|| sport_handle
->rx_run
) {
169 pr_err("Could you send a mail to cliff.cai@analog.com "
170 "to report this?\n");
174 memset(&out_frame
, 0, 2 * sizeof(struct ac97_frame
));
175 memset(&in_frame
, 0, 2 * sizeof(struct ac97_frame
));
176 out_frame
[0].ac97_tag
= TAG_VALID
| TAG_CMD
;
177 out_frame
[0].ac97_addr
= ((reg
<< 8) | 0x8000);
178 sport_send_and_recv(sport_handle
, (unsigned char *)&out_frame
,
179 (unsigned char *)&in_frame
,
180 2 * sizeof(struct ac97_frame
));
181 return in_frame
[1].ac97_data
;
184 void bf5xx_ac97_write(struct snd_ac97
*ac97
, unsigned short reg
,
187 pr_debug("%s enter 0x%x:0x%04x\n", __func__
, reg
, val
);
189 if (sport_handle
->tx_run
) {
190 enqueue_cmd(ac97
, (reg
<< 8), val
); /* write */
191 enqueue_cmd(ac97
, (reg
<< 8) | 0x8000, 0); /* read back */
193 struct ac97_frame frame
;
194 memset(&frame
, 0, sizeof(struct ac97_frame
));
195 frame
.ac97_tag
= TAG_VALID
| TAG_CMD
;
196 frame
.ac97_addr
= (reg
<< 8);
197 frame
.ac97_data
= val
;
198 sport_send_and_recv(sport_handle
, (unsigned char *)&frame
, \
199 NULL
, sizeof(struct ac97_frame
));
203 static void bf5xx_ac97_warm_reset(struct snd_ac97
*ac97
)
205 #if defined(CONFIG_BF54x) || defined(CONFIG_BF561) || \
206 (defined(BF537_FAMILY) && (CONFIG_SND_BF5XX_SPORT_NUM == 1))
208 #define CONCAT(a, b, c) a ## b ## c
209 #define BFIN_SPORT_RFS(x) CONCAT(P_SPORT, x, _RFS)
211 u16 per
= BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM
);
212 u16 gpio
= P_IDENT(BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM
));
214 pr_debug("%s enter\n", __func__
);
216 peripheral_free(per
);
217 gpio_request(gpio
, "bf5xx-ac97");
218 gpio_direction_output(gpio
, 1);
220 gpio_set_value(gpio
, 0);
223 peripheral_request(per
, "soc-audio");
225 pr_info("%s: Not implemented\n", __func__
);
229 static void bf5xx_ac97_cold_reset(struct snd_ac97
*ac97
)
231 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
232 pr_debug("%s enter\n", __func__
);
234 /* It is specified for bf548-ezkit */
235 gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM
, 0);
236 /* Keep reset pin low for 1 ms */
238 gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM
, 1);
239 /* Wait for bit clock recover */
242 pr_info("%s: Not implemented\n", __func__
);
246 struct snd_ac97_bus_ops soc_ac97_ops
= {
247 .read
= bf5xx_ac97_read
,
248 .write
= bf5xx_ac97_write
,
249 .warm_reset
= bf5xx_ac97_warm_reset
,
250 .reset
= bf5xx_ac97_cold_reset
,
252 EXPORT_SYMBOL_GPL(soc_ac97_ops
);
255 static int bf5xx_ac97_suspend(struct snd_soc_dai
*dai
)
257 struct sport_device
*sport
=
258 (struct sport_device
*)dai
->private_data
;
260 pr_debug("%s : sport %d\n", __func__
, dai
->id
);
263 if (dai
->capture
.active
)
264 sport_rx_stop(sport
);
265 if (dai
->playback
.active
)
266 sport_tx_stop(sport
);
270 static int bf5xx_ac97_resume(struct snd_soc_dai
*dai
)
273 struct sport_device
*sport
=
274 (struct sport_device
*)dai
->private_data
;
276 pr_debug("%s : sport %d\n", __func__
, dai
->id
);
280 ret
= sport_set_multichannel(sport_handle
, 16, 0x1F, 1);
282 pr_err("SPORT is busy!\n");
286 ret
= sport_config_rx(sport_handle
, IRFS
, 0xF, 0, (16*16-1));
288 pr_err("SPORT is busy!\n");
292 ret
= sport_config_tx(sport_handle
, ITFS
, 0xF, 0, (16*16-1));
294 pr_err("SPORT is busy!\n");
298 if (dai
->capture
.active
)
299 sport_rx_start(sport
);
300 if (dai
->playback
.active
)
301 sport_tx_start(sport
);
306 #define bf5xx_ac97_suspend NULL
307 #define bf5xx_ac97_resume NULL
310 static int bf5xx_ac97_probe(struct platform_device
*pdev
,
311 struct snd_soc_dai
*dai
)
314 cmd_count
= (int *)get_zeroed_page(GFP_KERNEL
);
315 if (cmd_count
== NULL
)
318 if (peripheral_request_list(sport_req
[sport_num
], "soc-audio")) {
319 pr_err("Requesting Peripherals failed\n");
324 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
325 /* Request PB3 as reset pin */
326 if (gpio_request(CONFIG_SND_BF5XX_RESET_GPIO_NUM
, "SND_AD198x RESET")) {
327 pr_err("Failed to request GPIO_%d for reset\n",
328 CONFIG_SND_BF5XX_RESET_GPIO_NUM
);
332 gpio_direction_output(CONFIG_SND_BF5XX_RESET_GPIO_NUM
, 1);
334 sport_handle
= sport_init(&sport_params
[sport_num
], 2, \
335 sizeof(struct ac97_frame
), NULL
);
340 /*SPORT works in TDM mode to simulate AC97 transfers*/
341 ret
= sport_set_multichannel(sport_handle
, 16, 0x1F, 1);
343 pr_err("SPORT is busy!\n");
345 goto sport_config_err
;
348 ret
= sport_config_rx(sport_handle
, IRFS
, 0xF, 0, (16*16-1));
350 pr_err("SPORT is busy!\n");
352 goto sport_config_err
;
355 ret
= sport_config_tx(sport_handle
, ITFS
, 0xF, 0, (16*16-1));
357 pr_err("SPORT is busy!\n");
359 goto sport_config_err
;
367 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
368 gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM
);
371 peripheral_free_list(sport_req
[sport_num
]);
373 free_page((unsigned long)cmd_count
);
379 static void bf5xx_ac97_remove(struct platform_device
*pdev
,
380 struct snd_soc_dai
*dai
)
382 free_page((unsigned long)cmd_count
);
384 peripheral_free_list(sport_req
[sport_num
]);
385 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
386 gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM
);
390 struct snd_soc_dai bfin_ac97_dai
= {
391 .name
= "bf5xx-ac97",
394 .probe
= bf5xx_ac97_probe
,
395 .remove
= bf5xx_ac97_remove
,
396 .suspend
= bf5xx_ac97_suspend
,
397 .resume
= bf5xx_ac97_resume
,
399 .stream_name
= "AC97 Playback",
401 #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
406 .rates
= SNDRV_PCM_RATE_48000
,
407 .formats
= SNDRV_PCM_FMTBIT_S16_LE
, },
409 .stream_name
= "AC97 Capture",
412 .rates
= SNDRV_PCM_RATE_48000
,
413 .formats
= SNDRV_PCM_FMTBIT_S16_LE
, },
415 EXPORT_SYMBOL_GPL(bfin_ac97_dai
);
417 static int __init
bfin_ac97_init(void)
419 return snd_soc_register_dai(&bfin_ac97_dai
);
421 module_init(bfin_ac97_init
);
423 static void __exit
bfin_ac97_exit(void)
425 snd_soc_unregister_dai(&bfin_ac97_dai
);
427 module_exit(bfin_ac97_exit
);
429 MODULE_AUTHOR("Roy Huang");
430 MODULE_DESCRIPTION("AC97 driver for ADI Blackfin");
431 MODULE_LICENSE("GPL");