azt3328.c 73 KB

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