hda_intel.c 66 KB

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