blob: 13c1e7703c49f52430dd346c2c6928e4e66363d1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
32#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020033#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020034#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * vendor / preset table
38 */
39
40struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
43};
44
45/* codec vendor labels */
46static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010047 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020048 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020049 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010050 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010051 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010052 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020053 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010054 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020055 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010056 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020057 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020059 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010060 { 0x17e8, "Chrontel" },
61 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000062 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010064 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020065 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 {} /* terminator */
67};
68
Takashi Iwai1289e9e2008-11-27 15:47:11 +010069static DEFINE_MUTEX(preset_mutex);
70static LIST_HEAD(hda_preset_tables);
71
72int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
73{
74 mutex_lock(&preset_mutex);
75 list_add_tail(&preset->list, &hda_preset_tables);
76 mutex_unlock(&preset_mutex);
77 return 0;
78}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010079EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010080
81int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
82{
83 mutex_lock(&preset_mutex);
84 list_del(&preset->list);
85 mutex_unlock(&preset_mutex);
86 return 0;
87}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010088EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwaicb53c622007-08-10 17:21:45 +020090#ifdef CONFIG_SND_HDA_POWER_SAVE
91static void hda_power_work(struct work_struct *work);
92static void hda_keep_power_on(struct hda_codec *codec);
93#else
94static inline void hda_keep_power_on(struct hda_codec *codec) {}
95#endif
96
Takashi Iwaid5191e52009-11-16 14:58:17 +010097/**
98 * snd_hda_get_jack_location - Give a location string of the jack
99 * @cfg: pin default config value
100 *
101 * Parse the pin default config value and returns the string of the
102 * jack location, e.g. "Rear", "Front", etc.
103 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400104const char *snd_hda_get_jack_location(u32 cfg)
105{
106 static char *bases[7] = {
107 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
108 };
109 static unsigned char specials_idx[] = {
110 0x07, 0x08,
111 0x17, 0x18, 0x19,
112 0x37, 0x38
113 };
114 static char *specials[] = {
115 "Rear Panel", "Drive Bar",
116 "Riser", "HDMI", "ATAPI",
117 "Mobile-In", "Mobile-Out"
118 };
119 int i;
120 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
121 if ((cfg & 0x0f) < 7)
122 return bases[cfg & 0x0f];
123 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
124 if (cfg == specials_idx[i])
125 return specials[i];
126 }
127 return "UNKNOWN";
128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100129EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400130
Takashi Iwaid5191e52009-11-16 14:58:17 +0100131/**
132 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
133 * @cfg: pin default config value
134 *
135 * Parse the pin default config value and returns the string of the
136 * jack connectivity, i.e. external or internal connection.
137 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400138const char *snd_hda_get_jack_connectivity(u32 cfg)
139{
140 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
141
142 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100144EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400145
Takashi Iwaid5191e52009-11-16 14:58:17 +0100146/**
147 * snd_hda_get_jack_type - Give a type string of the jack
148 * @cfg: pin default config value
149 *
150 * Parse the pin default config value and returns the string of the
151 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
152 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400153const char *snd_hda_get_jack_type(u32 cfg)
154{
155 static char *jack_types[16] = {
156 "Line Out", "Speaker", "HP Out", "CD",
157 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
158 "Line In", "Aux", "Mic", "Telephony",
159 "SPDIF In", "Digitial In", "Reserved", "Other"
160 };
161
162 return jack_types[(cfg & AC_DEFCFG_DEVICE)
163 >> AC_DEFCFG_DEVICE_SHIFT];
164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100165EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400166
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100167/*
168 * Compose a 32bit command word to be sent to the HD-audio controller
169 */
170static inline unsigned int
171make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
172 unsigned int verb, unsigned int parm)
173{
174 u32 val;
175
Takashi Iwai82e1b802009-07-17 12:47:34 +0200176 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
177 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800178 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
179 codec->addr, direct, nid, verb, parm);
180 return ~0;
181 }
182
183 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184 val |= (u32)direct << 27;
185 val |= (u32)nid << 20;
186 val |= verb << 8;
187 val |= parm;
188 return val;
189}
190
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200191/*
192 * Send and receive a verb
193 */
194static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
195 unsigned int *res)
196{
197 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200198 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200199
Wu Fengguang6430aee2009-07-17 16:49:19 +0800200 if (cmd == ~0)
201 return -1;
202
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200203 if (res)
204 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200205 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206 snd_hda_power_up(codec);
207 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200209 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800210 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200211 mutex_unlock(&bus->cmd_mutex);
212 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200213 if (res && *res == -1 && bus->rirb_error) {
214 if (bus->response_reset) {
215 snd_printd("hda_codec: resetting BUS due to "
216 "fatal communication error\n");
217 bus->ops.bus_reset(bus);
218 }
219 goto again;
220 }
221 /* clear reset-flag when the communication gets recovered */
222 if (!err)
223 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224 return err;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/**
228 * snd_hda_codec_read - send a command and get the response
229 * @codec: the HDA codec
230 * @nid: NID to send the command
231 * @direct: direct flag
232 * @verb: the verb to send
233 * @parm: the parameter for the verb
234 *
235 * Send a single command and read the corresponding response.
236 *
237 * Returns the obtained response value, or -1 for an error.
238 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200239unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
240 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned int verb, unsigned int parm)
242{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200243 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
244 unsigned int res;
245 codec_exec_verb(codec, cmd, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return res;
247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100248EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/**
251 * snd_hda_codec_write - send a single command without waiting for response
252 * @codec: the HDA codec
253 * @nid: NID to send the command
254 * @direct: direct flag
255 * @verb: the verb to send
256 * @parm: the parameter for the verb
257 *
258 * Send a single command without waiting for response.
259 *
260 * Returns 0 if successful, or a negative error code.
261 */
262int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
263 unsigned int verb, unsigned int parm)
264{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200265 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100266 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200267 return codec_exec_verb(codec, cmd,
268 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100270EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/**
273 * snd_hda_sequence_write - sequence writes
274 * @codec: the HDA codec
275 * @seq: VERB array to send
276 *
277 * Send the commands sequentially from the given array.
278 * The array must be terminated with NID=0.
279 */
280void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
281{
282 for (; seq->nid; seq++)
283 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100285EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/**
288 * snd_hda_get_sub_nodes - get the range of sub nodes
289 * @codec: the HDA codec
290 * @nid: NID to parse
291 * @start_id: the pointer to store the start NID
292 *
293 * Parse the NID and store the start NID of its sub-nodes.
294 * Returns the number of sub-nodes.
295 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200296int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
297 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned int parm;
300
301 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200302 if (parm == -1)
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *start_id = (parm >> 16) & 0x7fff;
305 return (int)(parm & 0x7fff);
306}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100307EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
310 * snd_hda_get_connections - get connection list
311 * @codec: the HDA codec
312 * @nid: NID to parse
313 * @conn_list: connection list array
314 * @max_conns: max. number of connections to store
315 *
316 * Parses the connection list of the given widget and stores the list
317 * of NIDs.
318 *
319 * Returns the number of connections, or a negative error code.
320 */
321int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
322 hda_nid_t *conn_list, int max_conns)
323{
324 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100325 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200327 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100328 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Takashi Iwaida3cec32008-08-08 17:12:14 +0200330 if (snd_BUG_ON(!conn_list || max_conns <= 0))
331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200333 wcaps = get_wcaps(codec, nid);
334 if (!(wcaps & AC_WCAP_CONN_LIST) &&
335 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200336 snd_printk(KERN_WARNING "hda_codec: "
337 "connection list not available for 0x%x\n", nid);
338 return -EINVAL;
339 }
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
342 if (parm & AC_CLIST_LONG) {
343 /* long form */
344 shift = 16;
345 num_elems = 2;
346 } else {
347 /* short form */
348 shift = 8;
349 num_elems = 4;
350 }
351 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mask = (1 << (shift-1)) - 1;
353
Takashi Iwai0ba21762007-04-16 11:29:14 +0200354 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0; /* no connection */
356
357 if (conn_len == 1) {
358 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200359 parm = snd_hda_codec_read(codec, nid, 0,
360 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200361 if (parm == -1 && codec->bus->rirb_error)
362 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 conn_list[0] = parm & mask;
364 return 1;
365 }
366
367 /* multi connection */
368 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100369 prev_nid = 0;
370 for (i = 0; i < conn_len; i++) {
371 int range_val;
372 hda_nid_t val, n;
373
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200374 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100375 parm = snd_hda_codec_read(codec, nid, 0,
376 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200377 if (parm == -1 && codec->bus->rirb_error)
378 return -EIO;
379 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200380 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100381 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200382 if (val == 0) {
383 snd_printk(KERN_WARNING "hda_codec: "
384 "invalid CONNECT_LIST verb %x[%i]:%x\n",
385 nid, i, parm);
386 return 0;
387 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100388 parm >>= shift;
389 if (range_val) {
390 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200391 if (!prev_nid || prev_nid >= val) {
392 snd_printk(KERN_WARNING "hda_codec: "
393 "invalid dep_range_val %x:%x\n",
394 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100395 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100397 for (n = prev_nid + 1; n <= val; n++) {
398 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200399 snd_printk(KERN_ERR "hda_codec: "
400 "Too many connections %d for NID 0x%x\n",
401 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100402 return -EINVAL;
403 }
404 conn_list[conns++] = n;
405 }
406 } else {
407 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200408 snd_printk(KERN_ERR "hda_codec: "
409 "Too many connections %d for NID 0x%x\n",
410 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100411 return -EINVAL;
412 }
413 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100415 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 return conns;
418}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100419EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421
422/**
423 * snd_hda_queue_unsol_event - add an unsolicited event to queue
424 * @bus: the BUS
425 * @res: unsolicited event (lower 32bit of RIRB entry)
426 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
427 *
428 * Adds the given event to the queue. The events are processed in
429 * the workqueue asynchronously. Call this function in the interrupt
430 * hanlder when RIRB receives an unsolicited event.
431 *
432 * Returns 0 if successful, or a negative error code.
433 */
434int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
435{
436 struct hda_bus_unsolicited *unsol;
437 unsigned int wp;
438
Takashi Iwai0ba21762007-04-16 11:29:14 +0200439 unsol = bus->unsol;
440 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442
443 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
444 unsol->wp = wp;
445
446 wp <<= 1;
447 unsol->queue[wp] = res;
448 unsol->queue[wp + 1] = res_ex;
449
Takashi Iwai6acaed32009-01-12 10:09:24 +0100450 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 return 0;
453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100454EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800457 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
David Howellsc4028952006-11-22 14:57:56 +0000459static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
David Howellsc4028952006-11-22 14:57:56 +0000461 struct hda_bus_unsolicited *unsol =
462 container_of(work, struct hda_bus_unsolicited, work);
463 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct hda_codec *codec;
465 unsigned int rp, caddr, res;
466
467 while (unsol->rp != unsol->wp) {
468 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
469 unsol->rp = rp;
470 rp <<= 1;
471 res = unsol->queue[rp];
472 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200473 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 continue;
475 codec = bus->caddr_tbl[caddr & 0x0f];
476 if (codec && codec->patch_ops.unsol_event)
477 codec->patch_ops.unsol_event(codec, res);
478 }
479}
480
481/*
482 * initialize unsolicited queue
483 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200484static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct hda_bus_unsolicited *unsol;
487
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100488 if (bus->unsol) /* already initialized */
489 return 0;
490
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200491 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200492 if (!unsol) {
493 snd_printk(KERN_ERR "hda_codec: "
494 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -ENOMEM;
496 }
David Howellsc4028952006-11-22 14:57:56 +0000497 INIT_WORK(&unsol->work, process_unsol_events);
498 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 bus->unsol = unsol;
500 return 0;
501}
502
503/*
504 * destructor
505 */
506static void snd_hda_codec_free(struct hda_codec *codec);
507
508static int snd_hda_bus_free(struct hda_bus *bus)
509{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200510 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Takashi Iwai0ba21762007-04-16 11:29:14 +0200512 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100514 if (bus->workq)
515 flush_workqueue(bus->workq);
516 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200518 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 snd_hda_codec_free(codec);
520 }
521 if (bus->ops.private_free)
522 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100523 if (bus->workq)
524 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 kfree(bus);
526 return 0;
527}
528
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100529static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100532 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return snd_hda_bus_free(bus);
534}
535
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200536#ifdef CONFIG_SND_HDA_HWDEP
537static int snd_hda_bus_dev_register(struct snd_device *device)
538{
539 struct hda_bus *bus = device->device_data;
540 struct hda_codec *codec;
541 list_for_each_entry(codec, &bus->codec_list, list) {
542 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100543 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200544 }
545 return 0;
546}
547#else
548#define snd_hda_bus_dev_register NULL
549#endif
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
552 * snd_hda_bus_new - create a HDA bus
553 * @card: the card entry
554 * @temp: the template for hda_bus information
555 * @busp: the pointer to store the created bus instance
556 *
557 * Returns 0 if successful, or a negative error code.
558 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100559int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200560 const struct hda_bus_template *temp,
561 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct hda_bus *bus;
564 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100565 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200566 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 .dev_free = snd_hda_bus_dev_free,
568 };
569
Takashi Iwaida3cec32008-08-08 17:12:14 +0200570 if (snd_BUG_ON(!temp))
571 return -EINVAL;
572 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (busp)
576 *busp = NULL;
577
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200578 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (bus == NULL) {
580 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
581 return -ENOMEM;
582 }
583
584 bus->card = card;
585 bus->private_data = temp->private_data;
586 bus->pci = temp->pci;
587 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100588 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 bus->ops = temp->ops;
590
Ingo Molnar62932df2006-01-16 16:34:20 +0100591 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200592 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 INIT_LIST_HEAD(&bus->codec_list);
594
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100595 snprintf(bus->workq_name, sizeof(bus->workq_name),
596 "hd-audio%d", card->number);
597 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100598 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100599 snd_printk(KERN_ERR "cannot create workqueue %s\n",
600 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100601 kfree(bus);
602 return -ENOMEM;
603 }
604
Takashi Iwai0ba21762007-04-16 11:29:14 +0200605 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
606 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 snd_hda_bus_free(bus);
608 return err;
609 }
610 if (busp)
611 *busp = bus;
612 return 0;
613}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100614EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Takashi Iwai82467612007-07-27 19:15:54 +0200616#ifdef CONFIG_SND_HDA_GENERIC
617#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200618 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200619#else
620#define is_generic_config(codec) 0
621#endif
622
Takashi Iwai645f10c2008-11-28 15:07:37 +0100623#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100624#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
625#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100626#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100627#endif
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*
630 * find a matching codec preset
631 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200632static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200633find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100635 struct hda_codec_preset_list *tbl;
636 const struct hda_codec_preset *preset;
637 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Takashi Iwai82467612007-07-27 19:15:54 +0200639 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100640 return NULL; /* use the generic parser */
641
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100642 again:
643 mutex_lock(&preset_mutex);
644 list_for_each_entry(tbl, &hda_preset_tables, list) {
645 if (!try_module_get(tbl->owner)) {
646 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
647 continue;
648 }
649 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100651 if (preset->afg && preset->afg != codec->afg)
652 continue;
653 if (preset->mfg && preset->mfg != codec->mfg)
654 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200657 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200658 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100659 preset->rev == codec->revision_id)) {
660 mutex_unlock(&preset_mutex);
661 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100665 module_put(tbl->owner);
666 }
667 mutex_unlock(&preset_mutex);
668
669 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
670 char name[32];
671 if (!mod_requested)
672 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
673 codec->vendor_id);
674 else
675 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
676 (codec->vendor_id >> 16) & 0xffff);
677 request_module(name);
678 mod_requested++;
679 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681 return NULL;
682}
683
684/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200685 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200687static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 const struct hda_vendor_id *c;
690 const char *vendor = NULL;
691 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200692 char tmp[16];
693
694 if (codec->vendor_name)
695 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 for (c = hda_vendor_ids; c->id; c++) {
698 if (c->id == vendor_id) {
699 vendor = c->name;
700 break;
701 }
702 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200703 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 sprintf(tmp, "Generic %04x", vendor_id);
705 vendor = tmp;
706 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200707 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
708 if (!codec->vendor_name)
709 return -ENOMEM;
710
711 get_chip_name:
712 if (codec->chip_name)
713 return 0;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200716 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
717 else {
718 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
719 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
720 }
721 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200722 return -ENOMEM;
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200727 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100729static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200731 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 hda_nid_t nid;
733
734 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
735 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200736 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200737 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200738 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200739 case AC_GRP_AUDIO_FUNCTION:
740 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200741 codec->afg_function_id = function_id & 0xff;
742 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200743 break;
744 case AC_GRP_MODEM_FUNCTION:
745 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200746 codec->mfg_function_id = function_id & 0xff;
747 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200748 break;
749 default:
750 break;
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100756 * read widget caps for each widget and store in cache
757 */
758static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
759{
760 int i;
761 hda_nid_t nid;
762
763 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
764 &codec->start_nid);
765 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200766 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100767 return -ENOMEM;
768 nid = codec->start_nid;
769 for (i = 0; i < codec->num_nodes; i++, nid++)
770 codec->wcaps[i] = snd_hda_param_read(codec, nid,
771 AC_PAR_AUDIO_WIDGET_CAP);
772 return 0;
773}
774
Takashi Iwai3be14142009-02-20 14:11:16 +0100775/* read all pin default configurations and save codec->init_pins */
776static int read_pin_defaults(struct hda_codec *codec)
777{
778 int i;
779 hda_nid_t nid = codec->start_nid;
780
781 for (i = 0; i < codec->num_nodes; i++, nid++) {
782 struct hda_pincfg *pin;
783 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200784 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100785 if (wid_type != AC_WID_PIN)
786 continue;
787 pin = snd_array_new(&codec->init_pins);
788 if (!pin)
789 return -ENOMEM;
790 pin->nid = nid;
791 pin->cfg = snd_hda_codec_read(codec, nid, 0,
792 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200793 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
794 AC_VERB_GET_PIN_WIDGET_CONTROL,
795 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100796 }
797 return 0;
798}
799
800/* look up the given pin config list and return the item matching with NID */
801static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
802 struct snd_array *array,
803 hda_nid_t nid)
804{
805 int i;
806 for (i = 0; i < array->used; i++) {
807 struct hda_pincfg *pin = snd_array_elem(array, i);
808 if (pin->nid == nid)
809 return pin;
810 }
811 return NULL;
812}
813
814/* write a config value for the given NID */
815static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
816 unsigned int cfg)
817{
818 int i;
819 for (i = 0; i < 4; i++) {
820 snd_hda_codec_write(codec, nid, 0,
821 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
822 cfg & 0xff);
823 cfg >>= 8;
824 }
825}
826
827/* set the current pin config value for the given NID.
828 * the value is cached, and read via snd_hda_codec_get_pincfg()
829 */
830int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
831 hda_nid_t nid, unsigned int cfg)
832{
833 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100834 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100835
Takashi Iwaib82855a2009-12-27 11:24:56 +0100836 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
837 return -EINVAL;
838
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100839 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100840 pin = look_up_pincfg(codec, list, nid);
841 if (!pin) {
842 pin = snd_array_new(list);
843 if (!pin)
844 return -ENOMEM;
845 pin->nid = nid;
846 }
847 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100848
849 /* change only when needed; e.g. if the pincfg is already present
850 * in user_pins[], don't write it
851 */
852 cfg = snd_hda_codec_get_pincfg(codec, nid);
853 if (oldcfg != cfg)
854 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100855 return 0;
856}
857
Takashi Iwaid5191e52009-11-16 14:58:17 +0100858/**
859 * snd_hda_codec_set_pincfg - Override a pin default configuration
860 * @codec: the HDA codec
861 * @nid: NID to set the pin config
862 * @cfg: the pin default config value
863 *
864 * Override a pin default configuration value in the cache.
865 * This value can be read by snd_hda_codec_get_pincfg() in a higher
866 * priority than the real hardware value.
867 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100868int snd_hda_codec_set_pincfg(struct hda_codec *codec,
869 hda_nid_t nid, unsigned int cfg)
870{
Takashi Iwai346ff702009-02-23 09:42:57 +0100871 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100872}
873EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
874
Takashi Iwaid5191e52009-11-16 14:58:17 +0100875/**
876 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
877 * @codec: the HDA codec
878 * @nid: NID to get the pin config
879 *
880 * Get the current pin config value of the given pin NID.
881 * If the pincfg value is cached or overridden via sysfs or driver,
882 * returns the cached value.
883 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100884unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
885{
886 struct hda_pincfg *pin;
887
Takashi Iwai3be14142009-02-20 14:11:16 +0100888#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100889 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100890 if (pin)
891 return pin->cfg;
892#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100893 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
894 if (pin)
895 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100896 pin = look_up_pincfg(codec, &codec->init_pins, nid);
897 if (pin)
898 return pin->cfg;
899 return 0;
900}
901EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
902
903/* restore all current pin configs */
904static void restore_pincfgs(struct hda_codec *codec)
905{
906 int i;
907 for (i = 0; i < codec->init_pins.used; i++) {
908 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
909 set_pincfg(codec, pin->nid,
910 snd_hda_codec_get_pincfg(codec, pin->nid));
911 }
912}
Takashi Iwai54d17402005-11-21 16:33:22 +0100913
Takashi Iwai92ee6162009-12-27 11:18:59 +0100914/**
915 * snd_hda_shutup_pins - Shut up all pins
916 * @codec: the HDA codec
917 *
918 * Clear all pin controls to shup up before suspend for avoiding click noise.
919 * The controls aren't cached so that they can be resumed properly.
920 */
921void snd_hda_shutup_pins(struct hda_codec *codec)
922{
923 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200924 /* don't shut up pins when unloading the driver; otherwise it breaks
925 * the default pin setup at the next load of the driver
926 */
927 if (codec->bus->shutdown)
928 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100929 for (i = 0; i < codec->init_pins.used; i++) {
930 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
931 /* use read here for syncing after issuing each verb */
932 snd_hda_codec_read(codec, pin->nid, 0,
933 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
934 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200935 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100936}
937EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
938
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200939/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
940static void restore_shutup_pins(struct hda_codec *codec)
941{
942 int i;
943 if (!codec->pins_shutup)
944 return;
945 if (codec->bus->shutdown)
946 return;
947 for (i = 0; i < codec->init_pins.used; i++) {
948 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
949 snd_hda_codec_write(codec, pin->nid, 0,
950 AC_VERB_SET_PIN_WIDGET_CONTROL,
951 pin->ctrl);
952 }
953 codec->pins_shutup = 0;
954}
955
Takashi Iwai01751f52007-08-10 16:59:39 +0200956static void init_hda_cache(struct hda_cache_rec *cache,
957 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200958static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200959
Takashi Iwai3be14142009-02-20 14:11:16 +0100960/* restore the initial pin cfgs and release all pincfg lists */
961static void restore_init_pincfgs(struct hda_codec *codec)
962{
Takashi Iwai346ff702009-02-23 09:42:57 +0100963 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100964 * so that only the values in init_pins are restored
965 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100966 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100967#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100968 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100969#endif
970 restore_pincfgs(codec);
971 snd_array_free(&codec->init_pins);
972}
973
Takashi Iwai54d17402005-11-21 16:33:22 +0100974/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200975 * audio-converter setup caches
976 */
977struct hda_cvt_setup {
978 hda_nid_t nid;
979 u8 stream_tag;
980 u8 channel_id;
981 u16 format_id;
982 unsigned char active; /* cvt is currently used */
983 unsigned char dirty; /* setups should be cleared */
984};
985
986/* get or create a cache entry for the given audio converter NID */
987static struct hda_cvt_setup *
988get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
989{
990 struct hda_cvt_setup *p;
991 int i;
992
993 for (i = 0; i < codec->cvt_setups.used; i++) {
994 p = snd_array_elem(&codec->cvt_setups, i);
995 if (p->nid == nid)
996 return p;
997 }
998 p = snd_array_new(&codec->cvt_setups);
999 if (p)
1000 p->nid = nid;
1001 return p;
1002}
1003
1004/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * codec destructor
1006 */
1007static void snd_hda_codec_free(struct hda_codec *codec)
1008{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001009 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001011 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001012#ifdef CONFIG_SND_HDA_POWER_SAVE
1013 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001014 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001017 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001018 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 codec->bus->caddr_tbl[codec->addr] = NULL;
1020 if (codec->patch_ops.free)
1021 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001022 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001023 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001024 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001025 kfree(codec->vendor_name);
1026 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001027 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001028 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 kfree(codec);
1030}
1031
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001032static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1033 unsigned int power_state);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/**
1036 * snd_hda_codec_new - create a HDA codec
1037 * @bus: the bus to assign
1038 * @codec_addr: the codec address
1039 * @codecp: the pointer to store the generated codec
1040 *
1041 * Returns 0 if successful, or a negative error code.
1042 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001043int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1044 unsigned int codec_addr,
1045 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001048 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int err;
1050
Takashi Iwaida3cec32008-08-08 17:12:14 +02001051 if (snd_BUG_ON(!bus))
1052 return -EINVAL;
1053 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1054 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001057 snd_printk(KERN_ERR "hda_codec: "
1058 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -EBUSY;
1060 }
1061
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001062 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (codec == NULL) {
1064 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1065 return -ENOMEM;
1066 }
1067
1068 codec->bus = bus;
1069 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001070 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001071 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001072 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001073 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001074 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1075 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001076 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001077 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001078 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001079 if (codec->bus->modelname) {
1080 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1081 if (!codec->modelname) {
1082 snd_hda_codec_free(codec);
1083 return -ENODEV;
1084 }
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Takashi Iwaicb53c622007-08-10 17:21:45 +02001087#ifdef CONFIG_SND_HDA_POWER_SAVE
1088 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1089 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1090 * the caller has to power down appropriatley after initialization
1091 * phase.
1092 */
1093 hda_keep_power_on(codec);
1094#endif
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 list_add_tail(&codec->list, &bus->codec_list);
1097 bus->caddr_tbl[codec_addr] = codec;
1098
Takashi Iwai0ba21762007-04-16 11:29:14 +02001099 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1100 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001101 if (codec->vendor_id == -1)
1102 /* read again, hopefully the access method was corrected
1103 * in the last read...
1104 */
1105 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1106 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001107 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1108 AC_PAR_SUBSYSTEM_ID);
1109 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1110 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001112 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001113 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001114 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001115 err = -ENODEV;
1116 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
Takashi Iwai3be14142009-02-20 14:11:16 +01001119 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1120 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001121 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001122 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001123 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001124 err = read_pin_defaults(codec);
1125 if (err < 0)
1126 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001127
Takashi Iwai0ba21762007-04-16 11:29:14 +02001128 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001129 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001130 codec->subsystem_id =
1131 snd_hda_codec_read(codec, nid, 0,
1132 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001133 }
1134
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001135 /* power-up all before initialization */
1136 hda_set_power_state(codec,
1137 codec->afg ? codec->afg : codec->mfg,
1138 AC_PWRST_D0);
1139
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001140 snd_hda_codec_proc_new(codec);
1141
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001142 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001143
1144 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1145 codec->subsystem_id, codec->revision_id);
1146 snd_component_add(codec->bus->card, component);
1147
1148 if (codecp)
1149 *codecp = codec;
1150 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001151
1152 error:
1153 snd_hda_codec_free(codec);
1154 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001155}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001156EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001157
Takashi Iwaid5191e52009-11-16 14:58:17 +01001158/**
1159 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1160 * @codec: the HDA codec
1161 *
1162 * Start parsing of the given codec tree and (re-)initialize the whole
1163 * patch instance.
1164 *
1165 * Returns 0 if successful or a negative error code.
1166 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001167int snd_hda_codec_configure(struct hda_codec *codec)
1168{
1169 int err;
1170
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001171 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001172 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001173 err = get_codec_name(codec);
1174 if (err < 0)
1175 return err;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Takashi Iwai82467612007-07-27 19:15:54 +02001178 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001180 goto patched;
1181 }
Takashi Iwai82467612007-07-27 19:15:54 +02001182 if (codec->preset && codec->preset->patch) {
1183 err = codec->preset->patch(codec);
1184 goto patched;
1185 }
1186
1187 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001188 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001189 if (err < 0)
1190 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001191
1192 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001193 if (!err && codec->patch_ops.unsol_event)
1194 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001195 /* audio codec should override the mixer name */
1196 if (!err && (codec->afg || !*codec->bus->card->mixername))
1197 snprintf(codec->bus->card->mixername,
1198 sizeof(codec->bus->card->mixername),
1199 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001202EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204/**
1205 * snd_hda_codec_setup_stream - set up the codec for streaming
1206 * @codec: the CODEC to set up
1207 * @nid: the NID to set up
1208 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1209 * @channel_id: channel id to pass, zero based.
1210 * @format: stream format.
1211 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001212void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1213 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int channel_id, int format)
1215{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001216 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001217 struct hda_cvt_setup *p;
1218 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001219 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001220 int i;
1221
Takashi Iwai0ba21762007-04-16 11:29:14 +02001222 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001223 return;
1224
Takashi Iwai0ba21762007-04-16 11:29:14 +02001225 snd_printdd("hda_codec_setup_stream: "
1226 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001228 p = get_hda_cvt_setup(codec, nid);
1229 if (!p)
1230 return;
1231 /* update the stream-id if changed */
1232 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1233 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1234 newval = (stream_tag << 4) | channel_id;
1235 if (oldval != newval)
1236 snd_hda_codec_write(codec, nid, 0,
1237 AC_VERB_SET_CHANNEL_STREAMID,
1238 newval);
1239 p->stream_tag = stream_tag;
1240 p->channel_id = channel_id;
1241 }
1242 /* update the format-id if changed */
1243 if (p->format_id != format) {
1244 oldval = snd_hda_codec_read(codec, nid, 0,
1245 AC_VERB_GET_STREAM_FORMAT, 0);
1246 if (oldval != format) {
1247 msleep(1);
1248 snd_hda_codec_write(codec, nid, 0,
1249 AC_VERB_SET_STREAM_FORMAT,
1250 format);
1251 }
1252 p->format_id = format;
1253 }
1254 p->active = 1;
1255 p->dirty = 0;
1256
1257 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001258 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001259 list_for_each_entry(c, &codec->bus->codec_list, list) {
1260 for (i = 0; i < c->cvt_setups.used; i++) {
1261 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001262 if (!p->active && p->stream_tag == stream_tag &&
1263 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001264 p->dirty = 1;
1265 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001268EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Takashi Iwaif0cea792010-08-13 11:56:53 +02001270static void really_cleanup_stream(struct hda_codec *codec,
1271 struct hda_cvt_setup *q);
1272
Takashi Iwaid5191e52009-11-16 14:58:17 +01001273/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001274 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001275 * @codec: the CODEC to clean up
1276 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001277 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001278 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001279void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1280 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001281{
Takashi Iwaieb541332010-08-06 13:48:11 +02001282 struct hda_cvt_setup *p;
1283
Takashi Iwai888afa12008-03-18 09:57:50 +01001284 if (!nid)
1285 return;
1286
1287 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001288 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001289 if (p) {
1290 /* here we just clear the active flag when do_now isn't set;
1291 * actual clean-ups will be done later in
1292 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1293 */
1294 if (do_now)
1295 really_cleanup_stream(codec, p);
1296 else
1297 p->active = 0;
1298 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001299}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001300EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001301
Takashi Iwaieb541332010-08-06 13:48:11 +02001302static void really_cleanup_stream(struct hda_codec *codec,
1303 struct hda_cvt_setup *q)
1304{
1305 hda_nid_t nid = q->nid;
1306 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1307 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1308 memset(q, 0, sizeof(*q));
1309 q->nid = nid;
1310}
1311
1312/* clean up the all conflicting obsolete streams */
1313static void purify_inactive_streams(struct hda_codec *codec)
1314{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001315 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001316 int i;
1317
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001318 list_for_each_entry(c, &codec->bus->codec_list, list) {
1319 for (i = 0; i < c->cvt_setups.used; i++) {
1320 struct hda_cvt_setup *p;
1321 p = snd_array_elem(&c->cvt_setups, i);
1322 if (p->dirty)
1323 really_cleanup_stream(c, p);
1324 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001325 }
1326}
1327
1328/* clean up all streams; called from suspend */
1329static void hda_cleanup_all_streams(struct hda_codec *codec)
1330{
1331 int i;
1332
1333 for (i = 0; i < codec->cvt_setups.used; i++) {
1334 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1335 if (p->stream_tag)
1336 really_cleanup_stream(codec, p);
1337 }
1338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340/*
1341 * amp access functions
1342 */
1343
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001344/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001345#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001346#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001347#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1348#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001350#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001353static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001354 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Takashi Iwai01751f52007-08-10 16:59:39 +02001356 memset(cache, 0, sizeof(*cache));
1357 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001358 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001359}
1360
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001361static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001362{
Takashi Iwai603c4012008-07-30 15:01:44 +02001363 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
1366/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001367static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Takashi Iwai01751f52007-08-10 16:59:39 +02001369 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1370 u16 cur = cache->hash[idx];
1371 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001374 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (info->key == key)
1376 return info;
1377 cur = info->next;
1378 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001379 return NULL;
1380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001382/* query the hash. allocate an entry if not found. */
1383static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1384 u32 key)
1385{
1386 struct hda_cache_head *info = get_hash(cache, key);
1387 if (!info) {
1388 u16 idx, cur;
1389 /* add a new hash entry */
1390 info = snd_array_new(&cache->buf);
1391 if (!info)
1392 return NULL;
1393 cur = snd_array_index(&cache->buf, info);
1394 info->key = key;
1395 info->val = 0;
1396 idx = key % (u16)ARRAY_SIZE(cache->hash);
1397 info->next = cache->hash[idx];
1398 cache->hash[idx] = cur;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return info;
1401}
1402
Takashi Iwai01751f52007-08-10 16:59:39 +02001403/* query and allocate an amp hash entry */
1404static inline struct hda_amp_info *
1405get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1406{
1407 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1408}
1409
Takashi Iwaid5191e52009-11-16 14:58:17 +01001410/**
1411 * query_amp_caps - query AMP capabilities
1412 * @codec: the HD-auio codec
1413 * @nid: the NID to query
1414 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1415 *
1416 * Query AMP capabilities for the given widget and direction.
1417 * Returns the obtained capability bits.
1418 *
1419 * When cap bits have been already read, this doesn't read again but
1420 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001422u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001424 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Takashi Iwai0ba21762007-04-16 11:29:14 +02001426 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1427 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001429 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001430 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001432 info->amp_caps = snd_hda_param_read(codec, nid,
1433 direction == HDA_OUTPUT ?
1434 AC_PAR_AMP_OUT_CAP :
1435 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001436 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001437 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439 return info->amp_caps;
1440}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001441EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Takashi Iwaid5191e52009-11-16 14:58:17 +01001443/**
1444 * snd_hda_override_amp_caps - Override the AMP capabilities
1445 * @codec: the CODEC to clean up
1446 * @nid: the NID to clean up
1447 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1448 * @caps: the capability bits to set
1449 *
1450 * Override the cached AMP caps bits value by the given one.
1451 * This function is useful if the driver needs to adjust the AMP ranges,
1452 * e.g. limit to 0dB, etc.
1453 *
1454 * Returns zero if successful or a negative error code.
1455 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001456int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1457 unsigned int caps)
1458{
1459 struct hda_amp_info *info;
1460
1461 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1462 if (!info)
1463 return -EINVAL;
1464 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001465 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001466 return 0;
1467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001468EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001469
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001470static unsigned int
1471query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1472 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001473{
1474 struct hda_amp_info *info;
1475
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001476 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001477 if (!info)
1478 return 0;
1479 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001480 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001481 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001482 }
1483 return info->amp_caps;
1484}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001485
1486static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1487{
1488 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1489}
1490
Takashi Iwaid5191e52009-11-16 14:58:17 +01001491/**
1492 * snd_hda_query_pin_caps - Query PIN capabilities
1493 * @codec: the HD-auio codec
1494 * @nid: the NID to query
1495 *
1496 * Query PIN capabilities for the given widget.
1497 * Returns the obtained capability bits.
1498 *
1499 * When cap bits have been already read, this doesn't read again but
1500 * returns the cached value.
1501 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001502u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1503{
1504 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1505 read_pin_cap);
1506}
Takashi Iwai1327a322009-03-23 13:07:47 +01001507EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1508
Wu Fengguang864f92b2009-11-18 12:38:02 +08001509/**
1510 * snd_hda_pin_sense - execute pin sense measurement
1511 * @codec: the CODEC to sense
1512 * @nid: the pin NID to sense
1513 *
1514 * Execute necessary pin sense measurement and return its Presence Detect,
1515 * Impedance, ELD Valid etc. status bits.
1516 */
1517u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1518{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001519 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001520
Takashi Iwai729d55b2009-12-25 22:49:01 +01001521 if (!codec->no_trigger_sense) {
1522 pincap = snd_hda_query_pin_caps(codec, nid);
1523 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001524 snd_hda_codec_read(codec, nid, 0,
1525 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001526 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001527 return snd_hda_codec_read(codec, nid, 0,
1528 AC_VERB_GET_PIN_SENSE, 0);
1529}
1530EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1531
1532/**
1533 * snd_hda_jack_detect - query pin Presence Detect status
1534 * @codec: the CODEC to sense
1535 * @nid: the pin NID to sense
1536 *
1537 * Query and return the pin's Presence Detect status.
1538 */
1539int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1540{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001541 u32 sense = snd_hda_pin_sense(codec, nid);
1542 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001543}
1544EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546/*
1547 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001548 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001550static unsigned int get_vol_mute(struct hda_codec *codec,
1551 struct hda_amp_info *info, hda_nid_t nid,
1552 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 u32 val, parm;
1555
Takashi Iwai01751f52007-08-10 16:59:39 +02001556 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001557 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1560 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1561 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001562 val = snd_hda_codec_read(codec, nid, 0,
1563 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001565 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001566 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
1569/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001570 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001572static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001573 hda_nid_t nid, int ch, int direction, int index,
1574 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 u32 parm;
1577
1578 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1579 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1580 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1581 parm |= val;
1582 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001583 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Takashi Iwaid5191e52009-11-16 14:58:17 +01001586/**
1587 * snd_hda_codec_amp_read - Read AMP value
1588 * @codec: HD-audio codec
1589 * @nid: NID to read the AMP value
1590 * @ch: channel (left=0 or right=1)
1591 * @direction: #HDA_INPUT or #HDA_OUTPUT
1592 * @index: the index value (only for input direction)
1593 *
1594 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
Takashi Iwai834be882006-03-01 14:16:17 +01001596int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1597 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001599 struct hda_amp_info *info;
1600 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1601 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001603 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001605EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Takashi Iwaid5191e52009-11-16 14:58:17 +01001607/**
1608 * snd_hda_codec_amp_update - update the AMP value
1609 * @codec: HD-audio codec
1610 * @nid: NID to read the AMP value
1611 * @ch: channel (left=0 or right=1)
1612 * @direction: #HDA_INPUT or #HDA_OUTPUT
1613 * @idx: the index value (only for input direction)
1614 * @mask: bit mask to set
1615 * @val: the bits value to set
1616 *
1617 * Update the AMP value with a bit mask.
1618 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001619 */
Takashi Iwai834be882006-03-01 14:16:17 +01001620int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1621 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001623 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001624
Takashi Iwai0ba21762007-04-16 11:29:14 +02001625 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1626 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001628 if (snd_BUG_ON(mask & ~0xff))
1629 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001630 val &= mask;
1631 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001632 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001634 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 1;
1636}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001637EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Takashi Iwaid5191e52009-11-16 14:58:17 +01001639/**
1640 * snd_hda_codec_amp_stereo - update the AMP stereo values
1641 * @codec: HD-audio codec
1642 * @nid: NID to read the AMP value
1643 * @direction: #HDA_INPUT or #HDA_OUTPUT
1644 * @idx: the index value (only for input direction)
1645 * @mask: bit mask to set
1646 * @val: the bits value to set
1647 *
1648 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1649 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001650 */
1651int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1652 int direction, int idx, int mask, int val)
1653{
1654 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001655
1656 if (snd_BUG_ON(mask & ~0xff))
1657 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001658 for (ch = 0; ch < 2; ch++)
1659 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1660 idx, mask, val);
1661 return ret;
1662}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001663EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001664
Takashi Iwaicb53c622007-08-10 17:21:45 +02001665#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001666/**
1667 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1668 * @codec: HD-audio codec
1669 *
1670 * Resume the all amp commands from the cache.
1671 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001672void snd_hda_codec_resume_amp(struct hda_codec *codec)
1673{
Takashi Iwai603c4012008-07-30 15:01:44 +02001674 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001675 int i;
1676
Takashi Iwai603c4012008-07-30 15:01:44 +02001677 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001678 u32 key = buffer->head.key;
1679 hda_nid_t nid;
1680 unsigned int idx, dir, ch;
1681 if (!key)
1682 continue;
1683 nid = key & 0xff;
1684 idx = (key >> 16) & 0xff;
1685 dir = (key >> 24) & 0xff;
1686 for (ch = 0; ch < 2; ch++) {
1687 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1688 continue;
1689 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1690 buffer->vol[ch]);
1691 }
1692 }
1693}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001694EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001695#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001697static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1698 unsigned int ofs)
1699{
1700 u32 caps = query_amp_caps(codec, nid, dir);
1701 /* get num steps */
1702 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1703 if (ofs < caps)
1704 caps -= ofs;
1705 return caps;
1706}
1707
Takashi Iwaid5191e52009-11-16 14:58:17 +01001708/**
1709 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1710 *
1711 * The control element is supposed to have the private_value field
1712 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1713 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001714int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1718 u16 nid = get_amp_nid(kcontrol);
1719 u8 chs = get_amp_channels(kcontrol);
1720 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001721 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001723 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1724 uinfo->count = chs == 3 ? 2 : 1;
1725 uinfo->value.integer.min = 0;
1726 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1727 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001728 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001729 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1730 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return -EINVAL;
1732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return 0;
1734}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001735EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001737
1738static inline unsigned int
1739read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1740 int ch, int dir, int idx, unsigned int ofs)
1741{
1742 unsigned int val;
1743 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1744 val &= HDA_AMP_VOLMASK;
1745 if (val >= ofs)
1746 val -= ofs;
1747 else
1748 val = 0;
1749 return val;
1750}
1751
1752static inline int
1753update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1754 int ch, int dir, int idx, unsigned int ofs,
1755 unsigned int val)
1756{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001757 unsigned int maxval;
1758
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001759 if (val > 0)
1760 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001761 /* ofs = 0: raw max value */
1762 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001763 if (val > maxval)
1764 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001765 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1766 HDA_AMP_VOLMASK, val);
1767}
1768
Takashi Iwaid5191e52009-11-16 14:58:17 +01001769/**
1770 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1771 *
1772 * The control element is supposed to have the private_value field
1773 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1774 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001775int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1776 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1779 hda_nid_t nid = get_amp_nid(kcontrol);
1780 int chs = get_amp_channels(kcontrol);
1781 int dir = get_amp_direction(kcontrol);
1782 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001783 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 long *valp = ucontrol->value.integer.value;
1785
1786 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001787 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001789 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001792EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Takashi Iwaid5191e52009-11-16 14:58:17 +01001794/**
1795 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1796 *
1797 * The control element is supposed to have the private_value field
1798 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1799 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001800int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1801 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1804 hda_nid_t nid = get_amp_nid(kcontrol);
1805 int chs = get_amp_channels(kcontrol);
1806 int dir = get_amp_direction(kcontrol);
1807 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001808 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 long *valp = ucontrol->value.integer.value;
1810 int change = 0;
1811
Takashi Iwaicb53c622007-08-10 17:21:45 +02001812 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001813 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001814 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001815 valp++;
1816 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001817 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001818 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001819 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return change;
1821}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001822EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Takashi Iwaid5191e52009-11-16 14:58:17 +01001824/**
1825 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1826 *
1827 * The control element is supposed to have the private_value field
1828 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1829 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001830int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1831 unsigned int size, unsigned int __user *_tlv)
1832{
1833 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1834 hda_nid_t nid = get_amp_nid(kcontrol);
1835 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001836 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001837 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001838 u32 caps, val1, val2;
1839
1840 if (size < 4 * sizeof(unsigned int))
1841 return -ENOMEM;
1842 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001843 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1844 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001845 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001846 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001847 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001848 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02001849 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001850 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1851 return -EFAULT;
1852 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1853 return -EFAULT;
1854 if (put_user(val1, _tlv + 2))
1855 return -EFAULT;
1856 if (put_user(val2, _tlv + 3))
1857 return -EFAULT;
1858 return 0;
1859}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001860EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001861
Takashi Iwaid5191e52009-11-16 14:58:17 +01001862/**
1863 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1864 * @codec: HD-audio codec
1865 * @nid: NID of a reference widget
1866 * @dir: #HDA_INPUT or #HDA_OUTPUT
1867 * @tlv: TLV data to be stored, at least 4 elements
1868 *
1869 * Set (static) TLV data for a virtual master volume using the AMP caps
1870 * obtained from the reference NID.
1871 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001872 */
1873void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1874 unsigned int *tlv)
1875{
1876 u32 caps;
1877 int nums, step;
1878
1879 caps = query_amp_caps(codec, nid, dir);
1880 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1881 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1882 step = (step + 1) * 25;
1883 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1884 tlv[1] = 2 * sizeof(unsigned int);
1885 tlv[2] = -nums * step;
1886 tlv[3] = step;
1887}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001888EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001889
1890/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001891static struct snd_kcontrol *
1892_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1893 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001894{
1895 struct snd_ctl_elem_id id;
1896 memset(&id, 0, sizeof(id));
1897 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001898 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001899 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1900 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001901 strcpy(id.name, name);
1902 return snd_ctl_find_id(codec->bus->card, &id);
1903}
1904
Takashi Iwaid5191e52009-11-16 14:58:17 +01001905/**
1906 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1907 * @codec: HD-audio codec
1908 * @name: ctl id name string
1909 *
1910 * Get the control element with the given id string and IFACE_MIXER.
1911 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001912struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1913 const char *name)
1914{
1915 return _snd_hda_find_mixer_ctl(codec, name, 0);
1916}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001917EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001918
Takashi Iwaid5191e52009-11-16 14:58:17 +01001919/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001920 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001921 * @codec: HD-audio codec
1922 * @nid: corresponding NID (optional)
1923 * @kctl: the control element to assign
1924 *
1925 * Add the given control element to an array inside the codec instance.
1926 * All control elements belonging to a codec are supposed to be added
1927 * by this function so that a proper clean-up works at the free or
1928 * reconfiguration time.
1929 *
1930 * If non-zero @nid is passed, the NID is assigned to the control element.
1931 * The assignment is shown in the codec proc file.
1932 *
1933 * snd_hda_ctl_add() checks the control subdev id field whether
1934 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001935 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1936 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001937 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001938int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1939 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001940{
1941 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001942 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001943 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001944
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001945 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001946 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001947 if (nid == 0)
1948 nid = get_amp_nid_(kctl->private_value);
1949 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001950 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1951 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001952 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001953 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001954 err = snd_ctl_add(codec->bus->card, kctl);
1955 if (err < 0)
1956 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001957 item = snd_array_new(&codec->mixers);
1958 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001959 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001960 item->kctl = kctl;
1961 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001962 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001963 return 0;
1964}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001965EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001966
Takashi Iwaid5191e52009-11-16 14:58:17 +01001967/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001968 * snd_hda_add_nid - Assign a NID to a control element
1969 * @codec: HD-audio codec
1970 * @nid: corresponding NID (optional)
1971 * @kctl: the control element to assign
1972 * @index: index to kctl
1973 *
1974 * Add the given control element to an array inside the codec instance.
1975 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1976 * NID:KCTL mapping - for example "Capture Source" selector.
1977 */
1978int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1979 unsigned int index, hda_nid_t nid)
1980{
1981 struct hda_nid_item *item;
1982
1983 if (nid > 0) {
1984 item = snd_array_new(&codec->nids);
1985 if (!item)
1986 return -ENOMEM;
1987 item->kctl = kctl;
1988 item->index = index;
1989 item->nid = nid;
1990 return 0;
1991 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01001992 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1993 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001994 return -EINVAL;
1995}
1996EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1997
1998/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001999 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2000 * @codec: HD-audio codec
2001 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002002void snd_hda_ctls_clear(struct hda_codec *codec)
2003{
2004 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002005 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002006 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002007 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002008 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002009 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002010}
2011
Takashi Iwaia65d6292009-02-23 16:57:04 +01002012/* pseudo device locking
2013 * toggle card->shutdown to allow/disallow the device access (as a hack)
2014 */
2015static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002016{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002017 spin_lock(&card->files_lock);
2018 if (card->shutdown) {
2019 spin_unlock(&card->files_lock);
2020 return -EINVAL;
2021 }
2022 card->shutdown = 1;
2023 spin_unlock(&card->files_lock);
2024 return 0;
2025}
2026
2027static void hda_unlock_devices(struct snd_card *card)
2028{
2029 spin_lock(&card->files_lock);
2030 card->shutdown = 0;
2031 spin_unlock(&card->files_lock);
2032}
2033
Takashi Iwaid5191e52009-11-16 14:58:17 +01002034/**
2035 * snd_hda_codec_reset - Clear all objects assigned to the codec
2036 * @codec: HD-audio codec
2037 *
2038 * This frees the all PCM and control elements assigned to the codec, and
2039 * clears the caches and restores the pin default configurations.
2040 *
2041 * When a device is being used, it returns -EBSY. If successfully freed,
2042 * returns zero.
2043 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002044int snd_hda_codec_reset(struct hda_codec *codec)
2045{
2046 struct snd_card *card = codec->bus->card;
2047 int i, pcm;
2048
2049 if (hda_lock_devices(card) < 0)
2050 return -EBUSY;
2051 /* check whether the codec isn't used by any mixer or PCM streams */
2052 if (!list_empty(&card->ctl_files)) {
2053 hda_unlock_devices(card);
2054 return -EBUSY;
2055 }
2056 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2057 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2058 if (!cpcm->pcm)
2059 continue;
2060 if (cpcm->pcm->streams[0].substream_opened ||
2061 cpcm->pcm->streams[1].substream_opened) {
2062 hda_unlock_devices(card);
2063 return -EBUSY;
2064 }
2065 }
2066
2067 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002068
2069#ifdef CONFIG_SND_HDA_POWER_SAVE
2070 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002071 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002072#endif
2073 snd_hda_ctls_clear(codec);
2074 /* relase PCMs */
2075 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002076 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002077 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002078 clear_bit(codec->pcm_info[i].device,
2079 codec->bus->pcm_dev_bits);
2080 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002081 }
2082 if (codec->patch_ops.free)
2083 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002084 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002085 codec->spec = NULL;
2086 free_hda_cache(&codec->amp_cache);
2087 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002088 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2089 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002090 /* free only driver_pins so that init_pins + user_pins are restored */
2091 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002092 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002093 codec->num_pcms = 0;
2094 codec->pcm_info = NULL;
2095 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002096 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2097 codec->slave_dig_outs = NULL;
2098 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002099 module_put(codec->owner);
2100 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002101
2102 /* allow device access again */
2103 hda_unlock_devices(card);
2104 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002105}
2106
Takashi Iwaid5191e52009-11-16 14:58:17 +01002107/**
2108 * snd_hda_add_vmaster - create a virtual master control and add slaves
2109 * @codec: HD-audio codec
2110 * @name: vmaster control name
2111 * @tlv: TLV data (optional)
2112 * @slaves: slave control names (optional)
2113 *
2114 * Create a virtual master control with the given name. The TLV data
2115 * must be either NULL or a valid data.
2116 *
2117 * @slaves is a NULL-terminated array of strings, each of which is a
2118 * slave control name. All controls with these names are assigned to
2119 * the new virtual master control.
2120 *
2121 * This function returns zero if successful or a negative error code.
2122 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002123int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2124 unsigned int *tlv, const char **slaves)
2125{
2126 struct snd_kcontrol *kctl;
2127 const char **s;
2128 int err;
2129
Takashi Iwai2f085542008-02-22 18:43:50 +01002130 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2131 ;
2132 if (!*s) {
2133 snd_printdd("No slave found for %s\n", name);
2134 return 0;
2135 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002136 kctl = snd_ctl_make_virtual_master(name, tlv);
2137 if (!kctl)
2138 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002139 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002140 if (err < 0)
2141 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002142
Takashi Iwai2134ea42008-01-10 16:53:55 +01002143 for (s = slaves; *s; s++) {
2144 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002145 int i = 0;
2146 for (;;) {
2147 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2148 if (!sctl) {
2149 if (!i)
2150 snd_printdd("Cannot find slave %s, "
2151 "skipped\n", *s);
2152 break;
2153 }
2154 err = snd_ctl_add_slave(kctl, sctl);
2155 if (err < 0)
2156 return err;
2157 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002158 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002159 }
2160 return 0;
2161}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002162EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002163
Takashi Iwaid5191e52009-11-16 14:58:17 +01002164/**
2165 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2166 *
2167 * The control element is supposed to have the private_value field
2168 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2169 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002170int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 int chs = get_amp_channels(kcontrol);
2174
2175 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2176 uinfo->count = chs == 3 ? 2 : 1;
2177 uinfo->value.integer.min = 0;
2178 uinfo->value.integer.max = 1;
2179 return 0;
2180}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002181EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Takashi Iwaid5191e52009-11-16 14:58:17 +01002183/**
2184 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2185 *
2186 * The control element is supposed to have the private_value field
2187 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2188 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002189int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2190 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2193 hda_nid_t nid = get_amp_nid(kcontrol);
2194 int chs = get_amp_channels(kcontrol);
2195 int dir = get_amp_direction(kcontrol);
2196 int idx = get_amp_index(kcontrol);
2197 long *valp = ucontrol->value.integer.value;
2198
2199 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002200 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002201 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002203 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002204 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return 0;
2206}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002207EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Takashi Iwaid5191e52009-11-16 14:58:17 +01002209/**
2210 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2211 *
2212 * The control element is supposed to have the private_value field
2213 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2214 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002215int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2219 hda_nid_t nid = get_amp_nid(kcontrol);
2220 int chs = get_amp_channels(kcontrol);
2221 int dir = get_amp_direction(kcontrol);
2222 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 long *valp = ucontrol->value.integer.value;
2224 int change = 0;
2225
Takashi Iwaicb53c622007-08-10 17:21:45 +02002226 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002227 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002228 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002229 HDA_AMP_MUTE,
2230 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002231 valp++;
2232 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002233 if (chs & 2)
2234 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002235 HDA_AMP_MUTE,
2236 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002237 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002238 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return change;
2240}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002241EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Takashi Iwai67d634c2009-11-16 15:35:59 +01002243#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002244/**
2245 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2246 *
2247 * This function calls snd_hda_enable_beep_device(), which behaves differently
2248 * depending on beep_mode option.
2249 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002250int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2251 struct snd_ctl_elem_value *ucontrol)
2252{
2253 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2254 long *valp = ucontrol->value.integer.value;
2255
2256 snd_hda_enable_beep_device(codec, *valp);
2257 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2258}
2259EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002260#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262/*
Takashi Iwai985be542005-11-02 18:26:49 +01002263 * bound volume controls
2264 *
2265 * bind multiple volumes (# indices, from 0)
2266 */
2267
2268#define AMP_VAL_IDX_SHIFT 19
2269#define AMP_VAL_IDX_MASK (0x0f<<19)
2270
Takashi Iwaid5191e52009-11-16 14:58:17 +01002271/**
2272 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2273 *
2274 * The control element is supposed to have the private_value field
2275 * set up via HDA_BIND_MUTE*() macros.
2276 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002277int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002279{
2280 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2281 unsigned long pval;
2282 int err;
2283
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002284 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002285 pval = kcontrol->private_value;
2286 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2287 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2288 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002289 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002290 return err;
2291}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002292EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002293
Takashi Iwaid5191e52009-11-16 14:58:17 +01002294/**
2295 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2296 *
2297 * The control element is supposed to have the private_value field
2298 * set up via HDA_BIND_MUTE*() macros.
2299 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002300int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2301 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002302{
2303 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2304 unsigned long pval;
2305 int i, indices, err = 0, change = 0;
2306
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002307 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002308 pval = kcontrol->private_value;
2309 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2310 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002311 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2312 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002313 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2314 if (err < 0)
2315 break;
2316 change |= err;
2317 }
2318 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002319 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002320 return err < 0 ? err : change;
2321}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002322EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002323
Takashi Iwaid5191e52009-11-16 14:58:17 +01002324/**
2325 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2326 *
2327 * The control element is supposed to have the private_value field
2328 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002329 */
2330int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_info *uinfo)
2332{
2333 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2334 struct hda_bind_ctls *c;
2335 int err;
2336
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002337 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002338 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002339 kcontrol->private_value = *c->values;
2340 err = c->ops->info(kcontrol, uinfo);
2341 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002342 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002343 return err;
2344}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002345EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002346
Takashi Iwaid5191e52009-11-16 14:58:17 +01002347/**
2348 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2349 *
2350 * The control element is supposed to have the private_value field
2351 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2352 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002353int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2354 struct snd_ctl_elem_value *ucontrol)
2355{
2356 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2357 struct hda_bind_ctls *c;
2358 int err;
2359
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002360 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002361 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002362 kcontrol->private_value = *c->values;
2363 err = c->ops->get(kcontrol, ucontrol);
2364 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002365 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002366 return err;
2367}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002368EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002369
Takashi Iwaid5191e52009-11-16 14:58:17 +01002370/**
2371 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2372 *
2373 * The control element is supposed to have the private_value field
2374 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2375 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002376int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2377 struct snd_ctl_elem_value *ucontrol)
2378{
2379 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2380 struct hda_bind_ctls *c;
2381 unsigned long *vals;
2382 int err = 0, change = 0;
2383
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002384 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002385 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002386 for (vals = c->values; *vals; vals++) {
2387 kcontrol->private_value = *vals;
2388 err = c->ops->put(kcontrol, ucontrol);
2389 if (err < 0)
2390 break;
2391 change |= err;
2392 }
2393 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002394 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002395 return err < 0 ? err : change;
2396}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002397EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002398
Takashi Iwaid5191e52009-11-16 14:58:17 +01002399/**
2400 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2401 *
2402 * The control element is supposed to have the private_value field
2403 * set up via HDA_BIND_VOL() macro.
2404 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002405int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2406 unsigned int size, unsigned int __user *tlv)
2407{
2408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2409 struct hda_bind_ctls *c;
2410 int err;
2411
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002412 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002413 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002414 kcontrol->private_value = *c->values;
2415 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2416 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002417 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002418 return err;
2419}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002420EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002421
2422struct hda_ctl_ops snd_hda_bind_vol = {
2423 .info = snd_hda_mixer_amp_volume_info,
2424 .get = snd_hda_mixer_amp_volume_get,
2425 .put = snd_hda_mixer_amp_volume_put,
2426 .tlv = snd_hda_mixer_amp_tlv
2427};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002428EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002429
2430struct hda_ctl_ops snd_hda_bind_sw = {
2431 .info = snd_hda_mixer_amp_switch_info,
2432 .get = snd_hda_mixer_amp_switch_get,
2433 .put = snd_hda_mixer_amp_switch_put,
2434 .tlv = snd_hda_mixer_amp_tlv
2435};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002436EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002437
2438/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 * SPDIF out controls
2440 */
2441
Takashi Iwai0ba21762007-04-16 11:29:14 +02002442static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2443 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2446 uinfo->count = 1;
2447 return 0;
2448}
2449
Takashi Iwai0ba21762007-04-16 11:29:14 +02002450static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2451 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
2453 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2454 IEC958_AES0_NONAUDIO |
2455 IEC958_AES0_CON_EMPHASIS_5015 |
2456 IEC958_AES0_CON_NOT_COPYRIGHT;
2457 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2458 IEC958_AES1_CON_ORIGINAL;
2459 return 0;
2460}
2461
Takashi Iwai0ba21762007-04-16 11:29:14 +02002462static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2463 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2466 IEC958_AES0_NONAUDIO |
2467 IEC958_AES0_PRO_EMPHASIS_5015;
2468 return 0;
2469}
2470
Takashi Iwai0ba21762007-04-16 11:29:14 +02002471static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2472 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2475
2476 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2477 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2478 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2479 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2480
2481 return 0;
2482}
2483
2484/* convert from SPDIF status bits to HDA SPDIF bits
2485 * bit 0 (DigEn) is always set zero (to be filled later)
2486 */
2487static unsigned short convert_from_spdif_status(unsigned int sbits)
2488{
2489 unsigned short val = 0;
2490
2491 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002492 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002494 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002496 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2497 IEC958_AES0_PRO_EMPHASIS_5015)
2498 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002500 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2501 IEC958_AES0_CON_EMPHASIS_5015)
2502 val |= AC_DIG1_EMPHASIS;
2503 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2504 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002506 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2508 }
2509 return val;
2510}
2511
2512/* convert to SPDIF status bits from HDA SPDIF bits
2513 */
2514static unsigned int convert_to_spdif_status(unsigned short val)
2515{
2516 unsigned int sbits = 0;
2517
Takashi Iwai0ba21762007-04-16 11:29:14 +02002518 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002520 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 sbits |= IEC958_AES0_PROFESSIONAL;
2522 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002523 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2525 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002526 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002528 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002530 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2532 sbits |= val & (0x7f << 8);
2533 }
2534 return sbits;
2535}
2536
Takashi Iwai2f728532008-09-25 16:32:41 +02002537/* set digital convert verbs both for the given NID and its slaves */
2538static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2539 int verb, int val)
2540{
2541 hda_nid_t *d;
2542
Takashi Iwai9e976972008-11-25 08:17:20 +01002543 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002544 d = codec->slave_dig_outs;
2545 if (!d)
2546 return;
2547 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002548 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002549}
2550
2551static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2552 int dig1, int dig2)
2553{
2554 if (dig1 != -1)
2555 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2556 if (dig2 != -1)
2557 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2558}
2559
Takashi Iwai0ba21762007-04-16 11:29:14 +02002560static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2561 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
2563 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2564 hda_nid_t nid = kcontrol->private_value;
2565 unsigned short val;
2566 int change;
2567
Ingo Molnar62932df2006-01-16 16:34:20 +01002568 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 codec->spdif_status = ucontrol->value.iec958.status[0] |
2570 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2571 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2572 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2573 val = convert_from_spdif_status(codec->spdif_status);
2574 val |= codec->spdif_ctls & 1;
2575 change = codec->spdif_ctls != val;
2576 codec->spdif_ctls = val;
2577
Takashi Iwai2f728532008-09-25 16:32:41 +02002578 if (change)
2579 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Ingo Molnar62932df2006-01-16 16:34:20 +01002581 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return change;
2583}
2584
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002585#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Takashi Iwai0ba21762007-04-16 11:29:14 +02002587static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2588 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2591
Takashi Iwai0ba21762007-04-16 11:29:14 +02002592 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 return 0;
2594}
2595
Takashi Iwai0ba21762007-04-16 11:29:14 +02002596static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2597 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2600 hda_nid_t nid = kcontrol->private_value;
2601 unsigned short val;
2602 int change;
2603
Ingo Molnar62932df2006-01-16 16:34:20 +01002604 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002605 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002607 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002609 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002611 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002612 /* unmute amp switch (if any) */
2613 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002614 (val & AC_DIG1_ENABLE))
2615 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2616 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002618 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return change;
2620}
2621
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002622static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 {
2624 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2625 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002626 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 .info = snd_hda_spdif_mask_info,
2628 .get = snd_hda_spdif_cmask_get,
2629 },
2630 {
2631 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2632 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002633 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 .info = snd_hda_spdif_mask_info,
2635 .get = snd_hda_spdif_pmask_get,
2636 },
2637 {
2638 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002639 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 .info = snd_hda_spdif_mask_info,
2641 .get = snd_hda_spdif_default_get,
2642 .put = snd_hda_spdif_default_put,
2643 },
2644 {
2645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002646 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 .info = snd_hda_spdif_out_switch_info,
2648 .get = snd_hda_spdif_out_switch_get,
2649 .put = snd_hda_spdif_out_switch_put,
2650 },
2651 { } /* end */
2652};
2653
Takashi Iwai09f99702008-02-04 12:31:13 +01002654#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656/**
2657 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2658 * @codec: the HDA codec
2659 * @nid: audio out widget NID
2660 *
2661 * Creates controls related with the SPDIF output.
2662 * Called from each patch supporting the SPDIF out.
2663 *
2664 * Returns 0 if successful, or a negative error code.
2665 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002666int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002669 struct snd_kcontrol *kctl;
2670 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002671 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Takashi Iwai09f99702008-02-04 12:31:13 +01002673 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2674 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2675 idx))
2676 break;
2677 }
2678 if (idx >= SPDIF_MAX_IDX) {
2679 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2680 return -EBUSY;
2681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2683 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002684 if (!kctl)
2685 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002686 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002688 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002689 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 return err;
2691 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002692 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002693 snd_hda_codec_read(codec, nid, 0,
2694 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2696 return 0;
2697}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002698EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002701 * SPDIF sharing with analog output
2702 */
2703static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2704 struct snd_ctl_elem_value *ucontrol)
2705{
2706 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2707 ucontrol->value.integer.value[0] = mout->share_spdif;
2708 return 0;
2709}
2710
2711static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_value *ucontrol)
2713{
2714 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2715 mout->share_spdif = !!ucontrol->value.integer.value[0];
2716 return 0;
2717}
2718
2719static struct snd_kcontrol_new spdif_share_sw = {
2720 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2721 .name = "IEC958 Default PCM Playback Switch",
2722 .info = snd_ctl_boolean_mono_info,
2723 .get = spdif_share_sw_get,
2724 .put = spdif_share_sw_put,
2725};
2726
Takashi Iwaid5191e52009-11-16 14:58:17 +01002727/**
2728 * snd_hda_create_spdif_share_sw - create Default PCM switch
2729 * @codec: the HDA codec
2730 * @mout: multi-out instance
2731 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002732int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2733 struct hda_multi_out *mout)
2734{
2735 if (!mout->dig_out_nid)
2736 return 0;
2737 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002738 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2739 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002740}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002741EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002742
2743/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 * SPDIF input
2745 */
2746
2747#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2748
Takashi Iwai0ba21762007-04-16 11:29:14 +02002749static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2750 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2753
2754 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2755 return 0;
2756}
2757
Takashi Iwai0ba21762007-04-16 11:29:14 +02002758static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
2761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2762 hda_nid_t nid = kcontrol->private_value;
2763 unsigned int val = !!ucontrol->value.integer.value[0];
2764 int change;
2765
Ingo Molnar62932df2006-01-16 16:34:20 +01002766 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002768 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002770 snd_hda_codec_write_cache(codec, nid, 0,
2771 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002773 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return change;
2775}
2776
Takashi Iwai0ba21762007-04-16 11:29:14 +02002777static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2778 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
2780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2781 hda_nid_t nid = kcontrol->private_value;
2782 unsigned short val;
2783 unsigned int sbits;
2784
Andrew Paprocki3982d172007-12-19 12:13:44 +01002785 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 sbits = convert_to_spdif_status(val);
2787 ucontrol->value.iec958.status[0] = sbits;
2788 ucontrol->value.iec958.status[1] = sbits >> 8;
2789 ucontrol->value.iec958.status[2] = sbits >> 16;
2790 ucontrol->value.iec958.status[3] = sbits >> 24;
2791 return 0;
2792}
2793
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002794static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 {
2796 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002797 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 .info = snd_hda_spdif_in_switch_info,
2799 .get = snd_hda_spdif_in_switch_get,
2800 .put = snd_hda_spdif_in_switch_put,
2801 },
2802 {
2803 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2804 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002805 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 .info = snd_hda_spdif_mask_info,
2807 .get = snd_hda_spdif_in_status_get,
2808 },
2809 { } /* end */
2810};
2811
2812/**
2813 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2814 * @codec: the HDA codec
2815 * @nid: audio in widget NID
2816 *
2817 * Creates controls related with the SPDIF input.
2818 * Called from each patch supporting the SPDIF in.
2819 *
2820 * Returns 0 if successful, or a negative error code.
2821 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002822int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
2824 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002825 struct snd_kcontrol *kctl;
2826 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002827 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Takashi Iwai09f99702008-02-04 12:31:13 +01002829 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2830 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2831 idx))
2832 break;
2833 }
2834 if (idx >= SPDIF_MAX_IDX) {
2835 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2836 return -EBUSY;
2837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2839 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002840 if (!kctl)
2841 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002843 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002844 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return err;
2846 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002847 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002848 snd_hda_codec_read(codec, nid, 0,
2849 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002850 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return 0;
2852}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002853EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Takashi Iwaicb53c622007-08-10 17:21:45 +02002855#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002856/*
2857 * command cache
2858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002860/* build a 32bit cache key with the widget id and the command parameter */
2861#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2862#define get_cmd_cache_nid(key) ((key) & 0xff)
2863#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2864
2865/**
2866 * snd_hda_codec_write_cache - send a single command with caching
2867 * @codec: the HDA codec
2868 * @nid: NID to send the command
2869 * @direct: direct flag
2870 * @verb: the verb to send
2871 * @parm: the parameter for the verb
2872 *
2873 * Send a single command without waiting for response.
2874 *
2875 * Returns 0 if successful, or a negative error code.
2876 */
2877int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2878 int direct, unsigned int verb, unsigned int parm)
2879{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002880 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2881 struct hda_cache_head *c;
2882 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002883
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002884 if (err < 0)
2885 return err;
2886 /* parm may contain the verb stuff for get/set amp */
2887 verb = verb | (parm >> 8);
2888 parm &= 0xff;
2889 key = build_cmd_cache_key(nid, verb);
2890 mutex_lock(&codec->bus->cmd_mutex);
2891 c = get_alloc_hash(&codec->cmd_cache, key);
2892 if (c)
2893 c->val = parm;
2894 mutex_unlock(&codec->bus->cmd_mutex);
2895 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002896}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002897EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002898
Takashi Iwaid5191e52009-11-16 14:58:17 +01002899/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002900 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2901 * @codec: the HDA codec
2902 * @nid: NID to send the command
2903 * @direct: direct flag
2904 * @verb: the verb to send
2905 * @parm: the parameter for the verb
2906 *
2907 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2908 * command if the parameter is already identical with the cached value.
2909 * If not, it sends the command and refreshes the cache.
2910 *
2911 * Returns 0 if successful, or a negative error code.
2912 */
2913int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2914 int direct, unsigned int verb, unsigned int parm)
2915{
2916 struct hda_cache_head *c;
2917 u32 key;
2918
2919 /* parm may contain the verb stuff for get/set amp */
2920 verb = verb | (parm >> 8);
2921 parm &= 0xff;
2922 key = build_cmd_cache_key(nid, verb);
2923 mutex_lock(&codec->bus->cmd_mutex);
2924 c = get_hash(&codec->cmd_cache, key);
2925 if (c && c->val == parm) {
2926 mutex_unlock(&codec->bus->cmd_mutex);
2927 return 0;
2928 }
2929 mutex_unlock(&codec->bus->cmd_mutex);
2930 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2931}
2932EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2933
2934/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002935 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2936 * @codec: HD-audio codec
2937 *
2938 * Execute all verbs recorded in the command caches to resume.
2939 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002940void snd_hda_codec_resume_cache(struct hda_codec *codec)
2941{
Takashi Iwai603c4012008-07-30 15:01:44 +02002942 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002943 int i;
2944
Takashi Iwai603c4012008-07-30 15:01:44 +02002945 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002946 u32 key = buffer->key;
2947 if (!key)
2948 continue;
2949 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2950 get_cmd_cache_cmd(key), buffer->val);
2951 }
2952}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002953EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002954
2955/**
2956 * snd_hda_sequence_write_cache - sequence writes with caching
2957 * @codec: the HDA codec
2958 * @seq: VERB array to send
2959 *
2960 * Send the commands sequentially from the given array.
2961 * Thte commands are recorded on cache for power-save and resume.
2962 * The array must be terminated with NID=0.
2963 */
2964void snd_hda_sequence_write_cache(struct hda_codec *codec,
2965 const struct hda_verb *seq)
2966{
2967 for (; seq->nid; seq++)
2968 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2969 seq->param);
2970}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002971EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002972#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002973
Takashi Iwai54d17402005-11-21 16:33:22 +01002974/*
2975 * set power state of the codec
2976 */
2977static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2978 unsigned int power_state)
2979{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002980 hda_nid_t nid;
2981 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002982
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002983 /* this delay seems necessary to avoid click noise at power-down */
2984 if (power_state == AC_PWRST_D3)
2985 msleep(100);
2986 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002987 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002988 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002989 if (power_state == AC_PWRST_D0 &&
2990 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002991 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002992
Takashi Iwaicb53c622007-08-10 17:21:45 +02002993 nid = codec->start_nid;
2994 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002995 unsigned int wcaps = get_wcaps(codec, nid);
2996 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002997 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002998 if (power_state == AC_PWRST_D3 &&
2999 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003000 unsigned int pincap;
3001 /*
3002 * don't power down the widget if it controls
3003 * eapd and EAPD_BTLENABLE is set.
3004 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003005 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003006 if (pincap & AC_PINCAP_EAPD) {
3007 int eapd = snd_hda_codec_read(codec,
3008 nid, 0,
3009 AC_VERB_GET_EAPD_BTLENABLE, 0);
3010 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003011 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003012 continue;
3013 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003014 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003015 snd_hda_codec_write(codec, nid, 0,
3016 AC_VERB_SET_POWER_STATE,
3017 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003018 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003019 }
3020
Takashi Iwaicb53c622007-08-10 17:21:45 +02003021 if (power_state == AC_PWRST_D0) {
3022 unsigned long end_time;
3023 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003024 /* wait until the codec reachs to D0 */
3025 end_time = jiffies + msecs_to_jiffies(500);
3026 do {
3027 state = snd_hda_codec_read(codec, fg, 0,
3028 AC_VERB_GET_POWER_STATE, 0);
3029 if (state == power_state)
3030 break;
3031 msleep(1);
3032 } while (time_after_eq(end_time, jiffies));
3033 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003034}
3035
Takashi Iwai11aeff02008-07-30 15:01:46 +02003036#ifdef CONFIG_SND_HDA_HWDEP
3037/* execute additional init verbs */
3038static void hda_exec_init_verbs(struct hda_codec *codec)
3039{
3040 if (codec->init_verbs.list)
3041 snd_hda_sequence_write(codec, codec->init_verbs.list);
3042}
3043#else
3044static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3045#endif
3046
Takashi Iwaicb53c622007-08-10 17:21:45 +02003047#ifdef SND_HDA_NEEDS_RESUME
3048/*
3049 * call suspend and power-down; used both from PM and power-save
3050 */
3051static void hda_call_codec_suspend(struct hda_codec *codec)
3052{
3053 if (codec->patch_ops.suspend)
3054 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003055 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003056 hda_set_power_state(codec,
3057 codec->afg ? codec->afg : codec->mfg,
3058 AC_PWRST_D3);
3059#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003060 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003061 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003062 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003063 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003064 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003065#endif
3066}
3067
3068/*
3069 * kick up codec; used both from PM and power-save
3070 */
3071static void hda_call_codec_resume(struct hda_codec *codec)
3072{
3073 hda_set_power_state(codec,
3074 codec->afg ? codec->afg : codec->mfg,
3075 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003076 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003077 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003078 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003079 if (codec->patch_ops.resume)
3080 codec->patch_ops.resume(codec);
3081 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003082 if (codec->patch_ops.init)
3083 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003084 snd_hda_codec_resume_amp(codec);
3085 snd_hda_codec_resume_cache(codec);
3086 }
3087}
3088#endif /* SND_HDA_NEEDS_RESUME */
3089
Takashi Iwai54d17402005-11-21 16:33:22 +01003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091/**
3092 * snd_hda_build_controls - build mixer controls
3093 * @bus: the BUS
3094 *
3095 * Creates mixer controls for each codec included in the bus.
3096 *
3097 * Returns 0 if successful, otherwise a negative error code.
3098 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003099int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003101 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Takashi Iwai0ba21762007-04-16 11:29:14 +02003103 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003104 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003105 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003106 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003107 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003108 err = snd_hda_codec_reset(codec);
3109 if (err < 0) {
3110 printk(KERN_ERR
3111 "hda_codec: cannot revert codec\n");
3112 return err;
3113 }
3114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003116 return 0;
3117}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003118EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003120int snd_hda_codec_build_controls(struct hda_codec *codec)
3121{
3122 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003123 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003124 /* continue to initialize... */
3125 if (codec->patch_ops.init)
3126 err = codec->patch_ops.init(codec);
3127 if (!err && codec->patch_ops.build_controls)
3128 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003129 if (err < 0)
3130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return 0;
3132}
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134/*
3135 * stream formats
3136 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003137struct hda_rate_tbl {
3138 unsigned int hz;
3139 unsigned int alsa_bits;
3140 unsigned int hda_fmt;
3141};
3142
Takashi Iwai92f10b32010-08-03 14:21:00 +02003143/* rate = base * mult / div */
3144#define HDA_RATE(base, mult, div) \
3145 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3146 (((div) - 1) << AC_FMT_DIV_SHIFT))
3147
Takashi Iwaibefdf312005-08-22 13:57:55 +02003148static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003150
3151 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003152 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3153 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3154 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3155 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3156 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3157 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3158 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3159 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3160 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3161 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3162 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003163#define AC_PAR_PCM_RATE_BITS 11
3164 /* up to bits 10, 384kHZ isn't supported properly */
3165
3166 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003167 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003168
Takashi Iwaibefdf312005-08-22 13:57:55 +02003169 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170};
3171
3172/**
3173 * snd_hda_calc_stream_format - calculate format bitset
3174 * @rate: the sample rate
3175 * @channels: the number of channels
3176 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3177 * @maxbps: the max. bps
3178 *
3179 * Calculate the format bitset from the given rate, channels and th PCM format.
3180 *
3181 * Return zero if invalid.
3182 */
3183unsigned int snd_hda_calc_stream_format(unsigned int rate,
3184 unsigned int channels,
3185 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003186 unsigned int maxbps,
3187 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
3189 int i;
3190 unsigned int val = 0;
3191
Takashi Iwaibefdf312005-08-22 13:57:55 +02003192 for (i = 0; rate_bits[i].hz; i++)
3193 if (rate_bits[i].hz == rate) {
3194 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 break;
3196 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003197 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 snd_printdd("invalid rate %d\n", rate);
3199 return 0;
3200 }
3201
3202 if (channels == 0 || channels > 8) {
3203 snd_printdd("invalid channels %d\n", channels);
3204 return 0;
3205 }
3206 val |= channels - 1;
3207
3208 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003209 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003210 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003211 break;
3212 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003213 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 case 20:
3216 case 24:
3217 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003218 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003219 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003221 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003223 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 break;
3225 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003226 snd_printdd("invalid format width %d\n",
3227 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return 0;
3229 }
3230
Anssi Hannula32c168c2010-08-03 13:28:57 +03003231 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003232 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return val;
3235}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003236EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003238static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3239{
3240 unsigned int val = 0;
3241 if (nid != codec->afg &&
3242 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3243 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3244 if (!val || val == -1)
3245 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3246 if (!val || val == -1)
3247 return 0;
3248 return val;
3249}
3250
3251static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3252{
3253 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3254 get_pcm_param);
3255}
3256
3257static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3258{
3259 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3260 if (!streams || streams == -1)
3261 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3262 if (!streams || streams == -1)
3263 return 0;
3264 return streams;
3265}
3266
3267static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3268{
3269 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3270 get_stream_param);
3271}
3272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273/**
3274 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3275 * @codec: the HDA codec
3276 * @nid: NID to query
3277 * @ratesp: the pointer to store the detected rate bitflags
3278 * @formatsp: the pointer to store the detected formats
3279 * @bpsp: the pointer to store the detected format widths
3280 *
3281 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3282 * or @bsps argument is ignored.
3283 *
3284 * Returns 0 if successful, otherwise a negative error code.
3285 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003286static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3288{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003289 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003291 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003292 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 if (ratesp) {
3295 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003296 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003298 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003300 if (rates == 0) {
3301 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3302 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3303 nid, val,
3304 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3305 return -EIO;
3306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 *ratesp = rates;
3308 }
3309
3310 if (formatsp || bpsp) {
3311 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003312 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003314 streams = query_stream_param(codec, nid);
3315 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 bps = 0;
3319 if (streams & AC_SUPFMT_PCM) {
3320 if (val & AC_SUPPCM_BITS_8) {
3321 formats |= SNDRV_PCM_FMTBIT_U8;
3322 bps = 8;
3323 }
3324 if (val & AC_SUPPCM_BITS_16) {
3325 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3326 bps = 16;
3327 }
3328 if (wcaps & AC_WCAP_DIGITAL) {
3329 if (val & AC_SUPPCM_BITS_32)
3330 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3331 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3332 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3333 if (val & AC_SUPPCM_BITS_24)
3334 bps = 24;
3335 else if (val & AC_SUPPCM_BITS_20)
3336 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003337 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3338 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3340 if (val & AC_SUPPCM_BITS_32)
3341 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 else if (val & AC_SUPPCM_BITS_24)
3343 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003344 else if (val & AC_SUPPCM_BITS_20)
3345 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 }
3347 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003348 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003350 if (!bps)
3351 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003352 }
3353 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003354 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 /* temporary hack: we have still no proper support
3356 * for the direct AC3 stream...
3357 */
3358 formats |= SNDRV_PCM_FMTBIT_U8;
3359 bps = 8;
3360 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003361 if (formats == 0) {
3362 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3363 "(nid=0x%x, val=0x%x, ovrd=%i, "
3364 "streams=0x%x)\n",
3365 nid, val,
3366 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3367 streams);
3368 return -EIO;
3369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (formatsp)
3371 *formatsp = formats;
3372 if (bpsp)
3373 *bpsp = bps;
3374 }
3375
3376 return 0;
3377}
3378
3379/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003380 * snd_hda_is_supported_format - Check the validity of the format
3381 * @codec: HD-audio codec
3382 * @nid: NID to check
3383 * @format: the HD-audio format value to check
3384 *
3385 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 *
3387 * Returns 1 if supported, 0 if not.
3388 */
3389int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3390 unsigned int format)
3391{
3392 int i;
3393 unsigned int val = 0, rate, stream;
3394
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003395 val = query_pcm_param(codec, nid);
3396 if (!val)
3397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
3399 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003400 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003401 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 if (val & (1 << i))
3403 break;
3404 return 0;
3405 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003406 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return 0;
3408
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003409 stream = query_stream_param(codec, nid);
3410 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return 0;
3412
3413 if (stream & AC_SUPFMT_PCM) {
3414 switch (format & 0xf0) {
3415 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003416 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 return 0;
3418 break;
3419 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003420 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return 0;
3422 break;
3423 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003424 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return 0;
3426 break;
3427 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003428 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return 0;
3430 break;
3431 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003432 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return 0;
3434 break;
3435 default:
3436 return 0;
3437 }
3438 } else {
3439 /* FIXME: check for float32 and AC3? */
3440 }
3441
3442 return 1;
3443}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003444EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
3446/*
3447 * PCM stuff
3448 */
3449static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3450 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003451 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
3453 return 0;
3454}
3455
3456static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3457 struct hda_codec *codec,
3458 unsigned int stream_tag,
3459 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003460 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
3462 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3463 return 0;
3464}
3465
3466static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3467 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003468 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
Takashi Iwai888afa12008-03-18 09:57:50 +01003470 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 return 0;
3472}
3473
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003474static int set_pcm_default_values(struct hda_codec *codec,
3475 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003477 int err;
3478
Takashi Iwai0ba21762007-04-16 11:29:14 +02003479 /* query support PCM information from the given NID */
3480 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003481 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003482 info->rates ? NULL : &info->rates,
3483 info->formats ? NULL : &info->formats,
3484 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003485 if (err < 0)
3486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
3488 if (info->ops.open == NULL)
3489 info->ops.open = hda_pcm_default_open_close;
3490 if (info->ops.close == NULL)
3491 info->ops.close = hda_pcm_default_open_close;
3492 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003493 if (snd_BUG_ON(!info->nid))
3494 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 info->ops.prepare = hda_pcm_default_prepare;
3496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003498 if (snd_BUG_ON(!info->nid))
3499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 info->ops.cleanup = hda_pcm_default_cleanup;
3501 }
3502 return 0;
3503}
3504
Takashi Iwaieb541332010-08-06 13:48:11 +02003505/*
3506 * codec prepare/cleanup entries
3507 */
3508int snd_hda_codec_prepare(struct hda_codec *codec,
3509 struct hda_pcm_stream *hinfo,
3510 unsigned int stream,
3511 unsigned int format,
3512 struct snd_pcm_substream *substream)
3513{
3514 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003515 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003516 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3517 if (ret >= 0)
3518 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003519 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003520 return ret;
3521}
3522EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3523
3524void snd_hda_codec_cleanup(struct hda_codec *codec,
3525 struct hda_pcm_stream *hinfo,
3526 struct snd_pcm_substream *substream)
3527{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003528 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003529 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003530 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003531}
3532EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3533
Takashi Iwaid5191e52009-11-16 14:58:17 +01003534/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003535const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3536 "Audio", "SPDIF", "HDMI", "Modem"
3537};
3538
Takashi Iwai176d5332008-07-30 15:01:44 +02003539/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003540 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003541 *
3542 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003543 */
3544static int get_empty_pcm_device(struct hda_bus *bus, int type)
3545{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003546 /* audio device indices; not linear to keep compatibility */
3547 static int audio_idx[HDA_PCM_NTYPES][5] = {
3548 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3549 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003550 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003551 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003552 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003553 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003554
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003555 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003556 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3557 return -EINVAL;
3558 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003559
3560 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3561 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3562 return audio_idx[type][i];
3563
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003564 snd_printk(KERN_WARNING "Too many %s devices\n",
3565 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003566 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003567}
3568
3569/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003570 * attach a new PCM stream
3571 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003572static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003573{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003574 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003575 struct hda_pcm_stream *info;
3576 int stream, err;
3577
Takashi Iwaib91f0802008-11-04 08:43:08 +01003578 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003579 return -EINVAL;
3580 for (stream = 0; stream < 2; stream++) {
3581 info = &pcm->stream[stream];
3582 if (info->substreams) {
3583 err = set_pcm_default_values(codec, info);
3584 if (err < 0)
3585 return err;
3586 }
3587 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003588 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003589}
3590
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003591/* assign all PCMs of the given codec */
3592int snd_hda_codec_build_pcms(struct hda_codec *codec)
3593{
3594 unsigned int pcm;
3595 int err;
3596
3597 if (!codec->num_pcms) {
3598 if (!codec->patch_ops.build_pcms)
3599 return 0;
3600 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003601 if (err < 0) {
3602 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003603 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003604 err = snd_hda_codec_reset(codec);
3605 if (err < 0) {
3606 printk(KERN_ERR
3607 "hda_codec: cannot revert codec\n");
3608 return err;
3609 }
3610 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003611 }
3612 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3613 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3614 int dev;
3615
3616 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003617 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003618
3619 if (!cpcm->pcm) {
3620 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3621 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003622 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003623 cpcm->device = dev;
3624 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003625 if (err < 0) {
3626 printk(KERN_ERR "hda_codec: cannot attach "
3627 "PCM stream %d for codec #%d\n",
3628 dev, codec->addr);
3629 continue; /* no fatal error */
3630 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003631 }
3632 }
3633 return 0;
3634}
3635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636/**
3637 * snd_hda_build_pcms - build PCM information
3638 * @bus: the BUS
3639 *
3640 * Create PCM information for each codec included in the bus.
3641 *
3642 * The build_pcms codec patch is requested to set up codec->num_pcms and
3643 * codec->pcm_info properly. The array is referred by the top-level driver
3644 * to create its PCM instances.
3645 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3646 * callback.
3647 *
3648 * At least, substreams, channels_min and channels_max must be filled for
3649 * each stream. substreams = 0 indicates that the stream doesn't exist.
3650 * When rates and/or formats are zero, the supported values are queried
3651 * from the given nid. The nid is used also by the default ops.prepare
3652 * and ops.cleanup callbacks.
3653 *
3654 * The driver needs to call ops.open in its open callback. Similarly,
3655 * ops.close is supposed to be called in the close callback.
3656 * ops.prepare should be called in the prepare or hw_params callback
3657 * with the proper parameters for set up.
3658 * ops.cleanup should be called in hw_free for clean up of streams.
3659 *
3660 * This function returns 0 if successfull, or a negative error code.
3661 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003662int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003664 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Takashi Iwai0ba21762007-04-16 11:29:14 +02003666 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003667 int err = snd_hda_codec_build_pcms(codec);
3668 if (err < 0)
3669 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
3671 return 0;
3672}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003673EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675/**
3676 * snd_hda_check_board_config - compare the current codec with the config table
3677 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003678 * @num_configs: number of config enums
3679 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 * @tbl: configuration table, terminated by null entries
3681 *
3682 * Compares the modelname or PCI subsystem id of the current codec with the
3683 * given configuration table. If a matching entry is found, returns its
3684 * config value (supposed to be 0 or positive).
3685 *
3686 * If no entries are matching, the function returns a negative value.
3687 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003688int snd_hda_check_board_config(struct hda_codec *codec,
3689 int num_configs, const char **models,
3690 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003692 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003693 int i;
3694 for (i = 0; i < num_configs; i++) {
3695 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003696 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003697 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3698 "selected\n", models[i]);
3699 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 }
3701 }
3702 }
3703
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003704 if (!codec->bus->pci || !tbl)
3705 return -1;
3706
3707 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3708 if (!tbl)
3709 return -1;
3710 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003711#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003712 char tmp[10];
3713 const char *model = NULL;
3714 if (models)
3715 model = models[tbl->value];
3716 if (!model) {
3717 sprintf(tmp, "#%d", tbl->value);
3718 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003720 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3721 "for config %x:%x (%s)\n",
3722 model, tbl->subvendor, tbl->subdevice,
3723 (tbl->name ? tbl->name : "Unknown device"));
3724#endif
3725 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 }
3727 return -1;
3728}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003729EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
3731/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003732 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003733 subsystem ID with the
3734 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003735
3736 This is important for Gateway notebooks with SB450 HDA Audio
3737 where the vendor ID of the PCI device is:
3738 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3739 and the vendor/subvendor are found only at the codec.
3740
3741 * @codec: the HDA codec
3742 * @num_configs: number of config enums
3743 * @models: array of model name strings
3744 * @tbl: configuration table, terminated by null entries
3745 *
3746 * Compares the modelname or PCI subsystem id of the current codec with the
3747 * given configuration table. If a matching entry is found, returns its
3748 * config value (supposed to be 0 or positive).
3749 *
3750 * If no entries are matching, the function returns a negative value.
3751 */
3752int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3753 int num_configs, const char **models,
3754 const struct snd_pci_quirk *tbl)
3755{
3756 const struct snd_pci_quirk *q;
3757
3758 /* Search for codec ID */
3759 for (q = tbl; q->subvendor; q++) {
3760 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3761
3762 if (vendorid == codec->subsystem_id)
3763 break;
3764 }
3765
3766 if (!q->subvendor)
3767 return -1;
3768
3769 tbl = q;
3770
3771 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003772#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003773 char tmp[10];
3774 const char *model = NULL;
3775 if (models)
3776 model = models[tbl->value];
3777 if (!model) {
3778 sprintf(tmp, "#%d", tbl->value);
3779 model = tmp;
3780 }
3781 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3782 "for config %x:%x (%s)\n",
3783 model, tbl->subvendor, tbl->subdevice,
3784 (tbl->name ? tbl->name : "Unknown device"));
3785#endif
3786 return tbl->value;
3787 }
3788 return -1;
3789}
3790EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3791
3792/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 * snd_hda_add_new_ctls - create controls from the array
3794 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003795 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 *
3797 * This helper function creates and add new controls in the given array.
3798 * The array must be terminated with an empty entry as terminator.
3799 *
3800 * Returns 0 if successful, or a negative error code.
3801 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003802int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003804 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
3806 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003807 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003808 if (knew->iface == -1) /* skip this codec private value */
3809 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003810 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003811 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003812 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003813 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003814 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003815 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003816 return err;
3817 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003818 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003819 return -ENOMEM;
3820 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003821 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003822 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003823 return err;
3824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
3826 return 0;
3827}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003828EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Takashi Iwaicb53c622007-08-10 17:21:45 +02003830#ifdef CONFIG_SND_HDA_POWER_SAVE
3831static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3832 unsigned int power_state);
3833
3834static void hda_power_work(struct work_struct *work)
3835{
3836 struct hda_codec *codec =
3837 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003838 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003839
Maxim Levitsky2e492462007-09-03 15:26:57 +02003840 if (!codec->power_on || codec->power_count) {
3841 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003842 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003843 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003844
3845 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003846 if (bus->ops.pm_notify)
3847 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003848}
3849
3850static void hda_keep_power_on(struct hda_codec *codec)
3851{
3852 codec->power_count++;
3853 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003854 codec->power_jiffies = jiffies;
3855}
3856
Takashi Iwaid5191e52009-11-16 14:58:17 +01003857/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003858void snd_hda_update_power_acct(struct hda_codec *codec)
3859{
3860 unsigned long delta = jiffies - codec->power_jiffies;
3861 if (codec->power_on)
3862 codec->power_on_acct += delta;
3863 else
3864 codec->power_off_acct += delta;
3865 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003866}
3867
Takashi Iwaid5191e52009-11-16 14:58:17 +01003868/**
3869 * snd_hda_power_up - Power-up the codec
3870 * @codec: HD-audio codec
3871 *
3872 * Increment the power-up counter and power up the hardware really when
3873 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003874 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003875void snd_hda_power_up(struct hda_codec *codec)
3876{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003877 struct hda_bus *bus = codec->bus;
3878
Takashi Iwaicb53c622007-08-10 17:21:45 +02003879 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003880 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003881 return;
3882
Takashi Iwaia2f63092009-11-11 09:34:25 +01003883 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003884 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003885 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003886 if (bus->ops.pm_notify)
3887 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003888 hda_call_codec_resume(codec);
3889 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003890 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003891}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003892EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003893
3894#define power_save(codec) \
3895 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003896
Takashi Iwaid5191e52009-11-16 14:58:17 +01003897/**
3898 * snd_hda_power_down - Power-down the codec
3899 * @codec: HD-audio codec
3900 *
3901 * Decrement the power-up counter and schedules the power-off work if
3902 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003903 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003904void snd_hda_power_down(struct hda_codec *codec)
3905{
3906 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003907 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003908 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003909 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003910 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003911 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003912 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003913 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003914}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003915EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003916
Takashi Iwaid5191e52009-11-16 14:58:17 +01003917/**
3918 * snd_hda_check_amp_list_power - Check the amp list and update the power
3919 * @codec: HD-audio codec
3920 * @check: the object containing an AMP list and the status
3921 * @nid: NID to check / update
3922 *
3923 * Check whether the given NID is in the amp list. If it's in the list,
3924 * check the current AMP status, and update the the power-status according
3925 * to the mute status.
3926 *
3927 * This function is supposed to be set or called from the check_power_status
3928 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003929 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003930int snd_hda_check_amp_list_power(struct hda_codec *codec,
3931 struct hda_loopback_check *check,
3932 hda_nid_t nid)
3933{
3934 struct hda_amp_list *p;
3935 int ch, v;
3936
3937 if (!check->amplist)
3938 return 0;
3939 for (p = check->amplist; p->nid; p++) {
3940 if (p->nid == nid)
3941 break;
3942 }
3943 if (!p->nid)
3944 return 0; /* nothing changed */
3945
3946 for (p = check->amplist; p->nid; p++) {
3947 for (ch = 0; ch < 2; ch++) {
3948 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3949 p->idx);
3950 if (!(v & HDA_AMP_MUTE) && v > 0) {
3951 if (!check->power_on) {
3952 check->power_on = 1;
3953 snd_hda_power_up(codec);
3954 }
3955 return 1;
3956 }
3957 }
3958 }
3959 if (check->power_on) {
3960 check->power_on = 0;
3961 snd_hda_power_down(codec);
3962 }
3963 return 0;
3964}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003965EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003968/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003969 * Channel mode helper
3970 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003971
3972/**
3973 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3974 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003975int snd_hda_ch_mode_info(struct hda_codec *codec,
3976 struct snd_ctl_elem_info *uinfo,
3977 const struct hda_channel_mode *chmode,
3978 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003979{
3980 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3981 uinfo->count = 1;
3982 uinfo->value.enumerated.items = num_chmodes;
3983 if (uinfo->value.enumerated.item >= num_chmodes)
3984 uinfo->value.enumerated.item = num_chmodes - 1;
3985 sprintf(uinfo->value.enumerated.name, "%dch",
3986 chmode[uinfo->value.enumerated.item].channels);
3987 return 0;
3988}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003989EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003990
Takashi Iwaid5191e52009-11-16 14:58:17 +01003991/**
3992 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3993 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003994int snd_hda_ch_mode_get(struct hda_codec *codec,
3995 struct snd_ctl_elem_value *ucontrol,
3996 const struct hda_channel_mode *chmode,
3997 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003998 int max_channels)
3999{
4000 int i;
4001
4002 for (i = 0; i < num_chmodes; i++) {
4003 if (max_channels == chmode[i].channels) {
4004 ucontrol->value.enumerated.item[0] = i;
4005 break;
4006 }
4007 }
4008 return 0;
4009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004010EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004011
Takashi Iwaid5191e52009-11-16 14:58:17 +01004012/**
4013 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4014 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004015int snd_hda_ch_mode_put(struct hda_codec *codec,
4016 struct snd_ctl_elem_value *ucontrol,
4017 const struct hda_channel_mode *chmode,
4018 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004019 int *max_channelsp)
4020{
4021 unsigned int mode;
4022
4023 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004024 if (mode >= num_chmodes)
4025 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004026 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004027 return 0;
4028 /* change the current channel setting */
4029 *max_channelsp = chmode[mode].channels;
4030 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004031 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004032 return 1;
4033}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004034EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036/*
4037 * input MUX helper
4038 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004039
4040/**
4041 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4042 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004043int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4044 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
4046 unsigned int index;
4047
4048 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4049 uinfo->count = 1;
4050 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004051 if (!imux->num_items)
4052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 index = uinfo->value.enumerated.item;
4054 if (index >= imux->num_items)
4055 index = imux->num_items - 1;
4056 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4057 return 0;
4058}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004059EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Takashi Iwaid5191e52009-11-16 14:58:17 +01004061/**
4062 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4063 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004064int snd_hda_input_mux_put(struct hda_codec *codec,
4065 const struct hda_input_mux *imux,
4066 struct snd_ctl_elem_value *ucontrol,
4067 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 unsigned int *cur_val)
4069{
4070 unsigned int idx;
4071
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004072 if (!imux->num_items)
4073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 idx = ucontrol->value.enumerated.item[0];
4075 if (idx >= imux->num_items)
4076 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004077 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004079 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4080 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 *cur_val = idx;
4082 return 1;
4083}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004084EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
4086
4087/*
4088 * Multi-channel / digital-out PCM helper functions
4089 */
4090
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004091/* setup SPDIF output stream */
4092static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4093 unsigned int stream_tag, unsigned int format)
4094{
4095 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02004096 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004097 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02004098 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4099 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004100 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004101 if (codec->slave_dig_outs) {
4102 hda_nid_t *d;
4103 for (d = codec->slave_dig_outs; *d; d++)
4104 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4105 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004106 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004107 /* turn on again (if needed) */
4108 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4109 set_dig_out_convert(codec, nid,
4110 codec->spdif_ctls & 0xff, -1);
4111}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004112
Takashi Iwai2f728532008-09-25 16:32:41 +02004113static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4114{
4115 snd_hda_codec_cleanup_stream(codec, nid);
4116 if (codec->slave_dig_outs) {
4117 hda_nid_t *d;
4118 for (d = codec->slave_dig_outs; *d; d++)
4119 snd_hda_codec_cleanup_stream(codec, *d);
4120 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004121}
4122
Takashi Iwaid5191e52009-11-16 14:58:17 +01004123/**
4124 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4125 * @bus: HD-audio bus
4126 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004127void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4128{
4129 struct hda_codec *codec;
4130
4131 if (!bus)
4132 return;
4133 list_for_each_entry(codec, &bus->codec_list, list) {
4134#ifdef CONFIG_SND_HDA_POWER_SAVE
4135 if (!codec->power_on)
4136 continue;
4137#endif
4138 if (codec->patch_ops.reboot_notify)
4139 codec->patch_ops.reboot_notify(codec);
4140 }
4141}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004142EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004143
Takashi Iwaid5191e52009-11-16 14:58:17 +01004144/**
4145 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004147int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4148 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
Ingo Molnar62932df2006-01-16 16:34:20 +01004150 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004151 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4152 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004153 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004155 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return 0;
4157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004158EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Takashi Iwaid5191e52009-11-16 14:58:17 +01004160/**
4161 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4162 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004163int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4164 struct hda_multi_out *mout,
4165 unsigned int stream_tag,
4166 unsigned int format,
4167 struct snd_pcm_substream *substream)
4168{
4169 mutex_lock(&codec->spdif_mutex);
4170 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4171 mutex_unlock(&codec->spdif_mutex);
4172 return 0;
4173}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004174EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004175
Takashi Iwaid5191e52009-11-16 14:58:17 +01004176/**
4177 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4178 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004179int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4180 struct hda_multi_out *mout)
4181{
4182 mutex_lock(&codec->spdif_mutex);
4183 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4184 mutex_unlock(&codec->spdif_mutex);
4185 return 0;
4186}
4187EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4188
Takashi Iwaid5191e52009-11-16 14:58:17 +01004189/**
4190 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004192int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4193 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
Ingo Molnar62932df2006-01-16 16:34:20 +01004195 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004197 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 return 0;
4199}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004200EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Takashi Iwaid5191e52009-11-16 14:58:17 +01004202/**
4203 * snd_hda_multi_out_analog_open - open analog outputs
4204 *
4205 * Open analog outputs and set up the hw-constraints.
4206 * If the digital outputs can be opened as slave, open the digital
4207 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004209int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4210 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004211 struct snd_pcm_substream *substream,
4212 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213{
Takashi Iwai9a081602008-02-12 18:37:26 +01004214 struct snd_pcm_runtime *runtime = substream->runtime;
4215 runtime->hw.channels_max = mout->max_channels;
4216 if (mout->dig_out_nid) {
4217 if (!mout->analog_rates) {
4218 mout->analog_rates = hinfo->rates;
4219 mout->analog_formats = hinfo->formats;
4220 mout->analog_maxbps = hinfo->maxbps;
4221 } else {
4222 runtime->hw.rates = mout->analog_rates;
4223 runtime->hw.formats = mout->analog_formats;
4224 hinfo->maxbps = mout->analog_maxbps;
4225 }
4226 if (!mout->spdif_rates) {
4227 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4228 &mout->spdif_rates,
4229 &mout->spdif_formats,
4230 &mout->spdif_maxbps);
4231 }
4232 mutex_lock(&codec->spdif_mutex);
4233 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004234 if ((runtime->hw.rates & mout->spdif_rates) &&
4235 (runtime->hw.formats & mout->spdif_formats)) {
4236 runtime->hw.rates &= mout->spdif_rates;
4237 runtime->hw.formats &= mout->spdif_formats;
4238 if (mout->spdif_maxbps < hinfo->maxbps)
4239 hinfo->maxbps = mout->spdif_maxbps;
4240 } else {
4241 mout->share_spdif = 0;
4242 /* FIXME: need notify? */
4243 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004244 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004245 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4248 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4249}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004250EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Takashi Iwaid5191e52009-11-16 14:58:17 +01004252/**
4253 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4254 *
4255 * Set up the i/o for analog out.
4256 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004258int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4259 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 unsigned int stream_tag,
4261 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004262 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
4264 hda_nid_t *nids = mout->dac_nids;
4265 int chs = substream->runtime->channels;
4266 int i;
4267
Ingo Molnar62932df2006-01-16 16:34:20 +01004268 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004269 if (mout->dig_out_nid && mout->share_spdif &&
4270 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004272 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4273 format) &&
4274 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004276 setup_dig_out_stream(codec, mout->dig_out_nid,
4277 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 } else {
4279 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004280 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 }
4282 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004283 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004286 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4287 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004288 if (!mout->no_share_stream &&
4289 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004291 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4292 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004293 /* extra outputs copied from front */
4294 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004295 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004296 snd_hda_codec_setup_stream(codec,
4297 mout->extra_out_nid[i],
4298 stream_tag, 0, format);
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 /* surrounds */
4301 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004302 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004303 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4304 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004305 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004306 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4307 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 }
4309 return 0;
4310}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004311EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Takashi Iwaid5191e52009-11-16 14:58:17 +01004313/**
4314 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004316int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4317 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
4319 hda_nid_t *nids = mout->dac_nids;
4320 int i;
4321
4322 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004323 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004325 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004326 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4327 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004328 snd_hda_codec_cleanup_stream(codec,
4329 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004330 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004332 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 mout->dig_out_used = 0;
4334 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004335 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 return 0;
4337}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004338EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004340/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004341 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004342 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004343
Takashi Iwai12f288b2007-08-02 15:51:59 +02004344static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004345{
4346 for (; *list; list++)
4347 if (*list == nid)
4348 return 1;
4349 return 0;
4350}
4351
Steve Longerbeam81937d32007-05-08 15:33:03 +02004352
4353/*
4354 * Sort an associated group of pins according to their sequence numbers.
4355 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004356static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004357 int num_pins)
4358{
4359 int i, j;
4360 short seq;
4361 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004362
Steve Longerbeam81937d32007-05-08 15:33:03 +02004363 for (i = 0; i < num_pins; i++) {
4364 for (j = i + 1; j < num_pins; j++) {
4365 if (sequences[i] > sequences[j]) {
4366 seq = sequences[i];
4367 sequences[i] = sequences[j];
4368 sequences[j] = seq;
4369 nid = pins[i];
4370 pins[i] = pins[j];
4371 pins[j] = nid;
4372 }
4373 }
4374 }
4375}
4376
4377
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004378/* add the found input-pin to the cfg->inputs[] table */
4379static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4380 int type)
4381{
4382 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4383 cfg->inputs[cfg->num_inputs].pin = nid;
4384 cfg->inputs[cfg->num_inputs].type = type;
4385 cfg->num_inputs++;
4386 }
4387}
4388
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004389/* sort inputs in the order of AUTO_PIN_* type */
4390static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4391{
4392 int i, j;
4393
4394 for (i = 0; i < cfg->num_inputs; i++) {
4395 for (j = i + 1; j < cfg->num_inputs; j++) {
4396 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4397 struct auto_pin_cfg_item tmp;
4398 tmp = cfg->inputs[i];
4399 cfg->inputs[i] = cfg->inputs[j];
4400 cfg->inputs[j] = tmp;
4401 }
4402 }
4403 }
4404}
4405
Takashi Iwai82bc9552006-03-21 11:24:42 +01004406/*
4407 * Parse all pin widgets and store the useful pin nids to cfg
4408 *
4409 * The number of line-outs or any primary output is stored in line_outs,
4410 * and the corresponding output pins are assigned to line_out_pins[],
4411 * in the order of front, rear, CLFE, side, ...
4412 *
4413 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004414 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004415 * is detected, one of speaker of HP pins is assigned as the primary
4416 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4417 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004418 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004419 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004420 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4421 * respectively.
4422 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004423int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4424 struct auto_pin_cfg *cfg,
4425 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004426{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004427 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004428 short seq, assoc_line_out, assoc_speaker;
4429 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4430 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004431 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004432 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004433
4434 memset(cfg, 0, sizeof(*cfg));
4435
Steve Longerbeam81937d32007-05-08 15:33:03 +02004436 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4437 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004438 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004439 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004440
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004441 end_nid = codec->start_nid + codec->num_nodes;
4442 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004443 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004444 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004445 unsigned int def_conf;
4446 short assoc, loc;
4447
4448 /* read all default configuration for pin complex */
4449 if (wid_type != AC_WID_PIN)
4450 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004451 /* ignore the given nids (e.g. pc-beep returns error) */
4452 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4453 continue;
4454
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004455 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004456 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4457 continue;
4458 loc = get_defcfg_location(def_conf);
4459 switch (get_defcfg_device(def_conf)) {
4460 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004461 seq = get_defcfg_sequence(def_conf);
4462 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004463
4464 if (!(wid_caps & AC_WCAP_STEREO))
4465 if (!cfg->mono_out_pin)
4466 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004467 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004468 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004469 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004470 assoc_line_out = assoc;
4471 else if (assoc_line_out != assoc)
4472 continue;
4473 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4474 continue;
4475 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004476 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004477 cfg->line_outs++;
4478 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004479 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004480 seq = get_defcfg_sequence(def_conf);
4481 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004482 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004483 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004484 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004485 assoc_speaker = assoc;
4486 else if (assoc_speaker != assoc)
4487 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004488 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4489 continue;
4490 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004491 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004492 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004493 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004494 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004495 seq = get_defcfg_sequence(def_conf);
4496 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004497 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4498 continue;
4499 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004500 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004501 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004502 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004503 case AC_JACK_MIC_IN:
4504 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004505 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004506 case AC_JACK_LINE_IN:
4507 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004508 break;
4509 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004510 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004511 break;
4512 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004513 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004514 break;
4515 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004516 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004517 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4518 continue;
4519 cfg->dig_out_pins[cfg->dig_outs] = nid;
4520 cfg->dig_out_type[cfg->dig_outs] =
4521 (loc == AC_JACK_LOC_HDMI) ?
4522 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4523 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004524 break;
4525 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004526 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004527 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004528 if (loc == AC_JACK_LOC_HDMI)
4529 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4530 else
4531 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004532 break;
4533 }
4534 }
4535
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004536 /* FIX-UP:
4537 * If no line-out is defined but multiple HPs are found,
4538 * some of them might be the real line-outs.
4539 */
4540 if (!cfg->line_outs && cfg->hp_outs > 1) {
4541 int i = 0;
4542 while (i < cfg->hp_outs) {
4543 /* The real HPs should have the sequence 0x0f */
4544 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4545 i++;
4546 continue;
4547 }
4548 /* Move it to the line-out table */
4549 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4550 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4551 cfg->line_outs++;
4552 cfg->hp_outs--;
4553 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4554 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004555 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004556 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4557 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004558 memset(cfg->hp_pins + cfg->hp_outs, 0,
4559 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004560 }
4561
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004562 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004563 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4564 cfg->line_outs);
4565 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4566 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004567 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4568 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004569
Steve Longerbeam81937d32007-05-08 15:33:03 +02004570 /*
4571 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4572 * as a primary output
4573 */
4574 if (!cfg->line_outs) {
4575 if (cfg->speaker_outs) {
4576 cfg->line_outs = cfg->speaker_outs;
4577 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4578 sizeof(cfg->speaker_pins));
4579 cfg->speaker_outs = 0;
4580 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4581 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4582 } else if (cfg->hp_outs) {
4583 cfg->line_outs = cfg->hp_outs;
4584 memcpy(cfg->line_out_pins, cfg->hp_pins,
4585 sizeof(cfg->hp_pins));
4586 cfg->hp_outs = 0;
4587 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4588 cfg->line_out_type = AUTO_PIN_HP_OUT;
4589 }
4590 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004591
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004592 /* Reorder the surround channels
4593 * ALSA sequence is front/surr/clfe/side
4594 * HDA sequence is:
4595 * 4-ch: front/surr => OK as it is
4596 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004597 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004598 */
4599 switch (cfg->line_outs) {
4600 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004601 case 4:
4602 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004603 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004604 cfg->line_out_pins[2] = nid;
4605 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004606 }
4607
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004608 sort_autocfg_input_pins(cfg);
4609
Takashi Iwai82bc9552006-03-21 11:24:42 +01004610 /*
4611 * debug prints of the parsed results
4612 */
4613 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4614 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4615 cfg->line_out_pins[2], cfg->line_out_pins[3],
4616 cfg->line_out_pins[4]);
4617 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4618 cfg->speaker_outs, cfg->speaker_pins[0],
4619 cfg->speaker_pins[1], cfg->speaker_pins[2],
4620 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004621 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4622 cfg->hp_outs, cfg->hp_pins[0],
4623 cfg->hp_pins[1], cfg->hp_pins[2],
4624 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004625 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004626 if (cfg->dig_outs)
4627 snd_printd(" dig-out=0x%x/0x%x\n",
4628 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004629 snd_printd(" inputs:");
4630 for (i = 0; i < cfg->num_inputs; i++) {
4631 snd_printdd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004632 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004633 cfg->inputs[i].pin);
4634 }
4635 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004636 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004637 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004638
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004639 return 0;
4640}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004641EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004642
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004643int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004644{
4645 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004646 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004647 if (conn == AC_JACK_PORT_NONE)
4648 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004649 /* Windows may claim the internal mic to be BOTH, too */
4650 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004651 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004652 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004653 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004654 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004655 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004656 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004657 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004658 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004659 return INPUT_PIN_ATTR_FRONT;
4660 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004661}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004662EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004663
Takashi Iwai990061c2010-09-09 22:08:44 +02004664/**
4665 * hda_get_input_pin_label - Give a label for the given input pin
4666 *
4667 * When check_location is true, the function checks the pin location
4668 * for mic and line-in pins, and set an appropriate prefix like "Front",
4669 * "Rear", "Internal".
4670 */
4671
Takashi Iwai10a20af2010-09-09 16:28:02 +02004672const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4673 int check_location)
4674{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004675 unsigned int def_conf;
4676 static const char *mic_names[] = {
4677 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4678 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004679 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004680
Takashi Iwai10a20af2010-09-09 16:28:02 +02004681 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004682
4683 switch (get_defcfg_device(def_conf)) {
4684 case AC_JACK_MIC_IN:
4685 if (!check_location)
4686 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004687 attr = snd_hda_get_input_pin_attr(def_conf);
4688 if (!attr)
4689 return "None";
4690 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004691 case AC_JACK_LINE_IN:
4692 if (!check_location)
4693 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004694 attr = snd_hda_get_input_pin_attr(def_conf);
4695 if (!attr)
4696 return "None";
4697 if (attr == INPUT_PIN_ATTR_DOCK)
4698 return "Dock Line";
4699 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004700 case AC_JACK_AUX:
4701 return "Aux";
4702 case AC_JACK_CD:
4703 return "CD";
4704 case AC_JACK_SPDIF_IN:
4705 return "SPDIF In";
4706 case AC_JACK_DIG_OTHER_IN:
4707 return "Digital In";
4708 default:
4709 return "Misc";
4710 }
4711}
4712EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4713
Takashi Iwaia1c98512010-09-09 21:36:27 +02004714/* Check whether the location prefix needs to be added to the label.
4715 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4716 * have to put "Front" prefix to each label. In such a case, returns false.
4717 */
4718static int check_mic_location_need(struct hda_codec *codec,
4719 const struct auto_pin_cfg *cfg,
4720 int input)
4721{
4722 unsigned int defc;
4723 int i, attr, attr2;
4724
4725 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004726 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004727 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004728 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004729 return 1;
4730
4731 attr = 0;
4732 for (i = 0; i < cfg->num_inputs; i++) {
4733 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004734 attr2 = snd_hda_get_input_pin_attr(defc);
4735 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004736 if (attr && attr != attr2)
4737 return 1; /* different locations found */
4738 attr = attr2;
4739 }
4740 }
4741 return 0;
4742}
4743
Takashi Iwai990061c2010-09-09 22:08:44 +02004744/**
4745 * hda_get_autocfg_input_label - Get a label for the given input
4746 *
4747 * Get a label for the given input pin defined by the autocfg item.
4748 * Unlike hda_get_input_pin_label(), this function checks all inputs
4749 * defined in autocfg and avoids the redundant mic/line prefix as much as
4750 * possible.
4751 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004752const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4753 const struct auto_pin_cfg *cfg,
4754 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004755{
4756 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004757 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004758
Takashi Iwai10a20af2010-09-09 16:28:02 +02004759 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4760 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4761 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004762 if (has_multiple_pins && type == AUTO_PIN_MIC)
4763 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004764 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4765 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004766}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004767EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4768
Takashi Iwai990061c2010-09-09 22:08:44 +02004769/**
4770 * snd_hda_add_imux_item - Add an item to input_mux
4771 *
4772 * When the same label is used already in the existing items, the number
4773 * suffix is appended to the label. This label index number is stored
4774 * to type_idx when non-NULL pointer is given.
4775 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004776int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4777 int index, int *type_idx)
4778{
4779 int i, label_idx = 0;
4780 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4781 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4782 return -EINVAL;
4783 }
4784 for (i = 0; i < imux->num_items; i++) {
4785 if (!strncmp(label, imux->items[i].label, strlen(label)))
4786 label_idx++;
4787 }
4788 if (type_idx)
4789 *type_idx = label_idx;
4790 if (label_idx > 0)
4791 snprintf(imux->items[imux->num_items].label,
4792 sizeof(imux->items[imux->num_items].label),
4793 "%s %d", label, label_idx);
4794 else
4795 strlcpy(imux->items[imux->num_items].label, label,
4796 sizeof(imux->items[imux->num_items].label));
4797 imux->items[imux->num_items].index = index;
4798 imux->num_items++;
4799 return 0;
4800}
4801EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004802
Takashi Iwai4a471b72005-12-07 13:56:29 +01004803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804#ifdef CONFIG_PM
4805/*
4806 * power management
4807 */
4808
4809/**
4810 * snd_hda_suspend - suspend the codecs
4811 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 *
4813 * Returns 0 if successful.
4814 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004815int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004817 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Takashi Iwai0ba21762007-04-16 11:29:14 +02004819 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004820#ifdef CONFIG_SND_HDA_POWER_SAVE
4821 if (!codec->power_on)
4822 continue;
4823#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004824 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 }
4826 return 0;
4827}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004828EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
4830/**
4831 * snd_hda_resume - resume the codecs
4832 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 *
4834 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004835 *
4836 * This fucntion is defined only when POWER_SAVE isn't set.
4837 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 */
4839int snd_hda_resume(struct hda_bus *bus)
4840{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004841 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Takashi Iwai0ba21762007-04-16 11:29:14 +02004843 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004844 if (snd_hda_codec_needs_resume(codec))
4845 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 return 0;
4848}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004849EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004850#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004851
4852/*
4853 * generic arrays
4854 */
4855
Takashi Iwaid5191e52009-11-16 14:58:17 +01004856/**
4857 * snd_array_new - get a new element from the given array
4858 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004859 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004860 * Get a new element from the given array. If it exceeds the
4861 * pre-allocated array size, re-allocate the array.
4862 *
4863 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004864 */
4865void *snd_array_new(struct snd_array *array)
4866{
4867 if (array->used >= array->alloced) {
4868 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004869 void *nlist;
4870 if (snd_BUG_ON(num >= 4096))
4871 return NULL;
4872 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004873 if (!nlist)
4874 return NULL;
4875 if (array->list) {
4876 memcpy(nlist, array->list,
4877 array->elem_size * array->alloced);
4878 kfree(array->list);
4879 }
4880 array->list = nlist;
4881 array->alloced = num;
4882 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004883 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004884}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004885EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004886
Takashi Iwaid5191e52009-11-16 14:58:17 +01004887/**
4888 * snd_array_free - free the given array elements
4889 * @array: the array object
4890 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004891void snd_array_free(struct snd_array *array)
4892{
4893 kfree(array->list);
4894 array->used = 0;
4895 array->alloced = 0;
4896 array->list = NULL;
4897}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004898EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004899
Takashi Iwaid5191e52009-11-16 14:58:17 +01004900/**
4901 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4902 * @pcm: PCM caps bits
4903 * @buf: the string buffer to write
4904 * @buflen: the max buffer length
4905 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004906 * used by hda_proc.c and hda_eld.c
4907 */
4908void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4909{
4910 static unsigned int rates[] = {
4911 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4912 96000, 176400, 192000, 384000
4913 };
4914 int i, j;
4915
4916 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4917 if (pcm & (1 << i))
4918 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4919
4920 buf[j] = '\0'; /* necessary when j == 0 */
4921}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004922EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004923
Takashi Iwaid5191e52009-11-16 14:58:17 +01004924/**
4925 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4926 * @pcm: PCM caps bits
4927 * @buf: the string buffer to write
4928 * @buflen: the max buffer length
4929 *
4930 * used by hda_proc.c and hda_eld.c
4931 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004932void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4933{
4934 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4935 int i, j;
4936
4937 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4938 if (pcm & (AC_SUPPCM_BITS_8 << i))
4939 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4940
4941 buf[j] = '\0'; /* necessary when j == 0 */
4942}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004943EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004944
4945MODULE_DESCRIPTION("HDA codec core");
4946MODULE_LICENSE("GPL");