snd_ps3.c 29 KB

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