hda_intel.c 67 KB

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