Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Philips UDA1341TS on Compaq iPAQ H3600 soundcard |
| 3 | * Copyright (C) 2002 Tomas Kasparek <tomas.kasparek@seznam.cz> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License. |
| 7 | * |
| 8 | * History: |
| 9 | * |
| 10 | * 2002-03-13 Tomas Kasparek initial release - based on h3600-uda1341.c from OSS |
| 11 | * 2002-03-20 Tomas Kasparek playback over ALSA is working |
| 12 | * 2002-03-28 Tomas Kasparek playback over OSS emulation is working |
| 13 | * 2002-03-29 Tomas Kasparek basic capture is working (native ALSA) |
| 14 | * 2002-03-29 Tomas Kasparek capture is working (OSS emulation) |
| 15 | * 2002-04-04 Tomas Kasparek better rates handling (allow non-standard rates) |
| 16 | * 2003-02-14 Brian Avery fixed full duplex mode, other updates |
| 17 | * 2003-02-20 Tomas Kasparek merged updates by Brian (except HAL) |
| 18 | * 2003-04-19 Jaroslav Kysela recoded DMA stuff to follow 2.4.18rmk3-hh24 kernel |
| 19 | * working suspend and resume |
| 20 | * 2003-04-28 Tomas Kasparek updated work by Jaroslav to compile it under 2.5.x again |
| 21 | * merged HAL layer (patches from Brian) |
| 22 | */ |
| 23 | |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame^] | 24 | /* $Id: sa11xx-uda1341.c,v 1.22 2005/09/05 16:17:58 tiwai Exp $ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | /*************************************************************************************************** |
| 27 | * |
| 28 | * To understand what Alsa Drivers should be doing look at "Writing an Alsa Driver" by Takashi Iwai |
| 29 | * available in the Alsa doc section on the website |
| 30 | * |
| 31 | * A few notes to make things clearer. The UDA1341 is hooked up to Serial port 4 on the SA1100. |
| 32 | * We are using SSP mode to talk to the UDA1341. The UDA1341 bit & wordselect clocks are generated |
| 33 | * by this UART. Unfortunately, the clock only runs if the transmit buffer has something in it. |
| 34 | * So, if we are just recording, we feed the transmit DMA stream a bunch of 0x0000 so that the |
| 35 | * transmit buffer is full and the clock keeps going. The zeroes come from FLUSH_BASE_PHYS which |
| 36 | * is a mem loc that always decodes to 0's w/ no off chip access. |
| 37 | * |
| 38 | * Some alsa terminology: |
| 39 | * frame => num_channels * sample_size e.g stereo 16 bit is 2 * 16 = 32 bytes |
| 40 | * period => the least number of bytes that will generate an interrupt e.g. we have a 1024 byte |
| 41 | * buffer and 4 periods in the runtime structure this means we'll get an int every 256 |
| 42 | * bytes or 4 times per buffer. |
| 43 | * A number of the sizes are in frames rather than bytes, use frames_to_bytes and |
| 44 | * bytes_to_frames to convert. The easiest way to tell the units is to look at the |
| 45 | * type i.e. runtime-> buffer_size is in frames and its type is snd_pcm_uframes_t |
| 46 | * |
| 47 | * Notes about the pointer fxn: |
| 48 | * The pointer fxn needs to return the offset into the dma buffer in frames. |
| 49 | * Interrupts must be blocked before calling the dma_get_pos fxn to avoid race with interrupts. |
| 50 | * |
| 51 | * Notes about pause/resume |
| 52 | * Implementing this would be complicated so it's skipped. The problem case is: |
| 53 | * A full duplex connection is going, then play is paused. At this point you need to start xmitting |
| 54 | * 0's to keep the record active which means you cant just freeze the dma and resume it later you'd |
| 55 | * need to save off the dma info, and restore it properly on a resume. Yeach! |
| 56 | * |
| 57 | * Notes about transfer methods: |
| 58 | * The async write calls fail. I probably need to implement something else to support them? |
| 59 | * |
| 60 | ***************************************************************************************************/ |
| 61 | |
| 62 | #include <linux/config.h> |
| 63 | #include <sound/driver.h> |
| 64 | #include <linux/module.h> |
| 65 | #include <linux/moduleparam.h> |
| 66 | #include <linux/init.h> |
| 67 | #include <linux/errno.h> |
| 68 | #include <linux/ioctl.h> |
| 69 | #include <linux/delay.h> |
| 70 | #include <linux/slab.h> |
| 71 | |
| 72 | #ifdef CONFIG_PM |
| 73 | #include <linux/pm.h> |
| 74 | #endif |
| 75 | |
| 76 | #include <asm/hardware.h> |
| 77 | #include <asm/arch/h3600.h> |
| 78 | #include <asm/mach-types.h> |
| 79 | #include <asm/dma.h> |
| 80 | |
| 81 | #ifdef CONFIG_H3600_HAL |
| 82 | #include <asm/semaphore.h> |
| 83 | #include <asm/uaccess.h> |
| 84 | #include <asm/arch/h3600_hal.h> |
| 85 | #endif |
| 86 | |
| 87 | #include <sound/core.h> |
| 88 | #include <sound/pcm.h> |
| 89 | #include <sound/initval.h> |
| 90 | |
| 91 | #include <linux/l3/l3.h> |
| 92 | |
| 93 | #undef DEBUG_MODE |
| 94 | #undef DEBUG_FUNCTION_NAMES |
| 95 | #include <sound/uda1341.h> |
| 96 | |
| 97 | /* |
| 98 | * FIXME: Is this enough as autodetection of 2.4.X-rmkY-hhZ kernels? |
| 99 | * We use DMA stuff from 2.4.18-rmk3-hh24 here to be able to compile this |
| 100 | * module for Familiar 0.6.1 |
| 101 | */ |
| 102 | #ifdef CONFIG_H3600_HAL |
| 103 | #define HH_VERSION 1 |
| 104 | #endif |
| 105 | |
| 106 | /* {{{ Type definitions */ |
| 107 | |
| 108 | MODULE_AUTHOR("Tomas Kasparek <tomas.kasparek@seznam.cz>"); |
| 109 | MODULE_LICENSE("GPL"); |
| 110 | MODULE_DESCRIPTION("SA1100/SA1111 + UDA1341TS driver for ALSA"); |
| 111 | MODULE_SUPPORTED_DEVICE("{{UDA1341,iPAQ H3600 UDA1341TS}}"); |
| 112 | |
| 113 | static char *id = NULL; /* ID for this card */ |
| 114 | |
| 115 | module_param(id, charp, 0444); |
| 116 | MODULE_PARM_DESC(id, "ID string for SA1100/SA1111 + UDA1341TS soundcard."); |
| 117 | |
| 118 | typedef struct audio_stream { |
| 119 | char *id; /* identification string */ |
| 120 | int stream_id; /* numeric identification */ |
| 121 | dma_device_t dma_dev; /* device identifier for DMA */ |
| 122 | #ifdef HH_VERSION |
| 123 | dmach_t dmach; /* dma channel identification */ |
| 124 | #else |
| 125 | dma_regs_t *dma_regs; /* points to our DMA registers */ |
| 126 | #endif |
| 127 | int active:1; /* we are using this stream for transfer now */ |
| 128 | int period; /* current transfer period */ |
| 129 | int periods; /* current count of periods registerd in the DMA engine */ |
| 130 | int tx_spin; /* are we recoding - flag used to do DMA trans. for sync */ |
| 131 | unsigned int old_offset; |
| 132 | spinlock_t dma_lock; /* for locking in DMA operations (see dma-sa1100.c in the kernel) */ |
| 133 | snd_pcm_substream_t *stream; |
| 134 | }audio_stream_t; |
| 135 | |
| 136 | typedef struct snd_card_sa11xx_uda1341 { |
| 137 | snd_card_t *card; |
| 138 | struct l3_client *uda1341; |
| 139 | snd_pcm_t *pcm; |
| 140 | long samplerate; |
| 141 | audio_stream_t s[2]; /* playback & capture */ |
| 142 | } sa11xx_uda1341_t; |
| 143 | |
| 144 | static struct snd_card_sa11xx_uda1341 *sa11xx_uda1341 = NULL; |
| 145 | |
| 146 | static unsigned int rates[] = { |
| 147 | 8000, 10666, 10985, 14647, |
| 148 | 16000, 21970, 22050, 24000, |
| 149 | 29400, 32000, 44100, 48000, |
| 150 | }; |
| 151 | |
| 152 | static snd_pcm_hw_constraint_list_t hw_constraints_rates = { |
| 153 | .count = ARRAY_SIZE(rates), |
| 154 | .list = rates, |
| 155 | .mask = 0, |
| 156 | }; |
| 157 | |
| 158 | /* }}} */ |
| 159 | |
| 160 | /* {{{ Clock and sample rate stuff */ |
| 161 | |
| 162 | /* |
| 163 | * Stop-gap solution until rest of hh.org HAL stuff is merged. |
| 164 | */ |
| 165 | #define GPIO_H3600_CLK_SET0 GPIO_GPIO (12) |
| 166 | #define GPIO_H3600_CLK_SET1 GPIO_GPIO (13) |
| 167 | |
| 168 | #ifdef CONFIG_SA1100_H3XXX |
| 169 | #define clr_sa11xx_uda1341_egpio(x) clr_h3600_egpio(x) |
| 170 | #define set_sa11xx_uda1341_egpio(x) set_h3600_egpio(x) |
| 171 | #else |
| 172 | #error This driver could serve H3x00 handhelds only! |
| 173 | #endif |
| 174 | |
| 175 | static void sa11xx_uda1341_set_audio_clock(long val) |
| 176 | { |
| 177 | switch (val) { |
| 178 | case 24000: case 32000: case 48000: /* 00: 12.288 MHz */ |
| 179 | GPCR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1; |
| 180 | break; |
| 181 | |
| 182 | case 22050: case 29400: case 44100: /* 01: 11.2896 MHz */ |
| 183 | GPSR = GPIO_H3600_CLK_SET0; |
| 184 | GPCR = GPIO_H3600_CLK_SET1; |
| 185 | break; |
| 186 | |
| 187 | case 8000: case 10666: case 16000: /* 10: 4.096 MHz */ |
| 188 | GPCR = GPIO_H3600_CLK_SET0; |
| 189 | GPSR = GPIO_H3600_CLK_SET1; |
| 190 | break; |
| 191 | |
| 192 | case 10985: case 14647: case 21970: /* 11: 5.6245 MHz */ |
| 193 | GPSR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static void sa11xx_uda1341_set_samplerate(sa11xx_uda1341_t *sa11xx_uda1341, long rate) |
| 199 | { |
| 200 | int clk_div = 0; |
| 201 | int clk=0; |
| 202 | |
| 203 | /* We don't want to mess with clocks when frames are in flight */ |
| 204 | Ser4SSCR0 &= ~SSCR0_SSE; |
| 205 | /* wait for any frame to complete */ |
| 206 | udelay(125); |
| 207 | |
| 208 | /* |
| 209 | * We have the following clock sources: |
| 210 | * 4.096 MHz, 5.6245 MHz, 11.2896 MHz, 12.288 MHz |
| 211 | * Those can be divided either by 256, 384 or 512. |
| 212 | * This makes up 12 combinations for the following samplerates... |
| 213 | */ |
| 214 | if (rate >= 48000) |
| 215 | rate = 48000; |
| 216 | else if (rate >= 44100) |
| 217 | rate = 44100; |
| 218 | else if (rate >= 32000) |
| 219 | rate = 32000; |
| 220 | else if (rate >= 29400) |
| 221 | rate = 29400; |
| 222 | else if (rate >= 24000) |
| 223 | rate = 24000; |
| 224 | else if (rate >= 22050) |
| 225 | rate = 22050; |
| 226 | else if (rate >= 21970) |
| 227 | rate = 21970; |
| 228 | else if (rate >= 16000) |
| 229 | rate = 16000; |
| 230 | else if (rate >= 14647) |
| 231 | rate = 14647; |
| 232 | else if (rate >= 10985) |
| 233 | rate = 10985; |
| 234 | else if (rate >= 10666) |
| 235 | rate = 10666; |
| 236 | else |
| 237 | rate = 8000; |
| 238 | |
| 239 | /* Set the external clock generator */ |
| 240 | #ifdef CONFIG_H3600_HAL |
| 241 | h3600_audio_clock(rate); |
| 242 | #else |
| 243 | sa11xx_uda1341_set_audio_clock(rate); |
| 244 | #endif |
| 245 | |
| 246 | /* Select the clock divisor */ |
| 247 | switch (rate) { |
| 248 | case 8000: |
| 249 | case 10985: |
| 250 | case 22050: |
| 251 | case 24000: |
| 252 | clk = F512; |
| 253 | clk_div = SSCR0_SerClkDiv(16); |
| 254 | break; |
| 255 | case 16000: |
| 256 | case 21970: |
| 257 | case 44100: |
| 258 | case 48000: |
| 259 | clk = F256; |
| 260 | clk_div = SSCR0_SerClkDiv(8); |
| 261 | break; |
| 262 | case 10666: |
| 263 | case 14647: |
| 264 | case 29400: |
| 265 | case 32000: |
| 266 | clk = F384; |
| 267 | clk_div = SSCR0_SerClkDiv(12); |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | /* FMT setting should be moved away when other FMTs are added (FIXME) */ |
| 272 | l3_command(sa11xx_uda1341->uda1341, CMD_FORMAT, (void *)LSB16); |
| 273 | |
| 274 | l3_command(sa11xx_uda1341->uda1341, CMD_FS, (void *)clk); |
| 275 | Ser4SSCR0 = (Ser4SSCR0 & ~0xff00) + clk_div + SSCR0_SSE; |
| 276 | sa11xx_uda1341->samplerate = rate; |
| 277 | } |
| 278 | |
| 279 | /* }}} */ |
| 280 | |
| 281 | /* {{{ HW init and shutdown */ |
| 282 | |
| 283 | static void sa11xx_uda1341_audio_init(sa11xx_uda1341_t *sa11xx_uda1341) |
| 284 | { |
| 285 | unsigned long flags; |
| 286 | |
| 287 | /* Setup DMA stuff */ |
| 288 | sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].id = "UDA1341 out"; |
| 289 | sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].stream_id = SNDRV_PCM_STREAM_PLAYBACK; |
| 290 | sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].dma_dev = DMA_Ser4SSPWr; |
| 291 | |
| 292 | sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].id = "UDA1341 in"; |
| 293 | sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].stream_id = SNDRV_PCM_STREAM_CAPTURE; |
| 294 | sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].dma_dev = DMA_Ser4SSPRd; |
| 295 | |
| 296 | /* Initialize the UDA1341 internal state */ |
| 297 | |
| 298 | /* Setup the uarts */ |
| 299 | local_irq_save(flags); |
| 300 | GAFR |= (GPIO_SSP_CLK); |
| 301 | GPDR &= ~(GPIO_SSP_CLK); |
| 302 | Ser4SSCR0 = 0; |
| 303 | Ser4SSCR0 = SSCR0_DataSize(16) + SSCR0_TI + SSCR0_SerClkDiv(8); |
| 304 | Ser4SSCR1 = SSCR1_SClkIactL + SSCR1_SClk1P + SSCR1_ExtClk; |
| 305 | Ser4SSCR0 |= SSCR0_SSE; |
| 306 | local_irq_restore(flags); |
| 307 | |
| 308 | /* Enable the audio power */ |
| 309 | #ifdef CONFIG_H3600_HAL |
| 310 | h3600_audio_power(AUDIO_RATE_DEFAULT); |
| 311 | #else |
| 312 | clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET); |
| 313 | set_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON); |
| 314 | set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE); |
| 315 | #endif |
| 316 | |
| 317 | /* Wait for the UDA1341 to wake up */ |
| 318 | mdelay(1); //FIXME - was removed by Perex - Why? |
| 319 | |
| 320 | /* Initialize the UDA1341 internal state */ |
| 321 | l3_open(sa11xx_uda1341->uda1341); |
| 322 | |
| 323 | /* external clock configuration (after l3_open - regs must be initialized */ |
| 324 | sa11xx_uda1341_set_samplerate(sa11xx_uda1341, sa11xx_uda1341->samplerate); |
| 325 | |
| 326 | /* Wait for the UDA1341 to wake up */ |
| 327 | set_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET); |
| 328 | mdelay(1); |
| 329 | |
| 330 | /* make the left and right channels unswapped (flip the WS latch) */ |
| 331 | Ser4SSDR = 0; |
| 332 | |
| 333 | #ifdef CONFIG_H3600_HAL |
| 334 | h3600_audio_mute(0); |
| 335 | #else |
| 336 | clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE); |
| 337 | #endif |
| 338 | } |
| 339 | |
| 340 | static void sa11xx_uda1341_audio_shutdown(sa11xx_uda1341_t *sa11xx_uda1341) |
| 341 | { |
| 342 | /* mute on */ |
| 343 | #ifdef CONFIG_H3600_HAL |
| 344 | h3600_audio_mute(1); |
| 345 | #else |
| 346 | set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE); |
| 347 | #endif |
| 348 | |
| 349 | /* disable the audio power and all signals leading to the audio chip */ |
| 350 | l3_close(sa11xx_uda1341->uda1341); |
| 351 | Ser4SSCR0 = 0; |
| 352 | clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET); |
| 353 | |
| 354 | /* power off and mute off */ |
| 355 | /* FIXME - is muting off necesary??? */ |
| 356 | #ifdef CONFIG_H3600_HAL |
| 357 | h3600_audio_power(0); |
| 358 | h3600_audio_mute(0); |
| 359 | #else |
| 360 | clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON); |
| 361 | clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE); |
| 362 | #endif |
| 363 | } |
| 364 | |
| 365 | /* }}} */ |
| 366 | |
| 367 | /* {{{ DMA staff */ |
| 368 | |
| 369 | /* |
| 370 | * these are the address and sizes used to fill the xmit buffer |
| 371 | * so we can get a clock in record only mode |
| 372 | */ |
| 373 | #define FORCE_CLOCK_ADDR (dma_addr_t)FLUSH_BASE_PHYS |
| 374 | #define FORCE_CLOCK_SIZE 4096 // was 2048 |
| 375 | |
| 376 | // FIXME Why this value exactly - wrote comment |
| 377 | #define DMA_BUF_SIZE 8176 /* <= MAX_DMA_SIZE from asm/arch-sa1100/dma.h */ |
| 378 | |
| 379 | #ifdef HH_VERSION |
| 380 | |
| 381 | static int audio_dma_request(audio_stream_t *s, void (*callback)(void *, int)) |
| 382 | { |
| 383 | int ret; |
| 384 | |
| 385 | ret = sa1100_request_dma(&s->dmach, s->id, s->dma_dev); |
| 386 | if (ret < 0) { |
| 387 | printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev); |
| 388 | return ret; |
| 389 | } |
| 390 | sa1100_dma_set_callback(s->dmach, callback); |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static inline void audio_dma_free(audio_stream_t *s) |
| 395 | { |
| 396 | sa1100_free_dma(s->dmach); |
| 397 | s->dmach = -1; |
| 398 | } |
| 399 | |
| 400 | #else |
| 401 | |
| 402 | static int audio_dma_request(audio_stream_t *s, void (*callback)(void *)) |
| 403 | { |
| 404 | int ret; |
| 405 | |
| 406 | ret = sa1100_request_dma(s->dma_dev, s->id, callback, s, &s->dma_regs); |
| 407 | if (ret < 0) |
| 408 | printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev); |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | static void audio_dma_free(audio_stream_t *s) |
| 413 | { |
| 414 | sa1100_free_dma((s)->dma_regs); |
| 415 | (s)->dma_regs = 0; |
| 416 | } |
| 417 | |
| 418 | #endif |
| 419 | |
| 420 | static u_int audio_get_dma_pos(audio_stream_t *s) |
| 421 | { |
| 422 | snd_pcm_substream_t * substream = s->stream; |
| 423 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 424 | unsigned int offset; |
| 425 | unsigned long flags; |
| 426 | dma_addr_t addr; |
| 427 | |
| 428 | // this must be called w/ interrupts locked out see dma-sa1100.c in the kernel |
| 429 | spin_lock_irqsave(&s->dma_lock, flags); |
| 430 | #ifdef HH_VERSION |
| 431 | sa1100_dma_get_current(s->dmach, NULL, &addr); |
| 432 | #else |
| 433 | addr = sa1100_get_dma_pos((s)->dma_regs); |
| 434 | #endif |
| 435 | offset = addr - runtime->dma_addr; |
| 436 | spin_unlock_irqrestore(&s->dma_lock, flags); |
| 437 | |
| 438 | offset = bytes_to_frames(runtime,offset); |
| 439 | if (offset >= runtime->buffer_size) |
| 440 | offset = 0; |
| 441 | |
| 442 | return offset; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * this stops the dma and clears the dma ptrs |
| 447 | */ |
| 448 | static void audio_stop_dma(audio_stream_t *s) |
| 449 | { |
| 450 | unsigned long flags; |
| 451 | |
| 452 | spin_lock_irqsave(&s->dma_lock, flags); |
| 453 | s->active = 0; |
| 454 | s->period = 0; |
| 455 | /* this stops the dma channel and clears the buffer ptrs */ |
| 456 | #ifdef HH_VERSION |
| 457 | sa1100_dma_flush_all(s->dmach); |
| 458 | #else |
| 459 | sa1100_clear_dma(s->dma_regs); |
| 460 | #endif |
| 461 | spin_unlock_irqrestore(&s->dma_lock, flags); |
| 462 | } |
| 463 | |
| 464 | static void audio_process_dma(audio_stream_t *s) |
| 465 | { |
| 466 | snd_pcm_substream_t *substream = s->stream; |
| 467 | snd_pcm_runtime_t *runtime; |
| 468 | unsigned int dma_size; |
| 469 | unsigned int offset; |
| 470 | int ret; |
| 471 | |
| 472 | /* we are requested to process synchronization DMA transfer */ |
| 473 | if (s->tx_spin) { |
| 474 | snd_assert(s->stream_id == SNDRV_PCM_STREAM_PLAYBACK, return); |
| 475 | /* fill the xmit dma buffers and return */ |
| 476 | #ifdef HH_VERSION |
| 477 | sa1100_dma_set_spin(s->dmach, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE); |
| 478 | #else |
| 479 | while (1) { |
| 480 | ret = sa1100_start_dma(s->dma_regs, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE); |
| 481 | if (ret) |
| 482 | return; |
| 483 | } |
| 484 | #endif |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | /* must be set here - only valid for running streams, not for forced_clock dma fills */ |
| 489 | runtime = substream->runtime; |
| 490 | while (s->active && s->periods < runtime->periods) { |
| 491 | dma_size = frames_to_bytes(runtime, runtime->period_size); |
| 492 | if (s->old_offset) { |
| 493 | /* a little trick, we need resume from old position */ |
| 494 | offset = frames_to_bytes(runtime, s->old_offset - 1); |
| 495 | s->old_offset = 0; |
| 496 | s->periods = 0; |
| 497 | s->period = offset / dma_size; |
| 498 | offset %= dma_size; |
| 499 | dma_size = dma_size - offset; |
| 500 | if (!dma_size) |
| 501 | continue; /* special case */ |
| 502 | } else { |
| 503 | offset = dma_size * s->period; |
| 504 | snd_assert(dma_size <= DMA_BUF_SIZE, ); |
| 505 | } |
| 506 | #ifdef HH_VERSION |
| 507 | ret = sa1100_dma_queue_buffer(s->dmach, s, runtime->dma_addr + offset, dma_size); |
| 508 | if (ret) |
| 509 | return; //FIXME |
| 510 | #else |
| 511 | ret = sa1100_start_dma((s)->dma_regs, runtime->dma_addr + offset, dma_size); |
| 512 | if (ret) { |
| 513 | printk(KERN_ERR "audio_process_dma: cannot queue DMA buffer (%i)\n", ret); |
| 514 | return; |
| 515 | } |
| 516 | #endif |
| 517 | |
| 518 | s->period++; |
| 519 | s->period %= runtime->periods; |
| 520 | s->periods++; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | #ifdef HH_VERSION |
| 525 | static void audio_dma_callback(void *data, int size) |
| 526 | #else |
| 527 | static void audio_dma_callback(void *data) |
| 528 | #endif |
| 529 | { |
| 530 | audio_stream_t *s = data; |
| 531 | |
| 532 | /* |
| 533 | * If we are getting a callback for an active stream then we inform |
| 534 | * the PCM middle layer we've finished a period |
| 535 | */ |
| 536 | if (s->active) |
| 537 | snd_pcm_period_elapsed(s->stream); |
| 538 | |
| 539 | spin_lock(&s->dma_lock); |
| 540 | if (!s->tx_spin && s->periods > 0) |
| 541 | s->periods--; |
| 542 | audio_process_dma(s); |
| 543 | spin_unlock(&s->dma_lock); |
| 544 | } |
| 545 | |
| 546 | /* }}} */ |
| 547 | |
| 548 | /* {{{ PCM setting */ |
| 549 | |
| 550 | /* {{{ trigger & timer */ |
| 551 | |
| 552 | static int snd_sa11xx_uda1341_trigger(snd_pcm_substream_t * substream, int cmd) |
| 553 | { |
| 554 | sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream); |
| 555 | int stream_id = substream->pstr->stream; |
| 556 | audio_stream_t *s = &chip->s[stream_id]; |
| 557 | audio_stream_t *s1 = &chip->s[stream_id ^ 1]; |
| 558 | int err = 0; |
| 559 | |
| 560 | /* note local interrupts are already disabled in the midlevel code */ |
| 561 | spin_lock(&s->dma_lock); |
| 562 | switch (cmd) { |
| 563 | case SNDRV_PCM_TRIGGER_START: |
| 564 | /* now we need to make sure a record only stream has a clock */ |
| 565 | if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) { |
| 566 | /* we need to force fill the xmit DMA with zeros */ |
| 567 | s1->tx_spin = 1; |
| 568 | audio_process_dma(s1); |
| 569 | } |
| 570 | /* this case is when you were recording then you turn on a |
| 571 | * playback stream so we stop (also clears it) the dma first, |
| 572 | * clear the sync flag and then we let it turned on |
| 573 | */ |
| 574 | else { |
| 575 | s->tx_spin = 0; |
| 576 | } |
| 577 | |
| 578 | /* requested stream startup */ |
| 579 | s->active = 1; |
| 580 | audio_process_dma(s); |
| 581 | break; |
| 582 | case SNDRV_PCM_TRIGGER_STOP: |
| 583 | /* requested stream shutdown */ |
| 584 | audio_stop_dma(s); |
| 585 | |
| 586 | /* |
| 587 | * now we need to make sure a record only stream has a clock |
| 588 | * so if we're stopping a playback with an active capture |
| 589 | * we need to turn the 0 fill dma on for the xmit side |
| 590 | */ |
| 591 | if (stream_id == SNDRV_PCM_STREAM_PLAYBACK && s1->active) { |
| 592 | /* we need to force fill the xmit DMA with zeros */ |
| 593 | s->tx_spin = 1; |
| 594 | audio_process_dma(s); |
| 595 | } |
| 596 | /* |
| 597 | * we killed a capture only stream, so we should also kill |
| 598 | * the zero fill transmit |
| 599 | */ |
| 600 | else { |
| 601 | if (s1->tx_spin) { |
| 602 | s1->tx_spin = 0; |
| 603 | audio_stop_dma(s1); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | break; |
| 608 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 609 | s->active = 0; |
| 610 | #ifdef HH_VERSION |
| 611 | sa1100_dma_stop(s->dmach); |
| 612 | #else |
| 613 | //FIXME - DMA API |
| 614 | #endif |
| 615 | s->old_offset = audio_get_dma_pos(s) + 1; |
| 616 | #ifdef HH_VERSION |
| 617 | sa1100_dma_flush_all(s->dmach); |
| 618 | #else |
| 619 | //FIXME - DMA API |
| 620 | #endif |
| 621 | s->periods = 0; |
| 622 | break; |
| 623 | case SNDRV_PCM_TRIGGER_RESUME: |
| 624 | s->active = 1; |
| 625 | s->tx_spin = 0; |
| 626 | audio_process_dma(s); |
| 627 | if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) { |
| 628 | s1->tx_spin = 1; |
| 629 | audio_process_dma(s1); |
| 630 | } |
| 631 | break; |
| 632 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 633 | #ifdef HH_VERSION |
| 634 | sa1100_dma_stop(s->dmach); |
| 635 | #else |
| 636 | //FIXME - DMA API |
| 637 | #endif |
| 638 | s->active = 0; |
| 639 | if (stream_id == SNDRV_PCM_STREAM_PLAYBACK) { |
| 640 | if (s1->active) { |
| 641 | s->tx_spin = 1; |
| 642 | s->old_offset = audio_get_dma_pos(s) + 1; |
| 643 | #ifdef HH_VERSION |
| 644 | sa1100_dma_flush_all(s->dmach); |
| 645 | #else |
| 646 | //FIXME - DMA API |
| 647 | #endif |
| 648 | audio_process_dma(s); |
| 649 | } |
| 650 | } else { |
| 651 | if (s1->tx_spin) { |
| 652 | s1->tx_spin = 0; |
| 653 | #ifdef HH_VERSION |
| 654 | sa1100_dma_flush_all(s1->dmach); |
| 655 | #else |
| 656 | //FIXME - DMA API |
| 657 | #endif |
| 658 | } |
| 659 | } |
| 660 | break; |
| 661 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 662 | s->active = 1; |
| 663 | if (s->old_offset) { |
| 664 | s->tx_spin = 0; |
| 665 | audio_process_dma(s); |
| 666 | break; |
| 667 | } |
| 668 | if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) { |
| 669 | s1->tx_spin = 1; |
| 670 | audio_process_dma(s1); |
| 671 | } |
| 672 | #ifdef HH_VERSION |
| 673 | sa1100_dma_resume(s->dmach); |
| 674 | #else |
| 675 | //FIXME - DMA API |
| 676 | #endif |
| 677 | break; |
| 678 | default: |
| 679 | err = -EINVAL; |
| 680 | break; |
| 681 | } |
| 682 | spin_unlock(&s->dma_lock); |
| 683 | return err; |
| 684 | } |
| 685 | |
| 686 | static int snd_sa11xx_uda1341_prepare(snd_pcm_substream_t * substream) |
| 687 | { |
| 688 | sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream); |
| 689 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 690 | audio_stream_t *s = &chip->s[substream->pstr->stream]; |
| 691 | |
| 692 | /* set requested samplerate */ |
| 693 | sa11xx_uda1341_set_samplerate(chip, runtime->rate); |
| 694 | |
| 695 | /* set requestd format when available */ |
| 696 | /* set FMT here !!! FIXME */ |
| 697 | |
| 698 | s->period = 0; |
| 699 | s->periods = 0; |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static snd_pcm_uframes_t snd_sa11xx_uda1341_pointer(snd_pcm_substream_t * substream) |
| 705 | { |
| 706 | sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream); |
| 707 | return audio_get_dma_pos(&chip->s[substream->pstr->stream]); |
| 708 | } |
| 709 | |
| 710 | /* }}} */ |
| 711 | |
| 712 | static snd_pcm_hardware_t snd_sa11xx_uda1341_capture = |
| 713 | { |
| 714 | .info = (SNDRV_PCM_INFO_INTERLEAVED | |
| 715 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 716 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | |
| 717 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME), |
| 718 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 719 | .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\ |
| 720 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\ |
| 721 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\ |
| 722 | SNDRV_PCM_RATE_KNOT), |
| 723 | .rate_min = 8000, |
| 724 | .rate_max = 48000, |
| 725 | .channels_min = 2, |
| 726 | .channels_max = 2, |
| 727 | .buffer_bytes_max = 64*1024, |
| 728 | .period_bytes_min = 64, |
| 729 | .period_bytes_max = DMA_BUF_SIZE, |
| 730 | .periods_min = 2, |
| 731 | .periods_max = 255, |
| 732 | .fifo_size = 0, |
| 733 | }; |
| 734 | |
| 735 | static snd_pcm_hardware_t snd_sa11xx_uda1341_playback = |
| 736 | { |
| 737 | .info = (SNDRV_PCM_INFO_INTERLEAVED | |
| 738 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 739 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | |
| 740 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME), |
| 741 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 742 | .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\ |
| 743 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\ |
| 744 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\ |
| 745 | SNDRV_PCM_RATE_KNOT), |
| 746 | .rate_min = 8000, |
| 747 | .rate_max = 48000, |
| 748 | .channels_min = 2, |
| 749 | .channels_max = 2, |
| 750 | .buffer_bytes_max = 64*1024, |
| 751 | .period_bytes_min = 64, |
| 752 | .period_bytes_max = DMA_BUF_SIZE, |
| 753 | .periods_min = 2, |
| 754 | .periods_max = 255, |
| 755 | .fifo_size = 0, |
| 756 | }; |
| 757 | |
| 758 | static int snd_card_sa11xx_uda1341_open(snd_pcm_substream_t * substream) |
| 759 | { |
| 760 | sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream); |
| 761 | snd_pcm_runtime_t *runtime = substream->runtime; |
| 762 | int stream_id = substream->pstr->stream; |
| 763 | int err; |
| 764 | |
| 765 | chip->s[stream_id].stream = substream; |
| 766 | |
| 767 | if (stream_id == SNDRV_PCM_STREAM_PLAYBACK) |
| 768 | runtime->hw = snd_sa11xx_uda1341_playback; |
| 769 | else |
| 770 | runtime->hw = snd_sa11xx_uda1341_capture; |
| 771 | if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) |
| 772 | return err; |
| 773 | if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates)) < 0) |
| 774 | return err; |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static int snd_card_sa11xx_uda1341_close(snd_pcm_substream_t * substream) |
| 780 | { |
| 781 | sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream); |
| 782 | |
| 783 | chip->s[substream->pstr->stream].stream = NULL; |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | /* {{{ HW params & free */ |
| 788 | |
| 789 | static int snd_sa11xx_uda1341_hw_params(snd_pcm_substream_t * substream, |
| 790 | snd_pcm_hw_params_t * hw_params) |
| 791 | { |
| 792 | |
| 793 | return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); |
| 794 | } |
| 795 | |
| 796 | static int snd_sa11xx_uda1341_hw_free(snd_pcm_substream_t * substream) |
| 797 | { |
| 798 | return snd_pcm_lib_free_pages(substream); |
| 799 | } |
| 800 | |
| 801 | /* }}} */ |
| 802 | |
| 803 | static snd_pcm_ops_t snd_card_sa11xx_uda1341_playback_ops = { |
| 804 | .open = snd_card_sa11xx_uda1341_open, |
| 805 | .close = snd_card_sa11xx_uda1341_close, |
| 806 | .ioctl = snd_pcm_lib_ioctl, |
| 807 | .hw_params = snd_sa11xx_uda1341_hw_params, |
| 808 | .hw_free = snd_sa11xx_uda1341_hw_free, |
| 809 | .prepare = snd_sa11xx_uda1341_prepare, |
| 810 | .trigger = snd_sa11xx_uda1341_trigger, |
| 811 | .pointer = snd_sa11xx_uda1341_pointer, |
| 812 | }; |
| 813 | |
| 814 | static snd_pcm_ops_t snd_card_sa11xx_uda1341_capture_ops = { |
| 815 | .open = snd_card_sa11xx_uda1341_open, |
| 816 | .close = snd_card_sa11xx_uda1341_close, |
| 817 | .ioctl = snd_pcm_lib_ioctl, |
| 818 | .hw_params = snd_sa11xx_uda1341_hw_params, |
| 819 | .hw_free = snd_sa11xx_uda1341_hw_free, |
| 820 | .prepare = snd_sa11xx_uda1341_prepare, |
| 821 | .trigger = snd_sa11xx_uda1341_trigger, |
| 822 | .pointer = snd_sa11xx_uda1341_pointer, |
| 823 | }; |
| 824 | |
| 825 | static int __init snd_card_sa11xx_uda1341_pcm(sa11xx_uda1341_t *sa11xx_uda1341, int device) |
| 826 | { |
| 827 | snd_pcm_t *pcm; |
| 828 | int err; |
| 829 | |
| 830 | if ((err = snd_pcm_new(sa11xx_uda1341->card, "UDA1341 PCM", device, 1, 1, &pcm)) < 0) |
| 831 | return err; |
| 832 | |
| 833 | /* |
| 834 | * this sets up our initial buffers and sets the dma_type to isa. |
| 835 | * isa works but I'm not sure why (or if) it's the right choice |
| 836 | * this may be too large, trying it for now |
| 837 | */ |
| 838 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_ISA, |
| 839 | snd_pcm_dma_flags(0), |
| 840 | 64*1024, 64*1024); |
| 841 | |
| 842 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_sa11xx_uda1341_playback_ops); |
| 843 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_sa11xx_uda1341_capture_ops); |
| 844 | pcm->private_data = sa11xx_uda1341; |
| 845 | pcm->info_flags = 0; |
| 846 | strcpy(pcm->name, "UDA1341 PCM"); |
| 847 | |
| 848 | sa11xx_uda1341_audio_init(sa11xx_uda1341); |
| 849 | |
| 850 | /* setup DMA controller */ |
| 851 | audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK], audio_dma_callback); |
| 852 | audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE], audio_dma_callback); |
| 853 | |
| 854 | sa11xx_uda1341->pcm = pcm; |
| 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | /* }}} */ |
| 860 | |
| 861 | /* {{{ module init & exit */ |
| 862 | |
| 863 | #ifdef CONFIG_PM |
| 864 | |
| 865 | static int snd_sa11xx_uda1341_suspend(snd_card_t *card, pm_message_t state) |
| 866 | { |
| 867 | sa11xx_uda1341_t *chip = card->pm_private_data; |
| 868 | |
| 869 | snd_pcm_suspend_all(chip->pcm); |
| 870 | #ifdef HH_VERSION |
| 871 | sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach); |
| 872 | sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach); |
| 873 | #else |
| 874 | //FIXME |
| 875 | #endif |
| 876 | l3_command(chip->uda1341, CMD_SUSPEND, NULL); |
| 877 | sa11xx_uda1341_audio_shutdown(chip); |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | static int snd_sa11xx_uda1341_resume(snd_card_t *card) |
| 882 | { |
| 883 | sa11xx_uda1341_t *chip = card->pm_private_data; |
| 884 | |
| 885 | sa11xx_uda1341_audio_init(chip); |
| 886 | l3_command(chip->uda1341, CMD_RESUME, NULL); |
| 887 | #ifdef HH_VERSION |
| 888 | sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach); |
| 889 | sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach); |
| 890 | #else |
| 891 | //FIXME |
| 892 | #endif |
| 893 | return 0; |
| 894 | } |
| 895 | #endif /* COMFIG_PM */ |
| 896 | |
| 897 | void snd_sa11xx_uda1341_free(snd_card_t *card) |
| 898 | { |
| 899 | sa11xx_uda1341_t *chip = card->private_data; |
| 900 | |
| 901 | audio_dma_free(&chip->s[SNDRV_PCM_STREAM_PLAYBACK]); |
| 902 | audio_dma_free(&chip->s[SNDRV_PCM_STREAM_CAPTURE]); |
| 903 | sa11xx_uda1341 = NULL; |
| 904 | card->private_data = NULL; |
| 905 | kfree(chip); |
| 906 | } |
| 907 | |
| 908 | static int __init sa11xx_uda1341_init(void) |
| 909 | { |
| 910 | int err; |
| 911 | snd_card_t *card; |
| 912 | |
| 913 | if (!machine_is_h3xxx()) |
| 914 | return -ENODEV; |
| 915 | |
| 916 | /* register the soundcard */ |
| 917 | card = snd_card_new(-1, id, THIS_MODULE, sizeof(sa11xx_uda1341_t)); |
| 918 | if (card == NULL) |
| 919 | return -ENOMEM; |
| 920 | |
| 921 | sa11xx_uda1341 = kcalloc(1, sizeof(*sa11xx_uda1341), GFP_KERNEL); |
| 922 | if (sa11xx_uda1341 == NULL) |
| 923 | return -ENOMEM; |
| 924 | spin_lock_init(&chip->s[0].dma_lock); |
| 925 | spin_lock_init(&chip->s[1].dma_lock); |
| 926 | |
| 927 | card->private_data = (void *)sa11xx_uda1341; |
| 928 | card->private_free = snd_sa11xx_uda1341_free; |
| 929 | |
| 930 | sa11xx_uda1341->card = card; |
| 931 | sa11xx_uda1341->samplerate = AUDIO_RATE_DEFAULT; |
| 932 | |
| 933 | // mixer |
| 934 | if ((err = snd_chip_uda1341_mixer_new(sa11xx_uda1341->card, &sa11xx_uda1341->uda1341))) |
| 935 | goto nodev; |
| 936 | |
| 937 | // PCM |
| 938 | if ((err = snd_card_sa11xx_uda1341_pcm(sa11xx_uda1341, 0)) < 0) |
| 939 | goto nodev; |
| 940 | |
| 941 | snd_card_set_generic_pm_callback(card, |
| 942 | snd_sa11xx_uda1341_suspend, snd_sa11_uda1341_resume, |
| 943 | sa11xx_uda1341); |
| 944 | |
| 945 | strcpy(card->driver, "UDA1341"); |
| 946 | strcpy(card->shortname, "H3600 UDA1341TS"); |
| 947 | sprintf(card->longname, "Compaq iPAQ H3600 with Philips UDA1341TS"); |
| 948 | |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame^] | 949 | if ((err = snd_card_set_generic_dev(card)) < 0) |
| 950 | goto nodev; |
| 951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | if ((err = snd_card_register(card)) == 0) { |
| 953 | printk( KERN_INFO "iPAQ audio support initialized\n" ); |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | nodev: |
| 958 | snd_card_free(card); |
| 959 | return err; |
| 960 | } |
| 961 | |
| 962 | static void __exit sa11xx_uda1341_exit(void) |
| 963 | { |
| 964 | snd_card_free(sa11xx_uda1341->card); |
| 965 | } |
| 966 | |
| 967 | module_init(sa11xx_uda1341_init); |
| 968 | module_exit(sa11xx_uda1341_exit); |
| 969 | |
| 970 | /* }}} */ |
| 971 | |
| 972 | /* |
| 973 | * Local variables: |
| 974 | * indent-tabs-mode: t |
| 975 | * End: |
| 976 | */ |