source: GPL/trunk/alsa-kernel/pci/hda/hda_codec.c

Last change on this file was 777, checked in by David Azarewicz, 7 months ago

Merge from uniaud32-exp branch

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