ymfpci_main.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Routines for control of YMF724/740/744/754 chips
  4. *
  5. * BUGS:
  6. * --
  7. *
  8. * TODO:
  9. * --
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #include <sound/driver.h>
  27. #include <linux/delay.h>
  28. #include <linux/init.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/pci.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/vmalloc.h>
  34. #include <sound/core.h>
  35. #include <sound/control.h>
  36. #include <sound/info.h>
  37. #include <sound/ymfpci.h>
  38. #include <sound/asoundef.h>
  39. #include <sound/mpu401.h>
  40. #include <asm/io.h>
  41. /*
  42. * constants
  43. */
  44. /*
  45. * common I/O routines
  46. */
  47. static void snd_ymfpci_irq_wait(ymfpci_t *chip);
  48. static inline u8 snd_ymfpci_readb(ymfpci_t *chip, u32 offset)
  49. {
  50. return readb(chip->reg_area_virt + offset);
  51. }
  52. static inline void snd_ymfpci_writeb(ymfpci_t *chip, u32 offset, u8 val)
  53. {
  54. writeb(val, chip->reg_area_virt + offset);
  55. }
  56. static inline u16 snd_ymfpci_readw(ymfpci_t *chip, u32 offset)
  57. {
  58. return readw(chip->reg_area_virt + offset);
  59. }
  60. static inline void snd_ymfpci_writew(ymfpci_t *chip, u32 offset, u16 val)
  61. {
  62. writew(val, chip->reg_area_virt + offset);
  63. }
  64. static inline u32 snd_ymfpci_readl(ymfpci_t *chip, u32 offset)
  65. {
  66. return readl(chip->reg_area_virt + offset);
  67. }
  68. static inline void snd_ymfpci_writel(ymfpci_t *chip, u32 offset, u32 val)
  69. {
  70. writel(val, chip->reg_area_virt + offset);
  71. }
  72. static int snd_ymfpci_codec_ready(ymfpci_t *chip, int secondary)
  73. {
  74. signed long end_time;
  75. u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
  76. end_time = (jiffies + ((3 * HZ) / 4)) + 1;
  77. do {
  78. if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
  79. return 0;
  80. set_current_state(TASK_UNINTERRUPTIBLE);
  81. schedule_timeout(1);
  82. } while (end_time - (signed long)jiffies >= 0);
  83. snd_printk("codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
  84. return -EBUSY;
  85. }
  86. static void snd_ymfpci_codec_write(ac97_t *ac97, u16 reg, u16 val)
  87. {
  88. ymfpci_t *chip = ac97->private_data;
  89. u32 cmd;
  90. snd_ymfpci_codec_ready(chip, 0);
  91. cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
  92. snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
  93. }
  94. static u16 snd_ymfpci_codec_read(ac97_t *ac97, u16 reg)
  95. {
  96. ymfpci_t *chip = ac97->private_data;
  97. if (snd_ymfpci_codec_ready(chip, 0))
  98. return ~0;
  99. snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
  100. if (snd_ymfpci_codec_ready(chip, 0))
  101. return ~0;
  102. if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
  103. int i;
  104. for (i = 0; i < 600; i++)
  105. snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
  106. }
  107. return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
  108. }
  109. /*
  110. * Misc routines
  111. */
  112. static u32 snd_ymfpci_calc_delta(u32 rate)
  113. {
  114. switch (rate) {
  115. case 8000: return 0x02aaab00;
  116. case 11025: return 0x03accd00;
  117. case 16000: return 0x05555500;
  118. case 22050: return 0x07599a00;
  119. case 32000: return 0x0aaaab00;
  120. case 44100: return 0x0eb33300;
  121. default: return ((rate << 16) / 375) << 5;
  122. }
  123. }
  124. static u32 def_rate[8] = {
  125. 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
  126. };
  127. static u32 snd_ymfpci_calc_lpfK(u32 rate)
  128. {
  129. u32 i;
  130. static u32 val[8] = {
  131. 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
  132. 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
  133. };
  134. if (rate == 44100)
  135. return 0x40000000; /* FIXME: What's the right value? */
  136. for (i = 0; i < 8; i++)
  137. if (rate <= def_rate[i])
  138. return val[i];
  139. return val[0];
  140. }
  141. static u32 snd_ymfpci_calc_lpfQ(u32 rate)
  142. {
  143. u32 i;
  144. static u32 val[8] = {
  145. 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
  146. 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
  147. };
  148. if (rate == 44100)
  149. return 0x370A0000;
  150. for (i = 0; i < 8; i++)
  151. if (rate <= def_rate[i])
  152. return val[i];
  153. return val[0];
  154. }
  155. /*
  156. * Hardware start management
  157. */
  158. static void snd_ymfpci_hw_start(ymfpci_t *chip)
  159. {
  160. unsigned long flags;
  161. spin_lock_irqsave(&chip->reg_lock, flags);
  162. if (chip->start_count++ > 0)
  163. goto __end;
  164. snd_ymfpci_writel(chip, YDSXGR_MODE,
  165. snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
  166. chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
  167. __end:
  168. spin_unlock_irqrestore(&chip->reg_lock, flags);
  169. }
  170. static void snd_ymfpci_hw_stop(ymfpci_t *chip)
  171. {
  172. unsigned long flags;
  173. long timeout = 1000;
  174. spin_lock_irqsave(&chip->reg_lock, flags);
  175. if (--chip->start_count > 0)
  176. goto __end;
  177. snd_ymfpci_writel(chip, YDSXGR_MODE,
  178. snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
  179. while (timeout-- > 0) {
  180. if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
  181. break;
  182. }
  183. if (atomic_read(&chip->interrupt_sleep_count)) {
  184. atomic_set(&chip->interrupt_sleep_count, 0);
  185. wake_up(&chip->interrupt_sleep);
  186. }
  187. __end:
  188. spin_unlock_irqrestore(&chip->reg_lock, flags);
  189. }
  190. /*
  191. * Playback voice management
  192. */
  193. static int voice_alloc(ymfpci_t *chip, ymfpci_voice_type_t type, int pair, ymfpci_voice_t **rvoice)
  194. {
  195. ymfpci_voice_t *voice, *voice2;
  196. int idx;
  197. *rvoice = NULL;
  198. for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
  199. voice = &chip->voices[idx];
  200. voice2 = pair ? &chip->voices[idx+1] : NULL;
  201. if (voice->use || (voice2 && voice2->use))
  202. continue;
  203. voice->use = 1;
  204. if (voice2)
  205. voice2->use = 1;
  206. switch (type) {
  207. case YMFPCI_PCM:
  208. voice->pcm = 1;
  209. if (voice2)
  210. voice2->pcm = 1;
  211. break;
  212. case YMFPCI_SYNTH:
  213. voice->synth = 1;
  214. break;
  215. case YMFPCI_MIDI:
  216. voice->midi = 1;
  217. break;
  218. }
  219. snd_ymfpci_hw_start(chip);
  220. if (voice2)
  221. snd_ymfpci_hw_start(chip);
  222. *rvoice = voice;
  223. return 0;
  224. }
  225. return -ENOMEM;
  226. }
  227. static int snd_ymfpci_voice_alloc(ymfpci_t *chip, ymfpci_voice_type_t type, int pair, ymfpci_voice_t **rvoice)
  228. {
  229. unsigned long flags;
  230. int result;
  231. snd_assert(rvoice != NULL, return -EINVAL);
  232. snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
  233. spin_lock_irqsave(&chip->voice_lock, flags);
  234. for (;;) {
  235. result = voice_alloc(chip, type, pair, rvoice);
  236. if (result == 0 || type != YMFPCI_PCM)
  237. break;
  238. /* TODO: synth/midi voice deallocation */
  239. break;
  240. }
  241. spin_unlock_irqrestore(&chip->voice_lock, flags);
  242. return result;
  243. }
  244. static int snd_ymfpci_voice_free(ymfpci_t *chip, ymfpci_voice_t *pvoice)
  245. {
  246. unsigned long flags;
  247. snd_assert(pvoice != NULL, return -EINVAL);
  248. snd_ymfpci_hw_stop(chip);
  249. spin_lock_irqsave(&chip->voice_lock, flags);
  250. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
  251. pvoice->ypcm = NULL;
  252. pvoice->interrupt = NULL;
  253. spin_unlock_irqrestore(&chip->voice_lock, flags);
  254. return 0;
  255. }
  256. /*
  257. * PCM part
  258. */
  259. static void snd_ymfpci_pcm_interrupt(ymfpci_t *chip, ymfpci_voice_t *voice)
  260. {
  261. ymfpci_pcm_t *ypcm;
  262. u32 pos, delta;
  263. if ((ypcm = voice->ypcm) == NULL)
  264. return;
  265. if (ypcm->substream == NULL)
  266. return;
  267. spin_lock(&chip->reg_lock);
  268. if (ypcm->running) {
  269. pos = le32_to_cpu(voice->bank[chip->active_bank].start);
  270. if (pos < ypcm->last_pos)
  271. delta = pos + (ypcm->buffer_size - ypcm->last_pos);
  272. else
  273. delta = pos - ypcm->last_pos;
  274. ypcm->period_pos += delta;
  275. ypcm->last_pos = pos;
  276. if (ypcm->period_pos >= ypcm->period_size) {
  277. // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
  278. ypcm->period_pos %= ypcm->period_size;
  279. spin_unlock(&chip->reg_lock);
  280. snd_pcm_period_elapsed(ypcm->substream);
  281. spin_lock(&chip->reg_lock);
  282. }
  283. }
  284. spin_unlock(&chip->reg_lock);
  285. }
  286. static void snd_ymfpci_pcm_capture_interrupt(snd_pcm_substream_t *substream)
  287. {
  288. snd_pcm_runtime_t *runtime = substream->runtime;
  289. ymfpci_pcm_t *ypcm = runtime->private_data;
  290. ymfpci_t *chip = ypcm->chip;
  291. u32 pos, delta;
  292. spin_lock(&chip->reg_lock);
  293. if (ypcm->running) {
  294. pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
  295. if (pos < ypcm->last_pos)
  296. delta = pos + (ypcm->buffer_size - ypcm->last_pos);
  297. else
  298. delta = pos - ypcm->last_pos;
  299. ypcm->period_pos += delta;
  300. ypcm->last_pos = pos;
  301. if (ypcm->period_pos >= ypcm->period_size) {
  302. ypcm->period_pos %= ypcm->period_size;
  303. // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
  304. spin_unlock(&chip->reg_lock);
  305. snd_pcm_period_elapsed(substream);
  306. spin_lock(&chip->reg_lock);
  307. }
  308. }
  309. spin_unlock(&chip->reg_lock);
  310. }
  311. static int snd_ymfpci_playback_trigger(snd_pcm_substream_t * substream,
  312. int cmd)
  313. {
  314. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  315. ymfpci_pcm_t *ypcm = substream->runtime->private_data;
  316. int result = 0;
  317. spin_lock(&chip->reg_lock);
  318. if (ypcm->voices[0] == NULL) {
  319. result = -EINVAL;
  320. goto __unlock;
  321. }
  322. switch (cmd) {
  323. case SNDRV_PCM_TRIGGER_START:
  324. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  325. case SNDRV_PCM_TRIGGER_RESUME:
  326. chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
  327. if (ypcm->voices[1] != NULL)
  328. chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
  329. ypcm->running = 1;
  330. break;
  331. case SNDRV_PCM_TRIGGER_STOP:
  332. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  333. case SNDRV_PCM_TRIGGER_SUSPEND:
  334. chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
  335. if (ypcm->voices[1] != NULL)
  336. chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
  337. ypcm->running = 0;
  338. break;
  339. default:
  340. result = -EINVAL;
  341. break;
  342. }
  343. __unlock:
  344. spin_unlock(&chip->reg_lock);
  345. return result;
  346. }
  347. static int snd_ymfpci_capture_trigger(snd_pcm_substream_t * substream,
  348. int cmd)
  349. {
  350. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  351. ymfpci_pcm_t *ypcm = substream->runtime->private_data;
  352. int result = 0;
  353. u32 tmp;
  354. spin_lock(&chip->reg_lock);
  355. switch (cmd) {
  356. case SNDRV_PCM_TRIGGER_START:
  357. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  358. case SNDRV_PCM_TRIGGER_RESUME:
  359. tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
  360. snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
  361. ypcm->running = 1;
  362. break;
  363. case SNDRV_PCM_TRIGGER_STOP:
  364. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  365. case SNDRV_PCM_TRIGGER_SUSPEND:
  366. tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
  367. snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
  368. ypcm->running = 0;
  369. break;
  370. default:
  371. result = -EINVAL;
  372. break;
  373. }
  374. spin_unlock(&chip->reg_lock);
  375. return result;
  376. }
  377. static int snd_ymfpci_pcm_voice_alloc(ymfpci_pcm_t *ypcm, int voices)
  378. {
  379. int err;
  380. if (ypcm->voices[1] != NULL && voices < 2) {
  381. snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
  382. ypcm->voices[1] = NULL;
  383. }
  384. if (voices == 1 && ypcm->voices[0] != NULL)
  385. return 0; /* already allocated */
  386. if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
  387. return 0; /* already allocated */
  388. if (voices > 1) {
  389. if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
  390. snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
  391. ypcm->voices[0] = NULL;
  392. }
  393. }
  394. err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
  395. if (err < 0)
  396. return err;
  397. ypcm->voices[0]->ypcm = ypcm;
  398. ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
  399. if (voices > 1) {
  400. ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
  401. ypcm->voices[1]->ypcm = ypcm;
  402. }
  403. return 0;
  404. }
  405. static void snd_ymfpci_pcm_init_voice(ymfpci_voice_t *voice, int stereo,
  406. int rate, int w_16, unsigned long addr,
  407. unsigned int end,
  408. int output_front, int output_rear)
  409. {
  410. u32 format;
  411. u32 delta = snd_ymfpci_calc_delta(rate);
  412. u32 lpfQ = snd_ymfpci_calc_lpfQ(rate);
  413. u32 lpfK = snd_ymfpci_calc_lpfK(rate);
  414. snd_ymfpci_playback_bank_t *bank;
  415. unsigned int nbank;
  416. snd_assert(voice != NULL, return);
  417. format = (stereo ? 0x00010000 : 0) | (w_16 ? 0 : 0x80000000);
  418. for (nbank = 0; nbank < 2; nbank++) {
  419. bank = &voice->bank[nbank];
  420. bank->format = cpu_to_le32(format);
  421. bank->loop_default = 0;
  422. bank->base = cpu_to_le32(addr);
  423. bank->loop_start = 0;
  424. bank->loop_end = cpu_to_le32(end);
  425. bank->loop_frac = 0;
  426. bank->eg_gain_end = cpu_to_le32(0x40000000);
  427. bank->lpfQ = cpu_to_le32(lpfQ);
  428. bank->status = 0;
  429. bank->num_of_frames = 0;
  430. bank->loop_count = 0;
  431. bank->start = 0;
  432. bank->start_frac = 0;
  433. bank->delta =
  434. bank->delta_end = cpu_to_le32(delta);
  435. bank->lpfK =
  436. bank->lpfK_end = cpu_to_le32(lpfK);
  437. bank->eg_gain = cpu_to_le32(0x40000000);
  438. bank->lpfD1 =
  439. bank->lpfD2 = 0;
  440. bank->left_gain =
  441. bank->right_gain =
  442. bank->left_gain_end =
  443. bank->right_gain_end =
  444. bank->eff1_gain =
  445. bank->eff2_gain =
  446. bank->eff3_gain =
  447. bank->eff1_gain_end =
  448. bank->eff2_gain_end =
  449. bank->eff3_gain_end = 0;
  450. if (!stereo) {
  451. if (output_front) {
  452. bank->left_gain =
  453. bank->right_gain =
  454. bank->left_gain_end =
  455. bank->right_gain_end = cpu_to_le32(0x40000000);
  456. }
  457. if (output_rear) {
  458. bank->eff2_gain =
  459. bank->eff2_gain_end =
  460. bank->eff3_gain =
  461. bank->eff3_gain_end = cpu_to_le32(0x40000000);
  462. }
  463. } else {
  464. if (output_front) {
  465. if ((voice->number & 1) == 0) {
  466. bank->left_gain =
  467. bank->left_gain_end = cpu_to_le32(0x40000000);
  468. } else {
  469. bank->format |= cpu_to_le32(1);
  470. bank->right_gain =
  471. bank->right_gain_end = cpu_to_le32(0x40000000);
  472. }
  473. }
  474. if (output_rear) {
  475. if ((voice->number & 1) == 0) {
  476. bank->eff3_gain =
  477. bank->eff3_gain_end = cpu_to_le32(0x40000000);
  478. } else {
  479. bank->format |= cpu_to_le32(1);
  480. bank->eff2_gain =
  481. bank->eff2_gain_end = cpu_to_le32(0x40000000);
  482. }
  483. }
  484. }
  485. }
  486. }
  487. static int __devinit snd_ymfpci_ac3_init(ymfpci_t *chip)
  488. {
  489. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  490. 4096, &chip->ac3_tmp_base) < 0)
  491. return -ENOMEM;
  492. chip->bank_effect[3][0]->base =
  493. chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
  494. chip->bank_effect[3][0]->loop_end =
  495. chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
  496. chip->bank_effect[4][0]->base =
  497. chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
  498. chip->bank_effect[4][0]->loop_end =
  499. chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
  500. spin_lock_irq(&chip->reg_lock);
  501. snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
  502. snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
  503. spin_unlock_irq(&chip->reg_lock);
  504. return 0;
  505. }
  506. static int snd_ymfpci_ac3_done(ymfpci_t *chip)
  507. {
  508. spin_lock_irq(&chip->reg_lock);
  509. snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
  510. snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
  511. spin_unlock_irq(&chip->reg_lock);
  512. // snd_ymfpci_irq_wait(chip);
  513. if (chip->ac3_tmp_base.area) {
  514. snd_dma_free_pages(&chip->ac3_tmp_base);
  515. chip->ac3_tmp_base.area = NULL;
  516. }
  517. return 0;
  518. }
  519. static int snd_ymfpci_playback_hw_params(snd_pcm_substream_t * substream,
  520. snd_pcm_hw_params_t * hw_params)
  521. {
  522. snd_pcm_runtime_t *runtime = substream->runtime;
  523. ymfpci_pcm_t *ypcm = runtime->private_data;
  524. int err;
  525. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  526. return err;
  527. if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
  528. return err;
  529. return 0;
  530. }
  531. static int snd_ymfpci_playback_hw_free(snd_pcm_substream_t * substream)
  532. {
  533. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  534. snd_pcm_runtime_t *runtime = substream->runtime;
  535. ymfpci_pcm_t *ypcm;
  536. if (runtime->private_data == NULL)
  537. return 0;
  538. ypcm = runtime->private_data;
  539. /* wait, until the PCI operations are not finished */
  540. snd_ymfpci_irq_wait(chip);
  541. snd_pcm_lib_free_pages(substream);
  542. if (ypcm->voices[1]) {
  543. snd_ymfpci_voice_free(chip, ypcm->voices[1]);
  544. ypcm->voices[1] = NULL;
  545. }
  546. if (ypcm->voices[0]) {
  547. snd_ymfpci_voice_free(chip, ypcm->voices[0]);
  548. ypcm->voices[0] = NULL;
  549. }
  550. return 0;
  551. }
  552. static int snd_ymfpci_playback_prepare(snd_pcm_substream_t * substream)
  553. {
  554. // ymfpci_t *chip = snd_pcm_substream_chip(substream);
  555. snd_pcm_runtime_t *runtime = substream->runtime;
  556. ymfpci_pcm_t *ypcm = runtime->private_data;
  557. unsigned int nvoice;
  558. ypcm->period_size = runtime->period_size;
  559. ypcm->buffer_size = runtime->buffer_size;
  560. ypcm->period_pos = 0;
  561. ypcm->last_pos = 0;
  562. for (nvoice = 0; nvoice < runtime->channels; nvoice++)
  563. snd_ymfpci_pcm_init_voice(ypcm->voices[nvoice],
  564. runtime->channels == 2,
  565. runtime->rate,
  566. snd_pcm_format_width(runtime->format) == 16,
  567. runtime->dma_addr,
  568. ypcm->buffer_size,
  569. ypcm->output_front,
  570. ypcm->output_rear);
  571. return 0;
  572. }
  573. static int snd_ymfpci_capture_hw_params(snd_pcm_substream_t * substream,
  574. snd_pcm_hw_params_t * hw_params)
  575. {
  576. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  577. }
  578. static int snd_ymfpci_capture_hw_free(snd_pcm_substream_t * substream)
  579. {
  580. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  581. /* wait, until the PCI operations are not finished */
  582. snd_ymfpci_irq_wait(chip);
  583. return snd_pcm_lib_free_pages(substream);
  584. }
  585. static int snd_ymfpci_capture_prepare(snd_pcm_substream_t * substream)
  586. {
  587. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  588. snd_pcm_runtime_t *runtime = substream->runtime;
  589. ymfpci_pcm_t *ypcm = runtime->private_data;
  590. snd_ymfpci_capture_bank_t * bank;
  591. int nbank;
  592. u32 rate, format;
  593. ypcm->period_size = runtime->period_size;
  594. ypcm->buffer_size = runtime->buffer_size;
  595. ypcm->period_pos = 0;
  596. ypcm->last_pos = 0;
  597. ypcm->shift = 0;
  598. rate = ((48000 * 4096) / runtime->rate) - 1;
  599. format = 0;
  600. if (runtime->channels == 2) {
  601. format |= 2;
  602. ypcm->shift++;
  603. }
  604. if (snd_pcm_format_width(runtime->format) == 8)
  605. format |= 1;
  606. else
  607. ypcm->shift++;
  608. switch (ypcm->capture_bank_number) {
  609. case 0:
  610. snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
  611. snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
  612. break;
  613. case 1:
  614. snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
  615. snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
  616. break;
  617. }
  618. for (nbank = 0; nbank < 2; nbank++) {
  619. bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
  620. bank->base = cpu_to_le32(runtime->dma_addr);
  621. bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
  622. bank->start = 0;
  623. bank->num_of_loops = 0;
  624. }
  625. return 0;
  626. }
  627. static snd_pcm_uframes_t snd_ymfpci_playback_pointer(snd_pcm_substream_t * substream)
  628. {
  629. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  630. snd_pcm_runtime_t *runtime = substream->runtime;
  631. ymfpci_pcm_t *ypcm = runtime->private_data;
  632. ymfpci_voice_t *voice = ypcm->voices[0];
  633. if (!(ypcm->running && voice))
  634. return 0;
  635. return le32_to_cpu(voice->bank[chip->active_bank].start);
  636. }
  637. static snd_pcm_uframes_t snd_ymfpci_capture_pointer(snd_pcm_substream_t * substream)
  638. {
  639. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  640. snd_pcm_runtime_t *runtime = substream->runtime;
  641. ymfpci_pcm_t *ypcm = runtime->private_data;
  642. if (!ypcm->running)
  643. return 0;
  644. return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
  645. }
  646. static void snd_ymfpci_irq_wait(ymfpci_t *chip)
  647. {
  648. wait_queue_t wait;
  649. int loops = 4;
  650. while (loops-- > 0) {
  651. if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
  652. continue;
  653. init_waitqueue_entry(&wait, current);
  654. add_wait_queue(&chip->interrupt_sleep, &wait);
  655. atomic_inc(&chip->interrupt_sleep_count);
  656. set_current_state(TASK_UNINTERRUPTIBLE);
  657. schedule_timeout(HZ/20);
  658. remove_wait_queue(&chip->interrupt_sleep, &wait);
  659. }
  660. }
  661. static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  662. {
  663. ymfpci_t *chip = dev_id;
  664. u32 status, nvoice, mode;
  665. ymfpci_voice_t *voice;
  666. status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
  667. if (status & 0x80000000) {
  668. chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
  669. spin_lock(&chip->voice_lock);
  670. for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
  671. voice = &chip->voices[nvoice];
  672. if (voice->interrupt)
  673. voice->interrupt(chip, voice);
  674. }
  675. for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
  676. if (chip->capture_substream[nvoice])
  677. snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
  678. }
  679. #if 0
  680. for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
  681. if (chip->effect_substream[nvoice])
  682. snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
  683. }
  684. #endif
  685. spin_unlock(&chip->voice_lock);
  686. spin_lock(&chip->reg_lock);
  687. snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
  688. mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
  689. snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
  690. spin_unlock(&chip->reg_lock);
  691. if (atomic_read(&chip->interrupt_sleep_count)) {
  692. atomic_set(&chip->interrupt_sleep_count, 0);
  693. wake_up(&chip->interrupt_sleep);
  694. }
  695. }
  696. status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
  697. if (status & 1) {
  698. if (chip->timer)
  699. snd_timer_interrupt(chip->timer, chip->timer->sticks);
  700. }
  701. snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
  702. if (chip->rawmidi)
  703. snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data, regs);
  704. return IRQ_HANDLED;
  705. }
  706. static snd_pcm_hardware_t snd_ymfpci_playback =
  707. {
  708. .info = (SNDRV_PCM_INFO_MMAP |
  709. SNDRV_PCM_INFO_MMAP_VALID |
  710. SNDRV_PCM_INFO_INTERLEAVED |
  711. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  712. SNDRV_PCM_INFO_PAUSE |
  713. SNDRV_PCM_INFO_RESUME),
  714. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  715. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  716. .rate_min = 8000,
  717. .rate_max = 48000,
  718. .channels_min = 1,
  719. .channels_max = 2,
  720. .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
  721. .period_bytes_min = 64,
  722. .period_bytes_max = 256 * 1024, /* FIXME: enough? */
  723. .periods_min = 3,
  724. .periods_max = 1024,
  725. .fifo_size = 0,
  726. };
  727. static snd_pcm_hardware_t snd_ymfpci_capture =
  728. {
  729. .info = (SNDRV_PCM_INFO_MMAP |
  730. SNDRV_PCM_INFO_MMAP_VALID |
  731. SNDRV_PCM_INFO_INTERLEAVED |
  732. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  733. SNDRV_PCM_INFO_PAUSE |
  734. SNDRV_PCM_INFO_RESUME),
  735. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  736. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  737. .rate_min = 8000,
  738. .rate_max = 48000,
  739. .channels_min = 1,
  740. .channels_max = 2,
  741. .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
  742. .period_bytes_min = 64,
  743. .period_bytes_max = 256 * 1024, /* FIXME: enough? */
  744. .periods_min = 3,
  745. .periods_max = 1024,
  746. .fifo_size = 0,
  747. };
  748. static void snd_ymfpci_pcm_free_substream(snd_pcm_runtime_t *runtime)
  749. {
  750. ymfpci_pcm_t *ypcm = runtime->private_data;
  751. kfree(ypcm);
  752. }
  753. static int snd_ymfpci_playback_open_1(snd_pcm_substream_t * substream)
  754. {
  755. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  756. snd_pcm_runtime_t *runtime = substream->runtime;
  757. ymfpci_pcm_t *ypcm;
  758. ypcm = kcalloc(1, sizeof(*ypcm), GFP_KERNEL);
  759. if (ypcm == NULL)
  760. return -ENOMEM;
  761. ypcm->chip = chip;
  762. ypcm->type = PLAYBACK_VOICE;
  763. ypcm->substream = substream;
  764. runtime->hw = snd_ymfpci_playback;
  765. runtime->private_data = ypcm;
  766. runtime->private_free = snd_ymfpci_pcm_free_substream;
  767. /* FIXME? True value is 256/48 = 5.33333 ms */
  768. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
  769. return 0;
  770. }
  771. /* call with spinlock held */
  772. static void ymfpci_open_extension(ymfpci_t *chip)
  773. {
  774. if (! chip->rear_opened) {
  775. if (! chip->spdif_opened) /* set AC3 */
  776. snd_ymfpci_writel(chip, YDSXGR_MODE,
  777. snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
  778. /* enable second codec (4CHEN) */
  779. snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
  780. (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
  781. }
  782. }
  783. /* call with spinlock held */
  784. static void ymfpci_close_extension(ymfpci_t *chip)
  785. {
  786. if (! chip->rear_opened) {
  787. if (! chip->spdif_opened)
  788. snd_ymfpci_writel(chip, YDSXGR_MODE,
  789. snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
  790. snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
  791. (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
  792. }
  793. }
  794. static int snd_ymfpci_playback_open(snd_pcm_substream_t * substream)
  795. {
  796. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  797. snd_pcm_runtime_t *runtime = substream->runtime;
  798. ymfpci_pcm_t *ypcm;
  799. int err;
  800. if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
  801. return err;
  802. ypcm = runtime->private_data;
  803. ypcm->output_front = 1;
  804. ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
  805. spin_lock_irq(&chip->reg_lock);
  806. if (ypcm->output_rear) {
  807. ymfpci_open_extension(chip);
  808. chip->rear_opened++;
  809. }
  810. spin_unlock_irq(&chip->reg_lock);
  811. return 0;
  812. }
  813. static int snd_ymfpci_playback_spdif_open(snd_pcm_substream_t * substream)
  814. {
  815. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  816. snd_pcm_runtime_t *runtime = substream->runtime;
  817. ymfpci_pcm_t *ypcm;
  818. int err;
  819. if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
  820. return err;
  821. ypcm = runtime->private_data;
  822. ypcm->output_front = 0;
  823. ypcm->output_rear = 1;
  824. spin_lock_irq(&chip->reg_lock);
  825. snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
  826. snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
  827. ymfpci_open_extension(chip);
  828. chip->spdif_pcm_bits = chip->spdif_bits;
  829. snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
  830. chip->spdif_opened++;
  831. spin_unlock_irq(&chip->reg_lock);
  832. chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  833. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
  834. SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
  835. return 0;
  836. }
  837. static int snd_ymfpci_playback_4ch_open(snd_pcm_substream_t * substream)
  838. {
  839. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  840. snd_pcm_runtime_t *runtime = substream->runtime;
  841. ymfpci_pcm_t *ypcm;
  842. int err;
  843. if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
  844. return err;
  845. ypcm = runtime->private_data;
  846. ypcm->output_front = 0;
  847. ypcm->output_rear = 1;
  848. spin_lock_irq(&chip->reg_lock);
  849. ymfpci_open_extension(chip);
  850. chip->rear_opened++;
  851. spin_unlock_irq(&chip->reg_lock);
  852. return 0;
  853. }
  854. static int snd_ymfpci_capture_open(snd_pcm_substream_t * substream,
  855. u32 capture_bank_number)
  856. {
  857. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  858. snd_pcm_runtime_t *runtime = substream->runtime;
  859. ymfpci_pcm_t *ypcm;
  860. ypcm = kcalloc(1, sizeof(*ypcm), GFP_KERNEL);
  861. if (ypcm == NULL)
  862. return -ENOMEM;
  863. ypcm->chip = chip;
  864. ypcm->type = capture_bank_number + CAPTURE_REC;
  865. ypcm->substream = substream;
  866. ypcm->capture_bank_number = capture_bank_number;
  867. chip->capture_substream[capture_bank_number] = substream;
  868. runtime->hw = snd_ymfpci_capture;
  869. /* FIXME? True value is 256/48 = 5.33333 ms */
  870. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
  871. runtime->private_data = ypcm;
  872. runtime->private_free = snd_ymfpci_pcm_free_substream;
  873. snd_ymfpci_hw_start(chip);
  874. return 0;
  875. }
  876. static int snd_ymfpci_capture_rec_open(snd_pcm_substream_t * substream)
  877. {
  878. return snd_ymfpci_capture_open(substream, 0);
  879. }
  880. static int snd_ymfpci_capture_ac97_open(snd_pcm_substream_t * substream)
  881. {
  882. return snd_ymfpci_capture_open(substream, 1);
  883. }
  884. static int snd_ymfpci_playback_close_1(snd_pcm_substream_t * substream)
  885. {
  886. return 0;
  887. }
  888. static int snd_ymfpci_playback_close(snd_pcm_substream_t * substream)
  889. {
  890. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  891. ymfpci_pcm_t *ypcm = substream->runtime->private_data;
  892. spin_lock_irq(&chip->reg_lock);
  893. if (ypcm->output_rear && chip->rear_opened > 0) {
  894. chip->rear_opened--;
  895. ymfpci_close_extension(chip);
  896. }
  897. spin_unlock_irq(&chip->reg_lock);
  898. return snd_ymfpci_playback_close_1(substream);
  899. }
  900. static int snd_ymfpci_playback_spdif_close(snd_pcm_substream_t * substream)
  901. {
  902. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  903. spin_lock_irq(&chip->reg_lock);
  904. chip->spdif_opened = 0;
  905. ymfpci_close_extension(chip);
  906. snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
  907. snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
  908. snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
  909. spin_unlock_irq(&chip->reg_lock);
  910. chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  911. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
  912. SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
  913. return snd_ymfpci_playback_close_1(substream);
  914. }
  915. static int snd_ymfpci_playback_4ch_close(snd_pcm_substream_t * substream)
  916. {
  917. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  918. spin_lock_irq(&chip->reg_lock);
  919. if (chip->rear_opened > 0) {
  920. chip->rear_opened--;
  921. ymfpci_close_extension(chip);
  922. }
  923. spin_unlock_irq(&chip->reg_lock);
  924. return snd_ymfpci_playback_close_1(substream);
  925. }
  926. static int snd_ymfpci_capture_close(snd_pcm_substream_t * substream)
  927. {
  928. ymfpci_t *chip = snd_pcm_substream_chip(substream);
  929. snd_pcm_runtime_t *runtime = substream->runtime;
  930. ymfpci_pcm_t *ypcm = runtime->private_data;
  931. if (ypcm != NULL) {
  932. chip->capture_substream[ypcm->capture_bank_number] = NULL;
  933. snd_ymfpci_hw_stop(chip);
  934. }
  935. return 0;
  936. }
  937. static snd_pcm_ops_t snd_ymfpci_playback_ops = {
  938. .open = snd_ymfpci_playback_open,
  939. .close = snd_ymfpci_playback_close,
  940. .ioctl = snd_pcm_lib_ioctl,
  941. .hw_params = snd_ymfpci_playback_hw_params,
  942. .hw_free = snd_ymfpci_playback_hw_free,
  943. .prepare = snd_ymfpci_playback_prepare,
  944. .trigger = snd_ymfpci_playback_trigger,
  945. .pointer = snd_ymfpci_playback_pointer,
  946. };
  947. static snd_pcm_ops_t snd_ymfpci_capture_rec_ops = {
  948. .open = snd_ymfpci_capture_rec_open,
  949. .close = snd_ymfpci_capture_close,
  950. .ioctl = snd_pcm_lib_ioctl,
  951. .hw_params = snd_ymfpci_capture_hw_params,
  952. .hw_free = snd_ymfpci_capture_hw_free,
  953. .prepare = snd_ymfpci_capture_prepare,
  954. .trigger = snd_ymfpci_capture_trigger,
  955. .pointer = snd_ymfpci_capture_pointer,
  956. };
  957. static void snd_ymfpci_pcm_free(snd_pcm_t *pcm)
  958. {
  959. ymfpci_t *chip = pcm->private_data;
  960. chip->pcm = NULL;
  961. snd_pcm_lib_preallocate_free_for_all(pcm);
  962. }
  963. int __devinit snd_ymfpci_pcm(ymfpci_t *chip, int device, snd_pcm_t ** rpcm)
  964. {
  965. snd_pcm_t *pcm;
  966. int err;
  967. if (rpcm)
  968. *rpcm = NULL;
  969. if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
  970. return err;
  971. pcm->private_data = chip;
  972. pcm->private_free = snd_ymfpci_pcm_free;
  973. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
  974. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
  975. /* global setup */
  976. pcm->info_flags = 0;
  977. strcpy(pcm->name, "YMFPCI");
  978. chip->pcm = pcm;
  979. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  980. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  981. if (rpcm)
  982. *rpcm = pcm;
  983. return 0;
  984. }
  985. static snd_pcm_ops_t snd_ymfpci_capture_ac97_ops = {
  986. .open = snd_ymfpci_capture_ac97_open,
  987. .close = snd_ymfpci_capture_close,
  988. .ioctl = snd_pcm_lib_ioctl,
  989. .hw_params = snd_ymfpci_capture_hw_params,
  990. .hw_free = snd_ymfpci_capture_hw_free,
  991. .prepare = snd_ymfpci_capture_prepare,
  992. .trigger = snd_ymfpci_capture_trigger,
  993. .pointer = snd_ymfpci_capture_pointer,
  994. };
  995. static void snd_ymfpci_pcm2_free(snd_pcm_t *pcm)
  996. {
  997. ymfpci_t *chip = pcm->private_data;
  998. chip->pcm2 = NULL;
  999. snd_pcm_lib_preallocate_free_for_all(pcm);
  1000. }
  1001. int __devinit snd_ymfpci_pcm2(ymfpci_t *chip, int device, snd_pcm_t ** rpcm)
  1002. {
  1003. snd_pcm_t *pcm;
  1004. int err;
  1005. if (rpcm)
  1006. *rpcm = NULL;
  1007. if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
  1008. return err;
  1009. pcm->private_data = chip;
  1010. pcm->private_free = snd_ymfpci_pcm2_free;
  1011. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
  1012. /* global setup */
  1013. pcm->info_flags = 0;
  1014. sprintf(pcm->name, "YMFPCI - %s",
  1015. chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
  1016. chip->pcm2 = pcm;
  1017. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1018. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1019. if (rpcm)
  1020. *rpcm = pcm;
  1021. return 0;
  1022. }
  1023. static snd_pcm_ops_t snd_ymfpci_playback_spdif_ops = {
  1024. .open = snd_ymfpci_playback_spdif_open,
  1025. .close = snd_ymfpci_playback_spdif_close,
  1026. .ioctl = snd_pcm_lib_ioctl,
  1027. .hw_params = snd_ymfpci_playback_hw_params,
  1028. .hw_free = snd_ymfpci_playback_hw_free,
  1029. .prepare = snd_ymfpci_playback_prepare,
  1030. .trigger = snd_ymfpci_playback_trigger,
  1031. .pointer = snd_ymfpci_playback_pointer,
  1032. };
  1033. static void snd_ymfpci_pcm_spdif_free(snd_pcm_t *pcm)
  1034. {
  1035. ymfpci_t *chip = pcm->private_data;
  1036. chip->pcm_spdif = NULL;
  1037. snd_pcm_lib_preallocate_free_for_all(pcm);
  1038. }
  1039. int __devinit snd_ymfpci_pcm_spdif(ymfpci_t *chip, int device, snd_pcm_t ** rpcm)
  1040. {
  1041. snd_pcm_t *pcm;
  1042. int err;
  1043. if (rpcm)
  1044. *rpcm = NULL;
  1045. if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
  1046. return err;
  1047. pcm->private_data = chip;
  1048. pcm->private_free = snd_ymfpci_pcm_spdif_free;
  1049. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
  1050. /* global setup */
  1051. pcm->info_flags = 0;
  1052. strcpy(pcm->name, "YMFPCI - IEC958");
  1053. chip->pcm_spdif = pcm;
  1054. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1055. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1056. if (rpcm)
  1057. *rpcm = pcm;
  1058. return 0;
  1059. }
  1060. static snd_pcm_ops_t snd_ymfpci_playback_4ch_ops = {
  1061. .open = snd_ymfpci_playback_4ch_open,
  1062. .close = snd_ymfpci_playback_4ch_close,
  1063. .ioctl = snd_pcm_lib_ioctl,
  1064. .hw_params = snd_ymfpci_playback_hw_params,
  1065. .hw_free = snd_ymfpci_playback_hw_free,
  1066. .prepare = snd_ymfpci_playback_prepare,
  1067. .trigger = snd_ymfpci_playback_trigger,
  1068. .pointer = snd_ymfpci_playback_pointer,
  1069. };
  1070. static void snd_ymfpci_pcm_4ch_free(snd_pcm_t *pcm)
  1071. {
  1072. ymfpci_t *chip = pcm->private_data;
  1073. chip->pcm_4ch = NULL;
  1074. snd_pcm_lib_preallocate_free_for_all(pcm);
  1075. }
  1076. int __devinit snd_ymfpci_pcm_4ch(ymfpci_t *chip, int device, snd_pcm_t ** rpcm)
  1077. {
  1078. snd_pcm_t *pcm;
  1079. int err;
  1080. if (rpcm)
  1081. *rpcm = NULL;
  1082. if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
  1083. return err;
  1084. pcm->private_data = chip;
  1085. pcm->private_free = snd_ymfpci_pcm_4ch_free;
  1086. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
  1087. /* global setup */
  1088. pcm->info_flags = 0;
  1089. strcpy(pcm->name, "YMFPCI - Rear PCM");
  1090. chip->pcm_4ch = pcm;
  1091. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1092. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1093. if (rpcm)
  1094. *rpcm = pcm;
  1095. return 0;
  1096. }
  1097. static int snd_ymfpci_spdif_default_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1098. {
  1099. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1100. uinfo->count = 1;
  1101. return 0;
  1102. }
  1103. static int snd_ymfpci_spdif_default_get(snd_kcontrol_t * kcontrol,
  1104. snd_ctl_elem_value_t * ucontrol)
  1105. {
  1106. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1107. spin_lock_irq(&chip->reg_lock);
  1108. ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
  1109. ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
  1110. spin_unlock_irq(&chip->reg_lock);
  1111. return 0;
  1112. }
  1113. static int snd_ymfpci_spdif_default_put(snd_kcontrol_t * kcontrol,
  1114. snd_ctl_elem_value_t * ucontrol)
  1115. {
  1116. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1117. unsigned int val;
  1118. int change;
  1119. val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
  1120. (ucontrol->value.iec958.status[1] << 8);
  1121. spin_lock_irq(&chip->reg_lock);
  1122. change = chip->spdif_bits != val;
  1123. chip->spdif_bits = val;
  1124. if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
  1125. snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
  1126. spin_unlock_irq(&chip->reg_lock);
  1127. return change;
  1128. }
  1129. static snd_kcontrol_new_t snd_ymfpci_spdif_default __devinitdata =
  1130. {
  1131. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1132. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1133. .info = snd_ymfpci_spdif_default_info,
  1134. .get = snd_ymfpci_spdif_default_get,
  1135. .put = snd_ymfpci_spdif_default_put
  1136. };
  1137. static int snd_ymfpci_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1138. {
  1139. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1140. uinfo->count = 1;
  1141. return 0;
  1142. }
  1143. static int snd_ymfpci_spdif_mask_get(snd_kcontrol_t * kcontrol,
  1144. snd_ctl_elem_value_t * ucontrol)
  1145. {
  1146. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1147. spin_lock_irq(&chip->reg_lock);
  1148. ucontrol->value.iec958.status[0] = 0x3e;
  1149. ucontrol->value.iec958.status[1] = 0xff;
  1150. spin_unlock_irq(&chip->reg_lock);
  1151. return 0;
  1152. }
  1153. static snd_kcontrol_new_t snd_ymfpci_spdif_mask __devinitdata =
  1154. {
  1155. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1156. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1157. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  1158. .info = snd_ymfpci_spdif_mask_info,
  1159. .get = snd_ymfpci_spdif_mask_get,
  1160. };
  1161. static int snd_ymfpci_spdif_stream_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1162. {
  1163. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1164. uinfo->count = 1;
  1165. return 0;
  1166. }
  1167. static int snd_ymfpci_spdif_stream_get(snd_kcontrol_t * kcontrol,
  1168. snd_ctl_elem_value_t * ucontrol)
  1169. {
  1170. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1171. spin_lock_irq(&chip->reg_lock);
  1172. ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
  1173. ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
  1174. spin_unlock_irq(&chip->reg_lock);
  1175. return 0;
  1176. }
  1177. static int snd_ymfpci_spdif_stream_put(snd_kcontrol_t * kcontrol,
  1178. snd_ctl_elem_value_t * ucontrol)
  1179. {
  1180. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1181. unsigned int val;
  1182. int change;
  1183. val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
  1184. (ucontrol->value.iec958.status[1] << 8);
  1185. spin_lock_irq(&chip->reg_lock);
  1186. change = chip->spdif_pcm_bits != val;
  1187. chip->spdif_pcm_bits = val;
  1188. if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
  1189. snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
  1190. spin_unlock_irq(&chip->reg_lock);
  1191. return change;
  1192. }
  1193. static snd_kcontrol_new_t snd_ymfpci_spdif_stream __devinitdata =
  1194. {
  1195. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  1196. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1197. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  1198. .info = snd_ymfpci_spdif_stream_info,
  1199. .get = snd_ymfpci_spdif_stream_get,
  1200. .put = snd_ymfpci_spdif_stream_put
  1201. };
  1202. static int snd_ymfpci_drec_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *info)
  1203. {
  1204. static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
  1205. info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1206. info->count = 1;
  1207. info->value.enumerated.items = 3;
  1208. if (info->value.enumerated.item > 2)
  1209. info->value.enumerated.item = 2;
  1210. strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
  1211. return 0;
  1212. }
  1213. static int snd_ymfpci_drec_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
  1214. {
  1215. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1216. u16 reg;
  1217. spin_lock_irq(&chip->reg_lock);
  1218. reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
  1219. spin_unlock_irq(&chip->reg_lock);
  1220. if (!(reg & 0x100))
  1221. value->value.enumerated.item[0] = 0;
  1222. else
  1223. value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
  1224. return 0;
  1225. }
  1226. static int snd_ymfpci_drec_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
  1227. {
  1228. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1229. u16 reg, old_reg;
  1230. spin_lock_irq(&chip->reg_lock);
  1231. old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
  1232. if (value->value.enumerated.item[0] == 0)
  1233. reg = old_reg & ~0x100;
  1234. else
  1235. reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
  1236. snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
  1237. spin_unlock_irq(&chip->reg_lock);
  1238. return reg != old_reg;
  1239. }
  1240. static snd_kcontrol_new_t snd_ymfpci_drec_source __devinitdata = {
  1241. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1242. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1243. .name = "Direct Recording Source",
  1244. .info = snd_ymfpci_drec_source_info,
  1245. .get = snd_ymfpci_drec_source_get,
  1246. .put = snd_ymfpci_drec_source_put
  1247. };
  1248. /*
  1249. * Mixer controls
  1250. */
  1251. #define YMFPCI_SINGLE(xname, xindex, reg) \
  1252. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1253. .info = snd_ymfpci_info_single, \
  1254. .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
  1255. .private_value = reg }
  1256. static int snd_ymfpci_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1257. {
  1258. unsigned int mask = 1;
  1259. switch (kcontrol->private_value) {
  1260. case YDSXGR_SPDIFOUTCTRL: break;
  1261. case YDSXGR_SPDIFINCTRL: break;
  1262. default: return -EINVAL;
  1263. }
  1264. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1265. uinfo->count = 1;
  1266. uinfo->value.integer.min = 0;
  1267. uinfo->value.integer.max = mask;
  1268. return 0;
  1269. }
  1270. static int snd_ymfpci_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1271. {
  1272. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1273. int reg = kcontrol->private_value;
  1274. unsigned int shift = 0, mask = 1, invert = 0;
  1275. switch (kcontrol->private_value) {
  1276. case YDSXGR_SPDIFOUTCTRL: break;
  1277. case YDSXGR_SPDIFINCTRL: break;
  1278. default: return -EINVAL;
  1279. }
  1280. ucontrol->value.integer.value[0] = (snd_ymfpci_readl(chip, reg) >> shift) & mask;
  1281. if (invert)
  1282. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1283. return 0;
  1284. }
  1285. static int snd_ymfpci_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1286. {
  1287. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1288. int reg = kcontrol->private_value;
  1289. unsigned int shift = 0, mask = 1, invert = 0;
  1290. int change;
  1291. unsigned int val, oval;
  1292. switch (kcontrol->private_value) {
  1293. case YDSXGR_SPDIFOUTCTRL: break;
  1294. case YDSXGR_SPDIFINCTRL: break;
  1295. default: return -EINVAL;
  1296. }
  1297. val = (ucontrol->value.integer.value[0] & mask);
  1298. if (invert)
  1299. val = mask - val;
  1300. val <<= shift;
  1301. spin_lock_irq(&chip->reg_lock);
  1302. oval = snd_ymfpci_readl(chip, reg);
  1303. val = (oval & ~(mask << shift)) | val;
  1304. change = val != oval;
  1305. snd_ymfpci_writel(chip, reg, val);
  1306. spin_unlock_irq(&chip->reg_lock);
  1307. return change;
  1308. }
  1309. #define YMFPCI_DOUBLE(xname, xindex, reg) \
  1310. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1311. .info = snd_ymfpci_info_double, \
  1312. .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
  1313. .private_value = reg }
  1314. static int snd_ymfpci_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1315. {
  1316. unsigned int reg = kcontrol->private_value;
  1317. unsigned int mask = 16383;
  1318. if (reg < 0x80 || reg >= 0xc0)
  1319. return -EINVAL;
  1320. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1321. uinfo->count = 2;
  1322. uinfo->value.integer.min = 0;
  1323. uinfo->value.integer.max = mask;
  1324. return 0;
  1325. }
  1326. static int snd_ymfpci_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1327. {
  1328. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1329. unsigned int reg = kcontrol->private_value;
  1330. unsigned int shift_left = 0, shift_right = 16, mask = 16383, invert = 0;
  1331. unsigned int val;
  1332. if (reg < 0x80 || reg >= 0xc0)
  1333. return -EINVAL;
  1334. spin_lock_irq(&chip->reg_lock);
  1335. val = snd_ymfpci_readl(chip, reg);
  1336. spin_unlock_irq(&chip->reg_lock);
  1337. ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
  1338. ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
  1339. if (invert) {
  1340. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1341. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1342. }
  1343. return 0;
  1344. }
  1345. static int snd_ymfpci_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1346. {
  1347. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1348. unsigned int reg = kcontrol->private_value;
  1349. unsigned int shift_left = 0, shift_right = 16, mask = 16383, invert = 0;
  1350. int change;
  1351. unsigned int val1, val2, oval;
  1352. if (reg < 0x80 || reg >= 0xc0)
  1353. return -EINVAL;
  1354. val1 = ucontrol->value.integer.value[0] & mask;
  1355. val2 = ucontrol->value.integer.value[1] & mask;
  1356. if (invert) {
  1357. val1 = mask - val1;
  1358. val2 = mask - val2;
  1359. }
  1360. val1 <<= shift_left;
  1361. val2 <<= shift_right;
  1362. spin_lock_irq(&chip->reg_lock);
  1363. oval = snd_ymfpci_readl(chip, reg);
  1364. val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
  1365. change = val1 != oval;
  1366. snd_ymfpci_writel(chip, reg, val1);
  1367. spin_unlock_irq(&chip->reg_lock);
  1368. return change;
  1369. }
  1370. /*
  1371. * 4ch duplication
  1372. */
  1373. static int snd_ymfpci_info_dup4ch(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1374. {
  1375. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1376. uinfo->count = 1;
  1377. uinfo->value.integer.min = 0;
  1378. uinfo->value.integer.max = 1;
  1379. return 0;
  1380. }
  1381. static int snd_ymfpci_get_dup4ch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1382. {
  1383. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1384. ucontrol->value.integer.value[0] = chip->mode_dup4ch;
  1385. return 0;
  1386. }
  1387. static int snd_ymfpci_put_dup4ch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1388. {
  1389. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1390. int change;
  1391. change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
  1392. if (change)
  1393. chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
  1394. return change;
  1395. }
  1396. static snd_kcontrol_new_t snd_ymfpci_controls[] __devinitdata = {
  1397. YMFPCI_DOUBLE("Wave Playback Volume", 0, YDSXGR_NATIVEDACOUTVOL),
  1398. YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
  1399. YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
  1400. YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
  1401. YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
  1402. YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
  1403. YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
  1404. YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
  1405. YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
  1406. YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
  1407. YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
  1408. YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
  1409. YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
  1410. YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL),
  1411. YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL),
  1412. {
  1413. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1414. .name = "4ch Duplication",
  1415. .info = snd_ymfpci_info_dup4ch,
  1416. .get = snd_ymfpci_get_dup4ch,
  1417. .put = snd_ymfpci_put_dup4ch,
  1418. },
  1419. };
  1420. /*
  1421. * GPIO
  1422. */
  1423. static int snd_ymfpci_get_gpio_out(ymfpci_t *chip, int pin)
  1424. {
  1425. u16 reg, mode;
  1426. unsigned long flags;
  1427. spin_lock_irqsave(&chip->reg_lock, flags);
  1428. reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
  1429. reg &= ~(1 << (pin + 8));
  1430. reg |= (1 << pin);
  1431. snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
  1432. /* set the level mode for input line */
  1433. mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
  1434. mode &= ~(3 << (pin * 2));
  1435. snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
  1436. snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
  1437. mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
  1438. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1439. return (mode >> pin) & 1;
  1440. }
  1441. static int snd_ymfpci_set_gpio_out(ymfpci_t *chip, int pin, int enable)
  1442. {
  1443. u16 reg;
  1444. unsigned long flags;
  1445. spin_lock_irqsave(&chip->reg_lock, flags);
  1446. reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
  1447. reg &= ~(1 << pin);
  1448. reg &= ~(1 << (pin + 8));
  1449. snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
  1450. snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
  1451. snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
  1452. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1453. return 0;
  1454. }
  1455. static int snd_ymfpci_gpio_sw_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1456. {
  1457. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1458. uinfo->count = 1;
  1459. uinfo->value.integer.min = 0;
  1460. uinfo->value.integer.max = 1;
  1461. return 0;
  1462. }
  1463. static int snd_ymfpci_gpio_sw_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1464. {
  1465. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1466. int pin = (int)kcontrol->private_value;
  1467. ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
  1468. return 0;
  1469. }
  1470. static int snd_ymfpci_gpio_sw_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1471. {
  1472. ymfpci_t *chip = snd_kcontrol_chip(kcontrol);
  1473. int pin = (int)kcontrol->private_value;
  1474. if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
  1475. snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
  1476. ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
  1477. return 1;
  1478. }
  1479. return 0;
  1480. }
  1481. static snd_kcontrol_new_t snd_ymfpci_rear_shared __devinitdata = {
  1482. .name = "Shared Rear/Line-In Switch",
  1483. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1484. .info = snd_ymfpci_gpio_sw_info,
  1485. .get = snd_ymfpci_gpio_sw_get,
  1486. .put = snd_ymfpci_gpio_sw_put,
  1487. .private_value = 2,
  1488. };
  1489. /*
  1490. * Mixer routines
  1491. */
  1492. static void snd_ymfpci_mixer_free_ac97_bus(ac97_bus_t *bus)
  1493. {
  1494. ymfpci_t *chip = bus->private_data;
  1495. chip->ac97_bus = NULL;
  1496. }
  1497. static void snd_ymfpci_mixer_free_ac97(ac97_t *ac97)
  1498. {
  1499. ymfpci_t *chip = ac97->private_data;
  1500. chip->ac97 = NULL;
  1501. }
  1502. int __devinit snd_ymfpci_mixer(ymfpci_t *chip, int rear_switch)
  1503. {
  1504. ac97_template_t ac97;
  1505. snd_kcontrol_t *kctl;
  1506. unsigned int idx;
  1507. int err;
  1508. static ac97_bus_ops_t ops = {
  1509. .write = snd_ymfpci_codec_write,
  1510. .read = snd_ymfpci_codec_read,
  1511. };
  1512. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  1513. return err;
  1514. chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
  1515. chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
  1516. memset(&ac97, 0, sizeof(ac97));
  1517. ac97.private_data = chip;
  1518. ac97.private_free = snd_ymfpci_mixer_free_ac97;
  1519. if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
  1520. return err;
  1521. /* to be sure */
  1522. snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
  1523. AC97_EA_VRA|AC97_EA_VRM, 0);
  1524. for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
  1525. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
  1526. return err;
  1527. }
  1528. /* add S/PDIF control */
  1529. snd_assert(chip->pcm_spdif != NULL, return -EIO);
  1530. if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
  1531. return err;
  1532. kctl->id.device = chip->pcm_spdif->device;
  1533. if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
  1534. return err;
  1535. kctl->id.device = chip->pcm_spdif->device;
  1536. if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
  1537. return err;
  1538. kctl->id.device = chip->pcm_spdif->device;
  1539. chip->spdif_pcm_ctl = kctl;
  1540. /* direct recording source */
  1541. if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
  1542. (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
  1543. return err;
  1544. /*
  1545. * shared rear/line-in
  1546. */
  1547. if (rear_switch) {
  1548. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
  1549. return err;
  1550. }
  1551. return 0;
  1552. }
  1553. /*
  1554. * timer
  1555. */
  1556. static int snd_ymfpci_timer_start(snd_timer_t *timer)
  1557. {
  1558. ymfpci_t *chip;
  1559. unsigned long flags;
  1560. unsigned int count;
  1561. chip = snd_timer_chip(timer);
  1562. count = timer->sticks - 1;
  1563. if (count == 0) /* minimum time is 20.8 us */
  1564. count = 1;
  1565. spin_lock_irqsave(&chip->reg_lock, flags);
  1566. snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
  1567. snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
  1568. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1569. return 0;
  1570. }
  1571. static int snd_ymfpci_timer_stop(snd_timer_t *timer)
  1572. {
  1573. ymfpci_t *chip;
  1574. unsigned long flags;
  1575. chip = snd_timer_chip(timer);
  1576. spin_lock_irqsave(&chip->reg_lock, flags);
  1577. snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
  1578. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1579. return 0;
  1580. }
  1581. static int snd_ymfpci_timer_precise_resolution(snd_timer_t *timer,
  1582. unsigned long *num, unsigned long *den)
  1583. {
  1584. *num = 1;
  1585. *den = 96000;
  1586. return 0;
  1587. }
  1588. static struct _snd_timer_hardware snd_ymfpci_timer_hw = {
  1589. .flags = SNDRV_TIMER_HW_AUTO,
  1590. .resolution = 10417, /* 1/2fs = 10.41666...us */
  1591. .ticks = 65536,
  1592. .start = snd_ymfpci_timer_start,
  1593. .stop = snd_ymfpci_timer_stop,
  1594. .precise_resolution = snd_ymfpci_timer_precise_resolution,
  1595. };
  1596. int __devinit snd_ymfpci_timer(ymfpci_t *chip, int device)
  1597. {
  1598. snd_timer_t *timer = NULL;
  1599. snd_timer_id_t tid;
  1600. int err;
  1601. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  1602. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1603. tid.card = chip->card->number;
  1604. tid.device = device;
  1605. tid.subdevice = 0;
  1606. if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
  1607. strcpy(timer->name, "YMFPCI timer");
  1608. timer->private_data = chip;
  1609. timer->hw = snd_ymfpci_timer_hw;
  1610. }
  1611. chip->timer = timer;
  1612. return err;
  1613. }
  1614. /*
  1615. * proc interface
  1616. */
  1617. static void snd_ymfpci_proc_read(snd_info_entry_t *entry,
  1618. snd_info_buffer_t * buffer)
  1619. {
  1620. ymfpci_t *chip = entry->private_data;
  1621. int i;
  1622. snd_iprintf(buffer, "YMFPCI\n\n");
  1623. for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
  1624. snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
  1625. }
  1626. static int __devinit snd_ymfpci_proc_init(snd_card_t * card, ymfpci_t *chip)
  1627. {
  1628. snd_info_entry_t *entry;
  1629. if (! snd_card_proc_new(card, "ymfpci", &entry))
  1630. snd_info_set_text_ops(entry, chip, 1024, snd_ymfpci_proc_read);
  1631. return 0;
  1632. }
  1633. /*
  1634. * initialization routines
  1635. */
  1636. static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
  1637. {
  1638. u8 cmd;
  1639. pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
  1640. #if 0 // force to reset
  1641. if (cmd & 0x03) {
  1642. #endif
  1643. pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
  1644. pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
  1645. pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
  1646. pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
  1647. pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
  1648. #if 0
  1649. }
  1650. #endif
  1651. }
  1652. static void snd_ymfpci_enable_dsp(ymfpci_t *chip)
  1653. {
  1654. snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
  1655. }
  1656. static void snd_ymfpci_disable_dsp(ymfpci_t *chip)
  1657. {
  1658. u32 val;
  1659. int timeout = 1000;
  1660. val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
  1661. if (val)
  1662. snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
  1663. while (timeout-- > 0) {
  1664. val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
  1665. if ((val & 0x00000002) == 0)
  1666. break;
  1667. }
  1668. }
  1669. #include "ymfpci_image.h"
  1670. static void snd_ymfpci_download_image(ymfpci_t *chip)
  1671. {
  1672. int i;
  1673. u16 ctrl;
  1674. unsigned long *inst;
  1675. snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
  1676. snd_ymfpci_disable_dsp(chip);
  1677. snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
  1678. snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
  1679. snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
  1680. snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
  1681. snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
  1682. snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
  1683. snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
  1684. ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
  1685. snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
  1686. /* setup DSP instruction code */
  1687. for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
  1688. snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), DspInst[i]);
  1689. /* setup control instruction code */
  1690. switch (chip->device_id) {
  1691. case PCI_DEVICE_ID_YAMAHA_724F:
  1692. case PCI_DEVICE_ID_YAMAHA_740C:
  1693. case PCI_DEVICE_ID_YAMAHA_744:
  1694. case PCI_DEVICE_ID_YAMAHA_754:
  1695. inst = CntrlInst1E;
  1696. break;
  1697. default:
  1698. inst = CntrlInst;
  1699. break;
  1700. }
  1701. for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
  1702. snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
  1703. snd_ymfpci_enable_dsp(chip);
  1704. }
  1705. static int __devinit snd_ymfpci_memalloc(ymfpci_t *chip)
  1706. {
  1707. long size, playback_ctrl_size;
  1708. int voice, bank, reg;
  1709. u8 *ptr;
  1710. dma_addr_t ptr_addr;
  1711. playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
  1712. chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
  1713. chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
  1714. chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
  1715. chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
  1716. size = ((playback_ctrl_size + 0x00ff) & ~0x00ff) +
  1717. ((chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES + 0x00ff) & ~0x00ff) +
  1718. ((chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES + 0x00ff) & ~0x00ff) +
  1719. ((chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES + 0x00ff) & ~0x00ff) +
  1720. chip->work_size;
  1721. /* work_ptr must be aligned to 256 bytes, but it's already
  1722. covered with the kernel page allocation mechanism */
  1723. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  1724. size, &chip->work_ptr) < 0)
  1725. return -ENOMEM;
  1726. ptr = chip->work_ptr.area;
  1727. ptr_addr = chip->work_ptr.addr;
  1728. memset(ptr, 0, size); /* for sure */
  1729. chip->bank_base_playback = ptr;
  1730. chip->bank_base_playback_addr = ptr_addr;
  1731. chip->ctrl_playback = (u32 *)ptr;
  1732. chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
  1733. ptr += (playback_ctrl_size + 0x00ff) & ~0x00ff;
  1734. ptr_addr += (playback_ctrl_size + 0x00ff) & ~0x00ff;
  1735. for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
  1736. chip->voices[voice].number = voice;
  1737. chip->voices[voice].bank = (snd_ymfpci_playback_bank_t *)ptr;
  1738. chip->voices[voice].bank_addr = ptr_addr;
  1739. for (bank = 0; bank < 2; bank++) {
  1740. chip->bank_playback[voice][bank] = (snd_ymfpci_playback_bank_t *)ptr;
  1741. ptr += chip->bank_size_playback;
  1742. ptr_addr += chip->bank_size_playback;
  1743. }
  1744. }
  1745. ptr = (char *)(((unsigned long)ptr + 0x00ff) & ~0x00ff);
  1746. ptr_addr = (ptr_addr + 0x00ff) & ~0x00ff;
  1747. chip->bank_base_capture = ptr;
  1748. chip->bank_base_capture_addr = ptr_addr;
  1749. for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
  1750. for (bank = 0; bank < 2; bank++) {
  1751. chip->bank_capture[voice][bank] = (snd_ymfpci_capture_bank_t *)ptr;
  1752. ptr += chip->bank_size_capture;
  1753. ptr_addr += chip->bank_size_capture;
  1754. }
  1755. ptr = (char *)(((unsigned long)ptr + 0x00ff) & ~0x00ff);
  1756. ptr_addr = (ptr_addr + 0x00ff) & ~0x00ff;
  1757. chip->bank_base_effect = ptr;
  1758. chip->bank_base_effect_addr = ptr_addr;
  1759. for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
  1760. for (bank = 0; bank < 2; bank++) {
  1761. chip->bank_effect[voice][bank] = (snd_ymfpci_effect_bank_t *)ptr;
  1762. ptr += chip->bank_size_effect;
  1763. ptr_addr += chip->bank_size_effect;
  1764. }
  1765. ptr = (char *)(((unsigned long)ptr + 0x00ff) & ~0x00ff);
  1766. ptr_addr = (ptr_addr + 0x00ff) & ~0x00ff;
  1767. chip->work_base = ptr;
  1768. chip->work_base_addr = ptr_addr;
  1769. snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
  1770. snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
  1771. snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
  1772. snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
  1773. snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
  1774. snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
  1775. /* S/PDIF output initialization */
  1776. chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
  1777. snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
  1778. snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
  1779. /* S/PDIF input initialization */
  1780. snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
  1781. /* digital mixer setup */
  1782. for (reg = 0x80; reg < 0xc0; reg += 4)
  1783. snd_ymfpci_writel(chip, reg, 0);
  1784. snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
  1785. snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
  1786. snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
  1787. snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
  1788. snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
  1789. snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
  1790. snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
  1791. return 0;
  1792. }
  1793. static int snd_ymfpci_free(ymfpci_t *chip)
  1794. {
  1795. u16 ctrl;
  1796. snd_assert(chip != NULL, return -EINVAL);
  1797. if (chip->res_reg_area) { /* don't touch busy hardware */
  1798. snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
  1799. snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
  1800. snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
  1801. snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
  1802. snd_ymfpci_disable_dsp(chip);
  1803. snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
  1804. snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
  1805. snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
  1806. snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
  1807. snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
  1808. ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
  1809. snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
  1810. }
  1811. snd_ymfpci_ac3_done(chip);
  1812. /* Set PCI device to D3 state */
  1813. #if 0
  1814. /* FIXME: temporarily disabled, otherwise we cannot fire up
  1815. * the chip again unless reboot. ACPI bug?
  1816. */
  1817. pci_set_power_state(chip->pci, 3);
  1818. #endif
  1819. #ifdef CONFIG_PM
  1820. vfree(chip->saved_regs);
  1821. #endif
  1822. if (chip->mpu_res) {
  1823. release_resource(chip->mpu_res);
  1824. kfree_nocheck(chip->mpu_res);
  1825. }
  1826. if (chip->fm_res) {
  1827. release_resource(chip->fm_res);
  1828. kfree_nocheck(chip->fm_res);
  1829. }
  1830. snd_ymfpci_free_gameport(chip);
  1831. if (chip->reg_area_virt)
  1832. iounmap(chip->reg_area_virt);
  1833. if (chip->work_ptr.area)
  1834. snd_dma_free_pages(&chip->work_ptr);
  1835. if (chip->irq >= 0)
  1836. free_irq(chip->irq, (void *)chip);
  1837. if (chip->res_reg_area) {
  1838. release_resource(chip->res_reg_area);
  1839. kfree_nocheck(chip->res_reg_area);
  1840. }
  1841. pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
  1842. pci_disable_device(chip->pci);
  1843. kfree(chip);
  1844. return 0;
  1845. }
  1846. static int snd_ymfpci_dev_free(snd_device_t *device)
  1847. {
  1848. ymfpci_t *chip = device->device_data;
  1849. return snd_ymfpci_free(chip);
  1850. }
  1851. #ifdef CONFIG_PM
  1852. static int saved_regs_index[] = {
  1853. /* spdif */
  1854. YDSXGR_SPDIFOUTCTRL,
  1855. YDSXGR_SPDIFOUTSTATUS,
  1856. YDSXGR_SPDIFINCTRL,
  1857. /* volumes */
  1858. YDSXGR_PRIADCLOOPVOL,
  1859. YDSXGR_NATIVEDACINVOL,
  1860. YDSXGR_NATIVEDACOUTVOL,
  1861. // YDSXGR_BUF441OUTVOL,
  1862. YDSXGR_NATIVEADCINVOL,
  1863. YDSXGR_SPDIFLOOPVOL,
  1864. YDSXGR_SPDIFOUTVOL,
  1865. YDSXGR_ZVOUTVOL,
  1866. YDSXGR_LEGACYOUTVOL,
  1867. /* address bases */
  1868. YDSXGR_PLAYCTRLBASE,
  1869. YDSXGR_RECCTRLBASE,
  1870. YDSXGR_EFFCTRLBASE,
  1871. YDSXGR_WORKBASE,
  1872. /* capture set up */
  1873. YDSXGR_MAPOFREC,
  1874. YDSXGR_RECFORMAT,
  1875. YDSXGR_RECSLOTSR,
  1876. YDSXGR_ADCFORMAT,
  1877. YDSXGR_ADCSLOTSR,
  1878. };
  1879. #define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
  1880. static int snd_ymfpci_suspend(snd_card_t *card, pm_message_t state)
  1881. {
  1882. ymfpci_t *chip = card->pm_private_data;
  1883. unsigned int i;
  1884. snd_pcm_suspend_all(chip->pcm);
  1885. snd_pcm_suspend_all(chip->pcm2);
  1886. snd_pcm_suspend_all(chip->pcm_spdif);
  1887. snd_pcm_suspend_all(chip->pcm_4ch);
  1888. snd_ac97_suspend(chip->ac97);
  1889. for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
  1890. chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
  1891. chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
  1892. snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
  1893. snd_ymfpci_disable_dsp(chip);
  1894. pci_disable_device(chip->pci);
  1895. return 0;
  1896. }
  1897. static int snd_ymfpci_resume(snd_card_t *card)
  1898. {
  1899. ymfpci_t *chip = card->pm_private_data;
  1900. unsigned int i;
  1901. pci_enable_device(chip->pci);
  1902. pci_set_master(chip->pci);
  1903. snd_ymfpci_aclink_reset(chip->pci);
  1904. snd_ymfpci_codec_ready(chip, 0);
  1905. snd_ymfpci_download_image(chip);
  1906. udelay(100);
  1907. for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
  1908. snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
  1909. snd_ac97_resume(chip->ac97);
  1910. /* start hw again */
  1911. if (chip->start_count > 0) {
  1912. spin_lock_irq(&chip->reg_lock);
  1913. snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
  1914. chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
  1915. spin_unlock_irq(&chip->reg_lock);
  1916. }
  1917. return 0;
  1918. }
  1919. #endif /* CONFIG_PM */
  1920. int __devinit snd_ymfpci_create(snd_card_t * card,
  1921. struct pci_dev * pci,
  1922. unsigned short old_legacy_ctrl,
  1923. ymfpci_t ** rchip)
  1924. {
  1925. ymfpci_t *chip;
  1926. int err;
  1927. static snd_device_ops_t ops = {
  1928. .dev_free = snd_ymfpci_dev_free,
  1929. };
  1930. *rchip = NULL;
  1931. /* enable PCI device */
  1932. if ((err = pci_enable_device(pci)) < 0)
  1933. return err;
  1934. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1935. if (chip == NULL) {
  1936. pci_disable_device(pci);
  1937. return -ENOMEM;
  1938. }
  1939. chip->old_legacy_ctrl = old_legacy_ctrl;
  1940. spin_lock_init(&chip->reg_lock);
  1941. spin_lock_init(&chip->voice_lock);
  1942. init_waitqueue_head(&chip->interrupt_sleep);
  1943. atomic_set(&chip->interrupt_sleep_count, 0);
  1944. chip->card = card;
  1945. chip->pci = pci;
  1946. chip->irq = -1;
  1947. chip->device_id = pci->device;
  1948. pci_read_config_byte(pci, PCI_REVISION_ID, (u8 *)&chip->rev);
  1949. chip->reg_area_phys = pci_resource_start(pci, 0);
  1950. chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
  1951. pci_set_master(pci);
  1952. if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
  1953. snd_printk("unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
  1954. snd_ymfpci_free(chip);
  1955. return -EBUSY;
  1956. }
  1957. if (request_irq(pci->irq, snd_ymfpci_interrupt, SA_INTERRUPT|SA_SHIRQ, "YMFPCI", (void *) chip)) {
  1958. snd_printk("unable to grab IRQ %d\n", pci->irq);
  1959. snd_ymfpci_free(chip);
  1960. return -EBUSY;
  1961. }
  1962. chip->irq = pci->irq;
  1963. snd_ymfpci_aclink_reset(pci);
  1964. if (snd_ymfpci_codec_ready(chip, 0) < 0) {
  1965. snd_ymfpci_free(chip);
  1966. return -EIO;
  1967. }
  1968. snd_ymfpci_download_image(chip);
  1969. udelay(100); /* seems we need a delay after downloading image.. */
  1970. if (snd_ymfpci_memalloc(chip) < 0) {
  1971. snd_ymfpci_free(chip);
  1972. return -EIO;
  1973. }
  1974. if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
  1975. snd_ymfpci_free(chip);
  1976. return err;
  1977. }
  1978. #ifdef CONFIG_PM
  1979. chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
  1980. if (chip->saved_regs == NULL) {
  1981. snd_ymfpci_free(chip);
  1982. return -ENOMEM;
  1983. }
  1984. snd_card_set_pm_callback(card, snd_ymfpci_suspend, snd_ymfpci_resume, chip);
  1985. #endif
  1986. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1987. snd_ymfpci_free(chip);
  1988. return err;
  1989. }
  1990. snd_ymfpci_proc_init(card, chip);
  1991. snd_card_set_dev(card, &pci->dev);
  1992. *rchip = chip;
  1993. return 0;
  1994. }