Cliff Cai | f202862 | 2008-09-05 18:21:37 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * bf5xx-ac97.c -- AC97 support for the ADI blackfin chip. |
| 3 | * |
| 4 | * Author: Roy Huang |
| 5 | * Created: 11th. June 2007 |
| 6 | * Copyright: Analog Device Inc. |
| 7 | * |
| 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. |
| 11 | */ |
| 12 | |
| 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 | |
| 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> |
| 25 | |
| 26 | #include <asm/irq.h> |
| 27 | #include <asm/portmux.h> |
| 28 | #include <linux/mutex.h> |
| 29 | #include <linux/gpio.h> |
| 30 | |
| 31 | #include "bf5xx-sport.h" |
| 32 | #include "bf5xx-ac97.h" |
| 33 | |
| 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} |
| 37 | |
| 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} |
| 40 | |
| 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} |
| 43 | |
| 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} |
| 46 | #else |
| 47 | #define PIN_REQ_SPORT_0 {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS, \ |
| 48 | P_SPORT0_DRPRI, P_SPORT0_RSCLK, 0} |
| 49 | |
| 50 | #define PIN_REQ_SPORT_1 {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, \ |
| 51 | P_SPORT1_DRPRI, P_SPORT1_RSCLK, 0} |
| 52 | #endif |
| 53 | |
| 54 | static int *cmd_count; |
| 55 | static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM; |
| 56 | |
| 57 | #if defined(CONFIG_BF54x) |
| 58 | static struct sport_param sport_params[4] = { |
| 59 | { |
| 60 | .dma_rx_chan = CH_SPORT0_RX, |
| 61 | .dma_tx_chan = CH_SPORT0_TX, |
| 62 | .err_irq = IRQ_SPORT0_ERR, |
| 63 | .regs = (struct sport_register *)SPORT0_TCR1, |
| 64 | }, |
| 65 | { |
| 66 | .dma_rx_chan = CH_SPORT1_RX, |
| 67 | .dma_tx_chan = CH_SPORT1_TX, |
| 68 | .err_irq = IRQ_SPORT1_ERR, |
| 69 | .regs = (struct sport_register *)SPORT1_TCR1, |
| 70 | }, |
| 71 | { |
| 72 | .dma_rx_chan = CH_SPORT2_RX, |
| 73 | .dma_tx_chan = CH_SPORT2_TX, |
| 74 | .err_irq = IRQ_SPORT2_ERR, |
| 75 | .regs = (struct sport_register *)SPORT2_TCR1, |
| 76 | }, |
| 77 | { |
| 78 | .dma_rx_chan = CH_SPORT3_RX, |
| 79 | .dma_tx_chan = CH_SPORT3_TX, |
| 80 | .err_irq = IRQ_SPORT3_ERR, |
| 81 | .regs = (struct sport_register *)SPORT3_TCR1, |
| 82 | } |
| 83 | }; |
| 84 | #else |
| 85 | static struct sport_param sport_params[2] = { |
| 86 | { |
| 87 | .dma_rx_chan = CH_SPORT0_RX, |
| 88 | .dma_tx_chan = CH_SPORT0_TX, |
| 89 | .err_irq = IRQ_SPORT0_ERROR, |
| 90 | .regs = (struct sport_register *)SPORT0_TCR1, |
| 91 | }, |
| 92 | { |
| 93 | .dma_rx_chan = CH_SPORT1_RX, |
| 94 | .dma_tx_chan = CH_SPORT1_TX, |
| 95 | .err_irq = IRQ_SPORT1_ERROR, |
| 96 | .regs = (struct sport_register *)SPORT1_TCR1, |
| 97 | } |
| 98 | }; |
| 99 | #endif |
| 100 | |
| 101 | void bf5xx_pcm_to_ac97(struct ac97_frame *dst, const __u32 *src, \ |
| 102 | size_t count) |
| 103 | { |
| 104 | while (count--) { |
| 105 | dst->ac97_tag = TAG_VALID | TAG_PCM; |
| 106 | (dst++)->ac97_pcm = *src++; |
| 107 | } |
| 108 | } |
| 109 | EXPORT_SYMBOL(bf5xx_pcm_to_ac97); |
| 110 | |
| 111 | void bf5xx_ac97_to_pcm(const struct ac97_frame *src, __u32 *dst, \ |
| 112 | size_t count) |
| 113 | { |
| 114 | while (count--) |
| 115 | *(dst++) = (src++)->ac97_pcm; |
| 116 | } |
| 117 | EXPORT_SYMBOL(bf5xx_ac97_to_pcm); |
| 118 | |
| 119 | static unsigned int sport_tx_curr_frag(struct sport_device *sport) |
| 120 | { |
| 121 | return sport->tx_curr_frag = sport_curr_offset_tx(sport) / \ |
| 122 | sport->tx_fragsize; |
| 123 | } |
| 124 | |
| 125 | static void enqueue_cmd(struct snd_ac97 *ac97, __u16 addr, __u16 data) |
| 126 | { |
| 127 | struct sport_device *sport = sport_handle; |
| 128 | int nextfrag = sport_tx_curr_frag(sport); |
| 129 | struct ac97_frame *nextwrite; |
| 130 | |
| 131 | sport_incfrag(sport, &nextfrag, 1); |
| 132 | sport_incfrag(sport, &nextfrag, 1); |
| 133 | |
| 134 | nextwrite = (struct ac97_frame *)(sport->tx_buf + \ |
| 135 | nextfrag * sport->tx_fragsize); |
| 136 | pr_debug("sport->tx_buf:%p, nextfrag:0x%x nextwrite:%p, cmd_count:%d\n", |
| 137 | sport->tx_buf, nextfrag, nextwrite, cmd_count[nextfrag]); |
| 138 | nextwrite[cmd_count[nextfrag]].ac97_tag |= TAG_CMD; |
| 139 | nextwrite[cmd_count[nextfrag]].ac97_addr = addr; |
| 140 | nextwrite[cmd_count[nextfrag]].ac97_data = data; |
| 141 | ++cmd_count[nextfrag]; |
| 142 | pr_debug("ac97_sport: Inserting %02x/%04x into fragment %d\n", |
| 143 | addr >> 8, data, nextfrag); |
| 144 | } |
| 145 | |
| 146 | static unsigned short bf5xx_ac97_read(struct snd_ac97 *ac97, |
| 147 | unsigned short reg) |
| 148 | { |
| 149 | struct ac97_frame out_frame[2], in_frame[2]; |
| 150 | |
| 151 | pr_debug("%s enter 0x%x\n", __func__, reg); |
| 152 | |
| 153 | /* When dma descriptor is enabled, the register should not be read */ |
| 154 | if (sport_handle->tx_run || sport_handle->rx_run) { |
| 155 | pr_err("Could you send a mail to cliff.cai@analog.com " |
| 156 | "to report this?\n"); |
| 157 | return -EFAULT; |
| 158 | } |
| 159 | |
| 160 | memset(&out_frame, 0, 2 * sizeof(struct ac97_frame)); |
| 161 | memset(&in_frame, 0, 2 * sizeof(struct ac97_frame)); |
| 162 | out_frame[0].ac97_tag = TAG_VALID | TAG_CMD; |
| 163 | out_frame[0].ac97_addr = ((reg << 8) | 0x8000); |
| 164 | sport_send_and_recv(sport_handle, (unsigned char *)&out_frame, |
| 165 | (unsigned char *)&in_frame, |
| 166 | 2 * sizeof(struct ac97_frame)); |
| 167 | return in_frame[1].ac97_data; |
| 168 | } |
| 169 | |
| 170 | void bf5xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg, |
| 171 | unsigned short val) |
| 172 | { |
| 173 | pr_debug("%s enter 0x%x:0x%04x\n", __func__, reg, val); |
| 174 | |
| 175 | if (sport_handle->tx_run) { |
| 176 | enqueue_cmd(ac97, (reg << 8), val); /* write */ |
| 177 | enqueue_cmd(ac97, (reg << 8) | 0x8000, 0); /* read back */ |
| 178 | } else { |
| 179 | struct ac97_frame frame; |
| 180 | memset(&frame, 0, sizeof(struct ac97_frame)); |
| 181 | frame.ac97_tag = TAG_VALID | TAG_CMD; |
| 182 | frame.ac97_addr = (reg << 8); |
| 183 | frame.ac97_data = val; |
| 184 | sport_send_and_recv(sport_handle, (unsigned char *)&frame, \ |
| 185 | NULL, sizeof(struct ac97_frame)); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static void bf5xx_ac97_warm_reset(struct snd_ac97 *ac97) |
| 190 | { |
| 191 | #if defined(CONFIG_BF54x) || defined(CONFIG_BF561) || \ |
| 192 | (defined(BF537_FAMILY) && (CONFIG_SND_BF5XX_SPORT_NUM == 1)) |
| 193 | |
| 194 | #define CONCAT(a, b, c) a ## b ## c |
| 195 | #define BFIN_SPORT_RFS(x) CONCAT(P_SPORT, x, _RFS) |
| 196 | |
| 197 | u16 per = BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM); |
| 198 | u16 gpio = P_IDENT(BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM)); |
| 199 | |
| 200 | pr_debug("%s enter\n", __func__); |
| 201 | |
| 202 | peripheral_free(per); |
| 203 | gpio_request(gpio, "bf5xx-ac97"); |
| 204 | gpio_direction_output(gpio, 1); |
| 205 | udelay(2); |
| 206 | gpio_set_value(gpio, 0); |
| 207 | udelay(1); |
| 208 | gpio_free(gpio); |
| 209 | peripheral_request(per, "soc-audio"); |
| 210 | #else |
| 211 | pr_info("%s: Not implemented\n", __func__); |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | static void bf5xx_ac97_cold_reset(struct snd_ac97 *ac97) |
| 216 | { |
| 217 | #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET |
| 218 | pr_debug("%s enter\n", __func__); |
| 219 | |
| 220 | /* It is specified for bf548-ezkit */ |
| 221 | gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 0); |
| 222 | /* Keep reset pin low for 1 ms */ |
| 223 | mdelay(1); |
| 224 | gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 1); |
| 225 | /* Wait for bit clock recover */ |
| 226 | mdelay(1); |
| 227 | #else |
| 228 | pr_info("%s: Not implemented\n", __func__); |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | struct snd_ac97_bus_ops soc_ac97_ops = { |
| 233 | .read = bf5xx_ac97_read, |
| 234 | .write = bf5xx_ac97_write, |
| 235 | .warm_reset = bf5xx_ac97_warm_reset, |
| 236 | .reset = bf5xx_ac97_cold_reset, |
| 237 | }; |
| 238 | EXPORT_SYMBOL_GPL(soc_ac97_ops); |
| 239 | |
| 240 | #ifdef CONFIG_PM |
| 241 | static int bf5xx_ac97_suspend(struct platform_device *pdev, |
| 242 | struct snd_soc_dai *dai) |
| 243 | { |
| 244 | struct sport_device *sport = |
| 245 | (struct sport_device *)dai->private_data; |
| 246 | |
| 247 | pr_debug("%s : sport %d\n", __func__, dai->id); |
| 248 | if (!dai->active) |
| 249 | return 0; |
| 250 | if (dai->capture.active) |
| 251 | sport_rx_stop(sport); |
| 252 | if (dai->playback.active) |
| 253 | sport_tx_stop(sport); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int bf5xx_ac97_resume(struct platform_device *pdev, |
| 258 | struct snd_soc_dai *dai) |
| 259 | { |
| 260 | int ret; |
| 261 | struct sport_device *sport = |
| 262 | (struct sport_device *)dai->private_data; |
| 263 | |
| 264 | pr_debug("%s : sport %d\n", __func__, dai->id); |
| 265 | if (!dai->active) |
| 266 | return 0; |
| 267 | |
| 268 | ret = sport_set_multichannel(sport_handle, 16, 0x1F, 1); |
| 269 | if (ret) { |
| 270 | pr_err("SPORT is busy!\n"); |
| 271 | return -EBUSY; |
| 272 | } |
| 273 | |
| 274 | ret = sport_config_rx(sport_handle, IRFS, 0xF, 0, (16*16-1)); |
| 275 | if (ret) { |
| 276 | pr_err("SPORT is busy!\n"); |
| 277 | return -EBUSY; |
| 278 | } |
| 279 | |
| 280 | ret = sport_config_tx(sport_handle, ITFS, 0xF, 0, (16*16-1)); |
| 281 | if (ret) { |
| 282 | pr_err("SPORT is busy!\n"); |
| 283 | return -EBUSY; |
| 284 | } |
| 285 | |
| 286 | if (dai->capture.active) |
| 287 | sport_rx_start(sport); |
| 288 | if (dai->playback.active) |
| 289 | sport_tx_start(sport); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | #else |
| 294 | #define bf5xx_ac97_suspend NULL |
| 295 | #define bf5xx_ac97_resume NULL |
| 296 | #endif |
| 297 | |
| 298 | static int bf5xx_ac97_probe(struct platform_device *pdev, |
| 299 | struct snd_soc_dai *dai) |
| 300 | { |
| 301 | int ret; |
| 302 | #if defined(CONFIG_BF54x) |
| 303 | u16 sport_req[][7] = {PIN_REQ_SPORT_0, PIN_REQ_SPORT_1, |
| 304 | PIN_REQ_SPORT_2, PIN_REQ_SPORT_3}; |
| 305 | #else |
| 306 | u16 sport_req[][7] = {PIN_REQ_SPORT_0, PIN_REQ_SPORT_1}; |
| 307 | #endif |
| 308 | cmd_count = (int *)get_zeroed_page(GFP_KERNEL); |
| 309 | if (cmd_count == NULL) |
| 310 | return -ENOMEM; |
| 311 | |
| 312 | if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) { |
| 313 | pr_err("Requesting Peripherals failed\n"); |
| 314 | return -EFAULT; |
| 315 | } |
| 316 | |
| 317 | #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET |
| 318 | /* Request PB3 as reset pin */ |
| 319 | if (gpio_request(CONFIG_SND_BF5XX_RESET_GPIO_NUM, "SND_AD198x RESET")) { |
| 320 | pr_err("Failed to request GPIO_%d for reset\n", |
| 321 | CONFIG_SND_BF5XX_RESET_GPIO_NUM); |
| 322 | peripheral_free_list(&sport_req[sport_num][0]); |
| 323 | return -1; |
| 324 | } |
| 325 | gpio_direction_output(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 1); |
| 326 | #endif |
| 327 | sport_handle = sport_init(&sport_params[sport_num], 2, \ |
| 328 | sizeof(struct ac97_frame), NULL); |
| 329 | if (!sport_handle) { |
| 330 | peripheral_free_list(&sport_req[sport_num][0]); |
| 331 | #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET |
| 332 | gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM); |
| 333 | #endif |
| 334 | return -ENODEV; |
| 335 | } |
| 336 | /*SPORT works in TDM mode to simulate AC97 transfers*/ |
| 337 | ret = sport_set_multichannel(sport_handle, 16, 0x1F, 1); |
| 338 | if (ret) { |
| 339 | pr_err("SPORT is busy!\n"); |
| 340 | kfree(sport_handle); |
| 341 | peripheral_free_list(&sport_req[sport_num][0]); |
| 342 | #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET |
| 343 | gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM); |
| 344 | #endif |
| 345 | return -EBUSY; |
| 346 | } |
| 347 | |
| 348 | ret = sport_config_rx(sport_handle, IRFS, 0xF, 0, (16*16-1)); |
| 349 | if (ret) { |
| 350 | pr_err("SPORT is busy!\n"); |
| 351 | kfree(sport_handle); |
| 352 | peripheral_free_list(&sport_req[sport_num][0]); |
| 353 | #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET |
| 354 | gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM); |
| 355 | #endif |
| 356 | return -EBUSY; |
| 357 | } |
| 358 | |
| 359 | ret = sport_config_tx(sport_handle, ITFS, 0xF, 0, (16*16-1)); |
| 360 | if (ret) { |
| 361 | pr_err("SPORT is busy!\n"); |
| 362 | kfree(sport_handle); |
| 363 | peripheral_free_list(&sport_req[sport_num][0]); |
| 364 | #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET |
| 365 | gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM); |
| 366 | #endif |
| 367 | return -EBUSY; |
| 368 | } |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void bf5xx_ac97_remove(struct platform_device *pdev, |
| 373 | struct snd_soc_dai *dai) |
| 374 | { |
| 375 | free_page((unsigned long)cmd_count); |
| 376 | cmd_count = NULL; |
| 377 | #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET |
| 378 | gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM); |
| 379 | #endif |
| 380 | } |
| 381 | |
| 382 | struct snd_soc_dai bfin_ac97_dai = { |
| 383 | .name = "bf5xx-ac97", |
| 384 | .id = 0, |
| 385 | .type = SND_SOC_DAI_AC97, |
| 386 | .probe = bf5xx_ac97_probe, |
| 387 | .remove = bf5xx_ac97_remove, |
| 388 | .suspend = bf5xx_ac97_suspend, |
| 389 | .resume = bf5xx_ac97_resume, |
| 390 | .playback = { |
| 391 | .stream_name = "AC97 Playback", |
| 392 | .channels_min = 2, |
| 393 | .channels_max = 2, |
| 394 | .rates = SNDRV_PCM_RATE_48000, |
| 395 | .formats = SNDRV_PCM_FMTBIT_S16_LE, }, |
| 396 | .capture = { |
| 397 | .stream_name = "AC97 Capture", |
| 398 | .channels_min = 2, |
| 399 | .channels_max = 2, |
| 400 | .rates = SNDRV_PCM_RATE_48000, |
| 401 | .formats = SNDRV_PCM_FMTBIT_S16_LE, }, |
| 402 | }; |
| 403 | EXPORT_SYMBOL_GPL(bfin_ac97_dai); |
| 404 | |
| 405 | MODULE_AUTHOR("Roy Huang"); |
| 406 | MODULE_DESCRIPTION("AC97 driver for ADI Blackfin"); |
| 407 | MODULE_LICENSE("GPL"); |