hda_intel.c 67 KB

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