~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/pci/hda/hda_codec.c

Version: ~ [ linux-5.17-rc1 ] ~ [ linux-5.16.2 ] ~ [ linux-5.15.16 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.93 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.173 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.225 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.262 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.297 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.299 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp