blob: 7f8502388a822ec3c194f3b87847f67800b243f7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020034#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020035#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * vendor / preset table
39 */
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46/* codec vendor labels */
47static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010048 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020049 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020050 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010051 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010052 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010053 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020054 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010055 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020056 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020058 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020060 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010061 { 0x17e8, "Chrontel" },
62 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000063 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010065 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020066 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 {} /* terminator */
68};
69
Takashi Iwai1289e9e2008-11-27 15:47:11 +010070static DEFINE_MUTEX(preset_mutex);
71static LIST_HEAD(hda_preset_tables);
72
73int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74{
75 mutex_lock(&preset_mutex);
76 list_add_tail(&preset->list, &hda_preset_tables);
77 mutex_unlock(&preset_mutex);
78 return 0;
79}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010080EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010081
82int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83{
84 mutex_lock(&preset_mutex);
85 list_del(&preset->list);
86 mutex_unlock(&preset_mutex);
87 return 0;
88}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010089EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwaicb53c622007-08-10 17:21:45 +020091#ifdef CONFIG_SND_HDA_POWER_SAVE
92static void hda_power_work(struct work_struct *work);
93static void hda_keep_power_on(struct hda_codec *codec);
94#else
95static inline void hda_keep_power_on(struct hda_codec *codec) {}
96#endif
97
Takashi Iwaid5191e52009-11-16 14:58:17 +010098/**
99 * snd_hda_get_jack_location - Give a location string of the jack
100 * @cfg: pin default config value
101 *
102 * Parse the pin default config value and returns the string of the
103 * jack location, e.g. "Rear", "Front", etc.
104 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400105const char *snd_hda_get_jack_location(u32 cfg)
106{
107 static char *bases[7] = {
108 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 };
110 static unsigned char specials_idx[] = {
111 0x07, 0x08,
112 0x17, 0x18, 0x19,
113 0x37, 0x38
114 };
115 static char *specials[] = {
116 "Rear Panel", "Drive Bar",
117 "Riser", "HDMI", "ATAPI",
118 "Mobile-In", "Mobile-Out"
119 };
120 int i;
121 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
122 if ((cfg & 0x0f) < 7)
123 return bases[cfg & 0x0f];
124 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
125 if (cfg == specials_idx[i])
126 return specials[i];
127 }
128 return "UNKNOWN";
129}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100130EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400131
Takashi Iwaid5191e52009-11-16 14:58:17 +0100132/**
133 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
134 * @cfg: pin default config value
135 *
136 * Parse the pin default config value and returns the string of the
137 * jack connectivity, i.e. external or internal connection.
138 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139const char *snd_hda_get_jack_connectivity(u32 cfg)
140{
141 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142
143 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100145EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400146
Takashi Iwaid5191e52009-11-16 14:58:17 +0100147/**
148 * snd_hda_get_jack_type - Give a type string of the jack
149 * @cfg: pin default config value
150 *
151 * Parse the pin default config value and returns the string of the
152 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154const char *snd_hda_get_jack_type(u32 cfg)
155{
156 static char *jack_types[16] = {
157 "Line Out", "Speaker", "HP Out", "CD",
158 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
159 "Line In", "Aux", "Mic", "Telephony",
160 "SPDIF In", "Digitial In", "Reserved", "Other"
161 };
162
163 return jack_types[(cfg & AC_DEFCFG_DEVICE)
164 >> AC_DEFCFG_DEVICE_SHIFT];
165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100166EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400167
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100168/*
169 * Compose a 32bit command word to be sent to the HD-audio controller
170 */
171static inline unsigned int
172make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
173 unsigned int verb, unsigned int parm)
174{
175 u32 val;
176
Takashi Iwai82e1b802009-07-17 12:47:34 +0200177 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
178 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800179 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
180 codec->addr, direct, nid, verb, parm);
181 return ~0;
182 }
183
184 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100185 val |= (u32)direct << 27;
186 val |= (u32)nid << 20;
187 val |= verb << 8;
188 val |= parm;
189 return val;
190}
191
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200192/*
193 * Send and receive a verb
194 */
195static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
196 unsigned int *res)
197{
198 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200199 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200
Wu Fengguang6430aee2009-07-17 16:49:19 +0800201 if (cmd == ~0)
202 return -1;
203
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200204 if (res)
205 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200206 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207 snd_hda_power_up(codec);
208 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200209 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200210 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800211 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 mutex_unlock(&bus->cmd_mutex);
213 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 if (res && *res == -1 && bus->rirb_error) {
215 if (bus->response_reset) {
216 snd_printd("hda_codec: resetting BUS due to "
217 "fatal communication error\n");
218 bus->ops.bus_reset(bus);
219 }
220 goto again;
221 }
222 /* clear reset-flag when the communication gets recovered */
223 if (!err)
224 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200225 return err;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/**
229 * snd_hda_codec_read - send a command and get the response
230 * @codec: the HDA codec
231 * @nid: NID to send the command
232 * @direct: direct flag
233 * @verb: the verb to send
234 * @parm: the parameter for the verb
235 *
236 * Send a single command and read the corresponding response.
237 *
238 * Returns the obtained response value, or -1 for an error.
239 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200240unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
241 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int verb, unsigned int parm)
243{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200244 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700246 if (codec_exec_verb(codec, cmd, &res))
247 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return res;
249}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100250EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252/**
253 * snd_hda_codec_write - send a single command without waiting for response
254 * @codec: the HDA codec
255 * @nid: NID to send the command
256 * @direct: direct flag
257 * @verb: the verb to send
258 * @parm: the parameter for the verb
259 *
260 * Send a single command without waiting for response.
261 *
262 * Returns 0 if successful, or a negative error code.
263 */
264int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
265 unsigned int verb, unsigned int parm)
266{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200267 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100268 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200269 return codec_exec_verb(codec, cmd,
270 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100272EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/**
275 * snd_hda_sequence_write - sequence writes
276 * @codec: the HDA codec
277 * @seq: VERB array to send
278 *
279 * Send the commands sequentially from the given array.
280 * The array must be terminated with NID=0.
281 */
282void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
283{
284 for (; seq->nid; seq++)
285 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100287EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/**
290 * snd_hda_get_sub_nodes - get the range of sub nodes
291 * @codec: the HDA codec
292 * @nid: NID to parse
293 * @start_id: the pointer to store the start NID
294 *
295 * Parse the NID and store the start NID of its sub-nodes.
296 * Returns the number of sub-nodes.
297 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200298int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
299 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 unsigned int parm;
302
303 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200304 if (parm == -1)
305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *start_id = (parm >> 16) & 0x7fff;
307 return (int)(parm & 0x7fff);
308}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100309EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200311static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
312 hda_nid_t *conn_list, int max_conns);
313static bool add_conn_list(struct snd_array *array, hda_nid_t nid);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/**
316 * snd_hda_get_connections - get connection list
317 * @codec: the HDA codec
318 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200319 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
321 * Parses the connection list of the given widget and stores the list
322 * of NIDs.
323 *
324 * Returns the number of connections, or a negative error code.
325 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200326int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
327 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200328{
329 struct snd_array *array = &codec->conn_lists;
Andrew Morton5b17b072011-04-14 15:06:13 -0700330 int i, len, old_used;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200331 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200332 hda_nid_t *p;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200333
334 /* look up the cached results */
335 for (i = 0; i < array->used; ) {
Takashi Iwaidce20792011-06-24 14:10:28 +0200336 p = snd_array_elem(array, i);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200337 len = p[1];
Takashi Iwaidce20792011-06-24 14:10:28 +0200338 if (nid == *p) {
339 if (listp)
340 *listp = p + 2;
341 return len;
342 }
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200343 i += len + 2;
344 }
345
346 len = _hda_get_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
347 if (len < 0)
348 return len;
349
350 /* add to the cache */
351 old_used = array->used;
352 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
353 goto error_add;
354 for (i = 0; i < len; i++)
355 if (!add_conn_list(array, list[i]))
356 goto error_add;
357
Takashi Iwaidce20792011-06-24 14:10:28 +0200358 p = snd_array_elem(array, old_used);
359 if (listp)
360 *listp = p + 2;
361 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200362
363 error_add:
364 array->used = old_used;
365 return -ENOMEM;
366}
Takashi Iwaidce20792011-06-24 14:10:28 +0200367EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
368
369/**
370 * snd_hda_get_connections - copy connection list
371 * @codec: the HDA codec
372 * @nid: NID to parse
373 * @conn_list: connection list array
374 * @max_conns: max. number of connections to store
375 *
376 * Parses the connection list of the given widget and stores the list
377 * of NIDs.
378 *
379 * Returns the number of connections, or a negative error code.
380 */
381int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
382 hda_nid_t *conn_list, int max_conns)
383{
384 const hda_nid_t *list;
385 int len = snd_hda_get_conn_list(codec, nid, &list);
386
387 if (len <= 0)
388 return len;
389 if (len > max_conns) {
390 snd_printk(KERN_ERR "hda_codec: "
391 "Too many connections %d for NID 0x%x\n",
392 len, nid);
393 return -EINVAL;
394 }
395 memcpy(conn_list, list, len * sizeof(hda_nid_t));
396 return len;
397}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200398EXPORT_SYMBOL_HDA(snd_hda_get_connections);
399
400static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
401 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100404 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200406 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100407 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Takashi Iwaida3cec32008-08-08 17:12:14 +0200409 if (snd_BUG_ON(!conn_list || max_conns <= 0))
410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200412 wcaps = get_wcaps(codec, nid);
413 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200414 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
415 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
418 if (parm & AC_CLIST_LONG) {
419 /* long form */
420 shift = 16;
421 num_elems = 2;
422 } else {
423 /* short form */
424 shift = 8;
425 num_elems = 4;
426 }
427 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 mask = (1 << (shift-1)) - 1;
429
Takashi Iwai0ba21762007-04-16 11:29:14 +0200430 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0; /* no connection */
432
433 if (conn_len == 1) {
434 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200435 parm = snd_hda_codec_read(codec, nid, 0,
436 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200437 if (parm == -1 && codec->bus->rirb_error)
438 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 conn_list[0] = parm & mask;
440 return 1;
441 }
442
443 /* multi connection */
444 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100445 prev_nid = 0;
446 for (i = 0; i < conn_len; i++) {
447 int range_val;
448 hda_nid_t val, n;
449
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200450 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100451 parm = snd_hda_codec_read(codec, nid, 0,
452 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200453 if (parm == -1 && codec->bus->rirb_error)
454 return -EIO;
455 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200456 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100457 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200458 if (val == 0) {
459 snd_printk(KERN_WARNING "hda_codec: "
460 "invalid CONNECT_LIST verb %x[%i]:%x\n",
461 nid, i, parm);
462 return 0;
463 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100464 parm >>= shift;
465 if (range_val) {
466 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200467 if (!prev_nid || prev_nid >= val) {
468 snd_printk(KERN_WARNING "hda_codec: "
469 "invalid dep_range_val %x:%x\n",
470 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100471 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100473 for (n = prev_nid + 1; n <= val; n++) {
474 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200475 snd_printk(KERN_ERR "hda_codec: "
476 "Too many connections %d for NID 0x%x\n",
477 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100478 return -EINVAL;
479 }
480 conn_list[conns++] = n;
481 }
482 } else {
483 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200484 snd_printk(KERN_ERR "hda_codec: "
485 "Too many connections %d for NID 0x%x\n",
486 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100487 return -EINVAL;
488 }
489 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100491 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 return conns;
494}
495
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200496static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
497{
498 hda_nid_t *p = snd_array_new(array);
499 if (!p)
500 return false;
501 *p = nid;
502 return true;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200506 * snd_hda_get_conn_index - get the connection index of the given NID
507 * @codec: the HDA codec
508 * @mux: NID containing the list
509 * @nid: NID to select
510 * @recursive: 1 when searching NID recursively, otherwise 0
511 *
512 * Parses the connection list of the widget @mux and checks whether the
513 * widget @nid is present. If it is, return the connection index.
514 * Otherwise it returns -1.
515 */
516int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
517 hda_nid_t nid, int recursive)
518{
519 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
520 int i, nums;
521
522 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
523 for (i = 0; i < nums; i++)
524 if (conn[i] == nid)
525 return i;
526 if (!recursive)
527 return -1;
528 if (recursive > 5) {
529 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
530 return -1;
531 }
532 recursive++;
533 for (i = 0; i < nums; i++)
534 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
535 return i;
536 return -1;
537}
538EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
539
540/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * snd_hda_queue_unsol_event - add an unsolicited event to queue
542 * @bus: the BUS
543 * @res: unsolicited event (lower 32bit of RIRB entry)
544 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
545 *
546 * Adds the given event to the queue. The events are processed in
547 * the workqueue asynchronously. Call this function in the interrupt
548 * hanlder when RIRB receives an unsolicited event.
549 *
550 * Returns 0 if successful, or a negative error code.
551 */
552int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
553{
554 struct hda_bus_unsolicited *unsol;
555 unsigned int wp;
556
Takashi Iwai0ba21762007-04-16 11:29:14 +0200557 unsol = bus->unsol;
558 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 0;
560
561 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
562 unsol->wp = wp;
563
564 wp <<= 1;
565 unsol->queue[wp] = res;
566 unsol->queue[wp + 1] = res_ex;
567
Takashi Iwai6acaed32009-01-12 10:09:24 +0100568 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 return 0;
571}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100572EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800575 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
David Howellsc4028952006-11-22 14:57:56 +0000577static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
David Howellsc4028952006-11-22 14:57:56 +0000579 struct hda_bus_unsolicited *unsol =
580 container_of(work, struct hda_bus_unsolicited, work);
581 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct hda_codec *codec;
583 unsigned int rp, caddr, res;
584
585 while (unsol->rp != unsol->wp) {
586 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
587 unsol->rp = rp;
588 rp <<= 1;
589 res = unsol->queue[rp];
590 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200591 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 continue;
593 codec = bus->caddr_tbl[caddr & 0x0f];
594 if (codec && codec->patch_ops.unsol_event)
595 codec->patch_ops.unsol_event(codec, res);
596 }
597}
598
599/*
600 * initialize unsolicited queue
601 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200602static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct hda_bus_unsolicited *unsol;
605
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100606 if (bus->unsol) /* already initialized */
607 return 0;
608
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200609 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200610 if (!unsol) {
611 snd_printk(KERN_ERR "hda_codec: "
612 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -ENOMEM;
614 }
David Howellsc4028952006-11-22 14:57:56 +0000615 INIT_WORK(&unsol->work, process_unsol_events);
616 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 bus->unsol = unsol;
618 return 0;
619}
620
621/*
622 * destructor
623 */
624static void snd_hda_codec_free(struct hda_codec *codec);
625
626static int snd_hda_bus_free(struct hda_bus *bus)
627{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200628 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Takashi Iwai0ba21762007-04-16 11:29:14 +0200630 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100632 if (bus->workq)
633 flush_workqueue(bus->workq);
634 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200636 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 snd_hda_codec_free(codec);
638 }
639 if (bus->ops.private_free)
640 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100641 if (bus->workq)
642 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 kfree(bus);
644 return 0;
645}
646
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100647static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100650 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return snd_hda_bus_free(bus);
652}
653
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200654#ifdef CONFIG_SND_HDA_HWDEP
655static int snd_hda_bus_dev_register(struct snd_device *device)
656{
657 struct hda_bus *bus = device->device_data;
658 struct hda_codec *codec;
659 list_for_each_entry(codec, &bus->codec_list, list) {
660 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100661 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200662 }
663 return 0;
664}
665#else
666#define snd_hda_bus_dev_register NULL
667#endif
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/**
670 * snd_hda_bus_new - create a HDA bus
671 * @card: the card entry
672 * @temp: the template for hda_bus information
673 * @busp: the pointer to store the created bus instance
674 *
675 * Returns 0 if successful, or a negative error code.
676 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100677int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200678 const struct hda_bus_template *temp,
679 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct hda_bus *bus;
682 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100683 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200684 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 .dev_free = snd_hda_bus_dev_free,
686 };
687
Takashi Iwaida3cec32008-08-08 17:12:14 +0200688 if (snd_BUG_ON(!temp))
689 return -EINVAL;
690 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
691 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (busp)
694 *busp = NULL;
695
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200696 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (bus == NULL) {
698 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
699 return -ENOMEM;
700 }
701
702 bus->card = card;
703 bus->private_data = temp->private_data;
704 bus->pci = temp->pci;
705 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100706 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 bus->ops = temp->ops;
708
Ingo Molnar62932df2006-01-16 16:34:20 +0100709 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200710 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 INIT_LIST_HEAD(&bus->codec_list);
712
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100713 snprintf(bus->workq_name, sizeof(bus->workq_name),
714 "hd-audio%d", card->number);
715 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100716 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100717 snd_printk(KERN_ERR "cannot create workqueue %s\n",
718 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100719 kfree(bus);
720 return -ENOMEM;
721 }
722
Takashi Iwai0ba21762007-04-16 11:29:14 +0200723 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
724 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 snd_hda_bus_free(bus);
726 return err;
727 }
728 if (busp)
729 *busp = bus;
730 return 0;
731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100732EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Takashi Iwai82467612007-07-27 19:15:54 +0200734#ifdef CONFIG_SND_HDA_GENERIC
735#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200736 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200737#else
738#define is_generic_config(codec) 0
739#endif
740
Takashi Iwai645f10c2008-11-28 15:07:37 +0100741#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100742#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
743#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100744#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100745#endif
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/*
748 * find a matching codec preset
749 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200750static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200751find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100753 struct hda_codec_preset_list *tbl;
754 const struct hda_codec_preset *preset;
755 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Takashi Iwai82467612007-07-27 19:15:54 +0200757 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100758 return NULL; /* use the generic parser */
759
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100760 again:
761 mutex_lock(&preset_mutex);
762 list_for_each_entry(tbl, &hda_preset_tables, list) {
763 if (!try_module_get(tbl->owner)) {
764 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
765 continue;
766 }
767 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100769 if (preset->afg && preset->afg != codec->afg)
770 continue;
771 if (preset->mfg && preset->mfg != codec->mfg)
772 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200773 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200775 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200776 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100777 preset->rev == codec->revision_id)) {
778 mutex_unlock(&preset_mutex);
779 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100783 module_put(tbl->owner);
784 }
785 mutex_unlock(&preset_mutex);
786
787 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
788 char name[32];
789 if (!mod_requested)
790 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
791 codec->vendor_id);
792 else
793 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
794 (codec->vendor_id >> 16) & 0xffff);
795 request_module(name);
796 mod_requested++;
797 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 return NULL;
800}
801
802/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200803 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200805static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 const struct hda_vendor_id *c;
808 const char *vendor = NULL;
809 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200810 char tmp[16];
811
812 if (codec->vendor_name)
813 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 for (c = hda_vendor_ids; c->id; c++) {
816 if (c->id == vendor_id) {
817 vendor = c->name;
818 break;
819 }
820 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200821 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 sprintf(tmp, "Generic %04x", vendor_id);
823 vendor = tmp;
824 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200825 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
826 if (!codec->vendor_name)
827 return -ENOMEM;
828
829 get_chip_name:
830 if (codec->chip_name)
831 return 0;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200834 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
835 else {
836 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
837 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
838 }
839 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200840 return -ENOMEM;
841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200845 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100847static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200849 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 hda_nid_t nid;
851
852 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
853 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200854 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200855 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200856 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200857 case AC_GRP_AUDIO_FUNCTION:
858 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200859 codec->afg_function_id = function_id & 0xff;
860 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200861 break;
862 case AC_GRP_MODEM_FUNCTION:
863 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200864 codec->mfg_function_id = function_id & 0xff;
865 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200866 break;
867 default:
868 break;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100874 * read widget caps for each widget and store in cache
875 */
876static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
877{
878 int i;
879 hda_nid_t nid;
880
881 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
882 &codec->start_nid);
883 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200884 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100885 return -ENOMEM;
886 nid = codec->start_nid;
887 for (i = 0; i < codec->num_nodes; i++, nid++)
888 codec->wcaps[i] = snd_hda_param_read(codec, nid,
889 AC_PAR_AUDIO_WIDGET_CAP);
890 return 0;
891}
892
Takashi Iwai3be14142009-02-20 14:11:16 +0100893/* read all pin default configurations and save codec->init_pins */
894static int read_pin_defaults(struct hda_codec *codec)
895{
896 int i;
897 hda_nid_t nid = codec->start_nid;
898
899 for (i = 0; i < codec->num_nodes; i++, nid++) {
900 struct hda_pincfg *pin;
901 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200902 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100903 if (wid_type != AC_WID_PIN)
904 continue;
905 pin = snd_array_new(&codec->init_pins);
906 if (!pin)
907 return -ENOMEM;
908 pin->nid = nid;
909 pin->cfg = snd_hda_codec_read(codec, nid, 0,
910 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200911 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
912 AC_VERB_GET_PIN_WIDGET_CONTROL,
913 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100914 }
915 return 0;
916}
917
918/* look up the given pin config list and return the item matching with NID */
919static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
920 struct snd_array *array,
921 hda_nid_t nid)
922{
923 int i;
924 for (i = 0; i < array->used; i++) {
925 struct hda_pincfg *pin = snd_array_elem(array, i);
926 if (pin->nid == nid)
927 return pin;
928 }
929 return NULL;
930}
931
932/* write a config value for the given NID */
933static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
934 unsigned int cfg)
935{
936 int i;
937 for (i = 0; i < 4; i++) {
938 snd_hda_codec_write(codec, nid, 0,
939 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
940 cfg & 0xff);
941 cfg >>= 8;
942 }
943}
944
945/* set the current pin config value for the given NID.
946 * the value is cached, and read via snd_hda_codec_get_pincfg()
947 */
948int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
949 hda_nid_t nid, unsigned int cfg)
950{
951 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100952 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100953
Takashi Iwaib82855a2009-12-27 11:24:56 +0100954 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
955 return -EINVAL;
956
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100957 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100958 pin = look_up_pincfg(codec, list, nid);
959 if (!pin) {
960 pin = snd_array_new(list);
961 if (!pin)
962 return -ENOMEM;
963 pin->nid = nid;
964 }
965 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100966
967 /* change only when needed; e.g. if the pincfg is already present
968 * in user_pins[], don't write it
969 */
970 cfg = snd_hda_codec_get_pincfg(codec, nid);
971 if (oldcfg != cfg)
972 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100973 return 0;
974}
975
Takashi Iwaid5191e52009-11-16 14:58:17 +0100976/**
977 * snd_hda_codec_set_pincfg - Override a pin default configuration
978 * @codec: the HDA codec
979 * @nid: NID to set the pin config
980 * @cfg: the pin default config value
981 *
982 * Override a pin default configuration value in the cache.
983 * This value can be read by snd_hda_codec_get_pincfg() in a higher
984 * priority than the real hardware value.
985 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100986int snd_hda_codec_set_pincfg(struct hda_codec *codec,
987 hda_nid_t nid, unsigned int cfg)
988{
Takashi Iwai346ff702009-02-23 09:42:57 +0100989 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100990}
991EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
992
Takashi Iwaid5191e52009-11-16 14:58:17 +0100993/**
994 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
995 * @codec: the HDA codec
996 * @nid: NID to get the pin config
997 *
998 * Get the current pin config value of the given pin NID.
999 * If the pincfg value is cached or overridden via sysfs or driver,
1000 * returns the cached value.
1001 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001002unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1003{
1004 struct hda_pincfg *pin;
1005
Takashi Iwai3be14142009-02-20 14:11:16 +01001006#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001007 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001008 if (pin)
1009 return pin->cfg;
1010#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001011 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1012 if (pin)
1013 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001014 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1015 if (pin)
1016 return pin->cfg;
1017 return 0;
1018}
1019EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1020
1021/* restore all current pin configs */
1022static void restore_pincfgs(struct hda_codec *codec)
1023{
1024 int i;
1025 for (i = 0; i < codec->init_pins.used; i++) {
1026 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1027 set_pincfg(codec, pin->nid,
1028 snd_hda_codec_get_pincfg(codec, pin->nid));
1029 }
1030}
Takashi Iwai54d17402005-11-21 16:33:22 +01001031
Takashi Iwai92ee6162009-12-27 11:18:59 +01001032/**
1033 * snd_hda_shutup_pins - Shut up all pins
1034 * @codec: the HDA codec
1035 *
1036 * Clear all pin controls to shup up before suspend for avoiding click noise.
1037 * The controls aren't cached so that they can be resumed properly.
1038 */
1039void snd_hda_shutup_pins(struct hda_codec *codec)
1040{
1041 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001042 /* don't shut up pins when unloading the driver; otherwise it breaks
1043 * the default pin setup at the next load of the driver
1044 */
1045 if (codec->bus->shutdown)
1046 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001047 for (i = 0; i < codec->init_pins.used; i++) {
1048 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1049 /* use read here for syncing after issuing each verb */
1050 snd_hda_codec_read(codec, pin->nid, 0,
1051 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1052 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001053 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001054}
1055EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1056
Mike Waychison1c7276c2011-04-20 12:04:36 -07001057#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001058/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1059static void restore_shutup_pins(struct hda_codec *codec)
1060{
1061 int i;
1062 if (!codec->pins_shutup)
1063 return;
1064 if (codec->bus->shutdown)
1065 return;
1066 for (i = 0; i < codec->init_pins.used; i++) {
1067 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1068 snd_hda_codec_write(codec, pin->nid, 0,
1069 AC_VERB_SET_PIN_WIDGET_CONTROL,
1070 pin->ctrl);
1071 }
1072 codec->pins_shutup = 0;
1073}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001074#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001075
Takashi Iwai01751f52007-08-10 16:59:39 +02001076static void init_hda_cache(struct hda_cache_rec *cache,
1077 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001078static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001079
Takashi Iwai3be14142009-02-20 14:11:16 +01001080/* restore the initial pin cfgs and release all pincfg lists */
1081static void restore_init_pincfgs(struct hda_codec *codec)
1082{
Takashi Iwai346ff702009-02-23 09:42:57 +01001083 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001084 * so that only the values in init_pins are restored
1085 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001086 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001087#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001088 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001089#endif
1090 restore_pincfgs(codec);
1091 snd_array_free(&codec->init_pins);
1092}
1093
Takashi Iwai54d17402005-11-21 16:33:22 +01001094/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001095 * audio-converter setup caches
1096 */
1097struct hda_cvt_setup {
1098 hda_nid_t nid;
1099 u8 stream_tag;
1100 u8 channel_id;
1101 u16 format_id;
1102 unsigned char active; /* cvt is currently used */
1103 unsigned char dirty; /* setups should be cleared */
1104};
1105
1106/* get or create a cache entry for the given audio converter NID */
1107static struct hda_cvt_setup *
1108get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1109{
1110 struct hda_cvt_setup *p;
1111 int i;
1112
1113 for (i = 0; i < codec->cvt_setups.used; i++) {
1114 p = snd_array_elem(&codec->cvt_setups, i);
1115 if (p->nid == nid)
1116 return p;
1117 }
1118 p = snd_array_new(&codec->cvt_setups);
1119 if (p)
1120 p->nid = nid;
1121 return p;
1122}
1123
1124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 * codec destructor
1126 */
1127static void snd_hda_codec_free(struct hda_codec *codec)
1128{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001129 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001131 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001132#ifdef CONFIG_SND_HDA_POWER_SAVE
1133 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001134 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001135#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001137 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001138 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001139 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001140 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 codec->bus->caddr_tbl[codec->addr] = NULL;
1142 if (codec->patch_ops.free)
1143 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001144 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001145 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001146 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001147 kfree(codec->vendor_name);
1148 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001149 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001150 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 kfree(codec);
1152}
1153
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001154static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1155 unsigned int power_state);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/**
1158 * snd_hda_codec_new - create a HDA codec
1159 * @bus: the bus to assign
1160 * @codec_addr: the codec address
1161 * @codecp: the pointer to store the generated codec
1162 *
1163 * Returns 0 if successful, or a negative error code.
1164 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001165int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1166 unsigned int codec_addr,
1167 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001170 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 int err;
1172
Takashi Iwaida3cec32008-08-08 17:12:14 +02001173 if (snd_BUG_ON(!bus))
1174 return -EINVAL;
1175 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1176 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001179 snd_printk(KERN_ERR "hda_codec: "
1180 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return -EBUSY;
1182 }
1183
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001184 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (codec == NULL) {
1186 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1187 return -ENOMEM;
1188 }
1189
1190 codec->bus = bus;
1191 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001192 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001193 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001194 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001195 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001196 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1197 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001198 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001199 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001200 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001201 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001202 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001203 if (codec->bus->modelname) {
1204 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1205 if (!codec->modelname) {
1206 snd_hda_codec_free(codec);
1207 return -ENODEV;
1208 }
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Takashi Iwaicb53c622007-08-10 17:21:45 +02001211#ifdef CONFIG_SND_HDA_POWER_SAVE
1212 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1213 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1214 * the caller has to power down appropriatley after initialization
1215 * phase.
1216 */
1217 hda_keep_power_on(codec);
1218#endif
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 list_add_tail(&codec->list, &bus->codec_list);
1221 bus->caddr_tbl[codec_addr] = codec;
1222
Takashi Iwai0ba21762007-04-16 11:29:14 +02001223 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1224 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001225 if (codec->vendor_id == -1)
1226 /* read again, hopefully the access method was corrected
1227 * in the last read...
1228 */
1229 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1230 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001231 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1232 AC_PAR_SUBSYSTEM_ID);
1233 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1234 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001236 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001237 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001238 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001239 err = -ENODEV;
1240 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Takashi Iwai3be14142009-02-20 14:11:16 +01001243 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1244 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001245 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001246 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001247 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001248 err = read_pin_defaults(codec);
1249 if (err < 0)
1250 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001251
Takashi Iwai0ba21762007-04-16 11:29:14 +02001252 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001253 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001254 codec->subsystem_id =
1255 snd_hda_codec_read(codec, nid, 0,
1256 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001257 }
1258
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001259 /* power-up all before initialization */
1260 hda_set_power_state(codec,
1261 codec->afg ? codec->afg : codec->mfg,
1262 AC_PWRST_D0);
1263
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001264 snd_hda_codec_proc_new(codec);
1265
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001266 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001267
1268 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1269 codec->subsystem_id, codec->revision_id);
1270 snd_component_add(codec->bus->card, component);
1271
1272 if (codecp)
1273 *codecp = codec;
1274 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001275
1276 error:
1277 snd_hda_codec_free(codec);
1278 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001279}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001280EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001281
Takashi Iwaid5191e52009-11-16 14:58:17 +01001282/**
1283 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1284 * @codec: the HDA codec
1285 *
1286 * Start parsing of the given codec tree and (re-)initialize the whole
1287 * patch instance.
1288 *
1289 * Returns 0 if successful or a negative error code.
1290 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001291int snd_hda_codec_configure(struct hda_codec *codec)
1292{
1293 int err;
1294
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001295 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001296 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001297 err = get_codec_name(codec);
1298 if (err < 0)
1299 return err;
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Takashi Iwai82467612007-07-27 19:15:54 +02001302 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001304 goto patched;
1305 }
Takashi Iwai82467612007-07-27 19:15:54 +02001306 if (codec->preset && codec->preset->patch) {
1307 err = codec->preset->patch(codec);
1308 goto patched;
1309 }
1310
1311 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001312 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001313 if (err < 0)
1314 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001315
1316 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001317 if (!err && codec->patch_ops.unsol_event)
1318 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001319 /* audio codec should override the mixer name */
1320 if (!err && (codec->afg || !*codec->bus->card->mixername))
1321 snprintf(codec->bus->card->mixername,
1322 sizeof(codec->bus->card->mixername),
1323 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001326EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328/**
1329 * snd_hda_codec_setup_stream - set up the codec for streaming
1330 * @codec: the CODEC to set up
1331 * @nid: the NID to set up
1332 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1333 * @channel_id: channel id to pass, zero based.
1334 * @format: stream format.
1335 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001336void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1337 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int channel_id, int format)
1339{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001340 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001341 struct hda_cvt_setup *p;
1342 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001343 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001344 int i;
1345
Takashi Iwai0ba21762007-04-16 11:29:14 +02001346 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001347 return;
1348
Takashi Iwai0ba21762007-04-16 11:29:14 +02001349 snd_printdd("hda_codec_setup_stream: "
1350 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001352 p = get_hda_cvt_setup(codec, nid);
1353 if (!p)
1354 return;
1355 /* update the stream-id if changed */
1356 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1357 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1358 newval = (stream_tag << 4) | channel_id;
1359 if (oldval != newval)
1360 snd_hda_codec_write(codec, nid, 0,
1361 AC_VERB_SET_CHANNEL_STREAMID,
1362 newval);
1363 p->stream_tag = stream_tag;
1364 p->channel_id = channel_id;
1365 }
1366 /* update the format-id if changed */
1367 if (p->format_id != format) {
1368 oldval = snd_hda_codec_read(codec, nid, 0,
1369 AC_VERB_GET_STREAM_FORMAT, 0);
1370 if (oldval != format) {
1371 msleep(1);
1372 snd_hda_codec_write(codec, nid, 0,
1373 AC_VERB_SET_STREAM_FORMAT,
1374 format);
1375 }
1376 p->format_id = format;
1377 }
1378 p->active = 1;
1379 p->dirty = 0;
1380
1381 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001382 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001383 list_for_each_entry(c, &codec->bus->codec_list, list) {
1384 for (i = 0; i < c->cvt_setups.used; i++) {
1385 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001386 if (!p->active && p->stream_tag == stream_tag &&
1387 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001388 p->dirty = 1;
1389 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001392EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Takashi Iwaif0cea792010-08-13 11:56:53 +02001394static void really_cleanup_stream(struct hda_codec *codec,
1395 struct hda_cvt_setup *q);
1396
Takashi Iwaid5191e52009-11-16 14:58:17 +01001397/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001398 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001399 * @codec: the CODEC to clean up
1400 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001401 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001402 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001403void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1404 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001405{
Takashi Iwaieb541332010-08-06 13:48:11 +02001406 struct hda_cvt_setup *p;
1407
Takashi Iwai888afa12008-03-18 09:57:50 +01001408 if (!nid)
1409 return;
1410
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001411 if (codec->no_sticky_stream)
1412 do_now = 1;
1413
Takashi Iwai888afa12008-03-18 09:57:50 +01001414 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001415 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001416 if (p) {
1417 /* here we just clear the active flag when do_now isn't set;
1418 * actual clean-ups will be done later in
1419 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1420 */
1421 if (do_now)
1422 really_cleanup_stream(codec, p);
1423 else
1424 p->active = 0;
1425 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001426}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001427EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001428
Takashi Iwaieb541332010-08-06 13:48:11 +02001429static void really_cleanup_stream(struct hda_codec *codec,
1430 struct hda_cvt_setup *q)
1431{
1432 hda_nid_t nid = q->nid;
1433 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1434 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1435 memset(q, 0, sizeof(*q));
1436 q->nid = nid;
1437}
1438
1439/* clean up the all conflicting obsolete streams */
1440static void purify_inactive_streams(struct hda_codec *codec)
1441{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001442 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001443 int i;
1444
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001445 list_for_each_entry(c, &codec->bus->codec_list, list) {
1446 for (i = 0; i < c->cvt_setups.used; i++) {
1447 struct hda_cvt_setup *p;
1448 p = snd_array_elem(&c->cvt_setups, i);
1449 if (p->dirty)
1450 really_cleanup_stream(c, p);
1451 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001452 }
1453}
1454
Mike Waychison1c7276c2011-04-20 12:04:36 -07001455#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaieb541332010-08-06 13:48:11 +02001456/* clean up all streams; called from suspend */
1457static void hda_cleanup_all_streams(struct hda_codec *codec)
1458{
1459 int i;
1460
1461 for (i = 0; i < codec->cvt_setups.used; i++) {
1462 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1463 if (p->stream_tag)
1464 really_cleanup_stream(codec, p);
1465 }
1466}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001467#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/*
1470 * amp access functions
1471 */
1472
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001473/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001474#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001475#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001476#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1477#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001479#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001482static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001483 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Takashi Iwai01751f52007-08-10 16:59:39 +02001485 memset(cache, 0, sizeof(*cache));
1486 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001487 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001488}
1489
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001490static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001491{
Takashi Iwai603c4012008-07-30 15:01:44 +02001492 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001496static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Takashi Iwai01751f52007-08-10 16:59:39 +02001498 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1499 u16 cur = cache->hash[idx];
1500 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001503 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (info->key == key)
1505 return info;
1506 cur = info->next;
1507 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001508 return NULL;
1509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001511/* query the hash. allocate an entry if not found. */
1512static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1513 u32 key)
1514{
1515 struct hda_cache_head *info = get_hash(cache, key);
1516 if (!info) {
1517 u16 idx, cur;
1518 /* add a new hash entry */
1519 info = snd_array_new(&cache->buf);
1520 if (!info)
1521 return NULL;
1522 cur = snd_array_index(&cache->buf, info);
1523 info->key = key;
1524 info->val = 0;
1525 idx = key % (u16)ARRAY_SIZE(cache->hash);
1526 info->next = cache->hash[idx];
1527 cache->hash[idx] = cur;
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return info;
1530}
1531
Takashi Iwai01751f52007-08-10 16:59:39 +02001532/* query and allocate an amp hash entry */
1533static inline struct hda_amp_info *
1534get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1535{
1536 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1537}
1538
Takashi Iwaid5191e52009-11-16 14:58:17 +01001539/**
1540 * query_amp_caps - query AMP capabilities
1541 * @codec: the HD-auio codec
1542 * @nid: the NID to query
1543 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1544 *
1545 * Query AMP capabilities for the given widget and direction.
1546 * Returns the obtained capability bits.
1547 *
1548 * When cap bits have been already read, this doesn't read again but
1549 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001551u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001553 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Takashi Iwai0ba21762007-04-16 11:29:14 +02001555 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1556 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001558 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001559 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001561 info->amp_caps = snd_hda_param_read(codec, nid,
1562 direction == HDA_OUTPUT ?
1563 AC_PAR_AMP_OUT_CAP :
1564 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001565 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001566 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 return info->amp_caps;
1569}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001570EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Takashi Iwaid5191e52009-11-16 14:58:17 +01001572/**
1573 * snd_hda_override_amp_caps - Override the AMP capabilities
1574 * @codec: the CODEC to clean up
1575 * @nid: the NID to clean up
1576 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1577 * @caps: the capability bits to set
1578 *
1579 * Override the cached AMP caps bits value by the given one.
1580 * This function is useful if the driver needs to adjust the AMP ranges,
1581 * e.g. limit to 0dB, etc.
1582 *
1583 * Returns zero if successful or a negative error code.
1584 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001585int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1586 unsigned int caps)
1587{
1588 struct hda_amp_info *info;
1589
1590 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1591 if (!info)
1592 return -EINVAL;
1593 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001594 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001595 return 0;
1596}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001597EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001598
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001599static unsigned int
1600query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1601 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001602{
1603 struct hda_amp_info *info;
1604
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001605 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001606 if (!info)
1607 return 0;
1608 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001609 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001610 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001611 }
1612 return info->amp_caps;
1613}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001614
1615static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1616{
1617 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1618}
1619
Takashi Iwaid5191e52009-11-16 14:58:17 +01001620/**
1621 * snd_hda_query_pin_caps - Query PIN capabilities
1622 * @codec: the HD-auio codec
1623 * @nid: the NID to query
1624 *
1625 * Query PIN capabilities for the given widget.
1626 * Returns the obtained capability bits.
1627 *
1628 * When cap bits have been already read, this doesn't read again but
1629 * returns the cached value.
1630 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001631u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1632{
1633 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1634 read_pin_cap);
1635}
Takashi Iwai1327a322009-03-23 13:07:47 +01001636EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1637
Wu Fengguang864f92b2009-11-18 12:38:02 +08001638/**
1639 * snd_hda_pin_sense - execute pin sense measurement
1640 * @codec: the CODEC to sense
1641 * @nid: the pin NID to sense
1642 *
1643 * Execute necessary pin sense measurement and return its Presence Detect,
1644 * Impedance, ELD Valid etc. status bits.
1645 */
1646u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1647{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001648 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001649
Takashi Iwai729d55b2009-12-25 22:49:01 +01001650 if (!codec->no_trigger_sense) {
1651 pincap = snd_hda_query_pin_caps(codec, nid);
1652 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001653 snd_hda_codec_read(codec, nid, 0,
1654 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001655 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001656 return snd_hda_codec_read(codec, nid, 0,
1657 AC_VERB_GET_PIN_SENSE, 0);
1658}
1659EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1660
1661/**
1662 * snd_hda_jack_detect - query pin Presence Detect status
1663 * @codec: the CODEC to sense
1664 * @nid: the pin NID to sense
1665 *
1666 * Query and return the pin's Presence Detect status.
1667 */
1668int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1669{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001670 u32 sense = snd_hda_pin_sense(codec, nid);
1671 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001672}
1673EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675/*
1676 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001677 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001679static unsigned int get_vol_mute(struct hda_codec *codec,
1680 struct hda_amp_info *info, hda_nid_t nid,
1681 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 u32 val, parm;
1684
Takashi Iwai01751f52007-08-10 16:59:39 +02001685 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001686 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1689 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1690 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001691 val = snd_hda_codec_read(codec, nid, 0,
1692 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001694 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001695 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
1698/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001699 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001701static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001702 hda_nid_t nid, int ch, int direction, int index,
1703 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 u32 parm;
1706
1707 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1708 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1709 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1710 parm |= val;
1711 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001712 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
Takashi Iwaid5191e52009-11-16 14:58:17 +01001715/**
1716 * snd_hda_codec_amp_read - Read AMP value
1717 * @codec: HD-audio codec
1718 * @nid: NID to read the AMP value
1719 * @ch: channel (left=0 or right=1)
1720 * @direction: #HDA_INPUT or #HDA_OUTPUT
1721 * @index: the index value (only for input direction)
1722 *
1723 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
Takashi Iwai834be882006-03-01 14:16:17 +01001725int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1726 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001728 struct hda_amp_info *info;
1729 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1730 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001732 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001734EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Takashi Iwaid5191e52009-11-16 14:58:17 +01001736/**
1737 * snd_hda_codec_amp_update - update the AMP value
1738 * @codec: HD-audio codec
1739 * @nid: NID to read the AMP value
1740 * @ch: channel (left=0 or right=1)
1741 * @direction: #HDA_INPUT or #HDA_OUTPUT
1742 * @idx: the index value (only for input direction)
1743 * @mask: bit mask to set
1744 * @val: the bits value to set
1745 *
1746 * Update the AMP value with a bit mask.
1747 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001748 */
Takashi Iwai834be882006-03-01 14:16:17 +01001749int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1750 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001752 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001753
Takashi Iwai0ba21762007-04-16 11:29:14 +02001754 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1755 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001757 if (snd_BUG_ON(mask & ~0xff))
1758 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001759 val &= mask;
1760 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001761 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001763 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return 1;
1765}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001766EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Takashi Iwaid5191e52009-11-16 14:58:17 +01001768/**
1769 * snd_hda_codec_amp_stereo - update the AMP stereo values
1770 * @codec: HD-audio codec
1771 * @nid: NID to read the AMP value
1772 * @direction: #HDA_INPUT or #HDA_OUTPUT
1773 * @idx: the index value (only for input direction)
1774 * @mask: bit mask to set
1775 * @val: the bits value to set
1776 *
1777 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1778 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001779 */
1780int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1781 int direction, int idx, int mask, int val)
1782{
1783 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001784
1785 if (snd_BUG_ON(mask & ~0xff))
1786 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001787 for (ch = 0; ch < 2; ch++)
1788 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1789 idx, mask, val);
1790 return ret;
1791}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001792EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001793
Takashi Iwaicb53c622007-08-10 17:21:45 +02001794#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001795/**
1796 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1797 * @codec: HD-audio codec
1798 *
1799 * Resume the all amp commands from the cache.
1800 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001801void snd_hda_codec_resume_amp(struct hda_codec *codec)
1802{
Takashi Iwai603c4012008-07-30 15:01:44 +02001803 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001804 int i;
1805
Takashi Iwai603c4012008-07-30 15:01:44 +02001806 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001807 u32 key = buffer->head.key;
1808 hda_nid_t nid;
1809 unsigned int idx, dir, ch;
1810 if (!key)
1811 continue;
1812 nid = key & 0xff;
1813 idx = (key >> 16) & 0xff;
1814 dir = (key >> 24) & 0xff;
1815 for (ch = 0; ch < 2; ch++) {
1816 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1817 continue;
1818 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1819 buffer->vol[ch]);
1820 }
1821 }
1822}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001823EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001824#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001826static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1827 unsigned int ofs)
1828{
1829 u32 caps = query_amp_caps(codec, nid, dir);
1830 /* get num steps */
1831 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1832 if (ofs < caps)
1833 caps -= ofs;
1834 return caps;
1835}
1836
Takashi Iwaid5191e52009-11-16 14:58:17 +01001837/**
1838 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1839 *
1840 * The control element is supposed to have the private_value field
1841 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1842 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001843int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1844 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1847 u16 nid = get_amp_nid(kcontrol);
1848 u8 chs = get_amp_channels(kcontrol);
1849 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001850 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001852 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1853 uinfo->count = chs == 3 ? 2 : 1;
1854 uinfo->value.integer.min = 0;
1855 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1856 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001857 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001858 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1859 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return -EINVAL;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return 0;
1863}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001864EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001866
1867static inline unsigned int
1868read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1869 int ch, int dir, int idx, unsigned int ofs)
1870{
1871 unsigned int val;
1872 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1873 val &= HDA_AMP_VOLMASK;
1874 if (val >= ofs)
1875 val -= ofs;
1876 else
1877 val = 0;
1878 return val;
1879}
1880
1881static inline int
1882update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1883 int ch, int dir, int idx, unsigned int ofs,
1884 unsigned int val)
1885{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001886 unsigned int maxval;
1887
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001888 if (val > 0)
1889 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001890 /* ofs = 0: raw max value */
1891 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001892 if (val > maxval)
1893 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001894 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1895 HDA_AMP_VOLMASK, val);
1896}
1897
Takashi Iwaid5191e52009-11-16 14:58:17 +01001898/**
1899 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1900 *
1901 * The control element is supposed to have the private_value field
1902 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1903 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001904int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1905 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1908 hda_nid_t nid = get_amp_nid(kcontrol);
1909 int chs = get_amp_channels(kcontrol);
1910 int dir = get_amp_direction(kcontrol);
1911 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001912 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 long *valp = ucontrol->value.integer.value;
1914
1915 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001916 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001918 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return 0;
1920}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001921EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Takashi Iwaid5191e52009-11-16 14:58:17 +01001923/**
1924 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1925 *
1926 * The control element is supposed to have the private_value field
1927 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1928 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001929int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1930 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1933 hda_nid_t nid = get_amp_nid(kcontrol);
1934 int chs = get_amp_channels(kcontrol);
1935 int dir = get_amp_direction(kcontrol);
1936 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001937 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 long *valp = ucontrol->value.integer.value;
1939 int change = 0;
1940
Takashi Iwaicb53c622007-08-10 17:21:45 +02001941 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001942 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001943 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001944 valp++;
1945 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001946 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001947 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001948 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return change;
1950}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001951EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Takashi Iwaid5191e52009-11-16 14:58:17 +01001953/**
1954 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1955 *
1956 * The control element is supposed to have the private_value field
1957 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1958 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001959int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1960 unsigned int size, unsigned int __user *_tlv)
1961{
1962 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1963 hda_nid_t nid = get_amp_nid(kcontrol);
1964 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001965 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001966 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001967 u32 caps, val1, val2;
1968
1969 if (size < 4 * sizeof(unsigned int))
1970 return -ENOMEM;
1971 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001972 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1973 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001974 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001975 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001976 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001977 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02001978 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001979 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1980 return -EFAULT;
1981 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1982 return -EFAULT;
1983 if (put_user(val1, _tlv + 2))
1984 return -EFAULT;
1985 if (put_user(val2, _tlv + 3))
1986 return -EFAULT;
1987 return 0;
1988}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001989EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001990
Takashi Iwaid5191e52009-11-16 14:58:17 +01001991/**
1992 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1993 * @codec: HD-audio codec
1994 * @nid: NID of a reference widget
1995 * @dir: #HDA_INPUT or #HDA_OUTPUT
1996 * @tlv: TLV data to be stored, at least 4 elements
1997 *
1998 * Set (static) TLV data for a virtual master volume using the AMP caps
1999 * obtained from the reference NID.
2000 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002001 */
2002void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2003 unsigned int *tlv)
2004{
2005 u32 caps;
2006 int nums, step;
2007
2008 caps = query_amp_caps(codec, nid, dir);
2009 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2010 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2011 step = (step + 1) * 25;
2012 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2013 tlv[1] = 2 * sizeof(unsigned int);
2014 tlv[2] = -nums * step;
2015 tlv[3] = step;
2016}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002017EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002018
2019/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002020static struct snd_kcontrol *
2021_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2022 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002023{
2024 struct snd_ctl_elem_id id;
2025 memset(&id, 0, sizeof(id));
2026 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002027 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002028 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2029 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002030 strcpy(id.name, name);
2031 return snd_ctl_find_id(codec->bus->card, &id);
2032}
2033
Takashi Iwaid5191e52009-11-16 14:58:17 +01002034/**
2035 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2036 * @codec: HD-audio codec
2037 * @name: ctl id name string
2038 *
2039 * Get the control element with the given id string and IFACE_MIXER.
2040 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002041struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2042 const char *name)
2043{
2044 return _snd_hda_find_mixer_ctl(codec, name, 0);
2045}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002046EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002047
Takashi Iwai1afe2062010-12-23 10:17:52 +01002048static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2049{
2050 int idx;
2051 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2052 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2053 return idx;
2054 }
2055 return -EBUSY;
2056}
2057
Takashi Iwaid5191e52009-11-16 14:58:17 +01002058/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002059 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002060 * @codec: HD-audio codec
2061 * @nid: corresponding NID (optional)
2062 * @kctl: the control element to assign
2063 *
2064 * Add the given control element to an array inside the codec instance.
2065 * All control elements belonging to a codec are supposed to be added
2066 * by this function so that a proper clean-up works at the free or
2067 * reconfiguration time.
2068 *
2069 * If non-zero @nid is passed, the NID is assigned to the control element.
2070 * The assignment is shown in the codec proc file.
2071 *
2072 * snd_hda_ctl_add() checks the control subdev id field whether
2073 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002074 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2075 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002076 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002077int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2078 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002079{
2080 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002081 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002082 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002083
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002084 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002085 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002086 if (nid == 0)
2087 nid = get_amp_nid_(kctl->private_value);
2088 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002089 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2090 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002091 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002092 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002093 err = snd_ctl_add(codec->bus->card, kctl);
2094 if (err < 0)
2095 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002096 item = snd_array_new(&codec->mixers);
2097 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002098 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002099 item->kctl = kctl;
2100 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002101 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002102 return 0;
2103}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002104EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002105
Takashi Iwaid5191e52009-11-16 14:58:17 +01002106/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002107 * snd_hda_add_nid - Assign a NID to a control element
2108 * @codec: HD-audio codec
2109 * @nid: corresponding NID (optional)
2110 * @kctl: the control element to assign
2111 * @index: index to kctl
2112 *
2113 * Add the given control element to an array inside the codec instance.
2114 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2115 * NID:KCTL mapping - for example "Capture Source" selector.
2116 */
2117int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2118 unsigned int index, hda_nid_t nid)
2119{
2120 struct hda_nid_item *item;
2121
2122 if (nid > 0) {
2123 item = snd_array_new(&codec->nids);
2124 if (!item)
2125 return -ENOMEM;
2126 item->kctl = kctl;
2127 item->index = index;
2128 item->nid = nid;
2129 return 0;
2130 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002131 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2132 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002133 return -EINVAL;
2134}
2135EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2136
2137/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002138 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2139 * @codec: HD-audio codec
2140 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002141void snd_hda_ctls_clear(struct hda_codec *codec)
2142{
2143 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002144 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002145 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002146 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002147 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002148 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002149}
2150
Takashi Iwaia65d6292009-02-23 16:57:04 +01002151/* pseudo device locking
2152 * toggle card->shutdown to allow/disallow the device access (as a hack)
2153 */
2154static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002155{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002156 spin_lock(&card->files_lock);
2157 if (card->shutdown) {
2158 spin_unlock(&card->files_lock);
2159 return -EINVAL;
2160 }
2161 card->shutdown = 1;
2162 spin_unlock(&card->files_lock);
2163 return 0;
2164}
2165
2166static void hda_unlock_devices(struct snd_card *card)
2167{
2168 spin_lock(&card->files_lock);
2169 card->shutdown = 0;
2170 spin_unlock(&card->files_lock);
2171}
2172
Takashi Iwaid5191e52009-11-16 14:58:17 +01002173/**
2174 * snd_hda_codec_reset - Clear all objects assigned to the codec
2175 * @codec: HD-audio codec
2176 *
2177 * This frees the all PCM and control elements assigned to the codec, and
2178 * clears the caches and restores the pin default configurations.
2179 *
2180 * When a device is being used, it returns -EBSY. If successfully freed,
2181 * returns zero.
2182 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002183int snd_hda_codec_reset(struct hda_codec *codec)
2184{
2185 struct snd_card *card = codec->bus->card;
2186 int i, pcm;
2187
2188 if (hda_lock_devices(card) < 0)
2189 return -EBUSY;
2190 /* check whether the codec isn't used by any mixer or PCM streams */
2191 if (!list_empty(&card->ctl_files)) {
2192 hda_unlock_devices(card);
2193 return -EBUSY;
2194 }
2195 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2196 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2197 if (!cpcm->pcm)
2198 continue;
2199 if (cpcm->pcm->streams[0].substream_opened ||
2200 cpcm->pcm->streams[1].substream_opened) {
2201 hda_unlock_devices(card);
2202 return -EBUSY;
2203 }
2204 }
2205
2206 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002207
2208#ifdef CONFIG_SND_HDA_POWER_SAVE
2209 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002210 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002211#endif
2212 snd_hda_ctls_clear(codec);
2213 /* relase PCMs */
2214 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002215 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002216 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002217 clear_bit(codec->pcm_info[i].device,
2218 codec->bus->pcm_dev_bits);
2219 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002220 }
2221 if (codec->patch_ops.free)
2222 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002223 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002224 codec->spec = NULL;
2225 free_hda_cache(&codec->amp_cache);
2226 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002227 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2228 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002229 /* free only driver_pins so that init_pins + user_pins are restored */
2230 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002231 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002232 codec->num_pcms = 0;
2233 codec->pcm_info = NULL;
2234 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002235 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2236 codec->slave_dig_outs = NULL;
2237 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002238 module_put(codec->owner);
2239 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002240
2241 /* allow device access again */
2242 hda_unlock_devices(card);
2243 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002244}
2245
Takashi Iwaid5191e52009-11-16 14:58:17 +01002246/**
2247 * snd_hda_add_vmaster - create a virtual master control and add slaves
2248 * @codec: HD-audio codec
2249 * @name: vmaster control name
2250 * @tlv: TLV data (optional)
2251 * @slaves: slave control names (optional)
2252 *
2253 * Create a virtual master control with the given name. The TLV data
2254 * must be either NULL or a valid data.
2255 *
2256 * @slaves is a NULL-terminated array of strings, each of which is a
2257 * slave control name. All controls with these names are assigned to
2258 * the new virtual master control.
2259 *
2260 * This function returns zero if successful or a negative error code.
2261 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002262int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002263 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002264{
2265 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002266 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002267 int err;
2268
Takashi Iwai2f085542008-02-22 18:43:50 +01002269 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2270 ;
2271 if (!*s) {
2272 snd_printdd("No slave found for %s\n", name);
2273 return 0;
2274 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002275 kctl = snd_ctl_make_virtual_master(name, tlv);
2276 if (!kctl)
2277 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002278 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002279 if (err < 0)
2280 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002281
Takashi Iwai2134ea42008-01-10 16:53:55 +01002282 for (s = slaves; *s; s++) {
2283 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002284 int i = 0;
2285 for (;;) {
2286 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2287 if (!sctl) {
2288 if (!i)
2289 snd_printdd("Cannot find slave %s, "
2290 "skipped\n", *s);
2291 break;
2292 }
2293 err = snd_ctl_add_slave(kctl, sctl);
2294 if (err < 0)
2295 return err;
2296 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002297 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002298 }
2299 return 0;
2300}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002301EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002302
Takashi Iwaid5191e52009-11-16 14:58:17 +01002303/**
2304 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2305 *
2306 * The control element is supposed to have the private_value field
2307 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2308 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002309int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2310 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
2312 int chs = get_amp_channels(kcontrol);
2313
2314 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2315 uinfo->count = chs == 3 ? 2 : 1;
2316 uinfo->value.integer.min = 0;
2317 uinfo->value.integer.max = 1;
2318 return 0;
2319}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002320EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Takashi Iwaid5191e52009-11-16 14:58:17 +01002322/**
2323 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2324 *
2325 * The control element is supposed to have the private_value field
2326 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2327 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002328int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2329 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2332 hda_nid_t nid = get_amp_nid(kcontrol);
2333 int chs = get_amp_channels(kcontrol);
2334 int dir = get_amp_direction(kcontrol);
2335 int idx = get_amp_index(kcontrol);
2336 long *valp = ucontrol->value.integer.value;
2337
2338 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002339 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002340 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002342 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002343 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return 0;
2345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002346EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Takashi Iwaid5191e52009-11-16 14:58:17 +01002348/**
2349 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2350 *
2351 * The control element is supposed to have the private_value field
2352 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2353 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002354int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
2357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2358 hda_nid_t nid = get_amp_nid(kcontrol);
2359 int chs = get_amp_channels(kcontrol);
2360 int dir = get_amp_direction(kcontrol);
2361 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 long *valp = ucontrol->value.integer.value;
2363 int change = 0;
2364
Takashi Iwaicb53c622007-08-10 17:21:45 +02002365 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002366 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002367 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002368 HDA_AMP_MUTE,
2369 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002370 valp++;
2371 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002372 if (chs & 2)
2373 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002374 HDA_AMP_MUTE,
2375 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002376 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002377 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return change;
2379}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002380EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Takashi Iwai67d634c2009-11-16 15:35:59 +01002382#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002383/**
2384 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2385 *
2386 * This function calls snd_hda_enable_beep_device(), which behaves differently
2387 * depending on beep_mode option.
2388 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002389int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2390 struct snd_ctl_elem_value *ucontrol)
2391{
2392 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2393 long *valp = ucontrol->value.integer.value;
2394
2395 snd_hda_enable_beep_device(codec, *valp);
2396 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2397}
2398EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002399#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401/*
Takashi Iwai985be542005-11-02 18:26:49 +01002402 * bound volume controls
2403 *
2404 * bind multiple volumes (# indices, from 0)
2405 */
2406
2407#define AMP_VAL_IDX_SHIFT 19
2408#define AMP_VAL_IDX_MASK (0x0f<<19)
2409
Takashi Iwaid5191e52009-11-16 14:58:17 +01002410/**
2411 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2412 *
2413 * The control element is supposed to have the private_value field
2414 * set up via HDA_BIND_MUTE*() macros.
2415 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002416int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002418{
2419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2420 unsigned long pval;
2421 int err;
2422
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002423 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002424 pval = kcontrol->private_value;
2425 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2426 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2427 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002428 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002429 return err;
2430}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002431EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002432
Takashi Iwaid5191e52009-11-16 14:58:17 +01002433/**
2434 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2435 *
2436 * The control element is supposed to have the private_value field
2437 * set up via HDA_BIND_MUTE*() macros.
2438 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002439int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2440 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002441{
2442 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2443 unsigned long pval;
2444 int i, indices, err = 0, change = 0;
2445
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002446 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002447 pval = kcontrol->private_value;
2448 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2449 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002450 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2451 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002452 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2453 if (err < 0)
2454 break;
2455 change |= err;
2456 }
2457 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002458 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002459 return err < 0 ? err : change;
2460}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002461EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002462
Takashi Iwaid5191e52009-11-16 14:58:17 +01002463/**
2464 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2465 *
2466 * The control element is supposed to have the private_value field
2467 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002468 */
2469int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2470 struct snd_ctl_elem_info *uinfo)
2471{
2472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2473 struct hda_bind_ctls *c;
2474 int err;
2475
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002476 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002477 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002478 kcontrol->private_value = *c->values;
2479 err = c->ops->info(kcontrol, uinfo);
2480 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002481 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002482 return err;
2483}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002484EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002485
Takashi Iwaid5191e52009-11-16 14:58:17 +01002486/**
2487 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2488 *
2489 * The control element is supposed to have the private_value field
2490 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2491 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002492int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2493 struct snd_ctl_elem_value *ucontrol)
2494{
2495 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2496 struct hda_bind_ctls *c;
2497 int err;
2498
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002499 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002500 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002501 kcontrol->private_value = *c->values;
2502 err = c->ops->get(kcontrol, ucontrol);
2503 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002504 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002505 return err;
2506}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002507EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002508
Takashi Iwaid5191e52009-11-16 14:58:17 +01002509/**
2510 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2511 *
2512 * The control element is supposed to have the private_value field
2513 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2514 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002515int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2516 struct snd_ctl_elem_value *ucontrol)
2517{
2518 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2519 struct hda_bind_ctls *c;
2520 unsigned long *vals;
2521 int err = 0, change = 0;
2522
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002523 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002524 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002525 for (vals = c->values; *vals; vals++) {
2526 kcontrol->private_value = *vals;
2527 err = c->ops->put(kcontrol, ucontrol);
2528 if (err < 0)
2529 break;
2530 change |= err;
2531 }
2532 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002533 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002534 return err < 0 ? err : change;
2535}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002536EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002537
Takashi Iwaid5191e52009-11-16 14:58:17 +01002538/**
2539 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2540 *
2541 * The control element is supposed to have the private_value field
2542 * set up via HDA_BIND_VOL() macro.
2543 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002544int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2545 unsigned int size, unsigned int __user *tlv)
2546{
2547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2548 struct hda_bind_ctls *c;
2549 int err;
2550
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002551 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002552 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002553 kcontrol->private_value = *c->values;
2554 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2555 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002556 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002557 return err;
2558}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002559EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002560
2561struct hda_ctl_ops snd_hda_bind_vol = {
2562 .info = snd_hda_mixer_amp_volume_info,
2563 .get = snd_hda_mixer_amp_volume_get,
2564 .put = snd_hda_mixer_amp_volume_put,
2565 .tlv = snd_hda_mixer_amp_tlv
2566};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002567EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002568
2569struct hda_ctl_ops snd_hda_bind_sw = {
2570 .info = snd_hda_mixer_amp_switch_info,
2571 .get = snd_hda_mixer_amp_switch_get,
2572 .put = snd_hda_mixer_amp_switch_put,
2573 .tlv = snd_hda_mixer_amp_tlv
2574};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002575EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002576
2577/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 * SPDIF out controls
2579 */
2580
Takashi Iwai0ba21762007-04-16 11:29:14 +02002581static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2582 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
2584 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2585 uinfo->count = 1;
2586 return 0;
2587}
2588
Takashi Iwai0ba21762007-04-16 11:29:14 +02002589static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2590 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
2592 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2593 IEC958_AES0_NONAUDIO |
2594 IEC958_AES0_CON_EMPHASIS_5015 |
2595 IEC958_AES0_CON_NOT_COPYRIGHT;
2596 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2597 IEC958_AES1_CON_ORIGINAL;
2598 return 0;
2599}
2600
Takashi Iwai0ba21762007-04-16 11:29:14 +02002601static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2602 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2605 IEC958_AES0_NONAUDIO |
2606 IEC958_AES0_PRO_EMPHASIS_5015;
2607 return 0;
2608}
2609
Takashi Iwai0ba21762007-04-16 11:29:14 +02002610static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002614 int idx = kcontrol->private_value;
2615 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Stephen Warren7c9359762011-06-01 11:14:17 -06002617 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2618 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2619 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2620 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 return 0;
2623}
2624
2625/* convert from SPDIF status bits to HDA SPDIF bits
2626 * bit 0 (DigEn) is always set zero (to be filled later)
2627 */
2628static unsigned short convert_from_spdif_status(unsigned int sbits)
2629{
2630 unsigned short val = 0;
2631
2632 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002633 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002635 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002637 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2638 IEC958_AES0_PRO_EMPHASIS_5015)
2639 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002641 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2642 IEC958_AES0_CON_EMPHASIS_5015)
2643 val |= AC_DIG1_EMPHASIS;
2644 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2645 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002647 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2649 }
2650 return val;
2651}
2652
2653/* convert to SPDIF status bits from HDA SPDIF bits
2654 */
2655static unsigned int convert_to_spdif_status(unsigned short val)
2656{
2657 unsigned int sbits = 0;
2658
Takashi Iwai0ba21762007-04-16 11:29:14 +02002659 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002661 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 sbits |= IEC958_AES0_PROFESSIONAL;
2663 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002664 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2666 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002667 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002669 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002671 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2673 sbits |= val & (0x7f << 8);
2674 }
2675 return sbits;
2676}
2677
Takashi Iwai2f728532008-09-25 16:32:41 +02002678/* set digital convert verbs both for the given NID and its slaves */
2679static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2680 int verb, int val)
2681{
Takashi Iwaidda14412011-05-02 11:29:30 +02002682 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002683
Takashi Iwai9e976972008-11-25 08:17:20 +01002684 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002685 d = codec->slave_dig_outs;
2686 if (!d)
2687 return;
2688 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002689 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002690}
2691
2692static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2693 int dig1, int dig2)
2694{
2695 if (dig1 != -1)
2696 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2697 if (dig2 != -1)
2698 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2699}
2700
Takashi Iwai0ba21762007-04-16 11:29:14 +02002701static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2702 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
2704 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002705 int idx = kcontrol->private_value;
2706 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2707 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 unsigned short val;
2709 int change;
2710
Ingo Molnar62932df2006-01-16 16:34:20 +01002711 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002712 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2714 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2715 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002716 val = convert_from_spdif_status(spdif->status);
2717 val |= spdif->ctls & 1;
2718 change = spdif->ctls != val;
2719 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002720 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002721 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002722 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return change;
2724}
2725
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002726#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Takashi Iwai0ba21762007-04-16 11:29:14 +02002728static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002732 int idx = kcontrol->private_value;
2733 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Stephen Warren7c9359762011-06-01 11:14:17 -06002735 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return 0;
2737}
2738
Stephen Warren74b654c2011-06-01 11:14:18 -06002739static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2740 int dig1, int dig2)
2741{
2742 set_dig_out_convert(codec, nid, dig1, dig2);
2743 /* unmute amp switch (if any) */
2744 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2745 (dig1 & AC_DIG1_ENABLE))
2746 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2747 HDA_AMP_MUTE, 0);
2748}
2749
Takashi Iwai0ba21762007-04-16 11:29:14 +02002750static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2751 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
2753 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002754 int idx = kcontrol->private_value;
2755 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2756 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 unsigned short val;
2758 int change;
2759
Ingo Molnar62932df2006-01-16 16:34:20 +01002760 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002761 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002763 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002764 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002765 spdif->ctls = val;
2766 if (change && nid != (u16)-1)
2767 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002768 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return change;
2770}
2771
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002772static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 {
2774 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2775 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002776 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 .info = snd_hda_spdif_mask_info,
2778 .get = snd_hda_spdif_cmask_get,
2779 },
2780 {
2781 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2782 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002783 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 .info = snd_hda_spdif_mask_info,
2785 .get = snd_hda_spdif_pmask_get,
2786 },
2787 {
2788 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002789 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 .info = snd_hda_spdif_mask_info,
2791 .get = snd_hda_spdif_default_get,
2792 .put = snd_hda_spdif_default_put,
2793 },
2794 {
2795 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002796 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 .info = snd_hda_spdif_out_switch_info,
2798 .get = snd_hda_spdif_out_switch_get,
2799 .put = snd_hda_spdif_out_switch_put,
2800 },
2801 { } /* end */
2802};
2803
2804/**
2805 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2806 * @codec: the HDA codec
2807 * @nid: audio out widget NID
2808 *
2809 * Creates controls related with the SPDIF output.
2810 * Called from each patch supporting the SPDIF out.
2811 *
2812 * Returns 0 if successful, or a negative error code.
2813 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002814int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2815 hda_nid_t associated_nid,
2816 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
2818 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002819 struct snd_kcontrol *kctl;
2820 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002821 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002822 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Takashi Iwai1afe2062010-12-23 10:17:52 +01002824 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2825 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002826 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2827 return -EBUSY;
2828 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002829 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2831 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002832 if (!kctl)
2833 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002834 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002835 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002836 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002837 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return err;
2839 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002840 spdif->nid = cvt_nid;
2841 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06002842 AC_VERB_GET_DIGI_CONVERT_1, 0);
2843 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return 0;
2845}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002846EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Stephen Warren7c9359762011-06-01 11:14:17 -06002848struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2849 hda_nid_t nid)
2850{
2851 int i;
2852 for (i = 0; i < codec->spdif_out.used; i++) {
2853 struct hda_spdif_out *spdif =
2854 snd_array_elem(&codec->spdif_out, i);
2855 if (spdif->nid == nid)
2856 return spdif;
2857 }
2858 return NULL;
2859}
2860EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2861
Stephen Warren74b654c2011-06-01 11:14:18 -06002862void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2863{
2864 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2865
2866 mutex_lock(&codec->spdif_mutex);
2867 spdif->nid = (u16)-1;
2868 mutex_unlock(&codec->spdif_mutex);
2869}
2870EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2871
2872void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2873{
2874 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2875 unsigned short val;
2876
2877 mutex_lock(&codec->spdif_mutex);
2878 if (spdif->nid != nid) {
2879 spdif->nid = nid;
2880 val = spdif->ctls;
2881 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2882 }
2883 mutex_unlock(&codec->spdif_mutex);
2884}
2885EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002888 * SPDIF sharing with analog output
2889 */
2890static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2891 struct snd_ctl_elem_value *ucontrol)
2892{
2893 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2894 ucontrol->value.integer.value[0] = mout->share_spdif;
2895 return 0;
2896}
2897
2898static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2900{
2901 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2902 mout->share_spdif = !!ucontrol->value.integer.value[0];
2903 return 0;
2904}
2905
2906static struct snd_kcontrol_new spdif_share_sw = {
2907 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2908 .name = "IEC958 Default PCM Playback Switch",
2909 .info = snd_ctl_boolean_mono_info,
2910 .get = spdif_share_sw_get,
2911 .put = spdif_share_sw_put,
2912};
2913
Takashi Iwaid5191e52009-11-16 14:58:17 +01002914/**
2915 * snd_hda_create_spdif_share_sw - create Default PCM switch
2916 * @codec: the HDA codec
2917 * @mout: multi-out instance
2918 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002919int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2920 struct hda_multi_out *mout)
2921{
2922 if (!mout->dig_out_nid)
2923 return 0;
2924 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002925 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2926 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002927}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002928EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002929
2930/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 * SPDIF input
2932 */
2933
2934#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2935
Takashi Iwai0ba21762007-04-16 11:29:14 +02002936static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2937 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2940
2941 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2942 return 0;
2943}
2944
Takashi Iwai0ba21762007-04-16 11:29:14 +02002945static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2946 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2949 hda_nid_t nid = kcontrol->private_value;
2950 unsigned int val = !!ucontrol->value.integer.value[0];
2951 int change;
2952
Ingo Molnar62932df2006-01-16 16:34:20 +01002953 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002955 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002957 snd_hda_codec_write_cache(codec, nid, 0,
2958 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002960 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 return change;
2962}
2963
Takashi Iwai0ba21762007-04-16 11:29:14 +02002964static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2965 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
2967 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2968 hda_nid_t nid = kcontrol->private_value;
2969 unsigned short val;
2970 unsigned int sbits;
2971
Andrew Paprocki3982d172007-12-19 12:13:44 +01002972 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 sbits = convert_to_spdif_status(val);
2974 ucontrol->value.iec958.status[0] = sbits;
2975 ucontrol->value.iec958.status[1] = sbits >> 8;
2976 ucontrol->value.iec958.status[2] = sbits >> 16;
2977 ucontrol->value.iec958.status[3] = sbits >> 24;
2978 return 0;
2979}
2980
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002981static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 {
2983 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002984 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 .info = snd_hda_spdif_in_switch_info,
2986 .get = snd_hda_spdif_in_switch_get,
2987 .put = snd_hda_spdif_in_switch_put,
2988 },
2989 {
2990 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2991 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002992 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 .info = snd_hda_spdif_mask_info,
2994 .get = snd_hda_spdif_in_status_get,
2995 },
2996 { } /* end */
2997};
2998
2999/**
3000 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3001 * @codec: the HDA codec
3002 * @nid: audio in widget NID
3003 *
3004 * Creates controls related with the SPDIF input.
3005 * Called from each patch supporting the SPDIF in.
3006 *
3007 * Returns 0 if successful, or a negative error code.
3008 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003009int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
3011 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003012 struct snd_kcontrol *kctl;
3013 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003014 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Takashi Iwai1afe2062010-12-23 10:17:52 +01003016 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3017 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003018 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3019 return -EBUSY;
3020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3022 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003023 if (!kctl)
3024 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003026 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003027 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return err;
3029 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003030 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003031 snd_hda_codec_read(codec, nid, 0,
3032 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003033 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return 0;
3035}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003036EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Takashi Iwaicb53c622007-08-10 17:21:45 +02003038#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003039/*
3040 * command cache
3041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003043/* build a 32bit cache key with the widget id and the command parameter */
3044#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3045#define get_cmd_cache_nid(key) ((key) & 0xff)
3046#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3047
3048/**
3049 * snd_hda_codec_write_cache - send a single command with caching
3050 * @codec: the HDA codec
3051 * @nid: NID to send the command
3052 * @direct: direct flag
3053 * @verb: the verb to send
3054 * @parm: the parameter for the verb
3055 *
3056 * Send a single command without waiting for response.
3057 *
3058 * Returns 0 if successful, or a negative error code.
3059 */
3060int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3061 int direct, unsigned int verb, unsigned int parm)
3062{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003063 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3064 struct hda_cache_head *c;
3065 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003066
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003067 if (err < 0)
3068 return err;
3069 /* parm may contain the verb stuff for get/set amp */
3070 verb = verb | (parm >> 8);
3071 parm &= 0xff;
3072 key = build_cmd_cache_key(nid, verb);
3073 mutex_lock(&codec->bus->cmd_mutex);
3074 c = get_alloc_hash(&codec->cmd_cache, key);
3075 if (c)
3076 c->val = parm;
3077 mutex_unlock(&codec->bus->cmd_mutex);
3078 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003079}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003080EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003081
Takashi Iwaid5191e52009-11-16 14:58:17 +01003082/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003083 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3084 * @codec: the HDA codec
3085 * @nid: NID to send the command
3086 * @direct: direct flag
3087 * @verb: the verb to send
3088 * @parm: the parameter for the verb
3089 *
3090 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3091 * command if the parameter is already identical with the cached value.
3092 * If not, it sends the command and refreshes the cache.
3093 *
3094 * Returns 0 if successful, or a negative error code.
3095 */
3096int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3097 int direct, unsigned int verb, unsigned int parm)
3098{
3099 struct hda_cache_head *c;
3100 u32 key;
3101
3102 /* parm may contain the verb stuff for get/set amp */
3103 verb = verb | (parm >> 8);
3104 parm &= 0xff;
3105 key = build_cmd_cache_key(nid, verb);
3106 mutex_lock(&codec->bus->cmd_mutex);
3107 c = get_hash(&codec->cmd_cache, key);
3108 if (c && c->val == parm) {
3109 mutex_unlock(&codec->bus->cmd_mutex);
3110 return 0;
3111 }
3112 mutex_unlock(&codec->bus->cmd_mutex);
3113 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3114}
3115EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3116
3117/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003118 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3119 * @codec: HD-audio codec
3120 *
3121 * Execute all verbs recorded in the command caches to resume.
3122 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003123void snd_hda_codec_resume_cache(struct hda_codec *codec)
3124{
Takashi Iwai603c4012008-07-30 15:01:44 +02003125 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003126 int i;
3127
Takashi Iwai603c4012008-07-30 15:01:44 +02003128 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003129 u32 key = buffer->key;
3130 if (!key)
3131 continue;
3132 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3133 get_cmd_cache_cmd(key), buffer->val);
3134 }
3135}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003136EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003137
3138/**
3139 * snd_hda_sequence_write_cache - sequence writes with caching
3140 * @codec: the HDA codec
3141 * @seq: VERB array to send
3142 *
3143 * Send the commands sequentially from the given array.
3144 * Thte commands are recorded on cache for power-save and resume.
3145 * The array must be terminated with NID=0.
3146 */
3147void snd_hda_sequence_write_cache(struct hda_codec *codec,
3148 const struct hda_verb *seq)
3149{
3150 for (; seq->nid; seq++)
3151 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3152 seq->param);
3153}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003154EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003155#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003156
Takashi Iwai54d17402005-11-21 16:33:22 +01003157/*
3158 * set power state of the codec
3159 */
3160static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3161 unsigned int power_state)
3162{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003163 hda_nid_t nid;
3164 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003165
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003166 /* this delay seems necessary to avoid click noise at power-down */
3167 if (power_state == AC_PWRST_D3)
3168 msleep(100);
3169 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003170 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003171 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003172 if (power_state == AC_PWRST_D0 &&
3173 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003174 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003175
Takashi Iwaicb53c622007-08-10 17:21:45 +02003176 nid = codec->start_nid;
3177 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003178 unsigned int wcaps = get_wcaps(codec, nid);
3179 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003180 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003181 if (power_state == AC_PWRST_D3 &&
3182 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003183 unsigned int pincap;
3184 /*
3185 * don't power down the widget if it controls
3186 * eapd and EAPD_BTLENABLE is set.
3187 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003188 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003189 if (pincap & AC_PINCAP_EAPD) {
3190 int eapd = snd_hda_codec_read(codec,
3191 nid, 0,
3192 AC_VERB_GET_EAPD_BTLENABLE, 0);
3193 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003194 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003195 continue;
3196 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003197 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003198 snd_hda_codec_write(codec, nid, 0,
3199 AC_VERB_SET_POWER_STATE,
3200 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003201 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003202 }
3203
Takashi Iwaicb53c622007-08-10 17:21:45 +02003204 if (power_state == AC_PWRST_D0) {
3205 unsigned long end_time;
3206 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003207 /* wait until the codec reachs to D0 */
3208 end_time = jiffies + msecs_to_jiffies(500);
3209 do {
3210 state = snd_hda_codec_read(codec, fg, 0,
3211 AC_VERB_GET_POWER_STATE, 0);
3212 if (state == power_state)
3213 break;
3214 msleep(1);
3215 } while (time_after_eq(end_time, jiffies));
3216 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003217}
3218
Takashi Iwai11aeff02008-07-30 15:01:46 +02003219#ifdef CONFIG_SND_HDA_HWDEP
3220/* execute additional init verbs */
3221static void hda_exec_init_verbs(struct hda_codec *codec)
3222{
3223 if (codec->init_verbs.list)
3224 snd_hda_sequence_write(codec, codec->init_verbs.list);
3225}
3226#else
3227static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3228#endif
3229
Takashi Iwaicb53c622007-08-10 17:21:45 +02003230#ifdef SND_HDA_NEEDS_RESUME
3231/*
3232 * call suspend and power-down; used both from PM and power-save
3233 */
3234static void hda_call_codec_suspend(struct hda_codec *codec)
3235{
3236 if (codec->patch_ops.suspend)
3237 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003238 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003239 hda_set_power_state(codec,
3240 codec->afg ? codec->afg : codec->mfg,
3241 AC_PWRST_D3);
3242#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003243 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003244 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003245 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003246 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003247 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003248#endif
3249}
3250
3251/*
3252 * kick up codec; used both from PM and power-save
3253 */
3254static void hda_call_codec_resume(struct hda_codec *codec)
3255{
3256 hda_set_power_state(codec,
3257 codec->afg ? codec->afg : codec->mfg,
3258 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003259 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003260 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003261 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003262 if (codec->patch_ops.resume)
3263 codec->patch_ops.resume(codec);
3264 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003265 if (codec->patch_ops.init)
3266 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003267 snd_hda_codec_resume_amp(codec);
3268 snd_hda_codec_resume_cache(codec);
3269 }
3270}
3271#endif /* SND_HDA_NEEDS_RESUME */
3272
Takashi Iwai54d17402005-11-21 16:33:22 +01003273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274/**
3275 * snd_hda_build_controls - build mixer controls
3276 * @bus: the BUS
3277 *
3278 * Creates mixer controls for each codec included in the bus.
3279 *
3280 * Returns 0 if successful, otherwise a negative error code.
3281 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003282int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003284 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Takashi Iwai0ba21762007-04-16 11:29:14 +02003286 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003287 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003288 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003289 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003290 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003291 err = snd_hda_codec_reset(codec);
3292 if (err < 0) {
3293 printk(KERN_ERR
3294 "hda_codec: cannot revert codec\n");
3295 return err;
3296 }
3297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003299 return 0;
3300}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003301EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003303int snd_hda_codec_build_controls(struct hda_codec *codec)
3304{
3305 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003306 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003307 /* continue to initialize... */
3308 if (codec->patch_ops.init)
3309 err = codec->patch_ops.init(codec);
3310 if (!err && codec->patch_ops.build_controls)
3311 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003312 if (err < 0)
3313 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 return 0;
3315}
3316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317/*
3318 * stream formats
3319 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003320struct hda_rate_tbl {
3321 unsigned int hz;
3322 unsigned int alsa_bits;
3323 unsigned int hda_fmt;
3324};
3325
Takashi Iwai92f10b32010-08-03 14:21:00 +02003326/* rate = base * mult / div */
3327#define HDA_RATE(base, mult, div) \
3328 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3329 (((div) - 1) << AC_FMT_DIV_SHIFT))
3330
Takashi Iwaibefdf312005-08-22 13:57:55 +02003331static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003333
3334 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003335 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3336 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3337 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3338 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3339 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3340 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3341 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3342 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3343 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3344 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3345 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003346#define AC_PAR_PCM_RATE_BITS 11
3347 /* up to bits 10, 384kHZ isn't supported properly */
3348
3349 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003350 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003351
Takashi Iwaibefdf312005-08-22 13:57:55 +02003352 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353};
3354
3355/**
3356 * snd_hda_calc_stream_format - calculate format bitset
3357 * @rate: the sample rate
3358 * @channels: the number of channels
3359 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3360 * @maxbps: the max. bps
3361 *
3362 * Calculate the format bitset from the given rate, channels and th PCM format.
3363 *
3364 * Return zero if invalid.
3365 */
3366unsigned int snd_hda_calc_stream_format(unsigned int rate,
3367 unsigned int channels,
3368 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003369 unsigned int maxbps,
3370 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 int i;
3373 unsigned int val = 0;
3374
Takashi Iwaibefdf312005-08-22 13:57:55 +02003375 for (i = 0; rate_bits[i].hz; i++)
3376 if (rate_bits[i].hz == rate) {
3377 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 break;
3379 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003380 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 snd_printdd("invalid rate %d\n", rate);
3382 return 0;
3383 }
3384
3385 if (channels == 0 || channels > 8) {
3386 snd_printdd("invalid channels %d\n", channels);
3387 return 0;
3388 }
3389 val |= channels - 1;
3390
3391 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003392 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003393 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003394 break;
3395 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003396 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 case 20:
3399 case 24:
3400 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003401 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003402 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003404 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003406 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 break;
3408 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003409 snd_printdd("invalid format width %d\n",
3410 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return 0;
3412 }
3413
Anssi Hannula32c168c2010-08-03 13:28:57 +03003414 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003415 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 return val;
3418}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003419EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003421static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3422{
3423 unsigned int val = 0;
3424 if (nid != codec->afg &&
3425 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3426 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3427 if (!val || val == -1)
3428 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3429 if (!val || val == -1)
3430 return 0;
3431 return val;
3432}
3433
3434static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3435{
3436 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3437 get_pcm_param);
3438}
3439
3440static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3441{
3442 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3443 if (!streams || streams == -1)
3444 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3445 if (!streams || streams == -1)
3446 return 0;
3447 return streams;
3448}
3449
3450static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3451{
3452 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3453 get_stream_param);
3454}
3455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456/**
3457 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3458 * @codec: the HDA codec
3459 * @nid: NID to query
3460 * @ratesp: the pointer to store the detected rate bitflags
3461 * @formatsp: the pointer to store the detected formats
3462 * @bpsp: the pointer to store the detected format widths
3463 *
3464 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3465 * or @bsps argument is ignored.
3466 *
3467 * Returns 0 if successful, otherwise a negative error code.
3468 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003469int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3471{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003472 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003474 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003475 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 if (ratesp) {
3478 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003479 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003481 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003483 if (rates == 0) {
3484 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3485 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3486 nid, val,
3487 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3488 return -EIO;
3489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 *ratesp = rates;
3491 }
3492
3493 if (formatsp || bpsp) {
3494 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003495 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003497 streams = query_stream_param(codec, nid);
3498 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
3501 bps = 0;
3502 if (streams & AC_SUPFMT_PCM) {
3503 if (val & AC_SUPPCM_BITS_8) {
3504 formats |= SNDRV_PCM_FMTBIT_U8;
3505 bps = 8;
3506 }
3507 if (val & AC_SUPPCM_BITS_16) {
3508 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3509 bps = 16;
3510 }
3511 if (wcaps & AC_WCAP_DIGITAL) {
3512 if (val & AC_SUPPCM_BITS_32)
3513 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3514 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3515 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3516 if (val & AC_SUPPCM_BITS_24)
3517 bps = 24;
3518 else if (val & AC_SUPPCM_BITS_20)
3519 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003520 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3521 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3523 if (val & AC_SUPPCM_BITS_32)
3524 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 else if (val & AC_SUPPCM_BITS_24)
3526 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003527 else if (val & AC_SUPPCM_BITS_20)
3528 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
3530 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003531 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003533 if (!bps)
3534 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003535 }
3536 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003537 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 /* temporary hack: we have still no proper support
3539 * for the direct AC3 stream...
3540 */
3541 formats |= SNDRV_PCM_FMTBIT_U8;
3542 bps = 8;
3543 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003544 if (formats == 0) {
3545 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3546 "(nid=0x%x, val=0x%x, ovrd=%i, "
3547 "streams=0x%x)\n",
3548 nid, val,
3549 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3550 streams);
3551 return -EIO;
3552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 if (formatsp)
3554 *formatsp = formats;
3555 if (bpsp)
3556 *bpsp = bps;
3557 }
3558
3559 return 0;
3560}
Stephen Warren384a48d2011-06-01 11:14:21 -06003561EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
3563/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003564 * snd_hda_is_supported_format - Check the validity of the format
3565 * @codec: HD-audio codec
3566 * @nid: NID to check
3567 * @format: the HD-audio format value to check
3568 *
3569 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 *
3571 * Returns 1 if supported, 0 if not.
3572 */
3573int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3574 unsigned int format)
3575{
3576 int i;
3577 unsigned int val = 0, rate, stream;
3578
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003579 val = query_pcm_param(codec, nid);
3580 if (!val)
3581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003584 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003585 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if (val & (1 << i))
3587 break;
3588 return 0;
3589 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003590 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 return 0;
3592
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003593 stream = query_stream_param(codec, nid);
3594 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 return 0;
3596
3597 if (stream & AC_SUPFMT_PCM) {
3598 switch (format & 0xf0) {
3599 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003600 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 return 0;
3602 break;
3603 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003604 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return 0;
3606 break;
3607 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003608 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 return 0;
3610 break;
3611 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003612 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return 0;
3614 break;
3615 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003616 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return 0;
3618 break;
3619 default:
3620 return 0;
3621 }
3622 } else {
3623 /* FIXME: check for float32 and AC3? */
3624 }
3625
3626 return 1;
3627}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003628EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630/*
3631 * PCM stuff
3632 */
3633static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3634 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003635 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
3637 return 0;
3638}
3639
3640static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3641 struct hda_codec *codec,
3642 unsigned int stream_tag,
3643 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003644 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
3646 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3647 return 0;
3648}
3649
3650static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3651 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003652 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Takashi Iwai888afa12008-03-18 09:57:50 +01003654 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return 0;
3656}
3657
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003658static int set_pcm_default_values(struct hda_codec *codec,
3659 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003661 int err;
3662
Takashi Iwai0ba21762007-04-16 11:29:14 +02003663 /* query support PCM information from the given NID */
3664 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003665 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003666 info->rates ? NULL : &info->rates,
3667 info->formats ? NULL : &info->formats,
3668 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003669 if (err < 0)
3670 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 }
3672 if (info->ops.open == NULL)
3673 info->ops.open = hda_pcm_default_open_close;
3674 if (info->ops.close == NULL)
3675 info->ops.close = hda_pcm_default_open_close;
3676 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003677 if (snd_BUG_ON(!info->nid))
3678 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 info->ops.prepare = hda_pcm_default_prepare;
3680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003682 if (snd_BUG_ON(!info->nid))
3683 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 info->ops.cleanup = hda_pcm_default_cleanup;
3685 }
3686 return 0;
3687}
3688
Takashi Iwaieb541332010-08-06 13:48:11 +02003689/*
3690 * codec prepare/cleanup entries
3691 */
3692int snd_hda_codec_prepare(struct hda_codec *codec,
3693 struct hda_pcm_stream *hinfo,
3694 unsigned int stream,
3695 unsigned int format,
3696 struct snd_pcm_substream *substream)
3697{
3698 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003699 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003700 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3701 if (ret >= 0)
3702 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003703 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003704 return ret;
3705}
3706EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3707
3708void snd_hda_codec_cleanup(struct hda_codec *codec,
3709 struct hda_pcm_stream *hinfo,
3710 struct snd_pcm_substream *substream)
3711{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003712 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003713 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003714 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003715}
3716EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3717
Takashi Iwaid5191e52009-11-16 14:58:17 +01003718/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003719const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3720 "Audio", "SPDIF", "HDMI", "Modem"
3721};
3722
Takashi Iwai176d5332008-07-30 15:01:44 +02003723/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003724 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003725 *
3726 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003727 */
3728static int get_empty_pcm_device(struct hda_bus *bus, int type)
3729{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003730 /* audio device indices; not linear to keep compatibility */
3731 static int audio_idx[HDA_PCM_NTYPES][5] = {
3732 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3733 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003734 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003735 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003736 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003737 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003738
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003739 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003740 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3741 return -EINVAL;
3742 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003743
3744 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3745 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3746 return audio_idx[type][i];
3747
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003748 snd_printk(KERN_WARNING "Too many %s devices\n",
3749 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003750 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003751}
3752
3753/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003754 * attach a new PCM stream
3755 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003756static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003757{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003758 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003759 struct hda_pcm_stream *info;
3760 int stream, err;
3761
Takashi Iwaib91f0802008-11-04 08:43:08 +01003762 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003763 return -EINVAL;
3764 for (stream = 0; stream < 2; stream++) {
3765 info = &pcm->stream[stream];
3766 if (info->substreams) {
3767 err = set_pcm_default_values(codec, info);
3768 if (err < 0)
3769 return err;
3770 }
3771 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003772 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003773}
3774
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003775/* assign all PCMs of the given codec */
3776int snd_hda_codec_build_pcms(struct hda_codec *codec)
3777{
3778 unsigned int pcm;
3779 int err;
3780
3781 if (!codec->num_pcms) {
3782 if (!codec->patch_ops.build_pcms)
3783 return 0;
3784 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003785 if (err < 0) {
3786 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003787 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003788 err = snd_hda_codec_reset(codec);
3789 if (err < 0) {
3790 printk(KERN_ERR
3791 "hda_codec: cannot revert codec\n");
3792 return err;
3793 }
3794 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003795 }
3796 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3797 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3798 int dev;
3799
3800 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003801 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003802
3803 if (!cpcm->pcm) {
3804 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3805 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003806 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003807 cpcm->device = dev;
3808 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003809 if (err < 0) {
3810 printk(KERN_ERR "hda_codec: cannot attach "
3811 "PCM stream %d for codec #%d\n",
3812 dev, codec->addr);
3813 continue; /* no fatal error */
3814 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003815 }
3816 }
3817 return 0;
3818}
3819
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820/**
3821 * snd_hda_build_pcms - build PCM information
3822 * @bus: the BUS
3823 *
3824 * Create PCM information for each codec included in the bus.
3825 *
3826 * The build_pcms codec patch is requested to set up codec->num_pcms and
3827 * codec->pcm_info properly. The array is referred by the top-level driver
3828 * to create its PCM instances.
3829 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3830 * callback.
3831 *
3832 * At least, substreams, channels_min and channels_max must be filled for
3833 * each stream. substreams = 0 indicates that the stream doesn't exist.
3834 * When rates and/or formats are zero, the supported values are queried
3835 * from the given nid. The nid is used also by the default ops.prepare
3836 * and ops.cleanup callbacks.
3837 *
3838 * The driver needs to call ops.open in its open callback. Similarly,
3839 * ops.close is supposed to be called in the close callback.
3840 * ops.prepare should be called in the prepare or hw_params callback
3841 * with the proper parameters for set up.
3842 * ops.cleanup should be called in hw_free for clean up of streams.
3843 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003844 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003846int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003848 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Takashi Iwai0ba21762007-04-16 11:29:14 +02003850 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003851 int err = snd_hda_codec_build_pcms(codec);
3852 if (err < 0)
3853 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
3855 return 0;
3856}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003857EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859/**
3860 * snd_hda_check_board_config - compare the current codec with the config table
3861 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003862 * @num_configs: number of config enums
3863 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 * @tbl: configuration table, terminated by null entries
3865 *
3866 * Compares the modelname or PCI subsystem id of the current codec with the
3867 * given configuration table. If a matching entry is found, returns its
3868 * config value (supposed to be 0 or positive).
3869 *
3870 * If no entries are matching, the function returns a negative value.
3871 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003872int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003873 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003874 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003876 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003877 int i;
3878 for (i = 0; i < num_configs; i++) {
3879 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003880 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003881 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3882 "selected\n", models[i]);
3883 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 }
3885 }
3886 }
3887
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003888 if (!codec->bus->pci || !tbl)
3889 return -1;
3890
3891 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3892 if (!tbl)
3893 return -1;
3894 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003895#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003896 char tmp[10];
3897 const char *model = NULL;
3898 if (models)
3899 model = models[tbl->value];
3900 if (!model) {
3901 sprintf(tmp, "#%d", tbl->value);
3902 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003904 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3905 "for config %x:%x (%s)\n",
3906 model, tbl->subvendor, tbl->subdevice,
3907 (tbl->name ? tbl->name : "Unknown device"));
3908#endif
3909 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911 return -1;
3912}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003913EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
3915/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003916 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003917 subsystem ID with the
3918 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003919
3920 This is important for Gateway notebooks with SB450 HDA Audio
3921 where the vendor ID of the PCI device is:
3922 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3923 and the vendor/subvendor are found only at the codec.
3924
3925 * @codec: the HDA codec
3926 * @num_configs: number of config enums
3927 * @models: array of model name strings
3928 * @tbl: configuration table, terminated by null entries
3929 *
3930 * Compares the modelname or PCI subsystem id of the current codec with the
3931 * given configuration table. If a matching entry is found, returns its
3932 * config value (supposed to be 0 or positive).
3933 *
3934 * If no entries are matching, the function returns a negative value.
3935 */
3936int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003937 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003938 const struct snd_pci_quirk *tbl)
3939{
3940 const struct snd_pci_quirk *q;
3941
3942 /* Search for codec ID */
3943 for (q = tbl; q->subvendor; q++) {
3944 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3945
3946 if (vendorid == codec->subsystem_id)
3947 break;
3948 }
3949
3950 if (!q->subvendor)
3951 return -1;
3952
3953 tbl = q;
3954
3955 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003956#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003957 char tmp[10];
3958 const char *model = NULL;
3959 if (models)
3960 model = models[tbl->value];
3961 if (!model) {
3962 sprintf(tmp, "#%d", tbl->value);
3963 model = tmp;
3964 }
3965 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3966 "for config %x:%x (%s)\n",
3967 model, tbl->subvendor, tbl->subdevice,
3968 (tbl->name ? tbl->name : "Unknown device"));
3969#endif
3970 return tbl->value;
3971 }
3972 return -1;
3973}
3974EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3975
3976/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 * snd_hda_add_new_ctls - create controls from the array
3978 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003979 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 *
3981 * This helper function creates and add new controls in the given array.
3982 * The array must be terminated with an empty entry as terminator.
3983 *
3984 * Returns 0 if successful, or a negative error code.
3985 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003986int snd_hda_add_new_ctls(struct hda_codec *codec,
3987 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003989 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
3991 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003992 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003993 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003994 if (knew->iface == -1) /* skip this codec private value */
3995 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003996 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003997 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003998 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003999 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004000 if (addr > 0)
4001 kctl->id.device = addr;
4002 if (idx > 0)
4003 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004004 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004005 if (!err)
4006 break;
4007 /* try first with another device index corresponding to
4008 * the codec addr; if it still fails (or it's the
4009 * primary codec), then try another control index
4010 */
4011 if (!addr && codec->addr)
4012 addr = codec->addr;
4013 else if (!idx && !knew->index) {
4014 idx = find_empty_mixer_ctl_idx(codec,
4015 knew->name);
4016 if (idx <= 0)
4017 return err;
4018 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004019 return err;
4020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
4022 return 0;
4023}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004024EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Takashi Iwaicb53c622007-08-10 17:21:45 +02004026#ifdef CONFIG_SND_HDA_POWER_SAVE
4027static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
4028 unsigned int power_state);
4029
4030static void hda_power_work(struct work_struct *work)
4031{
4032 struct hda_codec *codec =
4033 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004034 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004035
Maxim Levitsky2e492462007-09-03 15:26:57 +02004036 if (!codec->power_on || codec->power_count) {
4037 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004038 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004039 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004040
4041 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004042 if (bus->ops.pm_notify)
4043 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004044}
4045
4046static void hda_keep_power_on(struct hda_codec *codec)
4047{
4048 codec->power_count++;
4049 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004050 codec->power_jiffies = jiffies;
4051}
4052
Takashi Iwaid5191e52009-11-16 14:58:17 +01004053/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004054void snd_hda_update_power_acct(struct hda_codec *codec)
4055{
4056 unsigned long delta = jiffies - codec->power_jiffies;
4057 if (codec->power_on)
4058 codec->power_on_acct += delta;
4059 else
4060 codec->power_off_acct += delta;
4061 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004062}
4063
Takashi Iwaid5191e52009-11-16 14:58:17 +01004064/**
4065 * snd_hda_power_up - Power-up the codec
4066 * @codec: HD-audio codec
4067 *
4068 * Increment the power-up counter and power up the hardware really when
4069 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004070 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004071void snd_hda_power_up(struct hda_codec *codec)
4072{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004073 struct hda_bus *bus = codec->bus;
4074
Takashi Iwaicb53c622007-08-10 17:21:45 +02004075 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004076 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004077 return;
4078
Takashi Iwaia2f63092009-11-11 09:34:25 +01004079 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004080 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004081 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004082 if (bus->ops.pm_notify)
4083 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004084 hda_call_codec_resume(codec);
4085 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004086 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004087}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004088EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004089
4090#define power_save(codec) \
4091 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004092
Takashi Iwaid5191e52009-11-16 14:58:17 +01004093/**
4094 * snd_hda_power_down - Power-down the codec
4095 * @codec: HD-audio codec
4096 *
4097 * Decrement the power-up counter and schedules the power-off work if
4098 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004099 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004100void snd_hda_power_down(struct hda_codec *codec)
4101{
4102 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004103 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004104 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004105 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004106 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004107 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004108 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004109 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004110}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004111EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004112
Takashi Iwaid5191e52009-11-16 14:58:17 +01004113/**
4114 * snd_hda_check_amp_list_power - Check the amp list and update the power
4115 * @codec: HD-audio codec
4116 * @check: the object containing an AMP list and the status
4117 * @nid: NID to check / update
4118 *
4119 * Check whether the given NID is in the amp list. If it's in the list,
4120 * check the current AMP status, and update the the power-status according
4121 * to the mute status.
4122 *
4123 * This function is supposed to be set or called from the check_power_status
4124 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004125 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004126int snd_hda_check_amp_list_power(struct hda_codec *codec,
4127 struct hda_loopback_check *check,
4128 hda_nid_t nid)
4129{
Takashi Iwai031024e2011-05-02 11:29:30 +02004130 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004131 int ch, v;
4132
4133 if (!check->amplist)
4134 return 0;
4135 for (p = check->amplist; p->nid; p++) {
4136 if (p->nid == nid)
4137 break;
4138 }
4139 if (!p->nid)
4140 return 0; /* nothing changed */
4141
4142 for (p = check->amplist; p->nid; p++) {
4143 for (ch = 0; ch < 2; ch++) {
4144 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4145 p->idx);
4146 if (!(v & HDA_AMP_MUTE) && v > 0) {
4147 if (!check->power_on) {
4148 check->power_on = 1;
4149 snd_hda_power_up(codec);
4150 }
4151 return 1;
4152 }
4153 }
4154 }
4155 if (check->power_on) {
4156 check->power_on = 0;
4157 snd_hda_power_down(codec);
4158 }
4159 return 0;
4160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004161EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004164/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004165 * Channel mode helper
4166 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004167
4168/**
4169 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4170 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004171int snd_hda_ch_mode_info(struct hda_codec *codec,
4172 struct snd_ctl_elem_info *uinfo,
4173 const struct hda_channel_mode *chmode,
4174 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004175{
4176 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4177 uinfo->count = 1;
4178 uinfo->value.enumerated.items = num_chmodes;
4179 if (uinfo->value.enumerated.item >= num_chmodes)
4180 uinfo->value.enumerated.item = num_chmodes - 1;
4181 sprintf(uinfo->value.enumerated.name, "%dch",
4182 chmode[uinfo->value.enumerated.item].channels);
4183 return 0;
4184}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004185EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004186
Takashi Iwaid5191e52009-11-16 14:58:17 +01004187/**
4188 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4189 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004190int snd_hda_ch_mode_get(struct hda_codec *codec,
4191 struct snd_ctl_elem_value *ucontrol,
4192 const struct hda_channel_mode *chmode,
4193 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004194 int max_channels)
4195{
4196 int i;
4197
4198 for (i = 0; i < num_chmodes; i++) {
4199 if (max_channels == chmode[i].channels) {
4200 ucontrol->value.enumerated.item[0] = i;
4201 break;
4202 }
4203 }
4204 return 0;
4205}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004206EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004207
Takashi Iwaid5191e52009-11-16 14:58:17 +01004208/**
4209 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4210 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004211int snd_hda_ch_mode_put(struct hda_codec *codec,
4212 struct snd_ctl_elem_value *ucontrol,
4213 const struct hda_channel_mode *chmode,
4214 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004215 int *max_channelsp)
4216{
4217 unsigned int mode;
4218
4219 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004220 if (mode >= num_chmodes)
4221 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004222 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004223 return 0;
4224 /* change the current channel setting */
4225 *max_channelsp = chmode[mode].channels;
4226 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004227 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004228 return 1;
4229}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004230EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232/*
4233 * input MUX helper
4234 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004235
4236/**
4237 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4238 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004239int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4240 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
4242 unsigned int index;
4243
4244 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4245 uinfo->count = 1;
4246 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004247 if (!imux->num_items)
4248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 index = uinfo->value.enumerated.item;
4250 if (index >= imux->num_items)
4251 index = imux->num_items - 1;
4252 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4253 return 0;
4254}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004255EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Takashi Iwaid5191e52009-11-16 14:58:17 +01004257/**
4258 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4259 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004260int snd_hda_input_mux_put(struct hda_codec *codec,
4261 const struct hda_input_mux *imux,
4262 struct snd_ctl_elem_value *ucontrol,
4263 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 unsigned int *cur_val)
4265{
4266 unsigned int idx;
4267
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004268 if (!imux->num_items)
4269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 idx = ucontrol->value.enumerated.item[0];
4271 if (idx >= imux->num_items)
4272 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004273 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004275 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4276 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 *cur_val = idx;
4278 return 1;
4279}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004280EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282
4283/*
4284 * Multi-channel / digital-out PCM helper functions
4285 */
4286
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004287/* setup SPDIF output stream */
4288static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4289 unsigned int stream_tag, unsigned int format)
4290{
Stephen Warren7c9359762011-06-01 11:14:17 -06004291 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4292
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004293 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004294 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004295 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004296 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004297 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004298 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004299 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004300 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004301 for (d = codec->slave_dig_outs; *d; d++)
4302 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4303 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004304 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004305 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004306 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004307 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004308 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004309}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004310
Takashi Iwai2f728532008-09-25 16:32:41 +02004311static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4312{
4313 snd_hda_codec_cleanup_stream(codec, nid);
4314 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004315 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004316 for (d = codec->slave_dig_outs; *d; d++)
4317 snd_hda_codec_cleanup_stream(codec, *d);
4318 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004319}
4320
Takashi Iwaid5191e52009-11-16 14:58:17 +01004321/**
4322 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4323 * @bus: HD-audio bus
4324 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004325void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4326{
4327 struct hda_codec *codec;
4328
4329 if (!bus)
4330 return;
4331 list_for_each_entry(codec, &bus->codec_list, list) {
4332#ifdef CONFIG_SND_HDA_POWER_SAVE
4333 if (!codec->power_on)
4334 continue;
4335#endif
4336 if (codec->patch_ops.reboot_notify)
4337 codec->patch_ops.reboot_notify(codec);
4338 }
4339}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004340EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004341
Takashi Iwaid5191e52009-11-16 14:58:17 +01004342/**
4343 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004345int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4346 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
Ingo Molnar62932df2006-01-16 16:34:20 +01004348 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004349 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4350 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004351 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004353 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 return 0;
4355}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004356EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Takashi Iwaid5191e52009-11-16 14:58:17 +01004358/**
4359 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4360 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004361int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4362 struct hda_multi_out *mout,
4363 unsigned int stream_tag,
4364 unsigned int format,
4365 struct snd_pcm_substream *substream)
4366{
4367 mutex_lock(&codec->spdif_mutex);
4368 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4369 mutex_unlock(&codec->spdif_mutex);
4370 return 0;
4371}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004372EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004373
Takashi Iwaid5191e52009-11-16 14:58:17 +01004374/**
4375 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4376 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004377int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4378 struct hda_multi_out *mout)
4379{
4380 mutex_lock(&codec->spdif_mutex);
4381 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4382 mutex_unlock(&codec->spdif_mutex);
4383 return 0;
4384}
4385EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4386
Takashi Iwaid5191e52009-11-16 14:58:17 +01004387/**
4388 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004390int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4391 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392{
Ingo Molnar62932df2006-01-16 16:34:20 +01004393 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004395 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return 0;
4397}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004398EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Takashi Iwaid5191e52009-11-16 14:58:17 +01004400/**
4401 * snd_hda_multi_out_analog_open - open analog outputs
4402 *
4403 * Open analog outputs and set up the hw-constraints.
4404 * If the digital outputs can be opened as slave, open the digital
4405 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004407int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4408 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004409 struct snd_pcm_substream *substream,
4410 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411{
Takashi Iwai9a081602008-02-12 18:37:26 +01004412 struct snd_pcm_runtime *runtime = substream->runtime;
4413 runtime->hw.channels_max = mout->max_channels;
4414 if (mout->dig_out_nid) {
4415 if (!mout->analog_rates) {
4416 mout->analog_rates = hinfo->rates;
4417 mout->analog_formats = hinfo->formats;
4418 mout->analog_maxbps = hinfo->maxbps;
4419 } else {
4420 runtime->hw.rates = mout->analog_rates;
4421 runtime->hw.formats = mout->analog_formats;
4422 hinfo->maxbps = mout->analog_maxbps;
4423 }
4424 if (!mout->spdif_rates) {
4425 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4426 &mout->spdif_rates,
4427 &mout->spdif_formats,
4428 &mout->spdif_maxbps);
4429 }
4430 mutex_lock(&codec->spdif_mutex);
4431 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004432 if ((runtime->hw.rates & mout->spdif_rates) &&
4433 (runtime->hw.formats & mout->spdif_formats)) {
4434 runtime->hw.rates &= mout->spdif_rates;
4435 runtime->hw.formats &= mout->spdif_formats;
4436 if (mout->spdif_maxbps < hinfo->maxbps)
4437 hinfo->maxbps = mout->spdif_maxbps;
4438 } else {
4439 mout->share_spdif = 0;
4440 /* FIXME: need notify? */
4441 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004442 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004443 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4446 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4447}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004448EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Takashi Iwaid5191e52009-11-16 14:58:17 +01004450/**
4451 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4452 *
4453 * Set up the i/o for analog out.
4454 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004456int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4457 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 unsigned int stream_tag,
4459 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004460 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
Takashi Iwaidda14412011-05-02 11:29:30 +02004462 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004464 struct hda_spdif_out *spdif =
4465 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 int i;
4467
Ingo Molnar62932df2006-01-16 16:34:20 +01004468 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004469 if (mout->dig_out_nid && mout->share_spdif &&
4470 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004472 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4473 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004474 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004476 setup_dig_out_stream(codec, mout->dig_out_nid,
4477 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 } else {
4479 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004480 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 }
4482 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004483 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
4485 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004486 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4487 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004488 if (!mout->no_share_stream &&
4489 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004491 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4492 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004493 /* extra outputs copied from front */
4494 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004495 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004496 snd_hda_codec_setup_stream(codec,
4497 mout->extra_out_nid[i],
4498 stream_tag, 0, format);
4499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 /* surrounds */
4501 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004502 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004503 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4504 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004505 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004506 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4507 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 }
4509 return 0;
4510}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004511EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Takashi Iwaid5191e52009-11-16 14:58:17 +01004513/**
4514 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004516int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4517 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518{
Takashi Iwaidda14412011-05-02 11:29:30 +02004519 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 int i;
4521
4522 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004523 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004525 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004526 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4527 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004528 snd_hda_codec_cleanup_stream(codec,
4529 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004530 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004532 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 mout->dig_out_used = 0;
4534 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004535 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 return 0;
4537}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004538EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004540/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004541 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004542 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004543
Takashi Iwaidda14412011-05-02 11:29:30 +02004544static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004545{
4546 for (; *list; list++)
4547 if (*list == nid)
4548 return 1;
4549 return 0;
4550}
4551
Steve Longerbeam81937d32007-05-08 15:33:03 +02004552
4553/*
4554 * Sort an associated group of pins according to their sequence numbers.
4555 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004556static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004557 int num_pins)
4558{
4559 int i, j;
4560 short seq;
4561 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004562
Steve Longerbeam81937d32007-05-08 15:33:03 +02004563 for (i = 0; i < num_pins; i++) {
4564 for (j = i + 1; j < num_pins; j++) {
4565 if (sequences[i] > sequences[j]) {
4566 seq = sequences[i];
4567 sequences[i] = sequences[j];
4568 sequences[j] = seq;
4569 nid = pins[i];
4570 pins[i] = pins[j];
4571 pins[j] = nid;
4572 }
4573 }
4574 }
4575}
4576
4577
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004578/* add the found input-pin to the cfg->inputs[] table */
4579static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4580 int type)
4581{
4582 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4583 cfg->inputs[cfg->num_inputs].pin = nid;
4584 cfg->inputs[cfg->num_inputs].type = type;
4585 cfg->num_inputs++;
4586 }
4587}
4588
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004589/* sort inputs in the order of AUTO_PIN_* type */
4590static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4591{
4592 int i, j;
4593
4594 for (i = 0; i < cfg->num_inputs; i++) {
4595 for (j = i + 1; j < cfg->num_inputs; j++) {
4596 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4597 struct auto_pin_cfg_item tmp;
4598 tmp = cfg->inputs[i];
4599 cfg->inputs[i] = cfg->inputs[j];
4600 cfg->inputs[j] = tmp;
4601 }
4602 }
4603 }
4604}
4605
Takashi Iwai82bc9552006-03-21 11:24:42 +01004606/*
4607 * Parse all pin widgets and store the useful pin nids to cfg
4608 *
4609 * The number of line-outs or any primary output is stored in line_outs,
4610 * and the corresponding output pins are assigned to line_out_pins[],
4611 * in the order of front, rear, CLFE, side, ...
4612 *
4613 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004614 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004615 * is detected, one of speaker of HP pins is assigned as the primary
4616 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4617 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004618 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004619 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004620 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4621 * respectively.
4622 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004623int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4624 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004625 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004626{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004627 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004628 short seq, assoc_line_out, assoc_speaker;
4629 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4630 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004631 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004632 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004633
4634 memset(cfg, 0, sizeof(*cfg));
4635
Steve Longerbeam81937d32007-05-08 15:33:03 +02004636 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4637 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004638 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004639 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004640
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004641 end_nid = codec->start_nid + codec->num_nodes;
4642 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004643 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004644 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004645 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004646 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004647
4648 /* read all default configuration for pin complex */
4649 if (wid_type != AC_WID_PIN)
4650 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004651 /* ignore the given nids (e.g. pc-beep returns error) */
4652 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4653 continue;
4654
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004655 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004656 conn = get_defcfg_connect(def_conf);
4657 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004658 continue;
4659 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004660 dev = get_defcfg_device(def_conf);
4661
4662 /* workaround for buggy BIOS setups */
4663 if (dev == AC_JACK_LINE_OUT) {
4664 if (conn == AC_JACK_PORT_FIXED)
4665 dev = AC_JACK_SPEAKER;
4666 }
4667
4668 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004669 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004670 seq = get_defcfg_sequence(def_conf);
4671 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004672
4673 if (!(wid_caps & AC_WCAP_STEREO))
4674 if (!cfg->mono_out_pin)
4675 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004676 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004677 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004678 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004679 assoc_line_out = assoc;
4680 else if (assoc_line_out != assoc)
4681 continue;
4682 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4683 continue;
4684 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004685 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004686 cfg->line_outs++;
4687 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004688 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004689 seq = get_defcfg_sequence(def_conf);
4690 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004691 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004692 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004693 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004694 assoc_speaker = assoc;
4695 else if (assoc_speaker != assoc)
4696 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004697 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4698 continue;
4699 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004700 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004701 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004702 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004703 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004704 seq = get_defcfg_sequence(def_conf);
4705 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004706 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4707 continue;
4708 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004709 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004710 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004711 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004712 case AC_JACK_MIC_IN:
4713 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004714 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004715 case AC_JACK_LINE_IN:
4716 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004717 break;
4718 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004719 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004720 break;
4721 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004722 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004723 break;
4724 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004725 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004726 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4727 continue;
4728 cfg->dig_out_pins[cfg->dig_outs] = nid;
4729 cfg->dig_out_type[cfg->dig_outs] =
4730 (loc == AC_JACK_LOC_HDMI) ?
4731 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4732 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004733 break;
4734 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004735 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004736 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004737 if (loc == AC_JACK_LOC_HDMI)
4738 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4739 else
4740 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004741 break;
4742 }
4743 }
4744
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004745 /* FIX-UP:
4746 * If no line-out is defined but multiple HPs are found,
4747 * some of them might be the real line-outs.
4748 */
4749 if (!cfg->line_outs && cfg->hp_outs > 1) {
4750 int i = 0;
4751 while (i < cfg->hp_outs) {
4752 /* The real HPs should have the sequence 0x0f */
4753 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4754 i++;
4755 continue;
4756 }
4757 /* Move it to the line-out table */
4758 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4759 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4760 cfg->line_outs++;
4761 cfg->hp_outs--;
4762 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4763 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004764 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004765 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4766 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004767 memset(cfg->hp_pins + cfg->hp_outs, 0,
4768 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004769 if (!cfg->hp_outs)
4770 cfg->line_out_type = AUTO_PIN_HP_OUT;
4771
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004772 }
4773
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004774 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004775 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4776 cfg->line_outs);
4777 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4778 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004779 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4780 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004781
Steve Longerbeam81937d32007-05-08 15:33:03 +02004782 /*
4783 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4784 * as a primary output
4785 */
4786 if (!cfg->line_outs) {
4787 if (cfg->speaker_outs) {
4788 cfg->line_outs = cfg->speaker_outs;
4789 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4790 sizeof(cfg->speaker_pins));
4791 cfg->speaker_outs = 0;
4792 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4793 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4794 } else if (cfg->hp_outs) {
4795 cfg->line_outs = cfg->hp_outs;
4796 memcpy(cfg->line_out_pins, cfg->hp_pins,
4797 sizeof(cfg->hp_pins));
4798 cfg->hp_outs = 0;
4799 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4800 cfg->line_out_type = AUTO_PIN_HP_OUT;
4801 }
4802 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004803
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004804 /* Reorder the surround channels
4805 * ALSA sequence is front/surr/clfe/side
4806 * HDA sequence is:
4807 * 4-ch: front/surr => OK as it is
4808 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004809 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004810 */
4811 switch (cfg->line_outs) {
4812 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004813 case 4:
4814 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004815 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004816 cfg->line_out_pins[2] = nid;
4817 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004818 }
4819
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004820 sort_autocfg_input_pins(cfg);
4821
Takashi Iwai82bc9552006-03-21 11:24:42 +01004822 /*
4823 * debug prints of the parsed results
4824 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004825 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
Takashi Iwai82bc9552006-03-21 11:24:42 +01004826 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4827 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004828 cfg->line_out_pins[4],
4829 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4830 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4831 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004832 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4833 cfg->speaker_outs, cfg->speaker_pins[0],
4834 cfg->speaker_pins[1], cfg->speaker_pins[2],
4835 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004836 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4837 cfg->hp_outs, cfg->hp_pins[0],
4838 cfg->hp_pins[1], cfg->hp_pins[2],
4839 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004840 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004841 if (cfg->dig_outs)
4842 snd_printd(" dig-out=0x%x/0x%x\n",
4843 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004844 snd_printd(" inputs:");
4845 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004846 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004847 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004848 cfg->inputs[i].pin);
4849 }
4850 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004851 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004852 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004853
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004854 return 0;
4855}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004856EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004857
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004858int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004859{
4860 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004861 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004862 if (conn == AC_JACK_PORT_NONE)
4863 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004864 /* Windows may claim the internal mic to be BOTH, too */
4865 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004866 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004867 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004868 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004869 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004870 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004871 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004872 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004873 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004874 return INPUT_PIN_ATTR_FRONT;
4875 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004876}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004877EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004878
Takashi Iwai990061c2010-09-09 22:08:44 +02004879/**
4880 * hda_get_input_pin_label - Give a label for the given input pin
4881 *
4882 * When check_location is true, the function checks the pin location
4883 * for mic and line-in pins, and set an appropriate prefix like "Front",
4884 * "Rear", "Internal".
4885 */
4886
Takashi Iwai10a20af2010-09-09 16:28:02 +02004887const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4888 int check_location)
4889{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004890 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004891 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004892 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4893 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004894 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004895
Takashi Iwai10a20af2010-09-09 16:28:02 +02004896 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004897
4898 switch (get_defcfg_device(def_conf)) {
4899 case AC_JACK_MIC_IN:
4900 if (!check_location)
4901 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004902 attr = snd_hda_get_input_pin_attr(def_conf);
4903 if (!attr)
4904 return "None";
4905 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004906 case AC_JACK_LINE_IN:
4907 if (!check_location)
4908 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004909 attr = snd_hda_get_input_pin_attr(def_conf);
4910 if (!attr)
4911 return "None";
4912 if (attr == INPUT_PIN_ATTR_DOCK)
4913 return "Dock Line";
4914 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004915 case AC_JACK_AUX:
4916 return "Aux";
4917 case AC_JACK_CD:
4918 return "CD";
4919 case AC_JACK_SPDIF_IN:
4920 return "SPDIF In";
4921 case AC_JACK_DIG_OTHER_IN:
4922 return "Digital In";
4923 default:
4924 return "Misc";
4925 }
4926}
4927EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4928
Takashi Iwaia1c98512010-09-09 21:36:27 +02004929/* Check whether the location prefix needs to be added to the label.
4930 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4931 * have to put "Front" prefix to each label. In such a case, returns false.
4932 */
4933static int check_mic_location_need(struct hda_codec *codec,
4934 const struct auto_pin_cfg *cfg,
4935 int input)
4936{
4937 unsigned int defc;
4938 int i, attr, attr2;
4939
4940 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004941 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004942 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004943 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004944 return 1;
4945
4946 attr = 0;
4947 for (i = 0; i < cfg->num_inputs; i++) {
4948 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004949 attr2 = snd_hda_get_input_pin_attr(defc);
4950 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004951 if (attr && attr != attr2)
4952 return 1; /* different locations found */
4953 attr = attr2;
4954 }
4955 }
4956 return 0;
4957}
4958
Takashi Iwai990061c2010-09-09 22:08:44 +02004959/**
4960 * hda_get_autocfg_input_label - Get a label for the given input
4961 *
4962 * Get a label for the given input pin defined by the autocfg item.
4963 * Unlike hda_get_input_pin_label(), this function checks all inputs
4964 * defined in autocfg and avoids the redundant mic/line prefix as much as
4965 * possible.
4966 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004967const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4968 const struct auto_pin_cfg *cfg,
4969 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004970{
4971 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004972 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004973
Takashi Iwai10a20af2010-09-09 16:28:02 +02004974 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4975 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4976 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004977 if (has_multiple_pins && type == AUTO_PIN_MIC)
4978 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004979 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4980 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004981}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004982EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4983
Takashi Iwai990061c2010-09-09 22:08:44 +02004984/**
4985 * snd_hda_add_imux_item - Add an item to input_mux
4986 *
4987 * When the same label is used already in the existing items, the number
4988 * suffix is appended to the label. This label index number is stored
4989 * to type_idx when non-NULL pointer is given.
4990 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004991int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4992 int index, int *type_idx)
4993{
4994 int i, label_idx = 0;
4995 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4996 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4997 return -EINVAL;
4998 }
4999 for (i = 0; i < imux->num_items; i++) {
5000 if (!strncmp(label, imux->items[i].label, strlen(label)))
5001 label_idx++;
5002 }
5003 if (type_idx)
5004 *type_idx = label_idx;
5005 if (label_idx > 0)
5006 snprintf(imux->items[imux->num_items].label,
5007 sizeof(imux->items[imux->num_items].label),
5008 "%s %d", label, label_idx);
5009 else
5010 strlcpy(imux->items[imux->num_items].label, label,
5011 sizeof(imux->items[imux->num_items].label));
5012 imux->items[imux->num_items].index = index;
5013 imux->num_items++;
5014 return 0;
5015}
5016EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005017
Takashi Iwai4a471b72005-12-07 13:56:29 +01005018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019#ifdef CONFIG_PM
5020/*
5021 * power management
5022 */
5023
5024/**
5025 * snd_hda_suspend - suspend the codecs
5026 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 *
5028 * Returns 0 if successful.
5029 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005030int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005032 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
Takashi Iwai0ba21762007-04-16 11:29:14 +02005034 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02005035#ifdef CONFIG_SND_HDA_POWER_SAVE
5036 if (!codec->power_on)
5037 continue;
5038#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02005039 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 }
5041 return 0;
5042}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005043EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
5045/**
5046 * snd_hda_resume - resume the codecs
5047 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 *
5049 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005050 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005051 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005052 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 */
5054int snd_hda_resume(struct hda_bus *bus)
5055{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005056 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
Takashi Iwai0ba21762007-04-16 11:29:14 +02005058 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005059 if (snd_hda_codec_needs_resume(codec))
5060 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 return 0;
5063}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005064EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005065#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005066
5067/*
5068 * generic arrays
5069 */
5070
Takashi Iwaid5191e52009-11-16 14:58:17 +01005071/**
5072 * snd_array_new - get a new element from the given array
5073 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005074 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005075 * Get a new element from the given array. If it exceeds the
5076 * pre-allocated array size, re-allocate the array.
5077 *
5078 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005079 */
5080void *snd_array_new(struct snd_array *array)
5081{
5082 if (array->used >= array->alloced) {
5083 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005084 void *nlist;
5085 if (snd_BUG_ON(num >= 4096))
5086 return NULL;
5087 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005088 if (!nlist)
5089 return NULL;
5090 if (array->list) {
5091 memcpy(nlist, array->list,
5092 array->elem_size * array->alloced);
5093 kfree(array->list);
5094 }
5095 array->list = nlist;
5096 array->alloced = num;
5097 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005098 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005100EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005101
Takashi Iwaid5191e52009-11-16 14:58:17 +01005102/**
5103 * snd_array_free - free the given array elements
5104 * @array: the array object
5105 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005106void snd_array_free(struct snd_array *array)
5107{
5108 kfree(array->list);
5109 array->used = 0;
5110 array->alloced = 0;
5111 array->list = NULL;
5112}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005113EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005114
Takashi Iwaid5191e52009-11-16 14:58:17 +01005115/**
5116 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5117 * @pcm: PCM caps bits
5118 * @buf: the string buffer to write
5119 * @buflen: the max buffer length
5120 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005121 * used by hda_proc.c and hda_eld.c
5122 */
5123void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5124{
5125 static unsigned int rates[] = {
5126 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5127 96000, 176400, 192000, 384000
5128 };
5129 int i, j;
5130
5131 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5132 if (pcm & (1 << i))
5133 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5134
5135 buf[j] = '\0'; /* necessary when j == 0 */
5136}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005137EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005138
Takashi Iwaid5191e52009-11-16 14:58:17 +01005139/**
5140 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5141 * @pcm: PCM caps bits
5142 * @buf: the string buffer to write
5143 * @buflen: the max buffer length
5144 *
5145 * used by hda_proc.c and hda_eld.c
5146 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005147void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5148{
5149 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5150 int i, j;
5151
5152 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5153 if (pcm & (AC_SUPPCM_BITS_8 << i))
5154 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5155
5156 buf[j] = '\0'; /* necessary when j == 0 */
5157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005158EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005159
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005160#ifdef CONFIG_SND_HDA_INPUT_JACK
5161/*
5162 * Input-jack notification support
5163 */
5164struct hda_jack_item {
5165 hda_nid_t nid;
5166 int type;
5167 struct snd_jack *jack;
5168};
5169
5170static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5171 int type)
5172{
5173 switch (type) {
5174 case SND_JACK_HEADPHONE:
5175 return "Headphone";
5176 case SND_JACK_MICROPHONE:
5177 return "Mic";
5178 case SND_JACK_LINEOUT:
5179 return "Line-out";
5180 case SND_JACK_HEADSET:
5181 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005182 case SND_JACK_VIDEOOUT:
5183 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005184 default:
5185 return "Misc";
5186 }
5187}
5188
5189static void hda_free_jack_priv(struct snd_jack *jack)
5190{
5191 struct hda_jack_item *jacks = jack->private_data;
5192 jacks->nid = 0;
5193 jacks->jack = NULL;
5194}
5195
5196int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5197 const char *name)
5198{
5199 struct hda_jack_item *jack;
5200 int err;
5201
5202 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5203 jack = snd_array_new(&codec->jacks);
5204 if (!jack)
5205 return -ENOMEM;
5206
5207 jack->nid = nid;
5208 jack->type = type;
5209 if (!name)
5210 name = get_jack_default_name(codec, nid, type);
5211 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5212 if (err < 0) {
5213 jack->nid = 0;
5214 return err;
5215 }
5216 jack->jack->private_data = jack;
5217 jack->jack->private_free = hda_free_jack_priv;
5218 return 0;
5219}
5220EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5221
5222void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5223{
5224 struct hda_jack_item *jacks = codec->jacks.list;
5225 int i;
5226
5227 if (!jacks)
5228 return;
5229
5230 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5231 unsigned int pin_ctl;
5232 unsigned int present;
5233 int type;
5234
5235 if (jacks->nid != nid)
5236 continue;
5237 present = snd_hda_jack_detect(codec, nid);
5238 type = jacks->type;
5239 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5240 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5241 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5242 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5243 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5244 }
5245 snd_jack_report(jacks->jack, present ? type : 0);
5246 }
5247}
5248EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5249
5250/* free jack instances manually when clearing/reconfiguring */
5251void snd_hda_input_jack_free(struct hda_codec *codec)
5252{
5253 if (!codec->bus->shutdown && codec->jacks.list) {
5254 struct hda_jack_item *jacks = codec->jacks.list;
5255 int i;
5256 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5257 if (jacks->jack)
5258 snd_device_free(codec->bus->card, jacks->jack);
5259 }
5260 }
5261 snd_array_free(&codec->jacks);
5262}
5263EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5264#endif /* CONFIG_SND_HDA_INPUT_JACK */
5265
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005266MODULE_DESCRIPTION("HDA codec core");
5267MODULE_LICENSE("GPL");