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