snd_ps3.c 28 KB

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