hda_intel.c 38 KB

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