azt3328.c 82 KB

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