ymfpci_main.c 68 KB

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