PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/sound/pci/hda/hda_intel.c

https://bitbucket.org/abioy/linux
C | 2807 lines | 2063 code | 346 blank | 398 comment | 269 complexity | cb77fb430aecd7f7b5b87c495ed138d2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. *
  3. * hda_intel.c - Implementation of primary alsa driver code base
  4. * for Intel HD Audio.
  5. *
  6. * Copyright(c) 2004 Intel Corporation. All rights reserved.
  7. *
  8. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  9. * PeiSen Hou <pshou@realtek.com.tw>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program; if not, write to the Free Software Foundation, Inc., 59
  23. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. * CONTACTS:
  26. *
  27. * Matt Jared matt.jared@intel.com
  28. * Andy Kopp andy.kopp@intel.com
  29. * Dan Kogan dan.d.kogan@intel.com
  30. *
  31. * CHANGES:
  32. *
  33. * 2004.12.01 Major rewrite by tiwai, merged the work of pshou
  34. *
  35. */
  36. #include <asm/io.h>
  37. #include <linux/delay.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/moduleparam.h>
  43. #include <linux/init.h>
  44. #include <linux/slab.h>
  45. #include <linux/pci.h>
  46. #include <linux/mutex.h>
  47. #include <linux/reboot.h>
  48. #include <sound/core.h>
  49. #include <sound/initval.h>
  50. #include "hda_codec.h"
  51. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  52. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  53. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  54. static char *model[SNDRV_CARDS];
  55. static int position_fix[SNDRV_CARDS];
  56. static int bdl_pos_adj[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
  57. static int probe_mask[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
  58. static int probe_only[SNDRV_CARDS];
  59. static int single_cmd;
  60. static int enable_msi = -1;
  61. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  62. static char *patch[SNDRV_CARDS];
  63. #endif
  64. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  65. static int beep_mode[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] =
  66. CONFIG_SND_HDA_INPUT_BEEP_MODE};
  67. #endif
  68. module_param_array(index, int, NULL, 0444);
  69. MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
  70. module_param_array(id, charp, NULL, 0444);
  71. MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
  72. module_param_array(enable, bool, NULL, 0444);
  73. MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
  74. module_param_array(model, charp, NULL, 0444);
  75. MODULE_PARM_DESC(model, "Use the given board model.");
  76. module_param_array(position_fix, int, NULL, 0444);
  77. MODULE_PARM_DESC(position_fix, "Fix DMA pointer "
  78. "(0 = auto, 1 = none, 2 = POSBUF).");
  79. module_param_array(bdl_pos_adj, int, NULL, 0644);
  80. MODULE_PARM_DESC(bdl_pos_adj, "BDL position adjustment offset.");
  81. module_param_array(probe_mask, int, NULL, 0444);
  82. MODULE_PARM_DESC(probe_mask, "Bitmask to probe codecs (default = -1).");
  83. module_param_array(probe_only, bool, NULL, 0444);
  84. MODULE_PARM_DESC(probe_only, "Only probing and no codec initialization.");
  85. module_param(single_cmd, bool, 0444);
  86. MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
  87. "(for debugging only).");
  88. module_param(enable_msi, int, 0444);
  89. MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
  90. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  91. module_param_array(patch, charp, NULL, 0444);
  92. MODULE_PARM_DESC(patch, "Patch file for Intel HD audio interface.");
  93. #endif
  94. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  95. module_param_array(beep_mode, int, NULL, 0444);
  96. MODULE_PARM_DESC(beep_mode, "Select HDA Beep registration mode "
  97. "(0=off, 1=on, 2=mute switch on/off) (default=1).");
  98. #endif
  99. #ifdef CONFIG_SND_HDA_POWER_SAVE
  100. static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
  101. module_param(power_save, int, 0644);
  102. MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
  103. "(in second, 0 = disable).");
  104. /* reset the HD-audio controller in power save mode.
  105. * this may give more power-saving, but will take longer time to
  106. * wake up.
  107. */
  108. static int power_save_controller = 1;
  109. module_param(power_save_controller, bool, 0644);
  110. MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
  111. #endif
  112. MODULE_LICENSE("GPL");
  113. MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
  114. "{Intel, ICH6M},"
  115. "{Intel, ICH7},"
  116. "{Intel, ESB2},"
  117. "{Intel, ICH8},"
  118. "{Intel, ICH9},"
  119. "{Intel, ICH10},"
  120. "{Intel, PCH},"
  121. "{Intel, CPT},"
  122. "{Intel, SCH},"
  123. "{ATI, SB450},"
  124. "{ATI, SB600},"
  125. "{ATI, RS600},"
  126. "{ATI, RS690},"
  127. "{ATI, RS780},"
  128. "{ATI, R600},"
  129. "{ATI, RV630},"
  130. "{ATI, RV610},"
  131. "{ATI, RV670},"
  132. "{ATI, RV635},"
  133. "{ATI, RV620},"
  134. "{ATI, RV770},"
  135. "{VIA, VT8251},"
  136. "{VIA, VT8237A},"
  137. "{SiS, SIS966},"
  138. "{ULI, M5461}}");
  139. MODULE_DESCRIPTION("Intel HDA driver");
  140. #ifdef CONFIG_SND_VERBOSE_PRINTK
  141. #define SFX /* nop */
  142. #else
  143. #define SFX "hda-intel: "
  144. #endif
  145. /*
  146. * registers
  147. */
  148. #define ICH6_REG_GCAP 0x00
  149. #define ICH6_GCAP_64OK (1 << 0) /* 64bit address support */
  150. #define ICH6_GCAP_NSDO (3 << 1) /* # of serial data out signals */
  151. #define ICH6_GCAP_BSS (31 << 3) /* # of bidirectional streams */
  152. #define ICH6_GCAP_ISS (15 << 8) /* # of input streams */
  153. #define ICH6_GCAP_OSS (15 << 12) /* # of output streams */
  154. #define ICH6_REG_VMIN 0x02
  155. #define ICH6_REG_VMAJ 0x03
  156. #define ICH6_REG_OUTPAY 0x04
  157. #define ICH6_REG_INPAY 0x06
  158. #define ICH6_REG_GCTL 0x08
  159. #define ICH6_GCTL_RESET (1 << 0) /* controller reset */
  160. #define ICH6_GCTL_FCNTRL (1 << 1) /* flush control */
  161. #define ICH6_GCTL_UNSOL (1 << 8) /* accept unsol. response enable */
  162. #define ICH6_REG_WAKEEN 0x0c
  163. #define ICH6_REG_STATESTS 0x0e
  164. #define ICH6_REG_GSTS 0x10
  165. #define ICH6_GSTS_FSTS (1 << 1) /* flush status */
  166. #define ICH6_REG_INTCTL 0x20
  167. #define ICH6_REG_INTSTS 0x24
  168. #define ICH6_REG_WALCLK 0x30
  169. #define ICH6_REG_SYNC 0x34
  170. #define ICH6_REG_CORBLBASE 0x40
  171. #define ICH6_REG_CORBUBASE 0x44
  172. #define ICH6_REG_CORBWP 0x48
  173. #define ICH6_REG_CORBRP 0x4a
  174. #define ICH6_CORBRP_RST (1 << 15) /* read pointer reset */
  175. #define ICH6_REG_CORBCTL 0x4c
  176. #define ICH6_CORBCTL_RUN (1 << 1) /* enable DMA */
  177. #define ICH6_CORBCTL_CMEIE (1 << 0) /* enable memory error irq */
  178. #define ICH6_REG_CORBSTS 0x4d
  179. #define ICH6_CORBSTS_CMEI (1 << 0) /* memory error indication */
  180. #define ICH6_REG_CORBSIZE 0x4e
  181. #define ICH6_REG_RIRBLBASE 0x50
  182. #define ICH6_REG_RIRBUBASE 0x54
  183. #define ICH6_REG_RIRBWP 0x58
  184. #define ICH6_RIRBWP_RST (1 << 15) /* write pointer reset */
  185. #define ICH6_REG_RINTCNT 0x5a
  186. #define ICH6_REG_RIRBCTL 0x5c
  187. #define ICH6_RBCTL_IRQ_EN (1 << 0) /* enable IRQ */
  188. #define ICH6_RBCTL_DMA_EN (1 << 1) /* enable DMA */
  189. #define ICH6_RBCTL_OVERRUN_EN (1 << 2) /* enable overrun irq */
  190. #define ICH6_REG_RIRBSTS 0x5d
  191. #define ICH6_RBSTS_IRQ (1 << 0) /* response irq */
  192. #define ICH6_RBSTS_OVERRUN (1 << 2) /* overrun irq */
  193. #define ICH6_REG_RIRBSIZE 0x5e
  194. #define ICH6_REG_IC 0x60
  195. #define ICH6_REG_IR 0x64
  196. #define ICH6_REG_IRS 0x68
  197. #define ICH6_IRS_VALID (1<<1)
  198. #define ICH6_IRS_BUSY (1<<0)
  199. #define ICH6_REG_DPLBASE 0x70
  200. #define ICH6_REG_DPUBASE 0x74
  201. #define ICH6_DPLBASE_ENABLE 0x1 /* Enable position buffer */
  202. /* SD offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  203. enum { SDI0, SDI1, SDI2, SDI3, SDO0, SDO1, SDO2, SDO3 };
  204. /* stream register offsets from stream base */
  205. #define ICH6_REG_SD_CTL 0x00
  206. #define ICH6_REG_SD_STS 0x03
  207. #define ICH6_REG_SD_LPIB 0x04
  208. #define ICH6_REG_SD_CBL 0x08
  209. #define ICH6_REG_SD_LVI 0x0c
  210. #define ICH6_REG_SD_FIFOW 0x0e
  211. #define ICH6_REG_SD_FIFOSIZE 0x10
  212. #define ICH6_REG_SD_FORMAT 0x12
  213. #define ICH6_REG_SD_BDLPL 0x18
  214. #define ICH6_REG_SD_BDLPU 0x1c
  215. /* PCI space */
  216. #define ICH6_PCIREG_TCSEL 0x44
  217. /*
  218. * other constants
  219. */
  220. /* max number of SDs */
  221. /* ICH, ATI and VIA have 4 playback and 4 capture */
  222. #define ICH6_NUM_CAPTURE 4
  223. #define ICH6_NUM_PLAYBACK 4
  224. /* ULI has 6 playback and 5 capture */
  225. #define ULI_NUM_CAPTURE 5
  226. #define ULI_NUM_PLAYBACK 6
  227. /* ATI HDMI has 1 playback and 0 capture */
  228. #define ATIHDMI_NUM_CAPTURE 0
  229. #define ATIHDMI_NUM_PLAYBACK 1
  230. /* TERA has 4 playback and 3 capture */
  231. #define TERA_NUM_CAPTURE 3
  232. #define TERA_NUM_PLAYBACK 4
  233. /* this number is statically defined for simplicity */
  234. #define MAX_AZX_DEV 16
  235. /* max number of fragments - we may use more if allocating more pages for BDL */
  236. #define BDL_SIZE 4096
  237. #define AZX_MAX_BDL_ENTRIES (BDL_SIZE / 16)
  238. #define AZX_MAX_FRAG 32
  239. /* max buffer size - no h/w limit, you can increase as you like */
  240. #define AZX_MAX_BUF_SIZE (1024*1024*1024)
  241. /* RIRB int mask: overrun[2], response[0] */
  242. #define RIRB_INT_RESPONSE 0x01
  243. #define RIRB_INT_OVERRUN 0x04
  244. #define RIRB_INT_MASK 0x05
  245. /* STATESTS int mask: S3,SD2,SD1,SD0 */
  246. #define AZX_MAX_CODECS 8
  247. #define AZX_DEFAULT_CODECS 4
  248. #define STATESTS_INT_MASK ((1 << AZX_MAX_CODECS) - 1)
  249. /* SD_CTL bits */
  250. #define SD_CTL_STREAM_RESET 0x01 /* stream reset bit */
  251. #define SD_CTL_DMA_START 0x02 /* stream DMA start bit */
  252. #define SD_CTL_STRIPE (3 << 16) /* stripe control */
  253. #define SD_CTL_TRAFFIC_PRIO (1 << 18) /* traffic priority */
  254. #define SD_CTL_DIR (1 << 19) /* bi-directional stream */
  255. #define SD_CTL_STREAM_TAG_MASK (0xf << 20)
  256. #define SD_CTL_STREAM_TAG_SHIFT 20
  257. /* SD_CTL and SD_STS */
  258. #define SD_INT_DESC_ERR 0x10 /* descriptor error interrupt */
  259. #define SD_INT_FIFO_ERR 0x08 /* FIFO error interrupt */
  260. #define SD_INT_COMPLETE 0x04 /* completion interrupt */
  261. #define SD_INT_MASK (SD_INT_DESC_ERR|SD_INT_FIFO_ERR|\
  262. SD_INT_COMPLETE)
  263. /* SD_STS */
  264. #define SD_STS_FIFO_READY 0x20 /* FIFO ready */
  265. /* INTCTL and INTSTS */
  266. #define ICH6_INT_ALL_STREAM 0xff /* all stream interrupts */
  267. #define ICH6_INT_CTRL_EN 0x40000000 /* controller interrupt enable bit */
  268. #define ICH6_INT_GLOBAL_EN 0x80000000 /* global interrupt enable bit */
  269. /* below are so far hardcoded - should read registers in future */
  270. #define ICH6_MAX_CORB_ENTRIES 256
  271. #define ICH6_MAX_RIRB_ENTRIES 256
  272. /* position fix mode */
  273. enum {
  274. POS_FIX_AUTO,
  275. POS_FIX_LPIB,
  276. POS_FIX_POSBUF,
  277. };
  278. /* Defines for ATI HD Audio support in SB450 south bridge */
  279. #define ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR 0x42
  280. #define ATI_SB450_HDAUDIO_ENABLE_SNOOP 0x02
  281. /* Defines for Nvidia HDA support */
  282. #define NVIDIA_HDA_TRANSREG_ADDR 0x4e
  283. #define NVIDIA_HDA_ENABLE_COHBITS 0x0f
  284. #define NVIDIA_HDA_ISTRM_COH 0x4d
  285. #define NVIDIA_HDA_OSTRM_COH 0x4c
  286. #define NVIDIA_HDA_ENABLE_COHBIT 0x01
  287. /* Defines for Intel SCH HDA snoop control */
  288. #define INTEL_SCH_HDA_DEVC 0x78
  289. #define INTEL_SCH_HDA_DEVC_NOSNOOP (0x1<<11)
  290. /* Define IN stream 0 FIFO size offset in VIA controller */
  291. #define VIA_IN_STREAM0_FIFO_SIZE_OFFSET 0x90
  292. /* Define VIA HD Audio Device ID*/
  293. #define VIA_HDAC_DEVICE_ID 0x3288
  294. /* HD Audio class code */
  295. #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
  296. /*
  297. */
  298. struct azx_dev {
  299. struct snd_dma_buffer bdl; /* BDL buffer */
  300. u32 *posbuf; /* position buffer pointer */
  301. unsigned int bufsize; /* size of the play buffer in bytes */
  302. unsigned int period_bytes; /* size of the period in bytes */
  303. unsigned int frags; /* number for period in the play buffer */
  304. unsigned int fifo_size; /* FIFO size */
  305. unsigned long start_jiffies; /* start + minimum jiffies */
  306. unsigned long min_jiffies; /* minimum jiffies before position is valid */
  307. void __iomem *sd_addr; /* stream descriptor pointer */
  308. u32 sd_int_sta_mask; /* stream int status mask */
  309. /* pcm support */
  310. struct snd_pcm_substream *substream; /* assigned substream,
  311. * set in PCM open
  312. */
  313. unsigned int format_val; /* format value to be set in the
  314. * controller and the codec
  315. */
  316. unsigned char stream_tag; /* assigned stream */
  317. unsigned char index; /* stream index */
  318. int device; /* last device number assigned to */
  319. unsigned int opened :1;
  320. unsigned int running :1;
  321. unsigned int irq_pending :1;
  322. unsigned int start_flag: 1; /* stream full start flag */
  323. /*
  324. * For VIA:
  325. * A flag to ensure DMA position is 0
  326. * when link position is not greater than FIFO size
  327. */
  328. unsigned int insufficient :1;
  329. };
  330. /* CORB/RIRB */
  331. struct azx_rb {
  332. u32 *buf; /* CORB/RIRB buffer
  333. * Each CORB entry is 4byte, RIRB is 8byte
  334. */
  335. dma_addr_t addr; /* physical address of CORB/RIRB buffer */
  336. /* for RIRB */
  337. unsigned short rp, wp; /* read/write pointers */
  338. int cmds[AZX_MAX_CODECS]; /* number of pending requests */
  339. u32 res[AZX_MAX_CODECS]; /* last read value */
  340. };
  341. struct azx {
  342. struct snd_card *card;
  343. struct pci_dev *pci;
  344. int dev_index;
  345. /* chip type specific */
  346. int driver_type;
  347. int playback_streams;
  348. int playback_index_offset;
  349. int capture_streams;
  350. int capture_index_offset;
  351. int num_streams;
  352. /* pci resources */
  353. unsigned long addr;
  354. void __iomem *remap_addr;
  355. int irq;
  356. /* locks */
  357. spinlock_t reg_lock;
  358. struct mutex open_mutex;
  359. /* streams (x num_streams) */
  360. struct azx_dev *azx_dev;
  361. /* PCM */
  362. struct snd_pcm *pcm[HDA_MAX_PCMS];
  363. /* HD codec */
  364. unsigned short codec_mask;
  365. int codec_probe_mask; /* copied from probe_mask option */
  366. struct hda_bus *bus;
  367. unsigned int beep_mode;
  368. /* CORB/RIRB */
  369. struct azx_rb corb;
  370. struct azx_rb rirb;
  371. /* CORB/RIRB and position buffers */
  372. struct snd_dma_buffer rb;
  373. struct snd_dma_buffer posbuf;
  374. /* flags */
  375. int position_fix;
  376. int poll_count;
  377. unsigned int running :1;
  378. unsigned int initialized :1;
  379. unsigned int single_cmd :1;
  380. unsigned int polling_mode :1;
  381. unsigned int msi :1;
  382. unsigned int irq_pending_warned :1;
  383. unsigned int via_dmapos_patch :1; /* enable DMA-position fix for VIA */
  384. unsigned int probing :1; /* codec probing phase */
  385. /* for debugging */
  386. unsigned int last_cmd[AZX_MAX_CODECS];
  387. /* for pending irqs */
  388. struct work_struct irq_pending_work;
  389. /* reboot notifier (for mysterious hangup problem at power-down) */
  390. struct notifier_block reboot_notifier;
  391. };
  392. /* driver types */
  393. enum {
  394. AZX_DRIVER_ICH,
  395. AZX_DRIVER_PCH,
  396. AZX_DRIVER_SCH,
  397. AZX_DRIVER_ATI,
  398. AZX_DRIVER_ATIHDMI,
  399. AZX_DRIVER_VIA,
  400. AZX_DRIVER_SIS,
  401. AZX_DRIVER_ULI,
  402. AZX_DRIVER_NVIDIA,
  403. AZX_DRIVER_TERA,
  404. AZX_DRIVER_GENERIC,
  405. AZX_NUM_DRIVERS, /* keep this as last entry */
  406. };
  407. static char *driver_short_names[] __devinitdata = {
  408. [AZX_DRIVER_ICH] = "HDA Intel",
  409. [AZX_DRIVER_PCH] = "HDA Intel PCH",
  410. [AZX_DRIVER_SCH] = "HDA Intel MID",
  411. [AZX_DRIVER_ATI] = "HDA ATI SB",
  412. [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
  413. [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
  414. [AZX_DRIVER_SIS] = "HDA SIS966",
  415. [AZX_DRIVER_ULI] = "HDA ULI M5461",
  416. [AZX_DRIVER_NVIDIA] = "HDA NVidia",
  417. [AZX_DRIVER_TERA] = "HDA Teradici",
  418. [AZX_DRIVER_GENERIC] = "HD-Audio Generic",
  419. };
  420. /*
  421. * macros for easy use
  422. */
  423. #define azx_writel(chip,reg,value) \
  424. writel(value, (chip)->remap_addr + ICH6_REG_##reg)
  425. #define azx_readl(chip,reg) \
  426. readl((chip)->remap_addr + ICH6_REG_##reg)
  427. #define azx_writew(chip,reg,value) \
  428. writew(value, (chip)->remap_addr + ICH6_REG_##reg)
  429. #define azx_readw(chip,reg) \
  430. readw((chip)->remap_addr + ICH6_REG_##reg)
  431. #define azx_writeb(chip,reg,value) \
  432. writeb(value, (chip)->remap_addr + ICH6_REG_##reg)
  433. #define azx_readb(chip,reg) \
  434. readb((chip)->remap_addr + ICH6_REG_##reg)
  435. #define azx_sd_writel(dev,reg,value) \
  436. writel(value, (dev)->sd_addr + ICH6_REG_##reg)
  437. #define azx_sd_readl(dev,reg) \
  438. readl((dev)->sd_addr + ICH6_REG_##reg)
  439. #define azx_sd_writew(dev,reg,value) \
  440. writew(value, (dev)->sd_addr + ICH6_REG_##reg)
  441. #define azx_sd_readw(dev,reg) \
  442. readw((dev)->sd_addr + ICH6_REG_##reg)
  443. #define azx_sd_writeb(dev,reg,value) \
  444. writeb(value, (dev)->sd_addr + ICH6_REG_##reg)
  445. #define azx_sd_readb(dev,reg) \
  446. readb((dev)->sd_addr + ICH6_REG_##reg)
  447. /* for pcm support */
  448. #define get_azx_dev(substream) (substream->runtime->private_data)
  449. static int azx_acquire_irq(struct azx *chip, int do_disconnect);
  450. static int azx_send_cmd(struct hda_bus *bus, unsigned int val);
  451. /*
  452. * Interface for HD codec
  453. */
  454. /*
  455. * CORB / RIRB interface
  456. */
  457. static int azx_alloc_cmd_io(struct azx *chip)
  458. {
  459. int err;
  460. /* single page (at least 4096 bytes) must suffice for both ringbuffes */
  461. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  462. snd_dma_pci_data(chip->pci),
  463. PAGE_SIZE, &chip->rb);
  464. if (err < 0) {
  465. snd_printk(KERN_ERR SFX "cannot allocate CORB/RIRB\n");
  466. return err;
  467. }
  468. return 0;
  469. }
  470. static void azx_init_cmd_io(struct azx *chip)
  471. {
  472. spin_lock_irq(&chip->reg_lock);
  473. /* CORB set up */
  474. chip->corb.addr = chip->rb.addr;
  475. chip->corb.buf = (u32 *)chip->rb.area;
  476. azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
  477. azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
  478. /* set the corb size to 256 entries (ULI requires explicitly) */
  479. azx_writeb(chip, CORBSIZE, 0x02);
  480. /* set the corb write pointer to 0 */
  481. azx_writew(chip, CORBWP, 0);
  482. /* reset the corb hw read pointer */
  483. azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
  484. /* enable corb dma */
  485. azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
  486. /* RIRB set up */
  487. chip->rirb.addr = chip->rb.addr + 2048;
  488. chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
  489. chip->rirb.wp = chip->rirb.rp = 0;
  490. memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
  491. azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
  492. azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
  493. /* set the rirb size to 256 entries (ULI requires explicitly) */
  494. azx_writeb(chip, RIRBSIZE, 0x02);
  495. /* reset the rirb hw write pointer */
  496. azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
  497. /* set N=1, get RIRB response interrupt for new entry */
  498. azx_writew(chip, RINTCNT, 1);
  499. /* enable rirb dma and response irq */
  500. azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
  501. spin_unlock_irq(&chip->reg_lock);
  502. }
  503. static void azx_free_cmd_io(struct azx *chip)
  504. {
  505. spin_lock_irq(&chip->reg_lock);
  506. /* disable ringbuffer DMAs */
  507. azx_writeb(chip, RIRBCTL, 0);
  508. azx_writeb(chip, CORBCTL, 0);
  509. spin_unlock_irq(&chip->reg_lock);
  510. }
  511. static unsigned int azx_command_addr(u32 cmd)
  512. {
  513. unsigned int addr = cmd >> 28;
  514. if (addr >= AZX_MAX_CODECS) {
  515. snd_BUG();
  516. addr = 0;
  517. }
  518. return addr;
  519. }
  520. static unsigned int azx_response_addr(u32 res)
  521. {
  522. unsigned int addr = res & 0xf;
  523. if (addr >= AZX_MAX_CODECS) {
  524. snd_BUG();
  525. addr = 0;
  526. }
  527. return addr;
  528. }
  529. /* send a command */
  530. static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
  531. {
  532. struct azx *chip = bus->private_data;
  533. unsigned int addr = azx_command_addr(val);
  534. unsigned int wp;
  535. spin_lock_irq(&chip->reg_lock);
  536. /* add command to corb */
  537. wp = azx_readb(chip, CORBWP);
  538. wp++;
  539. wp %= ICH6_MAX_CORB_ENTRIES;
  540. chip->rirb.cmds[addr]++;
  541. chip->corb.buf[wp] = cpu_to_le32(val);
  542. azx_writel(chip, CORBWP, wp);
  543. spin_unlock_irq(&chip->reg_lock);
  544. return 0;
  545. }
  546. #define ICH6_RIRB_EX_UNSOL_EV (1<<4)
  547. /* retrieve RIRB entry - called from interrupt handler */
  548. static void azx_update_rirb(struct azx *chip)
  549. {
  550. unsigned int rp, wp;
  551. unsigned int addr;
  552. u32 res, res_ex;
  553. wp = azx_readb(chip, RIRBWP);
  554. if (wp == chip->rirb.wp)
  555. return;
  556. chip->rirb.wp = wp;
  557. while (chip->rirb.rp != wp) {
  558. chip->rirb.rp++;
  559. chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
  560. rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
  561. res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
  562. res = le32_to_cpu(chip->rirb.buf[rp]);
  563. addr = azx_response_addr(res_ex);
  564. if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
  565. snd_hda_queue_unsol_event(chip->bus, res, res_ex);
  566. else if (chip->rirb.cmds[addr]) {
  567. chip->rirb.res[addr] = res;
  568. smp_wmb();
  569. chip->rirb.cmds[addr]--;
  570. } else
  571. snd_printk(KERN_ERR SFX "spurious response %#x:%#x, "
  572. "last cmd=%#08x\n",
  573. res, res_ex,
  574. chip->last_cmd[addr]);
  575. }
  576. }
  577. /* receive a response */
  578. static unsigned int azx_rirb_get_response(struct hda_bus *bus,
  579. unsigned int addr)
  580. {
  581. struct azx *chip = bus->private_data;
  582. unsigned long timeout;
  583. int do_poll = 0;
  584. again:
  585. timeout = jiffies + msecs_to_jiffies(1000);
  586. for (;;) {
  587. if (chip->polling_mode || do_poll) {
  588. spin_lock_irq(&chip->reg_lock);
  589. azx_update_rirb(chip);
  590. spin_unlock_irq(&chip->reg_lock);
  591. }
  592. if (!chip->rirb.cmds[addr]) {
  593. smp_rmb();
  594. bus->rirb_error = 0;
  595. if (!do_poll)
  596. chip->poll_count = 0;
  597. return chip->rirb.res[addr]; /* the last value */
  598. }
  599. if (time_after(jiffies, timeout))
  600. break;
  601. if (bus->needs_damn_long_delay)
  602. msleep(2); /* temporary workaround */
  603. else {
  604. udelay(10);
  605. cond_resched();
  606. }
  607. }
  608. if (!chip->polling_mode && chip->poll_count < 2) {
  609. snd_printdd(SFX "azx_get_response timeout, "
  610. "polling the codec once: last cmd=0x%08x\n",
  611. chip->last_cmd[addr]);
  612. do_poll = 1;
  613. chip->poll_count++;
  614. goto again;
  615. }
  616. if (!chip->polling_mode) {
  617. snd_printk(KERN_WARNING SFX "azx_get_response timeout, "
  618. "switching to polling mode: last cmd=0x%08x\n",
  619. chip->last_cmd[addr]);
  620. chip->polling_mode = 1;
  621. goto again;
  622. }
  623. if (chip->msi) {
  624. snd_printk(KERN_WARNING SFX "No response from codec, "
  625. "disabling MSI: last cmd=0x%08x\n",
  626. chip->last_cmd[addr]);
  627. free_irq(chip->irq, chip);
  628. chip->irq = -1;
  629. pci_disable_msi(chip->pci);
  630. chip->msi = 0;
  631. if (azx_acquire_irq(chip, 1) < 0) {
  632. bus->rirb_error = 1;
  633. return -1;
  634. }
  635. goto again;
  636. }
  637. if (chip->probing) {
  638. /* If this critical timeout happens during the codec probing
  639. * phase, this is likely an access to a non-existing codec
  640. * slot. Better to return an error and reset the system.
  641. */
  642. return -1;
  643. }
  644. /* a fatal communication error; need either to reset or to fallback
  645. * to the single_cmd mode
  646. */
  647. bus->rirb_error = 1;
  648. if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
  649. bus->response_reset = 1;
  650. return -1; /* give a chance to retry */
  651. }
  652. snd_printk(KERN_ERR "hda_intel: azx_get_response timeout, "
  653. "switching to single_cmd mode: last cmd=0x%08x\n",
  654. chip->last_cmd[addr]);
  655. chip->single_cmd = 1;
  656. bus->response_reset = 0;
  657. /* release CORB/RIRB */
  658. azx_free_cmd_io(chip);
  659. /* disable unsolicited responses */
  660. azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
  661. return -1;
  662. }
  663. /*
  664. * Use the single immediate command instead of CORB/RIRB for simplicity
  665. *
  666. * Note: according to Intel, this is not preferred use. The command was
  667. * intended for the BIOS only, and may get confused with unsolicited
  668. * responses. So, we shouldn't use it for normal operation from the
  669. * driver.
  670. * I left the codes, however, for debugging/testing purposes.
  671. */
  672. /* receive a response */
  673. static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
  674. {
  675. int timeout = 50;
  676. while (timeout--) {
  677. /* check IRV busy bit */
  678. if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
  679. /* reuse rirb.res as the response return value */
  680. chip->rirb.res[addr] = azx_readl(chip, IR);
  681. return 0;
  682. }
  683. udelay(1);
  684. }
  685. if (printk_ratelimit())
  686. snd_printd(SFX "get_response timeout: IRS=0x%x\n",
  687. azx_readw(chip, IRS));
  688. chip->rirb.res[addr] = -1;
  689. return -EIO;
  690. }
  691. /* send a command */
  692. static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
  693. {
  694. struct azx *chip = bus->private_data;
  695. unsigned int addr = azx_command_addr(val);
  696. int timeout = 50;
  697. bus->rirb_error = 0;
  698. while (timeout--) {
  699. /* check ICB busy bit */
  700. if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
  701. /* Clear IRV valid bit */
  702. azx_writew(chip, IRS, azx_readw(chip, IRS) |
  703. ICH6_IRS_VALID);
  704. azx_writel(chip, IC, val);
  705. azx_writew(chip, IRS, azx_readw(chip, IRS) |
  706. ICH6_IRS_BUSY);
  707. return azx_single_wait_for_response(chip, addr);
  708. }
  709. udelay(1);
  710. }
  711. if (printk_ratelimit())
  712. snd_printd(SFX "send_cmd timeout: IRS=0x%x, val=0x%x\n",
  713. azx_readw(chip, IRS), val);
  714. return -EIO;
  715. }
  716. /* receive a response */
  717. static unsigned int azx_single_get_response(struct hda_bus *bus,
  718. unsigned int addr)
  719. {
  720. struct azx *chip = bus->private_data;
  721. return chip->rirb.res[addr];
  722. }
  723. /*
  724. * The below are the main callbacks from hda_codec.
  725. *
  726. * They are just the skeleton to call sub-callbacks according to the
  727. * current setting of chip->single_cmd.
  728. */
  729. /* send a command */
  730. static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
  731. {
  732. struct azx *chip = bus->private_data;
  733. chip->last_cmd[azx_command_addr(val)] = val;
  734. if (chip->single_cmd)
  735. return azx_single_send_cmd(bus, val);
  736. else
  737. return azx_corb_send_cmd(bus, val);
  738. }
  739. /* get a response */
  740. static unsigned int azx_get_response(struct hda_bus *bus,
  741. unsigned int addr)
  742. {
  743. struct azx *chip = bus->private_data;
  744. if (chip->single_cmd)
  745. return azx_single_get_response(bus, addr);
  746. else
  747. return azx_rirb_get_response(bus, addr);
  748. }
  749. #ifdef CONFIG_SND_HDA_POWER_SAVE
  750. static void azx_power_notify(struct hda_bus *bus);
  751. #endif
  752. /* reset codec link */
  753. static int azx_reset(struct azx *chip)
  754. {
  755. int count;
  756. /* clear STATESTS */
  757. azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
  758. /* reset controller */
  759. azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
  760. count = 50;
  761. while (azx_readb(chip, GCTL) && --count)
  762. msleep(1);
  763. /* delay for >= 100us for codec PLL to settle per spec
  764. * Rev 0.9 section 5.5.1
  765. */
  766. msleep(1);
  767. /* Bring controller out of reset */
  768. azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
  769. count = 50;
  770. while (!azx_readb(chip, GCTL) && --count)
  771. msleep(1);
  772. /* Brent Chartrand said to wait >= 540us for codecs to initialize */
  773. msleep(1);
  774. /* check to see if controller is ready */
  775. if (!azx_readb(chip, GCTL)) {
  776. snd_printd(SFX "azx_reset: controller not ready!\n");
  777. return -EBUSY;
  778. }
  779. /* Accept unsolicited responses */
  780. if (!chip->single_cmd)
  781. azx_writel(chip, GCTL, azx_readl(chip, GCTL) |
  782. ICH6_GCTL_UNSOL);
  783. /* detect codecs */
  784. if (!chip->codec_mask) {
  785. chip->codec_mask = azx_readw(chip, STATESTS);
  786. snd_printdd(SFX "codec_mask = 0x%x\n", chip->codec_mask);
  787. }
  788. return 0;
  789. }
  790. /*
  791. * Lowlevel interface
  792. */
  793. /* enable interrupts */
  794. static void azx_int_enable(struct azx *chip)
  795. {
  796. /* enable controller CIE and GIE */
  797. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
  798. ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
  799. }
  800. /* disable interrupts */
  801. static void azx_int_disable(struct azx *chip)
  802. {
  803. int i;
  804. /* disable interrupts in stream descriptor */
  805. for (i = 0; i < chip->num_streams; i++) {
  806. struct azx_dev *azx_dev = &chip->azx_dev[i];
  807. azx_sd_writeb(azx_dev, SD_CTL,
  808. azx_sd_readb(azx_dev, SD_CTL) & ~SD_INT_MASK);
  809. }
  810. /* disable SIE for all streams */
  811. azx_writeb(chip, INTCTL, 0);
  812. /* disable controller CIE and GIE */
  813. azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
  814. ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
  815. }
  816. /* clear interrupts */
  817. static void azx_int_clear(struct azx *chip)
  818. {
  819. int i;
  820. /* clear stream status */
  821. for (i = 0; i < chip->num_streams; i++) {
  822. struct azx_dev *azx_dev = &chip->azx_dev[i];
  823. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  824. }
  825. /* clear STATESTS */
  826. azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
  827. /* clear rirb status */
  828. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  829. /* clear int status */
  830. azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
  831. }
  832. /* start a stream */
  833. static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
  834. {
  835. /*
  836. * Before stream start, initialize parameter
  837. */
  838. azx_dev->insufficient = 1;
  839. /* enable SIE */
  840. azx_writel(chip, INTCTL,
  841. azx_readl(chip, INTCTL) | (1 << azx_dev->index));
  842. /* set DMA start and interrupt mask */
  843. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
  844. SD_CTL_DMA_START | SD_INT_MASK);
  845. }
  846. /* stop DMA */
  847. static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
  848. {
  849. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) &
  850. ~(SD_CTL_DMA_START | SD_INT_MASK));
  851. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
  852. }
  853. /* stop a stream */
  854. static void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
  855. {
  856. azx_stream_clear(chip, azx_dev);
  857. /* disable SIE */
  858. azx_writel(chip, INTCTL,
  859. azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
  860. }
  861. /*
  862. * reset and start the controller registers
  863. */
  864. static void azx_init_chip(struct azx *chip)
  865. {
  866. if (chip->initialized)
  867. return;
  868. /* reset controller */
  869. azx_reset(chip);
  870. /* initialize interrupts */
  871. azx_int_clear(chip);
  872. azx_int_enable(chip);
  873. /* initialize the codec command I/O */
  874. if (!chip->single_cmd)
  875. azx_init_cmd_io(chip);
  876. /* program the position buffer */
  877. azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
  878. azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr));
  879. chip->initialized = 1;
  880. }
  881. /*
  882. * initialize the PCI registers
  883. */
  884. /* update bits in a PCI register byte */
  885. static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
  886. unsigned char mask, unsigned char val)
  887. {
  888. unsigned char data;
  889. pci_read_config_byte(pci, reg, &data);
  890. data &= ~mask;
  891. data |= (val & mask);
  892. pci_write_config_byte(pci, reg, data);
  893. }
  894. static void azx_init_pci(struct azx *chip)
  895. {
  896. unsigned short snoop;
  897. /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
  898. * TCSEL == Traffic Class Select Register, which sets PCI express QOS
  899. * Ensuring these bits are 0 clears playback static on some HD Audio
  900. * codecs
  901. */
  902. update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
  903. switch (chip->driver_type) {
  904. case AZX_DRIVER_ATI:
  905. /* For ATI SB450 azalia HD audio, we need to enable snoop */
  906. update_pci_byte(chip->pci,
  907. ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR,
  908. 0x07, ATI_SB450_HDAUDIO_ENABLE_SNOOP);
  909. break;
  910. case AZX_DRIVER_NVIDIA:
  911. /* For NVIDIA HDA, enable snoop */
  912. update_pci_byte(chip->pci,
  913. NVIDIA_HDA_TRANSREG_ADDR,
  914. 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
  915. update_pci_byte(chip->pci,
  916. NVIDIA_HDA_ISTRM_COH,
  917. 0x01, NVIDIA_HDA_ENABLE_COHBIT);
  918. update_pci_byte(chip->pci,
  919. NVIDIA_HDA_OSTRM_COH,
  920. 0x01, NVIDIA_HDA_ENABLE_COHBIT);
  921. break;
  922. case AZX_DRIVER_SCH:
  923. case AZX_DRIVER_PCH:
  924. pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
  925. if (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) {
  926. pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC,
  927. snoop & (~INTEL_SCH_HDA_DEVC_NOSNOOP));
  928. pci_read_config_word(chip->pci,
  929. INTEL_SCH_HDA_DEVC, &snoop);
  930. snd_printdd(SFX "HDA snoop disabled, enabling ... %s\n",
  931. (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)
  932. ? "Failed" : "OK");
  933. }
  934. break;
  935. }
  936. }
  937. static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
  938. /*
  939. * interrupt handler
  940. */
  941. static irqreturn_t azx_interrupt(int irq, void *dev_id)
  942. {
  943. struct azx *chip = dev_id;
  944. struct azx_dev *azx_dev;
  945. u32 status;
  946. int i, ok;
  947. spin_lock(&chip->reg_lock);
  948. status = azx_readl(chip, INTSTS);
  949. if (status == 0) {
  950. spin_unlock(&chip->reg_lock);
  951. return IRQ_NONE;
  952. }
  953. for (i = 0; i < chip->num_streams; i++) {
  954. azx_dev = &chip->azx_dev[i];
  955. if (status & azx_dev->sd_int_sta_mask) {
  956. azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
  957. if (!azx_dev->substream || !azx_dev->running)
  958. continue;
  959. /* check whether this IRQ is really acceptable */
  960. ok = azx_position_ok(chip, azx_dev);
  961. if (ok == 1) {
  962. azx_dev->irq_pending = 0;
  963. spin_unlock(&chip->reg_lock);
  964. snd_pcm_period_elapsed(azx_dev->substream);
  965. spin_lock(&chip->reg_lock);
  966. } else if (ok == 0 && chip->bus && chip->bus->workq) {
  967. /* bogus IRQ, process it later */
  968. azx_dev->irq_pending = 1;
  969. queue_work(chip->bus->workq,
  970. &chip->irq_pending_work);
  971. }
  972. }
  973. }
  974. /* clear rirb int */
  975. status = azx_readb(chip, RIRBSTS);
  976. if (status & RIRB_INT_MASK) {
  977. if (status & RIRB_INT_RESPONSE)
  978. azx_update_rirb(chip);
  979. azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
  980. }
  981. #if 0
  982. /* clear state status int */
  983. if (azx_readb(chip, STATESTS) & 0x04)
  984. azx_writeb(chip, STATESTS, 0x04);
  985. #endif
  986. spin_unlock(&chip->reg_lock);
  987. return IRQ_HANDLED;
  988. }
  989. /*
  990. * set up a BDL entry
  991. */
  992. static int setup_bdle(struct snd_pcm_substream *substream,
  993. struct azx_dev *azx_dev, u32 **bdlp,
  994. int ofs, int size, int with_ioc)
  995. {
  996. u32 *bdl = *bdlp;
  997. while (size > 0) {
  998. dma_addr_t addr;
  999. int chunk;
  1000. if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
  1001. return -EINVAL;
  1002. addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  1003. /* program the address field of the BDL entry */
  1004. bdl[0] = cpu_to_le32((u32)addr);
  1005. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  1006. /* program the size field of the BDL entry */
  1007. chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
  1008. bdl[2] = cpu_to_le32(chunk);
  1009. /* program the IOC to enable interrupt
  1010. * only when the whole fragment is processed
  1011. */
  1012. size -= chunk;
  1013. bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
  1014. bdl += 4;
  1015. azx_dev->frags++;
  1016. ofs += chunk;
  1017. }
  1018. *bdlp = bdl;
  1019. return ofs;
  1020. }
  1021. /*
  1022. * set up BDL entries
  1023. */
  1024. static int azx_setup_periods(struct azx *chip,
  1025. struct snd_pcm_substream *substream,
  1026. struct azx_dev *azx_dev)
  1027. {
  1028. u32 *bdl;
  1029. int i, ofs, periods, period_bytes;
  1030. int pos_adj;
  1031. /* reset BDL address */
  1032. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  1033. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  1034. period_bytes = azx_dev->period_bytes;
  1035. periods = azx_dev->bufsize / period_bytes;
  1036. /* program the initial BDL entries */
  1037. bdl = (u32 *)azx_dev->bdl.area;
  1038. ofs = 0;
  1039. azx_dev->frags = 0;
  1040. pos_adj = bdl_pos_adj[chip->dev_index];
  1041. if (pos_adj > 0) {
  1042. struct snd_pcm_runtime *runtime = substream->runtime;
  1043. int pos_align = pos_adj;
  1044. pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
  1045. if (!pos_adj)
  1046. pos_adj = pos_align;
  1047. else
  1048. pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
  1049. pos_align;
  1050. pos_adj = frames_to_bytes(runtime, pos_adj);
  1051. if (pos_adj >= period_bytes) {
  1052. snd_printk(KERN_WARNING SFX "Too big adjustment %d\n",
  1053. bdl_pos_adj[chip->dev_index]);
  1054. pos_adj = 0;
  1055. } else {
  1056. ofs = setup_bdle(substream, azx_dev,
  1057. &bdl, ofs, pos_adj, 1);
  1058. if (ofs < 0)
  1059. goto error;
  1060. }
  1061. } else
  1062. pos_adj = 0;
  1063. for (i = 0; i < periods; i++) {
  1064. if (i == periods - 1 && pos_adj)
  1065. ofs = setup_bdle(substream, azx_dev, &bdl, ofs,
  1066. period_bytes - pos_adj, 0);
  1067. else
  1068. ofs = setup_bdle(substream, azx_dev, &bdl, ofs,
  1069. period_bytes, 1);
  1070. if (ofs < 0)
  1071. goto error;
  1072. }
  1073. return 0;
  1074. error:
  1075. snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
  1076. azx_dev->bufsize, period_bytes);
  1077. return -EINVAL;
  1078. }
  1079. /* reset stream */
  1080. static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
  1081. {
  1082. unsigned char val;
  1083. int timeout;
  1084. azx_stream_clear(chip, azx_dev);
  1085. azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
  1086. SD_CTL_STREAM_RESET);
  1087. udelay(3);
  1088. timeout = 300;
  1089. while (!((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  1090. --timeout)
  1091. ;
  1092. val &= ~SD_CTL_STREAM_RESET;
  1093. azx_sd_writeb(azx_dev, SD_CTL, val);
  1094. udelay(3);
  1095. timeout = 300;
  1096. /* waiting for hardware to report that the stream is out of reset */
  1097. while (((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
  1098. --timeout)
  1099. ;
  1100. /* reset first position - may not be synced with hw at this time */
  1101. *azx_dev->posbuf = 0;
  1102. }
  1103. /*
  1104. * set up the SD for streaming
  1105. */
  1106. static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
  1107. {
  1108. /* make sure the run bit is zero for SD */
  1109. azx_stream_clear(chip, azx_dev);
  1110. /* program the stream_tag */
  1111. azx_sd_writel(azx_dev, SD_CTL,
  1112. (azx_sd_readl(azx_dev, SD_CTL) & ~SD_CTL_STREAM_TAG_MASK)|
  1113. (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT));
  1114. /* program the length of samples in cyclic buffer */
  1115. azx_sd_writel(azx_dev, SD_CBL, azx_dev->bufsize);
  1116. /* program the stream format */
  1117. /* this value needs to be the same as the one programmed */
  1118. azx_sd_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
  1119. /* program the stream LVI (last valid index) of the BDL */
  1120. azx_sd_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
  1121. /* program the BDL address */
  1122. /* lower BDL address */
  1123. azx_sd_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
  1124. /* upper BDL address */
  1125. azx_sd_writel(azx_dev, SD_BDLPU, upper_32_bits(azx_dev->bdl.addr));
  1126. /* enable the position buffer */
  1127. if (chip->position_fix == POS_FIX_POSBUF ||
  1128. chip->position_fix == POS_FIX_AUTO ||
  1129. chip->via_dmapos_patch) {
  1130. if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
  1131. azx_writel(chip, DPLBASE,
  1132. (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
  1133. }
  1134. /* set the interrupt enable bits in the descriptor control register */
  1135. azx_sd_writel(azx_dev, SD_CTL,
  1136. azx_sd_readl(azx_dev, SD_CTL) | SD_INT_MASK);
  1137. return 0;
  1138. }
  1139. /*
  1140. * Probe the given codec address
  1141. */
  1142. static int probe_codec(struct azx *chip, int addr)
  1143. {
  1144. unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
  1145. (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
  1146. unsigned int res;
  1147. mutex_lock(&chip->bus->cmd_mutex);
  1148. chip->probing = 1;
  1149. azx_send_cmd(chip->bus, cmd);
  1150. res = azx_get_response(chip->bus, addr);
  1151. chip->probing = 0;
  1152. mutex_unlock(&chip->bus->cmd_mutex);
  1153. if (res == -1)
  1154. return -EIO;
  1155. snd_printdd(SFX "codec #%d probed OK\n", addr);
  1156. return 0;
  1157. }
  1158. static int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
  1159. struct hda_pcm *cpcm);
  1160. static void azx_stop_chip(struct azx *chip);
  1161. static void azx_bus_reset(struct hda_bus *bus)
  1162. {
  1163. struct azx *chip = bus->private_data;
  1164. bus->in_reset = 1;
  1165. azx_stop_chip(chip);
  1166. azx_init_chip(chip);
  1167. #ifdef CONFIG_PM
  1168. if (chip->initialized) {
  1169. int i;
  1170. for (i = 0; i < HDA_MAX_PCMS; i++)
  1171. snd_pcm_suspend_all(chip->pcm[i]);
  1172. snd_hda_suspend(chip->bus);
  1173. snd_hda_resume(chip->bus);
  1174. }
  1175. #endif
  1176. bus->in_reset = 0;
  1177. }
  1178. /*
  1179. * Codec initialization
  1180. */
  1181. /* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
  1182. static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] __devinitdata = {
  1183. [AZX_DRIVER_NVIDIA] = 8,
  1184. [AZX_DRIVER_TERA] = 1,
  1185. };
  1186. static int __devinit azx_codec_create(struct azx *chip, const char *model)
  1187. {
  1188. struct hda_bus_template bus_temp;
  1189. int c, codecs, err;
  1190. int max_slots;
  1191. memset(&bus_temp, 0, sizeof(bus_temp));
  1192. bus_temp.private_data = chip;
  1193. bus_temp.modelname = model;
  1194. bus_temp.pci = chip->pci;
  1195. bus_temp.ops.command = azx_send_cmd;
  1196. bus_temp.ops.get_response = azx_get_response;
  1197. bus_temp.ops.attach_pcm = azx_attach_pcm_stream;
  1198. bus_temp.ops.bus_reset = azx_bus_reset;
  1199. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1200. bus_temp.power_save = &power_save;
  1201. bus_temp.ops.pm_notify = azx_power_notify;
  1202. #endif
  1203. err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus);
  1204. if (err < 0)
  1205. return err;
  1206. if (chip->driver_type == AZX_DRIVER_NVIDIA)
  1207. chip->bus->needs_damn_long_delay = 1;
  1208. codecs = 0;
  1209. max_slots = azx_max_codecs[chip->driver_type];
  1210. if (!max_slots)
  1211. max_slots = AZX_DEFAULT_CODECS;
  1212. /* First try to probe all given codec slots */
  1213. for (c = 0; c < max_slots; c++) {
  1214. if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
  1215. if (probe_codec(chip, c) < 0) {
  1216. /* Some BIOSen give you wrong codec addresses
  1217. * that don't exist
  1218. */
  1219. snd_printk(KERN_WARNING SFX
  1220. "Codec #%d probe error; "
  1221. "disabling it...\n", c);
  1222. chip->codec_mask &= ~(1 << c);
  1223. /* More badly, accessing to a non-existing
  1224. * codec often screws up the controller chip,
  1225. * and disturbs the further communications.
  1226. * Thus if an error occurs during probing,
  1227. * better to reset the controller chip to
  1228. * get back to the sanity state.
  1229. */
  1230. azx_stop_chip(chip);
  1231. azx_init_chip(chip);
  1232. }
  1233. }
  1234. }
  1235. /* Then create codec instances */
  1236. for (c = 0; c < max_slots; c++) {
  1237. if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
  1238. struct hda_codec *codec;
  1239. err = snd_hda_codec_new(chip->bus, c, &codec);
  1240. if (err < 0)
  1241. continue;
  1242. codec->beep_mode = chip->beep_mode;
  1243. codecs++;
  1244. }
  1245. }
  1246. if (!codecs) {
  1247. snd_printk(KERN_ERR SFX "no codecs initialized\n");
  1248. return -ENXIO;
  1249. }
  1250. return 0;
  1251. }
  1252. /* configure each codec instance */
  1253. static int __devinit azx_codec_configure(struct azx *chip)
  1254. {
  1255. struct hda_codec *codec;
  1256. list_for_each_entry(codec, &chip->bus->codec_list, list) {
  1257. snd_hda_codec_configure(codec);
  1258. }
  1259. return 0;
  1260. }
  1261. /*
  1262. * PCM support
  1263. */
  1264. /* assign a stream for the PCM */
  1265. static inline struct azx_dev *
  1266. azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
  1267. {
  1268. int dev, i, nums;
  1269. struct azx_dev *res = NULL;
  1270. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1271. dev = chip->playback_index_offset;
  1272. nums = chip->playback_streams;
  1273. } else {
  1274. dev = chip->capture_index_offset;
  1275. nums = chip->capture_streams;
  1276. }
  1277. for (i = 0; i < nums; i++, dev++)
  1278. if (!chip->azx_dev[dev].opened) {
  1279. res = &chip->azx_dev[dev];
  1280. if (res->device == substream->pcm->device)
  1281. break;
  1282. }
  1283. if (res) {
  1284. res->opened = 1;
  1285. res->device = substream->pcm->device;
  1286. }
  1287. return res;
  1288. }
  1289. /* release the assigned stream */
  1290. static inline void azx_release_device(struct azx_dev *azx_dev)
  1291. {
  1292. azx_dev->opened = 0;
  1293. }
  1294. static struct snd_pcm_hardware azx_pcm_hw = {
  1295. .info = (SNDRV_PCM_INFO_MMAP |
  1296. SNDRV_PCM_INFO_INTERLEAVED |
  1297. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1298. SNDRV_PCM_INFO_MMAP_VALID |
  1299. /* No full-resume yet implemented */
  1300. /* SNDRV_PCM_INFO_RESUME |*/
  1301. SNDRV_PCM_INFO_PAUSE |
  1302. SNDRV_PCM_INFO_SYNC_START),
  1303. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1304. .rates = SNDRV_PCM_RATE_48000,
  1305. .rate_min = 48000,
  1306. .rate_max = 48000,
  1307. .channels_min = 2,
  1308. .channels_max = 2,
  1309. .buffer_bytes_max = AZX_MAX_BUF_SIZE,
  1310. .period_bytes_min = 128,
  1311. .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
  1312. .periods_min = 2,
  1313. .periods_max = AZX_MAX_FRAG,
  1314. .fifo_size = 0,
  1315. };
  1316. struct azx_pcm {
  1317. struct azx *chip;
  1318. struct hda_codec *codec;
  1319. struct hda_pcm_stream *hinfo[2];
  1320. };
  1321. static int azx_pcm_open(struct snd_pcm_substream *substream)
  1322. {
  1323. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1324. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1325. struct azx *chip = apcm->chip;
  1326. struct azx_dev *azx_dev;
  1327. struct snd_pcm_runtime *runtime = substream->runtime;
  1328. unsigned long flags;
  1329. int err;
  1330. mutex_lock(&chip->open_mutex);
  1331. azx_dev = azx_assign_device(chip, substream);
  1332. if (azx_dev == NULL) {
  1333. mutex_unlock(&chip->open_mutex);
  1334. return -EBUSY;
  1335. }
  1336. runtime->hw = azx_pcm_hw;
  1337. runtime->hw.channels_min = hinfo->channels_min;
  1338. runtime->hw.channels_max = hinfo->channels_max;
  1339. runtime->hw.formats = hinfo->formats;
  1340. runtime->hw.rates = hinfo->rates;
  1341. snd_pcm_limit_hw_rates(runtime);
  1342. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  1343. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1344. 128);
  1345. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1346. 128);
  1347. snd_hda_power_up(apcm->codec);
  1348. err = hinfo->ops.open(hinfo, apcm->codec, substream);
  1349. if (err < 0) {
  1350. azx_release_device(azx_dev);
  1351. snd_hda_power_down(apcm->codec);
  1352. mutex_unlock(&chip->open_mutex);
  1353. return err;
  1354. }
  1355. snd_pcm_limit_hw_rates(runtime);
  1356. /* sanity check */
  1357. if (snd_BUG_ON(!runtime->hw.channels_min) ||
  1358. snd_BUG_ON(!runtime->hw.channels_max) ||
  1359. snd_BUG_ON(!runtime->hw.formats) ||
  1360. snd_BUG_ON(!runtime->hw.rates)) {
  1361. azx_release_device(azx_dev);
  1362. hinfo->ops.close(hinfo, apcm->codec, substream);
  1363. snd_hda_power_down(apcm->codec);
  1364. mutex_unlock(&chip->open_mutex);
  1365. return -EINVAL;
  1366. }
  1367. spin_lock_irqsave(&chip->reg_lock, flags);
  1368. azx_dev->substream = substream;
  1369. azx_dev->running = 0;
  1370. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1371. runtime->private_data = azx_dev;
  1372. snd_pcm_set_sync(substream);
  1373. mutex_unlock(&chip->open_mutex);
  1374. return 0;
  1375. }
  1376. static int azx_pcm_close(struct snd_pcm_substream *substream)
  1377. {
  1378. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1379. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1380. struct azx *chip = apcm->chip;
  1381. struct azx_dev *azx_dev = get_azx_dev(substream);
  1382. unsigned long flags;
  1383. mutex_lock(&chip->open_mutex);
  1384. spin_lock_irqsave(&chip->reg_lock, flags);
  1385. azx_dev->substream = NULL;
  1386. azx_dev->running = 0;
  1387. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1388. azx_release_device(azx_dev);
  1389. hinfo->ops.close(hinfo, apcm->codec, substream);
  1390. snd_hda_power_down(apcm->codec);
  1391. mutex_unlock(&chip->open_mutex);
  1392. return 0;
  1393. }
  1394. static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
  1395. struct snd_pcm_hw_params *hw_params)
  1396. {
  1397. struct azx_dev *azx_dev = get_azx_dev(substream);
  1398. azx_dev->bufsize = 0;
  1399. azx_dev->period_bytes = 0;
  1400. azx_dev->format_val = 0;
  1401. return snd_pcm_lib_malloc_pages(substream,
  1402. params_buffer_bytes(hw_params));
  1403. }
  1404. static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
  1405. {
  1406. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1407. struct azx_dev *azx_dev = get_azx_dev(substream);
  1408. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1409. /* reset BDL address */
  1410. azx_sd_writel(azx_dev, SD_BDLPL, 0);
  1411. azx_sd_writel(azx_dev, SD_BDLPU, 0);
  1412. azx_sd_writel(azx_dev, SD_CTL, 0);
  1413. azx_dev->bufsize = 0;
  1414. azx_dev->period_bytes = 0;
  1415. azx_dev->format_val = 0;
  1416. hinfo->ops.cleanup(hinfo, apcm->codec, substream);
  1417. return snd_pcm_lib_free_pages(substream);
  1418. }
  1419. static int azx_pcm_prepare(struct snd_pcm_substream *substream)
  1420. {
  1421. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1422. struct azx *chip = apcm->chip;
  1423. struct azx_dev *azx_dev = get_azx_dev(substream);
  1424. struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
  1425. struct snd_pcm_runtime *runtime = substream->runtime;
  1426. unsigned int bufsize, period_bytes, format_val;
  1427. int err;
  1428. azx_stream_reset(chip, azx_dev);
  1429. format_val = snd_hda_calc_stream_format(runtime->rate,
  1430. runtime->channels,
  1431. runtime->format,
  1432. hinfo->maxbps);
  1433. if (!format_val) {
  1434. snd_printk(KERN_ERR SFX
  1435. "invalid format_val, rate=%d, ch=%d, format=%d\n",
  1436. runtime->rate, runtime->channels, runtime->format);
  1437. return -EINVAL;
  1438. }
  1439. bufsize = snd_pcm_lib_buffer_bytes(substream);
  1440. period_bytes = snd_pcm_lib_period_bytes(substream);
  1441. snd_printdd(SFX "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
  1442. bufsize, format_val);
  1443. if (bufsize != azx_dev->bufsize ||
  1444. period_bytes != azx_dev->period_bytes ||
  1445. format_val != azx_dev->format_val) {
  1446. azx_dev->bufsize = bufsize;
  1447. azx_dev->period_bytes = period_bytes;
  1448. azx_dev->format_val = format_val;
  1449. err = azx_setup_periods(chip, substream, azx_dev);
  1450. if (err < 0)
  1451. return err;
  1452. }
  1453. azx_dev->min_jiffies = (runtime->period_size * HZ) /
  1454. (runtime->rate * 2);
  1455. azx_setup_controller(chip, azx_dev);
  1456. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1457. azx_dev->fifo_size = azx_sd_readw(azx_dev, SD_FIFOSIZE) + 1;
  1458. else
  1459. azx_dev->fifo_size = 0;
  1460. return hinfo->ops.prepare(hinfo, apcm->codec, azx_dev->stream_tag,
  1461. azx_dev->format_val, substream);
  1462. }
  1463. static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  1464. {
  1465. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1466. struct azx *chip = apcm->chip;
  1467. struct azx_dev *azx_dev;
  1468. struct snd_pcm_substream *s;
  1469. int rstart = 0, start, nsync = 0, sbits = 0;
  1470. int nwait, timeout;
  1471. switch (cmd) {
  1472. case SNDRV_PCM_TRIGGER_START:
  1473. rstart = 1;
  1474. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1475. case SNDRV_PCM_TRIGGER_RESUME:
  1476. start = 1;
  1477. break;
  1478. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1479. case SNDRV_PCM_TRIGGER_SUSPEND:
  1480. case SNDRV_PCM_TRIGGER_STOP:
  1481. start = 0;
  1482. break;
  1483. default:
  1484. return -EINVAL;
  1485. }
  1486. snd_pcm_group_for_each_entry(s, substream) {
  1487. if (s->pcm->card != substream->pcm->card)
  1488. continue;
  1489. azx_dev = get_azx_dev(s);
  1490. sbits |= 1 << azx_dev->index;
  1491. nsync++;
  1492. snd_pcm_trigger_done(s, substream);
  1493. }
  1494. spin_lock(&chip->reg_lock);
  1495. if (nsync > 1) {
  1496. /* first, set SYNC bits of corresponding streams */
  1497. azx_writel(chip, SYNC, azx_readl(chip, SYNC) | sbits);
  1498. }
  1499. snd_pcm_group_for_each_entry(s, substream) {
  1500. if (s->pcm->card != substream->pcm->card)
  1501. continue;
  1502. azx_dev = get_azx_dev(s);
  1503. if (rstart) {
  1504. azx_dev->start_flag = 1;
  1505. azx_dev->start_jiffies = jiffies + azx_dev->min_jiffies;
  1506. }
  1507. if (start)
  1508. azx_stream_start(chip, azx_dev);
  1509. else
  1510. azx_stream_stop(chip, azx_dev);
  1511. azx_dev->running = start;
  1512. }
  1513. spin_unlock(&chip->reg_lock);
  1514. if (start) {
  1515. if (nsync == 1)
  1516. return 0;
  1517. /* wait until all FIFOs get ready */
  1518. for (timeout = 5000; timeout; timeout--) {
  1519. nwait = 0;
  1520. snd_pcm_group_for_each_entry(s, substream) {
  1521. if (s->pcm->card != substream->pcm->card)
  1522. continue;
  1523. azx_dev = get_azx_dev(s);
  1524. if (!(azx_sd_readb(azx_dev, SD_STS) &
  1525. SD_STS_FIFO_READY))
  1526. nwait++;
  1527. }
  1528. if (!nwait)
  1529. break;
  1530. cpu_relax();
  1531. }
  1532. } else {
  1533. /* wait until all RUN bits are cleared */
  1534. for (timeout = 5000; timeout; timeout--) {
  1535. nwait = 0;
  1536. snd_pcm_group_for_each_entry(s, substream) {
  1537. if (s->pcm->card != substream->pcm->card)
  1538. continue;
  1539. azx_dev = get_azx_dev(s);
  1540. if (azx_sd_readb(azx_dev, SD_CTL) &
  1541. SD_CTL_DMA_START)
  1542. nwait++;
  1543. }
  1544. if (!nwait)
  1545. break;
  1546. cpu_relax();
  1547. }
  1548. }
  1549. if (nsync > 1) {
  1550. spin_lock(&chip->reg_lock);
  1551. /* reset SYNC bits */
  1552. azx_writel(chip, SYNC, azx_readl(chip, SYNC) & ~sbits);
  1553. spin_unlock(&chip->reg_lock);
  1554. }
  1555. return 0;
  1556. }
  1557. /* get the current DMA position with correction on VIA chips */
  1558. static unsigned int azx_via_get_position(struct azx *chip,
  1559. struct azx_dev *azx_dev)
  1560. {
  1561. unsigned int link_pos, mini_pos, bound_pos;
  1562. unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
  1563. unsigned int fifo_size;
  1564. link_pos = azx_sd_readl(azx_dev, SD_LPIB);
  1565. if (azx_dev->index >= 4) {
  1566. /* Playback, no problem using link position */
  1567. return link_pos;
  1568. }
  1569. /* Capture */
  1570. /* For new chipset,
  1571. * use mod to get the DMA position just like old chipset
  1572. */
  1573. mod_dma_pos = le32_to_cpu(*azx_dev->posbuf);
  1574. mod_dma_pos %= azx_dev->period_bytes;
  1575. /* azx_dev->fifo_size can't get FIFO size of in stream.
  1576. * Get from base address + offset.
  1577. */
  1578. fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
  1579. if (azx_dev->insufficient) {
  1580. /* Link position never gather than FIFO size */
  1581. if (link_pos <= fifo_size)
  1582. return 0;
  1583. azx_dev->insufficient = 0;
  1584. }
  1585. if (link_pos <= fifo_size)
  1586. mini_pos = azx_dev->bufsize + link_pos - fifo_size;
  1587. else
  1588. mini_pos = link_pos - fifo_size;
  1589. /* Find nearest previous boudary */
  1590. mod_mini_pos = mini_pos % azx_dev->period_bytes;
  1591. mod_link_pos = link_pos % azx_dev->period_bytes;
  1592. if (mod_link_pos >= fifo_size)
  1593. bound_pos = link_pos - mod_link_pos;
  1594. else if (mod_dma_pos >= mod_mini_pos)
  1595. bound_pos = mini_pos - mod_mini_pos;
  1596. else {
  1597. bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes;
  1598. if (bound_pos >= azx_dev->bufsize)
  1599. bound_pos = 0;
  1600. }
  1601. /* Calculate real DMA position we want */
  1602. return bound_pos + mod_dma_pos;
  1603. }
  1604. static unsigned int azx_get_position(struct azx *chip,
  1605. struct azx_dev *azx_dev)
  1606. {
  1607. unsigned int pos;
  1608. if (chip->via_dmapos_patch)
  1609. pos = azx_via_get_position(chip, azx_dev);
  1610. else if (chip->position_fix == POS_FIX_POSBUF ||
  1611. chip->position_fix == POS_FIX_AUTO) {
  1612. /* use the position buffer */
  1613. pos = le32_to_cpu(*azx_dev->posbuf);
  1614. } else {
  1615. /* read LPIB */
  1616. pos = azx_sd_readl(azx_dev, SD_LPIB);
  1617. }
  1618. if (pos >= azx_dev->bufsize)
  1619. pos = 0;
  1620. return pos;
  1621. }
  1622. static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
  1623. {
  1624. struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
  1625. struct azx *chip = apcm->chip;
  1626. struct azx_dev *azx_dev = get_azx_dev(substream);
  1627. return bytes_to_frames(substream->runtime,
  1628. azx_get_position(chip, azx_dev));
  1629. }
  1630. /*
  1631. * Check whether the current DMA position is acceptable for updating
  1632. * periods. Returns non-zero if it's OK.
  1633. *
  1634. * Many HD-audio controllers appear pretty inaccurate about
  1635. * the update-IRQ timing. The IRQ is issued before actually the
  1636. * data is processed. So, we need to process it afterwords in a
  1637. * workqueue.
  1638. */
  1639. static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
  1640. {
  1641. unsigned int pos;
  1642. if (azx_dev->start_flag &&
  1643. time_before_eq(jiffies, azx_dev->start_jiffies))
  1644. return -1; /* bogus (too early) interrupt */
  1645. azx_dev->start_flag = 0;
  1646. pos = azx_get_position(chip, azx_dev);
  1647. if (chip->position_fix == POS_FIX_AUTO) {
  1648. if (!pos) {
  1649. printk(KERN_WARNING
  1650. "hda-intel: Invalid position buffer, "
  1651. "using LPIB read method instead.\n");
  1652. chip->position_fix = POS_FIX_LPIB;
  1653. pos = azx_get_position(chip, azx_dev);
  1654. } else
  1655. chip->position_fix = POS_FIX_POSBUF;
  1656. }
  1657. if (!bdl_pos_adj[chip->dev_index])
  1658. return 1; /* no delayed ack */
  1659. if (WARN_ONCE(!azx_dev->period_bytes,
  1660. "hda-intel: zero azx_dev->period_bytes"))
  1661. return 0; /* this shouldn't happen! */
  1662. if (pos % azx_dev->period_bytes > azx_dev->period_bytes / 2)
  1663. return 0; /* NG - it's below the period boundary */
  1664. return 1; /* OK, it's fine */
  1665. }
  1666. /*
  1667. * The work for pending PCM period updates.
  1668. */
  1669. static void azx_irq_pending_work(struct work_struct *work)
  1670. {
  1671. struct azx *chip = container_of(work, struct azx, irq_pending_work);
  1672. int i, pending;
  1673. if (!chip->irq_pending_warned) {
  1674. printk(KERN_WARNING
  1675. "hda-intel: IRQ timing workaround is activated "
  1676. "for card #%d. Suggest a bigger bdl_pos_adj.\n",
  1677. chip->card->number);
  1678. chip->irq_pending_warned = 1;
  1679. }
  1680. for (;;) {
  1681. pending = 0;
  1682. spin_lock_irq(&chip->reg_lock);
  1683. for (i = 0; i < chip->num_streams; i++) {
  1684. struct azx_dev *azx_dev = &chip->azx_dev[i];
  1685. if (!azx_dev->irq_pending ||
  1686. !azx_dev->substream ||
  1687. !azx_dev->running)
  1688. continue;
  1689. if (azx_position_ok(chip, azx_dev)) {
  1690. azx_dev->irq_pending = 0;
  1691. spin_unlock(&chip->reg_lock);
  1692. snd_pcm_period_elapsed(azx_dev->substream);
  1693. spin_lock(&chip->reg_lock);
  1694. } else
  1695. pending++;
  1696. }
  1697. spin_unlock_irq(&chip->reg_lock);
  1698. if (!pending)
  1699. return;
  1700. cond_resched();
  1701. }
  1702. }
  1703. /* clear irq_pending flags and assure no on-going workq */
  1704. static void azx_clear_irq_pending(struct azx *chip)
  1705. {
  1706. int i;
  1707. spin_lock_irq(&chip->reg_lock);
  1708. for (i = 0; i < chip->num_streams; i++)
  1709. chip->azx_dev[i].irq_pending = 0;
  1710. spin_unlock_irq(&chip->reg_lock);
  1711. }
  1712. static struct snd_pcm_ops azx_pcm_ops = {
  1713. .open = azx_pcm_open,
  1714. .close = azx_pcm_close,
  1715. .ioctl = snd_pcm_lib_ioctl,
  1716. .hw_params = azx_pcm_hw_params,
  1717. .hw_free = azx_pcm_hw_free,
  1718. .prepare = azx_pcm_prepare,
  1719. .trigger = azx_pcm_trigger,
  1720. .pointer = azx_pcm_pointer,
  1721. .page = snd_pcm_sgbuf_ops_page,
  1722. };
  1723. static void azx_pcm_free(struct snd_pcm *pcm)
  1724. {
  1725. struct azx_pcm *apcm = pcm->private_data;
  1726. if (apcm) {
  1727. apcm->chip->pcm[pcm->device] = NULL;
  1728. kfree(apcm);
  1729. }
  1730. }
  1731. static int
  1732. azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
  1733. struct hda_pcm *cpcm)
  1734. {
  1735. struct azx *chip = bus->private_data;
  1736. struct snd_pcm *pcm;
  1737. struct azx_pcm *apcm;
  1738. int pcm_dev = cpcm->device;
  1739. int s, err;
  1740. if (pcm_dev >= HDA_MAX_PCMS) {
  1741. snd_printk(KERN_ERR SFX "Invalid PCM device number %d\n",
  1742. pcm_dev);
  1743. return -EINVAL;
  1744. }
  1745. if (chip->pcm[pcm_dev]) {
  1746. snd_printk(KERN_ERR SFX "PCM %d already exists\n", pcm_dev);
  1747. return -EBUSY;
  1748. }
  1749. err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
  1750. cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
  1751. cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
  1752. &pcm);
  1753. if (err < 0)
  1754. return err;
  1755. strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
  1756. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  1757. if (apcm == NULL)
  1758. return -ENOMEM;
  1759. apcm->chip = chip;
  1760. apcm->codec = codec;
  1761. pcm->private_data = apcm;
  1762. pcm->private_free = azx_pcm_free;
  1763. if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
  1764. pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
  1765. chip->pcm[pcm_dev] = pcm;
  1766. cpcm->pcm = pcm;
  1767. for (s = 0; s < 2; s++) {
  1768. apcm->hinfo[s] = &cpcm->stream[s];
  1769. if (cpcm->stream[s].substreams)
  1770. snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
  1771. }
  1772. /* buffer pre-allocation */
  1773. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1774. snd_dma_pci_data(chip->pci),
  1775. 1024 * 64, 32 * 1024 * 1024);
  1776. return 0;
  1777. }
  1778. /*
  1779. * mixer creation - all stuff is implemented in hda module
  1780. */
  1781. static int __devinit azx_mixer_create(struct azx *chip)
  1782. {
  1783. return snd_hda_build_controls(chip->bus);
  1784. }
  1785. /*
  1786. * initialize SD streams
  1787. */
  1788. static int __devinit azx_init_stream(struct azx *chip)
  1789. {
  1790. int i;
  1791. /* initialize each stream (aka device)
  1792. * assign the starting bdl address to each stream (device)
  1793. * and initialize
  1794. */
  1795. for (i = 0; i < chip->num_streams; i++) {
  1796. struct azx_dev *azx_dev = &chip->azx_dev[i];
  1797. azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
  1798. /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  1799. azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
  1800. /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
  1801. azx_dev->sd_int_sta_mask = 1 << i;
  1802. /* stream tag: must be non-zero and unique */
  1803. azx_dev->index = i;
  1804. azx_dev->stream_tag = i + 1;
  1805. }
  1806. return 0;
  1807. }
  1808. static int azx_acquire_irq(struct azx *chip, int do_disconnect)
  1809. {
  1810. if (request_irq(chip->pci->irq, azx_interrupt,
  1811. chip->msi ? 0 : IRQF_SHARED,
  1812. "hda_intel", chip)) {
  1813. printk(KERN_ERR "hda-intel: unable to grab IRQ %d, "
  1814. "disabling device\n", chip->pci->irq);
  1815. if (do_disconnect)
  1816. snd_card_disconnect(chip->card);
  1817. return -1;
  1818. }
  1819. chip->irq = chip->pci->irq;
  1820. pci_intx(chip->pci, !chip->msi);
  1821. return 0;
  1822. }
  1823. static void azx_stop_chip(struct azx *chip)
  1824. {
  1825. if (!chip->initialized)
  1826. return;
  1827. /* disable interrupts */
  1828. azx_int_disable(chip);
  1829. azx_int_clear(chip);
  1830. /* disable CORB/RIRB */
  1831. azx_free_cmd_io(chip);
  1832. /* disable position buffer */
  1833. azx_writel(chip, DPLBASE, 0);
  1834. azx_writel(chip, DPUBASE, 0);
  1835. chip->initialized = 0;
  1836. }
  1837. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1838. /* power-up/down the controller */
  1839. static void azx_power_notify(struct hda_bus *bus)
  1840. {
  1841. struct azx *chip = bus->private_data;
  1842. struct hda_codec *c;
  1843. int power_on = 0;
  1844. list_for_each_entry(c, &bus->codec_list, list) {
  1845. if (c->power_on) {
  1846. power_on = 1;
  1847. break;
  1848. }
  1849. }
  1850. if (power_on)
  1851. azx_init_chip(chip);
  1852. else if (chip->running && power_save_controller &&
  1853. !bus->power_keep_link_on)
  1854. azx_stop_chip(chip);
  1855. }
  1856. #endif /* CONFIG_SND_HDA_POWER_SAVE */
  1857. #ifdef CONFIG_PM
  1858. /*
  1859. * power management
  1860. */
  1861. static int snd_hda_codecs_inuse(struct hda_bus *bus)
  1862. {
  1863. struct hda_codec *codec;
  1864. list_for_each_entry(codec, &bus->codec_list, list) {
  1865. if (snd_hda_codec_needs_resume(codec))
  1866. return 1;
  1867. }
  1868. return 0;
  1869. }
  1870. static int azx_suspend(struct pci_dev *pci, pm_message_t state)
  1871. {
  1872. struct snd_card *card = pci_get_drvdata(pci);
  1873. struct azx *chip = card->private_data;
  1874. int i;
  1875. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1876. azx_clear_irq_pending(chip);
  1877. for (i = 0; i < HDA_MAX_PCMS; i++)
  1878. snd_pcm_suspend_all(chip->pcm[i]);
  1879. if (chip->initialized)
  1880. snd_hda_suspend(chip->bus);
  1881. azx_stop_chip(chip);
  1882. if (chip->irq >= 0) {
  1883. free_irq(chip->irq, chip);
  1884. chip->irq = -1;
  1885. }
  1886. if (chip->msi)
  1887. pci_disable_msi(chip->pci);
  1888. pci_disable_device(pci);
  1889. pci_save_state(pci);
  1890. pci_set_power_state(pci, pci_choose_state(pci, state));
  1891. return 0;
  1892. }
  1893. static int azx_resume(struct pci_dev *pci)
  1894. {
  1895. struct snd_card *card = pci_get_drvdata(pci);
  1896. struct azx *chip = card->private_data;
  1897. pci_set_power_state(pci, PCI_D0);
  1898. pci_restore_state(pci);
  1899. if (pci_enable_device(pci) < 0) {
  1900. printk(KERN_ERR "hda-intel: pci_enable_device failed, "
  1901. "disabling device\n");
  1902. snd_card_disconnect(card);
  1903. return -EIO;
  1904. }
  1905. pci_set_master(pci);
  1906. if (chip->msi)
  1907. if (pci_enable_msi(pci) < 0)
  1908. chip->msi = 0;
  1909. if (azx_acquire_irq(chip, 1) < 0)
  1910. return -EIO;
  1911. azx_init_pci(chip);
  1912. if (snd_hda_codecs_inuse(chip->bus))
  1913. azx_init_chip(chip);
  1914. snd_hda_resume(chip->bus);
  1915. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1916. return 0;
  1917. }
  1918. #endif /* CONFIG_PM */
  1919. /*
  1920. * reboot notifier for hang-up problem at power-down
  1921. */
  1922. static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
  1923. {
  1924. struct azx *chip = container_of(nb, struct azx, reboot_notifier);
  1925. snd_hda_bus_reboot_notify(chip->bus);
  1926. azx_stop_chip(chip);
  1927. return NOTIFY_OK;
  1928. }
  1929. static void azx_notifier_register(struct azx *chip)
  1930. {
  1931. chip->reboot_notifier.notifier_call = azx_halt;
  1932. register_reboot_notifier(&chip->reboot_notifier);
  1933. }
  1934. static void azx_notifier_unregister(struct azx *chip)
  1935. {
  1936. if (chip->reboot_notifier.notifier_call)
  1937. unregister_reboot_notifier(&chip->reboot_notifier);
  1938. }
  1939. /*
  1940. * destructor
  1941. */
  1942. static int azx_free(struct azx *chip)
  1943. {
  1944. int i;
  1945. azx_notifier_unregister(chip);
  1946. if (chip->initialized) {
  1947. azx_clear_irq_pending(chip);
  1948. for (i = 0; i < chip->num_streams; i++)
  1949. azx_stream_stop(chip, &chip->azx_dev[i]);
  1950. azx_stop_chip(chip);
  1951. }
  1952. if (chip->irq >= 0)
  1953. free_irq(chip->irq, (void*)chip);
  1954. if (chip->msi)
  1955. pci_disable_msi(chip->pci);
  1956. if (chip->remap_addr)
  1957. iounmap(chip->remap_addr);
  1958. if (chip->azx_dev) {
  1959. for (i = 0; i < chip->num_streams; i++)
  1960. if (chip->azx_dev[i].bdl.area)
  1961. snd_dma_free_pages(&chip->azx_dev[i].bdl);
  1962. }
  1963. if (chip->rb.area)
  1964. snd_dma_free_pages(&chip->rb);
  1965. if (chip->posbuf.area)
  1966. snd_dma_free_pages(&chip->posbuf);
  1967. pci_release_regions(chip->pci);
  1968. pci_disable_device(chip->pci);
  1969. kfree(chip->azx_dev);
  1970. kfree(chip);
  1971. return 0;
  1972. }
  1973. static int azx_dev_free(struct snd_device *device)
  1974. {
  1975. return azx_free(device->device_data);
  1976. }
  1977. /*
  1978. * white/black-listing for position_fix
  1979. */
  1980. static struct snd_pci_quirk position_fix_list[] __devinitdata = {
  1981. SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
  1982. SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
  1983. SND_PCI_QUIRK(0x1028, 0x01f6, "Dell Latitude 131L", POS_FIX_LPIB),
  1984. SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
  1985. SND_PCI_QUIRK(0x1106, 0x3288, "ASUS M2V-MX SE", POS_FIX_LPIB),
  1986. SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
  1987. SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
  1988. SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
  1989. SND_PCI_QUIRK(0x1565, 0x820f, "Biostar Microtech", POS_FIX_LPIB),
  1990. SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
  1991. SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
  1992. SND_PCI_QUIRK(0x8086, 0xd601, "eMachines T5212", POS_FIX_LPIB),
  1993. {}
  1994. };
  1995. static int __devinit check_position_fix(struct azx *chip, int fix)
  1996. {
  1997. const struct snd_pci_quirk *q;
  1998. switch (fix) {
  1999. case POS_FIX_LPIB:
  2000. case POS_FIX_POSBUF:
  2001. return fix;
  2002. }
  2003. /* Check VIA/ATI HD Audio Controller exist */
  2004. switch (chip->driver_type) {
  2005. case AZX_DRIVER_VIA:
  2006. case AZX_DRIVER_ATI:
  2007. chip->via_dmapos_patch = 1;
  2008. /* Use link position directly, avoid any transfer problem. */
  2009. return POS_FIX_LPIB;
  2010. }
  2011. chip->via_dmapos_patch = 0;
  2012. q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
  2013. if (q) {
  2014. printk(KERN_INFO
  2015. "hda_intel: position_fix set to %d "
  2016. "for device %04x:%04x\n",
  2017. q->value, q->subvendor, q->subdevice);
  2018. return q->value;
  2019. }
  2020. return POS_FIX_AUTO;
  2021. }
  2022. /*
  2023. * black-lists for probe_mask
  2024. */
  2025. static struct snd_pci_quirk probe_mask_list[] __devinitdata = {
  2026. /* Thinkpad often breaks the controller communication when accessing
  2027. * to the non-working (or non-existing) modem codec slot.
  2028. */
  2029. SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
  2030. SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
  2031. SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
  2032. /* broken BIOS */
  2033. SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
  2034. /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
  2035. SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
  2036. /* forced codec slots */
  2037. SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
  2038. SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
  2039. {}
  2040. };
  2041. #define AZX_FORCE_CODEC_MASK 0x100
  2042. static void __devinit check_probe_mask(struct azx *chip, int dev)
  2043. {
  2044. const struct snd_pci_quirk *q;
  2045. chip->codec_probe_mask = probe_mask[dev];
  2046. if (chip->codec_probe_mask == -1) {
  2047. q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
  2048. if (q) {
  2049. printk(KERN_INFO
  2050. "hda_intel: probe_mask set to 0x%x "
  2051. "for device %04x:%04x\n",
  2052. q->value, q->subvendor, q->subdevice);
  2053. chip->codec_probe_mask = q->value;
  2054. }
  2055. }
  2056. /* check forced option */
  2057. if (chip->codec_probe_mask != -1 &&
  2058. (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
  2059. chip->codec_mask = chip->codec_probe_mask & 0xff;
  2060. printk(KERN_INFO "hda_intel: codec_mask forced to 0x%x\n",
  2061. chip->codec_mask);
  2062. }
  2063. }
  2064. /*
  2065. * white/black-list for enable_msi
  2066. */
  2067. static struct snd_pci_quirk msi_black_list[] __devinitdata = {
  2068. SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
  2069. SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
  2070. SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
  2071. SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
  2072. SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
  2073. {}
  2074. };
  2075. static void __devinit check_msi(struct azx *chip)
  2076. {
  2077. const struct snd_pci_quirk *q;
  2078. if (enable_msi >= 0) {
  2079. chip->msi = !!enable_msi;
  2080. return;
  2081. }
  2082. chip->msi = 1; /* enable MSI as default */
  2083. q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
  2084. if (q) {
  2085. printk(KERN_INFO
  2086. "hda_intel: msi for device %04x:%04x set to %d\n",
  2087. q->subvendor, q->subdevice, q->value);
  2088. chip->msi = q->value;
  2089. return;
  2090. }
  2091. /* NVidia chipsets seem to cause troubles with MSI */
  2092. if (chip->driver_type == AZX_DRIVER_NVIDIA) {
  2093. printk(KERN_INFO "hda_intel: Disable MSI for Nvidia chipset\n");
  2094. chip->msi = 0;
  2095. }
  2096. }
  2097. /*
  2098. * constructor
  2099. */
  2100. static int __devinit azx_create(struct snd_card *card, struct pci_dev *pci,
  2101. int dev, int driver_type,
  2102. struct azx **rchip)
  2103. {
  2104. struct azx *chip;
  2105. int i, err;
  2106. unsigned short gcap;
  2107. static struct snd_device_ops ops = {
  2108. .dev_free = azx_dev_free,
  2109. };
  2110. *rchip = NULL;
  2111. err = pci_enable_device(pci);
  2112. if (err < 0)
  2113. return err;
  2114. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  2115. if (!chip) {
  2116. snd_printk(KERN_ERR SFX "cannot allocate chip\n");
  2117. pci_disable_device(pci);
  2118. return -ENOMEM;
  2119. }
  2120. spin_lock_init(&chip->reg_lock);
  2121. mutex_init(&chip->open_mutex);
  2122. chip->card = card;
  2123. chip->pci = pci;
  2124. chip->irq = -1;
  2125. chip->driver_type = driver_type;
  2126. check_msi(chip);
  2127. chip->dev_index = dev;
  2128. INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
  2129. chip->position_fix = check_position_fix(chip, position_fix[dev]);
  2130. check_probe_mask(chip, dev);
  2131. chip->single_cmd = single_cmd;
  2132. if (bdl_pos_adj[dev] < 0) {
  2133. switch (chip->driver_type) {
  2134. case AZX_DRIVER_ICH:
  2135. case AZX_DRIVER_PCH:
  2136. bdl_pos_adj[dev] = 1;
  2137. break;
  2138. default:
  2139. bdl_pos_adj[dev] = 32;
  2140. break;
  2141. }
  2142. }
  2143. #if BITS_PER_LONG != 64
  2144. /* Fix up base address on ULI M5461 */
  2145. if (chip->driver_type == AZX_DRIVER_ULI) {
  2146. u16 tmp3;
  2147. pci_read_config_word(pci, 0x40, &tmp3);
  2148. pci_write_config_word(pci, 0x40, tmp3 | 0x10);
  2149. pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
  2150. }
  2151. #endif
  2152. err = pci_request_regions(pci, "ICH HD audio");
  2153. if (err < 0) {
  2154. kfree(chip);
  2155. pci_disable_device(pci);
  2156. return err;
  2157. }
  2158. chip->addr = pci_resource_start(pci, 0);
  2159. chip->remap_addr = pci_ioremap_bar(pci, 0);
  2160. if (chip->remap_addr == NULL) {
  2161. snd_printk(KERN_ERR SFX "ioremap error\n");
  2162. err = -ENXIO;
  2163. goto errout;
  2164. }
  2165. if (chip->msi)
  2166. if (pci_enable_msi(pci) < 0)
  2167. chip->msi = 0;
  2168. if (azx_acquire_irq(chip, 0) < 0) {
  2169. err = -EBUSY;
  2170. goto errout;
  2171. }
  2172. pci_set_master(pci);
  2173. synchronize_irq(chip->irq);
  2174. gcap = azx_readw(chip, GCAP);
  2175. snd_printdd(SFX "chipset global capabilities = 0x%x\n", gcap);
  2176. /* disable SB600 64bit support for safety */
  2177. if ((chip->driver_type == AZX_DRIVER_ATI) ||
  2178. (chip->driver_type == AZX_DRIVER_ATIHDMI)) {
  2179. struct pci_dev *p_smbus;
  2180. p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
  2181. PCI_DEVICE_ID_ATI_SBX00_SMBUS,
  2182. NULL);
  2183. if (p_smbus) {
  2184. if (p_smbus->revision < 0x30)
  2185. gcap &= ~ICH6_GCAP_64OK;
  2186. pci_dev_put(p_smbus);
  2187. }
  2188. }
  2189. /* disable 64bit DMA address for Teradici */
  2190. /* it does not work with device 6549:1200 subsys e4a2:040b */
  2191. if (chip->driver_type == AZX_DRIVER_TERA)
  2192. gcap &= ~ICH6_GCAP_64OK;
  2193. /* allow 64bit DMA address if supported by H/W */
  2194. if ((gcap & ICH6_GCAP_64OK) && !pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
  2195. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
  2196. else {
  2197. pci_set_dma_mask(pci, DMA_BIT_MASK(32));
  2198. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
  2199. }
  2200. /* read number of streams from GCAP register instead of using
  2201. * hardcoded value
  2202. */
  2203. chip->capture_streams = (gcap >> 8) & 0x0f;
  2204. chip->playback_streams = (gcap >> 12) & 0x0f;
  2205. if (!chip->playback_streams && !chip->capture_streams) {
  2206. /* gcap didn't give any info, switching to old method */
  2207. switch (chip->driver_type) {
  2208. case AZX_DRIVER_ULI:
  2209. chip->playback_streams = ULI_NUM_PLAYBACK;
  2210. chip->capture_streams = ULI_NUM_CAPTURE;
  2211. break;
  2212. case AZX_DRIVER_ATIHDMI:
  2213. chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
  2214. chip->capture_streams = ATIHDMI_NUM_CAPTURE;
  2215. break;
  2216. case AZX_DRIVER_GENERIC:
  2217. default:
  2218. chip->playback_streams = ICH6_NUM_PLAYBACK;
  2219. chip->capture_streams = ICH6_NUM_CAPTURE;
  2220. break;
  2221. }
  2222. }
  2223. chip->capture_index_offset = 0;
  2224. chip->playback_index_offset = chip->capture_streams;
  2225. chip->num_streams = chip->playback_streams + chip->capture_streams;
  2226. chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
  2227. GFP_KERNEL);
  2228. if (!chip->azx_dev) {
  2229. snd_printk(KERN_ERR SFX "cannot malloc azx_dev\n");
  2230. goto errout;
  2231. }
  2232. for (i = 0; i < chip->num_streams; i++) {
  2233. /* allocate memory for the BDL for each stream */
  2234. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  2235. snd_dma_pci_data(chip->pci),
  2236. BDL_SIZE, &chip->azx_dev[i].bdl);
  2237. if (err < 0) {
  2238. snd_printk(KERN_ERR SFX "cannot allocate BDL\n");
  2239. goto errout;
  2240. }
  2241. }
  2242. /* allocate memory for the position buffer */
  2243. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  2244. snd_dma_pci_data(chip->pci),
  2245. chip->num_streams * 8, &chip->posbuf);
  2246. if (err < 0) {
  2247. snd_printk(KERN_ERR SFX "cannot allocate posbuf\n");
  2248. goto errout;
  2249. }
  2250. /* allocate CORB/RIRB */
  2251. err = azx_alloc_cmd_io(chip);
  2252. if (err < 0)
  2253. goto errout;
  2254. /* initialize streams */
  2255. azx_init_stream(chip);
  2256. /* initialize chip */
  2257. azx_init_pci(chip);
  2258. azx_init_chip(chip);
  2259. /* codec detection */
  2260. if (!chip->codec_mask) {
  2261. snd_printk(KERN_ERR SFX "no codecs found!\n");
  2262. err = -ENODEV;
  2263. goto errout;
  2264. }
  2265. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  2266. if (err <0) {
  2267. snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
  2268. goto errout;
  2269. }
  2270. strcpy(card->driver, "HDA-Intel");
  2271. strlcpy(card->shortname, driver_short_names[chip->driver_type],
  2272. sizeof(card->shortname));
  2273. snprintf(card->longname, sizeof(card->longname),
  2274. "%s at 0x%lx irq %i",
  2275. card->shortname, chip->addr, chip->irq);
  2276. *rchip = chip;
  2277. return 0;
  2278. errout:
  2279. azx_free(chip);
  2280. return err;
  2281. }
  2282. static void power_down_all_codecs(struct azx *chip)
  2283. {
  2284. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2285. /* The codecs were powered up in snd_hda_codec_new().
  2286. * Now all initialization done, so turn them down if possible
  2287. */
  2288. struct hda_codec *codec;
  2289. list_for_each_entry(codec, &chip->bus->codec_list, list) {
  2290. snd_hda_power_down(codec);
  2291. }
  2292. #endif
  2293. }
  2294. static int __devinit azx_probe(struct pci_dev *pci,
  2295. const struct pci_device_id *pci_id)
  2296. {
  2297. static int dev;
  2298. struct snd_card *card;
  2299. struct azx *chip;
  2300. int err;
  2301. if (dev >= SNDRV_CARDS)
  2302. return -ENODEV;
  2303. if (!enable[dev]) {
  2304. dev++;
  2305. return -ENOENT;
  2306. }
  2307. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  2308. if (err < 0) {
  2309. snd_printk(KERN_ERR SFX "Error creating card!\n");
  2310. return err;
  2311. }
  2312. /* set this here since it's referred in snd_hda_load_patch() */
  2313. snd_card_set_dev(card, &pci->dev);
  2314. err = azx_create(card, pci, dev, pci_id->driver_data, &chip);
  2315. if (err < 0)
  2316. goto out_free;
  2317. card->private_data = chip;
  2318. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  2319. chip->beep_mode = beep_mode[dev];
  2320. #endif
  2321. /* create codec instances */
  2322. err = azx_codec_create(chip, model[dev]);
  2323. if (err < 0)
  2324. goto out_free;
  2325. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  2326. if (patch[dev]) {
  2327. snd_printk(KERN_ERR SFX "Applying patch firmware '%s'\n",
  2328. patch[dev]);
  2329. err = snd_hda_load_patch(chip->bus, patch[dev]);
  2330. if (err < 0)
  2331. goto out_free;
  2332. }
  2333. #endif
  2334. if (!probe_only[dev]) {
  2335. err = azx_codec_configure(chip);
  2336. if (err < 0)
  2337. goto out_free;
  2338. }
  2339. /* create PCM streams */
  2340. err = snd_hda_build_pcms(chip->bus);
  2341. if (err < 0)
  2342. goto out_free;
  2343. /* create mixer controls */
  2344. err = azx_mixer_create(chip);
  2345. if (err < 0)
  2346. goto out_free;
  2347. err = snd_card_register(card);
  2348. if (err < 0)
  2349. goto out_free;
  2350. pci_set_drvdata(pci, card);
  2351. chip->running = 1;
  2352. power_down_all_codecs(chip);
  2353. azx_notifier_register(chip);
  2354. dev++;
  2355. return err;
  2356. out_free:
  2357. snd_card_free(card);
  2358. return err;
  2359. }
  2360. static void __devexit azx_remove(struct pci_dev *pci)
  2361. {
  2362. snd_card_free(pci_get_drvdata(pci));
  2363. pci_set_drvdata(pci, NULL);
  2364. }
  2365. /* PCI IDs */
  2366. static DEFINE_PCI_DEVICE_TABLE(azx_ids) = {
  2367. /* ICH 6..10 */
  2368. { PCI_DEVICE(0x8086, 0x2668), .driver_data = AZX_DRIVER_ICH },
  2369. { PCI_DEVICE(0x8086, 0x27d8), .driver_data = AZX_DRIVER_ICH },
  2370. { PCI_DEVICE(0x8086, 0x269a), .driver_data = AZX_DRIVER_ICH },
  2371. { PCI_DEVICE(0x8086, 0x284b), .driver_data = AZX_DRIVER_ICH },
  2372. { PCI_DEVICE(0x8086, 0x2911), .driver_data = AZX_DRIVER_ICH },
  2373. { PCI_DEVICE(0x8086, 0x293e), .driver_data = AZX_DRIVER_ICH },
  2374. { PCI_DEVICE(0x8086, 0x293f), .driver_data = AZX_DRIVER_ICH },
  2375. { PCI_DEVICE(0x8086, 0x3a3e), .driver_data = AZX_DRIVER_ICH },
  2376. { PCI_DEVICE(0x8086, 0x3a6e), .driver_data = AZX_DRIVER_ICH },
  2377. /* PCH */
  2378. { PCI_DEVICE(0x8086, 0x3b56), .driver_data = AZX_DRIVER_ICH },
  2379. { PCI_DEVICE(0x8086, 0x3b57), .driver_data = AZX_DRIVER_ICH },
  2380. /* CPT */
  2381. { PCI_DEVICE(0x8086, 0x1c20), .driver_data = AZX_DRIVER_PCH },
  2382. /* SCH */
  2383. { PCI_DEVICE(0x8086, 0x811b), .driver_data = AZX_DRIVER_SCH },
  2384. /* ATI SB 450/600 */
  2385. { PCI_DEVICE(0x1002, 0x437b), .driver_data = AZX_DRIVER_ATI },
  2386. { PCI_DEVICE(0x1002, 0x4383), .driver_data = AZX_DRIVER_ATI },
  2387. /* ATI HDMI */
  2388. { PCI_DEVICE(0x1002, 0x793b), .driver_data = AZX_DRIVER_ATIHDMI },
  2389. { PCI_DEVICE(0x1002, 0x7919), .driver_data = AZX_DRIVER_ATIHDMI },
  2390. { PCI_DEVICE(0x1002, 0x960f), .driver_data = AZX_DRIVER_ATIHDMI },
  2391. { PCI_DEVICE(0x1002, 0x970f), .driver_data = AZX_DRIVER_ATIHDMI },
  2392. { PCI_DEVICE(0x1002, 0xaa00), .driver_data = AZX_DRIVER_ATIHDMI },
  2393. { PCI_DEVICE(0x1002, 0xaa08), .driver_data = AZX_DRIVER_ATIHDMI },
  2394. { PCI_DEVICE(0x1002, 0xaa10), .driver_data = AZX_DRIVER_ATIHDMI },
  2395. { PCI_DEVICE(0x1002, 0xaa18), .driver_data = AZX_DRIVER_ATIHDMI },
  2396. { PCI_DEVICE(0x1002, 0xaa20), .driver_data = AZX_DRIVER_ATIHDMI },
  2397. { PCI_DEVICE(0x1002, 0xaa28), .driver_data = AZX_DRIVER_ATIHDMI },
  2398. { PCI_DEVICE(0x1002, 0xaa30), .driver_data = AZX_DRIVER_ATIHDMI },
  2399. { PCI_DEVICE(0x1002, 0xaa38), .driver_data = AZX_DRIVER_ATIHDMI },
  2400. { PCI_DEVICE(0x1002, 0xaa40), .driver_data = AZX_DRIVER_ATIHDMI },
  2401. { PCI_DEVICE(0x1002, 0xaa48), .driver_data = AZX_DRIVER_ATIHDMI },
  2402. /* VIA VT8251/VT8237A */
  2403. { PCI_DEVICE(0x1106, 0x3288), .driver_data = AZX_DRIVER_VIA },
  2404. /* SIS966 */
  2405. { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
  2406. /* ULI M5461 */
  2407. { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
  2408. /* NVIDIA MCP */
  2409. { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
  2410. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2411. .class_mask = 0xffffff,
  2412. .driver_data = AZX_DRIVER_NVIDIA },
  2413. /* Teradici */
  2414. { PCI_DEVICE(0x6549, 0x1200), .driver_data = AZX_DRIVER_TERA },
  2415. /* Creative X-Fi (CA0110-IBG) */
  2416. #if !defined(CONFIG_SND_CTXFI) && !defined(CONFIG_SND_CTXFI_MODULE)
  2417. /* the following entry conflicts with snd-ctxfi driver,
  2418. * as ctxfi driver mutates from HD-audio to native mode with
  2419. * a special command sequence.
  2420. */
  2421. { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
  2422. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2423. .class_mask = 0xffffff,
  2424. .driver_data = AZX_DRIVER_GENERIC },
  2425. #else
  2426. /* this entry seems still valid -- i.e. without emu20kx chip */
  2427. { PCI_DEVICE(0x1102, 0x0009), .driver_data = AZX_DRIVER_GENERIC },
  2428. #endif
  2429. /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
  2430. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
  2431. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2432. .class_mask = 0xffffff,
  2433. .driver_data = AZX_DRIVER_GENERIC },
  2434. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
  2435. .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
  2436. .class_mask = 0xffffff,
  2437. .driver_data = AZX_DRIVER_GENERIC },
  2438. { 0, }
  2439. };
  2440. MODULE_DEVICE_TABLE(pci, azx_ids);
  2441. /* pci_driver definition */
  2442. static struct pci_driver driver = {
  2443. .name = "HDA Intel",
  2444. .id_table = azx_ids,
  2445. .probe = azx_probe,
  2446. .remove = __devexit_p(azx_remove),
  2447. #ifdef CONFIG_PM
  2448. .suspend = azx_suspend,
  2449. .resume = azx_resume,
  2450. #endif
  2451. };
  2452. static int __init alsa_card_azx_init(void)
  2453. {
  2454. return pci_register_driver(&driver);
  2455. }
  2456. static void __exit alsa_card_azx_exit(void)
  2457. {
  2458. pci_unregister_driver(&driver);
  2459. }
  2460. module_init(alsa_card_azx_init)
  2461. module_exit(alsa_card_azx_exit)