blob: 497bd0f9dcda189d2648a1756874343f3c923069 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.h>
Takashi Iwaicc72da72015-02-19 16:00:22 +010029#include <linux/pm.h>
30#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010036#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020038#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020039#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020040#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai83012a72012-08-24 18:38:08 +020042#ifdef CONFIG_PM
Takashi Iwai7639a062015-03-03 10:07:24 +010043#define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
Takashi Iwaicc72da72015-02-19 16:00:22 +010044#define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaicb53c622007-08-10 17:21:45 +020046#else
Takashi Iwaid846b172012-11-24 11:58:24 +010047#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020048#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020049#endif
50
Takashi Iwai7639a062015-03-03 10:07:24 +010051#define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53#define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
55
Takashi Iwaid5191e52009-11-16 14:58:17 +010056/**
57 * snd_hda_get_jack_location - Give a location string of the jack
58 * @cfg: pin default config value
59 *
60 * Parse the pin default config value and returns the string of the
61 * jack location, e.g. "Rear", "Front", etc.
62 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040063const char *snd_hda_get_jack_location(u32 cfg)
64{
65 static char *bases[7] = {
66 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
67 };
68 static unsigned char specials_idx[] = {
69 0x07, 0x08,
70 0x17, 0x18, 0x19,
71 0x37, 0x38
72 };
73 static char *specials[] = {
74 "Rear Panel", "Drive Bar",
75 "Riser", "HDMI", "ATAPI",
76 "Mobile-In", "Mobile-Out"
77 };
78 int i;
79 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
80 if ((cfg & 0x0f) < 7)
81 return bases[cfg & 0x0f];
82 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
83 if (cfg == specials_idx[i])
84 return specials[i];
85 }
86 return "UNKNOWN";
87}
Takashi Iwai2698ea92013-12-18 07:45:52 +010088EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -040089
Takashi Iwaid5191e52009-11-16 14:58:17 +010090/**
91 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
92 * @cfg: pin default config value
93 *
94 * Parse the pin default config value and returns the string of the
95 * jack connectivity, i.e. external or internal connection.
96 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040097const char *snd_hda_get_jack_connectivity(u32 cfg)
98{
99 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
100
101 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
102}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100103EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400104
Takashi Iwaid5191e52009-11-16 14:58:17 +0100105/**
106 * snd_hda_get_jack_type - Give a type string of the jack
107 * @cfg: pin default config value
108 *
109 * Parse the pin default config value and returns the string of the
110 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
111 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400112const char *snd_hda_get_jack_type(u32 cfg)
113{
114 static char *jack_types[16] = {
115 "Line Out", "Speaker", "HP Out", "CD",
116 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
117 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200118 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400119 };
120
121 return jack_types[(cfg & AC_DEFCFG_DEVICE)
122 >> AC_DEFCFG_DEVICE_SHIFT];
123}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100124EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400125
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100126/*
Takashi Iwai05852442015-03-03 15:40:08 +0100127 * Send and receive a verb - passed to exec_verb override for hdac_device
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200128 */
Takashi Iwai05852442015-03-03 15:40:08 +0100129static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
130 unsigned int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200131{
Takashi Iwai05852442015-03-03 15:40:08 +0100132 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200133 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200134 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200135
Wu Fengguang6430aee2009-07-17 16:49:19 +0800136 if (cmd == ~0)
137 return -1;
138
Takashi Iwai8dd78332009-06-02 01:16:07 +0200139 again:
Takashi Iwai664c7152015-04-08 11:43:14 +0200140 snd_hda_power_up_pm(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100141 mutex_lock(&bus->core.cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200142 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
143 bus->no_response_fallback = 1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100144 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
145 cmd, res);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200146 bus->no_response_fallback = 0;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100147 mutex_unlock(&bus->core.cmd_mutex);
Takashi Iwai664c7152015-04-08 11:43:14 +0200148 snd_hda_power_down_pm(codec);
Takashi Iwaicad372f2015-03-25 17:57:00 +0100149 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200150 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100151 codec_dbg(codec,
152 "resetting BUS due to fatal communication error\n");
Takashi Iwai0a505752015-04-16 23:25:02 +0200153 snd_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200154 }
155 goto again;
156 }
157 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100158 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200159 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200160 return err;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
164 * snd_hda_codec_read - send a command and get the response
165 * @codec: the HDA codec
166 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200167 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @verb: the verb to send
169 * @parm: the parameter for the verb
170 *
171 * Send a single command and read the corresponding response.
172 *
173 * Returns the obtained response value, or -1 for an error.
174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200175unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200176 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int verb, unsigned int parm)
178{
Takashi Iwai7639a062015-03-03 10:07:24 +0100179 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200180 unsigned int res;
Takashi Iwai05852442015-03-03 15:40:08 +0100181 if (snd_hdac_exec_verb(&codec->core, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700182 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return res;
184}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100185EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/**
188 * snd_hda_codec_write - send a single command without waiting for response
189 * @codec: the HDA codec
190 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200191 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * @verb: the verb to send
193 * @parm: the parameter for the verb
194 *
195 * Send a single command without waiting for response.
196 *
197 * Returns 0 if successful, or a negative error code.
198 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200199int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
200 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Takashi Iwai7639a062015-03-03 10:07:24 +0100202 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwai05852442015-03-03 15:40:08 +0100203 return snd_hdac_exec_verb(&codec->core, cmd, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100205EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/**
208 * snd_hda_sequence_write - sequence writes
209 * @codec: the HDA codec
210 * @seq: VERB array to send
211 *
212 * Send the commands sequentially from the given array.
213 * The array must be terminated with NID=0.
214 */
215void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
216{
217 for (; seq->nid; seq++)
218 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
219}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100220EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100222/* connection list element */
223struct hda_conn_list {
224 struct list_head list;
225 int len;
226 hda_nid_t nid;
227 hda_nid_t conns[0];
228};
229
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200230/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100231static struct hda_conn_list *
232lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200233{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100234 struct hda_conn_list *p;
235 list_for_each_entry(p, &codec->conn_list, list) {
236 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200237 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200238 }
239 return NULL;
240}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200241
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100242static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
243 const hda_nid_t *list)
244{
245 struct hda_conn_list *p;
246
247 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
248 if (!p)
249 return -ENOMEM;
250 p->len = len;
251 p->nid = nid;
252 memcpy(p->conns, list, len * sizeof(hda_nid_t));
253 list_add(&p->list, &codec->conn_list);
254 return 0;
255}
256
257static void remove_conn_list(struct hda_codec *codec)
258{
259 while (!list_empty(&codec->conn_list)) {
260 struct hda_conn_list *p;
261 p = list_first_entry(&codec->conn_list, typeof(*p), list);
262 list_del(&p->list);
263 kfree(p);
264 }
265}
266
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200267/* read the connection and add to the cache */
268static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200269{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100270 hda_nid_t list[32];
271 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200272 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200273
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200274 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100275 if (len == -ENOSPC) {
276 len = snd_hda_get_num_raw_conns(codec, nid);
277 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
278 if (!result)
279 return -ENOMEM;
280 len = snd_hda_get_raw_connections(codec, nid, result, len);
281 }
282 if (len >= 0)
283 len = snd_hda_override_conn_list(codec, nid, len, result);
284 if (result != list)
285 kfree(result);
286 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200287}
Takashi Iwaidce20792011-06-24 14:10:28 +0200288
289/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100290 * snd_hda_get_conn_list - get connection list
291 * @codec: the HDA codec
292 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100293 * @listp: the pointer to store NID list
294 *
295 * Parses the connection list of the given widget and stores the pointer
296 * to the list of NIDs.
297 *
298 * Returns the number of connections, or a negative error code.
299 *
300 * Note that the returned pointer isn't protected against the list
301 * modification. If snd_hda_override_conn_list() might be called
302 * concurrently, protect with a mutex appropriately.
303 */
304int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
305 const hda_nid_t **listp)
306{
307 bool added = false;
308
309 for (;;) {
310 int err;
311 const struct hda_conn_list *p;
312
313 /* if the connection-list is already cached, read it */
314 p = lookup_conn_list(codec, nid);
315 if (p) {
316 if (listp)
317 *listp = p->conns;
318 return p->len;
319 }
320 if (snd_BUG_ON(added))
321 return -EINVAL;
322
323 err = read_and_add_raw_conns(codec, nid);
324 if (err < 0)
325 return err;
326 added = true;
327 }
328}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100329EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100330
331/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200332 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * @codec: the HDA codec
334 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200335 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * @max_conns: max. number of connections to store
337 *
338 * Parses the connection list of the given widget and stores the list
339 * of NIDs.
340 *
341 * Returns the number of connections, or a negative error code.
342 */
343int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200344 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200345{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346 const hda_nid_t *list;
347 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 if (len > 0 && conn_list) {
350 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100351 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200352 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200353 return -EINVAL;
354 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100355 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200356 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200357
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100358 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200359}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100360EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200363 * snd_hda_override_conn_list - add/modify the connection-list to cache
364 * @codec: the HDA codec
365 * @nid: NID to parse
366 * @len: number of connection list entries
367 * @list: the list of connection entries
368 *
369 * Add or modify the given connection-list to the cache. If the corresponding
370 * cache already exists, invalidate it and append a new one.
371 *
372 * Returns zero or a negative error code.
373 */
374int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
375 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100377 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200378
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100379 p = lookup_conn_list(codec, nid);
380 if (p) {
381 list_del(&p->list);
382 kfree(p);
383 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200384
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100385 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100387EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200388
389/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200390 * snd_hda_get_conn_index - get the connection index of the given NID
391 * @codec: the HDA codec
392 * @mux: NID containing the list
393 * @nid: NID to select
394 * @recursive: 1 when searching NID recursively, otherwise 0
395 *
396 * Parses the connection list of the widget @mux and checks whether the
397 * widget @nid is present. If it is, return the connection index.
398 * Otherwise it returns -1.
399 */
400int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
401 hda_nid_t nid, int recursive)
402{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100403 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200404 int i, nums;
405
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100406 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200407 for (i = 0; i < nums; i++)
408 if (conn[i] == nid)
409 return i;
410 if (!recursive)
411 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100412 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100413 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200414 return -1;
415 }
416 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200417 for (i = 0; i < nums; i++) {
418 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
419 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
420 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200421 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
422 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200423 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200424 return -1;
425}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100426EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Mengdong Linf1aa0682013-08-26 21:35:21 -0400428
429/* return DEVLIST_LEN parameter of the given widget */
430static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
431{
432 unsigned int wcaps = get_wcaps(codec, nid);
433 unsigned int parm;
434
435 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
436 get_wcaps_type(wcaps) != AC_WID_PIN)
437 return 0;
438
Dave Airlie132bd962015-06-09 13:39:31 +1000439 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
Takashi Iwai86548442015-06-09 07:22:26 +0200440 if (parm == -1)
Mengdong Linf1aa0682013-08-26 21:35:21 -0400441 parm = 0;
442 return parm & AC_DEV_LIST_LEN_MASK;
443}
444
445/**
446 * snd_hda_get_devices - copy device list without cache
447 * @codec: the HDA codec
448 * @nid: NID of the pin to parse
449 * @dev_list: device list array
450 * @max_devices: max. number of devices to store
451 *
452 * Copy the device list. This info is dynamic and so not cached.
453 * Currently called only from hda_proc.c, so not exported.
454 */
455int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
456 u8 *dev_list, int max_devices)
457{
458 unsigned int parm;
459 int i, dev_len, devices;
460
461 parm = get_num_devices(codec, nid);
462 if (!parm) /* not multi-stream capable */
463 return 0;
464
465 dev_len = parm + 1;
466 dev_len = dev_len < max_devices ? dev_len : max_devices;
467
468 devices = 0;
469 while (devices < dev_len) {
Takashi Iwaicad372f2015-03-25 17:57:00 +0100470 if (snd_hdac_read(&codec->core, nid,
471 AC_VERB_GET_DEVICE_LIST, devices, &parm))
472 break; /* error */
Mengdong Linf1aa0682013-08-26 21:35:21 -0400473
474 for (i = 0; i < 8; i++) {
475 dev_list[devices] = (u8)parm;
476 parm >>= 4;
477 devices++;
478 if (devices >= dev_len)
479 break;
480 }
481 }
482 return devices;
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100486 * read widget caps for each widget and store in cache
487 */
488static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
489{
490 int i;
491 hda_nid_t nid;
492
Takashi Iwai7639a062015-03-03 10:07:24 +0100493 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200494 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100495 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100496 nid = codec->core.start_nid;
497 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100498 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
499 nid, AC_PAR_AUDIO_WIDGET_CAP);
Takashi Iwai54d17402005-11-21 16:33:22 +0100500 return 0;
501}
502
Takashi Iwai3be14142009-02-20 14:11:16 +0100503/* read all pin default configurations and save codec->init_pins */
504static int read_pin_defaults(struct hda_codec *codec)
505{
Takashi Iwai7639a062015-03-03 10:07:24 +0100506 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100507
Takashi Iwai7639a062015-03-03 10:07:24 +0100508 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100509 struct hda_pincfg *pin;
510 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200511 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100512 if (wid_type != AC_WID_PIN)
513 continue;
514 pin = snd_array_new(&codec->init_pins);
515 if (!pin)
516 return -ENOMEM;
517 pin->nid = nid;
518 pin->cfg = snd_hda_codec_read(codec, nid, 0,
519 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200520 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
521 AC_VERB_GET_PIN_WIDGET_CONTROL,
522 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100523 }
524 return 0;
525}
526
527/* look up the given pin config list and return the item matching with NID */
528static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
529 struct snd_array *array,
530 hda_nid_t nid)
531{
532 int i;
533 for (i = 0; i < array->used; i++) {
534 struct hda_pincfg *pin = snd_array_elem(array, i);
535 if (pin->nid == nid)
536 return pin;
537 }
538 return NULL;
539}
540
Takashi Iwai3be14142009-02-20 14:11:16 +0100541/* set the current pin config value for the given NID.
542 * the value is cached, and read via snd_hda_codec_get_pincfg()
543 */
544int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
545 hda_nid_t nid, unsigned int cfg)
546{
547 struct hda_pincfg *pin;
548
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200549 /* the check below may be invalid when pins are added by a fixup
550 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
551 * for now
552 */
553 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100554 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
555 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200556 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100557
Takashi Iwai3be14142009-02-20 14:11:16 +0100558 pin = look_up_pincfg(codec, list, nid);
559 if (!pin) {
560 pin = snd_array_new(list);
561 if (!pin)
562 return -ENOMEM;
563 pin->nid = nid;
564 }
565 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100566 return 0;
567}
568
Takashi Iwaid5191e52009-11-16 14:58:17 +0100569/**
570 * snd_hda_codec_set_pincfg - Override a pin default configuration
571 * @codec: the HDA codec
572 * @nid: NID to set the pin config
573 * @cfg: the pin default config value
574 *
575 * Override a pin default configuration value in the cache.
576 * This value can be read by snd_hda_codec_get_pincfg() in a higher
577 * priority than the real hardware value.
578 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100579int snd_hda_codec_set_pincfg(struct hda_codec *codec,
580 hda_nid_t nid, unsigned int cfg)
581{
Takashi Iwai346ff702009-02-23 09:42:57 +0100582 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100583}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100584EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100585
Takashi Iwaid5191e52009-11-16 14:58:17 +0100586/**
587 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
588 * @codec: the HDA codec
589 * @nid: NID to get the pin config
590 *
591 * Get the current pin config value of the given pin NID.
592 * If the pincfg value is cached or overridden via sysfs or driver,
593 * returns the cached value.
594 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100595unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
596{
597 struct hda_pincfg *pin;
598
Takashi Iwai648a8d22014-02-25 10:38:13 +0100599#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100600 {
601 unsigned int cfg = 0;
602 mutex_lock(&codec->user_mutex);
603 pin = look_up_pincfg(codec, &codec->user_pins, nid);
604 if (pin)
605 cfg = pin->cfg;
606 mutex_unlock(&codec->user_mutex);
607 if (cfg)
608 return cfg;
609 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100610#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100611 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
612 if (pin)
613 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100614 pin = look_up_pincfg(codec, &codec->init_pins, nid);
615 if (pin)
616 return pin->cfg;
617 return 0;
618}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100619EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100620
Takashi Iwai95a962c2014-10-29 16:03:58 +0100621/**
622 * snd_hda_codec_set_pin_target - remember the current pinctl target value
623 * @codec: the HDA codec
624 * @nid: pin NID
625 * @val: assigned pinctl value
626 *
627 * This function stores the given value to a pinctl target value in the
628 * pincfg table. This isn't always as same as the actually written value
629 * but can be referred at any time via snd_hda_codec_get_pin_target().
630 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100631int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
632 unsigned int val)
633{
634 struct hda_pincfg *pin;
635
636 pin = look_up_pincfg(codec, &codec->init_pins, nid);
637 if (!pin)
638 return -EINVAL;
639 pin->target = val;
640 return 0;
641}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100642EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100643
Takashi Iwai95a962c2014-10-29 16:03:58 +0100644/**
645 * snd_hda_codec_get_pin_target - return the current pinctl target value
646 * @codec: the HDA codec
647 * @nid: pin NID
648 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100649int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
650{
651 struct hda_pincfg *pin;
652
653 pin = look_up_pincfg(codec, &codec->init_pins, nid);
654 if (!pin)
655 return 0;
656 return pin->target;
657}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100658EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100659
Takashi Iwai92ee6162009-12-27 11:18:59 +0100660/**
661 * snd_hda_shutup_pins - Shut up all pins
662 * @codec: the HDA codec
663 *
664 * Clear all pin controls to shup up before suspend for avoiding click noise.
665 * The controls aren't cached so that they can be resumed properly.
666 */
667void snd_hda_shutup_pins(struct hda_codec *codec)
668{
669 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200670 /* don't shut up pins when unloading the driver; otherwise it breaks
671 * the default pin setup at the next load of the driver
672 */
673 if (codec->bus->shutdown)
674 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100675 for (i = 0; i < codec->init_pins.used; i++) {
676 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
677 /* use read here for syncing after issuing each verb */
678 snd_hda_codec_read(codec, pin->nid, 0,
679 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
680 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200681 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100682}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100683EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100684
Takashi Iwai2a439522011-07-26 09:52:50 +0200685#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200686/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
687static void restore_shutup_pins(struct hda_codec *codec)
688{
689 int i;
690 if (!codec->pins_shutup)
691 return;
692 if (codec->bus->shutdown)
693 return;
694 for (i = 0; i < codec->init_pins.used; i++) {
695 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
696 snd_hda_codec_write(codec, pin->nid, 0,
697 AC_VERB_SET_PIN_WIDGET_CONTROL,
698 pin->ctrl);
699 }
700 codec->pins_shutup = 0;
701}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700702#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200703
David Henningsson26a6cb62012-10-09 15:04:21 +0200704static void hda_jackpoll_work(struct work_struct *work)
705{
706 struct hda_codec *codec =
707 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200708
709 snd_hda_jack_set_dirty_all(codec);
710 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400711
712 if (!codec->jackpoll_interval)
713 return;
714
Takashi Iwai2f35c632015-02-27 22:43:26 +0100715 schedule_delayed_work(&codec->jackpoll_work,
716 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200717}
718
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100719/* release all pincfg lists */
720static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100721{
Takashi Iwai346ff702009-02-23 09:42:57 +0100722 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100723#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100724 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100725#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100726 snd_array_free(&codec->init_pins);
727}
728
Takashi Iwai54d17402005-11-21 16:33:22 +0100729/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200730 * audio-converter setup caches
731 */
732struct hda_cvt_setup {
733 hda_nid_t nid;
734 u8 stream_tag;
735 u8 channel_id;
736 u16 format_id;
737 unsigned char active; /* cvt is currently used */
738 unsigned char dirty; /* setups should be cleared */
739};
740
741/* get or create a cache entry for the given audio converter NID */
742static struct hda_cvt_setup *
743get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
744{
745 struct hda_cvt_setup *p;
746 int i;
747
748 for (i = 0; i < codec->cvt_setups.used; i++) {
749 p = snd_array_elem(&codec->cvt_setups, i);
750 if (p->nid == nid)
751 return p;
752 }
753 p = snd_array_new(&codec->cvt_setups);
754 if (p)
755 p->nid = nid;
756 return p;
757}
758
759/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100760 * PCM device
761 */
762static void release_pcm(struct kref *kref)
763{
764 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
765
766 if (pcm->pcm)
767 snd_device_free(pcm->codec->card, pcm->pcm);
768 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
769 kfree(pcm->name);
770 kfree(pcm);
771}
772
773void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
774{
775 kref_put(&pcm->kref, release_pcm);
776}
777EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
778
779struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
780 const char *fmt, ...)
781{
782 struct hda_pcm *pcm;
783 va_list args;
784
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100785 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
786 if (!pcm)
787 return NULL;
788
789 pcm->codec = codec;
790 kref_init(&pcm->kref);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200791 va_start(args, fmt);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100792 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200793 va_end(args);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100794 if (!pcm->name) {
795 kfree(pcm);
796 return NULL;
797 }
798
799 list_add_tail(&pcm->list, &codec->pcm_list_head);
800 return pcm;
801}
802EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
803
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100804/*
805 * codec destructor
806 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100807static void codec_release_pcms(struct hda_codec *codec)
808{
809 struct hda_pcm *pcm, *n;
810
811 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
812 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100813 if (pcm->pcm)
814 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100815 snd_hda_codec_pcm_put(pcm);
816 }
817}
818
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100819void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
820{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100821 if (codec->registered) {
822 /* pm_runtime_put() is called in snd_hdac_device_exit() */
823 pm_runtime_get_noresume(hda_codec_dev(codec));
824 pm_runtime_disable(hda_codec_dev(codec));
825 codec->registered = 0;
826 }
827
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100828 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100829 if (!codec->in_freeing)
830 snd_hda_ctls_clear(codec);
831 codec_release_pcms(codec);
832 snd_hda_detach_beep_device(codec);
833 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
834 snd_hda_jack_tbl_clear(codec);
835 codec->proc_widget_hook = NULL;
836 codec->spec = NULL;
837
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100838 /* free only driver_pins so that init_pins + user_pins are restored */
839 snd_array_free(&codec->driver_pins);
840 snd_array_free(&codec->cvt_setups);
841 snd_array_free(&codec->spdif_out);
842 snd_array_free(&codec->verbs);
843 codec->preset = NULL;
844 codec->slave_dig_outs = NULL;
845 codec->spdif_status_reset = 0;
846 snd_array_free(&codec->mixers);
847 snd_array_free(&codec->nids);
848 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100849 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100850}
851
Takashi Iwaid8193872012-08-31 07:54:38 -0700852static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100853 unsigned int power_state);
854
Takashi Iwaic4c25332015-03-03 17:22:12 +0100855/* also called from hda_bind.c */
856void snd_hda_codec_register(struct hda_codec *codec)
857{
858 if (codec->registered)
859 return;
860 if (device_is_registered(hda_codec_dev(codec))) {
861 snd_hda_register_beep_device(codec);
Mengdong Lina5e7e072015-04-29 17:43:20 +0800862 snd_hdac_link_power(&codec->core, true);
Takashi Iwaic4c25332015-03-03 17:22:12 +0100863 pm_runtime_enable(hda_codec_dev(codec));
864 /* it was powered up in snd_hda_codec_new(), now all done */
865 snd_hda_power_down(codec);
866 codec->registered = 1;
867 }
868}
869
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100870static int snd_hda_codec_dev_register(struct snd_device *device)
871{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100872 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100873 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100874}
875
876static int snd_hda_codec_dev_disconnect(struct snd_device *device)
877{
878 struct hda_codec *codec = device->device_data;
879
Takashi Iwaid604b392014-02-28 13:42:09 +0100880 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100881 return 0;
882}
883
Takashi Iwai2565c892014-02-19 11:41:09 +0100884static int snd_hda_codec_dev_free(struct snd_device *device)
885{
Takashi Iwaid56db742015-02-27 23:25:35 +0100886 struct hda_codec *codec = device->device_data;
887
888 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100889 snd_hdac_device_unregister(&codec->core);
Takashi Iwaia4577822015-06-11 14:02:49 +0200890 snd_hdac_link_power(&codec->core, false);
Takashi Iwaid56db742015-02-27 23:25:35 +0100891 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100892 return 0;
893}
894
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100895static void snd_hda_codec_dev_release(struct device *dev)
896{
Takashi Iwaid56db742015-02-27 23:25:35 +0100897 struct hda_codec *codec = dev_to_hda_codec(dev);
898
899 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100900 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100901 snd_hda_sysfs_clear(codec);
Takashi Iwaid56db742015-02-27 23:25:35 +0100902 kfree(codec->modelname);
903 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +0100904 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907/**
908 * snd_hda_codec_new - create a HDA codec
909 * @bus: the bus to assign
910 * @codec_addr: the codec address
911 * @codecp: the pointer to store the generated codec
912 *
913 * Returns 0 if successful, or a negative error code.
914 */
Takashi Iwai6efdd852015-02-27 16:09:22 +0100915int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
916 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200919 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -0700920 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +0100922 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100923 .dev_register = snd_hda_codec_dev_register,
924 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +0100925 .dev_free = snd_hda_codec_dev_free,
926 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Takashi Iwaida3cec32008-08-08 17:12:14 +0200928 if (snd_BUG_ON(!bus))
929 return -EINVAL;
930 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
931 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200933 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100934 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Takashi Iwai7639a062015-03-03 10:07:24 +0100937 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
938 err = snd_hdac_device_init(&codec->core, &bus->core, component,
939 codec_addr);
940 if (err < 0) {
941 kfree(codec);
942 return err;
943 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100944
Takashi Iwai7639a062015-03-03 10:07:24 +0100945 codec->core.dev.release = snd_hda_codec_dev_release;
946 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai05852442015-03-03 15:40:08 +0100947 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +0100950 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100952 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +0800953 mutex_init(&codec->control_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100954 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
955 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +0100956 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +0100957 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +0200958 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -0600959 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +0200960 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100961 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100962 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100963 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100964
David Henningsson26a6cb62012-10-09 15:04:21 +0200965 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -0500966 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +0200967 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Takashi Iwai83012a72012-08-24 18:38:08 +0200969#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +0100970 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200971#endif
972
Takashi Iwai648a8d22014-02-25 10:38:13 +0100973 snd_hda_sysfs_init(codec);
974
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200975 if (codec->bus->modelname) {
976 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
977 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100978 err = -ENODEV;
979 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200980 }
981 }
982
Takashi Iwai7639a062015-03-03 10:07:24 +0100983 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -0700984 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100985 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +0100986 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +0100987 err = read_pin_defaults(codec);
988 if (err < 0)
989 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +0100990
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100991 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -0700992 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100993
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200994 snd_hda_codec_proc_new(codec);
995
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200996 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200997
Takashi Iwai7639a062015-03-03 10:07:24 +0100998 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
999 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001000 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001001
Takashi Iwai6efdd852015-02-27 16:09:22 +01001002 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001003 if (err < 0)
1004 goto error;
1005
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001006 if (codecp)
1007 *codecp = codec;
1008 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001009
1010 error:
Takashi Iwaid56db742015-02-27 23:25:35 +01001011 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +01001012 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001013}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001014EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001015
Takashi Iwai95a962c2014-10-29 16:03:58 +01001016/**
1017 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1018 * @codec: the HDA codec
1019 *
1020 * Forcibly refresh the all widget caps and the init pin configurations of
1021 * the given codec.
1022 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001023int snd_hda_codec_update_widgets(struct hda_codec *codec)
1024{
1025 hda_nid_t fg;
1026 int err;
1027
Takashi Iwai7639a062015-03-03 10:07:24 +01001028 err = snd_hdac_refresh_widgets(&codec->core);
1029 if (err < 0)
1030 return err;
1031
Mengdong Lina15d05d2013-02-08 17:09:31 -05001032 /* Assume the function group node does not change,
1033 * only the widget nodes may change.
1034 */
1035 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +01001036 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001037 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001038 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001039 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001040
1041 snd_array_free(&codec->init_pins);
1042 err = read_pin_defaults(codec);
1043
1044 return err;
1045}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001046EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001047
Takashi Iwaied360812012-08-08 17:12:52 +02001048/* update the stream-id if changed */
1049static void update_pcm_stream_id(struct hda_codec *codec,
1050 struct hda_cvt_setup *p, hda_nid_t nid,
1051 u32 stream_tag, int channel_id)
1052{
1053 unsigned int oldval, newval;
1054
1055 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1056 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1057 newval = (stream_tag << 4) | channel_id;
1058 if (oldval != newval)
1059 snd_hda_codec_write(codec, nid, 0,
1060 AC_VERB_SET_CHANNEL_STREAMID,
1061 newval);
1062 p->stream_tag = stream_tag;
1063 p->channel_id = channel_id;
1064 }
1065}
1066
1067/* update the format-id if changed */
1068static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1069 hda_nid_t nid, int format)
1070{
1071 unsigned int oldval;
1072
1073 if (p->format_id != format) {
1074 oldval = snd_hda_codec_read(codec, nid, 0,
1075 AC_VERB_GET_STREAM_FORMAT, 0);
1076 if (oldval != format) {
1077 msleep(1);
1078 snd_hda_codec_write(codec, nid, 0,
1079 AC_VERB_SET_STREAM_FORMAT,
1080 format);
1081 }
1082 p->format_id = format;
1083 }
1084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086/**
1087 * snd_hda_codec_setup_stream - set up the codec for streaming
1088 * @codec: the CODEC to set up
1089 * @nid: the NID to set up
1090 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1091 * @channel_id: channel id to pass, zero based.
1092 * @format: stream format.
1093 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001094void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1095 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 int channel_id, int format)
1097{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001098 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001099 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001100 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001101 int i;
1102
Takashi Iwai0ba21762007-04-16 11:29:14 +02001103 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001104 return;
1105
Takashi Iwai4e76a882014-02-25 12:21:03 +01001106 codec_dbg(codec,
1107 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1108 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001109 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001110 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001111 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001112
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001113 if (codec->patch_ops.stream_pm)
1114 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001115 if (codec->pcm_format_first)
1116 update_pcm_format(codec, p, nid, format);
1117 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1118 if (!codec->pcm_format_first)
1119 update_pcm_format(codec, p, nid, format);
1120
Takashi Iwaieb541332010-08-06 13:48:11 +02001121 p->active = 1;
1122 p->dirty = 0;
1123
1124 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001125 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001126 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001127 for (i = 0; i < c->cvt_setups.used; i++) {
1128 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001129 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001130 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001131 p->dirty = 1;
1132 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001135EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Takashi Iwaif0cea792010-08-13 11:56:53 +02001137static void really_cleanup_stream(struct hda_codec *codec,
1138 struct hda_cvt_setup *q);
1139
Takashi Iwaid5191e52009-11-16 14:58:17 +01001140/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001141 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001142 * @codec: the CODEC to clean up
1143 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001144 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001145 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001146void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1147 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001148{
Takashi Iwaieb541332010-08-06 13:48:11 +02001149 struct hda_cvt_setup *p;
1150
Takashi Iwai888afa12008-03-18 09:57:50 +01001151 if (!nid)
1152 return;
1153
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001154 if (codec->no_sticky_stream)
1155 do_now = 1;
1156
Takashi Iwai4e76a882014-02-25 12:21:03 +01001157 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001158 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001159 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001160 /* here we just clear the active flag when do_now isn't set;
1161 * actual clean-ups will be done later in
1162 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1163 */
1164 if (do_now)
1165 really_cleanup_stream(codec, p);
1166 else
1167 p->active = 0;
1168 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001169}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001170EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001171
Takashi Iwaieb541332010-08-06 13:48:11 +02001172static void really_cleanup_stream(struct hda_codec *codec,
1173 struct hda_cvt_setup *q)
1174{
1175 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001176 if (q->stream_tag || q->channel_id)
1177 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1178 if (q->format_id)
1179 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1180);
Takashi Iwaieb541332010-08-06 13:48:11 +02001181 memset(q, 0, sizeof(*q));
1182 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001183 if (codec->patch_ops.stream_pm)
1184 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001185}
1186
1187/* clean up the all conflicting obsolete streams */
1188static void purify_inactive_streams(struct hda_codec *codec)
1189{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001190 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001191 int i;
1192
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001193 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001194 for (i = 0; i < c->cvt_setups.used; i++) {
1195 struct hda_cvt_setup *p;
1196 p = snd_array_elem(&c->cvt_setups, i);
1197 if (p->dirty)
1198 really_cleanup_stream(c, p);
1199 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001200 }
1201}
1202
Takashi Iwai2a439522011-07-26 09:52:50 +02001203#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001204/* clean up all streams; called from suspend */
1205static void hda_cleanup_all_streams(struct hda_codec *codec)
1206{
1207 int i;
1208
1209 for (i = 0; i < codec->cvt_setups.used; i++) {
1210 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1211 if (p->stream_tag)
1212 really_cleanup_stream(codec, p);
1213 }
1214}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001215#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217/*
1218 * amp access functions
1219 */
1220
Takashi Iwaid5191e52009-11-16 14:58:17 +01001221/**
1222 * query_amp_caps - query AMP capabilities
1223 * @codec: the HD-auio codec
1224 * @nid: the NID to query
1225 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1226 *
1227 * Query AMP capabilities for the given widget and direction.
1228 * Returns the obtained capability bits.
1229 *
1230 * When cap bits have been already read, this doesn't read again but
1231 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001233u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001235 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1236 nid = codec->core.afg;
1237 return snd_hda_param_read(codec, nid,
1238 direction == HDA_OUTPUT ?
1239 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001241EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Takashi Iwaid5191e52009-11-16 14:58:17 +01001243/**
David Henningsson861a04e2014-09-23 10:38:17 +02001244 * snd_hda_check_amp_caps - query AMP capabilities
1245 * @codec: the HD-audio codec
1246 * @nid: the NID to query
1247 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001248 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001249 *
1250 * Check whether the widget has the given amp capability for the direction.
1251 */
1252bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1253 int dir, unsigned int bits)
1254{
1255 if (!nid)
1256 return false;
1257 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1258 if (query_amp_caps(codec, nid, dir) & bits)
1259 return true;
1260 return false;
1261}
1262EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1263
1264/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001265 * snd_hda_override_amp_caps - Override the AMP capabilities
1266 * @codec: the CODEC to clean up
1267 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001268 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001269 * @caps: the capability bits to set
1270 *
1271 * Override the cached AMP caps bits value by the given one.
1272 * This function is useful if the driver needs to adjust the AMP ranges,
1273 * e.g. limit to 0dB, etc.
1274 *
1275 * Returns zero if successful or a negative error code.
1276 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001277int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1278 unsigned int caps)
1279{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001280 unsigned int parm;
1281
1282 snd_hda_override_wcaps(codec, nid,
1283 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1284 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1285 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001286}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001287EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001288
Takashi Iwaid5191e52009-11-16 14:58:17 +01001289/**
1290 * snd_hda_codec_amp_stereo - update the AMP stereo values
1291 * @codec: HD-audio codec
1292 * @nid: NID to read the AMP value
1293 * @direction: #HDA_INPUT or #HDA_OUTPUT
1294 * @idx: the index value (only for input direction)
1295 * @mask: bit mask to set
1296 * @val: the bits value to set
1297 *
1298 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1299 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001300 */
1301int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1302 int direction, int idx, int mask, int val)
1303{
1304 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001305
1306 if (snd_BUG_ON(mask & ~0xff))
1307 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001308 for (ch = 0; ch < 2; ch++)
1309 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1310 idx, mask, val);
1311 return ret;
1312}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001313EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001314
Takashi Iwai95a962c2014-10-29 16:03:58 +01001315/**
1316 * snd_hda_codec_amp_init - initialize the AMP value
1317 * @codec: the HDA codec
1318 * @nid: NID to read the AMP value
1319 * @ch: channel (left=0 or right=1)
1320 * @dir: #HDA_INPUT or #HDA_OUTPUT
1321 * @idx: the index value (only for input direction)
1322 * @mask: bit mask to set
1323 * @val: the bits value to set
1324 *
1325 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001326 * the first access. If the amp was already initialized / updated beforehand,
1327 * this does nothing.
1328 */
1329int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1330 int dir, int idx, int mask, int val)
1331{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001332 int orig;
1333
1334 if (!codec->core.regmap)
1335 return -EINVAL;
1336 regcache_cache_only(codec->core.regmap, true);
1337 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1338 regcache_cache_only(codec->core.regmap, false);
1339 if (orig >= 0)
1340 return 0;
1341 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001342}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001343EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001344
Takashi Iwai95a962c2014-10-29 16:03:58 +01001345/**
1346 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1347 * @codec: the HDA codec
1348 * @nid: NID to read the AMP value
1349 * @dir: #HDA_INPUT or #HDA_OUTPUT
1350 * @idx: the index value (only for input direction)
1351 * @mask: bit mask to set
1352 * @val: the bits value to set
1353 *
1354 * Call snd_hda_codec_amp_init() for both stereo channels.
1355 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001356int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1357 int dir, int idx, int mask, int val)
1358{
1359 int ch, ret = 0;
1360
1361 if (snd_BUG_ON(mask & ~0xff))
1362 mask &= 0xff;
1363 for (ch = 0; ch < 2; ch++)
1364 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1365 idx, mask, val);
1366 return ret;
1367}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001368EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001369
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001370static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1371 unsigned int ofs)
1372{
1373 u32 caps = query_amp_caps(codec, nid, dir);
1374 /* get num steps */
1375 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1376 if (ofs < caps)
1377 caps -= ofs;
1378 return caps;
1379}
1380
Takashi Iwaid5191e52009-11-16 14:58:17 +01001381/**
1382 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001383 * @kcontrol: referred ctl element
1384 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001385 *
1386 * The control element is supposed to have the private_value field
1387 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1388 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001389int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1390 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1393 u16 nid = get_amp_nid(kcontrol);
1394 u8 chs = get_amp_channels(kcontrol);
1395 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001396 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001398 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1399 uinfo->count = chs == 3 ? 2 : 1;
1400 uinfo->value.integer.min = 0;
1401 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1402 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001403 codec_warn(codec,
1404 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1405 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return -EINVAL;
1407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return 0;
1409}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001410EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001412
1413static inline unsigned int
1414read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1415 int ch, int dir, int idx, unsigned int ofs)
1416{
1417 unsigned int val;
1418 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1419 val &= HDA_AMP_VOLMASK;
1420 if (val >= ofs)
1421 val -= ofs;
1422 else
1423 val = 0;
1424 return val;
1425}
1426
1427static inline int
1428update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1429 int ch, int dir, int idx, unsigned int ofs,
1430 unsigned int val)
1431{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001432 unsigned int maxval;
1433
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001434 if (val > 0)
1435 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001436 /* ofs = 0: raw max value */
1437 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001438 if (val > maxval)
1439 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001440 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1441 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001442}
1443
Takashi Iwaid5191e52009-11-16 14:58:17 +01001444/**
1445 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001446 * @kcontrol: ctl element
1447 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001448 *
1449 * The control element is supposed to have the private_value field
1450 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1451 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001452int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1453 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1456 hda_nid_t nid = get_amp_nid(kcontrol);
1457 int chs = get_amp_channels(kcontrol);
1458 int dir = get_amp_direction(kcontrol);
1459 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001460 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 long *valp = ucontrol->value.integer.value;
1462
1463 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001464 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001466 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001469EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Takashi Iwaid5191e52009-11-16 14:58:17 +01001471/**
1472 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001473 * @kcontrol: ctl element
1474 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001475 *
1476 * The control element is supposed to have the private_value field
1477 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1478 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001479int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1480 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1483 hda_nid_t nid = get_amp_nid(kcontrol);
1484 int chs = get_amp_channels(kcontrol);
1485 int dir = get_amp_direction(kcontrol);
1486 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001487 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 long *valp = ucontrol->value.integer.value;
1489 int change = 0;
1490
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001491 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001492 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001493 valp++;
1494 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001495 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001496 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return change;
1498}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001499EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Takashi Iwaid5191e52009-11-16 14:58:17 +01001501/**
1502 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001503 * @kcontrol: ctl element
1504 * @op_flag: operation flag
1505 * @size: byte size of input TLV
1506 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001507 *
1508 * The control element is supposed to have the private_value field
1509 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1510 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001511int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1512 unsigned int size, unsigned int __user *_tlv)
1513{
1514 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1515 hda_nid_t nid = get_amp_nid(kcontrol);
1516 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001517 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001518 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001519 u32 caps, val1, val2;
1520
1521 if (size < 4 * sizeof(unsigned int))
1522 return -ENOMEM;
1523 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001524 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1525 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001526 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001527 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001528 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001529 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001530 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001531 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1532 return -EFAULT;
1533 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1534 return -EFAULT;
1535 if (put_user(val1, _tlv + 2))
1536 return -EFAULT;
1537 if (put_user(val2, _tlv + 3))
1538 return -EFAULT;
1539 return 0;
1540}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001541EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001542
Takashi Iwaid5191e52009-11-16 14:58:17 +01001543/**
1544 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1545 * @codec: HD-audio codec
1546 * @nid: NID of a reference widget
1547 * @dir: #HDA_INPUT or #HDA_OUTPUT
1548 * @tlv: TLV data to be stored, at least 4 elements
1549 *
1550 * Set (static) TLV data for a virtual master volume using the AMP caps
1551 * obtained from the reference NID.
1552 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001553 */
1554void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1555 unsigned int *tlv)
1556{
1557 u32 caps;
1558 int nums, step;
1559
1560 caps = query_amp_caps(codec, nid, dir);
1561 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1562 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1563 step = (step + 1) * 25;
1564 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1565 tlv[1] = 2 * sizeof(unsigned int);
1566 tlv[2] = -nums * step;
1567 tlv[3] = step;
1568}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001569EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001570
1571/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001572static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001573find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001574{
1575 struct snd_ctl_elem_id id;
1576 memset(&id, 0, sizeof(id));
1577 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001578 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001579 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001580 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1581 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001582 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001583 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001584}
1585
Takashi Iwaid5191e52009-11-16 14:58:17 +01001586/**
1587 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1588 * @codec: HD-audio codec
1589 * @name: ctl id name string
1590 *
1591 * Get the control element with the given id string and IFACE_MIXER.
1592 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001593struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1594 const char *name)
1595{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001596 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001597}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001598EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001599
Takashi Iwaidcda5802012-10-12 17:24:51 +02001600static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001601 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001602{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001603 int i, idx;
1604 /* 16 ctlrs should be large enough */
1605 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1606 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001607 return idx;
1608 }
1609 return -EBUSY;
1610}
1611
Takashi Iwaid5191e52009-11-16 14:58:17 +01001612/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001613 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001614 * @codec: HD-audio codec
1615 * @nid: corresponding NID (optional)
1616 * @kctl: the control element to assign
1617 *
1618 * Add the given control element to an array inside the codec instance.
1619 * All control elements belonging to a codec are supposed to be added
1620 * by this function so that a proper clean-up works at the free or
1621 * reconfiguration time.
1622 *
1623 * If non-zero @nid is passed, the NID is assigned to the control element.
1624 * The assignment is shown in the codec proc file.
1625 *
1626 * snd_hda_ctl_add() checks the control subdev id field whether
1627 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001628 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1629 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001630 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001631int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1632 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001633{
1634 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001635 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001636 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001637
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001638 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001639 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001640 if (nid == 0)
1641 nid = get_amp_nid_(kctl->private_value);
1642 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001643 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1644 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001645 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001646 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001647 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001648 if (err < 0)
1649 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001650 item = snd_array_new(&codec->mixers);
1651 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001652 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001653 item->kctl = kctl;
1654 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001655 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001656 return 0;
1657}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001658EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001659
Takashi Iwaid5191e52009-11-16 14:58:17 +01001660/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001661 * snd_hda_add_nid - Assign a NID to a control element
1662 * @codec: HD-audio codec
1663 * @nid: corresponding NID (optional)
1664 * @kctl: the control element to assign
1665 * @index: index to kctl
1666 *
1667 * Add the given control element to an array inside the codec instance.
1668 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1669 * NID:KCTL mapping - for example "Capture Source" selector.
1670 */
1671int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1672 unsigned int index, hda_nid_t nid)
1673{
1674 struct hda_nid_item *item;
1675
1676 if (nid > 0) {
1677 item = snd_array_new(&codec->nids);
1678 if (!item)
1679 return -ENOMEM;
1680 item->kctl = kctl;
1681 item->index = index;
1682 item->nid = nid;
1683 return 0;
1684 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001685 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1686 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001687 return -EINVAL;
1688}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001689EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001690
1691/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001692 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1693 * @codec: HD-audio codec
1694 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001695void snd_hda_ctls_clear(struct hda_codec *codec)
1696{
1697 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001698 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001699 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001700 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001701 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001702 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001703}
1704
Takashi Iwai95a962c2014-10-29 16:03:58 +01001705/**
1706 * snd_hda_lock_devices - pseudo device locking
1707 * @bus: the BUS
1708 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001709 * toggle card->shutdown to allow/disallow the device access (as a hack)
1710 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001711int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001712{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001713 struct snd_card *card = bus->card;
1714 struct hda_codec *codec;
1715
Takashi Iwaia65d6292009-02-23 16:57:04 +01001716 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001717 if (card->shutdown)
1718 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001719 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001720 if (!list_empty(&card->ctl_files))
1721 goto err_clear;
1722
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001723 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001724 struct hda_pcm *cpcm;
1725 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001726 if (!cpcm->pcm)
1727 continue;
1728 if (cpcm->pcm->streams[0].substream_opened ||
1729 cpcm->pcm->streams[1].substream_opened)
1730 goto err_clear;
1731 }
1732 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001733 spin_unlock(&card->files_lock);
1734 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001735
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001736 err_clear:
1737 card->shutdown = 0;
1738 err_unlock:
1739 spin_unlock(&card->files_lock);
1740 return -EINVAL;
1741}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001742EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001743
Takashi Iwai95a962c2014-10-29 16:03:58 +01001744/**
1745 * snd_hda_unlock_devices - pseudo device unlocking
1746 * @bus: the BUS
1747 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001748void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001749{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001750 struct snd_card *card = bus->card;
1751
Takashi Iwaia65d6292009-02-23 16:57:04 +01001752 spin_lock(&card->files_lock);
1753 card->shutdown = 0;
1754 spin_unlock(&card->files_lock);
1755}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001756EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001757
Takashi Iwaid5191e52009-11-16 14:58:17 +01001758/**
1759 * snd_hda_codec_reset - Clear all objects assigned to the codec
1760 * @codec: HD-audio codec
1761 *
1762 * This frees the all PCM and control elements assigned to the codec, and
1763 * clears the caches and restores the pin default configurations.
1764 *
1765 * When a device is being used, it returns -EBSY. If successfully freed,
1766 * returns zero.
1767 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001768int snd_hda_codec_reset(struct hda_codec *codec)
1769{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001770 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001771
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001772 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001773 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001774
1775 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01001776 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001777
Takashi Iwaia65d6292009-02-23 16:57:04 +01001778 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001779 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001780 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001781}
1782
Takashi Iwai6194b992014-06-06 18:12:16 +02001783typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001784
1785/* apply the function to all matching slave ctls in the mixer list */
1786static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001787 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001788{
1789 struct hda_nid_item *items;
1790 const char * const *s;
1791 int i, err;
1792
1793 items = codec->mixers.list;
1794 for (i = 0; i < codec->mixers.used; i++) {
1795 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01001796 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001797 continue;
1798 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01001799 char tmpname[sizeof(sctl->id.name)];
1800 const char *name = *s;
1801 if (suffix) {
1802 snprintf(tmpname, sizeof(tmpname), "%s %s",
1803 name, suffix);
1804 name = tmpname;
1805 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01001806 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001807 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001808 if (err)
1809 return err;
1810 break;
1811 }
1812 }
1813 }
1814 return 0;
1815}
1816
Takashi Iwai6194b992014-06-06 18:12:16 +02001817static int check_slave_present(struct hda_codec *codec,
1818 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001819{
1820 return 1;
1821}
1822
Takashi Iwai18478e82012-03-09 17:51:10 +01001823/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001824static int get_kctl_0dB_offset(struct hda_codec *codec,
1825 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01001826{
1827 int _tlv[4];
1828 const int *tlv = NULL;
1829 int val = -1;
1830
1831 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1832 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1833 mm_segment_t fs = get_fs();
1834 set_fs(get_ds());
1835 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1836 tlv = _tlv;
1837 set_fs(fs);
1838 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1839 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001840 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1841 int step = tlv[3];
1842 step &= ~TLV_DB_SCALE_MUTE;
1843 if (!step)
1844 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01001845 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001846 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01001847- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01001848 return -1;
1849 }
1850 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001851 val = -tlv[2] / step;
1852 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001853 return val;
1854}
1855
1856/* call kctl->put with the given value(s) */
1857static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1858{
1859 struct snd_ctl_elem_value *ucontrol;
1860 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1861 if (!ucontrol)
1862 return -ENOMEM;
1863 ucontrol->value.integer.value[0] = val;
1864 ucontrol->value.integer.value[1] = val;
1865 kctl->put(kctl, ucontrol);
1866 kfree(ucontrol);
1867 return 0;
1868}
1869
1870/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001871static int init_slave_0dB(struct hda_codec *codec,
1872 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001873{
Takashi Iwai6194b992014-06-06 18:12:16 +02001874 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01001875 if (offset > 0)
1876 put_kctl_with_value(slave, offset);
1877 return 0;
1878}
1879
1880/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02001881static int init_slave_unmute(struct hda_codec *codec,
1882 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001883{
1884 return put_kctl_with_value(slave, 1);
1885}
1886
Takashi Iwaie8750942014-06-30 14:02:39 +02001887static int add_slave(struct hda_codec *codec,
1888 void *data, struct snd_kcontrol *slave)
1889{
1890 return snd_ctl_add_slave(data, slave);
1891}
1892
Takashi Iwaid5191e52009-11-16 14:58:17 +01001893/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01001894 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01001895 * @codec: HD-audio codec
1896 * @name: vmaster control name
1897 * @tlv: TLV data (optional)
1898 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01001899 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01001900 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01001901 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01001902 *
1903 * Create a virtual master control with the given name. The TLV data
1904 * must be either NULL or a valid data.
1905 *
1906 * @slaves is a NULL-terminated array of strings, each of which is a
1907 * slave control name. All controls with these names are assigned to
1908 * the new virtual master control.
1909 *
1910 * This function returns zero if successful or a negative error code.
1911 */
Takashi Iwai18478e82012-03-09 17:51:10 +01001912int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001913 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01001914 const char *suffix, bool init_slave_vol,
1915 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001916{
1917 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001918 int err;
1919
Takashi Iwai29e58532012-03-12 12:25:03 +01001920 if (ctl_ret)
1921 *ctl_ret = NULL;
1922
Takashi Iwai9322ca52012-02-03 14:28:01 +01001923 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001924 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001925 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01001926 return 0;
1927 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001928 kctl = snd_ctl_make_virtual_master(name, tlv);
1929 if (!kctl)
1930 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001931 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001932 if (err < 0)
1933 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001934
Takashi Iwaie8750942014-06-30 14:02:39 +02001935 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001936 if (err < 0)
1937 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01001938
1939 /* init with master mute & zero volume */
1940 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01001941 if (init_slave_vol) {
1942 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01001943 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01001944 tlv ? init_slave_0dB : init_slave_unmute, &step);
1945 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001946
Takashi Iwai29e58532012-03-12 12:25:03 +01001947 if (ctl_ret)
1948 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001949 return 0;
1950}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001951EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001952
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001953/*
1954 * mute-LED control using vmaster
1955 */
1956static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_info *uinfo)
1958{
1959 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01001960 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001961 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001962
Takashi Iwai3ff72212014-10-20 18:17:28 +02001963 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001964}
1965
1966static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1967 struct snd_ctl_elem_value *ucontrol)
1968{
1969 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1970 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1971 return 0;
1972}
1973
1974static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1975 struct snd_ctl_elem_value *ucontrol)
1976{
1977 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1978 unsigned int old_mode = hook->mute_mode;
1979
1980 hook->mute_mode = ucontrol->value.enumerated.item[0];
1981 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1982 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1983 if (old_mode == hook->mute_mode)
1984 return 0;
1985 snd_hda_sync_vmaster_hook(hook);
1986 return 1;
1987}
1988
1989static struct snd_kcontrol_new vmaster_mute_mode = {
1990 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1991 .name = "Mute-LED Mode",
1992 .info = vmaster_mute_mode_info,
1993 .get = vmaster_mute_mode_get,
1994 .put = vmaster_mute_mode_put,
1995};
1996
Takashi Iwaiee52e562015-04-27 10:36:11 +02001997/* meta hook to call each driver's vmaster hook */
1998static void vmaster_hook(void *private_data, int enabled)
1999{
2000 struct hda_vmaster_mute_hook *hook = private_data;
2001
2002 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2003 enabled = hook->mute_mode;
2004 hook->hook(hook->codec, enabled);
2005}
2006
Takashi Iwai95a962c2014-10-29 16:03:58 +01002007/**
2008 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2009 * @codec: the HDA codec
2010 * @hook: the vmaster hook object
2011 * @expose_enum_ctl: flag to create an enum ctl
2012 *
2013 * Add a mute-LED hook with the given vmaster switch kctl.
2014 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2015 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002016 */
2017int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002018 struct hda_vmaster_mute_hook *hook,
2019 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002020{
2021 struct snd_kcontrol *kctl;
2022
2023 if (!hook->hook || !hook->sw_kctl)
2024 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002025 hook->codec = codec;
2026 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002027 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
Takashi Iwaif29735c2012-03-13 07:55:10 +01002028 if (!expose_enum_ctl)
2029 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002030 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2031 if (!kctl)
2032 return -ENOMEM;
2033 return snd_hda_ctl_add(codec, 0, kctl);
2034}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002035EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002036
Takashi Iwai95a962c2014-10-29 16:03:58 +01002037/**
2038 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2039 * @hook: the vmaster hook
2040 *
2041 * Call the hook with the current value for synchronization.
2042 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002043 */
2044void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2045{
2046 if (!hook->hook || !hook->codec)
2047 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002048 /* don't call vmaster hook in the destructor since it might have
2049 * been already destroyed
2050 */
2051 if (hook->codec->bus->shutdown)
2052 return;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002053 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002054}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002055EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002056
2057
Takashi Iwaid5191e52009-11-16 14:58:17 +01002058/**
2059 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002060 * @kcontrol: referred ctl element
2061 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002062 *
2063 * The control element is supposed to have the private_value field
2064 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2065 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002066int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 int chs = get_amp_channels(kcontrol);
2070
2071 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2072 uinfo->count = chs == 3 ? 2 : 1;
2073 uinfo->value.integer.min = 0;
2074 uinfo->value.integer.max = 1;
2075 return 0;
2076}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002077EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Takashi Iwaid5191e52009-11-16 14:58:17 +01002079/**
2080 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002081 * @kcontrol: ctl element
2082 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002083 *
2084 * The control element is supposed to have the private_value field
2085 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2086 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002087int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2088 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2091 hda_nid_t nid = get_amp_nid(kcontrol);
2092 int chs = get_amp_channels(kcontrol);
2093 int dir = get_amp_direction(kcontrol);
2094 int idx = get_amp_index(kcontrol);
2095 long *valp = ucontrol->value.integer.value;
2096
2097 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002098 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002099 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002101 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002102 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return 0;
2104}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002105EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Takashi Iwaid5191e52009-11-16 14:58:17 +01002107/**
2108 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002109 * @kcontrol: ctl element
2110 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002111 *
2112 * The control element is supposed to have the private_value field
2113 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2114 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002115int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2119 hda_nid_t nid = get_amp_nid(kcontrol);
2120 int chs = get_amp_channels(kcontrol);
2121 int dir = get_amp_direction(kcontrol);
2122 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 long *valp = ucontrol->value.integer.value;
2124 int change = 0;
2125
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002126 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002127 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2128 HDA_AMP_MUTE,
2129 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002130 valp++;
2131 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002132 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002133 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2134 HDA_AMP_MUTE,
2135 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002136 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return change;
2138}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002139EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141/*
Takashi Iwai985be542005-11-02 18:26:49 +01002142 * bound volume controls
2143 *
2144 * bind multiple volumes (# indices, from 0)
2145 */
2146
2147#define AMP_VAL_IDX_SHIFT 19
2148#define AMP_VAL_IDX_MASK (0x0f<<19)
2149
Takashi Iwaid5191e52009-11-16 14:58:17 +01002150/**
2151 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002152 * @kcontrol: ctl element
2153 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002154 *
2155 * The control element is supposed to have the private_value field
2156 * set up via HDA_BIND_MUTE*() macros.
2157 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002158int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2159 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002160{
2161 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2162 unsigned long pval;
2163 int err;
2164
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002165 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002166 pval = kcontrol->private_value;
2167 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2168 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2169 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002170 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002171 return err;
2172}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002173EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002174
Takashi Iwaid5191e52009-11-16 14:58:17 +01002175/**
2176 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002177 * @kcontrol: ctl element
2178 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002179 *
2180 * The control element is supposed to have the private_value field
2181 * set up via HDA_BIND_MUTE*() macros.
2182 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002183int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002185{
2186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2187 unsigned long pval;
2188 int i, indices, err = 0, change = 0;
2189
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002190 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002191 pval = kcontrol->private_value;
2192 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2193 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002194 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2195 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002196 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2197 if (err < 0)
2198 break;
2199 change |= err;
2200 }
2201 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002202 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002203 return err < 0 ? err : change;
2204}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002205EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002206
Takashi Iwaid5191e52009-11-16 14:58:17 +01002207/**
2208 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002209 * @kcontrol: referred ctl element
2210 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002211 *
2212 * The control element is supposed to have the private_value field
2213 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002214 */
2215int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_info *uinfo)
2217{
2218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2219 struct hda_bind_ctls *c;
2220 int err;
2221
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002222 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002223 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002224 kcontrol->private_value = *c->values;
2225 err = c->ops->info(kcontrol, uinfo);
2226 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002227 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002228 return err;
2229}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002230EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002231
Takashi Iwaid5191e52009-11-16 14:58:17 +01002232/**
2233 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002234 * @kcontrol: ctl element
2235 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002236 *
2237 * The control element is supposed to have the private_value field
2238 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2239 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002240int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
2243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2244 struct hda_bind_ctls *c;
2245 int err;
2246
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002247 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002248 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002249 kcontrol->private_value = *c->values;
2250 err = c->ops->get(kcontrol, ucontrol);
2251 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002252 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002253 return err;
2254}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002255EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002256
Takashi Iwaid5191e52009-11-16 14:58:17 +01002257/**
2258 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002259 * @kcontrol: ctl element
2260 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002261 *
2262 * The control element is supposed to have the private_value field
2263 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2264 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002265int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2266 struct snd_ctl_elem_value *ucontrol)
2267{
2268 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2269 struct hda_bind_ctls *c;
2270 unsigned long *vals;
2271 int err = 0, change = 0;
2272
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002273 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002274 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002275 for (vals = c->values; *vals; vals++) {
2276 kcontrol->private_value = *vals;
2277 err = c->ops->put(kcontrol, ucontrol);
2278 if (err < 0)
2279 break;
2280 change |= err;
2281 }
2282 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002283 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002284 return err < 0 ? err : change;
2285}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002286EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002287
Takashi Iwaid5191e52009-11-16 14:58:17 +01002288/**
2289 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002290 * @kcontrol: ctl element
2291 * @op_flag: operation flag
2292 * @size: byte size of input TLV
2293 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002294 *
2295 * The control element is supposed to have the private_value field
2296 * set up via HDA_BIND_VOL() macro.
2297 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002298int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2299 unsigned int size, unsigned int __user *tlv)
2300{
2301 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2302 struct hda_bind_ctls *c;
2303 int err;
2304
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002305 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002306 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002307 kcontrol->private_value = *c->values;
2308 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2309 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002310 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002311 return err;
2312}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002313EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002314
2315struct hda_ctl_ops snd_hda_bind_vol = {
2316 .info = snd_hda_mixer_amp_volume_info,
2317 .get = snd_hda_mixer_amp_volume_get,
2318 .put = snd_hda_mixer_amp_volume_put,
2319 .tlv = snd_hda_mixer_amp_tlv
2320};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002321EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002322
2323struct hda_ctl_ops snd_hda_bind_sw = {
2324 .info = snd_hda_mixer_amp_switch_info,
2325 .get = snd_hda_mixer_amp_switch_get,
2326 .put = snd_hda_mixer_amp_switch_put,
2327 .tlv = snd_hda_mixer_amp_tlv
2328};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002329EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002330
2331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 * SPDIF out controls
2333 */
2334
Takashi Iwai0ba21762007-04-16 11:29:14 +02002335static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
2338 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2339 uinfo->count = 1;
2340 return 0;
2341}
2342
Takashi Iwai0ba21762007-04-16 11:29:14 +02002343static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2347 IEC958_AES0_NONAUDIO |
2348 IEC958_AES0_CON_EMPHASIS_5015 |
2349 IEC958_AES0_CON_NOT_COPYRIGHT;
2350 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2351 IEC958_AES1_CON_ORIGINAL;
2352 return 0;
2353}
2354
Takashi Iwai0ba21762007-04-16 11:29:14 +02002355static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2356 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2359 IEC958_AES0_NONAUDIO |
2360 IEC958_AES0_PRO_EMPHASIS_5015;
2361 return 0;
2362}
2363
Takashi Iwai0ba21762007-04-16 11:29:14 +02002364static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2365 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002368 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002369 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002371 mutex_lock(&codec->spdif_mutex);
2372 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002373 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2374 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2375 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2376 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002377 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 return 0;
2380}
2381
2382/* convert from SPDIF status bits to HDA SPDIF bits
2383 * bit 0 (DigEn) is always set zero (to be filled later)
2384 */
2385static unsigned short convert_from_spdif_status(unsigned int sbits)
2386{
2387 unsigned short val = 0;
2388
2389 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002390 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002392 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002394 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2395 IEC958_AES0_PRO_EMPHASIS_5015)
2396 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002398 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2399 IEC958_AES0_CON_EMPHASIS_5015)
2400 val |= AC_DIG1_EMPHASIS;
2401 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2402 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002404 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2406 }
2407 return val;
2408}
2409
2410/* convert to SPDIF status bits from HDA SPDIF bits
2411 */
2412static unsigned int convert_to_spdif_status(unsigned short val)
2413{
2414 unsigned int sbits = 0;
2415
Takashi Iwai0ba21762007-04-16 11:29:14 +02002416 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002418 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 sbits |= IEC958_AES0_PROFESSIONAL;
2420 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002421 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2423 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002424 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002426 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002428 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2430 sbits |= val & (0x7f << 8);
2431 }
2432 return sbits;
2433}
2434
Takashi Iwai2f728532008-09-25 16:32:41 +02002435/* set digital convert verbs both for the given NID and its slaves */
2436static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia551d912015-02-26 12:34:49 +01002437 int mask, int val)
Takashi Iwai2f728532008-09-25 16:32:41 +02002438{
Takashi Iwaidda14412011-05-02 11:29:30 +02002439 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002440
Takashi Iwaia551d912015-02-26 12:34:49 +01002441 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2442 mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002443 d = codec->slave_dig_outs;
2444 if (!d)
2445 return;
2446 for (; *d; d++)
Takashi Iwai7d4b5e92015-04-16 15:14:53 +02002447 snd_hdac_regmap_update(&codec->core, *d,
Takashi Iwaia551d912015-02-26 12:34:49 +01002448 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002449}
2450
2451static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2452 int dig1, int dig2)
2453{
Takashi Iwaia551d912015-02-26 12:34:49 +01002454 unsigned int mask = 0;
2455 unsigned int val = 0;
2456
2457 if (dig1 != -1) {
2458 mask |= 0xff;
2459 val = dig1;
2460 }
2461 if (dig2 != -1) {
2462 mask |= 0xff00;
2463 val |= dig2 << 8;
2464 }
2465 set_dig_out(codec, nid, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002466}
2467
Takashi Iwai0ba21762007-04-16 11:29:14 +02002468static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002472 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002473 struct hda_spdif_out *spdif;
2474 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 unsigned short val;
2476 int change;
2477
Ingo Molnar62932df2006-01-16 16:34:20 +01002478 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002479 spdif = snd_array_elem(&codec->spdif_out, idx);
2480 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002481 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2483 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2484 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002485 val = convert_from_spdif_status(spdif->status);
2486 val |= spdif->ctls & 1;
2487 change = spdif->ctls != val;
2488 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002489 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002490 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002491 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return change;
2493}
2494
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002495#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Takashi Iwai0ba21762007-04-16 11:29:14 +02002497static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
2500 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002501 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002502 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002504 mutex_lock(&codec->spdif_mutex);
2505 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002506 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002507 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return 0;
2509}
2510
Stephen Warren74b654c2011-06-01 11:14:18 -06002511static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2512 int dig1, int dig2)
2513{
2514 set_dig_out_convert(codec, nid, dig1, dig2);
2515 /* unmute amp switch (if any) */
2516 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2517 (dig1 & AC_DIG1_ENABLE))
2518 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2519 HDA_AMP_MUTE, 0);
2520}
2521
Takashi Iwai0ba21762007-04-16 11:29:14 +02002522static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
2525 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002526 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002527 struct hda_spdif_out *spdif;
2528 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 unsigned short val;
2530 int change;
2531
Ingo Molnar62932df2006-01-16 16:34:20 +01002532 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002533 spdif = snd_array_elem(&codec->spdif_out, idx);
2534 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002535 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002537 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002538 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002539 spdif->ctls = val;
2540 if (change && nid != (u16)-1)
2541 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002542 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return change;
2544}
2545
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002546static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 {
2548 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2549 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002550 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 .info = snd_hda_spdif_mask_info,
2552 .get = snd_hda_spdif_cmask_get,
2553 },
2554 {
2555 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2556 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002557 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 .info = snd_hda_spdif_mask_info,
2559 .get = snd_hda_spdif_pmask_get,
2560 },
2561 {
2562 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002563 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 .info = snd_hda_spdif_mask_info,
2565 .get = snd_hda_spdif_default_get,
2566 .put = snd_hda_spdif_default_put,
2567 },
2568 {
2569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002570 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 .info = snd_hda_spdif_out_switch_info,
2572 .get = snd_hda_spdif_out_switch_get,
2573 .put = snd_hda_spdif_out_switch_put,
2574 },
2575 { } /* end */
2576};
2577
2578/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002579 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002581 * @associated_nid: NID that new ctls associated with
2582 * @cvt_nid: converter NID
2583 * @type: HDA_PCM_TYPE_*
2584 * Creates controls related with the digital output.
2585 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 *
2587 * Returns 0 if successful, or a negative error code.
2588 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002589int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2590 hda_nid_t associated_nid,
2591 hda_nid_t cvt_nid,
2592 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
2594 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002595 struct snd_kcontrol *kctl;
2596 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002597 int idx = 0;
Takashi Iwaia551d912015-02-26 12:34:49 +01002598 int val = 0;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002599 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06002600 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002601 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002603 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002604 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002605 idx = spdif_index;
2606 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002607 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002608 /* suppose a single SPDIF device */
2609 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2610 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2611 if (!kctl)
2612 break;
2613 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002614 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002615 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002616 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002617 if (!bus->primary_dig_out_type)
2618 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002619
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002620 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002621 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002622 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002623 return -EBUSY;
2624 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002625 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002626 if (!spdif)
2627 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2629 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002630 if (!kctl)
2631 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002632 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002633 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002634 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002635 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return err;
2637 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002638 spdif->nid = cvt_nid;
Takashi Iwaia551d912015-02-26 12:34:49 +01002639 snd_hdac_regmap_read(&codec->core, cvt_nid,
2640 AC_VERB_GET_DIGI_CONVERT_1, &val);
2641 spdif->ctls = val;
Stephen Warren7c9359762011-06-01 11:14:17 -06002642 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return 0;
2644}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002645EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Takashi Iwai95a962c2014-10-29 16:03:58 +01002647/**
2648 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2649 * @codec: the HDA codec
2650 * @nid: widget NID
2651 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002652 * call within spdif_mutex lock
2653 */
Stephen Warren7c9359762011-06-01 11:14:17 -06002654struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2655 hda_nid_t nid)
2656{
2657 int i;
2658 for (i = 0; i < codec->spdif_out.used; i++) {
2659 struct hda_spdif_out *spdif =
2660 snd_array_elem(&codec->spdif_out, i);
2661 if (spdif->nid == nid)
2662 return spdif;
2663 }
2664 return NULL;
2665}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002666EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06002667
Takashi Iwai95a962c2014-10-29 16:03:58 +01002668/**
2669 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2670 * @codec: the HDA codec
2671 * @idx: the SPDIF ctl index
2672 *
2673 * Unassign the widget from the given SPDIF control.
2674 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002675void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2676{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002677 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002678
2679 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002680 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002681 spdif->nid = (u16)-1;
2682 mutex_unlock(&codec->spdif_mutex);
2683}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002684EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002685
Takashi Iwai95a962c2014-10-29 16:03:58 +01002686/**
2687 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2688 * @codec: the HDA codec
2689 * @idx: the SPDIF ctl idx
2690 * @nid: widget NID
2691 *
2692 * Assign the widget to the SPDIF control with the given index.
2693 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002694void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2695{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002696 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002697 unsigned short val;
2698
2699 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002700 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002701 if (spdif->nid != nid) {
2702 spdif->nid = nid;
2703 val = spdif->ctls;
2704 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2705 }
2706 mutex_unlock(&codec->spdif_mutex);
2707}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002708EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002711 * SPDIF sharing with analog output
2712 */
2713static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2714 struct snd_ctl_elem_value *ucontrol)
2715{
2716 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2717 ucontrol->value.integer.value[0] = mout->share_spdif;
2718 return 0;
2719}
2720
2721static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2722 struct snd_ctl_elem_value *ucontrol)
2723{
2724 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2725 mout->share_spdif = !!ucontrol->value.integer.value[0];
2726 return 0;
2727}
2728
2729static struct snd_kcontrol_new spdif_share_sw = {
2730 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2731 .name = "IEC958 Default PCM Playback Switch",
2732 .info = snd_ctl_boolean_mono_info,
2733 .get = spdif_share_sw_get,
2734 .put = spdif_share_sw_put,
2735};
2736
Takashi Iwaid5191e52009-11-16 14:58:17 +01002737/**
2738 * snd_hda_create_spdif_share_sw - create Default PCM switch
2739 * @codec: the HDA codec
2740 * @mout: multi-out instance
2741 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002742int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2743 struct hda_multi_out *mout)
2744{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002745 struct snd_kcontrol *kctl;
2746
Takashi Iwai9a081602008-02-12 18:37:26 +01002747 if (!mout->dig_out_nid)
2748 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002749
2750 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2751 if (!kctl)
2752 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002753 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002754 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002755}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002756EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002757
2758/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 * SPDIF input
2760 */
2761
2762#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2763
Takashi Iwai0ba21762007-04-16 11:29:14 +02002764static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2768
2769 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2770 return 0;
2771}
2772
Takashi Iwai0ba21762007-04-16 11:29:14 +02002773static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2774 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
2776 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2777 hda_nid_t nid = kcontrol->private_value;
2778 unsigned int val = !!ucontrol->value.integer.value[0];
2779 int change;
2780
Ingo Molnar62932df2006-01-16 16:34:20 +01002781 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002783 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 codec->spdif_in_enable = val;
Takashi Iwaia551d912015-02-26 12:34:49 +01002785 snd_hdac_regmap_write(&codec->core, nid,
2786 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002788 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return change;
2790}
2791
Takashi Iwai0ba21762007-04-16 11:29:14 +02002792static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2796 hda_nid_t nid = kcontrol->private_value;
Takashi Iwaia551d912015-02-26 12:34:49 +01002797 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 unsigned int sbits;
2799
Takashi Iwaia551d912015-02-26 12:34:49 +01002800 snd_hdac_regmap_read(&codec->core, nid,
2801 AC_VERB_GET_DIGI_CONVERT_1, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 sbits = convert_to_spdif_status(val);
2803 ucontrol->value.iec958.status[0] = sbits;
2804 ucontrol->value.iec958.status[1] = sbits >> 8;
2805 ucontrol->value.iec958.status[2] = sbits >> 16;
2806 ucontrol->value.iec958.status[3] = sbits >> 24;
2807 return 0;
2808}
2809
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002810static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 {
2812 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002813 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 .info = snd_hda_spdif_in_switch_info,
2815 .get = snd_hda_spdif_in_switch_get,
2816 .put = snd_hda_spdif_in_switch_put,
2817 },
2818 {
2819 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2820 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002821 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 .info = snd_hda_spdif_mask_info,
2823 .get = snd_hda_spdif_in_status_get,
2824 },
2825 { } /* end */
2826};
2827
2828/**
2829 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2830 * @codec: the HDA codec
2831 * @nid: audio in widget NID
2832 *
2833 * Creates controls related with the SPDIF input.
2834 * Called from each patch supporting the SPDIF in.
2835 *
2836 * Returns 0 if successful, or a negative error code.
2837 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002838int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002841 struct snd_kcontrol *kctl;
2842 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002843 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Takashi Iwaidcda5802012-10-12 17:24:51 +02002845 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002846 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002847 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002848 return -EBUSY;
2849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2851 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002852 if (!kctl)
2853 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002855 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002856 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return err;
2858 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002859 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002860 snd_hda_codec_read(codec, nid, 0,
2861 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002862 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return 0;
2864}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002865EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Takashi Iwai95a962c2014-10-29 16:03:58 +01002867/**
2868 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2869 * @codec: the HDA codec
2870 * @fg: function group (not used now)
2871 * @power_state: the power state to set (AC_PWRST_*)
2872 *
2873 * Set the given power state to all widgets that have the power control.
2874 * If the codec has power_filter set, it evaluates the power state and
2875 * filter out if it's unchanged as D3.
2876 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002877void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002878 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01002879{
Takashi Iwai7639a062015-03-03 10:07:24 +01002880 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01002881
Takashi Iwai7639a062015-03-03 10:07:24 +01002882 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002883 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002884 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002885 if (!(wcaps & AC_WCAP_POWER))
2886 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002887 if (codec->power_filter) {
2888 state = codec->power_filter(codec, nid, power_state);
2889 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002890 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002891 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002892 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002893 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002894 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002895}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002896EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002897
2898/*
Takashi Iwai432c6412012-08-28 09:59:20 -07002899 * wait until the state is reached, returns the current state
2900 */
2901static unsigned int hda_sync_power_state(struct hda_codec *codec,
2902 hda_nid_t fg,
2903 unsigned int power_state)
2904{
2905 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2906 unsigned int state, actual_state;
2907
2908 for (;;) {
2909 state = snd_hda_codec_read(codec, fg, 0,
2910 AC_VERB_GET_POWER_STATE, 0);
2911 if (state & AC_PWRST_ERROR)
2912 break;
2913 actual_state = (state >> 4) & 0x0f;
2914 if (actual_state == power_state)
2915 break;
2916 if (time_after_eq(jiffies, end_time))
2917 break;
2918 /* wait until the codec reachs to the target state */
2919 msleep(1);
2920 }
2921 return state;
2922}
2923
Takashi Iwai95a962c2014-10-29 16:03:58 +01002924/**
2925 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2926 * @codec: the HDA codec
2927 * @nid: widget NID
2928 * @power_state: power state to evalue
2929 *
2930 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2931 * This can be used a codec power_filter callback.
2932 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01002933unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2934 hda_nid_t nid,
2935 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01002936{
Takashi Iwai7639a062015-03-03 10:07:24 +01002937 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002938 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002939 if (power_state == AC_PWRST_D3 &&
2940 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2941 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2942 int eapd = snd_hda_codec_read(codec, nid, 0,
2943 AC_VERB_GET_EAPD_BTLENABLE, 0);
2944 if (eapd & 0x02)
2945 return AC_PWRST_D0;
2946 }
2947 return power_state;
2948}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002949EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002950
Takashi Iwai432c6412012-08-28 09:59:20 -07002951/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002952 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002953 */
Takashi Iwaid8193872012-08-31 07:54:38 -07002954static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002955 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002956{
Takashi Iwai7639a062015-03-03 10:07:24 +01002957 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002958 int count;
2959 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02002960 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002961
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002962 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002963 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05002964 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01002965 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05002966 else if (codec->depop_delay > 0)
2967 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02002968 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002969 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08002970
2971 /* repeat power states setting at most 10 times*/
2972 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07002973 if (codec->patch_ops.set_power_state)
2974 codec->patch_ops.set_power_state(codec, fg,
2975 power_state);
2976 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002977 state = power_state;
2978 if (codec->power_filter)
2979 state = codec->power_filter(codec, fg, state);
2980 if (state == power_state || power_state != AC_PWRST_D3)
2981 snd_hda_codec_read(codec, fg, flags,
2982 AC_VERB_SET_POWER_STATE,
2983 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002984 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07002985 }
2986 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08002987 if (!(state & AC_PWRST_ERROR))
2988 break;
2989 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002990
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002991 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002992}
Takashi Iwai54d17402005-11-21 16:33:22 +01002993
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002994/* sync power states of all widgets;
2995 * this is called at the end of codec parsing
2996 */
2997static void sync_power_up_states(struct hda_codec *codec)
2998{
Takashi Iwai7639a062015-03-03 10:07:24 +01002999 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003000
Takashi Iwaiba615b82013-03-13 14:47:21 +01003001 /* don't care if no filter is used */
3002 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003003 return;
3004
Takashi Iwai7639a062015-03-03 10:07:24 +01003005 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003006 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003007 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003008 if (!(wcaps & AC_WCAP_POWER))
3009 continue;
3010 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3011 if (target == AC_PWRST_D0)
3012 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003013 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003014 snd_hda_codec_write(codec, nid, 0,
3015 AC_VERB_SET_POWER_STATE, target);
3016 }
3017}
3018
Takashi Iwai648a8d22014-02-25 10:38:13 +01003019#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003020/* execute additional init verbs */
3021static void hda_exec_init_verbs(struct hda_codec *codec)
3022{
3023 if (codec->init_verbs.list)
3024 snd_hda_sequence_write(codec, codec->init_verbs.list);
3025}
3026#else
3027static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3028#endif
3029
Takashi Iwai2a439522011-07-26 09:52:50 +02003030#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003031/* update the power on/off account with the current jiffies */
3032static void update_power_acct(struct hda_codec *codec, bool on)
3033{
3034 unsigned long delta = jiffies - codec->power_jiffies;
3035
3036 if (on)
3037 codec->power_on_acct += delta;
3038 else
3039 codec->power_off_acct += delta;
3040 codec->power_jiffies += delta;
3041}
3042
3043void snd_hda_update_power_acct(struct hda_codec *codec)
3044{
3045 update_power_acct(codec, hda_codec_is_power_on(codec));
3046}
3047
Takashi Iwaicb53c622007-08-10 17:21:45 +02003048/*
3049 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003050 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003051 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003052static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003053{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003054 unsigned int state;
3055
Takashi Iwai7639a062015-03-03 10:07:24 +01003056 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003057
Takashi Iwaicb53c622007-08-10 17:21:45 +02003058 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003059 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003060 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003061 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003062 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003063 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003064 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003065}
3066
3067/*
3068 * kick up codec; used both from PM and power-save
3069 */
3070static void hda_call_codec_resume(struct hda_codec *codec)
3071{
Takashi Iwai7639a062015-03-03 10:07:24 +01003072 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003073
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003074 if (codec->core.regmap)
3075 regcache_mark_dirty(codec->core.regmap);
3076
Takashi Iwaicc72da72015-02-19 16:00:22 +01003077 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003078
Takashi Iwaid8193872012-08-31 07:54:38 -07003079 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003080 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003081 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003082 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003083 if (codec->patch_ops.resume)
3084 codec->patch_ops.resume(codec);
3085 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003086 if (codec->patch_ops.init)
3087 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003088 if (codec->core.regmap)
3089 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003090 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003091
3092 if (codec->jackpoll_interval)
3093 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003094 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003095 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003096 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003097}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003098
Takashi Iwaicc72da72015-02-19 16:00:22 +01003099static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003100{
3101 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003102 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003103 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003104
3105 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003106 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3107 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003108 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003109 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3110 (state & AC_PWRST_CLK_STOP_OK))
3111 snd_hdac_codec_link_down(&codec->core);
Mengdong Lina5e7e072015-04-29 17:43:20 +08003112 snd_hdac_link_power(&codec->core, false);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003113 return 0;
3114}
3115
Takashi Iwaicc72da72015-02-19 16:00:22 +01003116static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003117{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003118 struct hda_codec *codec = dev_to_hda_codec(dev);
3119
Mengdong Lina5e7e072015-04-29 17:43:20 +08003120 snd_hdac_link_power(&codec->core, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003121 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003122 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003123 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003124 return 0;
3125}
Takashi Iwai2a439522011-07-26 09:52:50 +02003126#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003127
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003128/* referred in hda_bind.c */
3129const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003130 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3131 pm_runtime_force_resume)
3132 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3133 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003134};
Takashi Iwai54d17402005-11-21 16:33:22 +01003135
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003136/*
3137 * add standard channel maps if not specified
3138 */
3139static int add_std_chmaps(struct hda_codec *codec)
3140{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003141 struct hda_pcm *pcm;
3142 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003143
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003144 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003145 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003146 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003147 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003148 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003149
Sudip Mukherjee751e2212015-04-02 16:51:43 +05303150 if (!pcm || pcm->own_chmap ||
3151 !hinfo->substreams)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003152 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003153 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003154 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003155 hinfo->channels_max,
3156 0, &chmap);
3157 if (err < 0)
3158 return err;
3159 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3160 }
3161 }
3162 return 0;
3163}
3164
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003165/* default channel maps for 2.1 speakers;
3166 * since HD-audio supports only stereo, odd number channels are omitted
3167 */
3168const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3169 { .channels = 2,
3170 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3171 { .channels = 4,
3172 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3173 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3174 { }
3175};
3176EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3177
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003178int snd_hda_codec_build_controls(struct hda_codec *codec)
3179{
3180 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003181 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003182 /* continue to initialize... */
3183 if (codec->patch_ops.init)
3184 err = codec->patch_ops.init(codec);
3185 if (!err && codec->patch_ops.build_controls)
3186 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003187 if (err < 0)
3188 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003189
3190 /* we create chmaps here instead of build_pcms */
3191 err = add_std_chmaps(codec);
3192 if (err < 0)
3193 return err;
3194
David Henningsson26a6cb62012-10-09 15:04:21 +02003195 if (codec->jackpoll_interval)
3196 hda_jackpoll_work(&codec->jackpoll_work.work);
3197 else
3198 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003199 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 return 0;
3201}
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 * PCM stuff
3205 */
3206static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3207 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003208 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
3210 return 0;
3211}
3212
3213static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3214 struct hda_codec *codec,
3215 unsigned int stream_tag,
3216 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003217 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
3219 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3220 return 0;
3221}
3222
3223static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3224 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003225 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Takashi Iwai888afa12008-03-18 09:57:50 +01003227 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return 0;
3229}
3230
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003231static int set_pcm_default_values(struct hda_codec *codec,
3232 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003234 int err;
3235
Takashi Iwai0ba21762007-04-16 11:29:14 +02003236 /* query support PCM information from the given NID */
3237 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003238 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003239 info->rates ? NULL : &info->rates,
3240 info->formats ? NULL : &info->formats,
3241 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003242 if (err < 0)
3243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
3245 if (info->ops.open == NULL)
3246 info->ops.open = hda_pcm_default_open_close;
3247 if (info->ops.close == NULL)
3248 info->ops.close = hda_pcm_default_open_close;
3249 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003250 if (snd_BUG_ON(!info->nid))
3251 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 info->ops.prepare = hda_pcm_default_prepare;
3253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003255 if (snd_BUG_ON(!info->nid))
3256 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 info->ops.cleanup = hda_pcm_default_cleanup;
3258 }
3259 return 0;
3260}
3261
Takashi Iwaieb541332010-08-06 13:48:11 +02003262/*
3263 * codec prepare/cleanup entries
3264 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003265/**
3266 * snd_hda_codec_prepare - Prepare a stream
3267 * @codec: the HDA codec
3268 * @hinfo: PCM information
3269 * @stream: stream tag to assign
3270 * @format: format id to assign
3271 * @substream: PCM substream to assign
3272 *
3273 * Calls the prepare callback set by the codec with the given arguments.
3274 * Clean up the inactive streams when successful.
3275 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003276int snd_hda_codec_prepare(struct hda_codec *codec,
3277 struct hda_pcm_stream *hinfo,
3278 unsigned int stream,
3279 unsigned int format,
3280 struct snd_pcm_substream *substream)
3281{
3282 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003283 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003284 if (hinfo->ops.prepare)
3285 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3286 substream);
3287 else
3288 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02003289 if (ret >= 0)
3290 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003291 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003292 return ret;
3293}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003294EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02003295
Takashi Iwai95a962c2014-10-29 16:03:58 +01003296/**
3297 * snd_hda_codec_cleanup - Prepare a stream
3298 * @codec: the HDA codec
3299 * @hinfo: PCM information
3300 * @substream: PCM substream
3301 *
3302 * Calls the cleanup callback set by the codec with the given arguments.
3303 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003304void snd_hda_codec_cleanup(struct hda_codec *codec,
3305 struct hda_pcm_stream *hinfo,
3306 struct snd_pcm_substream *substream)
3307{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003308 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003309 if (hinfo->ops.cleanup)
3310 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003311 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003312}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003313EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02003314
Takashi Iwaid5191e52009-11-16 14:58:17 +01003315/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003316const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3317 "Audio", "SPDIF", "HDMI", "Modem"
3318};
3319
Takashi Iwai176d5332008-07-30 15:01:44 +02003320/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003321 * get the empty PCM device number to assign
3322 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003323static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003324{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003325 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003326 /* assigned to static slots up to dev#10; if more needed, assign
3327 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3328 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003329 static int audio_idx[HDA_PCM_NTYPES][5] = {
3330 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3331 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003332 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003333 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003334 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003335 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003336
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003337 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003338 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003339 return -EINVAL;
3340 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003341
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003342 for (i = 0; audio_idx[type][i] >= 0; i++) {
3343#ifndef CONFIG_SND_DYNAMIC_MINORS
3344 if (audio_idx[type][i] >= 8)
3345 break;
3346#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003347 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3348 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003349 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003350
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003351#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003352 /* non-fixed slots starting from 10 */
3353 for (i = 10; i < 32; i++) {
3354 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3355 return i;
3356 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003357#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003358
Takashi Iwai4e76a882014-02-25 12:21:03 +01003359 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003360 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003361#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01003362 dev_warn(bus->card->dev,
3363 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003364#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003365 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003366}
3367
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003368/* call build_pcms ops of the given codec and set up the default parameters */
3369int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02003370{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003371 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003372 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02003373
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003374 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003375 return 0; /* already parsed */
3376
3377 if (!codec->patch_ops.build_pcms)
3378 return 0;
3379
3380 err = codec->patch_ops.build_pcms(codec);
3381 if (err < 0) {
3382 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003383 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003384 return err;
3385 }
3386
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003387 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003388 int stream;
3389
3390 for (stream = 0; stream < 2; stream++) {
3391 struct hda_pcm_stream *info = &cpcm->stream[stream];
3392
3393 if (!info->substreams)
3394 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02003395 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003396 if (err < 0) {
3397 codec_warn(codec,
3398 "fail to setup default for PCM %s\n",
3399 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02003400 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003401 }
Takashi Iwai176d5332008-07-30 15:01:44 +02003402 }
3403 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003404
3405 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02003406}
3407
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003408/* assign all PCMs of the given codec */
3409int snd_hda_codec_build_pcms(struct hda_codec *codec)
3410{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003411 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003412 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003413 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003414
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003415 err = snd_hda_codec_parse_pcms(codec);
3416 if (err < 0) {
3417 snd_hda_codec_reset(codec);
3418 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003419 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003420
3421 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003422 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003423 if (cpcm->pcm)
3424 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003425 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003426 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003427
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003428 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3429 if (dev < 0)
3430 continue; /* no fatal error */
3431 cpcm->device = dev;
Takashi Iwai0a505752015-04-16 23:25:02 +02003432 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003433 if (err < 0) {
3434 codec_err(codec,
3435 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003436 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003437 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003438 }
3439 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003440
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003441 return 0;
3442}
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 * snd_hda_add_new_ctls - create controls from the array
3446 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003447 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 *
3449 * This helper function creates and add new controls in the given array.
3450 * The array must be terminated with an empty entry as terminator.
3451 *
3452 * Returns 0 if successful, or a negative error code.
3453 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003454int snd_hda_add_new_ctls(struct hda_codec *codec,
3455 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003457 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003460 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003461 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003462 if (knew->iface == -1) /* skip this codec private value */
3463 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003464 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003465 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003466 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003467 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003468 if (addr > 0)
3469 kctl->id.device = addr;
3470 if (idx > 0)
3471 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003472 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003473 if (!err)
3474 break;
3475 /* try first with another device index corresponding to
3476 * the codec addr; if it still fails (or it's the
3477 * primary codec), then try another control index
3478 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003479 if (!addr && codec->core.addr)
3480 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003481 else if (!idx && !knew->index) {
3482 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02003483 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003484 if (idx <= 0)
3485 return err;
3486 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003487 return err;
3488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 }
3490 return 0;
3491}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003492EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Takashi Iwai83012a72012-08-24 18:38:08 +02003494#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01003495static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01003496{
3497 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003498
Lu, Han2377c3c2015-06-09 16:50:38 +08003499 if (delay == 0 && codec->auto_runtime_pm)
3500 delay = 3000;
3501
Takashi Iwaicc72da72015-02-19 16:00:22 +01003502 if (delay > 0) {
3503 pm_runtime_set_autosuspend_delay(dev, delay);
3504 pm_runtime_use_autosuspend(dev);
3505 pm_runtime_allow(dev);
3506 if (!pm_runtime_suspended(dev))
3507 pm_runtime_mark_last_busy(dev);
3508 } else {
3509 pm_runtime_dont_use_autosuspend(dev);
3510 pm_runtime_forbid(dev);
3511 }
3512}
Takashi Iwaibb573922015-02-20 09:26:04 +01003513
3514/**
3515 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3516 * @bus: HD-audio bus
3517 * @delay: autosuspend delay in msec, 0 = off
3518 *
3519 * Synchronize the runtime PM autosuspend state from the power_save option.
3520 */
3521void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3522{
3523 struct hda_codec *c;
3524
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003525 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01003526 codec_set_power_save(c, delay);
3527}
3528EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003529
Takashi Iwaid5191e52009-11-16 14:58:17 +01003530/**
3531 * snd_hda_check_amp_list_power - Check the amp list and update the power
3532 * @codec: HD-audio codec
3533 * @check: the object containing an AMP list and the status
3534 * @nid: NID to check / update
3535 *
3536 * Check whether the given NID is in the amp list. If it's in the list,
3537 * check the current AMP status, and update the the power-status according
3538 * to the mute status.
3539 *
3540 * This function is supposed to be set or called from the check_power_status
3541 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003542 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003543int snd_hda_check_amp_list_power(struct hda_codec *codec,
3544 struct hda_loopback_check *check,
3545 hda_nid_t nid)
3546{
Takashi Iwai031024e2011-05-02 11:29:30 +02003547 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003548 int ch, v;
3549
3550 if (!check->amplist)
3551 return 0;
3552 for (p = check->amplist; p->nid; p++) {
3553 if (p->nid == nid)
3554 break;
3555 }
3556 if (!p->nid)
3557 return 0; /* nothing changed */
3558
3559 for (p = check->amplist; p->nid; p++) {
3560 for (ch = 0; ch < 2; ch++) {
3561 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3562 p->idx);
3563 if (!(v & HDA_AMP_MUTE) && v > 0) {
3564 if (!check->power_on) {
3565 check->power_on = 1;
Takashi Iwai664c7152015-04-08 11:43:14 +02003566 snd_hda_power_up_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003567 }
3568 return 1;
3569 }
3570 }
3571 }
3572 if (check->power_on) {
3573 check->power_on = 0;
Takashi Iwai664c7152015-04-08 11:43:14 +02003574 snd_hda_power_down_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003575 }
3576 return 0;
3577}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003578EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003579#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003581/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 * input MUX helper
3583 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003584
3585/**
3586 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003587 * @imux: imux helper object
3588 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003589 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003590int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3591 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 unsigned int index;
3594
3595 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3596 uinfo->count = 1;
3597 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003598 if (!imux->num_items)
3599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 index = uinfo->value.enumerated.item;
3601 if (index >= imux->num_items)
3602 index = imux->num_items - 1;
3603 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3604 return 0;
3605}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003606EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Takashi Iwaid5191e52009-11-16 14:58:17 +01003608/**
3609 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003610 * @codec: the HDA codec
3611 * @imux: imux helper object
3612 * @ucontrol: pointer to get/store the data
3613 * @nid: input mux NID
3614 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01003615 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003616int snd_hda_input_mux_put(struct hda_codec *codec,
3617 const struct hda_input_mux *imux,
3618 struct snd_ctl_elem_value *ucontrol,
3619 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 unsigned int *cur_val)
3621{
3622 unsigned int idx;
3623
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003624 if (!imux->num_items)
3625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 idx = ucontrol->value.enumerated.item[0];
3627 if (idx >= imux->num_items)
3628 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003629 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003631 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3632 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 *cur_val = idx;
3634 return 1;
3635}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003636EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003639/**
3640 * snd_hda_enum_helper_info - Helper for simple enum ctls
3641 * @kcontrol: ctl element
3642 * @uinfo: pointer to get/store the data
3643 * @num_items: number of enum items
3644 * @texts: enum item string array
3645 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01003646 * process kcontrol info callback of a simple string enum array
3647 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3648 */
3649int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3650 struct snd_ctl_elem_info *uinfo,
3651 int num_items, const char * const *texts)
3652{
3653 static const char * const texts_default[] = {
3654 "Disabled", "Enabled"
3655 };
3656
3657 if (!texts || !num_items) {
3658 num_items = 2;
3659 texts = texts_default;
3660 }
3661
Takashi Iwai3ff72212014-10-20 18:17:28 +02003662 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003663}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003664EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003665
3666/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 * Multi-channel / digital-out PCM helper functions
3668 */
3669
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003670/* setup SPDIF output stream */
3671static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3672 unsigned int stream_tag, unsigned int format)
3673{
Laurence Darby3bef1c32012-11-03 17:00:06 +00003674 struct hda_spdif_out *spdif;
3675 unsigned int curr_fmt;
3676 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06003677
Laurence Darby3bef1c32012-11-03 17:00:06 +00003678 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3679 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3680 AC_VERB_GET_STREAM_FORMAT, 0);
3681 reset = codec->spdif_status_reset &&
3682 (spdif->ctls & AC_DIG1_ENABLE) &&
3683 curr_fmt != format;
3684
3685 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3686 updated */
3687 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003688 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06003689 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02003690 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003691 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003692 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003693 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003694 for (d = codec->slave_dig_outs; *d; d++)
3695 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3696 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003697 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003698 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00003699 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02003700 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06003701 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02003702}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003703
Takashi Iwai2f728532008-09-25 16:32:41 +02003704static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3705{
3706 snd_hda_codec_cleanup_stream(codec, nid);
3707 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003708 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003709 for (d = codec->slave_dig_outs; *d; d++)
3710 snd_hda_codec_cleanup_stream(codec, *d);
3711 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003712}
3713
Takashi Iwaid5191e52009-11-16 14:58:17 +01003714/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003715 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003716 * @codec: the HDA codec
3717 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003719int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3720 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
Ingo Molnar62932df2006-01-16 16:34:20 +01003722 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003723 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3724 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003725 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003727 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 return 0;
3729}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003730EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Takashi Iwaid5191e52009-11-16 14:58:17 +01003732/**
3733 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003734 * @codec: the HDA codec
3735 * @mout: hda_multi_out object
3736 * @stream_tag: stream tag to assign
3737 * @format: format id to assign
3738 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003739 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003740int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3741 struct hda_multi_out *mout,
3742 unsigned int stream_tag,
3743 unsigned int format,
3744 struct snd_pcm_substream *substream)
3745{
3746 mutex_lock(&codec->spdif_mutex);
3747 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3748 mutex_unlock(&codec->spdif_mutex);
3749 return 0;
3750}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003751EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003752
Takashi Iwaid5191e52009-11-16 14:58:17 +01003753/**
3754 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003755 * @codec: the HDA codec
3756 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01003757 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003758int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3759 struct hda_multi_out *mout)
3760{
3761 mutex_lock(&codec->spdif_mutex);
3762 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3763 mutex_unlock(&codec->spdif_mutex);
3764 return 0;
3765}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003766EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01003767
Takashi Iwaid5191e52009-11-16 14:58:17 +01003768/**
3769 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003770 * @codec: the HDA codec
3771 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003773int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3774 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
Ingo Molnar62932df2006-01-16 16:34:20 +01003776 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003778 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 return 0;
3780}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003781EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Takashi Iwaid5191e52009-11-16 14:58:17 +01003783/**
3784 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003785 * @codec: the HDA codec
3786 * @mout: hda_multi_out object
3787 * @substream: PCM substream to assign
3788 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003789 *
3790 * Open analog outputs and set up the hw-constraints.
3791 * If the digital outputs can be opened as slave, open the digital
3792 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003794int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3795 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003796 struct snd_pcm_substream *substream,
3797 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
Takashi Iwai9a081602008-02-12 18:37:26 +01003799 struct snd_pcm_runtime *runtime = substream->runtime;
3800 runtime->hw.channels_max = mout->max_channels;
3801 if (mout->dig_out_nid) {
3802 if (!mout->analog_rates) {
3803 mout->analog_rates = hinfo->rates;
3804 mout->analog_formats = hinfo->formats;
3805 mout->analog_maxbps = hinfo->maxbps;
3806 } else {
3807 runtime->hw.rates = mout->analog_rates;
3808 runtime->hw.formats = mout->analog_formats;
3809 hinfo->maxbps = mout->analog_maxbps;
3810 }
3811 if (!mout->spdif_rates) {
3812 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3813 &mout->spdif_rates,
3814 &mout->spdif_formats,
3815 &mout->spdif_maxbps);
3816 }
3817 mutex_lock(&codec->spdif_mutex);
3818 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02003819 if ((runtime->hw.rates & mout->spdif_rates) &&
3820 (runtime->hw.formats & mout->spdif_formats)) {
3821 runtime->hw.rates &= mout->spdif_rates;
3822 runtime->hw.formats &= mout->spdif_formats;
3823 if (mout->spdif_maxbps < hinfo->maxbps)
3824 hinfo->maxbps = mout->spdif_maxbps;
3825 } else {
3826 mout->share_spdif = 0;
3827 /* FIXME: need notify? */
3828 }
Takashi Iwai9a081602008-02-12 18:37:26 +01003829 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003830 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3833 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3834}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003835EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Takashi Iwaid5191e52009-11-16 14:58:17 +01003837/**
3838 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003839 * @codec: the HDA codec
3840 * @mout: hda_multi_out object
3841 * @stream_tag: stream tag to assign
3842 * @format: format id to assign
3843 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003844 *
3845 * Set up the i/o for analog out.
3846 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003848int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3849 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 unsigned int stream_tag,
3851 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003852 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853{
Takashi Iwaidda14412011-05-02 11:29:30 +02003854 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003856 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 int i;
3858
Ingo Molnar62932df2006-01-16 16:34:20 +01003859 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003860 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01003861 if (mout->dig_out_nid && mout->share_spdif &&
3862 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003864 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3865 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06003866 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003868 setup_dig_out_stream(codec, mout->dig_out_nid,
3869 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 } else {
3871 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003872 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 }
3874 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003875 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
3877 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003878 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3879 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003880 if (!mout->no_share_stream &&
3881 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003883 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3884 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003885 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003886 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3887 if (!mout->no_share_stream && mout->hp_out_nid[i])
3888 snd_hda_codec_setup_stream(codec,
3889 mout->hp_out_nid[i],
3890 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 /* surrounds */
3893 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003894 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003895 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3896 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003897 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003898 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3899 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 }
David Henningssoncd4035e2013-10-10 09:01:25 +02003901
3902 /* extra surrounds */
3903 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3904 int ch = 0;
3905 if (!mout->extra_out_nid[i])
3906 break;
3907 if (chs >= (i + 1) * 2)
3908 ch = i * 2;
3909 else if (!mout->no_share_stream)
3910 break;
3911 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3912 stream_tag, ch, format);
3913 }
3914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 return 0;
3916}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003917EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
Takashi Iwaid5191e52009-11-16 14:58:17 +01003919/**
3920 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003921 * @codec: the HDA codec
3922 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003924int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3925 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
Takashi Iwaidda14412011-05-02 11:29:30 +02003927 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 int i;
3929
3930 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003931 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003933 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003934 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3935 if (mout->hp_out_nid[i])
3936 snd_hda_codec_cleanup_stream(codec,
3937 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003938 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3939 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003940 snd_hda_codec_cleanup_stream(codec,
3941 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003942 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003944 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 mout->dig_out_used = 0;
3946 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003947 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 return 0;
3949}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003950EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Takashi Iwai47408602012-04-20 13:06:53 +02003952/**
3953 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003954 * @codec: the HDA codec
3955 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02003956 *
3957 * Guess the suitable VREF pin bits to be set as the pin-control value.
3958 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3959 */
3960unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3961{
3962 unsigned int pincap;
3963 unsigned int oldval;
3964 oldval = snd_hda_codec_read(codec, pin, 0,
3965 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3966 pincap = snd_hda_query_pin_caps(codec, pin);
3967 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3968 /* Exception: if the default pin setup is vref50, we give it priority */
3969 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3970 return AC_PINCTL_VREF_80;
3971 else if (pincap & AC_PINCAP_VREF_50)
3972 return AC_PINCTL_VREF_50;
3973 else if (pincap & AC_PINCAP_VREF_100)
3974 return AC_PINCTL_VREF_100;
3975 else if (pincap & AC_PINCAP_VREF_GRD)
3976 return AC_PINCTL_VREF_GRD;
3977 return AC_PINCTL_VREF_HIZ;
3978}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003979EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02003980
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003981/**
3982 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3983 * @codec: the HDA codec
3984 * @pin: referred pin NID
3985 * @val: pin ctl value to audit
3986 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003987unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3988 hda_nid_t pin, unsigned int val)
3989{
3990 static unsigned int cap_lists[][2] = {
3991 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3992 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3993 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3994 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3995 };
3996 unsigned int cap;
3997
3998 if (!val)
3999 return 0;
4000 cap = snd_hda_query_pin_caps(codec, pin);
4001 if (!cap)
4002 return val; /* don't know what to do... */
4003
4004 if (val & AC_PINCTL_OUT_EN) {
4005 if (!(cap & AC_PINCAP_OUT))
4006 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4007 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4008 val &= ~AC_PINCTL_HP_EN;
4009 }
4010
4011 if (val & AC_PINCTL_IN_EN) {
4012 if (!(cap & AC_PINCAP_IN))
4013 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4014 else {
4015 unsigned int vcap, vref;
4016 int i;
4017 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4018 vref = val & AC_PINCTL_VREFEN;
4019 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4020 if (vref == cap_lists[i][0] &&
4021 !(vcap & cap_lists[i][1])) {
4022 if (i == ARRAY_SIZE(cap_lists) - 1)
4023 vref = AC_PINCTL_VREF_HIZ;
4024 else
4025 vref = cap_lists[i + 1][0];
4026 }
4027 }
4028 val &= ~AC_PINCTL_VREFEN;
4029 val |= vref;
4030 }
4031 }
4032
4033 return val;
4034}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004035EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004036
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004037/**
4038 * _snd_hda_pin_ctl - Helper to set pin ctl value
4039 * @codec: the HDA codec
4040 * @pin: referred pin NID
4041 * @val: pin control value to set
4042 * @cached: access over codec pinctl cache or direct write
4043 *
4044 * This function is a helper to set a pin ctl value more safely.
4045 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4046 * value in pin target array via snd_hda_codec_set_pin_target(), then
4047 * actually writes the value via either snd_hda_codec_update_cache() or
4048 * snd_hda_codec_write() depending on @cached flag.
4049 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004050int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4051 unsigned int val, bool cached)
4052{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004053 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004054 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004055 if (cached)
4056 return snd_hda_codec_update_cache(codec, pin, 0,
4057 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4058 else
4059 return snd_hda_codec_write(codec, pin, 0,
4060 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4061}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004062EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004063
Takashi Iwai990061c2010-09-09 22:08:44 +02004064/**
4065 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004066 * @codec: the HDA codec
4067 * @imux: imux helper object
4068 * @label: the name of imux item to assign
4069 * @index: index number of imux item to assign
4070 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004071 *
4072 * When the same label is used already in the existing items, the number
4073 * suffix is appended to the label. This label index number is stored
4074 * to type_idx when non-NULL pointer is given.
4075 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004076int snd_hda_add_imux_item(struct hda_codec *codec,
4077 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004078 int index, int *type_idx)
4079{
4080 int i, label_idx = 0;
4081 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004082 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004083 return -EINVAL;
4084 }
4085 for (i = 0; i < imux->num_items; i++) {
4086 if (!strncmp(label, imux->items[i].label, strlen(label)))
4087 label_idx++;
4088 }
4089 if (type_idx)
4090 *type_idx = label_idx;
4091 if (label_idx > 0)
4092 snprintf(imux->items[imux->num_items].label,
4093 sizeof(imux->items[imux->num_items].label),
4094 "%s %d", label, label_idx);
4095 else
4096 strlcpy(imux->items[imux->num_items].label, label,
4097 sizeof(imux->items[imux->num_items].label));
4098 imux->items[imux->num_items].index = index;
4099 imux->num_items++;
4100 return 0;
4101}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004102EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104/**
Takashi Iwai0a505752015-04-16 23:25:02 +02004105 * snd_hda_bus_reset_codecs - Reset the bus
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004106 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 */
Takashi Iwai0a505752015-04-16 23:25:02 +02004108void snd_hda_bus_reset_codecs(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004110 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004112 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004113 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004114 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004115#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004116 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004117 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004118 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004119 }
4120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122}
Takashi Iwaib2e18592008-07-30 15:01:44 +02004123
Takashi Iwaid5191e52009-11-16 14:58:17 +01004124/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004125 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4126 * @pcm: PCM caps bits
4127 * @buf: the string buffer to write
4128 * @buflen: the max buffer length
4129 *
4130 * used by hda_proc.c and hda_eld.c
4131 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004132void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4133{
4134 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4135 int i, j;
4136
4137 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4138 if (pcm & (AC_SUPPCM_BITS_8 << i))
4139 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4140
4141 buf[j] = '\0'; /* necessary when j == 0 */
4142}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004143EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004144
4145MODULE_DESCRIPTION("HDA codec core");
4146MODULE_LICENSE("GPL");