hda_intel.c 70 KB

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