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