blob: d96a14e1fa95af520402afe269f8ecf302866ce2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
Takashi Iwaid01ce992007-07-27 16:52:19 +02003 * hda_intel.c - Implementation of primary alsa driver code base
4 * for Intel HD Audio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/delay.h>
38#include <linux/interrupt.h>
Randy Dunlap362775e2005-11-07 14:43:23 +010039#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/module.h>
Andrew Morton24982c52008-03-04 10:08:58 +010041#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/moduleparam.h>
43#include <linux/init.h>
44#include <linux/slab.h>
45#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010046#include <linux/mutex.h>
Takashi Iwai0cbf0092008-10-29 16:18:25 +010047#include <linux/reboot.h>
Takashi Iwai27fe48d92011-09-28 17:16:09 +020048#include <linux/io.h>
Mengdong Linb8dfc4622012-08-23 17:32:30 +080049#include <linux/pm_runtime.h>
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -050050#include <linux/clocksource.h>
51#include <linux/time.h>
52
Takashi Iwai27fe48d92011-09-28 17:16:09 +020053#ifdef CONFIG_X86
54/* for snoop control */
55#include <asm/pgtable.h>
56#include <asm/cacheflush.h>
57#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <sound/core.h>
59#include <sound/initval.h>
Takashi Iwai91219472012-04-26 12:13:25 +020060#include <linux/vgaarb.h>
Takashi Iwaia82d51e2012-04-26 12:23:42 +020061#include <linux/vga_switcheroo.h>
Takashi Iwai4918cda2012-08-09 12:33:28 +020062#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "hda_codec.h"
64
65
Takashi Iwai5aba4f82008-01-07 15:16:37 +010066static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
67static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
Rusty Russella67ff6a2011-12-15 13:49:36 +103068static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Takashi Iwai5aba4f82008-01-07 15:16:37 +010069static char *model[SNDRV_CARDS];
Takashi Iwai1dac6692012-09-13 14:59:47 +020070static int position_fix[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +020071static int bdl_pos_adj[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
Takashi Iwai5aba4f82008-01-07 15:16:37 +010072static int probe_mask[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
Takashi Iwaid4d9cd032008-12-19 15:19:11 +010073static int probe_only[SNDRV_CARDS];
David Henningsson26a6cb62012-10-09 15:04:21 +020074static int jackpoll_ms[SNDRV_CARDS];
Rusty Russella67ff6a2011-12-15 13:49:36 +103075static bool single_cmd;
Takashi Iwai716238552009-09-28 13:14:04 +020076static int enable_msi = -1;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +020077#ifdef CONFIG_SND_HDA_PATCH_LOADER
78static char *patch[SNDRV_CARDS];
79#endif
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +010080#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwai0920c9b2012-07-03 16:58:48 +020081static bool beep_mode[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] =
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +010082 CONFIG_SND_HDA_INPUT_BEEP_MODE};
83#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Takashi Iwai5aba4f82008-01-07 15:16:37 +010085module_param_array(index, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010087module_param_array(id, charp, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010089module_param_array(enable, bool, NULL, 0444);
90MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
91module_param_array(model, charp, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092MODULE_PARM_DESC(model, "Use the given board model.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010093module_param_array(position_fix, int, NULL, 0444);
David Henningsson4cb36312010-09-30 10:12:50 +020094MODULE_PARM_DESC(position_fix, "DMA pointer read method."
Takashi Iwai1dac6692012-09-13 14:59:47 +020095 "(-1 = system default, 0 = auto, 1 = LPIB, 2 = POSBUF, 3 = VIACOMBO, 4 = COMBO).");
Takashi Iwai555e2192008-06-10 17:53:34 +020096module_param_array(bdl_pos_adj, int, NULL, 0644);
97MODULE_PARM_DESC(bdl_pos_adj, "BDL position adjustment offset.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010098module_param_array(probe_mask, int, NULL, 0444);
Takashi Iwai606ad752005-11-24 16:03:40 +010099MODULE_PARM_DESC(probe_mask, "Bitmask to probe codecs (default = -1).");
Jaroslav Kysela079e6832010-03-26 11:16:59 +0100100module_param_array(probe_only, int, NULL, 0444);
Takashi Iwaid4d9cd032008-12-19 15:19:11 +0100101MODULE_PARM_DESC(probe_only, "Only probing and no codec initialization.");
David Henningsson26a6cb62012-10-09 15:04:21 +0200102module_param_array(jackpoll_ms, int, NULL, 0444);
103MODULE_PARM_DESC(jackpoll_ms, "Ms between polling for jack events (default = 0, using unsol events only)");
Takashi Iwai27346162006-01-12 18:28:44 +0100104module_param(single_cmd, bool, 0444);
Takashi Iwaid01ce992007-07-27 16:52:19 +0200105MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
106 "(for debugging only).");
Takashi Iwaiac9ef6c2012-01-20 12:08:44 +0100107module_param(enable_msi, bint, 0444);
Takashi Iwai134a11f2006-11-10 12:08:37 +0100108MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200109#ifdef CONFIG_SND_HDA_PATCH_LOADER
110module_param_array(patch, charp, NULL, 0444);
111MODULE_PARM_DESC(patch, "Patch file for Intel HD audio interface.");
112#endif
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100113#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwai0920c9b2012-07-03 16:58:48 +0200114module_param_array(beep_mode, bool, NULL, 0444);
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100115MODULE_PARM_DESC(beep_mode, "Select HDA Beep registration mode "
Takashi Iwai0920c9b2012-07-03 16:58:48 +0200116 "(0=off, 1=on) (default=1).");
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100117#endif
Takashi Iwai606ad752005-11-24 16:03:40 +0100118
Takashi Iwai83012a72012-08-24 18:38:08 +0200119#ifdef CONFIG_PM
Takashi Iwai65fcd412012-08-14 17:13:32 +0200120static int param_set_xint(const char *val, const struct kernel_param *kp);
121static struct kernel_param_ops param_ops_xint = {
122 .set = param_set_xint,
123 .get = param_get_int,
124};
125#define param_check_xint param_check_int
126
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100127static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
Takashi Iwai65fcd412012-08-14 17:13:32 +0200128module_param(power_save, xint, 0644);
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100129MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
130 "(in second, 0 = disable).");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Takashi Iwaidee1b662007-08-13 16:10:30 +0200132/* reset the HD-audio controller in power save mode.
133 * this may give more power-saving, but will take longer time to
134 * wake up.
135 */
Rusty Russella67ff6a2011-12-15 13:49:36 +1030136static bool power_save_controller = 1;
Takashi Iwaidee1b662007-08-13 16:10:30 +0200137module_param(power_save_controller, bool, 0644);
138MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
Takashi Iwai83012a72012-08-24 18:38:08 +0200139#endif /* CONFIG_PM */
Takashi Iwaidee1b662007-08-13 16:10:30 +0200140
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100141static int align_buffer_size = -1;
142module_param(align_buffer_size, bint, 0644);
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -0500143MODULE_PARM_DESC(align_buffer_size,
144 "Force buffer and period sizes to be multiple of 128 bytes.");
145
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200146#ifdef CONFIG_X86
147static bool hda_snoop = true;
148module_param_named(snoop, hda_snoop, bool, 0444);
149MODULE_PARM_DESC(snoop, "Enable/disable snooping");
150#define azx_snoop(chip) (chip)->snoop
151#else
152#define hda_snoop true
153#define azx_snoop(chip) true
154#endif
155
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157MODULE_LICENSE("GPL");
158MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
159 "{Intel, ICH6M},"
Jason Gaston2f1b3812005-05-01 08:58:50 -0700160 "{Intel, ICH7},"
Frederick Lif5d40b32005-05-12 14:55:20 +0200161 "{Intel, ESB2},"
Jason Gastond2981392006-01-10 11:07:37 +0100162 "{Intel, ICH8},"
Jason Gastonf9cc8a82006-11-22 11:53:52 +0100163 "{Intel, ICH9},"
Jason Gastonc34f5a02008-01-29 12:38:49 +0100164 "{Intel, ICH10},"
Seth Heasleyb29c2362008-08-08 15:56:39 -0700165 "{Intel, PCH},"
Seth Heasleyd2f2fcd2010-01-12 17:03:35 -0800166 "{Intel, CPT},"
Seth Heasleyd2edeb72011-04-20 10:59:57 -0700167 "{Intel, PPT},"
Seth Heasley8bc039a2012-01-23 16:24:31 -0800168 "{Intel, LPT},"
James Ralston144dad92012-08-09 09:38:59 -0700169 "{Intel, LPT_LP},"
Wang Xingchaoe926f2c2012-06-13 10:23:51 +0800170 "{Intel, HPT},"
Seth Heasleycea310e2010-09-10 16:29:56 -0700171 "{Intel, PBG},"
Tobin Davis4979bca2008-01-30 08:13:55 +0100172 "{Intel, SCH},"
Takashi Iwaifc20a562005-05-12 15:00:41 +0200173 "{ATI, SB450},"
Felix Kuehling89be83f2006-03-31 12:33:59 +0200174 "{ATI, SB600},"
Felix Kuehling778b6e12006-05-17 11:22:21 +0200175 "{ATI, RS600},"
Felix Kuehling5b15c952006-10-16 12:49:47 +0200176 "{ATI, RS690},"
Wolke Liue6db1112007-04-27 12:20:57 +0200177 "{ATI, RS780},"
178 "{ATI, R600},"
Herton Ronaldo Krzesinski2797f722007-11-05 18:21:56 +0100179 "{ATI, RV630},"
180 "{ATI, RV610},"
Wolke Liu27da1832007-11-16 11:06:30 +0100181 "{ATI, RV670},"
182 "{ATI, RV635},"
183 "{ATI, RV620},"
184 "{ATI, RV770},"
Takashi Iwaifc20a562005-05-12 15:00:41 +0200185 "{VIA, VT8251},"
Takashi Iwai47672312005-08-12 16:44:04 +0200186 "{VIA, VT8237A},"
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200187 "{SiS, SIS966},"
188 "{ULI, M5461}}");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189MODULE_DESCRIPTION("Intel HDA driver");
190
Takashi Iwai4abc1cc2009-05-19 12:16:46 +0200191#ifdef CONFIG_SND_VERBOSE_PRINTK
192#define SFX /* nop */
193#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#define SFX "hda-intel: "
Takashi Iwai4abc1cc2009-05-19 12:16:46 +0200195#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +0200196
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200197#if defined(CONFIG_PM) && defined(CONFIG_VGA_SWITCHEROO)
198#ifdef CONFIG_SND_HDA_CODEC_HDMI
199#define SUPPORT_VGA_SWITCHEROO
200#endif
201#endif
202
203
Takashi Iwaicb53c622007-08-10 17:21:45 +0200204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * registers
206 */
207#define ICH6_REG_GCAP 0x00
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200208#define ICH6_GCAP_64OK (1 << 0) /* 64bit address support */
209#define ICH6_GCAP_NSDO (3 << 1) /* # of serial data out signals */
210#define ICH6_GCAP_BSS (31 << 3) /* # of bidirectional streams */
211#define ICH6_GCAP_ISS (15 << 8) /* # of input streams */
212#define ICH6_GCAP_OSS (15 << 12) /* # of output streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define ICH6_REG_VMIN 0x02
214#define ICH6_REG_VMAJ 0x03
215#define ICH6_REG_OUTPAY 0x04
216#define ICH6_REG_INPAY 0x06
217#define ICH6_REG_GCTL 0x08
Takashi Iwai8a933ec2009-05-31 09:28:12 +0200218#define ICH6_GCTL_RESET (1 << 0) /* controller reset */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200219#define ICH6_GCTL_FCNTRL (1 << 1) /* flush control */
220#define ICH6_GCTL_UNSOL (1 << 8) /* accept unsol. response enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#define ICH6_REG_WAKEEN 0x0c
222#define ICH6_REG_STATESTS 0x0e
223#define ICH6_REG_GSTS 0x10
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200224#define ICH6_GSTS_FSTS (1 << 1) /* flush status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#define ICH6_REG_INTCTL 0x20
226#define ICH6_REG_INTSTS 0x24
Jaroslav Kyselae5463722010-05-11 10:21:46 +0200227#define ICH6_REG_WALLCLK 0x30 /* 24Mhz source */
Takashi Iwai8b0bd222011-06-10 14:56:26 +0200228#define ICH6_REG_OLD_SSYNC 0x34 /* SSYNC for old ICH */
229#define ICH6_REG_SSYNC 0x38
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#define ICH6_REG_CORBLBASE 0x40
231#define ICH6_REG_CORBUBASE 0x44
232#define ICH6_REG_CORBWP 0x48
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200233#define ICH6_REG_CORBRP 0x4a
234#define ICH6_CORBRP_RST (1 << 15) /* read pointer reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#define ICH6_REG_CORBCTL 0x4c
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200236#define ICH6_CORBCTL_RUN (1 << 1) /* enable DMA */
237#define ICH6_CORBCTL_CMEIE (1 << 0) /* enable memory error irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#define ICH6_REG_CORBSTS 0x4d
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200239#define ICH6_CORBSTS_CMEI (1 << 0) /* memory error indication */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#define ICH6_REG_CORBSIZE 0x4e
241
242#define ICH6_REG_RIRBLBASE 0x50
243#define ICH6_REG_RIRBUBASE 0x54
244#define ICH6_REG_RIRBWP 0x58
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200245#define ICH6_RIRBWP_RST (1 << 15) /* write pointer reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#define ICH6_REG_RINTCNT 0x5a
247#define ICH6_REG_RIRBCTL 0x5c
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200248#define ICH6_RBCTL_IRQ_EN (1 << 0) /* enable IRQ */
249#define ICH6_RBCTL_DMA_EN (1 << 1) /* enable DMA */
250#define ICH6_RBCTL_OVERRUN_EN (1 << 2) /* enable overrun irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#define ICH6_REG_RIRBSTS 0x5d
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200252#define ICH6_RBSTS_IRQ (1 << 0) /* response irq */
253#define ICH6_RBSTS_OVERRUN (1 << 2) /* overrun irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#define ICH6_REG_RIRBSIZE 0x5e
255
256#define ICH6_REG_IC 0x60
257#define ICH6_REG_IR 0x64
258#define ICH6_REG_IRS 0x68
259#define ICH6_IRS_VALID (1<<1)
260#define ICH6_IRS_BUSY (1<<0)
261
262#define ICH6_REG_DPLBASE 0x70
263#define ICH6_REG_DPUBASE 0x74
264#define ICH6_DPLBASE_ENABLE 0x1 /* Enable position buffer */
265
266/* SD offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
267enum { SDI0, SDI1, SDI2, SDI3, SDO0, SDO1, SDO2, SDO3 };
268
269/* stream register offsets from stream base */
270#define ICH6_REG_SD_CTL 0x00
271#define ICH6_REG_SD_STS 0x03
272#define ICH6_REG_SD_LPIB 0x04
273#define ICH6_REG_SD_CBL 0x08
274#define ICH6_REG_SD_LVI 0x0c
275#define ICH6_REG_SD_FIFOW 0x0e
276#define ICH6_REG_SD_FIFOSIZE 0x10
277#define ICH6_REG_SD_FORMAT 0x12
278#define ICH6_REG_SD_BDLPL 0x18
279#define ICH6_REG_SD_BDLPU 0x1c
280
281/* PCI space */
282#define ICH6_PCIREG_TCSEL 0x44
283
284/*
285 * other constants
286 */
287
288/* max number of SDs */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200289/* ICH, ATI and VIA have 4 playback and 4 capture */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200290#define ICH6_NUM_CAPTURE 4
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200291#define ICH6_NUM_PLAYBACK 4
292
293/* ULI has 6 playback and 5 capture */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200294#define ULI_NUM_CAPTURE 5
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200295#define ULI_NUM_PLAYBACK 6
296
Felix Kuehling778b6e12006-05-17 11:22:21 +0200297/* ATI HDMI has 1 playback and 0 capture */
Felix Kuehling778b6e12006-05-17 11:22:21 +0200298#define ATIHDMI_NUM_CAPTURE 0
Felix Kuehling778b6e12006-05-17 11:22:21 +0200299#define ATIHDMI_NUM_PLAYBACK 1
300
Kailang Yangf2690022008-05-27 11:44:55 +0200301/* TERA has 4 playback and 3 capture */
302#define TERA_NUM_CAPTURE 3
303#define TERA_NUM_PLAYBACK 4
304
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200305/* this number is statically defined for simplicity */
306#define MAX_AZX_DEV 16
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/* max number of fragments - we may use more if allocating more pages for BDL */
Takashi Iwai4ce107b2008-02-06 14:50:19 +0100309#define BDL_SIZE 4096
310#define AZX_MAX_BDL_ENTRIES (BDL_SIZE / 16)
311#define AZX_MAX_FRAG 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* max buffer size - no h/w limit, you can increase as you like */
313#define AZX_MAX_BUF_SIZE (1024*1024*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/* RIRB int mask: overrun[2], response[0] */
316#define RIRB_INT_RESPONSE 0x01
317#define RIRB_INT_OVERRUN 0x04
318#define RIRB_INT_MASK 0x05
319
Takashi Iwai2f5983f2008-09-03 16:00:44 +0200320/* STATESTS int mask: S3,SD2,SD1,SD0 */
Wei Ni7445dfc2010-03-03 15:05:53 +0800321#define AZX_MAX_CODECS 8
322#define AZX_DEFAULT_CODECS 4
Wu Fengguangdeadff12009-08-01 18:45:16 +0800323#define STATESTS_INT_MASK ((1 << AZX_MAX_CODECS) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325/* SD_CTL bits */
326#define SD_CTL_STREAM_RESET 0x01 /* stream reset bit */
327#define SD_CTL_DMA_START 0x02 /* stream DMA start bit */
Takashi Iwai850f0e52008-03-18 17:11:05 +0100328#define SD_CTL_STRIPE (3 << 16) /* stripe control */
329#define SD_CTL_TRAFFIC_PRIO (1 << 18) /* traffic priority */
330#define SD_CTL_DIR (1 << 19) /* bi-directional stream */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#define SD_CTL_STREAM_TAG_MASK (0xf << 20)
332#define SD_CTL_STREAM_TAG_SHIFT 20
333
334/* SD_CTL and SD_STS */
335#define SD_INT_DESC_ERR 0x10 /* descriptor error interrupt */
336#define SD_INT_FIFO_ERR 0x08 /* FIFO error interrupt */
337#define SD_INT_COMPLETE 0x04 /* completion interrupt */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200338#define SD_INT_MASK (SD_INT_DESC_ERR|SD_INT_FIFO_ERR|\
339 SD_INT_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/* SD_STS */
342#define SD_STS_FIFO_READY 0x20 /* FIFO ready */
343
344/* INTCTL and INTSTS */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200345#define ICH6_INT_ALL_STREAM 0xff /* all stream interrupts */
346#define ICH6_INT_CTRL_EN 0x40000000 /* controller interrupt enable bit */
347#define ICH6_INT_GLOBAL_EN 0x80000000 /* global interrupt enable bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* below are so far hardcoded - should read registers in future */
350#define ICH6_MAX_CORB_ENTRIES 256
351#define ICH6_MAX_RIRB_ENTRIES 256
352
Takashi Iwaic74db862005-05-12 14:26:27 +0200353/* position fix mode */
354enum {
Takashi Iwai0be3b5d2005-09-05 17:11:40 +0200355 POS_FIX_AUTO,
Takashi Iwaid2e1c972008-06-10 17:53:34 +0200356 POS_FIX_LPIB,
Takashi Iwai0be3b5d2005-09-05 17:11:40 +0200357 POS_FIX_POSBUF,
David Henningsson4cb36312010-09-30 10:12:50 +0200358 POS_FIX_VIACOMBO,
Takashi Iwaia6f2fd52012-02-28 11:58:40 +0100359 POS_FIX_COMBO,
Takashi Iwaic74db862005-05-12 14:26:27 +0200360};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Frederick Lif5d40b32005-05-12 14:55:20 +0200362/* Defines for ATI HD Audio support in SB450 south bridge */
Frederick Lif5d40b32005-05-12 14:55:20 +0200363#define ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR 0x42
364#define ATI_SB450_HDAUDIO_ENABLE_SNOOP 0x02
365
Vinod Gda3fca22005-09-13 18:49:12 +0200366/* Defines for Nvidia HDA support */
367#define NVIDIA_HDA_TRANSREG_ADDR 0x4e
368#define NVIDIA_HDA_ENABLE_COHBITS 0x0f
Peer Chen320dcc32008-08-20 16:43:24 -0700369#define NVIDIA_HDA_ISTRM_COH 0x4d
370#define NVIDIA_HDA_OSTRM_COH 0x4c
371#define NVIDIA_HDA_ENABLE_COHBIT 0x01
Frederick Lif5d40b32005-05-12 14:55:20 +0200372
Takashi Iwai90a5ad52008-02-22 18:36:22 +0100373/* Defines for Intel SCH HDA snoop control */
374#define INTEL_SCH_HDA_DEVC 0x78
375#define INTEL_SCH_HDA_DEVC_NOSNOOP (0x1<<11)
376
Joseph Chan0e153472008-08-26 14:38:03 +0200377/* Define IN stream 0 FIFO size offset in VIA controller */
378#define VIA_IN_STREAM0_FIFO_SIZE_OFFSET 0x90
379/* Define VIA HD Audio Device ID*/
380#define VIA_HDAC_DEVICE_ID 0x3288
381
Yang, Libinc4da29c2008-11-13 11:07:07 +0100382/* HD Audio class code */
383#define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
Takashi Iwai90a5ad52008-02-22 18:36:22 +0100384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
387
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100388struct azx_dev {
Takashi Iwai4ce107b2008-02-06 14:50:19 +0100389 struct snd_dma_buffer bdl; /* BDL buffer */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200390 u32 *posbuf; /* position buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Takashi Iwaid01ce992007-07-27 16:52:19 +0200392 unsigned int bufsize; /* size of the play buffer in bytes */
Takashi Iwai9ad593f2008-05-16 12:34:47 +0200393 unsigned int period_bytes; /* size of the period in bytes */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200394 unsigned int frags; /* number for period in the play buffer */
395 unsigned int fifo_size; /* FIFO size */
Jaroslav Kyselae5463722010-05-11 10:21:46 +0200396 unsigned long start_wallclk; /* start + minimum wallclk */
397 unsigned long period_wallclk; /* wallclk for period */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Takashi Iwaid01ce992007-07-27 16:52:19 +0200399 void __iomem *sd_addr; /* stream descriptor pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Takashi Iwaid01ce992007-07-27 16:52:19 +0200401 u32 sd_int_sta_mask; /* stream int status mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /* pcm support */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200404 struct snd_pcm_substream *substream; /* assigned substream,
405 * set in PCM open
406 */
407 unsigned int format_val; /* format value to be set in the
408 * controller and the codec
409 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 unsigned char stream_tag; /* assigned stream */
411 unsigned char index; /* stream index */
Takashi Iwaid5cf9912011-10-06 10:07:58 +0200412 int assigned_key; /* last device# key assigned to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Pavel Machek927fc862006-08-31 17:03:43 +0200414 unsigned int opened :1;
415 unsigned int running :1;
Takashi Iwai675f25d2008-06-10 17:53:20 +0200416 unsigned int irq_pending :1;
Joseph Chan0e153472008-08-26 14:38:03 +0200417 /*
418 * For VIA:
419 * A flag to ensure DMA position is 0
420 * when link position is not greater than FIFO size
421 */
422 unsigned int insufficient :1;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200423 unsigned int wc_marked:1;
Takashi Iwai915bf292012-09-11 15:19:10 +0200424 unsigned int no_period_wakeup:1;
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -0500425
426 struct timecounter azx_tc;
427 struct cyclecounter azx_cc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428};
429
430/* CORB/RIRB */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100431struct azx_rb {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 u32 *buf; /* CORB/RIRB buffer
433 * Each CORB entry is 4byte, RIRB is 8byte
434 */
435 dma_addr_t addr; /* physical address of CORB/RIRB buffer */
436 /* for RIRB */
437 unsigned short rp, wp; /* read/write pointers */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800438 int cmds[AZX_MAX_CODECS]; /* number of pending requests */
439 u32 res[AZX_MAX_CODECS]; /* last read value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440};
441
Takashi Iwai01b65bf2011-11-24 14:31:46 +0100442struct azx_pcm {
443 struct azx *chip;
444 struct snd_pcm *pcm;
445 struct hda_codec *codec;
446 struct hda_pcm_stream *hinfo[2];
447 struct list_head list;
448};
449
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100450struct azx {
451 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct pci_dev *pci;
Takashi Iwai555e2192008-06-10 17:53:34 +0200453 int dev_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200455 /* chip type specific */
456 int driver_type;
Takashi Iwai9477c582011-05-25 09:11:37 +0200457 unsigned int driver_caps;
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200458 int playback_streams;
459 int playback_index_offset;
460 int capture_streams;
461 int capture_index_offset;
462 int num_streams;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* pci resources */
465 unsigned long addr;
466 void __iomem *remap_addr;
467 int irq;
468
469 /* locks */
470 spinlock_t reg_lock;
Ingo Molnar62932df2006-01-16 16:34:20 +0100471 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200473 /* streams (x num_streams) */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100474 struct azx_dev *azx_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* PCM */
Takashi Iwai01b65bf2011-11-24 14:31:46 +0100477 struct list_head pcm_list; /* azx_pcm list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* HD codec */
480 unsigned short codec_mask;
Takashi Iwaif1eaaee2009-02-13 08:16:55 +0100481 int codec_probe_mask; /* copied from probe_mask option */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct hda_bus *bus;
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100483 unsigned int beep_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* CORB/RIRB */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100486 struct azx_rb corb;
487 struct azx_rb rirb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Takashi Iwai4ce107b2008-02-06 14:50:19 +0100489 /* CORB/RIRB and position buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct snd_dma_buffer rb;
491 struct snd_dma_buffer posbuf;
Takashi Iwaic74db862005-05-12 14:26:27 +0200492
Takashi Iwai4918cda2012-08-09 12:33:28 +0200493#ifdef CONFIG_SND_HDA_PATCH_LOADER
494 const struct firmware *fw;
495#endif
496
Takashi Iwaic74db862005-05-12 14:26:27 +0200497 /* flags */
Shahin Ghazinouribeaffc32010-05-11 08:19:55 +0200498 int position_fix[2]; /* for both playback/capture streams */
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200499 int poll_count;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200500 unsigned int running :1;
Pavel Machek927fc862006-08-31 17:03:43 +0200501 unsigned int initialized :1;
502 unsigned int single_cmd :1;
503 unsigned int polling_mode :1;
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200504 unsigned int msi :1;
Takashi Iwaia6a950a2008-06-10 17:53:35 +0200505 unsigned int irq_pending_warned :1;
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +0100506 unsigned int probing :1; /* codec probing phase */
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200507 unsigned int snoop:1;
Takashi Iwai52409aa2012-01-23 17:10:24 +0100508 unsigned int align_buffer_size:1;
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200509 unsigned int region_requested:1;
510
511 /* VGA-switcheroo setup */
512 unsigned int use_vga_switcheroo:1;
Takashi Iwai128960a2012-10-12 17:28:18 +0200513 unsigned int vga_switcheroo_registered:1;
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200514 unsigned int init_failed:1; /* delayed init failed */
515 unsigned int disabled:1; /* disabled by VGA-switcher */
Takashi Iwai43bbb6c2007-07-06 20:22:05 +0200516
517 /* for debugging */
Wu Fengguangfeb27342009-08-01 19:17:14 +0800518 unsigned int last_cmd[AZX_MAX_CODECS];
Takashi Iwai9ad593f2008-05-16 12:34:47 +0200519
520 /* for pending irqs */
521 struct work_struct irq_pending_work;
Takashi Iwai0cbf0092008-10-29 16:18:25 +0100522
523 /* reboot notifier (for mysterious hangup problem at power-down) */
524 struct notifier_block reboot_notifier;
Takashi Iwai65fcd412012-08-14 17:13:32 +0200525
526 /* card list (for power_save trigger) */
527 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528};
529
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200530/* driver types */
531enum {
532 AZX_DRIVER_ICH,
Seth Heasley32679f92010-02-22 17:31:09 -0800533 AZX_DRIVER_PCH,
Tobin Davis4979bca2008-01-30 08:13:55 +0100534 AZX_DRIVER_SCH,
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200535 AZX_DRIVER_ATI,
Felix Kuehling778b6e12006-05-17 11:22:21 +0200536 AZX_DRIVER_ATIHDMI,
Andiry Xu1815b342011-12-14 16:10:27 +0800537 AZX_DRIVER_ATIHDMI_NS,
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200538 AZX_DRIVER_VIA,
539 AZX_DRIVER_SIS,
540 AZX_DRIVER_ULI,
Vinod Gda3fca22005-09-13 18:49:12 +0200541 AZX_DRIVER_NVIDIA,
Kailang Yangf2690022008-05-27 11:44:55 +0200542 AZX_DRIVER_TERA,
Takashi Iwai14d34f12010-10-21 09:03:25 +0200543 AZX_DRIVER_CTX,
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200544 AZX_DRIVER_CTHDA,
Yang, Libinc4da29c2008-11-13 11:07:07 +0100545 AZX_DRIVER_GENERIC,
Takashi Iwai2f5983f2008-09-03 16:00:44 +0200546 AZX_NUM_DRIVERS, /* keep this as last entry */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200547};
548
Takashi Iwai9477c582011-05-25 09:11:37 +0200549/* driver quirks (capabilities) */
550/* bits 0-7 are used for indicating driver type */
551#define AZX_DCAPS_NO_TCSEL (1 << 8) /* No Intel TCSEL bit */
552#define AZX_DCAPS_NO_MSI (1 << 9) /* No MSI support */
553#define AZX_DCAPS_ATI_SNOOP (1 << 10) /* ATI snoop enable */
554#define AZX_DCAPS_NVIDIA_SNOOP (1 << 11) /* Nvidia snoop enable */
555#define AZX_DCAPS_SCH_SNOOP (1 << 12) /* SCH/PCH snoop enable */
556#define AZX_DCAPS_RIRB_DELAY (1 << 13) /* Long delay in read loop */
557#define AZX_DCAPS_RIRB_PRE_DELAY (1 << 14) /* Put a delay before read */
558#define AZX_DCAPS_CTX_WORKAROUND (1 << 15) /* X-Fi workaround */
559#define AZX_DCAPS_POSFIX_LPIB (1 << 16) /* Use LPIB as default */
560#define AZX_DCAPS_POSFIX_VIA (1 << 17) /* Use VIACOMBO as default */
561#define AZX_DCAPS_NO_64BIT (1 << 18) /* No 64bit address */
562#define AZX_DCAPS_SYNC_WRITE (1 << 19) /* sync each cmd write */
Takashi Iwai8b0bd222011-06-10 14:56:26 +0200563#define AZX_DCAPS_OLD_SSYNC (1 << 20) /* Old SSYNC reg for ICH */
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -0500564#define AZX_DCAPS_BUFSIZE (1 << 21) /* no buffer size alignment */
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100565#define AZX_DCAPS_ALIGN_BUFSIZE (1 << 22) /* buffer size alignment */
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200566#define AZX_DCAPS_4K_BDLE_BOUNDARY (1 << 23) /* BDLE in 4k boundary */
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -0500567#define AZX_DCAPS_COUNT_LPIB_DELAY (1 << 25) /* Take LPIB as delay */
Takashi Iwai9477c582011-05-25 09:11:37 +0200568
569/* quirks for ATI SB / AMD Hudson */
570#define AZX_DCAPS_PRESET_ATI_SB \
571 (AZX_DCAPS_ATI_SNOOP | AZX_DCAPS_NO_TCSEL | \
572 AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
573
574/* quirks for ATI/AMD HDMI */
575#define AZX_DCAPS_PRESET_ATI_HDMI \
576 (AZX_DCAPS_NO_TCSEL | AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
577
578/* quirks for Nvidia */
579#define AZX_DCAPS_PRESET_NVIDIA \
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100580 (AZX_DCAPS_NVIDIA_SNOOP | AZX_DCAPS_RIRB_DELAY | AZX_DCAPS_NO_MSI |\
581 AZX_DCAPS_ALIGN_BUFSIZE)
Takashi Iwai9477c582011-05-25 09:11:37 +0200582
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200583#define AZX_DCAPS_PRESET_CTHDA \
584 (AZX_DCAPS_NO_MSI | AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_4K_BDLE_BOUNDARY)
585
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200586/*
587 * VGA-switcher support
588 */
589#ifdef SUPPORT_VGA_SWITCHEROO
Takashi Iwai5cb543d2012-08-09 13:49:23 +0200590#define use_vga_switcheroo(chip) ((chip)->use_vga_switcheroo)
591#else
592#define use_vga_switcheroo(chip) 0
593#endif
594
595#if defined(SUPPORT_VGA_SWITCHEROO) || defined(CONFIG_SND_HDA_PATCH_LOADER)
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200596#define DELAYED_INIT_MARK
597#define DELAYED_INITDATA_MARK
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200598#else
599#define DELAYED_INIT_MARK __devinit
600#define DELAYED_INITDATA_MARK __devinitdata
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200601#endif
602
603static char *driver_short_names[] DELAYED_INITDATA_MARK = {
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200604 [AZX_DRIVER_ICH] = "HDA Intel",
Seth Heasley32679f92010-02-22 17:31:09 -0800605 [AZX_DRIVER_PCH] = "HDA Intel PCH",
Tobin Davis4979bca2008-01-30 08:13:55 +0100606 [AZX_DRIVER_SCH] = "HDA Intel MID",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200607 [AZX_DRIVER_ATI] = "HDA ATI SB",
Felix Kuehling778b6e12006-05-17 11:22:21 +0200608 [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
Andiry Xu1815b342011-12-14 16:10:27 +0800609 [AZX_DRIVER_ATIHDMI_NS] = "HDA ATI HDMI",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200610 [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
611 [AZX_DRIVER_SIS] = "HDA SIS966",
Vinod Gda3fca22005-09-13 18:49:12 +0200612 [AZX_DRIVER_ULI] = "HDA ULI M5461",
613 [AZX_DRIVER_NVIDIA] = "HDA NVidia",
Kailang Yangf2690022008-05-27 11:44:55 +0200614 [AZX_DRIVER_TERA] = "HDA Teradici",
Takashi Iwai14d34f12010-10-21 09:03:25 +0200615 [AZX_DRIVER_CTX] = "HDA Creative",
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200616 [AZX_DRIVER_CTHDA] = "HDA Creative",
Yang, Libinc4da29c2008-11-13 11:07:07 +0100617 [AZX_DRIVER_GENERIC] = "HD-Audio Generic",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200618};
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
621 * macros for easy use
622 */
623#define azx_writel(chip,reg,value) \
624 writel(value, (chip)->remap_addr + ICH6_REG_##reg)
625#define azx_readl(chip,reg) \
626 readl((chip)->remap_addr + ICH6_REG_##reg)
627#define azx_writew(chip,reg,value) \
628 writew(value, (chip)->remap_addr + ICH6_REG_##reg)
629#define azx_readw(chip,reg) \
630 readw((chip)->remap_addr + ICH6_REG_##reg)
631#define azx_writeb(chip,reg,value) \
632 writeb(value, (chip)->remap_addr + ICH6_REG_##reg)
633#define azx_readb(chip,reg) \
634 readb((chip)->remap_addr + ICH6_REG_##reg)
635
636#define azx_sd_writel(dev,reg,value) \
637 writel(value, (dev)->sd_addr + ICH6_REG_##reg)
638#define azx_sd_readl(dev,reg) \
639 readl((dev)->sd_addr + ICH6_REG_##reg)
640#define azx_sd_writew(dev,reg,value) \
641 writew(value, (dev)->sd_addr + ICH6_REG_##reg)
642#define azx_sd_readw(dev,reg) \
643 readw((dev)->sd_addr + ICH6_REG_##reg)
644#define azx_sd_writeb(dev,reg,value) \
645 writeb(value, (dev)->sd_addr + ICH6_REG_##reg)
646#define azx_sd_readb(dev,reg) \
647 readb((dev)->sd_addr + ICH6_REG_##reg)
648
649/* for pcm support */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100650#define get_azx_dev(substream) (substream->runtime->private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200652#ifdef CONFIG_X86
653static void __mark_pages_wc(struct azx *chip, void *addr, size_t size, bool on)
654{
655 if (azx_snoop(chip))
656 return;
657 if (addr && size) {
658 int pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
659 if (on)
660 set_memory_wc((unsigned long)addr, pages);
661 else
662 set_memory_wb((unsigned long)addr, pages);
663 }
664}
665
666static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
667 bool on)
668{
669 __mark_pages_wc(chip, buf->area, buf->bytes, on);
670}
671static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
672 struct snd_pcm_runtime *runtime, bool on)
673{
674 if (azx_dev->wc_marked != on) {
675 __mark_pages_wc(chip, runtime->dma_area, runtime->dma_bytes, on);
676 azx_dev->wc_marked = on;
677 }
678}
679#else
680/* NOP for other archs */
681static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
682 bool on)
683{
684}
685static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
686 struct snd_pcm_runtime *runtime, bool on)
687{
688}
689#endif
690
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200691static int azx_acquire_irq(struct azx *chip, int do_disconnect);
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200692static int azx_send_cmd(struct hda_bus *bus, unsigned int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/*
694 * Interface for HD codec
695 */
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/*
698 * CORB / RIRB interface
699 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100700static int azx_alloc_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 int err;
703
704 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200705 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
706 snd_dma_pci_data(chip->pci),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 PAGE_SIZE, &chip->rb);
708 if (err < 0) {
709 snd_printk(KERN_ERR SFX "cannot allocate CORB/RIRB\n");
710 return err;
711 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200712 mark_pages_wc(chip, &chip->rb, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714}
715
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100716static void azx_init_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800718 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* CORB set up */
720 chip->corb.addr = chip->rb.addr;
721 chip->corb.buf = (u32 *)chip->rb.area;
722 azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200723 azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200725 /* set the corb size to 256 entries (ULI requires explicitly) */
726 azx_writeb(chip, CORBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* set the corb write pointer to 0 */
728 azx_writew(chip, CORBWP, 0);
729 /* reset the corb hw read pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200730 azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /* enable corb dma */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200732 azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /* RIRB set up */
735 chip->rirb.addr = chip->rb.addr + 2048;
736 chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800737 chip->rirb.wp = chip->rirb.rp = 0;
738 memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200740 azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200742 /* set the rirb size to 256 entries (ULI requires explicitly) */
743 azx_writeb(chip, RIRBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /* reset the rirb hw write pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200745 azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /* set N=1, get RIRB response interrupt for new entry */
Takashi Iwai9477c582011-05-25 09:11:37 +0200747 if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
Takashi Iwai14d34f12010-10-21 09:03:25 +0200748 azx_writew(chip, RINTCNT, 0xc0);
749 else
750 azx_writew(chip, RINTCNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* enable rirb dma and response irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800753 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100756static void azx_free_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800758 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* disable ringbuffer DMAs */
760 azx_writeb(chip, RIRBCTL, 0);
761 azx_writeb(chip, CORBCTL, 0);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800762 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Wu Fengguangdeadff12009-08-01 18:45:16 +0800765static unsigned int azx_command_addr(u32 cmd)
766{
767 unsigned int addr = cmd >> 28;
768
769 if (addr >= AZX_MAX_CODECS) {
770 snd_BUG();
771 addr = 0;
772 }
773
774 return addr;
775}
776
777static unsigned int azx_response_addr(u32 res)
778{
779 unsigned int addr = res & 0xf;
780
781 if (addr >= AZX_MAX_CODECS) {
782 snd_BUG();
783 addr = 0;
784 }
785
786 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100790static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100792 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800793 unsigned int addr = azx_command_addr(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 unsigned int wp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Wu Fengguangc32649f2009-08-01 18:48:12 +0800796 spin_lock_irq(&chip->reg_lock);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* add command to corb */
799 wp = azx_readb(chip, CORBWP);
800 wp++;
801 wp %= ICH6_MAX_CORB_ENTRIES;
802
Wu Fengguangdeadff12009-08-01 18:45:16 +0800803 chip->rirb.cmds[addr]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 chip->corb.buf[wp] = cpu_to_le32(val);
805 azx_writel(chip, CORBWP, wp);
Wu Fengguangc32649f2009-08-01 18:48:12 +0800806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 spin_unlock_irq(&chip->reg_lock);
808
809 return 0;
810}
811
812#define ICH6_RIRB_EX_UNSOL_EV (1<<4)
813
814/* retrieve RIRB entry - called from interrupt handler */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100815static void azx_update_rirb(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 unsigned int rp, wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800818 unsigned int addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 u32 res, res_ex;
820
821 wp = azx_readb(chip, RIRBWP);
822 if (wp == chip->rirb.wp)
823 return;
824 chip->rirb.wp = wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 while (chip->rirb.rp != wp) {
827 chip->rirb.rp++;
828 chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
829
830 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
831 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
832 res = le32_to_cpu(chip->rirb.buf[rp]);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800833 addr = azx_response_addr(res_ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
835 snd_hda_queue_unsol_event(chip->bus, res, res_ex);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800836 else if (chip->rirb.cmds[addr]) {
837 chip->rirb.res[addr] = res;
Takashi Iwai2add9b92008-03-18 09:47:06 +0100838 smp_wmb();
Wu Fengguangdeadff12009-08-01 18:45:16 +0800839 chip->rirb.cmds[addr]--;
Wu Fengguange310bb02009-08-01 19:18:45 +0800840 } else
Takashi Iwai9e3d3522012-10-17 08:39:37 +0200841 snd_printk(KERN_ERR SFX "%s: spurious response %#x:%#x, "
Wu Fengguange310bb02009-08-01 19:18:45 +0800842 "last cmd=%#08x\n",
Takashi Iwai9e3d3522012-10-17 08:39:37 +0200843 pci_name(chip->pci),
Wu Fengguange310bb02009-08-01 19:18:45 +0800844 res, res_ex,
845 chip->last_cmd[addr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847}
848
849/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800850static unsigned int azx_rirb_get_response(struct hda_bus *bus,
851 unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100853 struct azx *chip = bus->private_data;
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200854 unsigned long timeout;
David Henningsson32cf4022012-05-04 11:05:55 +0200855 unsigned long loopcounter;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200856 int do_poll = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200858 again:
859 timeout = jiffies + msecs_to_jiffies(1000);
David Henningsson32cf4022012-05-04 11:05:55 +0200860
861 for (loopcounter = 0;; loopcounter++) {
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200862 if (chip->polling_mode || do_poll) {
Takashi Iwaie96224a2006-08-21 17:57:44 +0200863 spin_lock_irq(&chip->reg_lock);
864 azx_update_rirb(chip);
865 spin_unlock_irq(&chip->reg_lock);
866 }
Wu Fengguangdeadff12009-08-01 18:45:16 +0800867 if (!chip->rirb.cmds[addr]) {
Takashi Iwai2add9b92008-03-18 09:47:06 +0100868 smp_rmb();
Takashi Iwaib6132912009-03-24 07:36:09 +0100869 bus->rirb_error = 0;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200870
871 if (!do_poll)
872 chip->poll_count = 0;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800873 return chip->rirb.res[addr]; /* the last value */
Takashi Iwai2add9b92008-03-18 09:47:06 +0100874 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100875 if (time_after(jiffies, timeout))
876 break;
David Henningsson32cf4022012-05-04 11:05:55 +0200877 if (bus->needs_damn_long_delay || loopcounter > 3000)
Takashi Iwai52987652008-01-16 16:09:47 +0100878 msleep(2); /* temporary workaround */
879 else {
880 udelay(10);
881 cond_resched();
882 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100883 }
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200884
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200885 if (!chip->polling_mode && chip->poll_count < 2) {
886 snd_printdd(SFX "azx_get_response timeout, "
887 "polling the codec once: last cmd=0x%08x\n",
888 chip->last_cmd[addr]);
889 do_poll = 1;
890 chip->poll_count++;
891 goto again;
892 }
893
894
Takashi Iwai23c4a882009-10-30 13:21:49 +0100895 if (!chip->polling_mode) {
896 snd_printk(KERN_WARNING SFX "azx_get_response timeout, "
897 "switching to polling mode: last cmd=0x%08x\n",
898 chip->last_cmd[addr]);
899 chip->polling_mode = 1;
900 goto again;
901 }
902
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200903 if (chip->msi) {
Takashi Iwai4abc1cc2009-05-19 12:16:46 +0200904 snd_printk(KERN_WARNING SFX "No response from codec, "
Wu Fengguangfeb27342009-08-01 19:17:14 +0800905 "disabling MSI: last cmd=0x%08x\n",
906 chip->last_cmd[addr]);
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200907 free_irq(chip->irq, chip);
908 chip->irq = -1;
909 pci_disable_msi(chip->pci);
910 chip->msi = 0;
Takashi Iwaib6132912009-03-24 07:36:09 +0100911 if (azx_acquire_irq(chip, 1) < 0) {
912 bus->rirb_error = 1;
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200913 return -1;
Takashi Iwaib6132912009-03-24 07:36:09 +0100914 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200915 goto again;
916 }
917
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +0100918 if (chip->probing) {
919 /* If this critical timeout happens during the codec probing
920 * phase, this is likely an access to a non-existing codec
921 * slot. Better to return an error and reset the system.
922 */
923 return -1;
924 }
925
Takashi Iwai8dd78332009-06-02 01:16:07 +0200926 /* a fatal communication error; need either to reset or to fallback
927 * to the single_cmd mode
928 */
Takashi Iwaib6132912009-03-24 07:36:09 +0100929 bus->rirb_error = 1;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200930 if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200931 bus->response_reset = 1;
932 return -1; /* give a chance to retry */
933 }
934
935 snd_printk(KERN_ERR "hda_intel: azx_get_response timeout, "
936 "switching to single_cmd mode: last cmd=0x%08x\n",
Wu Fengguangfeb27342009-08-01 19:17:14 +0800937 chip->last_cmd[addr]);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200938 chip->single_cmd = 1;
939 bus->response_reset = 0;
Takashi Iwai1a696972009-11-07 09:49:04 +0100940 /* release CORB/RIRB */
Takashi Iwai4fcd3922009-05-25 18:34:52 +0200941 azx_free_cmd_io(chip);
Takashi Iwai1a696972009-11-07 09:49:04 +0100942 /* disable unsolicited responses */
943 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200944 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/*
948 * Use the single immediate command instead of CORB/RIRB for simplicity
949 *
950 * Note: according to Intel, this is not preferred use. The command was
951 * intended for the BIOS only, and may get confused with unsolicited
952 * responses. So, we shouldn't use it for normal operation from the
953 * driver.
954 * I left the codes, however, for debugging/testing purposes.
955 */
956
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200957/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800958static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200959{
960 int timeout = 50;
961
962 while (timeout--) {
963 /* check IRV busy bit */
964 if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
965 /* reuse rirb.res as the response return value */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800966 chip->rirb.res[addr] = azx_readl(chip, IR);
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200967 return 0;
968 }
969 udelay(1);
970 }
971 if (printk_ratelimit())
972 snd_printd(SFX "get_response timeout: IRS=0x%x\n",
973 azx_readw(chip, IRS));
Wu Fengguangdeadff12009-08-01 18:45:16 +0800974 chip->rirb.res[addr] = -1;
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200975 return -EIO;
976}
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100979static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100981 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800982 unsigned int addr = azx_command_addr(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int timeout = 50;
984
Takashi Iwai8dd78332009-06-02 01:16:07 +0200985 bus->rirb_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 while (timeout--) {
987 /* check ICB busy bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200988 if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* Clear IRV valid bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200990 azx_writew(chip, IRS, azx_readw(chip, IRS) |
991 ICH6_IRS_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 azx_writel(chip, IC, val);
Takashi Iwaid01ce992007-07-27 16:52:19 +0200993 azx_writew(chip, IRS, azx_readw(chip, IRS) |
994 ICH6_IRS_BUSY);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800995 return azx_single_wait_for_response(chip, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 udelay(1);
998 }
Marc Boucher1cfd52b2008-01-22 15:29:26 +0100999 if (printk_ratelimit())
1000 snd_printd(SFX "send_cmd timeout: IRS=0x%x, val=0x%x\n",
1001 azx_readw(chip, IRS), val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return -EIO;
1003}
1004
1005/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +08001006static unsigned int azx_single_get_response(struct hda_bus *bus,
1007 unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001009 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +08001010 return chip->rirb.res[addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Takashi Iwai111d3af2006-02-16 18:17:58 +01001013/*
1014 * The below are the main callbacks from hda_codec.
1015 *
1016 * They are just the skeleton to call sub-callbacks according to the
1017 * current setting of chip->single_cmd.
1018 */
1019
1020/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001021static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
Takashi Iwai111d3af2006-02-16 18:17:58 +01001022{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001023 struct azx *chip = bus->private_data;
Takashi Iwai43bbb6c2007-07-06 20:22:05 +02001024
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001025 if (chip->disabled)
1026 return 0;
Wu Fengguangfeb27342009-08-01 19:17:14 +08001027 chip->last_cmd[azx_command_addr(val)] = val;
Takashi Iwai111d3af2006-02-16 18:17:58 +01001028 if (chip->single_cmd)
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001029 return azx_single_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001030 else
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001031 return azx_corb_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001032}
1033
1034/* get a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +08001035static unsigned int azx_get_response(struct hda_bus *bus,
1036 unsigned int addr)
Takashi Iwai111d3af2006-02-16 18:17:58 +01001037{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001038 struct azx *chip = bus->private_data;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001039 if (chip->disabled)
1040 return 0;
Takashi Iwai111d3af2006-02-16 18:17:58 +01001041 if (chip->single_cmd)
Wu Fengguangdeadff12009-08-01 18:45:16 +08001042 return azx_single_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001043 else
Wu Fengguangdeadff12009-08-01 18:45:16 +08001044 return azx_rirb_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001045}
1046
Takashi Iwai83012a72012-08-24 18:38:08 +02001047#ifdef CONFIG_PM
Takashi Iwai68467f52012-08-28 09:14:29 -07001048static void azx_power_notify(struct hda_bus *bus, bool power_up);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001049#endif
Takashi Iwai111d3af2006-02-16 18:17:58 +01001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/* reset codec link */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001052static int azx_reset(struct azx *chip, int full_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 int count;
1055
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001056 if (!full_reset)
1057 goto __skip;
1058
Danny Tholene8a7f132007-09-11 21:41:56 +02001059 /* clear STATESTS */
1060 azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* reset controller */
1063 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
1064
1065 count = 50;
1066 while (azx_readb(chip, GCTL) && --count)
1067 msleep(1);
1068
1069 /* delay for >= 100us for codec PLL to settle per spec
1070 * Rev 0.9 section 5.5.1
1071 */
1072 msleep(1);
1073
1074 /* Bring controller out of reset */
1075 azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
1076
1077 count = 50;
Pavel Machek927fc862006-08-31 17:03:43 +02001078 while (!azx_readb(chip, GCTL) && --count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 msleep(1);
1080
Pavel Machek927fc862006-08-31 17:03:43 +02001081 /* Brent Chartrand said to wait >= 540us for codecs to initialize */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 msleep(1);
1083
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001084 __skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* check to see if controller is ready */
Pavel Machek927fc862006-08-31 17:03:43 +02001086 if (!azx_readb(chip, GCTL)) {
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02001087 snd_printd(SFX "azx_reset: controller not ready!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -EBUSY;
1089 }
1090
Matt41e2fce2005-07-04 17:49:55 +02001091 /* Accept unsolicited responses */
Takashi Iwai1a696972009-11-07 09:49:04 +01001092 if (!chip->single_cmd)
1093 azx_writel(chip, GCTL, azx_readl(chip, GCTL) |
1094 ICH6_GCTL_UNSOL);
Matt41e2fce2005-07-04 17:49:55 +02001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* detect codecs */
Pavel Machek927fc862006-08-31 17:03:43 +02001097 if (!chip->codec_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 chip->codec_mask = azx_readw(chip, STATESTS);
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02001099 snd_printdd(SFX "codec_mask = 0x%x\n", chip->codec_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101
1102 return 0;
1103}
1104
1105
1106/*
1107 * Lowlevel interface
1108 */
1109
1110/* enable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001111static void azx_int_enable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 /* enable controller CIE and GIE */
1114 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
1115 ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
1116}
1117
1118/* disable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001119static void azx_int_disable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 int i;
1122
1123 /* disable interrupts in stream descriptor */
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001124 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001125 struct azx_dev *azx_dev = &chip->azx_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 azx_sd_writeb(azx_dev, SD_CTL,
1127 azx_sd_readb(azx_dev, SD_CTL) & ~SD_INT_MASK);
1128 }
1129
1130 /* disable SIE for all streams */
1131 azx_writeb(chip, INTCTL, 0);
1132
1133 /* disable controller CIE and GIE */
1134 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
1135 ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
1136}
1137
1138/* clear interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001139static void azx_int_clear(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 int i;
1142
1143 /* clear stream status */
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001144 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001145 struct azx_dev *azx_dev = &chip->azx_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
1147 }
1148
1149 /* clear STATESTS */
1150 azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
1151
1152 /* clear rirb status */
1153 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1154
1155 /* clear int status */
1156 azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
1157}
1158
1159/* start a stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001160static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Joseph Chan0e153472008-08-26 14:38:03 +02001162 /*
1163 * Before stream start, initialize parameter
1164 */
1165 azx_dev->insufficient = 1;
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /* enable SIE */
Wei Niccc5df02010-01-26 15:59:33 +08001168 azx_writel(chip, INTCTL,
1169 azx_readl(chip, INTCTL) | (1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /* set DMA start and interrupt mask */
1171 azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
1172 SD_CTL_DMA_START | SD_INT_MASK);
1173}
1174
Takashi Iwai1dddab42009-03-18 15:15:37 +01001175/* stop DMA */
1176static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) &
1179 ~(SD_CTL_DMA_START | SD_INT_MASK));
1180 azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
Takashi Iwai1dddab42009-03-18 15:15:37 +01001181}
1182
1183/* stop a stream */
1184static void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
1185{
1186 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* disable SIE */
Wei Niccc5df02010-01-26 15:59:33 +08001188 azx_writel(chip, INTCTL,
1189 azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192
1193/*
Takashi Iwaicb53c622007-08-10 17:21:45 +02001194 * reset and start the controller registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001196static void azx_init_chip(struct azx *chip, int full_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001198 if (chip->initialized)
1199 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /* reset controller */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001202 azx_reset(chip, full_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /* initialize interrupts */
1205 azx_int_clear(chip);
1206 azx_int_enable(chip);
1207
1208 /* initialize the codec command I/O */
Takashi Iwai1a696972009-11-07 09:49:04 +01001209 if (!chip->single_cmd)
1210 azx_init_cmd_io(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02001212 /* program the position buffer */
1213 azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +02001214 azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr));
Frederick Lif5d40b32005-05-12 14:55:20 +02001215
Takashi Iwaicb53c622007-08-10 17:21:45 +02001216 chip->initialized = 1;
1217}
1218
1219/*
1220 * initialize the PCI registers
1221 */
1222/* update bits in a PCI register byte */
1223static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
1224 unsigned char mask, unsigned char val)
1225{
1226 unsigned char data;
1227
1228 pci_read_config_byte(pci, reg, &data);
1229 data &= ~mask;
1230 data |= (val & mask);
1231 pci_write_config_byte(pci, reg, data);
1232}
1233
1234static void azx_init_pci(struct azx *chip)
1235{
1236 /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
1237 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
1238 * Ensuring these bits are 0 clears playback static on some HD Audio
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +01001239 * codecs.
1240 * The PCI register TCSEL is defined in the Intel manuals.
Takashi Iwaicb53c622007-08-10 17:21:45 +02001241 */
Linus Torvalds46f2cc82011-05-27 19:45:28 -07001242 if (!(chip->driver_caps & AZX_DCAPS_NO_TCSEL)) {
Takashi Iwai9477c582011-05-25 09:11:37 +02001243 snd_printdd(SFX "Clearing TCSEL\n");
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +01001244 update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
Takashi Iwai9477c582011-05-25 09:11:37 +02001245 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02001246
Takashi Iwai9477c582011-05-25 09:11:37 +02001247 /* For ATI SB450/600/700/800/900 and AMD Hudson azalia HD audio,
1248 * we need to enable snoop.
1249 */
1250 if (chip->driver_caps & AZX_DCAPS_ATI_SNOOP) {
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001251 snd_printdd(SFX "Setting ATI snoop: %d\n", azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001252 update_pci_byte(chip->pci,
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001253 ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR, 0x07,
1254 azx_snoop(chip) ? ATI_SB450_HDAUDIO_ENABLE_SNOOP : 0);
Takashi Iwai9477c582011-05-25 09:11:37 +02001255 }
1256
1257 /* For NVIDIA HDA, enable snoop */
1258 if (chip->driver_caps & AZX_DCAPS_NVIDIA_SNOOP) {
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001259 snd_printdd(SFX "Setting Nvidia snoop: %d\n", azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001260 update_pci_byte(chip->pci,
1261 NVIDIA_HDA_TRANSREG_ADDR,
1262 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
Peer Chen320dcc32008-08-20 16:43:24 -07001263 update_pci_byte(chip->pci,
1264 NVIDIA_HDA_ISTRM_COH,
1265 0x01, NVIDIA_HDA_ENABLE_COHBIT);
1266 update_pci_byte(chip->pci,
1267 NVIDIA_HDA_OSTRM_COH,
1268 0x01, NVIDIA_HDA_ENABLE_COHBIT);
Takashi Iwai9477c582011-05-25 09:11:37 +02001269 }
1270
1271 /* Enable SCH/PCH snoop if needed */
1272 if (chip->driver_caps & AZX_DCAPS_SCH_SNOOP) {
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001273 unsigned short snoop;
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001274 pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001275 if ((!azx_snoop(chip) && !(snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)) ||
1276 (azx_snoop(chip) && (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP))) {
1277 snoop &= ~INTEL_SCH_HDA_DEVC_NOSNOOP;
1278 if (!azx_snoop(chip))
1279 snoop |= INTEL_SCH_HDA_DEVC_NOSNOOP;
1280 pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC, snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001281 pci_read_config_word(chip->pci,
1282 INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001283 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001284 snd_printdd(SFX "SCH snoop: %s\n",
1285 (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)
1286 ? "Disabled" : "Enabled");
Vinod Gda3fca22005-09-13 18:49:12 +02001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001291static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293/*
1294 * interrupt handler
1295 */
David Howells7d12e782006-10-05 14:55:46 +01001296static irqreturn_t azx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001298 struct azx *chip = dev_id;
1299 struct azx_dev *azx_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 u32 status;
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001301 u8 sd_status;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001302 int i, ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001304#ifdef CONFIG_PM_RUNTIME
1305 if (chip->pci->dev.power.runtime_status != RPM_ACTIVE)
1306 return IRQ_NONE;
1307#endif
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 spin_lock(&chip->reg_lock);
1310
Dan Carpenter60911062012-05-18 10:36:11 +03001311 if (chip->disabled) {
1312 spin_unlock(&chip->reg_lock);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001313 return IRQ_NONE;
Dan Carpenter60911062012-05-18 10:36:11 +03001314 }
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 status = azx_readl(chip, INTSTS);
1317 if (status == 0) {
1318 spin_unlock(&chip->reg_lock);
1319 return IRQ_NONE;
1320 }
1321
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001322 for (i = 0; i < chip->num_streams; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 azx_dev = &chip->azx_dev[i];
1324 if (status & azx_dev->sd_int_sta_mask) {
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001325 sd_status = azx_sd_readb(azx_dev, SD_STS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001327 if (!azx_dev->substream || !azx_dev->running ||
1328 !(sd_status & SD_INT_COMPLETE))
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001329 continue;
1330 /* check whether this IRQ is really acceptable */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001331 ok = azx_position_ok(chip, azx_dev);
1332 if (ok == 1) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001333 azx_dev->irq_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 spin_unlock(&chip->reg_lock);
1335 snd_pcm_period_elapsed(azx_dev->substream);
1336 spin_lock(&chip->reg_lock);
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001337 } else if (ok == 0 && chip->bus && chip->bus->workq) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001338 /* bogus IRQ, process it later */
1339 azx_dev->irq_pending = 1;
Takashi Iwai6acaed32009-01-12 10:09:24 +01001340 queue_work(chip->bus->workq,
1341 &chip->irq_pending_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 }
1344 }
1345
1346 /* clear rirb int */
1347 status = azx_readb(chip, RIRBSTS);
1348 if (status & RIRB_INT_MASK) {
Takashi Iwai14d34f12010-10-21 09:03:25 +02001349 if (status & RIRB_INT_RESPONSE) {
Takashi Iwai9477c582011-05-25 09:11:37 +02001350 if (chip->driver_caps & AZX_DCAPS_RIRB_PRE_DELAY)
Takashi Iwai14d34f12010-10-21 09:03:25 +02001351 udelay(80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 azx_update_rirb(chip);
Takashi Iwai14d34f12010-10-21 09:03:25 +02001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1355 }
1356
1357#if 0
1358 /* clear state status int */
1359 if (azx_readb(chip, STATESTS) & 0x04)
1360 azx_writeb(chip, STATESTS, 0x04);
1361#endif
1362 spin_unlock(&chip->reg_lock);
1363
1364 return IRQ_HANDLED;
1365}
1366
1367
1368/*
Takashi Iwai675f25d2008-06-10 17:53:20 +02001369 * set up a BDL entry
1370 */
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001371static int setup_bdle(struct azx *chip,
1372 struct snd_pcm_substream *substream,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001373 struct azx_dev *azx_dev, u32 **bdlp,
1374 int ofs, int size, int with_ioc)
1375{
Takashi Iwai675f25d2008-06-10 17:53:20 +02001376 u32 *bdl = *bdlp;
1377
1378 while (size > 0) {
1379 dma_addr_t addr;
1380 int chunk;
1381
1382 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
1383 return -EINVAL;
1384
Takashi Iwai77a23f22008-08-21 13:00:13 +02001385 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001386 /* program the address field of the BDL entry */
1387 bdl[0] = cpu_to_le32((u32)addr);
Takashi Iwai766979e2008-06-13 20:53:56 +02001388 bdl[1] = cpu_to_le32(upper_32_bits(addr));
Takashi Iwai675f25d2008-06-10 17:53:20 +02001389 /* program the size field of the BDL entry */
Takashi Iwaifc4abee2008-07-30 15:13:34 +02001390 chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001391 /* one BDLE cannot cross 4K boundary on CTHDA chips */
1392 if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY) {
1393 u32 remain = 0x1000 - (ofs & 0xfff);
1394 if (chunk > remain)
1395 chunk = remain;
1396 }
Takashi Iwai675f25d2008-06-10 17:53:20 +02001397 bdl[2] = cpu_to_le32(chunk);
1398 /* program the IOC to enable interrupt
1399 * only when the whole fragment is processed
1400 */
1401 size -= chunk;
1402 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
1403 bdl += 4;
1404 azx_dev->frags++;
1405 ofs += chunk;
1406 }
1407 *bdlp = bdl;
1408 return ofs;
1409}
1410
1411/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * set up BDL entries
1413 */
Takashi Iwai555e2192008-06-10 17:53:34 +02001414static int azx_setup_periods(struct azx *chip,
1415 struct snd_pcm_substream *substream,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001416 struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001418 u32 *bdl;
1419 int i, ofs, periods, period_bytes;
Takashi Iwai555e2192008-06-10 17:53:34 +02001420 int pos_adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /* reset BDL address */
1423 azx_sd_writel(azx_dev, SD_BDLPL, 0);
1424 azx_sd_writel(azx_dev, SD_BDLPU, 0);
1425
Takashi Iwai97b71c92009-03-18 15:09:13 +01001426 period_bytes = azx_dev->period_bytes;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001427 periods = azx_dev->bufsize / period_bytes;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* program the initial BDL entries */
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001430 bdl = (u32 *)azx_dev->bdl.area;
1431 ofs = 0;
1432 azx_dev->frags = 0;
Takashi Iwai555e2192008-06-10 17:53:34 +02001433 pos_adj = bdl_pos_adj[chip->dev_index];
Takashi Iwai915bf292012-09-11 15:19:10 +02001434 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
Takashi Iwai675f25d2008-06-10 17:53:20 +02001435 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001436 int pos_align = pos_adj;
Takashi Iwai555e2192008-06-10 17:53:34 +02001437 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001438 if (!pos_adj)
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001439 pos_adj = pos_align;
1440 else
1441 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
1442 pos_align;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001443 pos_adj = frames_to_bytes(runtime, pos_adj);
1444 if (pos_adj >= period_bytes) {
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02001445 snd_printk(KERN_WARNING SFX "Too big adjustment %d\n",
Takashi Iwai555e2192008-06-10 17:53:34 +02001446 bdl_pos_adj[chip->dev_index]);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001447 pos_adj = 0;
1448 } else {
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001449 ofs = setup_bdle(chip, substream, azx_dev,
Takashi Iwai915bf292012-09-11 15:19:10 +02001450 &bdl, ofs, pos_adj, true);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001451 if (ofs < 0)
1452 goto error;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001453 }
Takashi Iwai555e2192008-06-10 17:53:34 +02001454 } else
1455 pos_adj = 0;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001456 for (i = 0; i < periods; i++) {
1457 if (i == periods - 1 && pos_adj)
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001458 ofs = setup_bdle(chip, substream, azx_dev, &bdl, ofs,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001459 period_bytes - pos_adj, 0);
1460 else
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001461 ofs = setup_bdle(chip, substream, azx_dev, &bdl, ofs,
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001462 period_bytes,
Takashi Iwai915bf292012-09-11 15:19:10 +02001463 !azx_dev->no_period_wakeup);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001464 if (ofs < 0)
1465 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001467 return 0;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001468
1469 error:
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02001470 snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
Takashi Iwai675f25d2008-06-10 17:53:20 +02001471 azx_dev->bufsize, period_bytes);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001472 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Takashi Iwai1dddab42009-03-18 15:15:37 +01001475/* reset stream */
1476static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 unsigned char val;
1479 int timeout;
1480
Takashi Iwai1dddab42009-03-18 15:15:37 +01001481 azx_stream_clear(chip, azx_dev);
1482
Takashi Iwaid01ce992007-07-27 16:52:19 +02001483 azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
1484 SD_CTL_STREAM_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 udelay(3);
1486 timeout = 300;
1487 while (!((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
1488 --timeout)
1489 ;
1490 val &= ~SD_CTL_STREAM_RESET;
1491 azx_sd_writeb(azx_dev, SD_CTL, val);
1492 udelay(3);
1493
1494 timeout = 300;
1495 /* waiting for hardware to report that the stream is out of reset */
1496 while (((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
1497 --timeout)
1498 ;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001499
1500 /* reset first position - may not be synced with hw at this time */
1501 *azx_dev->posbuf = 0;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Takashi Iwai1dddab42009-03-18 15:15:37 +01001504/*
1505 * set up the SD for streaming
1506 */
1507static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
1508{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001509 unsigned int val;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001510 /* make sure the run bit is zero for SD */
1511 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /* program the stream_tag */
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001513 val = azx_sd_readl(azx_dev, SD_CTL);
1514 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
1515 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
1516 if (!azx_snoop(chip))
1517 val |= SD_CTL_TRAFFIC_PRIO;
1518 azx_sd_writel(azx_dev, SD_CTL, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /* program the length of samples in cyclic buffer */
1521 azx_sd_writel(azx_dev, SD_CBL, azx_dev->bufsize);
1522
1523 /* program the stream format */
1524 /* this value needs to be the same as the one programmed */
1525 azx_sd_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
1526
1527 /* program the stream LVI (last valid index) of the BDL */
1528 azx_sd_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
1529
1530 /* program the BDL address */
1531 /* lower BDL address */
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001532 azx_sd_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /* upper BDL address */
Takashi Iwai766979e2008-06-13 20:53:56 +02001534 azx_sd_writel(azx_dev, SD_BDLPU, upper_32_bits(azx_dev->bdl.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02001536 /* enable the position buffer */
David Henningsson4cb36312010-09-30 10:12:50 +02001537 if (chip->position_fix[0] != POS_FIX_LPIB ||
1538 chip->position_fix[1] != POS_FIX_LPIB) {
Takashi Iwaiee9d6b92008-03-14 15:52:20 +01001539 if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
1540 azx_writel(chip, DPLBASE,
1541 (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
1542 }
Takashi Iwaic74db862005-05-12 14:26:27 +02001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* set the interrupt enable bits in the descriptor control register */
Takashi Iwaid01ce992007-07-27 16:52:19 +02001545 azx_sd_writel(azx_dev, SD_CTL,
1546 azx_sd_readl(azx_dev, SD_CTL) | SD_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 return 0;
1549}
1550
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001551/*
1552 * Probe the given codec address
1553 */
1554static int probe_codec(struct azx *chip, int addr)
1555{
1556 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
1557 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
1558 unsigned int res;
1559
Wu Fengguanga678cde2009-08-01 18:46:46 +08001560 mutex_lock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001561 chip->probing = 1;
1562 azx_send_cmd(chip->bus, cmd);
Wu Fengguangdeadff12009-08-01 18:45:16 +08001563 res = azx_get_response(chip->bus, addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001564 chip->probing = 0;
Wu Fengguanga678cde2009-08-01 18:46:46 +08001565 mutex_unlock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001566 if (res == -1)
1567 return -EIO;
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02001568 snd_printdd(SFX "codec #%d probed OK\n", addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001569 return 0;
1570}
1571
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001572static int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
1573 struct hda_pcm *cpcm);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001574static void azx_stop_chip(struct azx *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Takashi Iwai8dd78332009-06-02 01:16:07 +02001576static void azx_bus_reset(struct hda_bus *bus)
1577{
1578 struct azx *chip = bus->private_data;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001579
1580 bus->in_reset = 1;
1581 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001582 azx_init_chip(chip, 1);
Alexander Beregalov65f75982009-06-04 13:46:16 +04001583#ifdef CONFIG_PM
Takashi Iwai8dd78332009-06-02 01:16:07 +02001584 if (chip->initialized) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01001585 struct azx_pcm *p;
1586 list_for_each_entry(p, &chip->pcm_list, list)
1587 snd_pcm_suspend_all(p->pcm);
Takashi Iwai8dd78332009-06-02 01:16:07 +02001588 snd_hda_suspend(chip->bus);
1589 snd_hda_resume(chip->bus);
1590 }
Alexander Beregalov65f75982009-06-04 13:46:16 +04001591#endif
Takashi Iwai8dd78332009-06-02 01:16:07 +02001592 bus->in_reset = 0;
1593}
1594
David Henningsson26a6cb62012-10-09 15:04:21 +02001595static int get_jackpoll_interval(struct azx *chip)
1596{
1597 int i = jackpoll_ms[chip->dev_index];
1598 unsigned int j;
1599 if (i == 0)
1600 return 0;
1601 if (i < 50 || i > 60000)
1602 j = 0;
1603 else
1604 j = msecs_to_jiffies(i);
1605 if (j == 0)
1606 snd_printk(KERN_WARNING SFX
1607 "jackpoll_ms value out of range: %d\n", i);
1608 return j;
1609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/*
1612 * Codec initialization
1613 */
1614
Takashi Iwai2f5983f2008-09-03 16:00:44 +02001615/* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001616static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] DELAYED_INITDATA_MARK = {
Wei Ni7445dfc2010-03-03 15:05:53 +08001617 [AZX_DRIVER_NVIDIA] = 8,
Kailang Yangf2690022008-05-27 11:44:55 +02001618 [AZX_DRIVER_TERA] = 1,
Takashi Iwaia9995a32007-03-12 21:30:46 +01001619};
1620
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001621static int DELAYED_INIT_MARK azx_codec_create(struct azx *chip, const char *model)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct hda_bus_template bus_temp;
Takashi Iwai34c25352008-10-28 11:38:58 +01001624 int c, codecs, err;
1625 int max_slots;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 memset(&bus_temp, 0, sizeof(bus_temp));
1628 bus_temp.private_data = chip;
1629 bus_temp.modelname = model;
1630 bus_temp.pci = chip->pci;
Takashi Iwai111d3af2006-02-16 18:17:58 +01001631 bus_temp.ops.command = azx_send_cmd;
1632 bus_temp.ops.get_response = azx_get_response;
Takashi Iwai176d5332008-07-30 15:01:44 +02001633 bus_temp.ops.attach_pcm = azx_attach_pcm_stream;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001634 bus_temp.ops.bus_reset = azx_bus_reset;
Takashi Iwai83012a72012-08-24 18:38:08 +02001635#ifdef CONFIG_PM
Takashi Iwai11cd41b2008-11-28 07:22:18 +01001636 bus_temp.power_save = &power_save;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001637 bus_temp.ops.pm_notify = azx_power_notify;
1638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Takashi Iwaid01ce992007-07-27 16:52:19 +02001640 err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus);
1641 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return err;
1643
Takashi Iwai9477c582011-05-25 09:11:37 +02001644 if (chip->driver_caps & AZX_DCAPS_RIRB_DELAY) {
1645 snd_printd(SFX "Enable delay in RIRB handling\n");
Wei Nidc9c8e22008-09-26 13:55:56 +08001646 chip->bus->needs_damn_long_delay = 1;
Takashi Iwai9477c582011-05-25 09:11:37 +02001647 }
Wei Nidc9c8e22008-09-26 13:55:56 +08001648
Takashi Iwai34c25352008-10-28 11:38:58 +01001649 codecs = 0;
Takashi Iwai2f5983f2008-09-03 16:00:44 +02001650 max_slots = azx_max_codecs[chip->driver_type];
1651 if (!max_slots)
Wei Ni7445dfc2010-03-03 15:05:53 +08001652 max_slots = AZX_DEFAULT_CODECS;
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001653
1654 /* First try to probe all given codec slots */
1655 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001656 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001657 if (probe_codec(chip, c) < 0) {
1658 /* Some BIOSen give you wrong codec addresses
1659 * that don't exist
1660 */
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02001661 snd_printk(KERN_WARNING SFX
1662 "Codec #%d probe error; "
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001663 "disabling it...\n", c);
1664 chip->codec_mask &= ~(1 << c);
1665 /* More badly, accessing to a non-existing
1666 * codec often screws up the controller chip,
Paul Menzel24481582010-02-08 20:37:26 +01001667 * and disturbs the further communications.
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001668 * Thus if an error occurs during probing,
1669 * better to reset the controller chip to
1670 * get back to the sanity state.
1671 */
1672 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001673 azx_init_chip(chip, 1);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001674 }
1675 }
1676 }
1677
Takashi Iwaid507cd62011-04-26 15:25:02 +02001678 /* AMD chipsets often cause the communication stalls upon certain
1679 * sequence like the pin-detection. It seems that forcing the synced
1680 * access works around the stall. Grrr...
1681 */
Takashi Iwai9477c582011-05-25 09:11:37 +02001682 if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) {
1683 snd_printd(SFX "Enable sync_write for stable communication\n");
Takashi Iwaid507cd62011-04-26 15:25:02 +02001684 chip->bus->sync_write = 1;
1685 chip->bus->allow_bus_reset = 1;
1686 }
1687
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001688 /* Then create codec instances */
Takashi Iwai34c25352008-10-28 11:38:58 +01001689 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001690 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwaibccad142007-04-24 12:23:53 +02001691 struct hda_codec *codec;
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001692 err = snd_hda_codec_new(chip->bus, c, &codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (err < 0)
1694 continue;
David Henningsson26a6cb62012-10-09 15:04:21 +02001695 codec->jackpoll_interval = get_jackpoll_interval(chip);
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01001696 codec->beep_mode = chip->beep_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 codecs++;
Takashi Iwai19a982b2007-03-21 15:14:35 +01001698 }
1699 }
1700 if (!codecs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 snd_printk(KERN_ERR SFX "no codecs initialized\n");
1702 return -ENXIO;
1703 }
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001704 return 0;
1705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001707/* configure each codec instance */
1708static int __devinit azx_codec_configure(struct azx *chip)
1709{
1710 struct hda_codec *codec;
1711 list_for_each_entry(codec, &chip->bus->codec_list, list) {
1712 snd_hda_codec_configure(codec);
1713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return 0;
1715}
1716
1717
1718/*
1719 * PCM support
1720 */
1721
1722/* assign a stream for the PCM */
Wu Fengguangef18bed2009-12-25 13:14:27 +08001723static inline struct azx_dev *
1724azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001726 int dev, i, nums;
Wu Fengguangef18bed2009-12-25 13:14:27 +08001727 struct azx_dev *res = NULL;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001728 /* make a non-zero unique key for the substream */
1729 int key = (substream->pcm->device << 16) | (substream->number << 2) |
1730 (substream->stream + 1);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001731
1732 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001733 dev = chip->playback_index_offset;
1734 nums = chip->playback_streams;
1735 } else {
1736 dev = chip->capture_index_offset;
1737 nums = chip->capture_streams;
1738 }
1739 for (i = 0; i < nums; i++, dev++)
Takashi Iwaid01ce992007-07-27 16:52:19 +02001740 if (!chip->azx_dev[dev].opened) {
Wu Fengguangef18bed2009-12-25 13:14:27 +08001741 res = &chip->azx_dev[dev];
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001742 if (res->assigned_key == key)
Wu Fengguangef18bed2009-12-25 13:14:27 +08001743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
Wu Fengguangef18bed2009-12-25 13:14:27 +08001745 if (res) {
1746 res->opened = 1;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001747 res->assigned_key = key;
Wu Fengguangef18bed2009-12-25 13:14:27 +08001748 }
1749 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
1752/* release the assigned stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001753static inline void azx_release_device(struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
1755 azx_dev->opened = 0;
1756}
1757
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001758static cycle_t azx_cc_read(const struct cyclecounter *cc)
1759{
1760 struct azx_dev *azx_dev = container_of(cc, struct azx_dev, azx_cc);
1761 struct snd_pcm_substream *substream = azx_dev->substream;
1762 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1763 struct azx *chip = apcm->chip;
1764
1765 return azx_readl(chip, WALLCLK);
1766}
1767
1768static void azx_timecounter_init(struct snd_pcm_substream *substream,
1769 bool force, cycle_t last)
1770{
1771 struct azx_dev *azx_dev = get_azx_dev(substream);
1772 struct timecounter *tc = &azx_dev->azx_tc;
1773 struct cyclecounter *cc = &azx_dev->azx_cc;
1774 u64 nsec;
1775
1776 cc->read = azx_cc_read;
1777 cc->mask = CLOCKSOURCE_MASK(32);
1778
1779 /*
1780 * Converting from 24 MHz to ns means applying a 125/3 factor.
1781 * To avoid any saturation issues in intermediate operations,
1782 * the 125 factor is applied first. The division is applied
1783 * last after reading the timecounter value.
1784 * Applying the 1/3 factor as part of the multiplication
1785 * requires at least 20 bits for a decent precision, however
1786 * overflows occur after about 4 hours or less, not a option.
1787 */
1788
1789 cc->mult = 125; /* saturation after 195 years */
1790 cc->shift = 0;
1791
1792 nsec = 0; /* audio time is elapsed time since trigger */
1793 timecounter_init(tc, cc, nsec);
1794 if (force)
1795 /*
1796 * force timecounter to use predefined value,
1797 * used for synchronized starts
1798 */
1799 tc->cycle_last = last;
1800}
1801
1802static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
1803 struct timespec *ts)
1804{
1805 struct azx_dev *azx_dev = get_azx_dev(substream);
1806 u64 nsec;
1807
1808 nsec = timecounter_read(&azx_dev->azx_tc);
1809 nsec = div_u64(nsec, 3); /* can be optimized */
1810
1811 *ts = ns_to_timespec(nsec);
1812
1813 return 0;
1814}
1815
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001816static struct snd_pcm_hardware azx_pcm_hw = {
Takashi Iwaid01ce992007-07-27 16:52:19 +02001817 .info = (SNDRV_PCM_INFO_MMAP |
1818 SNDRV_PCM_INFO_INTERLEAVED |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1820 SNDRV_PCM_INFO_MMAP_VALID |
Pavel Machek927fc862006-08-31 17:03:43 +02001821 /* No full-resume yet implemented */
1822 /* SNDRV_PCM_INFO_RESUME |*/
Takashi Iwai850f0e52008-03-18 17:11:05 +01001823 SNDRV_PCM_INFO_PAUSE |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001824 SNDRV_PCM_INFO_SYNC_START |
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001825 SNDRV_PCM_INFO_HAS_WALL_CLOCK |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001826 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1828 .rates = SNDRV_PCM_RATE_48000,
1829 .rate_min = 48000,
1830 .rate_max = 48000,
1831 .channels_min = 2,
1832 .channels_max = 2,
1833 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
1834 .period_bytes_min = 128,
1835 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
1836 .periods_min = 2,
1837 .periods_max = AZX_MAX_FRAG,
1838 .fifo_size = 0,
1839};
1840
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001841static int azx_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1844 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001845 struct azx *chip = apcm->chip;
1846 struct azx_dev *azx_dev;
1847 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 unsigned long flags;
1849 int err;
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001850 int buff_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Ingo Molnar62932df2006-01-16 16:34:20 +01001852 mutex_lock(&chip->open_mutex);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001853 azx_dev = azx_assign_device(chip, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (azx_dev == NULL) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001855 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return -EBUSY;
1857 }
1858 runtime->hw = azx_pcm_hw;
1859 runtime->hw.channels_min = hinfo->channels_min;
1860 runtime->hw.channels_max = hinfo->channels_max;
1861 runtime->hw.formats = hinfo->formats;
1862 runtime->hw.rates = hinfo->rates;
1863 snd_pcm_limit_hw_rates(runtime);
1864 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001865
1866 /* avoid wrap-around with wall-clock */
1867 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1868 20,
1869 178000000);
1870
Takashi Iwai52409aa2012-01-23 17:10:24 +01001871 if (chip->align_buffer_size)
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001872 /* constrain buffer sizes to be multiple of 128
1873 bytes. This is more efficient in terms of memory
1874 access but isn't required by the HDA spec and
1875 prevents users from specifying exact period/buffer
1876 sizes. For example for 44.1kHz, a period size set
1877 to 20ms will be rounded to 19.59ms. */
1878 buff_step = 128;
1879 else
1880 /* Don't enforce steps on buffer sizes, still need to
1881 be multiple of 4 bytes (HDA spec). Tested on Intel
1882 HDA controllers, may not work on all devices where
1883 option needs to be disabled */
1884 buff_step = 4;
1885
Joachim Deguara5f1545b2007-03-16 15:01:36 +01001886 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001887 buff_step);
Joachim Deguara5f1545b2007-03-16 15:01:36 +01001888 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001889 buff_step);
Dylan Reidb4a91cf2012-06-15 19:36:23 -07001890 snd_hda_power_up_d3wait(apcm->codec);
Takashi Iwaid01ce992007-07-27 16:52:19 +02001891 err = hinfo->ops.open(hinfo, apcm->codec, substream);
1892 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 azx_release_device(azx_dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001894 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01001895 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return err;
1897 }
Takashi Iwai70d321e2009-07-03 23:06:45 +02001898 snd_pcm_limit_hw_rates(runtime);
Takashi Iwaiaba66532009-07-05 11:44:46 +02001899 /* sanity check */
1900 if (snd_BUG_ON(!runtime->hw.channels_min) ||
1901 snd_BUG_ON(!runtime->hw.channels_max) ||
1902 snd_BUG_ON(!runtime->hw.formats) ||
1903 snd_BUG_ON(!runtime->hw.rates)) {
1904 azx_release_device(azx_dev);
1905 hinfo->ops.close(hinfo, apcm->codec, substream);
1906 snd_hda_power_down(apcm->codec);
1907 mutex_unlock(&chip->open_mutex);
1908 return -EINVAL;
1909 }
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001910
1911 /* disable WALLCLOCK timestamps for capture streams
1912 until we figure out how to handle digital inputs */
1913 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1914 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 spin_lock_irqsave(&chip->reg_lock, flags);
1917 azx_dev->substream = substream;
1918 azx_dev->running = 0;
1919 spin_unlock_irqrestore(&chip->reg_lock, flags);
1920
1921 runtime->private_data = azx_dev;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001922 snd_pcm_set_sync(substream);
Ingo Molnar62932df2006-01-16 16:34:20 +01001923 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001927static int azx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1930 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001931 struct azx *chip = apcm->chip;
1932 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 unsigned long flags;
1934
Ingo Molnar62932df2006-01-16 16:34:20 +01001935 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 spin_lock_irqsave(&chip->reg_lock, flags);
1937 azx_dev->substream = NULL;
1938 azx_dev->running = 0;
1939 spin_unlock_irqrestore(&chip->reg_lock, flags);
1940 azx_release_device(azx_dev);
1941 hinfo->ops.close(hinfo, apcm->codec, substream);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001942 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01001943 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return 0;
1945}
1946
Takashi Iwaid01ce992007-07-27 16:52:19 +02001947static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
1948 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001950 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1951 struct azx *chip = apcm->chip;
1952 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001953 struct azx_dev *azx_dev = get_azx_dev(substream);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001954 int ret;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001955
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001956 mark_runtime_wc(chip, azx_dev, runtime, false);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001957 azx_dev->bufsize = 0;
1958 azx_dev->period_bytes = 0;
1959 azx_dev->format_val = 0;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001960 ret = snd_pcm_lib_malloc_pages(substream,
Takashi Iwaid01ce992007-07-27 16:52:19 +02001961 params_buffer_bytes(hw_params));
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001962 if (ret < 0)
1963 return ret;
1964 mark_runtime_wc(chip, azx_dev, runtime, true);
1965 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001968static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001971 struct azx_dev *azx_dev = get_azx_dev(substream);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001972 struct azx *chip = apcm->chip;
1973 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
1975
1976 /* reset BDL address */
1977 azx_sd_writel(azx_dev, SD_BDLPL, 0);
1978 azx_sd_writel(azx_dev, SD_BDLPU, 0);
1979 azx_sd_writel(azx_dev, SD_CTL, 0);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001980 azx_dev->bufsize = 0;
1981 azx_dev->period_bytes = 0;
1982 azx_dev->format_val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Takashi Iwaieb541332010-08-06 13:48:11 +02001984 snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001986 mark_runtime_wc(chip, azx_dev, runtime, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return snd_pcm_lib_free_pages(substream);
1988}
1989
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001990static int azx_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001993 struct azx *chip = apcm->chip;
1994 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001996 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001997 unsigned int bufsize, period_bytes, format_val, stream_tag;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001998 int err;
Stephen Warren7c9359762011-06-01 11:14:17 -06001999 struct hda_spdif_out *spdif =
2000 snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid);
2001 unsigned short ctls = spdif ? spdif->ctls : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002003 azx_stream_reset(chip, azx_dev);
Takashi Iwai97b71c92009-03-18 15:09:13 +01002004 format_val = snd_hda_calc_stream_format(runtime->rate,
2005 runtime->channels,
2006 runtime->format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03002007 hinfo->maxbps,
Stephen Warren7c9359762011-06-01 11:14:17 -06002008 ctls);
Takashi Iwai97b71c92009-03-18 15:09:13 +01002009 if (!format_val) {
Takashi Iwaid01ce992007-07-27 16:52:19 +02002010 snd_printk(KERN_ERR SFX
2011 "invalid format_val, rate=%d, ch=%d, format=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 runtime->rate, runtime->channels, runtime->format);
2013 return -EINVAL;
2014 }
2015
Takashi Iwai97b71c92009-03-18 15:09:13 +01002016 bufsize = snd_pcm_lib_buffer_bytes(substream);
2017 period_bytes = snd_pcm_lib_period_bytes(substream);
2018
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02002019 snd_printdd(SFX "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
Takashi Iwai97b71c92009-03-18 15:09:13 +01002020 bufsize, format_val);
2021
2022 if (bufsize != azx_dev->bufsize ||
2023 period_bytes != azx_dev->period_bytes ||
Takashi Iwai915bf292012-09-11 15:19:10 +02002024 format_val != azx_dev->format_val ||
2025 runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
Takashi Iwai97b71c92009-03-18 15:09:13 +01002026 azx_dev->bufsize = bufsize;
2027 azx_dev->period_bytes = period_bytes;
2028 azx_dev->format_val = format_val;
Takashi Iwai915bf292012-09-11 15:19:10 +02002029 azx_dev->no_period_wakeup = runtime->no_period_wakeup;
Takashi Iwai97b71c92009-03-18 15:09:13 +01002030 err = azx_setup_periods(chip, substream, azx_dev);
2031 if (err < 0)
2032 return err;
2033 }
2034
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002035 /* wallclk has 24Mhz clock source */
2036 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
2037 runtime->rate) * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 azx_setup_controller(chip, azx_dev);
2039 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2040 azx_dev->fifo_size = azx_sd_readw(azx_dev, SD_FIFOSIZE) + 1;
2041 else
2042 azx_dev->fifo_size = 0;
2043
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02002044 stream_tag = azx_dev->stream_tag;
2045 /* CA-IBG chips need the playback stream starting from 1 */
Takashi Iwai9477c582011-05-25 09:11:37 +02002046 if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02002047 stream_tag > chip->capture_streams)
2048 stream_tag -= chip->capture_streams;
2049 return snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
Takashi Iwaieb541332010-08-06 13:48:11 +02002050 azx_dev->format_val, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002053static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002056 struct azx *chip = apcm->chip;
Takashi Iwai850f0e52008-03-18 17:11:05 +01002057 struct azx_dev *azx_dev;
2058 struct snd_pcm_substream *s;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002059 int rstart = 0, start, nsync = 0, sbits = 0;
Takashi Iwai850f0e52008-03-18 17:11:05 +01002060 int nwait, timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 switch (cmd) {
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002063 case SNDRV_PCM_TRIGGER_START:
2064 rstart = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2066 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai850f0e52008-03-18 17:11:05 +01002067 start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 break;
2069 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jaroslav Kysela47123192005-08-15 20:53:07 +02002070 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai850f0e52008-03-18 17:11:05 +01002072 start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 break;
2074 default:
Takashi Iwai850f0e52008-03-18 17:11:05 +01002075 return -EINVAL;
2076 }
2077
2078 snd_pcm_group_for_each_entry(s, substream) {
2079 if (s->pcm->card != substream->pcm->card)
2080 continue;
2081 azx_dev = get_azx_dev(s);
2082 sbits |= 1 << azx_dev->index;
2083 nsync++;
2084 snd_pcm_trigger_done(s, substream);
2085 }
2086
2087 spin_lock(&chip->reg_lock);
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05002088
2089 /* first, set SYNC bits of corresponding streams */
2090 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
2091 azx_writel(chip, OLD_SSYNC,
2092 azx_readl(chip, OLD_SSYNC) | sbits);
2093 else
2094 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) | sbits);
2095
Takashi Iwai850f0e52008-03-18 17:11:05 +01002096 snd_pcm_group_for_each_entry(s, substream) {
2097 if (s->pcm->card != substream->pcm->card)
2098 continue;
2099 azx_dev = get_azx_dev(s);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002100 if (start) {
2101 azx_dev->start_wallclk = azx_readl(chip, WALLCLK);
2102 if (!rstart)
2103 azx_dev->start_wallclk -=
2104 azx_dev->period_wallclk;
Takashi Iwai850f0e52008-03-18 17:11:05 +01002105 azx_stream_start(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002106 } else {
Takashi Iwai850f0e52008-03-18 17:11:05 +01002107 azx_stream_stop(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002108 }
Takashi Iwai850f0e52008-03-18 17:11:05 +01002109 azx_dev->running = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01002112 if (start) {
Takashi Iwai850f0e52008-03-18 17:11:05 +01002113 /* wait until all FIFOs get ready */
2114 for (timeout = 5000; timeout; timeout--) {
2115 nwait = 0;
2116 snd_pcm_group_for_each_entry(s, substream) {
2117 if (s->pcm->card != substream->pcm->card)
2118 continue;
2119 azx_dev = get_azx_dev(s);
2120 if (!(azx_sd_readb(azx_dev, SD_STS) &
2121 SD_STS_FIFO_READY))
2122 nwait++;
2123 }
2124 if (!nwait)
2125 break;
2126 cpu_relax();
2127 }
2128 } else {
2129 /* wait until all RUN bits are cleared */
2130 for (timeout = 5000; timeout; timeout--) {
2131 nwait = 0;
2132 snd_pcm_group_for_each_entry(s, substream) {
2133 if (s->pcm->card != substream->pcm->card)
2134 continue;
2135 azx_dev = get_azx_dev(s);
2136 if (azx_sd_readb(azx_dev, SD_CTL) &
2137 SD_CTL_DMA_START)
2138 nwait++;
2139 }
2140 if (!nwait)
2141 break;
2142 cpu_relax();
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05002145 spin_lock(&chip->reg_lock);
2146 /* reset SYNC bits */
2147 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
2148 azx_writel(chip, OLD_SSYNC,
2149 azx_readl(chip, OLD_SSYNC) & ~sbits);
2150 else
2151 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) & ~sbits);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05002152 if (start) {
2153 azx_timecounter_init(substream, 0, 0);
2154 if (nsync > 1) {
2155 cycle_t cycle_last;
2156
2157 /* same start cycle for master and group */
2158 azx_dev = get_azx_dev(substream);
2159 cycle_last = azx_dev->azx_tc.cycle_last;
2160
2161 snd_pcm_group_for_each_entry(s, substream) {
2162 if (s->pcm->card != substream->pcm->card)
2163 continue;
2164 azx_timecounter_init(s, 1, cycle_last);
2165 }
2166 }
2167 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05002168 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01002169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Joseph Chan0e153472008-08-26 14:38:03 +02002172/* get the current DMA position with correction on VIA chips */
2173static unsigned int azx_via_get_position(struct azx *chip,
2174 struct azx_dev *azx_dev)
2175{
2176 unsigned int link_pos, mini_pos, bound_pos;
2177 unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
2178 unsigned int fifo_size;
2179
2180 link_pos = azx_sd_readl(azx_dev, SD_LPIB);
Takashi Iwaib4a655e2011-06-07 12:26:56 +02002181 if (azx_dev->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Joseph Chan0e153472008-08-26 14:38:03 +02002182 /* Playback, no problem using link position */
2183 return link_pos;
2184 }
2185
2186 /* Capture */
2187 /* For new chipset,
2188 * use mod to get the DMA position just like old chipset
2189 */
2190 mod_dma_pos = le32_to_cpu(*azx_dev->posbuf);
2191 mod_dma_pos %= azx_dev->period_bytes;
2192
2193 /* azx_dev->fifo_size can't get FIFO size of in stream.
2194 * Get from base address + offset.
2195 */
2196 fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
2197
2198 if (azx_dev->insufficient) {
2199 /* Link position never gather than FIFO size */
2200 if (link_pos <= fifo_size)
2201 return 0;
2202
2203 azx_dev->insufficient = 0;
2204 }
2205
2206 if (link_pos <= fifo_size)
2207 mini_pos = azx_dev->bufsize + link_pos - fifo_size;
2208 else
2209 mini_pos = link_pos - fifo_size;
2210
2211 /* Find nearest previous boudary */
2212 mod_mini_pos = mini_pos % azx_dev->period_bytes;
2213 mod_link_pos = link_pos % azx_dev->period_bytes;
2214 if (mod_link_pos >= fifo_size)
2215 bound_pos = link_pos - mod_link_pos;
2216 else if (mod_dma_pos >= mod_mini_pos)
2217 bound_pos = mini_pos - mod_mini_pos;
2218 else {
2219 bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes;
2220 if (bound_pos >= azx_dev->bufsize)
2221 bound_pos = 0;
2222 }
2223
2224 /* Calculate real DMA position we want */
2225 return bound_pos + mod_dma_pos;
2226}
2227
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002228static unsigned int azx_get_position(struct azx *chip,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002229 struct azx_dev *azx_dev,
2230 bool with_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 unsigned int pos;
David Henningsson4cb36312010-09-30 10:12:50 +02002233 int stream = azx_dev->substream->stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
David Henningsson4cb36312010-09-30 10:12:50 +02002235 switch (chip->position_fix[stream]) {
2236 case POS_FIX_LPIB:
2237 /* read LPIB */
2238 pos = azx_sd_readl(azx_dev, SD_LPIB);
2239 break;
2240 case POS_FIX_VIACOMBO:
Joseph Chan0e153472008-08-26 14:38:03 +02002241 pos = azx_via_get_position(chip, azx_dev);
David Henningsson4cb36312010-09-30 10:12:50 +02002242 break;
2243 default:
2244 /* use the position buffer */
2245 pos = le32_to_cpu(*azx_dev->posbuf);
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002246 if (with_check && chip->position_fix[stream] == POS_FIX_AUTO) {
Takashi Iwaia8103642011-06-07 12:23:23 +02002247 if (!pos || pos == (u32)-1) {
2248 printk(KERN_WARNING
2249 "hda-intel: Invalid position buffer, "
2250 "using LPIB read method instead.\n");
2251 chip->position_fix[stream] = POS_FIX_LPIB;
2252 pos = azx_sd_readl(azx_dev, SD_LPIB);
2253 } else
2254 chip->position_fix[stream] = POS_FIX_POSBUF;
2255 }
2256 break;
Takashi Iwaic74db862005-05-12 14:26:27 +02002257 }
David Henningsson4cb36312010-09-30 10:12:50 +02002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (pos >= azx_dev->bufsize)
2260 pos = 0;
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002261
2262 /* calculate runtime delay from LPIB */
2263 if (azx_dev->substream->runtime &&
2264 chip->position_fix[stream] == POS_FIX_POSBUF &&
2265 (chip->driver_caps & AZX_DCAPS_COUNT_LPIB_DELAY)) {
2266 unsigned int lpib_pos = azx_sd_readl(azx_dev, SD_LPIB);
2267 int delay;
2268 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2269 delay = pos - lpib_pos;
2270 else
2271 delay = lpib_pos - pos;
2272 if (delay < 0)
2273 delay += azx_dev->bufsize;
2274 if (delay >= azx_dev->period_bytes) {
2275 snd_printdd("delay %d > period_bytes %d\n",
2276 delay, azx_dev->period_bytes);
2277 delay = 0; /* something is wrong */
2278 }
2279 azx_dev->substream->runtime->delay =
2280 bytes_to_frames(azx_dev->substream->runtime, delay);
2281 }
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002282 return pos;
2283}
2284
2285static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
2286{
2287 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2288 struct azx *chip = apcm->chip;
2289 struct azx_dev *azx_dev = get_azx_dev(substream);
2290 return bytes_to_frames(substream->runtime,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002291 azx_get_position(chip, azx_dev, false));
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002292}
2293
2294/*
2295 * Check whether the current DMA position is acceptable for updating
2296 * periods. Returns non-zero if it's OK.
2297 *
2298 * Many HD-audio controllers appear pretty inaccurate about
2299 * the update-IRQ timing. The IRQ is issued before actually the
2300 * data is processed. So, we need to process it afterwords in a
2301 * workqueue.
2302 */
2303static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
2304{
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002305 u32 wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002306 unsigned int pos;
2307
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002308 wallclk = azx_readl(chip, WALLCLK) - azx_dev->start_wallclk;
2309 if (wallclk < (azx_dev->period_wallclk * 2) / 3)
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002310 return -1; /* bogus (too early) interrupt */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002311
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002312 pos = azx_get_position(chip, azx_dev, true);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002313
Takashi Iwaid6d8bf52010-02-12 18:17:06 +01002314 if (WARN_ONCE(!azx_dev->period_bytes,
2315 "hda-intel: zero azx_dev->period_bytes"))
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002316 return -1; /* this shouldn't happen! */
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002317 if (wallclk < (azx_dev->period_wallclk * 5) / 4 &&
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002318 pos % azx_dev->period_bytes > azx_dev->period_bytes / 2)
2319 /* NG - it's below the first next period boundary */
2320 return bdl_pos_adj[chip->dev_index] ? 0 : -1;
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002321 azx_dev->start_wallclk += wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002322 return 1; /* OK, it's fine */
2323}
2324
2325/*
2326 * The work for pending PCM period updates.
2327 */
2328static void azx_irq_pending_work(struct work_struct *work)
2329{
2330 struct azx *chip = container_of(work, struct azx, irq_pending_work);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002331 int i, pending, ok;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002332
Takashi Iwaia6a950a2008-06-10 17:53:35 +02002333 if (!chip->irq_pending_warned) {
2334 printk(KERN_WARNING
2335 "hda-intel: IRQ timing workaround is activated "
2336 "for card #%d. Suggest a bigger bdl_pos_adj.\n",
2337 chip->card->number);
2338 chip->irq_pending_warned = 1;
2339 }
2340
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002341 for (;;) {
2342 pending = 0;
2343 spin_lock_irq(&chip->reg_lock);
2344 for (i = 0; i < chip->num_streams; i++) {
2345 struct azx_dev *azx_dev = &chip->azx_dev[i];
2346 if (!azx_dev->irq_pending ||
2347 !azx_dev->substream ||
2348 !azx_dev->running)
2349 continue;
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002350 ok = azx_position_ok(chip, azx_dev);
2351 if (ok > 0) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002352 azx_dev->irq_pending = 0;
2353 spin_unlock(&chip->reg_lock);
2354 snd_pcm_period_elapsed(azx_dev->substream);
2355 spin_lock(&chip->reg_lock);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002356 } else if (ok < 0) {
2357 pending = 0; /* too early */
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002358 } else
2359 pending++;
2360 }
2361 spin_unlock_irq(&chip->reg_lock);
2362 if (!pending)
2363 return;
Takashi Iwai08af4952010-08-03 14:39:04 +02002364 msleep(1);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002365 }
2366}
2367
2368/* clear irq_pending flags and assure no on-going workq */
2369static void azx_clear_irq_pending(struct azx *chip)
2370{
2371 int i;
2372
2373 spin_lock_irq(&chip->reg_lock);
2374 for (i = 0; i < chip->num_streams; i++)
2375 chip->azx_dev[i].irq_pending = 0;
2376 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002379#ifdef CONFIG_X86
2380static int azx_pcm_mmap(struct snd_pcm_substream *substream,
2381 struct vm_area_struct *area)
2382{
2383 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2384 struct azx *chip = apcm->chip;
2385 if (!azx_snoop(chip))
2386 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
2387 return snd_pcm_lib_default_mmap(substream, area);
2388}
2389#else
2390#define azx_pcm_mmap NULL
2391#endif
2392
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002393static struct snd_pcm_ops azx_pcm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 .open = azx_pcm_open,
2395 .close = azx_pcm_close,
2396 .ioctl = snd_pcm_lib_ioctl,
2397 .hw_params = azx_pcm_hw_params,
2398 .hw_free = azx_pcm_hw_free,
2399 .prepare = azx_pcm_prepare,
2400 .trigger = azx_pcm_trigger,
2401 .pointer = azx_pcm_pointer,
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05002402 .wall_clock = azx_get_wallclock_tstamp,
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002403 .mmap = azx_pcm_mmap,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002404 .page = snd_pcm_sgbuf_ops_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405};
2406
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002407static void azx_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
Takashi Iwai176d5332008-07-30 15:01:44 +02002409 struct azx_pcm *apcm = pcm->private_data;
2410 if (apcm) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002411 list_del(&apcm->list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002412 kfree(apcm);
2413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002416#define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
2417
Takashi Iwai176d5332008-07-30 15:01:44 +02002418static int
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002419azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
2420 struct hda_pcm *cpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002422 struct azx *chip = bus->private_data;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002423 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 struct azx_pcm *apcm;
Takashi Iwai176d5332008-07-30 15:01:44 +02002425 int pcm_dev = cpcm->device;
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002426 unsigned int size;
Takashi Iwai176d5332008-07-30 15:01:44 +02002427 int s, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002429 list_for_each_entry(apcm, &chip->pcm_list, list) {
2430 if (apcm->pcm->device == pcm_dev) {
2431 snd_printk(KERN_ERR SFX "PCM %d already exists\n", pcm_dev);
2432 return -EBUSY;
2433 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002434 }
2435 err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
2436 cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
2437 cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 &pcm);
2439 if (err < 0)
2440 return err;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002441 strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
Takashi Iwai176d5332008-07-30 15:01:44 +02002442 apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (apcm == NULL)
2444 return -ENOMEM;
2445 apcm->chip = chip;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002446 apcm->pcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 apcm->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 pcm->private_data = apcm;
2449 pcm->private_free = azx_pcm_free;
Takashi Iwai176d5332008-07-30 15:01:44 +02002450 if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
2451 pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002452 list_add_tail(&apcm->list, &chip->pcm_list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002453 cpcm->pcm = pcm;
2454 for (s = 0; s < 2; s++) {
2455 apcm->hinfo[s] = &cpcm->stream[s];
2456 if (cpcm->stream[s].substreams)
2457 snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
2458 }
2459 /* buffer pre-allocation */
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002460 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
2461 if (size > MAX_PREALLOC_SIZE)
2462 size = MAX_PREALLOC_SIZE;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002463 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 snd_dma_pci_data(chip->pci),
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002465 size, MAX_PREALLOC_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return 0;
2467}
2468
2469/*
2470 * mixer creation - all stuff is implemented in hda module
2471 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002472static int __devinit azx_mixer_create(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 return snd_hda_build_controls(chip->bus);
2475}
2476
2477
2478/*
2479 * initialize SD streams
2480 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002481static int __devinit azx_init_stream(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 int i;
2484
2485 /* initialize each stream (aka device)
Takashi Iwaid01ce992007-07-27 16:52:19 +02002486 * assign the starting bdl address to each stream (device)
2487 * and initialize
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 */
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002489 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002490 struct azx_dev *azx_dev = &chip->azx_dev[i];
Takashi Iwai929861c2006-08-31 16:55:40 +02002491 azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
2493 azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
2494 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
2495 azx_dev->sd_int_sta_mask = 1 << i;
2496 /* stream tag: must be non-zero and unique */
2497 azx_dev->index = i;
2498 azx_dev->stream_tag = i + 1;
2499 }
2500
2501 return 0;
2502}
2503
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002504static int azx_acquire_irq(struct azx *chip, int do_disconnect)
2505{
Takashi Iwai437a5a42006-11-21 12:14:23 +01002506 if (request_irq(chip->pci->irq, azx_interrupt,
2507 chip->msi ? 0 : IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002508 KBUILD_MODNAME, chip)) {
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002509 printk(KERN_ERR "hda-intel: unable to grab IRQ %d, "
2510 "disabling device\n", chip->pci->irq);
2511 if (do_disconnect)
2512 snd_card_disconnect(chip->card);
2513 return -1;
2514 }
2515 chip->irq = chip->pci->irq;
Takashi Iwai69e13412006-11-21 12:10:55 +01002516 pci_intx(chip->pci, !chip->msi);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002517 return 0;
2518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Takashi Iwaicb53c622007-08-10 17:21:45 +02002521static void azx_stop_chip(struct azx *chip)
2522{
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002523 if (!chip->initialized)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002524 return;
2525
2526 /* disable interrupts */
2527 azx_int_disable(chip);
2528 azx_int_clear(chip);
2529
2530 /* disable CORB/RIRB */
2531 azx_free_cmd_io(chip);
2532
2533 /* disable position buffer */
2534 azx_writel(chip, DPLBASE, 0);
2535 azx_writel(chip, DPUBASE, 0);
2536
2537 chip->initialized = 0;
2538}
2539
Takashi Iwai83012a72012-08-24 18:38:08 +02002540#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02002541/* power-up/down the controller */
Takashi Iwai68467f52012-08-28 09:14:29 -07002542static void azx_power_notify(struct hda_bus *bus, bool power_up)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002543{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002544 struct azx *chip = bus->private_data;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002545
Takashi Iwai68467f52012-08-28 09:14:29 -07002546 if (power_up)
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002547 pm_runtime_get_sync(&chip->pci->dev);
2548 else
2549 pm_runtime_put_sync(&chip->pci->dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002550}
Takashi Iwai65fcd412012-08-14 17:13:32 +02002551
2552static DEFINE_MUTEX(card_list_lock);
2553static LIST_HEAD(card_list);
2554
2555static void azx_add_card_list(struct azx *chip)
2556{
2557 mutex_lock(&card_list_lock);
2558 list_add(&chip->list, &card_list);
2559 mutex_unlock(&card_list_lock);
2560}
2561
2562static void azx_del_card_list(struct azx *chip)
2563{
2564 mutex_lock(&card_list_lock);
2565 list_del_init(&chip->list);
2566 mutex_unlock(&card_list_lock);
2567}
2568
2569/* trigger power-save check at writing parameter */
2570static int param_set_xint(const char *val, const struct kernel_param *kp)
2571{
2572 struct azx *chip;
2573 struct hda_codec *c;
2574 int prev = power_save;
2575 int ret = param_set_int(val, kp);
2576
2577 if (ret || prev == power_save)
2578 return ret;
2579
2580 mutex_lock(&card_list_lock);
2581 list_for_each_entry(chip, &card_list, list) {
2582 if (!chip->bus || chip->disabled)
2583 continue;
2584 list_for_each_entry(c, &chip->bus->codec_list, list)
2585 snd_hda_power_sync(c);
2586 }
2587 mutex_unlock(&card_list_lock);
2588 return 0;
2589}
2590#else
2591#define azx_add_card_list(chip) /* NOP */
2592#define azx_del_card_list(chip) /* NOP */
Takashi Iwai83012a72012-08-24 18:38:08 +02002593#endif /* CONFIG_PM */
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002594
Takashi Iwai7ccbde52012-08-14 18:10:09 +02002595#if defined(CONFIG_PM_SLEEP) || defined(SUPPORT_VGA_SWITCHEROO)
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002596/*
2597 * power management
2598 */
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002599static int azx_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002601 struct pci_dev *pci = to_pci_dev(dev);
2602 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002603 struct azx *chip = card->private_data;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002604 struct azx_pcm *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Takashi Iwai421a1252005-11-17 16:11:09 +01002606 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002607 azx_clear_irq_pending(chip);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002608 list_for_each_entry(p, &chip->pcm_list, list)
2609 snd_pcm_suspend_all(p->pcm);
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02002610 if (chip->initialized)
Takashi Iwai8dd78332009-06-02 01:16:07 +02002611 snd_hda_suspend(chip->bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002612 azx_stop_chip(chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002613 if (chip->irq >= 0) {
Takashi Iwai43001c92006-09-08 12:30:03 +02002614 free_irq(chip->irq, chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002615 chip->irq = -1;
2616 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002617 if (chip->msi)
Takashi Iwai43001c92006-09-08 12:30:03 +02002618 pci_disable_msi(chip->pci);
Takashi Iwai421a1252005-11-17 16:11:09 +01002619 pci_disable_device(pci);
2620 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002621 pci_set_power_state(pci, PCI_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return 0;
2623}
2624
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002625static int azx_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002627 struct pci_dev *pci = to_pci_dev(dev);
2628 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002629 struct azx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Takashi Iwaid14a7e02009-02-16 10:13:03 +01002631 pci_set_power_state(pci, PCI_D0);
2632 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002633 if (pci_enable_device(pci) < 0) {
2634 printk(KERN_ERR "hda-intel: pci_enable_device failed, "
2635 "disabling device\n");
2636 snd_card_disconnect(card);
2637 return -EIO;
2638 }
2639 pci_set_master(pci);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002640 if (chip->msi)
2641 if (pci_enable_msi(pci) < 0)
2642 chip->msi = 0;
2643 if (azx_acquire_irq(chip, 1) < 0)
Takashi Iwai30b35392006-10-11 18:52:53 +02002644 return -EIO;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002645 azx_init_pci(chip);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002646
Takashi Iwai7f308302012-05-08 16:52:23 +02002647 azx_init_chip(chip, 1);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 snd_hda_resume(chip->bus);
Takashi Iwai421a1252005-11-17 16:11:09 +01002650 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return 0;
2652}
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002653#endif /* CONFIG_PM_SLEEP || SUPPORT_VGA_SWITCHEROO */
2654
2655#ifdef CONFIG_PM_RUNTIME
2656static int azx_runtime_suspend(struct device *dev)
2657{
2658 struct snd_card *card = dev_get_drvdata(dev);
2659 struct azx *chip = card->private_data;
2660
2661 if (!power_save_controller)
2662 return -EAGAIN;
2663
2664 azx_stop_chip(chip);
2665 azx_clear_irq_pending(chip);
2666 return 0;
2667}
2668
2669static int azx_runtime_resume(struct device *dev)
2670{
2671 struct snd_card *card = dev_get_drvdata(dev);
2672 struct azx *chip = card->private_data;
2673
2674 azx_init_pci(chip);
2675 azx_init_chip(chip, 1);
2676 return 0;
2677}
2678#endif /* CONFIG_PM_RUNTIME */
2679
2680#ifdef CONFIG_PM
2681static const struct dev_pm_ops azx_pm = {
2682 SET_SYSTEM_SLEEP_PM_OPS(azx_suspend, azx_resume)
2683 SET_RUNTIME_PM_OPS(azx_runtime_suspend, azx_runtime_resume, NULL)
2684};
2685
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002686#define AZX_PM_OPS &azx_pm
2687#else
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002688#define AZX_PM_OPS NULL
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002689#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691
2692/*
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002693 * reboot notifier for hang-up problem at power-down
2694 */
2695static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
2696{
2697 struct azx *chip = container_of(nb, struct azx, reboot_notifier);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01002698 snd_hda_bus_reboot_notify(chip->bus);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002699 azx_stop_chip(chip);
2700 return NOTIFY_OK;
2701}
2702
2703static void azx_notifier_register(struct azx *chip)
2704{
2705 chip->reboot_notifier.notifier_call = azx_halt;
2706 register_reboot_notifier(&chip->reboot_notifier);
2707}
2708
2709static void azx_notifier_unregister(struct azx *chip)
2710{
2711 if (chip->reboot_notifier.notifier_call)
2712 unregister_reboot_notifier(&chip->reboot_notifier);
2713}
2714
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002715static int DELAYED_INIT_MARK azx_first_init(struct azx *chip);
2716static int DELAYED_INIT_MARK azx_probe_continue(struct azx *chip);
2717
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002718#ifdef SUPPORT_VGA_SWITCHEROO
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002719static struct pci_dev __devinit *get_bound_vga(struct pci_dev *pci);
2720
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002721static void azx_vs_set_state(struct pci_dev *pci,
2722 enum vga_switcheroo_state state)
2723{
2724 struct snd_card *card = pci_get_drvdata(pci);
2725 struct azx *chip = card->private_data;
2726 bool disabled;
2727
2728 if (chip->init_failed)
2729 return;
2730
2731 disabled = (state == VGA_SWITCHEROO_OFF);
2732 if (chip->disabled == disabled)
2733 return;
2734
2735 if (!chip->bus) {
2736 chip->disabled = disabled;
2737 if (!disabled) {
2738 snd_printk(KERN_INFO SFX
2739 "%s: Start delayed initialization\n",
2740 pci_name(chip->pci));
2741 if (azx_first_init(chip) < 0 ||
2742 azx_probe_continue(chip) < 0) {
2743 snd_printk(KERN_ERR SFX
2744 "%s: initialization error\n",
2745 pci_name(chip->pci));
2746 chip->init_failed = true;
2747 }
2748 }
2749 } else {
2750 snd_printk(KERN_INFO SFX
2751 "%s %s via VGA-switcheroo\n",
2752 disabled ? "Disabling" : "Enabling",
2753 pci_name(chip->pci));
2754 if (disabled) {
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002755 azx_suspend(&pci->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002756 chip->disabled = true;
Takashi Iwai128960a2012-10-12 17:28:18 +02002757 if (snd_hda_lock_devices(chip->bus))
2758 snd_printk(KERN_WARNING SFX
2759 "Cannot lock devices!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002760 } else {
2761 snd_hda_unlock_devices(chip->bus);
2762 chip->disabled = false;
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002763 azx_resume(&pci->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002764 }
2765 }
2766}
2767
2768static bool azx_vs_can_switch(struct pci_dev *pci)
2769{
2770 struct snd_card *card = pci_get_drvdata(pci);
2771 struct azx *chip = card->private_data;
2772
2773 if (chip->init_failed)
2774 return false;
2775 if (chip->disabled || !chip->bus)
2776 return true;
2777 if (snd_hda_lock_devices(chip->bus))
2778 return false;
2779 snd_hda_unlock_devices(chip->bus);
2780 return true;
2781}
2782
2783static void __devinit init_vga_switcheroo(struct azx *chip)
2784{
2785 struct pci_dev *p = get_bound_vga(chip->pci);
2786 if (p) {
2787 snd_printk(KERN_INFO SFX
2788 "%s: Handle VGA-switcheroo audio client\n",
2789 pci_name(chip->pci));
2790 chip->use_vga_switcheroo = 1;
2791 pci_dev_put(p);
2792 }
2793}
2794
2795static const struct vga_switcheroo_client_ops azx_vs_ops = {
2796 .set_gpu_state = azx_vs_set_state,
2797 .can_switch = azx_vs_can_switch,
2798};
2799
2800static int __devinit register_vga_switcheroo(struct azx *chip)
2801{
Takashi Iwai128960a2012-10-12 17:28:18 +02002802 int err;
2803
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002804 if (!chip->use_vga_switcheroo)
2805 return 0;
2806 /* FIXME: currently only handling DIS controller
2807 * is there any machine with two switchable HDMI audio controllers?
2808 */
Takashi Iwai128960a2012-10-12 17:28:18 +02002809 err = vga_switcheroo_register_audio_client(chip->pci, &azx_vs_ops,
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002810 VGA_SWITCHEROO_DIS,
2811 chip->bus != NULL);
Takashi Iwai128960a2012-10-12 17:28:18 +02002812 if (err < 0)
2813 return err;
2814 chip->vga_switcheroo_registered = 1;
2815 return 0;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002816}
2817#else
2818#define init_vga_switcheroo(chip) /* NOP */
2819#define register_vga_switcheroo(chip) 0
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002820#define check_hdmi_disabled(pci) false
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002821#endif /* SUPPORT_VGA_SWITCHER */
2822
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002823/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 * destructor
2825 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002826static int azx_free(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002828 int i;
2829
Takashi Iwai65fcd412012-08-14 17:13:32 +02002830 azx_del_card_list(chip);
2831
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002832 azx_notifier_unregister(chip);
2833
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002834 if (use_vga_switcheroo(chip)) {
2835 if (chip->disabled && chip->bus)
2836 snd_hda_unlock_devices(chip->bus);
Takashi Iwai128960a2012-10-12 17:28:18 +02002837 if (chip->vga_switcheroo_registered)
2838 vga_switcheroo_unregister_client(chip->pci);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002839 }
2840
Takashi Iwaice43fba2005-05-30 20:33:44 +02002841 if (chip->initialized) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002842 azx_clear_irq_pending(chip);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002843 for (i = 0; i < chip->num_streams; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 azx_stream_stop(chip, &chip->azx_dev[i]);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002845 azx_stop_chip(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847
Jeff Garzikf000fd82008-04-22 13:50:34 +02002848 if (chip->irq >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 free_irq(chip->irq, (void*)chip);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002850 if (chip->msi)
Takashi Iwai30b35392006-10-11 18:52:53 +02002851 pci_disable_msi(chip->pci);
Takashi Iwaif079c252006-06-01 11:42:14 +02002852 if (chip->remap_addr)
2853 iounmap(chip->remap_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002855 if (chip->azx_dev) {
2856 for (i = 0; i < chip->num_streams; i++)
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002857 if (chip->azx_dev[i].bdl.area) {
2858 mark_pages_wc(chip, &chip->azx_dev[i].bdl, false);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002859 snd_dma_free_pages(&chip->azx_dev[i].bdl);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002860 }
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002861 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002862 if (chip->rb.area) {
2863 mark_pages_wc(chip, &chip->rb, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 snd_dma_free_pages(&chip->rb);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002865 }
2866 if (chip->posbuf.area) {
2867 mark_pages_wc(chip, &chip->posbuf, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 snd_dma_free_pages(&chip->posbuf);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002869 }
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002870 if (chip->region_requested)
2871 pci_release_regions(chip->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 pci_disable_device(chip->pci);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002873 kfree(chip->azx_dev);
Takashi Iwai4918cda2012-08-09 12:33:28 +02002874#ifdef CONFIG_SND_HDA_PATCH_LOADER
2875 if (chip->fw)
2876 release_firmware(chip->fw);
2877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 kfree(chip);
2879
2880 return 0;
2881}
2882
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002883static int azx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 return azx_free(device->device_data);
2886}
2887
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002888#ifdef SUPPORT_VGA_SWITCHEROO
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889/*
Takashi Iwai91219472012-04-26 12:13:25 +02002890 * Check of disabled HDMI controller by vga-switcheroo
2891 */
2892static struct pci_dev __devinit *get_bound_vga(struct pci_dev *pci)
2893{
2894 struct pci_dev *p;
2895
2896 /* check only discrete GPU */
2897 switch (pci->vendor) {
2898 case PCI_VENDOR_ID_ATI:
2899 case PCI_VENDOR_ID_AMD:
2900 case PCI_VENDOR_ID_NVIDIA:
2901 if (pci->devfn == 1) {
2902 p = pci_get_domain_bus_and_slot(pci_domain_nr(pci->bus),
2903 pci->bus->number, 0);
2904 if (p) {
2905 if ((p->class >> 8) == PCI_CLASS_DISPLAY_VGA)
2906 return p;
2907 pci_dev_put(p);
2908 }
2909 }
2910 break;
2911 }
2912 return NULL;
2913}
2914
2915static bool __devinit check_hdmi_disabled(struct pci_dev *pci)
2916{
2917 bool vga_inactive = false;
2918 struct pci_dev *p = get_bound_vga(pci);
2919
2920 if (p) {
Takashi Iwai12b78a72012-06-07 12:15:16 +02002921 if (vga_switcheroo_get_client_state(p) == VGA_SWITCHEROO_OFF)
Takashi Iwai91219472012-04-26 12:13:25 +02002922 vga_inactive = true;
2923 pci_dev_put(p);
2924 }
2925 return vga_inactive;
2926}
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002927#endif /* SUPPORT_VGA_SWITCHEROO */
Takashi Iwai91219472012-04-26 12:13:25 +02002928
2929/*
Takashi Iwai3372a152007-02-01 15:46:50 +01002930 * white/black-listing for position_fix
2931 */
Ralf Baechle623ec042007-03-13 15:29:47 +01002932static struct snd_pci_quirk position_fix_list[] __devinitdata = {
Takashi Iwaid2e1c972008-06-10 17:53:34 +02002933 SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
2934 SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
Takashi Iwai2f703e72009-12-01 14:17:37 +01002935 SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
Takashi Iwaid2e1c972008-06-10 17:53:34 +02002936 SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
Daniel T Chendd37f8e2010-05-30 01:17:03 -04002937 SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS", POS_FIX_LPIB),
Daniel T Chen9f75c1b2010-05-30 13:08:41 -04002938 SND_PCI_QUIRK(0x1043, 0x81e7, "ASUS M2V", POS_FIX_LPIB),
Daniel T Chene96d3122010-05-27 18:32:18 -04002939 SND_PCI_QUIRK(0x104d, 0x9069, "Sony VPCS11V9E", POS_FIX_LPIB),
David Henningssonb01de4f2012-01-12 16:31:14 +01002940 SND_PCI_QUIRK(0x10de, 0xcb89, "Macbook Pro 7,1", POS_FIX_LPIB),
Daniel T Chen61bb42c2010-05-29 11:04:11 -04002941 SND_PCI_QUIRK(0x1297, 0x3166, "Shuttle", POS_FIX_LPIB),
Daniel T Chen9ec8dda2010-03-28 02:34:40 -04002942 SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
Takashi Iwai45d4ebf2009-11-30 11:58:30 +01002943 SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
Takashi Iwai8815cd02010-04-15 09:02:41 +02002944 SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
Daniel T Chenb90c0762010-05-30 19:31:41 -04002945 SND_PCI_QUIRK(0x1849, 0x0888, "775Dual-VSTA", POS_FIX_LPIB),
Daniel T Chen0e0280d2010-04-21 19:55:43 -04002946 SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
Takashi Iwai3372a152007-02-01 15:46:50 +01002947 {}
2948};
2949
2950static int __devinit check_position_fix(struct azx *chip, int fix)
2951{
2952 const struct snd_pci_quirk *q;
2953
Takashi Iwaic673ba12009-03-17 07:49:14 +01002954 switch (fix) {
Takashi Iwai1dac6692012-09-13 14:59:47 +02002955 case POS_FIX_AUTO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01002956 case POS_FIX_LPIB:
2957 case POS_FIX_POSBUF:
David Henningsson4cb36312010-09-30 10:12:50 +02002958 case POS_FIX_VIACOMBO:
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01002959 case POS_FIX_COMBO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01002960 return fix;
2961 }
2962
Takashi Iwaic673ba12009-03-17 07:49:14 +01002963 q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
2964 if (q) {
2965 printk(KERN_INFO
2966 "hda_intel: position_fix set to %d "
2967 "for device %04x:%04x\n",
2968 q->value, q->subvendor, q->subdevice);
2969 return q->value;
Takashi Iwai3372a152007-02-01 15:46:50 +01002970 }
David Henningssonbdd9ef22010-10-04 12:02:14 +02002971
2972 /* Check VIA/ATI HD Audio Controller exist */
Takashi Iwai9477c582011-05-25 09:11:37 +02002973 if (chip->driver_caps & AZX_DCAPS_POSFIX_VIA) {
2974 snd_printd(SFX "Using VIACOMBO position fix\n");
David Henningssonbdd9ef22010-10-04 12:02:14 +02002975 return POS_FIX_VIACOMBO;
2976 }
Takashi Iwai9477c582011-05-25 09:11:37 +02002977 if (chip->driver_caps & AZX_DCAPS_POSFIX_LPIB) {
2978 snd_printd(SFX "Using LPIB position fix\n");
2979 return POS_FIX_LPIB;
2980 }
Takashi Iwaic673ba12009-03-17 07:49:14 +01002981 return POS_FIX_AUTO;
Takashi Iwai3372a152007-02-01 15:46:50 +01002982}
2983
2984/*
Takashi Iwai669ba272007-08-17 09:17:36 +02002985 * black-lists for probe_mask
2986 */
2987static struct snd_pci_quirk probe_mask_list[] __devinitdata = {
2988 /* Thinkpad often breaks the controller communication when accessing
2989 * to the non-working (or non-existing) modem codec slot.
2990 */
2991 SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
2992 SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
2993 SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
Takashi Iwai0edb9452008-11-07 14:53:09 +01002994 /* broken BIOS */
2995 SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
Takashi Iwaief1681d2008-11-24 17:29:28 +01002996 /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
2997 SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01002998 /* forced codec slots */
Ozan Çağlayan93574842009-05-23 15:00:04 +03002999 SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01003000 SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
Jaroslav Kyselaf3af9052012-04-26 17:52:35 +02003001 /* WinFast VP200 H (Teradici) user reported broken communication */
3002 SND_PCI_QUIRK(0x3a21, 0x040d, "WinFast VP200 H", 0x101),
Takashi Iwai669ba272007-08-17 09:17:36 +02003003 {}
3004};
3005
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003006#define AZX_FORCE_CODEC_MASK 0x100
3007
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003008static void __devinit check_probe_mask(struct azx *chip, int dev)
Takashi Iwai669ba272007-08-17 09:17:36 +02003009{
3010 const struct snd_pci_quirk *q;
3011
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003012 chip->codec_probe_mask = probe_mask[dev];
3013 if (chip->codec_probe_mask == -1) {
Takashi Iwai669ba272007-08-17 09:17:36 +02003014 q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
3015 if (q) {
3016 printk(KERN_INFO
3017 "hda_intel: probe_mask set to 0x%x "
3018 "for device %04x:%04x\n",
3019 q->value, q->subvendor, q->subdevice);
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003020 chip->codec_probe_mask = q->value;
Takashi Iwai669ba272007-08-17 09:17:36 +02003021 }
3022 }
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003023
3024 /* check forced option */
3025 if (chip->codec_probe_mask != -1 &&
3026 (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
3027 chip->codec_mask = chip->codec_probe_mask & 0xff;
3028 printk(KERN_INFO "hda_intel: codec_mask forced to 0x%x\n",
3029 chip->codec_mask);
3030 }
Takashi Iwai669ba272007-08-17 09:17:36 +02003031}
3032
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003033/*
Takashi Iwai716238552009-09-28 13:14:04 +02003034 * white/black-list for enable_msi
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003035 */
Takashi Iwai716238552009-09-28 13:14:04 +02003036static struct snd_pci_quirk msi_black_list[] __devinitdata = {
Takashi Iwai9dc83982009-12-22 08:15:01 +01003037 SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai0a27fcf2010-02-15 17:05:28 +01003038 SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
Ralf Gerbigecd21622010-03-09 18:25:47 +01003039 SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
Michele Ballabio4193d132010-03-06 21:06:46 +01003040 SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai38155952010-04-04 12:14:03 +02003041 SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003042 {}
3043};
3044
3045static void __devinit check_msi(struct azx *chip)
3046{
3047 const struct snd_pci_quirk *q;
3048
Takashi Iwai716238552009-09-28 13:14:04 +02003049 if (enable_msi >= 0) {
3050 chip->msi = !!enable_msi;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003051 return;
Takashi Iwai716238552009-09-28 13:14:04 +02003052 }
3053 chip->msi = 1; /* enable MSI as default */
3054 q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003055 if (q) {
3056 printk(KERN_INFO
3057 "hda_intel: msi for device %04x:%04x set to %d\n",
3058 q->subvendor, q->subdevice, q->value);
3059 chip->msi = q->value;
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003060 return;
3061 }
3062
3063 /* NVidia chipsets seem to cause troubles with MSI */
Takashi Iwai9477c582011-05-25 09:11:37 +02003064 if (chip->driver_caps & AZX_DCAPS_NO_MSI) {
3065 printk(KERN_INFO "hda_intel: Disabling MSI\n");
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003066 chip->msi = 0;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003067 }
3068}
3069
Takashi Iwaia1585d72011-12-14 09:27:04 +01003070/* check the snoop mode availability */
3071static void __devinit azx_check_snoop_available(struct azx *chip)
3072{
3073 bool snoop = chip->snoop;
3074
3075 switch (chip->driver_type) {
3076 case AZX_DRIVER_VIA:
3077 /* force to non-snoop mode for a new VIA controller
3078 * when BIOS is set
3079 */
3080 if (snoop) {
3081 u8 val;
3082 pci_read_config_byte(chip->pci, 0x42, &val);
3083 if (!(val & 0x80) && chip->pci->revision == 0x30)
3084 snoop = false;
3085 }
3086 break;
3087 case AZX_DRIVER_ATIHDMI_NS:
3088 /* new ATI HDMI requires non-snoop */
3089 snoop = false;
3090 break;
3091 }
3092
3093 if (snoop != chip->snoop) {
3094 snd_printk(KERN_INFO SFX "Force to %s mode\n",
3095 snoop ? "snoop" : "non-snoop");
3096 chip->snoop = snoop;
3097 }
3098}
Takashi Iwai669ba272007-08-17 09:17:36 +02003099
3100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 * constructor
3102 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003103static int __devinit azx_create(struct snd_card *card, struct pci_dev *pci,
Takashi Iwai9477c582011-05-25 09:11:37 +02003104 int dev, unsigned int driver_caps,
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003105 struct azx **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003107 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 .dev_free = azx_dev_free,
3109 };
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003110 struct azx *chip;
3111 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 *rchip = NULL;
Tobin Davisbcd72002008-01-15 11:23:55 +01003114
Pavel Machek927fc862006-08-31 17:03:43 +02003115 err = pci_enable_device(pci);
3116 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return err;
3118
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003119 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003120 if (!chip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 snd_printk(KERN_ERR SFX "cannot allocate chip\n");
3122 pci_disable_device(pci);
3123 return -ENOMEM;
3124 }
3125
3126 spin_lock_init(&chip->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01003127 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 chip->card = card;
3129 chip->pci = pci;
3130 chip->irq = -1;
Takashi Iwai9477c582011-05-25 09:11:37 +02003131 chip->driver_caps = driver_caps;
3132 chip->driver_type = driver_caps & 0xff;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003133 check_msi(chip);
Takashi Iwai555e2192008-06-10 17:53:34 +02003134 chip->dev_index = dev;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02003135 INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003136 INIT_LIST_HEAD(&chip->pcm_list);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003137 INIT_LIST_HEAD(&chip->list);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003138 init_vga_switcheroo(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Shahin Ghazinouribeaffc32010-05-11 08:19:55 +02003140 chip->position_fix[0] = chip->position_fix[1] =
3141 check_position_fix(chip, position_fix[dev]);
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01003142 /* combo mode uses LPIB for playback */
3143 if (chip->position_fix[0] == POS_FIX_COMBO) {
3144 chip->position_fix[0] = POS_FIX_LPIB;
3145 chip->position_fix[1] = POS_FIX_AUTO;
3146 }
3147
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003148 check_probe_mask(chip, dev);
Takashi Iwai3372a152007-02-01 15:46:50 +01003149
Takashi Iwai27346162006-01-12 18:28:44 +01003150 chip->single_cmd = single_cmd;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003151 chip->snoop = hda_snoop;
Takashi Iwaia1585d72011-12-14 09:27:04 +01003152 azx_check_snoop_available(chip);
Takashi Iwaic74db862005-05-12 14:26:27 +02003153
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003154 if (bdl_pos_adj[dev] < 0) {
3155 switch (chip->driver_type) {
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003156 case AZX_DRIVER_ICH:
Seth Heasley32679f92010-02-22 17:31:09 -08003157 case AZX_DRIVER_PCH:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003158 bdl_pos_adj[dev] = 1;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003159 break;
3160 default:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003161 bdl_pos_adj[dev] = 32;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003162 break;
3163 }
3164 }
3165
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003166 if (check_hdmi_disabled(pci)) {
3167 snd_printk(KERN_INFO SFX "VGA controller for %s is disabled\n",
3168 pci_name(pci));
3169 if (use_vga_switcheroo(chip)) {
3170 snd_printk(KERN_INFO SFX "Delaying initialization\n");
3171 chip->disabled = true;
3172 goto ok;
3173 }
3174 kfree(chip);
3175 pci_disable_device(pci);
3176 return -ENXIO;
3177 }
3178
3179 err = azx_first_init(chip);
3180 if (err < 0) {
3181 azx_free(chip);
3182 return err;
3183 }
3184
3185 ok:
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003186 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
3187 if (err < 0) {
3188 snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
3189 azx_free(chip);
3190 return err;
3191 }
3192
3193 *rchip = chip;
3194 return 0;
3195}
3196
3197static int DELAYED_INIT_MARK azx_first_init(struct azx *chip)
3198{
3199 int dev = chip->dev_index;
3200 struct pci_dev *pci = chip->pci;
3201 struct snd_card *card = chip->card;
3202 int i, err;
3203 unsigned short gcap;
3204
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003205#if BITS_PER_LONG != 64
3206 /* Fix up base address on ULI M5461 */
3207 if (chip->driver_type == AZX_DRIVER_ULI) {
3208 u16 tmp3;
3209 pci_read_config_word(pci, 0x40, &tmp3);
3210 pci_write_config_word(pci, 0x40, tmp3 | 0x10);
3211 pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
3212 }
3213#endif
3214
Pavel Machek927fc862006-08-31 17:03:43 +02003215 err = pci_request_regions(pci, "ICH HD audio");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003216 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return err;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003218 chip->region_requested = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Pavel Machek927fc862006-08-31 17:03:43 +02003220 chip->addr = pci_resource_start(pci, 0);
Arjan van de Ven2f5ad542008-09-28 16:20:09 -07003221 chip->remap_addr = pci_ioremap_bar(pci, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 if (chip->remap_addr == NULL) {
3223 snd_printk(KERN_ERR SFX "ioremap error\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003224 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 }
3226
Takashi Iwai68e7fff2006-10-23 13:40:59 +02003227 if (chip->msi)
3228 if (pci_enable_msi(pci) < 0)
3229 chip->msi = 0;
Stephen Hemminger7376d012006-08-21 19:17:46 +02003230
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003231 if (azx_acquire_irq(chip, 0) < 0)
3232 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
3234 pci_set_master(pci);
3235 synchronize_irq(chip->irq);
3236
Tobin Davisbcd72002008-01-15 11:23:55 +01003237 gcap = azx_readw(chip, GCAP);
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02003238 snd_printdd(SFX "chipset global capabilities = 0x%x\n", gcap);
Tobin Davisbcd72002008-01-15 11:23:55 +01003239
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003240 /* disable SB600 64bit support for safety */
Takashi Iwai9477c582011-05-25 09:11:37 +02003241 if (chip->pci->vendor == PCI_VENDOR_ID_ATI) {
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003242 struct pci_dev *p_smbus;
3243 p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
3244 PCI_DEVICE_ID_ATI_SBX00_SMBUS,
3245 NULL);
3246 if (p_smbus) {
3247 if (p_smbus->revision < 0x30)
3248 gcap &= ~ICH6_GCAP_64OK;
3249 pci_dev_put(p_smbus);
3250 }
3251 }
Takashi Iwai09240cf2009-03-17 07:47:18 +01003252
Takashi Iwai9477c582011-05-25 09:11:37 +02003253 /* disable 64bit DMA address on some devices */
3254 if (chip->driver_caps & AZX_DCAPS_NO_64BIT) {
3255 snd_printd(SFX "Disabling 64bit DMA\n");
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003256 gcap &= ~ICH6_GCAP_64OK;
Takashi Iwai9477c582011-05-25 09:11:37 +02003257 }
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003258
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003259 /* disable buffer size rounding to 128-byte multiples if supported */
Takashi Iwai7bfe0592012-01-23 17:53:39 +01003260 if (align_buffer_size >= 0)
3261 chip->align_buffer_size = !!align_buffer_size;
3262 else {
3263 if (chip->driver_caps & AZX_DCAPS_BUFSIZE)
3264 chip->align_buffer_size = 0;
3265 else if (chip->driver_caps & AZX_DCAPS_ALIGN_BUFSIZE)
3266 chip->align_buffer_size = 1;
3267 else
3268 chip->align_buffer_size = 1;
3269 }
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003270
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003271 /* allow 64bit DMA address if supported by H/W */
Takashi Iwaib21fadb2009-05-28 12:26:15 +02003272 if ((gcap & ICH6_GCAP_64OK) && !pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
Yang Hongyange9304382009-04-13 14:40:14 -07003273 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003274 else {
Yang Hongyange9304382009-04-13 14:40:14 -07003275 pci_set_dma_mask(pci, DMA_BIT_MASK(32));
3276 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003277 }
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003278
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003279 /* read number of streams from GCAP register instead of using
3280 * hardcoded value
3281 */
3282 chip->capture_streams = (gcap >> 8) & 0x0f;
3283 chip->playback_streams = (gcap >> 12) & 0x0f;
3284 if (!chip->playback_streams && !chip->capture_streams) {
Tobin Davisbcd72002008-01-15 11:23:55 +01003285 /* gcap didn't give any info, switching to old method */
3286
3287 switch (chip->driver_type) {
3288 case AZX_DRIVER_ULI:
3289 chip->playback_streams = ULI_NUM_PLAYBACK;
3290 chip->capture_streams = ULI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003291 break;
3292 case AZX_DRIVER_ATIHDMI:
Andiry Xu1815b342011-12-14 16:10:27 +08003293 case AZX_DRIVER_ATIHDMI_NS:
Tobin Davisbcd72002008-01-15 11:23:55 +01003294 chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
3295 chip->capture_streams = ATIHDMI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003296 break;
Yang, Libinc4da29c2008-11-13 11:07:07 +01003297 case AZX_DRIVER_GENERIC:
Tobin Davisbcd72002008-01-15 11:23:55 +01003298 default:
3299 chip->playback_streams = ICH6_NUM_PLAYBACK;
3300 chip->capture_streams = ICH6_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003301 break;
3302 }
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003303 }
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003304 chip->capture_index_offset = 0;
3305 chip->playback_index_offset = chip->capture_streams;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003306 chip->num_streams = chip->playback_streams + chip->capture_streams;
Takashi Iwaid01ce992007-07-27 16:52:19 +02003307 chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
3308 GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003309 if (!chip->azx_dev) {
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02003310 snd_printk(KERN_ERR SFX "cannot malloc azx_dev\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003311 return -ENOMEM;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003312 }
3313
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003314 for (i = 0; i < chip->num_streams; i++) {
3315 /* allocate memory for the BDL for each stream */
3316 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
3317 snd_dma_pci_data(chip->pci),
3318 BDL_SIZE, &chip->azx_dev[i].bdl);
3319 if (err < 0) {
3320 snd_printk(KERN_ERR SFX "cannot allocate BDL\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003321 return -ENOMEM;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003322 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003323 mark_pages_wc(chip, &chip->azx_dev[i].bdl, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02003325 /* allocate memory for the position buffer */
Takashi Iwaid01ce992007-07-27 16:52:19 +02003326 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
3327 snd_dma_pci_data(chip->pci),
3328 chip->num_streams * 8, &chip->posbuf);
3329 if (err < 0) {
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02003330 snd_printk(KERN_ERR SFX "cannot allocate posbuf\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003331 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003333 mark_pages_wc(chip, &chip->posbuf, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 /* allocate CORB/RIRB */
Takashi Iwai81740862009-05-26 15:22:00 +02003335 err = azx_alloc_cmd_io(chip);
3336 if (err < 0)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
3339 /* initialize streams */
3340 azx_init_stream(chip);
3341
3342 /* initialize chip */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003343 azx_init_pci(chip);
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003344 azx_init_chip(chip, (probe_only[dev] & 2) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
3346 /* codec detection */
Pavel Machek927fc862006-08-31 17:03:43 +02003347 if (!chip->codec_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 snd_printk(KERN_ERR SFX "no codecs found!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003349 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 }
3351
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003352 strcpy(card->driver, "HDA-Intel");
Takashi Iwai18cb7102009-04-16 10:22:24 +02003353 strlcpy(card->shortname, driver_short_names[chip->driver_type],
3354 sizeof(card->shortname));
3355 snprintf(card->longname, sizeof(card->longname),
3356 "%s at 0x%lx irq %i",
3357 card->shortname, chip->addr, chip->irq);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
Takashi Iwaicb53c622007-08-10 17:21:45 +02003362static void power_down_all_codecs(struct azx *chip)
3363{
Takashi Iwai83012a72012-08-24 18:38:08 +02003364#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003365 /* The codecs were powered up in snd_hda_codec_new().
3366 * Now all initialization done, so turn them down if possible
3367 */
3368 struct hda_codec *codec;
3369 list_for_each_entry(codec, &chip->bus->codec_list, list) {
3370 snd_hda_power_down(codec);
3371 }
3372#endif
3373}
3374
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003375#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003376/* callback from request_firmware_nowait() */
3377static void azx_firmware_cb(const struct firmware *fw, void *context)
3378{
3379 struct snd_card *card = context;
3380 struct azx *chip = card->private_data;
3381 struct pci_dev *pci = chip->pci;
3382
3383 if (!fw) {
3384 snd_printk(KERN_ERR SFX "Cannot load firmware, aborting\n");
3385 goto error;
3386 }
3387
3388 chip->fw = fw;
3389 if (!chip->disabled) {
3390 /* continue probing */
3391 if (azx_probe_continue(chip))
3392 goto error;
3393 }
3394 return; /* OK */
3395
3396 error:
3397 snd_card_free(card);
3398 pci_set_drvdata(pci, NULL);
3399}
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003400#endif
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003401
Takashi Iwaid01ce992007-07-27 16:52:19 +02003402static int __devinit azx_probe(struct pci_dev *pci,
3403 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003405 static int dev;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003406 struct snd_card *card;
3407 struct azx *chip;
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003408 bool probe_now;
Pavel Machek927fc862006-08-31 17:03:43 +02003409 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003411 if (dev >= SNDRV_CARDS)
3412 return -ENODEV;
3413 if (!enable[dev]) {
3414 dev++;
3415 return -ENOENT;
3416 }
3417
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003418 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
3419 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 snd_printk(KERN_ERR SFX "Error creating card!\n");
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003421 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 }
3423
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003424 snd_card_set_dev(card, &pci->dev);
3425
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003426 err = azx_create(card, pci, dev, pci_id->driver_data, &chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003427 if (err < 0)
3428 goto out_free;
Takashi Iwai421a1252005-11-17 16:11:09 +01003429 card->private_data = chip;
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003430 probe_now = !chip->disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Takashi Iwai4918cda2012-08-09 12:33:28 +02003432#ifdef CONFIG_SND_HDA_PATCH_LOADER
3433 if (patch[dev] && *patch[dev]) {
3434 snd_printk(KERN_ERR SFX "Applying patch firmware '%s'\n",
3435 patch[dev]);
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003436 err = request_firmware_nowait(THIS_MODULE, true, patch[dev],
3437 &pci->dev, GFP_KERNEL, card,
3438 azx_firmware_cb);
Takashi Iwai4918cda2012-08-09 12:33:28 +02003439 if (err < 0)
3440 goto out_free;
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003441 probe_now = false; /* continued in azx_firmware_cb() */
Takashi Iwai4918cda2012-08-09 12:33:28 +02003442 }
3443#endif /* CONFIG_SND_HDA_PATCH_LOADER */
3444
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003445 if (probe_now) {
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003446 err = azx_probe_continue(chip);
3447 if (err < 0)
3448 goto out_free;
3449 }
3450
3451 pci_set_drvdata(pci, card);
3452
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003453 if (pci_dev_run_wake(pci))
3454 pm_runtime_put_noidle(&pci->dev);
3455
Takashi Iwai128960a2012-10-12 17:28:18 +02003456 err = register_vga_switcheroo(chip);
3457 if (err < 0) {
3458 snd_printk(KERN_ERR SFX
3459 "Error registering VGA-switcheroo client\n");
3460 goto out_free;
3461 }
3462
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003463 dev++;
3464 return 0;
3465
3466out_free:
3467 snd_card_free(card);
3468 return err;
3469}
3470
3471static int DELAYED_INIT_MARK azx_probe_continue(struct azx *chip)
3472{
3473 int dev = chip->dev_index;
3474 int err;
3475
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01003476#ifdef CONFIG_SND_HDA_INPUT_BEEP
3477 chip->beep_mode = beep_mode[dev];
3478#endif
3479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 /* create codec instances */
Takashi Iwaia1e21c92009-06-17 09:33:52 +02003481 err = azx_codec_create(chip, model[dev]);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003482 if (err < 0)
3483 goto out_free;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003484#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai4918cda2012-08-09 12:33:28 +02003485 if (chip->fw) {
3486 err = snd_hda_load_patch(chip->bus, chip->fw->size,
3487 chip->fw->data);
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003488 if (err < 0)
3489 goto out_free;
Takashi Iwai4918cda2012-08-09 12:33:28 +02003490 release_firmware(chip->fw); /* no longer needed */
3491 chip->fw = NULL;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003492 }
3493#endif
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003494 if ((probe_only[dev] & 1) == 0) {
Takashi Iwaia1e21c92009-06-17 09:33:52 +02003495 err = azx_codec_configure(chip);
3496 if (err < 0)
3497 goto out_free;
3498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500 /* create PCM streams */
Takashi Iwai176d5332008-07-30 15:01:44 +02003501 err = snd_hda_build_pcms(chip->bus);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003502 if (err < 0)
3503 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 /* create mixer controls */
Takashi Iwaid01ce992007-07-27 16:52:19 +02003506 err = azx_mixer_create(chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003507 if (err < 0)
3508 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003510 err = snd_card_register(chip->card);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003511 if (err < 0)
3512 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Takashi Iwaicb53c622007-08-10 17:21:45 +02003514 chip->running = 1;
3515 power_down_all_codecs(chip);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01003516 azx_notifier_register(chip);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003517 azx_add_card_list(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Takashi Iwai91219472012-04-26 12:13:25 +02003519 return 0;
3520
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003521out_free:
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003522 chip->init_failed = 1;
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
3526static void __devexit azx_remove(struct pci_dev *pci)
3527{
Takashi Iwai91219472012-04-26 12:13:25 +02003528 struct snd_card *card = pci_get_drvdata(pci);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003529
3530 if (pci_dev_run_wake(pci))
3531 pm_runtime_get_noresume(&pci->dev);
3532
Takashi Iwai91219472012-04-26 12:13:25 +02003533 if (card)
3534 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 pci_set_drvdata(pci, NULL);
3536}
3537
3538/* PCI IDs */
Alexey Dobriyancebe41d2010-02-06 00:21:03 +02003539static DEFINE_PCI_DEVICE_TABLE(azx_ids) = {
Seth Heasleyd2f2fcd2010-01-12 17:03:35 -08003540 /* CPT */
Takashi Iwai9477c582011-05-25 09:11:37 +02003541 { PCI_DEVICE(0x8086, 0x1c20),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003542 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_SCH_SNOOP |
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05003543 AZX_DCAPS_BUFSIZE | AZX_DCAPS_COUNT_LPIB_DELAY },
Seth Heasleycea310e2010-09-10 16:29:56 -07003544 /* PBG */
Takashi Iwai9477c582011-05-25 09:11:37 +02003545 { PCI_DEVICE(0x8086, 0x1d20),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003546 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_SCH_SNOOP |
3547 AZX_DCAPS_BUFSIZE},
Seth Heasleyd2edeb72011-04-20 10:59:57 -07003548 /* Panther Point */
Takashi Iwai9477c582011-05-25 09:11:37 +02003549 { PCI_DEVICE(0x8086, 0x1e20),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003550 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_SCH_SNOOP |
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05003551 AZX_DCAPS_BUFSIZE | AZX_DCAPS_COUNT_LPIB_DELAY },
Seth Heasley8bc039a2012-01-23 16:24:31 -08003552 /* Lynx Point */
3553 { PCI_DEVICE(0x8086, 0x8c20),
3554 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_SCH_SNOOP |
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05003555 AZX_DCAPS_BUFSIZE | AZX_DCAPS_COUNT_LPIB_DELAY },
James Ralston144dad92012-08-09 09:38:59 -07003556 /* Lynx Point-LP */
3557 { PCI_DEVICE(0x8086, 0x9c20),
3558 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_SCH_SNOOP |
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05003559 AZX_DCAPS_BUFSIZE | AZX_DCAPS_COUNT_LPIB_DELAY },
James Ralston144dad92012-08-09 09:38:59 -07003560 /* Lynx Point-LP */
3561 { PCI_DEVICE(0x8086, 0x9c21),
3562 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_SCH_SNOOP |
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05003563 AZX_DCAPS_BUFSIZE | AZX_DCAPS_COUNT_LPIB_DELAY },
Wang Xingchaoe926f2c2012-06-13 10:23:51 +08003564 /* Haswell */
3565 { PCI_DEVICE(0x8086, 0x0c0c),
Takashi Iwaibdbe34d2012-07-16 16:17:10 +02003566 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_SCH_SNOOP |
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05003567 AZX_DCAPS_BUFSIZE | AZX_DCAPS_COUNT_LPIB_DELAY },
Wang Xingchaod279fae2012-09-17 13:10:23 +08003568 { PCI_DEVICE(0x8086, 0x0d0c),
3569 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_SCH_SNOOP |
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05003570 AZX_DCAPS_BUFSIZE | AZX_DCAPS_COUNT_LPIB_DELAY },
Pierre-Louis Bossart99df18b2012-09-21 18:39:07 -05003571 /* 5 Series/3400 */
3572 { PCI_DEVICE(0x8086, 0x3b56),
3573 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_SCH_SNOOP |
3574 AZX_DCAPS_BUFSIZE | AZX_DCAPS_COUNT_LPIB_DELAY },
Takashi Iwai87218e92008-02-21 08:13:11 +01003575 /* SCH */
Takashi Iwai9477c582011-05-25 09:11:37 +02003576 { PCI_DEVICE(0x8086, 0x811b),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003577 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_SCH_SNOOP |
David Henningsson645e9032011-12-14 15:52:30 +08003578 AZX_DCAPS_BUFSIZE | AZX_DCAPS_POSFIX_LPIB }, /* Poulsbo */
Li Peng09904b92011-12-28 15:17:26 +00003579 { PCI_DEVICE(0x8086, 0x080a),
3580 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_SCH_SNOOP |
David Henningsson716e5db2012-01-04 10:12:54 +01003581 AZX_DCAPS_BUFSIZE | AZX_DCAPS_POSFIX_LPIB }, /* Oaktrail */
David Henningsson645e9032011-12-14 15:52:30 +08003582 /* ICH */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003583 { PCI_DEVICE(0x8086, 0x2668),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003584 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3585 AZX_DCAPS_BUFSIZE }, /* ICH6 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003586 { PCI_DEVICE(0x8086, 0x27d8),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003587 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3588 AZX_DCAPS_BUFSIZE }, /* ICH7 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003589 { PCI_DEVICE(0x8086, 0x269a),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003590 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3591 AZX_DCAPS_BUFSIZE }, /* ESB2 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003592 { PCI_DEVICE(0x8086, 0x284b),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003593 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3594 AZX_DCAPS_BUFSIZE }, /* ICH8 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003595 { PCI_DEVICE(0x8086, 0x293e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003596 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3597 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003598 { PCI_DEVICE(0x8086, 0x293f),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003599 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3600 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003601 { PCI_DEVICE(0x8086, 0x3a3e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003602 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3603 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003604 { PCI_DEVICE(0x8086, 0x3a6e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003605 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3606 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwaib6864532010-09-15 10:17:26 +02003607 /* Generic Intel */
3608 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
3609 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3610 .class_mask = 0xffffff,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003611 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_BUFSIZE },
Takashi Iwai9477c582011-05-25 09:11:37 +02003612 /* ATI SB 450/600/700/800/900 */
3613 { PCI_DEVICE(0x1002, 0x437b),
3614 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
3615 { PCI_DEVICE(0x1002, 0x4383),
3616 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
3617 /* AMD Hudson */
3618 { PCI_DEVICE(0x1022, 0x780d),
3619 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB },
Takashi Iwai87218e92008-02-21 08:13:11 +01003620 /* ATI HDMI */
Takashi Iwai9477c582011-05-25 09:11:37 +02003621 { PCI_DEVICE(0x1002, 0x793b),
3622 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3623 { PCI_DEVICE(0x1002, 0x7919),
3624 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3625 { PCI_DEVICE(0x1002, 0x960f),
3626 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3627 { PCI_DEVICE(0x1002, 0x970f),
3628 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3629 { PCI_DEVICE(0x1002, 0xaa00),
3630 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3631 { PCI_DEVICE(0x1002, 0xaa08),
3632 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3633 { PCI_DEVICE(0x1002, 0xaa10),
3634 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3635 { PCI_DEVICE(0x1002, 0xaa18),
3636 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3637 { PCI_DEVICE(0x1002, 0xaa20),
3638 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3639 { PCI_DEVICE(0x1002, 0xaa28),
3640 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3641 { PCI_DEVICE(0x1002, 0xaa30),
3642 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3643 { PCI_DEVICE(0x1002, 0xaa38),
3644 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3645 { PCI_DEVICE(0x1002, 0xaa40),
3646 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3647 { PCI_DEVICE(0x1002, 0xaa48),
3648 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Xu1815b342011-12-14 16:10:27 +08003649 { PCI_DEVICE(0x1002, 0x9902),
3650 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3651 { PCI_DEVICE(0x1002, 0xaaa0),
3652 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3653 { PCI_DEVICE(0x1002, 0xaaa8),
3654 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3655 { PCI_DEVICE(0x1002, 0xaab0),
3656 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
Takashi Iwai87218e92008-02-21 08:13:11 +01003657 /* VIA VT8251/VT8237A */
Takashi Iwai9477c582011-05-25 09:11:37 +02003658 { PCI_DEVICE(0x1106, 0x3288),
3659 .driver_data = AZX_DRIVER_VIA | AZX_DCAPS_POSFIX_VIA },
Annie Liu754fdff2012-06-08 19:18:39 +08003660 /* VIA GFX VT7122/VX900 */
3661 { PCI_DEVICE(0x1106, 0x9170), .driver_data = AZX_DRIVER_GENERIC },
3662 /* VIA GFX VT6122/VX11 */
3663 { PCI_DEVICE(0x1106, 0x9140), .driver_data = AZX_DRIVER_GENERIC },
Takashi Iwai87218e92008-02-21 08:13:11 +01003664 /* SIS966 */
3665 { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
3666 /* ULI M5461 */
3667 { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
3668 /* NVIDIA MCP */
Takashi Iwai0c2fd1bf42009-12-18 16:41:39 +01003669 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
3670 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3671 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003672 .driver_data = AZX_DRIVER_NVIDIA | AZX_DCAPS_PRESET_NVIDIA },
Kailang Yangf2690022008-05-27 11:44:55 +02003673 /* Teradici */
Takashi Iwai9477c582011-05-25 09:11:37 +02003674 { PCI_DEVICE(0x6549, 0x1200),
3675 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
Takashi Iwai4e01f542009-04-16 08:53:34 +02003676 /* Creative X-Fi (CA0110-IBG) */
Takashi Iwaif2a8eca2012-06-11 15:51:54 +02003677 /* CTHDA chips */
3678 { PCI_DEVICE(0x1102, 0x0010),
3679 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
3680 { PCI_DEVICE(0x1102, 0x0012),
3681 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
Takashi Iwai313f6e22009-05-18 12:40:52 +02003682#if !defined(CONFIG_SND_CTXFI) && !defined(CONFIG_SND_CTXFI_MODULE)
3683 /* the following entry conflicts with snd-ctxfi driver,
3684 * as ctxfi driver mutates from HD-audio to native mode with
3685 * a special command sequence.
3686 */
Takashi Iwai4e01f542009-04-16 08:53:34 +02003687 { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
3688 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3689 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003690 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01003691 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02003692#else
3693 /* this entry seems still valid -- i.e. without emu20kx chip */
Takashi Iwai9477c582011-05-25 09:11:37 +02003694 { PCI_DEVICE(0x1102, 0x0009),
3695 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01003696 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02003697#endif
Otavio Salvadore35d4b12010-09-26 23:35:06 -03003698 /* Vortex86MX */
3699 { PCI_DEVICE(0x17f3, 0x3010), .driver_data = AZX_DRIVER_GENERIC },
Bankim Bhavsar0f0714c52011-01-17 15:23:21 +01003700 /* VMware HDAudio */
3701 { PCI_DEVICE(0x15ad, 0x1977), .driver_data = AZX_DRIVER_GENERIC },
Andiry Brienza9176b672009-07-17 11:32:32 +08003702 /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
Yang, Libinc4da29c2008-11-13 11:07:07 +01003703 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
3704 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3705 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003706 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Brienza9176b672009-07-17 11:32:32 +08003707 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
3708 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3709 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003710 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 { 0, }
3712};
3713MODULE_DEVICE_TABLE(pci, azx_ids);
3714
3715/* pci_driver definition */
Takashi Iwaie9f66d92012-04-24 12:25:00 +02003716static struct pci_driver azx_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02003717 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 .id_table = azx_ids,
3719 .probe = azx_probe,
3720 .remove = __devexit_p(azx_remove),
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003721 .driver = {
3722 .pm = AZX_PM_OPS,
3723 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724};
3725
Takashi Iwaie9f66d92012-04-24 12:25:00 +02003726module_pci_driver(azx_driver);