hda_intel.c 65 KB

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