Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Audio support for PS3 |
| 3 | * Copyright (C) 2007 Sony Computer Entertainment Inc. |
| 4 | * All rights reserved. |
| 5 | * Copyright 2006, 2007 Sony Corporation |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; version 2 of the Licence. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/interrupt.h> |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 25 | #include <sound/core.h> |
| 26 | #include <sound/initval.h> |
| 27 | #include <sound/pcm.h> |
| 28 | #include <sound/asound.h> |
| 29 | #include <sound/memalloc.h> |
| 30 | #include <sound/pcm_params.h> |
| 31 | #include <sound/control.h> |
| 32 | #include <linux/dmapool.h> |
| 33 | #include <linux/dma-mapping.h> |
| 34 | #include <asm/firmware.h> |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 35 | #include <asm/dma.h> |
| 36 | #include <asm/lv1call.h> |
| 37 | #include <asm/ps3.h> |
| 38 | #include <asm/ps3av.h> |
| 39 | |
| 40 | #include "snd_ps3_reg.h" |
| 41 | #include "snd_ps3.h" |
| 42 | |
| 43 | MODULE_LICENSE("GPL v2"); |
| 44 | MODULE_DESCRIPTION("PS3 sound driver"); |
| 45 | MODULE_AUTHOR("Sony Computer Entertainment Inc."); |
| 46 | |
| 47 | /* module entries */ |
| 48 | static int __init snd_ps3_init(void); |
| 49 | static void __exit snd_ps3_exit(void); |
| 50 | |
| 51 | /* ALSA snd driver ops */ |
| 52 | static int snd_ps3_pcm_open(struct snd_pcm_substream *substream); |
| 53 | static int snd_ps3_pcm_close(struct snd_pcm_substream *substream); |
| 54 | static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream); |
| 55 | static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream, |
| 56 | int cmd); |
| 57 | static snd_pcm_uframes_t snd_ps3_pcm_pointer(struct snd_pcm_substream |
| 58 | *substream); |
| 59 | static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream, |
| 60 | struct snd_pcm_hw_params *hw_params); |
| 61 | static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream); |
| 62 | |
| 63 | |
| 64 | /* ps3_system_bus_driver entries */ |
| 65 | static int __init snd_ps3_driver_probe(struct ps3_system_bus_device *dev); |
| 66 | static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev); |
| 67 | |
| 68 | /* address setup */ |
| 69 | static int snd_ps3_map_mmio(void); |
| 70 | static void snd_ps3_unmap_mmio(void); |
| 71 | static int snd_ps3_allocate_irq(void); |
| 72 | static void snd_ps3_free_irq(void); |
| 73 | static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start); |
| 74 | |
| 75 | /* interrupt handler */ |
| 76 | static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id); |
| 77 | |
| 78 | |
| 79 | /* set sampling rate/format */ |
| 80 | static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream); |
| 81 | /* take effect parameter change */ |
| 82 | static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card); |
| 83 | /* initialize avsetting and take it effect */ |
| 84 | static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card); |
| 85 | /* setup dma */ |
| 86 | static int snd_ps3_program_dma(struct snd_ps3_card_info *card, |
| 87 | enum snd_ps3_dma_filltype filltype); |
| 88 | static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card); |
| 89 | |
| 90 | static dma_addr_t v_to_bus(struct snd_ps3_card_info *, void *vaddr, int ch); |
| 91 | |
| 92 | |
| 93 | module_init(snd_ps3_init); |
| 94 | module_exit(snd_ps3_exit); |
| 95 | |
| 96 | /* |
| 97 | * global |
| 98 | */ |
| 99 | static struct snd_ps3_card_info the_card; |
| 100 | |
| 101 | static int snd_ps3_start_delay = CONFIG_SND_PS3_DEFAULT_START_DELAY; |
| 102 | |
| 103 | module_param_named(start_delay, snd_ps3_start_delay, uint, 0644); |
| 104 | MODULE_PARM_DESC(start_delay, "time to insert silent data in milisec"); |
| 105 | |
| 106 | static int index = SNDRV_DEFAULT_IDX1; |
| 107 | static char *id = SNDRV_DEFAULT_STR1; |
| 108 | |
| 109 | module_param(index, int, 0444); |
| 110 | MODULE_PARM_DESC(index, "Index value for PS3 soundchip."); |
| 111 | module_param(id, charp, 0444); |
| 112 | MODULE_PARM_DESC(id, "ID string for PS3 soundchip."); |
| 113 | |
| 114 | |
| 115 | /* |
| 116 | * PS3 audio register access |
| 117 | */ |
| 118 | static inline u32 read_reg(unsigned int reg) |
| 119 | { |
| 120 | return in_be32(the_card.mapped_mmio_vaddr + reg); |
| 121 | } |
| 122 | static inline void write_reg(unsigned int reg, u32 val) |
| 123 | { |
| 124 | out_be32(the_card.mapped_mmio_vaddr + reg, val); |
| 125 | } |
| 126 | static inline void update_reg(unsigned int reg, u32 or_val) |
| 127 | { |
| 128 | u32 newval = read_reg(reg) | or_val; |
| 129 | write_reg(reg, newval); |
| 130 | } |
| 131 | static inline void update_mask_reg(unsigned int reg, u32 mask, u32 or_val) |
| 132 | { |
| 133 | u32 newval = (read_reg(reg) & mask) | or_val; |
| 134 | write_reg(reg, newval); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * ALSA defs |
| 139 | */ |
Tobias Klauser | 3f76d98 | 2008-04-21 22:25:51 +0000 | [diff] [blame] | 140 | static const struct snd_pcm_hardware snd_ps3_pcm_hw = { |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 141 | .info = (SNDRV_PCM_INFO_MMAP | |
| 142 | SNDRV_PCM_INFO_NONINTERLEAVED | |
| 143 | SNDRV_PCM_INFO_MMAP_VALID), |
| 144 | .formats = (SNDRV_PCM_FMTBIT_S16_BE | |
| 145 | SNDRV_PCM_FMTBIT_S24_BE), |
| 146 | .rates = (SNDRV_PCM_RATE_44100 | |
| 147 | SNDRV_PCM_RATE_48000 | |
| 148 | SNDRV_PCM_RATE_88200 | |
| 149 | SNDRV_PCM_RATE_96000), |
| 150 | .rate_min = 44100, |
| 151 | .rate_max = 96000, |
| 152 | |
| 153 | .channels_min = 2, /* stereo only */ |
| 154 | .channels_max = 2, |
| 155 | |
| 156 | .buffer_bytes_max = PS3_AUDIO_FIFO_SIZE * 64, |
| 157 | |
| 158 | /* interrupt by four stages */ |
| 159 | .period_bytes_min = PS3_AUDIO_FIFO_STAGE_SIZE * 4, |
| 160 | .period_bytes_max = PS3_AUDIO_FIFO_STAGE_SIZE * 4, |
| 161 | |
| 162 | .periods_min = 16, |
| 163 | .periods_max = 32, /* buffer_size_max/ period_bytes_max */ |
| 164 | |
| 165 | .fifo_size = PS3_AUDIO_FIFO_SIZE |
| 166 | }; |
| 167 | |
| 168 | static struct snd_pcm_ops snd_ps3_pcm_spdif_ops = |
| 169 | { |
| 170 | .open = snd_ps3_pcm_open, |
| 171 | .close = snd_ps3_pcm_close, |
| 172 | .prepare = snd_ps3_pcm_prepare, |
| 173 | .ioctl = snd_pcm_lib_ioctl, |
| 174 | .trigger = snd_ps3_pcm_trigger, |
| 175 | .pointer = snd_ps3_pcm_pointer, |
| 176 | .hw_params = snd_ps3_pcm_hw_params, |
| 177 | .hw_free = snd_ps3_pcm_hw_free |
| 178 | }; |
| 179 | |
| 180 | static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info *card, |
| 181 | int count, int force_stop) |
| 182 | { |
| 183 | int dma_ch, done, retries, stop_forced = 0; |
| 184 | uint32_t status; |
| 185 | |
| 186 | for (dma_ch = 0; dma_ch < 8; dma_ch ++) { |
| 187 | retries = count; |
| 188 | do { |
| 189 | status = read_reg(PS3_AUDIO_KICK(dma_ch)) & |
| 190 | PS3_AUDIO_KICK_STATUS_MASK; |
| 191 | switch (status) { |
| 192 | case PS3_AUDIO_KICK_STATUS_DONE: |
| 193 | case PS3_AUDIO_KICK_STATUS_NOTIFY: |
| 194 | case PS3_AUDIO_KICK_STATUS_CLEAR: |
| 195 | case PS3_AUDIO_KICK_STATUS_ERROR: |
| 196 | done = 1; |
| 197 | break; |
| 198 | default: |
| 199 | done = 0; |
| 200 | udelay(10); |
| 201 | } |
| 202 | } while (!done && --retries); |
| 203 | if (!retries && force_stop) { |
| 204 | pr_info("%s: DMA ch %d is not stopped.", |
| 205 | __func__, dma_ch); |
| 206 | /* last resort. force to stop dma. |
| 207 | * NOTE: this cause DMA done interrupts |
| 208 | */ |
| 209 | update_reg(PS3_AUDIO_CONFIG, PS3_AUDIO_CONFIG_CLEAR); |
| 210 | stop_forced = 1; |
| 211 | } |
| 212 | } |
| 213 | return stop_forced; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * wait for all dma is done. |
| 218 | * NOTE: caller should reset card->running before call. |
| 219 | * If not, the interrupt handler will re-start DMA, |
| 220 | * then DMA is never stopped. |
| 221 | */ |
| 222 | static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card) |
| 223 | { |
| 224 | int stop_forced; |
| 225 | /* |
| 226 | * wait for the last dma is done |
| 227 | */ |
| 228 | |
| 229 | /* |
| 230 | * expected maximum DMA done time is 5.7ms + something (DMA itself). |
| 231 | * 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next |
| 232 | * DMA kick event would occur. |
| 233 | */ |
| 234 | stop_forced = snd_ps3_verify_dma_stop(card, 700, 1); |
| 235 | |
| 236 | /* |
| 237 | * clear outstanding interrupts. |
| 238 | */ |
| 239 | update_reg(PS3_AUDIO_INTR_0, 0); |
| 240 | update_reg(PS3_AUDIO_AX_IS, 0); |
| 241 | |
| 242 | /* |
| 243 | *revert CLEAR bit since it will not reset automatically after DMA stop |
| 244 | */ |
| 245 | if (stop_forced) |
| 246 | update_mask_reg(PS3_AUDIO_CONFIG, ~PS3_AUDIO_CONFIG_CLEAR, 0); |
| 247 | /* ensure the hardware sees changes */ |
| 248 | wmb(); |
| 249 | } |
| 250 | |
| 251 | static void snd_ps3_kick_dma(struct snd_ps3_card_info *card) |
| 252 | { |
| 253 | |
| 254 | update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST); |
| 255 | /* ensure the hardware sees the change */ |
| 256 | wmb(); |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * convert virtual addr to ioif bus addr. |
| 261 | */ |
| 262 | static dma_addr_t v_to_bus(struct snd_ps3_card_info *card, |
| 263 | void * paddr, |
| 264 | int ch) |
| 265 | { |
| 266 | return card->dma_start_bus_addr[ch] + |
| 267 | (paddr - card->dma_start_vaddr[ch]); |
| 268 | }; |
| 269 | |
| 270 | |
| 271 | /* |
| 272 | * increment ring buffer pointer. |
| 273 | * NOTE: caller must hold write spinlock |
| 274 | */ |
| 275 | static void snd_ps3_bump_buffer(struct snd_ps3_card_info *card, |
| 276 | enum snd_ps3_ch ch, size_t byte_count, |
| 277 | int stage) |
| 278 | { |
| 279 | if (!stage) |
| 280 | card->dma_last_transfer_vaddr[ch] = |
| 281 | card->dma_next_transfer_vaddr[ch]; |
| 282 | card->dma_next_transfer_vaddr[ch] += byte_count; |
| 283 | if ((card->dma_start_vaddr[ch] + (card->dma_buffer_size / 2)) <= |
| 284 | card->dma_next_transfer_vaddr[ch]) { |
| 285 | card->dma_next_transfer_vaddr[ch] = card->dma_start_vaddr[ch]; |
| 286 | } |
| 287 | } |
| 288 | /* |
| 289 | * setup dmac to send data to audio and attenuate samples on the ring buffer |
| 290 | */ |
| 291 | static int snd_ps3_program_dma(struct snd_ps3_card_info *card, |
| 292 | enum snd_ps3_dma_filltype filltype) |
| 293 | { |
| 294 | /* this dmac does not support over 4G */ |
| 295 | uint32_t dma_addr; |
| 296 | int fill_stages, dma_ch, stage; |
| 297 | enum snd_ps3_ch ch; |
| 298 | uint32_t ch0_kick_event = 0; /* initialize to mute gcc */ |
| 299 | void *start_vaddr; |
| 300 | unsigned long irqsave; |
| 301 | int silent = 0; |
| 302 | |
| 303 | switch (filltype) { |
| 304 | case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL: |
| 305 | silent = 1; |
| 306 | /* intentionally fall thru */ |
| 307 | case SND_PS3_DMA_FILLTYPE_FIRSTFILL: |
| 308 | ch0_kick_event = PS3_AUDIO_KICK_EVENT_ALWAYS; |
| 309 | break; |
| 310 | |
| 311 | case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING: |
| 312 | silent = 1; |
| 313 | /* intentionally fall thru */ |
| 314 | case SND_PS3_DMA_FILLTYPE_RUNNING: |
| 315 | ch0_kick_event = PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY; |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | snd_ps3_verify_dma_stop(card, 700, 0); |
| 320 | fill_stages = 4; |
| 321 | spin_lock_irqsave(&card->dma_lock, irqsave); |
| 322 | for (ch = 0; ch < 2; ch++) { |
| 323 | start_vaddr = card->dma_next_transfer_vaddr[0]; |
| 324 | for (stage = 0; stage < fill_stages; stage ++) { |
| 325 | dma_ch = stage * 2 + ch; |
| 326 | if (silent) |
| 327 | dma_addr = card->null_buffer_start_dma_addr; |
| 328 | else |
| 329 | dma_addr = |
| 330 | v_to_bus(card, |
| 331 | card->dma_next_transfer_vaddr[ch], |
| 332 | ch); |
| 333 | |
| 334 | write_reg(PS3_AUDIO_SOURCE(dma_ch), |
| 335 | (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY | |
| 336 | dma_addr)); |
| 337 | |
| 338 | /* dst: fixed to 3wire#0 */ |
| 339 | if (ch == 0) |
| 340 | write_reg(PS3_AUDIO_DEST(dma_ch), |
| 341 | (PS3_AUDIO_DEST_TARGET_AUDIOFIFO | |
| 342 | PS3_AUDIO_AO_3W_LDATA(0))); |
| 343 | else |
| 344 | write_reg(PS3_AUDIO_DEST(dma_ch), |
| 345 | (PS3_AUDIO_DEST_TARGET_AUDIOFIFO | |
| 346 | PS3_AUDIO_AO_3W_RDATA(0))); |
| 347 | |
| 348 | /* count always 1 DMA block (1/2 stage = 128 bytes) */ |
| 349 | write_reg(PS3_AUDIO_DMASIZE(dma_ch), 0); |
| 350 | /* bump pointer if needed */ |
| 351 | if (!silent) |
| 352 | snd_ps3_bump_buffer(card, ch, |
| 353 | PS3_AUDIO_DMAC_BLOCK_SIZE, |
| 354 | stage); |
| 355 | |
| 356 | /* kick event */ |
| 357 | if (dma_ch == 0) |
| 358 | write_reg(PS3_AUDIO_KICK(dma_ch), |
| 359 | ch0_kick_event); |
| 360 | else |
| 361 | write_reg(PS3_AUDIO_KICK(dma_ch), |
| 362 | PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch |
| 363 | - 1) | |
| 364 | PS3_AUDIO_KICK_REQUEST); |
| 365 | } |
| 366 | } |
| 367 | /* ensure the hardware sees the change */ |
| 368 | wmb(); |
| 369 | spin_unlock_irqrestore(&card->dma_lock, irqsave); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * audio mute on/off |
| 376 | * mute_on : 0 output enabled |
| 377 | * 1 mute |
| 378 | */ |
| 379 | static int snd_ps3_mute(int mute_on) |
| 380 | { |
| 381 | return ps3av_audio_mute(mute_on); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * PCM operators |
| 386 | */ |
| 387 | static int snd_ps3_pcm_open(struct snd_pcm_substream *substream) |
| 388 | { |
| 389 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 390 | struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream); |
| 391 | int pcm_index; |
| 392 | |
| 393 | pcm_index = substream->pcm->device; |
| 394 | /* to retrieve substream/runtime in interrupt handler */ |
| 395 | card->substream = substream; |
| 396 | |
| 397 | runtime->hw = snd_ps3_pcm_hw; |
| 398 | |
| 399 | card->start_delay = snd_ps3_start_delay; |
| 400 | |
| 401 | /* mute off */ |
| 402 | snd_ps3_mute(0); /* this function sleep */ |
| 403 | |
| 404 | snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 405 | PS3_AUDIO_FIFO_STAGE_SIZE * 4 * 2); |
| 406 | return 0; |
| 407 | }; |
| 408 | |
| 409 | static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream, |
| 410 | struct snd_pcm_hw_params *hw_params) |
| 411 | { |
| 412 | size_t size; |
| 413 | |
| 414 | /* alloc transport buffer */ |
| 415 | size = params_buffer_bytes(hw_params); |
| 416 | snd_pcm_lib_malloc_pages(substream, size); |
| 417 | return 0; |
| 418 | }; |
| 419 | |
| 420 | static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream, |
| 421 | unsigned int delay_ms) |
| 422 | { |
| 423 | int ret; |
| 424 | int rate ; |
| 425 | |
| 426 | rate = substream->runtime->rate; |
| 427 | ret = snd_pcm_format_size(substream->runtime->format, |
| 428 | rate * delay_ms / 1000) |
| 429 | * substream->runtime->channels; |
| 430 | |
| 431 | pr_debug(KERN_ERR "%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n", |
| 432 | __func__, |
| 433 | delay_ms, |
| 434 | rate, |
| 435 | snd_pcm_format_size(substream->runtime->format, rate), |
| 436 | rate * delay_ms / 1000, |
| 437 | ret); |
| 438 | |
| 439 | return ret; |
| 440 | }; |
| 441 | |
| 442 | static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream) |
| 443 | { |
| 444 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 445 | struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream); |
| 446 | unsigned long irqsave; |
| 447 | |
| 448 | if (!snd_ps3_set_avsetting(substream)) { |
| 449 | /* some parameter changed */ |
| 450 | write_reg(PS3_AUDIO_AX_IE, |
| 451 | PS3_AUDIO_AX_IE_ASOBEIE(0) | |
| 452 | PS3_AUDIO_AX_IE_ASOBUIE(0)); |
| 453 | /* |
| 454 | * let SPDIF device re-lock with SPDIF signal, |
| 455 | * start with some silence |
| 456 | */ |
| 457 | card->silent = snd_ps3_delay_to_bytes(substream, |
| 458 | card->start_delay) / |
| 459 | (PS3_AUDIO_FIFO_STAGE_SIZE * 4); /* every 4 times */ |
| 460 | } |
| 461 | |
| 462 | /* restart ring buffer pointer */ |
| 463 | spin_lock_irqsave(&card->dma_lock, irqsave); |
| 464 | { |
| 465 | card->dma_buffer_size = runtime->dma_bytes; |
| 466 | |
| 467 | card->dma_last_transfer_vaddr[SND_PS3_CH_L] = |
| 468 | card->dma_next_transfer_vaddr[SND_PS3_CH_L] = |
| 469 | card->dma_start_vaddr[SND_PS3_CH_L] = |
| 470 | runtime->dma_area; |
| 471 | card->dma_start_bus_addr[SND_PS3_CH_L] = runtime->dma_addr; |
| 472 | |
| 473 | card->dma_last_transfer_vaddr[SND_PS3_CH_R] = |
| 474 | card->dma_next_transfer_vaddr[SND_PS3_CH_R] = |
| 475 | card->dma_start_vaddr[SND_PS3_CH_R] = |
| 476 | runtime->dma_area + (runtime->dma_bytes / 2); |
| 477 | card->dma_start_bus_addr[SND_PS3_CH_R] = |
| 478 | runtime->dma_addr + (runtime->dma_bytes / 2); |
| 479 | |
Stephen Rothwell | 26db11a | 2009-01-13 20:07:55 +0000 | [diff] [blame] | 480 | pr_debug("%s: vaddr=%p bus=%#llx\n", __func__, |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 481 | card->dma_start_vaddr[SND_PS3_CH_L], |
| 482 | card->dma_start_bus_addr[SND_PS3_CH_L]); |
| 483 | |
| 484 | } |
| 485 | spin_unlock_irqrestore(&card->dma_lock, irqsave); |
| 486 | |
| 487 | /* ensure the hardware sees the change */ |
| 488 | mb(); |
| 489 | |
| 490 | return 0; |
| 491 | }; |
| 492 | |
| 493 | static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream, |
| 494 | int cmd) |
| 495 | { |
| 496 | struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream); |
| 497 | int ret = 0; |
| 498 | |
| 499 | switch (cmd) { |
| 500 | case SNDRV_PCM_TRIGGER_START: |
| 501 | /* clear outstanding interrupts */ |
| 502 | update_reg(PS3_AUDIO_AX_IS, 0); |
| 503 | |
| 504 | spin_lock(&card->dma_lock); |
| 505 | { |
| 506 | card->running = 1; |
| 507 | } |
| 508 | spin_unlock(&card->dma_lock); |
| 509 | |
| 510 | snd_ps3_program_dma(card, |
| 511 | SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL); |
| 512 | snd_ps3_kick_dma(card); |
| 513 | while (read_reg(PS3_AUDIO_KICK(7)) & |
| 514 | PS3_AUDIO_KICK_STATUS_MASK) { |
| 515 | udelay(1); |
| 516 | } |
| 517 | snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING); |
| 518 | snd_ps3_kick_dma(card); |
| 519 | break; |
| 520 | |
| 521 | case SNDRV_PCM_TRIGGER_STOP: |
| 522 | spin_lock(&card->dma_lock); |
| 523 | { |
| 524 | card->running = 0; |
| 525 | } |
| 526 | spin_unlock(&card->dma_lock); |
| 527 | snd_ps3_wait_for_dma_stop(card); |
| 528 | break; |
| 529 | default: |
| 530 | break; |
| 531 | |
| 532 | } |
| 533 | |
| 534 | return ret; |
| 535 | }; |
| 536 | |
| 537 | /* |
| 538 | * report current pointer |
| 539 | */ |
| 540 | static snd_pcm_uframes_t snd_ps3_pcm_pointer( |
| 541 | struct snd_pcm_substream *substream) |
| 542 | { |
| 543 | struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream); |
| 544 | size_t bytes; |
| 545 | snd_pcm_uframes_t ret; |
| 546 | |
| 547 | spin_lock(&card->dma_lock); |
| 548 | { |
| 549 | bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] - |
| 550 | card->dma_start_vaddr[SND_PS3_CH_L]); |
| 551 | } |
| 552 | spin_unlock(&card->dma_lock); |
| 553 | |
| 554 | ret = bytes_to_frames(substream->runtime, bytes * 2); |
| 555 | |
| 556 | return ret; |
| 557 | }; |
| 558 | |
| 559 | static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream) |
| 560 | { |
| 561 | int ret; |
| 562 | ret = snd_pcm_lib_free_pages(substream); |
| 563 | return ret; |
| 564 | }; |
| 565 | |
| 566 | static int snd_ps3_pcm_close(struct snd_pcm_substream *substream) |
| 567 | { |
| 568 | /* mute on */ |
| 569 | snd_ps3_mute(1); |
| 570 | return 0; |
| 571 | }; |
| 572 | |
| 573 | static void snd_ps3_audio_fixup(struct snd_ps3_card_info *card) |
| 574 | { |
| 575 | /* |
| 576 | * avsetting driver seems to never change the followings |
| 577 | * so, init them here once |
| 578 | */ |
| 579 | |
| 580 | /* no dma interrupt needed */ |
| 581 | write_reg(PS3_AUDIO_INTR_EN_0, 0); |
| 582 | |
| 583 | /* use every 4 buffer empty interrupt */ |
| 584 | update_mask_reg(PS3_AUDIO_AX_IC, |
| 585 | PS3_AUDIO_AX_IC_AASOIMD_MASK, |
| 586 | PS3_AUDIO_AX_IC_AASOIMD_EVERY4); |
| 587 | |
| 588 | /* enable 3wire clocks */ |
| 589 | update_mask_reg(PS3_AUDIO_AO_3WMCTRL, |
| 590 | ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED | |
| 591 | PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED), |
| 592 | 0); |
| 593 | update_reg(PS3_AUDIO_AO_3WMCTRL, |
| 594 | PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT); |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * av setting |
| 599 | * NOTE: calling this function may generate audio interrupt. |
| 600 | */ |
| 601 | static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card) |
| 602 | { |
| 603 | int ret, retries, i; |
| 604 | pr_debug("%s: start\n", __func__); |
| 605 | |
| 606 | ret = ps3av_set_audio_mode(card->avs.avs_audio_ch, |
| 607 | card->avs.avs_audio_rate, |
| 608 | card->avs.avs_audio_width, |
| 609 | card->avs.avs_audio_format, |
| 610 | card->avs.avs_audio_source); |
| 611 | /* |
| 612 | * Reset the following unwanted settings: |
| 613 | */ |
| 614 | |
| 615 | /* disable all 3wire buffers */ |
| 616 | update_mask_reg(PS3_AUDIO_AO_3WMCTRL, |
| 617 | ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) | |
| 618 | PS3_AUDIO_AO_3WMCTRL_ASOEN(1) | |
| 619 | PS3_AUDIO_AO_3WMCTRL_ASOEN(2) | |
| 620 | PS3_AUDIO_AO_3WMCTRL_ASOEN(3)), |
| 621 | 0); |
| 622 | wmb(); /* ensure the hardware sees the change */ |
| 623 | /* wait for actually stopped */ |
| 624 | retries = 1000; |
| 625 | while ((read_reg(PS3_AUDIO_AO_3WMCTRL) & |
| 626 | (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) | |
| 627 | PS3_AUDIO_AO_3WMCTRL_ASORUN(1) | |
| 628 | PS3_AUDIO_AO_3WMCTRL_ASORUN(2) | |
| 629 | PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) && |
| 630 | --retries) { |
| 631 | udelay(1); |
| 632 | } |
| 633 | |
| 634 | /* reset buffer pointer */ |
| 635 | for (i = 0; i < 4; i++) { |
| 636 | update_reg(PS3_AUDIO_AO_3WCTRL(i), |
| 637 | PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET); |
| 638 | udelay(10); |
| 639 | } |
| 640 | wmb(); /* ensure the hardware actually start resetting */ |
| 641 | |
| 642 | /* enable 3wire#0 buffer */ |
| 643 | update_reg(PS3_AUDIO_AO_3WMCTRL, PS3_AUDIO_AO_3WMCTRL_ASOEN(0)); |
| 644 | |
| 645 | |
| 646 | /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */ |
| 647 | update_mask_reg(PS3_AUDIO_AO_3WCTRL(0), |
| 648 | ~PS3_AUDIO_AO_3WCTRL_ASODF, |
| 649 | PS3_AUDIO_AO_3WCTRL_ASODF_LSB); |
| 650 | update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0), |
| 651 | ~PS3_AUDIO_AO_SPDCTRL_SPODF, |
| 652 | PS3_AUDIO_AO_SPDCTRL_SPODF_LSB); |
| 653 | /* ensure all the setting above is written back to register */ |
| 654 | wmb(); |
| 655 | /* avsetting driver altered AX_IE, caller must reset it if you want */ |
| 656 | pr_debug("%s: end\n", __func__); |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card) |
| 661 | { |
| 662 | int ret; |
| 663 | pr_debug("%s: start\n", __func__); |
| 664 | card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2; |
| 665 | card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K; |
| 666 | card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16; |
| 667 | card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM; |
| 668 | card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL; |
Takashi Iwai | 1ee2a32 | 2008-10-20 08:06:39 +0200 | [diff] [blame] | 669 | memcpy(card->avs.avs_cs_info, ps3av_mode_cs_info, 8); |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 670 | |
| 671 | ret = snd_ps3_change_avsetting(card); |
| 672 | |
| 673 | snd_ps3_audio_fixup(card); |
| 674 | |
| 675 | /* to start to generate SPDIF signal, fill data */ |
| 676 | snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL); |
| 677 | snd_ps3_kick_dma(card); |
| 678 | pr_debug("%s: end\n", __func__); |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * set sampling rate according to the substream |
| 684 | */ |
| 685 | static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream) |
| 686 | { |
| 687 | struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream); |
| 688 | struct snd_ps3_avsetting_info avs; |
Takashi Iwai | 1ee2a32 | 2008-10-20 08:06:39 +0200 | [diff] [blame] | 689 | int ret; |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 690 | |
| 691 | avs = card->avs; |
| 692 | |
| 693 | pr_debug("%s: called freq=%d width=%d\n", __func__, |
| 694 | substream->runtime->rate, |
| 695 | snd_pcm_format_width(substream->runtime->format)); |
| 696 | |
| 697 | pr_debug("%s: before freq=%d width=%d\n", __func__, |
| 698 | card->avs.avs_audio_rate, card->avs.avs_audio_width); |
| 699 | |
| 700 | /* sample rate */ |
| 701 | switch (substream->runtime->rate) { |
| 702 | case 44100: |
| 703 | avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_44K; |
| 704 | break; |
| 705 | case 48000: |
| 706 | avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K; |
| 707 | break; |
| 708 | case 88200: |
| 709 | avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_88K; |
| 710 | break; |
| 711 | case 96000: |
| 712 | avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_96K; |
| 713 | break; |
| 714 | default: |
| 715 | pr_info("%s: invalid rate %d\n", __func__, |
| 716 | substream->runtime->rate); |
| 717 | return 1; |
| 718 | } |
| 719 | |
| 720 | /* width */ |
| 721 | switch (snd_pcm_format_width(substream->runtime->format)) { |
| 722 | case 16: |
| 723 | avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16; |
| 724 | break; |
| 725 | case 24: |
| 726 | avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_24; |
| 727 | break; |
| 728 | default: |
| 729 | pr_info("%s: invalid width %d\n", __func__, |
| 730 | snd_pcm_format_width(substream->runtime->format)); |
| 731 | return 1; |
| 732 | } |
| 733 | |
Takashi Iwai | 1ee2a32 | 2008-10-20 08:06:39 +0200 | [diff] [blame] | 734 | memcpy(avs.avs_cs_info, ps3av_mode_cs_info, 8); |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 735 | |
Takashi Iwai | 1ee2a32 | 2008-10-20 08:06:39 +0200 | [diff] [blame] | 736 | if (memcmp(&card->avs, &avs, sizeof(avs))) { |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 737 | pr_debug("%s: after freq=%d width=%d\n", __func__, |
| 738 | card->avs.avs_audio_rate, card->avs.avs_audio_width); |
| 739 | |
Takashi Iwai | 1ee2a32 | 2008-10-20 08:06:39 +0200 | [diff] [blame] | 740 | card->avs = avs; |
| 741 | snd_ps3_change_avsetting(card); |
| 742 | ret = 0; |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 743 | } else |
Takashi Iwai | 1ee2a32 | 2008-10-20 08:06:39 +0200 | [diff] [blame] | 744 | ret = 1; |
| 745 | |
| 746 | /* check CS non-audio bit and mute accordingly */ |
| 747 | if (avs.avs_cs_info[0] & 0x02) |
| 748 | ps3av_audio_mute_analog(1); /* mute if non-audio */ |
| 749 | else |
| 750 | ps3av_audio_mute_analog(0); |
| 751 | |
| 752 | return ret; |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Takashi Iwai | 1ee2a32 | 2008-10-20 08:06:39 +0200 | [diff] [blame] | 755 | /* |
| 756 | * SPDIF status bits controls |
| 757 | */ |
| 758 | static int snd_ps3_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 759 | struct snd_ctl_elem_info *uinfo) |
| 760 | { |
| 761 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 762 | uinfo->count = 1; |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | /* FIXME: ps3av_set_audio_mode() assumes only consumer mode */ |
| 767 | static int snd_ps3_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 768 | struct snd_ctl_elem_value *ucontrol) |
| 769 | { |
| 770 | memset(ucontrol->value.iec958.status, 0xff, 8); |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static int snd_ps3_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 775 | struct snd_ctl_elem_value *ucontrol) |
| 776 | { |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | static int snd_ps3_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 781 | struct snd_ctl_elem_value *ucontrol) |
| 782 | { |
| 783 | memcpy(ucontrol->value.iec958.status, ps3av_mode_cs_info, 8); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static int snd_ps3_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 788 | struct snd_ctl_elem_value *ucontrol) |
| 789 | { |
| 790 | if (memcmp(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8)) { |
| 791 | memcpy(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8); |
| 792 | return 1; |
| 793 | } |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | static struct snd_kcontrol_new spdif_ctls[] = { |
| 798 | { |
| 799 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 800 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 801 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK), |
| 802 | .info = snd_ps3_spdif_mask_info, |
| 803 | .get = snd_ps3_spdif_cmask_get, |
| 804 | }, |
| 805 | { |
| 806 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 807 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 808 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK), |
| 809 | .info = snd_ps3_spdif_mask_info, |
| 810 | .get = snd_ps3_spdif_pmask_get, |
| 811 | }, |
| 812 | { |
| 813 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 814 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), |
| 815 | .info = snd_ps3_spdif_mask_info, |
| 816 | .get = snd_ps3_spdif_default_get, |
| 817 | .put = snd_ps3_spdif_default_put, |
| 818 | }, |
| 819 | }; |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 820 | |
| 821 | |
| 822 | static int snd_ps3_map_mmio(void) |
| 823 | { |
| 824 | the_card.mapped_mmio_vaddr = |
| 825 | ioremap(the_card.ps3_dev->m_region->bus_addr, |
| 826 | the_card.ps3_dev->m_region->len); |
| 827 | |
| 828 | if (!the_card.mapped_mmio_vaddr) { |
| 829 | pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n", |
| 830 | __func__, the_card.ps3_dev->m_region->lpar_addr, |
| 831 | the_card.ps3_dev->m_region->len); |
| 832 | return -ENXIO; |
| 833 | } |
| 834 | |
| 835 | return 0; |
| 836 | }; |
| 837 | |
| 838 | static void snd_ps3_unmap_mmio(void) |
| 839 | { |
| 840 | iounmap(the_card.mapped_mmio_vaddr); |
| 841 | the_card.mapped_mmio_vaddr = NULL; |
| 842 | } |
| 843 | |
| 844 | static int snd_ps3_allocate_irq(void) |
| 845 | { |
| 846 | int ret; |
| 847 | u64 lpar_addr, lpar_size; |
| 848 | u64 __iomem *mapped; |
| 849 | |
| 850 | /* FIXME: move this to device_init (H/W probe) */ |
| 851 | |
| 852 | /* get irq outlet */ |
| 853 | ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size); |
| 854 | if (ret) { |
| 855 | pr_info("%s: device map 1 failed %d\n", __func__, |
| 856 | ret); |
| 857 | return -ENXIO; |
| 858 | } |
| 859 | |
| 860 | mapped = ioremap(lpar_addr, lpar_size); |
| 861 | if (!mapped) { |
| 862 | pr_info("%s: ioremap 1 failed \n", __func__); |
| 863 | return -ENXIO; |
| 864 | } |
| 865 | |
| 866 | the_card.audio_irq_outlet = in_be64(mapped); |
| 867 | |
| 868 | iounmap(mapped); |
| 869 | ret = lv1_gpu_device_unmap(1); |
| 870 | if (ret) |
| 871 | pr_info("%s: unmap 1 failed\n", __func__); |
| 872 | |
| 873 | /* irq */ |
| 874 | ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, |
| 875 | the_card.audio_irq_outlet, |
| 876 | &the_card.irq_no); |
| 877 | if (ret) { |
| 878 | pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret); |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | ret = request_irq(the_card.irq_no, snd_ps3_interrupt, IRQF_DISABLED, |
| 883 | SND_PS3_DRIVER_NAME, &the_card); |
| 884 | if (ret) { |
| 885 | pr_info("%s: request_irq failed (%d)\n", __func__, ret); |
| 886 | goto cleanup_irq; |
| 887 | } |
| 888 | |
| 889 | return 0; |
| 890 | |
| 891 | cleanup_irq: |
| 892 | ps3_irq_plug_destroy(the_card.irq_no); |
| 893 | return ret; |
| 894 | }; |
| 895 | |
| 896 | static void snd_ps3_free_irq(void) |
| 897 | { |
| 898 | free_irq(the_card.irq_no, &the_card); |
| 899 | ps3_irq_plug_destroy(the_card.irq_no); |
| 900 | } |
| 901 | |
| 902 | static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start) |
| 903 | { |
| 904 | uint64_t val; |
| 905 | int ret; |
| 906 | |
| 907 | val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) | |
| 908 | (0x03UL << 24) | |
| 909 | (0x0fUL << 12) | |
| 910 | (PS3_AUDIO_IOID); |
| 911 | |
| 912 | ret = lv1_gpu_attribute(0x100, 0x007, val, 0, 0); |
| 913 | if (ret) |
| 914 | pr_info("%s: gpu_attribute failed %d\n", __func__, |
| 915 | ret); |
| 916 | } |
| 917 | |
| 918 | static int __init snd_ps3_driver_probe(struct ps3_system_bus_device *dev) |
| 919 | { |
Takashi Iwai | 1ee2a32 | 2008-10-20 08:06:39 +0200 | [diff] [blame] | 920 | int i, ret; |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 921 | u64 lpar_addr, lpar_size; |
| 922 | |
| 923 | BUG_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1)); |
| 924 | BUG_ON(dev->match_id != PS3_MATCH_ID_SOUND); |
| 925 | |
| 926 | the_card.ps3_dev = dev; |
| 927 | |
| 928 | ret = ps3_open_hv_device(dev); |
| 929 | |
| 930 | if (ret) |
| 931 | return -ENXIO; |
| 932 | |
| 933 | /* setup MMIO */ |
| 934 | ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size); |
| 935 | if (ret) { |
| 936 | pr_info("%s: device map 2 failed %d\n", __func__, ret); |
| 937 | goto clean_open; |
| 938 | } |
| 939 | ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size, |
| 940 | PAGE_SHIFT); |
| 941 | |
| 942 | ret = snd_ps3_map_mmio(); |
| 943 | if (ret) |
| 944 | goto clean_dev_map; |
| 945 | |
| 946 | /* setup DMA area */ |
| 947 | ps3_dma_region_init(dev, dev->d_region, |
| 948 | PAGE_SHIFT, /* use system page size */ |
| 949 | 0, /* dma type; not used */ |
| 950 | NULL, |
| 951 | _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE)); |
| 952 | dev->d_region->ioid = PS3_AUDIO_IOID; |
| 953 | |
| 954 | ret = ps3_dma_region_create(dev->d_region); |
| 955 | if (ret) { |
| 956 | pr_info("%s: region_create\n", __func__); |
| 957 | goto clean_mmio; |
| 958 | } |
| 959 | |
| 960 | snd_ps3_audio_set_base_addr(dev->d_region->bus_addr); |
| 961 | |
| 962 | /* CONFIG_SND_PS3_DEFAULT_START_DELAY */ |
| 963 | the_card.start_delay = snd_ps3_start_delay; |
| 964 | |
| 965 | /* irq */ |
| 966 | if (snd_ps3_allocate_irq()) { |
| 967 | ret = -ENXIO; |
| 968 | goto clean_dma_region; |
| 969 | } |
| 970 | |
| 971 | /* create card instance */ |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 972 | ret = snd_card_create(index, id, THIS_MODULE, 0, &the_card.card); |
| 973 | if (ret < 0) |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 974 | goto clean_irq; |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 975 | |
| 976 | strcpy(the_card.card->driver, "PS3"); |
| 977 | strcpy(the_card.card->shortname, "PS3"); |
| 978 | strcpy(the_card.card->longname, "PS3 sound"); |
Takashi Iwai | 1ee2a32 | 2008-10-20 08:06:39 +0200 | [diff] [blame] | 979 | |
| 980 | /* create control elements */ |
| 981 | for (i = 0; i < ARRAY_SIZE(spdif_ctls); i++) { |
| 982 | ret = snd_ctl_add(the_card.card, |
| 983 | snd_ctl_new1(&spdif_ctls[i], &the_card)); |
| 984 | if (ret < 0) |
| 985 | goto clean_card; |
| 986 | } |
| 987 | |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 988 | /* create PCM devices instance */ |
| 989 | /* NOTE:this driver works assuming pcm:substream = 1:1 */ |
| 990 | ret = snd_pcm_new(the_card.card, |
| 991 | "SPDIF", |
| 992 | 0, /* instance index, will be stored pcm.device*/ |
| 993 | 1, /* output substream */ |
| 994 | 0, /* input substream */ |
| 995 | &(the_card.pcm)); |
| 996 | if (ret) |
| 997 | goto clean_card; |
| 998 | |
| 999 | the_card.pcm->private_data = &the_card; |
| 1000 | strcpy(the_card.pcm->name, "SPDIF"); |
| 1001 | |
| 1002 | /* set pcm ops */ |
| 1003 | snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 1004 | &snd_ps3_pcm_spdif_ops); |
| 1005 | |
| 1006 | the_card.pcm->info_flags = SNDRV_PCM_INFO_NONINTERLEAVED; |
| 1007 | /* pre-alloc PCM DMA buffer*/ |
| 1008 | ret = snd_pcm_lib_preallocate_pages_for_all(the_card.pcm, |
| 1009 | SNDRV_DMA_TYPE_DEV, |
| 1010 | &dev->core, |
| 1011 | SND_PS3_PCM_PREALLOC_SIZE, |
| 1012 | SND_PS3_PCM_PREALLOC_SIZE); |
| 1013 | if (ret < 0) { |
| 1014 | pr_info("%s: prealloc failed\n", __func__); |
| 1015 | goto clean_card; |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * allocate null buffer |
| 1020 | * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2 |
| 1021 | * PAGE_SIZE is enogh |
| 1022 | */ |
| 1023 | if (!(the_card.null_buffer_start_vaddr = |
| 1024 | dma_alloc_coherent(&the_card.ps3_dev->core, |
| 1025 | PAGE_SIZE, |
| 1026 | &the_card.null_buffer_start_dma_addr, |
| 1027 | GFP_KERNEL))) { |
| 1028 | pr_info("%s: nullbuffer alloc failed\n", __func__); |
| 1029 | goto clean_preallocate; |
| 1030 | } |
Stephen Rothwell | 26db11a | 2009-01-13 20:07:55 +0000 | [diff] [blame] | 1031 | pr_debug("%s: null vaddr=%p dma=%#llx\n", __func__, |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 1032 | the_card.null_buffer_start_vaddr, |
| 1033 | the_card.null_buffer_start_dma_addr); |
| 1034 | /* set default sample rate/word width */ |
| 1035 | snd_ps3_init_avsetting(&the_card); |
| 1036 | |
| 1037 | /* register the card */ |
Takashi Iwai | f78dfac | 2007-12-17 16:24:04 +0100 | [diff] [blame] | 1038 | snd_card_set_dev(the_card.card, &dev->core); |
Masakazu Mokuno | c454fd4 | 2007-07-03 12:54:49 +0200 | [diff] [blame] | 1039 | ret = snd_card_register(the_card.card); |
| 1040 | if (ret < 0) |
| 1041 | goto clean_dma_map; |
| 1042 | |
| 1043 | pr_info("%s started. start_delay=%dms\n", |
| 1044 | the_card.card->longname, the_card.start_delay); |
| 1045 | return 0; |
| 1046 | |
| 1047 | clean_dma_map: |
| 1048 | dma_free_coherent(&the_card.ps3_dev->core, |
| 1049 | PAGE_SIZE, |
| 1050 | the_card.null_buffer_start_vaddr, |
| 1051 | the_card.null_buffer_start_dma_addr); |
| 1052 | clean_preallocate: |
| 1053 | snd_pcm_lib_preallocate_free_for_all(the_card.pcm); |
| 1054 | clean_card: |
| 1055 | snd_card_free(the_card.card); |
| 1056 | clean_irq: |
| 1057 | snd_ps3_free_irq(); |
| 1058 | clean_dma_region: |
| 1059 | ps3_dma_region_free(dev->d_region); |
| 1060 | clean_mmio: |
| 1061 | snd_ps3_unmap_mmio(); |
| 1062 | clean_dev_map: |
| 1063 | lv1_gpu_device_unmap(2); |
| 1064 | clean_open: |
| 1065 | ps3_close_hv_device(dev); |
| 1066 | /* |
| 1067 | * there is no destructor function to pcm. |
| 1068 | * midlayer automatically releases if the card removed |
| 1069 | */ |
| 1070 | return ret; |
| 1071 | }; /* snd_ps3_probe */ |
| 1072 | |
| 1073 | /* called when module removal */ |
| 1074 | static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev) |
| 1075 | { |
| 1076 | int ret; |
| 1077 | pr_info("%s:start id=%d\n", __func__, dev->match_id); |
| 1078 | if (dev->match_id != PS3_MATCH_ID_SOUND) |
| 1079 | return -ENXIO; |
| 1080 | |
| 1081 | /* |
| 1082 | * ctl and preallocate buffer will be freed in |
| 1083 | * snd_card_free |
| 1084 | */ |
| 1085 | ret = snd_card_free(the_card.card); |
| 1086 | if (ret) |
| 1087 | pr_info("%s: ctl freecard=%d\n", __func__, ret); |
| 1088 | |
| 1089 | dma_free_coherent(&dev->core, |
| 1090 | PAGE_SIZE, |
| 1091 | the_card.null_buffer_start_vaddr, |
| 1092 | the_card.null_buffer_start_dma_addr); |
| 1093 | |
| 1094 | ps3_dma_region_free(dev->d_region); |
| 1095 | |
| 1096 | snd_ps3_free_irq(); |
| 1097 | snd_ps3_unmap_mmio(); |
| 1098 | |
| 1099 | lv1_gpu_device_unmap(2); |
| 1100 | ps3_close_hv_device(dev); |
| 1101 | pr_info("%s:end id=%d\n", __func__, dev->match_id); |
| 1102 | return 0; |
| 1103 | } /* snd_ps3_remove */ |
| 1104 | |
| 1105 | static struct ps3_system_bus_driver snd_ps3_bus_driver_info = { |
| 1106 | .match_id = PS3_MATCH_ID_SOUND, |
| 1107 | .probe = snd_ps3_driver_probe, |
| 1108 | .remove = snd_ps3_driver_remove, |
| 1109 | .shutdown = snd_ps3_driver_remove, |
| 1110 | .core = { |
| 1111 | .name = SND_PS3_DRIVER_NAME, |
| 1112 | .owner = THIS_MODULE, |
| 1113 | }, |
| 1114 | }; |
| 1115 | |
| 1116 | |
| 1117 | /* |
| 1118 | * Interrupt handler |
| 1119 | */ |
| 1120 | static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id) |
| 1121 | { |
| 1122 | |
| 1123 | uint32_t port_intr; |
| 1124 | int underflow_occured = 0; |
| 1125 | struct snd_ps3_card_info *card = dev_id; |
| 1126 | |
| 1127 | if (!card->running) { |
| 1128 | update_reg(PS3_AUDIO_AX_IS, 0); |
| 1129 | update_reg(PS3_AUDIO_INTR_0, 0); |
| 1130 | return IRQ_HANDLED; |
| 1131 | } |
| 1132 | |
| 1133 | port_intr = read_reg(PS3_AUDIO_AX_IS); |
| 1134 | /* |
| 1135 | *serial buffer empty detected (every 4 times), |
| 1136 | *program next dma and kick it |
| 1137 | */ |
| 1138 | if (port_intr & PS3_AUDIO_AX_IE_ASOBEIE(0)) { |
| 1139 | write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBEIE(0)); |
| 1140 | if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) { |
| 1141 | write_reg(PS3_AUDIO_AX_IS, port_intr); |
| 1142 | underflow_occured = 1; |
| 1143 | } |
| 1144 | if (card->silent) { |
| 1145 | /* we are still in silent time */ |
| 1146 | snd_ps3_program_dma(card, |
| 1147 | (underflow_occured) ? |
| 1148 | SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL : |
| 1149 | SND_PS3_DMA_FILLTYPE_SILENT_RUNNING); |
| 1150 | snd_ps3_kick_dma(card); |
| 1151 | card->silent --; |
| 1152 | } else { |
| 1153 | snd_ps3_program_dma(card, |
| 1154 | (underflow_occured) ? |
| 1155 | SND_PS3_DMA_FILLTYPE_FIRSTFILL : |
| 1156 | SND_PS3_DMA_FILLTYPE_RUNNING); |
| 1157 | snd_ps3_kick_dma(card); |
| 1158 | snd_pcm_period_elapsed(card->substream); |
| 1159 | } |
| 1160 | } else if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) { |
| 1161 | write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBUIE(0)); |
| 1162 | /* |
| 1163 | * serial out underflow, but buffer empty not detected. |
| 1164 | * in this case, fill fifo with 0 to recover. After |
| 1165 | * filling dummy data, serial automatically start to |
| 1166 | * consume them and then will generate normal buffer |
| 1167 | * empty interrupts. |
| 1168 | * If both buffer underflow and buffer empty are occured, |
| 1169 | * it is better to do nomal data transfer than empty one |
| 1170 | */ |
| 1171 | snd_ps3_program_dma(card, |
| 1172 | SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL); |
| 1173 | snd_ps3_kick_dma(card); |
| 1174 | snd_ps3_program_dma(card, |
| 1175 | SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL); |
| 1176 | snd_ps3_kick_dma(card); |
| 1177 | } |
| 1178 | /* clear interrupt cause */ |
| 1179 | return IRQ_HANDLED; |
| 1180 | }; |
| 1181 | |
| 1182 | /* |
| 1183 | * module/subsystem initialize/terminate |
| 1184 | */ |
| 1185 | static int __init snd_ps3_init(void) |
| 1186 | { |
| 1187 | int ret; |
| 1188 | |
| 1189 | if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) |
| 1190 | return -ENXIO; |
| 1191 | |
| 1192 | memset(&the_card, 0, sizeof(the_card)); |
| 1193 | spin_lock_init(&the_card.dma_lock); |
| 1194 | |
| 1195 | /* register systembus DRIVER, this calls our probe() func */ |
| 1196 | ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info); |
| 1197 | |
| 1198 | return ret; |
| 1199 | } |
| 1200 | |
| 1201 | static void __exit snd_ps3_exit(void) |
| 1202 | { |
| 1203 | ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info); |
| 1204 | } |
| 1205 | |
| 1206 | MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND); |