via82xx.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  1. /*
  2. * ALSA driver for VIA VT82xx (South Bridge)
  3. *
  4. * VT82C686A/B/C, VT8233A/C, VT8235
  5. *
  6. * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
  7. * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
  8. * 2002 Takashi Iwai <tiwai@suse.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. /*
  26. * Changes:
  27. *
  28. * Dec. 19, 2002 Takashi Iwai <tiwai@suse.de>
  29. * - use the DSX channels for the first pcm playback.
  30. * (on VIA8233, 8233C and 8235 only)
  31. * this will allow you play simultaneously up to 4 streams.
  32. * multi-channel playback is assigned to the second device
  33. * on these chips.
  34. * - support the secondary capture (on VIA8233/C,8235)
  35. * - SPDIF support
  36. * the DSX3 channel can be used for SPDIF output.
  37. * on VIA8233A, this channel is assigned to the second pcm
  38. * playback.
  39. * the card config of alsa-lib will assign the correct
  40. * device for applications.
  41. * - clean up the code, separate low-level initialization
  42. * routines for each chipset.
  43. *
  44. * Sep. 26, 2005 Karsten Wiese <annabellesgarden@yahoo.de>
  45. * - Optimize position calculation for the 823x chips.
  46. */
  47. #include <linux/io.h>
  48. #include <linux/delay.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/init.h>
  51. #include <linux/pci.h>
  52. #include <linux/slab.h>
  53. #include <linux/gameport.h>
  54. #include <linux/module.h>
  55. #include <sound/core.h>
  56. #include <sound/pcm.h>
  57. #include <sound/pcm_params.h>
  58. #include <sound/info.h>
  59. #include <sound/tlv.h>
  60. #include <sound/ac97_codec.h>
  61. #include <sound/mpu401.h>
  62. #include <sound/initval.h>
  63. #if 0
  64. #define POINTER_DEBUG
  65. #endif
  66. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  67. MODULE_DESCRIPTION("VIA VT82xx audio");
  68. MODULE_LICENSE("GPL");
  69. MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
  70. #if IS_REACHABLE(CONFIG_GAMEPORT)
  71. #define SUPPORT_JOYSTICK 1
  72. #endif
  73. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  74. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  75. static long mpu_port;
  76. #ifdef SUPPORT_JOYSTICK
  77. static bool joystick;
  78. #endif
  79. static int ac97_clock = 48000;
  80. static char *ac97_quirk;
  81. static int dxs_support;
  82. static int dxs_init_volume = 31;
  83. static int nodelay;
  84. module_param(index, int, 0444);
  85. MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
  86. module_param(id, charp, 0444);
  87. MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
  88. module_param_hw(mpu_port, long, ioport, 0444);
  89. MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
  90. #ifdef SUPPORT_JOYSTICK
  91. module_param(joystick, bool, 0444);
  92. MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
  93. #endif
  94. module_param(ac97_clock, int, 0444);
  95. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  96. module_param(ac97_quirk, charp, 0444);
  97. MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
  98. module_param(dxs_support, int, 0444);
  99. MODULE_PARM_DESC(dxs_support, "Support for DXS channels (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA, 5 = enable any sample rate)");
  100. module_param(dxs_init_volume, int, 0644);
  101. MODULE_PARM_DESC(dxs_init_volume, "initial DXS volume (0-31)");
  102. module_param(nodelay, int, 0444);
  103. MODULE_PARM_DESC(nodelay, "Disable 500ms init delay");
  104. /* just for backward compatibility */
  105. static bool enable;
  106. module_param(enable, bool, 0444);
  107. /* revision numbers for via686 */
  108. #define VIA_REV_686_A 0x10
  109. #define VIA_REV_686_B 0x11
  110. #define VIA_REV_686_C 0x12
  111. #define VIA_REV_686_D 0x13
  112. #define VIA_REV_686_E 0x14
  113. #define VIA_REV_686_H 0x20
  114. /* revision numbers for via8233 */
  115. #define VIA_REV_PRE_8233 0x10 /* not in market */
  116. #define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
  117. #define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
  118. #define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
  119. #define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
  120. #define VIA_REV_8237 0x60
  121. #define VIA_REV_8251 0x70
  122. /*
  123. * Direct registers
  124. */
  125. #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
  126. #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
  127. /* common offsets */
  128. #define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
  129. #define VIA_REG_STAT_ACTIVE 0x80 /* RO */
  130. #define VIA8233_SHADOW_STAT_ACTIVE 0x08 /* RO */
  131. #define VIA_REG_STAT_PAUSED 0x40 /* RO */
  132. #define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
  133. #define VIA_REG_STAT_STOPPED 0x04 /* RWC */
  134. #define VIA_REG_STAT_EOL 0x02 /* RWC */
  135. #define VIA_REG_STAT_FLAG 0x01 /* RWC */
  136. #define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
  137. #define VIA_REG_CTRL_START 0x80 /* WO */
  138. #define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
  139. #define VIA_REG_CTRL_AUTOSTART 0x20
  140. #define VIA_REG_CTRL_PAUSE 0x08 /* RW */
  141. #define VIA_REG_CTRL_INT_STOP 0x04
  142. #define VIA_REG_CTRL_INT_EOL 0x02
  143. #define VIA_REG_CTRL_INT_FLAG 0x01
  144. #define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
  145. #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
  146. #define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
  147. #define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
  148. #define VIA_REG_TYPE_16BIT 0x20 /* RW */
  149. #define VIA_REG_TYPE_STEREO 0x10 /* RW */
  150. #define VIA_REG_TYPE_INT_LLINE 0x00
  151. #define VIA_REG_TYPE_INT_LSAMPLE 0x04
  152. #define VIA_REG_TYPE_INT_LESSONE 0x08
  153. #define VIA_REG_TYPE_INT_MASK 0x0c
  154. #define VIA_REG_TYPE_INT_EOL 0x02
  155. #define VIA_REG_TYPE_INT_FLAG 0x01
  156. #define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
  157. #define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
  158. #define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
  159. #define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
  160. #define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
  161. #define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
  162. #define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
  163. #define DEFINE_VIA_REGSET(name,val) \
  164. enum {\
  165. VIA_REG_##name##_STATUS = (val),\
  166. VIA_REG_##name##_CONTROL = (val) + 0x01,\
  167. VIA_REG_##name##_TYPE = (val) + 0x02,\
  168. VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
  169. VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
  170. VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
  171. VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
  172. }
  173. /* playback block */
  174. DEFINE_VIA_REGSET(PLAYBACK, 0x00);
  175. DEFINE_VIA_REGSET(CAPTURE, 0x10);
  176. DEFINE_VIA_REGSET(FM, 0x20);
  177. /* AC'97 */
  178. #define VIA_REG_AC97 0x80 /* dword */
  179. #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
  180. #define VIA_REG_AC97_CODEC_ID_SHIFT 30
  181. #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
  182. #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
  183. #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
  184. #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
  185. #define VIA_REG_AC97_BUSY (1<<24)
  186. #define VIA_REG_AC97_READ (1<<23)
  187. #define VIA_REG_AC97_CMD_SHIFT 16
  188. #define VIA_REG_AC97_CMD_MASK 0x7e
  189. #define VIA_REG_AC97_DATA_SHIFT 0
  190. #define VIA_REG_AC97_DATA_MASK 0xffff
  191. #define VIA_REG_SGD_SHADOW 0x84 /* dword */
  192. /* via686 */
  193. #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
  194. #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
  195. #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
  196. #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
  197. #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
  198. #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
  199. #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
  200. #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
  201. #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
  202. #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
  203. #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
  204. #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
  205. /* via8233 */
  206. #define VIA8233_REG_SGD_STAT_FLAG (1<<0)
  207. #define VIA8233_REG_SGD_STAT_EOL (1<<1)
  208. #define VIA8233_REG_SGD_STAT_STOP (1<<2)
  209. #define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
  210. #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
  211. #define VIA8233_REG_SGD_CHAN_SDX 0
  212. #define VIA8233_REG_SGD_CHAN_MULTI 4
  213. #define VIA8233_REG_SGD_CHAN_REC 6
  214. #define VIA8233_REG_SGD_CHAN_REC1 7
  215. #define VIA_REG_GPI_STATUS 0x88
  216. #define VIA_REG_GPI_INTR 0x8c
  217. /* multi-channel and capture registers for via8233 */
  218. DEFINE_VIA_REGSET(MULTPLAY, 0x40);
  219. DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
  220. /* via8233-specific registers */
  221. #define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
  222. #define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
  223. #define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
  224. #define VIA_REG_MULTPLAY_FMT_8BIT 0x00
  225. #define VIA_REG_MULTPLAY_FMT_16BIT 0x80
  226. #define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
  227. #define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
  228. #define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
  229. #define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
  230. #define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
  231. #define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
  232. #define VIA_REG_CAPTURE_CHANNEL_LINE 0
  233. #define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
  234. #define VIA_TBL_BIT_FLAG 0x40000000
  235. #define VIA_TBL_BIT_EOL 0x80000000
  236. /* pci space */
  237. #define VIA_ACLINK_STAT 0x40
  238. #define VIA_ACLINK_C11_READY 0x20
  239. #define VIA_ACLINK_C10_READY 0x10
  240. #define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
  241. #define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
  242. #define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
  243. #define VIA_ACLINK_CTRL 0x41
  244. #define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
  245. #define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
  246. #define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
  247. #define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
  248. #define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
  249. #define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
  250. #define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
  251. #define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
  252. #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
  253. VIA_ACLINK_CTRL_RESET|\
  254. VIA_ACLINK_CTRL_PCM|\
  255. VIA_ACLINK_CTRL_VRA)
  256. #define VIA_FUNC_ENABLE 0x42
  257. #define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
  258. #define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
  259. #define VIA_FUNC_RX2C_WRITE 0x20
  260. #define VIA_FUNC_SB_FIFO_EMPTY 0x10
  261. #define VIA_FUNC_ENABLE_GAME 0x08
  262. #define VIA_FUNC_ENABLE_FM 0x04
  263. #define VIA_FUNC_ENABLE_MIDI 0x02
  264. #define VIA_FUNC_ENABLE_SB 0x01
  265. #define VIA_PNP_CONTROL 0x43
  266. #define VIA_FM_NMI_CTRL 0x48
  267. #define VIA8233_VOLCHG_CTRL 0x48
  268. #define VIA8233_SPDIF_CTRL 0x49
  269. #define VIA8233_SPDIF_DX3 0x08
  270. #define VIA8233_SPDIF_SLOT_MASK 0x03
  271. #define VIA8233_SPDIF_SLOT_1011 0x00
  272. #define VIA8233_SPDIF_SLOT_34 0x01
  273. #define VIA8233_SPDIF_SLOT_78 0x02
  274. #define VIA8233_SPDIF_SLOT_69 0x03
  275. /*
  276. */
  277. #define VIA_DXS_AUTO 0
  278. #define VIA_DXS_ENABLE 1
  279. #define VIA_DXS_DISABLE 2
  280. #define VIA_DXS_48K 3
  281. #define VIA_DXS_NO_VRA 4
  282. #define VIA_DXS_SRC 5
  283. /*
  284. * pcm stream
  285. */
  286. struct snd_via_sg_table {
  287. unsigned int offset;
  288. unsigned int size;
  289. } ;
  290. #define VIA_TABLE_SIZE 255
  291. #define VIA_MAX_BUFSIZE (1<<24)
  292. struct viadev {
  293. unsigned int reg_offset;
  294. unsigned long port;
  295. int direction; /* playback = 0, capture = 1 */
  296. struct snd_pcm_substream *substream;
  297. int running;
  298. unsigned int tbl_entries; /* # descriptors */
  299. struct snd_dma_buffer table;
  300. struct snd_via_sg_table *idx_table;
  301. /* for recovery from the unexpected pointer */
  302. unsigned int lastpos;
  303. unsigned int fragsize;
  304. unsigned int bufsize;
  305. unsigned int bufsize2;
  306. int hwptr_done; /* processed frame position in the buffer */
  307. int in_interrupt;
  308. int shadow_shift;
  309. };
  310. enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
  311. enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
  312. #define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
  313. struct via_rate_lock {
  314. spinlock_t lock;
  315. int rate;
  316. int used;
  317. };
  318. struct via82xx {
  319. int irq;
  320. unsigned long port;
  321. struct resource *mpu_res;
  322. int chip_type;
  323. unsigned char revision;
  324. unsigned char old_legacy;
  325. unsigned char old_legacy_cfg;
  326. #ifdef CONFIG_PM_SLEEP
  327. unsigned char legacy_saved;
  328. unsigned char legacy_cfg_saved;
  329. unsigned char spdif_ctrl_saved;
  330. unsigned char capture_src_saved[2];
  331. unsigned int mpu_port_saved;
  332. #endif
  333. unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
  334. unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
  335. unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
  336. struct pci_dev *pci;
  337. struct snd_card *card;
  338. unsigned int num_devs;
  339. unsigned int playback_devno, multi_devno, capture_devno;
  340. struct viadev devs[VIA_MAX_DEVS];
  341. struct via_rate_lock rates[2]; /* playback and capture */
  342. unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
  343. unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
  344. unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
  345. unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
  346. struct snd_pcm *pcms[2];
  347. struct snd_rawmidi *rmidi;
  348. struct snd_kcontrol *dxs_controls[4];
  349. struct snd_ac97_bus *ac97_bus;
  350. struct snd_ac97 *ac97;
  351. unsigned int ac97_clock;
  352. unsigned int ac97_secondary; /* secondary AC'97 codec is present */
  353. spinlock_t reg_lock;
  354. struct snd_info_entry *proc_entry;
  355. #ifdef SUPPORT_JOYSTICK
  356. struct gameport *gameport;
  357. #endif
  358. };
  359. static const struct pci_device_id snd_via82xx_ids[] = {
  360. /* 0x1106, 0x3058 */
  361. { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_82C686_5), TYPE_CARD_VIA686, }, /* 686A */
  362. /* 0x1106, 0x3059 */
  363. { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_8233_5), TYPE_CARD_VIA8233, }, /* VT8233 */
  364. { 0, }
  365. };
  366. MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
  367. /*
  368. */
  369. /*
  370. * allocate and initialize the descriptor buffers
  371. * periods = number of periods
  372. * fragsize = period size in bytes
  373. */
  374. static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
  375. struct pci_dev *pci,
  376. unsigned int periods, unsigned int fragsize)
  377. {
  378. unsigned int i, idx, ofs, rest;
  379. struct via82xx *chip = snd_pcm_substream_chip(substream);
  380. if (dev->table.area == NULL) {
  381. /* the start of each lists must be aligned to 8 bytes,
  382. * but the kernel pages are much bigger, so we don't care
  383. */
  384. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  385. PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
  386. &dev->table) < 0)
  387. return -ENOMEM;
  388. }
  389. if (! dev->idx_table) {
  390. dev->idx_table = kmalloc_array(VIA_TABLE_SIZE,
  391. sizeof(*dev->idx_table),
  392. GFP_KERNEL);
  393. if (! dev->idx_table)
  394. return -ENOMEM;
  395. }
  396. /* fill the entries */
  397. idx = 0;
  398. ofs = 0;
  399. for (i = 0; i < periods; i++) {
  400. rest = fragsize;
  401. /* fill descriptors for a period.
  402. * a period can be split to several descriptors if it's
  403. * over page boundary.
  404. */
  405. do {
  406. unsigned int r;
  407. unsigned int flag;
  408. unsigned int addr;
  409. if (idx >= VIA_TABLE_SIZE) {
  410. dev_err(&pci->dev, "too much table size!\n");
  411. return -EINVAL;
  412. }
  413. addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  414. ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32(addr);
  415. r = snd_pcm_sgbuf_get_chunk_size(substream, ofs, rest);
  416. rest -= r;
  417. if (! rest) {
  418. if (i == periods - 1)
  419. flag = VIA_TBL_BIT_EOL; /* buffer boundary */
  420. else
  421. flag = VIA_TBL_BIT_FLAG; /* period boundary */
  422. } else
  423. flag = 0; /* period continues to the next */
  424. /*
  425. dev_dbg(&pci->dev,
  426. "tbl %d: at %d size %d (rest %d)\n",
  427. idx, ofs, r, rest);
  428. */
  429. ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
  430. dev->idx_table[idx].offset = ofs;
  431. dev->idx_table[idx].size = r;
  432. ofs += r;
  433. idx++;
  434. } while (rest > 0);
  435. }
  436. dev->tbl_entries = idx;
  437. dev->bufsize = periods * fragsize;
  438. dev->bufsize2 = dev->bufsize / 2;
  439. dev->fragsize = fragsize;
  440. return 0;
  441. }
  442. static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
  443. struct pci_dev *pci)
  444. {
  445. if (dev->table.area) {
  446. snd_dma_free_pages(&dev->table);
  447. dev->table.area = NULL;
  448. }
  449. kfree(dev->idx_table);
  450. dev->idx_table = NULL;
  451. return 0;
  452. }
  453. /*
  454. * Basic I/O
  455. */
  456. static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
  457. {
  458. return inl(VIAREG(chip, AC97));
  459. }
  460. static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
  461. {
  462. outl(val, VIAREG(chip, AC97));
  463. }
  464. static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
  465. {
  466. unsigned int timeout = 1000; /* 1ms */
  467. unsigned int val;
  468. while (timeout-- > 0) {
  469. udelay(1);
  470. if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
  471. return val & 0xffff;
  472. }
  473. dev_err(chip->card->dev, "codec_ready: codec %i is not ready [0x%x]\n",
  474. secondary, snd_via82xx_codec_xread(chip));
  475. return -EIO;
  476. }
  477. static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
  478. {
  479. unsigned int timeout = 1000; /* 1ms */
  480. unsigned int val, val1;
  481. unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
  482. VIA_REG_AC97_SECONDARY_VALID;
  483. while (timeout-- > 0) {
  484. val = snd_via82xx_codec_xread(chip);
  485. val1 = val & (VIA_REG_AC97_BUSY | stat);
  486. if (val1 == stat)
  487. return val & 0xffff;
  488. udelay(1);
  489. }
  490. return -EIO;
  491. }
  492. static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
  493. {
  494. struct via82xx *chip = ac97->private_data;
  495. int err;
  496. err = snd_via82xx_codec_ready(chip, ac97->num);
  497. /* here we need to wait fairly for long time.. */
  498. if (!nodelay)
  499. msleep(500);
  500. }
  501. static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
  502. unsigned short reg,
  503. unsigned short val)
  504. {
  505. struct via82xx *chip = ac97->private_data;
  506. unsigned int xval;
  507. xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
  508. xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
  509. xval |= reg << VIA_REG_AC97_CMD_SHIFT;
  510. xval |= val << VIA_REG_AC97_DATA_SHIFT;
  511. snd_via82xx_codec_xwrite(chip, xval);
  512. snd_via82xx_codec_ready(chip, ac97->num);
  513. }
  514. static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
  515. {
  516. struct via82xx *chip = ac97->private_data;
  517. unsigned int xval, val = 0xffff;
  518. int again = 0;
  519. xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
  520. xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
  521. xval |= VIA_REG_AC97_READ;
  522. xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
  523. while (1) {
  524. if (again++ > 3) {
  525. dev_err(chip->card->dev,
  526. "codec_read: codec %i is not valid [0x%x]\n",
  527. ac97->num, snd_via82xx_codec_xread(chip));
  528. return 0xffff;
  529. }
  530. snd_via82xx_codec_xwrite(chip, xval);
  531. udelay (20);
  532. if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
  533. udelay(25);
  534. val = snd_via82xx_codec_xread(chip);
  535. break;
  536. }
  537. }
  538. return val & 0xffff;
  539. }
  540. static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
  541. {
  542. outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
  543. VIADEV_REG(viadev, OFFSET_CONTROL));
  544. inb(VIADEV_REG(viadev, OFFSET_CONTROL));
  545. udelay(50);
  546. /* disable interrupts */
  547. outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
  548. /* clear interrupts */
  549. outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
  550. outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
  551. // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
  552. viadev->lastpos = 0;
  553. viadev->hwptr_done = 0;
  554. }
  555. /*
  556. * Interrupt handler
  557. * Used for 686 and 8233A
  558. */
  559. static irqreturn_t snd_via686_interrupt(int irq, void *dev_id)
  560. {
  561. struct via82xx *chip = dev_id;
  562. unsigned int status;
  563. unsigned int i;
  564. status = inl(VIAREG(chip, SGD_SHADOW));
  565. if (! (status & chip->intr_mask)) {
  566. if (chip->rmidi)
  567. /* check mpu401 interrupt */
  568. return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
  569. return IRQ_NONE;
  570. }
  571. /* check status for each stream */
  572. spin_lock(&chip->reg_lock);
  573. for (i = 0; i < chip->num_devs; i++) {
  574. struct viadev *viadev = &chip->devs[i];
  575. unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
  576. if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
  577. continue;
  578. if (viadev->substream && viadev->running) {
  579. /*
  580. * Update hwptr_done based on 'period elapsed'
  581. * interrupts. We'll use it, when the chip returns 0
  582. * for OFFSET_CURR_COUNT.
  583. */
  584. if (c_status & VIA_REG_STAT_EOL)
  585. viadev->hwptr_done = 0;
  586. else
  587. viadev->hwptr_done += viadev->fragsize;
  588. viadev->in_interrupt = c_status;
  589. spin_unlock(&chip->reg_lock);
  590. snd_pcm_period_elapsed(viadev->substream);
  591. spin_lock(&chip->reg_lock);
  592. viadev->in_interrupt = 0;
  593. }
  594. outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
  595. }
  596. spin_unlock(&chip->reg_lock);
  597. return IRQ_HANDLED;
  598. }
  599. /*
  600. * Interrupt handler
  601. */
  602. static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id)
  603. {
  604. struct via82xx *chip = dev_id;
  605. unsigned int status;
  606. unsigned int i;
  607. int irqreturn = 0;
  608. /* check status for each stream */
  609. spin_lock(&chip->reg_lock);
  610. status = inl(VIAREG(chip, SGD_SHADOW));
  611. for (i = 0; i < chip->num_devs; i++) {
  612. struct viadev *viadev = &chip->devs[i];
  613. struct snd_pcm_substream *substream;
  614. unsigned char c_status, shadow_status;
  615. shadow_status = (status >> viadev->shadow_shift) &
  616. (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
  617. VIA_REG_STAT_FLAG);
  618. c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
  619. if (!c_status)
  620. continue;
  621. substream = viadev->substream;
  622. if (substream && viadev->running) {
  623. /*
  624. * Update hwptr_done based on 'period elapsed'
  625. * interrupts. We'll use it, when the chip returns 0
  626. * for OFFSET_CURR_COUNT.
  627. */
  628. if (c_status & VIA_REG_STAT_EOL)
  629. viadev->hwptr_done = 0;
  630. else
  631. viadev->hwptr_done += viadev->fragsize;
  632. viadev->in_interrupt = c_status;
  633. if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
  634. viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
  635. spin_unlock(&chip->reg_lock);
  636. snd_pcm_period_elapsed(substream);
  637. spin_lock(&chip->reg_lock);
  638. viadev->in_interrupt = 0;
  639. }
  640. outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
  641. irqreturn = 1;
  642. }
  643. spin_unlock(&chip->reg_lock);
  644. return IRQ_RETVAL(irqreturn);
  645. }
  646. /*
  647. * PCM callbacks
  648. */
  649. /*
  650. * trigger callback
  651. */
  652. static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  653. {
  654. struct via82xx *chip = snd_pcm_substream_chip(substream);
  655. struct viadev *viadev = substream->runtime->private_data;
  656. unsigned char val;
  657. if (chip->chip_type != TYPE_VIA686)
  658. val = VIA_REG_CTRL_INT;
  659. else
  660. val = 0;
  661. switch (cmd) {
  662. case SNDRV_PCM_TRIGGER_START:
  663. case SNDRV_PCM_TRIGGER_RESUME:
  664. val |= VIA_REG_CTRL_START;
  665. viadev->running = 1;
  666. break;
  667. case SNDRV_PCM_TRIGGER_STOP:
  668. case SNDRV_PCM_TRIGGER_SUSPEND:
  669. val = VIA_REG_CTRL_TERMINATE;
  670. viadev->running = 0;
  671. break;
  672. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  673. val |= VIA_REG_CTRL_PAUSE;
  674. viadev->running = 0;
  675. break;
  676. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  677. viadev->running = 1;
  678. break;
  679. default:
  680. return -EINVAL;
  681. }
  682. outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
  683. if (cmd == SNDRV_PCM_TRIGGER_STOP)
  684. snd_via82xx_channel_reset(chip, viadev);
  685. return 0;
  686. }
  687. /*
  688. * pointer callbacks
  689. */
  690. /*
  691. * calculate the linear position at the given sg-buffer index and the rest count
  692. */
  693. #define check_invalid_pos(viadev,pos) \
  694. ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
  695. viadev->lastpos < viadev->bufsize2))
  696. static inline unsigned int calc_linear_pos(struct via82xx *chip,
  697. struct viadev *viadev,
  698. unsigned int idx,
  699. unsigned int count)
  700. {
  701. unsigned int size, base, res;
  702. size = viadev->idx_table[idx].size;
  703. base = viadev->idx_table[idx].offset;
  704. res = base + size - count;
  705. if (res >= viadev->bufsize)
  706. res -= viadev->bufsize;
  707. /* check the validity of the calculated position */
  708. if (size < count) {
  709. dev_dbg(chip->card->dev,
  710. "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
  711. (int)size, (int)count);
  712. res = viadev->lastpos;
  713. } else {
  714. if (! count) {
  715. /* Some mobos report count = 0 on the DMA boundary,
  716. * i.e. count = size indeed.
  717. * Let's check whether this step is above the expected size.
  718. */
  719. int delta = res - viadev->lastpos;
  720. if (delta < 0)
  721. delta += viadev->bufsize;
  722. if ((unsigned int)delta > viadev->fragsize)
  723. res = base;
  724. }
  725. if (check_invalid_pos(viadev, res)) {
  726. #ifdef POINTER_DEBUG
  727. dev_dbg(chip->card->dev,
  728. "fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n",
  729. idx, viadev->tbl_entries,
  730. viadev->lastpos, viadev->bufsize2,
  731. viadev->idx_table[idx].offset,
  732. viadev->idx_table[idx].size, count);
  733. #endif
  734. /* count register returns full size when end of buffer is reached */
  735. res = base + size;
  736. if (check_invalid_pos(viadev, res)) {
  737. dev_dbg(chip->card->dev,
  738. "invalid via82xx_cur_ptr (2), using last valid pointer\n");
  739. res = viadev->lastpos;
  740. }
  741. }
  742. }
  743. return res;
  744. }
  745. /*
  746. * get the current pointer on via686
  747. */
  748. static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
  749. {
  750. struct via82xx *chip = snd_pcm_substream_chip(substream);
  751. struct viadev *viadev = substream->runtime->private_data;
  752. unsigned int idx, ptr, count, res;
  753. if (snd_BUG_ON(!viadev->tbl_entries))
  754. return 0;
  755. if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
  756. return 0;
  757. spin_lock(&chip->reg_lock);
  758. count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
  759. /* The via686a does not have the current index register,
  760. * so we need to calculate the index from CURR_PTR.
  761. */
  762. ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
  763. if (ptr <= (unsigned int)viadev->table.addr)
  764. idx = 0;
  765. else /* CURR_PTR holds the address + 8 */
  766. idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
  767. res = calc_linear_pos(chip, viadev, idx, count);
  768. viadev->lastpos = res; /* remember the last position */
  769. spin_unlock(&chip->reg_lock);
  770. return bytes_to_frames(substream->runtime, res);
  771. }
  772. /*
  773. * get the current pointer on via823x
  774. */
  775. static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
  776. {
  777. struct via82xx *chip = snd_pcm_substream_chip(substream);
  778. struct viadev *viadev = substream->runtime->private_data;
  779. unsigned int idx, count, res;
  780. int status;
  781. if (snd_BUG_ON(!viadev->tbl_entries))
  782. return 0;
  783. spin_lock(&chip->reg_lock);
  784. count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
  785. status = viadev->in_interrupt;
  786. if (!status)
  787. status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
  788. /* An apparent bug in the 8251 is worked around by sending a
  789. * REG_CTRL_START. */
  790. if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
  791. snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
  792. if (!(status & VIA_REG_STAT_ACTIVE)) {
  793. res = 0;
  794. goto unlock;
  795. }
  796. if (count & 0xffffff) {
  797. idx = count >> 24;
  798. if (idx >= viadev->tbl_entries) {
  799. #ifdef POINTER_DEBUG
  800. dev_dbg(chip->card->dev,
  801. "fail: invalid idx = %i/%i\n", idx,
  802. viadev->tbl_entries);
  803. #endif
  804. res = viadev->lastpos;
  805. } else {
  806. count &= 0xffffff;
  807. res = calc_linear_pos(chip, viadev, idx, count);
  808. }
  809. } else {
  810. res = viadev->hwptr_done;
  811. if (!viadev->in_interrupt) {
  812. if (status & VIA_REG_STAT_EOL) {
  813. res = 0;
  814. } else
  815. if (status & VIA_REG_STAT_FLAG) {
  816. res += viadev->fragsize;
  817. }
  818. }
  819. }
  820. unlock:
  821. viadev->lastpos = res;
  822. spin_unlock(&chip->reg_lock);
  823. return bytes_to_frames(substream->runtime, res);
  824. }
  825. /*
  826. * hw_params callback:
  827. * allocate the buffer and build up the buffer description table
  828. */
  829. static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
  830. struct snd_pcm_hw_params *hw_params)
  831. {
  832. struct via82xx *chip = snd_pcm_substream_chip(substream);
  833. struct viadev *viadev = substream->runtime->private_data;
  834. int err;
  835. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  836. if (err < 0)
  837. return err;
  838. err = build_via_table(viadev, substream, chip->pci,
  839. params_periods(hw_params),
  840. params_period_bytes(hw_params));
  841. if (err < 0)
  842. return err;
  843. return 0;
  844. }
  845. /*
  846. * hw_free callback:
  847. * clean up the buffer description table and release the buffer
  848. */
  849. static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
  850. {
  851. struct via82xx *chip = snd_pcm_substream_chip(substream);
  852. struct viadev *viadev = substream->runtime->private_data;
  853. clean_via_table(viadev, substream, chip->pci);
  854. snd_pcm_lib_free_pages(substream);
  855. return 0;
  856. }
  857. /*
  858. * set up the table pointer
  859. */
  860. static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
  861. {
  862. snd_via82xx_codec_ready(chip, 0);
  863. outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
  864. udelay(20);
  865. snd_via82xx_codec_ready(chip, 0);
  866. }
  867. /*
  868. * prepare callback for playback and capture on via686
  869. */
  870. static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
  871. struct snd_pcm_runtime *runtime)
  872. {
  873. snd_via82xx_channel_reset(chip, viadev);
  874. /* this must be set after channel_reset */
  875. snd_via82xx_set_table_ptr(chip, viadev);
  876. outb(VIA_REG_TYPE_AUTOSTART |
  877. (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
  878. (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
  879. ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
  880. VIA_REG_TYPE_INT_EOL |
  881. VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
  882. }
  883. static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
  884. {
  885. struct via82xx *chip = snd_pcm_substream_chip(substream);
  886. struct viadev *viadev = substream->runtime->private_data;
  887. struct snd_pcm_runtime *runtime = substream->runtime;
  888. snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
  889. snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
  890. via686_setup_format(chip, viadev, runtime);
  891. return 0;
  892. }
  893. static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
  894. {
  895. struct via82xx *chip = snd_pcm_substream_chip(substream);
  896. struct viadev *viadev = substream->runtime->private_data;
  897. struct snd_pcm_runtime *runtime = substream->runtime;
  898. snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  899. via686_setup_format(chip, viadev, runtime);
  900. return 0;
  901. }
  902. /*
  903. * lock the current rate
  904. */
  905. static int via_lock_rate(struct via_rate_lock *rec, int rate)
  906. {
  907. int changed = 0;
  908. spin_lock_irq(&rec->lock);
  909. if (rec->rate != rate) {
  910. if (rec->rate && rec->used > 1) /* already set */
  911. changed = -EINVAL;
  912. else {
  913. rec->rate = rate;
  914. changed = 1;
  915. }
  916. }
  917. spin_unlock_irq(&rec->lock);
  918. return changed;
  919. }
  920. /*
  921. * prepare callback for DSX playback on via823x
  922. */
  923. static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
  924. {
  925. struct via82xx *chip = snd_pcm_substream_chip(substream);
  926. struct viadev *viadev = substream->runtime->private_data;
  927. struct snd_pcm_runtime *runtime = substream->runtime;
  928. int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
  929. int rate_changed;
  930. u32 rbits;
  931. if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
  932. return rate_changed;
  933. if (rate_changed)
  934. snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
  935. chip->no_vra ? 48000 : runtime->rate);
  936. if (chip->spdif_on && viadev->reg_offset == 0x30)
  937. snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
  938. if (runtime->rate == 48000)
  939. rbits = 0xfffff;
  940. else
  941. rbits = (0x100000 / 48000) * runtime->rate +
  942. ((0x100000 % 48000) * runtime->rate) / 48000;
  943. snd_BUG_ON(rbits & ~0xfffff);
  944. snd_via82xx_channel_reset(chip, viadev);
  945. snd_via82xx_set_table_ptr(chip, viadev);
  946. outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
  947. VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
  948. outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
  949. VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
  950. outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
  951. (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
  952. rbits | /* rate */
  953. 0xff000000, /* STOP index is never reached */
  954. VIADEV_REG(viadev, OFFSET_STOP_IDX));
  955. udelay(20);
  956. snd_via82xx_codec_ready(chip, 0);
  957. return 0;
  958. }
  959. /*
  960. * prepare callback for multi-channel playback on via823x
  961. */
  962. static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
  963. {
  964. struct via82xx *chip = snd_pcm_substream_chip(substream);
  965. struct viadev *viadev = substream->runtime->private_data;
  966. struct snd_pcm_runtime *runtime = substream->runtime;
  967. unsigned int slots;
  968. int fmt;
  969. if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
  970. return -EINVAL;
  971. snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
  972. snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
  973. snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
  974. snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
  975. snd_via82xx_channel_reset(chip, viadev);
  976. snd_via82xx_set_table_ptr(chip, viadev);
  977. fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
  978. VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
  979. fmt |= runtime->channels << 4;
  980. outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
  981. #if 0
  982. if (chip->revision == VIA_REV_8233A)
  983. slots = 0;
  984. else
  985. #endif
  986. {
  987. /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
  988. /* corresponding to FL, FR, RL, RR, C, LFE ?? */
  989. switch (runtime->channels) {
  990. case 1: slots = (1<<0) | (1<<4); break;
  991. case 2: slots = (1<<0) | (2<<4); break;
  992. case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
  993. case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
  994. case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
  995. case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
  996. default: slots = 0; break;
  997. }
  998. }
  999. /* STOP index is never reached */
  1000. outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
  1001. udelay(20);
  1002. snd_via82xx_codec_ready(chip, 0);
  1003. return 0;
  1004. }
  1005. /*
  1006. * prepare callback for capture on via823x
  1007. */
  1008. static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
  1009. {
  1010. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1011. struct viadev *viadev = substream->runtime->private_data;
  1012. struct snd_pcm_runtime *runtime = substream->runtime;
  1013. if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
  1014. return -EINVAL;
  1015. snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  1016. snd_via82xx_channel_reset(chip, viadev);
  1017. snd_via82xx_set_table_ptr(chip, viadev);
  1018. outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
  1019. outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
  1020. (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
  1021. 0xff000000, /* STOP index is never reached */
  1022. VIADEV_REG(viadev, OFFSET_STOP_IDX));
  1023. udelay(20);
  1024. snd_via82xx_codec_ready(chip, 0);
  1025. return 0;
  1026. }
  1027. /*
  1028. * pcm hardware definition, identical for both playback and capture
  1029. */
  1030. static const struct snd_pcm_hardware snd_via82xx_hw =
  1031. {
  1032. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1033. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1034. SNDRV_PCM_INFO_MMAP_VALID |
  1035. /* SNDRV_PCM_INFO_RESUME | */
  1036. SNDRV_PCM_INFO_PAUSE),
  1037. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1038. .rates = SNDRV_PCM_RATE_48000,
  1039. .rate_min = 48000,
  1040. .rate_max = 48000,
  1041. .channels_min = 1,
  1042. .channels_max = 2,
  1043. .buffer_bytes_max = VIA_MAX_BUFSIZE,
  1044. .period_bytes_min = 32,
  1045. .period_bytes_max = VIA_MAX_BUFSIZE / 2,
  1046. .periods_min = 2,
  1047. .periods_max = VIA_TABLE_SIZE / 2,
  1048. .fifo_size = 0,
  1049. };
  1050. /*
  1051. * open callback skeleton
  1052. */
  1053. static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
  1054. struct snd_pcm_substream *substream)
  1055. {
  1056. struct snd_pcm_runtime *runtime = substream->runtime;
  1057. int err;
  1058. struct via_rate_lock *ratep;
  1059. bool use_src = false;
  1060. runtime->hw = snd_via82xx_hw;
  1061. /* set the hw rate condition */
  1062. ratep = &chip->rates[viadev->direction];
  1063. spin_lock_irq(&ratep->lock);
  1064. ratep->used++;
  1065. if (chip->spdif_on && viadev->reg_offset == 0x30) {
  1066. /* DXS#3 and spdif is on */
  1067. runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
  1068. snd_pcm_limit_hw_rates(runtime);
  1069. } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
  1070. /* fixed DXS playback rate */
  1071. runtime->hw.rates = SNDRV_PCM_RATE_48000;
  1072. runtime->hw.rate_min = runtime->hw.rate_max = 48000;
  1073. } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
  1074. /* use full SRC capabilities of DXS */
  1075. runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
  1076. SNDRV_PCM_RATE_8000_48000);
  1077. runtime->hw.rate_min = 8000;
  1078. runtime->hw.rate_max = 48000;
  1079. use_src = true;
  1080. } else if (! ratep->rate) {
  1081. int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
  1082. runtime->hw.rates = chip->ac97->rates[idx];
  1083. snd_pcm_limit_hw_rates(runtime);
  1084. } else {
  1085. /* a fixed rate */
  1086. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  1087. runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
  1088. }
  1089. spin_unlock_irq(&ratep->lock);
  1090. /* we may remove following constaint when we modify table entries
  1091. in interrupt */
  1092. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  1093. return err;
  1094. if (use_src) {
  1095. err = snd_pcm_hw_rule_noresample(runtime, 48000);
  1096. if (err < 0)
  1097. return err;
  1098. }
  1099. runtime->private_data = viadev;
  1100. viadev->substream = substream;
  1101. return 0;
  1102. }
  1103. /*
  1104. * open callback for playback on via686
  1105. */
  1106. static int snd_via686_playback_open(struct snd_pcm_substream *substream)
  1107. {
  1108. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1109. struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
  1110. int err;
  1111. if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
  1112. return err;
  1113. return 0;
  1114. }
  1115. /*
  1116. * open callback for playback on via823x DXS
  1117. */
  1118. static int snd_via8233_playback_open(struct snd_pcm_substream *substream)
  1119. {
  1120. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1121. struct viadev *viadev;
  1122. unsigned int stream;
  1123. int err;
  1124. viadev = &chip->devs[chip->playback_devno + substream->number];
  1125. if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
  1126. return err;
  1127. stream = viadev->reg_offset / 0x10;
  1128. if (chip->dxs_controls[stream]) {
  1129. chip->playback_volume[stream][0] =
  1130. VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
  1131. chip->playback_volume[stream][1] =
  1132. VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
  1133. chip->dxs_controls[stream]->vd[0].access &=
  1134. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1135. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1136. SNDRV_CTL_EVENT_MASK_INFO,
  1137. &chip->dxs_controls[stream]->id);
  1138. }
  1139. return 0;
  1140. }
  1141. /*
  1142. * open callback for playback on via823x multi-channel
  1143. */
  1144. static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
  1145. {
  1146. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1147. struct viadev *viadev = &chip->devs[chip->multi_devno];
  1148. int err;
  1149. /* channels constraint for VIA8233A
  1150. * 3 and 5 channels are not supported
  1151. */
  1152. static const unsigned int channels[] = {
  1153. 1, 2, 4, 6
  1154. };
  1155. static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
  1156. .count = ARRAY_SIZE(channels),
  1157. .list = channels,
  1158. .mask = 0,
  1159. };
  1160. if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
  1161. return err;
  1162. substream->runtime->hw.channels_max = 6;
  1163. if (chip->revision == VIA_REV_8233A)
  1164. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1165. SNDRV_PCM_HW_PARAM_CHANNELS,
  1166. &hw_constraints_channels);
  1167. return 0;
  1168. }
  1169. /*
  1170. * open callback for capture on via686 and via823x
  1171. */
  1172. static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
  1173. {
  1174. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1175. struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
  1176. return snd_via82xx_pcm_open(chip, viadev, substream);
  1177. }
  1178. /*
  1179. * close callback
  1180. */
  1181. static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
  1182. {
  1183. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1184. struct viadev *viadev = substream->runtime->private_data;
  1185. struct via_rate_lock *ratep;
  1186. /* release the rate lock */
  1187. ratep = &chip->rates[viadev->direction];
  1188. spin_lock_irq(&ratep->lock);
  1189. ratep->used--;
  1190. if (! ratep->used)
  1191. ratep->rate = 0;
  1192. spin_unlock_irq(&ratep->lock);
  1193. if (! ratep->rate) {
  1194. if (! viadev->direction) {
  1195. snd_ac97_update_power(chip->ac97,
  1196. AC97_PCM_FRONT_DAC_RATE, 0);
  1197. snd_ac97_update_power(chip->ac97,
  1198. AC97_PCM_SURR_DAC_RATE, 0);
  1199. snd_ac97_update_power(chip->ac97,
  1200. AC97_PCM_LFE_DAC_RATE, 0);
  1201. } else
  1202. snd_ac97_update_power(chip->ac97,
  1203. AC97_PCM_LR_ADC_RATE, 0);
  1204. }
  1205. viadev->substream = NULL;
  1206. return 0;
  1207. }
  1208. static int snd_via8233_playback_close(struct snd_pcm_substream *substream)
  1209. {
  1210. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1211. struct viadev *viadev = substream->runtime->private_data;
  1212. unsigned int stream;
  1213. stream = viadev->reg_offset / 0x10;
  1214. if (chip->dxs_controls[stream]) {
  1215. chip->dxs_controls[stream]->vd[0].access |=
  1216. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1217. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO,
  1218. &chip->dxs_controls[stream]->id);
  1219. }
  1220. return snd_via82xx_pcm_close(substream);
  1221. }
  1222. /* via686 playback callbacks */
  1223. static const struct snd_pcm_ops snd_via686_playback_ops = {
  1224. .open = snd_via686_playback_open,
  1225. .close = snd_via82xx_pcm_close,
  1226. .ioctl = snd_pcm_lib_ioctl,
  1227. .hw_params = snd_via82xx_hw_params,
  1228. .hw_free = snd_via82xx_hw_free,
  1229. .prepare = snd_via686_playback_prepare,
  1230. .trigger = snd_via82xx_pcm_trigger,
  1231. .pointer = snd_via686_pcm_pointer,
  1232. .page = snd_pcm_sgbuf_ops_page,
  1233. };
  1234. /* via686 capture callbacks */
  1235. static const struct snd_pcm_ops snd_via686_capture_ops = {
  1236. .open = snd_via82xx_capture_open,
  1237. .close = snd_via82xx_pcm_close,
  1238. .ioctl = snd_pcm_lib_ioctl,
  1239. .hw_params = snd_via82xx_hw_params,
  1240. .hw_free = snd_via82xx_hw_free,
  1241. .prepare = snd_via686_capture_prepare,
  1242. .trigger = snd_via82xx_pcm_trigger,
  1243. .pointer = snd_via686_pcm_pointer,
  1244. .page = snd_pcm_sgbuf_ops_page,
  1245. };
  1246. /* via823x DSX playback callbacks */
  1247. static const struct snd_pcm_ops snd_via8233_playback_ops = {
  1248. .open = snd_via8233_playback_open,
  1249. .close = snd_via8233_playback_close,
  1250. .ioctl = snd_pcm_lib_ioctl,
  1251. .hw_params = snd_via82xx_hw_params,
  1252. .hw_free = snd_via82xx_hw_free,
  1253. .prepare = snd_via8233_playback_prepare,
  1254. .trigger = snd_via82xx_pcm_trigger,
  1255. .pointer = snd_via8233_pcm_pointer,
  1256. .page = snd_pcm_sgbuf_ops_page,
  1257. };
  1258. /* via823x multi-channel playback callbacks */
  1259. static const struct snd_pcm_ops snd_via8233_multi_ops = {
  1260. .open = snd_via8233_multi_open,
  1261. .close = snd_via82xx_pcm_close,
  1262. .ioctl = snd_pcm_lib_ioctl,
  1263. .hw_params = snd_via82xx_hw_params,
  1264. .hw_free = snd_via82xx_hw_free,
  1265. .prepare = snd_via8233_multi_prepare,
  1266. .trigger = snd_via82xx_pcm_trigger,
  1267. .pointer = snd_via8233_pcm_pointer,
  1268. .page = snd_pcm_sgbuf_ops_page,
  1269. };
  1270. /* via823x capture callbacks */
  1271. static const struct snd_pcm_ops snd_via8233_capture_ops = {
  1272. .open = snd_via82xx_capture_open,
  1273. .close = snd_via82xx_pcm_close,
  1274. .ioctl = snd_pcm_lib_ioctl,
  1275. .hw_params = snd_via82xx_hw_params,
  1276. .hw_free = snd_via82xx_hw_free,
  1277. .prepare = snd_via8233_capture_prepare,
  1278. .trigger = snd_via82xx_pcm_trigger,
  1279. .pointer = snd_via8233_pcm_pointer,
  1280. .page = snd_pcm_sgbuf_ops_page,
  1281. };
  1282. static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
  1283. int shadow_pos, int direction)
  1284. {
  1285. chip->devs[idx].reg_offset = reg_offset;
  1286. chip->devs[idx].shadow_shift = shadow_pos * 4;
  1287. chip->devs[idx].direction = direction;
  1288. chip->devs[idx].port = chip->port + reg_offset;
  1289. }
  1290. /*
  1291. * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
  1292. */
  1293. static int snd_via8233_pcm_new(struct via82xx *chip)
  1294. {
  1295. struct snd_pcm *pcm;
  1296. struct snd_pcm_chmap *chmap;
  1297. int i, err;
  1298. chip->playback_devno = 0; /* x 4 */
  1299. chip->multi_devno = 4; /* x 1 */
  1300. chip->capture_devno = 5; /* x 2 */
  1301. chip->num_devs = 7;
  1302. chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
  1303. /* PCM #0: 4 DSX playbacks and 1 capture */
  1304. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
  1305. if (err < 0)
  1306. return err;
  1307. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
  1308. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
  1309. pcm->private_data = chip;
  1310. strcpy(pcm->name, chip->card->shortname);
  1311. chip->pcms[0] = pcm;
  1312. /* set up playbacks */
  1313. for (i = 0; i < 4; i++)
  1314. init_viadev(chip, i, 0x10 * i, i, 0);
  1315. /* capture */
  1316. init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
  1317. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1318. snd_dma_pci_data(chip->pci),
  1319. 64*1024, VIA_MAX_BUFSIZE);
  1320. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1321. snd_pcm_std_chmaps, 2, 0,
  1322. &chmap);
  1323. if (err < 0)
  1324. return err;
  1325. /* PCM #1: multi-channel playback and 2nd capture */
  1326. err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
  1327. if (err < 0)
  1328. return err;
  1329. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
  1330. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
  1331. pcm->private_data = chip;
  1332. strcpy(pcm->name, chip->card->shortname);
  1333. chip->pcms[1] = pcm;
  1334. /* set up playback */
  1335. init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
  1336. /* set up capture */
  1337. init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
  1338. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1339. snd_dma_pci_data(chip->pci),
  1340. 64*1024, VIA_MAX_BUFSIZE);
  1341. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1342. snd_pcm_alt_chmaps, 6, 0,
  1343. &chmap);
  1344. if (err < 0)
  1345. return err;
  1346. chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
  1347. return 0;
  1348. }
  1349. /*
  1350. * create pcm instances for VIA8233A
  1351. */
  1352. static int snd_via8233a_pcm_new(struct via82xx *chip)
  1353. {
  1354. struct snd_pcm *pcm;
  1355. struct snd_pcm_chmap *chmap;
  1356. int err;
  1357. chip->multi_devno = 0;
  1358. chip->playback_devno = 1;
  1359. chip->capture_devno = 2;
  1360. chip->num_devs = 3;
  1361. chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
  1362. /* PCM #0: multi-channel playback and capture */
  1363. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  1364. if (err < 0)
  1365. return err;
  1366. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
  1367. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
  1368. pcm->private_data = chip;
  1369. strcpy(pcm->name, chip->card->shortname);
  1370. chip->pcms[0] = pcm;
  1371. /* set up playback */
  1372. init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
  1373. /* capture */
  1374. init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
  1375. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1376. snd_dma_pci_data(chip->pci),
  1377. 64*1024, VIA_MAX_BUFSIZE);
  1378. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1379. snd_pcm_alt_chmaps, 6, 0,
  1380. &chmap);
  1381. if (err < 0)
  1382. return err;
  1383. chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
  1384. /* SPDIF supported? */
  1385. if (! ac97_can_spdif(chip->ac97))
  1386. return 0;
  1387. /* PCM #1: DXS3 playback (for spdif) */
  1388. err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
  1389. if (err < 0)
  1390. return err;
  1391. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
  1392. pcm->private_data = chip;
  1393. strcpy(pcm->name, chip->card->shortname);
  1394. chip->pcms[1] = pcm;
  1395. /* set up playback */
  1396. init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
  1397. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1398. snd_dma_pci_data(chip->pci),
  1399. 64*1024, VIA_MAX_BUFSIZE);
  1400. return 0;
  1401. }
  1402. /*
  1403. * create a pcm instance for via686a/b
  1404. */
  1405. static int snd_via686_pcm_new(struct via82xx *chip)
  1406. {
  1407. struct snd_pcm *pcm;
  1408. int err;
  1409. chip->playback_devno = 0;
  1410. chip->capture_devno = 1;
  1411. chip->num_devs = 2;
  1412. chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
  1413. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  1414. if (err < 0)
  1415. return err;
  1416. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
  1417. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
  1418. pcm->private_data = chip;
  1419. strcpy(pcm->name, chip->card->shortname);
  1420. chip->pcms[0] = pcm;
  1421. init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
  1422. init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
  1423. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1424. snd_dma_pci_data(chip->pci),
  1425. 64*1024, VIA_MAX_BUFSIZE);
  1426. return 0;
  1427. }
  1428. /*
  1429. * Mixer part
  1430. */
  1431. static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
  1432. struct snd_ctl_elem_info *uinfo)
  1433. {
  1434. /* formerly they were "Line" and "Mic", but it looks like that they
  1435. * have nothing to do with the actual physical connections...
  1436. */
  1437. static const char * const texts[2] = {
  1438. "Input1", "Input2"
  1439. };
  1440. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  1441. }
  1442. static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
  1443. struct snd_ctl_elem_value *ucontrol)
  1444. {
  1445. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1446. unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
  1447. ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
  1448. return 0;
  1449. }
  1450. static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
  1451. struct snd_ctl_elem_value *ucontrol)
  1452. {
  1453. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1454. unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
  1455. u8 val, oval;
  1456. spin_lock_irq(&chip->reg_lock);
  1457. oval = inb(port);
  1458. val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
  1459. if (ucontrol->value.enumerated.item[0])
  1460. val |= VIA_REG_CAPTURE_CHANNEL_MIC;
  1461. if (val != oval)
  1462. outb(val, port);
  1463. spin_unlock_irq(&chip->reg_lock);
  1464. return val != oval;
  1465. }
  1466. static struct snd_kcontrol_new snd_via8233_capture_source = {
  1467. .name = "Input Source Select",
  1468. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1469. .info = snd_via8233_capture_source_info,
  1470. .get = snd_via8233_capture_source_get,
  1471. .put = snd_via8233_capture_source_put,
  1472. };
  1473. #define snd_via8233_dxs3_spdif_info snd_ctl_boolean_mono_info
  1474. static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
  1475. struct snd_ctl_elem_value *ucontrol)
  1476. {
  1477. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1478. u8 val;
  1479. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
  1480. ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
  1481. return 0;
  1482. }
  1483. static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
  1484. struct snd_ctl_elem_value *ucontrol)
  1485. {
  1486. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1487. u8 val, oval;
  1488. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
  1489. val = oval & ~VIA8233_SPDIF_DX3;
  1490. if (ucontrol->value.integer.value[0])
  1491. val |= VIA8233_SPDIF_DX3;
  1492. /* save the spdif flag for rate filtering */
  1493. chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
  1494. if (val != oval) {
  1495. pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
  1496. return 1;
  1497. }
  1498. return 0;
  1499. }
  1500. static const struct snd_kcontrol_new snd_via8233_dxs3_spdif_control = {
  1501. .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
  1502. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1503. .info = snd_via8233_dxs3_spdif_info,
  1504. .get = snd_via8233_dxs3_spdif_get,
  1505. .put = snd_via8233_dxs3_spdif_put,
  1506. };
  1507. static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
  1508. struct snd_ctl_elem_info *uinfo)
  1509. {
  1510. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1511. uinfo->count = 2;
  1512. uinfo->value.integer.min = 0;
  1513. uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
  1514. return 0;
  1515. }
  1516. static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
  1517. struct snd_ctl_elem_value *ucontrol)
  1518. {
  1519. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1520. unsigned int idx = kcontrol->id.subdevice;
  1521. ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
  1522. ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
  1523. return 0;
  1524. }
  1525. static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
  1526. struct snd_ctl_elem_value *ucontrol)
  1527. {
  1528. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1529. ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
  1530. ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
  1531. return 0;
  1532. }
  1533. static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
  1534. struct snd_ctl_elem_value *ucontrol)
  1535. {
  1536. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1537. unsigned int idx = kcontrol->id.subdevice;
  1538. unsigned long port = chip->port + 0x10 * idx;
  1539. unsigned char val;
  1540. int i, change = 0;
  1541. for (i = 0; i < 2; i++) {
  1542. val = ucontrol->value.integer.value[i];
  1543. if (val > VIA_DXS_MAX_VOLUME)
  1544. val = VIA_DXS_MAX_VOLUME;
  1545. val = VIA_DXS_MAX_VOLUME - val;
  1546. change |= val != chip->playback_volume[idx][i];
  1547. if (change) {
  1548. chip->playback_volume[idx][i] = val;
  1549. outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
  1550. }
  1551. }
  1552. return change;
  1553. }
  1554. static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
  1555. struct snd_ctl_elem_value *ucontrol)
  1556. {
  1557. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1558. unsigned int idx;
  1559. unsigned char val;
  1560. int i, change = 0;
  1561. for (i = 0; i < 2; i++) {
  1562. val = ucontrol->value.integer.value[i];
  1563. if (val > VIA_DXS_MAX_VOLUME)
  1564. val = VIA_DXS_MAX_VOLUME;
  1565. val = VIA_DXS_MAX_VOLUME - val;
  1566. if (val != chip->playback_volume_c[i]) {
  1567. change = 1;
  1568. chip->playback_volume_c[i] = val;
  1569. for (idx = 0; idx < 4; idx++) {
  1570. unsigned long port = chip->port + 0x10 * idx;
  1571. chip->playback_volume[idx][i] = val;
  1572. outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
  1573. }
  1574. }
  1575. }
  1576. return change;
  1577. }
  1578. static const DECLARE_TLV_DB_SCALE(db_scale_dxs, -4650, 150, 1);
  1579. static const struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control = {
  1580. .name = "PCM Playback Volume",
  1581. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1582. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1583. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  1584. .info = snd_via8233_dxs_volume_info,
  1585. .get = snd_via8233_pcmdxs_volume_get,
  1586. .put = snd_via8233_pcmdxs_volume_put,
  1587. .tlv = { .p = db_scale_dxs }
  1588. };
  1589. static const struct snd_kcontrol_new snd_via8233_dxs_volume_control = {
  1590. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1591. .device = 0,
  1592. /* .subdevice set later */
  1593. .name = "PCM Playback Volume",
  1594. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1595. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1596. SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  1597. .info = snd_via8233_dxs_volume_info,
  1598. .get = snd_via8233_dxs_volume_get,
  1599. .put = snd_via8233_dxs_volume_put,
  1600. .tlv = { .p = db_scale_dxs }
  1601. };
  1602. /*
  1603. */
  1604. static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
  1605. {
  1606. struct via82xx *chip = bus->private_data;
  1607. chip->ac97_bus = NULL;
  1608. }
  1609. static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
  1610. {
  1611. struct via82xx *chip = ac97->private_data;
  1612. chip->ac97 = NULL;
  1613. }
  1614. static const struct ac97_quirk ac97_quirks[] = {
  1615. {
  1616. .subvendor = 0x1106,
  1617. .subdevice = 0x4161,
  1618. .codec_id = 0x56494161, /* VT1612A */
  1619. .name = "Soltek SL-75DRV5",
  1620. .type = AC97_TUNE_NONE
  1621. },
  1622. { /* FIXME: which codec? */
  1623. .subvendor = 0x1106,
  1624. .subdevice = 0x4161,
  1625. .name = "ASRock K7VT2",
  1626. .type = AC97_TUNE_HP_ONLY
  1627. },
  1628. {
  1629. .subvendor = 0x110a,
  1630. .subdevice = 0x0079,
  1631. .name = "Fujitsu Siemens D1289",
  1632. .type = AC97_TUNE_HP_ONLY
  1633. },
  1634. {
  1635. .subvendor = 0x1019,
  1636. .subdevice = 0x0a81,
  1637. .name = "ECS K7VTA3",
  1638. .type = AC97_TUNE_HP_ONLY
  1639. },
  1640. {
  1641. .subvendor = 0x1019,
  1642. .subdevice = 0x0a85,
  1643. .name = "ECS L7VMM2",
  1644. .type = AC97_TUNE_HP_ONLY
  1645. },
  1646. {
  1647. .subvendor = 0x1019,
  1648. .subdevice = 0x1841,
  1649. .name = "ECS K7VTA3",
  1650. .type = AC97_TUNE_HP_ONLY
  1651. },
  1652. {
  1653. .subvendor = 0x1849,
  1654. .subdevice = 0x3059,
  1655. .name = "ASRock K7VM2",
  1656. .type = AC97_TUNE_HP_ONLY /* VT1616 */
  1657. },
  1658. {
  1659. .subvendor = 0x14cd,
  1660. .subdevice = 0x7002,
  1661. .name = "Unknown",
  1662. .type = AC97_TUNE_ALC_JACK
  1663. },
  1664. {
  1665. .subvendor = 0x1071,
  1666. .subdevice = 0x8590,
  1667. .name = "Mitac Mobo",
  1668. .type = AC97_TUNE_ALC_JACK
  1669. },
  1670. {
  1671. .subvendor = 0x161f,
  1672. .subdevice = 0x202b,
  1673. .name = "Arima Notebook",
  1674. .type = AC97_TUNE_HP_ONLY,
  1675. },
  1676. {
  1677. .subvendor = 0x161f,
  1678. .subdevice = 0x2032,
  1679. .name = "Targa Traveller 811",
  1680. .type = AC97_TUNE_HP_ONLY,
  1681. },
  1682. {
  1683. .subvendor = 0x161f,
  1684. .subdevice = 0x2032,
  1685. .name = "m680x",
  1686. .type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
  1687. },
  1688. {
  1689. .subvendor = 0x1297,
  1690. .subdevice = 0xa232,
  1691. .name = "Shuttle AK32VN",
  1692. .type = AC97_TUNE_HP_ONLY
  1693. },
  1694. { } /* terminator */
  1695. };
  1696. static int snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
  1697. {
  1698. struct snd_ac97_template ac97;
  1699. int err;
  1700. static struct snd_ac97_bus_ops ops = {
  1701. .write = snd_via82xx_codec_write,
  1702. .read = snd_via82xx_codec_read,
  1703. .wait = snd_via82xx_codec_wait,
  1704. };
  1705. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  1706. return err;
  1707. chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
  1708. chip->ac97_bus->clock = chip->ac97_clock;
  1709. memset(&ac97, 0, sizeof(ac97));
  1710. ac97.private_data = chip;
  1711. ac97.private_free = snd_via82xx_mixer_free_ac97;
  1712. ac97.pci = chip->pci;
  1713. ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
  1714. if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
  1715. return err;
  1716. snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
  1717. if (chip->chip_type != TYPE_VIA686) {
  1718. /* use slot 10/11 */
  1719. snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
  1720. }
  1721. return 0;
  1722. }
  1723. #ifdef SUPPORT_JOYSTICK
  1724. #define JOYSTICK_ADDR 0x200
  1725. static int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
  1726. {
  1727. struct gameport *gp;
  1728. struct resource *r;
  1729. if (!joystick)
  1730. return -ENODEV;
  1731. r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
  1732. if (!r) {
  1733. dev_warn(chip->card->dev, "cannot reserve joystick port %#x\n",
  1734. JOYSTICK_ADDR);
  1735. return -EBUSY;
  1736. }
  1737. chip->gameport = gp = gameport_allocate_port();
  1738. if (!gp) {
  1739. dev_err(chip->card->dev,
  1740. "cannot allocate memory for gameport\n");
  1741. release_and_free_resource(r);
  1742. return -ENOMEM;
  1743. }
  1744. gameport_set_name(gp, "VIA686 Gameport");
  1745. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  1746. gameport_set_dev_parent(gp, &chip->pci->dev);
  1747. gp->io = JOYSTICK_ADDR;
  1748. gameport_set_port_data(gp, r);
  1749. /* Enable legacy joystick port */
  1750. *legacy |= VIA_FUNC_ENABLE_GAME;
  1751. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
  1752. gameport_register_port(chip->gameport);
  1753. return 0;
  1754. }
  1755. static void snd_via686_free_gameport(struct via82xx *chip)
  1756. {
  1757. if (chip->gameport) {
  1758. struct resource *r = gameport_get_port_data(chip->gameport);
  1759. gameport_unregister_port(chip->gameport);
  1760. chip->gameport = NULL;
  1761. release_and_free_resource(r);
  1762. }
  1763. }
  1764. #else
  1765. static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
  1766. {
  1767. return -ENOSYS;
  1768. }
  1769. static inline void snd_via686_free_gameport(struct via82xx *chip) { }
  1770. #endif
  1771. /*
  1772. *
  1773. */
  1774. static int snd_via8233_init_misc(struct via82xx *chip)
  1775. {
  1776. int i, err, caps;
  1777. unsigned char val;
  1778. caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
  1779. for (i = 0; i < caps; i++) {
  1780. snd_via8233_capture_source.index = i;
  1781. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
  1782. if (err < 0)
  1783. return err;
  1784. }
  1785. if (ac97_can_spdif(chip->ac97)) {
  1786. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
  1787. if (err < 0)
  1788. return err;
  1789. }
  1790. if (chip->chip_type != TYPE_VIA8233A) {
  1791. /* when no h/w PCM volume control is found, use DXS volume control
  1792. * as the PCM vol control
  1793. */
  1794. struct snd_ctl_elem_id sid;
  1795. memset(&sid, 0, sizeof(sid));
  1796. strcpy(sid.name, "PCM Playback Volume");
  1797. sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1798. if (! snd_ctl_find_id(chip->card, &sid)) {
  1799. dev_info(chip->card->dev,
  1800. "Using DXS as PCM Playback\n");
  1801. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
  1802. if (err < 0)
  1803. return err;
  1804. }
  1805. else /* Using DXS when PCM emulation is enabled is really weird */
  1806. {
  1807. for (i = 0; i < 4; ++i) {
  1808. struct snd_kcontrol *kctl;
  1809. kctl = snd_ctl_new1(
  1810. &snd_via8233_dxs_volume_control, chip);
  1811. if (!kctl)
  1812. return -ENOMEM;
  1813. kctl->id.subdevice = i;
  1814. err = snd_ctl_add(chip->card, kctl);
  1815. if (err < 0)
  1816. return err;
  1817. chip->dxs_controls[i] = kctl;
  1818. }
  1819. }
  1820. }
  1821. /* select spdif data slot 10/11 */
  1822. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
  1823. val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
  1824. val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
  1825. pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
  1826. return 0;
  1827. }
  1828. static int snd_via686_init_misc(struct via82xx *chip)
  1829. {
  1830. unsigned char legacy, legacy_cfg;
  1831. int rev_h = 0;
  1832. legacy = chip->old_legacy;
  1833. legacy_cfg = chip->old_legacy_cfg;
  1834. legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
  1835. legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
  1836. if (chip->revision >= VIA_REV_686_H) {
  1837. rev_h = 1;
  1838. if (mpu_port >= 0x200) { /* force MIDI */
  1839. mpu_port &= 0xfffc;
  1840. pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
  1841. #ifdef CONFIG_PM_SLEEP
  1842. chip->mpu_port_saved = mpu_port;
  1843. #endif
  1844. } else {
  1845. mpu_port = pci_resource_start(chip->pci, 2);
  1846. }
  1847. } else {
  1848. switch (mpu_port) { /* force MIDI */
  1849. case 0x300:
  1850. case 0x310:
  1851. case 0x320:
  1852. case 0x330:
  1853. legacy_cfg &= ~(3 << 2);
  1854. legacy_cfg |= (mpu_port & 0x0030) >> 2;
  1855. break;
  1856. default: /* no, use BIOS settings */
  1857. if (legacy & VIA_FUNC_ENABLE_MIDI)
  1858. mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
  1859. break;
  1860. }
  1861. }
  1862. if (mpu_port >= 0x200 &&
  1863. (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
  1864. != NULL) {
  1865. if (rev_h)
  1866. legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
  1867. legacy |= VIA_FUNC_ENABLE_MIDI;
  1868. } else {
  1869. if (rev_h)
  1870. legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
  1871. legacy &= ~VIA_FUNC_ENABLE_MIDI;
  1872. mpu_port = 0;
  1873. }
  1874. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
  1875. pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
  1876. if (chip->mpu_res) {
  1877. if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
  1878. mpu_port, MPU401_INFO_INTEGRATED |
  1879. MPU401_INFO_IRQ_HOOK, -1,
  1880. &chip->rmidi) < 0) {
  1881. dev_warn(chip->card->dev,
  1882. "unable to initialize MPU-401 at 0x%lx, skipping\n",
  1883. mpu_port);
  1884. legacy &= ~VIA_FUNC_ENABLE_MIDI;
  1885. } else {
  1886. legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
  1887. }
  1888. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
  1889. }
  1890. snd_via686_create_gameport(chip, &legacy);
  1891. #ifdef CONFIG_PM_SLEEP
  1892. chip->legacy_saved = legacy;
  1893. chip->legacy_cfg_saved = legacy_cfg;
  1894. #endif
  1895. return 0;
  1896. }
  1897. /*
  1898. * proc interface
  1899. */
  1900. static void snd_via82xx_proc_read(struct snd_info_entry *entry,
  1901. struct snd_info_buffer *buffer)
  1902. {
  1903. struct via82xx *chip = entry->private_data;
  1904. int i;
  1905. snd_iprintf(buffer, "%s\n\n", chip->card->longname);
  1906. for (i = 0; i < 0xa0; i += 4) {
  1907. snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
  1908. }
  1909. }
  1910. static void snd_via82xx_proc_init(struct via82xx *chip)
  1911. {
  1912. struct snd_info_entry *entry;
  1913. if (! snd_card_proc_new(chip->card, "via82xx", &entry))
  1914. snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
  1915. }
  1916. /*
  1917. *
  1918. */
  1919. static int snd_via82xx_chip_init(struct via82xx *chip)
  1920. {
  1921. unsigned int val;
  1922. unsigned long end_time;
  1923. unsigned char pval;
  1924. #if 0 /* broken on K7M? */
  1925. if (chip->chip_type == TYPE_VIA686)
  1926. /* disable all legacy ports */
  1927. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
  1928. #endif
  1929. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  1930. if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
  1931. /* deassert ACLink reset, force SYNC */
  1932. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  1933. VIA_ACLINK_CTRL_ENABLE |
  1934. VIA_ACLINK_CTRL_RESET |
  1935. VIA_ACLINK_CTRL_SYNC);
  1936. udelay(100);
  1937. #if 1 /* FIXME: should we do full reset here for all chip models? */
  1938. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
  1939. udelay(100);
  1940. #else
  1941. /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
  1942. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  1943. VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
  1944. udelay(2);
  1945. #endif
  1946. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  1947. /* note - FM data out has trouble with non VRA codecs !! */
  1948. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  1949. udelay(100);
  1950. }
  1951. /* Make sure VRA is enabled, in case we didn't do a
  1952. * complete codec reset, above */
  1953. pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
  1954. if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
  1955. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  1956. /* note - FM data out has trouble with non VRA codecs !! */
  1957. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  1958. udelay(100);
  1959. }
  1960. /* wait until codec ready */
  1961. end_time = jiffies + msecs_to_jiffies(750);
  1962. do {
  1963. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  1964. if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
  1965. break;
  1966. schedule_timeout_uninterruptible(1);
  1967. } while (time_before(jiffies, end_time));
  1968. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
  1969. dev_err(chip->card->dev,
  1970. "AC'97 codec is not ready [0x%x]\n", val);
  1971. #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
  1972. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  1973. VIA_REG_AC97_SECONDARY_VALID |
  1974. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  1975. end_time = jiffies + msecs_to_jiffies(750);
  1976. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  1977. VIA_REG_AC97_SECONDARY_VALID |
  1978. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  1979. do {
  1980. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
  1981. chip->ac97_secondary = 1;
  1982. goto __ac97_ok2;
  1983. }
  1984. schedule_timeout_uninterruptible(1);
  1985. } while (time_before(jiffies, end_time));
  1986. /* This is ok, the most of motherboards have only one codec */
  1987. __ac97_ok2:
  1988. #endif
  1989. if (chip->chip_type == TYPE_VIA686) {
  1990. /* route FM trap to IRQ, disable FM trap */
  1991. pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
  1992. /* disable all GPI interrupts */
  1993. outl(0, VIAREG(chip, GPI_INTR));
  1994. }
  1995. if (chip->chip_type != TYPE_VIA686) {
  1996. /* Workaround for Award BIOS bug:
  1997. * DXS channels don't work properly with VRA if MC97 is disabled.
  1998. */
  1999. struct pci_dev *pci;
  2000. pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
  2001. if (pci) {
  2002. unsigned char data;
  2003. pci_read_config_byte(pci, 0x44, &data);
  2004. pci_write_config_byte(pci, 0x44, data | 0x40);
  2005. pci_dev_put(pci);
  2006. }
  2007. }
  2008. if (chip->chip_type != TYPE_VIA8233A) {
  2009. int i, idx;
  2010. for (idx = 0; idx < 4; idx++) {
  2011. unsigned long port = chip->port + 0x10 * idx;
  2012. for (i = 0; i < 2; i++) {
  2013. chip->playback_volume[idx][i]=chip->playback_volume_c[i];
  2014. outb(chip->playback_volume_c[i],
  2015. port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
  2016. }
  2017. }
  2018. }
  2019. return 0;
  2020. }
  2021. #ifdef CONFIG_PM_SLEEP
  2022. /*
  2023. * power management
  2024. */
  2025. static int snd_via82xx_suspend(struct device *dev)
  2026. {
  2027. struct snd_card *card = dev_get_drvdata(dev);
  2028. struct via82xx *chip = card->private_data;
  2029. int i;
  2030. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  2031. for (i = 0; i < 2; i++)
  2032. snd_pcm_suspend_all(chip->pcms[i]);
  2033. for (i = 0; i < chip->num_devs; i++)
  2034. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  2035. synchronize_irq(chip->irq);
  2036. snd_ac97_suspend(chip->ac97);
  2037. /* save misc values */
  2038. if (chip->chip_type != TYPE_VIA686) {
  2039. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
  2040. chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
  2041. chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
  2042. }
  2043. return 0;
  2044. }
  2045. static int snd_via82xx_resume(struct device *dev)
  2046. {
  2047. struct snd_card *card = dev_get_drvdata(dev);
  2048. struct via82xx *chip = card->private_data;
  2049. int i;
  2050. snd_via82xx_chip_init(chip);
  2051. if (chip->chip_type == TYPE_VIA686) {
  2052. if (chip->mpu_port_saved)
  2053. pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
  2054. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
  2055. pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
  2056. } else {
  2057. pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
  2058. outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
  2059. outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
  2060. }
  2061. snd_ac97_resume(chip->ac97);
  2062. for (i = 0; i < chip->num_devs; i++)
  2063. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  2064. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  2065. return 0;
  2066. }
  2067. static SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
  2068. #define SND_VIA82XX_PM_OPS &snd_via82xx_pm
  2069. #else
  2070. #define SND_VIA82XX_PM_OPS NULL
  2071. #endif /* CONFIG_PM_SLEEP */
  2072. static int snd_via82xx_free(struct via82xx *chip)
  2073. {
  2074. unsigned int i;
  2075. if (chip->irq < 0)
  2076. goto __end_hw;
  2077. /* disable interrupts */
  2078. for (i = 0; i < chip->num_devs; i++)
  2079. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  2080. if (chip->irq >= 0)
  2081. free_irq(chip->irq, chip);
  2082. __end_hw:
  2083. release_and_free_resource(chip->mpu_res);
  2084. pci_release_regions(chip->pci);
  2085. if (chip->chip_type == TYPE_VIA686) {
  2086. snd_via686_free_gameport(chip);
  2087. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
  2088. pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
  2089. }
  2090. pci_disable_device(chip->pci);
  2091. kfree(chip);
  2092. return 0;
  2093. }
  2094. static int snd_via82xx_dev_free(struct snd_device *device)
  2095. {
  2096. struct via82xx *chip = device->device_data;
  2097. return snd_via82xx_free(chip);
  2098. }
  2099. static int snd_via82xx_create(struct snd_card *card,
  2100. struct pci_dev *pci,
  2101. int chip_type,
  2102. int revision,
  2103. unsigned int ac97_clock,
  2104. struct via82xx **r_via)
  2105. {
  2106. struct via82xx *chip;
  2107. int err;
  2108. static struct snd_device_ops ops = {
  2109. .dev_free = snd_via82xx_dev_free,
  2110. };
  2111. if ((err = pci_enable_device(pci)) < 0)
  2112. return err;
  2113. if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
  2114. pci_disable_device(pci);
  2115. return -ENOMEM;
  2116. }
  2117. chip->chip_type = chip_type;
  2118. chip->revision = revision;
  2119. spin_lock_init(&chip->reg_lock);
  2120. spin_lock_init(&chip->rates[0].lock);
  2121. spin_lock_init(&chip->rates[1].lock);
  2122. chip->card = card;
  2123. chip->pci = pci;
  2124. chip->irq = -1;
  2125. pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
  2126. pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
  2127. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
  2128. chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
  2129. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  2130. kfree(chip);
  2131. pci_disable_device(pci);
  2132. return err;
  2133. }
  2134. chip->port = pci_resource_start(pci, 0);
  2135. if (request_irq(pci->irq,
  2136. chip_type == TYPE_VIA8233 ?
  2137. snd_via8233_interrupt : snd_via686_interrupt,
  2138. IRQF_SHARED,
  2139. KBUILD_MODNAME, chip)) {
  2140. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  2141. snd_via82xx_free(chip);
  2142. return -EBUSY;
  2143. }
  2144. chip->irq = pci->irq;
  2145. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  2146. chip->ac97_clock = ac97_clock;
  2147. synchronize_irq(chip->irq);
  2148. if ((err = snd_via82xx_chip_init(chip)) < 0) {
  2149. snd_via82xx_free(chip);
  2150. return err;
  2151. }
  2152. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  2153. snd_via82xx_free(chip);
  2154. return err;
  2155. }
  2156. /* The 8233 ac97 controller does not implement the master bit
  2157. * in the pci command register. IMHO this is a violation of the PCI spec.
  2158. * We call pci_set_master here because it does not hurt. */
  2159. pci_set_master(pci);
  2160. *r_via = chip;
  2161. return 0;
  2162. }
  2163. struct via823x_info {
  2164. int revision;
  2165. char *name;
  2166. int type;
  2167. };
  2168. static struct via823x_info via823x_cards[] = {
  2169. { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
  2170. { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
  2171. { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
  2172. { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
  2173. { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
  2174. { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
  2175. { VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
  2176. };
  2177. /*
  2178. * auto detection of DXS channel supports.
  2179. */
  2180. static struct snd_pci_quirk dxs_whitelist[] = {
  2181. SND_PCI_QUIRK(0x1005, 0x4710, "Avance Logic Mobo", VIA_DXS_ENABLE),
  2182. SND_PCI_QUIRK(0x1019, 0x0996, "ESC Mobo", VIA_DXS_48K),
  2183. SND_PCI_QUIRK(0x1019, 0x0a81, "ECS K7VTA3 v8.0", VIA_DXS_NO_VRA),
  2184. SND_PCI_QUIRK(0x1019, 0x0a85, "ECS L7VMM2", VIA_DXS_NO_VRA),
  2185. SND_PCI_QUIRK_VENDOR(0x1019, "ESC K8", VIA_DXS_SRC),
  2186. SND_PCI_QUIRK(0x1019, 0xaa01, "ESC K8T890-A", VIA_DXS_SRC),
  2187. SND_PCI_QUIRK(0x1025, 0x0033, "Acer Inspire 1353LM", VIA_DXS_NO_VRA),
  2188. SND_PCI_QUIRK(0x1025, 0x0046, "Acer Aspire 1524 WLMi", VIA_DXS_SRC),
  2189. SND_PCI_QUIRK_VENDOR(0x1043, "ASUS A7/A8", VIA_DXS_NO_VRA),
  2190. SND_PCI_QUIRK_VENDOR(0x1071, "Diverse Notebook", VIA_DXS_NO_VRA),
  2191. SND_PCI_QUIRK(0x10cf, 0x118e, "FSC Laptop", VIA_DXS_ENABLE),
  2192. SND_PCI_QUIRK_VENDOR(0x1106, "ASRock", VIA_DXS_SRC),
  2193. SND_PCI_QUIRK(0x1297, 0xa231, "Shuttle AK31v2", VIA_DXS_SRC),
  2194. SND_PCI_QUIRK(0x1297, 0xa232, "Shuttle", VIA_DXS_SRC),
  2195. SND_PCI_QUIRK(0x1297, 0xc160, "Shuttle Sk41G", VIA_DXS_SRC),
  2196. SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte GA-7VAXP", VIA_DXS_ENABLE),
  2197. SND_PCI_QUIRK(0x1462, 0x3800, "MSI KT266", VIA_DXS_ENABLE),
  2198. SND_PCI_QUIRK(0x1462, 0x7120, "MSI KT4V", VIA_DXS_ENABLE),
  2199. SND_PCI_QUIRK(0x1462, 0x7142, "MSI K8MM-V", VIA_DXS_ENABLE),
  2200. SND_PCI_QUIRK_VENDOR(0x1462, "MSI Mobo", VIA_DXS_SRC),
  2201. SND_PCI_QUIRK(0x147b, 0x1401, "ABIT KD7(-RAID)", VIA_DXS_ENABLE),
  2202. SND_PCI_QUIRK(0x147b, 0x1411, "ABIT VA-20", VIA_DXS_ENABLE),
  2203. SND_PCI_QUIRK(0x147b, 0x1413, "ABIT KV8 Pro", VIA_DXS_ENABLE),
  2204. SND_PCI_QUIRK(0x147b, 0x1415, "ABIT AV8", VIA_DXS_NO_VRA),
  2205. SND_PCI_QUIRK(0x14ff, 0x0403, "Twinhead mobo", VIA_DXS_ENABLE),
  2206. SND_PCI_QUIRK(0x14ff, 0x0408, "Twinhead laptop", VIA_DXS_SRC),
  2207. SND_PCI_QUIRK(0x1558, 0x4701, "Clevo D470", VIA_DXS_SRC),
  2208. SND_PCI_QUIRK(0x1584, 0x8120, "Diverse Laptop", VIA_DXS_ENABLE),
  2209. SND_PCI_QUIRK(0x1584, 0x8123, "Targa/Uniwill", VIA_DXS_NO_VRA),
  2210. SND_PCI_QUIRK(0x161f, 0x202b, "Amira Notebook", VIA_DXS_NO_VRA),
  2211. SND_PCI_QUIRK(0x161f, 0x2032, "m680x machines", VIA_DXS_48K),
  2212. SND_PCI_QUIRK(0x1631, 0xe004, "PB EasyNote 3174", VIA_DXS_ENABLE),
  2213. SND_PCI_QUIRK(0x1695, 0x3005, "EPoX EP-8K9A", VIA_DXS_ENABLE),
  2214. SND_PCI_QUIRK_VENDOR(0x1695, "EPoX mobo", VIA_DXS_SRC),
  2215. SND_PCI_QUIRK_VENDOR(0x16f3, "Jetway K8", VIA_DXS_SRC),
  2216. SND_PCI_QUIRK_VENDOR(0x1734, "FSC Laptop", VIA_DXS_SRC),
  2217. SND_PCI_QUIRK(0x1849, 0x3059, "ASRock K7VM2", VIA_DXS_NO_VRA),
  2218. SND_PCI_QUIRK_VENDOR(0x1849, "ASRock mobo", VIA_DXS_SRC),
  2219. SND_PCI_QUIRK(0x1919, 0x200a, "Soltek SL-K8", VIA_DXS_NO_VRA),
  2220. SND_PCI_QUIRK(0x4005, 0x4710, "MSI K7T266", VIA_DXS_SRC),
  2221. { } /* terminator */
  2222. };
  2223. static int check_dxs_list(struct pci_dev *pci, int revision)
  2224. {
  2225. const struct snd_pci_quirk *w;
  2226. w = snd_pci_quirk_lookup(pci, dxs_whitelist);
  2227. if (w) {
  2228. dev_dbg(&pci->dev, "DXS white list for %s found\n",
  2229. snd_pci_quirk_name(w));
  2230. return w->value;
  2231. }
  2232. /* for newer revision, default to DXS_SRC */
  2233. if (revision >= VIA_REV_8235)
  2234. return VIA_DXS_SRC;
  2235. /*
  2236. * not detected, try 48k rate only to be sure.
  2237. */
  2238. dev_info(&pci->dev, "Assuming DXS channels with 48k fixed sample rate.\n");
  2239. dev_info(&pci->dev, " Please try dxs_support=5 option\n");
  2240. dev_info(&pci->dev, " and report if it works on your machine.\n");
  2241. dev_info(&pci->dev, " For more details, read ALSA-Configuration.txt.\n");
  2242. return VIA_DXS_48K;
  2243. };
  2244. static int snd_via82xx_probe(struct pci_dev *pci,
  2245. const struct pci_device_id *pci_id)
  2246. {
  2247. struct snd_card *card;
  2248. struct via82xx *chip;
  2249. int chip_type = 0, card_type;
  2250. unsigned int i;
  2251. int err;
  2252. err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
  2253. if (err < 0)
  2254. return err;
  2255. card_type = pci_id->driver_data;
  2256. switch (card_type) {
  2257. case TYPE_CARD_VIA686:
  2258. strcpy(card->driver, "VIA686A");
  2259. sprintf(card->shortname, "VIA 82C686A/B rev%x", pci->revision);
  2260. chip_type = TYPE_VIA686;
  2261. break;
  2262. case TYPE_CARD_VIA8233:
  2263. chip_type = TYPE_VIA8233;
  2264. sprintf(card->shortname, "VIA 823x rev%x", pci->revision);
  2265. for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
  2266. if (pci->revision == via823x_cards[i].revision) {
  2267. chip_type = via823x_cards[i].type;
  2268. strcpy(card->shortname, via823x_cards[i].name);
  2269. break;
  2270. }
  2271. }
  2272. if (chip_type != TYPE_VIA8233A) {
  2273. if (dxs_support == VIA_DXS_AUTO)
  2274. dxs_support = check_dxs_list(pci, pci->revision);
  2275. /* force to use VIA8233 or 8233A model according to
  2276. * dxs_support module option
  2277. */
  2278. if (dxs_support == VIA_DXS_DISABLE)
  2279. chip_type = TYPE_VIA8233A;
  2280. else
  2281. chip_type = TYPE_VIA8233;
  2282. }
  2283. if (chip_type == TYPE_VIA8233A)
  2284. strcpy(card->driver, "VIA8233A");
  2285. else if (pci->revision >= VIA_REV_8237)
  2286. strcpy(card->driver, "VIA8237"); /* no slog assignment */
  2287. else
  2288. strcpy(card->driver, "VIA8233");
  2289. break;
  2290. default:
  2291. dev_err(card->dev, "invalid card type %d\n", card_type);
  2292. err = -EINVAL;
  2293. goto __error;
  2294. }
  2295. if ((err = snd_via82xx_create(card, pci, chip_type, pci->revision,
  2296. ac97_clock, &chip)) < 0)
  2297. goto __error;
  2298. card->private_data = chip;
  2299. if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
  2300. goto __error;
  2301. if (chip_type == TYPE_VIA686) {
  2302. if ((err = snd_via686_pcm_new(chip)) < 0 ||
  2303. (err = snd_via686_init_misc(chip)) < 0)
  2304. goto __error;
  2305. } else {
  2306. if (chip_type == TYPE_VIA8233A) {
  2307. if ((err = snd_via8233a_pcm_new(chip)) < 0)
  2308. goto __error;
  2309. // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
  2310. } else {
  2311. if ((err = snd_via8233_pcm_new(chip)) < 0)
  2312. goto __error;
  2313. if (dxs_support == VIA_DXS_48K)
  2314. chip->dxs_fixed = 1;
  2315. else if (dxs_support == VIA_DXS_NO_VRA)
  2316. chip->no_vra = 1;
  2317. else if (dxs_support == VIA_DXS_SRC) {
  2318. chip->no_vra = 1;
  2319. chip->dxs_src = 1;
  2320. }
  2321. }
  2322. if ((err = snd_via8233_init_misc(chip)) < 0)
  2323. goto __error;
  2324. }
  2325. /* disable interrupts */
  2326. for (i = 0; i < chip->num_devs; i++)
  2327. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  2328. snprintf(card->longname, sizeof(card->longname),
  2329. "%s with %s at %#lx, irq %d", card->shortname,
  2330. snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
  2331. snd_via82xx_proc_init(chip);
  2332. if ((err = snd_card_register(card)) < 0) {
  2333. snd_card_free(card);
  2334. return err;
  2335. }
  2336. pci_set_drvdata(pci, card);
  2337. return 0;
  2338. __error:
  2339. snd_card_free(card);
  2340. return err;
  2341. }
  2342. static void snd_via82xx_remove(struct pci_dev *pci)
  2343. {
  2344. snd_card_free(pci_get_drvdata(pci));
  2345. }
  2346. static struct pci_driver via82xx_driver = {
  2347. .name = KBUILD_MODNAME,
  2348. .id_table = snd_via82xx_ids,
  2349. .probe = snd_via82xx_probe,
  2350. .remove = snd_via82xx_remove,
  2351. .driver = {
  2352. .pm = SND_VIA82XX_PM_OPS,
  2353. },
  2354. };
  2355. module_pci_driver(via82xx_driver);