blob: 7630622e7f0fdfb4888dfee742b74d2ee11a76b5 [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/* for pcm support */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100297#define get_azx_dev(substream) (substream->runtime->private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200299#ifdef CONFIG_X86
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100300static void __mark_pages_wc(struct azx *chip, struct snd_dma_buffer *dmab, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200301{
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100302 int pages;
303
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200304 if (azx_snoop(chip))
305 return;
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100306 if (!dmab || !dmab->area || !dmab->bytes)
307 return;
308
309#ifdef CONFIG_SND_DMA_SGBUF
310 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_SG) {
311 struct snd_sg_buf *sgbuf = dmab->private_data;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200312 if (on)
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100313 set_pages_array_wc(sgbuf->page_table, sgbuf->pages);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200314 else
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100315 set_pages_array_wb(sgbuf->page_table, sgbuf->pages);
316 return;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200317 }
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100318#endif
319
320 pages = (dmab->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
321 if (on)
322 set_memory_wc((unsigned long)dmab->area, pages);
323 else
324 set_memory_wb((unsigned long)dmab->area, pages);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200325}
326
327static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
328 bool on)
329{
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100330 __mark_pages_wc(chip, buf, on);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200331}
332static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100333 struct snd_pcm_substream *substream, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200334{
335 if (azx_dev->wc_marked != on) {
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100336 __mark_pages_wc(chip, snd_pcm_get_dma_buf(substream), on);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200337 azx_dev->wc_marked = on;
338 }
339}
340#else
341/* NOP for other archs */
342static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
343 bool on)
344{
345}
346static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100347 struct snd_pcm_substream *substream, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200348{
349}
350#endif
351
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200352static int azx_acquire_irq(struct azx *chip, int do_disconnect);
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200353static int azx_send_cmd(struct hda_bus *bus, unsigned int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/*
355 * Interface for HD codec
356 */
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/*
359 * CORB / RIRB interface
360 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100361static int azx_alloc_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int err;
364
365 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200366 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
Dylan Reid89287562014-02-28 15:41:15 -0800367 chip->card->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 PAGE_SIZE, &chip->rb);
369 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100370 dev_err(chip->card->dev, "cannot allocate CORB/RIRB\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return err;
372 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200373 mark_pages_wc(chip, &chip->rb, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
376
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100377static void azx_init_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
David Henningssonca460f82014-02-28 07:56:58 +0100379 int timeout;
380
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800381 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* CORB set up */
383 chip->corb.addr = chip->rb.addr;
384 chip->corb.buf = (u32 *)chip->rb.area;
385 azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200386 azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200388 /* set the corb size to 256 entries (ULI requires explicitly) */
389 azx_writeb(chip, CORBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* set the corb write pointer to 0 */
391 azx_writew(chip, CORBWP, 0);
David Henningssonca460f82014-02-28 07:56:58 +0100392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* reset the corb hw read pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200394 azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
David Henningssonca460f82014-02-28 07:56:58 +0100395 for (timeout = 1000; timeout > 0; timeout--) {
396 if ((azx_readw(chip, CORBRP) & ICH6_CORBRP_RST) == ICH6_CORBRP_RST)
397 break;
398 udelay(1);
399 }
400 if (timeout <= 0)
401 dev_err(chip->card->dev, "CORB reset timeout#1, CORBRP = %d\n",
402 azx_readw(chip, CORBRP));
403
404 azx_writew(chip, CORBRP, 0);
405 for (timeout = 1000; timeout > 0; timeout--) {
406 if (azx_readw(chip, CORBRP) == 0)
407 break;
408 udelay(1);
409 }
410 if (timeout <= 0)
411 dev_err(chip->card->dev, "CORB reset timeout#2, CORBRP = %d\n",
412 azx_readw(chip, CORBRP));
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* enable corb dma */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200415 azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* RIRB set up */
418 chip->rirb.addr = chip->rb.addr + 2048;
419 chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800420 chip->rirb.wp = chip->rirb.rp = 0;
421 memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200423 azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200425 /* set the rirb size to 256 entries (ULI requires explicitly) */
426 azx_writeb(chip, RIRBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* reset the rirb hw write pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200428 azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* set N=1, get RIRB response interrupt for new entry */
Takashi Iwai9477c582011-05-25 09:11:37 +0200430 if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
Takashi Iwai14d34f12010-10-21 09:03:25 +0200431 azx_writew(chip, RINTCNT, 0xc0);
432 else
433 azx_writew(chip, RINTCNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* enable rirb dma and response irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800436 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100439static void azx_free_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800441 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* disable ringbuffer DMAs */
443 azx_writeb(chip, RIRBCTL, 0);
444 azx_writeb(chip, CORBCTL, 0);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800445 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Wu Fengguangdeadff12009-08-01 18:45:16 +0800448static unsigned int azx_command_addr(u32 cmd)
449{
450 unsigned int addr = cmd >> 28;
451
452 if (addr >= AZX_MAX_CODECS) {
453 snd_BUG();
454 addr = 0;
455 }
456
457 return addr;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100461static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100463 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800464 unsigned int addr = azx_command_addr(val);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100465 unsigned int wp, rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Wu Fengguangc32649f2009-08-01 18:48:12 +0800467 spin_lock_irq(&chip->reg_lock);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* add command to corb */
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100470 wp = azx_readw(chip, CORBWP);
471 if (wp == 0xffff) {
472 /* something wrong, controller likely turned to D3 */
473 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100474 return -EIO;
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 wp++;
477 wp %= ICH6_MAX_CORB_ENTRIES;
478
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100479 rp = azx_readw(chip, CORBRP);
480 if (wp == rp) {
481 /* oops, it's full */
482 spin_unlock_irq(&chip->reg_lock);
483 return -EAGAIN;
484 }
485
Wu Fengguangdeadff12009-08-01 18:45:16 +0800486 chip->rirb.cmds[addr]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 chip->corb.buf[wp] = cpu_to_le32(val);
David Henningssonca460f82014-02-28 07:56:58 +0100488 azx_writew(chip, CORBWP, wp);
Wu Fengguangc32649f2009-08-01 18:48:12 +0800489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 spin_unlock_irq(&chip->reg_lock);
491
492 return 0;
493}
494
495#define ICH6_RIRB_EX_UNSOL_EV (1<<4)
496
497/* retrieve RIRB entry - called from interrupt handler */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100498static void azx_update_rirb(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 unsigned int rp, wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800501 unsigned int addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 u32 res, res_ex;
503
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100504 wp = azx_readw(chip, RIRBWP);
505 if (wp == 0xffff) {
506 /* something wrong, controller likely turned to D3 */
507 return;
508 }
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (wp == chip->rirb.wp)
511 return;
512 chip->rirb.wp = wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 while (chip->rirb.rp != wp) {
515 chip->rirb.rp++;
516 chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
517
518 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
519 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
520 res = le32_to_cpu(chip->rirb.buf[rp]);
David Henningsson3d692452014-01-29 13:12:31 +0100521 addr = res_ex & 0xf;
522 if ((addr >= AZX_MAX_CODECS) || !(chip->codec_mask & (1 << addr))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100523 dev_err(chip->card->dev, "spurious response %#x:%#x, rp = %d, wp = %d",
524 res, res_ex,
525 chip->rirb.rp, wp);
David Henningsson3d692452014-01-29 13:12:31 +0100526 snd_BUG();
527 }
528 else if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 snd_hda_queue_unsol_event(chip->bus, res, res_ex);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800530 else if (chip->rirb.cmds[addr]) {
531 chip->rirb.res[addr] = res;
Takashi Iwai2add9b92008-03-18 09:47:06 +0100532 smp_wmb();
Wu Fengguangdeadff12009-08-01 18:45:16 +0800533 chip->rirb.cmds[addr]--;
Joe Perches3b70a672013-11-07 11:55:15 -0800534 } else if (printk_ratelimit()) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100535 dev_err(chip->card->dev, "spurious response %#x:%#x, last cmd=%#08x\n",
536 res, res_ex,
537 chip->last_cmd[addr]);
Joe Perches3b70a672013-11-07 11:55:15 -0800538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540}
541
542/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800543static unsigned int azx_rirb_get_response(struct hda_bus *bus,
544 unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100546 struct azx *chip = bus->private_data;
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200547 unsigned long timeout;
David Henningsson32cf4022012-05-04 11:05:55 +0200548 unsigned long loopcounter;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200549 int do_poll = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200551 again:
552 timeout = jiffies + msecs_to_jiffies(1000);
David Henningsson32cf4022012-05-04 11:05:55 +0200553
554 for (loopcounter = 0;; loopcounter++) {
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200555 if (chip->polling_mode || do_poll) {
Takashi Iwaie96224a2006-08-21 17:57:44 +0200556 spin_lock_irq(&chip->reg_lock);
557 azx_update_rirb(chip);
558 spin_unlock_irq(&chip->reg_lock);
559 }
Wu Fengguangdeadff12009-08-01 18:45:16 +0800560 if (!chip->rirb.cmds[addr]) {
Takashi Iwai2add9b92008-03-18 09:47:06 +0100561 smp_rmb();
Takashi Iwaib6132912009-03-24 07:36:09 +0100562 bus->rirb_error = 0;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200563
564 if (!do_poll)
565 chip->poll_count = 0;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800566 return chip->rirb.res[addr]; /* the last value */
Takashi Iwai2add9b92008-03-18 09:47:06 +0100567 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100568 if (time_after(jiffies, timeout))
569 break;
David Henningsson32cf4022012-05-04 11:05:55 +0200570 if (bus->needs_damn_long_delay || loopcounter > 3000)
Takashi Iwai52987652008-01-16 16:09:47 +0100571 msleep(2); /* temporary workaround */
572 else {
573 udelay(10);
574 cond_resched();
575 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100576 }
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200577
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200578 if (!bus->no_response_fallback)
579 return -1;
580
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200581 if (!chip->polling_mode && chip->poll_count < 2) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100582 dev_dbg(chip->card->dev,
583 "azx_get_response timeout, polling the codec once: last cmd=0x%08x\n",
584 chip->last_cmd[addr]);
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200585 do_poll = 1;
586 chip->poll_count++;
587 goto again;
588 }
589
590
Takashi Iwai23c4a882009-10-30 13:21:49 +0100591 if (!chip->polling_mode) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100592 dev_warn(chip->card->dev,
593 "azx_get_response timeout, switching to polling mode: last cmd=0x%08x\n",
594 chip->last_cmd[addr]);
Takashi Iwai23c4a882009-10-30 13:21:49 +0100595 chip->polling_mode = 1;
596 goto again;
597 }
598
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200599 if (chip->msi) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100600 dev_warn(chip->card->dev,
601 "No response from codec, disabling MSI: last cmd=0x%08x\n",
602 chip->last_cmd[addr]);
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200603 free_irq(chip->irq, chip);
604 chip->irq = -1;
605 pci_disable_msi(chip->pci);
606 chip->msi = 0;
Takashi Iwaib6132912009-03-24 07:36:09 +0100607 if (azx_acquire_irq(chip, 1) < 0) {
608 bus->rirb_error = 1;
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200609 return -1;
Takashi Iwaib6132912009-03-24 07:36:09 +0100610 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200611 goto again;
612 }
613
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +0100614 if (chip->probing) {
615 /* If this critical timeout happens during the codec probing
616 * phase, this is likely an access to a non-existing codec
617 * slot. Better to return an error and reset the system.
618 */
619 return -1;
620 }
621
Takashi Iwai8dd78332009-06-02 01:16:07 +0200622 /* a fatal communication error; need either to reset or to fallback
623 * to the single_cmd mode
624 */
Takashi Iwaib6132912009-03-24 07:36:09 +0100625 bus->rirb_error = 1;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200626 if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200627 bus->response_reset = 1;
628 return -1; /* give a chance to retry */
629 }
630
Takashi Iwai4e76a882014-02-25 12:21:03 +0100631 dev_err(chip->card->dev,
632 "azx_get_response timeout, switching to single_cmd mode: last cmd=0x%08x\n",
633 chip->last_cmd[addr]);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200634 chip->single_cmd = 1;
635 bus->response_reset = 0;
Takashi Iwai1a696972009-11-07 09:49:04 +0100636 /* release CORB/RIRB */
Takashi Iwai4fcd3922009-05-25 18:34:52 +0200637 azx_free_cmd_io(chip);
Takashi Iwai1a696972009-11-07 09:49:04 +0100638 /* disable unsolicited responses */
639 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200640 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/*
644 * Use the single immediate command instead of CORB/RIRB for simplicity
645 *
646 * Note: according to Intel, this is not preferred use. The command was
647 * intended for the BIOS only, and may get confused with unsolicited
648 * responses. So, we shouldn't use it for normal operation from the
649 * driver.
650 * I left the codes, however, for debugging/testing purposes.
651 */
652
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200653/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800654static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200655{
656 int timeout = 50;
657
658 while (timeout--) {
659 /* check IRV busy bit */
660 if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
661 /* reuse rirb.res as the response return value */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800662 chip->rirb.res[addr] = azx_readl(chip, IR);
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200663 return 0;
664 }
665 udelay(1);
666 }
667 if (printk_ratelimit())
Takashi Iwai4e76a882014-02-25 12:21:03 +0100668 dev_dbg(chip->card->dev, "get_response timeout: IRS=0x%x\n",
669 azx_readw(chip, IRS));
Wu Fengguangdeadff12009-08-01 18:45:16 +0800670 chip->rirb.res[addr] = -1;
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200671 return -EIO;
672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100675static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100677 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800678 unsigned int addr = azx_command_addr(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int timeout = 50;
680
Takashi Iwai8dd78332009-06-02 01:16:07 +0200681 bus->rirb_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 while (timeout--) {
683 /* check ICB busy bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200684 if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* Clear IRV valid bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200686 azx_writew(chip, IRS, azx_readw(chip, IRS) |
687 ICH6_IRS_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 azx_writel(chip, IC, val);
Takashi Iwaid01ce992007-07-27 16:52:19 +0200689 azx_writew(chip, IRS, azx_readw(chip, IRS) |
690 ICH6_IRS_BUSY);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800691 return azx_single_wait_for_response(chip, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 udelay(1);
694 }
Marc Boucher1cfd52b2008-01-22 15:29:26 +0100695 if (printk_ratelimit())
Takashi Iwai4e76a882014-02-25 12:21:03 +0100696 dev_dbg(chip->card->dev,
697 "send_cmd timeout: IRS=0x%x, val=0x%x\n",
698 azx_readw(chip, IRS), val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -EIO;
700}
701
702/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800703static unsigned int azx_single_get_response(struct hda_bus *bus,
704 unsigned int addr)
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 return chip->rirb.res[addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Takashi Iwai111d3af2006-02-16 18:17:58 +0100710/*
711 * The below are the main callbacks from hda_codec.
712 *
713 * They are just the skeleton to call sub-callbacks according to the
714 * current setting of chip->single_cmd.
715 */
716
717/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100718static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
Takashi Iwai111d3af2006-02-16 18:17:58 +0100719{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100720 struct azx *chip = bus->private_data;
Takashi Iwai43bbb6c2007-07-06 20:22:05 +0200721
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200722 if (chip->disabled)
723 return 0;
Wu Fengguangfeb27342009-08-01 19:17:14 +0800724 chip->last_cmd[azx_command_addr(val)] = val;
Takashi Iwai111d3af2006-02-16 18:17:58 +0100725 if (chip->single_cmd)
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100726 return azx_single_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100727 else
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100728 return azx_corb_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100729}
730
731/* get a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800732static unsigned int azx_get_response(struct hda_bus *bus,
733 unsigned int addr)
Takashi Iwai111d3af2006-02-16 18:17:58 +0100734{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100735 struct azx *chip = bus->private_data;
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200736 if (chip->disabled)
737 return 0;
Takashi Iwai111d3af2006-02-16 18:17:58 +0100738 if (chip->single_cmd)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800739 return azx_single_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100740 else
Wu Fengguangdeadff12009-08-01 18:45:16 +0800741 return azx_rirb_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100742}
743
Takashi Iwai83012a72012-08-24 18:38:08 +0200744#ifdef CONFIG_PM
Takashi Iwai68467f52012-08-28 09:14:29 -0700745static void azx_power_notify(struct hda_bus *bus, bool power_up);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200746#endif
Takashi Iwai111d3af2006-02-16 18:17:58 +0100747
Takashi Iwai1d1a4562012-09-20 20:29:13 -0700748#ifdef CONFIG_SND_HDA_DSP_LOADER
749static int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
750 unsigned int byte_size,
751 struct snd_dma_buffer *bufp);
752static void azx_load_dsp_trigger(struct hda_bus *bus, bool start);
753static void azx_load_dsp_cleanup(struct hda_bus *bus,
754 struct snd_dma_buffer *dmab);
755#endif
756
Mengdong Lin3af3f352013-06-24 10:18:54 -0400757/* enter link reset */
Mengdong Lin7295b262013-06-25 05:58:49 -0400758static void azx_enter_link_reset(struct azx *chip)
Mengdong Lin3af3f352013-06-24 10:18:54 -0400759{
760 unsigned long timeout;
761
762 /* reset controller */
763 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
764
765 timeout = jiffies + msecs_to_jiffies(100);
766 while ((azx_readb(chip, GCTL) & ICH6_GCTL_RESET) &&
767 time_before(jiffies, timeout))
768 usleep_range(500, 1000);
769}
770
Mengdong Lin7295b262013-06-25 05:58:49 -0400771/* exit link reset */
772static void azx_exit_link_reset(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Mengdong Linfa348da2012-12-12 09:16:15 -0500774 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Mengdong Lin7295b262013-06-25 05:58:49 -0400776 azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
777
778 timeout = jiffies + msecs_to_jiffies(100);
779 while (!azx_readb(chip, GCTL) &&
780 time_before(jiffies, timeout))
781 usleep_range(500, 1000);
782}
783
784/* reset codec link */
785static int azx_reset(struct azx *chip, int full_reset)
786{
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100787 if (!full_reset)
788 goto __skip;
789
Danny Tholene8a7f132007-09-11 21:41:56 +0200790 /* clear STATESTS */
Wang Xingchaoda7db6a2013-07-22 03:19:18 -0400791 azx_writew(chip, STATESTS, STATESTS_INT_MASK);
Danny Tholene8a7f132007-09-11 21:41:56 +0200792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* reset controller */
Mengdong Lin7295b262013-06-25 05:58:49 -0400794 azx_enter_link_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* delay for >= 100us for codec PLL to settle per spec
797 * Rev 0.9 section 5.5.1
798 */
Mengdong Linfa348da2012-12-12 09:16:15 -0500799 usleep_range(500, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 /* Bring controller out of reset */
Mengdong Lin7295b262013-06-25 05:58:49 -0400802 azx_exit_link_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Pavel Machek927fc862006-08-31 17:03:43 +0200804 /* Brent Chartrand said to wait >= 540us for codecs to initialize */
Mengdong Linfa348da2012-12-12 09:16:15 -0500805 usleep_range(1000, 1200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100807 __skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* check to see if controller is ready */
Pavel Machek927fc862006-08-31 17:03:43 +0200809 if (!azx_readb(chip, GCTL)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100810 dev_dbg(chip->card->dev, "azx_reset: controller not ready!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return -EBUSY;
812 }
813
Matt41e2fce2005-07-04 17:49:55 +0200814 /* Accept unsolicited responses */
Takashi Iwai1a696972009-11-07 09:49:04 +0100815 if (!chip->single_cmd)
816 azx_writel(chip, GCTL, azx_readl(chip, GCTL) |
817 ICH6_GCTL_UNSOL);
Matt41e2fce2005-07-04 17:49:55 +0200818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* detect codecs */
Pavel Machek927fc862006-08-31 17:03:43 +0200820 if (!chip->codec_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 chip->codec_mask = azx_readw(chip, STATESTS);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100822 dev_dbg(chip->card->dev, "codec_mask = 0x%x\n",
823 chip->codec_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
826 return 0;
827}
828
829
830/*
831 * Lowlevel interface
832 */
833
834/* enable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100835static void azx_int_enable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 /* enable controller CIE and GIE */
838 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
839 ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
840}
841
842/* disable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100843static void azx_int_disable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 int i;
846
847 /* disable interrupts in stream descriptor */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200848 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100849 struct azx_dev *azx_dev = &chip->azx_dev[i];
Dylan Reid40830812014-02-28 15:41:13 -0800850 azx_sd_writeb(chip, azx_dev, SD_CTL,
851 azx_sd_readb(chip, azx_dev, SD_CTL) &
852 ~SD_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
855 /* disable SIE for all streams */
856 azx_writeb(chip, INTCTL, 0);
857
858 /* disable controller CIE and GIE */
859 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
860 ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
861}
862
863/* clear interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100864static void azx_int_clear(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 int i;
867
868 /* clear stream status */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200869 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100870 struct azx_dev *azx_dev = &chip->azx_dev[i];
Dylan Reid40830812014-02-28 15:41:13 -0800871 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
874 /* clear STATESTS */
Wang Xingchaoda7db6a2013-07-22 03:19:18 -0400875 azx_writew(chip, STATESTS, STATESTS_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /* clear rirb status */
878 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
879
880 /* clear int status */
881 azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
882}
883
884/* start a stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100885static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Joseph Chan0e153472008-08-26 14:38:03 +0200887 /*
888 * Before stream start, initialize parameter
889 */
890 azx_dev->insufficient = 1;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* enable SIE */
Wei Niccc5df02010-01-26 15:59:33 +0800893 azx_writel(chip, INTCTL,
894 azx_readl(chip, INTCTL) | (1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* set DMA start and interrupt mask */
Dylan Reid40830812014-02-28 15:41:13 -0800896 azx_sd_writeb(chip, azx_dev, SD_CTL,
897 azx_sd_readb(chip, azx_dev, SD_CTL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 SD_CTL_DMA_START | SD_INT_MASK);
899}
900
Takashi Iwai1dddab42009-03-18 15:15:37 +0100901/* stop DMA */
902static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Dylan Reid40830812014-02-28 15:41:13 -0800904 azx_sd_writeb(chip, azx_dev, SD_CTL,
905 azx_sd_readb(chip, azx_dev, SD_CTL) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 ~(SD_CTL_DMA_START | SD_INT_MASK));
Dylan Reid40830812014-02-28 15:41:13 -0800907 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
Takashi Iwai1dddab42009-03-18 15:15:37 +0100908}
909
910/* stop a stream */
911static void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
912{
913 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* disable SIE */
Wei Niccc5df02010-01-26 15:59:33 +0800915 azx_writel(chip, INTCTL,
916 azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919
920/*
Takashi Iwaicb53c622007-08-10 17:21:45 +0200921 * reset and start the controller registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100923static void azx_init_chip(struct azx *chip, int full_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Takashi Iwaicb53c622007-08-10 17:21:45 +0200925 if (chip->initialized)
926 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /* reset controller */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100929 azx_reset(chip, full_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 /* initialize interrupts */
932 azx_int_clear(chip);
933 azx_int_enable(chip);
934
935 /* initialize the codec command I/O */
Takashi Iwai1a696972009-11-07 09:49:04 +0100936 if (!chip->single_cmd)
937 azx_init_cmd_io(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Takashi Iwai0be3b5d2005-09-05 17:11:40 +0200939 /* program the position buffer */
940 azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200941 azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr));
Frederick Lif5d40b32005-05-12 14:55:20 +0200942
Takashi Iwaicb53c622007-08-10 17:21:45 +0200943 chip->initialized = 1;
944}
945
946/*
947 * initialize the PCI registers
948 */
949/* update bits in a PCI register byte */
950static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
951 unsigned char mask, unsigned char val)
952{
953 unsigned char data;
954
955 pci_read_config_byte(pci, reg, &data);
956 data &= ~mask;
957 data |= (val & mask);
958 pci_write_config_byte(pci, reg, data);
959}
960
961static void azx_init_pci(struct azx *chip)
962{
963 /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
964 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
965 * Ensuring these bits are 0 clears playback static on some HD Audio
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +0100966 * codecs.
967 * The PCI register TCSEL is defined in the Intel manuals.
Takashi Iwaicb53c622007-08-10 17:21:45 +0200968 */
Linus Torvalds46f2cc82011-05-27 19:45:28 -0700969 if (!(chip->driver_caps & AZX_DCAPS_NO_TCSEL)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100970 dev_dbg(chip->card->dev, "Clearing TCSEL\n");
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +0100971 update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
Takashi Iwai9477c582011-05-25 09:11:37 +0200972 }
Takashi Iwaicb53c622007-08-10 17:21:45 +0200973
Takashi Iwai9477c582011-05-25 09:11:37 +0200974 /* For ATI SB450/600/700/800/900 and AMD Hudson azalia HD audio,
975 * we need to enable snoop.
976 */
977 if (chip->driver_caps & AZX_DCAPS_ATI_SNOOP) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100978 dev_dbg(chip->card->dev, "Setting ATI snoop: %d\n",
979 azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +0200980 update_pci_byte(chip->pci,
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200981 ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR, 0x07,
982 azx_snoop(chip) ? ATI_SB450_HDAUDIO_ENABLE_SNOOP : 0);
Takashi Iwai9477c582011-05-25 09:11:37 +0200983 }
984
985 /* For NVIDIA HDA, enable snoop */
986 if (chip->driver_caps & AZX_DCAPS_NVIDIA_SNOOP) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100987 dev_dbg(chip->card->dev, "Setting Nvidia snoop: %d\n",
988 azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +0200989 update_pci_byte(chip->pci,
990 NVIDIA_HDA_TRANSREG_ADDR,
991 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
Peer Chen320dcc32008-08-20 16:43:24 -0700992 update_pci_byte(chip->pci,
993 NVIDIA_HDA_ISTRM_COH,
994 0x01, NVIDIA_HDA_ENABLE_COHBIT);
995 update_pci_byte(chip->pci,
996 NVIDIA_HDA_OSTRM_COH,
997 0x01, NVIDIA_HDA_ENABLE_COHBIT);
Takashi Iwai9477c582011-05-25 09:11:37 +0200998 }
999
1000 /* Enable SCH/PCH snoop if needed */
1001 if (chip->driver_caps & AZX_DCAPS_SCH_SNOOP) {
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001002 unsigned short snoop;
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001003 pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001004 if ((!azx_snoop(chip) && !(snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)) ||
1005 (azx_snoop(chip) && (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP))) {
1006 snoop &= ~INTEL_SCH_HDA_DEVC_NOSNOOP;
1007 if (!azx_snoop(chip))
1008 snoop |= INTEL_SCH_HDA_DEVC_NOSNOOP;
1009 pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC, snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001010 pci_read_config_word(chip->pci,
1011 INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001012 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001013 dev_dbg(chip->card->dev, "SCH snoop: %s\n",
1014 (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) ?
1015 "Disabled" : "Enabled");
Vinod Gda3fca22005-09-13 18:49:12 +02001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001020static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
1023 * interrupt handler
1024 */
David Howells7d12e782006-10-05 14:55:46 +01001025static irqreturn_t azx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001027 struct azx *chip = dev_id;
1028 struct azx_dev *azx_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 u32 status;
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001030 u8 sd_status;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001031 int i, ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001033#ifdef CONFIG_PM_RUNTIME
Dave Airlie246efa42013-07-29 15:19:29 +10001034 if (chip->driver_caps & AZX_DCAPS_PM_RUNTIME)
Dylan Reid89287562014-02-28 15:41:15 -08001035 if (chip->card->dev->power.runtime_status != RPM_ACTIVE)
Dave Airlie246efa42013-07-29 15:19:29 +10001036 return IRQ_NONE;
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001037#endif
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 spin_lock(&chip->reg_lock);
1040
Dan Carpenter60911062012-05-18 10:36:11 +03001041 if (chip->disabled) {
1042 spin_unlock(&chip->reg_lock);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001043 return IRQ_NONE;
Dan Carpenter60911062012-05-18 10:36:11 +03001044 }
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 status = azx_readl(chip, INTSTS);
Dave Airlie246efa42013-07-29 15:19:29 +10001047 if (status == 0 || status == 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 spin_unlock(&chip->reg_lock);
1049 return IRQ_NONE;
1050 }
1051
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001052 for (i = 0; i < chip->num_streams; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 azx_dev = &chip->azx_dev[i];
1054 if (status & azx_dev->sd_int_sta_mask) {
Dylan Reid40830812014-02-28 15:41:13 -08001055 sd_status = azx_sd_readb(chip, azx_dev, SD_STS);
1056 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK);
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001057 if (!azx_dev->substream || !azx_dev->running ||
1058 !(sd_status & SD_INT_COMPLETE))
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001059 continue;
1060 /* check whether this IRQ is really acceptable */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001061 ok = azx_position_ok(chip, azx_dev);
1062 if (ok == 1) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001063 azx_dev->irq_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_unlock(&chip->reg_lock);
1065 snd_pcm_period_elapsed(azx_dev->substream);
1066 spin_lock(&chip->reg_lock);
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001067 } else if (ok == 0 && chip->bus && chip->bus->workq) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001068 /* bogus IRQ, process it later */
1069 azx_dev->irq_pending = 1;
Takashi Iwai6acaed32009-01-12 10:09:24 +01001070 queue_work(chip->bus->workq,
1071 &chip->irq_pending_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 }
1074 }
1075
1076 /* clear rirb int */
1077 status = azx_readb(chip, RIRBSTS);
1078 if (status & RIRB_INT_MASK) {
Takashi Iwai14d34f12010-10-21 09:03:25 +02001079 if (status & RIRB_INT_RESPONSE) {
Takashi Iwai9477c582011-05-25 09:11:37 +02001080 if (chip->driver_caps & AZX_DCAPS_RIRB_PRE_DELAY)
Takashi Iwai14d34f12010-10-21 09:03:25 +02001081 udelay(80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 azx_update_rirb(chip);
Takashi Iwai14d34f12010-10-21 09:03:25 +02001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1085 }
1086
1087#if 0
1088 /* clear state status int */
Wang Xingchaoda7db6a2013-07-22 03:19:18 -04001089 if (azx_readw(chip, STATESTS) & 0x04)
1090 azx_writew(chip, STATESTS, 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091#endif
1092 spin_unlock(&chip->reg_lock);
1093
1094 return IRQ_HANDLED;
1095}
1096
1097
1098/*
Takashi Iwai675f25d2008-06-10 17:53:20 +02001099 * set up a BDL entry
1100 */
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001101static int setup_bdle(struct azx *chip,
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001102 struct snd_dma_buffer *dmab,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001103 struct azx_dev *azx_dev, u32 **bdlp,
1104 int ofs, int size, int with_ioc)
1105{
Takashi Iwai675f25d2008-06-10 17:53:20 +02001106 u32 *bdl = *bdlp;
1107
1108 while (size > 0) {
1109 dma_addr_t addr;
1110 int chunk;
1111
1112 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
1113 return -EINVAL;
1114
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001115 addr = snd_sgbuf_get_addr(dmab, ofs);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001116 /* program the address field of the BDL entry */
1117 bdl[0] = cpu_to_le32((u32)addr);
Takashi Iwai766979e2008-06-13 20:53:56 +02001118 bdl[1] = cpu_to_le32(upper_32_bits(addr));
Takashi Iwai675f25d2008-06-10 17:53:20 +02001119 /* program the size field of the BDL entry */
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001120 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001121 /* one BDLE cannot cross 4K boundary on CTHDA chips */
1122 if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY) {
1123 u32 remain = 0x1000 - (ofs & 0xfff);
1124 if (chunk > remain)
1125 chunk = remain;
1126 }
Takashi Iwai675f25d2008-06-10 17:53:20 +02001127 bdl[2] = cpu_to_le32(chunk);
1128 /* program the IOC to enable interrupt
1129 * only when the whole fragment is processed
1130 */
1131 size -= chunk;
1132 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
1133 bdl += 4;
1134 azx_dev->frags++;
1135 ofs += chunk;
1136 }
1137 *bdlp = bdl;
1138 return ofs;
1139}
1140
1141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * set up BDL entries
1143 */
Takashi Iwai555e2192008-06-10 17:53:34 +02001144static int azx_setup_periods(struct azx *chip,
1145 struct snd_pcm_substream *substream,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001146 struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001148 u32 *bdl;
1149 int i, ofs, periods, period_bytes;
Dylan Reid9cdc0112014-02-28 15:41:14 -08001150 int pos_adj = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /* reset BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08001153 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
1154 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Takashi Iwai97b71c92009-03-18 15:09:13 +01001156 period_bytes = azx_dev->period_bytes;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001157 periods = azx_dev->bufsize / period_bytes;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* program the initial BDL entries */
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001160 bdl = (u32 *)azx_dev->bdl.area;
1161 ofs = 0;
1162 azx_dev->frags = 0;
Dylan Reid9cdc0112014-02-28 15:41:14 -08001163
1164 if (chip->bdl_pos_adj)
1165 pos_adj = chip->bdl_pos_adj[chip->dev_index];
Takashi Iwai915bf292012-09-11 15:19:10 +02001166 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
Takashi Iwai675f25d2008-06-10 17:53:20 +02001167 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001168 int pos_align = pos_adj;
Takashi Iwai555e2192008-06-10 17:53:34 +02001169 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001170 if (!pos_adj)
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001171 pos_adj = pos_align;
1172 else
1173 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
1174 pos_align;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001175 pos_adj = frames_to_bytes(runtime, pos_adj);
1176 if (pos_adj >= period_bytes) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001177 dev_warn(chip->card->dev,"Too big adjustment %d\n",
Dylan Reid9cdc0112014-02-28 15:41:14 -08001178 pos_adj);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001179 pos_adj = 0;
1180 } else {
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001181 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1182 azx_dev,
Takashi Iwai915bf292012-09-11 15:19:10 +02001183 &bdl, ofs, pos_adj, true);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001184 if (ofs < 0)
1185 goto error;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001186 }
Takashi Iwai555e2192008-06-10 17:53:34 +02001187 } else
1188 pos_adj = 0;
Dylan Reid9cdc0112014-02-28 15:41:14 -08001189
Takashi Iwai675f25d2008-06-10 17:53:20 +02001190 for (i = 0; i < periods; i++) {
1191 if (i == periods - 1 && pos_adj)
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001192 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1193 azx_dev, &bdl, ofs,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001194 period_bytes - pos_adj, 0);
1195 else
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001196 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1197 azx_dev, &bdl, ofs,
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001198 period_bytes,
Takashi Iwai915bf292012-09-11 15:19:10 +02001199 !azx_dev->no_period_wakeup);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001200 if (ofs < 0)
1201 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001203 return 0;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001204
1205 error:
Takashi Iwai4e76a882014-02-25 12:21:03 +01001206 dev_err(chip->card->dev, "Too many BDL entries: buffer=%d, period=%d\n",
1207 azx_dev->bufsize, period_bytes);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001208 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Takashi Iwai1dddab42009-03-18 15:15:37 +01001211/* reset stream */
1212static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 unsigned char val;
1215 int timeout;
1216
Takashi Iwai1dddab42009-03-18 15:15:37 +01001217 azx_stream_clear(chip, azx_dev);
1218
Dylan Reid40830812014-02-28 15:41:13 -08001219 azx_sd_writeb(chip, azx_dev, SD_CTL,
1220 azx_sd_readb(chip, azx_dev, SD_CTL) |
Takashi Iwaid01ce992007-07-27 16:52:19 +02001221 SD_CTL_STREAM_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 udelay(3);
1223 timeout = 300;
Dylan Reid40830812014-02-28 15:41:13 -08001224 while (!((val = azx_sd_readb(chip, azx_dev, SD_CTL)) &
1225 SD_CTL_STREAM_RESET) && --timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 ;
1227 val &= ~SD_CTL_STREAM_RESET;
Dylan Reid40830812014-02-28 15:41:13 -08001228 azx_sd_writeb(chip, azx_dev, SD_CTL, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 udelay(3);
1230
1231 timeout = 300;
1232 /* waiting for hardware to report that the stream is out of reset */
Dylan Reid40830812014-02-28 15:41:13 -08001233 while (((val = azx_sd_readb(chip, azx_dev, SD_CTL)) &
1234 SD_CTL_STREAM_RESET) && --timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001236
1237 /* reset first position - may not be synced with hw at this time */
1238 *azx_dev->posbuf = 0;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Takashi Iwai1dddab42009-03-18 15:15:37 +01001241/*
1242 * set up the SD for streaming
1243 */
1244static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
1245{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001246 unsigned int val;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001247 /* make sure the run bit is zero for SD */
1248 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* program the stream_tag */
Dylan Reid40830812014-02-28 15:41:13 -08001250 val = azx_sd_readl(chip, azx_dev, SD_CTL);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001251 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
1252 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
1253 if (!azx_snoop(chip))
1254 val |= SD_CTL_TRAFFIC_PRIO;
Dylan Reid40830812014-02-28 15:41:13 -08001255 azx_sd_writel(chip, azx_dev, SD_CTL, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* program the length of samples in cyclic buffer */
Dylan Reid40830812014-02-28 15:41:13 -08001258 azx_sd_writel(chip, azx_dev, SD_CBL, azx_dev->bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /* program the stream format */
1261 /* this value needs to be the same as the one programmed */
Dylan Reid40830812014-02-28 15:41:13 -08001262 azx_sd_writew(chip, azx_dev, SD_FORMAT, azx_dev->format_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* program the stream LVI (last valid index) of the BDL */
Dylan Reid40830812014-02-28 15:41:13 -08001265 azx_sd_writew(chip, azx_dev, SD_LVI, azx_dev->frags - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /* program the BDL address */
1268 /* lower BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08001269 azx_sd_writel(chip, azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* upper BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08001271 azx_sd_writel(chip, azx_dev, SD_BDLPU,
1272 upper_32_bits(azx_dev->bdl.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02001274 /* enable the position buffer */
David Henningsson4cb36312010-09-30 10:12:50 +02001275 if (chip->position_fix[0] != POS_FIX_LPIB ||
1276 chip->position_fix[1] != POS_FIX_LPIB) {
Takashi Iwaiee9d6b92008-03-14 15:52:20 +01001277 if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
1278 azx_writel(chip, DPLBASE,
1279 (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
1280 }
Takashi Iwaic74db862005-05-12 14:26:27 +02001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /* set the interrupt enable bits in the descriptor control register */
Dylan Reid40830812014-02-28 15:41:13 -08001283 azx_sd_writel(chip, azx_dev, SD_CTL,
1284 azx_sd_readl(chip, azx_dev, SD_CTL) | SD_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 return 0;
1287}
1288
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001289/*
1290 * Probe the given codec address
1291 */
1292static int probe_codec(struct azx *chip, int addr)
1293{
1294 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
1295 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
1296 unsigned int res;
1297
Wu Fengguanga678cde2009-08-01 18:46:46 +08001298 mutex_lock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001299 chip->probing = 1;
1300 azx_send_cmd(chip->bus, cmd);
Wu Fengguangdeadff12009-08-01 18:45:16 +08001301 res = azx_get_response(chip->bus, addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001302 chip->probing = 0;
Wu Fengguanga678cde2009-08-01 18:46:46 +08001303 mutex_unlock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001304 if (res == -1)
1305 return -EIO;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001306 dev_dbg(chip->card->dev, "codec #%d probed OK\n", addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001307 return 0;
1308}
1309
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001310static int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
1311 struct hda_pcm *cpcm);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001312static void azx_stop_chip(struct azx *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Takashi Iwai8dd78332009-06-02 01:16:07 +02001314static void azx_bus_reset(struct hda_bus *bus)
1315{
1316 struct azx *chip = bus->private_data;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001317
1318 bus->in_reset = 1;
1319 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001320 azx_init_chip(chip, 1);
Alexander Beregalov65f75982009-06-04 13:46:16 +04001321#ifdef CONFIG_PM
Takashi Iwai8dd78332009-06-02 01:16:07 +02001322 if (chip->initialized) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01001323 struct azx_pcm *p;
1324 list_for_each_entry(p, &chip->pcm_list, list)
1325 snd_pcm_suspend_all(p->pcm);
Takashi Iwai8dd78332009-06-02 01:16:07 +02001326 snd_hda_suspend(chip->bus);
1327 snd_hda_resume(chip->bus);
1328 }
Alexander Beregalov65f75982009-06-04 13:46:16 +04001329#endif
Takashi Iwai8dd78332009-06-02 01:16:07 +02001330 bus->in_reset = 0;
1331}
1332
David Henningsson26a6cb62012-10-09 15:04:21 +02001333static int get_jackpoll_interval(struct azx *chip)
1334{
1335 int i = jackpoll_ms[chip->dev_index];
1336 unsigned int j;
1337 if (i == 0)
1338 return 0;
1339 if (i < 50 || i > 60000)
1340 j = 0;
1341 else
1342 j = msecs_to_jiffies(i);
1343 if (j == 0)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001344 dev_warn(chip->card->dev,
1345 "jackpoll_ms value out of range: %d\n", i);
David Henningsson26a6cb62012-10-09 15:04:21 +02001346 return j;
1347}
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349/*
1350 * Codec initialization
1351 */
1352
Takashi Iwai2f5983f2008-09-03 16:00:44 +02001353/* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01001354static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] = {
Wei Ni7445dfc2010-03-03 15:05:53 +08001355 [AZX_DRIVER_NVIDIA] = 8,
Kailang Yangf2690022008-05-27 11:44:55 +02001356 [AZX_DRIVER_TERA] = 1,
Takashi Iwaia9995a32007-03-12 21:30:46 +01001357};
1358
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01001359static int azx_codec_create(struct azx *chip, const char *model)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct hda_bus_template bus_temp;
Takashi Iwai34c25352008-10-28 11:38:58 +01001362 int c, codecs, err;
1363 int max_slots;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 memset(&bus_temp, 0, sizeof(bus_temp));
1366 bus_temp.private_data = chip;
1367 bus_temp.modelname = model;
1368 bus_temp.pci = chip->pci;
Takashi Iwai111d3af2006-02-16 18:17:58 +01001369 bus_temp.ops.command = azx_send_cmd;
1370 bus_temp.ops.get_response = azx_get_response;
Takashi Iwai176d5332008-07-30 15:01:44 +02001371 bus_temp.ops.attach_pcm = azx_attach_pcm_stream;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001372 bus_temp.ops.bus_reset = azx_bus_reset;
Takashi Iwai83012a72012-08-24 18:38:08 +02001373#ifdef CONFIG_PM
Takashi Iwai11cd41b2008-11-28 07:22:18 +01001374 bus_temp.power_save = &power_save;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001375 bus_temp.ops.pm_notify = azx_power_notify;
1376#endif
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001377#ifdef CONFIG_SND_HDA_DSP_LOADER
1378 bus_temp.ops.load_dsp_prepare = azx_load_dsp_prepare;
1379 bus_temp.ops.load_dsp_trigger = azx_load_dsp_trigger;
1380 bus_temp.ops.load_dsp_cleanup = azx_load_dsp_cleanup;
1381#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Takashi Iwaid01ce992007-07-27 16:52:19 +02001383 err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus);
1384 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return err;
1386
Takashi Iwai9477c582011-05-25 09:11:37 +02001387 if (chip->driver_caps & AZX_DCAPS_RIRB_DELAY) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001388 dev_dbg(chip->card->dev, "Enable delay in RIRB handling\n");
Wei Nidc9c8e22008-09-26 13:55:56 +08001389 chip->bus->needs_damn_long_delay = 1;
Takashi Iwai9477c582011-05-25 09:11:37 +02001390 }
Wei Nidc9c8e22008-09-26 13:55:56 +08001391
Takashi Iwai34c25352008-10-28 11:38:58 +01001392 codecs = 0;
Takashi Iwai2f5983f2008-09-03 16:00:44 +02001393 max_slots = azx_max_codecs[chip->driver_type];
1394 if (!max_slots)
Wei Ni7445dfc2010-03-03 15:05:53 +08001395 max_slots = AZX_DEFAULT_CODECS;
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001396
1397 /* First try to probe all given codec slots */
1398 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001399 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001400 if (probe_codec(chip, c) < 0) {
1401 /* Some BIOSen give you wrong codec addresses
1402 * that don't exist
1403 */
Takashi Iwai4e76a882014-02-25 12:21:03 +01001404 dev_warn(chip->card->dev,
1405 "Codec #%d probe error; disabling it...\n", c);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001406 chip->codec_mask &= ~(1 << c);
1407 /* More badly, accessing to a non-existing
1408 * codec often screws up the controller chip,
Paul Menzel24481582010-02-08 20:37:26 +01001409 * and disturbs the further communications.
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001410 * Thus if an error occurs during probing,
1411 * better to reset the controller chip to
1412 * get back to the sanity state.
1413 */
1414 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001415 azx_init_chip(chip, 1);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001416 }
1417 }
1418 }
1419
Takashi Iwaid507cd62011-04-26 15:25:02 +02001420 /* AMD chipsets often cause the communication stalls upon certain
1421 * sequence like the pin-detection. It seems that forcing the synced
1422 * access works around the stall. Grrr...
1423 */
Takashi Iwai9477c582011-05-25 09:11:37 +02001424 if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001425 dev_dbg(chip->card->dev, "Enable sync_write for stable communication\n");
Takashi Iwaid507cd62011-04-26 15:25:02 +02001426 chip->bus->sync_write = 1;
1427 chip->bus->allow_bus_reset = 1;
1428 }
1429
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001430 /* Then create codec instances */
Takashi Iwai34c25352008-10-28 11:38:58 +01001431 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001432 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwaibccad142007-04-24 12:23:53 +02001433 struct hda_codec *codec;
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001434 err = snd_hda_codec_new(chip->bus, c, &codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (err < 0)
1436 continue;
David Henningsson26a6cb62012-10-09 15:04:21 +02001437 codec->jackpoll_interval = get_jackpoll_interval(chip);
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01001438 codec->beep_mode = chip->beep_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 codecs++;
Takashi Iwai19a982b2007-03-21 15:14:35 +01001440 }
1441 }
1442 if (!codecs) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001443 dev_err(chip->card->dev, "no codecs initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -ENXIO;
1445 }
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001446 return 0;
1447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001449/* configure each codec instance */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001450static int azx_codec_configure(struct azx *chip)
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001451{
1452 struct hda_codec *codec;
1453 list_for_each_entry(codec, &chip->bus->codec_list, list) {
1454 snd_hda_codec_configure(codec);
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return 0;
1457}
1458
1459
1460/*
1461 * PCM support
1462 */
1463
1464/* assign a stream for the PCM */
Wu Fengguangef18bed2009-12-25 13:14:27 +08001465static inline struct azx_dev *
1466azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001468 int dev, i, nums;
Wu Fengguangef18bed2009-12-25 13:14:27 +08001469 struct azx_dev *res = NULL;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001470 /* make a non-zero unique key for the substream */
1471 int key = (substream->pcm->device << 16) | (substream->number << 2) |
1472 (substream->stream + 1);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001473
1474 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001475 dev = chip->playback_index_offset;
1476 nums = chip->playback_streams;
1477 } else {
1478 dev = chip->capture_index_offset;
1479 nums = chip->capture_streams;
1480 }
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001481 for (i = 0; i < nums; i++, dev++) {
1482 struct azx_dev *azx_dev = &chip->azx_dev[dev];
1483 dsp_lock(azx_dev);
1484 if (!azx_dev->opened && !dsp_is_locked(azx_dev)) {
1485 res = azx_dev;
1486 if (res->assigned_key == key) {
1487 res->opened = 1;
1488 res->assigned_key = key;
1489 dsp_unlock(azx_dev);
1490 return azx_dev;
1491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001493 dsp_unlock(azx_dev);
1494 }
Wu Fengguangef18bed2009-12-25 13:14:27 +08001495 if (res) {
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001496 dsp_lock(res);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001497 res->opened = 1;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001498 res->assigned_key = key;
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001499 dsp_unlock(res);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001500 }
1501 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
1504/* release the assigned stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001505static inline void azx_release_device(struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 azx_dev->opened = 0;
1508}
1509
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001510static cycle_t azx_cc_read(const struct cyclecounter *cc)
1511{
1512 struct azx_dev *azx_dev = container_of(cc, struct azx_dev, azx_cc);
1513 struct snd_pcm_substream *substream = azx_dev->substream;
1514 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1515 struct azx *chip = apcm->chip;
1516
1517 return azx_readl(chip, WALLCLK);
1518}
1519
1520static void azx_timecounter_init(struct snd_pcm_substream *substream,
1521 bool force, cycle_t last)
1522{
1523 struct azx_dev *azx_dev = get_azx_dev(substream);
1524 struct timecounter *tc = &azx_dev->azx_tc;
1525 struct cyclecounter *cc = &azx_dev->azx_cc;
1526 u64 nsec;
1527
1528 cc->read = azx_cc_read;
1529 cc->mask = CLOCKSOURCE_MASK(32);
1530
1531 /*
1532 * Converting from 24 MHz to ns means applying a 125/3 factor.
1533 * To avoid any saturation issues in intermediate operations,
1534 * the 125 factor is applied first. The division is applied
1535 * last after reading the timecounter value.
1536 * Applying the 1/3 factor as part of the multiplication
1537 * requires at least 20 bits for a decent precision, however
1538 * overflows occur after about 4 hours or less, not a option.
1539 */
1540
1541 cc->mult = 125; /* saturation after 195 years */
1542 cc->shift = 0;
1543
1544 nsec = 0; /* audio time is elapsed time since trigger */
1545 timecounter_init(tc, cc, nsec);
1546 if (force)
1547 /*
1548 * force timecounter to use predefined value,
1549 * used for synchronized starts
1550 */
1551 tc->cycle_last = last;
1552}
1553
Dylan Reidae03bbb2013-04-15 11:57:05 -07001554static u64 azx_adjust_codec_delay(struct snd_pcm_substream *substream,
Dylan Reid78daea22013-04-08 18:20:30 -07001555 u64 nsec)
1556{
1557 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1558 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
1559 u64 codec_frames, codec_nsecs;
1560
1561 if (!hinfo->ops.get_delay)
1562 return nsec;
1563
1564 codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
1565 codec_nsecs = div_u64(codec_frames * 1000000000LL,
1566 substream->runtime->rate);
1567
Dylan Reidae03bbb2013-04-15 11:57:05 -07001568 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1569 return nsec + codec_nsecs;
1570
Dylan Reid78daea22013-04-08 18:20:30 -07001571 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
1572}
1573
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001574static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
1575 struct timespec *ts)
1576{
1577 struct azx_dev *azx_dev = get_azx_dev(substream);
1578 u64 nsec;
1579
1580 nsec = timecounter_read(&azx_dev->azx_tc);
1581 nsec = div_u64(nsec, 3); /* can be optimized */
Dylan Reidae03bbb2013-04-15 11:57:05 -07001582 nsec = azx_adjust_codec_delay(substream, nsec);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001583
1584 *ts = ns_to_timespec(nsec);
1585
1586 return 0;
1587}
1588
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001589static struct snd_pcm_hardware azx_pcm_hw = {
Takashi Iwaid01ce992007-07-27 16:52:19 +02001590 .info = (SNDRV_PCM_INFO_MMAP |
1591 SNDRV_PCM_INFO_INTERLEAVED |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1593 SNDRV_PCM_INFO_MMAP_VALID |
Pavel Machek927fc862006-08-31 17:03:43 +02001594 /* No full-resume yet implemented */
1595 /* SNDRV_PCM_INFO_RESUME |*/
Takashi Iwai850f0e52008-03-18 17:11:05 +01001596 SNDRV_PCM_INFO_PAUSE |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001597 SNDRV_PCM_INFO_SYNC_START |
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001598 SNDRV_PCM_INFO_HAS_WALL_CLOCK |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001599 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1601 .rates = SNDRV_PCM_RATE_48000,
1602 .rate_min = 48000,
1603 .rate_max = 48000,
1604 .channels_min = 2,
1605 .channels_max = 2,
1606 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
1607 .period_bytes_min = 128,
1608 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
1609 .periods_min = 2,
1610 .periods_max = AZX_MAX_FRAG,
1611 .fifo_size = 0,
1612};
1613
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001614static int azx_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1617 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001618 struct azx *chip = apcm->chip;
1619 struct azx_dev *azx_dev;
1620 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 unsigned long flags;
1622 int err;
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001623 int buff_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Ingo Molnar62932df2006-01-16 16:34:20 +01001625 mutex_lock(&chip->open_mutex);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001626 azx_dev = azx_assign_device(chip, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (azx_dev == NULL) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001628 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return -EBUSY;
1630 }
1631 runtime->hw = azx_pcm_hw;
1632 runtime->hw.channels_min = hinfo->channels_min;
1633 runtime->hw.channels_max = hinfo->channels_max;
1634 runtime->hw.formats = hinfo->formats;
1635 runtime->hw.rates = hinfo->rates;
1636 snd_pcm_limit_hw_rates(runtime);
1637 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001638
1639 /* avoid wrap-around with wall-clock */
1640 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1641 20,
1642 178000000);
1643
Takashi Iwai52409aa2012-01-23 17:10:24 +01001644 if (chip->align_buffer_size)
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001645 /* constrain buffer sizes to be multiple of 128
1646 bytes. This is more efficient in terms of memory
1647 access but isn't required by the HDA spec and
1648 prevents users from specifying exact period/buffer
1649 sizes. For example for 44.1kHz, a period size set
1650 to 20ms will be rounded to 19.59ms. */
1651 buff_step = 128;
1652 else
1653 /* Don't enforce steps on buffer sizes, still need to
1654 be multiple of 4 bytes (HDA spec). Tested on Intel
1655 HDA controllers, may not work on all devices where
1656 option needs to be disabled */
1657 buff_step = 4;
1658
Joachim Deguara5f1545b2007-03-16 15:01:36 +01001659 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001660 buff_step);
Joachim Deguara5f1545b2007-03-16 15:01:36 +01001661 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001662 buff_step);
Dylan Reidb4a91cf2012-06-15 19:36:23 -07001663 snd_hda_power_up_d3wait(apcm->codec);
Takashi Iwaid01ce992007-07-27 16:52:19 +02001664 err = hinfo->ops.open(hinfo, apcm->codec, substream);
1665 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 azx_release_device(azx_dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001667 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01001668 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return err;
1670 }
Takashi Iwai70d321e2009-07-03 23:06:45 +02001671 snd_pcm_limit_hw_rates(runtime);
Takashi Iwaiaba66532009-07-05 11:44:46 +02001672 /* sanity check */
1673 if (snd_BUG_ON(!runtime->hw.channels_min) ||
1674 snd_BUG_ON(!runtime->hw.channels_max) ||
1675 snd_BUG_ON(!runtime->hw.formats) ||
1676 snd_BUG_ON(!runtime->hw.rates)) {
1677 azx_release_device(azx_dev);
1678 hinfo->ops.close(hinfo, apcm->codec, substream);
1679 snd_hda_power_down(apcm->codec);
1680 mutex_unlock(&chip->open_mutex);
1681 return -EINVAL;
1682 }
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001683
1684 /* disable WALLCLOCK timestamps for capture streams
1685 until we figure out how to handle digital inputs */
1686 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1687 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 spin_lock_irqsave(&chip->reg_lock, flags);
1690 azx_dev->substream = substream;
1691 azx_dev->running = 0;
1692 spin_unlock_irqrestore(&chip->reg_lock, flags);
1693
1694 runtime->private_data = azx_dev;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001695 snd_pcm_set_sync(substream);
Ingo Molnar62932df2006-01-16 16:34:20 +01001696 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return 0;
1698}
1699
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001700static int azx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1703 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001704 struct azx *chip = apcm->chip;
1705 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 unsigned long flags;
1707
Ingo Molnar62932df2006-01-16 16:34:20 +01001708 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 spin_lock_irqsave(&chip->reg_lock, flags);
1710 azx_dev->substream = NULL;
1711 azx_dev->running = 0;
1712 spin_unlock_irqrestore(&chip->reg_lock, flags);
1713 azx_release_device(azx_dev);
1714 hinfo->ops.close(hinfo, apcm->codec, substream);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001715 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01001716 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return 0;
1718}
1719
Takashi Iwaid01ce992007-07-27 16:52:19 +02001720static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
1721 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001723 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1724 struct azx *chip = apcm->chip;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001725 struct azx_dev *azx_dev = get_azx_dev(substream);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001726 int ret;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001727
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001728 dsp_lock(azx_dev);
1729 if (dsp_is_locked(azx_dev)) {
1730 ret = -EBUSY;
1731 goto unlock;
1732 }
1733
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +01001734 mark_runtime_wc(chip, azx_dev, substream, false);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001735 azx_dev->bufsize = 0;
1736 azx_dev->period_bytes = 0;
1737 azx_dev->format_val = 0;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001738 ret = snd_pcm_lib_malloc_pages(substream,
Takashi Iwaid01ce992007-07-27 16:52:19 +02001739 params_buffer_bytes(hw_params));
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001740 if (ret < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001741 goto unlock;
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +01001742 mark_runtime_wc(chip, azx_dev, substream, true);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001743 unlock:
1744 dsp_unlock(azx_dev);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001745 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001748static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001751 struct azx_dev *azx_dev = get_azx_dev(substream);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001752 struct azx *chip = apcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
1754
1755 /* reset BDL address */
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001756 dsp_lock(azx_dev);
1757 if (!dsp_is_locked(azx_dev)) {
Dylan Reid40830812014-02-28 15:41:13 -08001758 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
1759 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
1760 azx_sd_writel(chip, azx_dev, SD_CTL, 0);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001761 azx_dev->bufsize = 0;
1762 azx_dev->period_bytes = 0;
1763 azx_dev->format_val = 0;
1764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Takashi Iwaieb541332010-08-06 13:48:11 +02001766 snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +01001768 mark_runtime_wc(chip, azx_dev, substream, false);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001769 azx_dev->prepared = 0;
1770 dsp_unlock(azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return snd_pcm_lib_free_pages(substream);
1772}
1773
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001774static int azx_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001777 struct azx *chip = apcm->chip;
1778 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001780 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001781 unsigned int bufsize, period_bytes, format_val, stream_tag;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001782 int err;
Stephen Warren7c935972011-06-01 11:14:17 -06001783 struct hda_spdif_out *spdif =
1784 snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid);
1785 unsigned short ctls = spdif ? spdif->ctls : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001787 dsp_lock(azx_dev);
1788 if (dsp_is_locked(azx_dev)) {
1789 err = -EBUSY;
1790 goto unlock;
1791 }
1792
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001793 azx_stream_reset(chip, azx_dev);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001794 format_val = snd_hda_calc_stream_format(runtime->rate,
1795 runtime->channels,
1796 runtime->format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03001797 hinfo->maxbps,
Stephen Warren7c935972011-06-01 11:14:17 -06001798 ctls);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001799 if (!format_val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001800 dev_err(chip->card->dev,
1801 "invalid format_val, rate=%d, ch=%d, format=%d\n",
1802 runtime->rate, runtime->channels, runtime->format);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001803 err = -EINVAL;
1804 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806
Takashi Iwai97b71c92009-03-18 15:09:13 +01001807 bufsize = snd_pcm_lib_buffer_bytes(substream);
1808 period_bytes = snd_pcm_lib_period_bytes(substream);
1809
Takashi Iwai4e76a882014-02-25 12:21:03 +01001810 dev_dbg(chip->card->dev, "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
1811 bufsize, format_val);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001812
1813 if (bufsize != azx_dev->bufsize ||
1814 period_bytes != azx_dev->period_bytes ||
Takashi Iwai915bf292012-09-11 15:19:10 +02001815 format_val != azx_dev->format_val ||
1816 runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
Takashi Iwai97b71c92009-03-18 15:09:13 +01001817 azx_dev->bufsize = bufsize;
1818 azx_dev->period_bytes = period_bytes;
1819 azx_dev->format_val = format_val;
Takashi Iwai915bf292012-09-11 15:19:10 +02001820 azx_dev->no_period_wakeup = runtime->no_period_wakeup;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001821 err = azx_setup_periods(chip, substream, azx_dev);
1822 if (err < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001823 goto unlock;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001824 }
1825
Takashi Iwaie8648e52013-12-06 17:15:01 +01001826 /* when LPIB delay correction gives a small negative value,
1827 * we ignore it; currently set the threshold statically to
1828 * 64 frames
1829 */
1830 if (runtime->period_size > 64)
1831 azx_dev->delay_negative_threshold = -frames_to_bytes(runtime, 64);
1832 else
1833 azx_dev->delay_negative_threshold = 0;
1834
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001835 /* wallclk has 24Mhz clock source */
1836 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
1837 runtime->rate) * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 azx_setup_controller(chip, azx_dev);
1839 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Dylan Reid40830812014-02-28 15:41:13 -08001840 azx_dev->fifo_size =
1841 azx_sd_readw(chip, azx_dev, SD_FIFOSIZE) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 else
1843 azx_dev->fifo_size = 0;
1844
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001845 stream_tag = azx_dev->stream_tag;
1846 /* CA-IBG chips need the playback stream starting from 1 */
Takashi Iwai9477c582011-05-25 09:11:37 +02001847 if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001848 stream_tag > chip->capture_streams)
1849 stream_tag -= chip->capture_streams;
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001850 err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
Takashi Iwaieb541332010-08-06 13:48:11 +02001851 azx_dev->format_val, substream);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001852
1853 unlock:
1854 if (!err)
1855 azx_dev->prepared = 1;
1856 dsp_unlock(azx_dev);
1857 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001860static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001863 struct azx *chip = apcm->chip;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001864 struct azx_dev *azx_dev;
1865 struct snd_pcm_substream *s;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001866 int rstart = 0, start, nsync = 0, sbits = 0;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001867 int nwait, timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Takashi Iwai1a8506d2012-10-16 15:10:08 +02001869 azx_dev = get_azx_dev(substream);
1870 trace_azx_pcm_trigger(chip, azx_dev, cmd);
1871
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001872 if (dsp_is_locked(azx_dev) || !azx_dev->prepared)
1873 return -EPIPE;
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 switch (cmd) {
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001876 case SNDRV_PCM_TRIGGER_START:
1877 rstart = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1879 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai850f0e52008-03-18 17:11:05 +01001880 start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 break;
1882 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jaroslav Kysela47123192005-08-15 20:53:07 +02001883 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai850f0e52008-03-18 17:11:05 +01001885 start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 break;
1887 default:
Takashi Iwai850f0e52008-03-18 17:11:05 +01001888 return -EINVAL;
1889 }
1890
1891 snd_pcm_group_for_each_entry(s, substream) {
1892 if (s->pcm->card != substream->pcm->card)
1893 continue;
1894 azx_dev = get_azx_dev(s);
1895 sbits |= 1 << azx_dev->index;
1896 nsync++;
1897 snd_pcm_trigger_done(s, substream);
1898 }
1899
1900 spin_lock(&chip->reg_lock);
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05001901
1902 /* first, set SYNC bits of corresponding streams */
1903 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
1904 azx_writel(chip, OLD_SSYNC,
1905 azx_readl(chip, OLD_SSYNC) | sbits);
1906 else
1907 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) | sbits);
1908
Takashi Iwai850f0e52008-03-18 17:11:05 +01001909 snd_pcm_group_for_each_entry(s, substream) {
1910 if (s->pcm->card != substream->pcm->card)
1911 continue;
1912 azx_dev = get_azx_dev(s);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001913 if (start) {
1914 azx_dev->start_wallclk = azx_readl(chip, WALLCLK);
1915 if (!rstart)
1916 azx_dev->start_wallclk -=
1917 azx_dev->period_wallclk;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001918 azx_stream_start(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001919 } else {
Takashi Iwai850f0e52008-03-18 17:11:05 +01001920 azx_stream_stop(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001921 }
Takashi Iwai850f0e52008-03-18 17:11:05 +01001922 azx_dev->running = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01001925 if (start) {
Takashi Iwai850f0e52008-03-18 17:11:05 +01001926 /* wait until all FIFOs get ready */
1927 for (timeout = 5000; timeout; timeout--) {
1928 nwait = 0;
1929 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);
Dylan Reid40830812014-02-28 15:41:13 -08001933 if (!(azx_sd_readb(chip, azx_dev, SD_STS) &
Takashi Iwai850f0e52008-03-18 17:11:05 +01001934 SD_STS_FIFO_READY))
1935 nwait++;
1936 }
1937 if (!nwait)
1938 break;
1939 cpu_relax();
1940 }
1941 } else {
1942 /* wait until all RUN bits are cleared */
1943 for (timeout = 5000; timeout; timeout--) {
1944 nwait = 0;
1945 snd_pcm_group_for_each_entry(s, substream) {
1946 if (s->pcm->card != substream->pcm->card)
1947 continue;
1948 azx_dev = get_azx_dev(s);
Dylan Reid40830812014-02-28 15:41:13 -08001949 if (azx_sd_readb(chip, azx_dev, SD_CTL) &
Takashi Iwai850f0e52008-03-18 17:11:05 +01001950 SD_CTL_DMA_START)
1951 nwait++;
1952 }
1953 if (!nwait)
1954 break;
1955 cpu_relax();
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05001958 spin_lock(&chip->reg_lock);
1959 /* reset SYNC bits */
1960 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
1961 azx_writel(chip, OLD_SSYNC,
1962 azx_readl(chip, OLD_SSYNC) & ~sbits);
1963 else
1964 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) & ~sbits);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001965 if (start) {
1966 azx_timecounter_init(substream, 0, 0);
1967 if (nsync > 1) {
1968 cycle_t cycle_last;
1969
1970 /* same start cycle for master and group */
1971 azx_dev = get_azx_dev(substream);
1972 cycle_last = azx_dev->azx_tc.cycle_last;
1973
1974 snd_pcm_group_for_each_entry(s, substream) {
1975 if (s->pcm->card != substream->pcm->card)
1976 continue;
1977 azx_timecounter_init(s, 1, cycle_last);
1978 }
1979 }
1980 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05001981 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01001982 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Joseph Chan0e153472008-08-26 14:38:03 +02001985/* get the current DMA position with correction on VIA chips */
1986static unsigned int azx_via_get_position(struct azx *chip,
1987 struct azx_dev *azx_dev)
1988{
1989 unsigned int link_pos, mini_pos, bound_pos;
1990 unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
1991 unsigned int fifo_size;
1992
Dylan Reid40830812014-02-28 15:41:13 -08001993 link_pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
Takashi Iwaib4a655e2011-06-07 12:26:56 +02001994 if (azx_dev->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Joseph Chan0e153472008-08-26 14:38:03 +02001995 /* Playback, no problem using link position */
1996 return link_pos;
1997 }
1998
1999 /* Capture */
2000 /* For new chipset,
2001 * use mod to get the DMA position just like old chipset
2002 */
2003 mod_dma_pos = le32_to_cpu(*azx_dev->posbuf);
2004 mod_dma_pos %= azx_dev->period_bytes;
2005
2006 /* azx_dev->fifo_size can't get FIFO size of in stream.
2007 * Get from base address + offset.
2008 */
2009 fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
2010
2011 if (azx_dev->insufficient) {
2012 /* Link position never gather than FIFO size */
2013 if (link_pos <= fifo_size)
2014 return 0;
2015
2016 azx_dev->insufficient = 0;
2017 }
2018
2019 if (link_pos <= fifo_size)
2020 mini_pos = azx_dev->bufsize + link_pos - fifo_size;
2021 else
2022 mini_pos = link_pos - fifo_size;
2023
2024 /* Find nearest previous boudary */
2025 mod_mini_pos = mini_pos % azx_dev->period_bytes;
2026 mod_link_pos = link_pos % azx_dev->period_bytes;
2027 if (mod_link_pos >= fifo_size)
2028 bound_pos = link_pos - mod_link_pos;
2029 else if (mod_dma_pos >= mod_mini_pos)
2030 bound_pos = mini_pos - mod_mini_pos;
2031 else {
2032 bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes;
2033 if (bound_pos >= azx_dev->bufsize)
2034 bound_pos = 0;
2035 }
2036
2037 /* Calculate real DMA position we want */
2038 return bound_pos + mod_dma_pos;
2039}
2040
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002041static unsigned int azx_get_position(struct azx *chip,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002042 struct azx_dev *azx_dev,
2043 bool with_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Takashi Iwai21229612013-04-05 07:27:45 +02002045 struct snd_pcm_substream *substream = azx_dev->substream;
2046 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 unsigned int pos;
Takashi Iwai21229612013-04-05 07:27:45 +02002048 int stream = substream->stream;
2049 struct hda_pcm_stream *hinfo = apcm->hinfo[stream];
Takashi Iwai1a8506d2012-10-16 15:10:08 +02002050 int delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
David Henningsson4cb36312010-09-30 10:12:50 +02002052 switch (chip->position_fix[stream]) {
2053 case POS_FIX_LPIB:
2054 /* read LPIB */
Dylan Reid40830812014-02-28 15:41:13 -08002055 pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
David Henningsson4cb36312010-09-30 10:12:50 +02002056 break;
2057 case POS_FIX_VIACOMBO:
Joseph Chan0e153472008-08-26 14:38:03 +02002058 pos = azx_via_get_position(chip, azx_dev);
David Henningsson4cb36312010-09-30 10:12:50 +02002059 break;
2060 default:
2061 /* use the position buffer */
2062 pos = le32_to_cpu(*azx_dev->posbuf);
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002063 if (with_check && chip->position_fix[stream] == POS_FIX_AUTO) {
Takashi Iwaia8103642011-06-07 12:23:23 +02002064 if (!pos || pos == (u32)-1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002065 dev_info(chip->card->dev,
2066 "Invalid position buffer, using LPIB read method instead.\n");
Takashi Iwaia8103642011-06-07 12:23:23 +02002067 chip->position_fix[stream] = POS_FIX_LPIB;
Dylan Reid40830812014-02-28 15:41:13 -08002068 pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
Takashi Iwaia8103642011-06-07 12:23:23 +02002069 } else
2070 chip->position_fix[stream] = POS_FIX_POSBUF;
2071 }
2072 break;
Takashi Iwaic74db862005-05-12 14:26:27 +02002073 }
David Henningsson4cb36312010-09-30 10:12:50 +02002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (pos >= azx_dev->bufsize)
2076 pos = 0;
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002077
2078 /* calculate runtime delay from LPIB */
Takashi Iwai21229612013-04-05 07:27:45 +02002079 if (substream->runtime &&
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002080 chip->position_fix[stream] == POS_FIX_POSBUF &&
2081 (chip->driver_caps & AZX_DCAPS_COUNT_LPIB_DELAY)) {
Dylan Reid40830812014-02-28 15:41:13 -08002082 unsigned int lpib_pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002083 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2084 delay = pos - lpib_pos;
2085 else
2086 delay = lpib_pos - pos;
Takashi Iwaie8648e52013-12-06 17:15:01 +01002087 if (delay < 0) {
2088 if (delay >= azx_dev->delay_negative_threshold)
2089 delay = 0;
2090 else
2091 delay += azx_dev->bufsize;
2092 }
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002093 if (delay >= azx_dev->period_bytes) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002094 dev_info(chip->card->dev,
2095 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
2096 delay, azx_dev->period_bytes);
Takashi Iwai1f046612012-10-16 16:52:26 +02002097 delay = 0;
2098 chip->driver_caps &= ~AZX_DCAPS_COUNT_LPIB_DELAY;
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002099 }
Takashi Iwai21229612013-04-05 07:27:45 +02002100 delay = bytes_to_frames(substream->runtime, delay);
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002101 }
Takashi Iwai21229612013-04-05 07:27:45 +02002102
2103 if (substream->runtime) {
2104 if (hinfo->ops.get_delay)
2105 delay += hinfo->ops.get_delay(hinfo, apcm->codec,
2106 substream);
2107 substream->runtime->delay = delay;
2108 }
2109
Takashi Iwai1a8506d2012-10-16 15:10:08 +02002110 trace_azx_get_position(chip, azx_dev, pos, delay);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002111 return pos;
2112}
2113
2114static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
2115{
2116 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2117 struct azx *chip = apcm->chip;
2118 struct azx_dev *azx_dev = get_azx_dev(substream);
2119 return bytes_to_frames(substream->runtime,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002120 azx_get_position(chip, azx_dev, false));
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002121}
2122
2123/*
2124 * Check whether the current DMA position is acceptable for updating
2125 * periods. Returns non-zero if it's OK.
2126 *
2127 * Many HD-audio controllers appear pretty inaccurate about
2128 * the update-IRQ timing. The IRQ is issued before actually the
2129 * data is processed. So, we need to process it afterwords in a
2130 * workqueue.
2131 */
2132static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
2133{
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002134 u32 wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002135 unsigned int pos;
2136
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002137 wallclk = azx_readl(chip, WALLCLK) - azx_dev->start_wallclk;
2138 if (wallclk < (azx_dev->period_wallclk * 2) / 3)
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002139 return -1; /* bogus (too early) interrupt */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002140
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002141 pos = azx_get_position(chip, azx_dev, true);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002142
Takashi Iwaid6d8bf52010-02-12 18:17:06 +01002143 if (WARN_ONCE(!azx_dev->period_bytes,
2144 "hda-intel: zero azx_dev->period_bytes"))
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002145 return -1; /* this shouldn't happen! */
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002146 if (wallclk < (azx_dev->period_wallclk * 5) / 4 &&
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002147 pos % azx_dev->period_bytes > azx_dev->period_bytes / 2)
2148 /* NG - it's below the first next period boundary */
Dylan Reid9cdc0112014-02-28 15:41:14 -08002149 return chip->bdl_pos_adj[chip->dev_index] ? 0 : -1;
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002150 azx_dev->start_wallclk += wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002151 return 1; /* OK, it's fine */
2152}
2153
2154/*
2155 * The work for pending PCM period updates.
2156 */
2157static void azx_irq_pending_work(struct work_struct *work)
2158{
2159 struct azx *chip = container_of(work, struct azx, irq_pending_work);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002160 int i, pending, ok;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002161
Takashi Iwaia6a950a2008-06-10 17:53:35 +02002162 if (!chip->irq_pending_warned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002163 dev_info(chip->card->dev,
2164 "IRQ timing workaround is activated for card #%d. Suggest a bigger bdl_pos_adj.\n",
2165 chip->card->number);
Takashi Iwaia6a950a2008-06-10 17:53:35 +02002166 chip->irq_pending_warned = 1;
2167 }
2168
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002169 for (;;) {
2170 pending = 0;
2171 spin_lock_irq(&chip->reg_lock);
2172 for (i = 0; i < chip->num_streams; i++) {
2173 struct azx_dev *azx_dev = &chip->azx_dev[i];
2174 if (!azx_dev->irq_pending ||
2175 !azx_dev->substream ||
2176 !azx_dev->running)
2177 continue;
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002178 ok = azx_position_ok(chip, azx_dev);
2179 if (ok > 0) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002180 azx_dev->irq_pending = 0;
2181 spin_unlock(&chip->reg_lock);
2182 snd_pcm_period_elapsed(azx_dev->substream);
2183 spin_lock(&chip->reg_lock);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002184 } else if (ok < 0) {
2185 pending = 0; /* too early */
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002186 } else
2187 pending++;
2188 }
2189 spin_unlock_irq(&chip->reg_lock);
2190 if (!pending)
2191 return;
Takashi Iwai08af4952010-08-03 14:39:04 +02002192 msleep(1);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002193 }
2194}
2195
2196/* clear irq_pending flags and assure no on-going workq */
2197static void azx_clear_irq_pending(struct azx *chip)
2198{
2199 int i;
2200
2201 spin_lock_irq(&chip->reg_lock);
2202 for (i = 0; i < chip->num_streams; i++)
2203 chip->azx_dev[i].irq_pending = 0;
2204 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002207#ifdef CONFIG_X86
2208static int azx_pcm_mmap(struct snd_pcm_substream *substream,
2209 struct vm_area_struct *area)
2210{
2211 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2212 struct azx *chip = apcm->chip;
2213 if (!azx_snoop(chip))
2214 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
2215 return snd_pcm_lib_default_mmap(substream, area);
2216}
2217#else
2218#define azx_pcm_mmap NULL
2219#endif
2220
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002221static struct snd_pcm_ops azx_pcm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 .open = azx_pcm_open,
2223 .close = azx_pcm_close,
2224 .ioctl = snd_pcm_lib_ioctl,
2225 .hw_params = azx_pcm_hw_params,
2226 .hw_free = azx_pcm_hw_free,
2227 .prepare = azx_pcm_prepare,
2228 .trigger = azx_pcm_trigger,
2229 .pointer = azx_pcm_pointer,
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05002230 .wall_clock = azx_get_wallclock_tstamp,
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002231 .mmap = azx_pcm_mmap,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002232 .page = snd_pcm_sgbuf_ops_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233};
2234
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002235static void azx_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Takashi Iwai176d5332008-07-30 15:01:44 +02002237 struct azx_pcm *apcm = pcm->private_data;
2238 if (apcm) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002239 list_del(&apcm->list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002240 kfree(apcm);
2241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002244#define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
2245
Takashi Iwai176d5332008-07-30 15:01:44 +02002246static int
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002247azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
2248 struct hda_pcm *cpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002250 struct azx *chip = bus->private_data;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002251 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct azx_pcm *apcm;
Takashi Iwai176d5332008-07-30 15:01:44 +02002253 int pcm_dev = cpcm->device;
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002254 unsigned int size;
Takashi Iwai176d5332008-07-30 15:01:44 +02002255 int s, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002257 list_for_each_entry(apcm, &chip->pcm_list, list) {
2258 if (apcm->pcm->device == pcm_dev) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002259 dev_err(chip->card->dev, "PCM %d already exists\n",
2260 pcm_dev);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002261 return -EBUSY;
2262 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002263 }
2264 err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
2265 cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
2266 cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 &pcm);
2268 if (err < 0)
2269 return err;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002270 strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
Takashi Iwai176d5332008-07-30 15:01:44 +02002271 apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (apcm == NULL)
2273 return -ENOMEM;
2274 apcm->chip = chip;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002275 apcm->pcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 apcm->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 pcm->private_data = apcm;
2278 pcm->private_free = azx_pcm_free;
Takashi Iwai176d5332008-07-30 15:01:44 +02002279 if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
2280 pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002281 list_add_tail(&apcm->list, &chip->pcm_list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002282 cpcm->pcm = pcm;
2283 for (s = 0; s < 2; s++) {
2284 apcm->hinfo[s] = &cpcm->stream[s];
2285 if (cpcm->stream[s].substreams)
2286 snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
2287 }
2288 /* buffer pre-allocation */
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002289 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
2290 if (size > MAX_PREALLOC_SIZE)
2291 size = MAX_PREALLOC_SIZE;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002292 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Dylan Reid89287562014-02-28 15:41:15 -08002293 chip->card->dev,
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002294 size, MAX_PREALLOC_SIZE);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01002295 /* link to codec */
2296 pcm->dev = &codec->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return 0;
2298}
2299
2300/*
2301 * mixer creation - all stuff is implemented in hda module
2302 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002303static int azx_mixer_create(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 return snd_hda_build_controls(chip->bus);
2306}
2307
2308
2309/*
2310 * initialize SD streams
2311 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002312static int azx_init_stream(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 int i;
2315
2316 /* initialize each stream (aka device)
Takashi Iwaid01ce992007-07-27 16:52:19 +02002317 * assign the starting bdl address to each stream (device)
2318 * and initialize
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 */
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002320 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002321 struct azx_dev *azx_dev = &chip->azx_dev[i];
Takashi Iwai929861c2006-08-31 16:55:40 +02002322 azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
2324 azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
2325 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
2326 azx_dev->sd_int_sta_mask = 1 << i;
2327 /* stream tag: must be non-zero and unique */
2328 azx_dev->index = i;
2329 azx_dev->stream_tag = i + 1;
2330 }
2331
2332 return 0;
2333}
2334
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002335static int azx_acquire_irq(struct azx *chip, int do_disconnect)
2336{
Takashi Iwai437a5a42006-11-21 12:14:23 +01002337 if (request_irq(chip->pci->irq, azx_interrupt,
2338 chip->msi ? 0 : IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002339 KBUILD_MODNAME, chip)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002340 dev_err(chip->card->dev,
2341 "unable to grab IRQ %d, disabling device\n",
2342 chip->pci->irq);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002343 if (do_disconnect)
2344 snd_card_disconnect(chip->card);
2345 return -1;
2346 }
2347 chip->irq = chip->pci->irq;
Takashi Iwai69e13412006-11-21 12:10:55 +01002348 pci_intx(chip->pci, !chip->msi);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002349 return 0;
2350}
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Takashi Iwaicb53c622007-08-10 17:21:45 +02002353static void azx_stop_chip(struct azx *chip)
2354{
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002355 if (!chip->initialized)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002356 return;
2357
2358 /* disable interrupts */
2359 azx_int_disable(chip);
2360 azx_int_clear(chip);
2361
2362 /* disable CORB/RIRB */
2363 azx_free_cmd_io(chip);
2364
2365 /* disable position buffer */
2366 azx_writel(chip, DPLBASE, 0);
2367 azx_writel(chip, DPUBASE, 0);
2368
2369 chip->initialized = 0;
2370}
2371
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002372#ifdef CONFIG_SND_HDA_DSP_LOADER
2373/*
2374 * DSP loading code (e.g. for CA0132)
2375 */
2376
2377/* use the first stream for loading DSP */
2378static struct azx_dev *
2379azx_get_dsp_loader_dev(struct azx *chip)
2380{
2381 return &chip->azx_dev[chip->playback_index_offset];
2382}
2383
2384static int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
2385 unsigned int byte_size,
2386 struct snd_dma_buffer *bufp)
2387{
2388 u32 *bdl;
2389 struct azx *chip = bus->private_data;
2390 struct azx_dev *azx_dev;
2391 int err;
2392
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002393 azx_dev = azx_get_dsp_loader_dev(chip);
2394
2395 dsp_lock(azx_dev);
2396 spin_lock_irq(&chip->reg_lock);
2397 if (azx_dev->running || azx_dev->locked) {
2398 spin_unlock_irq(&chip->reg_lock);
2399 err = -EBUSY;
2400 goto unlock;
2401 }
2402 azx_dev->prepared = 0;
2403 chip->saved_azx_dev = *azx_dev;
2404 azx_dev->locked = 1;
2405 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002406
2407 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
Dylan Reid89287562014-02-28 15:41:15 -08002408 chip->card->dev,
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002409 byte_size, bufp);
2410 if (err < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002411 goto err_alloc;
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002412
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002413 mark_pages_wc(chip, bufp, true);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002414 azx_dev->bufsize = byte_size;
2415 azx_dev->period_bytes = byte_size;
2416 azx_dev->format_val = format;
2417
2418 azx_stream_reset(chip, azx_dev);
2419
2420 /* reset BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08002421 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
2422 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002423
2424 azx_dev->frags = 0;
2425 bdl = (u32 *)azx_dev->bdl.area;
2426 err = setup_bdle(chip, bufp, azx_dev, &bdl, 0, byte_size, 0);
2427 if (err < 0)
2428 goto error;
2429
2430 azx_setup_controller(chip, azx_dev);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002431 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002432 return azx_dev->stream_tag;
2433
2434 error:
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002435 mark_pages_wc(chip, bufp, false);
2436 snd_dma_free_pages(bufp);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002437 err_alloc:
2438 spin_lock_irq(&chip->reg_lock);
2439 if (azx_dev->opened)
2440 *azx_dev = chip->saved_azx_dev;
2441 azx_dev->locked = 0;
2442 spin_unlock_irq(&chip->reg_lock);
2443 unlock:
2444 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002445 return err;
2446}
2447
2448static void azx_load_dsp_trigger(struct hda_bus *bus, bool start)
2449{
2450 struct azx *chip = bus->private_data;
2451 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
2452
2453 if (start)
2454 azx_stream_start(chip, azx_dev);
2455 else
2456 azx_stream_stop(chip, azx_dev);
2457 azx_dev->running = start;
2458}
2459
2460static void azx_load_dsp_cleanup(struct hda_bus *bus,
2461 struct snd_dma_buffer *dmab)
2462{
2463 struct azx *chip = bus->private_data;
2464 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
2465
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002466 if (!dmab->area || !azx_dev->locked)
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002467 return;
2468
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002469 dsp_lock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002470 /* reset BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08002471 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
2472 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
2473 azx_sd_writel(chip, azx_dev, SD_CTL, 0);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002474 azx_dev->bufsize = 0;
2475 azx_dev->period_bytes = 0;
2476 azx_dev->format_val = 0;
2477
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002478 mark_pages_wc(chip, dmab, false);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002479 snd_dma_free_pages(dmab);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002480 dmab->area = NULL;
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002481
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002482 spin_lock_irq(&chip->reg_lock);
2483 if (azx_dev->opened)
2484 *azx_dev = chip->saved_azx_dev;
2485 azx_dev->locked = 0;
2486 spin_unlock_irq(&chip->reg_lock);
2487 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002488}
2489#endif /* CONFIG_SND_HDA_DSP_LOADER */
2490
Takashi Iwai83012a72012-08-24 18:38:08 +02002491#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02002492/* power-up/down the controller */
Takashi Iwai68467f52012-08-28 09:14:29 -07002493static void azx_power_notify(struct hda_bus *bus, bool power_up)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002494{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002495 struct azx *chip = bus->private_data;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002496
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01002497 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2498 return;
2499
Takashi Iwai68467f52012-08-28 09:14:29 -07002500 if (power_up)
Dylan Reid89287562014-02-28 15:41:15 -08002501 pm_runtime_get_sync(chip->card->dev);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002502 else
Dylan Reid89287562014-02-28 15:41:15 -08002503 pm_runtime_put_sync(chip->card->dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002504}
Takashi Iwai65fcd412012-08-14 17:13:32 +02002505
2506static DEFINE_MUTEX(card_list_lock);
2507static LIST_HEAD(card_list);
2508
2509static void azx_add_card_list(struct azx *chip)
2510{
2511 mutex_lock(&card_list_lock);
2512 list_add(&chip->list, &card_list);
2513 mutex_unlock(&card_list_lock);
2514}
2515
2516static void azx_del_card_list(struct azx *chip)
2517{
2518 mutex_lock(&card_list_lock);
2519 list_del_init(&chip->list);
2520 mutex_unlock(&card_list_lock);
2521}
2522
2523/* trigger power-save check at writing parameter */
2524static int param_set_xint(const char *val, const struct kernel_param *kp)
2525{
2526 struct azx *chip;
2527 struct hda_codec *c;
2528 int prev = power_save;
2529 int ret = param_set_int(val, kp);
2530
2531 if (ret || prev == power_save)
2532 return ret;
2533
2534 mutex_lock(&card_list_lock);
2535 list_for_each_entry(chip, &card_list, list) {
2536 if (!chip->bus || chip->disabled)
2537 continue;
2538 list_for_each_entry(c, &chip->bus->codec_list, list)
2539 snd_hda_power_sync(c);
2540 }
2541 mutex_unlock(&card_list_lock);
2542 return 0;
2543}
2544#else
2545#define azx_add_card_list(chip) /* NOP */
2546#define azx_del_card_list(chip) /* NOP */
Takashi Iwai83012a72012-08-24 18:38:08 +02002547#endif /* CONFIG_PM */
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002548
Takashi Iwai7ccbde52012-08-14 18:10:09 +02002549#if defined(CONFIG_PM_SLEEP) || defined(SUPPORT_VGA_SWITCHEROO)
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002550/*
2551 * power management
2552 */
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002553static int azx_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002555 struct pci_dev *pci = to_pci_dev(dev);
2556 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002557 struct azx *chip = card->private_data;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002558 struct azx_pcm *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Takashi Iwaic5c21522012-12-04 17:01:25 +01002560 if (chip->disabled)
2561 return 0;
2562
Takashi Iwai421a1252005-11-17 16:11:09 +01002563 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002564 azx_clear_irq_pending(chip);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002565 list_for_each_entry(p, &chip->pcm_list, list)
2566 snd_pcm_suspend_all(p->pcm);
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02002567 if (chip->initialized)
Takashi Iwai8dd78332009-06-02 01:16:07 +02002568 snd_hda_suspend(chip->bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002569 azx_stop_chip(chip);
Mengdong Lin7295b262013-06-25 05:58:49 -04002570 azx_enter_link_reset(chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002571 if (chip->irq >= 0) {
Takashi Iwai43001c92006-09-08 12:30:03 +02002572 free_irq(chip->irq, chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002573 chip->irq = -1;
2574 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002575 if (chip->msi)
Takashi Iwai43001c92006-09-08 12:30:03 +02002576 pci_disable_msi(chip->pci);
Takashi Iwai421a1252005-11-17 16:11:09 +01002577 pci_disable_device(pci);
2578 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002579 pci_set_power_state(pci, PCI_D3hot);
Wang Xingchao99a20082013-05-30 22:07:10 +08002580 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2581 hda_display_power(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return 0;
2583}
2584
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002585static int azx_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002587 struct pci_dev *pci = to_pci_dev(dev);
2588 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002589 struct azx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Takashi Iwaic5c21522012-12-04 17:01:25 +01002591 if (chip->disabled)
2592 return 0;
2593
Wang Xingchao99a20082013-05-30 22:07:10 +08002594 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2595 hda_display_power(true);
Takashi Iwaid14a7e02009-02-16 10:13:03 +01002596 pci_set_power_state(pci, PCI_D0);
2597 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002598 if (pci_enable_device(pci) < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002599 dev_err(chip->card->dev,
2600 "pci_enable_device failed, disabling device\n");
Takashi Iwai30b35392006-10-11 18:52:53 +02002601 snd_card_disconnect(card);
2602 return -EIO;
2603 }
2604 pci_set_master(pci);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002605 if (chip->msi)
2606 if (pci_enable_msi(pci) < 0)
2607 chip->msi = 0;
2608 if (azx_acquire_irq(chip, 1) < 0)
Takashi Iwai30b35392006-10-11 18:52:53 +02002609 return -EIO;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002610 azx_init_pci(chip);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002611
Takashi Iwai7f308302012-05-08 16:52:23 +02002612 azx_init_chip(chip, 1);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 snd_hda_resume(chip->bus);
Takashi Iwai421a1252005-11-17 16:11:09 +01002615 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return 0;
2617}
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002618#endif /* CONFIG_PM_SLEEP || SUPPORT_VGA_SWITCHEROO */
2619
2620#ifdef CONFIG_PM_RUNTIME
2621static int azx_runtime_suspend(struct device *dev)
2622{
2623 struct snd_card *card = dev_get_drvdata(dev);
2624 struct azx *chip = card->private_data;
2625
Dave Airlie246efa42013-07-29 15:19:29 +10002626 if (chip->disabled)
2627 return 0;
2628
2629 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2630 return 0;
2631
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002632 /* enable controller wake up event */
2633 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) |
2634 STATESTS_INT_MASK);
2635
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002636 azx_stop_chip(chip);
Takashi Iwai873ce8a2013-11-26 11:58:40 +01002637 azx_enter_link_reset(chip);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002638 azx_clear_irq_pending(chip);
Wang Xingchao99a20082013-05-30 22:07:10 +08002639 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2640 hda_display_power(false);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002641 return 0;
2642}
2643
2644static int azx_runtime_resume(struct device *dev)
2645{
2646 struct snd_card *card = dev_get_drvdata(dev);
2647 struct azx *chip = card->private_data;
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002648 struct hda_bus *bus;
2649 struct hda_codec *codec;
2650 int status;
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002651
Dave Airlie246efa42013-07-29 15:19:29 +10002652 if (chip->disabled)
2653 return 0;
2654
2655 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2656 return 0;
2657
Wang Xingchao99a20082013-05-30 22:07:10 +08002658 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2659 hda_display_power(true);
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002660
2661 /* Read STATESTS before controller reset */
2662 status = azx_readw(chip, STATESTS);
2663
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002664 azx_init_pci(chip);
2665 azx_init_chip(chip, 1);
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002666
2667 bus = chip->bus;
2668 if (status && bus) {
2669 list_for_each_entry(codec, &bus->codec_list, list)
2670 if (status & (1 << codec->addr))
2671 queue_delayed_work(codec->bus->workq,
2672 &codec->jackpoll_work, codec->jackpoll_interval);
2673 }
2674
2675 /* disable controller Wake Up event*/
2676 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) &
2677 ~STATESTS_INT_MASK);
2678
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002679 return 0;
2680}
Takashi Iwai6eb827d2012-12-12 11:50:12 +01002681
2682static int azx_runtime_idle(struct device *dev)
2683{
2684 struct snd_card *card = dev_get_drvdata(dev);
2685 struct azx *chip = card->private_data;
2686
Dave Airlie246efa42013-07-29 15:19:29 +10002687 if (chip->disabled)
2688 return 0;
2689
Takashi Iwai6eb827d2012-12-12 11:50:12 +01002690 if (!power_save_controller ||
2691 !(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2692 return -EBUSY;
2693
2694 return 0;
2695}
2696
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002697#endif /* CONFIG_PM_RUNTIME */
2698
2699#ifdef CONFIG_PM
2700static const struct dev_pm_ops azx_pm = {
2701 SET_SYSTEM_SLEEP_PM_OPS(azx_suspend, azx_resume)
Takashi Iwai6eb827d2012-12-12 11:50:12 +01002702 SET_RUNTIME_PM_OPS(azx_runtime_suspend, azx_runtime_resume, azx_runtime_idle)
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002703};
2704
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002705#define AZX_PM_OPS &azx_pm
2706#else
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002707#define AZX_PM_OPS NULL
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002708#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710
2711/*
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002712 * reboot notifier for hang-up problem at power-down
2713 */
2714static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
2715{
2716 struct azx *chip = container_of(nb, struct azx, reboot_notifier);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01002717 snd_hda_bus_reboot_notify(chip->bus);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002718 azx_stop_chip(chip);
2719 return NOTIFY_OK;
2720}
2721
2722static void azx_notifier_register(struct azx *chip)
2723{
2724 chip->reboot_notifier.notifier_call = azx_halt;
2725 register_reboot_notifier(&chip->reboot_notifier);
2726}
2727
2728static void azx_notifier_unregister(struct azx *chip)
2729{
2730 if (chip->reboot_notifier.notifier_call)
2731 unregister_reboot_notifier(&chip->reboot_notifier);
2732}
2733
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01002734static int azx_probe_continue(struct azx *chip);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002735
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002736#ifdef SUPPORT_VGA_SWITCHEROO
Bill Pembertone23e7a12012-12-06 12:35:10 -05002737static struct pci_dev *get_bound_vga(struct pci_dev *pci);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002738
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002739static void azx_vs_set_state(struct pci_dev *pci,
2740 enum vga_switcheroo_state state)
2741{
2742 struct snd_card *card = pci_get_drvdata(pci);
2743 struct azx *chip = card->private_data;
2744 bool disabled;
2745
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002746 wait_for_completion(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002747 if (chip->init_failed)
2748 return;
2749
2750 disabled = (state == VGA_SWITCHEROO_OFF);
2751 if (chip->disabled == disabled)
2752 return;
2753
2754 if (!chip->bus) {
2755 chip->disabled = disabled;
2756 if (!disabled) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002757 dev_info(chip->card->dev,
2758 "Start delayed initialization\n");
Takashi Iwai5c906802013-05-30 22:07:09 +08002759 if (azx_probe_continue(chip) < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002760 dev_err(chip->card->dev, "initialization error\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002761 chip->init_failed = true;
2762 }
2763 }
2764 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002765 dev_info(chip->card->dev, "%s via VGA-switcheroo\n",
2766 disabled ? "Disabling" : "Enabling");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002767 if (disabled) {
Dylan Reid89287562014-02-28 15:41:15 -08002768 pm_runtime_put_sync_suspend(card->dev);
2769 azx_suspend(card->dev);
Dave Airlie246efa42013-07-29 15:19:29 +10002770 /* when we get suspended by vga switcheroo we end up in D3cold,
2771 * however we have no ACPI handle, so pci/acpi can't put us there,
2772 * put ourselves there */
2773 pci->current_state = PCI_D3cold;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002774 chip->disabled = true;
Takashi Iwai128960a2012-10-12 17:28:18 +02002775 if (snd_hda_lock_devices(chip->bus))
Takashi Iwai4e76a882014-02-25 12:21:03 +01002776 dev_warn(chip->card->dev,
2777 "Cannot lock devices!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002778 } else {
2779 snd_hda_unlock_devices(chip->bus);
Dylan Reid89287562014-02-28 15:41:15 -08002780 pm_runtime_get_noresume(card->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002781 chip->disabled = false;
Dylan Reid89287562014-02-28 15:41:15 -08002782 azx_resume(card->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002783 }
2784 }
2785}
2786
2787static bool azx_vs_can_switch(struct pci_dev *pci)
2788{
2789 struct snd_card *card = pci_get_drvdata(pci);
2790 struct azx *chip = card->private_data;
2791
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002792 wait_for_completion(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002793 if (chip->init_failed)
2794 return false;
2795 if (chip->disabled || !chip->bus)
2796 return true;
2797 if (snd_hda_lock_devices(chip->bus))
2798 return false;
2799 snd_hda_unlock_devices(chip->bus);
2800 return true;
2801}
2802
Bill Pembertone23e7a12012-12-06 12:35:10 -05002803static void init_vga_switcheroo(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002804{
2805 struct pci_dev *p = get_bound_vga(chip->pci);
2806 if (p) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002807 dev_info(chip->card->dev,
2808 "Handle VGA-switcheroo audio client\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002809 chip->use_vga_switcheroo = 1;
2810 pci_dev_put(p);
2811 }
2812}
2813
2814static const struct vga_switcheroo_client_ops azx_vs_ops = {
2815 .set_gpu_state = azx_vs_set_state,
2816 .can_switch = azx_vs_can_switch,
2817};
2818
Bill Pembertone23e7a12012-12-06 12:35:10 -05002819static int register_vga_switcheroo(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002820{
Takashi Iwai128960a2012-10-12 17:28:18 +02002821 int err;
2822
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002823 if (!chip->use_vga_switcheroo)
2824 return 0;
2825 /* FIXME: currently only handling DIS controller
2826 * is there any machine with two switchable HDMI audio controllers?
2827 */
Takashi Iwai128960a2012-10-12 17:28:18 +02002828 err = vga_switcheroo_register_audio_client(chip->pci, &azx_vs_ops,
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002829 VGA_SWITCHEROO_DIS,
2830 chip->bus != NULL);
Takashi Iwai128960a2012-10-12 17:28:18 +02002831 if (err < 0)
2832 return err;
2833 chip->vga_switcheroo_registered = 1;
Dave Airlie246efa42013-07-29 15:19:29 +10002834
2835 /* register as an optimus hdmi audio power domain */
Dylan Reid89287562014-02-28 15:41:15 -08002836 vga_switcheroo_init_domain_pm_optimus_hdmi_audio(chip->card->dev,
2837 &chip->hdmi_pm_domain);
Takashi Iwai128960a2012-10-12 17:28:18 +02002838 return 0;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002839}
2840#else
2841#define init_vga_switcheroo(chip) /* NOP */
2842#define register_vga_switcheroo(chip) 0
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002843#define check_hdmi_disabled(pci) false
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002844#endif /* SUPPORT_VGA_SWITCHER */
2845
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002846/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 * destructor
2848 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002849static int azx_free(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Wang Xingchaoc67e2222013-05-30 22:07:08 +08002851 struct pci_dev *pci = chip->pci;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002852 int i;
2853
Wang Xingchaoc67e2222013-05-30 22:07:08 +08002854 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME)
2855 && chip->running)
2856 pm_runtime_get_noresume(&pci->dev);
2857
Takashi Iwai65fcd412012-08-14 17:13:32 +02002858 azx_del_card_list(chip);
2859
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002860 azx_notifier_unregister(chip);
2861
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002862 chip->init_failed = 1; /* to be sure */
Daniel J Blueman44728e92012-12-18 23:59:33 +08002863 complete_all(&chip->probe_wait);
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002864
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002865 if (use_vga_switcheroo(chip)) {
2866 if (chip->disabled && chip->bus)
2867 snd_hda_unlock_devices(chip->bus);
Takashi Iwai128960a2012-10-12 17:28:18 +02002868 if (chip->vga_switcheroo_registered)
2869 vga_switcheroo_unregister_client(chip->pci);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002870 }
2871
Takashi Iwaice43fba2005-05-30 20:33:44 +02002872 if (chip->initialized) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002873 azx_clear_irq_pending(chip);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002874 for (i = 0; i < chip->num_streams; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 azx_stream_stop(chip, &chip->azx_dev[i]);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002876 azx_stop_chip(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878
Jeff Garzikf000fd82008-04-22 13:50:34 +02002879 if (chip->irq >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 free_irq(chip->irq, (void*)chip);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002881 if (chip->msi)
Takashi Iwai30b35392006-10-11 18:52:53 +02002882 pci_disable_msi(chip->pci);
Takashi Iwaif079c252006-06-01 11:42:14 +02002883 if (chip->remap_addr)
2884 iounmap(chip->remap_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002886 if (chip->azx_dev) {
2887 for (i = 0; i < chip->num_streams; i++)
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002888 if (chip->azx_dev[i].bdl.area) {
2889 mark_pages_wc(chip, &chip->azx_dev[i].bdl, false);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002890 snd_dma_free_pages(&chip->azx_dev[i].bdl);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002891 }
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002892 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002893 if (chip->rb.area) {
2894 mark_pages_wc(chip, &chip->rb, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 snd_dma_free_pages(&chip->rb);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002896 }
2897 if (chip->posbuf.area) {
2898 mark_pages_wc(chip, &chip->posbuf, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 snd_dma_free_pages(&chip->posbuf);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002900 }
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002901 if (chip->region_requested)
2902 pci_release_regions(chip->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 pci_disable_device(chip->pci);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002904 kfree(chip->azx_dev);
Takashi Iwai4918cda2012-08-09 12:33:28 +02002905#ifdef CONFIG_SND_HDA_PATCH_LOADER
2906 if (chip->fw)
2907 release_firmware(chip->fw);
2908#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08002909 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
2910 hda_display_power(false);
2911 hda_i915_exit();
2912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 kfree(chip);
2914
2915 return 0;
2916}
2917
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002918static int azx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
2920 return azx_free(device->device_data);
2921}
2922
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002923#ifdef SUPPORT_VGA_SWITCHEROO
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924/*
Takashi Iwai91219472012-04-26 12:13:25 +02002925 * Check of disabled HDMI controller by vga-switcheroo
2926 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002927static struct pci_dev *get_bound_vga(struct pci_dev *pci)
Takashi Iwai91219472012-04-26 12:13:25 +02002928{
2929 struct pci_dev *p;
2930
2931 /* check only discrete GPU */
2932 switch (pci->vendor) {
2933 case PCI_VENDOR_ID_ATI:
2934 case PCI_VENDOR_ID_AMD:
2935 case PCI_VENDOR_ID_NVIDIA:
2936 if (pci->devfn == 1) {
2937 p = pci_get_domain_bus_and_slot(pci_domain_nr(pci->bus),
2938 pci->bus->number, 0);
2939 if (p) {
2940 if ((p->class >> 8) == PCI_CLASS_DISPLAY_VGA)
2941 return p;
2942 pci_dev_put(p);
2943 }
2944 }
2945 break;
2946 }
2947 return NULL;
2948}
2949
Bill Pembertone23e7a12012-12-06 12:35:10 -05002950static bool check_hdmi_disabled(struct pci_dev *pci)
Takashi Iwai91219472012-04-26 12:13:25 +02002951{
2952 bool vga_inactive = false;
2953 struct pci_dev *p = get_bound_vga(pci);
2954
2955 if (p) {
Takashi Iwai12b78a72012-06-07 12:15:16 +02002956 if (vga_switcheroo_get_client_state(p) == VGA_SWITCHEROO_OFF)
Takashi Iwai91219472012-04-26 12:13:25 +02002957 vga_inactive = true;
2958 pci_dev_put(p);
2959 }
2960 return vga_inactive;
2961}
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002962#endif /* SUPPORT_VGA_SWITCHEROO */
Takashi Iwai91219472012-04-26 12:13:25 +02002963
2964/*
Takashi Iwai3372a152007-02-01 15:46:50 +01002965 * white/black-listing for position_fix
2966 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002967static struct snd_pci_quirk position_fix_list[] = {
Takashi Iwaid2e1c972008-06-10 17:53:34 +02002968 SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
2969 SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
Takashi Iwai2f703e72009-12-01 14:17:37 +01002970 SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
Takashi Iwaid2e1c972008-06-10 17:53:34 +02002971 SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
Daniel T Chendd37f8e2010-05-30 01:17:03 -04002972 SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS", POS_FIX_LPIB),
Daniel T Chen9f75c1b2010-05-30 13:08:41 -04002973 SND_PCI_QUIRK(0x1043, 0x81e7, "ASUS M2V", POS_FIX_LPIB),
Daniel T Chene96d3122010-05-27 18:32:18 -04002974 SND_PCI_QUIRK(0x104d, 0x9069, "Sony VPCS11V9E", POS_FIX_LPIB),
David Henningssonb01de4f2012-01-12 16:31:14 +01002975 SND_PCI_QUIRK(0x10de, 0xcb89, "Macbook Pro 7,1", POS_FIX_LPIB),
Daniel T Chen61bb42c2010-05-29 11:04:11 -04002976 SND_PCI_QUIRK(0x1297, 0x3166, "Shuttle", POS_FIX_LPIB),
Daniel T Chen9ec8dda2010-03-28 02:34:40 -04002977 SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
Takashi Iwai45d4ebf2009-11-30 11:58:30 +01002978 SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
Takashi Iwai8815cd02010-04-15 09:02:41 +02002979 SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
Daniel T Chenb90c0762010-05-30 19:31:41 -04002980 SND_PCI_QUIRK(0x1849, 0x0888, "775Dual-VSTA", POS_FIX_LPIB),
Daniel T Chen0e0280d2010-04-21 19:55:43 -04002981 SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
Takashi Iwai3372a152007-02-01 15:46:50 +01002982 {}
2983};
2984
Bill Pembertone23e7a12012-12-06 12:35:10 -05002985static int check_position_fix(struct azx *chip, int fix)
Takashi Iwai3372a152007-02-01 15:46:50 +01002986{
2987 const struct snd_pci_quirk *q;
2988
Takashi Iwaic673ba12009-03-17 07:49:14 +01002989 switch (fix) {
Takashi Iwai1dac6692012-09-13 14:59:47 +02002990 case POS_FIX_AUTO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01002991 case POS_FIX_LPIB:
2992 case POS_FIX_POSBUF:
David Henningsson4cb36312010-09-30 10:12:50 +02002993 case POS_FIX_VIACOMBO:
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01002994 case POS_FIX_COMBO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01002995 return fix;
2996 }
2997
Takashi Iwaic673ba12009-03-17 07:49:14 +01002998 q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
2999 if (q) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003000 dev_info(chip->card->dev,
3001 "position_fix set to %d for device %04x:%04x\n",
3002 q->value, q->subvendor, q->subdevice);
Takashi Iwaic673ba12009-03-17 07:49:14 +01003003 return q->value;
Takashi Iwai3372a152007-02-01 15:46:50 +01003004 }
David Henningssonbdd9ef22010-10-04 12:02:14 +02003005
3006 /* Check VIA/ATI HD Audio Controller exist */
Takashi Iwai9477c582011-05-25 09:11:37 +02003007 if (chip->driver_caps & AZX_DCAPS_POSFIX_VIA) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003008 dev_dbg(chip->card->dev, "Using VIACOMBO position fix\n");
David Henningssonbdd9ef22010-10-04 12:02:14 +02003009 return POS_FIX_VIACOMBO;
3010 }
Takashi Iwai9477c582011-05-25 09:11:37 +02003011 if (chip->driver_caps & AZX_DCAPS_POSFIX_LPIB) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003012 dev_dbg(chip->card->dev, "Using LPIB position fix\n");
Takashi Iwai9477c582011-05-25 09:11:37 +02003013 return POS_FIX_LPIB;
3014 }
Takashi Iwaic673ba12009-03-17 07:49:14 +01003015 return POS_FIX_AUTO;
Takashi Iwai3372a152007-02-01 15:46:50 +01003016}
3017
3018/*
Takashi Iwai669ba272007-08-17 09:17:36 +02003019 * black-lists for probe_mask
3020 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003021static struct snd_pci_quirk probe_mask_list[] = {
Takashi Iwai669ba272007-08-17 09:17:36 +02003022 /* Thinkpad often breaks the controller communication when accessing
3023 * to the non-working (or non-existing) modem codec slot.
3024 */
3025 SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
3026 SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
3027 SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
Takashi Iwai0edb9452008-11-07 14:53:09 +01003028 /* broken BIOS */
3029 SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
Takashi Iwaief1681d2008-11-24 17:29:28 +01003030 /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
3031 SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01003032 /* forced codec slots */
Ozan Çağlayan93574842009-05-23 15:00:04 +03003033 SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01003034 SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
Jaroslav Kyselaf3af9052012-04-26 17:52:35 +02003035 /* WinFast VP200 H (Teradici) user reported broken communication */
3036 SND_PCI_QUIRK(0x3a21, 0x040d, "WinFast VP200 H", 0x101),
Takashi Iwai669ba272007-08-17 09:17:36 +02003037 {}
3038};
3039
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003040#define AZX_FORCE_CODEC_MASK 0x100
3041
Bill Pembertone23e7a12012-12-06 12:35:10 -05003042static void check_probe_mask(struct azx *chip, int dev)
Takashi Iwai669ba272007-08-17 09:17:36 +02003043{
3044 const struct snd_pci_quirk *q;
3045
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003046 chip->codec_probe_mask = probe_mask[dev];
3047 if (chip->codec_probe_mask == -1) {
Takashi Iwai669ba272007-08-17 09:17:36 +02003048 q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
3049 if (q) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003050 dev_info(chip->card->dev,
3051 "probe_mask set to 0x%x for device %04x:%04x\n",
3052 q->value, q->subvendor, q->subdevice);
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003053 chip->codec_probe_mask = q->value;
Takashi Iwai669ba272007-08-17 09:17:36 +02003054 }
3055 }
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003056
3057 /* check forced option */
3058 if (chip->codec_probe_mask != -1 &&
3059 (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
3060 chip->codec_mask = chip->codec_probe_mask & 0xff;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003061 dev_info(chip->card->dev, "codec_mask forced to 0x%x\n",
3062 chip->codec_mask);
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003063 }
Takashi Iwai669ba272007-08-17 09:17:36 +02003064}
3065
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003066/*
Takashi Iwai716238552009-09-28 13:14:04 +02003067 * white/black-list for enable_msi
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003068 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003069static struct snd_pci_quirk msi_black_list[] = {
David Henningsson693e0cb2013-12-12 09:52:03 +01003070 SND_PCI_QUIRK(0x103c, 0x2191, "HP", 0), /* AMD Hudson */
3071 SND_PCI_QUIRK(0x103c, 0x2192, "HP", 0), /* AMD Hudson */
3072 SND_PCI_QUIRK(0x103c, 0x21f7, "HP", 0), /* AMD Hudson */
3073 SND_PCI_QUIRK(0x103c, 0x21fa, "HP", 0), /* AMD Hudson */
Takashi Iwai9dc83982009-12-22 08:15:01 +01003074 SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai0a27fcf2010-02-15 17:05:28 +01003075 SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
Ralf Gerbigecd21622010-03-09 18:25:47 +01003076 SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
Takashi Iwai83f72152013-09-09 10:20:48 +02003077 SND_PCI_QUIRK(0x1179, 0xfb44, "Toshiba Satellite C870", 0), /* AMD Hudson */
Michele Ballabio4193d132010-03-06 21:06:46 +01003078 SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai38155952010-04-04 12:14:03 +02003079 SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003080 {}
3081};
3082
Bill Pembertone23e7a12012-12-06 12:35:10 -05003083static void check_msi(struct azx *chip)
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003084{
3085 const struct snd_pci_quirk *q;
3086
Takashi Iwai716238552009-09-28 13:14:04 +02003087 if (enable_msi >= 0) {
3088 chip->msi = !!enable_msi;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003089 return;
Takashi Iwai716238552009-09-28 13:14:04 +02003090 }
3091 chip->msi = 1; /* enable MSI as default */
3092 q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003093 if (q) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003094 dev_info(chip->card->dev,
3095 "msi for device %04x:%04x set to %d\n",
3096 q->subvendor, q->subdevice, q->value);
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003097 chip->msi = q->value;
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003098 return;
3099 }
3100
3101 /* NVidia chipsets seem to cause troubles with MSI */
Takashi Iwai9477c582011-05-25 09:11:37 +02003102 if (chip->driver_caps & AZX_DCAPS_NO_MSI) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003103 dev_info(chip->card->dev, "Disabling MSI\n");
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003104 chip->msi = 0;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003105 }
3106}
3107
Takashi Iwaia1585d72011-12-14 09:27:04 +01003108/* check the snoop mode availability */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003109static void azx_check_snoop_available(struct azx *chip)
Takashi Iwaia1585d72011-12-14 09:27:04 +01003110{
3111 bool snoop = chip->snoop;
3112
3113 switch (chip->driver_type) {
3114 case AZX_DRIVER_VIA:
3115 /* force to non-snoop mode for a new VIA controller
3116 * when BIOS is set
3117 */
3118 if (snoop) {
3119 u8 val;
3120 pci_read_config_byte(chip->pci, 0x42, &val);
3121 if (!(val & 0x80) && chip->pci->revision == 0x30)
3122 snoop = false;
3123 }
3124 break;
3125 case AZX_DRIVER_ATIHDMI_NS:
3126 /* new ATI HDMI requires non-snoop */
3127 snoop = false;
3128 break;
Takashi Iwaic1279f82013-02-07 17:36:22 +01003129 case AZX_DRIVER_CTHDA:
3130 snoop = false;
3131 break;
Takashi Iwaia1585d72011-12-14 09:27:04 +01003132 }
3133
3134 if (snoop != chip->snoop) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003135 dev_info(chip->card->dev, "Force to %s mode\n",
3136 snoop ? "snoop" : "non-snoop");
Takashi Iwaia1585d72011-12-14 09:27:04 +01003137 chip->snoop = snoop;
3138 }
3139}
Takashi Iwai669ba272007-08-17 09:17:36 +02003140
Wang Xingchao99a20082013-05-30 22:07:10 +08003141static void azx_probe_work(struct work_struct *work)
3142{
3143 azx_probe_continue(container_of(work, struct azx, probe_work));
3144}
Wang Xingchao99a20082013-05-30 22:07:10 +08003145
Takashi Iwai669ba272007-08-17 09:17:36 +02003146/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 * constructor
3148 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003149static int azx_create(struct snd_card *card, struct pci_dev *pci,
3150 int dev, unsigned int driver_caps,
Dylan Reid40830812014-02-28 15:41:13 -08003151 const struct hda_controller_ops *hda_ops,
Bill Pembertone23e7a12012-12-06 12:35:10 -05003152 struct azx **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003154 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 .dev_free = azx_dev_free,
3156 };
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003157 struct azx *chip;
3158 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 *rchip = NULL;
Tobin Davisbcd72002008-01-15 11:23:55 +01003161
Pavel Machek927fc862006-08-31 17:03:43 +02003162 err = pci_enable_device(pci);
3163 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return err;
3165
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003166 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003167 if (!chip) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003168 dev_err(card->dev, "Cannot allocate chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 pci_disable_device(pci);
3170 return -ENOMEM;
3171 }
3172
3173 spin_lock_init(&chip->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01003174 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 chip->card = card;
3176 chip->pci = pci;
Dylan Reid40830812014-02-28 15:41:13 -08003177 chip->ops = hda_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 chip->irq = -1;
Takashi Iwai9477c582011-05-25 09:11:37 +02003179 chip->driver_caps = driver_caps;
3180 chip->driver_type = driver_caps & 0xff;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003181 check_msi(chip);
Takashi Iwai555e2192008-06-10 17:53:34 +02003182 chip->dev_index = dev;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02003183 INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003184 INIT_LIST_HEAD(&chip->pcm_list);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003185 INIT_LIST_HEAD(&chip->list);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003186 init_vga_switcheroo(chip);
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003187 init_completion(&chip->probe_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Shahin Ghazinouribeaffc32010-05-11 08:19:55 +02003189 chip->position_fix[0] = chip->position_fix[1] =
3190 check_position_fix(chip, position_fix[dev]);
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01003191 /* combo mode uses LPIB for playback */
3192 if (chip->position_fix[0] == POS_FIX_COMBO) {
3193 chip->position_fix[0] = POS_FIX_LPIB;
3194 chip->position_fix[1] = POS_FIX_AUTO;
3195 }
3196
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003197 check_probe_mask(chip, dev);
Takashi Iwai3372a152007-02-01 15:46:50 +01003198
Takashi Iwai27346162006-01-12 18:28:44 +01003199 chip->single_cmd = single_cmd;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003200 chip->snoop = hda_snoop;
Takashi Iwaia1585d72011-12-14 09:27:04 +01003201 azx_check_snoop_available(chip);
Takashi Iwaic74db862005-05-12 14:26:27 +02003202
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003203 if (bdl_pos_adj[dev] < 0) {
3204 switch (chip->driver_type) {
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003205 case AZX_DRIVER_ICH:
Seth Heasley32679f92010-02-22 17:31:09 -08003206 case AZX_DRIVER_PCH:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003207 bdl_pos_adj[dev] = 1;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003208 break;
3209 default:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003210 bdl_pos_adj[dev] = 32;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003211 break;
3212 }
3213 }
Dylan Reid9cdc0112014-02-28 15:41:14 -08003214 chip->bdl_pos_adj = bdl_pos_adj;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003215
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003216 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
3217 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003218 dev_err(card->dev, "Error creating device [card]!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003219 azx_free(chip);
3220 return err;
3221 }
3222
Wang Xingchao99a20082013-05-30 22:07:10 +08003223 /* continue probing in work context as may trigger request module */
3224 INIT_WORK(&chip->probe_work, azx_probe_work);
Wang Xingchao99a20082013-05-30 22:07:10 +08003225
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003226 *rchip = chip;
Wang Xingchao99a20082013-05-30 22:07:10 +08003227
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003228 return 0;
3229}
3230
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01003231static int azx_first_init(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003232{
3233 int dev = chip->dev_index;
3234 struct pci_dev *pci = chip->pci;
3235 struct snd_card *card = chip->card;
3236 int i, err;
3237 unsigned short gcap;
3238
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003239#if BITS_PER_LONG != 64
3240 /* Fix up base address on ULI M5461 */
3241 if (chip->driver_type == AZX_DRIVER_ULI) {
3242 u16 tmp3;
3243 pci_read_config_word(pci, 0x40, &tmp3);
3244 pci_write_config_word(pci, 0x40, tmp3 | 0x10);
3245 pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
3246 }
3247#endif
3248
Pavel Machek927fc862006-08-31 17:03:43 +02003249 err = pci_request_regions(pci, "ICH HD audio");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003250 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return err;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003252 chip->region_requested = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Pavel Machek927fc862006-08-31 17:03:43 +02003254 chip->addr = pci_resource_start(pci, 0);
Arjan van de Ven2f5ad542008-09-28 16:20:09 -07003255 chip->remap_addr = pci_ioremap_bar(pci, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 if (chip->remap_addr == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003257 dev_err(card->dev, "ioremap error\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003258 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 }
3260
Takashi Iwai68e7fff2006-10-23 13:40:59 +02003261 if (chip->msi)
3262 if (pci_enable_msi(pci) < 0)
3263 chip->msi = 0;
Stephen Hemminger7376d012006-08-21 19:17:46 +02003264
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003265 if (azx_acquire_irq(chip, 0) < 0)
3266 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 pci_set_master(pci);
3269 synchronize_irq(chip->irq);
3270
Tobin Davisbcd72002008-01-15 11:23:55 +01003271 gcap = azx_readw(chip, GCAP);
Takashi Iwai4e76a882014-02-25 12:21:03 +01003272 dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
Tobin Davisbcd72002008-01-15 11:23:55 +01003273
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003274 /* disable SB600 64bit support for safety */
Takashi Iwai9477c582011-05-25 09:11:37 +02003275 if (chip->pci->vendor == PCI_VENDOR_ID_ATI) {
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003276 struct pci_dev *p_smbus;
3277 p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
3278 PCI_DEVICE_ID_ATI_SBX00_SMBUS,
3279 NULL);
3280 if (p_smbus) {
3281 if (p_smbus->revision < 0x30)
3282 gcap &= ~ICH6_GCAP_64OK;
3283 pci_dev_put(p_smbus);
3284 }
3285 }
Takashi Iwai09240cf2009-03-17 07:47:18 +01003286
Takashi Iwai9477c582011-05-25 09:11:37 +02003287 /* disable 64bit DMA address on some devices */
3288 if (chip->driver_caps & AZX_DCAPS_NO_64BIT) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003289 dev_dbg(card->dev, "Disabling 64bit DMA\n");
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003290 gcap &= ~ICH6_GCAP_64OK;
Takashi Iwai9477c582011-05-25 09:11:37 +02003291 }
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003292
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003293 /* disable buffer size rounding to 128-byte multiples if supported */
Takashi Iwai7bfe0592012-01-23 17:53:39 +01003294 if (align_buffer_size >= 0)
3295 chip->align_buffer_size = !!align_buffer_size;
3296 else {
3297 if (chip->driver_caps & AZX_DCAPS_BUFSIZE)
3298 chip->align_buffer_size = 0;
3299 else if (chip->driver_caps & AZX_DCAPS_ALIGN_BUFSIZE)
3300 chip->align_buffer_size = 1;
3301 else
3302 chip->align_buffer_size = 1;
3303 }
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003304
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003305 /* allow 64bit DMA address if supported by H/W */
Takashi Iwaib21fadb2009-05-28 12:26:15 +02003306 if ((gcap & ICH6_GCAP_64OK) && !pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
Yang Hongyange9304382009-04-13 14:40:14 -07003307 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003308 else {
Yang Hongyange9304382009-04-13 14:40:14 -07003309 pci_set_dma_mask(pci, DMA_BIT_MASK(32));
3310 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003311 }
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003312
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003313 /* read number of streams from GCAP register instead of using
3314 * hardcoded value
3315 */
3316 chip->capture_streams = (gcap >> 8) & 0x0f;
3317 chip->playback_streams = (gcap >> 12) & 0x0f;
3318 if (!chip->playback_streams && !chip->capture_streams) {
Tobin Davisbcd72002008-01-15 11:23:55 +01003319 /* gcap didn't give any info, switching to old method */
3320
3321 switch (chip->driver_type) {
3322 case AZX_DRIVER_ULI:
3323 chip->playback_streams = ULI_NUM_PLAYBACK;
3324 chip->capture_streams = ULI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003325 break;
3326 case AZX_DRIVER_ATIHDMI:
Andiry Xu1815b342011-12-14 16:10:27 +08003327 case AZX_DRIVER_ATIHDMI_NS:
Tobin Davisbcd72002008-01-15 11:23:55 +01003328 chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
3329 chip->capture_streams = ATIHDMI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003330 break;
Yang, Libinc4da29c2008-11-13 11:07:07 +01003331 case AZX_DRIVER_GENERIC:
Tobin Davisbcd72002008-01-15 11:23:55 +01003332 default:
3333 chip->playback_streams = ICH6_NUM_PLAYBACK;
3334 chip->capture_streams = ICH6_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003335 break;
3336 }
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003337 }
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003338 chip->capture_index_offset = 0;
3339 chip->playback_index_offset = chip->capture_streams;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003340 chip->num_streams = chip->playback_streams + chip->capture_streams;
Takashi Iwaid01ce992007-07-27 16:52:19 +02003341 chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
3342 GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003343 if (!chip->azx_dev) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003344 dev_err(card->dev, "cannot malloc azx_dev\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003345 return -ENOMEM;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003346 }
3347
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003348 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaieb49faa2013-03-15 09:19:11 +01003349 dsp_lock_init(&chip->azx_dev[i]);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003350 /* allocate memory for the BDL for each stream */
3351 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
Dylan Reid89287562014-02-28 15:41:15 -08003352 chip->card->dev,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003353 BDL_SIZE, &chip->azx_dev[i].bdl);
3354 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003355 dev_err(card->dev, "cannot allocate BDL\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003356 return -ENOMEM;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003357 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003358 mark_pages_wc(chip, &chip->azx_dev[i].bdl, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02003360 /* allocate memory for the position buffer */
Takashi Iwaid01ce992007-07-27 16:52:19 +02003361 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
Dylan Reid89287562014-02-28 15:41:15 -08003362 chip->card->dev,
Takashi Iwaid01ce992007-07-27 16:52:19 +02003363 chip->num_streams * 8, &chip->posbuf);
3364 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003365 dev_err(card->dev, "cannot allocate posbuf\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003366 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003368 mark_pages_wc(chip, &chip->posbuf, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 /* allocate CORB/RIRB */
Takashi Iwai817408612009-05-26 15:22:00 +02003370 err = azx_alloc_cmd_io(chip);
3371 if (err < 0)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003372 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
3374 /* initialize streams */
3375 azx_init_stream(chip);
3376
3377 /* initialize chip */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003378 azx_init_pci(chip);
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003379 azx_init_chip(chip, (probe_only[dev] & 2) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
3381 /* codec detection */
Pavel Machek927fc862006-08-31 17:03:43 +02003382 if (!chip->codec_mask) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003383 dev_err(card->dev, "no codecs found!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003384 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
3386
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003387 strcpy(card->driver, "HDA-Intel");
Takashi Iwai18cb7102009-04-16 10:22:24 +02003388 strlcpy(card->shortname, driver_short_names[chip->driver_type],
3389 sizeof(card->shortname));
3390 snprintf(card->longname, sizeof(card->longname),
3391 "%s at 0x%lx irq %i",
3392 card->shortname, chip->addr, chip->irq);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
Takashi Iwaicb53c622007-08-10 17:21:45 +02003397static void power_down_all_codecs(struct azx *chip)
3398{
Takashi Iwai83012a72012-08-24 18:38:08 +02003399#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003400 /* The codecs were powered up in snd_hda_codec_new().
3401 * Now all initialization done, so turn them down if possible
3402 */
3403 struct hda_codec *codec;
3404 list_for_each_entry(codec, &chip->bus->codec_list, list) {
3405 snd_hda_power_down(codec);
3406 }
3407#endif
3408}
3409
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003410#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003411/* callback from request_firmware_nowait() */
3412static void azx_firmware_cb(const struct firmware *fw, void *context)
3413{
3414 struct snd_card *card = context;
3415 struct azx *chip = card->private_data;
3416 struct pci_dev *pci = chip->pci;
3417
3418 if (!fw) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003419 dev_err(card->dev, "Cannot load firmware, aborting\n");
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003420 goto error;
3421 }
3422
3423 chip->fw = fw;
3424 if (!chip->disabled) {
3425 /* continue probing */
3426 if (azx_probe_continue(chip))
3427 goto error;
3428 }
3429 return; /* OK */
3430
3431 error:
3432 snd_card_free(card);
3433 pci_set_drvdata(pci, NULL);
3434}
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003435#endif
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003436
Dylan Reid40830812014-02-28 15:41:13 -08003437/*
3438 * HDA controller ops.
3439 */
3440
3441/* PCI register access. */
3442static void pci_azx_writel(u32 value, u32 *addr)
3443{
3444 writel(value, addr);
3445}
3446
3447static u32 pci_azx_readl(u32 *addr)
3448{
3449 return readl(addr);
3450}
3451
3452static void pci_azx_writew(u16 value, u16 *addr)
3453{
3454 writew(value, addr);
3455}
3456
3457static u16 pci_azx_readw(u16 *addr)
3458{
3459 return readw(addr);
3460}
3461
3462static void pci_azx_writeb(u8 value, u8 *addr)
3463{
3464 writeb(value, addr);
3465}
3466
3467static u8 pci_azx_readb(u8 *addr)
3468{
3469 return readb(addr);
3470}
3471
3472static const struct hda_controller_ops pci_hda_ops = {
3473 .writel = pci_azx_writel,
3474 .readl = pci_azx_readl,
3475 .writew = pci_azx_writew,
3476 .readw = pci_azx_readw,
3477 .writeb = pci_azx_writeb,
3478 .readb = pci_azx_readb,
3479};
3480
Bill Pembertone23e7a12012-12-06 12:35:10 -05003481static int azx_probe(struct pci_dev *pci,
3482 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003484 static int dev;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003485 struct snd_card *card;
3486 struct azx *chip;
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003487 bool schedule_probe;
Pavel Machek927fc862006-08-31 17:03:43 +02003488 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003490 if (dev >= SNDRV_CARDS)
3491 return -ENODEV;
3492 if (!enable[dev]) {
3493 dev++;
3494 return -ENOENT;
3495 }
3496
Takashi Iwai60c57722014-01-29 14:20:19 +01003497 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
3498 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003499 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003500 dev_err(&pci->dev, "Error creating card!\n");
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
3503
Dylan Reid40830812014-02-28 15:41:13 -08003504 err = azx_create(card, pci, dev, pci_id->driver_data,
3505 &pci_hda_ops, &chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003506 if (err < 0)
3507 goto out_free;
Takashi Iwai421a1252005-11-17 16:11:09 +01003508 card->private_data = chip;
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003509
3510 pci_set_drvdata(pci, card);
3511
3512 err = register_vga_switcheroo(chip);
3513 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003514 dev_err(card->dev, "Error registering VGA-switcheroo client\n");
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003515 goto out_free;
3516 }
3517
3518 if (check_hdmi_disabled(pci)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003519 dev_info(card->dev, "VGA controller is disabled\n");
3520 dev_info(card->dev, "Delaying initialization\n");
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003521 chip->disabled = true;
3522 }
3523
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003524 schedule_probe = !chip->disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Takashi Iwai4918cda2012-08-09 12:33:28 +02003526#ifdef CONFIG_SND_HDA_PATCH_LOADER
3527 if (patch[dev] && *patch[dev]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003528 dev_info(card->dev, "Applying patch firmware '%s'\n",
3529 patch[dev]);
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003530 err = request_firmware_nowait(THIS_MODULE, true, patch[dev],
3531 &pci->dev, GFP_KERNEL, card,
3532 azx_firmware_cb);
Takashi Iwai4918cda2012-08-09 12:33:28 +02003533 if (err < 0)
3534 goto out_free;
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003535 schedule_probe = false; /* continued in azx_firmware_cb() */
Takashi Iwai4918cda2012-08-09 12:33:28 +02003536 }
3537#endif /* CONFIG_SND_HDA_PATCH_LOADER */
3538
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003539#ifndef CONFIG_SND_HDA_I915
3540 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
Takashi Iwai4e76a882014-02-25 12:21:03 +01003541 dev_err(card->dev, "Haswell must build in CONFIG_SND_HDA_I915\n");
Wang Xingchao99a20082013-05-30 22:07:10 +08003542#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08003543
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003544 if (schedule_probe)
3545 schedule_work(&chip->probe_work);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003546
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003547 dev++;
Takashi Iwai88d071f2013-12-02 11:12:28 +01003548 if (chip->disabled)
3549 complete_all(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003550 return 0;
3551
3552out_free:
3553 snd_card_free(card);
3554 return err;
3555}
3556
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01003557static int azx_probe_continue(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003558{
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003559 struct pci_dev *pci = chip->pci;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003560 int dev = chip->dev_index;
3561 int err;
3562
Wang Xingchao99a20082013-05-30 22:07:10 +08003563 /* Request power well for Haswell HDA controller and codec */
3564 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
David Henningssonc841ad22013-08-19 13:32:30 +02003565#ifdef CONFIG_SND_HDA_I915
Wang Xingchao99a20082013-05-30 22:07:10 +08003566 err = hda_i915_init();
3567 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003568 dev_err(chip->card->dev,
3569 "Error request power-well from i915\n");
Wang Xingchao99a20082013-05-30 22:07:10 +08003570 goto out_free;
3571 }
David Henningssonc841ad22013-08-19 13:32:30 +02003572#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08003573 hda_display_power(true);
3574 }
3575
Takashi Iwai5c906802013-05-30 22:07:09 +08003576 err = azx_first_init(chip);
3577 if (err < 0)
3578 goto out_free;
3579
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01003580#ifdef CONFIG_SND_HDA_INPUT_BEEP
3581 chip->beep_mode = beep_mode[dev];
3582#endif
3583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 /* create codec instances */
Takashi Iwaia1e21c92009-06-17 09:33:52 +02003585 err = azx_codec_create(chip, model[dev]);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003586 if (err < 0)
3587 goto out_free;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003588#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai4918cda2012-08-09 12:33:28 +02003589 if (chip->fw) {
3590 err = snd_hda_load_patch(chip->bus, chip->fw->size,
3591 chip->fw->data);
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003592 if (err < 0)
3593 goto out_free;
Takashi Iwaie39ae852012-11-22 16:18:13 +01003594#ifndef CONFIG_PM
Takashi Iwai4918cda2012-08-09 12:33:28 +02003595 release_firmware(chip->fw); /* no longer needed */
3596 chip->fw = NULL;
Takashi Iwaie39ae852012-11-22 16:18:13 +01003597#endif
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003598 }
3599#endif
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003600 if ((probe_only[dev] & 1) == 0) {
Takashi Iwaia1e21c92009-06-17 09:33:52 +02003601 err = azx_codec_configure(chip);
3602 if (err < 0)
3603 goto out_free;
3604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606 /* create PCM streams */
Takashi Iwai176d5332008-07-30 15:01:44 +02003607 err = snd_hda_build_pcms(chip->bus);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003608 if (err < 0)
3609 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
3611 /* create mixer controls */
Takashi Iwaid01ce992007-07-27 16:52:19 +02003612 err = azx_mixer_create(chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003613 if (err < 0)
3614 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003616 err = snd_card_register(chip->card);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003617 if (err < 0)
3618 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Takashi Iwaicb53c622007-08-10 17:21:45 +02003620 chip->running = 1;
3621 power_down_all_codecs(chip);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01003622 azx_notifier_register(chip);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003623 azx_add_card_list(chip);
Dave Airlie246efa42013-07-29 15:19:29 +10003624 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME) || chip->use_vga_switcheroo)
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003625 pm_runtime_put_noidle(&pci->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003627out_free:
Takashi Iwai88d071f2013-12-02 11:12:28 +01003628 if (err < 0)
3629 chip->init_failed = 1;
3630 complete_all(&chip->probe_wait);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003631 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Bill Pembertone23e7a12012-12-06 12:35:10 -05003634static void azx_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
Takashi Iwai91219472012-04-26 12:13:25 +02003636 struct snd_card *card = pci_get_drvdata(pci);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003637
Takashi Iwai91219472012-04-26 12:13:25 +02003638 if (card)
3639 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640}
3641
3642/* PCI IDs */
Alexey Dobriyancebe41d2010-02-06 00:21:03 +02003643static DEFINE_PCI_DEVICE_TABLE(azx_ids) = {
Seth Heasleyd2f2fcd2010-01-12 17:03:35 -08003644 /* CPT */
Takashi Iwai9477c582011-05-25 09:11:37 +02003645 { PCI_DEVICE(0x8086, 0x1c20),
Takashi Iwaid7dab4d2013-01-08 13:51:30 +01003646 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
Seth Heasleycea310e2010-09-10 16:29:56 -07003647 /* PBG */
Takashi Iwai9477c582011-05-25 09:11:37 +02003648 { PCI_DEVICE(0x8086, 0x1d20),
Takashi Iwaid7dab4d2013-01-08 13:51:30 +01003649 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
Seth Heasleyd2edeb72011-04-20 10:59:57 -07003650 /* Panther Point */
Takashi Iwai9477c582011-05-25 09:11:37 +02003651 { PCI_DEVICE(0x8086, 0x1e20),
Takashi Iwaib1920c22013-11-22 12:43:25 +01003652 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
Seth Heasley8bc039a2012-01-23 16:24:31 -08003653 /* Lynx Point */
3654 { PCI_DEVICE(0x8086, 0x8c20),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003655 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston884b0882013-02-08 17:29:40 -08003656 /* Wellsburg */
3657 { PCI_DEVICE(0x8086, 0x8d20),
3658 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
3659 { PCI_DEVICE(0x8086, 0x8d21),
3660 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston144dad92012-08-09 09:38:59 -07003661 /* Lynx Point-LP */
3662 { PCI_DEVICE(0x8086, 0x9c20),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003663 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston144dad92012-08-09 09:38:59 -07003664 /* Lynx Point-LP */
3665 { PCI_DEVICE(0x8086, 0x9c21),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003666 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston4eeca492013-11-04 09:27:45 -08003667 /* Wildcat Point-LP */
3668 { PCI_DEVICE(0x8086, 0x9ca0),
3669 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
Wang Xingchaoe926f2c2012-06-13 10:23:51 +08003670 /* Haswell */
Wang Xingchao4a7c5162013-02-01 22:42:19 +08003671 { PCI_DEVICE(0x8086, 0x0a0c),
Takashi Iwaifab12852013-11-05 17:54:05 +01003672 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Wang Xingchaoe926f2c2012-06-13 10:23:51 +08003673 { PCI_DEVICE(0x8086, 0x0c0c),
Takashi Iwaifab12852013-11-05 17:54:05 +01003674 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Wang Xingchaod279fae2012-09-17 13:10:23 +08003675 { PCI_DEVICE(0x8086, 0x0d0c),
Takashi Iwaifab12852013-11-05 17:54:05 +01003676 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Mengdong Lin862d7612014-01-08 15:55:14 -05003677 /* Broadwell */
3678 { PCI_DEVICE(0x8086, 0x160c),
3679 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Pierre-Louis Bossart99df18b2012-09-21 18:39:07 -05003680 /* 5 Series/3400 */
3681 { PCI_DEVICE(0x8086, 0x3b56),
Takashi Iwai2c1350f2013-02-14 09:44:55 +01003682 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
Takashi Iwaif748abc2013-01-29 10:12:23 +01003683 /* Poulsbo */
Takashi Iwai9477c582011-05-25 09:11:37 +02003684 { PCI_DEVICE(0x8086, 0x811b),
Takashi Iwaif748abc2013-01-29 10:12:23 +01003685 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
3686 /* Oaktrail */
Li Peng09904b92011-12-28 15:17:26 +00003687 { PCI_DEVICE(0x8086, 0x080a),
Takashi Iwaif748abc2013-01-29 10:12:23 +01003688 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
Chew, Chiau Eee44007e2013-05-16 15:36:12 +08003689 /* BayTrail */
3690 { PCI_DEVICE(0x8086, 0x0f04),
3691 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
David Henningsson645e9032011-12-14 15:52:30 +08003692 /* ICH */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003693 { PCI_DEVICE(0x8086, 0x2668),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003694 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3695 AZX_DCAPS_BUFSIZE }, /* ICH6 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003696 { PCI_DEVICE(0x8086, 0x27d8),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003697 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3698 AZX_DCAPS_BUFSIZE }, /* ICH7 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003699 { PCI_DEVICE(0x8086, 0x269a),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003700 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3701 AZX_DCAPS_BUFSIZE }, /* ESB2 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003702 { PCI_DEVICE(0x8086, 0x284b),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003703 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3704 AZX_DCAPS_BUFSIZE }, /* ICH8 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003705 { PCI_DEVICE(0x8086, 0x293e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003706 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3707 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003708 { PCI_DEVICE(0x8086, 0x293f),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003709 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3710 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003711 { PCI_DEVICE(0x8086, 0x3a3e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003712 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3713 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003714 { PCI_DEVICE(0x8086, 0x3a6e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003715 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3716 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwaib6864532010-09-15 10:17:26 +02003717 /* Generic Intel */
3718 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
3719 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3720 .class_mask = 0xffffff,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003721 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_BUFSIZE },
Takashi Iwai9477c582011-05-25 09:11:37 +02003722 /* ATI SB 450/600/700/800/900 */
3723 { PCI_DEVICE(0x1002, 0x437b),
3724 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
3725 { PCI_DEVICE(0x1002, 0x4383),
3726 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
3727 /* AMD Hudson */
3728 { PCI_DEVICE(0x1022, 0x780d),
3729 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB },
Takashi Iwai87218e92008-02-21 08:13:11 +01003730 /* ATI HDMI */
Takashi Iwai9477c582011-05-25 09:11:37 +02003731 { PCI_DEVICE(0x1002, 0x793b),
3732 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3733 { PCI_DEVICE(0x1002, 0x7919),
3734 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3735 { PCI_DEVICE(0x1002, 0x960f),
3736 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3737 { PCI_DEVICE(0x1002, 0x970f),
3738 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3739 { PCI_DEVICE(0x1002, 0xaa00),
3740 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3741 { PCI_DEVICE(0x1002, 0xaa08),
3742 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3743 { PCI_DEVICE(0x1002, 0xaa10),
3744 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3745 { PCI_DEVICE(0x1002, 0xaa18),
3746 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3747 { PCI_DEVICE(0x1002, 0xaa20),
3748 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3749 { PCI_DEVICE(0x1002, 0xaa28),
3750 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3751 { PCI_DEVICE(0x1002, 0xaa30),
3752 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3753 { PCI_DEVICE(0x1002, 0xaa38),
3754 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3755 { PCI_DEVICE(0x1002, 0xaa40),
3756 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3757 { PCI_DEVICE(0x1002, 0xaa48),
3758 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
Clemens Ladischbbaa0d62013-11-05 09:27:10 +01003759 { PCI_DEVICE(0x1002, 0xaa50),
3760 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3761 { PCI_DEVICE(0x1002, 0xaa58),
3762 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3763 { PCI_DEVICE(0x1002, 0xaa60),
3764 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3765 { PCI_DEVICE(0x1002, 0xaa68),
3766 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3767 { PCI_DEVICE(0x1002, 0xaa80),
3768 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3769 { PCI_DEVICE(0x1002, 0xaa88),
3770 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3771 { PCI_DEVICE(0x1002, 0xaa90),
3772 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3773 { PCI_DEVICE(0x1002, 0xaa98),
3774 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Xu1815b342011-12-14 16:10:27 +08003775 { PCI_DEVICE(0x1002, 0x9902),
3776 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3777 { PCI_DEVICE(0x1002, 0xaaa0),
3778 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3779 { PCI_DEVICE(0x1002, 0xaaa8),
3780 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3781 { PCI_DEVICE(0x1002, 0xaab0),
3782 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
Takashi Iwai87218e92008-02-21 08:13:11 +01003783 /* VIA VT8251/VT8237A */
Takashi Iwai9477c582011-05-25 09:11:37 +02003784 { PCI_DEVICE(0x1106, 0x3288),
3785 .driver_data = AZX_DRIVER_VIA | AZX_DCAPS_POSFIX_VIA },
Annie Liu754fdff2012-06-08 19:18:39 +08003786 /* VIA GFX VT7122/VX900 */
3787 { PCI_DEVICE(0x1106, 0x9170), .driver_data = AZX_DRIVER_GENERIC },
3788 /* VIA GFX VT6122/VX11 */
3789 { PCI_DEVICE(0x1106, 0x9140), .driver_data = AZX_DRIVER_GENERIC },
Takashi Iwai87218e92008-02-21 08:13:11 +01003790 /* SIS966 */
3791 { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
3792 /* ULI M5461 */
3793 { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
3794 /* NVIDIA MCP */
Takashi Iwai0c2fd1bf42009-12-18 16:41:39 +01003795 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
3796 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3797 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003798 .driver_data = AZX_DRIVER_NVIDIA | AZX_DCAPS_PRESET_NVIDIA },
Kailang Yangf2690022008-05-27 11:44:55 +02003799 /* Teradici */
Takashi Iwai9477c582011-05-25 09:11:37 +02003800 { PCI_DEVICE(0x6549, 0x1200),
3801 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
Lars R. Damerowf0b3da92012-11-02 13:10:39 -07003802 { PCI_DEVICE(0x6549, 0x2200),
3803 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
Takashi Iwai4e01f542009-04-16 08:53:34 +02003804 /* Creative X-Fi (CA0110-IBG) */
Takashi Iwaif2a8eca2012-06-11 15:51:54 +02003805 /* CTHDA chips */
3806 { PCI_DEVICE(0x1102, 0x0010),
3807 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
3808 { PCI_DEVICE(0x1102, 0x0012),
3809 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +01003810#if !IS_ENABLED(CONFIG_SND_CTXFI)
Takashi Iwai313f6e22009-05-18 12:40:52 +02003811 /* the following entry conflicts with snd-ctxfi driver,
3812 * as ctxfi driver mutates from HD-audio to native mode with
3813 * a special command sequence.
3814 */
Takashi Iwai4e01f542009-04-16 08:53:34 +02003815 { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
3816 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3817 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003818 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01003819 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02003820#else
3821 /* this entry seems still valid -- i.e. without emu20kx chip */
Takashi Iwai9477c582011-05-25 09:11:37 +02003822 { PCI_DEVICE(0x1102, 0x0009),
3823 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01003824 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02003825#endif
Otavio Salvadore35d4b12010-09-26 23:35:06 -03003826 /* Vortex86MX */
3827 { PCI_DEVICE(0x17f3, 0x3010), .driver_data = AZX_DRIVER_GENERIC },
Bankim Bhavsar0f0714c52011-01-17 15:23:21 +01003828 /* VMware HDAudio */
3829 { PCI_DEVICE(0x15ad, 0x1977), .driver_data = AZX_DRIVER_GENERIC },
Andiry Brienza9176b672009-07-17 11:32:32 +08003830 /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
Yang, Libinc4da29c2008-11-13 11:07:07 +01003831 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
3832 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3833 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003834 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Brienza9176b672009-07-17 11:32:32 +08003835 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
3836 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3837 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003838 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 { 0, }
3840};
3841MODULE_DEVICE_TABLE(pci, azx_ids);
3842
3843/* pci_driver definition */
Takashi Iwaie9f66d92012-04-24 12:25:00 +02003844static struct pci_driver azx_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02003845 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 .id_table = azx_ids,
3847 .probe = azx_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05003848 .remove = azx_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003849 .driver = {
3850 .pm = AZX_PM_OPS,
3851 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852};
3853
Takashi Iwaie9f66d92012-04-24 12:25:00 +02003854module_pci_driver(azx_driver);