azt3328.c 80 KB

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