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

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

Version: ~ [ linux-5.6-rc3 ] ~ [ linux-5.5.6 ] ~ [ linux-5.4.22 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.106 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.171 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.214 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.214 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.82 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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

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

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

osdn.jp