blob: aa8b765c92995c455fd1f67b43ea0e8c98c66b0a [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;
Dylan Reide62a42a2014-02-28 15:41:19 -0800131static int *power_save_addr = &power_save;
Takashi Iwai65fcd412012-08-14 17:13:32 +0200132module_param(power_save, xint, 0644);
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100133MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
134 "(in second, 0 = disable).");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Takashi Iwaidee1b662007-08-13 16:10:30 +0200136/* reset the HD-audio controller in power save mode.
137 * this may give more power-saving, but will take longer time to
138 * wake up.
139 */
Takashi Iwai8fc24422013-04-04 15:35:24 +0200140static bool power_save_controller = 1;
141module_param(power_save_controller, bool, 0644);
Takashi Iwaidee1b662007-08-13 16:10:30 +0200142MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
Dylan Reide62a42a2014-02-28 15:41:19 -0800143#else
144static int *power_save_addr;
Takashi Iwai83012a72012-08-24 18:38:08 +0200145#endif /* CONFIG_PM */
Takashi Iwaidee1b662007-08-13 16:10:30 +0200146
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100147static int align_buffer_size = -1;
148module_param(align_buffer_size, bint, 0644);
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -0500149MODULE_PARM_DESC(align_buffer_size,
150 "Force buffer and period sizes to be multiple of 128 bytes.");
151
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200152#ifdef CONFIG_X86
153static bool hda_snoop = true;
154module_param_named(snoop, hda_snoop, bool, 0444);
155MODULE_PARM_DESC(snoop, "Enable/disable snooping");
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200156#else
157#define hda_snoop true
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200158#endif
159
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161MODULE_LICENSE("GPL");
162MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
163 "{Intel, ICH6M},"
Jason Gaston2f1b3812005-05-01 08:58:50 -0700164 "{Intel, ICH7},"
Frederick Lif5d40b32005-05-12 14:55:20 +0200165 "{Intel, ESB2},"
Jason Gastond2981392006-01-10 11:07:37 +0100166 "{Intel, ICH8},"
Jason Gastonf9cc8a82006-11-22 11:53:52 +0100167 "{Intel, ICH9},"
Jason Gastonc34f5a02008-01-29 12:38:49 +0100168 "{Intel, ICH10},"
Seth Heasleyb29c2362008-08-08 15:56:39 -0700169 "{Intel, PCH},"
Seth Heasleyd2f2fcd2010-01-12 17:03:35 -0800170 "{Intel, CPT},"
Seth Heasleyd2edeb72011-04-20 10:59:57 -0700171 "{Intel, PPT},"
Seth Heasley8bc039a2012-01-23 16:24:31 -0800172 "{Intel, LPT},"
James Ralston144dad92012-08-09 09:38:59 -0700173 "{Intel, LPT_LP},"
James Ralston4eeca492013-11-04 09:27:45 -0800174 "{Intel, WPT_LP},"
Wang Xingchaoe926f2c2012-06-13 10:23:51 +0800175 "{Intel, HPT},"
Seth Heasleycea310e2010-09-10 16:29:56 -0700176 "{Intel, PBG},"
Tobin Davis4979bca2008-01-30 08:13:55 +0100177 "{Intel, SCH},"
Takashi Iwaifc20a562005-05-12 15:00:41 +0200178 "{ATI, SB450},"
Felix Kuehling89be83f2006-03-31 12:33:59 +0200179 "{ATI, SB600},"
Felix Kuehling778b6e12006-05-17 11:22:21 +0200180 "{ATI, RS600},"
Felix Kuehling5b15c952006-10-16 12:49:47 +0200181 "{ATI, RS690},"
Wolke Liue6db1112007-04-27 12:20:57 +0200182 "{ATI, RS780},"
183 "{ATI, R600},"
Herton Ronaldo Krzesinski2797f722007-11-05 18:21:56 +0100184 "{ATI, RV630},"
185 "{ATI, RV610},"
Wolke Liu27da1832007-11-16 11:06:30 +0100186 "{ATI, RV670},"
187 "{ATI, RV635},"
188 "{ATI, RV620},"
189 "{ATI, RV770},"
Takashi Iwaifc20a562005-05-12 15:00:41 +0200190 "{VIA, VT8251},"
Takashi Iwai47672312005-08-12 16:44:04 +0200191 "{VIA, VT8237A},"
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200192 "{SiS, SIS966},"
193 "{ULI, M5461}}");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194MODULE_DESCRIPTION("Intel HDA driver");
195
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200196#if defined(CONFIG_PM) && defined(CONFIG_VGA_SWITCHEROO)
Takashi Iwaif8f1bec2014-02-06 18:14:03 +0100197#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200198#define SUPPORT_VGA_SWITCHEROO
199#endif
200#endif
201
202
Takashi Iwaicb53c622007-08-10 17:21:45 +0200203/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Takashi Iwaieb49faa2013-03-15 09:19:11 +0100206/* DSP lock helpers */
207#ifdef CONFIG_SND_HDA_DSP_LOADER
208#define dsp_lock_init(dev) mutex_init(&(dev)->dsp_mutex)
209#define dsp_lock(dev) mutex_lock(&(dev)->dsp_mutex)
210#define dsp_unlock(dev) mutex_unlock(&(dev)->dsp_mutex)
211#define dsp_is_locked(dev) ((dev)->locked)
212#else
213#define dsp_lock_init(dev) do {} while (0)
214#define dsp_lock(dev) do {} while (0)
215#define dsp_unlock(dev) do {} while (0)
216#define dsp_is_locked(dev) 0
217#endif
218
Takashi Iwai1a8506d2012-10-16 15:10:08 +0200219#define CREATE_TRACE_POINTS
220#include "hda_intel_trace.h"
221
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200222/* driver types */
223enum {
224 AZX_DRIVER_ICH,
Seth Heasley32679f92010-02-22 17:31:09 -0800225 AZX_DRIVER_PCH,
Tobin Davis4979bca2008-01-30 08:13:55 +0100226 AZX_DRIVER_SCH,
Takashi Iwaifab12852013-11-05 17:54:05 +0100227 AZX_DRIVER_HDMI,
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200228 AZX_DRIVER_ATI,
Felix Kuehling778b6e12006-05-17 11:22:21 +0200229 AZX_DRIVER_ATIHDMI,
Andiry Xu1815b342011-12-14 16:10:27 +0800230 AZX_DRIVER_ATIHDMI_NS,
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200231 AZX_DRIVER_VIA,
232 AZX_DRIVER_SIS,
233 AZX_DRIVER_ULI,
Vinod Gda3fca22005-09-13 18:49:12 +0200234 AZX_DRIVER_NVIDIA,
Kailang Yangf2690022008-05-27 11:44:55 +0200235 AZX_DRIVER_TERA,
Takashi Iwai14d34f12010-10-21 09:03:25 +0200236 AZX_DRIVER_CTX,
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200237 AZX_DRIVER_CTHDA,
Yang, Libinc4da29c2008-11-13 11:07:07 +0100238 AZX_DRIVER_GENERIC,
Takashi Iwai2f5983f2008-09-03 16:00:44 +0200239 AZX_NUM_DRIVERS, /* keep this as last entry */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200240};
241
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +0100242/* quirks for Intel PCH */
Takashi Iwaid7dab4d2013-01-08 13:51:30 +0100243#define AZX_DCAPS_INTEL_PCH_NOPM \
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +0100244 (AZX_DCAPS_SCH_SNOOP | AZX_DCAPS_BUFSIZE | \
Takashi Iwaid7dab4d2013-01-08 13:51:30 +0100245 AZX_DCAPS_COUNT_LPIB_DELAY)
246
247#define AZX_DCAPS_INTEL_PCH \
248 (AZX_DCAPS_INTEL_PCH_NOPM | AZX_DCAPS_PM_RUNTIME)
Takashi Iwai9477c582011-05-25 09:11:37 +0200249
Takashi Iwai33499a12013-11-05 17:34:46 +0100250#define AZX_DCAPS_INTEL_HASWELL \
251 (AZX_DCAPS_SCH_SNOOP | AZX_DCAPS_ALIGN_BUFSIZE | \
252 AZX_DCAPS_COUNT_LPIB_DELAY | AZX_DCAPS_PM_RUNTIME | \
253 AZX_DCAPS_I915_POWERWELL)
254
Takashi Iwai9477c582011-05-25 09:11:37 +0200255/* quirks for ATI SB / AMD Hudson */
256#define AZX_DCAPS_PRESET_ATI_SB \
257 (AZX_DCAPS_ATI_SNOOP | AZX_DCAPS_NO_TCSEL | \
258 AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
259
260/* quirks for ATI/AMD HDMI */
261#define AZX_DCAPS_PRESET_ATI_HDMI \
262 (AZX_DCAPS_NO_TCSEL | AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
263
264/* quirks for Nvidia */
265#define AZX_DCAPS_PRESET_NVIDIA \
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100266 (AZX_DCAPS_NVIDIA_SNOOP | AZX_DCAPS_RIRB_DELAY | AZX_DCAPS_NO_MSI |\
Mike Travis49d9e772013-05-01 14:04:08 -0500267 AZX_DCAPS_ALIGN_BUFSIZE | AZX_DCAPS_NO_64BIT)
Takashi Iwai9477c582011-05-25 09:11:37 +0200268
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200269#define AZX_DCAPS_PRESET_CTHDA \
270 (AZX_DCAPS_NO_MSI | AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_4K_BDLE_BOUNDARY)
271
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200272/*
273 * VGA-switcher support
274 */
275#ifdef SUPPORT_VGA_SWITCHEROO
Takashi Iwai5cb543d2012-08-09 13:49:23 +0200276#define use_vga_switcheroo(chip) ((chip)->use_vga_switcheroo)
277#else
278#define use_vga_switcheroo(chip) 0
279#endif
280
Takashi Iwai48c8b0e2012-12-07 07:40:35 +0100281static char *driver_short_names[] = {
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200282 [AZX_DRIVER_ICH] = "HDA Intel",
Seth Heasley32679f92010-02-22 17:31:09 -0800283 [AZX_DRIVER_PCH] = "HDA Intel PCH",
Tobin Davis4979bca2008-01-30 08:13:55 +0100284 [AZX_DRIVER_SCH] = "HDA Intel MID",
Takashi Iwaifab12852013-11-05 17:54:05 +0100285 [AZX_DRIVER_HDMI] = "HDA Intel HDMI",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200286 [AZX_DRIVER_ATI] = "HDA ATI SB",
Felix Kuehling778b6e12006-05-17 11:22:21 +0200287 [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
Andiry Xu1815b342011-12-14 16:10:27 +0800288 [AZX_DRIVER_ATIHDMI_NS] = "HDA ATI HDMI",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200289 [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
290 [AZX_DRIVER_SIS] = "HDA SIS966",
Vinod Gda3fca22005-09-13 18:49:12 +0200291 [AZX_DRIVER_ULI] = "HDA ULI M5461",
292 [AZX_DRIVER_NVIDIA] = "HDA NVidia",
Kailang Yangf2690022008-05-27 11:44:55 +0200293 [AZX_DRIVER_TERA] = "HDA Teradici",
Takashi Iwai14d34f12010-10-21 09:03:25 +0200294 [AZX_DRIVER_CTX] = "HDA Creative",
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200295 [AZX_DRIVER_CTHDA] = "HDA Creative",
Yang, Libinc4da29c2008-11-13 11:07:07 +0100296 [AZX_DRIVER_GENERIC] = "HD-Audio Generic",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200297};
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/* for pcm support */
Dylan Reidb419b352014-02-28 15:41:20 -0800300static inline struct azx_dev *get_azx_dev(struct snd_pcm_substream *substream)
301{
302 return substream->runtime->private_data;
303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200305#ifdef CONFIG_X86
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100306static void __mark_pages_wc(struct azx *chip, struct snd_dma_buffer *dmab, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200307{
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100308 int pages;
309
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200310 if (azx_snoop(chip))
311 return;
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100312 if (!dmab || !dmab->area || !dmab->bytes)
313 return;
314
315#ifdef CONFIG_SND_DMA_SGBUF
316 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_SG) {
317 struct snd_sg_buf *sgbuf = dmab->private_data;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200318 if (on)
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100319 set_pages_array_wc(sgbuf->page_table, sgbuf->pages);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200320 else
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100321 set_pages_array_wb(sgbuf->page_table, sgbuf->pages);
322 return;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200323 }
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100324#endif
325
326 pages = (dmab->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
327 if (on)
328 set_memory_wc((unsigned long)dmab->area, pages);
329 else
330 set_memory_wb((unsigned long)dmab->area, pages);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200331}
332
333static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
334 bool on)
335{
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100336 __mark_pages_wc(chip, buf, on);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200337}
338static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100339 struct snd_pcm_substream *substream, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200340{
341 if (azx_dev->wc_marked != on) {
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100342 __mark_pages_wc(chip, snd_pcm_get_dma_buf(substream), on);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200343 azx_dev->wc_marked = on;
344 }
345}
346#else
347/* NOP for other archs */
348static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
349 bool on)
350{
351}
352static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100353 struct snd_pcm_substream *substream, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200354{
355}
356#endif
357
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200358static int azx_acquire_irq(struct azx *chip, int do_disconnect);
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200359static int azx_send_cmd(struct hda_bus *bus, unsigned int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*
361 * Interface for HD codec
362 */
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/*
365 * CORB / RIRB interface
366 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100367static int azx_alloc_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 int err;
370
371 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
Dylan Reidb419b352014-02-28 15:41:20 -0800372 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
373 PAGE_SIZE, &chip->rb);
374 if (err < 0)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100375 dev_err(chip->card->dev, "cannot allocate CORB/RIRB\n");
Dylan Reidb419b352014-02-28 15:41:20 -0800376 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100379static void azx_init_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
David Henningssonca460f82014-02-28 07:56:58 +0100381 int timeout;
382
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800383 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* CORB set up */
385 chip->corb.addr = chip->rb.addr;
386 chip->corb.buf = (u32 *)chip->rb.area;
387 azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200388 azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200390 /* set the corb size to 256 entries (ULI requires explicitly) */
391 azx_writeb(chip, CORBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* set the corb write pointer to 0 */
393 azx_writew(chip, CORBWP, 0);
David Henningssonca460f82014-02-28 07:56:58 +0100394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* reset the corb hw read pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200396 azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
David Henningssonca460f82014-02-28 07:56:58 +0100397 for (timeout = 1000; timeout > 0; timeout--) {
398 if ((azx_readw(chip, CORBRP) & ICH6_CORBRP_RST) == ICH6_CORBRP_RST)
399 break;
400 udelay(1);
401 }
402 if (timeout <= 0)
403 dev_err(chip->card->dev, "CORB reset timeout#1, CORBRP = %d\n",
404 azx_readw(chip, CORBRP));
405
406 azx_writew(chip, CORBRP, 0);
407 for (timeout = 1000; timeout > 0; timeout--) {
408 if (azx_readw(chip, CORBRP) == 0)
409 break;
410 udelay(1);
411 }
412 if (timeout <= 0)
413 dev_err(chip->card->dev, "CORB reset timeout#2, CORBRP = %d\n",
414 azx_readw(chip, CORBRP));
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* enable corb dma */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200417 azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /* RIRB set up */
420 chip->rirb.addr = chip->rb.addr + 2048;
421 chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800422 chip->rirb.wp = chip->rirb.rp = 0;
423 memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200425 azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200427 /* set the rirb size to 256 entries (ULI requires explicitly) */
428 azx_writeb(chip, RIRBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* reset the rirb hw write pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200430 azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* set N=1, get RIRB response interrupt for new entry */
Takashi Iwai9477c582011-05-25 09:11:37 +0200432 if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
Takashi Iwai14d34f12010-10-21 09:03:25 +0200433 azx_writew(chip, RINTCNT, 0xc0);
434 else
435 azx_writew(chip, RINTCNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* enable rirb dma and response irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800438 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100441static void azx_free_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800443 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* disable ringbuffer DMAs */
445 azx_writeb(chip, RIRBCTL, 0);
446 azx_writeb(chip, CORBCTL, 0);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800447 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Wu Fengguangdeadff12009-08-01 18:45:16 +0800450static unsigned int azx_command_addr(u32 cmd)
451{
452 unsigned int addr = cmd >> 28;
453
454 if (addr >= AZX_MAX_CODECS) {
455 snd_BUG();
456 addr = 0;
457 }
458
459 return addr;
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100463static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100465 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800466 unsigned int addr = azx_command_addr(val);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100467 unsigned int wp, rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Wu Fengguangc32649f2009-08-01 18:48:12 +0800469 spin_lock_irq(&chip->reg_lock);
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* add command to corb */
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100472 wp = azx_readw(chip, CORBWP);
473 if (wp == 0xffff) {
474 /* something wrong, controller likely turned to D3 */
475 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100476 return -EIO;
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 wp++;
479 wp %= ICH6_MAX_CORB_ENTRIES;
480
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100481 rp = azx_readw(chip, CORBRP);
482 if (wp == rp) {
483 /* oops, it's full */
484 spin_unlock_irq(&chip->reg_lock);
485 return -EAGAIN;
486 }
487
Wu Fengguangdeadff12009-08-01 18:45:16 +0800488 chip->rirb.cmds[addr]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 chip->corb.buf[wp] = cpu_to_le32(val);
David Henningssonca460f82014-02-28 07:56:58 +0100490 azx_writew(chip, CORBWP, wp);
Wu Fengguangc32649f2009-08-01 18:48:12 +0800491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 spin_unlock_irq(&chip->reg_lock);
493
494 return 0;
495}
496
497#define ICH6_RIRB_EX_UNSOL_EV (1<<4)
498
499/* retrieve RIRB entry - called from interrupt handler */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100500static void azx_update_rirb(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 unsigned int rp, wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800503 unsigned int addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 u32 res, res_ex;
505
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100506 wp = azx_readw(chip, RIRBWP);
507 if (wp == 0xffff) {
508 /* something wrong, controller likely turned to D3 */
509 return;
510 }
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (wp == chip->rirb.wp)
513 return;
514 chip->rirb.wp = wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 while (chip->rirb.rp != wp) {
517 chip->rirb.rp++;
518 chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
519
520 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
521 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
522 res = le32_to_cpu(chip->rirb.buf[rp]);
David Henningsson3d692452014-01-29 13:12:31 +0100523 addr = res_ex & 0xf;
524 if ((addr >= AZX_MAX_CODECS) || !(chip->codec_mask & (1 << addr))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100525 dev_err(chip->card->dev, "spurious response %#x:%#x, rp = %d, wp = %d",
526 res, res_ex,
527 chip->rirb.rp, wp);
David Henningsson3d692452014-01-29 13:12:31 +0100528 snd_BUG();
529 }
530 else if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 snd_hda_queue_unsol_event(chip->bus, res, res_ex);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800532 else if (chip->rirb.cmds[addr]) {
533 chip->rirb.res[addr] = res;
Takashi Iwai2add9b92008-03-18 09:47:06 +0100534 smp_wmb();
Wu Fengguangdeadff12009-08-01 18:45:16 +0800535 chip->rirb.cmds[addr]--;
Joe Perches3b70a672013-11-07 11:55:15 -0800536 } else if (printk_ratelimit()) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100537 dev_err(chip->card->dev, "spurious response %#x:%#x, last cmd=%#08x\n",
538 res, res_ex,
539 chip->last_cmd[addr]);
Joe Perches3b70a672013-11-07 11:55:15 -0800540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542}
543
544/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800545static unsigned int azx_rirb_get_response(struct hda_bus *bus,
546 unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100548 struct azx *chip = bus->private_data;
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200549 unsigned long timeout;
David Henningsson32cf4022012-05-04 11:05:55 +0200550 unsigned long loopcounter;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200551 int do_poll = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200553 again:
554 timeout = jiffies + msecs_to_jiffies(1000);
David Henningsson32cf4022012-05-04 11:05:55 +0200555
556 for (loopcounter = 0;; loopcounter++) {
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200557 if (chip->polling_mode || do_poll) {
Takashi Iwaie96224a2006-08-21 17:57:44 +0200558 spin_lock_irq(&chip->reg_lock);
559 azx_update_rirb(chip);
560 spin_unlock_irq(&chip->reg_lock);
561 }
Wu Fengguangdeadff12009-08-01 18:45:16 +0800562 if (!chip->rirb.cmds[addr]) {
Takashi Iwai2add9b92008-03-18 09:47:06 +0100563 smp_rmb();
Takashi Iwaib6132912009-03-24 07:36:09 +0100564 bus->rirb_error = 0;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200565
566 if (!do_poll)
567 chip->poll_count = 0;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800568 return chip->rirb.res[addr]; /* the last value */
Takashi Iwai2add9b92008-03-18 09:47:06 +0100569 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100570 if (time_after(jiffies, timeout))
571 break;
David Henningsson32cf4022012-05-04 11:05:55 +0200572 if (bus->needs_damn_long_delay || loopcounter > 3000)
Takashi Iwai52987652008-01-16 16:09:47 +0100573 msleep(2); /* temporary workaround */
574 else {
575 udelay(10);
576 cond_resched();
577 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100578 }
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200579
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200580 if (!bus->no_response_fallback)
581 return -1;
582
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200583 if (!chip->polling_mode && chip->poll_count < 2) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100584 dev_dbg(chip->card->dev,
585 "azx_get_response timeout, polling the codec once: last cmd=0x%08x\n",
586 chip->last_cmd[addr]);
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200587 do_poll = 1;
588 chip->poll_count++;
589 goto again;
590 }
591
592
Takashi Iwai23c4a882009-10-30 13:21:49 +0100593 if (!chip->polling_mode) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100594 dev_warn(chip->card->dev,
595 "azx_get_response timeout, switching to polling mode: last cmd=0x%08x\n",
596 chip->last_cmd[addr]);
Takashi Iwai23c4a882009-10-30 13:21:49 +0100597 chip->polling_mode = 1;
598 goto again;
599 }
600
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200601 if (chip->msi) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100602 dev_warn(chip->card->dev,
603 "No response from codec, disabling MSI: last cmd=0x%08x\n",
604 chip->last_cmd[addr]);
Dylan Reidf46ea602014-02-28 15:41:16 -0800605 if (chip->ops->disable_msi_reset_irq &&
606 chip->ops->disable_msi_reset_irq(chip) < 0) {
Takashi Iwaib6132912009-03-24 07:36:09 +0100607 bus->rirb_error = 1;
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200608 return -1;
Takashi Iwaib6132912009-03-24 07:36:09 +0100609 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200610 goto again;
611 }
612
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +0100613 if (chip->probing) {
614 /* If this critical timeout happens during the codec probing
615 * phase, this is likely an access to a non-existing codec
616 * slot. Better to return an error and reset the system.
617 */
618 return -1;
619 }
620
Takashi Iwai8dd78332009-06-02 01:16:07 +0200621 /* a fatal communication error; need either to reset or to fallback
622 * to the single_cmd mode
623 */
Takashi Iwaib6132912009-03-24 07:36:09 +0100624 bus->rirb_error = 1;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200625 if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200626 bus->response_reset = 1;
627 return -1; /* give a chance to retry */
628 }
629
Takashi Iwai4e76a882014-02-25 12:21:03 +0100630 dev_err(chip->card->dev,
631 "azx_get_response timeout, switching to single_cmd mode: last cmd=0x%08x\n",
632 chip->last_cmd[addr]);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200633 chip->single_cmd = 1;
634 bus->response_reset = 0;
Takashi Iwai1a696972009-11-07 09:49:04 +0100635 /* release CORB/RIRB */
Takashi Iwai4fcd3922009-05-25 18:34:52 +0200636 azx_free_cmd_io(chip);
Takashi Iwai1a696972009-11-07 09:49:04 +0100637 /* disable unsolicited responses */
638 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200639 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
643 * Use the single immediate command instead of CORB/RIRB for simplicity
644 *
645 * Note: according to Intel, this is not preferred use. The command was
646 * intended for the BIOS only, and may get confused with unsolicited
647 * responses. So, we shouldn't use it for normal operation from the
648 * driver.
649 * I left the codes, however, for debugging/testing purposes.
650 */
651
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200652/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800653static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200654{
655 int timeout = 50;
656
657 while (timeout--) {
658 /* check IRV busy bit */
659 if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
660 /* reuse rirb.res as the response return value */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800661 chip->rirb.res[addr] = azx_readl(chip, IR);
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200662 return 0;
663 }
664 udelay(1);
665 }
666 if (printk_ratelimit())
Takashi Iwai4e76a882014-02-25 12:21:03 +0100667 dev_dbg(chip->card->dev, "get_response timeout: IRS=0x%x\n",
668 azx_readw(chip, IRS));
Wu Fengguangdeadff12009-08-01 18:45:16 +0800669 chip->rirb.res[addr] = -1;
Takashi Iwaib05a7d42009-05-28 11:59:12 +0200670 return -EIO;
671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100674static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100676 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800677 unsigned int addr = azx_command_addr(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int timeout = 50;
679
Takashi Iwai8dd78332009-06-02 01:16:07 +0200680 bus->rirb_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 while (timeout--) {
682 /* check ICB busy bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200683 if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* Clear IRV valid bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200685 azx_writew(chip, IRS, azx_readw(chip, IRS) |
686 ICH6_IRS_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 azx_writel(chip, IC, val);
Takashi Iwaid01ce992007-07-27 16:52:19 +0200688 azx_writew(chip, IRS, azx_readw(chip, IRS) |
689 ICH6_IRS_BUSY);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800690 return azx_single_wait_for_response(chip, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 udelay(1);
693 }
Marc Boucher1cfd52b2008-01-22 15:29:26 +0100694 if (printk_ratelimit())
Takashi Iwai4e76a882014-02-25 12:21:03 +0100695 dev_dbg(chip->card->dev,
696 "send_cmd timeout: IRS=0x%x, val=0x%x\n",
697 azx_readw(chip, IRS), val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -EIO;
699}
700
701/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800702static unsigned int azx_single_get_response(struct hda_bus *bus,
703 unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100705 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800706 return chip->rirb.res[addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Takashi Iwai111d3af2006-02-16 18:17:58 +0100709/*
710 * The below are the main callbacks from hda_codec.
711 *
712 * They are just the skeleton to call sub-callbacks according to the
713 * current setting of chip->single_cmd.
714 */
715
716/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100717static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
Takashi Iwai111d3af2006-02-16 18:17:58 +0100718{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100719 struct azx *chip = bus->private_data;
Takashi Iwai43bbb6c2007-07-06 20:22:05 +0200720
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200721 if (chip->disabled)
722 return 0;
Wu Fengguangfeb27342009-08-01 19:17:14 +0800723 chip->last_cmd[azx_command_addr(val)] = val;
Takashi Iwai111d3af2006-02-16 18:17:58 +0100724 if (chip->single_cmd)
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100725 return azx_single_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100726 else
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100727 return azx_corb_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100728}
729
730/* get a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800731static unsigned int azx_get_response(struct hda_bus *bus,
732 unsigned int addr)
Takashi Iwai111d3af2006-02-16 18:17:58 +0100733{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100734 struct azx *chip = bus->private_data;
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200735 if (chip->disabled)
736 return 0;
Takashi Iwai111d3af2006-02-16 18:17:58 +0100737 if (chip->single_cmd)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800738 return azx_single_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100739 else
Wu Fengguangdeadff12009-08-01 18:45:16 +0800740 return azx_rirb_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100741}
742
Takashi Iwai83012a72012-08-24 18:38:08 +0200743#ifdef CONFIG_PM
Takashi Iwai68467f52012-08-28 09:14:29 -0700744static void azx_power_notify(struct hda_bus *bus, bool power_up);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200745#endif
Takashi Iwai111d3af2006-02-16 18:17:58 +0100746
Takashi Iwai1d1a4562012-09-20 20:29:13 -0700747#ifdef CONFIG_SND_HDA_DSP_LOADER
748static int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
749 unsigned int byte_size,
750 struct snd_dma_buffer *bufp);
751static void azx_load_dsp_trigger(struct hda_bus *bus, bool start);
752static void azx_load_dsp_cleanup(struct hda_bus *bus,
753 struct snd_dma_buffer *dmab);
754#endif
755
Mengdong Lin3af3f352013-06-24 10:18:54 -0400756/* enter link reset */
Mengdong Lin7295b262013-06-25 05:58:49 -0400757static void azx_enter_link_reset(struct azx *chip)
Mengdong Lin3af3f352013-06-24 10:18:54 -0400758{
759 unsigned long timeout;
760
761 /* reset controller */
762 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
763
764 timeout = jiffies + msecs_to_jiffies(100);
765 while ((azx_readb(chip, GCTL) & ICH6_GCTL_RESET) &&
766 time_before(jiffies, timeout))
767 usleep_range(500, 1000);
768}
769
Mengdong Lin7295b262013-06-25 05:58:49 -0400770/* exit link reset */
771static void azx_exit_link_reset(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Mengdong Linfa348da2012-12-12 09:16:15 -0500773 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Mengdong Lin7295b262013-06-25 05:58:49 -0400775 azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
776
777 timeout = jiffies + msecs_to_jiffies(100);
778 while (!azx_readb(chip, GCTL) &&
779 time_before(jiffies, timeout))
780 usleep_range(500, 1000);
781}
782
783/* reset codec link */
784static int azx_reset(struct azx *chip, int full_reset)
785{
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100786 if (!full_reset)
787 goto __skip;
788
Danny Tholene8a7f132007-09-11 21:41:56 +0200789 /* clear STATESTS */
Wang Xingchaoda7db6a2013-07-22 03:19:18 -0400790 azx_writew(chip, STATESTS, STATESTS_INT_MASK);
Danny Tholene8a7f132007-09-11 21:41:56 +0200791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /* reset controller */
Mengdong Lin7295b262013-06-25 05:58:49 -0400793 azx_enter_link_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /* delay for >= 100us for codec PLL to settle per spec
796 * Rev 0.9 section 5.5.1
797 */
Mengdong Linfa348da2012-12-12 09:16:15 -0500798 usleep_range(500, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /* Bring controller out of reset */
Mengdong Lin7295b262013-06-25 05:58:49 -0400801 azx_exit_link_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Pavel Machek927fc862006-08-31 17:03:43 +0200803 /* Brent Chartrand said to wait >= 540us for codecs to initialize */
Mengdong Linfa348da2012-12-12 09:16:15 -0500804 usleep_range(1000, 1200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100806 __skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* check to see if controller is ready */
Pavel Machek927fc862006-08-31 17:03:43 +0200808 if (!azx_readb(chip, GCTL)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100809 dev_dbg(chip->card->dev, "azx_reset: controller not ready!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EBUSY;
811 }
812
Matt41e2fce2005-07-04 17:49:55 +0200813 /* Accept unsolicited responses */
Takashi Iwai1a696972009-11-07 09:49:04 +0100814 if (!chip->single_cmd)
815 azx_writel(chip, GCTL, azx_readl(chip, GCTL) |
816 ICH6_GCTL_UNSOL);
Matt41e2fce2005-07-04 17:49:55 +0200817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* detect codecs */
Pavel Machek927fc862006-08-31 17:03:43 +0200819 if (!chip->codec_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 chip->codec_mask = azx_readw(chip, STATESTS);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100821 dev_dbg(chip->card->dev, "codec_mask = 0x%x\n",
822 chip->codec_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
825 return 0;
826}
827
828
829/*
830 * Lowlevel interface
831 */
832
833/* enable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100834static void azx_int_enable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 /* enable controller CIE and GIE */
837 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
838 ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
839}
840
841/* disable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100842static void azx_int_disable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 int i;
845
846 /* disable interrupts in stream descriptor */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200847 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100848 struct azx_dev *azx_dev = &chip->azx_dev[i];
Dylan Reid40830812014-02-28 15:41:13 -0800849 azx_sd_writeb(chip, azx_dev, SD_CTL,
850 azx_sd_readb(chip, azx_dev, SD_CTL) &
851 ~SD_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
854 /* disable SIE for all streams */
855 azx_writeb(chip, INTCTL, 0);
856
857 /* disable controller CIE and GIE */
858 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
859 ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
860}
861
862/* clear interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100863static void azx_int_clear(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 int i;
866
867 /* clear stream status */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200868 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100869 struct azx_dev *azx_dev = &chip->azx_dev[i];
Dylan Reid40830812014-02-28 15:41:13 -0800870 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
873 /* clear STATESTS */
Wang Xingchaoda7db6a2013-07-22 03:19:18 -0400874 azx_writew(chip, STATESTS, STATESTS_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* clear rirb status */
877 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
878
879 /* clear int status */
880 azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
881}
882
883/* start a stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100884static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Joseph Chan0e153472008-08-26 14:38:03 +0200886 /*
887 * Before stream start, initialize parameter
888 */
889 azx_dev->insufficient = 1;
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* enable SIE */
Wei Niccc5df02010-01-26 15:59:33 +0800892 azx_writel(chip, INTCTL,
893 azx_readl(chip, INTCTL) | (1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* set DMA start and interrupt mask */
Dylan Reid40830812014-02-28 15:41:13 -0800895 azx_sd_writeb(chip, azx_dev, SD_CTL,
896 azx_sd_readb(chip, azx_dev, SD_CTL) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 SD_CTL_DMA_START | SD_INT_MASK);
898}
899
Takashi Iwai1dddab42009-03-18 15:15:37 +0100900/* stop DMA */
901static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Dylan Reid40830812014-02-28 15:41:13 -0800903 azx_sd_writeb(chip, azx_dev, SD_CTL,
904 azx_sd_readb(chip, azx_dev, SD_CTL) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 ~(SD_CTL_DMA_START | SD_INT_MASK));
Dylan Reid40830812014-02-28 15:41:13 -0800906 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
Takashi Iwai1dddab42009-03-18 15:15:37 +0100907}
908
909/* stop a stream */
910static void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
911{
912 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* disable SIE */
Wei Niccc5df02010-01-26 15:59:33 +0800914 azx_writel(chip, INTCTL,
915 azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918
919/*
Takashi Iwaicb53c622007-08-10 17:21:45 +0200920 * reset and start the controller registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100922static void azx_init_chip(struct azx *chip, int full_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Takashi Iwaicb53c622007-08-10 17:21:45 +0200924 if (chip->initialized)
925 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 /* reset controller */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +0100928 azx_reset(chip, full_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 /* initialize interrupts */
931 azx_int_clear(chip);
932 azx_int_enable(chip);
933
934 /* initialize the codec command I/O */
Takashi Iwai1a696972009-11-07 09:49:04 +0100935 if (!chip->single_cmd)
936 azx_init_cmd_io(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Takashi Iwai0be3b5d2005-09-05 17:11:40 +0200938 /* program the position buffer */
939 azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200940 azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr));
Frederick Lif5d40b32005-05-12 14:55:20 +0200941
Takashi Iwaicb53c622007-08-10 17:21:45 +0200942 chip->initialized = 1;
943}
944
945/*
946 * initialize the PCI registers
947 */
948/* update bits in a PCI register byte */
949static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
950 unsigned char mask, unsigned char val)
951{
952 unsigned char data;
953
954 pci_read_config_byte(pci, reg, &data);
955 data &= ~mask;
956 data |= (val & mask);
957 pci_write_config_byte(pci, reg, data);
958}
959
960static void azx_init_pci(struct azx *chip)
961{
962 /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
963 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
964 * Ensuring these bits are 0 clears playback static on some HD Audio
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +0100965 * codecs.
966 * The PCI register TCSEL is defined in the Intel manuals.
Takashi Iwaicb53c622007-08-10 17:21:45 +0200967 */
Linus Torvalds46f2cc82011-05-27 19:45:28 -0700968 if (!(chip->driver_caps & AZX_DCAPS_NO_TCSEL)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100969 dev_dbg(chip->card->dev, "Clearing TCSEL\n");
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +0100970 update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
Takashi Iwai9477c582011-05-25 09:11:37 +0200971 }
Takashi Iwaicb53c622007-08-10 17:21:45 +0200972
Takashi Iwai9477c582011-05-25 09:11:37 +0200973 /* For ATI SB450/600/700/800/900 and AMD Hudson azalia HD audio,
974 * we need to enable snoop.
975 */
976 if (chip->driver_caps & AZX_DCAPS_ATI_SNOOP) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100977 dev_dbg(chip->card->dev, "Setting ATI snoop: %d\n",
978 azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +0200979 update_pci_byte(chip->pci,
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200980 ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR, 0x07,
981 azx_snoop(chip) ? ATI_SB450_HDAUDIO_ENABLE_SNOOP : 0);
Takashi Iwai9477c582011-05-25 09:11:37 +0200982 }
983
984 /* For NVIDIA HDA, enable snoop */
985 if (chip->driver_caps & AZX_DCAPS_NVIDIA_SNOOP) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100986 dev_dbg(chip->card->dev, "Setting Nvidia snoop: %d\n",
987 azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +0200988 update_pci_byte(chip->pci,
989 NVIDIA_HDA_TRANSREG_ADDR,
990 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
Peer Chen320dcc32008-08-20 16:43:24 -0700991 update_pci_byte(chip->pci,
992 NVIDIA_HDA_ISTRM_COH,
993 0x01, NVIDIA_HDA_ENABLE_COHBIT);
994 update_pci_byte(chip->pci,
995 NVIDIA_HDA_OSTRM_COH,
996 0x01, NVIDIA_HDA_ENABLE_COHBIT);
Takashi Iwai9477c582011-05-25 09:11:37 +0200997 }
998
999 /* Enable SCH/PCH snoop if needed */
1000 if (chip->driver_caps & AZX_DCAPS_SCH_SNOOP) {
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001001 unsigned short snoop;
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001002 pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001003 if ((!azx_snoop(chip) && !(snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)) ||
1004 (azx_snoop(chip) && (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP))) {
1005 snoop &= ~INTEL_SCH_HDA_DEVC_NOSNOOP;
1006 if (!azx_snoop(chip))
1007 snoop |= INTEL_SCH_HDA_DEVC_NOSNOOP;
1008 pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC, snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001009 pci_read_config_word(chip->pci,
1010 INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001011 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001012 dev_dbg(chip->card->dev, "SCH snoop: %s\n",
1013 (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) ?
1014 "Disabled" : "Enabled");
Vinod Gda3fca22005-09-13 18:49:12 +02001015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001019static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/*
1022 * interrupt handler
1023 */
David Howells7d12e782006-10-05 14:55:46 +01001024static irqreturn_t azx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001026 struct azx *chip = dev_id;
1027 struct azx_dev *azx_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 u32 status;
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001029 u8 sd_status;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001030 int i, ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001032#ifdef CONFIG_PM_RUNTIME
Dave Airlie246efa42013-07-29 15:19:29 +10001033 if (chip->driver_caps & AZX_DCAPS_PM_RUNTIME)
Dylan Reid89287562014-02-28 15:41:15 -08001034 if (chip->card->dev->power.runtime_status != RPM_ACTIVE)
Dave Airlie246efa42013-07-29 15:19:29 +10001035 return IRQ_NONE;
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001036#endif
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 spin_lock(&chip->reg_lock);
1039
Dan Carpenter60911062012-05-18 10:36:11 +03001040 if (chip->disabled) {
1041 spin_unlock(&chip->reg_lock);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001042 return IRQ_NONE;
Dan Carpenter60911062012-05-18 10:36:11 +03001043 }
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 status = azx_readl(chip, INTSTS);
Dave Airlie246efa42013-07-29 15:19:29 +10001046 if (status == 0 || status == 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 spin_unlock(&chip->reg_lock);
1048 return IRQ_NONE;
1049 }
1050
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001051 for (i = 0; i < chip->num_streams; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 azx_dev = &chip->azx_dev[i];
1053 if (status & azx_dev->sd_int_sta_mask) {
Dylan Reid40830812014-02-28 15:41:13 -08001054 sd_status = azx_sd_readb(chip, azx_dev, SD_STS);
1055 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK);
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001056 if (!azx_dev->substream || !azx_dev->running ||
1057 !(sd_status & SD_INT_COMPLETE))
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001058 continue;
1059 /* check whether this IRQ is really acceptable */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001060 ok = azx_position_ok(chip, azx_dev);
1061 if (ok == 1) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001062 azx_dev->irq_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 spin_unlock(&chip->reg_lock);
1064 snd_pcm_period_elapsed(azx_dev->substream);
1065 spin_lock(&chip->reg_lock);
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001066 } else if (ok == 0 && chip->bus && chip->bus->workq) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001067 /* bogus IRQ, process it later */
1068 azx_dev->irq_pending = 1;
Takashi Iwai6acaed32009-01-12 10:09:24 +01001069 queue_work(chip->bus->workq,
1070 &chip->irq_pending_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 }
1073 }
1074
1075 /* clear rirb int */
1076 status = azx_readb(chip, RIRBSTS);
1077 if (status & RIRB_INT_MASK) {
Takashi Iwai14d34f12010-10-21 09:03:25 +02001078 if (status & RIRB_INT_RESPONSE) {
Takashi Iwai9477c582011-05-25 09:11:37 +02001079 if (chip->driver_caps & AZX_DCAPS_RIRB_PRE_DELAY)
Takashi Iwai14d34f12010-10-21 09:03:25 +02001080 udelay(80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 azx_update_rirb(chip);
Takashi Iwai14d34f12010-10-21 09:03:25 +02001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1084 }
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 spin_unlock(&chip->reg_lock);
1087
1088 return IRQ_HANDLED;
1089}
1090
1091
1092/*
Takashi Iwai675f25d2008-06-10 17:53:20 +02001093 * set up a BDL entry
1094 */
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001095static int setup_bdle(struct azx *chip,
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001096 struct snd_dma_buffer *dmab,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001097 struct azx_dev *azx_dev, u32 **bdlp,
1098 int ofs, int size, int with_ioc)
1099{
Takashi Iwai675f25d2008-06-10 17:53:20 +02001100 u32 *bdl = *bdlp;
1101
1102 while (size > 0) {
1103 dma_addr_t addr;
1104 int chunk;
1105
1106 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
1107 return -EINVAL;
1108
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001109 addr = snd_sgbuf_get_addr(dmab, ofs);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001110 /* program the address field of the BDL entry */
1111 bdl[0] = cpu_to_le32((u32)addr);
Takashi Iwai766979e2008-06-13 20:53:56 +02001112 bdl[1] = cpu_to_le32(upper_32_bits(addr));
Takashi Iwai675f25d2008-06-10 17:53:20 +02001113 /* program the size field of the BDL entry */
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001114 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001115 /* one BDLE cannot cross 4K boundary on CTHDA chips */
1116 if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY) {
1117 u32 remain = 0x1000 - (ofs & 0xfff);
1118 if (chunk > remain)
1119 chunk = remain;
1120 }
Takashi Iwai675f25d2008-06-10 17:53:20 +02001121 bdl[2] = cpu_to_le32(chunk);
1122 /* program the IOC to enable interrupt
1123 * only when the whole fragment is processed
1124 */
1125 size -= chunk;
1126 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
1127 bdl += 4;
1128 azx_dev->frags++;
1129 ofs += chunk;
1130 }
1131 *bdlp = bdl;
1132 return ofs;
1133}
1134
1135/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * set up BDL entries
1137 */
Takashi Iwai555e2192008-06-10 17:53:34 +02001138static int azx_setup_periods(struct azx *chip,
1139 struct snd_pcm_substream *substream,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001140 struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001142 u32 *bdl;
1143 int i, ofs, periods, period_bytes;
Dylan Reid9cdc0112014-02-28 15:41:14 -08001144 int pos_adj = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /* reset BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08001147 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
1148 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Takashi Iwai97b71c92009-03-18 15:09:13 +01001150 period_bytes = azx_dev->period_bytes;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001151 periods = azx_dev->bufsize / period_bytes;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* program the initial BDL entries */
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001154 bdl = (u32 *)azx_dev->bdl.area;
1155 ofs = 0;
1156 azx_dev->frags = 0;
Dylan Reid9cdc0112014-02-28 15:41:14 -08001157
1158 if (chip->bdl_pos_adj)
1159 pos_adj = chip->bdl_pos_adj[chip->dev_index];
Takashi Iwai915bf292012-09-11 15:19:10 +02001160 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
Takashi Iwai675f25d2008-06-10 17:53:20 +02001161 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001162 int pos_align = pos_adj;
Takashi Iwai555e2192008-06-10 17:53:34 +02001163 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001164 if (!pos_adj)
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001165 pos_adj = pos_align;
1166 else
1167 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
1168 pos_align;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001169 pos_adj = frames_to_bytes(runtime, pos_adj);
1170 if (pos_adj >= period_bytes) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001171 dev_warn(chip->card->dev,"Too big adjustment %d\n",
Dylan Reid9cdc0112014-02-28 15:41:14 -08001172 pos_adj);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001173 pos_adj = 0;
1174 } else {
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001175 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1176 azx_dev,
Takashi Iwai915bf292012-09-11 15:19:10 +02001177 &bdl, ofs, pos_adj, true);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001178 if (ofs < 0)
1179 goto error;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001180 }
Takashi Iwai555e2192008-06-10 17:53:34 +02001181 } else
1182 pos_adj = 0;
Dylan Reid9cdc0112014-02-28 15:41:14 -08001183
Takashi Iwai675f25d2008-06-10 17:53:20 +02001184 for (i = 0; i < periods; i++) {
1185 if (i == periods - 1 && pos_adj)
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001186 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1187 azx_dev, &bdl, ofs,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001188 period_bytes - pos_adj, 0);
1189 else
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001190 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1191 azx_dev, &bdl, ofs,
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001192 period_bytes,
Takashi Iwai915bf292012-09-11 15:19:10 +02001193 !azx_dev->no_period_wakeup);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001194 if (ofs < 0)
1195 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001197 return 0;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001198
1199 error:
Takashi Iwai4e76a882014-02-25 12:21:03 +01001200 dev_err(chip->card->dev, "Too many BDL entries: buffer=%d, period=%d\n",
1201 azx_dev->bufsize, period_bytes);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001202 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Takashi Iwai1dddab42009-03-18 15:15:37 +01001205/* reset stream */
1206static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 unsigned char val;
1209 int timeout;
1210
Takashi Iwai1dddab42009-03-18 15:15:37 +01001211 azx_stream_clear(chip, azx_dev);
1212
Dylan Reid40830812014-02-28 15:41:13 -08001213 azx_sd_writeb(chip, azx_dev, SD_CTL,
1214 azx_sd_readb(chip, azx_dev, SD_CTL) |
Takashi Iwaid01ce992007-07-27 16:52:19 +02001215 SD_CTL_STREAM_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 udelay(3);
1217 timeout = 300;
Dylan Reid40830812014-02-28 15:41:13 -08001218 while (!((val = azx_sd_readb(chip, azx_dev, SD_CTL)) &
1219 SD_CTL_STREAM_RESET) && --timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 ;
1221 val &= ~SD_CTL_STREAM_RESET;
Dylan Reid40830812014-02-28 15:41:13 -08001222 azx_sd_writeb(chip, azx_dev, SD_CTL, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 udelay(3);
1224
1225 timeout = 300;
1226 /* waiting for hardware to report that the stream is out of reset */
Dylan Reid40830812014-02-28 15:41:13 -08001227 while (((val = azx_sd_readb(chip, azx_dev, SD_CTL)) &
1228 SD_CTL_STREAM_RESET) && --timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 ;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001230
1231 /* reset first position - may not be synced with hw at this time */
1232 *azx_dev->posbuf = 0;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Takashi Iwai1dddab42009-03-18 15:15:37 +01001235/*
1236 * set up the SD for streaming
1237 */
1238static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
1239{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001240 unsigned int val;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001241 /* make sure the run bit is zero for SD */
1242 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* program the stream_tag */
Dylan Reid40830812014-02-28 15:41:13 -08001244 val = azx_sd_readl(chip, azx_dev, SD_CTL);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001245 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
1246 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
1247 if (!azx_snoop(chip))
1248 val |= SD_CTL_TRAFFIC_PRIO;
Dylan Reid40830812014-02-28 15:41:13 -08001249 azx_sd_writel(chip, azx_dev, SD_CTL, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* program the length of samples in cyclic buffer */
Dylan Reid40830812014-02-28 15:41:13 -08001252 azx_sd_writel(chip, azx_dev, SD_CBL, azx_dev->bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* program the stream format */
1255 /* this value needs to be the same as the one programmed */
Dylan Reid40830812014-02-28 15:41:13 -08001256 azx_sd_writew(chip, azx_dev, SD_FORMAT, azx_dev->format_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /* program the stream LVI (last valid index) of the BDL */
Dylan Reid40830812014-02-28 15:41:13 -08001259 azx_sd_writew(chip, azx_dev, SD_LVI, azx_dev->frags - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 /* program the BDL address */
1262 /* lower BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08001263 azx_sd_writel(chip, azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* upper BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08001265 azx_sd_writel(chip, azx_dev, SD_BDLPU,
1266 upper_32_bits(azx_dev->bdl.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02001268 /* enable the position buffer */
David Henningsson4cb36312010-09-30 10:12:50 +02001269 if (chip->position_fix[0] != POS_FIX_LPIB ||
1270 chip->position_fix[1] != POS_FIX_LPIB) {
Takashi Iwaiee9d6b92008-03-14 15:52:20 +01001271 if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
1272 azx_writel(chip, DPLBASE,
1273 (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
1274 }
Takashi Iwaic74db862005-05-12 14:26:27 +02001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /* set the interrupt enable bits in the descriptor control register */
Dylan Reid40830812014-02-28 15:41:13 -08001277 azx_sd_writel(chip, azx_dev, SD_CTL,
1278 azx_sd_readl(chip, azx_dev, SD_CTL) | SD_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 return 0;
1281}
1282
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001283/*
1284 * Probe the given codec address
1285 */
1286static int probe_codec(struct azx *chip, int addr)
1287{
1288 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
1289 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
1290 unsigned int res;
1291
Wu Fengguanga678cde2009-08-01 18:46:46 +08001292 mutex_lock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001293 chip->probing = 1;
1294 azx_send_cmd(chip->bus, cmd);
Wu Fengguangdeadff12009-08-01 18:45:16 +08001295 res = azx_get_response(chip->bus, addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001296 chip->probing = 0;
Wu Fengguanga678cde2009-08-01 18:46:46 +08001297 mutex_unlock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001298 if (res == -1)
1299 return -EIO;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001300 dev_dbg(chip->card->dev, "codec #%d probed OK\n", addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001301 return 0;
1302}
1303
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001304static int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
1305 struct hda_pcm *cpcm);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001306static void azx_stop_chip(struct azx *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Takashi Iwai8dd78332009-06-02 01:16:07 +02001308static void azx_bus_reset(struct hda_bus *bus)
1309{
1310 struct azx *chip = bus->private_data;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001311
1312 bus->in_reset = 1;
1313 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001314 azx_init_chip(chip, 1);
Alexander Beregalov65f75982009-06-04 13:46:16 +04001315#ifdef CONFIG_PM
Takashi Iwai8dd78332009-06-02 01:16:07 +02001316 if (chip->initialized) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01001317 struct azx_pcm *p;
1318 list_for_each_entry(p, &chip->pcm_list, list)
1319 snd_pcm_suspend_all(p->pcm);
Takashi Iwai8dd78332009-06-02 01:16:07 +02001320 snd_hda_suspend(chip->bus);
1321 snd_hda_resume(chip->bus);
1322 }
Alexander Beregalov65f75982009-06-04 13:46:16 +04001323#endif
Takashi Iwai8dd78332009-06-02 01:16:07 +02001324 bus->in_reset = 0;
1325}
1326
David Henningsson26a6cb62012-10-09 15:04:21 +02001327static int get_jackpoll_interval(struct azx *chip)
1328{
Dylan Reid749ee282014-02-28 15:41:18 -08001329 int i;
David Henningsson26a6cb62012-10-09 15:04:21 +02001330 unsigned int j;
Dylan Reid749ee282014-02-28 15:41:18 -08001331
1332 if (!chip->jackpoll_ms)
1333 return 0;
1334
1335 i = chip->jackpoll_ms[chip->dev_index];
David Henningsson26a6cb62012-10-09 15:04:21 +02001336 if (i == 0)
1337 return 0;
1338 if (i < 50 || i > 60000)
1339 j = 0;
1340 else
1341 j = msecs_to_jiffies(i);
1342 if (j == 0)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001343 dev_warn(chip->card->dev,
1344 "jackpoll_ms value out of range: %d\n", i);
David Henningsson26a6cb62012-10-09 15:04:21 +02001345 return j;
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348/*
1349 * Codec initialization
1350 */
1351
Dylan Reide62a42a2014-02-28 15:41:19 -08001352static int azx_codec_create(struct azx *chip, const char *model,
1353 unsigned int max_slots,
1354 int *power_save_to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct hda_bus_template bus_temp;
Takashi Iwai34c25352008-10-28 11:38:58 +01001357 int c, codecs, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 memset(&bus_temp, 0, sizeof(bus_temp));
1360 bus_temp.private_data = chip;
1361 bus_temp.modelname = model;
1362 bus_temp.pci = chip->pci;
Takashi Iwai111d3af2006-02-16 18:17:58 +01001363 bus_temp.ops.command = azx_send_cmd;
1364 bus_temp.ops.get_response = azx_get_response;
Takashi Iwai176d5332008-07-30 15:01:44 +02001365 bus_temp.ops.attach_pcm = azx_attach_pcm_stream;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001366 bus_temp.ops.bus_reset = azx_bus_reset;
Takashi Iwai83012a72012-08-24 18:38:08 +02001367#ifdef CONFIG_PM
Dylan Reide62a42a2014-02-28 15:41:19 -08001368 bus_temp.power_save = power_save_to;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001369 bus_temp.ops.pm_notify = azx_power_notify;
1370#endif
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001371#ifdef CONFIG_SND_HDA_DSP_LOADER
1372 bus_temp.ops.load_dsp_prepare = azx_load_dsp_prepare;
1373 bus_temp.ops.load_dsp_trigger = azx_load_dsp_trigger;
1374 bus_temp.ops.load_dsp_cleanup = azx_load_dsp_cleanup;
1375#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Takashi Iwaid01ce992007-07-27 16:52:19 +02001377 err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus);
1378 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return err;
1380
Takashi Iwai9477c582011-05-25 09:11:37 +02001381 if (chip->driver_caps & AZX_DCAPS_RIRB_DELAY) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001382 dev_dbg(chip->card->dev, "Enable delay in RIRB handling\n");
Wei Nidc9c8e22008-09-26 13:55:56 +08001383 chip->bus->needs_damn_long_delay = 1;
Takashi Iwai9477c582011-05-25 09:11:37 +02001384 }
Wei Nidc9c8e22008-09-26 13:55:56 +08001385
Takashi Iwai34c25352008-10-28 11:38:58 +01001386 codecs = 0;
Takashi Iwai2f5983f2008-09-03 16:00:44 +02001387 if (!max_slots)
Wei Ni7445dfc2010-03-03 15:05:53 +08001388 max_slots = AZX_DEFAULT_CODECS;
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001389
1390 /* First try to probe all given codec slots */
1391 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001392 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001393 if (probe_codec(chip, c) < 0) {
1394 /* Some BIOSen give you wrong codec addresses
1395 * that don't exist
1396 */
Takashi Iwai4e76a882014-02-25 12:21:03 +01001397 dev_warn(chip->card->dev,
1398 "Codec #%d probe error; disabling it...\n", c);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001399 chip->codec_mask &= ~(1 << c);
1400 /* More badly, accessing to a non-existing
1401 * codec often screws up the controller chip,
Paul Menzel24481582010-02-08 20:37:26 +01001402 * and disturbs the further communications.
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001403 * Thus if an error occurs during probing,
1404 * better to reset the controller chip to
1405 * get back to the sanity state.
1406 */
1407 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001408 azx_init_chip(chip, 1);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001409 }
1410 }
1411 }
1412
Takashi Iwaid507cd62011-04-26 15:25:02 +02001413 /* AMD chipsets often cause the communication stalls upon certain
1414 * sequence like the pin-detection. It seems that forcing the synced
1415 * access works around the stall. Grrr...
1416 */
Takashi Iwai9477c582011-05-25 09:11:37 +02001417 if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001418 dev_dbg(chip->card->dev, "Enable sync_write for stable communication\n");
Takashi Iwaid507cd62011-04-26 15:25:02 +02001419 chip->bus->sync_write = 1;
1420 chip->bus->allow_bus_reset = 1;
1421 }
1422
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001423 /* Then create codec instances */
Takashi Iwai34c25352008-10-28 11:38:58 +01001424 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001425 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwaibccad142007-04-24 12:23:53 +02001426 struct hda_codec *codec;
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001427 err = snd_hda_codec_new(chip->bus, c, &codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (err < 0)
1429 continue;
David Henningsson26a6cb62012-10-09 15:04:21 +02001430 codec->jackpoll_interval = get_jackpoll_interval(chip);
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01001431 codec->beep_mode = chip->beep_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 codecs++;
Takashi Iwai19a982b2007-03-21 15:14:35 +01001433 }
1434 }
1435 if (!codecs) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001436 dev_err(chip->card->dev, "no codecs initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return -ENXIO;
1438 }
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001439 return 0;
1440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001442/* configure each codec instance */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001443static int azx_codec_configure(struct azx *chip)
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001444{
1445 struct hda_codec *codec;
1446 list_for_each_entry(codec, &chip->bus->codec_list, list) {
1447 snd_hda_codec_configure(codec);
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return 0;
1450}
1451
1452
1453/*
1454 * PCM support
1455 */
1456
1457/* assign a stream for the PCM */
Wu Fengguangef18bed2009-12-25 13:14:27 +08001458static inline struct azx_dev *
1459azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001461 int dev, i, nums;
Wu Fengguangef18bed2009-12-25 13:14:27 +08001462 struct azx_dev *res = NULL;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001463 /* make a non-zero unique key for the substream */
1464 int key = (substream->pcm->device << 16) | (substream->number << 2) |
1465 (substream->stream + 1);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001466
1467 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001468 dev = chip->playback_index_offset;
1469 nums = chip->playback_streams;
1470 } else {
1471 dev = chip->capture_index_offset;
1472 nums = chip->capture_streams;
1473 }
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001474 for (i = 0; i < nums; i++, dev++) {
1475 struct azx_dev *azx_dev = &chip->azx_dev[dev];
1476 dsp_lock(azx_dev);
1477 if (!azx_dev->opened && !dsp_is_locked(azx_dev)) {
1478 res = azx_dev;
1479 if (res->assigned_key == key) {
1480 res->opened = 1;
1481 res->assigned_key = key;
1482 dsp_unlock(azx_dev);
1483 return azx_dev;
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001486 dsp_unlock(azx_dev);
1487 }
Wu Fengguangef18bed2009-12-25 13:14:27 +08001488 if (res) {
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001489 dsp_lock(res);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001490 res->opened = 1;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001491 res->assigned_key = key;
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001492 dsp_unlock(res);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001493 }
1494 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497/* release the assigned stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001498static inline void azx_release_device(struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 azx_dev->opened = 0;
1501}
1502
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001503static cycle_t azx_cc_read(const struct cyclecounter *cc)
1504{
1505 struct azx_dev *azx_dev = container_of(cc, struct azx_dev, azx_cc);
1506 struct snd_pcm_substream *substream = azx_dev->substream;
1507 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1508 struct azx *chip = apcm->chip;
1509
1510 return azx_readl(chip, WALLCLK);
1511}
1512
1513static void azx_timecounter_init(struct snd_pcm_substream *substream,
1514 bool force, cycle_t last)
1515{
1516 struct azx_dev *azx_dev = get_azx_dev(substream);
1517 struct timecounter *tc = &azx_dev->azx_tc;
1518 struct cyclecounter *cc = &azx_dev->azx_cc;
1519 u64 nsec;
1520
1521 cc->read = azx_cc_read;
1522 cc->mask = CLOCKSOURCE_MASK(32);
1523
1524 /*
1525 * Converting from 24 MHz to ns means applying a 125/3 factor.
1526 * To avoid any saturation issues in intermediate operations,
1527 * the 125 factor is applied first. The division is applied
1528 * last after reading the timecounter value.
1529 * Applying the 1/3 factor as part of the multiplication
1530 * requires at least 20 bits for a decent precision, however
1531 * overflows occur after about 4 hours or less, not a option.
1532 */
1533
1534 cc->mult = 125; /* saturation after 195 years */
1535 cc->shift = 0;
1536
1537 nsec = 0; /* audio time is elapsed time since trigger */
1538 timecounter_init(tc, cc, nsec);
1539 if (force)
1540 /*
1541 * force timecounter to use predefined value,
1542 * used for synchronized starts
1543 */
1544 tc->cycle_last = last;
1545}
1546
Dylan Reidae03bbb2013-04-15 11:57:05 -07001547static u64 azx_adjust_codec_delay(struct snd_pcm_substream *substream,
Dylan Reid78daea22013-04-08 18:20:30 -07001548 u64 nsec)
1549{
1550 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1551 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
1552 u64 codec_frames, codec_nsecs;
1553
1554 if (!hinfo->ops.get_delay)
1555 return nsec;
1556
1557 codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
1558 codec_nsecs = div_u64(codec_frames * 1000000000LL,
1559 substream->runtime->rate);
1560
Dylan Reidae03bbb2013-04-15 11:57:05 -07001561 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1562 return nsec + codec_nsecs;
1563
Dylan Reid78daea22013-04-08 18:20:30 -07001564 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
1565}
1566
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001567static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
1568 struct timespec *ts)
1569{
1570 struct azx_dev *azx_dev = get_azx_dev(substream);
1571 u64 nsec;
1572
1573 nsec = timecounter_read(&azx_dev->azx_tc);
1574 nsec = div_u64(nsec, 3); /* can be optimized */
Dylan Reidae03bbb2013-04-15 11:57:05 -07001575 nsec = azx_adjust_codec_delay(substream, nsec);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001576
1577 *ts = ns_to_timespec(nsec);
1578
1579 return 0;
1580}
1581
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001582static struct snd_pcm_hardware azx_pcm_hw = {
Takashi Iwaid01ce992007-07-27 16:52:19 +02001583 .info = (SNDRV_PCM_INFO_MMAP |
1584 SNDRV_PCM_INFO_INTERLEAVED |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1586 SNDRV_PCM_INFO_MMAP_VALID |
Pavel Machek927fc862006-08-31 17:03:43 +02001587 /* No full-resume yet implemented */
1588 /* SNDRV_PCM_INFO_RESUME |*/
Takashi Iwai850f0e52008-03-18 17:11:05 +01001589 SNDRV_PCM_INFO_PAUSE |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001590 SNDRV_PCM_INFO_SYNC_START |
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001591 SNDRV_PCM_INFO_HAS_WALL_CLOCK |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001592 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1594 .rates = SNDRV_PCM_RATE_48000,
1595 .rate_min = 48000,
1596 .rate_max = 48000,
1597 .channels_min = 2,
1598 .channels_max = 2,
1599 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
1600 .period_bytes_min = 128,
1601 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
1602 .periods_min = 2,
1603 .periods_max = AZX_MAX_FRAG,
1604 .fifo_size = 0,
1605};
1606
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001607static int azx_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1610 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001611 struct azx *chip = apcm->chip;
1612 struct azx_dev *azx_dev;
1613 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 unsigned long flags;
1615 int err;
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001616 int buff_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Ingo Molnar62932df2006-01-16 16:34:20 +01001618 mutex_lock(&chip->open_mutex);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001619 azx_dev = azx_assign_device(chip, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (azx_dev == NULL) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001621 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return -EBUSY;
1623 }
1624 runtime->hw = azx_pcm_hw;
1625 runtime->hw.channels_min = hinfo->channels_min;
1626 runtime->hw.channels_max = hinfo->channels_max;
1627 runtime->hw.formats = hinfo->formats;
1628 runtime->hw.rates = hinfo->rates;
1629 snd_pcm_limit_hw_rates(runtime);
1630 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001631
1632 /* avoid wrap-around with wall-clock */
1633 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1634 20,
1635 178000000);
1636
Takashi Iwai52409aa2012-01-23 17:10:24 +01001637 if (chip->align_buffer_size)
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001638 /* constrain buffer sizes to be multiple of 128
1639 bytes. This is more efficient in terms of memory
1640 access but isn't required by the HDA spec and
1641 prevents users from specifying exact period/buffer
1642 sizes. For example for 44.1kHz, a period size set
1643 to 20ms will be rounded to 19.59ms. */
1644 buff_step = 128;
1645 else
1646 /* Don't enforce steps on buffer sizes, still need to
1647 be multiple of 4 bytes (HDA spec). Tested on Intel
1648 HDA controllers, may not work on all devices where
1649 option needs to be disabled */
1650 buff_step = 4;
1651
Joachim Deguara5f1545b2007-03-16 15:01:36 +01001652 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001653 buff_step);
Joachim Deguara5f1545b2007-03-16 15:01:36 +01001654 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001655 buff_step);
Dylan Reidb4a91cf2012-06-15 19:36:23 -07001656 snd_hda_power_up_d3wait(apcm->codec);
Takashi Iwaid01ce992007-07-27 16:52:19 +02001657 err = hinfo->ops.open(hinfo, apcm->codec, substream);
1658 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 azx_release_device(azx_dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001660 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01001661 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return err;
1663 }
Takashi Iwai70d321e2009-07-03 23:06:45 +02001664 snd_pcm_limit_hw_rates(runtime);
Takashi Iwaiaba66532009-07-05 11:44:46 +02001665 /* sanity check */
1666 if (snd_BUG_ON(!runtime->hw.channels_min) ||
1667 snd_BUG_ON(!runtime->hw.channels_max) ||
1668 snd_BUG_ON(!runtime->hw.formats) ||
1669 snd_BUG_ON(!runtime->hw.rates)) {
1670 azx_release_device(azx_dev);
1671 hinfo->ops.close(hinfo, apcm->codec, substream);
1672 snd_hda_power_down(apcm->codec);
1673 mutex_unlock(&chip->open_mutex);
1674 return -EINVAL;
1675 }
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001676
1677 /* disable WALLCLOCK timestamps for capture streams
1678 until we figure out how to handle digital inputs */
1679 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1680 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK;
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 spin_lock_irqsave(&chip->reg_lock, flags);
1683 azx_dev->substream = substream;
1684 azx_dev->running = 0;
1685 spin_unlock_irqrestore(&chip->reg_lock, flags);
1686
1687 runtime->private_data = azx_dev;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001688 snd_pcm_set_sync(substream);
Ingo Molnar62932df2006-01-16 16:34:20 +01001689 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
1691}
1692
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001693static int azx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1696 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001697 struct azx *chip = apcm->chip;
1698 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 unsigned long flags;
1700
Ingo Molnar62932df2006-01-16 16:34:20 +01001701 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 spin_lock_irqsave(&chip->reg_lock, flags);
1703 azx_dev->substream = NULL;
1704 azx_dev->running = 0;
1705 spin_unlock_irqrestore(&chip->reg_lock, flags);
1706 azx_release_device(azx_dev);
1707 hinfo->ops.close(hinfo, apcm->codec, substream);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001708 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01001709 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return 0;
1711}
1712
Takashi Iwaid01ce992007-07-27 16:52:19 +02001713static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
1714 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001716 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1717 struct azx *chip = apcm->chip;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001718 int ret;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001719
Dylan Reidb419b352014-02-28 15:41:20 -08001720 dsp_lock(get_azx_dev(substream));
1721 if (dsp_is_locked(get_azx_dev(substream))) {
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001722 ret = -EBUSY;
1723 goto unlock;
1724 }
1725
Dylan Reidb419b352014-02-28 15:41:20 -08001726 ret = chip->ops->substream_alloc_pages(chip, substream,
1727 params_buffer_bytes(hw_params));
1728unlock:
1729 dsp_unlock(get_azx_dev(substream));
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001733static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001736 struct azx_dev *azx_dev = get_azx_dev(substream);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001737 struct azx *chip = apcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Dylan Reidb419b352014-02-28 15:41:20 -08001739 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 /* reset BDL address */
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001742 dsp_lock(azx_dev);
1743 if (!dsp_is_locked(azx_dev)) {
Dylan Reid40830812014-02-28 15:41:13 -08001744 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
1745 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
1746 azx_sd_writel(chip, azx_dev, SD_CTL, 0);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001747 azx_dev->bufsize = 0;
1748 azx_dev->period_bytes = 0;
1749 azx_dev->format_val = 0;
1750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Takashi Iwaieb541332010-08-06 13:48:11 +02001752 snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Dylan Reidb419b352014-02-28 15:41:20 -08001754 err = chip->ops->substream_free_pages(chip, substream);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001755 azx_dev->prepared = 0;
1756 dsp_unlock(azx_dev);
Dylan Reidb419b352014-02-28 15:41:20 -08001757 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001760static int azx_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001763 struct azx *chip = apcm->chip;
1764 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001766 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001767 unsigned int bufsize, period_bytes, format_val, stream_tag;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001768 int err;
Stephen Warren7c9359762011-06-01 11:14:17 -06001769 struct hda_spdif_out *spdif =
1770 snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid);
1771 unsigned short ctls = spdif ? spdif->ctls : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001773 dsp_lock(azx_dev);
1774 if (dsp_is_locked(azx_dev)) {
1775 err = -EBUSY;
1776 goto unlock;
1777 }
1778
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001779 azx_stream_reset(chip, azx_dev);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001780 format_val = snd_hda_calc_stream_format(runtime->rate,
1781 runtime->channels,
1782 runtime->format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03001783 hinfo->maxbps,
Stephen Warren7c9359762011-06-01 11:14:17 -06001784 ctls);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001785 if (!format_val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001786 dev_err(chip->card->dev,
1787 "invalid format_val, rate=%d, ch=%d, format=%d\n",
1788 runtime->rate, runtime->channels, runtime->format);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001789 err = -EINVAL;
1790 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792
Takashi Iwai97b71c92009-03-18 15:09:13 +01001793 bufsize = snd_pcm_lib_buffer_bytes(substream);
1794 period_bytes = snd_pcm_lib_period_bytes(substream);
1795
Takashi Iwai4e76a882014-02-25 12:21:03 +01001796 dev_dbg(chip->card->dev, "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
1797 bufsize, format_val);
Takashi Iwai97b71c92009-03-18 15:09:13 +01001798
1799 if (bufsize != azx_dev->bufsize ||
1800 period_bytes != azx_dev->period_bytes ||
Takashi Iwai915bf292012-09-11 15:19:10 +02001801 format_val != azx_dev->format_val ||
1802 runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
Takashi Iwai97b71c92009-03-18 15:09:13 +01001803 azx_dev->bufsize = bufsize;
1804 azx_dev->period_bytes = period_bytes;
1805 azx_dev->format_val = format_val;
Takashi Iwai915bf292012-09-11 15:19:10 +02001806 azx_dev->no_period_wakeup = runtime->no_period_wakeup;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001807 err = azx_setup_periods(chip, substream, azx_dev);
1808 if (err < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001809 goto unlock;
Takashi Iwai97b71c92009-03-18 15:09:13 +01001810 }
1811
Takashi Iwaie8648e52013-12-06 17:15:01 +01001812 /* when LPIB delay correction gives a small negative value,
1813 * we ignore it; currently set the threshold statically to
1814 * 64 frames
1815 */
1816 if (runtime->period_size > 64)
1817 azx_dev->delay_negative_threshold = -frames_to_bytes(runtime, 64);
1818 else
1819 azx_dev->delay_negative_threshold = 0;
1820
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001821 /* wallclk has 24Mhz clock source */
1822 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
1823 runtime->rate) * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 azx_setup_controller(chip, azx_dev);
1825 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Dylan Reid40830812014-02-28 15:41:13 -08001826 azx_dev->fifo_size =
1827 azx_sd_readw(chip, azx_dev, SD_FIFOSIZE) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 else
1829 azx_dev->fifo_size = 0;
1830
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001831 stream_tag = azx_dev->stream_tag;
1832 /* CA-IBG chips need the playback stream starting from 1 */
Takashi Iwai9477c582011-05-25 09:11:37 +02001833 if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001834 stream_tag > chip->capture_streams)
1835 stream_tag -= chip->capture_streams;
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001836 err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
Takashi Iwaieb541332010-08-06 13:48:11 +02001837 azx_dev->format_val, substream);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001838
1839 unlock:
1840 if (!err)
1841 azx_dev->prepared = 1;
1842 dsp_unlock(azx_dev);
1843 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001846static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001849 struct azx *chip = apcm->chip;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001850 struct azx_dev *azx_dev;
1851 struct snd_pcm_substream *s;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001852 int rstart = 0, start, nsync = 0, sbits = 0;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001853 int nwait, timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Takashi Iwai1a8506d2012-10-16 15:10:08 +02001855 azx_dev = get_azx_dev(substream);
1856 trace_azx_pcm_trigger(chip, azx_dev, cmd);
1857
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001858 if (dsp_is_locked(azx_dev) || !azx_dev->prepared)
1859 return -EPIPE;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 switch (cmd) {
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001862 case SNDRV_PCM_TRIGGER_START:
1863 rstart = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1865 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai850f0e52008-03-18 17:11:05 +01001866 start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
1868 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jaroslav Kysela47123192005-08-15 20:53:07 +02001869 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai850f0e52008-03-18 17:11:05 +01001871 start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 break;
1873 default:
Takashi Iwai850f0e52008-03-18 17:11:05 +01001874 return -EINVAL;
1875 }
1876
1877 snd_pcm_group_for_each_entry(s, substream) {
1878 if (s->pcm->card != substream->pcm->card)
1879 continue;
1880 azx_dev = get_azx_dev(s);
1881 sbits |= 1 << azx_dev->index;
1882 nsync++;
1883 snd_pcm_trigger_done(s, substream);
1884 }
1885
1886 spin_lock(&chip->reg_lock);
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05001887
1888 /* first, set SYNC bits of corresponding streams */
1889 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
1890 azx_writel(chip, OLD_SSYNC,
1891 azx_readl(chip, OLD_SSYNC) | sbits);
1892 else
1893 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) | sbits);
1894
Takashi Iwai850f0e52008-03-18 17:11:05 +01001895 snd_pcm_group_for_each_entry(s, substream) {
1896 if (s->pcm->card != substream->pcm->card)
1897 continue;
1898 azx_dev = get_azx_dev(s);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001899 if (start) {
1900 azx_dev->start_wallclk = azx_readl(chip, WALLCLK);
1901 if (!rstart)
1902 azx_dev->start_wallclk -=
1903 azx_dev->period_wallclk;
Takashi Iwai850f0e52008-03-18 17:11:05 +01001904 azx_stream_start(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001905 } else {
Takashi Iwai850f0e52008-03-18 17:11:05 +01001906 azx_stream_stop(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02001907 }
Takashi Iwai850f0e52008-03-18 17:11:05 +01001908 azx_dev->running = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01001911 if (start) {
Takashi Iwai850f0e52008-03-18 17:11:05 +01001912 /* wait until all FIFOs get ready */
1913 for (timeout = 5000; timeout; timeout--) {
1914 nwait = 0;
1915 snd_pcm_group_for_each_entry(s, substream) {
1916 if (s->pcm->card != substream->pcm->card)
1917 continue;
1918 azx_dev = get_azx_dev(s);
Dylan Reid40830812014-02-28 15:41:13 -08001919 if (!(azx_sd_readb(chip, azx_dev, SD_STS) &
Takashi Iwai850f0e52008-03-18 17:11:05 +01001920 SD_STS_FIFO_READY))
1921 nwait++;
1922 }
1923 if (!nwait)
1924 break;
1925 cpu_relax();
1926 }
1927 } else {
1928 /* wait until all RUN bits are cleared */
1929 for (timeout = 5000; timeout; timeout--) {
1930 nwait = 0;
1931 snd_pcm_group_for_each_entry(s, substream) {
1932 if (s->pcm->card != substream->pcm->card)
1933 continue;
1934 azx_dev = get_azx_dev(s);
Dylan Reid40830812014-02-28 15:41:13 -08001935 if (azx_sd_readb(chip, azx_dev, SD_CTL) &
Takashi Iwai850f0e52008-03-18 17:11:05 +01001936 SD_CTL_DMA_START)
1937 nwait++;
1938 }
1939 if (!nwait)
1940 break;
1941 cpu_relax();
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05001944 spin_lock(&chip->reg_lock);
1945 /* reset SYNC bits */
1946 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
1947 azx_writel(chip, OLD_SSYNC,
1948 azx_readl(chip, OLD_SSYNC) & ~sbits);
1949 else
1950 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) & ~sbits);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001951 if (start) {
1952 azx_timecounter_init(substream, 0, 0);
1953 if (nsync > 1) {
1954 cycle_t cycle_last;
1955
1956 /* same start cycle for master and group */
1957 azx_dev = get_azx_dev(substream);
1958 cycle_last = azx_dev->azx_tc.cycle_last;
1959
1960 snd_pcm_group_for_each_entry(s, substream) {
1961 if (s->pcm->card != substream->pcm->card)
1962 continue;
1963 azx_timecounter_init(s, 1, cycle_last);
1964 }
1965 }
1966 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05001967 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01001968 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Joseph Chan0e153472008-08-26 14:38:03 +02001971/* get the current DMA position with correction on VIA chips */
1972static unsigned int azx_via_get_position(struct azx *chip,
1973 struct azx_dev *azx_dev)
1974{
1975 unsigned int link_pos, mini_pos, bound_pos;
1976 unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
1977 unsigned int fifo_size;
1978
Dylan Reid40830812014-02-28 15:41:13 -08001979 link_pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
Takashi Iwaib4a655e2011-06-07 12:26:56 +02001980 if (azx_dev->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Joseph Chan0e153472008-08-26 14:38:03 +02001981 /* Playback, no problem using link position */
1982 return link_pos;
1983 }
1984
1985 /* Capture */
1986 /* For new chipset,
1987 * use mod to get the DMA position just like old chipset
1988 */
1989 mod_dma_pos = le32_to_cpu(*azx_dev->posbuf);
1990 mod_dma_pos %= azx_dev->period_bytes;
1991
1992 /* azx_dev->fifo_size can't get FIFO size of in stream.
1993 * Get from base address + offset.
1994 */
1995 fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
1996
1997 if (azx_dev->insufficient) {
1998 /* Link position never gather than FIFO size */
1999 if (link_pos <= fifo_size)
2000 return 0;
2001
2002 azx_dev->insufficient = 0;
2003 }
2004
2005 if (link_pos <= fifo_size)
2006 mini_pos = azx_dev->bufsize + link_pos - fifo_size;
2007 else
2008 mini_pos = link_pos - fifo_size;
2009
2010 /* Find nearest previous boudary */
2011 mod_mini_pos = mini_pos % azx_dev->period_bytes;
2012 mod_link_pos = link_pos % azx_dev->period_bytes;
2013 if (mod_link_pos >= fifo_size)
2014 bound_pos = link_pos - mod_link_pos;
2015 else if (mod_dma_pos >= mod_mini_pos)
2016 bound_pos = mini_pos - mod_mini_pos;
2017 else {
2018 bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes;
2019 if (bound_pos >= azx_dev->bufsize)
2020 bound_pos = 0;
2021 }
2022
2023 /* Calculate real DMA position we want */
2024 return bound_pos + mod_dma_pos;
2025}
2026
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002027static unsigned int azx_get_position(struct azx *chip,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002028 struct azx_dev *azx_dev,
2029 bool with_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Takashi Iwai21229612013-04-05 07:27:45 +02002031 struct snd_pcm_substream *substream = azx_dev->substream;
2032 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 unsigned int pos;
Takashi Iwai21229612013-04-05 07:27:45 +02002034 int stream = substream->stream;
2035 struct hda_pcm_stream *hinfo = apcm->hinfo[stream];
Takashi Iwai1a8506d2012-10-16 15:10:08 +02002036 int delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
David Henningsson4cb36312010-09-30 10:12:50 +02002038 switch (chip->position_fix[stream]) {
2039 case POS_FIX_LPIB:
2040 /* read LPIB */
Dylan Reid40830812014-02-28 15:41:13 -08002041 pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
David Henningsson4cb36312010-09-30 10:12:50 +02002042 break;
2043 case POS_FIX_VIACOMBO:
Joseph Chan0e153472008-08-26 14:38:03 +02002044 pos = azx_via_get_position(chip, azx_dev);
David Henningsson4cb36312010-09-30 10:12:50 +02002045 break;
2046 default:
2047 /* use the position buffer */
2048 pos = le32_to_cpu(*azx_dev->posbuf);
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002049 if (with_check && chip->position_fix[stream] == POS_FIX_AUTO) {
Takashi Iwaia8103642011-06-07 12:23:23 +02002050 if (!pos || pos == (u32)-1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002051 dev_info(chip->card->dev,
2052 "Invalid position buffer, using LPIB read method instead.\n");
Takashi Iwaia8103642011-06-07 12:23:23 +02002053 chip->position_fix[stream] = POS_FIX_LPIB;
Dylan Reid40830812014-02-28 15:41:13 -08002054 pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
Takashi Iwaia8103642011-06-07 12:23:23 +02002055 } else
2056 chip->position_fix[stream] = POS_FIX_POSBUF;
2057 }
2058 break;
Takashi Iwaic74db862005-05-12 14:26:27 +02002059 }
David Henningsson4cb36312010-09-30 10:12:50 +02002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (pos >= azx_dev->bufsize)
2062 pos = 0;
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002063
2064 /* calculate runtime delay from LPIB */
Takashi Iwai21229612013-04-05 07:27:45 +02002065 if (substream->runtime &&
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002066 chip->position_fix[stream] == POS_FIX_POSBUF &&
2067 (chip->driver_caps & AZX_DCAPS_COUNT_LPIB_DELAY)) {
Dylan Reid40830812014-02-28 15:41:13 -08002068 unsigned int lpib_pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002069 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2070 delay = pos - lpib_pos;
2071 else
2072 delay = lpib_pos - pos;
Takashi Iwaie8648e52013-12-06 17:15:01 +01002073 if (delay < 0) {
2074 if (delay >= azx_dev->delay_negative_threshold)
2075 delay = 0;
2076 else
2077 delay += azx_dev->bufsize;
2078 }
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002079 if (delay >= azx_dev->period_bytes) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002080 dev_info(chip->card->dev,
2081 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
2082 delay, azx_dev->period_bytes);
Takashi Iwai1f046612012-10-16 16:52:26 +02002083 delay = 0;
2084 chip->driver_caps &= ~AZX_DCAPS_COUNT_LPIB_DELAY;
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002085 }
Takashi Iwai21229612013-04-05 07:27:45 +02002086 delay = bytes_to_frames(substream->runtime, delay);
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002087 }
Takashi Iwai21229612013-04-05 07:27:45 +02002088
2089 if (substream->runtime) {
2090 if (hinfo->ops.get_delay)
2091 delay += hinfo->ops.get_delay(hinfo, apcm->codec,
2092 substream);
2093 substream->runtime->delay = delay;
2094 }
2095
Takashi Iwai1a8506d2012-10-16 15:10:08 +02002096 trace_azx_get_position(chip, azx_dev, pos, delay);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002097 return pos;
2098}
2099
2100static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
2101{
2102 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2103 struct azx *chip = apcm->chip;
2104 struct azx_dev *azx_dev = get_azx_dev(substream);
2105 return bytes_to_frames(substream->runtime,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002106 azx_get_position(chip, azx_dev, false));
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002107}
2108
2109/*
2110 * Check whether the current DMA position is acceptable for updating
2111 * periods. Returns non-zero if it's OK.
2112 *
2113 * Many HD-audio controllers appear pretty inaccurate about
2114 * the update-IRQ timing. The IRQ is issued before actually the
2115 * data is processed. So, we need to process it afterwords in a
2116 * workqueue.
2117 */
2118static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
2119{
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002120 u32 wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002121 unsigned int pos;
2122
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002123 wallclk = azx_readl(chip, WALLCLK) - azx_dev->start_wallclk;
2124 if (wallclk < (azx_dev->period_wallclk * 2) / 3)
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002125 return -1; /* bogus (too early) interrupt */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002126
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002127 pos = azx_get_position(chip, azx_dev, true);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002128
Takashi Iwaid6d8bf52010-02-12 18:17:06 +01002129 if (WARN_ONCE(!azx_dev->period_bytes,
2130 "hda-intel: zero azx_dev->period_bytes"))
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002131 return -1; /* this shouldn't happen! */
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002132 if (wallclk < (azx_dev->period_wallclk * 5) / 4 &&
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002133 pos % azx_dev->period_bytes > azx_dev->period_bytes / 2)
2134 /* NG - it's below the first next period boundary */
Dylan Reid9cdc0112014-02-28 15:41:14 -08002135 return chip->bdl_pos_adj[chip->dev_index] ? 0 : -1;
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002136 azx_dev->start_wallclk += wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002137 return 1; /* OK, it's fine */
2138}
2139
2140/*
2141 * The work for pending PCM period updates.
2142 */
2143static void azx_irq_pending_work(struct work_struct *work)
2144{
2145 struct azx *chip = container_of(work, struct azx, irq_pending_work);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002146 int i, pending, ok;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002147
Takashi Iwaia6a950a2008-06-10 17:53:35 +02002148 if (!chip->irq_pending_warned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002149 dev_info(chip->card->dev,
2150 "IRQ timing workaround is activated for card #%d. Suggest a bigger bdl_pos_adj.\n",
2151 chip->card->number);
Takashi Iwaia6a950a2008-06-10 17:53:35 +02002152 chip->irq_pending_warned = 1;
2153 }
2154
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002155 for (;;) {
2156 pending = 0;
2157 spin_lock_irq(&chip->reg_lock);
2158 for (i = 0; i < chip->num_streams; i++) {
2159 struct azx_dev *azx_dev = &chip->azx_dev[i];
2160 if (!azx_dev->irq_pending ||
2161 !azx_dev->substream ||
2162 !azx_dev->running)
2163 continue;
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002164 ok = azx_position_ok(chip, azx_dev);
2165 if (ok > 0) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002166 azx_dev->irq_pending = 0;
2167 spin_unlock(&chip->reg_lock);
2168 snd_pcm_period_elapsed(azx_dev->substream);
2169 spin_lock(&chip->reg_lock);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002170 } else if (ok < 0) {
2171 pending = 0; /* too early */
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002172 } else
2173 pending++;
2174 }
2175 spin_unlock_irq(&chip->reg_lock);
2176 if (!pending)
2177 return;
Takashi Iwai08af4952010-08-03 14:39:04 +02002178 msleep(1);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002179 }
2180}
2181
2182/* clear irq_pending flags and assure no on-going workq */
2183static void azx_clear_irq_pending(struct azx *chip)
2184{
2185 int i;
2186
2187 spin_lock_irq(&chip->reg_lock);
2188 for (i = 0; i < chip->num_streams; i++)
2189 chip->azx_dev[i].irq_pending = 0;
2190 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002193static int azx_pcm_mmap(struct snd_pcm_substream *substream,
2194 struct vm_area_struct *area)
2195{
2196 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2197 struct azx *chip = apcm->chip;
Dylan Reid8769b2782014-02-28 15:41:21 -08002198 if (chip->ops->pcm_mmap_prepare)
2199 chip->ops->pcm_mmap_prepare(substream, area);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002200 return snd_pcm_lib_default_mmap(substream, area);
2201}
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002202
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002203static struct snd_pcm_ops azx_pcm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 .open = azx_pcm_open,
2205 .close = azx_pcm_close,
2206 .ioctl = snd_pcm_lib_ioctl,
2207 .hw_params = azx_pcm_hw_params,
2208 .hw_free = azx_pcm_hw_free,
2209 .prepare = azx_pcm_prepare,
2210 .trigger = azx_pcm_trigger,
2211 .pointer = azx_pcm_pointer,
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05002212 .wall_clock = azx_get_wallclock_tstamp,
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002213 .mmap = azx_pcm_mmap,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002214 .page = snd_pcm_sgbuf_ops_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215};
2216
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002217static void azx_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Takashi Iwai176d5332008-07-30 15:01:44 +02002219 struct azx_pcm *apcm = pcm->private_data;
2220 if (apcm) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002221 list_del(&apcm->list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002222 kfree(apcm);
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002226#define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
2227
Takashi Iwai176d5332008-07-30 15:01:44 +02002228static int
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002229azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
2230 struct hda_pcm *cpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002232 struct azx *chip = bus->private_data;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002233 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 struct azx_pcm *apcm;
Takashi Iwai176d5332008-07-30 15:01:44 +02002235 int pcm_dev = cpcm->device;
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002236 unsigned int size;
Takashi Iwai176d5332008-07-30 15:01:44 +02002237 int s, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002239 list_for_each_entry(apcm, &chip->pcm_list, list) {
2240 if (apcm->pcm->device == pcm_dev) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002241 dev_err(chip->card->dev, "PCM %d already exists\n",
2242 pcm_dev);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002243 return -EBUSY;
2244 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002245 }
2246 err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
2247 cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
2248 cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 &pcm);
2250 if (err < 0)
2251 return err;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002252 strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
Takashi Iwai176d5332008-07-30 15:01:44 +02002253 apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (apcm == NULL)
2255 return -ENOMEM;
2256 apcm->chip = chip;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002257 apcm->pcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 apcm->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 pcm->private_data = apcm;
2260 pcm->private_free = azx_pcm_free;
Takashi Iwai176d5332008-07-30 15:01:44 +02002261 if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
2262 pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002263 list_add_tail(&apcm->list, &chip->pcm_list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002264 cpcm->pcm = pcm;
2265 for (s = 0; s < 2; s++) {
2266 apcm->hinfo[s] = &cpcm->stream[s];
2267 if (cpcm->stream[s].substreams)
2268 snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
2269 }
2270 /* buffer pre-allocation */
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002271 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
2272 if (size > MAX_PREALLOC_SIZE)
2273 size = MAX_PREALLOC_SIZE;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002274 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Dylan Reid89287562014-02-28 15:41:15 -08002275 chip->card->dev,
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002276 size, MAX_PREALLOC_SIZE);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01002277 /* link to codec */
2278 pcm->dev = &codec->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return 0;
2280}
2281
2282/*
2283 * mixer creation - all stuff is implemented in hda module
2284 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002285static int azx_mixer_create(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 return snd_hda_build_controls(chip->bus);
2288}
2289
2290
2291/*
2292 * initialize SD streams
2293 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002294static int azx_init_stream(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 int i;
2297
2298 /* initialize each stream (aka device)
Takashi Iwaid01ce992007-07-27 16:52:19 +02002299 * assign the starting bdl address to each stream (device)
2300 * and initialize
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 */
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002302 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002303 struct azx_dev *azx_dev = &chip->azx_dev[i];
Takashi Iwai929861c2006-08-31 16:55:40 +02002304 azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
2306 azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
2307 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
2308 azx_dev->sd_int_sta_mask = 1 << i;
2309 /* stream tag: must be non-zero and unique */
2310 azx_dev->index = i;
2311 azx_dev->stream_tag = i + 1;
2312 }
2313
2314 return 0;
2315}
2316
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002317static int azx_acquire_irq(struct azx *chip, int do_disconnect)
2318{
Takashi Iwai437a5a42006-11-21 12:14:23 +01002319 if (request_irq(chip->pci->irq, azx_interrupt,
2320 chip->msi ? 0 : IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002321 KBUILD_MODNAME, chip)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002322 dev_err(chip->card->dev,
2323 "unable to grab IRQ %d, disabling device\n",
2324 chip->pci->irq);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002325 if (do_disconnect)
2326 snd_card_disconnect(chip->card);
2327 return -1;
2328 }
2329 chip->irq = chip->pci->irq;
Takashi Iwai69e13412006-11-21 12:10:55 +01002330 pci_intx(chip->pci, !chip->msi);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002331 return 0;
2332}
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Takashi Iwaicb53c622007-08-10 17:21:45 +02002335static void azx_stop_chip(struct azx *chip)
2336{
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002337 if (!chip->initialized)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002338 return;
2339
2340 /* disable interrupts */
2341 azx_int_disable(chip);
2342 azx_int_clear(chip);
2343
2344 /* disable CORB/RIRB */
2345 azx_free_cmd_io(chip);
2346
2347 /* disable position buffer */
2348 azx_writel(chip, DPLBASE, 0);
2349 azx_writel(chip, DPUBASE, 0);
2350
2351 chip->initialized = 0;
2352}
2353
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002354#ifdef CONFIG_SND_HDA_DSP_LOADER
2355/*
2356 * DSP loading code (e.g. for CA0132)
2357 */
2358
2359/* use the first stream for loading DSP */
2360static struct azx_dev *
2361azx_get_dsp_loader_dev(struct azx *chip)
2362{
2363 return &chip->azx_dev[chip->playback_index_offset];
2364}
2365
2366static int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
2367 unsigned int byte_size,
2368 struct snd_dma_buffer *bufp)
2369{
2370 u32 *bdl;
2371 struct azx *chip = bus->private_data;
2372 struct azx_dev *azx_dev;
2373 int err;
2374
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002375 azx_dev = azx_get_dsp_loader_dev(chip);
2376
2377 dsp_lock(azx_dev);
2378 spin_lock_irq(&chip->reg_lock);
2379 if (azx_dev->running || azx_dev->locked) {
2380 spin_unlock_irq(&chip->reg_lock);
2381 err = -EBUSY;
2382 goto unlock;
2383 }
2384 azx_dev->prepared = 0;
2385 chip->saved_azx_dev = *azx_dev;
2386 azx_dev->locked = 1;
2387 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002388
Dylan Reidb419b352014-02-28 15:41:20 -08002389 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV_SG,
2390 byte_size, bufp);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002391 if (err < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002392 goto err_alloc;
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002393
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002394 azx_dev->bufsize = byte_size;
2395 azx_dev->period_bytes = byte_size;
2396 azx_dev->format_val = format;
2397
2398 azx_stream_reset(chip, azx_dev);
2399
2400 /* reset BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08002401 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
2402 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002403
2404 azx_dev->frags = 0;
2405 bdl = (u32 *)azx_dev->bdl.area;
2406 err = setup_bdle(chip, bufp, azx_dev, &bdl, 0, byte_size, 0);
2407 if (err < 0)
2408 goto error;
2409
2410 azx_setup_controller(chip, azx_dev);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002411 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002412 return azx_dev->stream_tag;
2413
2414 error:
Dylan Reidb419b352014-02-28 15:41:20 -08002415 chip->ops->dma_free_pages(chip, bufp);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002416 err_alloc:
2417 spin_lock_irq(&chip->reg_lock);
2418 if (azx_dev->opened)
2419 *azx_dev = chip->saved_azx_dev;
2420 azx_dev->locked = 0;
2421 spin_unlock_irq(&chip->reg_lock);
2422 unlock:
2423 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002424 return err;
2425}
2426
2427static void azx_load_dsp_trigger(struct hda_bus *bus, bool start)
2428{
2429 struct azx *chip = bus->private_data;
2430 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
2431
2432 if (start)
2433 azx_stream_start(chip, azx_dev);
2434 else
2435 azx_stream_stop(chip, azx_dev);
2436 azx_dev->running = start;
2437}
2438
2439static void azx_load_dsp_cleanup(struct hda_bus *bus,
2440 struct snd_dma_buffer *dmab)
2441{
2442 struct azx *chip = bus->private_data;
2443 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
2444
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002445 if (!dmab->area || !azx_dev->locked)
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002446 return;
2447
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002448 dsp_lock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002449 /* reset BDL address */
Dylan Reid40830812014-02-28 15:41:13 -08002450 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
2451 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
2452 azx_sd_writel(chip, azx_dev, SD_CTL, 0);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002453 azx_dev->bufsize = 0;
2454 azx_dev->period_bytes = 0;
2455 azx_dev->format_val = 0;
2456
Dylan Reidb419b352014-02-28 15:41:20 -08002457 chip->ops->dma_free_pages(chip, dmab);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002458 dmab->area = NULL;
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002459
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002460 spin_lock_irq(&chip->reg_lock);
2461 if (azx_dev->opened)
2462 *azx_dev = chip->saved_azx_dev;
2463 azx_dev->locked = 0;
2464 spin_unlock_irq(&chip->reg_lock);
2465 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002466}
2467#endif /* CONFIG_SND_HDA_DSP_LOADER */
2468
Takashi Iwai83012a72012-08-24 18:38:08 +02002469#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02002470/* power-up/down the controller */
Takashi Iwai68467f52012-08-28 09:14:29 -07002471static void azx_power_notify(struct hda_bus *bus, bool power_up)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002472{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002473 struct azx *chip = bus->private_data;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002474
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01002475 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2476 return;
2477
Takashi Iwai68467f52012-08-28 09:14:29 -07002478 if (power_up)
Dylan Reid89287562014-02-28 15:41:15 -08002479 pm_runtime_get_sync(chip->card->dev);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002480 else
Dylan Reid89287562014-02-28 15:41:15 -08002481 pm_runtime_put_sync(chip->card->dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002482}
Takashi Iwai65fcd412012-08-14 17:13:32 +02002483
2484static DEFINE_MUTEX(card_list_lock);
2485static LIST_HEAD(card_list);
2486
2487static void azx_add_card_list(struct azx *chip)
2488{
2489 mutex_lock(&card_list_lock);
2490 list_add(&chip->list, &card_list);
2491 mutex_unlock(&card_list_lock);
2492}
2493
2494static void azx_del_card_list(struct azx *chip)
2495{
2496 mutex_lock(&card_list_lock);
2497 list_del_init(&chip->list);
2498 mutex_unlock(&card_list_lock);
2499}
2500
2501/* trigger power-save check at writing parameter */
2502static int param_set_xint(const char *val, const struct kernel_param *kp)
2503{
2504 struct azx *chip;
2505 struct hda_codec *c;
2506 int prev = power_save;
2507 int ret = param_set_int(val, kp);
2508
2509 if (ret || prev == power_save)
2510 return ret;
2511
2512 mutex_lock(&card_list_lock);
2513 list_for_each_entry(chip, &card_list, list) {
2514 if (!chip->bus || chip->disabled)
2515 continue;
2516 list_for_each_entry(c, &chip->bus->codec_list, list)
2517 snd_hda_power_sync(c);
2518 }
2519 mutex_unlock(&card_list_lock);
2520 return 0;
2521}
2522#else
2523#define azx_add_card_list(chip) /* NOP */
2524#define azx_del_card_list(chip) /* NOP */
Takashi Iwai83012a72012-08-24 18:38:08 +02002525#endif /* CONFIG_PM */
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002526
Takashi Iwai7ccbde52012-08-14 18:10:09 +02002527#if defined(CONFIG_PM_SLEEP) || defined(SUPPORT_VGA_SWITCHEROO)
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002528/*
2529 * power management
2530 */
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002531static int azx_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002533 struct pci_dev *pci = to_pci_dev(dev);
2534 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002535 struct azx *chip = card->private_data;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002536 struct azx_pcm *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Takashi Iwaic5c21522012-12-04 17:01:25 +01002538 if (chip->disabled)
2539 return 0;
2540
Takashi Iwai421a1252005-11-17 16:11:09 +01002541 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002542 azx_clear_irq_pending(chip);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002543 list_for_each_entry(p, &chip->pcm_list, list)
2544 snd_pcm_suspend_all(p->pcm);
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02002545 if (chip->initialized)
Takashi Iwai8dd78332009-06-02 01:16:07 +02002546 snd_hda_suspend(chip->bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002547 azx_stop_chip(chip);
Mengdong Lin7295b262013-06-25 05:58:49 -04002548 azx_enter_link_reset(chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002549 if (chip->irq >= 0) {
Takashi Iwai43001c92006-09-08 12:30:03 +02002550 free_irq(chip->irq, chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002551 chip->irq = -1;
2552 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002553 if (chip->msi)
Takashi Iwai43001c92006-09-08 12:30:03 +02002554 pci_disable_msi(chip->pci);
Takashi Iwai421a1252005-11-17 16:11:09 +01002555 pci_disable_device(pci);
2556 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002557 pci_set_power_state(pci, PCI_D3hot);
Wang Xingchao99a20082013-05-30 22:07:10 +08002558 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2559 hda_display_power(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return 0;
2561}
2562
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002563static int azx_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002565 struct pci_dev *pci = to_pci_dev(dev);
2566 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002567 struct azx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Takashi Iwaic5c21522012-12-04 17:01:25 +01002569 if (chip->disabled)
2570 return 0;
2571
Wang Xingchao99a20082013-05-30 22:07:10 +08002572 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2573 hda_display_power(true);
Takashi Iwaid14a7e02009-02-16 10:13:03 +01002574 pci_set_power_state(pci, PCI_D0);
2575 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002576 if (pci_enable_device(pci) < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002577 dev_err(chip->card->dev,
2578 "pci_enable_device failed, disabling device\n");
Takashi Iwai30b35392006-10-11 18:52:53 +02002579 snd_card_disconnect(card);
2580 return -EIO;
2581 }
2582 pci_set_master(pci);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002583 if (chip->msi)
2584 if (pci_enable_msi(pci) < 0)
2585 chip->msi = 0;
2586 if (azx_acquire_irq(chip, 1) < 0)
Takashi Iwai30b35392006-10-11 18:52:53 +02002587 return -EIO;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002588 azx_init_pci(chip);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002589
Takashi Iwai7f308302012-05-08 16:52:23 +02002590 azx_init_chip(chip, 1);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 snd_hda_resume(chip->bus);
Takashi Iwai421a1252005-11-17 16:11:09 +01002593 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return 0;
2595}
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002596#endif /* CONFIG_PM_SLEEP || SUPPORT_VGA_SWITCHEROO */
2597
2598#ifdef CONFIG_PM_RUNTIME
2599static int azx_runtime_suspend(struct device *dev)
2600{
2601 struct snd_card *card = dev_get_drvdata(dev);
2602 struct azx *chip = card->private_data;
2603
Dave Airlie246efa42013-07-29 15:19:29 +10002604 if (chip->disabled)
2605 return 0;
2606
2607 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2608 return 0;
2609
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002610 /* enable controller wake up event */
2611 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) |
2612 STATESTS_INT_MASK);
2613
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002614 azx_stop_chip(chip);
Takashi Iwai873ce8a2013-11-26 11:58:40 +01002615 azx_enter_link_reset(chip);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002616 azx_clear_irq_pending(chip);
Wang Xingchao99a20082013-05-30 22:07:10 +08002617 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2618 hda_display_power(false);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002619 return 0;
2620}
2621
2622static int azx_runtime_resume(struct device *dev)
2623{
2624 struct snd_card *card = dev_get_drvdata(dev);
2625 struct azx *chip = card->private_data;
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002626 struct hda_bus *bus;
2627 struct hda_codec *codec;
2628 int status;
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002629
Dave Airlie246efa42013-07-29 15:19:29 +10002630 if (chip->disabled)
2631 return 0;
2632
2633 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2634 return 0;
2635
Wang Xingchao99a20082013-05-30 22:07:10 +08002636 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2637 hda_display_power(true);
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002638
2639 /* Read STATESTS before controller reset */
2640 status = azx_readw(chip, STATESTS);
2641
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002642 azx_init_pci(chip);
2643 azx_init_chip(chip, 1);
Wang Xingchao7d4f6062013-07-25 23:34:46 -04002644
2645 bus = chip->bus;
2646 if (status && bus) {
2647 list_for_each_entry(codec, &bus->codec_list, list)
2648 if (status & (1 << codec->addr))
2649 queue_delayed_work(codec->bus->workq,
2650 &codec->jackpoll_work, codec->jackpoll_interval);
2651 }
2652
2653 /* disable controller Wake Up event*/
2654 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) &
2655 ~STATESTS_INT_MASK);
2656
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002657 return 0;
2658}
Takashi Iwai6eb827d2012-12-12 11:50:12 +01002659
2660static int azx_runtime_idle(struct device *dev)
2661{
2662 struct snd_card *card = dev_get_drvdata(dev);
2663 struct azx *chip = card->private_data;
2664
Dave Airlie246efa42013-07-29 15:19:29 +10002665 if (chip->disabled)
2666 return 0;
2667
Takashi Iwai6eb827d2012-12-12 11:50:12 +01002668 if (!power_save_controller ||
2669 !(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2670 return -EBUSY;
2671
2672 return 0;
2673}
2674
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002675#endif /* CONFIG_PM_RUNTIME */
2676
2677#ifdef CONFIG_PM
2678static const struct dev_pm_ops azx_pm = {
2679 SET_SYSTEM_SLEEP_PM_OPS(azx_suspend, azx_resume)
Takashi Iwai6eb827d2012-12-12 11:50:12 +01002680 SET_RUNTIME_PM_OPS(azx_runtime_suspend, azx_runtime_resume, azx_runtime_idle)
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002681};
2682
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002683#define AZX_PM_OPS &azx_pm
2684#else
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002685#define AZX_PM_OPS NULL
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002686#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688
2689/*
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002690 * reboot notifier for hang-up problem at power-down
2691 */
2692static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
2693{
2694 struct azx *chip = container_of(nb, struct azx, reboot_notifier);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01002695 snd_hda_bus_reboot_notify(chip->bus);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002696 azx_stop_chip(chip);
2697 return NOTIFY_OK;
2698}
2699
2700static void azx_notifier_register(struct azx *chip)
2701{
2702 chip->reboot_notifier.notifier_call = azx_halt;
2703 register_reboot_notifier(&chip->reboot_notifier);
2704}
2705
2706static void azx_notifier_unregister(struct azx *chip)
2707{
2708 if (chip->reboot_notifier.notifier_call)
2709 unregister_reboot_notifier(&chip->reboot_notifier);
2710}
2711
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01002712static int azx_probe_continue(struct azx *chip);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002713
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002714#ifdef SUPPORT_VGA_SWITCHEROO
Bill Pembertone23e7a12012-12-06 12:35:10 -05002715static struct pci_dev *get_bound_vga(struct pci_dev *pci);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002716
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002717static void azx_vs_set_state(struct pci_dev *pci,
2718 enum vga_switcheroo_state state)
2719{
2720 struct snd_card *card = pci_get_drvdata(pci);
2721 struct azx *chip = card->private_data;
2722 bool disabled;
2723
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002724 wait_for_completion(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002725 if (chip->init_failed)
2726 return;
2727
2728 disabled = (state == VGA_SWITCHEROO_OFF);
2729 if (chip->disabled == disabled)
2730 return;
2731
2732 if (!chip->bus) {
2733 chip->disabled = disabled;
2734 if (!disabled) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002735 dev_info(chip->card->dev,
2736 "Start delayed initialization\n");
Takashi Iwai5c906802013-05-30 22:07:09 +08002737 if (azx_probe_continue(chip) < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002738 dev_err(chip->card->dev, "initialization error\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002739 chip->init_failed = true;
2740 }
2741 }
2742 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002743 dev_info(chip->card->dev, "%s via VGA-switcheroo\n",
2744 disabled ? "Disabling" : "Enabling");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002745 if (disabled) {
Dylan Reid89287562014-02-28 15:41:15 -08002746 pm_runtime_put_sync_suspend(card->dev);
2747 azx_suspend(card->dev);
Dave Airlie246efa42013-07-29 15:19:29 +10002748 /* when we get suspended by vga switcheroo we end up in D3cold,
2749 * however we have no ACPI handle, so pci/acpi can't put us there,
2750 * put ourselves there */
2751 pci->current_state = PCI_D3cold;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002752 chip->disabled = true;
Takashi Iwai128960a2012-10-12 17:28:18 +02002753 if (snd_hda_lock_devices(chip->bus))
Takashi Iwai4e76a882014-02-25 12:21:03 +01002754 dev_warn(chip->card->dev,
2755 "Cannot lock devices!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002756 } else {
2757 snd_hda_unlock_devices(chip->bus);
Dylan Reid89287562014-02-28 15:41:15 -08002758 pm_runtime_get_noresume(card->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002759 chip->disabled = false;
Dylan Reid89287562014-02-28 15:41:15 -08002760 azx_resume(card->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002761 }
2762 }
2763}
2764
2765static bool azx_vs_can_switch(struct pci_dev *pci)
2766{
2767 struct snd_card *card = pci_get_drvdata(pci);
2768 struct azx *chip = card->private_data;
2769
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002770 wait_for_completion(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002771 if (chip->init_failed)
2772 return false;
2773 if (chip->disabled || !chip->bus)
2774 return true;
2775 if (snd_hda_lock_devices(chip->bus))
2776 return false;
2777 snd_hda_unlock_devices(chip->bus);
2778 return true;
2779}
2780
Bill Pembertone23e7a12012-12-06 12:35:10 -05002781static void init_vga_switcheroo(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002782{
2783 struct pci_dev *p = get_bound_vga(chip->pci);
2784 if (p) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002785 dev_info(chip->card->dev,
2786 "Handle VGA-switcheroo audio client\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002787 chip->use_vga_switcheroo = 1;
2788 pci_dev_put(p);
2789 }
2790}
2791
2792static const struct vga_switcheroo_client_ops azx_vs_ops = {
2793 .set_gpu_state = azx_vs_set_state,
2794 .can_switch = azx_vs_can_switch,
2795};
2796
Bill Pembertone23e7a12012-12-06 12:35:10 -05002797static int register_vga_switcheroo(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002798{
Takashi Iwai128960a2012-10-12 17:28:18 +02002799 int err;
2800
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002801 if (!chip->use_vga_switcheroo)
2802 return 0;
2803 /* FIXME: currently only handling DIS controller
2804 * is there any machine with two switchable HDMI audio controllers?
2805 */
Takashi Iwai128960a2012-10-12 17:28:18 +02002806 err = vga_switcheroo_register_audio_client(chip->pci, &azx_vs_ops,
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002807 VGA_SWITCHEROO_DIS,
2808 chip->bus != NULL);
Takashi Iwai128960a2012-10-12 17:28:18 +02002809 if (err < 0)
2810 return err;
2811 chip->vga_switcheroo_registered = 1;
Dave Airlie246efa42013-07-29 15:19:29 +10002812
2813 /* register as an optimus hdmi audio power domain */
Dylan Reid89287562014-02-28 15:41:15 -08002814 vga_switcheroo_init_domain_pm_optimus_hdmi_audio(chip->card->dev,
2815 &chip->hdmi_pm_domain);
Takashi Iwai128960a2012-10-12 17:28:18 +02002816 return 0;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002817}
2818#else
2819#define init_vga_switcheroo(chip) /* NOP */
2820#define register_vga_switcheroo(chip) 0
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002821#define check_hdmi_disabled(pci) false
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002822#endif /* SUPPORT_VGA_SWITCHER */
2823
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002824/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 * destructor
2826 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002827static int azx_free(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Wang Xingchaoc67e2222013-05-30 22:07:08 +08002829 struct pci_dev *pci = chip->pci;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002830 int i;
2831
Wang Xingchaoc67e2222013-05-30 22:07:08 +08002832 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME)
2833 && chip->running)
2834 pm_runtime_get_noresume(&pci->dev);
2835
Takashi Iwai65fcd412012-08-14 17:13:32 +02002836 azx_del_card_list(chip);
2837
Takashi Iwai0cbf0092008-10-29 16:18:25 +01002838 azx_notifier_unregister(chip);
2839
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002840 chip->init_failed = 1; /* to be sure */
Daniel J Blueman44728e92012-12-18 23:59:33 +08002841 complete_all(&chip->probe_wait);
Takashi Iwaif4c482a2012-12-04 15:09:23 +01002842
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002843 if (use_vga_switcheroo(chip)) {
2844 if (chip->disabled && chip->bus)
2845 snd_hda_unlock_devices(chip->bus);
Takashi Iwai128960a2012-10-12 17:28:18 +02002846 if (chip->vga_switcheroo_registered)
2847 vga_switcheroo_unregister_client(chip->pci);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002848 }
2849
Takashi Iwaice43fba2005-05-30 20:33:44 +02002850 if (chip->initialized) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002851 azx_clear_irq_pending(chip);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002852 for (i = 0; i < chip->num_streams; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 azx_stream_stop(chip, &chip->azx_dev[i]);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002854 azx_stop_chip(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856
Jeff Garzikf000fd82008-04-22 13:50:34 +02002857 if (chip->irq >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 free_irq(chip->irq, (void*)chip);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002859 if (chip->msi)
Takashi Iwai30b35392006-10-11 18:52:53 +02002860 pci_disable_msi(chip->pci);
Takashi Iwaif079c252006-06-01 11:42:14 +02002861 if (chip->remap_addr)
2862 iounmap(chip->remap_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002864 if (chip->azx_dev) {
2865 for (i = 0; i < chip->num_streams; i++)
Dylan Reidb419b352014-02-28 15:41:20 -08002866 if (chip->azx_dev[i].bdl.area)
2867 chip->ops->dma_free_pages(
2868 chip, &chip->azx_dev[i].bdl);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002869 }
Dylan Reidb419b352014-02-28 15:41:20 -08002870 if (chip->rb.area)
2871 chip->ops->dma_free_pages(chip, &chip->rb);
2872 if (chip->posbuf.area)
2873 chip->ops->dma_free_pages(chip, &chip->posbuf);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02002874 if (chip->region_requested)
2875 pci_release_regions(chip->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 pci_disable_device(chip->pci);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002877 kfree(chip->azx_dev);
Takashi Iwai4918cda2012-08-09 12:33:28 +02002878#ifdef CONFIG_SND_HDA_PATCH_LOADER
2879 if (chip->fw)
2880 release_firmware(chip->fw);
2881#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08002882 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
2883 hda_display_power(false);
2884 hda_i915_exit();
2885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 kfree(chip);
2887
2888 return 0;
2889}
2890
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002891static int azx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
2893 return azx_free(device->device_data);
2894}
2895
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002896#ifdef SUPPORT_VGA_SWITCHEROO
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897/*
Takashi Iwai91219472012-04-26 12:13:25 +02002898 * Check of disabled HDMI controller by vga-switcheroo
2899 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002900static struct pci_dev *get_bound_vga(struct pci_dev *pci)
Takashi Iwai91219472012-04-26 12:13:25 +02002901{
2902 struct pci_dev *p;
2903
2904 /* check only discrete GPU */
2905 switch (pci->vendor) {
2906 case PCI_VENDOR_ID_ATI:
2907 case PCI_VENDOR_ID_AMD:
2908 case PCI_VENDOR_ID_NVIDIA:
2909 if (pci->devfn == 1) {
2910 p = pci_get_domain_bus_and_slot(pci_domain_nr(pci->bus),
2911 pci->bus->number, 0);
2912 if (p) {
2913 if ((p->class >> 8) == PCI_CLASS_DISPLAY_VGA)
2914 return p;
2915 pci_dev_put(p);
2916 }
2917 }
2918 break;
2919 }
2920 return NULL;
2921}
2922
Bill Pembertone23e7a12012-12-06 12:35:10 -05002923static bool check_hdmi_disabled(struct pci_dev *pci)
Takashi Iwai91219472012-04-26 12:13:25 +02002924{
2925 bool vga_inactive = false;
2926 struct pci_dev *p = get_bound_vga(pci);
2927
2928 if (p) {
Takashi Iwai12b78a72012-06-07 12:15:16 +02002929 if (vga_switcheroo_get_client_state(p) == VGA_SWITCHEROO_OFF)
Takashi Iwai91219472012-04-26 12:13:25 +02002930 vga_inactive = true;
2931 pci_dev_put(p);
2932 }
2933 return vga_inactive;
2934}
Steven Newbury8393ec4a2012-06-08 13:06:29 +02002935#endif /* SUPPORT_VGA_SWITCHEROO */
Takashi Iwai91219472012-04-26 12:13:25 +02002936
2937/*
Takashi Iwai3372a152007-02-01 15:46:50 +01002938 * white/black-listing for position_fix
2939 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002940static struct snd_pci_quirk position_fix_list[] = {
Takashi Iwaid2e1c972008-06-10 17:53:34 +02002941 SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
2942 SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
Takashi Iwai2f703e72009-12-01 14:17:37 +01002943 SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
Takashi Iwaid2e1c972008-06-10 17:53:34 +02002944 SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
Daniel T Chendd37f8e2010-05-30 01:17:03 -04002945 SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS", POS_FIX_LPIB),
Daniel T Chen9f75c1b2010-05-30 13:08:41 -04002946 SND_PCI_QUIRK(0x1043, 0x81e7, "ASUS M2V", POS_FIX_LPIB),
Daniel T Chene96d3122010-05-27 18:32:18 -04002947 SND_PCI_QUIRK(0x104d, 0x9069, "Sony VPCS11V9E", POS_FIX_LPIB),
David Henningssonb01de4f2012-01-12 16:31:14 +01002948 SND_PCI_QUIRK(0x10de, 0xcb89, "Macbook Pro 7,1", POS_FIX_LPIB),
Daniel T Chen61bb42c2010-05-29 11:04:11 -04002949 SND_PCI_QUIRK(0x1297, 0x3166, "Shuttle", POS_FIX_LPIB),
Daniel T Chen9ec8dda2010-03-28 02:34:40 -04002950 SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
Takashi Iwai45d4ebf2009-11-30 11:58:30 +01002951 SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
Takashi Iwai8815cd02010-04-15 09:02:41 +02002952 SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
Daniel T Chenb90c0762010-05-30 19:31:41 -04002953 SND_PCI_QUIRK(0x1849, 0x0888, "775Dual-VSTA", POS_FIX_LPIB),
Daniel T Chen0e0280d2010-04-21 19:55:43 -04002954 SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
Takashi Iwai3372a152007-02-01 15:46:50 +01002955 {}
2956};
2957
Bill Pembertone23e7a12012-12-06 12:35:10 -05002958static int check_position_fix(struct azx *chip, int fix)
Takashi Iwai3372a152007-02-01 15:46:50 +01002959{
2960 const struct snd_pci_quirk *q;
2961
Takashi Iwaic673ba12009-03-17 07:49:14 +01002962 switch (fix) {
Takashi Iwai1dac6692012-09-13 14:59:47 +02002963 case POS_FIX_AUTO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01002964 case POS_FIX_LPIB:
2965 case POS_FIX_POSBUF:
David Henningsson4cb36312010-09-30 10:12:50 +02002966 case POS_FIX_VIACOMBO:
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01002967 case POS_FIX_COMBO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01002968 return fix;
2969 }
2970
Takashi Iwaic673ba12009-03-17 07:49:14 +01002971 q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
2972 if (q) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002973 dev_info(chip->card->dev,
2974 "position_fix set to %d for device %04x:%04x\n",
2975 q->value, q->subvendor, q->subdevice);
Takashi Iwaic673ba12009-03-17 07:49:14 +01002976 return q->value;
Takashi Iwai3372a152007-02-01 15:46:50 +01002977 }
David Henningssonbdd9ef22010-10-04 12:02:14 +02002978
2979 /* Check VIA/ATI HD Audio Controller exist */
Takashi Iwai9477c582011-05-25 09:11:37 +02002980 if (chip->driver_caps & AZX_DCAPS_POSFIX_VIA) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002981 dev_dbg(chip->card->dev, "Using VIACOMBO position fix\n");
David Henningssonbdd9ef22010-10-04 12:02:14 +02002982 return POS_FIX_VIACOMBO;
2983 }
Takashi Iwai9477c582011-05-25 09:11:37 +02002984 if (chip->driver_caps & AZX_DCAPS_POSFIX_LPIB) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002985 dev_dbg(chip->card->dev, "Using LPIB position fix\n");
Takashi Iwai9477c582011-05-25 09:11:37 +02002986 return POS_FIX_LPIB;
2987 }
Takashi Iwaic673ba12009-03-17 07:49:14 +01002988 return POS_FIX_AUTO;
Takashi Iwai3372a152007-02-01 15:46:50 +01002989}
2990
2991/*
Takashi Iwai669ba272007-08-17 09:17:36 +02002992 * black-lists for probe_mask
2993 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002994static struct snd_pci_quirk probe_mask_list[] = {
Takashi Iwai669ba272007-08-17 09:17:36 +02002995 /* Thinkpad often breaks the controller communication when accessing
2996 * to the non-working (or non-existing) modem codec slot.
2997 */
2998 SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
2999 SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
3000 SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
Takashi Iwai0edb9452008-11-07 14:53:09 +01003001 /* broken BIOS */
3002 SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
Takashi Iwaief1681d2008-11-24 17:29:28 +01003003 /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
3004 SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01003005 /* forced codec slots */
Ozan Çağlayan93574842009-05-23 15:00:04 +03003006 SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01003007 SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
Jaroslav Kyselaf3af9052012-04-26 17:52:35 +02003008 /* WinFast VP200 H (Teradici) user reported broken communication */
3009 SND_PCI_QUIRK(0x3a21, 0x040d, "WinFast VP200 H", 0x101),
Takashi Iwai669ba272007-08-17 09:17:36 +02003010 {}
3011};
3012
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003013#define AZX_FORCE_CODEC_MASK 0x100
3014
Bill Pembertone23e7a12012-12-06 12:35:10 -05003015static void check_probe_mask(struct azx *chip, int dev)
Takashi Iwai669ba272007-08-17 09:17:36 +02003016{
3017 const struct snd_pci_quirk *q;
3018
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003019 chip->codec_probe_mask = probe_mask[dev];
3020 if (chip->codec_probe_mask == -1) {
Takashi Iwai669ba272007-08-17 09:17:36 +02003021 q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
3022 if (q) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003023 dev_info(chip->card->dev,
3024 "probe_mask set to 0x%x for device %04x:%04x\n",
3025 q->value, q->subvendor, q->subdevice);
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003026 chip->codec_probe_mask = q->value;
Takashi Iwai669ba272007-08-17 09:17:36 +02003027 }
3028 }
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003029
3030 /* check forced option */
3031 if (chip->codec_probe_mask != -1 &&
3032 (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
3033 chip->codec_mask = chip->codec_probe_mask & 0xff;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003034 dev_info(chip->card->dev, "codec_mask forced to 0x%x\n",
3035 chip->codec_mask);
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003036 }
Takashi Iwai669ba272007-08-17 09:17:36 +02003037}
3038
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003039/*
Takashi Iwai716238552009-09-28 13:14:04 +02003040 * white/black-list for enable_msi
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003041 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003042static struct snd_pci_quirk msi_black_list[] = {
David Henningsson693e0cb2013-12-12 09:52:03 +01003043 SND_PCI_QUIRK(0x103c, 0x2191, "HP", 0), /* AMD Hudson */
3044 SND_PCI_QUIRK(0x103c, 0x2192, "HP", 0), /* AMD Hudson */
3045 SND_PCI_QUIRK(0x103c, 0x21f7, "HP", 0), /* AMD Hudson */
3046 SND_PCI_QUIRK(0x103c, 0x21fa, "HP", 0), /* AMD Hudson */
Takashi Iwai9dc83982009-12-22 08:15:01 +01003047 SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai0a27fcf2010-02-15 17:05:28 +01003048 SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
Ralf Gerbigecd21622010-03-09 18:25:47 +01003049 SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
Takashi Iwai83f72152013-09-09 10:20:48 +02003050 SND_PCI_QUIRK(0x1179, 0xfb44, "Toshiba Satellite C870", 0), /* AMD Hudson */
Michele Ballabio4193d132010-03-06 21:06:46 +01003051 SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai38155952010-04-04 12:14:03 +02003052 SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003053 {}
3054};
3055
Bill Pembertone23e7a12012-12-06 12:35:10 -05003056static void check_msi(struct azx *chip)
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003057{
3058 const struct snd_pci_quirk *q;
3059
Takashi Iwai716238552009-09-28 13:14:04 +02003060 if (enable_msi >= 0) {
3061 chip->msi = !!enable_msi;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003062 return;
Takashi Iwai716238552009-09-28 13:14:04 +02003063 }
3064 chip->msi = 1; /* enable MSI as default */
3065 q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003066 if (q) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003067 dev_info(chip->card->dev,
3068 "msi for device %04x:%04x set to %d\n",
3069 q->subvendor, q->subdevice, q->value);
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003070 chip->msi = q->value;
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003071 return;
3072 }
3073
3074 /* NVidia chipsets seem to cause troubles with MSI */
Takashi Iwai9477c582011-05-25 09:11:37 +02003075 if (chip->driver_caps & AZX_DCAPS_NO_MSI) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003076 dev_info(chip->card->dev, "Disabling MSI\n");
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003077 chip->msi = 0;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003078 }
3079}
3080
Takashi Iwaia1585d72011-12-14 09:27:04 +01003081/* check the snoop mode availability */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003082static void azx_check_snoop_available(struct azx *chip)
Takashi Iwaia1585d72011-12-14 09:27:04 +01003083{
3084 bool snoop = chip->snoop;
3085
3086 switch (chip->driver_type) {
3087 case AZX_DRIVER_VIA:
3088 /* force to non-snoop mode for a new VIA controller
3089 * when BIOS is set
3090 */
3091 if (snoop) {
3092 u8 val;
3093 pci_read_config_byte(chip->pci, 0x42, &val);
3094 if (!(val & 0x80) && chip->pci->revision == 0x30)
3095 snoop = false;
3096 }
3097 break;
3098 case AZX_DRIVER_ATIHDMI_NS:
3099 /* new ATI HDMI requires non-snoop */
3100 snoop = false;
3101 break;
Takashi Iwaic1279f82013-02-07 17:36:22 +01003102 case AZX_DRIVER_CTHDA:
3103 snoop = false;
3104 break;
Takashi Iwaia1585d72011-12-14 09:27:04 +01003105 }
3106
3107 if (snoop != chip->snoop) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003108 dev_info(chip->card->dev, "Force to %s mode\n",
3109 snoop ? "snoop" : "non-snoop");
Takashi Iwaia1585d72011-12-14 09:27:04 +01003110 chip->snoop = snoop;
3111 }
3112}
Takashi Iwai669ba272007-08-17 09:17:36 +02003113
Wang Xingchao99a20082013-05-30 22:07:10 +08003114static void azx_probe_work(struct work_struct *work)
3115{
3116 azx_probe_continue(container_of(work, struct azx, probe_work));
3117}
Wang Xingchao99a20082013-05-30 22:07:10 +08003118
Takashi Iwai669ba272007-08-17 09:17:36 +02003119/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 * constructor
3121 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003122static int azx_create(struct snd_card *card, struct pci_dev *pci,
3123 int dev, unsigned int driver_caps,
Dylan Reid40830812014-02-28 15:41:13 -08003124 const struct hda_controller_ops *hda_ops,
Bill Pembertone23e7a12012-12-06 12:35:10 -05003125 struct azx **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003127 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 .dev_free = azx_dev_free,
3129 };
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003130 struct azx *chip;
3131 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133 *rchip = NULL;
Tobin Davisbcd72002008-01-15 11:23:55 +01003134
Pavel Machek927fc862006-08-31 17:03:43 +02003135 err = pci_enable_device(pci);
3136 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return err;
3138
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003139 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003140 if (!chip) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003141 dev_err(card->dev, "Cannot allocate chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 pci_disable_device(pci);
3143 return -ENOMEM;
3144 }
3145
3146 spin_lock_init(&chip->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01003147 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 chip->card = card;
3149 chip->pci = pci;
Dylan Reid40830812014-02-28 15:41:13 -08003150 chip->ops = hda_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 chip->irq = -1;
Takashi Iwai9477c582011-05-25 09:11:37 +02003152 chip->driver_caps = driver_caps;
3153 chip->driver_type = driver_caps & 0xff;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003154 check_msi(chip);
Takashi Iwai555e2192008-06-10 17:53:34 +02003155 chip->dev_index = dev;
Dylan Reid749ee282014-02-28 15:41:18 -08003156 chip->jackpoll_ms = jackpoll_ms;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02003157 INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003158 INIT_LIST_HEAD(&chip->pcm_list);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003159 INIT_LIST_HEAD(&chip->list);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003160 init_vga_switcheroo(chip);
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003161 init_completion(&chip->probe_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Shahin Ghazinouribeaffc32010-05-11 08:19:55 +02003163 chip->position_fix[0] = chip->position_fix[1] =
3164 check_position_fix(chip, position_fix[dev]);
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01003165 /* combo mode uses LPIB for playback */
3166 if (chip->position_fix[0] == POS_FIX_COMBO) {
3167 chip->position_fix[0] = POS_FIX_LPIB;
3168 chip->position_fix[1] = POS_FIX_AUTO;
3169 }
3170
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003171 check_probe_mask(chip, dev);
Takashi Iwai3372a152007-02-01 15:46:50 +01003172
Takashi Iwai27346162006-01-12 18:28:44 +01003173 chip->single_cmd = single_cmd;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003174 chip->snoop = hda_snoop;
Takashi Iwaia1585d72011-12-14 09:27:04 +01003175 azx_check_snoop_available(chip);
Takashi Iwaic74db862005-05-12 14:26:27 +02003176
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003177 if (bdl_pos_adj[dev] < 0) {
3178 switch (chip->driver_type) {
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003179 case AZX_DRIVER_ICH:
Seth Heasley32679f92010-02-22 17:31:09 -08003180 case AZX_DRIVER_PCH:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003181 bdl_pos_adj[dev] = 1;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003182 break;
3183 default:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003184 bdl_pos_adj[dev] = 32;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003185 break;
3186 }
3187 }
Dylan Reid9cdc0112014-02-28 15:41:14 -08003188 chip->bdl_pos_adj = bdl_pos_adj;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003189
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003190 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
3191 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003192 dev_err(card->dev, "Error creating device [card]!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003193 azx_free(chip);
3194 return err;
3195 }
3196
Wang Xingchao99a20082013-05-30 22:07:10 +08003197 /* continue probing in work context as may trigger request module */
3198 INIT_WORK(&chip->probe_work, azx_probe_work);
Wang Xingchao99a20082013-05-30 22:07:10 +08003199
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003200 *rchip = chip;
Wang Xingchao99a20082013-05-30 22:07:10 +08003201
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003202 return 0;
3203}
3204
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01003205static int azx_first_init(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003206{
3207 int dev = chip->dev_index;
3208 struct pci_dev *pci = chip->pci;
3209 struct snd_card *card = chip->card;
3210 int i, err;
3211 unsigned short gcap;
3212
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003213#if BITS_PER_LONG != 64
3214 /* Fix up base address on ULI M5461 */
3215 if (chip->driver_type == AZX_DRIVER_ULI) {
3216 u16 tmp3;
3217 pci_read_config_word(pci, 0x40, &tmp3);
3218 pci_write_config_word(pci, 0x40, tmp3 | 0x10);
3219 pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
3220 }
3221#endif
3222
Pavel Machek927fc862006-08-31 17:03:43 +02003223 err = pci_request_regions(pci, "ICH HD audio");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003224 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return err;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003226 chip->region_requested = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Pavel Machek927fc862006-08-31 17:03:43 +02003228 chip->addr = pci_resource_start(pci, 0);
Arjan van de Ven2f5ad542008-09-28 16:20:09 -07003229 chip->remap_addr = pci_ioremap_bar(pci, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (chip->remap_addr == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003231 dev_err(card->dev, "ioremap error\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003232 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
3234
Takashi Iwai68e7fff2006-10-23 13:40:59 +02003235 if (chip->msi)
3236 if (pci_enable_msi(pci) < 0)
3237 chip->msi = 0;
Stephen Hemminger7376d012006-08-21 19:17:46 +02003238
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003239 if (azx_acquire_irq(chip, 0) < 0)
3240 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 pci_set_master(pci);
3243 synchronize_irq(chip->irq);
3244
Tobin Davisbcd72002008-01-15 11:23:55 +01003245 gcap = azx_readw(chip, GCAP);
Takashi Iwai4e76a882014-02-25 12:21:03 +01003246 dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
Tobin Davisbcd72002008-01-15 11:23:55 +01003247
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003248 /* disable SB600 64bit support for safety */
Takashi Iwai9477c582011-05-25 09:11:37 +02003249 if (chip->pci->vendor == PCI_VENDOR_ID_ATI) {
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003250 struct pci_dev *p_smbus;
3251 p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
3252 PCI_DEVICE_ID_ATI_SBX00_SMBUS,
3253 NULL);
3254 if (p_smbus) {
3255 if (p_smbus->revision < 0x30)
3256 gcap &= ~ICH6_GCAP_64OK;
3257 pci_dev_put(p_smbus);
3258 }
3259 }
Takashi Iwai09240cf2009-03-17 07:47:18 +01003260
Takashi Iwai9477c582011-05-25 09:11:37 +02003261 /* disable 64bit DMA address on some devices */
3262 if (chip->driver_caps & AZX_DCAPS_NO_64BIT) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003263 dev_dbg(card->dev, "Disabling 64bit DMA\n");
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003264 gcap &= ~ICH6_GCAP_64OK;
Takashi Iwai9477c582011-05-25 09:11:37 +02003265 }
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003266
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003267 /* disable buffer size rounding to 128-byte multiples if supported */
Takashi Iwai7bfe0592012-01-23 17:53:39 +01003268 if (align_buffer_size >= 0)
3269 chip->align_buffer_size = !!align_buffer_size;
3270 else {
3271 if (chip->driver_caps & AZX_DCAPS_BUFSIZE)
3272 chip->align_buffer_size = 0;
3273 else if (chip->driver_caps & AZX_DCAPS_ALIGN_BUFSIZE)
3274 chip->align_buffer_size = 1;
3275 else
3276 chip->align_buffer_size = 1;
3277 }
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003278
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003279 /* allow 64bit DMA address if supported by H/W */
Takashi Iwaib21fadb2009-05-28 12:26:15 +02003280 if ((gcap & ICH6_GCAP_64OK) && !pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
Yang Hongyange9304382009-04-13 14:40:14 -07003281 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003282 else {
Yang Hongyange9304382009-04-13 14:40:14 -07003283 pci_set_dma_mask(pci, DMA_BIT_MASK(32));
3284 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003285 }
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003286
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003287 /* read number of streams from GCAP register instead of using
3288 * hardcoded value
3289 */
3290 chip->capture_streams = (gcap >> 8) & 0x0f;
3291 chip->playback_streams = (gcap >> 12) & 0x0f;
3292 if (!chip->playback_streams && !chip->capture_streams) {
Tobin Davisbcd72002008-01-15 11:23:55 +01003293 /* gcap didn't give any info, switching to old method */
3294
3295 switch (chip->driver_type) {
3296 case AZX_DRIVER_ULI:
3297 chip->playback_streams = ULI_NUM_PLAYBACK;
3298 chip->capture_streams = ULI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003299 break;
3300 case AZX_DRIVER_ATIHDMI:
Andiry Xu1815b342011-12-14 16:10:27 +08003301 case AZX_DRIVER_ATIHDMI_NS:
Tobin Davisbcd72002008-01-15 11:23:55 +01003302 chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
3303 chip->capture_streams = ATIHDMI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003304 break;
Yang, Libinc4da29c2008-11-13 11:07:07 +01003305 case AZX_DRIVER_GENERIC:
Tobin Davisbcd72002008-01-15 11:23:55 +01003306 default:
3307 chip->playback_streams = ICH6_NUM_PLAYBACK;
3308 chip->capture_streams = ICH6_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003309 break;
3310 }
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003311 }
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003312 chip->capture_index_offset = 0;
3313 chip->playback_index_offset = chip->capture_streams;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003314 chip->num_streams = chip->playback_streams + chip->capture_streams;
Takashi Iwaid01ce992007-07-27 16:52:19 +02003315 chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
3316 GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003317 if (!chip->azx_dev) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003318 dev_err(card->dev, "cannot malloc azx_dev\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003319 return -ENOMEM;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003320 }
3321
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003322 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaieb49faa2013-03-15 09:19:11 +01003323 dsp_lock_init(&chip->azx_dev[i]);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003324 /* allocate memory for the BDL for each stream */
Dylan Reidb419b352014-02-28 15:41:20 -08003325 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
3326 BDL_SIZE,
3327 &chip->azx_dev[i].bdl);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003328 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003329 dev_err(card->dev, "cannot allocate BDL\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003330 return -ENOMEM;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02003333 /* allocate memory for the position buffer */
Dylan Reidb419b352014-02-28 15:41:20 -08003334 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
3335 chip->num_streams * 8, &chip->posbuf);
Takashi Iwaid01ce992007-07-27 16:52:19 +02003336 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003337 dev_err(card->dev, "cannot allocate posbuf\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003338 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 /* allocate CORB/RIRB */
Takashi Iwai81740862009-05-26 15:22:00 +02003341 err = azx_alloc_cmd_io(chip);
3342 if (err < 0)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
3345 /* initialize streams */
3346 azx_init_stream(chip);
3347
3348 /* initialize chip */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003349 azx_init_pci(chip);
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003350 azx_init_chip(chip, (probe_only[dev] & 2) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
3352 /* codec detection */
Pavel Machek927fc862006-08-31 17:03:43 +02003353 if (!chip->codec_mask) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003354 dev_err(card->dev, "no codecs found!\n");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003355 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
3357
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003358 strcpy(card->driver, "HDA-Intel");
Takashi Iwai18cb7102009-04-16 10:22:24 +02003359 strlcpy(card->shortname, driver_short_names[chip->driver_type],
3360 sizeof(card->shortname));
3361 snprintf(card->longname, sizeof(card->longname),
3362 "%s at 0x%lx irq %i",
3363 card->shortname, chip->addr, chip->irq);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
Takashi Iwaicb53c622007-08-10 17:21:45 +02003368static void power_down_all_codecs(struct azx *chip)
3369{
Takashi Iwai83012a72012-08-24 18:38:08 +02003370#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003371 /* The codecs were powered up in snd_hda_codec_new().
3372 * Now all initialization done, so turn them down if possible
3373 */
3374 struct hda_codec *codec;
3375 list_for_each_entry(codec, &chip->bus->codec_list, list) {
3376 snd_hda_power_down(codec);
3377 }
3378#endif
3379}
3380
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003381#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003382/* callback from request_firmware_nowait() */
3383static void azx_firmware_cb(const struct firmware *fw, void *context)
3384{
3385 struct snd_card *card = context;
3386 struct azx *chip = card->private_data;
3387 struct pci_dev *pci = chip->pci;
3388
3389 if (!fw) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003390 dev_err(card->dev, "Cannot load firmware, aborting\n");
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003391 goto error;
3392 }
3393
3394 chip->fw = fw;
3395 if (!chip->disabled) {
3396 /* continue probing */
3397 if (azx_probe_continue(chip))
3398 goto error;
3399 }
3400 return; /* OK */
3401
3402 error:
3403 snd_card_free(card);
3404 pci_set_drvdata(pci, NULL);
3405}
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003406#endif
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003407
Dylan Reid40830812014-02-28 15:41:13 -08003408/*
3409 * HDA controller ops.
3410 */
3411
3412/* PCI register access. */
3413static void pci_azx_writel(u32 value, u32 *addr)
3414{
3415 writel(value, addr);
3416}
3417
3418static u32 pci_azx_readl(u32 *addr)
3419{
3420 return readl(addr);
3421}
3422
3423static void pci_azx_writew(u16 value, u16 *addr)
3424{
3425 writew(value, addr);
3426}
3427
3428static u16 pci_azx_readw(u16 *addr)
3429{
3430 return readw(addr);
3431}
3432
3433static void pci_azx_writeb(u8 value, u8 *addr)
3434{
3435 writeb(value, addr);
3436}
3437
3438static u8 pci_azx_readb(u8 *addr)
3439{
3440 return readb(addr);
3441}
3442
Dylan Reidf46ea602014-02-28 15:41:16 -08003443static int disable_msi_reset_irq(struct azx *chip)
3444{
3445 int err;
3446
3447 free_irq(chip->irq, chip);
3448 chip->irq = -1;
3449 pci_disable_msi(chip->pci);
3450 chip->msi = 0;
3451 err = azx_acquire_irq(chip, 1);
3452 if (err < 0)
3453 return err;
3454
3455 return 0;
3456}
3457
Dylan Reidb419b352014-02-28 15:41:20 -08003458/* DMA page allocation helpers. */
3459static int dma_alloc_pages(struct azx *chip,
3460 int type,
3461 size_t size,
3462 struct snd_dma_buffer *buf)
3463{
3464 int err;
3465
3466 err = snd_dma_alloc_pages(type,
3467 chip->card->dev,
3468 size, buf);
3469 if (err < 0)
3470 return err;
3471 mark_pages_wc(chip, buf, true);
3472 return 0;
3473}
3474
3475static void dma_free_pages(struct azx *chip, struct snd_dma_buffer *buf)
3476{
3477 mark_pages_wc(chip, buf, false);
3478 snd_dma_free_pages(buf);
3479}
3480
3481static int substream_alloc_pages(struct azx *chip,
3482 struct snd_pcm_substream *substream,
3483 size_t size)
3484{
3485 struct azx_dev *azx_dev = get_azx_dev(substream);
3486 int ret;
3487
3488 mark_runtime_wc(chip, azx_dev, substream, false);
3489 azx_dev->bufsize = 0;
3490 azx_dev->period_bytes = 0;
3491 azx_dev->format_val = 0;
3492 ret = snd_pcm_lib_malloc_pages(substream, size);
3493 if (ret < 0)
3494 return ret;
3495 mark_runtime_wc(chip, azx_dev, substream, true);
3496 return 0;
3497}
3498
3499static int substream_free_pages(struct azx *chip,
3500 struct snd_pcm_substream *substream)
3501{
3502 struct azx_dev *azx_dev = get_azx_dev(substream);
3503 mark_runtime_wc(chip, azx_dev, substream, false);
3504 return snd_pcm_lib_free_pages(substream);
3505}
3506
Dylan Reid8769b2782014-02-28 15:41:21 -08003507static void pcm_mmap_prepare(struct snd_pcm_substream *substream,
3508 struct vm_area_struct *area)
3509{
3510#ifdef CONFIG_X86
3511 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
3512 struct azx *chip = apcm->chip;
3513 if (!azx_snoop(chip))
3514 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3515#endif
3516}
3517
Dylan Reid40830812014-02-28 15:41:13 -08003518static const struct hda_controller_ops pci_hda_ops = {
3519 .writel = pci_azx_writel,
3520 .readl = pci_azx_readl,
3521 .writew = pci_azx_writew,
3522 .readw = pci_azx_readw,
3523 .writeb = pci_azx_writeb,
3524 .readb = pci_azx_readb,
Dylan Reidf46ea602014-02-28 15:41:16 -08003525 .disable_msi_reset_irq = disable_msi_reset_irq,
Dylan Reidb419b352014-02-28 15:41:20 -08003526 .dma_alloc_pages = dma_alloc_pages,
3527 .dma_free_pages = dma_free_pages,
3528 .substream_alloc_pages = substream_alloc_pages,
3529 .substream_free_pages = substream_free_pages,
Dylan Reid8769b2782014-02-28 15:41:21 -08003530 .pcm_mmap_prepare = pcm_mmap_prepare,
Dylan Reid40830812014-02-28 15:41:13 -08003531};
3532
Bill Pembertone23e7a12012-12-06 12:35:10 -05003533static int azx_probe(struct pci_dev *pci,
3534 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003536 static int dev;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003537 struct snd_card *card;
3538 struct azx *chip;
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003539 bool schedule_probe;
Pavel Machek927fc862006-08-31 17:03:43 +02003540 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003542 if (dev >= SNDRV_CARDS)
3543 return -ENODEV;
3544 if (!enable[dev]) {
3545 dev++;
3546 return -ENOENT;
3547 }
3548
Takashi Iwai60c57722014-01-29 14:20:19 +01003549 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
3550 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003551 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003552 dev_err(&pci->dev, "Error creating card!\n");
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
3555
Dylan Reid40830812014-02-28 15:41:13 -08003556 err = azx_create(card, pci, dev, pci_id->driver_data,
3557 &pci_hda_ops, &chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003558 if (err < 0)
3559 goto out_free;
Takashi Iwai421a1252005-11-17 16:11:09 +01003560 card->private_data = chip;
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003561
3562 pci_set_drvdata(pci, card);
3563
3564 err = register_vga_switcheroo(chip);
3565 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003566 dev_err(card->dev, "Error registering VGA-switcheroo client\n");
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003567 goto out_free;
3568 }
3569
3570 if (check_hdmi_disabled(pci)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003571 dev_info(card->dev, "VGA controller is disabled\n");
3572 dev_info(card->dev, "Delaying initialization\n");
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003573 chip->disabled = true;
3574 }
3575
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003576 schedule_probe = !chip->disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Takashi Iwai4918cda2012-08-09 12:33:28 +02003578#ifdef CONFIG_SND_HDA_PATCH_LOADER
3579 if (patch[dev] && *patch[dev]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003580 dev_info(card->dev, "Applying patch firmware '%s'\n",
3581 patch[dev]);
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003582 err = request_firmware_nowait(THIS_MODULE, true, patch[dev],
3583 &pci->dev, GFP_KERNEL, card,
3584 azx_firmware_cb);
Takashi Iwai4918cda2012-08-09 12:33:28 +02003585 if (err < 0)
3586 goto out_free;
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003587 schedule_probe = false; /* continued in azx_firmware_cb() */
Takashi Iwai4918cda2012-08-09 12:33:28 +02003588 }
3589#endif /* CONFIG_SND_HDA_PATCH_LOADER */
3590
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003591#ifndef CONFIG_SND_HDA_I915
3592 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
Takashi Iwai4e76a882014-02-25 12:21:03 +01003593 dev_err(card->dev, "Haswell must build in CONFIG_SND_HDA_I915\n");
Wang Xingchao99a20082013-05-30 22:07:10 +08003594#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08003595
Takashi Iwaiaad730d2013-12-02 13:33:57 +01003596 if (schedule_probe)
3597 schedule_work(&chip->probe_work);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003598
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003599 dev++;
Takashi Iwai88d071f2013-12-02 11:12:28 +01003600 if (chip->disabled)
3601 complete_all(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003602 return 0;
3603
3604out_free:
3605 snd_card_free(card);
3606 return err;
3607}
3608
Dylan Reide62a42a2014-02-28 15:41:19 -08003609/* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
3610static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] = {
3611 [AZX_DRIVER_NVIDIA] = 8,
3612 [AZX_DRIVER_TERA] = 1,
3613};
3614
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01003615static int azx_probe_continue(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003616{
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003617 struct pci_dev *pci = chip->pci;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003618 int dev = chip->dev_index;
3619 int err;
3620
Wang Xingchao99a20082013-05-30 22:07:10 +08003621 /* Request power well for Haswell HDA controller and codec */
3622 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
David Henningssonc841ad22013-08-19 13:32:30 +02003623#ifdef CONFIG_SND_HDA_I915
Wang Xingchao99a20082013-05-30 22:07:10 +08003624 err = hda_i915_init();
3625 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003626 dev_err(chip->card->dev,
3627 "Error request power-well from i915\n");
Wang Xingchao99a20082013-05-30 22:07:10 +08003628 goto out_free;
3629 }
David Henningssonc841ad22013-08-19 13:32:30 +02003630#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08003631 hda_display_power(true);
3632 }
3633
Takashi Iwai5c906802013-05-30 22:07:09 +08003634 err = azx_first_init(chip);
3635 if (err < 0)
3636 goto out_free;
3637
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01003638#ifdef CONFIG_SND_HDA_INPUT_BEEP
3639 chip->beep_mode = beep_mode[dev];
3640#endif
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 /* create codec instances */
Dylan Reide62a42a2014-02-28 15:41:19 -08003643 err = azx_codec_create(chip, model[dev],
3644 azx_max_codecs[chip->driver_type],
3645 power_save_addr);
3646
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003647 if (err < 0)
3648 goto out_free;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003649#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai4918cda2012-08-09 12:33:28 +02003650 if (chip->fw) {
3651 err = snd_hda_load_patch(chip->bus, chip->fw->size,
3652 chip->fw->data);
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003653 if (err < 0)
3654 goto out_free;
Takashi Iwaie39ae852012-11-22 16:18:13 +01003655#ifndef CONFIG_PM
Takashi Iwai4918cda2012-08-09 12:33:28 +02003656 release_firmware(chip->fw); /* no longer needed */
3657 chip->fw = NULL;
Takashi Iwaie39ae852012-11-22 16:18:13 +01003658#endif
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003659 }
3660#endif
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003661 if ((probe_only[dev] & 1) == 0) {
Takashi Iwaia1e21c92009-06-17 09:33:52 +02003662 err = azx_codec_configure(chip);
3663 if (err < 0)
3664 goto out_free;
3665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
3667 /* create PCM streams */
Takashi Iwai176d5332008-07-30 15:01:44 +02003668 err = snd_hda_build_pcms(chip->bus);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003669 if (err < 0)
3670 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
3672 /* create mixer controls */
Takashi Iwaid01ce992007-07-27 16:52:19 +02003673 err = azx_mixer_create(chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003674 if (err < 0)
3675 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003677 err = snd_card_register(chip->card);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003678 if (err < 0)
3679 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Takashi Iwaicb53c622007-08-10 17:21:45 +02003681 chip->running = 1;
3682 power_down_all_codecs(chip);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01003683 azx_notifier_register(chip);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003684 azx_add_card_list(chip);
Dave Airlie246efa42013-07-29 15:19:29 +10003685 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME) || chip->use_vga_switcheroo)
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003686 pm_runtime_put_noidle(&pci->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003688out_free:
Takashi Iwai88d071f2013-12-02 11:12:28 +01003689 if (err < 0)
3690 chip->init_failed = 1;
3691 complete_all(&chip->probe_wait);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003692 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Bill Pembertone23e7a12012-12-06 12:35:10 -05003695static void azx_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
Takashi Iwai91219472012-04-26 12:13:25 +02003697 struct snd_card *card = pci_get_drvdata(pci);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003698
Takashi Iwai91219472012-04-26 12:13:25 +02003699 if (card)
3700 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
3703/* PCI IDs */
Alexey Dobriyancebe41d2010-02-06 00:21:03 +02003704static DEFINE_PCI_DEVICE_TABLE(azx_ids) = {
Seth Heasleyd2f2fcd2010-01-12 17:03:35 -08003705 /* CPT */
Takashi Iwai9477c582011-05-25 09:11:37 +02003706 { PCI_DEVICE(0x8086, 0x1c20),
Takashi Iwaid7dab4d2013-01-08 13:51:30 +01003707 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
Seth Heasleycea310e2010-09-10 16:29:56 -07003708 /* PBG */
Takashi Iwai9477c582011-05-25 09:11:37 +02003709 { PCI_DEVICE(0x8086, 0x1d20),
Takashi Iwaid7dab4d2013-01-08 13:51:30 +01003710 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
Seth Heasleyd2edeb72011-04-20 10:59:57 -07003711 /* Panther Point */
Takashi Iwai9477c582011-05-25 09:11:37 +02003712 { PCI_DEVICE(0x8086, 0x1e20),
Takashi Iwaib1920c22013-11-22 12:43:25 +01003713 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
Seth Heasley8bc039a2012-01-23 16:24:31 -08003714 /* Lynx Point */
3715 { PCI_DEVICE(0x8086, 0x8c20),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003716 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston884b0882013-02-08 17:29:40 -08003717 /* Wellsburg */
3718 { PCI_DEVICE(0x8086, 0x8d20),
3719 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
3720 { PCI_DEVICE(0x8086, 0x8d21),
3721 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston144dad92012-08-09 09:38:59 -07003722 /* Lynx Point-LP */
3723 { PCI_DEVICE(0x8086, 0x9c20),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003724 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston144dad92012-08-09 09:38:59 -07003725 /* Lynx Point-LP */
3726 { PCI_DEVICE(0x8086, 0x9c21),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003727 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston4eeca492013-11-04 09:27:45 -08003728 /* Wildcat Point-LP */
3729 { PCI_DEVICE(0x8086, 0x9ca0),
3730 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
Wang Xingchaoe926f2c2012-06-13 10:23:51 +08003731 /* Haswell */
Wang Xingchao4a7c5162013-02-01 22:42:19 +08003732 { PCI_DEVICE(0x8086, 0x0a0c),
Takashi Iwaifab12852013-11-05 17:54:05 +01003733 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Wang Xingchaoe926f2c2012-06-13 10:23:51 +08003734 { PCI_DEVICE(0x8086, 0x0c0c),
Takashi Iwaifab12852013-11-05 17:54:05 +01003735 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Wang Xingchaod279fae2012-09-17 13:10:23 +08003736 { PCI_DEVICE(0x8086, 0x0d0c),
Takashi Iwaifab12852013-11-05 17:54:05 +01003737 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Mengdong Lin862d7612014-01-08 15:55:14 -05003738 /* Broadwell */
3739 { PCI_DEVICE(0x8086, 0x160c),
3740 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
Pierre-Louis Bossart99df18b2012-09-21 18:39:07 -05003741 /* 5 Series/3400 */
3742 { PCI_DEVICE(0x8086, 0x3b56),
Takashi Iwai2c1350f2013-02-14 09:44:55 +01003743 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
Takashi Iwaif748abc2013-01-29 10:12:23 +01003744 /* Poulsbo */
Takashi Iwai9477c582011-05-25 09:11:37 +02003745 { PCI_DEVICE(0x8086, 0x811b),
Takashi Iwaif748abc2013-01-29 10:12:23 +01003746 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
3747 /* Oaktrail */
Li Peng09904b92011-12-28 15:17:26 +00003748 { PCI_DEVICE(0x8086, 0x080a),
Takashi Iwaif748abc2013-01-29 10:12:23 +01003749 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
Chew, Chiau Eee44007e2013-05-16 15:36:12 +08003750 /* BayTrail */
3751 { PCI_DEVICE(0x8086, 0x0f04),
3752 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
David Henningsson645e9032011-12-14 15:52:30 +08003753 /* ICH */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003754 { PCI_DEVICE(0x8086, 0x2668),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003755 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3756 AZX_DCAPS_BUFSIZE }, /* ICH6 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003757 { PCI_DEVICE(0x8086, 0x27d8),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003758 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3759 AZX_DCAPS_BUFSIZE }, /* ICH7 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003760 { PCI_DEVICE(0x8086, 0x269a),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003761 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3762 AZX_DCAPS_BUFSIZE }, /* ESB2 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003763 { PCI_DEVICE(0x8086, 0x284b),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003764 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3765 AZX_DCAPS_BUFSIZE }, /* ICH8 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003766 { PCI_DEVICE(0x8086, 0x293e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003767 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3768 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003769 { PCI_DEVICE(0x8086, 0x293f),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003770 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3771 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003772 { PCI_DEVICE(0x8086, 0x3a3e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003773 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3774 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003775 { PCI_DEVICE(0x8086, 0x3a6e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003776 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3777 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwaib6864532010-09-15 10:17:26 +02003778 /* Generic Intel */
3779 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
3780 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3781 .class_mask = 0xffffff,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003782 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_BUFSIZE },
Takashi Iwai9477c582011-05-25 09:11:37 +02003783 /* ATI SB 450/600/700/800/900 */
3784 { PCI_DEVICE(0x1002, 0x437b),
3785 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
3786 { PCI_DEVICE(0x1002, 0x4383),
3787 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
3788 /* AMD Hudson */
3789 { PCI_DEVICE(0x1022, 0x780d),
3790 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB },
Takashi Iwai87218e92008-02-21 08:13:11 +01003791 /* ATI HDMI */
Takashi Iwai9477c582011-05-25 09:11:37 +02003792 { PCI_DEVICE(0x1002, 0x793b),
3793 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3794 { PCI_DEVICE(0x1002, 0x7919),
3795 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3796 { PCI_DEVICE(0x1002, 0x960f),
3797 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3798 { PCI_DEVICE(0x1002, 0x970f),
3799 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3800 { PCI_DEVICE(0x1002, 0xaa00),
3801 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3802 { PCI_DEVICE(0x1002, 0xaa08),
3803 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3804 { PCI_DEVICE(0x1002, 0xaa10),
3805 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3806 { PCI_DEVICE(0x1002, 0xaa18),
3807 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3808 { PCI_DEVICE(0x1002, 0xaa20),
3809 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3810 { PCI_DEVICE(0x1002, 0xaa28),
3811 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3812 { PCI_DEVICE(0x1002, 0xaa30),
3813 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3814 { PCI_DEVICE(0x1002, 0xaa38),
3815 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3816 { PCI_DEVICE(0x1002, 0xaa40),
3817 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3818 { PCI_DEVICE(0x1002, 0xaa48),
3819 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
Clemens Ladischbbaa0d62013-11-05 09:27:10 +01003820 { PCI_DEVICE(0x1002, 0xaa50),
3821 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3822 { PCI_DEVICE(0x1002, 0xaa58),
3823 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3824 { PCI_DEVICE(0x1002, 0xaa60),
3825 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3826 { PCI_DEVICE(0x1002, 0xaa68),
3827 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3828 { PCI_DEVICE(0x1002, 0xaa80),
3829 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3830 { PCI_DEVICE(0x1002, 0xaa88),
3831 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3832 { PCI_DEVICE(0x1002, 0xaa90),
3833 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
3834 { PCI_DEVICE(0x1002, 0xaa98),
3835 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Xu1815b342011-12-14 16:10:27 +08003836 { PCI_DEVICE(0x1002, 0x9902),
3837 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3838 { PCI_DEVICE(0x1002, 0xaaa0),
3839 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3840 { PCI_DEVICE(0x1002, 0xaaa8),
3841 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
3842 { PCI_DEVICE(0x1002, 0xaab0),
3843 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
Takashi Iwai87218e92008-02-21 08:13:11 +01003844 /* VIA VT8251/VT8237A */
Takashi Iwai9477c582011-05-25 09:11:37 +02003845 { PCI_DEVICE(0x1106, 0x3288),
3846 .driver_data = AZX_DRIVER_VIA | AZX_DCAPS_POSFIX_VIA },
Annie Liu754fdff2012-06-08 19:18:39 +08003847 /* VIA GFX VT7122/VX900 */
3848 { PCI_DEVICE(0x1106, 0x9170), .driver_data = AZX_DRIVER_GENERIC },
3849 /* VIA GFX VT6122/VX11 */
3850 { PCI_DEVICE(0x1106, 0x9140), .driver_data = AZX_DRIVER_GENERIC },
Takashi Iwai87218e92008-02-21 08:13:11 +01003851 /* SIS966 */
3852 { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
3853 /* ULI M5461 */
3854 { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
3855 /* NVIDIA MCP */
Takashi Iwai0c2fd1bf42009-12-18 16:41:39 +01003856 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
3857 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3858 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003859 .driver_data = AZX_DRIVER_NVIDIA | AZX_DCAPS_PRESET_NVIDIA },
Kailang Yangf2690022008-05-27 11:44:55 +02003860 /* Teradici */
Takashi Iwai9477c582011-05-25 09:11:37 +02003861 { PCI_DEVICE(0x6549, 0x1200),
3862 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
Lars R. Damerowf0b3da92012-11-02 13:10:39 -07003863 { PCI_DEVICE(0x6549, 0x2200),
3864 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
Takashi Iwai4e01f542009-04-16 08:53:34 +02003865 /* Creative X-Fi (CA0110-IBG) */
Takashi Iwaif2a8eca2012-06-11 15:51:54 +02003866 /* CTHDA chips */
3867 { PCI_DEVICE(0x1102, 0x0010),
3868 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
3869 { PCI_DEVICE(0x1102, 0x0012),
3870 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +01003871#if !IS_ENABLED(CONFIG_SND_CTXFI)
Takashi Iwai313f6e22009-05-18 12:40:52 +02003872 /* the following entry conflicts with snd-ctxfi driver,
3873 * as ctxfi driver mutates from HD-audio to native mode with
3874 * a special command sequence.
3875 */
Takashi Iwai4e01f542009-04-16 08:53:34 +02003876 { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
3877 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3878 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003879 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01003880 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02003881#else
3882 /* this entry seems still valid -- i.e. without emu20kx chip */
Takashi Iwai9477c582011-05-25 09:11:37 +02003883 { PCI_DEVICE(0x1102, 0x0009),
3884 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01003885 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02003886#endif
Otavio Salvadore35d4b12010-09-26 23:35:06 -03003887 /* Vortex86MX */
3888 { PCI_DEVICE(0x17f3, 0x3010), .driver_data = AZX_DRIVER_GENERIC },
Bankim Bhavsar0f0714c52011-01-17 15:23:21 +01003889 /* VMware HDAudio */
3890 { PCI_DEVICE(0x15ad, 0x1977), .driver_data = AZX_DRIVER_GENERIC },
Andiry Brienza9176b672009-07-17 11:32:32 +08003891 /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
Yang, Libinc4da29c2008-11-13 11:07:07 +01003892 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
3893 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3894 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003895 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Brienza9176b672009-07-17 11:32:32 +08003896 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
3897 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
3898 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02003899 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 { 0, }
3901};
3902MODULE_DEVICE_TABLE(pci, azx_ids);
3903
3904/* pci_driver definition */
Takashi Iwaie9f66d92012-04-24 12:25:00 +02003905static struct pci_driver azx_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02003906 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 .id_table = azx_ids,
3908 .probe = azx_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05003909 .remove = azx_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003910 .driver = {
3911 .pm = AZX_PM_OPS,
3912 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913};
3914
Takashi Iwaie9f66d92012-04-24 12:25:00 +02003915module_pci_driver(azx_driver);