hda_intel.c 65 KB

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