blob: ae5c5d5e4b7ce73aaef33052d2707e0847a6e15b [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>
32#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020033#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020034#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * vendor / preset table
38 */
39
40struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
43};
44
45/* codec vendor labels */
46static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010047 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020048 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020049 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010050 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010051 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010052 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020053 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010054 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020055 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010056 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020057 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020059 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010060 { 0x17e8, "Chrontel" },
61 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000062 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010064 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020065 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 {} /* terminator */
67};
68
Takashi Iwai1289e9e2008-11-27 15:47:11 +010069static DEFINE_MUTEX(preset_mutex);
70static LIST_HEAD(hda_preset_tables);
71
72int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
73{
74 mutex_lock(&preset_mutex);
75 list_add_tail(&preset->list, &hda_preset_tables);
76 mutex_unlock(&preset_mutex);
77 return 0;
78}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010079EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010080
81int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
82{
83 mutex_lock(&preset_mutex);
84 list_del(&preset->list);
85 mutex_unlock(&preset_mutex);
86 return 0;
87}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010088EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwaicb53c622007-08-10 17:21:45 +020090#ifdef CONFIG_SND_HDA_POWER_SAVE
91static void hda_power_work(struct work_struct *work);
92static void hda_keep_power_on(struct hda_codec *codec);
93#else
94static inline void hda_keep_power_on(struct hda_codec *codec) {}
95#endif
96
Takashi Iwaid5191e52009-11-16 14:58:17 +010097/**
98 * snd_hda_get_jack_location - Give a location string of the jack
99 * @cfg: pin default config value
100 *
101 * Parse the pin default config value and returns the string of the
102 * jack location, e.g. "Rear", "Front", etc.
103 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400104const char *snd_hda_get_jack_location(u32 cfg)
105{
106 static char *bases[7] = {
107 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
108 };
109 static unsigned char specials_idx[] = {
110 0x07, 0x08,
111 0x17, 0x18, 0x19,
112 0x37, 0x38
113 };
114 static char *specials[] = {
115 "Rear Panel", "Drive Bar",
116 "Riser", "HDMI", "ATAPI",
117 "Mobile-In", "Mobile-Out"
118 };
119 int i;
120 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
121 if ((cfg & 0x0f) < 7)
122 return bases[cfg & 0x0f];
123 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
124 if (cfg == specials_idx[i])
125 return specials[i];
126 }
127 return "UNKNOWN";
128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100129EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400130
Takashi Iwaid5191e52009-11-16 14:58:17 +0100131/**
132 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
133 * @cfg: pin default config value
134 *
135 * Parse the pin default config value and returns the string of the
136 * jack connectivity, i.e. external or internal connection.
137 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400138const char *snd_hda_get_jack_connectivity(u32 cfg)
139{
140 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
141
142 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100144EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400145
Takashi Iwaid5191e52009-11-16 14:58:17 +0100146/**
147 * snd_hda_get_jack_type - Give a type string of the jack
148 * @cfg: pin default config value
149 *
150 * Parse the pin default config value and returns the string of the
151 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
152 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400153const char *snd_hda_get_jack_type(u32 cfg)
154{
155 static char *jack_types[16] = {
156 "Line Out", "Speaker", "HP Out", "CD",
157 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
158 "Line In", "Aux", "Mic", "Telephony",
159 "SPDIF In", "Digitial In", "Reserved", "Other"
160 };
161
162 return jack_types[(cfg & AC_DEFCFG_DEVICE)
163 >> AC_DEFCFG_DEVICE_SHIFT];
164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100165EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400166
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100167/*
168 * Compose a 32bit command word to be sent to the HD-audio controller
169 */
170static inline unsigned int
171make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
172 unsigned int verb, unsigned int parm)
173{
174 u32 val;
175
Takashi Iwai82e1b802009-07-17 12:47:34 +0200176 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
177 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800178 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
179 codec->addr, direct, nid, verb, parm);
180 return ~0;
181 }
182
183 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184 val |= (u32)direct << 27;
185 val |= (u32)nid << 20;
186 val |= verb << 8;
187 val |= parm;
188 return val;
189}
190
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200191/*
192 * Send and receive a verb
193 */
194static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
195 unsigned int *res)
196{
197 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200198 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200199
Wu Fengguang6430aee2009-07-17 16:49:19 +0800200 if (cmd == ~0)
201 return -1;
202
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200203 if (res)
204 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200205 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206 snd_hda_power_up(codec);
207 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200209 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800210 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200211 mutex_unlock(&bus->cmd_mutex);
212 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200213 if (res && *res == -1 && bus->rirb_error) {
214 if (bus->response_reset) {
215 snd_printd("hda_codec: resetting BUS due to "
216 "fatal communication error\n");
217 bus->ops.bus_reset(bus);
218 }
219 goto again;
220 }
221 /* clear reset-flag when the communication gets recovered */
222 if (!err)
223 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224 return err;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/**
228 * snd_hda_codec_read - send a command and get the response
229 * @codec: the HDA codec
230 * @nid: NID to send the command
231 * @direct: direct flag
232 * @verb: the verb to send
233 * @parm: the parameter for the verb
234 *
235 * Send a single command and read the corresponding response.
236 *
237 * Returns the obtained response value, or -1 for an error.
238 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200239unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
240 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned int verb, unsigned int parm)
242{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200243 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
244 unsigned int res;
245 codec_exec_verb(codec, cmd, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return res;
247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100248EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/**
251 * snd_hda_codec_write - send a single command without waiting for response
252 * @codec: the HDA codec
253 * @nid: NID to send the command
254 * @direct: direct flag
255 * @verb: the verb to send
256 * @parm: the parameter for the verb
257 *
258 * Send a single command without waiting for response.
259 *
260 * Returns 0 if successful, or a negative error code.
261 */
262int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
263 unsigned int verb, unsigned int parm)
264{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200265 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100266 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200267 return codec_exec_verb(codec, cmd,
268 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100270EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/**
273 * snd_hda_sequence_write - sequence writes
274 * @codec: the HDA codec
275 * @seq: VERB array to send
276 *
277 * Send the commands sequentially from the given array.
278 * The array must be terminated with NID=0.
279 */
280void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
281{
282 for (; seq->nid; seq++)
283 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100285EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/**
288 * snd_hda_get_sub_nodes - get the range of sub nodes
289 * @codec: the HDA codec
290 * @nid: NID to parse
291 * @start_id: the pointer to store the start NID
292 *
293 * Parse the NID and store the start NID of its sub-nodes.
294 * Returns the number of sub-nodes.
295 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200296int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
297 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned int parm;
300
301 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200302 if (parm == -1)
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *start_id = (parm >> 16) & 0x7fff;
305 return (int)(parm & 0x7fff);
306}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100307EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
310 * snd_hda_get_connections - get connection list
311 * @codec: the HDA codec
312 * @nid: NID to parse
313 * @conn_list: connection list array
314 * @max_conns: max. number of connections to store
315 *
316 * Parses the connection list of the given widget and stores the list
317 * of NIDs.
318 *
319 * Returns the number of connections, or a negative error code.
320 */
321int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
322 hda_nid_t *conn_list, int max_conns)
323{
324 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100325 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200327 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100328 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Takashi Iwaida3cec32008-08-08 17:12:14 +0200330 if (snd_BUG_ON(!conn_list || max_conns <= 0))
331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200333 wcaps = get_wcaps(codec, nid);
334 if (!(wcaps & AC_WCAP_CONN_LIST) &&
335 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200336 snd_printk(KERN_WARNING "hda_codec: "
337 "connection list not available for 0x%x\n", nid);
338 return -EINVAL;
339 }
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
342 if (parm & AC_CLIST_LONG) {
343 /* long form */
344 shift = 16;
345 num_elems = 2;
346 } else {
347 /* short form */
348 shift = 8;
349 num_elems = 4;
350 }
351 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mask = (1 << (shift-1)) - 1;
353
Takashi Iwai0ba21762007-04-16 11:29:14 +0200354 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0; /* no connection */
356
357 if (conn_len == 1) {
358 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200359 parm = snd_hda_codec_read(codec, nid, 0,
360 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200361 if (parm == -1 && codec->bus->rirb_error)
362 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 conn_list[0] = parm & mask;
364 return 1;
365 }
366
367 /* multi connection */
368 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100369 prev_nid = 0;
370 for (i = 0; i < conn_len; i++) {
371 int range_val;
372 hda_nid_t val, n;
373
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200374 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100375 parm = snd_hda_codec_read(codec, nid, 0,
376 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200377 if (parm == -1 && codec->bus->rirb_error)
378 return -EIO;
379 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200380 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100381 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200382 if (val == 0) {
383 snd_printk(KERN_WARNING "hda_codec: "
384 "invalid CONNECT_LIST verb %x[%i]:%x\n",
385 nid, i, parm);
386 return 0;
387 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100388 parm >>= shift;
389 if (range_val) {
390 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200391 if (!prev_nid || prev_nid >= val) {
392 snd_printk(KERN_WARNING "hda_codec: "
393 "invalid dep_range_val %x:%x\n",
394 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100395 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100397 for (n = prev_nid + 1; n <= val; n++) {
398 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200399 snd_printk(KERN_ERR "hda_codec: "
400 "Too many connections %d for NID 0x%x\n",
401 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100402 return -EINVAL;
403 }
404 conn_list[conns++] = n;
405 }
406 } else {
407 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200408 snd_printk(KERN_ERR "hda_codec: "
409 "Too many connections %d for NID 0x%x\n",
410 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100411 return -EINVAL;
412 }
413 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100415 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 return conns;
418}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100419EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421
422/**
423 * snd_hda_queue_unsol_event - add an unsolicited event to queue
424 * @bus: the BUS
425 * @res: unsolicited event (lower 32bit of RIRB entry)
426 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
427 *
428 * Adds the given event to the queue. The events are processed in
429 * the workqueue asynchronously. Call this function in the interrupt
430 * hanlder when RIRB receives an unsolicited event.
431 *
432 * Returns 0 if successful, or a negative error code.
433 */
434int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
435{
436 struct hda_bus_unsolicited *unsol;
437 unsigned int wp;
438
Takashi Iwai0ba21762007-04-16 11:29:14 +0200439 unsol = bus->unsol;
440 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442
443 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
444 unsol->wp = wp;
445
446 wp <<= 1;
447 unsol->queue[wp] = res;
448 unsol->queue[wp + 1] = res_ex;
449
Takashi Iwai6acaed32009-01-12 10:09:24 +0100450 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 return 0;
453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100454EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800457 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
David Howellsc4028952006-11-22 14:57:56 +0000459static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
David Howellsc4028952006-11-22 14:57:56 +0000461 struct hda_bus_unsolicited *unsol =
462 container_of(work, struct hda_bus_unsolicited, work);
463 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct hda_codec *codec;
465 unsigned int rp, caddr, res;
466
467 while (unsol->rp != unsol->wp) {
468 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
469 unsol->rp = rp;
470 rp <<= 1;
471 res = unsol->queue[rp];
472 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200473 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 continue;
475 codec = bus->caddr_tbl[caddr & 0x0f];
476 if (codec && codec->patch_ops.unsol_event)
477 codec->patch_ops.unsol_event(codec, res);
478 }
479}
480
481/*
482 * initialize unsolicited queue
483 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200484static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct hda_bus_unsolicited *unsol;
487
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100488 if (bus->unsol) /* already initialized */
489 return 0;
490
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200491 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200492 if (!unsol) {
493 snd_printk(KERN_ERR "hda_codec: "
494 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -ENOMEM;
496 }
David Howellsc4028952006-11-22 14:57:56 +0000497 INIT_WORK(&unsol->work, process_unsol_events);
498 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 bus->unsol = unsol;
500 return 0;
501}
502
503/*
504 * destructor
505 */
506static void snd_hda_codec_free(struct hda_codec *codec);
507
508static int snd_hda_bus_free(struct hda_bus *bus)
509{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200510 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Takashi Iwai0ba21762007-04-16 11:29:14 +0200512 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100514 if (bus->workq)
515 flush_workqueue(bus->workq);
516 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200518 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 snd_hda_codec_free(codec);
520 }
521 if (bus->ops.private_free)
522 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100523 if (bus->workq)
524 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 kfree(bus);
526 return 0;
527}
528
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100529static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100532 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return snd_hda_bus_free(bus);
534}
535
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200536#ifdef CONFIG_SND_HDA_HWDEP
537static int snd_hda_bus_dev_register(struct snd_device *device)
538{
539 struct hda_bus *bus = device->device_data;
540 struct hda_codec *codec;
541 list_for_each_entry(codec, &bus->codec_list, list) {
542 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100543 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200544 }
545 return 0;
546}
547#else
548#define snd_hda_bus_dev_register NULL
549#endif
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
552 * snd_hda_bus_new - create a HDA bus
553 * @card: the card entry
554 * @temp: the template for hda_bus information
555 * @busp: the pointer to store the created bus instance
556 *
557 * Returns 0 if successful, or a negative error code.
558 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100559int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200560 const struct hda_bus_template *temp,
561 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct hda_bus *bus;
564 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100565 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200566 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 .dev_free = snd_hda_bus_dev_free,
568 };
569
Takashi Iwaida3cec32008-08-08 17:12:14 +0200570 if (snd_BUG_ON(!temp))
571 return -EINVAL;
572 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (busp)
576 *busp = NULL;
577
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200578 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (bus == NULL) {
580 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
581 return -ENOMEM;
582 }
583
584 bus->card = card;
585 bus->private_data = temp->private_data;
586 bus->pci = temp->pci;
587 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100588 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 bus->ops = temp->ops;
590
Ingo Molnar62932df2006-01-16 16:34:20 +0100591 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200592 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 INIT_LIST_HEAD(&bus->codec_list);
594
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100595 snprintf(bus->workq_name, sizeof(bus->workq_name),
596 "hd-audio%d", card->number);
597 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100598 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100599 snd_printk(KERN_ERR "cannot create workqueue %s\n",
600 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100601 kfree(bus);
602 return -ENOMEM;
603 }
604
Takashi Iwai0ba21762007-04-16 11:29:14 +0200605 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
606 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 snd_hda_bus_free(bus);
608 return err;
609 }
610 if (busp)
611 *busp = bus;
612 return 0;
613}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100614EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Takashi Iwai82467612007-07-27 19:15:54 +0200616#ifdef CONFIG_SND_HDA_GENERIC
617#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200618 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200619#else
620#define is_generic_config(codec) 0
621#endif
622
Takashi Iwai645f10c2008-11-28 15:07:37 +0100623#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100624#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
625#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100626#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100627#endif
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*
630 * find a matching codec preset
631 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200632static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200633find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100635 struct hda_codec_preset_list *tbl;
636 const struct hda_codec_preset *preset;
637 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Takashi Iwai82467612007-07-27 19:15:54 +0200639 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100640 return NULL; /* use the generic parser */
641
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100642 again:
643 mutex_lock(&preset_mutex);
644 list_for_each_entry(tbl, &hda_preset_tables, list) {
645 if (!try_module_get(tbl->owner)) {
646 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
647 continue;
648 }
649 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100651 if (preset->afg && preset->afg != codec->afg)
652 continue;
653 if (preset->mfg && preset->mfg != codec->mfg)
654 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200657 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200658 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100659 preset->rev == codec->revision_id)) {
660 mutex_unlock(&preset_mutex);
661 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100665 module_put(tbl->owner);
666 }
667 mutex_unlock(&preset_mutex);
668
669 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
670 char name[32];
671 if (!mod_requested)
672 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
673 codec->vendor_id);
674 else
675 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
676 (codec->vendor_id >> 16) & 0xffff);
677 request_module(name);
678 mod_requested++;
679 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681 return NULL;
682}
683
684/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200685 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200687static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 const struct hda_vendor_id *c;
690 const char *vendor = NULL;
691 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200692 char tmp[16];
693
694 if (codec->vendor_name)
695 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 for (c = hda_vendor_ids; c->id; c++) {
698 if (c->id == vendor_id) {
699 vendor = c->name;
700 break;
701 }
702 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200703 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 sprintf(tmp, "Generic %04x", vendor_id);
705 vendor = tmp;
706 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200707 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
708 if (!codec->vendor_name)
709 return -ENOMEM;
710
711 get_chip_name:
712 if (codec->chip_name)
713 return 0;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200716 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
717 else {
718 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
719 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
720 }
721 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200722 return -ENOMEM;
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200727 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100729static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200731 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 hda_nid_t nid;
733
734 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
735 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200736 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200737 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200738 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200739 case AC_GRP_AUDIO_FUNCTION:
740 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200741 codec->afg_function_id = function_id & 0xff;
742 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200743 break;
744 case AC_GRP_MODEM_FUNCTION:
745 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200746 codec->mfg_function_id = function_id & 0xff;
747 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200748 break;
749 default:
750 break;
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100756 * read widget caps for each widget and store in cache
757 */
758static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
759{
760 int i;
761 hda_nid_t nid;
762
763 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
764 &codec->start_nid);
765 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200766 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100767 return -ENOMEM;
768 nid = codec->start_nid;
769 for (i = 0; i < codec->num_nodes; i++, nid++)
770 codec->wcaps[i] = snd_hda_param_read(codec, nid,
771 AC_PAR_AUDIO_WIDGET_CAP);
772 return 0;
773}
774
Takashi Iwai3be14142009-02-20 14:11:16 +0100775/* read all pin default configurations and save codec->init_pins */
776static int read_pin_defaults(struct hda_codec *codec)
777{
778 int i;
779 hda_nid_t nid = codec->start_nid;
780
781 for (i = 0; i < codec->num_nodes; i++, nid++) {
782 struct hda_pincfg *pin;
783 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200784 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100785 if (wid_type != AC_WID_PIN)
786 continue;
787 pin = snd_array_new(&codec->init_pins);
788 if (!pin)
789 return -ENOMEM;
790 pin->nid = nid;
791 pin->cfg = snd_hda_codec_read(codec, nid, 0,
792 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200793 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
794 AC_VERB_GET_PIN_WIDGET_CONTROL,
795 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100796 }
797 return 0;
798}
799
800/* look up the given pin config list and return the item matching with NID */
801static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
802 struct snd_array *array,
803 hda_nid_t nid)
804{
805 int i;
806 for (i = 0; i < array->used; i++) {
807 struct hda_pincfg *pin = snd_array_elem(array, i);
808 if (pin->nid == nid)
809 return pin;
810 }
811 return NULL;
812}
813
814/* write a config value for the given NID */
815static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
816 unsigned int cfg)
817{
818 int i;
819 for (i = 0; i < 4; i++) {
820 snd_hda_codec_write(codec, nid, 0,
821 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
822 cfg & 0xff);
823 cfg >>= 8;
824 }
825}
826
827/* set the current pin config value for the given NID.
828 * the value is cached, and read via snd_hda_codec_get_pincfg()
829 */
830int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
831 hda_nid_t nid, unsigned int cfg)
832{
833 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100834 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100835
Takashi Iwaib82855a2009-12-27 11:24:56 +0100836 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
837 return -EINVAL;
838
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100839 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100840 pin = look_up_pincfg(codec, list, nid);
841 if (!pin) {
842 pin = snd_array_new(list);
843 if (!pin)
844 return -ENOMEM;
845 pin->nid = nid;
846 }
847 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100848
849 /* change only when needed; e.g. if the pincfg is already present
850 * in user_pins[], don't write it
851 */
852 cfg = snd_hda_codec_get_pincfg(codec, nid);
853 if (oldcfg != cfg)
854 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100855 return 0;
856}
857
Takashi Iwaid5191e52009-11-16 14:58:17 +0100858/**
859 * snd_hda_codec_set_pincfg - Override a pin default configuration
860 * @codec: the HDA codec
861 * @nid: NID to set the pin config
862 * @cfg: the pin default config value
863 *
864 * Override a pin default configuration value in the cache.
865 * This value can be read by snd_hda_codec_get_pincfg() in a higher
866 * priority than the real hardware value.
867 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100868int snd_hda_codec_set_pincfg(struct hda_codec *codec,
869 hda_nid_t nid, unsigned int cfg)
870{
Takashi Iwai346ff702009-02-23 09:42:57 +0100871 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100872}
873EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
874
Takashi Iwaid5191e52009-11-16 14:58:17 +0100875/**
876 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
877 * @codec: the HDA codec
878 * @nid: NID to get the pin config
879 *
880 * Get the current pin config value of the given pin NID.
881 * If the pincfg value is cached or overridden via sysfs or driver,
882 * returns the cached value.
883 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100884unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
885{
886 struct hda_pincfg *pin;
887
Takashi Iwai3be14142009-02-20 14:11:16 +0100888#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100889 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100890 if (pin)
891 return pin->cfg;
892#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100893 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
894 if (pin)
895 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100896 pin = look_up_pincfg(codec, &codec->init_pins, nid);
897 if (pin)
898 return pin->cfg;
899 return 0;
900}
901EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
902
903/* restore all current pin configs */
904static void restore_pincfgs(struct hda_codec *codec)
905{
906 int i;
907 for (i = 0; i < codec->init_pins.used; i++) {
908 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
909 set_pincfg(codec, pin->nid,
910 snd_hda_codec_get_pincfg(codec, pin->nid));
911 }
912}
Takashi Iwai54d17402005-11-21 16:33:22 +0100913
Takashi Iwai92ee6162009-12-27 11:18:59 +0100914/**
915 * snd_hda_shutup_pins - Shut up all pins
916 * @codec: the HDA codec
917 *
918 * Clear all pin controls to shup up before suspend for avoiding click noise.
919 * The controls aren't cached so that they can be resumed properly.
920 */
921void snd_hda_shutup_pins(struct hda_codec *codec)
922{
923 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200924 /* don't shut up pins when unloading the driver; otherwise it breaks
925 * the default pin setup at the next load of the driver
926 */
927 if (codec->bus->shutdown)
928 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100929 for (i = 0; i < codec->init_pins.used; i++) {
930 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
931 /* use read here for syncing after issuing each verb */
932 snd_hda_codec_read(codec, pin->nid, 0,
933 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
934 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200935 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100936}
937EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
938
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200939/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
940static void restore_shutup_pins(struct hda_codec *codec)
941{
942 int i;
943 if (!codec->pins_shutup)
944 return;
945 if (codec->bus->shutdown)
946 return;
947 for (i = 0; i < codec->init_pins.used; i++) {
948 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
949 snd_hda_codec_write(codec, pin->nid, 0,
950 AC_VERB_SET_PIN_WIDGET_CONTROL,
951 pin->ctrl);
952 }
953 codec->pins_shutup = 0;
954}
955
Takashi Iwai01751f52007-08-10 16:59:39 +0200956static void init_hda_cache(struct hda_cache_rec *cache,
957 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200958static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200959
Takashi Iwai3be14142009-02-20 14:11:16 +0100960/* restore the initial pin cfgs and release all pincfg lists */
961static void restore_init_pincfgs(struct hda_codec *codec)
962{
Takashi Iwai346ff702009-02-23 09:42:57 +0100963 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100964 * so that only the values in init_pins are restored
965 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100966 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100967#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100968 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100969#endif
970 restore_pincfgs(codec);
971 snd_array_free(&codec->init_pins);
972}
973
Takashi Iwai54d17402005-11-21 16:33:22 +0100974/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200975 * audio-converter setup caches
976 */
977struct hda_cvt_setup {
978 hda_nid_t nid;
979 u8 stream_tag;
980 u8 channel_id;
981 u16 format_id;
982 unsigned char active; /* cvt is currently used */
983 unsigned char dirty; /* setups should be cleared */
984};
985
986/* get or create a cache entry for the given audio converter NID */
987static struct hda_cvt_setup *
988get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
989{
990 struct hda_cvt_setup *p;
991 int i;
992
993 for (i = 0; i < codec->cvt_setups.used; i++) {
994 p = snd_array_elem(&codec->cvt_setups, i);
995 if (p->nid == nid)
996 return p;
997 }
998 p = snd_array_new(&codec->cvt_setups);
999 if (p)
1000 p->nid = nid;
1001 return p;
1002}
1003
1004/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * codec destructor
1006 */
1007static void snd_hda_codec_free(struct hda_codec *codec)
1008{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001009 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001011 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001012#ifdef CONFIG_SND_HDA_POWER_SAVE
1013 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001014 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001017 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001018 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 codec->bus->caddr_tbl[codec->addr] = NULL;
1020 if (codec->patch_ops.free)
1021 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001022 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001023 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001024 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001025 kfree(codec->vendor_name);
1026 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001027 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001028 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 kfree(codec);
1030}
1031
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001032static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1033 unsigned int power_state);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/**
1036 * snd_hda_codec_new - create a HDA codec
1037 * @bus: the bus to assign
1038 * @codec_addr: the codec address
1039 * @codecp: the pointer to store the generated codec
1040 *
1041 * Returns 0 if successful, or a negative error code.
1042 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001043int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1044 unsigned int codec_addr,
1045 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001048 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int err;
1050
Takashi Iwaida3cec32008-08-08 17:12:14 +02001051 if (snd_BUG_ON(!bus))
1052 return -EINVAL;
1053 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1054 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001057 snd_printk(KERN_ERR "hda_codec: "
1058 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -EBUSY;
1060 }
1061
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001062 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (codec == NULL) {
1064 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1065 return -ENOMEM;
1066 }
1067
1068 codec->bus = bus;
1069 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001070 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001071 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001072 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001073 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001074 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1075 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001076 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001077 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001078 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001079 if (codec->bus->modelname) {
1080 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1081 if (!codec->modelname) {
1082 snd_hda_codec_free(codec);
1083 return -ENODEV;
1084 }
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Takashi Iwaicb53c622007-08-10 17:21:45 +02001087#ifdef CONFIG_SND_HDA_POWER_SAVE
1088 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1089 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1090 * the caller has to power down appropriatley after initialization
1091 * phase.
1092 */
1093 hda_keep_power_on(codec);
1094#endif
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 list_add_tail(&codec->list, &bus->codec_list);
1097 bus->caddr_tbl[codec_addr] = codec;
1098
Takashi Iwai0ba21762007-04-16 11:29:14 +02001099 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1100 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001101 if (codec->vendor_id == -1)
1102 /* read again, hopefully the access method was corrected
1103 * in the last read...
1104 */
1105 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1106 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001107 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1108 AC_PAR_SUBSYSTEM_ID);
1109 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1110 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001112 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001113 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001114 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001115 err = -ENODEV;
1116 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
Takashi Iwai3be14142009-02-20 14:11:16 +01001119 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1120 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001121 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001122 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001123 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001124 err = read_pin_defaults(codec);
1125 if (err < 0)
1126 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001127
Takashi Iwai0ba21762007-04-16 11:29:14 +02001128 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001129 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001130 codec->subsystem_id =
1131 snd_hda_codec_read(codec, nid, 0,
1132 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001133 }
1134
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001135 /* power-up all before initialization */
1136 hda_set_power_state(codec,
1137 codec->afg ? codec->afg : codec->mfg,
1138 AC_PWRST_D0);
1139
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001140 snd_hda_codec_proc_new(codec);
1141
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001142 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001143
1144 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1145 codec->subsystem_id, codec->revision_id);
1146 snd_component_add(codec->bus->card, component);
1147
1148 if (codecp)
1149 *codecp = codec;
1150 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001151
1152 error:
1153 snd_hda_codec_free(codec);
1154 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001155}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001156EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001157
Takashi Iwaid5191e52009-11-16 14:58:17 +01001158/**
1159 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1160 * @codec: the HDA codec
1161 *
1162 * Start parsing of the given codec tree and (re-)initialize the whole
1163 * patch instance.
1164 *
1165 * Returns 0 if successful or a negative error code.
1166 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001167int snd_hda_codec_configure(struct hda_codec *codec)
1168{
1169 int err;
1170
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001171 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001172 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001173 err = get_codec_name(codec);
1174 if (err < 0)
1175 return err;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Takashi Iwai82467612007-07-27 19:15:54 +02001178 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001180 goto patched;
1181 }
Takashi Iwai82467612007-07-27 19:15:54 +02001182 if (codec->preset && codec->preset->patch) {
1183 err = codec->preset->patch(codec);
1184 goto patched;
1185 }
1186
1187 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001188 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001189 if (err < 0)
1190 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001191
1192 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001193 if (!err && codec->patch_ops.unsol_event)
1194 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001195 /* audio codec should override the mixer name */
1196 if (!err && (codec->afg || !*codec->bus->card->mixername))
1197 snprintf(codec->bus->card->mixername,
1198 sizeof(codec->bus->card->mixername),
1199 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001202EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204/**
1205 * snd_hda_codec_setup_stream - set up the codec for streaming
1206 * @codec: the CODEC to set up
1207 * @nid: the NID to set up
1208 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1209 * @channel_id: channel id to pass, zero based.
1210 * @format: stream format.
1211 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001212void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1213 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int channel_id, int format)
1215{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001216 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001217 struct hda_cvt_setup *p;
1218 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001219 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001220 int i;
1221
Takashi Iwai0ba21762007-04-16 11:29:14 +02001222 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001223 return;
1224
Takashi Iwai0ba21762007-04-16 11:29:14 +02001225 snd_printdd("hda_codec_setup_stream: "
1226 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001228 p = get_hda_cvt_setup(codec, nid);
1229 if (!p)
1230 return;
1231 /* update the stream-id if changed */
1232 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1233 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1234 newval = (stream_tag << 4) | channel_id;
1235 if (oldval != newval)
1236 snd_hda_codec_write(codec, nid, 0,
1237 AC_VERB_SET_CHANNEL_STREAMID,
1238 newval);
1239 p->stream_tag = stream_tag;
1240 p->channel_id = channel_id;
1241 }
1242 /* update the format-id if changed */
1243 if (p->format_id != format) {
1244 oldval = snd_hda_codec_read(codec, nid, 0,
1245 AC_VERB_GET_STREAM_FORMAT, 0);
1246 if (oldval != format) {
1247 msleep(1);
1248 snd_hda_codec_write(codec, nid, 0,
1249 AC_VERB_SET_STREAM_FORMAT,
1250 format);
1251 }
1252 p->format_id = format;
1253 }
1254 p->active = 1;
1255 p->dirty = 0;
1256
1257 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001258 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001259 list_for_each_entry(c, &codec->bus->codec_list, list) {
1260 for (i = 0; i < c->cvt_setups.used; i++) {
1261 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001262 if (!p->active && p->stream_tag == stream_tag &&
1263 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001264 p->dirty = 1;
1265 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001268EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Takashi Iwaif0cea792010-08-13 11:56:53 +02001270static void really_cleanup_stream(struct hda_codec *codec,
1271 struct hda_cvt_setup *q);
1272
Takashi Iwaid5191e52009-11-16 14:58:17 +01001273/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001274 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001275 * @codec: the CODEC to clean up
1276 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001277 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001278 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001279void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1280 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001281{
Takashi Iwaieb541332010-08-06 13:48:11 +02001282 struct hda_cvt_setup *p;
1283
Takashi Iwai888afa12008-03-18 09:57:50 +01001284 if (!nid)
1285 return;
1286
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001287 if (codec->no_sticky_stream)
1288 do_now = 1;
1289
Takashi Iwai888afa12008-03-18 09:57:50 +01001290 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001291 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001292 if (p) {
1293 /* here we just clear the active flag when do_now isn't set;
1294 * actual clean-ups will be done later in
1295 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1296 */
1297 if (do_now)
1298 really_cleanup_stream(codec, p);
1299 else
1300 p->active = 0;
1301 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001302}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001303EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001304
Takashi Iwaieb541332010-08-06 13:48:11 +02001305static void really_cleanup_stream(struct hda_codec *codec,
1306 struct hda_cvt_setup *q)
1307{
1308 hda_nid_t nid = q->nid;
1309 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1310 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1311 memset(q, 0, sizeof(*q));
1312 q->nid = nid;
1313}
1314
1315/* clean up the all conflicting obsolete streams */
1316static void purify_inactive_streams(struct hda_codec *codec)
1317{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001318 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001319 int i;
1320
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001321 list_for_each_entry(c, &codec->bus->codec_list, list) {
1322 for (i = 0; i < c->cvt_setups.used; i++) {
1323 struct hda_cvt_setup *p;
1324 p = snd_array_elem(&c->cvt_setups, i);
1325 if (p->dirty)
1326 really_cleanup_stream(c, p);
1327 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001328 }
1329}
1330
1331/* clean up all streams; called from suspend */
1332static void hda_cleanup_all_streams(struct hda_codec *codec)
1333{
1334 int i;
1335
1336 for (i = 0; i < codec->cvt_setups.used; i++) {
1337 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1338 if (p->stream_tag)
1339 really_cleanup_stream(codec, p);
1340 }
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/*
1344 * amp access functions
1345 */
1346
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001347/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001348#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001349#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001350#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1351#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001353#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001356static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001357 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Takashi Iwai01751f52007-08-10 16:59:39 +02001359 memset(cache, 0, sizeof(*cache));
1360 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001361 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001362}
1363
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001364static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001365{
Takashi Iwai603c4012008-07-30 15:01:44 +02001366 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001370static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Takashi Iwai01751f52007-08-10 16:59:39 +02001372 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1373 u16 cur = cache->hash[idx];
1374 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001377 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (info->key == key)
1379 return info;
1380 cur = info->next;
1381 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001382 return NULL;
1383}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001385/* query the hash. allocate an entry if not found. */
1386static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1387 u32 key)
1388{
1389 struct hda_cache_head *info = get_hash(cache, key);
1390 if (!info) {
1391 u16 idx, cur;
1392 /* add a new hash entry */
1393 info = snd_array_new(&cache->buf);
1394 if (!info)
1395 return NULL;
1396 cur = snd_array_index(&cache->buf, info);
1397 info->key = key;
1398 info->val = 0;
1399 idx = key % (u16)ARRAY_SIZE(cache->hash);
1400 info->next = cache->hash[idx];
1401 cache->hash[idx] = cur;
1402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return info;
1404}
1405
Takashi Iwai01751f52007-08-10 16:59:39 +02001406/* query and allocate an amp hash entry */
1407static inline struct hda_amp_info *
1408get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1409{
1410 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1411}
1412
Takashi Iwaid5191e52009-11-16 14:58:17 +01001413/**
1414 * query_amp_caps - query AMP capabilities
1415 * @codec: the HD-auio codec
1416 * @nid: the NID to query
1417 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1418 *
1419 * Query AMP capabilities for the given widget and direction.
1420 * Returns the obtained capability bits.
1421 *
1422 * When cap bits have been already read, this doesn't read again but
1423 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001425u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001427 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Takashi Iwai0ba21762007-04-16 11:29:14 +02001429 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1430 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001432 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001433 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001435 info->amp_caps = snd_hda_param_read(codec, nid,
1436 direction == HDA_OUTPUT ?
1437 AC_PAR_AMP_OUT_CAP :
1438 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001439 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001440 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442 return info->amp_caps;
1443}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001444EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Takashi Iwaid5191e52009-11-16 14:58:17 +01001446/**
1447 * snd_hda_override_amp_caps - Override the AMP capabilities
1448 * @codec: the CODEC to clean up
1449 * @nid: the NID to clean up
1450 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1451 * @caps: the capability bits to set
1452 *
1453 * Override the cached AMP caps bits value by the given one.
1454 * This function is useful if the driver needs to adjust the AMP ranges,
1455 * e.g. limit to 0dB, etc.
1456 *
1457 * Returns zero if successful or a negative error code.
1458 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001459int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1460 unsigned int caps)
1461{
1462 struct hda_amp_info *info;
1463
1464 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1465 if (!info)
1466 return -EINVAL;
1467 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001468 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001469 return 0;
1470}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001471EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001472
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001473static unsigned int
1474query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1475 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001476{
1477 struct hda_amp_info *info;
1478
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001479 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001480 if (!info)
1481 return 0;
1482 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001483 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001484 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001485 }
1486 return info->amp_caps;
1487}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001488
1489static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1490{
1491 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1492}
1493
Takashi Iwaid5191e52009-11-16 14:58:17 +01001494/**
1495 * snd_hda_query_pin_caps - Query PIN capabilities
1496 * @codec: the HD-auio codec
1497 * @nid: the NID to query
1498 *
1499 * Query PIN capabilities for the given widget.
1500 * Returns the obtained capability bits.
1501 *
1502 * When cap bits have been already read, this doesn't read again but
1503 * returns the cached value.
1504 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001505u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1506{
1507 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1508 read_pin_cap);
1509}
Takashi Iwai1327a322009-03-23 13:07:47 +01001510EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1511
Wu Fengguang864f92b2009-11-18 12:38:02 +08001512/**
1513 * snd_hda_pin_sense - execute pin sense measurement
1514 * @codec: the CODEC to sense
1515 * @nid: the pin NID to sense
1516 *
1517 * Execute necessary pin sense measurement and return its Presence Detect,
1518 * Impedance, ELD Valid etc. status bits.
1519 */
1520u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1521{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001522 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001523
Takashi Iwai729d55b2009-12-25 22:49:01 +01001524 if (!codec->no_trigger_sense) {
1525 pincap = snd_hda_query_pin_caps(codec, nid);
1526 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001527 snd_hda_codec_read(codec, nid, 0,
1528 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001529 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001530 return snd_hda_codec_read(codec, nid, 0,
1531 AC_VERB_GET_PIN_SENSE, 0);
1532}
1533EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1534
1535/**
1536 * snd_hda_jack_detect - query pin Presence Detect status
1537 * @codec: the CODEC to sense
1538 * @nid: the pin NID to sense
1539 *
1540 * Query and return the pin's Presence Detect status.
1541 */
1542int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1543{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001544 u32 sense = snd_hda_pin_sense(codec, nid);
1545 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001546}
1547EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/*
1550 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001551 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001553static unsigned int get_vol_mute(struct hda_codec *codec,
1554 struct hda_amp_info *info, hda_nid_t nid,
1555 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 u32 val, parm;
1558
Takashi Iwai01751f52007-08-10 16:59:39 +02001559 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001560 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1563 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1564 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001565 val = snd_hda_codec_read(codec, nid, 0,
1566 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001568 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001569 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
1572/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001573 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001575static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001576 hda_nid_t nid, int ch, int direction, int index,
1577 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 u32 parm;
1580
1581 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1582 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1583 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1584 parm |= val;
1585 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001586 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Takashi Iwaid5191e52009-11-16 14:58:17 +01001589/**
1590 * snd_hda_codec_amp_read - Read AMP value
1591 * @codec: HD-audio codec
1592 * @nid: NID to read the AMP value
1593 * @ch: channel (left=0 or right=1)
1594 * @direction: #HDA_INPUT or #HDA_OUTPUT
1595 * @index: the index value (only for input direction)
1596 *
1597 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
Takashi Iwai834be882006-03-01 14:16:17 +01001599int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1600 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001602 struct hda_amp_info *info;
1603 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1604 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001606 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001608EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Takashi Iwaid5191e52009-11-16 14:58:17 +01001610/**
1611 * snd_hda_codec_amp_update - update the AMP value
1612 * @codec: HD-audio codec
1613 * @nid: NID to read the AMP value
1614 * @ch: channel (left=0 or right=1)
1615 * @direction: #HDA_INPUT or #HDA_OUTPUT
1616 * @idx: the index value (only for input direction)
1617 * @mask: bit mask to set
1618 * @val: the bits value to set
1619 *
1620 * Update the AMP value with a bit mask.
1621 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001622 */
Takashi Iwai834be882006-03-01 14:16:17 +01001623int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1624 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001626 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001627
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1629 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001631 if (snd_BUG_ON(mask & ~0xff))
1632 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001633 val &= mask;
1634 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001635 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001637 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return 1;
1639}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001640EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Takashi Iwaid5191e52009-11-16 14:58:17 +01001642/**
1643 * snd_hda_codec_amp_stereo - update the AMP stereo values
1644 * @codec: HD-audio codec
1645 * @nid: NID to read the AMP value
1646 * @direction: #HDA_INPUT or #HDA_OUTPUT
1647 * @idx: the index value (only for input direction)
1648 * @mask: bit mask to set
1649 * @val: the bits value to set
1650 *
1651 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1652 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001653 */
1654int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1655 int direction, int idx, int mask, int val)
1656{
1657 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001658
1659 if (snd_BUG_ON(mask & ~0xff))
1660 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001661 for (ch = 0; ch < 2; ch++)
1662 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1663 idx, mask, val);
1664 return ret;
1665}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001666EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001667
Takashi Iwaicb53c622007-08-10 17:21:45 +02001668#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001669/**
1670 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1671 * @codec: HD-audio codec
1672 *
1673 * Resume the all amp commands from the cache.
1674 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001675void snd_hda_codec_resume_amp(struct hda_codec *codec)
1676{
Takashi Iwai603c4012008-07-30 15:01:44 +02001677 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001678 int i;
1679
Takashi Iwai603c4012008-07-30 15:01:44 +02001680 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001681 u32 key = buffer->head.key;
1682 hda_nid_t nid;
1683 unsigned int idx, dir, ch;
1684 if (!key)
1685 continue;
1686 nid = key & 0xff;
1687 idx = (key >> 16) & 0xff;
1688 dir = (key >> 24) & 0xff;
1689 for (ch = 0; ch < 2; ch++) {
1690 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1691 continue;
1692 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1693 buffer->vol[ch]);
1694 }
1695 }
1696}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001697EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001698#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001700static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1701 unsigned int ofs)
1702{
1703 u32 caps = query_amp_caps(codec, nid, dir);
1704 /* get num steps */
1705 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1706 if (ofs < caps)
1707 caps -= ofs;
1708 return caps;
1709}
1710
Takashi Iwaid5191e52009-11-16 14:58:17 +01001711/**
1712 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1713 *
1714 * The control element is supposed to have the private_value field
1715 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1716 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001717int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1718 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1721 u16 nid = get_amp_nid(kcontrol);
1722 u8 chs = get_amp_channels(kcontrol);
1723 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001724 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001726 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1727 uinfo->count = chs == 3 ? 2 : 1;
1728 uinfo->value.integer.min = 0;
1729 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1730 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001731 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001732 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1733 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return -EINVAL;
1735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return 0;
1737}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001738EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001740
1741static inline unsigned int
1742read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1743 int ch, int dir, int idx, unsigned int ofs)
1744{
1745 unsigned int val;
1746 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1747 val &= HDA_AMP_VOLMASK;
1748 if (val >= ofs)
1749 val -= ofs;
1750 else
1751 val = 0;
1752 return val;
1753}
1754
1755static inline int
1756update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1757 int ch, int dir, int idx, unsigned int ofs,
1758 unsigned int val)
1759{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001760 unsigned int maxval;
1761
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001762 if (val > 0)
1763 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001764 /* ofs = 0: raw max value */
1765 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001766 if (val > maxval)
1767 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001768 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1769 HDA_AMP_VOLMASK, val);
1770}
1771
Takashi Iwaid5191e52009-11-16 14:58:17 +01001772/**
1773 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1774 *
1775 * The control element is supposed to have the private_value field
1776 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1777 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001778int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1779 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1782 hda_nid_t nid = get_amp_nid(kcontrol);
1783 int chs = get_amp_channels(kcontrol);
1784 int dir = get_amp_direction(kcontrol);
1785 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001786 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 long *valp = ucontrol->value.integer.value;
1788
1789 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001790 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001792 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return 0;
1794}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001795EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Takashi Iwaid5191e52009-11-16 14:58:17 +01001797/**
1798 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1799 *
1800 * The control element is supposed to have the private_value field
1801 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1802 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001803int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1804 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1807 hda_nid_t nid = get_amp_nid(kcontrol);
1808 int chs = get_amp_channels(kcontrol);
1809 int dir = get_amp_direction(kcontrol);
1810 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001811 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 long *valp = ucontrol->value.integer.value;
1813 int change = 0;
1814
Takashi Iwaicb53c622007-08-10 17:21:45 +02001815 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001816 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001817 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001818 valp++;
1819 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001820 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001821 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001822 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return change;
1824}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001825EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Takashi Iwaid5191e52009-11-16 14:58:17 +01001827/**
1828 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1829 *
1830 * The control element is supposed to have the private_value field
1831 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1832 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001833int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1834 unsigned int size, unsigned int __user *_tlv)
1835{
1836 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1837 hda_nid_t nid = get_amp_nid(kcontrol);
1838 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001839 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001840 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001841 u32 caps, val1, val2;
1842
1843 if (size < 4 * sizeof(unsigned int))
1844 return -ENOMEM;
1845 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001846 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1847 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001848 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001849 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001850 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001851 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02001852 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001853 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1854 return -EFAULT;
1855 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1856 return -EFAULT;
1857 if (put_user(val1, _tlv + 2))
1858 return -EFAULT;
1859 if (put_user(val2, _tlv + 3))
1860 return -EFAULT;
1861 return 0;
1862}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001863EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001864
Takashi Iwaid5191e52009-11-16 14:58:17 +01001865/**
1866 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1867 * @codec: HD-audio codec
1868 * @nid: NID of a reference widget
1869 * @dir: #HDA_INPUT or #HDA_OUTPUT
1870 * @tlv: TLV data to be stored, at least 4 elements
1871 *
1872 * Set (static) TLV data for a virtual master volume using the AMP caps
1873 * obtained from the reference NID.
1874 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001875 */
1876void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1877 unsigned int *tlv)
1878{
1879 u32 caps;
1880 int nums, step;
1881
1882 caps = query_amp_caps(codec, nid, dir);
1883 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1884 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1885 step = (step + 1) * 25;
1886 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1887 tlv[1] = 2 * sizeof(unsigned int);
1888 tlv[2] = -nums * step;
1889 tlv[3] = step;
1890}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001891EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001892
1893/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001894static struct snd_kcontrol *
1895_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1896 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001897{
1898 struct snd_ctl_elem_id id;
1899 memset(&id, 0, sizeof(id));
1900 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001901 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001902 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1903 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001904 strcpy(id.name, name);
1905 return snd_ctl_find_id(codec->bus->card, &id);
1906}
1907
Takashi Iwaid5191e52009-11-16 14:58:17 +01001908/**
1909 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1910 * @codec: HD-audio codec
1911 * @name: ctl id name string
1912 *
1913 * Get the control element with the given id string and IFACE_MIXER.
1914 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001915struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1916 const char *name)
1917{
1918 return _snd_hda_find_mixer_ctl(codec, name, 0);
1919}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001920EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001921
Takashi Iwai1afe2062010-12-23 10:17:52 +01001922static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
1923{
1924 int idx;
1925 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
1926 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
1927 return idx;
1928 }
1929 return -EBUSY;
1930}
1931
Takashi Iwaid5191e52009-11-16 14:58:17 +01001932/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001933 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001934 * @codec: HD-audio codec
1935 * @nid: corresponding NID (optional)
1936 * @kctl: the control element to assign
1937 *
1938 * Add the given control element to an array inside the codec instance.
1939 * All control elements belonging to a codec are supposed to be added
1940 * by this function so that a proper clean-up works at the free or
1941 * reconfiguration time.
1942 *
1943 * If non-zero @nid is passed, the NID is assigned to the control element.
1944 * The assignment is shown in the codec proc file.
1945 *
1946 * snd_hda_ctl_add() checks the control subdev id field whether
1947 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001948 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1949 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001950 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001951int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1952 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001953{
1954 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001955 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001956 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001957
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001958 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001959 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001960 if (nid == 0)
1961 nid = get_amp_nid_(kctl->private_value);
1962 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001963 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1964 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001965 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001966 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001967 err = snd_ctl_add(codec->bus->card, kctl);
1968 if (err < 0)
1969 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001970 item = snd_array_new(&codec->mixers);
1971 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001972 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001973 item->kctl = kctl;
1974 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001975 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001976 return 0;
1977}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001978EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001979
Takashi Iwaid5191e52009-11-16 14:58:17 +01001980/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001981 * snd_hda_add_nid - Assign a NID to a control element
1982 * @codec: HD-audio codec
1983 * @nid: corresponding NID (optional)
1984 * @kctl: the control element to assign
1985 * @index: index to kctl
1986 *
1987 * Add the given control element to an array inside the codec instance.
1988 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1989 * NID:KCTL mapping - for example "Capture Source" selector.
1990 */
1991int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1992 unsigned int index, hda_nid_t nid)
1993{
1994 struct hda_nid_item *item;
1995
1996 if (nid > 0) {
1997 item = snd_array_new(&codec->nids);
1998 if (!item)
1999 return -ENOMEM;
2000 item->kctl = kctl;
2001 item->index = index;
2002 item->nid = nid;
2003 return 0;
2004 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002005 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2006 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002007 return -EINVAL;
2008}
2009EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2010
2011/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002012 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2013 * @codec: HD-audio codec
2014 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002015void snd_hda_ctls_clear(struct hda_codec *codec)
2016{
2017 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002018 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002019 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002020 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002021 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002022 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002023}
2024
Takashi Iwaia65d6292009-02-23 16:57:04 +01002025/* pseudo device locking
2026 * toggle card->shutdown to allow/disallow the device access (as a hack)
2027 */
2028static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002029{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002030 spin_lock(&card->files_lock);
2031 if (card->shutdown) {
2032 spin_unlock(&card->files_lock);
2033 return -EINVAL;
2034 }
2035 card->shutdown = 1;
2036 spin_unlock(&card->files_lock);
2037 return 0;
2038}
2039
2040static void hda_unlock_devices(struct snd_card *card)
2041{
2042 spin_lock(&card->files_lock);
2043 card->shutdown = 0;
2044 spin_unlock(&card->files_lock);
2045}
2046
Takashi Iwaid5191e52009-11-16 14:58:17 +01002047/**
2048 * snd_hda_codec_reset - Clear all objects assigned to the codec
2049 * @codec: HD-audio codec
2050 *
2051 * This frees the all PCM and control elements assigned to the codec, and
2052 * clears the caches and restores the pin default configurations.
2053 *
2054 * When a device is being used, it returns -EBSY. If successfully freed,
2055 * returns zero.
2056 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002057int snd_hda_codec_reset(struct hda_codec *codec)
2058{
2059 struct snd_card *card = codec->bus->card;
2060 int i, pcm;
2061
2062 if (hda_lock_devices(card) < 0)
2063 return -EBUSY;
2064 /* check whether the codec isn't used by any mixer or PCM streams */
2065 if (!list_empty(&card->ctl_files)) {
2066 hda_unlock_devices(card);
2067 return -EBUSY;
2068 }
2069 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2070 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2071 if (!cpcm->pcm)
2072 continue;
2073 if (cpcm->pcm->streams[0].substream_opened ||
2074 cpcm->pcm->streams[1].substream_opened) {
2075 hda_unlock_devices(card);
2076 return -EBUSY;
2077 }
2078 }
2079
2080 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002081
2082#ifdef CONFIG_SND_HDA_POWER_SAVE
2083 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002084 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002085#endif
2086 snd_hda_ctls_clear(codec);
2087 /* relase PCMs */
2088 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002089 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002090 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002091 clear_bit(codec->pcm_info[i].device,
2092 codec->bus->pcm_dev_bits);
2093 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002094 }
2095 if (codec->patch_ops.free)
2096 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002097 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002098 codec->spec = NULL;
2099 free_hda_cache(&codec->amp_cache);
2100 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002101 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2102 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002103 /* free only driver_pins so that init_pins + user_pins are restored */
2104 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002105 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002106 codec->num_pcms = 0;
2107 codec->pcm_info = NULL;
2108 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002109 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2110 codec->slave_dig_outs = NULL;
2111 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002112 module_put(codec->owner);
2113 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002114
2115 /* allow device access again */
2116 hda_unlock_devices(card);
2117 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002118}
2119
Takashi Iwaid5191e52009-11-16 14:58:17 +01002120/**
2121 * snd_hda_add_vmaster - create a virtual master control and add slaves
2122 * @codec: HD-audio codec
2123 * @name: vmaster control name
2124 * @tlv: TLV data (optional)
2125 * @slaves: slave control names (optional)
2126 *
2127 * Create a virtual master control with the given name. The TLV data
2128 * must be either NULL or a valid data.
2129 *
2130 * @slaves is a NULL-terminated array of strings, each of which is a
2131 * slave control name. All controls with these names are assigned to
2132 * the new virtual master control.
2133 *
2134 * This function returns zero if successful or a negative error code.
2135 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002136int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002137 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002138{
2139 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002140 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002141 int err;
2142
Takashi Iwai2f085542008-02-22 18:43:50 +01002143 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2144 ;
2145 if (!*s) {
2146 snd_printdd("No slave found for %s\n", name);
2147 return 0;
2148 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002149 kctl = snd_ctl_make_virtual_master(name, tlv);
2150 if (!kctl)
2151 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002152 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002153 if (err < 0)
2154 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002155
Takashi Iwai2134ea42008-01-10 16:53:55 +01002156 for (s = slaves; *s; s++) {
2157 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002158 int i = 0;
2159 for (;;) {
2160 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2161 if (!sctl) {
2162 if (!i)
2163 snd_printdd("Cannot find slave %s, "
2164 "skipped\n", *s);
2165 break;
2166 }
2167 err = snd_ctl_add_slave(kctl, sctl);
2168 if (err < 0)
2169 return err;
2170 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002171 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002172 }
2173 return 0;
2174}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002175EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002176
Takashi Iwaid5191e52009-11-16 14:58:17 +01002177/**
2178 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2179 *
2180 * The control element is supposed to have the private_value field
2181 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2182 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002183int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 int chs = get_amp_channels(kcontrol);
2187
2188 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2189 uinfo->count = chs == 3 ? 2 : 1;
2190 uinfo->value.integer.min = 0;
2191 uinfo->value.integer.max = 1;
2192 return 0;
2193}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002194EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196/**
2197 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2198 *
2199 * The control element is supposed to have the private_value field
2200 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2201 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002202int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2203 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2206 hda_nid_t nid = get_amp_nid(kcontrol);
2207 int chs = get_amp_channels(kcontrol);
2208 int dir = get_amp_direction(kcontrol);
2209 int idx = get_amp_index(kcontrol);
2210 long *valp = ucontrol->value.integer.value;
2211
2212 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002213 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002214 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002216 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002217 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return 0;
2219}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002220EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Takashi Iwaid5191e52009-11-16 14:58:17 +01002222/**
2223 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2224 *
2225 * The control element is supposed to have the private_value field
2226 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2227 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002228int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2229 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2232 hda_nid_t nid = get_amp_nid(kcontrol);
2233 int chs = get_amp_channels(kcontrol);
2234 int dir = get_amp_direction(kcontrol);
2235 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 long *valp = ucontrol->value.integer.value;
2237 int change = 0;
2238
Takashi Iwaicb53c622007-08-10 17:21:45 +02002239 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002240 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002241 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002242 HDA_AMP_MUTE,
2243 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002244 valp++;
2245 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002246 if (chs & 2)
2247 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002248 HDA_AMP_MUTE,
2249 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002250 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002251 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return change;
2253}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002254EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Takashi Iwai67d634c2009-11-16 15:35:59 +01002256#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002257/**
2258 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2259 *
2260 * This function calls snd_hda_enable_beep_device(), which behaves differently
2261 * depending on beep_mode option.
2262 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002263int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2264 struct snd_ctl_elem_value *ucontrol)
2265{
2266 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2267 long *valp = ucontrol->value.integer.value;
2268
2269 snd_hda_enable_beep_device(codec, *valp);
2270 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2271}
2272EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002273#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275/*
Takashi Iwai985be542005-11-02 18:26:49 +01002276 * bound volume controls
2277 *
2278 * bind multiple volumes (# indices, from 0)
2279 */
2280
2281#define AMP_VAL_IDX_SHIFT 19
2282#define AMP_VAL_IDX_MASK (0x0f<<19)
2283
Takashi Iwaid5191e52009-11-16 14:58:17 +01002284/**
2285 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2286 *
2287 * The control element is supposed to have the private_value field
2288 * set up via HDA_BIND_MUTE*() macros.
2289 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002290int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2291 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002292{
2293 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2294 unsigned long pval;
2295 int err;
2296
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002297 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002298 pval = kcontrol->private_value;
2299 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2300 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2301 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002302 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002303 return err;
2304}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002305EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002306
Takashi Iwaid5191e52009-11-16 14:58:17 +01002307/**
2308 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2309 *
2310 * The control element is supposed to have the private_value field
2311 * set up via HDA_BIND_MUTE*() macros.
2312 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002313int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2314 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002315{
2316 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2317 unsigned long pval;
2318 int i, indices, err = 0, change = 0;
2319
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002320 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002321 pval = kcontrol->private_value;
2322 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2323 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002324 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2325 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002326 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2327 if (err < 0)
2328 break;
2329 change |= err;
2330 }
2331 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002332 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002333 return err < 0 ? err : change;
2334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002335EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002336
Takashi Iwaid5191e52009-11-16 14:58:17 +01002337/**
2338 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2339 *
2340 * The control element is supposed to have the private_value field
2341 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002342 */
2343int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_info *uinfo)
2345{
2346 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2347 struct hda_bind_ctls *c;
2348 int err;
2349
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002350 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002351 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002352 kcontrol->private_value = *c->values;
2353 err = c->ops->info(kcontrol, uinfo);
2354 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002355 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002356 return err;
2357}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002358EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002359
Takashi Iwaid5191e52009-11-16 14:58:17 +01002360/**
2361 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2362 *
2363 * The control element is supposed to have the private_value field
2364 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2365 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002366int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2367 struct snd_ctl_elem_value *ucontrol)
2368{
2369 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2370 struct hda_bind_ctls *c;
2371 int err;
2372
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002373 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002374 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002375 kcontrol->private_value = *c->values;
2376 err = c->ops->get(kcontrol, ucontrol);
2377 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002378 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002379 return err;
2380}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002381EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002382
Takashi Iwaid5191e52009-11-16 14:58:17 +01002383/**
2384 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2385 *
2386 * The control element is supposed to have the private_value field
2387 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2388 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002389int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2390 struct snd_ctl_elem_value *ucontrol)
2391{
2392 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2393 struct hda_bind_ctls *c;
2394 unsigned long *vals;
2395 int err = 0, change = 0;
2396
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002397 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002398 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002399 for (vals = c->values; *vals; vals++) {
2400 kcontrol->private_value = *vals;
2401 err = c->ops->put(kcontrol, ucontrol);
2402 if (err < 0)
2403 break;
2404 change |= err;
2405 }
2406 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002407 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002408 return err < 0 ? err : change;
2409}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002410EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002411
Takashi Iwaid5191e52009-11-16 14:58:17 +01002412/**
2413 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2414 *
2415 * The control element is supposed to have the private_value field
2416 * set up via HDA_BIND_VOL() macro.
2417 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002418int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2419 unsigned int size, unsigned int __user *tlv)
2420{
2421 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2422 struct hda_bind_ctls *c;
2423 int err;
2424
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002425 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002426 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002427 kcontrol->private_value = *c->values;
2428 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2429 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002430 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002431 return err;
2432}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002433EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002434
2435struct hda_ctl_ops snd_hda_bind_vol = {
2436 .info = snd_hda_mixer_amp_volume_info,
2437 .get = snd_hda_mixer_amp_volume_get,
2438 .put = snd_hda_mixer_amp_volume_put,
2439 .tlv = snd_hda_mixer_amp_tlv
2440};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002441EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002442
2443struct hda_ctl_ops snd_hda_bind_sw = {
2444 .info = snd_hda_mixer_amp_switch_info,
2445 .get = snd_hda_mixer_amp_switch_get,
2446 .put = snd_hda_mixer_amp_switch_put,
2447 .tlv = snd_hda_mixer_amp_tlv
2448};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002449EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002450
2451/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 * SPDIF out controls
2453 */
2454
Takashi Iwai0ba21762007-04-16 11:29:14 +02002455static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
2458 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2459 uinfo->count = 1;
2460 return 0;
2461}
2462
Takashi Iwai0ba21762007-04-16 11:29:14 +02002463static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2467 IEC958_AES0_NONAUDIO |
2468 IEC958_AES0_CON_EMPHASIS_5015 |
2469 IEC958_AES0_CON_NOT_COPYRIGHT;
2470 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2471 IEC958_AES1_CON_ORIGINAL;
2472 return 0;
2473}
2474
Takashi Iwai0ba21762007-04-16 11:29:14 +02002475static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2476 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2479 IEC958_AES0_NONAUDIO |
2480 IEC958_AES0_PRO_EMPHASIS_5015;
2481 return 0;
2482}
2483
Takashi Iwai0ba21762007-04-16 11:29:14 +02002484static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2488
2489 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2490 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2491 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2492 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2493
2494 return 0;
2495}
2496
2497/* convert from SPDIF status bits to HDA SPDIF bits
2498 * bit 0 (DigEn) is always set zero (to be filled later)
2499 */
2500static unsigned short convert_from_spdif_status(unsigned int sbits)
2501{
2502 unsigned short val = 0;
2503
2504 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002505 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002507 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002509 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2510 IEC958_AES0_PRO_EMPHASIS_5015)
2511 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002513 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2514 IEC958_AES0_CON_EMPHASIS_5015)
2515 val |= AC_DIG1_EMPHASIS;
2516 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2517 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002519 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2521 }
2522 return val;
2523}
2524
2525/* convert to SPDIF status bits from HDA SPDIF bits
2526 */
2527static unsigned int convert_to_spdif_status(unsigned short val)
2528{
2529 unsigned int sbits = 0;
2530
Takashi Iwai0ba21762007-04-16 11:29:14 +02002531 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002533 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 sbits |= IEC958_AES0_PROFESSIONAL;
2535 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002536 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2538 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002539 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002541 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002543 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2545 sbits |= val & (0x7f << 8);
2546 }
2547 return sbits;
2548}
2549
Takashi Iwai2f728532008-09-25 16:32:41 +02002550/* set digital convert verbs both for the given NID and its slaves */
2551static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2552 int verb, int val)
2553{
2554 hda_nid_t *d;
2555
Takashi Iwai9e976972008-11-25 08:17:20 +01002556 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002557 d = codec->slave_dig_outs;
2558 if (!d)
2559 return;
2560 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002561 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002562}
2563
2564static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2565 int dig1, int dig2)
2566{
2567 if (dig1 != -1)
2568 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2569 if (dig2 != -1)
2570 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2571}
2572
Takashi Iwai0ba21762007-04-16 11:29:14 +02002573static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2574 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
2576 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2577 hda_nid_t nid = kcontrol->private_value;
2578 unsigned short val;
2579 int change;
2580
Ingo Molnar62932df2006-01-16 16:34:20 +01002581 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 codec->spdif_status = ucontrol->value.iec958.status[0] |
2583 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2584 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2585 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2586 val = convert_from_spdif_status(codec->spdif_status);
2587 val |= codec->spdif_ctls & 1;
2588 change = codec->spdif_ctls != val;
2589 codec->spdif_ctls = val;
2590
Takashi Iwai2f728532008-09-25 16:32:41 +02002591 if (change)
2592 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Ingo Molnar62932df2006-01-16 16:34:20 +01002594 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return change;
2596}
2597
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002598#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Takashi Iwai0ba21762007-04-16 11:29:14 +02002600static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2601 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
2603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2604
Takashi Iwai0ba21762007-04-16 11:29:14 +02002605 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return 0;
2607}
2608
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2610 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
2612 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2613 hda_nid_t nid = kcontrol->private_value;
2614 unsigned short val;
2615 int change;
2616
Ingo Molnar62932df2006-01-16 16:34:20 +01002617 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002618 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002620 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002622 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002624 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002625 /* unmute amp switch (if any) */
2626 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002627 (val & AC_DIG1_ENABLE))
2628 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2629 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002631 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return change;
2633}
2634
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002635static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 {
2637 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2638 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002639 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 .info = snd_hda_spdif_mask_info,
2641 .get = snd_hda_spdif_cmask_get,
2642 },
2643 {
2644 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002646 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 .info = snd_hda_spdif_mask_info,
2648 .get = snd_hda_spdif_pmask_get,
2649 },
2650 {
2651 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002652 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 .info = snd_hda_spdif_mask_info,
2654 .get = snd_hda_spdif_default_get,
2655 .put = snd_hda_spdif_default_put,
2656 },
2657 {
2658 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002659 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 .info = snd_hda_spdif_out_switch_info,
2661 .get = snd_hda_spdif_out_switch_get,
2662 .put = snd_hda_spdif_out_switch_put,
2663 },
2664 { } /* end */
2665};
2666
2667/**
2668 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2669 * @codec: the HDA codec
2670 * @nid: audio out widget NID
2671 *
2672 * Creates controls related with the SPDIF output.
2673 * Called from each patch supporting the SPDIF out.
2674 *
2675 * Returns 0 if successful, or a negative error code.
2676 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002677int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
2679 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002680 struct snd_kcontrol *kctl;
2681 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002682 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Takashi Iwai1afe2062010-12-23 10:17:52 +01002684 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2685 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002686 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2687 return -EBUSY;
2688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2690 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002691 if (!kctl)
2692 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002693 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002695 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002696 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return err;
2698 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002699 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002700 snd_hda_codec_read(codec, nid, 0,
2701 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2703 return 0;
2704}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002705EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002708 * SPDIF sharing with analog output
2709 */
2710static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
2712{
2713 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2714 ucontrol->value.integer.value[0] = mout->share_spdif;
2715 return 0;
2716}
2717
2718static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2719 struct snd_ctl_elem_value *ucontrol)
2720{
2721 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2722 mout->share_spdif = !!ucontrol->value.integer.value[0];
2723 return 0;
2724}
2725
2726static struct snd_kcontrol_new spdif_share_sw = {
2727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2728 .name = "IEC958 Default PCM Playback Switch",
2729 .info = snd_ctl_boolean_mono_info,
2730 .get = spdif_share_sw_get,
2731 .put = spdif_share_sw_put,
2732};
2733
Takashi Iwaid5191e52009-11-16 14:58:17 +01002734/**
2735 * snd_hda_create_spdif_share_sw - create Default PCM switch
2736 * @codec: the HDA codec
2737 * @mout: multi-out instance
2738 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002739int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2740 struct hda_multi_out *mout)
2741{
2742 if (!mout->dig_out_nid)
2743 return 0;
2744 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002745 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2746 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002747}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002748EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002749
2750/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 * SPDIF input
2752 */
2753
2754#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2755
Takashi Iwai0ba21762007-04-16 11:29:14 +02002756static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2757 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2760
2761 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2762 return 0;
2763}
2764
Takashi Iwai0ba21762007-04-16 11:29:14 +02002765static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
2768 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2769 hda_nid_t nid = kcontrol->private_value;
2770 unsigned int val = !!ucontrol->value.integer.value[0];
2771 int change;
2772
Ingo Molnar62932df2006-01-16 16:34:20 +01002773 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002775 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002777 snd_hda_codec_write_cache(codec, nid, 0,
2778 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002780 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return change;
2782}
2783
Takashi Iwai0ba21762007-04-16 11:29:14 +02002784static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2785 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
2787 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2788 hda_nid_t nid = kcontrol->private_value;
2789 unsigned short val;
2790 unsigned int sbits;
2791
Andrew Paprocki3982d172007-12-19 12:13:44 +01002792 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 sbits = convert_to_spdif_status(val);
2794 ucontrol->value.iec958.status[0] = sbits;
2795 ucontrol->value.iec958.status[1] = sbits >> 8;
2796 ucontrol->value.iec958.status[2] = sbits >> 16;
2797 ucontrol->value.iec958.status[3] = sbits >> 24;
2798 return 0;
2799}
2800
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002801static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 {
2803 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002804 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 .info = snd_hda_spdif_in_switch_info,
2806 .get = snd_hda_spdif_in_switch_get,
2807 .put = snd_hda_spdif_in_switch_put,
2808 },
2809 {
2810 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2811 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002812 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 .info = snd_hda_spdif_mask_info,
2814 .get = snd_hda_spdif_in_status_get,
2815 },
2816 { } /* end */
2817};
2818
2819/**
2820 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2821 * @codec: the HDA codec
2822 * @nid: audio in widget NID
2823 *
2824 * Creates controls related with the SPDIF input.
2825 * Called from each patch supporting the SPDIF in.
2826 *
2827 * Returns 0 if successful, or a negative error code.
2828 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002829int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
2831 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002832 struct snd_kcontrol *kctl;
2833 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002834 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Takashi Iwai1afe2062010-12-23 10:17:52 +01002836 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
2837 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002838 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2839 return -EBUSY;
2840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2842 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002843 if (!kctl)
2844 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002846 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002847 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return err;
2849 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002850 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002851 snd_hda_codec_read(codec, nid, 0,
2852 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002853 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return 0;
2855}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002856EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Takashi Iwaicb53c622007-08-10 17:21:45 +02002858#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002859/*
2860 * command cache
2861 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002863/* build a 32bit cache key with the widget id and the command parameter */
2864#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2865#define get_cmd_cache_nid(key) ((key) & 0xff)
2866#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2867
2868/**
2869 * snd_hda_codec_write_cache - send a single command with caching
2870 * @codec: the HDA codec
2871 * @nid: NID to send the command
2872 * @direct: direct flag
2873 * @verb: the verb to send
2874 * @parm: the parameter for the verb
2875 *
2876 * Send a single command without waiting for response.
2877 *
2878 * Returns 0 if successful, or a negative error code.
2879 */
2880int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2881 int direct, unsigned int verb, unsigned int parm)
2882{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002883 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2884 struct hda_cache_head *c;
2885 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002886
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002887 if (err < 0)
2888 return err;
2889 /* parm may contain the verb stuff for get/set amp */
2890 verb = verb | (parm >> 8);
2891 parm &= 0xff;
2892 key = build_cmd_cache_key(nid, verb);
2893 mutex_lock(&codec->bus->cmd_mutex);
2894 c = get_alloc_hash(&codec->cmd_cache, key);
2895 if (c)
2896 c->val = parm;
2897 mutex_unlock(&codec->bus->cmd_mutex);
2898 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002899}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002900EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002901
Takashi Iwaid5191e52009-11-16 14:58:17 +01002902/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002903 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2904 * @codec: the HDA codec
2905 * @nid: NID to send the command
2906 * @direct: direct flag
2907 * @verb: the verb to send
2908 * @parm: the parameter for the verb
2909 *
2910 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2911 * command if the parameter is already identical with the cached value.
2912 * If not, it sends the command and refreshes the cache.
2913 *
2914 * Returns 0 if successful, or a negative error code.
2915 */
2916int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2917 int direct, unsigned int verb, unsigned int parm)
2918{
2919 struct hda_cache_head *c;
2920 u32 key;
2921
2922 /* parm may contain the verb stuff for get/set amp */
2923 verb = verb | (parm >> 8);
2924 parm &= 0xff;
2925 key = build_cmd_cache_key(nid, verb);
2926 mutex_lock(&codec->bus->cmd_mutex);
2927 c = get_hash(&codec->cmd_cache, key);
2928 if (c && c->val == parm) {
2929 mutex_unlock(&codec->bus->cmd_mutex);
2930 return 0;
2931 }
2932 mutex_unlock(&codec->bus->cmd_mutex);
2933 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2934}
2935EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2936
2937/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002938 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2939 * @codec: HD-audio codec
2940 *
2941 * Execute all verbs recorded in the command caches to resume.
2942 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002943void snd_hda_codec_resume_cache(struct hda_codec *codec)
2944{
Takashi Iwai603c4012008-07-30 15:01:44 +02002945 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002946 int i;
2947
Takashi Iwai603c4012008-07-30 15:01:44 +02002948 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002949 u32 key = buffer->key;
2950 if (!key)
2951 continue;
2952 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2953 get_cmd_cache_cmd(key), buffer->val);
2954 }
2955}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002956EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002957
2958/**
2959 * snd_hda_sequence_write_cache - sequence writes with caching
2960 * @codec: the HDA codec
2961 * @seq: VERB array to send
2962 *
2963 * Send the commands sequentially from the given array.
2964 * Thte commands are recorded on cache for power-save and resume.
2965 * The array must be terminated with NID=0.
2966 */
2967void snd_hda_sequence_write_cache(struct hda_codec *codec,
2968 const struct hda_verb *seq)
2969{
2970 for (; seq->nid; seq++)
2971 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2972 seq->param);
2973}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002974EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002975#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002976
Takashi Iwai54d17402005-11-21 16:33:22 +01002977/*
2978 * set power state of the codec
2979 */
2980static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2981 unsigned int power_state)
2982{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002983 hda_nid_t nid;
2984 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002985
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002986 /* this delay seems necessary to avoid click noise at power-down */
2987 if (power_state == AC_PWRST_D3)
2988 msleep(100);
2989 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002990 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002991 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002992 if (power_state == AC_PWRST_D0 &&
2993 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002994 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002995
Takashi Iwaicb53c622007-08-10 17:21:45 +02002996 nid = codec->start_nid;
2997 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002998 unsigned int wcaps = get_wcaps(codec, nid);
2999 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003000 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003001 if (power_state == AC_PWRST_D3 &&
3002 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003003 unsigned int pincap;
3004 /*
3005 * don't power down the widget if it controls
3006 * eapd and EAPD_BTLENABLE is set.
3007 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003008 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003009 if (pincap & AC_PINCAP_EAPD) {
3010 int eapd = snd_hda_codec_read(codec,
3011 nid, 0,
3012 AC_VERB_GET_EAPD_BTLENABLE, 0);
3013 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003014 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003015 continue;
3016 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003017 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003018 snd_hda_codec_write(codec, nid, 0,
3019 AC_VERB_SET_POWER_STATE,
3020 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003021 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003022 }
3023
Takashi Iwaicb53c622007-08-10 17:21:45 +02003024 if (power_state == AC_PWRST_D0) {
3025 unsigned long end_time;
3026 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003027 /* wait until the codec reachs to D0 */
3028 end_time = jiffies + msecs_to_jiffies(500);
3029 do {
3030 state = snd_hda_codec_read(codec, fg, 0,
3031 AC_VERB_GET_POWER_STATE, 0);
3032 if (state == power_state)
3033 break;
3034 msleep(1);
3035 } while (time_after_eq(end_time, jiffies));
3036 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003037}
3038
Takashi Iwai11aeff02008-07-30 15:01:46 +02003039#ifdef CONFIG_SND_HDA_HWDEP
3040/* execute additional init verbs */
3041static void hda_exec_init_verbs(struct hda_codec *codec)
3042{
3043 if (codec->init_verbs.list)
3044 snd_hda_sequence_write(codec, codec->init_verbs.list);
3045}
3046#else
3047static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3048#endif
3049
Takashi Iwaicb53c622007-08-10 17:21:45 +02003050#ifdef SND_HDA_NEEDS_RESUME
3051/*
3052 * call suspend and power-down; used both from PM and power-save
3053 */
3054static void hda_call_codec_suspend(struct hda_codec *codec)
3055{
3056 if (codec->patch_ops.suspend)
3057 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003058 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003059 hda_set_power_state(codec,
3060 codec->afg ? codec->afg : codec->mfg,
3061 AC_PWRST_D3);
3062#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003063 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003064 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003065 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003066 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003067 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003068#endif
3069}
3070
3071/*
3072 * kick up codec; used both from PM and power-save
3073 */
3074static void hda_call_codec_resume(struct hda_codec *codec)
3075{
3076 hda_set_power_state(codec,
3077 codec->afg ? codec->afg : codec->mfg,
3078 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003079 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003080 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003081 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003082 if (codec->patch_ops.resume)
3083 codec->patch_ops.resume(codec);
3084 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003085 if (codec->patch_ops.init)
3086 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003087 snd_hda_codec_resume_amp(codec);
3088 snd_hda_codec_resume_cache(codec);
3089 }
3090}
3091#endif /* SND_HDA_NEEDS_RESUME */
3092
Takashi Iwai54d17402005-11-21 16:33:22 +01003093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094/**
3095 * snd_hda_build_controls - build mixer controls
3096 * @bus: the BUS
3097 *
3098 * Creates mixer controls for each codec included in the bus.
3099 *
3100 * Returns 0 if successful, otherwise a negative error code.
3101 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003102int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003104 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Takashi Iwai0ba21762007-04-16 11:29:14 +02003106 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003107 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003108 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003109 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003110 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003111 err = snd_hda_codec_reset(codec);
3112 if (err < 0) {
3113 printk(KERN_ERR
3114 "hda_codec: cannot revert codec\n");
3115 return err;
3116 }
3117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003119 return 0;
3120}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003121EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003123int snd_hda_codec_build_controls(struct hda_codec *codec)
3124{
3125 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003126 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003127 /* continue to initialize... */
3128 if (codec->patch_ops.init)
3129 err = codec->patch_ops.init(codec);
3130 if (!err && codec->patch_ops.build_controls)
3131 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003132 if (err < 0)
3133 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return 0;
3135}
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137/*
3138 * stream formats
3139 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003140struct hda_rate_tbl {
3141 unsigned int hz;
3142 unsigned int alsa_bits;
3143 unsigned int hda_fmt;
3144};
3145
Takashi Iwai92f10b32010-08-03 14:21:00 +02003146/* rate = base * mult / div */
3147#define HDA_RATE(base, mult, div) \
3148 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3149 (((div) - 1) << AC_FMT_DIV_SHIFT))
3150
Takashi Iwaibefdf312005-08-22 13:57:55 +02003151static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003153
3154 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003155 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3156 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3157 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3158 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3159 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3160 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3161 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3162 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3163 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3164 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3165 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003166#define AC_PAR_PCM_RATE_BITS 11
3167 /* up to bits 10, 384kHZ isn't supported properly */
3168
3169 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003170 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003171
Takashi Iwaibefdf312005-08-22 13:57:55 +02003172 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173};
3174
3175/**
3176 * snd_hda_calc_stream_format - calculate format bitset
3177 * @rate: the sample rate
3178 * @channels: the number of channels
3179 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3180 * @maxbps: the max. bps
3181 *
3182 * Calculate the format bitset from the given rate, channels and th PCM format.
3183 *
3184 * Return zero if invalid.
3185 */
3186unsigned int snd_hda_calc_stream_format(unsigned int rate,
3187 unsigned int channels,
3188 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003189 unsigned int maxbps,
3190 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
3192 int i;
3193 unsigned int val = 0;
3194
Takashi Iwaibefdf312005-08-22 13:57:55 +02003195 for (i = 0; rate_bits[i].hz; i++)
3196 if (rate_bits[i].hz == rate) {
3197 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 break;
3199 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003200 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 snd_printdd("invalid rate %d\n", rate);
3202 return 0;
3203 }
3204
3205 if (channels == 0 || channels > 8) {
3206 snd_printdd("invalid channels %d\n", channels);
3207 return 0;
3208 }
3209 val |= channels - 1;
3210
3211 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003212 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003213 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003214 break;
3215 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003216 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 case 20:
3219 case 24:
3220 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003221 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003222 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003224 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003226 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 break;
3228 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003229 snd_printdd("invalid format width %d\n",
3230 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return 0;
3232 }
3233
Anssi Hannula32c168c2010-08-03 13:28:57 +03003234 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003235 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return val;
3238}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003239EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003241static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3242{
3243 unsigned int val = 0;
3244 if (nid != codec->afg &&
3245 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3246 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3247 if (!val || val == -1)
3248 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3249 if (!val || val == -1)
3250 return 0;
3251 return val;
3252}
3253
3254static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3255{
3256 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3257 get_pcm_param);
3258}
3259
3260static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3261{
3262 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3263 if (!streams || streams == -1)
3264 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3265 if (!streams || streams == -1)
3266 return 0;
3267 return streams;
3268}
3269
3270static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3271{
3272 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3273 get_stream_param);
3274}
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276/**
3277 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3278 * @codec: the HDA codec
3279 * @nid: NID to query
3280 * @ratesp: the pointer to store the detected rate bitflags
3281 * @formatsp: the pointer to store the detected formats
3282 * @bpsp: the pointer to store the detected format widths
3283 *
3284 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3285 * or @bsps argument is ignored.
3286 *
3287 * Returns 0 if successful, otherwise a negative error code.
3288 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003289static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3291{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003292 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003294 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003295 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297 if (ratesp) {
3298 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003299 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003301 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003303 if (rates == 0) {
3304 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3305 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3306 nid, val,
3307 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3308 return -EIO;
3309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 *ratesp = rates;
3311 }
3312
3313 if (formatsp || bpsp) {
3314 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003315 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003317 streams = query_stream_param(codec, nid);
3318 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
3321 bps = 0;
3322 if (streams & AC_SUPFMT_PCM) {
3323 if (val & AC_SUPPCM_BITS_8) {
3324 formats |= SNDRV_PCM_FMTBIT_U8;
3325 bps = 8;
3326 }
3327 if (val & AC_SUPPCM_BITS_16) {
3328 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3329 bps = 16;
3330 }
3331 if (wcaps & AC_WCAP_DIGITAL) {
3332 if (val & AC_SUPPCM_BITS_32)
3333 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3334 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3335 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3336 if (val & AC_SUPPCM_BITS_24)
3337 bps = 24;
3338 else if (val & AC_SUPPCM_BITS_20)
3339 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003340 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3341 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3343 if (val & AC_SUPPCM_BITS_32)
3344 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 else if (val & AC_SUPPCM_BITS_24)
3346 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003347 else if (val & AC_SUPPCM_BITS_20)
3348 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
3350 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003351 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003353 if (!bps)
3354 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003355 }
3356 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003357 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 /* temporary hack: we have still no proper support
3359 * for the direct AC3 stream...
3360 */
3361 formats |= SNDRV_PCM_FMTBIT_U8;
3362 bps = 8;
3363 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003364 if (formats == 0) {
3365 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3366 "(nid=0x%x, val=0x%x, ovrd=%i, "
3367 "streams=0x%x)\n",
3368 nid, val,
3369 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3370 streams);
3371 return -EIO;
3372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 if (formatsp)
3374 *formatsp = formats;
3375 if (bpsp)
3376 *bpsp = bps;
3377 }
3378
3379 return 0;
3380}
3381
3382/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003383 * snd_hda_is_supported_format - Check the validity of the format
3384 * @codec: HD-audio codec
3385 * @nid: NID to check
3386 * @format: the HD-audio format value to check
3387 *
3388 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 *
3390 * Returns 1 if supported, 0 if not.
3391 */
3392int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3393 unsigned int format)
3394{
3395 int i;
3396 unsigned int val = 0, rate, stream;
3397
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003398 val = query_pcm_param(codec, nid);
3399 if (!val)
3400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
3402 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003403 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003404 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 if (val & (1 << i))
3406 break;
3407 return 0;
3408 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003409 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return 0;
3411
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003412 stream = query_stream_param(codec, nid);
3413 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return 0;
3415
3416 if (stream & AC_SUPFMT_PCM) {
3417 switch (format & 0xf0) {
3418 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003419 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 return 0;
3421 break;
3422 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003423 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 return 0;
3425 break;
3426 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003427 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return 0;
3429 break;
3430 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003431 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 return 0;
3433 break;
3434 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003435 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return 0;
3437 break;
3438 default:
3439 return 0;
3440 }
3441 } else {
3442 /* FIXME: check for float32 and AC3? */
3443 }
3444
3445 return 1;
3446}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003447EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
3449/*
3450 * PCM stuff
3451 */
3452static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3453 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003454 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
3456 return 0;
3457}
3458
3459static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3460 struct hda_codec *codec,
3461 unsigned int stream_tag,
3462 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003463 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
3465 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3466 return 0;
3467}
3468
3469static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3470 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003471 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Takashi Iwai888afa12008-03-18 09:57:50 +01003473 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 return 0;
3475}
3476
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003477static int set_pcm_default_values(struct hda_codec *codec,
3478 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003480 int err;
3481
Takashi Iwai0ba21762007-04-16 11:29:14 +02003482 /* query support PCM information from the given NID */
3483 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003484 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003485 info->rates ? NULL : &info->rates,
3486 info->formats ? NULL : &info->formats,
3487 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003488 if (err < 0)
3489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
3491 if (info->ops.open == NULL)
3492 info->ops.open = hda_pcm_default_open_close;
3493 if (info->ops.close == NULL)
3494 info->ops.close = hda_pcm_default_open_close;
3495 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003496 if (snd_BUG_ON(!info->nid))
3497 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 info->ops.prepare = hda_pcm_default_prepare;
3499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003501 if (snd_BUG_ON(!info->nid))
3502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 info->ops.cleanup = hda_pcm_default_cleanup;
3504 }
3505 return 0;
3506}
3507
Takashi Iwaieb541332010-08-06 13:48:11 +02003508/*
3509 * codec prepare/cleanup entries
3510 */
3511int snd_hda_codec_prepare(struct hda_codec *codec,
3512 struct hda_pcm_stream *hinfo,
3513 unsigned int stream,
3514 unsigned int format,
3515 struct snd_pcm_substream *substream)
3516{
3517 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003518 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003519 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3520 if (ret >= 0)
3521 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003522 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003523 return ret;
3524}
3525EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3526
3527void snd_hda_codec_cleanup(struct hda_codec *codec,
3528 struct hda_pcm_stream *hinfo,
3529 struct snd_pcm_substream *substream)
3530{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003531 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003532 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003533 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003534}
3535EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3536
Takashi Iwaid5191e52009-11-16 14:58:17 +01003537/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003538const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3539 "Audio", "SPDIF", "HDMI", "Modem"
3540};
3541
Takashi Iwai176d5332008-07-30 15:01:44 +02003542/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003543 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003544 *
3545 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003546 */
3547static int get_empty_pcm_device(struct hda_bus *bus, int type)
3548{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003549 /* audio device indices; not linear to keep compatibility */
3550 static int audio_idx[HDA_PCM_NTYPES][5] = {
3551 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3552 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003553 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003554 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003555 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003556 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003557
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003558 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003559 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3560 return -EINVAL;
3561 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003562
3563 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3564 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3565 return audio_idx[type][i];
3566
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003567 snd_printk(KERN_WARNING "Too many %s devices\n",
3568 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003569 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003570}
3571
3572/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003573 * attach a new PCM stream
3574 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003575static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003576{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003577 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003578 struct hda_pcm_stream *info;
3579 int stream, err;
3580
Takashi Iwaib91f0802008-11-04 08:43:08 +01003581 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003582 return -EINVAL;
3583 for (stream = 0; stream < 2; stream++) {
3584 info = &pcm->stream[stream];
3585 if (info->substreams) {
3586 err = set_pcm_default_values(codec, info);
3587 if (err < 0)
3588 return err;
3589 }
3590 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003591 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003592}
3593
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003594/* assign all PCMs of the given codec */
3595int snd_hda_codec_build_pcms(struct hda_codec *codec)
3596{
3597 unsigned int pcm;
3598 int err;
3599
3600 if (!codec->num_pcms) {
3601 if (!codec->patch_ops.build_pcms)
3602 return 0;
3603 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003604 if (err < 0) {
3605 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003606 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003607 err = snd_hda_codec_reset(codec);
3608 if (err < 0) {
3609 printk(KERN_ERR
3610 "hda_codec: cannot revert codec\n");
3611 return err;
3612 }
3613 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003614 }
3615 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3616 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3617 int dev;
3618
3619 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003620 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003621
3622 if (!cpcm->pcm) {
3623 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3624 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003625 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003626 cpcm->device = dev;
3627 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003628 if (err < 0) {
3629 printk(KERN_ERR "hda_codec: cannot attach "
3630 "PCM stream %d for codec #%d\n",
3631 dev, codec->addr);
3632 continue; /* no fatal error */
3633 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003634 }
3635 }
3636 return 0;
3637}
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639/**
3640 * snd_hda_build_pcms - build PCM information
3641 * @bus: the BUS
3642 *
3643 * Create PCM information for each codec included in the bus.
3644 *
3645 * The build_pcms codec patch is requested to set up codec->num_pcms and
3646 * codec->pcm_info properly. The array is referred by the top-level driver
3647 * to create its PCM instances.
3648 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3649 * callback.
3650 *
3651 * At least, substreams, channels_min and channels_max must be filled for
3652 * each stream. substreams = 0 indicates that the stream doesn't exist.
3653 * When rates and/or formats are zero, the supported values are queried
3654 * from the given nid. The nid is used also by the default ops.prepare
3655 * and ops.cleanup callbacks.
3656 *
3657 * The driver needs to call ops.open in its open callback. Similarly,
3658 * ops.close is supposed to be called in the close callback.
3659 * ops.prepare should be called in the prepare or hw_params callback
3660 * with the proper parameters for set up.
3661 * ops.cleanup should be called in hw_free for clean up of streams.
3662 *
3663 * This function returns 0 if successfull, or a negative error code.
3664 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003665int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003667 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Takashi Iwai0ba21762007-04-16 11:29:14 +02003669 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003670 int err = snd_hda_codec_build_pcms(codec);
3671 if (err < 0)
3672 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
3674 return 0;
3675}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003676EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678/**
3679 * snd_hda_check_board_config - compare the current codec with the config table
3680 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003681 * @num_configs: number of config enums
3682 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 * @tbl: configuration table, terminated by null entries
3684 *
3685 * Compares the modelname or PCI subsystem id of the current codec with the
3686 * given configuration table. If a matching entry is found, returns its
3687 * config value (supposed to be 0 or positive).
3688 *
3689 * If no entries are matching, the function returns a negative value.
3690 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003691int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003692 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003693 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003695 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003696 int i;
3697 for (i = 0; i < num_configs; i++) {
3698 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003699 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003700 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3701 "selected\n", models[i]);
3702 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
3704 }
3705 }
3706
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003707 if (!codec->bus->pci || !tbl)
3708 return -1;
3709
3710 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3711 if (!tbl)
3712 return -1;
3713 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003714#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003715 char tmp[10];
3716 const char *model = NULL;
3717 if (models)
3718 model = models[tbl->value];
3719 if (!model) {
3720 sprintf(tmp, "#%d", tbl->value);
3721 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003723 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3724 "for config %x:%x (%s)\n",
3725 model, tbl->subvendor, tbl->subdevice,
3726 (tbl->name ? tbl->name : "Unknown device"));
3727#endif
3728 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 }
3730 return -1;
3731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003732EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
3734/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003735 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003736 subsystem ID with the
3737 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003738
3739 This is important for Gateway notebooks with SB450 HDA Audio
3740 where the vendor ID of the PCI device is:
3741 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3742 and the vendor/subvendor are found only at the codec.
3743
3744 * @codec: the HDA codec
3745 * @num_configs: number of config enums
3746 * @models: array of model name strings
3747 * @tbl: configuration table, terminated by null entries
3748 *
3749 * Compares the modelname or PCI subsystem id of the current codec with the
3750 * given configuration table. If a matching entry is found, returns its
3751 * config value (supposed to be 0 or positive).
3752 *
3753 * If no entries are matching, the function returns a negative value.
3754 */
3755int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003756 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003757 const struct snd_pci_quirk *tbl)
3758{
3759 const struct snd_pci_quirk *q;
3760
3761 /* Search for codec ID */
3762 for (q = tbl; q->subvendor; q++) {
3763 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3764
3765 if (vendorid == codec->subsystem_id)
3766 break;
3767 }
3768
3769 if (!q->subvendor)
3770 return -1;
3771
3772 tbl = q;
3773
3774 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003775#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003776 char tmp[10];
3777 const char *model = NULL;
3778 if (models)
3779 model = models[tbl->value];
3780 if (!model) {
3781 sprintf(tmp, "#%d", tbl->value);
3782 model = tmp;
3783 }
3784 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3785 "for config %x:%x (%s)\n",
3786 model, tbl->subvendor, tbl->subdevice,
3787 (tbl->name ? tbl->name : "Unknown device"));
3788#endif
3789 return tbl->value;
3790 }
3791 return -1;
3792}
3793EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3794
3795/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 * snd_hda_add_new_ctls - create controls from the array
3797 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003798 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 *
3800 * This helper function creates and add new controls in the given array.
3801 * The array must be terminated with an empty entry as terminator.
3802 *
3803 * Returns 0 if successful, or a negative error code.
3804 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003805int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003807 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
3809 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003810 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003811 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003812 if (knew->iface == -1) /* skip this codec private value */
3813 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003814 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003815 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003816 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003817 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003818 if (addr > 0)
3819 kctl->id.device = addr;
3820 if (idx > 0)
3821 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003822 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003823 if (!err)
3824 break;
3825 /* try first with another device index corresponding to
3826 * the codec addr; if it still fails (or it's the
3827 * primary codec), then try another control index
3828 */
3829 if (!addr && codec->addr)
3830 addr = codec->addr;
3831 else if (!idx && !knew->index) {
3832 idx = find_empty_mixer_ctl_idx(codec,
3833 knew->name);
3834 if (idx <= 0)
3835 return err;
3836 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003837 return err;
3838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 }
3840 return 0;
3841}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003842EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Takashi Iwaicb53c622007-08-10 17:21:45 +02003844#ifdef CONFIG_SND_HDA_POWER_SAVE
3845static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3846 unsigned int power_state);
3847
3848static void hda_power_work(struct work_struct *work)
3849{
3850 struct hda_codec *codec =
3851 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003852 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003853
Maxim Levitsky2e492462007-09-03 15:26:57 +02003854 if (!codec->power_on || codec->power_count) {
3855 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003856 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003857 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003858
3859 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003860 if (bus->ops.pm_notify)
3861 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003862}
3863
3864static void hda_keep_power_on(struct hda_codec *codec)
3865{
3866 codec->power_count++;
3867 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003868 codec->power_jiffies = jiffies;
3869}
3870
Takashi Iwaid5191e52009-11-16 14:58:17 +01003871/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003872void snd_hda_update_power_acct(struct hda_codec *codec)
3873{
3874 unsigned long delta = jiffies - codec->power_jiffies;
3875 if (codec->power_on)
3876 codec->power_on_acct += delta;
3877 else
3878 codec->power_off_acct += delta;
3879 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003880}
3881
Takashi Iwaid5191e52009-11-16 14:58:17 +01003882/**
3883 * snd_hda_power_up - Power-up the codec
3884 * @codec: HD-audio codec
3885 *
3886 * Increment the power-up counter and power up the hardware really when
3887 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003888 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003889void snd_hda_power_up(struct hda_codec *codec)
3890{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003891 struct hda_bus *bus = codec->bus;
3892
Takashi Iwaicb53c622007-08-10 17:21:45 +02003893 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003894 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003895 return;
3896
Takashi Iwaia2f63092009-11-11 09:34:25 +01003897 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003898 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003899 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003900 if (bus->ops.pm_notify)
3901 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003902 hda_call_codec_resume(codec);
3903 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003904 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003905}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003906EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003907
3908#define power_save(codec) \
3909 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003910
Takashi Iwaid5191e52009-11-16 14:58:17 +01003911/**
3912 * snd_hda_power_down - Power-down the codec
3913 * @codec: HD-audio codec
3914 *
3915 * Decrement the power-up counter and schedules the power-off work if
3916 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003917 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003918void snd_hda_power_down(struct hda_codec *codec)
3919{
3920 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003921 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003922 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003923 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003924 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003925 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003926 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003927 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003928}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003929EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003930
Takashi Iwaid5191e52009-11-16 14:58:17 +01003931/**
3932 * snd_hda_check_amp_list_power - Check the amp list and update the power
3933 * @codec: HD-audio codec
3934 * @check: the object containing an AMP list and the status
3935 * @nid: NID to check / update
3936 *
3937 * Check whether the given NID is in the amp list. If it's in the list,
3938 * check the current AMP status, and update the the power-status according
3939 * to the mute status.
3940 *
3941 * This function is supposed to be set or called from the check_power_status
3942 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003943 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003944int snd_hda_check_amp_list_power(struct hda_codec *codec,
3945 struct hda_loopback_check *check,
3946 hda_nid_t nid)
3947{
3948 struct hda_amp_list *p;
3949 int ch, v;
3950
3951 if (!check->amplist)
3952 return 0;
3953 for (p = check->amplist; p->nid; p++) {
3954 if (p->nid == nid)
3955 break;
3956 }
3957 if (!p->nid)
3958 return 0; /* nothing changed */
3959
3960 for (p = check->amplist; p->nid; p++) {
3961 for (ch = 0; ch < 2; ch++) {
3962 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3963 p->idx);
3964 if (!(v & HDA_AMP_MUTE) && v > 0) {
3965 if (!check->power_on) {
3966 check->power_on = 1;
3967 snd_hda_power_up(codec);
3968 }
3969 return 1;
3970 }
3971 }
3972 }
3973 if (check->power_on) {
3974 check->power_on = 0;
3975 snd_hda_power_down(codec);
3976 }
3977 return 0;
3978}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003979EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003980#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003982/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003983 * Channel mode helper
3984 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003985
3986/**
3987 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3988 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003989int snd_hda_ch_mode_info(struct hda_codec *codec,
3990 struct snd_ctl_elem_info *uinfo,
3991 const struct hda_channel_mode *chmode,
3992 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003993{
3994 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3995 uinfo->count = 1;
3996 uinfo->value.enumerated.items = num_chmodes;
3997 if (uinfo->value.enumerated.item >= num_chmodes)
3998 uinfo->value.enumerated.item = num_chmodes - 1;
3999 sprintf(uinfo->value.enumerated.name, "%dch",
4000 chmode[uinfo->value.enumerated.item].channels);
4001 return 0;
4002}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004003EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004004
Takashi Iwaid5191e52009-11-16 14:58:17 +01004005/**
4006 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4007 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004008int snd_hda_ch_mode_get(struct hda_codec *codec,
4009 struct snd_ctl_elem_value *ucontrol,
4010 const struct hda_channel_mode *chmode,
4011 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004012 int max_channels)
4013{
4014 int i;
4015
4016 for (i = 0; i < num_chmodes; i++) {
4017 if (max_channels == chmode[i].channels) {
4018 ucontrol->value.enumerated.item[0] = i;
4019 break;
4020 }
4021 }
4022 return 0;
4023}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004024EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004025
Takashi Iwaid5191e52009-11-16 14:58:17 +01004026/**
4027 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4028 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004029int snd_hda_ch_mode_put(struct hda_codec *codec,
4030 struct snd_ctl_elem_value *ucontrol,
4031 const struct hda_channel_mode *chmode,
4032 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004033 int *max_channelsp)
4034{
4035 unsigned int mode;
4036
4037 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004038 if (mode >= num_chmodes)
4039 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004040 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004041 return 0;
4042 /* change the current channel setting */
4043 *max_channelsp = chmode[mode].channels;
4044 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004045 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004046 return 1;
4047}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004048EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050/*
4051 * input MUX helper
4052 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004053
4054/**
4055 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4056 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004057int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4058 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
4060 unsigned int index;
4061
4062 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4063 uinfo->count = 1;
4064 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004065 if (!imux->num_items)
4066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 index = uinfo->value.enumerated.item;
4068 if (index >= imux->num_items)
4069 index = imux->num_items - 1;
4070 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4071 return 0;
4072}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004073EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Takashi Iwaid5191e52009-11-16 14:58:17 +01004075/**
4076 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4077 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004078int snd_hda_input_mux_put(struct hda_codec *codec,
4079 const struct hda_input_mux *imux,
4080 struct snd_ctl_elem_value *ucontrol,
4081 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 unsigned int *cur_val)
4083{
4084 unsigned int idx;
4085
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004086 if (!imux->num_items)
4087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 idx = ucontrol->value.enumerated.item[0];
4089 if (idx >= imux->num_items)
4090 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004091 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004093 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4094 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 *cur_val = idx;
4096 return 1;
4097}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004098EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
4100
4101/*
4102 * Multi-channel / digital-out PCM helper functions
4103 */
4104
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004105/* setup SPDIF output stream */
4106static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4107 unsigned int stream_tag, unsigned int format)
4108{
4109 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02004110 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004111 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02004112 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4113 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004114 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004115 if (codec->slave_dig_outs) {
4116 hda_nid_t *d;
4117 for (d = codec->slave_dig_outs; *d; d++)
4118 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4119 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004120 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004121 /* turn on again (if needed) */
4122 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4123 set_dig_out_convert(codec, nid,
4124 codec->spdif_ctls & 0xff, -1);
4125}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004126
Takashi Iwai2f728532008-09-25 16:32:41 +02004127static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4128{
4129 snd_hda_codec_cleanup_stream(codec, nid);
4130 if (codec->slave_dig_outs) {
4131 hda_nid_t *d;
4132 for (d = codec->slave_dig_outs; *d; d++)
4133 snd_hda_codec_cleanup_stream(codec, *d);
4134 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004135}
4136
Takashi Iwaid5191e52009-11-16 14:58:17 +01004137/**
4138 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4139 * @bus: HD-audio bus
4140 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004141void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4142{
4143 struct hda_codec *codec;
4144
4145 if (!bus)
4146 return;
4147 list_for_each_entry(codec, &bus->codec_list, list) {
4148#ifdef CONFIG_SND_HDA_POWER_SAVE
4149 if (!codec->power_on)
4150 continue;
4151#endif
4152 if (codec->patch_ops.reboot_notify)
4153 codec->patch_ops.reboot_notify(codec);
4154 }
4155}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004156EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004157
Takashi Iwaid5191e52009-11-16 14:58:17 +01004158/**
4159 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004161int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4162 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
Ingo Molnar62932df2006-01-16 16:34:20 +01004164 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004165 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4166 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004167 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004169 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return 0;
4171}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004172EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Takashi Iwaid5191e52009-11-16 14:58:17 +01004174/**
4175 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4176 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004177int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4178 struct hda_multi_out *mout,
4179 unsigned int stream_tag,
4180 unsigned int format,
4181 struct snd_pcm_substream *substream)
4182{
4183 mutex_lock(&codec->spdif_mutex);
4184 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4185 mutex_unlock(&codec->spdif_mutex);
4186 return 0;
4187}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004188EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004189
Takashi Iwaid5191e52009-11-16 14:58:17 +01004190/**
4191 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4192 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004193int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4194 struct hda_multi_out *mout)
4195{
4196 mutex_lock(&codec->spdif_mutex);
4197 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4198 mutex_unlock(&codec->spdif_mutex);
4199 return 0;
4200}
4201EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4202
Takashi Iwaid5191e52009-11-16 14:58:17 +01004203/**
4204 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004206int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4207 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208{
Ingo Molnar62932df2006-01-16 16:34:20 +01004209 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004211 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 return 0;
4213}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004214EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Takashi Iwaid5191e52009-11-16 14:58:17 +01004216/**
4217 * snd_hda_multi_out_analog_open - open analog outputs
4218 *
4219 * Open analog outputs and set up the hw-constraints.
4220 * If the digital outputs can be opened as slave, open the digital
4221 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004223int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4224 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004225 struct snd_pcm_substream *substream,
4226 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
Takashi Iwai9a081602008-02-12 18:37:26 +01004228 struct snd_pcm_runtime *runtime = substream->runtime;
4229 runtime->hw.channels_max = mout->max_channels;
4230 if (mout->dig_out_nid) {
4231 if (!mout->analog_rates) {
4232 mout->analog_rates = hinfo->rates;
4233 mout->analog_formats = hinfo->formats;
4234 mout->analog_maxbps = hinfo->maxbps;
4235 } else {
4236 runtime->hw.rates = mout->analog_rates;
4237 runtime->hw.formats = mout->analog_formats;
4238 hinfo->maxbps = mout->analog_maxbps;
4239 }
4240 if (!mout->spdif_rates) {
4241 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4242 &mout->spdif_rates,
4243 &mout->spdif_formats,
4244 &mout->spdif_maxbps);
4245 }
4246 mutex_lock(&codec->spdif_mutex);
4247 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004248 if ((runtime->hw.rates & mout->spdif_rates) &&
4249 (runtime->hw.formats & mout->spdif_formats)) {
4250 runtime->hw.rates &= mout->spdif_rates;
4251 runtime->hw.formats &= mout->spdif_formats;
4252 if (mout->spdif_maxbps < hinfo->maxbps)
4253 hinfo->maxbps = mout->spdif_maxbps;
4254 } else {
4255 mout->share_spdif = 0;
4256 /* FIXME: need notify? */
4257 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004258 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004259 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4262 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4263}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004264EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Takashi Iwaid5191e52009-11-16 14:58:17 +01004266/**
4267 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4268 *
4269 * Set up the i/o for analog out.
4270 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004272int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4273 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 unsigned int stream_tag,
4275 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004276 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277{
4278 hda_nid_t *nids = mout->dac_nids;
4279 int chs = substream->runtime->channels;
4280 int i;
4281
Ingo Molnar62932df2006-01-16 16:34:20 +01004282 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004283 if (mout->dig_out_nid && mout->share_spdif &&
4284 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004286 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4287 format) &&
4288 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004290 setup_dig_out_stream(codec, mout->dig_out_nid,
4291 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 } else {
4293 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004294 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 }
4296 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004297 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
4299 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004300 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4301 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004302 if (!mout->no_share_stream &&
4303 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004305 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4306 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004307 /* extra outputs copied from front */
4308 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004309 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004310 snd_hda_codec_setup_stream(codec,
4311 mout->extra_out_nid[i],
4312 stream_tag, 0, format);
4313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 /* surrounds */
4315 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004316 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004317 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4318 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004319 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004320 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4321 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
4323 return 0;
4324}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004325EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Takashi Iwaid5191e52009-11-16 14:58:17 +01004327/**
4328 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004330int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4331 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
4333 hda_nid_t *nids = mout->dac_nids;
4334 int i;
4335
4336 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004337 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004339 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004340 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4341 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004342 snd_hda_codec_cleanup_stream(codec,
4343 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004344 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004346 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 mout->dig_out_used = 0;
4348 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004349 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return 0;
4351}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004352EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004354/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004355 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004356 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004357
Takashi Iwai12f288b2007-08-02 15:51:59 +02004358static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004359{
4360 for (; *list; list++)
4361 if (*list == nid)
4362 return 1;
4363 return 0;
4364}
4365
Steve Longerbeam81937d32007-05-08 15:33:03 +02004366
4367/*
4368 * Sort an associated group of pins according to their sequence numbers.
4369 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004370static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004371 int num_pins)
4372{
4373 int i, j;
4374 short seq;
4375 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004376
Steve Longerbeam81937d32007-05-08 15:33:03 +02004377 for (i = 0; i < num_pins; i++) {
4378 for (j = i + 1; j < num_pins; j++) {
4379 if (sequences[i] > sequences[j]) {
4380 seq = sequences[i];
4381 sequences[i] = sequences[j];
4382 sequences[j] = seq;
4383 nid = pins[i];
4384 pins[i] = pins[j];
4385 pins[j] = nid;
4386 }
4387 }
4388 }
4389}
4390
4391
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004392/* add the found input-pin to the cfg->inputs[] table */
4393static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4394 int type)
4395{
4396 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4397 cfg->inputs[cfg->num_inputs].pin = nid;
4398 cfg->inputs[cfg->num_inputs].type = type;
4399 cfg->num_inputs++;
4400 }
4401}
4402
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004403/* sort inputs in the order of AUTO_PIN_* type */
4404static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4405{
4406 int i, j;
4407
4408 for (i = 0; i < cfg->num_inputs; i++) {
4409 for (j = i + 1; j < cfg->num_inputs; j++) {
4410 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4411 struct auto_pin_cfg_item tmp;
4412 tmp = cfg->inputs[i];
4413 cfg->inputs[i] = cfg->inputs[j];
4414 cfg->inputs[j] = tmp;
4415 }
4416 }
4417 }
4418}
4419
Takashi Iwai82bc9552006-03-21 11:24:42 +01004420/*
4421 * Parse all pin widgets and store the useful pin nids to cfg
4422 *
4423 * The number of line-outs or any primary output is stored in line_outs,
4424 * and the corresponding output pins are assigned to line_out_pins[],
4425 * in the order of front, rear, CLFE, side, ...
4426 *
4427 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004428 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004429 * is detected, one of speaker of HP pins is assigned as the primary
4430 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4431 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004432 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004433 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004434 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4435 * respectively.
4436 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004437int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4438 struct auto_pin_cfg *cfg,
4439 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004440{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004441 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004442 short seq, assoc_line_out, assoc_speaker;
4443 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4444 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004445 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004446 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004447
4448 memset(cfg, 0, sizeof(*cfg));
4449
Steve Longerbeam81937d32007-05-08 15:33:03 +02004450 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4451 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004452 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004453 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004454
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004455 end_nid = codec->start_nid + codec->num_nodes;
4456 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004457 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004458 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004459 unsigned int def_conf;
4460 short assoc, loc;
4461
4462 /* read all default configuration for pin complex */
4463 if (wid_type != AC_WID_PIN)
4464 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004465 /* ignore the given nids (e.g. pc-beep returns error) */
4466 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4467 continue;
4468
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004469 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004470 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4471 continue;
4472 loc = get_defcfg_location(def_conf);
4473 switch (get_defcfg_device(def_conf)) {
4474 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004475 seq = get_defcfg_sequence(def_conf);
4476 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004477
4478 if (!(wid_caps & AC_WCAP_STEREO))
4479 if (!cfg->mono_out_pin)
4480 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004481 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004482 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004483 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004484 assoc_line_out = assoc;
4485 else if (assoc_line_out != assoc)
4486 continue;
4487 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4488 continue;
4489 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004490 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004491 cfg->line_outs++;
4492 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004493 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004494 seq = get_defcfg_sequence(def_conf);
4495 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004496 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004497 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004498 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004499 assoc_speaker = assoc;
4500 else if (assoc_speaker != assoc)
4501 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004502 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4503 continue;
4504 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004505 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004506 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004507 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004508 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004509 seq = get_defcfg_sequence(def_conf);
4510 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004511 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4512 continue;
4513 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004514 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004515 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004516 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004517 case AC_JACK_MIC_IN:
4518 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004519 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004520 case AC_JACK_LINE_IN:
4521 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004522 break;
4523 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004524 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004525 break;
4526 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004527 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004528 break;
4529 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004530 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004531 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4532 continue;
4533 cfg->dig_out_pins[cfg->dig_outs] = nid;
4534 cfg->dig_out_type[cfg->dig_outs] =
4535 (loc == AC_JACK_LOC_HDMI) ?
4536 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4537 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004538 break;
4539 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004540 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004541 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004542 if (loc == AC_JACK_LOC_HDMI)
4543 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4544 else
4545 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004546 break;
4547 }
4548 }
4549
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004550 /* FIX-UP:
4551 * If no line-out is defined but multiple HPs are found,
4552 * some of them might be the real line-outs.
4553 */
4554 if (!cfg->line_outs && cfg->hp_outs > 1) {
4555 int i = 0;
4556 while (i < cfg->hp_outs) {
4557 /* The real HPs should have the sequence 0x0f */
4558 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4559 i++;
4560 continue;
4561 }
4562 /* Move it to the line-out table */
4563 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4564 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4565 cfg->line_outs++;
4566 cfg->hp_outs--;
4567 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4568 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004569 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004570 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4571 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004572 memset(cfg->hp_pins + cfg->hp_outs, 0,
4573 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004574 if (!cfg->hp_outs)
4575 cfg->line_out_type = AUTO_PIN_HP_OUT;
4576
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004577 }
4578
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004579 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004580 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4581 cfg->line_outs);
4582 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4583 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004584 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4585 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004586
Steve Longerbeam81937d32007-05-08 15:33:03 +02004587 /*
4588 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4589 * as a primary output
4590 */
4591 if (!cfg->line_outs) {
4592 if (cfg->speaker_outs) {
4593 cfg->line_outs = cfg->speaker_outs;
4594 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4595 sizeof(cfg->speaker_pins));
4596 cfg->speaker_outs = 0;
4597 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4598 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4599 } else if (cfg->hp_outs) {
4600 cfg->line_outs = cfg->hp_outs;
4601 memcpy(cfg->line_out_pins, cfg->hp_pins,
4602 sizeof(cfg->hp_pins));
4603 cfg->hp_outs = 0;
4604 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4605 cfg->line_out_type = AUTO_PIN_HP_OUT;
4606 }
4607 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004608
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004609 /* Reorder the surround channels
4610 * ALSA sequence is front/surr/clfe/side
4611 * HDA sequence is:
4612 * 4-ch: front/surr => OK as it is
4613 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004614 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004615 */
4616 switch (cfg->line_outs) {
4617 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004618 case 4:
4619 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004620 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004621 cfg->line_out_pins[2] = nid;
4622 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004623 }
4624
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004625 sort_autocfg_input_pins(cfg);
4626
Takashi Iwai82bc9552006-03-21 11:24:42 +01004627 /*
4628 * debug prints of the parsed results
4629 */
4630 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4631 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4632 cfg->line_out_pins[2], cfg->line_out_pins[3],
4633 cfg->line_out_pins[4]);
4634 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4635 cfg->speaker_outs, cfg->speaker_pins[0],
4636 cfg->speaker_pins[1], cfg->speaker_pins[2],
4637 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004638 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4639 cfg->hp_outs, cfg->hp_pins[0],
4640 cfg->hp_pins[1], cfg->hp_pins[2],
4641 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004642 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004643 if (cfg->dig_outs)
4644 snd_printd(" dig-out=0x%x/0x%x\n",
4645 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004646 snd_printd(" inputs:");
4647 for (i = 0; i < cfg->num_inputs; i++) {
4648 snd_printdd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004649 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004650 cfg->inputs[i].pin);
4651 }
4652 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004653 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004654 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004655
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004656 return 0;
4657}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004658EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004659
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004660int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004661{
4662 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004663 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004664 if (conn == AC_JACK_PORT_NONE)
4665 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004666 /* Windows may claim the internal mic to be BOTH, too */
4667 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004668 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004669 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004670 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004671 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004672 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004673 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004674 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004675 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004676 return INPUT_PIN_ATTR_FRONT;
4677 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004678}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004679EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004680
Takashi Iwai990061c2010-09-09 22:08:44 +02004681/**
4682 * hda_get_input_pin_label - Give a label for the given input pin
4683 *
4684 * When check_location is true, the function checks the pin location
4685 * for mic and line-in pins, and set an appropriate prefix like "Front",
4686 * "Rear", "Internal".
4687 */
4688
Takashi Iwai10a20af2010-09-09 16:28:02 +02004689const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4690 int check_location)
4691{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004692 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004693 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004694 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4695 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004696 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004697
Takashi Iwai10a20af2010-09-09 16:28:02 +02004698 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004699
4700 switch (get_defcfg_device(def_conf)) {
4701 case AC_JACK_MIC_IN:
4702 if (!check_location)
4703 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004704 attr = snd_hda_get_input_pin_attr(def_conf);
4705 if (!attr)
4706 return "None";
4707 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004708 case AC_JACK_LINE_IN:
4709 if (!check_location)
4710 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004711 attr = snd_hda_get_input_pin_attr(def_conf);
4712 if (!attr)
4713 return "None";
4714 if (attr == INPUT_PIN_ATTR_DOCK)
4715 return "Dock Line";
4716 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004717 case AC_JACK_AUX:
4718 return "Aux";
4719 case AC_JACK_CD:
4720 return "CD";
4721 case AC_JACK_SPDIF_IN:
4722 return "SPDIF In";
4723 case AC_JACK_DIG_OTHER_IN:
4724 return "Digital In";
4725 default:
4726 return "Misc";
4727 }
4728}
4729EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4730
Takashi Iwaia1c98512010-09-09 21:36:27 +02004731/* Check whether the location prefix needs to be added to the label.
4732 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4733 * have to put "Front" prefix to each label. In such a case, returns false.
4734 */
4735static int check_mic_location_need(struct hda_codec *codec,
4736 const struct auto_pin_cfg *cfg,
4737 int input)
4738{
4739 unsigned int defc;
4740 int i, attr, attr2;
4741
4742 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004743 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004744 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004745 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004746 return 1;
4747
4748 attr = 0;
4749 for (i = 0; i < cfg->num_inputs; i++) {
4750 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004751 attr2 = snd_hda_get_input_pin_attr(defc);
4752 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004753 if (attr && attr != attr2)
4754 return 1; /* different locations found */
4755 attr = attr2;
4756 }
4757 }
4758 return 0;
4759}
4760
Takashi Iwai990061c2010-09-09 22:08:44 +02004761/**
4762 * hda_get_autocfg_input_label - Get a label for the given input
4763 *
4764 * Get a label for the given input pin defined by the autocfg item.
4765 * Unlike hda_get_input_pin_label(), this function checks all inputs
4766 * defined in autocfg and avoids the redundant mic/line prefix as much as
4767 * possible.
4768 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004769const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4770 const struct auto_pin_cfg *cfg,
4771 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004772{
4773 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004774 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004775
Takashi Iwai10a20af2010-09-09 16:28:02 +02004776 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4777 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4778 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004779 if (has_multiple_pins && type == AUTO_PIN_MIC)
4780 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004781 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4782 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004783}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004784EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4785
Takashi Iwai990061c2010-09-09 22:08:44 +02004786/**
4787 * snd_hda_add_imux_item - Add an item to input_mux
4788 *
4789 * When the same label is used already in the existing items, the number
4790 * suffix is appended to the label. This label index number is stored
4791 * to type_idx when non-NULL pointer is given.
4792 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004793int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4794 int index, int *type_idx)
4795{
4796 int i, label_idx = 0;
4797 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4798 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4799 return -EINVAL;
4800 }
4801 for (i = 0; i < imux->num_items; i++) {
4802 if (!strncmp(label, imux->items[i].label, strlen(label)))
4803 label_idx++;
4804 }
4805 if (type_idx)
4806 *type_idx = label_idx;
4807 if (label_idx > 0)
4808 snprintf(imux->items[imux->num_items].label,
4809 sizeof(imux->items[imux->num_items].label),
4810 "%s %d", label, label_idx);
4811 else
4812 strlcpy(imux->items[imux->num_items].label, label,
4813 sizeof(imux->items[imux->num_items].label));
4814 imux->items[imux->num_items].index = index;
4815 imux->num_items++;
4816 return 0;
4817}
4818EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004819
Takashi Iwai4a471b72005-12-07 13:56:29 +01004820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821#ifdef CONFIG_PM
4822/*
4823 * power management
4824 */
4825
4826/**
4827 * snd_hda_suspend - suspend the codecs
4828 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 *
4830 * Returns 0 if successful.
4831 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004832int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004834 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Takashi Iwai0ba21762007-04-16 11:29:14 +02004836 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004837#ifdef CONFIG_SND_HDA_POWER_SAVE
4838 if (!codec->power_on)
4839 continue;
4840#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004841 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 }
4843 return 0;
4844}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004845EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
4847/**
4848 * snd_hda_resume - resume the codecs
4849 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 *
4851 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004852 *
4853 * This fucntion is defined only when POWER_SAVE isn't set.
4854 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 */
4856int snd_hda_resume(struct hda_bus *bus)
4857{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004858 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Takashi Iwai0ba21762007-04-16 11:29:14 +02004860 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004861 if (snd_hda_codec_needs_resume(codec))
4862 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 return 0;
4865}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004866EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004867#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004868
4869/*
4870 * generic arrays
4871 */
4872
Takashi Iwaid5191e52009-11-16 14:58:17 +01004873/**
4874 * snd_array_new - get a new element from the given array
4875 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004876 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004877 * Get a new element from the given array. If it exceeds the
4878 * pre-allocated array size, re-allocate the array.
4879 *
4880 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004881 */
4882void *snd_array_new(struct snd_array *array)
4883{
4884 if (array->used >= array->alloced) {
4885 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004886 void *nlist;
4887 if (snd_BUG_ON(num >= 4096))
4888 return NULL;
4889 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004890 if (!nlist)
4891 return NULL;
4892 if (array->list) {
4893 memcpy(nlist, array->list,
4894 array->elem_size * array->alloced);
4895 kfree(array->list);
4896 }
4897 array->list = nlist;
4898 array->alloced = num;
4899 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004900 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004901}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004902EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004903
Takashi Iwaid5191e52009-11-16 14:58:17 +01004904/**
4905 * snd_array_free - free the given array elements
4906 * @array: the array object
4907 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004908void snd_array_free(struct snd_array *array)
4909{
4910 kfree(array->list);
4911 array->used = 0;
4912 array->alloced = 0;
4913 array->list = NULL;
4914}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004915EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004916
Takashi Iwaid5191e52009-11-16 14:58:17 +01004917/**
4918 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4919 * @pcm: PCM caps bits
4920 * @buf: the string buffer to write
4921 * @buflen: the max buffer length
4922 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004923 * used by hda_proc.c and hda_eld.c
4924 */
4925void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4926{
4927 static unsigned int rates[] = {
4928 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4929 96000, 176400, 192000, 384000
4930 };
4931 int i, j;
4932
4933 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4934 if (pcm & (1 << i))
4935 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4936
4937 buf[j] = '\0'; /* necessary when j == 0 */
4938}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004939EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004940
Takashi Iwaid5191e52009-11-16 14:58:17 +01004941/**
4942 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4943 * @pcm: PCM caps bits
4944 * @buf: the string buffer to write
4945 * @buflen: the max buffer length
4946 *
4947 * used by hda_proc.c and hda_eld.c
4948 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004949void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4950{
4951 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4952 int i, j;
4953
4954 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4955 if (pcm & (AC_SUPPCM_BITS_8 << i))
4956 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4957
4958 buf[j] = '\0'; /* necessary when j == 0 */
4959}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004960EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004961
4962MODULE_DESCRIPTION("HDA codec core");
4963MODULE_LICENSE("GPL");