hda_intel.c 38 KB

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