azt3328.c 85 KB

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