hda_intel.c 41 KB

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