hda_intel.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  1. /*
  2. *
  3. * hda_intel.c - Implementation of primary alsa driver code base for Intel HD Audio.
  4. *
  5. * Copyright(c) 2004 Intel Corporation. All rights reserved.
  6. *
  7. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  8. * PeiSen Hou <pshou@realtek.com.tw>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * this program; if not, write to the Free Software Foundation, Inc., 59
  22. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. * CONTACTS:
  25. *
  26. * Matt Jared matt.jared@intel.com
  27. * Andy Kopp andy.kopp@intel.com
  28. * Dan Kogan dan.d.kogan@intel.com
  29. *
  30. * CHANGES:
  31. *
  32. * 2004.12.01 Major rewrite by tiwai, merged the work of pshou
  33. *
  34. */
  35. #include <sound/driver.h>
  36. #include <asm/io.h>
  37. #include <linux/delay.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/init.h>
  42. #include <linux/slab.h>
  43. #include <linux/pci.h>
  44. #include <sound/core.h>
  45. #include <sound/initval.h>
  46. #include "hda_codec.h"
  47. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  48. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  49. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  50. static char *model[SNDRV_CARDS];
  51. static int position_fix[SNDRV_CARDS];
  52. module_param_array(index, int, NULL, 0444);
  53. MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
  54. module_param_array(id, charp, NULL, 0444);
  55. MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
  56. module_param_array(enable, bool, NULL, 0444);
  57. MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
  58. module_param_array(model, charp, NULL, 0444);
  59. MODULE_PARM_DESC(model, "Use the given board model.");
  60. module_param_array(position_fix, int, NULL, 0444);
  61. MODULE_PARM_DESC(position_fix, "Fix DMA pointer (0 = FIFO size, 1 = none, 2 = POSBUF).");
  62. MODULE_LICENSE("GPL");
  63. MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
  64. "{Intel, ICH6M},"
  65. "{Intel, ICH7},"
  66. "{Intel, ESB2},"
  67. "{ATI, SB450},"
  68. "{VIA, VT8251},"
  69. "{VIA, VT8237A}}");
  70. MODULE_DESCRIPTION("Intel HDA driver");
  71. #define SFX "hda-intel: "
  72. /*
  73. * registers
  74. */
  75. #define ICH6_REG_GCAP 0x00
  76. #define ICH6_REG_VMIN 0x02
  77. #define ICH6_REG_VMAJ 0x03
  78. #define ICH6_REG_OUTPAY 0x04
  79. #define ICH6_REG_INPAY 0x06
  80. #define ICH6_REG_GCTL 0x08
  81. #define ICH6_REG_WAKEEN 0x0c
  82. #define ICH6_REG_STATESTS 0x0e
  83. #define ICH6_REG_GSTS 0x10
  84. #define ICH6_REG_INTCTL 0x20
  85. #define ICH6_REG_INTSTS 0x24
  86. #define ICH6_REG_WALCLK 0x30
  87. #define ICH6_REG_SYNC 0x34
  88. #define ICH6_REG_CORBLBASE 0x40
  89. #define ICH6_REG_CORBUBASE 0x44
  90. #define ICH6_REG_CORBWP 0x48
  91. #define ICH6_REG_CORBRP 0x4A
  92. #define ICH6_REG_CORBCTL 0x4c
  93. #define ICH6_REG_CORBSTS 0x4d
  94. #define ICH6_REG_CORBSIZE 0x4e
  95. #define ICH6_REG_RIRBLBASE 0x50
  96. #define ICH6_REG_RIRBUBASE 0x54
  97. #define ICH6_REG_RIRBWP 0x58
  98. #define ICH6_REG_RINTCNT 0x5a
  99. #define ICH6_REG_RIRBCTL 0x5c
  100. #define ICH6_REG_RIRBSTS 0x5d
  101. #define ICH6_REG_RIRBSIZE 0x5e
  102. #define ICH6_REG_IC 0x60
  103. #define ICH6_REG_IR 0x64
  104. #define ICH6_REG_IRS 0x68
  105. #define ICH6_IRS_VALID (1<<1)
  106. #define ICH6_IRS_BUSY (1<<0)
  107. #define ICH6_REG_DPLBASE 0x70
  108. #define ICH6_REG_DPUBASE 0x74
  109. #define ICH6_DPLBASE_ENABLE 0x1 /* Enable position buffer */
  110. /* SD offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  111. enum { SDI0, SDI1, SDI2, SDI3, SDO0, SDO1, SDO2, SDO3 };
  112. /* stream register offsets from stream base */
  113. #define ICH6_REG_SD_CTL 0x00
  114. #define ICH6_REG_SD_STS 0x03
  115. #define ICH6_REG_SD_LPIB 0x04
  116. #define ICH6_REG_SD_CBL 0x08
  117. #define ICH6_REG_SD_LVI 0x0c
  118. #define ICH6_REG_SD_FIFOW 0x0e
  119. #define ICH6_REG_SD_FIFOSIZE 0x10
  120. #define ICH6_REG_SD_FORMAT 0x12
  121. #define ICH6_REG_SD_BDLPL 0x18
  122. #define ICH6_REG_SD_BDLPU 0x1c
  123. /* PCI space */
  124. #define ICH6_PCIREG_TCSEL 0x44
  125. /*
  126. * other constants
  127. */
  128. /* max number of SDs */
  129. #define MAX_ICH6_DEV 8
  130. /* max number of fragments - we may use more if allocating more pages for BDL */
  131. #define AZX_MAX_FRAG (PAGE_SIZE / (MAX_ICH6_DEV * 16))
  132. /* max buffer size - no h/w limit, you can increase as you like */
  133. #define AZX_MAX_BUF_SIZE (1024*1024*1024)
  134. /* max number of PCM devics per card */
  135. #define AZX_MAX_PCMS 8
  136. /* RIRB int mask: overrun[2], response[0] */
  137. #define RIRB_INT_RESPONSE 0x01
  138. #define RIRB_INT_OVERRUN 0x04
  139. #define RIRB_INT_MASK 0x05
  140. /* STATESTS int mask: SD2,SD1,SD0 */
  141. #define STATESTS_INT_MASK 0x07
  142. #define AZX_MAX_CODECS 4
  143. /* SD_CTL bits */
  144. #define SD_CTL_STREAM_RESET 0x01 /* stream reset bit */
  145. #define SD_CTL_DMA_START 0x02 /* stream DMA start bit */
  146. #define SD_CTL_STREAM_TAG_MASK (0xf << 20)
  147. #define SD_CTL_STREAM_TAG_SHIFT 20
  148. /* SD_CTL and SD_STS */
  149. #define SD_INT_DESC_ERR 0x10 /* descriptor error interrupt */
  150. #define SD_INT_FIFO_ERR 0x08 /* FIFO error interrupt */
  151. #define SD_INT_COMPLETE 0x04 /* completion interrupt */
  152. #define SD_INT_MASK (SD_INT_DESC_ERR|SD_INT_FIFO_ERR|SD_INT_COMPLETE)
  153. /* SD_STS */
  154. #define SD_STS_FIFO_READY 0x20 /* FIFO ready */
  155. /* INTCTL and INTSTS */
  156. #define ICH6_INT_ALL_STREAM 0xff /* all stream interrupts */
  157. #define ICH6_INT_CTRL_EN 0x40000000 /* controller interrupt enable bit */
  158. #define ICH6_INT_GLOBAL_EN 0x80000000 /* global interrupt enable bit */
  159. /* GCTL unsolicited response enable bit */
  160. #define ICH6_GCTL_UREN (1<<8)
  161. /* GCTL reset bit */
  162. #define ICH6_GCTL_RESET (1<<0)
  163. /* CORB/RIRB control, read/write pointer */
  164. #define ICH6_RBCTL_DMA_EN 0x02 /* enable DMA */
  165. #define ICH6_RBCTL_IRQ_EN 0x01 /* enable IRQ */
  166. #define ICH6_RBRWP_CLR 0x8000 /* read/write pointer clear */
  167. /* below are so far hardcoded - should read registers in future */
  168. #define ICH6_MAX_CORB_ENTRIES 256
  169. #define ICH6_MAX_RIRB_ENTRIES 256
  170. /* position fix mode */
  171. enum {
  172. POS_FIX_FIFO,
  173. POS_FIX_NONE,
  174. POS_FIX_POSBUF
  175. };
  176. /* Defines for ATI HD Audio support in SB450 south bridge */
  177. #define ATI_SB450_HDAUDIO_PCI_DEVICE_ID 0x437b
  178. #define ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR 0x42
  179. #define ATI_SB450_HDAUDIO_ENABLE_SNOOP 0x02
  180. /*
  181. * Use CORB/RIRB for communication from/to codecs.
  182. * This is the way recommended by Intel (see below).
  183. */
  184. #define USE_CORB_RIRB
  185. /*
  186. */
  187. typedef struct snd_azx azx_t;
  188. typedef struct snd_azx_rb azx_rb_t;
  189. typedef struct snd_azx_dev azx_dev_t;
  190. struct snd_azx_dev {
  191. u32 *bdl; /* virtual address of the BDL */
  192. dma_addr_t bdl_addr; /* physical address of the BDL */
  193. volatile u32 *posbuf; /* position buffer pointer */
  194. unsigned int bufsize; /* size of the play buffer in bytes */
  195. unsigned int fragsize; /* size of each period in bytes */
  196. unsigned int frags; /* number for period in the play buffer */
  197. unsigned int fifo_size; /* FIFO size */
  198. void __iomem *sd_addr; /* stream descriptor pointer */
  199. u32 sd_int_sta_mask; /* stream int status mask */
  200. /* pcm support */
  201. snd_pcm_substream_t *substream; /* assigned substream, set in PCM open */
  202. unsigned int format_val; /* format value to be set in the controller and the codec */
  203. unsigned char stream_tag; /* assigned stream */
  204. unsigned char index; /* stream index */
  205. unsigned int opened: 1;
  206. unsigned int running: 1;
  207. };
  208. /* CORB/RIRB */
  209. struct snd_azx_rb {
  210. u32 *buf; /* CORB/RIRB buffer
  211. * Each CORB entry is 4byte, RIRB is 8byte
  212. */
  213. dma_addr_t addr; /* physical address of CORB/RIRB buffer */
  214. /* for RIRB */
  215. unsigned short rp, wp; /* read/write pointers */
  216. int cmds; /* number of pending requests */
  217. u32 res; /* last read value */
  218. };
  219. struct snd_azx {
  220. snd_card_t *card;
  221. struct pci_dev *pci;
  222. /* pci resources */
  223. unsigned long addr;
  224. void __iomem *remap_addr;
  225. int irq;
  226. /* locks */
  227. spinlock_t reg_lock;
  228. struct semaphore open_mutex;
  229. /* streams */
  230. azx_dev_t azx_dev[MAX_ICH6_DEV];
  231. /* PCM */
  232. unsigned int pcm_devs;
  233. snd_pcm_t *pcm[AZX_MAX_PCMS];
  234. /* HD codec */
  235. unsigned short codec_mask;
  236. struct hda_bus *bus;
  237. /* CORB/RIRB */
  238. azx_rb_t corb;
  239. azx_rb_t rirb;
  240. /* BDL, CORB/RIRB and position buffers */
  241. struct snd_dma_buffer bdl;
  242. struct snd_dma_buffer rb;
  243. struct snd_dma_buffer posbuf;
  244. /* flags */
  245. int position_fix;
  246. unsigned int initialized: 1;
  247. };
  248. /*
  249. * macros for easy use
  250. */
  251. #define azx_writel(chip,reg,value) \
  252. writel(value, (chip)->remap_addr + ICH6_REG_##reg)
  253. #define azx_readl(chip,reg) \
  254. readl((chip)->remap_addr + ICH6_REG_##reg)
  255. #define azx_writew(chip,reg,value) \
  256. writew(value, (chip)->remap_addr + ICH6_REG_##reg)
  257. #define azx_readw(chip,reg) \
  258. readw((chip)->remap_addr + ICH6_REG_##reg)
  259. #define azx_writeb(chip,reg,value) \
  260. writeb(value, (chip)->remap_addr + ICH6_REG_##reg)
  261. #define azx_readb(chip,reg) \
  262. readb((chip)->remap_addr + ICH6_REG_##reg)
  263. #define azx_sd_writel(dev,reg,value) \
  264. writel(value, (dev)->sd_addr + ICH6_REG_##reg)
  265. #define azx_sd_readl(dev,reg) \
  266. readl((dev)->sd_addr + ICH6_REG_##reg)
  267. #define azx_sd_writew(dev,reg,value) \
  268. writew(value, (dev)->sd_addr + ICH6_REG_##reg)
  269. #define azx_sd_readw(dev,reg) \
  270. readw((dev)->sd_addr + ICH6_REG_##reg)
  271. #define azx_sd_writeb(dev,reg,value) \
  272. writeb(value, (dev)->sd_addr + ICH6_REG_##reg)
  273. #define azx_sd_readb(dev,reg) \
  274. readb((dev)->sd_addr + ICH6_REG_##reg)
  275. /* for pcm support */
  276. #define get_azx_dev(substream) (azx_dev_t*)(substream->runtime->private_data)
  277. /* Get the upper 32bit of the given dma_addr_t
  278. * Compiler should optimize and eliminate the code if dma_addr_t is 32bit
  279. */
  280. #define upper_32bit(addr) (sizeof(addr) > 4 ? (u32)((addr) >> 32) : (u32)0)
  281. /*
  282. * Interface for HD codec
  283. */
  284. #ifdef USE_CORB_RIRB
  285. /*
  286. * CORB / RIRB interface
  287. */
  288. static int azx_alloc_cmd_io(azx_t *chip)
  289. {
  290. int err;
  291. /* single page (at least 4096 bytes) must suffice for both ringbuffes */
  292. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  293. PAGE_SIZE, &chip->rb);
  294. if (err < 0) {
  295. snd_printk(KERN_ERR SFX "cannot allocate CORB/RIRB\n");
  296. return err;
  297. }
  298. return 0;
  299. }
  300. static void azx_init_cmd_io(azx_t *chip)
  301. {
  302. /* CORB set up */
  303. chip->corb.addr = chip->rb.addr;
  304. chip->corb.buf = (u32 *)chip->rb.area;
  305. azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
  306. azx_writel(chip, CORBUBASE, upper_32bit(chip->corb.addr));
  307. /* set the corb write pointer to 0 */
  308. azx_writew(chip, CORBWP, 0);
  309. /* reset the corb hw read pointer */
  310. azx_writew(chip, CORBRP, ICH6_RBRWP_CLR);
  311. /* enable corb dma */
  312. azx_writeb(chip, CORBCTL, ICH6_RBCTL_DMA_EN);
  313. /* RIRB set up */
  314. chip->rirb.addr = chip->rb.addr + 2048;
  315. chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
  316. azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
  317. azx_writel(chip, RIRBUBASE, upper_32bit(chip->rirb.addr));
  318. /* reset the rirb hw write pointer */
  319. azx_writew(chip, RIRBWP, ICH6_RBRWP_CLR);
  320. /* set N=1, get RIRB response interrupt for new entry */
  321. azx_writew(chip, RINTCNT, 1);
  322. /* enable rirb dma and response irq */
  323. #ifdef USE_CORB_RIRB
  324. azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
  325. #else
  326. azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN);
  327. #endif
  328. chip->rirb.rp = chip->rirb.cmds = 0;
  329. }
  330. static void azx_free_cmd_io(azx_t *chip)
  331. {
  332. /* disable ringbuffer DMAs */
  333. azx_writeb(chip, RIRBCTL, 0);
  334. azx_writeb(chip, CORBCTL, 0);
  335. }
  336. /* send a command */
  337. static int azx_send_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
  338. unsigned int verb, unsigned int para)
  339. {
  340. azx_t *chip = codec->bus->private_data;
  341. unsigned int wp;
  342. u32 val;
  343. val = (u32)(codec->addr & 0x0f) << 28;
  344. val |= (u32)direct << 27;
  345. val |= (u32)nid << 20;
  346. val |= verb << 8;
  347. val |= para;
  348. /* add command to corb */
  349. wp = azx_readb(chip, CORBWP);
  350. wp++;
  351. wp %= ICH6_MAX_CORB_ENTRIES;
  352. spin_lock_irq(&chip->reg_lock);
  353. chip->rirb.cmds++;
  354. chip->corb.buf[wp] = cpu_to_le32(val);
  355. azx_writel(chip, CORBWP, wp);
  356. spin_unlock_irq(&chip->reg_lock);
  357. return 0;
  358. }
  359. #define ICH6_RIRB_EX_UNSOL_EV (1<<4)
  360. /* retrieve RIRB entry - called from interrupt handler */
  361. static void azx_update_rirb(azx_t *chip)
  362. {
  363. unsigned int rp, wp;
  364. u32 res, res_ex;
  365. wp = azx_readb(chip, RIRBWP);
  366. if (wp == chip->rirb.wp)
  367. return;
  368. chip->rirb.wp = wp;
  369. while (chip->rirb.rp != wp) {
  370. chip->rirb.rp++;
  371. chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
  372. rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
  373. res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
  374. res = le32_to_cpu(chip->rirb.buf[rp]);
  375. if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
  376. snd_hda_queue_unsol_event(chip->bus, res, res_ex);
  377. else if (chip->rirb.cmds) {
  378. chip->rirb.cmds--;
  379. chip->rirb.res = res;
  380. }
  381. }
  382. }
  383. /* receive a response */
  384. static unsigned int azx_get_response(struct hda_codec *codec)
  385. {
  386. azx_t *chip = codec->bus->private_data;
  387. int timeout = 50;
  388. while (chip->rirb.cmds) {
  389. if (! --timeout) {
  390. snd_printk(KERN_ERR "azx_get_response timeout\n");
  391. chip->rirb.rp = azx_readb(chip, RIRBWP);
  392. chip->rirb.cmds = 0;
  393. return -1;
  394. }
  395. msleep(1);
  396. }
  397. return chip->rirb.res; /* the last value */
  398. }
  399. #else
  400. /*
  401. * Use the single immediate command instead of CORB/RIRB for simplicity
  402. *
  403. * Note: according to Intel, this is not preferred use. The command was
  404. * intended for the BIOS only, and may get confused with unsolicited
  405. * responses. So, we shouldn't use it for normal operation from the
  406. * driver.
  407. * I left the codes, however, for debugging/testing purposes.
  408. */
  409. #define azx_alloc_cmd_io(chip) 0
  410. #define azx_init_cmd_io(chip)
  411. #define azx_free_cmd_io(chip)
  412. /* send a command */
  413. static int azx_send_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
  414. unsigned int verb, unsigned int para)
  415. {
  416. azx_t *chip = codec->bus->private_data;
  417. u32 val;
  418. int timeout = 50;
  419. val = (u32)(codec->addr & 0x0f) << 28;
  420. val |= (u32)direct << 27;
  421. val |= (u32)nid << 20;
  422. val |= verb << 8;
  423. val |= para;
  424. while (timeout--) {
  425. /* check ICB busy bit */
  426. if (! (azx_readw(chip, IRS) & ICH6_IRS_BUSY)) {
  427. /* Clear IRV valid bit */
  428. azx_writew(chip, IRS, azx_readw(chip, IRS) | ICH6_IRS_VALID);
  429. azx_writel(chip, IC, val);
  430. azx_writew(chip, IRS, azx_readw(chip, IRS) | ICH6_IRS_BUSY);
  431. return 0;
  432. }
  433. udelay(1);
  434. }
  435. snd_printd(SFX "send_cmd timeout: IRS=0x%x, val=0x%x\n", azx_readw(chip, IRS), val);
  436. return -EIO;
  437. }
  438. /* receive a response */
  439. static unsigned int azx_get_response(struct hda_codec *codec)
  440. {
  441. azx_t *chip = codec->bus->private_data;
  442. int timeout = 50;
  443. while (timeout--) {
  444. /* check IRV busy bit */
  445. if (azx_readw(chip, IRS) & ICH6_IRS_VALID)
  446. return azx_readl(chip, IR);
  447. udelay(1);
  448. }
  449. snd_printd(SFX "get_response timeout: IRS=0x%x\n", azx_readw(chip, IRS));
  450. return (unsigned int)-1;
  451. }
  452. #define azx_update_rirb(chip)
  453. #endif /* USE_CORB_RIRB */
  454. /* reset codec link */
  455. static int azx_reset(azx_t *chip)
  456. {
  457. int count;
  458. /* reset controller */
  459. azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
  460. count = 50;
  461. while (azx_readb(chip, GCTL) && --count)
  462. msleep(1);
  463. /* delay for >= 100us for codec PLL to settle per spec
  464. * Rev 0.9 section 5.5.1
  465. */
  466. msleep(1);
  467. /* Bring controller out of reset */
  468. azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
  469. count = 50;
  470. while (! azx_readb(chip, GCTL) && --count)
  471. msleep(1);
  472. /* Brent Chartrand said to wait >= 540us for codecs to intialize */
  473. msleep(1);
  474. /* check to see if controller is ready */
  475. if (! azx_readb(chip, GCTL)) {
  476. snd_printd("azx_reset: controller not ready!\n");
  477. return -EBUSY;
  478. }
  479. /* Accept unsolicited responses */
  480. azx_writel(chip, GCTL, azx_readl(chip, GCTL) | ICH6_GCTL_UREN);
  481. /* detect codecs */
  482. if (! chip->codec_mask) {
  483. chip->codec_mask = azx_readw(chip, STATESTS);
  484. snd_printdd("codec_mask = 0x%x\n", chip->codec_mask);
  485. }
  486. return 0;
  487. }
  488. /*
  489. * Lowlevel interface
  490. */
  491. /* enable interrupts */
  492. static void azx_int_enable(azx_t *chip)
  493. {
  494. /* enable controller CIE and GIE */
  495. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
  496. ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
  497. }
  498. /* disable interrupts */
  499. static void azx_int_disable(azx_t *chip)
  500. {
  501. int i;
  502. /* disable interrupts in stream descriptor */
  503. for (i = 0; i < MAX_ICH6_DEV; i++) {
  504. azx_dev_t *azx_dev = &chip->azx_dev[i];
  505. azx_sd_writeb(azx_dev, SD_CTL,
  506. azx_sd_readb(azx_dev, SD_CTL) & ~SD_INT_MASK);
  507. }
  508. /* disable SIE for all streams */
  509. azx_writeb(chip, INTCTL, 0);
  510. /* disable controller CIE and GIE */
  511. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
  512. ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
  513. }
  514. /* clear interrupts */
  515. static void azx_int_clear(azx_t *chip)
  516. {
  517. int i;
  518. /* clear stream status */
  519. for (i = 0; i < MAX_ICH6_DEV; i++) {
  520. azx_dev_t *azx_dev = &chip->azx_dev[i];
  521. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  522. }
  523. /* clear STATESTS */
  524. azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
  525. /* clear rirb status */
  526. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  527. /* clear int status */
  528. azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
  529. }
  530. /* start a stream */
  531. static void azx_stream_start(azx_t *chip, azx_dev_t *azx_dev)
  532. {
  533. /* enable SIE */
  534. azx_writeb(chip, INTCTL,
  535. azx_readb(chip, INTCTL) | (1 << azx_dev->index));
  536. /* set DMA start and interrupt mask */
  537. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
  538. SD_CTL_DMA_START | SD_INT_MASK);
  539. }
  540. /* stop a stream */
  541. static void azx_stream_stop(azx_t *chip, azx_dev_t *azx_dev)
  542. {
  543. /* stop DMA */
  544. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) &
  545. ~(SD_CTL_DMA_START | SD_INT_MASK));
  546. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
  547. /* disable SIE */
  548. azx_writeb(chip, INTCTL,
  549. azx_readb(chip, INTCTL) & ~(1 << azx_dev->index));
  550. }
  551. /*
  552. * initialize the chip
  553. */
  554. static void azx_init_chip(azx_t *chip)
  555. {
  556. unsigned char tcsel_reg, ati_misc_cntl2;
  557. /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
  558. * TCSEL == Traffic Class Select Register, which sets PCI express QOS
  559. * Ensuring these bits are 0 clears playback static on some HD Audio codecs
  560. */
  561. pci_read_config_byte (chip->pci, ICH6_PCIREG_TCSEL, &tcsel_reg);
  562. pci_write_config_byte(chip->pci, ICH6_PCIREG_TCSEL, tcsel_reg & 0xf8);
  563. /* reset controller */
  564. azx_reset(chip);
  565. /* initialize interrupts */
  566. azx_int_clear(chip);
  567. azx_int_enable(chip);
  568. /* initialize the codec command I/O */
  569. azx_init_cmd_io(chip);
  570. if (chip->position_fix == POS_FIX_POSBUF) {
  571. /* program the position buffer */
  572. azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
  573. azx_writel(chip, DPUBASE, upper_32bit(chip->posbuf.addr));
  574. }
  575. /* For ATI SB450 azalia HD audio, we need to enable snoop */
  576. if (chip->pci->vendor == PCI_VENDOR_ID_ATI &&
  577. chip->pci->device == ATI_SB450_HDAUDIO_PCI_DEVICE_ID) {
  578. pci_read_config_byte(chip->pci, ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR,
  579. &ati_misc_cntl2);
  580. pci_write_config_byte(chip->pci, ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR,
  581. (ati_misc_cntl2 & 0xf8) | ATI_SB450_HDAUDIO_ENABLE_SNOOP);
  582. }
  583. }
  584. /*
  585. * interrupt handler
  586. */
  587. static irqreturn_t azx_interrupt(int irq, void* dev_id, struct pt_regs *regs)
  588. {
  589. azx_t *chip = dev_id;
  590. azx_dev_t *azx_dev;
  591. u32 status;
  592. int i;
  593. spin_lock(&chip->reg_lock);
  594. status = azx_readl(chip, INTSTS);
  595. if (status == 0) {
  596. spin_unlock(&chip->reg_lock);
  597. return IRQ_NONE;
  598. }
  599. for (i = 0; i < MAX_ICH6_DEV; i++) {
  600. azx_dev = &chip->azx_dev[i];
  601. if (status & azx_dev->sd_int_sta_mask) {
  602. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  603. if (azx_dev->substream && azx_dev->running) {
  604. spin_unlock(&chip->reg_lock);
  605. snd_pcm_period_elapsed(azx_dev->substream);
  606. spin_lock(&chip->reg_lock);
  607. }
  608. }
  609. }
  610. /* clear rirb int */
  611. status = azx_readb(chip, RIRBSTS);
  612. if (status & RIRB_INT_MASK) {
  613. if (status & RIRB_INT_RESPONSE)
  614. azx_update_rirb(chip);
  615. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  616. }
  617. #if 0
  618. /* clear state status int */
  619. if (azx_readb(chip, STATESTS) & 0x04)
  620. azx_writeb(chip, STATESTS, 0x04);
  621. #endif
  622. spin_unlock(&chip->reg_lock);
  623. return IRQ_HANDLED;
  624. }
  625. /*
  626. * set up BDL entries
  627. */
  628. static void azx_setup_periods(azx_dev_t *azx_dev)
  629. {
  630. u32 *bdl = azx_dev->bdl;
  631. dma_addr_t dma_addr = azx_dev->substream->runtime->dma_addr;
  632. int idx;
  633. /* reset BDL address */
  634. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  635. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  636. /* program the initial BDL entries */
  637. for (idx = 0; idx < azx_dev->frags; idx++) {
  638. unsigned int off = idx << 2; /* 4 dword step */
  639. dma_addr_t addr = dma_addr + idx * azx_dev->fragsize;
  640. /* program the address field of the BDL entry */
  641. bdl[off] = cpu_to_le32((u32)addr);
  642. bdl[off+1] = cpu_to_le32(upper_32bit(addr));
  643. /* program the size field of the BDL entry */
  644. bdl[off+2] = cpu_to_le32(azx_dev->fragsize);
  645. /* program the IOC to enable interrupt when buffer completes */
  646. bdl[off+3] = cpu_to_le32(0x01);
  647. }
  648. }
  649. /*
  650. * set up the SD for streaming
  651. */
  652. static int azx_setup_controller(azx_t *chip, azx_dev_t *azx_dev)
  653. {
  654. unsigned char val;
  655. int timeout;
  656. /* make sure the run bit is zero for SD */
  657. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) & ~SD_CTL_DMA_START);
  658. /* reset stream */
  659. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) | SD_CTL_STREAM_RESET);
  660. udelay(3);
  661. timeout = 300;
  662. while (!((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  663. --timeout)
  664. ;
  665. val &= ~SD_CTL_STREAM_RESET;
  666. azx_sd_writeb(azx_dev, SD_CTL, val);
  667. udelay(3);
  668. timeout = 300;
  669. /* waiting for hardware to report that the stream is out of reset */
  670. while (((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  671. --timeout)
  672. ;
  673. /* program the stream_tag */
  674. azx_sd_writel(azx_dev, SD_CTL,
  675. (azx_sd_readl(azx_dev, SD_CTL) & ~SD_CTL_STREAM_TAG_MASK) |
  676. (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT));
  677. /* program the length of samples in cyclic buffer */
  678. azx_sd_writel(azx_dev, SD_CBL, azx_dev->bufsize);
  679. /* program the stream format */
  680. /* this value needs to be the same as the one programmed */
  681. azx_sd_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
  682. /* program the stream LVI (last valid index) of the BDL */
  683. azx_sd_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
  684. /* program the BDL address */
  685. /* lower BDL address */
  686. azx_sd_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl_addr);
  687. /* upper BDL address */
  688. azx_sd_writel(azx_dev, SD_BDLPU, upper_32bit(azx_dev->bdl_addr));
  689. if (chip->position_fix == POS_FIX_POSBUF) {
  690. /* enable the position buffer */
  691. if (! (azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
  692. azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
  693. }
  694. /* set the interrupt enable bits in the descriptor control register */
  695. azx_sd_writel(azx_dev, SD_CTL, azx_sd_readl(azx_dev, SD_CTL) | SD_INT_MASK);
  696. return 0;
  697. }
  698. /*
  699. * Codec initialization
  700. */
  701. static int __devinit azx_codec_create(azx_t *chip, const char *model)
  702. {
  703. struct hda_bus_template bus_temp;
  704. int c, codecs, err;
  705. memset(&bus_temp, 0, sizeof(bus_temp));
  706. bus_temp.private_data = chip;
  707. bus_temp.modelname = model;
  708. bus_temp.pci = chip->pci;
  709. bus_temp.ops.command = azx_send_cmd;
  710. bus_temp.ops.get_response = azx_get_response;
  711. if ((err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus)) < 0)
  712. return err;
  713. codecs = 0;
  714. for (c = 0; c < AZX_MAX_CODECS; c++) {
  715. if (chip->codec_mask & (1 << c)) {
  716. err = snd_hda_codec_new(chip->bus, c, NULL);
  717. if (err < 0)
  718. continue;
  719. codecs++;
  720. }
  721. }
  722. if (! codecs) {
  723. snd_printk(KERN_ERR SFX "no codecs initialized\n");
  724. return -ENXIO;
  725. }
  726. return 0;
  727. }
  728. /*
  729. * PCM support
  730. */
  731. /* assign a stream for the PCM */
  732. static inline azx_dev_t *azx_assign_device(azx_t *chip, int stream)
  733. {
  734. int dev, i;
  735. dev = stream == SNDRV_PCM_STREAM_PLAYBACK ? 4 : 0;
  736. for (i = 0; i < 4; i++, dev++)
  737. if (! chip->azx_dev[dev].opened) {
  738. chip->azx_dev[dev].opened = 1;
  739. return &chip->azx_dev[dev];
  740. }
  741. return NULL;
  742. }
  743. /* release the assigned stream */
  744. static inline void azx_release_device(azx_dev_t *azx_dev)
  745. {
  746. azx_dev->opened = 0;
  747. }
  748. static snd_pcm_hardware_t azx_pcm_hw = {
  749. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  750. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  751. SNDRV_PCM_INFO_MMAP_VALID |
  752. SNDRV_PCM_INFO_PAUSE |
  753. SNDRV_PCM_INFO_RESUME),
  754. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  755. .rates = SNDRV_PCM_RATE_48000,
  756. .rate_min = 48000,
  757. .rate_max = 48000,
  758. .channels_min = 2,
  759. .channels_max = 2,
  760. .buffer_bytes_max = AZX_MAX_BUF_SIZE,
  761. .period_bytes_min = 128,
  762. .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
  763. .periods_min = 2,
  764. .periods_max = AZX_MAX_FRAG,
  765. .fifo_size = 0,
  766. };
  767. struct azx_pcm {
  768. azx_t *chip;
  769. struct hda_codec *codec;
  770. struct hda_pcm_stream *hinfo[2];
  771. };
  772. static int azx_pcm_open(snd_pcm_substream_t *substream)
  773. {
  774. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  775. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  776. azx_t *chip = apcm->chip;
  777. azx_dev_t *azx_dev;
  778. snd_pcm_runtime_t *runtime = substream->runtime;
  779. unsigned long flags;
  780. int err;
  781. down(&chip->open_mutex);
  782. azx_dev = azx_assign_device(chip, substream->stream);
  783. if (azx_dev == NULL) {
  784. up(&chip->open_mutex);
  785. return -EBUSY;
  786. }
  787. runtime->hw = azx_pcm_hw;
  788. runtime->hw.channels_min = hinfo->channels_min;
  789. runtime->hw.channels_max = hinfo->channels_max;
  790. runtime->hw.formats = hinfo->formats;
  791. runtime->hw.rates = hinfo->rates;
  792. snd_pcm_limit_hw_rates(runtime);
  793. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  794. if ((err = hinfo->ops.open(hinfo, apcm->codec, substream)) < 0) {
  795. azx_release_device(azx_dev);
  796. up(&chip->open_mutex);
  797. return err;
  798. }
  799. spin_lock_irqsave(&chip->reg_lock, flags);
  800. azx_dev->substream = substream;
  801. azx_dev->running = 0;
  802. spin_unlock_irqrestore(&chip->reg_lock, flags);
  803. runtime->private_data = azx_dev;
  804. up(&chip->open_mutex);
  805. return 0;
  806. }
  807. static int azx_pcm_close(snd_pcm_substream_t *substream)
  808. {
  809. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  810. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  811. azx_t *chip = apcm->chip;
  812. azx_dev_t *azx_dev = get_azx_dev(substream);
  813. unsigned long flags;
  814. down(&chip->open_mutex);
  815. spin_lock_irqsave(&chip->reg_lock, flags);
  816. azx_dev->substream = NULL;
  817. azx_dev->running = 0;
  818. spin_unlock_irqrestore(&chip->reg_lock, flags);
  819. azx_release_device(azx_dev);
  820. hinfo->ops.close(hinfo, apcm->codec, substream);
  821. up(&chip->open_mutex);
  822. return 0;
  823. }
  824. static int azx_pcm_hw_params(snd_pcm_substream_t *substream, snd_pcm_hw_params_t *hw_params)
  825. {
  826. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  827. }
  828. static int azx_pcm_hw_free(snd_pcm_substream_t *substream)
  829. {
  830. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  831. azx_dev_t *azx_dev = get_azx_dev(substream);
  832. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  833. /* reset BDL address */
  834. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  835. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  836. azx_sd_writel(azx_dev, SD_CTL, 0);
  837. hinfo->ops.cleanup(hinfo, apcm->codec, substream);
  838. return snd_pcm_lib_free_pages(substream);
  839. }
  840. static int azx_pcm_prepare(snd_pcm_substream_t *substream)
  841. {
  842. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  843. azx_t *chip = apcm->chip;
  844. azx_dev_t *azx_dev = get_azx_dev(substream);
  845. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  846. snd_pcm_runtime_t *runtime = substream->runtime;
  847. azx_dev->bufsize = snd_pcm_lib_buffer_bytes(substream);
  848. azx_dev->fragsize = snd_pcm_lib_period_bytes(substream);
  849. azx_dev->frags = azx_dev->bufsize / azx_dev->fragsize;
  850. azx_dev->format_val = snd_hda_calc_stream_format(runtime->rate,
  851. runtime->channels,
  852. runtime->format,
  853. hinfo->maxbps);
  854. if (! azx_dev->format_val) {
  855. snd_printk(KERN_ERR SFX "invalid format_val, rate=%d, ch=%d, format=%d\n",
  856. runtime->rate, runtime->channels, runtime->format);
  857. return -EINVAL;
  858. }
  859. snd_printdd("azx_pcm_prepare: bufsize=0x%x, fragsize=0x%x, format=0x%x\n",
  860. azx_dev->bufsize, azx_dev->fragsize, azx_dev->format_val);
  861. azx_setup_periods(azx_dev);
  862. azx_setup_controller(chip, azx_dev);
  863. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  864. azx_dev->fifo_size = azx_sd_readw(azx_dev, SD_FIFOSIZE) + 1;
  865. else
  866. azx_dev->fifo_size = 0;
  867. return hinfo->ops.prepare(hinfo, apcm->codec, azx_dev->stream_tag,
  868. azx_dev->format_val, substream);
  869. }
  870. static int azx_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  871. {
  872. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  873. azx_dev_t *azx_dev = get_azx_dev(substream);
  874. azx_t *chip = apcm->chip;
  875. int err = 0;
  876. spin_lock(&chip->reg_lock);
  877. switch (cmd) {
  878. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  879. case SNDRV_PCM_TRIGGER_RESUME:
  880. case SNDRV_PCM_TRIGGER_START:
  881. azx_stream_start(chip, azx_dev);
  882. azx_dev->running = 1;
  883. break;
  884. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  885. case SNDRV_PCM_TRIGGER_STOP:
  886. azx_stream_stop(chip, azx_dev);
  887. azx_dev->running = 0;
  888. break;
  889. default:
  890. err = -EINVAL;
  891. }
  892. spin_unlock(&chip->reg_lock);
  893. if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH ||
  894. cmd == SNDRV_PCM_TRIGGER_STOP) {
  895. int timeout = 5000;
  896. while (azx_sd_readb(azx_dev, SD_CTL) & SD_CTL_DMA_START && --timeout)
  897. ;
  898. }
  899. return err;
  900. }
  901. static snd_pcm_uframes_t azx_pcm_pointer(snd_pcm_substream_t *substream)
  902. {
  903. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  904. azx_t *chip = apcm->chip;
  905. azx_dev_t *azx_dev = get_azx_dev(substream);
  906. unsigned int pos;
  907. if (chip->position_fix == POS_FIX_POSBUF) {
  908. /* use the position buffer */
  909. pos = *azx_dev->posbuf;
  910. } else {
  911. /* read LPIB */
  912. pos = azx_sd_readl(azx_dev, SD_LPIB);
  913. if (chip->position_fix == POS_FIX_FIFO)
  914. pos += azx_dev->fifo_size;
  915. }
  916. if (pos >= azx_dev->bufsize)
  917. pos = 0;
  918. return bytes_to_frames(substream->runtime, pos);
  919. }
  920. static snd_pcm_ops_t azx_pcm_ops = {
  921. .open = azx_pcm_open,
  922. .close = azx_pcm_close,
  923. .ioctl = snd_pcm_lib_ioctl,
  924. .hw_params = azx_pcm_hw_params,
  925. .hw_free = azx_pcm_hw_free,
  926. .prepare = azx_pcm_prepare,
  927. .trigger = azx_pcm_trigger,
  928. .pointer = azx_pcm_pointer,
  929. };
  930. static void azx_pcm_free(snd_pcm_t *pcm)
  931. {
  932. kfree(pcm->private_data);
  933. }
  934. static int __devinit create_codec_pcm(azx_t *chip, struct hda_codec *codec,
  935. struct hda_pcm *cpcm, int pcm_dev)
  936. {
  937. int err;
  938. snd_pcm_t *pcm;
  939. struct azx_pcm *apcm;
  940. snd_assert(cpcm->stream[0].substreams || cpcm->stream[1].substreams, return -EINVAL);
  941. snd_assert(cpcm->name, return -EINVAL);
  942. err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
  943. cpcm->stream[0].substreams, cpcm->stream[1].substreams,
  944. &pcm);
  945. if (err < 0)
  946. return err;
  947. strcpy(pcm->name, cpcm->name);
  948. apcm = kmalloc(sizeof(*apcm), GFP_KERNEL);
  949. if (apcm == NULL)
  950. return -ENOMEM;
  951. apcm->chip = chip;
  952. apcm->codec = codec;
  953. apcm->hinfo[0] = &cpcm->stream[0];
  954. apcm->hinfo[1] = &cpcm->stream[1];
  955. pcm->private_data = apcm;
  956. pcm->private_free = azx_pcm_free;
  957. if (cpcm->stream[0].substreams)
  958. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &azx_pcm_ops);
  959. if (cpcm->stream[1].substreams)
  960. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &azx_pcm_ops);
  961. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  962. snd_dma_pci_data(chip->pci),
  963. 1024 * 64, 1024 * 128);
  964. chip->pcm[pcm_dev] = pcm;
  965. return 0;
  966. }
  967. static int __devinit azx_pcm_create(azx_t *chip)
  968. {
  969. struct list_head *p;
  970. struct hda_codec *codec;
  971. int c, err;
  972. int pcm_dev;
  973. if ((err = snd_hda_build_pcms(chip->bus)) < 0)
  974. return err;
  975. pcm_dev = 0;
  976. list_for_each(p, &chip->bus->codec_list) {
  977. codec = list_entry(p, struct hda_codec, list);
  978. for (c = 0; c < codec->num_pcms; c++) {
  979. if (pcm_dev >= AZX_MAX_PCMS) {
  980. snd_printk(KERN_ERR SFX "Too many PCMs\n");
  981. return -EINVAL;
  982. }
  983. err = create_codec_pcm(chip, codec, &codec->pcm_info[c], pcm_dev);
  984. if (err < 0)
  985. return err;
  986. pcm_dev++;
  987. }
  988. }
  989. return 0;
  990. }
  991. /*
  992. * mixer creation - all stuff is implemented in hda module
  993. */
  994. static int __devinit azx_mixer_create(azx_t *chip)
  995. {
  996. return snd_hda_build_controls(chip->bus);
  997. }
  998. /*
  999. * initialize SD streams
  1000. */
  1001. static int __devinit azx_init_stream(azx_t *chip)
  1002. {
  1003. int i;
  1004. /* initialize each stream (aka device)
  1005. * assign the starting bdl address to each stream (device) and initialize
  1006. */
  1007. for (i = 0; i < MAX_ICH6_DEV; i++) {
  1008. unsigned int off = sizeof(u32) * (i * AZX_MAX_FRAG * 4);
  1009. azx_dev_t *azx_dev = &chip->azx_dev[i];
  1010. azx_dev->bdl = (u32 *)(chip->bdl.area + off);
  1011. azx_dev->bdl_addr = chip->bdl.addr + off;
  1012. if (chip->position_fix == POS_FIX_POSBUF)
  1013. azx_dev->posbuf = (volatile u32 *)(chip->posbuf.area + i * 8);
  1014. /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  1015. azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
  1016. /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
  1017. azx_dev->sd_int_sta_mask = 1 << i;
  1018. /* stream tag: must be non-zero and unique */
  1019. azx_dev->index = i;
  1020. azx_dev->stream_tag = i + 1;
  1021. }
  1022. return 0;
  1023. }
  1024. #ifdef CONFIG_PM
  1025. /*
  1026. * power management
  1027. */
  1028. static int azx_suspend(snd_card_t *card, pm_message_t state)
  1029. {
  1030. azx_t *chip = card->pm_private_data;
  1031. int i;
  1032. for (i = 0; i < chip->pcm_devs; i++)
  1033. if (chip->pcm[i])
  1034. snd_pcm_suspend_all(chip->pcm[i]);
  1035. snd_hda_suspend(chip->bus, state);
  1036. azx_free_cmd_io(chip);
  1037. pci_disable_device(chip->pci);
  1038. return 0;
  1039. }
  1040. static int azx_resume(snd_card_t *card)
  1041. {
  1042. azx_t *chip = card->pm_private_data;
  1043. pci_enable_device(chip->pci);
  1044. pci_set_master(chip->pci);
  1045. azx_init_chip(chip);
  1046. snd_hda_resume(chip->bus);
  1047. return 0;
  1048. }
  1049. #endif /* CONFIG_PM */
  1050. /*
  1051. * destructor
  1052. */
  1053. static int azx_free(azx_t *chip)
  1054. {
  1055. if (chip->initialized) {
  1056. int i;
  1057. for (i = 0; i < MAX_ICH6_DEV; i++)
  1058. azx_stream_stop(chip, &chip->azx_dev[i]);
  1059. /* disable interrupts */
  1060. azx_int_disable(chip);
  1061. azx_int_clear(chip);
  1062. /* disable CORB/RIRB */
  1063. azx_free_cmd_io(chip);
  1064. /* disable position buffer */
  1065. azx_writel(chip, DPLBASE, 0);
  1066. azx_writel(chip, DPUBASE, 0);
  1067. /* wait a little for interrupts to finish */
  1068. msleep(1);
  1069. iounmap(chip->remap_addr);
  1070. }
  1071. if (chip->irq >= 0)
  1072. free_irq(chip->irq, (void*)chip);
  1073. if (chip->bdl.area)
  1074. snd_dma_free_pages(&chip->bdl);
  1075. if (chip->rb.area)
  1076. snd_dma_free_pages(&chip->rb);
  1077. if (chip->posbuf.area)
  1078. snd_dma_free_pages(&chip->posbuf);
  1079. pci_release_regions(chip->pci);
  1080. pci_disable_device(chip->pci);
  1081. kfree(chip);
  1082. return 0;
  1083. }
  1084. static int azx_dev_free(snd_device_t *device)
  1085. {
  1086. return azx_free(device->device_data);
  1087. }
  1088. /*
  1089. * constructor
  1090. */
  1091. static int __devinit azx_create(snd_card_t *card, struct pci_dev *pci,
  1092. int posfix, azx_t **rchip)
  1093. {
  1094. azx_t *chip;
  1095. int err = 0;
  1096. static snd_device_ops_t ops = {
  1097. .dev_free = azx_dev_free,
  1098. };
  1099. *rchip = NULL;
  1100. if ((err = pci_enable_device(pci)) < 0)
  1101. return err;
  1102. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1103. if (NULL == chip) {
  1104. snd_printk(KERN_ERR SFX "cannot allocate chip\n");
  1105. pci_disable_device(pci);
  1106. return -ENOMEM;
  1107. }
  1108. spin_lock_init(&chip->reg_lock);
  1109. init_MUTEX(&chip->open_mutex);
  1110. chip->card = card;
  1111. chip->pci = pci;
  1112. chip->irq = -1;
  1113. chip->position_fix = posfix;
  1114. if ((err = pci_request_regions(pci, "ICH HD audio")) < 0) {
  1115. kfree(chip);
  1116. pci_disable_device(pci);
  1117. return err;
  1118. }
  1119. chip->addr = pci_resource_start(pci,0);
  1120. chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci,0));
  1121. if (chip->remap_addr == NULL) {
  1122. snd_printk(KERN_ERR SFX "ioremap error\n");
  1123. err = -ENXIO;
  1124. goto errout;
  1125. }
  1126. if (request_irq(pci->irq, azx_interrupt, SA_INTERRUPT|SA_SHIRQ,
  1127. "HDA Intel", (void*)chip)) {
  1128. snd_printk(KERN_ERR SFX "unable to grab IRQ %d\n", pci->irq);
  1129. err = -EBUSY;
  1130. goto errout;
  1131. }
  1132. chip->irq = pci->irq;
  1133. pci_set_master(pci);
  1134. synchronize_irq(chip->irq);
  1135. /* allocate memory for the BDL for each stream */
  1136. if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  1137. PAGE_SIZE, &chip->bdl)) < 0) {
  1138. snd_printk(KERN_ERR SFX "cannot allocate BDL\n");
  1139. goto errout;
  1140. }
  1141. if (chip->position_fix == POS_FIX_POSBUF) {
  1142. /* allocate memory for the position buffer */
  1143. if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  1144. MAX_ICH6_DEV * 8, &chip->posbuf)) < 0) {
  1145. snd_printk(KERN_ERR SFX "cannot allocate posbuf\n");
  1146. goto errout;
  1147. }
  1148. }
  1149. /* allocate CORB/RIRB */
  1150. if ((err = azx_alloc_cmd_io(chip)) < 0)
  1151. goto errout;
  1152. /* initialize streams */
  1153. azx_init_stream(chip);
  1154. /* initialize chip */
  1155. azx_init_chip(chip);
  1156. chip->initialized = 1;
  1157. /* codec detection */
  1158. if (! chip->codec_mask) {
  1159. snd_printk(KERN_ERR SFX "no codecs found!\n");
  1160. err = -ENODEV;
  1161. goto errout;
  1162. }
  1163. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) <0) {
  1164. snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
  1165. goto errout;
  1166. }
  1167. *rchip = chip;
  1168. return 0;
  1169. errout:
  1170. azx_free(chip);
  1171. return err;
  1172. }
  1173. static int __devinit azx_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  1174. {
  1175. static int dev;
  1176. snd_card_t *card;
  1177. azx_t *chip;
  1178. int err = 0;
  1179. if (dev >= SNDRV_CARDS)
  1180. return -ENODEV;
  1181. if (! enable[dev]) {
  1182. dev++;
  1183. return -ENOENT;
  1184. }
  1185. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1186. if (NULL == card) {
  1187. snd_printk(KERN_ERR SFX "Error creating card!\n");
  1188. return -ENOMEM;
  1189. }
  1190. if ((err = azx_create(card, pci, position_fix[dev], &chip)) < 0) {
  1191. snd_card_free(card);
  1192. return err;
  1193. }
  1194. strcpy(card->driver, "HDA-Intel");
  1195. strcpy(card->shortname, "HDA Intel");
  1196. sprintf(card->longname, "%s at 0x%lx irq %i", card->shortname, chip->addr, chip->irq);
  1197. /* create codec instances */
  1198. if ((err = azx_codec_create(chip, model[dev])) < 0) {
  1199. snd_card_free(card);
  1200. return err;
  1201. }
  1202. /* create PCM streams */
  1203. if ((err = azx_pcm_create(chip)) < 0) {
  1204. snd_card_free(card);
  1205. return err;
  1206. }
  1207. /* create mixer controls */
  1208. if ((err = azx_mixer_create(chip)) < 0) {
  1209. snd_card_free(card);
  1210. return err;
  1211. }
  1212. snd_card_set_pm_callback(card, azx_suspend, azx_resume, chip);
  1213. snd_card_set_dev(card, &pci->dev);
  1214. if ((err = snd_card_register(card)) < 0) {
  1215. snd_card_free(card);
  1216. return err;
  1217. }
  1218. pci_set_drvdata(pci, card);
  1219. dev++;
  1220. return err;
  1221. }
  1222. static void __devexit azx_remove(struct pci_dev *pci)
  1223. {
  1224. snd_card_free(pci_get_drvdata(pci));
  1225. pci_set_drvdata(pci, NULL);
  1226. }
  1227. /* PCI IDs */
  1228. static struct pci_device_id azx_ids[] = {
  1229. { 0x8086, 0x2668, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* ICH6 */
  1230. { 0x8086, 0x27d8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* ICH7 */
  1231. { 0x8086, 0x269a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* ESB2 */
  1232. { 0x1002, 0x437b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* ATI SB450 */
  1233. { 0x1106, 0x3288, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* VIA VT8251/VT8237A */
  1234. { 0x10b9, 0x5461, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* ALI 5461? */
  1235. { 0, }
  1236. };
  1237. MODULE_DEVICE_TABLE(pci, azx_ids);
  1238. /* pci_driver definition */
  1239. static struct pci_driver driver = {
  1240. .name = "HDA Intel",
  1241. .id_table = azx_ids,
  1242. .probe = azx_probe,
  1243. .remove = __devexit_p(azx_remove),
  1244. SND_PCI_PM_CALLBACKS
  1245. };
  1246. static int __init alsa_card_azx_init(void)
  1247. {
  1248. return pci_register_driver(&driver);
  1249. }
  1250. static void __exit alsa_card_azx_exit(void)
  1251. {
  1252. pci_unregister_driver(&driver);
  1253. }
  1254. module_init(alsa_card_azx_init)
  1255. module_exit(alsa_card_azx_exit)