• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5  *
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This driver is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21 
22 #include <linux/mm.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
36 #include "hda_beep.h"
37 #include "hda_jack.h"
38 #include <sound/hda_hwdep.h>
39 
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
42 
43 /*
44  * vendor / preset table
45  */
46 
47 struct hda_vendor_id {
48 	unsigned int id;
49 	const char *name;
50 };
51 
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids[] = {
54 	{ 0x1002, "ATI" },
55 	{ 0x1013, "Cirrus Logic" },
56 	{ 0x1057, "Motorola" },
57 	{ 0x1095, "Silicon Image" },
58 	{ 0x10de, "Nvidia" },
59 	{ 0x10ec, "Realtek" },
60 	{ 0x1102, "Creative" },
61 	{ 0x1106, "VIA" },
62 	{ 0x111d, "IDT" },
63 	{ 0x11c1, "LSI" },
64 	{ 0x11d4, "Analog Devices" },
65 	{ 0x13f6, "C-Media" },
66 	{ 0x14f1, "Conexant" },
67 	{ 0x17e8, "Chrontel" },
68 	{ 0x1854, "LG" },
69 	{ 0x1aec, "Wolfson Microelectronics" },
70 	{ 0x1af4, "QEMU" },
71 	{ 0x434d, "C-Media" },
72 	{ 0x8086, "Intel" },
73 	{ 0x8384, "SigmaTel" },
74 	{} /* terminator */
75 };
76 
77 static DEFINE_MUTEX(preset_mutex);
78 static LIST_HEAD(hda_preset_tables);
79 
snd_hda_add_codec_preset(struct hda_codec_preset_list * preset)80 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
81 {
82 	mutex_lock(&preset_mutex);
83 	list_add_tail(&preset->list, &hda_preset_tables);
84 	mutex_unlock(&preset_mutex);
85 	return 0;
86 }
87 EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
88 
snd_hda_delete_codec_preset(struct hda_codec_preset_list * preset)89 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
90 {
91 	mutex_lock(&preset_mutex);
92 	list_del(&preset->list);
93 	mutex_unlock(&preset_mutex);
94 	return 0;
95 }
96 EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
97 
98 #ifdef CONFIG_PM
99 #define codec_in_pm(codec)	((codec)->in_pm)
100 static void hda_power_work(struct work_struct *work);
101 static void hda_keep_power_on(struct hda_codec *codec);
102 #define hda_codec_is_power_on(codec)	((codec)->power_on)
103 
hda_call_pm_notify(struct hda_codec * codec,bool power_up)104 static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
105 {
106 	struct hda_bus *bus = codec->bus;
107 
108 	if ((power_up && codec->pm_up_notified) ||
109 	    (!power_up && !codec->pm_up_notified))
110 		return;
111 	if (bus->ops.pm_notify)
112 		bus->ops.pm_notify(bus, power_up);
113 	codec->pm_up_notified = power_up;
114 }
115 
116 #else
117 #define codec_in_pm(codec)	0
hda_keep_power_on(struct hda_codec * codec)118 static inline void hda_keep_power_on(struct hda_codec *codec) {}
119 #define hda_codec_is_power_on(codec)	1
120 #define hda_call_pm_notify(codec, state) {}
121 #endif
122 
123 /**
124  * snd_hda_get_jack_location - Give a location string of the jack
125  * @cfg: pin default config value
126  *
127  * Parse the pin default config value and returns the string of the
128  * jack location, e.g. "Rear", "Front", etc.
129  */
snd_hda_get_jack_location(u32 cfg)130 const char *snd_hda_get_jack_location(u32 cfg)
131 {
132 	static char *bases[7] = {
133 		"N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
134 	};
135 	static unsigned char specials_idx[] = {
136 		0x07, 0x08,
137 		0x17, 0x18, 0x19,
138 		0x37, 0x38
139 	};
140 	static char *specials[] = {
141 		"Rear Panel", "Drive Bar",
142 		"Riser", "HDMI", "ATAPI",
143 		"Mobile-In", "Mobile-Out"
144 	};
145 	int i;
146 	cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
147 	if ((cfg & 0x0f) < 7)
148 		return bases[cfg & 0x0f];
149 	for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
150 		if (cfg == specials_idx[i])
151 			return specials[i];
152 	}
153 	return "UNKNOWN";
154 }
155 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
156 
157 /**
158  * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
159  * @cfg: pin default config value
160  *
161  * Parse the pin default config value and returns the string of the
162  * jack connectivity, i.e. external or internal connection.
163  */
snd_hda_get_jack_connectivity(u32 cfg)164 const char *snd_hda_get_jack_connectivity(u32 cfg)
165 {
166 	static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
167 
168 	return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
169 }
170 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
171 
172 /**
173  * snd_hda_get_jack_type - Give a type string of the jack
174  * @cfg: pin default config value
175  *
176  * Parse the pin default config value and returns the string of the
177  * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
178  */
snd_hda_get_jack_type(u32 cfg)179 const char *snd_hda_get_jack_type(u32 cfg)
180 {
181 	static char *jack_types[16] = {
182 		"Line Out", "Speaker", "HP Out", "CD",
183 		"SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
184 		"Line In", "Aux", "Mic", "Telephony",
185 		"SPDIF In", "Digital In", "Reserved", "Other"
186 	};
187 
188 	return jack_types[(cfg & AC_DEFCFG_DEVICE)
189 				>> AC_DEFCFG_DEVICE_SHIFT];
190 }
191 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
192 
193 /*
194  * Compose a 32bit command word to be sent to the HD-audio controller
195  */
196 static inline unsigned int
make_codec_cmd(struct hda_codec * codec,hda_nid_t nid,int flags,unsigned int verb,unsigned int parm)197 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
198 	       unsigned int verb, unsigned int parm)
199 {
200 	u32 val;
201 
202 	if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
203 	    (verb & ~0xfff) || (parm & ~0xffff)) {
204 		codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
205 		       codec->addr, nid, verb, parm);
206 		return ~0;
207 	}
208 
209 	val = (u32)codec->addr << 28;
210 	val |= (u32)nid << 20;
211 	val |= verb << 8;
212 	val |= parm;
213 	return val;
214 }
215 
216 /*
217  * Send and receive a verb
218  */
codec_exec_verb(struct hda_codec * codec,unsigned int cmd,int flags,unsigned int * res)219 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
220 			   int flags, unsigned int *res)
221 {
222 	struct hda_bus *bus = codec->bus;
223 	int err;
224 
225 	if (cmd == ~0)
226 		return -1;
227 
228 	if (res)
229 		*res = -1;
230  again:
231 	snd_hda_power_up(codec);
232 	mutex_lock(&bus->cmd_mutex);
233 	if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
234 		bus->no_response_fallback = 1;
235 	for (;;) {
236 		trace_hda_send_cmd(codec, cmd);
237 		err = bus->ops.command(bus, cmd);
238 		if (err != -EAGAIN)
239 			break;
240 		/* process pending verbs */
241 		bus->ops.get_response(bus, codec->addr);
242 	}
243 	if (!err && res) {
244 		*res = bus->ops.get_response(bus, codec->addr);
245 		trace_hda_get_response(codec, *res);
246 	}
247 	bus->no_response_fallback = 0;
248 	mutex_unlock(&bus->cmd_mutex);
249 	snd_hda_power_down(codec);
250 	if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
251 		if (bus->response_reset) {
252 			codec_dbg(codec,
253 				  "resetting BUS due to fatal communication error\n");
254 			trace_hda_bus_reset(bus);
255 			bus->ops.bus_reset(bus);
256 		}
257 		goto again;
258 	}
259 	/* clear reset-flag when the communication gets recovered */
260 	if (!err || codec_in_pm(codec))
261 		bus->response_reset = 0;
262 	return err;
263 }
264 
265 /**
266  * snd_hda_codec_read - send a command and get the response
267  * @codec: the HDA codec
268  * @nid: NID to send the command
269  * @flags: optional bit flags
270  * @verb: the verb to send
271  * @parm: the parameter for the verb
272  *
273  * Send a single command and read the corresponding response.
274  *
275  * Returns the obtained response value, or -1 for an error.
276  */
snd_hda_codec_read(struct hda_codec * codec,hda_nid_t nid,int flags,unsigned int verb,unsigned int parm)277 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
278 				int flags,
279 				unsigned int verb, unsigned int parm)
280 {
281 	unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
282 	unsigned int res;
283 	if (codec_exec_verb(codec, cmd, flags, &res))
284 		return -1;
285 	return res;
286 }
287 EXPORT_SYMBOL_GPL(snd_hda_codec_read);
288 
289 /**
290  * snd_hda_codec_write - send a single command without waiting for response
291  * @codec: the HDA codec
292  * @nid: NID to send the command
293  * @flags: optional bit flags
294  * @verb: the verb to send
295  * @parm: the parameter for the verb
296  *
297  * Send a single command without waiting for response.
298  *
299  * Returns 0 if successful, or a negative error code.
300  */
snd_hda_codec_write(struct hda_codec * codec,hda_nid_t nid,int flags,unsigned int verb,unsigned int parm)301 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
302 			unsigned int verb, unsigned int parm)
303 {
304 	unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
305 	unsigned int res;
306 	return codec_exec_verb(codec, cmd, flags,
307 			       codec->bus->sync_write ? &res : NULL);
308 }
309 EXPORT_SYMBOL_GPL(snd_hda_codec_write);
310 
311 /**
312  * snd_hda_sequence_write - sequence writes
313  * @codec: the HDA codec
314  * @seq: VERB array to send
315  *
316  * Send the commands sequentially from the given array.
317  * The array must be terminated with NID=0.
318  */
snd_hda_sequence_write(struct hda_codec * codec,const struct hda_verb * seq)319 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
320 {
321 	for (; seq->nid; seq++)
322 		snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
323 }
324 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
325 
326 /**
327  * snd_hda_get_sub_nodes - get the range of sub nodes
328  * @codec: the HDA codec
329  * @nid: NID to parse
330  * @start_id: the pointer to store the start NID
331  *
332  * Parse the NID and store the start NID of its sub-nodes.
333  * Returns the number of sub-nodes.
334  */
snd_hda_get_sub_nodes(struct hda_codec * codec,hda_nid_t nid,hda_nid_t * start_id)335 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
336 			  hda_nid_t *start_id)
337 {
338 	unsigned int parm;
339 
340 	parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
341 	if (parm == -1) {
342 		*start_id = 0;
343 		return 0;
344 	}
345 	*start_id = (parm >> 16) & 0x7fff;
346 	return (int)(parm & 0x7fff);
347 }
348 EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
349 
350 /* connection list element */
351 struct hda_conn_list {
352 	struct list_head list;
353 	int len;
354 	hda_nid_t nid;
355 	hda_nid_t conns[0];
356 };
357 
358 /* look up the cached results */
359 static struct hda_conn_list *
lookup_conn_list(struct hda_codec * codec,hda_nid_t nid)360 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
361 {
362 	struct hda_conn_list *p;
363 	list_for_each_entry(p, &codec->conn_list, list) {
364 		if (p->nid == nid)
365 			return p;
366 	}
367 	return NULL;
368 }
369 
add_conn_list(struct hda_codec * codec,hda_nid_t nid,int len,const hda_nid_t * list)370 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
371 			 const hda_nid_t *list)
372 {
373 	struct hda_conn_list *p;
374 
375 	p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
376 	if (!p)
377 		return -ENOMEM;
378 	p->len = len;
379 	p->nid = nid;
380 	memcpy(p->conns, list, len * sizeof(hda_nid_t));
381 	list_add(&p->list, &codec->conn_list);
382 	return 0;
383 }
384 
remove_conn_list(struct hda_codec * codec)385 static void remove_conn_list(struct hda_codec *codec)
386 {
387 	while (!list_empty(&codec->conn_list)) {
388 		struct hda_conn_list *p;
389 		p = list_first_entry(&codec->conn_list, typeof(*p), list);
390 		list_del(&p->list);
391 		kfree(p);
392 	}
393 }
394 
395 /* read the connection and add to the cache */
read_and_add_raw_conns(struct hda_codec * codec,hda_nid_t nid)396 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
397 {
398 	hda_nid_t list[32];
399 	hda_nid_t *result = list;
400 	int len;
401 
402 	len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
403 	if (len == -ENOSPC) {
404 		len = snd_hda_get_num_raw_conns(codec, nid);
405 		result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
406 		if (!result)
407 			return -ENOMEM;
408 		len = snd_hda_get_raw_connections(codec, nid, result, len);
409 	}
410 	if (len >= 0)
411 		len = snd_hda_override_conn_list(codec, nid, len, result);
412 	if (result != list)
413 		kfree(result);
414 	return len;
415 }
416 
417 /**
418  * snd_hda_get_conn_list - get connection list
419  * @codec: the HDA codec
420  * @nid: NID to parse
421  * @len: number of connection list entries
422  * @listp: the pointer to store NID list
423  *
424  * Parses the connection list of the given widget and stores the pointer
425  * to the list of NIDs.
426  *
427  * Returns the number of connections, or a negative error code.
428  *
429  * Note that the returned pointer isn't protected against the list
430  * modification.  If snd_hda_override_conn_list() might be called
431  * concurrently, protect with a mutex appropriately.
432  */
snd_hda_get_conn_list(struct hda_codec * codec,hda_nid_t nid,const hda_nid_t ** listp)433 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
434 			  const hda_nid_t **listp)
435 {
436 	bool added = false;
437 
438 	for (;;) {
439 		int err;
440 		const struct hda_conn_list *p;
441 
442 		/* if the connection-list is already cached, read it */
443 		p = lookup_conn_list(codec, nid);
444 		if (p) {
445 			if (listp)
446 				*listp = p->conns;
447 			return p->len;
448 		}
449 		if (snd_BUG_ON(added))
450 			return -EINVAL;
451 
452 		err = read_and_add_raw_conns(codec, nid);
453 		if (err < 0)
454 			return err;
455 		added = true;
456 	}
457 }
458 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
459 
460 /**
461  * snd_hda_get_connections - copy connection list
462  * @codec: the HDA codec
463  * @nid: NID to parse
464  * @conn_list: connection list array; when NULL, checks only the size
465  * @max_conns: max. number of connections to store
466  *
467  * Parses the connection list of the given widget and stores the list
468  * of NIDs.
469  *
470  * Returns the number of connections, or a negative error code.
471  */
snd_hda_get_connections(struct hda_codec * codec,hda_nid_t nid,hda_nid_t * conn_list,int max_conns)472 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
473 			    hda_nid_t *conn_list, int max_conns)
474 {
475 	const hda_nid_t *list;
476 	int len = snd_hda_get_conn_list(codec, nid, &list);
477 
478 	if (len > 0 && conn_list) {
479 		if (len > max_conns) {
480 			codec_err(codec, "Too many connections %d for NID 0x%x\n",
481 				   len, nid);
482 			return -EINVAL;
483 		}
484 		memcpy(conn_list, list, len * sizeof(hda_nid_t));
485 	}
486 
487 	return len;
488 }
489 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
490 
491 /* return CONNLIST_LEN parameter of the given widget */
get_num_conns(struct hda_codec * codec,hda_nid_t nid)492 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
493 {
494 	unsigned int wcaps = get_wcaps(codec, nid);
495 	unsigned int parm;
496 
497 	if (!(wcaps & AC_WCAP_CONN_LIST) &&
498 	    get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
499 		return 0;
500 
501 	parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
502 	if (parm == -1)
503 		parm = 0;
504 	return parm;
505 }
506 
snd_hda_get_num_raw_conns(struct hda_codec * codec,hda_nid_t nid)507 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
508 {
509 	return snd_hda_get_raw_connections(codec, nid, NULL, 0);
510 }
511 
512 /**
513  * snd_hda_get_raw_connections - copy connection list without cache
514  * @codec: the HDA codec
515  * @nid: NID to parse
516  * @conn_list: connection list array
517  * @max_conns: max. number of connections to store
518  *
519  * Like snd_hda_get_connections(), copy the connection list but without
520  * checking through the connection-list cache.
521  * Currently called only from hda_proc.c, so not exported.
522  */
snd_hda_get_raw_connections(struct hda_codec * codec,hda_nid_t nid,hda_nid_t * conn_list,int max_conns)523 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
524 				hda_nid_t *conn_list, int max_conns)
525 {
526 	unsigned int parm;
527 	int i, conn_len, conns;
528 	unsigned int shift, num_elems, mask;
529 	hda_nid_t prev_nid;
530 	int null_count = 0;
531 
532 	parm = get_num_conns(codec, nid);
533 	if (!parm)
534 		return 0;
535 
536 	if (parm & AC_CLIST_LONG) {
537 		/* long form */
538 		shift = 16;
539 		num_elems = 2;
540 	} else {
541 		/* short form */
542 		shift = 8;
543 		num_elems = 4;
544 	}
545 	conn_len = parm & AC_CLIST_LENGTH;
546 	mask = (1 << (shift-1)) - 1;
547 
548 	if (!conn_len)
549 		return 0; /* no connection */
550 
551 	if (conn_len == 1) {
552 		/* single connection */
553 		parm = snd_hda_codec_read(codec, nid, 0,
554 					  AC_VERB_GET_CONNECT_LIST, 0);
555 		if (parm == -1 && codec->bus->rirb_error)
556 			return -EIO;
557 		if (conn_list)
558 			conn_list[0] = parm & mask;
559 		return 1;
560 	}
561 
562 	/* multi connection */
563 	conns = 0;
564 	prev_nid = 0;
565 	for (i = 0; i < conn_len; i++) {
566 		int range_val;
567 		hda_nid_t val, n;
568 
569 		if (i % num_elems == 0) {
570 			parm = snd_hda_codec_read(codec, nid, 0,
571 						  AC_VERB_GET_CONNECT_LIST, i);
572 			if (parm == -1 && codec->bus->rirb_error)
573 				return -EIO;
574 		}
575 		range_val = !!(parm & (1 << (shift-1))); /* ranges */
576 		val = parm & mask;
577 		if (val == 0 && null_count++) {  /* no second chance */
578 			codec_dbg(codec,
579 				  "invalid CONNECT_LIST verb %x[%i]:%x\n",
580 				    nid, i, parm);
581 			return 0;
582 		}
583 		parm >>= shift;
584 		if (range_val) {
585 			/* ranges between the previous and this one */
586 			if (!prev_nid || prev_nid >= val) {
587 				codec_warn(codec,
588 					   "invalid dep_range_val %x:%x\n",
589 					   prev_nid, val);
590 				continue;
591 			}
592 			for (n = prev_nid + 1; n <= val; n++) {
593 				if (conn_list) {
594 					if (conns >= max_conns)
595 						return -ENOSPC;
596 					conn_list[conns] = n;
597 				}
598 				conns++;
599 			}
600 		} else {
601 			if (conn_list) {
602 				if (conns >= max_conns)
603 					return -ENOSPC;
604 				conn_list[conns] = val;
605 			}
606 			conns++;
607 		}
608 		prev_nid = val;
609 	}
610 	return conns;
611 }
612 
613 /**
614  * snd_hda_override_conn_list - add/modify the connection-list to cache
615  * @codec: the HDA codec
616  * @nid: NID to parse
617  * @len: number of connection list entries
618  * @list: the list of connection entries
619  *
620  * Add or modify the given connection-list to the cache.  If the corresponding
621  * cache already exists, invalidate it and append a new one.
622  *
623  * Returns zero or a negative error code.
624  */
snd_hda_override_conn_list(struct hda_codec * codec,hda_nid_t nid,int len,const hda_nid_t * list)625 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
626 			       const hda_nid_t *list)
627 {
628 	struct hda_conn_list *p;
629 
630 	p = lookup_conn_list(codec, nid);
631 	if (p) {
632 		list_del(&p->list);
633 		kfree(p);
634 	}
635 
636 	return add_conn_list(codec, nid, len, list);
637 }
638 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
639 
640 /**
641  * snd_hda_get_conn_index - get the connection index of the given NID
642  * @codec: the HDA codec
643  * @mux: NID containing the list
644  * @nid: NID to select
645  * @recursive: 1 when searching NID recursively, otherwise 0
646  *
647  * Parses the connection list of the widget @mux and checks whether the
648  * widget @nid is present.  If it is, return the connection index.
649  * Otherwise it returns -1.
650  */
snd_hda_get_conn_index(struct hda_codec * codec,hda_nid_t mux,hda_nid_t nid,int recursive)651 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
652 			   hda_nid_t nid, int recursive)
653 {
654 	const hda_nid_t *conn;
655 	int i, nums;
656 
657 	nums = snd_hda_get_conn_list(codec, mux, &conn);
658 	for (i = 0; i < nums; i++)
659 		if (conn[i] == nid)
660 			return i;
661 	if (!recursive)
662 		return -1;
663 	if (recursive > 10) {
664 		codec_dbg(codec, "too deep connection for 0x%x\n", nid);
665 		return -1;
666 	}
667 	recursive++;
668 	for (i = 0; i < nums; i++) {
669 		unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
670 		if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
671 			continue;
672 		if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
673 			return i;
674 	}
675 	return -1;
676 }
677 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
678 
679 
680 /* return DEVLIST_LEN parameter of the given widget */
get_num_devices(struct hda_codec * codec,hda_nid_t nid)681 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
682 {
683 	unsigned int wcaps = get_wcaps(codec, nid);
684 	unsigned int parm;
685 
686 	if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
687 	    get_wcaps_type(wcaps) != AC_WID_PIN)
688 		return 0;
689 
690 	parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
691 	if (parm == -1 && codec->bus->rirb_error)
692 		parm = 0;
693 	return parm & AC_DEV_LIST_LEN_MASK;
694 }
695 
696 /**
697  * snd_hda_get_devices - copy device list without cache
698  * @codec: the HDA codec
699  * @nid: NID of the pin to parse
700  * @dev_list: device list array
701  * @max_devices: max. number of devices to store
702  *
703  * Copy the device list. This info is dynamic and so not cached.
704  * Currently called only from hda_proc.c, so not exported.
705  */
snd_hda_get_devices(struct hda_codec * codec,hda_nid_t nid,u8 * dev_list,int max_devices)706 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
707 			u8 *dev_list, int max_devices)
708 {
709 	unsigned int parm;
710 	int i, dev_len, devices;
711 
712 	parm = get_num_devices(codec, nid);
713 	if (!parm)	/* not multi-stream capable */
714 		return 0;
715 
716 	dev_len = parm + 1;
717 	dev_len = dev_len < max_devices ? dev_len : max_devices;
718 
719 	devices = 0;
720 	while (devices < dev_len) {
721 		parm = snd_hda_codec_read(codec, nid, 0,
722 					  AC_VERB_GET_DEVICE_LIST, devices);
723 		if (parm == -1 && codec->bus->rirb_error)
724 			break;
725 
726 		for (i = 0; i < 8; i++) {
727 			dev_list[devices] = (u8)parm;
728 			parm >>= 4;
729 			devices++;
730 			if (devices >= dev_len)
731 				break;
732 		}
733 	}
734 	return devices;
735 }
736 
737 /**
738  * snd_hda_queue_unsol_event - add an unsolicited event to queue
739  * @bus: the BUS
740  * @res: unsolicited event (lower 32bit of RIRB entry)
741  * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
742  *
743  * Adds the given event to the queue.  The events are processed in
744  * the workqueue asynchronously.  Call this function in the interrupt
745  * hanlder when RIRB receives an unsolicited event.
746  *
747  * Returns 0 if successful, or a negative error code.
748  */
snd_hda_queue_unsol_event(struct hda_bus * bus,u32 res,u32 res_ex)749 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
750 {
751 	struct hda_bus_unsolicited *unsol;
752 	unsigned int wp;
753 
754 	if (!bus || !bus->workq)
755 		return 0;
756 
757 	trace_hda_unsol_event(bus, res, res_ex);
758 	unsol = bus->unsol;
759 	if (!unsol)
760 		return 0;
761 
762 	wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
763 	unsol->wp = wp;
764 
765 	wp <<= 1;
766 	unsol->queue[wp] = res;
767 	unsol->queue[wp + 1] = res_ex;
768 
769 	queue_work(bus->workq, &unsol->work);
770 
771 	return 0;
772 }
773 EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
774 
775 /*
776  * process queued unsolicited events
777  */
process_unsol_events(struct work_struct * work)778 static void process_unsol_events(struct work_struct *work)
779 {
780 	struct hda_bus_unsolicited *unsol =
781 		container_of(work, struct hda_bus_unsolicited, work);
782 	struct hda_bus *bus = unsol->bus;
783 	struct hda_codec *codec;
784 	unsigned int rp, caddr, res;
785 
786 	while (unsol->rp != unsol->wp) {
787 		rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
788 		unsol->rp = rp;
789 		rp <<= 1;
790 		res = unsol->queue[rp];
791 		caddr = unsol->queue[rp + 1];
792 		if (!(caddr & (1 << 4))) /* no unsolicited event? */
793 			continue;
794 		codec = bus->caddr_tbl[caddr & 0x0f];
795 		if (codec && codec->patch_ops.unsol_event)
796 			codec->patch_ops.unsol_event(codec, res);
797 	}
798 }
799 
800 /*
801  * initialize unsolicited queue
802  */
init_unsol_queue(struct hda_bus * bus)803 static int init_unsol_queue(struct hda_bus *bus)
804 {
805 	struct hda_bus_unsolicited *unsol;
806 
807 	if (bus->unsol) /* already initialized */
808 		return 0;
809 
810 	unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
811 	if (!unsol) {
812 		dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
813 		return -ENOMEM;
814 	}
815 	INIT_WORK(&unsol->work, process_unsol_events);
816 	unsol->bus = bus;
817 	bus->unsol = unsol;
818 	return 0;
819 }
820 
821 /*
822  * destructor
823  */
snd_hda_bus_free(struct hda_bus * bus)824 static void snd_hda_bus_free(struct hda_bus *bus)
825 {
826 	if (!bus)
827 		return;
828 
829 	WARN_ON(!list_empty(&bus->codec_list));
830 	if (bus->workq)
831 		flush_workqueue(bus->workq);
832 	if (bus->unsol)
833 		kfree(bus->unsol);
834 	if (bus->ops.private_free)
835 		bus->ops.private_free(bus);
836 	if (bus->workq)
837 		destroy_workqueue(bus->workq);
838 
839 	kfree(bus);
840 }
841 
snd_hda_bus_dev_free(struct snd_device * device)842 static int snd_hda_bus_dev_free(struct snd_device *device)
843 {
844 	snd_hda_bus_free(device->device_data);
845 	return 0;
846 }
847 
snd_hda_bus_dev_disconnect(struct snd_device * device)848 static int snd_hda_bus_dev_disconnect(struct snd_device *device)
849 {
850 	struct hda_bus *bus = device->device_data;
851 	bus->shutdown = 1;
852 	return 0;
853 }
854 
855 /**
856  * snd_hda_bus_new - create a HDA bus
857  * @card: the card entry
858  * @temp: the template for hda_bus information
859  * @busp: the pointer to store the created bus instance
860  *
861  * Returns 0 if successful, or a negative error code.
862  */
snd_hda_bus_new(struct snd_card * card,const struct hda_bus_template * temp,struct hda_bus ** busp)863 int snd_hda_bus_new(struct snd_card *card,
864 			      const struct hda_bus_template *temp,
865 			      struct hda_bus **busp)
866 {
867 	struct hda_bus *bus;
868 	int err;
869 	static struct snd_device_ops dev_ops = {
870 		.dev_disconnect = snd_hda_bus_dev_disconnect,
871 		.dev_free = snd_hda_bus_dev_free,
872 	};
873 
874 	if (snd_BUG_ON(!temp))
875 		return -EINVAL;
876 	if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
877 		return -EINVAL;
878 
879 	if (busp)
880 		*busp = NULL;
881 
882 	bus = kzalloc(sizeof(*bus), GFP_KERNEL);
883 	if (bus == NULL) {
884 		dev_err(card->dev, "can't allocate struct hda_bus\n");
885 		return -ENOMEM;
886 	}
887 
888 	bus->card = card;
889 	bus->private_data = temp->private_data;
890 	bus->pci = temp->pci;
891 	bus->modelname = temp->modelname;
892 	bus->power_save = temp->power_save;
893 	bus->ops = temp->ops;
894 
895 	mutex_init(&bus->cmd_mutex);
896 	mutex_init(&bus->prepare_mutex);
897 	INIT_LIST_HEAD(&bus->codec_list);
898 
899 	snprintf(bus->workq_name, sizeof(bus->workq_name),
900 		 "hd-audio%d", card->number);
901 	bus->workq = create_singlethread_workqueue(bus->workq_name);
902 	if (!bus->workq) {
903 		dev_err(card->dev, "cannot create workqueue %s\n",
904 			   bus->workq_name);
905 		kfree(bus);
906 		return -ENOMEM;
907 	}
908 
909 	err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
910 	if (err < 0) {
911 		snd_hda_bus_free(bus);
912 		return err;
913 	}
914 	if (busp)
915 		*busp = bus;
916 	return 0;
917 }
918 EXPORT_SYMBOL_GPL(snd_hda_bus_new);
919 
920 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
921 #define is_generic_config(codec) \
922 	(codec->modelname && !strcmp(codec->modelname, "generic"))
923 #else
924 #define is_generic_config(codec)	0
925 #endif
926 
927 #ifdef MODULE
928 #define HDA_MODREQ_MAX_COUNT	2	/* two request_modules()'s */
929 #else
930 #define HDA_MODREQ_MAX_COUNT	0	/* all presets are statically linked */
931 #endif
932 
933 /*
934  * find a matching codec preset
935  */
936 static const struct hda_codec_preset *
find_codec_preset(struct hda_codec * codec)937 find_codec_preset(struct hda_codec *codec)
938 {
939 	struct hda_codec_preset_list *tbl;
940 	const struct hda_codec_preset *preset;
941 	unsigned int mod_requested = 0;
942 
943  again:
944 	mutex_lock(&preset_mutex);
945 	list_for_each_entry(tbl, &hda_preset_tables, list) {
946 		if (!try_module_get(tbl->owner)) {
947 			codec_err(codec, "cannot module_get\n");
948 			continue;
949 		}
950 		for (preset = tbl->preset; preset->id; preset++) {
951 			u32 mask = preset->mask;
952 			if (preset->afg && preset->afg != codec->afg)
953 				continue;
954 			if (preset->mfg && preset->mfg != codec->mfg)
955 				continue;
956 			if (!mask)
957 				mask = ~0;
958 			if (preset->id == (codec->vendor_id & mask) &&
959 			    (!preset->rev ||
960 			     preset->rev == codec->revision_id)) {
961 				mutex_unlock(&preset_mutex);
962 				codec->owner = tbl->owner;
963 				return preset;
964 			}
965 		}
966 		module_put(tbl->owner);
967 	}
968 	mutex_unlock(&preset_mutex);
969 
970 	if (mod_requested < HDA_MODREQ_MAX_COUNT) {
971 		char name[32];
972 		if (!mod_requested)
973 			snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
974 				 codec->vendor_id);
975 		else
976 			snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
977 				 (codec->vendor_id >> 16) & 0xffff);
978 		request_module(name);
979 		mod_requested++;
980 		goto again;
981 	}
982 	return NULL;
983 }
984 
985 /*
986  * get_codec_name - store the codec name
987  */
get_codec_name(struct hda_codec * codec)988 static int get_codec_name(struct hda_codec *codec)
989 {
990 	const struct hda_vendor_id *c;
991 	const char *vendor = NULL;
992 	u16 vendor_id = codec->vendor_id >> 16;
993 	char tmp[16];
994 
995 	if (codec->vendor_name)
996 		goto get_chip_name;
997 
998 	for (c = hda_vendor_ids; c->id; c++) {
999 		if (c->id == vendor_id) {
1000 			vendor = c->name;
1001 			break;
1002 		}
1003 	}
1004 	if (!vendor) {
1005 		sprintf(tmp, "Generic %04x", vendor_id);
1006 		vendor = tmp;
1007 	}
1008 	codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1009 	if (!codec->vendor_name)
1010 		return -ENOMEM;
1011 
1012  get_chip_name:
1013 	if (codec->chip_name)
1014 		return 0;
1015 
1016 	if (codec->preset && codec->preset->name)
1017 		codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1018 	else {
1019 		sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1020 		codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1021 	}
1022 	if (!codec->chip_name)
1023 		return -ENOMEM;
1024 	return 0;
1025 }
1026 
1027 /*
1028  * look for an AFG and MFG nodes
1029  */
setup_fg_nodes(struct hda_codec * codec)1030 static void setup_fg_nodes(struct hda_codec *codec)
1031 {
1032 	int i, total_nodes, function_id;
1033 	hda_nid_t nid;
1034 
1035 	total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1036 	for (i = 0; i < total_nodes; i++, nid++) {
1037 		function_id = snd_hda_param_read(codec, nid,
1038 						AC_PAR_FUNCTION_TYPE);
1039 		switch (function_id & 0xff) {
1040 		case AC_GRP_AUDIO_FUNCTION:
1041 			codec->afg = nid;
1042 			codec->afg_function_id = function_id & 0xff;
1043 			codec->afg_unsol = (function_id >> 8) & 1;
1044 			break;
1045 		case AC_GRP_MODEM_FUNCTION:
1046 			codec->mfg = nid;
1047 			codec->mfg_function_id = function_id & 0xff;
1048 			codec->mfg_unsol = (function_id >> 8) & 1;
1049 			break;
1050 		default:
1051 			break;
1052 		}
1053 	}
1054 }
1055 
1056 /*
1057  * read widget caps for each widget and store in cache
1058  */
read_widget_caps(struct hda_codec * codec,hda_nid_t fg_node)1059 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1060 {
1061 	int i;
1062 	hda_nid_t nid;
1063 
1064 	codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1065 						 &codec->start_nid);
1066 	codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1067 	if (!codec->wcaps)
1068 		return -ENOMEM;
1069 	nid = codec->start_nid;
1070 	for (i = 0; i < codec->num_nodes; i++, nid++)
1071 		codec->wcaps[i] = snd_hda_param_read(codec, nid,
1072 						     AC_PAR_AUDIO_WIDGET_CAP);
1073 	return 0;
1074 }
1075 
1076 /* read all pin default configurations and save codec->init_pins */
read_pin_defaults(struct hda_codec * codec)1077 static int read_pin_defaults(struct hda_codec *codec)
1078 {
1079 	int i;
1080 	hda_nid_t nid = codec->start_nid;
1081 
1082 	for (i = 0; i < codec->num_nodes; i++, nid++) {
1083 		struct hda_pincfg *pin;
1084 		unsigned int wcaps = get_wcaps(codec, nid);
1085 		unsigned int wid_type = get_wcaps_type(wcaps);
1086 		if (wid_type != AC_WID_PIN)
1087 			continue;
1088 		pin = snd_array_new(&codec->init_pins);
1089 		if (!pin)
1090 			return -ENOMEM;
1091 		pin->nid = nid;
1092 		pin->cfg = snd_hda_codec_read(codec, nid, 0,
1093 					      AC_VERB_GET_CONFIG_DEFAULT, 0);
1094 		pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1095 					       AC_VERB_GET_PIN_WIDGET_CONTROL,
1096 					       0);
1097 	}
1098 	return 0;
1099 }
1100 
1101 /* look up the given pin config list and return the item matching with NID */
look_up_pincfg(struct hda_codec * codec,struct snd_array * array,hda_nid_t nid)1102 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1103 					 struct snd_array *array,
1104 					 hda_nid_t nid)
1105 {
1106 	int i;
1107 	for (i = 0; i < array->used; i++) {
1108 		struct hda_pincfg *pin = snd_array_elem(array, i);
1109 		if (pin->nid == nid)
1110 			return pin;
1111 	}
1112 	return NULL;
1113 }
1114 
1115 /* set the current pin config value for the given NID.
1116  * the value is cached, and read via snd_hda_codec_get_pincfg()
1117  */
snd_hda_add_pincfg(struct hda_codec * codec,struct snd_array * list,hda_nid_t nid,unsigned int cfg)1118 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1119 		       hda_nid_t nid, unsigned int cfg)
1120 {
1121 	struct hda_pincfg *pin;
1122 
1123 	/* the check below may be invalid when pins are added by a fixup
1124 	 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1125 	 * for now
1126 	 */
1127 	/*
1128 	if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1129 		return -EINVAL;
1130 	*/
1131 
1132 	pin = look_up_pincfg(codec, list, nid);
1133 	if (!pin) {
1134 		pin = snd_array_new(list);
1135 		if (!pin)
1136 			return -ENOMEM;
1137 		pin->nid = nid;
1138 	}
1139 	pin->cfg = cfg;
1140 	return 0;
1141 }
1142 
1143 /**
1144  * snd_hda_codec_set_pincfg - Override a pin default configuration
1145  * @codec: the HDA codec
1146  * @nid: NID to set the pin config
1147  * @cfg: the pin default config value
1148  *
1149  * Override a pin default configuration value in the cache.
1150  * This value can be read by snd_hda_codec_get_pincfg() in a higher
1151  * priority than the real hardware value.
1152  */
snd_hda_codec_set_pincfg(struct hda_codec * codec,hda_nid_t nid,unsigned int cfg)1153 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1154 			     hda_nid_t nid, unsigned int cfg)
1155 {
1156 	return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1157 }
1158 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
1159 
1160 /**
1161  * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1162  * @codec: the HDA codec
1163  * @nid: NID to get the pin config
1164  *
1165  * Get the current pin config value of the given pin NID.
1166  * If the pincfg value is cached or overridden via sysfs or driver,
1167  * returns the cached value.
1168  */
snd_hda_codec_get_pincfg(struct hda_codec * codec,hda_nid_t nid)1169 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1170 {
1171 	struct hda_pincfg *pin;
1172 
1173 #ifdef CONFIG_SND_HDA_RECONFIG
1174 	{
1175 		unsigned int cfg = 0;
1176 		mutex_lock(&codec->user_mutex);
1177 		pin = look_up_pincfg(codec, &codec->user_pins, nid);
1178 		if (pin)
1179 			cfg = pin->cfg;
1180 		mutex_unlock(&codec->user_mutex);
1181 		if (cfg)
1182 			return cfg;
1183 	}
1184 #endif
1185 	pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1186 	if (pin)
1187 		return pin->cfg;
1188 	pin = look_up_pincfg(codec, &codec->init_pins, nid);
1189 	if (pin)
1190 		return pin->cfg;
1191 	return 0;
1192 }
1193 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
1194 
1195 /* remember the current pinctl target value */
snd_hda_codec_set_pin_target(struct hda_codec * codec,hda_nid_t nid,unsigned int val)1196 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1197 				 unsigned int val)
1198 {
1199 	struct hda_pincfg *pin;
1200 
1201 	pin = look_up_pincfg(codec, &codec->init_pins, nid);
1202 	if (!pin)
1203 		return -EINVAL;
1204 	pin->target = val;
1205 	return 0;
1206 }
1207 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
1208 
1209 /* return the current pinctl target value */
snd_hda_codec_get_pin_target(struct hda_codec * codec,hda_nid_t nid)1210 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1211 {
1212 	struct hda_pincfg *pin;
1213 
1214 	pin = look_up_pincfg(codec, &codec->init_pins, nid);
1215 	if (!pin)
1216 		return 0;
1217 	return pin->target;
1218 }
1219 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
1220 
1221 /**
1222  * snd_hda_shutup_pins - Shut up all pins
1223  * @codec: the HDA codec
1224  *
1225  * Clear all pin controls to shup up before suspend for avoiding click noise.
1226  * The controls aren't cached so that they can be resumed properly.
1227  */
snd_hda_shutup_pins(struct hda_codec * codec)1228 void snd_hda_shutup_pins(struct hda_codec *codec)
1229 {
1230 	int i;
1231 	/* don't shut up pins when unloading the driver; otherwise it breaks
1232 	 * the default pin setup at the next load of the driver
1233 	 */
1234 	if (codec->bus->shutdown)
1235 		return;
1236 	for (i = 0; i < codec->init_pins.used; i++) {
1237 		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1238 		/* use read here for syncing after issuing each verb */
1239 		snd_hda_codec_read(codec, pin->nid, 0,
1240 				   AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1241 	}
1242 	codec->pins_shutup = 1;
1243 }
1244 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
1245 
1246 #ifdef CONFIG_PM
1247 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
restore_shutup_pins(struct hda_codec * codec)1248 static void restore_shutup_pins(struct hda_codec *codec)
1249 {
1250 	int i;
1251 	if (!codec->pins_shutup)
1252 		return;
1253 	if (codec->bus->shutdown)
1254 		return;
1255 	for (i = 0; i < codec->init_pins.used; i++) {
1256 		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1257 		snd_hda_codec_write(codec, pin->nid, 0,
1258 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
1259 				    pin->ctrl);
1260 	}
1261 	codec->pins_shutup = 0;
1262 }
1263 #endif
1264 
hda_jackpoll_work(struct work_struct * work)1265 static void hda_jackpoll_work(struct work_struct *work)
1266 {
1267 	struct hda_codec *codec =
1268 		container_of(work, struct hda_codec, jackpoll_work.work);
1269 
1270 	snd_hda_jack_set_dirty_all(codec);
1271 	snd_hda_jack_poll_all(codec);
1272 
1273 	if (!codec->jackpoll_interval)
1274 		return;
1275 
1276 	queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1277 			   codec->jackpoll_interval);
1278 }
1279 
1280 static void init_hda_cache(struct hda_cache_rec *cache,
1281 			   unsigned int record_size);
1282 static void free_hda_cache(struct hda_cache_rec *cache);
1283 
1284 /* release all pincfg lists */
free_init_pincfgs(struct hda_codec * codec)1285 static void free_init_pincfgs(struct hda_codec *codec)
1286 {
1287 	snd_array_free(&codec->driver_pins);
1288 #ifdef CONFIG_SND_HDA_RECONFIG
1289 	snd_array_free(&codec->user_pins);
1290 #endif
1291 	snd_array_free(&codec->init_pins);
1292 }
1293 
1294 /*
1295  * audio-converter setup caches
1296  */
1297 struct hda_cvt_setup {
1298 	hda_nid_t nid;
1299 	u8 stream_tag;
1300 	u8 channel_id;
1301 	u16 format_id;
1302 	unsigned char active;	/* cvt is currently used */
1303 	unsigned char dirty;	/* setups should be cleared */
1304 };
1305 
1306 /* get or create a cache entry for the given audio converter NID */
1307 static struct hda_cvt_setup *
get_hda_cvt_setup(struct hda_codec * codec,hda_nid_t nid)1308 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1309 {
1310 	struct hda_cvt_setup *p;
1311 	int i;
1312 
1313 	for (i = 0; i < codec->cvt_setups.used; i++) {
1314 		p = snd_array_elem(&codec->cvt_setups, i);
1315 		if (p->nid == nid)
1316 			return p;
1317 	}
1318 	p = snd_array_new(&codec->cvt_setups);
1319 	if (p)
1320 		p->nid = nid;
1321 	return p;
1322 }
1323 
1324 /*
1325  * Dynamic symbol binding for the codec parsers
1326  */
1327 
1328 #define load_parser(codec, sym) \
1329 	((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
1330 
unload_parser(struct hda_codec * codec)1331 static void unload_parser(struct hda_codec *codec)
1332 {
1333 	if (codec->parser)
1334 		symbol_put_addr(codec->parser);
1335 	codec->parser = NULL;
1336 }
1337 
1338 /*
1339  * codec destructor
1340  */
snd_hda_codec_free(struct hda_codec * codec)1341 static void snd_hda_codec_free(struct hda_codec *codec)
1342 {
1343 	if (!codec)
1344 		return;
1345 	cancel_delayed_work_sync(&codec->jackpoll_work);
1346 	snd_hda_jack_tbl_clear(codec);
1347 	free_init_pincfgs(codec);
1348 #ifdef CONFIG_PM
1349 	cancel_delayed_work(&codec->power_work);
1350 	flush_workqueue(codec->bus->workq);
1351 #endif
1352 	list_del(&codec->list);
1353 	snd_array_free(&codec->mixers);
1354 	snd_array_free(&codec->nids);
1355 	snd_array_free(&codec->cvt_setups);
1356 	snd_array_free(&codec->spdif_out);
1357 	remove_conn_list(codec);
1358 	codec->bus->caddr_tbl[codec->addr] = NULL;
1359 	if (codec->patch_ops.free)
1360 		codec->patch_ops.free(codec);
1361 	hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
1362 	snd_hda_sysfs_clear(codec);
1363 	unload_parser(codec);
1364 	module_put(codec->owner);
1365 	free_hda_cache(&codec->amp_cache);
1366 	free_hda_cache(&codec->cmd_cache);
1367 	kfree(codec->vendor_name);
1368 	kfree(codec->chip_name);
1369 	kfree(codec->modelname);
1370 	kfree(codec->wcaps);
1371 	codec->bus->num_codecs--;
1372 	put_device(&codec->dev);
1373 }
1374 
1375 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1376 				hda_nid_t fg, unsigned int power_state);
1377 
1378 static unsigned int hda_set_power_state(struct hda_codec *codec,
1379 				unsigned int power_state);
1380 
snd_hda_codec_dev_register(struct snd_device * device)1381 static int snd_hda_codec_dev_register(struct snd_device *device)
1382 {
1383 	struct hda_codec *codec = device->device_data;
1384 	int err = device_add(&codec->dev);
1385 
1386 	if (err < 0)
1387 		return err;
1388 	snd_hda_register_beep_device(codec);
1389 	return 0;
1390 }
1391 
snd_hda_codec_dev_disconnect(struct snd_device * device)1392 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1393 {
1394 	struct hda_codec *codec = device->device_data;
1395 
1396 	snd_hda_detach_beep_device(codec);
1397 	device_del(&codec->dev);
1398 	return 0;
1399 }
1400 
snd_hda_codec_dev_free(struct snd_device * device)1401 static int snd_hda_codec_dev_free(struct snd_device *device)
1402 {
1403 	snd_hda_codec_free(device->device_data);
1404 	return 0;
1405 }
1406 
1407 /* just free the container */
snd_hda_codec_dev_release(struct device * dev)1408 static void snd_hda_codec_dev_release(struct device *dev)
1409 {
1410 	kfree(container_of(dev, struct hda_codec, dev));
1411 }
1412 
1413 /**
1414  * snd_hda_codec_new - create a HDA codec
1415  * @bus: the bus to assign
1416  * @codec_addr: the codec address
1417  * @codecp: the pointer to store the generated codec
1418  *
1419  * Returns 0 if successful, or a negative error code.
1420  */
snd_hda_codec_new(struct hda_bus * bus,unsigned int codec_addr,struct hda_codec ** codecp)1421 int snd_hda_codec_new(struct hda_bus *bus,
1422 				unsigned int codec_addr,
1423 				struct hda_codec **codecp)
1424 {
1425 	struct hda_codec *codec;
1426 	char component[31];
1427 	hda_nid_t fg;
1428 	int err;
1429 	static struct snd_device_ops dev_ops = {
1430 		.dev_register = snd_hda_codec_dev_register,
1431 		.dev_disconnect = snd_hda_codec_dev_disconnect,
1432 		.dev_free = snd_hda_codec_dev_free,
1433 	};
1434 
1435 	if (snd_BUG_ON(!bus))
1436 		return -EINVAL;
1437 	if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1438 		return -EINVAL;
1439 
1440 	if (bus->caddr_tbl[codec_addr]) {
1441 		dev_err(bus->card->dev,
1442 			"address 0x%x is already occupied\n",
1443 			codec_addr);
1444 		return -EBUSY;
1445 	}
1446 
1447 	codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1448 	if (codec == NULL) {
1449 		dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
1450 		return -ENOMEM;
1451 	}
1452 
1453 	device_initialize(&codec->dev);
1454 	codec->dev.parent = &bus->card->card_dev;
1455 	codec->dev.class = sound_class;
1456 	codec->dev.release = snd_hda_codec_dev_release;
1457 	codec->dev.groups = snd_hda_dev_attr_groups;
1458 	dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1459 		     codec_addr);
1460 	dev_set_drvdata(&codec->dev, codec); /* for sysfs */
1461 
1462 	codec->bus = bus;
1463 	codec->addr = codec_addr;
1464 	mutex_init(&codec->spdif_mutex);
1465 	mutex_init(&codec->control_mutex);
1466 	mutex_init(&codec->hash_mutex);
1467 	init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1468 	init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1469 	snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1470 	snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1471 	snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1472 	snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1473 	snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1474 	snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1475 	snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1476 	snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1477 	INIT_LIST_HEAD(&codec->conn_list);
1478 
1479 	INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1480 	codec->depop_delay = -1;
1481 	codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
1482 
1483 #ifdef CONFIG_PM
1484 	spin_lock_init(&codec->power_lock);
1485 	INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1486 	/* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1487 	 * the caller has to power down appropriatley after initialization
1488 	 * phase.
1489 	 */
1490 	hda_keep_power_on(codec);
1491 #endif
1492 
1493 	snd_hda_sysfs_init(codec);
1494 
1495 	if (codec->bus->modelname) {
1496 		codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1497 		if (!codec->modelname) {
1498 			err = -ENODEV;
1499 			goto error;
1500 		}
1501 	}
1502 
1503 	list_add_tail(&codec->list, &bus->codec_list);
1504 	bus->num_codecs++;
1505 
1506 	bus->caddr_tbl[codec_addr] = codec;
1507 
1508 	codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1509 					      AC_PAR_VENDOR_ID);
1510 	if (codec->vendor_id == -1)
1511 		/* read again, hopefully the access method was corrected
1512 		 * in the last read...
1513 		 */
1514 		codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1515 						      AC_PAR_VENDOR_ID);
1516 	codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1517 						 AC_PAR_SUBSYSTEM_ID);
1518 	codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1519 						AC_PAR_REV_ID);
1520 
1521 	setup_fg_nodes(codec);
1522 	if (!codec->afg && !codec->mfg) {
1523 		dev_err(bus->card->dev, "no AFG or MFG node found\n");
1524 		err = -ENODEV;
1525 		goto error;
1526 	}
1527 
1528 	fg = codec->afg ? codec->afg : codec->mfg;
1529 	err = read_widget_caps(codec, fg);
1530 	if (err < 0) {
1531 		dev_err(bus->card->dev, "cannot malloc\n");
1532 		goto error;
1533 	}
1534 	err = read_pin_defaults(codec);
1535 	if (err < 0)
1536 		goto error;
1537 
1538 	if (!codec->subsystem_id) {
1539 		codec->subsystem_id =
1540 			snd_hda_codec_read(codec, fg, 0,
1541 					   AC_VERB_GET_SUBSYSTEM_ID, 0);
1542 	}
1543 
1544 #ifdef CONFIG_PM
1545 	codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1546 					AC_PWRST_CLKSTOP);
1547 #endif
1548 	codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1549 					AC_PWRST_EPSS);
1550 #ifdef CONFIG_PM
1551 	if (!codec->d3_stop_clk || !codec->epss)
1552 		bus->power_keep_link_on = 1;
1553 #endif
1554 
1555 
1556 	/* power-up all before initialization */
1557 	hda_set_power_state(codec, AC_PWRST_D0);
1558 
1559 	snd_hda_codec_proc_new(codec);
1560 
1561 	snd_hda_create_hwdep(codec);
1562 
1563 	sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1564 		codec->subsystem_id, codec->revision_id);
1565 	snd_component_add(codec->bus->card, component);
1566 
1567 	err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1568 	if (err < 0)
1569 		goto error;
1570 
1571 	if (codecp)
1572 		*codecp = codec;
1573 	return 0;
1574 
1575  error:
1576 	snd_hda_codec_free(codec);
1577 	return err;
1578 }
1579 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1580 
snd_hda_codec_update_widgets(struct hda_codec * codec)1581 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1582 {
1583 	hda_nid_t fg;
1584 	int err;
1585 
1586 	/* Assume the function group node does not change,
1587 	 * only the widget nodes may change.
1588 	 */
1589 	kfree(codec->wcaps);
1590 	fg = codec->afg ? codec->afg : codec->mfg;
1591 	err = read_widget_caps(codec, fg);
1592 	if (err < 0) {
1593 		codec_err(codec, "cannot malloc\n");
1594 		return err;
1595 	}
1596 
1597 	snd_array_free(&codec->init_pins);
1598 	err = read_pin_defaults(codec);
1599 
1600 	return err;
1601 }
1602 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1603 
1604 
1605 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
1606 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
is_likely_hdmi_codec(struct hda_codec * codec)1607 static bool is_likely_hdmi_codec(struct hda_codec *codec)
1608 {
1609 	hda_nid_t nid = codec->start_nid;
1610 	int i;
1611 
1612 	for (i = 0; i < codec->num_nodes; i++, nid++) {
1613 		unsigned int wcaps = get_wcaps(codec, nid);
1614 		switch (get_wcaps_type(wcaps)) {
1615 		case AC_WID_AUD_IN:
1616 			return false; /* HDMI parser supports only HDMI out */
1617 		case AC_WID_AUD_OUT:
1618 			if (!(wcaps & AC_WCAP_DIGITAL))
1619 				return false;
1620 			break;
1621 		}
1622 	}
1623 	return true;
1624 }
1625 #else
1626 /* no HDMI codec parser support */
1627 #define is_likely_hdmi_codec(codec)	false
1628 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
1629 
1630 /**
1631  * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1632  * @codec: the HDA codec
1633  *
1634  * Start parsing of the given codec tree and (re-)initialize the whole
1635  * patch instance.
1636  *
1637  * Returns 0 if successful or a negative error code.
1638  */
snd_hda_codec_configure(struct hda_codec * codec)1639 int snd_hda_codec_configure(struct hda_codec *codec)
1640 {
1641 	int (*patch)(struct hda_codec *) = NULL;
1642 	int err;
1643 
1644 	codec->preset = find_codec_preset(codec);
1645 	if (!codec->vendor_name || !codec->chip_name) {
1646 		err = get_codec_name(codec);
1647 		if (err < 0)
1648 			return err;
1649 	}
1650 
1651 	if (!is_generic_config(codec) && codec->preset)
1652 		patch = codec->preset->patch;
1653 	if (!patch) {
1654 		unload_parser(codec); /* to be sure */
1655 		if (is_likely_hdmi_codec(codec)) {
1656 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
1657 			patch = load_parser(codec, snd_hda_parse_hdmi_codec);
1658 #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1659 			patch = snd_hda_parse_hdmi_codec;
1660 #endif
1661 		}
1662 		if (!patch) {
1663 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1664 			patch = load_parser(codec, snd_hda_parse_generic_codec);
1665 #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1666 			patch = snd_hda_parse_generic_codec;
1667 #endif
1668 		}
1669 		if (!patch) {
1670 			codec_err(codec, "No codec parser is available\n");
1671 			return -ENODEV;
1672 		}
1673 	}
1674 
1675 	err = patch(codec);
1676 	if (err < 0) {
1677 		unload_parser(codec);
1678 		return err;
1679 	}
1680 
1681 	if (codec->patch_ops.unsol_event) {
1682 		err = init_unsol_queue(codec->bus);
1683 		if (err < 0)
1684 			return err;
1685 	}
1686 
1687 	/* audio codec should override the mixer name */
1688 	if (codec->afg || !*codec->bus->card->mixername)
1689 		snprintf(codec->bus->card->mixername,
1690 			 sizeof(codec->bus->card->mixername),
1691 			 "%s %s", codec->vendor_name, codec->chip_name);
1692 	return 0;
1693 }
1694 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
1695 
1696 /* update the stream-id if changed */
update_pcm_stream_id(struct hda_codec * codec,struct hda_cvt_setup * p,hda_nid_t nid,u32 stream_tag,int channel_id)1697 static void update_pcm_stream_id(struct hda_codec *codec,
1698 				 struct hda_cvt_setup *p, hda_nid_t nid,
1699 				 u32 stream_tag, int channel_id)
1700 {
1701 	unsigned int oldval, newval;
1702 
1703 	if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1704 		oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1705 		newval = (stream_tag << 4) | channel_id;
1706 		if (oldval != newval)
1707 			snd_hda_codec_write(codec, nid, 0,
1708 					    AC_VERB_SET_CHANNEL_STREAMID,
1709 					    newval);
1710 		p->stream_tag = stream_tag;
1711 		p->channel_id = channel_id;
1712 	}
1713 }
1714 
1715 /* update the format-id if changed */
update_pcm_format(struct hda_codec * codec,struct hda_cvt_setup * p,hda_nid_t nid,int format)1716 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1717 			      hda_nid_t nid, int format)
1718 {
1719 	unsigned int oldval;
1720 
1721 	if (p->format_id != format) {
1722 		oldval = snd_hda_codec_read(codec, nid, 0,
1723 					    AC_VERB_GET_STREAM_FORMAT, 0);
1724 		if (oldval != format) {
1725 			msleep(1);
1726 			snd_hda_codec_write(codec, nid, 0,
1727 					    AC_VERB_SET_STREAM_FORMAT,
1728 					    format);
1729 		}
1730 		p->format_id = format;
1731 	}
1732 }
1733 
1734 /**
1735  * snd_hda_codec_setup_stream - set up the codec for streaming
1736  * @codec: the CODEC to set up
1737  * @nid: the NID to set up
1738  * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1739  * @channel_id: channel id to pass, zero based.
1740  * @format: stream format.
1741  */
snd_hda_codec_setup_stream(struct hda_codec * codec,hda_nid_t nid,u32 stream_tag,int channel_id,int format)1742 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1743 				u32 stream_tag,
1744 				int channel_id, int format)
1745 {
1746 	struct hda_codec *c;
1747 	struct hda_cvt_setup *p;
1748 	int type;
1749 	int i;
1750 
1751 	if (!nid)
1752 		return;
1753 
1754 	codec_dbg(codec,
1755 		  "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1756 		  nid, stream_tag, channel_id, format);
1757 	p = get_hda_cvt_setup(codec, nid);
1758 	if (!p)
1759 		return;
1760 
1761 	if (codec->pcm_format_first)
1762 		update_pcm_format(codec, p, nid, format);
1763 	update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1764 	if (!codec->pcm_format_first)
1765 		update_pcm_format(codec, p, nid, format);
1766 
1767 	p->active = 1;
1768 	p->dirty = 0;
1769 
1770 	/* make other inactive cvts with the same stream-tag dirty */
1771 	type = get_wcaps_type(get_wcaps(codec, nid));
1772 	list_for_each_entry(c, &codec->bus->codec_list, list) {
1773 		for (i = 0; i < c->cvt_setups.used; i++) {
1774 			p = snd_array_elem(&c->cvt_setups, i);
1775 			if (!p->active && p->stream_tag == stream_tag &&
1776 			    get_wcaps_type(get_wcaps(c, p->nid)) == type)
1777 				p->dirty = 1;
1778 		}
1779 	}
1780 }
1781 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1782 
1783 static void really_cleanup_stream(struct hda_codec *codec,
1784 				  struct hda_cvt_setup *q);
1785 
1786 /**
1787  * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1788  * @codec: the CODEC to clean up
1789  * @nid: the NID to clean up
1790  * @do_now: really clean up the stream instead of clearing the active flag
1791  */
__snd_hda_codec_cleanup_stream(struct hda_codec * codec,hda_nid_t nid,int do_now)1792 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1793 				    int do_now)
1794 {
1795 	struct hda_cvt_setup *p;
1796 
1797 	if (!nid)
1798 		return;
1799 
1800 	if (codec->no_sticky_stream)
1801 		do_now = 1;
1802 
1803 	codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1804 	p = get_hda_cvt_setup(codec, nid);
1805 	if (p) {
1806 		/* here we just clear the active flag when do_now isn't set;
1807 		 * actual clean-ups will be done later in
1808 		 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1809 		 */
1810 		if (do_now)
1811 			really_cleanup_stream(codec, p);
1812 		else
1813 			p->active = 0;
1814 	}
1815 }
1816 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1817 
really_cleanup_stream(struct hda_codec * codec,struct hda_cvt_setup * q)1818 static void really_cleanup_stream(struct hda_codec *codec,
1819 				  struct hda_cvt_setup *q)
1820 {
1821 	hda_nid_t nid = q->nid;
1822 	if (q->stream_tag || q->channel_id)
1823 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1824 	if (q->format_id)
1825 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1826 );
1827 	memset(q, 0, sizeof(*q));
1828 	q->nid = nid;
1829 }
1830 
1831 /* clean up the all conflicting obsolete streams */
purify_inactive_streams(struct hda_codec * codec)1832 static void purify_inactive_streams(struct hda_codec *codec)
1833 {
1834 	struct hda_codec *c;
1835 	int i;
1836 
1837 	list_for_each_entry(c, &codec->bus->codec_list, list) {
1838 		for (i = 0; i < c->cvt_setups.used; i++) {
1839 			struct hda_cvt_setup *p;
1840 			p = snd_array_elem(&c->cvt_setups, i);
1841 			if (p->dirty)
1842 				really_cleanup_stream(c, p);
1843 		}
1844 	}
1845 }
1846 
1847 #ifdef CONFIG_PM
1848 /* clean up all streams; called from suspend */
hda_cleanup_all_streams(struct hda_codec * codec)1849 static void hda_cleanup_all_streams(struct hda_codec *codec)
1850 {
1851 	int i;
1852 
1853 	for (i = 0; i < codec->cvt_setups.used; i++) {
1854 		struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1855 		if (p->stream_tag)
1856 			really_cleanup_stream(codec, p);
1857 	}
1858 }
1859 #endif
1860 
1861 /*
1862  * amp access functions
1863  */
1864 
1865 /* FIXME: more better hash key? */
1866 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1867 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1868 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1869 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1870 #define INFO_AMP_CAPS	(1<<0)
1871 #define INFO_AMP_VOL(ch)	(1 << (1 + (ch)))
1872 
1873 /* initialize the hash table */
init_hda_cache(struct hda_cache_rec * cache,unsigned int record_size)1874 static void init_hda_cache(struct hda_cache_rec *cache,
1875 				     unsigned int record_size)
1876 {
1877 	memset(cache, 0, sizeof(*cache));
1878 	memset(cache->hash, 0xff, sizeof(cache->hash));
1879 	snd_array_init(&cache->buf, record_size, 64);
1880 }
1881 
free_hda_cache(struct hda_cache_rec * cache)1882 static void free_hda_cache(struct hda_cache_rec *cache)
1883 {
1884 	snd_array_free(&cache->buf);
1885 }
1886 
1887 /* query the hash.  allocate an entry if not found. */
get_hash(struct hda_cache_rec * cache,u32 key)1888 static struct hda_cache_head  *get_hash(struct hda_cache_rec *cache, u32 key)
1889 {
1890 	u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1891 	u16 cur = cache->hash[idx];
1892 	struct hda_cache_head *info;
1893 
1894 	while (cur != 0xffff) {
1895 		info = snd_array_elem(&cache->buf, cur);
1896 		if (info->key == key)
1897 			return info;
1898 		cur = info->next;
1899 	}
1900 	return NULL;
1901 }
1902 
1903 /* query the hash.  allocate an entry if not found. */
get_alloc_hash(struct hda_cache_rec * cache,u32 key)1904 static struct hda_cache_head  *get_alloc_hash(struct hda_cache_rec *cache,
1905 					      u32 key)
1906 {
1907 	struct hda_cache_head *info = get_hash(cache, key);
1908 	if (!info) {
1909 		u16 idx, cur;
1910 		/* add a new hash entry */
1911 		info = snd_array_new(&cache->buf);
1912 		if (!info)
1913 			return NULL;
1914 		cur = snd_array_index(&cache->buf, info);
1915 		info->key = key;
1916 		info->val = 0;
1917 		info->dirty = 0;
1918 		idx = key % (u16)ARRAY_SIZE(cache->hash);
1919 		info->next = cache->hash[idx];
1920 		cache->hash[idx] = cur;
1921 	}
1922 	return info;
1923 }
1924 
1925 /* query and allocate an amp hash entry */
1926 static inline struct hda_amp_info *
get_alloc_amp_hash(struct hda_codec * codec,u32 key)1927 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1928 {
1929 	return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1930 }
1931 
1932 /* overwrite the value with the key in the caps hash */
write_caps_hash(struct hda_codec * codec,u32 key,unsigned int val)1933 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1934 {
1935 	struct hda_amp_info *info;
1936 
1937 	mutex_lock(&codec->hash_mutex);
1938 	info = get_alloc_amp_hash(codec, key);
1939 	if (!info) {
1940 		mutex_unlock(&codec->hash_mutex);
1941 		return -EINVAL;
1942 	}
1943 	info->amp_caps = val;
1944 	info->head.val |= INFO_AMP_CAPS;
1945 	mutex_unlock(&codec->hash_mutex);
1946 	return 0;
1947 }
1948 
1949 /* query the value from the caps hash; if not found, fetch the current
1950  * value from the given function and store in the hash
1951  */
1952 static unsigned int
query_caps_hash(struct hda_codec * codec,hda_nid_t nid,int dir,u32 key,unsigned int (* func)(struct hda_codec *,hda_nid_t,int))1953 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1954 		unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1955 {
1956 	struct hda_amp_info *info;
1957 	unsigned int val;
1958 
1959 	mutex_lock(&codec->hash_mutex);
1960 	info = get_alloc_amp_hash(codec, key);
1961 	if (!info) {
1962 		mutex_unlock(&codec->hash_mutex);
1963 		return 0;
1964 	}
1965 	if (!(info->head.val & INFO_AMP_CAPS)) {
1966 		mutex_unlock(&codec->hash_mutex); /* for reentrance */
1967 		val = func(codec, nid, dir);
1968 		write_caps_hash(codec, key, val);
1969 	} else {
1970 		val = info->amp_caps;
1971 		mutex_unlock(&codec->hash_mutex);
1972 	}
1973 	return val;
1974 }
1975 
read_amp_cap(struct hda_codec * codec,hda_nid_t nid,int direction)1976 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1977 				 int direction)
1978 {
1979 	if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1980 		nid = codec->afg;
1981 	return snd_hda_param_read(codec, nid,
1982 				  direction == HDA_OUTPUT ?
1983 				  AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1984 }
1985 
1986 /**
1987  * query_amp_caps - query AMP capabilities
1988  * @codec: the HD-auio codec
1989  * @nid: the NID to query
1990  * @direction: either #HDA_INPUT or #HDA_OUTPUT
1991  *
1992  * Query AMP capabilities for the given widget and direction.
1993  * Returns the obtained capability bits.
1994  *
1995  * When cap bits have been already read, this doesn't read again but
1996  * returns the cached value.
1997  */
query_amp_caps(struct hda_codec * codec,hda_nid_t nid,int direction)1998 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1999 {
2000 	return query_caps_hash(codec, nid, direction,
2001 			       HDA_HASH_KEY(nid, direction, 0),
2002 			       read_amp_cap);
2003 }
2004 EXPORT_SYMBOL_GPL(query_amp_caps);
2005 
2006 /**
2007  * snd_hda_check_amp_caps - query AMP capabilities
2008  * @codec: the HD-audio codec
2009  * @nid: the NID to query
2010  * @dir: either #HDA_INPUT or #HDA_OUTPUT
2011  *
2012  * Check whether the widget has the given amp capability for the direction.
2013  */
snd_hda_check_amp_caps(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int bits)2014 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
2015 			   int dir, unsigned int bits)
2016 {
2017 	if (!nid)
2018 		return false;
2019 	if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2020 		if (query_amp_caps(codec, nid, dir) & bits)
2021 			return true;
2022 	return false;
2023 }
2024 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2025 
2026 /**
2027  * snd_hda_override_amp_caps - Override the AMP capabilities
2028  * @codec: the CODEC to clean up
2029  * @nid: the NID to clean up
2030  * @direction: either #HDA_INPUT or #HDA_OUTPUT
2031  * @caps: the capability bits to set
2032  *
2033  * Override the cached AMP caps bits value by the given one.
2034  * This function is useful if the driver needs to adjust the AMP ranges,
2035  * e.g. limit to 0dB, etc.
2036  *
2037  * Returns zero if successful or a negative error code.
2038  */
snd_hda_override_amp_caps(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int caps)2039 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2040 			      unsigned int caps)
2041 {
2042 	return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
2043 }
2044 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
2045 
read_pin_cap(struct hda_codec * codec,hda_nid_t nid,int dir)2046 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2047 				 int dir)
2048 {
2049 	return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2050 }
2051 
2052 /**
2053  * snd_hda_query_pin_caps - Query PIN capabilities
2054  * @codec: the HD-auio codec
2055  * @nid: the NID to query
2056  *
2057  * Query PIN capabilities for the given widget.
2058  * Returns the obtained capability bits.
2059  *
2060  * When cap bits have been already read, this doesn't read again but
2061  * returns the cached value.
2062  */
snd_hda_query_pin_caps(struct hda_codec * codec,hda_nid_t nid)2063 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2064 {
2065 	return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
2066 			       read_pin_cap);
2067 }
2068 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
2069 
2070 /**
2071  * snd_hda_override_pin_caps - Override the pin capabilities
2072  * @codec: the CODEC
2073  * @nid: the NID to override
2074  * @caps: the capability bits to set
2075  *
2076  * Override the cached PIN capabilitiy bits value by the given one.
2077  *
2078  * Returns zero if successful or a negative error code.
2079  */
snd_hda_override_pin_caps(struct hda_codec * codec,hda_nid_t nid,unsigned int caps)2080 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2081 			      unsigned int caps)
2082 {
2083 	return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
2084 }
2085 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
2086 
2087 /* read or sync the hash value with the current value;
2088  * call within hash_mutex
2089  */
2090 static struct hda_amp_info *
update_amp_hash(struct hda_codec * codec,hda_nid_t nid,int ch,int direction,int index,bool init_only)2091 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
2092 		int direction, int index, bool init_only)
2093 {
2094 	struct hda_amp_info *info;
2095 	unsigned int parm, val = 0;
2096 	bool val_read = false;
2097 
2098  retry:
2099 	info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2100 	if (!info)
2101 		return NULL;
2102 	if (!(info->head.val & INFO_AMP_VOL(ch))) {
2103 		if (!val_read) {
2104 			mutex_unlock(&codec->hash_mutex);
2105 			parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2106 			parm |= direction == HDA_OUTPUT ?
2107 				AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2108 			parm |= index;
2109 			val = snd_hda_codec_read(codec, nid, 0,
2110 				 AC_VERB_GET_AMP_GAIN_MUTE, parm);
2111 			val &= 0xff;
2112 			val_read = true;
2113 			mutex_lock(&codec->hash_mutex);
2114 			goto retry;
2115 		}
2116 		info->vol[ch] = val;
2117 		info->head.val |= INFO_AMP_VOL(ch);
2118 	} else if (init_only)
2119 		return NULL;
2120 	return info;
2121 }
2122 
2123 /*
2124  * write the current volume in info to the h/w
2125  */
put_vol_mute(struct hda_codec * codec,unsigned int amp_caps,hda_nid_t nid,int ch,int direction,int index,int val)2126 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
2127 			 hda_nid_t nid, int ch, int direction, int index,
2128 			 int val)
2129 {
2130 	u32 parm;
2131 
2132 	parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2133 	parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2134 	parm |= index << AC_AMP_SET_INDEX_SHIFT;
2135 	if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2136 	    (amp_caps & AC_AMPCAP_MIN_MUTE))
2137 		; /* set the zero value as a fake mute */
2138 	else
2139 		parm |= val;
2140 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2141 }
2142 
2143 /* meta hook to call each driver's vmaster hook */
vmaster_hook(void * private_data,int enabled)2144 static void vmaster_hook(void *private_data, int enabled)
2145 {
2146 	struct hda_vmaster_mute_hook *hook = private_data;
2147 
2148 	if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2149 		enabled = hook->mute_mode;
2150 	hook->hook(hook->codec, enabled);
2151 }
2152 
2153 /**
2154  * snd_hda_codec_amp_read - Read AMP value
2155  * @codec: HD-audio codec
2156  * @nid: NID to read the AMP value
2157  * @ch: channel (left=0 or right=1)
2158  * @direction: #HDA_INPUT or #HDA_OUTPUT
2159  * @index: the index value (only for input direction)
2160  *
2161  * Read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
2162  */
snd_hda_codec_amp_read(struct hda_codec * codec,hda_nid_t nid,int ch,int direction,int index)2163 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2164 			   int direction, int index)
2165 {
2166 	struct hda_amp_info *info;
2167 	unsigned int val = 0;
2168 
2169 	mutex_lock(&codec->hash_mutex);
2170 	info = update_amp_hash(codec, nid, ch, direction, index, false);
2171 	if (info)
2172 		val = info->vol[ch];
2173 	mutex_unlock(&codec->hash_mutex);
2174 	return val;
2175 }
2176 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
2177 
codec_amp_update(struct hda_codec * codec,hda_nid_t nid,int ch,int direction,int idx,int mask,int val,bool init_only)2178 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2179 			    int direction, int idx, int mask, int val,
2180 			    bool init_only)
2181 {
2182 	struct hda_amp_info *info;
2183 	unsigned int caps;
2184 	unsigned int cache_only;
2185 
2186 	if (snd_BUG_ON(mask & ~0xff))
2187 		mask &= 0xff;
2188 	val &= mask;
2189 
2190 	mutex_lock(&codec->hash_mutex);
2191 	info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2192 	if (!info) {
2193 		mutex_unlock(&codec->hash_mutex);
2194 		return 0;
2195 	}
2196 	val |= info->vol[ch] & ~mask;
2197 	if (info->vol[ch] == val) {
2198 		mutex_unlock(&codec->hash_mutex);
2199 		return 0;
2200 	}
2201 	info->vol[ch] = val;
2202 	cache_only = info->head.dirty = codec->cached_write;
2203 	caps = info->amp_caps;
2204 	mutex_unlock(&codec->hash_mutex);
2205 	if (!cache_only)
2206 		put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2207 	return 1;
2208 }
2209 
2210 /**
2211  * snd_hda_codec_amp_update - update the AMP value
2212  * @codec: HD-audio codec
2213  * @nid: NID to read the AMP value
2214  * @ch: channel (left=0 or right=1)
2215  * @direction: #HDA_INPUT or #HDA_OUTPUT
2216  * @idx: the index value (only for input direction)
2217  * @mask: bit mask to set
2218  * @val: the bits value to set
2219  *
2220  * Update the AMP value with a bit mask.
2221  * Returns 0 if the value is unchanged, 1 if changed.
2222  */
snd_hda_codec_amp_update(struct hda_codec * codec,hda_nid_t nid,int ch,int direction,int idx,int mask,int val)2223 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2224 			     int direction, int idx, int mask, int val)
2225 {
2226 	return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2227 }
2228 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
2229 
2230 /**
2231  * snd_hda_codec_amp_stereo - update the AMP stereo values
2232  * @codec: HD-audio codec
2233  * @nid: NID to read the AMP value
2234  * @direction: #HDA_INPUT or #HDA_OUTPUT
2235  * @idx: the index value (only for input direction)
2236  * @mask: bit mask to set
2237  * @val: the bits value to set
2238  *
2239  * Update the AMP values like snd_hda_codec_amp_update(), but for a
2240  * stereo widget with the same mask and value.
2241  */
snd_hda_codec_amp_stereo(struct hda_codec * codec,hda_nid_t nid,int direction,int idx,int mask,int val)2242 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2243 			     int direction, int idx, int mask, int val)
2244 {
2245 	int ch, ret = 0;
2246 
2247 	if (snd_BUG_ON(mask & ~0xff))
2248 		mask &= 0xff;
2249 	for (ch = 0; ch < 2; ch++)
2250 		ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2251 						idx, mask, val);
2252 	return ret;
2253 }
2254 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
2255 
2256 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2257  * the first access.  If the amp was already initialized / updated beforehand,
2258  * this does nothing.
2259  */
snd_hda_codec_amp_init(struct hda_codec * codec,hda_nid_t nid,int ch,int dir,int idx,int mask,int val)2260 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2261 			   int dir, int idx, int mask, int val)
2262 {
2263 	return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2264 }
2265 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
2266 
snd_hda_codec_amp_init_stereo(struct hda_codec * codec,hda_nid_t nid,int dir,int idx,int mask,int val)2267 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2268 				  int dir, int idx, int mask, int val)
2269 {
2270 	int ch, ret = 0;
2271 
2272 	if (snd_BUG_ON(mask & ~0xff))
2273 		mask &= 0xff;
2274 	for (ch = 0; ch < 2; ch++)
2275 		ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2276 					      idx, mask, val);
2277 	return ret;
2278 }
2279 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
2280 
2281 /**
2282  * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2283  * @codec: HD-audio codec
2284  *
2285  * Resume the all amp commands from the cache.
2286  */
snd_hda_codec_resume_amp(struct hda_codec * codec)2287 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2288 {
2289 	int i;
2290 
2291 	mutex_lock(&codec->hash_mutex);
2292 	codec->cached_write = 0;
2293 	for (i = 0; i < codec->amp_cache.buf.used; i++) {
2294 		struct hda_amp_info *buffer;
2295 		u32 key;
2296 		hda_nid_t nid;
2297 		unsigned int idx, dir, ch;
2298 		struct hda_amp_info info;
2299 
2300 		buffer = snd_array_elem(&codec->amp_cache.buf, i);
2301 		if (!buffer->head.dirty)
2302 			continue;
2303 		buffer->head.dirty = 0;
2304 		info = *buffer;
2305 		key = info.head.key;
2306 		if (!key)
2307 			continue;
2308 		nid = key & 0xff;
2309 		idx = (key >> 16) & 0xff;
2310 		dir = (key >> 24) & 0xff;
2311 		for (ch = 0; ch < 2; ch++) {
2312 			if (!(info.head.val & INFO_AMP_VOL(ch)))
2313 				continue;
2314 			mutex_unlock(&codec->hash_mutex);
2315 			put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2316 				     info.vol[ch]);
2317 			mutex_lock(&codec->hash_mutex);
2318 		}
2319 	}
2320 	mutex_unlock(&codec->hash_mutex);
2321 }
2322 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
2323 
get_amp_max_value(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int ofs)2324 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2325 			     unsigned int ofs)
2326 {
2327 	u32 caps = query_amp_caps(codec, nid, dir);
2328 	/* get num steps */
2329 	caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2330 	if (ofs < caps)
2331 		caps -= ofs;
2332 	return caps;
2333 }
2334 
2335 /**
2336  * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2337  *
2338  * The control element is supposed to have the private_value field
2339  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2340  */
snd_hda_mixer_amp_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2341 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2342 				  struct snd_ctl_elem_info *uinfo)
2343 {
2344 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2345 	u16 nid = get_amp_nid(kcontrol);
2346 	u8 chs = get_amp_channels(kcontrol);
2347 	int dir = get_amp_direction(kcontrol);
2348 	unsigned int ofs = get_amp_offset(kcontrol);
2349 
2350 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2351 	uinfo->count = chs == 3 ? 2 : 1;
2352 	uinfo->value.integer.min = 0;
2353 	uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2354 	if (!uinfo->value.integer.max) {
2355 		codec_warn(codec,
2356 			   "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2357 			   nid, kcontrol->id.name);
2358 		return -EINVAL;
2359 	}
2360 	return 0;
2361 }
2362 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
2363 
2364 
2365 static inline unsigned int
read_amp_value(struct hda_codec * codec,hda_nid_t nid,int ch,int dir,int idx,unsigned int ofs)2366 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2367 	       int ch, int dir, int idx, unsigned int ofs)
2368 {
2369 	unsigned int val;
2370 	val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2371 	val &= HDA_AMP_VOLMASK;
2372 	if (val >= ofs)
2373 		val -= ofs;
2374 	else
2375 		val = 0;
2376 	return val;
2377 }
2378 
2379 static inline int
update_amp_value(struct hda_codec * codec,hda_nid_t nid,int ch,int dir,int idx,unsigned int ofs,unsigned int val)2380 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2381 		 int ch, int dir, int idx, unsigned int ofs,
2382 		 unsigned int val)
2383 {
2384 	unsigned int maxval;
2385 
2386 	if (val > 0)
2387 		val += ofs;
2388 	/* ofs = 0: raw max value */
2389 	maxval = get_amp_max_value(codec, nid, dir, 0);
2390 	if (val > maxval)
2391 		val = maxval;
2392 	return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2393 					HDA_AMP_VOLMASK, val);
2394 }
2395 
2396 /**
2397  * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2398  *
2399  * The control element is supposed to have the private_value field
2400  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2401  */
snd_hda_mixer_amp_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2402 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2403 				 struct snd_ctl_elem_value *ucontrol)
2404 {
2405 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2406 	hda_nid_t nid = get_amp_nid(kcontrol);
2407 	int chs = get_amp_channels(kcontrol);
2408 	int dir = get_amp_direction(kcontrol);
2409 	int idx = get_amp_index(kcontrol);
2410 	unsigned int ofs = get_amp_offset(kcontrol);
2411 	long *valp = ucontrol->value.integer.value;
2412 
2413 	if (chs & 1)
2414 		*valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2415 	if (chs & 2)
2416 		*valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2417 	return 0;
2418 }
2419 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
2420 
2421 /**
2422  * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2423  *
2424  * The control element is supposed to have the private_value field
2425  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2426  */
snd_hda_mixer_amp_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2427 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2428 				 struct snd_ctl_elem_value *ucontrol)
2429 {
2430 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2431 	hda_nid_t nid = get_amp_nid(kcontrol);
2432 	int chs = get_amp_channels(kcontrol);
2433 	int dir = get_amp_direction(kcontrol);
2434 	int idx = get_amp_index(kcontrol);
2435 	unsigned int ofs = get_amp_offset(kcontrol);
2436 	long *valp = ucontrol->value.integer.value;
2437 	int change = 0;
2438 
2439 	snd_hda_power_up(codec);
2440 	if (chs & 1) {
2441 		change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2442 		valp++;
2443 	}
2444 	if (chs & 2)
2445 		change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2446 	snd_hda_power_down(codec);
2447 	return change;
2448 }
2449 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
2450 
2451 /**
2452  * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2453  *
2454  * The control element is supposed to have the private_value field
2455  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2456  */
snd_hda_mixer_amp_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * _tlv)2457 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2458 			  unsigned int size, unsigned int __user *_tlv)
2459 {
2460 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2461 	hda_nid_t nid = get_amp_nid(kcontrol);
2462 	int dir = get_amp_direction(kcontrol);
2463 	unsigned int ofs = get_amp_offset(kcontrol);
2464 	bool min_mute = get_amp_min_mute(kcontrol);
2465 	u32 caps, val1, val2;
2466 
2467 	if (size < 4 * sizeof(unsigned int))
2468 		return -ENOMEM;
2469 	caps = query_amp_caps(codec, nid, dir);
2470 	val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2471 	val2 = (val2 + 1) * 25;
2472 	val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2473 	val1 += ofs;
2474 	val1 = ((int)val1) * ((int)val2);
2475 	if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2476 		val2 |= TLV_DB_SCALE_MUTE;
2477 	if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2478 		return -EFAULT;
2479 	if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2480 		return -EFAULT;
2481 	if (put_user(val1, _tlv + 2))
2482 		return -EFAULT;
2483 	if (put_user(val2, _tlv + 3))
2484 		return -EFAULT;
2485 	return 0;
2486 }
2487 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
2488 
2489 /**
2490  * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2491  * @codec: HD-audio codec
2492  * @nid: NID of a reference widget
2493  * @dir: #HDA_INPUT or #HDA_OUTPUT
2494  * @tlv: TLV data to be stored, at least 4 elements
2495  *
2496  * Set (static) TLV data for a virtual master volume using the AMP caps
2497  * obtained from the reference NID.
2498  * The volume range is recalculated as if the max volume is 0dB.
2499  */
snd_hda_set_vmaster_tlv(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int * tlv)2500 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2501 			     unsigned int *tlv)
2502 {
2503 	u32 caps;
2504 	int nums, step;
2505 
2506 	caps = query_amp_caps(codec, nid, dir);
2507 	nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2508 	step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2509 	step = (step + 1) * 25;
2510 	tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2511 	tlv[1] = 2 * sizeof(unsigned int);
2512 	tlv[2] = -nums * step;
2513 	tlv[3] = step;
2514 }
2515 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
2516 
2517 /* find a mixer control element with the given name */
2518 static struct snd_kcontrol *
find_mixer_ctl(struct hda_codec * codec,const char * name,int dev,int idx)2519 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2520 {
2521 	struct snd_ctl_elem_id id;
2522 	memset(&id, 0, sizeof(id));
2523 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2524 	id.device = dev;
2525 	id.index = idx;
2526 	if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2527 		return NULL;
2528 	strcpy(id.name, name);
2529 	return snd_ctl_find_id(codec->bus->card, &id);
2530 }
2531 
2532 /**
2533  * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2534  * @codec: HD-audio codec
2535  * @name: ctl id name string
2536  *
2537  * Get the control element with the given id string and IFACE_MIXER.
2538  */
snd_hda_find_mixer_ctl(struct hda_codec * codec,const char * name)2539 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2540 					    const char *name)
2541 {
2542 	return find_mixer_ctl(codec, name, 0, 0);
2543 }
2544 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
2545 
find_empty_mixer_ctl_idx(struct hda_codec * codec,const char * name,int start_idx)2546 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2547 				    int start_idx)
2548 {
2549 	int i, idx;
2550 	/* 16 ctlrs should be large enough */
2551 	for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2552 		if (!find_mixer_ctl(codec, name, 0, idx))
2553 			return idx;
2554 	}
2555 	return -EBUSY;
2556 }
2557 
2558 /**
2559  * snd_hda_ctl_add - Add a control element and assign to the codec
2560  * @codec: HD-audio codec
2561  * @nid: corresponding NID (optional)
2562  * @kctl: the control element to assign
2563  *
2564  * Add the given control element to an array inside the codec instance.
2565  * All control elements belonging to a codec are supposed to be added
2566  * by this function so that a proper clean-up works at the free or
2567  * reconfiguration time.
2568  *
2569  * If non-zero @nid is passed, the NID is assigned to the control element.
2570  * The assignment is shown in the codec proc file.
2571  *
2572  * snd_hda_ctl_add() checks the control subdev id field whether
2573  * #HDA_SUBDEV_NID_FLAG bit is set.  If set (and @nid is zero), the lower
2574  * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2575  * specifies if kctl->private_value is a HDA amplifier value.
2576  */
snd_hda_ctl_add(struct hda_codec * codec,hda_nid_t nid,struct snd_kcontrol * kctl)2577 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2578 		    struct snd_kcontrol *kctl)
2579 {
2580 	int err;
2581 	unsigned short flags = 0;
2582 	struct hda_nid_item *item;
2583 
2584 	if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2585 		flags |= HDA_NID_ITEM_AMP;
2586 		if (nid == 0)
2587 			nid = get_amp_nid_(kctl->private_value);
2588 	}
2589 	if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2590 		nid = kctl->id.subdevice & 0xffff;
2591 	if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2592 		kctl->id.subdevice = 0;
2593 	err = snd_ctl_add(codec->bus->card, kctl);
2594 	if (err < 0)
2595 		return err;
2596 	item = snd_array_new(&codec->mixers);
2597 	if (!item)
2598 		return -ENOMEM;
2599 	item->kctl = kctl;
2600 	item->nid = nid;
2601 	item->flags = flags;
2602 	return 0;
2603 }
2604 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
2605 
2606 /**
2607  * snd_hda_add_nid - Assign a NID to a control element
2608  * @codec: HD-audio codec
2609  * @nid: corresponding NID (optional)
2610  * @kctl: the control element to assign
2611  * @index: index to kctl
2612  *
2613  * Add the given control element to an array inside the codec instance.
2614  * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2615  * NID:KCTL mapping - for example "Capture Source" selector.
2616  */
snd_hda_add_nid(struct hda_codec * codec,struct snd_kcontrol * kctl,unsigned int index,hda_nid_t nid)2617 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2618 		    unsigned int index, hda_nid_t nid)
2619 {
2620 	struct hda_nid_item *item;
2621 
2622 	if (nid > 0) {
2623 		item = snd_array_new(&codec->nids);
2624 		if (!item)
2625 			return -ENOMEM;
2626 		item->kctl = kctl;
2627 		item->index = index;
2628 		item->nid = nid;
2629 		return 0;
2630 	}
2631 	codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2632 		  kctl->id.name, kctl->id.index, index);
2633 	return -EINVAL;
2634 }
2635 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
2636 
2637 /**
2638  * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2639  * @codec: HD-audio codec
2640  */
snd_hda_ctls_clear(struct hda_codec * codec)2641 void snd_hda_ctls_clear(struct hda_codec *codec)
2642 {
2643 	int i;
2644 	struct hda_nid_item *items = codec->mixers.list;
2645 	for (i = 0; i < codec->mixers.used; i++)
2646 		snd_ctl_remove(codec->bus->card, items[i].kctl);
2647 	snd_array_free(&codec->mixers);
2648 	snd_array_free(&codec->nids);
2649 }
2650 
2651 /* pseudo device locking
2652  * toggle card->shutdown to allow/disallow the device access (as a hack)
2653  */
snd_hda_lock_devices(struct hda_bus * bus)2654 int snd_hda_lock_devices(struct hda_bus *bus)
2655 {
2656 	struct snd_card *card = bus->card;
2657 	struct hda_codec *codec;
2658 
2659 	spin_lock(&card->files_lock);
2660 	if (card->shutdown)
2661 		goto err_unlock;
2662 	card->shutdown = 1;
2663 	if (!list_empty(&card->ctl_files))
2664 		goto err_clear;
2665 
2666 	list_for_each_entry(codec, &bus->codec_list, list) {
2667 		int pcm;
2668 		for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2669 			struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2670 			if (!cpcm->pcm)
2671 				continue;
2672 			if (cpcm->pcm->streams[0].substream_opened ||
2673 			    cpcm->pcm->streams[1].substream_opened)
2674 				goto err_clear;
2675 		}
2676 	}
2677 	spin_unlock(&card->files_lock);
2678 	return 0;
2679 
2680  err_clear:
2681 	card->shutdown = 0;
2682  err_unlock:
2683 	spin_unlock(&card->files_lock);
2684 	return -EINVAL;
2685 }
2686 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
2687 
snd_hda_unlock_devices(struct hda_bus * bus)2688 void snd_hda_unlock_devices(struct hda_bus *bus)
2689 {
2690 	struct snd_card *card = bus->card;
2691 
2692 	card = bus->card;
2693 	spin_lock(&card->files_lock);
2694 	card->shutdown = 0;
2695 	spin_unlock(&card->files_lock);
2696 }
2697 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
2698 
2699 /**
2700  * snd_hda_codec_reset - Clear all objects assigned to the codec
2701  * @codec: HD-audio codec
2702  *
2703  * This frees the all PCM and control elements assigned to the codec, and
2704  * clears the caches and restores the pin default configurations.
2705  *
2706  * When a device is being used, it returns -EBSY.  If successfully freed,
2707  * returns zero.
2708  */
snd_hda_codec_reset(struct hda_codec * codec)2709 int snd_hda_codec_reset(struct hda_codec *codec)
2710 {
2711 	struct hda_bus *bus = codec->bus;
2712 	struct snd_card *card = bus->card;
2713 	int i;
2714 
2715 	if (snd_hda_lock_devices(bus) < 0)
2716 		return -EBUSY;
2717 
2718 	/* OK, let it free */
2719 	cancel_delayed_work_sync(&codec->jackpoll_work);
2720 #ifdef CONFIG_PM
2721 	cancel_delayed_work_sync(&codec->power_work);
2722 	flush_workqueue(bus->workq);
2723 #endif
2724 	snd_hda_ctls_clear(codec);
2725 	/* release PCMs */
2726 	for (i = 0; i < codec->num_pcms; i++) {
2727 		if (codec->pcm_info[i].pcm) {
2728 			snd_device_free(card, codec->pcm_info[i].pcm);
2729 			clear_bit(codec->pcm_info[i].device,
2730 				  bus->pcm_dev_bits);
2731 		}
2732 	}
2733 	snd_hda_detach_beep_device(codec);
2734 	if (codec->patch_ops.free)
2735 		codec->patch_ops.free(codec);
2736 	memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2737 	snd_hda_jack_tbl_clear(codec);
2738 	codec->proc_widget_hook = NULL;
2739 	codec->spec = NULL;
2740 	free_hda_cache(&codec->amp_cache);
2741 	free_hda_cache(&codec->cmd_cache);
2742 	init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2743 	init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2744 	/* free only driver_pins so that init_pins + user_pins are restored */
2745 	snd_array_free(&codec->driver_pins);
2746 	snd_array_free(&codec->cvt_setups);
2747 	snd_array_free(&codec->spdif_out);
2748 	snd_array_free(&codec->verbs);
2749 	codec->num_pcms = 0;
2750 	codec->pcm_info = NULL;
2751 	codec->preset = NULL;
2752 	codec->slave_dig_outs = NULL;
2753 	codec->spdif_status_reset = 0;
2754 	unload_parser(codec);
2755 	module_put(codec->owner);
2756 	codec->owner = NULL;
2757 
2758 	/* allow device access again */
2759 	snd_hda_unlock_devices(bus);
2760 	return 0;
2761 }
2762 
2763 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
2764 
2765 /* apply the function to all matching slave ctls in the mixer list */
map_slaves(struct hda_codec * codec,const char * const * slaves,const char * suffix,map_slave_func_t func,void * data)2766 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2767 		      const char *suffix, map_slave_func_t func, void *data)
2768 {
2769 	struct hda_nid_item *items;
2770 	const char * const *s;
2771 	int i, err;
2772 
2773 	items = codec->mixers.list;
2774 	for (i = 0; i < codec->mixers.used; i++) {
2775 		struct snd_kcontrol *sctl = items[i].kctl;
2776 		if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2777 			continue;
2778 		for (s = slaves; *s; s++) {
2779 			char tmpname[sizeof(sctl->id.name)];
2780 			const char *name = *s;
2781 			if (suffix) {
2782 				snprintf(tmpname, sizeof(tmpname), "%s %s",
2783 					 name, suffix);
2784 				name = tmpname;
2785 			}
2786 			if (!strcmp(sctl->id.name, name)) {
2787 				err = func(codec, data, sctl);
2788 				if (err)
2789 					return err;
2790 				break;
2791 			}
2792 		}
2793 	}
2794 	return 0;
2795 }
2796 
check_slave_present(struct hda_codec * codec,void * data,struct snd_kcontrol * sctl)2797 static int check_slave_present(struct hda_codec *codec,
2798 			       void *data, struct snd_kcontrol *sctl)
2799 {
2800 	return 1;
2801 }
2802 
2803 /* guess the value corresponding to 0dB */
get_kctl_0dB_offset(struct hda_codec * codec,struct snd_kcontrol * kctl,int * step_to_check)2804 static int get_kctl_0dB_offset(struct hda_codec *codec,
2805 			       struct snd_kcontrol *kctl, int *step_to_check)
2806 {
2807 	int _tlv[4];
2808 	const int *tlv = NULL;
2809 	int val = -1;
2810 
2811 	if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2812 		/* FIXME: set_fs() hack for obtaining user-space TLV data */
2813 		mm_segment_t fs = get_fs();
2814 		set_fs(get_ds());
2815 		if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2816 			tlv = _tlv;
2817 		set_fs(fs);
2818 	} else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2819 		tlv = kctl->tlv.p;
2820 	if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2821 		int step = tlv[3];
2822 		step &= ~TLV_DB_SCALE_MUTE;
2823 		if (!step)
2824 			return -1;
2825 		if (*step_to_check && *step_to_check != step) {
2826 			codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2827 				   *step_to_check, step);
2828 			return -1;
2829 		}
2830 		*step_to_check = step;
2831 		val = -tlv[2] / step;
2832 	}
2833 	return val;
2834 }
2835 
2836 /* call kctl->put with the given value(s) */
put_kctl_with_value(struct snd_kcontrol * kctl,int val)2837 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2838 {
2839 	struct snd_ctl_elem_value *ucontrol;
2840 	ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2841 	if (!ucontrol)
2842 		return -ENOMEM;
2843 	ucontrol->value.integer.value[0] = val;
2844 	ucontrol->value.integer.value[1] = val;
2845 	kctl->put(kctl, ucontrol);
2846 	kfree(ucontrol);
2847 	return 0;
2848 }
2849 
2850 /* initialize the slave volume with 0dB */
init_slave_0dB(struct hda_codec * codec,void * data,struct snd_kcontrol * slave)2851 static int init_slave_0dB(struct hda_codec *codec,
2852 			  void *data, struct snd_kcontrol *slave)
2853 {
2854 	int offset = get_kctl_0dB_offset(codec, slave, data);
2855 	if (offset > 0)
2856 		put_kctl_with_value(slave, offset);
2857 	return 0;
2858 }
2859 
2860 /* unmute the slave */
init_slave_unmute(struct hda_codec * codec,void * data,struct snd_kcontrol * slave)2861 static int init_slave_unmute(struct hda_codec *codec,
2862 			     void *data, struct snd_kcontrol *slave)
2863 {
2864 	return put_kctl_with_value(slave, 1);
2865 }
2866 
add_slave(struct hda_codec * codec,void * data,struct snd_kcontrol * slave)2867 static int add_slave(struct hda_codec *codec,
2868 		     void *data, struct snd_kcontrol *slave)
2869 {
2870 	return snd_ctl_add_slave(data, slave);
2871 }
2872 
2873 /**
2874  * snd_hda_add_vmaster - create a virtual master control and add slaves
2875  * @codec: HD-audio codec
2876  * @name: vmaster control name
2877  * @tlv: TLV data (optional)
2878  * @slaves: slave control names (optional)
2879  * @suffix: suffix string to each slave name (optional)
2880  * @init_slave_vol: initialize slaves to unmute/0dB
2881  * @ctl_ret: store the vmaster kcontrol in return
2882  *
2883  * Create a virtual master control with the given name.  The TLV data
2884  * must be either NULL or a valid data.
2885  *
2886  * @slaves is a NULL-terminated array of strings, each of which is a
2887  * slave control name.  All controls with these names are assigned to
2888  * the new virtual master control.
2889  *
2890  * This function returns zero if successful or a negative error code.
2891  */
__snd_hda_add_vmaster(struct hda_codec * codec,char * name,unsigned int * tlv,const char * const * slaves,const char * suffix,bool init_slave_vol,struct snd_kcontrol ** ctl_ret)2892 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2893 			unsigned int *tlv, const char * const *slaves,
2894 			  const char *suffix, bool init_slave_vol,
2895 			  struct snd_kcontrol **ctl_ret)
2896 {
2897 	struct snd_kcontrol *kctl;
2898 	int err;
2899 
2900 	if (ctl_ret)
2901 		*ctl_ret = NULL;
2902 
2903 	err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2904 	if (err != 1) {
2905 		codec_dbg(codec, "No slave found for %s\n", name);
2906 		return 0;
2907 	}
2908 	kctl = snd_ctl_make_virtual_master(name, tlv);
2909 	if (!kctl)
2910 		return -ENOMEM;
2911 	err = snd_hda_ctl_add(codec, 0, kctl);
2912 	if (err < 0)
2913 		return err;
2914 
2915 	err = map_slaves(codec, slaves, suffix, add_slave, kctl);
2916 	if (err < 0)
2917 		return err;
2918 
2919 	/* init with master mute & zero volume */
2920 	put_kctl_with_value(kctl, 0);
2921 	if (init_slave_vol) {
2922 		int step = 0;
2923 		map_slaves(codec, slaves, suffix,
2924 			   tlv ? init_slave_0dB : init_slave_unmute, &step);
2925 	}
2926 
2927 	if (ctl_ret)
2928 		*ctl_ret = kctl;
2929 	return 0;
2930 }
2931 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2932 
2933 /*
2934  * mute-LED control using vmaster
2935  */
vmaster_mute_mode_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2936 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2937 				  struct snd_ctl_elem_info *uinfo)
2938 {
2939 	static const char * const texts[] = {
2940 		"On", "Off", "Follow Master"
2941 	};
2942 	unsigned int index;
2943 
2944 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2945 	uinfo->count = 1;
2946 	uinfo->value.enumerated.items = 3;
2947 	index = uinfo->value.enumerated.item;
2948 	if (index >= 3)
2949 		index = 2;
2950 	strcpy(uinfo->value.enumerated.name, texts[index]);
2951 	return 0;
2952 }
2953 
vmaster_mute_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2954 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2955 				 struct snd_ctl_elem_value *ucontrol)
2956 {
2957 	struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2958 	ucontrol->value.enumerated.item[0] = hook->mute_mode;
2959 	return 0;
2960 }
2961 
vmaster_mute_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2962 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2963 				 struct snd_ctl_elem_value *ucontrol)
2964 {
2965 	struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2966 	unsigned int old_mode = hook->mute_mode;
2967 
2968 	hook->mute_mode = ucontrol->value.enumerated.item[0];
2969 	if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2970 		hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2971 	if (old_mode == hook->mute_mode)
2972 		return 0;
2973 	snd_hda_sync_vmaster_hook(hook);
2974 	return 1;
2975 }
2976 
2977 static struct snd_kcontrol_new vmaster_mute_mode = {
2978 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2979 	.name = "Mute-LED Mode",
2980 	.info = vmaster_mute_mode_info,
2981 	.get = vmaster_mute_mode_get,
2982 	.put = vmaster_mute_mode_put,
2983 };
2984 
2985 /*
2986  * Add a mute-LED hook with the given vmaster switch kctl
2987  * "Mute-LED Mode" control is automatically created and associated with
2988  * the given hook.
2989  */
snd_hda_add_vmaster_hook(struct hda_codec * codec,struct hda_vmaster_mute_hook * hook,bool expose_enum_ctl)2990 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2991 			     struct hda_vmaster_mute_hook *hook,
2992 			     bool expose_enum_ctl)
2993 {
2994 	struct snd_kcontrol *kctl;
2995 
2996 	if (!hook->hook || !hook->sw_kctl)
2997 		return 0;
2998 	hook->codec = codec;
2999 	hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
3000 	snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
3001 	if (!expose_enum_ctl)
3002 		return 0;
3003 	kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
3004 	if (!kctl)
3005 		return -ENOMEM;
3006 	return snd_hda_ctl_add(codec, 0, kctl);
3007 }
3008 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
3009 
3010 /*
3011  * Call the hook with the current value for synchronization
3012  * Should be called in init callback
3013  */
snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook * hook)3014 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
3015 {
3016 	if (!hook->hook || !hook->codec)
3017 		return;
3018 	/* don't call vmaster hook in the destructor since it might have
3019 	 * been already destroyed
3020 	 */
3021 	if (hook->codec->bus->shutdown)
3022 		return;
3023 	snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3024 }
3025 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
3026 
3027 
3028 /**
3029  * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
3030  *
3031  * The control element is supposed to have the private_value field
3032  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3033  */
snd_hda_mixer_amp_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)3034 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3035 				  struct snd_ctl_elem_info *uinfo)
3036 {
3037 	int chs = get_amp_channels(kcontrol);
3038 
3039 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3040 	uinfo->count = chs == 3 ? 2 : 1;
3041 	uinfo->value.integer.min = 0;
3042 	uinfo->value.integer.max = 1;
3043 	return 0;
3044 }
3045 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
3046 
3047 /**
3048  * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
3049  *
3050  * The control element is supposed to have the private_value field
3051  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3052  */
snd_hda_mixer_amp_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3053 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3054 				 struct snd_ctl_elem_value *ucontrol)
3055 {
3056 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3057 	hda_nid_t nid = get_amp_nid(kcontrol);
3058 	int chs = get_amp_channels(kcontrol);
3059 	int dir = get_amp_direction(kcontrol);
3060 	int idx = get_amp_index(kcontrol);
3061 	long *valp = ucontrol->value.integer.value;
3062 
3063 	if (chs & 1)
3064 		*valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
3065 			   HDA_AMP_MUTE) ? 0 : 1;
3066 	if (chs & 2)
3067 		*valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
3068 			 HDA_AMP_MUTE) ? 0 : 1;
3069 	return 0;
3070 }
3071 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
3072 
3073 /**
3074  * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3075  *
3076  * The control element is supposed to have the private_value field
3077  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3078  */
snd_hda_mixer_amp_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3079 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3080 				 struct snd_ctl_elem_value *ucontrol)
3081 {
3082 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3083 	hda_nid_t nid = get_amp_nid(kcontrol);
3084 	int chs = get_amp_channels(kcontrol);
3085 	int dir = get_amp_direction(kcontrol);
3086 	int idx = get_amp_index(kcontrol);
3087 	long *valp = ucontrol->value.integer.value;
3088 	int change = 0;
3089 
3090 	snd_hda_power_up(codec);
3091 	if (chs & 1) {
3092 		change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
3093 						  HDA_AMP_MUTE,
3094 						  *valp ? 0 : HDA_AMP_MUTE);
3095 		valp++;
3096 	}
3097 	if (chs & 2)
3098 		change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
3099 						   HDA_AMP_MUTE,
3100 						   *valp ? 0 : HDA_AMP_MUTE);
3101 	hda_call_check_power_status(codec, nid);
3102 	snd_hda_power_down(codec);
3103 	return change;
3104 }
3105 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
3106 
3107 /*
3108  * bound volume controls
3109  *
3110  * bind multiple volumes (# indices, from 0)
3111  */
3112 
3113 #define AMP_VAL_IDX_SHIFT	19
3114 #define AMP_VAL_IDX_MASK	(0x0f<<19)
3115 
3116 /**
3117  * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3118  *
3119  * The control element is supposed to have the private_value field
3120  * set up via HDA_BIND_MUTE*() macros.
3121  */
snd_hda_mixer_bind_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3122 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3123 				  struct snd_ctl_elem_value *ucontrol)
3124 {
3125 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3126 	unsigned long pval;
3127 	int err;
3128 
3129 	mutex_lock(&codec->control_mutex);
3130 	pval = kcontrol->private_value;
3131 	kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3132 	err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3133 	kcontrol->private_value = pval;
3134 	mutex_unlock(&codec->control_mutex);
3135 	return err;
3136 }
3137 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
3138 
3139 /**
3140  * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3141  *
3142  * The control element is supposed to have the private_value field
3143  * set up via HDA_BIND_MUTE*() macros.
3144  */
snd_hda_mixer_bind_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3145 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3146 				  struct snd_ctl_elem_value *ucontrol)
3147 {
3148 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3149 	unsigned long pval;
3150 	int i, indices, err = 0, change = 0;
3151 
3152 	mutex_lock(&codec->control_mutex);
3153 	pval = kcontrol->private_value;
3154 	indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3155 	for (i = 0; i < indices; i++) {
3156 		kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3157 			(i << AMP_VAL_IDX_SHIFT);
3158 		err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3159 		if (err < 0)
3160 			break;
3161 		change |= err;
3162 	}
3163 	kcontrol->private_value = pval;
3164 	mutex_unlock(&codec->control_mutex);
3165 	return err < 0 ? err : change;
3166 }
3167 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
3168 
3169 /**
3170  * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3171  *
3172  * The control element is supposed to have the private_value field
3173  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3174  */
snd_hda_mixer_bind_ctls_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)3175 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3176 				 struct snd_ctl_elem_info *uinfo)
3177 {
3178 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3179 	struct hda_bind_ctls *c;
3180 	int err;
3181 
3182 	mutex_lock(&codec->control_mutex);
3183 	c = (struct hda_bind_ctls *)kcontrol->private_value;
3184 	kcontrol->private_value = *c->values;
3185 	err = c->ops->info(kcontrol, uinfo);
3186 	kcontrol->private_value = (long)c;
3187 	mutex_unlock(&codec->control_mutex);
3188 	return err;
3189 }
3190 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
3191 
3192 /**
3193  * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3194  *
3195  * The control element is supposed to have the private_value field
3196  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3197  */
snd_hda_mixer_bind_ctls_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3198 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3199 				struct snd_ctl_elem_value *ucontrol)
3200 {
3201 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3202 	struct hda_bind_ctls *c;
3203 	int err;
3204 
3205 	mutex_lock(&codec->control_mutex);
3206 	c = (struct hda_bind_ctls *)kcontrol->private_value;
3207 	kcontrol->private_value = *c->values;
3208 	err = c->ops->get(kcontrol, ucontrol);
3209 	kcontrol->private_value = (long)c;
3210 	mutex_unlock(&codec->control_mutex);
3211 	return err;
3212 }
3213 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
3214 
3215 /**
3216  * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3217  *
3218  * The control element is supposed to have the private_value field
3219  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3220  */
snd_hda_mixer_bind_ctls_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3221 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3222 				struct snd_ctl_elem_value *ucontrol)
3223 {
3224 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3225 	struct hda_bind_ctls *c;
3226 	unsigned long *vals;
3227 	int err = 0, change = 0;
3228 
3229 	mutex_lock(&codec->control_mutex);
3230 	c = (struct hda_bind_ctls *)kcontrol->private_value;
3231 	for (vals = c->values; *vals; vals++) {
3232 		kcontrol->private_value = *vals;
3233 		err = c->ops->put(kcontrol, ucontrol);
3234 		if (err < 0)
3235 			break;
3236 		change |= err;
3237 	}
3238 	kcontrol->private_value = (long)c;
3239 	mutex_unlock(&codec->control_mutex);
3240 	return err < 0 ? err : change;
3241 }
3242 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
3243 
3244 /**
3245  * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3246  *
3247  * The control element is supposed to have the private_value field
3248  * set up via HDA_BIND_VOL() macro.
3249  */
snd_hda_mixer_bind_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * tlv)3250 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3251 			   unsigned int size, unsigned int __user *tlv)
3252 {
3253 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3254 	struct hda_bind_ctls *c;
3255 	int err;
3256 
3257 	mutex_lock(&codec->control_mutex);
3258 	c = (struct hda_bind_ctls *)kcontrol->private_value;
3259 	kcontrol->private_value = *c->values;
3260 	err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3261 	kcontrol->private_value = (long)c;
3262 	mutex_unlock(&codec->control_mutex);
3263 	return err;
3264 }
3265 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
3266 
3267 struct hda_ctl_ops snd_hda_bind_vol = {
3268 	.info = snd_hda_mixer_amp_volume_info,
3269 	.get = snd_hda_mixer_amp_volume_get,
3270 	.put = snd_hda_mixer_amp_volume_put,
3271 	.tlv = snd_hda_mixer_amp_tlv
3272 };
3273 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
3274 
3275 struct hda_ctl_ops snd_hda_bind_sw = {
3276 	.info = snd_hda_mixer_amp_switch_info,
3277 	.get = snd_hda_mixer_amp_switch_get,
3278 	.put = snd_hda_mixer_amp_switch_put,
3279 	.tlv = snd_hda_mixer_amp_tlv
3280 };
3281 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
3282 
3283 /*
3284  * SPDIF out controls
3285  */
3286 
snd_hda_spdif_mask_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)3287 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3288 				   struct snd_ctl_elem_info *uinfo)
3289 {
3290 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3291 	uinfo->count = 1;
3292 	return 0;
3293 }
3294 
snd_hda_spdif_cmask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3295 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3296 				   struct snd_ctl_elem_value *ucontrol)
3297 {
3298 	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3299 					   IEC958_AES0_NONAUDIO |
3300 					   IEC958_AES0_CON_EMPHASIS_5015 |
3301 					   IEC958_AES0_CON_NOT_COPYRIGHT;
3302 	ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3303 					   IEC958_AES1_CON_ORIGINAL;
3304 	return 0;
3305 }
3306 
snd_hda_spdif_pmask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3307 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3308 				   struct snd_ctl_elem_value *ucontrol)
3309 {
3310 	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3311 					   IEC958_AES0_NONAUDIO |
3312 					   IEC958_AES0_PRO_EMPHASIS_5015;
3313 	return 0;
3314 }
3315 
snd_hda_spdif_default_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3316 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3317 				     struct snd_ctl_elem_value *ucontrol)
3318 {
3319 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3320 	int idx = kcontrol->private_value;
3321 	struct hda_spdif_out *spdif;
3322 
3323 	mutex_lock(&codec->spdif_mutex);
3324 	spdif = snd_array_elem(&codec->spdif_out, idx);
3325 	ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3326 	ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3327 	ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3328 	ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3329 	mutex_unlock(&codec->spdif_mutex);
3330 
3331 	return 0;
3332 }
3333 
3334 /* convert from SPDIF status bits to HDA SPDIF bits
3335  * bit 0 (DigEn) is always set zero (to be filled later)
3336  */
convert_from_spdif_status(unsigned int sbits)3337 static unsigned short convert_from_spdif_status(unsigned int sbits)
3338 {
3339 	unsigned short val = 0;
3340 
3341 	if (sbits & IEC958_AES0_PROFESSIONAL)
3342 		val |= AC_DIG1_PROFESSIONAL;
3343 	if (sbits & IEC958_AES0_NONAUDIO)
3344 		val |= AC_DIG1_NONAUDIO;
3345 	if (sbits & IEC958_AES0_PROFESSIONAL) {
3346 		if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3347 		    IEC958_AES0_PRO_EMPHASIS_5015)
3348 			val |= AC_DIG1_EMPHASIS;
3349 	} else {
3350 		if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3351 		    IEC958_AES0_CON_EMPHASIS_5015)
3352 			val |= AC_DIG1_EMPHASIS;
3353 		if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3354 			val |= AC_DIG1_COPYRIGHT;
3355 		if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3356 			val |= AC_DIG1_LEVEL;
3357 		val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3358 	}
3359 	return val;
3360 }
3361 
3362 /* convert to SPDIF status bits from HDA SPDIF bits
3363  */
convert_to_spdif_status(unsigned short val)3364 static unsigned int convert_to_spdif_status(unsigned short val)
3365 {
3366 	unsigned int sbits = 0;
3367 
3368 	if (val & AC_DIG1_NONAUDIO)
3369 		sbits |= IEC958_AES0_NONAUDIO;
3370 	if (val & AC_DIG1_PROFESSIONAL)
3371 		sbits |= IEC958_AES0_PROFESSIONAL;
3372 	if (sbits & IEC958_AES0_PROFESSIONAL) {
3373 		if (val & AC_DIG1_EMPHASIS)
3374 			sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3375 	} else {
3376 		if (val & AC_DIG1_EMPHASIS)
3377 			sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3378 		if (!(val & AC_DIG1_COPYRIGHT))
3379 			sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3380 		if (val & AC_DIG1_LEVEL)
3381 			sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3382 		sbits |= val & (0x7f << 8);
3383 	}
3384 	return sbits;
3385 }
3386 
3387 /* set digital convert verbs both for the given NID and its slaves */
set_dig_out(struct hda_codec * codec,hda_nid_t nid,int verb,int val)3388 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3389 			int verb, int val)
3390 {
3391 	const hda_nid_t *d;
3392 
3393 	snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3394 	d = codec->slave_dig_outs;
3395 	if (!d)
3396 		return;
3397 	for (; *d; d++)
3398 		snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3399 }
3400 
set_dig_out_convert(struct hda_codec * codec,hda_nid_t nid,int dig1,int dig2)3401 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3402 				       int dig1, int dig2)
3403 {
3404 	if (dig1 != -1)
3405 		set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3406 	if (dig2 != -1)
3407 		set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3408 }
3409 
snd_hda_spdif_default_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3410 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3411 				     struct snd_ctl_elem_value *ucontrol)
3412 {
3413 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3414 	int idx = kcontrol->private_value;
3415 	struct hda_spdif_out *spdif;
3416 	hda_nid_t nid;
3417 	unsigned short val;
3418 	int change;
3419 
3420 	mutex_lock(&codec->spdif_mutex);
3421 	spdif = snd_array_elem(&codec->spdif_out, idx);
3422 	nid = spdif->nid;
3423 	spdif->status = ucontrol->value.iec958.status[0] |
3424 		((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3425 		((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3426 		((unsigned int)ucontrol->value.iec958.status[3] << 24);
3427 	val = convert_from_spdif_status(spdif->status);
3428 	val |= spdif->ctls & 1;
3429 	change = spdif->ctls != val;
3430 	spdif->ctls = val;
3431 	if (change && nid != (u16)-1)
3432 		set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3433 	mutex_unlock(&codec->spdif_mutex);
3434 	return change;
3435 }
3436 
3437 #define snd_hda_spdif_out_switch_info	snd_ctl_boolean_mono_info
3438 
snd_hda_spdif_out_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3439 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3440 					struct snd_ctl_elem_value *ucontrol)
3441 {
3442 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3443 	int idx = kcontrol->private_value;
3444 	struct hda_spdif_out *spdif;
3445 
3446 	mutex_lock(&codec->spdif_mutex);
3447 	spdif = snd_array_elem(&codec->spdif_out, idx);
3448 	ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3449 	mutex_unlock(&codec->spdif_mutex);
3450 	return 0;
3451 }
3452 
set_spdif_ctls(struct hda_codec * codec,hda_nid_t nid,int dig1,int dig2)3453 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3454 				  int dig1, int dig2)
3455 {
3456 	set_dig_out_convert(codec, nid, dig1, dig2);
3457 	/* unmute amp switch (if any) */
3458 	if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3459 	    (dig1 & AC_DIG1_ENABLE))
3460 		snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3461 					    HDA_AMP_MUTE, 0);
3462 }
3463 
snd_hda_spdif_out_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3464 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3465 					struct snd_ctl_elem_value *ucontrol)
3466 {
3467 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3468 	int idx = kcontrol->private_value;
3469 	struct hda_spdif_out *spdif;
3470 	hda_nid_t nid;
3471 	unsigned short val;
3472 	int change;
3473 
3474 	mutex_lock(&codec->spdif_mutex);
3475 	spdif = snd_array_elem(&codec->spdif_out, idx);
3476 	nid = spdif->nid;
3477 	val = spdif->ctls & ~AC_DIG1_ENABLE;
3478 	if (ucontrol->value.integer.value[0])
3479 		val |= AC_DIG1_ENABLE;
3480 	change = spdif->ctls != val;
3481 	spdif->ctls = val;
3482 	if (change && nid != (u16)-1)
3483 		set_spdif_ctls(codec, nid, val & 0xff, -1);
3484 	mutex_unlock(&codec->spdif_mutex);
3485 	return change;
3486 }
3487 
3488 static struct snd_kcontrol_new dig_mixes[] = {
3489 	{
3490 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
3491 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3492 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3493 		.info = snd_hda_spdif_mask_info,
3494 		.get = snd_hda_spdif_cmask_get,
3495 	},
3496 	{
3497 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
3498 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3499 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3500 		.info = snd_hda_spdif_mask_info,
3501 		.get = snd_hda_spdif_pmask_get,
3502 	},
3503 	{
3504 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3505 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3506 		.info = snd_hda_spdif_mask_info,
3507 		.get = snd_hda_spdif_default_get,
3508 		.put = snd_hda_spdif_default_put,
3509 	},
3510 	{
3511 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3512 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3513 		.info = snd_hda_spdif_out_switch_info,
3514 		.get = snd_hda_spdif_out_switch_get,
3515 		.put = snd_hda_spdif_out_switch_put,
3516 	},
3517 	{ } /* end */
3518 };
3519 
3520 /**
3521  * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3522  * @codec: the HDA codec
3523  * @associated_nid: NID that new ctls associated with
3524  * @cvt_nid: converter NID
3525  * @type: HDA_PCM_TYPE_*
3526  * Creates controls related with the digital output.
3527  * Called from each patch supporting the digital out.
3528  *
3529  * Returns 0 if successful, or a negative error code.
3530  */
snd_hda_create_dig_out_ctls(struct hda_codec * codec,hda_nid_t associated_nid,hda_nid_t cvt_nid,int type)3531 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3532 				hda_nid_t associated_nid,
3533 				hda_nid_t cvt_nid,
3534 				int type)
3535 {
3536 	int err;
3537 	struct snd_kcontrol *kctl;
3538 	struct snd_kcontrol_new *dig_mix;
3539 	int idx = 0;
3540 	const int spdif_index = 16;
3541 	struct hda_spdif_out *spdif;
3542 	struct hda_bus *bus = codec->bus;
3543 
3544 	if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3545 	    type == HDA_PCM_TYPE_SPDIF) {
3546 		idx = spdif_index;
3547 	} else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3548 		   type == HDA_PCM_TYPE_HDMI) {
3549 		/* suppose a single SPDIF device */
3550 		for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3551 			kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3552 			if (!kctl)
3553 				break;
3554 			kctl->id.index = spdif_index;
3555 		}
3556 		bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3557 	}
3558 	if (!bus->primary_dig_out_type)
3559 		bus->primary_dig_out_type = type;
3560 
3561 	idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3562 	if (idx < 0) {
3563 		codec_err(codec, "too many IEC958 outputs\n");
3564 		return -EBUSY;
3565 	}
3566 	spdif = snd_array_new(&codec->spdif_out);
3567 	if (!spdif)
3568 		return -ENOMEM;
3569 	for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3570 		kctl = snd_ctl_new1(dig_mix, codec);
3571 		if (!kctl)
3572 			return -ENOMEM;
3573 		kctl->id.index = idx;
3574 		kctl->private_value = codec->spdif_out.used - 1;
3575 		err = snd_hda_ctl_add(codec, associated_nid, kctl);
3576 		if (err < 0)
3577 			return err;
3578 	}
3579 	spdif->nid = cvt_nid;
3580 	spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3581 					 AC_VERB_GET_DIGI_CONVERT_1, 0);
3582 	spdif->status = convert_to_spdif_status(spdif->ctls);
3583 	return 0;
3584 }
3585 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
3586 
3587 /* get the hda_spdif_out entry from the given NID
3588  * call within spdif_mutex lock
3589  */
snd_hda_spdif_out_of_nid(struct hda_codec * codec,hda_nid_t nid)3590 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3591 					       hda_nid_t nid)
3592 {
3593 	int i;
3594 	for (i = 0; i < codec->spdif_out.used; i++) {
3595 		struct hda_spdif_out *spdif =
3596 				snd_array_elem(&codec->spdif_out, i);
3597 		if (spdif->nid == nid)
3598 			return spdif;
3599 	}
3600 	return NULL;
3601 }
3602 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
3603 
snd_hda_spdif_ctls_unassign(struct hda_codec * codec,int idx)3604 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3605 {
3606 	struct hda_spdif_out *spdif;
3607 
3608 	mutex_lock(&codec->spdif_mutex);
3609 	spdif = snd_array_elem(&codec->spdif_out, idx);
3610 	spdif->nid = (u16)-1;
3611 	mutex_unlock(&codec->spdif_mutex);
3612 }
3613 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
3614 
snd_hda_spdif_ctls_assign(struct hda_codec * codec,int idx,hda_nid_t nid)3615 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3616 {
3617 	struct hda_spdif_out *spdif;
3618 	unsigned short val;
3619 
3620 	mutex_lock(&codec->spdif_mutex);
3621 	spdif = snd_array_elem(&codec->spdif_out, idx);
3622 	if (spdif->nid != nid) {
3623 		spdif->nid = nid;
3624 		val = spdif->ctls;
3625 		set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3626 	}
3627 	mutex_unlock(&codec->spdif_mutex);
3628 }
3629 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
3630 
3631 /*
3632  * SPDIF sharing with analog output
3633  */
spdif_share_sw_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3634 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3635 			      struct snd_ctl_elem_value *ucontrol)
3636 {
3637 	struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3638 	ucontrol->value.integer.value[0] = mout->share_spdif;
3639 	return 0;
3640 }
3641 
spdif_share_sw_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3642 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3643 			      struct snd_ctl_elem_value *ucontrol)
3644 {
3645 	struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3646 	mout->share_spdif = !!ucontrol->value.integer.value[0];
3647 	return 0;
3648 }
3649 
3650 static struct snd_kcontrol_new spdif_share_sw = {
3651 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3652 	.name = "IEC958 Default PCM Playback Switch",
3653 	.info = snd_ctl_boolean_mono_info,
3654 	.get = spdif_share_sw_get,
3655 	.put = spdif_share_sw_put,
3656 };
3657 
3658 /**
3659  * snd_hda_create_spdif_share_sw - create Default PCM switch
3660  * @codec: the HDA codec
3661  * @mout: multi-out instance
3662  */
snd_hda_create_spdif_share_sw(struct hda_codec * codec,struct hda_multi_out * mout)3663 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3664 				  struct hda_multi_out *mout)
3665 {
3666 	struct snd_kcontrol *kctl;
3667 
3668 	if (!mout->dig_out_nid)
3669 		return 0;
3670 
3671 	kctl = snd_ctl_new1(&spdif_share_sw, mout);
3672 	if (!kctl)
3673 		return -ENOMEM;
3674 	/* ATTENTION: here mout is passed as private_data, instead of codec */
3675 	return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3676 }
3677 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
3678 
3679 /*
3680  * SPDIF input
3681  */
3682 
3683 #define snd_hda_spdif_in_switch_info	snd_hda_spdif_out_switch_info
3684 
snd_hda_spdif_in_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3685 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3686 				       struct snd_ctl_elem_value *ucontrol)
3687 {
3688 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3689 
3690 	ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3691 	return 0;
3692 }
3693 
snd_hda_spdif_in_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3694 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3695 				       struct snd_ctl_elem_value *ucontrol)
3696 {
3697 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3698 	hda_nid_t nid = kcontrol->private_value;
3699 	unsigned int val = !!ucontrol->value.integer.value[0];
3700 	int change;
3701 
3702 	mutex_lock(&codec->spdif_mutex);
3703 	change = codec->spdif_in_enable != val;
3704 	if (change) {
3705 		codec->spdif_in_enable = val;
3706 		snd_hda_codec_write_cache(codec, nid, 0,
3707 					  AC_VERB_SET_DIGI_CONVERT_1, val);
3708 	}
3709 	mutex_unlock(&codec->spdif_mutex);
3710 	return change;
3711 }
3712 
snd_hda_spdif_in_status_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3713 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3714 				       struct snd_ctl_elem_value *ucontrol)
3715 {
3716 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3717 	hda_nid_t nid = kcontrol->private_value;
3718 	unsigned short val;
3719 	unsigned int sbits;
3720 
3721 	val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3722 	sbits = convert_to_spdif_status(val);
3723 	ucontrol->value.iec958.status[0] = sbits;
3724 	ucontrol->value.iec958.status[1] = sbits >> 8;
3725 	ucontrol->value.iec958.status[2] = sbits >> 16;
3726 	ucontrol->value.iec958.status[3] = sbits >> 24;
3727 	return 0;
3728 }
3729 
3730 static struct snd_kcontrol_new dig_in_ctls[] = {
3731 	{
3732 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3733 		.name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3734 		.info = snd_hda_spdif_in_switch_info,
3735 		.get = snd_hda_spdif_in_switch_get,
3736 		.put = snd_hda_spdif_in_switch_put,
3737 	},
3738 	{
3739 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
3740 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3741 		.name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3742 		.info = snd_hda_spdif_mask_info,
3743 		.get = snd_hda_spdif_in_status_get,
3744 	},
3745 	{ } /* end */
3746 };
3747 
3748 /**
3749  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3750  * @codec: the HDA codec
3751  * @nid: audio in widget NID
3752  *
3753  * Creates controls related with the SPDIF input.
3754  * Called from each patch supporting the SPDIF in.
3755  *
3756  * Returns 0 if successful, or a negative error code.
3757  */
snd_hda_create_spdif_in_ctls(struct hda_codec * codec,hda_nid_t nid)3758 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3759 {
3760 	int err;
3761 	struct snd_kcontrol *kctl;
3762 	struct snd_kcontrol_new *dig_mix;
3763 	int idx;
3764 
3765 	idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3766 	if (idx < 0) {
3767 		codec_err(codec, "too many IEC958 inputs\n");
3768 		return -EBUSY;
3769 	}
3770 	for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3771 		kctl = snd_ctl_new1(dig_mix, codec);
3772 		if (!kctl)
3773 			return -ENOMEM;
3774 		kctl->private_value = nid;
3775 		err = snd_hda_ctl_add(codec, nid, kctl);
3776 		if (err < 0)
3777 			return err;
3778 	}
3779 	codec->spdif_in_enable =
3780 		snd_hda_codec_read(codec, nid, 0,
3781 				   AC_VERB_GET_DIGI_CONVERT_1, 0) &
3782 		AC_DIG1_ENABLE;
3783 	return 0;
3784 }
3785 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
3786 
3787 /*
3788  * command cache
3789  */
3790 
3791 /* build a 31bit cache key with the widget id and the command parameter */
3792 #define build_cmd_cache_key(nid, verb)	((verb << 8) | nid)
3793 #define get_cmd_cache_nid(key)		((key) & 0xff)
3794 #define get_cmd_cache_cmd(key)		(((key) >> 8) & 0xffff)
3795 
3796 /**
3797  * snd_hda_codec_write_cache - send a single command with caching
3798  * @codec: the HDA codec
3799  * @nid: NID to send the command
3800  * @flags: optional bit flags
3801  * @verb: the verb to send
3802  * @parm: the parameter for the verb
3803  *
3804  * Send a single command without waiting for response.
3805  *
3806  * Returns 0 if successful, or a negative error code.
3807  */
snd_hda_codec_write_cache(struct hda_codec * codec,hda_nid_t nid,int flags,unsigned int verb,unsigned int parm)3808 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3809 			      int flags, unsigned int verb, unsigned int parm)
3810 {
3811 	int err;
3812 	struct hda_cache_head *c;
3813 	u32 key;
3814 	unsigned int cache_only;
3815 
3816 	cache_only = codec->cached_write;
3817 	if (!cache_only) {
3818 		err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3819 		if (err < 0)
3820 			return err;
3821 	}
3822 
3823 	/* parm may contain the verb stuff for get/set amp */
3824 	verb = verb | (parm >> 8);
3825 	parm &= 0xff;
3826 	key = build_cmd_cache_key(nid, verb);
3827 	mutex_lock(&codec->bus->cmd_mutex);
3828 	c = get_alloc_hash(&codec->cmd_cache, key);
3829 	if (c) {
3830 		c->val = parm;
3831 		c->dirty = cache_only;
3832 	}
3833 	mutex_unlock(&codec->bus->cmd_mutex);
3834 	return 0;
3835 }
3836 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
3837 
3838 /**
3839  * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3840  * @codec: the HDA codec
3841  * @nid: NID to send the command
3842  * @flags: optional bit flags
3843  * @verb: the verb to send
3844  * @parm: the parameter for the verb
3845  *
3846  * This function works like snd_hda_codec_write_cache(), but it doesn't send
3847  * command if the parameter is already identical with the cached value.
3848  * If not, it sends the command and refreshes the cache.
3849  *
3850  * Returns 0 if successful, or a negative error code.
3851  */
snd_hda_codec_update_cache(struct hda_codec * codec,hda_nid_t nid,int flags,unsigned int verb,unsigned int parm)3852 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3853 			       int flags, unsigned int verb, unsigned int parm)
3854 {
3855 	struct hda_cache_head *c;
3856 	u32 key;
3857 
3858 	/* parm may contain the verb stuff for get/set amp */
3859 	verb = verb | (parm >> 8);
3860 	parm &= 0xff;
3861 	key = build_cmd_cache_key(nid, verb);
3862 	mutex_lock(&codec->bus->cmd_mutex);
3863 	c = get_hash(&codec->cmd_cache, key);
3864 	if (c && c->val == parm) {
3865 		mutex_unlock(&codec->bus->cmd_mutex);
3866 		return 0;
3867 	}
3868 	mutex_unlock(&codec->bus->cmd_mutex);
3869 	return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3870 }
3871 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
3872 
3873 /**
3874  * snd_hda_codec_resume_cache - Resume the all commands from the cache
3875  * @codec: HD-audio codec
3876  *
3877  * Execute all verbs recorded in the command caches to resume.
3878  */
snd_hda_codec_resume_cache(struct hda_codec * codec)3879 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3880 {
3881 	int i;
3882 
3883 	mutex_lock(&codec->hash_mutex);
3884 	codec->cached_write = 0;
3885 	for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3886 		struct hda_cache_head *buffer;
3887 		u32 key;
3888 
3889 		buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3890 		key = buffer->key;
3891 		if (!key)
3892 			continue;
3893 		if (!buffer->dirty)
3894 			continue;
3895 		buffer->dirty = 0;
3896 		mutex_unlock(&codec->hash_mutex);
3897 		snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3898 				    get_cmd_cache_cmd(key), buffer->val);
3899 		mutex_lock(&codec->hash_mutex);
3900 	}
3901 	mutex_unlock(&codec->hash_mutex);
3902 }
3903 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
3904 
3905 /**
3906  * snd_hda_sequence_write_cache - sequence writes with caching
3907  * @codec: the HDA codec
3908  * @seq: VERB array to send
3909  *
3910  * Send the commands sequentially from the given array.
3911  * Thte commands are recorded on cache for power-save and resume.
3912  * The array must be terminated with NID=0.
3913  */
snd_hda_sequence_write_cache(struct hda_codec * codec,const struct hda_verb * seq)3914 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3915 				  const struct hda_verb *seq)
3916 {
3917 	for (; seq->nid; seq++)
3918 		snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3919 					  seq->param);
3920 }
3921 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
3922 
3923 /**
3924  * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3925  * @codec: HD-audio codec
3926  */
snd_hda_codec_flush_cache(struct hda_codec * codec)3927 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3928 {
3929 	snd_hda_codec_resume_amp(codec);
3930 	snd_hda_codec_resume_cache(codec);
3931 }
3932 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
3933 
snd_hda_codec_set_power_to_all(struct hda_codec * codec,hda_nid_t fg,unsigned int power_state)3934 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3935 				    unsigned int power_state)
3936 {
3937 	hda_nid_t nid = codec->start_nid;
3938 	int i;
3939 
3940 	for (i = 0; i < codec->num_nodes; i++, nid++) {
3941 		unsigned int wcaps = get_wcaps(codec, nid);
3942 		unsigned int state = power_state;
3943 		if (!(wcaps & AC_WCAP_POWER))
3944 			continue;
3945 		if (codec->power_filter) {
3946 			state = codec->power_filter(codec, nid, power_state);
3947 			if (state != power_state && power_state == AC_PWRST_D3)
3948 				continue;
3949 		}
3950 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3951 				    state);
3952 	}
3953 }
3954 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
3955 
3956 /*
3957  *  supported power states check
3958  */
snd_hda_codec_get_supported_ps(struct hda_codec * codec,hda_nid_t fg,unsigned int power_state)3959 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3960 				unsigned int power_state)
3961 {
3962 	int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3963 
3964 	if (sup == -1)
3965 		return false;
3966 	if (sup & power_state)
3967 		return true;
3968 	else
3969 		return false;
3970 }
3971 
3972 /*
3973  * wait until the state is reached, returns the current state
3974  */
hda_sync_power_state(struct hda_codec * codec,hda_nid_t fg,unsigned int power_state)3975 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3976 					 hda_nid_t fg,
3977 					 unsigned int power_state)
3978 {
3979 	unsigned long end_time = jiffies + msecs_to_jiffies(500);
3980 	unsigned int state, actual_state;
3981 
3982 	for (;;) {
3983 		state = snd_hda_codec_read(codec, fg, 0,
3984 					   AC_VERB_GET_POWER_STATE, 0);
3985 		if (state & AC_PWRST_ERROR)
3986 			break;
3987 		actual_state = (state >> 4) & 0x0f;
3988 		if (actual_state == power_state)
3989 			break;
3990 		if (time_after_eq(jiffies, end_time))
3991 			break;
3992 		/* wait until the codec reachs to the target state */
3993 		msleep(1);
3994 	}
3995 	return state;
3996 }
3997 
3998 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
snd_hda_codec_eapd_power_filter(struct hda_codec * codec,hda_nid_t nid,unsigned int power_state)3999 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
4000 					     hda_nid_t nid,
4001 					     unsigned int power_state)
4002 {
4003 	if (nid == codec->afg || nid == codec->mfg)
4004 		return power_state;
4005 	if (power_state == AC_PWRST_D3 &&
4006 	    get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
4007 	    (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
4008 		int eapd = snd_hda_codec_read(codec, nid, 0,
4009 					      AC_VERB_GET_EAPD_BTLENABLE, 0);
4010 		if (eapd & 0x02)
4011 			return AC_PWRST_D0;
4012 	}
4013 	return power_state;
4014 }
4015 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
4016 
4017 /*
4018  * set power state of the codec, and return the power state
4019  */
hda_set_power_state(struct hda_codec * codec,unsigned int power_state)4020 static unsigned int hda_set_power_state(struct hda_codec *codec,
4021 					unsigned int power_state)
4022 {
4023 	hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
4024 	int count;
4025 	unsigned int state;
4026 	int flags = 0;
4027 
4028 	/* this delay seems necessary to avoid click noise at power-down */
4029 	if (power_state == AC_PWRST_D3) {
4030 		if (codec->depop_delay < 0)
4031 			msleep(codec->epss ? 10 : 100);
4032 		else if (codec->depop_delay > 0)
4033 			msleep(codec->depop_delay);
4034 		flags = HDA_RW_NO_RESPONSE_FALLBACK;
4035 	}
4036 
4037 	/* repeat power states setting at most 10 times*/
4038 	for (count = 0; count < 10; count++) {
4039 		if (codec->patch_ops.set_power_state)
4040 			codec->patch_ops.set_power_state(codec, fg,
4041 							 power_state);
4042 		else {
4043 			state = power_state;
4044 			if (codec->power_filter)
4045 				state = codec->power_filter(codec, fg, state);
4046 			if (state == power_state || power_state != AC_PWRST_D3)
4047 				snd_hda_codec_read(codec, fg, flags,
4048 						   AC_VERB_SET_POWER_STATE,
4049 						   state);
4050 			snd_hda_codec_set_power_to_all(codec, fg, power_state);
4051 		}
4052 		state = hda_sync_power_state(codec, fg, power_state);
4053 		if (!(state & AC_PWRST_ERROR))
4054 			break;
4055 	}
4056 
4057 	return state;
4058 }
4059 
4060 /* sync power states of all widgets;
4061  * this is called at the end of codec parsing
4062  */
sync_power_up_states(struct hda_codec * codec)4063 static void sync_power_up_states(struct hda_codec *codec)
4064 {
4065 	hda_nid_t nid = codec->start_nid;
4066 	int i;
4067 
4068 	/* don't care if no filter is used */
4069 	if (!codec->power_filter)
4070 		return;
4071 
4072 	for (i = 0; i < codec->num_nodes; i++, nid++) {
4073 		unsigned int wcaps = get_wcaps(codec, nid);
4074 		unsigned int target;
4075 		if (!(wcaps & AC_WCAP_POWER))
4076 			continue;
4077 		target = codec->power_filter(codec, nid, AC_PWRST_D0);
4078 		if (target == AC_PWRST_D0)
4079 			continue;
4080 		if (!snd_hda_check_power_state(codec, nid, target))
4081 			snd_hda_codec_write(codec, nid, 0,
4082 					    AC_VERB_SET_POWER_STATE, target);
4083 	}
4084 }
4085 
4086 #ifdef CONFIG_SND_HDA_RECONFIG
4087 /* execute additional init verbs */
hda_exec_init_verbs(struct hda_codec * codec)4088 static void hda_exec_init_verbs(struct hda_codec *codec)
4089 {
4090 	if (codec->init_verbs.list)
4091 		snd_hda_sequence_write(codec, codec->init_verbs.list);
4092 }
4093 #else
hda_exec_init_verbs(struct hda_codec * codec)4094 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4095 #endif
4096 
4097 #ifdef CONFIG_PM
4098 /*
4099  * call suspend and power-down; used both from PM and power-save
4100  * this function returns the power state in the end
4101  */
hda_call_codec_suspend(struct hda_codec * codec,bool in_wq)4102 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
4103 {
4104 	unsigned int state;
4105 
4106 	codec->in_pm = 1;
4107 
4108 	if (codec->patch_ops.suspend)
4109 		codec->patch_ops.suspend(codec);
4110 	hda_cleanup_all_streams(codec);
4111 	state = hda_set_power_state(codec, AC_PWRST_D3);
4112 	/* Cancel delayed work if we aren't currently running from it. */
4113 	if (!in_wq)
4114 		cancel_delayed_work_sync(&codec->power_work);
4115 	spin_lock(&codec->power_lock);
4116 	snd_hda_update_power_acct(codec);
4117 	trace_hda_power_down(codec);
4118 	codec->power_on = 0;
4119 	codec->power_transition = 0;
4120 	codec->power_jiffies = jiffies;
4121 	spin_unlock(&codec->power_lock);
4122 	codec->in_pm = 0;
4123 	return state;
4124 }
4125 
4126 /* mark all entries of cmd and amp caches dirty */
hda_mark_cmd_cache_dirty(struct hda_codec * codec)4127 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4128 {
4129 	int i;
4130 	for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4131 		struct hda_cache_head *cmd;
4132 		cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4133 		cmd->dirty = 1;
4134 	}
4135 	for (i = 0; i < codec->amp_cache.buf.used; i++) {
4136 		struct hda_amp_info *amp;
4137 		amp = snd_array_elem(&codec->amp_cache.buf, i);
4138 		amp->head.dirty = 1;
4139 	}
4140 }
4141 
4142 /*
4143  * kick up codec; used both from PM and power-save
4144  */
hda_call_codec_resume(struct hda_codec * codec)4145 static void hda_call_codec_resume(struct hda_codec *codec)
4146 {
4147 	codec->in_pm = 1;
4148 
4149 	hda_mark_cmd_cache_dirty(codec);
4150 
4151 	/* set as if powered on for avoiding re-entering the resume
4152 	 * in the resume / power-save sequence
4153 	 */
4154 	hda_keep_power_on(codec);
4155 	hda_set_power_state(codec, AC_PWRST_D0);
4156 	restore_shutup_pins(codec);
4157 	hda_exec_init_verbs(codec);
4158 	snd_hda_jack_set_dirty_all(codec);
4159 	if (codec->patch_ops.resume)
4160 		codec->patch_ops.resume(codec);
4161 	else {
4162 		if (codec->patch_ops.init)
4163 			codec->patch_ops.init(codec);
4164 		snd_hda_codec_resume_amp(codec);
4165 		snd_hda_codec_resume_cache(codec);
4166 	}
4167 
4168 	if (codec->jackpoll_interval)
4169 		hda_jackpoll_work(&codec->jackpoll_work.work);
4170 	else
4171 		snd_hda_jack_report_sync(codec);
4172 
4173 	codec->in_pm = 0;
4174 	snd_hda_power_down(codec); /* flag down before returning */
4175 }
4176 #endif /* CONFIG_PM */
4177 
4178 
4179 /**
4180  * snd_hda_build_controls - build mixer controls
4181  * @bus: the BUS
4182  *
4183  * Creates mixer controls for each codec included in the bus.
4184  *
4185  * Returns 0 if successful, otherwise a negative error code.
4186  */
snd_hda_build_controls(struct hda_bus * bus)4187 int snd_hda_build_controls(struct hda_bus *bus)
4188 {
4189 	struct hda_codec *codec;
4190 
4191 	list_for_each_entry(codec, &bus->codec_list, list) {
4192 		int err = snd_hda_codec_build_controls(codec);
4193 		if (err < 0) {
4194 			codec_err(codec,
4195 				  "cannot build controls for #%d (error %d)\n",
4196 				  codec->addr, err);
4197 			err = snd_hda_codec_reset(codec);
4198 			if (err < 0) {
4199 				codec_err(codec,
4200 					  "cannot revert codec\n");
4201 				return err;
4202 			}
4203 		}
4204 	}
4205 	return 0;
4206 }
4207 EXPORT_SYMBOL_GPL(snd_hda_build_controls);
4208 
4209 /*
4210  * add standard channel maps if not specified
4211  */
add_std_chmaps(struct hda_codec * codec)4212 static int add_std_chmaps(struct hda_codec *codec)
4213 {
4214 	int i, str, err;
4215 
4216 	for (i = 0; i < codec->num_pcms; i++) {
4217 		for (str = 0; str < 2; str++) {
4218 			struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4219 			struct hda_pcm_stream *hinfo =
4220 				&codec->pcm_info[i].stream[str];
4221 			struct snd_pcm_chmap *chmap;
4222 			const struct snd_pcm_chmap_elem *elem;
4223 
4224 			if (codec->pcm_info[i].own_chmap)
4225 				continue;
4226 			if (!pcm || !hinfo->substreams)
4227 				continue;
4228 			elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4229 			err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4230 						     hinfo->channels_max,
4231 						     0, &chmap);
4232 			if (err < 0)
4233 				return err;
4234 			chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4235 		}
4236 	}
4237 	return 0;
4238 }
4239 
4240 /* default channel maps for 2.1 speakers;
4241  * since HD-audio supports only stereo, odd number channels are omitted
4242  */
4243 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4244 	{ .channels = 2,
4245 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4246 	{ .channels = 4,
4247 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4248 		   SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4249 	{ }
4250 };
4251 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4252 
snd_hda_codec_build_controls(struct hda_codec * codec)4253 int snd_hda_codec_build_controls(struct hda_codec *codec)
4254 {
4255 	int err = 0;
4256 	hda_exec_init_verbs(codec);
4257 	/* continue to initialize... */
4258 	if (codec->patch_ops.init)
4259 		err = codec->patch_ops.init(codec);
4260 	if (!err && codec->patch_ops.build_controls)
4261 		err = codec->patch_ops.build_controls(codec);
4262 	if (err < 0)
4263 		return err;
4264 
4265 	/* we create chmaps here instead of build_pcms */
4266 	err = add_std_chmaps(codec);
4267 	if (err < 0)
4268 		return err;
4269 
4270 	if (codec->jackpoll_interval)
4271 		hda_jackpoll_work(&codec->jackpoll_work.work);
4272 	else
4273 		snd_hda_jack_report_sync(codec); /* call at the last init point */
4274 	sync_power_up_states(codec);
4275 	return 0;
4276 }
4277 
4278 /*
4279  * stream formats
4280  */
4281 struct hda_rate_tbl {
4282 	unsigned int hz;
4283 	unsigned int alsa_bits;
4284 	unsigned int hda_fmt;
4285 };
4286 
4287 /* rate = base * mult / div */
4288 #define HDA_RATE(base, mult, div) \
4289 	(AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4290 	 (((div) - 1) << AC_FMT_DIV_SHIFT))
4291 
4292 static struct hda_rate_tbl rate_bits[] = {
4293 	/* rate in Hz, ALSA rate bitmask, HDA format value */
4294 
4295 	/* autodetected value used in snd_hda_query_supported_pcm */
4296 	{ 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4297 	{ 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4298 	{ 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4299 	{ 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4300 	{ 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4301 	{ 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4302 	{ 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4303 	{ 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4304 	{ 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4305 	{ 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4306 	{ 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4307 #define AC_PAR_PCM_RATE_BITS	11
4308 	/* up to bits 10, 384kHZ isn't supported properly */
4309 
4310 	/* not autodetected value */
4311 	{ 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4312 
4313 	{ 0 } /* terminator */
4314 };
4315 
4316 /**
4317  * snd_hda_calc_stream_format - calculate format bitset
4318  * @codec: HD-audio codec
4319  * @rate: the sample rate
4320  * @channels: the number of channels
4321  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4322  * @maxbps: the max. bps
4323  *
4324  * Calculate the format bitset from the given rate, channels and th PCM format.
4325  *
4326  * Return zero if invalid.
4327  */
snd_hda_calc_stream_format(struct hda_codec * codec,unsigned int rate,unsigned int channels,unsigned int format,unsigned int maxbps,unsigned short spdif_ctls)4328 unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4329 					unsigned int rate,
4330 					unsigned int channels,
4331 					unsigned int format,
4332 					unsigned int maxbps,
4333 					unsigned short spdif_ctls)
4334 {
4335 	int i;
4336 	unsigned int val = 0;
4337 
4338 	for (i = 0; rate_bits[i].hz; i++)
4339 		if (rate_bits[i].hz == rate) {
4340 			val = rate_bits[i].hda_fmt;
4341 			break;
4342 		}
4343 	if (!rate_bits[i].hz) {
4344 		codec_dbg(codec, "invalid rate %d\n", rate);
4345 		return 0;
4346 	}
4347 
4348 	if (channels == 0 || channels > 8) {
4349 		codec_dbg(codec, "invalid channels %d\n", channels);
4350 		return 0;
4351 	}
4352 	val |= channels - 1;
4353 
4354 	switch (snd_pcm_format_width(format)) {
4355 	case 8:
4356 		val |= AC_FMT_BITS_8;
4357 		break;
4358 	case 16:
4359 		val |= AC_FMT_BITS_16;
4360 		break;
4361 	case 20:
4362 	case 24:
4363 	case 32:
4364 		if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4365 			val |= AC_FMT_BITS_32;
4366 		else if (maxbps >= 24)
4367 			val |= AC_FMT_BITS_24;
4368 		else
4369 			val |= AC_FMT_BITS_20;
4370 		break;
4371 	default:
4372 		codec_dbg(codec, "invalid format width %d\n",
4373 			  snd_pcm_format_width(format));
4374 		return 0;
4375 	}
4376 
4377 	if (spdif_ctls & AC_DIG1_NONAUDIO)
4378 		val |= AC_FMT_TYPE_NON_PCM;
4379 
4380 	return val;
4381 }
4382 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
4383 
get_pcm_param(struct hda_codec * codec,hda_nid_t nid,int dir)4384 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4385 				  int dir)
4386 {
4387 	unsigned int val = 0;
4388 	if (nid != codec->afg &&
4389 	    (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4390 		val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4391 	if (!val || val == -1)
4392 		val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4393 	if (!val || val == -1)
4394 		return 0;
4395 	return val;
4396 }
4397 
query_pcm_param(struct hda_codec * codec,hda_nid_t nid)4398 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4399 {
4400 	return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4401 			       get_pcm_param);
4402 }
4403 
get_stream_param(struct hda_codec * codec,hda_nid_t nid,int dir)4404 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4405 				     int dir)
4406 {
4407 	unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4408 	if (!streams || streams == -1)
4409 		streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4410 	if (!streams || streams == -1)
4411 		return 0;
4412 	return streams;
4413 }
4414 
query_stream_param(struct hda_codec * codec,hda_nid_t nid)4415 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4416 {
4417 	return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4418 			       get_stream_param);
4419 }
4420 
4421 /**
4422  * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4423  * @codec: the HDA codec
4424  * @nid: NID to query
4425  * @ratesp: the pointer to store the detected rate bitflags
4426  * @formatsp: the pointer to store the detected formats
4427  * @bpsp: the pointer to store the detected format widths
4428  *
4429  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
4430  * or @bsps argument is ignored.
4431  *
4432  * Returns 0 if successful, otherwise a negative error code.
4433  */
snd_hda_query_supported_pcm(struct hda_codec * codec,hda_nid_t nid,u32 * ratesp,u64 * formatsp,unsigned int * bpsp)4434 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4435 				u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4436 {
4437 	unsigned int i, val, wcaps;
4438 
4439 	wcaps = get_wcaps(codec, nid);
4440 	val = query_pcm_param(codec, nid);
4441 
4442 	if (ratesp) {
4443 		u32 rates = 0;
4444 		for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4445 			if (val & (1 << i))
4446 				rates |= rate_bits[i].alsa_bits;
4447 		}
4448 		if (rates == 0) {
4449 			codec_err(codec,
4450 				  "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4451 				  nid, val,
4452 				  (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4453 			return -EIO;
4454 		}
4455 		*ratesp = rates;
4456 	}
4457 
4458 	if (formatsp || bpsp) {
4459 		u64 formats = 0;
4460 		unsigned int streams, bps;
4461 
4462 		streams = query_stream_param(codec, nid);
4463 		if (!streams)
4464 			return -EIO;
4465 
4466 		bps = 0;
4467 		if (streams & AC_SUPFMT_PCM) {
4468 			if (val & AC_SUPPCM_BITS_8) {
4469 				formats |= SNDRV_PCM_FMTBIT_U8;
4470 				bps = 8;
4471 			}
4472 			if (val & AC_SUPPCM_BITS_16) {
4473 				formats |= SNDRV_PCM_FMTBIT_S16_LE;
4474 				bps = 16;
4475 			}
4476 			if (wcaps & AC_WCAP_DIGITAL) {
4477 				if (val & AC_SUPPCM_BITS_32)
4478 					formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4479 				if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4480 					formats |= SNDRV_PCM_FMTBIT_S32_LE;
4481 				if (val & AC_SUPPCM_BITS_24)
4482 					bps = 24;
4483 				else if (val & AC_SUPPCM_BITS_20)
4484 					bps = 20;
4485 			} else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4486 					  AC_SUPPCM_BITS_32)) {
4487 				formats |= SNDRV_PCM_FMTBIT_S32_LE;
4488 				if (val & AC_SUPPCM_BITS_32)
4489 					bps = 32;
4490 				else if (val & AC_SUPPCM_BITS_24)
4491 					bps = 24;
4492 				else if (val & AC_SUPPCM_BITS_20)
4493 					bps = 20;
4494 			}
4495 		}
4496 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4497 		if (streams & AC_SUPFMT_FLOAT32) {
4498 			formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4499 			if (!bps)
4500 				bps = 32;
4501 		}
4502 #endif
4503 		if (streams == AC_SUPFMT_AC3) {
4504 			/* should be exclusive */
4505 			/* temporary hack: we have still no proper support
4506 			 * for the direct AC3 stream...
4507 			 */
4508 			formats |= SNDRV_PCM_FMTBIT_U8;
4509 			bps = 8;
4510 		}
4511 		if (formats == 0) {
4512 			codec_err(codec,
4513 				  "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4514 				  nid, val,
4515 				  (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4516 				  streams);
4517 			return -EIO;
4518 		}
4519 		if (formatsp)
4520 			*formatsp = formats;
4521 		if (bpsp)
4522 			*bpsp = bps;
4523 	}
4524 
4525 	return 0;
4526 }
4527 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
4528 
4529 /**
4530  * snd_hda_is_supported_format - Check the validity of the format
4531  * @codec: HD-audio codec
4532  * @nid: NID to check
4533  * @format: the HD-audio format value to check
4534  *
4535  * Check whether the given node supports the format value.
4536  *
4537  * Returns 1 if supported, 0 if not.
4538  */
snd_hda_is_supported_format(struct hda_codec * codec,hda_nid_t nid,unsigned int format)4539 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4540 				unsigned int format)
4541 {
4542 	int i;
4543 	unsigned int val = 0, rate, stream;
4544 
4545 	val = query_pcm_param(codec, nid);
4546 	if (!val)
4547 		return 0;
4548 
4549 	rate = format & 0xff00;
4550 	for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4551 		if (rate_bits[i].hda_fmt == rate) {
4552 			if (val & (1 << i))
4553 				break;
4554 			return 0;
4555 		}
4556 	if (i >= AC_PAR_PCM_RATE_BITS)
4557 		return 0;
4558 
4559 	stream = query_stream_param(codec, nid);
4560 	if (!stream)
4561 		return 0;
4562 
4563 	if (stream & AC_SUPFMT_PCM) {
4564 		switch (format & 0xf0) {
4565 		case 0x00:
4566 			if (!(val & AC_SUPPCM_BITS_8))
4567 				return 0;
4568 			break;
4569 		case 0x10:
4570 			if (!(val & AC_SUPPCM_BITS_16))
4571 				return 0;
4572 			break;
4573 		case 0x20:
4574 			if (!(val & AC_SUPPCM_BITS_20))
4575 				return 0;
4576 			break;
4577 		case 0x30:
4578 			if (!(val & AC_SUPPCM_BITS_24))
4579 				return 0;
4580 			break;
4581 		case 0x40:
4582 			if (!(val & AC_SUPPCM_BITS_32))
4583 				return 0;
4584 			break;
4585 		default:
4586 			return 0;
4587 		}
4588 	} else {
4589 		/* FIXME: check for float32 and AC3? */
4590 	}
4591 
4592 	return 1;
4593 }
4594 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
4595 
4596 /*
4597  * PCM stuff
4598  */
hda_pcm_default_open_close(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)4599 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4600 				      struct hda_codec *codec,
4601 				      struct snd_pcm_substream *substream)
4602 {
4603 	return 0;
4604 }
4605 
hda_pcm_default_prepare(struct hda_pcm_stream * hinfo,struct hda_codec * codec,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)4606 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4607 				   struct hda_codec *codec,
4608 				   unsigned int stream_tag,
4609 				   unsigned int format,
4610 				   struct snd_pcm_substream *substream)
4611 {
4612 	snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4613 	return 0;
4614 }
4615 
hda_pcm_default_cleanup(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)4616 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4617 				   struct hda_codec *codec,
4618 				   struct snd_pcm_substream *substream)
4619 {
4620 	snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4621 	return 0;
4622 }
4623 
set_pcm_default_values(struct hda_codec * codec,struct hda_pcm_stream * info)4624 static int set_pcm_default_values(struct hda_codec *codec,
4625 				  struct hda_pcm_stream *info)
4626 {
4627 	int err;
4628 
4629 	/* query support PCM information from the given NID */
4630 	if (info->nid && (!info->rates || !info->formats)) {
4631 		err = snd_hda_query_supported_pcm(codec, info->nid,
4632 				info->rates ? NULL : &info->rates,
4633 				info->formats ? NULL : &info->formats,
4634 				info->maxbps ? NULL : &info->maxbps);
4635 		if (err < 0)
4636 			return err;
4637 	}
4638 	if (info->ops.open == NULL)
4639 		info->ops.open = hda_pcm_default_open_close;
4640 	if (info->ops.close == NULL)
4641 		info->ops.close = hda_pcm_default_open_close;
4642 	if (info->ops.prepare == NULL) {
4643 		if (snd_BUG_ON(!info->nid))
4644 			return -EINVAL;
4645 		info->ops.prepare = hda_pcm_default_prepare;
4646 	}
4647 	if (info->ops.cleanup == NULL) {
4648 		if (snd_BUG_ON(!info->nid))
4649 			return -EINVAL;
4650 		info->ops.cleanup = hda_pcm_default_cleanup;
4651 	}
4652 	return 0;
4653 }
4654 
4655 /*
4656  * codec prepare/cleanup entries
4657  */
snd_hda_codec_prepare(struct hda_codec * codec,struct hda_pcm_stream * hinfo,unsigned int stream,unsigned int format,struct snd_pcm_substream * substream)4658 int snd_hda_codec_prepare(struct hda_codec *codec,
4659 			  struct hda_pcm_stream *hinfo,
4660 			  unsigned int stream,
4661 			  unsigned int format,
4662 			  struct snd_pcm_substream *substream)
4663 {
4664 	int ret;
4665 	mutex_lock(&codec->bus->prepare_mutex);
4666 	ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4667 	if (ret >= 0)
4668 		purify_inactive_streams(codec);
4669 	mutex_unlock(&codec->bus->prepare_mutex);
4670 	return ret;
4671 }
4672 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
4673 
snd_hda_codec_cleanup(struct hda_codec * codec,struct hda_pcm_stream * hinfo,struct snd_pcm_substream * substream)4674 void snd_hda_codec_cleanup(struct hda_codec *codec,
4675 			   struct hda_pcm_stream *hinfo,
4676 			   struct snd_pcm_substream *substream)
4677 {
4678 	mutex_lock(&codec->bus->prepare_mutex);
4679 	hinfo->ops.cleanup(hinfo, codec, substream);
4680 	mutex_unlock(&codec->bus->prepare_mutex);
4681 }
4682 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
4683 
4684 /* global */
4685 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4686 	"Audio", "SPDIF", "HDMI", "Modem"
4687 };
4688 
4689 /*
4690  * get the empty PCM device number to assign
4691  */
get_empty_pcm_device(struct hda_bus * bus,unsigned int type)4692 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4693 {
4694 	/* audio device indices; not linear to keep compatibility */
4695 	/* assigned to static slots up to dev#10; if more needed, assign
4696 	 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4697 	 */
4698 	static int audio_idx[HDA_PCM_NTYPES][5] = {
4699 		[HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4700 		[HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4701 		[HDA_PCM_TYPE_HDMI]  = { 3, 7, 8, 9, -1 },
4702 		[HDA_PCM_TYPE_MODEM] = { 6, -1 },
4703 	};
4704 	int i;
4705 
4706 	if (type >= HDA_PCM_NTYPES) {
4707 		dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
4708 		return -EINVAL;
4709 	}
4710 
4711 	for (i = 0; audio_idx[type][i] >= 0; i++) {
4712 #ifndef CONFIG_SND_DYNAMIC_MINORS
4713 		if (audio_idx[type][i] >= 8)
4714 			break;
4715 #endif
4716 		if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4717 			return audio_idx[type][i];
4718 	}
4719 
4720 #ifdef CONFIG_SND_DYNAMIC_MINORS
4721 	/* non-fixed slots starting from 10 */
4722 	for (i = 10; i < 32; i++) {
4723 		if (!test_and_set_bit(i, bus->pcm_dev_bits))
4724 			return i;
4725 	}
4726 #endif
4727 
4728 	dev_warn(bus->card->dev, "Too many %s devices\n",
4729 		snd_hda_pcm_type_name[type]);
4730 #ifndef CONFIG_SND_DYNAMIC_MINORS
4731 	dev_warn(bus->card->dev,
4732 		 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4733 #endif
4734 	return -EAGAIN;
4735 }
4736 
4737 /*
4738  * attach a new PCM stream
4739  */
snd_hda_attach_pcm(struct hda_codec * codec,struct hda_pcm * pcm)4740 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4741 {
4742 	struct hda_bus *bus = codec->bus;
4743 	struct hda_pcm_stream *info;
4744 	int stream, err;
4745 
4746 	if (snd_BUG_ON(!pcm->name))
4747 		return -EINVAL;
4748 	for (stream = 0; stream < 2; stream++) {
4749 		info = &pcm->stream[stream];
4750 		if (info->substreams) {
4751 			err = set_pcm_default_values(codec, info);
4752 			if (err < 0)
4753 				return err;
4754 		}
4755 	}
4756 	return bus->ops.attach_pcm(bus, codec, pcm);
4757 }
4758 
4759 /* assign all PCMs of the given codec */
snd_hda_codec_build_pcms(struct hda_codec * codec)4760 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4761 {
4762 	unsigned int pcm;
4763 	int err;
4764 
4765 	if (!codec->num_pcms) {
4766 		if (!codec->patch_ops.build_pcms)
4767 			return 0;
4768 		err = codec->patch_ops.build_pcms(codec);
4769 		if (err < 0) {
4770 			codec_err(codec,
4771 				  "cannot build PCMs for #%d (error %d)\n",
4772 				  codec->addr, err);
4773 			err = snd_hda_codec_reset(codec);
4774 			if (err < 0) {
4775 				codec_err(codec,
4776 					  "cannot revert codec\n");
4777 				return err;
4778 			}
4779 		}
4780 	}
4781 	for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4782 		struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4783 		int dev;
4784 
4785 		if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4786 			continue; /* no substreams assigned */
4787 
4788 		if (!cpcm->pcm) {
4789 			dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4790 			if (dev < 0)
4791 				continue; /* no fatal error */
4792 			cpcm->device = dev;
4793 			err = snd_hda_attach_pcm(codec, cpcm);
4794 			if (err < 0) {
4795 				codec_err(codec,
4796 					  "cannot attach PCM stream %d for codec #%d\n",
4797 					  dev, codec->addr);
4798 				continue; /* no fatal error */
4799 			}
4800 		}
4801 	}
4802 	return 0;
4803 }
4804 
4805 /**
4806  * snd_hda_build_pcms - build PCM information
4807  * @bus: the BUS
4808  *
4809  * Create PCM information for each codec included in the bus.
4810  *
4811  * The build_pcms codec patch is requested to set up codec->num_pcms and
4812  * codec->pcm_info properly.  The array is referred by the top-level driver
4813  * to create its PCM instances.
4814  * The allocated codec->pcm_info should be released in codec->patch_ops.free
4815  * callback.
4816  *
4817  * At least, substreams, channels_min and channels_max must be filled for
4818  * each stream.  substreams = 0 indicates that the stream doesn't exist.
4819  * When rates and/or formats are zero, the supported values are queried
4820  * from the given nid.  The nid is used also by the default ops.prepare
4821  * and ops.cleanup callbacks.
4822  *
4823  * The driver needs to call ops.open in its open callback.  Similarly,
4824  * ops.close is supposed to be called in the close callback.
4825  * ops.prepare should be called in the prepare or hw_params callback
4826  * with the proper parameters for set up.
4827  * ops.cleanup should be called in hw_free for clean up of streams.
4828  *
4829  * This function returns 0 if successful, or a negative error code.
4830  */
snd_hda_build_pcms(struct hda_bus * bus)4831 int snd_hda_build_pcms(struct hda_bus *bus)
4832 {
4833 	struct hda_codec *codec;
4834 
4835 	list_for_each_entry(codec, &bus->codec_list, list) {
4836 		int err = snd_hda_codec_build_pcms(codec);
4837 		if (err < 0)
4838 			return err;
4839 	}
4840 	return 0;
4841 }
4842 EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
4843 
4844 /**
4845  * snd_hda_add_new_ctls - create controls from the array
4846  * @codec: the HDA codec
4847  * @knew: the array of struct snd_kcontrol_new
4848  *
4849  * This helper function creates and add new controls in the given array.
4850  * The array must be terminated with an empty entry as terminator.
4851  *
4852  * Returns 0 if successful, or a negative error code.
4853  */
snd_hda_add_new_ctls(struct hda_codec * codec,const struct snd_kcontrol_new * knew)4854 int snd_hda_add_new_ctls(struct hda_codec *codec,
4855 			 const struct snd_kcontrol_new *knew)
4856 {
4857 	int err;
4858 
4859 	for (; knew->name; knew++) {
4860 		struct snd_kcontrol *kctl;
4861 		int addr = 0, idx = 0;
4862 		if (knew->iface == -1)	/* skip this codec private value */
4863 			continue;
4864 		for (;;) {
4865 			kctl = snd_ctl_new1(knew, codec);
4866 			if (!kctl)
4867 				return -ENOMEM;
4868 			if (addr > 0)
4869 				kctl->id.device = addr;
4870 			if (idx > 0)
4871 				kctl->id.index = idx;
4872 			err = snd_hda_ctl_add(codec, 0, kctl);
4873 			if (!err)
4874 				break;
4875 			/* try first with another device index corresponding to
4876 			 * the codec addr; if it still fails (or it's the
4877 			 * primary codec), then try another control index
4878 			 */
4879 			if (!addr && codec->addr)
4880 				addr = codec->addr;
4881 			else if (!idx && !knew->index) {
4882 				idx = find_empty_mixer_ctl_idx(codec,
4883 							       knew->name, 0);
4884 				if (idx <= 0)
4885 					return err;
4886 			} else
4887 				return err;
4888 		}
4889 	}
4890 	return 0;
4891 }
4892 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
4893 
4894 #ifdef CONFIG_PM
hda_power_work(struct work_struct * work)4895 static void hda_power_work(struct work_struct *work)
4896 {
4897 	struct hda_codec *codec =
4898 		container_of(work, struct hda_codec, power_work.work);
4899 	struct hda_bus *bus = codec->bus;
4900 	unsigned int state;
4901 
4902 	spin_lock(&codec->power_lock);
4903 	if (codec->power_transition > 0) { /* during power-up sequence? */
4904 		spin_unlock(&codec->power_lock);
4905 		return;
4906 	}
4907 	if (!codec->power_on || codec->power_count) {
4908 		codec->power_transition = 0;
4909 		spin_unlock(&codec->power_lock);
4910 		return;
4911 	}
4912 	spin_unlock(&codec->power_lock);
4913 
4914 	state = hda_call_codec_suspend(codec, true);
4915 	if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4916 		hda_call_pm_notify(codec, false);
4917 }
4918 
hda_keep_power_on(struct hda_codec * codec)4919 static void hda_keep_power_on(struct hda_codec *codec)
4920 {
4921 	spin_lock(&codec->power_lock);
4922 	codec->power_count++;
4923 	codec->power_on = 1;
4924 	codec->power_jiffies = jiffies;
4925 	spin_unlock(&codec->power_lock);
4926 	hda_call_pm_notify(codec, true);
4927 }
4928 
4929 /* update the power on/off account with the current jiffies */
snd_hda_update_power_acct(struct hda_codec * codec)4930 void snd_hda_update_power_acct(struct hda_codec *codec)
4931 {
4932 	unsigned long delta = jiffies - codec->power_jiffies;
4933 	if (codec->power_on)
4934 		codec->power_on_acct += delta;
4935 	else
4936 		codec->power_off_acct += delta;
4937 	codec->power_jiffies += delta;
4938 }
4939 
4940 /* Transition to powered up, if wait_power_down then wait for a pending
4941  * transition to D3 to complete. A pending D3 transition is indicated
4942  * with power_transition == -1. */
4943 /* call this with codec->power_lock held! */
__snd_hda_power_up(struct hda_codec * codec,bool wait_power_down)4944 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4945 {
4946 	/* Return if power_on or transitioning to power_on, unless currently
4947 	 * powering down. */
4948 	if ((codec->power_on || codec->power_transition > 0) &&
4949 	    !(wait_power_down && codec->power_transition < 0))
4950 		return;
4951 	spin_unlock(&codec->power_lock);
4952 
4953 	cancel_delayed_work_sync(&codec->power_work);
4954 
4955 	spin_lock(&codec->power_lock);
4956 	/* If the power down delayed work was cancelled above before starting,
4957 	 * then there is no need to go through power up here.
4958 	 */
4959 	if (codec->power_on) {
4960 		if (codec->power_transition < 0)
4961 			codec->power_transition = 0;
4962 		return;
4963 	}
4964 
4965 	trace_hda_power_up(codec);
4966 	snd_hda_update_power_acct(codec);
4967 	codec->power_on = 1;
4968 	codec->power_jiffies = jiffies;
4969 	codec->power_transition = 1; /* avoid reentrance */
4970 	spin_unlock(&codec->power_lock);
4971 
4972 	hda_call_codec_resume(codec);
4973 
4974 	spin_lock(&codec->power_lock);
4975 	codec->power_transition = 0;
4976 }
4977 
4978 #define power_save(codec)	\
4979 	((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4980 
4981 /* Transition to powered down */
__snd_hda_power_down(struct hda_codec * codec)4982 static void __snd_hda_power_down(struct hda_codec *codec)
4983 {
4984 	if (!codec->power_on || codec->power_count || codec->power_transition)
4985 		return;
4986 
4987 	if (power_save(codec)) {
4988 		codec->power_transition = -1; /* avoid reentrance */
4989 		queue_delayed_work(codec->bus->workq, &codec->power_work,
4990 				msecs_to_jiffies(power_save(codec) * 1000));
4991 	}
4992 }
4993 
4994 /**
4995  * snd_hda_power_save - Power-up/down/sync the codec
4996  * @codec: HD-audio codec
4997  * @delta: the counter delta to change
4998  *
4999  * Change the power-up counter via @delta, and power up or down the hardware
5000  * appropriately.  For the power-down, queue to the delayed action.
5001  * Passing zero to @delta means to synchronize the power state.
5002  */
snd_hda_power_save(struct hda_codec * codec,int delta,bool d3wait)5003 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5004 {
5005 	spin_lock(&codec->power_lock);
5006 	codec->power_count += delta;
5007 	trace_hda_power_count(codec);
5008 	if (delta > 0)
5009 		__snd_hda_power_up(codec, d3wait);
5010 	else
5011 		__snd_hda_power_down(codec);
5012 	spin_unlock(&codec->power_lock);
5013 }
5014 EXPORT_SYMBOL_GPL(snd_hda_power_save);
5015 
5016 /**
5017  * snd_hda_check_amp_list_power - Check the amp list and update the power
5018  * @codec: HD-audio codec
5019  * @check: the object containing an AMP list and the status
5020  * @nid: NID to check / update
5021  *
5022  * Check whether the given NID is in the amp list.  If it's in the list,
5023  * check the current AMP status, and update the the power-status according
5024  * to the mute status.
5025  *
5026  * This function is supposed to be set or called from the check_power_status
5027  * patch ops.
5028  */
snd_hda_check_amp_list_power(struct hda_codec * codec,struct hda_loopback_check * check,hda_nid_t nid)5029 int snd_hda_check_amp_list_power(struct hda_codec *codec,
5030 				 struct hda_loopback_check *check,
5031 				 hda_nid_t nid)
5032 {
5033 	const struct hda_amp_list *p;
5034 	int ch, v;
5035 
5036 	if (!check->amplist)
5037 		return 0;
5038 	for (p = check->amplist; p->nid; p++) {
5039 		if (p->nid == nid)
5040 			break;
5041 	}
5042 	if (!p->nid)
5043 		return 0; /* nothing changed */
5044 
5045 	for (p = check->amplist; p->nid; p++) {
5046 		for (ch = 0; ch < 2; ch++) {
5047 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5048 						   p->idx);
5049 			if (!(v & HDA_AMP_MUTE) && v > 0) {
5050 				if (!check->power_on) {
5051 					check->power_on = 1;
5052 					snd_hda_power_up(codec);
5053 				}
5054 				return 1;
5055 			}
5056 		}
5057 	}
5058 	if (check->power_on) {
5059 		check->power_on = 0;
5060 		snd_hda_power_down(codec);
5061 	}
5062 	return 0;
5063 }
5064 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
5065 #endif
5066 
5067 /*
5068  * Channel mode helper
5069  */
5070 
5071 /**
5072  * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5073  */
snd_hda_ch_mode_info(struct hda_codec * codec,struct snd_ctl_elem_info * uinfo,const struct hda_channel_mode * chmode,int num_chmodes)5074 int snd_hda_ch_mode_info(struct hda_codec *codec,
5075 			 struct snd_ctl_elem_info *uinfo,
5076 			 const struct hda_channel_mode *chmode,
5077 			 int num_chmodes)
5078 {
5079 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5080 	uinfo->count = 1;
5081 	uinfo->value.enumerated.items = num_chmodes;
5082 	if (uinfo->value.enumerated.item >= num_chmodes)
5083 		uinfo->value.enumerated.item = num_chmodes - 1;
5084 	sprintf(uinfo->value.enumerated.name, "%dch",
5085 		chmode[uinfo->value.enumerated.item].channels);
5086 	return 0;
5087 }
5088 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
5089 
5090 /**
5091  * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5092  */
snd_hda_ch_mode_get(struct hda_codec * codec,struct snd_ctl_elem_value * ucontrol,const struct hda_channel_mode * chmode,int num_chmodes,int max_channels)5093 int snd_hda_ch_mode_get(struct hda_codec *codec,
5094 			struct snd_ctl_elem_value *ucontrol,
5095 			const struct hda_channel_mode *chmode,
5096 			int num_chmodes,
5097 			int max_channels)
5098 {
5099 	int i;
5100 
5101 	for (i = 0; i < num_chmodes; i++) {
5102 		if (max_channels == chmode[i].channels) {
5103 			ucontrol->value.enumerated.item[0] = i;
5104 			break;
5105 		}
5106 	}
5107 	return 0;
5108 }
5109 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
5110 
5111 /**
5112  * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5113  */
snd_hda_ch_mode_put(struct hda_codec * codec,struct snd_ctl_elem_value * ucontrol,const struct hda_channel_mode * chmode,int num_chmodes,int * max_channelsp)5114 int snd_hda_ch_mode_put(struct hda_codec *codec,
5115 			struct snd_ctl_elem_value *ucontrol,
5116 			const struct hda_channel_mode *chmode,
5117 			int num_chmodes,
5118 			int *max_channelsp)
5119 {
5120 	unsigned int mode;
5121 
5122 	mode = ucontrol->value.enumerated.item[0];
5123 	if (mode >= num_chmodes)
5124 		return -EINVAL;
5125 	if (*max_channelsp == chmode[mode].channels)
5126 		return 0;
5127 	/* change the current channel setting */
5128 	*max_channelsp = chmode[mode].channels;
5129 	if (chmode[mode].sequence)
5130 		snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5131 	return 1;
5132 }
5133 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
5134 
5135 /*
5136  * input MUX helper
5137  */
5138 
5139 /**
5140  * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5141  */
snd_hda_input_mux_info(const struct hda_input_mux * imux,struct snd_ctl_elem_info * uinfo)5142 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5143 			   struct snd_ctl_elem_info *uinfo)
5144 {
5145 	unsigned int index;
5146 
5147 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5148 	uinfo->count = 1;
5149 	uinfo->value.enumerated.items = imux->num_items;
5150 	if (!imux->num_items)
5151 		return 0;
5152 	index = uinfo->value.enumerated.item;
5153 	if (index >= imux->num_items)
5154 		index = imux->num_items - 1;
5155 	strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5156 	return 0;
5157 }
5158 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
5159 
5160 /**
5161  * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5162  */
snd_hda_input_mux_put(struct hda_codec * codec,const struct hda_input_mux * imux,struct snd_ctl_elem_value * ucontrol,hda_nid_t nid,unsigned int * cur_val)5163 int snd_hda_input_mux_put(struct hda_codec *codec,
5164 			  const struct hda_input_mux *imux,
5165 			  struct snd_ctl_elem_value *ucontrol,
5166 			  hda_nid_t nid,
5167 			  unsigned int *cur_val)
5168 {
5169 	unsigned int idx;
5170 
5171 	if (!imux->num_items)
5172 		return 0;
5173 	idx = ucontrol->value.enumerated.item[0];
5174 	if (idx >= imux->num_items)
5175 		idx = imux->num_items - 1;
5176 	if (*cur_val == idx)
5177 		return 0;
5178 	snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5179 				  imux->items[idx].index);
5180 	*cur_val = idx;
5181 	return 1;
5182 }
5183 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
5184 
5185 
5186 /*
5187  * process kcontrol info callback of a simple string enum array
5188  * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5189  */
snd_hda_enum_helper_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo,int num_items,const char * const * texts)5190 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5191 			     struct snd_ctl_elem_info *uinfo,
5192 			     int num_items, const char * const *texts)
5193 {
5194 	static const char * const texts_default[] = {
5195 		"Disabled", "Enabled"
5196 	};
5197 
5198 	if (!texts || !num_items) {
5199 		num_items = 2;
5200 		texts = texts_default;
5201 	}
5202 
5203 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5204 	uinfo->count = 1;
5205 	uinfo->value.enumerated.items = num_items;
5206 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5207 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5208 	strcpy(uinfo->value.enumerated.name,
5209 	       texts[uinfo->value.enumerated.item]);
5210 	return 0;
5211 }
5212 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
5213 
5214 /*
5215  * Multi-channel / digital-out PCM helper functions
5216  */
5217 
5218 /* setup SPDIF output stream */
setup_dig_out_stream(struct hda_codec * codec,hda_nid_t nid,unsigned int stream_tag,unsigned int format)5219 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5220 				 unsigned int stream_tag, unsigned int format)
5221 {
5222 	struct hda_spdif_out *spdif;
5223 	unsigned int curr_fmt;
5224 	bool reset;
5225 
5226 	spdif = snd_hda_spdif_out_of_nid(codec, nid);
5227 	curr_fmt = snd_hda_codec_read(codec, nid, 0,
5228 				      AC_VERB_GET_STREAM_FORMAT, 0);
5229 	reset = codec->spdif_status_reset &&
5230 		(spdif->ctls & AC_DIG1_ENABLE) &&
5231 		curr_fmt != format;
5232 
5233 	/* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5234 	   updated */
5235 	if (reset)
5236 		set_dig_out_convert(codec, nid,
5237 				    spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5238 				    -1);
5239 	snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5240 	if (codec->slave_dig_outs) {
5241 		const hda_nid_t *d;
5242 		for (d = codec->slave_dig_outs; *d; d++)
5243 			snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5244 						   format);
5245 	}
5246 	/* turn on again (if needed) */
5247 	if (reset)
5248 		set_dig_out_convert(codec, nid,
5249 				    spdif->ctls & 0xff, -1);
5250 }
5251 
cleanup_dig_out_stream(struct hda_codec * codec,hda_nid_t nid)5252 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5253 {
5254 	snd_hda_codec_cleanup_stream(codec, nid);
5255 	if (codec->slave_dig_outs) {
5256 		const hda_nid_t *d;
5257 		for (d = codec->slave_dig_outs; *d; d++)
5258 			snd_hda_codec_cleanup_stream(codec, *d);
5259 	}
5260 }
5261 
5262 /**
5263  * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5264  * @bus: HD-audio bus
5265  */
snd_hda_bus_reboot_notify(struct hda_bus * bus)5266 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5267 {
5268 	struct hda_codec *codec;
5269 
5270 	if (!bus)
5271 		return;
5272 	list_for_each_entry(codec, &bus->codec_list, list) {
5273 		if (hda_codec_is_power_on(codec) &&
5274 		    codec->patch_ops.reboot_notify)
5275 			codec->patch_ops.reboot_notify(codec);
5276 	}
5277 }
5278 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
5279 
5280 /**
5281  * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5282  */
snd_hda_multi_out_dig_open(struct hda_codec * codec,struct hda_multi_out * mout)5283 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5284 			       struct hda_multi_out *mout)
5285 {
5286 	mutex_lock(&codec->spdif_mutex);
5287 	if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5288 		/* already opened as analog dup; reset it once */
5289 		cleanup_dig_out_stream(codec, mout->dig_out_nid);
5290 	mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5291 	mutex_unlock(&codec->spdif_mutex);
5292 	return 0;
5293 }
5294 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
5295 
5296 /**
5297  * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5298  */
snd_hda_multi_out_dig_prepare(struct hda_codec * codec,struct hda_multi_out * mout,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)5299 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5300 				  struct hda_multi_out *mout,
5301 				  unsigned int stream_tag,
5302 				  unsigned int format,
5303 				  struct snd_pcm_substream *substream)
5304 {
5305 	mutex_lock(&codec->spdif_mutex);
5306 	setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5307 	mutex_unlock(&codec->spdif_mutex);
5308 	return 0;
5309 }
5310 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
5311 
5312 /**
5313  * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5314  */
snd_hda_multi_out_dig_cleanup(struct hda_codec * codec,struct hda_multi_out * mout)5315 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5316 				  struct hda_multi_out *mout)
5317 {
5318 	mutex_lock(&codec->spdif_mutex);
5319 	cleanup_dig_out_stream(codec, mout->dig_out_nid);
5320 	mutex_unlock(&codec->spdif_mutex);
5321 	return 0;
5322 }
5323 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
5324 
5325 /**
5326  * snd_hda_multi_out_dig_close - release the digital out stream
5327  */
snd_hda_multi_out_dig_close(struct hda_codec * codec,struct hda_multi_out * mout)5328 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5329 				struct hda_multi_out *mout)
5330 {
5331 	mutex_lock(&codec->spdif_mutex);
5332 	mout->dig_out_used = 0;
5333 	mutex_unlock(&codec->spdif_mutex);
5334 	return 0;
5335 }
5336 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
5337 
5338 /**
5339  * snd_hda_multi_out_analog_open - open analog outputs
5340  *
5341  * Open analog outputs and set up the hw-constraints.
5342  * If the digital outputs can be opened as slave, open the digital
5343  * outputs, too.
5344  */
snd_hda_multi_out_analog_open(struct hda_codec * codec,struct hda_multi_out * mout,struct snd_pcm_substream * substream,struct hda_pcm_stream * hinfo)5345 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5346 				  struct hda_multi_out *mout,
5347 				  struct snd_pcm_substream *substream,
5348 				  struct hda_pcm_stream *hinfo)
5349 {
5350 	struct snd_pcm_runtime *runtime = substream->runtime;
5351 	runtime->hw.channels_max = mout->max_channels;
5352 	if (mout->dig_out_nid) {
5353 		if (!mout->analog_rates) {
5354 			mout->analog_rates = hinfo->rates;
5355 			mout->analog_formats = hinfo->formats;
5356 			mout->analog_maxbps = hinfo->maxbps;
5357 		} else {
5358 			runtime->hw.rates = mout->analog_rates;
5359 			runtime->hw.formats = mout->analog_formats;
5360 			hinfo->maxbps = mout->analog_maxbps;
5361 		}
5362 		if (!mout->spdif_rates) {
5363 			snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5364 						    &mout->spdif_rates,
5365 						    &mout->spdif_formats,
5366 						    &mout->spdif_maxbps);
5367 		}
5368 		mutex_lock(&codec->spdif_mutex);
5369 		if (mout->share_spdif) {
5370 			if ((runtime->hw.rates & mout->spdif_rates) &&
5371 			    (runtime->hw.formats & mout->spdif_formats)) {
5372 				runtime->hw.rates &= mout->spdif_rates;
5373 				runtime->hw.formats &= mout->spdif_formats;
5374 				if (mout->spdif_maxbps < hinfo->maxbps)
5375 					hinfo->maxbps = mout->spdif_maxbps;
5376 			} else {
5377 				mout->share_spdif = 0;
5378 				/* FIXME: need notify? */
5379 			}
5380 		}
5381 		mutex_unlock(&codec->spdif_mutex);
5382 	}
5383 	return snd_pcm_hw_constraint_step(substream->runtime, 0,
5384 					  SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5385 }
5386 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
5387 
5388 /**
5389  * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5390  *
5391  * Set up the i/o for analog out.
5392  * When the digital out is available, copy the front out to digital out, too.
5393  */
snd_hda_multi_out_analog_prepare(struct hda_codec * codec,struct hda_multi_out * mout,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)5394 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5395 				     struct hda_multi_out *mout,
5396 				     unsigned int stream_tag,
5397 				     unsigned int format,
5398 				     struct snd_pcm_substream *substream)
5399 {
5400 	const hda_nid_t *nids = mout->dac_nids;
5401 	int chs = substream->runtime->channels;
5402 	struct hda_spdif_out *spdif;
5403 	int i;
5404 
5405 	mutex_lock(&codec->spdif_mutex);
5406 	spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5407 	if (mout->dig_out_nid && mout->share_spdif &&
5408 	    mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5409 		if (chs == 2 &&
5410 		    snd_hda_is_supported_format(codec, mout->dig_out_nid,
5411 						format) &&
5412 		    !(spdif->status & IEC958_AES0_NONAUDIO)) {
5413 			mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5414 			setup_dig_out_stream(codec, mout->dig_out_nid,
5415 					     stream_tag, format);
5416 		} else {
5417 			mout->dig_out_used = 0;
5418 			cleanup_dig_out_stream(codec, mout->dig_out_nid);
5419 		}
5420 	}
5421 	mutex_unlock(&codec->spdif_mutex);
5422 
5423 	/* front */
5424 	snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5425 				   0, format);
5426 	if (!mout->no_share_stream &&
5427 	    mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5428 		/* headphone out will just decode front left/right (stereo) */
5429 		snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5430 					   0, format);
5431 	/* extra outputs copied from front */
5432 	for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5433 		if (!mout->no_share_stream && mout->hp_out_nid[i])
5434 			snd_hda_codec_setup_stream(codec,
5435 						   mout->hp_out_nid[i],
5436 						   stream_tag, 0, format);
5437 
5438 	/* surrounds */
5439 	for (i = 1; i < mout->num_dacs; i++) {
5440 		if (chs >= (i + 1) * 2) /* independent out */
5441 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5442 						   i * 2, format);
5443 		else if (!mout->no_share_stream) /* copy front */
5444 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5445 						   0, format);
5446 	}
5447 
5448 	/* extra surrounds */
5449 	for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5450 		int ch = 0;
5451 		if (!mout->extra_out_nid[i])
5452 			break;
5453 		if (chs >= (i + 1) * 2)
5454 			ch = i * 2;
5455 		else if (!mout->no_share_stream)
5456 			break;
5457 		snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5458 					   stream_tag, ch, format);
5459 	}
5460 
5461 	return 0;
5462 }
5463 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
5464 
5465 /**
5466  * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5467  */
snd_hda_multi_out_analog_cleanup(struct hda_codec * codec,struct hda_multi_out * mout)5468 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5469 				     struct hda_multi_out *mout)
5470 {
5471 	const hda_nid_t *nids = mout->dac_nids;
5472 	int i;
5473 
5474 	for (i = 0; i < mout->num_dacs; i++)
5475 		snd_hda_codec_cleanup_stream(codec, nids[i]);
5476 	if (mout->hp_nid)
5477 		snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5478 	for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5479 		if (mout->hp_out_nid[i])
5480 			snd_hda_codec_cleanup_stream(codec,
5481 						     mout->hp_out_nid[i]);
5482 	for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5483 		if (mout->extra_out_nid[i])
5484 			snd_hda_codec_cleanup_stream(codec,
5485 						     mout->extra_out_nid[i]);
5486 	mutex_lock(&codec->spdif_mutex);
5487 	if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5488 		cleanup_dig_out_stream(codec, mout->dig_out_nid);
5489 		mout->dig_out_used = 0;
5490 	}
5491 	mutex_unlock(&codec->spdif_mutex);
5492 	return 0;
5493 }
5494 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
5495 
5496 /**
5497  * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5498  *
5499  * Guess the suitable VREF pin bits to be set as the pin-control value.
5500  * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5501  */
snd_hda_get_default_vref(struct hda_codec * codec,hda_nid_t pin)5502 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5503 {
5504 	unsigned int pincap;
5505 	unsigned int oldval;
5506 	oldval = snd_hda_codec_read(codec, pin, 0,
5507 				    AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5508 	pincap = snd_hda_query_pin_caps(codec, pin);
5509 	pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5510 	/* Exception: if the default pin setup is vref50, we give it priority */
5511 	if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5512 		return AC_PINCTL_VREF_80;
5513 	else if (pincap & AC_PINCAP_VREF_50)
5514 		return AC_PINCTL_VREF_50;
5515 	else if (pincap & AC_PINCAP_VREF_100)
5516 		return AC_PINCTL_VREF_100;
5517 	else if (pincap & AC_PINCAP_VREF_GRD)
5518 		return AC_PINCTL_VREF_GRD;
5519 	return AC_PINCTL_VREF_HIZ;
5520 }
5521 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
5522 
5523 /* correct the pin ctl value for matching with the pin cap */
snd_hda_correct_pin_ctl(struct hda_codec * codec,hda_nid_t pin,unsigned int val)5524 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5525 				     hda_nid_t pin, unsigned int val)
5526 {
5527 	static unsigned int cap_lists[][2] = {
5528 		{ AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5529 		{ AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5530 		{ AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5531 		{ AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5532 	};
5533 	unsigned int cap;
5534 
5535 	if (!val)
5536 		return 0;
5537 	cap = snd_hda_query_pin_caps(codec, pin);
5538 	if (!cap)
5539 		return val; /* don't know what to do... */
5540 
5541 	if (val & AC_PINCTL_OUT_EN) {
5542 		if (!(cap & AC_PINCAP_OUT))
5543 			val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5544 		else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5545 			val &= ~AC_PINCTL_HP_EN;
5546 	}
5547 
5548 	if (val & AC_PINCTL_IN_EN) {
5549 		if (!(cap & AC_PINCAP_IN))
5550 			val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5551 		else {
5552 			unsigned int vcap, vref;
5553 			int i;
5554 			vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5555 			vref = val & AC_PINCTL_VREFEN;
5556 			for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5557 				if (vref == cap_lists[i][0] &&
5558 				    !(vcap & cap_lists[i][1])) {
5559 					if (i == ARRAY_SIZE(cap_lists) - 1)
5560 						vref = AC_PINCTL_VREF_HIZ;
5561 					else
5562 						vref = cap_lists[i + 1][0];
5563 				}
5564 			}
5565 			val &= ~AC_PINCTL_VREFEN;
5566 			val |= vref;
5567 		}
5568 	}
5569 
5570 	return val;
5571 }
5572 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
5573 
_snd_hda_set_pin_ctl(struct hda_codec * codec,hda_nid_t pin,unsigned int val,bool cached)5574 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5575 			 unsigned int val, bool cached)
5576 {
5577 	val = snd_hda_correct_pin_ctl(codec, pin, val);
5578 	snd_hda_codec_set_pin_target(codec, pin, val);
5579 	if (cached)
5580 		return snd_hda_codec_update_cache(codec, pin, 0,
5581 				AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5582 	else
5583 		return snd_hda_codec_write(codec, pin, 0,
5584 					   AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5585 }
5586 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
5587 
5588 /**
5589  * snd_hda_add_imux_item - Add an item to input_mux
5590  *
5591  * When the same label is used already in the existing items, the number
5592  * suffix is appended to the label.  This label index number is stored
5593  * to type_idx when non-NULL pointer is given.
5594  */
snd_hda_add_imux_item(struct hda_codec * codec,struct hda_input_mux * imux,const char * label,int index,int * type_idx)5595 int snd_hda_add_imux_item(struct hda_codec *codec,
5596 			  struct hda_input_mux *imux, const char *label,
5597 			  int index, int *type_idx)
5598 {
5599 	int i, label_idx = 0;
5600 	if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5601 		codec_err(codec, "hda_codec: Too many imux items!\n");
5602 		return -EINVAL;
5603 	}
5604 	for (i = 0; i < imux->num_items; i++) {
5605 		if (!strncmp(label, imux->items[i].label, strlen(label)))
5606 			label_idx++;
5607 	}
5608 	if (type_idx)
5609 		*type_idx = label_idx;
5610 	if (label_idx > 0)
5611 		snprintf(imux->items[imux->num_items].label,
5612 			 sizeof(imux->items[imux->num_items].label),
5613 			 "%s %d", label, label_idx);
5614 	else
5615 		strlcpy(imux->items[imux->num_items].label, label,
5616 			sizeof(imux->items[imux->num_items].label));
5617 	imux->items[imux->num_items].index = index;
5618 	imux->num_items++;
5619 	return 0;
5620 }
5621 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
5622 
5623 
5624 #ifdef CONFIG_PM
5625 /*
5626  * power management
5627  */
5628 
5629 
hda_async_suspend(void * data,async_cookie_t cookie)5630 static void hda_async_suspend(void *data, async_cookie_t cookie)
5631 {
5632 	hda_call_codec_suspend(data, false);
5633 }
5634 
hda_async_resume(void * data,async_cookie_t cookie)5635 static void hda_async_resume(void *data, async_cookie_t cookie)
5636 {
5637 	hda_call_codec_resume(data);
5638 }
5639 
5640 /**
5641  * snd_hda_suspend - suspend the codecs
5642  * @bus: the HDA bus
5643  *
5644  * Returns 0 if successful.
5645  */
snd_hda_suspend(struct hda_bus * bus)5646 int snd_hda_suspend(struct hda_bus *bus)
5647 {
5648 	struct hda_codec *codec;
5649 	ASYNC_DOMAIN_EXCLUSIVE(domain);
5650 
5651 	list_for_each_entry(codec, &bus->codec_list, list) {
5652 		cancel_delayed_work_sync(&codec->jackpoll_work);
5653 		if (hda_codec_is_power_on(codec)) {
5654 			if (bus->num_codecs > 1)
5655 				async_schedule_domain(hda_async_suspend, codec,
5656 						      &domain);
5657 			else
5658 				hda_call_codec_suspend(codec, false);
5659 		}
5660 	}
5661 
5662 	if (bus->num_codecs > 1)
5663 		async_synchronize_full_domain(&domain);
5664 
5665 	return 0;
5666 }
5667 EXPORT_SYMBOL_GPL(snd_hda_suspend);
5668 
5669 /**
5670  * snd_hda_resume - resume the codecs
5671  * @bus: the HDA bus
5672  *
5673  * Returns 0 if successful.
5674  */
snd_hda_resume(struct hda_bus * bus)5675 int snd_hda_resume(struct hda_bus *bus)
5676 {
5677 	struct hda_codec *codec;
5678 	ASYNC_DOMAIN_EXCLUSIVE(domain);
5679 
5680 	list_for_each_entry(codec, &bus->codec_list, list) {
5681 		if (bus->num_codecs > 1)
5682 			async_schedule_domain(hda_async_resume, codec, &domain);
5683 		else
5684 			hda_call_codec_resume(codec);
5685 	}
5686 
5687 	if (bus->num_codecs > 1)
5688 		async_synchronize_full_domain(&domain);
5689 
5690 	return 0;
5691 }
5692 EXPORT_SYMBOL_GPL(snd_hda_resume);
5693 #endif /* CONFIG_PM */
5694 
5695 /*
5696  * generic arrays
5697  */
5698 
5699 /**
5700  * snd_array_new - get a new element from the given array
5701  * @array: the array object
5702  *
5703  * Get a new element from the given array.  If it exceeds the
5704  * pre-allocated array size, re-allocate the array.
5705  *
5706  * Returns NULL if allocation failed.
5707  */
snd_array_new(struct snd_array * array)5708 void *snd_array_new(struct snd_array *array)
5709 {
5710 	if (snd_BUG_ON(!array->elem_size))
5711 		return NULL;
5712 	if (array->used >= array->alloced) {
5713 		int num = array->alloced + array->alloc_align;
5714 		int size = (num + 1) * array->elem_size;
5715 		void *nlist;
5716 		if (snd_BUG_ON(num >= 4096))
5717 			return NULL;
5718 		nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5719 		if (!nlist)
5720 			return NULL;
5721 		array->list = nlist;
5722 		array->alloced = num;
5723 	}
5724 	return snd_array_elem(array, array->used++);
5725 }
5726 EXPORT_SYMBOL_GPL(snd_array_new);
5727 
5728 /**
5729  * snd_array_free - free the given array elements
5730  * @array: the array object
5731  */
snd_array_free(struct snd_array * array)5732 void snd_array_free(struct snd_array *array)
5733 {
5734 	kfree(array->list);
5735 	array->used = 0;
5736 	array->alloced = 0;
5737 	array->list = NULL;
5738 }
5739 EXPORT_SYMBOL_GPL(snd_array_free);
5740 
5741 /**
5742  * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5743  * @pcm: PCM caps bits
5744  * @buf: the string buffer to write
5745  * @buflen: the max buffer length
5746  *
5747  * used by hda_proc.c and hda_eld.c
5748  */
snd_print_pcm_bits(int pcm,char * buf,int buflen)5749 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5750 {
5751 	static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5752 	int i, j;
5753 
5754 	for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5755 		if (pcm & (AC_SUPPCM_BITS_8 << i))
5756 			j += snprintf(buf + j, buflen - j,  " %d", bits[i]);
5757 
5758 	buf[j] = '\0'; /* necessary when j == 0 */
5759 }
5760 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
5761 
5762 MODULE_DESCRIPTION("HDA codec core");
5763 MODULE_LICENSE("GPL");
5764