hda_intel.c 42 KB

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