aboutsummaryrefslogtreecommitdiffstats
path: root/sound/pci
diff options
context:
space:
mode:
authorTakashi Iwai <tiwai@suse.de>2012-12-19 06:52:06 -0500
committerTakashi Iwai <tiwai@suse.de>2013-01-12 02:30:53 -0500
commit352f7f914ebb8fe19f9b3f03e7767b04eedf5be3 (patch)
treeba6fa51a175f698d51ed09f0aafcff869ebd499c /sound/pci
parentfdf52cab88bc76d0826c42d0f7e014d31e4a7445 (diff)
ALSA: hda - Merge Realtek parser code to generic parser
Finally the whole generic parser code in Realtek driver is moved into hda_generic.c so that it can be used for generic codec driver. The old dumb generic driver is replaced. Yay. The future plan is to adapt this generic parser for other codecs, i.e. the codec driver calls the exported functions in generic driver but adds some codec-specific fixes and setups. As of this commit, the complete driver code is still duplicated in Realtek codec driver. The big code reduction will come from now on. Signed-off-by: Takashi Iwai <tiwai@suse.de>
Diffstat (limited to 'sound/pci')
-rw-r--r--sound/pci/hda/hda_generic.c3996
-rw-r--r--sound/pci/hda/hda_generic.h199
2 files changed, 3451 insertions, 744 deletions
diff --git a/sound/pci/hda/hda_generic.c b/sound/pci/hda/hda_generic.c
index b81d3d0b952d..2d19b915dbf1 100644
--- a/sound/pci/hda/hda_generic.c
+++ b/sound/pci/hda/hda_generic.c
@@ -23,1063 +23,3571 @@
23#include <linux/init.h> 23#include <linux/init.h>
24#include <linux/slab.h> 24#include <linux/slab.h>
25#include <linux/export.h> 25#include <linux/export.h>
26#include <linux/sort.h>
26#include <sound/core.h> 27#include <sound/core.h>
28#include <sound/jack.h>
27#include "hda_codec.h" 29#include "hda_codec.h"
28#include "hda_local.h" 30#include "hda_local.h"
31#include "hda_auto_parser.h"
32#include "hda_jack.h"
33#include "hda_generic.h"
29 34
30/* widget node for parsing */
31struct hda_gnode {
32 hda_nid_t nid; /* NID of this widget */
33 unsigned short nconns; /* number of input connections */
34 hda_nid_t *conn_list;
35 hda_nid_t slist[2]; /* temporay list */
36 unsigned int wid_caps; /* widget capabilities */
37 unsigned char type; /* widget type */
38 unsigned char pin_ctl; /* pin controls */
39 unsigned char checked; /* the flag indicates that the node is already parsed */
40 unsigned int pin_caps; /* pin widget capabilities */
41 unsigned int def_cfg; /* default configuration */
42 unsigned int amp_out_caps; /* AMP out capabilities */
43 unsigned int amp_in_caps; /* AMP in capabilities */
44 struct list_head list;
45};
46
47/* patch-specific record */
48
49#define MAX_PCM_VOLS 2
50struct pcm_vol {
51 struct hda_gnode *node; /* Node for PCM volume */
52 unsigned int index; /* connection of PCM volume */
53};
54 35
55struct hda_gspec { 36/* initialize hda_gen_spec struct */
56 struct hda_gnode *dac_node[2]; /* DAC node */ 37int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
57 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */ 38{
58 struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */ 39 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
59 unsigned int pcm_vol_nodes; /* number of PCM volumes */ 40 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
41 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
42 return 0;
43}
44EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
60 45
61 struct hda_gnode *adc_node; /* ADC node */ 46static struct snd_kcontrol_new *
62 struct hda_gnode *cap_vol_node; /* Node for capture volume */ 47add_kctl(struct hda_gen_spec *spec, const char *name,
63 unsigned int cur_cap_src; /* current capture source */ 48 const struct snd_kcontrol_new *temp)
64 struct hda_input_mux input_mux; 49{
50 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
51 if (!knew)
52 return NULL;
53 *knew = *temp;
54 if (name)
55 knew->name = kstrdup(name, GFP_KERNEL);
56 else if (knew->name)
57 knew->name = kstrdup(knew->name, GFP_KERNEL);
58 if (!knew->name)
59 return NULL;
60 return knew;
61}
65 62
66 unsigned int def_amp_in_caps; 63static void free_kctls(struct hda_gen_spec *spec)
67 unsigned int def_amp_out_caps; 64{
65 if (spec->kctls.list) {
66 struct snd_kcontrol_new *kctl = spec->kctls.list;
67 int i;
68 for (i = 0; i < spec->kctls.used; i++)
69 kfree(kctl[i].name);
70 }
71 snd_array_free(&spec->kctls);
72}
68 73
69 struct hda_pcm pcm_rec; /* PCM information */ 74static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
75 unsigned int nums,
76 struct hda_ctl_ops *ops)
77{
78 struct hda_gen_spec *spec = codec->spec;
79 struct hda_bind_ctls **ctlp, *ctl;
80 ctlp = snd_array_new(&spec->bind_ctls);
81 if (!ctlp)
82 return NULL;
83 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
84 *ctlp = ctl;
85 if (ctl)
86 ctl->ops = ops;
87 return ctl;
88}
70 89
71 struct list_head nid_list; /* list of widgets */ 90static void free_bind_ctls(struct hda_gen_spec *spec)
91{
92 if (spec->bind_ctls.list) {
93 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
94 int i;
95 for (i = 0; i < spec->bind_ctls.used; i++)
96 kfree(ctl[i]);
97 }
98 snd_array_free(&spec->bind_ctls);
99}
72 100
73#ifdef CONFIG_PM 101void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
74#define MAX_LOOPBACK_AMPS 7 102{
75 struct hda_loopback_check loopback; 103 if (!spec)
76 int num_loopbacks; 104 return;
77 struct hda_amp_list loopback_list[MAX_LOOPBACK_AMPS + 1]; 105 free_kctls(spec);
78#endif 106 free_bind_ctls(spec);
79}; 107 snd_array_free(&spec->paths);
108}
109EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
80 110
81/* 111/*
82 * retrieve the default device type from the default config value 112 * parsing paths
83 */ 113 */
84#define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
85 AC_DEFCFG_DEVICE_SHIFT)
86#define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
87 AC_DEFCFG_LOCATION_SHIFT)
88#define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
89 AC_DEFCFG_PORT_CONN_SHIFT)
90 114
91/* 115/* get the path between the given NIDs;
92 * destructor 116 * passing 0 to either @pin or @dac behaves as a wildcard
93 */ 117 */
94static void snd_hda_generic_free(struct hda_codec *codec) 118struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
119 hda_nid_t from_nid, hda_nid_t to_nid)
95{ 120{
96 struct hda_gspec *spec = codec->spec; 121 struct hda_gen_spec *spec = codec->spec;
97 struct hda_gnode *node, *n; 122 int i;
98 123
99 if (! spec) 124 for (i = 0; i < spec->paths.used; i++) {
100 return; 125 struct nid_path *path = snd_array_elem(&spec->paths, i);
101 /* free all widgets */ 126 if (path->depth <= 0)
102 list_for_each_entry_safe(node, n, &spec->nid_list, list) { 127 continue;
103 if (node->conn_list != node->slist) 128 if ((!from_nid || path->path[0] == from_nid) &&
104 kfree(node->conn_list); 129 (!to_nid || path->path[path->depth - 1] == to_nid))
105 kfree(node); 130 return path;
106 } 131 }
107 kfree(spec); 132 return NULL;
108} 133}
134EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
109 135
136/* check whether the given DAC is already found in any existing paths */
137static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
138{
139 struct hda_gen_spec *spec = codec->spec;
140 int i;
110 141
111/* 142 for (i = 0; i < spec->paths.used; i++) {
112 * add a new widget node and read its attributes 143 struct nid_path *path = snd_array_elem(&spec->paths, i);
113 */ 144 if (path->path[0] == nid)
114static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid) 145 return true;
146 }
147 return false;
148}
149
150/* check whether the given two widgets can be connected */
151static bool is_reachable_path(struct hda_codec *codec,
152 hda_nid_t from_nid, hda_nid_t to_nid)
115{ 153{
116 struct hda_gnode *node; 154 if (!from_nid || !to_nid)
117 int nconns; 155 return false;
118 hda_nid_t conn_list[HDA_MAX_CONNECTIONS]; 156 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
157}
119 158
120 node = kzalloc(sizeof(*node), GFP_KERNEL); 159/* nid, dir and idx */
121 if (node == NULL) 160#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
122 return -ENOMEM; 161
123 node->nid = nid; 162/* check whether the given ctl is already assigned in any path elements */
124 node->wid_caps = get_wcaps(codec, nid); 163static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
125 node->type = get_wcaps_type(node->wid_caps); 164{
126 if (node->wid_caps & AC_WCAP_CONN_LIST) { 165 struct hda_gen_spec *spec = codec->spec;
127 nconns = snd_hda_get_connections(codec, nid, conn_list, 166 int i;
128 HDA_MAX_CONNECTIONS); 167
129 if (nconns < 0) { 168 val &= AMP_VAL_COMPARE_MASK;
130 kfree(node); 169 for (i = 0; i < spec->paths.used; i++) {
131 return nconns; 170 struct nid_path *path = snd_array_elem(&spec->paths, i);
132 } 171 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
133 } else { 172 return true;
134 nconns = 0; 173 }
135 } 174 return false;
136 if (nconns <= ARRAY_SIZE(node->slist)) 175}
137 node->conn_list = node->slist; 176
138 else { 177/* check whether a control with the given (nid, dir, idx) was assigned */
139 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns, 178static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
140 GFP_KERNEL); 179 int dir, int idx)
141 if (! node->conn_list) { 180{
142 snd_printk(KERN_ERR "hda-generic: cannot malloc\n"); 181 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
143 kfree(node); 182 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
144 return -ENOMEM; 183 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
184}
185
186/* called recursively */
187static bool __parse_nid_path(struct hda_codec *codec,
188 hda_nid_t from_nid, hda_nid_t to_nid,
189 int with_aa_mix, struct nid_path *path, int depth)
190{
191 struct hda_gen_spec *spec = codec->spec;
192 hda_nid_t conn[16];
193 int i, nums;
194
195 if (to_nid == spec->mixer_nid) {
196 if (!with_aa_mix)
197 return false;
198 with_aa_mix = 2; /* mark aa-mix is included */
199 }
200
201 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
202 for (i = 0; i < nums; i++) {
203 if (conn[i] != from_nid) {
204 /* special case: when from_nid is 0,
205 * try to find an empty DAC
206 */
207 if (from_nid ||
208 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
209 is_dac_already_used(codec, conn[i]))
210 continue;
145 } 211 }
212 /* aa-mix is requested but not included? */
213 if (!(spec->mixer_nid && with_aa_mix == 1))
214 goto found;
215 }
216 if (depth >= MAX_NID_PATH_DEPTH)
217 return false;
218 for (i = 0; i < nums; i++) {
219 unsigned int type;
220 type = get_wcaps_type(get_wcaps(codec, conn[i]));
221 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
222 type == AC_WID_PIN)
223 continue;
224 if (__parse_nid_path(codec, from_nid, conn[i],
225 with_aa_mix, path, depth + 1))
226 goto found;
227 }
228 return false;
229
230 found:
231 path->path[path->depth] = conn[i];
232 path->idx[path->depth + 1] = i;
233 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
234 path->multi[path->depth + 1] = 1;
235 path->depth++;
236 return true;
237}
238
239/* parse the widget path from the given nid to the target nid;
240 * when @from_nid is 0, try to find an empty DAC;
241 * when @with_aa_mix is 0, paths with spec->mixer_nid are excluded.
242 * when @with_aa_mix is 1, paths without spec->mixer_nid are excluded.
243 * when @with_aa_mix is 2, no special handling about spec->mixer_nid.
244 */
245bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
246 hda_nid_t to_nid, int with_aa_mix,
247 struct nid_path *path)
248{
249 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
250 path->path[path->depth] = to_nid;
251 path->depth++;
252#if 0
253 snd_printdd("path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
254 path->depth, path->path[0], path->path[1],
255 path->path[2], path->path[3], path->path[4]);
256#endif
257 return true;
146 } 258 }
147 memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t)); 259 return false;
148 node->nconns = nconns; 260}
261EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
262
263/*
264 * parse the path between the given NIDs and add to the path list.
265 * if no valid path is found, return NULL
266 */
267struct nid_path *
268snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
269 hda_nid_t to_nid, int with_aa_mix)
270{
271 struct hda_gen_spec *spec = codec->spec;
272 struct nid_path *path;
273
274 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
275 return NULL;
276
277 path = snd_array_new(&spec->paths);
278 if (!path)
279 return NULL;
280 memset(path, 0, sizeof(*path));
281 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
282 return path;
283 /* push back */
284 spec->paths.used--;
285 return NULL;
286}
287EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
288
289/* look for an empty DAC slot */
290static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
291 bool is_digital)
292{
293 struct hda_gen_spec *spec = codec->spec;
294 bool cap_digital;
295 int i;
149 296
150 if (node->type == AC_WID_PIN) { 297 for (i = 0; i < spec->num_all_dacs; i++) {
151 node->pin_caps = snd_hda_query_pin_caps(codec, node->nid); 298 hda_nid_t nid = spec->all_dacs[i];
152 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0); 299 if (!nid || is_dac_already_used(codec, nid))
153 node->def_cfg = snd_hda_codec_get_pincfg(codec, node->nid); 300 continue;
301 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
302 if (is_digital != cap_digital)
303 continue;
304 if (is_reachable_path(codec, nid, pin))
305 return nid;
154 } 306 }
307 return 0;
308}
309
310/* replace the channels in the composed amp value with the given number */
311static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
312{
313 val &= ~(0x3U << 16);
314 val |= chs << 16;
315 return val;
316}
317
318/* check whether the widget has the given amp capability for the direction */
319static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
320 int dir, unsigned int bits)
321{
322 if (!nid)
323 return false;
324 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
325 if (query_amp_caps(codec, nid, dir) & bits)
326 return true;
327 return false;
328}
329
330#define nid_has_mute(codec, nid, dir) \
331 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
332#define nid_has_volume(codec, nid, dir) \
333 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
155 334
156 if (node->wid_caps & AC_WCAP_OUT_AMP) { 335/* look for a widget suitable for assigning a mute switch in the path */
157 if (node->wid_caps & AC_WCAP_AMP_OVRD) 336static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
158 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP); 337 struct nid_path *path)
159 if (! node->amp_out_caps) 338{
160 node->amp_out_caps = spec->def_amp_out_caps; 339 int i;
340
341 for (i = path->depth - 1; i >= 0; i--) {
342 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
343 return path->path[i];
344 if (i != path->depth - 1 && i != 0 &&
345 nid_has_mute(codec, path->path[i], HDA_INPUT))
346 return path->path[i];
161 } 347 }
162 if (node->wid_caps & AC_WCAP_IN_AMP) { 348 return 0;
163 if (node->wid_caps & AC_WCAP_AMP_OVRD) 349}
164 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP); 350
165 if (! node->amp_in_caps) 351/* look for a widget suitable for assigning a volume ctl in the path */
166 node->amp_in_caps = spec->def_amp_in_caps; 352static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
353 struct nid_path *path)
354{
355 int i;
356
357 for (i = path->depth - 1; i >= 0; i--) {
358 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
359 return path->path[i];
167 } 360 }
168 list_add_tail(&node->list, &spec->nid_list);
169 return 0; 361 return 0;
170} 362}
171 363
172/* 364/*
173 * build the AFG subtree 365 * path activation / deactivation
174 */ 366 */
175static int build_afg_tree(struct hda_codec *codec) 367
368/* can have the amp-in capability? */
369static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
176{ 370{
177 struct hda_gspec *spec = codec->spec; 371 hda_nid_t nid = path->path[idx];
178 int i, nodes, err; 372 unsigned int caps = get_wcaps(codec, nid);
179 hda_nid_t nid; 373 unsigned int type = get_wcaps_type(caps);
374
375 if (!(caps & AC_WCAP_IN_AMP))
376 return false;
377 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
378 return false;
379 return true;
380}
180 381
181 if (snd_BUG_ON(!spec)) 382/* can have the amp-out capability? */
182 return -EINVAL; 383static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
384{
385 hda_nid_t nid = path->path[idx];
386 unsigned int caps = get_wcaps(codec, nid);
387 unsigned int type = get_wcaps_type(caps);
388
389 if (!(caps & AC_WCAP_OUT_AMP))
390 return false;
391 if (type == AC_WID_PIN && !idx) /* only for output pins */
392 return false;
393 return true;
394}
183 395
184 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP); 396/* check whether the given (nid,dir,idx) is active */
185 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP); 397static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
398 unsigned int idx, unsigned int dir)
399{
400 struct hda_gen_spec *spec = codec->spec;
401 int i, n;
186 402
187 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid); 403 for (n = 0; n < spec->paths.used; n++) {
188 if (! nid || nodes < 0) { 404 struct nid_path *path = snd_array_elem(&spec->paths, n);
189 printk(KERN_ERR "Invalid AFG subtree\n"); 405 if (!path->active)
190 return -EINVAL; 406 continue;
407 for (i = 0; i < path->depth; i++) {
408 if (path->path[i] == nid) {
409 if (dir == HDA_OUTPUT || path->idx[i] == idx)
410 return true;
411 break;
412 }
413 }
191 } 414 }
415 return false;
416}
192 417
193 /* parse all nodes belonging to the AFG */ 418/* get the default amp value for the target state */
194 for (i = 0; i < nodes; i++, nid++) { 419static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
195 if ((err = add_new_node(codec, spec, nid)) < 0) 420 int dir, bool enable)
196 return err; 421{
422 unsigned int caps;
423 unsigned int val = 0;
424
425 caps = query_amp_caps(codec, nid, dir);
426 if (caps & AC_AMPCAP_NUM_STEPS) {
427 /* set to 0dB */
428 if (enable)
429 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
197 } 430 }
431 if (caps & AC_AMPCAP_MUTE) {
432 if (!enable)
433 val |= HDA_AMP_MUTE;
434 }
435 return val;
436}
198 437
199 return 0; 438/* initialize the amp value (only at the first time) */
439static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
440{
441 int val = get_amp_val_to_activate(codec, nid, dir, false);
442 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
200} 443}
201 444
445static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
446 int idx, bool enable)
447{
448 int val;
449 if (is_ctl_associated(codec, nid, dir, idx) ||
450 is_active_nid(codec, nid, dir, idx))
451 return;
452 val = get_amp_val_to_activate(codec, nid, dir, enable);
453 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
454}
202 455
203/* 456static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
204 * look for the node record for the given NID 457 int i, bool enable)
205 */ 458{
206/* FIXME: should avoid the braindead linear search */ 459 hda_nid_t nid = path->path[i];
207static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid) 460 init_amp(codec, nid, HDA_OUTPUT, 0);
461 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
462}
463
464static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
465 int i, bool enable, bool add_aamix)
208{ 466{
209 struct hda_gnode *node; 467 struct hda_gen_spec *spec = codec->spec;
468 hda_nid_t conn[16];
469 int n, nums, idx;
470 int type;
471 hda_nid_t nid = path->path[i];
472
473 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
474 type = get_wcaps_type(get_wcaps(codec, nid));
475 if (type == AC_WID_PIN ||
476 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
477 nums = 1;
478 idx = 0;
479 } else
480 idx = path->idx[i];
481
482 for (n = 0; n < nums; n++)
483 init_amp(codec, nid, HDA_INPUT, n);
484
485 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
486 return;
210 487
211 list_for_each_entry(node, &spec->nid_list, list) { 488 /* here is a little bit tricky in comparison with activate_amp_out();
212 if (node->nid == nid) 489 * when aa-mixer is available, we need to enable the path as well
213 return node; 490 */
491 for (n = 0; n < nums; n++) {
492 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
493 continue;
494 activate_amp(codec, nid, HDA_INPUT, n, enable);
214 } 495 }
215 return NULL;
216} 496}
217 497
218/* 498/* activate or deactivate the given path
219 * unmute (and set max vol) the output amplifier 499 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
220 */ 500 */
221static int unmute_output(struct hda_codec *codec, struct hda_gnode *node) 501void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
222{ 502 bool enable, bool add_aamix)
223 unsigned int val, ofs; 503{
224 snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid); 504 int i;
225 val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; 505
226 ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT; 506 if (!enable)
227 if (val >= ofs) 507 path->active = false;
228 val -= ofs; 508
229 snd_hda_codec_amp_stereo(codec, node->nid, HDA_OUTPUT, 0, 0xff, val); 509 for (i = path->depth - 1; i >= 0; i--) {
230 return 0; 510 if (enable && path->multi[i])
511 snd_hda_codec_write_cache(codec, path->path[i], 0,
512 AC_VERB_SET_CONNECT_SEL,
513 path->idx[i]);
514 if (has_amp_in(codec, path, i))
515 activate_amp_in(codec, path, i, enable, add_aamix);
516 if (has_amp_out(codec, path, i))
517 activate_amp_out(codec, path, i, enable);
518 }
519
520 if (enable)
521 path->active = true;
231} 522}
523EXPORT_SYMBOL_HDA(snd_hda_activate_path);
524
232 525
233/* 526/*
234 * unmute (and set max vol) the input amplifier 527 * Helper functions for creating mixer ctl elements
235 */ 528 */
236static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index) 529
237{ 530enum {
238 unsigned int val, ofs; 531 HDA_CTL_WIDGET_VOL,
239 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index); 532 HDA_CTL_WIDGET_MUTE,
240 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; 533 HDA_CTL_BIND_MUTE,
241 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT; 534 HDA_CTL_BIND_VOL,
242 if (val >= ofs) 535 HDA_CTL_BIND_SW,
243 val -= ofs; 536};
244 snd_hda_codec_amp_stereo(codec, node->nid, HDA_INPUT, index, 0xff, val); 537static const struct snd_kcontrol_new control_templates[] = {
538 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
539 HDA_CODEC_MUTE(NULL, 0, 0, 0),
540 HDA_BIND_MUTE(NULL, 0, 0, 0),
541 HDA_BIND_VOL(NULL, 0),
542 HDA_BIND_SW(NULL, 0),
543};
544
545/* add dynamic controls from template */
546static int add_control(struct hda_gen_spec *spec, int type, const char *name,
547 int cidx, unsigned long val)
548{
549 struct snd_kcontrol_new *knew;
550
551 knew = add_kctl(spec, name, &control_templates[type]);
552 if (!knew)
553 return -ENOMEM;
554 knew->index = cidx;
555 if (get_amp_nid_(val))
556 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
557 knew->private_value = val;
245 return 0; 558 return 0;
246} 559}
247 560
248/* 561static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
249 * select the input connection of the given node. 562 const char *pfx, const char *dir,
250 */ 563 const char *sfx, int cidx, unsigned long val)
251static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
252 unsigned int index)
253{ 564{
254 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index); 565 char name[32];
255 return snd_hda_codec_write_cache(codec, node->nid, 0, 566 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
256 AC_VERB_SET_CONNECT_SEL, index); 567 return add_control(spec, type, name, cidx, val);
257} 568}
258 569
259/* 570#define add_pb_vol_ctrl(spec, type, pfx, val) \
260 * clear checked flag of each node in the node list 571 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
572#define add_pb_sw_ctrl(spec, type, pfx, val) \
573 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
574#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
575 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
576#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
577 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
578
579static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
580 unsigned int chs, struct nid_path *path)
581{
582 unsigned int val;
583 if (!path)
584 return 0;
585 val = path->ctls[NID_PATH_VOL_CTL];
586 if (!val)
587 return 0;
588 val = amp_val_replace_channels(val, chs);
589 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
590}
591
592/* return the channel bits suitable for the given path->ctls[] */
593static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
594 int type)
595{
596 int chs = 1; /* mono (left only) */
597 if (path) {
598 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
599 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
600 chs = 3; /* stereo */
601 }
602 return chs;
603}
604
605static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
606 struct nid_path *path)
607{
608 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
609 return add_vol_ctl(codec, pfx, cidx, chs, path);
610}
611
612/* create a mute-switch for the given mixer widget;
613 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
261 */ 614 */
262static void clear_check_flags(struct hda_gspec *spec) 615static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
616 unsigned int chs, struct nid_path *path)
617{
618 unsigned int val;
619 int type = HDA_CTL_WIDGET_MUTE;
620
621 if (!path)
622 return 0;
623 val = path->ctls[NID_PATH_MUTE_CTL];
624 if (!val)
625 return 0;
626 val = amp_val_replace_channels(val, chs);
627 if (get_amp_direction_(val) == HDA_INPUT) {
628 hda_nid_t nid = get_amp_nid_(val);
629 int nums = snd_hda_get_num_conns(codec, nid);
630 if (nums > 1) {
631 type = HDA_CTL_BIND_MUTE;
632 val |= nums << 19;
633 }
634 }
635 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
636}
637
638static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
639 int cidx, struct nid_path *path)
640{
641 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
642 return add_sw_ctl(codec, pfx, cidx, chs, path);
643}
644
645static const char * const channel_name[4] = {
646 "Front", "Surround", "CLFE", "Side"
647};
648
649/* give some appropriate ctl name prefix for the given line out channel */
650static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
651 bool can_be_master, int *index)
263{ 652{
264 struct hda_gnode *node; 653 struct auto_pin_cfg *cfg = &spec->autocfg;
265 654
266 list_for_each_entry(node, &spec->nid_list, list) { 655 *index = 0;
267 node->checked = 0; 656 if (cfg->line_outs == 1 && !spec->multi_ios &&
657 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
658 return spec->vmaster_mute.hook ? "PCM" : "Master";
659
660 /* if there is really a single DAC used in the whole output paths,
661 * use it master (or "PCM" if a vmaster hook is present)
662 */
663 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
664 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
665 return spec->vmaster_mute.hook ? "PCM" : "Master";
666
667 switch (cfg->line_out_type) {
668 case AUTO_PIN_SPEAKER_OUT:
669 if (cfg->line_outs == 1)
670 return "Speaker";
671 if (cfg->line_outs == 2)
672 return ch ? "Bass Speaker" : "Speaker";
673 break;
674 case AUTO_PIN_HP_OUT:
675 /* for multi-io case, only the primary out */
676 if (ch && spec->multi_ios)
677 break;
678 *index = ch;
679 return "Headphone";
680 default:
681 if (cfg->line_outs == 1 && !spec->multi_ios)
682 return "PCM";
683 break;
268 } 684 }
685 if (ch >= ARRAY_SIZE(channel_name)) {
686 snd_BUG();
687 return "PCM";
688 }
689
690 return channel_name[ch];
269} 691}
270 692
271/* 693/*
272 * parse the output path recursively until reach to an audio output widget 694 * Parse output paths
695 */
696
697/* badness definition */
698enum {
699 /* No primary DAC is found for the main output */
700 BAD_NO_PRIMARY_DAC = 0x10000,
701 /* No DAC is found for the extra output */
702 BAD_NO_DAC = 0x4000,
703 /* No possible multi-ios */
704 BAD_MULTI_IO = 0x103,
705 /* No individual DAC for extra output */
706 BAD_NO_EXTRA_DAC = 0x102,
707 /* No individual DAC for extra surrounds */
708 BAD_NO_EXTRA_SURR_DAC = 0x101,
709 /* Primary DAC shared with main surrounds */
710 BAD_SHARED_SURROUND = 0x100,
711 /* Primary DAC shared with main CLFE */
712 BAD_SHARED_CLFE = 0x10,
713 /* Primary DAC shared with extra surrounds */
714 BAD_SHARED_EXTRA_SURROUND = 0x10,
715 /* Volume widget is shared */
716 BAD_SHARED_VOL = 0x10,
717};
718
719/* look for widgets in the path between the given NIDs appropriate for
720 * volume and mute controls, and assign the values to ctls[].
273 * 721 *
274 * returns 0 if not found, 1 if found, or a negative error code. 722 * When no appropriate widget is found in the path, the badness value
723 * is incremented depending on the situation. The function returns the
724 * total badness for both volume and mute controls.
275 */ 725 */
276static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec, 726static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
277 struct hda_gnode *node, int dac_idx) 727 hda_nid_t dac)
278{ 728{
279 int i, err; 729 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
280 struct hda_gnode *child; 730 hda_nid_t nid;
731 unsigned int val;
732 int badness = 0;
733
734 if (!path)
735 return BAD_SHARED_VOL * 2;
736 nid = look_for_out_vol_nid(codec, path);
737 if (nid) {
738 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
739 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
740 badness += BAD_SHARED_VOL;
741 else
742 path->ctls[NID_PATH_VOL_CTL] = val;
743 } else
744 badness += BAD_SHARED_VOL;
745 nid = look_for_out_mute_nid(codec, path);
746 if (nid) {
747 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
748 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
749 nid_has_mute(codec, nid, HDA_OUTPUT))
750 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
751 else
752 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
753 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
754 badness += BAD_SHARED_VOL;
755 else
756 path->ctls[NID_PATH_MUTE_CTL] = val;
757 } else
758 badness += BAD_SHARED_VOL;
759 return badness;
760}
761
762struct badness_table {
763 int no_primary_dac; /* no primary DAC */
764 int no_dac; /* no secondary DACs */
765 int shared_primary; /* primary DAC is shared with main output */
766 int shared_surr; /* secondary DAC shared with main or primary */
767 int shared_clfe; /* third DAC shared with main or primary */
768 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
769};
770
771static struct badness_table main_out_badness = {
772 .no_primary_dac = BAD_NO_PRIMARY_DAC,
773 .no_dac = BAD_NO_DAC,
774 .shared_primary = BAD_NO_PRIMARY_DAC,
775 .shared_surr = BAD_SHARED_SURROUND,
776 .shared_clfe = BAD_SHARED_CLFE,
777 .shared_surr_main = BAD_SHARED_SURROUND,
778};
281 779
282 if (node->checked) 780static struct badness_table extra_out_badness = {
781 .no_primary_dac = BAD_NO_DAC,
782 .no_dac = BAD_NO_DAC,
783 .shared_primary = BAD_NO_EXTRA_DAC,
784 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
785 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
786 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
787};
788
789/* try to assign DACs to pins and return the resultant badness */
790static int try_assign_dacs(struct hda_codec *codec, int num_outs,
791 const hda_nid_t *pins, hda_nid_t *dacs,
792 const struct badness_table *bad)
793{
794 struct hda_gen_spec *spec = codec->spec;
795 struct auto_pin_cfg *cfg = &spec->autocfg;
796 int i, j;
797 int badness = 0;
798 hda_nid_t dac;
799
800 if (!num_outs)
283 return 0; 801 return 0;
284 802
285 node->checked = 1; 803 for (i = 0; i < num_outs; i++) {
286 if (node->type == AC_WID_AUD_OUT) { 804 hda_nid_t pin = pins[i];
287 if (node->wid_caps & AC_WCAP_DIGITAL) { 805 if (!dacs[i])
288 snd_printdd("Skip Digital OUT node %x\n", node->nid); 806 dacs[i] = look_for_dac(codec, pin, false);
289 return 0; 807 if (!dacs[i] && !i) {
290 } 808 for (j = 1; j < num_outs; j++) {
291 snd_printdd("AUD_OUT found %x\n", node->nid); 809 if (is_reachable_path(codec, dacs[j], pin)) {
292 if (spec->dac_node[dac_idx]) { 810 dacs[0] = dacs[j];
293 /* already DAC node is assigned, just unmute & connect */ 811 dacs[j] = 0;
294 return node == spec->dac_node[dac_idx]; 812 break;
813 }
814 }
295 } 815 }
296 spec->dac_node[dac_idx] = node; 816 dac = dacs[i];
297 if ((node->wid_caps & AC_WCAP_OUT_AMP) && 817 if (!dac) {
298 spec->pcm_vol_nodes < MAX_PCM_VOLS) { 818 if (is_reachable_path(codec, dacs[0], pin))
299 spec->pcm_vol[spec->pcm_vol_nodes].node = node; 819 dac = dacs[0];
300 spec->pcm_vol[spec->pcm_vol_nodes].index = 0; 820 else if (cfg->line_outs > i &&
301 spec->pcm_vol_nodes++; 821 is_reachable_path(codec, spec->private_dac_nids[i], pin))
822 dac = spec->private_dac_nids[i];
823 if (dac) {
824 if (!i)
825 badness += bad->shared_primary;
826 else if (i == 1)
827 badness += bad->shared_surr;
828 else
829 badness += bad->shared_clfe;
830 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
831 dac = spec->private_dac_nids[0];
832 badness += bad->shared_surr_main;
833 } else if (!i)
834 badness += bad->no_primary_dac;
835 else
836 badness += bad->no_dac;
302 } 837 }
303 return 1; /* found */ 838 if (!snd_hda_add_new_path(codec, dac, pin, 0))
839 dac = dacs[i] = 0;
840 if (dac)
841 badness += assign_out_path_ctls(codec, pin, dac);
304 } 842 }
305 843
306 for (i = 0; i < node->nconns; i++) { 844 return badness;
307 child = hda_get_node(spec, node->conn_list[i]); 845}
308 if (! child) 846
847/* return NID if the given pin has only a single connection to a certain DAC */
848static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
849{
850 struct hda_gen_spec *spec = codec->spec;
851 int i;
852 hda_nid_t nid_found = 0;
853
854 for (i = 0; i < spec->num_all_dacs; i++) {
855 hda_nid_t nid = spec->all_dacs[i];
856 if (!nid || is_dac_already_used(codec, nid))
309 continue; 857 continue;
310 err = parse_output_path(codec, spec, child, dac_idx); 858 if (is_reachable_path(codec, nid, pin)) {
311 if (err < 0) 859 if (nid_found)
312 return err; 860 return 0;
313 else if (err > 0) { 861 nid_found = nid;
314 /* found one,
315 * select the path, unmute both input and output
316 */
317 if (node->nconns > 1)
318 select_input_connection(codec, node, i);
319 unmute_input(codec, node, i);
320 unmute_output(codec, node);
321 if (spec->dac_node[dac_idx] &&
322 spec->pcm_vol_nodes < MAX_PCM_VOLS &&
323 !(spec->dac_node[dac_idx]->wid_caps &
324 AC_WCAP_OUT_AMP)) {
325 if ((node->wid_caps & AC_WCAP_IN_AMP) ||
326 (node->wid_caps & AC_WCAP_OUT_AMP)) {
327 int n = spec->pcm_vol_nodes;
328 spec->pcm_vol[n].node = node;
329 spec->pcm_vol[n].index = i;
330 spec->pcm_vol_nodes++;
331 }
332 }
333 return 1;
334 } 862 }
335 } 863 }
336 return 0; 864 return nid_found;
865}
866
867/* check whether the given pin can be a multi-io pin */
868static bool can_be_multiio_pin(struct hda_codec *codec,
869 unsigned int location, hda_nid_t nid)
870{
871 unsigned int defcfg, caps;
872
873 defcfg = snd_hda_codec_get_pincfg(codec, nid);
874 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
875 return false;
876 if (location && get_defcfg_location(defcfg) != location)
877 return false;
878 caps = snd_hda_query_pin_caps(codec, nid);
879 if (!(caps & AC_PINCAP_OUT))
880 return false;
881 return true;
337} 882}
338 883
339/* 884/*
340 * Look for the output PIN widget with the given jack type 885 * multi-io helper
341 * and parse the output path to that PIN.
342 * 886 *
343 * Returns the PIN node when the path to DAC is established. 887 * When hardwired is set, try to fill ony hardwired pins, and returns
888 * zero if any pins are filled, non-zero if nothing found.
889 * When hardwired is off, try to fill possible input pins, and returns
890 * the badness value.
344 */ 891 */
345static struct hda_gnode *parse_output_jack(struct hda_codec *codec, 892static int fill_multi_ios(struct hda_codec *codec,
346 struct hda_gspec *spec, 893 hda_nid_t reference_pin,
347 int jack_type) 894 bool hardwired, int offset)
348{ 895{
349 struct hda_gnode *node; 896 struct hda_gen_spec *spec = codec->spec;
350 int err; 897 struct auto_pin_cfg *cfg = &spec->autocfg;
898 int type, i, j, dacs, num_pins, old_pins;
899 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
900 unsigned int location = get_defcfg_location(defcfg);
901 int badness = 0;
902
903 old_pins = spec->multi_ios;
904 if (old_pins >= 2)
905 goto end_fill;
906
907 num_pins = 0;
908 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
909 for (i = 0; i < cfg->num_inputs; i++) {
910 if (cfg->inputs[i].type != type)
911 continue;
912 if (can_be_multiio_pin(codec, location,
913 cfg->inputs[i].pin))
914 num_pins++;
915 }
916 }
917 if (num_pins < 2)
918 goto end_fill;
351 919
352 list_for_each_entry(node, &spec->nid_list, list) { 920 dacs = spec->multiout.num_dacs;
353 if (node->type != AC_WID_PIN) 921 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
354 continue; 922 for (i = 0; i < cfg->num_inputs; i++) {
355 /* output capable? */ 923 hda_nid_t nid = cfg->inputs[i].pin;
356 if (! (node->pin_caps & AC_PINCAP_OUT)) 924 hda_nid_t dac = 0;
357 continue; 925
358 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE) 926 if (cfg->inputs[i].type != type)
359 continue; /* unconnected */
360 if (jack_type >= 0) {
361 if (jack_type != defcfg_type(node))
362 continue; 927 continue;
363 if (node->wid_caps & AC_WCAP_DIGITAL) 928 if (!can_be_multiio_pin(codec, location, nid))
364 continue; /* skip SPDIF */ 929 continue;
365 } else { 930 for (j = 0; j < spec->multi_ios; j++) {
366 /* output as default? */ 931 if (nid == spec->multi_io[j].pin)
367 if (! (node->pin_ctl & AC_PINCTL_OUT_EN)) 932 break;
933 }
934 if (j < spec->multi_ios)
368 continue; 935 continue;
936
937 if (offset && offset + spec->multi_ios < dacs) {
938 dac = spec->private_dac_nids[offset + spec->multi_ios];
939 if (!is_reachable_path(codec, dac, nid))
940 dac = 0;
941 }
942 if (hardwired)
943 dac = get_dac_if_single(codec, nid);
944 else if (!dac)
945 dac = look_for_dac(codec, nid, false);
946 if (!dac) {
947 badness++;
948 continue;
949 }
950 if (!snd_hda_add_new_path(codec, dac, nid, 0)) {
951 badness++;
952 continue;
953 }
954 spec->multi_io[spec->multi_ios].pin = nid;
955 spec->multi_io[spec->multi_ios].dac = dac;
956 spec->multi_ios++;
957 if (spec->multi_ios >= 2)
958 break;
959 }
960 }
961 end_fill:
962 if (badness)
963 badness = BAD_MULTI_IO;
964 if (old_pins == spec->multi_ios) {
965 if (hardwired)
966 return 1; /* nothing found */
967 else
968 return badness; /* no badness if nothing found */
969 }
970 if (!hardwired && spec->multi_ios < 2) {
971 /* cancel newly assigned paths */
972 spec->paths.used -= spec->multi_ios - old_pins;
973 spec->multi_ios = old_pins;
974 return badness;
975 }
976
977 /* assign volume and mute controls */
978 for (i = old_pins; i < spec->multi_ios; i++)
979 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
980 spec->multi_io[i].dac);
981
982 return badness;
983}
984
985/* map DACs for all pins in the list if they are single connections */
986static bool map_singles(struct hda_codec *codec, int outs,
987 const hda_nid_t *pins, hda_nid_t *dacs)
988{
989 int i;
990 bool found = false;
991 for (i = 0; i < outs; i++) {
992 hda_nid_t dac;
993 if (dacs[i])
994 continue;
995 dac = get_dac_if_single(codec, pins[i]);
996 if (!dac)
997 continue;
998 if (snd_hda_add_new_path(codec, dac, pins[i], 0)) {
999 dacs[i] = dac;
1000 found = true;
1001 }
1002 }
1003 return found;
1004}
1005
1006/* fill in the dac_nids table from the parsed pin configuration */
1007static int fill_and_eval_dacs(struct hda_codec *codec,
1008 bool fill_hardwired,
1009 bool fill_mio_first)
1010{
1011 struct hda_gen_spec *spec = codec->spec;
1012 struct auto_pin_cfg *cfg = &spec->autocfg;
1013 int i, err, badness;
1014
1015 /* set num_dacs once to full for look_for_dac() */
1016 spec->multiout.num_dacs = cfg->line_outs;
1017 spec->multiout.dac_nids = spec->private_dac_nids;
1018 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1019 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1020 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1021 spec->multi_ios = 0;
1022 snd_array_free(&spec->paths);
1023 badness = 0;
1024
1025 /* fill hard-wired DACs first */
1026 if (fill_hardwired) {
1027 bool mapped;
1028 do {
1029 mapped = map_singles(codec, cfg->line_outs,
1030 cfg->line_out_pins,
1031 spec->private_dac_nids);
1032 mapped |= map_singles(codec, cfg->hp_outs,
1033 cfg->hp_pins,
1034 spec->multiout.hp_out_nid);
1035 mapped |= map_singles(codec, cfg->speaker_outs,
1036 cfg->speaker_pins,
1037 spec->multiout.extra_out_nid);
1038 if (fill_mio_first && cfg->line_outs == 1 &&
1039 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1040 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1041 if (!err)
1042 mapped = true;
1043 }
1044 } while (mapped);
1045 }
1046
1047 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1048 spec->private_dac_nids,
1049 &main_out_badness);
1050
1051 /* re-count num_dacs and squash invalid entries */
1052 spec->multiout.num_dacs = 0;
1053 for (i = 0; i < cfg->line_outs; i++) {
1054 if (spec->private_dac_nids[i])
1055 spec->multiout.num_dacs++;
1056 else {
1057 memmove(spec->private_dac_nids + i,
1058 spec->private_dac_nids + i + 1,
1059 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1060 spec->private_dac_nids[cfg->line_outs - 1] = 0;
369 } 1061 }
370 clear_check_flags(spec); 1062 }
371 err = parse_output_path(codec, spec, node, 0); 1063
1064 if (fill_mio_first &&
1065 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1066 /* try to fill multi-io first */
1067 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
372 if (err < 0) 1068 if (err < 0)
373 return NULL; 1069 return err;
374 if (! err && spec->out_pin_node[0]) { 1070 /* we don't count badness at this stage yet */
375 err = parse_output_path(codec, spec, node, 1); 1071 }
376 if (err < 0) 1072
377 return NULL; 1073 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1074 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1075 spec->multiout.hp_out_nid,
1076 &extra_out_badness);
1077 if (err < 0)
1078 return err;
1079 badness += err;
1080 }
1081 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1082 err = try_assign_dacs(codec, cfg->speaker_outs,
1083 cfg->speaker_pins,
1084 spec->multiout.extra_out_nid,
1085 &extra_out_badness);
1086 if (err < 0)
1087 return err;
1088 badness += err;
1089 }
1090 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1091 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1092 if (err < 0)
1093 return err;
1094 badness += err;
1095 }
1096 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1097 /* try multi-ios with HP + inputs */
1098 int offset = 0;
1099 if (cfg->line_outs >= 3)
1100 offset = 1;
1101 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1102 if (err < 0)
1103 return err;
1104 badness += err;
1105 }
1106
1107 if (spec->multi_ios == 2) {
1108 for (i = 0; i < 2; i++)
1109 spec->private_dac_nids[spec->multiout.num_dacs++] =
1110 spec->multi_io[i].dac;
1111 spec->ext_channel_count = 2;
1112 } else if (spec->multi_ios) {
1113 spec->multi_ios = 0;
1114 badness += BAD_MULTI_IO;
1115 }
1116
1117 return badness;
1118}
1119
1120#define DEBUG_BADNESS
1121
1122#ifdef DEBUG_BADNESS
1123#define debug_badness snd_printdd
1124#else
1125#define debug_badness(...)
1126#endif
1127
1128static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1129{
1130 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1131 cfg->line_out_pins[0], cfg->line_out_pins[1],
1132 cfg->line_out_pins[2], cfg->line_out_pins[2],
1133 spec->multiout.dac_nids[0],
1134 spec->multiout.dac_nids[1],
1135 spec->multiout.dac_nids[2],
1136 spec->multiout.dac_nids[3]);
1137 if (spec->multi_ios > 0)
1138 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1139 spec->multi_ios,
1140 spec->multi_io[0].pin, spec->multi_io[1].pin,
1141 spec->multi_io[0].dac, spec->multi_io[1].dac);
1142 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1143 cfg->hp_pins[0], cfg->hp_pins[1],
1144 cfg->hp_pins[2], cfg->hp_pins[2],
1145 spec->multiout.hp_out_nid[0],
1146 spec->multiout.hp_out_nid[1],
1147 spec->multiout.hp_out_nid[2],
1148 spec->multiout.hp_out_nid[3]);
1149 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1150 cfg->speaker_pins[0], cfg->speaker_pins[1],
1151 cfg->speaker_pins[2], cfg->speaker_pins[3],
1152 spec->multiout.extra_out_nid[0],
1153 spec->multiout.extra_out_nid[1],
1154 spec->multiout.extra_out_nid[2],
1155 spec->multiout.extra_out_nid[3]);
1156}
1157
1158/* find all available DACs of the codec */
1159static void fill_all_dac_nids(struct hda_codec *codec)
1160{
1161 struct hda_gen_spec *spec = codec->spec;
1162 int i;
1163 hda_nid_t nid = codec->start_nid;
1164
1165 spec->num_all_dacs = 0;
1166 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1167 for (i = 0; i < codec->num_nodes; i++, nid++) {
1168 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1169 continue;
1170 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1171 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1172 break;
1173 }
1174 spec->all_dacs[spec->num_all_dacs++] = nid;
1175 }
1176}
1177
1178static int parse_output_paths(struct hda_codec *codec)
1179{
1180 struct hda_gen_spec *spec = codec->spec;
1181 struct auto_pin_cfg *cfg = &spec->autocfg;
1182 struct auto_pin_cfg *best_cfg;
1183 int best_badness = INT_MAX;
1184 int badness;
1185 bool fill_hardwired = true, fill_mio_first = true;
1186 bool best_wired = true, best_mio = true;
1187 bool hp_spk_swapped = false;
1188
1189 fill_all_dac_nids(codec);
1190
1191 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1192 if (!best_cfg)
1193 return -ENOMEM;
1194 *best_cfg = *cfg;
1195
1196 for (;;) {
1197 badness = fill_and_eval_dacs(codec, fill_hardwired,
1198 fill_mio_first);
1199 if (badness < 0) {
1200 kfree(best_cfg);
1201 return badness;
378 } 1202 }
379 if (err > 0) { 1203 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
380 /* unmute the PIN output */ 1204 cfg->line_out_type, fill_hardwired, fill_mio_first,
381 unmute_output(codec, node); 1205 badness);
382 /* set PIN-Out enable */ 1206 debug_show_configs(spec, cfg);
383 snd_hda_codec_write_cache(codec, node->nid, 0, 1207 if (badness < best_badness) {
384 AC_VERB_SET_PIN_WIDGET_CONTROL, 1208 best_badness = badness;
385 AC_PINCTL_OUT_EN | 1209 *best_cfg = *cfg;
386 ((node->pin_caps & AC_PINCAP_HP_DRV) ? 1210 best_wired = fill_hardwired;
387 AC_PINCTL_HP_EN : 0)); 1211 best_mio = fill_mio_first;
388 return node;
389 } 1212 }
1213 if (!badness)
1214 break;
1215 fill_mio_first = !fill_mio_first;
1216 if (!fill_mio_first)
1217 continue;
1218 fill_hardwired = !fill_hardwired;
1219 if (!fill_hardwired)
1220 continue;
1221 if (hp_spk_swapped)
1222 break;
1223 hp_spk_swapped = true;
1224 if (cfg->speaker_outs > 0 &&
1225 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1226 cfg->hp_outs = cfg->line_outs;
1227 memcpy(cfg->hp_pins, cfg->line_out_pins,
1228 sizeof(cfg->hp_pins));
1229 cfg->line_outs = cfg->speaker_outs;
1230 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1231 sizeof(cfg->speaker_pins));
1232 cfg->speaker_outs = 0;
1233 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1234 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1235 fill_hardwired = true;
1236 continue;
1237 }
1238 if (cfg->hp_outs > 0 &&
1239 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1240 cfg->speaker_outs = cfg->line_outs;
1241 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1242 sizeof(cfg->speaker_pins));
1243 cfg->line_outs = cfg->hp_outs;
1244 memcpy(cfg->line_out_pins, cfg->hp_pins,
1245 sizeof(cfg->hp_pins));
1246 cfg->hp_outs = 0;
1247 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1248 cfg->line_out_type = AUTO_PIN_HP_OUT;
1249 fill_hardwired = true;
1250 continue;
1251 }
1252 break;
390 } 1253 }
391 return NULL; 1254
1255 if (badness) {
1256 *cfg = *best_cfg;
1257 fill_and_eval_dacs(codec, best_wired, best_mio);
1258 }
1259 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1260 cfg->line_out_type, best_wired, best_mio);
1261 debug_show_configs(spec, cfg);
1262
1263 if (cfg->line_out_pins[0]) {
1264 struct nid_path *path;
1265 path = snd_hda_get_nid_path(codec,
1266 spec->multiout.dac_nids[0],
1267 cfg->line_out_pins[0]);
1268 if (path)
1269 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1270 }
1271
1272 kfree(best_cfg);
1273 return 0;
392} 1274}
393 1275
1276/* add playback controls from the parsed DAC table */
1277static int create_multi_out_ctls(struct hda_codec *codec,
1278 const struct auto_pin_cfg *cfg)
1279{
1280 struct hda_gen_spec *spec = codec->spec;
1281 int i, err, noutputs;
394 1282
395/* 1283 noutputs = cfg->line_outs;
396 * parse outputs 1284 if (spec->multi_ios > 0 && cfg->line_outs < 3)
397 */ 1285 noutputs += spec->multi_ios;
398static int parse_output(struct hda_codec *codec) 1286
1287 for (i = 0; i < noutputs; i++) {
1288 const char *name;
1289 int index;
1290 hda_nid_t dac, pin;
1291 struct nid_path *path;
1292
1293 dac = spec->multiout.dac_nids[i];
1294 if (!dac)
1295 continue;
1296 if (i >= cfg->line_outs) {
1297 pin = spec->multi_io[i - 1].pin;
1298 index = 0;
1299 name = channel_name[i];
1300 } else {
1301 pin = cfg->line_out_pins[i];
1302 name = get_line_out_pfx(spec, i, true, &index);
1303 }
1304
1305 path = snd_hda_get_nid_path(codec, dac, pin);
1306 if (!path)
1307 continue;
1308 if (!name || !strcmp(name, "CLFE")) {
1309 /* Center/LFE */
1310 err = add_vol_ctl(codec, "Center", 0, 1, path);
1311 if (err < 0)
1312 return err;
1313 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1314 if (err < 0)
1315 return err;
1316 err = add_sw_ctl(codec, "Center", 0, 1, path);
1317 if (err < 0)
1318 return err;
1319 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1320 if (err < 0)
1321 return err;
1322 } else {
1323 err = add_stereo_vol(codec, name, index, path);
1324 if (err < 0)
1325 return err;
1326 err = add_stereo_sw(codec, name, index, path);
1327 if (err < 0)
1328 return err;
1329 }
1330 }
1331 return 0;
1332}
1333
1334static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1335 hda_nid_t dac, const char *pfx, int cidx)
399{ 1336{
400 struct hda_gspec *spec = codec->spec; 1337 struct nid_path *path;
401 struct hda_gnode *node; 1338 int err;
402 1339
403 /* 1340 path = snd_hda_get_nid_path(codec, dac, pin);
404 * Look for the output PIN widget 1341 if (!path)
405 */ 1342 return 0;
406 /* first, look for the line-out pin */ 1343 /* bind volume control will be created in the case of dac = 0 */
407 node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT); 1344 if (dac) {
408 if (node) /* found, remember the PIN node */ 1345 err = add_stereo_vol(codec, pfx, cidx, path);
409 spec->out_pin_node[0] = node; 1346 if (err < 0)
410 else { 1347 return err;
411 /* if no line-out is found, try speaker out */ 1348 }
412 node = parse_output_jack(codec, spec, AC_JACK_SPEAKER); 1349 err = add_stereo_sw(codec, pfx, cidx, path);
413 if (node) 1350 if (err < 0)
414 spec->out_pin_node[0] = node; 1351 return err;
415 } 1352 return 0;
416 /* look for the HP-out pin */ 1353}
417 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT); 1354
418 if (node) { 1355/* add playback controls for speaker and HP outputs */
419 if (! spec->out_pin_node[0]) 1356static int create_extra_outs(struct hda_codec *codec, int num_pins,
420 spec->out_pin_node[0] = node; 1357 const hda_nid_t *pins, const hda_nid_t *dacs,
421 else 1358 const char *pfx)
422 spec->out_pin_node[1] = node; 1359{
1360 struct hda_gen_spec *spec = codec->spec;
1361 struct hda_bind_ctls *ctl;
1362 char name[32];
1363 int i, n, err;
1364
1365 if (!num_pins || !pins[0])
1366 return 0;
1367
1368 if (num_pins == 1) {
1369 hda_nid_t dac = *dacs;
1370 if (!dac)
1371 dac = spec->multiout.dac_nids[0];
1372 return create_extra_out(codec, *pins, dac, pfx, 0);
423 } 1373 }
424 1374
425 if (! spec->out_pin_node[0]) { 1375 for (i = 0; i < num_pins; i++) {
426 /* no line-out or HP pins found, 1376 hda_nid_t dac;
427 * then choose for the first output pin 1377 if (dacs[num_pins - 1])
428 */ 1378 dac = dacs[i]; /* with individual volumes */
429 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1); 1379 else
430 if (! spec->out_pin_node[0]) 1380 dac = 0;
431 snd_printd("hda_generic: no proper output path found\n"); 1381 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1382 err = create_extra_out(codec, pins[i], dac,
1383 "Bass Speaker", 0);
1384 } else if (num_pins >= 3) {
1385 snprintf(name, sizeof(name), "%s %s",
1386 pfx, channel_name[i]);
1387 err = create_extra_out(codec, pins[i], dac, name, 0);
1388 } else {
1389 err = create_extra_out(codec, pins[i], dac, pfx, i);
1390 }
1391 if (err < 0)
1392 return err;
432 } 1393 }
1394 if (dacs[num_pins - 1])
1395 return 0;
433 1396
1397 /* Let's create a bind-controls for volumes */
1398 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1399 if (!ctl)
1400 return -ENOMEM;
1401 n = 0;
1402 for (i = 0; i < num_pins; i++) {
1403 hda_nid_t vol;
1404 struct nid_path *path;
1405 if (!pins[i] || !dacs[i])
1406 continue;
1407 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1408 if (!path)
1409 continue;
1410 vol = look_for_out_vol_nid(codec, path);
1411 if (vol)
1412 ctl->values[n++] =
1413 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1414 }
1415 if (n) {
1416 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1417 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1418 if (err < 0)
1419 return err;
1420 }
434 return 0; 1421 return 0;
435} 1422}
436 1423
1424static int create_hp_out_ctls(struct hda_codec *codec)
1425{
1426 struct hda_gen_spec *spec = codec->spec;
1427 return create_extra_outs(codec, spec->autocfg.hp_outs,
1428 spec->autocfg.hp_pins,
1429 spec->multiout.hp_out_nid,
1430 "Headphone");
1431}
1432
1433static int create_speaker_out_ctls(struct hda_codec *codec)
1434{
1435 struct hda_gen_spec *spec = codec->spec;
1436 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1437 spec->autocfg.speaker_pins,
1438 spec->multiout.extra_out_nid,
1439 "Speaker");
1440}
1441
437/* 1442/*
438 * input MUX 1443 * channel mode enum control
439 */ 1444 */
440 1445
441/* control callbacks */ 1446static int ch_mode_info(struct snd_kcontrol *kcontrol,
442static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 1447 struct snd_ctl_elem_info *uinfo)
443{ 1448{
444 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 1449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
445 struct hda_gspec *spec = codec->spec; 1450 struct hda_gen_spec *spec = codec->spec;
446 return snd_hda_input_mux_info(&spec->input_mux, uinfo); 1451
1452 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1453 uinfo->count = 1;
1454 uinfo->value.enumerated.items = spec->multi_ios + 1;
1455 if (uinfo->value.enumerated.item > spec->multi_ios)
1456 uinfo->value.enumerated.item = spec->multi_ios;
1457 sprintf(uinfo->value.enumerated.name, "%dch",
1458 (uinfo->value.enumerated.item + 1) * 2);
1459 return 0;
447} 1460}
448 1461
449static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 1462static int ch_mode_get(struct snd_kcontrol *kcontrol,
1463 struct snd_ctl_elem_value *ucontrol)
450{ 1464{
451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 1465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
452 struct hda_gspec *spec = codec->spec; 1466 struct hda_gen_spec *spec = codec->spec;
1467 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1468 return 0;
1469}
1470
1471static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1472{
1473 struct hda_gen_spec *spec = codec->spec;
1474 hda_nid_t nid = spec->multi_io[idx].pin;
1475 struct nid_path *path;
1476
1477 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1478 if (!path)
1479 return -EINVAL;
1480
1481 if (path->active == output)
1482 return 0;
453 1483
454 ucontrol->value.enumerated.item[0] = spec->cur_cap_src; 1484 if (output) {
1485 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1486 snd_hda_activate_path(codec, path, true, true);
1487 } else {
1488 snd_hda_activate_path(codec, path, false, true);
1489 snd_hda_set_pin_ctl_cache(codec, nid,
1490 spec->multi_io[idx].ctl_in);
1491 }
455 return 0; 1492 return 0;
456} 1493}
457 1494
458static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 1495static int ch_mode_put(struct snd_kcontrol *kcontrol,
1496 struct snd_ctl_elem_value *ucontrol)
459{ 1497{
460 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 1498 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
461 struct hda_gspec *spec = codec->spec; 1499 struct hda_gen_spec *spec = codec->spec;
462 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol, 1500 int i, ch;
463 spec->adc_node->nid, &spec->cur_cap_src); 1501
1502 ch = ucontrol->value.enumerated.item[0];
1503 if (ch < 0 || ch > spec->multi_ios)
1504 return -EINVAL;
1505 if (ch == (spec->ext_channel_count - 1) / 2)
1506 return 0;
1507 spec->ext_channel_count = (ch + 1) * 2;
1508 for (i = 0; i < spec->multi_ios; i++)
1509 set_multi_io(codec, i, i < ch);
1510 spec->multiout.max_channels = max(spec->ext_channel_count,
1511 spec->const_channel_count);
1512 if (spec->need_dac_fix)
1513 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1514 return 1;
464} 1515}
465 1516
466/* 1517static const struct snd_kcontrol_new channel_mode_enum = {
467 * return the string name of the given input PIN widget 1518 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
468 */ 1519 .name = "Channel Mode",
469static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl) 1520 .info = ch_mode_info,
470{ 1521 .get = ch_mode_get,
471 unsigned int location = defcfg_location(node); 1522 .put = ch_mode_put,
472 switch (defcfg_type(node)) { 1523};
473 case AC_JACK_LINE_IN: 1524
474 if ((location & 0x0f) == AC_JACK_LOC_FRONT) 1525static int create_multi_channel_mode(struct hda_codec *codec)
475 return "Front Line"; 1526{
476 return "Line"; 1527 struct hda_gen_spec *spec = codec->spec;
477 case AC_JACK_CD: 1528
478#if 0 1529 if (spec->multi_ios > 0) {
479 if (pinctl) 1530 if (!add_kctl(spec, NULL, &channel_mode_enum))
480 *pinctl |= AC_PINCTL_VREF_GRD; 1531 return -ENOMEM;
481#endif
482 return "CD";
483 case AC_JACK_AUX:
484 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
485 return "Front Aux";
486 return "Aux";
487 case AC_JACK_MIC_IN:
488 if (pinctl &&
489 (node->pin_caps &
490 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
491 *pinctl |= AC_PINCTL_VREF_80;
492 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
493 return "Front Mic";
494 return "Mic";
495 case AC_JACK_SPDIF_IN:
496 return "SPDIF";
497 case AC_JACK_DIG_OTHER_IN:
498 return "Digital";
499 } 1532 }
500 return NULL; 1533 return 0;
501} 1534}
502 1535
503/* 1536/*
504 * parse the nodes recursively until reach to the input PIN 1537 * shared headphone/mic handling
505 *
506 * returns 0 if not found, 1 if found, or a negative error code.
507 */ 1538 */
508static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec, 1539
509 struct hda_gnode *node, int idx) 1540static void call_update_outputs(struct hda_codec *codec);
1541
1542/* for shared I/O, change the pin-control accordingly */
1543static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
510{ 1544{
511 int i, err; 1545 struct hda_gen_spec *spec = codec->spec;
512 unsigned int pinctl; 1546 unsigned int val;
513 const char *type; 1547 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1548 /* NOTE: this assumes that there are only two inputs, the
1549 * first is the real internal mic and the second is HP/mic jack.
1550 */
514 1551
515 if (node->checked) 1552 val = snd_hda_get_default_vref(codec, pin);
516 return 0;
517 1553
518 node->checked = 1; 1554 /* This pin does not have vref caps - let's enable vref on pin 0x18
519 if (node->type != AC_WID_PIN) { 1555 instead, as suggested by Realtek */
520 for (i = 0; i < node->nconns; i++) { 1556 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
521 struct hda_gnode *child; 1557 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
522 child = hda_get_node(spec, node->conn_list[i]); 1558 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
523 if (! child) 1559 if (vref_val != AC_PINCTL_VREF_HIZ)
524 continue; 1560 snd_hda_set_pin_ctl(codec, vref_pin, PIN_IN | (set_as_mic ? vref_val : 0));
525 err = parse_adc_sub_nodes(codec, spec, child, idx);
526 if (err < 0)
527 return err;
528 if (err > 0) {
529 /* found one,
530 * select the path, unmute both input and output
531 */
532 if (node->nconns > 1)
533 select_input_connection(codec, node, i);
534 unmute_input(codec, node, i);
535 unmute_output(codec, node);
536 return err;
537 }
538 }
539 return 0;
540 } 1561 }
541 1562
542 /* input capable? */ 1563 val = set_as_mic ? val | PIN_IN : PIN_HP;
543 if (! (node->pin_caps & AC_PINCAP_IN)) 1564 snd_hda_set_pin_ctl(codec, pin, val);
1565
1566 spec->automute_speaker = !set_as_mic;
1567 call_update_outputs(codec);
1568}
1569
1570/* create a shared input with the headphone out */
1571static int create_shared_input(struct hda_codec *codec)
1572{
1573 struct hda_gen_spec *spec = codec->spec;
1574 struct auto_pin_cfg *cfg = &spec->autocfg;
1575 unsigned int defcfg;
1576 hda_nid_t nid;
1577
1578 /* only one internal input pin? */
1579 if (cfg->num_inputs != 1)
1580 return 0;
1581 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1582 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
544 return 0; 1583 return 0;
545 1584
546 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE) 1585 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
547 return 0; /* unconnected */ 1586 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1587 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1588 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1589 else
1590 return 0; /* both not available */
1591
1592 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1593 return 0; /* no input */
1594
1595 cfg->inputs[1].pin = nid;
1596 cfg->inputs[1].type = AUTO_PIN_MIC;
1597 cfg->num_inputs = 2;
1598 spec->shared_mic_hp = 1;
1599 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1600 return 0;
1601}
1602
1603
1604/*
1605 * Parse input paths
1606 */
1607
1608#ifdef CONFIG_PM
1609/* add the powersave loopback-list entry */
1610static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1611{
1612 struct hda_amp_list *list;
1613
1614 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1615 return;
1616 list = spec->loopback_list + spec->num_loopbacks;
1617 list->nid = mix;
1618 list->dir = HDA_INPUT;
1619 list->idx = idx;
1620 spec->num_loopbacks++;
1621 spec->loopback.amplist = spec->loopback_list;
1622}
1623#else
1624#define add_loopback_list(spec, mix, idx) /* NOP */
1625#endif
1626
1627/* create input playback/capture controls for the given pin */
1628static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1629 const char *ctlname, int ctlidx,
1630 hda_nid_t mix_nid)
1631{
1632 struct hda_gen_spec *spec = codec->spec;
1633 struct nid_path *path;
1634 unsigned int val;
1635 int err, idx;
548 1636
549 if (node->wid_caps & AC_WCAP_DIGITAL) 1637 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
550 return 0; /* skip SPDIF */ 1638 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1639 return 0; /* no need for analog loopback */
551 1640
552 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) { 1641 path = snd_hda_add_new_path(codec, pin, mix_nid, 2);
553 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n"); 1642 if (!path)
554 return -EINVAL; 1643 return -EINVAL;
1644
1645 idx = path->idx[path->depth - 1];
1646 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1647 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1648 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1649 if (err < 0)
1650 return err;
1651 path->ctls[NID_PATH_VOL_CTL] = val;
555 } 1652 }
556 1653
557 pinctl = AC_PINCTL_IN_EN; 1654 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
558 /* create a proper capture source label */ 1655 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
559 type = get_input_type(node, &pinctl); 1656 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
560 if (! type) { 1657 if (err < 0)
561 /* input as default? */ 1658 return err;
562 if (! (node->pin_ctl & AC_PINCTL_IN_EN)) 1659 path->ctls[NID_PATH_MUTE_CTL] = val;
563 return 0;
564 type = "Input";
565 } 1660 }
566 snd_hda_add_imux_item(&spec->input_mux, type, idx, NULL);
567 1661
568 /* unmute the PIN external input */ 1662 path->active = true;
569 unmute_input(codec, node, 0); /* index = 0? */ 1663 add_loopback_list(spec, mix_nid, idx);
570 /* set PIN-In enable */ 1664 return 0;
571 snd_hda_codec_write_cache(codec, node->nid, 0, 1665}
572 AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
573 1666
574 return 1; /* found */ 1667static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1668{
1669 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1670 return (pincap & AC_PINCAP_IN) != 0;
575} 1671}
576 1672
577/* 1673/* Parse the codec tree and retrieve ADCs */
578 * parse input 1674static int fill_adc_nids(struct hda_codec *codec)
579 */
580static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
581{ 1675{
582 struct hda_gspec *spec = codec->spec; 1676 struct hda_gen_spec *spec = codec->spec;
583 struct hda_gnode *node; 1677 hda_nid_t nid;
584 int i, err; 1678 hda_nid_t *adc_nids = spec->adc_nids;
1679 int max_nums = ARRAY_SIZE(spec->adc_nids);
1680 int i, nums = 0;
585 1681
586 snd_printdd("AUD_IN = %x\n", adc_node->nid); 1682 nid = codec->start_nid;
587 clear_check_flags(spec); 1683 for (i = 0; i < codec->num_nodes; i++, nid++) {
1684 unsigned int caps = get_wcaps(codec, nid);
1685 int type = get_wcaps_type(caps);
588 1686
589 // awk added - fixed no recording due to muted widget 1687 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
590 unmute_input(codec, adc_node, 0); 1688 continue;
591 1689 adc_nids[nums] = nid;
592 /* 1690 if (++nums >= max_nums)
593 * check each connection of the ADC 1691 break;
594 * if it reaches to a proper input PIN, add the path as the 1692 }
595 * input path. 1693 spec->num_adc_nids = nums;
596 */ 1694 return nums;
597 /* first, check the direct connections to PIN widgets */ 1695}
598 for (i = 0; i < adc_node->nconns; i++) { 1696
599 node = hda_get_node(spec, adc_node->conn_list[i]); 1697/* filter out invalid adc_nids that don't give all active input pins;
600 if (node && node->type == AC_WID_PIN) { 1698 * if needed, check whether dynamic ADC-switching is available
601 err = parse_adc_sub_nodes(codec, spec, node, i); 1699 */
602 if (err < 0) 1700static int check_dyn_adc_switch(struct hda_codec *codec)
603 return err; 1701{
1702 struct hda_gen_spec *spec = codec->spec;
1703 struct hda_input_mux *imux = &spec->input_mux;
1704 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1705 int i, n, nums;
1706 hda_nid_t pin, adc;
1707
1708 again:
1709 nums = 0;
1710 for (n = 0; n < spec->num_adc_nids; n++) {
1711 adc = spec->adc_nids[n];
1712 for (i = 0; i < imux->num_items; i++) {
1713 pin = spec->imux_pins[i];
1714 if (!is_reachable_path(codec, pin, adc))
1715 break;
604 } 1716 }
1717 if (i >= imux->num_items)
1718 adc_nids[nums++] = adc;
605 } 1719 }
606 /* ... then check the rests, more complicated connections */ 1720
607 for (i = 0; i < adc_node->nconns; i++) { 1721 if (!nums) {
608 node = hda_get_node(spec, adc_node->conn_list[i]); 1722 if (spec->shared_mic_hp) {
609 if (node && node->type != AC_WID_PIN) { 1723 spec->shared_mic_hp = 0;
610 err = parse_adc_sub_nodes(codec, spec, node, i); 1724 imux->num_items = 1;
611 if (err < 0) 1725 goto again;
612 return err; 1726 }
1727
1728 /* check whether ADC-switch is possible */
1729 for (i = 0; i < imux->num_items; i++) {
1730 pin = spec->imux_pins[i];
1731 for (n = 0; n < spec->num_adc_nids; n++) {
1732 adc = spec->adc_nids[n];
1733 if (is_reachable_path(codec, pin, adc)) {
1734 spec->dyn_adc_idx[i] = n;
1735 break;
1736 }
1737 }
613 } 1738 }
1739
1740 snd_printdd("hda-codec: enabling ADC switching\n");
1741 spec->dyn_adc_switch = 1;
1742 } else if (nums != spec->num_adc_nids) {
1743 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1744 spec->num_adc_nids = nums;
614 } 1745 }
615 1746
616 if (! spec->input_mux.num_items) 1747 if (imux->num_items == 1 || spec->shared_mic_hp) {
617 return 0; /* no input path found... */ 1748 snd_printdd("hda-codec: reducing to a single ADC\n");
1749 spec->num_adc_nids = 1; /* reduce to a single ADC */
1750 }
618 1751
619 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items); 1752 /* single index for individual volumes ctls */
620 for (i = 0; i < spec->input_mux.num_items; i++) 1753 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
621 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label, 1754 spec->num_adc_nids = 1;
622 spec->input_mux.items[i].index);
623 1755
624 spec->adc_node = adc_node; 1756 return 0;
625 return 1;
626} 1757}
627 1758
628/* 1759/*
629 * parse input 1760 * create playback/capture controls for input pins
630 */ 1761 */
631static int parse_input(struct hda_codec *codec) 1762static int create_input_ctls(struct hda_codec *codec)
632{ 1763{
633 struct hda_gspec *spec = codec->spec; 1764 struct hda_gen_spec *spec = codec->spec;
634 struct hda_gnode *node; 1765 const struct auto_pin_cfg *cfg = &spec->autocfg;
635 int err; 1766 hda_nid_t mixer = spec->mixer_nid;
1767 struct hda_input_mux *imux = &spec->input_mux;
1768 int num_adcs;
1769 int i, c, err, type_idx = 0;
1770 const char *prev_label = NULL;
1771
1772 num_adcs = fill_adc_nids(codec);
1773 if (num_adcs < 0)
1774 return 0;
636 1775
637 /* 1776 for (i = 0; i < cfg->num_inputs; i++) {
638 * At first we look for an audio input widget. 1777 hda_nid_t pin;
639 * If it reaches to certain input PINs, we take it as the 1778 const char *label;
640 * input path. 1779 bool imux_added;
641 */ 1780
642 list_for_each_entry(node, &spec->nid_list, list) { 1781 pin = cfg->inputs[i].pin;
643 if (node->wid_caps & AC_WCAP_DIGITAL) 1782 if (!is_input_pin(codec, pin))
644 continue; /* skip SPDIF */ 1783 continue;
645 if (node->type == AC_WID_AUD_IN) { 1784
646 err = parse_input_path(codec, node); 1785 label = hda_get_autocfg_input_label(codec, cfg, i);
647 if (err < 0) 1786 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
648 return err; 1787 label = "Headphone Mic";
649 else if (err > 0) 1788 if (prev_label && !strcmp(label, prev_label))
650 return 0; 1789 type_idx++;
1790 else
1791 type_idx = 0;
1792 prev_label = label;
1793
1794 if (mixer) {
1795 if (is_reachable_path(codec, pin, mixer)) {
1796 err = new_analog_input(codec, pin,
1797 label, type_idx, mixer);
1798 if (err < 0)
1799 return err;
1800 }
1801 }
1802
1803 imux_added = false;
1804 for (c = 0; c < num_adcs; c++) {
1805 struct nid_path *path;
1806 hda_nid_t adc = spec->adc_nids[c];
1807
1808 if (!is_reachable_path(codec, pin, adc))
1809 continue;
1810 path = snd_array_new(&spec->paths);
1811 if (!path)
1812 return -ENOMEM;
1813 memset(path, 0, sizeof(*path));
1814 if (!snd_hda_parse_nid_path(codec, pin, adc, 2, path)) {
1815 snd_printd(KERN_ERR
1816 "invalid input path 0x%x -> 0x%x\n",
1817 pin, adc);
1818 spec->paths.used--;
1819 continue;
1820 }
1821
1822 if (!imux_added) {
1823 spec->imux_pins[imux->num_items] = pin;
1824 snd_hda_add_imux_item(imux, label,
1825 imux->num_items, NULL);
1826 imux_added = true;
1827 }
651 } 1828 }
652 } 1829 }
653 snd_printd("hda_generic: no proper input path found\n"); 1830
654 return 0; 1831 return 0;
655} 1832}
656 1833
657#ifdef CONFIG_PM 1834
658static void add_input_loopback(struct hda_codec *codec, hda_nid_t nid, 1835/*
659 int dir, int idx) 1836 * input source mux
1837 */
1838
1839/* get the ADC NID corresponding to the given index */
1840static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
660{ 1841{
661 struct hda_gspec *spec = codec->spec; 1842 struct hda_gen_spec *spec = codec->spec;
662 struct hda_amp_list *p; 1843 if (spec->dyn_adc_switch)
1844 adc_idx = spec->dyn_adc_idx[imux_idx];
1845 return spec->adc_nids[adc_idx];
1846}
663 1847
664 if (spec->num_loopbacks >= MAX_LOOPBACK_AMPS) { 1848static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
665 snd_printk(KERN_ERR "hda_generic: Too many loopback ctls\n"); 1849 unsigned int idx);
666 return; 1850
667 } 1851static int mux_enum_info(struct snd_kcontrol *kcontrol,
668 p = &spec->loopback_list[spec->num_loopbacks++]; 1852 struct snd_ctl_elem_info *uinfo)
669 p->nid = nid; 1853{
670 p->dir = dir; 1854 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
671 p->idx = idx; 1855 struct hda_gen_spec *spec = codec->spec;
672 spec->loopback.amplist = spec->loopback_list; 1856 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1857}
1858
1859static int mux_enum_get(struct snd_kcontrol *kcontrol,
1860 struct snd_ctl_elem_value *ucontrol)
1861{
1862 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1863 struct hda_gen_spec *spec = codec->spec;
1864 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1865
1866 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1867 return 0;
1868}
1869
1870static int mux_enum_put(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_value *ucontrol)
1872{
1873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1874 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1875 return mux_select(codec, adc_idx,
1876 ucontrol->value.enumerated.item[0]);
673} 1877}
674#else
675#define add_input_loopback(codec,nid,dir,idx)
676#endif
677 1878
678/* 1879/*
679 * create mixer controls if possible 1880 * capture volume and capture switch ctls
680 */ 1881 */
681static int create_mixer(struct hda_codec *codec, struct hda_gnode *node, 1882
682 unsigned int index, const char *type, 1883static const struct snd_kcontrol_new cap_src_temp = {
683 const char *dir_sfx, int is_loopback) 1884 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1885 .name = "Input Source",
1886 .info = mux_enum_info,
1887 .get = mux_enum_get,
1888 .put = mux_enum_put,
1889};
1890
1891typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1892 struct snd_ctl_elem_value *ucontrol);
1893
1894static int cap_put_caller(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_value *ucontrol,
1896 put_call_t func, int type)
684{ 1897{
685 char name[32]; 1898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1899 struct hda_gen_spec *spec = codec->spec;
1900 const struct hda_input_mux *imux;
1901 struct nid_path *path;
1902 int i, adc_idx, err = 0;
1903
1904 imux = &spec->input_mux;
1905 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1906 mutex_lock(&codec->control_mutex);
1907 codec->cached_write = 1;
1908 for (i = 0; i < imux->num_items; i++) {
1909 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1910 get_adc_nid(codec, adc_idx, i));
1911 if (!path->ctls[type])
1912 continue;
1913 kcontrol->private_value = path->ctls[type];
1914 err = func(kcontrol, ucontrol);
1915 if (err < 0)
1916 goto error;
1917 }
1918 error:
1919 codec->cached_write = 0;
1920 mutex_unlock(&codec->control_mutex);
1921 if (err >= 0 && spec->cap_sync_hook)
1922 spec->cap_sync_hook(codec);
1923 return err;
1924}
1925
1926/* capture volume ctl callbacks */
1927#define cap_vol_info snd_hda_mixer_amp_volume_info
1928#define cap_vol_get snd_hda_mixer_amp_volume_get
1929#define cap_vol_tlv snd_hda_mixer_amp_tlv
1930
1931static int cap_vol_put(struct snd_kcontrol *kcontrol,
1932 struct snd_ctl_elem_value *ucontrol)
1933{
1934 return cap_put_caller(kcontrol, ucontrol,
1935 snd_hda_mixer_amp_volume_put,
1936 NID_PATH_VOL_CTL);
1937}
1938
1939static const struct snd_kcontrol_new cap_vol_temp = {
1940 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1941 .name = "Capture Volume",
1942 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1943 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1944 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
1945 .info = cap_vol_info,
1946 .get = cap_vol_get,
1947 .put = cap_vol_put,
1948 .tlv = { .c = cap_vol_tlv },
1949};
1950
1951/* capture switch ctl callbacks */
1952#define cap_sw_info snd_ctl_boolean_stereo_info
1953#define cap_sw_get snd_hda_mixer_amp_switch_get
1954
1955static int cap_sw_put(struct snd_kcontrol *kcontrol,
1956 struct snd_ctl_elem_value *ucontrol)
1957{
1958 return cap_put_caller(kcontrol, ucontrol,
1959 snd_hda_mixer_amp_switch_put,
1960 NID_PATH_MUTE_CTL);
1961}
1962
1963static const struct snd_kcontrol_new cap_sw_temp = {
1964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1965 .name = "Capture Switch",
1966 .info = cap_sw_info,
1967 .get = cap_sw_get,
1968 .put = cap_sw_put,
1969};
1970
1971static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
1972{
1973 hda_nid_t nid;
1974 int i, depth;
1975
1976 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
1977 for (depth = 0; depth < 3; depth++) {
1978 if (depth >= path->depth)
1979 return -EINVAL;
1980 i = path->depth - depth - 1;
1981 nid = path->path[i];
1982 if (!path->ctls[NID_PATH_VOL_CTL]) {
1983 if (nid_has_volume(codec, nid, HDA_OUTPUT))
1984 path->ctls[NID_PATH_VOL_CTL] =
1985 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1986 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
1987 int idx = path->idx[i];
1988 if (!depth && codec->single_adc_amp)
1989 idx = 0;
1990 path->ctls[NID_PATH_VOL_CTL] =
1991 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
1992 }
1993 }
1994 if (!path->ctls[NID_PATH_MUTE_CTL]) {
1995 if (nid_has_mute(codec, nid, HDA_OUTPUT))
1996 path->ctls[NID_PATH_MUTE_CTL] =
1997 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1998 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
1999 int idx = path->idx[i];
2000 if (!depth && codec->single_adc_amp)
2001 idx = 0;
2002 path->ctls[NID_PATH_MUTE_CTL] =
2003 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2004 }
2005 }
2006 }
2007 return 0;
2008}
2009
2010static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2011{
2012 struct hda_gen_spec *spec = codec->spec;
2013 struct auto_pin_cfg *cfg = &spec->autocfg;
2014 unsigned int val;
2015 int i;
2016
2017 if (!spec->inv_dmic_split)
2018 return false;
2019 for (i = 0; i < cfg->num_inputs; i++) {
2020 if (cfg->inputs[i].pin != nid)
2021 continue;
2022 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2023 return false;
2024 val = snd_hda_codec_get_pincfg(codec, nid);
2025 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2026 }
2027 return false;
2028}
2029
2030static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2031 int idx, bool is_switch, unsigned int ctl,
2032 bool inv_dmic)
2033{
2034 struct hda_gen_spec *spec = codec->spec;
2035 char tmpname[44];
2036 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2037 const char *sfx = is_switch ? "Switch" : "Volume";
2038 unsigned int chs = inv_dmic ? 1 : 3;
686 int err; 2039 int err;
687 int created = 0;
688 struct snd_kcontrol_new knew;
689 2040
690 if (type) 2041 if (!ctl)
691 sprintf(name, "%s %s Switch", type, dir_sfx); 2042 return 0;
2043
2044 if (label)
2045 snprintf(tmpname, sizeof(tmpname),
2046 "%s Capture %s", label, sfx);
692 else 2047 else
693 sprintf(name, "%s Switch", dir_sfx); 2048 snprintf(tmpname, sizeof(tmpname),
694 if ((node->wid_caps & AC_WCAP_IN_AMP) && 2049 "Capture %s", sfx);
695 (node->amp_in_caps & AC_AMPCAP_MUTE)) { 2050 err = add_control(spec, type, tmpname, idx,
696 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT); 2051 amp_val_replace_channels(ctl, chs));
697 if (is_loopback) 2052 if (err < 0 || !inv_dmic)
698 add_input_loopback(codec, node->nid, HDA_INPUT, index); 2053 return err;
699 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index); 2054
700 err = snd_hda_ctl_add(codec, node->nid, 2055 /* Make independent right kcontrol */
701 snd_ctl_new1(&knew, codec)); 2056 if (label)
702 if (err < 0) 2057 snprintf(tmpname, sizeof(tmpname),
703 return err; 2058 "Inverted %s Capture %s", label, sfx);
704 created = 1; 2059 else
705 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) && 2060 snprintf(tmpname, sizeof(tmpname),
706 (node->amp_out_caps & AC_AMPCAP_MUTE)) { 2061 "Inverted Capture %s", sfx);
707 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT); 2062 return add_control(spec, type, tmpname, idx,
708 if (is_loopback) 2063 amp_val_replace_channels(ctl, 2));
709 add_input_loopback(codec, node->nid, HDA_OUTPUT, 0); 2064}
710 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid); 2065
711 err = snd_hda_ctl_add(codec, node->nid, 2066/* create single (and simple) capture volume and switch controls */
712 snd_ctl_new1(&knew, codec)); 2067static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
713 if (err < 0) 2068 unsigned int vol_ctl, unsigned int sw_ctl,
714 return err; 2069 bool inv_dmic)
715 created = 1; 2070{
2071 int err;
2072 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2073 if (err < 0)
2074 return err;
2075 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2076 if (err < 0)
2077 return err;
2078 return 0;
2079}
2080
2081/* create bound capture volume and switch controls */
2082static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2083 unsigned int vol_ctl, unsigned int sw_ctl)
2084{
2085 struct hda_gen_spec *spec = codec->spec;
2086 struct snd_kcontrol_new *knew;
2087
2088 if (vol_ctl) {
2089 knew = add_kctl(spec, NULL, &cap_vol_temp);
2090 if (!knew)
2091 return -ENOMEM;
2092 knew->index = idx;
2093 knew->private_value = vol_ctl;
2094 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2095 }
2096 if (sw_ctl) {
2097 knew = add_kctl(spec, NULL, &cap_sw_temp);
2098 if (!knew)
2099 return -ENOMEM;
2100 knew->index = idx;
2101 knew->private_value = sw_ctl;
2102 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2103 }
2104 return 0;
2105}
2106
2107/* return the vol ctl when used first in the imux list */
2108static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2109{
2110 struct hda_gen_spec *spec = codec->spec;
2111 struct nid_path *path;
2112 unsigned int ctl;
2113 int i;
2114
2115 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2116 get_adc_nid(codec, 0, idx));
2117 if (!path)
2118 return 0;
2119 ctl = path->ctls[type];
2120 if (!ctl)
2121 return 0;
2122 for (i = 0; i < idx - 1; i++) {
2123 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2124 get_adc_nid(codec, 0, i));
2125 if (path && path->ctls[type] == ctl)
2126 return 0;
2127 }
2128 return ctl;
2129}
2130
2131/* create individual capture volume and switch controls per input */
2132static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2133{
2134 struct hda_gen_spec *spec = codec->spec;
2135 struct hda_input_mux *imux = &spec->input_mux;
2136 int i, err, type, type_idx = 0;
2137 const char *prev_label = NULL;
2138
2139 for (i = 0; i < imux->num_items; i++) {
2140 const char *label;
2141 bool inv_dmic;
2142 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2143 if (prev_label && !strcmp(label, prev_label))
2144 type_idx++;
2145 else
2146 type_idx = 0;
2147 prev_label = label;
2148 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2149
2150 for (type = 0; type < 2; type++) {
2151 err = add_single_cap_ctl(codec, label, type_idx, type,
2152 get_first_cap_ctl(codec, i, type),
2153 inv_dmic);
2154 if (err < 0)
2155 return err;
2156 }
716 } 2157 }
2158 return 0;
2159}
2160
2161static int create_capture_mixers(struct hda_codec *codec)
2162{
2163 struct hda_gen_spec *spec = codec->spec;
2164 struct hda_input_mux *imux = &spec->input_mux;
2165 int i, n, nums, err;
717 2166
718 if (type) 2167 if (spec->dyn_adc_switch)
719 sprintf(name, "%s %s Volume", type, dir_sfx); 2168 nums = 1;
720 else 2169 else
721 sprintf(name, "%s Volume", dir_sfx); 2170 nums = spec->num_adc_nids;
722 if ((node->wid_caps & AC_WCAP_IN_AMP) && 2171
723 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) { 2172 if (!spec->auto_mic && imux->num_items > 1) {
724 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT); 2173 struct snd_kcontrol_new *knew;
725 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index); 2174 knew = add_kctl(spec, NULL, &cap_src_temp);
726 err = snd_hda_ctl_add(codec, node->nid, 2175 if (!knew)
727 snd_ctl_new1(&knew, codec)); 2176 return -ENOMEM;
728 if (err < 0) 2177 knew->count = nums;
729 return err; 2178 }
730 created = 1; 2179
731 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) && 2180 for (n = 0; n < nums; n++) {
732 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) { 2181 bool multi = false;
733 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT); 2182 bool inv_dmic = false;
734 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid); 2183 int vol, sw;
735 err = snd_hda_ctl_add(codec, node->nid, 2184
736 snd_ctl_new1(&knew, codec)); 2185 vol = sw = 0;
2186 for (i = 0; i < imux->num_items; i++) {
2187 struct nid_path *path;
2188 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2189 get_adc_nid(codec, n, i));
2190 if (!path)
2191 continue;
2192 parse_capvol_in_path(codec, path);
2193 if (!vol)
2194 vol = path->ctls[NID_PATH_VOL_CTL];
2195 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2196 multi = true;
2197 if (!sw)
2198 sw = path->ctls[NID_PATH_MUTE_CTL];
2199 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2200 multi = true;
2201 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2202 inv_dmic = true;
2203 }
2204
2205 if (!multi)
2206 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2207 inv_dmic);
2208 else if (!spec->multi_cap_vol)
2209 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2210 else
2211 err = create_multi_cap_vol_ctl(codec);
737 if (err < 0) 2212 if (err < 0)
738 return err; 2213 return err;
739 created = 1;
740 } 2214 }
741 2215
742 return created; 2216 return 0;
743} 2217}
744 2218
745/* 2219/*
746 * check whether the controls with the given name and direction suffix already exist 2220 * add mic boosts if needed
747 */ 2221 */
748static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir) 2222static int parse_mic_boost(struct hda_codec *codec)
749{ 2223{
750 struct snd_ctl_elem_id id; 2224 struct hda_gen_spec *spec = codec->spec;
751 memset(&id, 0, sizeof(id)); 2225 struct auto_pin_cfg *cfg = &spec->autocfg;
752 sprintf(id.name, "%s %s Volume", type, dir); 2226 int i, err;
753 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 2227 int type_idx = 0;
754 if (snd_ctl_find_id(codec->bus->card, &id)) 2228 hda_nid_t nid;
755 return 1; 2229 const char *prev_label = NULL;
756 sprintf(id.name, "%s %s Switch", type, dir); 2230
757 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 2231 for (i = 0; i < cfg->num_inputs; i++) {
758 if (snd_ctl_find_id(codec->bus->card, &id)) 2232 if (cfg->inputs[i].type > AUTO_PIN_MIC)
759 return 1; 2233 break;
2234 nid = cfg->inputs[i].pin;
2235 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2236 const char *label;
2237 char boost_label[32];
2238 struct nid_path *path;
2239 unsigned int val;
2240
2241 label = hda_get_autocfg_input_label(codec, cfg, i);
2242 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2243 label = "Headphone Mic";
2244 if (prev_label && !strcmp(label, prev_label))
2245 type_idx++;
2246 else
2247 type_idx = 0;
2248 prev_label = label;
2249
2250 snprintf(boost_label, sizeof(boost_label),
2251 "%s Boost Volume", label);
2252 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2253 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2254 boost_label, type_idx, val);
2255 if (err < 0)
2256 return err;
2257
2258 path = snd_hda_get_nid_path(codec, nid, 0);
2259 if (path)
2260 path->ctls[NID_PATH_BOOST_CTL] = val;
2261 }
2262 }
760 return 0; 2263 return 0;
761} 2264}
762 2265
763/* 2266/*
764 * build output mixer controls 2267 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
765 */ 2268 */
766static int create_output_mixers(struct hda_codec *codec, 2269static void parse_digital(struct hda_codec *codec)
767 const char * const *names)
768{ 2270{
769 struct hda_gspec *spec = codec->spec; 2271 struct hda_gen_spec *spec = codec->spec;
770 int i, err; 2272 int i, nums;
2273 hda_nid_t dig_nid;
2274
2275 /* support multiple SPDIFs; the secondary is set up as a slave */
2276 nums = 0;
2277 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2278 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2279 dig_nid = look_for_dac(codec, pin, true);
2280 if (!dig_nid)
2281 continue;
2282 if (!snd_hda_add_new_path(codec, dig_nid, pin, 2))
2283 continue;
2284 if (!nums) {
2285 spec->multiout.dig_out_nid = dig_nid;
2286 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2287 } else {
2288 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2289 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2290 break;
2291 spec->slave_dig_outs[nums - 1] = dig_nid;
2292 }
2293 nums++;
2294 }
771 2295
772 for (i = 0; i < spec->pcm_vol_nodes; i++) { 2296 if (spec->autocfg.dig_in_pin) {
773 err = create_mixer(codec, spec->pcm_vol[i].node, 2297 dig_nid = codec->start_nid;
774 spec->pcm_vol[i].index, 2298 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
775 names[i], "Playback", 0); 2299 struct nid_path *path;
776 if (err < 0) 2300 unsigned int wcaps = get_wcaps(codec, dig_nid);
777 return err; 2301 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2302 continue;
2303 if (!(wcaps & AC_WCAP_DIGITAL))
2304 continue;
2305 path = snd_hda_add_new_path(codec,
2306 spec->autocfg.dig_in_pin,
2307 dig_nid, 2);
2308 if (path) {
2309 path->active = true;
2310 spec->dig_in_nid = dig_nid;
2311 break;
2312 }
2313 }
2314 }
2315}
2316
2317
2318/*
2319 * input MUX handling
2320 */
2321
2322static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2323
2324/* select the given imux item; either unmute exclusively or select the route */
2325static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2326 unsigned int idx)
2327{
2328 struct hda_gen_spec *spec = codec->spec;
2329 const struct hda_input_mux *imux;
2330 struct nid_path *path;
2331
2332 imux = &spec->input_mux;
2333 if (!imux->num_items)
2334 return 0;
2335
2336 if (idx >= imux->num_items)
2337 idx = imux->num_items - 1;
2338 if (spec->cur_mux[adc_idx] == idx)
2339 return 0;
2340
2341 path = snd_hda_get_nid_path(codec,
2342 spec->imux_pins[spec->cur_mux[adc_idx]],
2343 spec->adc_nids[adc_idx]);
2344 if (!path)
2345 return 0;
2346 if (path->active)
2347 snd_hda_activate_path(codec, path, false, false);
2348
2349 spec->cur_mux[adc_idx] = idx;
2350
2351 if (spec->shared_mic_hp)
2352 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2353
2354 if (spec->dyn_adc_switch)
2355 dyn_adc_pcm_resetup(codec, idx);
2356
2357 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2358 get_adc_nid(codec, adc_idx, idx));
2359 if (!path)
2360 return 0;
2361 if (path->active)
2362 return 0;
2363 snd_hda_activate_path(codec, path, true, false);
2364 if (spec->cap_sync_hook)
2365 spec->cap_sync_hook(codec);
2366 return 1;
2367}
2368
2369
2370/*
2371 * Jack detections for HP auto-mute and mic-switch
2372 */
2373
2374/* check each pin in the given array; returns true if any of them is plugged */
2375static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2376{
2377 int i, present = 0;
2378
2379 for (i = 0; i < num_pins; i++) {
2380 hda_nid_t nid = pins[i];
2381 if (!nid)
2382 break;
2383 present |= snd_hda_jack_detect(codec, nid);
2384 }
2385 return present;
2386}
2387
2388/* standard HP/line-out auto-mute helper */
2389static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2390 bool mute, bool hp_out)
2391{
2392 struct hda_gen_spec *spec = codec->spec;
2393 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2394 int i;
2395
2396 for (i = 0; i < num_pins; i++) {
2397 hda_nid_t nid = pins[i];
2398 unsigned int val;
2399 if (!nid)
2400 break;
2401 /* don't reset VREF value in case it's controlling
2402 * the amp (see alc861_fixup_asus_amp_vref_0f())
2403 */
2404 if (spec->keep_vref_in_automute) {
2405 val = snd_hda_codec_read(codec, nid, 0,
2406 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2407 val &= ~PIN_HP;
2408 } else
2409 val = 0;
2410 val |= pin_bits;
2411 snd_hda_set_pin_ctl(codec, nid, val);
2412 }
2413}
2414
2415/* Toggle outputs muting */
2416static void update_outputs(struct hda_codec *codec)
2417{
2418 struct hda_gen_spec *spec = codec->spec;
2419 int on;
2420
2421 /* Control HP pins/amps depending on master_mute state;
2422 * in general, HP pins/amps control should be enabled in all cases,
2423 * but currently set only for master_mute, just to be safe
2424 */
2425 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2426 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2427 spec->autocfg.hp_pins, spec->master_mute, true);
2428
2429 if (!spec->automute_speaker)
2430 on = 0;
2431 else
2432 on = spec->hp_jack_present | spec->line_jack_present;
2433 on |= spec->master_mute;
2434 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2435 spec->autocfg.speaker_pins, on, false);
2436
2437 /* toggle line-out mutes if needed, too */
2438 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2439 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2440 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2441 return;
2442 if (!spec->automute_lo)
2443 on = 0;
2444 else
2445 on = spec->hp_jack_present;
2446 on |= spec->master_mute;
2447 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2448 spec->autocfg.line_out_pins, on, false);
2449}
2450
2451static void call_update_outputs(struct hda_codec *codec)
2452{
2453 struct hda_gen_spec *spec = codec->spec;
2454 if (spec->automute_hook)
2455 spec->automute_hook(codec);
2456 else
2457 update_outputs(codec);
2458}
2459
2460/* standard HP-automute helper */
2461static void hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2462{
2463 struct hda_gen_spec *spec = codec->spec;
2464
2465 spec->hp_jack_present =
2466 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2467 spec->autocfg.hp_pins);
2468 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2469 return;
2470 call_update_outputs(codec);
2471}
2472
2473/* standard line-out-automute helper */
2474static void line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2475{
2476 struct hda_gen_spec *spec = codec->spec;
2477
2478 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2479 return;
2480 /* check LO jack only when it's different from HP */
2481 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2482 return;
2483
2484 spec->line_jack_present =
2485 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2486 spec->autocfg.line_out_pins);
2487 if (!spec->automute_speaker || !spec->detect_lo)
2488 return;
2489 call_update_outputs(codec);
2490}
2491
2492/* standard mic auto-switch helper */
2493static void mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2494{
2495 struct hda_gen_spec *spec = codec->spec;
2496 int i;
2497
2498 if (!spec->auto_mic)
2499 return;
2500
2501 for (i = spec->am_num_entries - 1; i > 0; i--) {
2502 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2503 mux_select(codec, 0, spec->am_entry[i].idx);
2504 return;
2505 }
778 } 2506 }
2507 mux_select(codec, 0, spec->am_entry[0].idx);
2508}
2509
2510/*
2511 * Auto-Mute mode mixer enum support
2512 */
2513static int automute_mode_info(struct snd_kcontrol *kcontrol,
2514 struct snd_ctl_elem_info *uinfo)
2515{
2516 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2517 struct hda_gen_spec *spec = codec->spec;
2518 static const char * const texts3[] = {
2519 "Disabled", "Speaker Only", "Line Out+Speaker"
2520 };
2521
2522 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2523 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2524 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2525}
2526
2527static int automute_mode_get(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_value *ucontrol)
2529{
2530 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2531 struct hda_gen_spec *spec = codec->spec;
2532 unsigned int val = 0;
2533 if (spec->automute_speaker)
2534 val++;
2535 if (spec->automute_lo)
2536 val++;
2537
2538 ucontrol->value.enumerated.item[0] = val;
779 return 0; 2539 return 0;
780} 2540}
781 2541
782static int build_output_controls(struct hda_codec *codec) 2542static int automute_mode_put(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_value *ucontrol)
783{ 2544{
784 struct hda_gspec *spec = codec->spec; 2545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
785 static const char * const types_speaker[] = { "Speaker", "Headphone" }; 2546 struct hda_gen_spec *spec = codec->spec;
786 static const char * const types_line[] = { "Front", "Headphone" };
787 2547
788 switch (spec->pcm_vol_nodes) { 2548 switch (ucontrol->value.enumerated.item[0]) {
2549 case 0:
2550 if (!spec->automute_speaker && !spec->automute_lo)
2551 return 0;
2552 spec->automute_speaker = 0;
2553 spec->automute_lo = 0;
2554 break;
789 case 1: 2555 case 1:
790 return create_mixer(codec, spec->pcm_vol[0].node, 2556 if (spec->automute_speaker_possible) {
791 spec->pcm_vol[0].index, 2557 if (!spec->automute_lo && spec->automute_speaker)
792 "Master", "Playback", 0); 2558 return 0;
2559 spec->automute_speaker = 1;
2560 spec->automute_lo = 0;
2561 } else if (spec->automute_lo_possible) {
2562 if (spec->automute_lo)
2563 return 0;
2564 spec->automute_lo = 1;
2565 } else
2566 return -EINVAL;
2567 break;
793 case 2: 2568 case 2:
794 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER) 2569 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
795 return create_output_mixers(codec, types_speaker); 2570 return -EINVAL;
796 else 2571 if (spec->automute_speaker && spec->automute_lo)
797 return create_output_mixers(codec, types_line); 2572 return 0;
2573 spec->automute_speaker = 1;
2574 spec->automute_lo = 1;
2575 break;
2576 default:
2577 return -EINVAL;
798 } 2578 }
2579 call_update_outputs(codec);
2580 return 1;
2581}
2582
2583static const struct snd_kcontrol_new automute_mode_enum = {
2584 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2585 .name = "Auto-Mute Mode",
2586 .info = automute_mode_info,
2587 .get = automute_mode_get,
2588 .put = automute_mode_put,
2589};
2590
2591static int add_automute_mode_enum(struct hda_codec *codec)
2592{
2593 struct hda_gen_spec *spec = codec->spec;
2594
2595 if (!add_kctl(spec, NULL, &automute_mode_enum))
2596 return -ENOMEM;
799 return 0; 2597 return 0;
800} 2598}
801 2599
802/* create capture volume/switch */ 2600/*
803static int build_input_controls(struct hda_codec *codec) 2601 * Check the availability of HP/line-out auto-mute;
2602 * Set up appropriately if really supported
2603 */
2604static int check_auto_mute_availability(struct hda_codec *codec)
804{ 2605{
805 struct hda_gspec *spec = codec->spec; 2606 struct hda_gen_spec *spec = codec->spec;
806 struct hda_gnode *adc_node = spec->adc_node; 2607 struct auto_pin_cfg *cfg = &spec->autocfg;
2608 int present = 0;
807 int i, err; 2609 int i, err;
808 static struct snd_kcontrol_new cap_sel = {
809 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
810 .name = "Capture Source",
811 .info = capture_source_info,
812 .get = capture_source_get,
813 .put = capture_source_put,
814 };
815 2610
816 if (! adc_node || ! spec->input_mux.num_items) 2611 if (cfg->hp_pins[0])
817 return 0; /* not found */ 2612 present++;
2613 if (cfg->line_out_pins[0])
2614 present++;
2615 if (cfg->speaker_pins[0])
2616 present++;
2617 if (present < 2) /* need two different output types */
2618 return 0;
818 2619
819 spec->cur_cap_src = 0; 2620 if (!cfg->speaker_pins[0] &&
820 select_input_connection(codec, adc_node, 2621 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
821 spec->input_mux.items[0].index); 2622 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2623 sizeof(cfg->speaker_pins));
2624 cfg->speaker_outs = cfg->line_outs;
2625 }
822 2626
823 /* create capture volume and switch controls if the ADC has an amp */ 2627 if (!cfg->hp_pins[0] &&
824 /* do we have only a single item? */ 2628 cfg->line_out_type == AUTO_PIN_HP_OUT) {
825 if (spec->input_mux.num_items == 1) { 2629 memcpy(cfg->hp_pins, cfg->line_out_pins,
826 err = create_mixer(codec, adc_node, 2630 sizeof(cfg->hp_pins));
827 spec->input_mux.items[0].index, 2631 cfg->hp_outs = cfg->line_outs;
828 NULL, "Capture", 0);
829 if (err < 0)
830 return err;
831 return 0;
832 } 2632 }
833 2633
834 /* create input MUX if multiple sources are available */ 2634 for (i = 0; i < cfg->hp_outs; i++) {
835 err = snd_hda_ctl_add(codec, spec->adc_node->nid, 2635 hda_nid_t nid = cfg->hp_pins[i];
836 snd_ctl_new1(&cap_sel, codec)); 2636 if (!is_jack_detectable(codec, nid))
837 if (err < 0) 2637 continue;
838 return err; 2638 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2639 nid);
2640 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2641 hp_automute);
2642 spec->detect_hp = 1;
2643 }
839 2644
840 /* no volume control? */ 2645 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
841 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) || 2646 if (cfg->speaker_outs)
842 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) 2647 for (i = 0; i < cfg->line_outs; i++) {
843 return 0; 2648 hda_nid_t nid = cfg->line_out_pins[i];
2649 if (!is_jack_detectable(codec, nid))
2650 continue;
2651 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2652 snd_hda_jack_detect_enable_callback(codec, nid,
2653 HDA_GEN_FRONT_EVENT,
2654 line_automute);
2655 spec->detect_lo = 1;
2656 }
2657 spec->automute_lo_possible = spec->detect_hp;
2658 }
2659
2660 spec->automute_speaker_possible = cfg->speaker_outs &&
2661 (spec->detect_hp || spec->detect_lo);
2662
2663 spec->automute_lo = spec->automute_lo_possible;
2664 spec->automute_speaker = spec->automute_speaker_possible;
844 2665
845 for (i = 0; i < spec->input_mux.num_items; i++) { 2666 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
846 struct snd_kcontrol_new knew; 2667 /* create a control for automute mode */
847 char name[32]; 2668 err = add_automute_mode_enum(codec);
848 sprintf(name, "%s Capture Volume",
849 spec->input_mux.items[i].label);
850 knew = (struct snd_kcontrol_new)
851 HDA_CODEC_VOLUME(name, adc_node->nid,
852 spec->input_mux.items[i].index,
853 HDA_INPUT);
854 err = snd_hda_ctl_add(codec, adc_node->nid,
855 snd_ctl_new1(&knew, codec));
856 if (err < 0) 2669 if (err < 0)
857 return err; 2670 return err;
858 } 2671 }
859
860 return 0; 2672 return 0;
861} 2673}
862 2674
2675/* return the position of NID in the list, or -1 if not found */
2676static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2677{
2678 int i;
2679 for (i = 0; i < nums; i++)
2680 if (list[i] == nid)
2681 return i;
2682 return -1;
2683}
2684
2685/* check whether all auto-mic pins are valid; setup indices if OK */
2686static bool auto_mic_check_imux(struct hda_codec *codec)
2687{
2688 struct hda_gen_spec *spec = codec->spec;
2689 const struct hda_input_mux *imux;
2690 int i;
2691
2692 imux = &spec->input_mux;
2693 for (i = 0; i < spec->am_num_entries; i++) {
2694 spec->am_entry[i].idx =
2695 find_idx_in_nid_list(spec->am_entry[i].pin,
2696 spec->imux_pins, imux->num_items);
2697 if (spec->am_entry[i].idx < 0)
2698 return false; /* no corresponding imux */
2699 }
2700
2701 /* we don't need the jack detection for the first pin */
2702 for (i = 1; i < spec->am_num_entries; i++)
2703 snd_hda_jack_detect_enable_callback(codec,
2704 spec->am_entry[i].pin,
2705 HDA_GEN_MIC_EVENT,
2706 mic_autoswitch);
2707 return true;
2708}
2709
2710static int compare_attr(const void *ap, const void *bp)
2711{
2712 const struct automic_entry *a = ap;
2713 const struct automic_entry *b = bp;
2714 return (int)(a->attr - b->attr);
2715}
863 2716
864/* 2717/*
865 * parse the nodes recursively until reach to the output PIN. 2718 * Check the availability of auto-mic switch;
866 * 2719 * Set up if really supported
867 * returns 0 - if not found,
868 * 1 - if found, but no mixer is created
869 * 2 - if found and mixer was already created, (just skip)
870 * a negative error code
871 */ 2720 */
872static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec, 2721static int check_auto_mic_availability(struct hda_codec *codec)
873 struct hda_gnode *node, struct hda_gnode *dest_node,
874 const char *type)
875{ 2722{
876 int i, err; 2723 struct hda_gen_spec *spec = codec->spec;
2724 struct auto_pin_cfg *cfg = &spec->autocfg;
2725 unsigned int types;
2726 int i, num_pins;
2727
2728 types = 0;
2729 num_pins = 0;
2730 for (i = 0; i < cfg->num_inputs; i++) {
2731 hda_nid_t nid = cfg->inputs[i].pin;
2732 unsigned int attr;
2733 attr = snd_hda_codec_get_pincfg(codec, nid);
2734 attr = snd_hda_get_input_pin_attr(attr);
2735 if (types & (1 << attr))
2736 return 0; /* already occupied */
2737 switch (attr) {
2738 case INPUT_PIN_ATTR_INT:
2739 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2740 return 0; /* invalid type */
2741 break;
2742 case INPUT_PIN_ATTR_UNUSED:
2743 return 0; /* invalid entry */
2744 default:
2745 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2746 return 0; /* invalid type */
2747 if (!spec->line_in_auto_switch &&
2748 cfg->inputs[i].type != AUTO_PIN_MIC)
2749 return 0; /* only mic is allowed */
2750 if (!is_jack_detectable(codec, nid))
2751 return 0; /* no unsol support */
2752 break;
2753 }
2754 if (num_pins >= MAX_AUTO_MIC_PINS)
2755 return 0;
2756 types |= (1 << attr);
2757 spec->am_entry[num_pins].pin = nid;
2758 spec->am_entry[num_pins].attr = attr;
2759 num_pins++;
2760 }
877 2761
878 if (node->checked) 2762 if (num_pins < 2)
879 return 0; 2763 return 0;
880 2764
881 node->checked = 1; 2765 spec->am_num_entries = num_pins;
882 if (node == dest_node) { 2766 /* sort the am_entry in the order of attr so that the pin with a
883 /* loopback connection found */ 2767 * higher attr will be selected when the jack is plugged.
884 return 1; 2768 */
2769 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2770 compare_attr, NULL);
2771
2772 if (!auto_mic_check_imux(codec))
2773 return 0;
2774
2775 spec->auto_mic = 1;
2776 spec->num_adc_nids = 1;
2777 spec->cur_mux[0] = spec->am_entry[0].idx;
2778 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2779 spec->am_entry[0].pin,
2780 spec->am_entry[1].pin,
2781 spec->am_entry[2].pin);
2782
2783 return 0;
2784}
2785
2786
2787/* parse the BIOS configuration and set up the hda_gen_spec */
2788/* return 1 if successful, 0 if the proper config is not found,
2789 * or a negative error code
2790 */
2791int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
2792 const hda_nid_t *ignore_nids)
2793{
2794 struct hda_gen_spec *spec = codec->spec;
2795 struct auto_pin_cfg *cfg = &spec->autocfg;
2796 int err;
2797
2798 err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
2799 spec->parse_flags);
2800 if (err < 0)
2801 return err;
2802 if (!cfg->line_outs) {
2803 if (cfg->dig_outs || cfg->dig_in_pin) {
2804 spec->multiout.max_channels = 2;
2805 spec->no_analog = 1;
2806 goto dig_only;
2807 }
2808 return 0; /* can't find valid BIOS pin config */
885 } 2809 }
886 2810
887 for (i = 0; i < node->nconns; i++) { 2811 if (!spec->no_primary_hp &&
888 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]); 2812 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
889 if (! child) 2813 cfg->line_outs <= cfg->hp_outs) {
890 continue; 2814 /* use HP as primary out */
891 err = parse_loopback_path(codec, spec, child, dest_node, type); 2815 cfg->speaker_outs = cfg->line_outs;
2816 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2817 sizeof(cfg->speaker_pins));
2818 cfg->line_outs = cfg->hp_outs;
2819 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2820 cfg->hp_outs = 0;
2821 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2822 cfg->line_out_type = AUTO_PIN_HP_OUT;
2823 }
2824
2825 err = parse_output_paths(codec);
2826 if (err < 0)
2827 return err;
2828 err = create_multi_channel_mode(codec);
2829 if (err < 0)
2830 return err;
2831 err = create_multi_out_ctls(codec, cfg);
2832 if (err < 0)
2833 return err;
2834 err = create_hp_out_ctls(codec);
2835 if (err < 0)
2836 return err;
2837 err = create_speaker_out_ctls(codec);
2838 if (err < 0)
2839 return err;
2840 err = create_shared_input(codec);
2841 if (err < 0)
2842 return err;
2843 err = create_input_ctls(codec);
2844 if (err < 0)
2845 return err;
2846
2847 /* check the multiple speaker pins */
2848 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2849 spec->const_channel_count = cfg->line_outs * 2;
2850 else
2851 spec->const_channel_count = cfg->speaker_outs * 2;
2852
2853 if (spec->multi_ios > 0)
2854 spec->multiout.max_channels = max(spec->ext_channel_count,
2855 spec->const_channel_count);
2856 else
2857 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2858
2859 err = check_auto_mute_availability(codec);
2860 if (err < 0)
2861 return err;
2862
2863 err = check_dyn_adc_switch(codec);
2864 if (err < 0)
2865 return err;
2866
2867 if (!spec->shared_mic_hp) {
2868 err = check_auto_mic_availability(codec);
892 if (err < 0) 2869 if (err < 0)
893 return err; 2870 return err;
894 else if (err >= 1) {
895 if (err == 1) {
896 err = create_mixer(codec, node, i, type,
897 "Playback", 1);
898 if (err < 0)
899 return err;
900 if (err > 0)
901 return 2; /* ok, created */
902 /* not created, maybe in the lower path */
903 err = 1;
904 }
905 /* connect and unmute */
906 if (node->nconns > 1)
907 select_input_connection(codec, node, i);
908 unmute_input(codec, node, i);
909 unmute_output(codec, node);
910 return err;
911 }
912 } 2871 }
913 return 0; 2872
2873 err = create_capture_mixers(codec);
2874 if (err < 0)
2875 return err;
2876
2877 err = parse_mic_boost(codec);
2878 if (err < 0)
2879 return err;
2880
2881 dig_only:
2882 parse_digital(codec);
2883
2884 return 1;
914} 2885}
2886EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
2887
915 2888
916/* 2889/*
917 * parse the tree and build the loopback controls 2890 * Build control elements
918 */ 2891 */
919static int build_loopback_controls(struct hda_codec *codec) 2892
2893/* slave controls for virtual master */
2894static const char * const slave_pfxs[] = {
2895 "Front", "Surround", "Center", "LFE", "Side",
2896 "Headphone", "Speaker", "Mono", "Line Out",
2897 "CLFE", "Bass Speaker", "PCM",
2898 NULL,
2899};
2900
2901int snd_hda_gen_build_controls(struct hda_codec *codec)
920{ 2902{
921 struct hda_gspec *spec = codec->spec; 2903 struct hda_gen_spec *spec = codec->spec;
922 struct hda_gnode *node;
923 int err; 2904 int err;
924 const char *type;
925 2905
926 if (! spec->out_pin_node[0]) 2906 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
927 return 0; 2907 if (err < 0)
2908 return err;
928 2909
929 list_for_each_entry(node, &spec->nid_list, list) { 2910 if (spec->multiout.dig_out_nid) {
930 if (node->type != AC_WID_PIN) 2911 err = snd_hda_create_dig_out_ctls(codec,
931 continue; 2912 spec->multiout.dig_out_nid,
932 /* input capable? */ 2913 spec->multiout.dig_out_nid,
933 if (! (node->pin_caps & AC_PINCAP_IN)) 2914 spec->pcm_rec[1].pcm_type);
934 return 0; 2915 if (err < 0)
935 type = get_input_type(node, NULL); 2916 return err;
936 if (type) { 2917 if (!spec->no_analog) {
937 if (check_existing_control(codec, type, "Playback")) 2918 err = snd_hda_create_spdif_share_sw(codec,
938 continue; 2919 &spec->multiout);
939 clear_check_flags(spec);
940 err = parse_loopback_path(codec, spec,
941 spec->out_pin_node[0],
942 node, type);
943 if (err < 0) 2920 if (err < 0)
944 return err; 2921 return err;
945 if (! err) 2922 spec->multiout.share_spdif = 1;
946 continue;
947 } 2923 }
948 } 2924 }
2925 if (spec->dig_in_nid) {
2926 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2927 if (err < 0)
2928 return err;
2929 }
2930
2931 /* if we have no master control, let's create it */
2932 if (!spec->no_analog &&
2933 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
2934 unsigned int vmaster_tlv[4];
2935 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2936 HDA_OUTPUT, vmaster_tlv);
2937 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
2938 vmaster_tlv, slave_pfxs,
2939 "Playback Volume");
2940 if (err < 0)
2941 return err;
2942 }
2943 if (!spec->no_analog &&
2944 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
2945 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
2946 NULL, slave_pfxs,
2947 "Playback Switch",
2948 true, &spec->vmaster_mute.sw_kctl);
2949 if (err < 0)
2950 return err;
2951 if (spec->vmaster_mute.hook)
2952 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute, true);
2953 }
2954
2955 free_kctls(spec); /* no longer needed */
2956
2957 if (spec->shared_mic_hp) {
2958 int err;
2959 int nid = spec->autocfg.inputs[1].pin;
2960 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
2961 if (err < 0)
2962 return err;
2963 err = snd_hda_jack_detect_enable(codec, nid, 0);
2964 if (err < 0)
2965 return err;
2966 }
2967
2968 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
2969 if (err < 0)
2970 return err;
2971
949 return 0; 2972 return 0;
950} 2973}
2974EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
2975
2976
2977/*
2978 * PCM definitions
2979 */
951 2980
952/* 2981/*
953 * build mixer controls 2982 * Analog playback callbacks
954 */ 2983 */
955static int build_generic_controls(struct hda_codec *codec) 2984static int playback_pcm_open(struct hda_pcm_stream *hinfo,
2985 struct hda_codec *codec,
2986 struct snd_pcm_substream *substream)
956{ 2987{
957 int err; 2988 struct hda_gen_spec *spec = codec->spec;
2989 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2990 hinfo);
2991}
958 2992
959 if ((err = build_input_controls(codec)) < 0 || 2993static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
960 (err = build_output_controls(codec)) < 0 || 2994 struct hda_codec *codec,
961 (err = build_loopback_controls(codec)) < 0) 2995 unsigned int stream_tag,
962 return err; 2996 unsigned int format,
2997 struct snd_pcm_substream *substream)
2998{
2999 struct hda_gen_spec *spec = codec->spec;
3000 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3001 stream_tag, format, substream);
3002}
3003
3004static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3005 struct hda_codec *codec,
3006 struct snd_pcm_substream *substream)
3007{
3008 struct hda_gen_spec *spec = codec->spec;
3009 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3010}
3011
3012/*
3013 * Digital out
3014 */
3015static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3016 struct hda_codec *codec,
3017 struct snd_pcm_substream *substream)
3018{
3019 struct hda_gen_spec *spec = codec->spec;
3020 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3021}
3022
3023static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3024 struct hda_codec *codec,
3025 unsigned int stream_tag,
3026 unsigned int format,
3027 struct snd_pcm_substream *substream)
3028{
3029 struct hda_gen_spec *spec = codec->spec;
3030 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3031 stream_tag, format, substream);
3032}
3033
3034static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3035 struct hda_codec *codec,
3036 struct snd_pcm_substream *substream)
3037{
3038 struct hda_gen_spec *spec = codec->spec;
3039 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3040}
3041
3042static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3043 struct hda_codec *codec,
3044 struct snd_pcm_substream *substream)
3045{
3046 struct hda_gen_spec *spec = codec->spec;
3047 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3048}
3049
3050/*
3051 * Analog capture
3052 */
3053static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3054 struct hda_codec *codec,
3055 unsigned int stream_tag,
3056 unsigned int format,
3057 struct snd_pcm_substream *substream)
3058{
3059 struct hda_gen_spec *spec = codec->spec;
3060
3061 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3062 stream_tag, 0, format);
3063 return 0;
3064}
3065
3066static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3067 struct hda_codec *codec,
3068 struct snd_pcm_substream *substream)
3069{
3070 struct hda_gen_spec *spec = codec->spec;
963 3071
3072 snd_hda_codec_cleanup_stream(codec,
3073 spec->adc_nids[substream->number + 1]);
964 return 0; 3074 return 0;
965} 3075}
966 3076
967/* 3077/*
968 * PCM
969 */ 3078 */
970static struct hda_pcm_stream generic_pcm_playback = { 3079static const struct hda_pcm_stream pcm_analog_playback = {
3080 .substreams = 1,
3081 .channels_min = 2,
3082 .channels_max = 8,
3083 /* NID is set in build_pcms */
3084 .ops = {
3085 .open = playback_pcm_open,
3086 .prepare = playback_pcm_prepare,
3087 .cleanup = playback_pcm_cleanup
3088 },
3089};
3090
3091static const struct hda_pcm_stream pcm_analog_capture = {
971 .substreams = 1, 3092 .substreams = 1,
972 .channels_min = 2, 3093 .channels_min = 2,
973 .channels_max = 2, 3094 .channels_max = 2,
3095 /* NID is set in build_pcms */
974}; 3096};
975 3097
976static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo, 3098static const struct hda_pcm_stream pcm_analog_alt_playback = {
977 struct hda_codec *codec, 3099 .substreams = 1,
978 unsigned int stream_tag, 3100 .channels_min = 2,
979 unsigned int format, 3101 .channels_max = 2,
980 struct snd_pcm_substream *substream) 3102 /* NID is set in build_pcms */
3103};
3104
3105static const struct hda_pcm_stream pcm_analog_alt_capture = {
3106 .substreams = 2, /* can be overridden */
3107 .channels_min = 2,
3108 .channels_max = 2,
3109 /* NID is set in build_pcms */
3110 .ops = {
3111 .prepare = alt_capture_pcm_prepare,
3112 .cleanup = alt_capture_pcm_cleanup
3113 },
3114};
3115
3116static const struct hda_pcm_stream pcm_digital_playback = {
3117 .substreams = 1,
3118 .channels_min = 2,
3119 .channels_max = 2,
3120 /* NID is set in build_pcms */
3121 .ops = {
3122 .open = dig_playback_pcm_open,
3123 .close = dig_playback_pcm_close,
3124 .prepare = dig_playback_pcm_prepare,
3125 .cleanup = dig_playback_pcm_cleanup
3126 },
3127};
3128
3129static const struct hda_pcm_stream pcm_digital_capture = {
3130 .substreams = 1,
3131 .channels_min = 2,
3132 .channels_max = 2,
3133 /* NID is set in build_pcms */
3134};
3135
3136/* Used by build_pcms to flag that a PCM has no playback stream */
3137static const struct hda_pcm_stream pcm_null_stream = {
3138 .substreams = 0,
3139 .channels_min = 0,
3140 .channels_max = 0,
3141};
3142
3143/*
3144 * dynamic changing ADC PCM streams
3145 */
3146static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
981{ 3147{
982 struct hda_gspec *spec = codec->spec; 3148 struct hda_gen_spec *spec = codec->spec;
3149 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3150
3151 if (spec->cur_adc && spec->cur_adc != new_adc) {
3152 /* stream is running, let's swap the current ADC */
3153 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3154 spec->cur_adc = new_adc;
3155 snd_hda_codec_setup_stream(codec, new_adc,
3156 spec->cur_adc_stream_tag, 0,
3157 spec->cur_adc_format);
3158 return true;
3159 }
3160 return false;
3161}
983 3162
984 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); 3163/* analog capture with dynamic dual-adc changes */
985 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 3164static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
986 stream_tag, 0, format); 3165 struct hda_codec *codec,
3166 unsigned int stream_tag,
3167 unsigned int format,
3168 struct snd_pcm_substream *substream)
3169{
3170 struct hda_gen_spec *spec = codec->spec;
3171 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3172 spec->cur_adc_stream_tag = stream_tag;
3173 spec->cur_adc_format = format;
3174 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
987 return 0; 3175 return 0;
988} 3176}
989 3177
990static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo, 3178static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
991 struct hda_codec *codec, 3179 struct hda_codec *codec,
992 struct snd_pcm_substream *substream) 3180 struct snd_pcm_substream *substream)
993{ 3181{
994 struct hda_gspec *spec = codec->spec; 3182 struct hda_gen_spec *spec = codec->spec;
995 3183 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
996 snd_hda_codec_cleanup_stream(codec, hinfo->nid); 3184 spec->cur_adc = 0;
997 snd_hda_codec_cleanup_stream(codec, spec->dac_node[1]->nid);
998 return 0; 3185 return 0;
999} 3186}
1000 3187
1001static int build_generic_pcms(struct hda_codec *codec) 3188static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3189 .substreams = 1,
3190 .channels_min = 2,
3191 .channels_max = 2,
3192 .nid = 0, /* fill later */
3193 .ops = {
3194 .prepare = dyn_adc_capture_pcm_prepare,
3195 .cleanup = dyn_adc_capture_pcm_cleanup
3196 },
3197};
3198
3199/* build PCM streams based on the parsed results */
3200int snd_hda_gen_build_pcms(struct hda_codec *codec)
1002{ 3201{
1003 struct hda_gspec *spec = codec->spec; 3202 struct hda_gen_spec *spec = codec->spec;
1004 struct hda_pcm *info = &spec->pcm_rec; 3203 struct hda_pcm *info = spec->pcm_rec;
3204 const struct hda_pcm_stream *p;
3205 bool have_multi_adcs;
3206 int i;
3207
3208 codec->num_pcms = 1;
3209 codec->pcm_info = info;
3210
3211 if (spec->no_analog)
3212 goto skip_analog;
3213
3214 snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
3215 "%s Analog", codec->chip_name);
3216 info->name = spec->stream_name_analog;
3217
3218 if (spec->multiout.num_dacs > 0) {
3219 p = spec->stream_analog_playback;
3220 if (!p)
3221 p = &pcm_analog_playback;
3222 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3223 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3224 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3225 spec->multiout.max_channels;
3226 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3227 spec->autocfg.line_outs == 2)
3228 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3229 snd_pcm_2_1_chmaps;
3230 }
3231 if (spec->num_adc_nids) {
3232 p = spec->stream_analog_capture;
3233 if (!p) {
3234 if (spec->dyn_adc_switch)
3235 p = &dyn_adc_pcm_analog_capture;
3236 else
3237 p = &pcm_analog_capture;
3238 }
3239 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3240 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3241 }
3242
3243 if (spec->channel_mode) {
3244 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
3245 for (i = 0; i < spec->num_channel_mode; i++) {
3246 if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
3247 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
3248 }
3249 }
3250 }
3251
3252 skip_analog:
3253 /* SPDIF for stream index #1 */
3254 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3255 snprintf(spec->stream_name_digital,
3256 sizeof(spec->stream_name_digital),
3257 "%s Digital", codec->chip_name);
3258 codec->num_pcms = 2;
3259 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3260 info = spec->pcm_rec + 1;
3261 info->name = spec->stream_name_digital;
3262 if (spec->dig_out_type)
3263 info->pcm_type = spec->dig_out_type;
3264 else
3265 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3266 if (spec->multiout.dig_out_nid) {
3267 p = spec->stream_digital_playback;
3268 if (!p)
3269 p = &pcm_digital_playback;
3270 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3271 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3272 }
3273 if (spec->dig_in_nid) {
3274 p = spec->stream_digital_capture;
3275 if (!p)
3276 p = &pcm_digital_capture;
3277 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3278 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3279 }
3280 }
1005 3281
1006 if (! spec->dac_node[0] && ! spec->adc_node) { 3282 if (spec->no_analog)
1007 snd_printd("hda_generic: no PCM found\n");
1008 return 0; 3283 return 0;
3284
3285 /* If the use of more than one ADC is requested for the current
3286 * model, configure a second analog capture-only PCM.
3287 */
3288 have_multi_adcs = (spec->num_adc_nids > 1) &&
3289 !spec->dyn_adc_switch && !spec->auto_mic;
3290 /* Additional Analaog capture for index #2 */
3291 if (spec->alt_dac_nid || have_multi_adcs) {
3292 codec->num_pcms = 3;
3293 info = spec->pcm_rec + 2;
3294 info->name = spec->stream_name_analog;
3295 if (spec->alt_dac_nid) {
3296 p = spec->stream_analog_alt_playback;
3297 if (!p)
3298 p = &pcm_analog_alt_playback;
3299 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3300 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3301 spec->alt_dac_nid;
3302 } else {
3303 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3304 pcm_null_stream;
3305 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3306 }
3307 if (have_multi_adcs) {
3308 p = spec->stream_analog_alt_capture;
3309 if (!p)
3310 p = &pcm_analog_alt_capture;
3311 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3312 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3313 spec->adc_nids[1];
3314 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3315 spec->num_adc_nids - 1;
3316 } else {
3317 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3318 pcm_null_stream;
3319 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3320 }
1009 } 3321 }
1010 3322
1011 codec->num_pcms = 1; 3323 return 0;
1012 codec->pcm_info = info; 3324}
3325EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3326
3327
3328/*
3329 * Standard auto-parser initializations
3330 */
3331
3332/* configure the path from the given dac to the pin as the proper output */
3333static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3334 int pin_type, hda_nid_t dac)
3335{
3336 struct nid_path *path;
3337
3338 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3339 path = snd_hda_get_nid_path(codec, dac, pin);
3340 if (!path)
3341 return;
3342 if (path->active)
3343 return;
3344 snd_hda_activate_path(codec, path, true, true);
3345}
3346
3347/* initialize primary output paths */
3348static void init_multi_out(struct hda_codec *codec)
3349{
3350 struct hda_gen_spec *spec = codec->spec;
3351 int pin_type;
3352 int i;
1013 3353
1014 info->name = "HDA Generic"; 3354 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1015 if (spec->dac_node[0]) { 3355 pin_type = PIN_HP;
1016 info->stream[0] = generic_pcm_playback; 3356 else
1017 info->stream[0].nid = spec->dac_node[0]->nid; 3357 pin_type = PIN_OUT;
1018 if (spec->dac_node[1]) { 3358
1019 info->stream[0].ops.prepare = generic_pcm2_prepare; 3359 for (i = 0; i <= HDA_SIDE; i++) {
1020 info->stream[0].ops.cleanup = generic_pcm2_cleanup; 3360 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3361 if (nid)
3362 set_output_and_unmute(codec, nid, pin_type,
3363 spec->multiout.dac_nids[i]);
3364
3365 }
3366}
3367
3368/* initialize hp and speaker paths */
3369static void init_extra_out(struct hda_codec *codec)
3370{
3371 struct hda_gen_spec *spec = codec->spec;
3372 int i;
3373 hda_nid_t pin, dac;
3374
3375 for (i = 0; i < spec->autocfg.hp_outs; i++) {
3376 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3377 break;
3378 pin = spec->autocfg.hp_pins[i];
3379 if (!pin)
3380 break;
3381 dac = spec->multiout.hp_out_nid[i];
3382 if (!dac) {
3383 if (i > 0 && spec->multiout.hp_out_nid[0])
3384 dac = spec->multiout.hp_out_nid[0];
3385 else
3386 dac = spec->multiout.dac_nids[0];
3387 }
3388 set_output_and_unmute(codec, pin, PIN_HP, dac);
3389 }
3390 for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3391 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3392 break;
3393 pin = spec->autocfg.speaker_pins[i];
3394 if (!pin)
3395 break;
3396 dac = spec->multiout.extra_out_nid[i];
3397 if (!dac) {
3398 if (i > 0 && spec->multiout.extra_out_nid[0])
3399 dac = spec->multiout.extra_out_nid[0];
3400 else
3401 dac = spec->multiout.dac_nids[0];
3402 }
3403 set_output_and_unmute(codec, pin, PIN_OUT, dac);
3404 }
3405}
3406
3407/* initialize multi-io paths */
3408static void init_multi_io(struct hda_codec *codec)
3409{
3410 struct hda_gen_spec *spec = codec->spec;
3411 int i;
3412
3413 for (i = 0; i < spec->multi_ios; i++) {
3414 hda_nid_t pin = spec->multi_io[i].pin;
3415 struct nid_path *path;
3416 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3417 if (!path)
3418 continue;
3419 if (!spec->multi_io[i].ctl_in)
3420 spec->multi_io[i].ctl_in =
3421 snd_hda_codec_update_cache(codec, pin, 0,
3422 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3423 snd_hda_activate_path(codec, path, path->active, true);
3424 }
3425}
3426
3427/* set up the input pin config, depending on the given auto-pin type */
3428static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3429 int auto_pin_type)
3430{
3431 unsigned int val = PIN_IN;
3432 if (auto_pin_type == AUTO_PIN_MIC)
3433 val |= snd_hda_get_default_vref(codec, nid);
3434 snd_hda_set_pin_ctl(codec, nid, val);
3435}
3436
3437/* set up input pins and loopback paths */
3438static void init_analog_input(struct hda_codec *codec)
3439{
3440 struct hda_gen_spec *spec = codec->spec;
3441 struct auto_pin_cfg *cfg = &spec->autocfg;
3442 int i;
3443
3444 for (i = 0; i < cfg->num_inputs; i++) {
3445 hda_nid_t nid = cfg->inputs[i].pin;
3446 if (is_input_pin(codec, nid))
3447 set_input_pin(codec, nid, cfg->inputs[i].type);
3448
3449 /* init loopback inputs */
3450 if (spec->mixer_nid) {
3451 struct nid_path *path;
3452 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3453 if (path)
3454 snd_hda_activate_path(codec, path,
3455 path->active, false);
1021 } 3456 }
1022 } 3457 }
1023 if (spec->adc_node) { 3458}
1024 info->stream[1] = generic_pcm_playback; 3459
1025 info->stream[1].nid = spec->adc_node->nid; 3460/* initialize ADC paths */
3461static void init_input_src(struct hda_codec *codec)
3462{
3463 struct hda_gen_spec *spec = codec->spec;
3464 struct hda_input_mux *imux = &spec->input_mux;
3465 struct nid_path *path;
3466 int i, c, nums;
3467
3468 if (spec->dyn_adc_switch)
3469 nums = 1;
3470 else
3471 nums = spec->num_adc_nids;
3472
3473 for (c = 0; c < nums; c++) {
3474 for (i = 0; i < imux->num_items; i++) {
3475 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3476 get_adc_nid(codec, c, i));
3477 if (path) {
3478 bool active = path->active;
3479 if (i == spec->cur_mux[c])
3480 active = true;
3481 snd_hda_activate_path(codec, path, active, false);
3482 }
3483 }
1026 } 3484 }
1027 3485
3486 if (spec->shared_mic_hp)
3487 update_shared_mic_hp(codec, spec->cur_mux[0]);
3488
3489 if (spec->cap_sync_hook)
3490 spec->cap_sync_hook(codec);
3491}
3492
3493/* set right pin controls for digital I/O */
3494static void init_digital(struct hda_codec *codec)
3495{
3496 struct hda_gen_spec *spec = codec->spec;
3497 int i;
3498 hda_nid_t pin;
3499
3500 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3501 pin = spec->autocfg.dig_out_pins[i];
3502 if (!pin)
3503 continue;
3504 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3505 }
3506 pin = spec->autocfg.dig_in_pin;
3507 if (pin)
3508 snd_hda_set_pin_ctl(codec, pin, PIN_IN);
3509}
3510
3511int snd_hda_gen_init(struct hda_codec *codec)
3512{
3513 struct hda_gen_spec *spec = codec->spec;
3514
3515 if (spec->init_hook)
3516 spec->init_hook(codec);
3517
3518 snd_hda_apply_verbs(codec);
3519
3520 init_multi_out(codec);
3521 init_extra_out(codec);
3522 init_multi_io(codec);
3523 init_analog_input(codec);
3524 init_input_src(codec);
3525 init_digital(codec);
3526
3527 /* call init functions of standard auto-mute helpers */
3528 hp_automute(codec, NULL);
3529 line_automute(codec, NULL);
3530 mic_autoswitch(codec, NULL);
3531
3532 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3533 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3534
3535 hda_call_check_power_status(codec, 0x01);
1028 return 0; 3536 return 0;
1029} 3537}
3538EXPORT_SYMBOL(snd_hda_gen_init);
3539
3540
3541/*
3542 * the generic codec support
3543 */
1030 3544
1031#ifdef CONFIG_PM 3545#ifdef CONFIG_PM
1032static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid) 3546static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1033{ 3547{
1034 struct hda_gspec *spec = codec->spec; 3548 struct hda_gen_spec *spec = codec->spec;
1035 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid); 3549 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1036} 3550}
1037#endif 3551#endif
1038 3552
3553static void generic_free(struct hda_codec *codec)
3554{
3555 snd_hda_gen_spec_free(codec->spec);
3556 kfree(codec->spec);
3557 codec->spec = NULL;
3558}
1039 3559
1040/* 3560static const struct hda_codec_ops generic_patch_ops = {
1041 */ 3561 .build_controls = snd_hda_gen_build_controls,
1042static struct hda_codec_ops generic_patch_ops = { 3562 .build_pcms = snd_hda_gen_build_pcms,
1043 .build_controls = build_generic_controls, 3563 .init = snd_hda_gen_init,
1044 .build_pcms = build_generic_pcms, 3564 .free = generic_free,
1045 .free = snd_hda_generic_free, 3565 .unsol_event = snd_hda_jack_unsol_event,
1046#ifdef CONFIG_PM 3566#ifdef CONFIG_PM
1047 .check_power_status = generic_check_power_status, 3567 .check_power_status = generic_check_power_status,
1048#endif 3568#endif
1049}; 3569};
1050 3570
1051/*
1052 * the generic parser
1053 */
1054int snd_hda_parse_generic_codec(struct hda_codec *codec) 3571int snd_hda_parse_generic_codec(struct hda_codec *codec)
1055{ 3572{
1056 struct hda_gspec *spec; 3573 struct hda_gen_spec *spec;
1057 int err; 3574 int err;
1058 3575
1059 if(!codec->afg)
1060 return 0;
1061
1062 spec = kzalloc(sizeof(*spec), GFP_KERNEL); 3576 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1063 if (spec == NULL) { 3577 if (!spec)
1064 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1065 return -ENOMEM; 3578 return -ENOMEM;
1066 } 3579 snd_hda_gen_spec_init(spec);
1067 codec->spec = spec; 3580 codec->spec = spec;
1068 INIT_LIST_HEAD(&spec->nid_list);
1069
1070 if ((err = build_afg_tree(codec)) < 0)
1071 goto error;
1072 3581
1073 if ((err = parse_input(codec)) < 0 || 3582 err = snd_hda_gen_parse_auto_config(codec, NULL);
1074 (err = parse_output(codec)) < 0) 3583 if (err < 0)
1075 goto error; 3584 goto error;
1076 3585
1077 codec->patch_ops = generic_patch_ops; 3586 codec->patch_ops = generic_patch_ops;
1078
1079 return 0; 3587 return 0;
1080 3588
1081 error: 3589error:
1082 snd_hda_generic_free(codec); 3590 generic_free(codec);
1083 return err; 3591 return err;
1084} 3592}
1085EXPORT_SYMBOL(snd_hda_parse_generic_codec); 3593EXPORT_SYMBOL(snd_hda_parse_generic_codec);
diff --git a/sound/pci/hda/hda_generic.h b/sound/pci/hda/hda_generic.h
new file mode 100644
index 000000000000..a9f4f63b4894
--- /dev/null
+++ b/sound/pci/hda/hda_generic.h
@@ -0,0 +1,199 @@
1/*
2 * Generic BIOS auto-parser helper functions for HD-audio
3 *
4 * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
5 *
6 * This driver is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#ifndef __SOUND_HDA_GENERIC_H
13#define __SOUND_HDA_GENERIC_H
14
15/* unsol event tags */
16enum {
17 HDA_GEN_HP_EVENT, HDA_GEN_FRONT_EVENT, HDA_GEN_MIC_EVENT,
18 HDA_GEN_LAST_EVENT = HDA_GEN_MIC_EVENT
19};
20
21/* table entry for multi-io paths */
22struct hda_multi_io {
23 hda_nid_t pin; /* multi-io widget pin NID */
24 hda_nid_t dac; /* DAC to be connected */
25 unsigned int ctl_in; /* cached input-pin control value */
26};
27
28/* Widget connection path
29 *
30 * For output, stored in the order of DAC -> ... -> pin,
31 * for input, pin -> ... -> ADC.
32 *
33 * idx[i] contains the source index number to select on of the widget path[i];
34 * e.g. idx[1] is the index of the DAC (path[0]) selected by path[1] widget
35 * multi[] indicates whether it's a selector widget with multi-connectors
36 * (i.e. the connection selection is mandatory)
37 * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
38 */
39
40#define MAX_NID_PATH_DEPTH 5
41
42enum {
43 NID_PATH_VOL_CTL,
44 NID_PATH_MUTE_CTL,
45 NID_PATH_BOOST_CTL,
46 NID_PATH_NUM_CTLS
47};
48
49struct nid_path {
50 int depth;
51 hda_nid_t path[MAX_NID_PATH_DEPTH];
52 unsigned char idx[MAX_NID_PATH_DEPTH];
53 unsigned char multi[MAX_NID_PATH_DEPTH];
54 unsigned int ctls[NID_PATH_NUM_CTLS]; /* NID_PATH_XXX_CTL */
55 bool active;
56};
57
58/* mic/line-in auto switching entry */
59
60#define MAX_AUTO_MIC_PINS 3
61
62struct automic_entry {
63 hda_nid_t pin; /* pin */
64 int idx; /* imux index, -1 = invalid */
65 unsigned int attr; /* pin attribute (INPUT_PIN_ATTR_*) */
66};
67
68struct hda_gen_spec {
69 char stream_name_analog[32]; /* analog PCM stream */
70 const struct hda_pcm_stream *stream_analog_playback;
71 const struct hda_pcm_stream *stream_analog_capture;
72 const struct hda_pcm_stream *stream_analog_alt_playback;
73 const struct hda_pcm_stream *stream_analog_alt_capture;
74
75 char stream_name_digital[32]; /* digital PCM stream */
76 const struct hda_pcm_stream *stream_digital_playback;
77 const struct hda_pcm_stream *stream_digital_capture;
78
79 /* playback */
80 struct hda_multi_out multiout; /* playback set-up
81 * max_channels, dacs must be set
82 * dig_out_nid and hp_nid are optional
83 */
84 hda_nid_t alt_dac_nid;
85 hda_nid_t slave_dig_outs[3]; /* optional - for auto-parsing */
86 int dig_out_type;
87
88 /* capture */
89 unsigned int num_adc_nids;
90 hda_nid_t adc_nids[AUTO_CFG_MAX_OUTS];
91 hda_nid_t dig_in_nid; /* digital-in NID; optional */
92 hda_nid_t mixer_nid; /* analog-mixer NID */
93
94 /* capture setup for dynamic dual-adc switch */
95 hda_nid_t cur_adc;
96 unsigned int cur_adc_stream_tag;
97 unsigned int cur_adc_format;
98
99 /* capture source */
100 struct hda_input_mux input_mux;
101 unsigned int cur_mux[3];
102
103 /* channel model */
104 const struct hda_channel_mode *channel_mode;
105 int num_channel_mode;
106 int const_channel_count; /* min. channel count (for speakers) */
107 int ext_channel_count; /* current channel count for multi-io */
108
109 /* PCM information */
110 struct hda_pcm pcm_rec[3]; /* used in build_pcms() */
111
112 /* dynamic controls, init_verbs and input_mux */
113 struct auto_pin_cfg autocfg;
114 struct snd_array kctls;
115 hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
116 hda_nid_t imux_pins[HDA_MAX_NUM_INPUTS];
117 unsigned int dyn_adc_idx[HDA_MAX_NUM_INPUTS];
118 hda_nid_t shared_mic_vref_pin;
119
120 /* DAC list */
121 int num_all_dacs;
122 hda_nid_t all_dacs[16];
123
124 /* path list */
125 struct snd_array paths;
126
127 /* auto-mic stuff */
128 int am_num_entries;
129 struct automic_entry am_entry[MAX_AUTO_MIC_PINS];
130
131 /* for pin sensing */
132 unsigned int hp_jack_present:1;
133 unsigned int line_jack_present:1;
134 unsigned int master_mute:1;
135 unsigned int auto_mic:1;
136 unsigned int automute_speaker:1; /* automute speaker outputs */
137 unsigned int automute_lo:1; /* automute LO outputs */
138 unsigned int detect_hp:1; /* Headphone detection enabled */
139 unsigned int detect_lo:1; /* Line-out detection enabled */
140 unsigned int automute_speaker_possible:1; /* there are speakers and either LO or HP */
141 unsigned int automute_lo_possible:1; /* there are line outs and HP */
142 unsigned int keep_vref_in_automute:1; /* Don't clear VREF in automute */
143 unsigned int line_in_auto_switch:1; /* allow line-in auto switch */
144
145 /* other flags */
146 unsigned int need_dac_fix:1; /* need to limit DACs for multi channels */
147 unsigned int no_analog:1; /* digital I/O only */
148 unsigned int dyn_adc_switch:1; /* switch ADCs (for ALC275) */
149 unsigned int shared_mic_hp:1; /* HP/Mic-in sharing */
150 unsigned int no_primary_hp:1; /* Don't prefer HP pins to speaker pins */
151 unsigned int multi_cap_vol:1; /* allow multiple capture xxx volumes */
152 unsigned int inv_dmic_split:1; /* inverted dmic w/a for conexant */
153
154 unsigned int parse_flags; /* passed to snd_hda_parse_pin_defcfg() */
155
156 /* for virtual master */
157 hda_nid_t vmaster_nid;
158 struct hda_vmaster_mute_hook vmaster_mute;
159#ifdef CONFIG_PM
160 struct hda_loopback_check loopback;
161 int num_loopbacks;
162 struct hda_amp_list loopback_list[8];
163#endif
164
165 /* multi-io */
166 int multi_ios;
167 struct hda_multi_io multi_io[4];
168
169 /* bind volumes */
170 struct snd_array bind_ctls;
171
172 /* hooks */
173 void (*init_hook)(struct hda_codec *codec);
174 void (*automute_hook)(struct hda_codec *codec);
175 void (*cap_sync_hook)(struct hda_codec *codec);
176};
177
178int snd_hda_gen_spec_init(struct hda_gen_spec *spec);
179void snd_hda_gen_spec_free(struct hda_gen_spec *spec);
180
181int snd_hda_gen_init(struct hda_codec *codec);
182
183struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
184 hda_nid_t from_nid, hda_nid_t to_nid);
185bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
186 hda_nid_t to_nid, int with_aa_mix,
187 struct nid_path *path);
188struct nid_path *
189snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
190 hda_nid_t to_nid, int with_aa_mix);
191void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
192 bool enable, bool add_aamix);
193
194int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
195 const hda_nid_t *ignore_nids);
196int snd_hda_gen_build_controls(struct hda_codec *codec);
197int snd_hda_gen_build_pcms(struct hda_codec *codec);
198
199#endif /* __SOUND_HDA_GENERIC_H */