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/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/module.h>
27 #include <linux/pm.h>
28 #include <linux/pm_runtime.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 #ifdef CONFIG_PM
41 #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
42 #define hda_codec_is_power_on(codec) \
43 (!pm_runtime_suspended(hda_codec_dev(codec)))
44 #else
45 #define codec_in_pm(codec) 0
46 #define hda_codec_is_power_on(codec) 1
47 #endif
48
49 #define codec_has_epss(codec) \
50 ((codec)->core.power_caps & AC_PWRST_EPSS)
51 #define codec_has_clkstop(codec) \
52 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
53
54 /*
55 * Send and receive a verb - passed to exec_verb override for hdac_device
56 */
codec_exec_verb(struct hdac_device * dev,unsigned int cmd,unsigned int flags,unsigned int * res)57 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
58 unsigned int flags, unsigned int *res)
59 {
60 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
61 struct hda_bus *bus = codec->bus;
62 int err;
63
64 if (cmd == ~0)
65 return -1;
66
67 again:
68 snd_hda_power_up_pm(codec);
69 mutex_lock(&bus->core.cmd_mutex);
70 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
71 bus->no_response_fallback = 1;
72 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
73 cmd, res);
74 bus->no_response_fallback = 0;
75 mutex_unlock(&bus->core.cmd_mutex);
76 snd_hda_power_down_pm(codec);
77 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
78 if (bus->response_reset) {
79 codec_dbg(codec,
80 "resetting BUS due to fatal communication error\n");
81 snd_hda_bus_reset(bus);
82 }
83 goto again;
84 }
85 /* clear reset-flag when the communication gets recovered */
86 if (!err || codec_in_pm(codec))
87 bus->response_reset = 0;
88 return err;
89 }
90
91 /**
92 * snd_hda_sequence_write - sequence writes
93 * @codec: the HDA codec
94 * @seq: VERB array to send
95 *
96 * Send the commands sequentially from the given array.
97 * The array must be terminated with NID=0.
98 */
snd_hda_sequence_write(struct hda_codec * codec,const struct hda_verb * seq)99 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
100 {
101 for (; seq->nid; seq++)
102 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
103 }
104 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
105
106 /* connection list element */
107 struct hda_conn_list {
108 struct list_head list;
109 int len;
110 hda_nid_t nid;
111 hda_nid_t conns[0];
112 };
113
114 /* look up the cached results */
115 static struct hda_conn_list *
lookup_conn_list(struct hda_codec * codec,hda_nid_t nid)116 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
117 {
118 struct hda_conn_list *p;
119 list_for_each_entry(p, &codec->conn_list, list) {
120 if (p->nid == nid)
121 return p;
122 }
123 return NULL;
124 }
125
add_conn_list(struct hda_codec * codec,hda_nid_t nid,int len,const hda_nid_t * list)126 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
127 const hda_nid_t *list)
128 {
129 struct hda_conn_list *p;
130
131 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
132 if (!p)
133 return -ENOMEM;
134 p->len = len;
135 p->nid = nid;
136 memcpy(p->conns, list, len * sizeof(hda_nid_t));
137 list_add(&p->list, &codec->conn_list);
138 return 0;
139 }
140
remove_conn_list(struct hda_codec * codec)141 static void remove_conn_list(struct hda_codec *codec)
142 {
143 while (!list_empty(&codec->conn_list)) {
144 struct hda_conn_list *p;
145 p = list_first_entry(&codec->conn_list, typeof(*p), list);
146 list_del(&p->list);
147 kfree(p);
148 }
149 }
150
151 /* read the connection and add to the cache */
read_and_add_raw_conns(struct hda_codec * codec,hda_nid_t nid)152 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
153 {
154 hda_nid_t list[32];
155 hda_nid_t *result = list;
156 int len;
157
158 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
159 if (len == -ENOSPC) {
160 len = snd_hda_get_num_raw_conns(codec, nid);
161 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
162 if (!result)
163 return -ENOMEM;
164 len = snd_hda_get_raw_connections(codec, nid, result, len);
165 }
166 if (len >= 0)
167 len = snd_hda_override_conn_list(codec, nid, len, result);
168 if (result != list)
169 kfree(result);
170 return len;
171 }
172
173 /**
174 * snd_hda_get_conn_list - get connection list
175 * @codec: the HDA codec
176 * @nid: NID to parse
177 * @listp: the pointer to store NID list
178 *
179 * Parses the connection list of the given widget and stores the pointer
180 * to the list of NIDs.
181 *
182 * Returns the number of connections, or a negative error code.
183 *
184 * Note that the returned pointer isn't protected against the list
185 * modification. If snd_hda_override_conn_list() might be called
186 * concurrently, protect with a mutex appropriately.
187 */
snd_hda_get_conn_list(struct hda_codec * codec,hda_nid_t nid,const hda_nid_t ** listp)188 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
189 const hda_nid_t **listp)
190 {
191 bool added = false;
192
193 for (;;) {
194 int err;
195 const struct hda_conn_list *p;
196
197 /* if the connection-list is already cached, read it */
198 p = lookup_conn_list(codec, nid);
199 if (p) {
200 if (listp)
201 *listp = p->conns;
202 return p->len;
203 }
204 if (snd_BUG_ON(added))
205 return -EINVAL;
206
207 err = read_and_add_raw_conns(codec, nid);
208 if (err < 0)
209 return err;
210 added = true;
211 }
212 }
213 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
214
215 /**
216 * snd_hda_get_connections - copy connection list
217 * @codec: the HDA codec
218 * @nid: NID to parse
219 * @conn_list: connection list array; when NULL, checks only the size
220 * @max_conns: max. number of connections to store
221 *
222 * Parses the connection list of the given widget and stores the list
223 * of NIDs.
224 *
225 * Returns the number of connections, or a negative error code.
226 */
snd_hda_get_connections(struct hda_codec * codec,hda_nid_t nid,hda_nid_t * conn_list,int max_conns)227 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
228 hda_nid_t *conn_list, int max_conns)
229 {
230 const hda_nid_t *list;
231 int len = snd_hda_get_conn_list(codec, nid, &list);
232
233 if (len > 0 && conn_list) {
234 if (len > max_conns) {
235 codec_err(codec, "Too many connections %d for NID 0x%x\n",
236 len, nid);
237 return -EINVAL;
238 }
239 memcpy(conn_list, list, len * sizeof(hda_nid_t));
240 }
241
242 return len;
243 }
244 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
245
246 /**
247 * snd_hda_override_conn_list - add/modify the connection-list to cache
248 * @codec: the HDA codec
249 * @nid: NID to parse
250 * @len: number of connection list entries
251 * @list: the list of connection entries
252 *
253 * Add or modify the given connection-list to the cache. If the corresponding
254 * cache already exists, invalidate it and append a new one.
255 *
256 * Returns zero or a negative error code.
257 */
snd_hda_override_conn_list(struct hda_codec * codec,hda_nid_t nid,int len,const hda_nid_t * list)258 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
259 const hda_nid_t *list)
260 {
261 struct hda_conn_list *p;
262
263 p = lookup_conn_list(codec, nid);
264 if (p) {
265 list_del(&p->list);
266 kfree(p);
267 }
268
269 return add_conn_list(codec, nid, len, list);
270 }
271 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
272
273 /**
274 * snd_hda_get_conn_index - get the connection index of the given NID
275 * @codec: the HDA codec
276 * @mux: NID containing the list
277 * @nid: NID to select
278 * @recursive: 1 when searching NID recursively, otherwise 0
279 *
280 * Parses the connection list of the widget @mux and checks whether the
281 * widget @nid is present. If it is, return the connection index.
282 * Otherwise it returns -1.
283 */
snd_hda_get_conn_index(struct hda_codec * codec,hda_nid_t mux,hda_nid_t nid,int recursive)284 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
285 hda_nid_t nid, int recursive)
286 {
287 const hda_nid_t *conn;
288 int i, nums;
289
290 nums = snd_hda_get_conn_list(codec, mux, &conn);
291 for (i = 0; i < nums; i++)
292 if (conn[i] == nid)
293 return i;
294 if (!recursive)
295 return -1;
296 if (recursive > 10) {
297 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
298 return -1;
299 }
300 recursive++;
301 for (i = 0; i < nums; i++) {
302 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
303 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
304 continue;
305 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
306 return i;
307 }
308 return -1;
309 }
310 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
311
312 /**
313 * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget
314 * @codec: the HDA codec
315 * @nid: NID of the pin to parse
316 *
317 * Get the device entry number on the given widget. This is a feature of
318 * DP MST audio. Each pin can have several device entries in it.
319 */
snd_hda_get_num_devices(struct hda_codec * codec,hda_nid_t nid)320 unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid)
321 {
322 unsigned int wcaps = get_wcaps(codec, nid);
323 unsigned int parm;
324
325 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
326 get_wcaps_type(wcaps) != AC_WID_PIN)
327 return 0;
328
329 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
330 if (parm == -1)
331 parm = 0;
332 return parm & AC_DEV_LIST_LEN_MASK;
333 }
334 EXPORT_SYMBOL_GPL(snd_hda_get_num_devices);
335
336 /**
337 * snd_hda_get_devices - copy device list without cache
338 * @codec: the HDA codec
339 * @nid: NID of the pin to parse
340 * @dev_list: device list array
341 * @max_devices: max. number of devices to store
342 *
343 * Copy the device list. This info is dynamic and so not cached.
344 * Currently called only from hda_proc.c, so not exported.
345 */
snd_hda_get_devices(struct hda_codec * codec,hda_nid_t nid,u8 * dev_list,int max_devices)346 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
347 u8 *dev_list, int max_devices)
348 {
349 unsigned int parm;
350 int i, dev_len, devices;
351
352 parm = snd_hda_get_num_devices(codec, nid);
353 if (!parm) /* not multi-stream capable */
354 return 0;
355
356 dev_len = parm + 1;
357 dev_len = dev_len < max_devices ? dev_len : max_devices;
358
359 devices = 0;
360 while (devices < dev_len) {
361 if (snd_hdac_read(&codec->core, nid,
362 AC_VERB_GET_DEVICE_LIST, devices, &parm))
363 break; /* error */
364
365 for (i = 0; i < 8; i++) {
366 dev_list[devices] = (u8)parm;
367 parm >>= 4;
368 devices++;
369 if (devices >= dev_len)
370 break;
371 }
372 }
373 return devices;
374 }
375
376 /**
377 * snd_hda_get_dev_select - get device entry select on the pin
378 * @codec: the HDA codec
379 * @nid: NID of the pin to get device entry select
380 *
381 * Get the devcie entry select on the pin. Return the device entry
382 * id selected on the pin. Return 0 means the first device entry
383 * is selected or MST is not supported.
384 */
snd_hda_get_dev_select(struct hda_codec * codec,hda_nid_t nid)385 int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid)
386 {
387 /* not support dp_mst will always return 0, using first dev_entry */
388 if (!codec->dp_mst)
389 return 0;
390
391 return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0);
392 }
393 EXPORT_SYMBOL_GPL(snd_hda_get_dev_select);
394
395 /**
396 * snd_hda_set_dev_select - set device entry select on the pin
397 * @codec: the HDA codec
398 * @nid: NID of the pin to set device entry select
399 * @dev_id: device entry id to be set
400 *
401 * Set the device entry select on the pin nid.
402 */
snd_hda_set_dev_select(struct hda_codec * codec,hda_nid_t nid,int dev_id)403 int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id)
404 {
405 int ret, num_devices;
406
407 /* not support dp_mst will always return 0, using first dev_entry */
408 if (!codec->dp_mst)
409 return 0;
410
411 /* AC_PAR_DEVLIST_LEN is 0 based. */
412 num_devices = snd_hda_get_num_devices(codec, nid) + 1;
413 /* If Device List Length is 0 (num_device = 1),
414 * the pin is not multi stream capable.
415 * Do nothing in this case.
416 */
417 if (num_devices == 1)
418 return 0;
419
420 /* Behavior of setting index being equal to or greater than
421 * Device List Length is not predictable
422 */
423 if (num_devices <= dev_id)
424 return -EINVAL;
425
426 ret = snd_hda_codec_write(codec, nid, 0,
427 AC_VERB_SET_DEVICE_SEL, dev_id);
428
429 return ret;
430 }
431 EXPORT_SYMBOL_GPL(snd_hda_set_dev_select);
432
433 /*
434 * read widget caps for each widget and store in cache
435 */
read_widget_caps(struct hda_codec * codec,hda_nid_t fg_node)436 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
437 {
438 int i;
439 hda_nid_t nid;
440
441 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
442 if (!codec->wcaps)
443 return -ENOMEM;
444 nid = codec->core.start_nid;
445 for (i = 0; i < codec->core.num_nodes; i++, nid++)
446 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
447 nid, AC_PAR_AUDIO_WIDGET_CAP);
448 return 0;
449 }
450
451 /* read all pin default configurations and save codec->init_pins */
read_pin_defaults(struct hda_codec * codec)452 static int read_pin_defaults(struct hda_codec *codec)
453 {
454 hda_nid_t nid;
455
456 for_each_hda_codec_node(nid, codec) {
457 struct hda_pincfg *pin;
458 unsigned int wcaps = get_wcaps(codec, nid);
459 unsigned int wid_type = get_wcaps_type(wcaps);
460 if (wid_type != AC_WID_PIN)
461 continue;
462 pin = snd_array_new(&codec->init_pins);
463 if (!pin)
464 return -ENOMEM;
465 pin->nid = nid;
466 pin->cfg = snd_hda_codec_read(codec, nid, 0,
467 AC_VERB_GET_CONFIG_DEFAULT, 0);
468 /*
469 * all device entries are the same widget control so far
470 * fixme: if any codec is different, need fix here
471 */
472 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
473 AC_VERB_GET_PIN_WIDGET_CONTROL,
474 0);
475 }
476 return 0;
477 }
478
479 /* 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)480 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
481 struct snd_array *array,
482 hda_nid_t nid)
483 {
484 int i;
485 for (i = 0; i < array->used; i++) {
486 struct hda_pincfg *pin = snd_array_elem(array, i);
487 if (pin->nid == nid)
488 return pin;
489 }
490 return NULL;
491 }
492
493 /* set the current pin config value for the given NID.
494 * the value is cached, and read via snd_hda_codec_get_pincfg()
495 */
snd_hda_add_pincfg(struct hda_codec * codec,struct snd_array * list,hda_nid_t nid,unsigned int cfg)496 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
497 hda_nid_t nid, unsigned int cfg)
498 {
499 struct hda_pincfg *pin;
500
501 /* the check below may be invalid when pins are added by a fixup
502 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
503 * for now
504 */
505 /*
506 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
507 return -EINVAL;
508 */
509
510 pin = look_up_pincfg(codec, list, nid);
511 if (!pin) {
512 pin = snd_array_new(list);
513 if (!pin)
514 return -ENOMEM;
515 pin->nid = nid;
516 }
517 pin->cfg = cfg;
518 return 0;
519 }
520
521 /**
522 * snd_hda_codec_set_pincfg - Override a pin default configuration
523 * @codec: the HDA codec
524 * @nid: NID to set the pin config
525 * @cfg: the pin default config value
526 *
527 * Override a pin default configuration value in the cache.
528 * This value can be read by snd_hda_codec_get_pincfg() in a higher
529 * priority than the real hardware value.
530 */
snd_hda_codec_set_pincfg(struct hda_codec * codec,hda_nid_t nid,unsigned int cfg)531 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
532 hda_nid_t nid, unsigned int cfg)
533 {
534 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
535 }
536 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
537
538 /**
539 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
540 * @codec: the HDA codec
541 * @nid: NID to get the pin config
542 *
543 * Get the current pin config value of the given pin NID.
544 * If the pincfg value is cached or overridden via sysfs or driver,
545 * returns the cached value.
546 */
snd_hda_codec_get_pincfg(struct hda_codec * codec,hda_nid_t nid)547 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
548 {
549 struct hda_pincfg *pin;
550
551 #ifdef CONFIG_SND_HDA_RECONFIG
552 {
553 unsigned int cfg = 0;
554 mutex_lock(&codec->user_mutex);
555 pin = look_up_pincfg(codec, &codec->user_pins, nid);
556 if (pin)
557 cfg = pin->cfg;
558 mutex_unlock(&codec->user_mutex);
559 if (cfg)
560 return cfg;
561 }
562 #endif
563 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
564 if (pin)
565 return pin->cfg;
566 pin = look_up_pincfg(codec, &codec->init_pins, nid);
567 if (pin)
568 return pin->cfg;
569 return 0;
570 }
571 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
572
573 /**
574 * snd_hda_codec_set_pin_target - remember the current pinctl target value
575 * @codec: the HDA codec
576 * @nid: pin NID
577 * @val: assigned pinctl value
578 *
579 * This function stores the given value to a pinctl target value in the
580 * pincfg table. This isn't always as same as the actually written value
581 * but can be referred at any time via snd_hda_codec_get_pin_target().
582 */
snd_hda_codec_set_pin_target(struct hda_codec * codec,hda_nid_t nid,unsigned int val)583 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
584 unsigned int val)
585 {
586 struct hda_pincfg *pin;
587
588 pin = look_up_pincfg(codec, &codec->init_pins, nid);
589 if (!pin)
590 return -EINVAL;
591 pin->target = val;
592 return 0;
593 }
594 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
595
596 /**
597 * snd_hda_codec_get_pin_target - return the current pinctl target value
598 * @codec: the HDA codec
599 * @nid: pin NID
600 */
snd_hda_codec_get_pin_target(struct hda_codec * codec,hda_nid_t nid)601 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
602 {
603 struct hda_pincfg *pin;
604
605 pin = look_up_pincfg(codec, &codec->init_pins, nid);
606 if (!pin)
607 return 0;
608 return pin->target;
609 }
610 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
611
612 /**
613 * snd_hda_shutup_pins - Shut up all pins
614 * @codec: the HDA codec
615 *
616 * Clear all pin controls to shup up before suspend for avoiding click noise.
617 * The controls aren't cached so that they can be resumed properly.
618 */
snd_hda_shutup_pins(struct hda_codec * codec)619 void snd_hda_shutup_pins(struct hda_codec *codec)
620 {
621 int i;
622 /* don't shut up pins when unloading the driver; otherwise it breaks
623 * the default pin setup at the next load of the driver
624 */
625 if (codec->bus->shutdown)
626 return;
627 for (i = 0; i < codec->init_pins.used; i++) {
628 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
629 /* use read here for syncing after issuing each verb */
630 snd_hda_codec_read(codec, pin->nid, 0,
631 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
632 }
633 codec->pins_shutup = 1;
634 }
635 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
636
637 #ifdef CONFIG_PM
638 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
restore_shutup_pins(struct hda_codec * codec)639 static void restore_shutup_pins(struct hda_codec *codec)
640 {
641 int i;
642 if (!codec->pins_shutup)
643 return;
644 if (codec->bus->shutdown)
645 return;
646 for (i = 0; i < codec->init_pins.used; i++) {
647 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
648 snd_hda_codec_write(codec, pin->nid, 0,
649 AC_VERB_SET_PIN_WIDGET_CONTROL,
650 pin->ctrl);
651 }
652 codec->pins_shutup = 0;
653 }
654 #endif
655
hda_jackpoll_work(struct work_struct * work)656 static void hda_jackpoll_work(struct work_struct *work)
657 {
658 struct hda_codec *codec =
659 container_of(work, struct hda_codec, jackpoll_work.work);
660
661 snd_hda_jack_set_dirty_all(codec);
662 snd_hda_jack_poll_all(codec);
663
664 if (!codec->jackpoll_interval)
665 return;
666
667 schedule_delayed_work(&codec->jackpoll_work,
668 codec->jackpoll_interval);
669 }
670
671 /* release all pincfg lists */
free_init_pincfgs(struct hda_codec * codec)672 static void free_init_pincfgs(struct hda_codec *codec)
673 {
674 snd_array_free(&codec->driver_pins);
675 #ifdef CONFIG_SND_HDA_RECONFIG
676 snd_array_free(&codec->user_pins);
677 #endif
678 snd_array_free(&codec->init_pins);
679 }
680
681 /*
682 * audio-converter setup caches
683 */
684 struct hda_cvt_setup {
685 hda_nid_t nid;
686 u8 stream_tag;
687 u8 channel_id;
688 u16 format_id;
689 unsigned char active; /* cvt is currently used */
690 unsigned char dirty; /* setups should be cleared */
691 };
692
693 /* get or create a cache entry for the given audio converter NID */
694 static struct hda_cvt_setup *
get_hda_cvt_setup(struct hda_codec * codec,hda_nid_t nid)695 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
696 {
697 struct hda_cvt_setup *p;
698 int i;
699
700 for (i = 0; i < codec->cvt_setups.used; i++) {
701 p = snd_array_elem(&codec->cvt_setups, i);
702 if (p->nid == nid)
703 return p;
704 }
705 p = snd_array_new(&codec->cvt_setups);
706 if (p)
707 p->nid = nid;
708 return p;
709 }
710
711 /*
712 * PCM device
713 */
release_pcm(struct kref * kref)714 static void release_pcm(struct kref *kref)
715 {
716 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
717
718 if (pcm->pcm)
719 snd_device_free(pcm->codec->card, pcm->pcm);
720 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
721 kfree(pcm->name);
722 kfree(pcm);
723 }
724
snd_hda_codec_pcm_put(struct hda_pcm * pcm)725 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
726 {
727 kref_put(&pcm->kref, release_pcm);
728 }
729 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
730
snd_hda_codec_pcm_new(struct hda_codec * codec,const char * fmt,...)731 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
732 const char *fmt, ...)
733 {
734 struct hda_pcm *pcm;
735 va_list args;
736
737 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
738 if (!pcm)
739 return NULL;
740
741 pcm->codec = codec;
742 kref_init(&pcm->kref);
743 va_start(args, fmt);
744 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
745 va_end(args);
746 if (!pcm->name) {
747 kfree(pcm);
748 return NULL;
749 }
750
751 list_add_tail(&pcm->list, &codec->pcm_list_head);
752 return pcm;
753 }
754 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
755
756 /*
757 * codec destructor
758 */
codec_release_pcms(struct hda_codec * codec)759 static void codec_release_pcms(struct hda_codec *codec)
760 {
761 struct hda_pcm *pcm, *n;
762
763 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
764 list_del_init(&pcm->list);
765 if (pcm->pcm)
766 snd_device_disconnect(codec->card, pcm->pcm);
767 snd_hda_codec_pcm_put(pcm);
768 }
769 }
770
snd_hda_codec_cleanup_for_unbind(struct hda_codec * codec)771 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
772 {
773 if (codec->registered) {
774 /* pm_runtime_put() is called in snd_hdac_device_exit() */
775 pm_runtime_get_noresume(hda_codec_dev(codec));
776 pm_runtime_disable(hda_codec_dev(codec));
777 codec->registered = 0;
778 }
779
780 cancel_delayed_work_sync(&codec->jackpoll_work);
781 if (!codec->in_freeing)
782 snd_hda_ctls_clear(codec);
783 codec_release_pcms(codec);
784 snd_hda_detach_beep_device(codec);
785 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
786 snd_hda_jack_tbl_clear(codec);
787 codec->proc_widget_hook = NULL;
788 codec->spec = NULL;
789
790 /* free only driver_pins so that init_pins + user_pins are restored */
791 snd_array_free(&codec->driver_pins);
792 snd_array_free(&codec->cvt_setups);
793 snd_array_free(&codec->spdif_out);
794 snd_array_free(&codec->verbs);
795 codec->preset = NULL;
796 codec->slave_dig_outs = NULL;
797 codec->spdif_status_reset = 0;
798 snd_array_free(&codec->mixers);
799 snd_array_free(&codec->nids);
800 remove_conn_list(codec);
801 snd_hdac_regmap_exit(&codec->core);
802 }
803
804 static unsigned int hda_set_power_state(struct hda_codec *codec,
805 unsigned int power_state);
806
807 /* also called from hda_bind.c */
snd_hda_codec_register(struct hda_codec * codec)808 void snd_hda_codec_register(struct hda_codec *codec)
809 {
810 if (codec->registered)
811 return;
812 if (device_is_registered(hda_codec_dev(codec))) {
813 snd_hda_register_beep_device(codec);
814 snd_hdac_link_power(&codec->core, true);
815 pm_runtime_enable(hda_codec_dev(codec));
816 /* it was powered up in snd_hda_codec_new(), now all done */
817 snd_hda_power_down(codec);
818 codec->registered = 1;
819 }
820 }
821
snd_hda_codec_dev_register(struct snd_device * device)822 static int snd_hda_codec_dev_register(struct snd_device *device)
823 {
824 snd_hda_codec_register(device->device_data);
825 return 0;
826 }
827
snd_hda_codec_dev_disconnect(struct snd_device * device)828 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
829 {
830 struct hda_codec *codec = device->device_data;
831
832 snd_hda_detach_beep_device(codec);
833 return 0;
834 }
835
snd_hda_codec_dev_free(struct snd_device * device)836 static int snd_hda_codec_dev_free(struct snd_device *device)
837 {
838 struct hda_codec *codec = device->device_data;
839
840 codec->in_freeing = 1;
841 snd_hdac_device_unregister(&codec->core);
842 snd_hdac_link_power(&codec->core, false);
843 put_device(hda_codec_dev(codec));
844 return 0;
845 }
846
snd_hda_codec_dev_release(struct device * dev)847 static void snd_hda_codec_dev_release(struct device *dev)
848 {
849 struct hda_codec *codec = dev_to_hda_codec(dev);
850
851 free_init_pincfgs(codec);
852 snd_hdac_device_exit(&codec->core);
853 snd_hda_sysfs_clear(codec);
854 kfree(codec->modelname);
855 kfree(codec->wcaps);
856 kfree(codec);
857 }
858
859 /**
860 * snd_hda_codec_new - create a HDA codec
861 * @bus: the bus to assign
862 * @codec_addr: the codec address
863 * @codecp: the pointer to store the generated codec
864 *
865 * Returns 0 if successful, or a negative error code.
866 */
snd_hda_codec_new(struct hda_bus * bus,struct snd_card * card,unsigned int codec_addr,struct hda_codec ** codecp)867 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
868 unsigned int codec_addr, struct hda_codec **codecp)
869 {
870 struct hda_codec *codec;
871 char component[31];
872 hda_nid_t fg;
873 int err;
874 static struct snd_device_ops dev_ops = {
875 .dev_register = snd_hda_codec_dev_register,
876 .dev_disconnect = snd_hda_codec_dev_disconnect,
877 .dev_free = snd_hda_codec_dev_free,
878 };
879
880 if (snd_BUG_ON(!bus))
881 return -EINVAL;
882 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
883 return -EINVAL;
884
885 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
886 if (!codec)
887 return -ENOMEM;
888
889 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
890 err = snd_hdac_device_init(&codec->core, &bus->core, component,
891 codec_addr);
892 if (err < 0) {
893 kfree(codec);
894 return err;
895 }
896
897 codec->core.dev.release = snd_hda_codec_dev_release;
898 codec->core.type = HDA_DEV_LEGACY;
899 codec->core.exec_verb = codec_exec_verb;
900
901 codec->bus = bus;
902 codec->card = card;
903 codec->addr = codec_addr;
904 mutex_init(&codec->spdif_mutex);
905 mutex_init(&codec->control_mutex);
906 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
907 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
908 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
909 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
910 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
911 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
912 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
913 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
914 INIT_LIST_HEAD(&codec->conn_list);
915 INIT_LIST_HEAD(&codec->pcm_list_head);
916
917 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
918 codec->depop_delay = -1;
919 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
920
921 #ifdef CONFIG_PM
922 codec->power_jiffies = jiffies;
923 #endif
924
925 snd_hda_sysfs_init(codec);
926
927 if (codec->bus->modelname) {
928 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
929 if (!codec->modelname) {
930 err = -ENOMEM;
931 goto error;
932 }
933 }
934
935 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
936 err = read_widget_caps(codec, fg);
937 if (err < 0)
938 goto error;
939 err = read_pin_defaults(codec);
940 if (err < 0)
941 goto error;
942
943 /* power-up all before initialization */
944 hda_set_power_state(codec, AC_PWRST_D0);
945
946 snd_hda_codec_proc_new(codec);
947
948 snd_hda_create_hwdep(codec);
949
950 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
951 codec->core.subsystem_id, codec->core.revision_id);
952 snd_component_add(card, component);
953
954 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
955 if (err < 0)
956 goto error;
957
958 if (codecp)
959 *codecp = codec;
960 return 0;
961
962 error:
963 put_device(hda_codec_dev(codec));
964 return err;
965 }
966 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
967
968 /**
969 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
970 * @codec: the HDA codec
971 *
972 * Forcibly refresh the all widget caps and the init pin configurations of
973 * the given codec.
974 */
snd_hda_codec_update_widgets(struct hda_codec * codec)975 int snd_hda_codec_update_widgets(struct hda_codec *codec)
976 {
977 hda_nid_t fg;
978 int err;
979
980 err = snd_hdac_refresh_widget_sysfs(&codec->core);
981 if (err < 0)
982 return err;
983
984 /* Assume the function group node does not change,
985 * only the widget nodes may change.
986 */
987 kfree(codec->wcaps);
988 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
989 err = read_widget_caps(codec, fg);
990 if (err < 0)
991 return err;
992
993 snd_array_free(&codec->init_pins);
994 err = read_pin_defaults(codec);
995
996 return err;
997 }
998 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
999
1000 /* 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)1001 static void update_pcm_stream_id(struct hda_codec *codec,
1002 struct hda_cvt_setup *p, hda_nid_t nid,
1003 u32 stream_tag, int channel_id)
1004 {
1005 unsigned int oldval, newval;
1006
1007 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1008 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1009 newval = (stream_tag << 4) | channel_id;
1010 if (oldval != newval)
1011 snd_hda_codec_write(codec, nid, 0,
1012 AC_VERB_SET_CHANNEL_STREAMID,
1013 newval);
1014 p->stream_tag = stream_tag;
1015 p->channel_id = channel_id;
1016 }
1017 }
1018
1019 /* update the format-id if changed */
update_pcm_format(struct hda_codec * codec,struct hda_cvt_setup * p,hda_nid_t nid,int format)1020 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1021 hda_nid_t nid, int format)
1022 {
1023 unsigned int oldval;
1024
1025 if (p->format_id != format) {
1026 oldval = snd_hda_codec_read(codec, nid, 0,
1027 AC_VERB_GET_STREAM_FORMAT, 0);
1028 if (oldval != format) {
1029 msleep(1);
1030 snd_hda_codec_write(codec, nid, 0,
1031 AC_VERB_SET_STREAM_FORMAT,
1032 format);
1033 }
1034 p->format_id = format;
1035 }
1036 }
1037
1038 /**
1039 * snd_hda_codec_setup_stream - set up the codec for streaming
1040 * @codec: the CODEC to set up
1041 * @nid: the NID to set up
1042 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1043 * @channel_id: channel id to pass, zero based.
1044 * @format: stream format.
1045 */
snd_hda_codec_setup_stream(struct hda_codec * codec,hda_nid_t nid,u32 stream_tag,int channel_id,int format)1046 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1047 u32 stream_tag,
1048 int channel_id, int format)
1049 {
1050 struct hda_codec *c;
1051 struct hda_cvt_setup *p;
1052 int type;
1053 int i;
1054
1055 if (!nid)
1056 return;
1057
1058 codec_dbg(codec,
1059 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1060 nid, stream_tag, channel_id, format);
1061 p = get_hda_cvt_setup(codec, nid);
1062 if (!p)
1063 return;
1064
1065 if (codec->patch_ops.stream_pm)
1066 codec->patch_ops.stream_pm(codec, nid, true);
1067 if (codec->pcm_format_first)
1068 update_pcm_format(codec, p, nid, format);
1069 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1070 if (!codec->pcm_format_first)
1071 update_pcm_format(codec, p, nid, format);
1072
1073 p->active = 1;
1074 p->dirty = 0;
1075
1076 /* make other inactive cvts with the same stream-tag dirty */
1077 type = get_wcaps_type(get_wcaps(codec, nid));
1078 list_for_each_codec(c, codec->bus) {
1079 for (i = 0; i < c->cvt_setups.used; i++) {
1080 p = snd_array_elem(&c->cvt_setups, i);
1081 if (!p->active && p->stream_tag == stream_tag &&
1082 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1083 p->dirty = 1;
1084 }
1085 }
1086 }
1087 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1088
1089 static void really_cleanup_stream(struct hda_codec *codec,
1090 struct hda_cvt_setup *q);
1091
1092 /**
1093 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1094 * @codec: the CODEC to clean up
1095 * @nid: the NID to clean up
1096 * @do_now: really clean up the stream instead of clearing the active flag
1097 */
__snd_hda_codec_cleanup_stream(struct hda_codec * codec,hda_nid_t nid,int do_now)1098 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1099 int do_now)
1100 {
1101 struct hda_cvt_setup *p;
1102
1103 if (!nid)
1104 return;
1105
1106 if (codec->no_sticky_stream)
1107 do_now = 1;
1108
1109 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1110 p = get_hda_cvt_setup(codec, nid);
1111 if (p) {
1112 /* here we just clear the active flag when do_now isn't set;
1113 * actual clean-ups will be done later in
1114 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1115 */
1116 if (do_now)
1117 really_cleanup_stream(codec, p);
1118 else
1119 p->active = 0;
1120 }
1121 }
1122 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1123
really_cleanup_stream(struct hda_codec * codec,struct hda_cvt_setup * q)1124 static void really_cleanup_stream(struct hda_codec *codec,
1125 struct hda_cvt_setup *q)
1126 {
1127 hda_nid_t nid = q->nid;
1128 if (q->stream_tag || q->channel_id)
1129 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1130 if (q->format_id)
1131 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1132 );
1133 memset(q, 0, sizeof(*q));
1134 q->nid = nid;
1135 if (codec->patch_ops.stream_pm)
1136 codec->patch_ops.stream_pm(codec, nid, false);
1137 }
1138
1139 /* clean up the all conflicting obsolete streams */
purify_inactive_streams(struct hda_codec * codec)1140 static void purify_inactive_streams(struct hda_codec *codec)
1141 {
1142 struct hda_codec *c;
1143 int i;
1144
1145 list_for_each_codec(c, codec->bus) {
1146 for (i = 0; i < c->cvt_setups.used; i++) {
1147 struct hda_cvt_setup *p;
1148 p = snd_array_elem(&c->cvt_setups, i);
1149 if (p->dirty)
1150 really_cleanup_stream(c, p);
1151 }
1152 }
1153 }
1154
1155 #ifdef CONFIG_PM
1156 /* clean up all streams; called from suspend */
hda_cleanup_all_streams(struct hda_codec * codec)1157 static void hda_cleanup_all_streams(struct hda_codec *codec)
1158 {
1159 int i;
1160
1161 for (i = 0; i < codec->cvt_setups.used; i++) {
1162 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1163 if (p->stream_tag)
1164 really_cleanup_stream(codec, p);
1165 }
1166 }
1167 #endif
1168
1169 /*
1170 * amp access functions
1171 */
1172
1173 /**
1174 * query_amp_caps - query AMP capabilities
1175 * @codec: the HD-auio codec
1176 * @nid: the NID to query
1177 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1178 *
1179 * Query AMP capabilities for the given widget and direction.
1180 * Returns the obtained capability bits.
1181 *
1182 * When cap bits have been already read, this doesn't read again but
1183 * returns the cached value.
1184 */
query_amp_caps(struct hda_codec * codec,hda_nid_t nid,int direction)1185 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1186 {
1187 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1188 nid = codec->core.afg;
1189 return snd_hda_param_read(codec, nid,
1190 direction == HDA_OUTPUT ?
1191 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1192 }
1193 EXPORT_SYMBOL_GPL(query_amp_caps);
1194
1195 /**
1196 * snd_hda_check_amp_caps - query AMP capabilities
1197 * @codec: the HD-audio codec
1198 * @nid: the NID to query
1199 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1200 * @bits: bit mask to check the result
1201 *
1202 * Check whether the widget has the given amp capability for the direction.
1203 */
snd_hda_check_amp_caps(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int bits)1204 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1205 int dir, unsigned int bits)
1206 {
1207 if (!nid)
1208 return false;
1209 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1210 if (query_amp_caps(codec, nid, dir) & bits)
1211 return true;
1212 return false;
1213 }
1214 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1215
1216 /**
1217 * snd_hda_override_amp_caps - Override the AMP capabilities
1218 * @codec: the CODEC to clean up
1219 * @nid: the NID to clean up
1220 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1221 * @caps: the capability bits to set
1222 *
1223 * Override the cached AMP caps bits value by the given one.
1224 * This function is useful if the driver needs to adjust the AMP ranges,
1225 * e.g. limit to 0dB, etc.
1226 *
1227 * Returns zero if successful or a negative error code.
1228 */
snd_hda_override_amp_caps(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int caps)1229 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1230 unsigned int caps)
1231 {
1232 unsigned int parm;
1233
1234 snd_hda_override_wcaps(codec, nid,
1235 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1236 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1237 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1238 }
1239 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1240
1241 /**
1242 * snd_hda_codec_amp_update - update the AMP mono value
1243 * @codec: HD-audio codec
1244 * @nid: NID to read the AMP value
1245 * @ch: channel to update (0 or 1)
1246 * @dir: #HDA_INPUT or #HDA_OUTPUT
1247 * @idx: the index value (only for input direction)
1248 * @mask: bit mask to set
1249 * @val: the bits value to set
1250 *
1251 * Update the AMP values for the given channel, direction and index.
1252 */
snd_hda_codec_amp_update(struct hda_codec * codec,hda_nid_t nid,int ch,int dir,int idx,int mask,int val)1253 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1254 int ch, int dir, int idx, int mask, int val)
1255 {
1256 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1257
1258 /* enable fake mute if no h/w mute but min=mute */
1259 if ((query_amp_caps(codec, nid, dir) &
1260 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1261 cmd |= AC_AMP_FAKE_MUTE;
1262 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1263 }
1264 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1265
1266 /**
1267 * snd_hda_codec_amp_stereo - update the AMP stereo values
1268 * @codec: HD-audio codec
1269 * @nid: NID to read the AMP value
1270 * @direction: #HDA_INPUT or #HDA_OUTPUT
1271 * @idx: the index value (only for input direction)
1272 * @mask: bit mask to set
1273 * @val: the bits value to set
1274 *
1275 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1276 * stereo widget with the same mask and value.
1277 */
snd_hda_codec_amp_stereo(struct hda_codec * codec,hda_nid_t nid,int direction,int idx,int mask,int val)1278 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1279 int direction, int idx, int mask, int val)
1280 {
1281 int ch, ret = 0;
1282
1283 if (snd_BUG_ON(mask & ~0xff))
1284 mask &= 0xff;
1285 for (ch = 0; ch < 2; ch++)
1286 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1287 idx, mask, val);
1288 return ret;
1289 }
1290 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1291
1292 /**
1293 * snd_hda_codec_amp_init - initialize the AMP value
1294 * @codec: the HDA codec
1295 * @nid: NID to read the AMP value
1296 * @ch: channel (left=0 or right=1)
1297 * @dir: #HDA_INPUT or #HDA_OUTPUT
1298 * @idx: the index value (only for input direction)
1299 * @mask: bit mask to set
1300 * @val: the bits value to set
1301 *
1302 * Works like snd_hda_codec_amp_update() but it writes the value only at
1303 * the first access. If the amp was already initialized / updated beforehand,
1304 * this does nothing.
1305 */
snd_hda_codec_amp_init(struct hda_codec * codec,hda_nid_t nid,int ch,int dir,int idx,int mask,int val)1306 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1307 int dir, int idx, int mask, int val)
1308 {
1309 int orig;
1310
1311 if (!codec->core.regmap)
1312 return -EINVAL;
1313 regcache_cache_only(codec->core.regmap, true);
1314 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1315 regcache_cache_only(codec->core.regmap, false);
1316 if (orig >= 0)
1317 return 0;
1318 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1319 }
1320 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1321
1322 /**
1323 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1324 * @codec: the HDA codec
1325 * @nid: NID to read the AMP value
1326 * @dir: #HDA_INPUT or #HDA_OUTPUT
1327 * @idx: the index value (only for input direction)
1328 * @mask: bit mask to set
1329 * @val: the bits value to set
1330 *
1331 * Call snd_hda_codec_amp_init() for both stereo channels.
1332 */
snd_hda_codec_amp_init_stereo(struct hda_codec * codec,hda_nid_t nid,int dir,int idx,int mask,int val)1333 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1334 int dir, int idx, int mask, int val)
1335 {
1336 int ch, ret = 0;
1337
1338 if (snd_BUG_ON(mask & ~0xff))
1339 mask &= 0xff;
1340 for (ch = 0; ch < 2; ch++)
1341 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1342 idx, mask, val);
1343 return ret;
1344 }
1345 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1346
get_amp_max_value(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int ofs)1347 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1348 unsigned int ofs)
1349 {
1350 u32 caps = query_amp_caps(codec, nid, dir);
1351 /* get num steps */
1352 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1353 if (ofs < caps)
1354 caps -= ofs;
1355 return caps;
1356 }
1357
1358 /**
1359 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1360 * @kcontrol: referred ctl element
1361 * @uinfo: pointer to get/store the data
1362 *
1363 * The control element is supposed to have the private_value field
1364 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1365 */
snd_hda_mixer_amp_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1366 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1367 struct snd_ctl_elem_info *uinfo)
1368 {
1369 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1370 u16 nid = get_amp_nid(kcontrol);
1371 u8 chs = get_amp_channels(kcontrol);
1372 int dir = get_amp_direction(kcontrol);
1373 unsigned int ofs = get_amp_offset(kcontrol);
1374
1375 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1376 uinfo->count = chs == 3 ? 2 : 1;
1377 uinfo->value.integer.min = 0;
1378 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1379 if (!uinfo->value.integer.max) {
1380 codec_warn(codec,
1381 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1382 nid, kcontrol->id.name);
1383 return -EINVAL;
1384 }
1385 return 0;
1386 }
1387 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1388
1389
1390 static inline unsigned int
read_amp_value(struct hda_codec * codec,hda_nid_t nid,int ch,int dir,int idx,unsigned int ofs)1391 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1392 int ch, int dir, int idx, unsigned int ofs)
1393 {
1394 unsigned int val;
1395 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1396 val &= HDA_AMP_VOLMASK;
1397 if (val >= ofs)
1398 val -= ofs;
1399 else
1400 val = 0;
1401 return val;
1402 }
1403
1404 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)1405 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1406 int ch, int dir, int idx, unsigned int ofs,
1407 unsigned int val)
1408 {
1409 unsigned int maxval;
1410
1411 if (val > 0)
1412 val += ofs;
1413 /* ofs = 0: raw max value */
1414 maxval = get_amp_max_value(codec, nid, dir, 0);
1415 if (val > maxval)
1416 val = maxval;
1417 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1418 HDA_AMP_VOLMASK, val);
1419 }
1420
1421 /**
1422 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1423 * @kcontrol: ctl element
1424 * @ucontrol: pointer to get/store the data
1425 *
1426 * The control element is supposed to have the private_value field
1427 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1428 */
snd_hda_mixer_amp_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1429 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1430 struct snd_ctl_elem_value *ucontrol)
1431 {
1432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1433 hda_nid_t nid = get_amp_nid(kcontrol);
1434 int chs = get_amp_channels(kcontrol);
1435 int dir = get_amp_direction(kcontrol);
1436 int idx = get_amp_index(kcontrol);
1437 unsigned int ofs = get_amp_offset(kcontrol);
1438 long *valp = ucontrol->value.integer.value;
1439
1440 if (chs & 1)
1441 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1442 if (chs & 2)
1443 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1444 return 0;
1445 }
1446 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1447
1448 /**
1449 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1450 * @kcontrol: ctl element
1451 * @ucontrol: pointer to get/store the data
1452 *
1453 * The control element is supposed to have the private_value field
1454 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1455 */
snd_hda_mixer_amp_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1456 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1457 struct snd_ctl_elem_value *ucontrol)
1458 {
1459 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1460 hda_nid_t nid = get_amp_nid(kcontrol);
1461 int chs = get_amp_channels(kcontrol);
1462 int dir = get_amp_direction(kcontrol);
1463 int idx = get_amp_index(kcontrol);
1464 unsigned int ofs = get_amp_offset(kcontrol);
1465 long *valp = ucontrol->value.integer.value;
1466 int change = 0;
1467
1468 if (chs & 1) {
1469 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1470 valp++;
1471 }
1472 if (chs & 2)
1473 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1474 return change;
1475 }
1476 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1477
1478 /* inquiry the amp caps and convert to TLV */
get_ctl_amp_tlv(struct snd_kcontrol * kcontrol,unsigned int * tlv)1479 static void get_ctl_amp_tlv(struct snd_kcontrol *kcontrol, unsigned int *tlv)
1480 {
1481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1482 hda_nid_t nid = get_amp_nid(kcontrol);
1483 int dir = get_amp_direction(kcontrol);
1484 unsigned int ofs = get_amp_offset(kcontrol);
1485 bool min_mute = get_amp_min_mute(kcontrol);
1486 u32 caps, val1, val2;
1487
1488 caps = query_amp_caps(codec, nid, dir);
1489 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1490 val2 = (val2 + 1) * 25;
1491 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1492 val1 += ofs;
1493 val1 = ((int)val1) * ((int)val2);
1494 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1495 val2 |= TLV_DB_SCALE_MUTE;
1496 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1497 tlv[1] = 2 * sizeof(unsigned int);
1498 tlv[2] = val1;
1499 tlv[3] = val2;
1500 }
1501
1502 /**
1503 * snd_hda_mixer_amp_tlv - TLV callback for a standard AMP mixer volume
1504 * @kcontrol: ctl element
1505 * @op_flag: operation flag
1506 * @size: byte size of input TLV
1507 * @_tlv: TLV data
1508 *
1509 * The control element is supposed to have the private_value field
1510 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1511 */
snd_hda_mixer_amp_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * _tlv)1512 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1513 unsigned int size, unsigned int __user *_tlv)
1514 {
1515 unsigned int tlv[4];
1516
1517 if (size < 4 * sizeof(unsigned int))
1518 return -ENOMEM;
1519 get_ctl_amp_tlv(kcontrol, tlv);
1520 if (copy_to_user(_tlv, tlv, sizeof(tlv)))
1521 return -EFAULT;
1522 return 0;
1523 }
1524 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1525
1526 /**
1527 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1528 * @codec: HD-audio codec
1529 * @nid: NID of a reference widget
1530 * @dir: #HDA_INPUT or #HDA_OUTPUT
1531 * @tlv: TLV data to be stored, at least 4 elements
1532 *
1533 * Set (static) TLV data for a virtual master volume using the AMP caps
1534 * obtained from the reference NID.
1535 * The volume range is recalculated as if the max volume is 0dB.
1536 */
snd_hda_set_vmaster_tlv(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int * tlv)1537 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1538 unsigned int *tlv)
1539 {
1540 u32 caps;
1541 int nums, step;
1542
1543 caps = query_amp_caps(codec, nid, dir);
1544 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1545 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1546 step = (step + 1) * 25;
1547 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1548 tlv[1] = 2 * sizeof(unsigned int);
1549 tlv[2] = -nums * step;
1550 tlv[3] = step;
1551 }
1552 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1553
1554 /* find a mixer control element with the given name */
1555 static struct snd_kcontrol *
find_mixer_ctl(struct hda_codec * codec,const char * name,int dev,int idx)1556 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1557 {
1558 struct snd_ctl_elem_id id;
1559 memset(&id, 0, sizeof(id));
1560 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1561 id.device = dev;
1562 id.index = idx;
1563 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1564 return NULL;
1565 strcpy(id.name, name);
1566 return snd_ctl_find_id(codec->card, &id);
1567 }
1568
1569 /**
1570 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1571 * @codec: HD-audio codec
1572 * @name: ctl id name string
1573 *
1574 * Get the control element with the given id string and IFACE_MIXER.
1575 */
snd_hda_find_mixer_ctl(struct hda_codec * codec,const char * name)1576 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1577 const char *name)
1578 {
1579 return find_mixer_ctl(codec, name, 0, 0);
1580 }
1581 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1582
find_empty_mixer_ctl_idx(struct hda_codec * codec,const char * name,int start_idx)1583 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1584 int start_idx)
1585 {
1586 int i, idx;
1587 /* 16 ctlrs should be large enough */
1588 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1589 if (!find_mixer_ctl(codec, name, 0, idx))
1590 return idx;
1591 }
1592 return -EBUSY;
1593 }
1594
1595 /**
1596 * snd_hda_ctl_add - Add a control element and assign to the codec
1597 * @codec: HD-audio codec
1598 * @nid: corresponding NID (optional)
1599 * @kctl: the control element to assign
1600 *
1601 * Add the given control element to an array inside the codec instance.
1602 * All control elements belonging to a codec are supposed to be added
1603 * by this function so that a proper clean-up works at the free or
1604 * reconfiguration time.
1605 *
1606 * If non-zero @nid is passed, the NID is assigned to the control element.
1607 * The assignment is shown in the codec proc file.
1608 *
1609 * snd_hda_ctl_add() checks the control subdev id field whether
1610 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1611 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1612 * specifies if kctl->private_value is a HDA amplifier value.
1613 */
snd_hda_ctl_add(struct hda_codec * codec,hda_nid_t nid,struct snd_kcontrol * kctl)1614 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1615 struct snd_kcontrol *kctl)
1616 {
1617 int err;
1618 unsigned short flags = 0;
1619 struct hda_nid_item *item;
1620
1621 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1622 flags |= HDA_NID_ITEM_AMP;
1623 if (nid == 0)
1624 nid = get_amp_nid_(kctl->private_value);
1625 }
1626 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1627 nid = kctl->id.subdevice & 0xffff;
1628 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1629 kctl->id.subdevice = 0;
1630 err = snd_ctl_add(codec->card, kctl);
1631 if (err < 0)
1632 return err;
1633 item = snd_array_new(&codec->mixers);
1634 if (!item)
1635 return -ENOMEM;
1636 item->kctl = kctl;
1637 item->nid = nid;
1638 item->flags = flags;
1639 return 0;
1640 }
1641 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1642
1643 /**
1644 * snd_hda_add_nid - Assign a NID to a control element
1645 * @codec: HD-audio codec
1646 * @nid: corresponding NID (optional)
1647 * @kctl: the control element to assign
1648 * @index: index to kctl
1649 *
1650 * Add the given control element to an array inside the codec instance.
1651 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1652 * NID:KCTL mapping - for example "Capture Source" selector.
1653 */
snd_hda_add_nid(struct hda_codec * codec,struct snd_kcontrol * kctl,unsigned int index,hda_nid_t nid)1654 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1655 unsigned int index, hda_nid_t nid)
1656 {
1657 struct hda_nid_item *item;
1658
1659 if (nid > 0) {
1660 item = snd_array_new(&codec->nids);
1661 if (!item)
1662 return -ENOMEM;
1663 item->kctl = kctl;
1664 item->index = index;
1665 item->nid = nid;
1666 return 0;
1667 }
1668 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1669 kctl->id.name, kctl->id.index, index);
1670 return -EINVAL;
1671 }
1672 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1673
1674 /**
1675 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1676 * @codec: HD-audio codec
1677 */
snd_hda_ctls_clear(struct hda_codec * codec)1678 void snd_hda_ctls_clear(struct hda_codec *codec)
1679 {
1680 int i;
1681 struct hda_nid_item *items = codec->mixers.list;
1682 for (i = 0; i < codec->mixers.used; i++)
1683 snd_ctl_remove(codec->card, items[i].kctl);
1684 snd_array_free(&codec->mixers);
1685 snd_array_free(&codec->nids);
1686 }
1687
1688 /**
1689 * snd_hda_lock_devices - pseudo device locking
1690 * @bus: the BUS
1691 *
1692 * toggle card->shutdown to allow/disallow the device access (as a hack)
1693 */
snd_hda_lock_devices(struct hda_bus * bus)1694 int snd_hda_lock_devices(struct hda_bus *bus)
1695 {
1696 struct snd_card *card = bus->card;
1697 struct hda_codec *codec;
1698
1699 spin_lock(&card->files_lock);
1700 if (card->shutdown)
1701 goto err_unlock;
1702 card->shutdown = 1;
1703 if (!list_empty(&card->ctl_files))
1704 goto err_clear;
1705
1706 list_for_each_codec(codec, bus) {
1707 struct hda_pcm *cpcm;
1708 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1709 if (!cpcm->pcm)
1710 continue;
1711 if (cpcm->pcm->streams[0].substream_opened ||
1712 cpcm->pcm->streams[1].substream_opened)
1713 goto err_clear;
1714 }
1715 }
1716 spin_unlock(&card->files_lock);
1717 return 0;
1718
1719 err_clear:
1720 card->shutdown = 0;
1721 err_unlock:
1722 spin_unlock(&card->files_lock);
1723 return -EINVAL;
1724 }
1725 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1726
1727 /**
1728 * snd_hda_unlock_devices - pseudo device unlocking
1729 * @bus: the BUS
1730 */
snd_hda_unlock_devices(struct hda_bus * bus)1731 void snd_hda_unlock_devices(struct hda_bus *bus)
1732 {
1733 struct snd_card *card = bus->card;
1734
1735 spin_lock(&card->files_lock);
1736 card->shutdown = 0;
1737 spin_unlock(&card->files_lock);
1738 }
1739 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1740
1741 /**
1742 * snd_hda_codec_reset - Clear all objects assigned to the codec
1743 * @codec: HD-audio codec
1744 *
1745 * This frees the all PCM and control elements assigned to the codec, and
1746 * clears the caches and restores the pin default configurations.
1747 *
1748 * When a device is being used, it returns -EBSY. If successfully freed,
1749 * returns zero.
1750 */
snd_hda_codec_reset(struct hda_codec * codec)1751 int snd_hda_codec_reset(struct hda_codec *codec)
1752 {
1753 struct hda_bus *bus = codec->bus;
1754
1755 if (snd_hda_lock_devices(bus) < 0)
1756 return -EBUSY;
1757
1758 /* OK, let it free */
1759 snd_hdac_device_unregister(&codec->core);
1760
1761 /* allow device access again */
1762 snd_hda_unlock_devices(bus);
1763 return 0;
1764 }
1765
1766 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1767
1768 /* 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)1769 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1770 const char *suffix, map_slave_func_t func, void *data)
1771 {
1772 struct hda_nid_item *items;
1773 const char * const *s;
1774 int i, err;
1775
1776 items = codec->mixers.list;
1777 for (i = 0; i < codec->mixers.used; i++) {
1778 struct snd_kcontrol *sctl = items[i].kctl;
1779 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1780 continue;
1781 for (s = slaves; *s; s++) {
1782 char tmpname[sizeof(sctl->id.name)];
1783 const char *name = *s;
1784 if (suffix) {
1785 snprintf(tmpname, sizeof(tmpname), "%s %s",
1786 name, suffix);
1787 name = tmpname;
1788 }
1789 if (!strcmp(sctl->id.name, name)) {
1790 err = func(codec, data, sctl);
1791 if (err)
1792 return err;
1793 break;
1794 }
1795 }
1796 }
1797 return 0;
1798 }
1799
check_slave_present(struct hda_codec * codec,void * data,struct snd_kcontrol * sctl)1800 static int check_slave_present(struct hda_codec *codec,
1801 void *data, struct snd_kcontrol *sctl)
1802 {
1803 return 1;
1804 }
1805
1806 /* call kctl->put with the given value(s) */
put_kctl_with_value(struct snd_kcontrol * kctl,int val)1807 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1808 {
1809 struct snd_ctl_elem_value *ucontrol;
1810 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1811 if (!ucontrol)
1812 return -ENOMEM;
1813 ucontrol->value.integer.value[0] = val;
1814 ucontrol->value.integer.value[1] = val;
1815 kctl->put(kctl, ucontrol);
1816 kfree(ucontrol);
1817 return 0;
1818 }
1819
1820 struct slave_init_arg {
1821 struct hda_codec *codec;
1822 int step;
1823 };
1824
1825 /* initialize the slave volume with 0dB via snd_ctl_apply_vmaster_slaves() */
init_slave_0dB(struct snd_kcontrol * slave,struct snd_kcontrol * kctl,void * _arg)1826 static int init_slave_0dB(struct snd_kcontrol *slave,
1827 struct snd_kcontrol *kctl,
1828 void *_arg)
1829 {
1830 struct slave_init_arg *arg = _arg;
1831 int _tlv[4];
1832 const int *tlv = NULL;
1833 int step;
1834 int val;
1835
1836 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1837 if (kctl->tlv.c != snd_hda_mixer_amp_tlv) {
1838 codec_err(arg->codec,
1839 "Unexpected TLV callback for slave %s:%d\n",
1840 kctl->id.name, kctl->id.index);
1841 return 0; /* ignore */
1842 }
1843 get_ctl_amp_tlv(kctl, _tlv);
1844 tlv = _tlv;
1845 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1846 tlv = kctl->tlv.p;
1847
1848 if (!tlv || tlv[0] != SNDRV_CTL_TLVT_DB_SCALE)
1849 return 0;
1850
1851 step = tlv[3];
1852 step &= ~TLV_DB_SCALE_MUTE;
1853 if (!step)
1854 return 0;
1855 if (arg->step && arg->step != step) {
1856 codec_err(arg->codec,
1857 "Mismatching dB step for vmaster slave (%d!=%d)\n",
1858 arg->step, step);
1859 return 0;
1860 }
1861
1862 arg->step = step;
1863 val = -tlv[2] / step;
1864 if (val > 0) {
1865 put_kctl_with_value(slave, val);
1866 return val;
1867 }
1868
1869 return 0;
1870 }
1871
1872 /* unmute the slave via snd_ctl_apply_vmaster_slaves() */
init_slave_unmute(struct snd_kcontrol * slave,struct snd_kcontrol * kctl,void * _arg)1873 static int init_slave_unmute(struct snd_kcontrol *slave,
1874 struct snd_kcontrol *kctl,
1875 void *_arg)
1876 {
1877 return put_kctl_with_value(slave, 1);
1878 }
1879
add_slave(struct hda_codec * codec,void * data,struct snd_kcontrol * slave)1880 static int add_slave(struct hda_codec *codec,
1881 void *data, struct snd_kcontrol *slave)
1882 {
1883 return snd_ctl_add_slave(data, slave);
1884 }
1885
1886 /**
1887 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1888 * @codec: HD-audio codec
1889 * @name: vmaster control name
1890 * @tlv: TLV data (optional)
1891 * @slaves: slave control names (optional)
1892 * @suffix: suffix string to each slave name (optional)
1893 * @init_slave_vol: initialize slaves to unmute/0dB
1894 * @ctl_ret: store the vmaster kcontrol in return
1895 *
1896 * Create a virtual master control with the given name. The TLV data
1897 * must be either NULL or a valid data.
1898 *
1899 * @slaves is a NULL-terminated array of strings, each of which is a
1900 * slave control name. All controls with these names are assigned to
1901 * the new virtual master control.
1902 *
1903 * This function returns zero if successful or a negative error code.
1904 */
__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)1905 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1906 unsigned int *tlv, const char * const *slaves,
1907 const char *suffix, bool init_slave_vol,
1908 struct snd_kcontrol **ctl_ret)
1909 {
1910 struct snd_kcontrol *kctl;
1911 int err;
1912
1913 if (ctl_ret)
1914 *ctl_ret = NULL;
1915
1916 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1917 if (err != 1) {
1918 codec_dbg(codec, "No slave found for %s\n", name);
1919 return 0;
1920 }
1921 kctl = snd_ctl_make_virtual_master(name, tlv);
1922 if (!kctl)
1923 return -ENOMEM;
1924 err = snd_hda_ctl_add(codec, 0, kctl);
1925 if (err < 0)
1926 return err;
1927
1928 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1929 if (err < 0)
1930 return err;
1931
1932 /* init with master mute & zero volume */
1933 put_kctl_with_value(kctl, 0);
1934 if (init_slave_vol) {
1935 struct slave_init_arg arg = {
1936 .codec = codec,
1937 .step = 0,
1938 };
1939 snd_ctl_apply_vmaster_slaves(kctl,
1940 tlv ? init_slave_0dB : init_slave_unmute,
1941 &arg);
1942 }
1943
1944 if (ctl_ret)
1945 *ctl_ret = kctl;
1946 return 0;
1947 }
1948 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1949
1950 /*
1951 * mute-LED control using vmaster
1952 */
vmaster_mute_mode_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1953 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1954 struct snd_ctl_elem_info *uinfo)
1955 {
1956 static const char * const texts[] = {
1957 "On", "Off", "Follow Master"
1958 };
1959
1960 return snd_ctl_enum_info(uinfo, 1, 3, texts);
1961 }
1962
vmaster_mute_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1963 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1964 struct snd_ctl_elem_value *ucontrol)
1965 {
1966 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1967 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1968 return 0;
1969 }
1970
vmaster_mute_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1971 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1972 struct snd_ctl_elem_value *ucontrol)
1973 {
1974 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1975 unsigned int old_mode = hook->mute_mode;
1976
1977 hook->mute_mode = ucontrol->value.enumerated.item[0];
1978 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1979 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1980 if (old_mode == hook->mute_mode)
1981 return 0;
1982 snd_hda_sync_vmaster_hook(hook);
1983 return 1;
1984 }
1985
1986 static const struct snd_kcontrol_new vmaster_mute_mode = {
1987 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1988 .name = "Mute-LED Mode",
1989 .info = vmaster_mute_mode_info,
1990 .get = vmaster_mute_mode_get,
1991 .put = vmaster_mute_mode_put,
1992 };
1993
1994 /* meta hook to call each driver's vmaster hook */
vmaster_hook(void * private_data,int enabled)1995 static void vmaster_hook(void *private_data, int enabled)
1996 {
1997 struct hda_vmaster_mute_hook *hook = private_data;
1998
1999 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2000 enabled = hook->mute_mode;
2001 hook->hook(hook->codec, enabled);
2002 }
2003
2004 /**
2005 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2006 * @codec: the HDA codec
2007 * @hook: the vmaster hook object
2008 * @expose_enum_ctl: flag to create an enum ctl
2009 *
2010 * Add a mute-LED hook with the given vmaster switch kctl.
2011 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2012 * created and associated with the given hook.
2013 */
snd_hda_add_vmaster_hook(struct hda_codec * codec,struct hda_vmaster_mute_hook * hook,bool expose_enum_ctl)2014 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2015 struct hda_vmaster_mute_hook *hook,
2016 bool expose_enum_ctl)
2017 {
2018 struct snd_kcontrol *kctl;
2019
2020 if (!hook->hook || !hook->sw_kctl)
2021 return 0;
2022 hook->codec = codec;
2023 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2024 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
2025 if (!expose_enum_ctl)
2026 return 0;
2027 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2028 if (!kctl)
2029 return -ENOMEM;
2030 return snd_hda_ctl_add(codec, 0, kctl);
2031 }
2032 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2033
2034 /**
2035 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2036 * @hook: the vmaster hook
2037 *
2038 * Call the hook with the current value for synchronization.
2039 * Should be called in init callback.
2040 */
snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook * hook)2041 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2042 {
2043 if (!hook->hook || !hook->codec)
2044 return;
2045 /* don't call vmaster hook in the destructor since it might have
2046 * been already destroyed
2047 */
2048 if (hook->codec->bus->shutdown)
2049 return;
2050 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2051 }
2052 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2053
2054
2055 /**
2056 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2057 * @kcontrol: referred ctl element
2058 * @uinfo: pointer to get/store the data
2059 *
2060 * The control element is supposed to have the private_value field
2061 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2062 */
snd_hda_mixer_amp_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2063 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_info *uinfo)
2065 {
2066 int chs = get_amp_channels(kcontrol);
2067
2068 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2069 uinfo->count = chs == 3 ? 2 : 1;
2070 uinfo->value.integer.min = 0;
2071 uinfo->value.integer.max = 1;
2072 return 0;
2073 }
2074 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2075
2076 /**
2077 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2078 * @kcontrol: ctl element
2079 * @ucontrol: pointer to get/store the data
2080 *
2081 * The control element is supposed to have the private_value field
2082 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2083 */
snd_hda_mixer_amp_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2084 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2085 struct snd_ctl_elem_value *ucontrol)
2086 {
2087 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2088 hda_nid_t nid = get_amp_nid(kcontrol);
2089 int chs = get_amp_channels(kcontrol);
2090 int dir = get_amp_direction(kcontrol);
2091 int idx = get_amp_index(kcontrol);
2092 long *valp = ucontrol->value.integer.value;
2093
2094 if (chs & 1)
2095 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2096 HDA_AMP_MUTE) ? 0 : 1;
2097 if (chs & 2)
2098 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2099 HDA_AMP_MUTE) ? 0 : 1;
2100 return 0;
2101 }
2102 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2103
2104 /**
2105 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2106 * @kcontrol: ctl element
2107 * @ucontrol: pointer to get/store the data
2108 *
2109 * The control element is supposed to have the private_value field
2110 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2111 */
snd_hda_mixer_amp_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2112 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2113 struct snd_ctl_elem_value *ucontrol)
2114 {
2115 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2116 hda_nid_t nid = get_amp_nid(kcontrol);
2117 int chs = get_amp_channels(kcontrol);
2118 int dir = get_amp_direction(kcontrol);
2119 int idx = get_amp_index(kcontrol);
2120 long *valp = ucontrol->value.integer.value;
2121 int change = 0;
2122
2123 if (chs & 1) {
2124 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2125 HDA_AMP_MUTE,
2126 *valp ? 0 : HDA_AMP_MUTE);
2127 valp++;
2128 }
2129 if (chs & 2)
2130 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2131 HDA_AMP_MUTE,
2132 *valp ? 0 : HDA_AMP_MUTE);
2133 hda_call_check_power_status(codec, nid);
2134 return change;
2135 }
2136 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2137
2138 /*
2139 * SPDIF out controls
2140 */
2141
snd_hda_spdif_mask_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2142 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2143 struct snd_ctl_elem_info *uinfo)
2144 {
2145 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2146 uinfo->count = 1;
2147 return 0;
2148 }
2149
snd_hda_spdif_cmask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2150 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2151 struct snd_ctl_elem_value *ucontrol)
2152 {
2153 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2154 IEC958_AES0_NONAUDIO |
2155 IEC958_AES0_CON_EMPHASIS_5015 |
2156 IEC958_AES0_CON_NOT_COPYRIGHT;
2157 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2158 IEC958_AES1_CON_ORIGINAL;
2159 return 0;
2160 }
2161
snd_hda_spdif_pmask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2162 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2163 struct snd_ctl_elem_value *ucontrol)
2164 {
2165 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2166 IEC958_AES0_NONAUDIO |
2167 IEC958_AES0_PRO_EMPHASIS_5015;
2168 return 0;
2169 }
2170
snd_hda_spdif_default_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2171 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2172 struct snd_ctl_elem_value *ucontrol)
2173 {
2174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2175 int idx = kcontrol->private_value;
2176 struct hda_spdif_out *spdif;
2177
2178 mutex_lock(&codec->spdif_mutex);
2179 spdif = snd_array_elem(&codec->spdif_out, idx);
2180 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2181 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2182 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2183 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2184 mutex_unlock(&codec->spdif_mutex);
2185
2186 return 0;
2187 }
2188
2189 /* convert from SPDIF status bits to HDA SPDIF bits
2190 * bit 0 (DigEn) is always set zero (to be filled later)
2191 */
convert_from_spdif_status(unsigned int sbits)2192 static unsigned short convert_from_spdif_status(unsigned int sbits)
2193 {
2194 unsigned short val = 0;
2195
2196 if (sbits & IEC958_AES0_PROFESSIONAL)
2197 val |= AC_DIG1_PROFESSIONAL;
2198 if (sbits & IEC958_AES0_NONAUDIO)
2199 val |= AC_DIG1_NONAUDIO;
2200 if (sbits & IEC958_AES0_PROFESSIONAL) {
2201 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2202 IEC958_AES0_PRO_EMPHASIS_5015)
2203 val |= AC_DIG1_EMPHASIS;
2204 } else {
2205 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2206 IEC958_AES0_CON_EMPHASIS_5015)
2207 val |= AC_DIG1_EMPHASIS;
2208 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2209 val |= AC_DIG1_COPYRIGHT;
2210 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2211 val |= AC_DIG1_LEVEL;
2212 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2213 }
2214 return val;
2215 }
2216
2217 /* convert to SPDIF status bits from HDA SPDIF bits
2218 */
convert_to_spdif_status(unsigned short val)2219 static unsigned int convert_to_spdif_status(unsigned short val)
2220 {
2221 unsigned int sbits = 0;
2222
2223 if (val & AC_DIG1_NONAUDIO)
2224 sbits |= IEC958_AES0_NONAUDIO;
2225 if (val & AC_DIG1_PROFESSIONAL)
2226 sbits |= IEC958_AES0_PROFESSIONAL;
2227 if (sbits & IEC958_AES0_PROFESSIONAL) {
2228 if (val & AC_DIG1_EMPHASIS)
2229 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2230 } else {
2231 if (val & AC_DIG1_EMPHASIS)
2232 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2233 if (!(val & AC_DIG1_COPYRIGHT))
2234 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2235 if (val & AC_DIG1_LEVEL)
2236 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2237 sbits |= val & (0x7f << 8);
2238 }
2239 return sbits;
2240 }
2241
2242 /* set digital convert verbs both for the given NID and its slaves */
set_dig_out(struct hda_codec * codec,hda_nid_t nid,int mask,int val)2243 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2244 int mask, int val)
2245 {
2246 const hda_nid_t *d;
2247
2248 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2249 mask, val);
2250 d = codec->slave_dig_outs;
2251 if (!d)
2252 return;
2253 for (; *d; d++)
2254 snd_hdac_regmap_update(&codec->core, *d,
2255 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2256 }
2257
set_dig_out_convert(struct hda_codec * codec,hda_nid_t nid,int dig1,int dig2)2258 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2259 int dig1, int dig2)
2260 {
2261 unsigned int mask = 0;
2262 unsigned int val = 0;
2263
2264 if (dig1 != -1) {
2265 mask |= 0xff;
2266 val = dig1;
2267 }
2268 if (dig2 != -1) {
2269 mask |= 0xff00;
2270 val |= dig2 << 8;
2271 }
2272 set_dig_out(codec, nid, mask, val);
2273 }
2274
snd_hda_spdif_default_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2275 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2276 struct snd_ctl_elem_value *ucontrol)
2277 {
2278 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2279 int idx = kcontrol->private_value;
2280 struct hda_spdif_out *spdif;
2281 hda_nid_t nid;
2282 unsigned short val;
2283 int change;
2284
2285 mutex_lock(&codec->spdif_mutex);
2286 spdif = snd_array_elem(&codec->spdif_out, idx);
2287 nid = spdif->nid;
2288 spdif->status = ucontrol->value.iec958.status[0] |
2289 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2290 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2291 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2292 val = convert_from_spdif_status(spdif->status);
2293 val |= spdif->ctls & 1;
2294 change = spdif->ctls != val;
2295 spdif->ctls = val;
2296 if (change && nid != (u16)-1)
2297 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2298 mutex_unlock(&codec->spdif_mutex);
2299 return change;
2300 }
2301
2302 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2303
snd_hda_spdif_out_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2304 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2305 struct snd_ctl_elem_value *ucontrol)
2306 {
2307 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2308 int idx = kcontrol->private_value;
2309 struct hda_spdif_out *spdif;
2310
2311 mutex_lock(&codec->spdif_mutex);
2312 spdif = snd_array_elem(&codec->spdif_out, idx);
2313 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2314 mutex_unlock(&codec->spdif_mutex);
2315 return 0;
2316 }
2317
set_spdif_ctls(struct hda_codec * codec,hda_nid_t nid,int dig1,int dig2)2318 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2319 int dig1, int dig2)
2320 {
2321 set_dig_out_convert(codec, nid, dig1, dig2);
2322 /* unmute amp switch (if any) */
2323 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2324 (dig1 & AC_DIG1_ENABLE))
2325 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2326 HDA_AMP_MUTE, 0);
2327 }
2328
snd_hda_spdif_out_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2329 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_value *ucontrol)
2331 {
2332 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2333 int idx = kcontrol->private_value;
2334 struct hda_spdif_out *spdif;
2335 hda_nid_t nid;
2336 unsigned short val;
2337 int change;
2338
2339 mutex_lock(&codec->spdif_mutex);
2340 spdif = snd_array_elem(&codec->spdif_out, idx);
2341 nid = spdif->nid;
2342 val = spdif->ctls & ~AC_DIG1_ENABLE;
2343 if (ucontrol->value.integer.value[0])
2344 val |= AC_DIG1_ENABLE;
2345 change = spdif->ctls != val;
2346 spdif->ctls = val;
2347 if (change && nid != (u16)-1)
2348 set_spdif_ctls(codec, nid, val & 0xff, -1);
2349 mutex_unlock(&codec->spdif_mutex);
2350 return change;
2351 }
2352
2353 static struct snd_kcontrol_new dig_mixes[] = {
2354 {
2355 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2356 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2357 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2358 .info = snd_hda_spdif_mask_info,
2359 .get = snd_hda_spdif_cmask_get,
2360 },
2361 {
2362 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2363 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2364 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2365 .info = snd_hda_spdif_mask_info,
2366 .get = snd_hda_spdif_pmask_get,
2367 },
2368 {
2369 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2370 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2371 .info = snd_hda_spdif_mask_info,
2372 .get = snd_hda_spdif_default_get,
2373 .put = snd_hda_spdif_default_put,
2374 },
2375 {
2376 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2377 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2378 .info = snd_hda_spdif_out_switch_info,
2379 .get = snd_hda_spdif_out_switch_get,
2380 .put = snd_hda_spdif_out_switch_put,
2381 },
2382 { } /* end */
2383 };
2384
2385 /**
2386 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2387 * @codec: the HDA codec
2388 * @associated_nid: NID that new ctls associated with
2389 * @cvt_nid: converter NID
2390 * @type: HDA_PCM_TYPE_*
2391 * Creates controls related with the digital output.
2392 * Called from each patch supporting the digital out.
2393 *
2394 * Returns 0 if successful, or a negative error code.
2395 */
snd_hda_create_dig_out_ctls(struct hda_codec * codec,hda_nid_t associated_nid,hda_nid_t cvt_nid,int type)2396 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2397 hda_nid_t associated_nid,
2398 hda_nid_t cvt_nid,
2399 int type)
2400 {
2401 int err;
2402 struct snd_kcontrol *kctl;
2403 struct snd_kcontrol_new *dig_mix;
2404 int idx = 0;
2405 int val = 0;
2406 const int spdif_index = 16;
2407 struct hda_spdif_out *spdif;
2408 struct hda_bus *bus = codec->bus;
2409
2410 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2411 type == HDA_PCM_TYPE_SPDIF) {
2412 idx = spdif_index;
2413 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2414 type == HDA_PCM_TYPE_HDMI) {
2415 /* suppose a single SPDIF device */
2416 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2417 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2418 if (!kctl)
2419 break;
2420 kctl->id.index = spdif_index;
2421 }
2422 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2423 }
2424 if (!bus->primary_dig_out_type)
2425 bus->primary_dig_out_type = type;
2426
2427 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2428 if (idx < 0) {
2429 codec_err(codec, "too many IEC958 outputs\n");
2430 return -EBUSY;
2431 }
2432 spdif = snd_array_new(&codec->spdif_out);
2433 if (!spdif)
2434 return -ENOMEM;
2435 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2436 kctl = snd_ctl_new1(dig_mix, codec);
2437 if (!kctl)
2438 return -ENOMEM;
2439 kctl->id.index = idx;
2440 kctl->private_value = codec->spdif_out.used - 1;
2441 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2442 if (err < 0)
2443 return err;
2444 }
2445 spdif->nid = cvt_nid;
2446 snd_hdac_regmap_read(&codec->core, cvt_nid,
2447 AC_VERB_GET_DIGI_CONVERT_1, &val);
2448 spdif->ctls = val;
2449 spdif->status = convert_to_spdif_status(spdif->ctls);
2450 return 0;
2451 }
2452 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2453
2454 /**
2455 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2456 * @codec: the HDA codec
2457 * @nid: widget NID
2458 *
2459 * call within spdif_mutex lock
2460 */
snd_hda_spdif_out_of_nid(struct hda_codec * codec,hda_nid_t nid)2461 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2462 hda_nid_t nid)
2463 {
2464 int i;
2465 for (i = 0; i < codec->spdif_out.used; i++) {
2466 struct hda_spdif_out *spdif =
2467 snd_array_elem(&codec->spdif_out, i);
2468 if (spdif->nid == nid)
2469 return spdif;
2470 }
2471 return NULL;
2472 }
2473 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2474
2475 /**
2476 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2477 * @codec: the HDA codec
2478 * @idx: the SPDIF ctl index
2479 *
2480 * Unassign the widget from the given SPDIF control.
2481 */
snd_hda_spdif_ctls_unassign(struct hda_codec * codec,int idx)2482 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2483 {
2484 struct hda_spdif_out *spdif;
2485
2486 mutex_lock(&codec->spdif_mutex);
2487 spdif = snd_array_elem(&codec->spdif_out, idx);
2488 spdif->nid = (u16)-1;
2489 mutex_unlock(&codec->spdif_mutex);
2490 }
2491 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2492
2493 /**
2494 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2495 * @codec: the HDA codec
2496 * @idx: the SPDIF ctl idx
2497 * @nid: widget NID
2498 *
2499 * Assign the widget to the SPDIF control with the given index.
2500 */
snd_hda_spdif_ctls_assign(struct hda_codec * codec,int idx,hda_nid_t nid)2501 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2502 {
2503 struct hda_spdif_out *spdif;
2504 unsigned short val;
2505
2506 mutex_lock(&codec->spdif_mutex);
2507 spdif = snd_array_elem(&codec->spdif_out, idx);
2508 if (spdif->nid != nid) {
2509 spdif->nid = nid;
2510 val = spdif->ctls;
2511 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2512 }
2513 mutex_unlock(&codec->spdif_mutex);
2514 }
2515 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2516
2517 /*
2518 * SPDIF sharing with analog output
2519 */
spdif_share_sw_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2520 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_value *ucontrol)
2522 {
2523 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2524 ucontrol->value.integer.value[0] = mout->share_spdif;
2525 return 0;
2526 }
2527
spdif_share_sw_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2528 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_value *ucontrol)
2530 {
2531 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2532 mout->share_spdif = !!ucontrol->value.integer.value[0];
2533 return 0;
2534 }
2535
2536 static const struct snd_kcontrol_new spdif_share_sw = {
2537 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2538 .name = "IEC958 Default PCM Playback Switch",
2539 .info = snd_ctl_boolean_mono_info,
2540 .get = spdif_share_sw_get,
2541 .put = spdif_share_sw_put,
2542 };
2543
2544 /**
2545 * snd_hda_create_spdif_share_sw - create Default PCM switch
2546 * @codec: the HDA codec
2547 * @mout: multi-out instance
2548 */
snd_hda_create_spdif_share_sw(struct hda_codec * codec,struct hda_multi_out * mout)2549 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2550 struct hda_multi_out *mout)
2551 {
2552 struct snd_kcontrol *kctl;
2553
2554 if (!mout->dig_out_nid)
2555 return 0;
2556
2557 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2558 if (!kctl)
2559 return -ENOMEM;
2560 /* ATTENTION: here mout is passed as private_data, instead of codec */
2561 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2562 }
2563 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2564
2565 /*
2566 * SPDIF input
2567 */
2568
2569 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2570
snd_hda_spdif_in_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2571 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2572 struct snd_ctl_elem_value *ucontrol)
2573 {
2574 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2575
2576 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2577 return 0;
2578 }
2579
snd_hda_spdif_in_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2580 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
2582 {
2583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2584 hda_nid_t nid = kcontrol->private_value;
2585 unsigned int val = !!ucontrol->value.integer.value[0];
2586 int change;
2587
2588 mutex_lock(&codec->spdif_mutex);
2589 change = codec->spdif_in_enable != val;
2590 if (change) {
2591 codec->spdif_in_enable = val;
2592 snd_hdac_regmap_write(&codec->core, nid,
2593 AC_VERB_SET_DIGI_CONVERT_1, val);
2594 }
2595 mutex_unlock(&codec->spdif_mutex);
2596 return change;
2597 }
2598
snd_hda_spdif_in_status_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2599 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2600 struct snd_ctl_elem_value *ucontrol)
2601 {
2602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2603 hda_nid_t nid = kcontrol->private_value;
2604 unsigned int val;
2605 unsigned int sbits;
2606
2607 snd_hdac_regmap_read(&codec->core, nid,
2608 AC_VERB_GET_DIGI_CONVERT_1, &val);
2609 sbits = convert_to_spdif_status(val);
2610 ucontrol->value.iec958.status[0] = sbits;
2611 ucontrol->value.iec958.status[1] = sbits >> 8;
2612 ucontrol->value.iec958.status[2] = sbits >> 16;
2613 ucontrol->value.iec958.status[3] = sbits >> 24;
2614 return 0;
2615 }
2616
2617 static struct snd_kcontrol_new dig_in_ctls[] = {
2618 {
2619 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2620 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2621 .info = snd_hda_spdif_in_switch_info,
2622 .get = snd_hda_spdif_in_switch_get,
2623 .put = snd_hda_spdif_in_switch_put,
2624 },
2625 {
2626 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2627 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2628 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2629 .info = snd_hda_spdif_mask_info,
2630 .get = snd_hda_spdif_in_status_get,
2631 },
2632 { } /* end */
2633 };
2634
2635 /**
2636 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2637 * @codec: the HDA codec
2638 * @nid: audio in widget NID
2639 *
2640 * Creates controls related with the SPDIF input.
2641 * Called from each patch supporting the SPDIF in.
2642 *
2643 * Returns 0 if successful, or a negative error code.
2644 */
snd_hda_create_spdif_in_ctls(struct hda_codec * codec,hda_nid_t nid)2645 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2646 {
2647 int err;
2648 struct snd_kcontrol *kctl;
2649 struct snd_kcontrol_new *dig_mix;
2650 int idx;
2651
2652 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2653 if (idx < 0) {
2654 codec_err(codec, "too many IEC958 inputs\n");
2655 return -EBUSY;
2656 }
2657 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2658 kctl = snd_ctl_new1(dig_mix, codec);
2659 if (!kctl)
2660 return -ENOMEM;
2661 kctl->private_value = nid;
2662 err = snd_hda_ctl_add(codec, nid, kctl);
2663 if (err < 0)
2664 return err;
2665 }
2666 codec->spdif_in_enable =
2667 snd_hda_codec_read(codec, nid, 0,
2668 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2669 AC_DIG1_ENABLE;
2670 return 0;
2671 }
2672 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2673
2674 /**
2675 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2676 * @codec: the HDA codec
2677 * @fg: function group (not used now)
2678 * @power_state: the power state to set (AC_PWRST_*)
2679 *
2680 * Set the given power state to all widgets that have the power control.
2681 * If the codec has power_filter set, it evaluates the power state and
2682 * filter out if it's unchanged as D3.
2683 */
snd_hda_codec_set_power_to_all(struct hda_codec * codec,hda_nid_t fg,unsigned int power_state)2684 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2685 unsigned int power_state)
2686 {
2687 hda_nid_t nid;
2688
2689 for_each_hda_codec_node(nid, codec) {
2690 unsigned int wcaps = get_wcaps(codec, nid);
2691 unsigned int state = power_state;
2692 if (!(wcaps & AC_WCAP_POWER))
2693 continue;
2694 if (codec->power_filter) {
2695 state = codec->power_filter(codec, nid, power_state);
2696 if (state != power_state && power_state == AC_PWRST_D3)
2697 continue;
2698 }
2699 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2700 state);
2701 }
2702 }
2703 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2704
2705 /*
2706 * wait until the state is reached, returns the current state
2707 */
hda_sync_power_state(struct hda_codec * codec,hda_nid_t fg,unsigned int power_state)2708 static unsigned int hda_sync_power_state(struct hda_codec *codec,
2709 hda_nid_t fg,
2710 unsigned int power_state)
2711 {
2712 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2713 unsigned int state, actual_state;
2714
2715 for (;;) {
2716 state = snd_hda_codec_read(codec, fg, 0,
2717 AC_VERB_GET_POWER_STATE, 0);
2718 if (state & AC_PWRST_ERROR)
2719 break;
2720 actual_state = (state >> 4) & 0x0f;
2721 if (actual_state == power_state)
2722 break;
2723 if (time_after_eq(jiffies, end_time))
2724 break;
2725 /* wait until the codec reachs to the target state */
2726 msleep(1);
2727 }
2728 return state;
2729 }
2730
2731 /**
2732 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2733 * @codec: the HDA codec
2734 * @nid: widget NID
2735 * @power_state: power state to evalue
2736 *
2737 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2738 * This can be used a codec power_filter callback.
2739 */
snd_hda_codec_eapd_power_filter(struct hda_codec * codec,hda_nid_t nid,unsigned int power_state)2740 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2741 hda_nid_t nid,
2742 unsigned int power_state)
2743 {
2744 if (nid == codec->core.afg || nid == codec->core.mfg)
2745 return power_state;
2746 if (power_state == AC_PWRST_D3 &&
2747 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2748 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2749 int eapd = snd_hda_codec_read(codec, nid, 0,
2750 AC_VERB_GET_EAPD_BTLENABLE, 0);
2751 if (eapd & 0x02)
2752 return AC_PWRST_D0;
2753 }
2754 return power_state;
2755 }
2756 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2757
2758 /*
2759 * set power state of the codec, and return the power state
2760 */
hda_set_power_state(struct hda_codec * codec,unsigned int power_state)2761 static unsigned int hda_set_power_state(struct hda_codec *codec,
2762 unsigned int power_state)
2763 {
2764 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2765 int count;
2766 unsigned int state;
2767 int flags = 0;
2768
2769 /* this delay seems necessary to avoid click noise at power-down */
2770 if (power_state == AC_PWRST_D3) {
2771 if (codec->depop_delay < 0)
2772 msleep(codec_has_epss(codec) ? 10 : 100);
2773 else if (codec->depop_delay > 0)
2774 msleep(codec->depop_delay);
2775 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2776 }
2777
2778 /* repeat power states setting at most 10 times*/
2779 for (count = 0; count < 10; count++) {
2780 if (codec->patch_ops.set_power_state)
2781 codec->patch_ops.set_power_state(codec, fg,
2782 power_state);
2783 else {
2784 state = power_state;
2785 if (codec->power_filter)
2786 state = codec->power_filter(codec, fg, state);
2787 if (state == power_state || power_state != AC_PWRST_D3)
2788 snd_hda_codec_read(codec, fg, flags,
2789 AC_VERB_SET_POWER_STATE,
2790 state);
2791 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2792 }
2793 state = hda_sync_power_state(codec, fg, power_state);
2794 if (!(state & AC_PWRST_ERROR))
2795 break;
2796 }
2797
2798 return state;
2799 }
2800
2801 /* sync power states of all widgets;
2802 * this is called at the end of codec parsing
2803 */
sync_power_up_states(struct hda_codec * codec)2804 static void sync_power_up_states(struct hda_codec *codec)
2805 {
2806 hda_nid_t nid;
2807
2808 /* don't care if no filter is used */
2809 if (!codec->power_filter)
2810 return;
2811
2812 for_each_hda_codec_node(nid, codec) {
2813 unsigned int wcaps = get_wcaps(codec, nid);
2814 unsigned int target;
2815 if (!(wcaps & AC_WCAP_POWER))
2816 continue;
2817 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2818 if (target == AC_PWRST_D0)
2819 continue;
2820 if (!snd_hda_check_power_state(codec, nid, target))
2821 snd_hda_codec_write(codec, nid, 0,
2822 AC_VERB_SET_POWER_STATE, target);
2823 }
2824 }
2825
2826 #ifdef CONFIG_SND_HDA_RECONFIG
2827 /* execute additional init verbs */
hda_exec_init_verbs(struct hda_codec * codec)2828 static void hda_exec_init_verbs(struct hda_codec *codec)
2829 {
2830 if (codec->init_verbs.list)
2831 snd_hda_sequence_write(codec, codec->init_verbs.list);
2832 }
2833 #else
hda_exec_init_verbs(struct hda_codec * codec)2834 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2835 #endif
2836
2837 #ifdef CONFIG_PM
2838 /* update the power on/off account with the current jiffies */
update_power_acct(struct hda_codec * codec,bool on)2839 static void update_power_acct(struct hda_codec *codec, bool on)
2840 {
2841 unsigned long delta = jiffies - codec->power_jiffies;
2842
2843 if (on)
2844 codec->power_on_acct += delta;
2845 else
2846 codec->power_off_acct += delta;
2847 codec->power_jiffies += delta;
2848 }
2849
snd_hda_update_power_acct(struct hda_codec * codec)2850 void snd_hda_update_power_acct(struct hda_codec *codec)
2851 {
2852 update_power_acct(codec, hda_codec_is_power_on(codec));
2853 }
2854
2855 /*
2856 * call suspend and power-down; used both from PM and power-save
2857 * this function returns the power state in the end
2858 */
hda_call_codec_suspend(struct hda_codec * codec)2859 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
2860 {
2861 unsigned int state;
2862
2863 atomic_inc(&codec->core.in_pm);
2864
2865 if (codec->patch_ops.suspend)
2866 codec->patch_ops.suspend(codec);
2867 hda_cleanup_all_streams(codec);
2868 state = hda_set_power_state(codec, AC_PWRST_D3);
2869 update_power_acct(codec, true);
2870 atomic_dec(&codec->core.in_pm);
2871 return state;
2872 }
2873
2874 /*
2875 * kick up codec; used both from PM and power-save
2876 */
hda_call_codec_resume(struct hda_codec * codec)2877 static void hda_call_codec_resume(struct hda_codec *codec)
2878 {
2879 atomic_inc(&codec->core.in_pm);
2880
2881 if (codec->core.regmap)
2882 regcache_mark_dirty(codec->core.regmap);
2883
2884 codec->power_jiffies = jiffies;
2885
2886 hda_set_power_state(codec, AC_PWRST_D0);
2887 restore_shutup_pins(codec);
2888 hda_exec_init_verbs(codec);
2889 snd_hda_jack_set_dirty_all(codec);
2890 if (codec->patch_ops.resume)
2891 codec->patch_ops.resume(codec);
2892 else {
2893 if (codec->patch_ops.init)
2894 codec->patch_ops.init(codec);
2895 if (codec->core.regmap)
2896 regcache_sync(codec->core.regmap);
2897 }
2898
2899 if (codec->jackpoll_interval)
2900 hda_jackpoll_work(&codec->jackpoll_work.work);
2901 else
2902 snd_hda_jack_report_sync(codec);
2903 codec->core.dev.power.power_state = PMSG_ON;
2904 atomic_dec(&codec->core.in_pm);
2905 }
2906
hda_codec_runtime_suspend(struct device * dev)2907 static int hda_codec_runtime_suspend(struct device *dev)
2908 {
2909 struct hda_codec *codec = dev_to_hda_codec(dev);
2910 struct hda_pcm *pcm;
2911 unsigned int state;
2912
2913 cancel_delayed_work_sync(&codec->jackpoll_work);
2914 list_for_each_entry(pcm, &codec->pcm_list_head, list)
2915 snd_pcm_suspend_all(pcm->pcm);
2916 state = hda_call_codec_suspend(codec);
2917 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
2918 (state & AC_PWRST_CLK_STOP_OK))
2919 snd_hdac_codec_link_down(&codec->core);
2920 snd_hdac_link_power(&codec->core, false);
2921 return 0;
2922 }
2923
hda_codec_runtime_resume(struct device * dev)2924 static int hda_codec_runtime_resume(struct device *dev)
2925 {
2926 struct hda_codec *codec = dev_to_hda_codec(dev);
2927
2928 snd_hdac_link_power(&codec->core, true);
2929 snd_hdac_codec_link_up(&codec->core);
2930 hda_call_codec_resume(codec);
2931 pm_runtime_mark_last_busy(dev);
2932 return 0;
2933 }
2934 #endif /* CONFIG_PM */
2935
2936 #ifdef CONFIG_PM_SLEEP
hda_codec_force_resume(struct device * dev)2937 static int hda_codec_force_resume(struct device *dev)
2938 {
2939 int ret;
2940
2941 /* The get/put pair below enforces the runtime resume even if the
2942 * device hasn't been used at suspend time. This trick is needed to
2943 * update the jack state change during the sleep.
2944 */
2945 pm_runtime_get_noresume(dev);
2946 ret = pm_runtime_force_resume(dev);
2947 pm_runtime_put(dev);
2948 return ret;
2949 }
2950
hda_codec_pm_suspend(struct device * dev)2951 static int hda_codec_pm_suspend(struct device *dev)
2952 {
2953 dev->power.power_state = PMSG_SUSPEND;
2954 return pm_runtime_force_suspend(dev);
2955 }
2956
hda_codec_pm_resume(struct device * dev)2957 static int hda_codec_pm_resume(struct device *dev)
2958 {
2959 dev->power.power_state = PMSG_RESUME;
2960 return hda_codec_force_resume(dev);
2961 }
2962
hda_codec_pm_freeze(struct device * dev)2963 static int hda_codec_pm_freeze(struct device *dev)
2964 {
2965 dev->power.power_state = PMSG_FREEZE;
2966 return pm_runtime_force_suspend(dev);
2967 }
2968
hda_codec_pm_thaw(struct device * dev)2969 static int hda_codec_pm_thaw(struct device *dev)
2970 {
2971 dev->power.power_state = PMSG_THAW;
2972 return hda_codec_force_resume(dev);
2973 }
2974
hda_codec_pm_restore(struct device * dev)2975 static int hda_codec_pm_restore(struct device *dev)
2976 {
2977 dev->power.power_state = PMSG_RESTORE;
2978 return hda_codec_force_resume(dev);
2979 }
2980 #endif /* CONFIG_PM_SLEEP */
2981
2982 /* referred in hda_bind.c */
2983 const struct dev_pm_ops hda_codec_driver_pm = {
2984 #ifdef CONFIG_PM_SLEEP
2985 .suspend = hda_codec_pm_suspend,
2986 .resume = hda_codec_pm_resume,
2987 .freeze = hda_codec_pm_freeze,
2988 .thaw = hda_codec_pm_thaw,
2989 .poweroff = hda_codec_pm_suspend,
2990 .restore = hda_codec_pm_restore,
2991 #endif /* CONFIG_PM_SLEEP */
2992 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
2993 NULL)
2994 };
2995
2996 /*
2997 * add standard channel maps if not specified
2998 */
add_std_chmaps(struct hda_codec * codec)2999 static int add_std_chmaps(struct hda_codec *codec)
3000 {
3001 struct hda_pcm *pcm;
3002 int str, err;
3003
3004 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3005 for (str = 0; str < 2; str++) {
3006 struct hda_pcm_stream *hinfo = &pcm->stream[str];
3007 struct snd_pcm_chmap *chmap;
3008 const struct snd_pcm_chmap_elem *elem;
3009
3010 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
3011 continue;
3012 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3013 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3014 hinfo->channels_max,
3015 0, &chmap);
3016 if (err < 0)
3017 return err;
3018 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3019 }
3020 }
3021 return 0;
3022 }
3023
3024 /* default channel maps for 2.1 speakers;
3025 * since HD-audio supports only stereo, odd number channels are omitted
3026 */
3027 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3028 { .channels = 2,
3029 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3030 { .channels = 4,
3031 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3032 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3033 { }
3034 };
3035 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3036
snd_hda_codec_build_controls(struct hda_codec * codec)3037 int snd_hda_codec_build_controls(struct hda_codec *codec)
3038 {
3039 int err = 0;
3040 hda_exec_init_verbs(codec);
3041 /* continue to initialize... */
3042 if (codec->patch_ops.init)
3043 err = codec->patch_ops.init(codec);
3044 if (!err && codec->patch_ops.build_controls)
3045 err = codec->patch_ops.build_controls(codec);
3046 if (err < 0)
3047 return err;
3048
3049 /* we create chmaps here instead of build_pcms */
3050 err = add_std_chmaps(codec);
3051 if (err < 0)
3052 return err;
3053
3054 if (codec->jackpoll_interval)
3055 hda_jackpoll_work(&codec->jackpoll_work.work);
3056 else
3057 snd_hda_jack_report_sync(codec); /* call at the last init point */
3058 sync_power_up_states(codec);
3059 return 0;
3060 }
3061
3062 /*
3063 * PCM stuff
3064 */
hda_pcm_default_open_close(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)3065 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3066 struct hda_codec *codec,
3067 struct snd_pcm_substream *substream)
3068 {
3069 return 0;
3070 }
3071
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)3072 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3073 struct hda_codec *codec,
3074 unsigned int stream_tag,
3075 unsigned int format,
3076 struct snd_pcm_substream *substream)
3077 {
3078 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3079 return 0;
3080 }
3081
hda_pcm_default_cleanup(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)3082 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3083 struct hda_codec *codec,
3084 struct snd_pcm_substream *substream)
3085 {
3086 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3087 return 0;
3088 }
3089
set_pcm_default_values(struct hda_codec * codec,struct hda_pcm_stream * info)3090 static int set_pcm_default_values(struct hda_codec *codec,
3091 struct hda_pcm_stream *info)
3092 {
3093 int err;
3094
3095 /* query support PCM information from the given NID */
3096 if (info->nid && (!info->rates || !info->formats)) {
3097 err = snd_hda_query_supported_pcm(codec, info->nid,
3098 info->rates ? NULL : &info->rates,
3099 info->formats ? NULL : &info->formats,
3100 info->maxbps ? NULL : &info->maxbps);
3101 if (err < 0)
3102 return err;
3103 }
3104 if (info->ops.open == NULL)
3105 info->ops.open = hda_pcm_default_open_close;
3106 if (info->ops.close == NULL)
3107 info->ops.close = hda_pcm_default_open_close;
3108 if (info->ops.prepare == NULL) {
3109 if (snd_BUG_ON(!info->nid))
3110 return -EINVAL;
3111 info->ops.prepare = hda_pcm_default_prepare;
3112 }
3113 if (info->ops.cleanup == NULL) {
3114 if (snd_BUG_ON(!info->nid))
3115 return -EINVAL;
3116 info->ops.cleanup = hda_pcm_default_cleanup;
3117 }
3118 return 0;
3119 }
3120
3121 /*
3122 * codec prepare/cleanup entries
3123 */
3124 /**
3125 * snd_hda_codec_prepare - Prepare a stream
3126 * @codec: the HDA codec
3127 * @hinfo: PCM information
3128 * @stream: stream tag to assign
3129 * @format: format id to assign
3130 * @substream: PCM substream to assign
3131 *
3132 * Calls the prepare callback set by the codec with the given arguments.
3133 * Clean up the inactive streams when successful.
3134 */
snd_hda_codec_prepare(struct hda_codec * codec,struct hda_pcm_stream * hinfo,unsigned int stream,unsigned int format,struct snd_pcm_substream * substream)3135 int snd_hda_codec_prepare(struct hda_codec *codec,
3136 struct hda_pcm_stream *hinfo,
3137 unsigned int stream,
3138 unsigned int format,
3139 struct snd_pcm_substream *substream)
3140 {
3141 int ret;
3142 mutex_lock(&codec->bus->prepare_mutex);
3143 if (hinfo->ops.prepare)
3144 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3145 substream);
3146 else
3147 ret = -ENODEV;
3148 if (ret >= 0)
3149 purify_inactive_streams(codec);
3150 mutex_unlock(&codec->bus->prepare_mutex);
3151 return ret;
3152 }
3153 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3154
3155 /**
3156 * snd_hda_codec_cleanup - Prepare a stream
3157 * @codec: the HDA codec
3158 * @hinfo: PCM information
3159 * @substream: PCM substream
3160 *
3161 * Calls the cleanup callback set by the codec with the given arguments.
3162 */
snd_hda_codec_cleanup(struct hda_codec * codec,struct hda_pcm_stream * hinfo,struct snd_pcm_substream * substream)3163 void snd_hda_codec_cleanup(struct hda_codec *codec,
3164 struct hda_pcm_stream *hinfo,
3165 struct snd_pcm_substream *substream)
3166 {
3167 mutex_lock(&codec->bus->prepare_mutex);
3168 if (hinfo->ops.cleanup)
3169 hinfo->ops.cleanup(hinfo, codec, substream);
3170 mutex_unlock(&codec->bus->prepare_mutex);
3171 }
3172 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3173
3174 /* global */
3175 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3176 "Audio", "SPDIF", "HDMI", "Modem"
3177 };
3178
3179 /*
3180 * get the empty PCM device number to assign
3181 */
get_empty_pcm_device(struct hda_bus * bus,unsigned int type)3182 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3183 {
3184 /* audio device indices; not linear to keep compatibility */
3185 /* assigned to static slots up to dev#10; if more needed, assign
3186 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3187 */
3188 static int audio_idx[HDA_PCM_NTYPES][5] = {
3189 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3190 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3191 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3192 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3193 };
3194 int i;
3195
3196 if (type >= HDA_PCM_NTYPES) {
3197 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3198 return -EINVAL;
3199 }
3200
3201 for (i = 0; audio_idx[type][i] >= 0; i++) {
3202 #ifndef CONFIG_SND_DYNAMIC_MINORS
3203 if (audio_idx[type][i] >= 8)
3204 break;
3205 #endif
3206 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3207 return audio_idx[type][i];
3208 }
3209
3210 #ifdef CONFIG_SND_DYNAMIC_MINORS
3211 /* non-fixed slots starting from 10 */
3212 for (i = 10; i < 32; i++) {
3213 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3214 return i;
3215 }
3216 #endif
3217
3218 dev_warn(bus->card->dev, "Too many %s devices\n",
3219 snd_hda_pcm_type_name[type]);
3220 #ifndef CONFIG_SND_DYNAMIC_MINORS
3221 dev_warn(bus->card->dev,
3222 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3223 #endif
3224 return -EAGAIN;
3225 }
3226
3227 /* call build_pcms ops of the given codec and set up the default parameters */
snd_hda_codec_parse_pcms(struct hda_codec * codec)3228 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3229 {
3230 struct hda_pcm *cpcm;
3231 int err;
3232
3233 if (!list_empty(&codec->pcm_list_head))
3234 return 0; /* already parsed */
3235
3236 if (!codec->patch_ops.build_pcms)
3237 return 0;
3238
3239 err = codec->patch_ops.build_pcms(codec);
3240 if (err < 0) {
3241 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3242 codec->core.addr, err);
3243 return err;
3244 }
3245
3246 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3247 int stream;
3248
3249 for (stream = 0; stream < 2; stream++) {
3250 struct hda_pcm_stream *info = &cpcm->stream[stream];
3251
3252 if (!info->substreams)
3253 continue;
3254 err = set_pcm_default_values(codec, info);
3255 if (err < 0) {
3256 codec_warn(codec,
3257 "fail to setup default for PCM %s\n",
3258 cpcm->name);
3259 return err;
3260 }
3261 }
3262 }
3263
3264 return 0;
3265 }
3266
3267 /* assign all PCMs of the given codec */
snd_hda_codec_build_pcms(struct hda_codec * codec)3268 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3269 {
3270 struct hda_bus *bus = codec->bus;
3271 struct hda_pcm *cpcm;
3272 int dev, err;
3273
3274 err = snd_hda_codec_parse_pcms(codec);
3275 if (err < 0)
3276 return err;
3277
3278 /* attach a new PCM streams */
3279 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3280 if (cpcm->pcm)
3281 continue; /* already attached */
3282 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3283 continue; /* no substreams assigned */
3284
3285 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3286 if (dev < 0) {
3287 cpcm->device = SNDRV_PCM_INVALID_DEVICE;
3288 continue; /* no fatal error */
3289 }
3290 cpcm->device = dev;
3291 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
3292 if (err < 0) {
3293 codec_err(codec,
3294 "cannot attach PCM stream %d for codec #%d\n",
3295 dev, codec->core.addr);
3296 continue; /* no fatal error */
3297 }
3298 }
3299
3300 return 0;
3301 }
3302
3303 /**
3304 * snd_hda_add_new_ctls - create controls from the array
3305 * @codec: the HDA codec
3306 * @knew: the array of struct snd_kcontrol_new
3307 *
3308 * This helper function creates and add new controls in the given array.
3309 * The array must be terminated with an empty entry as terminator.
3310 *
3311 * Returns 0 if successful, or a negative error code.
3312 */
snd_hda_add_new_ctls(struct hda_codec * codec,const struct snd_kcontrol_new * knew)3313 int snd_hda_add_new_ctls(struct hda_codec *codec,
3314 const struct snd_kcontrol_new *knew)
3315 {
3316 int err;
3317
3318 for (; knew->name; knew++) {
3319 struct snd_kcontrol *kctl;
3320 int addr = 0, idx = 0;
3321 if (knew->iface == -1) /* skip this codec private value */
3322 continue;
3323 for (;;) {
3324 kctl = snd_ctl_new1(knew, codec);
3325 if (!kctl)
3326 return -ENOMEM;
3327 if (addr > 0)
3328 kctl->id.device = addr;
3329 if (idx > 0)
3330 kctl->id.index = idx;
3331 err = snd_hda_ctl_add(codec, 0, kctl);
3332 if (!err)
3333 break;
3334 /* try first with another device index corresponding to
3335 * the codec addr; if it still fails (or it's the
3336 * primary codec), then try another control index
3337 */
3338 if (!addr && codec->core.addr)
3339 addr = codec->core.addr;
3340 else if (!idx && !knew->index) {
3341 idx = find_empty_mixer_ctl_idx(codec,
3342 knew->name, 0);
3343 if (idx <= 0)
3344 return err;
3345 } else
3346 return err;
3347 }
3348 }
3349 return 0;
3350 }
3351 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3352
3353 #ifdef CONFIG_PM
codec_set_power_save(struct hda_codec * codec,int delay)3354 static void codec_set_power_save(struct hda_codec *codec, int delay)
3355 {
3356 struct device *dev = hda_codec_dev(codec);
3357
3358 if (delay == 0 && codec->auto_runtime_pm)
3359 delay = 3000;
3360
3361 if (delay > 0) {
3362 pm_runtime_set_autosuspend_delay(dev, delay);
3363 pm_runtime_use_autosuspend(dev);
3364 pm_runtime_allow(dev);
3365 if (!pm_runtime_suspended(dev))
3366 pm_runtime_mark_last_busy(dev);
3367 } else {
3368 pm_runtime_dont_use_autosuspend(dev);
3369 pm_runtime_forbid(dev);
3370 }
3371 }
3372
3373 /**
3374 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3375 * @bus: HD-audio bus
3376 * @delay: autosuspend delay in msec, 0 = off
3377 *
3378 * Synchronize the runtime PM autosuspend state from the power_save option.
3379 */
snd_hda_set_power_save(struct hda_bus * bus,int delay)3380 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3381 {
3382 struct hda_codec *c;
3383
3384 list_for_each_codec(c, bus)
3385 codec_set_power_save(c, delay);
3386 }
3387 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3388
3389 /**
3390 * snd_hda_check_amp_list_power - Check the amp list and update the power
3391 * @codec: HD-audio codec
3392 * @check: the object containing an AMP list and the status
3393 * @nid: NID to check / update
3394 *
3395 * Check whether the given NID is in the amp list. If it's in the list,
3396 * check the current AMP status, and update the the power-status according
3397 * to the mute status.
3398 *
3399 * This function is supposed to be set or called from the check_power_status
3400 * patch ops.
3401 */
snd_hda_check_amp_list_power(struct hda_codec * codec,struct hda_loopback_check * check,hda_nid_t nid)3402 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3403 struct hda_loopback_check *check,
3404 hda_nid_t nid)
3405 {
3406 const struct hda_amp_list *p;
3407 int ch, v;
3408
3409 if (!check->amplist)
3410 return 0;
3411 for (p = check->amplist; p->nid; p++) {
3412 if (p->nid == nid)
3413 break;
3414 }
3415 if (!p->nid)
3416 return 0; /* nothing changed */
3417
3418 for (p = check->amplist; p->nid; p++) {
3419 for (ch = 0; ch < 2; ch++) {
3420 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3421 p->idx);
3422 if (!(v & HDA_AMP_MUTE) && v > 0) {
3423 if (!check->power_on) {
3424 check->power_on = 1;
3425 snd_hda_power_up_pm(codec);
3426 }
3427 return 1;
3428 }
3429 }
3430 }
3431 if (check->power_on) {
3432 check->power_on = 0;
3433 snd_hda_power_down_pm(codec);
3434 }
3435 return 0;
3436 }
3437 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3438 #endif
3439
3440 /*
3441 * input MUX helper
3442 */
3443
3444 /**
3445 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3446 * @imux: imux helper object
3447 * @uinfo: pointer to get/store the data
3448 */
snd_hda_input_mux_info(const struct hda_input_mux * imux,struct snd_ctl_elem_info * uinfo)3449 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3450 struct snd_ctl_elem_info *uinfo)
3451 {
3452 unsigned int index;
3453
3454 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3455 uinfo->count = 1;
3456 uinfo->value.enumerated.items = imux->num_items;
3457 if (!imux->num_items)
3458 return 0;
3459 index = uinfo->value.enumerated.item;
3460 if (index >= imux->num_items)
3461 index = imux->num_items - 1;
3462 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3463 return 0;
3464 }
3465 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3466
3467 /**
3468 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3469 * @codec: the HDA codec
3470 * @imux: imux helper object
3471 * @ucontrol: pointer to get/store the data
3472 * @nid: input mux NID
3473 * @cur_val: pointer to get/store the current imux value
3474 */
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)3475 int snd_hda_input_mux_put(struct hda_codec *codec,
3476 const struct hda_input_mux *imux,
3477 struct snd_ctl_elem_value *ucontrol,
3478 hda_nid_t nid,
3479 unsigned int *cur_val)
3480 {
3481 unsigned int idx;
3482
3483 if (!imux->num_items)
3484 return 0;
3485 idx = ucontrol->value.enumerated.item[0];
3486 if (idx >= imux->num_items)
3487 idx = imux->num_items - 1;
3488 if (*cur_val == idx)
3489 return 0;
3490 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3491 imux->items[idx].index);
3492 *cur_val = idx;
3493 return 1;
3494 }
3495 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3496
3497
3498 /**
3499 * snd_hda_enum_helper_info - Helper for simple enum ctls
3500 * @kcontrol: ctl element
3501 * @uinfo: pointer to get/store the data
3502 * @num_items: number of enum items
3503 * @texts: enum item string array
3504 *
3505 * process kcontrol info callback of a simple string enum array
3506 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3507 */
snd_hda_enum_helper_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo,int num_items,const char * const * texts)3508 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3509 struct snd_ctl_elem_info *uinfo,
3510 int num_items, const char * const *texts)
3511 {
3512 static const char * const texts_default[] = {
3513 "Disabled", "Enabled"
3514 };
3515
3516 if (!texts || !num_items) {
3517 num_items = 2;
3518 texts = texts_default;
3519 }
3520
3521 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3522 }
3523 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3524
3525 /*
3526 * Multi-channel / digital-out PCM helper functions
3527 */
3528
3529 /* setup SPDIF output stream */
setup_dig_out_stream(struct hda_codec * codec,hda_nid_t nid,unsigned int stream_tag,unsigned int format)3530 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3531 unsigned int stream_tag, unsigned int format)
3532 {
3533 struct hda_spdif_out *spdif;
3534 unsigned int curr_fmt;
3535 bool reset;
3536
3537 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3538 /* Add sanity check to pass klockwork check.
3539 * This should never happen.
3540 */
3541 if (WARN_ON(spdif == NULL))
3542 return;
3543
3544 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3545 AC_VERB_GET_STREAM_FORMAT, 0);
3546 reset = codec->spdif_status_reset &&
3547 (spdif->ctls & AC_DIG1_ENABLE) &&
3548 curr_fmt != format;
3549
3550 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3551 updated */
3552 if (reset)
3553 set_dig_out_convert(codec, nid,
3554 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3555 -1);
3556 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3557 if (codec->slave_dig_outs) {
3558 const hda_nid_t *d;
3559 for (d = codec->slave_dig_outs; *d; d++)
3560 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3561 format);
3562 }
3563 /* turn on again (if needed) */
3564 if (reset)
3565 set_dig_out_convert(codec, nid,
3566 spdif->ctls & 0xff, -1);
3567 }
3568
cleanup_dig_out_stream(struct hda_codec * codec,hda_nid_t nid)3569 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3570 {
3571 snd_hda_codec_cleanup_stream(codec, nid);
3572 if (codec->slave_dig_outs) {
3573 const hda_nid_t *d;
3574 for (d = codec->slave_dig_outs; *d; d++)
3575 snd_hda_codec_cleanup_stream(codec, *d);
3576 }
3577 }
3578
3579 /**
3580 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3581 * @codec: the HDA codec
3582 * @mout: hda_multi_out object
3583 */
snd_hda_multi_out_dig_open(struct hda_codec * codec,struct hda_multi_out * mout)3584 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3585 struct hda_multi_out *mout)
3586 {
3587 mutex_lock(&codec->spdif_mutex);
3588 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3589 /* already opened as analog dup; reset it once */
3590 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3591 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3592 mutex_unlock(&codec->spdif_mutex);
3593 return 0;
3594 }
3595 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3596
3597 /**
3598 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3599 * @codec: the HDA codec
3600 * @mout: hda_multi_out object
3601 * @stream_tag: stream tag to assign
3602 * @format: format id to assign
3603 * @substream: PCM substream to assign
3604 */
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)3605 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3606 struct hda_multi_out *mout,
3607 unsigned int stream_tag,
3608 unsigned int format,
3609 struct snd_pcm_substream *substream)
3610 {
3611 mutex_lock(&codec->spdif_mutex);
3612 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3613 mutex_unlock(&codec->spdif_mutex);
3614 return 0;
3615 }
3616 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3617
3618 /**
3619 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3620 * @codec: the HDA codec
3621 * @mout: hda_multi_out object
3622 */
snd_hda_multi_out_dig_cleanup(struct hda_codec * codec,struct hda_multi_out * mout)3623 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3624 struct hda_multi_out *mout)
3625 {
3626 mutex_lock(&codec->spdif_mutex);
3627 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3628 mutex_unlock(&codec->spdif_mutex);
3629 return 0;
3630 }
3631 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3632
3633 /**
3634 * snd_hda_multi_out_dig_close - release the digital out stream
3635 * @codec: the HDA codec
3636 * @mout: hda_multi_out object
3637 */
snd_hda_multi_out_dig_close(struct hda_codec * codec,struct hda_multi_out * mout)3638 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3639 struct hda_multi_out *mout)
3640 {
3641 mutex_lock(&codec->spdif_mutex);
3642 mout->dig_out_used = 0;
3643 mutex_unlock(&codec->spdif_mutex);
3644 return 0;
3645 }
3646 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3647
3648 /**
3649 * snd_hda_multi_out_analog_open - open analog outputs
3650 * @codec: the HDA codec
3651 * @mout: hda_multi_out object
3652 * @substream: PCM substream to assign
3653 * @hinfo: PCM information to assign
3654 *
3655 * Open analog outputs and set up the hw-constraints.
3656 * If the digital outputs can be opened as slave, open the digital
3657 * outputs, too.
3658 */
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)3659 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3660 struct hda_multi_out *mout,
3661 struct snd_pcm_substream *substream,
3662 struct hda_pcm_stream *hinfo)
3663 {
3664 struct snd_pcm_runtime *runtime = substream->runtime;
3665 runtime->hw.channels_max = mout->max_channels;
3666 if (mout->dig_out_nid) {
3667 if (!mout->analog_rates) {
3668 mout->analog_rates = hinfo->rates;
3669 mout->analog_formats = hinfo->formats;
3670 mout->analog_maxbps = hinfo->maxbps;
3671 } else {
3672 runtime->hw.rates = mout->analog_rates;
3673 runtime->hw.formats = mout->analog_formats;
3674 hinfo->maxbps = mout->analog_maxbps;
3675 }
3676 if (!mout->spdif_rates) {
3677 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3678 &mout->spdif_rates,
3679 &mout->spdif_formats,
3680 &mout->spdif_maxbps);
3681 }
3682 mutex_lock(&codec->spdif_mutex);
3683 if (mout->share_spdif) {
3684 if ((runtime->hw.rates & mout->spdif_rates) &&
3685 (runtime->hw.formats & mout->spdif_formats)) {
3686 runtime->hw.rates &= mout->spdif_rates;
3687 runtime->hw.formats &= mout->spdif_formats;
3688 if (mout->spdif_maxbps < hinfo->maxbps)
3689 hinfo->maxbps = mout->spdif_maxbps;
3690 } else {
3691 mout->share_spdif = 0;
3692 /* FIXME: need notify? */
3693 }
3694 }
3695 mutex_unlock(&codec->spdif_mutex);
3696 }
3697 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3698 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3699 }
3700 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3701
3702 /**
3703 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3704 * @codec: the HDA codec
3705 * @mout: hda_multi_out object
3706 * @stream_tag: stream tag to assign
3707 * @format: format id to assign
3708 * @substream: PCM substream to assign
3709 *
3710 * Set up the i/o for analog out.
3711 * When the digital out is available, copy the front out to digital out, too.
3712 */
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)3713 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3714 struct hda_multi_out *mout,
3715 unsigned int stream_tag,
3716 unsigned int format,
3717 struct snd_pcm_substream *substream)
3718 {
3719 const hda_nid_t *nids = mout->dac_nids;
3720 int chs = substream->runtime->channels;
3721 struct hda_spdif_out *spdif;
3722 int i;
3723
3724 mutex_lock(&codec->spdif_mutex);
3725 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3726 if (mout->dig_out_nid && mout->share_spdif &&
3727 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3728 if (chs == 2 && spdif != NULL &&
3729 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3730 format) &&
3731 !(spdif->status & IEC958_AES0_NONAUDIO)) {
3732 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3733 setup_dig_out_stream(codec, mout->dig_out_nid,
3734 stream_tag, format);
3735 } else {
3736 mout->dig_out_used = 0;
3737 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3738 }
3739 }
3740 mutex_unlock(&codec->spdif_mutex);
3741
3742 /* front */
3743 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3744 0, format);
3745 if (!mout->no_share_stream &&
3746 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3747 /* headphone out will just decode front left/right (stereo) */
3748 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3749 0, format);
3750 /* extra outputs copied from front */
3751 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3752 if (!mout->no_share_stream && mout->hp_out_nid[i])
3753 snd_hda_codec_setup_stream(codec,
3754 mout->hp_out_nid[i],
3755 stream_tag, 0, format);
3756
3757 /* surrounds */
3758 for (i = 1; i < mout->num_dacs; i++) {
3759 if (chs >= (i + 1) * 2) /* independent out */
3760 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3761 i * 2, format);
3762 else if (!mout->no_share_stream) /* copy front */
3763 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3764 0, format);
3765 }
3766
3767 /* extra surrounds */
3768 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3769 int ch = 0;
3770 if (!mout->extra_out_nid[i])
3771 break;
3772 if (chs >= (i + 1) * 2)
3773 ch = i * 2;
3774 else if (!mout->no_share_stream)
3775 break;
3776 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3777 stream_tag, ch, format);
3778 }
3779
3780 return 0;
3781 }
3782 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3783
3784 /**
3785 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3786 * @codec: the HDA codec
3787 * @mout: hda_multi_out object
3788 */
snd_hda_multi_out_analog_cleanup(struct hda_codec * codec,struct hda_multi_out * mout)3789 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3790 struct hda_multi_out *mout)
3791 {
3792 const hda_nid_t *nids = mout->dac_nids;
3793 int i;
3794
3795 for (i = 0; i < mout->num_dacs; i++)
3796 snd_hda_codec_cleanup_stream(codec, nids[i]);
3797 if (mout->hp_nid)
3798 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3799 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3800 if (mout->hp_out_nid[i])
3801 snd_hda_codec_cleanup_stream(codec,
3802 mout->hp_out_nid[i]);
3803 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3804 if (mout->extra_out_nid[i])
3805 snd_hda_codec_cleanup_stream(codec,
3806 mout->extra_out_nid[i]);
3807 mutex_lock(&codec->spdif_mutex);
3808 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3809 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3810 mout->dig_out_used = 0;
3811 }
3812 mutex_unlock(&codec->spdif_mutex);
3813 return 0;
3814 }
3815 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3816
3817 /**
3818 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3819 * @codec: the HDA codec
3820 * @pin: referred pin NID
3821 *
3822 * Guess the suitable VREF pin bits to be set as the pin-control value.
3823 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3824 */
snd_hda_get_default_vref(struct hda_codec * codec,hda_nid_t pin)3825 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3826 {
3827 unsigned int pincap;
3828 unsigned int oldval;
3829 oldval = snd_hda_codec_read(codec, pin, 0,
3830 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3831 pincap = snd_hda_query_pin_caps(codec, pin);
3832 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3833 /* Exception: if the default pin setup is vref50, we give it priority */
3834 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3835 return AC_PINCTL_VREF_80;
3836 else if (pincap & AC_PINCAP_VREF_50)
3837 return AC_PINCTL_VREF_50;
3838 else if (pincap & AC_PINCAP_VREF_100)
3839 return AC_PINCTL_VREF_100;
3840 else if (pincap & AC_PINCAP_VREF_GRD)
3841 return AC_PINCTL_VREF_GRD;
3842 return AC_PINCTL_VREF_HIZ;
3843 }
3844 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3845
3846 /**
3847 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3848 * @codec: the HDA codec
3849 * @pin: referred pin NID
3850 * @val: pin ctl value to audit
3851 */
snd_hda_correct_pin_ctl(struct hda_codec * codec,hda_nid_t pin,unsigned int val)3852 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3853 hda_nid_t pin, unsigned int val)
3854 {
3855 static unsigned int cap_lists[][2] = {
3856 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3857 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3858 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3859 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3860 };
3861 unsigned int cap;
3862
3863 if (!val)
3864 return 0;
3865 cap = snd_hda_query_pin_caps(codec, pin);
3866 if (!cap)
3867 return val; /* don't know what to do... */
3868
3869 if (val & AC_PINCTL_OUT_EN) {
3870 if (!(cap & AC_PINCAP_OUT))
3871 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3872 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3873 val &= ~AC_PINCTL_HP_EN;
3874 }
3875
3876 if (val & AC_PINCTL_IN_EN) {
3877 if (!(cap & AC_PINCAP_IN))
3878 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3879 else {
3880 unsigned int vcap, vref;
3881 int i;
3882 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3883 vref = val & AC_PINCTL_VREFEN;
3884 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3885 if (vref == cap_lists[i][0] &&
3886 !(vcap & cap_lists[i][1])) {
3887 if (i == ARRAY_SIZE(cap_lists) - 1)
3888 vref = AC_PINCTL_VREF_HIZ;
3889 else
3890 vref = cap_lists[i + 1][0];
3891 }
3892 }
3893 val &= ~AC_PINCTL_VREFEN;
3894 val |= vref;
3895 }
3896 }
3897
3898 return val;
3899 }
3900 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
3901
3902 /**
3903 * _snd_hda_pin_ctl - Helper to set pin ctl value
3904 * @codec: the HDA codec
3905 * @pin: referred pin NID
3906 * @val: pin control value to set
3907 * @cached: access over codec pinctl cache or direct write
3908 *
3909 * This function is a helper to set a pin ctl value more safely.
3910 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
3911 * value in pin target array via snd_hda_codec_set_pin_target(), then
3912 * actually writes the value via either snd_hda_codec_update_cache() or
3913 * snd_hda_codec_write() depending on @cached flag.
3914 */
_snd_hda_set_pin_ctl(struct hda_codec * codec,hda_nid_t pin,unsigned int val,bool cached)3915 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
3916 unsigned int val, bool cached)
3917 {
3918 val = snd_hda_correct_pin_ctl(codec, pin, val);
3919 snd_hda_codec_set_pin_target(codec, pin, val);
3920 if (cached)
3921 return snd_hda_codec_update_cache(codec, pin, 0,
3922 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3923 else
3924 return snd_hda_codec_write(codec, pin, 0,
3925 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3926 }
3927 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
3928
3929 /**
3930 * snd_hda_add_imux_item - Add an item to input_mux
3931 * @codec: the HDA codec
3932 * @imux: imux helper object
3933 * @label: the name of imux item to assign
3934 * @index: index number of imux item to assign
3935 * @type_idx: pointer to store the resultant label index
3936 *
3937 * When the same label is used already in the existing items, the number
3938 * suffix is appended to the label. This label index number is stored
3939 * to type_idx when non-NULL pointer is given.
3940 */
snd_hda_add_imux_item(struct hda_codec * codec,struct hda_input_mux * imux,const char * label,int index,int * type_idx)3941 int snd_hda_add_imux_item(struct hda_codec *codec,
3942 struct hda_input_mux *imux, const char *label,
3943 int index, int *type_idx)
3944 {
3945 int i, label_idx = 0;
3946 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
3947 codec_err(codec, "hda_codec: Too many imux items!\n");
3948 return -EINVAL;
3949 }
3950 for (i = 0; i < imux->num_items; i++) {
3951 if (!strncmp(label, imux->items[i].label, strlen(label)))
3952 label_idx++;
3953 }
3954 if (type_idx)
3955 *type_idx = label_idx;
3956 if (label_idx > 0)
3957 snprintf(imux->items[imux->num_items].label,
3958 sizeof(imux->items[imux->num_items].label),
3959 "%s %d", label, label_idx);
3960 else
3961 strlcpy(imux->items[imux->num_items].label, label,
3962 sizeof(imux->items[imux->num_items].label));
3963 imux->items[imux->num_items].index = index;
3964 imux->num_items++;
3965 return 0;
3966 }
3967 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
3968
3969 /**
3970 * snd_hda_bus_reset_codecs - Reset the bus
3971 * @bus: HD-audio bus
3972 */
snd_hda_bus_reset_codecs(struct hda_bus * bus)3973 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
3974 {
3975 struct hda_codec *codec;
3976
3977 list_for_each_codec(codec, bus) {
3978 /* FIXME: maybe a better way needed for forced reset */
3979 if (current_work() != &codec->jackpoll_work.work)
3980 cancel_delayed_work_sync(&codec->jackpoll_work);
3981 #ifdef CONFIG_PM
3982 if (hda_codec_is_power_on(codec)) {
3983 hda_call_codec_suspend(codec);
3984 hda_call_codec_resume(codec);
3985 }
3986 #endif
3987 }
3988 }
3989
3990 /**
3991 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
3992 * @pcm: PCM caps bits
3993 * @buf: the string buffer to write
3994 * @buflen: the max buffer length
3995 *
3996 * used by hda_proc.c and hda_eld.c
3997 */
snd_print_pcm_bits(int pcm,char * buf,int buflen)3998 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3999 {
4000 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4001 int i, j;
4002
4003 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4004 if (pcm & (AC_SUPPCM_BITS_8 << i))
4005 j += scnprintf(buf + j, buflen - j, " %d", bits[i]);
4006
4007 buf[j] = '\0'; /* necessary when j == 0 */
4008 }
4009 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
4010
4011 MODULE_DESCRIPTION("HDA codec core");
4012 MODULE_LICENSE("GPL");
4013