HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / sound / pci / hda / hda_codec.c
blob4962a9d8a572bac33b8fd7813e55f5e2c20c95e7
1 /*
2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/mm.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
29 #include <linux/pm.h>
30 #include <linux/pm_runtime.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include <sound/asoundef.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <sound/jack.h>
37 #include "hda_local.h"
38 #include "hda_beep.h"
39 #include "hda_jack.h"
40 #include <sound/hda_hwdep.h>
42 #ifdef CONFIG_PM
43 #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
44 #define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
46 #else
47 #define codec_in_pm(codec) 0
48 #define hda_codec_is_power_on(codec) 1
49 #endif
51 #define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53 #define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
57 * Send and receive a verb - passed to exec_verb override for hdac_device
59 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
60 unsigned int flags, unsigned int *res)
62 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
63 struct hda_bus *bus = codec->bus;
64 int err;
66 if (cmd == ~0)
67 return -1;
69 again:
70 snd_hda_power_up_pm(codec);
71 mutex_lock(&bus->core.cmd_mutex);
72 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
73 bus->no_response_fallback = 1;
74 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
75 cmd, res);
76 bus->no_response_fallback = 0;
77 mutex_unlock(&bus->core.cmd_mutex);
78 snd_hda_power_down_pm(codec);
79 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
80 if (bus->response_reset) {
81 codec_dbg(codec,
82 "resetting BUS due to fatal communication error\n");
83 snd_hda_bus_reset(bus);
85 goto again;
87 /* clear reset-flag when the communication gets recovered */
88 if (!err || codec_in_pm(codec))
89 bus->response_reset = 0;
90 return err;
93 /**
94 * snd_hda_sequence_write - sequence writes
95 * @codec: the HDA codec
96 * @seq: VERB array to send
98 * Send the commands sequentially from the given array.
99 * The array must be terminated with NID=0.
101 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
103 for (; seq->nid; seq++)
104 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
106 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
108 /* connection list element */
109 struct hda_conn_list {
110 struct list_head list;
111 int len;
112 hda_nid_t nid;
113 hda_nid_t conns[0];
116 /* look up the cached results */
117 static struct hda_conn_list *
118 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
120 struct hda_conn_list *p;
121 list_for_each_entry(p, &codec->conn_list, list) {
122 if (p->nid == nid)
123 return p;
125 return NULL;
128 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
129 const hda_nid_t *list)
131 struct hda_conn_list *p;
133 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
134 if (!p)
135 return -ENOMEM;
136 p->len = len;
137 p->nid = nid;
138 memcpy(p->conns, list, len * sizeof(hda_nid_t));
139 list_add(&p->list, &codec->conn_list);
140 return 0;
143 static void remove_conn_list(struct hda_codec *codec)
145 while (!list_empty(&codec->conn_list)) {
146 struct hda_conn_list *p;
147 p = list_first_entry(&codec->conn_list, typeof(*p), list);
148 list_del(&p->list);
149 kfree(p);
153 /* read the connection and add to the cache */
154 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
156 hda_nid_t list[32];
157 hda_nid_t *result = list;
158 int len;
160 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
161 if (len == -ENOSPC) {
162 len = snd_hda_get_num_raw_conns(codec, nid);
163 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
164 if (!result)
165 return -ENOMEM;
166 len = snd_hda_get_raw_connections(codec, nid, result, len);
168 if (len >= 0)
169 len = snd_hda_override_conn_list(codec, nid, len, result);
170 if (result != list)
171 kfree(result);
172 return len;
176 * snd_hda_get_conn_list - get connection list
177 * @codec: the HDA codec
178 * @nid: NID to parse
179 * @listp: the pointer to store NID list
181 * Parses the connection list of the given widget and stores the pointer
182 * to the list of NIDs.
184 * Returns the number of connections, or a negative error code.
186 * Note that the returned pointer isn't protected against the list
187 * modification. If snd_hda_override_conn_list() might be called
188 * concurrently, protect with a mutex appropriately.
190 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
191 const hda_nid_t **listp)
193 bool added = false;
195 for (;;) {
196 int err;
197 const struct hda_conn_list *p;
199 /* if the connection-list is already cached, read it */
200 p = lookup_conn_list(codec, nid);
201 if (p) {
202 if (listp)
203 *listp = p->conns;
204 return p->len;
206 if (snd_BUG_ON(added))
207 return -EINVAL;
209 err = read_and_add_raw_conns(codec, nid);
210 if (err < 0)
211 return err;
212 added = true;
215 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
218 * snd_hda_get_connections - copy connection list
219 * @codec: the HDA codec
220 * @nid: NID to parse
221 * @conn_list: connection list array; when NULL, checks only the size
222 * @max_conns: max. number of connections to store
224 * Parses the connection list of the given widget and stores the list
225 * of NIDs.
227 * Returns the number of connections, or a negative error code.
229 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
230 hda_nid_t *conn_list, int max_conns)
232 const hda_nid_t *list;
233 int len = snd_hda_get_conn_list(codec, nid, &list);
235 if (len > 0 && conn_list) {
236 if (len > max_conns) {
237 codec_err(codec, "Too many connections %d for NID 0x%x\n",
238 len, nid);
239 return -EINVAL;
241 memcpy(conn_list, list, len * sizeof(hda_nid_t));
244 return len;
246 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
249 * snd_hda_override_conn_list - add/modify the connection-list to cache
250 * @codec: the HDA codec
251 * @nid: NID to parse
252 * @len: number of connection list entries
253 * @list: the list of connection entries
255 * Add or modify the given connection-list to the cache. If the corresponding
256 * cache already exists, invalidate it and append a new one.
258 * Returns zero or a negative error code.
260 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
261 const hda_nid_t *list)
263 struct hda_conn_list *p;
265 p = lookup_conn_list(codec, nid);
266 if (p) {
267 list_del(&p->list);
268 kfree(p);
271 return add_conn_list(codec, nid, len, list);
273 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
276 * snd_hda_get_conn_index - get the connection index of the given NID
277 * @codec: the HDA codec
278 * @mux: NID containing the list
279 * @nid: NID to select
280 * @recursive: 1 when searching NID recursively, otherwise 0
282 * Parses the connection list of the widget @mux and checks whether the
283 * widget @nid is present. If it is, return the connection index.
284 * Otherwise it returns -1.
286 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
287 hda_nid_t nid, int recursive)
289 const hda_nid_t *conn;
290 int i, nums;
292 nums = snd_hda_get_conn_list(codec, mux, &conn);
293 for (i = 0; i < nums; i++)
294 if (conn[i] == nid)
295 return i;
296 if (!recursive)
297 return -1;
298 if (recursive > 10) {
299 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
300 return -1;
302 recursive++;
303 for (i = 0; i < nums; i++) {
304 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
305 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
306 continue;
307 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
308 return i;
310 return -1;
312 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
315 /* return DEVLIST_LEN parameter of the given widget */
316 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
318 unsigned int wcaps = get_wcaps(codec, nid);
319 unsigned int parm;
321 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
322 get_wcaps_type(wcaps) != AC_WID_PIN)
323 return 0;
325 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
326 if (parm == -1)
327 parm = 0;
328 return parm & AC_DEV_LIST_LEN_MASK;
332 * snd_hda_get_devices - copy device list without cache
333 * @codec: the HDA codec
334 * @nid: NID of the pin to parse
335 * @dev_list: device list array
336 * @max_devices: max. number of devices to store
338 * Copy the device list. This info is dynamic and so not cached.
339 * Currently called only from hda_proc.c, so not exported.
341 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
342 u8 *dev_list, int max_devices)
344 unsigned int parm;
345 int i, dev_len, devices;
347 parm = get_num_devices(codec, nid);
348 if (!parm) /* not multi-stream capable */
349 return 0;
351 dev_len = parm + 1;
352 dev_len = dev_len < max_devices ? dev_len : max_devices;
354 devices = 0;
355 while (devices < dev_len) {
356 if (snd_hdac_read(&codec->core, nid,
357 AC_VERB_GET_DEVICE_LIST, devices, &parm))
358 break; /* error */
360 for (i = 0; i < 8; i++) {
361 dev_list[devices] = (u8)parm;
362 parm >>= 4;
363 devices++;
364 if (devices >= dev_len)
365 break;
368 return devices;
372 * read widget caps for each widget and store in cache
374 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
376 int i;
377 hda_nid_t nid;
379 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
380 if (!codec->wcaps)
381 return -ENOMEM;
382 nid = codec->core.start_nid;
383 for (i = 0; i < codec->core.num_nodes; i++, nid++)
384 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
385 nid, AC_PAR_AUDIO_WIDGET_CAP);
386 return 0;
389 /* read all pin default configurations and save codec->init_pins */
390 static int read_pin_defaults(struct hda_codec *codec)
392 hda_nid_t nid;
394 for_each_hda_codec_node(nid, codec) {
395 struct hda_pincfg *pin;
396 unsigned int wcaps = get_wcaps(codec, nid);
397 unsigned int wid_type = get_wcaps_type(wcaps);
398 if (wid_type != AC_WID_PIN)
399 continue;
400 pin = snd_array_new(&codec->init_pins);
401 if (!pin)
402 return -ENOMEM;
403 pin->nid = nid;
404 pin->cfg = snd_hda_codec_read(codec, nid, 0,
405 AC_VERB_GET_CONFIG_DEFAULT, 0);
406 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
407 AC_VERB_GET_PIN_WIDGET_CONTROL,
410 return 0;
413 /* look up the given pin config list and return the item matching with NID */
414 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
415 struct snd_array *array,
416 hda_nid_t nid)
418 int i;
419 for (i = 0; i < array->used; i++) {
420 struct hda_pincfg *pin = snd_array_elem(array, i);
421 if (pin->nid == nid)
422 return pin;
424 return NULL;
427 /* set the current pin config value for the given NID.
428 * the value is cached, and read via snd_hda_codec_get_pincfg()
430 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
431 hda_nid_t nid, unsigned int cfg)
433 struct hda_pincfg *pin;
435 /* the check below may be invalid when pins are added by a fixup
436 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
437 * for now
440 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
441 return -EINVAL;
444 pin = look_up_pincfg(codec, list, nid);
445 if (!pin) {
446 pin = snd_array_new(list);
447 if (!pin)
448 return -ENOMEM;
449 pin->nid = nid;
451 pin->cfg = cfg;
452 return 0;
456 * snd_hda_codec_set_pincfg - Override a pin default configuration
457 * @codec: the HDA codec
458 * @nid: NID to set the pin config
459 * @cfg: the pin default config value
461 * Override a pin default configuration value in the cache.
462 * This value can be read by snd_hda_codec_get_pincfg() in a higher
463 * priority than the real hardware value.
465 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
466 hda_nid_t nid, unsigned int cfg)
468 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
470 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
473 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
474 * @codec: the HDA codec
475 * @nid: NID to get the pin config
477 * Get the current pin config value of the given pin NID.
478 * If the pincfg value is cached or overridden via sysfs or driver,
479 * returns the cached value.
481 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
483 struct hda_pincfg *pin;
485 #ifdef CONFIG_SND_HDA_RECONFIG
487 unsigned int cfg = 0;
488 mutex_lock(&codec->user_mutex);
489 pin = look_up_pincfg(codec, &codec->user_pins, nid);
490 if (pin)
491 cfg = pin->cfg;
492 mutex_unlock(&codec->user_mutex);
493 if (cfg)
494 return cfg;
496 #endif
497 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
498 if (pin)
499 return pin->cfg;
500 pin = look_up_pincfg(codec, &codec->init_pins, nid);
501 if (pin)
502 return pin->cfg;
503 return 0;
505 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
508 * snd_hda_codec_set_pin_target - remember the current pinctl target value
509 * @codec: the HDA codec
510 * @nid: pin NID
511 * @val: assigned pinctl value
513 * This function stores the given value to a pinctl target value in the
514 * pincfg table. This isn't always as same as the actually written value
515 * but can be referred at any time via snd_hda_codec_get_pin_target().
517 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
518 unsigned int val)
520 struct hda_pincfg *pin;
522 pin = look_up_pincfg(codec, &codec->init_pins, nid);
523 if (!pin)
524 return -EINVAL;
525 pin->target = val;
526 return 0;
528 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
531 * snd_hda_codec_get_pin_target - return the current pinctl target value
532 * @codec: the HDA codec
533 * @nid: pin NID
535 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
537 struct hda_pincfg *pin;
539 pin = look_up_pincfg(codec, &codec->init_pins, nid);
540 if (!pin)
541 return 0;
542 return pin->target;
544 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
547 * snd_hda_shutup_pins - Shut up all pins
548 * @codec: the HDA codec
550 * Clear all pin controls to shup up before suspend for avoiding click noise.
551 * The controls aren't cached so that they can be resumed properly.
553 void snd_hda_shutup_pins(struct hda_codec *codec)
555 int i;
556 /* don't shut up pins when unloading the driver; otherwise it breaks
557 * the default pin setup at the next load of the driver
559 if (codec->bus->shutdown)
560 return;
561 for (i = 0; i < codec->init_pins.used; i++) {
562 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
563 /* use read here for syncing after issuing each verb */
564 snd_hda_codec_read(codec, pin->nid, 0,
565 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
567 codec->pins_shutup = 1;
569 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
571 #ifdef CONFIG_PM
572 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
573 static void restore_shutup_pins(struct hda_codec *codec)
575 int i;
576 if (!codec->pins_shutup)
577 return;
578 if (codec->bus->shutdown)
579 return;
580 for (i = 0; i < codec->init_pins.used; i++) {
581 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
582 snd_hda_codec_write(codec, pin->nid, 0,
583 AC_VERB_SET_PIN_WIDGET_CONTROL,
584 pin->ctrl);
586 codec->pins_shutup = 0;
588 #endif
590 static void hda_jackpoll_work(struct work_struct *work)
592 struct hda_codec *codec =
593 container_of(work, struct hda_codec, jackpoll_work.work);
595 snd_hda_jack_set_dirty_all(codec);
596 snd_hda_jack_poll_all(codec);
598 if (!codec->jackpoll_interval)
599 return;
601 schedule_delayed_work(&codec->jackpoll_work,
602 codec->jackpoll_interval);
605 /* release all pincfg lists */
606 static void free_init_pincfgs(struct hda_codec *codec)
608 snd_array_free(&codec->driver_pins);
609 #ifdef CONFIG_SND_HDA_RECONFIG
610 snd_array_free(&codec->user_pins);
611 #endif
612 snd_array_free(&codec->init_pins);
616 * audio-converter setup caches
618 struct hda_cvt_setup {
619 hda_nid_t nid;
620 u8 stream_tag;
621 u8 channel_id;
622 u16 format_id;
623 unsigned char active; /* cvt is currently used */
624 unsigned char dirty; /* setups should be cleared */
627 /* get or create a cache entry for the given audio converter NID */
628 static struct hda_cvt_setup *
629 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
631 struct hda_cvt_setup *p;
632 int i;
634 for (i = 0; i < codec->cvt_setups.used; i++) {
635 p = snd_array_elem(&codec->cvt_setups, i);
636 if (p->nid == nid)
637 return p;
639 p = snd_array_new(&codec->cvt_setups);
640 if (p)
641 p->nid = nid;
642 return p;
646 * PCM device
648 static void release_pcm(struct kref *kref)
650 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
652 if (pcm->pcm)
653 snd_device_free(pcm->codec->card, pcm->pcm);
654 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
655 kfree(pcm->name);
656 kfree(pcm);
659 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
661 kref_put(&pcm->kref, release_pcm);
663 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
665 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
666 const char *fmt, ...)
668 struct hda_pcm *pcm;
669 va_list args;
671 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
672 if (!pcm)
673 return NULL;
675 pcm->codec = codec;
676 kref_init(&pcm->kref);
677 va_start(args, fmt);
678 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
679 va_end(args);
680 if (!pcm->name) {
681 kfree(pcm);
682 return NULL;
685 list_add_tail(&pcm->list, &codec->pcm_list_head);
686 return pcm;
688 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
691 * codec destructor
693 static void codec_release_pcms(struct hda_codec *codec)
695 struct hda_pcm *pcm, *n;
697 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
698 list_del_init(&pcm->list);
699 if (pcm->pcm)
700 snd_device_disconnect(codec->card, pcm->pcm);
701 snd_hda_codec_pcm_put(pcm);
705 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
707 if (codec->registered) {
708 /* pm_runtime_put() is called in snd_hdac_device_exit() */
709 pm_runtime_get_noresume(hda_codec_dev(codec));
710 pm_runtime_disable(hda_codec_dev(codec));
711 codec->registered = 0;
714 cancel_delayed_work_sync(&codec->jackpoll_work);
715 if (!codec->in_freeing)
716 snd_hda_ctls_clear(codec);
717 codec_release_pcms(codec);
718 snd_hda_detach_beep_device(codec);
719 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
720 snd_hda_jack_tbl_clear(codec);
721 codec->proc_widget_hook = NULL;
722 codec->spec = NULL;
724 /* free only driver_pins so that init_pins + user_pins are restored */
725 snd_array_free(&codec->driver_pins);
726 snd_array_free(&codec->cvt_setups);
727 snd_array_free(&codec->spdif_out);
728 snd_array_free(&codec->verbs);
729 codec->preset = NULL;
730 codec->slave_dig_outs = NULL;
731 codec->spdif_status_reset = 0;
732 snd_array_free(&codec->mixers);
733 snd_array_free(&codec->nids);
734 remove_conn_list(codec);
735 snd_hdac_regmap_exit(&codec->core);
738 static unsigned int hda_set_power_state(struct hda_codec *codec,
739 unsigned int power_state);
741 /* also called from hda_bind.c */
742 void snd_hda_codec_register(struct hda_codec *codec)
744 if (codec->registered)
745 return;
746 if (device_is_registered(hda_codec_dev(codec))) {
747 snd_hda_register_beep_device(codec);
748 snd_hdac_link_power(&codec->core, true);
749 pm_runtime_enable(hda_codec_dev(codec));
750 /* it was powered up in snd_hda_codec_new(), now all done */
751 snd_hda_power_down(codec);
752 codec->registered = 1;
756 static int snd_hda_codec_dev_register(struct snd_device *device)
758 snd_hda_codec_register(device->device_data);
759 return 0;
762 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
764 struct hda_codec *codec = device->device_data;
766 snd_hda_detach_beep_device(codec);
767 return 0;
770 static int snd_hda_codec_dev_free(struct snd_device *device)
772 struct hda_codec *codec = device->device_data;
774 codec->in_freeing = 1;
775 snd_hdac_device_unregister(&codec->core);
776 snd_hdac_link_power(&codec->core, false);
777 put_device(hda_codec_dev(codec));
778 return 0;
781 static void snd_hda_codec_dev_release(struct device *dev)
783 struct hda_codec *codec = dev_to_hda_codec(dev);
785 free_init_pincfgs(codec);
786 snd_hdac_device_exit(&codec->core);
787 snd_hda_sysfs_clear(codec);
788 kfree(codec->modelname);
789 kfree(codec->wcaps);
790 kfree(codec);
794 * snd_hda_codec_new - create a HDA codec
795 * @bus: the bus to assign
796 * @codec_addr: the codec address
797 * @codecp: the pointer to store the generated codec
799 * Returns 0 if successful, or a negative error code.
801 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
802 unsigned int codec_addr, struct hda_codec **codecp)
804 struct hda_codec *codec;
805 char component[31];
806 hda_nid_t fg;
807 int err;
808 static struct snd_device_ops dev_ops = {
809 .dev_register = snd_hda_codec_dev_register,
810 .dev_disconnect = snd_hda_codec_dev_disconnect,
811 .dev_free = snd_hda_codec_dev_free,
814 if (snd_BUG_ON(!bus))
815 return -EINVAL;
816 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
817 return -EINVAL;
819 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
820 if (!codec)
821 return -ENOMEM;
823 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
824 err = snd_hdac_device_init(&codec->core, &bus->core, component,
825 codec_addr);
826 if (err < 0) {
827 kfree(codec);
828 return err;
831 codec->core.dev.release = snd_hda_codec_dev_release;
832 codec->core.type = HDA_DEV_LEGACY;
833 codec->core.exec_verb = codec_exec_verb;
835 codec->bus = bus;
836 codec->card = card;
837 codec->addr = codec_addr;
838 mutex_init(&codec->spdif_mutex);
839 mutex_init(&codec->control_mutex);
840 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
841 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
842 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
843 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
844 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
845 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
846 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
847 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
848 INIT_LIST_HEAD(&codec->conn_list);
849 INIT_LIST_HEAD(&codec->pcm_list_head);
851 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
852 codec->depop_delay = -1;
853 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
855 #ifdef CONFIG_PM
856 codec->power_jiffies = jiffies;
857 #endif
859 snd_hda_sysfs_init(codec);
861 if (codec->bus->modelname) {
862 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
863 if (!codec->modelname) {
864 err = -ENOMEM;
865 goto error;
869 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
870 err = read_widget_caps(codec, fg);
871 if (err < 0)
872 goto error;
873 err = read_pin_defaults(codec);
874 if (err < 0)
875 goto error;
877 /* power-up all before initialization */
878 hda_set_power_state(codec, AC_PWRST_D0);
879 codec->core.dev.power.power_state = PMSG_ON;
881 snd_hda_codec_proc_new(codec);
883 snd_hda_create_hwdep(codec);
885 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
886 codec->core.subsystem_id, codec->core.revision_id);
887 snd_component_add(card, component);
889 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
890 if (err < 0)
891 goto error;
893 if (codecp)
894 *codecp = codec;
895 return 0;
897 error:
898 put_device(hda_codec_dev(codec));
899 return err;
901 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
904 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
905 * @codec: the HDA codec
907 * Forcibly refresh the all widget caps and the init pin configurations of
908 * the given codec.
910 int snd_hda_codec_update_widgets(struct hda_codec *codec)
912 hda_nid_t fg;
913 int err;
915 err = snd_hdac_refresh_widget_sysfs(&codec->core);
916 if (err < 0)
917 return err;
919 /* Assume the function group node does not change,
920 * only the widget nodes may change.
922 kfree(codec->wcaps);
923 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
924 err = read_widget_caps(codec, fg);
925 if (err < 0)
926 return err;
928 snd_array_free(&codec->init_pins);
929 err = read_pin_defaults(codec);
931 return err;
933 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
935 /* update the stream-id if changed */
936 static void update_pcm_stream_id(struct hda_codec *codec,
937 struct hda_cvt_setup *p, hda_nid_t nid,
938 u32 stream_tag, int channel_id)
940 unsigned int oldval, newval;
942 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
943 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
944 newval = (stream_tag << 4) | channel_id;
945 if (oldval != newval)
946 snd_hda_codec_write(codec, nid, 0,
947 AC_VERB_SET_CHANNEL_STREAMID,
948 newval);
949 p->stream_tag = stream_tag;
950 p->channel_id = channel_id;
954 /* update the format-id if changed */
955 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
956 hda_nid_t nid, int format)
958 unsigned int oldval;
960 if (p->format_id != format) {
961 oldval = snd_hda_codec_read(codec, nid, 0,
962 AC_VERB_GET_STREAM_FORMAT, 0);
963 if (oldval != format) {
964 msleep(1);
965 snd_hda_codec_write(codec, nid, 0,
966 AC_VERB_SET_STREAM_FORMAT,
967 format);
969 p->format_id = format;
974 * snd_hda_codec_setup_stream - set up the codec for streaming
975 * @codec: the CODEC to set up
976 * @nid: the NID to set up
977 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
978 * @channel_id: channel id to pass, zero based.
979 * @format: stream format.
981 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
982 u32 stream_tag,
983 int channel_id, int format)
985 struct hda_codec *c;
986 struct hda_cvt_setup *p;
987 int type;
988 int i;
990 if (!nid)
991 return;
993 codec_dbg(codec,
994 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
995 nid, stream_tag, channel_id, format);
996 p = get_hda_cvt_setup(codec, nid);
997 if (!p)
998 return;
1000 if (codec->patch_ops.stream_pm)
1001 codec->patch_ops.stream_pm(codec, nid, true);
1002 if (codec->pcm_format_first)
1003 update_pcm_format(codec, p, nid, format);
1004 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1005 if (!codec->pcm_format_first)
1006 update_pcm_format(codec, p, nid, format);
1008 p->active = 1;
1009 p->dirty = 0;
1011 /* make other inactive cvts with the same stream-tag dirty */
1012 type = get_wcaps_type(get_wcaps(codec, nid));
1013 list_for_each_codec(c, codec->bus) {
1014 for (i = 0; i < c->cvt_setups.used; i++) {
1015 p = snd_array_elem(&c->cvt_setups, i);
1016 if (!p->active && p->stream_tag == stream_tag &&
1017 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1018 p->dirty = 1;
1022 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1024 static void really_cleanup_stream(struct hda_codec *codec,
1025 struct hda_cvt_setup *q);
1028 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1029 * @codec: the CODEC to clean up
1030 * @nid: the NID to clean up
1031 * @do_now: really clean up the stream instead of clearing the active flag
1033 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1034 int do_now)
1036 struct hda_cvt_setup *p;
1038 if (!nid)
1039 return;
1041 if (codec->no_sticky_stream)
1042 do_now = 1;
1044 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1045 p = get_hda_cvt_setup(codec, nid);
1046 if (p) {
1047 /* here we just clear the active flag when do_now isn't set;
1048 * actual clean-ups will be done later in
1049 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1051 if (do_now)
1052 really_cleanup_stream(codec, p);
1053 else
1054 p->active = 0;
1057 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1059 static void really_cleanup_stream(struct hda_codec *codec,
1060 struct hda_cvt_setup *q)
1062 hda_nid_t nid = q->nid;
1063 if (q->stream_tag || q->channel_id)
1064 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1065 if (q->format_id)
1066 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1068 memset(q, 0, sizeof(*q));
1069 q->nid = nid;
1070 if (codec->patch_ops.stream_pm)
1071 codec->patch_ops.stream_pm(codec, nid, false);
1074 /* clean up the all conflicting obsolete streams */
1075 static void purify_inactive_streams(struct hda_codec *codec)
1077 struct hda_codec *c;
1078 int i;
1080 list_for_each_codec(c, codec->bus) {
1081 for (i = 0; i < c->cvt_setups.used; i++) {
1082 struct hda_cvt_setup *p;
1083 p = snd_array_elem(&c->cvt_setups, i);
1084 if (p->dirty)
1085 really_cleanup_stream(c, p);
1090 #ifdef CONFIG_PM
1091 /* clean up all streams; called from suspend */
1092 static void hda_cleanup_all_streams(struct hda_codec *codec)
1094 int i;
1096 for (i = 0; i < codec->cvt_setups.used; i++) {
1097 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1098 if (p->stream_tag)
1099 really_cleanup_stream(codec, p);
1102 #endif
1105 * amp access functions
1109 * query_amp_caps - query AMP capabilities
1110 * @codec: the HD-auio codec
1111 * @nid: the NID to query
1112 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1114 * Query AMP capabilities for the given widget and direction.
1115 * Returns the obtained capability bits.
1117 * When cap bits have been already read, this doesn't read again but
1118 * returns the cached value.
1120 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1122 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1123 nid = codec->core.afg;
1124 return snd_hda_param_read(codec, nid,
1125 direction == HDA_OUTPUT ?
1126 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1128 EXPORT_SYMBOL_GPL(query_amp_caps);
1131 * snd_hda_check_amp_caps - query AMP capabilities
1132 * @codec: the HD-audio codec
1133 * @nid: the NID to query
1134 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1135 * @bits: bit mask to check the result
1137 * Check whether the widget has the given amp capability for the direction.
1139 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1140 int dir, unsigned int bits)
1142 if (!nid)
1143 return false;
1144 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1145 if (query_amp_caps(codec, nid, dir) & bits)
1146 return true;
1147 return false;
1149 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1152 * snd_hda_override_amp_caps - Override the AMP capabilities
1153 * @codec: the CODEC to clean up
1154 * @nid: the NID to clean up
1155 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1156 * @caps: the capability bits to set
1158 * Override the cached AMP caps bits value by the given one.
1159 * This function is useful if the driver needs to adjust the AMP ranges,
1160 * e.g. limit to 0dB, etc.
1162 * Returns zero if successful or a negative error code.
1164 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1165 unsigned int caps)
1167 unsigned int parm;
1169 snd_hda_override_wcaps(codec, nid,
1170 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1171 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1172 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1174 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1177 * snd_hda_codec_amp_update - update the AMP mono value
1178 * @codec: HD-audio codec
1179 * @nid: NID to read the AMP value
1180 * @ch: channel to update (0 or 1)
1181 * @dir: #HDA_INPUT or #HDA_OUTPUT
1182 * @idx: the index value (only for input direction)
1183 * @mask: bit mask to set
1184 * @val: the bits value to set
1186 * Update the AMP values for the given channel, direction and index.
1188 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1189 int ch, int dir, int idx, int mask, int val)
1191 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1193 /* enable fake mute if no h/w mute but min=mute */
1194 if ((query_amp_caps(codec, nid, dir) &
1195 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1196 cmd |= AC_AMP_FAKE_MUTE;
1197 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1199 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1202 * snd_hda_codec_amp_stereo - update the AMP stereo values
1203 * @codec: HD-audio codec
1204 * @nid: NID to read the AMP value
1205 * @direction: #HDA_INPUT or #HDA_OUTPUT
1206 * @idx: the index value (only for input direction)
1207 * @mask: bit mask to set
1208 * @val: the bits value to set
1210 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1211 * stereo widget with the same mask and value.
1213 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1214 int direction, int idx, int mask, int val)
1216 int ch, ret = 0;
1218 if (snd_BUG_ON(mask & ~0xff))
1219 mask &= 0xff;
1220 for (ch = 0; ch < 2; ch++)
1221 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1222 idx, mask, val);
1223 return ret;
1225 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1228 * snd_hda_codec_amp_init - initialize the AMP value
1229 * @codec: the HDA codec
1230 * @nid: NID to read the AMP value
1231 * @ch: channel (left=0 or right=1)
1232 * @dir: #HDA_INPUT or #HDA_OUTPUT
1233 * @idx: the index value (only for input direction)
1234 * @mask: bit mask to set
1235 * @val: the bits value to set
1237 * Works like snd_hda_codec_amp_update() but it writes the value only at
1238 * the first access. If the amp was already initialized / updated beforehand,
1239 * this does nothing.
1241 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1242 int dir, int idx, int mask, int val)
1244 int orig;
1246 if (!codec->core.regmap)
1247 return -EINVAL;
1248 regcache_cache_only(codec->core.regmap, true);
1249 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1250 regcache_cache_only(codec->core.regmap, false);
1251 if (orig >= 0)
1252 return 0;
1253 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1255 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1258 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1259 * @codec: the HDA codec
1260 * @nid: NID to read the AMP value
1261 * @dir: #HDA_INPUT or #HDA_OUTPUT
1262 * @idx: the index value (only for input direction)
1263 * @mask: bit mask to set
1264 * @val: the bits value to set
1266 * Call snd_hda_codec_amp_init() for both stereo channels.
1268 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1269 int dir, int idx, int mask, int val)
1271 int ch, ret = 0;
1273 if (snd_BUG_ON(mask & ~0xff))
1274 mask &= 0xff;
1275 for (ch = 0; ch < 2; ch++)
1276 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1277 idx, mask, val);
1278 return ret;
1280 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1282 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1283 unsigned int ofs)
1285 u32 caps = query_amp_caps(codec, nid, dir);
1286 /* get num steps */
1287 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1288 if (ofs < caps)
1289 caps -= ofs;
1290 return caps;
1294 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1295 * @kcontrol: referred ctl element
1296 * @uinfo: pointer to get/store the data
1298 * The control element is supposed to have the private_value field
1299 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1301 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1302 struct snd_ctl_elem_info *uinfo)
1304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1305 u16 nid = get_amp_nid(kcontrol);
1306 u8 chs = get_amp_channels(kcontrol);
1307 int dir = get_amp_direction(kcontrol);
1308 unsigned int ofs = get_amp_offset(kcontrol);
1310 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1311 uinfo->count = chs == 3 ? 2 : 1;
1312 uinfo->value.integer.min = 0;
1313 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1314 if (!uinfo->value.integer.max) {
1315 codec_warn(codec,
1316 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1317 nid, kcontrol->id.name);
1318 return -EINVAL;
1320 return 0;
1322 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1325 static inline unsigned int
1326 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1327 int ch, int dir, int idx, unsigned int ofs)
1329 unsigned int val;
1330 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1331 val &= HDA_AMP_VOLMASK;
1332 if (val >= ofs)
1333 val -= ofs;
1334 else
1335 val = 0;
1336 return val;
1339 static inline int
1340 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1341 int ch, int dir, int idx, unsigned int ofs,
1342 unsigned int val)
1344 unsigned int maxval;
1346 if (val > 0)
1347 val += ofs;
1348 /* ofs = 0: raw max value */
1349 maxval = get_amp_max_value(codec, nid, dir, 0);
1350 if (val > maxval)
1351 val = maxval;
1352 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1353 HDA_AMP_VOLMASK, val);
1357 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1358 * @kcontrol: ctl element
1359 * @ucontrol: pointer to get/store the data
1361 * The control element is supposed to have the private_value field
1362 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1364 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1365 struct snd_ctl_elem_value *ucontrol)
1367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1368 hda_nid_t nid = get_amp_nid(kcontrol);
1369 int chs = get_amp_channels(kcontrol);
1370 int dir = get_amp_direction(kcontrol);
1371 int idx = get_amp_index(kcontrol);
1372 unsigned int ofs = get_amp_offset(kcontrol);
1373 long *valp = ucontrol->value.integer.value;
1375 if (chs & 1)
1376 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1377 if (chs & 2)
1378 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1379 return 0;
1381 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1384 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1385 * @kcontrol: ctl element
1386 * @ucontrol: pointer to get/store the data
1388 * The control element is supposed to have the private_value field
1389 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1391 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1392 struct snd_ctl_elem_value *ucontrol)
1394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1395 hda_nid_t nid = get_amp_nid(kcontrol);
1396 int chs = get_amp_channels(kcontrol);
1397 int dir = get_amp_direction(kcontrol);
1398 int idx = get_amp_index(kcontrol);
1399 unsigned int ofs = get_amp_offset(kcontrol);
1400 long *valp = ucontrol->value.integer.value;
1401 int change = 0;
1403 if (chs & 1) {
1404 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1405 valp++;
1407 if (chs & 2)
1408 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1409 return change;
1411 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1414 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1415 * @kcontrol: ctl element
1416 * @op_flag: operation flag
1417 * @size: byte size of input TLV
1418 * @_tlv: TLV data
1420 * The control element is supposed to have the private_value field
1421 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1423 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1424 unsigned int size, unsigned int __user *_tlv)
1426 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1427 hda_nid_t nid = get_amp_nid(kcontrol);
1428 int dir = get_amp_direction(kcontrol);
1429 unsigned int ofs = get_amp_offset(kcontrol);
1430 bool min_mute = get_amp_min_mute(kcontrol);
1431 u32 caps, val1, val2;
1433 if (size < 4 * sizeof(unsigned int))
1434 return -ENOMEM;
1435 caps = query_amp_caps(codec, nid, dir);
1436 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1437 val2 = (val2 + 1) * 25;
1438 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1439 val1 += ofs;
1440 val1 = ((int)val1) * ((int)val2);
1441 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1442 val2 |= TLV_DB_SCALE_MUTE;
1443 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1444 return -EFAULT;
1445 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1446 return -EFAULT;
1447 if (put_user(val1, _tlv + 2))
1448 return -EFAULT;
1449 if (put_user(val2, _tlv + 3))
1450 return -EFAULT;
1451 return 0;
1453 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1456 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1457 * @codec: HD-audio codec
1458 * @nid: NID of a reference widget
1459 * @dir: #HDA_INPUT or #HDA_OUTPUT
1460 * @tlv: TLV data to be stored, at least 4 elements
1462 * Set (static) TLV data for a virtual master volume using the AMP caps
1463 * obtained from the reference NID.
1464 * The volume range is recalculated as if the max volume is 0dB.
1466 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1467 unsigned int *tlv)
1469 u32 caps;
1470 int nums, step;
1472 caps = query_amp_caps(codec, nid, dir);
1473 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1474 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1475 step = (step + 1) * 25;
1476 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1477 tlv[1] = 2 * sizeof(unsigned int);
1478 tlv[2] = -nums * step;
1479 tlv[3] = step;
1481 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1483 /* find a mixer control element with the given name */
1484 static struct snd_kcontrol *
1485 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1487 struct snd_ctl_elem_id id;
1488 memset(&id, 0, sizeof(id));
1489 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1490 id.device = dev;
1491 id.index = idx;
1492 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1493 return NULL;
1494 strcpy(id.name, name);
1495 return snd_ctl_find_id(codec->card, &id);
1499 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1500 * @codec: HD-audio codec
1501 * @name: ctl id name string
1503 * Get the control element with the given id string and IFACE_MIXER.
1505 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1506 const char *name)
1508 return find_mixer_ctl(codec, name, 0, 0);
1510 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1512 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1513 int start_idx)
1515 int i, idx;
1516 /* 16 ctlrs should be large enough */
1517 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1518 if (!find_mixer_ctl(codec, name, 0, idx))
1519 return idx;
1521 return -EBUSY;
1525 * snd_hda_ctl_add - Add a control element and assign to the codec
1526 * @codec: HD-audio codec
1527 * @nid: corresponding NID (optional)
1528 * @kctl: the control element to assign
1530 * Add the given control element to an array inside the codec instance.
1531 * All control elements belonging to a codec are supposed to be added
1532 * by this function so that a proper clean-up works at the free or
1533 * reconfiguration time.
1535 * If non-zero @nid is passed, the NID is assigned to the control element.
1536 * The assignment is shown in the codec proc file.
1538 * snd_hda_ctl_add() checks the control subdev id field whether
1539 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1540 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1541 * specifies if kctl->private_value is a HDA amplifier value.
1543 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1544 struct snd_kcontrol *kctl)
1546 int err;
1547 unsigned short flags = 0;
1548 struct hda_nid_item *item;
1550 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1551 flags |= HDA_NID_ITEM_AMP;
1552 if (nid == 0)
1553 nid = get_amp_nid_(kctl->private_value);
1555 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1556 nid = kctl->id.subdevice & 0xffff;
1557 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1558 kctl->id.subdevice = 0;
1559 err = snd_ctl_add(codec->card, kctl);
1560 if (err < 0)
1561 return err;
1562 item = snd_array_new(&codec->mixers);
1563 if (!item)
1564 return -ENOMEM;
1565 item->kctl = kctl;
1566 item->nid = nid;
1567 item->flags = flags;
1568 return 0;
1570 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1573 * snd_hda_add_nid - Assign a NID to a control element
1574 * @codec: HD-audio codec
1575 * @nid: corresponding NID (optional)
1576 * @kctl: the control element to assign
1577 * @index: index to kctl
1579 * Add the given control element to an array inside the codec instance.
1580 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1581 * NID:KCTL mapping - for example "Capture Source" selector.
1583 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1584 unsigned int index, hda_nid_t nid)
1586 struct hda_nid_item *item;
1588 if (nid > 0) {
1589 item = snd_array_new(&codec->nids);
1590 if (!item)
1591 return -ENOMEM;
1592 item->kctl = kctl;
1593 item->index = index;
1594 item->nid = nid;
1595 return 0;
1597 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1598 kctl->id.name, kctl->id.index, index);
1599 return -EINVAL;
1601 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1604 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1605 * @codec: HD-audio codec
1607 void snd_hda_ctls_clear(struct hda_codec *codec)
1609 int i;
1610 struct hda_nid_item *items = codec->mixers.list;
1611 for (i = 0; i < codec->mixers.used; i++)
1612 snd_ctl_remove(codec->card, items[i].kctl);
1613 snd_array_free(&codec->mixers);
1614 snd_array_free(&codec->nids);
1618 * snd_hda_lock_devices - pseudo device locking
1619 * @bus: the BUS
1621 * toggle card->shutdown to allow/disallow the device access (as a hack)
1623 int snd_hda_lock_devices(struct hda_bus *bus)
1625 struct snd_card *card = bus->card;
1626 struct hda_codec *codec;
1628 spin_lock(&card->files_lock);
1629 if (card->shutdown)
1630 goto err_unlock;
1631 card->shutdown = 1;
1632 if (!list_empty(&card->ctl_files))
1633 goto err_clear;
1635 list_for_each_codec(codec, bus) {
1636 struct hda_pcm *cpcm;
1637 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1638 if (!cpcm->pcm)
1639 continue;
1640 if (cpcm->pcm->streams[0].substream_opened ||
1641 cpcm->pcm->streams[1].substream_opened)
1642 goto err_clear;
1645 spin_unlock(&card->files_lock);
1646 return 0;
1648 err_clear:
1649 card->shutdown = 0;
1650 err_unlock:
1651 spin_unlock(&card->files_lock);
1652 return -EINVAL;
1654 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1657 * snd_hda_unlock_devices - pseudo device unlocking
1658 * @bus: the BUS
1660 void snd_hda_unlock_devices(struct hda_bus *bus)
1662 struct snd_card *card = bus->card;
1664 spin_lock(&card->files_lock);
1665 card->shutdown = 0;
1666 spin_unlock(&card->files_lock);
1668 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1671 * snd_hda_codec_reset - Clear all objects assigned to the codec
1672 * @codec: HD-audio codec
1674 * This frees the all PCM and control elements assigned to the codec, and
1675 * clears the caches and restores the pin default configurations.
1677 * When a device is being used, it returns -EBSY. If successfully freed,
1678 * returns zero.
1680 int snd_hda_codec_reset(struct hda_codec *codec)
1682 struct hda_bus *bus = codec->bus;
1684 if (snd_hda_lock_devices(bus) < 0)
1685 return -EBUSY;
1687 /* OK, let it free */
1688 snd_hdac_device_unregister(&codec->core);
1690 /* allow device access again */
1691 snd_hda_unlock_devices(bus);
1692 return 0;
1695 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1697 /* apply the function to all matching slave ctls in the mixer list */
1698 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1699 const char *suffix, map_slave_func_t func, void *data)
1701 struct hda_nid_item *items;
1702 const char * const *s;
1703 int i, err;
1705 items = codec->mixers.list;
1706 for (i = 0; i < codec->mixers.used; i++) {
1707 struct snd_kcontrol *sctl = items[i].kctl;
1708 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1709 continue;
1710 for (s = slaves; *s; s++) {
1711 char tmpname[sizeof(sctl->id.name)];
1712 const char *name = *s;
1713 if (suffix) {
1714 snprintf(tmpname, sizeof(tmpname), "%s %s",
1715 name, suffix);
1716 name = tmpname;
1718 if (!strcmp(sctl->id.name, name)) {
1719 err = func(codec, data, sctl);
1720 if (err)
1721 return err;
1722 break;
1726 return 0;
1729 static int check_slave_present(struct hda_codec *codec,
1730 void *data, struct snd_kcontrol *sctl)
1732 return 1;
1735 /* guess the value corresponding to 0dB */
1736 static int get_kctl_0dB_offset(struct hda_codec *codec,
1737 struct snd_kcontrol *kctl, int *step_to_check)
1739 int _tlv[4];
1740 const int *tlv = NULL;
1741 int val = -1;
1743 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1744 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1745 mm_segment_t fs = get_fs();
1746 set_fs(get_ds());
1747 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1748 tlv = _tlv;
1749 set_fs(fs);
1750 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1751 tlv = kctl->tlv.p;
1752 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1753 int step = tlv[3];
1754 step &= ~TLV_DB_SCALE_MUTE;
1755 if (!step)
1756 return -1;
1757 if (*step_to_check && *step_to_check != step) {
1758 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
1759 *step_to_check, step);
1760 return -1;
1762 *step_to_check = step;
1763 val = -tlv[2] / step;
1765 return val;
1768 /* call kctl->put with the given value(s) */
1769 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1771 struct snd_ctl_elem_value *ucontrol;
1772 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1773 if (!ucontrol)
1774 return -ENOMEM;
1775 ucontrol->value.integer.value[0] = val;
1776 ucontrol->value.integer.value[1] = val;
1777 kctl->put(kctl, ucontrol);
1778 kfree(ucontrol);
1779 return 0;
1782 /* initialize the slave volume with 0dB */
1783 static int init_slave_0dB(struct hda_codec *codec,
1784 void *data, struct snd_kcontrol *slave)
1786 int offset = get_kctl_0dB_offset(codec, slave, data);
1787 if (offset > 0)
1788 put_kctl_with_value(slave, offset);
1789 return 0;
1792 /* unmute the slave */
1793 static int init_slave_unmute(struct hda_codec *codec,
1794 void *data, struct snd_kcontrol *slave)
1796 return put_kctl_with_value(slave, 1);
1799 static int add_slave(struct hda_codec *codec,
1800 void *data, struct snd_kcontrol *slave)
1802 return snd_ctl_add_slave(data, slave);
1806 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1807 * @codec: HD-audio codec
1808 * @name: vmaster control name
1809 * @tlv: TLV data (optional)
1810 * @slaves: slave control names (optional)
1811 * @suffix: suffix string to each slave name (optional)
1812 * @init_slave_vol: initialize slaves to unmute/0dB
1813 * @ctl_ret: store the vmaster kcontrol in return
1815 * Create a virtual master control with the given name. The TLV data
1816 * must be either NULL or a valid data.
1818 * @slaves is a NULL-terminated array of strings, each of which is a
1819 * slave control name. All controls with these names are assigned to
1820 * the new virtual master control.
1822 * This function returns zero if successful or a negative error code.
1824 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1825 unsigned int *tlv, const char * const *slaves,
1826 const char *suffix, bool init_slave_vol,
1827 struct snd_kcontrol **ctl_ret)
1829 struct snd_kcontrol *kctl;
1830 int err;
1832 if (ctl_ret)
1833 *ctl_ret = NULL;
1835 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1836 if (err != 1) {
1837 codec_dbg(codec, "No slave found for %s\n", name);
1838 return 0;
1840 kctl = snd_ctl_make_virtual_master(name, tlv);
1841 if (!kctl)
1842 return -ENOMEM;
1843 err = snd_hda_ctl_add(codec, 0, kctl);
1844 if (err < 0)
1845 return err;
1847 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1848 if (err < 0)
1849 return err;
1851 /* init with master mute & zero volume */
1852 put_kctl_with_value(kctl, 0);
1853 if (init_slave_vol) {
1854 int step = 0;
1855 map_slaves(codec, slaves, suffix,
1856 tlv ? init_slave_0dB : init_slave_unmute, &step);
1859 if (ctl_ret)
1860 *ctl_ret = kctl;
1861 return 0;
1863 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1866 * mute-LED control using vmaster
1868 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1869 struct snd_ctl_elem_info *uinfo)
1871 static const char * const texts[] = {
1872 "On", "Off", "Follow Master"
1875 return snd_ctl_enum_info(uinfo, 1, 3, texts);
1878 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1879 struct snd_ctl_elem_value *ucontrol)
1881 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1882 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1883 return 0;
1886 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1887 struct snd_ctl_elem_value *ucontrol)
1889 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1890 unsigned int old_mode = hook->mute_mode;
1892 hook->mute_mode = ucontrol->value.enumerated.item[0];
1893 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1894 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1895 if (old_mode == hook->mute_mode)
1896 return 0;
1897 snd_hda_sync_vmaster_hook(hook);
1898 return 1;
1901 static struct snd_kcontrol_new vmaster_mute_mode = {
1902 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1903 .name = "Mute-LED Mode",
1904 .info = vmaster_mute_mode_info,
1905 .get = vmaster_mute_mode_get,
1906 .put = vmaster_mute_mode_put,
1909 /* meta hook to call each driver's vmaster hook */
1910 static void vmaster_hook(void *private_data, int enabled)
1912 struct hda_vmaster_mute_hook *hook = private_data;
1914 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1915 enabled = hook->mute_mode;
1916 hook->hook(hook->codec, enabled);
1920 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1921 * @codec: the HDA codec
1922 * @hook: the vmaster hook object
1923 * @expose_enum_ctl: flag to create an enum ctl
1925 * Add a mute-LED hook with the given vmaster switch kctl.
1926 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1927 * created and associated with the given hook.
1929 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
1930 struct hda_vmaster_mute_hook *hook,
1931 bool expose_enum_ctl)
1933 struct snd_kcontrol *kctl;
1935 if (!hook->hook || !hook->sw_kctl)
1936 return 0;
1937 hook->codec = codec;
1938 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1939 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
1940 if (!expose_enum_ctl)
1941 return 0;
1942 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
1943 if (!kctl)
1944 return -ENOMEM;
1945 return snd_hda_ctl_add(codec, 0, kctl);
1947 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
1950 * snd_hda_sync_vmaster_hook - Sync vmaster hook
1951 * @hook: the vmaster hook
1953 * Call the hook with the current value for synchronization.
1954 * Should be called in init callback.
1956 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
1958 if (!hook->hook || !hook->codec)
1959 return;
1960 /* don't call vmaster hook in the destructor since it might have
1961 * been already destroyed
1963 if (hook->codec->bus->shutdown)
1964 return;
1965 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
1967 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
1971 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
1972 * @kcontrol: referred ctl element
1973 * @uinfo: pointer to get/store the data
1975 * The control element is supposed to have the private_value field
1976 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1978 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1979 struct snd_ctl_elem_info *uinfo)
1981 int chs = get_amp_channels(kcontrol);
1983 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1984 uinfo->count = chs == 3 ? 2 : 1;
1985 uinfo->value.integer.min = 0;
1986 uinfo->value.integer.max = 1;
1987 return 0;
1989 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
1992 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
1993 * @kcontrol: ctl element
1994 * @ucontrol: pointer to get/store the data
1996 * The control element is supposed to have the private_value field
1997 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1999 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2000 struct snd_ctl_elem_value *ucontrol)
2002 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2003 hda_nid_t nid = get_amp_nid(kcontrol);
2004 int chs = get_amp_channels(kcontrol);
2005 int dir = get_amp_direction(kcontrol);
2006 int idx = get_amp_index(kcontrol);
2007 long *valp = ucontrol->value.integer.value;
2009 if (chs & 1)
2010 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2011 HDA_AMP_MUTE) ? 0 : 1;
2012 if (chs & 2)
2013 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2014 HDA_AMP_MUTE) ? 0 : 1;
2015 return 0;
2017 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2020 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2021 * @kcontrol: ctl element
2022 * @ucontrol: pointer to get/store the data
2024 * The control element is supposed to have the private_value field
2025 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2027 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2028 struct snd_ctl_elem_value *ucontrol)
2030 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2031 hda_nid_t nid = get_amp_nid(kcontrol);
2032 int chs = get_amp_channels(kcontrol);
2033 int dir = get_amp_direction(kcontrol);
2034 int idx = get_amp_index(kcontrol);
2035 long *valp = ucontrol->value.integer.value;
2036 int change = 0;
2038 if (chs & 1) {
2039 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2040 HDA_AMP_MUTE,
2041 *valp ? 0 : HDA_AMP_MUTE);
2042 valp++;
2044 if (chs & 2)
2045 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2046 HDA_AMP_MUTE,
2047 *valp ? 0 : HDA_AMP_MUTE);
2048 hda_call_check_power_status(codec, nid);
2049 return change;
2051 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2054 * bound volume controls
2056 * bind multiple volumes (# indices, from 0)
2059 #define AMP_VAL_IDX_SHIFT 19
2060 #define AMP_VAL_IDX_MASK (0x0f<<19)
2063 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2064 * @kcontrol: ctl element
2065 * @ucontrol: pointer to get/store the data
2067 * The control element is supposed to have the private_value field
2068 * set up via HDA_BIND_MUTE*() macros.
2070 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 unsigned long pval;
2075 int err;
2077 mutex_lock(&codec->control_mutex);
2078 pval = kcontrol->private_value;
2079 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2080 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2081 kcontrol->private_value = pval;
2082 mutex_unlock(&codec->control_mutex);
2083 return err;
2085 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2088 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2089 * @kcontrol: ctl element
2090 * @ucontrol: pointer to get/store the data
2092 * The control element is supposed to have the private_value field
2093 * set up via HDA_BIND_MUTE*() macros.
2095 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_value *ucontrol)
2098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2099 unsigned long pval;
2100 int i, indices, err = 0, change = 0;
2102 mutex_lock(&codec->control_mutex);
2103 pval = kcontrol->private_value;
2104 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2105 for (i = 0; i < indices; i++) {
2106 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2107 (i << AMP_VAL_IDX_SHIFT);
2108 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2109 if (err < 0)
2110 break;
2111 change |= err;
2113 kcontrol->private_value = pval;
2114 mutex_unlock(&codec->control_mutex);
2115 return err < 0 ? err : change;
2117 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2120 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2121 * @kcontrol: referred ctl element
2122 * @uinfo: pointer to get/store the data
2124 * The control element is supposed to have the private_value field
2125 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2127 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2128 struct snd_ctl_elem_info *uinfo)
2130 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2131 struct hda_bind_ctls *c;
2132 int err;
2134 mutex_lock(&codec->control_mutex);
2135 c = (struct hda_bind_ctls *)kcontrol->private_value;
2136 kcontrol->private_value = *c->values;
2137 err = c->ops->info(kcontrol, uinfo);
2138 kcontrol->private_value = (long)c;
2139 mutex_unlock(&codec->control_mutex);
2140 return err;
2142 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2145 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2146 * @kcontrol: ctl element
2147 * @ucontrol: pointer to get/store the data
2149 * The control element is supposed to have the private_value field
2150 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2152 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2153 struct snd_ctl_elem_value *ucontrol)
2155 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2156 struct hda_bind_ctls *c;
2157 int err;
2159 mutex_lock(&codec->control_mutex);
2160 c = (struct hda_bind_ctls *)kcontrol->private_value;
2161 kcontrol->private_value = *c->values;
2162 err = c->ops->get(kcontrol, ucontrol);
2163 kcontrol->private_value = (long)c;
2164 mutex_unlock(&codec->control_mutex);
2165 return err;
2167 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2170 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2171 * @kcontrol: ctl element
2172 * @ucontrol: pointer to get/store the data
2174 * The control element is supposed to have the private_value field
2175 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2177 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2178 struct snd_ctl_elem_value *ucontrol)
2180 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2181 struct hda_bind_ctls *c;
2182 unsigned long *vals;
2183 int err = 0, change = 0;
2185 mutex_lock(&codec->control_mutex);
2186 c = (struct hda_bind_ctls *)kcontrol->private_value;
2187 for (vals = c->values; *vals; vals++) {
2188 kcontrol->private_value = *vals;
2189 err = c->ops->put(kcontrol, ucontrol);
2190 if (err < 0)
2191 break;
2192 change |= err;
2194 kcontrol->private_value = (long)c;
2195 mutex_unlock(&codec->control_mutex);
2196 return err < 0 ? err : change;
2198 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
2201 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2202 * @kcontrol: ctl element
2203 * @op_flag: operation flag
2204 * @size: byte size of input TLV
2205 * @tlv: TLV data
2207 * The control element is supposed to have the private_value field
2208 * set up via HDA_BIND_VOL() macro.
2210 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2211 unsigned int size, unsigned int __user *tlv)
2213 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2214 struct hda_bind_ctls *c;
2215 int err;
2217 mutex_lock(&codec->control_mutex);
2218 c = (struct hda_bind_ctls *)kcontrol->private_value;
2219 kcontrol->private_value = *c->values;
2220 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2221 kcontrol->private_value = (long)c;
2222 mutex_unlock(&codec->control_mutex);
2223 return err;
2225 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
2227 struct hda_ctl_ops snd_hda_bind_vol = {
2228 .info = snd_hda_mixer_amp_volume_info,
2229 .get = snd_hda_mixer_amp_volume_get,
2230 .put = snd_hda_mixer_amp_volume_put,
2231 .tlv = snd_hda_mixer_amp_tlv
2233 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
2235 struct hda_ctl_ops snd_hda_bind_sw = {
2236 .info = snd_hda_mixer_amp_switch_info,
2237 .get = snd_hda_mixer_amp_switch_get,
2238 .put = snd_hda_mixer_amp_switch_put,
2239 .tlv = snd_hda_mixer_amp_tlv
2241 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
2244 * SPDIF out controls
2247 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_info *uinfo)
2250 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2251 uinfo->count = 1;
2252 return 0;
2255 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2256 struct snd_ctl_elem_value *ucontrol)
2258 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2259 IEC958_AES0_NONAUDIO |
2260 IEC958_AES0_CON_EMPHASIS_5015 |
2261 IEC958_AES0_CON_NOT_COPYRIGHT;
2262 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2263 IEC958_AES1_CON_ORIGINAL;
2264 return 0;
2267 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2268 struct snd_ctl_elem_value *ucontrol)
2270 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2271 IEC958_AES0_NONAUDIO |
2272 IEC958_AES0_PRO_EMPHASIS_5015;
2273 return 0;
2276 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2277 struct snd_ctl_elem_value *ucontrol)
2279 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2280 int idx = kcontrol->private_value;
2281 struct hda_spdif_out *spdif;
2283 mutex_lock(&codec->spdif_mutex);
2284 spdif = snd_array_elem(&codec->spdif_out, idx);
2285 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2286 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2287 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2288 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2289 mutex_unlock(&codec->spdif_mutex);
2291 return 0;
2294 /* convert from SPDIF status bits to HDA SPDIF bits
2295 * bit 0 (DigEn) is always set zero (to be filled later)
2297 static unsigned short convert_from_spdif_status(unsigned int sbits)
2299 unsigned short val = 0;
2301 if (sbits & IEC958_AES0_PROFESSIONAL)
2302 val |= AC_DIG1_PROFESSIONAL;
2303 if (sbits & IEC958_AES0_NONAUDIO)
2304 val |= AC_DIG1_NONAUDIO;
2305 if (sbits & IEC958_AES0_PROFESSIONAL) {
2306 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2307 IEC958_AES0_PRO_EMPHASIS_5015)
2308 val |= AC_DIG1_EMPHASIS;
2309 } else {
2310 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2311 IEC958_AES0_CON_EMPHASIS_5015)
2312 val |= AC_DIG1_EMPHASIS;
2313 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2314 val |= AC_DIG1_COPYRIGHT;
2315 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2316 val |= AC_DIG1_LEVEL;
2317 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2319 return val;
2322 /* convert to SPDIF status bits from HDA SPDIF bits
2324 static unsigned int convert_to_spdif_status(unsigned short val)
2326 unsigned int sbits = 0;
2328 if (val & AC_DIG1_NONAUDIO)
2329 sbits |= IEC958_AES0_NONAUDIO;
2330 if (val & AC_DIG1_PROFESSIONAL)
2331 sbits |= IEC958_AES0_PROFESSIONAL;
2332 if (sbits & IEC958_AES0_PROFESSIONAL) {
2333 if (val & AC_DIG1_EMPHASIS)
2334 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2335 } else {
2336 if (val & AC_DIG1_EMPHASIS)
2337 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2338 if (!(val & AC_DIG1_COPYRIGHT))
2339 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2340 if (val & AC_DIG1_LEVEL)
2341 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2342 sbits |= val & (0x7f << 8);
2344 return sbits;
2347 /* set digital convert verbs both for the given NID and its slaves */
2348 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2349 int mask, int val)
2351 const hda_nid_t *d;
2353 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2354 mask, val);
2355 d = codec->slave_dig_outs;
2356 if (!d)
2357 return;
2358 for (; *d; d++)
2359 snd_hdac_regmap_update(&codec->core, *d,
2360 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2363 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2364 int dig1, int dig2)
2366 unsigned int mask = 0;
2367 unsigned int val = 0;
2369 if (dig1 != -1) {
2370 mask |= 0xff;
2371 val = dig1;
2373 if (dig2 != -1) {
2374 mask |= 0xff00;
2375 val |= dig2 << 8;
2377 set_dig_out(codec, nid, mask, val);
2380 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
2383 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2384 int idx = kcontrol->private_value;
2385 struct hda_spdif_out *spdif;
2386 hda_nid_t nid;
2387 unsigned short val;
2388 int change;
2390 mutex_lock(&codec->spdif_mutex);
2391 spdif = snd_array_elem(&codec->spdif_out, idx);
2392 nid = spdif->nid;
2393 spdif->status = ucontrol->value.iec958.status[0] |
2394 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2395 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2396 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2397 val = convert_from_spdif_status(spdif->status);
2398 val |= spdif->ctls & 1;
2399 change = spdif->ctls != val;
2400 spdif->ctls = val;
2401 if (change && nid != (u16)-1)
2402 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2403 mutex_unlock(&codec->spdif_mutex);
2404 return change;
2407 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2409 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2410 struct snd_ctl_elem_value *ucontrol)
2412 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2413 int idx = kcontrol->private_value;
2414 struct hda_spdif_out *spdif;
2416 mutex_lock(&codec->spdif_mutex);
2417 spdif = snd_array_elem(&codec->spdif_out, idx);
2418 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2419 mutex_unlock(&codec->spdif_mutex);
2420 return 0;
2423 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2424 int dig1, int dig2)
2426 set_dig_out_convert(codec, nid, dig1, dig2);
2427 /* unmute amp switch (if any) */
2428 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2429 (dig1 & AC_DIG1_ENABLE))
2430 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2431 HDA_AMP_MUTE, 0);
2434 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_value *ucontrol)
2437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2438 int idx = kcontrol->private_value;
2439 struct hda_spdif_out *spdif;
2440 hda_nid_t nid;
2441 unsigned short val;
2442 int change;
2444 mutex_lock(&codec->spdif_mutex);
2445 spdif = snd_array_elem(&codec->spdif_out, idx);
2446 nid = spdif->nid;
2447 val = spdif->ctls & ~AC_DIG1_ENABLE;
2448 if (ucontrol->value.integer.value[0])
2449 val |= AC_DIG1_ENABLE;
2450 change = spdif->ctls != val;
2451 spdif->ctls = val;
2452 if (change && nid != (u16)-1)
2453 set_spdif_ctls(codec, nid, val & 0xff, -1);
2454 mutex_unlock(&codec->spdif_mutex);
2455 return change;
2458 static struct snd_kcontrol_new dig_mixes[] = {
2460 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2461 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2462 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2463 .info = snd_hda_spdif_mask_info,
2464 .get = snd_hda_spdif_cmask_get,
2467 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2468 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2469 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2470 .info = snd_hda_spdif_mask_info,
2471 .get = snd_hda_spdif_pmask_get,
2474 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2475 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2476 .info = snd_hda_spdif_mask_info,
2477 .get = snd_hda_spdif_default_get,
2478 .put = snd_hda_spdif_default_put,
2481 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2482 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2483 .info = snd_hda_spdif_out_switch_info,
2484 .get = snd_hda_spdif_out_switch_get,
2485 .put = snd_hda_spdif_out_switch_put,
2487 { } /* end */
2491 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2492 * @codec: the HDA codec
2493 * @associated_nid: NID that new ctls associated with
2494 * @cvt_nid: converter NID
2495 * @type: HDA_PCM_TYPE_*
2496 * Creates controls related with the digital output.
2497 * Called from each patch supporting the digital out.
2499 * Returns 0 if successful, or a negative error code.
2501 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2502 hda_nid_t associated_nid,
2503 hda_nid_t cvt_nid,
2504 int type)
2506 int err;
2507 struct snd_kcontrol *kctl;
2508 struct snd_kcontrol_new *dig_mix;
2509 int idx = 0;
2510 int val = 0;
2511 const int spdif_index = 16;
2512 struct hda_spdif_out *spdif;
2513 struct hda_bus *bus = codec->bus;
2515 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2516 type == HDA_PCM_TYPE_SPDIF) {
2517 idx = spdif_index;
2518 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2519 type == HDA_PCM_TYPE_HDMI) {
2520 /* suppose a single SPDIF device */
2521 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2522 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2523 if (!kctl)
2524 break;
2525 kctl->id.index = spdif_index;
2527 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2529 if (!bus->primary_dig_out_type)
2530 bus->primary_dig_out_type = type;
2532 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2533 if (idx < 0) {
2534 codec_err(codec, "too many IEC958 outputs\n");
2535 return -EBUSY;
2537 spdif = snd_array_new(&codec->spdif_out);
2538 if (!spdif)
2539 return -ENOMEM;
2540 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2541 kctl = snd_ctl_new1(dig_mix, codec);
2542 if (!kctl)
2543 return -ENOMEM;
2544 kctl->id.index = idx;
2545 kctl->private_value = codec->spdif_out.used - 1;
2546 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2547 if (err < 0)
2548 return err;
2550 spdif->nid = cvt_nid;
2551 snd_hdac_regmap_read(&codec->core, cvt_nid,
2552 AC_VERB_GET_DIGI_CONVERT_1, &val);
2553 spdif->ctls = val;
2554 spdif->status = convert_to_spdif_status(spdif->ctls);
2555 return 0;
2557 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2560 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2561 * @codec: the HDA codec
2562 * @nid: widget NID
2564 * call within spdif_mutex lock
2566 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2567 hda_nid_t nid)
2569 int i;
2570 for (i = 0; i < codec->spdif_out.used; i++) {
2571 struct hda_spdif_out *spdif =
2572 snd_array_elem(&codec->spdif_out, i);
2573 if (spdif->nid == nid)
2574 return spdif;
2576 return NULL;
2578 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2581 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2582 * @codec: the HDA codec
2583 * @idx: the SPDIF ctl index
2585 * Unassign the widget from the given SPDIF control.
2587 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2589 struct hda_spdif_out *spdif;
2591 mutex_lock(&codec->spdif_mutex);
2592 spdif = snd_array_elem(&codec->spdif_out, idx);
2593 spdif->nid = (u16)-1;
2594 mutex_unlock(&codec->spdif_mutex);
2596 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2599 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2600 * @codec: the HDA codec
2601 * @idx: the SPDIF ctl idx
2602 * @nid: widget NID
2604 * Assign the widget to the SPDIF control with the given index.
2606 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2608 struct hda_spdif_out *spdif;
2609 unsigned short val;
2611 mutex_lock(&codec->spdif_mutex);
2612 spdif = snd_array_elem(&codec->spdif_out, idx);
2613 if (spdif->nid != nid) {
2614 spdif->nid = nid;
2615 val = spdif->ctls;
2616 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2618 mutex_unlock(&codec->spdif_mutex);
2620 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2623 * SPDIF sharing with analog output
2625 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2626 struct snd_ctl_elem_value *ucontrol)
2628 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2629 ucontrol->value.integer.value[0] = mout->share_spdif;
2630 return 0;
2633 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2634 struct snd_ctl_elem_value *ucontrol)
2636 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2637 mout->share_spdif = !!ucontrol->value.integer.value[0];
2638 return 0;
2641 static struct snd_kcontrol_new spdif_share_sw = {
2642 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2643 .name = "IEC958 Default PCM Playback Switch",
2644 .info = snd_ctl_boolean_mono_info,
2645 .get = spdif_share_sw_get,
2646 .put = spdif_share_sw_put,
2650 * snd_hda_create_spdif_share_sw - create Default PCM switch
2651 * @codec: the HDA codec
2652 * @mout: multi-out instance
2654 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2655 struct hda_multi_out *mout)
2657 struct snd_kcontrol *kctl;
2659 if (!mout->dig_out_nid)
2660 return 0;
2662 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2663 if (!kctl)
2664 return -ENOMEM;
2665 /* ATTENTION: here mout is passed as private_data, instead of codec */
2666 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2668 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2671 * SPDIF input
2674 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2676 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2677 struct snd_ctl_elem_value *ucontrol)
2679 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2681 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2682 return 0;
2685 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2686 struct snd_ctl_elem_value *ucontrol)
2688 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2689 hda_nid_t nid = kcontrol->private_value;
2690 unsigned int val = !!ucontrol->value.integer.value[0];
2691 int change;
2693 mutex_lock(&codec->spdif_mutex);
2694 change = codec->spdif_in_enable != val;
2695 if (change) {
2696 codec->spdif_in_enable = val;
2697 snd_hdac_regmap_write(&codec->core, nid,
2698 AC_VERB_SET_DIGI_CONVERT_1, val);
2700 mutex_unlock(&codec->spdif_mutex);
2701 return change;
2704 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2705 struct snd_ctl_elem_value *ucontrol)
2707 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2708 hda_nid_t nid = kcontrol->private_value;
2709 unsigned int val;
2710 unsigned int sbits;
2712 snd_hdac_regmap_read(&codec->core, nid,
2713 AC_VERB_GET_DIGI_CONVERT_1, &val);
2714 sbits = convert_to_spdif_status(val);
2715 ucontrol->value.iec958.status[0] = sbits;
2716 ucontrol->value.iec958.status[1] = sbits >> 8;
2717 ucontrol->value.iec958.status[2] = sbits >> 16;
2718 ucontrol->value.iec958.status[3] = sbits >> 24;
2719 return 0;
2722 static struct snd_kcontrol_new dig_in_ctls[] = {
2724 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2725 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2726 .info = snd_hda_spdif_in_switch_info,
2727 .get = snd_hda_spdif_in_switch_get,
2728 .put = snd_hda_spdif_in_switch_put,
2731 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2732 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2733 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2734 .info = snd_hda_spdif_mask_info,
2735 .get = snd_hda_spdif_in_status_get,
2737 { } /* end */
2741 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2742 * @codec: the HDA codec
2743 * @nid: audio in widget NID
2745 * Creates controls related with the SPDIF input.
2746 * Called from each patch supporting the SPDIF in.
2748 * Returns 0 if successful, or a negative error code.
2750 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2752 int err;
2753 struct snd_kcontrol *kctl;
2754 struct snd_kcontrol_new *dig_mix;
2755 int idx;
2757 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2758 if (idx < 0) {
2759 codec_err(codec, "too many IEC958 inputs\n");
2760 return -EBUSY;
2762 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2763 kctl = snd_ctl_new1(dig_mix, codec);
2764 if (!kctl)
2765 return -ENOMEM;
2766 kctl->private_value = nid;
2767 err = snd_hda_ctl_add(codec, nid, kctl);
2768 if (err < 0)
2769 return err;
2771 codec->spdif_in_enable =
2772 snd_hda_codec_read(codec, nid, 0,
2773 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2774 AC_DIG1_ENABLE;
2775 return 0;
2777 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2780 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2781 * @codec: the HDA codec
2782 * @fg: function group (not used now)
2783 * @power_state: the power state to set (AC_PWRST_*)
2785 * Set the given power state to all widgets that have the power control.
2786 * If the codec has power_filter set, it evaluates the power state and
2787 * filter out if it's unchanged as D3.
2789 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2790 unsigned int power_state)
2792 hda_nid_t nid;
2794 for_each_hda_codec_node(nid, codec) {
2795 unsigned int wcaps = get_wcaps(codec, nid);
2796 unsigned int state = power_state;
2797 if (!(wcaps & AC_WCAP_POWER))
2798 continue;
2799 if (codec->power_filter) {
2800 state = codec->power_filter(codec, nid, power_state);
2801 if (state != power_state && power_state == AC_PWRST_D3)
2802 continue;
2804 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2805 state);
2808 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2811 * wait until the state is reached, returns the current state
2813 static unsigned int hda_sync_power_state(struct hda_codec *codec,
2814 hda_nid_t fg,
2815 unsigned int power_state)
2817 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2818 unsigned int state, actual_state;
2820 for (;;) {
2821 state = snd_hda_codec_read(codec, fg, 0,
2822 AC_VERB_GET_POWER_STATE, 0);
2823 if (state & AC_PWRST_ERROR)
2824 break;
2825 actual_state = (state >> 4) & 0x0f;
2826 if (actual_state == power_state)
2827 break;
2828 if (time_after_eq(jiffies, end_time))
2829 break;
2830 /* wait until the codec reachs to the target state */
2831 msleep(1);
2833 return state;
2837 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2838 * @codec: the HDA codec
2839 * @nid: widget NID
2840 * @power_state: power state to evalue
2842 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2843 * This can be used a codec power_filter callback.
2845 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2846 hda_nid_t nid,
2847 unsigned int power_state)
2849 if (nid == codec->core.afg || nid == codec->core.mfg)
2850 return power_state;
2851 if (power_state == AC_PWRST_D3 &&
2852 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2853 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2854 int eapd = snd_hda_codec_read(codec, nid, 0,
2855 AC_VERB_GET_EAPD_BTLENABLE, 0);
2856 if (eapd & 0x02)
2857 return AC_PWRST_D0;
2859 return power_state;
2861 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2864 * set power state of the codec, and return the power state
2866 static unsigned int hda_set_power_state(struct hda_codec *codec,
2867 unsigned int power_state)
2869 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2870 int count;
2871 unsigned int state;
2872 int flags = 0;
2874 /* this delay seems necessary to avoid click noise at power-down */
2875 if (power_state == AC_PWRST_D3) {
2876 if (codec->depop_delay < 0)
2877 msleep(codec_has_epss(codec) ? 10 : 100);
2878 else if (codec->depop_delay > 0)
2879 msleep(codec->depop_delay);
2880 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2883 /* repeat power states setting at most 10 times*/
2884 for (count = 0; count < 10; count++) {
2885 if (codec->patch_ops.set_power_state)
2886 codec->patch_ops.set_power_state(codec, fg,
2887 power_state);
2888 else {
2889 state = power_state;
2890 if (codec->power_filter)
2891 state = codec->power_filter(codec, fg, state);
2892 if (state == power_state || power_state != AC_PWRST_D3)
2893 snd_hda_codec_read(codec, fg, flags,
2894 AC_VERB_SET_POWER_STATE,
2895 state);
2896 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2898 state = hda_sync_power_state(codec, fg, power_state);
2899 if (!(state & AC_PWRST_ERROR))
2900 break;
2903 return state;
2906 /* sync power states of all widgets;
2907 * this is called at the end of codec parsing
2909 static void sync_power_up_states(struct hda_codec *codec)
2911 hda_nid_t nid;
2913 /* don't care if no filter is used */
2914 if (!codec->power_filter)
2915 return;
2917 for_each_hda_codec_node(nid, codec) {
2918 unsigned int wcaps = get_wcaps(codec, nid);
2919 unsigned int target;
2920 if (!(wcaps & AC_WCAP_POWER))
2921 continue;
2922 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2923 if (target == AC_PWRST_D0)
2924 continue;
2925 if (!snd_hda_check_power_state(codec, nid, target))
2926 snd_hda_codec_write(codec, nid, 0,
2927 AC_VERB_SET_POWER_STATE, target);
2931 #ifdef CONFIG_SND_HDA_RECONFIG
2932 /* execute additional init verbs */
2933 static void hda_exec_init_verbs(struct hda_codec *codec)
2935 if (codec->init_verbs.list)
2936 snd_hda_sequence_write(codec, codec->init_verbs.list);
2938 #else
2939 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2940 #endif
2942 #ifdef CONFIG_PM
2943 /* update the power on/off account with the current jiffies */
2944 static void update_power_acct(struct hda_codec *codec, bool on)
2946 unsigned long delta = jiffies - codec->power_jiffies;
2948 if (on)
2949 codec->power_on_acct += delta;
2950 else
2951 codec->power_off_acct += delta;
2952 codec->power_jiffies += delta;
2955 void snd_hda_update_power_acct(struct hda_codec *codec)
2957 update_power_acct(codec, hda_codec_is_power_on(codec));
2961 * call suspend and power-down; used both from PM and power-save
2962 * this function returns the power state in the end
2964 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
2966 unsigned int state;
2968 atomic_inc(&codec->core.in_pm);
2970 if (codec->patch_ops.suspend)
2971 codec->patch_ops.suspend(codec);
2972 hda_cleanup_all_streams(codec);
2973 state = hda_set_power_state(codec, AC_PWRST_D3);
2974 update_power_acct(codec, true);
2975 atomic_dec(&codec->core.in_pm);
2976 return state;
2980 * kick up codec; used both from PM and power-save
2982 static void hda_call_codec_resume(struct hda_codec *codec)
2984 atomic_inc(&codec->core.in_pm);
2986 if (codec->core.regmap)
2987 regcache_mark_dirty(codec->core.regmap);
2989 codec->power_jiffies = jiffies;
2991 hda_set_power_state(codec, AC_PWRST_D0);
2992 restore_shutup_pins(codec);
2993 hda_exec_init_verbs(codec);
2994 snd_hda_jack_set_dirty_all(codec);
2995 if (codec->patch_ops.resume)
2996 codec->patch_ops.resume(codec);
2997 else {
2998 if (codec->patch_ops.init)
2999 codec->patch_ops.init(codec);
3000 if (codec->core.regmap)
3001 regcache_sync(codec->core.regmap);
3004 if (codec->jackpoll_interval)
3005 hda_jackpoll_work(&codec->jackpoll_work.work);
3006 else
3007 snd_hda_jack_report_sync(codec);
3008 codec->core.dev.power.power_state = PMSG_ON;
3009 atomic_dec(&codec->core.in_pm);
3012 static int hda_codec_runtime_suspend(struct device *dev)
3014 struct hda_codec *codec = dev_to_hda_codec(dev);
3015 struct hda_pcm *pcm;
3016 unsigned int state;
3018 cancel_delayed_work_sync(&codec->jackpoll_work);
3019 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3020 snd_pcm_suspend_all(pcm->pcm);
3021 state = hda_call_codec_suspend(codec);
3022 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3023 (state & AC_PWRST_CLK_STOP_OK))
3024 snd_hdac_codec_link_down(&codec->core);
3025 snd_hdac_link_power(&codec->core, false);
3026 return 0;
3029 static int hda_codec_runtime_resume(struct device *dev)
3031 struct hda_codec *codec = dev_to_hda_codec(dev);
3033 snd_hdac_link_power(&codec->core, true);
3034 snd_hdac_codec_link_up(&codec->core);
3035 hda_call_codec_resume(codec);
3036 pm_runtime_mark_last_busy(dev);
3037 return 0;
3039 #endif /* CONFIG_PM */
3041 #ifdef CONFIG_PM_SLEEP
3042 static int hda_codec_force_resume(struct device *dev)
3044 int ret;
3046 /* The get/put pair below enforces the runtime resume even if the
3047 * device hasn't been used at suspend time. This trick is needed to
3048 * update the jack state change during the sleep.
3050 pm_runtime_get_noresume(dev);
3051 ret = pm_runtime_force_resume(dev);
3052 pm_runtime_put(dev);
3053 return ret;
3056 static int hda_codec_pm_suspend(struct device *dev)
3058 dev->power.power_state = PMSG_SUSPEND;
3059 return pm_runtime_force_suspend(dev);
3062 static int hda_codec_pm_resume(struct device *dev)
3064 dev->power.power_state = PMSG_RESUME;
3065 return hda_codec_force_resume(dev);
3068 static int hda_codec_pm_freeze(struct device *dev)
3070 dev->power.power_state = PMSG_FREEZE;
3071 return pm_runtime_force_suspend(dev);
3074 static int hda_codec_pm_thaw(struct device *dev)
3076 dev->power.power_state = PMSG_THAW;
3077 return hda_codec_force_resume(dev);
3080 static int hda_codec_pm_restore(struct device *dev)
3082 dev->power.power_state = PMSG_RESTORE;
3083 return hda_codec_force_resume(dev);
3085 #endif /* CONFIG_PM_SLEEP */
3087 /* referred in hda_bind.c */
3088 const struct dev_pm_ops hda_codec_driver_pm = {
3089 #ifdef CONFIG_PM_SLEEP
3090 .suspend = hda_codec_pm_suspend,
3091 .resume = hda_codec_pm_resume,
3092 .freeze = hda_codec_pm_freeze,
3093 .thaw = hda_codec_pm_thaw,
3094 .poweroff = hda_codec_pm_suspend,
3095 .restore = hda_codec_pm_restore,
3096 #endif /* CONFIG_PM_SLEEP */
3097 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3098 NULL)
3102 * add standard channel maps if not specified
3104 static int add_std_chmaps(struct hda_codec *codec)
3106 struct hda_pcm *pcm;
3107 int str, err;
3109 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3110 for (str = 0; str < 2; str++) {
3111 struct hda_pcm_stream *hinfo = &pcm->stream[str];
3112 struct snd_pcm_chmap *chmap;
3113 const struct snd_pcm_chmap_elem *elem;
3115 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
3116 continue;
3117 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3118 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3119 hinfo->channels_max,
3120 0, &chmap);
3121 if (err < 0)
3122 return err;
3123 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3126 return 0;
3129 /* default channel maps for 2.1 speakers;
3130 * since HD-audio supports only stereo, odd number channels are omitted
3132 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3133 { .channels = 2,
3134 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3135 { .channels = 4,
3136 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3137 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3140 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3142 int snd_hda_codec_build_controls(struct hda_codec *codec)
3144 int err = 0;
3145 hda_exec_init_verbs(codec);
3146 /* continue to initialize... */
3147 if (codec->patch_ops.init)
3148 err = codec->patch_ops.init(codec);
3149 if (!err && codec->patch_ops.build_controls)
3150 err = codec->patch_ops.build_controls(codec);
3151 if (err < 0)
3152 return err;
3154 /* we create chmaps here instead of build_pcms */
3155 err = add_std_chmaps(codec);
3156 if (err < 0)
3157 return err;
3159 if (codec->jackpoll_interval)
3160 hda_jackpoll_work(&codec->jackpoll_work.work);
3161 else
3162 snd_hda_jack_report_sync(codec); /* call at the last init point */
3163 sync_power_up_states(codec);
3164 return 0;
3168 * PCM stuff
3170 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3171 struct hda_codec *codec,
3172 struct snd_pcm_substream *substream)
3174 return 0;
3177 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3178 struct hda_codec *codec,
3179 unsigned int stream_tag,
3180 unsigned int format,
3181 struct snd_pcm_substream *substream)
3183 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3184 return 0;
3187 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3188 struct hda_codec *codec,
3189 struct snd_pcm_substream *substream)
3191 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3192 return 0;
3195 static int set_pcm_default_values(struct hda_codec *codec,
3196 struct hda_pcm_stream *info)
3198 int err;
3200 /* query support PCM information from the given NID */
3201 if (info->nid && (!info->rates || !info->formats)) {
3202 err = snd_hda_query_supported_pcm(codec, info->nid,
3203 info->rates ? NULL : &info->rates,
3204 info->formats ? NULL : &info->formats,
3205 info->maxbps ? NULL : &info->maxbps);
3206 if (err < 0)
3207 return err;
3209 if (info->ops.open == NULL)
3210 info->ops.open = hda_pcm_default_open_close;
3211 if (info->ops.close == NULL)
3212 info->ops.close = hda_pcm_default_open_close;
3213 if (info->ops.prepare == NULL) {
3214 if (snd_BUG_ON(!info->nid))
3215 return -EINVAL;
3216 info->ops.prepare = hda_pcm_default_prepare;
3218 if (info->ops.cleanup == NULL) {
3219 if (snd_BUG_ON(!info->nid))
3220 return -EINVAL;
3221 info->ops.cleanup = hda_pcm_default_cleanup;
3223 return 0;
3227 * codec prepare/cleanup entries
3230 * snd_hda_codec_prepare - Prepare a stream
3231 * @codec: the HDA codec
3232 * @hinfo: PCM information
3233 * @stream: stream tag to assign
3234 * @format: format id to assign
3235 * @substream: PCM substream to assign
3237 * Calls the prepare callback set by the codec with the given arguments.
3238 * Clean up the inactive streams when successful.
3240 int snd_hda_codec_prepare(struct hda_codec *codec,
3241 struct hda_pcm_stream *hinfo,
3242 unsigned int stream,
3243 unsigned int format,
3244 struct snd_pcm_substream *substream)
3246 int ret;
3247 mutex_lock(&codec->bus->prepare_mutex);
3248 if (hinfo->ops.prepare)
3249 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3250 substream);
3251 else
3252 ret = -ENODEV;
3253 if (ret >= 0)
3254 purify_inactive_streams(codec);
3255 mutex_unlock(&codec->bus->prepare_mutex);
3256 return ret;
3258 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3261 * snd_hda_codec_cleanup - Prepare a stream
3262 * @codec: the HDA codec
3263 * @hinfo: PCM information
3264 * @substream: PCM substream
3266 * Calls the cleanup callback set by the codec with the given arguments.
3268 void snd_hda_codec_cleanup(struct hda_codec *codec,
3269 struct hda_pcm_stream *hinfo,
3270 struct snd_pcm_substream *substream)
3272 mutex_lock(&codec->bus->prepare_mutex);
3273 if (hinfo->ops.cleanup)
3274 hinfo->ops.cleanup(hinfo, codec, substream);
3275 mutex_unlock(&codec->bus->prepare_mutex);
3277 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3279 /* global */
3280 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3281 "Audio", "SPDIF", "HDMI", "Modem"
3285 * get the empty PCM device number to assign
3287 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3289 /* audio device indices; not linear to keep compatibility */
3290 /* assigned to static slots up to dev#10; if more needed, assign
3291 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3293 static int audio_idx[HDA_PCM_NTYPES][5] = {
3294 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3295 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3296 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3297 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3299 int i;
3301 if (type >= HDA_PCM_NTYPES) {
3302 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3303 return -EINVAL;
3306 for (i = 0; audio_idx[type][i] >= 0; i++) {
3307 #ifndef CONFIG_SND_DYNAMIC_MINORS
3308 if (audio_idx[type][i] >= 8)
3309 break;
3310 #endif
3311 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3312 return audio_idx[type][i];
3315 #ifdef CONFIG_SND_DYNAMIC_MINORS
3316 /* non-fixed slots starting from 10 */
3317 for (i = 10; i < 32; i++) {
3318 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3319 return i;
3321 #endif
3323 dev_warn(bus->card->dev, "Too many %s devices\n",
3324 snd_hda_pcm_type_name[type]);
3325 #ifndef CONFIG_SND_DYNAMIC_MINORS
3326 dev_warn(bus->card->dev,
3327 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3328 #endif
3329 return -EAGAIN;
3332 /* call build_pcms ops of the given codec and set up the default parameters */
3333 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3335 struct hda_pcm *cpcm;
3336 int err;
3338 if (!list_empty(&codec->pcm_list_head))
3339 return 0; /* already parsed */
3341 if (!codec->patch_ops.build_pcms)
3342 return 0;
3344 err = codec->patch_ops.build_pcms(codec);
3345 if (err < 0) {
3346 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3347 codec->core.addr, err);
3348 return err;
3351 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3352 int stream;
3354 for (stream = 0; stream < 2; stream++) {
3355 struct hda_pcm_stream *info = &cpcm->stream[stream];
3357 if (!info->substreams)
3358 continue;
3359 err = set_pcm_default_values(codec, info);
3360 if (err < 0) {
3361 codec_warn(codec,
3362 "fail to setup default for PCM %s\n",
3363 cpcm->name);
3364 return err;
3369 return 0;
3372 /* assign all PCMs of the given codec */
3373 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3375 struct hda_bus *bus = codec->bus;
3376 struct hda_pcm *cpcm;
3377 int dev, err;
3379 err = snd_hda_codec_parse_pcms(codec);
3380 if (err < 0)
3381 return err;
3383 /* attach a new PCM streams */
3384 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3385 if (cpcm->pcm)
3386 continue; /* already attached */
3387 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3388 continue; /* no substreams assigned */
3390 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3391 if (dev < 0)
3392 continue; /* no fatal error */
3393 cpcm->device = dev;
3394 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
3395 if (err < 0) {
3396 codec_err(codec,
3397 "cannot attach PCM stream %d for codec #%d\n",
3398 dev, codec->core.addr);
3399 continue; /* no fatal error */
3403 return 0;
3407 * snd_hda_add_new_ctls - create controls from the array
3408 * @codec: the HDA codec
3409 * @knew: the array of struct snd_kcontrol_new
3411 * This helper function creates and add new controls in the given array.
3412 * The array must be terminated with an empty entry as terminator.
3414 * Returns 0 if successful, or a negative error code.
3416 int snd_hda_add_new_ctls(struct hda_codec *codec,
3417 const struct snd_kcontrol_new *knew)
3419 int err;
3421 for (; knew->name; knew++) {
3422 struct snd_kcontrol *kctl;
3423 int addr = 0, idx = 0;
3424 if (knew->iface == -1) /* skip this codec private value */
3425 continue;
3426 for (;;) {
3427 kctl = snd_ctl_new1(knew, codec);
3428 if (!kctl)
3429 return -ENOMEM;
3430 if (addr > 0)
3431 kctl->id.device = addr;
3432 if (idx > 0)
3433 kctl->id.index = idx;
3434 err = snd_hda_ctl_add(codec, 0, kctl);
3435 if (!err)
3436 break;
3437 /* try first with another device index corresponding to
3438 * the codec addr; if it still fails (or it's the
3439 * primary codec), then try another control index
3441 if (!addr && codec->core.addr)
3442 addr = codec->core.addr;
3443 else if (!idx && !knew->index) {
3444 idx = find_empty_mixer_ctl_idx(codec,
3445 knew->name, 0);
3446 if (idx <= 0)
3447 return err;
3448 } else
3449 return err;
3452 return 0;
3454 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3456 #ifdef CONFIG_PM
3457 static void codec_set_power_save(struct hda_codec *codec, int delay)
3459 struct device *dev = hda_codec_dev(codec);
3461 if (delay == 0 && codec->auto_runtime_pm)
3462 delay = 3000;
3464 if (delay > 0) {
3465 pm_runtime_set_autosuspend_delay(dev, delay);
3466 pm_runtime_use_autosuspend(dev);
3467 pm_runtime_allow(dev);
3468 if (!pm_runtime_suspended(dev))
3469 pm_runtime_mark_last_busy(dev);
3470 } else {
3471 pm_runtime_dont_use_autosuspend(dev);
3472 pm_runtime_forbid(dev);
3477 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3478 * @bus: HD-audio bus
3479 * @delay: autosuspend delay in msec, 0 = off
3481 * Synchronize the runtime PM autosuspend state from the power_save option.
3483 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3485 struct hda_codec *c;
3487 list_for_each_codec(c, bus)
3488 codec_set_power_save(c, delay);
3490 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3493 * snd_hda_check_amp_list_power - Check the amp list and update the power
3494 * @codec: HD-audio codec
3495 * @check: the object containing an AMP list and the status
3496 * @nid: NID to check / update
3498 * Check whether the given NID is in the amp list. If it's in the list,
3499 * check the current AMP status, and update the power-status according
3500 * to the mute status.
3502 * This function is supposed to be set or called from the check_power_status
3503 * patch ops.
3505 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3506 struct hda_loopback_check *check,
3507 hda_nid_t nid)
3509 const struct hda_amp_list *p;
3510 int ch, v;
3512 if (!check->amplist)
3513 return 0;
3514 for (p = check->amplist; p->nid; p++) {
3515 if (p->nid == nid)
3516 break;
3518 if (!p->nid)
3519 return 0; /* nothing changed */
3521 for (p = check->amplist; p->nid; p++) {
3522 for (ch = 0; ch < 2; ch++) {
3523 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3524 p->idx);
3525 if (!(v & HDA_AMP_MUTE) && v > 0) {
3526 if (!check->power_on) {
3527 check->power_on = 1;
3528 snd_hda_power_up_pm(codec);
3530 return 1;
3534 if (check->power_on) {
3535 check->power_on = 0;
3536 snd_hda_power_down_pm(codec);
3538 return 0;
3540 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3541 #endif
3544 * input MUX helper
3548 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3549 * @imux: imux helper object
3550 * @uinfo: pointer to get/store the data
3552 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3553 struct snd_ctl_elem_info *uinfo)
3555 unsigned int index;
3557 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3558 uinfo->count = 1;
3559 uinfo->value.enumerated.items = imux->num_items;
3560 if (!imux->num_items)
3561 return 0;
3562 index = uinfo->value.enumerated.item;
3563 if (index >= imux->num_items)
3564 index = imux->num_items - 1;
3565 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3566 return 0;
3568 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3571 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3572 * @codec: the HDA codec
3573 * @imux: imux helper object
3574 * @ucontrol: pointer to get/store the data
3575 * @nid: input mux NID
3576 * @cur_val: pointer to get/store the current imux value
3578 int snd_hda_input_mux_put(struct hda_codec *codec,
3579 const struct hda_input_mux *imux,
3580 struct snd_ctl_elem_value *ucontrol,
3581 hda_nid_t nid,
3582 unsigned int *cur_val)
3584 unsigned int idx;
3586 if (!imux->num_items)
3587 return 0;
3588 idx = ucontrol->value.enumerated.item[0];
3589 if (idx >= imux->num_items)
3590 idx = imux->num_items - 1;
3591 if (*cur_val == idx)
3592 return 0;
3593 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3594 imux->items[idx].index);
3595 *cur_val = idx;
3596 return 1;
3598 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3602 * snd_hda_enum_helper_info - Helper for simple enum ctls
3603 * @kcontrol: ctl element
3604 * @uinfo: pointer to get/store the data
3605 * @num_items: number of enum items
3606 * @texts: enum item string array
3608 * process kcontrol info callback of a simple string enum array
3609 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3611 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3612 struct snd_ctl_elem_info *uinfo,
3613 int num_items, const char * const *texts)
3615 static const char * const texts_default[] = {
3616 "Disabled", "Enabled"
3619 if (!texts || !num_items) {
3620 num_items = 2;
3621 texts = texts_default;
3624 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3626 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3629 * Multi-channel / digital-out PCM helper functions
3632 /* setup SPDIF output stream */
3633 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3634 unsigned int stream_tag, unsigned int format)
3636 struct hda_spdif_out *spdif;
3637 unsigned int curr_fmt;
3638 bool reset;
3640 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3641 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3642 AC_VERB_GET_STREAM_FORMAT, 0);
3643 reset = codec->spdif_status_reset &&
3644 (spdif->ctls & AC_DIG1_ENABLE) &&
3645 curr_fmt != format;
3647 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3648 updated */
3649 if (reset)
3650 set_dig_out_convert(codec, nid,
3651 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3652 -1);
3653 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3654 if (codec->slave_dig_outs) {
3655 const hda_nid_t *d;
3656 for (d = codec->slave_dig_outs; *d; d++)
3657 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3658 format);
3660 /* turn on again (if needed) */
3661 if (reset)
3662 set_dig_out_convert(codec, nid,
3663 spdif->ctls & 0xff, -1);
3666 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3668 snd_hda_codec_cleanup_stream(codec, nid);
3669 if (codec->slave_dig_outs) {
3670 const hda_nid_t *d;
3671 for (d = codec->slave_dig_outs; *d; d++)
3672 snd_hda_codec_cleanup_stream(codec, *d);
3677 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3678 * @codec: the HDA codec
3679 * @mout: hda_multi_out object
3681 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3682 struct hda_multi_out *mout)
3684 mutex_lock(&codec->spdif_mutex);
3685 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3686 /* already opened as analog dup; reset it once */
3687 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3688 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3689 mutex_unlock(&codec->spdif_mutex);
3690 return 0;
3692 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3695 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3696 * @codec: the HDA codec
3697 * @mout: hda_multi_out object
3698 * @stream_tag: stream tag to assign
3699 * @format: format id to assign
3700 * @substream: PCM substream to assign
3702 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3703 struct hda_multi_out *mout,
3704 unsigned int stream_tag,
3705 unsigned int format,
3706 struct snd_pcm_substream *substream)
3708 mutex_lock(&codec->spdif_mutex);
3709 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3710 mutex_unlock(&codec->spdif_mutex);
3711 return 0;
3713 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3716 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3717 * @codec: the HDA codec
3718 * @mout: hda_multi_out object
3720 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3721 struct hda_multi_out *mout)
3723 mutex_lock(&codec->spdif_mutex);
3724 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3725 mutex_unlock(&codec->spdif_mutex);
3726 return 0;
3728 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3731 * snd_hda_multi_out_dig_close - release the digital out stream
3732 * @codec: the HDA codec
3733 * @mout: hda_multi_out object
3735 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3736 struct hda_multi_out *mout)
3738 mutex_lock(&codec->spdif_mutex);
3739 mout->dig_out_used = 0;
3740 mutex_unlock(&codec->spdif_mutex);
3741 return 0;
3743 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3746 * snd_hda_multi_out_analog_open - open analog outputs
3747 * @codec: the HDA codec
3748 * @mout: hda_multi_out object
3749 * @substream: PCM substream to assign
3750 * @hinfo: PCM information to assign
3752 * Open analog outputs and set up the hw-constraints.
3753 * If the digital outputs can be opened as slave, open the digital
3754 * outputs, too.
3756 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3757 struct hda_multi_out *mout,
3758 struct snd_pcm_substream *substream,
3759 struct hda_pcm_stream *hinfo)
3761 struct snd_pcm_runtime *runtime = substream->runtime;
3762 runtime->hw.channels_max = mout->max_channels;
3763 if (mout->dig_out_nid) {
3764 if (!mout->analog_rates) {
3765 mout->analog_rates = hinfo->rates;
3766 mout->analog_formats = hinfo->formats;
3767 mout->analog_maxbps = hinfo->maxbps;
3768 } else {
3769 runtime->hw.rates = mout->analog_rates;
3770 runtime->hw.formats = mout->analog_formats;
3771 hinfo->maxbps = mout->analog_maxbps;
3773 if (!mout->spdif_rates) {
3774 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3775 &mout->spdif_rates,
3776 &mout->spdif_formats,
3777 &mout->spdif_maxbps);
3779 mutex_lock(&codec->spdif_mutex);
3780 if (mout->share_spdif) {
3781 if ((runtime->hw.rates & mout->spdif_rates) &&
3782 (runtime->hw.formats & mout->spdif_formats)) {
3783 runtime->hw.rates &= mout->spdif_rates;
3784 runtime->hw.formats &= mout->spdif_formats;
3785 if (mout->spdif_maxbps < hinfo->maxbps)
3786 hinfo->maxbps = mout->spdif_maxbps;
3787 } else {
3788 mout->share_spdif = 0;
3789 /* FIXME: need notify? */
3792 mutex_unlock(&codec->spdif_mutex);
3794 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3795 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3797 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3800 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3801 * @codec: the HDA codec
3802 * @mout: hda_multi_out object
3803 * @stream_tag: stream tag to assign
3804 * @format: format id to assign
3805 * @substream: PCM substream to assign
3807 * Set up the i/o for analog out.
3808 * When the digital out is available, copy the front out to digital out, too.
3810 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3811 struct hda_multi_out *mout,
3812 unsigned int stream_tag,
3813 unsigned int format,
3814 struct snd_pcm_substream *substream)
3816 const hda_nid_t *nids = mout->dac_nids;
3817 int chs = substream->runtime->channels;
3818 struct hda_spdif_out *spdif;
3819 int i;
3821 mutex_lock(&codec->spdif_mutex);
3822 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3823 if (mout->dig_out_nid && mout->share_spdif &&
3824 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3825 if (chs == 2 &&
3826 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3827 format) &&
3828 !(spdif->status & IEC958_AES0_NONAUDIO)) {
3829 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3830 setup_dig_out_stream(codec, mout->dig_out_nid,
3831 stream_tag, format);
3832 } else {
3833 mout->dig_out_used = 0;
3834 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3837 mutex_unlock(&codec->spdif_mutex);
3839 /* front */
3840 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3841 0, format);
3842 if (!mout->no_share_stream &&
3843 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3844 /* headphone out will just decode front left/right (stereo) */
3845 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3846 0, format);
3847 /* extra outputs copied from front */
3848 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3849 if (!mout->no_share_stream && mout->hp_out_nid[i])
3850 snd_hda_codec_setup_stream(codec,
3851 mout->hp_out_nid[i],
3852 stream_tag, 0, format);
3854 /* surrounds */
3855 for (i = 1; i < mout->num_dacs; i++) {
3856 if (chs >= (i + 1) * 2) /* independent out */
3857 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3858 i * 2, format);
3859 else if (!mout->no_share_stream) /* copy front */
3860 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3861 0, format);
3864 /* extra surrounds */
3865 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3866 int ch = 0;
3867 if (!mout->extra_out_nid[i])
3868 break;
3869 if (chs >= (i + 1) * 2)
3870 ch = i * 2;
3871 else if (!mout->no_share_stream)
3872 break;
3873 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3874 stream_tag, ch, format);
3877 return 0;
3879 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3882 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3883 * @codec: the HDA codec
3884 * @mout: hda_multi_out object
3886 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3887 struct hda_multi_out *mout)
3889 const hda_nid_t *nids = mout->dac_nids;
3890 int i;
3892 for (i = 0; i < mout->num_dacs; i++)
3893 snd_hda_codec_cleanup_stream(codec, nids[i]);
3894 if (mout->hp_nid)
3895 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3896 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3897 if (mout->hp_out_nid[i])
3898 snd_hda_codec_cleanup_stream(codec,
3899 mout->hp_out_nid[i]);
3900 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3901 if (mout->extra_out_nid[i])
3902 snd_hda_codec_cleanup_stream(codec,
3903 mout->extra_out_nid[i]);
3904 mutex_lock(&codec->spdif_mutex);
3905 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3906 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3907 mout->dig_out_used = 0;
3909 mutex_unlock(&codec->spdif_mutex);
3910 return 0;
3912 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3915 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3916 * @codec: the HDA codec
3917 * @pin: referred pin NID
3919 * Guess the suitable VREF pin bits to be set as the pin-control value.
3920 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3922 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3924 unsigned int pincap;
3925 unsigned int oldval;
3926 oldval = snd_hda_codec_read(codec, pin, 0,
3927 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3928 pincap = snd_hda_query_pin_caps(codec, pin);
3929 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3930 /* Exception: if the default pin setup is vref50, we give it priority */
3931 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3932 return AC_PINCTL_VREF_80;
3933 else if (pincap & AC_PINCAP_VREF_50)
3934 return AC_PINCTL_VREF_50;
3935 else if (pincap & AC_PINCAP_VREF_100)
3936 return AC_PINCTL_VREF_100;
3937 else if (pincap & AC_PINCAP_VREF_GRD)
3938 return AC_PINCTL_VREF_GRD;
3939 return AC_PINCTL_VREF_HIZ;
3941 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3944 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3945 * @codec: the HDA codec
3946 * @pin: referred pin NID
3947 * @val: pin ctl value to audit
3949 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3950 hda_nid_t pin, unsigned int val)
3952 static unsigned int cap_lists[][2] = {
3953 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3954 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3955 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3956 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3958 unsigned int cap;
3960 if (!val)
3961 return 0;
3962 cap = snd_hda_query_pin_caps(codec, pin);
3963 if (!cap)
3964 return val; /* don't know what to do... */
3966 if (val & AC_PINCTL_OUT_EN) {
3967 if (!(cap & AC_PINCAP_OUT))
3968 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3969 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3970 val &= ~AC_PINCTL_HP_EN;
3973 if (val & AC_PINCTL_IN_EN) {
3974 if (!(cap & AC_PINCAP_IN))
3975 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3976 else {
3977 unsigned int vcap, vref;
3978 int i;
3979 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3980 vref = val & AC_PINCTL_VREFEN;
3981 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3982 if (vref == cap_lists[i][0] &&
3983 !(vcap & cap_lists[i][1])) {
3984 if (i == ARRAY_SIZE(cap_lists) - 1)
3985 vref = AC_PINCTL_VREF_HIZ;
3986 else
3987 vref = cap_lists[i + 1][0];
3990 val &= ~AC_PINCTL_VREFEN;
3991 val |= vref;
3995 return val;
3997 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
4000 * _snd_hda_pin_ctl - Helper to set pin ctl value
4001 * @codec: the HDA codec
4002 * @pin: referred pin NID
4003 * @val: pin control value to set
4004 * @cached: access over codec pinctl cache or direct write
4006 * This function is a helper to set a pin ctl value more safely.
4007 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4008 * value in pin target array via snd_hda_codec_set_pin_target(), then
4009 * actually writes the value via either snd_hda_codec_update_cache() or
4010 * snd_hda_codec_write() depending on @cached flag.
4012 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4013 unsigned int val, bool cached)
4015 val = snd_hda_correct_pin_ctl(codec, pin, val);
4016 snd_hda_codec_set_pin_target(codec, pin, val);
4017 if (cached)
4018 return snd_hda_codec_update_cache(codec, pin, 0,
4019 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4020 else
4021 return snd_hda_codec_write(codec, pin, 0,
4022 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4024 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
4027 * snd_hda_add_imux_item - Add an item to input_mux
4028 * @codec: the HDA codec
4029 * @imux: imux helper object
4030 * @label: the name of imux item to assign
4031 * @index: index number of imux item to assign
4032 * @type_idx: pointer to store the resultant label index
4034 * When the same label is used already in the existing items, the number
4035 * suffix is appended to the label. This label index number is stored
4036 * to type_idx when non-NULL pointer is given.
4038 int snd_hda_add_imux_item(struct hda_codec *codec,
4039 struct hda_input_mux *imux, const char *label,
4040 int index, int *type_idx)
4042 int i, label_idx = 0;
4043 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4044 codec_err(codec, "hda_codec: Too many imux items!\n");
4045 return -EINVAL;
4047 for (i = 0; i < imux->num_items; i++) {
4048 if (!strncmp(label, imux->items[i].label, strlen(label)))
4049 label_idx++;
4051 if (type_idx)
4052 *type_idx = label_idx;
4053 if (label_idx > 0)
4054 snprintf(imux->items[imux->num_items].label,
4055 sizeof(imux->items[imux->num_items].label),
4056 "%s %d", label, label_idx);
4057 else
4058 strlcpy(imux->items[imux->num_items].label, label,
4059 sizeof(imux->items[imux->num_items].label));
4060 imux->items[imux->num_items].index = index;
4061 imux->num_items++;
4062 return 0;
4064 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
4067 * snd_hda_bus_reset_codecs - Reset the bus
4068 * @bus: HD-audio bus
4070 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
4072 struct hda_codec *codec;
4074 list_for_each_codec(codec, bus) {
4075 /* FIXME: maybe a better way needed for forced reset */
4076 if (current_work() != &codec->jackpoll_work.work)
4077 cancel_delayed_work_sync(&codec->jackpoll_work);
4078 #ifdef CONFIG_PM
4079 if (hda_codec_is_power_on(codec)) {
4080 hda_call_codec_suspend(codec);
4081 hda_call_codec_resume(codec);
4083 #endif
4088 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4089 * @pcm: PCM caps bits
4090 * @buf: the string buffer to write
4091 * @buflen: the max buffer length
4093 * used by hda_proc.c and hda_eld.c
4095 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4097 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4098 int i, j;
4100 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4101 if (pcm & (AC_SUPPCM_BITS_8 << i))
4102 j += scnprintf(buf + j, buflen - j, " %d", bits[i]);
4104 buf[j] = '\0'; /* necessary when j == 0 */
4106 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
4108 MODULE_DESCRIPTION("HDA codec core");
4109 MODULE_LICENSE("GPL");