blob: 72334b7f60e58d1157c992df153bd223d5cfcf4b [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;
1219 int i;
1220
Takashi Iwai0ba21762007-04-16 11:29:14 +02001221 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001222 return;
1223
Takashi Iwai0ba21762007-04-16 11:29:14 +02001224 snd_printdd("hda_codec_setup_stream: "
1225 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001227 p = get_hda_cvt_setup(codec, nid);
1228 if (!p)
1229 return;
1230 /* update the stream-id if changed */
1231 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1232 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1233 newval = (stream_tag << 4) | channel_id;
1234 if (oldval != newval)
1235 snd_hda_codec_write(codec, nid, 0,
1236 AC_VERB_SET_CHANNEL_STREAMID,
1237 newval);
1238 p->stream_tag = stream_tag;
1239 p->channel_id = channel_id;
1240 }
1241 /* update the format-id if changed */
1242 if (p->format_id != format) {
1243 oldval = snd_hda_codec_read(codec, nid, 0,
1244 AC_VERB_GET_STREAM_FORMAT, 0);
1245 if (oldval != format) {
1246 msleep(1);
1247 snd_hda_codec_write(codec, nid, 0,
1248 AC_VERB_SET_STREAM_FORMAT,
1249 format);
1250 }
1251 p->format_id = format;
1252 }
1253 p->active = 1;
1254 p->dirty = 0;
1255
1256 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001257 list_for_each_entry(c, &codec->bus->codec_list, list) {
1258 for (i = 0; i < c->cvt_setups.used; i++) {
1259 p = snd_array_elem(&c->cvt_setups, i);
1260 if (!p->active && p->stream_tag == stream_tag)
1261 p->dirty = 1;
1262 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001265EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Takashi Iwaif0cea792010-08-13 11:56:53 +02001267static void really_cleanup_stream(struct hda_codec *codec,
1268 struct hda_cvt_setup *q);
1269
Takashi Iwaid5191e52009-11-16 14:58:17 +01001270/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001271 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001272 * @codec: the CODEC to clean up
1273 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001274 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001275 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001276void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1277 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001278{
Takashi Iwaieb541332010-08-06 13:48:11 +02001279 struct hda_cvt_setup *p;
1280
Takashi Iwai888afa12008-03-18 09:57:50 +01001281 if (!nid)
1282 return;
1283
1284 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001285 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001286 if (p) {
1287 /* here we just clear the active flag when do_now isn't set;
1288 * actual clean-ups will be done later in
1289 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1290 */
1291 if (do_now)
1292 really_cleanup_stream(codec, p);
1293 else
1294 p->active = 0;
1295 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001296}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001297EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001298
Takashi Iwaieb541332010-08-06 13:48:11 +02001299static void really_cleanup_stream(struct hda_codec *codec,
1300 struct hda_cvt_setup *q)
1301{
1302 hda_nid_t nid = q->nid;
1303 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1304 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1305 memset(q, 0, sizeof(*q));
1306 q->nid = nid;
1307}
1308
1309/* clean up the all conflicting obsolete streams */
1310static void purify_inactive_streams(struct hda_codec *codec)
1311{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001312 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001313 int i;
1314
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001315 list_for_each_entry(c, &codec->bus->codec_list, list) {
1316 for (i = 0; i < c->cvt_setups.used; i++) {
1317 struct hda_cvt_setup *p;
1318 p = snd_array_elem(&c->cvt_setups, i);
1319 if (p->dirty)
1320 really_cleanup_stream(c, p);
1321 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001322 }
1323}
1324
1325/* clean up all streams; called from suspend */
1326static void hda_cleanup_all_streams(struct hda_codec *codec)
1327{
1328 int i;
1329
1330 for (i = 0; i < codec->cvt_setups.used; i++) {
1331 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1332 if (p->stream_tag)
1333 really_cleanup_stream(codec, p);
1334 }
1335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/*
1338 * amp access functions
1339 */
1340
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001341/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001342#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001343#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001344#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1345#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001347#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001350static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001351 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Takashi Iwai01751f52007-08-10 16:59:39 +02001353 memset(cache, 0, sizeof(*cache));
1354 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001355 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001356}
1357
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001358static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001359{
Takashi Iwai603c4012008-07-30 15:01:44 +02001360 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001364static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Takashi Iwai01751f52007-08-10 16:59:39 +02001366 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1367 u16 cur = cache->hash[idx];
1368 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001371 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (info->key == key)
1373 return info;
1374 cur = info->next;
1375 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001376 return NULL;
1377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001379/* query the hash. allocate an entry if not found. */
1380static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1381 u32 key)
1382{
1383 struct hda_cache_head *info = get_hash(cache, key);
1384 if (!info) {
1385 u16 idx, cur;
1386 /* add a new hash entry */
1387 info = snd_array_new(&cache->buf);
1388 if (!info)
1389 return NULL;
1390 cur = snd_array_index(&cache->buf, info);
1391 info->key = key;
1392 info->val = 0;
1393 idx = key % (u16)ARRAY_SIZE(cache->hash);
1394 info->next = cache->hash[idx];
1395 cache->hash[idx] = cur;
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return info;
1398}
1399
Takashi Iwai01751f52007-08-10 16:59:39 +02001400/* query and allocate an amp hash entry */
1401static inline struct hda_amp_info *
1402get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1403{
1404 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1405}
1406
Takashi Iwaid5191e52009-11-16 14:58:17 +01001407/**
1408 * query_amp_caps - query AMP capabilities
1409 * @codec: the HD-auio codec
1410 * @nid: the NID to query
1411 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1412 *
1413 * Query AMP capabilities for the given widget and direction.
1414 * Returns the obtained capability bits.
1415 *
1416 * When cap bits have been already read, this doesn't read again but
1417 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001419u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001421 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Takashi Iwai0ba21762007-04-16 11:29:14 +02001423 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1424 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001426 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001427 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001429 info->amp_caps = snd_hda_param_read(codec, nid,
1430 direction == HDA_OUTPUT ?
1431 AC_PAR_AMP_OUT_CAP :
1432 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001433 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001434 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 return info->amp_caps;
1437}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001438EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Takashi Iwaid5191e52009-11-16 14:58:17 +01001440/**
1441 * snd_hda_override_amp_caps - Override the AMP capabilities
1442 * @codec: the CODEC to clean up
1443 * @nid: the NID to clean up
1444 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1445 * @caps: the capability bits to set
1446 *
1447 * Override the cached AMP caps bits value by the given one.
1448 * This function is useful if the driver needs to adjust the AMP ranges,
1449 * e.g. limit to 0dB, etc.
1450 *
1451 * Returns zero if successful or a negative error code.
1452 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001453int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1454 unsigned int caps)
1455{
1456 struct hda_amp_info *info;
1457
1458 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1459 if (!info)
1460 return -EINVAL;
1461 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001462 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001463 return 0;
1464}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001465EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001466
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001467static unsigned int
1468query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1469 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001470{
1471 struct hda_amp_info *info;
1472
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001473 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001474 if (!info)
1475 return 0;
1476 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001477 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001478 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001479 }
1480 return info->amp_caps;
1481}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001482
1483static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1484{
1485 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1486}
1487
Takashi Iwaid5191e52009-11-16 14:58:17 +01001488/**
1489 * snd_hda_query_pin_caps - Query PIN capabilities
1490 * @codec: the HD-auio codec
1491 * @nid: the NID to query
1492 *
1493 * Query PIN capabilities for the given widget.
1494 * Returns the obtained capability bits.
1495 *
1496 * When cap bits have been already read, this doesn't read again but
1497 * returns the cached value.
1498 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001499u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1500{
1501 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1502 read_pin_cap);
1503}
Takashi Iwai1327a322009-03-23 13:07:47 +01001504EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1505
Wu Fengguang864f92b2009-11-18 12:38:02 +08001506/**
1507 * snd_hda_pin_sense - execute pin sense measurement
1508 * @codec: the CODEC to sense
1509 * @nid: the pin NID to sense
1510 *
1511 * Execute necessary pin sense measurement and return its Presence Detect,
1512 * Impedance, ELD Valid etc. status bits.
1513 */
1514u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1515{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001516 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001517
Takashi Iwai729d55b2009-12-25 22:49:01 +01001518 if (!codec->no_trigger_sense) {
1519 pincap = snd_hda_query_pin_caps(codec, nid);
1520 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001521 snd_hda_codec_read(codec, nid, 0,
1522 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001523 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001524 return snd_hda_codec_read(codec, nid, 0,
1525 AC_VERB_GET_PIN_SENSE, 0);
1526}
1527EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1528
1529/**
1530 * snd_hda_jack_detect - query pin Presence Detect status
1531 * @codec: the CODEC to sense
1532 * @nid: the pin NID to sense
1533 *
1534 * Query and return the pin's Presence Detect status.
1535 */
1536int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1537{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001538 u32 sense = snd_hda_pin_sense(codec, nid);
1539 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001540}
1541EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543/*
1544 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001545 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001547static unsigned int get_vol_mute(struct hda_codec *codec,
1548 struct hda_amp_info *info, hda_nid_t nid,
1549 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 u32 val, parm;
1552
Takashi Iwai01751f52007-08-10 16:59:39 +02001553 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001554 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1557 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1558 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001559 val = snd_hda_codec_read(codec, nid, 0,
1560 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001562 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001563 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001567 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001569static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001570 hda_nid_t nid, int ch, int direction, int index,
1571 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 u32 parm;
1574
1575 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1576 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1577 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1578 parm |= val;
1579 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001580 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Takashi Iwaid5191e52009-11-16 14:58:17 +01001583/**
1584 * snd_hda_codec_amp_read - Read AMP value
1585 * @codec: HD-audio codec
1586 * @nid: NID to read the AMP value
1587 * @ch: channel (left=0 or right=1)
1588 * @direction: #HDA_INPUT or #HDA_OUTPUT
1589 * @index: the index value (only for input direction)
1590 *
1591 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 */
Takashi Iwai834be882006-03-01 14:16:17 +01001593int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1594 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001596 struct hda_amp_info *info;
1597 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1598 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001600 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001602EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Takashi Iwaid5191e52009-11-16 14:58:17 +01001604/**
1605 * snd_hda_codec_amp_update - update the AMP value
1606 * @codec: HD-audio codec
1607 * @nid: NID to read the AMP value
1608 * @ch: channel (left=0 or right=1)
1609 * @direction: #HDA_INPUT or #HDA_OUTPUT
1610 * @idx: the index value (only for input direction)
1611 * @mask: bit mask to set
1612 * @val: the bits value to set
1613 *
1614 * Update the AMP value with a bit mask.
1615 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001616 */
Takashi Iwai834be882006-03-01 14:16:17 +01001617int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1618 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001620 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001621
Takashi Iwai0ba21762007-04-16 11:29:14 +02001622 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1623 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001625 if (snd_BUG_ON(mask & ~0xff))
1626 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001627 val &= mask;
1628 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001629 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001631 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return 1;
1633}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001634EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Takashi Iwaid5191e52009-11-16 14:58:17 +01001636/**
1637 * snd_hda_codec_amp_stereo - update the AMP stereo values
1638 * @codec: HD-audio codec
1639 * @nid: NID to read the AMP value
1640 * @direction: #HDA_INPUT or #HDA_OUTPUT
1641 * @idx: the index value (only for input direction)
1642 * @mask: bit mask to set
1643 * @val: the bits value to set
1644 *
1645 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1646 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001647 */
1648int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1649 int direction, int idx, int mask, int val)
1650{
1651 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001652
1653 if (snd_BUG_ON(mask & ~0xff))
1654 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001655 for (ch = 0; ch < 2; ch++)
1656 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1657 idx, mask, val);
1658 return ret;
1659}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001660EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001661
Takashi Iwaicb53c622007-08-10 17:21:45 +02001662#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001663/**
1664 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1665 * @codec: HD-audio codec
1666 *
1667 * Resume the all amp commands from the cache.
1668 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001669void snd_hda_codec_resume_amp(struct hda_codec *codec)
1670{
Takashi Iwai603c4012008-07-30 15:01:44 +02001671 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001672 int i;
1673
Takashi Iwai603c4012008-07-30 15:01:44 +02001674 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001675 u32 key = buffer->head.key;
1676 hda_nid_t nid;
1677 unsigned int idx, dir, ch;
1678 if (!key)
1679 continue;
1680 nid = key & 0xff;
1681 idx = (key >> 16) & 0xff;
1682 dir = (key >> 24) & 0xff;
1683 for (ch = 0; ch < 2; ch++) {
1684 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1685 continue;
1686 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1687 buffer->vol[ch]);
1688 }
1689 }
1690}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001691EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001692#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001694static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1695 unsigned int ofs)
1696{
1697 u32 caps = query_amp_caps(codec, nid, dir);
1698 /* get num steps */
1699 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1700 if (ofs < caps)
1701 caps -= ofs;
1702 return caps;
1703}
1704
Takashi Iwaid5191e52009-11-16 14:58:17 +01001705/**
1706 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1707 *
1708 * The control element is supposed to have the private_value field
1709 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1710 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001711int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1715 u16 nid = get_amp_nid(kcontrol);
1716 u8 chs = get_amp_channels(kcontrol);
1717 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001718 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001720 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1721 uinfo->count = chs == 3 ? 2 : 1;
1722 uinfo->value.integer.min = 0;
1723 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1724 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001725 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001726 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1727 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return -EINVAL;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return 0;
1731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001732EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001734
1735static inline unsigned int
1736read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1737 int ch, int dir, int idx, unsigned int ofs)
1738{
1739 unsigned int val;
1740 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1741 val &= HDA_AMP_VOLMASK;
1742 if (val >= ofs)
1743 val -= ofs;
1744 else
1745 val = 0;
1746 return val;
1747}
1748
1749static inline int
1750update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1751 int ch, int dir, int idx, unsigned int ofs,
1752 unsigned int val)
1753{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001754 unsigned int maxval;
1755
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001756 if (val > 0)
1757 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001758 /* ofs = 0: raw max value */
1759 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001760 if (val > maxval)
1761 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001762 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1763 HDA_AMP_VOLMASK, val);
1764}
1765
Takashi Iwaid5191e52009-11-16 14:58:17 +01001766/**
1767 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1768 *
1769 * The control element is supposed to have the private_value field
1770 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1771 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001772int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1773 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1776 hda_nid_t nid = get_amp_nid(kcontrol);
1777 int chs = get_amp_channels(kcontrol);
1778 int dir = get_amp_direction(kcontrol);
1779 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001780 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 long *valp = ucontrol->value.integer.value;
1782
1783 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001784 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001786 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return 0;
1788}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001789EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Takashi Iwaid5191e52009-11-16 14:58:17 +01001791/**
1792 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1793 *
1794 * The control element is supposed to have the private_value field
1795 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1796 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001797int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1798 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1801 hda_nid_t nid = get_amp_nid(kcontrol);
1802 int chs = get_amp_channels(kcontrol);
1803 int dir = get_amp_direction(kcontrol);
1804 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001805 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 long *valp = ucontrol->value.integer.value;
1807 int change = 0;
1808
Takashi Iwaicb53c622007-08-10 17:21:45 +02001809 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001810 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001811 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001812 valp++;
1813 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001814 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001815 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001816 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return change;
1818}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001819EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Takashi Iwaid5191e52009-11-16 14:58:17 +01001821/**
1822 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1823 *
1824 * The control element is supposed to have the private_value field
1825 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1826 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001827int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1828 unsigned int size, unsigned int __user *_tlv)
1829{
1830 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1831 hda_nid_t nid = get_amp_nid(kcontrol);
1832 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001833 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001834 u32 caps, val1, val2;
1835
1836 if (size < 4 * sizeof(unsigned int))
1837 return -ENOMEM;
1838 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001839 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1840 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001841 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001842 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001843 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001844 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1845 return -EFAULT;
1846 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1847 return -EFAULT;
1848 if (put_user(val1, _tlv + 2))
1849 return -EFAULT;
1850 if (put_user(val2, _tlv + 3))
1851 return -EFAULT;
1852 return 0;
1853}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001854EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001855
Takashi Iwaid5191e52009-11-16 14:58:17 +01001856/**
1857 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1858 * @codec: HD-audio codec
1859 * @nid: NID of a reference widget
1860 * @dir: #HDA_INPUT or #HDA_OUTPUT
1861 * @tlv: TLV data to be stored, at least 4 elements
1862 *
1863 * Set (static) TLV data for a virtual master volume using the AMP caps
1864 * obtained from the reference NID.
1865 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001866 */
1867void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1868 unsigned int *tlv)
1869{
1870 u32 caps;
1871 int nums, step;
1872
1873 caps = query_amp_caps(codec, nid, dir);
1874 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1875 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1876 step = (step + 1) * 25;
1877 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1878 tlv[1] = 2 * sizeof(unsigned int);
1879 tlv[2] = -nums * step;
1880 tlv[3] = step;
1881}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001882EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001883
1884/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001885static struct snd_kcontrol *
1886_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1887 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001888{
1889 struct snd_ctl_elem_id id;
1890 memset(&id, 0, sizeof(id));
1891 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001892 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001893 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1894 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001895 strcpy(id.name, name);
1896 return snd_ctl_find_id(codec->bus->card, &id);
1897}
1898
Takashi Iwaid5191e52009-11-16 14:58:17 +01001899/**
1900 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1901 * @codec: HD-audio codec
1902 * @name: ctl id name string
1903 *
1904 * Get the control element with the given id string and IFACE_MIXER.
1905 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001906struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1907 const char *name)
1908{
1909 return _snd_hda_find_mixer_ctl(codec, name, 0);
1910}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001911EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001912
Takashi Iwaid5191e52009-11-16 14:58:17 +01001913/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001914 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001915 * @codec: HD-audio codec
1916 * @nid: corresponding NID (optional)
1917 * @kctl: the control element to assign
1918 *
1919 * Add the given control element to an array inside the codec instance.
1920 * All control elements belonging to a codec are supposed to be added
1921 * by this function so that a proper clean-up works at the free or
1922 * reconfiguration time.
1923 *
1924 * If non-zero @nid is passed, the NID is assigned to the control element.
1925 * The assignment is shown in the codec proc file.
1926 *
1927 * snd_hda_ctl_add() checks the control subdev id field whether
1928 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001929 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1930 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001931 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001932int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1933 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001934{
1935 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001936 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001937 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001938
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001939 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001940 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001941 if (nid == 0)
1942 nid = get_amp_nid_(kctl->private_value);
1943 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001944 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1945 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001946 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001947 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001948 err = snd_ctl_add(codec->bus->card, kctl);
1949 if (err < 0)
1950 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001951 item = snd_array_new(&codec->mixers);
1952 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001953 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001954 item->kctl = kctl;
1955 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001956 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001957 return 0;
1958}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001959EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001960
Takashi Iwaid5191e52009-11-16 14:58:17 +01001961/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001962 * snd_hda_add_nid - Assign a NID to a control element
1963 * @codec: HD-audio codec
1964 * @nid: corresponding NID (optional)
1965 * @kctl: the control element to assign
1966 * @index: index to kctl
1967 *
1968 * Add the given control element to an array inside the codec instance.
1969 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1970 * NID:KCTL mapping - for example "Capture Source" selector.
1971 */
1972int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1973 unsigned int index, hda_nid_t nid)
1974{
1975 struct hda_nid_item *item;
1976
1977 if (nid > 0) {
1978 item = snd_array_new(&codec->nids);
1979 if (!item)
1980 return -ENOMEM;
1981 item->kctl = kctl;
1982 item->index = index;
1983 item->nid = nid;
1984 return 0;
1985 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01001986 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1987 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001988 return -EINVAL;
1989}
1990EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1991
1992/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001993 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1994 * @codec: HD-audio codec
1995 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001996void snd_hda_ctls_clear(struct hda_codec *codec)
1997{
1998 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001999 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002000 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002001 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002002 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002003 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002004}
2005
Takashi Iwaia65d6292009-02-23 16:57:04 +01002006/* pseudo device locking
2007 * toggle card->shutdown to allow/disallow the device access (as a hack)
2008 */
2009static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002010{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002011 spin_lock(&card->files_lock);
2012 if (card->shutdown) {
2013 spin_unlock(&card->files_lock);
2014 return -EINVAL;
2015 }
2016 card->shutdown = 1;
2017 spin_unlock(&card->files_lock);
2018 return 0;
2019}
2020
2021static void hda_unlock_devices(struct snd_card *card)
2022{
2023 spin_lock(&card->files_lock);
2024 card->shutdown = 0;
2025 spin_unlock(&card->files_lock);
2026}
2027
Takashi Iwaid5191e52009-11-16 14:58:17 +01002028/**
2029 * snd_hda_codec_reset - Clear all objects assigned to the codec
2030 * @codec: HD-audio codec
2031 *
2032 * This frees the all PCM and control elements assigned to the codec, and
2033 * clears the caches and restores the pin default configurations.
2034 *
2035 * When a device is being used, it returns -EBSY. If successfully freed,
2036 * returns zero.
2037 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002038int snd_hda_codec_reset(struct hda_codec *codec)
2039{
2040 struct snd_card *card = codec->bus->card;
2041 int i, pcm;
2042
2043 if (hda_lock_devices(card) < 0)
2044 return -EBUSY;
2045 /* check whether the codec isn't used by any mixer or PCM streams */
2046 if (!list_empty(&card->ctl_files)) {
2047 hda_unlock_devices(card);
2048 return -EBUSY;
2049 }
2050 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2051 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2052 if (!cpcm->pcm)
2053 continue;
2054 if (cpcm->pcm->streams[0].substream_opened ||
2055 cpcm->pcm->streams[1].substream_opened) {
2056 hda_unlock_devices(card);
2057 return -EBUSY;
2058 }
2059 }
2060
2061 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002062
2063#ifdef CONFIG_SND_HDA_POWER_SAVE
2064 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002065 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002066#endif
2067 snd_hda_ctls_clear(codec);
2068 /* relase PCMs */
2069 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002070 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002071 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002072 clear_bit(codec->pcm_info[i].device,
2073 codec->bus->pcm_dev_bits);
2074 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002075 }
2076 if (codec->patch_ops.free)
2077 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002078 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002079 codec->spec = NULL;
2080 free_hda_cache(&codec->amp_cache);
2081 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002082 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2083 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002084 /* free only driver_pins so that init_pins + user_pins are restored */
2085 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002086 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002087 codec->num_pcms = 0;
2088 codec->pcm_info = NULL;
2089 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002090 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2091 codec->slave_dig_outs = NULL;
2092 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002093 module_put(codec->owner);
2094 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002095
2096 /* allow device access again */
2097 hda_unlock_devices(card);
2098 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002099}
2100
Takashi Iwaid5191e52009-11-16 14:58:17 +01002101/**
2102 * snd_hda_add_vmaster - create a virtual master control and add slaves
2103 * @codec: HD-audio codec
2104 * @name: vmaster control name
2105 * @tlv: TLV data (optional)
2106 * @slaves: slave control names (optional)
2107 *
2108 * Create a virtual master control with the given name. The TLV data
2109 * must be either NULL or a valid data.
2110 *
2111 * @slaves is a NULL-terminated array of strings, each of which is a
2112 * slave control name. All controls with these names are assigned to
2113 * the new virtual master control.
2114 *
2115 * This function returns zero if successful or a negative error code.
2116 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002117int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2118 unsigned int *tlv, const char **slaves)
2119{
2120 struct snd_kcontrol *kctl;
2121 const char **s;
2122 int err;
2123
Takashi Iwai2f085542008-02-22 18:43:50 +01002124 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2125 ;
2126 if (!*s) {
2127 snd_printdd("No slave found for %s\n", name);
2128 return 0;
2129 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002130 kctl = snd_ctl_make_virtual_master(name, tlv);
2131 if (!kctl)
2132 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002133 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002134 if (err < 0)
2135 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002136
Takashi Iwai2134ea42008-01-10 16:53:55 +01002137 for (s = slaves; *s; s++) {
2138 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002139 int i = 0;
2140 for (;;) {
2141 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2142 if (!sctl) {
2143 if (!i)
2144 snd_printdd("Cannot find slave %s, "
2145 "skipped\n", *s);
2146 break;
2147 }
2148 err = snd_ctl_add_slave(kctl, sctl);
2149 if (err < 0)
2150 return err;
2151 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002152 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002153 }
2154 return 0;
2155}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002156EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002157
Takashi Iwaid5191e52009-11-16 14:58:17 +01002158/**
2159 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2160 *
2161 * The control element is supposed to have the private_value field
2162 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2163 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002164int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2165 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 int chs = get_amp_channels(kcontrol);
2168
2169 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2170 uinfo->count = chs == 3 ? 2 : 1;
2171 uinfo->value.integer.min = 0;
2172 uinfo->value.integer.max = 1;
2173 return 0;
2174}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002175EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Takashi Iwaid5191e52009-11-16 14:58:17 +01002177/**
2178 * snd_hda_mixer_amp_switch_get - Get 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_get(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2187 hda_nid_t nid = get_amp_nid(kcontrol);
2188 int chs = get_amp_channels(kcontrol);
2189 int dir = get_amp_direction(kcontrol);
2190 int idx = get_amp_index(kcontrol);
2191 long *valp = ucontrol->value.integer.value;
2192
2193 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002194 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002195 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002197 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002198 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 0;
2200}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002201EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Takashi Iwaid5191e52009-11-16 14:58:17 +01002203/**
2204 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2205 *
2206 * The control element is supposed to have the private_value field
2207 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2208 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002209int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2210 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2213 hda_nid_t nid = get_amp_nid(kcontrol);
2214 int chs = get_amp_channels(kcontrol);
2215 int dir = get_amp_direction(kcontrol);
2216 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 long *valp = ucontrol->value.integer.value;
2218 int change = 0;
2219
Takashi Iwaicb53c622007-08-10 17:21:45 +02002220 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002221 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002222 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002223 HDA_AMP_MUTE,
2224 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002225 valp++;
2226 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002227 if (chs & 2)
2228 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002229 HDA_AMP_MUTE,
2230 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002231#ifdef CONFIG_SND_HDA_POWER_SAVE
2232 if (codec->patch_ops.check_power_status)
2233 codec->patch_ops.check_power_status(codec, nid);
2234#endif
2235 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return change;
2237}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002238EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Takashi Iwai67d634c2009-11-16 15:35:59 +01002240#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002241/**
2242 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2243 *
2244 * This function calls snd_hda_enable_beep_device(), which behaves differently
2245 * depending on beep_mode option.
2246 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002247int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_value *ucontrol)
2249{
2250 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2251 long *valp = ucontrol->value.integer.value;
2252
2253 snd_hda_enable_beep_device(codec, *valp);
2254 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2255}
2256EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002257#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259/*
Takashi Iwai985be542005-11-02 18:26:49 +01002260 * bound volume controls
2261 *
2262 * bind multiple volumes (# indices, from 0)
2263 */
2264
2265#define AMP_VAL_IDX_SHIFT 19
2266#define AMP_VAL_IDX_MASK (0x0f<<19)
2267
Takashi Iwaid5191e52009-11-16 14:58:17 +01002268/**
2269 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2270 *
2271 * The control element is supposed to have the private_value field
2272 * set up via HDA_BIND_MUTE*() macros.
2273 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002274int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2275 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002276{
2277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2278 unsigned long pval;
2279 int err;
2280
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002281 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002282 pval = kcontrol->private_value;
2283 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2284 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2285 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002286 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002287 return err;
2288}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002289EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002290
Takashi Iwaid5191e52009-11-16 14:58:17 +01002291/**
2292 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2293 *
2294 * The control element is supposed to have the private_value field
2295 * set up via HDA_BIND_MUTE*() macros.
2296 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002297int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2298 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002299{
2300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2301 unsigned long pval;
2302 int i, indices, err = 0, change = 0;
2303
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002304 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002305 pval = kcontrol->private_value;
2306 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2307 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002308 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2309 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002310 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2311 if (err < 0)
2312 break;
2313 change |= err;
2314 }
2315 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002316 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002317 return err < 0 ? err : change;
2318}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002319EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002320
Takashi Iwaid5191e52009-11-16 14:58:17 +01002321/**
2322 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2323 *
2324 * The control element is supposed to have the private_value field
2325 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002326 */
2327int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2328 struct snd_ctl_elem_info *uinfo)
2329{
2330 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2331 struct hda_bind_ctls *c;
2332 int err;
2333
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002334 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002335 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002336 kcontrol->private_value = *c->values;
2337 err = c->ops->info(kcontrol, uinfo);
2338 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002339 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002340 return err;
2341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002342EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002343
Takashi Iwaid5191e52009-11-16 14:58:17 +01002344/**
2345 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2346 *
2347 * The control element is supposed to have the private_value field
2348 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2349 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002350int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2351 struct snd_ctl_elem_value *ucontrol)
2352{
2353 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2354 struct hda_bind_ctls *c;
2355 int err;
2356
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002357 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002358 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002359 kcontrol->private_value = *c->values;
2360 err = c->ops->get(kcontrol, ucontrol);
2361 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002362 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002363 return err;
2364}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002365EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002366
Takashi Iwaid5191e52009-11-16 14:58:17 +01002367/**
2368 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2369 *
2370 * The control element is supposed to have the private_value field
2371 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2372 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002373int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_value *ucontrol)
2375{
2376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2377 struct hda_bind_ctls *c;
2378 unsigned long *vals;
2379 int err = 0, change = 0;
2380
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002381 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002382 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002383 for (vals = c->values; *vals; vals++) {
2384 kcontrol->private_value = *vals;
2385 err = c->ops->put(kcontrol, ucontrol);
2386 if (err < 0)
2387 break;
2388 change |= err;
2389 }
2390 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002391 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002392 return err < 0 ? err : change;
2393}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002394EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002395
Takashi Iwaid5191e52009-11-16 14:58:17 +01002396/**
2397 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2398 *
2399 * The control element is supposed to have the private_value field
2400 * set up via HDA_BIND_VOL() macro.
2401 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002402int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2403 unsigned int size, unsigned int __user *tlv)
2404{
2405 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2406 struct hda_bind_ctls *c;
2407 int err;
2408
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002409 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002410 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002411 kcontrol->private_value = *c->values;
2412 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2413 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002414 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002415 return err;
2416}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002417EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002418
2419struct hda_ctl_ops snd_hda_bind_vol = {
2420 .info = snd_hda_mixer_amp_volume_info,
2421 .get = snd_hda_mixer_amp_volume_get,
2422 .put = snd_hda_mixer_amp_volume_put,
2423 .tlv = snd_hda_mixer_amp_tlv
2424};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002425EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002426
2427struct hda_ctl_ops snd_hda_bind_sw = {
2428 .info = snd_hda_mixer_amp_switch_info,
2429 .get = snd_hda_mixer_amp_switch_get,
2430 .put = snd_hda_mixer_amp_switch_put,
2431 .tlv = snd_hda_mixer_amp_tlv
2432};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002433EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002434
2435/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 * SPDIF out controls
2437 */
2438
Takashi Iwai0ba21762007-04-16 11:29:14 +02002439static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2440 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
2442 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2443 uinfo->count = 1;
2444 return 0;
2445}
2446
Takashi Iwai0ba21762007-04-16 11:29:14 +02002447static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
2450 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2451 IEC958_AES0_NONAUDIO |
2452 IEC958_AES0_CON_EMPHASIS_5015 |
2453 IEC958_AES0_CON_NOT_COPYRIGHT;
2454 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2455 IEC958_AES1_CON_ORIGINAL;
2456 return 0;
2457}
2458
Takashi Iwai0ba21762007-04-16 11:29:14 +02002459static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2460 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
2462 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2463 IEC958_AES0_NONAUDIO |
2464 IEC958_AES0_PRO_EMPHASIS_5015;
2465 return 0;
2466}
2467
Takashi Iwai0ba21762007-04-16 11:29:14 +02002468static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2472
2473 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2474 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2475 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2476 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2477
2478 return 0;
2479}
2480
2481/* convert from SPDIF status bits to HDA SPDIF bits
2482 * bit 0 (DigEn) is always set zero (to be filled later)
2483 */
2484static unsigned short convert_from_spdif_status(unsigned int sbits)
2485{
2486 unsigned short val = 0;
2487
2488 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002489 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002491 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002493 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2494 IEC958_AES0_PRO_EMPHASIS_5015)
2495 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002497 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2498 IEC958_AES0_CON_EMPHASIS_5015)
2499 val |= AC_DIG1_EMPHASIS;
2500 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2501 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002503 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2505 }
2506 return val;
2507}
2508
2509/* convert to SPDIF status bits from HDA SPDIF bits
2510 */
2511static unsigned int convert_to_spdif_status(unsigned short val)
2512{
2513 unsigned int sbits = 0;
2514
Takashi Iwai0ba21762007-04-16 11:29:14 +02002515 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002517 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 sbits |= IEC958_AES0_PROFESSIONAL;
2519 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002520 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2522 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002523 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002525 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002527 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2529 sbits |= val & (0x7f << 8);
2530 }
2531 return sbits;
2532}
2533
Takashi Iwai2f728532008-09-25 16:32:41 +02002534/* set digital convert verbs both for the given NID and its slaves */
2535static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2536 int verb, int val)
2537{
2538 hda_nid_t *d;
2539
Takashi Iwai9e976972008-11-25 08:17:20 +01002540 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002541 d = codec->slave_dig_outs;
2542 if (!d)
2543 return;
2544 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002545 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002546}
2547
2548static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2549 int dig1, int dig2)
2550{
2551 if (dig1 != -1)
2552 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2553 if (dig2 != -1)
2554 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2555}
2556
Takashi Iwai0ba21762007-04-16 11:29:14 +02002557static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2561 hda_nid_t nid = kcontrol->private_value;
2562 unsigned short val;
2563 int change;
2564
Ingo Molnar62932df2006-01-16 16:34:20 +01002565 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 codec->spdif_status = ucontrol->value.iec958.status[0] |
2567 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2568 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2569 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2570 val = convert_from_spdif_status(codec->spdif_status);
2571 val |= codec->spdif_ctls & 1;
2572 change = codec->spdif_ctls != val;
2573 codec->spdif_ctls = val;
2574
Takashi Iwai2f728532008-09-25 16:32:41 +02002575 if (change)
2576 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Ingo Molnar62932df2006-01-16 16:34:20 +01002578 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return change;
2580}
2581
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002582#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Takashi Iwai0ba21762007-04-16 11:29:14 +02002584static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2585 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
2587 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2588
Takashi Iwai0ba21762007-04-16 11:29:14 +02002589 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return 0;
2591}
2592
Takashi Iwai0ba21762007-04-16 11:29:14 +02002593static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 hda_nid_t nid = kcontrol->private_value;
2598 unsigned short val;
2599 int change;
2600
Ingo Molnar62932df2006-01-16 16:34:20 +01002601 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002602 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002604 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002606 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002608 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609 /* unmute amp switch (if any) */
2610 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002611 (val & AC_DIG1_ENABLE))
2612 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2613 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002615 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return change;
2617}
2618
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002619static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 {
2621 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2622 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002623 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 .info = snd_hda_spdif_mask_info,
2625 .get = snd_hda_spdif_cmask_get,
2626 },
2627 {
2628 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2629 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002630 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 .info = snd_hda_spdif_mask_info,
2632 .get = snd_hda_spdif_pmask_get,
2633 },
2634 {
2635 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002636 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 .info = snd_hda_spdif_mask_info,
2638 .get = snd_hda_spdif_default_get,
2639 .put = snd_hda_spdif_default_put,
2640 },
2641 {
2642 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002643 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 .info = snd_hda_spdif_out_switch_info,
2645 .get = snd_hda_spdif_out_switch_get,
2646 .put = snd_hda_spdif_out_switch_put,
2647 },
2648 { } /* end */
2649};
2650
Takashi Iwai09f99702008-02-04 12:31:13 +01002651#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653/**
2654 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2655 * @codec: the HDA codec
2656 * @nid: audio out widget NID
2657 *
2658 * Creates controls related with the SPDIF output.
2659 * Called from each patch supporting the SPDIF out.
2660 *
2661 * Returns 0 if successful, or a negative error code.
2662 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002663int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
2665 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002666 struct snd_kcontrol *kctl;
2667 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002668 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Takashi Iwai09f99702008-02-04 12:31:13 +01002670 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2671 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2672 idx))
2673 break;
2674 }
2675 if (idx >= SPDIF_MAX_IDX) {
2676 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2677 return -EBUSY;
2678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2680 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002681 if (!kctl)
2682 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002683 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002685 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002686 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return err;
2688 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002689 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002690 snd_hda_codec_read(codec, nid, 0,
2691 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2693 return 0;
2694}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002695EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002698 * SPDIF sharing with analog output
2699 */
2700static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2701 struct snd_ctl_elem_value *ucontrol)
2702{
2703 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2704 ucontrol->value.integer.value[0] = mout->share_spdif;
2705 return 0;
2706}
2707
2708static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2709 struct snd_ctl_elem_value *ucontrol)
2710{
2711 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2712 mout->share_spdif = !!ucontrol->value.integer.value[0];
2713 return 0;
2714}
2715
2716static struct snd_kcontrol_new spdif_share_sw = {
2717 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2718 .name = "IEC958 Default PCM Playback Switch",
2719 .info = snd_ctl_boolean_mono_info,
2720 .get = spdif_share_sw_get,
2721 .put = spdif_share_sw_put,
2722};
2723
Takashi Iwaid5191e52009-11-16 14:58:17 +01002724/**
2725 * snd_hda_create_spdif_share_sw - create Default PCM switch
2726 * @codec: the HDA codec
2727 * @mout: multi-out instance
2728 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002729int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2730 struct hda_multi_out *mout)
2731{
2732 if (!mout->dig_out_nid)
2733 return 0;
2734 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002735 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2736 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002737}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002738EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002739
2740/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 * SPDIF input
2742 */
2743
2744#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2745
Takashi Iwai0ba21762007-04-16 11:29:14 +02002746static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2747 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2750
2751 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2752 return 0;
2753}
2754
Takashi Iwai0ba21762007-04-16 11:29:14 +02002755static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2756 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
2758 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2759 hda_nid_t nid = kcontrol->private_value;
2760 unsigned int val = !!ucontrol->value.integer.value[0];
2761 int change;
2762
Ingo Molnar62932df2006-01-16 16:34:20 +01002763 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002765 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002767 snd_hda_codec_write_cache(codec, nid, 0,
2768 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002770 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return change;
2772}
2773
Takashi Iwai0ba21762007-04-16 11:29:14 +02002774static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2775 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
2777 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2778 hda_nid_t nid = kcontrol->private_value;
2779 unsigned short val;
2780 unsigned int sbits;
2781
Andrew Paprocki3982d172007-12-19 12:13:44 +01002782 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 sbits = convert_to_spdif_status(val);
2784 ucontrol->value.iec958.status[0] = sbits;
2785 ucontrol->value.iec958.status[1] = sbits >> 8;
2786 ucontrol->value.iec958.status[2] = sbits >> 16;
2787 ucontrol->value.iec958.status[3] = sbits >> 24;
2788 return 0;
2789}
2790
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002791static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 {
2793 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002794 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 .info = snd_hda_spdif_in_switch_info,
2796 .get = snd_hda_spdif_in_switch_get,
2797 .put = snd_hda_spdif_in_switch_put,
2798 },
2799 {
2800 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2801 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002802 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 .info = snd_hda_spdif_mask_info,
2804 .get = snd_hda_spdif_in_status_get,
2805 },
2806 { } /* end */
2807};
2808
2809/**
2810 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2811 * @codec: the HDA codec
2812 * @nid: audio in widget NID
2813 *
2814 * Creates controls related with the SPDIF input.
2815 * Called from each patch supporting the SPDIF in.
2816 *
2817 * Returns 0 if successful, or a negative error code.
2818 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002819int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002822 struct snd_kcontrol *kctl;
2823 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002824 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Takashi Iwai09f99702008-02-04 12:31:13 +01002826 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2827 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2828 idx))
2829 break;
2830 }
2831 if (idx >= SPDIF_MAX_IDX) {
2832 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2833 return -EBUSY;
2834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2836 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002837 if (!kctl)
2838 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002840 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002841 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return err;
2843 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002844 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002845 snd_hda_codec_read(codec, nid, 0,
2846 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002847 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return 0;
2849}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002850EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Takashi Iwaicb53c622007-08-10 17:21:45 +02002852#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002853/*
2854 * command cache
2855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002857/* build a 32bit cache key with the widget id and the command parameter */
2858#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2859#define get_cmd_cache_nid(key) ((key) & 0xff)
2860#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2861
2862/**
2863 * snd_hda_codec_write_cache - send a single command with caching
2864 * @codec: the HDA codec
2865 * @nid: NID to send the command
2866 * @direct: direct flag
2867 * @verb: the verb to send
2868 * @parm: the parameter for the verb
2869 *
2870 * Send a single command without waiting for response.
2871 *
2872 * Returns 0 if successful, or a negative error code.
2873 */
2874int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2875 int direct, unsigned int verb, unsigned int parm)
2876{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002877 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2878 struct hda_cache_head *c;
2879 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002880
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002881 if (err < 0)
2882 return err;
2883 /* parm may contain the verb stuff for get/set amp */
2884 verb = verb | (parm >> 8);
2885 parm &= 0xff;
2886 key = build_cmd_cache_key(nid, verb);
2887 mutex_lock(&codec->bus->cmd_mutex);
2888 c = get_alloc_hash(&codec->cmd_cache, key);
2889 if (c)
2890 c->val = parm;
2891 mutex_unlock(&codec->bus->cmd_mutex);
2892 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002893}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002894EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002895
Takashi Iwaid5191e52009-11-16 14:58:17 +01002896/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002897 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2898 * @codec: the HDA codec
2899 * @nid: NID to send the command
2900 * @direct: direct flag
2901 * @verb: the verb to send
2902 * @parm: the parameter for the verb
2903 *
2904 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2905 * command if the parameter is already identical with the cached value.
2906 * If not, it sends the command and refreshes the cache.
2907 *
2908 * Returns 0 if successful, or a negative error code.
2909 */
2910int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2911 int direct, unsigned int verb, unsigned int parm)
2912{
2913 struct hda_cache_head *c;
2914 u32 key;
2915
2916 /* parm may contain the verb stuff for get/set amp */
2917 verb = verb | (parm >> 8);
2918 parm &= 0xff;
2919 key = build_cmd_cache_key(nid, verb);
2920 mutex_lock(&codec->bus->cmd_mutex);
2921 c = get_hash(&codec->cmd_cache, key);
2922 if (c && c->val == parm) {
2923 mutex_unlock(&codec->bus->cmd_mutex);
2924 return 0;
2925 }
2926 mutex_unlock(&codec->bus->cmd_mutex);
2927 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2928}
2929EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2930
2931/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002932 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2933 * @codec: HD-audio codec
2934 *
2935 * Execute all verbs recorded in the command caches to resume.
2936 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002937void snd_hda_codec_resume_cache(struct hda_codec *codec)
2938{
Takashi Iwai603c4012008-07-30 15:01:44 +02002939 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002940 int i;
2941
Takashi Iwai603c4012008-07-30 15:01:44 +02002942 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002943 u32 key = buffer->key;
2944 if (!key)
2945 continue;
2946 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2947 get_cmd_cache_cmd(key), buffer->val);
2948 }
2949}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002950EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002951
2952/**
2953 * snd_hda_sequence_write_cache - sequence writes with caching
2954 * @codec: the HDA codec
2955 * @seq: VERB array to send
2956 *
2957 * Send the commands sequentially from the given array.
2958 * Thte commands are recorded on cache for power-save and resume.
2959 * The array must be terminated with NID=0.
2960 */
2961void snd_hda_sequence_write_cache(struct hda_codec *codec,
2962 const struct hda_verb *seq)
2963{
2964 for (; seq->nid; seq++)
2965 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2966 seq->param);
2967}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002968EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002969#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002970
Takashi Iwai54d17402005-11-21 16:33:22 +01002971/*
2972 * set power state of the codec
2973 */
2974static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2975 unsigned int power_state)
2976{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002977 hda_nid_t nid;
2978 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002979
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002980 /* this delay seems necessary to avoid click noise at power-down */
2981 if (power_state == AC_PWRST_D3)
2982 msleep(100);
2983 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002984 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002985 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002986 if (power_state == AC_PWRST_D0 &&
2987 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002988 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002989
Takashi Iwaicb53c622007-08-10 17:21:45 +02002990 nid = codec->start_nid;
2991 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002992 unsigned int wcaps = get_wcaps(codec, nid);
2993 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002994 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002995 if (power_state == AC_PWRST_D3 &&
2996 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002997 unsigned int pincap;
2998 /*
2999 * don't power down the widget if it controls
3000 * eapd and EAPD_BTLENABLE is set.
3001 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003002 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003003 if (pincap & AC_PINCAP_EAPD) {
3004 int eapd = snd_hda_codec_read(codec,
3005 nid, 0,
3006 AC_VERB_GET_EAPD_BTLENABLE, 0);
3007 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003008 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003009 continue;
3010 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003011 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003012 snd_hda_codec_write(codec, nid, 0,
3013 AC_VERB_SET_POWER_STATE,
3014 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003015 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003016 }
3017
Takashi Iwaicb53c622007-08-10 17:21:45 +02003018 if (power_state == AC_PWRST_D0) {
3019 unsigned long end_time;
3020 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003021 /* wait until the codec reachs to D0 */
3022 end_time = jiffies + msecs_to_jiffies(500);
3023 do {
3024 state = snd_hda_codec_read(codec, fg, 0,
3025 AC_VERB_GET_POWER_STATE, 0);
3026 if (state == power_state)
3027 break;
3028 msleep(1);
3029 } while (time_after_eq(end_time, jiffies));
3030 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003031}
3032
Takashi Iwai11aeff02008-07-30 15:01:46 +02003033#ifdef CONFIG_SND_HDA_HWDEP
3034/* execute additional init verbs */
3035static void hda_exec_init_verbs(struct hda_codec *codec)
3036{
3037 if (codec->init_verbs.list)
3038 snd_hda_sequence_write(codec, codec->init_verbs.list);
3039}
3040#else
3041static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3042#endif
3043
Takashi Iwaicb53c622007-08-10 17:21:45 +02003044#ifdef SND_HDA_NEEDS_RESUME
3045/*
3046 * call suspend and power-down; used both from PM and power-save
3047 */
3048static void hda_call_codec_suspend(struct hda_codec *codec)
3049{
3050 if (codec->patch_ops.suspend)
3051 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003052 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003053 hda_set_power_state(codec,
3054 codec->afg ? codec->afg : codec->mfg,
3055 AC_PWRST_D3);
3056#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003057 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003058 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003059 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003060 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003061 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003062#endif
3063}
3064
3065/*
3066 * kick up codec; used both from PM and power-save
3067 */
3068static void hda_call_codec_resume(struct hda_codec *codec)
3069{
3070 hda_set_power_state(codec,
3071 codec->afg ? codec->afg : codec->mfg,
3072 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003073 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003074 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003075 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003076 if (codec->patch_ops.resume)
3077 codec->patch_ops.resume(codec);
3078 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003079 if (codec->patch_ops.init)
3080 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003081 snd_hda_codec_resume_amp(codec);
3082 snd_hda_codec_resume_cache(codec);
3083 }
3084}
3085#endif /* SND_HDA_NEEDS_RESUME */
3086
Takashi Iwai54d17402005-11-21 16:33:22 +01003087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088/**
3089 * snd_hda_build_controls - build mixer controls
3090 * @bus: the BUS
3091 *
3092 * Creates mixer controls for each codec included in the bus.
3093 *
3094 * Returns 0 if successful, otherwise a negative error code.
3095 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003096int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003098 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Takashi Iwai0ba21762007-04-16 11:29:14 +02003100 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003101 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003102 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003103 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003104 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003105 err = snd_hda_codec_reset(codec);
3106 if (err < 0) {
3107 printk(KERN_ERR
3108 "hda_codec: cannot revert codec\n");
3109 return err;
3110 }
3111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003113 return 0;
3114}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003115EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003117int snd_hda_codec_build_controls(struct hda_codec *codec)
3118{
3119 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003120 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003121 /* continue to initialize... */
3122 if (codec->patch_ops.init)
3123 err = codec->patch_ops.init(codec);
3124 if (!err && codec->patch_ops.build_controls)
3125 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003126 if (err < 0)
3127 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return 0;
3129}
3130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131/*
3132 * stream formats
3133 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003134struct hda_rate_tbl {
3135 unsigned int hz;
3136 unsigned int alsa_bits;
3137 unsigned int hda_fmt;
3138};
3139
Takashi Iwai92f10b32010-08-03 14:21:00 +02003140/* rate = base * mult / div */
3141#define HDA_RATE(base, mult, div) \
3142 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3143 (((div) - 1) << AC_FMT_DIV_SHIFT))
3144
Takashi Iwaibefdf312005-08-22 13:57:55 +02003145static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003147
3148 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003149 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3150 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3151 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3152 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3153 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3154 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3155 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3156 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3157 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3158 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3159 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003160#define AC_PAR_PCM_RATE_BITS 11
3161 /* up to bits 10, 384kHZ isn't supported properly */
3162
3163 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003164 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003165
Takashi Iwaibefdf312005-08-22 13:57:55 +02003166 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167};
3168
3169/**
3170 * snd_hda_calc_stream_format - calculate format bitset
3171 * @rate: the sample rate
3172 * @channels: the number of channels
3173 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3174 * @maxbps: the max. bps
3175 *
3176 * Calculate the format bitset from the given rate, channels and th PCM format.
3177 *
3178 * Return zero if invalid.
3179 */
3180unsigned int snd_hda_calc_stream_format(unsigned int rate,
3181 unsigned int channels,
3182 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003183 unsigned int maxbps,
3184 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
3186 int i;
3187 unsigned int val = 0;
3188
Takashi Iwaibefdf312005-08-22 13:57:55 +02003189 for (i = 0; rate_bits[i].hz; i++)
3190 if (rate_bits[i].hz == rate) {
3191 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 break;
3193 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003194 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 snd_printdd("invalid rate %d\n", rate);
3196 return 0;
3197 }
3198
3199 if (channels == 0 || channels > 8) {
3200 snd_printdd("invalid channels %d\n", channels);
3201 return 0;
3202 }
3203 val |= channels - 1;
3204
3205 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003206 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003207 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003208 break;
3209 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003210 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 case 20:
3213 case 24:
3214 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003215 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003216 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003218 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003220 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 break;
3222 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003223 snd_printdd("invalid format width %d\n",
3224 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return 0;
3226 }
3227
Anssi Hannula32c168c2010-08-03 13:28:57 +03003228 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003229 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return val;
3232}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003233EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003235static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3236{
3237 unsigned int val = 0;
3238 if (nid != codec->afg &&
3239 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3240 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3241 if (!val || val == -1)
3242 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3243 if (!val || val == -1)
3244 return 0;
3245 return val;
3246}
3247
3248static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3249{
3250 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3251 get_pcm_param);
3252}
3253
3254static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3255{
3256 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3257 if (!streams || streams == -1)
3258 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3259 if (!streams || streams == -1)
3260 return 0;
3261 return streams;
3262}
3263
3264static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3265{
3266 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3267 get_stream_param);
3268}
3269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270/**
3271 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3272 * @codec: the HDA codec
3273 * @nid: NID to query
3274 * @ratesp: the pointer to store the detected rate bitflags
3275 * @formatsp: the pointer to store the detected formats
3276 * @bpsp: the pointer to store the detected format widths
3277 *
3278 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3279 * or @bsps argument is ignored.
3280 *
3281 * Returns 0 if successful, otherwise a negative error code.
3282 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003283static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3285{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003286 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003288 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003289 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
3291 if (ratesp) {
3292 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003293 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003295 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003297 if (rates == 0) {
3298 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3299 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3300 nid, val,
3301 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3302 return -EIO;
3303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 *ratesp = rates;
3305 }
3306
3307 if (formatsp || bpsp) {
3308 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003309 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003311 streams = query_stream_param(codec, nid);
3312 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 bps = 0;
3316 if (streams & AC_SUPFMT_PCM) {
3317 if (val & AC_SUPPCM_BITS_8) {
3318 formats |= SNDRV_PCM_FMTBIT_U8;
3319 bps = 8;
3320 }
3321 if (val & AC_SUPPCM_BITS_16) {
3322 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3323 bps = 16;
3324 }
3325 if (wcaps & AC_WCAP_DIGITAL) {
3326 if (val & AC_SUPPCM_BITS_32)
3327 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3328 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3329 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3330 if (val & AC_SUPPCM_BITS_24)
3331 bps = 24;
3332 else if (val & AC_SUPPCM_BITS_20)
3333 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003334 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3335 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3337 if (val & AC_SUPPCM_BITS_32)
3338 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 else if (val & AC_SUPPCM_BITS_24)
3340 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003341 else if (val & AC_SUPPCM_BITS_20)
3342 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
3344 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003345 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003347 if (!bps)
3348 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003349 }
3350 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003351 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 /* temporary hack: we have still no proper support
3353 * for the direct AC3 stream...
3354 */
3355 formats |= SNDRV_PCM_FMTBIT_U8;
3356 bps = 8;
3357 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003358 if (formats == 0) {
3359 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3360 "(nid=0x%x, val=0x%x, ovrd=%i, "
3361 "streams=0x%x)\n",
3362 nid, val,
3363 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3364 streams);
3365 return -EIO;
3366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (formatsp)
3368 *formatsp = formats;
3369 if (bpsp)
3370 *bpsp = bps;
3371 }
3372
3373 return 0;
3374}
3375
3376/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003377 * snd_hda_is_supported_format - Check the validity of the format
3378 * @codec: HD-audio codec
3379 * @nid: NID to check
3380 * @format: the HD-audio format value to check
3381 *
3382 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 *
3384 * Returns 1 if supported, 0 if not.
3385 */
3386int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3387 unsigned int format)
3388{
3389 int i;
3390 unsigned int val = 0, rate, stream;
3391
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003392 val = query_pcm_param(codec, nid);
3393 if (!val)
3394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
3396 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003397 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003398 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 if (val & (1 << i))
3400 break;
3401 return 0;
3402 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003403 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return 0;
3405
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003406 stream = query_stream_param(codec, nid);
3407 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return 0;
3409
3410 if (stream & AC_SUPFMT_PCM) {
3411 switch (format & 0xf0) {
3412 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003413 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return 0;
3415 break;
3416 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003417 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return 0;
3419 break;
3420 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003421 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return 0;
3423 break;
3424 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003425 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 return 0;
3427 break;
3428 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003429 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return 0;
3431 break;
3432 default:
3433 return 0;
3434 }
3435 } else {
3436 /* FIXME: check for float32 and AC3? */
3437 }
3438
3439 return 1;
3440}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003441EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
3443/*
3444 * PCM stuff
3445 */
3446static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3447 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003448 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
3450 return 0;
3451}
3452
3453static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3454 struct hda_codec *codec,
3455 unsigned int stream_tag,
3456 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003457 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
3459 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3460 return 0;
3461}
3462
3463static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3464 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003465 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
Takashi Iwai888afa12008-03-18 09:57:50 +01003467 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 return 0;
3469}
3470
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003471static int set_pcm_default_values(struct hda_codec *codec,
3472 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003474 int err;
3475
Takashi Iwai0ba21762007-04-16 11:29:14 +02003476 /* query support PCM information from the given NID */
3477 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003478 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003479 info->rates ? NULL : &info->rates,
3480 info->formats ? NULL : &info->formats,
3481 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003482 if (err < 0)
3483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
3485 if (info->ops.open == NULL)
3486 info->ops.open = hda_pcm_default_open_close;
3487 if (info->ops.close == NULL)
3488 info->ops.close = hda_pcm_default_open_close;
3489 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003490 if (snd_BUG_ON(!info->nid))
3491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 info->ops.prepare = hda_pcm_default_prepare;
3493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003495 if (snd_BUG_ON(!info->nid))
3496 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 info->ops.cleanup = hda_pcm_default_cleanup;
3498 }
3499 return 0;
3500}
3501
Takashi Iwaieb541332010-08-06 13:48:11 +02003502/*
3503 * codec prepare/cleanup entries
3504 */
3505int snd_hda_codec_prepare(struct hda_codec *codec,
3506 struct hda_pcm_stream *hinfo,
3507 unsigned int stream,
3508 unsigned int format,
3509 struct snd_pcm_substream *substream)
3510{
3511 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003512 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003513 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3514 if (ret >= 0)
3515 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003516 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003517 return ret;
3518}
3519EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3520
3521void snd_hda_codec_cleanup(struct hda_codec *codec,
3522 struct hda_pcm_stream *hinfo,
3523 struct snd_pcm_substream *substream)
3524{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003525 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003526 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003527 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003528}
3529EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3530
Takashi Iwaid5191e52009-11-16 14:58:17 +01003531/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003532const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3533 "Audio", "SPDIF", "HDMI", "Modem"
3534};
3535
Takashi Iwai176d5332008-07-30 15:01:44 +02003536/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003537 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003538 *
3539 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003540 */
3541static int get_empty_pcm_device(struct hda_bus *bus, int type)
3542{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003543 /* audio device indices; not linear to keep compatibility */
3544 static int audio_idx[HDA_PCM_NTYPES][5] = {
3545 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3546 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003547 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003548 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003549 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003550 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003551
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003552 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003553 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3554 return -EINVAL;
3555 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003556
3557 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3558 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3559 return audio_idx[type][i];
3560
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003561 snd_printk(KERN_WARNING "Too many %s devices\n",
3562 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003563 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003564}
3565
3566/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003567 * attach a new PCM stream
3568 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003569static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003570{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003571 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003572 struct hda_pcm_stream *info;
3573 int stream, err;
3574
Takashi Iwaib91f0802008-11-04 08:43:08 +01003575 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003576 return -EINVAL;
3577 for (stream = 0; stream < 2; stream++) {
3578 info = &pcm->stream[stream];
3579 if (info->substreams) {
3580 err = set_pcm_default_values(codec, info);
3581 if (err < 0)
3582 return err;
3583 }
3584 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003585 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003586}
3587
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003588/* assign all PCMs of the given codec */
3589int snd_hda_codec_build_pcms(struct hda_codec *codec)
3590{
3591 unsigned int pcm;
3592 int err;
3593
3594 if (!codec->num_pcms) {
3595 if (!codec->patch_ops.build_pcms)
3596 return 0;
3597 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003598 if (err < 0) {
3599 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003600 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003601 err = snd_hda_codec_reset(codec);
3602 if (err < 0) {
3603 printk(KERN_ERR
3604 "hda_codec: cannot revert codec\n");
3605 return err;
3606 }
3607 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003608 }
3609 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3610 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3611 int dev;
3612
3613 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003614 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003615
3616 if (!cpcm->pcm) {
3617 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3618 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003619 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003620 cpcm->device = dev;
3621 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003622 if (err < 0) {
3623 printk(KERN_ERR "hda_codec: cannot attach "
3624 "PCM stream %d for codec #%d\n",
3625 dev, codec->addr);
3626 continue; /* no fatal error */
3627 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003628 }
3629 }
3630 return 0;
3631}
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633/**
3634 * snd_hda_build_pcms - build PCM information
3635 * @bus: the BUS
3636 *
3637 * Create PCM information for each codec included in the bus.
3638 *
3639 * The build_pcms codec patch is requested to set up codec->num_pcms and
3640 * codec->pcm_info properly. The array is referred by the top-level driver
3641 * to create its PCM instances.
3642 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3643 * callback.
3644 *
3645 * At least, substreams, channels_min and channels_max must be filled for
3646 * each stream. substreams = 0 indicates that the stream doesn't exist.
3647 * When rates and/or formats are zero, the supported values are queried
3648 * from the given nid. The nid is used also by the default ops.prepare
3649 * and ops.cleanup callbacks.
3650 *
3651 * The driver needs to call ops.open in its open callback. Similarly,
3652 * ops.close is supposed to be called in the close callback.
3653 * ops.prepare should be called in the prepare or hw_params callback
3654 * with the proper parameters for set up.
3655 * ops.cleanup should be called in hw_free for clean up of streams.
3656 *
3657 * This function returns 0 if successfull, or a negative error code.
3658 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003659int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003661 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Takashi Iwai0ba21762007-04-16 11:29:14 +02003663 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003664 int err = snd_hda_codec_build_pcms(codec);
3665 if (err < 0)
3666 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 }
3668 return 0;
3669}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003670EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672/**
3673 * snd_hda_check_board_config - compare the current codec with the config table
3674 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003675 * @num_configs: number of config enums
3676 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 * @tbl: configuration table, terminated by null entries
3678 *
3679 * Compares the modelname or PCI subsystem id of the current codec with the
3680 * given configuration table. If a matching entry is found, returns its
3681 * config value (supposed to be 0 or positive).
3682 *
3683 * If no entries are matching, the function returns a negative value.
3684 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003685int snd_hda_check_board_config(struct hda_codec *codec,
3686 int num_configs, const char **models,
3687 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003689 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003690 int i;
3691 for (i = 0; i < num_configs; i++) {
3692 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003693 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003694 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3695 "selected\n", models[i]);
3696 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 }
3698 }
3699 }
3700
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003701 if (!codec->bus->pci || !tbl)
3702 return -1;
3703
3704 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3705 if (!tbl)
3706 return -1;
3707 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003708#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003709 char tmp[10];
3710 const char *model = NULL;
3711 if (models)
3712 model = models[tbl->value];
3713 if (!model) {
3714 sprintf(tmp, "#%d", tbl->value);
3715 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003717 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3718 "for config %x:%x (%s)\n",
3719 model, tbl->subvendor, tbl->subdevice,
3720 (tbl->name ? tbl->name : "Unknown device"));
3721#endif
3722 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 }
3724 return -1;
3725}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003726EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
3728/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003729 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003730 subsystem ID with the
3731 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003732
3733 This is important for Gateway notebooks with SB450 HDA Audio
3734 where the vendor ID of the PCI device is:
3735 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3736 and the vendor/subvendor are found only at the codec.
3737
3738 * @codec: the HDA codec
3739 * @num_configs: number of config enums
3740 * @models: array of model name strings
3741 * @tbl: configuration table, terminated by null entries
3742 *
3743 * Compares the modelname or PCI subsystem id of the current codec with the
3744 * given configuration table. If a matching entry is found, returns its
3745 * config value (supposed to be 0 or positive).
3746 *
3747 * If no entries are matching, the function returns a negative value.
3748 */
3749int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3750 int num_configs, const char **models,
3751 const struct snd_pci_quirk *tbl)
3752{
3753 const struct snd_pci_quirk *q;
3754
3755 /* Search for codec ID */
3756 for (q = tbl; q->subvendor; q++) {
3757 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3758
3759 if (vendorid == codec->subsystem_id)
3760 break;
3761 }
3762
3763 if (!q->subvendor)
3764 return -1;
3765
3766 tbl = q;
3767
3768 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003769#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003770 char tmp[10];
3771 const char *model = NULL;
3772 if (models)
3773 model = models[tbl->value];
3774 if (!model) {
3775 sprintf(tmp, "#%d", tbl->value);
3776 model = tmp;
3777 }
3778 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3779 "for config %x:%x (%s)\n",
3780 model, tbl->subvendor, tbl->subdevice,
3781 (tbl->name ? tbl->name : "Unknown device"));
3782#endif
3783 return tbl->value;
3784 }
3785 return -1;
3786}
3787EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3788
3789/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 * snd_hda_add_new_ctls - create controls from the array
3791 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003792 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 *
3794 * This helper function creates and add new controls in the given array.
3795 * The array must be terminated with an empty entry as terminator.
3796 *
3797 * Returns 0 if successful, or a negative error code.
3798 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003799int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003801 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
3803 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003804 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003805 if (knew->iface == -1) /* skip this codec private value */
3806 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003807 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003808 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003809 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003810 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003811 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003812 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003813 return err;
3814 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003815 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003816 return -ENOMEM;
3817 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003818 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003819 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003820 return err;
3821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
3823 return 0;
3824}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003825EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Takashi Iwaicb53c622007-08-10 17:21:45 +02003827#ifdef CONFIG_SND_HDA_POWER_SAVE
3828static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3829 unsigned int power_state);
3830
3831static void hda_power_work(struct work_struct *work)
3832{
3833 struct hda_codec *codec =
3834 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003835 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003836
Maxim Levitsky2e492462007-09-03 15:26:57 +02003837 if (!codec->power_on || codec->power_count) {
3838 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003839 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003840 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003841
3842 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003843 if (bus->ops.pm_notify)
3844 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003845}
3846
3847static void hda_keep_power_on(struct hda_codec *codec)
3848{
3849 codec->power_count++;
3850 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003851 codec->power_jiffies = jiffies;
3852}
3853
Takashi Iwaid5191e52009-11-16 14:58:17 +01003854/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003855void snd_hda_update_power_acct(struct hda_codec *codec)
3856{
3857 unsigned long delta = jiffies - codec->power_jiffies;
3858 if (codec->power_on)
3859 codec->power_on_acct += delta;
3860 else
3861 codec->power_off_acct += delta;
3862 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003863}
3864
Takashi Iwaid5191e52009-11-16 14:58:17 +01003865/**
3866 * snd_hda_power_up - Power-up the codec
3867 * @codec: HD-audio codec
3868 *
3869 * Increment the power-up counter and power up the hardware really when
3870 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003871 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003872void snd_hda_power_up(struct hda_codec *codec)
3873{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003874 struct hda_bus *bus = codec->bus;
3875
Takashi Iwaicb53c622007-08-10 17:21:45 +02003876 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003877 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003878 return;
3879
Takashi Iwaia2f63092009-11-11 09:34:25 +01003880 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003881 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003882 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003883 if (bus->ops.pm_notify)
3884 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003885 hda_call_codec_resume(codec);
3886 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003887 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003888}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003889EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003890
3891#define power_save(codec) \
3892 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003893
Takashi Iwaid5191e52009-11-16 14:58:17 +01003894/**
3895 * snd_hda_power_down - Power-down the codec
3896 * @codec: HD-audio codec
3897 *
3898 * Decrement the power-up counter and schedules the power-off work if
3899 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003900 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003901void snd_hda_power_down(struct hda_codec *codec)
3902{
3903 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003904 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003905 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003906 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003907 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003908 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003909 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003910 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003911}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003912EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003913
Takashi Iwaid5191e52009-11-16 14:58:17 +01003914/**
3915 * snd_hda_check_amp_list_power - Check the amp list and update the power
3916 * @codec: HD-audio codec
3917 * @check: the object containing an AMP list and the status
3918 * @nid: NID to check / update
3919 *
3920 * Check whether the given NID is in the amp list. If it's in the list,
3921 * check the current AMP status, and update the the power-status according
3922 * to the mute status.
3923 *
3924 * This function is supposed to be set or called from the check_power_status
3925 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003926 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003927int snd_hda_check_amp_list_power(struct hda_codec *codec,
3928 struct hda_loopback_check *check,
3929 hda_nid_t nid)
3930{
3931 struct hda_amp_list *p;
3932 int ch, v;
3933
3934 if (!check->amplist)
3935 return 0;
3936 for (p = check->amplist; p->nid; p++) {
3937 if (p->nid == nid)
3938 break;
3939 }
3940 if (!p->nid)
3941 return 0; /* nothing changed */
3942
3943 for (p = check->amplist; p->nid; p++) {
3944 for (ch = 0; ch < 2; ch++) {
3945 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3946 p->idx);
3947 if (!(v & HDA_AMP_MUTE) && v > 0) {
3948 if (!check->power_on) {
3949 check->power_on = 1;
3950 snd_hda_power_up(codec);
3951 }
3952 return 1;
3953 }
3954 }
3955 }
3956 if (check->power_on) {
3957 check->power_on = 0;
3958 snd_hda_power_down(codec);
3959 }
3960 return 0;
3961}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003962EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003965/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003966 * Channel mode helper
3967 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003968
3969/**
3970 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3971 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003972int snd_hda_ch_mode_info(struct hda_codec *codec,
3973 struct snd_ctl_elem_info *uinfo,
3974 const struct hda_channel_mode *chmode,
3975 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003976{
3977 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3978 uinfo->count = 1;
3979 uinfo->value.enumerated.items = num_chmodes;
3980 if (uinfo->value.enumerated.item >= num_chmodes)
3981 uinfo->value.enumerated.item = num_chmodes - 1;
3982 sprintf(uinfo->value.enumerated.name, "%dch",
3983 chmode[uinfo->value.enumerated.item].channels);
3984 return 0;
3985}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003986EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003987
Takashi Iwaid5191e52009-11-16 14:58:17 +01003988/**
3989 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3990 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003991int snd_hda_ch_mode_get(struct hda_codec *codec,
3992 struct snd_ctl_elem_value *ucontrol,
3993 const struct hda_channel_mode *chmode,
3994 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003995 int max_channels)
3996{
3997 int i;
3998
3999 for (i = 0; i < num_chmodes; i++) {
4000 if (max_channels == chmode[i].channels) {
4001 ucontrol->value.enumerated.item[0] = i;
4002 break;
4003 }
4004 }
4005 return 0;
4006}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004007EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004008
Takashi Iwaid5191e52009-11-16 14:58:17 +01004009/**
4010 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4011 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004012int snd_hda_ch_mode_put(struct hda_codec *codec,
4013 struct snd_ctl_elem_value *ucontrol,
4014 const struct hda_channel_mode *chmode,
4015 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004016 int *max_channelsp)
4017{
4018 unsigned int mode;
4019
4020 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004021 if (mode >= num_chmodes)
4022 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004023 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004024 return 0;
4025 /* change the current channel setting */
4026 *max_channelsp = chmode[mode].channels;
4027 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004028 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004029 return 1;
4030}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004031EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033/*
4034 * input MUX helper
4035 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004036
4037/**
4038 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4039 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004040int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4041 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042{
4043 unsigned int index;
4044
4045 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4046 uinfo->count = 1;
4047 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004048 if (!imux->num_items)
4049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 index = uinfo->value.enumerated.item;
4051 if (index >= imux->num_items)
4052 index = imux->num_items - 1;
4053 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4054 return 0;
4055}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004056EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Takashi Iwaid5191e52009-11-16 14:58:17 +01004058/**
4059 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4060 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004061int snd_hda_input_mux_put(struct hda_codec *codec,
4062 const struct hda_input_mux *imux,
4063 struct snd_ctl_elem_value *ucontrol,
4064 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 unsigned int *cur_val)
4066{
4067 unsigned int idx;
4068
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004069 if (!imux->num_items)
4070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 idx = ucontrol->value.enumerated.item[0];
4072 if (idx >= imux->num_items)
4073 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004074 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004076 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4077 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 *cur_val = idx;
4079 return 1;
4080}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004081EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
4083
4084/*
4085 * Multi-channel / digital-out PCM helper functions
4086 */
4087
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004088/* setup SPDIF output stream */
4089static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4090 unsigned int stream_tag, unsigned int format)
4091{
4092 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02004093 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004094 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02004095 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4096 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004097 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004098 if (codec->slave_dig_outs) {
4099 hda_nid_t *d;
4100 for (d = codec->slave_dig_outs; *d; d++)
4101 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4102 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004103 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004104 /* turn on again (if needed) */
4105 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4106 set_dig_out_convert(codec, nid,
4107 codec->spdif_ctls & 0xff, -1);
4108}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004109
Takashi Iwai2f728532008-09-25 16:32:41 +02004110static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4111{
4112 snd_hda_codec_cleanup_stream(codec, nid);
4113 if (codec->slave_dig_outs) {
4114 hda_nid_t *d;
4115 for (d = codec->slave_dig_outs; *d; d++)
4116 snd_hda_codec_cleanup_stream(codec, *d);
4117 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004118}
4119
Takashi Iwaid5191e52009-11-16 14:58:17 +01004120/**
4121 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4122 * @bus: HD-audio bus
4123 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004124void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4125{
4126 struct hda_codec *codec;
4127
4128 if (!bus)
4129 return;
4130 list_for_each_entry(codec, &bus->codec_list, list) {
4131#ifdef CONFIG_SND_HDA_POWER_SAVE
4132 if (!codec->power_on)
4133 continue;
4134#endif
4135 if (codec->patch_ops.reboot_notify)
4136 codec->patch_ops.reboot_notify(codec);
4137 }
4138}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004139EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004140
Takashi Iwaid5191e52009-11-16 14:58:17 +01004141/**
4142 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004144int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4145 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
Ingo Molnar62932df2006-01-16 16:34:20 +01004147 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004148 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4149 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004150 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004152 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 return 0;
4154}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004155EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Takashi Iwaid5191e52009-11-16 14:58:17 +01004157/**
4158 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4159 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004160int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4161 struct hda_multi_out *mout,
4162 unsigned int stream_tag,
4163 unsigned int format,
4164 struct snd_pcm_substream *substream)
4165{
4166 mutex_lock(&codec->spdif_mutex);
4167 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4168 mutex_unlock(&codec->spdif_mutex);
4169 return 0;
4170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004171EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004172
Takashi Iwaid5191e52009-11-16 14:58:17 +01004173/**
4174 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4175 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004176int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4177 struct hda_multi_out *mout)
4178{
4179 mutex_lock(&codec->spdif_mutex);
4180 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4181 mutex_unlock(&codec->spdif_mutex);
4182 return 0;
4183}
4184EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4185
Takashi Iwaid5191e52009-11-16 14:58:17 +01004186/**
4187 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004189int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4190 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191{
Ingo Molnar62932df2006-01-16 16:34:20 +01004192 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004194 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return 0;
4196}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004197EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Takashi Iwaid5191e52009-11-16 14:58:17 +01004199/**
4200 * snd_hda_multi_out_analog_open - open analog outputs
4201 *
4202 * Open analog outputs and set up the hw-constraints.
4203 * If the digital outputs can be opened as slave, open the digital
4204 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004206int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4207 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004208 struct snd_pcm_substream *substream,
4209 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
Takashi Iwai9a081602008-02-12 18:37:26 +01004211 struct snd_pcm_runtime *runtime = substream->runtime;
4212 runtime->hw.channels_max = mout->max_channels;
4213 if (mout->dig_out_nid) {
4214 if (!mout->analog_rates) {
4215 mout->analog_rates = hinfo->rates;
4216 mout->analog_formats = hinfo->formats;
4217 mout->analog_maxbps = hinfo->maxbps;
4218 } else {
4219 runtime->hw.rates = mout->analog_rates;
4220 runtime->hw.formats = mout->analog_formats;
4221 hinfo->maxbps = mout->analog_maxbps;
4222 }
4223 if (!mout->spdif_rates) {
4224 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4225 &mout->spdif_rates,
4226 &mout->spdif_formats,
4227 &mout->spdif_maxbps);
4228 }
4229 mutex_lock(&codec->spdif_mutex);
4230 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004231 if ((runtime->hw.rates & mout->spdif_rates) &&
4232 (runtime->hw.formats & mout->spdif_formats)) {
4233 runtime->hw.rates &= mout->spdif_rates;
4234 runtime->hw.formats &= mout->spdif_formats;
4235 if (mout->spdif_maxbps < hinfo->maxbps)
4236 hinfo->maxbps = mout->spdif_maxbps;
4237 } else {
4238 mout->share_spdif = 0;
4239 /* FIXME: need notify? */
4240 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004241 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004242 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4245 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4246}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004247EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Takashi Iwaid5191e52009-11-16 14:58:17 +01004249/**
4250 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4251 *
4252 * Set up the i/o for analog out.
4253 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004255int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4256 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 unsigned int stream_tag,
4258 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004259 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
4261 hda_nid_t *nids = mout->dac_nids;
4262 int chs = substream->runtime->channels;
4263 int i;
4264
Ingo Molnar62932df2006-01-16 16:34:20 +01004265 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004266 if (mout->dig_out_nid && mout->share_spdif &&
4267 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004269 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4270 format) &&
4271 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004273 setup_dig_out_stream(codec, mout->dig_out_nid,
4274 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 } else {
4276 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004277 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 }
4279 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004280 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004283 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4284 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004285 if (!mout->no_share_stream &&
4286 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004288 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4289 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004290 /* extra outputs copied from front */
4291 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004292 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004293 snd_hda_codec_setup_stream(codec,
4294 mout->extra_out_nid[i],
4295 stream_tag, 0, format);
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 /* surrounds */
4298 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004299 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004300 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4301 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004302 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004303 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4304 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 }
4306 return 0;
4307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004308EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Takashi Iwaid5191e52009-11-16 14:58:17 +01004310/**
4311 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004313int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4314 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
4316 hda_nid_t *nids = mout->dac_nids;
4317 int i;
4318
4319 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004320 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004322 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004323 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4324 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004325 snd_hda_codec_cleanup_stream(codec,
4326 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004327 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004329 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 mout->dig_out_used = 0;
4331 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004332 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return 0;
4334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004335EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004337/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004338 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004339 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004340
Takashi Iwai12f288b2007-08-02 15:51:59 +02004341static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004342{
4343 for (; *list; list++)
4344 if (*list == nid)
4345 return 1;
4346 return 0;
4347}
4348
Steve Longerbeam81937d32007-05-08 15:33:03 +02004349
4350/*
4351 * Sort an associated group of pins according to their sequence numbers.
4352 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004353static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004354 int num_pins)
4355{
4356 int i, j;
4357 short seq;
4358 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004359
Steve Longerbeam81937d32007-05-08 15:33:03 +02004360 for (i = 0; i < num_pins; i++) {
4361 for (j = i + 1; j < num_pins; j++) {
4362 if (sequences[i] > sequences[j]) {
4363 seq = sequences[i];
4364 sequences[i] = sequences[j];
4365 sequences[j] = seq;
4366 nid = pins[i];
4367 pins[i] = pins[j];
4368 pins[j] = nid;
4369 }
4370 }
4371 }
4372}
4373
4374
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004375/* add the found input-pin to the cfg->inputs[] table */
4376static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4377 int type)
4378{
4379 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4380 cfg->inputs[cfg->num_inputs].pin = nid;
4381 cfg->inputs[cfg->num_inputs].type = type;
4382 cfg->num_inputs++;
4383 }
4384}
4385
Takashi Iwai82bc9552006-03-21 11:24:42 +01004386/*
4387 * Parse all pin widgets and store the useful pin nids to cfg
4388 *
4389 * The number of line-outs or any primary output is stored in line_outs,
4390 * and the corresponding output pins are assigned to line_out_pins[],
4391 * in the order of front, rear, CLFE, side, ...
4392 *
4393 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004394 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004395 * is detected, one of speaker of HP pins is assigned as the primary
4396 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4397 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004398 *
Takashi Iwai82bc9552006-03-21 11:24:42 +01004399 * The analog input pins are assigned to input_pins array.
4400 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4401 * respectively.
4402 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004403int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4404 struct auto_pin_cfg *cfg,
4405 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004406{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004407 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004408 short seq, assoc_line_out, assoc_speaker;
4409 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4410 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004411 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004412 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004413
4414 memset(cfg, 0, sizeof(*cfg));
4415
Steve Longerbeam81937d32007-05-08 15:33:03 +02004416 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4417 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004418 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004419 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004420
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004421 end_nid = codec->start_nid + codec->num_nodes;
4422 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004423 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004424 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004425 unsigned int def_conf;
4426 short assoc, loc;
4427
4428 /* read all default configuration for pin complex */
4429 if (wid_type != AC_WID_PIN)
4430 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004431 /* ignore the given nids (e.g. pc-beep returns error) */
4432 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4433 continue;
4434
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004435 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004436 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4437 continue;
4438 loc = get_defcfg_location(def_conf);
4439 switch (get_defcfg_device(def_conf)) {
4440 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004441 seq = get_defcfg_sequence(def_conf);
4442 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004443
4444 if (!(wid_caps & AC_WCAP_STEREO))
4445 if (!cfg->mono_out_pin)
4446 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004447 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004448 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004449 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004450 assoc_line_out = assoc;
4451 else if (assoc_line_out != assoc)
4452 continue;
4453 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4454 continue;
4455 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004456 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004457 cfg->line_outs++;
4458 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004459 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004460 seq = get_defcfg_sequence(def_conf);
4461 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004462 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004463 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004464 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004465 assoc_speaker = assoc;
4466 else if (assoc_speaker != assoc)
4467 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004468 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4469 continue;
4470 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004471 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004472 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004473 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004474 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004475 seq = get_defcfg_sequence(def_conf);
4476 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004477 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4478 continue;
4479 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004480 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004481 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004482 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004483 case AC_JACK_MIC_IN: {
4484 int preferred, alt;
Kailang Yang6ff86a32010-03-19 11:14:36 +01004485 if (loc == AC_JACK_LOC_FRONT ||
4486 (loc & 0x30) == AC_JACK_LOC_INTERNAL) {
Takashi Iwai314634b2006-09-21 11:56:18 +02004487 preferred = AUTO_PIN_FRONT_MIC;
4488 alt = AUTO_PIN_MIC;
4489 } else {
4490 preferred = AUTO_PIN_MIC;
4491 alt = AUTO_PIN_FRONT_MIC;
4492 }
4493 if (!cfg->input_pins[preferred])
4494 cfg->input_pins[preferred] = nid;
4495 else if (!cfg->input_pins[alt])
4496 cfg->input_pins[alt] = nid;
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004497 add_auto_cfg_input_pin(cfg, nid, preferred);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004498 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004499 }
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004500 case AC_JACK_LINE_IN: {
4501 int type;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004502 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004503 type = AUTO_PIN_FRONT_LINE;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004504 else
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004505 type = AUTO_PIN_LINE;
4506 cfg->input_pins[type] = nid;
4507 add_auto_cfg_input_pin(cfg, nid, type);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004508 break;
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004509 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004510 case AC_JACK_CD:
4511 cfg->input_pins[AUTO_PIN_CD] = nid;
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004512 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004513 break;
4514 case AC_JACK_AUX:
4515 cfg->input_pins[AUTO_PIN_AUX] = nid;
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004516 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004517 break;
4518 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004519 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004520 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4521 continue;
4522 cfg->dig_out_pins[cfg->dig_outs] = nid;
4523 cfg->dig_out_type[cfg->dig_outs] =
4524 (loc == AC_JACK_LOC_HDMI) ?
4525 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4526 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004527 break;
4528 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004529 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004530 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004531 if (loc == AC_JACK_LOC_HDMI)
4532 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4533 else
4534 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004535 break;
4536 }
4537 }
4538
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004539 /* FIX-UP:
4540 * If no line-out is defined but multiple HPs are found,
4541 * some of them might be the real line-outs.
4542 */
4543 if (!cfg->line_outs && cfg->hp_outs > 1) {
4544 int i = 0;
4545 while (i < cfg->hp_outs) {
4546 /* The real HPs should have the sequence 0x0f */
4547 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4548 i++;
4549 continue;
4550 }
4551 /* Move it to the line-out table */
4552 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4553 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4554 cfg->line_outs++;
4555 cfg->hp_outs--;
4556 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4557 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4558 memmove(sequences_hp + i - 1, sequences_hp + i,
4559 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4560 }
4561 }
4562
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004563 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004564 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4565 cfg->line_outs);
4566 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4567 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004568 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4569 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004570
Takashi Iwaif889fa92007-10-31 15:49:32 +01004571 /* if we have only one mic, make it AUTO_PIN_MIC */
4572 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4573 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4574 cfg->input_pins[AUTO_PIN_MIC] =
4575 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4576 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4577 }
4578 /* ditto for line-in */
4579 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4580 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4581 cfg->input_pins[AUTO_PIN_LINE] =
4582 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4583 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4584 }
4585
Steve Longerbeam81937d32007-05-08 15:33:03 +02004586 /*
4587 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4588 * as a primary output
4589 */
4590 if (!cfg->line_outs) {
4591 if (cfg->speaker_outs) {
4592 cfg->line_outs = cfg->speaker_outs;
4593 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4594 sizeof(cfg->speaker_pins));
4595 cfg->speaker_outs = 0;
4596 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4597 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4598 } else if (cfg->hp_outs) {
4599 cfg->line_outs = cfg->hp_outs;
4600 memcpy(cfg->line_out_pins, cfg->hp_pins,
4601 sizeof(cfg->hp_pins));
4602 cfg->hp_outs = 0;
4603 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4604 cfg->line_out_type = AUTO_PIN_HP_OUT;
4605 }
4606 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004607
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004608 /* Reorder the surround channels
4609 * ALSA sequence is front/surr/clfe/side
4610 * HDA sequence is:
4611 * 4-ch: front/surr => OK as it is
4612 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004613 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004614 */
4615 switch (cfg->line_outs) {
4616 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004617 case 4:
4618 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004619 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004620 cfg->line_out_pins[2] = nid;
4621 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004622 }
4623
Takashi Iwai82bc9552006-03-21 11:24:42 +01004624 /*
4625 * debug prints of the parsed results
4626 */
4627 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4628 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4629 cfg->line_out_pins[2], cfg->line_out_pins[3],
4630 cfg->line_out_pins[4]);
4631 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4632 cfg->speaker_outs, cfg->speaker_pins[0],
4633 cfg->speaker_pins[1], cfg->speaker_pins[2],
4634 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004635 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4636 cfg->hp_outs, cfg->hp_pins[0],
4637 cfg->hp_pins[1], cfg->hp_pins[2],
4638 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004639 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004640 if (cfg->dig_outs)
4641 snd_printd(" dig-out=0x%x/0x%x\n",
4642 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004643 snd_printd(" inputs:");
4644 for (i = 0; i < cfg->num_inputs; i++) {
4645 snd_printdd(" %s=0x%x",
4646 auto_pin_cfg_labels[cfg->inputs[i].type],
4647 cfg->inputs[i].pin);
4648 }
4649 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004650 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004651 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004652
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004653 return 0;
4654}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004655EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004656
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004657/* labels for input pins - for obsoleted config stuff */
Takashi Iwai4a471b72005-12-07 13:56:29 +01004658const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4659 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4660};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004661EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01004662
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004663static const char *input_labels[AUTO_PIN_LAST][4] = {
4664 { "Mic", "Mic 2", "Mic 3", "Mic 4" },
4665 { "Front Mic", "Front Mic 2", "Front Mic 3", "Front Mic 4" },
4666 { "Line", "Line 2", "Line 3", "Line 4" },
4667 { "Front Line", "Front Line 2", "Front Line 3", "Front Line 4" },
4668 { "CD", "CD 2", "CD 3", "CD 4" },
4669 { "Aux", "Aux 2", "Aux 3", "Aux 4" },
4670};
4671
4672const char *snd_hda_get_input_pin_label(const struct auto_pin_cfg *cfg,
4673 int input)
4674{
4675 int type = cfg->inputs[input].type;
4676 int idx;
4677
4678 for (idx = 0; idx < 3 && --input >= 0; idx++) {
4679 if (type != cfg->inputs[input].type)
4680 break;
4681 }
4682 return input_labels[type][idx];
4683}
4684EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_label);
4685
Takashi Iwai4a471b72005-12-07 13:56:29 +01004686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687#ifdef CONFIG_PM
4688/*
4689 * power management
4690 */
4691
4692/**
4693 * snd_hda_suspend - suspend the codecs
4694 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 *
4696 * Returns 0 if successful.
4697 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004698int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004700 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Takashi Iwai0ba21762007-04-16 11:29:14 +02004702 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004703#ifdef CONFIG_SND_HDA_POWER_SAVE
4704 if (!codec->power_on)
4705 continue;
4706#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004707 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 }
4709 return 0;
4710}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004711EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
4713/**
4714 * snd_hda_resume - resume the codecs
4715 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 *
4717 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004718 *
4719 * This fucntion is defined only when POWER_SAVE isn't set.
4720 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 */
4722int snd_hda_resume(struct hda_bus *bus)
4723{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004724 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Takashi Iwai0ba21762007-04-16 11:29:14 +02004726 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004727 if (snd_hda_codec_needs_resume(codec))
4728 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 return 0;
4731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004732EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004733#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004734
4735/*
4736 * generic arrays
4737 */
4738
Takashi Iwaid5191e52009-11-16 14:58:17 +01004739/**
4740 * snd_array_new - get a new element from the given array
4741 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004742 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004743 * Get a new element from the given array. If it exceeds the
4744 * pre-allocated array size, re-allocate the array.
4745 *
4746 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004747 */
4748void *snd_array_new(struct snd_array *array)
4749{
4750 if (array->used >= array->alloced) {
4751 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004752 void *nlist;
4753 if (snd_BUG_ON(num >= 4096))
4754 return NULL;
4755 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004756 if (!nlist)
4757 return NULL;
4758 if (array->list) {
4759 memcpy(nlist, array->list,
4760 array->elem_size * array->alloced);
4761 kfree(array->list);
4762 }
4763 array->list = nlist;
4764 array->alloced = num;
4765 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004766 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004767}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004768EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004769
Takashi Iwaid5191e52009-11-16 14:58:17 +01004770/**
4771 * snd_array_free - free the given array elements
4772 * @array: the array object
4773 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004774void snd_array_free(struct snd_array *array)
4775{
4776 kfree(array->list);
4777 array->used = 0;
4778 array->alloced = 0;
4779 array->list = NULL;
4780}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004781EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004782
Takashi Iwaid5191e52009-11-16 14:58:17 +01004783/**
4784 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4785 * @pcm: PCM caps bits
4786 * @buf: the string buffer to write
4787 * @buflen: the max buffer length
4788 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004789 * used by hda_proc.c and hda_eld.c
4790 */
4791void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4792{
4793 static unsigned int rates[] = {
4794 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4795 96000, 176400, 192000, 384000
4796 };
4797 int i, j;
4798
4799 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4800 if (pcm & (1 << i))
4801 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4802
4803 buf[j] = '\0'; /* necessary when j == 0 */
4804}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004805EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004806
Takashi Iwaid5191e52009-11-16 14:58:17 +01004807/**
4808 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4809 * @pcm: PCM caps bits
4810 * @buf: the string buffer to write
4811 * @buflen: the max buffer length
4812 *
4813 * used by hda_proc.c and hda_eld.c
4814 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004815void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4816{
4817 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4818 int i, j;
4819
4820 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4821 if (pcm & (AC_SUPPCM_BITS_8 << i))
4822 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4823
4824 buf[j] = '\0'; /* necessary when j == 0 */
4825}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004826EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004827
4828MODULE_DESCRIPTION("HDA codec core");
4829MODULE_LICENSE("GPL");