hda_intel.c 36 KB

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