hda_intel.c 80 KB

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