hda_intel.c 55 KB

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