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