blob: da717b5223c4ce2952c8f8b79eb3446f1c121042 [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>
Takashi Iwaif4c482a2012-12-04 15:09:23 +010052#include <linux/completion.h>
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -050053
Takashi Iwai27fe48d92011-09-28 17:16:09 +020054#ifdef CONFIG_X86
55/* for snoop control */
56#include <asm/pgtable.h>
57#include <asm/cacheflush.h>
58#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <sound/core.h>
60#include <sound/initval.h>
Takashi Iwai91219472012-04-26 12:13:25 +020061#include <linux/vgaarb.h>
Takashi Iwaia82d51e2012-04-26 12:23:42 +020062#include <linux/vga_switcheroo.h>
Takashi Iwai4918cda2012-08-09 12:33:28 +020063#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "hda_codec.h"
Wang Xingchao99a20082013-05-30 22:07:10 +080065#include "hda_i915.h"
Dylan Reid2538a4f2014-02-28 15:41:12 -080066#include "hda_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68
Takashi Iwai5aba4f82008-01-07 15:16:37 +010069static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
70static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
Rusty Russella67ff6a2011-12-15 13:49:36 +103071static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Takashi Iwai5aba4f82008-01-07 15:16:37 +010072static char *model[SNDRV_CARDS];
Takashi Iwai1dac6692012-09-13 14:59:47 +020073static int position_fix[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +020074static int bdl_pos_adj[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
Takashi Iwai5aba4f82008-01-07 15:16:37 +010075static int probe_mask[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
Takashi Iwaid4d9cd032008-12-19 15:19:11 +010076static int probe_only[SNDRV_CARDS];
David Henningsson26a6cb62012-10-09 15:04:21 +020077static int jackpoll_ms[SNDRV_CARDS];
Rusty Russella67ff6a2011-12-15 13:49:36 +103078static bool single_cmd;
Takashi Iwai716238552009-09-28 13:14:04 +020079static int enable_msi = -1;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +020080#ifdef CONFIG_SND_HDA_PATCH_LOADER
81static char *patch[SNDRV_CARDS];
82#endif
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +010083#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwai0920c9b2012-07-03 16:58:48 +020084static bool beep_mode[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] =
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +010085 CONFIG_SND_HDA_INPUT_BEEP_MODE};
86#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Takashi Iwai5aba4f82008-01-07 15:16:37 +010088module_param_array(index, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010090module_param_array(id, charp, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010092module_param_array(enable, bool, NULL, 0444);
93MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
94module_param_array(model, charp, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095MODULE_PARM_DESC(model, "Use the given board model.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010096module_param_array(position_fix, int, NULL, 0444);
David Henningsson4cb36312010-09-30 10:12:50 +020097MODULE_PARM_DESC(position_fix, "DMA pointer read method."
Takashi Iwai1dac6692012-09-13 14:59:47 +020098 "(-1 = system default, 0 = auto, 1 = LPIB, 2 = POSBUF, 3 = VIACOMBO, 4 = COMBO).");
Takashi Iwai555e2192008-06-10 17:53:34 +020099module_param_array(bdl_pos_adj, int, NULL, 0644);
100MODULE_PARM_DESC(bdl_pos_adj, "BDL position adjustment offset.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +0100101module_param_array(probe_mask, int, NULL, 0444);
Takashi Iwai606ad752005-11-24 16:03:40 +0100102MODULE_PARM_DESC(probe_mask, "Bitmask to probe codecs (default = -1).");
Jaroslav Kysela079e6832010-03-26 11:16:59 +0100103module_param_array(probe_only, int, NULL, 0444);
Takashi Iwaid4d9cd032008-12-19 15:19:11 +0100104MODULE_PARM_DESC(probe_only, "Only probing and no codec initialization.");
David Henningsson26a6cb62012-10-09 15:04:21 +0200105module_param_array(jackpoll_ms, int, NULL, 0444);
106MODULE_PARM_DESC(jackpoll_ms, "Ms between polling for jack events (default = 0, using unsol events only)");
Takashi Iwai27346162006-01-12 18:28:44 +0100107module_param(single_cmd, bool, 0444);
Takashi Iwaid01ce992007-07-27 16:52:19 +0200108MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
109 "(for debugging only).");
Takashi Iwaiac9ef6c2012-01-20 12:08:44 +0100110module_param(enable_msi, bint, 0444);
Takashi Iwai134a11f2006-11-10 12:08:37 +0100111MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200112#ifdef CONFIG_SND_HDA_PATCH_LOADER
113module_param_array(patch, charp, NULL, 0444);
114MODULE_PARM_DESC(patch, "Patch file for Intel HD audio interface.");
115#endif
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100116#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwai0920c9b2012-07-03 16:58:48 +0200117module_param_array(beep_mode, bool, NULL, 0444);
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100118MODULE_PARM_DESC(beep_mode, "Select HDA Beep registration mode "
Takashi Iwai0920c9b2012-07-03 16:58:48 +0200119 "(0=off, 1=on) (default=1).");
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100120#endif
Takashi Iwai606ad752005-11-24 16:03:40 +0100121
Takashi Iwai83012a72012-08-24 18:38:08 +0200122#ifdef CONFIG_PM
Takashi Iwai65fcd412012-08-14 17:13:32 +0200123static int param_set_xint(const char *val, const struct kernel_param *kp);
124static struct kernel_param_ops param_ops_xint = {
125 .set = param_set_xint,
126 .get = param_get_int,
127};
128#define param_check_xint param_check_int
129
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100130static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
Takashi Iwai65fcd412012-08-14 17:13:32 +0200131module_param(power_save, xint, 0644);
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100132MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
133 "(in second, 0 = disable).");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Takashi Iwaidee1b662007-08-13 16:10:30 +0200135/* reset the HD-audio controller in power save mode.
136 * this may give more power-saving, but will take longer time to
137 * wake up.
138 */
Takashi Iwai8fc24422013-04-04 15:35:24 +0200139static bool power_save_controller = 1;
140module_param(power_save_controller, bool, 0644);
Takashi Iwaidee1b662007-08-13 16:10:30 +0200141MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
Takashi Iwai83012a72012-08-24 18:38:08 +0200142#endif /* CONFIG_PM */
Takashi Iwaidee1b662007-08-13 16:10:30 +0200143
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100144static int align_buffer_size = -1;
145module_param(align_buffer_size, bint, 0644);
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -0500146MODULE_PARM_DESC(align_buffer_size,
147 "Force buffer and period sizes to be multiple of 128 bytes.");
148
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200149#ifdef CONFIG_X86
150static bool hda_snoop = true;
151module_param_named(snoop, hda_snoop, bool, 0444);
152MODULE_PARM_DESC(snoop, "Enable/disable snooping");
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200153#else
154#define hda_snoop true
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200155#endif
156
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158MODULE_LICENSE("GPL");
159MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
160 "{Intel, ICH6M},"
Jason Gaston2f1b3812005-05-01 08:58:50 -0700161 "{Intel, ICH7},"
Frederick Lif5d40b32005-05-12 14:55:20 +0200162 "{Intel, ESB2},"
Jason Gastond2981392006-01-10 11:07:37 +0100163 "{Intel, ICH8},"
Jason Gastonf9cc8a82006-11-22 11:53:52 +0100164 "{Intel, ICH9},"
Jason Gastonc34f5a02008-01-29 12:38:49 +0100165 "{Intel, ICH10},"
Seth Heasleyb29c2362008-08-08 15:56:39 -0700166 "{Intel, PCH},"
Seth Heasleyd2f2fcd2010-01-12 17:03:35 -0800167 "{Intel, CPT},"
Seth Heasleyd2edeb72011-04-20 10:59:57 -0700168 "{Intel, PPT},"
Seth Heasley8bc039a2012-01-23 16:24:31 -0800169 "{Intel, LPT},"
James Ralston144dad92012-08-09 09:38:59 -0700170 "{Intel, LPT_LP},"
James Ralston4eeca492013-11-04 09:27:45 -0800171 "{Intel, WPT_LP},"
Wang Xingchaoe926f2c2012-06-13 10:23:51 +0800172 "{Intel, HPT},"
Seth Heasleycea310e2010-09-10 16:29:56 -0700173 "{Intel, PBG},"
Tobin Davis4979bca2008-01-30 08:13:55 +0100174 "{Intel, SCH},"
Takashi Iwaifc20a562005-05-12 15:00:41 +0200175 "{ATI, SB450},"
Felix Kuehling89be83f2006-03-31 12:33:59 +0200176 "{ATI, SB600},"
Felix Kuehling778b6e12006-05-17 11:22:21 +0200177 "{ATI, RS600},"
Felix Kuehling5b15c952006-10-16 12:49:47 +0200178 "{ATI, RS690},"
Wolke Liue6db1112007-04-27 12:20:57 +0200179 "{ATI, RS780},"
180 "{ATI, R600},"
Herton Ronaldo Krzesinski2797f722007-11-05 18:21:56 +0100181 "{ATI, RV630},"
182 "{ATI, RV610},"
Wolke Liu27da1832007-11-16 11:06:30 +0100183 "{ATI, RV670},"
184 "{ATI, RV635},"
185 "{ATI, RV620},"
186 "{ATI, RV770},"
Takashi Iwaifc20a562005-05-12 15:00:41 +0200187 "{VIA, VT8251},"
Takashi Iwai47672312005-08-12 16:44:04 +0200188 "{VIA, VT8237A},"
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200189 "{SiS, SIS966},"
190 "{ULI, M5461}}");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191MODULE_DESCRIPTION("Intel HDA driver");
192
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200193#if defined(CONFIG_PM) && defined(CONFIG_VGA_SWITCHEROO)
Takashi Iwaif8f1bec2014-02-06 18:14:03 +0100194#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200195#define SUPPORT_VGA_SWITCHEROO
196#endif
197#endif
198
199
Takashi Iwaicb53c622007-08-10 17:21:45 +0200200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Takashi Iwaieb49faa2013-03-15 09:19:11 +0100203/* DSP lock helpers */
204#ifdef CONFIG_SND_HDA_DSP_LOADER
205#define dsp_lock_init(dev) mutex_init(&(dev)->dsp_mutex)
206#define dsp_lock(dev) mutex_lock(&(dev)->dsp_mutex)
207#define dsp_unlock(dev) mutex_unlock(&(dev)->dsp_mutex)
208#define dsp_is_locked(dev) ((dev)->locked)
209#else
210#define dsp_lock_init(dev) do {} while (0)
211#define dsp_lock(dev) do {} while (0)
212#define dsp_unlock(dev) do {} while (0)
213#define dsp_is_locked(dev) 0
214#endif
215
Takashi Iwai1a8506d2012-10-16 15:10:08 +0200216#define CREATE_TRACE_POINTS
217#include "hda_intel_trace.h"
218
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200219/* driver types */
220enum {
221 AZX_DRIVER_ICH,
Seth Heasley32679f92010-02-22 17:31:09 -0800222 AZX_DRIVER_PCH,
Tobin Davis4979bca2008-01-30 08:13:55 +0100223 AZX_DRIVER_SCH,
Takashi Iwaifab12852013-11-05 17:54:05 +0100224 AZX_DRIVER_HDMI,
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200225 AZX_DRIVER_ATI,
Felix Kuehling778b6e12006-05-17 11:22:21 +0200226 AZX_DRIVER_ATIHDMI,
Andiry Xu1815b342011-12-14 16:10:27 +0800227 AZX_DRIVER_ATIHDMI_NS,
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200228 AZX_DRIVER_VIA,
229 AZX_DRIVER_SIS,
230 AZX_DRIVER_ULI,
Vinod Gda3fca22005-09-13 18:49:12 +0200231 AZX_DRIVER_NVIDIA,
Kailang Yangf2690022008-05-27 11:44:55 +0200232 AZX_DRIVER_TERA,
Takashi Iwai14d34f12010-10-21 09:03:25 +0200233 AZX_DRIVER_CTX,
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200234 AZX_DRIVER_CTHDA,
Yang, Libinc4da29c2008-11-13 11:07:07 +0100235 AZX_DRIVER_GENERIC,
Takashi Iwai2f5983f2008-09-03 16:00:44 +0200236 AZX_NUM_DRIVERS, /* keep this as last entry */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200237};
238
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +0100239/* quirks for Intel PCH */
Takashi Iwaid7dab4d2013-01-08 13:51:30 +0100240#define AZX_DCAPS_INTEL_PCH_NOPM \
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +0100241 (AZX_DCAPS_SCH_SNOOP | AZX_DCAPS_BUFSIZE | \
Takashi Iwaid7dab4d2013-01-08 13:51:30 +0100242 AZX_DCAPS_COUNT_LPIB_DELAY)
243
244#define AZX_DCAPS_INTEL_PCH \
245 (AZX_DCAPS_INTEL_PCH_NOPM | AZX_DCAPS_PM_RUNTIME)
Takashi Iwai9477c582011-05-25 09:11:37 +0200246
Takashi Iwai33499a12013-11-05 17:34:46 +0100247#define AZX_DCAPS_INTEL_HASWELL \
248 (AZX_DCAPS_SCH_SNOOP | AZX_DCAPS_ALIGN_BUFSIZE | \
249 AZX_DCAPS_COUNT_LPIB_DELAY | AZX_DCAPS_PM_RUNTIME | \
250 AZX_DCAPS_I915_POWERWELL)
251
Takashi Iwai9477c582011-05-25 09:11:37 +0200252/* quirks for ATI SB / AMD Hudson */
253#define AZX_DCAPS_PRESET_ATI_SB \
254 (AZX_DCAPS_ATI_SNOOP | AZX_DCAPS_NO_TCSEL | \
255 AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
256
257/* quirks for ATI/AMD HDMI */
258#define AZX_DCAPS_PRESET_ATI_HDMI \
259 (AZX_DCAPS_NO_TCSEL | AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
260
261/* quirks for Nvidia */
262#define AZX_DCAPS_PRESET_NVIDIA \
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100263 (AZX_DCAPS_NVIDIA_SNOOP | AZX_DCAPS_RIRB_DELAY | AZX_DCAPS_NO_MSI |\
Mike Travis49d9e772013-05-01 14:04:08 -0500264 AZX_DCAPS_ALIGN_BUFSIZE | AZX_DCAPS_NO_64BIT)
Takashi Iwai9477c582011-05-25 09:11:37 +0200265
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200266#define AZX_DCAPS_PRESET_CTHDA \
267 (AZX_DCAPS_NO_MSI | AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_4K_BDLE_BOUNDARY)
268
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200269/*
270 * VGA-switcher support
271 */
272#ifdef SUPPORT_VGA_SWITCHEROO
Takashi Iwai5cb543d2012-08-09 13:49:23 +0200273#define use_vga_switcheroo(chip) ((chip)->use_vga_switcheroo)
274#else
275#define use_vga_switcheroo(chip) 0
276#endif
277
Takashi Iwai48c8b0e2012-12-07 07:40:35 +0100278static char *driver_short_names[] = {
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200279 [AZX_DRIVER_ICH] = "HDA Intel",
Seth Heasley32679f92010-02-22 17:31:09 -0800280 [AZX_DRIVER_PCH] = "HDA Intel PCH",
Tobin Davis4979bca2008-01-30 08:13:55 +0100281 [AZX_DRIVER_SCH] = "HDA Intel MID",
Takashi Iwaifab12852013-11-05 17:54:05 +0100282 [AZX_DRIVER_HDMI] = "HDA Intel HDMI",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200283 [AZX_DRIVER_ATI] = "HDA ATI SB",
Felix Kuehling778b6e12006-05-17 11:22:21 +0200284 [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
Andiry Xu1815b342011-12-14 16:10:27 +0800285 [AZX_DRIVER_ATIHDMI_NS] = "HDA ATI HDMI",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200286 [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
287 [AZX_DRIVER_SIS] = "HDA SIS966",
Vinod Gda3fca22005-09-13 18:49:12 +0200288 [AZX_DRIVER_ULI] = "HDA ULI M5461",
289 [AZX_DRIVER_NVIDIA] = "HDA NVidia",
Kailang Yangf2690022008-05-27 11:44:55 +0200290 [AZX_DRIVER_TERA] = "HDA Teradici",
Takashi Iwai14d34f12010-10-21 09:03:25 +0200291 [AZX_DRIVER_CTX] = "HDA Creative",
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200292 [AZX_DRIVER_CTHDA] = "HDA Creative",
Yang, Libinc4da29c2008-11-13 11:07:07 +0100293 [AZX_DRIVER_GENERIC] = "HD-Audio Generic",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200294};
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/*
297 * macros for easy use
298 */
299#define azx_writel(chip,reg,value) \
300 writel(value, (chip)->remap_addr + ICH6_REG_##reg)
301#define azx_readl(chip,reg) \
302 readl((chip)->remap_addr + ICH6_REG_##reg)
303#define azx_writew(chip,reg,value) \
304 writew(value, (chip)->remap_addr + ICH6_REG_##reg)
305#define azx_readw(chip,reg) \
306 readw((chip)->remap_addr + ICH6_REG_##reg)
307#define azx_writeb(chip,reg,value) \
308 writeb(value, (chip)->remap_addr + ICH6_REG_##reg)
309#define azx_readb(chip,reg) \
310 readb((chip)->remap_addr + ICH6_REG_##reg)
311
312#define azx_sd_writel(dev,reg,value) \
313 writel(value, (dev)->sd_addr + ICH6_REG_##reg)
314#define azx_sd_readl(dev,reg) \
315 readl((dev)->sd_addr + ICH6_REG_##reg)
316#define azx_sd_writew(dev,reg,value) \
317 writew(value, (dev)->sd_addr + ICH6_REG_##reg)
318#define azx_sd_readw(dev,reg) \
319 readw((dev)->sd_addr + ICH6_REG_##reg)
320#define azx_sd_writeb(dev,reg,value) \
321 writeb(value, (dev)->sd_addr + ICH6_REG_##reg)
322#define azx_sd_readb(dev,reg) \
323 readb((dev)->sd_addr + ICH6_REG_##reg)
324
325/* for pcm support */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100326#define get_azx_dev(substream) (substream->runtime->private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200328#ifdef CONFIG_X86
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100329static void __mark_pages_wc(struct azx *chip, struct snd_dma_buffer *dmab, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200330{
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100331 int pages;
332
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200333 if (azx_snoop(chip))
334 return;
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100335 if (!dmab || !dmab->area || !dmab->bytes)
336 return;
337
338#ifdef CONFIG_SND_DMA_SGBUF
339 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_SG) {
340 struct snd_sg_buf *sgbuf = dmab->private_data;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200341 if (on)
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100342 set_pages_array_wc(sgbuf->page_table, sgbuf->pages);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200343 else
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100344 set_pages_array_wb(sgbuf->page_table, sgbuf->pages);
345 return;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200346 }
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100347#endif
348
349 pages = (dmab->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
350 if (on)
351 set_memory_wc((unsigned long)dmab->area, pages);
352 else
353 set_memory_wb((unsigned long)dmab->area, pages);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200354}
355
356static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
357 bool on)
358{
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100359 __mark_pages_wc(chip, buf, on);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200360}
361static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100362 struct snd_pcm_substream *substream, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200363{
364 if (azx_dev->wc_marked != on) {
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100365 __mark_pages_wc(chip, snd_pcm_get_dma_buf(substream), on);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200366 azx_dev->wc_marked = on;
367 }
368}
369#else
370/* NOP for other archs */
371static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
372 bool on)
373{
374}
375static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100376 struct snd_pcm_substream *substream, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200377{
378}
379#endif
380
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200381static int azx_acquire_irq(struct azx *chip, int do_disconnect);
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200382static int azx_send_cmd(struct hda_bus *bus, unsigned int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/*
384 * Interface for HD codec
385 */
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
388 * CORB / RIRB interface
389 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100390static int azx_alloc_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 int err;
393
394 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200395 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
396 snd_dma_pci_data(chip->pci),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 PAGE_SIZE, &chip->rb);
398 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100399 dev_err(chip->card->dev, "cannot allocate CORB/RIRB\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return err;
401 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200402 mark_pages_wc(chip, &chip->rb, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
404}
405
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100406static void azx_init_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
David Henningssonca460f82014-02-28 07:56:58 +0100408 int timeout;
409
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800410 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* CORB set up */
412 chip->corb.addr = chip->rb.addr;
413 chip->corb.buf = (u32 *)chip->rb.area;
414 azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200415 azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200417 /* set the corb size to 256 entries (ULI requires explicitly) */
418 azx_writeb(chip, CORBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* set the corb write pointer to 0 */
420 azx_writew(chip, CORBWP, 0);
David Henningssonca460f82014-02-28 07:56:58 +0100421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* reset the corb hw read pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200423 azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
David Henningssonca460f82014-02-28 07:56:58 +0100424 for (timeout = 1000; timeout > 0; timeout--) {
425 if ((azx_readw(chip, CORBRP) & ICH6_CORBRP_RST) == ICH6_CORBRP_RST)
426 break;
427 udelay(1);
428 }
429 if (timeout <= 0)
430 dev_err(chip->card->dev, "CORB reset timeout#1, CORBRP = %d\n",
431 azx_readw(chip, CORBRP));
432
433 azx_writew(chip, CORBRP, 0);
434 for (timeout = 1000; timeout > 0; timeout--) {
435 if (azx_readw(chip, CORBRP) == 0)
436 break;
437 udelay(1);
438 }
439 if (timeout <= 0)
440 dev_err(chip->card->dev, "CORB reset timeout#2, CORBRP = %d\n",
441 azx_readw(chip, CORBRP));
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* enable corb dma */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200444 azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* RIRB set up */
447 chip->rirb.addr = chip->rb.addr + 2048;
448 chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800449 chip->rirb.wp = chip->rirb.rp = 0;
450 memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200452 azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200454 /* set the rirb size to 256 entries (ULI requires explicitly) */
455 azx_writeb(chip, RIRBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* reset the rirb hw write pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200457 azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* set N=1, get RIRB response interrupt for new entry */
Takashi Iwai9477c582011-05-25 09:11:37 +0200459 if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
Takashi Iwai14d34f12010-10-21 09:03:25 +0200460 azx_writew(chip, RINTCNT, 0xc0);
461 else
462 azx_writew(chip, RINTCNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /* enable rirb dma and response irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800465 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100468static void azx_free_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800470 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* disable ringbuffer DMAs */
472 azx_writeb(chip, RIRBCTL, 0);
473 azx_writeb(chip, CORBCTL, 0);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800474 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Wu Fengguangdeadff12009-08-01 18:45:16 +0800477static unsigned int azx_command_addr(u32 cmd)
478{
479 unsigned int addr = cmd >> 28;
480
481 if (addr >= AZX_MAX_CODECS) {
482 snd_BUG();
483 addr = 0;
484 }
485
486 return addr;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100490static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100492 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800493 unsigned int addr = azx_command_addr(val);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100494 unsigned int wp, rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Wu Fengguangc32649f2009-08-01 18:48:12 +0800496 spin_lock_irq(&chip->reg_lock);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* add command to corb */
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100499 wp = azx_readw(chip, CORBWP);
500 if (wp == 0xffff) {
501 /* something wrong, controller likely turned to D3 */
502 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100503 return -EIO;
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 wp++;
506 wp %= ICH6_MAX_CORB_ENTRIES;
507
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100508 rp = azx_readw(chip, CORBRP);
509 if (wp == rp) {
510 /* oops, it's full */
511 spin_unlock_irq(&chip->reg_lock);
512 return -EAGAIN;
513 }
514
Wu Fengguangdeadff12009-08-01 18:45:16 +0800515 chip->rirb.cmds[addr]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 chip->corb.buf[wp] = cpu_to_le32(val);
David Henningssonca460f82014-02-28 07:56:58 +0100517 azx_writew(chip, CORBWP, wp);
Wu Fengguangc32649f2009-08-01 18:48:12 +0800518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 spin_unlock_irq(&chip->reg_lock);
520
521 return 0;
522}
523
524#define ICH6_RIRB_EX_UNSOL_EV (1<<4)
525
526/* retrieve RIRB entry - called from interrupt handler */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100527static void azx_update_rirb(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 unsigned int rp, wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800530 unsigned int addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 u32 res, res_ex;
532
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100533 wp = azx_readw(chip, RIRBWP);
534 if (wp == 0xffff) {
535 /* something wrong, controller likely turned to D3 */
536 return;
537 }
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (wp == chip->rirb.wp)
540 return;
541 chip->rirb.wp = wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 while (chip->rirb.rp != wp) {
544 chip->rirb.rp++;
545 chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
546
547 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
548 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
549 res = le32_to_cpu(chip->rirb.buf[rp]);
David Henningsson3d692452014-01-29 13:12:31 +0100550 addr = res_ex & 0xf;
551 if ((addr >= AZX_MAX_CODECS) || !(chip->codec_mask & (1 << addr))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100552 dev_err(chip->card->dev, "spurious response %#x:%#x, rp = %d, wp = %d",
553 res, res_ex,
554 chip->rirb.rp, wp);
David Henningsson3d692452014-01-29 13:12:31 +0100555 snd_BUG();
556 }
557 else if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 snd_hda_queue_unsol_event(chip->bus, res, res_ex);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800559 else if (chip->rirb.cmds[addr]) {
560 chip->rirb.res[addr] = res;
Takashi Iwai2add9b92008-03-18 09:47:06 +0100561 smp_wmb();
Wu Fengguangdeadff12009-08-01 18:45:16 +0800562 chip->rirb.cmds[addr]--;
Joe Perches3b70a672013-11-07 11:55:15 -0800563 } else if (printk_ratelimit()) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100564 dev_err(chip->card->dev, "spurious response %#x:%#x, last cmd=%#08x\n",
565 res, res_ex,
566 chip->last_cmd[addr]);
Joe Perches3b70a672013-11-07 11:55:15 -0800567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569}
570
571/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800572static unsigned int azx_rirb_get_response(struct hda_bus *bus,
573 unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100575 struct azx *chip = bus->private_data;
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200576 unsigned long timeout;
David Henningsson32cf4022012-05-04 11:05:55 +0200577 unsigned long loopcounter;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200578 int do_poll = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200580 again:
581 timeout = jiffies + msecs_to_jiffies(1000);
David Henningsson32cf4022012-05-04 11:05:55 +0200582
583 for (loopcounter = 0;; loopcounter++) {
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200584 if (chip->polling_mode || do_poll) {
Takashi Iwaie96224a2006-08-21 17:57:44 +0200585 spin_lock_irq(&chip->reg_lock);
586 azx_update_rirb(chip);
587 spin_unlock_irq(&chip->reg_lock);
588 }
Wu Fengguangdeadff12009-08-01 18:45:16 +0800589 if (!chip->rirb.cmds[addr]) {
Takashi Iwai2add9b92008-03-18 09:47:06 +0100590 smp_rmb();
Takashi Iwaib6132912009-03-24 07:36:09 +0100591 bus->rirb_error = 0;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200592
593 if (!do_poll)
594 chip->poll_count = 0;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800595 return chip->rirb.res[addr]; /* the last value */
Takashi Iwai2add9b92008-03-18 09:47:06 +0100596 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100597 if (time_after(jiffies, timeout))
598 break;
David Henningsson32cf4022012-05-04 11:05:55 +0200599 if (bus->needs_damn_long_delay || loopcounter > 3000)
Takashi Iwai52987652008-01-16 16:09:47 +0100600 msleep(2); /* temporary workaround */
601 else {
602 udelay(10);
603 cond_resched();
604 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100605 }
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200606
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200607 if (!bus->no_response_fallback)
608 return -1;
609
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200610 if (!chip->polling_mode && chip->poll_count < 2) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100611 dev_dbg(chip->card->dev,
612 "azx_get_response timeout, polling the codec once: last cmd=0x%08x\n",
613 chip->last_cmd[addr]);
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200614 do_poll = 1;
615 chip->poll_count++;
616 goto again;
617 }
618
619
Takashi Iwai23c4a882009-10-30 13:21:49 +0100620 if (!chip->polling_mode) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100621 dev_warn(chip->card->dev,
622 "azx_get_response timeout, switching to polling mode: last cmd=0x%08x\n",
623 chip->last_cmd[addr]);
Takashi Iwai23c4a882009-10-30 13:21:49 +0100624 chip->polling_mode = 1;
625 goto again;
626 }
627
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200628 if (chip->msi) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100629 dev_warn(chip->card->dev,
630 "No response from codec, disabling MSI: last cmd=0x%08x\n",
631 chip->last_cmd[addr]);
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200632 free_irq(chip->irq, chip);
633 chip->irq = -1;
634 pci_disable_msi(chip->pci);
635 chip->msi = 0;
Takashi Iwaib6132912009-03-24 07:36:09 +0100636 if (azx_acquire_irq(chip, 1) < 0) {
637 bus->rirb_error = 1;
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200638 return -1;
Takashi Iwaib6132912009-03-24 07:36:09 +0100639 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200640 goto again;
641 }
642
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +0100643 if (chip->probing) {
644 /* If this critical timeout happens during the codec probing
645 * phase, this is likely an access to a non-existing codec
646 * slot. Better to return an error and reset the system.
647 */
648 return -1;
649 }
650
Takashi Iwai8dd78332009-06-02 01:16:07 +0200651 /* a fatal communication error; need either to reset or to fallback
652 * to the single_cmd mode
653 */
Takashi Iwaib6132912009-03-24 07:36:09 +0100654 bus->rirb_error = 1;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200655 if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200656 bus->response_reset = 1;
657 return -1; /* give a chance to retry */
658 }
659
Takashi Iwai4e76a882014-02-25 12:21:03 +0100660 dev_err(chip->card->dev,
661 "azx_get_response timeout, switching to single_cmd mode: last cmd=0x%08x\n",
662 chip->last_cmd[addr]);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200663 chip->single_cmd = 1;
664 bus->response_reset = 0;
Takashi Iwai1a696972009-11-07 09:49:04 +0100665 /* release CORB/RIRB */
Takashi Iwai4fcd3922009-05-25 18:34:52 +0200666 azx_free_cmd_io(chip);
Takashi Iwai1a696972009-11-07 09:49:04 +0100667 /* disable unsolicited responses */
668 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200669 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/*
673 * Use the single immediate command instead of CORB/RIRB for simplicity
674 *
675 * Note: according to Intel, this is not preferred use. The command was
676 * intended for the BIOS only, and may get confused with unsolicited
677 * responses. So, we shouldn't use it for normal operation from the
678 * driver.
679 * I left the codes, however, for debugging/testing purposes.
680 */
681
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200682/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800683static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200684{
685 int timeout = 50;
686
687 while (timeout--) {
688 /* check IRV busy bit */
689 if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
690 /* reuse rirb.res as the response return value */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800691 chip->rirb.res[addr] = azx_readl(chip, IR);
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200692 return 0;
693 }
694 udelay(1);
695 }
696 if (printk_ratelimit())
Takashi Iwai4e76a882014-02-25 12:21:03 +0100697 dev_dbg(chip->card->dev, "get_response timeout: IRS=0x%x\n",
698 azx_readw(chip, IRS));
Wu Fengguangdeadff12009-08-01 18:45:16 +0800699 chip->rirb.res[addr] = -1;
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200700 return -EIO;
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100704static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100706 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800707 unsigned int addr = azx_command_addr(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int timeout = 50;
709
Takashi Iwai8dd78332009-06-02 01:16:07 +0200710 bus->rirb_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 while (timeout--) {
712 /* check ICB busy bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200713 if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* Clear IRV valid bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200715 azx_writew(chip, IRS, azx_readw(chip, IRS) |
716 ICH6_IRS_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 azx_writel(chip, IC, val);
Takashi Iwaid01ce992007-07-27 16:52:19 +0200718 azx_writew(chip, IRS, azx_readw(chip, IRS) |
719 ICH6_IRS_BUSY);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800720 return azx_single_wait_for_response(chip, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722 udelay(1);
723 }
Marc Boucher1cfd52b2008-01-22 15:29:26 +0100724 if (printk_ratelimit())
Takashi Iwai4e76a882014-02-25 12:21:03 +0100725 dev_dbg(chip->card->dev,
726 "send_cmd timeout: IRS=0x%x, val=0x%x\n",
727 azx_readw(chip, IRS), val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return -EIO;
729}
730
731/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800732static unsigned int azx_single_get_response(struct hda_bus *bus,
733 unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100735 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800736 return chip->rirb.res[addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Takashi Iwai111d3af2006-02-16 18:17:58 +0100739/*
740 * The below are the main callbacks from hda_codec.
741 *
742 * They are just the skeleton to call sub-callbacks according to the
743 * current setting of chip->single_cmd.
744 */
745
746/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100747static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
Takashi Iwai111d3af2006-02-16 18:17:58 +0100748{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100749 struct azx *chip = bus->private_data;
Takashi Iwai43bbb6c2007-07-06 20:22:05 +0200750
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200751 if (chip->disabled)
752 return 0;
Wu Fengguangfeb27342009-08-01 19:17:14 +0800753 chip->last_cmd[azx_command_addr(val)] = val;
Takashi Iwai111d3af2006-02-16 18:17:58 +0100754 if (chip->single_cmd)
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100755 return azx_single_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100756 else
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100757 return azx_corb_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100758}
759
760/* get a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800761static unsigned int azx_get_response(struct hda_bus *bus,
762 unsigned int addr)
Takashi Iwai111d3af2006-02-16 18:17:58 +0100763{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100764 struct azx *chip = bus->private_data;
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200765 if (chip->disabled)
766 return 0;
Takashi Iwai111d3af2006-02-16 18:17:58 +0100767 if (chip->single_cmd)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800768 return azx_single_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100769 else
Wu Fengguangdeadff12009-08-01 18:45:16 +0800770 return azx_rirb_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100771}
772
Takashi Iwai83012a72012-08-24 18:38:08 +0200773#ifdef CONFIG_PM
Takashi Iwai68467f52012-08-28 09:14:29 -0700774static void azx_power_notify(struct hda_bus *bus, bool power_up);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200775#endif
Takashi Iwai111d3af2006-02-16 18:17:58 +0100776
Takashi Iwai1d1a4562012-09-20 20:29:13 -0700777#ifdef CONFIG_SND_HDA_DSP_LOADER
778static int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
779 unsigned int byte_size,
780 struct snd_dma_buffer *bufp);
781static void azx_load_dsp_trigger(struct hda_bus *bus, bool start);
782static void azx_load_dsp_cleanup(struct hda_bus *bus,
783 struct snd_dma_buffer *dmab);
784#endif
785
Mengdong Lin3af3f352013-06-24 10:18:54 -0400786/* enter link reset */
Mengdong Lin7295b262013-06-25 05:58:49 -0400787static void azx_enter_link_reset(struct azx *chip)
Mengdong Lin3af3f352013-06-24 10:18:54 -0400788{
789 unsigned long timeout;
790
791 /* reset controller */
792 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
793
794 timeout = jiffies + msecs_to_jiffies(100);
795 while ((azx_readb(chip, GCTL) & ICH6_GCTL_RESET) &&
796 time_before(jiffies, timeout))
797 usleep_range(500, 1000);
798}
799
Mengdong Lin7295b262013-06-25 05:58:49 -0400800/* exit link reset */
801static void azx_exit_link_reset(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Mengdong Linfa348da2012-12-12 09:16:15 -0500803 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Mengdong Lin7295b262013-06-25 05:58:49 -0400805 azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
806
807 timeout = jiffies + msecs_to_jiffies(100);
808 while (!azx_readb(chip, GCTL) &&
809 time_before(jiffies, timeout))
810 usleep_range(500, 1000);
811}
812
813/* reset codec link */
814static int azx_reset(struct azx *chip, int full_reset)
815{
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100816 if (!full_reset)
817 goto __skip;
818
Danny Tholene8a7f132007-09-11 21:41:56 +0200819 /* clear STATESTS */
Wang Xingchaoda7db6a2013-07-22 03:19:18 -0400820 azx_writew(chip, STATESTS, STATESTS_INT_MASK);
Danny Tholene8a7f132007-09-11 21:41:56 +0200821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* reset controller */
Mengdong Lin7295b262013-06-25 05:58:49 -0400823 azx_enter_link_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* delay for >= 100us for codec PLL to settle per spec
826 * Rev 0.9 section 5.5.1
827 */
Mengdong Linfa348da2012-12-12 09:16:15 -0500828 usleep_range(500, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* Bring controller out of reset */
Mengdong Lin7295b262013-06-25 05:58:49 -0400831 azx_exit_link_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Pavel Machek927fc862006-08-31 17:03:43 +0200833 /* Brent Chartrand said to wait >= 540us for codecs to initialize */
Mengdong Linfa348da2012-12-12 09:16:15 -0500834 usleep_range(1000, 1200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100836 __skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* check to see if controller is ready */
Pavel Machek927fc862006-08-31 17:03:43 +0200838 if (!azx_readb(chip, GCTL)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100839 dev_dbg(chip->card->dev, "azx_reset: controller not ready!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -EBUSY;
841 }
842
Matt41e2fce2005-07-04 17:49:55 +0200843 /* Accept unsolicited responses */
Takashi Iwai1a696972009-11-07 09:49:04 +0100844 if (!chip->single_cmd)
845 azx_writel(chip, GCTL, azx_readl(chip, GCTL) |
846 ICH6_GCTL_UNSOL);
Matt41e2fce2005-07-04 17:49:55 +0200847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* detect codecs */
Pavel Machek927fc862006-08-31 17:03:43 +0200849 if (!chip->codec_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 chip->codec_mask = azx_readw(chip, STATESTS);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100851 dev_dbg(chip->card->dev, "codec_mask = 0x%x\n",
852 chip->codec_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
855 return 0;
856}
857
858
859/*
860 * Lowlevel interface
861 */
862
863/* enable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100864static void azx_int_enable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 /* enable controller CIE and GIE */
867 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
868 ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
869}
870
871/* disable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100872static void azx_int_disable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 int i;
875
876 /* disable interrupts in stream descriptor */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200877 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100878 struct azx_dev *azx_dev = &chip->azx_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 azx_sd_writeb(azx_dev, SD_CTL,
880 azx_sd_readb(azx_dev, SD_CTL) & ~SD_INT_MASK);
881 }
882
883 /* disable SIE for all streams */
884 azx_writeb(chip, INTCTL, 0);
885
886 /* disable controller CIE and GIE */
887 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
888 ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
889}
890
891/* clear interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100892static void azx_int_clear(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 int i;
895
896 /* clear stream status */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200897 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100898 struct azx_dev *azx_dev = &chip->azx_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
900 }
901
902 /* clear STATESTS */
Wang Xingchaoda7db6a2013-07-22 03:19:18 -0400903 azx_writew(chip, STATESTS, STATESTS_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* clear rirb status */
906 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
907
908 /* clear int status */
909 azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
910}
911
912/* start a stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100913static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Joseph Chan0e153472008-08-26 14:38:03 +0200915 /*
916 * Before stream start, initialize parameter
917 */
918 azx_dev->insufficient = 1;
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* enable SIE */
Wei Niccc5df02010-01-26 15:59:33 +0800921 azx_writel(chip, INTCTL,
922 azx_readl(chip, INTCTL) | (1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /* set DMA start and interrupt mask */
924 azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
925 SD_CTL_DMA_START | SD_INT_MASK);
926}
927
Takashi Iwai1dddab42009-03-18 15:15:37 +0100928/* stop DMA */
929static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) &
932 ~(SD_CTL_DMA_START | SD_INT_MASK));
933 azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
Takashi Iwai1dddab42009-03-18 15:15:37 +0100934}
935
936/* stop a stream */
937static void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
938{
939 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* disable SIE */
Wei Niccc5df02010-01-26 15:59:33 +0800941 azx_writel(chip, INTCTL,
942 azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945
946/*
Takashi Iwaicb53c622007-08-10 17:21:45 +0200947 * reset and start the controller registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100949static void azx_init_chip(struct azx *chip, int full_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Takashi Iwaicb53c622007-08-10 17:21:45 +0200951 if (chip->initialized)
952 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /* reset controller */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100955 azx_reset(chip, full_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /* initialize interrupts */
958 azx_int_clear(chip);
959 azx_int_enable(chip);
960
961 /* initialize the codec command I/O */
Takashi Iwai1a696972009-11-07 09:49:04 +0100962 if (!chip->single_cmd)
963 azx_init_cmd_io(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Takashi Iwai0be3b5d2005-09-05 17:11:40 +0200965 /* program the position buffer */
966 azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200967 azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr));
Frederick Lif5d40b32005-05-12 14:55:20 +0200968
Takashi Iwaicb53c622007-08-10 17:21:45 +0200969 chip->initialized = 1;
970}
971
972/*
973 * initialize the PCI registers
974 */
975/* update bits in a PCI register byte */
976static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
977 unsigned char mask, unsigned char val)
978{
979 unsigned char data;
980
981 pci_read_config_byte(pci, reg, &data);
982 data &= ~mask;
983 data |= (val & mask);
984 pci_write_config_byte(pci, reg, data);
985}
986
987static void azx_init_pci(struct azx *chip)
988{
989 /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
990 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
991 * Ensuring these bits are 0 clears playback static on some HD Audio
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +0100992 * codecs.
993 * The PCI register TCSEL is defined in the Intel manuals.
Takashi Iwaicb53c622007-08-10 17:21:45 +0200994 */
Linus Torvalds46f2cc82011-05-27 19:45:28 -0700995 if (!(chip->driver_caps & AZX_DCAPS_NO_TCSEL)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100996 dev_dbg(chip->card->dev, "Clearing TCSEL\n");
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +0100997 update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
Takashi Iwai9477c582011-05-25 09:11:37 +0200998 }
Takashi Iwaicb53c622007-08-10 17:21:45 +0200999
Takashi Iwai9477c582011-05-25 09:11:37 +02001000 /* For ATI SB450/600/700/800/900 and AMD Hudson azalia HD audio,
1001 * we need to enable snoop.
1002 */
1003 if (chip->driver_caps & AZX_DCAPS_ATI_SNOOP) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001004 dev_dbg(chip->card->dev, "Setting ATI snoop: %d\n",
1005 azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001006 update_pci_byte(chip->pci,
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001007 ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR, 0x07,
1008 azx_snoop(chip) ? ATI_SB450_HDAUDIO_ENABLE_SNOOP : 0);
Takashi Iwai9477c582011-05-25 09:11:37 +02001009 }
1010
1011 /* For NVIDIA HDA, enable snoop */
1012 if (chip->driver_caps & AZX_DCAPS_NVIDIA_SNOOP) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001013 dev_dbg(chip->card->dev, "Setting Nvidia snoop: %d\n",
1014 azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001015 update_pci_byte(chip->pci,
1016 NVIDIA_HDA_TRANSREG_ADDR,
1017 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
Peer Chen320dcc32008-08-20 16:43:24 -07001018 update_pci_byte(chip->pci,
1019 NVIDIA_HDA_ISTRM_COH,
1020 0x01, NVIDIA_HDA_ENABLE_COHBIT);
1021 update_pci_byte(chip->pci,
1022 NVIDIA_HDA_OSTRM_COH,
1023 0x01, NVIDIA_HDA_ENABLE_COHBIT);
Takashi Iwai9477c582011-05-25 09:11:37 +02001024 }
1025
1026 /* Enable SCH/PCH snoop if needed */
1027 if (chip->driver_caps & AZX_DCAPS_SCH_SNOOP) {
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001028 unsigned short snoop;
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001029 pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001030 if ((!azx_snoop(chip) && !(snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)) ||
1031 (azx_snoop(chip) && (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP))) {
1032 snoop &= ~INTEL_SCH_HDA_DEVC_NOSNOOP;
1033 if (!azx_snoop(chip))
1034 snoop |= INTEL_SCH_HDA_DEVC_NOSNOOP;
1035 pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC, snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001036 pci_read_config_word(chip->pci,
1037 INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001038 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001039 dev_dbg(chip->card->dev, "SCH snoop: %s\n",
1040 (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) ?
1041 "Disabled" : "Enabled");
Vinod Gda3fca22005-09-13 18:49:12 +02001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001046static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/*
1049 * interrupt handler
1050 */
David Howells7d12e782006-10-05 14:55:46 +01001051static irqreturn_t azx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001053 struct azx *chip = dev_id;
1054 struct azx_dev *azx_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 u32 status;
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001056 u8 sd_status;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001057 int i, ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001059#ifdef CONFIG_PM_RUNTIME
Dave Airlie246efa42013-07-29 15:19:29 +10001060 if (chip->driver_caps & AZX_DCAPS_PM_RUNTIME)
1061 if (chip->pci->dev.power.runtime_status != RPM_ACTIVE)
1062 return IRQ_NONE;
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001063#endif
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 spin_lock(&chip->reg_lock);
1066
Dan Carpenter60911062012-05-18 10:36:11 +03001067 if (chip->disabled) {
1068 spin_unlock(&chip->reg_lock);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001069 return IRQ_NONE;
Dan Carpenter60911062012-05-18 10:36:11 +03001070 }
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 status = azx_readl(chip, INTSTS);
Dave Airlie246efa42013-07-29 15:19:29 +10001073 if (status == 0 || status == 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 spin_unlock(&chip->reg_lock);
1075 return IRQ_NONE;
1076 }
1077
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001078 for (i = 0; i < chip->num_streams; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 azx_dev = &chip->azx_dev[i];
1080 if (status & azx_dev->sd_int_sta_mask) {
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001081 sd_status = azx_sd_readb(azx_dev, SD_STS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001083 if (!azx_dev->substream || !azx_dev->running ||
1084 !(sd_status & SD_INT_COMPLETE))
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001085 continue;
1086 /* check whether this IRQ is really acceptable */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001087 ok = azx_position_ok(chip, azx_dev);
1088 if (ok == 1) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001089 azx_dev->irq_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 spin_unlock(&chip->reg_lock);
1091 snd_pcm_period_elapsed(azx_dev->substream);
1092 spin_lock(&chip->reg_lock);
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001093 } else if (ok == 0 && chip->bus && chip->bus->workq) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001094 /* bogus IRQ, process it later */
1095 azx_dev->irq_pending = 1;
Takashi Iwai6acaed32009-01-12 10:09:24 +01001096 queue_work(chip->bus->workq,
1097 &chip->irq_pending_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099 }
1100 }
1101
1102 /* clear rirb int */
1103 status = azx_readb(chip, RIRBSTS);
1104 if (status & RIRB_INT_MASK) {
Takashi Iwai14d34f12010-10-21 09:03:25 +02001105 if (status & RIRB_INT_RESPONSE) {
Takashi Iwai9477c582011-05-25 09:11:37 +02001106 if (chip->driver_caps & AZX_DCAPS_RIRB_PRE_DELAY)
Takashi Iwai14d34f12010-10-21 09:03:25 +02001107 udelay(80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 azx_update_rirb(chip);
Takashi Iwai14d34f12010-10-21 09:03:25 +02001109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1111 }
1112
1113#if 0
1114 /* clear state status int */
Wang Xingchaoda7db6a2013-07-22 03:19:18 -04001115 if (azx_readw(chip, STATESTS) & 0x04)
1116 azx_writew(chip, STATESTS, 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117#endif
1118 spin_unlock(&chip->reg_lock);
1119
1120 return IRQ_HANDLED;
1121}
1122
1123
1124/*
Takashi Iwai675f25d2008-06-10 17:53:20 +02001125 * set up a BDL entry
1126 */
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001127static int setup_bdle(struct azx *chip,
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001128 struct snd_dma_buffer *dmab,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001129 struct azx_dev *azx_dev, u32 **bdlp,
1130 int ofs, int size, int with_ioc)
1131{
Takashi Iwai675f25d2008-06-10 17:53:20 +02001132 u32 *bdl = *bdlp;
1133
1134 while (size > 0) {
1135 dma_addr_t addr;
1136 int chunk;
1137
1138 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
1139 return -EINVAL;
1140
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001141 addr = snd_sgbuf_get_addr(dmab, ofs);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001142 /* program the address field of the BDL entry */
1143 bdl[0] = cpu_to_le32((u32)addr);
Takashi Iwai766979e2008-06-13 20:53:56 +02001144 bdl[1] = cpu_to_le32(upper_32_bits(addr));
Takashi Iwai675f25d2008-06-10 17:53:20 +02001145 /* program the size field of the BDL entry */
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001146 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001147 /* one BDLE cannot cross 4K boundary on CTHDA chips */
1148 if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY) {
1149 u32 remain = 0x1000 - (ofs & 0xfff);
1150 if (chunk > remain)
1151 chunk = remain;
1152 }
Takashi Iwai675f25d2008-06-10 17:53:20 +02001153 bdl[2] = cpu_to_le32(chunk);
1154 /* program the IOC to enable interrupt
1155 * only when the whole fragment is processed
1156 */
1157 size -= chunk;
1158 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
1159 bdl += 4;
1160 azx_dev->frags++;
1161 ofs += chunk;
1162 }
1163 *bdlp = bdl;
1164 return ofs;
1165}
1166
1167/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 * set up BDL entries
1169 */
Takashi Iwai555e2192008-06-10 17:53:34 +02001170static int azx_setup_periods(struct azx *chip,
1171 struct snd_pcm_substream *substream,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001172 struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001174 u32 *bdl;
1175 int i, ofs, periods, period_bytes;
Takashi Iwai555e2192008-06-10 17:53:34 +02001176 int pos_adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* reset BDL address */
1179 azx_sd_writel(azx_dev, SD_BDLPL, 0);
1180 azx_sd_writel(azx_dev, SD_BDLPU, 0);
1181
Takashi Iwai97b71c92009-03-18 15:09:13 +01001182 period_bytes = azx_dev->period_bytes;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001183 periods = azx_dev->bufsize / period_bytes;
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /* program the initial BDL entries */
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001186 bdl = (u32 *)azx_dev->bdl.area;
1187 ofs = 0;
1188 azx_dev->frags = 0;
Takashi Iwai555e2192008-06-10 17:53:34 +02001189 pos_adj = bdl_pos_adj[chip->dev_index];
Takashi Iwai915bf292012-09-11 15:19:10 +02001190 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
Takashi Iwai675f25d2008-06-10 17:53:20 +02001191 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001192 int pos_align = pos_adj;
Takashi Iwai555e2192008-06-10 17:53:34 +02001193 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001194 if (!pos_adj)
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001195 pos_adj = pos_align;
1196 else
1197 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
1198 pos_align;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001199 pos_adj = frames_to_bytes(runtime, pos_adj);
1200 if (pos_adj >= period_bytes) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001201 dev_warn(chip->card->dev,"Too big adjustment %d\n",
1202 bdl_pos_adj[chip->dev_index]);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001203 pos_adj = 0;
1204 } else {
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001205 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1206 azx_dev,
Takashi Iwai915bf292012-09-11 15:19:10 +02001207 &bdl, ofs, pos_adj, true);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001208 if (ofs < 0)
1209 goto error;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001210 }
Takashi Iwai555e2192008-06-10 17:53:34 +02001211 } else
1212 pos_adj = 0;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001213 for (i = 0; i < periods; i++) {
1214 if (i == periods - 1 && pos_adj)
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001215 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1216 azx_dev, &bdl, ofs,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001217 period_bytes - pos_adj, 0);
1218 else
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001219 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1220 azx_dev, &bdl, ofs,
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001221 period_bytes,
Takashi Iwai915bf292012-09-11 15:19:10 +02001222 !azx_dev->no_period_wakeup);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001223 if (ofs < 0)
1224 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001226 return 0;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001227
1228 error:
Takashi Iwai4e76a882014-02-25 12:21:03 +01001229 dev_err(chip->card->dev, "Too many BDL entries: buffer=%d, period=%d\n",
1230 azx_dev->bufsize, period_bytes);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001231 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Takashi Iwai1dddab42009-03-18 15:15:37 +01001234/* reset stream */
1235static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 unsigned char val;
1238 int timeout;
1239
Takashi Iwai1dddab42009-03-18 15:15:37 +01001240 azx_stream_clear(chip, azx_dev);
1241
Takashi Iwaid01ce992007-07-27 16:52:19 +02001242 azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
1243 SD_CTL_STREAM_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 udelay(3);
1245 timeout = 300;
1246 while (!((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
1247 --timeout)
1248 ;
1249 val &= ~SD_CTL_STREAM_RESET;
1250 azx_sd_writeb(azx_dev, SD_CTL, val);
1251 udelay(3);
1252
1253 timeout = 300;
1254 /* waiting for hardware to report that the stream is out of reset */
1255 while (((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
1256 --timeout)
1257 ;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001258
1259 /* reset first position - may not be synced with hw at this time */
1260 *azx_dev->posbuf = 0;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Takashi Iwai1dddab42009-03-18 15:15:37 +01001263/*
1264 * set up the SD for streaming
1265 */
1266static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
1267{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001268 unsigned int val;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001269 /* make sure the run bit is zero for SD */
1270 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /* program the stream_tag */
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001272 val = azx_sd_readl(azx_dev, SD_CTL);
1273 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
1274 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
1275 if (!azx_snoop(chip))
1276 val |= SD_CTL_TRAFFIC_PRIO;
1277 azx_sd_writel(azx_dev, SD_CTL, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /* program the length of samples in cyclic buffer */
1280 azx_sd_writel(azx_dev, SD_CBL, azx_dev->bufsize);
1281
1282 /* program the stream format */
1283 /* this value needs to be the same as the one programmed */
1284 azx_sd_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
1285
1286 /* program the stream LVI (last valid index) of the BDL */
1287 azx_sd_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
1288
1289 /* program the BDL address */
1290 /* lower BDL address */
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001291 azx_sd_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /* upper BDL address */
Takashi Iwai766979e2008-06-13 20:53:56 +02001293 azx_sd_writel(azx_dev, SD_BDLPU, upper_32_bits(azx_dev->bdl.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02001295 /* enable the position buffer */
David Henningsson4cb36312010-09-30 10:12:50 +02001296 if (chip->position_fix[0] != POS_FIX_LPIB ||
1297 chip->position_fix[1] != POS_FIX_LPIB) {
Takashi Iwaiee9d6b92008-03-14 15:52:20 +01001298 if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
1299 azx_writel(chip, DPLBASE,
1300 (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
1301 }
Takashi Iwaic74db862005-05-12 14:26:27 +02001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /* set the interrupt enable bits in the descriptor control register */
Takashi Iwaid01ce992007-07-27 16:52:19 +02001304 azx_sd_writel(azx_dev, SD_CTL,
1305 azx_sd_readl(azx_dev, SD_CTL) | SD_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 return 0;
1308}
1309
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001310/*
1311 * Probe the given codec address
1312 */
1313static int probe_codec(struct azx *chip, int addr)
1314{
1315 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
1316 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
1317 unsigned int res;
1318
Wu Fengguanga678cde2009-08-01 18:46:46 +08001319 mutex_lock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001320 chip->probing = 1;
1321 azx_send_cmd(chip->bus, cmd);
Wu Fengguangdeadff12009-08-01 18:45:16 +08001322 res = azx_get_response(chip->bus, addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001323 chip->probing = 0;
Wu Fengguanga678cde2009-08-01 18:46:46 +08001324 mutex_unlock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001325 if (res == -1)
1326 return -EIO;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001327 dev_dbg(chip->card->dev, "codec #%d probed OK\n", addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001328 return 0;
1329}
1330
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001331static int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
1332 struct hda_pcm *cpcm);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001333static void azx_stop_chip(struct azx *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Takashi Iwai8dd78332009-06-02 01:16:07 +02001335static void azx_bus_reset(struct hda_bus *bus)
1336{
1337 struct azx *chip = bus->private_data;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001338
1339 bus->in_reset = 1;
1340 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001341 azx_init_chip(chip, 1);
Alexander Beregalov65f75982009-06-04 13:46:16 +04001342#ifdef CONFIG_PM
Takashi Iwai8dd78332009-06-02 01:16:07 +02001343 if (chip->initialized) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01001344 struct azx_pcm *p;
1345 list_for_each_entry(p, &chip->pcm_list, list)
1346 snd_pcm_suspend_all(p->pcm);
Takashi Iwai8dd78332009-06-02 01:16:07 +02001347 snd_hda_suspend(chip->bus);
1348 snd_hda_resume(chip->bus);
1349 }
Alexander Beregalov65f75982009-06-04 13:46:16 +04001350#endif
Takashi Iwai8dd78332009-06-02 01:16:07 +02001351 bus->in_reset = 0;
1352}
1353
David Henningsson26a6cb62012-10-09 15:04:21 +02001354static int get_jackpoll_interval(struct azx *chip)
1355{
1356 int i = jackpoll_ms[chip->dev_index];
1357 unsigned int j;
1358 if (i == 0)
1359 return 0;
1360 if (i < 50 || i > 60000)
1361 j = 0;
1362 else
1363 j = msecs_to_jiffies(i);
1364 if (j == 0)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001365 dev_warn(chip->card->dev,
1366 "jackpoll_ms value out of range: %d\n", i);
David Henningsson26a6cb62012-10-09 15:04:21 +02001367 return j;
1368}
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370/*
1371 * Codec initialization
1372 */
1373
Takashi Iwai2f5983f2008-09-03 16:00:44 +02001374/* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01001375static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] = {
Wei Ni7445dfc2010-03-03 15:05:53 +08001376 [AZX_DRIVER_NVIDIA] = 8,
Kailang Yangf2690022008-05-27 11:44:55 +02001377 [AZX_DRIVER_TERA] = 1,
Takashi Iwaia9995a32007-03-12 21:30:46 +01001378};
1379
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01001380static int azx_codec_create(struct azx *chip, const char *model)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct hda_bus_template bus_temp;
Takashi Iwai34c25352008-10-28 11:38:58 +01001383 int c, codecs, err;
1384 int max_slots;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 memset(&bus_temp, 0, sizeof(bus_temp));
1387 bus_temp.private_data = chip;
1388 bus_temp.modelname = model;
1389 bus_temp.pci = chip->pci;
Takashi Iwai111d3af2006-02-16 18:17:58 +01001390 bus_temp.ops.command = azx_send_cmd;
1391 bus_temp.ops.get_response = azx_get_response;
Takashi Iwai176d5332008-07-30 15:01:44 +02001392 bus_temp.ops.attach_pcm = azx_attach_pcm_stream;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001393 bus_temp.ops.bus_reset = azx_bus_reset;
Takashi Iwai83012a72012-08-24 18:38:08 +02001394#ifdef CONFIG_PM
Takashi Iwai11cd41b2008-11-28 07:22:18 +01001395 bus_temp.power_save = &power_save;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001396 bus_temp.ops.pm_notify = azx_power_notify;
1397#endif
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001398#ifdef CONFIG_SND_HDA_DSP_LOADER
1399 bus_temp.ops.load_dsp_prepare = azx_load_dsp_prepare;
1400 bus_temp.ops.load_dsp_trigger = azx_load_dsp_trigger;
1401 bus_temp.ops.load_dsp_cleanup = azx_load_dsp_cleanup;
1402#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Takashi Iwaid01ce992007-07-27 16:52:19 +02001404 err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus);
1405 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return err;
1407
Takashi Iwai9477c582011-05-25 09:11:37 +02001408 if (chip->driver_caps & AZX_DCAPS_RIRB_DELAY) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001409 dev_dbg(chip->card->dev, "Enable delay in RIRB handling\n");
Wei Nidc9c8e22008-09-26 13:55:56 +08001410 chip->bus->needs_damn_long_delay = 1;
Takashi Iwai9477c582011-05-25 09:11:37 +02001411 }
Wei Nidc9c8e22008-09-26 13:55:56 +08001412
Takashi Iwai34c25352008-10-28 11:38:58 +01001413 codecs = 0;
Takashi Iwai2f5983f2008-09-03 16:00:44 +02001414 max_slots = azx_max_codecs[chip->driver_type];
1415 if (!max_slots)
Wei Ni7445dfc2010-03-03 15:05:53 +08001416 max_slots = AZX_DEFAULT_CODECS;
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001417
1418 /* First try to probe all given codec slots */
1419 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001420 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001421 if (probe_codec(chip, c) < 0) {
1422 /* Some BIOSen give you wrong codec addresses
1423 * that don't exist
1424 */
Takashi Iwai4e76a882014-02-25 12:21:03 +01001425 dev_warn(chip->card->dev,
1426 "Codec #%d probe error; disabling it...\n", c);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001427 chip->codec_mask &= ~(1 << c);
1428 /* More badly, accessing to a non-existing
1429 * codec often screws up the controller chip,
Paul Menzel24481582010-02-08 20:37:26 +01001430 * and disturbs the further communications.
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001431 * Thus if an error occurs during probing,
1432 * better to reset the controller chip to
1433 * get back to the sanity state.
1434 */
1435 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001436 azx_init_chip(chip, 1);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001437 }
1438 }
1439 }
1440
Takashi Iwaid507cd62011-04-26 15:25:02 +02001441 /* AMD chipsets often cause the communication stalls upon certain
1442 * sequence like the pin-detection. It seems that forcing the synced
1443 * access works around the stall. Grrr...
1444 */
Takashi Iwai9477c582011-05-25 09:11:37 +02001445 if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001446 dev_dbg(chip->card->dev, "Enable sync_write for stable communication\n");
Takashi Iwaid507cd62011-04-26 15:25:02 +02001447 chip->bus->sync_write = 1;
1448 chip->bus->allow_bus_reset = 1;
1449 }
1450
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001451 /* Then create codec instances */
Takashi Iwai34c25352008-10-28 11:38:58 +01001452 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001453 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwaibccad142007-04-24 12:23:53 +02001454 struct hda_codec *codec;
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001455 err = snd_hda_codec_new(chip->bus, c, &codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (err < 0)
1457 continue;
David Henningsson26a6cb62012-10-09 15:04:21 +02001458 codec->jackpoll_interval = get_jackpoll_interval(chip);
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01001459 codec->beep_mode = chip->beep_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 codecs++;
Takashi Iwai19a982b2007-03-21 15:14:35 +01001461 }
1462 }
1463 if (!codecs) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001464 dev_err(chip->card->dev, "no codecs initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return -ENXIO;
1466 }
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001467 return 0;
1468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001470/* configure each codec instance */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001471static int azx_codec_configure(struct azx *chip)
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001472{
1473 struct hda_codec *codec;
1474 list_for_each_entry(codec, &chip->bus->codec_list, list) {
1475 snd_hda_codec_configure(codec);
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478}
1479
1480
1481/*
1482 * PCM support
1483 */
1484
1485/* assign a stream for the PCM */
Wu Fengguangef18bed2009-12-25 13:14:27 +08001486static inline struct azx_dev *
1487azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001489 int dev, i, nums;
Wu Fengguangef18bed2009-12-25 13:14:27 +08001490 struct azx_dev *res = NULL;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001491 /* make a non-zero unique key for the substream */
1492 int key = (substream->pcm->device << 16) | (substream->number << 2) |
1493 (substream->stream + 1);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001494
1495 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001496 dev = chip->playback_index_offset;
1497 nums = chip->playback_streams;
1498 } else {
1499 dev = chip->capture_index_offset;
1500 nums = chip->capture_streams;
1501 }
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001502 for (i = 0; i < nums; i++, dev++) {
1503 struct azx_dev *azx_dev = &chip->azx_dev[dev];
1504 dsp_lock(azx_dev);
1505 if (!azx_dev->opened && !dsp_is_locked(azx_dev)) {
1506 res = azx_dev;
1507 if (res->assigned_key == key) {
1508 res->opened = 1;
1509 res->assigned_key = key;
1510 dsp_unlock(azx_dev);
1511 return azx_dev;
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001514 dsp_unlock(azx_dev);
1515 }
Wu Fengguangef18bed2009-12-25 13:14:27 +08001516 if (res) {
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001517 dsp_lock(res);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001518 res->opened = 1;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001519 res->assigned_key = key;
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001520 dsp_unlock(res);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001521 }
1522 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525/* release the assigned stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001526static inline void azx_release_device(struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 azx_dev->opened = 0;
1529}
1530
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001531static cycle_t azx_cc_read(const struct cyclecounter *cc)
1532{
1533 struct azx_dev *azx_dev = container_of(cc, struct azx_dev, azx_cc);
1534 struct snd_pcm_substream *substream = azx_dev->substream;
1535 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1536 struct azx *chip = apcm->chip;
1537
1538 return azx_readl(chip, WALLCLK);
1539}
1540
1541static void azx_timecounter_init(struct snd_pcm_substream *substream,
1542 bool force, cycle_t last)
1543{
1544 struct azx_dev *azx_dev = get_azx_dev(substream);
1545 struct timecounter *tc = &azx_dev->azx_tc;
1546 struct cyclecounter *cc = &azx_dev->azx_cc;
1547 u64 nsec;
1548
1549 cc->read = azx_cc_read;
1550 cc->mask = CLOCKSOURCE_MASK(32);
1551
1552 /*
1553 * Converting from 24 MHz to ns means applying a 125/3 factor.
1554 * To avoid any saturation issues in intermediate operations,
1555 * the 125 factor is applied first. The division is applied
1556 * last after reading the timecounter value.
1557 * Applying the 1/3 factor as part of the multiplication
1558 * requires at least 20 bits for a decent precision, however
1559 * overflows occur after about 4 hours or less, not a option.
1560 */
1561
1562 cc->mult = 125; /* saturation after 195 years */
1563 cc->shift = 0;
1564
1565 nsec = 0; /* audio time is elapsed time since trigger */
1566 timecounter_init(tc, cc, nsec);
1567 if (force)
1568 /*
1569 * force timecounter to use predefined value,
1570 * used for synchronized starts
1571 */
1572 tc->cycle_last = last;
1573}
1574
Dylan Reidae03bbb2013-04-15 11:57:05 -07001575static u64 azx_adjust_codec_delay(struct snd_pcm_substream *substream,
Dylan Reid78daea22013-04-08 18:20:30 -07001576 u64 nsec)
1577{
1578 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1579 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
1580 u64 codec_frames, codec_nsecs;
1581
1582 if (!hinfo->ops.get_delay)
1583 return nsec;
1584
1585 codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
1586 codec_nsecs = div_u64(codec_frames * 1000000000LL,
1587 substream->runtime->rate);
1588
Dylan Reidae03bbb2013-04-15 11:57:05 -07001589 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1590 return nsec + codec_nsecs;
1591
Dylan Reid78daea22013-04-08 18:20:30 -07001592 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
1593}
1594
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001595static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
1596 struct timespec *ts)
1597{
1598 struct azx_dev *azx_dev = get_azx_dev(substream);
1599 u64 nsec;
1600
1601 nsec = timecounter_read(&azx_dev->azx_tc);
1602 nsec = div_u64(nsec, 3); /* can be optimized */
Dylan Reidae03bbb2013-04-15 11:57:05 -07001603 nsec = azx_adjust_codec_delay(substream, nsec);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001604
1605 *ts = ns_to_timespec(nsec);
1606
1607 return 0;
1608}
1609
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001610static struct snd_pcm_hardware azx_pcm_hw = {
Takashi Iwaid01ce992007-07-27 16:52:19 +02001611 .info = (SNDRV_PCM_INFO_MMAP |
1612 SNDRV_PCM_INFO_INTERLEAVED |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1614 SNDRV_PCM_INFO_MMAP_VALID |
Pavel Machek927fc862006-08-31 17:03:43 +02001615 /* No full-resume yet implemented */
1616 /* SNDRV_PCM_INFO_RESUME |*/
Takashi Iwai850f0e52008-03-18 17:11:05 +01001617 SNDRV_PCM_INFO_PAUSE |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001618 SNDRV_PCM_INFO_SYNC_START |
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001619 SNDRV_PCM_INFO_HAS_WALL_CLOCK |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001620 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1622 .rates = SNDRV_PCM_RATE_48000,
1623 .rate_min = 48000,
1624 .rate_max = 48000,
1625 .channels_min = 2,
1626 .channels_max = 2,
1627 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
1628 .period_bytes_min = 128,
1629 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
1630 .periods_min = 2,
1631 .periods_max = AZX_MAX_FRAG,
1632 .fifo_size = 0,
1633};
1634
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001635static int azx_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1638 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001639 struct azx *chip = apcm->chip;
1640 struct azx_dev *azx_dev;
1641 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 unsigned long flags;
1643 int err;
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001644 int buff_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Ingo Molnar62932df2006-01-16 16:34:20 +01001646 mutex_lock(&chip->open_mutex);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001647 azx_dev = azx_assign_device(chip, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (azx_dev == NULL) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001649 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return -EBUSY;
1651 }
1652 runtime->hw = azx_pcm_hw;
1653 runtime->hw.channels_min = hinfo->channels_min;
1654 runtime->hw.channels_max = hinfo->channels_max;
1655 runtime->hw.formats = hinfo->formats;
1656 runtime->hw.rates = hinfo->rates;
1657 snd_pcm_limit_hw_rates(runtime);
1658 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001659
1660 /* avoid wrap-around with wall-clock */
1661 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1662 20,
1663 178000000);
1664
Takashi Iwai52409aa2012-01-23 17:10:24 +01001665 if (chip->align_buffer_size)
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001666 /* constrain buffer sizes to be multiple of 128
1667 bytes. This is more efficient in terms of memory
1668 access but isn't required by the HDA spec and
1669 prevents users from specifying exact period/buffer
1670 sizes. For example for 44.1kHz, a period size set
1671 to 20ms will be rounded to 19.59ms. */
1672 buff_step = 128;
1673 else
1674 /* Don't enforce steps on buffer sizes, still need to
1675 be multiple of 4 bytes (HDA spec). Tested on Intel
1676 HDA controllers, may not work on all devices where
1677 option needs to be disabled */
1678 buff_step = 4;
1679
Joachim Deguara5f1545b2007-03-16 15:01:36 +01001680 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001681 buff_step);
Joachim Deguara5f1545b2007-03-16 15:01:36 +01001682 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001683 buff_step);
Dylan Reidb4a91cf2012-06-15 19:36:23 -07001684 snd_hda_power_up_d3wait(apcm->codec);
Takashi Iwaid01ce992007-07-27 16:52:19 +02001685 err = hinfo->ops.open(hinfo, apcm->codec, substream);
1686 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 azx_release_device(azx_dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001688 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01001689 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return err;
1691 }
Takashi Iwai70d321e2009-07-03 23:06:45 +02001692 snd_pcm_limit_hw_rates(runtime);
Takashi Iwaiaba66532009-07-05 11:44:46 +02001693 /* sanity check */
1694 if (snd_BUG_ON(!runtime->hw.channels_min) ||
1695 snd_BUG_ON(!runtime->hw.channels_max) ||
1696 snd_BUG_ON(!runtime->hw.formats) ||
1697 snd_BUG_ON(!runtime->hw.rates)) {
1698 azx_release_device(azx_dev);
1699 hinfo->ops.close(hinfo, apcm->codec, substream);
1700 snd_hda_power_down(apcm->codec);
1701 mutex_unlock(&chip->open_mutex);
1702 return -EINVAL;
1703 }
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001704
1705 /* disable WALLCLOCK timestamps for capture streams
1706 until we figure out how to handle digital inputs */
1707 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1708 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK;
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 spin_lock_irqsave(&chip->reg_lock, flags);
1711 azx_dev->substream = substream;
1712 azx_dev->running = 0;
1713 spin_unlock_irqrestore(&chip->reg_lock, flags);
1714
1715 runtime->private_data = azx_dev;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001716 snd_pcm_set_sync(substream);
Ingo Molnar62932df2006-01-16 16:34:20 +01001717 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return 0;
1719}
1720
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001721static int azx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1724 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001725 struct azx *chip = apcm->chip;
1726 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 unsigned long flags;
1728
Ingo Molnar62932df2006-01-16 16:34:20 +01001729 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 spin_lock_irqsave(&chip->reg_lock, flags);
1731 azx_dev->substream = NULL;
1732 azx_dev->running = 0;
1733 spin_unlock_irqrestore(&chip->reg_lock, flags);
1734 azx_release_device(azx_dev);
1735 hinfo->ops.close(hinfo, apcm->codec, substream);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001736 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01001737 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return 0;
1739}
1740
Takashi Iwaid01ce992007-07-27 16:52:19 +02001741static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
1742 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001744 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1745 struct azx *chip = apcm->chip;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001746 struct azx_dev *azx_dev = get_azx_dev(substream);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001747 int ret;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001748
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001749 dsp_lock(azx_dev);
1750 if (dsp_is_locked(azx_dev)) {
1751 ret = -EBUSY;
1752 goto unlock;
1753 }
1754
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +01001755 mark_runtime_wc(chip, azx_dev, substream, false);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001756 azx_dev->bufsize = 0;
1757 azx_dev->period_bytes = 0;
1758 azx_dev->format_val = 0;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001759 ret = snd_pcm_lib_malloc_pages(substream,
Takashi Iwaid01ce992007-07-27 16:52:19 +02001760 params_buffer_bytes(hw_params));
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001761 if (ret < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001762 goto unlock;
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +01001763 mark_runtime_wc(chip, azx_dev, substream, true);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001764 unlock:
1765 dsp_unlock(azx_dev);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001766 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001769static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001772 struct azx_dev *azx_dev = get_azx_dev(substream);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001773 struct azx *chip = apcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
1775
1776 /* reset BDL address */
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001777 dsp_lock(azx_dev);
1778 if (!dsp_is_locked(azx_dev)) {
1779 azx_sd_writel(azx_dev, SD_BDLPL, 0);
1780 azx_sd_writel(azx_dev, SD_BDLPU, 0);
1781 azx_sd_writel(azx_dev, SD_CTL, 0);
1782 azx_dev->bufsize = 0;
1783 azx_dev->period_bytes = 0;
1784 azx_dev->format_val = 0;
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Takashi Iwaieb541332010-08-06 13:48:11 +02001787 snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +01001789 mark_runtime_wc(chip, azx_dev, substream, false);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001790 azx_dev->prepared = 0;
1791 dsp_unlock(azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return snd_pcm_lib_free_pages(substream);
1793}
1794
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001795static int azx_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001798 struct azx *chip = apcm->chip;
1799 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001801 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001802 unsigned int bufsize, period_bytes, format_val, stream_tag;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001803 int err;
Stephen Warren7c9359762011-06-01 11:14:17 -06001804 struct hda_spdif_out *spdif =
1805 snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid);
1806 unsigned short ctls = spdif ? spdif->ctls : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001808 dsp_lock(azx_dev);
1809 if (dsp_is_locked(azx_dev)) {
1810 err = -EBUSY;
1811 goto unlock;
1812 }
1813
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001814 azx_stream_reset(chip, azx_dev);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001815 format_val = snd_hda_calc_stream_format(runtime->rate,
1816 runtime->channels,
1817 runtime->format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03001818 hinfo->maxbps,
Stephen Warren7c9359762011-06-01 11:14:17 -06001819 ctls);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001820 if (!format_val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001821 dev_err(chip->card->dev,
1822 "invalid format_val, rate=%d, ch=%d, format=%d\n",
1823 runtime->rate, runtime->channels, runtime->format);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001824 err = -EINVAL;
1825 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827
Takashi Iwai97b71c92009-03-18 15:09:13 +01001828 bufsize = snd_pcm_lib_buffer_bytes(substream);
1829 period_bytes = snd_pcm_lib_period_bytes(substream);
1830
Takashi Iwai4e76a882014-02-25 12:21:03 +01001831 dev_dbg(chip->card->dev, "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
1832 bufsize, format_val);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001833
1834 if (bufsize != azx_dev->bufsize ||
1835 period_bytes != azx_dev->period_bytes ||
Takashi Iwai915bf292012-09-11 15:19:10 +02001836 format_val != azx_dev->format_val ||
1837 runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
Takashi Iwai97b71c92009-03-18 15:09:13 +01001838 azx_dev->bufsize = bufsize;
1839 azx_dev->period_bytes = period_bytes;
1840 azx_dev->format_val = format_val;
Takashi Iwai915bf292012-09-11 15:19:10 +02001841 azx_dev->no_period_wakeup = runtime->no_period_wakeup;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001842 err = azx_setup_periods(chip, substream, azx_dev);
1843 if (err < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001844 goto unlock;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001845 }
1846
Takashi Iwaie8648e52013-12-06 17:15:01 +01001847 /* when LPIB delay correction gives a small negative value,
1848 * we ignore it; currently set the threshold statically to
1849 * 64 frames
1850 */
1851 if (runtime->period_size > 64)
1852 azx_dev->delay_negative_threshold = -frames_to_bytes(runtime, 64);
1853 else
1854 azx_dev->delay_negative_threshold = 0;
1855
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001856 /* wallclk has 24Mhz clock source */
1857 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
1858 runtime->rate) * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 azx_setup_controller(chip, azx_dev);
1860 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1861 azx_dev->fifo_size = azx_sd_readw(azx_dev, SD_FIFOSIZE) + 1;
1862 else
1863 azx_dev->fifo_size = 0;
1864
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001865 stream_tag = azx_dev->stream_tag;
1866 /* CA-IBG chips need the playback stream starting from 1 */
Takashi Iwai9477c582011-05-25 09:11:37 +02001867 if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001868 stream_tag > chip->capture_streams)
1869 stream_tag -= chip->capture_streams;
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001870 err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
Takashi Iwaieb541332010-08-06 13:48:11 +02001871 azx_dev->format_val, substream);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001872
1873 unlock:
1874 if (!err)
1875 azx_dev->prepared = 1;
1876 dsp_unlock(azx_dev);
1877 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001880static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001883 struct azx *chip = apcm->chip;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001884 struct azx_dev *azx_dev;
1885 struct snd_pcm_substream *s;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001886 int rstart = 0, start, nsync = 0, sbits = 0;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001887 int nwait, timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Takashi Iwai1a8506d2012-10-16 15:10:08 +02001889 azx_dev = get_azx_dev(substream);
1890 trace_azx_pcm_trigger(chip, azx_dev, cmd);
1891
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001892 if (dsp_is_locked(azx_dev) || !azx_dev->prepared)
1893 return -EPIPE;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 switch (cmd) {
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001896 case SNDRV_PCM_TRIGGER_START:
1897 rstart = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1899 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai850f0e52008-03-18 17:11:05 +01001900 start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 break;
1902 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jaroslav Kysela47123192005-08-15 20:53:07 +02001903 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai850f0e52008-03-18 17:11:05 +01001905 start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 break;
1907 default:
Takashi Iwai850f0e52008-03-18 17:11:05 +01001908 return -EINVAL;
1909 }
1910
1911 snd_pcm_group_for_each_entry(s, substream) {
1912 if (s->pcm->card != substream->pcm->card)
1913 continue;
1914 azx_dev = get_azx_dev(s);
1915 sbits |= 1 << azx_dev->index;
1916 nsync++;
1917 snd_pcm_trigger_done(s, substream);
1918 }
1919
1920 spin_lock(&chip->reg_lock);
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05001921
1922 /* first, set SYNC bits of corresponding streams */
1923 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
1924 azx_writel(chip, OLD_SSYNC,
1925 azx_readl(chip, OLD_SSYNC) | sbits);
1926 else
1927 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) | sbits);
1928
Takashi Iwai850f0e52008-03-18 17:11:05 +01001929 snd_pcm_group_for_each_entry(s, substream) {
1930 if (s->pcm->card != substream->pcm->card)
1931 continue;
1932 azx_dev = get_azx_dev(s);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001933 if (start) {
1934 azx_dev->start_wallclk = azx_readl(chip, WALLCLK);
1935 if (!rstart)
1936 azx_dev->start_wallclk -=
1937 azx_dev->period_wallclk;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001938 azx_stream_start(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001939 } else {
Takashi Iwai850f0e52008-03-18 17:11:05 +01001940 azx_stream_stop(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001941 }
Takashi Iwai850f0e52008-03-18 17:11:05 +01001942 azx_dev->running = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01001945 if (start) {
Takashi Iwai850f0e52008-03-18 17:11:05 +01001946 /* wait until all FIFOs get ready */
1947 for (timeout = 5000; timeout; timeout--) {
1948 nwait = 0;
1949 snd_pcm_group_for_each_entry(s, substream) {
1950 if (s->pcm->card != substream->pcm->card)
1951 continue;
1952 azx_dev = get_azx_dev(s);
1953 if (!(azx_sd_readb(azx_dev, SD_STS) &
1954 SD_STS_FIFO_READY))
1955 nwait++;
1956 }
1957 if (!nwait)
1958 break;
1959 cpu_relax();
1960 }
1961 } else {
1962 /* wait until all RUN bits are cleared */
1963 for (timeout = 5000; timeout; timeout--) {
1964 nwait = 0;
1965 snd_pcm_group_for_each_entry(s, substream) {
1966 if (s->pcm->card != substream->pcm->card)
1967 continue;
1968 azx_dev = get_azx_dev(s);
1969 if (azx_sd_readb(azx_dev, SD_CTL) &
1970 SD_CTL_DMA_START)
1971 nwait++;
1972 }
1973 if (!nwait)
1974 break;
1975 cpu_relax();
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05001978 spin_lock(&chip->reg_lock);
1979 /* reset SYNC bits */
1980 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
1981 azx_writel(chip, OLD_SSYNC,
1982 azx_readl(chip, OLD_SSYNC) & ~sbits);
1983 else
1984 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) & ~sbits);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001985 if (start) {
1986 azx_timecounter_init(substream, 0, 0);
1987 if (nsync > 1) {
1988 cycle_t cycle_last;
1989
1990 /* same start cycle for master and group */
1991 azx_dev = get_azx_dev(substream);
1992 cycle_last = azx_dev->azx_tc.cycle_last;
1993
1994 snd_pcm_group_for_each_entry(s, substream) {
1995 if (s->pcm->card != substream->pcm->card)
1996 continue;
1997 azx_timecounter_init(s, 1, cycle_last);
1998 }
1999 }
2000 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05002001 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01002002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Joseph Chan0e153472008-08-26 14:38:03 +02002005/* get the current DMA position with correction on VIA chips */
2006static unsigned int azx_via_get_position(struct azx *chip,
2007 struct azx_dev *azx_dev)
2008{
2009 unsigned int link_pos, mini_pos, bound_pos;
2010 unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
2011 unsigned int fifo_size;
2012
2013 link_pos = azx_sd_readl(azx_dev, SD_LPIB);
Takashi Iwaib4a655e2011-06-07 12:26:56 +02002014 if (azx_dev->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Joseph Chan0e153472008-08-26 14:38:03 +02002015 /* Playback, no problem using link position */
2016 return link_pos;
2017 }
2018
2019 /* Capture */
2020 /* For new chipset,
2021 * use mod to get the DMA position just like old chipset
2022 */
2023 mod_dma_pos = le32_to_cpu(*azx_dev->posbuf);
2024 mod_dma_pos %= azx_dev->period_bytes;
2025
2026 /* azx_dev->fifo_size can't get FIFO size of in stream.
2027 * Get from base address + offset.
2028 */
2029 fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
2030
2031 if (azx_dev->insufficient) {
2032 /* Link position never gather than FIFO size */
2033 if (link_pos <= fifo_size)
2034 return 0;
2035
2036 azx_dev->insufficient = 0;
2037 }
2038
2039 if (link_pos <= fifo_size)
2040 mini_pos = azx_dev->bufsize + link_pos - fifo_size;
2041 else
2042 mini_pos = link_pos - fifo_size;
2043
2044 /* Find nearest previous boudary */
2045 mod_mini_pos = mini_pos % azx_dev->period_bytes;
2046 mod_link_pos = link_pos % azx_dev->period_bytes;
2047 if (mod_link_pos >= fifo_size)
2048 bound_pos = link_pos - mod_link_pos;
2049 else if (mod_dma_pos >= mod_mini_pos)
2050 bound_pos = mini_pos - mod_mini_pos;
2051 else {
2052 bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes;
2053 if (bound_pos >= azx_dev->bufsize)
2054 bound_pos = 0;
2055 }
2056
2057 /* Calculate real DMA position we want */
2058 return bound_pos + mod_dma_pos;
2059}
2060
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002061static unsigned int azx_get_position(struct azx *chip,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002062 struct azx_dev *azx_dev,
2063 bool with_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Takashi Iwai21229612013-04-05 07:27:45 +02002065 struct snd_pcm_substream *substream = azx_dev->substream;
2066 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 unsigned int pos;
Takashi Iwai21229612013-04-05 07:27:45 +02002068 int stream = substream->stream;
2069 struct hda_pcm_stream *hinfo = apcm->hinfo[stream];
Takashi Iwai1a8506d2012-10-16 15:10:08 +02002070 int delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
David Henningsson4cb36312010-09-30 10:12:50 +02002072 switch (chip->position_fix[stream]) {
2073 case POS_FIX_LPIB:
2074 /* read LPIB */
2075 pos = azx_sd_readl(azx_dev, SD_LPIB);
2076 break;
2077 case POS_FIX_VIACOMBO:
Joseph Chan0e153472008-08-26 14:38:03 +02002078 pos = azx_via_get_position(chip, azx_dev);
David Henningsson4cb36312010-09-30 10:12:50 +02002079 break;
2080 default:
2081 /* use the position buffer */
2082 pos = le32_to_cpu(*azx_dev->posbuf);
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002083 if (with_check && chip->position_fix[stream] == POS_FIX_AUTO) {
Takashi Iwaia8103642011-06-07 12:23:23 +02002084 if (!pos || pos == (u32)-1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002085 dev_info(chip->card->dev,
2086 "Invalid position buffer, using LPIB read method instead.\n");
Takashi Iwaia8103642011-06-07 12:23:23 +02002087 chip->position_fix[stream] = POS_FIX_LPIB;
2088 pos = azx_sd_readl(azx_dev, SD_LPIB);
2089 } else
2090 chip->position_fix[stream] = POS_FIX_POSBUF;
2091 }
2092 break;
Takashi Iwaic74db862005-05-12 14:26:27 +02002093 }
David Henningsson4cb36312010-09-30 10:12:50 +02002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (pos >= azx_dev->bufsize)
2096 pos = 0;
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002097
2098 /* calculate runtime delay from LPIB */
Takashi Iwai21229612013-04-05 07:27:45 +02002099 if (substream->runtime &&
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002100 chip->position_fix[stream] == POS_FIX_POSBUF &&
2101 (chip->driver_caps & AZX_DCAPS_COUNT_LPIB_DELAY)) {
2102 unsigned int lpib_pos = azx_sd_readl(azx_dev, SD_LPIB);
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002103 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2104 delay = pos - lpib_pos;
2105 else
2106 delay = lpib_pos - pos;
Takashi Iwaie8648e52013-12-06 17:15:01 +01002107 if (delay < 0) {
2108 if (delay >= azx_dev->delay_negative_threshold)
2109 delay = 0;
2110 else
2111 delay += azx_dev->bufsize;
2112 }
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002113 if (delay >= azx_dev->period_bytes) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002114 dev_info(chip->card->dev,
2115 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
2116 delay, azx_dev->period_bytes);
Takashi Iwai1f046612012-10-16 16:52:26 +02002117 delay = 0;
2118 chip->driver_caps &= ~AZX_DCAPS_COUNT_LPIB_DELAY;
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002119 }
Takashi Iwai21229612013-04-05 07:27:45 +02002120 delay = bytes_to_frames(substream->runtime, delay);
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002121 }
Takashi Iwai21229612013-04-05 07:27:45 +02002122
2123 if (substream->runtime) {
2124 if (hinfo->ops.get_delay)
2125 delay += hinfo->ops.get_delay(hinfo, apcm->codec,
2126 substream);
2127 substream->runtime->delay = delay;
2128 }
2129
Takashi Iwai1a8506d2012-10-16 15:10:08 +02002130 trace_azx_get_position(chip, azx_dev, pos, delay);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002131 return pos;
2132}
2133
2134static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
2135{
2136 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2137 struct azx *chip = apcm->chip;
2138 struct azx_dev *azx_dev = get_azx_dev(substream);
2139 return bytes_to_frames(substream->runtime,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002140 azx_get_position(chip, azx_dev, false));
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002141}
2142
2143/*
2144 * Check whether the current DMA position is acceptable for updating
2145 * periods. Returns non-zero if it's OK.
2146 *
2147 * Many HD-audio controllers appear pretty inaccurate about
2148 * the update-IRQ timing. The IRQ is issued before actually the
2149 * data is processed. So, we need to process it afterwords in a
2150 * workqueue.
2151 */
2152static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
2153{
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002154 u32 wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002155 unsigned int pos;
2156
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002157 wallclk = azx_readl(chip, WALLCLK) - azx_dev->start_wallclk;
2158 if (wallclk < (azx_dev->period_wallclk * 2) / 3)
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002159 return -1; /* bogus (too early) interrupt */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002160
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002161 pos = azx_get_position(chip, azx_dev, true);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002162
Takashi Iwaid6d8bf52010-02-12 18:17:06 +01002163 if (WARN_ONCE(!azx_dev->period_bytes,
2164 "hda-intel: zero azx_dev->period_bytes"))
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002165 return -1; /* this shouldn't happen! */
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002166 if (wallclk < (azx_dev->period_wallclk * 5) / 4 &&
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002167 pos % azx_dev->period_bytes > azx_dev->period_bytes / 2)
2168 /* NG - it's below the first next period boundary */
2169 return bdl_pos_adj[chip->dev_index] ? 0 : -1;
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002170 azx_dev->start_wallclk += wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002171 return 1; /* OK, it's fine */
2172}
2173
2174/*
2175 * The work for pending PCM period updates.
2176 */
2177static void azx_irq_pending_work(struct work_struct *work)
2178{
2179 struct azx *chip = container_of(work, struct azx, irq_pending_work);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002180 int i, pending, ok;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002181
Takashi Iwaia6a950a2008-06-10 17:53:35 +02002182 if (!chip->irq_pending_warned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002183 dev_info(chip->card->dev,
2184 "IRQ timing workaround is activated for card #%d. Suggest a bigger bdl_pos_adj.\n",
2185 chip->card->number);
Takashi Iwaia6a950a2008-06-10 17:53:35 +02002186 chip->irq_pending_warned = 1;
2187 }
2188
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002189 for (;;) {
2190 pending = 0;
2191 spin_lock_irq(&chip->reg_lock);
2192 for (i = 0; i < chip->num_streams; i++) {
2193 struct azx_dev *azx_dev = &chip->azx_dev[i];
2194 if (!azx_dev->irq_pending ||
2195 !azx_dev->substream ||
2196 !azx_dev->running)
2197 continue;
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002198 ok = azx_position_ok(chip, azx_dev);
2199 if (ok > 0) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002200 azx_dev->irq_pending = 0;
2201 spin_unlock(&chip->reg_lock);
2202 snd_pcm_period_elapsed(azx_dev->substream);
2203 spin_lock(&chip->reg_lock);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002204 } else if (ok < 0) {
2205 pending = 0; /* too early */
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002206 } else
2207 pending++;
2208 }
2209 spin_unlock_irq(&chip->reg_lock);
2210 if (!pending)
2211 return;
Takashi Iwai08af4952010-08-03 14:39:04 +02002212 msleep(1);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002213 }
2214}
2215
2216/* clear irq_pending flags and assure no on-going workq */
2217static void azx_clear_irq_pending(struct azx *chip)
2218{
2219 int i;
2220
2221 spin_lock_irq(&chip->reg_lock);
2222 for (i = 0; i < chip->num_streams; i++)
2223 chip->azx_dev[i].irq_pending = 0;
2224 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002227#ifdef CONFIG_X86
2228static int azx_pcm_mmap(struct snd_pcm_substream *substream,
2229 struct vm_area_struct *area)
2230{
2231 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2232 struct azx *chip = apcm->chip;
2233 if (!azx_snoop(chip))
2234 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
2235 return snd_pcm_lib_default_mmap(substream, area);
2236}
2237#else
2238#define azx_pcm_mmap NULL
2239#endif
2240
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002241static struct snd_pcm_ops azx_pcm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 .open = azx_pcm_open,
2243 .close = azx_pcm_close,
2244 .ioctl = snd_pcm_lib_ioctl,
2245 .hw_params = azx_pcm_hw_params,
2246 .hw_free = azx_pcm_hw_free,
2247 .prepare = azx_pcm_prepare,
2248 .trigger = azx_pcm_trigger,
2249 .pointer = azx_pcm_pointer,
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05002250 .wall_clock = azx_get_wallclock_tstamp,
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002251 .mmap = azx_pcm_mmap,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002252 .page = snd_pcm_sgbuf_ops_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253};
2254
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002255static void azx_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Takashi Iwai176d5332008-07-30 15:01:44 +02002257 struct azx_pcm *apcm = pcm->private_data;
2258 if (apcm) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002259 list_del(&apcm->list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002260 kfree(apcm);
2261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002264#define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
2265
Takashi Iwai176d5332008-07-30 15:01:44 +02002266static int
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002267azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
2268 struct hda_pcm *cpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002270 struct azx *chip = bus->private_data;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002271 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 struct azx_pcm *apcm;
Takashi Iwai176d5332008-07-30 15:01:44 +02002273 int pcm_dev = cpcm->device;
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002274 unsigned int size;
Takashi Iwai176d5332008-07-30 15:01:44 +02002275 int s, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002277 list_for_each_entry(apcm, &chip->pcm_list, list) {
2278 if (apcm->pcm->device == pcm_dev) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002279 dev_err(chip->card->dev, "PCM %d already exists\n",
2280 pcm_dev);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002281 return -EBUSY;
2282 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002283 }
2284 err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
2285 cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
2286 cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 &pcm);
2288 if (err < 0)
2289 return err;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002290 strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
Takashi Iwai176d5332008-07-30 15:01:44 +02002291 apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (apcm == NULL)
2293 return -ENOMEM;
2294 apcm->chip = chip;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002295 apcm->pcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 apcm->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 pcm->private_data = apcm;
2298 pcm->private_free = azx_pcm_free;
Takashi Iwai176d5332008-07-30 15:01:44 +02002299 if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
2300 pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002301 list_add_tail(&apcm->list, &chip->pcm_list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002302 cpcm->pcm = pcm;
2303 for (s = 0; s < 2; s++) {
2304 apcm->hinfo[s] = &cpcm->stream[s];
2305 if (cpcm->stream[s].substreams)
2306 snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
2307 }
2308 /* buffer pre-allocation */
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002309 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
2310 if (size > MAX_PREALLOC_SIZE)
2311 size = MAX_PREALLOC_SIZE;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002312 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 snd_dma_pci_data(chip->pci),
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002314 size, MAX_PREALLOC_SIZE);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01002315 /* link to codec */
2316 pcm->dev = &codec->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return 0;
2318}
2319
2320/*
2321 * mixer creation - all stuff is implemented in hda module
2322 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002323static int azx_mixer_create(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 return snd_hda_build_controls(chip->bus);
2326}
2327
2328
2329/*
2330 * initialize SD streams
2331 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002332static int azx_init_stream(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 int i;
2335
2336 /* initialize each stream (aka device)
Takashi Iwaid01ce992007-07-27 16:52:19 +02002337 * assign the starting bdl address to each stream (device)
2338 * and initialize
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 */
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002340 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002341 struct azx_dev *azx_dev = &chip->azx_dev[i];
Takashi Iwai929861c2006-08-31 16:55:40 +02002342 azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
2344 azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
2345 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
2346 azx_dev->sd_int_sta_mask = 1 << i;
2347 /* stream tag: must be non-zero and unique */
2348 azx_dev->index = i;
2349 azx_dev->stream_tag = i + 1;
2350 }
2351
2352 return 0;
2353}
2354
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002355static int azx_acquire_irq(struct azx *chip, int do_disconnect)
2356{
Takashi Iwai437a5a42006-11-21 12:14:23 +01002357 if (request_irq(chip->pci->irq, azx_interrupt,
2358 chip->msi ? 0 : IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002359 KBUILD_MODNAME, chip)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002360 dev_err(chip->card->dev,
2361 "unable to grab IRQ %d, disabling device\n",
2362 chip->pci->irq);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002363 if (do_disconnect)
2364 snd_card_disconnect(chip->card);
2365 return -1;
2366 }
2367 chip->irq = chip->pci->irq;
Takashi Iwai69e13412006-11-21 12:10:55 +01002368 pci_intx(chip->pci, !chip->msi);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002369 return 0;
2370}
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Takashi Iwaicb53c622007-08-10 17:21:45 +02002373static void azx_stop_chip(struct azx *chip)
2374{
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002375 if (!chip->initialized)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002376 return;
2377
2378 /* disable interrupts */
2379 azx_int_disable(chip);
2380 azx_int_clear(chip);
2381
2382 /* disable CORB/RIRB */
2383 azx_free_cmd_io(chip);
2384
2385 /* disable position buffer */
2386 azx_writel(chip, DPLBASE, 0);
2387 azx_writel(chip, DPUBASE, 0);
2388
2389 chip->initialized = 0;
2390}
2391
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002392#ifdef CONFIG_SND_HDA_DSP_LOADER
2393/*
2394 * DSP loading code (e.g. for CA0132)
2395 */
2396
2397/* use the first stream for loading DSP */
2398static struct azx_dev *
2399azx_get_dsp_loader_dev(struct azx *chip)
2400{
2401 return &chip->azx_dev[chip->playback_index_offset];
2402}
2403
2404static int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
2405 unsigned int byte_size,
2406 struct snd_dma_buffer *bufp)
2407{
2408 u32 *bdl;
2409 struct azx *chip = bus->private_data;
2410 struct azx_dev *azx_dev;
2411 int err;
2412
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002413 azx_dev = azx_get_dsp_loader_dev(chip);
2414
2415 dsp_lock(azx_dev);
2416 spin_lock_irq(&chip->reg_lock);
2417 if (azx_dev->running || azx_dev->locked) {
2418 spin_unlock_irq(&chip->reg_lock);
2419 err = -EBUSY;
2420 goto unlock;
2421 }
2422 azx_dev->prepared = 0;
2423 chip->saved_azx_dev = *azx_dev;
2424 azx_dev->locked = 1;
2425 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002426
2427 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
2428 snd_dma_pci_data(chip->pci),
2429 byte_size, bufp);
2430 if (err < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002431 goto err_alloc;
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002432
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002433 mark_pages_wc(chip, bufp, true);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002434 azx_dev->bufsize = byte_size;
2435 azx_dev->period_bytes = byte_size;
2436 azx_dev->format_val = format;
2437
2438 azx_stream_reset(chip, azx_dev);
2439
2440 /* reset BDL address */
2441 azx_sd_writel(azx_dev, SD_BDLPL, 0);
2442 azx_sd_writel(azx_dev, SD_BDLPU, 0);
2443
2444 azx_dev->frags = 0;
2445 bdl = (u32 *)azx_dev->bdl.area;
2446 err = setup_bdle(chip, bufp, azx_dev, &bdl, 0, byte_size, 0);
2447 if (err < 0)
2448 goto error;
2449
2450 azx_setup_controller(chip, azx_dev);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002451 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002452 return azx_dev->stream_tag;
2453
2454 error:
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002455 mark_pages_wc(chip, bufp, false);
2456 snd_dma_free_pages(bufp);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002457 err_alloc:
2458 spin_lock_irq(&chip->reg_lock);
2459 if (azx_dev->opened)
2460 *azx_dev = chip->saved_azx_dev;
2461 azx_dev->locked = 0;
2462 spin_unlock_irq(&chip->reg_lock);
2463 unlock:
2464 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002465 return err;
2466}
2467
2468static void azx_load_dsp_trigger(struct hda_bus *bus, bool start)
2469{
2470 struct azx *chip = bus->private_data;
2471 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
2472
2473 if (start)
2474 azx_stream_start(chip, azx_dev);
2475 else
2476 azx_stream_stop(chip, azx_dev);
2477 azx_dev->running = start;
2478}
2479
2480static void azx_load_dsp_cleanup(struct hda_bus *bus,
2481 struct snd_dma_buffer *dmab)
2482{
2483 struct azx *chip = bus->private_data;
2484 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
2485
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002486 if (!dmab->area || !azx_dev->locked)
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002487 return;
2488
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002489 dsp_lock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002490 /* reset BDL address */
2491 azx_sd_writel(azx_dev, SD_BDLPL, 0);
2492 azx_sd_writel(azx_dev, SD_BDLPU, 0);
2493 azx_sd_writel(azx_dev, SD_CTL, 0);
2494 azx_dev->bufsize = 0;
2495 azx_dev->period_bytes = 0;
2496 azx_dev->format_val = 0;
2497
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002498 mark_pages_wc(chip, dmab, false);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002499 snd_dma_free_pages(dmab);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002500 dmab->area = NULL;
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002501
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002502 spin_lock_irq(&chip->reg_lock);
2503 if (azx_dev->opened)
2504 *azx_dev = chip->saved_azx_dev;
2505 azx_dev->locked = 0;
2506 spin_unlock_irq(&chip->reg_lock);
2507 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002508}
2509#endif /* CONFIG_SND_HDA_DSP_LOADER */
2510
Takashi Iwai83012a72012-08-24 18:38:08 +02002511#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02002512/* power-up/down the controller */
Takashi Iwai68467f52012-08-28 09:14:29 -07002513static void azx_power_notify(struct hda_bus *bus, bool power_up)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002514{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002515 struct azx *chip = bus->private_data;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002516
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01002517 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2518 return;
2519
Takashi Iwai68467f52012-08-28 09:14:29 -07002520 if (power_up)
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002521 pm_runtime_get_sync(&chip->pci->dev);
2522 else
2523 pm_runtime_put_sync(&chip->pci->dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002524}
Takashi Iwai65fcd412012-08-14 17:13:32 +02002525
2526static DEFINE_MUTEX(card_list_lock);
2527static LIST_HEAD(card_list);
2528
2529static void azx_add_card_list(struct azx *chip)
2530{
2531 mutex_lock(&card_list_lock);
2532 list_add(&chip->list, &card_list);
2533 mutex_unlock(&card_list_lock);
2534}
2535
2536static void azx_del_card_list(struct azx *chip)
2537{
2538 mutex_lock(&card_list_lock);
2539 list_del_init(&chip->list);
2540 mutex_unlock(&card_list_lock);
2541}
2542
2543/* trigger power-save check at writing parameter */
2544static int param_set_xint(const char *val, const struct kernel_param *kp)
2545{
2546 struct azx *chip;
2547 struct hda_codec *c;
2548 int prev = power_save;
2549 int ret = param_set_int(val, kp);
2550
2551 if (ret || prev == power_save)
2552 return ret;
2553
2554 mutex_lock(&card_list_lock);
2555 list_for_each_entry(chip, &card_list, list) {
2556 if (!chip->bus || chip->disabled)
2557 continue;
2558 list_for_each_entry(c, &chip->bus->codec_list, list)
2559 snd_hda_power_sync(c);
2560 }
2561 mutex_unlock(&card_list_lock);
2562 return 0;
2563}
2564#else
2565#define azx_add_card_list(chip) /* NOP */
2566#define azx_del_card_list(chip) /* NOP */
Takashi Iwai83012a72012-08-24 18:38:08 +02002567#endif /* CONFIG_PM */
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002568
Takashi Iwai7ccbde52012-08-14 18:10:09 +02002569#if defined(CONFIG_PM_SLEEP) || defined(SUPPORT_VGA_SWITCHEROO)
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002570/*
2571 * power management
2572 */
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002573static int azx_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002575 struct pci_dev *pci = to_pci_dev(dev);
2576 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002577 struct azx *chip = card->private_data;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002578 struct azx_pcm *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Takashi Iwaic5c21522012-12-04 17:01:25 +01002580 if (chip->disabled)
2581 return 0;
2582
Takashi Iwai421a1252005-11-17 16:11:09 +01002583 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002584 azx_clear_irq_pending(chip);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002585 list_for_each_entry(p, &chip->pcm_list, list)
2586 snd_pcm_suspend_all(p->pcm);
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02002587 if (chip->initialized)
Takashi Iwai8dd78332009-06-02 01:16:07 +02002588 snd_hda_suspend(chip->bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002589 azx_stop_chip(chip);
Mengdong Lin7295b262013-06-25 05:58:49 -04002590 azx_enter_link_reset(chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002591 if (chip->irq >= 0) {
Takashi Iwai43001c92006-09-08 12:30:03 +02002592 free_irq(chip->irq, chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002593 chip->irq = -1;
2594 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002595 if (chip->msi)
Takashi Iwai43001c92006-09-08 12:30:03 +02002596 pci_disable_msi(chip->pci);
Takashi Iwai421a1252005-11-17 16:11:09 +01002597 pci_disable_device(pci);
2598 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002599 pci_set_power_state(pci, PCI_D3hot);
Wang Xingchao99a20082013-05-30 22:07:10 +08002600 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2601 hda_display_power(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return 0;
2603}
2604
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002605static int azx_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002607 struct pci_dev *pci = to_pci_dev(dev);
2608 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002609 struct azx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Takashi Iwaic5c21522012-12-04 17:01:25 +01002611 if (chip->disabled)
2612 return 0;
2613
Wang Xingchao99a20082013-05-30 22:07:10 +08002614 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2615 hda_display_power(true);
Takashi Iwaid14a7e02009-02-16 10:13:03 +01002616 pci_set_power_state(pci, PCI_D0);
2617 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002618 if (pci_enable_device(pci) < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002619 dev_err(chip->card->dev,
2620 "pci_enable_device failed, disabling device\n");
Takashi Iwai30b35392006-10-11 18:52:53 +02002621 snd_card_disconnect(card);
2622 return -EIO;
2623 }
2624 pci_set_master(pci);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002625 if (chip->msi)
2626 if (pci_enable_msi(pci) < 0)
2627 chip->msi = 0;
2628 if (azx_acquire_irq(chip, 1) < 0)
Takashi Iwai30b35392006-10-11 18:52:53 +02002629 return -EIO;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002630 azx_init_pci(chip);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002631
Takashi Iwai7f308302012-05-08 16:52:23 +02002632 azx_init_chip(chip, 1);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 snd_hda_resume(chip->bus);
Takashi Iwai421a1252005-11-17 16:11:09 +01002635 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return 0;
2637}
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002638#endif /* CONFIG_PM_SLEEP || SUPPORT_VGA_SWITCHEROO */
2639
2640#ifdef CONFIG_PM_RUNTIME
2641static int azx_runtime_suspend(struct device *dev)
2642{
2643 struct snd_card *card = dev_get_drvdata(dev);
2644 struct azx *chip = card->private_data;
2645
Dave Airlie246efa42013-07-29 15:19:29 +10002646 if (chip->disabled)
2647 return 0;
2648
2649 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2650 return 0;
2651
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002652 /* enable controller wake up event */
2653 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) |
2654 STATESTS_INT_MASK);
2655
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002656 azx_stop_chip(chip);
Takashi Iwai873ce8a2013-11-26 11:58:40 +01002657 azx_enter_link_reset(chip);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002658 azx_clear_irq_pending(chip);
Wang Xingchao99a20082013-05-30 22:07:10 +08002659 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2660 hda_display_power(false);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002661 return 0;
2662}
2663
2664static int azx_runtime_resume(struct device *dev)
2665{
2666 struct snd_card *card = dev_get_drvdata(dev);
2667 struct azx *chip = card->private_data;
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002668 struct hda_bus *bus;
2669 struct hda_codec *codec;
2670 int status;
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002671
Dave Airlie246efa42013-07-29 15:19:29 +10002672 if (chip->disabled)
2673 return 0;
2674
2675 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2676 return 0;
2677
Wang Xingchao99a20082013-05-30 22:07:10 +08002678 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2679 hda_display_power(true);
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002680
2681 /* Read STATESTS before controller reset */
2682 status = azx_readw(chip, STATESTS);
2683
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002684 azx_init_pci(chip);
2685 azx_init_chip(chip, 1);
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002686
2687 bus = chip->bus;
2688 if (status && bus) {
2689 list_for_each_entry(codec, &bus->codec_list, list)
2690 if (status & (1 << codec->addr))
2691 queue_delayed_work(codec->bus->workq,
2692 &codec->jackpoll_work, codec->jackpoll_interval);
2693 }
2694
2695 /* disable controller Wake Up event*/
2696 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) &
2697 ~STATESTS_INT_MASK);
2698
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002699 return 0;
2700}
Takashi Iwai6eb827d2012-12-12 11:50:12 +01002701
2702static int azx_runtime_idle(struct device *dev)
2703{
2704 struct snd_card *card = dev_get_drvdata(dev);
2705 struct azx *chip = card->private_data;
2706
Dave Airlie246efa42013-07-29 15:19:29 +10002707 if (chip->disabled)
2708 return 0;
2709
Takashi Iwai6eb827d2012-12-12 11:50:12 +01002710 if (!power_save_controller ||
2711 !(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2712 return -EBUSY;
2713
2714 return 0;
2715}
2716
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002717#endif /* CONFIG_PM_RUNTIME */
2718
2719#ifdef CONFIG_PM
2720static const struct dev_pm_ops azx_pm = {
2721 SET_SYSTEM_SLEEP_PM_OPS(azx_suspend, azx_resume)
Takashi Iwai6eb827d2012-12-12 11:50:12 +01002722 SET_RUNTIME_PM_OPS(azx_runtime_suspend, azx_runtime_resume, azx_runtime_idle)
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002723};
2724
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002725#define AZX_PM_OPS &azx_pm
2726#else
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002727#define AZX_PM_OPS NULL
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002728#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730
2731/*
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002732 * reboot notifier for hang-up problem at power-down
2733 */
2734static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
2735{
2736 struct azx *chip = container_of(nb, struct azx, reboot_notifier);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01002737 snd_hda_bus_reboot_notify(chip->bus);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002738 azx_stop_chip(chip);
2739 return NOTIFY_OK;
2740}
2741
2742static void azx_notifier_register(struct azx *chip)
2743{
2744 chip->reboot_notifier.notifier_call = azx_halt;
2745 register_reboot_notifier(&chip->reboot_notifier);
2746}
2747
2748static void azx_notifier_unregister(struct azx *chip)
2749{
2750 if (chip->reboot_notifier.notifier_call)
2751 unregister_reboot_notifier(&chip->reboot_notifier);
2752}
2753
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01002754static int azx_probe_continue(struct azx *chip);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002755
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002756#ifdef SUPPORT_VGA_SWITCHEROO
Bill Pembertone23e7a12012-12-06 12:35:10 -05002757static struct pci_dev *get_bound_vga(struct pci_dev *pci);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002758
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002759static void azx_vs_set_state(struct pci_dev *pci,
2760 enum vga_switcheroo_state state)
2761{
2762 struct snd_card *card = pci_get_drvdata(pci);
2763 struct azx *chip = card->private_data;
2764 bool disabled;
2765
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002766 wait_for_completion(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002767 if (chip->init_failed)
2768 return;
2769
2770 disabled = (state == VGA_SWITCHEROO_OFF);
2771 if (chip->disabled == disabled)
2772 return;
2773
2774 if (!chip->bus) {
2775 chip->disabled = disabled;
2776 if (!disabled) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002777 dev_info(chip->card->dev,
2778 "Start delayed initialization\n");
Takashi Iwai5c906802013-05-30 22:07:09 +08002779 if (azx_probe_continue(chip) < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002780 dev_err(chip->card->dev, "initialization error\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002781 chip->init_failed = true;
2782 }
2783 }
2784 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002785 dev_info(chip->card->dev, "%s via VGA-switcheroo\n",
2786 disabled ? "Disabling" : "Enabling");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002787 if (disabled) {
Dave Airlie246efa42013-07-29 15:19:29 +10002788 pm_runtime_put_sync_suspend(&pci->dev);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002789 azx_suspend(&pci->dev);
Dave Airlie246efa42013-07-29 15:19:29 +10002790 /* when we get suspended by vga switcheroo we end up in D3cold,
2791 * however we have no ACPI handle, so pci/acpi can't put us there,
2792 * put ourselves there */
2793 pci->current_state = PCI_D3cold;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002794 chip->disabled = true;
Takashi Iwai128960a2012-10-12 17:28:18 +02002795 if (snd_hda_lock_devices(chip->bus))
Takashi Iwai4e76a882014-02-25 12:21:03 +01002796 dev_warn(chip->card->dev,
2797 "Cannot lock devices!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002798 } else {
2799 snd_hda_unlock_devices(chip->bus);
Dave Airlie246efa42013-07-29 15:19:29 +10002800 pm_runtime_get_noresume(&pci->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002801 chip->disabled = false;
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002802 azx_resume(&pci->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002803 }
2804 }
2805}
2806
2807static bool azx_vs_can_switch(struct pci_dev *pci)
2808{
2809 struct snd_card *card = pci_get_drvdata(pci);
2810 struct azx *chip = card->private_data;
2811
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002812 wait_for_completion(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002813 if (chip->init_failed)
2814 return false;
2815 if (chip->disabled || !chip->bus)
2816 return true;
2817 if (snd_hda_lock_devices(chip->bus))
2818 return false;
2819 snd_hda_unlock_devices(chip->bus);
2820 return true;
2821}
2822
Bill Pembertone23e7a12012-12-06 12:35:10 -05002823static void init_vga_switcheroo(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002824{
2825 struct pci_dev *p = get_bound_vga(chip->pci);
2826 if (p) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002827 dev_info(chip->card->dev,
2828 "Handle VGA-switcheroo audio client\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002829 chip->use_vga_switcheroo = 1;
2830 pci_dev_put(p);
2831 }
2832}
2833
2834static const struct vga_switcheroo_client_ops azx_vs_ops = {
2835 .set_gpu_state = azx_vs_set_state,
2836 .can_switch = azx_vs_can_switch,
2837};
2838
Bill Pembertone23e7a12012-12-06 12:35:10 -05002839static int register_vga_switcheroo(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002840{
Takashi Iwai128960a2012-10-12 17:28:18 +02002841 int err;
2842
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002843 if (!chip->use_vga_switcheroo)
2844 return 0;
2845 /* FIXME: currently only handling DIS controller
2846 * is there any machine with two switchable HDMI audio controllers?
2847 */
Takashi Iwai128960a2012-10-12 17:28:18 +02002848 err = vga_switcheroo_register_audio_client(chip->pci, &azx_vs_ops,
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002849 VGA_SWITCHEROO_DIS,
2850 chip->bus != NULL);
Takashi Iwai128960a2012-10-12 17:28:18 +02002851 if (err < 0)
2852 return err;
2853 chip->vga_switcheroo_registered = 1;
Dave Airlie246efa42013-07-29 15:19:29 +10002854
2855 /* register as an optimus hdmi audio power domain */
2856 vga_switcheroo_init_domain_pm_optimus_hdmi_audio(&chip->pci->dev, &chip->hdmi_pm_domain);
Takashi Iwai128960a2012-10-12 17:28:18 +02002857 return 0;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002858}
2859#else
2860#define init_vga_switcheroo(chip) /* NOP */
2861#define register_vga_switcheroo(chip) 0
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002862#define check_hdmi_disabled(pci) false
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002863#endif /* SUPPORT_VGA_SWITCHER */
2864
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002865/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 * destructor
2867 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002868static int azx_free(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Wang Xingchaoc67e2222013-05-30 22:07:08 +08002870 struct pci_dev *pci = chip->pci;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002871 int i;
2872
Wang Xingchaoc67e2222013-05-30 22:07:08 +08002873 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME)
2874 && chip->running)
2875 pm_runtime_get_noresume(&pci->dev);
2876
Takashi Iwai65fcd412012-08-14 17:13:32 +02002877 azx_del_card_list(chip);
2878
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002879 azx_notifier_unregister(chip);
2880
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002881 chip->init_failed = 1; /* to be sure */
Daniel J Blueman44728e92012-12-18 23:59:33 +08002882 complete_all(&chip->probe_wait);
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002883
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002884 if (use_vga_switcheroo(chip)) {
2885 if (chip->disabled && chip->bus)
2886 snd_hda_unlock_devices(chip->bus);
Takashi Iwai128960a2012-10-12 17:28:18 +02002887 if (chip->vga_switcheroo_registered)
2888 vga_switcheroo_unregister_client(chip->pci);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002889 }
2890
Takashi Iwaice43fba2005-05-30 20:33:44 +02002891 if (chip->initialized) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002892 azx_clear_irq_pending(chip);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002893 for (i = 0; i < chip->num_streams; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 azx_stream_stop(chip, &chip->azx_dev[i]);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002895 azx_stop_chip(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
2897
Jeff Garzikf000fd82008-04-22 13:50:34 +02002898 if (chip->irq >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 free_irq(chip->irq, (void*)chip);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002900 if (chip->msi)
Takashi Iwai30b35392006-10-11 18:52:53 +02002901 pci_disable_msi(chip->pci);
Takashi Iwaif079c252006-06-01 11:42:14 +02002902 if (chip->remap_addr)
2903 iounmap(chip->remap_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002905 if (chip->azx_dev) {
2906 for (i = 0; i < chip->num_streams; i++)
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002907 if (chip->azx_dev[i].bdl.area) {
2908 mark_pages_wc(chip, &chip->azx_dev[i].bdl, false);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002909 snd_dma_free_pages(&chip->azx_dev[i].bdl);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002910 }
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002911 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002912 if (chip->rb.area) {
2913 mark_pages_wc(chip, &chip->rb, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 snd_dma_free_pages(&chip->rb);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002915 }
2916 if (chip->posbuf.area) {
2917 mark_pages_wc(chip, &chip->posbuf, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 snd_dma_free_pages(&chip->posbuf);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002919 }
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002920 if (chip->region_requested)
2921 pci_release_regions(chip->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 pci_disable_device(chip->pci);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002923 kfree(chip->azx_dev);
Takashi Iwai4918cda2012-08-09 12:33:28 +02002924#ifdef CONFIG_SND_HDA_PATCH_LOADER
2925 if (chip->fw)
2926 release_firmware(chip->fw);
2927#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08002928 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
2929 hda_display_power(false);
2930 hda_i915_exit();
2931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 kfree(chip);
2933
2934 return 0;
2935}
2936
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002937static int azx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 return azx_free(device->device_data);
2940}
2941
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002942#ifdef SUPPORT_VGA_SWITCHEROO
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943/*
Takashi Iwai91219472012-04-26 12:13:25 +02002944 * Check of disabled HDMI controller by vga-switcheroo
2945 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002946static struct pci_dev *get_bound_vga(struct pci_dev *pci)
Takashi Iwai91219472012-04-26 12:13:25 +02002947{
2948 struct pci_dev *p;
2949
2950 /* check only discrete GPU */
2951 switch (pci->vendor) {
2952 case PCI_VENDOR_ID_ATI:
2953 case PCI_VENDOR_ID_AMD:
2954 case PCI_VENDOR_ID_NVIDIA:
2955 if (pci->devfn == 1) {
2956 p = pci_get_domain_bus_and_slot(pci_domain_nr(pci->bus),
2957 pci->bus->number, 0);
2958 if (p) {
2959 if ((p->class >> 8) == PCI_CLASS_DISPLAY_VGA)
2960 return p;
2961 pci_dev_put(p);
2962 }
2963 }
2964 break;
2965 }
2966 return NULL;
2967}
2968
Bill Pembertone23e7a12012-12-06 12:35:10 -05002969static bool check_hdmi_disabled(struct pci_dev *pci)
Takashi Iwai91219472012-04-26 12:13:25 +02002970{
2971 bool vga_inactive = false;
2972 struct pci_dev *p = get_bound_vga(pci);
2973
2974 if (p) {
Takashi Iwai12b78a72012-06-07 12:15:16 +02002975 if (vga_switcheroo_get_client_state(p) == VGA_SWITCHEROO_OFF)
Takashi Iwai91219472012-04-26 12:13:25 +02002976 vga_inactive = true;
2977 pci_dev_put(p);
2978 }
2979 return vga_inactive;
2980}
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002981#endif /* SUPPORT_VGA_SWITCHEROO */
Takashi Iwai91219472012-04-26 12:13:25 +02002982
2983/*
Takashi Iwai3372a152007-02-01 15:46:50 +01002984 * white/black-listing for position_fix
2985 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002986static struct snd_pci_quirk position_fix_list[] = {
Takashi Iwaid2e1c972008-06-10 17:53:34 +02002987 SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
2988 SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
Takashi Iwai2f703e72009-12-01 14:17:37 +01002989 SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
Takashi Iwaid2e1c972008-06-10 17:53:34 +02002990 SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
Daniel T Chendd37f8e2010-05-30 01:17:03 -04002991 SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS", POS_FIX_LPIB),
Daniel T Chen9f75c1b2010-05-30 13:08:41 -04002992 SND_PCI_QUIRK(0x1043, 0x81e7, "ASUS M2V", POS_FIX_LPIB),
Daniel T Chene96d3122010-05-27 18:32:18 -04002993 SND_PCI_QUIRK(0x104d, 0x9069, "Sony VPCS11V9E", POS_FIX_LPIB),
David Henningssonb01de4f2012-01-12 16:31:14 +01002994 SND_PCI_QUIRK(0x10de, 0xcb89, "Macbook Pro 7,1", POS_FIX_LPIB),
Daniel T Chen61bb42c2010-05-29 11:04:11 -04002995 SND_PCI_QUIRK(0x1297, 0x3166, "Shuttle", POS_FIX_LPIB),
Daniel T Chen9ec8dda2010-03-28 02:34:40 -04002996 SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
Takashi Iwai45d4ebf2009-11-30 11:58:30 +01002997 SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
Takashi Iwai8815cd02010-04-15 09:02:41 +02002998 SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
Daniel T Chenb90c0762010-05-30 19:31:41 -04002999 SND_PCI_QUIRK(0x1849, 0x0888, "775Dual-VSTA", POS_FIX_LPIB),
Daniel T Chen0e0280d2010-04-21 19:55:43 -04003000 SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
Takashi Iwai3372a152007-02-01 15:46:50 +01003001 {}
3002};
3003
Bill Pembertone23e7a12012-12-06 12:35:10 -05003004static int check_position_fix(struct azx *chip, int fix)
Takashi Iwai3372a152007-02-01 15:46:50 +01003005{
3006 const struct snd_pci_quirk *q;
3007
Takashi Iwaic673ba12009-03-17 07:49:14 +01003008 switch (fix) {
Takashi Iwai1dac6692012-09-13 14:59:47 +02003009 case POS_FIX_AUTO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01003010 case POS_FIX_LPIB:
3011 case POS_FIX_POSBUF:
David Henningsson4cb36312010-09-30 10:12:50 +02003012 case POS_FIX_VIACOMBO:
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01003013 case POS_FIX_COMBO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01003014 return fix;
3015 }
3016
Takashi Iwaic673ba12009-03-17 07:49:14 +01003017 q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
3018 if (q) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003019 dev_info(chip->card->dev,
3020 "position_fix set to %d for device %04x:%04x\n",
3021 q->value, q->subvendor, q->subdevice);
Takashi Iwaic673ba12009-03-17 07:49:14 +01003022 return q->value;
Takashi Iwai3372a152007-02-01 15:46:50 +01003023 }
David Henningssonbdd9ef22010-10-04 12:02:14 +02003024
3025 /* Check VIA/ATI HD Audio Controller exist */
Takashi Iwai9477c582011-05-25 09:11:37 +02003026 if (chip->driver_caps & AZX_DCAPS_POSFIX_VIA) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003027 dev_dbg(chip->card->dev, "Using VIACOMBO position fix\n");
David Henningssonbdd9ef22010-10-04 12:02:14 +02003028 return POS_FIX_VIACOMBO;
3029 }
Takashi Iwai9477c582011-05-25 09:11:37 +02003030 if (chip->driver_caps & AZX_DCAPS_POSFIX_LPIB) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003031 dev_dbg(chip->card->dev, "Using LPIB position fix\n");
Takashi Iwai9477c582011-05-25 09:11:37 +02003032 return POS_FIX_LPIB;
3033 }
Takashi Iwaic673ba12009-03-17 07:49:14 +01003034 return POS_FIX_AUTO;
Takashi Iwai3372a152007-02-01 15:46:50 +01003035}
3036
3037/*
Takashi Iwai669ba272007-08-17 09:17:36 +02003038 * black-lists for probe_mask
3039 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003040static struct snd_pci_quirk probe_mask_list[] = {
Takashi Iwai669ba272007-08-17 09:17:36 +02003041 /* Thinkpad often breaks the controller communication when accessing
3042 * to the non-working (or non-existing) modem codec slot.
3043 */
3044 SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
3045 SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
3046 SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
Takashi Iwai0edb9452008-11-07 14:53:09 +01003047 /* broken BIOS */
3048 SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
Takashi Iwaief1681d2008-11-24 17:29:28 +01003049 /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
3050 SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01003051 /* forced codec slots */
Ozan Çağlayan93574842009-05-23 15:00:04 +03003052 SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01003053 SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
Jaroslav Kyselaf3af9052012-04-26 17:52:35 +02003054 /* WinFast VP200 H (Teradici) user reported broken communication */
3055 SND_PCI_QUIRK(0x3a21, 0x040d, "WinFast VP200 H", 0x101),
Takashi Iwai669ba272007-08-17 09:17:36 +02003056 {}
3057};
3058
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003059#define AZX_FORCE_CODEC_MASK 0x100
3060
Bill Pembertone23e7a12012-12-06 12:35:10 -05003061static void check_probe_mask(struct azx *chip, int dev)
Takashi Iwai669ba272007-08-17 09:17:36 +02003062{
3063 const struct snd_pci_quirk *q;
3064
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003065 chip->codec_probe_mask = probe_mask[dev];
3066 if (chip->codec_probe_mask == -1) {
Takashi Iwai669ba272007-08-17 09:17:36 +02003067 q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
3068 if (q) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003069 dev_info(chip->card->dev,
3070 "probe_mask set to 0x%x for device %04x:%04x\n",
3071 q->value, q->subvendor, q->subdevice);
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003072 chip->codec_probe_mask = q->value;
Takashi Iwai669ba272007-08-17 09:17:36 +02003073 }
3074 }
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003075
3076 /* check forced option */
3077 if (chip->codec_probe_mask != -1 &&
3078 (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
3079 chip->codec_mask = chip->codec_probe_mask & 0xff;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003080 dev_info(chip->card->dev, "codec_mask forced to 0x%x\n",
3081 chip->codec_mask);
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003082 }
Takashi Iwai669ba272007-08-17 09:17:36 +02003083}
3084
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003085/*
Takashi Iwai716238552009-09-28 13:14:04 +02003086 * white/black-list for enable_msi
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003087 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003088static struct snd_pci_quirk msi_black_list[] = {
David Henningsson693e0cb2013-12-12 09:52:03 +01003089 SND_PCI_QUIRK(0x103c, 0x2191, "HP", 0), /* AMD Hudson */
3090 SND_PCI_QUIRK(0x103c, 0x2192, "HP", 0), /* AMD Hudson */
3091 SND_PCI_QUIRK(0x103c, 0x21f7, "HP", 0), /* AMD Hudson */
3092 SND_PCI_QUIRK(0x103c, 0x21fa, "HP", 0), /* AMD Hudson */
Takashi Iwai9dc83982009-12-22 08:15:01 +01003093 SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai0a27fcf2010-02-15 17:05:28 +01003094 SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
Ralf Gerbigecd21622010-03-09 18:25:47 +01003095 SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
Takashi Iwai83f72152013-09-09 10:20:48 +02003096 SND_PCI_QUIRK(0x1179, 0xfb44, "Toshiba Satellite C870", 0), /* AMD Hudson */
Michele Ballabio4193d132010-03-06 21:06:46 +01003097 SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai38155952010-04-04 12:14:03 +02003098 SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003099 {}
3100};
3101
Bill Pembertone23e7a12012-12-06 12:35:10 -05003102static void check_msi(struct azx *chip)
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003103{
3104 const struct snd_pci_quirk *q;
3105
Takashi Iwai716238552009-09-28 13:14:04 +02003106 if (enable_msi >= 0) {
3107 chip->msi = !!enable_msi;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003108 return;
Takashi Iwai716238552009-09-28 13:14:04 +02003109 }
3110 chip->msi = 1; /* enable MSI as default */
3111 q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003112 if (q) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003113 dev_info(chip->card->dev,
3114 "msi for device %04x:%04x set to %d\n",
3115 q->subvendor, q->subdevice, q->value);
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003116 chip->msi = q->value;
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003117 return;
3118 }
3119
3120 /* NVidia chipsets seem to cause troubles with MSI */
Takashi Iwai9477c582011-05-25 09:11:37 +02003121 if (chip->driver_caps & AZX_DCAPS_NO_MSI) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003122 dev_info(chip->card->dev, "Disabling MSI\n");
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003123 chip->msi = 0;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003124 }
3125}
3126
Takashi Iwaia1585d72011-12-14 09:27:04 +01003127/* check the snoop mode availability */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003128static void azx_check_snoop_available(struct azx *chip)
Takashi Iwaia1585d72011-12-14 09:27:04 +01003129{
3130 bool snoop = chip->snoop;
3131
3132 switch (chip->driver_type) {
3133 case AZX_DRIVER_VIA:
3134 /* force to non-snoop mode for a new VIA controller
3135 * when BIOS is set
3136 */
3137 if (snoop) {
3138 u8 val;
3139 pci_read_config_byte(chip->pci, 0x42, &val);
3140 if (!(val & 0x80) && chip->pci->revision == 0x30)
3141 snoop = false;
3142 }
3143 break;
3144 case AZX_DRIVER_ATIHDMI_NS:
3145 /* new ATI HDMI requires non-snoop */
3146 snoop = false;
3147 break;
Takashi Iwaic1279f82013-02-07 17:36:22 +01003148 case AZX_DRIVER_CTHDA:
3149 snoop = false;
3150 break;
Takashi Iwaia1585d72011-12-14 09:27:04 +01003151 }
3152
3153 if (snoop != chip->snoop) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003154 dev_info(chip->card->dev, "Force to %s mode\n",
3155 snoop ? "snoop" : "non-snoop");
Takashi Iwaia1585d72011-12-14 09:27:04 +01003156 chip->snoop = snoop;
3157 }
3158}
Takashi Iwai669ba272007-08-17 09:17:36 +02003159
Wang Xingchao99a20082013-05-30 22:07:10 +08003160static void azx_probe_work(struct work_struct *work)
3161{
3162 azx_probe_continue(container_of(work, struct azx, probe_work));
3163}
Wang Xingchao99a20082013-05-30 22:07:10 +08003164
Takashi Iwai669ba272007-08-17 09:17:36 +02003165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 * constructor
3167 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003168static int azx_create(struct snd_card *card, struct pci_dev *pci,
3169 int dev, unsigned int driver_caps,
3170 struct azx **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003172 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 .dev_free = azx_dev_free,
3174 };
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003175 struct azx *chip;
3176 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 *rchip = NULL;
Tobin Davisbcd72002008-01-15 11:23:55 +01003179
Pavel Machek927fc862006-08-31 17:03:43 +02003180 err = pci_enable_device(pci);
3181 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return err;
3183
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003184 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003185 if (!chip) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003186 dev_err(card->dev, "Cannot allocate chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 pci_disable_device(pci);
3188 return -ENOMEM;
3189 }
3190
3191 spin_lock_init(&chip->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01003192 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 chip->card = card;
3194 chip->pci = pci;
3195 chip->irq = -1;
Takashi Iwai9477c582011-05-25 09:11:37 +02003196 chip->driver_caps = driver_caps;
3197 chip->driver_type = driver_caps & 0xff;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003198 check_msi(chip);
Takashi Iwai555e2192008-06-10 17:53:34 +02003199 chip->dev_index = dev;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02003200 INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003201 INIT_LIST_HEAD(&chip->pcm_list);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003202 INIT_LIST_HEAD(&chip->list);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003203 init_vga_switcheroo(chip);
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003204 init_completion(&chip->probe_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Shahin Ghazinouribeaffc32010-05-11 08:19:55 +02003206 chip->position_fix[0] = chip->position_fix[1] =
3207 check_position_fix(chip, position_fix[dev]);
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01003208 /* combo mode uses LPIB for playback */
3209 if (chip->position_fix[0] == POS_FIX_COMBO) {
3210 chip->position_fix[0] = POS_FIX_LPIB;
3211 chip->position_fix[1] = POS_FIX_AUTO;
3212 }
3213
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003214 check_probe_mask(chip, dev);
Takashi Iwai3372a152007-02-01 15:46:50 +01003215
Takashi Iwai27346162006-01-12 18:28:44 +01003216 chip->single_cmd = single_cmd;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003217 chip->snoop = hda_snoop;
Takashi Iwaia1585d72011-12-14 09:27:04 +01003218 azx_check_snoop_available(chip);
Takashi Iwaic74db862005-05-12 14:26:27 +02003219
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003220 if (bdl_pos_adj[dev] < 0) {
3221 switch (chip->driver_type) {
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003222 case AZX_DRIVER_ICH:
Seth Heasley32679f92010-02-22 17:31:09 -08003223 case AZX_DRIVER_PCH:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003224 bdl_pos_adj[dev] = 1;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003225 break;
3226 default:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003227 bdl_pos_adj[dev] = 32;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003228 break;
3229 }
3230 }
3231
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003232 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
3233 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003234 dev_err(card->dev, "Error creating device [card]!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003235 azx_free(chip);
3236 return err;
3237 }
3238
Wang Xingchao99a20082013-05-30 22:07:10 +08003239 /* continue probing in work context as may trigger request module */
3240 INIT_WORK(&chip->probe_work, azx_probe_work);
Wang Xingchao99a20082013-05-30 22:07:10 +08003241
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003242 *rchip = chip;
Wang Xingchao99a20082013-05-30 22:07:10 +08003243
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003244 return 0;
3245}
3246
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01003247static int azx_first_init(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003248{
3249 int dev = chip->dev_index;
3250 struct pci_dev *pci = chip->pci;
3251 struct snd_card *card = chip->card;
3252 int i, err;
3253 unsigned short gcap;
3254
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003255#if BITS_PER_LONG != 64
3256 /* Fix up base address on ULI M5461 */
3257 if (chip->driver_type == AZX_DRIVER_ULI) {
3258 u16 tmp3;
3259 pci_read_config_word(pci, 0x40, &tmp3);
3260 pci_write_config_word(pci, 0x40, tmp3 | 0x10);
3261 pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
3262 }
3263#endif
3264
Pavel Machek927fc862006-08-31 17:03:43 +02003265 err = pci_request_regions(pci, "ICH HD audio");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003266 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 return err;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003268 chip->region_requested = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Pavel Machek927fc862006-08-31 17:03:43 +02003270 chip->addr = pci_resource_start(pci, 0);
Arjan van de Ven2f5ad542008-09-28 16:20:09 -07003271 chip->remap_addr = pci_ioremap_bar(pci, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (chip->remap_addr == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003273 dev_err(card->dev, "ioremap error\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003274 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
3276
Takashi Iwai68e7fff2006-10-23 13:40:59 +02003277 if (chip->msi)
3278 if (pci_enable_msi(pci) < 0)
3279 chip->msi = 0;
Stephen Hemminger7376d012006-08-21 19:17:46 +02003280
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003281 if (azx_acquire_irq(chip, 0) < 0)
3282 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 pci_set_master(pci);
3285 synchronize_irq(chip->irq);
3286
Tobin Davisbcd72002008-01-15 11:23:55 +01003287 gcap = azx_readw(chip, GCAP);
Takashi Iwai4e76a882014-02-25 12:21:03 +01003288 dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
Tobin Davisbcd72002008-01-15 11:23:55 +01003289
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003290 /* disable SB600 64bit support for safety */
Takashi Iwai9477c582011-05-25 09:11:37 +02003291 if (chip->pci->vendor == PCI_VENDOR_ID_ATI) {
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003292 struct pci_dev *p_smbus;
3293 p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
3294 PCI_DEVICE_ID_ATI_SBX00_SMBUS,
3295 NULL);
3296 if (p_smbus) {
3297 if (p_smbus->revision < 0x30)
3298 gcap &= ~ICH6_GCAP_64OK;
3299 pci_dev_put(p_smbus);
3300 }
3301 }
Takashi Iwai09240cf2009-03-17 07:47:18 +01003302
Takashi Iwai9477c582011-05-25 09:11:37 +02003303 /* disable 64bit DMA address on some devices */
3304 if (chip->driver_caps & AZX_DCAPS_NO_64BIT) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003305 dev_dbg(card->dev, "Disabling 64bit DMA\n");
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003306 gcap &= ~ICH6_GCAP_64OK;
Takashi Iwai9477c582011-05-25 09:11:37 +02003307 }
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003308
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003309 /* disable buffer size rounding to 128-byte multiples if supported */
Takashi Iwai7bfe0592012-01-23 17:53:39 +01003310 if (align_buffer_size >= 0)
3311 chip->align_buffer_size = !!align_buffer_size;
3312 else {
3313 if (chip->driver_caps & AZX_DCAPS_BUFSIZE)
3314 chip->align_buffer_size = 0;
3315 else if (chip->driver_caps & AZX_DCAPS_ALIGN_BUFSIZE)
3316 chip->align_buffer_size = 1;
3317 else
3318 chip->align_buffer_size = 1;
3319 }
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003320
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003321 /* allow 64bit DMA address if supported by H/W */
Takashi Iwaib21fadb2009-05-28 12:26:15 +02003322 if ((gcap & ICH6_GCAP_64OK) && !pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
Yang Hongyange9304382009-04-13 14:40:14 -07003323 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003324 else {
Yang Hongyange9304382009-04-13 14:40:14 -07003325 pci_set_dma_mask(pci, DMA_BIT_MASK(32));
3326 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003327 }
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003328
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003329 /* read number of streams from GCAP register instead of using
3330 * hardcoded value
3331 */
3332 chip->capture_streams = (gcap >> 8) & 0x0f;
3333 chip->playback_streams = (gcap >> 12) & 0x0f;
3334 if (!chip->playback_streams && !chip->capture_streams) {
Tobin Davisbcd72002008-01-15 11:23:55 +01003335 /* gcap didn't give any info, switching to old method */
3336
3337 switch (chip->driver_type) {
3338 case AZX_DRIVER_ULI:
3339 chip->playback_streams = ULI_NUM_PLAYBACK;
3340 chip->capture_streams = ULI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003341 break;
3342 case AZX_DRIVER_ATIHDMI:
Andiry Xu1815b342011-12-14 16:10:27 +08003343 case AZX_DRIVER_ATIHDMI_NS:
Tobin Davisbcd72002008-01-15 11:23:55 +01003344 chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
3345 chip->capture_streams = ATIHDMI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003346 break;
Yang, Libinc4da29c2008-11-13 11:07:07 +01003347 case AZX_DRIVER_GENERIC:
Tobin Davisbcd72002008-01-15 11:23:55 +01003348 default:
3349 chip->playback_streams = ICH6_NUM_PLAYBACK;
3350 chip->capture_streams = ICH6_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003351 break;
3352 }
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003353 }
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003354 chip->capture_index_offset = 0;
3355 chip->playback_index_offset = chip->capture_streams;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003356 chip->num_streams = chip->playback_streams + chip->capture_streams;
Takashi Iwaid01ce992007-07-27 16:52:19 +02003357 chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
3358 GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003359 if (!chip->azx_dev) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003360 dev_err(card->dev, "cannot malloc azx_dev\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003361 return -ENOMEM;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003362 }
3363
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003364 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaieb49faa2013-03-15 09:19:11 +01003365 dsp_lock_init(&chip->azx_dev[i]);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003366 /* allocate memory for the BDL for each stream */
3367 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
3368 snd_dma_pci_data(chip->pci),
3369 BDL_SIZE, &chip->azx_dev[i].bdl);
3370 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003371 dev_err(card->dev, "cannot allocate BDL\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003372 return -ENOMEM;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003373 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003374 mark_pages_wc(chip, &chip->azx_dev[i].bdl, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02003376 /* allocate memory for the position buffer */
Takashi Iwaid01ce992007-07-27 16:52:19 +02003377 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
3378 snd_dma_pci_data(chip->pci),
3379 chip->num_streams * 8, &chip->posbuf);
3380 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003381 dev_err(card->dev, "cannot allocate posbuf\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003382 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003384 mark_pages_wc(chip, &chip->posbuf, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 /* allocate CORB/RIRB */
Takashi Iwai81740862009-05-26 15:22:00 +02003386 err = azx_alloc_cmd_io(chip);
3387 if (err < 0)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003388 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 /* initialize streams */
3391 azx_init_stream(chip);
3392
3393 /* initialize chip */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003394 azx_init_pci(chip);
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003395 azx_init_chip(chip, (probe_only[dev] & 2) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 /* codec detection */
Pavel Machek927fc862006-08-31 17:03:43 +02003398 if (!chip->codec_mask) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003399 dev_err(card->dev, "no codecs found!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003400 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 }
3402
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003403 strcpy(card->driver, "HDA-Intel");
Takashi Iwai18cb7102009-04-16 10:22:24 +02003404 strlcpy(card->shortname, driver_short_names[chip->driver_type],
3405 sizeof(card->shortname));
3406 snprintf(card->longname, sizeof(card->longname),
3407 "%s at 0x%lx irq %i",
3408 card->shortname, chip->addr, chip->irq);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
Takashi Iwaicb53c622007-08-10 17:21:45 +02003413static void power_down_all_codecs(struct azx *chip)
3414{
Takashi Iwai83012a72012-08-24 18:38:08 +02003415#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003416 /* The codecs were powered up in snd_hda_codec_new().
3417 * Now all initialization done, so turn them down if possible
3418 */
3419 struct hda_codec *codec;
3420 list_for_each_entry(codec, &chip->bus->codec_list, list) {
3421 snd_hda_power_down(codec);
3422 }
3423#endif
3424}
3425
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003426#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003427/* callback from request_firmware_nowait() */
3428static void azx_firmware_cb(const struct firmware *fw, void *context)
3429{
3430 struct snd_card *card = context;
3431 struct azx *chip = card->private_data;
3432 struct pci_dev *pci = chip->pci;
3433
3434 if (!fw) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003435 dev_err(card->dev, "Cannot load firmware, aborting\n");
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003436 goto error;
3437 }
3438
3439 chip->fw = fw;
3440 if (!chip->disabled) {
3441 /* continue probing */
3442 if (azx_probe_continue(chip))
3443 goto error;
3444 }
3445 return; /* OK */
3446
3447 error:
3448 snd_card_free(card);
3449 pci_set_drvdata(pci, NULL);
3450}
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003451#endif
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003452
Bill Pembertone23e7a12012-12-06 12:35:10 -05003453static int azx_probe(struct pci_dev *pci,
3454 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003456 static int dev;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003457 struct snd_card *card;
3458 struct azx *chip;
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003459 bool schedule_probe;
Pavel Machek927fc862006-08-31 17:03:43 +02003460 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003462 if (dev >= SNDRV_CARDS)
3463 return -ENODEV;
3464 if (!enable[dev]) {
3465 dev++;
3466 return -ENOENT;
3467 }
3468
Takashi Iwai60c57722014-01-29 14:20:19 +01003469 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
3470 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003471 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003472 dev_err(&pci->dev, "Error creating card!\n");
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 }
3475
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003476 err = azx_create(card, pci, dev, pci_id->driver_data, &chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003477 if (err < 0)
3478 goto out_free;
Takashi Iwai421a1252005-11-17 16:11:09 +01003479 card->private_data = chip;
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003480
3481 pci_set_drvdata(pci, card);
3482
3483 err = register_vga_switcheroo(chip);
3484 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003485 dev_err(card->dev, "Error registering VGA-switcheroo client\n");
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003486 goto out_free;
3487 }
3488
3489 if (check_hdmi_disabled(pci)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003490 dev_info(card->dev, "VGA controller is disabled\n");
3491 dev_info(card->dev, "Delaying initialization\n");
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003492 chip->disabled = true;
3493 }
3494
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003495 schedule_probe = !chip->disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Takashi Iwai4918cda2012-08-09 12:33:28 +02003497#ifdef CONFIG_SND_HDA_PATCH_LOADER
3498 if (patch[dev] && *patch[dev]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003499 dev_info(card->dev, "Applying patch firmware '%s'\n",
3500 patch[dev]);
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003501 err = request_firmware_nowait(THIS_MODULE, true, patch[dev],
3502 &pci->dev, GFP_KERNEL, card,
3503 azx_firmware_cb);
Takashi Iwai4918cda2012-08-09 12:33:28 +02003504 if (err < 0)
3505 goto out_free;
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003506 schedule_probe = false; /* continued in azx_firmware_cb() */
Takashi Iwai4918cda2012-08-09 12:33:28 +02003507 }
3508#endif /* CONFIG_SND_HDA_PATCH_LOADER */
3509
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003510#ifndef CONFIG_SND_HDA_I915
3511 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
Takashi Iwai4e76a882014-02-25 12:21:03 +01003512 dev_err(card->dev, "Haswell must build in CONFIG_SND_HDA_I915\n");
Wang Xingchao99a20082013-05-30 22:07:10 +08003513#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08003514
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003515 if (schedule_probe)
3516 schedule_work(&chip->probe_work);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003517
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003518 dev++;
Takashi Iwai88d071f2013-12-02 11:12:28 +01003519 if (chip->disabled)
3520 complete_all(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003521 return 0;
3522
3523out_free:
3524 snd_card_free(card);
3525 return err;
3526}
3527
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01003528static int azx_probe_continue(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003529{
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003530 struct pci_dev *pci = chip->pci;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003531 int dev = chip->dev_index;
3532 int err;
3533
Wang Xingchao99a20082013-05-30 22:07:10 +08003534 /* Request power well for Haswell HDA controller and codec */
3535 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
David Henningssonc841ad22013-08-19 13:32:30 +02003536#ifdef CONFIG_SND_HDA_I915
Wang Xingchao99a20082013-05-30 22:07:10 +08003537 err = hda_i915_init();
3538 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003539 dev_err(chip->card->dev,
3540 "Error request power-well from i915\n");
Wang Xingchao99a20082013-05-30 22:07:10 +08003541 goto out_free;
3542 }
David Henningssonc841ad22013-08-19 13:32:30 +02003543#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08003544 hda_display_power(true);
3545 }
3546
Takashi Iwai5c906802013-05-30 22:07:09 +08003547 err = azx_first_init(chip);
3548 if (err < 0)
3549 goto out_free;
3550
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01003551#ifdef CONFIG_SND_HDA_INPUT_BEEP
3552 chip->beep_mode = beep_mode[dev];
3553#endif
3554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 /* create codec instances */
Takashi Iwaia1e21c92009-06-17 09:33:52 +02003556 err = azx_codec_create(chip, model[dev]);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003557 if (err < 0)
3558 goto out_free;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003559#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai4918cda2012-08-09 12:33:28 +02003560 if (chip->fw) {
3561 err = snd_hda_load_patch(chip->bus, chip->fw->size,
3562 chip->fw->data);
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003563 if (err < 0)
3564 goto out_free;
Takashi Iwaie39ae852012-11-22 16:18:13 +01003565#ifndef CONFIG_PM
Takashi Iwai4918cda2012-08-09 12:33:28 +02003566 release_firmware(chip->fw); /* no longer needed */
3567 chip->fw = NULL;
Takashi Iwaie39ae852012-11-22 16:18:13 +01003568#endif
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003569 }
3570#endif
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003571 if ((probe_only[dev] & 1) == 0) {
Takashi Iwaia1e21c92009-06-17 09:33:52 +02003572 err = azx_codec_configure(chip);
3573 if (err < 0)
3574 goto out_free;
3575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 /* create PCM streams */
Takashi Iwai176d5332008-07-30 15:01:44 +02003578 err = snd_hda_build_pcms(chip->bus);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003579 if (err < 0)
3580 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
3582 /* create mixer controls */
Takashi Iwaid01ce992007-07-27 16:52:19 +02003583 err = azx_mixer_create(chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003584 if (err < 0)
3585 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003587 err = snd_card_register(chip->card);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003588 if (err < 0)
3589 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Takashi Iwaicb53c622007-08-10 17:21:45 +02003591 chip->running = 1;
3592 power_down_all_codecs(chip);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01003593 azx_notifier_register(chip);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003594 azx_add_card_list(chip);
Dave Airlie246efa42013-07-29 15:19:29 +10003595 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME) || chip->use_vga_switcheroo)
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003596 pm_runtime_put_noidle(&pci->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003598out_free:
Takashi Iwai88d071f2013-12-02 11:12:28 +01003599 if (err < 0)
3600 chip->init_failed = 1;
3601 complete_all(&chip->probe_wait);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003602 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Bill Pembertone23e7a12012-12-06 12:35:10 -05003605static void azx_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
Takashi Iwai91219472012-04-26 12:13:25 +02003607 struct snd_card *card = pci_get_drvdata(pci);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003608
Takashi Iwai91219472012-04-26 12:13:25 +02003609 if (card)
3610 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
3613/* PCI IDs */
Alexey Dobriyancebe41d2010-02-06 00:21:03 +02003614static DEFINE_PCI_DEVICE_TABLE(azx_ids) = {
Seth Heasleyd2f2fcd2010-01-12 17:03:35 -08003615 /* CPT */
Takashi Iwai9477c582011-05-25 09:11:37 +02003616 { PCI_DEVICE(0x8086, 0x1c20),
Takashi Iwaid7dab4d2013-01-08 13:51:30 +01003617 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
Seth Heasleycea310e2010-09-10 16:29:56 -07003618 /* PBG */
Takashi Iwai9477c582011-05-25 09:11:37 +02003619 { PCI_DEVICE(0x8086, 0x1d20),
Takashi Iwaid7dab4d2013-01-08 13:51:30 +01003620 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
Seth Heasleyd2edeb72011-04-20 10:59:57 -07003621 /* Panther Point */
Takashi Iwai9477c582011-05-25 09:11:37 +02003622 { PCI_DEVICE(0x8086, 0x1e20),
Takashi Iwaib1920c22013-11-22 12:43:25 +01003623 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
Seth Heasley8bc039a2012-01-23 16:24:31 -08003624 /* Lynx Point */
3625 { PCI_DEVICE(0x8086, 0x8c20),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003626 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston884b0882013-02-08 17:29:40 -08003627 /* Wellsburg */
3628 { PCI_DEVICE(0x8086, 0x8d20),
3629 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
3630 { PCI_DEVICE(0x8086, 0x8d21),
3631 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston144dad92012-08-09 09:38:59 -07003632 /* Lynx Point-LP */
3633 { PCI_DEVICE(0x8086, 0x9c20),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003634 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston144dad92012-08-09 09:38:59 -07003635 /* Lynx Point-LP */
3636 { PCI_DEVICE(0x8086, 0x9c21),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003637 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston4eeca492013-11-04 09:27:45 -08003638 /* Wildcat Point-LP */
3639 { PCI_DEVICE(0x8086, 0x9ca0),
3640 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
Wang Xingchaoe926f2c2012-06-13 10:23:51 +08003641 /* Haswell */
Wang Xingchao4a7c5162013-02-01 22:42:19 +08003642 { PCI_DEVICE(0x8086, 0x0a0c),
Takashi Iwaifab12852013-11-05 17:54:05 +01003643 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Wang Xingchaoe926f2c2012-06-13 10:23:51 +08003644 { PCI_DEVICE(0x8086, 0x0c0c),
Takashi Iwaifab12852013-11-05 17:54:05 +01003645 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Wang Xingchaod279fae2012-09-17 13:10:23 +08003646 { PCI_DEVICE(0x8086, 0x0d0c),
Takashi Iwaifab12852013-11-05 17:54:05 +01003647 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Mengdong Lin862d7612014-01-08 15:55:14 -05003648 /* Broadwell */
3649 { PCI_DEVICE(0x8086, 0x160c),
3650 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Pierre-Louis Bossart99df18b2012-09-21 18:39:07 -05003651 /* 5 Series/3400 */
3652 { PCI_DEVICE(0x8086, 0x3b56),
Takashi Iwai2c1350f2013-02-14 09:44:55 +01003653 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
Takashi Iwaif748abc2013-01-29 10:12:23 +01003654 /* Poulsbo */
Takashi Iwai9477c582011-05-25 09:11:37 +02003655 { PCI_DEVICE(0x8086, 0x811b),
Takashi Iwaif748abc2013-01-29 10:12:23 +01003656 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
3657 /* Oaktrail */
Li Peng09904b92011-12-28 15:17:26 +00003658 { PCI_DEVICE(0x8086, 0x080a),
Takashi Iwaif748abc2013-01-29 10:12:23 +01003659 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
Chew, Chiau Eee44007e2013-05-16 15:36:12 +08003660 /* BayTrail */
3661 { PCI_DEVICE(0x8086, 0x0f04),
3662 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
David Henningsson645e9032011-12-14 15:52:30 +08003663 /* ICH */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003664 { PCI_DEVICE(0x8086, 0x2668),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003665 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3666 AZX_DCAPS_BUFSIZE }, /* ICH6 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003667 { PCI_DEVICE(0x8086, 0x27d8),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003668 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3669 AZX_DCAPS_BUFSIZE }, /* ICH7 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003670 { PCI_DEVICE(0x8086, 0x269a),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003671 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3672 AZX_DCAPS_BUFSIZE }, /* ESB2 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003673 { PCI_DEVICE(0x8086, 0x284b),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003674 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3675 AZX_DCAPS_BUFSIZE }, /* ICH8 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003676 { PCI_DEVICE(0x8086, 0x293e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003677 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3678 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003679 { PCI_DEVICE(0x8086, 0x293f),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003680 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3681 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003682 { PCI_DEVICE(0x8086, 0x3a3e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003683 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3684 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003685 { PCI_DEVICE(0x8086, 0x3a6e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003686 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3687 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwaib6864532010-09-15 10:17:26 +02003688 /* Generic Intel */
3689 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
3690 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3691 .class_mask = 0xffffff,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003692 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_BUFSIZE },
Takashi Iwai9477c582011-05-25 09:11:37 +02003693 /* ATI SB 450/600/700/800/900 */
3694 { PCI_DEVICE(0x1002, 0x437b),
3695 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
3696 { PCI_DEVICE(0x1002, 0x4383),
3697 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
3698 /* AMD Hudson */
3699 { PCI_DEVICE(0x1022, 0x780d),
3700 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB },
Takashi Iwai87218e92008-02-21 08:13:11 +01003701 /* ATI HDMI */
Takashi Iwai9477c582011-05-25 09:11:37 +02003702 { PCI_DEVICE(0x1002, 0x793b),
3703 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3704 { PCI_DEVICE(0x1002, 0x7919),
3705 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3706 { PCI_DEVICE(0x1002, 0x960f),
3707 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3708 { PCI_DEVICE(0x1002, 0x970f),
3709 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3710 { PCI_DEVICE(0x1002, 0xaa00),
3711 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3712 { PCI_DEVICE(0x1002, 0xaa08),
3713 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3714 { PCI_DEVICE(0x1002, 0xaa10),
3715 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3716 { PCI_DEVICE(0x1002, 0xaa18),
3717 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3718 { PCI_DEVICE(0x1002, 0xaa20),
3719 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3720 { PCI_DEVICE(0x1002, 0xaa28),
3721 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3722 { PCI_DEVICE(0x1002, 0xaa30),
3723 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3724 { PCI_DEVICE(0x1002, 0xaa38),
3725 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3726 { PCI_DEVICE(0x1002, 0xaa40),
3727 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3728 { PCI_DEVICE(0x1002, 0xaa48),
3729 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
Clemens Ladischbbaa0d62013-11-05 09:27:10 +01003730 { PCI_DEVICE(0x1002, 0xaa50),
3731 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3732 { PCI_DEVICE(0x1002, 0xaa58),
3733 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3734 { PCI_DEVICE(0x1002, 0xaa60),
3735 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3736 { PCI_DEVICE(0x1002, 0xaa68),
3737 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3738 { PCI_DEVICE(0x1002, 0xaa80),
3739 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3740 { PCI_DEVICE(0x1002, 0xaa88),
3741 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3742 { PCI_DEVICE(0x1002, 0xaa90),
3743 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3744 { PCI_DEVICE(0x1002, 0xaa98),
3745 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Xu1815b342011-12-14 16:10:27 +08003746 { PCI_DEVICE(0x1002, 0x9902),
3747 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3748 { PCI_DEVICE(0x1002, 0xaaa0),
3749 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3750 { PCI_DEVICE(0x1002, 0xaaa8),
3751 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3752 { PCI_DEVICE(0x1002, 0xaab0),
3753 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
Takashi Iwai87218e92008-02-21 08:13:11 +01003754 /* VIA VT8251/VT8237A */
Takashi Iwai9477c582011-05-25 09:11:37 +02003755 { PCI_DEVICE(0x1106, 0x3288),
3756 .driver_data = AZX_DRIVER_VIA | AZX_DCAPS_POSFIX_VIA },
Annie Liu754fdff2012-06-08 19:18:39 +08003757 /* VIA GFX VT7122/VX900 */
3758 { PCI_DEVICE(0x1106, 0x9170), .driver_data = AZX_DRIVER_GENERIC },
3759 /* VIA GFX VT6122/VX11 */
3760 { PCI_DEVICE(0x1106, 0x9140), .driver_data = AZX_DRIVER_GENERIC },
Takashi Iwai87218e92008-02-21 08:13:11 +01003761 /* SIS966 */
3762 { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
3763 /* ULI M5461 */
3764 { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
3765 /* NVIDIA MCP */
Takashi Iwai0c2fd1bf42009-12-18 16:41:39 +01003766 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
3767 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3768 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003769 .driver_data = AZX_DRIVER_NVIDIA | AZX_DCAPS_PRESET_NVIDIA },
Kailang Yangf2690022008-05-27 11:44:55 +02003770 /* Teradici */
Takashi Iwai9477c582011-05-25 09:11:37 +02003771 { PCI_DEVICE(0x6549, 0x1200),
3772 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
Lars R. Damerowf0b3da92012-11-02 13:10:39 -07003773 { PCI_DEVICE(0x6549, 0x2200),
3774 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
Takashi Iwai4e01f542009-04-16 08:53:34 +02003775 /* Creative X-Fi (CA0110-IBG) */
Takashi Iwaif2a8eca2012-06-11 15:51:54 +02003776 /* CTHDA chips */
3777 { PCI_DEVICE(0x1102, 0x0010),
3778 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
3779 { PCI_DEVICE(0x1102, 0x0012),
3780 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +01003781#if !IS_ENABLED(CONFIG_SND_CTXFI)
Takashi Iwai313f6e22009-05-18 12:40:52 +02003782 /* the following entry conflicts with snd-ctxfi driver,
3783 * as ctxfi driver mutates from HD-audio to native mode with
3784 * a special command sequence.
3785 */
Takashi Iwai4e01f542009-04-16 08:53:34 +02003786 { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
3787 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3788 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003789 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01003790 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02003791#else
3792 /* this entry seems still valid -- i.e. without emu20kx chip */
Takashi Iwai9477c582011-05-25 09:11:37 +02003793 { PCI_DEVICE(0x1102, 0x0009),
3794 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01003795 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02003796#endif
Otavio Salvadore35d4b12010-09-26 23:35:06 -03003797 /* Vortex86MX */
3798 { PCI_DEVICE(0x17f3, 0x3010), .driver_data = AZX_DRIVER_GENERIC },
Bankim Bhavsar0f0714c52011-01-17 15:23:21 +01003799 /* VMware HDAudio */
3800 { PCI_DEVICE(0x15ad, 0x1977), .driver_data = AZX_DRIVER_GENERIC },
Andiry Brienza9176b672009-07-17 11:32:32 +08003801 /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
Yang, Libinc4da29c2008-11-13 11:07:07 +01003802 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
3803 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3804 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003805 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Brienza9176b672009-07-17 11:32:32 +08003806 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
3807 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3808 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003809 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 { 0, }
3811};
3812MODULE_DEVICE_TABLE(pci, azx_ids);
3813
3814/* pci_driver definition */
Takashi Iwaie9f66d92012-04-24 12:25:00 +02003815static struct pci_driver azx_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02003816 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 .id_table = azx_ids,
3818 .probe = azx_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05003819 .remove = azx_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003820 .driver = {
3821 .pm = AZX_PM_OPS,
3822 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823};
3824
Takashi Iwaie9f66d92012-04-24 12:25:00 +02003825module_pci_driver(azx_driver);