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 <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. return snd_pcm_lib_free_pages(substream);
  498. };
  499. static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream,
  500. unsigned int delay_ms)
  501. {
  502. int ret;
  503. int rate ;
  504. rate = substream->runtime->rate;
  505. ret = snd_pcm_format_size(substream->runtime->format,
  506. rate * delay_ms / 1000)
  507. * substream->runtime->channels;
  508. pr_debug("%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
  509. __func__,
  510. delay_ms,
  511. rate,
  512. snd_pcm_format_size(substream->runtime->format, rate),
  513. rate * delay_ms / 1000,
  514. ret);
  515. return ret;
  516. };
  517. static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream)
  518. {
  519. struct snd_pcm_runtime *runtime = substream->runtime;
  520. struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
  521. unsigned long irqsave;
  522. if (!snd_ps3_set_avsetting(substream)) {
  523. /* some parameter changed */
  524. write_reg(PS3_AUDIO_AX_IE,
  525. PS3_AUDIO_AX_IE_ASOBEIE(0) |
  526. PS3_AUDIO_AX_IE_ASOBUIE(0));
  527. /*
  528. * let SPDIF device re-lock with SPDIF signal,
  529. * start with some silence
  530. */
  531. card->silent = snd_ps3_delay_to_bytes(substream,
  532. card->start_delay) /
  533. (PS3_AUDIO_FIFO_STAGE_SIZE * 4); /* every 4 times */
  534. }
  535. /* restart ring buffer pointer */
  536. spin_lock_irqsave(&card->dma_lock, irqsave);
  537. {
  538. card->dma_buffer_size = runtime->dma_bytes;
  539. card->dma_last_transfer_vaddr[SND_PS3_CH_L] =
  540. card->dma_next_transfer_vaddr[SND_PS3_CH_L] =
  541. card->dma_start_vaddr[SND_PS3_CH_L] =
  542. runtime->dma_area;
  543. card->dma_start_bus_addr[SND_PS3_CH_L] = runtime->dma_addr;
  544. card->dma_last_transfer_vaddr[SND_PS3_CH_R] =
  545. card->dma_next_transfer_vaddr[SND_PS3_CH_R] =
  546. card->dma_start_vaddr[SND_PS3_CH_R] =
  547. runtime->dma_area + (runtime->dma_bytes / 2);
  548. card->dma_start_bus_addr[SND_PS3_CH_R] =
  549. runtime->dma_addr + (runtime->dma_bytes / 2);
  550. pr_debug("%s: vaddr=%p bus=%#llx\n", __func__,
  551. card->dma_start_vaddr[SND_PS3_CH_L],
  552. card->dma_start_bus_addr[SND_PS3_CH_L]);
  553. }
  554. spin_unlock_irqrestore(&card->dma_lock, irqsave);
  555. /* ensure the hardware sees the change */
  556. mb();
  557. return 0;
  558. };
  559. static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
  560. int cmd)
  561. {
  562. struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
  563. int ret = 0;
  564. switch (cmd) {
  565. case SNDRV_PCM_TRIGGER_START:
  566. /* clear outstanding interrupts */
  567. update_reg(PS3_AUDIO_AX_IS, 0);
  568. spin_lock(&card->dma_lock);
  569. {
  570. card->running = 1;
  571. }
  572. spin_unlock(&card->dma_lock);
  573. snd_ps3_program_dma(card,
  574. SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
  575. snd_ps3_kick_dma(card);
  576. while (read_reg(PS3_AUDIO_KICK(7)) &
  577. PS3_AUDIO_KICK_STATUS_MASK) {
  578. udelay(1);
  579. }
  580. snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
  581. snd_ps3_kick_dma(card);
  582. break;
  583. case SNDRV_PCM_TRIGGER_STOP:
  584. spin_lock(&card->dma_lock);
  585. {
  586. card->running = 0;
  587. }
  588. spin_unlock(&card->dma_lock);
  589. snd_ps3_wait_for_dma_stop(card);
  590. break;
  591. default:
  592. break;
  593. }
  594. return ret;
  595. };
  596. /*
  597. * report current pointer
  598. */
  599. static snd_pcm_uframes_t snd_ps3_pcm_pointer(
  600. struct snd_pcm_substream *substream)
  601. {
  602. struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
  603. size_t bytes;
  604. snd_pcm_uframes_t ret;
  605. spin_lock(&card->dma_lock);
  606. {
  607. bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] -
  608. card->dma_start_vaddr[SND_PS3_CH_L]);
  609. }
  610. spin_unlock(&card->dma_lock);
  611. ret = bytes_to_frames(substream->runtime, bytes * 2);
  612. return ret;
  613. };
  614. /*
  615. * SPDIF status bits controls
  616. */
  617. static int snd_ps3_spdif_mask_info(struct snd_kcontrol *kcontrol,
  618. struct snd_ctl_elem_info *uinfo)
  619. {
  620. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  621. uinfo->count = 1;
  622. return 0;
  623. }
  624. /* FIXME: ps3av_set_audio_mode() assumes only consumer mode */
  625. static int snd_ps3_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  626. struct snd_ctl_elem_value *ucontrol)
  627. {
  628. memset(ucontrol->value.iec958.status, 0xff, 8);
  629. return 0;
  630. }
  631. static int snd_ps3_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  632. struct snd_ctl_elem_value *ucontrol)
  633. {
  634. return 0;
  635. }
  636. static int snd_ps3_spdif_default_get(struct snd_kcontrol *kcontrol,
  637. struct snd_ctl_elem_value *ucontrol)
  638. {
  639. memcpy(ucontrol->value.iec958.status, ps3av_mode_cs_info, 8);
  640. return 0;
  641. }
  642. static int snd_ps3_spdif_default_put(struct snd_kcontrol *kcontrol,
  643. struct snd_ctl_elem_value *ucontrol)
  644. {
  645. if (memcmp(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8)) {
  646. memcpy(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8);
  647. return 1;
  648. }
  649. return 0;
  650. }
  651. static struct snd_kcontrol_new spdif_ctls[] = {
  652. {
  653. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  654. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  655. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  656. .info = snd_ps3_spdif_mask_info,
  657. .get = snd_ps3_spdif_cmask_get,
  658. },
  659. {
  660. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  661. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  662. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  663. .info = snd_ps3_spdif_mask_info,
  664. .get = snd_ps3_spdif_pmask_get,
  665. },
  666. {
  667. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  668. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  669. .info = snd_ps3_spdif_mask_info,
  670. .get = snd_ps3_spdif_default_get,
  671. .put = snd_ps3_spdif_default_put,
  672. },
  673. };
  674. static struct snd_pcm_ops snd_ps3_pcm_spdif_ops = {
  675. .open = snd_ps3_pcm_open,
  676. .close = snd_ps3_pcm_close,
  677. .ioctl = snd_pcm_lib_ioctl,
  678. .hw_params = snd_ps3_pcm_hw_params,
  679. .hw_free = snd_ps3_pcm_hw_free,
  680. .prepare = snd_ps3_pcm_prepare,
  681. .trigger = snd_ps3_pcm_trigger,
  682. .pointer = snd_ps3_pcm_pointer,
  683. };
  684. static int snd_ps3_map_mmio(void)
  685. {
  686. the_card.mapped_mmio_vaddr =
  687. ioremap(the_card.ps3_dev->m_region->bus_addr,
  688. the_card.ps3_dev->m_region->len);
  689. if (!the_card.mapped_mmio_vaddr) {
  690. pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
  691. __func__, the_card.ps3_dev->m_region->lpar_addr,
  692. the_card.ps3_dev->m_region->len);
  693. return -ENXIO;
  694. }
  695. return 0;
  696. };
  697. static void snd_ps3_unmap_mmio(void)
  698. {
  699. iounmap(the_card.mapped_mmio_vaddr);
  700. the_card.mapped_mmio_vaddr = NULL;
  701. }
  702. static int snd_ps3_allocate_irq(void)
  703. {
  704. int ret;
  705. u64 lpar_addr, lpar_size;
  706. u64 __iomem *mapped;
  707. /* FIXME: move this to device_init (H/W probe) */
  708. /* get irq outlet */
  709. ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size);
  710. if (ret) {
  711. pr_info("%s: device map 1 failed %d\n", __func__,
  712. ret);
  713. return -ENXIO;
  714. }
  715. mapped = ioremap(lpar_addr, lpar_size);
  716. if (!mapped) {
  717. pr_info("%s: ioremap 1 failed \n", __func__);
  718. return -ENXIO;
  719. }
  720. the_card.audio_irq_outlet = in_be64(mapped);
  721. iounmap(mapped);
  722. ret = lv1_gpu_device_unmap(1);
  723. if (ret)
  724. pr_info("%s: unmap 1 failed\n", __func__);
  725. /* irq */
  726. ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY,
  727. the_card.audio_irq_outlet,
  728. &the_card.irq_no);
  729. if (ret) {
  730. pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret);
  731. return ret;
  732. }
  733. ret = request_irq(the_card.irq_no, snd_ps3_interrupt, 0,
  734. SND_PS3_DRIVER_NAME, &the_card);
  735. if (ret) {
  736. pr_info("%s: request_irq failed (%d)\n", __func__, ret);
  737. goto cleanup_irq;
  738. }
  739. return 0;
  740. cleanup_irq:
  741. ps3_irq_plug_destroy(the_card.irq_no);
  742. return ret;
  743. };
  744. static void snd_ps3_free_irq(void)
  745. {
  746. free_irq(the_card.irq_no, &the_card);
  747. ps3_irq_plug_destroy(the_card.irq_no);
  748. }
  749. static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start)
  750. {
  751. uint64_t val;
  752. int ret;
  753. val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) |
  754. (0x03UL << 24) |
  755. (0x0fUL << 12) |
  756. (PS3_AUDIO_IOID);
  757. ret = lv1_gpu_attribute(0x100, 0x007, val);
  758. if (ret)
  759. pr_info("%s: gpu_attribute failed %d\n", __func__,
  760. ret);
  761. }
  762. static void snd_ps3_audio_fixup(struct snd_ps3_card_info *card)
  763. {
  764. /*
  765. * avsetting driver seems to never change the followings
  766. * so, init them here once
  767. */
  768. /* no dma interrupt needed */
  769. write_reg(PS3_AUDIO_INTR_EN_0, 0);
  770. /* use every 4 buffer empty interrupt */
  771. update_mask_reg(PS3_AUDIO_AX_IC,
  772. PS3_AUDIO_AX_IC_AASOIMD_MASK,
  773. PS3_AUDIO_AX_IC_AASOIMD_EVERY4);
  774. /* enable 3wire clocks */
  775. update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
  776. ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED |
  777. PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED),
  778. 0);
  779. update_reg(PS3_AUDIO_AO_3WMCTRL,
  780. PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT);
  781. }
  782. static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card)
  783. {
  784. int ret;
  785. pr_debug("%s: start\n", __func__);
  786. card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2;
  787. card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
  788. card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
  789. card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM;
  790. card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL;
  791. memcpy(card->avs.avs_cs_info, ps3av_mode_cs_info, 8);
  792. ret = snd_ps3_change_avsetting(card);
  793. snd_ps3_audio_fixup(card);
  794. /* to start to generate SPDIF signal, fill data */
  795. snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
  796. snd_ps3_kick_dma(card);
  797. pr_debug("%s: end\n", __func__);
  798. return ret;
  799. }
  800. static int snd_ps3_driver_probe(struct ps3_system_bus_device *dev)
  801. {
  802. int i, ret;
  803. u64 lpar_addr, lpar_size;
  804. if (WARN_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1)))
  805. return -ENODEV;
  806. if (WARN_ON(dev->match_id != PS3_MATCH_ID_SOUND))
  807. return -ENODEV;
  808. the_card.ps3_dev = dev;
  809. ret = ps3_open_hv_device(dev);
  810. if (ret)
  811. return -ENXIO;
  812. /* setup MMIO */
  813. ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size);
  814. if (ret) {
  815. pr_info("%s: device map 2 failed %d\n", __func__, ret);
  816. goto clean_open;
  817. }
  818. ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size,
  819. PAGE_SHIFT);
  820. ret = snd_ps3_map_mmio();
  821. if (ret)
  822. goto clean_dev_map;
  823. /* setup DMA area */
  824. ps3_dma_region_init(dev, dev->d_region,
  825. PAGE_SHIFT, /* use system page size */
  826. 0, /* dma type; not used */
  827. NULL,
  828. _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE));
  829. dev->d_region->ioid = PS3_AUDIO_IOID;
  830. ret = ps3_dma_region_create(dev->d_region);
  831. if (ret) {
  832. pr_info("%s: region_create\n", __func__);
  833. goto clean_mmio;
  834. }
  835. snd_ps3_audio_set_base_addr(dev->d_region->bus_addr);
  836. /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
  837. the_card.start_delay = snd_ps3_start_delay;
  838. /* irq */
  839. if (snd_ps3_allocate_irq()) {
  840. ret = -ENXIO;
  841. goto clean_dma_region;
  842. }
  843. /* create card instance */
  844. ret = snd_card_new(&dev->core, index, id, THIS_MODULE,
  845. 0, &the_card.card);
  846. if (ret < 0)
  847. goto clean_irq;
  848. strcpy(the_card.card->driver, "PS3");
  849. strcpy(the_card.card->shortname, "PS3");
  850. strcpy(the_card.card->longname, "PS3 sound");
  851. /* create control elements */
  852. for (i = 0; i < ARRAY_SIZE(spdif_ctls); i++) {
  853. ret = snd_ctl_add(the_card.card,
  854. snd_ctl_new1(&spdif_ctls[i], &the_card));
  855. if (ret < 0)
  856. goto clean_card;
  857. }
  858. /* create PCM devices instance */
  859. /* NOTE:this driver works assuming pcm:substream = 1:1 */
  860. ret = snd_pcm_new(the_card.card,
  861. "SPDIF",
  862. 0, /* instance index, will be stored pcm.device*/
  863. 1, /* output substream */
  864. 0, /* input substream */
  865. &(the_card.pcm));
  866. if (ret)
  867. goto clean_card;
  868. the_card.pcm->private_data = &the_card;
  869. strcpy(the_card.pcm->name, "SPDIF");
  870. /* set pcm ops */
  871. snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK,
  872. &snd_ps3_pcm_spdif_ops);
  873. the_card.pcm->info_flags = SNDRV_PCM_INFO_NONINTERLEAVED;
  874. /* pre-alloc PCM DMA buffer*/
  875. ret = snd_pcm_lib_preallocate_pages_for_all(the_card.pcm,
  876. SNDRV_DMA_TYPE_DEV,
  877. &dev->core,
  878. SND_PS3_PCM_PREALLOC_SIZE,
  879. SND_PS3_PCM_PREALLOC_SIZE);
  880. if (ret < 0) {
  881. pr_info("%s: prealloc failed\n", __func__);
  882. goto clean_card;
  883. }
  884. /*
  885. * allocate null buffer
  886. * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
  887. * PAGE_SIZE is enogh
  888. */
  889. the_card.null_buffer_start_vaddr =
  890. dma_alloc_coherent(&the_card.ps3_dev->core,
  891. PAGE_SIZE,
  892. &the_card.null_buffer_start_dma_addr,
  893. GFP_KERNEL);
  894. if (!the_card.null_buffer_start_vaddr) {
  895. pr_info("%s: nullbuffer alloc failed\n", __func__);
  896. ret = -ENOMEM;
  897. goto clean_card;
  898. }
  899. pr_debug("%s: null vaddr=%p dma=%#llx\n", __func__,
  900. the_card.null_buffer_start_vaddr,
  901. the_card.null_buffer_start_dma_addr);
  902. /* set default sample rate/word width */
  903. snd_ps3_init_avsetting(&the_card);
  904. /* register the card */
  905. ret = snd_card_register(the_card.card);
  906. if (ret < 0)
  907. goto clean_dma_map;
  908. pr_info("%s started. start_delay=%dms\n",
  909. the_card.card->longname, the_card.start_delay);
  910. return 0;
  911. clean_dma_map:
  912. dma_free_coherent(&the_card.ps3_dev->core,
  913. PAGE_SIZE,
  914. the_card.null_buffer_start_vaddr,
  915. the_card.null_buffer_start_dma_addr);
  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);