azt3328.c 82 KB

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