blob: bf5e58ec1efe140e124f162a89f557424bde2a58 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67
Takashi Iwai5aba4f82008-01-07 15:16:37 +010068static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
69static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
Rusty Russella67ff6a2011-12-15 13:49:36 +103070static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Takashi Iwai5aba4f82008-01-07 15:16:37 +010071static char *model[SNDRV_CARDS];
Takashi Iwai1dac6692012-09-13 14:59:47 +020072static int position_fix[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +020073static int bdl_pos_adj[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
Takashi Iwai5aba4f82008-01-07 15:16:37 +010074static int probe_mask[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
Takashi Iwaid4d9cd032008-12-19 15:19:11 +010075static int probe_only[SNDRV_CARDS];
David Henningsson26a6cb62012-10-09 15:04:21 +020076static int jackpoll_ms[SNDRV_CARDS];
Rusty Russella67ff6a2011-12-15 13:49:36 +103077static bool single_cmd;
Takashi Iwai716238552009-09-28 13:14:04 +020078static int enable_msi = -1;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +020079#ifdef CONFIG_SND_HDA_PATCH_LOADER
80static char *patch[SNDRV_CARDS];
81#endif
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +010082#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwai0920c9b2012-07-03 16:58:48 +020083static bool beep_mode[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] =
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +010084 CONFIG_SND_HDA_INPUT_BEEP_MODE};
85#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Takashi Iwai5aba4f82008-01-07 15:16:37 +010087module_param_array(index, int, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010089module_param_array(id, charp, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010091module_param_array(enable, bool, NULL, 0444);
92MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
93module_param_array(model, charp, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094MODULE_PARM_DESC(model, "Use the given board model.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +010095module_param_array(position_fix, int, NULL, 0444);
David Henningsson4cb36312010-09-30 10:12:50 +020096MODULE_PARM_DESC(position_fix, "DMA pointer read method."
Takashi Iwai1dac6692012-09-13 14:59:47 +020097 "(-1 = system default, 0 = auto, 1 = LPIB, 2 = POSBUF, 3 = VIACOMBO, 4 = COMBO).");
Takashi Iwai555e2192008-06-10 17:53:34 +020098module_param_array(bdl_pos_adj, int, NULL, 0644);
99MODULE_PARM_DESC(bdl_pos_adj, "BDL position adjustment offset.");
Takashi Iwai5aba4f82008-01-07 15:16:37 +0100100module_param_array(probe_mask, int, NULL, 0444);
Takashi Iwai606ad752005-11-24 16:03:40 +0100101MODULE_PARM_DESC(probe_mask, "Bitmask to probe codecs (default = -1).");
Jaroslav Kysela079e6832010-03-26 11:16:59 +0100102module_param_array(probe_only, int, NULL, 0444);
Takashi Iwaid4d9cd032008-12-19 15:19:11 +0100103MODULE_PARM_DESC(probe_only, "Only probing and no codec initialization.");
David Henningsson26a6cb62012-10-09 15:04:21 +0200104module_param_array(jackpoll_ms, int, NULL, 0444);
105MODULE_PARM_DESC(jackpoll_ms, "Ms between polling for jack events (default = 0, using unsol events only)");
Takashi Iwai27346162006-01-12 18:28:44 +0100106module_param(single_cmd, bool, 0444);
Takashi Iwaid01ce992007-07-27 16:52:19 +0200107MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
108 "(for debugging only).");
Takashi Iwaiac9ef6c2012-01-20 12:08:44 +0100109module_param(enable_msi, bint, 0444);
Takashi Iwai134a11f2006-11-10 12:08:37 +0100110MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200111#ifdef CONFIG_SND_HDA_PATCH_LOADER
112module_param_array(patch, charp, NULL, 0444);
113MODULE_PARM_DESC(patch, "Patch file for Intel HD audio interface.");
114#endif
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100115#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwai0920c9b2012-07-03 16:58:48 +0200116module_param_array(beep_mode, bool, NULL, 0444);
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100117MODULE_PARM_DESC(beep_mode, "Select HDA Beep registration mode "
Takashi Iwai0920c9b2012-07-03 16:58:48 +0200118 "(0=off, 1=on) (default=1).");
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100119#endif
Takashi Iwai606ad752005-11-24 16:03:40 +0100120
Takashi Iwai83012a72012-08-24 18:38:08 +0200121#ifdef CONFIG_PM
Takashi Iwai65fcd412012-08-14 17:13:32 +0200122static int param_set_xint(const char *val, const struct kernel_param *kp);
123static struct kernel_param_ops param_ops_xint = {
124 .set = param_set_xint,
125 .get = param_get_int,
126};
127#define param_check_xint param_check_int
128
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100129static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
Takashi Iwai65fcd412012-08-14 17:13:32 +0200130module_param(power_save, xint, 0644);
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100131MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
132 "(in second, 0 = disable).");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Takashi Iwaidee1b662007-08-13 16:10:30 +0200134/* reset the HD-audio controller in power save mode.
135 * this may give more power-saving, but will take longer time to
136 * wake up.
137 */
Takashi Iwai8fc24422013-04-04 15:35:24 +0200138static bool power_save_controller = 1;
139module_param(power_save_controller, bool, 0644);
Takashi Iwaidee1b662007-08-13 16:10:30 +0200140MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
Takashi Iwai83012a72012-08-24 18:38:08 +0200141#endif /* CONFIG_PM */
Takashi Iwaidee1b662007-08-13 16:10:30 +0200142
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100143static int align_buffer_size = -1;
144module_param(align_buffer_size, bint, 0644);
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -0500145MODULE_PARM_DESC(align_buffer_size,
146 "Force buffer and period sizes to be multiple of 128 bytes.");
147
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200148#ifdef CONFIG_X86
149static bool hda_snoop = true;
150module_param_named(snoop, hda_snoop, bool, 0444);
151MODULE_PARM_DESC(snoop, "Enable/disable snooping");
152#define azx_snoop(chip) (chip)->snoop
153#else
154#define hda_snoop true
155#define azx_snoop(chip) true
156#endif
157
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159MODULE_LICENSE("GPL");
160MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
161 "{Intel, ICH6M},"
Jason Gaston2f1b3812005-05-01 08:58:50 -0700162 "{Intel, ICH7},"
Frederick Lif5d40b32005-05-12 14:55:20 +0200163 "{Intel, ESB2},"
Jason Gastond2981392006-01-10 11:07:37 +0100164 "{Intel, ICH8},"
Jason Gastonf9cc8a82006-11-22 11:53:52 +0100165 "{Intel, ICH9},"
Jason Gastonc34f5a02008-01-29 12:38:49 +0100166 "{Intel, ICH10},"
Seth Heasleyb29c2362008-08-08 15:56:39 -0700167 "{Intel, PCH},"
Seth Heasleyd2f2fcd2010-01-12 17:03:35 -0800168 "{Intel, CPT},"
Seth Heasleyd2edeb72011-04-20 10:59:57 -0700169 "{Intel, PPT},"
Seth Heasley8bc039a2012-01-23 16:24:31 -0800170 "{Intel, LPT},"
James Ralston144dad92012-08-09 09:38:59 -0700171 "{Intel, LPT_LP},"
Wang Xingchaoe926f2c2012-06-13 10:23:51 +0800172 "{Intel, HPT},"
Seth Heasleycea310e2010-09-10 16:29:56 -0700173 "{Intel, PBG},"
Tobin Davis4979bca2008-01-30 08:13:55 +0100174 "{Intel, SCH},"
Takashi Iwaifc20a562005-05-12 15:00:41 +0200175 "{ATI, SB450},"
Felix Kuehling89be83f2006-03-31 12:33:59 +0200176 "{ATI, SB600},"
Felix Kuehling778b6e12006-05-17 11:22:21 +0200177 "{ATI, RS600},"
Felix Kuehling5b15c952006-10-16 12:49:47 +0200178 "{ATI, RS690},"
Wolke Liue6db1112007-04-27 12:20:57 +0200179 "{ATI, RS780},"
180 "{ATI, R600},"
Herton Ronaldo Krzesinski2797f722007-11-05 18:21:56 +0100181 "{ATI, RV630},"
182 "{ATI, RV610},"
Wolke Liu27da1832007-11-16 11:06:30 +0100183 "{ATI, RV670},"
184 "{ATI, RV635},"
185 "{ATI, RV620},"
186 "{ATI, RV770},"
Takashi Iwaifc20a562005-05-12 15:00:41 +0200187 "{VIA, VT8251},"
Takashi Iwai47672312005-08-12 16:44:04 +0200188 "{VIA, VT8237A},"
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200189 "{SiS, SIS966},"
190 "{ULI, M5461}}");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191MODULE_DESCRIPTION("Intel HDA driver");
192
Takashi Iwai4abc1cc2009-05-19 12:16:46 +0200193#ifdef CONFIG_SND_VERBOSE_PRINTK
194#define SFX /* nop */
195#else
Daniel J Blueman445a51b2012-12-05 23:04:21 +0800196#define SFX "hda-intel "
Takashi Iwai4abc1cc2009-05-19 12:16:46 +0200197#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +0200198
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200199#if defined(CONFIG_PM) && defined(CONFIG_VGA_SWITCHEROO)
200#ifdef CONFIG_SND_HDA_CODEC_HDMI
201#define SUPPORT_VGA_SWITCHEROO
202#endif
203#endif
204
205
Takashi Iwaicb53c622007-08-10 17:21:45 +0200206/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * registers
208 */
209#define ICH6_REG_GCAP 0x00
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200210#define ICH6_GCAP_64OK (1 << 0) /* 64bit address support */
211#define ICH6_GCAP_NSDO (3 << 1) /* # of serial data out signals */
212#define ICH6_GCAP_BSS (31 << 3) /* # of bidirectional streams */
213#define ICH6_GCAP_ISS (15 << 8) /* # of input streams */
214#define ICH6_GCAP_OSS (15 << 12) /* # of output streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#define ICH6_REG_VMIN 0x02
216#define ICH6_REG_VMAJ 0x03
217#define ICH6_REG_OUTPAY 0x04
218#define ICH6_REG_INPAY 0x06
219#define ICH6_REG_GCTL 0x08
Takashi Iwai8a933ec2009-05-31 09:28:12 +0200220#define ICH6_GCTL_RESET (1 << 0) /* controller reset */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200221#define ICH6_GCTL_FCNTRL (1 << 1) /* flush control */
222#define ICH6_GCTL_UNSOL (1 << 8) /* accept unsol. response enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#define ICH6_REG_WAKEEN 0x0c
224#define ICH6_REG_STATESTS 0x0e
225#define ICH6_REG_GSTS 0x10
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200226#define ICH6_GSTS_FSTS (1 << 1) /* flush status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#define ICH6_REG_INTCTL 0x20
228#define ICH6_REG_INTSTS 0x24
Jaroslav Kyselae5463722010-05-11 10:21:46 +0200229#define ICH6_REG_WALLCLK 0x30 /* 24Mhz source */
Takashi Iwai8b0bd222011-06-10 14:56:26 +0200230#define ICH6_REG_OLD_SSYNC 0x34 /* SSYNC for old ICH */
231#define ICH6_REG_SSYNC 0x38
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#define ICH6_REG_CORBLBASE 0x40
233#define ICH6_REG_CORBUBASE 0x44
234#define ICH6_REG_CORBWP 0x48
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200235#define ICH6_REG_CORBRP 0x4a
236#define ICH6_CORBRP_RST (1 << 15) /* read pointer reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#define ICH6_REG_CORBCTL 0x4c
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200238#define ICH6_CORBCTL_RUN (1 << 1) /* enable DMA */
239#define ICH6_CORBCTL_CMEIE (1 << 0) /* enable memory error irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#define ICH6_REG_CORBSTS 0x4d
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200241#define ICH6_CORBSTS_CMEI (1 << 0) /* memory error indication */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#define ICH6_REG_CORBSIZE 0x4e
243
244#define ICH6_REG_RIRBLBASE 0x50
245#define ICH6_REG_RIRBUBASE 0x54
246#define ICH6_REG_RIRBWP 0x58
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200247#define ICH6_RIRBWP_RST (1 << 15) /* write pointer reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#define ICH6_REG_RINTCNT 0x5a
249#define ICH6_REG_RIRBCTL 0x5c
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200250#define ICH6_RBCTL_IRQ_EN (1 << 0) /* enable IRQ */
251#define ICH6_RBCTL_DMA_EN (1 << 1) /* enable DMA */
252#define ICH6_RBCTL_OVERRUN_EN (1 << 2) /* enable overrun irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#define ICH6_REG_RIRBSTS 0x5d
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200254#define ICH6_RBSTS_IRQ (1 << 0) /* response irq */
255#define ICH6_RBSTS_OVERRUN (1 << 2) /* overrun irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#define ICH6_REG_RIRBSIZE 0x5e
257
258#define ICH6_REG_IC 0x60
259#define ICH6_REG_IR 0x64
260#define ICH6_REG_IRS 0x68
261#define ICH6_IRS_VALID (1<<1)
262#define ICH6_IRS_BUSY (1<<0)
263
264#define ICH6_REG_DPLBASE 0x70
265#define ICH6_REG_DPUBASE 0x74
266#define ICH6_DPLBASE_ENABLE 0x1 /* Enable position buffer */
267
268/* SD offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
269enum { SDI0, SDI1, SDI2, SDI3, SDO0, SDO1, SDO2, SDO3 };
270
271/* stream register offsets from stream base */
272#define ICH6_REG_SD_CTL 0x00
273#define ICH6_REG_SD_STS 0x03
274#define ICH6_REG_SD_LPIB 0x04
275#define ICH6_REG_SD_CBL 0x08
276#define ICH6_REG_SD_LVI 0x0c
277#define ICH6_REG_SD_FIFOW 0x0e
278#define ICH6_REG_SD_FIFOSIZE 0x10
279#define ICH6_REG_SD_FORMAT 0x12
280#define ICH6_REG_SD_BDLPL 0x18
281#define ICH6_REG_SD_BDLPU 0x1c
282
283/* PCI space */
284#define ICH6_PCIREG_TCSEL 0x44
285
286/*
287 * other constants
288 */
289
290/* max number of SDs */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200291/* ICH, ATI and VIA have 4 playback and 4 capture */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200292#define ICH6_NUM_CAPTURE 4
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200293#define ICH6_NUM_PLAYBACK 4
294
295/* ULI has 6 playback and 5 capture */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200296#define ULI_NUM_CAPTURE 5
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200297#define ULI_NUM_PLAYBACK 6
298
Felix Kuehling778b6e12006-05-17 11:22:21 +0200299/* ATI HDMI has 1 playback and 0 capture */
Felix Kuehling778b6e12006-05-17 11:22:21 +0200300#define ATIHDMI_NUM_CAPTURE 0
Felix Kuehling778b6e12006-05-17 11:22:21 +0200301#define ATIHDMI_NUM_PLAYBACK 1
302
Kailang Yangf2690022008-05-27 11:44:55 +0200303/* TERA has 4 playback and 3 capture */
304#define TERA_NUM_CAPTURE 3
305#define TERA_NUM_PLAYBACK 4
306
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200307/* this number is statically defined for simplicity */
308#define MAX_AZX_DEV 16
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/* max number of fragments - we may use more if allocating more pages for BDL */
Takashi Iwai4ce107b2008-02-06 14:50:19 +0100311#define BDL_SIZE 4096
312#define AZX_MAX_BDL_ENTRIES (BDL_SIZE / 16)
313#define AZX_MAX_FRAG 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* max buffer size - no h/w limit, you can increase as you like */
315#define AZX_MAX_BUF_SIZE (1024*1024*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/* RIRB int mask: overrun[2], response[0] */
318#define RIRB_INT_RESPONSE 0x01
319#define RIRB_INT_OVERRUN 0x04
320#define RIRB_INT_MASK 0x05
321
Takashi Iwai2f5983f2008-09-03 16:00:44 +0200322/* STATESTS int mask: S3,SD2,SD1,SD0 */
Wei Ni7445dfc2010-03-03 15:05:53 +0800323#define AZX_MAX_CODECS 8
324#define AZX_DEFAULT_CODECS 4
Wu Fengguangdeadff12009-08-01 18:45:16 +0800325#define STATESTS_INT_MASK ((1 << AZX_MAX_CODECS) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327/* SD_CTL bits */
328#define SD_CTL_STREAM_RESET 0x01 /* stream reset bit */
329#define SD_CTL_DMA_START 0x02 /* stream DMA start bit */
Takashi Iwai850f0e52008-03-18 17:11:05 +0100330#define SD_CTL_STRIPE (3 << 16) /* stripe control */
331#define SD_CTL_TRAFFIC_PRIO (1 << 18) /* traffic priority */
332#define SD_CTL_DIR (1 << 19) /* bi-directional stream */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#define SD_CTL_STREAM_TAG_MASK (0xf << 20)
334#define SD_CTL_STREAM_TAG_SHIFT 20
335
336/* SD_CTL and SD_STS */
337#define SD_INT_DESC_ERR 0x10 /* descriptor error interrupt */
338#define SD_INT_FIFO_ERR 0x08 /* FIFO error interrupt */
339#define SD_INT_COMPLETE 0x04 /* completion interrupt */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200340#define SD_INT_MASK (SD_INT_DESC_ERR|SD_INT_FIFO_ERR|\
341 SD_INT_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343/* SD_STS */
344#define SD_STS_FIFO_READY 0x20 /* FIFO ready */
345
346/* INTCTL and INTSTS */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200347#define ICH6_INT_ALL_STREAM 0xff /* all stream interrupts */
348#define ICH6_INT_CTRL_EN 0x40000000 /* controller interrupt enable bit */
349#define ICH6_INT_GLOBAL_EN 0x80000000 /* global interrupt enable bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/* below are so far hardcoded - should read registers in future */
352#define ICH6_MAX_CORB_ENTRIES 256
353#define ICH6_MAX_RIRB_ENTRIES 256
354
Takashi Iwaic74db862005-05-12 14:26:27 +0200355/* position fix mode */
356enum {
Takashi Iwai0be3b5d2005-09-05 17:11:40 +0200357 POS_FIX_AUTO,
Takashi Iwaid2e1c972008-06-10 17:53:34 +0200358 POS_FIX_LPIB,
Takashi Iwai0be3b5d2005-09-05 17:11:40 +0200359 POS_FIX_POSBUF,
David Henningsson4cb36312010-09-30 10:12:50 +0200360 POS_FIX_VIACOMBO,
Takashi Iwaia6f2fd52012-02-28 11:58:40 +0100361 POS_FIX_COMBO,
Takashi Iwaic74db862005-05-12 14:26:27 +0200362};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Frederick Lif5d40b32005-05-12 14:55:20 +0200364/* Defines for ATI HD Audio support in SB450 south bridge */
Frederick Lif5d40b32005-05-12 14:55:20 +0200365#define ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR 0x42
366#define ATI_SB450_HDAUDIO_ENABLE_SNOOP 0x02
367
Vinod Gda3fca22005-09-13 18:49:12 +0200368/* Defines for Nvidia HDA support */
369#define NVIDIA_HDA_TRANSREG_ADDR 0x4e
370#define NVIDIA_HDA_ENABLE_COHBITS 0x0f
Peer Chen320dcc32008-08-20 16:43:24 -0700371#define NVIDIA_HDA_ISTRM_COH 0x4d
372#define NVIDIA_HDA_OSTRM_COH 0x4c
373#define NVIDIA_HDA_ENABLE_COHBIT 0x01
Frederick Lif5d40b32005-05-12 14:55:20 +0200374
Takashi Iwai90a5ad52008-02-22 18:36:22 +0100375/* Defines for Intel SCH HDA snoop control */
376#define INTEL_SCH_HDA_DEVC 0x78
377#define INTEL_SCH_HDA_DEVC_NOSNOOP (0x1<<11)
378
Joseph Chan0e153472008-08-26 14:38:03 +0200379/* Define IN stream 0 FIFO size offset in VIA controller */
380#define VIA_IN_STREAM0_FIFO_SIZE_OFFSET 0x90
381/* Define VIA HD Audio Device ID*/
382#define VIA_HDAC_DEVICE_ID 0x3288
383
Yang, Libinc4da29c2008-11-13 11:07:07 +0100384/* HD Audio class code */
385#define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
Takashi Iwai90a5ad52008-02-22 18:36:22 +0100386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
389
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100390struct azx_dev {
Takashi Iwai4ce107b2008-02-06 14:50:19 +0100391 struct snd_dma_buffer bdl; /* BDL buffer */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200392 u32 *posbuf; /* position buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Takashi Iwaid01ce992007-07-27 16:52:19 +0200394 unsigned int bufsize; /* size of the play buffer in bytes */
Takashi Iwai9ad593f2008-05-16 12:34:47 +0200395 unsigned int period_bytes; /* size of the period in bytes */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200396 unsigned int frags; /* number for period in the play buffer */
397 unsigned int fifo_size; /* FIFO size */
Jaroslav Kyselae5463722010-05-11 10:21:46 +0200398 unsigned long start_wallclk; /* start + minimum wallclk */
399 unsigned long period_wallclk; /* wallclk for period */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Takashi Iwaid01ce992007-07-27 16:52:19 +0200401 void __iomem *sd_addr; /* stream descriptor pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Takashi Iwaid01ce992007-07-27 16:52:19 +0200403 u32 sd_int_sta_mask; /* stream int status mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* pcm support */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200406 struct snd_pcm_substream *substream; /* assigned substream,
407 * set in PCM open
408 */
409 unsigned int format_val; /* format value to be set in the
410 * controller and the codec
411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 unsigned char stream_tag; /* assigned stream */
413 unsigned char index; /* stream index */
Takashi Iwaid5cf9912011-10-06 10:07:58 +0200414 int assigned_key; /* last device# key assigned to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Pavel Machek927fc862006-08-31 17:03:43 +0200416 unsigned int opened :1;
417 unsigned int running :1;
Takashi Iwai675f25d2008-06-10 17:53:20 +0200418 unsigned int irq_pending :1;
Takashi Iwaieb49faa2013-03-15 09:19:11 +0100419 unsigned int prepared:1;
420 unsigned int locked:1;
Joseph Chan0e153472008-08-26 14:38:03 +0200421 /*
422 * For VIA:
423 * A flag to ensure DMA position is 0
424 * when link position is not greater than FIFO size
425 */
426 unsigned int insufficient :1;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200427 unsigned int wc_marked:1;
Takashi Iwai915bf292012-09-11 15:19:10 +0200428 unsigned int no_period_wakeup:1;
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -0500429
430 struct timecounter azx_tc;
431 struct cyclecounter azx_cc;
Takashi Iwaieb49faa2013-03-15 09:19:11 +0100432
433#ifdef CONFIG_SND_HDA_DSP_LOADER
434 struct mutex dsp_mutex;
435#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436};
437
Takashi Iwaieb49faa2013-03-15 09:19:11 +0100438/* DSP lock helpers */
439#ifdef CONFIG_SND_HDA_DSP_LOADER
440#define dsp_lock_init(dev) mutex_init(&(dev)->dsp_mutex)
441#define dsp_lock(dev) mutex_lock(&(dev)->dsp_mutex)
442#define dsp_unlock(dev) mutex_unlock(&(dev)->dsp_mutex)
443#define dsp_is_locked(dev) ((dev)->locked)
444#else
445#define dsp_lock_init(dev) do {} while (0)
446#define dsp_lock(dev) do {} while (0)
447#define dsp_unlock(dev) do {} while (0)
448#define dsp_is_locked(dev) 0
449#endif
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/* CORB/RIRB */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100452struct azx_rb {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 u32 *buf; /* CORB/RIRB buffer
454 * Each CORB entry is 4byte, RIRB is 8byte
455 */
456 dma_addr_t addr; /* physical address of CORB/RIRB buffer */
457 /* for RIRB */
458 unsigned short rp, wp; /* read/write pointers */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800459 int cmds[AZX_MAX_CODECS]; /* number of pending requests */
460 u32 res[AZX_MAX_CODECS]; /* last read value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461};
462
Takashi Iwai01b65bf2011-11-24 14:31:46 +0100463struct azx_pcm {
464 struct azx *chip;
465 struct snd_pcm *pcm;
466 struct hda_codec *codec;
467 struct hda_pcm_stream *hinfo[2];
468 struct list_head list;
469};
470
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100471struct azx {
472 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct pci_dev *pci;
Takashi Iwai555e2192008-06-10 17:53:34 +0200474 int dev_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200476 /* chip type specific */
477 int driver_type;
Takashi Iwai9477c582011-05-25 09:11:37 +0200478 unsigned int driver_caps;
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200479 int playback_streams;
480 int playback_index_offset;
481 int capture_streams;
482 int capture_index_offset;
483 int num_streams;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* pci resources */
486 unsigned long addr;
487 void __iomem *remap_addr;
488 int irq;
489
490 /* locks */
491 spinlock_t reg_lock;
Ingo Molnar62932df2006-01-16 16:34:20 +0100492 struct mutex open_mutex;
Takashi Iwaif4c482a2012-12-04 15:09:23 +0100493 struct completion probe_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200495 /* streams (x num_streams) */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100496 struct azx_dev *azx_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* PCM */
Takashi Iwai01b65bf2011-11-24 14:31:46 +0100499 struct list_head pcm_list; /* azx_pcm list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* HD codec */
502 unsigned short codec_mask;
Takashi Iwaif1eaaee2009-02-13 08:16:55 +0100503 int codec_probe_mask; /* copied from probe_mask option */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct hda_bus *bus;
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +0100505 unsigned int beep_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 /* CORB/RIRB */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100508 struct azx_rb corb;
509 struct azx_rb rirb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Takashi Iwai4ce107b2008-02-06 14:50:19 +0100511 /* CORB/RIRB and position buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct snd_dma_buffer rb;
513 struct snd_dma_buffer posbuf;
Takashi Iwaic74db862005-05-12 14:26:27 +0200514
Takashi Iwai4918cda2012-08-09 12:33:28 +0200515#ifdef CONFIG_SND_HDA_PATCH_LOADER
516 const struct firmware *fw;
517#endif
518
Takashi Iwaic74db862005-05-12 14:26:27 +0200519 /* flags */
Shahin Ghazinouribeaffc32010-05-11 08:19:55 +0200520 int position_fix[2]; /* for both playback/capture streams */
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200521 int poll_count;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200522 unsigned int running :1;
Pavel Machek927fc862006-08-31 17:03:43 +0200523 unsigned int initialized :1;
524 unsigned int single_cmd :1;
525 unsigned int polling_mode :1;
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200526 unsigned int msi :1;
Takashi Iwaia6a950a2008-06-10 17:53:35 +0200527 unsigned int irq_pending_warned :1;
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +0100528 unsigned int probing :1; /* codec probing phase */
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200529 unsigned int snoop:1;
Takashi Iwai52409aa2012-01-23 17:10:24 +0100530 unsigned int align_buffer_size:1;
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200531 unsigned int region_requested:1;
532
533 /* VGA-switcheroo setup */
534 unsigned int use_vga_switcheroo:1;
Takashi Iwai128960a2012-10-12 17:28:18 +0200535 unsigned int vga_switcheroo_registered:1;
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200536 unsigned int init_failed:1; /* delayed init failed */
537 unsigned int disabled:1; /* disabled by VGA-switcher */
Takashi Iwai43bbb6c2007-07-06 20:22:05 +0200538
539 /* for debugging */
Wu Fengguangfeb27342009-08-01 19:17:14 +0800540 unsigned int last_cmd[AZX_MAX_CODECS];
Takashi Iwai9ad593f2008-05-16 12:34:47 +0200541
542 /* for pending irqs */
543 struct work_struct irq_pending_work;
Takashi Iwai0cbf0092008-10-29 16:18:25 +0100544
Wang Xingchao99a20082013-05-30 22:07:10 +0800545#ifdef CONFIG_SND_HDA_I915
546 struct work_struct probe_work;
547#endif
548
Takashi Iwai0cbf0092008-10-29 16:18:25 +0100549 /* reboot notifier (for mysterious hangup problem at power-down) */
550 struct notifier_block reboot_notifier;
Takashi Iwai65fcd412012-08-14 17:13:32 +0200551
552 /* card list (for power_save trigger) */
553 struct list_head list;
Takashi Iwaieb49faa2013-03-15 09:19:11 +0100554
555#ifdef CONFIG_SND_HDA_DSP_LOADER
556 struct azx_dev saved_azx_dev;
557#endif
Dave Airlie246efa42013-07-29 15:19:29 +1000558
559 /* secondary power domain for hdmi audio under vga device */
560 struct dev_pm_domain hdmi_pm_domain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561};
562
Takashi Iwai1a8506d2012-10-16 15:10:08 +0200563#define CREATE_TRACE_POINTS
564#include "hda_intel_trace.h"
565
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200566/* driver types */
567enum {
568 AZX_DRIVER_ICH,
Seth Heasley32679f92010-02-22 17:31:09 -0800569 AZX_DRIVER_PCH,
Tobin Davis4979bca2008-01-30 08:13:55 +0100570 AZX_DRIVER_SCH,
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200571 AZX_DRIVER_ATI,
Felix Kuehling778b6e12006-05-17 11:22:21 +0200572 AZX_DRIVER_ATIHDMI,
Andiry Xu1815b342011-12-14 16:10:27 +0800573 AZX_DRIVER_ATIHDMI_NS,
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200574 AZX_DRIVER_VIA,
575 AZX_DRIVER_SIS,
576 AZX_DRIVER_ULI,
Vinod Gda3fca22005-09-13 18:49:12 +0200577 AZX_DRIVER_NVIDIA,
Kailang Yangf2690022008-05-27 11:44:55 +0200578 AZX_DRIVER_TERA,
Takashi Iwai14d34f12010-10-21 09:03:25 +0200579 AZX_DRIVER_CTX,
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200580 AZX_DRIVER_CTHDA,
Yang, Libinc4da29c2008-11-13 11:07:07 +0100581 AZX_DRIVER_GENERIC,
Takashi Iwai2f5983f2008-09-03 16:00:44 +0200582 AZX_NUM_DRIVERS, /* keep this as last entry */
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200583};
584
Takashi Iwai9477c582011-05-25 09:11:37 +0200585/* driver quirks (capabilities) */
586/* bits 0-7 are used for indicating driver type */
587#define AZX_DCAPS_NO_TCSEL (1 << 8) /* No Intel TCSEL bit */
588#define AZX_DCAPS_NO_MSI (1 << 9) /* No MSI support */
589#define AZX_DCAPS_ATI_SNOOP (1 << 10) /* ATI snoop enable */
590#define AZX_DCAPS_NVIDIA_SNOOP (1 << 11) /* Nvidia snoop enable */
591#define AZX_DCAPS_SCH_SNOOP (1 << 12) /* SCH/PCH snoop enable */
592#define AZX_DCAPS_RIRB_DELAY (1 << 13) /* Long delay in read loop */
593#define AZX_DCAPS_RIRB_PRE_DELAY (1 << 14) /* Put a delay before read */
594#define AZX_DCAPS_CTX_WORKAROUND (1 << 15) /* X-Fi workaround */
595#define AZX_DCAPS_POSFIX_LPIB (1 << 16) /* Use LPIB as default */
596#define AZX_DCAPS_POSFIX_VIA (1 << 17) /* Use VIACOMBO as default */
597#define AZX_DCAPS_NO_64BIT (1 << 18) /* No 64bit address */
598#define AZX_DCAPS_SYNC_WRITE (1 << 19) /* sync each cmd write */
Takashi Iwai8b0bd222011-06-10 14:56:26 +0200599#define AZX_DCAPS_OLD_SSYNC (1 << 20) /* Old SSYNC reg for ICH */
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -0500600#define AZX_DCAPS_BUFSIZE (1 << 21) /* no buffer size alignment */
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100601#define AZX_DCAPS_ALIGN_BUFSIZE (1 << 22) /* buffer size alignment */
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200602#define AZX_DCAPS_4K_BDLE_BOUNDARY (1 << 23) /* BDLE in 4k boundary */
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -0500603#define AZX_DCAPS_COUNT_LPIB_DELAY (1 << 25) /* Take LPIB as delay */
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +0100604#define AZX_DCAPS_PM_RUNTIME (1 << 26) /* runtime PM support */
Wang Xingchao99a20082013-05-30 22:07:10 +0800605#define AZX_DCAPS_I915_POWERWELL (1 << 27) /* HSW i915 power well support */
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +0100606
607/* quirks for Intel PCH */
Takashi Iwaid7dab4d2013-01-08 13:51:30 +0100608#define AZX_DCAPS_INTEL_PCH_NOPM \
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +0100609 (AZX_DCAPS_SCH_SNOOP | AZX_DCAPS_BUFSIZE | \
Takashi Iwaid7dab4d2013-01-08 13:51:30 +0100610 AZX_DCAPS_COUNT_LPIB_DELAY)
611
612#define AZX_DCAPS_INTEL_PCH \
613 (AZX_DCAPS_INTEL_PCH_NOPM | AZX_DCAPS_PM_RUNTIME)
Takashi Iwai9477c582011-05-25 09:11:37 +0200614
615/* quirks for ATI SB / AMD Hudson */
616#define AZX_DCAPS_PRESET_ATI_SB \
617 (AZX_DCAPS_ATI_SNOOP | AZX_DCAPS_NO_TCSEL | \
618 AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
619
620/* quirks for ATI/AMD HDMI */
621#define AZX_DCAPS_PRESET_ATI_HDMI \
622 (AZX_DCAPS_NO_TCSEL | AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
623
624/* quirks for Nvidia */
625#define AZX_DCAPS_PRESET_NVIDIA \
Takashi Iwai7bfe0592012-01-23 17:53:39 +0100626 (AZX_DCAPS_NVIDIA_SNOOP | AZX_DCAPS_RIRB_DELAY | AZX_DCAPS_NO_MSI |\
Mike Travis49d9e772013-05-01 14:04:08 -0500627 AZX_DCAPS_ALIGN_BUFSIZE | AZX_DCAPS_NO_64BIT)
Takashi Iwai9477c582011-05-25 09:11:37 +0200628
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200629#define AZX_DCAPS_PRESET_CTHDA \
630 (AZX_DCAPS_NO_MSI | AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_4K_BDLE_BOUNDARY)
631
Takashi Iwaia82d51e2012-04-26 12:23:42 +0200632/*
633 * VGA-switcher support
634 */
635#ifdef SUPPORT_VGA_SWITCHEROO
Takashi Iwai5cb543d2012-08-09 13:49:23 +0200636#define use_vga_switcheroo(chip) ((chip)->use_vga_switcheroo)
637#else
638#define use_vga_switcheroo(chip) 0
639#endif
640
Takashi Iwai48c8b0e2012-12-07 07:40:35 +0100641static char *driver_short_names[] = {
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200642 [AZX_DRIVER_ICH] = "HDA Intel",
Seth Heasley32679f92010-02-22 17:31:09 -0800643 [AZX_DRIVER_PCH] = "HDA Intel PCH",
Tobin Davis4979bca2008-01-30 08:13:55 +0100644 [AZX_DRIVER_SCH] = "HDA Intel MID",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200645 [AZX_DRIVER_ATI] = "HDA ATI SB",
Felix Kuehling778b6e12006-05-17 11:22:21 +0200646 [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
Andiry Xu1815b342011-12-14 16:10:27 +0800647 [AZX_DRIVER_ATIHDMI_NS] = "HDA ATI HDMI",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200648 [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
649 [AZX_DRIVER_SIS] = "HDA SIS966",
Vinod Gda3fca22005-09-13 18:49:12 +0200650 [AZX_DRIVER_ULI] = "HDA ULI M5461",
651 [AZX_DRIVER_NVIDIA] = "HDA NVidia",
Kailang Yangf2690022008-05-27 11:44:55 +0200652 [AZX_DRIVER_TERA] = "HDA Teradici",
Takashi Iwai14d34f12010-10-21 09:03:25 +0200653 [AZX_DRIVER_CTX] = "HDA Creative",
Takashi Iwai5ae763b2012-05-08 10:34:08 +0200654 [AZX_DRIVER_CTHDA] = "HDA Creative",
Yang, Libinc4da29c2008-11-13 11:07:07 +0100655 [AZX_DRIVER_GENERIC] = "HD-Audio Generic",
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200656};
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/*
659 * macros for easy use
660 */
661#define azx_writel(chip,reg,value) \
662 writel(value, (chip)->remap_addr + ICH6_REG_##reg)
663#define azx_readl(chip,reg) \
664 readl((chip)->remap_addr + ICH6_REG_##reg)
665#define azx_writew(chip,reg,value) \
666 writew(value, (chip)->remap_addr + ICH6_REG_##reg)
667#define azx_readw(chip,reg) \
668 readw((chip)->remap_addr + ICH6_REG_##reg)
669#define azx_writeb(chip,reg,value) \
670 writeb(value, (chip)->remap_addr + ICH6_REG_##reg)
671#define azx_readb(chip,reg) \
672 readb((chip)->remap_addr + ICH6_REG_##reg)
673
674#define azx_sd_writel(dev,reg,value) \
675 writel(value, (dev)->sd_addr + ICH6_REG_##reg)
676#define azx_sd_readl(dev,reg) \
677 readl((dev)->sd_addr + ICH6_REG_##reg)
678#define azx_sd_writew(dev,reg,value) \
679 writew(value, (dev)->sd_addr + ICH6_REG_##reg)
680#define azx_sd_readw(dev,reg) \
681 readw((dev)->sd_addr + ICH6_REG_##reg)
682#define azx_sd_writeb(dev,reg,value) \
683 writeb(value, (dev)->sd_addr + ICH6_REG_##reg)
684#define azx_sd_readb(dev,reg) \
685 readb((dev)->sd_addr + ICH6_REG_##reg)
686
687/* for pcm support */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100688#define get_azx_dev(substream) (substream->runtime->private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200690#ifdef CONFIG_X86
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100691static void __mark_pages_wc(struct azx *chip, struct snd_dma_buffer *dmab, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200692{
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100693 int pages;
694
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200695 if (azx_snoop(chip))
696 return;
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100697 if (!dmab || !dmab->area || !dmab->bytes)
698 return;
699
700#ifdef CONFIG_SND_DMA_SGBUF
701 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_SG) {
702 struct snd_sg_buf *sgbuf = dmab->private_data;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200703 if (on)
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100704 set_pages_array_wc(sgbuf->page_table, sgbuf->pages);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200705 else
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100706 set_pages_array_wb(sgbuf->page_table, sgbuf->pages);
707 return;
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200708 }
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100709#endif
710
711 pages = (dmab->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
712 if (on)
713 set_memory_wc((unsigned long)dmab->area, pages);
714 else
715 set_memory_wb((unsigned long)dmab->area, pages);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200716}
717
718static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
719 bool on)
720{
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100721 __mark_pages_wc(chip, buf, on);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200722}
723static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100724 struct snd_pcm_substream *substream, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200725{
726 if (azx_dev->wc_marked != on) {
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100727 __mark_pages_wc(chip, snd_pcm_get_dma_buf(substream), on);
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200728 azx_dev->wc_marked = on;
729 }
730}
731#else
732/* NOP for other archs */
733static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
734 bool on)
735{
736}
737static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +0100738 struct snd_pcm_substream *substream, bool on)
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200739{
740}
741#endif
742
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200743static int azx_acquire_irq(struct azx *chip, int do_disconnect);
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200744static int azx_send_cmd(struct hda_bus *bus, unsigned int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745/*
746 * Interface for HD codec
747 */
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/*
750 * CORB / RIRB interface
751 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100752static int azx_alloc_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 int err;
755
756 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
Takashi Iwaid01ce992007-07-27 16:52:19 +0200757 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
758 snd_dma_pci_data(chip->pci),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 PAGE_SIZE, &chip->rb);
760 if (err < 0) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +0800761 snd_printk(KERN_ERR SFX "%s: cannot allocate CORB/RIRB\n", pci_name(chip->pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return err;
763 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +0200764 mark_pages_wc(chip, &chip->rb, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 0;
766}
767
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100768static void azx_init_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800770 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* CORB set up */
772 chip->corb.addr = chip->rb.addr;
773 chip->corb.buf = (u32 *)chip->rb.area;
774 azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200775 azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200777 /* set the corb size to 256 entries (ULI requires explicitly) */
778 azx_writeb(chip, CORBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* set the corb write pointer to 0 */
780 azx_writew(chip, CORBWP, 0);
781 /* reset the corb hw read pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200782 azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* enable corb dma */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200784 azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /* RIRB set up */
787 chip->rirb.addr = chip->rb.addr + 2048;
788 chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800789 chip->rirb.wp = chip->rirb.rp = 0;
790 memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +0200792 azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Takashi Iwai07e4ca52005-08-24 14:14:57 +0200794 /* set the rirb size to 256 entries (ULI requires explicitly) */
795 azx_writeb(chip, RIRBSIZE, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /* reset the rirb hw write pointer */
Takashi Iwaib21fadb2009-05-28 12:26:15 +0200797 azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* set N=1, get RIRB response interrupt for new entry */
Takashi Iwai9477c582011-05-25 09:11:37 +0200799 if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
Takashi Iwai14d34f12010-10-21 09:03:25 +0200800 azx_writew(chip, RINTCNT, 0xc0);
801 else
802 azx_writew(chip, RINTCNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* enable rirb dma and response irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800805 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100808static void azx_free_cmd_io(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800810 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* disable ringbuffer DMAs */
812 azx_writeb(chip, RIRBCTL, 0);
813 azx_writeb(chip, CORBCTL, 0);
Wu Fengguangcdb1fbf2009-08-01 18:47:41 +0800814 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Wu Fengguangdeadff12009-08-01 18:45:16 +0800817static unsigned int azx_command_addr(u32 cmd)
818{
819 unsigned int addr = cmd >> 28;
820
821 if (addr >= AZX_MAX_CODECS) {
822 snd_BUG();
823 addr = 0;
824 }
825
826 return addr;
827}
828
829static unsigned int azx_response_addr(u32 res)
830{
831 unsigned int addr = res & 0xf;
832
833 if (addr >= AZX_MAX_CODECS) {
834 snd_BUG();
835 addr = 0;
836 }
837
838 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100842static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100844 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800845 unsigned int addr = azx_command_addr(val);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100846 unsigned int wp, rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Wu Fengguangc32649f2009-08-01 18:48:12 +0800848 spin_lock_irq(&chip->reg_lock);
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* add command to corb */
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100851 wp = azx_readw(chip, CORBWP);
852 if (wp == 0xffff) {
853 /* something wrong, controller likely turned to D3 */
854 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100855 return -EIO;
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 wp++;
858 wp %= ICH6_MAX_CORB_ENTRIES;
859
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100860 rp = azx_readw(chip, CORBRP);
861 if (wp == rp) {
862 /* oops, it's full */
863 spin_unlock_irq(&chip->reg_lock);
864 return -EAGAIN;
865 }
866
Wu Fengguangdeadff12009-08-01 18:45:16 +0800867 chip->rirb.cmds[addr]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 chip->corb.buf[wp] = cpu_to_le32(val);
869 azx_writel(chip, CORBWP, wp);
Wu Fengguangc32649f2009-08-01 18:48:12 +0800870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 spin_unlock_irq(&chip->reg_lock);
872
873 return 0;
874}
875
876#define ICH6_RIRB_EX_UNSOL_EV (1<<4)
877
878/* retrieve RIRB entry - called from interrupt handler */
Takashi Iwaia98f90f2005-11-17 14:59:02 +0100879static void azx_update_rirb(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 unsigned int rp, wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800882 unsigned int addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 u32 res, res_ex;
884
Takashi Iwaicc5ede32012-12-12 11:10:49 +0100885 wp = azx_readw(chip, RIRBWP);
886 if (wp == 0xffff) {
887 /* something wrong, controller likely turned to D3 */
888 return;
889 }
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (wp == chip->rirb.wp)
892 return;
893 chip->rirb.wp = wp;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 while (chip->rirb.rp != wp) {
896 chip->rirb.rp++;
897 chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
898
899 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
900 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
901 res = le32_to_cpu(chip->rirb.buf[rp]);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800902 addr = azx_response_addr(res_ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
904 snd_hda_queue_unsol_event(chip->bus, res, res_ex);
Wu Fengguangdeadff12009-08-01 18:45:16 +0800905 else if (chip->rirb.cmds[addr]) {
906 chip->rirb.res[addr] = res;
Takashi Iwai2add9b92008-03-18 09:47:06 +0100907 smp_wmb();
Wu Fengguangdeadff12009-08-01 18:45:16 +0800908 chip->rirb.cmds[addr]--;
Wu Fengguange310bb02009-08-01 19:18:45 +0800909 } else
Takashi Iwai9e3d3522012-10-17 08:39:37 +0200910 snd_printk(KERN_ERR SFX "%s: spurious response %#x:%#x, "
Wu Fengguange310bb02009-08-01 19:18:45 +0800911 "last cmd=%#08x\n",
Takashi Iwai9e3d3522012-10-17 08:39:37 +0200912 pci_name(chip->pci),
Wu Fengguange310bb02009-08-01 19:18:45 +0800913 res, res_ex,
914 chip->last_cmd[addr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916}
917
918/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +0800919static unsigned int azx_rirb_get_response(struct hda_bus *bus,
920 unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100922 struct azx *chip = bus->private_data;
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200923 unsigned long timeout;
David Henningsson32cf4022012-05-04 11:05:55 +0200924 unsigned long loopcounter;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200925 int do_poll = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200927 again:
928 timeout = jiffies + msecs_to_jiffies(1000);
David Henningsson32cf4022012-05-04 11:05:55 +0200929
930 for (loopcounter = 0;; loopcounter++) {
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200931 if (chip->polling_mode || do_poll) {
Takashi Iwaie96224a2006-08-21 17:57:44 +0200932 spin_lock_irq(&chip->reg_lock);
933 azx_update_rirb(chip);
934 spin_unlock_irq(&chip->reg_lock);
935 }
Wu Fengguangdeadff12009-08-01 18:45:16 +0800936 if (!chip->rirb.cmds[addr]) {
Takashi Iwai2add9b92008-03-18 09:47:06 +0100937 smp_rmb();
Takashi Iwaib6132912009-03-24 07:36:09 +0100938 bus->rirb_error = 0;
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200939
940 if (!do_poll)
941 chip->poll_count = 0;
Wu Fengguangdeadff12009-08-01 18:45:16 +0800942 return chip->rirb.res[addr]; /* the last value */
Takashi Iwai2add9b92008-03-18 09:47:06 +0100943 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100944 if (time_after(jiffies, timeout))
945 break;
David Henningsson32cf4022012-05-04 11:05:55 +0200946 if (bus->needs_damn_long_delay || loopcounter > 3000)
Takashi Iwai52987652008-01-16 16:09:47 +0100947 msleep(2); /* temporary workaround */
948 else {
949 udelay(10);
950 cond_resched();
951 }
Takashi Iwai28a0d9d2008-01-18 15:32:32 +0100952 }
Takashi Iwai5c79b1f2006-09-21 13:34:13 +0200953
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200954 if (!bus->no_response_fallback)
955 return -1;
956
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200957 if (!chip->polling_mode && chip->poll_count < 2) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +0800958 snd_printdd(SFX "%s: azx_get_response timeout, "
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200959 "polling the codec once: last cmd=0x%08x\n",
Daniel J Blueman445a51b2012-12-05 23:04:21 +0800960 pci_name(chip->pci), chip->last_cmd[addr]);
Maxim Levitsky1eb6dc72010-02-04 22:21:47 +0200961 do_poll = 1;
962 chip->poll_count++;
963 goto again;
964 }
965
966
Takashi Iwai23c4a882009-10-30 13:21:49 +0100967 if (!chip->polling_mode) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +0800968 snd_printk(KERN_WARNING SFX "%s: azx_get_response timeout, "
Takashi Iwai23c4a882009-10-30 13:21:49 +0100969 "switching to polling mode: last cmd=0x%08x\n",
Daniel J Blueman445a51b2012-12-05 23:04:21 +0800970 pci_name(chip->pci), chip->last_cmd[addr]);
Takashi Iwai23c4a882009-10-30 13:21:49 +0100971 chip->polling_mode = 1;
972 goto again;
973 }
974
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200975 if (chip->msi) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +0800976 snd_printk(KERN_WARNING SFX "%s: No response from codec, "
Wu Fengguangfeb27342009-08-01 19:17:14 +0800977 "disabling MSI: last cmd=0x%08x\n",
Daniel J Blueman445a51b2012-12-05 23:04:21 +0800978 pci_name(chip->pci), chip->last_cmd[addr]);
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200979 free_irq(chip->irq, chip);
980 chip->irq = -1;
981 pci_disable_msi(chip->pci);
982 chip->msi = 0;
Takashi Iwaib6132912009-03-24 07:36:09 +0100983 if (azx_acquire_irq(chip, 1) < 0) {
984 bus->rirb_error = 1;
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200985 return -1;
Takashi Iwaib6132912009-03-24 07:36:09 +0100986 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +0200987 goto again;
988 }
989
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +0100990 if (chip->probing) {
991 /* If this critical timeout happens during the codec probing
992 * phase, this is likely an access to a non-existing codec
993 * slot. Better to return an error and reset the system.
994 */
995 return -1;
996 }
997
Takashi Iwai8dd78332009-06-02 01:16:07 +0200998 /* a fatal communication error; need either to reset or to fallback
999 * to the single_cmd mode
1000 */
Takashi Iwaib6132912009-03-24 07:36:09 +01001001 bus->rirb_error = 1;
Takashi Iwaib20f3b82009-06-02 01:20:22 +02001002 if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
Takashi Iwai8dd78332009-06-02 01:16:07 +02001003 bus->response_reset = 1;
1004 return -1; /* give a chance to retry */
1005 }
1006
1007 snd_printk(KERN_ERR "hda_intel: azx_get_response timeout, "
1008 "switching to single_cmd mode: last cmd=0x%08x\n",
Wu Fengguangfeb27342009-08-01 19:17:14 +08001009 chip->last_cmd[addr]);
Takashi Iwai8dd78332009-06-02 01:16:07 +02001010 chip->single_cmd = 1;
1011 bus->response_reset = 0;
Takashi Iwai1a696972009-11-07 09:49:04 +01001012 /* release CORB/RIRB */
Takashi Iwai4fcd3922009-05-25 18:34:52 +02001013 azx_free_cmd_io(chip);
Takashi Iwai1a696972009-11-07 09:49:04 +01001014 /* disable unsolicited responses */
1015 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
Takashi Iwai5c79b1f2006-09-21 13:34:13 +02001016 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/*
1020 * Use the single immediate command instead of CORB/RIRB for simplicity
1021 *
1022 * Note: according to Intel, this is not preferred use. The command was
1023 * intended for the BIOS only, and may get confused with unsolicited
1024 * responses. So, we shouldn't use it for normal operation from the
1025 * driver.
1026 * I left the codes, however, for debugging/testing purposes.
1027 */
1028
Takashi Iwaib05a7d42009-05-28 11:59:12 +02001029/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +08001030static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
Takashi Iwaib05a7d42009-05-28 11:59:12 +02001031{
1032 int timeout = 50;
1033
1034 while (timeout--) {
1035 /* check IRV busy bit */
1036 if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
1037 /* reuse rirb.res as the response return value */
Wu Fengguangdeadff12009-08-01 18:45:16 +08001038 chip->rirb.res[addr] = azx_readl(chip, IR);
Takashi Iwaib05a7d42009-05-28 11:59:12 +02001039 return 0;
1040 }
1041 udelay(1);
1042 }
1043 if (printk_ratelimit())
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001044 snd_printd(SFX "%s: get_response timeout: IRS=0x%x\n",
1045 pci_name(chip->pci), azx_readw(chip, IRS));
Wu Fengguangdeadff12009-08-01 18:45:16 +08001046 chip->rirb.res[addr] = -1;
Takashi Iwaib05a7d42009-05-28 11:59:12 +02001047 return -EIO;
1048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001051static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001053 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +08001054 unsigned int addr = azx_command_addr(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 int timeout = 50;
1056
Takashi Iwai8dd78332009-06-02 01:16:07 +02001057 bus->rirb_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 while (timeout--) {
1059 /* check ICB busy bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +02001060 if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* Clear IRV valid bit */
Takashi Iwaid01ce992007-07-27 16:52:19 +02001062 azx_writew(chip, IRS, azx_readw(chip, IRS) |
1063 ICH6_IRS_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 azx_writel(chip, IC, val);
Takashi Iwaid01ce992007-07-27 16:52:19 +02001065 azx_writew(chip, IRS, azx_readw(chip, IRS) |
1066 ICH6_IRS_BUSY);
Wu Fengguangdeadff12009-08-01 18:45:16 +08001067 return azx_single_wait_for_response(chip, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 udelay(1);
1070 }
Marc Boucher1cfd52b2008-01-22 15:29:26 +01001071 if (printk_ratelimit())
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001072 snd_printd(SFX "%s: send_cmd timeout: IRS=0x%x, val=0x%x\n",
1073 pci_name(chip->pci), azx_readw(chip, IRS), val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return -EIO;
1075}
1076
1077/* receive a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +08001078static unsigned int azx_single_get_response(struct hda_bus *bus,
1079 unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001081 struct azx *chip = bus->private_data;
Wu Fengguangdeadff12009-08-01 18:45:16 +08001082 return chip->rirb.res[addr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Takashi Iwai111d3af2006-02-16 18:17:58 +01001085/*
1086 * The below are the main callbacks from hda_codec.
1087 *
1088 * They are just the skeleton to call sub-callbacks according to the
1089 * current setting of chip->single_cmd.
1090 */
1091
1092/* send a command */
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001093static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
Takashi Iwai111d3af2006-02-16 18:17:58 +01001094{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001095 struct azx *chip = bus->private_data;
Takashi Iwai43bbb6c2007-07-06 20:22:05 +02001096
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001097 if (chip->disabled)
1098 return 0;
Wu Fengguangfeb27342009-08-01 19:17:14 +08001099 chip->last_cmd[azx_command_addr(val)] = val;
Takashi Iwai111d3af2006-02-16 18:17:58 +01001100 if (chip->single_cmd)
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001101 return azx_single_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001102 else
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001103 return azx_corb_send_cmd(bus, val);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001104}
1105
1106/* get a response */
Wu Fengguangdeadff12009-08-01 18:45:16 +08001107static unsigned int azx_get_response(struct hda_bus *bus,
1108 unsigned int addr)
Takashi Iwai111d3af2006-02-16 18:17:58 +01001109{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001110 struct azx *chip = bus->private_data;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001111 if (chip->disabled)
1112 return 0;
Takashi Iwai111d3af2006-02-16 18:17:58 +01001113 if (chip->single_cmd)
Wu Fengguangdeadff12009-08-01 18:45:16 +08001114 return azx_single_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001115 else
Wu Fengguangdeadff12009-08-01 18:45:16 +08001116 return azx_rirb_get_response(bus, addr);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001117}
1118
Takashi Iwai83012a72012-08-24 18:38:08 +02001119#ifdef CONFIG_PM
Takashi Iwai68467f52012-08-28 09:14:29 -07001120static void azx_power_notify(struct hda_bus *bus, bool power_up);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001121#endif
Takashi Iwai111d3af2006-02-16 18:17:58 +01001122
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001123#ifdef CONFIG_SND_HDA_DSP_LOADER
1124static int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
1125 unsigned int byte_size,
1126 struct snd_dma_buffer *bufp);
1127static void azx_load_dsp_trigger(struct hda_bus *bus, bool start);
1128static void azx_load_dsp_cleanup(struct hda_bus *bus,
1129 struct snd_dma_buffer *dmab);
1130#endif
1131
Mengdong Lin3af3f352013-06-24 10:18:54 -04001132/* enter link reset */
Mengdong Lin7295b262013-06-25 05:58:49 -04001133static void azx_enter_link_reset(struct azx *chip)
Mengdong Lin3af3f352013-06-24 10:18:54 -04001134{
1135 unsigned long timeout;
1136
1137 /* reset controller */
1138 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET);
1139
1140 timeout = jiffies + msecs_to_jiffies(100);
1141 while ((azx_readb(chip, GCTL) & ICH6_GCTL_RESET) &&
1142 time_before(jiffies, timeout))
1143 usleep_range(500, 1000);
1144}
1145
Mengdong Lin7295b262013-06-25 05:58:49 -04001146/* exit link reset */
1147static void azx_exit_link_reset(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Mengdong Linfa348da2012-12-12 09:16:15 -05001149 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Mengdong Lin7295b262013-06-25 05:58:49 -04001151 azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET);
1152
1153 timeout = jiffies + msecs_to_jiffies(100);
1154 while (!azx_readb(chip, GCTL) &&
1155 time_before(jiffies, timeout))
1156 usleep_range(500, 1000);
1157}
1158
1159/* reset codec link */
1160static int azx_reset(struct azx *chip, int full_reset)
1161{
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001162 if (!full_reset)
1163 goto __skip;
1164
Danny Tholene8a7f132007-09-11 21:41:56 +02001165 /* clear STATESTS */
1166 azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* reset controller */
Mengdong Lin7295b262013-06-25 05:58:49 -04001169 azx_enter_link_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /* delay for >= 100us for codec PLL to settle per spec
1172 * Rev 0.9 section 5.5.1
1173 */
Mengdong Linfa348da2012-12-12 09:16:15 -05001174 usleep_range(500, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* Bring controller out of reset */
Mengdong Lin7295b262013-06-25 05:58:49 -04001177 azx_exit_link_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Pavel Machek927fc862006-08-31 17:03:43 +02001179 /* Brent Chartrand said to wait >= 540us for codecs to initialize */
Mengdong Linfa348da2012-12-12 09:16:15 -05001180 usleep_range(1000, 1200);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001182 __skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* check to see if controller is ready */
Pavel Machek927fc862006-08-31 17:03:43 +02001184 if (!azx_readb(chip, GCTL)) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001185 snd_printd(SFX "%s: azx_reset: controller not ready!\n", pci_name(chip->pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return -EBUSY;
1187 }
1188
Matt41e2fce2005-07-04 17:49:55 +02001189 /* Accept unsolicited responses */
Takashi Iwai1a696972009-11-07 09:49:04 +01001190 if (!chip->single_cmd)
1191 azx_writel(chip, GCTL, azx_readl(chip, GCTL) |
1192 ICH6_GCTL_UNSOL);
Matt41e2fce2005-07-04 17:49:55 +02001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* detect codecs */
Pavel Machek927fc862006-08-31 17:03:43 +02001195 if (!chip->codec_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 chip->codec_mask = azx_readw(chip, STATESTS);
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001197 snd_printdd(SFX "%s: codec_mask = 0x%x\n", pci_name(chip->pci), chip->codec_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
1200 return 0;
1201}
1202
1203
1204/*
1205 * Lowlevel interface
1206 */
1207
1208/* enable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001209static void azx_int_enable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 /* enable controller CIE and GIE */
1212 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
1213 ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN);
1214}
1215
1216/* disable interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001217static void azx_int_disable(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 int i;
1220
1221 /* disable interrupts in stream descriptor */
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001222 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001223 struct azx_dev *azx_dev = &chip->azx_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 azx_sd_writeb(azx_dev, SD_CTL,
1225 azx_sd_readb(azx_dev, SD_CTL) & ~SD_INT_MASK);
1226 }
1227
1228 /* disable SIE for all streams */
1229 azx_writeb(chip, INTCTL, 0);
1230
1231 /* disable controller CIE and GIE */
1232 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
1233 ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN));
1234}
1235
1236/* clear interrupts */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001237static void azx_int_clear(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 int i;
1240
1241 /* clear stream status */
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001242 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001243 struct azx_dev *azx_dev = &chip->azx_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
1245 }
1246
1247 /* clear STATESTS */
1248 azx_writeb(chip, STATESTS, STATESTS_INT_MASK);
1249
1250 /* clear rirb status */
1251 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1252
1253 /* clear int status */
1254 azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM);
1255}
1256
1257/* start a stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001258static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Joseph Chan0e153472008-08-26 14:38:03 +02001260 /*
1261 * Before stream start, initialize parameter
1262 */
1263 azx_dev->insufficient = 1;
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 /* enable SIE */
Wei Niccc5df02010-01-26 15:59:33 +08001266 azx_writel(chip, INTCTL,
1267 azx_readl(chip, INTCTL) | (1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /* set DMA start and interrupt mask */
1269 azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
1270 SD_CTL_DMA_START | SD_INT_MASK);
1271}
1272
Takashi Iwai1dddab42009-03-18 15:15:37 +01001273/* stop DMA */
1274static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) &
1277 ~(SD_CTL_DMA_START | SD_INT_MASK));
1278 azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
Takashi Iwai1dddab42009-03-18 15:15:37 +01001279}
1280
1281/* stop a stream */
1282static void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
1283{
1284 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* disable SIE */
Wei Niccc5df02010-01-26 15:59:33 +08001286 azx_writel(chip, INTCTL,
1287 azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290
1291/*
Takashi Iwaicb53c622007-08-10 17:21:45 +02001292 * reset and start the controller registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001294static void azx_init_chip(struct azx *chip, int full_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001296 if (chip->initialized)
1297 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /* reset controller */
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001300 azx_reset(chip, full_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* initialize interrupts */
1303 azx_int_clear(chip);
1304 azx_int_enable(chip);
1305
1306 /* initialize the codec command I/O */
Takashi Iwai1a696972009-11-07 09:49:04 +01001307 if (!chip->single_cmd)
1308 azx_init_cmd_io(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02001310 /* program the position buffer */
1311 azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
Takashi Iwai766979e2008-06-13 20:53:56 +02001312 azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr));
Frederick Lif5d40b32005-05-12 14:55:20 +02001313
Takashi Iwaicb53c622007-08-10 17:21:45 +02001314 chip->initialized = 1;
1315}
1316
1317/*
1318 * initialize the PCI registers
1319 */
1320/* update bits in a PCI register byte */
1321static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
1322 unsigned char mask, unsigned char val)
1323{
1324 unsigned char data;
1325
1326 pci_read_config_byte(pci, reg, &data);
1327 data &= ~mask;
1328 data |= (val & mask);
1329 pci_write_config_byte(pci, reg, data);
1330}
1331
1332static void azx_init_pci(struct azx *chip)
1333{
1334 /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
1335 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
1336 * Ensuring these bits are 0 clears playback static on some HD Audio
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +01001337 * codecs.
1338 * The PCI register TCSEL is defined in the Intel manuals.
Takashi Iwaicb53c622007-08-10 17:21:45 +02001339 */
Linus Torvalds46f2cc82011-05-27 19:45:28 -07001340 if (!(chip->driver_caps & AZX_DCAPS_NO_TCSEL)) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001341 snd_printdd(SFX "%s: Clearing TCSEL\n", pci_name(chip->pci));
Adam Lackorzynskia09e89f2011-03-10 17:41:56 +01001342 update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
Takashi Iwai9477c582011-05-25 09:11:37 +02001343 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02001344
Takashi Iwai9477c582011-05-25 09:11:37 +02001345 /* For ATI SB450/600/700/800/900 and AMD Hudson azalia HD audio,
1346 * we need to enable snoop.
1347 */
1348 if (chip->driver_caps & AZX_DCAPS_ATI_SNOOP) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001349 snd_printdd(SFX "%s: Setting ATI snoop: %d\n", pci_name(chip->pci), azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001350 update_pci_byte(chip->pci,
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001351 ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR, 0x07,
1352 azx_snoop(chip) ? ATI_SB450_HDAUDIO_ENABLE_SNOOP : 0);
Takashi Iwai9477c582011-05-25 09:11:37 +02001353 }
1354
1355 /* For NVIDIA HDA, enable snoop */
1356 if (chip->driver_caps & AZX_DCAPS_NVIDIA_SNOOP) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001357 snd_printdd(SFX "%s: Setting Nvidia snoop: %d\n", pci_name(chip->pci), azx_snoop(chip));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001358 update_pci_byte(chip->pci,
1359 NVIDIA_HDA_TRANSREG_ADDR,
1360 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
Peer Chen320dcc32008-08-20 16:43:24 -07001361 update_pci_byte(chip->pci,
1362 NVIDIA_HDA_ISTRM_COH,
1363 0x01, NVIDIA_HDA_ENABLE_COHBIT);
1364 update_pci_byte(chip->pci,
1365 NVIDIA_HDA_OSTRM_COH,
1366 0x01, NVIDIA_HDA_ENABLE_COHBIT);
Takashi Iwai9477c582011-05-25 09:11:37 +02001367 }
1368
1369 /* Enable SCH/PCH snoop if needed */
1370 if (chip->driver_caps & AZX_DCAPS_SCH_SNOOP) {
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001371 unsigned short snoop;
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001372 pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001373 if ((!azx_snoop(chip) && !(snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)) ||
1374 (azx_snoop(chip) && (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP))) {
1375 snoop &= ~INTEL_SCH_HDA_DEVC_NOSNOOP;
1376 if (!azx_snoop(chip))
1377 snoop |= INTEL_SCH_HDA_DEVC_NOSNOOP;
1378 pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC, snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001379 pci_read_config_word(chip->pci,
1380 INTEL_SCH_HDA_DEVC, &snoop);
Takashi Iwai90a5ad52008-02-22 18:36:22 +01001381 }
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001382 snd_printdd(SFX "%s: SCH snoop: %s\n",
1383 pci_name(chip->pci), (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001384 ? "Disabled" : "Enabled");
Vinod Gda3fca22005-09-13 18:49:12 +02001385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001389static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391/*
1392 * interrupt handler
1393 */
David Howells7d12e782006-10-05 14:55:46 +01001394static irqreturn_t azx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001396 struct azx *chip = dev_id;
1397 struct azx_dev *azx_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 u32 status;
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001399 u8 sd_status;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001400 int i, ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001402#ifdef CONFIG_PM_RUNTIME
Dave Airlie246efa42013-07-29 15:19:29 +10001403 if (chip->driver_caps & AZX_DCAPS_PM_RUNTIME)
1404 if (chip->pci->dev.power.runtime_status != RPM_ACTIVE)
1405 return IRQ_NONE;
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001406#endif
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 spin_lock(&chip->reg_lock);
1409
Dan Carpenter60911062012-05-18 10:36:11 +03001410 if (chip->disabled) {
1411 spin_unlock(&chip->reg_lock);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001412 return IRQ_NONE;
Dan Carpenter60911062012-05-18 10:36:11 +03001413 }
Takashi Iwaia82d51e2012-04-26 12:23:42 +02001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 status = azx_readl(chip, INTSTS);
Dave Airlie246efa42013-07-29 15:19:29 +10001416 if (status == 0 || status == 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 spin_unlock(&chip->reg_lock);
1418 return IRQ_NONE;
1419 }
1420
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001421 for (i = 0; i < chip->num_streams; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 azx_dev = &chip->azx_dev[i];
1423 if (status & azx_dev->sd_int_sta_mask) {
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001424 sd_status = azx_sd_readb(azx_dev, SD_STS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 azx_sd_writeb(azx_dev, SD_STS, SD_INT_MASK);
Clemens Ladisch9ef04062010-05-25 09:03:40 +02001426 if (!azx_dev->substream || !azx_dev->running ||
1427 !(sd_status & SD_INT_COMPLETE))
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001428 continue;
1429 /* check whether this IRQ is really acceptable */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001430 ok = azx_position_ok(chip, azx_dev);
1431 if (ok == 1) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001432 azx_dev->irq_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 spin_unlock(&chip->reg_lock);
1434 snd_pcm_period_elapsed(azx_dev->substream);
1435 spin_lock(&chip->reg_lock);
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001436 } else if (ok == 0 && chip->bus && chip->bus->workq) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02001437 /* bogus IRQ, process it later */
1438 azx_dev->irq_pending = 1;
Takashi Iwai6acaed32009-01-12 10:09:24 +01001439 queue_work(chip->bus->workq,
1440 &chip->irq_pending_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442 }
1443 }
1444
1445 /* clear rirb int */
1446 status = azx_readb(chip, RIRBSTS);
1447 if (status & RIRB_INT_MASK) {
Takashi Iwai14d34f12010-10-21 09:03:25 +02001448 if (status & RIRB_INT_RESPONSE) {
Takashi Iwai9477c582011-05-25 09:11:37 +02001449 if (chip->driver_caps & AZX_DCAPS_RIRB_PRE_DELAY)
Takashi Iwai14d34f12010-10-21 09:03:25 +02001450 udelay(80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 azx_update_rirb(chip);
Takashi Iwai14d34f12010-10-21 09:03:25 +02001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1454 }
1455
1456#if 0
1457 /* clear state status int */
1458 if (azx_readb(chip, STATESTS) & 0x04)
1459 azx_writeb(chip, STATESTS, 0x04);
1460#endif
1461 spin_unlock(&chip->reg_lock);
1462
1463 return IRQ_HANDLED;
1464}
1465
1466
1467/*
Takashi Iwai675f25d2008-06-10 17:53:20 +02001468 * set up a BDL entry
1469 */
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001470static int setup_bdle(struct azx *chip,
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001471 struct snd_dma_buffer *dmab,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001472 struct azx_dev *azx_dev, u32 **bdlp,
1473 int ofs, int size, int with_ioc)
1474{
Takashi Iwai675f25d2008-06-10 17:53:20 +02001475 u32 *bdl = *bdlp;
1476
1477 while (size > 0) {
1478 dma_addr_t addr;
1479 int chunk;
1480
1481 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
1482 return -EINVAL;
1483
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001484 addr = snd_sgbuf_get_addr(dmab, ofs);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001485 /* program the address field of the BDL entry */
1486 bdl[0] = cpu_to_le32((u32)addr);
Takashi Iwai766979e2008-06-13 20:53:56 +02001487 bdl[1] = cpu_to_le32(upper_32_bits(addr));
Takashi Iwai675f25d2008-06-10 17:53:20 +02001488 /* program the size field of the BDL entry */
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001489 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
Takashi Iwai5ae763b2012-05-08 10:34:08 +02001490 /* one BDLE cannot cross 4K boundary on CTHDA chips */
1491 if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY) {
1492 u32 remain = 0x1000 - (ofs & 0xfff);
1493 if (chunk > remain)
1494 chunk = remain;
1495 }
Takashi Iwai675f25d2008-06-10 17:53:20 +02001496 bdl[2] = cpu_to_le32(chunk);
1497 /* program the IOC to enable interrupt
1498 * only when the whole fragment is processed
1499 */
1500 size -= chunk;
1501 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
1502 bdl += 4;
1503 azx_dev->frags++;
1504 ofs += chunk;
1505 }
1506 *bdlp = bdl;
1507 return ofs;
1508}
1509
1510/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 * set up BDL entries
1512 */
Takashi Iwai555e2192008-06-10 17:53:34 +02001513static int azx_setup_periods(struct azx *chip,
1514 struct snd_pcm_substream *substream,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001515 struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001517 u32 *bdl;
1518 int i, ofs, periods, period_bytes;
Takashi Iwai555e2192008-06-10 17:53:34 +02001519 int pos_adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 /* reset BDL address */
1522 azx_sd_writel(azx_dev, SD_BDLPL, 0);
1523 azx_sd_writel(azx_dev, SD_BDLPU, 0);
1524
Takashi Iwai97b71c92009-03-18 15:09:13 +01001525 period_bytes = azx_dev->period_bytes;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001526 periods = azx_dev->bufsize / period_bytes;
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 /* program the initial BDL entries */
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001529 bdl = (u32 *)azx_dev->bdl.area;
1530 ofs = 0;
1531 azx_dev->frags = 0;
Takashi Iwai555e2192008-06-10 17:53:34 +02001532 pos_adj = bdl_pos_adj[chip->dev_index];
Takashi Iwai915bf292012-09-11 15:19:10 +02001533 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
Takashi Iwai675f25d2008-06-10 17:53:20 +02001534 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001535 int pos_align = pos_adj;
Takashi Iwai555e2192008-06-10 17:53:34 +02001536 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001537 if (!pos_adj)
Takashi Iwaie785d3d2008-07-15 16:28:43 +02001538 pos_adj = pos_align;
1539 else
1540 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
1541 pos_align;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001542 pos_adj = frames_to_bytes(runtime, pos_adj);
1543 if (pos_adj >= period_bytes) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001544 snd_printk(KERN_WARNING SFX "%s: Too big adjustment %d\n",
1545 pci_name(chip->pci), bdl_pos_adj[chip->dev_index]);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001546 pos_adj = 0;
1547 } else {
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001548 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1549 azx_dev,
Takashi Iwai915bf292012-09-11 15:19:10 +02001550 &bdl, ofs, pos_adj, true);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001551 if (ofs < 0)
1552 goto error;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001553 }
Takashi Iwai555e2192008-06-10 17:53:34 +02001554 } else
1555 pos_adj = 0;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001556 for (i = 0; i < periods; i++) {
1557 if (i == periods - 1 && pos_adj)
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001558 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1559 azx_dev, &bdl, ofs,
Takashi Iwai675f25d2008-06-10 17:53:20 +02001560 period_bytes - pos_adj, 0);
1561 else
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001562 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
1563 azx_dev, &bdl, ofs,
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001564 period_bytes,
Takashi Iwai915bf292012-09-11 15:19:10 +02001565 !azx_dev->no_period_wakeup);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001566 if (ofs < 0)
1567 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001569 return 0;
Takashi Iwai675f25d2008-06-10 17:53:20 +02001570
1571 error:
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001572 snd_printk(KERN_ERR SFX "%s: Too many BDL entries: buffer=%d, period=%d\n",
1573 pci_name(chip->pci), azx_dev->bufsize, period_bytes);
Takashi Iwai675f25d2008-06-10 17:53:20 +02001574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Takashi Iwai1dddab42009-03-18 15:15:37 +01001577/* reset stream */
1578static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 unsigned char val;
1581 int timeout;
1582
Takashi Iwai1dddab42009-03-18 15:15:37 +01001583 azx_stream_clear(chip, azx_dev);
1584
Takashi Iwaid01ce992007-07-27 16:52:19 +02001585 azx_sd_writeb(azx_dev, SD_CTL, azx_sd_readb(azx_dev, SD_CTL) |
1586 SD_CTL_STREAM_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 udelay(3);
1588 timeout = 300;
1589 while (!((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
1590 --timeout)
1591 ;
1592 val &= ~SD_CTL_STREAM_RESET;
1593 azx_sd_writeb(azx_dev, SD_CTL, val);
1594 udelay(3);
1595
1596 timeout = 300;
1597 /* waiting for hardware to report that the stream is out of reset */
1598 while (((val = azx_sd_readb(azx_dev, SD_CTL)) & SD_CTL_STREAM_RESET) &&
1599 --timeout)
1600 ;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02001601
1602 /* reset first position - may not be synced with hw at this time */
1603 *azx_dev->posbuf = 0;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Takashi Iwai1dddab42009-03-18 15:15:37 +01001606/*
1607 * set up the SD for streaming
1608 */
1609static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
1610{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001611 unsigned int val;
Takashi Iwai1dddab42009-03-18 15:15:37 +01001612 /* make sure the run bit is zero for SD */
1613 azx_stream_clear(chip, azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* program the stream_tag */
Takashi Iwai27fe48d92011-09-28 17:16:09 +02001615 val = azx_sd_readl(azx_dev, SD_CTL);
1616 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
1617 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
1618 if (!azx_snoop(chip))
1619 val |= SD_CTL_TRAFFIC_PRIO;
1620 azx_sd_writel(azx_dev, SD_CTL, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /* program the length of samples in cyclic buffer */
1623 azx_sd_writel(azx_dev, SD_CBL, azx_dev->bufsize);
1624
1625 /* program the stream format */
1626 /* this value needs to be the same as the one programmed */
1627 azx_sd_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
1628
1629 /* program the stream LVI (last valid index) of the BDL */
1630 azx_sd_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
1631
1632 /* program the BDL address */
1633 /* lower BDL address */
Takashi Iwai4ce107b2008-02-06 14:50:19 +01001634 azx_sd_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /* upper BDL address */
Takashi Iwai766979e2008-06-13 20:53:56 +02001636 azx_sd_writel(azx_dev, SD_BDLPU, upper_32_bits(azx_dev->bdl.addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02001638 /* enable the position buffer */
David Henningsson4cb36312010-09-30 10:12:50 +02001639 if (chip->position_fix[0] != POS_FIX_LPIB ||
1640 chip->position_fix[1] != POS_FIX_LPIB) {
Takashi Iwaiee9d6b92008-03-14 15:52:20 +01001641 if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
1642 azx_writel(chip, DPLBASE,
1643 (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
1644 }
Takashi Iwaic74db862005-05-12 14:26:27 +02001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /* set the interrupt enable bits in the descriptor control register */
Takashi Iwaid01ce992007-07-27 16:52:19 +02001647 azx_sd_writel(azx_dev, SD_CTL,
1648 azx_sd_readl(azx_dev, SD_CTL) | SD_INT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 return 0;
1651}
1652
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001653/*
1654 * Probe the given codec address
1655 */
1656static int probe_codec(struct azx *chip, int addr)
1657{
1658 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
1659 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
1660 unsigned int res;
1661
Wu Fengguanga678cde2009-08-01 18:46:46 +08001662 mutex_lock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001663 chip->probing = 1;
1664 azx_send_cmd(chip->bus, cmd);
Wu Fengguangdeadff12009-08-01 18:45:16 +08001665 res = azx_get_response(chip->bus, addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001666 chip->probing = 0;
Wu Fengguanga678cde2009-08-01 18:46:46 +08001667 mutex_unlock(&chip->bus->cmd_mutex);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001668 if (res == -1)
1669 return -EIO;
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001670 snd_printdd(SFX "%s: codec #%d probed OK\n", pci_name(chip->pci), addr);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001671 return 0;
1672}
1673
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001674static int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
1675 struct hda_pcm *cpcm);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001676static void azx_stop_chip(struct azx *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Takashi Iwai8dd78332009-06-02 01:16:07 +02001678static void azx_bus_reset(struct hda_bus *bus)
1679{
1680 struct azx *chip = bus->private_data;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001681
1682 bus->in_reset = 1;
1683 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001684 azx_init_chip(chip, 1);
Alexander Beregalov65f75982009-06-04 13:46:16 +04001685#ifdef CONFIG_PM
Takashi Iwai8dd78332009-06-02 01:16:07 +02001686 if (chip->initialized) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01001687 struct azx_pcm *p;
1688 list_for_each_entry(p, &chip->pcm_list, list)
1689 snd_pcm_suspend_all(p->pcm);
Takashi Iwai8dd78332009-06-02 01:16:07 +02001690 snd_hda_suspend(chip->bus);
1691 snd_hda_resume(chip->bus);
1692 }
Alexander Beregalov65f75982009-06-04 13:46:16 +04001693#endif
Takashi Iwai8dd78332009-06-02 01:16:07 +02001694 bus->in_reset = 0;
1695}
1696
David Henningsson26a6cb62012-10-09 15:04:21 +02001697static int get_jackpoll_interval(struct azx *chip)
1698{
1699 int i = jackpoll_ms[chip->dev_index];
1700 unsigned int j;
1701 if (i == 0)
1702 return 0;
1703 if (i < 50 || i > 60000)
1704 j = 0;
1705 else
1706 j = msecs_to_jiffies(i);
1707 if (j == 0)
1708 snd_printk(KERN_WARNING SFX
1709 "jackpoll_ms value out of range: %d\n", i);
1710 return j;
1711}
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713/*
1714 * Codec initialization
1715 */
1716
Takashi Iwai2f5983f2008-09-03 16:00:44 +02001717/* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01001718static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] = {
Wei Ni7445dfc2010-03-03 15:05:53 +08001719 [AZX_DRIVER_NVIDIA] = 8,
Kailang Yangf2690022008-05-27 11:44:55 +02001720 [AZX_DRIVER_TERA] = 1,
Takashi Iwaia9995a32007-03-12 21:30:46 +01001721};
1722
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01001723static int azx_codec_create(struct azx *chip, const char *model)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 struct hda_bus_template bus_temp;
Takashi Iwai34c25352008-10-28 11:38:58 +01001726 int c, codecs, err;
1727 int max_slots;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 memset(&bus_temp, 0, sizeof(bus_temp));
1730 bus_temp.private_data = chip;
1731 bus_temp.modelname = model;
1732 bus_temp.pci = chip->pci;
Takashi Iwai111d3af2006-02-16 18:17:58 +01001733 bus_temp.ops.command = azx_send_cmd;
1734 bus_temp.ops.get_response = azx_get_response;
Takashi Iwai176d5332008-07-30 15:01:44 +02001735 bus_temp.ops.attach_pcm = azx_attach_pcm_stream;
Takashi Iwai8dd78332009-06-02 01:16:07 +02001736 bus_temp.ops.bus_reset = azx_bus_reset;
Takashi Iwai83012a72012-08-24 18:38:08 +02001737#ifdef CONFIG_PM
Takashi Iwai11cd41b2008-11-28 07:22:18 +01001738 bus_temp.power_save = &power_save;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001739 bus_temp.ops.pm_notify = azx_power_notify;
1740#endif
Takashi Iwai1d1a4562012-09-20 20:29:13 -07001741#ifdef CONFIG_SND_HDA_DSP_LOADER
1742 bus_temp.ops.load_dsp_prepare = azx_load_dsp_prepare;
1743 bus_temp.ops.load_dsp_trigger = azx_load_dsp_trigger;
1744 bus_temp.ops.load_dsp_cleanup = azx_load_dsp_cleanup;
1745#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Takashi Iwaid01ce992007-07-27 16:52:19 +02001747 err = snd_hda_bus_new(chip->card, &bus_temp, &chip->bus);
1748 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return err;
1750
Takashi Iwai9477c582011-05-25 09:11:37 +02001751 if (chip->driver_caps & AZX_DCAPS_RIRB_DELAY) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001752 snd_printd(SFX "%s: Enable delay in RIRB handling\n", pci_name(chip->pci));
Wei Nidc9c8e22008-09-26 13:55:56 +08001753 chip->bus->needs_damn_long_delay = 1;
Takashi Iwai9477c582011-05-25 09:11:37 +02001754 }
Wei Nidc9c8e22008-09-26 13:55:56 +08001755
Takashi Iwai34c25352008-10-28 11:38:58 +01001756 codecs = 0;
Takashi Iwai2f5983f2008-09-03 16:00:44 +02001757 max_slots = azx_max_codecs[chip->driver_type];
1758 if (!max_slots)
Wei Ni7445dfc2010-03-03 15:05:53 +08001759 max_slots = AZX_DEFAULT_CODECS;
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001760
1761 /* First try to probe all given codec slots */
1762 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001763 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001764 if (probe_codec(chip, c) < 0) {
1765 /* Some BIOSen give you wrong codec addresses
1766 * that don't exist
1767 */
Takashi Iwai4abc1cc2009-05-19 12:16:46 +02001768 snd_printk(KERN_WARNING SFX
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001769 "%s: Codec #%d probe error; "
1770 "disabling it...\n", pci_name(chip->pci), c);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001771 chip->codec_mask &= ~(1 << c);
1772 /* More badly, accessing to a non-existing
1773 * codec often screws up the controller chip,
Paul Menzel24481582010-02-08 20:37:26 +01001774 * and disturbs the further communications.
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001775 * Thus if an error occurs during probing,
1776 * better to reset the controller chip to
1777 * get back to the sanity state.
1778 */
1779 azx_stop_chip(chip);
Jaroslav Kyselacd508fe2010-03-26 10:28:46 +01001780 azx_init_chip(chip, 1);
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001781 }
1782 }
1783 }
1784
Takashi Iwaid507cd62011-04-26 15:25:02 +02001785 /* AMD chipsets often cause the communication stalls upon certain
1786 * sequence like the pin-detection. It seems that forcing the synced
1787 * access works around the stall. Grrr...
1788 */
Takashi Iwai9477c582011-05-25 09:11:37 +02001789 if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001790 snd_printd(SFX "%s: Enable sync_write for stable communication\n",
1791 pci_name(chip->pci));
Takashi Iwaid507cd62011-04-26 15:25:02 +02001792 chip->bus->sync_write = 1;
1793 chip->bus->allow_bus_reset = 1;
1794 }
1795
Takashi Iwai6ce4a3b2008-11-06 17:11:10 +01001796 /* Then create codec instances */
Takashi Iwai34c25352008-10-28 11:38:58 +01001797 for (c = 0; c < max_slots; c++) {
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01001798 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
Takashi Iwaibccad142007-04-24 12:23:53 +02001799 struct hda_codec *codec;
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001800 err = snd_hda_codec_new(chip->bus, c, &codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (err < 0)
1802 continue;
David Henningsson26a6cb62012-10-09 15:04:21 +02001803 codec->jackpoll_interval = get_jackpoll_interval(chip);
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01001804 codec->beep_mode = chip->beep_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 codecs++;
Takashi Iwai19a982b2007-03-21 15:14:35 +01001806 }
1807 }
1808 if (!codecs) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08001809 snd_printk(KERN_ERR SFX "%s: no codecs initialized\n", pci_name(chip->pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return -ENXIO;
1811 }
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001812 return 0;
1813}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001815/* configure each codec instance */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001816static int azx_codec_configure(struct azx *chip)
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001817{
1818 struct hda_codec *codec;
1819 list_for_each_entry(codec, &chip->bus->codec_list, list) {
1820 snd_hda_codec_configure(codec);
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return 0;
1823}
1824
1825
1826/*
1827 * PCM support
1828 */
1829
1830/* assign a stream for the PCM */
Wu Fengguangef18bed2009-12-25 13:14:27 +08001831static inline struct azx_dev *
1832azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001834 int dev, i, nums;
Wu Fengguangef18bed2009-12-25 13:14:27 +08001835 struct azx_dev *res = NULL;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001836 /* make a non-zero unique key for the substream */
1837 int key = (substream->pcm->device << 16) | (substream->number << 2) |
1838 (substream->stream + 1);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001839
1840 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai07e4ca52005-08-24 14:14:57 +02001841 dev = chip->playback_index_offset;
1842 nums = chip->playback_streams;
1843 } else {
1844 dev = chip->capture_index_offset;
1845 nums = chip->capture_streams;
1846 }
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001847 for (i = 0; i < nums; i++, dev++) {
1848 struct azx_dev *azx_dev = &chip->azx_dev[dev];
1849 dsp_lock(azx_dev);
1850 if (!azx_dev->opened && !dsp_is_locked(azx_dev)) {
1851 res = azx_dev;
1852 if (res->assigned_key == key) {
1853 res->opened = 1;
1854 res->assigned_key = key;
1855 dsp_unlock(azx_dev);
1856 return azx_dev;
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001859 dsp_unlock(azx_dev);
1860 }
Wu Fengguangef18bed2009-12-25 13:14:27 +08001861 if (res) {
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001862 dsp_lock(res);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001863 res->opened = 1;
Takashi Iwaid5cf9912011-10-06 10:07:58 +02001864 res->assigned_key = key;
Takashi Iwaieb49faa2013-03-15 09:19:11 +01001865 dsp_unlock(res);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001866 }
1867 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870/* release the assigned stream */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001871static inline void azx_release_device(struct azx_dev *azx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 azx_dev->opened = 0;
1874}
1875
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001876static cycle_t azx_cc_read(const struct cyclecounter *cc)
1877{
1878 struct azx_dev *azx_dev = container_of(cc, struct azx_dev, azx_cc);
1879 struct snd_pcm_substream *substream = azx_dev->substream;
1880 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1881 struct azx *chip = apcm->chip;
1882
1883 return azx_readl(chip, WALLCLK);
1884}
1885
1886static void azx_timecounter_init(struct snd_pcm_substream *substream,
1887 bool force, cycle_t last)
1888{
1889 struct azx_dev *azx_dev = get_azx_dev(substream);
1890 struct timecounter *tc = &azx_dev->azx_tc;
1891 struct cyclecounter *cc = &azx_dev->azx_cc;
1892 u64 nsec;
1893
1894 cc->read = azx_cc_read;
1895 cc->mask = CLOCKSOURCE_MASK(32);
1896
1897 /*
1898 * Converting from 24 MHz to ns means applying a 125/3 factor.
1899 * To avoid any saturation issues in intermediate operations,
1900 * the 125 factor is applied first. The division is applied
1901 * last after reading the timecounter value.
1902 * Applying the 1/3 factor as part of the multiplication
1903 * requires at least 20 bits for a decent precision, however
1904 * overflows occur after about 4 hours or less, not a option.
1905 */
1906
1907 cc->mult = 125; /* saturation after 195 years */
1908 cc->shift = 0;
1909
1910 nsec = 0; /* audio time is elapsed time since trigger */
1911 timecounter_init(tc, cc, nsec);
1912 if (force)
1913 /*
1914 * force timecounter to use predefined value,
1915 * used for synchronized starts
1916 */
1917 tc->cycle_last = last;
1918}
1919
Dylan Reidae03bbb2013-04-15 11:57:05 -07001920static u64 azx_adjust_codec_delay(struct snd_pcm_substream *substream,
Dylan Reid78daea22013-04-08 18:20:30 -07001921 u64 nsec)
1922{
1923 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1924 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
1925 u64 codec_frames, codec_nsecs;
1926
1927 if (!hinfo->ops.get_delay)
1928 return nsec;
1929
1930 codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
1931 codec_nsecs = div_u64(codec_frames * 1000000000LL,
1932 substream->runtime->rate);
1933
Dylan Reidae03bbb2013-04-15 11:57:05 -07001934 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1935 return nsec + codec_nsecs;
1936
Dylan Reid78daea22013-04-08 18:20:30 -07001937 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
1938}
1939
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001940static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
1941 struct timespec *ts)
1942{
1943 struct azx_dev *azx_dev = get_azx_dev(substream);
1944 u64 nsec;
1945
1946 nsec = timecounter_read(&azx_dev->azx_tc);
1947 nsec = div_u64(nsec, 3); /* can be optimized */
Dylan Reidae03bbb2013-04-15 11:57:05 -07001948 nsec = azx_adjust_codec_delay(substream, nsec);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001949
1950 *ts = ns_to_timespec(nsec);
1951
1952 return 0;
1953}
1954
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001955static struct snd_pcm_hardware azx_pcm_hw = {
Takashi Iwaid01ce992007-07-27 16:52:19 +02001956 .info = (SNDRV_PCM_INFO_MMAP |
1957 SNDRV_PCM_INFO_INTERLEAVED |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1959 SNDRV_PCM_INFO_MMAP_VALID |
Pavel Machek927fc862006-08-31 17:03:43 +02001960 /* No full-resume yet implemented */
1961 /* SNDRV_PCM_INFO_RESUME |*/
Takashi Iwai850f0e52008-03-18 17:11:05 +01001962 SNDRV_PCM_INFO_PAUSE |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001963 SNDRV_PCM_INFO_SYNC_START |
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05001964 SNDRV_PCM_INFO_HAS_WALL_CLOCK |
Clemens Ladisch7bb8fb72010-11-15 10:49:47 +01001965 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1967 .rates = SNDRV_PCM_RATE_48000,
1968 .rate_min = 48000,
1969 .rate_max = 48000,
1970 .channels_min = 2,
1971 .channels_max = 2,
1972 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
1973 .period_bytes_min = 128,
1974 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
1975 .periods_min = 2,
1976 .periods_max = AZX_MAX_FRAG,
1977 .fifo_size = 0,
1978};
1979
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001980static int azx_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1983 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01001984 struct azx *chip = apcm->chip;
1985 struct azx_dev *azx_dev;
1986 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 unsigned long flags;
1988 int err;
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05001989 int buff_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Ingo Molnar62932df2006-01-16 16:34:20 +01001991 mutex_lock(&chip->open_mutex);
Wu Fengguangef18bed2009-12-25 13:14:27 +08001992 azx_dev = azx_assign_device(chip, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (azx_dev == NULL) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001994 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return -EBUSY;
1996 }
1997 runtime->hw = azx_pcm_hw;
1998 runtime->hw.channels_min = hinfo->channels_min;
1999 runtime->hw.channels_max = hinfo->channels_max;
2000 runtime->hw.formats = hinfo->formats;
2001 runtime->hw.rates = hinfo->rates;
2002 snd_pcm_limit_hw_rates(runtime);
2003 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05002004
2005 /* avoid wrap-around with wall-clock */
2006 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2007 20,
2008 178000000);
2009
Takashi Iwai52409aa2012-01-23 17:10:24 +01002010 if (chip->align_buffer_size)
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05002011 /* constrain buffer sizes to be multiple of 128
2012 bytes. This is more efficient in terms of memory
2013 access but isn't required by the HDA spec and
2014 prevents users from specifying exact period/buffer
2015 sizes. For example for 44.1kHz, a period size set
2016 to 20ms will be rounded to 19.59ms. */
2017 buff_step = 128;
2018 else
2019 /* Don't enforce steps on buffer sizes, still need to
2020 be multiple of 4 bytes (HDA spec). Tested on Intel
2021 HDA controllers, may not work on all devices where
2022 option needs to be disabled */
2023 buff_step = 4;
2024
Joachim Deguara5f1545b2007-03-16 15:01:36 +01002025 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05002026 buff_step);
Joachim Deguara5f1545b2007-03-16 15:01:36 +01002027 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05002028 buff_step);
Dylan Reidb4a91cf2012-06-15 19:36:23 -07002029 snd_hda_power_up_d3wait(apcm->codec);
Takashi Iwaid01ce992007-07-27 16:52:19 +02002030 err = hinfo->ops.open(hinfo, apcm->codec, substream);
2031 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 azx_release_device(azx_dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002033 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01002034 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return err;
2036 }
Takashi Iwai70d321e2009-07-03 23:06:45 +02002037 snd_pcm_limit_hw_rates(runtime);
Takashi Iwaiaba66532009-07-05 11:44:46 +02002038 /* sanity check */
2039 if (snd_BUG_ON(!runtime->hw.channels_min) ||
2040 snd_BUG_ON(!runtime->hw.channels_max) ||
2041 snd_BUG_ON(!runtime->hw.formats) ||
2042 snd_BUG_ON(!runtime->hw.rates)) {
2043 azx_release_device(azx_dev);
2044 hinfo->ops.close(hinfo, apcm->codec, substream);
2045 snd_hda_power_down(apcm->codec);
2046 mutex_unlock(&chip->open_mutex);
2047 return -EINVAL;
2048 }
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05002049
2050 /* disable WALLCLOCK timestamps for capture streams
2051 until we figure out how to handle digital inputs */
2052 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2053 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK;
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 spin_lock_irqsave(&chip->reg_lock, flags);
2056 azx_dev->substream = substream;
2057 azx_dev->running = 0;
2058 spin_unlock_irqrestore(&chip->reg_lock, flags);
2059
2060 runtime->private_data = azx_dev;
Takashi Iwai850f0e52008-03-18 17:11:05 +01002061 snd_pcm_set_sync(substream);
Ingo Molnar62932df2006-01-16 16:34:20 +01002062 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return 0;
2064}
2065
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002066static int azx_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2069 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002070 struct azx *chip = apcm->chip;
2071 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 unsigned long flags;
2073
Ingo Molnar62932df2006-01-16 16:34:20 +01002074 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 spin_lock_irqsave(&chip->reg_lock, flags);
2076 azx_dev->substream = NULL;
2077 azx_dev->running = 0;
2078 spin_unlock_irqrestore(&chip->reg_lock, flags);
2079 azx_release_device(azx_dev);
2080 hinfo->ops.close(hinfo, apcm->codec, substream);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002081 snd_hda_power_down(apcm->codec);
Ingo Molnar62932df2006-01-16 16:34:20 +01002082 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return 0;
2084}
2085
Takashi Iwaid01ce992007-07-27 16:52:19 +02002086static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
2087 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002089 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2090 struct azx *chip = apcm->chip;
Takashi Iwai97b71c92009-03-18 15:09:13 +01002091 struct azx_dev *azx_dev = get_azx_dev(substream);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002092 int ret;
Takashi Iwai97b71c92009-03-18 15:09:13 +01002093
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002094 dsp_lock(azx_dev);
2095 if (dsp_is_locked(azx_dev)) {
2096 ret = -EBUSY;
2097 goto unlock;
2098 }
2099
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +01002100 mark_runtime_wc(chip, azx_dev, substream, false);
Takashi Iwai97b71c92009-03-18 15:09:13 +01002101 azx_dev->bufsize = 0;
2102 azx_dev->period_bytes = 0;
2103 azx_dev->format_val = 0;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002104 ret = snd_pcm_lib_malloc_pages(substream,
Takashi Iwaid01ce992007-07-27 16:52:19 +02002105 params_buffer_bytes(hw_params));
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002106 if (ret < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002107 goto unlock;
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +01002108 mark_runtime_wc(chip, azx_dev, substream, true);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002109 unlock:
2110 dsp_unlock(azx_dev);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002114static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002117 struct azx_dev *azx_dev = get_azx_dev(substream);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002118 struct azx *chip = apcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
2120
2121 /* reset BDL address */
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002122 dsp_lock(azx_dev);
2123 if (!dsp_is_locked(azx_dev)) {
2124 azx_sd_writel(azx_dev, SD_BDLPL, 0);
2125 azx_sd_writel(azx_dev, SD_BDLPU, 0);
2126 azx_sd_writel(azx_dev, SD_CTL, 0);
2127 azx_dev->bufsize = 0;
2128 azx_dev->period_bytes = 0;
2129 azx_dev->format_val = 0;
2130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Takashi Iwaieb541332010-08-06 13:48:11 +02002132 snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Takashi Iwai9ddf1ae2013-01-29 18:07:22 +01002134 mark_runtime_wc(chip, azx_dev, substream, false);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002135 azx_dev->prepared = 0;
2136 dsp_unlock(azx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return snd_pcm_lib_free_pages(substream);
2138}
2139
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002140static int azx_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002143 struct azx *chip = apcm->chip;
2144 struct azx_dev *azx_dev = get_azx_dev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002146 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02002147 unsigned int bufsize, period_bytes, format_val, stream_tag;
Takashi Iwai97b71c92009-03-18 15:09:13 +01002148 int err;
Stephen Warren7c9359762011-06-01 11:14:17 -06002149 struct hda_spdif_out *spdif =
2150 snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid);
2151 unsigned short ctls = spdif ? spdif->ctls : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002153 dsp_lock(azx_dev);
2154 if (dsp_is_locked(azx_dev)) {
2155 err = -EBUSY;
2156 goto unlock;
2157 }
2158
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002159 azx_stream_reset(chip, azx_dev);
Takashi Iwai97b71c92009-03-18 15:09:13 +01002160 format_val = snd_hda_calc_stream_format(runtime->rate,
2161 runtime->channels,
2162 runtime->format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03002163 hinfo->maxbps,
Stephen Warren7c9359762011-06-01 11:14:17 -06002164 ctls);
Takashi Iwai97b71c92009-03-18 15:09:13 +01002165 if (!format_val) {
Takashi Iwaid01ce992007-07-27 16:52:19 +02002166 snd_printk(KERN_ERR SFX
Daniel J Blueman445a51b2012-12-05 23:04:21 +08002167 "%s: invalid format_val, rate=%d, ch=%d, format=%d\n",
2168 pci_name(chip->pci), runtime->rate, runtime->channels, runtime->format);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002169 err = -EINVAL;
2170 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172
Takashi Iwai97b71c92009-03-18 15:09:13 +01002173 bufsize = snd_pcm_lib_buffer_bytes(substream);
2174 period_bytes = snd_pcm_lib_period_bytes(substream);
2175
Daniel J Blueman445a51b2012-12-05 23:04:21 +08002176 snd_printdd(SFX "%s: azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
2177 pci_name(chip->pci), bufsize, format_val);
Takashi Iwai97b71c92009-03-18 15:09:13 +01002178
2179 if (bufsize != azx_dev->bufsize ||
2180 period_bytes != azx_dev->period_bytes ||
Takashi Iwai915bf292012-09-11 15:19:10 +02002181 format_val != azx_dev->format_val ||
2182 runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
Takashi Iwai97b71c92009-03-18 15:09:13 +01002183 azx_dev->bufsize = bufsize;
2184 azx_dev->period_bytes = period_bytes;
2185 azx_dev->format_val = format_val;
Takashi Iwai915bf292012-09-11 15:19:10 +02002186 azx_dev->no_period_wakeup = runtime->no_period_wakeup;
Takashi Iwai97b71c92009-03-18 15:09:13 +01002187 err = azx_setup_periods(chip, substream, azx_dev);
2188 if (err < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002189 goto unlock;
Takashi Iwai97b71c92009-03-18 15:09:13 +01002190 }
2191
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002192 /* wallclk has 24Mhz clock source */
2193 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
2194 runtime->rate) * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 azx_setup_controller(chip, azx_dev);
2196 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2197 azx_dev->fifo_size = azx_sd_readw(azx_dev, SD_FIFOSIZE) + 1;
2198 else
2199 azx_dev->fifo_size = 0;
2200
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02002201 stream_tag = azx_dev->stream_tag;
2202 /* CA-IBG chips need the playback stream starting from 1 */
Takashi Iwai9477c582011-05-25 09:11:37 +02002203 if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02002204 stream_tag > chip->capture_streams)
2205 stream_tag -= chip->capture_streams;
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002206 err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
Takashi Iwaieb541332010-08-06 13:48:11 +02002207 azx_dev->format_val, substream);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002208
2209 unlock:
2210 if (!err)
2211 azx_dev->prepared = 1;
2212 dsp_unlock(azx_dev);
2213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002216static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002219 struct azx *chip = apcm->chip;
Takashi Iwai850f0e52008-03-18 17:11:05 +01002220 struct azx_dev *azx_dev;
2221 struct snd_pcm_substream *s;
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002222 int rstart = 0, start, nsync = 0, sbits = 0;
Takashi Iwai850f0e52008-03-18 17:11:05 +01002223 int nwait, timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Takashi Iwai1a8506d2012-10-16 15:10:08 +02002225 azx_dev = get_azx_dev(substream);
2226 trace_azx_pcm_trigger(chip, azx_dev, cmd);
2227
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002228 if (dsp_is_locked(azx_dev) || !azx_dev->prepared)
2229 return -EPIPE;
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 switch (cmd) {
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002232 case SNDRV_PCM_TRIGGER_START:
2233 rstart = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2235 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai850f0e52008-03-18 17:11:05 +01002236 start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 break;
2238 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jaroslav Kysela47123192005-08-15 20:53:07 +02002239 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai850f0e52008-03-18 17:11:05 +01002241 start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 break;
2243 default:
Takashi Iwai850f0e52008-03-18 17:11:05 +01002244 return -EINVAL;
2245 }
2246
2247 snd_pcm_group_for_each_entry(s, substream) {
2248 if (s->pcm->card != substream->pcm->card)
2249 continue;
2250 azx_dev = get_azx_dev(s);
2251 sbits |= 1 << azx_dev->index;
2252 nsync++;
2253 snd_pcm_trigger_done(s, substream);
2254 }
2255
2256 spin_lock(&chip->reg_lock);
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05002257
2258 /* first, set SYNC bits of corresponding streams */
2259 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
2260 azx_writel(chip, OLD_SSYNC,
2261 azx_readl(chip, OLD_SSYNC) | sbits);
2262 else
2263 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) | sbits);
2264
Takashi Iwai850f0e52008-03-18 17:11:05 +01002265 snd_pcm_group_for_each_entry(s, substream) {
2266 if (s->pcm->card != substream->pcm->card)
2267 continue;
2268 azx_dev = get_azx_dev(s);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002269 if (start) {
2270 azx_dev->start_wallclk = azx_readl(chip, WALLCLK);
2271 if (!rstart)
2272 azx_dev->start_wallclk -=
2273 azx_dev->period_wallclk;
Takashi Iwai850f0e52008-03-18 17:11:05 +01002274 azx_stream_start(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002275 } else {
Takashi Iwai850f0e52008-03-18 17:11:05 +01002276 azx_stream_stop(chip, azx_dev);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002277 }
Takashi Iwai850f0e52008-03-18 17:11:05 +01002278 azx_dev->running = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01002281 if (start) {
Takashi Iwai850f0e52008-03-18 17:11:05 +01002282 /* wait until all FIFOs get ready */
2283 for (timeout = 5000; timeout; timeout--) {
2284 nwait = 0;
2285 snd_pcm_group_for_each_entry(s, substream) {
2286 if (s->pcm->card != substream->pcm->card)
2287 continue;
2288 azx_dev = get_azx_dev(s);
2289 if (!(azx_sd_readb(azx_dev, SD_STS) &
2290 SD_STS_FIFO_READY))
2291 nwait++;
2292 }
2293 if (!nwait)
2294 break;
2295 cpu_relax();
2296 }
2297 } else {
2298 /* wait until all RUN bits are cleared */
2299 for (timeout = 5000; timeout; timeout--) {
2300 nwait = 0;
2301 snd_pcm_group_for_each_entry(s, substream) {
2302 if (s->pcm->card != substream->pcm->card)
2303 continue;
2304 azx_dev = get_azx_dev(s);
2305 if (azx_sd_readb(azx_dev, SD_CTL) &
2306 SD_CTL_DMA_START)
2307 nwait++;
2308 }
2309 if (!nwait)
2310 break;
2311 cpu_relax();
2312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05002314 spin_lock(&chip->reg_lock);
2315 /* reset SYNC bits */
2316 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
2317 azx_writel(chip, OLD_SSYNC,
2318 azx_readl(chip, OLD_SSYNC) & ~sbits);
2319 else
2320 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) & ~sbits);
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05002321 if (start) {
2322 azx_timecounter_init(substream, 0, 0);
2323 if (nsync > 1) {
2324 cycle_t cycle_last;
2325
2326 /* same start cycle for master and group */
2327 azx_dev = get_azx_dev(substream);
2328 cycle_last = azx_dev->azx_tc.cycle_last;
2329
2330 snd_pcm_group_for_each_entry(s, substream) {
2331 if (s->pcm->card != substream->pcm->card)
2332 continue;
2333 azx_timecounter_init(s, 1, cycle_last);
2334 }
2335 }
2336 }
Pierre-Louis Bossart172d3b22012-09-21 18:39:05 -05002337 spin_unlock(&chip->reg_lock);
Takashi Iwai850f0e52008-03-18 17:11:05 +01002338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Joseph Chan0e153472008-08-26 14:38:03 +02002341/* get the current DMA position with correction on VIA chips */
2342static unsigned int azx_via_get_position(struct azx *chip,
2343 struct azx_dev *azx_dev)
2344{
2345 unsigned int link_pos, mini_pos, bound_pos;
2346 unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
2347 unsigned int fifo_size;
2348
2349 link_pos = azx_sd_readl(azx_dev, SD_LPIB);
Takashi Iwaib4a655e2011-06-07 12:26:56 +02002350 if (azx_dev->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Joseph Chan0e153472008-08-26 14:38:03 +02002351 /* Playback, no problem using link position */
2352 return link_pos;
2353 }
2354
2355 /* Capture */
2356 /* For new chipset,
2357 * use mod to get the DMA position just like old chipset
2358 */
2359 mod_dma_pos = le32_to_cpu(*azx_dev->posbuf);
2360 mod_dma_pos %= azx_dev->period_bytes;
2361
2362 /* azx_dev->fifo_size can't get FIFO size of in stream.
2363 * Get from base address + offset.
2364 */
2365 fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
2366
2367 if (azx_dev->insufficient) {
2368 /* Link position never gather than FIFO size */
2369 if (link_pos <= fifo_size)
2370 return 0;
2371
2372 azx_dev->insufficient = 0;
2373 }
2374
2375 if (link_pos <= fifo_size)
2376 mini_pos = azx_dev->bufsize + link_pos - fifo_size;
2377 else
2378 mini_pos = link_pos - fifo_size;
2379
2380 /* Find nearest previous boudary */
2381 mod_mini_pos = mini_pos % azx_dev->period_bytes;
2382 mod_link_pos = link_pos % azx_dev->period_bytes;
2383 if (mod_link_pos >= fifo_size)
2384 bound_pos = link_pos - mod_link_pos;
2385 else if (mod_dma_pos >= mod_mini_pos)
2386 bound_pos = mini_pos - mod_mini_pos;
2387 else {
2388 bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes;
2389 if (bound_pos >= azx_dev->bufsize)
2390 bound_pos = 0;
2391 }
2392
2393 /* Calculate real DMA position we want */
2394 return bound_pos + mod_dma_pos;
2395}
2396
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002397static unsigned int azx_get_position(struct azx *chip,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002398 struct azx_dev *azx_dev,
2399 bool with_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Takashi Iwai21229612013-04-05 07:27:45 +02002401 struct snd_pcm_substream *substream = azx_dev->substream;
2402 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 unsigned int pos;
Takashi Iwai21229612013-04-05 07:27:45 +02002404 int stream = substream->stream;
2405 struct hda_pcm_stream *hinfo = apcm->hinfo[stream];
Takashi Iwai1a8506d2012-10-16 15:10:08 +02002406 int delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
David Henningsson4cb36312010-09-30 10:12:50 +02002408 switch (chip->position_fix[stream]) {
2409 case POS_FIX_LPIB:
2410 /* read LPIB */
2411 pos = azx_sd_readl(azx_dev, SD_LPIB);
2412 break;
2413 case POS_FIX_VIACOMBO:
Joseph Chan0e153472008-08-26 14:38:03 +02002414 pos = azx_via_get_position(chip, azx_dev);
David Henningsson4cb36312010-09-30 10:12:50 +02002415 break;
2416 default:
2417 /* use the position buffer */
2418 pos = le32_to_cpu(*azx_dev->posbuf);
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002419 if (with_check && chip->position_fix[stream] == POS_FIX_AUTO) {
Takashi Iwaia8103642011-06-07 12:23:23 +02002420 if (!pos || pos == (u32)-1) {
2421 printk(KERN_WARNING
2422 "hda-intel: Invalid position buffer, "
2423 "using LPIB read method instead.\n");
2424 chip->position_fix[stream] = POS_FIX_LPIB;
2425 pos = azx_sd_readl(azx_dev, SD_LPIB);
2426 } else
2427 chip->position_fix[stream] = POS_FIX_POSBUF;
2428 }
2429 break;
Takashi Iwaic74db862005-05-12 14:26:27 +02002430 }
David Henningsson4cb36312010-09-30 10:12:50 +02002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (pos >= azx_dev->bufsize)
2433 pos = 0;
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002434
2435 /* calculate runtime delay from LPIB */
Takashi Iwai21229612013-04-05 07:27:45 +02002436 if (substream->runtime &&
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002437 chip->position_fix[stream] == POS_FIX_POSBUF &&
2438 (chip->driver_caps & AZX_DCAPS_COUNT_LPIB_DELAY)) {
2439 unsigned int lpib_pos = azx_sd_readl(azx_dev, SD_LPIB);
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002440 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2441 delay = pos - lpib_pos;
2442 else
2443 delay = lpib_pos - pos;
2444 if (delay < 0)
2445 delay += azx_dev->bufsize;
2446 if (delay >= azx_dev->period_bytes) {
Takashi Iwai1f046612012-10-16 16:52:26 +02002447 snd_printk(KERN_WARNING SFX
Daniel J Blueman445a51b2012-12-05 23:04:21 +08002448 "%s: Unstable LPIB (%d >= %d); "
Takashi Iwai1f046612012-10-16 16:52:26 +02002449 "disabling LPIB delay counting\n",
Daniel J Blueman445a51b2012-12-05 23:04:21 +08002450 pci_name(chip->pci), delay, azx_dev->period_bytes);
Takashi Iwai1f046612012-10-16 16:52:26 +02002451 delay = 0;
2452 chip->driver_caps &= ~AZX_DCAPS_COUNT_LPIB_DELAY;
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002453 }
Takashi Iwai21229612013-04-05 07:27:45 +02002454 delay = bytes_to_frames(substream->runtime, delay);
Pierre-Louis Bossart90accc52012-09-21 18:39:06 -05002455 }
Takashi Iwai21229612013-04-05 07:27:45 +02002456
2457 if (substream->runtime) {
2458 if (hinfo->ops.get_delay)
2459 delay += hinfo->ops.get_delay(hinfo, apcm->codec,
2460 substream);
2461 substream->runtime->delay = delay;
2462 }
2463
Takashi Iwai1a8506d2012-10-16 15:10:08 +02002464 trace_azx_get_position(chip, azx_dev, pos, delay);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002465 return pos;
2466}
2467
2468static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
2469{
2470 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2471 struct azx *chip = apcm->chip;
2472 struct azx_dev *azx_dev = get_azx_dev(substream);
2473 return bytes_to_frames(substream->runtime,
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002474 azx_get_position(chip, azx_dev, false));
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002475}
2476
2477/*
2478 * Check whether the current DMA position is acceptable for updating
2479 * periods. Returns non-zero if it's OK.
2480 *
2481 * Many HD-audio controllers appear pretty inaccurate about
2482 * the update-IRQ timing. The IRQ is issued before actually the
2483 * data is processed. So, we need to process it afterwords in a
2484 * workqueue.
2485 */
2486static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
2487{
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002488 u32 wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002489 unsigned int pos;
2490
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002491 wallclk = azx_readl(chip, WALLCLK) - azx_dev->start_wallclk;
2492 if (wallclk < (azx_dev->period_wallclk * 2) / 3)
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002493 return -1; /* bogus (too early) interrupt */
Jaroslav Kyselafa00e042009-04-10 12:20:45 +02002494
Takashi Iwai798cb7e2011-09-30 08:52:26 +02002495 pos = azx_get_position(chip, azx_dev, true);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002496
Takashi Iwaid6d8bf52010-02-12 18:17:06 +01002497 if (WARN_ONCE(!azx_dev->period_bytes,
2498 "hda-intel: zero azx_dev->period_bytes"))
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002499 return -1; /* this shouldn't happen! */
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002500 if (wallclk < (azx_dev->period_wallclk * 5) / 4 &&
Jaroslav Kyselaf48f6062010-05-11 12:10:47 +02002501 pos % azx_dev->period_bytes > azx_dev->period_bytes / 2)
2502 /* NG - it's below the first next period boundary */
2503 return bdl_pos_adj[chip->dev_index] ? 0 : -1;
Jaroslav Kyselaedb39932010-06-02 13:29:17 +02002504 azx_dev->start_wallclk += wallclk;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002505 return 1; /* OK, it's fine */
2506}
2507
2508/*
2509 * The work for pending PCM period updates.
2510 */
2511static void azx_irq_pending_work(struct work_struct *work)
2512{
2513 struct azx *chip = container_of(work, struct azx, irq_pending_work);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002514 int i, pending, ok;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002515
Takashi Iwaia6a950a2008-06-10 17:53:35 +02002516 if (!chip->irq_pending_warned) {
2517 printk(KERN_WARNING
2518 "hda-intel: IRQ timing workaround is activated "
2519 "for card #%d. Suggest a bigger bdl_pos_adj.\n",
2520 chip->card->number);
2521 chip->irq_pending_warned = 1;
2522 }
2523
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002524 for (;;) {
2525 pending = 0;
2526 spin_lock_irq(&chip->reg_lock);
2527 for (i = 0; i < chip->num_streams; i++) {
2528 struct azx_dev *azx_dev = &chip->azx_dev[i];
2529 if (!azx_dev->irq_pending ||
2530 !azx_dev->substream ||
2531 !azx_dev->running)
2532 continue;
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002533 ok = azx_position_ok(chip, azx_dev);
2534 if (ok > 0) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002535 azx_dev->irq_pending = 0;
2536 spin_unlock(&chip->reg_lock);
2537 snd_pcm_period_elapsed(azx_dev->substream);
2538 spin_lock(&chip->reg_lock);
Jaroslav Kyselae5463722010-05-11 10:21:46 +02002539 } else if (ok < 0) {
2540 pending = 0; /* too early */
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002541 } else
2542 pending++;
2543 }
2544 spin_unlock_irq(&chip->reg_lock);
2545 if (!pending)
2546 return;
Takashi Iwai08af4952010-08-03 14:39:04 +02002547 msleep(1);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002548 }
2549}
2550
2551/* clear irq_pending flags and assure no on-going workq */
2552static void azx_clear_irq_pending(struct azx *chip)
2553{
2554 int i;
2555
2556 spin_lock_irq(&chip->reg_lock);
2557 for (i = 0; i < chip->num_streams; i++)
2558 chip->azx_dev[i].irq_pending = 0;
2559 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002562#ifdef CONFIG_X86
2563static int azx_pcm_mmap(struct snd_pcm_substream *substream,
2564 struct vm_area_struct *area)
2565{
2566 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
2567 struct azx *chip = apcm->chip;
2568 if (!azx_snoop(chip))
2569 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
2570 return snd_pcm_lib_default_mmap(substream, area);
2571}
2572#else
2573#define azx_pcm_mmap NULL
2574#endif
2575
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002576static struct snd_pcm_ops azx_pcm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 .open = azx_pcm_open,
2578 .close = azx_pcm_close,
2579 .ioctl = snd_pcm_lib_ioctl,
2580 .hw_params = azx_pcm_hw_params,
2581 .hw_free = azx_pcm_hw_free,
2582 .prepare = azx_pcm_prepare,
2583 .trigger = azx_pcm_trigger,
2584 .pointer = azx_pcm_pointer,
Pierre-Louis Bossart5d890f52012-10-22 16:42:16 -05002585 .wall_clock = azx_get_wallclock_tstamp,
Takashi Iwai27fe48d92011-09-28 17:16:09 +02002586 .mmap = azx_pcm_mmap,
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002587 .page = snd_pcm_sgbuf_ops_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588};
2589
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002590static void azx_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Takashi Iwai176d5332008-07-30 15:01:44 +02002592 struct azx_pcm *apcm = pcm->private_data;
2593 if (apcm) {
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002594 list_del(&apcm->list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002595 kfree(apcm);
2596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002599#define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
2600
Takashi Iwai176d5332008-07-30 15:01:44 +02002601static int
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002602azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
2603 struct hda_pcm *cpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002605 struct azx *chip = bus->private_data;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002606 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 struct azx_pcm *apcm;
Takashi Iwai176d5332008-07-30 15:01:44 +02002608 int pcm_dev = cpcm->device;
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002609 unsigned int size;
Takashi Iwai176d5332008-07-30 15:01:44 +02002610 int s, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002612 list_for_each_entry(apcm, &chip->pcm_list, list) {
2613 if (apcm->pcm->device == pcm_dev) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08002614 snd_printk(KERN_ERR SFX "%s: PCM %d already exists\n",
2615 pci_name(chip->pci), pcm_dev);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002616 return -EBUSY;
2617 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002618 }
2619 err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
2620 cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
2621 cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 &pcm);
2623 if (err < 0)
2624 return err;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002625 strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
Takashi Iwai176d5332008-07-30 15:01:44 +02002626 apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (apcm == NULL)
2628 return -ENOMEM;
2629 apcm->chip = chip;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002630 apcm->pcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 apcm->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 pcm->private_data = apcm;
2633 pcm->private_free = azx_pcm_free;
Takashi Iwai176d5332008-07-30 15:01:44 +02002634 if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
2635 pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002636 list_add_tail(&apcm->list, &chip->pcm_list);
Takashi Iwai176d5332008-07-30 15:01:44 +02002637 cpcm->pcm = pcm;
2638 for (s = 0; s < 2; s++) {
2639 apcm->hinfo[s] = &cpcm->stream[s];
2640 if (cpcm->stream[s].substreams)
2641 snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
2642 }
2643 /* buffer pre-allocation */
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002644 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
2645 if (size > MAX_PREALLOC_SIZE)
2646 size = MAX_PREALLOC_SIZE;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01002647 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 snd_dma_pci_data(chip->pci),
Takashi Iwaiacfa6342011-07-12 17:27:46 +02002649 size, MAX_PREALLOC_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 return 0;
2651}
2652
2653/*
2654 * mixer creation - all stuff is implemented in hda module
2655 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002656static int azx_mixer_create(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
2658 return snd_hda_build_controls(chip->bus);
2659}
2660
2661
2662/*
2663 * initialize SD streams
2664 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002665static int azx_init_stream(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
2667 int i;
2668
2669 /* initialize each stream (aka device)
Takashi Iwaid01ce992007-07-27 16:52:19 +02002670 * assign the starting bdl address to each stream (device)
2671 * and initialize
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 */
Takashi Iwai07e4ca52005-08-24 14:14:57 +02002673 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaia98f90f2005-11-17 14:59:02 +01002674 struct azx_dev *azx_dev = &chip->azx_dev[i];
Takashi Iwai929861c2006-08-31 16:55:40 +02002675 azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
2677 azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
2678 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
2679 azx_dev->sd_int_sta_mask = 1 << i;
2680 /* stream tag: must be non-zero and unique */
2681 azx_dev->index = i;
2682 azx_dev->stream_tag = i + 1;
2683 }
2684
2685 return 0;
2686}
2687
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002688static int azx_acquire_irq(struct azx *chip, int do_disconnect)
2689{
Takashi Iwai437a5a42006-11-21 12:14:23 +01002690 if (request_irq(chip->pci->irq, azx_interrupt,
2691 chip->msi ? 0 : IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002692 KBUILD_MODNAME, chip)) {
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002693 printk(KERN_ERR "hda-intel: unable to grab IRQ %d, "
2694 "disabling device\n", chip->pci->irq);
2695 if (do_disconnect)
2696 snd_card_disconnect(chip->card);
2697 return -1;
2698 }
2699 chip->irq = chip->pci->irq;
Takashi Iwai69e13412006-11-21 12:10:55 +01002700 pci_intx(chip->pci, !chip->msi);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002701 return 0;
2702}
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Takashi Iwaicb53c622007-08-10 17:21:45 +02002705static void azx_stop_chip(struct azx *chip)
2706{
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002707 if (!chip->initialized)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002708 return;
2709
2710 /* disable interrupts */
2711 azx_int_disable(chip);
2712 azx_int_clear(chip);
2713
2714 /* disable CORB/RIRB */
2715 azx_free_cmd_io(chip);
2716
2717 /* disable position buffer */
2718 azx_writel(chip, DPLBASE, 0);
2719 azx_writel(chip, DPUBASE, 0);
2720
2721 chip->initialized = 0;
2722}
2723
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002724#ifdef CONFIG_SND_HDA_DSP_LOADER
2725/*
2726 * DSP loading code (e.g. for CA0132)
2727 */
2728
2729/* use the first stream for loading DSP */
2730static struct azx_dev *
2731azx_get_dsp_loader_dev(struct azx *chip)
2732{
2733 return &chip->azx_dev[chip->playback_index_offset];
2734}
2735
2736static int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
2737 unsigned int byte_size,
2738 struct snd_dma_buffer *bufp)
2739{
2740 u32 *bdl;
2741 struct azx *chip = bus->private_data;
2742 struct azx_dev *azx_dev;
2743 int err;
2744
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002745 azx_dev = azx_get_dsp_loader_dev(chip);
2746
2747 dsp_lock(azx_dev);
2748 spin_lock_irq(&chip->reg_lock);
2749 if (azx_dev->running || azx_dev->locked) {
2750 spin_unlock_irq(&chip->reg_lock);
2751 err = -EBUSY;
2752 goto unlock;
2753 }
2754 azx_dev->prepared = 0;
2755 chip->saved_azx_dev = *azx_dev;
2756 azx_dev->locked = 1;
2757 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002758
2759 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
2760 snd_dma_pci_data(chip->pci),
2761 byte_size, bufp);
2762 if (err < 0)
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002763 goto err_alloc;
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002764
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002765 mark_pages_wc(chip, bufp, true);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002766 azx_dev->bufsize = byte_size;
2767 azx_dev->period_bytes = byte_size;
2768 azx_dev->format_val = format;
2769
2770 azx_stream_reset(chip, azx_dev);
2771
2772 /* reset BDL address */
2773 azx_sd_writel(azx_dev, SD_BDLPL, 0);
2774 azx_sd_writel(azx_dev, SD_BDLPU, 0);
2775
2776 azx_dev->frags = 0;
2777 bdl = (u32 *)azx_dev->bdl.area;
2778 err = setup_bdle(chip, bufp, azx_dev, &bdl, 0, byte_size, 0);
2779 if (err < 0)
2780 goto error;
2781
2782 azx_setup_controller(chip, azx_dev);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002783 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002784 return azx_dev->stream_tag;
2785
2786 error:
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002787 mark_pages_wc(chip, bufp, false);
2788 snd_dma_free_pages(bufp);
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002789 err_alloc:
2790 spin_lock_irq(&chip->reg_lock);
2791 if (azx_dev->opened)
2792 *azx_dev = chip->saved_azx_dev;
2793 azx_dev->locked = 0;
2794 spin_unlock_irq(&chip->reg_lock);
2795 unlock:
2796 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002797 return err;
2798}
2799
2800static void azx_load_dsp_trigger(struct hda_bus *bus, bool start)
2801{
2802 struct azx *chip = bus->private_data;
2803 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
2804
2805 if (start)
2806 azx_stream_start(chip, azx_dev);
2807 else
2808 azx_stream_stop(chip, azx_dev);
2809 azx_dev->running = start;
2810}
2811
2812static void azx_load_dsp_cleanup(struct hda_bus *bus,
2813 struct snd_dma_buffer *dmab)
2814{
2815 struct azx *chip = bus->private_data;
2816 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
2817
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002818 if (!dmab->area || !azx_dev->locked)
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002819 return;
2820
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002821 dsp_lock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002822 /* reset BDL address */
2823 azx_sd_writel(azx_dev, SD_BDLPL, 0);
2824 azx_sd_writel(azx_dev, SD_BDLPU, 0);
2825 azx_sd_writel(azx_dev, SD_CTL, 0);
2826 azx_dev->bufsize = 0;
2827 azx_dev->period_bytes = 0;
2828 azx_dev->format_val = 0;
2829
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002830 mark_pages_wc(chip, dmab, false);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002831 snd_dma_free_pages(dmab);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002832 dmab->area = NULL;
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002833
Takashi Iwaieb49faa2013-03-15 09:19:11 +01002834 spin_lock_irq(&chip->reg_lock);
2835 if (azx_dev->opened)
2836 *azx_dev = chip->saved_azx_dev;
2837 azx_dev->locked = 0;
2838 spin_unlock_irq(&chip->reg_lock);
2839 dsp_unlock(azx_dev);
Takashi Iwai1d1a4562012-09-20 20:29:13 -07002840}
2841#endif /* CONFIG_SND_HDA_DSP_LOADER */
2842
Takashi Iwai83012a72012-08-24 18:38:08 +02002843#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02002844/* power-up/down the controller */
Takashi Iwai68467f52012-08-28 09:14:29 -07002845static void azx_power_notify(struct hda_bus *bus, bool power_up)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002846{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002847 struct azx *chip = bus->private_data;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002848
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01002849 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2850 return;
2851
Takashi Iwai68467f52012-08-28 09:14:29 -07002852 if (power_up)
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002853 pm_runtime_get_sync(&chip->pci->dev);
2854 else
2855 pm_runtime_put_sync(&chip->pci->dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002856}
Takashi Iwai65fcd412012-08-14 17:13:32 +02002857
2858static DEFINE_MUTEX(card_list_lock);
2859static LIST_HEAD(card_list);
2860
2861static void azx_add_card_list(struct azx *chip)
2862{
2863 mutex_lock(&card_list_lock);
2864 list_add(&chip->list, &card_list);
2865 mutex_unlock(&card_list_lock);
2866}
2867
2868static void azx_del_card_list(struct azx *chip)
2869{
2870 mutex_lock(&card_list_lock);
2871 list_del_init(&chip->list);
2872 mutex_unlock(&card_list_lock);
2873}
2874
2875/* trigger power-save check at writing parameter */
2876static int param_set_xint(const char *val, const struct kernel_param *kp)
2877{
2878 struct azx *chip;
2879 struct hda_codec *c;
2880 int prev = power_save;
2881 int ret = param_set_int(val, kp);
2882
2883 if (ret || prev == power_save)
2884 return ret;
2885
2886 mutex_lock(&card_list_lock);
2887 list_for_each_entry(chip, &card_list, list) {
2888 if (!chip->bus || chip->disabled)
2889 continue;
2890 list_for_each_entry(c, &chip->bus->codec_list, list)
2891 snd_hda_power_sync(c);
2892 }
2893 mutex_unlock(&card_list_lock);
2894 return 0;
2895}
2896#else
2897#define azx_add_card_list(chip) /* NOP */
2898#define azx_del_card_list(chip) /* NOP */
Takashi Iwai83012a72012-08-24 18:38:08 +02002899#endif /* CONFIG_PM */
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002900
Takashi Iwai7ccbde52012-08-14 18:10:09 +02002901#if defined(CONFIG_PM_SLEEP) || defined(SUPPORT_VGA_SWITCHEROO)
Takashi Iwai5c0b9be2008-12-11 11:47:17 +01002902/*
2903 * power management
2904 */
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002905static int azx_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002907 struct pci_dev *pci = to_pci_dev(dev);
2908 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002909 struct azx *chip = card->private_data;
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002910 struct azx_pcm *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Takashi Iwaic5c21522012-12-04 17:01:25 +01002912 if (chip->disabled)
2913 return 0;
2914
Takashi Iwai421a1252005-11-17 16:11:09 +01002915 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwai9ad593f2008-05-16 12:34:47 +02002916 azx_clear_irq_pending(chip);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01002917 list_for_each_entry(p, &chip->pcm_list, list)
2918 snd_pcm_suspend_all(p->pcm);
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02002919 if (chip->initialized)
Takashi Iwai8dd78332009-06-02 01:16:07 +02002920 snd_hda_suspend(chip->bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002921 azx_stop_chip(chip);
Mengdong Lin7295b262013-06-25 05:58:49 -04002922 azx_enter_link_reset(chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002923 if (chip->irq >= 0) {
Takashi Iwai43001c92006-09-08 12:30:03 +02002924 free_irq(chip->irq, chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002925 chip->irq = -1;
2926 }
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002927 if (chip->msi)
Takashi Iwai43001c92006-09-08 12:30:03 +02002928 pci_disable_msi(chip->pci);
Takashi Iwai421a1252005-11-17 16:11:09 +01002929 pci_disable_device(pci);
2930 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002931 pci_set_power_state(pci, PCI_D3hot);
Wang Xingchao99a20082013-05-30 22:07:10 +08002932 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2933 hda_display_power(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return 0;
2935}
2936
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002937static int azx_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002939 struct pci_dev *pci = to_pci_dev(dev);
2940 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai421a1252005-11-17 16:11:09 +01002941 struct azx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Takashi Iwaic5c21522012-12-04 17:01:25 +01002943 if (chip->disabled)
2944 return 0;
2945
Wang Xingchao99a20082013-05-30 22:07:10 +08002946 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2947 hda_display_power(true);
Takashi Iwaid14a7e02009-02-16 10:13:03 +01002948 pci_set_power_state(pci, PCI_D0);
2949 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002950 if (pci_enable_device(pci) < 0) {
2951 printk(KERN_ERR "hda-intel: pci_enable_device failed, "
2952 "disabling device\n");
2953 snd_card_disconnect(card);
2954 return -EIO;
2955 }
2956 pci_set_master(pci);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02002957 if (chip->msi)
2958 if (pci_enable_msi(pci) < 0)
2959 chip->msi = 0;
2960 if (azx_acquire_irq(chip, 1) < 0)
Takashi Iwai30b35392006-10-11 18:52:53 +02002961 return -EIO;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002962 azx_init_pci(chip);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002963
Takashi Iwai7f308302012-05-08 16:52:23 +02002964 azx_init_chip(chip, 1);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02002965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 snd_hda_resume(chip->bus);
Takashi Iwai421a1252005-11-17 16:11:09 +01002967 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return 0;
2969}
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002970#endif /* CONFIG_PM_SLEEP || SUPPORT_VGA_SWITCHEROO */
2971
2972#ifdef CONFIG_PM_RUNTIME
2973static int azx_runtime_suspend(struct device *dev)
2974{
2975 struct snd_card *card = dev_get_drvdata(dev);
2976 struct azx *chip = card->private_data;
2977
Dave Airlie246efa42013-07-29 15:19:29 +10002978 if (chip->disabled)
2979 return 0;
2980
2981 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
2982 return 0;
2983
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002984 azx_stop_chip(chip);
Mengdong Lin7295b262013-06-25 05:58:49 -04002985 azx_enter_link_reset(chip);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002986 azx_clear_irq_pending(chip);
Wang Xingchao99a20082013-05-30 22:07:10 +08002987 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
2988 hda_display_power(false);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002989 return 0;
2990}
2991
2992static int azx_runtime_resume(struct device *dev)
2993{
2994 struct snd_card *card = dev_get_drvdata(dev);
2995 struct azx *chip = card->private_data;
2996
Dave Airlie246efa42013-07-29 15:19:29 +10002997 if (chip->disabled)
2998 return 0;
2999
3000 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
3001 return 0;
3002
Wang Xingchao99a20082013-05-30 22:07:10 +08003003 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
3004 hda_display_power(true);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003005 azx_init_pci(chip);
3006 azx_init_chip(chip, 1);
3007 return 0;
3008}
Takashi Iwai6eb827d2012-12-12 11:50:12 +01003009
3010static int azx_runtime_idle(struct device *dev)
3011{
3012 struct snd_card *card = dev_get_drvdata(dev);
3013 struct azx *chip = card->private_data;
3014
Dave Airlie246efa42013-07-29 15:19:29 +10003015 if (chip->disabled)
3016 return 0;
3017
Takashi Iwai6eb827d2012-12-12 11:50:12 +01003018 if (!power_save_controller ||
3019 !(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
3020 return -EBUSY;
3021
3022 return 0;
3023}
3024
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003025#endif /* CONFIG_PM_RUNTIME */
3026
3027#ifdef CONFIG_PM
3028static const struct dev_pm_ops azx_pm = {
3029 SET_SYSTEM_SLEEP_PM_OPS(azx_suspend, azx_resume)
Takashi Iwai6eb827d2012-12-12 11:50:12 +01003030 SET_RUNTIME_PM_OPS(azx_runtime_suspend, azx_runtime_resume, azx_runtime_idle)
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003031};
3032
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003033#define AZX_PM_OPS &azx_pm
3034#else
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003035#define AZX_PM_OPS NULL
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003036#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038
3039/*
Takashi Iwai0cbf0092008-10-29 16:18:25 +01003040 * reboot notifier for hang-up problem at power-down
3041 */
3042static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
3043{
3044 struct azx *chip = container_of(nb, struct azx, reboot_notifier);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003045 snd_hda_bus_reboot_notify(chip->bus);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01003046 azx_stop_chip(chip);
3047 return NOTIFY_OK;
3048}
3049
3050static void azx_notifier_register(struct azx *chip)
3051{
3052 chip->reboot_notifier.notifier_call = azx_halt;
3053 register_reboot_notifier(&chip->reboot_notifier);
3054}
3055
3056static void azx_notifier_unregister(struct azx *chip)
3057{
3058 if (chip->reboot_notifier.notifier_call)
3059 unregister_reboot_notifier(&chip->reboot_notifier);
3060}
3061
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01003062static int azx_probe_continue(struct azx *chip);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003063
Steven Newbury8393ec4a2012-06-08 13:06:29 +02003064#ifdef SUPPORT_VGA_SWITCHEROO
Bill Pembertone23e7a12012-12-06 12:35:10 -05003065static struct pci_dev *get_bound_vga(struct pci_dev *pci);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003066
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003067static void azx_vs_set_state(struct pci_dev *pci,
3068 enum vga_switcheroo_state state)
3069{
3070 struct snd_card *card = pci_get_drvdata(pci);
3071 struct azx *chip = card->private_data;
3072 bool disabled;
3073
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003074 wait_for_completion(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003075 if (chip->init_failed)
3076 return;
3077
3078 disabled = (state == VGA_SWITCHEROO_OFF);
3079 if (chip->disabled == disabled)
3080 return;
3081
3082 if (!chip->bus) {
3083 chip->disabled = disabled;
3084 if (!disabled) {
3085 snd_printk(KERN_INFO SFX
3086 "%s: Start delayed initialization\n",
3087 pci_name(chip->pci));
Takashi Iwai5c906802013-05-30 22:07:09 +08003088 if (azx_probe_continue(chip) < 0) {
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003089 snd_printk(KERN_ERR SFX
3090 "%s: initialization error\n",
3091 pci_name(chip->pci));
3092 chip->init_failed = true;
3093 }
3094 }
3095 } else {
3096 snd_printk(KERN_INFO SFX
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003097 "%s: %s via VGA-switcheroo\n", pci_name(chip->pci),
3098 disabled ? "Disabling" : "Enabling");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003099 if (disabled) {
Dave Airlie246efa42013-07-29 15:19:29 +10003100 pm_runtime_put_sync_suspend(&pci->dev);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003101 azx_suspend(&pci->dev);
Dave Airlie246efa42013-07-29 15:19:29 +10003102 /* when we get suspended by vga switcheroo we end up in D3cold,
3103 * however we have no ACPI handle, so pci/acpi can't put us there,
3104 * put ourselves there */
3105 pci->current_state = PCI_D3cold;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003106 chip->disabled = true;
Takashi Iwai128960a2012-10-12 17:28:18 +02003107 if (snd_hda_lock_devices(chip->bus))
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003108 snd_printk(KERN_WARNING SFX "%s: Cannot lock devices!\n",
3109 pci_name(chip->pci));
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003110 } else {
3111 snd_hda_unlock_devices(chip->bus);
Dave Airlie246efa42013-07-29 15:19:29 +10003112 pm_runtime_get_noresume(&pci->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003113 chip->disabled = false;
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003114 azx_resume(&pci->dev);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003115 }
3116 }
3117}
3118
3119static bool azx_vs_can_switch(struct pci_dev *pci)
3120{
3121 struct snd_card *card = pci_get_drvdata(pci);
3122 struct azx *chip = card->private_data;
3123
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003124 wait_for_completion(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003125 if (chip->init_failed)
3126 return false;
3127 if (chip->disabled || !chip->bus)
3128 return true;
3129 if (snd_hda_lock_devices(chip->bus))
3130 return false;
3131 snd_hda_unlock_devices(chip->bus);
3132 return true;
3133}
3134
Bill Pembertone23e7a12012-12-06 12:35:10 -05003135static void init_vga_switcheroo(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003136{
3137 struct pci_dev *p = get_bound_vga(chip->pci);
3138 if (p) {
3139 snd_printk(KERN_INFO SFX
3140 "%s: Handle VGA-switcheroo audio client\n",
3141 pci_name(chip->pci));
3142 chip->use_vga_switcheroo = 1;
3143 pci_dev_put(p);
3144 }
3145}
3146
3147static const struct vga_switcheroo_client_ops azx_vs_ops = {
3148 .set_gpu_state = azx_vs_set_state,
3149 .can_switch = azx_vs_can_switch,
3150};
3151
Bill Pembertone23e7a12012-12-06 12:35:10 -05003152static int register_vga_switcheroo(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003153{
Takashi Iwai128960a2012-10-12 17:28:18 +02003154 int err;
3155
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003156 if (!chip->use_vga_switcheroo)
3157 return 0;
3158 /* FIXME: currently only handling DIS controller
3159 * is there any machine with two switchable HDMI audio controllers?
3160 */
Takashi Iwai128960a2012-10-12 17:28:18 +02003161 err = vga_switcheroo_register_audio_client(chip->pci, &azx_vs_ops,
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003162 VGA_SWITCHEROO_DIS,
3163 chip->bus != NULL);
Takashi Iwai128960a2012-10-12 17:28:18 +02003164 if (err < 0)
3165 return err;
3166 chip->vga_switcheroo_registered = 1;
Dave Airlie246efa42013-07-29 15:19:29 +10003167
3168 /* register as an optimus hdmi audio power domain */
3169 vga_switcheroo_init_domain_pm_optimus_hdmi_audio(&chip->pci->dev, &chip->hdmi_pm_domain);
Takashi Iwai128960a2012-10-12 17:28:18 +02003170 return 0;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003171}
3172#else
3173#define init_vga_switcheroo(chip) /* NOP */
3174#define register_vga_switcheroo(chip) 0
Steven Newbury8393ec4a2012-06-08 13:06:29 +02003175#define check_hdmi_disabled(pci) false
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003176#endif /* SUPPORT_VGA_SWITCHER */
3177
Takashi Iwai0cbf0092008-10-29 16:18:25 +01003178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 * destructor
3180 */
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003181static int azx_free(struct azx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003183 struct pci_dev *pci = chip->pci;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003184 int i;
3185
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003186 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME)
3187 && chip->running)
3188 pm_runtime_get_noresume(&pci->dev);
3189
Takashi Iwai65fcd412012-08-14 17:13:32 +02003190 azx_del_card_list(chip);
3191
Takashi Iwai0cbf0092008-10-29 16:18:25 +01003192 azx_notifier_unregister(chip);
3193
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003194 chip->init_failed = 1; /* to be sure */
Daniel J Blueman44728e92012-12-18 23:59:33 +08003195 complete_all(&chip->probe_wait);
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003196
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003197 if (use_vga_switcheroo(chip)) {
3198 if (chip->disabled && chip->bus)
3199 snd_hda_unlock_devices(chip->bus);
Takashi Iwai128960a2012-10-12 17:28:18 +02003200 if (chip->vga_switcheroo_registered)
3201 vga_switcheroo_unregister_client(chip->pci);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003202 }
3203
Takashi Iwaice43fba2005-05-30 20:33:44 +02003204 if (chip->initialized) {
Takashi Iwai9ad593f2008-05-16 12:34:47 +02003205 azx_clear_irq_pending(chip);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003206 for (i = 0; i < chip->num_streams; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 azx_stream_stop(chip, &chip->azx_dev[i]);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003208 azx_stop_chip(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210
Jeff Garzikf000fd82008-04-22 13:50:34 +02003211 if (chip->irq >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 free_irq(chip->irq, (void*)chip);
Takashi Iwai68e7fff2006-10-23 13:40:59 +02003213 if (chip->msi)
Takashi Iwai30b35392006-10-11 18:52:53 +02003214 pci_disable_msi(chip->pci);
Takashi Iwaif079c252006-06-01 11:42:14 +02003215 if (chip->remap_addr)
3216 iounmap(chip->remap_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003218 if (chip->azx_dev) {
3219 for (i = 0; i < chip->num_streams; i++)
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003220 if (chip->azx_dev[i].bdl.area) {
3221 mark_pages_wc(chip, &chip->azx_dev[i].bdl, false);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003222 snd_dma_free_pages(&chip->azx_dev[i].bdl);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003223 }
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003224 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003225 if (chip->rb.area) {
3226 mark_pages_wc(chip, &chip->rb, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 snd_dma_free_pages(&chip->rb);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003228 }
3229 if (chip->posbuf.area) {
3230 mark_pages_wc(chip, &chip->posbuf, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 snd_dma_free_pages(&chip->posbuf);
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003232 }
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003233 if (chip->region_requested)
3234 pci_release_regions(chip->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 pci_disable_device(chip->pci);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003236 kfree(chip->azx_dev);
Takashi Iwai4918cda2012-08-09 12:33:28 +02003237#ifdef CONFIG_SND_HDA_PATCH_LOADER
3238 if (chip->fw)
3239 release_firmware(chip->fw);
3240#endif
Wang Xingchao99a20082013-05-30 22:07:10 +08003241 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
3242 hda_display_power(false);
3243 hda_i915_exit();
3244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 kfree(chip);
3246
3247 return 0;
3248}
3249
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003250static int azx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
3252 return azx_free(device->device_data);
3253}
3254
Steven Newbury8393ec4a2012-06-08 13:06:29 +02003255#ifdef SUPPORT_VGA_SWITCHEROO
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256/*
Takashi Iwai91219472012-04-26 12:13:25 +02003257 * Check of disabled HDMI controller by vga-switcheroo
3258 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003259static struct pci_dev *get_bound_vga(struct pci_dev *pci)
Takashi Iwai91219472012-04-26 12:13:25 +02003260{
3261 struct pci_dev *p;
3262
3263 /* check only discrete GPU */
3264 switch (pci->vendor) {
3265 case PCI_VENDOR_ID_ATI:
3266 case PCI_VENDOR_ID_AMD:
3267 case PCI_VENDOR_ID_NVIDIA:
3268 if (pci->devfn == 1) {
3269 p = pci_get_domain_bus_and_slot(pci_domain_nr(pci->bus),
3270 pci->bus->number, 0);
3271 if (p) {
3272 if ((p->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3273 return p;
3274 pci_dev_put(p);
3275 }
3276 }
3277 break;
3278 }
3279 return NULL;
3280}
3281
Bill Pembertone23e7a12012-12-06 12:35:10 -05003282static bool check_hdmi_disabled(struct pci_dev *pci)
Takashi Iwai91219472012-04-26 12:13:25 +02003283{
3284 bool vga_inactive = false;
3285 struct pci_dev *p = get_bound_vga(pci);
3286
3287 if (p) {
Takashi Iwai12b78a72012-06-07 12:15:16 +02003288 if (vga_switcheroo_get_client_state(p) == VGA_SWITCHEROO_OFF)
Takashi Iwai91219472012-04-26 12:13:25 +02003289 vga_inactive = true;
3290 pci_dev_put(p);
3291 }
3292 return vga_inactive;
3293}
Steven Newbury8393ec4a2012-06-08 13:06:29 +02003294#endif /* SUPPORT_VGA_SWITCHEROO */
Takashi Iwai91219472012-04-26 12:13:25 +02003295
3296/*
Takashi Iwai3372a152007-02-01 15:46:50 +01003297 * white/black-listing for position_fix
3298 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003299static struct snd_pci_quirk position_fix_list[] = {
Takashi Iwaid2e1c972008-06-10 17:53:34 +02003300 SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
3301 SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
Takashi Iwai2f703e72009-12-01 14:17:37 +01003302 SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
Takashi Iwaid2e1c972008-06-10 17:53:34 +02003303 SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
Daniel T Chendd37f8e2010-05-30 01:17:03 -04003304 SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS", POS_FIX_LPIB),
Daniel T Chen9f75c1b2010-05-30 13:08:41 -04003305 SND_PCI_QUIRK(0x1043, 0x81e7, "ASUS M2V", POS_FIX_LPIB),
Daniel T Chene96d3122010-05-27 18:32:18 -04003306 SND_PCI_QUIRK(0x104d, 0x9069, "Sony VPCS11V9E", POS_FIX_LPIB),
David Henningssonb01de4f2012-01-12 16:31:14 +01003307 SND_PCI_QUIRK(0x10de, 0xcb89, "Macbook Pro 7,1", POS_FIX_LPIB),
Daniel T Chen61bb42c2010-05-29 11:04:11 -04003308 SND_PCI_QUIRK(0x1297, 0x3166, "Shuttle", POS_FIX_LPIB),
Daniel T Chen9ec8dda2010-03-28 02:34:40 -04003309 SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
Takashi Iwai45d4ebf2009-11-30 11:58:30 +01003310 SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
Takashi Iwai8815cd02010-04-15 09:02:41 +02003311 SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
Daniel T Chenb90c0762010-05-30 19:31:41 -04003312 SND_PCI_QUIRK(0x1849, 0x0888, "775Dual-VSTA", POS_FIX_LPIB),
Daniel T Chen0e0280d2010-04-21 19:55:43 -04003313 SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
Takashi Iwai3372a152007-02-01 15:46:50 +01003314 {}
3315};
3316
Bill Pembertone23e7a12012-12-06 12:35:10 -05003317static int check_position_fix(struct azx *chip, int fix)
Takashi Iwai3372a152007-02-01 15:46:50 +01003318{
3319 const struct snd_pci_quirk *q;
3320
Takashi Iwaic673ba12009-03-17 07:49:14 +01003321 switch (fix) {
Takashi Iwai1dac6692012-09-13 14:59:47 +02003322 case POS_FIX_AUTO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01003323 case POS_FIX_LPIB:
3324 case POS_FIX_POSBUF:
David Henningsson4cb36312010-09-30 10:12:50 +02003325 case POS_FIX_VIACOMBO:
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01003326 case POS_FIX_COMBO:
Takashi Iwaic673ba12009-03-17 07:49:14 +01003327 return fix;
3328 }
3329
Takashi Iwaic673ba12009-03-17 07:49:14 +01003330 q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
3331 if (q) {
3332 printk(KERN_INFO
3333 "hda_intel: position_fix set to %d "
3334 "for device %04x:%04x\n",
3335 q->value, q->subvendor, q->subdevice);
3336 return q->value;
Takashi Iwai3372a152007-02-01 15:46:50 +01003337 }
David Henningssonbdd9ef22010-10-04 12:02:14 +02003338
3339 /* Check VIA/ATI HD Audio Controller exist */
Takashi Iwai9477c582011-05-25 09:11:37 +02003340 if (chip->driver_caps & AZX_DCAPS_POSFIX_VIA) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003341 snd_printd(SFX "%s: Using VIACOMBO position fix\n", pci_name(chip->pci));
David Henningssonbdd9ef22010-10-04 12:02:14 +02003342 return POS_FIX_VIACOMBO;
3343 }
Takashi Iwai9477c582011-05-25 09:11:37 +02003344 if (chip->driver_caps & AZX_DCAPS_POSFIX_LPIB) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003345 snd_printd(SFX "%s: Using LPIB position fix\n", pci_name(chip->pci));
Takashi Iwai9477c582011-05-25 09:11:37 +02003346 return POS_FIX_LPIB;
3347 }
Takashi Iwaic673ba12009-03-17 07:49:14 +01003348 return POS_FIX_AUTO;
Takashi Iwai3372a152007-02-01 15:46:50 +01003349}
3350
3351/*
Takashi Iwai669ba272007-08-17 09:17:36 +02003352 * black-lists for probe_mask
3353 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003354static struct snd_pci_quirk probe_mask_list[] = {
Takashi Iwai669ba272007-08-17 09:17:36 +02003355 /* Thinkpad often breaks the controller communication when accessing
3356 * to the non-working (or non-existing) modem codec slot.
3357 */
3358 SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
3359 SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
3360 SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
Takashi Iwai0edb9452008-11-07 14:53:09 +01003361 /* broken BIOS */
3362 SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
Takashi Iwaief1681d2008-11-24 17:29:28 +01003363 /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
3364 SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01003365 /* forced codec slots */
Ozan Çağlayan93574842009-05-23 15:00:04 +03003366 SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
Takashi Iwai20db7cb2009-02-13 08:18:48 +01003367 SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
Jaroslav Kyselaf3af9052012-04-26 17:52:35 +02003368 /* WinFast VP200 H (Teradici) user reported broken communication */
3369 SND_PCI_QUIRK(0x3a21, 0x040d, "WinFast VP200 H", 0x101),
Takashi Iwai669ba272007-08-17 09:17:36 +02003370 {}
3371};
3372
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003373#define AZX_FORCE_CODEC_MASK 0x100
3374
Bill Pembertone23e7a12012-12-06 12:35:10 -05003375static void check_probe_mask(struct azx *chip, int dev)
Takashi Iwai669ba272007-08-17 09:17:36 +02003376{
3377 const struct snd_pci_quirk *q;
3378
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003379 chip->codec_probe_mask = probe_mask[dev];
3380 if (chip->codec_probe_mask == -1) {
Takashi Iwai669ba272007-08-17 09:17:36 +02003381 q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
3382 if (q) {
3383 printk(KERN_INFO
3384 "hda_intel: probe_mask set to 0x%x "
3385 "for device %04x:%04x\n",
3386 q->value, q->subvendor, q->subdevice);
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003387 chip->codec_probe_mask = q->value;
Takashi Iwai669ba272007-08-17 09:17:36 +02003388 }
3389 }
Takashi Iwaif1eaaee2009-02-13 08:16:55 +01003390
3391 /* check forced option */
3392 if (chip->codec_probe_mask != -1 &&
3393 (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
3394 chip->codec_mask = chip->codec_probe_mask & 0xff;
3395 printk(KERN_INFO "hda_intel: codec_mask forced to 0x%x\n",
3396 chip->codec_mask);
3397 }
Takashi Iwai669ba272007-08-17 09:17:36 +02003398}
3399
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003400/*
Takashi Iwai716238552009-09-28 13:14:04 +02003401 * white/black-list for enable_msi
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003402 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003403static struct snd_pci_quirk msi_black_list[] = {
Takashi Iwai9dc83982009-12-22 08:15:01 +01003404 SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai0a27fcf2010-02-15 17:05:28 +01003405 SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
Ralf Gerbigecd21622010-03-09 18:25:47 +01003406 SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
Michele Ballabio4193d132010-03-06 21:06:46 +01003407 SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
Takashi Iwai38155952010-04-04 12:14:03 +02003408 SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003409 {}
3410};
3411
Bill Pembertone23e7a12012-12-06 12:35:10 -05003412static void check_msi(struct azx *chip)
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003413{
3414 const struct snd_pci_quirk *q;
3415
Takashi Iwai716238552009-09-28 13:14:04 +02003416 if (enable_msi >= 0) {
3417 chip->msi = !!enable_msi;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003418 return;
Takashi Iwai716238552009-09-28 13:14:04 +02003419 }
3420 chip->msi = 1; /* enable MSI as default */
3421 q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003422 if (q) {
3423 printk(KERN_INFO
3424 "hda_intel: msi for device %04x:%04x set to %d\n",
3425 q->subvendor, q->subdevice, q->value);
3426 chip->msi = q->value;
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003427 return;
3428 }
3429
3430 /* NVidia chipsets seem to cause troubles with MSI */
Takashi Iwai9477c582011-05-25 09:11:37 +02003431 if (chip->driver_caps & AZX_DCAPS_NO_MSI) {
3432 printk(KERN_INFO "hda_intel: Disabling MSI\n");
Takashi Iwai80c43ed2010-03-15 15:51:53 +01003433 chip->msi = 0;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003434 }
3435}
3436
Takashi Iwaia1585d72011-12-14 09:27:04 +01003437/* check the snoop mode availability */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003438static void azx_check_snoop_available(struct azx *chip)
Takashi Iwaia1585d72011-12-14 09:27:04 +01003439{
3440 bool snoop = chip->snoop;
3441
3442 switch (chip->driver_type) {
3443 case AZX_DRIVER_VIA:
3444 /* force to non-snoop mode for a new VIA controller
3445 * when BIOS is set
3446 */
3447 if (snoop) {
3448 u8 val;
3449 pci_read_config_byte(chip->pci, 0x42, &val);
3450 if (!(val & 0x80) && chip->pci->revision == 0x30)
3451 snoop = false;
3452 }
3453 break;
3454 case AZX_DRIVER_ATIHDMI_NS:
3455 /* new ATI HDMI requires non-snoop */
3456 snoop = false;
3457 break;
Takashi Iwaic1279f82013-02-07 17:36:22 +01003458 case AZX_DRIVER_CTHDA:
3459 snoop = false;
3460 break;
Takashi Iwaia1585d72011-12-14 09:27:04 +01003461 }
3462
3463 if (snoop != chip->snoop) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003464 snd_printk(KERN_INFO SFX "%s: Force to %s mode\n",
3465 pci_name(chip->pci), snoop ? "snoop" : "non-snoop");
Takashi Iwaia1585d72011-12-14 09:27:04 +01003466 chip->snoop = snoop;
3467 }
3468}
Takashi Iwai669ba272007-08-17 09:17:36 +02003469
Wang Xingchao99a20082013-05-30 22:07:10 +08003470#ifdef CONFIG_SND_HDA_I915
3471static void azx_probe_work(struct work_struct *work)
3472{
3473 azx_probe_continue(container_of(work, struct azx, probe_work));
3474}
3475#endif
3476
Takashi Iwai669ba272007-08-17 09:17:36 +02003477/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 * constructor
3479 */
Bill Pembertone23e7a12012-12-06 12:35:10 -05003480static int azx_create(struct snd_card *card, struct pci_dev *pci,
3481 int dev, unsigned int driver_caps,
3482 struct azx **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003484 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 .dev_free = azx_dev_free,
3486 };
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003487 struct azx *chip;
3488 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490 *rchip = NULL;
Tobin Davisbcd72002008-01-15 11:23:55 +01003491
Pavel Machek927fc862006-08-31 17:03:43 +02003492 err = pci_enable_device(pci);
3493 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return err;
3495
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003496 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003497 if (!chip) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003498 snd_printk(KERN_ERR SFX "%s: Cannot allocate chip\n", pci_name(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 pci_disable_device(pci);
3500 return -ENOMEM;
3501 }
3502
3503 spin_lock_init(&chip->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01003504 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 chip->card = card;
3506 chip->pci = pci;
3507 chip->irq = -1;
Takashi Iwai9477c582011-05-25 09:11:37 +02003508 chip->driver_caps = driver_caps;
3509 chip->driver_type = driver_caps & 0xff;
Takashi Iwai4d8e22e2009-08-11 14:21:26 +02003510 check_msi(chip);
Takashi Iwai555e2192008-06-10 17:53:34 +02003511 chip->dev_index = dev;
Takashi Iwai9ad593f2008-05-16 12:34:47 +02003512 INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003513 INIT_LIST_HEAD(&chip->pcm_list);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003514 INIT_LIST_HEAD(&chip->list);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003515 init_vga_switcheroo(chip);
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003516 init_completion(&chip->probe_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Shahin Ghazinouribeaffc32010-05-11 08:19:55 +02003518 chip->position_fix[0] = chip->position_fix[1] =
3519 check_position_fix(chip, position_fix[dev]);
Takashi Iwaia6f2fd52012-02-28 11:58:40 +01003520 /* combo mode uses LPIB for playback */
3521 if (chip->position_fix[0] == POS_FIX_COMBO) {
3522 chip->position_fix[0] = POS_FIX_LPIB;
3523 chip->position_fix[1] = POS_FIX_AUTO;
3524 }
3525
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003526 check_probe_mask(chip, dev);
Takashi Iwai3372a152007-02-01 15:46:50 +01003527
Takashi Iwai27346162006-01-12 18:28:44 +01003528 chip->single_cmd = single_cmd;
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003529 chip->snoop = hda_snoop;
Takashi Iwaia1585d72011-12-14 09:27:04 +01003530 azx_check_snoop_available(chip);
Takashi Iwaic74db862005-05-12 14:26:27 +02003531
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003532 if (bdl_pos_adj[dev] < 0) {
3533 switch (chip->driver_type) {
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003534 case AZX_DRIVER_ICH:
Seth Heasley32679f92010-02-22 17:31:09 -08003535 case AZX_DRIVER_PCH:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003536 bdl_pos_adj[dev] = 1;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003537 break;
3538 default:
Takashi Iwai0c6341a2008-06-13 20:50:27 +02003539 bdl_pos_adj[dev] = 32;
Takashi Iwai5c0d7bc2008-06-10 17:53:35 +02003540 break;
3541 }
3542 }
3543
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003544 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
3545 if (err < 0) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003546 snd_printk(KERN_ERR SFX "%s: Error creating device [card]!\n",
3547 pci_name(chip->pci));
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003548 azx_free(chip);
3549 return err;
3550 }
3551
Wang Xingchao99a20082013-05-30 22:07:10 +08003552#ifdef CONFIG_SND_HDA_I915
3553 /* continue probing in work context as may trigger request module */
3554 INIT_WORK(&chip->probe_work, azx_probe_work);
3555#endif
3556
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003557 *rchip = chip;
Wang Xingchao99a20082013-05-30 22:07:10 +08003558
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003559 return 0;
3560}
3561
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01003562static int azx_first_init(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003563{
3564 int dev = chip->dev_index;
3565 struct pci_dev *pci = chip->pci;
3566 struct snd_card *card = chip->card;
3567 int i, err;
3568 unsigned short gcap;
3569
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003570#if BITS_PER_LONG != 64
3571 /* Fix up base address on ULI M5461 */
3572 if (chip->driver_type == AZX_DRIVER_ULI) {
3573 u16 tmp3;
3574 pci_read_config_word(pci, 0x40, &tmp3);
3575 pci_write_config_word(pci, 0x40, tmp3 | 0x10);
3576 pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
3577 }
3578#endif
3579
Pavel Machek927fc862006-08-31 17:03:43 +02003580 err = pci_request_regions(pci, "ICH HD audio");
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003581 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 return err;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003583 chip->region_requested = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Pavel Machek927fc862006-08-31 17:03:43 +02003585 chip->addr = pci_resource_start(pci, 0);
Arjan van de Ven2f5ad542008-09-28 16:20:09 -07003586 chip->remap_addr = pci_ioremap_bar(pci, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 if (chip->remap_addr == NULL) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003588 snd_printk(KERN_ERR SFX "%s: ioremap error\n", pci_name(chip->pci));
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003589 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
3591
Takashi Iwai68e7fff2006-10-23 13:40:59 +02003592 if (chip->msi)
3593 if (pci_enable_msi(pci) < 0)
3594 chip->msi = 0;
Stephen Hemminger7376d012006-08-21 19:17:46 +02003595
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003596 if (azx_acquire_irq(chip, 0) < 0)
3597 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599 pci_set_master(pci);
3600 synchronize_irq(chip->irq);
3601
Tobin Davisbcd72002008-01-15 11:23:55 +01003602 gcap = azx_readw(chip, GCAP);
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003603 snd_printdd(SFX "%s: chipset global capabilities = 0x%x\n", pci_name(chip->pci), gcap);
Tobin Davisbcd72002008-01-15 11:23:55 +01003604
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003605 /* disable SB600 64bit support for safety */
Takashi Iwai9477c582011-05-25 09:11:37 +02003606 if (chip->pci->vendor == PCI_VENDOR_ID_ATI) {
Andiry Brienzadc4c2e62009-07-08 13:55:31 +08003607 struct pci_dev *p_smbus;
3608 p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
3609 PCI_DEVICE_ID_ATI_SBX00_SMBUS,
3610 NULL);
3611 if (p_smbus) {
3612 if (p_smbus->revision < 0x30)
3613 gcap &= ~ICH6_GCAP_64OK;
3614 pci_dev_put(p_smbus);
3615 }
3616 }
Takashi Iwai09240cf2009-03-17 07:47:18 +01003617
Takashi Iwai9477c582011-05-25 09:11:37 +02003618 /* disable 64bit DMA address on some devices */
3619 if (chip->driver_caps & AZX_DCAPS_NO_64BIT) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003620 snd_printd(SFX "%s: Disabling 64bit DMA\n", pci_name(chip->pci));
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003621 gcap &= ~ICH6_GCAP_64OK;
Takashi Iwai9477c582011-05-25 09:11:37 +02003622 }
Jaroslav Kysela396087e2009-12-09 10:44:47 +01003623
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003624 /* disable buffer size rounding to 128-byte multiples if supported */
Takashi Iwai7bfe0592012-01-23 17:53:39 +01003625 if (align_buffer_size >= 0)
3626 chip->align_buffer_size = !!align_buffer_size;
3627 else {
3628 if (chip->driver_caps & AZX_DCAPS_BUFSIZE)
3629 chip->align_buffer_size = 0;
3630 else if (chip->driver_caps & AZX_DCAPS_ALIGN_BUFSIZE)
3631 chip->align_buffer_size = 1;
3632 else
3633 chip->align_buffer_size = 1;
3634 }
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003635
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003636 /* allow 64bit DMA address if supported by H/W */
Takashi Iwaib21fadb2009-05-28 12:26:15 +02003637 if ((gcap & ICH6_GCAP_64OK) && !pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
Yang Hongyange9304382009-04-13 14:40:14 -07003638 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003639 else {
Yang Hongyange9304382009-04-13 14:40:14 -07003640 pci_set_dma_mask(pci, DMA_BIT_MASK(32));
3641 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
Takashi Iwai09240cf2009-03-17 07:47:18 +01003642 }
Takashi Iwaicf7aaca2008-02-06 15:05:57 +01003643
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003644 /* read number of streams from GCAP register instead of using
3645 * hardcoded value
3646 */
3647 chip->capture_streams = (gcap >> 8) & 0x0f;
3648 chip->playback_streams = (gcap >> 12) & 0x0f;
3649 if (!chip->playback_streams && !chip->capture_streams) {
Tobin Davisbcd72002008-01-15 11:23:55 +01003650 /* gcap didn't give any info, switching to old method */
3651
3652 switch (chip->driver_type) {
3653 case AZX_DRIVER_ULI:
3654 chip->playback_streams = ULI_NUM_PLAYBACK;
3655 chip->capture_streams = ULI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003656 break;
3657 case AZX_DRIVER_ATIHDMI:
Andiry Xu1815b342011-12-14 16:10:27 +08003658 case AZX_DRIVER_ATIHDMI_NS:
Tobin Davisbcd72002008-01-15 11:23:55 +01003659 chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
3660 chip->capture_streams = ATIHDMI_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003661 break;
Yang, Libinc4da29c2008-11-13 11:07:07 +01003662 case AZX_DRIVER_GENERIC:
Tobin Davisbcd72002008-01-15 11:23:55 +01003663 default:
3664 chip->playback_streams = ICH6_NUM_PLAYBACK;
3665 chip->capture_streams = ICH6_NUM_CAPTURE;
Tobin Davisbcd72002008-01-15 11:23:55 +01003666 break;
3667 }
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003668 }
Takashi Iwai8b6ed8e2008-02-19 11:36:35 +01003669 chip->capture_index_offset = 0;
3670 chip->playback_index_offset = chip->capture_streams;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003671 chip->num_streams = chip->playback_streams + chip->capture_streams;
Takashi Iwaid01ce992007-07-27 16:52:19 +02003672 chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
3673 GFP_KERNEL);
Pavel Machek927fc862006-08-31 17:03:43 +02003674 if (!chip->azx_dev) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003675 snd_printk(KERN_ERR SFX "%s: cannot malloc azx_dev\n", pci_name(chip->pci));
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003676 return -ENOMEM;
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003677 }
3678
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003679 for (i = 0; i < chip->num_streams; i++) {
Takashi Iwaieb49faa2013-03-15 09:19:11 +01003680 dsp_lock_init(&chip->azx_dev[i]);
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003681 /* allocate memory for the BDL for each stream */
3682 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
3683 snd_dma_pci_data(chip->pci),
3684 BDL_SIZE, &chip->azx_dev[i].bdl);
3685 if (err < 0) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003686 snd_printk(KERN_ERR SFX "%s: cannot allocate BDL\n", pci_name(chip->pci));
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003687 return -ENOMEM;
Takashi Iwai4ce107b2008-02-06 14:50:19 +01003688 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003689 mark_pages_wc(chip, &chip->azx_dev[i].bdl, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 }
Takashi Iwai0be3b5d2005-09-05 17:11:40 +02003691 /* allocate memory for the position buffer */
Takashi Iwaid01ce992007-07-27 16:52:19 +02003692 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
3693 snd_dma_pci_data(chip->pci),
3694 chip->num_streams * 8, &chip->posbuf);
3695 if (err < 0) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003696 snd_printk(KERN_ERR SFX "%s: cannot allocate posbuf\n", pci_name(chip->pci));
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003697 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
Takashi Iwai27fe48d92011-09-28 17:16:09 +02003699 mark_pages_wc(chip, &chip->posbuf, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 /* allocate CORB/RIRB */
Takashi Iwai81740862009-05-26 15:22:00 +02003701 err = azx_alloc_cmd_io(chip);
3702 if (err < 0)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
3705 /* initialize streams */
3706 azx_init_stream(chip);
3707
3708 /* initialize chip */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003709 azx_init_pci(chip);
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003710 azx_init_chip(chip, (probe_only[dev] & 2) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712 /* codec detection */
Pavel Machek927fc862006-08-31 17:03:43 +02003713 if (!chip->codec_mask) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003714 snd_printk(KERN_ERR SFX "%s: no codecs found!\n", pci_name(chip->pci));
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003715 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 }
3717
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003718 strcpy(card->driver, "HDA-Intel");
Takashi Iwai18cb7102009-04-16 10:22:24 +02003719 strlcpy(card->shortname, driver_short_names[chip->driver_type],
3720 sizeof(card->shortname));
3721 snprintf(card->longname, sizeof(card->longname),
3722 "%s at 0x%lx irq %i",
3723 card->shortname, chip->addr, chip->irq);
Takashi Iwai07e4ca52005-08-24 14:14:57 +02003724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
Takashi Iwaicb53c622007-08-10 17:21:45 +02003728static void power_down_all_codecs(struct azx *chip)
3729{
Takashi Iwai83012a72012-08-24 18:38:08 +02003730#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003731 /* The codecs were powered up in snd_hda_codec_new().
3732 * Now all initialization done, so turn them down if possible
3733 */
3734 struct hda_codec *codec;
3735 list_for_each_entry(codec, &chip->bus->codec_list, list) {
3736 snd_hda_power_down(codec);
3737 }
3738#endif
3739}
3740
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003741#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003742/* callback from request_firmware_nowait() */
3743static void azx_firmware_cb(const struct firmware *fw, void *context)
3744{
3745 struct snd_card *card = context;
3746 struct azx *chip = card->private_data;
3747 struct pci_dev *pci = chip->pci;
3748
3749 if (!fw) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003750 snd_printk(KERN_ERR SFX "%s: Cannot load firmware, aborting\n",
3751 pci_name(chip->pci));
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003752 goto error;
3753 }
3754
3755 chip->fw = fw;
3756 if (!chip->disabled) {
3757 /* continue probing */
3758 if (azx_probe_continue(chip))
3759 goto error;
3760 }
3761 return; /* OK */
3762
3763 error:
3764 snd_card_free(card);
3765 pci_set_drvdata(pci, NULL);
3766}
Takashi Iwai97c6a3d2012-08-09 17:40:46 +02003767#endif
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003768
Bill Pembertone23e7a12012-12-06 12:35:10 -05003769static int azx_probe(struct pci_dev *pci,
3770 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003772 static int dev;
Takashi Iwaia98f90f2005-11-17 14:59:02 +01003773 struct snd_card *card;
3774 struct azx *chip;
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003775 bool probe_now;
Pavel Machek927fc862006-08-31 17:03:43 +02003776 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003778 if (dev >= SNDRV_CARDS)
3779 return -ENODEV;
3780 if (!enable[dev]) {
3781 dev++;
3782 return -ENOENT;
3783 }
3784
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003785 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
3786 if (err < 0) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003787 snd_printk(KERN_ERR "hda-intel: Error creating card!\n");
Takashi Iwaie58de7b2008-12-28 16:44:30 +01003788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
3790
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003791 snd_card_set_dev(card, &pci->dev);
3792
Takashi Iwai5aba4f82008-01-07 15:16:37 +01003793 err = azx_create(card, pci, dev, pci_id->driver_data, &chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003794 if (err < 0)
3795 goto out_free;
Takashi Iwai421a1252005-11-17 16:11:09 +01003796 card->private_data = chip;
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003797
3798 pci_set_drvdata(pci, card);
3799
3800 err = register_vga_switcheroo(chip);
3801 if (err < 0) {
3802 snd_printk(KERN_ERR SFX
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003803 "%s: Error registering VGA-switcheroo client\n", pci_name(pci));
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003804 goto out_free;
3805 }
3806
3807 if (check_hdmi_disabled(pci)) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003808 snd_printk(KERN_INFO SFX "%s: VGA controller is disabled\n",
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003809 pci_name(pci));
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003810 snd_printk(KERN_INFO SFX "%s: Delaying initialization\n", pci_name(pci));
Takashi Iwaif4c482a2012-12-04 15:09:23 +01003811 chip->disabled = true;
3812 }
3813
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003814 probe_now = !chip->disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Takashi Iwai4918cda2012-08-09 12:33:28 +02003816#ifdef CONFIG_SND_HDA_PATCH_LOADER
3817 if (patch[dev] && *patch[dev]) {
Daniel J Blueman445a51b2012-12-05 23:04:21 +08003818 snd_printk(KERN_ERR SFX "%s: Applying patch firmware '%s'\n",
3819 pci_name(pci), patch[dev]);
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003820 err = request_firmware_nowait(THIS_MODULE, true, patch[dev],
3821 &pci->dev, GFP_KERNEL, card,
3822 azx_firmware_cb);
Takashi Iwai4918cda2012-08-09 12:33:28 +02003823 if (err < 0)
3824 goto out_free;
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003825 probe_now = false; /* continued in azx_firmware_cb() */
Takashi Iwai4918cda2012-08-09 12:33:28 +02003826 }
3827#endif /* CONFIG_SND_HDA_PATCH_LOADER */
3828
Wang Xingchao99a20082013-05-30 22:07:10 +08003829 /* continue probing in work context, avoid request_module deadlock */
3830 if (probe_now && (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)) {
3831#ifdef CONFIG_SND_HDA_I915
3832 probe_now = false;
3833 schedule_work(&chip->probe_work);
3834#else
3835 snd_printk(KERN_ERR SFX "Haswell must build in CONFIG_SND_HDA_I915\n");
3836#endif
3837 }
3838
Takashi Iwai5cb543d2012-08-09 13:49:23 +02003839 if (probe_now) {
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003840 err = azx_probe_continue(chip);
3841 if (err < 0)
3842 goto out_free;
3843 }
3844
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003845 dev++;
Daniel J Blueman44728e92012-12-18 23:59:33 +08003846 complete_all(&chip->probe_wait);
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003847 return 0;
3848
3849out_free:
3850 snd_card_free(card);
3851 return err;
3852}
3853
Takashi Iwai48c8b0e2012-12-07 07:40:35 +01003854static int azx_probe_continue(struct azx *chip)
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003855{
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003856 struct pci_dev *pci = chip->pci;
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003857 int dev = chip->dev_index;
3858 int err;
3859
Wang Xingchao99a20082013-05-30 22:07:10 +08003860 /* Request power well for Haswell HDA controller and codec */
3861 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
3862 err = hda_i915_init();
3863 if (err < 0) {
3864 snd_printk(KERN_ERR SFX "Error request power-well from i915\n");
3865 goto out_free;
3866 }
3867 hda_display_power(true);
3868 }
3869
Takashi Iwai5c906802013-05-30 22:07:09 +08003870 err = azx_first_init(chip);
3871 if (err < 0)
3872 goto out_free;
3873
Jaroslav Kysela2dca0bb2009-11-13 18:41:52 +01003874#ifdef CONFIG_SND_HDA_INPUT_BEEP
3875 chip->beep_mode = beep_mode[dev];
3876#endif
3877
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 /* create codec instances */
Takashi Iwaia1e21c92009-06-17 09:33:52 +02003879 err = azx_codec_create(chip, model[dev]);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003880 if (err < 0)
3881 goto out_free;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003882#ifdef CONFIG_SND_HDA_PATCH_LOADER
Takashi Iwai4918cda2012-08-09 12:33:28 +02003883 if (chip->fw) {
3884 err = snd_hda_load_patch(chip->bus, chip->fw->size,
3885 chip->fw->data);
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003886 if (err < 0)
3887 goto out_free;
Takashi Iwaie39ae852012-11-22 16:18:13 +01003888#ifndef CONFIG_PM
Takashi Iwai4918cda2012-08-09 12:33:28 +02003889 release_firmware(chip->fw); /* no longer needed */
3890 chip->fw = NULL;
Takashi Iwaie39ae852012-11-22 16:18:13 +01003891#endif
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +02003892 }
3893#endif
Jaroslav Kysela10e77dd2010-03-26 11:04:38 +01003894 if ((probe_only[dev] & 1) == 0) {
Takashi Iwaia1e21c92009-06-17 09:33:52 +02003895 err = azx_codec_configure(chip);
3896 if (err < 0)
3897 goto out_free;
3898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
3900 /* create PCM streams */
Takashi Iwai176d5332008-07-30 15:01:44 +02003901 err = snd_hda_build_pcms(chip->bus);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003902 if (err < 0)
3903 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 /* create mixer controls */
Takashi Iwaid01ce992007-07-27 16:52:19 +02003906 err = azx_mixer_create(chip);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003907 if (err < 0)
3908 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003910 err = snd_card_register(chip->card);
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003911 if (err < 0)
3912 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Takashi Iwaicb53c622007-08-10 17:21:45 +02003914 chip->running = 1;
3915 power_down_all_codecs(chip);
Takashi Iwai0cbf0092008-10-29 16:18:25 +01003916 azx_notifier_register(chip);
Takashi Iwai65fcd412012-08-14 17:13:32 +02003917 azx_add_card_list(chip);
Dave Airlie246efa42013-07-29 15:19:29 +10003918 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME) || chip->use_vga_switcheroo)
Wang Xingchaoc67e2222013-05-30 22:07:08 +08003919 pm_runtime_put_noidle(&pci->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Takashi Iwai91219472012-04-26 12:13:25 +02003921 return 0;
3922
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003923out_free:
Takashi Iwaia82d51e2012-04-26 12:23:42 +02003924 chip->init_failed = 1;
Wu Fengguang41dda0f2008-11-20 09:24:52 +08003925 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926}
3927
Bill Pembertone23e7a12012-12-06 12:35:10 -05003928static void azx_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
Takashi Iwai91219472012-04-26 12:13:25 +02003930 struct snd_card *card = pci_get_drvdata(pci);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003931
Takashi Iwai91219472012-04-26 12:13:25 +02003932 if (card)
3933 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934}
3935
3936/* PCI IDs */
Alexey Dobriyancebe41d2010-02-06 00:21:03 +02003937static DEFINE_PCI_DEVICE_TABLE(azx_ids) = {
Seth Heasleyd2f2fcd2010-01-12 17:03:35 -08003938 /* CPT */
Takashi Iwai9477c582011-05-25 09:11:37 +02003939 { PCI_DEVICE(0x8086, 0x1c20),
Takashi Iwaid7dab4d2013-01-08 13:51:30 +01003940 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
Seth Heasleycea310e2010-09-10 16:29:56 -07003941 /* PBG */
Takashi Iwai9477c582011-05-25 09:11:37 +02003942 { PCI_DEVICE(0x8086, 0x1d20),
Takashi Iwaid7dab4d2013-01-08 13:51:30 +01003943 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
Seth Heasleyd2edeb72011-04-20 10:59:57 -07003944 /* Panther Point */
Takashi Iwai9477c582011-05-25 09:11:37 +02003945 { PCI_DEVICE(0x8086, 0x1e20),
Takashi Iwaid7dab4d2013-01-08 13:51:30 +01003946 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
Seth Heasley8bc039a2012-01-23 16:24:31 -08003947 /* Lynx Point */
3948 { PCI_DEVICE(0x8086, 0x8c20),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003949 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston884b0882013-02-08 17:29:40 -08003950 /* Wellsburg */
3951 { PCI_DEVICE(0x8086, 0x8d20),
3952 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
3953 { PCI_DEVICE(0x8086, 0x8d21),
3954 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston144dad92012-08-09 09:38:59 -07003955 /* Lynx Point-LP */
3956 { PCI_DEVICE(0x8086, 0x9c20),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003957 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
James Ralston144dad92012-08-09 09:38:59 -07003958 /* Lynx Point-LP */
3959 { PCI_DEVICE(0x8086, 0x9c21),
Takashi Iwai2ea3c6a2012-11-19 20:03:37 +01003960 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
Wang Xingchaoe926f2c2012-06-13 10:23:51 +08003961 /* Haswell */
Wang Xingchao4a7c5162013-02-01 22:42:19 +08003962 { PCI_DEVICE(0x8086, 0x0a0c),
Wang Xingchao99a20082013-05-30 22:07:10 +08003963 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH |
3964 AZX_DCAPS_I915_POWERWELL },
Wang Xingchaoe926f2c2012-06-13 10:23:51 +08003965 { PCI_DEVICE(0x8086, 0x0c0c),
Wang Xingchao99a20082013-05-30 22:07:10 +08003966 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH |
3967 AZX_DCAPS_I915_POWERWELL },
Wang Xingchaod279fae2012-09-17 13:10:23 +08003968 { PCI_DEVICE(0x8086, 0x0d0c),
Wang Xingchao99a20082013-05-30 22:07:10 +08003969 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH |
3970 AZX_DCAPS_I915_POWERWELL },
Pierre-Louis Bossart99df18b2012-09-21 18:39:07 -05003971 /* 5 Series/3400 */
3972 { PCI_DEVICE(0x8086, 0x3b56),
Takashi Iwai2c1350f2013-02-14 09:44:55 +01003973 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
Takashi Iwaif748abc2013-01-29 10:12:23 +01003974 /* Poulsbo */
Takashi Iwai9477c582011-05-25 09:11:37 +02003975 { PCI_DEVICE(0x8086, 0x811b),
Takashi Iwaif748abc2013-01-29 10:12:23 +01003976 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
3977 /* Oaktrail */
Li Peng09904b92011-12-28 15:17:26 +00003978 { PCI_DEVICE(0x8086, 0x080a),
Takashi Iwaif748abc2013-01-29 10:12:23 +01003979 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
Chew, Chiau Eee44007e2013-05-16 15:36:12 +08003980 /* BayTrail */
3981 { PCI_DEVICE(0x8086, 0x0f04),
3982 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
David Henningsson645e9032011-12-14 15:52:30 +08003983 /* ICH */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003984 { PCI_DEVICE(0x8086, 0x2668),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003985 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3986 AZX_DCAPS_BUFSIZE }, /* ICH6 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003987 { PCI_DEVICE(0x8086, 0x27d8),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003988 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3989 AZX_DCAPS_BUFSIZE }, /* ICH7 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003990 { PCI_DEVICE(0x8086, 0x269a),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003991 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3992 AZX_DCAPS_BUFSIZE }, /* ESB2 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003993 { PCI_DEVICE(0x8086, 0x284b),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003994 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3995 AZX_DCAPS_BUFSIZE }, /* ICH8 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003996 { PCI_DEVICE(0x8086, 0x293e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05003997 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
3998 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02003999 { PCI_DEVICE(0x8086, 0x293f),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05004000 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
4001 AZX_DCAPS_BUFSIZE }, /* ICH9 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02004002 { PCI_DEVICE(0x8086, 0x3a3e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05004003 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
4004 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwai8b0bd222011-06-10 14:56:26 +02004005 { PCI_DEVICE(0x8086, 0x3a6e),
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05004006 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
4007 AZX_DCAPS_BUFSIZE }, /* ICH10 */
Takashi Iwaib6864532010-09-15 10:17:26 +02004008 /* Generic Intel */
4009 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
4010 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
4011 .class_mask = 0xffffff,
Pierre-Louis Bossart2ae66c22011-08-04 10:12:56 -05004012 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_BUFSIZE },
Takashi Iwai9477c582011-05-25 09:11:37 +02004013 /* ATI SB 450/600/700/800/900 */
4014 { PCI_DEVICE(0x1002, 0x437b),
4015 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
4016 { PCI_DEVICE(0x1002, 0x4383),
4017 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
4018 /* AMD Hudson */
4019 { PCI_DEVICE(0x1022, 0x780d),
4020 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB },
Takashi Iwai87218e92008-02-21 08:13:11 +01004021 /* ATI HDMI */
Takashi Iwai9477c582011-05-25 09:11:37 +02004022 { PCI_DEVICE(0x1002, 0x793b),
4023 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4024 { PCI_DEVICE(0x1002, 0x7919),
4025 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4026 { PCI_DEVICE(0x1002, 0x960f),
4027 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4028 { PCI_DEVICE(0x1002, 0x970f),
4029 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4030 { PCI_DEVICE(0x1002, 0xaa00),
4031 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4032 { PCI_DEVICE(0x1002, 0xaa08),
4033 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4034 { PCI_DEVICE(0x1002, 0xaa10),
4035 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4036 { PCI_DEVICE(0x1002, 0xaa18),
4037 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4038 { PCI_DEVICE(0x1002, 0xaa20),
4039 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4040 { PCI_DEVICE(0x1002, 0xaa28),
4041 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4042 { PCI_DEVICE(0x1002, 0xaa30),
4043 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4044 { PCI_DEVICE(0x1002, 0xaa38),
4045 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4046 { PCI_DEVICE(0x1002, 0xaa40),
4047 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
4048 { PCI_DEVICE(0x1002, 0xaa48),
4049 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Xu1815b342011-12-14 16:10:27 +08004050 { PCI_DEVICE(0x1002, 0x9902),
4051 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
4052 { PCI_DEVICE(0x1002, 0xaaa0),
4053 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
4054 { PCI_DEVICE(0x1002, 0xaaa8),
4055 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
4056 { PCI_DEVICE(0x1002, 0xaab0),
4057 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
Takashi Iwai87218e92008-02-21 08:13:11 +01004058 /* VIA VT8251/VT8237A */
Takashi Iwai9477c582011-05-25 09:11:37 +02004059 { PCI_DEVICE(0x1106, 0x3288),
4060 .driver_data = AZX_DRIVER_VIA | AZX_DCAPS_POSFIX_VIA },
Annie Liu754fdff2012-06-08 19:18:39 +08004061 /* VIA GFX VT7122/VX900 */
4062 { PCI_DEVICE(0x1106, 0x9170), .driver_data = AZX_DRIVER_GENERIC },
4063 /* VIA GFX VT6122/VX11 */
4064 { PCI_DEVICE(0x1106, 0x9140), .driver_data = AZX_DRIVER_GENERIC },
Takashi Iwai87218e92008-02-21 08:13:11 +01004065 /* SIS966 */
4066 { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
4067 /* ULI M5461 */
4068 { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
4069 /* NVIDIA MCP */
Takashi Iwai0c2fd1bf42009-12-18 16:41:39 +01004070 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
4071 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
4072 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02004073 .driver_data = AZX_DRIVER_NVIDIA | AZX_DCAPS_PRESET_NVIDIA },
Kailang Yangf2690022008-05-27 11:44:55 +02004074 /* Teradici */
Takashi Iwai9477c582011-05-25 09:11:37 +02004075 { PCI_DEVICE(0x6549, 0x1200),
4076 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
Lars R. Damerowf0b3da92012-11-02 13:10:39 -07004077 { PCI_DEVICE(0x6549, 0x2200),
4078 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
Takashi Iwai4e01f542009-04-16 08:53:34 +02004079 /* Creative X-Fi (CA0110-IBG) */
Takashi Iwaif2a8eca2012-06-11 15:51:54 +02004080 /* CTHDA chips */
4081 { PCI_DEVICE(0x1102, 0x0010),
4082 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
4083 { PCI_DEVICE(0x1102, 0x0012),
4084 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
Takashi Iwai313f6e22009-05-18 12:40:52 +02004085#if !defined(CONFIG_SND_CTXFI) && !defined(CONFIG_SND_CTXFI_MODULE)
4086 /* the following entry conflicts with snd-ctxfi driver,
4087 * as ctxfi driver mutates from HD-audio to native mode with
4088 * a special command sequence.
4089 */
Takashi Iwai4e01f542009-04-16 08:53:34 +02004090 { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
4091 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
4092 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02004093 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01004094 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02004095#else
4096 /* this entry seems still valid -- i.e. without emu20kx chip */
Takashi Iwai9477c582011-05-25 09:11:37 +02004097 { PCI_DEVICE(0x1102, 0x0009),
4098 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
Takashi Iwai69f9ba92011-11-06 13:49:13 +01004099 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
Takashi Iwai313f6e22009-05-18 12:40:52 +02004100#endif
Otavio Salvadore35d4b12010-09-26 23:35:06 -03004101 /* Vortex86MX */
4102 { PCI_DEVICE(0x17f3, 0x3010), .driver_data = AZX_DRIVER_GENERIC },
Bankim Bhavsar0f0714c52011-01-17 15:23:21 +01004103 /* VMware HDAudio */
4104 { PCI_DEVICE(0x15ad, 0x1977), .driver_data = AZX_DRIVER_GENERIC },
Andiry Brienza9176b672009-07-17 11:32:32 +08004105 /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
Yang, Libinc4da29c2008-11-13 11:07:07 +01004106 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
4107 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
4108 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02004109 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Andiry Brienza9176b672009-07-17 11:32:32 +08004110 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
4111 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
4112 .class_mask = 0xffffff,
Takashi Iwai9477c582011-05-25 09:11:37 +02004113 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 { 0, }
4115};
4116MODULE_DEVICE_TABLE(pci, azx_ids);
4117
4118/* pci_driver definition */
Takashi Iwaie9f66d92012-04-24 12:25:00 +02004119static struct pci_driver azx_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02004120 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 .id_table = azx_ids,
4122 .probe = azx_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05004123 .remove = azx_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004124 .driver = {
4125 .pm = AZX_PM_OPS,
4126 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127};
4128
Takashi Iwaie9f66d92012-04-24 12:25:00 +02004129module_pci_driver(azx_driver);