blob: 720a81d711e3c9007dbcd9b0ef0eb562ec3d32af [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 INIT_LIST_HEAD(&bus->codec_list);
593
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100594 snprintf(bus->workq_name, sizeof(bus->workq_name),
595 "hd-audio%d", card->number);
596 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100597 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100598 snd_printk(KERN_ERR "cannot create workqueue %s\n",
599 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100600 kfree(bus);
601 return -ENOMEM;
602 }
603
Takashi Iwai0ba21762007-04-16 11:29:14 +0200604 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
605 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 snd_hda_bus_free(bus);
607 return err;
608 }
609 if (busp)
610 *busp = bus;
611 return 0;
612}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100613EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Takashi Iwai82467612007-07-27 19:15:54 +0200615#ifdef CONFIG_SND_HDA_GENERIC
616#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200617 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200618#else
619#define is_generic_config(codec) 0
620#endif
621
Takashi Iwai645f10c2008-11-28 15:07:37 +0100622#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100623#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
624#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100625#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100626#endif
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/*
629 * find a matching codec preset
630 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200631static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200632find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100634 struct hda_codec_preset_list *tbl;
635 const struct hda_codec_preset *preset;
636 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Takashi Iwai82467612007-07-27 19:15:54 +0200638 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100639 return NULL; /* use the generic parser */
640
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100641 again:
642 mutex_lock(&preset_mutex);
643 list_for_each_entry(tbl, &hda_preset_tables, list) {
644 if (!try_module_get(tbl->owner)) {
645 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
646 continue;
647 }
648 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100650 if (preset->afg && preset->afg != codec->afg)
651 continue;
652 if (preset->mfg && preset->mfg != codec->mfg)
653 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200654 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200656 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200657 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100658 preset->rev == codec->revision_id)) {
659 mutex_unlock(&preset_mutex);
660 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100664 module_put(tbl->owner);
665 }
666 mutex_unlock(&preset_mutex);
667
668 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
669 char name[32];
670 if (!mod_requested)
671 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
672 codec->vendor_id);
673 else
674 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
675 (codec->vendor_id >> 16) & 0xffff);
676 request_module(name);
677 mod_requested++;
678 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 return NULL;
681}
682
683/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200684 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200686static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 const struct hda_vendor_id *c;
689 const char *vendor = NULL;
690 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200691 char tmp[16];
692
693 if (codec->vendor_name)
694 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 for (c = hda_vendor_ids; c->id; c++) {
697 if (c->id == vendor_id) {
698 vendor = c->name;
699 break;
700 }
701 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200702 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 sprintf(tmp, "Generic %04x", vendor_id);
704 vendor = tmp;
705 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200706 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
707 if (!codec->vendor_name)
708 return -ENOMEM;
709
710 get_chip_name:
711 if (codec->chip_name)
712 return 0;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200715 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
716 else {
717 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
718 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
719 }
720 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200721 return -ENOMEM;
722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200726 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100728static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200730 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 hda_nid_t nid;
732
733 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
734 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200735 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200736 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200737 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200738 case AC_GRP_AUDIO_FUNCTION:
739 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200740 codec->afg_function_id = function_id & 0xff;
741 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200742 break;
743 case AC_GRP_MODEM_FUNCTION:
744 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200745 codec->mfg_function_id = function_id & 0xff;
746 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200747 break;
748 default:
749 break;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100755 * read widget caps for each widget and store in cache
756 */
757static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
758{
759 int i;
760 hda_nid_t nid;
761
762 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
763 &codec->start_nid);
764 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200765 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100766 return -ENOMEM;
767 nid = codec->start_nid;
768 for (i = 0; i < codec->num_nodes; i++, nid++)
769 codec->wcaps[i] = snd_hda_param_read(codec, nid,
770 AC_PAR_AUDIO_WIDGET_CAP);
771 return 0;
772}
773
Takashi Iwai3be14142009-02-20 14:11:16 +0100774/* read all pin default configurations and save codec->init_pins */
775static int read_pin_defaults(struct hda_codec *codec)
776{
777 int i;
778 hda_nid_t nid = codec->start_nid;
779
780 for (i = 0; i < codec->num_nodes; i++, nid++) {
781 struct hda_pincfg *pin;
782 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200783 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100784 if (wid_type != AC_WID_PIN)
785 continue;
786 pin = snd_array_new(&codec->init_pins);
787 if (!pin)
788 return -ENOMEM;
789 pin->nid = nid;
790 pin->cfg = snd_hda_codec_read(codec, nid, 0,
791 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200792 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
793 AC_VERB_GET_PIN_WIDGET_CONTROL,
794 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100795 }
796 return 0;
797}
798
799/* look up the given pin config list and return the item matching with NID */
800static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
801 struct snd_array *array,
802 hda_nid_t nid)
803{
804 int i;
805 for (i = 0; i < array->used; i++) {
806 struct hda_pincfg *pin = snd_array_elem(array, i);
807 if (pin->nid == nid)
808 return pin;
809 }
810 return NULL;
811}
812
813/* write a config value for the given NID */
814static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
815 unsigned int cfg)
816{
817 int i;
818 for (i = 0; i < 4; i++) {
819 snd_hda_codec_write(codec, nid, 0,
820 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
821 cfg & 0xff);
822 cfg >>= 8;
823 }
824}
825
826/* set the current pin config value for the given NID.
827 * the value is cached, and read via snd_hda_codec_get_pincfg()
828 */
829int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
830 hda_nid_t nid, unsigned int cfg)
831{
832 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100833 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100834
Takashi Iwaib82855a2009-12-27 11:24:56 +0100835 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
836 return -EINVAL;
837
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100838 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100839 pin = look_up_pincfg(codec, list, nid);
840 if (!pin) {
841 pin = snd_array_new(list);
842 if (!pin)
843 return -ENOMEM;
844 pin->nid = nid;
845 }
846 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100847
848 /* change only when needed; e.g. if the pincfg is already present
849 * in user_pins[], don't write it
850 */
851 cfg = snd_hda_codec_get_pincfg(codec, nid);
852 if (oldcfg != cfg)
853 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100854 return 0;
855}
856
Takashi Iwaid5191e52009-11-16 14:58:17 +0100857/**
858 * snd_hda_codec_set_pincfg - Override a pin default configuration
859 * @codec: the HDA codec
860 * @nid: NID to set the pin config
861 * @cfg: the pin default config value
862 *
863 * Override a pin default configuration value in the cache.
864 * This value can be read by snd_hda_codec_get_pincfg() in a higher
865 * priority than the real hardware value.
866 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100867int snd_hda_codec_set_pincfg(struct hda_codec *codec,
868 hda_nid_t nid, unsigned int cfg)
869{
Takashi Iwai346ff702009-02-23 09:42:57 +0100870 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100871}
872EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
873
Takashi Iwaid5191e52009-11-16 14:58:17 +0100874/**
875 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
876 * @codec: the HDA codec
877 * @nid: NID to get the pin config
878 *
879 * Get the current pin config value of the given pin NID.
880 * If the pincfg value is cached or overridden via sysfs or driver,
881 * returns the cached value.
882 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100883unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
884{
885 struct hda_pincfg *pin;
886
Takashi Iwai3be14142009-02-20 14:11:16 +0100887#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100888 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100889 if (pin)
890 return pin->cfg;
891#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100892 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
893 if (pin)
894 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100895 pin = look_up_pincfg(codec, &codec->init_pins, nid);
896 if (pin)
897 return pin->cfg;
898 return 0;
899}
900EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
901
902/* restore all current pin configs */
903static void restore_pincfgs(struct hda_codec *codec)
904{
905 int i;
906 for (i = 0; i < codec->init_pins.used; i++) {
907 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
908 set_pincfg(codec, pin->nid,
909 snd_hda_codec_get_pincfg(codec, pin->nid));
910 }
911}
Takashi Iwai54d17402005-11-21 16:33:22 +0100912
Takashi Iwai92ee6162009-12-27 11:18:59 +0100913/**
914 * snd_hda_shutup_pins - Shut up all pins
915 * @codec: the HDA codec
916 *
917 * Clear all pin controls to shup up before suspend for avoiding click noise.
918 * The controls aren't cached so that they can be resumed properly.
919 */
920void snd_hda_shutup_pins(struct hda_codec *codec)
921{
922 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200923 /* don't shut up pins when unloading the driver; otherwise it breaks
924 * the default pin setup at the next load of the driver
925 */
926 if (codec->bus->shutdown)
927 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100928 for (i = 0; i < codec->init_pins.used; i++) {
929 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
930 /* use read here for syncing after issuing each verb */
931 snd_hda_codec_read(codec, pin->nid, 0,
932 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
933 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200934 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100935}
936EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
937
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200938/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
939static void restore_shutup_pins(struct hda_codec *codec)
940{
941 int i;
942 if (!codec->pins_shutup)
943 return;
944 if (codec->bus->shutdown)
945 return;
946 for (i = 0; i < codec->init_pins.used; i++) {
947 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
948 snd_hda_codec_write(codec, pin->nid, 0,
949 AC_VERB_SET_PIN_WIDGET_CONTROL,
950 pin->ctrl);
951 }
952 codec->pins_shutup = 0;
953}
954
Takashi Iwai01751f52007-08-10 16:59:39 +0200955static void init_hda_cache(struct hda_cache_rec *cache,
956 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200957static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200958
Takashi Iwai3be14142009-02-20 14:11:16 +0100959/* restore the initial pin cfgs and release all pincfg lists */
960static void restore_init_pincfgs(struct hda_codec *codec)
961{
Takashi Iwai346ff702009-02-23 09:42:57 +0100962 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100963 * so that only the values in init_pins are restored
964 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100965 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100966#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100967 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100968#endif
969 restore_pincfgs(codec);
970 snd_array_free(&codec->init_pins);
971}
972
Takashi Iwai54d17402005-11-21 16:33:22 +0100973/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200974 * audio-converter setup caches
975 */
976struct hda_cvt_setup {
977 hda_nid_t nid;
978 u8 stream_tag;
979 u8 channel_id;
980 u16 format_id;
981 unsigned char active; /* cvt is currently used */
982 unsigned char dirty; /* setups should be cleared */
983};
984
985/* get or create a cache entry for the given audio converter NID */
986static struct hda_cvt_setup *
987get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
988{
989 struct hda_cvt_setup *p;
990 int i;
991
992 for (i = 0; i < codec->cvt_setups.used; i++) {
993 p = snd_array_elem(&codec->cvt_setups, i);
994 if (p->nid == nid)
995 return p;
996 }
997 p = snd_array_new(&codec->cvt_setups);
998 if (p)
999 p->nid = nid;
1000 return p;
1001}
1002
1003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * codec destructor
1005 */
1006static void snd_hda_codec_free(struct hda_codec *codec)
1007{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001008 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001010 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001011#ifdef CONFIG_SND_HDA_POWER_SAVE
1012 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001013 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001016 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001017 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 codec->bus->caddr_tbl[codec->addr] = NULL;
1019 if (codec->patch_ops.free)
1020 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001021 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001022 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001023 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001024 kfree(codec->vendor_name);
1025 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001026 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001027 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 kfree(codec);
1029}
1030
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001031static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1032 unsigned int power_state);
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/**
1035 * snd_hda_codec_new - create a HDA codec
1036 * @bus: the bus to assign
1037 * @codec_addr: the codec address
1038 * @codecp: the pointer to store the generated codec
1039 *
1040 * Returns 0 if successful, or a negative error code.
1041 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001042int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1043 unsigned int codec_addr,
1044 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001047 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int err;
1049
Takashi Iwaida3cec32008-08-08 17:12:14 +02001050 if (snd_BUG_ON(!bus))
1051 return -EINVAL;
1052 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1053 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001056 snd_printk(KERN_ERR "hda_codec: "
1057 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -EBUSY;
1059 }
1060
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001061 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (codec == NULL) {
1063 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1064 return -ENOMEM;
1065 }
1066
1067 codec->bus = bus;
1068 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001069 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001070 mutex_init(&codec->control_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02001071 mutex_init(&codec->prepare_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 Iwaieb541332010-08-06 13:48:11 +02001216 struct hda_cvt_setup *p;
1217 unsigned int oldval, newval;
1218 int i;
1219
Takashi Iwai0ba21762007-04-16 11:29:14 +02001220 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001221 return;
1222
Takashi Iwai0ba21762007-04-16 11:29:14 +02001223 snd_printdd("hda_codec_setup_stream: "
1224 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001226 p = get_hda_cvt_setup(codec, nid);
1227 if (!p)
1228 return;
1229 /* update the stream-id if changed */
1230 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1231 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1232 newval = (stream_tag << 4) | channel_id;
1233 if (oldval != newval)
1234 snd_hda_codec_write(codec, nid, 0,
1235 AC_VERB_SET_CHANNEL_STREAMID,
1236 newval);
1237 p->stream_tag = stream_tag;
1238 p->channel_id = channel_id;
1239 }
1240 /* update the format-id if changed */
1241 if (p->format_id != format) {
1242 oldval = snd_hda_codec_read(codec, nid, 0,
1243 AC_VERB_GET_STREAM_FORMAT, 0);
1244 if (oldval != format) {
1245 msleep(1);
1246 snd_hda_codec_write(codec, nid, 0,
1247 AC_VERB_SET_STREAM_FORMAT,
1248 format);
1249 }
1250 p->format_id = format;
1251 }
1252 p->active = 1;
1253 p->dirty = 0;
1254
1255 /* make other inactive cvts with the same stream-tag dirty */
1256 for (i = 0; i < codec->cvt_setups.used; i++) {
1257 p = snd_array_elem(&codec->cvt_setups, i);
1258 if (!p->active && p->stream_tag == stream_tag)
1259 p->dirty = 1;
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001262EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Takashi Iwaid5191e52009-11-16 14:58:17 +01001264/**
1265 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1266 * @codec: the CODEC to clean up
1267 * @nid: the NID to clean up
1268 */
Takashi Iwai888afa12008-03-18 09:57:50 +01001269void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1270{
Takashi Iwaieb541332010-08-06 13:48:11 +02001271 struct hda_cvt_setup *p;
1272
Takashi Iwai888afa12008-03-18 09:57:50 +01001273 if (!nid)
1274 return;
1275
1276 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001277 /* here we just clear the active flag; actual clean-ups will be done
1278 * in purify_inactive_streams()
1279 */
1280 p = get_hda_cvt_setup(codec, nid);
1281 if (p)
1282 p->active = 0;
Takashi Iwai888afa12008-03-18 09:57:50 +01001283}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001284EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001285
Takashi Iwaieb541332010-08-06 13:48:11 +02001286static void really_cleanup_stream(struct hda_codec *codec,
1287 struct hda_cvt_setup *q)
1288{
1289 hda_nid_t nid = q->nid;
1290 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1291 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1292 memset(q, 0, sizeof(*q));
1293 q->nid = nid;
1294}
1295
1296/* clean up the all conflicting obsolete streams */
1297static void purify_inactive_streams(struct hda_codec *codec)
1298{
1299 int i;
1300
1301 for (i = 0; i < codec->cvt_setups.used; i++) {
1302 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1303 if (p->dirty)
1304 really_cleanup_stream(codec, p);
1305 }
1306}
1307
1308/* clean up all streams; called from suspend */
1309static void hda_cleanup_all_streams(struct hda_codec *codec)
1310{
1311 int i;
1312
1313 for (i = 0; i < codec->cvt_setups.used; i++) {
1314 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1315 if (p->stream_tag)
1316 really_cleanup_stream(codec, p);
1317 }
1318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320/*
1321 * amp access functions
1322 */
1323
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001324/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001325#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001326#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001327#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1328#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001330#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001333static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001334 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Takashi Iwai01751f52007-08-10 16:59:39 +02001336 memset(cache, 0, sizeof(*cache));
1337 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001338 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001339}
1340
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001341static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001342{
Takashi Iwai603c4012008-07-30 15:01:44 +02001343 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001347static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Takashi Iwai01751f52007-08-10 16:59:39 +02001349 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1350 u16 cur = cache->hash[idx];
1351 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001354 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (info->key == key)
1356 return info;
1357 cur = info->next;
1358 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001359 return NULL;
1360}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001362/* query the hash. allocate an entry if not found. */
1363static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1364 u32 key)
1365{
1366 struct hda_cache_head *info = get_hash(cache, key);
1367 if (!info) {
1368 u16 idx, cur;
1369 /* add a new hash entry */
1370 info = snd_array_new(&cache->buf);
1371 if (!info)
1372 return NULL;
1373 cur = snd_array_index(&cache->buf, info);
1374 info->key = key;
1375 info->val = 0;
1376 idx = key % (u16)ARRAY_SIZE(cache->hash);
1377 info->next = cache->hash[idx];
1378 cache->hash[idx] = cur;
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return info;
1381}
1382
Takashi Iwai01751f52007-08-10 16:59:39 +02001383/* query and allocate an amp hash entry */
1384static inline struct hda_amp_info *
1385get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1386{
1387 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1388}
1389
Takashi Iwaid5191e52009-11-16 14:58:17 +01001390/**
1391 * query_amp_caps - query AMP capabilities
1392 * @codec: the HD-auio codec
1393 * @nid: the NID to query
1394 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1395 *
1396 * Query AMP capabilities for the given widget and direction.
1397 * Returns the obtained capability bits.
1398 *
1399 * When cap bits have been already read, this doesn't read again but
1400 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001402u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001404 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Takashi Iwai0ba21762007-04-16 11:29:14 +02001406 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1407 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001409 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001410 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001412 info->amp_caps = snd_hda_param_read(codec, nid,
1413 direction == HDA_OUTPUT ?
1414 AC_PAR_AMP_OUT_CAP :
1415 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001416 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001417 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419 return info->amp_caps;
1420}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001421EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Takashi Iwaid5191e52009-11-16 14:58:17 +01001423/**
1424 * snd_hda_override_amp_caps - Override the AMP capabilities
1425 * @codec: the CODEC to clean up
1426 * @nid: the NID to clean up
1427 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1428 * @caps: the capability bits to set
1429 *
1430 * Override the cached AMP caps bits value by the given one.
1431 * This function is useful if the driver needs to adjust the AMP ranges,
1432 * e.g. limit to 0dB, etc.
1433 *
1434 * Returns zero if successful or a negative error code.
1435 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001436int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1437 unsigned int caps)
1438{
1439 struct hda_amp_info *info;
1440
1441 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1442 if (!info)
1443 return -EINVAL;
1444 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001445 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001446 return 0;
1447}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001448EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001449
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001450static unsigned int
1451query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1452 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001453{
1454 struct hda_amp_info *info;
1455
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001456 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001457 if (!info)
1458 return 0;
1459 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001460 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001461 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001462 }
1463 return info->amp_caps;
1464}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001465
1466static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1467{
1468 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1469}
1470
Takashi Iwaid5191e52009-11-16 14:58:17 +01001471/**
1472 * snd_hda_query_pin_caps - Query PIN capabilities
1473 * @codec: the HD-auio codec
1474 * @nid: the NID to query
1475 *
1476 * Query PIN capabilities for the given widget.
1477 * Returns the obtained capability bits.
1478 *
1479 * When cap bits have been already read, this doesn't read again but
1480 * returns the cached value.
1481 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001482u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1483{
1484 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1485 read_pin_cap);
1486}
Takashi Iwai1327a322009-03-23 13:07:47 +01001487EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1488
Wu Fengguang864f92b2009-11-18 12:38:02 +08001489/**
1490 * snd_hda_pin_sense - execute pin sense measurement
1491 * @codec: the CODEC to sense
1492 * @nid: the pin NID to sense
1493 *
1494 * Execute necessary pin sense measurement and return its Presence Detect,
1495 * Impedance, ELD Valid etc. status bits.
1496 */
1497u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1498{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001499 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001500
Takashi Iwai729d55b2009-12-25 22:49:01 +01001501 if (!codec->no_trigger_sense) {
1502 pincap = snd_hda_query_pin_caps(codec, nid);
1503 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001504 snd_hda_codec_read(codec, nid, 0,
1505 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001506 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001507 return snd_hda_codec_read(codec, nid, 0,
1508 AC_VERB_GET_PIN_SENSE, 0);
1509}
1510EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1511
1512/**
1513 * snd_hda_jack_detect - query pin Presence Detect status
1514 * @codec: the CODEC to sense
1515 * @nid: the pin NID to sense
1516 *
1517 * Query and return the pin's Presence Detect status.
1518 */
1519int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1520{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001521 u32 sense = snd_hda_pin_sense(codec, nid);
1522 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001523}
1524EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526/*
1527 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001528 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001530static unsigned int get_vol_mute(struct hda_codec *codec,
1531 struct hda_amp_info *info, hda_nid_t nid,
1532 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 u32 val, parm;
1535
Takashi Iwai01751f52007-08-10 16:59:39 +02001536 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001537 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1540 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1541 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001542 val = snd_hda_codec_read(codec, nid, 0,
1543 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001545 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001546 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001550 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001552static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001553 hda_nid_t nid, int ch, int direction, int index,
1554 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 u32 parm;
1557
1558 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1559 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1560 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1561 parm |= val;
1562 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001563 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Takashi Iwaid5191e52009-11-16 14:58:17 +01001566/**
1567 * snd_hda_codec_amp_read - Read AMP value
1568 * @codec: HD-audio codec
1569 * @nid: NID to read the AMP value
1570 * @ch: channel (left=0 or right=1)
1571 * @direction: #HDA_INPUT or #HDA_OUTPUT
1572 * @index: the index value (only for input direction)
1573 *
1574 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 */
Takashi Iwai834be882006-03-01 14:16:17 +01001576int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1577 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001579 struct hda_amp_info *info;
1580 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1581 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001583 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001585EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Takashi Iwaid5191e52009-11-16 14:58:17 +01001587/**
1588 * snd_hda_codec_amp_update - update the AMP value
1589 * @codec: HD-audio codec
1590 * @nid: NID to read the AMP value
1591 * @ch: channel (left=0 or right=1)
1592 * @direction: #HDA_INPUT or #HDA_OUTPUT
1593 * @idx: the index value (only for input direction)
1594 * @mask: bit mask to set
1595 * @val: the bits value to set
1596 *
1597 * Update the AMP value with a bit mask.
1598 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001599 */
Takashi Iwai834be882006-03-01 14:16:17 +01001600int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1601 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001603 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001604
Takashi Iwai0ba21762007-04-16 11:29:14 +02001605 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1606 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001608 if (snd_BUG_ON(mask & ~0xff))
1609 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001610 val &= mask;
1611 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001612 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001614 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return 1;
1616}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001617EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Takashi Iwaid5191e52009-11-16 14:58:17 +01001619/**
1620 * snd_hda_codec_amp_stereo - update the AMP stereo values
1621 * @codec: HD-audio codec
1622 * @nid: NID to read the AMP value
1623 * @direction: #HDA_INPUT or #HDA_OUTPUT
1624 * @idx: the index value (only for input direction)
1625 * @mask: bit mask to set
1626 * @val: the bits value to set
1627 *
1628 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1629 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001630 */
1631int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1632 int direction, int idx, int mask, int val)
1633{
1634 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001635
1636 if (snd_BUG_ON(mask & ~0xff))
1637 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001638 for (ch = 0; ch < 2; ch++)
1639 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1640 idx, mask, val);
1641 return ret;
1642}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001643EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001644
Takashi Iwaicb53c622007-08-10 17:21:45 +02001645#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001646/**
1647 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1648 * @codec: HD-audio codec
1649 *
1650 * Resume the all amp commands from the cache.
1651 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001652void snd_hda_codec_resume_amp(struct hda_codec *codec)
1653{
Takashi Iwai603c4012008-07-30 15:01:44 +02001654 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001655 int i;
1656
Takashi Iwai603c4012008-07-30 15:01:44 +02001657 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001658 u32 key = buffer->head.key;
1659 hda_nid_t nid;
1660 unsigned int idx, dir, ch;
1661 if (!key)
1662 continue;
1663 nid = key & 0xff;
1664 idx = (key >> 16) & 0xff;
1665 dir = (key >> 24) & 0xff;
1666 for (ch = 0; ch < 2; ch++) {
1667 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1668 continue;
1669 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1670 buffer->vol[ch]);
1671 }
1672 }
1673}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001674EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001675#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001677static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1678 unsigned int ofs)
1679{
1680 u32 caps = query_amp_caps(codec, nid, dir);
1681 /* get num steps */
1682 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1683 if (ofs < caps)
1684 caps -= ofs;
1685 return caps;
1686}
1687
Takashi Iwaid5191e52009-11-16 14:58:17 +01001688/**
1689 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1690 *
1691 * The control element is supposed to have the private_value field
1692 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1693 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001694int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1695 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1698 u16 nid = get_amp_nid(kcontrol);
1699 u8 chs = get_amp_channels(kcontrol);
1700 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001701 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001703 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1704 uinfo->count = chs == 3 ? 2 : 1;
1705 uinfo->value.integer.min = 0;
1706 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1707 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001708 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001709 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1710 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return -EINVAL;
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return 0;
1714}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001715EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001717
1718static inline unsigned int
1719read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1720 int ch, int dir, int idx, unsigned int ofs)
1721{
1722 unsigned int val;
1723 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1724 val &= HDA_AMP_VOLMASK;
1725 if (val >= ofs)
1726 val -= ofs;
1727 else
1728 val = 0;
1729 return val;
1730}
1731
1732static inline int
1733update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1734 int ch, int dir, int idx, unsigned int ofs,
1735 unsigned int val)
1736{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001737 unsigned int maxval;
1738
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001739 if (val > 0)
1740 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001741 /* ofs = 0: raw max value */
1742 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001743 if (val > maxval)
1744 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001745 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1746 HDA_AMP_VOLMASK, val);
1747}
1748
Takashi Iwaid5191e52009-11-16 14:58:17 +01001749/**
1750 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1751 *
1752 * The control element is supposed to have the private_value field
1753 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1754 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001755int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1756 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1759 hda_nid_t nid = get_amp_nid(kcontrol);
1760 int chs = get_amp_channels(kcontrol);
1761 int dir = get_amp_direction(kcontrol);
1762 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001763 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 long *valp = ucontrol->value.integer.value;
1765
1766 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001767 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001769 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return 0;
1771}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001772EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Takashi Iwaid5191e52009-11-16 14:58:17 +01001774/**
1775 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1776 *
1777 * The control element is supposed to have the private_value field
1778 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1779 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001780int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1781 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1784 hda_nid_t nid = get_amp_nid(kcontrol);
1785 int chs = get_amp_channels(kcontrol);
1786 int dir = get_amp_direction(kcontrol);
1787 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001788 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 long *valp = ucontrol->value.integer.value;
1790 int change = 0;
1791
Takashi Iwaicb53c622007-08-10 17:21:45 +02001792 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001793 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001794 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001795 valp++;
1796 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001797 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001798 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001799 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return change;
1801}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001802EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Takashi Iwaid5191e52009-11-16 14:58:17 +01001804/**
1805 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1806 *
1807 * The control element is supposed to have the private_value field
1808 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1809 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001810int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1811 unsigned int size, unsigned int __user *_tlv)
1812{
1813 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1814 hda_nid_t nid = get_amp_nid(kcontrol);
1815 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001816 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001817 u32 caps, val1, val2;
1818
1819 if (size < 4 * sizeof(unsigned int))
1820 return -ENOMEM;
1821 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001822 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1823 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001824 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001825 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001826 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001827 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1828 return -EFAULT;
1829 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1830 return -EFAULT;
1831 if (put_user(val1, _tlv + 2))
1832 return -EFAULT;
1833 if (put_user(val2, _tlv + 3))
1834 return -EFAULT;
1835 return 0;
1836}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001837EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001838
Takashi Iwaid5191e52009-11-16 14:58:17 +01001839/**
1840 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1841 * @codec: HD-audio codec
1842 * @nid: NID of a reference widget
1843 * @dir: #HDA_INPUT or #HDA_OUTPUT
1844 * @tlv: TLV data to be stored, at least 4 elements
1845 *
1846 * Set (static) TLV data for a virtual master volume using the AMP caps
1847 * obtained from the reference NID.
1848 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001849 */
1850void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1851 unsigned int *tlv)
1852{
1853 u32 caps;
1854 int nums, step;
1855
1856 caps = query_amp_caps(codec, nid, dir);
1857 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1858 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1859 step = (step + 1) * 25;
1860 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1861 tlv[1] = 2 * sizeof(unsigned int);
1862 tlv[2] = -nums * step;
1863 tlv[3] = step;
1864}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001865EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001866
1867/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001868static struct snd_kcontrol *
1869_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1870 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001871{
1872 struct snd_ctl_elem_id id;
1873 memset(&id, 0, sizeof(id));
1874 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001875 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001876 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1877 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001878 strcpy(id.name, name);
1879 return snd_ctl_find_id(codec->bus->card, &id);
1880}
1881
Takashi Iwaid5191e52009-11-16 14:58:17 +01001882/**
1883 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1884 * @codec: HD-audio codec
1885 * @name: ctl id name string
1886 *
1887 * Get the control element with the given id string and IFACE_MIXER.
1888 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001889struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1890 const char *name)
1891{
1892 return _snd_hda_find_mixer_ctl(codec, name, 0);
1893}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001894EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001895
Takashi Iwaid5191e52009-11-16 14:58:17 +01001896/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001897 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001898 * @codec: HD-audio codec
1899 * @nid: corresponding NID (optional)
1900 * @kctl: the control element to assign
1901 *
1902 * Add the given control element to an array inside the codec instance.
1903 * All control elements belonging to a codec are supposed to be added
1904 * by this function so that a proper clean-up works at the free or
1905 * reconfiguration time.
1906 *
1907 * If non-zero @nid is passed, the NID is assigned to the control element.
1908 * The assignment is shown in the codec proc file.
1909 *
1910 * snd_hda_ctl_add() checks the control subdev id field whether
1911 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001912 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1913 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001914 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001915int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1916 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001917{
1918 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001919 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001920 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001921
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001922 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001923 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001924 if (nid == 0)
1925 nid = get_amp_nid_(kctl->private_value);
1926 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001927 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1928 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001929 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001930 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001931 err = snd_ctl_add(codec->bus->card, kctl);
1932 if (err < 0)
1933 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001934 item = snd_array_new(&codec->mixers);
1935 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001936 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001937 item->kctl = kctl;
1938 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001939 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001940 return 0;
1941}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001942EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001943
Takashi Iwaid5191e52009-11-16 14:58:17 +01001944/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001945 * snd_hda_add_nid - Assign a NID to a control element
1946 * @codec: HD-audio codec
1947 * @nid: corresponding NID (optional)
1948 * @kctl: the control element to assign
1949 * @index: index to kctl
1950 *
1951 * Add the given control element to an array inside the codec instance.
1952 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1953 * NID:KCTL mapping - for example "Capture Source" selector.
1954 */
1955int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1956 unsigned int index, hda_nid_t nid)
1957{
1958 struct hda_nid_item *item;
1959
1960 if (nid > 0) {
1961 item = snd_array_new(&codec->nids);
1962 if (!item)
1963 return -ENOMEM;
1964 item->kctl = kctl;
1965 item->index = index;
1966 item->nid = nid;
1967 return 0;
1968 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01001969 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1970 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001971 return -EINVAL;
1972}
1973EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1974
1975/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001976 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1977 * @codec: HD-audio codec
1978 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001979void snd_hda_ctls_clear(struct hda_codec *codec)
1980{
1981 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001982 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001983 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001984 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001985 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001986 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001987}
1988
Takashi Iwaia65d6292009-02-23 16:57:04 +01001989/* pseudo device locking
1990 * toggle card->shutdown to allow/disallow the device access (as a hack)
1991 */
1992static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001993{
Takashi Iwaia65d6292009-02-23 16:57:04 +01001994 spin_lock(&card->files_lock);
1995 if (card->shutdown) {
1996 spin_unlock(&card->files_lock);
1997 return -EINVAL;
1998 }
1999 card->shutdown = 1;
2000 spin_unlock(&card->files_lock);
2001 return 0;
2002}
2003
2004static void hda_unlock_devices(struct snd_card *card)
2005{
2006 spin_lock(&card->files_lock);
2007 card->shutdown = 0;
2008 spin_unlock(&card->files_lock);
2009}
2010
Takashi Iwaid5191e52009-11-16 14:58:17 +01002011/**
2012 * snd_hda_codec_reset - Clear all objects assigned to the codec
2013 * @codec: HD-audio codec
2014 *
2015 * This frees the all PCM and control elements assigned to the codec, and
2016 * clears the caches and restores the pin default configurations.
2017 *
2018 * When a device is being used, it returns -EBSY. If successfully freed,
2019 * returns zero.
2020 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002021int snd_hda_codec_reset(struct hda_codec *codec)
2022{
2023 struct snd_card *card = codec->bus->card;
2024 int i, pcm;
2025
2026 if (hda_lock_devices(card) < 0)
2027 return -EBUSY;
2028 /* check whether the codec isn't used by any mixer or PCM streams */
2029 if (!list_empty(&card->ctl_files)) {
2030 hda_unlock_devices(card);
2031 return -EBUSY;
2032 }
2033 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2034 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2035 if (!cpcm->pcm)
2036 continue;
2037 if (cpcm->pcm->streams[0].substream_opened ||
2038 cpcm->pcm->streams[1].substream_opened) {
2039 hda_unlock_devices(card);
2040 return -EBUSY;
2041 }
2042 }
2043
2044 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002045
2046#ifdef CONFIG_SND_HDA_POWER_SAVE
2047 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002048 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002049#endif
2050 snd_hda_ctls_clear(codec);
2051 /* relase PCMs */
2052 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002053 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002054 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002055 clear_bit(codec->pcm_info[i].device,
2056 codec->bus->pcm_dev_bits);
2057 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002058 }
2059 if (codec->patch_ops.free)
2060 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002061 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002062 codec->spec = NULL;
2063 free_hda_cache(&codec->amp_cache);
2064 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002065 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2066 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002067 /* free only driver_pins so that init_pins + user_pins are restored */
2068 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002069 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002070 codec->num_pcms = 0;
2071 codec->pcm_info = NULL;
2072 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002073 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2074 codec->slave_dig_outs = NULL;
2075 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002076 module_put(codec->owner);
2077 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002078
2079 /* allow device access again */
2080 hda_unlock_devices(card);
2081 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002082}
2083
Takashi Iwaid5191e52009-11-16 14:58:17 +01002084/**
2085 * snd_hda_add_vmaster - create a virtual master control and add slaves
2086 * @codec: HD-audio codec
2087 * @name: vmaster control name
2088 * @tlv: TLV data (optional)
2089 * @slaves: slave control names (optional)
2090 *
2091 * Create a virtual master control with the given name. The TLV data
2092 * must be either NULL or a valid data.
2093 *
2094 * @slaves is a NULL-terminated array of strings, each of which is a
2095 * slave control name. All controls with these names are assigned to
2096 * the new virtual master control.
2097 *
2098 * This function returns zero if successful or a negative error code.
2099 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002100int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2101 unsigned int *tlv, const char **slaves)
2102{
2103 struct snd_kcontrol *kctl;
2104 const char **s;
2105 int err;
2106
Takashi Iwai2f085542008-02-22 18:43:50 +01002107 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2108 ;
2109 if (!*s) {
2110 snd_printdd("No slave found for %s\n", name);
2111 return 0;
2112 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002113 kctl = snd_ctl_make_virtual_master(name, tlv);
2114 if (!kctl)
2115 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002116 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002117 if (err < 0)
2118 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002119
Takashi Iwai2134ea42008-01-10 16:53:55 +01002120 for (s = slaves; *s; s++) {
2121 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002122 int i = 0;
2123 for (;;) {
2124 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2125 if (!sctl) {
2126 if (!i)
2127 snd_printdd("Cannot find slave %s, "
2128 "skipped\n", *s);
2129 break;
2130 }
2131 err = snd_ctl_add_slave(kctl, sctl);
2132 if (err < 0)
2133 return err;
2134 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002135 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002136 }
2137 return 0;
2138}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002139EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002140
Takashi Iwaid5191e52009-11-16 14:58:17 +01002141/**
2142 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2143 *
2144 * The control element is supposed to have the private_value field
2145 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2146 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002147int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2148 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 int chs = get_amp_channels(kcontrol);
2151
2152 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2153 uinfo->count = chs == 3 ? 2 : 1;
2154 uinfo->value.integer.min = 0;
2155 uinfo->value.integer.max = 1;
2156 return 0;
2157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002158EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Takashi Iwaid5191e52009-11-16 14:58:17 +01002160/**
2161 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2162 *
2163 * The control element is supposed to have the private_value field
2164 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2165 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002166int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2167 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2170 hda_nid_t nid = get_amp_nid(kcontrol);
2171 int chs = get_amp_channels(kcontrol);
2172 int dir = get_amp_direction(kcontrol);
2173 int idx = get_amp_index(kcontrol);
2174 long *valp = ucontrol->value.integer.value;
2175
2176 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002177 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002178 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002180 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002181 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return 0;
2183}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002184EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Takashi Iwaid5191e52009-11-16 14:58:17 +01002186/**
2187 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2188 *
2189 * The control element is supposed to have the private_value field
2190 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2191 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002192int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2193 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2196 hda_nid_t nid = get_amp_nid(kcontrol);
2197 int chs = get_amp_channels(kcontrol);
2198 int dir = get_amp_direction(kcontrol);
2199 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 long *valp = ucontrol->value.integer.value;
2201 int change = 0;
2202
Takashi Iwaicb53c622007-08-10 17:21:45 +02002203 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002204 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002205 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002206 HDA_AMP_MUTE,
2207 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002208 valp++;
2209 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002210 if (chs & 2)
2211 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002212 HDA_AMP_MUTE,
2213 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002214#ifdef CONFIG_SND_HDA_POWER_SAVE
2215 if (codec->patch_ops.check_power_status)
2216 codec->patch_ops.check_power_status(codec, nid);
2217#endif
2218 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return change;
2220}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002221EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Takashi Iwai67d634c2009-11-16 15:35:59 +01002223#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002224/**
2225 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2226 *
2227 * This function calls snd_hda_enable_beep_device(), which behaves differently
2228 * depending on beep_mode option.
2229 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002230int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2231 struct snd_ctl_elem_value *ucontrol)
2232{
2233 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2234 long *valp = ucontrol->value.integer.value;
2235
2236 snd_hda_enable_beep_device(codec, *valp);
2237 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2238}
2239EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002240#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242/*
Takashi Iwai985be542005-11-02 18:26:49 +01002243 * bound volume controls
2244 *
2245 * bind multiple volumes (# indices, from 0)
2246 */
2247
2248#define AMP_VAL_IDX_SHIFT 19
2249#define AMP_VAL_IDX_MASK (0x0f<<19)
2250
Takashi Iwaid5191e52009-11-16 14:58:17 +01002251/**
2252 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2253 *
2254 * The control element is supposed to have the private_value field
2255 * set up via HDA_BIND_MUTE*() macros.
2256 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002257int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2258 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002259{
2260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2261 unsigned long pval;
2262 int err;
2263
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002264 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002265 pval = kcontrol->private_value;
2266 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2267 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2268 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002269 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002270 return err;
2271}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002272EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002273
Takashi Iwaid5191e52009-11-16 14:58:17 +01002274/**
2275 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2276 *
2277 * The control element is supposed to have the private_value field
2278 * set up via HDA_BIND_MUTE*() macros.
2279 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002280int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002282{
2283 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2284 unsigned long pval;
2285 int i, indices, err = 0, change = 0;
2286
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002287 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002288 pval = kcontrol->private_value;
2289 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2290 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002291 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2292 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002293 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2294 if (err < 0)
2295 break;
2296 change |= err;
2297 }
2298 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002299 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002300 return err < 0 ? err : change;
2301}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002302EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002303
Takashi Iwaid5191e52009-11-16 14:58:17 +01002304/**
2305 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2306 *
2307 * The control element is supposed to have the private_value field
2308 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002309 */
2310int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2311 struct snd_ctl_elem_info *uinfo)
2312{
2313 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2314 struct hda_bind_ctls *c;
2315 int err;
2316
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002317 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002318 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002319 kcontrol->private_value = *c->values;
2320 err = c->ops->info(kcontrol, uinfo);
2321 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002322 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002323 return err;
2324}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002325EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002326
Takashi Iwaid5191e52009-11-16 14:58:17 +01002327/**
2328 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2329 *
2330 * The control element is supposed to have the private_value field
2331 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2332 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002333int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2335{
2336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2337 struct hda_bind_ctls *c;
2338 int err;
2339
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002340 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002341 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002342 kcontrol->private_value = *c->values;
2343 err = c->ops->get(kcontrol, ucontrol);
2344 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002345 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002346 return err;
2347}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002348EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002349
Takashi Iwaid5191e52009-11-16 14:58:17 +01002350/**
2351 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2352 *
2353 * The control element is supposed to have the private_value field
2354 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2355 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002356int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
2358{
2359 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2360 struct hda_bind_ctls *c;
2361 unsigned long *vals;
2362 int err = 0, change = 0;
2363
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002364 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002365 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002366 for (vals = c->values; *vals; vals++) {
2367 kcontrol->private_value = *vals;
2368 err = c->ops->put(kcontrol, ucontrol);
2369 if (err < 0)
2370 break;
2371 change |= err;
2372 }
2373 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002374 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002375 return err < 0 ? err : change;
2376}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002377EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002378
Takashi Iwaid5191e52009-11-16 14:58:17 +01002379/**
2380 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2381 *
2382 * The control element is supposed to have the private_value field
2383 * set up via HDA_BIND_VOL() macro.
2384 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002385int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2386 unsigned int size, unsigned int __user *tlv)
2387{
2388 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2389 struct hda_bind_ctls *c;
2390 int err;
2391
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002392 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002393 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002394 kcontrol->private_value = *c->values;
2395 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2396 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002397 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002398 return err;
2399}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002400EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002401
2402struct hda_ctl_ops snd_hda_bind_vol = {
2403 .info = snd_hda_mixer_amp_volume_info,
2404 .get = snd_hda_mixer_amp_volume_get,
2405 .put = snd_hda_mixer_amp_volume_put,
2406 .tlv = snd_hda_mixer_amp_tlv
2407};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002408EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002409
2410struct hda_ctl_ops snd_hda_bind_sw = {
2411 .info = snd_hda_mixer_amp_switch_info,
2412 .get = snd_hda_mixer_amp_switch_get,
2413 .put = snd_hda_mixer_amp_switch_put,
2414 .tlv = snd_hda_mixer_amp_tlv
2415};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002416EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002417
2418/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 * SPDIF out controls
2420 */
2421
Takashi Iwai0ba21762007-04-16 11:29:14 +02002422static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
2425 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2426 uinfo->count = 1;
2427 return 0;
2428}
2429
Takashi Iwai0ba21762007-04-16 11:29:14 +02002430static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2431 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2434 IEC958_AES0_NONAUDIO |
2435 IEC958_AES0_CON_EMPHASIS_5015 |
2436 IEC958_AES0_CON_NOT_COPYRIGHT;
2437 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2438 IEC958_AES1_CON_ORIGINAL;
2439 return 0;
2440}
2441
Takashi Iwai0ba21762007-04-16 11:29:14 +02002442static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2443 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2446 IEC958_AES0_NONAUDIO |
2447 IEC958_AES0_PRO_EMPHASIS_5015;
2448 return 0;
2449}
2450
Takashi Iwai0ba21762007-04-16 11:29:14 +02002451static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
2454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2455
2456 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2457 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2458 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2459 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2460
2461 return 0;
2462}
2463
2464/* convert from SPDIF status bits to HDA SPDIF bits
2465 * bit 0 (DigEn) is always set zero (to be filled later)
2466 */
2467static unsigned short convert_from_spdif_status(unsigned int sbits)
2468{
2469 unsigned short val = 0;
2470
2471 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002472 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002474 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002476 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2477 IEC958_AES0_PRO_EMPHASIS_5015)
2478 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002480 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2481 IEC958_AES0_CON_EMPHASIS_5015)
2482 val |= AC_DIG1_EMPHASIS;
2483 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2484 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002486 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2488 }
2489 return val;
2490}
2491
2492/* convert to SPDIF status bits from HDA SPDIF bits
2493 */
2494static unsigned int convert_to_spdif_status(unsigned short val)
2495{
2496 unsigned int sbits = 0;
2497
Takashi Iwai0ba21762007-04-16 11:29:14 +02002498 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002500 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 sbits |= IEC958_AES0_PROFESSIONAL;
2502 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002503 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2505 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002506 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002508 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002510 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2512 sbits |= val & (0x7f << 8);
2513 }
2514 return sbits;
2515}
2516
Takashi Iwai2f728532008-09-25 16:32:41 +02002517/* set digital convert verbs both for the given NID and its slaves */
2518static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2519 int verb, int val)
2520{
2521 hda_nid_t *d;
2522
Takashi Iwai9e976972008-11-25 08:17:20 +01002523 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002524 d = codec->slave_dig_outs;
2525 if (!d)
2526 return;
2527 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002528 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002529}
2530
2531static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2532 int dig1, int dig2)
2533{
2534 if (dig1 != -1)
2535 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2536 if (dig2 != -1)
2537 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2538}
2539
Takashi Iwai0ba21762007-04-16 11:29:14 +02002540static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2541 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2544 hda_nid_t nid = kcontrol->private_value;
2545 unsigned short val;
2546 int change;
2547
Ingo Molnar62932df2006-01-16 16:34:20 +01002548 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 codec->spdif_status = ucontrol->value.iec958.status[0] |
2550 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2551 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2552 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2553 val = convert_from_spdif_status(codec->spdif_status);
2554 val |= codec->spdif_ctls & 1;
2555 change = codec->spdif_ctls != val;
2556 codec->spdif_ctls = val;
2557
Takashi Iwai2f728532008-09-25 16:32:41 +02002558 if (change)
2559 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Ingo Molnar62932df2006-01-16 16:34:20 +01002561 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return change;
2563}
2564
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002565#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Takashi Iwai0ba21762007-04-16 11:29:14 +02002567static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2568 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2571
Takashi Iwai0ba21762007-04-16 11:29:14 +02002572 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return 0;
2574}
2575
Takashi Iwai0ba21762007-04-16 11:29:14 +02002576static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2577 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2580 hda_nid_t nid = kcontrol->private_value;
2581 unsigned short val;
2582 int change;
2583
Ingo Molnar62932df2006-01-16 16:34:20 +01002584 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002585 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002587 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002589 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002591 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002592 /* unmute amp switch (if any) */
2593 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002594 (val & AC_DIG1_ENABLE))
2595 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2596 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002598 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 return change;
2600}
2601
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002602static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 {
2604 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2605 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002606 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 .info = snd_hda_spdif_mask_info,
2608 .get = snd_hda_spdif_cmask_get,
2609 },
2610 {
2611 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2612 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002613 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 .info = snd_hda_spdif_mask_info,
2615 .get = snd_hda_spdif_pmask_get,
2616 },
2617 {
2618 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002619 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 .info = snd_hda_spdif_mask_info,
2621 .get = snd_hda_spdif_default_get,
2622 .put = snd_hda_spdif_default_put,
2623 },
2624 {
2625 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002626 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 .info = snd_hda_spdif_out_switch_info,
2628 .get = snd_hda_spdif_out_switch_get,
2629 .put = snd_hda_spdif_out_switch_put,
2630 },
2631 { } /* end */
2632};
2633
Takashi Iwai09f99702008-02-04 12:31:13 +01002634#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636/**
2637 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2638 * @codec: the HDA codec
2639 * @nid: audio out widget NID
2640 *
2641 * Creates controls related with the SPDIF output.
2642 * Called from each patch supporting the SPDIF out.
2643 *
2644 * Returns 0 if successful, or a negative error code.
2645 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002646int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
2648 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002649 struct snd_kcontrol *kctl;
2650 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002651 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Takashi Iwai09f99702008-02-04 12:31:13 +01002653 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2654 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2655 idx))
2656 break;
2657 }
2658 if (idx >= SPDIF_MAX_IDX) {
2659 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2660 return -EBUSY;
2661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2663 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002664 if (!kctl)
2665 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002666 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002668 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002669 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return err;
2671 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002672 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002673 snd_hda_codec_read(codec, nid, 0,
2674 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2676 return 0;
2677}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002678EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002681 * SPDIF sharing with analog output
2682 */
2683static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2684 struct snd_ctl_elem_value *ucontrol)
2685{
2686 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2687 ucontrol->value.integer.value[0] = mout->share_spdif;
2688 return 0;
2689}
2690
2691static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2693{
2694 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2695 mout->share_spdif = !!ucontrol->value.integer.value[0];
2696 return 0;
2697}
2698
2699static struct snd_kcontrol_new spdif_share_sw = {
2700 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2701 .name = "IEC958 Default PCM Playback Switch",
2702 .info = snd_ctl_boolean_mono_info,
2703 .get = spdif_share_sw_get,
2704 .put = spdif_share_sw_put,
2705};
2706
Takashi Iwaid5191e52009-11-16 14:58:17 +01002707/**
2708 * snd_hda_create_spdif_share_sw - create Default PCM switch
2709 * @codec: the HDA codec
2710 * @mout: multi-out instance
2711 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002712int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2713 struct hda_multi_out *mout)
2714{
2715 if (!mout->dig_out_nid)
2716 return 0;
2717 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002718 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2719 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002720}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002721EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002722
2723/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 * SPDIF input
2725 */
2726
2727#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2728
Takashi Iwai0ba21762007-04-16 11:29:14 +02002729static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2730 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
2732 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2733
2734 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2735 return 0;
2736}
2737
Takashi Iwai0ba21762007-04-16 11:29:14 +02002738static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2739 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
2741 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2742 hda_nid_t nid = kcontrol->private_value;
2743 unsigned int val = !!ucontrol->value.integer.value[0];
2744 int change;
2745
Ingo Molnar62932df2006-01-16 16:34:20 +01002746 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002748 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002750 snd_hda_codec_write_cache(codec, nid, 0,
2751 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002753 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return change;
2755}
2756
Takashi Iwai0ba21762007-04-16 11:29:14 +02002757static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
2760 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2761 hda_nid_t nid = kcontrol->private_value;
2762 unsigned short val;
2763 unsigned int sbits;
2764
Andrew Paprocki3982d172007-12-19 12:13:44 +01002765 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 sbits = convert_to_spdif_status(val);
2767 ucontrol->value.iec958.status[0] = sbits;
2768 ucontrol->value.iec958.status[1] = sbits >> 8;
2769 ucontrol->value.iec958.status[2] = sbits >> 16;
2770 ucontrol->value.iec958.status[3] = sbits >> 24;
2771 return 0;
2772}
2773
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002774static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 {
2776 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002777 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 .info = snd_hda_spdif_in_switch_info,
2779 .get = snd_hda_spdif_in_switch_get,
2780 .put = snd_hda_spdif_in_switch_put,
2781 },
2782 {
2783 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2784 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002785 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 .info = snd_hda_spdif_mask_info,
2787 .get = snd_hda_spdif_in_status_get,
2788 },
2789 { } /* end */
2790};
2791
2792/**
2793 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2794 * @codec: the HDA codec
2795 * @nid: audio in widget NID
2796 *
2797 * Creates controls related with the SPDIF input.
2798 * Called from each patch supporting the SPDIF in.
2799 *
2800 * Returns 0 if successful, or a negative error code.
2801 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002802int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
2804 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002805 struct snd_kcontrol *kctl;
2806 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002807 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Takashi Iwai09f99702008-02-04 12:31:13 +01002809 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2810 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2811 idx))
2812 break;
2813 }
2814 if (idx >= SPDIF_MAX_IDX) {
2815 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2816 return -EBUSY;
2817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2819 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002820 if (!kctl)
2821 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002823 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002824 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return err;
2826 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002827 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002828 snd_hda_codec_read(codec, nid, 0,
2829 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002830 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return 0;
2832}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002833EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Takashi Iwaicb53c622007-08-10 17:21:45 +02002835#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002836/*
2837 * command cache
2838 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002840/* build a 32bit cache key with the widget id and the command parameter */
2841#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2842#define get_cmd_cache_nid(key) ((key) & 0xff)
2843#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2844
2845/**
2846 * snd_hda_codec_write_cache - send a single command with caching
2847 * @codec: the HDA codec
2848 * @nid: NID to send the command
2849 * @direct: direct flag
2850 * @verb: the verb to send
2851 * @parm: the parameter for the verb
2852 *
2853 * Send a single command without waiting for response.
2854 *
2855 * Returns 0 if successful, or a negative error code.
2856 */
2857int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2858 int direct, unsigned int verb, unsigned int parm)
2859{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002860 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2861 struct hda_cache_head *c;
2862 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002863
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002864 if (err < 0)
2865 return err;
2866 /* parm may contain the verb stuff for get/set amp */
2867 verb = verb | (parm >> 8);
2868 parm &= 0xff;
2869 key = build_cmd_cache_key(nid, verb);
2870 mutex_lock(&codec->bus->cmd_mutex);
2871 c = get_alloc_hash(&codec->cmd_cache, key);
2872 if (c)
2873 c->val = parm;
2874 mutex_unlock(&codec->bus->cmd_mutex);
2875 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002876}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002877EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002878
Takashi Iwaid5191e52009-11-16 14:58:17 +01002879/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02002880 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2881 * @codec: the HDA codec
2882 * @nid: NID to send the command
2883 * @direct: direct flag
2884 * @verb: the verb to send
2885 * @parm: the parameter for the verb
2886 *
2887 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2888 * command if the parameter is already identical with the cached value.
2889 * If not, it sends the command and refreshes the cache.
2890 *
2891 * Returns 0 if successful, or a negative error code.
2892 */
2893int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2894 int direct, unsigned int verb, unsigned int parm)
2895{
2896 struct hda_cache_head *c;
2897 u32 key;
2898
2899 /* parm may contain the verb stuff for get/set amp */
2900 verb = verb | (parm >> 8);
2901 parm &= 0xff;
2902 key = build_cmd_cache_key(nid, verb);
2903 mutex_lock(&codec->bus->cmd_mutex);
2904 c = get_hash(&codec->cmd_cache, key);
2905 if (c && c->val == parm) {
2906 mutex_unlock(&codec->bus->cmd_mutex);
2907 return 0;
2908 }
2909 mutex_unlock(&codec->bus->cmd_mutex);
2910 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2911}
2912EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2913
2914/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002915 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2916 * @codec: HD-audio codec
2917 *
2918 * Execute all verbs recorded in the command caches to resume.
2919 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002920void snd_hda_codec_resume_cache(struct hda_codec *codec)
2921{
Takashi Iwai603c4012008-07-30 15:01:44 +02002922 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002923 int i;
2924
Takashi Iwai603c4012008-07-30 15:01:44 +02002925 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002926 u32 key = buffer->key;
2927 if (!key)
2928 continue;
2929 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2930 get_cmd_cache_cmd(key), buffer->val);
2931 }
2932}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002933EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002934
2935/**
2936 * snd_hda_sequence_write_cache - sequence writes with caching
2937 * @codec: the HDA codec
2938 * @seq: VERB array to send
2939 *
2940 * Send the commands sequentially from the given array.
2941 * Thte commands are recorded on cache for power-save and resume.
2942 * The array must be terminated with NID=0.
2943 */
2944void snd_hda_sequence_write_cache(struct hda_codec *codec,
2945 const struct hda_verb *seq)
2946{
2947 for (; seq->nid; seq++)
2948 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2949 seq->param);
2950}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002951EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002952#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002953
Takashi Iwai54d17402005-11-21 16:33:22 +01002954/*
2955 * set power state of the codec
2956 */
2957static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2958 unsigned int power_state)
2959{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002960 hda_nid_t nid;
2961 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002962
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002963 /* this delay seems necessary to avoid click noise at power-down */
2964 if (power_state == AC_PWRST_D3)
2965 msleep(100);
2966 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002967 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002968 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002969 if (power_state == AC_PWRST_D0 &&
2970 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002971 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002972
Takashi Iwaicb53c622007-08-10 17:21:45 +02002973 nid = codec->start_nid;
2974 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002975 unsigned int wcaps = get_wcaps(codec, nid);
2976 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002977 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002978 if (power_state == AC_PWRST_D3 &&
2979 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002980 unsigned int pincap;
2981 /*
2982 * don't power down the widget if it controls
2983 * eapd and EAPD_BTLENABLE is set.
2984 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01002985 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002986 if (pincap & AC_PINCAP_EAPD) {
2987 int eapd = snd_hda_codec_read(codec,
2988 nid, 0,
2989 AC_VERB_GET_EAPD_BTLENABLE, 0);
2990 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002991 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002992 continue;
2993 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002994 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002995 snd_hda_codec_write(codec, nid, 0,
2996 AC_VERB_SET_POWER_STATE,
2997 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002998 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002999 }
3000
Takashi Iwaicb53c622007-08-10 17:21:45 +02003001 if (power_state == AC_PWRST_D0) {
3002 unsigned long end_time;
3003 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003004 /* wait until the codec reachs to D0 */
3005 end_time = jiffies + msecs_to_jiffies(500);
3006 do {
3007 state = snd_hda_codec_read(codec, fg, 0,
3008 AC_VERB_GET_POWER_STATE, 0);
3009 if (state == power_state)
3010 break;
3011 msleep(1);
3012 } while (time_after_eq(end_time, jiffies));
3013 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003014}
3015
Takashi Iwai11aeff02008-07-30 15:01:46 +02003016#ifdef CONFIG_SND_HDA_HWDEP
3017/* execute additional init verbs */
3018static void hda_exec_init_verbs(struct hda_codec *codec)
3019{
3020 if (codec->init_verbs.list)
3021 snd_hda_sequence_write(codec, codec->init_verbs.list);
3022}
3023#else
3024static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3025#endif
3026
Takashi Iwaicb53c622007-08-10 17:21:45 +02003027#ifdef SND_HDA_NEEDS_RESUME
3028/*
3029 * call suspend and power-down; used both from PM and power-save
3030 */
3031static void hda_call_codec_suspend(struct hda_codec *codec)
3032{
3033 if (codec->patch_ops.suspend)
3034 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003035 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003036 hda_set_power_state(codec,
3037 codec->afg ? codec->afg : codec->mfg,
3038 AC_PWRST_D3);
3039#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003040 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003041 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003042 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003043 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003044 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003045#endif
3046}
3047
3048/*
3049 * kick up codec; used both from PM and power-save
3050 */
3051static void hda_call_codec_resume(struct hda_codec *codec)
3052{
3053 hda_set_power_state(codec,
3054 codec->afg ? codec->afg : codec->mfg,
3055 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003056 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003057 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003058 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003059 if (codec->patch_ops.resume)
3060 codec->patch_ops.resume(codec);
3061 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003062 if (codec->patch_ops.init)
3063 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003064 snd_hda_codec_resume_amp(codec);
3065 snd_hda_codec_resume_cache(codec);
3066 }
3067}
3068#endif /* SND_HDA_NEEDS_RESUME */
3069
Takashi Iwai54d17402005-11-21 16:33:22 +01003070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071/**
3072 * snd_hda_build_controls - build mixer controls
3073 * @bus: the BUS
3074 *
3075 * Creates mixer controls for each codec included in the bus.
3076 *
3077 * Returns 0 if successful, otherwise a negative error code.
3078 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003079int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003081 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Takashi Iwai0ba21762007-04-16 11:29:14 +02003083 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003084 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003085 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003086 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003087 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003088 err = snd_hda_codec_reset(codec);
3089 if (err < 0) {
3090 printk(KERN_ERR
3091 "hda_codec: cannot revert codec\n");
3092 return err;
3093 }
3094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003096 return 0;
3097}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003098EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003100int snd_hda_codec_build_controls(struct hda_codec *codec)
3101{
3102 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003103 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003104 /* continue to initialize... */
3105 if (codec->patch_ops.init)
3106 err = codec->patch_ops.init(codec);
3107 if (!err && codec->patch_ops.build_controls)
3108 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003109 if (err < 0)
3110 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return 0;
3112}
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114/*
3115 * stream formats
3116 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003117struct hda_rate_tbl {
3118 unsigned int hz;
3119 unsigned int alsa_bits;
3120 unsigned int hda_fmt;
3121};
3122
Takashi Iwai92f10b32010-08-03 14:21:00 +02003123/* rate = base * mult / div */
3124#define HDA_RATE(base, mult, div) \
3125 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3126 (((div) - 1) << AC_FMT_DIV_SHIFT))
3127
Takashi Iwaibefdf312005-08-22 13:57:55 +02003128static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003130
3131 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003132 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3133 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3134 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3135 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3136 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3137 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3138 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3139 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3140 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3141 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3142 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003143#define AC_PAR_PCM_RATE_BITS 11
3144 /* up to bits 10, 384kHZ isn't supported properly */
3145
3146 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003147 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003148
Takashi Iwaibefdf312005-08-22 13:57:55 +02003149 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150};
3151
3152/**
3153 * snd_hda_calc_stream_format - calculate format bitset
3154 * @rate: the sample rate
3155 * @channels: the number of channels
3156 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3157 * @maxbps: the max. bps
3158 *
3159 * Calculate the format bitset from the given rate, channels and th PCM format.
3160 *
3161 * Return zero if invalid.
3162 */
3163unsigned int snd_hda_calc_stream_format(unsigned int rate,
3164 unsigned int channels,
3165 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003166 unsigned int maxbps,
3167 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
3169 int i;
3170 unsigned int val = 0;
3171
Takashi Iwaibefdf312005-08-22 13:57:55 +02003172 for (i = 0; rate_bits[i].hz; i++)
3173 if (rate_bits[i].hz == rate) {
3174 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 break;
3176 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003177 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 snd_printdd("invalid rate %d\n", rate);
3179 return 0;
3180 }
3181
3182 if (channels == 0 || channels > 8) {
3183 snd_printdd("invalid channels %d\n", channels);
3184 return 0;
3185 }
3186 val |= channels - 1;
3187
3188 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003189 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003190 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003191 break;
3192 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003193 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 case 20:
3196 case 24:
3197 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003198 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003199 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003201 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003203 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 break;
3205 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003206 snd_printdd("invalid format width %d\n",
3207 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return 0;
3209 }
3210
Anssi Hannula32c168c2010-08-03 13:28:57 +03003211 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003212 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return val;
3215}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003216EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003218static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3219{
3220 unsigned int val = 0;
3221 if (nid != codec->afg &&
3222 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3223 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3224 if (!val || val == -1)
3225 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3226 if (!val || val == -1)
3227 return 0;
3228 return val;
3229}
3230
3231static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3232{
3233 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3234 get_pcm_param);
3235}
3236
3237static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3238{
3239 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3240 if (!streams || streams == -1)
3241 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3242 if (!streams || streams == -1)
3243 return 0;
3244 return streams;
3245}
3246
3247static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3248{
3249 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3250 get_stream_param);
3251}
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253/**
3254 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3255 * @codec: the HDA codec
3256 * @nid: NID to query
3257 * @ratesp: the pointer to store the detected rate bitflags
3258 * @formatsp: the pointer to store the detected formats
3259 * @bpsp: the pointer to store the detected format widths
3260 *
3261 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3262 * or @bsps argument is ignored.
3263 *
3264 * Returns 0 if successful, otherwise a negative error code.
3265 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003266static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3268{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003269 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003271 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003272 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 if (ratesp) {
3275 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003276 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003278 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003280 if (rates == 0) {
3281 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3282 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3283 nid, val,
3284 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3285 return -EIO;
3286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 *ratesp = rates;
3288 }
3289
3290 if (formatsp || bpsp) {
3291 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003292 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003294 streams = query_stream_param(codec, nid);
3295 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298 bps = 0;
3299 if (streams & AC_SUPFMT_PCM) {
3300 if (val & AC_SUPPCM_BITS_8) {
3301 formats |= SNDRV_PCM_FMTBIT_U8;
3302 bps = 8;
3303 }
3304 if (val & AC_SUPPCM_BITS_16) {
3305 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3306 bps = 16;
3307 }
3308 if (wcaps & AC_WCAP_DIGITAL) {
3309 if (val & AC_SUPPCM_BITS_32)
3310 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3311 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3312 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3313 if (val & AC_SUPPCM_BITS_24)
3314 bps = 24;
3315 else if (val & AC_SUPPCM_BITS_20)
3316 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003317 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3318 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3320 if (val & AC_SUPPCM_BITS_32)
3321 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 else if (val & AC_SUPPCM_BITS_24)
3323 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003324 else if (val & AC_SUPPCM_BITS_20)
3325 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 }
3327 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003328 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003330 if (!bps)
3331 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003332 }
3333 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003334 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 /* temporary hack: we have still no proper support
3336 * for the direct AC3 stream...
3337 */
3338 formats |= SNDRV_PCM_FMTBIT_U8;
3339 bps = 8;
3340 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003341 if (formats == 0) {
3342 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3343 "(nid=0x%x, val=0x%x, ovrd=%i, "
3344 "streams=0x%x)\n",
3345 nid, val,
3346 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3347 streams);
3348 return -EIO;
3349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 if (formatsp)
3351 *formatsp = formats;
3352 if (bpsp)
3353 *bpsp = bps;
3354 }
3355
3356 return 0;
3357}
3358
3359/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003360 * snd_hda_is_supported_format - Check the validity of the format
3361 * @codec: HD-audio codec
3362 * @nid: NID to check
3363 * @format: the HD-audio format value to check
3364 *
3365 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 *
3367 * Returns 1 if supported, 0 if not.
3368 */
3369int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3370 unsigned int format)
3371{
3372 int i;
3373 unsigned int val = 0, rate, stream;
3374
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003375 val = query_pcm_param(codec, nid);
3376 if (!val)
3377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
3379 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003380 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003381 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 if (val & (1 << i))
3383 break;
3384 return 0;
3385 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003386 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return 0;
3388
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003389 stream = query_stream_param(codec, nid);
3390 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 return 0;
3392
3393 if (stream & AC_SUPFMT_PCM) {
3394 switch (format & 0xf0) {
3395 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003396 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return 0;
3398 break;
3399 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003400 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return 0;
3402 break;
3403 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003404 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return 0;
3406 break;
3407 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003408 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 return 0;
3410 break;
3411 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003412 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return 0;
3414 break;
3415 default:
3416 return 0;
3417 }
3418 } else {
3419 /* FIXME: check for float32 and AC3? */
3420 }
3421
3422 return 1;
3423}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003424EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426/*
3427 * PCM stuff
3428 */
3429static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3430 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003431 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
3433 return 0;
3434}
3435
3436static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3437 struct hda_codec *codec,
3438 unsigned int stream_tag,
3439 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003440 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
3442 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3443 return 0;
3444}
3445
3446static int hda_pcm_default_cleanup(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{
Takashi Iwai888afa12008-03-18 09:57:50 +01003450 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 return 0;
3452}
3453
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003454static int set_pcm_default_values(struct hda_codec *codec,
3455 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003457 int err;
3458
Takashi Iwai0ba21762007-04-16 11:29:14 +02003459 /* query support PCM information from the given NID */
3460 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003461 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003462 info->rates ? NULL : &info->rates,
3463 info->formats ? NULL : &info->formats,
3464 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003465 if (err < 0)
3466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
3468 if (info->ops.open == NULL)
3469 info->ops.open = hda_pcm_default_open_close;
3470 if (info->ops.close == NULL)
3471 info->ops.close = hda_pcm_default_open_close;
3472 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003473 if (snd_BUG_ON(!info->nid))
3474 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 info->ops.prepare = hda_pcm_default_prepare;
3476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003478 if (snd_BUG_ON(!info->nid))
3479 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 info->ops.cleanup = hda_pcm_default_cleanup;
3481 }
3482 return 0;
3483}
3484
Takashi Iwaieb541332010-08-06 13:48:11 +02003485/*
3486 * codec prepare/cleanup entries
3487 */
3488int snd_hda_codec_prepare(struct hda_codec *codec,
3489 struct hda_pcm_stream *hinfo,
3490 unsigned int stream,
3491 unsigned int format,
3492 struct snd_pcm_substream *substream)
3493{
3494 int ret;
3495 mutex_lock(&codec->prepare_mutex);
3496 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3497 if (ret >= 0)
3498 purify_inactive_streams(codec);
3499 mutex_unlock(&codec->prepare_mutex);
3500 return ret;
3501}
3502EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3503
3504void snd_hda_codec_cleanup(struct hda_codec *codec,
3505 struct hda_pcm_stream *hinfo,
3506 struct snd_pcm_substream *substream)
3507{
3508 mutex_lock(&codec->prepare_mutex);
3509 hinfo->ops.cleanup(hinfo, codec, substream);
3510 mutex_unlock(&codec->prepare_mutex);
3511}
3512EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3513
Takashi Iwaid5191e52009-11-16 14:58:17 +01003514/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003515const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3516 "Audio", "SPDIF", "HDMI", "Modem"
3517};
3518
Takashi Iwai176d5332008-07-30 15:01:44 +02003519/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003520 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003521 *
3522 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003523 */
3524static int get_empty_pcm_device(struct hda_bus *bus, int type)
3525{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003526 /* audio device indices; not linear to keep compatibility */
3527 static int audio_idx[HDA_PCM_NTYPES][5] = {
3528 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3529 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003530 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003531 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003532 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003533 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003534
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003535 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003536 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3537 return -EINVAL;
3538 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003539
3540 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3541 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3542 return audio_idx[type][i];
3543
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003544 snd_printk(KERN_WARNING "Too many %s devices\n",
3545 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003546 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003547}
3548
3549/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003550 * attach a new PCM stream
3551 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003552static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003553{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003554 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003555 struct hda_pcm_stream *info;
3556 int stream, err;
3557
Takashi Iwaib91f0802008-11-04 08:43:08 +01003558 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003559 return -EINVAL;
3560 for (stream = 0; stream < 2; stream++) {
3561 info = &pcm->stream[stream];
3562 if (info->substreams) {
3563 err = set_pcm_default_values(codec, info);
3564 if (err < 0)
3565 return err;
3566 }
3567 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003568 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003569}
3570
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003571/* assign all PCMs of the given codec */
3572int snd_hda_codec_build_pcms(struct hda_codec *codec)
3573{
3574 unsigned int pcm;
3575 int err;
3576
3577 if (!codec->num_pcms) {
3578 if (!codec->patch_ops.build_pcms)
3579 return 0;
3580 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003581 if (err < 0) {
3582 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003583 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003584 err = snd_hda_codec_reset(codec);
3585 if (err < 0) {
3586 printk(KERN_ERR
3587 "hda_codec: cannot revert codec\n");
3588 return err;
3589 }
3590 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003591 }
3592 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3593 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3594 int dev;
3595
3596 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003597 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003598
3599 if (!cpcm->pcm) {
3600 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3601 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003602 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003603 cpcm->device = dev;
3604 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003605 if (err < 0) {
3606 printk(KERN_ERR "hda_codec: cannot attach "
3607 "PCM stream %d for codec #%d\n",
3608 dev, codec->addr);
3609 continue; /* no fatal error */
3610 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003611 }
3612 }
3613 return 0;
3614}
3615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616/**
3617 * snd_hda_build_pcms - build PCM information
3618 * @bus: the BUS
3619 *
3620 * Create PCM information for each codec included in the bus.
3621 *
3622 * The build_pcms codec patch is requested to set up codec->num_pcms and
3623 * codec->pcm_info properly. The array is referred by the top-level driver
3624 * to create its PCM instances.
3625 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3626 * callback.
3627 *
3628 * At least, substreams, channels_min and channels_max must be filled for
3629 * each stream. substreams = 0 indicates that the stream doesn't exist.
3630 * When rates and/or formats are zero, the supported values are queried
3631 * from the given nid. The nid is used also by the default ops.prepare
3632 * and ops.cleanup callbacks.
3633 *
3634 * The driver needs to call ops.open in its open callback. Similarly,
3635 * ops.close is supposed to be called in the close callback.
3636 * ops.prepare should be called in the prepare or hw_params callback
3637 * with the proper parameters for set up.
3638 * ops.cleanup should be called in hw_free for clean up of streams.
3639 *
3640 * This function returns 0 if successfull, or a negative error code.
3641 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003642int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003644 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Takashi Iwai0ba21762007-04-16 11:29:14 +02003646 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003647 int err = snd_hda_codec_build_pcms(codec);
3648 if (err < 0)
3649 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
3651 return 0;
3652}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003653EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655/**
3656 * snd_hda_check_board_config - compare the current codec with the config table
3657 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003658 * @num_configs: number of config enums
3659 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 * @tbl: configuration table, terminated by null entries
3661 *
3662 * Compares the modelname or PCI subsystem id of the current codec with the
3663 * given configuration table. If a matching entry is found, returns its
3664 * config value (supposed to be 0 or positive).
3665 *
3666 * If no entries are matching, the function returns a negative value.
3667 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003668int snd_hda_check_board_config(struct hda_codec *codec,
3669 int num_configs, const char **models,
3670 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003672 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003673 int i;
3674 for (i = 0; i < num_configs; i++) {
3675 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003676 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003677 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3678 "selected\n", models[i]);
3679 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 }
3681 }
3682 }
3683
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003684 if (!codec->bus->pci || !tbl)
3685 return -1;
3686
3687 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3688 if (!tbl)
3689 return -1;
3690 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003691#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003692 char tmp[10];
3693 const char *model = NULL;
3694 if (models)
3695 model = models[tbl->value];
3696 if (!model) {
3697 sprintf(tmp, "#%d", tbl->value);
3698 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003700 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3701 "for config %x:%x (%s)\n",
3702 model, tbl->subvendor, tbl->subdevice,
3703 (tbl->name ? tbl->name : "Unknown device"));
3704#endif
3705 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 }
3707 return -1;
3708}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003709EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003712 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003713 subsystem ID with the
3714 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003715
3716 This is important for Gateway notebooks with SB450 HDA Audio
3717 where the vendor ID of the PCI device is:
3718 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3719 and the vendor/subvendor are found only at the codec.
3720
3721 * @codec: the HDA codec
3722 * @num_configs: number of config enums
3723 * @models: array of model name strings
3724 * @tbl: configuration table, terminated by null entries
3725 *
3726 * Compares the modelname or PCI subsystem id of the current codec with the
3727 * given configuration table. If a matching entry is found, returns its
3728 * config value (supposed to be 0 or positive).
3729 *
3730 * If no entries are matching, the function returns a negative value.
3731 */
3732int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3733 int num_configs, const char **models,
3734 const struct snd_pci_quirk *tbl)
3735{
3736 const struct snd_pci_quirk *q;
3737
3738 /* Search for codec ID */
3739 for (q = tbl; q->subvendor; q++) {
3740 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3741
3742 if (vendorid == codec->subsystem_id)
3743 break;
3744 }
3745
3746 if (!q->subvendor)
3747 return -1;
3748
3749 tbl = q;
3750
3751 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003752#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003753 char tmp[10];
3754 const char *model = NULL;
3755 if (models)
3756 model = models[tbl->value];
3757 if (!model) {
3758 sprintf(tmp, "#%d", tbl->value);
3759 model = tmp;
3760 }
3761 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3762 "for config %x:%x (%s)\n",
3763 model, tbl->subvendor, tbl->subdevice,
3764 (tbl->name ? tbl->name : "Unknown device"));
3765#endif
3766 return tbl->value;
3767 }
3768 return -1;
3769}
3770EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3771
3772/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 * snd_hda_add_new_ctls - create controls from the array
3774 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003775 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 *
3777 * This helper function creates and add new controls in the given array.
3778 * The array must be terminated with an empty entry as terminator.
3779 *
3780 * Returns 0 if successful, or a negative error code.
3781 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003782int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003784 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
3786 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003787 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003788 if (knew->iface == -1) /* skip this codec private value */
3789 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003790 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003791 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003792 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003793 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003794 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003795 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003796 return err;
3797 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003798 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003799 return -ENOMEM;
3800 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003801 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003802 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003803 return err;
3804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
3806 return 0;
3807}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003808EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Takashi Iwaicb53c622007-08-10 17:21:45 +02003810#ifdef CONFIG_SND_HDA_POWER_SAVE
3811static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3812 unsigned int power_state);
3813
3814static void hda_power_work(struct work_struct *work)
3815{
3816 struct hda_codec *codec =
3817 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003818 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003819
Maxim Levitsky2e492462007-09-03 15:26:57 +02003820 if (!codec->power_on || codec->power_count) {
3821 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003822 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003823 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003824
3825 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003826 if (bus->ops.pm_notify)
3827 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003828}
3829
3830static void hda_keep_power_on(struct hda_codec *codec)
3831{
3832 codec->power_count++;
3833 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003834 codec->power_jiffies = jiffies;
3835}
3836
Takashi Iwaid5191e52009-11-16 14:58:17 +01003837/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003838void snd_hda_update_power_acct(struct hda_codec *codec)
3839{
3840 unsigned long delta = jiffies - codec->power_jiffies;
3841 if (codec->power_on)
3842 codec->power_on_acct += delta;
3843 else
3844 codec->power_off_acct += delta;
3845 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003846}
3847
Takashi Iwaid5191e52009-11-16 14:58:17 +01003848/**
3849 * snd_hda_power_up - Power-up the codec
3850 * @codec: HD-audio codec
3851 *
3852 * Increment the power-up counter and power up the hardware really when
3853 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003854 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003855void snd_hda_power_up(struct hda_codec *codec)
3856{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003857 struct hda_bus *bus = codec->bus;
3858
Takashi Iwaicb53c622007-08-10 17:21:45 +02003859 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003860 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003861 return;
3862
Takashi Iwaia2f63092009-11-11 09:34:25 +01003863 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003864 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003865 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003866 if (bus->ops.pm_notify)
3867 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003868 hda_call_codec_resume(codec);
3869 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003870 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003871}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003872EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003873
3874#define power_save(codec) \
3875 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003876
Takashi Iwaid5191e52009-11-16 14:58:17 +01003877/**
3878 * snd_hda_power_down - Power-down the codec
3879 * @codec: HD-audio codec
3880 *
3881 * Decrement the power-up counter and schedules the power-off work if
3882 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003883 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003884void snd_hda_power_down(struct hda_codec *codec)
3885{
3886 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003887 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003888 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003889 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003890 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003891 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003892 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003893 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003894}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003895EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003896
Takashi Iwaid5191e52009-11-16 14:58:17 +01003897/**
3898 * snd_hda_check_amp_list_power - Check the amp list and update the power
3899 * @codec: HD-audio codec
3900 * @check: the object containing an AMP list and the status
3901 * @nid: NID to check / update
3902 *
3903 * Check whether the given NID is in the amp list. If it's in the list,
3904 * check the current AMP status, and update the the power-status according
3905 * to the mute status.
3906 *
3907 * This function is supposed to be set or called from the check_power_status
3908 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003909 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003910int snd_hda_check_amp_list_power(struct hda_codec *codec,
3911 struct hda_loopback_check *check,
3912 hda_nid_t nid)
3913{
3914 struct hda_amp_list *p;
3915 int ch, v;
3916
3917 if (!check->amplist)
3918 return 0;
3919 for (p = check->amplist; p->nid; p++) {
3920 if (p->nid == nid)
3921 break;
3922 }
3923 if (!p->nid)
3924 return 0; /* nothing changed */
3925
3926 for (p = check->amplist; p->nid; p++) {
3927 for (ch = 0; ch < 2; ch++) {
3928 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3929 p->idx);
3930 if (!(v & HDA_AMP_MUTE) && v > 0) {
3931 if (!check->power_on) {
3932 check->power_on = 1;
3933 snd_hda_power_up(codec);
3934 }
3935 return 1;
3936 }
3937 }
3938 }
3939 if (check->power_on) {
3940 check->power_on = 0;
3941 snd_hda_power_down(codec);
3942 }
3943 return 0;
3944}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003945EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003946#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003948/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003949 * Channel mode helper
3950 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003951
3952/**
3953 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3954 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003955int snd_hda_ch_mode_info(struct hda_codec *codec,
3956 struct snd_ctl_elem_info *uinfo,
3957 const struct hda_channel_mode *chmode,
3958 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003959{
3960 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3961 uinfo->count = 1;
3962 uinfo->value.enumerated.items = num_chmodes;
3963 if (uinfo->value.enumerated.item >= num_chmodes)
3964 uinfo->value.enumerated.item = num_chmodes - 1;
3965 sprintf(uinfo->value.enumerated.name, "%dch",
3966 chmode[uinfo->value.enumerated.item].channels);
3967 return 0;
3968}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003969EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003970
Takashi Iwaid5191e52009-11-16 14:58:17 +01003971/**
3972 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3973 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003974int snd_hda_ch_mode_get(struct hda_codec *codec,
3975 struct snd_ctl_elem_value *ucontrol,
3976 const struct hda_channel_mode *chmode,
3977 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003978 int max_channels)
3979{
3980 int i;
3981
3982 for (i = 0; i < num_chmodes; i++) {
3983 if (max_channels == chmode[i].channels) {
3984 ucontrol->value.enumerated.item[0] = i;
3985 break;
3986 }
3987 }
3988 return 0;
3989}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003990EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003991
Takashi Iwaid5191e52009-11-16 14:58:17 +01003992/**
3993 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3994 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003995int snd_hda_ch_mode_put(struct hda_codec *codec,
3996 struct snd_ctl_elem_value *ucontrol,
3997 const struct hda_channel_mode *chmode,
3998 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003999 int *max_channelsp)
4000{
4001 unsigned int mode;
4002
4003 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004004 if (mode >= num_chmodes)
4005 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004006 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004007 return 0;
4008 /* change the current channel setting */
4009 *max_channelsp = chmode[mode].channels;
4010 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004011 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004012 return 1;
4013}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004014EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016/*
4017 * input MUX helper
4018 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004019
4020/**
4021 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4022 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004023int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4024 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
4026 unsigned int index;
4027
4028 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4029 uinfo->count = 1;
4030 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004031 if (!imux->num_items)
4032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 index = uinfo->value.enumerated.item;
4034 if (index >= imux->num_items)
4035 index = imux->num_items - 1;
4036 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4037 return 0;
4038}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004039EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Takashi Iwaid5191e52009-11-16 14:58:17 +01004041/**
4042 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4043 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004044int snd_hda_input_mux_put(struct hda_codec *codec,
4045 const struct hda_input_mux *imux,
4046 struct snd_ctl_elem_value *ucontrol,
4047 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 unsigned int *cur_val)
4049{
4050 unsigned int idx;
4051
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004052 if (!imux->num_items)
4053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 idx = ucontrol->value.enumerated.item[0];
4055 if (idx >= imux->num_items)
4056 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004057 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004059 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4060 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 *cur_val = idx;
4062 return 1;
4063}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004064EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
4066
4067/*
4068 * Multi-channel / digital-out PCM helper functions
4069 */
4070
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004071/* setup SPDIF output stream */
4072static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4073 unsigned int stream_tag, unsigned int format)
4074{
4075 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02004076 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004077 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02004078 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4079 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004080 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004081 if (codec->slave_dig_outs) {
4082 hda_nid_t *d;
4083 for (d = codec->slave_dig_outs; *d; d++)
4084 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4085 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004086 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004087 /* turn on again (if needed) */
4088 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4089 set_dig_out_convert(codec, nid,
4090 codec->spdif_ctls & 0xff, -1);
4091}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004092
Takashi Iwai2f728532008-09-25 16:32:41 +02004093static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4094{
4095 snd_hda_codec_cleanup_stream(codec, nid);
4096 if (codec->slave_dig_outs) {
4097 hda_nid_t *d;
4098 for (d = codec->slave_dig_outs; *d; d++)
4099 snd_hda_codec_cleanup_stream(codec, *d);
4100 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004101}
4102
Takashi Iwaid5191e52009-11-16 14:58:17 +01004103/**
4104 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4105 * @bus: HD-audio bus
4106 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004107void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4108{
4109 struct hda_codec *codec;
4110
4111 if (!bus)
4112 return;
4113 list_for_each_entry(codec, &bus->codec_list, list) {
4114#ifdef CONFIG_SND_HDA_POWER_SAVE
4115 if (!codec->power_on)
4116 continue;
4117#endif
4118 if (codec->patch_ops.reboot_notify)
4119 codec->patch_ops.reboot_notify(codec);
4120 }
4121}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004122EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004123
Takashi Iwaid5191e52009-11-16 14:58:17 +01004124/**
4125 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004127int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4128 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
Ingo Molnar62932df2006-01-16 16:34:20 +01004130 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004131 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4132 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004133 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004135 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return 0;
4137}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004138EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Takashi Iwaid5191e52009-11-16 14:58:17 +01004140/**
4141 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4142 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004143int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4144 struct hda_multi_out *mout,
4145 unsigned int stream_tag,
4146 unsigned int format,
4147 struct snd_pcm_substream *substream)
4148{
4149 mutex_lock(&codec->spdif_mutex);
4150 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4151 mutex_unlock(&codec->spdif_mutex);
4152 return 0;
4153}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004154EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004155
Takashi Iwaid5191e52009-11-16 14:58:17 +01004156/**
4157 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4158 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004159int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4160 struct hda_multi_out *mout)
4161{
4162 mutex_lock(&codec->spdif_mutex);
4163 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4164 mutex_unlock(&codec->spdif_mutex);
4165 return 0;
4166}
4167EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4168
Takashi Iwaid5191e52009-11-16 14:58:17 +01004169/**
4170 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004172int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4173 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
Ingo Molnar62932df2006-01-16 16:34:20 +01004175 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004177 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 return 0;
4179}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004180EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Takashi Iwaid5191e52009-11-16 14:58:17 +01004182/**
4183 * snd_hda_multi_out_analog_open - open analog outputs
4184 *
4185 * Open analog outputs and set up the hw-constraints.
4186 * If the digital outputs can be opened as slave, open the digital
4187 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004189int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4190 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004191 struct snd_pcm_substream *substream,
4192 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
Takashi Iwai9a081602008-02-12 18:37:26 +01004194 struct snd_pcm_runtime *runtime = substream->runtime;
4195 runtime->hw.channels_max = mout->max_channels;
4196 if (mout->dig_out_nid) {
4197 if (!mout->analog_rates) {
4198 mout->analog_rates = hinfo->rates;
4199 mout->analog_formats = hinfo->formats;
4200 mout->analog_maxbps = hinfo->maxbps;
4201 } else {
4202 runtime->hw.rates = mout->analog_rates;
4203 runtime->hw.formats = mout->analog_formats;
4204 hinfo->maxbps = mout->analog_maxbps;
4205 }
4206 if (!mout->spdif_rates) {
4207 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4208 &mout->spdif_rates,
4209 &mout->spdif_formats,
4210 &mout->spdif_maxbps);
4211 }
4212 mutex_lock(&codec->spdif_mutex);
4213 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004214 if ((runtime->hw.rates & mout->spdif_rates) &&
4215 (runtime->hw.formats & mout->spdif_formats)) {
4216 runtime->hw.rates &= mout->spdif_rates;
4217 runtime->hw.formats &= mout->spdif_formats;
4218 if (mout->spdif_maxbps < hinfo->maxbps)
4219 hinfo->maxbps = mout->spdif_maxbps;
4220 } else {
4221 mout->share_spdif = 0;
4222 /* FIXME: need notify? */
4223 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004224 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004225 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4228 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4229}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004230EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Takashi Iwaid5191e52009-11-16 14:58:17 +01004232/**
4233 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4234 *
4235 * Set up the i/o for analog out.
4236 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004238int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4239 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 unsigned int stream_tag,
4241 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004242 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
4244 hda_nid_t *nids = mout->dac_nids;
4245 int chs = substream->runtime->channels;
4246 int i;
4247
Ingo Molnar62932df2006-01-16 16:34:20 +01004248 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004249 if (mout->dig_out_nid && mout->share_spdif &&
4250 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004252 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4253 format) &&
4254 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004256 setup_dig_out_stream(codec, mout->dig_out_nid,
4257 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 } else {
4259 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004260 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 }
4262 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004263 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004266 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4267 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004268 if (!mout->no_share_stream &&
4269 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004271 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4272 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004273 /* extra outputs copied from front */
4274 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004275 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004276 snd_hda_codec_setup_stream(codec,
4277 mout->extra_out_nid[i],
4278 stream_tag, 0, format);
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 /* surrounds */
4281 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004282 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004283 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4284 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004285 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004286 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4287 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 }
4289 return 0;
4290}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004291EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Takashi Iwaid5191e52009-11-16 14:58:17 +01004293/**
4294 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004296int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4297 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298{
4299 hda_nid_t *nids = mout->dac_nids;
4300 int i;
4301
4302 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004303 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004305 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004306 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4307 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004308 snd_hda_codec_cleanup_stream(codec,
4309 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004310 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004312 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 mout->dig_out_used = 0;
4314 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004315 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 return 0;
4317}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004318EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004320/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004321 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004322 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004323
Takashi Iwai12f288b2007-08-02 15:51:59 +02004324static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004325{
4326 for (; *list; list++)
4327 if (*list == nid)
4328 return 1;
4329 return 0;
4330}
4331
Steve Longerbeam81937d32007-05-08 15:33:03 +02004332
4333/*
4334 * Sort an associated group of pins according to their sequence numbers.
4335 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004336static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004337 int num_pins)
4338{
4339 int i, j;
4340 short seq;
4341 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004342
Steve Longerbeam81937d32007-05-08 15:33:03 +02004343 for (i = 0; i < num_pins; i++) {
4344 for (j = i + 1; j < num_pins; j++) {
4345 if (sequences[i] > sequences[j]) {
4346 seq = sequences[i];
4347 sequences[i] = sequences[j];
4348 sequences[j] = seq;
4349 nid = pins[i];
4350 pins[i] = pins[j];
4351 pins[j] = nid;
4352 }
4353 }
4354 }
4355}
4356
4357
Takashi Iwai82bc9552006-03-21 11:24:42 +01004358/*
4359 * Parse all pin widgets and store the useful pin nids to cfg
4360 *
4361 * The number of line-outs or any primary output is stored in line_outs,
4362 * and the corresponding output pins are assigned to line_out_pins[],
4363 * in the order of front, rear, CLFE, side, ...
4364 *
4365 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004366 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004367 * is detected, one of speaker of HP pins is assigned as the primary
4368 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4369 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004370 *
Takashi Iwai82bc9552006-03-21 11:24:42 +01004371 * The analog input pins are assigned to input_pins array.
4372 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4373 * respectively.
4374 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004375int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4376 struct auto_pin_cfg *cfg,
4377 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004378{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004379 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004380 short seq, assoc_line_out, assoc_speaker;
4381 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4382 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004383 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004384
4385 memset(cfg, 0, sizeof(*cfg));
4386
Steve Longerbeam81937d32007-05-08 15:33:03 +02004387 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4388 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004389 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004390 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004391
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004392 end_nid = codec->start_nid + codec->num_nodes;
4393 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004394 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004395 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004396 unsigned int def_conf;
4397 short assoc, loc;
4398
4399 /* read all default configuration for pin complex */
4400 if (wid_type != AC_WID_PIN)
4401 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004402 /* ignore the given nids (e.g. pc-beep returns error) */
4403 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4404 continue;
4405
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004406 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004407 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4408 continue;
4409 loc = get_defcfg_location(def_conf);
4410 switch (get_defcfg_device(def_conf)) {
4411 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004412 seq = get_defcfg_sequence(def_conf);
4413 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004414
4415 if (!(wid_caps & AC_WCAP_STEREO))
4416 if (!cfg->mono_out_pin)
4417 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004418 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004419 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004420 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004421 assoc_line_out = assoc;
4422 else if (assoc_line_out != assoc)
4423 continue;
4424 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4425 continue;
4426 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004427 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004428 cfg->line_outs++;
4429 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004430 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004431 seq = get_defcfg_sequence(def_conf);
4432 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004433 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004434 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004435 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004436 assoc_speaker = assoc;
4437 else if (assoc_speaker != assoc)
4438 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004439 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4440 continue;
4441 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004442 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004443 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004444 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004445 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004446 seq = get_defcfg_sequence(def_conf);
4447 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004448 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4449 continue;
4450 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004451 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004452 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004453 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004454 case AC_JACK_MIC_IN: {
4455 int preferred, alt;
Kailang Yang6ff86a32010-03-19 11:14:36 +01004456 if (loc == AC_JACK_LOC_FRONT ||
4457 (loc & 0x30) == AC_JACK_LOC_INTERNAL) {
Takashi Iwai314634b2006-09-21 11:56:18 +02004458 preferred = AUTO_PIN_FRONT_MIC;
4459 alt = AUTO_PIN_MIC;
4460 } else {
4461 preferred = AUTO_PIN_MIC;
4462 alt = AUTO_PIN_FRONT_MIC;
4463 }
4464 if (!cfg->input_pins[preferred])
4465 cfg->input_pins[preferred] = nid;
4466 else if (!cfg->input_pins[alt])
4467 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004468 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004469 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004470 case AC_JACK_LINE_IN:
4471 if (loc == AC_JACK_LOC_FRONT)
4472 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4473 else
4474 cfg->input_pins[AUTO_PIN_LINE] = nid;
4475 break;
4476 case AC_JACK_CD:
4477 cfg->input_pins[AUTO_PIN_CD] = nid;
4478 break;
4479 case AC_JACK_AUX:
4480 cfg->input_pins[AUTO_PIN_AUX] = nid;
4481 break;
4482 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004483 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004484 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4485 continue;
4486 cfg->dig_out_pins[cfg->dig_outs] = nid;
4487 cfg->dig_out_type[cfg->dig_outs] =
4488 (loc == AC_JACK_LOC_HDMI) ?
4489 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4490 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004491 break;
4492 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004493 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004494 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004495 if (loc == AC_JACK_LOC_HDMI)
4496 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4497 else
4498 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004499 break;
4500 }
4501 }
4502
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004503 /* FIX-UP:
4504 * If no line-out is defined but multiple HPs are found,
4505 * some of them might be the real line-outs.
4506 */
4507 if (!cfg->line_outs && cfg->hp_outs > 1) {
4508 int i = 0;
4509 while (i < cfg->hp_outs) {
4510 /* The real HPs should have the sequence 0x0f */
4511 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4512 i++;
4513 continue;
4514 }
4515 /* Move it to the line-out table */
4516 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4517 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4518 cfg->line_outs++;
4519 cfg->hp_outs--;
4520 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4521 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4522 memmove(sequences_hp + i - 1, sequences_hp + i,
4523 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4524 }
4525 }
4526
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004527 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004528 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4529 cfg->line_outs);
4530 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4531 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004532 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4533 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004534
Takashi Iwaif889fa92007-10-31 15:49:32 +01004535 /* if we have only one mic, make it AUTO_PIN_MIC */
4536 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4537 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4538 cfg->input_pins[AUTO_PIN_MIC] =
4539 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4540 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4541 }
4542 /* ditto for line-in */
4543 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4544 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4545 cfg->input_pins[AUTO_PIN_LINE] =
4546 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4547 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4548 }
4549
Steve Longerbeam81937d32007-05-08 15:33:03 +02004550 /*
4551 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4552 * as a primary output
4553 */
4554 if (!cfg->line_outs) {
4555 if (cfg->speaker_outs) {
4556 cfg->line_outs = cfg->speaker_outs;
4557 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4558 sizeof(cfg->speaker_pins));
4559 cfg->speaker_outs = 0;
4560 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4561 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4562 } else if (cfg->hp_outs) {
4563 cfg->line_outs = cfg->hp_outs;
4564 memcpy(cfg->line_out_pins, cfg->hp_pins,
4565 sizeof(cfg->hp_pins));
4566 cfg->hp_outs = 0;
4567 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4568 cfg->line_out_type = AUTO_PIN_HP_OUT;
4569 }
4570 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004571
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004572 /* Reorder the surround channels
4573 * ALSA sequence is front/surr/clfe/side
4574 * HDA sequence is:
4575 * 4-ch: front/surr => OK as it is
4576 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004577 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004578 */
4579 switch (cfg->line_outs) {
4580 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004581 case 4:
4582 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004583 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004584 cfg->line_out_pins[2] = nid;
4585 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004586 }
4587
Takashi Iwai82bc9552006-03-21 11:24:42 +01004588 /*
4589 * debug prints of the parsed results
4590 */
4591 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4592 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4593 cfg->line_out_pins[2], cfg->line_out_pins[3],
4594 cfg->line_out_pins[4]);
4595 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4596 cfg->speaker_outs, cfg->speaker_pins[0],
4597 cfg->speaker_pins[1], cfg->speaker_pins[2],
4598 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004599 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4600 cfg->hp_outs, cfg->hp_pins[0],
4601 cfg->hp_pins[1], cfg->hp_pins[2],
4602 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004603 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004604 if (cfg->dig_outs)
4605 snd_printd(" dig-out=0x%x/0x%x\n",
4606 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004607 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4608 " cd=0x%x, aux=0x%x\n",
4609 cfg->input_pins[AUTO_PIN_MIC],
4610 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4611 cfg->input_pins[AUTO_PIN_LINE],
4612 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4613 cfg->input_pins[AUTO_PIN_CD],
4614 cfg->input_pins[AUTO_PIN_AUX]);
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004615 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004616 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004617
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004618 return 0;
4619}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004620EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004621
Takashi Iwai4a471b72005-12-07 13:56:29 +01004622/* labels for input pins */
4623const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4624 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4625};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004626EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01004627
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629#ifdef CONFIG_PM
4630/*
4631 * power management
4632 */
4633
4634/**
4635 * snd_hda_suspend - suspend the codecs
4636 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 *
4638 * Returns 0 if successful.
4639 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004640int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004642 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
Takashi Iwai0ba21762007-04-16 11:29:14 +02004644 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004645#ifdef CONFIG_SND_HDA_POWER_SAVE
4646 if (!codec->power_on)
4647 continue;
4648#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004649 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 }
4651 return 0;
4652}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004653EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
4655/**
4656 * snd_hda_resume - resume the codecs
4657 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 *
4659 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004660 *
4661 * This fucntion is defined only when POWER_SAVE isn't set.
4662 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 */
4664int snd_hda_resume(struct hda_bus *bus)
4665{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004666 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Takashi Iwai0ba21762007-04-16 11:29:14 +02004668 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004669 if (snd_hda_codec_needs_resume(codec))
4670 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 return 0;
4673}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004674EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004675#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004676
4677/*
4678 * generic arrays
4679 */
4680
Takashi Iwaid5191e52009-11-16 14:58:17 +01004681/**
4682 * snd_array_new - get a new element from the given array
4683 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004684 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004685 * Get a new element from the given array. If it exceeds the
4686 * pre-allocated array size, re-allocate the array.
4687 *
4688 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004689 */
4690void *snd_array_new(struct snd_array *array)
4691{
4692 if (array->used >= array->alloced) {
4693 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004694 void *nlist;
4695 if (snd_BUG_ON(num >= 4096))
4696 return NULL;
4697 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004698 if (!nlist)
4699 return NULL;
4700 if (array->list) {
4701 memcpy(nlist, array->list,
4702 array->elem_size * array->alloced);
4703 kfree(array->list);
4704 }
4705 array->list = nlist;
4706 array->alloced = num;
4707 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004708 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004709}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004710EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004711
Takashi Iwaid5191e52009-11-16 14:58:17 +01004712/**
4713 * snd_array_free - free the given array elements
4714 * @array: the array object
4715 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004716void snd_array_free(struct snd_array *array)
4717{
4718 kfree(array->list);
4719 array->used = 0;
4720 array->alloced = 0;
4721 array->list = NULL;
4722}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004723EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004724
Takashi Iwaid5191e52009-11-16 14:58:17 +01004725/**
4726 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4727 * @pcm: PCM caps bits
4728 * @buf: the string buffer to write
4729 * @buflen: the max buffer length
4730 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004731 * used by hda_proc.c and hda_eld.c
4732 */
4733void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4734{
4735 static unsigned int rates[] = {
4736 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4737 96000, 176400, 192000, 384000
4738 };
4739 int i, j;
4740
4741 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4742 if (pcm & (1 << i))
4743 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4744
4745 buf[j] = '\0'; /* necessary when j == 0 */
4746}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004747EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004748
Takashi Iwaid5191e52009-11-16 14:58:17 +01004749/**
4750 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4751 * @pcm: PCM caps bits
4752 * @buf: the string buffer to write
4753 * @buflen: the max buffer length
4754 *
4755 * used by hda_proc.c and hda_eld.c
4756 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004757void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4758{
4759 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4760 int i, j;
4761
4762 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4763 if (pcm & (AC_SUPPCM_BITS_8 << i))
4764 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4765
4766 buf[j] = '\0'; /* necessary when j == 0 */
4767}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004768EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004769
4770MODULE_DESCRIPTION("HDA codec core");
4771MODULE_LICENSE("GPL");