hda_intel.c 71 KB

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