hda_intel.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  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/moduleparam.h>
  42. #include <linux/init.h>
  43. #include <linux/slab.h>
  44. #include <linux/pci.h>
  45. #include <linux/mutex.h>
  46. #include <sound/core.h>
  47. #include <sound/initval.h>
  48. #include "hda_codec.h"
  49. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  50. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  51. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  52. static char *model[SNDRV_CARDS];
  53. static int position_fix[SNDRV_CARDS];
  54. static int probe_mask[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
  55. static int single_cmd;
  56. static int enable_msi;
  57. module_param_array(index, int, NULL, 0444);
  58. MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
  59. module_param_array(id, charp, NULL, 0444);
  60. MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
  61. module_param_array(enable, bool, NULL, 0444);
  62. MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
  63. module_param_array(model, charp, NULL, 0444);
  64. MODULE_PARM_DESC(model, "Use the given board model.");
  65. module_param_array(position_fix, int, NULL, 0444);
  66. MODULE_PARM_DESC(position_fix, "Fix DMA pointer "
  67. "(0 = auto, 1 = none, 2 = POSBUF, 3 = FIFO size).");
  68. module_param_array(probe_mask, int, NULL, 0444);
  69. MODULE_PARM_DESC(probe_mask, "Bitmask to probe codecs (default = -1).");
  70. module_param(single_cmd, bool, 0444);
  71. MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
  72. "(for debugging only).");
  73. module_param(enable_msi, int, 0444);
  74. MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
  75. #ifdef CONFIG_SND_HDA_POWER_SAVE
  76. /* power_save option is defined in hda_codec.c */
  77. /* reset the HD-audio controller in power save mode.
  78. * this may give more power-saving, but will take longer time to
  79. * wake up.
  80. */
  81. static int power_save_controller = 1;
  82. module_param(power_save_controller, bool, 0644);
  83. MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
  84. #endif
  85. MODULE_LICENSE("GPL");
  86. MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
  87. "{Intel, ICH6M},"
  88. "{Intel, ICH7},"
  89. "{Intel, ESB2},"
  90. "{Intel, ICH8},"
  91. "{Intel, ICH9},"
  92. "{Intel, ICH10},"
  93. "{Intel, SCH},"
  94. "{ATI, SB450},"
  95. "{ATI, SB600},"
  96. "{ATI, RS600},"
  97. "{ATI, RS690},"
  98. "{ATI, RS780},"
  99. "{ATI, R600},"
  100. "{ATI, RV630},"
  101. "{ATI, RV610},"
  102. "{ATI, RV670},"
  103. "{ATI, RV635},"
  104. "{ATI, RV620},"
  105. "{ATI, RV770},"
  106. "{VIA, VT8251},"
  107. "{VIA, VT8237A},"
  108. "{SiS, SIS966},"
  109. "{ULI, M5461}}");
  110. MODULE_DESCRIPTION("Intel HDA driver");
  111. #define SFX "hda-intel: "
  112. /*
  113. * registers
  114. */
  115. #define ICH6_REG_GCAP 0x00
  116. #define ICH6_REG_VMIN 0x02
  117. #define ICH6_REG_VMAJ 0x03
  118. #define ICH6_REG_OUTPAY 0x04
  119. #define ICH6_REG_INPAY 0x06
  120. #define ICH6_REG_GCTL 0x08
  121. #define ICH6_REG_WAKEEN 0x0c
  122. #define ICH6_REG_STATESTS 0x0e
  123. #define ICH6_REG_GSTS 0x10
  124. #define ICH6_REG_INTCTL 0x20
  125. #define ICH6_REG_INTSTS 0x24
  126. #define ICH6_REG_WALCLK 0x30
  127. #define ICH6_REG_SYNC 0x34
  128. #define ICH6_REG_CORBLBASE 0x40
  129. #define ICH6_REG_CORBUBASE 0x44
  130. #define ICH6_REG_CORBWP 0x48
  131. #define ICH6_REG_CORBRP 0x4A
  132. #define ICH6_REG_CORBCTL 0x4c
  133. #define ICH6_REG_CORBSTS 0x4d
  134. #define ICH6_REG_CORBSIZE 0x4e
  135. #define ICH6_REG_RIRBLBASE 0x50
  136. #define ICH6_REG_RIRBUBASE 0x54
  137. #define ICH6_REG_RIRBWP 0x58
  138. #define ICH6_REG_RINTCNT 0x5a
  139. #define ICH6_REG_RIRBCTL 0x5c
  140. #define ICH6_REG_RIRBSTS 0x5d
  141. #define ICH6_REG_RIRBSIZE 0x5e
  142. #define ICH6_REG_IC 0x60
  143. #define ICH6_REG_IR 0x64
  144. #define ICH6_REG_IRS 0x68
  145. #define ICH6_IRS_VALID (1<<1)
  146. #define ICH6_IRS_BUSY (1<<0)
  147. #define ICH6_REG_DPLBASE 0x70
  148. #define ICH6_REG_DPUBASE 0x74
  149. #define ICH6_DPLBASE_ENABLE 0x1 /* Enable position buffer */
  150. /* SD offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  151. enum { SDI0, SDI1, SDI2, SDI3, SDO0, SDO1, SDO2, SDO3 };
  152. /* stream register offsets from stream base */
  153. #define ICH6_REG_SD_CTL 0x00
  154. #define ICH6_REG_SD_STS 0x03
  155. #define ICH6_REG_SD_LPIB 0x04
  156. #define ICH6_REG_SD_CBL 0x08
  157. #define ICH6_REG_SD_LVI 0x0c
  158. #define ICH6_REG_SD_FIFOW 0x0e
  159. #define ICH6_REG_SD_FIFOSIZE 0x10
  160. #define ICH6_REG_SD_FORMAT 0x12
  161. #define ICH6_REG_SD_BDLPL 0x18
  162. #define ICH6_REG_SD_BDLPU 0x1c
  163. /* PCI space */
  164. #define ICH6_PCIREG_TCSEL 0x44
  165. /*
  166. * other constants
  167. */
  168. /* max number of SDs */
  169. /* ICH, ATI and VIA have 4 playback and 4 capture */
  170. #define ICH6_NUM_CAPTURE 4
  171. #define ICH6_NUM_PLAYBACK 4
  172. /* ULI has 6 playback and 5 capture */
  173. #define ULI_NUM_CAPTURE 5
  174. #define ULI_NUM_PLAYBACK 6
  175. /* ATI HDMI has 1 playback and 0 capture */
  176. #define ATIHDMI_NUM_CAPTURE 0
  177. #define ATIHDMI_NUM_PLAYBACK 1
  178. /* this number is statically defined for simplicity */
  179. #define MAX_AZX_DEV 16
  180. /* max number of fragments - we may use more if allocating more pages for BDL */
  181. #define BDL_SIZE 4096
  182. #define AZX_MAX_BDL_ENTRIES (BDL_SIZE / 16)
  183. #define AZX_MAX_FRAG 32
  184. /* max buffer size - no h/w limit, you can increase as you like */
  185. #define AZX_MAX_BUF_SIZE (1024*1024*1024)
  186. /* max number of PCM devics per card */
  187. #define AZX_MAX_PCMS 8
  188. /* RIRB int mask: overrun[2], response[0] */
  189. #define RIRB_INT_RESPONSE 0x01
  190. #define RIRB_INT_OVERRUN 0x04
  191. #define RIRB_INT_MASK 0x05
  192. /* STATESTS int mask: SD2,SD1,SD0 */
  193. #define AZX_MAX_CODECS 3
  194. #define STATESTS_INT_MASK 0x07
  195. /* SD_CTL bits */
  196. #define SD_CTL_STREAM_RESET 0x01 /* stream reset bit */
  197. #define SD_CTL_DMA_START 0x02 /* stream DMA start bit */
  198. #define SD_CTL_STREAM_TAG_MASK (0xf << 20)
  199. #define SD_CTL_STREAM_TAG_SHIFT 20
  200. /* SD_CTL and SD_STS */
  201. #define SD_INT_DESC_ERR 0x10 /* descriptor error interrupt */
  202. #define SD_INT_FIFO_ERR 0x08 /* FIFO error interrupt */
  203. #define SD_INT_COMPLETE 0x04 /* completion interrupt */
  204. #define SD_INT_MASK (SD_INT_DESC_ERR|SD_INT_FIFO_ERR|\
  205. SD_INT_COMPLETE)
  206. /* SD_STS */
  207. #define SD_STS_FIFO_READY 0x20 /* FIFO ready */
  208. /* INTCTL and INTSTS */
  209. #define ICH6_INT_ALL_STREAM 0xff /* all stream interrupts */
  210. #define ICH6_INT_CTRL_EN 0x40000000 /* controller interrupt enable bit */
  211. #define ICH6_INT_GLOBAL_EN 0x80000000 /* global interrupt enable bit */
  212. /* GCTL unsolicited response enable bit */
  213. #define ICH6_GCTL_UREN (1<<8)
  214. /* GCTL reset bit */
  215. #define ICH6_GCTL_RESET (1<<0)
  216. /* CORB/RIRB control, read/write pointer */
  217. #define ICH6_RBCTL_DMA_EN 0x02 /* enable DMA */
  218. #define ICH6_RBCTL_IRQ_EN 0x01 /* enable IRQ */
  219. #define ICH6_RBRWP_CLR 0x8000 /* read/write pointer clear */
  220. /* below are so far hardcoded - should read registers in future */
  221. #define ICH6_MAX_CORB_ENTRIES 256
  222. #define ICH6_MAX_RIRB_ENTRIES 256
  223. /* position fix mode */
  224. enum {
  225. POS_FIX_AUTO,
  226. POS_FIX_NONE,
  227. POS_FIX_POSBUF,
  228. POS_FIX_FIFO,
  229. };
  230. /* Defines for ATI HD Audio support in SB450 south bridge */
  231. #define ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR 0x42
  232. #define ATI_SB450_HDAUDIO_ENABLE_SNOOP 0x02
  233. /* Defines for Nvidia HDA support */
  234. #define NVIDIA_HDA_TRANSREG_ADDR 0x4e
  235. #define NVIDIA_HDA_ENABLE_COHBITS 0x0f
  236. /* Defines for Intel SCH HDA snoop control */
  237. #define INTEL_SCH_HDA_DEVC 0x78
  238. #define INTEL_SCH_HDA_DEVC_NOSNOOP (0x1<<11)
  239. /*
  240. */
  241. struct azx_dev {
  242. struct snd_dma_buffer bdl; /* BDL buffer */
  243. u32 *posbuf; /* position buffer pointer */
  244. unsigned int bufsize; /* size of the play buffer in bytes */
  245. unsigned int frags; /* number for period in the play buffer */
  246. unsigned int fifo_size; /* FIFO size */
  247. void __iomem *sd_addr; /* stream descriptor pointer */
  248. u32 sd_int_sta_mask; /* stream int status mask */
  249. /* pcm support */
  250. struct snd_pcm_substream *substream; /* assigned substream,
  251. * set in PCM open
  252. */
  253. unsigned int format_val; /* format value to be set in the
  254. * controller and the codec
  255. */
  256. unsigned char stream_tag; /* assigned stream */
  257. unsigned char index; /* stream index */
  258. /* for sanity check of position buffer */
  259. unsigned int period_intr;
  260. unsigned int opened :1;
  261. unsigned int running :1;
  262. };
  263. /* CORB/RIRB */
  264. struct azx_rb {
  265. u32 *buf; /* CORB/RIRB buffer
  266. * Each CORB entry is 4byte, RIRB is 8byte
  267. */
  268. dma_addr_t addr; /* physical address of CORB/RIRB buffer */
  269. /* for RIRB */
  270. unsigned short rp, wp; /* read/write pointers */
  271. int cmds; /* number of pending requests */
  272. u32 res; /* last read value */
  273. };
  274. struct azx {
  275. struct snd_card *card;
  276. struct pci_dev *pci;
  277. /* chip type specific */
  278. int driver_type;
  279. int playback_streams;
  280. int playback_index_offset;
  281. int capture_streams;
  282. int capture_index_offset;
  283. int num_streams;
  284. /* pci resources */
  285. unsigned long addr;
  286. void __iomem *remap_addr;
  287. int irq;
  288. /* locks */
  289. spinlock_t reg_lock;
  290. struct mutex open_mutex;
  291. /* streams (x num_streams) */
  292. struct azx_dev *azx_dev;
  293. /* PCM */
  294. struct snd_pcm *pcm[AZX_MAX_PCMS];
  295. /* HD codec */
  296. unsigned short codec_mask;
  297. struct hda_bus *bus;
  298. /* CORB/RIRB */
  299. struct azx_rb corb;
  300. struct azx_rb rirb;
  301. /* CORB/RIRB and position buffers */
  302. struct snd_dma_buffer rb;
  303. struct snd_dma_buffer posbuf;
  304. /* flags */
  305. int position_fix;
  306. unsigned int running :1;
  307. unsigned int initialized :1;
  308. unsigned int single_cmd :1;
  309. unsigned int polling_mode :1;
  310. unsigned int msi :1;
  311. /* for debugging */
  312. unsigned int last_cmd; /* last issued command (to sync) */
  313. };
  314. /* driver types */
  315. enum {
  316. AZX_DRIVER_ICH,
  317. AZX_DRIVER_SCH,
  318. AZX_DRIVER_ATI,
  319. AZX_DRIVER_ATIHDMI,
  320. AZX_DRIVER_VIA,
  321. AZX_DRIVER_SIS,
  322. AZX_DRIVER_ULI,
  323. AZX_DRIVER_NVIDIA,
  324. };
  325. static char *driver_short_names[] __devinitdata = {
  326. [AZX_DRIVER_ICH] = "HDA Intel",
  327. [AZX_DRIVER_SCH] = "HDA Intel MID",
  328. [AZX_DRIVER_ATI] = "HDA ATI SB",
  329. [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
  330. [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
  331. [AZX_DRIVER_SIS] = "HDA SIS966",
  332. [AZX_DRIVER_ULI] = "HDA ULI M5461",
  333. [AZX_DRIVER_NVIDIA] = "HDA NVidia",
  334. };
  335. /*
  336. * macros for easy use
  337. */
  338. #define azx_writel(chip,reg,value) \
  339. writel(value, (chip)->remap_addr + ICH6_REG_##reg)
  340. #define azx_readl(chip,reg) \
  341. readl((chip)->remap_addr + ICH6_REG_##reg)
  342. #define azx_writew(chip,reg,value) \
  343. writew(value, (chip)->remap_addr + ICH6_REG_##reg)
  344. #define azx_readw(chip,reg) \
  345. readw((chip)->remap_addr + ICH6_REG_##reg)
  346. #define azx_writeb(chip,reg,value) \
  347. writeb(value, (chip)->remap_addr + ICH6_REG_##reg)
  348. #define azx_readb(chip,reg) \
  349. readb((chip)->remap_addr + ICH6_REG_##reg)
  350. #define azx_sd_writel(dev,reg,value) \
  351. writel(value, (dev)->sd_addr + ICH6_REG_##reg)
  352. #define azx_sd_readl(dev,reg) \
  353. readl((dev)->sd_addr + ICH6_REG_##reg)
  354. #define azx_sd_writew(dev,reg,value) \
  355. writew(value, (dev)->sd_addr + ICH6_REG_##reg)
  356. #define azx_sd_readw(dev,reg) \
  357. readw((dev)->sd_addr + ICH6_REG_##reg)
  358. #define azx_sd_writeb(dev,reg,value) \
  359. writeb(value, (dev)->sd_addr + ICH6_REG_##reg)
  360. #define azx_sd_readb(dev,reg) \
  361. readb((dev)->sd_addr + ICH6_REG_##reg)
  362. /* for pcm support */
  363. #define get_azx_dev(substream) (substream->runtime->private_data)
  364. /* Get the upper 32bit of the given dma_addr_t
  365. * Compiler should optimize and eliminate the code if dma_addr_t is 32bit
  366. */
  367. #define upper_32bit(addr) (sizeof(addr) > 4 ? (u32)((addr) >> 32) : (u32)0)
  368. static int azx_acquire_irq(struct azx *chip, int do_disconnect);
  369. /*
  370. * Interface for HD codec
  371. */
  372. /*
  373. * CORB / RIRB interface
  374. */
  375. static int azx_alloc_cmd_io(struct azx *chip)
  376. {
  377. int err;
  378. /* single page (at least 4096 bytes) must suffice for both ringbuffes */
  379. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  380. snd_dma_pci_data(chip->pci),
  381. PAGE_SIZE, &chip->rb);
  382. if (err < 0) {
  383. snd_printk(KERN_ERR SFX "cannot allocate CORB/RIRB\n");
  384. return err;
  385. }
  386. return 0;
  387. }
  388. static void azx_init_cmd_io(struct azx *chip)
  389. {
  390. /* CORB set up */
  391. chip->corb.addr = chip->rb.addr;
  392. chip->corb.buf = (u32 *)chip->rb.area;
  393. azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
  394. azx_writel(chip, CORBUBASE, upper_32bit(chip->corb.addr));
  395. /* set the corb size to 256 entries (ULI requires explicitly) */
  396. azx_writeb(chip, CORBSIZE, 0x02);
  397. /* set the corb write pointer to 0 */
  398. azx_writew(chip, CORBWP, 0);
  399. /* reset the corb hw read pointer */
  400. azx_writew(chip, CORBRP, ICH6_RBRWP_CLR);
  401. /* enable corb dma */
  402. azx_writeb(chip, CORBCTL, ICH6_RBCTL_DMA_EN);
  403. /* RIRB set up */
  404. chip->rirb.addr = chip->rb.addr + 2048;
  405. chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
  406. azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
  407. azx_writel(chip, RIRBUBASE, upper_32bit(chip->rirb.addr));
  408. /* set the rirb size to 256 entries (ULI requires explicitly) */
  409. azx_writeb(chip, RIRBSIZE, 0x02);
  410. /* reset the rirb hw write pointer */
  411. azx_writew(chip, RIRBWP, ICH6_RBRWP_CLR);
  412. /* set N=1, get RIRB response interrupt for new entry */
  413. azx_writew(chip, RINTCNT, 1);
  414. /* enable rirb dma and response irq */
  415. azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
  416. chip->rirb.rp = chip->rirb.cmds = 0;
  417. }
  418. static void azx_free_cmd_io(struct azx *chip)
  419. {
  420. /* disable ringbuffer DMAs */
  421. azx_writeb(chip, RIRBCTL, 0);
  422. azx_writeb(chip, CORBCTL, 0);
  423. }
  424. /* send a command */
  425. static int azx_corb_send_cmd(struct hda_codec *codec, u32 val)
  426. {
  427. struct azx *chip = codec->bus->private_data;
  428. unsigned int wp;
  429. /* add command to corb */
  430. wp = azx_readb(chip, CORBWP);
  431. wp++;
  432. wp %= ICH6_MAX_CORB_ENTRIES;
  433. spin_lock_irq(&chip->reg_lock);
  434. chip->rirb.cmds++;
  435. chip->corb.buf[wp] = cpu_to_le32(val);
  436. azx_writel(chip, CORBWP, wp);
  437. spin_unlock_irq(&chip->reg_lock);
  438. return 0;
  439. }
  440. #define ICH6_RIRB_EX_UNSOL_EV (1<<4)
  441. /* retrieve RIRB entry - called from interrupt handler */
  442. static void azx_update_rirb(struct azx *chip)
  443. {
  444. unsigned int rp, wp;
  445. u32 res, res_ex;
  446. wp = azx_readb(chip, RIRBWP);
  447. if (wp == chip->rirb.wp)
  448. return;
  449. chip->rirb.wp = wp;
  450. while (chip->rirb.rp != wp) {
  451. chip->rirb.rp++;
  452. chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
  453. rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
  454. res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
  455. res = le32_to_cpu(chip->rirb.buf[rp]);
  456. if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
  457. snd_hda_queue_unsol_event(chip->bus, res, res_ex);
  458. else if (chip->rirb.cmds) {
  459. chip->rirb.cmds--;
  460. chip->rirb.res = res;
  461. }
  462. }
  463. }
  464. /* receive a response */
  465. static unsigned int azx_rirb_get_response(struct hda_codec *codec)
  466. {
  467. struct azx *chip = codec->bus->private_data;
  468. unsigned long timeout;
  469. again:
  470. timeout = jiffies + msecs_to_jiffies(1000);
  471. for (;;) {
  472. if (chip->polling_mode) {
  473. spin_lock_irq(&chip->reg_lock);
  474. azx_update_rirb(chip);
  475. spin_unlock_irq(&chip->reg_lock);
  476. }
  477. if (!chip->rirb.cmds)
  478. return chip->rirb.res; /* the last value */
  479. if (time_after(jiffies, timeout))
  480. break;
  481. if (codec->bus->needs_damn_long_delay)
  482. msleep(2); /* temporary workaround */
  483. else {
  484. udelay(10);
  485. cond_resched();
  486. }
  487. }
  488. if (chip->msi) {
  489. snd_printk(KERN_WARNING "hda_intel: No response from codec, "
  490. "disabling MSI: last cmd=0x%08x\n", chip->last_cmd);
  491. free_irq(chip->irq, chip);
  492. chip->irq = -1;
  493. pci_disable_msi(chip->pci);
  494. chip->msi = 0;
  495. if (azx_acquire_irq(chip, 1) < 0)
  496. return -1;
  497. goto again;
  498. }
  499. if (!chip->polling_mode) {
  500. snd_printk(KERN_WARNING "hda_intel: azx_get_response timeout, "
  501. "switching to polling mode: last cmd=0x%08x\n",
  502. chip->last_cmd);
  503. chip->polling_mode = 1;
  504. goto again;
  505. }
  506. snd_printk(KERN_ERR "hda_intel: azx_get_response timeout, "
  507. "switching to single_cmd mode: last cmd=0x%08x\n",
  508. chip->last_cmd);
  509. chip->rirb.rp = azx_readb(chip, RIRBWP);
  510. chip->rirb.cmds = 0;
  511. /* switch to single_cmd mode */
  512. chip->single_cmd = 1;
  513. azx_free_cmd_io(chip);
  514. return -1;
  515. }
  516. /*
  517. * Use the single immediate command instead of CORB/RIRB for simplicity
  518. *
  519. * Note: according to Intel, this is not preferred use. The command was
  520. * intended for the BIOS only, and may get confused with unsolicited
  521. * responses. So, we shouldn't use it for normal operation from the
  522. * driver.
  523. * I left the codes, however, for debugging/testing purposes.
  524. */
  525. /* send a command */
  526. static int azx_single_send_cmd(struct hda_codec *codec, u32 val)
  527. {
  528. struct azx *chip = codec->bus->private_data;
  529. int timeout = 50;
  530. while (timeout--) {
  531. /* check ICB busy bit */
  532. if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
  533. /* Clear IRV valid bit */
  534. azx_writew(chip, IRS, azx_readw(chip, IRS) |
  535. ICH6_IRS_VALID);
  536. azx_writel(chip, IC, val);
  537. azx_writew(chip, IRS, azx_readw(chip, IRS) |
  538. ICH6_IRS_BUSY);
  539. return 0;
  540. }
  541. udelay(1);
  542. }
  543. if (printk_ratelimit())
  544. snd_printd(SFX "send_cmd timeout: IRS=0x%x, val=0x%x\n",
  545. azx_readw(chip, IRS), val);
  546. return -EIO;
  547. }
  548. /* receive a response */
  549. static unsigned int azx_single_get_response(struct hda_codec *codec)
  550. {
  551. struct azx *chip = codec->bus->private_data;
  552. int timeout = 50;
  553. while (timeout--) {
  554. /* check IRV busy bit */
  555. if (azx_readw(chip, IRS) & ICH6_IRS_VALID)
  556. return azx_readl(chip, IR);
  557. udelay(1);
  558. }
  559. if (printk_ratelimit())
  560. snd_printd(SFX "get_response timeout: IRS=0x%x\n",
  561. azx_readw(chip, IRS));
  562. return (unsigned int)-1;
  563. }
  564. /*
  565. * The below are the main callbacks from hda_codec.
  566. *
  567. * They are just the skeleton to call sub-callbacks according to the
  568. * current setting of chip->single_cmd.
  569. */
  570. /* send a command */
  571. static int azx_send_cmd(struct hda_codec *codec, hda_nid_t nid,
  572. int direct, unsigned int verb,
  573. unsigned int para)
  574. {
  575. struct azx *chip = codec->bus->private_data;
  576. u32 val;
  577. val = (u32)(codec->addr & 0x0f) << 28;
  578. val |= (u32)direct << 27;
  579. val |= (u32)nid << 20;
  580. val |= verb << 8;
  581. val |= para;
  582. chip->last_cmd = val;
  583. if (chip->single_cmd)
  584. return azx_single_send_cmd(codec, val);
  585. else
  586. return azx_corb_send_cmd(codec, val);
  587. }
  588. /* get a response */
  589. static unsigned int azx_get_response(struct hda_codec *codec)
  590. {
  591. struct azx *chip = codec->bus->private_data;
  592. if (chip->single_cmd)
  593. return azx_single_get_response(codec);
  594. else
  595. return azx_rirb_get_response(codec);
  596. }
  597. #ifdef CONFIG_SND_HDA_POWER_SAVE
  598. static void azx_power_notify(struct hda_codec *codec);
  599. #endif
  600. /* reset codec link */
  601. static int azx_reset(struct azx *chip)
  602. {
  603. int count;
  604. /* clear STATESTS */
  605. azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
  606. /* reset controller */
  607. azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
  608. count = 50;
  609. while (azx_readb(chip, GCTL) && --count)
  610. msleep(1);
  611. /* delay for >= 100us for codec PLL to settle per spec
  612. * Rev 0.9 section 5.5.1
  613. */
  614. msleep(1);
  615. /* Bring controller out of reset */
  616. azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
  617. count = 50;
  618. while (!azx_readb(chip, GCTL) && --count)
  619. msleep(1);
  620. /* Brent Chartrand said to wait >= 540us for codecs to initialize */
  621. msleep(1);
  622. /* check to see if controller is ready */
  623. if (!azx_readb(chip, GCTL)) {
  624. snd_printd("azx_reset: controller not ready!\n");
  625. return -EBUSY;
  626. }
  627. /* Accept unsolicited responses */
  628. azx_writel(chip, GCTL, azx_readl(chip, GCTL) | ICH6_GCTL_UREN);
  629. /* detect codecs */
  630. if (!chip->codec_mask) {
  631. chip->codec_mask = azx_readw(chip, STATESTS);
  632. snd_printdd("codec_mask = 0x%x\n", chip->codec_mask);
  633. }
  634. return 0;
  635. }
  636. /*
  637. * Lowlevel interface
  638. */
  639. /* enable interrupts */
  640. static void azx_int_enable(struct azx *chip)
  641. {
  642. /* enable controller CIE and GIE */
  643. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
  644. ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
  645. }
  646. /* disable interrupts */
  647. static void azx_int_disable(struct azx *chip)
  648. {
  649. int i;
  650. /* disable interrupts in stream descriptor */
  651. for (i = 0; i < chip->num_streams; i++) {
  652. struct azx_dev *azx_dev = &chip->azx_dev[i];
  653. azx_sd_writeb(azx_dev, SD_CTL,
  654. azx_sd_readb(azx_dev, SD_CTL) & ~SD_INT_MASK);
  655. }
  656. /* disable SIE for all streams */
  657. azx_writeb(chip, INTCTL, 0);
  658. /* disable controller CIE and GIE */
  659. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
  660. ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
  661. }
  662. /* clear interrupts */
  663. static void azx_int_clear(struct azx *chip)
  664. {
  665. int i;
  666. /* clear stream status */
  667. for (i = 0; i < chip->num_streams; i++) {
  668. struct azx_dev *azx_dev = &chip->azx_dev[i];
  669. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  670. }
  671. /* clear STATESTS */
  672. azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
  673. /* clear rirb status */
  674. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  675. /* clear int status */
  676. azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
  677. }
  678. /* start a stream */
  679. static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
  680. {
  681. /* enable SIE */
  682. azx_writeb(chip, INTCTL,
  683. azx_readb(chip, INTCTL) | (1 << azx_dev->index));
  684. /* set DMA start and interrupt mask */
  685. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
  686. SD_CTL_DMA_START | SD_INT_MASK);
  687. }
  688. /* stop a stream */
  689. static void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
  690. {
  691. /* stop DMA */
  692. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) &
  693. ~(SD_CTL_DMA_START | SD_INT_MASK));
  694. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
  695. /* disable SIE */
  696. azx_writeb(chip, INTCTL,
  697. azx_readb(chip, INTCTL) & ~(1 << azx_dev->index));
  698. }
  699. /*
  700. * reset and start the controller registers
  701. */
  702. static void azx_init_chip(struct azx *chip)
  703. {
  704. if (chip->initialized)
  705. return;
  706. /* reset controller */
  707. azx_reset(chip);
  708. /* initialize interrupts */
  709. azx_int_clear(chip);
  710. azx_int_enable(chip);
  711. /* initialize the codec command I/O */
  712. if (!chip->single_cmd)
  713. azx_init_cmd_io(chip);
  714. /* program the position buffer */
  715. azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
  716. azx_writel(chip, DPUBASE, upper_32bit(chip->posbuf.addr));
  717. chip->initialized = 1;
  718. }
  719. /*
  720. * initialize the PCI registers
  721. */
  722. /* update bits in a PCI register byte */
  723. static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
  724. unsigned char mask, unsigned char val)
  725. {
  726. unsigned char data;
  727. pci_read_config_byte(pci, reg, &data);
  728. data &= ~mask;
  729. data |= (val & mask);
  730. pci_write_config_byte(pci, reg, data);
  731. }
  732. static void azx_init_pci(struct azx *chip)
  733. {
  734. unsigned short snoop;
  735. /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
  736. * TCSEL == Traffic Class Select Register, which sets PCI express QOS
  737. * Ensuring these bits are 0 clears playback static on some HD Audio
  738. * codecs
  739. */
  740. update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
  741. switch (chip->driver_type) {
  742. case AZX_DRIVER_ATI:
  743. /* For ATI SB450 azalia HD audio, we need to enable snoop */
  744. update_pci_byte(chip->pci,
  745. ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR,
  746. 0x07, ATI_SB450_HDAUDIO_ENABLE_SNOOP);
  747. break;
  748. case AZX_DRIVER_NVIDIA:
  749. /* For NVIDIA HDA, enable snoop */
  750. update_pci_byte(chip->pci,
  751. NVIDIA_HDA_TRANSREG_ADDR,
  752. 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
  753. break;
  754. case AZX_DRIVER_SCH:
  755. pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
  756. if (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) {
  757. pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC, \
  758. snoop & (~INTEL_SCH_HDA_DEVC_NOSNOOP));
  759. pci_read_config_word(chip->pci,
  760. INTEL_SCH_HDA_DEVC, &snoop);
  761. snd_printdd("HDA snoop disabled, enabling ... %s\n",\
  762. (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) \
  763. ? "Failed" : "OK");
  764. }
  765. break;
  766. }
  767. }
  768. /*
  769. * interrupt handler
  770. */
  771. static irqreturn_t azx_interrupt(int irq, void *dev_id)
  772. {
  773. struct azx *chip = dev_id;
  774. struct azx_dev *azx_dev;
  775. u32 status;
  776. int i;
  777. spin_lock(&chip->reg_lock);
  778. status = azx_readl(chip, INTSTS);
  779. if (status == 0) {
  780. spin_unlock(&chip->reg_lock);
  781. return IRQ_NONE;
  782. }
  783. for (i = 0; i < chip->num_streams; i++) {
  784. azx_dev = &chip->azx_dev[i];
  785. if (status & azx_dev->sd_int_sta_mask) {
  786. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  787. if (azx_dev->substream && azx_dev->running) {
  788. azx_dev->period_intr++;
  789. spin_unlock(&chip->reg_lock);
  790. snd_pcm_period_elapsed(azx_dev->substream);
  791. spin_lock(&chip->reg_lock);
  792. }
  793. }
  794. }
  795. /* clear rirb int */
  796. status = azx_readb(chip, RIRBSTS);
  797. if (status & RIRB_INT_MASK) {
  798. if (!chip->single_cmd && (status & RIRB_INT_RESPONSE))
  799. azx_update_rirb(chip);
  800. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  801. }
  802. #if 0
  803. /* clear state status int */
  804. if (azx_readb(chip, STATESTS) & 0x04)
  805. azx_writeb(chip, STATESTS, 0x04);
  806. #endif
  807. spin_unlock(&chip->reg_lock);
  808. return IRQ_HANDLED;
  809. }
  810. /*
  811. * set up BDL entries
  812. */
  813. static int azx_setup_periods(struct snd_pcm_substream *substream,
  814. struct azx_dev *azx_dev)
  815. {
  816. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  817. u32 *bdl;
  818. int i, ofs, periods, period_bytes;
  819. /* reset BDL address */
  820. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  821. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  822. period_bytes = snd_pcm_lib_period_bytes(substream);
  823. periods = azx_dev->bufsize / period_bytes;
  824. /* program the initial BDL entries */
  825. bdl = (u32 *)azx_dev->bdl.area;
  826. ofs = 0;
  827. azx_dev->frags = 0;
  828. for (i = 0; i < periods; i++) {
  829. int size, rest;
  830. if (i >= AZX_MAX_BDL_ENTRIES) {
  831. snd_printk(KERN_ERR "Too many BDL entries: "
  832. "buffer=%d, period=%d\n",
  833. azx_dev->bufsize, period_bytes);
  834. /* reset */
  835. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  836. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  837. return -EINVAL;
  838. }
  839. rest = period_bytes;
  840. do {
  841. dma_addr_t addr = snd_pcm_sgbuf_get_addr(sgbuf, ofs);
  842. /* program the address field of the BDL entry */
  843. bdl[0] = cpu_to_le32((u32)addr);
  844. bdl[1] = cpu_to_le32(upper_32bit(addr));
  845. /* program the size field of the BDL entry */
  846. size = PAGE_SIZE - (ofs % PAGE_SIZE);
  847. if (rest < size)
  848. size = rest;
  849. bdl[2] = cpu_to_le32(size);
  850. /* program the IOC to enable interrupt
  851. * only when the whole fragment is processed
  852. */
  853. rest -= size;
  854. bdl[3] = rest ? 0 : cpu_to_le32(0x01);
  855. bdl += 4;
  856. azx_dev->frags++;
  857. ofs += size;
  858. } while (rest > 0);
  859. }
  860. return 0;
  861. }
  862. /*
  863. * set up the SD for streaming
  864. */
  865. static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
  866. {
  867. unsigned char val;
  868. int timeout;
  869. /* make sure the run bit is zero for SD */
  870. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) &
  871. ~SD_CTL_DMA_START);
  872. /* reset stream */
  873. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
  874. SD_CTL_STREAM_RESET);
  875. udelay(3);
  876. timeout = 300;
  877. while (!((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  878. --timeout)
  879. ;
  880. val &= ~SD_CTL_STREAM_RESET;
  881. azx_sd_writeb(azx_dev, SD_CTL, val);
  882. udelay(3);
  883. timeout = 300;
  884. /* waiting for hardware to report that the stream is out of reset */
  885. while (((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  886. --timeout)
  887. ;
  888. /* program the stream_tag */
  889. azx_sd_writel(azx_dev, SD_CTL,
  890. (azx_sd_readl(azx_dev, SD_CTL) & ~SD_CTL_STREAM_TAG_MASK)|
  891. (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT));
  892. /* program the length of samples in cyclic buffer */
  893. azx_sd_writel(azx_dev, SD_CBL, azx_dev->bufsize);
  894. /* program the stream format */
  895. /* this value needs to be the same as the one programmed */
  896. azx_sd_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
  897. /* program the stream LVI (last valid index) of the BDL */
  898. azx_sd_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
  899. /* program the BDL address */
  900. /* lower BDL address */
  901. azx_sd_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
  902. /* upper BDL address */
  903. azx_sd_writel(azx_dev, SD_BDLPU, upper_32bit(azx_dev->bdl.addr));
  904. /* enable the position buffer */
  905. if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
  906. azx_writel(chip, DPLBASE,
  907. (u32)chip->posbuf.addr |ICH6_DPLBASE_ENABLE);
  908. /* set the interrupt enable bits in the descriptor control register */
  909. azx_sd_writel(azx_dev, SD_CTL,
  910. azx_sd_readl(azx_dev, SD_CTL) | SD_INT_MASK);
  911. return 0;
  912. }
  913. /*
  914. * Codec initialization
  915. */
  916. static unsigned int azx_max_codecs[] __devinitdata = {
  917. [AZX_DRIVER_ICH] = 3,
  918. [AZX_DRIVER_SCH] = 3,
  919. [AZX_DRIVER_ATI] = 4,
  920. [AZX_DRIVER_ATIHDMI] = 4,
  921. [AZX_DRIVER_VIA] = 3, /* FIXME: correct? */
  922. [AZX_DRIVER_SIS] = 3, /* FIXME: correct? */
  923. [AZX_DRIVER_ULI] = 3, /* FIXME: correct? */
  924. [AZX_DRIVER_NVIDIA] = 3, /* FIXME: correct? */
  925. };
  926. static int __devinit azx_codec_create(struct azx *chip, const char *model,
  927. unsigned int codec_probe_mask)
  928. {
  929. struct hda_bus_template bus_temp;
  930. int c, codecs, audio_codecs, err;
  931. memset(&bus_temp, 0, sizeof(bus_temp));
  932. bus_temp.private_data = chip;
  933. bus_temp.modelname = model;
  934. bus_temp.pci = chip->pci;
  935. bus_temp.ops.command = azx_send_cmd;
  936. bus_temp.ops.get_response = azx_get_response;
  937. #ifdef CONFIG_SND_HDA_POWER_SAVE
  938. bus_temp.ops.pm_notify = azx_power_notify;
  939. #endif
  940. err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus);
  941. if (err < 0)
  942. return err;
  943. codecs = audio_codecs = 0;
  944. for (c = 0; c < AZX_MAX_CODECS; c++) {
  945. if ((chip->codec_mask & (1 << c)) & codec_probe_mask) {
  946. struct hda_codec *codec;
  947. err = snd_hda_codec_new(chip->bus, c, &codec);
  948. if (err < 0)
  949. continue;
  950. codecs++;
  951. if (codec->afg)
  952. audio_codecs++;
  953. }
  954. }
  955. if (!audio_codecs) {
  956. /* probe additional slots if no codec is found */
  957. for (; c < azx_max_codecs[chip->driver_type]; c++) {
  958. if ((chip->codec_mask & (1 << c)) & codec_probe_mask) {
  959. err = snd_hda_codec_new(chip->bus, c, NULL);
  960. if (err < 0)
  961. continue;
  962. codecs++;
  963. }
  964. }
  965. }
  966. if (!codecs) {
  967. snd_printk(KERN_ERR SFX "no codecs initialized\n");
  968. return -ENXIO;
  969. }
  970. return 0;
  971. }
  972. /*
  973. * PCM support
  974. */
  975. /* assign a stream for the PCM */
  976. static inline struct azx_dev *azx_assign_device(struct azx *chip, int stream)
  977. {
  978. int dev, i, nums;
  979. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  980. dev = chip->playback_index_offset;
  981. nums = chip->playback_streams;
  982. } else {
  983. dev = chip->capture_index_offset;
  984. nums = chip->capture_streams;
  985. }
  986. for (i = 0; i < nums; i++, dev++)
  987. if (!chip->azx_dev[dev].opened) {
  988. chip->azx_dev[dev].opened = 1;
  989. return &chip->azx_dev[dev];
  990. }
  991. return NULL;
  992. }
  993. /* release the assigned stream */
  994. static inline void azx_release_device(struct azx_dev *azx_dev)
  995. {
  996. azx_dev->opened = 0;
  997. }
  998. static struct snd_pcm_hardware azx_pcm_hw = {
  999. .info = (SNDRV_PCM_INFO_MMAP |
  1000. SNDRV_PCM_INFO_INTERLEAVED |
  1001. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1002. SNDRV_PCM_INFO_MMAP_VALID |
  1003. /* No full-resume yet implemented */
  1004. /* SNDRV_PCM_INFO_RESUME |*/
  1005. SNDRV_PCM_INFO_PAUSE),
  1006. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1007. .rates = SNDRV_PCM_RATE_48000,
  1008. .rate_min = 48000,
  1009. .rate_max = 48000,
  1010. .channels_min = 2,
  1011. .channels_max = 2,
  1012. .buffer_bytes_max = AZX_MAX_BUF_SIZE,
  1013. .period_bytes_min = 128,
  1014. .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
  1015. .periods_min = 2,
  1016. .periods_max = AZX_MAX_FRAG,
  1017. .fifo_size = 0,
  1018. };
  1019. struct azx_pcm {
  1020. struct azx *chip;
  1021. struct hda_codec *codec;
  1022. struct hda_pcm_stream *hinfo[2];
  1023. };
  1024. static int azx_pcm_open(struct snd_pcm_substream *substream)
  1025. {
  1026. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1027. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1028. struct azx *chip = apcm->chip;
  1029. struct azx_dev *azx_dev;
  1030. struct snd_pcm_runtime *runtime = substream->runtime;
  1031. unsigned long flags;
  1032. int err;
  1033. mutex_lock(&chip->open_mutex);
  1034. azx_dev = azx_assign_device(chip, substream->stream);
  1035. if (azx_dev == NULL) {
  1036. mutex_unlock(&chip->open_mutex);
  1037. return -EBUSY;
  1038. }
  1039. runtime->hw = azx_pcm_hw;
  1040. runtime->hw.channels_min = hinfo->channels_min;
  1041. runtime->hw.channels_max = hinfo->channels_max;
  1042. runtime->hw.formats = hinfo->formats;
  1043. runtime->hw.rates = hinfo->rates;
  1044. snd_pcm_limit_hw_rates(runtime);
  1045. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  1046. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1047. 128);
  1048. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1049. 128);
  1050. snd_hda_power_up(apcm->codec);
  1051. err = hinfo->ops.open(hinfo, apcm->codec, substream);
  1052. if (err < 0) {
  1053. azx_release_device(azx_dev);
  1054. snd_hda_power_down(apcm->codec);
  1055. mutex_unlock(&chip->open_mutex);
  1056. return err;
  1057. }
  1058. spin_lock_irqsave(&chip->reg_lock, flags);
  1059. azx_dev->substream = substream;
  1060. azx_dev->running = 0;
  1061. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1062. runtime->private_data = azx_dev;
  1063. mutex_unlock(&chip->open_mutex);
  1064. return 0;
  1065. }
  1066. static int azx_pcm_close(struct snd_pcm_substream *substream)
  1067. {
  1068. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1069. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1070. struct azx *chip = apcm->chip;
  1071. struct azx_dev *azx_dev = get_azx_dev(substream);
  1072. unsigned long flags;
  1073. mutex_lock(&chip->open_mutex);
  1074. spin_lock_irqsave(&chip->reg_lock, flags);
  1075. azx_dev->substream = NULL;
  1076. azx_dev->running = 0;
  1077. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1078. azx_release_device(azx_dev);
  1079. hinfo->ops.close(hinfo, apcm->codec, substream);
  1080. snd_hda_power_down(apcm->codec);
  1081. mutex_unlock(&chip->open_mutex);
  1082. return 0;
  1083. }
  1084. static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
  1085. struct snd_pcm_hw_params *hw_params)
  1086. {
  1087. return snd_pcm_lib_malloc_pages(substream,
  1088. params_buffer_bytes(hw_params));
  1089. }
  1090. static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
  1091. {
  1092. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1093. struct azx_dev *azx_dev = get_azx_dev(substream);
  1094. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1095. /* reset BDL address */
  1096. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  1097. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  1098. azx_sd_writel(azx_dev, SD_CTL, 0);
  1099. hinfo->ops.cleanup(hinfo, apcm->codec, substream);
  1100. return snd_pcm_lib_free_pages(substream);
  1101. }
  1102. static int azx_pcm_prepare(struct snd_pcm_substream *substream)
  1103. {
  1104. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1105. struct azx *chip = apcm->chip;
  1106. struct azx_dev *azx_dev = get_azx_dev(substream);
  1107. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1108. struct snd_pcm_runtime *runtime = substream->runtime;
  1109. azx_dev->bufsize = snd_pcm_lib_buffer_bytes(substream);
  1110. azx_dev->format_val = snd_hda_calc_stream_format(runtime->rate,
  1111. runtime->channels,
  1112. runtime->format,
  1113. hinfo->maxbps);
  1114. if (!azx_dev->format_val) {
  1115. snd_printk(KERN_ERR SFX
  1116. "invalid format_val, rate=%d, ch=%d, format=%d\n",
  1117. runtime->rate, runtime->channels, runtime->format);
  1118. return -EINVAL;
  1119. }
  1120. snd_printdd("azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
  1121. azx_dev->bufsize, azx_dev->format_val);
  1122. if (azx_setup_periods(substream, azx_dev) < 0)
  1123. return -EINVAL;
  1124. azx_setup_controller(chip, azx_dev);
  1125. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1126. azx_dev->fifo_size = azx_sd_readw(azx_dev, SD_FIFOSIZE) + 1;
  1127. else
  1128. azx_dev->fifo_size = 0;
  1129. return hinfo->ops.prepare(hinfo, apcm->codec, azx_dev->stream_tag,
  1130. azx_dev->format_val, substream);
  1131. }
  1132. static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  1133. {
  1134. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1135. struct azx_dev *azx_dev = get_azx_dev(substream);
  1136. struct azx *chip = apcm->chip;
  1137. int err = 0;
  1138. spin_lock(&chip->reg_lock);
  1139. switch (cmd) {
  1140. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1141. case SNDRV_PCM_TRIGGER_RESUME:
  1142. case SNDRV_PCM_TRIGGER_START:
  1143. azx_stream_start(chip, azx_dev);
  1144. azx_dev->running = 1;
  1145. break;
  1146. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1147. case SNDRV_PCM_TRIGGER_SUSPEND:
  1148. case SNDRV_PCM_TRIGGER_STOP:
  1149. azx_stream_stop(chip, azx_dev);
  1150. azx_dev->running = 0;
  1151. break;
  1152. default:
  1153. err = -EINVAL;
  1154. }
  1155. spin_unlock(&chip->reg_lock);
  1156. if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH ||
  1157. cmd == SNDRV_PCM_TRIGGER_SUSPEND ||
  1158. cmd == SNDRV_PCM_TRIGGER_STOP) {
  1159. int timeout = 5000;
  1160. while ((azx_sd_readb(azx_dev, SD_CTL) & SD_CTL_DMA_START) &&
  1161. --timeout)
  1162. ;
  1163. }
  1164. return err;
  1165. }
  1166. static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
  1167. {
  1168. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1169. struct azx *chip = apcm->chip;
  1170. struct azx_dev *azx_dev = get_azx_dev(substream);
  1171. unsigned int pos;
  1172. if (chip->position_fix == POS_FIX_POSBUF ||
  1173. chip->position_fix == POS_FIX_AUTO) {
  1174. /* use the position buffer */
  1175. pos = le32_to_cpu(*azx_dev->posbuf);
  1176. if (chip->position_fix == POS_FIX_AUTO &&
  1177. azx_dev->period_intr == 1 && !pos) {
  1178. printk(KERN_WARNING
  1179. "hda-intel: Invalid position buffer, "
  1180. "using LPIB read method instead.\n");
  1181. chip->position_fix = POS_FIX_NONE;
  1182. goto read_lpib;
  1183. }
  1184. } else {
  1185. read_lpib:
  1186. /* read LPIB */
  1187. pos = azx_sd_readl(azx_dev, SD_LPIB);
  1188. if (chip->position_fix == POS_FIX_FIFO)
  1189. pos += azx_dev->fifo_size;
  1190. }
  1191. if (pos >= azx_dev->bufsize)
  1192. pos = 0;
  1193. return bytes_to_frames(substream->runtime, pos);
  1194. }
  1195. static struct snd_pcm_ops azx_pcm_ops = {
  1196. .open = azx_pcm_open,
  1197. .close = azx_pcm_close,
  1198. .ioctl = snd_pcm_lib_ioctl,
  1199. .hw_params = azx_pcm_hw_params,
  1200. .hw_free = azx_pcm_hw_free,
  1201. .prepare = azx_pcm_prepare,
  1202. .trigger = azx_pcm_trigger,
  1203. .pointer = azx_pcm_pointer,
  1204. .page = snd_pcm_sgbuf_ops_page,
  1205. };
  1206. static void azx_pcm_free(struct snd_pcm *pcm)
  1207. {
  1208. kfree(pcm->private_data);
  1209. }
  1210. static int __devinit create_codec_pcm(struct azx *chip, struct hda_codec *codec,
  1211. struct hda_pcm *cpcm)
  1212. {
  1213. int err;
  1214. struct snd_pcm *pcm;
  1215. struct azx_pcm *apcm;
  1216. /* if no substreams are defined for both playback and capture,
  1217. * it's just a placeholder. ignore it.
  1218. */
  1219. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  1220. return 0;
  1221. snd_assert(cpcm->name, return -EINVAL);
  1222. err = snd_pcm_new(chip->card, cpcm->name, cpcm->device,
  1223. cpcm->stream[0].substreams,
  1224. cpcm->stream[1].substreams,
  1225. &pcm);
  1226. if (err < 0)
  1227. return err;
  1228. strcpy(pcm->name, cpcm->name);
  1229. apcm = kmalloc(sizeof(*apcm), GFP_KERNEL);
  1230. if (apcm == NULL)
  1231. return -ENOMEM;
  1232. apcm->chip = chip;
  1233. apcm->codec = codec;
  1234. apcm->hinfo[0] = &cpcm->stream[0];
  1235. apcm->hinfo[1] = &cpcm->stream[1];
  1236. pcm->private_data = apcm;
  1237. pcm->private_free = azx_pcm_free;
  1238. if (cpcm->stream[0].substreams)
  1239. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &azx_pcm_ops);
  1240. if (cpcm->stream[1].substreams)
  1241. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &azx_pcm_ops);
  1242. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1243. snd_dma_pci_data(chip->pci),
  1244. 1024 * 64, 1024 * 1024);
  1245. chip->pcm[cpcm->device] = pcm;
  1246. return 0;
  1247. }
  1248. static int __devinit azx_pcm_create(struct azx *chip)
  1249. {
  1250. static const char *dev_name[HDA_PCM_NTYPES] = {
  1251. "Audio", "SPDIF", "HDMI", "Modem"
  1252. };
  1253. /* starting device index for each PCM type */
  1254. static int dev_idx[HDA_PCM_NTYPES] = {
  1255. [HDA_PCM_TYPE_AUDIO] = 0,
  1256. [HDA_PCM_TYPE_SPDIF] = 1,
  1257. [HDA_PCM_TYPE_HDMI] = 3,
  1258. [HDA_PCM_TYPE_MODEM] = 6
  1259. };
  1260. /* normal audio device indices; not linear to keep compatibility */
  1261. static int audio_idx[4] = { 0, 2, 4, 5 };
  1262. struct hda_codec *codec;
  1263. int c, err;
  1264. int num_devs[HDA_PCM_NTYPES];
  1265. err = snd_hda_build_pcms(chip->bus);
  1266. if (err < 0)
  1267. return err;
  1268. /* create audio PCMs */
  1269. memset(num_devs, 0, sizeof(num_devs));
  1270. list_for_each_entry(codec, &chip->bus->codec_list, list) {
  1271. for (c = 0; c < codec->num_pcms; c++) {
  1272. struct hda_pcm *cpcm = &codec->pcm_info[c];
  1273. int type = cpcm->pcm_type;
  1274. switch (type) {
  1275. case HDA_PCM_TYPE_AUDIO:
  1276. if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
  1277. snd_printk(KERN_WARNING
  1278. "Too many audio devices\n");
  1279. continue;
  1280. }
  1281. cpcm->device = audio_idx[num_devs[type]];
  1282. break;
  1283. case HDA_PCM_TYPE_SPDIF:
  1284. case HDA_PCM_TYPE_HDMI:
  1285. case HDA_PCM_TYPE_MODEM:
  1286. if (num_devs[type]) {
  1287. snd_printk(KERN_WARNING
  1288. "%s already defined\n",
  1289. dev_name[type]);
  1290. continue;
  1291. }
  1292. cpcm->device = dev_idx[type];
  1293. break;
  1294. default:
  1295. snd_printk(KERN_WARNING
  1296. "Invalid PCM type %d\n", type);
  1297. continue;
  1298. }
  1299. num_devs[type]++;
  1300. err = create_codec_pcm(chip, codec, cpcm);
  1301. if (err < 0)
  1302. return err;
  1303. }
  1304. }
  1305. return 0;
  1306. }
  1307. /*
  1308. * mixer creation - all stuff is implemented in hda module
  1309. */
  1310. static int __devinit azx_mixer_create(struct azx *chip)
  1311. {
  1312. return snd_hda_build_controls(chip->bus);
  1313. }
  1314. /*
  1315. * initialize SD streams
  1316. */
  1317. static int __devinit azx_init_stream(struct azx *chip)
  1318. {
  1319. int i;
  1320. /* initialize each stream (aka device)
  1321. * assign the starting bdl address to each stream (device)
  1322. * and initialize
  1323. */
  1324. for (i = 0; i < chip->num_streams; i++) {
  1325. struct azx_dev *azx_dev = &chip->azx_dev[i];
  1326. azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
  1327. /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  1328. azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
  1329. /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
  1330. azx_dev->sd_int_sta_mask = 1 << i;
  1331. /* stream tag: must be non-zero and unique */
  1332. azx_dev->index = i;
  1333. azx_dev->stream_tag = i + 1;
  1334. }
  1335. return 0;
  1336. }
  1337. static int azx_acquire_irq(struct azx *chip, int do_disconnect)
  1338. {
  1339. if (request_irq(chip->pci->irq, azx_interrupt,
  1340. chip->msi ? 0 : IRQF_SHARED,
  1341. "HDA Intel", chip)) {
  1342. printk(KERN_ERR "hda-intel: unable to grab IRQ %d, "
  1343. "disabling device\n", chip->pci->irq);
  1344. if (do_disconnect)
  1345. snd_card_disconnect(chip->card);
  1346. return -1;
  1347. }
  1348. chip->irq = chip->pci->irq;
  1349. pci_intx(chip->pci, !chip->msi);
  1350. return 0;
  1351. }
  1352. static void azx_stop_chip(struct azx *chip)
  1353. {
  1354. if (!chip->initialized)
  1355. return;
  1356. /* disable interrupts */
  1357. azx_int_disable(chip);
  1358. azx_int_clear(chip);
  1359. /* disable CORB/RIRB */
  1360. azx_free_cmd_io(chip);
  1361. /* disable position buffer */
  1362. azx_writel(chip, DPLBASE, 0);
  1363. azx_writel(chip, DPUBASE, 0);
  1364. chip->initialized = 0;
  1365. }
  1366. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1367. /* power-up/down the controller */
  1368. static void azx_power_notify(struct hda_codec *codec)
  1369. {
  1370. struct azx *chip = codec->bus->private_data;
  1371. struct hda_codec *c;
  1372. int power_on = 0;
  1373. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1374. if (c->power_on) {
  1375. power_on = 1;
  1376. break;
  1377. }
  1378. }
  1379. if (power_on)
  1380. azx_init_chip(chip);
  1381. else if (chip->running && power_save_controller)
  1382. azx_stop_chip(chip);
  1383. }
  1384. #endif /* CONFIG_SND_HDA_POWER_SAVE */
  1385. #ifdef CONFIG_PM
  1386. /*
  1387. * power management
  1388. */
  1389. static int azx_suspend(struct pci_dev *pci, pm_message_t state)
  1390. {
  1391. struct snd_card *card = pci_get_drvdata(pci);
  1392. struct azx *chip = card->private_data;
  1393. int i;
  1394. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1395. for (i = 0; i < AZX_MAX_PCMS; i++)
  1396. snd_pcm_suspend_all(chip->pcm[i]);
  1397. if (chip->initialized)
  1398. snd_hda_suspend(chip->bus, state);
  1399. azx_stop_chip(chip);
  1400. if (chip->irq >= 0) {
  1401. synchronize_irq(chip->irq);
  1402. free_irq(chip->irq, chip);
  1403. chip->irq = -1;
  1404. }
  1405. if (chip->msi)
  1406. pci_disable_msi(chip->pci);
  1407. pci_disable_device(pci);
  1408. pci_save_state(pci);
  1409. pci_set_power_state(pci, pci_choose_state(pci, state));
  1410. return 0;
  1411. }
  1412. static int azx_resume(struct pci_dev *pci)
  1413. {
  1414. struct snd_card *card = pci_get_drvdata(pci);
  1415. struct azx *chip = card->private_data;
  1416. pci_set_power_state(pci, PCI_D0);
  1417. pci_restore_state(pci);
  1418. if (pci_enable_device(pci) < 0) {
  1419. printk(KERN_ERR "hda-intel: pci_enable_device failed, "
  1420. "disabling device\n");
  1421. snd_card_disconnect(card);
  1422. return -EIO;
  1423. }
  1424. pci_set_master(pci);
  1425. if (chip->msi)
  1426. if (pci_enable_msi(pci) < 0)
  1427. chip->msi = 0;
  1428. if (azx_acquire_irq(chip, 1) < 0)
  1429. return -EIO;
  1430. azx_init_pci(chip);
  1431. if (snd_hda_codecs_inuse(chip->bus))
  1432. azx_init_chip(chip);
  1433. snd_hda_resume(chip->bus);
  1434. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1435. return 0;
  1436. }
  1437. #endif /* CONFIG_PM */
  1438. /*
  1439. * destructor
  1440. */
  1441. static int azx_free(struct azx *chip)
  1442. {
  1443. int i;
  1444. if (chip->initialized) {
  1445. for (i = 0; i < chip->num_streams; i++)
  1446. azx_stream_stop(chip, &chip->azx_dev[i]);
  1447. azx_stop_chip(chip);
  1448. }
  1449. if (chip->irq >= 0) {
  1450. synchronize_irq(chip->irq);
  1451. free_irq(chip->irq, (void*)chip);
  1452. }
  1453. if (chip->msi)
  1454. pci_disable_msi(chip->pci);
  1455. if (chip->remap_addr)
  1456. iounmap(chip->remap_addr);
  1457. if (chip->azx_dev) {
  1458. for (i = 0; i < chip->num_streams; i++)
  1459. if (chip->azx_dev[i].bdl.area)
  1460. snd_dma_free_pages(&chip->azx_dev[i].bdl);
  1461. }
  1462. if (chip->rb.area)
  1463. snd_dma_free_pages(&chip->rb);
  1464. if (chip->posbuf.area)
  1465. snd_dma_free_pages(&chip->posbuf);
  1466. pci_release_regions(chip->pci);
  1467. pci_disable_device(chip->pci);
  1468. kfree(chip->azx_dev);
  1469. kfree(chip);
  1470. return 0;
  1471. }
  1472. static int azx_dev_free(struct snd_device *device)
  1473. {
  1474. return azx_free(device->device_data);
  1475. }
  1476. /*
  1477. * white/black-listing for position_fix
  1478. */
  1479. static struct snd_pci_quirk position_fix_list[] __devinitdata = {
  1480. SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_NONE),
  1481. SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_NONE),
  1482. {}
  1483. };
  1484. static int __devinit check_position_fix(struct azx *chip, int fix)
  1485. {
  1486. const struct snd_pci_quirk *q;
  1487. if (fix == POS_FIX_AUTO) {
  1488. q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
  1489. if (q) {
  1490. printk(KERN_INFO
  1491. "hda_intel: position_fix set to %d "
  1492. "for device %04x:%04x\n",
  1493. q->value, q->subvendor, q->subdevice);
  1494. return q->value;
  1495. }
  1496. }
  1497. return fix;
  1498. }
  1499. /*
  1500. * black-lists for probe_mask
  1501. */
  1502. static struct snd_pci_quirk probe_mask_list[] __devinitdata = {
  1503. /* Thinkpad often breaks the controller communication when accessing
  1504. * to the non-working (or non-existing) modem codec slot.
  1505. */
  1506. SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
  1507. SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
  1508. SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
  1509. {}
  1510. };
  1511. static void __devinit check_probe_mask(struct azx *chip, int dev)
  1512. {
  1513. const struct snd_pci_quirk *q;
  1514. if (probe_mask[dev] == -1) {
  1515. q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
  1516. if (q) {
  1517. printk(KERN_INFO
  1518. "hda_intel: probe_mask set to 0x%x "
  1519. "for device %04x:%04x\n",
  1520. q->value, q->subvendor, q->subdevice);
  1521. probe_mask[dev] = q->value;
  1522. }
  1523. }
  1524. }
  1525. /*
  1526. * constructor
  1527. */
  1528. static int __devinit azx_create(struct snd_card *card, struct pci_dev *pci,
  1529. int dev, int driver_type,
  1530. struct azx **rchip)
  1531. {
  1532. struct azx *chip;
  1533. int i, err;
  1534. unsigned short gcap;
  1535. static struct snd_device_ops ops = {
  1536. .dev_free = azx_dev_free,
  1537. };
  1538. *rchip = NULL;
  1539. err = pci_enable_device(pci);
  1540. if (err < 0)
  1541. return err;
  1542. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1543. if (!chip) {
  1544. snd_printk(KERN_ERR SFX "cannot allocate chip\n");
  1545. pci_disable_device(pci);
  1546. return -ENOMEM;
  1547. }
  1548. spin_lock_init(&chip->reg_lock);
  1549. mutex_init(&chip->open_mutex);
  1550. chip->card = card;
  1551. chip->pci = pci;
  1552. chip->irq = -1;
  1553. chip->driver_type = driver_type;
  1554. chip->msi = enable_msi;
  1555. chip->position_fix = check_position_fix(chip, position_fix[dev]);
  1556. check_probe_mask(chip, dev);
  1557. chip->single_cmd = single_cmd;
  1558. #if BITS_PER_LONG != 64
  1559. /* Fix up base address on ULI M5461 */
  1560. if (chip->driver_type == AZX_DRIVER_ULI) {
  1561. u16 tmp3;
  1562. pci_read_config_word(pci, 0x40, &tmp3);
  1563. pci_write_config_word(pci, 0x40, tmp3 | 0x10);
  1564. pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
  1565. }
  1566. #endif
  1567. err = pci_request_regions(pci, "ICH HD audio");
  1568. if (err < 0) {
  1569. kfree(chip);
  1570. pci_disable_device(pci);
  1571. return err;
  1572. }
  1573. chip->addr = pci_resource_start(pci, 0);
  1574. chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci,0));
  1575. if (chip->remap_addr == NULL) {
  1576. snd_printk(KERN_ERR SFX "ioremap error\n");
  1577. err = -ENXIO;
  1578. goto errout;
  1579. }
  1580. if (chip->msi)
  1581. if (pci_enable_msi(pci) < 0)
  1582. chip->msi = 0;
  1583. if (azx_acquire_irq(chip, 0) < 0) {
  1584. err = -EBUSY;
  1585. goto errout;
  1586. }
  1587. pci_set_master(pci);
  1588. synchronize_irq(chip->irq);
  1589. gcap = azx_readw(chip, GCAP);
  1590. snd_printdd("chipset global capabilities = 0x%x\n", gcap);
  1591. /* allow 64bit DMA address if supported by H/W */
  1592. if ((gcap & 0x01) && !pci_set_dma_mask(pci, DMA_64BIT_MASK))
  1593. pci_set_consistent_dma_mask(pci, DMA_64BIT_MASK);
  1594. /* read number of streams from GCAP register instead of using
  1595. * hardcoded value
  1596. */
  1597. chip->capture_streams = (gcap >> 8) & 0x0f;
  1598. chip->playback_streams = (gcap >> 12) & 0x0f;
  1599. if (!chip->playback_streams && !chip->capture_streams) {
  1600. /* gcap didn't give any info, switching to old method */
  1601. switch (chip->driver_type) {
  1602. case AZX_DRIVER_ULI:
  1603. chip->playback_streams = ULI_NUM_PLAYBACK;
  1604. chip->capture_streams = ULI_NUM_CAPTURE;
  1605. break;
  1606. case AZX_DRIVER_ATIHDMI:
  1607. chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
  1608. chip->capture_streams = ATIHDMI_NUM_CAPTURE;
  1609. break;
  1610. default:
  1611. chip->playback_streams = ICH6_NUM_PLAYBACK;
  1612. chip->capture_streams = ICH6_NUM_CAPTURE;
  1613. break;
  1614. }
  1615. }
  1616. chip->capture_index_offset = 0;
  1617. chip->playback_index_offset = chip->capture_streams;
  1618. chip->num_streams = chip->playback_streams + chip->capture_streams;
  1619. chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
  1620. GFP_KERNEL);
  1621. if (!chip->azx_dev) {
  1622. snd_printk(KERN_ERR "cannot malloc azx_dev\n");
  1623. goto errout;
  1624. }
  1625. for (i = 0; i < chip->num_streams; i++) {
  1626. /* allocate memory for the BDL for each stream */
  1627. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  1628. snd_dma_pci_data(chip->pci),
  1629. BDL_SIZE, &chip->azx_dev[i].bdl);
  1630. if (err < 0) {
  1631. snd_printk(KERN_ERR SFX "cannot allocate BDL\n");
  1632. goto errout;
  1633. }
  1634. }
  1635. /* allocate memory for the position buffer */
  1636. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  1637. snd_dma_pci_data(chip->pci),
  1638. chip->num_streams * 8, &chip->posbuf);
  1639. if (err < 0) {
  1640. snd_printk(KERN_ERR SFX "cannot allocate posbuf\n");
  1641. goto errout;
  1642. }
  1643. /* allocate CORB/RIRB */
  1644. if (!chip->single_cmd) {
  1645. err = azx_alloc_cmd_io(chip);
  1646. if (err < 0)
  1647. goto errout;
  1648. }
  1649. /* initialize streams */
  1650. azx_init_stream(chip);
  1651. /* initialize chip */
  1652. azx_init_pci(chip);
  1653. azx_init_chip(chip);
  1654. /* codec detection */
  1655. if (!chip->codec_mask) {
  1656. snd_printk(KERN_ERR SFX "no codecs found!\n");
  1657. err = -ENODEV;
  1658. goto errout;
  1659. }
  1660. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1661. if (err <0) {
  1662. snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
  1663. goto errout;
  1664. }
  1665. strcpy(card->driver, "HDA-Intel");
  1666. strcpy(card->shortname, driver_short_names[chip->driver_type]);
  1667. sprintf(card->longname, "%s at 0x%lx irq %i",
  1668. card->shortname, chip->addr, chip->irq);
  1669. *rchip = chip;
  1670. return 0;
  1671. errout:
  1672. azx_free(chip);
  1673. return err;
  1674. }
  1675. static void power_down_all_codecs(struct azx *chip)
  1676. {
  1677. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1678. /* The codecs were powered up in snd_hda_codec_new().
  1679. * Now all initialization done, so turn them down if possible
  1680. */
  1681. struct hda_codec *codec;
  1682. list_for_each_entry(codec, &chip->bus->codec_list, list) {
  1683. snd_hda_power_down(codec);
  1684. }
  1685. #endif
  1686. }
  1687. static int __devinit azx_probe(struct pci_dev *pci,
  1688. const struct pci_device_id *pci_id)
  1689. {
  1690. static int dev;
  1691. struct snd_card *card;
  1692. struct azx *chip;
  1693. int err;
  1694. if (dev >= SNDRV_CARDS)
  1695. return -ENODEV;
  1696. if (!enable[dev]) {
  1697. dev++;
  1698. return -ENOENT;
  1699. }
  1700. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1701. if (!card) {
  1702. snd_printk(KERN_ERR SFX "Error creating card!\n");
  1703. return -ENOMEM;
  1704. }
  1705. err = azx_create(card, pci, dev, pci_id->driver_data, &chip);
  1706. if (err < 0) {
  1707. snd_card_free(card);
  1708. return err;
  1709. }
  1710. card->private_data = chip;
  1711. /* create codec instances */
  1712. err = azx_codec_create(chip, model[dev], probe_mask[dev]);
  1713. if (err < 0) {
  1714. snd_card_free(card);
  1715. return err;
  1716. }
  1717. /* create PCM streams */
  1718. err = azx_pcm_create(chip);
  1719. if (err < 0) {
  1720. snd_card_free(card);
  1721. return err;
  1722. }
  1723. /* create mixer controls */
  1724. err = azx_mixer_create(chip);
  1725. if (err < 0) {
  1726. snd_card_free(card);
  1727. return err;
  1728. }
  1729. snd_card_set_dev(card, &pci->dev);
  1730. err = snd_card_register(card);
  1731. if (err < 0) {
  1732. snd_card_free(card);
  1733. return err;
  1734. }
  1735. pci_set_drvdata(pci, card);
  1736. chip->running = 1;
  1737. power_down_all_codecs(chip);
  1738. dev++;
  1739. return err;
  1740. }
  1741. static void __devexit azx_remove(struct pci_dev *pci)
  1742. {
  1743. snd_card_free(pci_get_drvdata(pci));
  1744. pci_set_drvdata(pci, NULL);
  1745. }
  1746. /* PCI IDs */
  1747. static struct pci_device_id azx_ids[] = {
  1748. { 0x8086, 0x2668, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ICH }, /* ICH6 */
  1749. { 0x8086, 0x27d8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ICH }, /* ICH7 */
  1750. { 0x8086, 0x269a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ICH }, /* ESB2 */
  1751. { 0x8086, 0x284b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ICH }, /* ICH8 */
  1752. { 0x8086, 0x293e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ICH }, /* ICH9 */
  1753. { 0x8086, 0x293f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ICH }, /* ICH9 */
  1754. { 0x8086, 0x3a3e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ICH }, /* ICH10 */
  1755. { 0x8086, 0x3a6e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ICH }, /* ICH10 */
  1756. { 0x8086, 0x811b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_SCH }, /* SCH*/
  1757. { 0x1002, 0x437b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATI }, /* ATI SB450 */
  1758. { 0x1002, 0x4383, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATI }, /* ATI SB600 */
  1759. { 0x1002, 0x793b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RS600 HDMI */
  1760. { 0x1002, 0x7919, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RS690 HDMI */
  1761. { 0x1002, 0x960f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RS780 HDMI */
  1762. { 0x1002, 0xaa00, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI R600 HDMI */
  1763. { 0x1002, 0xaa08, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RV630 HDMI */
  1764. { 0x1002, 0xaa10, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RV610 HDMI */
  1765. { 0x1002, 0xaa18, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RV670 HDMI */
  1766. { 0x1002, 0xaa20, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RV635 HDMI */
  1767. { 0x1002, 0xaa28, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RV620 HDMI */
  1768. { 0x1002, 0xaa30, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RV770 HDMI */
  1769. { 0x1002, 0xaa38, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RV730 HDMI */
  1770. { 0x1002, 0xaa40, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RV710 HDMI */
  1771. { 0x1002, 0xaa48, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ATIHDMI }, /* ATI RV740 HDMI */
  1772. { 0x1106, 0x3288, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_VIA }, /* VIA VT8251/VT8237A */
  1773. { 0x1039, 0x7502, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_SIS }, /* SIS966 */
  1774. { 0x10b9, 0x5461, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_ULI }, /* ULI M5461 */
  1775. { 0x10de, 0x026c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP51 */
  1776. { 0x10de, 0x0371, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP55 */
  1777. { 0x10de, 0x03e4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP61 */
  1778. { 0x10de, 0x03f0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP61 */
  1779. { 0x10de, 0x044a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP65 */
  1780. { 0x10de, 0x044b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP65 */
  1781. { 0x10de, 0x055c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP67 */
  1782. { 0x10de, 0x055d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP67 */
  1783. { 0x10de, 0x07fc, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP73 */
  1784. { 0x10de, 0x07fd, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP73 */
  1785. { 0x10de, 0x0774, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP77 */
  1786. { 0x10de, 0x0775, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP77 */
  1787. { 0x10de, 0x0776, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP77 */
  1788. { 0x10de, 0x0777, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP77 */
  1789. { 0x10de, 0x0ac0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP79 */
  1790. { 0x10de, 0x0ac1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP79 */
  1791. { 0x10de, 0x0ac2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP79 */
  1792. { 0x10de, 0x0ac3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, AZX_DRIVER_NVIDIA }, /* NVIDIA MCP79 */
  1793. { 0, }
  1794. };
  1795. MODULE_DEVICE_TABLE(pci, azx_ids);
  1796. /* pci_driver definition */
  1797. static struct pci_driver driver = {
  1798. .name = "HDA Intel",
  1799. .id_table = azx_ids,
  1800. .probe = azx_probe,
  1801. .remove = __devexit_p(azx_remove),
  1802. #ifdef CONFIG_PM
  1803. .suspend = azx_suspend,
  1804. .resume = azx_resume,
  1805. #endif
  1806. };
  1807. static int __init alsa_card_azx_init(void)
  1808. {
  1809. return pci_register_driver(&driver);
  1810. }
  1811. static void __exit alsa_card_azx_exit(void)
  1812. {
  1813. pci_unregister_driver(&driver);
  1814. }
  1815. module_init(alsa_card_azx_init)
  1816. module_exit(alsa_card_azx_exit)