hda_intel.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932
  1. /*
  2. *
  3. * hda_intel.c - Implementation of primary alsa driver code base
  4. * for Intel HD Audio.
  5. *
  6. * Copyright(c) 2004 Intel Corporation. All rights reserved.
  7. *
  8. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  9. * PeiSen Hou <pshou@realtek.com.tw>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program; if not, write to the Free Software Foundation, Inc., 59
  23. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. * CONTACTS:
  26. *
  27. * Matt Jared matt.jared@intel.com
  28. * Andy Kopp andy.kopp@intel.com
  29. * Dan Kogan dan.d.kogan@intel.com
  30. *
  31. * CHANGES:
  32. *
  33. * 2004.12.01 Major rewrite by tiwai, merged the work of pshou
  34. *
  35. */
  36. #include <asm/io.h>
  37. #include <linux/delay.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/moduleparam.h>
  43. #include <linux/init.h>
  44. #include <linux/slab.h>
  45. #include <linux/pci.h>
  46. #include <linux/mutex.h>
  47. #include <linux/reboot.h>
  48. #include <sound/core.h>
  49. #include <sound/initval.h>
  50. #include "hda_codec.h"
  51. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  52. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  53. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  54. static char *model[SNDRV_CARDS];
  55. static int position_fix[SNDRV_CARDS];
  56. static int bdl_pos_adj[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
  57. static int probe_mask[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
  58. static int probe_only[SNDRV_CARDS];
  59. static int single_cmd;
  60. static int enable_msi = -1;
  61. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  62. static char *patch[SNDRV_CARDS];
  63. #endif
  64. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  65. static int beep_mode[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] =
  66. CONFIG_SND_HDA_INPUT_BEEP_MODE};
  67. #endif
  68. module_param_array(index, int, NULL, 0444);
  69. MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
  70. module_param_array(id, charp, NULL, 0444);
  71. MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
  72. module_param_array(enable, bool, NULL, 0444);
  73. MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
  74. module_param_array(model, charp, NULL, 0444);
  75. MODULE_PARM_DESC(model, "Use the given board model.");
  76. module_param_array(position_fix, int, NULL, 0444);
  77. MODULE_PARM_DESC(position_fix, "DMA pointer read method."
  78. "(0 = auto, 1 = LPIB, 2 = POSBUF, 3 = VIACOMBO).");
  79. module_param_array(bdl_pos_adj, int, NULL, 0644);
  80. MODULE_PARM_DESC(bdl_pos_adj, "BDL position adjustment offset.");
  81. module_param_array(probe_mask, int, NULL, 0444);
  82. MODULE_PARM_DESC(probe_mask, "Bitmask to probe codecs (default = -1).");
  83. module_param_array(probe_only, int, NULL, 0444);
  84. MODULE_PARM_DESC(probe_only, "Only probing and no codec initialization.");
  85. module_param(single_cmd, bool, 0444);
  86. MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
  87. "(for debugging only).");
  88. module_param(enable_msi, int, 0444);
  89. MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
  90. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  91. module_param_array(patch, charp, NULL, 0444);
  92. MODULE_PARM_DESC(patch, "Patch file for Intel HD audio interface.");
  93. #endif
  94. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  95. module_param_array(beep_mode, int, NULL, 0444);
  96. MODULE_PARM_DESC(beep_mode, "Select HDA Beep registration mode "
  97. "(0=off, 1=on, 2=mute switch on/off) (default=1).");
  98. #endif
  99. #ifdef CONFIG_SND_HDA_POWER_SAVE
  100. static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
  101. module_param(power_save, int, 0644);
  102. MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
  103. "(in second, 0 = disable).");
  104. /* reset the HD-audio controller in power save mode.
  105. * this may give more power-saving, but will take longer time to
  106. * wake up.
  107. */
  108. static int power_save_controller = 1;
  109. module_param(power_save_controller, bool, 0644);
  110. MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
  111. #endif
  112. MODULE_LICENSE("GPL");
  113. MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
  114. "{Intel, ICH6M},"
  115. "{Intel, ICH7},"
  116. "{Intel, ESB2},"
  117. "{Intel, ICH8},"
  118. "{Intel, ICH9},"
  119. "{Intel, ICH10},"
  120. "{Intel, PCH},"
  121. "{Intel, CPT},"
  122. "{Intel, PPT},"
  123. "{Intel, PBG},"
  124. "{Intel, SCH},"
  125. "{ATI, SB450},"
  126. "{ATI, SB600},"
  127. "{ATI, RS600},"
  128. "{ATI, RS690},"
  129. "{ATI, RS780},"
  130. "{ATI, R600},"
  131. "{ATI, RV630},"
  132. "{ATI, RV610},"
  133. "{ATI, RV670},"
  134. "{ATI, RV635},"
  135. "{ATI, RV620},"
  136. "{ATI, RV770},"
  137. "{VIA, VT8251},"
  138. "{VIA, VT8237A},"
  139. "{SiS, SIS966},"
  140. "{ULI, M5461}}");
  141. MODULE_DESCRIPTION("Intel HDA driver");
  142. #ifdef CONFIG_SND_VERBOSE_PRINTK
  143. #define SFX /* nop */
  144. #else
  145. #define SFX "hda-intel: "
  146. #endif
  147. /*
  148. * registers
  149. */
  150. #define ICH6_REG_GCAP 0x00
  151. #define ICH6_GCAP_64OK (1 << 0) /* 64bit address support */
  152. #define ICH6_GCAP_NSDO (3 << 1) /* # of serial data out signals */
  153. #define ICH6_GCAP_BSS (31 << 3) /* # of bidirectional streams */
  154. #define ICH6_GCAP_ISS (15 << 8) /* # of input streams */
  155. #define ICH6_GCAP_OSS (15 << 12) /* # of output streams */
  156. #define ICH6_REG_VMIN 0x02
  157. #define ICH6_REG_VMAJ 0x03
  158. #define ICH6_REG_OUTPAY 0x04
  159. #define ICH6_REG_INPAY 0x06
  160. #define ICH6_REG_GCTL 0x08
  161. #define ICH6_GCTL_RESET (1 << 0) /* controller reset */
  162. #define ICH6_GCTL_FCNTRL (1 << 1) /* flush control */
  163. #define ICH6_GCTL_UNSOL (1 << 8) /* accept unsol. response enable */
  164. #define ICH6_REG_WAKEEN 0x0c
  165. #define ICH6_REG_STATESTS 0x0e
  166. #define ICH6_REG_GSTS 0x10
  167. #define ICH6_GSTS_FSTS (1 << 1) /* flush status */
  168. #define ICH6_REG_INTCTL 0x20
  169. #define ICH6_REG_INTSTS 0x24
  170. #define ICH6_REG_WALLCLK 0x30 /* 24Mhz source */
  171. #define ICH6_REG_SYNC 0x34
  172. #define ICH6_REG_CORBLBASE 0x40
  173. #define ICH6_REG_CORBUBASE 0x44
  174. #define ICH6_REG_CORBWP 0x48
  175. #define ICH6_REG_CORBRP 0x4a
  176. #define ICH6_CORBRP_RST (1 << 15) /* read pointer reset */
  177. #define ICH6_REG_CORBCTL 0x4c
  178. #define ICH6_CORBCTL_RUN (1 << 1) /* enable DMA */
  179. #define ICH6_CORBCTL_CMEIE (1 << 0) /* enable memory error irq */
  180. #define ICH6_REG_CORBSTS 0x4d
  181. #define ICH6_CORBSTS_CMEI (1 << 0) /* memory error indication */
  182. #define ICH6_REG_CORBSIZE 0x4e
  183. #define ICH6_REG_RIRBLBASE 0x50
  184. #define ICH6_REG_RIRBUBASE 0x54
  185. #define ICH6_REG_RIRBWP 0x58
  186. #define ICH6_RIRBWP_RST (1 << 15) /* write pointer reset */
  187. #define ICH6_REG_RINTCNT 0x5a
  188. #define ICH6_REG_RIRBCTL 0x5c
  189. #define ICH6_RBCTL_IRQ_EN (1 << 0) /* enable IRQ */
  190. #define ICH6_RBCTL_DMA_EN (1 << 1) /* enable DMA */
  191. #define ICH6_RBCTL_OVERRUN_EN (1 << 2) /* enable overrun irq */
  192. #define ICH6_REG_RIRBSTS 0x5d
  193. #define ICH6_RBSTS_IRQ (1 << 0) /* response irq */
  194. #define ICH6_RBSTS_OVERRUN (1 << 2) /* overrun irq */
  195. #define ICH6_REG_RIRBSIZE 0x5e
  196. #define ICH6_REG_IC 0x60
  197. #define ICH6_REG_IR 0x64
  198. #define ICH6_REG_IRS 0x68
  199. #define ICH6_IRS_VALID (1<<1)
  200. #define ICH6_IRS_BUSY (1<<0)
  201. #define ICH6_REG_DPLBASE 0x70
  202. #define ICH6_REG_DPUBASE 0x74
  203. #define ICH6_DPLBASE_ENABLE 0x1 /* Enable position buffer */
  204. /* SD offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  205. enum { SDI0, SDI1, SDI2, SDI3, SDO0, SDO1, SDO2, SDO3 };
  206. /* stream register offsets from stream base */
  207. #define ICH6_REG_SD_CTL 0x00
  208. #define ICH6_REG_SD_STS 0x03
  209. #define ICH6_REG_SD_LPIB 0x04
  210. #define ICH6_REG_SD_CBL 0x08
  211. #define ICH6_REG_SD_LVI 0x0c
  212. #define ICH6_REG_SD_FIFOW 0x0e
  213. #define ICH6_REG_SD_FIFOSIZE 0x10
  214. #define ICH6_REG_SD_FORMAT 0x12
  215. #define ICH6_REG_SD_BDLPL 0x18
  216. #define ICH6_REG_SD_BDLPU 0x1c
  217. /* PCI space */
  218. #define ICH6_PCIREG_TCSEL 0x44
  219. /*
  220. * other constants
  221. */
  222. /* max number of SDs */
  223. /* ICH, ATI and VIA have 4 playback and 4 capture */
  224. #define ICH6_NUM_CAPTURE 4
  225. #define ICH6_NUM_PLAYBACK 4
  226. /* ULI has 6 playback and 5 capture */
  227. #define ULI_NUM_CAPTURE 5
  228. #define ULI_NUM_PLAYBACK 6
  229. /* ATI HDMI has 1 playback and 0 capture */
  230. #define ATIHDMI_NUM_CAPTURE 0
  231. #define ATIHDMI_NUM_PLAYBACK 1
  232. /* TERA has 4 playback and 3 capture */
  233. #define TERA_NUM_CAPTURE 3
  234. #define TERA_NUM_PLAYBACK 4
  235. /* this number is statically defined for simplicity */
  236. #define MAX_AZX_DEV 16
  237. /* max number of fragments - we may use more if allocating more pages for BDL */
  238. #define BDL_SIZE 4096
  239. #define AZX_MAX_BDL_ENTRIES (BDL_SIZE / 16)
  240. #define AZX_MAX_FRAG 32
  241. /* max buffer size - no h/w limit, you can increase as you like */
  242. #define AZX_MAX_BUF_SIZE (1024*1024*1024)
  243. /* RIRB int mask: overrun[2], response[0] */
  244. #define RIRB_INT_RESPONSE 0x01
  245. #define RIRB_INT_OVERRUN 0x04
  246. #define RIRB_INT_MASK 0x05
  247. /* STATESTS int mask: S3,SD2,SD1,SD0 */
  248. #define AZX_MAX_CODECS 8
  249. #define AZX_DEFAULT_CODECS 4
  250. #define STATESTS_INT_MASK ((1 << AZX_MAX_CODECS) - 1)
  251. /* SD_CTL bits */
  252. #define SD_CTL_STREAM_RESET 0x01 /* stream reset bit */
  253. #define SD_CTL_DMA_START 0x02 /* stream DMA start bit */
  254. #define SD_CTL_STRIPE (3 << 16) /* stripe control */
  255. #define SD_CTL_TRAFFIC_PRIO (1 << 18) /* traffic priority */
  256. #define SD_CTL_DIR (1 << 19) /* bi-directional stream */
  257. #define SD_CTL_STREAM_TAG_MASK (0xf << 20)
  258. #define SD_CTL_STREAM_TAG_SHIFT 20
  259. /* SD_CTL and SD_STS */
  260. #define SD_INT_DESC_ERR 0x10 /* descriptor error interrupt */
  261. #define SD_INT_FIFO_ERR 0x08 /* FIFO error interrupt */
  262. #define SD_INT_COMPLETE 0x04 /* completion interrupt */
  263. #define SD_INT_MASK (SD_INT_DESC_ERR|SD_INT_FIFO_ERR|\
  264. SD_INT_COMPLETE)
  265. /* SD_STS */
  266. #define SD_STS_FIFO_READY 0x20 /* FIFO ready */
  267. /* INTCTL and INTSTS */
  268. #define ICH6_INT_ALL_STREAM 0xff /* all stream interrupts */
  269. #define ICH6_INT_CTRL_EN 0x40000000 /* controller interrupt enable bit */
  270. #define ICH6_INT_GLOBAL_EN 0x80000000 /* global interrupt enable bit */
  271. /* below are so far hardcoded - should read registers in future */
  272. #define ICH6_MAX_CORB_ENTRIES 256
  273. #define ICH6_MAX_RIRB_ENTRIES 256
  274. /* position fix mode */
  275. enum {
  276. POS_FIX_AUTO,
  277. POS_FIX_LPIB,
  278. POS_FIX_POSBUF,
  279. POS_FIX_VIACOMBO,
  280. };
  281. /* Defines for ATI HD Audio support in SB450 south bridge */
  282. #define ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR 0x42
  283. #define ATI_SB450_HDAUDIO_ENABLE_SNOOP 0x02
  284. /* Defines for Nvidia HDA support */
  285. #define NVIDIA_HDA_TRANSREG_ADDR 0x4e
  286. #define NVIDIA_HDA_ENABLE_COHBITS 0x0f
  287. #define NVIDIA_HDA_ISTRM_COH 0x4d
  288. #define NVIDIA_HDA_OSTRM_COH 0x4c
  289. #define NVIDIA_HDA_ENABLE_COHBIT 0x01
  290. /* Defines for Intel SCH HDA snoop control */
  291. #define INTEL_SCH_HDA_DEVC 0x78
  292. #define INTEL_SCH_HDA_DEVC_NOSNOOP (0x1<<11)
  293. /* Define IN stream 0 FIFO size offset in VIA controller */
  294. #define VIA_IN_STREAM0_FIFO_SIZE_OFFSET 0x90
  295. /* Define VIA HD Audio Device ID*/
  296. #define VIA_HDAC_DEVICE_ID 0x3288
  297. /* HD Audio class code */
  298. #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
  299. /*
  300. */
  301. struct azx_dev {
  302. struct snd_dma_buffer bdl; /* BDL buffer */
  303. u32 *posbuf; /* position buffer pointer */
  304. unsigned int bufsize; /* size of the play buffer in bytes */
  305. unsigned int period_bytes; /* size of the period in bytes */
  306. unsigned int frags; /* number for period in the play buffer */
  307. unsigned int fifo_size; /* FIFO size */
  308. unsigned long start_wallclk; /* start + minimum wallclk */
  309. unsigned long period_wallclk; /* wallclk for period */
  310. void __iomem *sd_addr; /* stream descriptor pointer */
  311. u32 sd_int_sta_mask; /* stream int status mask */
  312. /* pcm support */
  313. struct snd_pcm_substream *substream; /* assigned substream,
  314. * set in PCM open
  315. */
  316. unsigned int format_val; /* format value to be set in the
  317. * controller and the codec
  318. */
  319. unsigned char stream_tag; /* assigned stream */
  320. unsigned char index; /* stream index */
  321. int device; /* last device number assigned to */
  322. unsigned int opened :1;
  323. unsigned int running :1;
  324. unsigned int irq_pending :1;
  325. /*
  326. * For VIA:
  327. * A flag to ensure DMA position is 0
  328. * when link position is not greater than FIFO size
  329. */
  330. unsigned int insufficient :1;
  331. };
  332. /* CORB/RIRB */
  333. struct azx_rb {
  334. u32 *buf; /* CORB/RIRB buffer
  335. * Each CORB entry is 4byte, RIRB is 8byte
  336. */
  337. dma_addr_t addr; /* physical address of CORB/RIRB buffer */
  338. /* for RIRB */
  339. unsigned short rp, wp; /* read/write pointers */
  340. int cmds[AZX_MAX_CODECS]; /* number of pending requests */
  341. u32 res[AZX_MAX_CODECS]; /* last read value */
  342. };
  343. struct azx {
  344. struct snd_card *card;
  345. struct pci_dev *pci;
  346. int dev_index;
  347. /* chip type specific */
  348. int driver_type;
  349. unsigned int driver_caps;
  350. int playback_streams;
  351. int playback_index_offset;
  352. int capture_streams;
  353. int capture_index_offset;
  354. int num_streams;
  355. /* pci resources */
  356. unsigned long addr;
  357. void __iomem *remap_addr;
  358. int irq;
  359. /* locks */
  360. spinlock_t reg_lock;
  361. struct mutex open_mutex;
  362. /* streams (x num_streams) */
  363. struct azx_dev *azx_dev;
  364. /* PCM */
  365. struct snd_pcm *pcm[HDA_MAX_PCMS];
  366. /* HD codec */
  367. unsigned short codec_mask;
  368. int codec_probe_mask; /* copied from probe_mask option */
  369. struct hda_bus *bus;
  370. unsigned int beep_mode;
  371. /* CORB/RIRB */
  372. struct azx_rb corb;
  373. struct azx_rb rirb;
  374. /* CORB/RIRB and position buffers */
  375. struct snd_dma_buffer rb;
  376. struct snd_dma_buffer posbuf;
  377. /* flags */
  378. int position_fix[2]; /* for both playback/capture streams */
  379. int poll_count;
  380. unsigned int running :1;
  381. unsigned int initialized :1;
  382. unsigned int single_cmd :1;
  383. unsigned int polling_mode :1;
  384. unsigned int msi :1;
  385. unsigned int irq_pending_warned :1;
  386. unsigned int probing :1; /* codec probing phase */
  387. /* for debugging */
  388. unsigned int last_cmd[AZX_MAX_CODECS];
  389. /* for pending irqs */
  390. struct work_struct irq_pending_work;
  391. /* reboot notifier (for mysterious hangup problem at power-down) */
  392. struct notifier_block reboot_notifier;
  393. };
  394. /* driver types */
  395. enum {
  396. AZX_DRIVER_ICH,
  397. AZX_DRIVER_PCH,
  398. AZX_DRIVER_SCH,
  399. AZX_DRIVER_ATI,
  400. AZX_DRIVER_ATIHDMI,
  401. AZX_DRIVER_VIA,
  402. AZX_DRIVER_SIS,
  403. AZX_DRIVER_ULI,
  404. AZX_DRIVER_NVIDIA,
  405. AZX_DRIVER_TERA,
  406. AZX_DRIVER_CTX,
  407. AZX_DRIVER_GENERIC,
  408. AZX_NUM_DRIVERS, /* keep this as last entry */
  409. };
  410. /* driver quirks (capabilities) */
  411. /* bits 0-7 are used for indicating driver type */
  412. #define AZX_DCAPS_NO_TCSEL (1 << 8) /* No Intel TCSEL bit */
  413. #define AZX_DCAPS_NO_MSI (1 << 9) /* No MSI support */
  414. #define AZX_DCAPS_ATI_SNOOP (1 << 10) /* ATI snoop enable */
  415. #define AZX_DCAPS_NVIDIA_SNOOP (1 << 11) /* Nvidia snoop enable */
  416. #define AZX_DCAPS_SCH_SNOOP (1 << 12) /* SCH/PCH snoop enable */
  417. #define AZX_DCAPS_RIRB_DELAY (1 << 13) /* Long delay in read loop */
  418. #define AZX_DCAPS_RIRB_PRE_DELAY (1 << 14) /* Put a delay before read */
  419. #define AZX_DCAPS_CTX_WORKAROUND (1 << 15) /* X-Fi workaround */
  420. #define AZX_DCAPS_POSFIX_LPIB (1 << 16) /* Use LPIB as default */
  421. #define AZX_DCAPS_POSFIX_VIA (1 << 17) /* Use VIACOMBO as default */
  422. #define AZX_DCAPS_NO_64BIT (1 << 18) /* No 64bit address */
  423. #define AZX_DCAPS_SYNC_WRITE (1 << 19) /* sync each cmd write */
  424. /* quirks for ATI SB / AMD Hudson */
  425. #define AZX_DCAPS_PRESET_ATI_SB \
  426. (AZX_DCAPS_ATI_SNOOP | AZX_DCAPS_NO_TCSEL | \
  427. AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
  428. /* quirks for ATI/AMD HDMI */
  429. #define AZX_DCAPS_PRESET_ATI_HDMI \
  430. (AZX_DCAPS_NO_TCSEL | AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
  431. /* quirks for Nvidia */
  432. #define AZX_DCAPS_PRESET_NVIDIA \
  433. (AZX_DCAPS_NVIDIA_SNOOP | AZX_DCAPS_RIRB_DELAY | AZX_DCAPS_NO_MSI)
  434. static char *driver_short_names[] __devinitdata = {
  435. [AZX_DRIVER_ICH] = "HDA Intel",
  436. [AZX_DRIVER_PCH] = "HDA Intel PCH",
  437. [AZX_DRIVER_SCH] = "HDA Intel MID",
  438. [AZX_DRIVER_ATI] = "HDA ATI SB",
  439. [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
  440. [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
  441. [AZX_DRIVER_SIS] = "HDA SIS966",
  442. [AZX_DRIVER_ULI] = "HDA ULI M5461",
  443. [AZX_DRIVER_NVIDIA] = "HDA NVidia",
  444. [AZX_DRIVER_TERA] = "HDA Teradici",
  445. [AZX_DRIVER_CTX] = "HDA Creative",
  446. [AZX_DRIVER_GENERIC] = "HD-Audio Generic",
  447. };
  448. /*
  449. * macros for easy use
  450. */
  451. #define azx_writel(chip,reg,value) \
  452. writel(value, (chip)->remap_addr + ICH6_REG_##reg)
  453. #define azx_readl(chip,reg) \
  454. readl((chip)->remap_addr + ICH6_REG_##reg)
  455. #define azx_writew(chip,reg,value) \
  456. writew(value, (chip)->remap_addr + ICH6_REG_##reg)
  457. #define azx_readw(chip,reg) \
  458. readw((chip)->remap_addr + ICH6_REG_##reg)
  459. #define azx_writeb(chip,reg,value) \
  460. writeb(value, (chip)->remap_addr + ICH6_REG_##reg)
  461. #define azx_readb(chip,reg) \
  462. readb((chip)->remap_addr + ICH6_REG_##reg)
  463. #define azx_sd_writel(dev,reg,value) \
  464. writel(value, (dev)->sd_addr + ICH6_REG_##reg)
  465. #define azx_sd_readl(dev,reg) \
  466. readl((dev)->sd_addr + ICH6_REG_##reg)
  467. #define azx_sd_writew(dev,reg,value) \
  468. writew(value, (dev)->sd_addr + ICH6_REG_##reg)
  469. #define azx_sd_readw(dev,reg) \
  470. readw((dev)->sd_addr + ICH6_REG_##reg)
  471. #define azx_sd_writeb(dev,reg,value) \
  472. writeb(value, (dev)->sd_addr + ICH6_REG_##reg)
  473. #define azx_sd_readb(dev,reg) \
  474. readb((dev)->sd_addr + ICH6_REG_##reg)
  475. /* for pcm support */
  476. #define get_azx_dev(substream) (substream->runtime->private_data)
  477. static int azx_acquire_irq(struct azx *chip, int do_disconnect);
  478. static int azx_send_cmd(struct hda_bus *bus, unsigned int val);
  479. /*
  480. * Interface for HD codec
  481. */
  482. /*
  483. * CORB / RIRB interface
  484. */
  485. static int azx_alloc_cmd_io(struct azx *chip)
  486. {
  487. int err;
  488. /* single page (at least 4096 bytes) must suffice for both ringbuffes */
  489. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  490. snd_dma_pci_data(chip->pci),
  491. PAGE_SIZE, &chip->rb);
  492. if (err < 0) {
  493. snd_printk(KERN_ERR SFX "cannot allocate CORB/RIRB\n");
  494. return err;
  495. }
  496. return 0;
  497. }
  498. static void azx_init_cmd_io(struct azx *chip)
  499. {
  500. spin_lock_irq(&chip->reg_lock);
  501. /* CORB set up */
  502. chip->corb.addr = chip->rb.addr;
  503. chip->corb.buf = (u32 *)chip->rb.area;
  504. azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
  505. azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
  506. /* set the corb size to 256 entries (ULI requires explicitly) */
  507. azx_writeb(chip, CORBSIZE, 0x02);
  508. /* set the corb write pointer to 0 */
  509. azx_writew(chip, CORBWP, 0);
  510. /* reset the corb hw read pointer */
  511. azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
  512. /* enable corb dma */
  513. azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
  514. /* RIRB set up */
  515. chip->rirb.addr = chip->rb.addr + 2048;
  516. chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
  517. chip->rirb.wp = chip->rirb.rp = 0;
  518. memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
  519. azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
  520. azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
  521. /* set the rirb size to 256 entries (ULI requires explicitly) */
  522. azx_writeb(chip, RIRBSIZE, 0x02);
  523. /* reset the rirb hw write pointer */
  524. azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
  525. /* set N=1, get RIRB response interrupt for new entry */
  526. if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
  527. azx_writew(chip, RINTCNT, 0xc0);
  528. else
  529. azx_writew(chip, RINTCNT, 1);
  530. /* enable rirb dma and response irq */
  531. azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
  532. spin_unlock_irq(&chip->reg_lock);
  533. }
  534. static void azx_free_cmd_io(struct azx *chip)
  535. {
  536. spin_lock_irq(&chip->reg_lock);
  537. /* disable ringbuffer DMAs */
  538. azx_writeb(chip, RIRBCTL, 0);
  539. azx_writeb(chip, CORBCTL, 0);
  540. spin_unlock_irq(&chip->reg_lock);
  541. }
  542. static unsigned int azx_command_addr(u32 cmd)
  543. {
  544. unsigned int addr = cmd >> 28;
  545. if (addr >= AZX_MAX_CODECS) {
  546. snd_BUG();
  547. addr = 0;
  548. }
  549. return addr;
  550. }
  551. static unsigned int azx_response_addr(u32 res)
  552. {
  553. unsigned int addr = res & 0xf;
  554. if (addr >= AZX_MAX_CODECS) {
  555. snd_BUG();
  556. addr = 0;
  557. }
  558. return addr;
  559. }
  560. /* send a command */
  561. static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
  562. {
  563. struct azx *chip = bus->private_data;
  564. unsigned int addr = azx_command_addr(val);
  565. unsigned int wp;
  566. spin_lock_irq(&chip->reg_lock);
  567. /* add command to corb */
  568. wp = azx_readb(chip, CORBWP);
  569. wp++;
  570. wp %= ICH6_MAX_CORB_ENTRIES;
  571. chip->rirb.cmds[addr]++;
  572. chip->corb.buf[wp] = cpu_to_le32(val);
  573. azx_writel(chip, CORBWP, wp);
  574. spin_unlock_irq(&chip->reg_lock);
  575. return 0;
  576. }
  577. #define ICH6_RIRB_EX_UNSOL_EV (1<<4)
  578. /* retrieve RIRB entry - called from interrupt handler */
  579. static void azx_update_rirb(struct azx *chip)
  580. {
  581. unsigned int rp, wp;
  582. unsigned int addr;
  583. u32 res, res_ex;
  584. wp = azx_readb(chip, RIRBWP);
  585. if (wp == chip->rirb.wp)
  586. return;
  587. chip->rirb.wp = wp;
  588. while (chip->rirb.rp != wp) {
  589. chip->rirb.rp++;
  590. chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
  591. rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
  592. res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
  593. res = le32_to_cpu(chip->rirb.buf[rp]);
  594. addr = azx_response_addr(res_ex);
  595. if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
  596. snd_hda_queue_unsol_event(chip->bus, res, res_ex);
  597. else if (chip->rirb.cmds[addr]) {
  598. chip->rirb.res[addr] = res;
  599. smp_wmb();
  600. chip->rirb.cmds[addr]--;
  601. } else
  602. snd_printk(KERN_ERR SFX "spurious response %#x:%#x, "
  603. "last cmd=%#08x\n",
  604. res, res_ex,
  605. chip->last_cmd[addr]);
  606. }
  607. }
  608. /* receive a response */
  609. static unsigned int azx_rirb_get_response(struct hda_bus *bus,
  610. unsigned int addr)
  611. {
  612. struct azx *chip = bus->private_data;
  613. unsigned long timeout;
  614. int do_poll = 0;
  615. again:
  616. timeout = jiffies + msecs_to_jiffies(1000);
  617. for (;;) {
  618. if (chip->polling_mode || do_poll) {
  619. spin_lock_irq(&chip->reg_lock);
  620. azx_update_rirb(chip);
  621. spin_unlock_irq(&chip->reg_lock);
  622. }
  623. if (!chip->rirb.cmds[addr]) {
  624. smp_rmb();
  625. bus->rirb_error = 0;
  626. if (!do_poll)
  627. chip->poll_count = 0;
  628. return chip->rirb.res[addr]; /* the last value */
  629. }
  630. if (time_after(jiffies, timeout))
  631. break;
  632. if (bus->needs_damn_long_delay)
  633. msleep(2); /* temporary workaround */
  634. else {
  635. udelay(10);
  636. cond_resched();
  637. }
  638. }
  639. if (!chip->polling_mode && chip->poll_count < 2) {
  640. snd_printdd(SFX "azx_get_response timeout, "
  641. "polling the codec once: last cmd=0x%08x\n",
  642. chip->last_cmd[addr]);
  643. do_poll = 1;
  644. chip->poll_count++;
  645. goto again;
  646. }
  647. if (!chip->polling_mode) {
  648. snd_printk(KERN_WARNING SFX "azx_get_response timeout, "
  649. "switching to polling mode: last cmd=0x%08x\n",
  650. chip->last_cmd[addr]);
  651. chip->polling_mode = 1;
  652. goto again;
  653. }
  654. if (chip->msi) {
  655. snd_printk(KERN_WARNING SFX "No response from codec, "
  656. "disabling MSI: last cmd=0x%08x\n",
  657. chip->last_cmd[addr]);
  658. free_irq(chip->irq, chip);
  659. chip->irq = -1;
  660. pci_disable_msi(chip->pci);
  661. chip->msi = 0;
  662. if (azx_acquire_irq(chip, 1) < 0) {
  663. bus->rirb_error = 1;
  664. return -1;
  665. }
  666. goto again;
  667. }
  668. if (chip->probing) {
  669. /* If this critical timeout happens during the codec probing
  670. * phase, this is likely an access to a non-existing codec
  671. * slot. Better to return an error and reset the system.
  672. */
  673. return -1;
  674. }
  675. /* a fatal communication error; need either to reset or to fallback
  676. * to the single_cmd mode
  677. */
  678. bus->rirb_error = 1;
  679. if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
  680. bus->response_reset = 1;
  681. return -1; /* give a chance to retry */
  682. }
  683. snd_printk(KERN_ERR "hda_intel: azx_get_response timeout, "
  684. "switching to single_cmd mode: last cmd=0x%08x\n",
  685. chip->last_cmd[addr]);
  686. chip->single_cmd = 1;
  687. bus->response_reset = 0;
  688. /* release CORB/RIRB */
  689. azx_free_cmd_io(chip);
  690. /* disable unsolicited responses */
  691. azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
  692. return -1;
  693. }
  694. /*
  695. * Use the single immediate command instead of CORB/RIRB for simplicity
  696. *
  697. * Note: according to Intel, this is not preferred use. The command was
  698. * intended for the BIOS only, and may get confused with unsolicited
  699. * responses. So, we shouldn't use it for normal operation from the
  700. * driver.
  701. * I left the codes, however, for debugging/testing purposes.
  702. */
  703. /* receive a response */
  704. static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
  705. {
  706. int timeout = 50;
  707. while (timeout--) {
  708. /* check IRV busy bit */
  709. if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
  710. /* reuse rirb.res as the response return value */
  711. chip->rirb.res[addr] = azx_readl(chip, IR);
  712. return 0;
  713. }
  714. udelay(1);
  715. }
  716. if (printk_ratelimit())
  717. snd_printd(SFX "get_response timeout: IRS=0x%x\n",
  718. azx_readw(chip, IRS));
  719. chip->rirb.res[addr] = -1;
  720. return -EIO;
  721. }
  722. /* send a command */
  723. static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
  724. {
  725. struct azx *chip = bus->private_data;
  726. unsigned int addr = azx_command_addr(val);
  727. int timeout = 50;
  728. bus->rirb_error = 0;
  729. while (timeout--) {
  730. /* check ICB busy bit */
  731. if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
  732. /* Clear IRV valid bit */
  733. azx_writew(chip, IRS, azx_readw(chip, IRS) |
  734. ICH6_IRS_VALID);
  735. azx_writel(chip, IC, val);
  736. azx_writew(chip, IRS, azx_readw(chip, IRS) |
  737. ICH6_IRS_BUSY);
  738. return azx_single_wait_for_response(chip, addr);
  739. }
  740. udelay(1);
  741. }
  742. if (printk_ratelimit())
  743. snd_printd(SFX "send_cmd timeout: IRS=0x%x, val=0x%x\n",
  744. azx_readw(chip, IRS), val);
  745. return -EIO;
  746. }
  747. /* receive a response */
  748. static unsigned int azx_single_get_response(struct hda_bus *bus,
  749. unsigned int addr)
  750. {
  751. struct azx *chip = bus->private_data;
  752. return chip->rirb.res[addr];
  753. }
  754. /*
  755. * The below are the main callbacks from hda_codec.
  756. *
  757. * They are just the skeleton to call sub-callbacks according to the
  758. * current setting of chip->single_cmd.
  759. */
  760. /* send a command */
  761. static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
  762. {
  763. struct azx *chip = bus->private_data;
  764. chip->last_cmd[azx_command_addr(val)] = val;
  765. if (chip->single_cmd)
  766. return azx_single_send_cmd(bus, val);
  767. else
  768. return azx_corb_send_cmd(bus, val);
  769. }
  770. /* get a response */
  771. static unsigned int azx_get_response(struct hda_bus *bus,
  772. unsigned int addr)
  773. {
  774. struct azx *chip = bus->private_data;
  775. if (chip->single_cmd)
  776. return azx_single_get_response(bus, addr);
  777. else
  778. return azx_rirb_get_response(bus, addr);
  779. }
  780. #ifdef CONFIG_SND_HDA_POWER_SAVE
  781. static void azx_power_notify(struct hda_bus *bus);
  782. #endif
  783. /* reset codec link */
  784. static int azx_reset(struct azx *chip, int full_reset)
  785. {
  786. int count;
  787. if (!full_reset)
  788. goto __skip;
  789. /* clear STATESTS */
  790. azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
  791. /* reset controller */
  792. azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
  793. count = 50;
  794. while (azx_readb(chip, GCTL) && --count)
  795. msleep(1);
  796. /* delay for >= 100us for codec PLL to settle per spec
  797. * Rev 0.9 section 5.5.1
  798. */
  799. msleep(1);
  800. /* Bring controller out of reset */
  801. azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
  802. count = 50;
  803. while (!azx_readb(chip, GCTL) && --count)
  804. msleep(1);
  805. /* Brent Chartrand said to wait >= 540us for codecs to initialize */
  806. msleep(1);
  807. __skip:
  808. /* check to see if controller is ready */
  809. if (!azx_readb(chip, GCTL)) {
  810. snd_printd(SFX "azx_reset: controller not ready!\n");
  811. return -EBUSY;
  812. }
  813. /* Accept unsolicited responses */
  814. if (!chip->single_cmd)
  815. azx_writel(chip, GCTL, azx_readl(chip, GCTL) |
  816. ICH6_GCTL_UNSOL);
  817. /* detect codecs */
  818. if (!chip->codec_mask) {
  819. chip->codec_mask = azx_readw(chip, STATESTS);
  820. snd_printdd(SFX "codec_mask = 0x%x\n", chip->codec_mask);
  821. }
  822. return 0;
  823. }
  824. /*
  825. * Lowlevel interface
  826. */
  827. /* enable interrupts */
  828. static void azx_int_enable(struct azx *chip)
  829. {
  830. /* enable controller CIE and GIE */
  831. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
  832. ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
  833. }
  834. /* disable interrupts */
  835. static void azx_int_disable(struct azx *chip)
  836. {
  837. int i;
  838. /* disable interrupts in stream descriptor */
  839. for (i = 0; i < chip->num_streams; i++) {
  840. struct azx_dev *azx_dev = &chip->azx_dev[i];
  841. azx_sd_writeb(azx_dev, SD_CTL,
  842. azx_sd_readb(azx_dev, SD_CTL) & ~SD_INT_MASK);
  843. }
  844. /* disable SIE for all streams */
  845. azx_writeb(chip, INTCTL, 0);
  846. /* disable controller CIE and GIE */
  847. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
  848. ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
  849. }
  850. /* clear interrupts */
  851. static void azx_int_clear(struct azx *chip)
  852. {
  853. int i;
  854. /* clear stream status */
  855. for (i = 0; i < chip->num_streams; i++) {
  856. struct azx_dev *azx_dev = &chip->azx_dev[i];
  857. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  858. }
  859. /* clear STATESTS */
  860. azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
  861. /* clear rirb status */
  862. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  863. /* clear int status */
  864. azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
  865. }
  866. /* start a stream */
  867. static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
  868. {
  869. /*
  870. * Before stream start, initialize parameter
  871. */
  872. azx_dev->insufficient = 1;
  873. /* enable SIE */
  874. azx_writel(chip, INTCTL,
  875. azx_readl(chip, INTCTL) | (1 << azx_dev->index));
  876. /* set DMA start and interrupt mask */
  877. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
  878. SD_CTL_DMA_START | SD_INT_MASK);
  879. }
  880. /* stop DMA */
  881. static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
  882. {
  883. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) &
  884. ~(SD_CTL_DMA_START | SD_INT_MASK));
  885. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
  886. }
  887. /* stop a stream */
  888. static void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
  889. {
  890. azx_stream_clear(chip, azx_dev);
  891. /* disable SIE */
  892. azx_writel(chip, INTCTL,
  893. azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
  894. }
  895. /*
  896. * reset and start the controller registers
  897. */
  898. static void azx_init_chip(struct azx *chip, int full_reset)
  899. {
  900. if (chip->initialized)
  901. return;
  902. /* reset controller */
  903. azx_reset(chip, full_reset);
  904. /* initialize interrupts */
  905. azx_int_clear(chip);
  906. azx_int_enable(chip);
  907. /* initialize the codec command I/O */
  908. if (!chip->single_cmd)
  909. azx_init_cmd_io(chip);
  910. /* program the position buffer */
  911. azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
  912. azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr));
  913. chip->initialized = 1;
  914. }
  915. /*
  916. * initialize the PCI registers
  917. */
  918. /* update bits in a PCI register byte */
  919. static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
  920. unsigned char mask, unsigned char val)
  921. {
  922. unsigned char data;
  923. pci_read_config_byte(pci, reg, &data);
  924. data &= ~mask;
  925. data |= (val & mask);
  926. pci_write_config_byte(pci, reg, data);
  927. }
  928. static void azx_init_pci(struct azx *chip)
  929. {
  930. unsigned short snoop;
  931. /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
  932. * TCSEL == Traffic Class Select Register, which sets PCI express QOS
  933. * Ensuring these bits are 0 clears playback static on some HD Audio
  934. * codecs.
  935. * The PCI register TCSEL is defined in the Intel manuals.
  936. */
  937. if (!(chip->driver_caps & AZX_DCAPS_NO_TCSEL)) {
  938. snd_printdd(SFX "Clearing TCSEL\n");
  939. update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
  940. }
  941. /* For ATI SB450/600/700/800/900 and AMD Hudson azalia HD audio,
  942. * we need to enable snoop.
  943. */
  944. if (chip->driver_caps & AZX_DCAPS_ATI_SNOOP) {
  945. snd_printdd(SFX "Enabling ATI snoop\n");
  946. update_pci_byte(chip->pci,
  947. ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR,
  948. 0x07, ATI_SB450_HDAUDIO_ENABLE_SNOOP);
  949. }
  950. /* For NVIDIA HDA, enable snoop */
  951. if (chip->driver_caps & AZX_DCAPS_NVIDIA_SNOOP) {
  952. snd_printdd(SFX "Enabling Nvidia snoop\n");
  953. update_pci_byte(chip->pci,
  954. NVIDIA_HDA_TRANSREG_ADDR,
  955. 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
  956. update_pci_byte(chip->pci,
  957. NVIDIA_HDA_ISTRM_COH,
  958. 0x01, NVIDIA_HDA_ENABLE_COHBIT);
  959. update_pci_byte(chip->pci,
  960. NVIDIA_HDA_OSTRM_COH,
  961. 0x01, NVIDIA_HDA_ENABLE_COHBIT);
  962. }
  963. /* Enable SCH/PCH snoop if needed */
  964. if (chip->driver_caps & AZX_DCAPS_SCH_SNOOP) {
  965. pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
  966. if (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) {
  967. pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC,
  968. snoop & (~INTEL_SCH_HDA_DEVC_NOSNOOP));
  969. pci_read_config_word(chip->pci,
  970. INTEL_SCH_HDA_DEVC, &snoop);
  971. snd_printdd(SFX "HDA snoop disabled, enabling ... %s\n",
  972. (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)
  973. ? "Failed" : "OK");
  974. }
  975. }
  976. }
  977. static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
  978. /*
  979. * interrupt handler
  980. */
  981. static irqreturn_t azx_interrupt(int irq, void *dev_id)
  982. {
  983. struct azx *chip = dev_id;
  984. struct azx_dev *azx_dev;
  985. u32 status;
  986. u8 sd_status;
  987. int i, ok;
  988. spin_lock(&chip->reg_lock);
  989. status = azx_readl(chip, INTSTS);
  990. if (status == 0) {
  991. spin_unlock(&chip->reg_lock);
  992. return IRQ_NONE;
  993. }
  994. for (i = 0; i < chip->num_streams; i++) {
  995. azx_dev = &chip->azx_dev[i];
  996. if (status & azx_dev->sd_int_sta_mask) {
  997. sd_status = azx_sd_readb(azx_dev, SD_STS);
  998. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  999. if (!azx_dev->substream || !azx_dev->running ||
  1000. !(sd_status & SD_INT_COMPLETE))
  1001. continue;
  1002. /* check whether this IRQ is really acceptable */
  1003. ok = azx_position_ok(chip, azx_dev);
  1004. if (ok == 1) {
  1005. azx_dev->irq_pending = 0;
  1006. spin_unlock(&chip->reg_lock);
  1007. snd_pcm_period_elapsed(azx_dev->substream);
  1008. spin_lock(&chip->reg_lock);
  1009. } else if (ok == 0 && chip->bus && chip->bus->workq) {
  1010. /* bogus IRQ, process it later */
  1011. azx_dev->irq_pending = 1;
  1012. queue_work(chip->bus->workq,
  1013. &chip->irq_pending_work);
  1014. }
  1015. }
  1016. }
  1017. /* clear rirb int */
  1018. status = azx_readb(chip, RIRBSTS);
  1019. if (status & RIRB_INT_MASK) {
  1020. if (status & RIRB_INT_RESPONSE) {
  1021. if (chip->driver_caps & AZX_DCAPS_RIRB_PRE_DELAY)
  1022. udelay(80);
  1023. azx_update_rirb(chip);
  1024. }
  1025. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  1026. }
  1027. #if 0
  1028. /* clear state status int */
  1029. if (azx_readb(chip, STATESTS) & 0x04)
  1030. azx_writeb(chip, STATESTS, 0x04);
  1031. #endif
  1032. spin_unlock(&chip->reg_lock);
  1033. return IRQ_HANDLED;
  1034. }
  1035. /*
  1036. * set up a BDL entry
  1037. */
  1038. static int setup_bdle(struct snd_pcm_substream *substream,
  1039. struct azx_dev *azx_dev, u32 **bdlp,
  1040. int ofs, int size, int with_ioc)
  1041. {
  1042. u32 *bdl = *bdlp;
  1043. while (size > 0) {
  1044. dma_addr_t addr;
  1045. int chunk;
  1046. if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
  1047. return -EINVAL;
  1048. addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  1049. /* program the address field of the BDL entry */
  1050. bdl[0] = cpu_to_le32((u32)addr);
  1051. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  1052. /* program the size field of the BDL entry */
  1053. chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
  1054. bdl[2] = cpu_to_le32(chunk);
  1055. /* program the IOC to enable interrupt
  1056. * only when the whole fragment is processed
  1057. */
  1058. size -= chunk;
  1059. bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
  1060. bdl += 4;
  1061. azx_dev->frags++;
  1062. ofs += chunk;
  1063. }
  1064. *bdlp = bdl;
  1065. return ofs;
  1066. }
  1067. /*
  1068. * set up BDL entries
  1069. */
  1070. static int azx_setup_periods(struct azx *chip,
  1071. struct snd_pcm_substream *substream,
  1072. struct azx_dev *azx_dev)
  1073. {
  1074. u32 *bdl;
  1075. int i, ofs, periods, period_bytes;
  1076. int pos_adj;
  1077. /* reset BDL address */
  1078. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  1079. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  1080. period_bytes = azx_dev->period_bytes;
  1081. periods = azx_dev->bufsize / period_bytes;
  1082. /* program the initial BDL entries */
  1083. bdl = (u32 *)azx_dev->bdl.area;
  1084. ofs = 0;
  1085. azx_dev->frags = 0;
  1086. pos_adj = bdl_pos_adj[chip->dev_index];
  1087. if (pos_adj > 0) {
  1088. struct snd_pcm_runtime *runtime = substream->runtime;
  1089. int pos_align = pos_adj;
  1090. pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
  1091. if (!pos_adj)
  1092. pos_adj = pos_align;
  1093. else
  1094. pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
  1095. pos_align;
  1096. pos_adj = frames_to_bytes(runtime, pos_adj);
  1097. if (pos_adj >= period_bytes) {
  1098. snd_printk(KERN_WARNING SFX "Too big adjustment %d\n",
  1099. bdl_pos_adj[chip->dev_index]);
  1100. pos_adj = 0;
  1101. } else {
  1102. ofs = setup_bdle(substream, azx_dev,
  1103. &bdl, ofs, pos_adj,
  1104. !substream->runtime->no_period_wakeup);
  1105. if (ofs < 0)
  1106. goto error;
  1107. }
  1108. } else
  1109. pos_adj = 0;
  1110. for (i = 0; i < periods; i++) {
  1111. if (i == periods - 1 && pos_adj)
  1112. ofs = setup_bdle(substream, azx_dev, &bdl, ofs,
  1113. period_bytes - pos_adj, 0);
  1114. else
  1115. ofs = setup_bdle(substream, azx_dev, &bdl, ofs,
  1116. period_bytes,
  1117. !substream->runtime->no_period_wakeup);
  1118. if (ofs < 0)
  1119. goto error;
  1120. }
  1121. return 0;
  1122. error:
  1123. snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
  1124. azx_dev->bufsize, period_bytes);
  1125. return -EINVAL;
  1126. }
  1127. /* reset stream */
  1128. static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
  1129. {
  1130. unsigned char val;
  1131. int timeout;
  1132. azx_stream_clear(chip, azx_dev);
  1133. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
  1134. SD_CTL_STREAM_RESET);
  1135. udelay(3);
  1136. timeout = 300;
  1137. while (!((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  1138. --timeout)
  1139. ;
  1140. val &= ~SD_CTL_STREAM_RESET;
  1141. azx_sd_writeb(azx_dev, SD_CTL, val);
  1142. udelay(3);
  1143. timeout = 300;
  1144. /* waiting for hardware to report that the stream is out of reset */
  1145. while (((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  1146. --timeout)
  1147. ;
  1148. /* reset first position - may not be synced with hw at this time */
  1149. *azx_dev->posbuf = 0;
  1150. }
  1151. /*
  1152. * set up the SD for streaming
  1153. */
  1154. static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
  1155. {
  1156. /* make sure the run bit is zero for SD */
  1157. azx_stream_clear(chip, azx_dev);
  1158. /* program the stream_tag */
  1159. azx_sd_writel(azx_dev, SD_CTL,
  1160. (azx_sd_readl(azx_dev, SD_CTL) & ~SD_CTL_STREAM_TAG_MASK)|
  1161. (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT));
  1162. /* program the length of samples in cyclic buffer */
  1163. azx_sd_writel(azx_dev, SD_CBL, azx_dev->bufsize);
  1164. /* program the stream format */
  1165. /* this value needs to be the same as the one programmed */
  1166. azx_sd_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
  1167. /* program the stream LVI (last valid index) of the BDL */
  1168. azx_sd_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
  1169. /* program the BDL address */
  1170. /* lower BDL address */
  1171. azx_sd_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
  1172. /* upper BDL address */
  1173. azx_sd_writel(azx_dev, SD_BDLPU, upper_32_bits(azx_dev->bdl.addr));
  1174. /* enable the position buffer */
  1175. if (chip->position_fix[0] != POS_FIX_LPIB ||
  1176. chip->position_fix[1] != POS_FIX_LPIB) {
  1177. if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
  1178. azx_writel(chip, DPLBASE,
  1179. (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
  1180. }
  1181. /* set the interrupt enable bits in the descriptor control register */
  1182. azx_sd_writel(azx_dev, SD_CTL,
  1183. azx_sd_readl(azx_dev, SD_CTL) | SD_INT_MASK);
  1184. return 0;
  1185. }
  1186. /*
  1187. * Probe the given codec address
  1188. */
  1189. static int probe_codec(struct azx *chip, int addr)
  1190. {
  1191. unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
  1192. (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
  1193. unsigned int res;
  1194. mutex_lock(&chip->bus->cmd_mutex);
  1195. chip->probing = 1;
  1196. azx_send_cmd(chip->bus, cmd);
  1197. res = azx_get_response(chip->bus, addr);
  1198. chip->probing = 0;
  1199. mutex_unlock(&chip->bus->cmd_mutex);
  1200. if (res == -1)
  1201. return -EIO;
  1202. snd_printdd(SFX "codec #%d probed OK\n", addr);
  1203. return 0;
  1204. }
  1205. static int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
  1206. struct hda_pcm *cpcm);
  1207. static void azx_stop_chip(struct azx *chip);
  1208. static void azx_bus_reset(struct hda_bus *bus)
  1209. {
  1210. struct azx *chip = bus->private_data;
  1211. bus->in_reset = 1;
  1212. azx_stop_chip(chip);
  1213. azx_init_chip(chip, 1);
  1214. #ifdef CONFIG_PM
  1215. if (chip->initialized) {
  1216. int i;
  1217. for (i = 0; i < HDA_MAX_PCMS; i++)
  1218. snd_pcm_suspend_all(chip->pcm[i]);
  1219. snd_hda_suspend(chip->bus);
  1220. snd_hda_resume(chip->bus);
  1221. }
  1222. #endif
  1223. bus->in_reset = 0;
  1224. }
  1225. /*
  1226. * Codec initialization
  1227. */
  1228. /* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
  1229. static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] __devinitdata = {
  1230. [AZX_DRIVER_NVIDIA] = 8,
  1231. [AZX_DRIVER_TERA] = 1,
  1232. };
  1233. static int __devinit azx_codec_create(struct azx *chip, const char *model)
  1234. {
  1235. struct hda_bus_template bus_temp;
  1236. int c, codecs, err;
  1237. int max_slots;
  1238. memset(&bus_temp, 0, sizeof(bus_temp));
  1239. bus_temp.private_data = chip;
  1240. bus_temp.modelname = model;
  1241. bus_temp.pci = chip->pci;
  1242. bus_temp.ops.command = azx_send_cmd;
  1243. bus_temp.ops.get_response = azx_get_response;
  1244. bus_temp.ops.attach_pcm = azx_attach_pcm_stream;
  1245. bus_temp.ops.bus_reset = azx_bus_reset;
  1246. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1247. bus_temp.power_save = &power_save;
  1248. bus_temp.ops.pm_notify = azx_power_notify;
  1249. #endif
  1250. err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus);
  1251. if (err < 0)
  1252. return err;
  1253. if (chip->driver_caps & AZX_DCAPS_RIRB_DELAY) {
  1254. snd_printd(SFX "Enable delay in RIRB handling\n");
  1255. chip->bus->needs_damn_long_delay = 1;
  1256. }
  1257. codecs = 0;
  1258. max_slots = azx_max_codecs[chip->driver_type];
  1259. if (!max_slots)
  1260. max_slots = AZX_DEFAULT_CODECS;
  1261. /* First try to probe all given codec slots */
  1262. for (c = 0; c < max_slots; c++) {
  1263. if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
  1264. if (probe_codec(chip, c) < 0) {
  1265. /* Some BIOSen give you wrong codec addresses
  1266. * that don't exist
  1267. */
  1268. snd_printk(KERN_WARNING SFX
  1269. "Codec #%d probe error; "
  1270. "disabling it...\n", c);
  1271. chip->codec_mask &= ~(1 << c);
  1272. /* More badly, accessing to a non-existing
  1273. * codec often screws up the controller chip,
  1274. * and disturbs the further communications.
  1275. * Thus if an error occurs during probing,
  1276. * better to reset the controller chip to
  1277. * get back to the sanity state.
  1278. */
  1279. azx_stop_chip(chip);
  1280. azx_init_chip(chip, 1);
  1281. }
  1282. }
  1283. }
  1284. /* AMD chipsets often cause the communication stalls upon certain
  1285. * sequence like the pin-detection. It seems that forcing the synced
  1286. * access works around the stall. Grrr...
  1287. */
  1288. if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) {
  1289. snd_printd(SFX "Enable sync_write for stable communication\n");
  1290. chip->bus->sync_write = 1;
  1291. chip->bus->allow_bus_reset = 1;
  1292. }
  1293. /* Then create codec instances */
  1294. for (c = 0; c < max_slots; c++) {
  1295. if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
  1296. struct hda_codec *codec;
  1297. err = snd_hda_codec_new(chip->bus, c, &codec);
  1298. if (err < 0)
  1299. continue;
  1300. codec->beep_mode = chip->beep_mode;
  1301. codecs++;
  1302. }
  1303. }
  1304. if (!codecs) {
  1305. snd_printk(KERN_ERR SFX "no codecs initialized\n");
  1306. return -ENXIO;
  1307. }
  1308. return 0;
  1309. }
  1310. /* configure each codec instance */
  1311. static int __devinit azx_codec_configure(struct azx *chip)
  1312. {
  1313. struct hda_codec *codec;
  1314. list_for_each_entry(codec, &chip->bus->codec_list, list) {
  1315. snd_hda_codec_configure(codec);
  1316. }
  1317. return 0;
  1318. }
  1319. /*
  1320. * PCM support
  1321. */
  1322. /* assign a stream for the PCM */
  1323. static inline struct azx_dev *
  1324. azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
  1325. {
  1326. int dev, i, nums;
  1327. struct azx_dev *res = NULL;
  1328. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1329. dev = chip->playback_index_offset;
  1330. nums = chip->playback_streams;
  1331. } else {
  1332. dev = chip->capture_index_offset;
  1333. nums = chip->capture_streams;
  1334. }
  1335. for (i = 0; i < nums; i++, dev++)
  1336. if (!chip->azx_dev[dev].opened) {
  1337. res = &chip->azx_dev[dev];
  1338. if (res->device == substream->pcm->device)
  1339. break;
  1340. }
  1341. if (res) {
  1342. res->opened = 1;
  1343. res->device = substream->pcm->device;
  1344. }
  1345. return res;
  1346. }
  1347. /* release the assigned stream */
  1348. static inline void azx_release_device(struct azx_dev *azx_dev)
  1349. {
  1350. azx_dev->opened = 0;
  1351. }
  1352. static struct snd_pcm_hardware azx_pcm_hw = {
  1353. .info = (SNDRV_PCM_INFO_MMAP |
  1354. SNDRV_PCM_INFO_INTERLEAVED |
  1355. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1356. SNDRV_PCM_INFO_MMAP_VALID |
  1357. /* No full-resume yet implemented */
  1358. /* SNDRV_PCM_INFO_RESUME |*/
  1359. SNDRV_PCM_INFO_PAUSE |
  1360. SNDRV_PCM_INFO_SYNC_START |
  1361. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
  1362. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1363. .rates = SNDRV_PCM_RATE_48000,
  1364. .rate_min = 48000,
  1365. .rate_max = 48000,
  1366. .channels_min = 2,
  1367. .channels_max = 2,
  1368. .buffer_bytes_max = AZX_MAX_BUF_SIZE,
  1369. .period_bytes_min = 128,
  1370. .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
  1371. .periods_min = 2,
  1372. .periods_max = AZX_MAX_FRAG,
  1373. .fifo_size = 0,
  1374. };
  1375. struct azx_pcm {
  1376. struct azx *chip;
  1377. struct hda_codec *codec;
  1378. struct hda_pcm_stream *hinfo[2];
  1379. };
  1380. static int azx_pcm_open(struct snd_pcm_substream *substream)
  1381. {
  1382. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1383. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1384. struct azx *chip = apcm->chip;
  1385. struct azx_dev *azx_dev;
  1386. struct snd_pcm_runtime *runtime = substream->runtime;
  1387. unsigned long flags;
  1388. int err;
  1389. mutex_lock(&chip->open_mutex);
  1390. azx_dev = azx_assign_device(chip, substream);
  1391. if (azx_dev == NULL) {
  1392. mutex_unlock(&chip->open_mutex);
  1393. return -EBUSY;
  1394. }
  1395. runtime->hw = azx_pcm_hw;
  1396. runtime->hw.channels_min = hinfo->channels_min;
  1397. runtime->hw.channels_max = hinfo->channels_max;
  1398. runtime->hw.formats = hinfo->formats;
  1399. runtime->hw.rates = hinfo->rates;
  1400. snd_pcm_limit_hw_rates(runtime);
  1401. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  1402. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1403. 128);
  1404. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1405. 128);
  1406. snd_hda_power_up(apcm->codec);
  1407. err = hinfo->ops.open(hinfo, apcm->codec, substream);
  1408. if (err < 0) {
  1409. azx_release_device(azx_dev);
  1410. snd_hda_power_down(apcm->codec);
  1411. mutex_unlock(&chip->open_mutex);
  1412. return err;
  1413. }
  1414. snd_pcm_limit_hw_rates(runtime);
  1415. /* sanity check */
  1416. if (snd_BUG_ON(!runtime->hw.channels_min) ||
  1417. snd_BUG_ON(!runtime->hw.channels_max) ||
  1418. snd_BUG_ON(!runtime->hw.formats) ||
  1419. snd_BUG_ON(!runtime->hw.rates)) {
  1420. azx_release_device(azx_dev);
  1421. hinfo->ops.close(hinfo, apcm->codec, substream);
  1422. snd_hda_power_down(apcm->codec);
  1423. mutex_unlock(&chip->open_mutex);
  1424. return -EINVAL;
  1425. }
  1426. spin_lock_irqsave(&chip->reg_lock, flags);
  1427. azx_dev->substream = substream;
  1428. azx_dev->running = 0;
  1429. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1430. runtime->private_data = azx_dev;
  1431. snd_pcm_set_sync(substream);
  1432. mutex_unlock(&chip->open_mutex);
  1433. return 0;
  1434. }
  1435. static int azx_pcm_close(struct snd_pcm_substream *substream)
  1436. {
  1437. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1438. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1439. struct azx *chip = apcm->chip;
  1440. struct azx_dev *azx_dev = get_azx_dev(substream);
  1441. unsigned long flags;
  1442. mutex_lock(&chip->open_mutex);
  1443. spin_lock_irqsave(&chip->reg_lock, flags);
  1444. azx_dev->substream = NULL;
  1445. azx_dev->running = 0;
  1446. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1447. azx_release_device(azx_dev);
  1448. hinfo->ops.close(hinfo, apcm->codec, substream);
  1449. snd_hda_power_down(apcm->codec);
  1450. mutex_unlock(&chip->open_mutex);
  1451. return 0;
  1452. }
  1453. static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
  1454. struct snd_pcm_hw_params *hw_params)
  1455. {
  1456. struct azx_dev *azx_dev = get_azx_dev(substream);
  1457. azx_dev->bufsize = 0;
  1458. azx_dev->period_bytes = 0;
  1459. azx_dev->format_val = 0;
  1460. return snd_pcm_lib_malloc_pages(substream,
  1461. params_buffer_bytes(hw_params));
  1462. }
  1463. static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
  1464. {
  1465. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1466. struct azx_dev *azx_dev = get_azx_dev(substream);
  1467. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1468. /* reset BDL address */
  1469. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  1470. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  1471. azx_sd_writel(azx_dev, SD_CTL, 0);
  1472. azx_dev->bufsize = 0;
  1473. azx_dev->period_bytes = 0;
  1474. azx_dev->format_val = 0;
  1475. snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
  1476. return snd_pcm_lib_free_pages(substream);
  1477. }
  1478. static int azx_pcm_prepare(struct snd_pcm_substream *substream)
  1479. {
  1480. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1481. struct azx *chip = apcm->chip;
  1482. struct azx_dev *azx_dev = get_azx_dev(substream);
  1483. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1484. struct snd_pcm_runtime *runtime = substream->runtime;
  1485. unsigned int bufsize, period_bytes, format_val, stream_tag;
  1486. int err;
  1487. azx_stream_reset(chip, azx_dev);
  1488. format_val = snd_hda_calc_stream_format(runtime->rate,
  1489. runtime->channels,
  1490. runtime->format,
  1491. hinfo->maxbps,
  1492. apcm->codec->spdif_ctls);
  1493. if (!format_val) {
  1494. snd_printk(KERN_ERR SFX
  1495. "invalid format_val, rate=%d, ch=%d, format=%d\n",
  1496. runtime->rate, runtime->channels, runtime->format);
  1497. return -EINVAL;
  1498. }
  1499. bufsize = snd_pcm_lib_buffer_bytes(substream);
  1500. period_bytes = snd_pcm_lib_period_bytes(substream);
  1501. snd_printdd(SFX "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
  1502. bufsize, format_val);
  1503. if (bufsize != azx_dev->bufsize ||
  1504. period_bytes != azx_dev->period_bytes ||
  1505. format_val != azx_dev->format_val) {
  1506. azx_dev->bufsize = bufsize;
  1507. azx_dev->period_bytes = period_bytes;
  1508. azx_dev->format_val = format_val;
  1509. err = azx_setup_periods(chip, substream, azx_dev);
  1510. if (err < 0)
  1511. return err;
  1512. }
  1513. /* wallclk has 24Mhz clock source */
  1514. azx_dev->period_wallclk = (((runtime->period_size * 24000) /
  1515. runtime->rate) * 1000);
  1516. azx_setup_controller(chip, azx_dev);
  1517. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1518. azx_dev->fifo_size = azx_sd_readw(azx_dev, SD_FIFOSIZE) + 1;
  1519. else
  1520. azx_dev->fifo_size = 0;
  1521. stream_tag = azx_dev->stream_tag;
  1522. /* CA-IBG chips need the playback stream starting from 1 */
  1523. if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
  1524. stream_tag > chip->capture_streams)
  1525. stream_tag -= chip->capture_streams;
  1526. return snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
  1527. azx_dev->format_val, substream);
  1528. }
  1529. static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  1530. {
  1531. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1532. struct azx *chip = apcm->chip;
  1533. struct azx_dev *azx_dev;
  1534. struct snd_pcm_substream *s;
  1535. int rstart = 0, start, nsync = 0, sbits = 0;
  1536. int nwait, timeout;
  1537. switch (cmd) {
  1538. case SNDRV_PCM_TRIGGER_START:
  1539. rstart = 1;
  1540. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1541. case SNDRV_PCM_TRIGGER_RESUME:
  1542. start = 1;
  1543. break;
  1544. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1545. case SNDRV_PCM_TRIGGER_SUSPEND:
  1546. case SNDRV_PCM_TRIGGER_STOP:
  1547. start = 0;
  1548. break;
  1549. default:
  1550. return -EINVAL;
  1551. }
  1552. snd_pcm_group_for_each_entry(s, substream) {
  1553. if (s->pcm->card != substream->pcm->card)
  1554. continue;
  1555. azx_dev = get_azx_dev(s);
  1556. sbits |= 1 << azx_dev->index;
  1557. nsync++;
  1558. snd_pcm_trigger_done(s, substream);
  1559. }
  1560. spin_lock(&chip->reg_lock);
  1561. if (nsync > 1) {
  1562. /* first, set SYNC bits of corresponding streams */
  1563. azx_writel(chip, SYNC, azx_readl(chip, SYNC) | sbits);
  1564. }
  1565. snd_pcm_group_for_each_entry(s, substream) {
  1566. if (s->pcm->card != substream->pcm->card)
  1567. continue;
  1568. azx_dev = get_azx_dev(s);
  1569. if (start) {
  1570. azx_dev->start_wallclk = azx_readl(chip, WALLCLK);
  1571. if (!rstart)
  1572. azx_dev->start_wallclk -=
  1573. azx_dev->period_wallclk;
  1574. azx_stream_start(chip, azx_dev);
  1575. } else {
  1576. azx_stream_stop(chip, azx_dev);
  1577. }
  1578. azx_dev->running = start;
  1579. }
  1580. spin_unlock(&chip->reg_lock);
  1581. if (start) {
  1582. if (nsync == 1)
  1583. return 0;
  1584. /* wait until all FIFOs get ready */
  1585. for (timeout = 5000; timeout; timeout--) {
  1586. nwait = 0;
  1587. snd_pcm_group_for_each_entry(s, substream) {
  1588. if (s->pcm->card != substream->pcm->card)
  1589. continue;
  1590. azx_dev = get_azx_dev(s);
  1591. if (!(azx_sd_readb(azx_dev, SD_STS) &
  1592. SD_STS_FIFO_READY))
  1593. nwait++;
  1594. }
  1595. if (!nwait)
  1596. break;
  1597. cpu_relax();
  1598. }
  1599. } else {
  1600. /* wait until all RUN bits are cleared */
  1601. for (timeout = 5000; timeout; timeout--) {
  1602. nwait = 0;
  1603. snd_pcm_group_for_each_entry(s, substream) {
  1604. if (s->pcm->card != substream->pcm->card)
  1605. continue;
  1606. azx_dev = get_azx_dev(s);
  1607. if (azx_sd_readb(azx_dev, SD_CTL) &
  1608. SD_CTL_DMA_START)
  1609. nwait++;
  1610. }
  1611. if (!nwait)
  1612. break;
  1613. cpu_relax();
  1614. }
  1615. }
  1616. if (nsync > 1) {
  1617. spin_lock(&chip->reg_lock);
  1618. /* reset SYNC bits */
  1619. azx_writel(chip, SYNC, azx_readl(chip, SYNC) & ~sbits);
  1620. spin_unlock(&chip->reg_lock);
  1621. }
  1622. return 0;
  1623. }
  1624. /* get the current DMA position with correction on VIA chips */
  1625. static unsigned int azx_via_get_position(struct azx *chip,
  1626. struct azx_dev *azx_dev)
  1627. {
  1628. unsigned int link_pos, mini_pos, bound_pos;
  1629. unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
  1630. unsigned int fifo_size;
  1631. link_pos = azx_sd_readl(azx_dev, SD_LPIB);
  1632. if (azx_dev->index >= 4) {
  1633. /* Playback, no problem using link position */
  1634. return link_pos;
  1635. }
  1636. /* Capture */
  1637. /* For new chipset,
  1638. * use mod to get the DMA position just like old chipset
  1639. */
  1640. mod_dma_pos = le32_to_cpu(*azx_dev->posbuf);
  1641. mod_dma_pos %= azx_dev->period_bytes;
  1642. /* azx_dev->fifo_size can't get FIFO size of in stream.
  1643. * Get from base address + offset.
  1644. */
  1645. fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
  1646. if (azx_dev->insufficient) {
  1647. /* Link position never gather than FIFO size */
  1648. if (link_pos <= fifo_size)
  1649. return 0;
  1650. azx_dev->insufficient = 0;
  1651. }
  1652. if (link_pos <= fifo_size)
  1653. mini_pos = azx_dev->bufsize + link_pos - fifo_size;
  1654. else
  1655. mini_pos = link_pos - fifo_size;
  1656. /* Find nearest previous boudary */
  1657. mod_mini_pos = mini_pos % azx_dev->period_bytes;
  1658. mod_link_pos = link_pos % azx_dev->period_bytes;
  1659. if (mod_link_pos >= fifo_size)
  1660. bound_pos = link_pos - mod_link_pos;
  1661. else if (mod_dma_pos >= mod_mini_pos)
  1662. bound_pos = mini_pos - mod_mini_pos;
  1663. else {
  1664. bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes;
  1665. if (bound_pos >= azx_dev->bufsize)
  1666. bound_pos = 0;
  1667. }
  1668. /* Calculate real DMA position we want */
  1669. return bound_pos + mod_dma_pos;
  1670. }
  1671. static unsigned int azx_get_position(struct azx *chip,
  1672. struct azx_dev *azx_dev)
  1673. {
  1674. unsigned int pos;
  1675. int stream = azx_dev->substream->stream;
  1676. switch (chip->position_fix[stream]) {
  1677. case POS_FIX_LPIB:
  1678. /* read LPIB */
  1679. pos = azx_sd_readl(azx_dev, SD_LPIB);
  1680. break;
  1681. case POS_FIX_VIACOMBO:
  1682. pos = azx_via_get_position(chip, azx_dev);
  1683. break;
  1684. default:
  1685. /* use the position buffer */
  1686. pos = le32_to_cpu(*azx_dev->posbuf);
  1687. }
  1688. if (pos >= azx_dev->bufsize)
  1689. pos = 0;
  1690. return pos;
  1691. }
  1692. static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
  1693. {
  1694. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1695. struct azx *chip = apcm->chip;
  1696. struct azx_dev *azx_dev = get_azx_dev(substream);
  1697. return bytes_to_frames(substream->runtime,
  1698. azx_get_position(chip, azx_dev));
  1699. }
  1700. /*
  1701. * Check whether the current DMA position is acceptable for updating
  1702. * periods. Returns non-zero if it's OK.
  1703. *
  1704. * Many HD-audio controllers appear pretty inaccurate about
  1705. * the update-IRQ timing. The IRQ is issued before actually the
  1706. * data is processed. So, we need to process it afterwords in a
  1707. * workqueue.
  1708. */
  1709. static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
  1710. {
  1711. u32 wallclk;
  1712. unsigned int pos;
  1713. int stream;
  1714. wallclk = azx_readl(chip, WALLCLK) - azx_dev->start_wallclk;
  1715. if (wallclk < (azx_dev->period_wallclk * 2) / 3)
  1716. return -1; /* bogus (too early) interrupt */
  1717. stream = azx_dev->substream->stream;
  1718. pos = azx_get_position(chip, azx_dev);
  1719. if (chip->position_fix[stream] == POS_FIX_AUTO) {
  1720. if (!pos) {
  1721. printk(KERN_WARNING
  1722. "hda-intel: Invalid position buffer, "
  1723. "using LPIB read method instead.\n");
  1724. chip->position_fix[stream] = POS_FIX_LPIB;
  1725. pos = azx_get_position(chip, azx_dev);
  1726. } else
  1727. chip->position_fix[stream] = POS_FIX_POSBUF;
  1728. }
  1729. if (WARN_ONCE(!azx_dev->period_bytes,
  1730. "hda-intel: zero azx_dev->period_bytes"))
  1731. return -1; /* this shouldn't happen! */
  1732. if (wallclk < (azx_dev->period_wallclk * 5) / 4 &&
  1733. pos % azx_dev->period_bytes > azx_dev->period_bytes / 2)
  1734. /* NG - it's below the first next period boundary */
  1735. return bdl_pos_adj[chip->dev_index] ? 0 : -1;
  1736. azx_dev->start_wallclk += wallclk;
  1737. return 1; /* OK, it's fine */
  1738. }
  1739. /*
  1740. * The work for pending PCM period updates.
  1741. */
  1742. static void azx_irq_pending_work(struct work_struct *work)
  1743. {
  1744. struct azx *chip = container_of(work, struct azx, irq_pending_work);
  1745. int i, pending, ok;
  1746. if (!chip->irq_pending_warned) {
  1747. printk(KERN_WARNING
  1748. "hda-intel: IRQ timing workaround is activated "
  1749. "for card #%d. Suggest a bigger bdl_pos_adj.\n",
  1750. chip->card->number);
  1751. chip->irq_pending_warned = 1;
  1752. }
  1753. for (;;) {
  1754. pending = 0;
  1755. spin_lock_irq(&chip->reg_lock);
  1756. for (i = 0; i < chip->num_streams; i++) {
  1757. struct azx_dev *azx_dev = &chip->azx_dev[i];
  1758. if (!azx_dev->irq_pending ||
  1759. !azx_dev->substream ||
  1760. !azx_dev->running)
  1761. continue;
  1762. ok = azx_position_ok(chip, azx_dev);
  1763. if (ok > 0) {
  1764. azx_dev->irq_pending = 0;
  1765. spin_unlock(&chip->reg_lock);
  1766. snd_pcm_period_elapsed(azx_dev->substream);
  1767. spin_lock(&chip->reg_lock);
  1768. } else if (ok < 0) {
  1769. pending = 0; /* too early */
  1770. } else
  1771. pending++;
  1772. }
  1773. spin_unlock_irq(&chip->reg_lock);
  1774. if (!pending)
  1775. return;
  1776. msleep(1);
  1777. }
  1778. }
  1779. /* clear irq_pending flags and assure no on-going workq */
  1780. static void azx_clear_irq_pending(struct azx *chip)
  1781. {
  1782. int i;
  1783. spin_lock_irq(&chip->reg_lock);
  1784. for (i = 0; i < chip->num_streams; i++)
  1785. chip->azx_dev[i].irq_pending = 0;
  1786. spin_unlock_irq(&chip->reg_lock);
  1787. }
  1788. static struct snd_pcm_ops azx_pcm_ops = {
  1789. .open = azx_pcm_open,
  1790. .close = azx_pcm_close,
  1791. .ioctl = snd_pcm_lib_ioctl,
  1792. .hw_params = azx_pcm_hw_params,
  1793. .hw_free = azx_pcm_hw_free,
  1794. .prepare = azx_pcm_prepare,
  1795. .trigger = azx_pcm_trigger,
  1796. .pointer = azx_pcm_pointer,
  1797. .page = snd_pcm_sgbuf_ops_page,
  1798. };
  1799. static void azx_pcm_free(struct snd_pcm *pcm)
  1800. {
  1801. struct azx_pcm *apcm = pcm->private_data;
  1802. if (apcm) {
  1803. apcm->chip->pcm[pcm->device] = NULL;
  1804. kfree(apcm);
  1805. }
  1806. }
  1807. static int
  1808. azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
  1809. struct hda_pcm *cpcm)
  1810. {
  1811. struct azx *chip = bus->private_data;
  1812. struct snd_pcm *pcm;
  1813. struct azx_pcm *apcm;
  1814. int pcm_dev = cpcm->device;
  1815. int s, err;
  1816. if (pcm_dev >= HDA_MAX_PCMS) {
  1817. snd_printk(KERN_ERR SFX "Invalid PCM device number %d\n",
  1818. pcm_dev);
  1819. return -EINVAL;
  1820. }
  1821. if (chip->pcm[pcm_dev]) {
  1822. snd_printk(KERN_ERR SFX "PCM %d already exists\n", pcm_dev);
  1823. return -EBUSY;
  1824. }
  1825. err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
  1826. cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
  1827. cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
  1828. &pcm);
  1829. if (err < 0)
  1830. return err;
  1831. strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
  1832. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  1833. if (apcm == NULL)
  1834. return -ENOMEM;
  1835. apcm->chip = chip;
  1836. apcm->codec = codec;
  1837. pcm->private_data = apcm;
  1838. pcm->private_free = azx_pcm_free;
  1839. if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
  1840. pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
  1841. chip->pcm[pcm_dev] = pcm;
  1842. cpcm->pcm = pcm;
  1843. for (s = 0; s < 2; s++) {
  1844. apcm->hinfo[s] = &cpcm->stream[s];
  1845. if (cpcm->stream[s].substreams)
  1846. snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
  1847. }
  1848. /* buffer pre-allocation */
  1849. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1850. snd_dma_pci_data(chip->pci),
  1851. 1024 * 64, 32 * 1024 * 1024);
  1852. return 0;
  1853. }
  1854. /*
  1855. * mixer creation - all stuff is implemented in hda module
  1856. */
  1857. static int __devinit azx_mixer_create(struct azx *chip)
  1858. {
  1859. return snd_hda_build_controls(chip->bus);
  1860. }
  1861. /*
  1862. * initialize SD streams
  1863. */
  1864. static int __devinit azx_init_stream(struct azx *chip)
  1865. {
  1866. int i;
  1867. /* initialize each stream (aka device)
  1868. * assign the starting bdl address to each stream (device)
  1869. * and initialize
  1870. */
  1871. for (i = 0; i < chip->num_streams; i++) {
  1872. struct azx_dev *azx_dev = &chip->azx_dev[i];
  1873. azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
  1874. /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  1875. azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
  1876. /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
  1877. azx_dev->sd_int_sta_mask = 1 << i;
  1878. /* stream tag: must be non-zero and unique */
  1879. azx_dev->index = i;
  1880. azx_dev->stream_tag = i + 1;
  1881. }
  1882. return 0;
  1883. }
  1884. static int azx_acquire_irq(struct azx *chip, int do_disconnect)
  1885. {
  1886. if (request_irq(chip->pci->irq, azx_interrupt,
  1887. chip->msi ? 0 : IRQF_SHARED,
  1888. "hda_intel", chip)) {
  1889. printk(KERN_ERR "hda-intel: unable to grab IRQ %d, "
  1890. "disabling device\n", chip->pci->irq);
  1891. if (do_disconnect)
  1892. snd_card_disconnect(chip->card);
  1893. return -1;
  1894. }
  1895. chip->irq = chip->pci->irq;
  1896. pci_intx(chip->pci, !chip->msi);
  1897. return 0;
  1898. }
  1899. static void azx_stop_chip(struct azx *chip)
  1900. {
  1901. if (!chip->initialized)
  1902. return;
  1903. /* disable interrupts */
  1904. azx_int_disable(chip);
  1905. azx_int_clear(chip);
  1906. /* disable CORB/RIRB */
  1907. azx_free_cmd_io(chip);
  1908. /* disable position buffer */
  1909. azx_writel(chip, DPLBASE, 0);
  1910. azx_writel(chip, DPUBASE, 0);
  1911. chip->initialized = 0;
  1912. }
  1913. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1914. /* power-up/down the controller */
  1915. static void azx_power_notify(struct hda_bus *bus)
  1916. {
  1917. struct azx *chip = bus->private_data;
  1918. struct hda_codec *c;
  1919. int power_on = 0;
  1920. list_for_each_entry(c, &bus->codec_list, list) {
  1921. if (c->power_on) {
  1922. power_on = 1;
  1923. break;
  1924. }
  1925. }
  1926. if (power_on)
  1927. azx_init_chip(chip, 1);
  1928. else if (chip->running && power_save_controller &&
  1929. !bus->power_keep_link_on)
  1930. azx_stop_chip(chip);
  1931. }
  1932. #endif /* CONFIG_SND_HDA_POWER_SAVE */
  1933. #ifdef CONFIG_PM
  1934. /*
  1935. * power management
  1936. */
  1937. static int snd_hda_codecs_inuse(struct hda_bus *bus)
  1938. {
  1939. struct hda_codec *codec;
  1940. list_for_each_entry(codec, &bus->codec_list, list) {
  1941. if (snd_hda_codec_needs_resume(codec))
  1942. return 1;
  1943. }
  1944. return 0;
  1945. }
  1946. static int azx_suspend(struct pci_dev *pci, pm_message_t state)
  1947. {
  1948. struct snd_card *card = pci_get_drvdata(pci);
  1949. struct azx *chip = card->private_data;
  1950. int i;
  1951. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1952. azx_clear_irq_pending(chip);
  1953. for (i = 0; i < HDA_MAX_PCMS; i++)
  1954. snd_pcm_suspend_all(chip->pcm[i]);
  1955. if (chip->initialized)
  1956. snd_hda_suspend(chip->bus);
  1957. azx_stop_chip(chip);
  1958. if (chip->irq >= 0) {
  1959. free_irq(chip->irq, chip);
  1960. chip->irq = -1;
  1961. }
  1962. if (chip->msi)
  1963. pci_disable_msi(chip->pci);
  1964. pci_disable_device(pci);
  1965. pci_save_state(pci);
  1966. pci_set_power_state(pci, pci_choose_state(pci, state));
  1967. return 0;
  1968. }
  1969. static int azx_resume(struct pci_dev *pci)
  1970. {
  1971. struct snd_card *card = pci_get_drvdata(pci);
  1972. struct azx *chip = card->private_data;
  1973. pci_set_power_state(pci, PCI_D0);
  1974. pci_restore_state(pci);
  1975. if (pci_enable_device(pci) < 0) {
  1976. printk(KERN_ERR "hda-intel: pci_enable_device failed, "
  1977. "disabling device\n");
  1978. snd_card_disconnect(card);
  1979. return -EIO;
  1980. }
  1981. pci_set_master(pci);
  1982. if (chip->msi)
  1983. if (pci_enable_msi(pci) < 0)
  1984. chip->msi = 0;
  1985. if (azx_acquire_irq(chip, 1) < 0)
  1986. return -EIO;
  1987. azx_init_pci(chip);
  1988. if (snd_hda_codecs_inuse(chip->bus))
  1989. azx_init_chip(chip, 1);
  1990. snd_hda_resume(chip->bus);
  1991. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1992. return 0;
  1993. }
  1994. #endif /* CONFIG_PM */
  1995. /*
  1996. * reboot notifier for hang-up problem at power-down
  1997. */
  1998. static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
  1999. {
  2000. struct azx *chip = container_of(nb, struct azx, reboot_notifier);
  2001. snd_hda_bus_reboot_notify(chip->bus);
  2002. azx_stop_chip(chip);
  2003. return NOTIFY_OK;
  2004. }
  2005. static void azx_notifier_register(struct azx *chip)
  2006. {
  2007. chip->reboot_notifier.notifier_call = azx_halt;
  2008. register_reboot_notifier(&chip->reboot_notifier);
  2009. }
  2010. static void azx_notifier_unregister(struct azx *chip)
  2011. {
  2012. if (chip->reboot_notifier.notifier_call)
  2013. unregister_reboot_notifier(&chip->reboot_notifier);
  2014. }
  2015. /*
  2016. * destructor
  2017. */
  2018. static int azx_free(struct azx *chip)
  2019. {
  2020. int i;
  2021. azx_notifier_unregister(chip);
  2022. if (chip->initialized) {
  2023. azx_clear_irq_pending(chip);
  2024. for (i = 0; i < chip->num_streams; i++)
  2025. azx_stream_stop(chip, &chip->azx_dev[i]);
  2026. azx_stop_chip(chip);
  2027. }
  2028. if (chip->irq >= 0)
  2029. free_irq(chip->irq, (void*)chip);
  2030. if (chip->msi)
  2031. pci_disable_msi(chip->pci);
  2032. if (chip->remap_addr)
  2033. iounmap(chip->remap_addr);
  2034. if (chip->azx_dev) {
  2035. for (i = 0; i < chip->num_streams; i++)
  2036. if (chip->azx_dev[i].bdl.area)
  2037. snd_dma_free_pages(&chip->azx_dev[i].bdl);
  2038. }
  2039. if (chip->rb.area)
  2040. snd_dma_free_pages(&chip->rb);
  2041. if (chip->posbuf.area)
  2042. snd_dma_free_pages(&chip->posbuf);
  2043. pci_release_regions(chip->pci);
  2044. pci_disable_device(chip->pci);
  2045. kfree(chip->azx_dev);
  2046. kfree(chip);
  2047. return 0;
  2048. }
  2049. static int azx_dev_free(struct snd_device *device)
  2050. {
  2051. return azx_free(device->device_data);
  2052. }
  2053. /*
  2054. * white/black-listing for position_fix
  2055. */
  2056. static struct snd_pci_quirk position_fix_list[] __devinitdata = {
  2057. SND_PCI_QUIRK(0x1025, 0x009f, "Acer Aspire 5110", POS_FIX_LPIB),
  2058. SND_PCI_QUIRK(0x1025, 0x026f, "Acer Aspire 5538", POS_FIX_LPIB),
  2059. SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
  2060. SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
  2061. SND_PCI_QUIRK(0x1028, 0x01f6, "Dell Latitude 131L", POS_FIX_LPIB),
  2062. SND_PCI_QUIRK(0x1028, 0x0470, "Dell Inspiron 1120", POS_FIX_LPIB),
  2063. SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
  2064. SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
  2065. SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS", POS_FIX_LPIB),
  2066. SND_PCI_QUIRK(0x1043, 0x81e7, "ASUS M2V", POS_FIX_LPIB),
  2067. SND_PCI_QUIRK(0x1043, 0x8410, "ASUS", POS_FIX_LPIB),
  2068. SND_PCI_QUIRK(0x104d, 0x9069, "Sony VPCS11V9E", POS_FIX_LPIB),
  2069. SND_PCI_QUIRK(0x1106, 0x3288, "ASUS M2V-MX SE", POS_FIX_LPIB),
  2070. SND_PCI_QUIRK(0x1179, 0xff10, "Toshiba A100-259", POS_FIX_LPIB),
  2071. SND_PCI_QUIRK(0x1297, 0x3166, "Shuttle", POS_FIX_LPIB),
  2072. SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
  2073. SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
  2074. SND_PCI_QUIRK(0x1565, 0x820f, "Biostar Microtech", POS_FIX_LPIB),
  2075. SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
  2076. SND_PCI_QUIRK(0x1849, 0x0888, "775Dual-VSTA", POS_FIX_LPIB),
  2077. SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
  2078. SND_PCI_QUIRK(0x8086, 0xd601, "eMachines T5212", POS_FIX_LPIB),
  2079. {}
  2080. };
  2081. static int __devinit check_position_fix(struct azx *chip, int fix)
  2082. {
  2083. const struct snd_pci_quirk *q;
  2084. switch (fix) {
  2085. case POS_FIX_LPIB:
  2086. case POS_FIX_POSBUF:
  2087. case POS_FIX_VIACOMBO:
  2088. return fix;
  2089. }
  2090. q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
  2091. if (q) {
  2092. printk(KERN_INFO
  2093. "hda_intel: position_fix set to %d "
  2094. "for device %04x:%04x\n",
  2095. q->value, q->subvendor, q->subdevice);
  2096. return q->value;
  2097. }
  2098. /* Check VIA/ATI HD Audio Controller exist */
  2099. if (chip->driver_caps & AZX_DCAPS_POSFIX_VIA) {
  2100. snd_printd(SFX "Using VIACOMBO position fix\n");
  2101. return POS_FIX_VIACOMBO;
  2102. }
  2103. if (chip->driver_caps & AZX_DCAPS_POSFIX_LPIB) {
  2104. snd_printd(SFX "Using LPIB position fix\n");
  2105. return POS_FIX_LPIB;
  2106. }
  2107. return POS_FIX_AUTO;
  2108. }
  2109. /*
  2110. * black-lists for probe_mask
  2111. */
  2112. static struct snd_pci_quirk probe_mask_list[] __devinitdata = {
  2113. /* Thinkpad often breaks the controller communication when accessing
  2114. * to the non-working (or non-existing) modem codec slot.
  2115. */
  2116. SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
  2117. SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
  2118. SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
  2119. /* broken BIOS */
  2120. SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
  2121. /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
  2122. SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
  2123. /* forced codec slots */
  2124. SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
  2125. SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
  2126. {}
  2127. };
  2128. #define AZX_FORCE_CODEC_MASK 0x100
  2129. static void __devinit check_probe_mask(struct azx *chip, int dev)
  2130. {
  2131. const struct snd_pci_quirk *q;
  2132. chip->codec_probe_mask = probe_mask[dev];
  2133. if (chip->codec_probe_mask == -1) {
  2134. q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
  2135. if (q) {
  2136. printk(KERN_INFO
  2137. "hda_intel: probe_mask set to 0x%x "
  2138. "for device %04x:%04x\n",
  2139. q->value, q->subvendor, q->subdevice);
  2140. chip->codec_probe_mask = q->value;
  2141. }
  2142. }
  2143. /* check forced option */
  2144. if (chip->codec_probe_mask != -1 &&
  2145. (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
  2146. chip->codec_mask = chip->codec_probe_mask & 0xff;
  2147. printk(KERN_INFO "hda_intel: codec_mask forced to 0x%x\n",
  2148. chip->codec_mask);
  2149. }
  2150. }
  2151. /*
  2152. * white/black-list for enable_msi
  2153. */
  2154. static struct snd_pci_quirk msi_black_list[] __devinitdata = {
  2155. SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
  2156. SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
  2157. SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
  2158. SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
  2159. SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
  2160. {}
  2161. };
  2162. static void __devinit check_msi(struct azx *chip)
  2163. {
  2164. const struct snd_pci_quirk *q;
  2165. if (enable_msi >= 0) {
  2166. chip->msi = !!enable_msi;
  2167. return;
  2168. }
  2169. chip->msi = 1; /* enable MSI as default */
  2170. q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
  2171. if (q) {
  2172. printk(KERN_INFO
  2173. "hda_intel: msi for device %04x:%04x set to %d\n",
  2174. q->subvendor, q->subdevice, q->value);
  2175. chip->msi = q->value;
  2176. return;
  2177. }
  2178. /* NVidia chipsets seem to cause troubles with MSI */
  2179. if (chip->driver_caps & AZX_DCAPS_NO_MSI) {
  2180. printk(KERN_INFO "hda_intel: Disabling MSI\n");
  2181. chip->msi = 0;
  2182. }
  2183. }
  2184. /*
  2185. * constructor
  2186. */
  2187. static int __devinit azx_create(struct snd_card *card, struct pci_dev *pci,
  2188. int dev, unsigned int driver_caps,
  2189. struct azx **rchip)
  2190. {
  2191. struct azx *chip;
  2192. int i, err;
  2193. unsigned short gcap;
  2194. static struct snd_device_ops ops = {
  2195. .dev_free = azx_dev_free,
  2196. };
  2197. *rchip = NULL;
  2198. err = pci_enable_device(pci);
  2199. if (err < 0)
  2200. return err;
  2201. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  2202. if (!chip) {
  2203. snd_printk(KERN_ERR SFX "cannot allocate chip\n");
  2204. pci_disable_device(pci);
  2205. return -ENOMEM;
  2206. }
  2207. spin_lock_init(&chip->reg_lock);
  2208. mutex_init(&chip->open_mutex);
  2209. chip->card = card;
  2210. chip->pci = pci;
  2211. chip->irq = -1;
  2212. chip->driver_caps = driver_caps;
  2213. chip->driver_type = driver_caps & 0xff;
  2214. check_msi(chip);
  2215. chip->dev_index = dev;
  2216. INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
  2217. chip->position_fix[0] = chip->position_fix[1] =
  2218. check_position_fix(chip, position_fix[dev]);
  2219. check_probe_mask(chip, dev);
  2220. chip->single_cmd = single_cmd;
  2221. if (bdl_pos_adj[dev] < 0) {
  2222. switch (chip->driver_type) {
  2223. case AZX_DRIVER_ICH:
  2224. case AZX_DRIVER_PCH:
  2225. bdl_pos_adj[dev] = 1;
  2226. break;
  2227. default:
  2228. bdl_pos_adj[dev] = 32;
  2229. break;
  2230. }
  2231. }
  2232. #if BITS_PER_LONG != 64
  2233. /* Fix up base address on ULI M5461 */
  2234. if (chip->driver_type == AZX_DRIVER_ULI) {
  2235. u16 tmp3;
  2236. pci_read_config_word(pci, 0x40, &tmp3);
  2237. pci_write_config_word(pci, 0x40, tmp3 | 0x10);
  2238. pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
  2239. }
  2240. #endif
  2241. err = pci_request_regions(pci, "ICH HD audio");
  2242. if (err < 0) {
  2243. kfree(chip);
  2244. pci_disable_device(pci);
  2245. return err;
  2246. }
  2247. chip->addr = pci_resource_start(pci, 0);
  2248. chip->remap_addr = pci_ioremap_bar(pci, 0);
  2249. if (chip->remap_addr == NULL) {
  2250. snd_printk(KERN_ERR SFX "ioremap error\n");
  2251. err = -ENXIO;
  2252. goto errout;
  2253. }
  2254. if (chip->msi)
  2255. if (pci_enable_msi(pci) < 0)
  2256. chip->msi = 0;
  2257. if (azx_acquire_irq(chip, 0) < 0) {
  2258. err = -EBUSY;
  2259. goto errout;
  2260. }
  2261. pci_set_master(pci);
  2262. synchronize_irq(chip->irq);
  2263. gcap = azx_readw(chip, GCAP);
  2264. snd_printdd(SFX "chipset global capabilities = 0x%x\n", gcap);
  2265. /* disable SB600 64bit support for safety */
  2266. if (chip->pci->vendor == PCI_VENDOR_ID_ATI) {
  2267. struct pci_dev *p_smbus;
  2268. p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
  2269. PCI_DEVICE_ID_ATI_SBX00_SMBUS,
  2270. NULL);
  2271. if (p_smbus) {
  2272. if (p_smbus->revision < 0x30)
  2273. gcap &= ~ICH6_GCAP_64OK;
  2274. pci_dev_put(p_smbus);
  2275. }
  2276. }
  2277. /* disable 64bit DMA address on some devices */
  2278. if (chip->driver_caps & AZX_DCAPS_NO_64BIT) {
  2279. snd_printd(SFX "Disabling 64bit DMA\n");
  2280. gcap &= ~ICH6_GCAP_64OK;
  2281. }
  2282. /* allow 64bit DMA address if supported by H/W */
  2283. if ((gcap & ICH6_GCAP_64OK) && !pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
  2284. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
  2285. else {
  2286. pci_set_dma_mask(pci, DMA_BIT_MASK(32));
  2287. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
  2288. }
  2289. /* read number of streams from GCAP register instead of using
  2290. * hardcoded value
  2291. */
  2292. chip->capture_streams = (gcap >> 8) & 0x0f;
  2293. chip->playback_streams = (gcap >> 12) & 0x0f;
  2294. if (!chip->playback_streams && !chip->capture_streams) {
  2295. /* gcap didn't give any info, switching to old method */
  2296. switch (chip->driver_type) {
  2297. case AZX_DRIVER_ULI:
  2298. chip->playback_streams = ULI_NUM_PLAYBACK;
  2299. chip->capture_streams = ULI_NUM_CAPTURE;
  2300. break;
  2301. case AZX_DRIVER_ATIHDMI:
  2302. chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
  2303. chip->capture_streams = ATIHDMI_NUM_CAPTURE;
  2304. break;
  2305. case AZX_DRIVER_GENERIC:
  2306. default:
  2307. chip->playback_streams = ICH6_NUM_PLAYBACK;
  2308. chip->capture_streams = ICH6_NUM_CAPTURE;
  2309. break;
  2310. }
  2311. }
  2312. chip->capture_index_offset = 0;
  2313. chip->playback_index_offset = chip->capture_streams;
  2314. chip->num_streams = chip->playback_streams + chip->capture_streams;
  2315. chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
  2316. GFP_KERNEL);
  2317. if (!chip->azx_dev) {
  2318. snd_printk(KERN_ERR SFX "cannot malloc azx_dev\n");
  2319. goto errout;
  2320. }
  2321. for (i = 0; i < chip->num_streams; i++) {
  2322. /* allocate memory for the BDL for each stream */
  2323. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  2324. snd_dma_pci_data(chip->pci),
  2325. BDL_SIZE, &chip->azx_dev[i].bdl);
  2326. if (err < 0) {
  2327. snd_printk(KERN_ERR SFX "cannot allocate BDL\n");
  2328. goto errout;
  2329. }
  2330. }
  2331. /* allocate memory for the position buffer */
  2332. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  2333. snd_dma_pci_data(chip->pci),
  2334. chip->num_streams * 8, &chip->posbuf);
  2335. if (err < 0) {
  2336. snd_printk(KERN_ERR SFX "cannot allocate posbuf\n");
  2337. goto errout;
  2338. }
  2339. /* allocate CORB/RIRB */
  2340. err = azx_alloc_cmd_io(chip);
  2341. if (err < 0)
  2342. goto errout;
  2343. /* initialize streams */
  2344. azx_init_stream(chip);
  2345. /* initialize chip */
  2346. azx_init_pci(chip);
  2347. azx_init_chip(chip, (probe_only[dev] & 2) == 0);
  2348. /* codec detection */
  2349. if (!chip->codec_mask) {
  2350. snd_printk(KERN_ERR SFX "no codecs found!\n");
  2351. err = -ENODEV;
  2352. goto errout;
  2353. }
  2354. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  2355. if (err <0) {
  2356. snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
  2357. goto errout;
  2358. }
  2359. strcpy(card->driver, "HDA-Intel");
  2360. strlcpy(card->shortname, driver_short_names[chip->driver_type],
  2361. sizeof(card->shortname));
  2362. snprintf(card->longname, sizeof(card->longname),
  2363. "%s at 0x%lx irq %i",
  2364. card->shortname, chip->addr, chip->irq);
  2365. *rchip = chip;
  2366. return 0;
  2367. errout:
  2368. azx_free(chip);
  2369. return err;
  2370. }
  2371. static void power_down_all_codecs(struct azx *chip)
  2372. {
  2373. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2374. /* The codecs were powered up in snd_hda_codec_new().
  2375. * Now all initialization done, so turn them down if possible
  2376. */
  2377. struct hda_codec *codec;
  2378. list_for_each_entry(codec, &chip->bus->codec_list, list) {
  2379. snd_hda_power_down(codec);
  2380. }
  2381. #endif
  2382. }
  2383. static int __devinit azx_probe(struct pci_dev *pci,
  2384. const struct pci_device_id *pci_id)
  2385. {
  2386. static int dev;
  2387. struct snd_card *card;
  2388. struct azx *chip;
  2389. int err;
  2390. if (dev >= SNDRV_CARDS)
  2391. return -ENODEV;
  2392. if (!enable[dev]) {
  2393. dev++;
  2394. return -ENOENT;
  2395. }
  2396. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  2397. if (err < 0) {
  2398. snd_printk(KERN_ERR SFX "Error creating card!\n");
  2399. return err;
  2400. }
  2401. /* set this here since it's referred in snd_hda_load_patch() */
  2402. snd_card_set_dev(card, &pci->dev);
  2403. err = azx_create(card, pci, dev, pci_id->driver_data, &chip);
  2404. if (err < 0)
  2405. goto out_free;
  2406. card->private_data = chip;
  2407. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  2408. chip->beep_mode = beep_mode[dev];
  2409. #endif
  2410. /* create codec instances */
  2411. err = azx_codec_create(chip, model[dev]);
  2412. if (err < 0)
  2413. goto out_free;
  2414. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  2415. if (patch[dev] && *patch[dev]) {
  2416. snd_printk(KERN_ERR SFX "Applying patch firmware '%s'\n",
  2417. patch[dev]);
  2418. err = snd_hda_load_patch(chip->bus, patch[dev]);
  2419. if (err < 0)
  2420. goto out_free;
  2421. }
  2422. #endif
  2423. if ((probe_only[dev] & 1) == 0) {
  2424. err = azx_codec_configure(chip);
  2425. if (err < 0)
  2426. goto out_free;
  2427. }
  2428. /* create PCM streams */
  2429. err = snd_hda_build_pcms(chip->bus);
  2430. if (err < 0)
  2431. goto out_free;
  2432. /* create mixer controls */
  2433. err = azx_mixer_create(chip);
  2434. if (err < 0)
  2435. goto out_free;
  2436. err = snd_card_register(card);
  2437. if (err < 0)
  2438. goto out_free;
  2439. pci_set_drvdata(pci, card);
  2440. chip->running = 1;
  2441. power_down_all_codecs(chip);
  2442. azx_notifier_register(chip);
  2443. dev++;
  2444. return err;
  2445. out_free:
  2446. snd_card_free(card);
  2447. return err;
  2448. }
  2449. static void __devexit azx_remove(struct pci_dev *pci)
  2450. {
  2451. snd_card_free(pci_get_drvdata(pci));
  2452. pci_set_drvdata(pci, NULL);
  2453. }
  2454. /* PCI IDs */
  2455. static DEFINE_PCI_DEVICE_TABLE(azx_ids) = {
  2456. /* CPT */
  2457. { PCI_DEVICE(0x8086, 0x1c20),
  2458. .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_SCH_SNOOP },
  2459. /* PBG */
  2460. { PCI_DEVICE(0x8086, 0x1d20),
  2461. .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_SCH_SNOOP },
  2462. /* Panther Point */
  2463. { PCI_DEVICE(0x8086, 0x1e20),
  2464. .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_SCH_SNOOP },
  2465. /* SCH */
  2466. { PCI_DEVICE(0x8086, 0x811b),
  2467. .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_SCH_SNOOP },
  2468. /* Generic Intel */
  2469. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
  2470. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2471. .class_mask = 0xffffff,
  2472. .driver_data = AZX_DRIVER_ICH },
  2473. /* ATI SB 450/600/700/800/900 */
  2474. { PCI_DEVICE(0x1002, 0x437b),
  2475. .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
  2476. { PCI_DEVICE(0x1002, 0x4383),
  2477. .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
  2478. /* AMD Hudson */
  2479. { PCI_DEVICE(0x1022, 0x780d),
  2480. .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB },
  2481. /* ATI HDMI */
  2482. { PCI_DEVICE(0x1002, 0x793b),
  2483. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2484. { PCI_DEVICE(0x1002, 0x7919),
  2485. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2486. { PCI_DEVICE(0x1002, 0x960f),
  2487. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2488. { PCI_DEVICE(0x1002, 0x970f),
  2489. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2490. { PCI_DEVICE(0x1002, 0xaa00),
  2491. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2492. { PCI_DEVICE(0x1002, 0xaa08),
  2493. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2494. { PCI_DEVICE(0x1002, 0xaa10),
  2495. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2496. { PCI_DEVICE(0x1002, 0xaa18),
  2497. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2498. { PCI_DEVICE(0x1002, 0xaa20),
  2499. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2500. { PCI_DEVICE(0x1002, 0xaa28),
  2501. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2502. { PCI_DEVICE(0x1002, 0xaa30),
  2503. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2504. { PCI_DEVICE(0x1002, 0xaa38),
  2505. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2506. { PCI_DEVICE(0x1002, 0xaa40),
  2507. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2508. { PCI_DEVICE(0x1002, 0xaa48),
  2509. .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
  2510. /* VIA VT8251/VT8237A */
  2511. { PCI_DEVICE(0x1106, 0x3288),
  2512. .driver_data = AZX_DRIVER_VIA | AZX_DCAPS_POSFIX_VIA },
  2513. /* SIS966 */
  2514. { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
  2515. /* ULI M5461 */
  2516. { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
  2517. /* NVIDIA MCP */
  2518. { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
  2519. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2520. .class_mask = 0xffffff,
  2521. .driver_data = AZX_DRIVER_NVIDIA | AZX_DCAPS_PRESET_NVIDIA },
  2522. /* Teradici */
  2523. { PCI_DEVICE(0x6549, 0x1200),
  2524. .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
  2525. /* Creative X-Fi (CA0110-IBG) */
  2526. #if !defined(CONFIG_SND_CTXFI) && !defined(CONFIG_SND_CTXFI_MODULE)
  2527. /* the following entry conflicts with snd-ctxfi driver,
  2528. * as ctxfi driver mutates from HD-audio to native mode with
  2529. * a special command sequence.
  2530. */
  2531. { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
  2532. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2533. .class_mask = 0xffffff,
  2534. .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
  2535. AZX_DCAPS_RIRB_PRE_DELAY },
  2536. #else
  2537. /* this entry seems still valid -- i.e. without emu20kx chip */
  2538. { PCI_DEVICE(0x1102, 0x0009),
  2539. .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
  2540. AZX_DCAPS_RIRB_PRE_DELAY },
  2541. #endif
  2542. /* Vortex86MX */
  2543. { PCI_DEVICE(0x17f3, 0x3010), .driver_data = AZX_DRIVER_GENERIC },
  2544. /* VMware HDAudio */
  2545. { PCI_DEVICE(0x15ad, 0x1977), .driver_data = AZX_DRIVER_GENERIC },
  2546. /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
  2547. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
  2548. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2549. .class_mask = 0xffffff,
  2550. .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
  2551. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
  2552. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2553. .class_mask = 0xffffff,
  2554. .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
  2555. { 0, }
  2556. };
  2557. MODULE_DEVICE_TABLE(pci, azx_ids);
  2558. /* pci_driver definition */
  2559. static struct pci_driver driver = {
  2560. .name = "HDA Intel",
  2561. .id_table = azx_ids,
  2562. .probe = azx_probe,
  2563. .remove = __devexit_p(azx_remove),
  2564. #ifdef CONFIG_PM
  2565. .suspend = azx_suspend,
  2566. .resume = azx_resume,
  2567. #endif
  2568. };
  2569. static int __init alsa_card_azx_init(void)
  2570. {
  2571. return pci_register_driver(&driver);
  2572. }
  2573. static void __exit alsa_card_azx_exit(void)
  2574. {
  2575. pci_unregister_driver(&driver);
  2576. }
  2577. module_init(alsa_card_azx_init)
  2578. module_exit(alsa_card_azx_exit)