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

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

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

~ [ 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