snd_ps3.c 28 KB

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