hda_intel.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888
  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 probe_only[SNDRV_CARDS];
  59. static int single_cmd;
  60. static int enable_msi = -1;
  61. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  62. static char *patch[SNDRV_CARDS];
  63. #endif
  64. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  65. static int beep_mode[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] =
  66. CONFIG_SND_HDA_INPUT_BEEP_MODE};
  67. #endif
  68. module_param_array(index, int, NULL, 0444);
  69. MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
  70. module_param_array(id, charp, NULL, 0444);
  71. MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
  72. module_param_array(enable, bool, NULL, 0444);
  73. MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
  74. module_param_array(model, charp, NULL, 0444);
  75. MODULE_PARM_DESC(model, "Use the given board model.");
  76. module_param_array(position_fix, int, NULL, 0444);
  77. MODULE_PARM_DESC(position_fix, "DMA pointer read method."
  78. "(0 = auto, 1 = LPIB, 2 = POSBUF, 3 = VIACOMBO).");
  79. module_param_array(bdl_pos_adj, int, NULL, 0644);
  80. MODULE_PARM_DESC(bdl_pos_adj, "BDL position adjustment offset.");
  81. module_param_array(probe_mask, int, NULL, 0444);
  82. MODULE_PARM_DESC(probe_mask, "Bitmask to probe codecs (default = -1).");
  83. module_param_array(probe_only, int, NULL, 0444);
  84. MODULE_PARM_DESC(probe_only, "Only probing and no codec initialization.");
  85. module_param(single_cmd, bool, 0444);
  86. MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
  87. "(for debugging only).");
  88. module_param(enable_msi, int, 0444);
  89. MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
  90. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  91. module_param_array(patch, charp, NULL, 0444);
  92. MODULE_PARM_DESC(patch, "Patch file for Intel HD audio interface.");
  93. #endif
  94. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  95. module_param_array(beep_mode, int, NULL, 0444);
  96. MODULE_PARM_DESC(beep_mode, "Select HDA Beep registration mode "
  97. "(0=off, 1=on, 2=mute switch on/off) (default=1).");
  98. #endif
  99. #ifdef CONFIG_SND_HDA_POWER_SAVE
  100. static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
  101. module_param(power_save, int, 0644);
  102. MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
  103. "(in second, 0 = disable).");
  104. /* reset the HD-audio controller in power save mode.
  105. * this may give more power-saving, but will take longer time to
  106. * wake up.
  107. */
  108. static int power_save_controller = 1;
  109. module_param(power_save_controller, bool, 0644);
  110. MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
  111. #endif
  112. MODULE_LICENSE("GPL");
  113. MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
  114. "{Intel, ICH6M},"
  115. "{Intel, ICH7},"
  116. "{Intel, ESB2},"
  117. "{Intel, ICH8},"
  118. "{Intel, ICH9},"
  119. "{Intel, ICH10},"
  120. "{Intel, PCH},"
  121. "{Intel, CPT},"
  122. "{Intel, PPT},"
  123. "{Intel, PBG},"
  124. "{Intel, SCH},"
  125. "{ATI, SB450},"
  126. "{ATI, SB600},"
  127. "{ATI, RS600},"
  128. "{ATI, RS690},"
  129. "{ATI, RS780},"
  130. "{ATI, R600},"
  131. "{ATI, RV630},"
  132. "{ATI, RV610},"
  133. "{ATI, RV670},"
  134. "{ATI, RV635},"
  135. "{ATI, RV620},"
  136. "{ATI, RV770},"
  137. "{VIA, VT8251},"
  138. "{VIA, VT8237A},"
  139. "{SiS, SIS966},"
  140. "{ULI, M5461}}");
  141. MODULE_DESCRIPTION("Intel HDA driver");
  142. #ifdef CONFIG_SND_VERBOSE_PRINTK
  143. #define SFX /* nop */
  144. #else
  145. #define SFX "hda-intel: "
  146. #endif
  147. /*
  148. * registers
  149. */
  150. #define ICH6_REG_GCAP 0x00
  151. #define ICH6_GCAP_64OK (1 << 0) /* 64bit address support */
  152. #define ICH6_GCAP_NSDO (3 << 1) /* # of serial data out signals */
  153. #define ICH6_GCAP_BSS (31 << 3) /* # of bidirectional streams */
  154. #define ICH6_GCAP_ISS (15 << 8) /* # of input streams */
  155. #define ICH6_GCAP_OSS (15 << 12) /* # of output streams */
  156. #define ICH6_REG_VMIN 0x02
  157. #define ICH6_REG_VMAJ 0x03
  158. #define ICH6_REG_OUTPAY 0x04
  159. #define ICH6_REG_INPAY 0x06
  160. #define ICH6_REG_GCTL 0x08
  161. #define ICH6_GCTL_RESET (1 << 0) /* controller reset */
  162. #define ICH6_GCTL_FCNTRL (1 << 1) /* flush control */
  163. #define ICH6_GCTL_UNSOL (1 << 8) /* accept unsol. response enable */
  164. #define ICH6_REG_WAKEEN 0x0c
  165. #define ICH6_REG_STATESTS 0x0e
  166. #define ICH6_REG_GSTS 0x10
  167. #define ICH6_GSTS_FSTS (1 << 1) /* flush status */
  168. #define ICH6_REG_INTCTL 0x20
  169. #define ICH6_REG_INTSTS 0x24
  170. #define ICH6_REG_WALLCLK 0x30 /* 24Mhz source */
  171. #define ICH6_REG_SYNC 0x34
  172. #define ICH6_REG_CORBLBASE 0x40
  173. #define ICH6_REG_CORBUBASE 0x44
  174. #define ICH6_REG_CORBWP 0x48
  175. #define ICH6_REG_CORBRP 0x4a
  176. #define ICH6_CORBRP_RST (1 << 15) /* read pointer reset */
  177. #define ICH6_REG_CORBCTL 0x4c
  178. #define ICH6_CORBCTL_RUN (1 << 1) /* enable DMA */
  179. #define ICH6_CORBCTL_CMEIE (1 << 0) /* enable memory error irq */
  180. #define ICH6_REG_CORBSTS 0x4d
  181. #define ICH6_CORBSTS_CMEI (1 << 0) /* memory error indication */
  182. #define ICH6_REG_CORBSIZE 0x4e
  183. #define ICH6_REG_RIRBLBASE 0x50
  184. #define ICH6_REG_RIRBUBASE 0x54
  185. #define ICH6_REG_RIRBWP 0x58
  186. #define ICH6_RIRBWP_RST (1 << 15) /* write pointer reset */
  187. #define ICH6_REG_RINTCNT 0x5a
  188. #define ICH6_REG_RIRBCTL 0x5c
  189. #define ICH6_RBCTL_IRQ_EN (1 << 0) /* enable IRQ */
  190. #define ICH6_RBCTL_DMA_EN (1 << 1) /* enable DMA */
  191. #define ICH6_RBCTL_OVERRUN_EN (1 << 2) /* enable overrun irq */
  192. #define ICH6_REG_RIRBSTS 0x5d
  193. #define ICH6_RBSTS_IRQ (1 << 0) /* response irq */
  194. #define ICH6_RBSTS_OVERRUN (1 << 2) /* overrun irq */
  195. #define ICH6_REG_RIRBSIZE 0x5e
  196. #define ICH6_REG_IC 0x60
  197. #define ICH6_REG_IR 0x64
  198. #define ICH6_REG_IRS 0x68
  199. #define ICH6_IRS_VALID (1<<1)
  200. #define ICH6_IRS_BUSY (1<<0)
  201. #define ICH6_REG_DPLBASE 0x70
  202. #define ICH6_REG_DPUBASE 0x74
  203. #define ICH6_DPLBASE_ENABLE 0x1 /* Enable position buffer */
  204. /* SD offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  205. enum { SDI0, SDI1, SDI2, SDI3, SDO0, SDO1, SDO2, SDO3 };
  206. /* stream register offsets from stream base */
  207. #define ICH6_REG_SD_CTL 0x00
  208. #define ICH6_REG_SD_STS 0x03
  209. #define ICH6_REG_SD_LPIB 0x04
  210. #define ICH6_REG_SD_CBL 0x08
  211. #define ICH6_REG_SD_LVI 0x0c
  212. #define ICH6_REG_SD_FIFOW 0x0e
  213. #define ICH6_REG_SD_FIFOSIZE 0x10
  214. #define ICH6_REG_SD_FORMAT 0x12
  215. #define ICH6_REG_SD_BDLPL 0x18
  216. #define ICH6_REG_SD_BDLPU 0x1c
  217. /* PCI space */
  218. #define ICH6_PCIREG_TCSEL 0x44
  219. /*
  220. * other constants
  221. */
  222. /* max number of SDs */
  223. /* ICH, ATI and VIA have 4 playback and 4 capture */
  224. #define ICH6_NUM_CAPTURE 4
  225. #define ICH6_NUM_PLAYBACK 4
  226. /* ULI has 6 playback and 5 capture */
  227. #define ULI_NUM_CAPTURE 5
  228. #define ULI_NUM_PLAYBACK 6
  229. /* ATI HDMI has 1 playback and 0 capture */
  230. #define ATIHDMI_NUM_CAPTURE 0
  231. #define ATIHDMI_NUM_PLAYBACK 1
  232. /* TERA has 4 playback and 3 capture */
  233. #define TERA_NUM_CAPTURE 3
  234. #define TERA_NUM_PLAYBACK 4
  235. /* this number is statically defined for simplicity */
  236. #define MAX_AZX_DEV 16
  237. /* max number of fragments - we may use more if allocating more pages for BDL */
  238. #define BDL_SIZE 4096
  239. #define AZX_MAX_BDL_ENTRIES (BDL_SIZE / 16)
  240. #define AZX_MAX_FRAG 32
  241. /* max buffer size - no h/w limit, you can increase as you like */
  242. #define AZX_MAX_BUF_SIZE (1024*1024*1024)
  243. /* RIRB int mask: overrun[2], response[0] */
  244. #define RIRB_INT_RESPONSE 0x01
  245. #define RIRB_INT_OVERRUN 0x04
  246. #define RIRB_INT_MASK 0x05
  247. /* STATESTS int mask: S3,SD2,SD1,SD0 */
  248. #define AZX_MAX_CODECS 8
  249. #define AZX_DEFAULT_CODECS 4
  250. #define STATESTS_INT_MASK ((1 << AZX_MAX_CODECS) - 1)
  251. /* SD_CTL bits */
  252. #define SD_CTL_STREAM_RESET 0x01 /* stream reset bit */
  253. #define SD_CTL_DMA_START 0x02 /* stream DMA start bit */
  254. #define SD_CTL_STRIPE (3 << 16) /* stripe control */
  255. #define SD_CTL_TRAFFIC_PRIO (1 << 18) /* traffic priority */
  256. #define SD_CTL_DIR (1 << 19) /* bi-directional stream */
  257. #define SD_CTL_STREAM_TAG_MASK (0xf << 20)
  258. #define SD_CTL_STREAM_TAG_SHIFT 20
  259. /* SD_CTL and SD_STS */
  260. #define SD_INT_DESC_ERR 0x10 /* descriptor error interrupt */
  261. #define SD_INT_FIFO_ERR 0x08 /* FIFO error interrupt */
  262. #define SD_INT_COMPLETE 0x04 /* completion interrupt */
  263. #define SD_INT_MASK (SD_INT_DESC_ERR|SD_INT_FIFO_ERR|\
  264. SD_INT_COMPLETE)
  265. /* SD_STS */
  266. #define SD_STS_FIFO_READY 0x20 /* FIFO ready */
  267. /* INTCTL and INTSTS */
  268. #define ICH6_INT_ALL_STREAM 0xff /* all stream interrupts */
  269. #define ICH6_INT_CTRL_EN 0x40000000 /* controller interrupt enable bit */
  270. #define ICH6_INT_GLOBAL_EN 0x80000000 /* global interrupt enable bit */
  271. /* below are so far hardcoded - should read registers in future */
  272. #define ICH6_MAX_CORB_ENTRIES 256
  273. #define ICH6_MAX_RIRB_ENTRIES 256
  274. /* position fix mode */
  275. enum {
  276. POS_FIX_AUTO,
  277. POS_FIX_LPIB,
  278. POS_FIX_POSBUF,
  279. POS_FIX_VIACOMBO,
  280. };
  281. /* Defines for ATI HD Audio support in SB450 south bridge */
  282. #define ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR 0x42
  283. #define ATI_SB450_HDAUDIO_ENABLE_SNOOP 0x02
  284. /* Defines for Nvidia HDA support */
  285. #define NVIDIA_HDA_TRANSREG_ADDR 0x4e
  286. #define NVIDIA_HDA_ENABLE_COHBITS 0x0f
  287. #define NVIDIA_HDA_ISTRM_COH 0x4d
  288. #define NVIDIA_HDA_OSTRM_COH 0x4c
  289. #define NVIDIA_HDA_ENABLE_COHBIT 0x01
  290. /* Defines for Intel SCH HDA snoop control */
  291. #define INTEL_SCH_HDA_DEVC 0x78
  292. #define INTEL_SCH_HDA_DEVC_NOSNOOP (0x1<<11)
  293. /* Define IN stream 0 FIFO size offset in VIA controller */
  294. #define VIA_IN_STREAM0_FIFO_SIZE_OFFSET 0x90
  295. /* Define VIA HD Audio Device ID*/
  296. #define VIA_HDAC_DEVICE_ID 0x3288
  297. /* HD Audio class code */
  298. #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
  299. /*
  300. */
  301. struct azx_dev {
  302. struct snd_dma_buffer bdl; /* BDL buffer */
  303. u32 *posbuf; /* position buffer pointer */
  304. unsigned int bufsize; /* size of the play buffer in bytes */
  305. unsigned int period_bytes; /* size of the period in bytes */
  306. unsigned int frags; /* number for period in the play buffer */
  307. unsigned int fifo_size; /* FIFO size */
  308. unsigned long start_wallclk; /* start + minimum wallclk */
  309. unsigned long period_wallclk; /* wallclk for period */
  310. void __iomem *sd_addr; /* stream descriptor pointer */
  311. u32 sd_int_sta_mask; /* stream int status mask */
  312. /* pcm support */
  313. struct snd_pcm_substream *substream; /* assigned substream,
  314. * set in PCM open
  315. */
  316. unsigned int format_val; /* format value to be set in the
  317. * controller and the codec
  318. */
  319. unsigned char stream_tag; /* assigned stream */
  320. unsigned char index; /* stream index */
  321. int device; /* last device number assigned to */
  322. unsigned int opened :1;
  323. unsigned int running :1;
  324. unsigned int irq_pending :1;
  325. /*
  326. * For VIA:
  327. * A flag to ensure DMA position is 0
  328. * when link position is not greater than FIFO size
  329. */
  330. unsigned int insufficient :1;
  331. };
  332. /* CORB/RIRB */
  333. struct azx_rb {
  334. u32 *buf; /* CORB/RIRB buffer
  335. * Each CORB entry is 4byte, RIRB is 8byte
  336. */
  337. dma_addr_t addr; /* physical address of CORB/RIRB buffer */
  338. /* for RIRB */
  339. unsigned short rp, wp; /* read/write pointers */
  340. int cmds[AZX_MAX_CODECS]; /* number of pending requests */
  341. u32 res[AZX_MAX_CODECS]; /* last read value */
  342. };
  343. struct azx {
  344. struct snd_card *card;
  345. struct pci_dev *pci;
  346. int dev_index;
  347. /* chip type specific */
  348. int driver_type;
  349. int playback_streams;
  350. int playback_index_offset;
  351. int capture_streams;
  352. int capture_index_offset;
  353. int num_streams;
  354. /* pci resources */
  355. unsigned long addr;
  356. void __iomem *remap_addr;
  357. int irq;
  358. /* locks */
  359. spinlock_t reg_lock;
  360. struct mutex open_mutex;
  361. /* streams (x num_streams) */
  362. struct azx_dev *azx_dev;
  363. /* PCM */
  364. struct snd_pcm *pcm[HDA_MAX_PCMS];
  365. /* HD codec */
  366. unsigned short codec_mask;
  367. int codec_probe_mask; /* copied from probe_mask option */
  368. struct hda_bus *bus;
  369. unsigned int beep_mode;
  370. /* CORB/RIRB */
  371. struct azx_rb corb;
  372. struct azx_rb rirb;
  373. /* CORB/RIRB and position buffers */
  374. struct snd_dma_buffer rb;
  375. struct snd_dma_buffer posbuf;
  376. /* flags */
  377. int position_fix[2]; /* for both playback/capture streams */
  378. int poll_count;
  379. unsigned int running :1;
  380. unsigned int initialized :1;
  381. unsigned int single_cmd :1;
  382. unsigned int polling_mode :1;
  383. unsigned int msi :1;
  384. unsigned int irq_pending_warned :1;
  385. unsigned int probing :1; /* codec probing phase */
  386. /* for debugging */
  387. unsigned int last_cmd[AZX_MAX_CODECS];
  388. /* for pending irqs */
  389. struct work_struct irq_pending_work;
  390. /* reboot notifier (for mysterious hangup problem at power-down) */
  391. struct notifier_block reboot_notifier;
  392. };
  393. /* driver types */
  394. enum {
  395. AZX_DRIVER_ICH,
  396. AZX_DRIVER_PCH,
  397. AZX_DRIVER_SCH,
  398. AZX_DRIVER_ATI,
  399. AZX_DRIVER_ATIHDMI,
  400. AZX_DRIVER_VIA,
  401. AZX_DRIVER_SIS,
  402. AZX_DRIVER_ULI,
  403. AZX_DRIVER_NVIDIA,
  404. AZX_DRIVER_TERA,
  405. AZX_DRIVER_CTX,
  406. AZX_DRIVER_GENERIC,
  407. AZX_NUM_DRIVERS, /* keep this as last entry */
  408. };
  409. static char *driver_short_names[] __devinitdata = {
  410. [AZX_DRIVER_ICH] = "HDA Intel",
  411. [AZX_DRIVER_PCH] = "HDA Intel PCH",
  412. [AZX_DRIVER_SCH] = "HDA Intel MID",
  413. [AZX_DRIVER_ATI] = "HDA ATI SB",
  414. [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
  415. [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
  416. [AZX_DRIVER_SIS] = "HDA SIS966",
  417. [AZX_DRIVER_ULI] = "HDA ULI M5461",
  418. [AZX_DRIVER_NVIDIA] = "HDA NVidia",
  419. [AZX_DRIVER_TERA] = "HDA Teradici",
  420. [AZX_DRIVER_CTX] = "HDA Creative",
  421. [AZX_DRIVER_GENERIC] = "HD-Audio Generic",
  422. };
  423. /*
  424. * macros for easy use
  425. */
  426. #define azx_writel(chip,reg,value) \
  427. writel(value, (chip)->remap_addr + ICH6_REG_##reg)
  428. #define azx_readl(chip,reg) \
  429. readl((chip)->remap_addr + ICH6_REG_##reg)
  430. #define azx_writew(chip,reg,value) \
  431. writew(value, (chip)->remap_addr + ICH6_REG_##reg)
  432. #define azx_readw(chip,reg) \
  433. readw((chip)->remap_addr + ICH6_REG_##reg)
  434. #define azx_writeb(chip,reg,value) \
  435. writeb(value, (chip)->remap_addr + ICH6_REG_##reg)
  436. #define azx_readb(chip,reg) \
  437. readb((chip)->remap_addr + ICH6_REG_##reg)
  438. #define azx_sd_writel(dev,reg,value) \
  439. writel(value, (dev)->sd_addr + ICH6_REG_##reg)
  440. #define azx_sd_readl(dev,reg) \
  441. readl((dev)->sd_addr + ICH6_REG_##reg)
  442. #define azx_sd_writew(dev,reg,value) \
  443. writew(value, (dev)->sd_addr + ICH6_REG_##reg)
  444. #define azx_sd_readw(dev,reg) \
  445. readw((dev)->sd_addr + ICH6_REG_##reg)
  446. #define azx_sd_writeb(dev,reg,value) \
  447. writeb(value, (dev)->sd_addr + ICH6_REG_##reg)
  448. #define azx_sd_readb(dev,reg) \
  449. readb((dev)->sd_addr + ICH6_REG_##reg)
  450. /* for pcm support */
  451. #define get_azx_dev(substream) (substream->runtime->private_data)
  452. static int azx_acquire_irq(struct azx *chip, int do_disconnect);
  453. static int azx_send_cmd(struct hda_bus *bus, unsigned int val);
  454. /*
  455. * Interface for HD codec
  456. */
  457. /*
  458. * CORB / RIRB interface
  459. */
  460. static int azx_alloc_cmd_io(struct azx *chip)
  461. {
  462. int err;
  463. /* single page (at least 4096 bytes) must suffice for both ringbuffes */
  464. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  465. snd_dma_pci_data(chip->pci),
  466. PAGE_SIZE, &chip->rb);
  467. if (err < 0) {
  468. snd_printk(KERN_ERR SFX "cannot allocate CORB/RIRB\n");
  469. return err;
  470. }
  471. return 0;
  472. }
  473. static void azx_init_cmd_io(struct azx *chip)
  474. {
  475. spin_lock_irq(&chip->reg_lock);
  476. /* CORB set up */
  477. chip->corb.addr = chip->rb.addr;
  478. chip->corb.buf = (u32 *)chip->rb.area;
  479. azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
  480. azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
  481. /* set the corb size to 256 entries (ULI requires explicitly) */
  482. azx_writeb(chip, CORBSIZE, 0x02);
  483. /* set the corb write pointer to 0 */
  484. azx_writew(chip, CORBWP, 0);
  485. /* reset the corb hw read pointer */
  486. azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
  487. /* enable corb dma */
  488. azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
  489. /* RIRB set up */
  490. chip->rirb.addr = chip->rb.addr + 2048;
  491. chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
  492. chip->rirb.wp = chip->rirb.rp = 0;
  493. memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
  494. azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
  495. azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
  496. /* set the rirb size to 256 entries (ULI requires explicitly) */
  497. azx_writeb(chip, RIRBSIZE, 0x02);
  498. /* reset the rirb hw write pointer */
  499. azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
  500. /* set N=1, get RIRB response interrupt for new entry */
  501. if (chip->driver_type == AZX_DRIVER_CTX)
  502. azx_writew(chip, RINTCNT, 0xc0);
  503. else
  504. azx_writew(chip, RINTCNT, 1);
  505. /* enable rirb dma and response irq */
  506. azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
  507. spin_unlock_irq(&chip->reg_lock);
  508. }
  509. static void azx_free_cmd_io(struct azx *chip)
  510. {
  511. spin_lock_irq(&chip->reg_lock);
  512. /* disable ringbuffer DMAs */
  513. azx_writeb(chip, RIRBCTL, 0);
  514. azx_writeb(chip, CORBCTL, 0);
  515. spin_unlock_irq(&chip->reg_lock);
  516. }
  517. static unsigned int azx_command_addr(u32 cmd)
  518. {
  519. unsigned int addr = cmd >> 28;
  520. if (addr >= AZX_MAX_CODECS) {
  521. snd_BUG();
  522. addr = 0;
  523. }
  524. return addr;
  525. }
  526. static unsigned int azx_response_addr(u32 res)
  527. {
  528. unsigned int addr = res & 0xf;
  529. if (addr >= AZX_MAX_CODECS) {
  530. snd_BUG();
  531. addr = 0;
  532. }
  533. return addr;
  534. }
  535. /* send a command */
  536. static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
  537. {
  538. struct azx *chip = bus->private_data;
  539. unsigned int addr = azx_command_addr(val);
  540. unsigned int wp;
  541. spin_lock_irq(&chip->reg_lock);
  542. /* add command to corb */
  543. wp = azx_readb(chip, CORBWP);
  544. wp++;
  545. wp %= ICH6_MAX_CORB_ENTRIES;
  546. chip->rirb.cmds[addr]++;
  547. chip->corb.buf[wp] = cpu_to_le32(val);
  548. azx_writel(chip, CORBWP, wp);
  549. spin_unlock_irq(&chip->reg_lock);
  550. return 0;
  551. }
  552. #define ICH6_RIRB_EX_UNSOL_EV (1<<4)
  553. /* retrieve RIRB entry - called from interrupt handler */
  554. static void azx_update_rirb(struct azx *chip)
  555. {
  556. unsigned int rp, wp;
  557. unsigned int addr;
  558. u32 res, res_ex;
  559. wp = azx_readb(chip, RIRBWP);
  560. if (wp == chip->rirb.wp)
  561. return;
  562. chip->rirb.wp = wp;
  563. while (chip->rirb.rp != wp) {
  564. chip->rirb.rp++;
  565. chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
  566. rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
  567. res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
  568. res = le32_to_cpu(chip->rirb.buf[rp]);
  569. addr = azx_response_addr(res_ex);
  570. if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
  571. snd_hda_queue_unsol_event(chip->bus, res, res_ex);
  572. else if (chip->rirb.cmds[addr]) {
  573. chip->rirb.res[addr] = res;
  574. smp_wmb();
  575. chip->rirb.cmds[addr]--;
  576. } else
  577. snd_printk(KERN_ERR SFX "spurious response %#x:%#x, "
  578. "last cmd=%#08x\n",
  579. res, res_ex,
  580. chip->last_cmd[addr]);
  581. }
  582. }
  583. /* receive a response */
  584. static unsigned int azx_rirb_get_response(struct hda_bus *bus,
  585. unsigned int addr)
  586. {
  587. struct azx *chip = bus->private_data;
  588. unsigned long timeout;
  589. int do_poll = 0;
  590. again:
  591. timeout = jiffies + msecs_to_jiffies(1000);
  592. for (;;) {
  593. if (chip->polling_mode || do_poll) {
  594. spin_lock_irq(&chip->reg_lock);
  595. azx_update_rirb(chip);
  596. spin_unlock_irq(&chip->reg_lock);
  597. }
  598. if (!chip->rirb.cmds[addr]) {
  599. smp_rmb();
  600. bus->rirb_error = 0;
  601. if (!do_poll)
  602. chip->poll_count = 0;
  603. return chip->rirb.res[addr]; /* the last value */
  604. }
  605. if (time_after(jiffies, timeout))
  606. break;
  607. if (bus->needs_damn_long_delay)
  608. msleep(2); /* temporary workaround */
  609. else {
  610. udelay(10);
  611. cond_resched();
  612. }
  613. }
  614. if (!chip->polling_mode && chip->poll_count < 2) {
  615. snd_printdd(SFX "azx_get_response timeout, "
  616. "polling the codec once: last cmd=0x%08x\n",
  617. chip->last_cmd[addr]);
  618. do_poll = 1;
  619. chip->poll_count++;
  620. goto again;
  621. }
  622. if (!chip->polling_mode) {
  623. snd_printk(KERN_WARNING SFX "azx_get_response timeout, "
  624. "switching to polling mode: last cmd=0x%08x\n",
  625. chip->last_cmd[addr]);
  626. chip->polling_mode = 1;
  627. goto again;
  628. }
  629. if (chip->msi) {
  630. snd_printk(KERN_WARNING SFX "No response from codec, "
  631. "disabling MSI: last cmd=0x%08x\n",
  632. chip->last_cmd[addr]);
  633. free_irq(chip->irq, chip);
  634. chip->irq = -1;
  635. pci_disable_msi(chip->pci);
  636. chip->msi = 0;
  637. if (azx_acquire_irq(chip, 1) < 0) {
  638. bus->rirb_error = 1;
  639. return -1;
  640. }
  641. goto again;
  642. }
  643. if (chip->probing) {
  644. /* If this critical timeout happens during the codec probing
  645. * phase, this is likely an access to a non-existing codec
  646. * slot. Better to return an error and reset the system.
  647. */
  648. return -1;
  649. }
  650. /* a fatal communication error; need either to reset or to fallback
  651. * to the single_cmd mode
  652. */
  653. bus->rirb_error = 1;
  654. if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
  655. bus->response_reset = 1;
  656. return -1; /* give a chance to retry */
  657. }
  658. snd_printk(KERN_ERR "hda_intel: azx_get_response timeout, "
  659. "switching to single_cmd mode: last cmd=0x%08x\n",
  660. chip->last_cmd[addr]);
  661. chip->single_cmd = 1;
  662. bus->response_reset = 0;
  663. /* release CORB/RIRB */
  664. azx_free_cmd_io(chip);
  665. /* disable unsolicited responses */
  666. azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
  667. return -1;
  668. }
  669. /*
  670. * Use the single immediate command instead of CORB/RIRB for simplicity
  671. *
  672. * Note: according to Intel, this is not preferred use. The command was
  673. * intended for the BIOS only, and may get confused with unsolicited
  674. * responses. So, we shouldn't use it for normal operation from the
  675. * driver.
  676. * I left the codes, however, for debugging/testing purposes.
  677. */
  678. /* receive a response */
  679. static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
  680. {
  681. int timeout = 50;
  682. while (timeout--) {
  683. /* check IRV busy bit */
  684. if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
  685. /* reuse rirb.res as the response return value */
  686. chip->rirb.res[addr] = azx_readl(chip, IR);
  687. return 0;
  688. }
  689. udelay(1);
  690. }
  691. if (printk_ratelimit())
  692. snd_printd(SFX "get_response timeout: IRS=0x%x\n",
  693. azx_readw(chip, IRS));
  694. chip->rirb.res[addr] = -1;
  695. return -EIO;
  696. }
  697. /* send a command */
  698. static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
  699. {
  700. struct azx *chip = bus->private_data;
  701. unsigned int addr = azx_command_addr(val);
  702. int timeout = 50;
  703. bus->rirb_error = 0;
  704. while (timeout--) {
  705. /* check ICB busy bit */
  706. if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
  707. /* Clear IRV valid bit */
  708. azx_writew(chip, IRS, azx_readw(chip, IRS) |
  709. ICH6_IRS_VALID);
  710. azx_writel(chip, IC, val);
  711. azx_writew(chip, IRS, azx_readw(chip, IRS) |
  712. ICH6_IRS_BUSY);
  713. return azx_single_wait_for_response(chip, addr);
  714. }
  715. udelay(1);
  716. }
  717. if (printk_ratelimit())
  718. snd_printd(SFX "send_cmd timeout: IRS=0x%x, val=0x%x\n",
  719. azx_readw(chip, IRS), val);
  720. return -EIO;
  721. }
  722. /* receive a response */
  723. static unsigned int azx_single_get_response(struct hda_bus *bus,
  724. unsigned int addr)
  725. {
  726. struct azx *chip = bus->private_data;
  727. return chip->rirb.res[addr];
  728. }
  729. /*
  730. * The below are the main callbacks from hda_codec.
  731. *
  732. * They are just the skeleton to call sub-callbacks according to the
  733. * current setting of chip->single_cmd.
  734. */
  735. /* send a command */
  736. static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
  737. {
  738. struct azx *chip = bus->private_data;
  739. chip->last_cmd[azx_command_addr(val)] = val;
  740. if (chip->single_cmd)
  741. return azx_single_send_cmd(bus, val);
  742. else
  743. return azx_corb_send_cmd(bus, val);
  744. }
  745. /* get a response */
  746. static unsigned int azx_get_response(struct hda_bus *bus,
  747. unsigned int addr)
  748. {
  749. struct azx *chip = bus->private_data;
  750. if (chip->single_cmd)
  751. return azx_single_get_response(bus, addr);
  752. else
  753. return azx_rirb_get_response(bus, addr);
  754. }
  755. #ifdef CONFIG_SND_HDA_POWER_SAVE
  756. static void azx_power_notify(struct hda_bus *bus);
  757. #endif
  758. /* reset codec link */
  759. static int azx_reset(struct azx *chip, int full_reset)
  760. {
  761. int count;
  762. if (!full_reset)
  763. goto __skip;
  764. /* clear STATESTS */
  765. azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
  766. /* reset controller */
  767. azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
  768. count = 50;
  769. while (azx_readb(chip, GCTL) && --count)
  770. msleep(1);
  771. /* delay for >= 100us for codec PLL to settle per spec
  772. * Rev 0.9 section 5.5.1
  773. */
  774. msleep(1);
  775. /* Bring controller out of reset */
  776. azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
  777. count = 50;
  778. while (!azx_readb(chip, GCTL) && --count)
  779. msleep(1);
  780. /* Brent Chartrand said to wait >= 540us for codecs to initialize */
  781. msleep(1);
  782. __skip:
  783. /* check to see if controller is ready */
  784. if (!azx_readb(chip, GCTL)) {
  785. snd_printd(SFX "azx_reset: controller not ready!\n");
  786. return -EBUSY;
  787. }
  788. /* Accept unsolicited responses */
  789. if (!chip->single_cmd)
  790. azx_writel(chip, GCTL, azx_readl(chip, GCTL) |
  791. ICH6_GCTL_UNSOL);
  792. /* detect codecs */
  793. if (!chip->codec_mask) {
  794. chip->codec_mask = azx_readw(chip, STATESTS);
  795. snd_printdd(SFX "codec_mask = 0x%x\n", chip->codec_mask);
  796. }
  797. return 0;
  798. }
  799. /*
  800. * Lowlevel interface
  801. */
  802. /* enable interrupts */
  803. static void azx_int_enable(struct azx *chip)
  804. {
  805. /* enable controller CIE and GIE */
  806. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
  807. ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
  808. }
  809. /* disable interrupts */
  810. static void azx_int_disable(struct azx *chip)
  811. {
  812. int i;
  813. /* disable interrupts in stream descriptor */
  814. for (i = 0; i < chip->num_streams; i++) {
  815. struct azx_dev *azx_dev = &chip->azx_dev[i];
  816. azx_sd_writeb(azx_dev, SD_CTL,
  817. azx_sd_readb(azx_dev, SD_CTL) & ~SD_INT_MASK);
  818. }
  819. /* disable SIE for all streams */
  820. azx_writeb(chip, INTCTL, 0);
  821. /* disable controller CIE and GIE */
  822. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
  823. ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
  824. }
  825. /* clear interrupts */
  826. static void azx_int_clear(struct azx *chip)
  827. {
  828. int i;
  829. /* clear stream status */
  830. for (i = 0; i < chip->num_streams; i++) {
  831. struct azx_dev *azx_dev = &chip->azx_dev[i];
  832. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  833. }
  834. /* clear STATESTS */
  835. azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
  836. /* clear rirb status */
  837. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  838. /* clear int status */
  839. azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
  840. }
  841. /* start a stream */
  842. static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
  843. {
  844. /*
  845. * Before stream start, initialize parameter
  846. */
  847. azx_dev->insufficient = 1;
  848. /* enable SIE */
  849. azx_writel(chip, INTCTL,
  850. azx_readl(chip, INTCTL) | (1 << azx_dev->index));
  851. /* set DMA start and interrupt mask */
  852. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
  853. SD_CTL_DMA_START | SD_INT_MASK);
  854. }
  855. /* stop DMA */
  856. static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
  857. {
  858. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) &
  859. ~(SD_CTL_DMA_START | SD_INT_MASK));
  860. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
  861. }
  862. /* stop a stream */
  863. static void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
  864. {
  865. azx_stream_clear(chip, azx_dev);
  866. /* disable SIE */
  867. azx_writel(chip, INTCTL,
  868. azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
  869. }
  870. /*
  871. * reset and start the controller registers
  872. */
  873. static void azx_init_chip(struct azx *chip, int full_reset)
  874. {
  875. if (chip->initialized)
  876. return;
  877. /* reset controller */
  878. azx_reset(chip, full_reset);
  879. /* initialize interrupts */
  880. azx_int_clear(chip);
  881. azx_int_enable(chip);
  882. /* initialize the codec command I/O */
  883. if (!chip->single_cmd)
  884. azx_init_cmd_io(chip);
  885. /* program the position buffer */
  886. azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
  887. azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr));
  888. chip->initialized = 1;
  889. }
  890. /*
  891. * initialize the PCI registers
  892. */
  893. /* update bits in a PCI register byte */
  894. static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
  895. unsigned char mask, unsigned char val)
  896. {
  897. unsigned char data;
  898. pci_read_config_byte(pci, reg, &data);
  899. data &= ~mask;
  900. data |= (val & mask);
  901. pci_write_config_byte(pci, reg, data);
  902. }
  903. static void azx_init_pci(struct azx *chip)
  904. {
  905. unsigned short snoop;
  906. /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
  907. * TCSEL == Traffic Class Select Register, which sets PCI express QOS
  908. * Ensuring these bits are 0 clears playback static on some HD Audio
  909. * codecs.
  910. * The PCI register TCSEL is defined in the Intel manuals.
  911. */
  912. if (chip->driver_type != AZX_DRIVER_ATI &&
  913. chip->driver_type != AZX_DRIVER_ATIHDMI)
  914. update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
  915. switch (chip->driver_type) {
  916. case AZX_DRIVER_ATI:
  917. /* For ATI SB450 azalia HD audio, we need to enable snoop */
  918. update_pci_byte(chip->pci,
  919. ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR,
  920. 0x07, ATI_SB450_HDAUDIO_ENABLE_SNOOP);
  921. break;
  922. case AZX_DRIVER_NVIDIA:
  923. /* For NVIDIA HDA, enable snoop */
  924. update_pci_byte(chip->pci,
  925. NVIDIA_HDA_TRANSREG_ADDR,
  926. 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
  927. update_pci_byte(chip->pci,
  928. NVIDIA_HDA_ISTRM_COH,
  929. 0x01, NVIDIA_HDA_ENABLE_COHBIT);
  930. update_pci_byte(chip->pci,
  931. NVIDIA_HDA_OSTRM_COH,
  932. 0x01, NVIDIA_HDA_ENABLE_COHBIT);
  933. break;
  934. case AZX_DRIVER_SCH:
  935. case AZX_DRIVER_PCH:
  936. pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
  937. if (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) {
  938. pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC,
  939. snoop & (~INTEL_SCH_HDA_DEVC_NOSNOOP));
  940. pci_read_config_word(chip->pci,
  941. INTEL_SCH_HDA_DEVC, &snoop);
  942. snd_printdd(SFX "HDA snoop disabled, enabling ... %s\n",
  943. (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)
  944. ? "Failed" : "OK");
  945. }
  946. break;
  947. default:
  948. /* AMD Hudson needs the similar snoop, as it seems... */
  949. if (chip->pci->vendor == PCI_VENDOR_ID_AMD)
  950. update_pci_byte(chip->pci,
  951. ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR,
  952. 0x07, ATI_SB450_HDAUDIO_ENABLE_SNOOP);
  953. break;
  954. }
  955. }
  956. static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
  957. /*
  958. * interrupt handler
  959. */
  960. static irqreturn_t azx_interrupt(int irq, void *dev_id)
  961. {
  962. struct azx *chip = dev_id;
  963. struct azx_dev *azx_dev;
  964. u32 status;
  965. u8 sd_status;
  966. int i, ok;
  967. spin_lock(&chip->reg_lock);
  968. status = azx_readl(chip, INTSTS);
  969. if (status == 0) {
  970. spin_unlock(&chip->reg_lock);
  971. return IRQ_NONE;
  972. }
  973. for (i = 0; i < chip->num_streams; i++) {
  974. azx_dev = &chip->azx_dev[i];
  975. if (status & azx_dev->sd_int_sta_mask) {
  976. sd_status = azx_sd_readb(azx_dev, SD_STS);
  977. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  978. if (!azx_dev->substream || !azx_dev->running ||
  979. !(sd_status & SD_INT_COMPLETE))
  980. continue;
  981. /* check whether this IRQ is really acceptable */
  982. ok = azx_position_ok(chip, azx_dev);
  983. if (ok == 1) {
  984. azx_dev->irq_pending = 0;
  985. spin_unlock(&chip->reg_lock);
  986. snd_pcm_period_elapsed(azx_dev->substream);
  987. spin_lock(&chip->reg_lock);
  988. } else if (ok == 0 && chip->bus && chip->bus->workq) {
  989. /* bogus IRQ, process it later */
  990. azx_dev->irq_pending = 1;
  991. queue_work(chip->bus->workq,
  992. &chip->irq_pending_work);
  993. }
  994. }
  995. }
  996. /* clear rirb int */
  997. status = azx_readb(chip, RIRBSTS);
  998. if (status & RIRB_INT_MASK) {
  999. if (status & RIRB_INT_RESPONSE) {
  1000. if (chip->driver_type == AZX_DRIVER_CTX)
  1001. udelay(80);
  1002. azx_update_rirb(chip);
  1003. }
  1004. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  1005. }
  1006. #if 0
  1007. /* clear state status int */
  1008. if (azx_readb(chip, STATESTS) & 0x04)
  1009. azx_writeb(chip, STATESTS, 0x04);
  1010. #endif
  1011. spin_unlock(&chip->reg_lock);
  1012. return IRQ_HANDLED;
  1013. }
  1014. /*
  1015. * set up a BDL entry
  1016. */
  1017. static int setup_bdle(struct snd_pcm_substream *substream,
  1018. struct azx_dev *azx_dev, u32 **bdlp,
  1019. int ofs, int size, int with_ioc)
  1020. {
  1021. u32 *bdl = *bdlp;
  1022. while (size > 0) {
  1023. dma_addr_t addr;
  1024. int chunk;
  1025. if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
  1026. return -EINVAL;
  1027. addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  1028. /* program the address field of the BDL entry */
  1029. bdl[0] = cpu_to_le32((u32)addr);
  1030. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  1031. /* program the size field of the BDL entry */
  1032. chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
  1033. bdl[2] = cpu_to_le32(chunk);
  1034. /* program the IOC to enable interrupt
  1035. * only when the whole fragment is processed
  1036. */
  1037. size -= chunk;
  1038. bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
  1039. bdl += 4;
  1040. azx_dev->frags++;
  1041. ofs += chunk;
  1042. }
  1043. *bdlp = bdl;
  1044. return ofs;
  1045. }
  1046. /*
  1047. * set up BDL entries
  1048. */
  1049. static int azx_setup_periods(struct azx *chip,
  1050. struct snd_pcm_substream *substream,
  1051. struct azx_dev *azx_dev)
  1052. {
  1053. u32 *bdl;
  1054. int i, ofs, periods, period_bytes;
  1055. int pos_adj;
  1056. /* reset BDL address */
  1057. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  1058. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  1059. period_bytes = azx_dev->period_bytes;
  1060. periods = azx_dev->bufsize / period_bytes;
  1061. /* program the initial BDL entries */
  1062. bdl = (u32 *)azx_dev->bdl.area;
  1063. ofs = 0;
  1064. azx_dev->frags = 0;
  1065. pos_adj = bdl_pos_adj[chip->dev_index];
  1066. if (pos_adj > 0) {
  1067. struct snd_pcm_runtime *runtime = substream->runtime;
  1068. int pos_align = pos_adj;
  1069. pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
  1070. if (!pos_adj)
  1071. pos_adj = pos_align;
  1072. else
  1073. pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
  1074. pos_align;
  1075. pos_adj = frames_to_bytes(runtime, pos_adj);
  1076. if (pos_adj >= period_bytes) {
  1077. snd_printk(KERN_WARNING SFX "Too big adjustment %d\n",
  1078. bdl_pos_adj[chip->dev_index]);
  1079. pos_adj = 0;
  1080. } else {
  1081. ofs = setup_bdle(substream, azx_dev,
  1082. &bdl, ofs, pos_adj,
  1083. !substream->runtime->no_period_wakeup);
  1084. if (ofs < 0)
  1085. goto error;
  1086. }
  1087. } else
  1088. pos_adj = 0;
  1089. for (i = 0; i < periods; i++) {
  1090. if (i == periods - 1 && pos_adj)
  1091. ofs = setup_bdle(substream, azx_dev, &bdl, ofs,
  1092. period_bytes - pos_adj, 0);
  1093. else
  1094. ofs = setup_bdle(substream, azx_dev, &bdl, ofs,
  1095. period_bytes,
  1096. !substream->runtime->no_period_wakeup);
  1097. if (ofs < 0)
  1098. goto error;
  1099. }
  1100. return 0;
  1101. error:
  1102. snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
  1103. azx_dev->bufsize, period_bytes);
  1104. return -EINVAL;
  1105. }
  1106. /* reset stream */
  1107. static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
  1108. {
  1109. unsigned char val;
  1110. int timeout;
  1111. azx_stream_clear(chip, azx_dev);
  1112. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
  1113. SD_CTL_STREAM_RESET);
  1114. udelay(3);
  1115. timeout = 300;
  1116. while (!((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  1117. --timeout)
  1118. ;
  1119. val &= ~SD_CTL_STREAM_RESET;
  1120. azx_sd_writeb(azx_dev, SD_CTL, val);
  1121. udelay(3);
  1122. timeout = 300;
  1123. /* waiting for hardware to report that the stream is out of reset */
  1124. while (((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  1125. --timeout)
  1126. ;
  1127. /* reset first position - may not be synced with hw at this time */
  1128. *azx_dev->posbuf = 0;
  1129. }
  1130. /*
  1131. * set up the SD for streaming
  1132. */
  1133. static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
  1134. {
  1135. /* make sure the run bit is zero for SD */
  1136. azx_stream_clear(chip, azx_dev);
  1137. /* program the stream_tag */
  1138. azx_sd_writel(azx_dev, SD_CTL,
  1139. (azx_sd_readl(azx_dev, SD_CTL) & ~SD_CTL_STREAM_TAG_MASK)|
  1140. (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT));
  1141. /* program the length of samples in cyclic buffer */
  1142. azx_sd_writel(azx_dev, SD_CBL, azx_dev->bufsize);
  1143. /* program the stream format */
  1144. /* this value needs to be the same as the one programmed */
  1145. azx_sd_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
  1146. /* program the stream LVI (last valid index) of the BDL */
  1147. azx_sd_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
  1148. /* program the BDL address */
  1149. /* lower BDL address */
  1150. azx_sd_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
  1151. /* upper BDL address */
  1152. azx_sd_writel(azx_dev, SD_BDLPU, upper_32_bits(azx_dev->bdl.addr));
  1153. /* enable the position buffer */
  1154. if (chip->position_fix[0] != POS_FIX_LPIB ||
  1155. chip->position_fix[1] != POS_FIX_LPIB) {
  1156. if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
  1157. azx_writel(chip, DPLBASE,
  1158. (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
  1159. }
  1160. /* set the interrupt enable bits in the descriptor control register */
  1161. azx_sd_writel(azx_dev, SD_CTL,
  1162. azx_sd_readl(azx_dev, SD_CTL) | SD_INT_MASK);
  1163. return 0;
  1164. }
  1165. /*
  1166. * Probe the given codec address
  1167. */
  1168. static int probe_codec(struct azx *chip, int addr)
  1169. {
  1170. unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
  1171. (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
  1172. unsigned int res;
  1173. mutex_lock(&chip->bus->cmd_mutex);
  1174. chip->probing = 1;
  1175. azx_send_cmd(chip->bus, cmd);
  1176. res = azx_get_response(chip->bus, addr);
  1177. chip->probing = 0;
  1178. mutex_unlock(&chip->bus->cmd_mutex);
  1179. if (res == -1)
  1180. return -EIO;
  1181. snd_printdd(SFX "codec #%d probed OK\n", addr);
  1182. return 0;
  1183. }
  1184. static int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
  1185. struct hda_pcm *cpcm);
  1186. static void azx_stop_chip(struct azx *chip);
  1187. static void azx_bus_reset(struct hda_bus *bus)
  1188. {
  1189. struct azx *chip = bus->private_data;
  1190. bus->in_reset = 1;
  1191. azx_stop_chip(chip);
  1192. azx_init_chip(chip, 1);
  1193. #ifdef CONFIG_PM
  1194. if (chip->initialized) {
  1195. int i;
  1196. for (i = 0; i < HDA_MAX_PCMS; i++)
  1197. snd_pcm_suspend_all(chip->pcm[i]);
  1198. snd_hda_suspend(chip->bus);
  1199. snd_hda_resume(chip->bus);
  1200. }
  1201. #endif
  1202. bus->in_reset = 0;
  1203. }
  1204. /*
  1205. * Codec initialization
  1206. */
  1207. /* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
  1208. static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] __devinitdata = {
  1209. [AZX_DRIVER_NVIDIA] = 8,
  1210. [AZX_DRIVER_TERA] = 1,
  1211. };
  1212. static int __devinit azx_codec_create(struct azx *chip, const char *model)
  1213. {
  1214. struct hda_bus_template bus_temp;
  1215. int c, codecs, err;
  1216. int max_slots;
  1217. memset(&bus_temp, 0, sizeof(bus_temp));
  1218. bus_temp.private_data = chip;
  1219. bus_temp.modelname = model;
  1220. bus_temp.pci = chip->pci;
  1221. bus_temp.ops.command = azx_send_cmd;
  1222. bus_temp.ops.get_response = azx_get_response;
  1223. bus_temp.ops.attach_pcm = azx_attach_pcm_stream;
  1224. bus_temp.ops.bus_reset = azx_bus_reset;
  1225. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1226. bus_temp.power_save = &power_save;
  1227. bus_temp.ops.pm_notify = azx_power_notify;
  1228. #endif
  1229. err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus);
  1230. if (err < 0)
  1231. return err;
  1232. if (chip->driver_type == AZX_DRIVER_NVIDIA)
  1233. chip->bus->needs_damn_long_delay = 1;
  1234. codecs = 0;
  1235. max_slots = azx_max_codecs[chip->driver_type];
  1236. if (!max_slots)
  1237. max_slots = AZX_DEFAULT_CODECS;
  1238. /* First try to probe all given codec slots */
  1239. for (c = 0; c < max_slots; c++) {
  1240. if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
  1241. if (probe_codec(chip, c) < 0) {
  1242. /* Some BIOSen give you wrong codec addresses
  1243. * that don't exist
  1244. */
  1245. snd_printk(KERN_WARNING SFX
  1246. "Codec #%d probe error; "
  1247. "disabling it...\n", c);
  1248. chip->codec_mask &= ~(1 << c);
  1249. /* More badly, accessing to a non-existing
  1250. * codec often screws up the controller chip,
  1251. * and disturbs the further communications.
  1252. * Thus if an error occurs during probing,
  1253. * better to reset the controller chip to
  1254. * get back to the sanity state.
  1255. */
  1256. azx_stop_chip(chip);
  1257. azx_init_chip(chip, 1);
  1258. }
  1259. }
  1260. }
  1261. /* AMD chipsets often cause the communication stalls upon certain
  1262. * sequence like the pin-detection. It seems that forcing the synced
  1263. * access works around the stall. Grrr...
  1264. */
  1265. if (chip->pci->vendor == PCI_VENDOR_ID_AMD ||
  1266. chip->pci->vendor == PCI_VENDOR_ID_ATI) {
  1267. snd_printk(KERN_INFO SFX "Enable sync_write for AMD chipset\n");
  1268. chip->bus->sync_write = 1;
  1269. chip->bus->allow_bus_reset = 1;
  1270. }
  1271. /* Then create codec instances */
  1272. for (c = 0; c < max_slots; c++) {
  1273. if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
  1274. struct hda_codec *codec;
  1275. err = snd_hda_codec_new(chip->bus, c, &codec);
  1276. if (err < 0)
  1277. continue;
  1278. codec->beep_mode = chip->beep_mode;
  1279. codecs++;
  1280. }
  1281. }
  1282. if (!codecs) {
  1283. snd_printk(KERN_ERR SFX "no codecs initialized\n");
  1284. return -ENXIO;
  1285. }
  1286. return 0;
  1287. }
  1288. /* configure each codec instance */
  1289. static int __devinit azx_codec_configure(struct azx *chip)
  1290. {
  1291. struct hda_codec *codec;
  1292. list_for_each_entry(codec, &chip->bus->codec_list, list) {
  1293. snd_hda_codec_configure(codec);
  1294. }
  1295. return 0;
  1296. }
  1297. /*
  1298. * PCM support
  1299. */
  1300. /* assign a stream for the PCM */
  1301. static inline struct azx_dev *
  1302. azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
  1303. {
  1304. int dev, i, nums;
  1305. struct azx_dev *res = NULL;
  1306. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1307. dev = chip->playback_index_offset;
  1308. nums = chip->playback_streams;
  1309. } else {
  1310. dev = chip->capture_index_offset;
  1311. nums = chip->capture_streams;
  1312. }
  1313. for (i = 0; i < nums; i++, dev++)
  1314. if (!chip->azx_dev[dev].opened) {
  1315. res = &chip->azx_dev[dev];
  1316. if (res->device == substream->pcm->device)
  1317. break;
  1318. }
  1319. if (res) {
  1320. res->opened = 1;
  1321. res->device = substream->pcm->device;
  1322. }
  1323. return res;
  1324. }
  1325. /* release the assigned stream */
  1326. static inline void azx_release_device(struct azx_dev *azx_dev)
  1327. {
  1328. azx_dev->opened = 0;
  1329. }
  1330. static struct snd_pcm_hardware azx_pcm_hw = {
  1331. .info = (SNDRV_PCM_INFO_MMAP |
  1332. SNDRV_PCM_INFO_INTERLEAVED |
  1333. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1334. SNDRV_PCM_INFO_MMAP_VALID |
  1335. /* No full-resume yet implemented */
  1336. /* SNDRV_PCM_INFO_RESUME |*/
  1337. SNDRV_PCM_INFO_PAUSE |
  1338. SNDRV_PCM_INFO_SYNC_START |
  1339. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
  1340. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1341. .rates = SNDRV_PCM_RATE_48000,
  1342. .rate_min = 48000,
  1343. .rate_max = 48000,
  1344. .channels_min = 2,
  1345. .channels_max = 2,
  1346. .buffer_bytes_max = AZX_MAX_BUF_SIZE,
  1347. .period_bytes_min = 128,
  1348. .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
  1349. .periods_min = 2,
  1350. .periods_max = AZX_MAX_FRAG,
  1351. .fifo_size = 0,
  1352. };
  1353. struct azx_pcm {
  1354. struct azx *chip;
  1355. struct hda_codec *codec;
  1356. struct hda_pcm_stream *hinfo[2];
  1357. };
  1358. static int azx_pcm_open(struct snd_pcm_substream *substream)
  1359. {
  1360. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1361. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1362. struct azx *chip = apcm->chip;
  1363. struct azx_dev *azx_dev;
  1364. struct snd_pcm_runtime *runtime = substream->runtime;
  1365. unsigned long flags;
  1366. int err;
  1367. mutex_lock(&chip->open_mutex);
  1368. azx_dev = azx_assign_device(chip, substream);
  1369. if (azx_dev == NULL) {
  1370. mutex_unlock(&chip->open_mutex);
  1371. return -EBUSY;
  1372. }
  1373. runtime->hw = azx_pcm_hw;
  1374. runtime->hw.channels_min = hinfo->channels_min;
  1375. runtime->hw.channels_max = hinfo->channels_max;
  1376. runtime->hw.formats = hinfo->formats;
  1377. runtime->hw.rates = hinfo->rates;
  1378. snd_pcm_limit_hw_rates(runtime);
  1379. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  1380. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1381. 128);
  1382. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1383. 128);
  1384. snd_hda_power_up(apcm->codec);
  1385. err = hinfo->ops.open(hinfo, apcm->codec, substream);
  1386. if (err < 0) {
  1387. azx_release_device(azx_dev);
  1388. snd_hda_power_down(apcm->codec);
  1389. mutex_unlock(&chip->open_mutex);
  1390. return err;
  1391. }
  1392. snd_pcm_limit_hw_rates(runtime);
  1393. /* sanity check */
  1394. if (snd_BUG_ON(!runtime->hw.channels_min) ||
  1395. snd_BUG_ON(!runtime->hw.channels_max) ||
  1396. snd_BUG_ON(!runtime->hw.formats) ||
  1397. snd_BUG_ON(!runtime->hw.rates)) {
  1398. azx_release_device(azx_dev);
  1399. hinfo->ops.close(hinfo, apcm->codec, substream);
  1400. snd_hda_power_down(apcm->codec);
  1401. mutex_unlock(&chip->open_mutex);
  1402. return -EINVAL;
  1403. }
  1404. spin_lock_irqsave(&chip->reg_lock, flags);
  1405. azx_dev->substream = substream;
  1406. azx_dev->running = 0;
  1407. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1408. runtime->private_data = azx_dev;
  1409. snd_pcm_set_sync(substream);
  1410. mutex_unlock(&chip->open_mutex);
  1411. return 0;
  1412. }
  1413. static int azx_pcm_close(struct snd_pcm_substream *substream)
  1414. {
  1415. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1416. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1417. struct azx *chip = apcm->chip;
  1418. struct azx_dev *azx_dev = get_azx_dev(substream);
  1419. unsigned long flags;
  1420. mutex_lock(&chip->open_mutex);
  1421. spin_lock_irqsave(&chip->reg_lock, flags);
  1422. azx_dev->substream = NULL;
  1423. azx_dev->running = 0;
  1424. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1425. azx_release_device(azx_dev);
  1426. hinfo->ops.close(hinfo, apcm->codec, substream);
  1427. snd_hda_power_down(apcm->codec);
  1428. mutex_unlock(&chip->open_mutex);
  1429. return 0;
  1430. }
  1431. static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
  1432. struct snd_pcm_hw_params *hw_params)
  1433. {
  1434. struct azx_dev *azx_dev = get_azx_dev(substream);
  1435. azx_dev->bufsize = 0;
  1436. azx_dev->period_bytes = 0;
  1437. azx_dev->format_val = 0;
  1438. return snd_pcm_lib_malloc_pages(substream,
  1439. params_buffer_bytes(hw_params));
  1440. }
  1441. static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
  1442. {
  1443. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1444. struct azx_dev *azx_dev = get_azx_dev(substream);
  1445. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1446. /* reset BDL address */
  1447. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  1448. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  1449. azx_sd_writel(azx_dev, SD_CTL, 0);
  1450. azx_dev->bufsize = 0;
  1451. azx_dev->period_bytes = 0;
  1452. azx_dev->format_val = 0;
  1453. snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
  1454. return snd_pcm_lib_free_pages(substream);
  1455. }
  1456. static int azx_pcm_prepare(struct snd_pcm_substream *substream)
  1457. {
  1458. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1459. struct azx *chip = apcm->chip;
  1460. struct azx_dev *azx_dev = get_azx_dev(substream);
  1461. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1462. struct snd_pcm_runtime *runtime = substream->runtime;
  1463. unsigned int bufsize, period_bytes, format_val, stream_tag;
  1464. int err;
  1465. azx_stream_reset(chip, azx_dev);
  1466. format_val = snd_hda_calc_stream_format(runtime->rate,
  1467. runtime->channels,
  1468. runtime->format,
  1469. hinfo->maxbps,
  1470. apcm->codec->spdif_ctls);
  1471. if (!format_val) {
  1472. snd_printk(KERN_ERR SFX
  1473. "invalid format_val, rate=%d, ch=%d, format=%d\n",
  1474. runtime->rate, runtime->channels, runtime->format);
  1475. return -EINVAL;
  1476. }
  1477. bufsize = snd_pcm_lib_buffer_bytes(substream);
  1478. period_bytes = snd_pcm_lib_period_bytes(substream);
  1479. snd_printdd(SFX "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
  1480. bufsize, format_val);
  1481. if (bufsize != azx_dev->bufsize ||
  1482. period_bytes != azx_dev->period_bytes ||
  1483. format_val != azx_dev->format_val) {
  1484. azx_dev->bufsize = bufsize;
  1485. azx_dev->period_bytes = period_bytes;
  1486. azx_dev->format_val = format_val;
  1487. err = azx_setup_periods(chip, substream, azx_dev);
  1488. if (err < 0)
  1489. return err;
  1490. }
  1491. /* wallclk has 24Mhz clock source */
  1492. azx_dev->period_wallclk = (((runtime->period_size * 24000) /
  1493. runtime->rate) * 1000);
  1494. azx_setup_controller(chip, azx_dev);
  1495. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1496. azx_dev->fifo_size = azx_sd_readw(azx_dev, SD_FIFOSIZE) + 1;
  1497. else
  1498. azx_dev->fifo_size = 0;
  1499. stream_tag = azx_dev->stream_tag;
  1500. /* CA-IBG chips need the playback stream starting from 1 */
  1501. if (chip->driver_type == AZX_DRIVER_CTX &&
  1502. stream_tag > chip->capture_streams)
  1503. stream_tag -= chip->capture_streams;
  1504. return snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
  1505. azx_dev->format_val, substream);
  1506. }
  1507. static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  1508. {
  1509. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1510. struct azx *chip = apcm->chip;
  1511. struct azx_dev *azx_dev;
  1512. struct snd_pcm_substream *s;
  1513. int rstart = 0, start, nsync = 0, sbits = 0;
  1514. int nwait, timeout;
  1515. switch (cmd) {
  1516. case SNDRV_PCM_TRIGGER_START:
  1517. rstart = 1;
  1518. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1519. case SNDRV_PCM_TRIGGER_RESUME:
  1520. start = 1;
  1521. break;
  1522. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1523. case SNDRV_PCM_TRIGGER_SUSPEND:
  1524. case SNDRV_PCM_TRIGGER_STOP:
  1525. start = 0;
  1526. break;
  1527. default:
  1528. return -EINVAL;
  1529. }
  1530. snd_pcm_group_for_each_entry(s, substream) {
  1531. if (s->pcm->card != substream->pcm->card)
  1532. continue;
  1533. azx_dev = get_azx_dev(s);
  1534. sbits |= 1 << azx_dev->index;
  1535. nsync++;
  1536. snd_pcm_trigger_done(s, substream);
  1537. }
  1538. spin_lock(&chip->reg_lock);
  1539. if (nsync > 1) {
  1540. /* first, set SYNC bits of corresponding streams */
  1541. azx_writel(chip, SYNC, azx_readl(chip, SYNC) | sbits);
  1542. }
  1543. snd_pcm_group_for_each_entry(s, substream) {
  1544. if (s->pcm->card != substream->pcm->card)
  1545. continue;
  1546. azx_dev = get_azx_dev(s);
  1547. if (start) {
  1548. azx_dev->start_wallclk = azx_readl(chip, WALLCLK);
  1549. if (!rstart)
  1550. azx_dev->start_wallclk -=
  1551. azx_dev->period_wallclk;
  1552. azx_stream_start(chip, azx_dev);
  1553. } else {
  1554. azx_stream_stop(chip, azx_dev);
  1555. }
  1556. azx_dev->running = start;
  1557. }
  1558. spin_unlock(&chip->reg_lock);
  1559. if (start) {
  1560. if (nsync == 1)
  1561. return 0;
  1562. /* wait until all FIFOs get ready */
  1563. for (timeout = 5000; timeout; timeout--) {
  1564. nwait = 0;
  1565. snd_pcm_group_for_each_entry(s, substream) {
  1566. if (s->pcm->card != substream->pcm->card)
  1567. continue;
  1568. azx_dev = get_azx_dev(s);
  1569. if (!(azx_sd_readb(azx_dev, SD_STS) &
  1570. SD_STS_FIFO_READY))
  1571. nwait++;
  1572. }
  1573. if (!nwait)
  1574. break;
  1575. cpu_relax();
  1576. }
  1577. } else {
  1578. /* wait until all RUN bits are cleared */
  1579. for (timeout = 5000; timeout; timeout--) {
  1580. nwait = 0;
  1581. snd_pcm_group_for_each_entry(s, substream) {
  1582. if (s->pcm->card != substream->pcm->card)
  1583. continue;
  1584. azx_dev = get_azx_dev(s);
  1585. if (azx_sd_readb(azx_dev, SD_CTL) &
  1586. SD_CTL_DMA_START)
  1587. nwait++;
  1588. }
  1589. if (!nwait)
  1590. break;
  1591. cpu_relax();
  1592. }
  1593. }
  1594. if (nsync > 1) {
  1595. spin_lock(&chip->reg_lock);
  1596. /* reset SYNC bits */
  1597. azx_writel(chip, SYNC, azx_readl(chip, SYNC) & ~sbits);
  1598. spin_unlock(&chip->reg_lock);
  1599. }
  1600. return 0;
  1601. }
  1602. /* get the current DMA position with correction on VIA chips */
  1603. static unsigned int azx_via_get_position(struct azx *chip,
  1604. struct azx_dev *azx_dev)
  1605. {
  1606. unsigned int link_pos, mini_pos, bound_pos;
  1607. unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
  1608. unsigned int fifo_size;
  1609. link_pos = azx_sd_readl(azx_dev, SD_LPIB);
  1610. if (azx_dev->index >= 4) {
  1611. /* Playback, no problem using link position */
  1612. return link_pos;
  1613. }
  1614. /* Capture */
  1615. /* For new chipset,
  1616. * use mod to get the DMA position just like old chipset
  1617. */
  1618. mod_dma_pos = le32_to_cpu(*azx_dev->posbuf);
  1619. mod_dma_pos %= azx_dev->period_bytes;
  1620. /* azx_dev->fifo_size can't get FIFO size of in stream.
  1621. * Get from base address + offset.
  1622. */
  1623. fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
  1624. if (azx_dev->insufficient) {
  1625. /* Link position never gather than FIFO size */
  1626. if (link_pos <= fifo_size)
  1627. return 0;
  1628. azx_dev->insufficient = 0;
  1629. }
  1630. if (link_pos <= fifo_size)
  1631. mini_pos = azx_dev->bufsize + link_pos - fifo_size;
  1632. else
  1633. mini_pos = link_pos - fifo_size;
  1634. /* Find nearest previous boudary */
  1635. mod_mini_pos = mini_pos % azx_dev->period_bytes;
  1636. mod_link_pos = link_pos % azx_dev->period_bytes;
  1637. if (mod_link_pos >= fifo_size)
  1638. bound_pos = link_pos - mod_link_pos;
  1639. else if (mod_dma_pos >= mod_mini_pos)
  1640. bound_pos = mini_pos - mod_mini_pos;
  1641. else {
  1642. bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes;
  1643. if (bound_pos >= azx_dev->bufsize)
  1644. bound_pos = 0;
  1645. }
  1646. /* Calculate real DMA position we want */
  1647. return bound_pos + mod_dma_pos;
  1648. }
  1649. static unsigned int azx_get_position(struct azx *chip,
  1650. struct azx_dev *azx_dev)
  1651. {
  1652. unsigned int pos;
  1653. int stream = azx_dev->substream->stream;
  1654. switch (chip->position_fix[stream]) {
  1655. case POS_FIX_LPIB:
  1656. /* read LPIB */
  1657. pos = azx_sd_readl(azx_dev, SD_LPIB);
  1658. break;
  1659. case POS_FIX_VIACOMBO:
  1660. pos = azx_via_get_position(chip, azx_dev);
  1661. break;
  1662. default:
  1663. /* use the position buffer */
  1664. pos = le32_to_cpu(*azx_dev->posbuf);
  1665. }
  1666. if (pos >= azx_dev->bufsize)
  1667. pos = 0;
  1668. return pos;
  1669. }
  1670. static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
  1671. {
  1672. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1673. struct azx *chip = apcm->chip;
  1674. struct azx_dev *azx_dev = get_azx_dev(substream);
  1675. return bytes_to_frames(substream->runtime,
  1676. azx_get_position(chip, azx_dev));
  1677. }
  1678. /*
  1679. * Check whether the current DMA position is acceptable for updating
  1680. * periods. Returns non-zero if it's OK.
  1681. *
  1682. * Many HD-audio controllers appear pretty inaccurate about
  1683. * the update-IRQ timing. The IRQ is issued before actually the
  1684. * data is processed. So, we need to process it afterwords in a
  1685. * workqueue.
  1686. */
  1687. static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
  1688. {
  1689. u32 wallclk;
  1690. unsigned int pos;
  1691. int stream;
  1692. wallclk = azx_readl(chip, WALLCLK) - azx_dev->start_wallclk;
  1693. if (wallclk < (azx_dev->period_wallclk * 2) / 3)
  1694. return -1; /* bogus (too early) interrupt */
  1695. stream = azx_dev->substream->stream;
  1696. pos = azx_get_position(chip, azx_dev);
  1697. if (chip->position_fix[stream] == POS_FIX_AUTO) {
  1698. if (!pos) {
  1699. printk(KERN_WARNING
  1700. "hda-intel: Invalid position buffer, "
  1701. "using LPIB read method instead.\n");
  1702. chip->position_fix[stream] = POS_FIX_LPIB;
  1703. pos = azx_get_position(chip, azx_dev);
  1704. } else
  1705. chip->position_fix[stream] = POS_FIX_POSBUF;
  1706. }
  1707. if (WARN_ONCE(!azx_dev->period_bytes,
  1708. "hda-intel: zero azx_dev->period_bytes"))
  1709. return -1; /* this shouldn't happen! */
  1710. if (wallclk < (azx_dev->period_wallclk * 5) / 4 &&
  1711. pos % azx_dev->period_bytes > azx_dev->period_bytes / 2)
  1712. /* NG - it's below the first next period boundary */
  1713. return bdl_pos_adj[chip->dev_index] ? 0 : -1;
  1714. azx_dev->start_wallclk += wallclk;
  1715. return 1; /* OK, it's fine */
  1716. }
  1717. /*
  1718. * The work for pending PCM period updates.
  1719. */
  1720. static void azx_irq_pending_work(struct work_struct *work)
  1721. {
  1722. struct azx *chip = container_of(work, struct azx, irq_pending_work);
  1723. int i, pending, ok;
  1724. if (!chip->irq_pending_warned) {
  1725. printk(KERN_WARNING
  1726. "hda-intel: IRQ timing workaround is activated "
  1727. "for card #%d. Suggest a bigger bdl_pos_adj.\n",
  1728. chip->card->number);
  1729. chip->irq_pending_warned = 1;
  1730. }
  1731. for (;;) {
  1732. pending = 0;
  1733. spin_lock_irq(&chip->reg_lock);
  1734. for (i = 0; i < chip->num_streams; i++) {
  1735. struct azx_dev *azx_dev = &chip->azx_dev[i];
  1736. if (!azx_dev->irq_pending ||
  1737. !azx_dev->substream ||
  1738. !azx_dev->running)
  1739. continue;
  1740. ok = azx_position_ok(chip, azx_dev);
  1741. if (ok > 0) {
  1742. azx_dev->irq_pending = 0;
  1743. spin_unlock(&chip->reg_lock);
  1744. snd_pcm_period_elapsed(azx_dev->substream);
  1745. spin_lock(&chip->reg_lock);
  1746. } else if (ok < 0) {
  1747. pending = 0; /* too early */
  1748. } else
  1749. pending++;
  1750. }
  1751. spin_unlock_irq(&chip->reg_lock);
  1752. if (!pending)
  1753. return;
  1754. msleep(1);
  1755. }
  1756. }
  1757. /* clear irq_pending flags and assure no on-going workq */
  1758. static void azx_clear_irq_pending(struct azx *chip)
  1759. {
  1760. int i;
  1761. spin_lock_irq(&chip->reg_lock);
  1762. for (i = 0; i < chip->num_streams; i++)
  1763. chip->azx_dev[i].irq_pending = 0;
  1764. spin_unlock_irq(&chip->reg_lock);
  1765. }
  1766. static struct snd_pcm_ops azx_pcm_ops = {
  1767. .open = azx_pcm_open,
  1768. .close = azx_pcm_close,
  1769. .ioctl = snd_pcm_lib_ioctl,
  1770. .hw_params = azx_pcm_hw_params,
  1771. .hw_free = azx_pcm_hw_free,
  1772. .prepare = azx_pcm_prepare,
  1773. .trigger = azx_pcm_trigger,
  1774. .pointer = azx_pcm_pointer,
  1775. .page = snd_pcm_sgbuf_ops_page,
  1776. };
  1777. static void azx_pcm_free(struct snd_pcm *pcm)
  1778. {
  1779. struct azx_pcm *apcm = pcm->private_data;
  1780. if (apcm) {
  1781. apcm->chip->pcm[pcm->device] = NULL;
  1782. kfree(apcm);
  1783. }
  1784. }
  1785. static int
  1786. azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
  1787. struct hda_pcm *cpcm)
  1788. {
  1789. struct azx *chip = bus->private_data;
  1790. struct snd_pcm *pcm;
  1791. struct azx_pcm *apcm;
  1792. int pcm_dev = cpcm->device;
  1793. int s, err;
  1794. if (pcm_dev >= HDA_MAX_PCMS) {
  1795. snd_printk(KERN_ERR SFX "Invalid PCM device number %d\n",
  1796. pcm_dev);
  1797. return -EINVAL;
  1798. }
  1799. if (chip->pcm[pcm_dev]) {
  1800. snd_printk(KERN_ERR SFX "PCM %d already exists\n", pcm_dev);
  1801. return -EBUSY;
  1802. }
  1803. err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
  1804. cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
  1805. cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
  1806. &pcm);
  1807. if (err < 0)
  1808. return err;
  1809. strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
  1810. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  1811. if (apcm == NULL)
  1812. return -ENOMEM;
  1813. apcm->chip = chip;
  1814. apcm->codec = codec;
  1815. pcm->private_data = apcm;
  1816. pcm->private_free = azx_pcm_free;
  1817. if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
  1818. pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
  1819. chip->pcm[pcm_dev] = pcm;
  1820. cpcm->pcm = pcm;
  1821. for (s = 0; s < 2; s++) {
  1822. apcm->hinfo[s] = &cpcm->stream[s];
  1823. if (cpcm->stream[s].substreams)
  1824. snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
  1825. }
  1826. /* buffer pre-allocation */
  1827. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1828. snd_dma_pci_data(chip->pci),
  1829. 1024 * 64, 32 * 1024 * 1024);
  1830. return 0;
  1831. }
  1832. /*
  1833. * mixer creation - all stuff is implemented in hda module
  1834. */
  1835. static int __devinit azx_mixer_create(struct azx *chip)
  1836. {
  1837. return snd_hda_build_controls(chip->bus);
  1838. }
  1839. /*
  1840. * initialize SD streams
  1841. */
  1842. static int __devinit azx_init_stream(struct azx *chip)
  1843. {
  1844. int i;
  1845. /* initialize each stream (aka device)
  1846. * assign the starting bdl address to each stream (device)
  1847. * and initialize
  1848. */
  1849. for (i = 0; i < chip->num_streams; i++) {
  1850. struct azx_dev *azx_dev = &chip->azx_dev[i];
  1851. azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
  1852. /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  1853. azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
  1854. /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
  1855. azx_dev->sd_int_sta_mask = 1 << i;
  1856. /* stream tag: must be non-zero and unique */
  1857. azx_dev->index = i;
  1858. azx_dev->stream_tag = i + 1;
  1859. }
  1860. return 0;
  1861. }
  1862. static int azx_acquire_irq(struct azx *chip, int do_disconnect)
  1863. {
  1864. if (request_irq(chip->pci->irq, azx_interrupt,
  1865. chip->msi ? 0 : IRQF_SHARED,
  1866. "hda_intel", chip)) {
  1867. printk(KERN_ERR "hda-intel: unable to grab IRQ %d, "
  1868. "disabling device\n", chip->pci->irq);
  1869. if (do_disconnect)
  1870. snd_card_disconnect(chip->card);
  1871. return -1;
  1872. }
  1873. chip->irq = chip->pci->irq;
  1874. pci_intx(chip->pci, !chip->msi);
  1875. return 0;
  1876. }
  1877. static void azx_stop_chip(struct azx *chip)
  1878. {
  1879. if (!chip->initialized)
  1880. return;
  1881. /* disable interrupts */
  1882. azx_int_disable(chip);
  1883. azx_int_clear(chip);
  1884. /* disable CORB/RIRB */
  1885. azx_free_cmd_io(chip);
  1886. /* disable position buffer */
  1887. azx_writel(chip, DPLBASE, 0);
  1888. azx_writel(chip, DPUBASE, 0);
  1889. chip->initialized = 0;
  1890. }
  1891. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1892. /* power-up/down the controller */
  1893. static void azx_power_notify(struct hda_bus *bus)
  1894. {
  1895. struct azx *chip = bus->private_data;
  1896. struct hda_codec *c;
  1897. int power_on = 0;
  1898. list_for_each_entry(c, &bus->codec_list, list) {
  1899. if (c->power_on) {
  1900. power_on = 1;
  1901. break;
  1902. }
  1903. }
  1904. if (power_on)
  1905. azx_init_chip(chip, 1);
  1906. else if (chip->running && power_save_controller &&
  1907. !bus->power_keep_link_on)
  1908. azx_stop_chip(chip);
  1909. }
  1910. #endif /* CONFIG_SND_HDA_POWER_SAVE */
  1911. #ifdef CONFIG_PM
  1912. /*
  1913. * power management
  1914. */
  1915. static int snd_hda_codecs_inuse(struct hda_bus *bus)
  1916. {
  1917. struct hda_codec *codec;
  1918. list_for_each_entry(codec, &bus->codec_list, list) {
  1919. if (snd_hda_codec_needs_resume(codec))
  1920. return 1;
  1921. }
  1922. return 0;
  1923. }
  1924. static int azx_suspend(struct pci_dev *pci, pm_message_t state)
  1925. {
  1926. struct snd_card *card = pci_get_drvdata(pci);
  1927. struct azx *chip = card->private_data;
  1928. int i;
  1929. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1930. azx_clear_irq_pending(chip);
  1931. for (i = 0; i < HDA_MAX_PCMS; i++)
  1932. snd_pcm_suspend_all(chip->pcm[i]);
  1933. if (chip->initialized)
  1934. snd_hda_suspend(chip->bus);
  1935. azx_stop_chip(chip);
  1936. if (chip->irq >= 0) {
  1937. free_irq(chip->irq, chip);
  1938. chip->irq = -1;
  1939. }
  1940. if (chip->msi)
  1941. pci_disable_msi(chip->pci);
  1942. pci_disable_device(pci);
  1943. pci_save_state(pci);
  1944. pci_set_power_state(pci, pci_choose_state(pci, state));
  1945. return 0;
  1946. }
  1947. static int azx_resume(struct pci_dev *pci)
  1948. {
  1949. struct snd_card *card = pci_get_drvdata(pci);
  1950. struct azx *chip = card->private_data;
  1951. pci_set_power_state(pci, PCI_D0);
  1952. pci_restore_state(pci);
  1953. if (pci_enable_device(pci) < 0) {
  1954. printk(KERN_ERR "hda-intel: pci_enable_device failed, "
  1955. "disabling device\n");
  1956. snd_card_disconnect(card);
  1957. return -EIO;
  1958. }
  1959. pci_set_master(pci);
  1960. if (chip->msi)
  1961. if (pci_enable_msi(pci) < 0)
  1962. chip->msi = 0;
  1963. if (azx_acquire_irq(chip, 1) < 0)
  1964. return -EIO;
  1965. azx_init_pci(chip);
  1966. if (snd_hda_codecs_inuse(chip->bus))
  1967. azx_init_chip(chip, 1);
  1968. snd_hda_resume(chip->bus);
  1969. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1970. return 0;
  1971. }
  1972. #endif /* CONFIG_PM */
  1973. /*
  1974. * reboot notifier for hang-up problem at power-down
  1975. */
  1976. static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
  1977. {
  1978. struct azx *chip = container_of(nb, struct azx, reboot_notifier);
  1979. snd_hda_bus_reboot_notify(chip->bus);
  1980. azx_stop_chip(chip);
  1981. return NOTIFY_OK;
  1982. }
  1983. static void azx_notifier_register(struct azx *chip)
  1984. {
  1985. chip->reboot_notifier.notifier_call = azx_halt;
  1986. register_reboot_notifier(&chip->reboot_notifier);
  1987. }
  1988. static void azx_notifier_unregister(struct azx *chip)
  1989. {
  1990. if (chip->reboot_notifier.notifier_call)
  1991. unregister_reboot_notifier(&chip->reboot_notifier);
  1992. }
  1993. /*
  1994. * destructor
  1995. */
  1996. static int azx_free(struct azx *chip)
  1997. {
  1998. int i;
  1999. azx_notifier_unregister(chip);
  2000. if (chip->initialized) {
  2001. azx_clear_irq_pending(chip);
  2002. for (i = 0; i < chip->num_streams; i++)
  2003. azx_stream_stop(chip, &chip->azx_dev[i]);
  2004. azx_stop_chip(chip);
  2005. }
  2006. if (chip->irq >= 0)
  2007. free_irq(chip->irq, (void*)chip);
  2008. if (chip->msi)
  2009. pci_disable_msi(chip->pci);
  2010. if (chip->remap_addr)
  2011. iounmap(chip->remap_addr);
  2012. if (chip->azx_dev) {
  2013. for (i = 0; i < chip->num_streams; i++)
  2014. if (chip->azx_dev[i].bdl.area)
  2015. snd_dma_free_pages(&chip->azx_dev[i].bdl);
  2016. }
  2017. if (chip->rb.area)
  2018. snd_dma_free_pages(&chip->rb);
  2019. if (chip->posbuf.area)
  2020. snd_dma_free_pages(&chip->posbuf);
  2021. pci_release_regions(chip->pci);
  2022. pci_disable_device(chip->pci);
  2023. kfree(chip->azx_dev);
  2024. kfree(chip);
  2025. return 0;
  2026. }
  2027. static int azx_dev_free(struct snd_device *device)
  2028. {
  2029. return azx_free(device->device_data);
  2030. }
  2031. /*
  2032. * white/black-listing for position_fix
  2033. */
  2034. static struct snd_pci_quirk position_fix_list[] __devinitdata = {
  2035. SND_PCI_QUIRK(0x1025, 0x009f, "Acer Aspire 5110", POS_FIX_LPIB),
  2036. SND_PCI_QUIRK(0x1025, 0x026f, "Acer Aspire 5538", POS_FIX_LPIB),
  2037. SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
  2038. SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
  2039. SND_PCI_QUIRK(0x1028, 0x01f6, "Dell Latitude 131L", POS_FIX_LPIB),
  2040. SND_PCI_QUIRK(0x1028, 0x0470, "Dell Inspiron 1120", POS_FIX_LPIB),
  2041. SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
  2042. SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
  2043. SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS", POS_FIX_LPIB),
  2044. SND_PCI_QUIRK(0x1043, 0x81e7, "ASUS M2V", POS_FIX_LPIB),
  2045. SND_PCI_QUIRK(0x1043, 0x8410, "ASUS", POS_FIX_LPIB),
  2046. SND_PCI_QUIRK(0x104d, 0x9069, "Sony VPCS11V9E", POS_FIX_LPIB),
  2047. SND_PCI_QUIRK(0x1106, 0x3288, "ASUS M2V-MX SE", POS_FIX_LPIB),
  2048. SND_PCI_QUIRK(0x1179, 0xff10, "Toshiba A100-259", POS_FIX_LPIB),
  2049. SND_PCI_QUIRK(0x1297, 0x3166, "Shuttle", POS_FIX_LPIB),
  2050. SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
  2051. SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
  2052. SND_PCI_QUIRK(0x1565, 0x820f, "Biostar Microtech", POS_FIX_LPIB),
  2053. SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
  2054. SND_PCI_QUIRK(0x1849, 0x0888, "775Dual-VSTA", POS_FIX_LPIB),
  2055. SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
  2056. SND_PCI_QUIRK(0x8086, 0xd601, "eMachines T5212", POS_FIX_LPIB),
  2057. {}
  2058. };
  2059. static int __devinit check_position_fix(struct azx *chip, int fix)
  2060. {
  2061. const struct snd_pci_quirk *q;
  2062. switch (fix) {
  2063. case POS_FIX_LPIB:
  2064. case POS_FIX_POSBUF:
  2065. case POS_FIX_VIACOMBO:
  2066. return fix;
  2067. }
  2068. q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
  2069. if (q) {
  2070. printk(KERN_INFO
  2071. "hda_intel: position_fix set to %d "
  2072. "for device %04x:%04x\n",
  2073. q->value, q->subvendor, q->subdevice);
  2074. return q->value;
  2075. }
  2076. /* Check VIA/ATI HD Audio Controller exist */
  2077. switch (chip->driver_type) {
  2078. case AZX_DRIVER_VIA:
  2079. /* Use link position directly, avoid any transfer problem. */
  2080. return POS_FIX_VIACOMBO;
  2081. case AZX_DRIVER_ATI:
  2082. /* ATI chipsets don't work well with position-buffer */
  2083. return POS_FIX_LPIB;
  2084. case AZX_DRIVER_GENERIC:
  2085. /* AMD chipsets also don't work with position-buffer */
  2086. if (chip->pci->vendor == PCI_VENDOR_ID_AMD)
  2087. return POS_FIX_LPIB;
  2088. break;
  2089. }
  2090. return POS_FIX_AUTO;
  2091. }
  2092. /*
  2093. * black-lists for probe_mask
  2094. */
  2095. static struct snd_pci_quirk probe_mask_list[] __devinitdata = {
  2096. /* Thinkpad often breaks the controller communication when accessing
  2097. * to the non-working (or non-existing) modem codec slot.
  2098. */
  2099. SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
  2100. SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
  2101. SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
  2102. /* broken BIOS */
  2103. SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
  2104. /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
  2105. SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
  2106. /* forced codec slots */
  2107. SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
  2108. SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
  2109. {}
  2110. };
  2111. #define AZX_FORCE_CODEC_MASK 0x100
  2112. static void __devinit check_probe_mask(struct azx *chip, int dev)
  2113. {
  2114. const struct snd_pci_quirk *q;
  2115. chip->codec_probe_mask = probe_mask[dev];
  2116. if (chip->codec_probe_mask == -1) {
  2117. q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
  2118. if (q) {
  2119. printk(KERN_INFO
  2120. "hda_intel: probe_mask set to 0x%x "
  2121. "for device %04x:%04x\n",
  2122. q->value, q->subvendor, q->subdevice);
  2123. chip->codec_probe_mask = q->value;
  2124. }
  2125. }
  2126. /* check forced option */
  2127. if (chip->codec_probe_mask != -1 &&
  2128. (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
  2129. chip->codec_mask = chip->codec_probe_mask & 0xff;
  2130. printk(KERN_INFO "hda_intel: codec_mask forced to 0x%x\n",
  2131. chip->codec_mask);
  2132. }
  2133. }
  2134. /*
  2135. * white/black-list for enable_msi
  2136. */
  2137. static struct snd_pci_quirk msi_black_list[] __devinitdata = {
  2138. SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
  2139. SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
  2140. SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
  2141. SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
  2142. SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
  2143. {}
  2144. };
  2145. static void __devinit check_msi(struct azx *chip)
  2146. {
  2147. const struct snd_pci_quirk *q;
  2148. if (enable_msi >= 0) {
  2149. chip->msi = !!enable_msi;
  2150. return;
  2151. }
  2152. chip->msi = 1; /* enable MSI as default */
  2153. q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
  2154. if (q) {
  2155. printk(KERN_INFO
  2156. "hda_intel: msi for device %04x:%04x set to %d\n",
  2157. q->subvendor, q->subdevice, q->value);
  2158. chip->msi = q->value;
  2159. return;
  2160. }
  2161. /* NVidia chipsets seem to cause troubles with MSI */
  2162. if (chip->driver_type == AZX_DRIVER_NVIDIA) {
  2163. printk(KERN_INFO "hda_intel: Disable MSI for Nvidia chipset\n");
  2164. chip->msi = 0;
  2165. }
  2166. }
  2167. /*
  2168. * constructor
  2169. */
  2170. static int __devinit azx_create(struct snd_card *card, struct pci_dev *pci,
  2171. int dev, int driver_type,
  2172. struct azx **rchip)
  2173. {
  2174. struct azx *chip;
  2175. int i, err;
  2176. unsigned short gcap;
  2177. static struct snd_device_ops ops = {
  2178. .dev_free = azx_dev_free,
  2179. };
  2180. *rchip = NULL;
  2181. err = pci_enable_device(pci);
  2182. if (err < 0)
  2183. return err;
  2184. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  2185. if (!chip) {
  2186. snd_printk(KERN_ERR SFX "cannot allocate chip\n");
  2187. pci_disable_device(pci);
  2188. return -ENOMEM;
  2189. }
  2190. spin_lock_init(&chip->reg_lock);
  2191. mutex_init(&chip->open_mutex);
  2192. chip->card = card;
  2193. chip->pci = pci;
  2194. chip->irq = -1;
  2195. chip->driver_type = driver_type;
  2196. check_msi(chip);
  2197. chip->dev_index = dev;
  2198. INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
  2199. chip->position_fix[0] = chip->position_fix[1] =
  2200. check_position_fix(chip, position_fix[dev]);
  2201. check_probe_mask(chip, dev);
  2202. chip->single_cmd = single_cmd;
  2203. if (bdl_pos_adj[dev] < 0) {
  2204. switch (chip->driver_type) {
  2205. case AZX_DRIVER_ICH:
  2206. case AZX_DRIVER_PCH:
  2207. bdl_pos_adj[dev] = 1;
  2208. break;
  2209. default:
  2210. bdl_pos_adj[dev] = 32;
  2211. break;
  2212. }
  2213. }
  2214. #if BITS_PER_LONG != 64
  2215. /* Fix up base address on ULI M5461 */
  2216. if (chip->driver_type == AZX_DRIVER_ULI) {
  2217. u16 tmp3;
  2218. pci_read_config_word(pci, 0x40, &tmp3);
  2219. pci_write_config_word(pci, 0x40, tmp3 | 0x10);
  2220. pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
  2221. }
  2222. #endif
  2223. err = pci_request_regions(pci, "ICH HD audio");
  2224. if (err < 0) {
  2225. kfree(chip);
  2226. pci_disable_device(pci);
  2227. return err;
  2228. }
  2229. chip->addr = pci_resource_start(pci, 0);
  2230. chip->remap_addr = pci_ioremap_bar(pci, 0);
  2231. if (chip->remap_addr == NULL) {
  2232. snd_printk(KERN_ERR SFX "ioremap error\n");
  2233. err = -ENXIO;
  2234. goto errout;
  2235. }
  2236. if (chip->msi)
  2237. if (pci_enable_msi(pci) < 0)
  2238. chip->msi = 0;
  2239. if (azx_acquire_irq(chip, 0) < 0) {
  2240. err = -EBUSY;
  2241. goto errout;
  2242. }
  2243. pci_set_master(pci);
  2244. synchronize_irq(chip->irq);
  2245. gcap = azx_readw(chip, GCAP);
  2246. snd_printdd(SFX "chipset global capabilities = 0x%x\n", gcap);
  2247. /* disable SB600 64bit support for safety */
  2248. if ((chip->driver_type == AZX_DRIVER_ATI) ||
  2249. (chip->driver_type == AZX_DRIVER_ATIHDMI)) {
  2250. struct pci_dev *p_smbus;
  2251. p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
  2252. PCI_DEVICE_ID_ATI_SBX00_SMBUS,
  2253. NULL);
  2254. if (p_smbus) {
  2255. if (p_smbus->revision < 0x30)
  2256. gcap &= ~ICH6_GCAP_64OK;
  2257. pci_dev_put(p_smbus);
  2258. }
  2259. } else {
  2260. /* FIXME: not sure whether this is really needed, but
  2261. * Hudson isn't stable enough for allowing everything...
  2262. * let's check later again.
  2263. */
  2264. if (chip->pci->vendor == PCI_VENDOR_ID_AMD)
  2265. gcap &= ~ICH6_GCAP_64OK;
  2266. }
  2267. /* disable 64bit DMA address for Teradici */
  2268. /* it does not work with device 6549:1200 subsys e4a2:040b */
  2269. if (chip->driver_type == AZX_DRIVER_TERA)
  2270. gcap &= ~ICH6_GCAP_64OK;
  2271. /* allow 64bit DMA address if supported by H/W */
  2272. if ((gcap & ICH6_GCAP_64OK) && !pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
  2273. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
  2274. else {
  2275. pci_set_dma_mask(pci, DMA_BIT_MASK(32));
  2276. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
  2277. }
  2278. /* read number of streams from GCAP register instead of using
  2279. * hardcoded value
  2280. */
  2281. chip->capture_streams = (gcap >> 8) & 0x0f;
  2282. chip->playback_streams = (gcap >> 12) & 0x0f;
  2283. if (!chip->playback_streams && !chip->capture_streams) {
  2284. /* gcap didn't give any info, switching to old method */
  2285. switch (chip->driver_type) {
  2286. case AZX_DRIVER_ULI:
  2287. chip->playback_streams = ULI_NUM_PLAYBACK;
  2288. chip->capture_streams = ULI_NUM_CAPTURE;
  2289. break;
  2290. case AZX_DRIVER_ATIHDMI:
  2291. chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
  2292. chip->capture_streams = ATIHDMI_NUM_CAPTURE;
  2293. break;
  2294. case AZX_DRIVER_GENERIC:
  2295. default:
  2296. chip->playback_streams = ICH6_NUM_PLAYBACK;
  2297. chip->capture_streams = ICH6_NUM_CAPTURE;
  2298. break;
  2299. }
  2300. }
  2301. chip->capture_index_offset = 0;
  2302. chip->playback_index_offset = chip->capture_streams;
  2303. chip->num_streams = chip->playback_streams + chip->capture_streams;
  2304. chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
  2305. GFP_KERNEL);
  2306. if (!chip->azx_dev) {
  2307. snd_printk(KERN_ERR SFX "cannot malloc azx_dev\n");
  2308. goto errout;
  2309. }
  2310. for (i = 0; i < chip->num_streams; i++) {
  2311. /* allocate memory for the BDL for each stream */
  2312. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  2313. snd_dma_pci_data(chip->pci),
  2314. BDL_SIZE, &chip->azx_dev[i].bdl);
  2315. if (err < 0) {
  2316. snd_printk(KERN_ERR SFX "cannot allocate BDL\n");
  2317. goto errout;
  2318. }
  2319. }
  2320. /* allocate memory for the position buffer */
  2321. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  2322. snd_dma_pci_data(chip->pci),
  2323. chip->num_streams * 8, &chip->posbuf);
  2324. if (err < 0) {
  2325. snd_printk(KERN_ERR SFX "cannot allocate posbuf\n");
  2326. goto errout;
  2327. }
  2328. /* allocate CORB/RIRB */
  2329. err = azx_alloc_cmd_io(chip);
  2330. if (err < 0)
  2331. goto errout;
  2332. /* initialize streams */
  2333. azx_init_stream(chip);
  2334. /* initialize chip */
  2335. azx_init_pci(chip);
  2336. azx_init_chip(chip, (probe_only[dev] & 2) == 0);
  2337. /* codec detection */
  2338. if (!chip->codec_mask) {
  2339. snd_printk(KERN_ERR SFX "no codecs found!\n");
  2340. err = -ENODEV;
  2341. goto errout;
  2342. }
  2343. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  2344. if (err <0) {
  2345. snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
  2346. goto errout;
  2347. }
  2348. strcpy(card->driver, "HDA-Intel");
  2349. strlcpy(card->shortname, driver_short_names[chip->driver_type],
  2350. sizeof(card->shortname));
  2351. snprintf(card->longname, sizeof(card->longname),
  2352. "%s at 0x%lx irq %i",
  2353. card->shortname, chip->addr, chip->irq);
  2354. *rchip = chip;
  2355. return 0;
  2356. errout:
  2357. azx_free(chip);
  2358. return err;
  2359. }
  2360. static void power_down_all_codecs(struct azx *chip)
  2361. {
  2362. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2363. /* The codecs were powered up in snd_hda_codec_new().
  2364. * Now all initialization done, so turn them down if possible
  2365. */
  2366. struct hda_codec *codec;
  2367. list_for_each_entry(codec, &chip->bus->codec_list, list) {
  2368. snd_hda_power_down(codec);
  2369. }
  2370. #endif
  2371. }
  2372. static int __devinit azx_probe(struct pci_dev *pci,
  2373. const struct pci_device_id *pci_id)
  2374. {
  2375. static int dev;
  2376. struct snd_card *card;
  2377. struct azx *chip;
  2378. int err;
  2379. if (dev >= SNDRV_CARDS)
  2380. return -ENODEV;
  2381. if (!enable[dev]) {
  2382. dev++;
  2383. return -ENOENT;
  2384. }
  2385. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  2386. if (err < 0) {
  2387. snd_printk(KERN_ERR SFX "Error creating card!\n");
  2388. return err;
  2389. }
  2390. /* set this here since it's referred in snd_hda_load_patch() */
  2391. snd_card_set_dev(card, &pci->dev);
  2392. err = azx_create(card, pci, dev, pci_id->driver_data, &chip);
  2393. if (err < 0)
  2394. goto out_free;
  2395. card->private_data = chip;
  2396. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  2397. chip->beep_mode = beep_mode[dev];
  2398. #endif
  2399. /* create codec instances */
  2400. err = azx_codec_create(chip, model[dev]);
  2401. if (err < 0)
  2402. goto out_free;
  2403. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  2404. if (patch[dev] && *patch[dev]) {
  2405. snd_printk(KERN_ERR SFX "Applying patch firmware '%s'\n",
  2406. patch[dev]);
  2407. err = snd_hda_load_patch(chip->bus, patch[dev]);
  2408. if (err < 0)
  2409. goto out_free;
  2410. }
  2411. #endif
  2412. if ((probe_only[dev] & 1) == 0) {
  2413. err = azx_codec_configure(chip);
  2414. if (err < 0)
  2415. goto out_free;
  2416. }
  2417. /* create PCM streams */
  2418. err = snd_hda_build_pcms(chip->bus);
  2419. if (err < 0)
  2420. goto out_free;
  2421. /* create mixer controls */
  2422. err = azx_mixer_create(chip);
  2423. if (err < 0)
  2424. goto out_free;
  2425. err = snd_card_register(card);
  2426. if (err < 0)
  2427. goto out_free;
  2428. pci_set_drvdata(pci, card);
  2429. chip->running = 1;
  2430. power_down_all_codecs(chip);
  2431. azx_notifier_register(chip);
  2432. dev++;
  2433. return err;
  2434. out_free:
  2435. snd_card_free(card);
  2436. return err;
  2437. }
  2438. static void __devexit azx_remove(struct pci_dev *pci)
  2439. {
  2440. snd_card_free(pci_get_drvdata(pci));
  2441. pci_set_drvdata(pci, NULL);
  2442. }
  2443. /* PCI IDs */
  2444. static DEFINE_PCI_DEVICE_TABLE(azx_ids) = {
  2445. /* CPT */
  2446. { PCI_DEVICE(0x8086, 0x1c20), .driver_data = AZX_DRIVER_PCH },
  2447. /* PBG */
  2448. { PCI_DEVICE(0x8086, 0x1d20), .driver_data = AZX_DRIVER_PCH },
  2449. /* Panther Point */
  2450. { PCI_DEVICE(0x8086, 0x1e20), .driver_data = AZX_DRIVER_PCH },
  2451. /* SCH */
  2452. { PCI_DEVICE(0x8086, 0x811b), .driver_data = AZX_DRIVER_SCH },
  2453. /* Generic Intel */
  2454. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
  2455. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2456. .class_mask = 0xffffff,
  2457. .driver_data = AZX_DRIVER_ICH },
  2458. /* ATI SB 450/600 */
  2459. { PCI_DEVICE(0x1002, 0x437b), .driver_data = AZX_DRIVER_ATI },
  2460. { PCI_DEVICE(0x1002, 0x4383), .driver_data = AZX_DRIVER_ATI },
  2461. /* ATI HDMI */
  2462. { PCI_DEVICE(0x1002, 0x793b), .driver_data = AZX_DRIVER_ATIHDMI },
  2463. { PCI_DEVICE(0x1002, 0x7919), .driver_data = AZX_DRIVER_ATIHDMI },
  2464. { PCI_DEVICE(0x1002, 0x960f), .driver_data = AZX_DRIVER_ATIHDMI },
  2465. { PCI_DEVICE(0x1002, 0x970f), .driver_data = AZX_DRIVER_ATIHDMI },
  2466. { PCI_DEVICE(0x1002, 0xaa00), .driver_data = AZX_DRIVER_ATIHDMI },
  2467. { PCI_DEVICE(0x1002, 0xaa08), .driver_data = AZX_DRIVER_ATIHDMI },
  2468. { PCI_DEVICE(0x1002, 0xaa10), .driver_data = AZX_DRIVER_ATIHDMI },
  2469. { PCI_DEVICE(0x1002, 0xaa18), .driver_data = AZX_DRIVER_ATIHDMI },
  2470. { PCI_DEVICE(0x1002, 0xaa20), .driver_data = AZX_DRIVER_ATIHDMI },
  2471. { PCI_DEVICE(0x1002, 0xaa28), .driver_data = AZX_DRIVER_ATIHDMI },
  2472. { PCI_DEVICE(0x1002, 0xaa30), .driver_data = AZX_DRIVER_ATIHDMI },
  2473. { PCI_DEVICE(0x1002, 0xaa38), .driver_data = AZX_DRIVER_ATIHDMI },
  2474. { PCI_DEVICE(0x1002, 0xaa40), .driver_data = AZX_DRIVER_ATIHDMI },
  2475. { PCI_DEVICE(0x1002, 0xaa48), .driver_data = AZX_DRIVER_ATIHDMI },
  2476. /* VIA VT8251/VT8237A */
  2477. { PCI_DEVICE(0x1106, 0x3288), .driver_data = AZX_DRIVER_VIA },
  2478. /* SIS966 */
  2479. { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
  2480. /* ULI M5461 */
  2481. { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
  2482. /* NVIDIA MCP */
  2483. { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
  2484. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2485. .class_mask = 0xffffff,
  2486. .driver_data = AZX_DRIVER_NVIDIA },
  2487. /* Teradici */
  2488. { PCI_DEVICE(0x6549, 0x1200), .driver_data = AZX_DRIVER_TERA },
  2489. /* Creative X-Fi (CA0110-IBG) */
  2490. #if !defined(CONFIG_SND_CTXFI) && !defined(CONFIG_SND_CTXFI_MODULE)
  2491. /* the following entry conflicts with snd-ctxfi driver,
  2492. * as ctxfi driver mutates from HD-audio to native mode with
  2493. * a special command sequence.
  2494. */
  2495. { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
  2496. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2497. .class_mask = 0xffffff,
  2498. .driver_data = AZX_DRIVER_CTX },
  2499. #else
  2500. /* this entry seems still valid -- i.e. without emu20kx chip */
  2501. { PCI_DEVICE(0x1102, 0x0009), .driver_data = AZX_DRIVER_CTX },
  2502. #endif
  2503. /* Vortex86MX */
  2504. { PCI_DEVICE(0x17f3, 0x3010), .driver_data = AZX_DRIVER_GENERIC },
  2505. /* VMware HDAudio */
  2506. { PCI_DEVICE(0x15ad, 0x1977), .driver_data = AZX_DRIVER_GENERIC },
  2507. /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
  2508. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
  2509. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2510. .class_mask = 0xffffff,
  2511. .driver_data = AZX_DRIVER_GENERIC },
  2512. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
  2513. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2514. .class_mask = 0xffffff,
  2515. .driver_data = AZX_DRIVER_GENERIC },
  2516. { 0, }
  2517. };
  2518. MODULE_DEVICE_TABLE(pci, azx_ids);
  2519. /* pci_driver definition */
  2520. static struct pci_driver driver = {
  2521. .name = "HDA Intel",
  2522. .id_table = azx_ids,
  2523. .probe = azx_probe,
  2524. .remove = __devexit_p(azx_remove),
  2525. #ifdef CONFIG_PM
  2526. .suspend = azx_suspend,
  2527. .resume = azx_resume,
  2528. #endif
  2529. };
  2530. static int __init alsa_card_azx_init(void)
  2531. {
  2532. return pci_register_driver(&driver);
  2533. }
  2534. static void __exit alsa_card_azx_exit(void)
  2535. {
  2536. pci_unregister_driver(&driver);
  2537. }
  2538. module_init(alsa_card_azx_init)
  2539. module_exit(alsa_card_azx_exit)