azt3328.c 75 KB

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