hda_intel.c 68 KB

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