hda_intel.c 38 KB

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