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