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