GNU Linux-libre 4.9.333-gnu1
[releases.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
855 #ifdef CONFIG_PM
856         codec->power_jiffies = jiffies;
857 #endif
858
859         snd_hda_sysfs_init(codec);
860
861         if (codec->bus->modelname) {
862                 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
863                 if (!codec->modelname) {
864                         err = -ENOMEM;
865                         goto error;
866                 }
867         }
868
869         fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
870         err = read_widget_caps(codec, fg);
871         if (err < 0)
872                 goto error;
873         err = read_pin_defaults(codec);
874         if (err < 0)
875                 goto error;
876
877         /* power-up all before initialization */
878         hda_set_power_state(codec, AC_PWRST_D0);
879         codec->core.dev.power.power_state = PMSG_ON;
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
1612         down_write(&codec->card->controls_rwsem);
1613         for (i = 0; i < codec->mixers.used; i++)
1614                 snd_ctl_remove(codec->card, items[i].kctl);
1615         up_write(&codec->card->controls_rwsem);
1616         snd_array_free(&codec->mixers);
1617         snd_array_free(&codec->nids);
1618 }
1619
1620 /**
1621  * snd_hda_lock_devices - pseudo device locking
1622  * @bus: the BUS
1623  *
1624  * toggle card->shutdown to allow/disallow the device access (as a hack)
1625  */
1626 int snd_hda_lock_devices(struct hda_bus *bus)
1627 {
1628         struct snd_card *card = bus->card;
1629         struct hda_codec *codec;
1630
1631         spin_lock(&card->files_lock);
1632         if (card->shutdown)
1633                 goto err_unlock;
1634         card->shutdown = 1;
1635         if (!list_empty(&card->ctl_files))
1636                 goto err_clear;
1637
1638         list_for_each_codec(codec, bus) {
1639                 struct hda_pcm *cpcm;
1640                 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1641                         if (!cpcm->pcm)
1642                                 continue;
1643                         if (cpcm->pcm->streams[0].substream_opened ||
1644                             cpcm->pcm->streams[1].substream_opened)
1645                                 goto err_clear;
1646                 }
1647         }
1648         spin_unlock(&card->files_lock);
1649         return 0;
1650
1651  err_clear:
1652         card->shutdown = 0;
1653  err_unlock:
1654         spin_unlock(&card->files_lock);
1655         return -EINVAL;
1656 }
1657 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1658
1659 /**
1660  * snd_hda_unlock_devices - pseudo device unlocking
1661  * @bus: the BUS
1662  */
1663 void snd_hda_unlock_devices(struct hda_bus *bus)
1664 {
1665         struct snd_card *card = bus->card;
1666
1667         spin_lock(&card->files_lock);
1668         card->shutdown = 0;
1669         spin_unlock(&card->files_lock);
1670 }
1671 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1672
1673 /**
1674  * snd_hda_codec_reset - Clear all objects assigned to the codec
1675  * @codec: HD-audio codec
1676  *
1677  * This frees the all PCM and control elements assigned to the codec, and
1678  * clears the caches and restores the pin default configurations.
1679  *
1680  * When a device is being used, it returns -EBSY.  If successfully freed,
1681  * returns zero.
1682  */
1683 int snd_hda_codec_reset(struct hda_codec *codec)
1684 {
1685         struct hda_bus *bus = codec->bus;
1686
1687         if (snd_hda_lock_devices(bus) < 0)
1688                 return -EBUSY;
1689
1690         /* OK, let it free */
1691         snd_hdac_device_unregister(&codec->core);
1692
1693         /* allow device access again */
1694         snd_hda_unlock_devices(bus);
1695         return 0;
1696 }
1697
1698 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1699
1700 /* apply the function to all matching slave ctls in the mixer list */
1701 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1702                       const char *suffix, map_slave_func_t func, void *data) 
1703 {
1704         struct hda_nid_item *items;
1705         const char * const *s;
1706         int i, err;
1707
1708         items = codec->mixers.list;
1709         for (i = 0; i < codec->mixers.used; i++) {
1710                 struct snd_kcontrol *sctl = items[i].kctl;
1711                 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1712                         continue;
1713                 for (s = slaves; *s; s++) {
1714                         char tmpname[sizeof(sctl->id.name)];
1715                         const char *name = *s;
1716                         if (suffix) {
1717                                 snprintf(tmpname, sizeof(tmpname), "%s %s",
1718                                          name, suffix);
1719                                 name = tmpname;
1720                         }
1721                         if (!strcmp(sctl->id.name, name)) {
1722                                 err = func(codec, data, sctl);
1723                                 if (err)
1724                                         return err;
1725                                 break;
1726                         }
1727                 }
1728         }
1729         return 0;
1730 }
1731
1732 static int check_slave_present(struct hda_codec *codec,
1733                                void *data, struct snd_kcontrol *sctl)
1734 {
1735         return 1;
1736 }
1737
1738 /* guess the value corresponding to 0dB */
1739 static int get_kctl_0dB_offset(struct hda_codec *codec,
1740                                struct snd_kcontrol *kctl, int *step_to_check)
1741 {
1742         int _tlv[4];
1743         const int *tlv = NULL;
1744         int val = -1;
1745
1746         if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1747                 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1748                 mm_segment_t fs = get_fs();
1749                 set_fs(get_ds());
1750                 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1751                         tlv = _tlv;
1752                 set_fs(fs);
1753         } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1754                 tlv = kctl->tlv.p;
1755         if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1756                 int step = tlv[3];
1757                 step &= ~TLV_DB_SCALE_MUTE;
1758                 if (!step)
1759                         return -1;
1760                 if (*step_to_check && *step_to_check != step) {
1761                         codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
1762                                    *step_to_check, step);
1763                         return -1;
1764                 }
1765                 *step_to_check = step;
1766                 val = -tlv[2] / step;
1767         }
1768         return val;
1769 }
1770
1771 /* call kctl->put with the given value(s) */
1772 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1773 {
1774         struct snd_ctl_elem_value *ucontrol;
1775         ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1776         if (!ucontrol)
1777                 return -ENOMEM;
1778         ucontrol->value.integer.value[0] = val;
1779         ucontrol->value.integer.value[1] = val;
1780         kctl->put(kctl, ucontrol);
1781         kfree(ucontrol);
1782         return 0;
1783 }
1784
1785 /* initialize the slave volume with 0dB */
1786 static int init_slave_0dB(struct hda_codec *codec,
1787                           void *data, struct snd_kcontrol *slave)
1788 {
1789         int offset = get_kctl_0dB_offset(codec, slave, data);
1790         if (offset > 0)
1791                 put_kctl_with_value(slave, offset);
1792         return 0;
1793 }
1794
1795 /* unmute the slave */
1796 static int init_slave_unmute(struct hda_codec *codec,
1797                              void *data, struct snd_kcontrol *slave)
1798 {
1799         return put_kctl_with_value(slave, 1);
1800 }
1801
1802 static int add_slave(struct hda_codec *codec,
1803                      void *data, struct snd_kcontrol *slave)
1804 {
1805         return snd_ctl_add_slave(data, slave);
1806 }
1807
1808 /**
1809  * __snd_hda_add_vmaster - create a virtual master control and add slaves
1810  * @codec: HD-audio codec
1811  * @name: vmaster control name
1812  * @tlv: TLV data (optional)
1813  * @slaves: slave control names (optional)
1814  * @suffix: suffix string to each slave name (optional)
1815  * @init_slave_vol: initialize slaves to unmute/0dB
1816  * @ctl_ret: store the vmaster kcontrol in return
1817  *
1818  * Create a virtual master control with the given name.  The TLV data
1819  * must be either NULL or a valid data.
1820  *
1821  * @slaves is a NULL-terminated array of strings, each of which is a
1822  * slave control name.  All controls with these names are assigned to
1823  * the new virtual master control.
1824  *
1825  * This function returns zero if successful or a negative error code.
1826  */
1827 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1828                         unsigned int *tlv, const char * const *slaves,
1829                           const char *suffix, bool init_slave_vol,
1830                           struct snd_kcontrol **ctl_ret)
1831 {
1832         struct snd_kcontrol *kctl;
1833         int err;
1834
1835         if (ctl_ret)
1836                 *ctl_ret = NULL;
1837
1838         err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1839         if (err != 1) {
1840                 codec_dbg(codec, "No slave found for %s\n", name);
1841                 return 0;
1842         }
1843         kctl = snd_ctl_make_virtual_master(name, tlv);
1844         if (!kctl)
1845                 return -ENOMEM;
1846         err = snd_hda_ctl_add(codec, 0, kctl);
1847         if (err < 0)
1848                 return err;
1849
1850         err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1851         if (err < 0)
1852                 return err;
1853
1854         /* init with master mute & zero volume */
1855         put_kctl_with_value(kctl, 0);
1856         if (init_slave_vol) {
1857                 int step = 0;
1858                 map_slaves(codec, slaves, suffix,
1859                            tlv ? init_slave_0dB : init_slave_unmute, &step);
1860         }
1861
1862         if (ctl_ret)
1863                 *ctl_ret = kctl;
1864         return 0;
1865 }
1866 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1867
1868 /*
1869  * mute-LED control using vmaster
1870  */
1871 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1872                                   struct snd_ctl_elem_info *uinfo)
1873 {
1874         static const char * const texts[] = {
1875                 "On", "Off", "Follow Master"
1876         };
1877
1878         return snd_ctl_enum_info(uinfo, 1, 3, texts);
1879 }
1880
1881 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1882                                  struct snd_ctl_elem_value *ucontrol)
1883 {
1884         struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1885         ucontrol->value.enumerated.item[0] = hook->mute_mode;
1886         return 0;
1887 }
1888
1889 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1890                                  struct snd_ctl_elem_value *ucontrol)
1891 {
1892         struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1893         unsigned int old_mode = hook->mute_mode;
1894
1895         hook->mute_mode = ucontrol->value.enumerated.item[0];
1896         if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1897                 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1898         if (old_mode == hook->mute_mode)
1899                 return 0;
1900         snd_hda_sync_vmaster_hook(hook);
1901         return 1;
1902 }
1903
1904 static struct snd_kcontrol_new vmaster_mute_mode = {
1905         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1906         .name = "Mute-LED Mode",
1907         .info = vmaster_mute_mode_info,
1908         .get = vmaster_mute_mode_get,
1909         .put = vmaster_mute_mode_put,
1910 };
1911
1912 /* meta hook to call each driver's vmaster hook */
1913 static void vmaster_hook(void *private_data, int enabled)
1914 {
1915         struct hda_vmaster_mute_hook *hook = private_data;
1916
1917         if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1918                 enabled = hook->mute_mode;
1919         hook->hook(hook->codec, enabled);
1920 }
1921
1922 /**
1923  * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1924  * @codec: the HDA codec
1925  * @hook: the vmaster hook object
1926  * @expose_enum_ctl: flag to create an enum ctl
1927  *
1928  * Add a mute-LED hook with the given vmaster switch kctl.
1929  * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1930  * created and associated with the given hook.
1931  */
1932 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
1933                              struct hda_vmaster_mute_hook *hook,
1934                              bool expose_enum_ctl)
1935 {
1936         struct snd_kcontrol *kctl;
1937
1938         if (!hook->hook || !hook->sw_kctl)
1939                 return 0;
1940         hook->codec = codec;
1941         hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1942         snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
1943         if (!expose_enum_ctl)
1944                 return 0;
1945         kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
1946         if (!kctl)
1947                 return -ENOMEM;
1948         return snd_hda_ctl_add(codec, 0, kctl);
1949 }
1950 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
1951
1952 /**
1953  * snd_hda_sync_vmaster_hook - Sync vmaster hook
1954  * @hook: the vmaster hook
1955  *
1956  * Call the hook with the current value for synchronization.
1957  * Should be called in init callback.
1958  */
1959 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
1960 {
1961         if (!hook->hook || !hook->codec)
1962                 return;
1963         /* don't call vmaster hook in the destructor since it might have
1964          * been already destroyed
1965          */
1966         if (hook->codec->bus->shutdown)
1967                 return;
1968         snd_ctl_sync_vmaster_hook(hook->sw_kctl);
1969 }
1970 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
1971
1972
1973 /**
1974  * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
1975  * @kcontrol: referred ctl element
1976  * @uinfo: pointer to get/store the data
1977  *
1978  * The control element is supposed to have the private_value field
1979  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1980  */
1981 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1982                                   struct snd_ctl_elem_info *uinfo)
1983 {
1984         int chs = get_amp_channels(kcontrol);
1985
1986         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1987         uinfo->count = chs == 3 ? 2 : 1;
1988         uinfo->value.integer.min = 0;
1989         uinfo->value.integer.max = 1;
1990         return 0;
1991 }
1992 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
1993
1994 /**
1995  * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
1996  * @kcontrol: ctl element
1997  * @ucontrol: pointer to get/store the data
1998  *
1999  * The control element is supposed to have the private_value field
2000  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2001  */
2002 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2003                                  struct snd_ctl_elem_value *ucontrol)
2004 {
2005         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2006         hda_nid_t nid = get_amp_nid(kcontrol);
2007         int chs = get_amp_channels(kcontrol);
2008         int dir = get_amp_direction(kcontrol);
2009         int idx = get_amp_index(kcontrol);
2010         long *valp = ucontrol->value.integer.value;
2011
2012         if (chs & 1)
2013                 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2014                            HDA_AMP_MUTE) ? 0 : 1;
2015         if (chs & 2)
2016                 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2017                          HDA_AMP_MUTE) ? 0 : 1;
2018         return 0;
2019 }
2020 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2021
2022 /**
2023  * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2024  * @kcontrol: ctl element
2025  * @ucontrol: pointer to get/store the data
2026  *
2027  * The control element is supposed to have the private_value field
2028  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2029  */
2030 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2031                                  struct snd_ctl_elem_value *ucontrol)
2032 {
2033         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2034         hda_nid_t nid = get_amp_nid(kcontrol);
2035         int chs = get_amp_channels(kcontrol);
2036         int dir = get_amp_direction(kcontrol);
2037         int idx = get_amp_index(kcontrol);
2038         long *valp = ucontrol->value.integer.value;
2039         int change = 0;
2040
2041         if (chs & 1) {
2042                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2043                                                   HDA_AMP_MUTE,
2044                                                   *valp ? 0 : HDA_AMP_MUTE);
2045                 valp++;
2046         }
2047         if (chs & 2)
2048                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2049                                                    HDA_AMP_MUTE,
2050                                                    *valp ? 0 : HDA_AMP_MUTE);
2051         hda_call_check_power_status(codec, nid);
2052         return change;
2053 }
2054 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2055
2056 /*
2057  * bound volume controls
2058  *
2059  * bind multiple volumes (# indices, from 0)
2060  */
2061
2062 #define AMP_VAL_IDX_SHIFT       19
2063 #define AMP_VAL_IDX_MASK        (0x0f<<19)
2064
2065 /**
2066  * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2067  * @kcontrol: ctl element
2068  * @ucontrol: pointer to get/store the data
2069  *
2070  * The control element is supposed to have the private_value field
2071  * set up via HDA_BIND_MUTE*() macros.
2072  */
2073 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2074                                   struct snd_ctl_elem_value *ucontrol)
2075 {
2076         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2077         unsigned long pval;
2078         int err;
2079
2080         mutex_lock(&codec->control_mutex);
2081         pval = kcontrol->private_value;
2082         kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2083         err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2084         kcontrol->private_value = pval;
2085         mutex_unlock(&codec->control_mutex);
2086         return err;
2087 }
2088 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2089
2090 /**
2091  * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2092  * @kcontrol: ctl element
2093  * @ucontrol: pointer to get/store the data
2094  *
2095  * The control element is supposed to have the private_value field
2096  * set up via HDA_BIND_MUTE*() macros.
2097  */
2098 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2099                                   struct snd_ctl_elem_value *ucontrol)
2100 {
2101         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2102         unsigned long pval;
2103         int i, indices, err = 0, change = 0;
2104
2105         mutex_lock(&codec->control_mutex);
2106         pval = kcontrol->private_value;
2107         indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2108         for (i = 0; i < indices; i++) {
2109                 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2110                         (i << AMP_VAL_IDX_SHIFT);
2111                 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2112                 if (err < 0)
2113                         break;
2114                 change |= err;
2115         }
2116         kcontrol->private_value = pval;
2117         mutex_unlock(&codec->control_mutex);
2118         return err < 0 ? err : change;
2119 }
2120 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2121
2122 /**
2123  * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2124  * @kcontrol: referred ctl element
2125  * @uinfo: pointer to get/store the data
2126  *
2127  * The control element is supposed to have the private_value field
2128  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2129  */
2130 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2131                                  struct snd_ctl_elem_info *uinfo)
2132 {
2133         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2134         struct hda_bind_ctls *c;
2135         int err;
2136
2137         mutex_lock(&codec->control_mutex);
2138         c = (struct hda_bind_ctls *)kcontrol->private_value;
2139         kcontrol->private_value = *c->values;
2140         err = c->ops->info(kcontrol, uinfo);
2141         kcontrol->private_value = (long)c;
2142         mutex_unlock(&codec->control_mutex);
2143         return err;
2144 }
2145 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2146
2147 /**
2148  * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2149  * @kcontrol: ctl element
2150  * @ucontrol: pointer to get/store the data
2151  *
2152  * The control element is supposed to have the private_value field
2153  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2154  */
2155 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2156                                 struct snd_ctl_elem_value *ucontrol)
2157 {
2158         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2159         struct hda_bind_ctls *c;
2160         int err;
2161
2162         mutex_lock(&codec->control_mutex);
2163         c = (struct hda_bind_ctls *)kcontrol->private_value;
2164         kcontrol->private_value = *c->values;
2165         err = c->ops->get(kcontrol, ucontrol);
2166         kcontrol->private_value = (long)c;
2167         mutex_unlock(&codec->control_mutex);
2168         return err;
2169 }
2170 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2171
2172 /**
2173  * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2174  * @kcontrol: ctl element
2175  * @ucontrol: pointer to get/store the data
2176  *
2177  * The control element is supposed to have the private_value field
2178  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2179  */
2180 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2181                                 struct snd_ctl_elem_value *ucontrol)
2182 {
2183         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2184         struct hda_bind_ctls *c;
2185         unsigned long *vals;
2186         int err = 0, change = 0;
2187
2188         mutex_lock(&codec->control_mutex);
2189         c = (struct hda_bind_ctls *)kcontrol->private_value;
2190         for (vals = c->values; *vals; vals++) {
2191                 kcontrol->private_value = *vals;
2192                 err = c->ops->put(kcontrol, ucontrol);
2193                 if (err < 0)
2194                         break;
2195                 change |= err;
2196         }
2197         kcontrol->private_value = (long)c;
2198         mutex_unlock(&codec->control_mutex);
2199         return err < 0 ? err : change;
2200 }
2201 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
2202
2203 /**
2204  * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2205  * @kcontrol: ctl element
2206  * @op_flag: operation flag
2207  * @size: byte size of input TLV
2208  * @tlv: TLV data
2209  *
2210  * The control element is supposed to have the private_value field
2211  * set up via HDA_BIND_VOL() macro.
2212  */
2213 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2214                            unsigned int size, unsigned int __user *tlv)
2215 {
2216         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2217         struct hda_bind_ctls *c;
2218         int err;
2219
2220         mutex_lock(&codec->control_mutex);
2221         c = (struct hda_bind_ctls *)kcontrol->private_value;
2222         kcontrol->private_value = *c->values;
2223         err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2224         kcontrol->private_value = (long)c;
2225         mutex_unlock(&codec->control_mutex);
2226         return err;
2227 }
2228 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
2229
2230 struct hda_ctl_ops snd_hda_bind_vol = {
2231         .info = snd_hda_mixer_amp_volume_info,
2232         .get = snd_hda_mixer_amp_volume_get,
2233         .put = snd_hda_mixer_amp_volume_put,
2234         .tlv = snd_hda_mixer_amp_tlv
2235 };
2236 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
2237
2238 struct hda_ctl_ops snd_hda_bind_sw = {
2239         .info = snd_hda_mixer_amp_switch_info,
2240         .get = snd_hda_mixer_amp_switch_get,
2241         .put = snd_hda_mixer_amp_switch_put,
2242         .tlv = snd_hda_mixer_amp_tlv
2243 };
2244 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
2245
2246 /*
2247  * SPDIF out controls
2248  */
2249
2250 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2251                                    struct snd_ctl_elem_info *uinfo)
2252 {
2253         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2254         uinfo->count = 1;
2255         return 0;
2256 }
2257
2258 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2259                                    struct snd_ctl_elem_value *ucontrol)
2260 {
2261         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2262                                            IEC958_AES0_NONAUDIO |
2263                                            IEC958_AES0_CON_EMPHASIS_5015 |
2264                                            IEC958_AES0_CON_NOT_COPYRIGHT;
2265         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2266                                            IEC958_AES1_CON_ORIGINAL;
2267         return 0;
2268 }
2269
2270 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2271                                    struct snd_ctl_elem_value *ucontrol)
2272 {
2273         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2274                                            IEC958_AES0_NONAUDIO |
2275                                            IEC958_AES0_PRO_EMPHASIS_5015;
2276         return 0;
2277 }
2278
2279 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2280                                      struct snd_ctl_elem_value *ucontrol)
2281 {
2282         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2283         int idx = kcontrol->private_value;
2284         struct hda_spdif_out *spdif;
2285
2286         mutex_lock(&codec->spdif_mutex);
2287         spdif = snd_array_elem(&codec->spdif_out, idx);
2288         ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2289         ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2290         ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2291         ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2292         mutex_unlock(&codec->spdif_mutex);
2293
2294         return 0;
2295 }
2296
2297 /* convert from SPDIF status bits to HDA SPDIF bits
2298  * bit 0 (DigEn) is always set zero (to be filled later)
2299  */
2300 static unsigned short convert_from_spdif_status(unsigned int sbits)
2301 {
2302         unsigned short val = 0;
2303
2304         if (sbits & IEC958_AES0_PROFESSIONAL)
2305                 val |= AC_DIG1_PROFESSIONAL;
2306         if (sbits & IEC958_AES0_NONAUDIO)
2307                 val |= AC_DIG1_NONAUDIO;
2308         if (sbits & IEC958_AES0_PROFESSIONAL) {
2309                 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2310                     IEC958_AES0_PRO_EMPHASIS_5015)
2311                         val |= AC_DIG1_EMPHASIS;
2312         } else {
2313                 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2314                     IEC958_AES0_CON_EMPHASIS_5015)
2315                         val |= AC_DIG1_EMPHASIS;
2316                 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2317                         val |= AC_DIG1_COPYRIGHT;
2318                 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2319                         val |= AC_DIG1_LEVEL;
2320                 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2321         }
2322         return val;
2323 }
2324
2325 /* convert to SPDIF status bits from HDA SPDIF bits
2326  */
2327 static unsigned int convert_to_spdif_status(unsigned short val)
2328 {
2329         unsigned int sbits = 0;
2330
2331         if (val & AC_DIG1_NONAUDIO)
2332                 sbits |= IEC958_AES0_NONAUDIO;
2333         if (val & AC_DIG1_PROFESSIONAL)
2334                 sbits |= IEC958_AES0_PROFESSIONAL;
2335         if (sbits & IEC958_AES0_PROFESSIONAL) {
2336                 if (val & AC_DIG1_EMPHASIS)
2337                         sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2338         } else {
2339                 if (val & AC_DIG1_EMPHASIS)
2340                         sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2341                 if (!(val & AC_DIG1_COPYRIGHT))
2342                         sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2343                 if (val & AC_DIG1_LEVEL)
2344                         sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2345                 sbits |= val & (0x7f << 8);
2346         }
2347         return sbits;
2348 }
2349
2350 /* set digital convert verbs both for the given NID and its slaves */
2351 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2352                         int mask, int val)
2353 {
2354         const hda_nid_t *d;
2355
2356         snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2357                                mask, val);
2358         d = codec->slave_dig_outs;
2359         if (!d)
2360                 return;
2361         for (; *d; d++)
2362                 snd_hdac_regmap_update(&codec->core, *d,
2363                                        AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2364 }
2365
2366 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2367                                        int dig1, int dig2)
2368 {
2369         unsigned int mask = 0;
2370         unsigned int val = 0;
2371
2372         if (dig1 != -1) {
2373                 mask |= 0xff;
2374                 val = dig1;
2375         }
2376         if (dig2 != -1) {
2377                 mask |= 0xff00;
2378                 val |= dig2 << 8;
2379         }
2380         set_dig_out(codec, nid, mask, val);
2381 }
2382
2383 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2384                                      struct snd_ctl_elem_value *ucontrol)
2385 {
2386         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2387         int idx = kcontrol->private_value;
2388         struct hda_spdif_out *spdif;
2389         hda_nid_t nid;
2390         unsigned short val;
2391         int change;
2392
2393         mutex_lock(&codec->spdif_mutex);
2394         spdif = snd_array_elem(&codec->spdif_out, idx);
2395         nid = spdif->nid;
2396         spdif->status = ucontrol->value.iec958.status[0] |
2397                 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2398                 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2399                 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2400         val = convert_from_spdif_status(spdif->status);
2401         val |= spdif->ctls & 1;
2402         change = spdif->ctls != val;
2403         spdif->ctls = val;
2404         if (change && nid != (u16)-1)
2405                 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2406         mutex_unlock(&codec->spdif_mutex);
2407         return change;
2408 }
2409
2410 #define snd_hda_spdif_out_switch_info   snd_ctl_boolean_mono_info
2411
2412 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2413                                         struct snd_ctl_elem_value *ucontrol)
2414 {
2415         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2416         int idx = kcontrol->private_value;
2417         struct hda_spdif_out *spdif;
2418
2419         mutex_lock(&codec->spdif_mutex);
2420         spdif = snd_array_elem(&codec->spdif_out, idx);
2421         ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2422         mutex_unlock(&codec->spdif_mutex);
2423         return 0;
2424 }
2425
2426 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2427                                   int dig1, int dig2)
2428 {
2429         set_dig_out_convert(codec, nid, dig1, dig2);
2430         /* unmute amp switch (if any) */
2431         if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2432             (dig1 & AC_DIG1_ENABLE))
2433                 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2434                                             HDA_AMP_MUTE, 0);
2435 }
2436
2437 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2438                                         struct snd_ctl_elem_value *ucontrol)
2439 {
2440         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2441         int idx = kcontrol->private_value;
2442         struct hda_spdif_out *spdif;
2443         hda_nid_t nid;
2444         unsigned short val;
2445         int change;
2446
2447         mutex_lock(&codec->spdif_mutex);
2448         spdif = snd_array_elem(&codec->spdif_out, idx);
2449         nid = spdif->nid;
2450         val = spdif->ctls & ~AC_DIG1_ENABLE;
2451         if (ucontrol->value.integer.value[0])
2452                 val |= AC_DIG1_ENABLE;
2453         change = spdif->ctls != val;
2454         spdif->ctls = val;
2455         if (change && nid != (u16)-1)
2456                 set_spdif_ctls(codec, nid, val & 0xff, -1);
2457         mutex_unlock(&codec->spdif_mutex);
2458         return change;
2459 }
2460
2461 static struct snd_kcontrol_new dig_mixes[] = {
2462         {
2463                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2464                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2465                 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2466                 .info = snd_hda_spdif_mask_info,
2467                 .get = snd_hda_spdif_cmask_get,
2468         },
2469         {
2470                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2471                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2472                 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2473                 .info = snd_hda_spdif_mask_info,
2474                 .get = snd_hda_spdif_pmask_get,
2475         },
2476         {
2477                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2478                 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2479                 .info = snd_hda_spdif_mask_info,
2480                 .get = snd_hda_spdif_default_get,
2481                 .put = snd_hda_spdif_default_put,
2482         },
2483         {
2484                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2485                 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2486                 .info = snd_hda_spdif_out_switch_info,
2487                 .get = snd_hda_spdif_out_switch_get,
2488                 .put = snd_hda_spdif_out_switch_put,
2489         },
2490         { } /* end */
2491 };
2492
2493 /**
2494  * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2495  * @codec: the HDA codec
2496  * @associated_nid: NID that new ctls associated with
2497  * @cvt_nid: converter NID
2498  * @type: HDA_PCM_TYPE_*
2499  * Creates controls related with the digital output.
2500  * Called from each patch supporting the digital out.
2501  *
2502  * Returns 0 if successful, or a negative error code.
2503  */
2504 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2505                                 hda_nid_t associated_nid,
2506                                 hda_nid_t cvt_nid,
2507                                 int type)
2508 {
2509         int err;
2510         struct snd_kcontrol *kctl;
2511         struct snd_kcontrol_new *dig_mix;
2512         int idx = 0;
2513         int val = 0;
2514         const int spdif_index = 16;
2515         struct hda_spdif_out *spdif;
2516         struct hda_bus *bus = codec->bus;
2517
2518         if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2519             type == HDA_PCM_TYPE_SPDIF) {
2520                 idx = spdif_index;
2521         } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2522                    type == HDA_PCM_TYPE_HDMI) {
2523                 /* suppose a single SPDIF device */
2524                 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2525                         kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2526                         if (!kctl)
2527                                 break;
2528                         kctl->id.index = spdif_index;
2529                 }
2530                 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2531         }
2532         if (!bus->primary_dig_out_type)
2533                 bus->primary_dig_out_type = type;
2534
2535         idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2536         if (idx < 0) {
2537                 codec_err(codec, "too many IEC958 outputs\n");
2538                 return -EBUSY;
2539         }
2540         spdif = snd_array_new(&codec->spdif_out);
2541         if (!spdif)
2542                 return -ENOMEM;
2543         for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2544                 kctl = snd_ctl_new1(dig_mix, codec);
2545                 if (!kctl)
2546                         return -ENOMEM;
2547                 kctl->id.index = idx;
2548                 kctl->private_value = codec->spdif_out.used - 1;
2549                 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2550                 if (err < 0)
2551                         return err;
2552         }
2553         spdif->nid = cvt_nid;
2554         snd_hdac_regmap_read(&codec->core, cvt_nid,
2555                              AC_VERB_GET_DIGI_CONVERT_1, &val);
2556         spdif->ctls = val;
2557         spdif->status = convert_to_spdif_status(spdif->ctls);
2558         return 0;
2559 }
2560 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2561
2562 /**
2563  * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2564  * @codec: the HDA codec
2565  * @nid: widget NID
2566  *
2567  * call within spdif_mutex lock
2568  */
2569 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2570                                                hda_nid_t nid)
2571 {
2572         int i;
2573         for (i = 0; i < codec->spdif_out.used; i++) {
2574                 struct hda_spdif_out *spdif =
2575                                 snd_array_elem(&codec->spdif_out, i);
2576                 if (spdif->nid == nid)
2577                         return spdif;
2578         }
2579         return NULL;
2580 }
2581 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2582
2583 /**
2584  * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2585  * @codec: the HDA codec
2586  * @idx: the SPDIF ctl index
2587  *
2588  * Unassign the widget from the given SPDIF control.
2589  */
2590 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2591 {
2592         struct hda_spdif_out *spdif;
2593
2594         mutex_lock(&codec->spdif_mutex);
2595         spdif = snd_array_elem(&codec->spdif_out, idx);
2596         spdif->nid = (u16)-1;
2597         mutex_unlock(&codec->spdif_mutex);
2598 }
2599 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2600
2601 /**
2602  * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2603  * @codec: the HDA codec
2604  * @idx: the SPDIF ctl idx
2605  * @nid: widget NID
2606  *
2607  * Assign the widget to the SPDIF control with the given index.
2608  */
2609 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2610 {
2611         struct hda_spdif_out *spdif;
2612         unsigned short val;
2613
2614         mutex_lock(&codec->spdif_mutex);
2615         spdif = snd_array_elem(&codec->spdif_out, idx);
2616         if (spdif->nid != nid) {
2617                 spdif->nid = nid;
2618                 val = spdif->ctls;
2619                 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2620         }
2621         mutex_unlock(&codec->spdif_mutex);
2622 }
2623 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2624
2625 /*
2626  * SPDIF sharing with analog output
2627  */
2628 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2629                               struct snd_ctl_elem_value *ucontrol)
2630 {
2631         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2632         ucontrol->value.integer.value[0] = mout->share_spdif;
2633         return 0;
2634 }
2635
2636 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2637                               struct snd_ctl_elem_value *ucontrol)
2638 {
2639         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2640         mout->share_spdif = !!ucontrol->value.integer.value[0];
2641         return 0;
2642 }
2643
2644 static struct snd_kcontrol_new spdif_share_sw = {
2645         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2646         .name = "IEC958 Default PCM Playback Switch",
2647         .info = snd_ctl_boolean_mono_info,
2648         .get = spdif_share_sw_get,
2649         .put = spdif_share_sw_put,
2650 };
2651
2652 /**
2653  * snd_hda_create_spdif_share_sw - create Default PCM switch
2654  * @codec: the HDA codec
2655  * @mout: multi-out instance
2656  */
2657 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2658                                   struct hda_multi_out *mout)
2659 {
2660         struct snd_kcontrol *kctl;
2661
2662         if (!mout->dig_out_nid)
2663                 return 0;
2664
2665         kctl = snd_ctl_new1(&spdif_share_sw, mout);
2666         if (!kctl)
2667                 return -ENOMEM;
2668         /* ATTENTION: here mout is passed as private_data, instead of codec */
2669         return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2670 }
2671 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2672
2673 /*
2674  * SPDIF input
2675  */
2676
2677 #define snd_hda_spdif_in_switch_info    snd_hda_spdif_out_switch_info
2678
2679 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2680                                        struct snd_ctl_elem_value *ucontrol)
2681 {
2682         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2683
2684         ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2685         return 0;
2686 }
2687
2688 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2689                                        struct snd_ctl_elem_value *ucontrol)
2690 {
2691         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2692         hda_nid_t nid = kcontrol->private_value;
2693         unsigned int val = !!ucontrol->value.integer.value[0];
2694         int change;
2695
2696         mutex_lock(&codec->spdif_mutex);
2697         change = codec->spdif_in_enable != val;
2698         if (change) {
2699                 codec->spdif_in_enable = val;
2700                 snd_hdac_regmap_write(&codec->core, nid,
2701                                       AC_VERB_SET_DIGI_CONVERT_1, val);
2702         }
2703         mutex_unlock(&codec->spdif_mutex);
2704         return change;
2705 }
2706
2707 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2708                                        struct snd_ctl_elem_value *ucontrol)
2709 {
2710         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2711         hda_nid_t nid = kcontrol->private_value;
2712         unsigned int val;
2713         unsigned int sbits;
2714
2715         snd_hdac_regmap_read(&codec->core, nid,
2716                              AC_VERB_GET_DIGI_CONVERT_1, &val);
2717         sbits = convert_to_spdif_status(val);
2718         ucontrol->value.iec958.status[0] = sbits;
2719         ucontrol->value.iec958.status[1] = sbits >> 8;
2720         ucontrol->value.iec958.status[2] = sbits >> 16;
2721         ucontrol->value.iec958.status[3] = sbits >> 24;
2722         return 0;
2723 }
2724
2725 static struct snd_kcontrol_new dig_in_ctls[] = {
2726         {
2727                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2728                 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2729                 .info = snd_hda_spdif_in_switch_info,
2730                 .get = snd_hda_spdif_in_switch_get,
2731                 .put = snd_hda_spdif_in_switch_put,
2732         },
2733         {
2734                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2735                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2736                 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2737                 .info = snd_hda_spdif_mask_info,
2738                 .get = snd_hda_spdif_in_status_get,
2739         },
2740         { } /* end */
2741 };
2742
2743 /**
2744  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2745  * @codec: the HDA codec
2746  * @nid: audio in widget NID
2747  *
2748  * Creates controls related with the SPDIF input.
2749  * Called from each patch supporting the SPDIF in.
2750  *
2751  * Returns 0 if successful, or a negative error code.
2752  */
2753 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2754 {
2755         int err;
2756         struct snd_kcontrol *kctl;
2757         struct snd_kcontrol_new *dig_mix;
2758         int idx;
2759
2760         idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2761         if (idx < 0) {
2762                 codec_err(codec, "too many IEC958 inputs\n");
2763                 return -EBUSY;
2764         }
2765         for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2766                 kctl = snd_ctl_new1(dig_mix, codec);
2767                 if (!kctl)
2768                         return -ENOMEM;
2769                 kctl->private_value = nid;
2770                 err = snd_hda_ctl_add(codec, nid, kctl);
2771                 if (err < 0)
2772                         return err;
2773         }
2774         codec->spdif_in_enable =
2775                 snd_hda_codec_read(codec, nid, 0,
2776                                    AC_VERB_GET_DIGI_CONVERT_1, 0) &
2777                 AC_DIG1_ENABLE;
2778         return 0;
2779 }
2780 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2781
2782 /**
2783  * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2784  * @codec: the HDA codec
2785  * @fg: function group (not used now)
2786  * @power_state: the power state to set (AC_PWRST_*)
2787  *
2788  * Set the given power state to all widgets that have the power control.
2789  * If the codec has power_filter set, it evaluates the power state and
2790  * filter out if it's unchanged as D3.
2791  */
2792 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2793                                     unsigned int power_state)
2794 {
2795         hda_nid_t nid;
2796
2797         for_each_hda_codec_node(nid, codec) {
2798                 unsigned int wcaps = get_wcaps(codec, nid);
2799                 unsigned int state = power_state;
2800                 if (!(wcaps & AC_WCAP_POWER))
2801                         continue;
2802                 if (codec->power_filter) {
2803                         state = codec->power_filter(codec, nid, power_state);
2804                         if (state != power_state && power_state == AC_PWRST_D3)
2805                                 continue;
2806                 }
2807                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2808                                     state);
2809         }
2810 }
2811 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2812
2813 /*
2814  * wait until the state is reached, returns the current state
2815  */
2816 static unsigned int hda_sync_power_state(struct hda_codec *codec,
2817                                          hda_nid_t fg,
2818                                          unsigned int power_state)
2819 {
2820         unsigned long end_time = jiffies + msecs_to_jiffies(500);
2821         unsigned int state, actual_state;
2822
2823         for (;;) {
2824                 state = snd_hda_codec_read(codec, fg, 0,
2825                                            AC_VERB_GET_POWER_STATE, 0);
2826                 if (state & AC_PWRST_ERROR)
2827                         break;
2828                 actual_state = (state >> 4) & 0x0f;
2829                 if (actual_state == power_state)
2830                         break;
2831                 if (time_after_eq(jiffies, end_time))
2832                         break;
2833                 /* wait until the codec reachs to the target state */
2834                 msleep(1);
2835         }
2836         return state;
2837 }
2838
2839 /**
2840  * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2841  * @codec: the HDA codec
2842  * @nid: widget NID
2843  * @power_state: power state to evalue
2844  *
2845  * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2846  * This can be used a codec power_filter callback.
2847  */
2848 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2849                                              hda_nid_t nid,
2850                                              unsigned int power_state)
2851 {
2852         if (nid == codec->core.afg || nid == codec->core.mfg)
2853                 return power_state;
2854         if (power_state == AC_PWRST_D3 &&
2855             get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2856             (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2857                 int eapd = snd_hda_codec_read(codec, nid, 0,
2858                                               AC_VERB_GET_EAPD_BTLENABLE, 0);
2859                 if (eapd & 0x02)
2860                         return AC_PWRST_D0;
2861         }
2862         return power_state;
2863 }
2864 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2865
2866 /*
2867  * set power state of the codec, and return the power state
2868  */
2869 static unsigned int hda_set_power_state(struct hda_codec *codec,
2870                                         unsigned int power_state)
2871 {
2872         hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2873         int count;
2874         unsigned int state;
2875         int flags = 0;
2876
2877         /* this delay seems necessary to avoid click noise at power-down */
2878         if (power_state == AC_PWRST_D3) {
2879                 if (codec->depop_delay < 0)
2880                         msleep(codec_has_epss(codec) ? 10 : 100);
2881                 else if (codec->depop_delay > 0)
2882                         msleep(codec->depop_delay);
2883                 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2884         }
2885
2886         /* repeat power states setting at most 10 times*/
2887         for (count = 0; count < 10; count++) {
2888                 if (codec->patch_ops.set_power_state)
2889                         codec->patch_ops.set_power_state(codec, fg,
2890                                                          power_state);
2891                 else {
2892                         state = power_state;
2893                         if (codec->power_filter)
2894                                 state = codec->power_filter(codec, fg, state);
2895                         if (state == power_state || power_state != AC_PWRST_D3)
2896                                 snd_hda_codec_read(codec, fg, flags,
2897                                                    AC_VERB_SET_POWER_STATE,
2898                                                    state);
2899                         snd_hda_codec_set_power_to_all(codec, fg, power_state);
2900                 }
2901                 state = hda_sync_power_state(codec, fg, power_state);
2902                 if (!(state & AC_PWRST_ERROR))
2903                         break;
2904         }
2905
2906         return state;
2907 }
2908
2909 /* sync power states of all widgets;
2910  * this is called at the end of codec parsing
2911  */
2912 static void sync_power_up_states(struct hda_codec *codec)
2913 {
2914         hda_nid_t nid;
2915
2916         /* don't care if no filter is used */
2917         if (!codec->power_filter)
2918                 return;
2919
2920         for_each_hda_codec_node(nid, codec) {
2921                 unsigned int wcaps = get_wcaps(codec, nid);
2922                 unsigned int target;
2923                 if (!(wcaps & AC_WCAP_POWER))
2924                         continue;
2925                 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2926                 if (target == AC_PWRST_D0)
2927                         continue;
2928                 if (!snd_hda_check_power_state(codec, nid, target))
2929                         snd_hda_codec_write(codec, nid, 0,
2930                                             AC_VERB_SET_POWER_STATE, target);
2931         }
2932 }
2933
2934 #ifdef CONFIG_SND_HDA_RECONFIG
2935 /* execute additional init verbs */
2936 static void hda_exec_init_verbs(struct hda_codec *codec)
2937 {
2938         if (codec->init_verbs.list)
2939                 snd_hda_sequence_write(codec, codec->init_verbs.list);
2940 }
2941 #else
2942 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2943 #endif
2944
2945 #ifdef CONFIG_PM
2946 /* update the power on/off account with the current jiffies */
2947 static void update_power_acct(struct hda_codec *codec, bool on)
2948 {
2949         unsigned long delta = jiffies - codec->power_jiffies;
2950
2951         if (on)
2952                 codec->power_on_acct += delta;
2953         else
2954                 codec->power_off_acct += delta;
2955         codec->power_jiffies += delta;
2956 }
2957
2958 void snd_hda_update_power_acct(struct hda_codec *codec)
2959 {
2960         update_power_acct(codec, hda_codec_is_power_on(codec));
2961 }
2962
2963 /*
2964  * call suspend and power-down; used both from PM and power-save
2965  * this function returns the power state in the end
2966  */
2967 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
2968 {
2969         unsigned int state;
2970
2971         atomic_inc(&codec->core.in_pm);
2972
2973         if (codec->patch_ops.suspend)
2974                 codec->patch_ops.suspend(codec);
2975         hda_cleanup_all_streams(codec);
2976         state = hda_set_power_state(codec, AC_PWRST_D3);
2977         update_power_acct(codec, true);
2978         atomic_dec(&codec->core.in_pm);
2979         return state;
2980 }
2981
2982 /*
2983  * kick up codec; used both from PM and power-save
2984  */
2985 static void hda_call_codec_resume(struct hda_codec *codec)
2986 {
2987         atomic_inc(&codec->core.in_pm);
2988
2989         if (codec->core.regmap)
2990                 regcache_mark_dirty(codec->core.regmap);
2991
2992         codec->power_jiffies = jiffies;
2993
2994         hda_set_power_state(codec, AC_PWRST_D0);
2995         restore_shutup_pins(codec);
2996         hda_exec_init_verbs(codec);
2997         snd_hda_jack_set_dirty_all(codec);
2998         if (codec->patch_ops.resume)
2999                 codec->patch_ops.resume(codec);
3000         else {
3001                 if (codec->patch_ops.init)
3002                         codec->patch_ops.init(codec);
3003                 if (codec->core.regmap)
3004                         regcache_sync(codec->core.regmap);
3005         }
3006
3007         if (codec->jackpoll_interval)
3008                 hda_jackpoll_work(&codec->jackpoll_work.work);
3009         else
3010                 snd_hda_jack_report_sync(codec);
3011         codec->core.dev.power.power_state = PMSG_ON;
3012         atomic_dec(&codec->core.in_pm);
3013 }
3014
3015 static int hda_codec_runtime_suspend(struct device *dev)
3016 {
3017         struct hda_codec *codec = dev_to_hda_codec(dev);
3018         struct hda_pcm *pcm;
3019         unsigned int state;
3020
3021         cancel_delayed_work_sync(&codec->jackpoll_work);
3022         list_for_each_entry(pcm, &codec->pcm_list_head, list)
3023                 snd_pcm_suspend_all(pcm->pcm);
3024         state = hda_call_codec_suspend(codec);
3025         if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3026             (state & AC_PWRST_CLK_STOP_OK))
3027                 snd_hdac_codec_link_down(&codec->core);
3028         snd_hdac_link_power(&codec->core, false);
3029         return 0;
3030 }
3031
3032 static int hda_codec_runtime_resume(struct device *dev)
3033 {
3034         struct hda_codec *codec = dev_to_hda_codec(dev);
3035
3036         snd_hdac_link_power(&codec->core, true);
3037         snd_hdac_codec_link_up(&codec->core);
3038         hda_call_codec_resume(codec);
3039         pm_runtime_mark_last_busy(dev);
3040         return 0;
3041 }
3042 #endif /* CONFIG_PM */
3043
3044 #ifdef CONFIG_PM_SLEEP
3045 static int hda_codec_force_resume(struct device *dev)
3046 {
3047         int ret;
3048
3049         /* The get/put pair below enforces the runtime resume even if the
3050          * device hasn't been used at suspend time.  This trick is needed to
3051          * update the jack state change during the sleep.
3052          */
3053         pm_runtime_get_noresume(dev);
3054         ret = pm_runtime_force_resume(dev);
3055         pm_runtime_put(dev);
3056         return ret;
3057 }
3058
3059 static int hda_codec_pm_suspend(struct device *dev)
3060 {
3061         dev->power.power_state = PMSG_SUSPEND;
3062         return pm_runtime_force_suspend(dev);
3063 }
3064
3065 static int hda_codec_pm_resume(struct device *dev)
3066 {
3067         dev->power.power_state = PMSG_RESUME;
3068         return hda_codec_force_resume(dev);
3069 }
3070
3071 static int hda_codec_pm_freeze(struct device *dev)
3072 {
3073         dev->power.power_state = PMSG_FREEZE;
3074         return pm_runtime_force_suspend(dev);
3075 }
3076
3077 static int hda_codec_pm_thaw(struct device *dev)
3078 {
3079         dev->power.power_state = PMSG_THAW;
3080         return hda_codec_force_resume(dev);
3081 }
3082
3083 static int hda_codec_pm_restore(struct device *dev)
3084 {
3085         dev->power.power_state = PMSG_RESTORE;
3086         return hda_codec_force_resume(dev);
3087 }
3088 #endif /* CONFIG_PM_SLEEP */
3089
3090 /* referred in hda_bind.c */
3091 const struct dev_pm_ops hda_codec_driver_pm = {
3092 #ifdef CONFIG_PM_SLEEP
3093         .suspend = hda_codec_pm_suspend,
3094         .resume = hda_codec_pm_resume,
3095         .freeze = hda_codec_pm_freeze,
3096         .thaw = hda_codec_pm_thaw,
3097         .poweroff = hda_codec_pm_suspend,
3098         .restore = hda_codec_pm_restore,
3099 #endif /* CONFIG_PM_SLEEP */
3100         SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3101                            NULL)
3102 };
3103
3104 /*
3105  * add standard channel maps if not specified
3106  */
3107 static int add_std_chmaps(struct hda_codec *codec)
3108 {
3109         struct hda_pcm *pcm;
3110         int str, err;
3111
3112         list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3113                 for (str = 0; str < 2; str++) {
3114                         struct hda_pcm_stream *hinfo = &pcm->stream[str];
3115                         struct snd_pcm_chmap *chmap;
3116                         const struct snd_pcm_chmap_elem *elem;
3117
3118                         if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
3119                                 continue;
3120                         elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3121                         err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3122                                                      hinfo->channels_max,
3123                                                      0, &chmap);
3124                         if (err < 0)
3125                                 return err;
3126                         chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3127                 }
3128         }
3129         return 0;
3130 }
3131
3132 /* default channel maps for 2.1 speakers;
3133  * since HD-audio supports only stereo, odd number channels are omitted
3134  */
3135 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3136         { .channels = 2,
3137           .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3138         { .channels = 4,
3139           .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3140                    SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3141         { }
3142 };
3143 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3144
3145 int snd_hda_codec_build_controls(struct hda_codec *codec)
3146 {
3147         int err = 0;
3148         hda_exec_init_verbs(codec);
3149         /* continue to initialize... */
3150         if (codec->patch_ops.init)
3151                 err = codec->patch_ops.init(codec);
3152         if (!err && codec->patch_ops.build_controls)
3153                 err = codec->patch_ops.build_controls(codec);
3154         if (err < 0)
3155                 return err;
3156
3157         /* we create chmaps here instead of build_pcms */
3158         err = add_std_chmaps(codec);
3159         if (err < 0)
3160                 return err;
3161
3162         if (codec->jackpoll_interval)
3163                 hda_jackpoll_work(&codec->jackpoll_work.work);
3164         else
3165                 snd_hda_jack_report_sync(codec); /* call at the last init point */
3166         sync_power_up_states(codec);
3167         return 0;
3168 }
3169
3170 /*
3171  * PCM stuff
3172  */
3173 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3174                                       struct hda_codec *codec,
3175                                       struct snd_pcm_substream *substream)
3176 {
3177         return 0;
3178 }
3179
3180 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3181                                    struct hda_codec *codec,
3182                                    unsigned int stream_tag,
3183                                    unsigned int format,
3184                                    struct snd_pcm_substream *substream)
3185 {
3186         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3187         return 0;
3188 }
3189
3190 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3191                                    struct hda_codec *codec,
3192                                    struct snd_pcm_substream *substream)
3193 {
3194         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3195         return 0;
3196 }
3197
3198 static int set_pcm_default_values(struct hda_codec *codec,
3199                                   struct hda_pcm_stream *info)
3200 {
3201         int err;
3202
3203         /* query support PCM information from the given NID */
3204         if (info->nid && (!info->rates || !info->formats)) {
3205                 err = snd_hda_query_supported_pcm(codec, info->nid,
3206                                 info->rates ? NULL : &info->rates,
3207                                 info->formats ? NULL : &info->formats,
3208                                 info->maxbps ? NULL : &info->maxbps);
3209                 if (err < 0)
3210                         return err;
3211         }
3212         if (info->ops.open == NULL)
3213                 info->ops.open = hda_pcm_default_open_close;
3214         if (info->ops.close == NULL)
3215                 info->ops.close = hda_pcm_default_open_close;
3216         if (info->ops.prepare == NULL) {
3217                 if (snd_BUG_ON(!info->nid))
3218                         return -EINVAL;
3219                 info->ops.prepare = hda_pcm_default_prepare;
3220         }
3221         if (info->ops.cleanup == NULL) {
3222                 if (snd_BUG_ON(!info->nid))
3223                         return -EINVAL;
3224                 info->ops.cleanup = hda_pcm_default_cleanup;
3225         }
3226         return 0;
3227 }
3228
3229 /*
3230  * codec prepare/cleanup entries
3231  */
3232 /**
3233  * snd_hda_codec_prepare - Prepare a stream
3234  * @codec: the HDA codec
3235  * @hinfo: PCM information
3236  * @stream: stream tag to assign
3237  * @format: format id to assign
3238  * @substream: PCM substream to assign
3239  *
3240  * Calls the prepare callback set by the codec with the given arguments.
3241  * Clean up the inactive streams when successful.
3242  */
3243 int snd_hda_codec_prepare(struct hda_codec *codec,
3244                           struct hda_pcm_stream *hinfo,
3245                           unsigned int stream,
3246                           unsigned int format,
3247                           struct snd_pcm_substream *substream)
3248 {
3249         int ret;
3250         mutex_lock(&codec->bus->prepare_mutex);
3251         if (hinfo->ops.prepare)
3252                 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3253                                          substream);
3254         else
3255                 ret = -ENODEV;
3256         if (ret >= 0)
3257                 purify_inactive_streams(codec);
3258         mutex_unlock(&codec->bus->prepare_mutex);
3259         return ret;
3260 }
3261 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3262
3263 /**
3264  * snd_hda_codec_cleanup - Prepare a stream
3265  * @codec: the HDA codec
3266  * @hinfo: PCM information
3267  * @substream: PCM substream
3268  *
3269  * Calls the cleanup callback set by the codec with the given arguments.
3270  */
3271 void snd_hda_codec_cleanup(struct hda_codec *codec,
3272                            struct hda_pcm_stream *hinfo,
3273                            struct snd_pcm_substream *substream)
3274 {
3275         mutex_lock(&codec->bus->prepare_mutex);
3276         if (hinfo->ops.cleanup)
3277                 hinfo->ops.cleanup(hinfo, codec, substream);
3278         mutex_unlock(&codec->bus->prepare_mutex);
3279 }
3280 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3281
3282 /* global */
3283 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3284         "Audio", "SPDIF", "HDMI", "Modem"
3285 };
3286
3287 /*
3288  * get the empty PCM device number to assign
3289  */
3290 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3291 {
3292         /* audio device indices; not linear to keep compatibility */
3293         /* assigned to static slots up to dev#10; if more needed, assign
3294          * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3295          */
3296         static int audio_idx[HDA_PCM_NTYPES][5] = {
3297                 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3298                 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3299                 [HDA_PCM_TYPE_HDMI]  = { 3, 7, 8, 9, -1 },
3300                 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3301         };
3302         int i;
3303
3304         if (type >= HDA_PCM_NTYPES) {
3305                 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3306                 return -EINVAL;
3307         }
3308
3309         for (i = 0; audio_idx[type][i] >= 0; i++) {
3310 #ifndef CONFIG_SND_DYNAMIC_MINORS
3311                 if (audio_idx[type][i] >= 8)
3312                         break;
3313 #endif
3314                 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3315                         return audio_idx[type][i];
3316         }
3317
3318 #ifdef CONFIG_SND_DYNAMIC_MINORS
3319         /* non-fixed slots starting from 10 */
3320         for (i = 10; i < 32; i++) {
3321                 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3322                         return i;
3323         }
3324 #endif
3325
3326         dev_warn(bus->card->dev, "Too many %s devices\n",
3327                 snd_hda_pcm_type_name[type]);
3328 #ifndef CONFIG_SND_DYNAMIC_MINORS
3329         dev_warn(bus->card->dev,
3330                  "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3331 #endif
3332         return -EAGAIN;
3333 }
3334
3335 /* call build_pcms ops of the given codec and set up the default parameters */
3336 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3337 {
3338         struct hda_pcm *cpcm;
3339         int err;
3340
3341         if (!list_empty(&codec->pcm_list_head))
3342                 return 0; /* already parsed */
3343
3344         if (!codec->patch_ops.build_pcms)
3345                 return 0;
3346
3347         err = codec->patch_ops.build_pcms(codec);
3348         if (err < 0) {
3349                 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3350                           codec->core.addr, err);
3351                 return err;
3352         }
3353
3354         list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3355                 int stream;
3356
3357                 for (stream = 0; stream < 2; stream++) {
3358                         struct hda_pcm_stream *info = &cpcm->stream[stream];
3359
3360                         if (!info->substreams)
3361                                 continue;
3362                         err = set_pcm_default_values(codec, info);
3363                         if (err < 0) {
3364                                 codec_warn(codec,
3365                                            "fail to setup default for PCM %s\n",
3366                                            cpcm->name);
3367                                 return err;
3368                         }
3369                 }
3370         }
3371
3372         return 0;
3373 }
3374
3375 /* assign all PCMs of the given codec */
3376 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3377 {
3378         struct hda_bus *bus = codec->bus;
3379         struct hda_pcm *cpcm;
3380         int dev, err;
3381
3382         err = snd_hda_codec_parse_pcms(codec);
3383         if (err < 0)
3384                 return err;
3385
3386         /* attach a new PCM streams */
3387         list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3388                 if (cpcm->pcm)
3389                         continue; /* already attached */
3390                 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3391                         continue; /* no substreams assigned */
3392
3393                 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3394                 if (dev < 0)
3395                         continue; /* no fatal error */
3396                 cpcm->device = dev;
3397                 err =  snd_hda_attach_pcm_stream(bus, codec, cpcm);
3398                 if (err < 0) {
3399                         codec_err(codec,
3400                                   "cannot attach PCM stream %d for codec #%d\n",
3401                                   dev, codec->core.addr);
3402                         continue; /* no fatal error */
3403                 }
3404         }
3405
3406         return 0;
3407 }
3408
3409 /**
3410  * snd_hda_add_new_ctls - create controls from the array
3411  * @codec: the HDA codec
3412  * @knew: the array of struct snd_kcontrol_new
3413  *
3414  * This helper function creates and add new controls in the given array.
3415  * The array must be terminated with an empty entry as terminator.
3416  *
3417  * Returns 0 if successful, or a negative error code.
3418  */
3419 int snd_hda_add_new_ctls(struct hda_codec *codec,
3420                          const struct snd_kcontrol_new *knew)
3421 {
3422         int err;
3423
3424         for (; knew->name; knew++) {
3425                 struct snd_kcontrol *kctl;
3426                 int addr = 0, idx = 0;
3427                 if (knew->iface == -1)  /* skip this codec private value */
3428                         continue;
3429                 for (;;) {
3430                         kctl = snd_ctl_new1(knew, codec);
3431                         if (!kctl)
3432                                 return -ENOMEM;
3433                         if (addr > 0)
3434                                 kctl->id.device = addr;
3435                         if (idx > 0)
3436                                 kctl->id.index = idx;
3437                         err = snd_hda_ctl_add(codec, 0, kctl);
3438                         if (!err)
3439                                 break;
3440                         /* try first with another device index corresponding to
3441                          * the codec addr; if it still fails (or it's the
3442                          * primary codec), then try another control index
3443                          */
3444                         if (!addr && codec->core.addr)
3445                                 addr = codec->core.addr;
3446                         else if (!idx && !knew->index) {
3447                                 idx = find_empty_mixer_ctl_idx(codec,
3448                                                                knew->name, 0);
3449                                 if (idx <= 0)
3450                                         return err;
3451                         } else
3452                                 return err;
3453                 }
3454         }
3455         return 0;
3456 }
3457 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3458
3459 #ifdef CONFIG_PM
3460 static void codec_set_power_save(struct hda_codec *codec, int delay)
3461 {
3462         struct device *dev = hda_codec_dev(codec);
3463
3464         if (delay == 0 && codec->auto_runtime_pm)
3465                 delay = 3000;
3466
3467         if (delay > 0) {
3468                 pm_runtime_set_autosuspend_delay(dev, delay);
3469                 pm_runtime_use_autosuspend(dev);
3470                 pm_runtime_allow(dev);
3471                 if (!pm_runtime_suspended(dev))
3472                         pm_runtime_mark_last_busy(dev);
3473         } else {
3474                 pm_runtime_dont_use_autosuspend(dev);
3475                 pm_runtime_forbid(dev);
3476         }
3477 }
3478
3479 /**
3480  * snd_hda_set_power_save - reprogram autosuspend for the given delay
3481  * @bus: HD-audio bus
3482  * @delay: autosuspend delay in msec, 0 = off
3483  *
3484  * Synchronize the runtime PM autosuspend state from the power_save option.
3485  */
3486 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3487 {
3488         struct hda_codec *c;
3489
3490         list_for_each_codec(c, bus)
3491                 codec_set_power_save(c, delay);
3492 }
3493 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3494
3495 /**
3496  * snd_hda_check_amp_list_power - Check the amp list and update the power
3497  * @codec: HD-audio codec
3498  * @check: the object containing an AMP list and the status
3499  * @nid: NID to check / update
3500  *
3501  * Check whether the given NID is in the amp list.  If it's in the list,
3502  * check the current AMP status, and update the power-status according
3503  * to the mute status.
3504  *
3505  * This function is supposed to be set or called from the check_power_status
3506  * patch ops.
3507  */
3508 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3509                                  struct hda_loopback_check *check,
3510                                  hda_nid_t nid)
3511 {
3512         const struct hda_amp_list *p;
3513         int ch, v;
3514
3515         if (!check->amplist)
3516                 return 0;
3517         for (p = check->amplist; p->nid; p++) {
3518                 if (p->nid == nid)
3519                         break;
3520         }
3521         if (!p->nid)
3522                 return 0; /* nothing changed */
3523
3524         for (p = check->amplist; p->nid; p++) {
3525                 for (ch = 0; ch < 2; ch++) {
3526                         v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3527                                                    p->idx);
3528                         if (!(v & HDA_AMP_MUTE) && v > 0) {
3529                                 if (!check->power_on) {
3530                                         check->power_on = 1;
3531                                         snd_hda_power_up_pm(codec);
3532                                 }
3533                                 return 1;
3534                         }
3535                 }
3536         }
3537         if (check->power_on) {
3538                 check->power_on = 0;
3539                 snd_hda_power_down_pm(codec);
3540         }
3541         return 0;
3542 }
3543 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3544 #endif
3545
3546 /*
3547  * input MUX helper
3548  */
3549
3550 /**
3551  * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3552  * @imux: imux helper object
3553  * @uinfo: pointer to get/store the data
3554  */
3555 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3556                            struct snd_ctl_elem_info *uinfo)
3557 {
3558         unsigned int index;
3559
3560         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3561         uinfo->count = 1;
3562         uinfo->value.enumerated.items = imux->num_items;
3563         if (!imux->num_items)
3564                 return 0;
3565         index = uinfo->value.enumerated.item;
3566         if (index >= imux->num_items)
3567                 index = imux->num_items - 1;
3568         strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3569         return 0;
3570 }
3571 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3572
3573 /**
3574  * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3575  * @codec: the HDA codec
3576  * @imux: imux helper object
3577  * @ucontrol: pointer to get/store the data
3578  * @nid: input mux NID
3579  * @cur_val: pointer to get/store the current imux value
3580  */
3581 int snd_hda_input_mux_put(struct hda_codec *codec,
3582                           const struct hda_input_mux *imux,
3583                           struct snd_ctl_elem_value *ucontrol,
3584                           hda_nid_t nid,
3585                           unsigned int *cur_val)
3586 {
3587         unsigned int idx;
3588
3589         if (!imux->num_items)
3590                 return 0;
3591         idx = ucontrol->value.enumerated.item[0];
3592         if (idx >= imux->num_items)
3593                 idx = imux->num_items - 1;
3594         if (*cur_val == idx)
3595                 return 0;
3596         snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3597                                   imux->items[idx].index);
3598         *cur_val = idx;
3599         return 1;
3600 }
3601 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3602
3603
3604 /**
3605  * snd_hda_enum_helper_info - Helper for simple enum ctls
3606  * @kcontrol: ctl element
3607  * @uinfo: pointer to get/store the data
3608  * @num_items: number of enum items
3609  * @texts: enum item string array
3610  *
3611  * process kcontrol info callback of a simple string enum array
3612  * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3613  */
3614 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3615                              struct snd_ctl_elem_info *uinfo,
3616                              int num_items, const char * const *texts)
3617 {
3618         static const char * const texts_default[] = {
3619                 "Disabled", "Enabled"
3620         };
3621
3622         if (!texts || !num_items) {
3623                 num_items = 2;
3624                 texts = texts_default;
3625         }
3626
3627         return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3628 }
3629 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3630
3631 /*
3632  * Multi-channel / digital-out PCM helper functions
3633  */
3634
3635 /* setup SPDIF output stream */
3636 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3637                                  unsigned int stream_tag, unsigned int format)
3638 {
3639         struct hda_spdif_out *spdif;
3640         unsigned int curr_fmt;
3641         bool reset;
3642
3643         spdif = snd_hda_spdif_out_of_nid(codec, nid);
3644         /* Add sanity check to pass klockwork check.
3645          * This should never happen.
3646          */
3647         if (WARN_ON(spdif == NULL))
3648                 return;
3649
3650         curr_fmt = snd_hda_codec_read(codec, nid, 0,
3651                                       AC_VERB_GET_STREAM_FORMAT, 0);
3652         reset = codec->spdif_status_reset &&
3653                 (spdif->ctls & AC_DIG1_ENABLE) &&
3654                 curr_fmt != format;
3655
3656         /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3657            updated */
3658         if (reset)
3659                 set_dig_out_convert(codec, nid,
3660                                     spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3661                                     -1);
3662         snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3663         if (codec->slave_dig_outs) {
3664                 const hda_nid_t *d;
3665                 for (d = codec->slave_dig_outs; *d; d++)
3666                         snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3667                                                    format);
3668         }
3669         /* turn on again (if needed) */
3670         if (reset)
3671                 set_dig_out_convert(codec, nid,
3672                                     spdif->ctls & 0xff, -1);
3673 }
3674
3675 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3676 {
3677         snd_hda_codec_cleanup_stream(codec, nid);
3678         if (codec->slave_dig_outs) {
3679                 const hda_nid_t *d;
3680                 for (d = codec->slave_dig_outs; *d; d++)
3681                         snd_hda_codec_cleanup_stream(codec, *d);
3682         }
3683 }
3684
3685 /**
3686  * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3687  * @codec: the HDA codec
3688  * @mout: hda_multi_out object
3689  */
3690 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3691                                struct hda_multi_out *mout)
3692 {
3693         mutex_lock(&codec->spdif_mutex);
3694         if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3695                 /* already opened as analog dup; reset it once */
3696                 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3697         mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3698         mutex_unlock(&codec->spdif_mutex);
3699         return 0;
3700 }
3701 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3702
3703 /**
3704  * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3705  * @codec: the HDA codec
3706  * @mout: hda_multi_out object
3707  * @stream_tag: stream tag to assign
3708  * @format: format id to assign
3709  * @substream: PCM substream to assign
3710  */
3711 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3712                                   struct hda_multi_out *mout,
3713                                   unsigned int stream_tag,
3714                                   unsigned int format,
3715                                   struct snd_pcm_substream *substream)
3716 {
3717         mutex_lock(&codec->spdif_mutex);
3718         setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3719         mutex_unlock(&codec->spdif_mutex);
3720         return 0;
3721 }
3722 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3723
3724 /**
3725  * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3726  * @codec: the HDA codec
3727  * @mout: hda_multi_out object
3728  */
3729 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3730                                   struct hda_multi_out *mout)
3731 {
3732         mutex_lock(&codec->spdif_mutex);
3733         cleanup_dig_out_stream(codec, mout->dig_out_nid);
3734         mutex_unlock(&codec->spdif_mutex);
3735         return 0;
3736 }
3737 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3738
3739 /**
3740  * snd_hda_multi_out_dig_close - release the digital out stream
3741  * @codec: the HDA codec
3742  * @mout: hda_multi_out object
3743  */
3744 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3745                                 struct hda_multi_out *mout)
3746 {
3747         mutex_lock(&codec->spdif_mutex);
3748         mout->dig_out_used = 0;
3749         mutex_unlock(&codec->spdif_mutex);
3750         return 0;
3751 }
3752 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3753
3754 /**
3755  * snd_hda_multi_out_analog_open - open analog outputs
3756  * @codec: the HDA codec
3757  * @mout: hda_multi_out object
3758  * @substream: PCM substream to assign
3759  * @hinfo: PCM information to assign
3760  *
3761  * Open analog outputs and set up the hw-constraints.
3762  * If the digital outputs can be opened as slave, open the digital
3763  * outputs, too.
3764  */
3765 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3766                                   struct hda_multi_out *mout,
3767                                   struct snd_pcm_substream *substream,
3768                                   struct hda_pcm_stream *hinfo)
3769 {
3770         struct snd_pcm_runtime *runtime = substream->runtime;
3771         runtime->hw.channels_max = mout->max_channels;
3772         if (mout->dig_out_nid) {
3773                 if (!mout->analog_rates) {
3774                         mout->analog_rates = hinfo->rates;
3775                         mout->analog_formats = hinfo->formats;
3776                         mout->analog_maxbps = hinfo->maxbps;
3777                 } else {
3778                         runtime->hw.rates = mout->analog_rates;
3779                         runtime->hw.formats = mout->analog_formats;
3780                         hinfo->maxbps = mout->analog_maxbps;
3781                 }
3782                 if (!mout->spdif_rates) {
3783                         snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3784                                                     &mout->spdif_rates,
3785                                                     &mout->spdif_formats,
3786                                                     &mout->spdif_maxbps);
3787                 }
3788                 mutex_lock(&codec->spdif_mutex);
3789                 if (mout->share_spdif) {
3790                         if ((runtime->hw.rates & mout->spdif_rates) &&
3791                             (runtime->hw.formats & mout->spdif_formats)) {
3792                                 runtime->hw.rates &= mout->spdif_rates;
3793                                 runtime->hw.formats &= mout->spdif_formats;
3794                                 if (mout->spdif_maxbps < hinfo->maxbps)
3795                                         hinfo->maxbps = mout->spdif_maxbps;
3796                         } else {
3797                                 mout->share_spdif = 0;
3798                                 /* FIXME: need notify? */
3799                         }
3800                 }
3801                 mutex_unlock(&codec->spdif_mutex);
3802         }
3803         return snd_pcm_hw_constraint_step(substream->runtime, 0,
3804                                           SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3805 }
3806 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3807
3808 /**
3809  * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3810  * @codec: the HDA codec
3811  * @mout: hda_multi_out object
3812  * @stream_tag: stream tag to assign
3813  * @format: format id to assign
3814  * @substream: PCM substream to assign
3815  *
3816  * Set up the i/o for analog out.
3817  * When the digital out is available, copy the front out to digital out, too.
3818  */
3819 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3820                                      struct hda_multi_out *mout,
3821                                      unsigned int stream_tag,
3822                                      unsigned int format,
3823                                      struct snd_pcm_substream *substream)
3824 {
3825         const hda_nid_t *nids = mout->dac_nids;
3826         int chs = substream->runtime->channels;
3827         struct hda_spdif_out *spdif;
3828         int i;
3829
3830         mutex_lock(&codec->spdif_mutex);
3831         spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3832         if (mout->dig_out_nid && mout->share_spdif &&
3833             mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3834                 if (chs == 2 && spdif != NULL &&
3835                     snd_hda_is_supported_format(codec, mout->dig_out_nid,
3836                                                 format) &&
3837                     !(spdif->status & IEC958_AES0_NONAUDIO)) {
3838                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3839                         setup_dig_out_stream(codec, mout->dig_out_nid,
3840                                              stream_tag, format);
3841                 } else {
3842                         mout->dig_out_used = 0;
3843                         cleanup_dig_out_stream(codec, mout->dig_out_nid);
3844                 }
3845         }
3846         mutex_unlock(&codec->spdif_mutex);
3847
3848         /* front */
3849         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3850                                    0, format);
3851         if (!mout->no_share_stream &&
3852             mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3853                 /* headphone out will just decode front left/right (stereo) */
3854                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3855                                            0, format);
3856         /* extra outputs copied from front */
3857         for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3858                 if (!mout->no_share_stream && mout->hp_out_nid[i])
3859                         snd_hda_codec_setup_stream(codec,
3860                                                    mout->hp_out_nid[i],
3861                                                    stream_tag, 0, format);
3862
3863         /* surrounds */
3864         for (i = 1; i < mout->num_dacs; i++) {
3865                 if (chs >= (i + 1) * 2) /* independent out */
3866                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3867                                                    i * 2, format);
3868                 else if (!mout->no_share_stream) /* copy front */
3869                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3870                                                    0, format);
3871         }
3872
3873         /* extra surrounds */
3874         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3875                 int ch = 0;
3876                 if (!mout->extra_out_nid[i])
3877                         break;
3878                 if (chs >= (i + 1) * 2)
3879                         ch = i * 2;
3880                 else if (!mout->no_share_stream)
3881                         break;
3882                 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3883                                            stream_tag, ch, format);
3884         }
3885
3886         return 0;
3887 }
3888 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3889
3890 /**
3891  * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3892  * @codec: the HDA codec
3893  * @mout: hda_multi_out object
3894  */
3895 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3896                                      struct hda_multi_out *mout)
3897 {
3898         const hda_nid_t *nids = mout->dac_nids;
3899         int i;
3900
3901         for (i = 0; i < mout->num_dacs; i++)
3902                 snd_hda_codec_cleanup_stream(codec, nids[i]);
3903         if (mout->hp_nid)
3904                 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3905         for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3906                 if (mout->hp_out_nid[i])
3907                         snd_hda_codec_cleanup_stream(codec,
3908                                                      mout->hp_out_nid[i]);
3909         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3910                 if (mout->extra_out_nid[i])
3911                         snd_hda_codec_cleanup_stream(codec,
3912                                                      mout->extra_out_nid[i]);
3913         mutex_lock(&codec->spdif_mutex);
3914         if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3915                 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3916                 mout->dig_out_used = 0;
3917         }
3918         mutex_unlock(&codec->spdif_mutex);
3919         return 0;
3920 }
3921 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3922
3923 /**
3924  * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3925  * @codec: the HDA codec
3926  * @pin: referred pin NID
3927  *
3928  * Guess the suitable VREF pin bits to be set as the pin-control value.
3929  * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3930  */
3931 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3932 {
3933         unsigned int pincap;
3934         unsigned int oldval;
3935         oldval = snd_hda_codec_read(codec, pin, 0,
3936                                     AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3937         pincap = snd_hda_query_pin_caps(codec, pin);
3938         pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3939         /* Exception: if the default pin setup is vref50, we give it priority */
3940         if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3941                 return AC_PINCTL_VREF_80;
3942         else if (pincap & AC_PINCAP_VREF_50)
3943                 return AC_PINCTL_VREF_50;
3944         else if (pincap & AC_PINCAP_VREF_100)
3945                 return AC_PINCTL_VREF_100;
3946         else if (pincap & AC_PINCAP_VREF_GRD)
3947                 return AC_PINCTL_VREF_GRD;
3948         return AC_PINCTL_VREF_HIZ;
3949 }
3950 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3951
3952 /**
3953  * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3954  * @codec: the HDA codec
3955  * @pin: referred pin NID
3956  * @val: pin ctl value to audit
3957  */
3958 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3959                                      hda_nid_t pin, unsigned int val)
3960 {
3961         static unsigned int cap_lists[][2] = {
3962                 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3963                 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3964                 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3965                 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3966         };
3967         unsigned int cap;
3968
3969         if (!val)
3970                 return 0;
3971         cap = snd_hda_query_pin_caps(codec, pin);
3972         if (!cap)
3973                 return val; /* don't know what to do... */
3974
3975         if (val & AC_PINCTL_OUT_EN) {
3976                 if (!(cap & AC_PINCAP_OUT))
3977                         val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3978                 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3979                         val &= ~AC_PINCTL_HP_EN;
3980         }
3981
3982         if (val & AC_PINCTL_IN_EN) {
3983                 if (!(cap & AC_PINCAP_IN))
3984                         val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3985                 else {
3986                         unsigned int vcap, vref;
3987                         int i;
3988                         vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3989                         vref = val & AC_PINCTL_VREFEN;
3990                         for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3991                                 if (vref == cap_lists[i][0] &&
3992                                     !(vcap & cap_lists[i][1])) {
3993                                         if (i == ARRAY_SIZE(cap_lists) - 1)
3994                                                 vref = AC_PINCTL_VREF_HIZ;
3995                                         else
3996                                                 vref = cap_lists[i + 1][0];
3997                                 }
3998                         }
3999                         val &= ~AC_PINCTL_VREFEN;
4000                         val |= vref;
4001                 }
4002         }
4003
4004         return val;
4005 }
4006 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
4007
4008 /**
4009  * _snd_hda_pin_ctl - Helper to set pin ctl value
4010  * @codec: the HDA codec
4011  * @pin: referred pin NID
4012  * @val: pin control value to set
4013  * @cached: access over codec pinctl cache or direct write
4014  *
4015  * This function is a helper to set a pin ctl value more safely.
4016  * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4017  * value in pin target array via snd_hda_codec_set_pin_target(), then
4018  * actually writes the value via either snd_hda_codec_update_cache() or
4019  * snd_hda_codec_write() depending on @cached flag.
4020  */
4021 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4022                          unsigned int val, bool cached)
4023 {
4024         val = snd_hda_correct_pin_ctl(codec, pin, val);
4025         snd_hda_codec_set_pin_target(codec, pin, val);
4026         if (cached)
4027                 return snd_hda_codec_update_cache(codec, pin, 0,
4028                                 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4029         else
4030                 return snd_hda_codec_write(codec, pin, 0,
4031                                            AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4032 }
4033 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
4034
4035 /**
4036  * snd_hda_add_imux_item - Add an item to input_mux
4037  * @codec: the HDA codec
4038  * @imux: imux helper object
4039  * @label: the name of imux item to assign
4040  * @index: index number of imux item to assign
4041  * @type_idx: pointer to store the resultant label index
4042  *
4043  * When the same label is used already in the existing items, the number
4044  * suffix is appended to the label.  This label index number is stored
4045  * to type_idx when non-NULL pointer is given.
4046  */
4047 int snd_hda_add_imux_item(struct hda_codec *codec,
4048                           struct hda_input_mux *imux, const char *label,
4049                           int index, int *type_idx)
4050 {
4051         int i, label_idx = 0;
4052         if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4053                 codec_err(codec, "hda_codec: Too many imux items!\n");
4054                 return -EINVAL;
4055         }
4056         for (i = 0; i < imux->num_items; i++) {
4057                 if (!strncmp(label, imux->items[i].label, strlen(label)))
4058                         label_idx++;
4059         }
4060         if (type_idx)
4061                 *type_idx = label_idx;
4062         if (label_idx > 0)
4063                 snprintf(imux->items[imux->num_items].label,
4064                          sizeof(imux->items[imux->num_items].label),
4065                          "%s %d", label, label_idx);
4066         else
4067                 strlcpy(imux->items[imux->num_items].label, label,
4068                         sizeof(imux->items[imux->num_items].label));
4069         imux->items[imux->num_items].index = index;
4070         imux->num_items++;
4071         return 0;
4072 }
4073 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
4074
4075 /**
4076  * snd_hda_bus_reset_codecs - Reset the bus
4077  * @bus: HD-audio bus
4078  */
4079 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
4080 {
4081         struct hda_codec *codec;
4082
4083         list_for_each_codec(codec, bus) {
4084                 /* FIXME: maybe a better way needed for forced reset */
4085                 if (current_work() != &codec->jackpoll_work.work)
4086                         cancel_delayed_work_sync(&codec->jackpoll_work);
4087 #ifdef CONFIG_PM
4088                 if (hda_codec_is_power_on(codec)) {
4089                         hda_call_codec_suspend(codec);
4090                         hda_call_codec_resume(codec);
4091                 }
4092 #endif
4093         }
4094 }
4095
4096 /**
4097  * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4098  * @pcm: PCM caps bits
4099  * @buf: the string buffer to write
4100  * @buflen: the max buffer length
4101  *
4102  * used by hda_proc.c and hda_eld.c
4103  */
4104 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4105 {
4106         static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4107         int i, j;
4108
4109         for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4110                 if (pcm & (AC_SUPPCM_BITS_8 << i))
4111                         j += scnprintf(buf + j, buflen - j,  " %d", bits[i]);
4112
4113         buf[j] = '\0'; /* necessary when j == 0 */
4114 }
4115 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
4116
4117 MODULE_DESCRIPTION("HDA codec core");
4118 MODULE_LICENSE("GPL");