hda_intel.c 63 KB

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