hda_intel.c 69 KB

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