hda_intel.c 70 KB

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