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>
19 #include <linux/slab.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/ac97_codec.h>
24 #include <sound/initval.h>
25 #include <sound/soc.h>
28 #include <asm/portmux.h>
29 #include <linux/mutex.h>
30 #include <linux/gpio.h>
32 #include "bf5xx-sport.h"
33 #include "bf5xx-ac97.h"
36 * 05000250 - AD1980 is running in TDM mode and RFS/TFS are generated by SPORT
37 * contrtoller. But, RFSDIV and TFSDIV are always set to 16*16-1,
38 * while the max AC97 data size is 13*16. The DIV is always larger
39 * than data size. AD73311 and ad2602 are not running in TDM mode.
40 * AD1836 and AD73322 depend on external RFS/TFS only. So, this
41 * anomaly does not affect blackfin sound drivers.
44 static struct sport_device
*ac97_sport_handle
;
46 void bf5xx_pcm_to_ac97(struct ac97_frame
*dst
, const __u16
*src
,
47 size_t count
, unsigned int chan_mask
)
50 dst
->ac97_tag
= TAG_VALID
;
51 if (chan_mask
& SP_FL
) {
52 dst
->ac97_pcm_r
= *src
++;
53 dst
->ac97_tag
|= TAG_PCM_RIGHT
;
55 if (chan_mask
& SP_FR
) {
56 dst
->ac97_pcm_l
= *src
++;
57 dst
->ac97_tag
|= TAG_PCM_LEFT
;
60 #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
61 if (chan_mask
& SP_SR
) {
62 dst
->ac97_sl
= *src
++;
63 dst
->ac97_tag
|= TAG_PCM_SL
;
65 if (chan_mask
& SP_SL
) {
66 dst
->ac97_sr
= *src
++;
67 dst
->ac97_tag
|= TAG_PCM_SR
;
69 if (chan_mask
& SP_LFE
) {
70 dst
->ac97_lfe
= *src
++;
71 dst
->ac97_tag
|= TAG_PCM_LFE
;
73 if (chan_mask
& SP_FC
) {
74 dst
->ac97_center
= *src
++;
75 dst
->ac97_tag
|= TAG_PCM_CENTER
;
81 EXPORT_SYMBOL(bf5xx_pcm_to_ac97
);
83 void bf5xx_ac97_to_pcm(const struct ac97_frame
*src
, __u16
*dst
,
87 *(dst
++) = src
->ac97_pcm_l
;
88 *(dst
++) = src
->ac97_pcm_r
;
92 EXPORT_SYMBOL(bf5xx_ac97_to_pcm
);
94 static unsigned int sport_tx_curr_frag(struct sport_device
*sport
)
96 return sport
->tx_curr_frag
= sport_curr_offset_tx(sport
) /
100 static void enqueue_cmd(struct snd_ac97
*ac97
, __u16 addr
, __u16 data
)
102 struct sport_device
*sport
= ac97_sport_handle
;
103 int *cmd_count
= sport
->private_data
;
104 int nextfrag
= sport_tx_curr_frag(sport
);
105 struct ac97_frame
*nextwrite
;
107 sport_incfrag(sport
, &nextfrag
, 1);
109 nextwrite
= (struct ac97_frame
*)(sport
->tx_buf
+
110 nextfrag
* sport
->tx_fragsize
);
111 pr_debug("sport->tx_buf:%p, nextfrag:0x%x nextwrite:%p, cmd_count:%d\n",
112 sport
->tx_buf
, nextfrag
, nextwrite
, cmd_count
[nextfrag
]);
113 nextwrite
[cmd_count
[nextfrag
]].ac97_tag
|= TAG_CMD
;
114 nextwrite
[cmd_count
[nextfrag
]].ac97_addr
= addr
;
115 nextwrite
[cmd_count
[nextfrag
]].ac97_data
= data
;
116 ++cmd_count
[nextfrag
];
117 pr_debug("ac97_sport: Inserting %02x/%04x into fragment %d\n",
118 addr
>> 8, data
, nextfrag
);
121 static unsigned short bf5xx_ac97_read(struct snd_ac97
*ac97
,
124 struct sport_device
*sport_handle
= ac97_sport_handle
;
125 struct ac97_frame out_frame
[2], in_frame
[2];
127 pr_debug("%s enter 0x%x\n", __func__
, reg
);
129 /* When dma descriptor is enabled, the register should not be read */
130 if (sport_handle
->tx_run
|| sport_handle
->rx_run
) {
131 pr_err("Could you send a mail to cliff.cai@analog.com "
132 "to report this?\n");
136 memset(&out_frame
, 0, 2 * sizeof(struct ac97_frame
));
137 memset(&in_frame
, 0, 2 * sizeof(struct ac97_frame
));
138 out_frame
[0].ac97_tag
= TAG_VALID
| TAG_CMD
;
139 out_frame
[0].ac97_addr
= ((reg
<< 8) | 0x8000);
140 sport_send_and_recv(sport_handle
, (unsigned char *)&out_frame
,
141 (unsigned char *)&in_frame
,
142 2 * sizeof(struct ac97_frame
));
143 return in_frame
[1].ac97_data
;
146 void bf5xx_ac97_write(struct snd_ac97
*ac97
, unsigned short reg
,
149 struct sport_device
*sport_handle
= ac97_sport_handle
;
151 pr_debug("%s enter 0x%x:0x%04x\n", __func__
, reg
, val
);
153 if (sport_handle
->tx_run
) {
154 enqueue_cmd(ac97
, (reg
<< 8), val
); /* write */
155 enqueue_cmd(ac97
, (reg
<< 8) | 0x8000, 0); /* read back */
157 struct ac97_frame frame
;
158 memset(&frame
, 0, sizeof(struct ac97_frame
));
159 frame
.ac97_tag
= TAG_VALID
| TAG_CMD
;
160 frame
.ac97_addr
= (reg
<< 8);
161 frame
.ac97_data
= val
;
162 sport_send_and_recv(sport_handle
, (unsigned char *)&frame
, \
163 NULL
, sizeof(struct ac97_frame
));
167 static void bf5xx_ac97_warm_reset(struct snd_ac97
*ac97
)
169 struct sport_device
*sport_handle
= ac97_sport_handle
;
170 u16 gpio
= P_IDENT(sport_handle
->pin_req
[3]);
172 pr_debug("%s enter\n", __func__
);
174 peripheral_free_list(sport_handle
->pin_req
);
175 gpio_request(gpio
, "bf5xx-ac97");
176 gpio_direction_output(gpio
, 1);
178 gpio_set_value(gpio
, 0);
181 peripheral_request_list(sport_handle
->pin_req
, "soc-audio");
184 static void bf5xx_ac97_cold_reset(struct snd_ac97
*ac97
)
186 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
187 pr_debug("%s enter\n", __func__
);
189 /* It is specified for bf548-ezkit */
190 gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM
, 0);
191 /* Keep reset pin low for 1 ms */
193 gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM
, 1);
194 /* Wait for bit clock recover */
197 pr_info("%s: Not implemented\n", __func__
);
201 struct snd_ac97_bus_ops soc_ac97_ops
= {
202 .read
= bf5xx_ac97_read
,
203 .write
= bf5xx_ac97_write
,
204 .warm_reset
= bf5xx_ac97_warm_reset
,
205 .reset
= bf5xx_ac97_cold_reset
,
207 EXPORT_SYMBOL_GPL(soc_ac97_ops
);
210 static int bf5xx_ac97_suspend(struct snd_soc_dai
*dai
)
212 struct sport_device
*sport
= snd_soc_dai_get_drvdata(dai
);
214 pr_debug("%s : sport %d\n", __func__
, dai
->id
);
217 if (dai
->capture_active
)
218 sport_rx_stop(sport
);
219 if (dai
->playback_active
)
220 sport_tx_stop(sport
);
224 static int bf5xx_ac97_resume(struct snd_soc_dai
*dai
)
227 struct sport_device
*sport
= snd_soc_dai_get_drvdata(dai
);
229 pr_debug("%s : sport %d\n", __func__
, dai
->id
);
233 #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
234 ret
= sport_set_multichannel(sport
, 16, 0x3FF, 1);
236 ret
= sport_set_multichannel(sport
, 16, 0x1F, 1);
239 pr_err("SPORT is busy!\n");
243 ret
= sport_config_rx(sport
, IRFS
, 0xF, 0, (16*16-1));
245 pr_err("SPORT is busy!\n");
249 ret
= sport_config_tx(sport
, ITFS
, 0xF, 0, (16*16-1));
251 pr_err("SPORT is busy!\n");
259 #define bf5xx_ac97_suspend NULL
260 #define bf5xx_ac97_resume NULL
263 static struct snd_soc_dai_driver bfin_ac97_dai
= {
265 .suspend
= bf5xx_ac97_suspend
,
266 .resume
= bf5xx_ac97_resume
,
268 .stream_name
= "AC97 Playback",
270 #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
275 .rates
= SNDRV_PCM_RATE_48000
,
276 .formats
= SNDRV_PCM_FMTBIT_S16_LE
, },
278 .stream_name
= "AC97 Capture",
281 .rates
= SNDRV_PCM_RATE_48000
,
282 .formats
= SNDRV_PCM_FMTBIT_S16_LE
, },
285 static int __devinit
asoc_bfin_ac97_probe(struct platform_device
*pdev
)
287 struct sport_device
*sport_handle
;
290 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
291 /* Request PB3 as reset pin */
292 if (gpio_request(CONFIG_SND_BF5XX_RESET_GPIO_NUM
, "SND_AD198x RESET")) {
293 pr_err("Failed to request GPIO_%d for reset\n",
294 CONFIG_SND_BF5XX_RESET_GPIO_NUM
);
298 gpio_direction_output(CONFIG_SND_BF5XX_RESET_GPIO_NUM
, 1);
301 sport_handle
= sport_init(pdev
, 2, sizeof(struct ac97_frame
),
308 /*SPORT works in TDM mode to simulate AC97 transfers*/
309 #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
310 ret
= sport_set_multichannel(sport_handle
, 16, 0x3FF, 1);
312 ret
= sport_set_multichannel(sport_handle
, 16, 0x1F, 1);
315 pr_err("SPORT is busy!\n");
317 goto sport_config_err
;
320 ret
= sport_config_rx(sport_handle
, IRFS
, 0xF, 0, (16*16-1));
322 pr_err("SPORT is busy!\n");
324 goto sport_config_err
;
327 ret
= sport_config_tx(sport_handle
, ITFS
, 0xF, 0, (16*16-1));
329 pr_err("SPORT is busy!\n");
331 goto sport_config_err
;
334 ret
= snd_soc_register_dai(&pdev
->dev
, &bfin_ac97_dai
);
336 pr_err("Failed to register DAI: %d\n", ret
);
337 goto sport_config_err
;
340 ac97_sport_handle
= sport_handle
;
345 sport_done(sport_handle
);
347 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
348 gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM
);
355 static int __devexit
asoc_bfin_ac97_remove(struct platform_device
*pdev
)
357 struct sport_device
*sport_handle
= platform_get_drvdata(pdev
);
359 snd_soc_unregister_dai(&pdev
->dev
);
360 sport_done(sport_handle
);
361 #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
362 gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM
);
368 static struct platform_driver asoc_bfin_ac97_driver
= {
371 .owner
= THIS_MODULE
,
374 .probe
= asoc_bfin_ac97_probe
,
375 .remove
= __devexit_p(asoc_bfin_ac97_remove
),
378 static int __init
bfin_ac97_init(void)
380 return platform_driver_register(&asoc_bfin_ac97_driver
);
382 module_init(bfin_ac97_init
);
384 static void __exit
bfin_ac97_exit(void)
386 platform_driver_unregister(&asoc_bfin_ac97_driver
);
388 module_exit(bfin_ac97_exit
);
391 MODULE_AUTHOR("Roy Huang");
392 MODULE_DESCRIPTION("AC97 driver for ADI Blackfin");
393 MODULE_LICENSE("GPL");