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