azt3328.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412
  1. /*
  2. * azt3328.c - driver for Aztech AZF3328 based soundcards (e.g. PCI168).
  3. * Copyright (C) 2002, 2005 - 2008 by Andreas Mohr <andi AT lisas.de>
  4. *
  5. * Framework borrowed from Bart Hartgers's als4000.c.
  6. * Driver developed on PCI168 AP(W) version (PCI rev. 10, subsystem ID 1801),
  7. * found in a Fujitsu-Siemens PC ("Cordant", aluminum case).
  8. * Other versions are:
  9. * PCI168 A(W), sub ID 1800
  10. * PCI168 A/AP, sub ID 8000
  11. * Please give me feedback in case you try my driver with one of these!!
  12. *
  13. * GPL LICENSE
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * NOTES
  28. * Since Aztech does not provide any chipset documentation,
  29. * even on repeated request to various addresses,
  30. * and the answer that was finally given was negative
  31. * (and I was stupid enough to manage to get hold of a PCI168 soundcard
  32. * in the first place >:-P}),
  33. * I was forced to base this driver on reverse engineering
  34. * (3 weeks' worth of evenings filled with driver work).
  35. * (and no, I did NOT go the easy way: to pick up a SB PCI128 for 9 Euros)
  36. *
  37. * It is quite likely that the AZF3328 chip is the PCI cousin of the
  38. * AZF3318 ("azt1020 pnp", "MM Pro 16") ISA chip, given very similar specs.
  39. *
  40. * The AZF3328 chip (note: AZF3328, *not* AZT3328, that's just the driver name
  41. * for compatibility reasons) from Azfin (joint-venture of Aztech and Fincitec,
  42. * Fincitec acquired by National Semiconductor in 2002, together with the
  43. * Fincitec-related company ARSmikro) has the following features:
  44. *
  45. * - compatibility & compliance:
  46. * - Microsoft PC 97 ("PC 97 Hardware Design Guide",
  47. * http://www.microsoft.com/whdc/archive/pcguides.mspx)
  48. * - Microsoft PC 98 Baseline Audio
  49. * - MPU401 UART
  50. * - Sound Blaster Emulation (DOS Box)
  51. * - builtin AC97 conformant codec (SNR over 80dB)
  52. * Note that "conformant" != "compliant"!! this chip's mixer register layout
  53. * *differs* from the standard AC97 layout:
  54. * they chose to not implement the headphone register (which is not a
  55. * problem since it's merely optional), yet when doing this, they committed
  56. * the grave sin of letting other registers follow immediately instead of
  57. * keeping a headphone dummy register, thereby shifting the mixer register
  58. * addresses illegally. So far unfortunately it looks like the very flexible
  59. * ALSA AC97 support is still not enough to easily compensate for such a
  60. * grave layout violation despite all tweaks and quirks mechanisms it offers.
  61. * - builtin genuine OPL3 - verified to work fine, 20080506
  62. * - full duplex 16bit playback/record at independent sampling rate
  63. * - MPU401 (+ legacy address support, claimed by one official spec sheet)
  64. * FIXME: how to enable legacy addr??
  65. * - game port (legacy address support)
  66. * - builtin DirectInput support, helps reduce CPU overhead (interrupt-driven
  67. * features supported). - See common term "Digital Enhanced Game Port"...
  68. * (probably DirectInput 3.0 spec - confirm)
  69. * - builtin 3D enhancement (said to be YAMAHA Ymersion)
  70. * - built-in General DirectX timer having a 20 bits counter
  71. * with 1us resolution (see below!)
  72. * - I2S serial output port for external DAC
  73. * - supports 33MHz PCI spec 2.1, PCI power management 1.0, compliant with ACPI
  74. * - supports hardware volume control
  75. * - single chip low cost solution (128 pin QFP)
  76. * - supports programmable Sub-vendor and Sub-system ID
  77. * required for Microsoft's logo compliance (FIXME: where?)
  78. * At least the Trident 4D Wave DX has one bit somewhere
  79. * to enable writes to PCI subsystem VID registers, that should be it.
  80. * This might easily be in extended PCI reg space, since PCI168 also has
  81. * some custom data starting at 0x80. What kind of config settings
  82. * are located in our extended PCI space anyway??
  83. * - PCI168 AP(W) card: power amplifier with 4 Watts/channel at 4 Ohms
  84. *
  85. * Note that this driver now is actually *better* than the Windows driver,
  86. * since it additionally supports the card's 1MHz DirectX timer - just try
  87. * the following snd-seq module parameters etc.:
  88. * - options snd-seq seq_default_timer_class=2 seq_default_timer_sclass=0
  89. * seq_default_timer_card=0 seq_client_load=1 seq_default_timer_device=0
  90. * seq_default_timer_subdevice=0 seq_default_timer_resolution=1000000
  91. * - "timidity -iAv -B2,8 -Os -EFreverb=0"
  92. * - "pmidi -p 128:0 jazz.mid"
  93. *
  94. * OPL3 hardware playback testing, try something like:
  95. * cat /proc/asound/hwdep
  96. * and
  97. * aconnect -o
  98. * Then use
  99. * sbiload -Dhw:x,y --opl3 /usr/share/sounds/opl3/std.o3 ......./drums.o3
  100. * where x,y is the xx-yy number as given in hwdep.
  101. * Then try
  102. * pmidi -p a:b jazz.mid
  103. * where a:b is the client number plus 0 usually, as given by aconnect above.
  104. * Oh, and make sure to unmute the FM mixer control (doh!)
  105. * NOTE: power use during OPL3 playback is _VERY_ high (70W --> 90W!)
  106. * despite no CPU activity, possibly due to hindering ACPI idling somehow.
  107. * Shouldn't be a problem of the AZF3328 chip itself, I'd hope.
  108. * Higher PCM / FM mixer levels seem to conflict (causes crackling),
  109. * at least sometimes. Maybe even use with hardware sequencer timer above :)
  110. * adplay/adplug-utils might soon offer hardware-based OPL3 playback, too.
  111. *
  112. * Certain PCI versions of this card are susceptible to DMA traffic underruns
  113. * in some systems (resulting in sound crackling/clicking/popping),
  114. * probably because they don't have a DMA FIFO buffer or so.
  115. * Overview (PCI ID/PCI subID/PCI rev.):
  116. * - no DMA crackling on SiS735: 0x50DC/0x1801/16
  117. * - unknown performance: 0x50DC/0x1801/10
  118. * (well, it's not bad on an Athlon 1800 with now very optimized IRQ handler)
  119. *
  120. * Crackling happens with VIA chipsets or, in my case, an SiS735, which is
  121. * supposed to be very fast and supposed to get rid of crackling much
  122. * better than a VIA, yet ironically I still get crackling, like many other
  123. * people with the same chipset.
  124. * Possible remedies:
  125. * - use speaker (amplifier) output instead of headphone output
  126. * (in case crackling is due to overloaded output clipping)
  127. * - plug card into a different PCI slot, preferrably one that isn't shared
  128. * too much (this helps a lot, but not completely!)
  129. * - get rid of PCI VGA card, use AGP instead
  130. * - upgrade or downgrade BIOS
  131. * - fiddle with PCI latency settings (setpci -v -s BUSID latency_timer=XX)
  132. * Not too helpful.
  133. * - Disable ACPI/power management/"Auto Detect RAM/PCI Clk" in BIOS
  134. *
  135. * BUGS
  136. * - full-duplex might *still* be problematic, however a recent test was fine
  137. * - (non-bug) "Bass/Treble or 3D settings don't work" - they do get evaluated
  138. * if you set PCM output switch to "pre 3D" instead of "post 3D".
  139. * If this can't be set, then get a mixer application that Isn't Stupid (tm)
  140. * (e.g. kmix, gamix) - unfortunately several are!!
  141. * - locking is not entirely clean, especially the audio stream activity
  142. * ints --> may be racy
  143. * - an _unconnected_ secondary joystick at the gameport will be reported
  144. * to be "active" (floating values, not precisely -1) due to the way we need
  145. * to read the Digital Enhanced Game Port. Not sure whether it is fixable.
  146. *
  147. * TODO
  148. * - test MPU401 MIDI playback etc.
  149. * - add more power micro-management (disable various units of the card
  150. * as long as they're unused). However this requires more I/O ports which I
  151. * haven't figured out yet and which thus might not even exist...
  152. * The standard suspend/resume functionality could probably make use of
  153. * some improvement, too...
  154. * - figure out what all unknown port bits are responsible for
  155. * - figure out some cleverly evil scheme to possibly make ALSA AC97 code
  156. * fully accept our quite incompatible ""AC97"" mixer and thus save some
  157. * code (but I'm not too optimistic that doing this is possible at all)
  158. * - use MMIO (memory-mapped I/O)? Slightly faster access, e.g. for gameport.
  159. */
  160. #include <asm/io.h>
  161. #include <linux/init.h>
  162. #include <linux/pci.h>
  163. #include <linux/delay.h>
  164. #include <linux/slab.h>
  165. #include <linux/gameport.h>
  166. #include <linux/moduleparam.h>
  167. #include <linux/dma-mapping.h>
  168. #include <sound/core.h>
  169. #include <sound/control.h>
  170. #include <sound/pcm.h>
  171. #include <sound/rawmidi.h>
  172. #include <sound/mpu401.h>
  173. #include <sound/opl3.h>
  174. #include <sound/initval.h>
  175. #include "azt3328.h"
  176. MODULE_AUTHOR("Andreas Mohr <andi AT lisas.de>");
  177. MODULE_DESCRIPTION("Aztech AZF3328 (PCI168)");
  178. MODULE_LICENSE("GPL");
  179. MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");
  180. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  181. #define SUPPORT_GAMEPORT 1
  182. #endif
  183. #define DEBUG_MISC 0
  184. #define DEBUG_CALLS 0
  185. #define DEBUG_MIXER 0
  186. #define DEBUG_PLAY_REC 0
  187. #define DEBUG_IO 0
  188. #define DEBUG_TIMER 0
  189. #define DEBUG_GAME 0
  190. #define MIXER_TESTING 0
  191. #if DEBUG_MISC
  192. #define snd_azf3328_dbgmisc(format, args...) printk(KERN_ERR format, ##args)
  193. #else
  194. #define snd_azf3328_dbgmisc(format, args...)
  195. #endif
  196. #if DEBUG_CALLS
  197. #define snd_azf3328_dbgcalls(format, args...) printk(format, ##args)
  198. #define snd_azf3328_dbgcallenter() printk(KERN_ERR "--> %s\n", __func__)
  199. #define snd_azf3328_dbgcallleave() printk(KERN_ERR "<-- %s\n", __func__)
  200. #else
  201. #define snd_azf3328_dbgcalls(format, args...)
  202. #define snd_azf3328_dbgcallenter()
  203. #define snd_azf3328_dbgcallleave()
  204. #endif
  205. #if DEBUG_MIXER
  206. #define snd_azf3328_dbgmixer(format, args...) printk(KERN_DEBUG format, ##args)
  207. #else
  208. #define snd_azf3328_dbgmixer(format, args...)
  209. #endif
  210. #if DEBUG_PLAY_REC
  211. #define snd_azf3328_dbgplay(format, args...) printk(KERN_DEBUG format, ##args)
  212. #else
  213. #define snd_azf3328_dbgplay(format, args...)
  214. #endif
  215. #if DEBUG_MISC
  216. #define snd_azf3328_dbgtimer(format, args...) printk(KERN_DEBUG format, ##args)
  217. #else
  218. #define snd_azf3328_dbgtimer(format, args...)
  219. #endif
  220. #if DEBUG_GAME
  221. #define snd_azf3328_dbggame(format, args...) printk(KERN_DEBUG format, ##args)
  222. #else
  223. #define snd_azf3328_dbggame(format, args...)
  224. #endif
  225. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  226. module_param_array(index, int, NULL, 0444);
  227. MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");
  228. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  229. module_param_array(id, charp, NULL, 0444);
  230. MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");
  231. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  232. module_param_array(enable, bool, NULL, 0444);
  233. MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");
  234. static int seqtimer_scaling = 128;
  235. module_param(seqtimer_scaling, int, 0444);
  236. MODULE_PARM_DESC(seqtimer_scaling, "Set 1024000Hz sequencer timer scale factor (lockup danger!). Default 128.");
  237. struct snd_azf3328_audio_stream {
  238. struct snd_pcm_substream *substream;
  239. int enabled;
  240. int running;
  241. unsigned long portbase;
  242. };
  243. enum snd_azf3328_stream_index {
  244. AZF_PLAYBACK = 0,
  245. AZF_CAPTURE = 1,
  246. };
  247. struct snd_azf3328 {
  248. /* often-used fields towards beginning, then grouped */
  249. unsigned long codec_io; /* usually 0xb000, size 128 */
  250. unsigned long game_io; /* usually 0xb400, size 8 */
  251. unsigned long mpu_io; /* usually 0xb800, size 4 */
  252. unsigned long opl3_io; /* usually 0xbc00, size 8 */
  253. unsigned long mixer_io; /* usually 0xc000, size 64 */
  254. spinlock_t reg_lock;
  255. struct snd_timer *timer;
  256. struct snd_pcm *pcm;
  257. struct snd_azf3328_audio_stream audio_stream[2];
  258. struct snd_card *card;
  259. struct snd_rawmidi *rmidi;
  260. #ifdef SUPPORT_GAMEPORT
  261. struct gameport *gameport;
  262. int axes[4];
  263. #endif
  264. struct pci_dev *pci;
  265. int irq;
  266. /* register 0x6a is write-only, thus need to remember setting.
  267. * If we need to add more registers here, then we might try to fold this
  268. * into some transparent combined shadow register handling with
  269. * CONFIG_PM register storage below, but that's slightly difficult. */
  270. u16 shadow_reg_codec_6AH;
  271. #ifdef CONFIG_PM
  272. /* register value containers for power management
  273. * Note: not always full I/O range preserved (just like Win driver!) */
  274. u16 saved_regs_codec[AZF_IO_SIZE_CODEC_PM / 2];
  275. u16 saved_regs_game [AZF_IO_SIZE_GAME_PM / 2];
  276. u16 saved_regs_mpu [AZF_IO_SIZE_MPU_PM / 2];
  277. u16 saved_regs_opl3 [AZF_IO_SIZE_OPL3_PM / 2];
  278. u16 saved_regs_mixer[AZF_IO_SIZE_MIXER_PM / 2];
  279. #endif
  280. };
  281. static const struct pci_device_id snd_azf3328_ids[] = {
  282. { 0x122D, 0x50DC, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* PCI168/3328 */
  283. { 0x122D, 0x80DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* 3328 */
  284. { 0, }
  285. };
  286. MODULE_DEVICE_TABLE(pci, snd_azf3328_ids);
  287. static int
  288. snd_azf3328_io_reg_setb(unsigned reg, u8 mask, int do_set)
  289. {
  290. u8 prev = inb(reg), new;
  291. new = (do_set) ? (prev|mask) : (prev & ~mask);
  292. /* we need to always write the new value no matter whether it differs
  293. * or not, since some register bits don't indicate their setting */
  294. outb(new, reg);
  295. if (new != prev)
  296. return 1;
  297. return 0;
  298. }
  299. static inline void
  300. snd_azf3328_codec_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
  301. {
  302. outb(value, chip->codec_io + reg);
  303. }
  304. static inline u8
  305. snd_azf3328_codec_inb(const struct snd_azf3328 *chip, unsigned reg)
  306. {
  307. return inb(chip->codec_io + reg);
  308. }
  309. static inline void
  310. snd_azf3328_codec_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
  311. {
  312. outw(value, chip->codec_io + reg);
  313. }
  314. static inline u16
  315. snd_azf3328_codec_inw(const struct snd_azf3328 *chip, unsigned reg)
  316. {
  317. return inw(chip->codec_io + reg);
  318. }
  319. static inline void
  320. snd_azf3328_codec_outl(const struct snd_azf3328 *chip, unsigned reg, u32 value)
  321. {
  322. outl(value, chip->codec_io + reg);
  323. }
  324. static inline u32
  325. snd_azf3328_codec_inl(const struct snd_azf3328 *chip, unsigned reg)
  326. {
  327. return inl(chip->codec_io + reg);
  328. }
  329. static inline void
  330. snd_azf3328_game_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
  331. {
  332. outb(value, chip->game_io + reg);
  333. }
  334. static inline void
  335. snd_azf3328_game_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
  336. {
  337. outw(value, chip->game_io + reg);
  338. }
  339. static inline u8
  340. snd_azf3328_game_inb(const struct snd_azf3328 *chip, unsigned reg)
  341. {
  342. return inb(chip->game_io + reg);
  343. }
  344. static inline u16
  345. snd_azf3328_game_inw(const struct snd_azf3328 *chip, unsigned reg)
  346. {
  347. return inw(chip->game_io + reg);
  348. }
  349. static inline void
  350. snd_azf3328_mixer_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
  351. {
  352. outw(value, chip->mixer_io + reg);
  353. }
  354. static inline u16
  355. snd_azf3328_mixer_inw(const struct snd_azf3328 *chip, unsigned reg)
  356. {
  357. return inw(chip->mixer_io + reg);
  358. }
  359. #define AZF_MUTE_BIT 0x80
  360. static int
  361. snd_azf3328_mixer_set_mute(const struct snd_azf3328 *chip,
  362. unsigned reg, int do_mute
  363. )
  364. {
  365. unsigned long portbase = chip->mixer_io + reg + 1;
  366. int updated;
  367. /* the mute bit is on the *second* (i.e. right) register of a
  368. * left/right channel setting */
  369. updated = snd_azf3328_io_reg_setb(portbase, AZF_MUTE_BIT, do_mute);
  370. /* indicate whether it was muted before */
  371. return (do_mute) ? !updated : updated;
  372. }
  373. static void
  374. snd_azf3328_mixer_write_volume_gradually(const struct snd_azf3328 *chip,
  375. unsigned reg,
  376. unsigned char dst_vol_left,
  377. unsigned char dst_vol_right,
  378. int chan_sel, int delay
  379. )
  380. {
  381. unsigned long portbase = chip->mixer_io + reg;
  382. unsigned char curr_vol_left = 0, curr_vol_right = 0;
  383. int left_change = 0, right_change = 0;
  384. snd_azf3328_dbgcallenter();
  385. if (chan_sel & SET_CHAN_LEFT) {
  386. curr_vol_left = inb(portbase + 1);
  387. /* take care of muting flag contained in left channel */
  388. if (curr_vol_left & AZF_MUTE_BIT)
  389. dst_vol_left |= AZF_MUTE_BIT;
  390. else
  391. dst_vol_left &= ~AZF_MUTE_BIT;
  392. left_change = (curr_vol_left > dst_vol_left) ? -1 : 1;
  393. }
  394. if (chan_sel & SET_CHAN_RIGHT) {
  395. curr_vol_right = inb(portbase + 0);
  396. right_change = (curr_vol_right > dst_vol_right) ? -1 : 1;
  397. }
  398. do {
  399. if (left_change) {
  400. if (curr_vol_left != dst_vol_left) {
  401. curr_vol_left += left_change;
  402. outb(curr_vol_left, portbase + 1);
  403. } else
  404. left_change = 0;
  405. }
  406. if (right_change) {
  407. if (curr_vol_right != dst_vol_right) {
  408. curr_vol_right += right_change;
  409. /* during volume change, the right channel is crackling
  410. * somewhat more than the left channel, unfortunately.
  411. * This seems to be a hardware issue. */
  412. outb(curr_vol_right, portbase + 0);
  413. } else
  414. right_change = 0;
  415. }
  416. if (delay)
  417. mdelay(delay);
  418. } while ((left_change) || (right_change));
  419. snd_azf3328_dbgcallleave();
  420. }
  421. /*
  422. * general mixer element
  423. */
  424. struct azf3328_mixer_reg {
  425. unsigned reg;
  426. unsigned int lchan_shift, rchan_shift;
  427. unsigned int mask;
  428. unsigned int invert: 1;
  429. unsigned int stereo: 1;
  430. unsigned int enum_c: 4;
  431. };
  432. #define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
  433. ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | \
  434. (mask << 16) | \
  435. (invert << 24) | \
  436. (stereo << 25) | \
  437. (enum_c << 26))
  438. static void snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg *r, unsigned long val)
  439. {
  440. r->reg = val & 0xff;
  441. r->lchan_shift = (val >> 8) & 0x0f;
  442. r->rchan_shift = (val >> 12) & 0x0f;
  443. r->mask = (val >> 16) & 0xff;
  444. r->invert = (val >> 24) & 1;
  445. r->stereo = (val >> 25) & 1;
  446. r->enum_c = (val >> 26) & 0x0f;
  447. }
  448. /*
  449. * mixer switches/volumes
  450. */
  451. #define AZF3328_MIXER_SWITCH(xname, reg, shift, invert) \
  452. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  453. .info = snd_azf3328_info_mixer, \
  454. .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
  455. .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0x1, invert, 0, 0), \
  456. }
  457. #define AZF3328_MIXER_VOL_STEREO(xname, reg, mask, invert) \
  458. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  459. .info = snd_azf3328_info_mixer, \
  460. .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
  461. .private_value = COMPOSE_MIXER_REG(reg, 8, 0, mask, invert, 1, 0), \
  462. }
  463. #define AZF3328_MIXER_VOL_MONO(xname, reg, mask, is_right_chan) \
  464. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  465. .info = snd_azf3328_info_mixer, \
  466. .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
  467. .private_value = COMPOSE_MIXER_REG(reg, is_right_chan ? 0 : 8, 0, mask, 1, 0, 0), \
  468. }
  469. #define AZF3328_MIXER_VOL_SPECIAL(xname, reg, mask, shift, invert) \
  470. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  471. .info = snd_azf3328_info_mixer, \
  472. .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
  473. .private_value = COMPOSE_MIXER_REG(reg, shift, 0, mask, invert, 0, 0), \
  474. }
  475. #define AZF3328_MIXER_ENUM(xname, reg, enum_c, shift) \
  476. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  477. .info = snd_azf3328_info_mixer_enum, \
  478. .get = snd_azf3328_get_mixer_enum, .put = snd_azf3328_put_mixer_enum, \
  479. .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0, 0, 0, enum_c), \
  480. }
  481. static int
  482. snd_azf3328_info_mixer(struct snd_kcontrol *kcontrol,
  483. struct snd_ctl_elem_info *uinfo)
  484. {
  485. struct azf3328_mixer_reg reg;
  486. snd_azf3328_dbgcallenter();
  487. snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
  488. uinfo->type = reg.mask == 1 ?
  489. SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  490. uinfo->count = reg.stereo + 1;
  491. uinfo->value.integer.min = 0;
  492. uinfo->value.integer.max = reg.mask;
  493. snd_azf3328_dbgcallleave();
  494. return 0;
  495. }
  496. static int
  497. snd_azf3328_get_mixer(struct snd_kcontrol *kcontrol,
  498. struct snd_ctl_elem_value *ucontrol)
  499. {
  500. struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
  501. struct azf3328_mixer_reg reg;
  502. unsigned int oreg, val;
  503. snd_azf3328_dbgcallenter();
  504. snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
  505. oreg = snd_azf3328_mixer_inw(chip, reg.reg);
  506. val = (oreg >> reg.lchan_shift) & reg.mask;
  507. if (reg.invert)
  508. val = reg.mask - val;
  509. ucontrol->value.integer.value[0] = val;
  510. if (reg.stereo) {
  511. val = (oreg >> reg.rchan_shift) & reg.mask;
  512. if (reg.invert)
  513. val = reg.mask - val;
  514. ucontrol->value.integer.value[1] = val;
  515. }
  516. snd_azf3328_dbgmixer("get: %02x is %04x -> vol %02lx|%02lx "
  517. "(shift %02d|%02d, mask %02x, inv. %d, stereo %d)\n",
  518. reg.reg, oreg,
  519. ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
  520. reg.lchan_shift, reg.rchan_shift, reg.mask, reg.invert, reg.stereo);
  521. snd_azf3328_dbgcallleave();
  522. return 0;
  523. }
  524. static int
  525. snd_azf3328_put_mixer(struct snd_kcontrol *kcontrol,
  526. struct snd_ctl_elem_value *ucontrol)
  527. {
  528. struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
  529. struct azf3328_mixer_reg reg;
  530. unsigned int oreg, nreg, val;
  531. snd_azf3328_dbgcallenter();
  532. snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
  533. oreg = snd_azf3328_mixer_inw(chip, reg.reg);
  534. val = ucontrol->value.integer.value[0] & reg.mask;
  535. if (reg.invert)
  536. val = reg.mask - val;
  537. nreg = oreg & ~(reg.mask << reg.lchan_shift);
  538. nreg |= (val << reg.lchan_shift);
  539. if (reg.stereo) {
  540. val = ucontrol->value.integer.value[1] & reg.mask;
  541. if (reg.invert)
  542. val = reg.mask - val;
  543. nreg &= ~(reg.mask << reg.rchan_shift);
  544. nreg |= (val << reg.rchan_shift);
  545. }
  546. if (reg.mask >= 0x07) /* it's a volume control, so better take care */
  547. snd_azf3328_mixer_write_volume_gradually(
  548. chip, reg.reg, nreg >> 8, nreg & 0xff,
  549. /* just set both channels, doesn't matter */
  550. SET_CHAN_LEFT|SET_CHAN_RIGHT,
  551. 0);
  552. else
  553. snd_azf3328_mixer_outw(chip, reg.reg, nreg);
  554. snd_azf3328_dbgmixer("put: %02x to %02lx|%02lx, "
  555. "oreg %04x; shift %02d|%02d -> nreg %04x; after: %04x\n",
  556. reg.reg, ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
  557. oreg, reg.lchan_shift, reg.rchan_shift,
  558. nreg, snd_azf3328_mixer_inw(chip, reg.reg));
  559. snd_azf3328_dbgcallleave();
  560. return (nreg != oreg);
  561. }
  562. static int
  563. snd_azf3328_info_mixer_enum(struct snd_kcontrol *kcontrol,
  564. struct snd_ctl_elem_info *uinfo)
  565. {
  566. static const char * const texts1[] = {
  567. "Mic1", "Mic2"
  568. };
  569. static const char * const texts2[] = {
  570. "Mix", "Mic"
  571. };
  572. static const char * const texts3[] = {
  573. "Mic", "CD", "Video", "Aux",
  574. "Line", "Mix", "Mix Mono", "Phone"
  575. };
  576. static const char * const texts4[] = {
  577. "pre 3D", "post 3D"
  578. };
  579. struct azf3328_mixer_reg reg;
  580. const char * const *p = NULL;
  581. snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
  582. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  583. uinfo->count = (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1;
  584. uinfo->value.enumerated.items = reg.enum_c;
  585. if (uinfo->value.enumerated.item > reg.enum_c - 1U)
  586. uinfo->value.enumerated.item = reg.enum_c - 1U;
  587. if (reg.reg == IDX_MIXER_ADVCTL2) {
  588. switch(reg.lchan_shift) {
  589. case 8: /* modem out sel */
  590. p = texts1;
  591. break;
  592. case 9: /* mono sel source */
  593. p = texts2;
  594. break;
  595. case 15: /* PCM Out Path */
  596. p = texts4;
  597. break;
  598. }
  599. } else
  600. if (reg.reg == IDX_MIXER_REC_SELECT)
  601. p = texts3;
  602. strcpy(uinfo->value.enumerated.name, p[uinfo->value.enumerated.item]);
  603. return 0;
  604. }
  605. static int
  606. snd_azf3328_get_mixer_enum(struct snd_kcontrol *kcontrol,
  607. struct snd_ctl_elem_value *ucontrol)
  608. {
  609. struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
  610. struct azf3328_mixer_reg reg;
  611. unsigned short val;
  612. snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
  613. val = snd_azf3328_mixer_inw(chip, reg.reg);
  614. if (reg.reg == IDX_MIXER_REC_SELECT) {
  615. ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
  616. ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
  617. } else
  618. ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
  619. snd_azf3328_dbgmixer("get_enum: %02x is %04x -> %d|%d (shift %02d, enum_c %d)\n",
  620. reg.reg, val, ucontrol->value.enumerated.item[0], ucontrol->value.enumerated.item[1],
  621. reg.lchan_shift, reg.enum_c);
  622. return 0;
  623. }
  624. static int
  625. snd_azf3328_put_mixer_enum(struct snd_kcontrol *kcontrol,
  626. struct snd_ctl_elem_value *ucontrol)
  627. {
  628. struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
  629. struct azf3328_mixer_reg reg;
  630. unsigned int oreg, nreg, val;
  631. snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
  632. oreg = snd_azf3328_mixer_inw(chip, reg.reg);
  633. val = oreg;
  634. if (reg.reg == IDX_MIXER_REC_SELECT) {
  635. if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U ||
  636. ucontrol->value.enumerated.item[1] > reg.enum_c - 1U)
  637. return -EINVAL;
  638. val = (ucontrol->value.enumerated.item[0] << 8) |
  639. (ucontrol->value.enumerated.item[1] << 0);
  640. } else {
  641. if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U)
  642. return -EINVAL;
  643. val &= ~((reg.enum_c - 1) << reg.lchan_shift);
  644. val |= (ucontrol->value.enumerated.item[0] << reg.lchan_shift);
  645. }
  646. snd_azf3328_mixer_outw(chip, reg.reg, val);
  647. nreg = val;
  648. snd_azf3328_dbgmixer("put_enum: %02x to %04x, oreg %04x\n", reg.reg, val, oreg);
  649. return (nreg != oreg);
  650. }
  651. static struct snd_kcontrol_new snd_azf3328_mixer_controls[] __devinitdata = {
  652. AZF3328_MIXER_SWITCH("Master Playback Switch", IDX_MIXER_PLAY_MASTER, 15, 1),
  653. AZF3328_MIXER_VOL_STEREO("Master Playback Volume", IDX_MIXER_PLAY_MASTER, 0x1f, 1),
  654. AZF3328_MIXER_SWITCH("PCM Playback Switch", IDX_MIXER_WAVEOUT, 15, 1),
  655. AZF3328_MIXER_VOL_STEREO("PCM Playback Volume",
  656. IDX_MIXER_WAVEOUT, 0x1f, 1),
  657. AZF3328_MIXER_SWITCH("PCM 3D Bypass Playback Switch",
  658. IDX_MIXER_ADVCTL2, 7, 1),
  659. AZF3328_MIXER_SWITCH("FM Playback Switch", IDX_MIXER_FMSYNTH, 15, 1),
  660. AZF3328_MIXER_VOL_STEREO("FM Playback Volume", IDX_MIXER_FMSYNTH, 0x1f, 1),
  661. AZF3328_MIXER_SWITCH("CD Playback Switch", IDX_MIXER_CDAUDIO, 15, 1),
  662. AZF3328_MIXER_VOL_STEREO("CD Playback Volume", IDX_MIXER_CDAUDIO, 0x1f, 1),
  663. AZF3328_MIXER_SWITCH("Capture Switch", IDX_MIXER_REC_VOLUME, 15, 1),
  664. AZF3328_MIXER_VOL_STEREO("Capture Volume", IDX_MIXER_REC_VOLUME, 0x0f, 0),
  665. AZF3328_MIXER_ENUM("Capture Source", IDX_MIXER_REC_SELECT, 8, 0),
  666. AZF3328_MIXER_SWITCH("Mic Playback Switch", IDX_MIXER_MIC, 15, 1),
  667. AZF3328_MIXER_VOL_MONO("Mic Playback Volume", IDX_MIXER_MIC, 0x1f, 1),
  668. AZF3328_MIXER_SWITCH("Mic Boost (+20dB)", IDX_MIXER_MIC, 6, 0),
  669. AZF3328_MIXER_SWITCH("Line Playback Switch", IDX_MIXER_LINEIN, 15, 1),
  670. AZF3328_MIXER_VOL_STEREO("Line Playback Volume", IDX_MIXER_LINEIN, 0x1f, 1),
  671. AZF3328_MIXER_SWITCH("PC Speaker Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
  672. AZF3328_MIXER_VOL_SPECIAL("PC Speaker Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
  673. AZF3328_MIXER_SWITCH("Video Playback Switch", IDX_MIXER_VIDEO, 15, 1),
  674. AZF3328_MIXER_VOL_STEREO("Video Playback Volume", IDX_MIXER_VIDEO, 0x1f, 1),
  675. AZF3328_MIXER_SWITCH("Aux Playback Switch", IDX_MIXER_AUX, 15, 1),
  676. AZF3328_MIXER_VOL_STEREO("Aux Playback Volume", IDX_MIXER_AUX, 0x1f, 1),
  677. AZF3328_MIXER_SWITCH("Modem Playback Switch", IDX_MIXER_MODEMOUT, 15, 1),
  678. AZF3328_MIXER_VOL_MONO("Modem Playback Volume", IDX_MIXER_MODEMOUT, 0x1f, 1),
  679. AZF3328_MIXER_SWITCH("Modem Capture Switch", IDX_MIXER_MODEMIN, 15, 1),
  680. AZF3328_MIXER_VOL_MONO("Modem Capture Volume", IDX_MIXER_MODEMIN, 0x1f, 1),
  681. AZF3328_MIXER_ENUM("Mic Select", IDX_MIXER_ADVCTL2, 2, 8),
  682. AZF3328_MIXER_ENUM("Mono Output Select", IDX_MIXER_ADVCTL2, 2, 9),
  683. AZF3328_MIXER_ENUM("PCM Output Route", IDX_MIXER_ADVCTL2, 2, 15), /* PCM Out Path, place in front since it controls *both* 3D and Bass/Treble! */
  684. AZF3328_MIXER_VOL_SPECIAL("Tone Control - Treble", IDX_MIXER_BASSTREBLE, 0x07, 1, 0),
  685. AZF3328_MIXER_VOL_SPECIAL("Tone Control - Bass", IDX_MIXER_BASSTREBLE, 0x07, 9, 0),
  686. AZF3328_MIXER_SWITCH("3D Control - Switch", IDX_MIXER_ADVCTL2, 13, 0),
  687. AZF3328_MIXER_VOL_SPECIAL("3D Control - Width", IDX_MIXER_ADVCTL1, 0x07, 1, 0), /* "3D Width" */
  688. AZF3328_MIXER_VOL_SPECIAL("3D Control - Depth", IDX_MIXER_ADVCTL1, 0x03, 8, 0), /* "Hifi 3D" */
  689. #if MIXER_TESTING
  690. AZF3328_MIXER_SWITCH("0", IDX_MIXER_ADVCTL2, 0, 0),
  691. AZF3328_MIXER_SWITCH("1", IDX_MIXER_ADVCTL2, 1, 0),
  692. AZF3328_MIXER_SWITCH("2", IDX_MIXER_ADVCTL2, 2, 0),
  693. AZF3328_MIXER_SWITCH("3", IDX_MIXER_ADVCTL2, 3, 0),
  694. AZF3328_MIXER_SWITCH("4", IDX_MIXER_ADVCTL2, 4, 0),
  695. AZF3328_MIXER_SWITCH("5", IDX_MIXER_ADVCTL2, 5, 0),
  696. AZF3328_MIXER_SWITCH("6", IDX_MIXER_ADVCTL2, 6, 0),
  697. AZF3328_MIXER_SWITCH("7", IDX_MIXER_ADVCTL2, 7, 0),
  698. AZF3328_MIXER_SWITCH("8", IDX_MIXER_ADVCTL2, 8, 0),
  699. AZF3328_MIXER_SWITCH("9", IDX_MIXER_ADVCTL2, 9, 0),
  700. AZF3328_MIXER_SWITCH("10", IDX_MIXER_ADVCTL2, 10, 0),
  701. AZF3328_MIXER_SWITCH("11", IDX_MIXER_ADVCTL2, 11, 0),
  702. AZF3328_MIXER_SWITCH("12", IDX_MIXER_ADVCTL2, 12, 0),
  703. AZF3328_MIXER_SWITCH("13", IDX_MIXER_ADVCTL2, 13, 0),
  704. AZF3328_MIXER_SWITCH("14", IDX_MIXER_ADVCTL2, 14, 0),
  705. AZF3328_MIXER_SWITCH("15", IDX_MIXER_ADVCTL2, 15, 0),
  706. #endif
  707. };
  708. static u16 __devinitdata snd_azf3328_init_values[][2] = {
  709. { IDX_MIXER_PLAY_MASTER, MIXER_MUTE_MASK|0x1f1f },
  710. { IDX_MIXER_MODEMOUT, MIXER_MUTE_MASK|0x1f1f },
  711. { IDX_MIXER_BASSTREBLE, 0x0000 },
  712. { IDX_MIXER_PCBEEP, MIXER_MUTE_MASK|0x1f1f },
  713. { IDX_MIXER_MODEMIN, MIXER_MUTE_MASK|0x1f1f },
  714. { IDX_MIXER_MIC, MIXER_MUTE_MASK|0x001f },
  715. { IDX_MIXER_LINEIN, MIXER_MUTE_MASK|0x1f1f },
  716. { IDX_MIXER_CDAUDIO, MIXER_MUTE_MASK|0x1f1f },
  717. { IDX_MIXER_VIDEO, MIXER_MUTE_MASK|0x1f1f },
  718. { IDX_MIXER_AUX, MIXER_MUTE_MASK|0x1f1f },
  719. { IDX_MIXER_WAVEOUT, MIXER_MUTE_MASK|0x1f1f },
  720. { IDX_MIXER_FMSYNTH, MIXER_MUTE_MASK|0x1f1f },
  721. { IDX_MIXER_REC_VOLUME, MIXER_MUTE_MASK|0x0707 },
  722. };
  723. static int __devinit
  724. snd_azf3328_mixer_new(struct snd_azf3328 *chip)
  725. {
  726. struct snd_card *card;
  727. const struct snd_kcontrol_new *sw;
  728. unsigned int idx;
  729. int err;
  730. snd_azf3328_dbgcallenter();
  731. if (snd_BUG_ON(!chip || !chip->card))
  732. return -EINVAL;
  733. card = chip->card;
  734. /* mixer reset */
  735. snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
  736. /* mute and zero volume channels */
  737. for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_init_values); ++idx) {
  738. snd_azf3328_mixer_outw(chip,
  739. snd_azf3328_init_values[idx][0],
  740. snd_azf3328_init_values[idx][1]);
  741. }
  742. /* add mixer controls */
  743. sw = snd_azf3328_mixer_controls;
  744. for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls);
  745. ++idx, ++sw) {
  746. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(sw, chip))) < 0)
  747. return err;
  748. }
  749. snd_component_add(card, "AZF3328 mixer");
  750. strcpy(card->mixername, "AZF3328 mixer");
  751. snd_azf3328_dbgcallleave();
  752. return 0;
  753. }
  754. static int
  755. snd_azf3328_hw_params(struct snd_pcm_substream *substream,
  756. struct snd_pcm_hw_params *hw_params)
  757. {
  758. int res;
  759. snd_azf3328_dbgcallenter();
  760. res = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  761. snd_azf3328_dbgcallleave();
  762. return res;
  763. }
  764. static int
  765. snd_azf3328_hw_free(struct snd_pcm_substream *substream)
  766. {
  767. snd_azf3328_dbgcallenter();
  768. snd_pcm_lib_free_pages(substream);
  769. snd_azf3328_dbgcallleave();
  770. return 0;
  771. }
  772. static void
  773. snd_azf3328_codec_setfmt(struct snd_azf3328 *chip,
  774. unsigned reg,
  775. enum azf_freq_t bitrate,
  776. unsigned int format_width,
  777. unsigned int channels
  778. )
  779. {
  780. u16 val = 0xff00;
  781. unsigned long flags;
  782. snd_azf3328_dbgcallenter();
  783. switch (bitrate) {
  784. case AZF_FREQ_4000: val |= SOUNDFORMAT_FREQ_SUSPECTED_4000; break;
  785. case AZF_FREQ_4800: val |= SOUNDFORMAT_FREQ_SUSPECTED_4800; break;
  786. case AZF_FREQ_5512:
  787. /* the AZF3328 names it "5510" for some strange reason */
  788. val |= SOUNDFORMAT_FREQ_5510; break;
  789. case AZF_FREQ_6620: val |= SOUNDFORMAT_FREQ_6620; break;
  790. case AZF_FREQ_8000: val |= SOUNDFORMAT_FREQ_8000; break;
  791. case AZF_FREQ_9600: val |= SOUNDFORMAT_FREQ_9600; break;
  792. case AZF_FREQ_11025: val |= SOUNDFORMAT_FREQ_11025; break;
  793. case AZF_FREQ_13240: val |= SOUNDFORMAT_FREQ_SUSPECTED_13240; break;
  794. case AZF_FREQ_16000: val |= SOUNDFORMAT_FREQ_16000; break;
  795. case AZF_FREQ_22050: val |= SOUNDFORMAT_FREQ_22050; break;
  796. case AZF_FREQ_32000: val |= SOUNDFORMAT_FREQ_32000; break;
  797. default:
  798. snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!\n", bitrate);
  799. /* fall-through */
  800. case AZF_FREQ_44100: val |= SOUNDFORMAT_FREQ_44100; break;
  801. case AZF_FREQ_48000: val |= SOUNDFORMAT_FREQ_48000; break;
  802. case AZF_FREQ_66200: val |= SOUNDFORMAT_FREQ_SUSPECTED_66200; break;
  803. }
  804. /* val = 0xff07; 3m27.993s (65301Hz; -> 64000Hz???) hmm, 66120, 65967, 66123 */
  805. /* val = 0xff09; 17m15.098s (13123,478Hz; -> 12000Hz???) hmm, 13237.2Hz? */
  806. /* val = 0xff0a; 47m30.599s (4764,891Hz; -> 4800Hz???) yup, 4803Hz */
  807. /* val = 0xff0c; 57m0.510s (4010,263Hz; -> 4000Hz???) yup, 4003Hz */
  808. /* val = 0xff05; 5m11.556s (... -> 44100Hz) */
  809. /* val = 0xff03; 10m21.529s (21872,463Hz; -> 22050Hz???) */
  810. /* val = 0xff0f; 20m41.883s (10937,993Hz; -> 11025Hz???) */
  811. /* val = 0xff0d; 41m23.135s (5523,600Hz; -> 5512Hz???) */
  812. /* val = 0xff0e; 28m30.777s (8017Hz; -> 8000Hz???) */
  813. if (channels == 2)
  814. val |= SOUNDFORMAT_FLAG_2CHANNELS;
  815. if (format_width == 16)
  816. val |= SOUNDFORMAT_FLAG_16BIT;
  817. spin_lock_irqsave(&chip->reg_lock, flags);
  818. /* set bitrate/format */
  819. snd_azf3328_codec_outw(chip, reg, val);
  820. /* changing the bitrate/format settings switches off the
  821. * audio output with an annoying click in case of 8/16bit format change
  822. * (maybe shutting down DAC/ADC?), thus immediately
  823. * do some tweaking to reenable it and get rid of the clicking
  824. * (FIXME: yes, it works, but what exactly am I doing here?? :)
  825. * FIXME: does this have some side effects for full-duplex
  826. * or other dramatic side effects? */
  827. if (reg == IDX_IO_PLAY_SOUNDFORMAT) /* only do it for playback */
  828. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
  829. snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS) |
  830. DMA_PLAY_SOMETHING1 |
  831. DMA_PLAY_SOMETHING2 |
  832. SOMETHING_ALMOST_ALWAYS_SET |
  833. DMA_EPILOGUE_SOMETHING |
  834. DMA_SOMETHING_ELSE
  835. );
  836. spin_unlock_irqrestore(&chip->reg_lock, flags);
  837. snd_azf3328_dbgcallleave();
  838. }
  839. static inline void
  840. snd_azf3328_codec_setfmt_lowpower(struct snd_azf3328 *chip,
  841. unsigned reg
  842. )
  843. {
  844. /* choose lowest frequency for low power consumption.
  845. * While this will cause louder noise due to rather coarse frequency,
  846. * it should never matter since output should always
  847. * get disabled properly when idle anyway. */
  848. snd_azf3328_codec_setfmt(chip, reg, AZF_FREQ_4000, 8, 1);
  849. }
  850. static void
  851. snd_azf3328_codec_reg_6AH_update(struct snd_azf3328 *chip,
  852. unsigned bitmask,
  853. int enable
  854. )
  855. {
  856. if (enable)
  857. chip->shadow_reg_codec_6AH &= ~bitmask;
  858. else
  859. chip->shadow_reg_codec_6AH |= bitmask;
  860. snd_azf3328_dbgplay("6AH_update mask 0x%04x enable %d: val 0x%04x\n",
  861. bitmask, enable, chip->shadow_reg_codec_6AH);
  862. snd_azf3328_codec_outw(chip, IDX_IO_6AH, chip->shadow_reg_codec_6AH);
  863. }
  864. static inline void
  865. snd_azf3328_codec_enable(struct snd_azf3328 *chip, int enable)
  866. {
  867. snd_azf3328_dbgplay("codec_enable %d\n", enable);
  868. /* no idea what exactly is being done here, but I strongly assume it's
  869. * PM related */
  870. snd_azf3328_codec_reg_6AH_update(
  871. chip, IO_6A_PAUSE_PLAYBACK_BIT8, enable
  872. );
  873. }
  874. static void
  875. snd_azf3328_codec_activity(struct snd_azf3328 *chip,
  876. enum snd_azf3328_stream_index stream_type,
  877. int enable
  878. )
  879. {
  880. int need_change = (chip->audio_stream[stream_type].running != enable);
  881. snd_azf3328_dbgplay(
  882. "codec_activity: type %d, enable %d, need_change %d\n",
  883. stream_type, enable, need_change
  884. );
  885. if (need_change) {
  886. enum snd_azf3328_stream_index other =
  887. (stream_type == AZF_PLAYBACK) ?
  888. AZF_CAPTURE : AZF_PLAYBACK;
  889. /* small check to prevent shutting down the other party
  890. * in case it's active */
  891. if ((enable) || !(chip->audio_stream[other].running))
  892. snd_azf3328_codec_enable(chip, enable);
  893. /* ...and adjust clock, too
  894. * (reduce noise and power consumption) */
  895. if (!enable)
  896. snd_azf3328_codec_setfmt_lowpower(
  897. chip,
  898. chip->audio_stream[stream_type].portbase
  899. + IDX_IO_PLAY_SOUNDFORMAT
  900. );
  901. }
  902. chip->audio_stream[stream_type].running = enable;
  903. }
  904. static void
  905. snd_azf3328_setdmaa(struct snd_azf3328 *chip,
  906. long unsigned int addr,
  907. unsigned int count,
  908. unsigned int size,
  909. enum snd_azf3328_stream_index stream_type
  910. )
  911. {
  912. snd_azf3328_dbgcallenter();
  913. if (!chip->audio_stream[stream_type].running) {
  914. /* AZF3328 uses a two buffer pointer DMA playback approach */
  915. unsigned long flags, portbase, addr_area2;
  916. /* width 32bit (prevent overflow): */
  917. unsigned long count_areas, count_tmp;
  918. portbase = chip->audio_stream[stream_type].portbase;
  919. count_areas = size/2;
  920. addr_area2 = addr+count_areas;
  921. count_areas--; /* max. index */
  922. snd_azf3328_dbgplay("set DMA: buf1 %08lx[%lu], buf2 %08lx[%lu]\n", addr, count_areas, addr_area2, count_areas);
  923. /* build combined I/O buffer length word */
  924. count_tmp = count_areas;
  925. count_areas |= (count_tmp << 16);
  926. spin_lock_irqsave(&chip->reg_lock, flags);
  927. outl(addr, portbase + IDX_IO_PLAY_DMA_START_1);
  928. outl(addr_area2, portbase + IDX_IO_PLAY_DMA_START_2);
  929. outl(count_areas, portbase + IDX_IO_PLAY_DMA_LEN_1);
  930. spin_unlock_irqrestore(&chip->reg_lock, flags);
  931. }
  932. snd_azf3328_dbgcallleave();
  933. }
  934. static int
  935. snd_azf3328_playback_prepare(struct snd_pcm_substream *substream)
  936. {
  937. #if 0
  938. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  939. struct snd_pcm_runtime *runtime = substream->runtime;
  940. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  941. unsigned int count = snd_pcm_lib_period_bytes(substream);
  942. #endif
  943. snd_azf3328_dbgcallenter();
  944. #if 0
  945. snd_azf3328_codec_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT,
  946. runtime->rate,
  947. snd_pcm_format_width(runtime->format),
  948. runtime->channels);
  949. snd_azf3328_setdmaa(chip, runtime->dma_addr, count, size, AZF_PLAYBACK);
  950. #endif
  951. snd_azf3328_dbgcallleave();
  952. return 0;
  953. }
  954. static int
  955. snd_azf3328_capture_prepare(struct snd_pcm_substream *substream)
  956. {
  957. #if 0
  958. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  959. struct snd_pcm_runtime *runtime = substream->runtime;
  960. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  961. unsigned int count = snd_pcm_lib_period_bytes(substream);
  962. #endif
  963. snd_azf3328_dbgcallenter();
  964. #if 0
  965. snd_azf3328_codec_setfmt(chip, IDX_IO_REC_SOUNDFORMAT,
  966. runtime->rate,
  967. snd_pcm_format_width(runtime->format),
  968. runtime->channels);
  969. snd_azf3328_setdmaa(chip, runtime->dma_addr, count, size, AZF_CAPTURE);
  970. #endif
  971. snd_azf3328_dbgcallleave();
  972. return 0;
  973. }
  974. static int
  975. snd_azf3328_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  976. {
  977. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  978. struct snd_pcm_runtime *runtime = substream->runtime;
  979. int result = 0;
  980. unsigned int status1;
  981. int previously_muted;
  982. snd_azf3328_dbgcalls("snd_azf3328_playback_trigger cmd %d\n", cmd);
  983. switch (cmd) {
  984. case SNDRV_PCM_TRIGGER_START:
  985. snd_azf3328_dbgplay("START PLAYBACK\n");
  986. /* mute WaveOut (avoid clicking during setup) */
  987. previously_muted =
  988. snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
  989. snd_azf3328_codec_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT,
  990. runtime->rate,
  991. snd_pcm_format_width(runtime->format),
  992. runtime->channels);
  993. spin_lock(&chip->reg_lock);
  994. /* first, remember current value: */
  995. status1 = snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS);
  996. /* stop playback */
  997. status1 &= ~DMA_RESUME;
  998. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
  999. /* FIXME: clear interrupts or what??? */
  1000. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_IRQTYPE, 0xffff);
  1001. spin_unlock(&chip->reg_lock);
  1002. snd_azf3328_setdmaa(chip, runtime->dma_addr,
  1003. snd_pcm_lib_period_bytes(substream),
  1004. snd_pcm_lib_buffer_bytes(substream),
  1005. AZF_PLAYBACK);
  1006. spin_lock(&chip->reg_lock);
  1007. #ifdef WIN9X
  1008. /* FIXME: enable playback/recording??? */
  1009. status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
  1010. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
  1011. /* start playback again */
  1012. /* FIXME: what is this value (0x0010)??? */
  1013. status1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
  1014. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
  1015. #else /* NT4 */
  1016. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
  1017. 0x0000);
  1018. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
  1019. DMA_PLAY_SOMETHING1);
  1020. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
  1021. DMA_PLAY_SOMETHING1 |
  1022. DMA_PLAY_SOMETHING2);
  1023. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
  1024. DMA_RESUME |
  1025. SOMETHING_ALMOST_ALWAYS_SET |
  1026. DMA_EPILOGUE_SOMETHING |
  1027. DMA_SOMETHING_ELSE);
  1028. #endif
  1029. spin_unlock(&chip->reg_lock);
  1030. snd_azf3328_codec_activity(chip, AZF_PLAYBACK, 1);
  1031. /* now unmute WaveOut */
  1032. if (!previously_muted)
  1033. snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);
  1034. snd_azf3328_dbgplay("STARTED PLAYBACK\n");
  1035. break;
  1036. case SNDRV_PCM_TRIGGER_RESUME:
  1037. snd_azf3328_dbgplay("RESUME PLAYBACK\n");
  1038. /* resume playback if we were active */
  1039. spin_lock(&chip->reg_lock);
  1040. if (chip->audio_stream[AZF_PLAYBACK].running)
  1041. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
  1042. snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS) | DMA_RESUME);
  1043. spin_unlock(&chip->reg_lock);
  1044. break;
  1045. case SNDRV_PCM_TRIGGER_STOP:
  1046. snd_azf3328_dbgplay("STOP PLAYBACK\n");
  1047. /* mute WaveOut (avoid clicking during setup) */
  1048. previously_muted =
  1049. snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
  1050. spin_lock(&chip->reg_lock);
  1051. /* first, remember current value: */
  1052. status1 = snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS);
  1053. /* stop playback */
  1054. status1 &= ~DMA_RESUME;
  1055. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
  1056. /* hmm, is this really required? we're resetting the same bit
  1057. * immediately thereafter... */
  1058. status1 |= DMA_PLAY_SOMETHING1;
  1059. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
  1060. status1 &= ~DMA_PLAY_SOMETHING1;
  1061. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
  1062. spin_unlock(&chip->reg_lock);
  1063. snd_azf3328_codec_activity(chip, AZF_PLAYBACK, 0);
  1064. /* now unmute WaveOut */
  1065. if (!previously_muted)
  1066. snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);
  1067. snd_azf3328_dbgplay("STOPPED PLAYBACK\n");
  1068. break;
  1069. case SNDRV_PCM_TRIGGER_SUSPEND:
  1070. snd_azf3328_dbgplay("SUSPEND PLAYBACK\n");
  1071. /* make sure playback is stopped */
  1072. snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
  1073. snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS) & ~DMA_RESUME);
  1074. break;
  1075. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1076. snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
  1077. break;
  1078. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1079. snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
  1080. break;
  1081. default:
  1082. printk(KERN_ERR "FIXME: unknown trigger mode!\n");
  1083. return -EINVAL;
  1084. }
  1085. snd_azf3328_dbgcallleave();
  1086. return result;
  1087. }
  1088. /* this is just analogous to playback; I'm not quite sure whether recording
  1089. * should actually be triggered like that */
  1090. static int
  1091. snd_azf3328_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  1092. {
  1093. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  1094. struct snd_pcm_runtime *runtime = substream->runtime;
  1095. int result = 0;
  1096. unsigned int status1;
  1097. snd_azf3328_dbgcalls("snd_azf3328_capture_trigger cmd %d\n", cmd);
  1098. switch (cmd) {
  1099. case SNDRV_PCM_TRIGGER_START:
  1100. snd_azf3328_dbgplay("START CAPTURE\n");
  1101. snd_azf3328_codec_setfmt(chip, IDX_IO_REC_SOUNDFORMAT,
  1102. runtime->rate,
  1103. snd_pcm_format_width(runtime->format),
  1104. runtime->channels);
  1105. spin_lock(&chip->reg_lock);
  1106. /* first, remember current value: */
  1107. status1 = snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS);
  1108. /* stop recording */
  1109. status1 &= ~DMA_RESUME;
  1110. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
  1111. /* FIXME: clear interrupts or what??? */
  1112. snd_azf3328_codec_outw(chip, IDX_IO_REC_IRQTYPE, 0xffff);
  1113. spin_unlock(&chip->reg_lock);
  1114. snd_azf3328_setdmaa(chip, runtime->dma_addr,
  1115. snd_pcm_lib_period_bytes(substream),
  1116. snd_pcm_lib_buffer_bytes(substream),
  1117. AZF_CAPTURE);
  1118. spin_lock(&chip->reg_lock);
  1119. #ifdef WIN9X
  1120. /* FIXME: enable playback/recording??? */
  1121. status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
  1122. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
  1123. /* start capture again */
  1124. /* FIXME: what is this value (0x0010)??? */
  1125. status1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
  1126. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
  1127. #else
  1128. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
  1129. 0x0000);
  1130. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
  1131. DMA_PLAY_SOMETHING1);
  1132. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
  1133. DMA_PLAY_SOMETHING1 |
  1134. DMA_PLAY_SOMETHING2);
  1135. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
  1136. DMA_RESUME |
  1137. SOMETHING_ALMOST_ALWAYS_SET |
  1138. DMA_EPILOGUE_SOMETHING |
  1139. DMA_SOMETHING_ELSE);
  1140. #endif
  1141. spin_unlock(&chip->reg_lock);
  1142. snd_azf3328_codec_activity(chip, AZF_CAPTURE, 1);
  1143. snd_azf3328_dbgplay("STARTED CAPTURE\n");
  1144. break;
  1145. case SNDRV_PCM_TRIGGER_RESUME:
  1146. snd_azf3328_dbgplay("RESUME CAPTURE\n");
  1147. /* resume recording if we were active */
  1148. spin_lock(&chip->reg_lock);
  1149. if (chip->audio_stream[AZF_CAPTURE].running)
  1150. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
  1151. snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS) | DMA_RESUME);
  1152. spin_unlock(&chip->reg_lock);
  1153. break;
  1154. case SNDRV_PCM_TRIGGER_STOP:
  1155. snd_azf3328_dbgplay("STOP CAPTURE\n");
  1156. spin_lock(&chip->reg_lock);
  1157. /* first, remember current value: */
  1158. status1 = snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS);
  1159. /* stop recording */
  1160. status1 &= ~DMA_RESUME;
  1161. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
  1162. status1 |= DMA_PLAY_SOMETHING1;
  1163. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
  1164. status1 &= ~DMA_PLAY_SOMETHING1;
  1165. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
  1166. spin_unlock(&chip->reg_lock);
  1167. snd_azf3328_codec_activity(chip, AZF_CAPTURE, 0);
  1168. snd_azf3328_dbgplay("STOPPED CAPTURE\n");
  1169. break;
  1170. case SNDRV_PCM_TRIGGER_SUSPEND:
  1171. snd_azf3328_dbgplay("SUSPEND CAPTURE\n");
  1172. /* make sure recording is stopped */
  1173. snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
  1174. snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS) & ~DMA_RESUME);
  1175. break;
  1176. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1177. snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
  1178. break;
  1179. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1180. snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
  1181. break;
  1182. default:
  1183. printk(KERN_ERR "FIXME: unknown trigger mode!\n");
  1184. return -EINVAL;
  1185. }
  1186. snd_azf3328_dbgcallleave();
  1187. return result;
  1188. }
  1189. static snd_pcm_uframes_t
  1190. snd_azf3328_playback_pointer(struct snd_pcm_substream *substream)
  1191. {
  1192. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  1193. unsigned long bufptr, result;
  1194. snd_pcm_uframes_t frmres;
  1195. #ifdef QUERY_HARDWARE
  1196. bufptr = snd_azf3328_codec_inl(chip, IDX_IO_PLAY_DMA_START_1);
  1197. #else
  1198. bufptr = substream->runtime->dma_addr;
  1199. #endif
  1200. result = snd_azf3328_codec_inl(chip, IDX_IO_PLAY_DMA_CURRPOS);
  1201. /* calculate offset */
  1202. result -= bufptr;
  1203. frmres = bytes_to_frames( substream->runtime, result);
  1204. snd_azf3328_dbgplay("PLAY @ 0x%8lx, frames %8ld\n", result, frmres);
  1205. return frmres;
  1206. }
  1207. static snd_pcm_uframes_t
  1208. snd_azf3328_capture_pointer(struct snd_pcm_substream *substream)
  1209. {
  1210. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  1211. unsigned long bufptr, result;
  1212. snd_pcm_uframes_t frmres;
  1213. #ifdef QUERY_HARDWARE
  1214. bufptr = snd_azf3328_codec_inl(chip, IDX_IO_REC_DMA_START_1);
  1215. #else
  1216. bufptr = substream->runtime->dma_addr;
  1217. #endif
  1218. result = snd_azf3328_codec_inl(chip, IDX_IO_REC_DMA_CURRPOS);
  1219. /* calculate offset */
  1220. result -= bufptr;
  1221. frmres = bytes_to_frames( substream->runtime, result);
  1222. snd_azf3328_dbgplay("REC @ 0x%8lx, frames %8ld\n", result, frmres);
  1223. return frmres;
  1224. }
  1225. /******************************************************************/
  1226. #ifdef SUPPORT_GAMEPORT
  1227. static inline void
  1228. snd_azf3328_gameport_irq_enable(struct snd_azf3328 *chip, int enable)
  1229. {
  1230. snd_azf3328_io_reg_setb(
  1231. chip->game_io+IDX_GAME_HWCONFIG,
  1232. GAME_HWCFG_IRQ_ENABLE,
  1233. enable
  1234. );
  1235. }
  1236. static inline void
  1237. snd_azf3328_gameport_legacy_address_enable(struct snd_azf3328 *chip, int enable)
  1238. {
  1239. snd_azf3328_io_reg_setb(
  1240. chip->game_io+IDX_GAME_HWCONFIG,
  1241. GAME_HWCFG_LEGACY_ADDRESS_ENABLE,
  1242. enable
  1243. );
  1244. }
  1245. static inline void
  1246. snd_azf3328_gameport_axis_circuit_enable(struct snd_azf3328 *chip, int enable)
  1247. {
  1248. snd_azf3328_codec_reg_6AH_update(
  1249. chip, IO_6A_SOMETHING2_GAMEPORT, enable
  1250. );
  1251. }
  1252. static inline void
  1253. snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
  1254. {
  1255. /*
  1256. * skeleton handler only
  1257. * (we do not want axis reading in interrupt handler - too much load!)
  1258. */
  1259. snd_azf3328_dbggame("gameport irq\n");
  1260. /* this should ACK the gameport IRQ properly, hopefully. */
  1261. snd_azf3328_game_inw(chip, IDX_GAME_AXIS_VALUE);
  1262. }
  1263. static int
  1264. snd_azf3328_gameport_open(struct gameport *gameport, int mode)
  1265. {
  1266. struct snd_azf3328 *chip = gameport_get_port_data(gameport);
  1267. int res;
  1268. snd_azf3328_dbggame("gameport_open, mode %d\n", mode);
  1269. switch (mode) {
  1270. case GAMEPORT_MODE_COOKED:
  1271. case GAMEPORT_MODE_RAW:
  1272. res = 0;
  1273. break;
  1274. default:
  1275. res = -1;
  1276. break;
  1277. }
  1278. snd_azf3328_gameport_axis_circuit_enable(chip, (res == 0));
  1279. return res;
  1280. }
  1281. static void
  1282. snd_azf3328_gameport_close(struct gameport *gameport)
  1283. {
  1284. struct snd_azf3328 *chip = gameport_get_port_data(gameport);
  1285. snd_azf3328_dbggame("gameport_close\n");
  1286. snd_azf3328_gameport_axis_circuit_enable(chip, 0);
  1287. }
  1288. static int
  1289. snd_azf3328_gameport_cooked_read(struct gameport *gameport,
  1290. int *axes,
  1291. int *buttons
  1292. )
  1293. {
  1294. struct snd_azf3328 *chip = gameport_get_port_data(gameport);
  1295. int i;
  1296. u8 val;
  1297. unsigned long flags;
  1298. if (snd_BUG_ON(!chip))
  1299. return 0;
  1300. spin_lock_irqsave(&chip->reg_lock, flags);
  1301. val = snd_azf3328_game_inb(chip, IDX_GAME_LEGACY_COMPATIBLE);
  1302. *buttons = (~(val) >> 4) & 0xf;
  1303. /* ok, this one is a bit dirty: cooked_read is being polled by a timer,
  1304. * thus we're atomic and cannot actively wait in here
  1305. * (which would be useful for us since it probably would be better
  1306. * to trigger a measurement in here, then wait a short amount of
  1307. * time until it's finished, then read values of _this_ measurement).
  1308. *
  1309. * Thus we simply resort to reading values if they're available already
  1310. * and trigger the next measurement.
  1311. */
  1312. val = snd_azf3328_game_inb(chip, IDX_GAME_AXES_CONFIG);
  1313. if (val & GAME_AXES_SAMPLING_READY) {
  1314. for (i = 0; i < 4; ++i) {
  1315. /* configure the axis to read */
  1316. val = (i << 4) | 0x0f;
  1317. snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
  1318. chip->axes[i] = snd_azf3328_game_inw(
  1319. chip, IDX_GAME_AXIS_VALUE
  1320. );
  1321. }
  1322. }
  1323. /* trigger next axes sampling, to be evaluated the next time we
  1324. * enter this function */
  1325. /* for some very, very strange reason we cannot enable
  1326. * Measurement Ready monitoring for all axes here,
  1327. * at least not when only one joystick connected */
  1328. val = 0x03; /* we're able to monitor axes 1 and 2 only */
  1329. snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
  1330. snd_azf3328_game_outw(chip, IDX_GAME_AXIS_VALUE, 0xffff);
  1331. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1332. for (i = 0; i < 4; i++) {
  1333. axes[i] = chip->axes[i];
  1334. if (axes[i] == 0xffff)
  1335. axes[i] = -1;
  1336. }
  1337. snd_azf3328_dbggame("cooked_read: axes %d %d %d %d buttons %d\n",
  1338. axes[0], axes[1], axes[2], axes[3], *buttons
  1339. );
  1340. return 0;
  1341. }
  1342. static int __devinit
  1343. snd_azf3328_gameport(struct snd_azf3328 *chip, int dev)
  1344. {
  1345. struct gameport *gp;
  1346. chip->gameport = gp = gameport_allocate_port();
  1347. if (!gp) {
  1348. printk(KERN_ERR "azt3328: cannot alloc memory for gameport\n");
  1349. return -ENOMEM;
  1350. }
  1351. gameport_set_name(gp, "AZF3328 Gameport");
  1352. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  1353. gameport_set_dev_parent(gp, &chip->pci->dev);
  1354. gp->io = chip->game_io;
  1355. gameport_set_port_data(gp, chip);
  1356. gp->open = snd_azf3328_gameport_open;
  1357. gp->close = snd_azf3328_gameport_close;
  1358. gp->fuzz = 16; /* seems ok */
  1359. gp->cooked_read = snd_azf3328_gameport_cooked_read;
  1360. /* DISABLE legacy address: we don't need it! */
  1361. snd_azf3328_gameport_legacy_address_enable(chip, 0);
  1362. snd_azf3328_gameport_axis_circuit_enable(chip, 0);
  1363. gameport_register_port(chip->gameport);
  1364. return 0;
  1365. }
  1366. static void
  1367. snd_azf3328_gameport_free(struct snd_azf3328 *chip)
  1368. {
  1369. if (chip->gameport) {
  1370. gameport_unregister_port(chip->gameport);
  1371. chip->gameport = NULL;
  1372. }
  1373. snd_azf3328_gameport_irq_enable(chip, 0);
  1374. }
  1375. #else
  1376. static inline int
  1377. snd_azf3328_gameport(struct snd_azf3328 *chip, int dev) { return -ENOSYS; }
  1378. static inline void
  1379. snd_azf3328_gameport_free(struct snd_azf3328 *chip) { }
  1380. static inline void
  1381. snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
  1382. {
  1383. printk(KERN_WARNING "huh, game port IRQ occurred!?\n");
  1384. }
  1385. #endif /* SUPPORT_GAMEPORT */
  1386. /******************************************************************/
  1387. static inline void
  1388. snd_azf3328_irq_log_unknown_type(u8 which)
  1389. {
  1390. snd_azf3328_dbgplay(
  1391. "azt3328: unknown IRQ type (%x) occurred, please report!\n",
  1392. which
  1393. );
  1394. }
  1395. static irqreturn_t
  1396. snd_azf3328_interrupt(int irq, void *dev_id)
  1397. {
  1398. struct snd_azf3328 *chip = dev_id;
  1399. u8 status, which;
  1400. #if DEBUG_PLAY_REC
  1401. static unsigned long irq_count;
  1402. #endif
  1403. status = snd_azf3328_codec_inb(chip, IDX_IO_IRQSTATUS);
  1404. /* fast path out, to ease interrupt sharing */
  1405. if (!(status &
  1406. (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_GAMEPORT|IRQ_MPU401|IRQ_TIMER)
  1407. ))
  1408. return IRQ_NONE; /* must be interrupt for another device */
  1409. snd_azf3328_dbgplay(
  1410. "irq_count %ld! IDX_IO_PLAY_FLAGS %04x, "
  1411. "IDX_IO_PLAY_IRQTYPE %04x, IDX_IO_IRQSTATUS %04x\n",
  1412. irq_count++ /* debug-only */,
  1413. snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS),
  1414. snd_azf3328_codec_inw(chip, IDX_IO_PLAY_IRQTYPE),
  1415. status
  1416. );
  1417. if (status & IRQ_TIMER) {
  1418. /* snd_azf3328_dbgplay("timer %ld\n",
  1419. snd_azf3328_codec_inl(chip, IDX_IO_TIMER_VALUE)
  1420. & TIMER_VALUE_MASK
  1421. ); */
  1422. if (chip->timer)
  1423. snd_timer_interrupt(chip->timer, chip->timer->sticks);
  1424. /* ACK timer */
  1425. spin_lock(&chip->reg_lock);
  1426. snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
  1427. spin_unlock(&chip->reg_lock);
  1428. snd_azf3328_dbgplay("azt3328: timer IRQ\n");
  1429. }
  1430. if (status & IRQ_PLAYBACK) {
  1431. spin_lock(&chip->reg_lock);
  1432. which = snd_azf3328_codec_inb(chip, IDX_IO_PLAY_IRQTYPE);
  1433. /* ack all IRQ types immediately */
  1434. snd_azf3328_codec_outb(chip, IDX_IO_PLAY_IRQTYPE, which);
  1435. spin_unlock(&chip->reg_lock);
  1436. if (chip->pcm && chip->audio_stream[AZF_PLAYBACK].substream) {
  1437. snd_pcm_period_elapsed(
  1438. chip->audio_stream[AZF_PLAYBACK].substream
  1439. );
  1440. snd_azf3328_dbgplay("PLAY period done (#%x), @ %x\n",
  1441. which,
  1442. snd_azf3328_codec_inl(
  1443. chip, IDX_IO_PLAY_DMA_CURRPOS
  1444. )
  1445. );
  1446. } else
  1447. printk(KERN_WARNING "azt3328: irq handler problem!\n");
  1448. if (which & IRQ_PLAY_SOMETHING)
  1449. snd_azf3328_irq_log_unknown_type(which);
  1450. }
  1451. if (status & IRQ_RECORDING) {
  1452. spin_lock(&chip->reg_lock);
  1453. which = snd_azf3328_codec_inb(chip, IDX_IO_REC_IRQTYPE);
  1454. /* ack all IRQ types immediately */
  1455. snd_azf3328_codec_outb(chip, IDX_IO_REC_IRQTYPE, which);
  1456. spin_unlock(&chip->reg_lock);
  1457. if (chip->pcm && chip->audio_stream[AZF_CAPTURE].substream) {
  1458. snd_pcm_period_elapsed(
  1459. chip->audio_stream[AZF_CAPTURE].substream
  1460. );
  1461. snd_azf3328_dbgplay("REC period done (#%x), @ %x\n",
  1462. which,
  1463. snd_azf3328_codec_inl(
  1464. chip, IDX_IO_REC_DMA_CURRPOS
  1465. )
  1466. );
  1467. } else
  1468. printk(KERN_WARNING "azt3328: irq handler problem!\n");
  1469. if (which & IRQ_REC_SOMETHING)
  1470. snd_azf3328_irq_log_unknown_type(which);
  1471. }
  1472. if (status & IRQ_GAMEPORT)
  1473. snd_azf3328_gameport_interrupt(chip);
  1474. /* MPU401 has less critical IRQ requirements
  1475. * than timer and playback/recording, right? */
  1476. if (status & IRQ_MPU401) {
  1477. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
  1478. /* hmm, do we have to ack the IRQ here somehow?
  1479. * If so, then I don't know how... */
  1480. snd_azf3328_dbgplay("azt3328: MPU401 IRQ\n");
  1481. }
  1482. return IRQ_HANDLED;
  1483. }
  1484. /*****************************************************************/
  1485. static const struct snd_pcm_hardware snd_azf3328_playback =
  1486. {
  1487. /* FIXME!! Correct? */
  1488. .info = SNDRV_PCM_INFO_MMAP |
  1489. SNDRV_PCM_INFO_INTERLEAVED |
  1490. SNDRV_PCM_INFO_MMAP_VALID,
  1491. .formats = SNDRV_PCM_FMTBIT_S8 |
  1492. SNDRV_PCM_FMTBIT_U8 |
  1493. SNDRV_PCM_FMTBIT_S16_LE |
  1494. SNDRV_PCM_FMTBIT_U16_LE,
  1495. .rates = SNDRV_PCM_RATE_5512 |
  1496. SNDRV_PCM_RATE_8000_48000 |
  1497. SNDRV_PCM_RATE_KNOT,
  1498. .rate_min = AZF_FREQ_4000,
  1499. .rate_max = AZF_FREQ_66200,
  1500. .channels_min = 1,
  1501. .channels_max = 2,
  1502. .buffer_bytes_max = 65536,
  1503. .period_bytes_min = 64,
  1504. .period_bytes_max = 65536,
  1505. .periods_min = 1,
  1506. .periods_max = 1024,
  1507. /* FIXME: maybe that card actually has a FIFO?
  1508. * Hmm, it seems newer revisions do have one, but we still don't know
  1509. * its size... */
  1510. .fifo_size = 0,
  1511. };
  1512. static const struct snd_pcm_hardware snd_azf3328_capture =
  1513. {
  1514. /* FIXME */
  1515. .info = SNDRV_PCM_INFO_MMAP |
  1516. SNDRV_PCM_INFO_INTERLEAVED |
  1517. SNDRV_PCM_INFO_MMAP_VALID,
  1518. .formats = SNDRV_PCM_FMTBIT_S8 |
  1519. SNDRV_PCM_FMTBIT_U8 |
  1520. SNDRV_PCM_FMTBIT_S16_LE |
  1521. SNDRV_PCM_FMTBIT_U16_LE,
  1522. .rates = SNDRV_PCM_RATE_5512 |
  1523. SNDRV_PCM_RATE_8000_48000 |
  1524. SNDRV_PCM_RATE_KNOT,
  1525. .rate_min = AZF_FREQ_4000,
  1526. .rate_max = AZF_FREQ_66200,
  1527. .channels_min = 1,
  1528. .channels_max = 2,
  1529. .buffer_bytes_max = 65536,
  1530. .period_bytes_min = 64,
  1531. .period_bytes_max = 65536,
  1532. .periods_min = 1,
  1533. .periods_max = 1024,
  1534. .fifo_size = 0,
  1535. };
  1536. static unsigned int snd_azf3328_fixed_rates[] = {
  1537. AZF_FREQ_4000,
  1538. AZF_FREQ_4800,
  1539. AZF_FREQ_5512,
  1540. AZF_FREQ_6620,
  1541. AZF_FREQ_8000,
  1542. AZF_FREQ_9600,
  1543. AZF_FREQ_11025,
  1544. AZF_FREQ_13240,
  1545. AZF_FREQ_16000,
  1546. AZF_FREQ_22050,
  1547. AZF_FREQ_32000,
  1548. AZF_FREQ_44100,
  1549. AZF_FREQ_48000,
  1550. AZF_FREQ_66200
  1551. };
  1552. static struct snd_pcm_hw_constraint_list snd_azf3328_hw_constraints_rates = {
  1553. .count = ARRAY_SIZE(snd_azf3328_fixed_rates),
  1554. .list = snd_azf3328_fixed_rates,
  1555. .mask = 0,
  1556. };
  1557. /*****************************************************************/
  1558. static int
  1559. snd_azf3328_playback_open(struct snd_pcm_substream *substream)
  1560. {
  1561. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  1562. struct snd_pcm_runtime *runtime = substream->runtime;
  1563. snd_azf3328_dbgcallenter();
  1564. chip->audio_stream[AZF_PLAYBACK].substream = substream;
  1565. runtime->hw = snd_azf3328_playback;
  1566. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1567. &snd_azf3328_hw_constraints_rates);
  1568. snd_azf3328_dbgcallleave();
  1569. return 0;
  1570. }
  1571. static int
  1572. snd_azf3328_capture_open(struct snd_pcm_substream *substream)
  1573. {
  1574. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  1575. struct snd_pcm_runtime *runtime = substream->runtime;
  1576. snd_azf3328_dbgcallenter();
  1577. chip->audio_stream[AZF_CAPTURE].substream = substream;
  1578. runtime->hw = snd_azf3328_capture;
  1579. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1580. &snd_azf3328_hw_constraints_rates);
  1581. snd_azf3328_dbgcallleave();
  1582. return 0;
  1583. }
  1584. static int
  1585. snd_azf3328_playback_close(struct snd_pcm_substream *substream)
  1586. {
  1587. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  1588. snd_azf3328_dbgcallenter();
  1589. chip->audio_stream[AZF_PLAYBACK].substream = NULL;
  1590. snd_azf3328_dbgcallleave();
  1591. return 0;
  1592. }
  1593. static int
  1594. snd_azf3328_capture_close(struct snd_pcm_substream *substream)
  1595. {
  1596. struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  1597. snd_azf3328_dbgcallenter();
  1598. chip->audio_stream[AZF_CAPTURE].substream = NULL;
  1599. snd_azf3328_dbgcallleave();
  1600. return 0;
  1601. }
  1602. /******************************************************************/
  1603. static struct snd_pcm_ops snd_azf3328_playback_ops = {
  1604. .open = snd_azf3328_playback_open,
  1605. .close = snd_azf3328_playback_close,
  1606. .ioctl = snd_pcm_lib_ioctl,
  1607. .hw_params = snd_azf3328_hw_params,
  1608. .hw_free = snd_azf3328_hw_free,
  1609. .prepare = snd_azf3328_playback_prepare,
  1610. .trigger = snd_azf3328_playback_trigger,
  1611. .pointer = snd_azf3328_playback_pointer
  1612. };
  1613. static struct snd_pcm_ops snd_azf3328_capture_ops = {
  1614. .open = snd_azf3328_capture_open,
  1615. .close = snd_azf3328_capture_close,
  1616. .ioctl = snd_pcm_lib_ioctl,
  1617. .hw_params = snd_azf3328_hw_params,
  1618. .hw_free = snd_azf3328_hw_free,
  1619. .prepare = snd_azf3328_capture_prepare,
  1620. .trigger = snd_azf3328_capture_trigger,
  1621. .pointer = snd_azf3328_capture_pointer
  1622. };
  1623. static int __devinit
  1624. snd_azf3328_pcm(struct snd_azf3328 *chip, int device)
  1625. {
  1626. struct snd_pcm *pcm;
  1627. int err;
  1628. snd_azf3328_dbgcallenter();
  1629. if ((err = snd_pcm_new(chip->card, "AZF3328 DSP", device, 1, 1, &pcm)) < 0)
  1630. return err;
  1631. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_azf3328_playback_ops);
  1632. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_azf3328_capture_ops);
  1633. pcm->private_data = chip;
  1634. pcm->info_flags = 0;
  1635. strcpy(pcm->name, chip->card->shortname);
  1636. chip->pcm = pcm;
  1637. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1638. snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
  1639. snd_azf3328_dbgcallleave();
  1640. return 0;
  1641. }
  1642. /******************************************************************/
  1643. /*** NOTE: the physical timer resolution actually is 1024000 ticks per second
  1644. *** (probably derived from main crystal via a divider of 24),
  1645. *** but announcing those attributes to user-space would make programs
  1646. *** configure the timer to a 1 tick value, resulting in an absolutely fatal
  1647. *** timer IRQ storm.
  1648. *** Thus I chose to announce a down-scaled virtual timer to the outside and
  1649. *** calculate real timer countdown values internally.
  1650. *** (the scale factor can be set via module parameter "seqtimer_scaling").
  1651. ***/
  1652. static int
  1653. snd_azf3328_timer_start(struct snd_timer *timer)
  1654. {
  1655. struct snd_azf3328 *chip;
  1656. unsigned long flags;
  1657. unsigned int delay;
  1658. snd_azf3328_dbgcallenter();
  1659. chip = snd_timer_chip(timer);
  1660. delay = ((timer->sticks * seqtimer_scaling) - 1) & TIMER_VALUE_MASK;
  1661. if (delay < 49) {
  1662. /* uhoh, that's not good, since user-space won't know about
  1663. * this timing tweak
  1664. * (we need to do it to avoid a lockup, though) */
  1665. snd_azf3328_dbgtimer("delay was too low (%d)!\n", delay);
  1666. delay = 49; /* minimum time is 49 ticks */
  1667. }
  1668. snd_azf3328_dbgtimer("setting timer countdown value %d, add COUNTDOWN|IRQ\n", delay);
  1669. delay |= TIMER_COUNTDOWN_ENABLE | TIMER_IRQ_ENABLE;
  1670. spin_lock_irqsave(&chip->reg_lock, flags);
  1671. snd_azf3328_codec_outl(chip, IDX_IO_TIMER_VALUE, delay);
  1672. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1673. snd_azf3328_dbgcallleave();
  1674. return 0;
  1675. }
  1676. static int
  1677. snd_azf3328_timer_stop(struct snd_timer *timer)
  1678. {
  1679. struct snd_azf3328 *chip;
  1680. unsigned long flags;
  1681. snd_azf3328_dbgcallenter();
  1682. chip = snd_timer_chip(timer);
  1683. spin_lock_irqsave(&chip->reg_lock, flags);
  1684. /* disable timer countdown and interrupt */
  1685. /* FIXME: should we write TIMER_IRQ_ACK here? */
  1686. snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0);
  1687. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1688. snd_azf3328_dbgcallleave();
  1689. return 0;
  1690. }
  1691. static int
  1692. snd_azf3328_timer_precise_resolution(struct snd_timer *timer,
  1693. unsigned long *num, unsigned long *den)
  1694. {
  1695. snd_azf3328_dbgcallenter();
  1696. *num = 1;
  1697. *den = 1024000 / seqtimer_scaling;
  1698. snd_azf3328_dbgcallleave();
  1699. return 0;
  1700. }
  1701. static struct snd_timer_hardware snd_azf3328_timer_hw = {
  1702. .flags = SNDRV_TIMER_HW_AUTO,
  1703. .resolution = 977, /* 1000000/1024000 = 0.9765625us */
  1704. .ticks = 1024000, /* max tick count, defined by the value register; actually it's not 1024000, but 1048576, but we don't care */
  1705. .start = snd_azf3328_timer_start,
  1706. .stop = snd_azf3328_timer_stop,
  1707. .precise_resolution = snd_azf3328_timer_precise_resolution,
  1708. };
  1709. static int __devinit
  1710. snd_azf3328_timer(struct snd_azf3328 *chip, int device)
  1711. {
  1712. struct snd_timer *timer = NULL;
  1713. struct snd_timer_id tid;
  1714. int err;
  1715. snd_azf3328_dbgcallenter();
  1716. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  1717. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1718. tid.card = chip->card->number;
  1719. tid.device = device;
  1720. tid.subdevice = 0;
  1721. snd_azf3328_timer_hw.resolution *= seqtimer_scaling;
  1722. snd_azf3328_timer_hw.ticks /= seqtimer_scaling;
  1723. err = snd_timer_new(chip->card, "AZF3328", &tid, &timer);
  1724. if (err < 0)
  1725. goto out;
  1726. strcpy(timer->name, "AZF3328 timer");
  1727. timer->private_data = chip;
  1728. timer->hw = snd_azf3328_timer_hw;
  1729. chip->timer = timer;
  1730. snd_azf3328_timer_stop(timer);
  1731. err = 0;
  1732. out:
  1733. snd_azf3328_dbgcallleave();
  1734. return err;
  1735. }
  1736. /******************************************************************/
  1737. static int
  1738. snd_azf3328_free(struct snd_azf3328 *chip)
  1739. {
  1740. if (chip->irq < 0)
  1741. goto __end_hw;
  1742. /* reset (close) mixer:
  1743. * first mute master volume, then reset
  1744. */
  1745. snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1);
  1746. snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
  1747. snd_azf3328_timer_stop(chip->timer);
  1748. snd_azf3328_gameport_free(chip);
  1749. if (chip->irq >= 0)
  1750. synchronize_irq(chip->irq);
  1751. __end_hw:
  1752. if (chip->irq >= 0)
  1753. free_irq(chip->irq, chip);
  1754. pci_release_regions(chip->pci);
  1755. pci_disable_device(chip->pci);
  1756. kfree(chip);
  1757. return 0;
  1758. }
  1759. static int
  1760. snd_azf3328_dev_free(struct snd_device *device)
  1761. {
  1762. struct snd_azf3328 *chip = device->device_data;
  1763. return snd_azf3328_free(chip);
  1764. }
  1765. #if 0
  1766. /* check whether a bit can be modified */
  1767. static void
  1768. snd_azf3328_test_bit(unsigned unsigned reg, int bit)
  1769. {
  1770. unsigned char val, valoff, valon;
  1771. val = inb(reg);
  1772. outb(val & ~(1 << bit), reg);
  1773. valoff = inb(reg);
  1774. outb(val|(1 << bit), reg);
  1775. valon = inb(reg);
  1776. outb(val, reg);
  1777. printk(KERN_ERR "reg %04x bit %d: %02x %02x %02x\n",
  1778. reg, bit, val, valoff, valon
  1779. );
  1780. }
  1781. #endif
  1782. static inline void
  1783. snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip)
  1784. {
  1785. #if DEBUG_MISC
  1786. u16 tmp;
  1787. snd_azf3328_dbgmisc(
  1788. "codec_io 0x%lx, game_io 0x%lx, mpu_io 0x%lx, "
  1789. "opl3_io 0x%lx, mixer_io 0x%lx, irq %d\n",
  1790. chip->codec_io, chip->game_io, chip->mpu_io,
  1791. chip->opl3_io, chip->mixer_io, chip->irq
  1792. );
  1793. snd_azf3328_dbgmisc("game %02x %02x %02x %02x %02x %02x\n",
  1794. snd_azf3328_game_inb(chip, 0),
  1795. snd_azf3328_game_inb(chip, 1),
  1796. snd_azf3328_game_inb(chip, 2),
  1797. snd_azf3328_game_inb(chip, 3),
  1798. snd_azf3328_game_inb(chip, 4),
  1799. snd_azf3328_game_inb(chip, 5)
  1800. );
  1801. for (tmp = 0; tmp < 0x07; tmp += 1)
  1802. snd_azf3328_dbgmisc("mpu_io 0x%04x\n", inb(chip->mpu_io + tmp));
  1803. for (tmp = 0; tmp <= 0x07; tmp += 1)
  1804. snd_azf3328_dbgmisc("0x%02x: game200 0x%04x, game208 0x%04x\n",
  1805. tmp, inb(0x200 + tmp), inb(0x208 + tmp));
  1806. for (tmp = 0; tmp <= 0x01; tmp += 1)
  1807. snd_azf3328_dbgmisc(
  1808. "0x%02x: mpu300 0x%04x, mpu310 0x%04x, mpu320 0x%04x, "
  1809. "mpu330 0x%04x opl388 0x%04x opl38c 0x%04x\n",
  1810. tmp,
  1811. inb(0x300 + tmp),
  1812. inb(0x310 + tmp),
  1813. inb(0x320 + tmp),
  1814. inb(0x330 + tmp),
  1815. inb(0x388 + tmp),
  1816. inb(0x38c + tmp)
  1817. );
  1818. for (tmp = 0; tmp < AZF_IO_SIZE_CODEC; tmp += 2)
  1819. snd_azf3328_dbgmisc("codec 0x%02x: 0x%04x\n",
  1820. tmp, snd_azf3328_codec_inw(chip, tmp)
  1821. );
  1822. for (tmp = 0; tmp < AZF_IO_SIZE_MIXER; tmp += 2)
  1823. snd_azf3328_dbgmisc("mixer 0x%02x: 0x%04x\n",
  1824. tmp, snd_azf3328_mixer_inw(chip, tmp)
  1825. );
  1826. #endif /* DEBUG_MISC */
  1827. }
  1828. static int __devinit
  1829. snd_azf3328_create(struct snd_card *card,
  1830. struct pci_dev *pci,
  1831. unsigned long device_type,
  1832. struct snd_azf3328 **rchip)
  1833. {
  1834. struct snd_azf3328 *chip;
  1835. int err;
  1836. static struct snd_device_ops ops = {
  1837. .dev_free = snd_azf3328_dev_free,
  1838. };
  1839. u16 tmp;
  1840. *rchip = NULL;
  1841. err = pci_enable_device(pci);
  1842. if (err < 0)
  1843. return err;
  1844. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1845. if (chip == NULL) {
  1846. err = -ENOMEM;
  1847. goto out_err;
  1848. }
  1849. spin_lock_init(&chip->reg_lock);
  1850. chip->card = card;
  1851. chip->pci = pci;
  1852. chip->irq = -1;
  1853. /* check if we can restrict PCI DMA transfers to 24 bits */
  1854. if (pci_set_dma_mask(pci, DMA_BIT_MASK(24)) < 0 ||
  1855. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(24)) < 0) {
  1856. snd_printk(KERN_ERR "architecture does not support "
  1857. "24bit PCI busmaster DMA\n"
  1858. );
  1859. err = -ENXIO;
  1860. goto out_err;
  1861. }
  1862. err = pci_request_regions(pci, "Aztech AZF3328");
  1863. if (err < 0)
  1864. goto out_err;
  1865. chip->codec_io = pci_resource_start(pci, 0);
  1866. chip->game_io = pci_resource_start(pci, 1);
  1867. chip->mpu_io = pci_resource_start(pci, 2);
  1868. chip->opl3_io = pci_resource_start(pci, 3);
  1869. chip->mixer_io = pci_resource_start(pci, 4);
  1870. chip->audio_stream[AZF_PLAYBACK].portbase = chip->codec_io + 0x00;
  1871. chip->audio_stream[AZF_CAPTURE].portbase = chip->codec_io + 0x20;
  1872. if (request_irq(pci->irq, snd_azf3328_interrupt,
  1873. IRQF_SHARED, card->shortname, chip)) {
  1874. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1875. err = -EBUSY;
  1876. goto out_err;
  1877. }
  1878. chip->irq = pci->irq;
  1879. pci_set_master(pci);
  1880. synchronize_irq(chip->irq);
  1881. snd_azf3328_debug_show_ports(chip);
  1882. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1883. if (err < 0)
  1884. goto out_err;
  1885. /* create mixer interface & switches */
  1886. err = snd_azf3328_mixer_new(chip);
  1887. if (err < 0)
  1888. goto out_err;
  1889. /* shutdown codecs to save power */
  1890. /* have snd_azf3328_codec_activity() act properly */
  1891. chip->audio_stream[AZF_PLAYBACK].running = 1;
  1892. snd_azf3328_codec_activity(chip, AZF_PLAYBACK, 0);
  1893. /* standard chip init stuff */
  1894. /* default IRQ init value */
  1895. tmp = DMA_PLAY_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE;
  1896. spin_lock_irq(&chip->reg_lock);
  1897. snd_azf3328_codec_outb(chip, IDX_IO_PLAY_FLAGS, tmp);
  1898. snd_azf3328_codec_outb(chip, IDX_IO_REC_FLAGS, tmp);
  1899. snd_azf3328_codec_outb(chip, IDX_IO_SOMETHING_FLAGS, tmp);
  1900. spin_unlock_irq(&chip->reg_lock);
  1901. snd_card_set_dev(card, &pci->dev);
  1902. *rchip = chip;
  1903. err = 0;
  1904. goto out;
  1905. out_err:
  1906. if (chip)
  1907. snd_azf3328_free(chip);
  1908. pci_disable_device(pci);
  1909. out:
  1910. return err;
  1911. }
  1912. static int __devinit
  1913. snd_azf3328_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  1914. {
  1915. static int dev;
  1916. struct snd_card *card;
  1917. struct snd_azf3328 *chip;
  1918. struct snd_opl3 *opl3;
  1919. int err;
  1920. snd_azf3328_dbgcallenter();
  1921. if (dev >= SNDRV_CARDS)
  1922. return -ENODEV;
  1923. if (!enable[dev]) {
  1924. dev++;
  1925. return -ENOENT;
  1926. }
  1927. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  1928. if (err < 0)
  1929. return err;
  1930. strcpy(card->driver, "AZF3328");
  1931. strcpy(card->shortname, "Aztech AZF3328 (PCI168)");
  1932. err = snd_azf3328_create(card, pci, pci_id->driver_data, &chip);
  1933. if (err < 0)
  1934. goto out_err;
  1935. card->private_data = chip;
  1936. err = snd_mpu401_uart_new(
  1937. card, 0, MPU401_HW_MPU401, chip->mpu_io, MPU401_INFO_INTEGRATED,
  1938. pci->irq, 0, &chip->rmidi
  1939. );
  1940. if (err < 0) {
  1941. snd_printk(KERN_ERR "azf3328: no MPU-401 device at 0x%lx?\n",
  1942. chip->mpu_io
  1943. );
  1944. goto out_err;
  1945. }
  1946. err = snd_azf3328_timer(chip, 0);
  1947. if (err < 0)
  1948. goto out_err;
  1949. err = snd_azf3328_pcm(chip, 0);
  1950. if (err < 0)
  1951. goto out_err;
  1952. if (snd_opl3_create(card, chip->opl3_io, chip->opl3_io+2,
  1953. OPL3_HW_AUTO, 1, &opl3) < 0) {
  1954. snd_printk(KERN_ERR "azf3328: no OPL3 device at 0x%lx-0x%lx?\n",
  1955. chip->opl3_io, chip->opl3_io+2
  1956. );
  1957. } else {
  1958. /* need to use IDs 1, 2 since ID 0 is snd_azf3328_timer above */
  1959. err = snd_opl3_timer_new(opl3, 1, 2);
  1960. if (err < 0)
  1961. goto out_err;
  1962. err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  1963. if (err < 0)
  1964. goto out_err;
  1965. }
  1966. opl3->private_data = chip;
  1967. sprintf(card->longname, "%s at 0x%lx, irq %i",
  1968. card->shortname, chip->codec_io, chip->irq);
  1969. err = snd_card_register(card);
  1970. if (err < 0)
  1971. goto out_err;
  1972. #ifdef MODULE
  1973. printk(
  1974. "azt3328: Sound driver for Aztech AZF3328-based soundcards such as PCI168.\n"
  1975. "azt3328: Hardware was completely undocumented, unfortunately.\n"
  1976. "azt3328: Feel free to contact andi AT lisas.de for bug reports etc.!\n"
  1977. "azt3328: User-scalable sequencer timer set to %dHz (1024000Hz / %d).\n",
  1978. 1024000 / seqtimer_scaling, seqtimer_scaling);
  1979. #endif
  1980. snd_azf3328_gameport(chip, dev);
  1981. pci_set_drvdata(pci, card);
  1982. dev++;
  1983. err = 0;
  1984. goto out;
  1985. out_err:
  1986. snd_printk(KERN_ERR "azf3328: something failed, exiting\n");
  1987. snd_card_free(card);
  1988. out:
  1989. snd_azf3328_dbgcallleave();
  1990. return err;
  1991. }
  1992. static void __devexit
  1993. snd_azf3328_remove(struct pci_dev *pci)
  1994. {
  1995. snd_azf3328_dbgcallenter();
  1996. snd_card_free(pci_get_drvdata(pci));
  1997. pci_set_drvdata(pci, NULL);
  1998. snd_azf3328_dbgcallleave();
  1999. }
  2000. #ifdef CONFIG_PM
  2001. static int
  2002. snd_azf3328_suspend(struct pci_dev *pci, pm_message_t state)
  2003. {
  2004. struct snd_card *card = pci_get_drvdata(pci);
  2005. struct snd_azf3328 *chip = card->private_data;
  2006. unsigned reg;
  2007. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  2008. snd_pcm_suspend_all(chip->pcm);
  2009. for (reg = 0; reg < AZF_IO_SIZE_MIXER_PM / 2; ++reg)
  2010. chip->saved_regs_mixer[reg] = inw(chip->mixer_io + reg * 2);
  2011. /* make sure to disable master volume etc. to prevent looping sound */
  2012. snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1);
  2013. snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
  2014. for (reg = 0; reg < AZF_IO_SIZE_CODEC_PM / 2; ++reg)
  2015. chip->saved_regs_codec[reg] = inw(chip->codec_io + reg * 2);
  2016. /* manually store the one currently relevant write-only reg, too */
  2017. chip->saved_regs_codec[IDX_IO_6AH / 2] = chip->shadow_reg_codec_6AH;
  2018. for (reg = 0; reg < AZF_IO_SIZE_GAME_PM / 2; ++reg)
  2019. chip->saved_regs_game[reg] = inw(chip->game_io + reg * 2);
  2020. for (reg = 0; reg < AZF_IO_SIZE_MPU_PM / 2; ++reg)
  2021. chip->saved_regs_mpu[reg] = inw(chip->mpu_io + reg * 2);
  2022. for (reg = 0; reg < AZF_IO_SIZE_OPL3_PM / 2; ++reg)
  2023. chip->saved_regs_opl3[reg] = inw(chip->opl3_io + reg * 2);
  2024. pci_disable_device(pci);
  2025. pci_save_state(pci);
  2026. pci_set_power_state(pci, pci_choose_state(pci, state));
  2027. return 0;
  2028. }
  2029. static int
  2030. snd_azf3328_resume(struct pci_dev *pci)
  2031. {
  2032. struct snd_card *card = pci_get_drvdata(pci);
  2033. struct snd_azf3328 *chip = card->private_data;
  2034. unsigned reg;
  2035. pci_set_power_state(pci, PCI_D0);
  2036. pci_restore_state(pci);
  2037. if (pci_enable_device(pci) < 0) {
  2038. printk(KERN_ERR "azt3328: pci_enable_device failed, "
  2039. "disabling device\n");
  2040. snd_card_disconnect(card);
  2041. return -EIO;
  2042. }
  2043. pci_set_master(pci);
  2044. for (reg = 0; reg < AZF_IO_SIZE_GAME_PM / 2; ++reg)
  2045. outw(chip->saved_regs_game[reg], chip->game_io + reg * 2);
  2046. for (reg = 0; reg < AZF_IO_SIZE_MPU_PM / 2; ++reg)
  2047. outw(chip->saved_regs_mpu[reg], chip->mpu_io + reg * 2);
  2048. for (reg = 0; reg < AZF_IO_SIZE_OPL3_PM / 2; ++reg)
  2049. outw(chip->saved_regs_opl3[reg], chip->opl3_io + reg * 2);
  2050. for (reg = 0; reg < AZF_IO_SIZE_MIXER_PM / 2; ++reg)
  2051. outw(chip->saved_regs_mixer[reg], chip->mixer_io + reg * 2);
  2052. for (reg = 0; reg < AZF_IO_SIZE_CODEC_PM / 2; ++reg)
  2053. outw(chip->saved_regs_codec[reg], chip->codec_io + reg * 2);
  2054. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  2055. return 0;
  2056. }
  2057. #endif /* CONFIG_PM */
  2058. static struct pci_driver driver = {
  2059. .name = "AZF3328",
  2060. .id_table = snd_azf3328_ids,
  2061. .probe = snd_azf3328_probe,
  2062. .remove = __devexit_p(snd_azf3328_remove),
  2063. #ifdef CONFIG_PM
  2064. .suspend = snd_azf3328_suspend,
  2065. .resume = snd_azf3328_resume,
  2066. #endif
  2067. };
  2068. static int __init
  2069. alsa_card_azf3328_init(void)
  2070. {
  2071. int err;
  2072. snd_azf3328_dbgcallenter();
  2073. err = pci_register_driver(&driver);
  2074. snd_azf3328_dbgcallleave();
  2075. return err;
  2076. }
  2077. static void __exit
  2078. alsa_card_azf3328_exit(void)
  2079. {
  2080. snd_azf3328_dbgcallenter();
  2081. pci_unregister_driver(&driver);
  2082. snd_azf3328_dbgcallleave();
  2083. }
  2084. module_init(alsa_card_azf3328_init)
  2085. module_exit(alsa_card_azf3328_exit)