hda_intel.c 56 KB

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