hda_intel.c 57 KB

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