hda_intel.c 64 KB

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