ymfpci_main.c 71 KB

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