snd_ps3.c 29 KB

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