blob: 88a1c6acbcbd92570ff47c2459959630f7185b6a [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 Iwai0ba21762007-04-16 11:29:14 +0200399 snd_printk(KERN_ERR
400 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100401 return -EINVAL;
402 }
403 conn_list[conns++] = n;
404 }
405 } else {
406 if (conns >= max_conns) {
407 snd_printk(KERN_ERR "Too many connections\n");
408 return -EINVAL;
409 }
410 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100412 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 return conns;
415}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100416EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418
419/**
420 * snd_hda_queue_unsol_event - add an unsolicited event to queue
421 * @bus: the BUS
422 * @res: unsolicited event (lower 32bit of RIRB entry)
423 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
424 *
425 * Adds the given event to the queue. The events are processed in
426 * the workqueue asynchronously. Call this function in the interrupt
427 * hanlder when RIRB receives an unsolicited event.
428 *
429 * Returns 0 if successful, or a negative error code.
430 */
431int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
432{
433 struct hda_bus_unsolicited *unsol;
434 unsigned int wp;
435
Takashi Iwai0ba21762007-04-16 11:29:14 +0200436 unsol = bus->unsol;
437 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439
440 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
441 unsol->wp = wp;
442
443 wp <<= 1;
444 unsol->queue[wp] = res;
445 unsol->queue[wp + 1] = res_ex;
446
Takashi Iwai6acaed32009-01-12 10:09:24 +0100447 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return 0;
450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100451EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800454 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
David Howellsc4028952006-11-22 14:57:56 +0000456static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
David Howellsc4028952006-11-22 14:57:56 +0000458 struct hda_bus_unsolicited *unsol =
459 container_of(work, struct hda_bus_unsolicited, work);
460 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct hda_codec *codec;
462 unsigned int rp, caddr, res;
463
464 while (unsol->rp != unsol->wp) {
465 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
466 unsol->rp = rp;
467 rp <<= 1;
468 res = unsol->queue[rp];
469 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200470 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 continue;
472 codec = bus->caddr_tbl[caddr & 0x0f];
473 if (codec && codec->patch_ops.unsol_event)
474 codec->patch_ops.unsol_event(codec, res);
475 }
476}
477
478/*
479 * initialize unsolicited queue
480 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200481static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct hda_bus_unsolicited *unsol;
484
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100485 if (bus->unsol) /* already initialized */
486 return 0;
487
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200488 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200489 if (!unsol) {
490 snd_printk(KERN_ERR "hda_codec: "
491 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -ENOMEM;
493 }
David Howellsc4028952006-11-22 14:57:56 +0000494 INIT_WORK(&unsol->work, process_unsol_events);
495 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 bus->unsol = unsol;
497 return 0;
498}
499
500/*
501 * destructor
502 */
503static void snd_hda_codec_free(struct hda_codec *codec);
504
505static int snd_hda_bus_free(struct hda_bus *bus)
506{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200507 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Takashi Iwai0ba21762007-04-16 11:29:14 +0200509 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100511 if (bus->workq)
512 flush_workqueue(bus->workq);
513 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200515 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 snd_hda_codec_free(codec);
517 }
518 if (bus->ops.private_free)
519 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100520 if (bus->workq)
521 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 kfree(bus);
523 return 0;
524}
525
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100526static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100529 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return snd_hda_bus_free(bus);
531}
532
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200533#ifdef CONFIG_SND_HDA_HWDEP
534static int snd_hda_bus_dev_register(struct snd_device *device)
535{
536 struct hda_bus *bus = device->device_data;
537 struct hda_codec *codec;
538 list_for_each_entry(codec, &bus->codec_list, list) {
539 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100540 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200541 }
542 return 0;
543}
544#else
545#define snd_hda_bus_dev_register NULL
546#endif
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/**
549 * snd_hda_bus_new - create a HDA bus
550 * @card: the card entry
551 * @temp: the template for hda_bus information
552 * @busp: the pointer to store the created bus instance
553 *
554 * Returns 0 if successful, or a negative error code.
555 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100556int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200557 const struct hda_bus_template *temp,
558 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct hda_bus *bus;
561 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100562 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200563 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .dev_free = snd_hda_bus_dev_free,
565 };
566
Takashi Iwaida3cec32008-08-08 17:12:14 +0200567 if (snd_BUG_ON(!temp))
568 return -EINVAL;
569 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (busp)
573 *busp = NULL;
574
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200575 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (bus == NULL) {
577 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
578 return -ENOMEM;
579 }
580
581 bus->card = card;
582 bus->private_data = temp->private_data;
583 bus->pci = temp->pci;
584 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100585 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 bus->ops = temp->ops;
587
Ingo Molnar62932df2006-01-16 16:34:20 +0100588 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 INIT_LIST_HEAD(&bus->codec_list);
590
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100591 snprintf(bus->workq_name, sizeof(bus->workq_name),
592 "hd-audio%d", card->number);
593 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100594 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100595 snd_printk(KERN_ERR "cannot create workqueue %s\n",
596 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100597 kfree(bus);
598 return -ENOMEM;
599 }
600
Takashi Iwai0ba21762007-04-16 11:29:14 +0200601 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
602 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 snd_hda_bus_free(bus);
604 return err;
605 }
606 if (busp)
607 *busp = bus;
608 return 0;
609}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100610EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Takashi Iwai82467612007-07-27 19:15:54 +0200612#ifdef CONFIG_SND_HDA_GENERIC
613#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200614 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200615#else
616#define is_generic_config(codec) 0
617#endif
618
Takashi Iwai645f10c2008-11-28 15:07:37 +0100619#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100620#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
621#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100622#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100623#endif
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*
626 * find a matching codec preset
627 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200628static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200629find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100631 struct hda_codec_preset_list *tbl;
632 const struct hda_codec_preset *preset;
633 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Takashi Iwai82467612007-07-27 19:15:54 +0200635 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100636 return NULL; /* use the generic parser */
637
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100638 again:
639 mutex_lock(&preset_mutex);
640 list_for_each_entry(tbl, &hda_preset_tables, list) {
641 if (!try_module_get(tbl->owner)) {
642 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
643 continue;
644 }
645 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100647 if (preset->afg && preset->afg != codec->afg)
648 continue;
649 if (preset->mfg && preset->mfg != codec->mfg)
650 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200651 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200653 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200654 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100655 preset->rev == codec->revision_id)) {
656 mutex_unlock(&preset_mutex);
657 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100661 module_put(tbl->owner);
662 }
663 mutex_unlock(&preset_mutex);
664
665 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
666 char name[32];
667 if (!mod_requested)
668 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
669 codec->vendor_id);
670 else
671 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
672 (codec->vendor_id >> 16) & 0xffff);
673 request_module(name);
674 mod_requested++;
675 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 return NULL;
678}
679
680/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200681 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200683static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 const struct hda_vendor_id *c;
686 const char *vendor = NULL;
687 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200688 char tmp[16];
689
690 if (codec->vendor_name)
691 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 for (c = hda_vendor_ids; c->id; c++) {
694 if (c->id == vendor_id) {
695 vendor = c->name;
696 break;
697 }
698 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200699 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 sprintf(tmp, "Generic %04x", vendor_id);
701 vendor = tmp;
702 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200703 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
704 if (!codec->vendor_name)
705 return -ENOMEM;
706
707 get_chip_name:
708 if (codec->chip_name)
709 return 0;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200712 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
713 else {
714 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
715 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
716 }
717 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200718 return -ENOMEM;
719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200723 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100725static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200727 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 hda_nid_t nid;
729
730 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
731 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200732 function_id = snd_hda_param_read(codec, nid,
Pascal de Bruijn234b4342009-03-23 11:15:59 +0100733 AC_PAR_FUNCTION_TYPE) & 0xff;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200734 switch (function_id) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200735 case AC_GRP_AUDIO_FUNCTION:
736 codec->afg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200737 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200738 break;
739 case AC_GRP_MODEM_FUNCTION:
740 codec->mfg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200741 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200742 break;
743 default:
744 break;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100750 * read widget caps for each widget and store in cache
751 */
752static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
753{
754 int i;
755 hda_nid_t nid;
756
757 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
758 &codec->start_nid);
759 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200760 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100761 return -ENOMEM;
762 nid = codec->start_nid;
763 for (i = 0; i < codec->num_nodes; i++, nid++)
764 codec->wcaps[i] = snd_hda_param_read(codec, nid,
765 AC_PAR_AUDIO_WIDGET_CAP);
766 return 0;
767}
768
Takashi Iwai3be14142009-02-20 14:11:16 +0100769/* read all pin default configurations and save codec->init_pins */
770static int read_pin_defaults(struct hda_codec *codec)
771{
772 int i;
773 hda_nid_t nid = codec->start_nid;
774
775 for (i = 0; i < codec->num_nodes; i++, nid++) {
776 struct hda_pincfg *pin;
777 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200778 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100779 if (wid_type != AC_WID_PIN)
780 continue;
781 pin = snd_array_new(&codec->init_pins);
782 if (!pin)
783 return -ENOMEM;
784 pin->nid = nid;
785 pin->cfg = snd_hda_codec_read(codec, nid, 0,
786 AC_VERB_GET_CONFIG_DEFAULT, 0);
787 }
788 return 0;
789}
790
791/* look up the given pin config list and return the item matching with NID */
792static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
793 struct snd_array *array,
794 hda_nid_t nid)
795{
796 int i;
797 for (i = 0; i < array->used; i++) {
798 struct hda_pincfg *pin = snd_array_elem(array, i);
799 if (pin->nid == nid)
800 return pin;
801 }
802 return NULL;
803}
804
805/* write a config value for the given NID */
806static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
807 unsigned int cfg)
808{
809 int i;
810 for (i = 0; i < 4; i++) {
811 snd_hda_codec_write(codec, nid, 0,
812 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
813 cfg & 0xff);
814 cfg >>= 8;
815 }
816}
817
818/* set the current pin config value for the given NID.
819 * the value is cached, and read via snd_hda_codec_get_pincfg()
820 */
821int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
822 hda_nid_t nid, unsigned int cfg)
823{
824 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100825 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100826
Takashi Iwaib82855a2009-12-27 11:24:56 +0100827 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
828 return -EINVAL;
829
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100830 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100831 pin = look_up_pincfg(codec, list, nid);
832 if (!pin) {
833 pin = snd_array_new(list);
834 if (!pin)
835 return -ENOMEM;
836 pin->nid = nid;
837 }
838 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100839
840 /* change only when needed; e.g. if the pincfg is already present
841 * in user_pins[], don't write it
842 */
843 cfg = snd_hda_codec_get_pincfg(codec, nid);
844 if (oldcfg != cfg)
845 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100846 return 0;
847}
848
Takashi Iwaid5191e52009-11-16 14:58:17 +0100849/**
850 * snd_hda_codec_set_pincfg - Override a pin default configuration
851 * @codec: the HDA codec
852 * @nid: NID to set the pin config
853 * @cfg: the pin default config value
854 *
855 * Override a pin default configuration value in the cache.
856 * This value can be read by snd_hda_codec_get_pincfg() in a higher
857 * priority than the real hardware value.
858 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100859int snd_hda_codec_set_pincfg(struct hda_codec *codec,
860 hda_nid_t nid, unsigned int cfg)
861{
Takashi Iwai346ff702009-02-23 09:42:57 +0100862 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100863}
864EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
865
Takashi Iwaid5191e52009-11-16 14:58:17 +0100866/**
867 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
868 * @codec: the HDA codec
869 * @nid: NID to get the pin config
870 *
871 * Get the current pin config value of the given pin NID.
872 * If the pincfg value is cached or overridden via sysfs or driver,
873 * returns the cached value.
874 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100875unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
876{
877 struct hda_pincfg *pin;
878
Takashi Iwai3be14142009-02-20 14:11:16 +0100879#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100880 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100881 if (pin)
882 return pin->cfg;
883#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100884 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
885 if (pin)
886 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100887 pin = look_up_pincfg(codec, &codec->init_pins, nid);
888 if (pin)
889 return pin->cfg;
890 return 0;
891}
892EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
893
894/* restore all current pin configs */
895static void restore_pincfgs(struct hda_codec *codec)
896{
897 int i;
898 for (i = 0; i < codec->init_pins.used; i++) {
899 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
900 set_pincfg(codec, pin->nid,
901 snd_hda_codec_get_pincfg(codec, pin->nid));
902 }
903}
Takashi Iwai54d17402005-11-21 16:33:22 +0100904
Takashi Iwai92ee6162009-12-27 11:18:59 +0100905/**
906 * snd_hda_shutup_pins - Shut up all pins
907 * @codec: the HDA codec
908 *
909 * Clear all pin controls to shup up before suspend for avoiding click noise.
910 * The controls aren't cached so that they can be resumed properly.
911 */
912void snd_hda_shutup_pins(struct hda_codec *codec)
913{
914 int i;
915 for (i = 0; i < codec->init_pins.used; i++) {
916 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
917 /* use read here for syncing after issuing each verb */
918 snd_hda_codec_read(codec, pin->nid, 0,
919 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
920 }
921}
922EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
923
Takashi Iwai01751f52007-08-10 16:59:39 +0200924static void init_hda_cache(struct hda_cache_rec *cache,
925 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200926static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200927
Takashi Iwai3be14142009-02-20 14:11:16 +0100928/* restore the initial pin cfgs and release all pincfg lists */
929static void restore_init_pincfgs(struct hda_codec *codec)
930{
Takashi Iwai346ff702009-02-23 09:42:57 +0100931 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100932 * so that only the values in init_pins are restored
933 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100934 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100935#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100936 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100937#endif
938 restore_pincfgs(codec);
939 snd_array_free(&codec->init_pins);
940}
941
Takashi Iwai54d17402005-11-21 16:33:22 +0100942/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * codec destructor
944 */
945static void snd_hda_codec_free(struct hda_codec *codec)
946{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200947 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return;
Takashi Iwai3be14142009-02-20 14:11:16 +0100949 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200950#ifdef CONFIG_SND_HDA_POWER_SAVE
951 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100952 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200955 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100956 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 codec->bus->caddr_tbl[codec->addr] = NULL;
958 if (codec->patch_ops.free)
959 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100960 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200961 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200962 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200963 kfree(codec->vendor_name);
964 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200965 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100966 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 kfree(codec);
968}
969
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100970static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
971 unsigned int power_state);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/**
974 * snd_hda_codec_new - create a HDA codec
975 * @bus: the bus to assign
976 * @codec_addr: the codec address
977 * @codecp: the pointer to store the generated codec
978 *
979 * Returns 0 if successful, or a negative error code.
980 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +0100981int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
982 unsigned int codec_addr,
983 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200986 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int err;
988
Takashi Iwaida3cec32008-08-08 17:12:14 +0200989 if (snd_BUG_ON(!bus))
990 return -EINVAL;
991 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
992 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200995 snd_printk(KERN_ERR "hda_codec: "
996 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -EBUSY;
998 }
999
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001000 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (codec == NULL) {
1002 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1003 return -ENOMEM;
1004 }
1005
1006 codec->bus = bus;
1007 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001008 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001009 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001010 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001011 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001012 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1013 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001014 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001015 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001016 if (codec->bus->modelname) {
1017 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1018 if (!codec->modelname) {
1019 snd_hda_codec_free(codec);
1020 return -ENODEV;
1021 }
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Takashi Iwaicb53c622007-08-10 17:21:45 +02001024#ifdef CONFIG_SND_HDA_POWER_SAVE
1025 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1026 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1027 * the caller has to power down appropriatley after initialization
1028 * phase.
1029 */
1030 hda_keep_power_on(codec);
1031#endif
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 list_add_tail(&codec->list, &bus->codec_list);
1034 bus->caddr_tbl[codec_addr] = codec;
1035
Takashi Iwai0ba21762007-04-16 11:29:14 +02001036 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1037 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001038 if (codec->vendor_id == -1)
1039 /* read again, hopefully the access method was corrected
1040 * in the last read...
1041 */
1042 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1043 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001044 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1045 AC_PAR_SUBSYSTEM_ID);
1046 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1047 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001049 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001050 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001051 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001052 err = -ENODEV;
1053 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Takashi Iwai3be14142009-02-20 14:11:16 +01001056 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1057 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001058 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001059 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001060 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001061 err = read_pin_defaults(codec);
1062 if (err < 0)
1063 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001064
Takashi Iwai0ba21762007-04-16 11:29:14 +02001065 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001066 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001067 codec->subsystem_id =
1068 snd_hda_codec_read(codec, nid, 0,
1069 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001070 }
1071
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001072 /* power-up all before initialization */
1073 hda_set_power_state(codec,
1074 codec->afg ? codec->afg : codec->mfg,
1075 AC_PWRST_D0);
1076
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001077 snd_hda_codec_proc_new(codec);
1078
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001079 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001080
1081 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1082 codec->subsystem_id, codec->revision_id);
1083 snd_component_add(codec->bus->card, component);
1084
1085 if (codecp)
1086 *codecp = codec;
1087 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001088
1089 error:
1090 snd_hda_codec_free(codec);
1091 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001093EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001094
Takashi Iwaid5191e52009-11-16 14:58:17 +01001095/**
1096 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1097 * @codec: the HDA codec
1098 *
1099 * Start parsing of the given codec tree and (re-)initialize the whole
1100 * patch instance.
1101 *
1102 * Returns 0 if successful or a negative error code.
1103 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001104int snd_hda_codec_configure(struct hda_codec *codec)
1105{
1106 int err;
1107
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001108 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001109 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001110 err = get_codec_name(codec);
1111 if (err < 0)
1112 return err;
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Takashi Iwai82467612007-07-27 19:15:54 +02001115 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001117 goto patched;
1118 }
Takashi Iwai82467612007-07-27 19:15:54 +02001119 if (codec->preset && codec->preset->patch) {
1120 err = codec->preset->patch(codec);
1121 goto patched;
1122 }
1123
1124 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001125 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001126 if (err < 0)
1127 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001128
1129 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001130 if (!err && codec->patch_ops.unsol_event)
1131 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001132 /* audio codec should override the mixer name */
1133 if (!err && (codec->afg || !*codec->bus->card->mixername))
1134 snprintf(codec->bus->card->mixername,
1135 sizeof(codec->bus->card->mixername),
1136 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001139EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141/**
1142 * snd_hda_codec_setup_stream - set up the codec for streaming
1143 * @codec: the CODEC to set up
1144 * @nid: the NID to set up
1145 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1146 * @channel_id: channel id to pass, zero based.
1147 * @format: stream format.
1148 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001149void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1150 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int channel_id, int format)
1152{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001153 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001154 return;
1155
Takashi Iwai0ba21762007-04-16 11:29:14 +02001156 snd_printdd("hda_codec_setup_stream: "
1157 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 nid, stream_tag, channel_id, format);
1159 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1160 (stream_tag << 4) | channel_id);
1161 msleep(1);
1162 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1163}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001164EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Takashi Iwaid5191e52009-11-16 14:58:17 +01001166/**
1167 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1168 * @codec: the CODEC to clean up
1169 * @nid: the NID to clean up
1170 */
Takashi Iwai888afa12008-03-18 09:57:50 +01001171void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1172{
1173 if (!nid)
1174 return;
1175
1176 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1177 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1178#if 0 /* keep the format */
1179 msleep(1);
1180 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1181#endif
1182}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001183EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185/*
1186 * amp access functions
1187 */
1188
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001189/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001190#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001191#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001192#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1193#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001195#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001198static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001199 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Takashi Iwai01751f52007-08-10 16:59:39 +02001201 memset(cache, 0, sizeof(*cache));
1202 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001203 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001204}
1205
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001206static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001207{
Takashi Iwai603c4012008-07-30 15:01:44 +02001208 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001212static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwai01751f52007-08-10 16:59:39 +02001214 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1215 u16 cur = cache->hash[idx];
1216 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001219 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (info->key == key)
1221 return info;
1222 cur = info->next;
1223 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001224 return NULL;
1225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001227/* query the hash. allocate an entry if not found. */
1228static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1229 u32 key)
1230{
1231 struct hda_cache_head *info = get_hash(cache, key);
1232 if (!info) {
1233 u16 idx, cur;
1234 /* add a new hash entry */
1235 info = snd_array_new(&cache->buf);
1236 if (!info)
1237 return NULL;
1238 cur = snd_array_index(&cache->buf, info);
1239 info->key = key;
1240 info->val = 0;
1241 idx = key % (u16)ARRAY_SIZE(cache->hash);
1242 info->next = cache->hash[idx];
1243 cache->hash[idx] = cur;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return info;
1246}
1247
Takashi Iwai01751f52007-08-10 16:59:39 +02001248/* query and allocate an amp hash entry */
1249static inline struct hda_amp_info *
1250get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1251{
1252 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1253}
1254
Takashi Iwaid5191e52009-11-16 14:58:17 +01001255/**
1256 * query_amp_caps - query AMP capabilities
1257 * @codec: the HD-auio codec
1258 * @nid: the NID to query
1259 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1260 *
1261 * Query AMP capabilities for the given widget and direction.
1262 * Returns the obtained capability bits.
1263 *
1264 * When cap bits have been already read, this doesn't read again but
1265 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001267u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001269 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Takashi Iwai0ba21762007-04-16 11:29:14 +02001271 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1272 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001274 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001275 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001277 info->amp_caps = snd_hda_param_read(codec, nid,
1278 direction == HDA_OUTPUT ?
1279 AC_PAR_AMP_OUT_CAP :
1280 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001281 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001282 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 return info->amp_caps;
1285}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001286EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Takashi Iwaid5191e52009-11-16 14:58:17 +01001288/**
1289 * snd_hda_override_amp_caps - Override the AMP capabilities
1290 * @codec: the CODEC to clean up
1291 * @nid: the NID to clean up
1292 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1293 * @caps: the capability bits to set
1294 *
1295 * Override the cached AMP caps bits value by the given one.
1296 * This function is useful if the driver needs to adjust the AMP ranges,
1297 * e.g. limit to 0dB, etc.
1298 *
1299 * Returns zero if successful or a negative error code.
1300 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001301int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1302 unsigned int caps)
1303{
1304 struct hda_amp_info *info;
1305
1306 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1307 if (!info)
1308 return -EINVAL;
1309 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001310 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001311 return 0;
1312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001313EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001314
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001315static unsigned int
1316query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1317 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001318{
1319 struct hda_amp_info *info;
1320
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001321 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001322 if (!info)
1323 return 0;
1324 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001325 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001326 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001327 }
1328 return info->amp_caps;
1329}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001330
1331static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1332{
1333 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1334}
1335
Takashi Iwaid5191e52009-11-16 14:58:17 +01001336/**
1337 * snd_hda_query_pin_caps - Query PIN capabilities
1338 * @codec: the HD-auio codec
1339 * @nid: the NID to query
1340 *
1341 * Query PIN capabilities for the given widget.
1342 * Returns the obtained capability bits.
1343 *
1344 * When cap bits have been already read, this doesn't read again but
1345 * returns the cached value.
1346 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001347u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1348{
1349 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1350 read_pin_cap);
1351}
Takashi Iwai1327a322009-03-23 13:07:47 +01001352EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1353
Wu Fengguang864f92b2009-11-18 12:38:02 +08001354/**
1355 * snd_hda_pin_sense - execute pin sense measurement
1356 * @codec: the CODEC to sense
1357 * @nid: the pin NID to sense
1358 *
1359 * Execute necessary pin sense measurement and return its Presence Detect,
1360 * Impedance, ELD Valid etc. status bits.
1361 */
1362u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1363{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001364 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001365
Takashi Iwai729d55b2009-12-25 22:49:01 +01001366 if (!codec->no_trigger_sense) {
1367 pincap = snd_hda_query_pin_caps(codec, nid);
1368 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001369 snd_hda_codec_read(codec, nid, 0,
1370 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001371 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001372 return snd_hda_codec_read(codec, nid, 0,
1373 AC_VERB_GET_PIN_SENSE, 0);
1374}
1375EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1376
1377/**
1378 * snd_hda_jack_detect - query pin Presence Detect status
1379 * @codec: the CODEC to sense
1380 * @nid: the pin NID to sense
1381 *
1382 * Query and return the pin's Presence Detect status.
1383 */
1384int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1385{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001386 u32 sense = snd_hda_pin_sense(codec, nid);
1387 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001388}
1389EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391/*
1392 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001393 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001395static unsigned int get_vol_mute(struct hda_codec *codec,
1396 struct hda_amp_info *info, hda_nid_t nid,
1397 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 u32 val, parm;
1400
Takashi Iwai01751f52007-08-10 16:59:39 +02001401 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001402 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1405 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1406 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001407 val = snd_hda_codec_read(codec, nid, 0,
1408 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001410 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001411 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
1414/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001415 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001417static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 hda_nid_t nid, int ch, int direction, int index,
1419 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 u32 parm;
1422
1423 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1424 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1425 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1426 parm |= val;
1427 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001428 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Takashi Iwaid5191e52009-11-16 14:58:17 +01001431/**
1432 * snd_hda_codec_amp_read - Read AMP value
1433 * @codec: HD-audio codec
1434 * @nid: NID to read the AMP value
1435 * @ch: channel (left=0 or right=1)
1436 * @direction: #HDA_INPUT or #HDA_OUTPUT
1437 * @index: the index value (only for input direction)
1438 *
1439 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
Takashi Iwai834be882006-03-01 14:16:17 +01001441int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1442 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001444 struct hda_amp_info *info;
1445 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1446 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001448 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001450EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Takashi Iwaid5191e52009-11-16 14:58:17 +01001452/**
1453 * snd_hda_codec_amp_update - update the AMP value
1454 * @codec: HD-audio codec
1455 * @nid: NID to read the AMP value
1456 * @ch: channel (left=0 or right=1)
1457 * @direction: #HDA_INPUT or #HDA_OUTPUT
1458 * @idx: the index value (only for input direction)
1459 * @mask: bit mask to set
1460 * @val: the bits value to set
1461 *
1462 * Update the AMP value with a bit mask.
1463 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001464 */
Takashi Iwai834be882006-03-01 14:16:17 +01001465int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1466 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001468 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001469
Takashi Iwai0ba21762007-04-16 11:29:14 +02001470 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1471 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001473 if (snd_BUG_ON(mask & ~0xff))
1474 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001475 val &= mask;
1476 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001477 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001479 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 1;
1481}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001482EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Takashi Iwaid5191e52009-11-16 14:58:17 +01001484/**
1485 * snd_hda_codec_amp_stereo - update the AMP stereo values
1486 * @codec: HD-audio codec
1487 * @nid: NID to read the AMP value
1488 * @direction: #HDA_INPUT or #HDA_OUTPUT
1489 * @idx: the index value (only for input direction)
1490 * @mask: bit mask to set
1491 * @val: the bits value to set
1492 *
1493 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1494 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001495 */
1496int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1497 int direction, int idx, int mask, int val)
1498{
1499 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001500
1501 if (snd_BUG_ON(mask & ~0xff))
1502 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001503 for (ch = 0; ch < 2; ch++)
1504 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1505 idx, mask, val);
1506 return ret;
1507}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001508EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001509
Takashi Iwaicb53c622007-08-10 17:21:45 +02001510#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001511/**
1512 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1513 * @codec: HD-audio codec
1514 *
1515 * Resume the all amp commands from the cache.
1516 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001517void snd_hda_codec_resume_amp(struct hda_codec *codec)
1518{
Takashi Iwai603c4012008-07-30 15:01:44 +02001519 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001520 int i;
1521
Takashi Iwai603c4012008-07-30 15:01:44 +02001522 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001523 u32 key = buffer->head.key;
1524 hda_nid_t nid;
1525 unsigned int idx, dir, ch;
1526 if (!key)
1527 continue;
1528 nid = key & 0xff;
1529 idx = (key >> 16) & 0xff;
1530 dir = (key >> 24) & 0xff;
1531 for (ch = 0; ch < 2; ch++) {
1532 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1533 continue;
1534 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1535 buffer->vol[ch]);
1536 }
1537 }
1538}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001539EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001540#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001542static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1543 unsigned int ofs)
1544{
1545 u32 caps = query_amp_caps(codec, nid, dir);
1546 /* get num steps */
1547 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1548 if (ofs < caps)
1549 caps -= ofs;
1550 return caps;
1551}
1552
Takashi Iwaid5191e52009-11-16 14:58:17 +01001553/**
1554 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1555 *
1556 * The control element is supposed to have the private_value field
1557 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1558 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001559int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1563 u16 nid = get_amp_nid(kcontrol);
1564 u8 chs = get_amp_channels(kcontrol);
1565 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001566 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001568 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1569 uinfo->count = chs == 3 ? 2 : 1;
1570 uinfo->value.integer.min = 0;
1571 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1572 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001573 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001574 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1575 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return -EINVAL;
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001580EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001582
1583static inline unsigned int
1584read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1585 int ch, int dir, int idx, unsigned int ofs)
1586{
1587 unsigned int val;
1588 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1589 val &= HDA_AMP_VOLMASK;
1590 if (val >= ofs)
1591 val -= ofs;
1592 else
1593 val = 0;
1594 return val;
1595}
1596
1597static inline int
1598update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1599 int ch, int dir, int idx, unsigned int ofs,
1600 unsigned int val)
1601{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001602 unsigned int maxval;
1603
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001604 if (val > 0)
1605 val += ofs;
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001606 maxval = get_amp_max_value(codec, nid, dir, ofs);
1607 if (val > maxval)
1608 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001609 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1610 HDA_AMP_VOLMASK, val);
1611}
1612
Takashi Iwaid5191e52009-11-16 14:58:17 +01001613/**
1614 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1615 *
1616 * The control element is supposed to have the private_value field
1617 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1618 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001619int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1620 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1623 hda_nid_t nid = get_amp_nid(kcontrol);
1624 int chs = get_amp_channels(kcontrol);
1625 int dir = get_amp_direction(kcontrol);
1626 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001627 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 long *valp = ucontrol->value.integer.value;
1629
1630 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001631 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001633 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return 0;
1635}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001636EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Takashi Iwaid5191e52009-11-16 14:58:17 +01001638/**
1639 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1640 *
1641 * The control element is supposed to have the private_value field
1642 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1643 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001644int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1645 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1648 hda_nid_t nid = get_amp_nid(kcontrol);
1649 int chs = get_amp_channels(kcontrol);
1650 int dir = get_amp_direction(kcontrol);
1651 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001652 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 long *valp = ucontrol->value.integer.value;
1654 int change = 0;
1655
Takashi Iwaicb53c622007-08-10 17:21:45 +02001656 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001657 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001658 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001659 valp++;
1660 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001661 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001662 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001663 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return change;
1665}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001666EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Takashi Iwaid5191e52009-11-16 14:58:17 +01001668/**
1669 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1670 *
1671 * The control element is supposed to have the private_value field
1672 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1673 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001674int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1675 unsigned int size, unsigned int __user *_tlv)
1676{
1677 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1678 hda_nid_t nid = get_amp_nid(kcontrol);
1679 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001680 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001681 u32 caps, val1, val2;
1682
1683 if (size < 4 * sizeof(unsigned int))
1684 return -ENOMEM;
1685 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001686 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1687 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001688 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001689 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001690 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001691 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1692 return -EFAULT;
1693 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1694 return -EFAULT;
1695 if (put_user(val1, _tlv + 2))
1696 return -EFAULT;
1697 if (put_user(val2, _tlv + 3))
1698 return -EFAULT;
1699 return 0;
1700}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001701EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001702
Takashi Iwaid5191e52009-11-16 14:58:17 +01001703/**
1704 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1705 * @codec: HD-audio codec
1706 * @nid: NID of a reference widget
1707 * @dir: #HDA_INPUT or #HDA_OUTPUT
1708 * @tlv: TLV data to be stored, at least 4 elements
1709 *
1710 * Set (static) TLV data for a virtual master volume using the AMP caps
1711 * obtained from the reference NID.
1712 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001713 */
1714void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1715 unsigned int *tlv)
1716{
1717 u32 caps;
1718 int nums, step;
1719
1720 caps = query_amp_caps(codec, nid, dir);
1721 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1722 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1723 step = (step + 1) * 25;
1724 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1725 tlv[1] = 2 * sizeof(unsigned int);
1726 tlv[2] = -nums * step;
1727 tlv[3] = step;
1728}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001729EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001730
1731/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001732static struct snd_kcontrol *
1733_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1734 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001735{
1736 struct snd_ctl_elem_id id;
1737 memset(&id, 0, sizeof(id));
1738 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001739 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001740 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1741 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001742 strcpy(id.name, name);
1743 return snd_ctl_find_id(codec->bus->card, &id);
1744}
1745
Takashi Iwaid5191e52009-11-16 14:58:17 +01001746/**
1747 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1748 * @codec: HD-audio codec
1749 * @name: ctl id name string
1750 *
1751 * Get the control element with the given id string and IFACE_MIXER.
1752 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001753struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1754 const char *name)
1755{
1756 return _snd_hda_find_mixer_ctl(codec, name, 0);
1757}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001758EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001759
Takashi Iwaid5191e52009-11-16 14:58:17 +01001760/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001761 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001762 * @codec: HD-audio codec
1763 * @nid: corresponding NID (optional)
1764 * @kctl: the control element to assign
1765 *
1766 * Add the given control element to an array inside the codec instance.
1767 * All control elements belonging to a codec are supposed to be added
1768 * by this function so that a proper clean-up works at the free or
1769 * reconfiguration time.
1770 *
1771 * If non-zero @nid is passed, the NID is assigned to the control element.
1772 * The assignment is shown in the codec proc file.
1773 *
1774 * snd_hda_ctl_add() checks the control subdev id field whether
1775 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001776 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1777 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001778 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001779int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1780 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001781{
1782 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001783 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001784 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001785
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001786 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001787 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001788 if (nid == 0)
1789 nid = get_amp_nid_(kctl->private_value);
1790 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001791 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1792 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001793 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001794 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001795 err = snd_ctl_add(codec->bus->card, kctl);
1796 if (err < 0)
1797 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001798 item = snd_array_new(&codec->mixers);
1799 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001800 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001801 item->kctl = kctl;
1802 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001803 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001804 return 0;
1805}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001806EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001807
Takashi Iwaid5191e52009-11-16 14:58:17 +01001808/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001809 * snd_hda_add_nid - Assign a NID to a control element
1810 * @codec: HD-audio codec
1811 * @nid: corresponding NID (optional)
1812 * @kctl: the control element to assign
1813 * @index: index to kctl
1814 *
1815 * Add the given control element to an array inside the codec instance.
1816 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1817 * NID:KCTL mapping - for example "Capture Source" selector.
1818 */
1819int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1820 unsigned int index, hda_nid_t nid)
1821{
1822 struct hda_nid_item *item;
1823
1824 if (nid > 0) {
1825 item = snd_array_new(&codec->nids);
1826 if (!item)
1827 return -ENOMEM;
1828 item->kctl = kctl;
1829 item->index = index;
1830 item->nid = nid;
1831 return 0;
1832 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01001833 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1834 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001835 return -EINVAL;
1836}
1837EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1838
1839/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001840 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1841 * @codec: HD-audio codec
1842 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001843void snd_hda_ctls_clear(struct hda_codec *codec)
1844{
1845 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001846 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001847 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001848 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001849 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001850 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001851}
1852
Takashi Iwaia65d6292009-02-23 16:57:04 +01001853/* pseudo device locking
1854 * toggle card->shutdown to allow/disallow the device access (as a hack)
1855 */
1856static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001857{
Takashi Iwaia65d6292009-02-23 16:57:04 +01001858 spin_lock(&card->files_lock);
1859 if (card->shutdown) {
1860 spin_unlock(&card->files_lock);
1861 return -EINVAL;
1862 }
1863 card->shutdown = 1;
1864 spin_unlock(&card->files_lock);
1865 return 0;
1866}
1867
1868static void hda_unlock_devices(struct snd_card *card)
1869{
1870 spin_lock(&card->files_lock);
1871 card->shutdown = 0;
1872 spin_unlock(&card->files_lock);
1873}
1874
Takashi Iwaid5191e52009-11-16 14:58:17 +01001875/**
1876 * snd_hda_codec_reset - Clear all objects assigned to the codec
1877 * @codec: HD-audio codec
1878 *
1879 * This frees the all PCM and control elements assigned to the codec, and
1880 * clears the caches and restores the pin default configurations.
1881 *
1882 * When a device is being used, it returns -EBSY. If successfully freed,
1883 * returns zero.
1884 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001885int snd_hda_codec_reset(struct hda_codec *codec)
1886{
1887 struct snd_card *card = codec->bus->card;
1888 int i, pcm;
1889
1890 if (hda_lock_devices(card) < 0)
1891 return -EBUSY;
1892 /* check whether the codec isn't used by any mixer or PCM streams */
1893 if (!list_empty(&card->ctl_files)) {
1894 hda_unlock_devices(card);
1895 return -EBUSY;
1896 }
1897 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1898 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1899 if (!cpcm->pcm)
1900 continue;
1901 if (cpcm->pcm->streams[0].substream_opened ||
1902 cpcm->pcm->streams[1].substream_opened) {
1903 hda_unlock_devices(card);
1904 return -EBUSY;
1905 }
1906 }
1907
1908 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001909
1910#ifdef CONFIG_SND_HDA_POWER_SAVE
1911 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001912 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001913#endif
1914 snd_hda_ctls_clear(codec);
1915 /* relase PCMs */
1916 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001917 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01001918 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001919 clear_bit(codec->pcm_info[i].device,
1920 codec->bus->pcm_dev_bits);
1921 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001922 }
1923 if (codec->patch_ops.free)
1924 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001925 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001926 codec->spec = NULL;
1927 free_hda_cache(&codec->amp_cache);
1928 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001929 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1930 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01001931 /* free only driver_pins so that init_pins + user_pins are restored */
1932 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001933 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001934 codec->num_pcms = 0;
1935 codec->pcm_info = NULL;
1936 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01001937 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1938 codec->slave_dig_outs = NULL;
1939 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001940 module_put(codec->owner);
1941 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001942
1943 /* allow device access again */
1944 hda_unlock_devices(card);
1945 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001946}
1947
Takashi Iwaid5191e52009-11-16 14:58:17 +01001948/**
1949 * snd_hda_add_vmaster - create a virtual master control and add slaves
1950 * @codec: HD-audio codec
1951 * @name: vmaster control name
1952 * @tlv: TLV data (optional)
1953 * @slaves: slave control names (optional)
1954 *
1955 * Create a virtual master control with the given name. The TLV data
1956 * must be either NULL or a valid data.
1957 *
1958 * @slaves is a NULL-terminated array of strings, each of which is a
1959 * slave control name. All controls with these names are assigned to
1960 * the new virtual master control.
1961 *
1962 * This function returns zero if successful or a negative error code.
1963 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01001964int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1965 unsigned int *tlv, const char **slaves)
1966{
1967 struct snd_kcontrol *kctl;
1968 const char **s;
1969 int err;
1970
Takashi Iwai2f085542008-02-22 18:43:50 +01001971 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1972 ;
1973 if (!*s) {
1974 snd_printdd("No slave found for %s\n", name);
1975 return 0;
1976 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001977 kctl = snd_ctl_make_virtual_master(name, tlv);
1978 if (!kctl)
1979 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001980 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001981 if (err < 0)
1982 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001983
Takashi Iwai2134ea42008-01-10 16:53:55 +01001984 for (s = slaves; *s; s++) {
1985 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01001986 int i = 0;
1987 for (;;) {
1988 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1989 if (!sctl) {
1990 if (!i)
1991 snd_printdd("Cannot find slave %s, "
1992 "skipped\n", *s);
1993 break;
1994 }
1995 err = snd_ctl_add_slave(kctl, sctl);
1996 if (err < 0)
1997 return err;
1998 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001999 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002000 }
2001 return 0;
2002}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002003EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002004
Takashi Iwaid5191e52009-11-16 14:58:17 +01002005/**
2006 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2007 *
2008 * The control element is supposed to have the private_value field
2009 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2010 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002011int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2012 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 int chs = get_amp_channels(kcontrol);
2015
2016 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2017 uinfo->count = chs == 3 ? 2 : 1;
2018 uinfo->value.integer.min = 0;
2019 uinfo->value.integer.max = 1;
2020 return 0;
2021}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002022EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Takashi Iwaid5191e52009-11-16 14:58:17 +01002024/**
2025 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2026 *
2027 * The control element is supposed to have the private_value field
2028 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2029 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002030int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2031 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2034 hda_nid_t nid = get_amp_nid(kcontrol);
2035 int chs = get_amp_channels(kcontrol);
2036 int dir = get_amp_direction(kcontrol);
2037 int idx = get_amp_index(kcontrol);
2038 long *valp = ucontrol->value.integer.value;
2039
2040 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002041 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002042 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002044 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002045 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return 0;
2047}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002048EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Takashi Iwaid5191e52009-11-16 14:58:17 +01002050/**
2051 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2052 *
2053 * The control element is supposed to have the private_value field
2054 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2055 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002056int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2060 hda_nid_t nid = get_amp_nid(kcontrol);
2061 int chs = get_amp_channels(kcontrol);
2062 int dir = get_amp_direction(kcontrol);
2063 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 long *valp = ucontrol->value.integer.value;
2065 int change = 0;
2066
Takashi Iwaicb53c622007-08-10 17:21:45 +02002067 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002068 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002069 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002070 HDA_AMP_MUTE,
2071 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002072 valp++;
2073 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002074 if (chs & 2)
2075 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002076 HDA_AMP_MUTE,
2077 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002078#ifdef CONFIG_SND_HDA_POWER_SAVE
2079 if (codec->patch_ops.check_power_status)
2080 codec->patch_ops.check_power_status(codec, nid);
2081#endif
2082 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return change;
2084}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002085EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Takashi Iwai67d634c2009-11-16 15:35:59 +01002087#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002088/**
2089 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2090 *
2091 * This function calls snd_hda_enable_beep_device(), which behaves differently
2092 * depending on beep_mode option.
2093 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002094int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2095 struct snd_ctl_elem_value *ucontrol)
2096{
2097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2098 long *valp = ucontrol->value.integer.value;
2099
2100 snd_hda_enable_beep_device(codec, *valp);
2101 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2102}
2103EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002104#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106/*
Takashi Iwai985be542005-11-02 18:26:49 +01002107 * bound volume controls
2108 *
2109 * bind multiple volumes (# indices, from 0)
2110 */
2111
2112#define AMP_VAL_IDX_SHIFT 19
2113#define AMP_VAL_IDX_MASK (0x0f<<19)
2114
Takashi Iwaid5191e52009-11-16 14:58:17 +01002115/**
2116 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2117 *
2118 * The control element is supposed to have the private_value field
2119 * set up via HDA_BIND_MUTE*() macros.
2120 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002121int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2122 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002123{
2124 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2125 unsigned long pval;
2126 int err;
2127
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002128 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002129 pval = kcontrol->private_value;
2130 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2131 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2132 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002133 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002134 return err;
2135}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002136EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002137
Takashi Iwaid5191e52009-11-16 14:58:17 +01002138/**
2139 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2140 *
2141 * The control element is supposed to have the private_value field
2142 * set up via HDA_BIND_MUTE*() macros.
2143 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002144int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2145 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002146{
2147 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2148 unsigned long pval;
2149 int i, indices, err = 0, change = 0;
2150
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002151 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002152 pval = kcontrol->private_value;
2153 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2154 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002155 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2156 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002157 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2158 if (err < 0)
2159 break;
2160 change |= err;
2161 }
2162 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002163 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002164 return err < 0 ? err : change;
2165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002166EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002167
Takashi Iwaid5191e52009-11-16 14:58:17 +01002168/**
2169 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2170 *
2171 * The control element is supposed to have the private_value field
2172 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002173 */
2174int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_info *uinfo)
2176{
2177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2178 struct hda_bind_ctls *c;
2179 int err;
2180
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002181 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002182 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002183 kcontrol->private_value = *c->values;
2184 err = c->ops->info(kcontrol, uinfo);
2185 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002186 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002187 return err;
2188}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002189EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002190
Takashi Iwaid5191e52009-11-16 14:58:17 +01002191/**
2192 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2193 *
2194 * The control element is supposed to have the private_value field
2195 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2196 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002197int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2198 struct snd_ctl_elem_value *ucontrol)
2199{
2200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2201 struct hda_bind_ctls *c;
2202 int err;
2203
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002204 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002205 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002206 kcontrol->private_value = *c->values;
2207 err = c->ops->get(kcontrol, ucontrol);
2208 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002209 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002210 return err;
2211}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002212EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002213
Takashi Iwaid5191e52009-11-16 14:58:17 +01002214/**
2215 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2216 *
2217 * The control element is supposed to have the private_value field
2218 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2219 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002220int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2221 struct snd_ctl_elem_value *ucontrol)
2222{
2223 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2224 struct hda_bind_ctls *c;
2225 unsigned long *vals;
2226 int err = 0, change = 0;
2227
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002228 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002229 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002230 for (vals = c->values; *vals; vals++) {
2231 kcontrol->private_value = *vals;
2232 err = c->ops->put(kcontrol, ucontrol);
2233 if (err < 0)
2234 break;
2235 change |= err;
2236 }
2237 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002238 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002239 return err < 0 ? err : change;
2240}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002241EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002242
Takashi Iwaid5191e52009-11-16 14:58:17 +01002243/**
2244 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2245 *
2246 * The control element is supposed to have the private_value field
2247 * set up via HDA_BIND_VOL() macro.
2248 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002249int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2250 unsigned int size, unsigned int __user *tlv)
2251{
2252 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2253 struct hda_bind_ctls *c;
2254 int err;
2255
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002256 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002257 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002258 kcontrol->private_value = *c->values;
2259 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2260 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002261 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002262 return err;
2263}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002264EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002265
2266struct hda_ctl_ops snd_hda_bind_vol = {
2267 .info = snd_hda_mixer_amp_volume_info,
2268 .get = snd_hda_mixer_amp_volume_get,
2269 .put = snd_hda_mixer_amp_volume_put,
2270 .tlv = snd_hda_mixer_amp_tlv
2271};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002272EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002273
2274struct hda_ctl_ops snd_hda_bind_sw = {
2275 .info = snd_hda_mixer_amp_switch_info,
2276 .get = snd_hda_mixer_amp_switch_get,
2277 .put = snd_hda_mixer_amp_switch_put,
2278 .tlv = snd_hda_mixer_amp_tlv
2279};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002280EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002281
2282/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 * SPDIF out controls
2284 */
2285
Takashi Iwai0ba21762007-04-16 11:29:14 +02002286static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
2289 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2290 uinfo->count = 1;
2291 return 0;
2292}
2293
Takashi Iwai0ba21762007-04-16 11:29:14 +02002294static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2295 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2298 IEC958_AES0_NONAUDIO |
2299 IEC958_AES0_CON_EMPHASIS_5015 |
2300 IEC958_AES0_CON_NOT_COPYRIGHT;
2301 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2302 IEC958_AES1_CON_ORIGINAL;
2303 return 0;
2304}
2305
Takashi Iwai0ba21762007-04-16 11:29:14 +02002306static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2307 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2310 IEC958_AES0_NONAUDIO |
2311 IEC958_AES0_PRO_EMPHASIS_5015;
2312 return 0;
2313}
2314
Takashi Iwai0ba21762007-04-16 11:29:14 +02002315static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2316 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2319
2320 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2321 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2322 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2323 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2324
2325 return 0;
2326}
2327
2328/* convert from SPDIF status bits to HDA SPDIF bits
2329 * bit 0 (DigEn) is always set zero (to be filled later)
2330 */
2331static unsigned short convert_from_spdif_status(unsigned int sbits)
2332{
2333 unsigned short val = 0;
2334
2335 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002336 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002338 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002340 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2341 IEC958_AES0_PRO_EMPHASIS_5015)
2342 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002344 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2345 IEC958_AES0_CON_EMPHASIS_5015)
2346 val |= AC_DIG1_EMPHASIS;
2347 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2348 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002350 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2352 }
2353 return val;
2354}
2355
2356/* convert to SPDIF status bits from HDA SPDIF bits
2357 */
2358static unsigned int convert_to_spdif_status(unsigned short val)
2359{
2360 unsigned int sbits = 0;
2361
Takashi Iwai0ba21762007-04-16 11:29:14 +02002362 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002364 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 sbits |= IEC958_AES0_PROFESSIONAL;
2366 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002367 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2369 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002370 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002372 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002374 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2376 sbits |= val & (0x7f << 8);
2377 }
2378 return sbits;
2379}
2380
Takashi Iwai2f728532008-09-25 16:32:41 +02002381/* set digital convert verbs both for the given NID and its slaves */
2382static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2383 int verb, int val)
2384{
2385 hda_nid_t *d;
2386
Takashi Iwai9e976972008-11-25 08:17:20 +01002387 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002388 d = codec->slave_dig_outs;
2389 if (!d)
2390 return;
2391 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002392 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002393}
2394
2395static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2396 int dig1, int dig2)
2397{
2398 if (dig1 != -1)
2399 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2400 if (dig2 != -1)
2401 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2402}
2403
Takashi Iwai0ba21762007-04-16 11:29:14 +02002404static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2405 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
2407 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2408 hda_nid_t nid = kcontrol->private_value;
2409 unsigned short val;
2410 int change;
2411
Ingo Molnar62932df2006-01-16 16:34:20 +01002412 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 codec->spdif_status = ucontrol->value.iec958.status[0] |
2414 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2415 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2416 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2417 val = convert_from_spdif_status(codec->spdif_status);
2418 val |= codec->spdif_ctls & 1;
2419 change = codec->spdif_ctls != val;
2420 codec->spdif_ctls = val;
2421
Takashi Iwai2f728532008-09-25 16:32:41 +02002422 if (change)
2423 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Ingo Molnar62932df2006-01-16 16:34:20 +01002425 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return change;
2427}
2428
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002429#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Takashi Iwai0ba21762007-04-16 11:29:14 +02002431static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2432 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
2434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2435
Takashi Iwai0ba21762007-04-16 11:29:14 +02002436 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return 0;
2438}
2439
Takashi Iwai0ba21762007-04-16 11:29:14 +02002440static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
2443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2444 hda_nid_t nid = kcontrol->private_value;
2445 unsigned short val;
2446 int change;
2447
Ingo Molnar62932df2006-01-16 16:34:20 +01002448 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002449 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002451 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002453 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002455 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002456 /* unmute amp switch (if any) */
2457 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002458 (val & AC_DIG1_ENABLE))
2459 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2460 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002462 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return change;
2464}
2465
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002466static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 {
2468 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2469 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002470 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 .info = snd_hda_spdif_mask_info,
2472 .get = snd_hda_spdif_cmask_get,
2473 },
2474 {
2475 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2476 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002477 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 .info = snd_hda_spdif_mask_info,
2479 .get = snd_hda_spdif_pmask_get,
2480 },
2481 {
2482 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002483 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 .info = snd_hda_spdif_mask_info,
2485 .get = snd_hda_spdif_default_get,
2486 .put = snd_hda_spdif_default_put,
2487 },
2488 {
2489 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002490 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 .info = snd_hda_spdif_out_switch_info,
2492 .get = snd_hda_spdif_out_switch_get,
2493 .put = snd_hda_spdif_out_switch_put,
2494 },
2495 { } /* end */
2496};
2497
Takashi Iwai09f99702008-02-04 12:31:13 +01002498#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500/**
2501 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2502 * @codec: the HDA codec
2503 * @nid: audio out widget NID
2504 *
2505 * Creates controls related with the SPDIF output.
2506 * Called from each patch supporting the SPDIF out.
2507 *
2508 * Returns 0 if successful, or a negative error code.
2509 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002510int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002513 struct snd_kcontrol *kctl;
2514 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002515 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Takashi Iwai09f99702008-02-04 12:31:13 +01002517 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2518 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2519 idx))
2520 break;
2521 }
2522 if (idx >= SPDIF_MAX_IDX) {
2523 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2524 return -EBUSY;
2525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2527 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002528 if (!kctl)
2529 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002530 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002532 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002533 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return err;
2535 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002536 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002537 snd_hda_codec_read(codec, nid, 0,
2538 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2540 return 0;
2541}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002542EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002545 * SPDIF sharing with analog output
2546 */
2547static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2548 struct snd_ctl_elem_value *ucontrol)
2549{
2550 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2551 ucontrol->value.integer.value[0] = mout->share_spdif;
2552 return 0;
2553}
2554
2555static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2556 struct snd_ctl_elem_value *ucontrol)
2557{
2558 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2559 mout->share_spdif = !!ucontrol->value.integer.value[0];
2560 return 0;
2561}
2562
2563static struct snd_kcontrol_new spdif_share_sw = {
2564 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2565 .name = "IEC958 Default PCM Playback Switch",
2566 .info = snd_ctl_boolean_mono_info,
2567 .get = spdif_share_sw_get,
2568 .put = spdif_share_sw_put,
2569};
2570
Takashi Iwaid5191e52009-11-16 14:58:17 +01002571/**
2572 * snd_hda_create_spdif_share_sw - create Default PCM switch
2573 * @codec: the HDA codec
2574 * @mout: multi-out instance
2575 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002576int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2577 struct hda_multi_out *mout)
2578{
2579 if (!mout->dig_out_nid)
2580 return 0;
2581 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002582 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2583 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002584}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002585EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002586
2587/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 * SPDIF input
2589 */
2590
2591#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2592
Takashi Iwai0ba21762007-04-16 11:29:14 +02002593static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597
2598 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2599 return 0;
2600}
2601
Takashi Iwai0ba21762007-04-16 11:29:14 +02002602static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2603 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2606 hda_nid_t nid = kcontrol->private_value;
2607 unsigned int val = !!ucontrol->value.integer.value[0];
2608 int change;
2609
Ingo Molnar62932df2006-01-16 16:34:20 +01002610 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002612 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002614 snd_hda_codec_write_cache(codec, nid, 0,
2615 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002617 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return change;
2619}
2620
Takashi Iwai0ba21762007-04-16 11:29:14 +02002621static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2622 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
2624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2625 hda_nid_t nid = kcontrol->private_value;
2626 unsigned short val;
2627 unsigned int sbits;
2628
Andrew Paprocki3982d172007-12-19 12:13:44 +01002629 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 sbits = convert_to_spdif_status(val);
2631 ucontrol->value.iec958.status[0] = sbits;
2632 ucontrol->value.iec958.status[1] = sbits >> 8;
2633 ucontrol->value.iec958.status[2] = sbits >> 16;
2634 ucontrol->value.iec958.status[3] = sbits >> 24;
2635 return 0;
2636}
2637
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002638static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 {
2640 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002641 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 .info = snd_hda_spdif_in_switch_info,
2643 .get = snd_hda_spdif_in_switch_get,
2644 .put = snd_hda_spdif_in_switch_put,
2645 },
2646 {
2647 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2648 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002649 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 .info = snd_hda_spdif_mask_info,
2651 .get = snd_hda_spdif_in_status_get,
2652 },
2653 { } /* end */
2654};
2655
2656/**
2657 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2658 * @codec: the HDA codec
2659 * @nid: audio in widget NID
2660 *
2661 * Creates controls related with the SPDIF input.
2662 * Called from each patch supporting the SPDIF in.
2663 *
2664 * Returns 0 if successful, or a negative error code.
2665 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002666int snd_hda_create_spdif_in_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 Capture Switch",
2675 idx))
2676 break;
2677 }
2678 if (idx >= SPDIF_MAX_IDX) {
2679 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2680 return -EBUSY;
2681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2683 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002684 if (!kctl)
2685 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002687 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002688 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return err;
2690 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002691 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002692 snd_hda_codec_read(codec, nid, 0,
2693 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002694 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return 0;
2696}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002697EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Takashi Iwaicb53c622007-08-10 17:21:45 +02002699#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002700/*
2701 * command cache
2702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002704/* build a 32bit cache key with the widget id and the command parameter */
2705#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2706#define get_cmd_cache_nid(key) ((key) & 0xff)
2707#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2708
2709/**
2710 * snd_hda_codec_write_cache - send a single command with caching
2711 * @codec: the HDA codec
2712 * @nid: NID to send the command
2713 * @direct: direct flag
2714 * @verb: the verb to send
2715 * @parm: the parameter for the verb
2716 *
2717 * Send a single command without waiting for response.
2718 *
2719 * Returns 0 if successful, or a negative error code.
2720 */
2721int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2722 int direct, unsigned int verb, unsigned int parm)
2723{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002724 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2725 struct hda_cache_head *c;
2726 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002727
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002728 if (err < 0)
2729 return err;
2730 /* parm may contain the verb stuff for get/set amp */
2731 verb = verb | (parm >> 8);
2732 parm &= 0xff;
2733 key = build_cmd_cache_key(nid, verb);
2734 mutex_lock(&codec->bus->cmd_mutex);
2735 c = get_alloc_hash(&codec->cmd_cache, key);
2736 if (c)
2737 c->val = parm;
2738 mutex_unlock(&codec->bus->cmd_mutex);
2739 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002740}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002741EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002742
Takashi Iwaid5191e52009-11-16 14:58:17 +01002743/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002744 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2745 * @codec: the HDA codec
2746 * @nid: NID to send the command
2747 * @direct: direct flag
2748 * @verb: the verb to send
2749 * @parm: the parameter for the verb
2750 *
2751 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2752 * command if the parameter is already identical with the cached value.
2753 * If not, it sends the command and refreshes the cache.
2754 *
2755 * Returns 0 if successful, or a negative error code.
2756 */
2757int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2758 int direct, unsigned int verb, unsigned int parm)
2759{
2760 struct hda_cache_head *c;
2761 u32 key;
2762
2763 /* parm may contain the verb stuff for get/set amp */
2764 verb = verb | (parm >> 8);
2765 parm &= 0xff;
2766 key = build_cmd_cache_key(nid, verb);
2767 mutex_lock(&codec->bus->cmd_mutex);
2768 c = get_hash(&codec->cmd_cache, key);
2769 if (c && c->val == parm) {
2770 mutex_unlock(&codec->bus->cmd_mutex);
2771 return 0;
2772 }
2773 mutex_unlock(&codec->bus->cmd_mutex);
2774 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2775}
2776EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2777
2778/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002779 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2780 * @codec: HD-audio codec
2781 *
2782 * Execute all verbs recorded in the command caches to resume.
2783 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002784void snd_hda_codec_resume_cache(struct hda_codec *codec)
2785{
Takashi Iwai603c4012008-07-30 15:01:44 +02002786 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002787 int i;
2788
Takashi Iwai603c4012008-07-30 15:01:44 +02002789 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002790 u32 key = buffer->key;
2791 if (!key)
2792 continue;
2793 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2794 get_cmd_cache_cmd(key), buffer->val);
2795 }
2796}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002797EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002798
2799/**
2800 * snd_hda_sequence_write_cache - sequence writes with caching
2801 * @codec: the HDA codec
2802 * @seq: VERB array to send
2803 *
2804 * Send the commands sequentially from the given array.
2805 * Thte commands are recorded on cache for power-save and resume.
2806 * The array must be terminated with NID=0.
2807 */
2808void snd_hda_sequence_write_cache(struct hda_codec *codec,
2809 const struct hda_verb *seq)
2810{
2811 for (; seq->nid; seq++)
2812 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2813 seq->param);
2814}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002815EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002816#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002817
Takashi Iwai54d17402005-11-21 16:33:22 +01002818/*
2819 * set power state of the codec
2820 */
2821static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2822 unsigned int power_state)
2823{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002824 hda_nid_t nid;
2825 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002826
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002827 /* this delay seems necessary to avoid click noise at power-down */
2828 if (power_state == AC_PWRST_D3)
2829 msleep(100);
2830 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002831 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002832 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002833 if (power_state == AC_PWRST_D0 &&
2834 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002835 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002836
Takashi Iwaicb53c622007-08-10 17:21:45 +02002837 nid = codec->start_nid;
2838 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002839 unsigned int wcaps = get_wcaps(codec, nid);
2840 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002841 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002842 if (power_state == AC_PWRST_D3 &&
2843 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002844 unsigned int pincap;
2845 /*
2846 * don't power down the widget if it controls
2847 * eapd and EAPD_BTLENABLE is set.
2848 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01002849 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002850 if (pincap & AC_PINCAP_EAPD) {
2851 int eapd = snd_hda_codec_read(codec,
2852 nid, 0,
2853 AC_VERB_GET_EAPD_BTLENABLE, 0);
2854 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002855 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002856 continue;
2857 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002858 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002859 snd_hda_codec_write(codec, nid, 0,
2860 AC_VERB_SET_POWER_STATE,
2861 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002862 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002863 }
2864
Takashi Iwaicb53c622007-08-10 17:21:45 +02002865 if (power_state == AC_PWRST_D0) {
2866 unsigned long end_time;
2867 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002868 /* wait until the codec reachs to D0 */
2869 end_time = jiffies + msecs_to_jiffies(500);
2870 do {
2871 state = snd_hda_codec_read(codec, fg, 0,
2872 AC_VERB_GET_POWER_STATE, 0);
2873 if (state == power_state)
2874 break;
2875 msleep(1);
2876 } while (time_after_eq(end_time, jiffies));
2877 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002878}
2879
Takashi Iwai11aeff02008-07-30 15:01:46 +02002880#ifdef CONFIG_SND_HDA_HWDEP
2881/* execute additional init verbs */
2882static void hda_exec_init_verbs(struct hda_codec *codec)
2883{
2884 if (codec->init_verbs.list)
2885 snd_hda_sequence_write(codec, codec->init_verbs.list);
2886}
2887#else
2888static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2889#endif
2890
Takashi Iwaicb53c622007-08-10 17:21:45 +02002891#ifdef SND_HDA_NEEDS_RESUME
2892/*
2893 * call suspend and power-down; used both from PM and power-save
2894 */
2895static void hda_call_codec_suspend(struct hda_codec *codec)
2896{
2897 if (codec->patch_ops.suspend)
2898 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2899 hda_set_power_state(codec,
2900 codec->afg ? codec->afg : codec->mfg,
2901 AC_PWRST_D3);
2902#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01002903 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002904 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002905 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002906 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01002907 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002908#endif
2909}
2910
2911/*
2912 * kick up codec; used both from PM and power-save
2913 */
2914static void hda_call_codec_resume(struct hda_codec *codec)
2915{
2916 hda_set_power_state(codec,
2917 codec->afg ? codec->afg : codec->mfg,
2918 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01002919 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwai11aeff02008-07-30 15:01:46 +02002920 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002921 if (codec->patch_ops.resume)
2922 codec->patch_ops.resume(codec);
2923 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002924 if (codec->patch_ops.init)
2925 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002926 snd_hda_codec_resume_amp(codec);
2927 snd_hda_codec_resume_cache(codec);
2928 }
2929}
2930#endif /* SND_HDA_NEEDS_RESUME */
2931
Takashi Iwai54d17402005-11-21 16:33:22 +01002932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933/**
2934 * snd_hda_build_controls - build mixer controls
2935 * @bus: the BUS
2936 *
2937 * Creates mixer controls for each codec included in the bus.
2938 *
2939 * Returns 0 if successful, otherwise a negative error code.
2940 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002941int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002943 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Takashi Iwai0ba21762007-04-16 11:29:14 +02002945 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002946 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002947 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01002948 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002949 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002950 err = snd_hda_codec_reset(codec);
2951 if (err < 0) {
2952 printk(KERN_ERR
2953 "hda_codec: cannot revert codec\n");
2954 return err;
2955 }
2956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002958 return 0;
2959}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002960EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002962int snd_hda_codec_build_controls(struct hda_codec *codec)
2963{
2964 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02002965 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002966 /* continue to initialize... */
2967 if (codec->patch_ops.init)
2968 err = codec->patch_ops.init(codec);
2969 if (!err && codec->patch_ops.build_controls)
2970 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002971 if (err < 0)
2972 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return 0;
2974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976/*
2977 * stream formats
2978 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002979struct hda_rate_tbl {
2980 unsigned int hz;
2981 unsigned int alsa_bits;
2982 unsigned int hda_fmt;
2983};
2984
2985static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002987
2988 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2990 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2991 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2992 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2993 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2994 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2995 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2996 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2997 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2998 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2999 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003000#define AC_PAR_PCM_RATE_BITS 11
3001 /* up to bits 10, 384kHZ isn't supported properly */
3002
3003 /* not autodetected value */
3004 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003005
Takashi Iwaibefdf312005-08-22 13:57:55 +02003006 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007};
3008
3009/**
3010 * snd_hda_calc_stream_format - calculate format bitset
3011 * @rate: the sample rate
3012 * @channels: the number of channels
3013 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3014 * @maxbps: the max. bps
3015 *
3016 * Calculate the format bitset from the given rate, channels and th PCM format.
3017 *
3018 * Return zero if invalid.
3019 */
3020unsigned int snd_hda_calc_stream_format(unsigned int rate,
3021 unsigned int channels,
3022 unsigned int format,
3023 unsigned int maxbps)
3024{
3025 int i;
3026 unsigned int val = 0;
3027
Takashi Iwaibefdf312005-08-22 13:57:55 +02003028 for (i = 0; rate_bits[i].hz; i++)
3029 if (rate_bits[i].hz == rate) {
3030 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 break;
3032 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003033 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 snd_printdd("invalid rate %d\n", rate);
3035 return 0;
3036 }
3037
3038 if (channels == 0 || channels > 8) {
3039 snd_printdd("invalid channels %d\n", channels);
3040 return 0;
3041 }
3042 val |= channels - 1;
3043
3044 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003045 case 8:
3046 val |= 0x00;
3047 break;
3048 case 16:
3049 val |= 0x10;
3050 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 case 20:
3052 case 24:
3053 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003054 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 val |= 0x40;
3056 else if (maxbps >= 24)
3057 val |= 0x30;
3058 else
3059 val |= 0x20;
3060 break;
3061 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003062 snd_printdd("invalid format width %d\n",
3063 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 return 0;
3065 }
3066
3067 return val;
3068}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003069EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003071static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3072{
3073 unsigned int val = 0;
3074 if (nid != codec->afg &&
3075 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3076 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3077 if (!val || val == -1)
3078 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3079 if (!val || val == -1)
3080 return 0;
3081 return val;
3082}
3083
3084static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3085{
3086 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3087 get_pcm_param);
3088}
3089
3090static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3091{
3092 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3093 if (!streams || streams == -1)
3094 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3095 if (!streams || streams == -1)
3096 return 0;
3097 return streams;
3098}
3099
3100static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3101{
3102 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3103 get_stream_param);
3104}
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106/**
3107 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3108 * @codec: the HDA codec
3109 * @nid: NID to query
3110 * @ratesp: the pointer to store the detected rate bitflags
3111 * @formatsp: the pointer to store the detected formats
3112 * @bpsp: the pointer to store the detected format widths
3113 *
3114 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3115 * or @bsps argument is ignored.
3116 *
3117 * Returns 0 if successful, otherwise a negative error code.
3118 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003119static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3121{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003122 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003124 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003125 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 if (ratesp) {
3128 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003129 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003131 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003133 if (rates == 0) {
3134 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3135 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3136 nid, val,
3137 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3138 return -EIO;
3139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 *ratesp = rates;
3141 }
3142
3143 if (formatsp || bpsp) {
3144 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003145 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003147 streams = query_stream_param(codec, nid);
3148 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
3151 bps = 0;
3152 if (streams & AC_SUPFMT_PCM) {
3153 if (val & AC_SUPPCM_BITS_8) {
3154 formats |= SNDRV_PCM_FMTBIT_U8;
3155 bps = 8;
3156 }
3157 if (val & AC_SUPPCM_BITS_16) {
3158 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3159 bps = 16;
3160 }
3161 if (wcaps & AC_WCAP_DIGITAL) {
3162 if (val & AC_SUPPCM_BITS_32)
3163 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3164 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3165 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3166 if (val & AC_SUPPCM_BITS_24)
3167 bps = 24;
3168 else if (val & AC_SUPPCM_BITS_20)
3169 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003170 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3171 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3173 if (val & AC_SUPPCM_BITS_32)
3174 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 else if (val & AC_SUPPCM_BITS_24)
3176 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003177 else if (val & AC_SUPPCM_BITS_20)
3178 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
3180 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003181 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003183 if (!bps)
3184 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003185 }
3186 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003187 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /* temporary hack: we have still no proper support
3189 * for the direct AC3 stream...
3190 */
3191 formats |= SNDRV_PCM_FMTBIT_U8;
3192 bps = 8;
3193 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003194 if (formats == 0) {
3195 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3196 "(nid=0x%x, val=0x%x, ovrd=%i, "
3197 "streams=0x%x)\n",
3198 nid, val,
3199 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3200 streams);
3201 return -EIO;
3202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (formatsp)
3204 *formatsp = formats;
3205 if (bpsp)
3206 *bpsp = bps;
3207 }
3208
3209 return 0;
3210}
3211
3212/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003213 * snd_hda_is_supported_format - Check the validity of the format
3214 * @codec: HD-audio codec
3215 * @nid: NID to check
3216 * @format: the HD-audio format value to check
3217 *
3218 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 *
3220 * Returns 1 if supported, 0 if not.
3221 */
3222int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3223 unsigned int format)
3224{
3225 int i;
3226 unsigned int val = 0, rate, stream;
3227
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003228 val = query_pcm_param(codec, nid);
3229 if (!val)
3230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
3232 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003233 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003234 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (val & (1 << i))
3236 break;
3237 return 0;
3238 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003239 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return 0;
3241
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003242 stream = query_stream_param(codec, nid);
3243 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return 0;
3245
3246 if (stream & AC_SUPFMT_PCM) {
3247 switch (format & 0xf0) {
3248 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003249 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return 0;
3251 break;
3252 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003253 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return 0;
3255 break;
3256 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003257 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return 0;
3259 break;
3260 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003261 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 return 0;
3263 break;
3264 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003265 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 return 0;
3267 break;
3268 default:
3269 return 0;
3270 }
3271 } else {
3272 /* FIXME: check for float32 and AC3? */
3273 }
3274
3275 return 1;
3276}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003277EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279/*
3280 * PCM stuff
3281 */
3282static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3283 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003284 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
3286 return 0;
3287}
3288
3289static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3290 struct hda_codec *codec,
3291 unsigned int stream_tag,
3292 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003293 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
3295 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3296 return 0;
3297}
3298
3299static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3300 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003301 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
Takashi Iwai888afa12008-03-18 09:57:50 +01003303 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return 0;
3305}
3306
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003307static int set_pcm_default_values(struct hda_codec *codec,
3308 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003310 int err;
3311
Takashi Iwai0ba21762007-04-16 11:29:14 +02003312 /* query support PCM information from the given NID */
3313 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003314 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003315 info->rates ? NULL : &info->rates,
3316 info->formats ? NULL : &info->formats,
3317 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003318 if (err < 0)
3319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321 if (info->ops.open == NULL)
3322 info->ops.open = hda_pcm_default_open_close;
3323 if (info->ops.close == NULL)
3324 info->ops.close = hda_pcm_default_open_close;
3325 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003326 if (snd_BUG_ON(!info->nid))
3327 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 info->ops.prepare = hda_pcm_default_prepare;
3329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003331 if (snd_BUG_ON(!info->nid))
3332 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 info->ops.cleanup = hda_pcm_default_cleanup;
3334 }
3335 return 0;
3336}
3337
Takashi Iwaid5191e52009-11-16 14:58:17 +01003338/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003339const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3340 "Audio", "SPDIF", "HDMI", "Modem"
3341};
3342
Takashi Iwai176d5332008-07-30 15:01:44 +02003343/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003344 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003345 *
3346 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003347 */
3348static int get_empty_pcm_device(struct hda_bus *bus, int type)
3349{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003350 /* audio device indices; not linear to keep compatibility */
3351 static int audio_idx[HDA_PCM_NTYPES][5] = {
3352 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3353 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003354 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003355 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003356 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003357 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003358
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003359 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003360 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3361 return -EINVAL;
3362 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003363
3364 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3365 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3366 return audio_idx[type][i];
3367
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003368 snd_printk(KERN_WARNING "Too many %s devices\n",
3369 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003370 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003371}
3372
3373/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003374 * attach a new PCM stream
3375 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003376static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003377{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003378 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003379 struct hda_pcm_stream *info;
3380 int stream, err;
3381
Takashi Iwaib91f0802008-11-04 08:43:08 +01003382 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003383 return -EINVAL;
3384 for (stream = 0; stream < 2; stream++) {
3385 info = &pcm->stream[stream];
3386 if (info->substreams) {
3387 err = set_pcm_default_values(codec, info);
3388 if (err < 0)
3389 return err;
3390 }
3391 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003392 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003393}
3394
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003395/* assign all PCMs of the given codec */
3396int snd_hda_codec_build_pcms(struct hda_codec *codec)
3397{
3398 unsigned int pcm;
3399 int err;
3400
3401 if (!codec->num_pcms) {
3402 if (!codec->patch_ops.build_pcms)
3403 return 0;
3404 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003405 if (err < 0) {
3406 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003407 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003408 err = snd_hda_codec_reset(codec);
3409 if (err < 0) {
3410 printk(KERN_ERR
3411 "hda_codec: cannot revert codec\n");
3412 return err;
3413 }
3414 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003415 }
3416 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3417 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3418 int dev;
3419
3420 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003421 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003422
3423 if (!cpcm->pcm) {
3424 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3425 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003426 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003427 cpcm->device = dev;
3428 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003429 if (err < 0) {
3430 printk(KERN_ERR "hda_codec: cannot attach "
3431 "PCM stream %d for codec #%d\n",
3432 dev, codec->addr);
3433 continue; /* no fatal error */
3434 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003435 }
3436 }
3437 return 0;
3438}
3439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440/**
3441 * snd_hda_build_pcms - build PCM information
3442 * @bus: the BUS
3443 *
3444 * Create PCM information for each codec included in the bus.
3445 *
3446 * The build_pcms codec patch is requested to set up codec->num_pcms and
3447 * codec->pcm_info properly. The array is referred by the top-level driver
3448 * to create its PCM instances.
3449 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3450 * callback.
3451 *
3452 * At least, substreams, channels_min and channels_max must be filled for
3453 * each stream. substreams = 0 indicates that the stream doesn't exist.
3454 * When rates and/or formats are zero, the supported values are queried
3455 * from the given nid. The nid is used also by the default ops.prepare
3456 * and ops.cleanup callbacks.
3457 *
3458 * The driver needs to call ops.open in its open callback. Similarly,
3459 * ops.close is supposed to be called in the close callback.
3460 * ops.prepare should be called in the prepare or hw_params callback
3461 * with the proper parameters for set up.
3462 * ops.cleanup should be called in hw_free for clean up of streams.
3463 *
3464 * This function returns 0 if successfull, or a negative error code.
3465 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003466int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003468 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Takashi Iwai0ba21762007-04-16 11:29:14 +02003470 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003471 int err = snd_hda_codec_build_pcms(codec);
3472 if (err < 0)
3473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 }
3475 return 0;
3476}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003477EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479/**
3480 * snd_hda_check_board_config - compare the current codec with the config table
3481 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003482 * @num_configs: number of config enums
3483 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 * @tbl: configuration table, terminated by null entries
3485 *
3486 * Compares the modelname or PCI subsystem id of the current codec with the
3487 * given configuration table. If a matching entry is found, returns its
3488 * config value (supposed to be 0 or positive).
3489 *
3490 * If no entries are matching, the function returns a negative value.
3491 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003492int snd_hda_check_board_config(struct hda_codec *codec,
3493 int num_configs, const char **models,
3494 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003496 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003497 int i;
3498 for (i = 0; i < num_configs; i++) {
3499 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003500 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003501 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3502 "selected\n", models[i]);
3503 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
3505 }
3506 }
3507
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003508 if (!codec->bus->pci || !tbl)
3509 return -1;
3510
3511 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3512 if (!tbl)
3513 return -1;
3514 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003515#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003516 char tmp[10];
3517 const char *model = NULL;
3518 if (models)
3519 model = models[tbl->value];
3520 if (!model) {
3521 sprintf(tmp, "#%d", tbl->value);
3522 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003524 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3525 "for config %x:%x (%s)\n",
3526 model, tbl->subvendor, tbl->subdevice,
3527 (tbl->name ? tbl->name : "Unknown device"));
3528#endif
3529 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 }
3531 return -1;
3532}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003533EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
3535/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003536 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003537 subsystem ID with the
3538 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003539
3540 This is important for Gateway notebooks with SB450 HDA Audio
3541 where the vendor ID of the PCI device is:
3542 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3543 and the vendor/subvendor are found only at the codec.
3544
3545 * @codec: the HDA codec
3546 * @num_configs: number of config enums
3547 * @models: array of model name strings
3548 * @tbl: configuration table, terminated by null entries
3549 *
3550 * Compares the modelname or PCI subsystem id of the current codec with the
3551 * given configuration table. If a matching entry is found, returns its
3552 * config value (supposed to be 0 or positive).
3553 *
3554 * If no entries are matching, the function returns a negative value.
3555 */
3556int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3557 int num_configs, const char **models,
3558 const struct snd_pci_quirk *tbl)
3559{
3560 const struct snd_pci_quirk *q;
3561
3562 /* Search for codec ID */
3563 for (q = tbl; q->subvendor; q++) {
3564 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3565
3566 if (vendorid == codec->subsystem_id)
3567 break;
3568 }
3569
3570 if (!q->subvendor)
3571 return -1;
3572
3573 tbl = q;
3574
3575 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003576#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003577 char tmp[10];
3578 const char *model = NULL;
3579 if (models)
3580 model = models[tbl->value];
3581 if (!model) {
3582 sprintf(tmp, "#%d", tbl->value);
3583 model = tmp;
3584 }
3585 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3586 "for config %x:%x (%s)\n",
3587 model, tbl->subvendor, tbl->subdevice,
3588 (tbl->name ? tbl->name : "Unknown device"));
3589#endif
3590 return tbl->value;
3591 }
3592 return -1;
3593}
3594EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3595
3596/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 * snd_hda_add_new_ctls - create controls from the array
3598 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003599 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 *
3601 * This helper function creates and add new controls in the given array.
3602 * The array must be terminated with an empty entry as terminator.
3603 *
3604 * Returns 0 if successful, or a negative error code.
3605 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003606int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003608 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003611 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003612 if (knew->iface == -1) /* skip this codec private value */
3613 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003614 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003615 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003616 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003617 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003618 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003619 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003620 return err;
3621 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003622 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003623 return -ENOMEM;
3624 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003625 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003626 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003627 return err;
3628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
3630 return 0;
3631}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003632EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Takashi Iwaicb53c622007-08-10 17:21:45 +02003634#ifdef CONFIG_SND_HDA_POWER_SAVE
3635static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3636 unsigned int power_state);
3637
3638static void hda_power_work(struct work_struct *work)
3639{
3640 struct hda_codec *codec =
3641 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003642 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003643
Maxim Levitsky2e492462007-09-03 15:26:57 +02003644 if (!codec->power_on || codec->power_count) {
3645 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003646 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003647 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003648
3649 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003650 if (bus->ops.pm_notify)
3651 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003652}
3653
3654static void hda_keep_power_on(struct hda_codec *codec)
3655{
3656 codec->power_count++;
3657 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003658 codec->power_jiffies = jiffies;
3659}
3660
Takashi Iwaid5191e52009-11-16 14:58:17 +01003661/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003662void snd_hda_update_power_acct(struct hda_codec *codec)
3663{
3664 unsigned long delta = jiffies - codec->power_jiffies;
3665 if (codec->power_on)
3666 codec->power_on_acct += delta;
3667 else
3668 codec->power_off_acct += delta;
3669 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003670}
3671
Takashi Iwaid5191e52009-11-16 14:58:17 +01003672/**
3673 * snd_hda_power_up - Power-up the codec
3674 * @codec: HD-audio codec
3675 *
3676 * Increment the power-up counter and power up the hardware really when
3677 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003678 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003679void snd_hda_power_up(struct hda_codec *codec)
3680{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003681 struct hda_bus *bus = codec->bus;
3682
Takashi Iwaicb53c622007-08-10 17:21:45 +02003683 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003684 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003685 return;
3686
Takashi Iwaia2f63092009-11-11 09:34:25 +01003687 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003688 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003689 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003690 if (bus->ops.pm_notify)
3691 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003692 hda_call_codec_resume(codec);
3693 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003694 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003695}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003696EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003697
3698#define power_save(codec) \
3699 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003700
Takashi Iwaid5191e52009-11-16 14:58:17 +01003701/**
3702 * snd_hda_power_down - Power-down the codec
3703 * @codec: HD-audio codec
3704 *
3705 * Decrement the power-up counter and schedules the power-off work if
3706 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003707 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003708void snd_hda_power_down(struct hda_codec *codec)
3709{
3710 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003711 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003712 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003713 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003714 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003715 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003716 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003717 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003718}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003719EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003720
Takashi Iwaid5191e52009-11-16 14:58:17 +01003721/**
3722 * snd_hda_check_amp_list_power - Check the amp list and update the power
3723 * @codec: HD-audio codec
3724 * @check: the object containing an AMP list and the status
3725 * @nid: NID to check / update
3726 *
3727 * Check whether the given NID is in the amp list. If it's in the list,
3728 * check the current AMP status, and update the the power-status according
3729 * to the mute status.
3730 *
3731 * This function is supposed to be set or called from the check_power_status
3732 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003733 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003734int snd_hda_check_amp_list_power(struct hda_codec *codec,
3735 struct hda_loopback_check *check,
3736 hda_nid_t nid)
3737{
3738 struct hda_amp_list *p;
3739 int ch, v;
3740
3741 if (!check->amplist)
3742 return 0;
3743 for (p = check->amplist; p->nid; p++) {
3744 if (p->nid == nid)
3745 break;
3746 }
3747 if (!p->nid)
3748 return 0; /* nothing changed */
3749
3750 for (p = check->amplist; p->nid; p++) {
3751 for (ch = 0; ch < 2; ch++) {
3752 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3753 p->idx);
3754 if (!(v & HDA_AMP_MUTE) && v > 0) {
3755 if (!check->power_on) {
3756 check->power_on = 1;
3757 snd_hda_power_up(codec);
3758 }
3759 return 1;
3760 }
3761 }
3762 }
3763 if (check->power_on) {
3764 check->power_on = 0;
3765 snd_hda_power_down(codec);
3766 }
3767 return 0;
3768}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003769EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003770#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003772/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003773 * Channel mode helper
3774 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003775
3776/**
3777 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3778 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003779int snd_hda_ch_mode_info(struct hda_codec *codec,
3780 struct snd_ctl_elem_info *uinfo,
3781 const struct hda_channel_mode *chmode,
3782 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003783{
3784 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3785 uinfo->count = 1;
3786 uinfo->value.enumerated.items = num_chmodes;
3787 if (uinfo->value.enumerated.item >= num_chmodes)
3788 uinfo->value.enumerated.item = num_chmodes - 1;
3789 sprintf(uinfo->value.enumerated.name, "%dch",
3790 chmode[uinfo->value.enumerated.item].channels);
3791 return 0;
3792}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003793EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003794
Takashi Iwaid5191e52009-11-16 14:58:17 +01003795/**
3796 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3797 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003798int snd_hda_ch_mode_get(struct hda_codec *codec,
3799 struct snd_ctl_elem_value *ucontrol,
3800 const struct hda_channel_mode *chmode,
3801 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003802 int max_channels)
3803{
3804 int i;
3805
3806 for (i = 0; i < num_chmodes; i++) {
3807 if (max_channels == chmode[i].channels) {
3808 ucontrol->value.enumerated.item[0] = i;
3809 break;
3810 }
3811 }
3812 return 0;
3813}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003814EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003815
Takashi Iwaid5191e52009-11-16 14:58:17 +01003816/**
3817 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3818 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003819int snd_hda_ch_mode_put(struct hda_codec *codec,
3820 struct snd_ctl_elem_value *ucontrol,
3821 const struct hda_channel_mode *chmode,
3822 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003823 int *max_channelsp)
3824{
3825 unsigned int mode;
3826
3827 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01003828 if (mode >= num_chmodes)
3829 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003830 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003831 return 0;
3832 /* change the current channel setting */
3833 *max_channelsp = chmode[mode].channels;
3834 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003835 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003836 return 1;
3837}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003838EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840/*
3841 * input MUX helper
3842 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003843
3844/**
3845 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3846 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003847int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3848 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849{
3850 unsigned int index;
3851
3852 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3853 uinfo->count = 1;
3854 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003855 if (!imux->num_items)
3856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 index = uinfo->value.enumerated.item;
3858 if (index >= imux->num_items)
3859 index = imux->num_items - 1;
3860 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3861 return 0;
3862}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003863EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Takashi Iwaid5191e52009-11-16 14:58:17 +01003865/**
3866 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3867 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003868int snd_hda_input_mux_put(struct hda_codec *codec,
3869 const struct hda_input_mux *imux,
3870 struct snd_ctl_elem_value *ucontrol,
3871 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 unsigned int *cur_val)
3873{
3874 unsigned int idx;
3875
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003876 if (!imux->num_items)
3877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 idx = ucontrol->value.enumerated.item[0];
3879 if (idx >= imux->num_items)
3880 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003881 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003883 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3884 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 *cur_val = idx;
3886 return 1;
3887}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003888EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890
3891/*
3892 * Multi-channel / digital-out PCM helper functions
3893 */
3894
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003895/* setup SPDIF output stream */
3896static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3897 unsigned int stream_tag, unsigned int format)
3898{
3899 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02003900 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003901 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02003902 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3903 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003904 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003905 if (codec->slave_dig_outs) {
3906 hda_nid_t *d;
3907 for (d = codec->slave_dig_outs; *d; d++)
3908 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3909 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003910 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003911 /* turn on again (if needed) */
3912 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3913 set_dig_out_convert(codec, nid,
3914 codec->spdif_ctls & 0xff, -1);
3915}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003916
Takashi Iwai2f728532008-09-25 16:32:41 +02003917static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3918{
3919 snd_hda_codec_cleanup_stream(codec, nid);
3920 if (codec->slave_dig_outs) {
3921 hda_nid_t *d;
3922 for (d = codec->slave_dig_outs; *d; d++)
3923 snd_hda_codec_cleanup_stream(codec, *d);
3924 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003925}
3926
Takashi Iwaid5191e52009-11-16 14:58:17 +01003927/**
3928 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
3929 * @bus: HD-audio bus
3930 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003931void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3932{
3933 struct hda_codec *codec;
3934
3935 if (!bus)
3936 return;
3937 list_for_each_entry(codec, &bus->codec_list, list) {
3938#ifdef CONFIG_SND_HDA_POWER_SAVE
3939 if (!codec->power_on)
3940 continue;
3941#endif
3942 if (codec->patch_ops.reboot_notify)
3943 codec->patch_ops.reboot_notify(codec);
3944 }
3945}
Takashi Iwai8f217a22009-11-10 18:26:12 +01003946EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003947
Takashi Iwaid5191e52009-11-16 14:58:17 +01003948/**
3949 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003951int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3952 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953{
Ingo Molnar62932df2006-01-16 16:34:20 +01003954 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003955 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3956 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003957 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003959 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 return 0;
3961}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003962EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Takashi Iwaid5191e52009-11-16 14:58:17 +01003964/**
3965 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3966 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003967int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3968 struct hda_multi_out *mout,
3969 unsigned int stream_tag,
3970 unsigned int format,
3971 struct snd_pcm_substream *substream)
3972{
3973 mutex_lock(&codec->spdif_mutex);
3974 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3975 mutex_unlock(&codec->spdif_mutex);
3976 return 0;
3977}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003978EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003979
Takashi Iwaid5191e52009-11-16 14:58:17 +01003980/**
3981 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3982 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003983int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3984 struct hda_multi_out *mout)
3985{
3986 mutex_lock(&codec->spdif_mutex);
3987 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3988 mutex_unlock(&codec->spdif_mutex);
3989 return 0;
3990}
3991EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3992
Takashi Iwaid5191e52009-11-16 14:58:17 +01003993/**
3994 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003996int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3997 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
Ingo Molnar62932df2006-01-16 16:34:20 +01003999 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004001 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 return 0;
4003}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004004EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Takashi Iwaid5191e52009-11-16 14:58:17 +01004006/**
4007 * snd_hda_multi_out_analog_open - open analog outputs
4008 *
4009 * Open analog outputs and set up the hw-constraints.
4010 * If the digital outputs can be opened as slave, open the digital
4011 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004013int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4014 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004015 struct snd_pcm_substream *substream,
4016 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Takashi Iwai9a081602008-02-12 18:37:26 +01004018 struct snd_pcm_runtime *runtime = substream->runtime;
4019 runtime->hw.channels_max = mout->max_channels;
4020 if (mout->dig_out_nid) {
4021 if (!mout->analog_rates) {
4022 mout->analog_rates = hinfo->rates;
4023 mout->analog_formats = hinfo->formats;
4024 mout->analog_maxbps = hinfo->maxbps;
4025 } else {
4026 runtime->hw.rates = mout->analog_rates;
4027 runtime->hw.formats = mout->analog_formats;
4028 hinfo->maxbps = mout->analog_maxbps;
4029 }
4030 if (!mout->spdif_rates) {
4031 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4032 &mout->spdif_rates,
4033 &mout->spdif_formats,
4034 &mout->spdif_maxbps);
4035 }
4036 mutex_lock(&codec->spdif_mutex);
4037 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004038 if ((runtime->hw.rates & mout->spdif_rates) &&
4039 (runtime->hw.formats & mout->spdif_formats)) {
4040 runtime->hw.rates &= mout->spdif_rates;
4041 runtime->hw.formats &= mout->spdif_formats;
4042 if (mout->spdif_maxbps < hinfo->maxbps)
4043 hinfo->maxbps = mout->spdif_maxbps;
4044 } else {
4045 mout->share_spdif = 0;
4046 /* FIXME: need notify? */
4047 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004048 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004049 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4052 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4053}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004054EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Takashi Iwaid5191e52009-11-16 14:58:17 +01004056/**
4057 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4058 *
4059 * Set up the i/o for analog out.
4060 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004062int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4063 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 unsigned int stream_tag,
4065 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004066 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
4068 hda_nid_t *nids = mout->dac_nids;
4069 int chs = substream->runtime->channels;
4070 int i;
4071
Ingo Molnar62932df2006-01-16 16:34:20 +01004072 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004073 if (mout->dig_out_nid && mout->share_spdif &&
4074 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004076 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4077 format) &&
4078 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004080 setup_dig_out_stream(codec, mout->dig_out_nid,
4081 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 } else {
4083 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004084 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 }
4086 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004087 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004090 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4091 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004092 if (!mout->no_share_stream &&
4093 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004095 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4096 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004097 /* extra outputs copied from front */
4098 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004099 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004100 snd_hda_codec_setup_stream(codec,
4101 mout->extra_out_nid[i],
4102 stream_tag, 0, format);
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 /* surrounds */
4105 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004106 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004107 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4108 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004109 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004110 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4111 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 }
4113 return 0;
4114}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004115EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Takashi Iwaid5191e52009-11-16 14:58:17 +01004117/**
4118 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004120int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4121 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
4123 hda_nid_t *nids = mout->dac_nids;
4124 int i;
4125
4126 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004127 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004129 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004130 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4131 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004132 snd_hda_codec_cleanup_stream(codec,
4133 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004134 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004136 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 mout->dig_out_used = 0;
4138 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004139 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return 0;
4141}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004142EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004144/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004145 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004146 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004147
Takashi Iwai12f288b2007-08-02 15:51:59 +02004148static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004149{
4150 for (; *list; list++)
4151 if (*list == nid)
4152 return 1;
4153 return 0;
4154}
4155
Steve Longerbeam81937d32007-05-08 15:33:03 +02004156
4157/*
4158 * Sort an associated group of pins according to their sequence numbers.
4159 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004160static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004161 int num_pins)
4162{
4163 int i, j;
4164 short seq;
4165 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004166
Steve Longerbeam81937d32007-05-08 15:33:03 +02004167 for (i = 0; i < num_pins; i++) {
4168 for (j = i + 1; j < num_pins; j++) {
4169 if (sequences[i] > sequences[j]) {
4170 seq = sequences[i];
4171 sequences[i] = sequences[j];
4172 sequences[j] = seq;
4173 nid = pins[i];
4174 pins[i] = pins[j];
4175 pins[j] = nid;
4176 }
4177 }
4178 }
4179}
4180
4181
Takashi Iwai82bc9552006-03-21 11:24:42 +01004182/*
4183 * Parse all pin widgets and store the useful pin nids to cfg
4184 *
4185 * The number of line-outs or any primary output is stored in line_outs,
4186 * and the corresponding output pins are assigned to line_out_pins[],
4187 * in the order of front, rear, CLFE, side, ...
4188 *
4189 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004190 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004191 * is detected, one of speaker of HP pins is assigned as the primary
4192 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4193 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004194 *
Takashi Iwai82bc9552006-03-21 11:24:42 +01004195 * The analog input pins are assigned to input_pins array.
4196 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4197 * respectively.
4198 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004199int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4200 struct auto_pin_cfg *cfg,
4201 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004202{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004203 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004204 short seq, assoc_line_out, assoc_speaker;
4205 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4206 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004207 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004208
4209 memset(cfg, 0, sizeof(*cfg));
4210
Steve Longerbeam81937d32007-05-08 15:33:03 +02004211 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4212 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004213 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004214 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004215
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004216 end_nid = codec->start_nid + codec->num_nodes;
4217 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004218 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004219 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004220 unsigned int def_conf;
4221 short assoc, loc;
4222
4223 /* read all default configuration for pin complex */
4224 if (wid_type != AC_WID_PIN)
4225 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004226 /* ignore the given nids (e.g. pc-beep returns error) */
4227 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4228 continue;
4229
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004230 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004231 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4232 continue;
4233 loc = get_defcfg_location(def_conf);
4234 switch (get_defcfg_device(def_conf)) {
4235 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004236 seq = get_defcfg_sequence(def_conf);
4237 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004238
4239 if (!(wid_caps & AC_WCAP_STEREO))
4240 if (!cfg->mono_out_pin)
4241 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004242 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004243 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004244 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004245 assoc_line_out = assoc;
4246 else if (assoc_line_out != assoc)
4247 continue;
4248 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4249 continue;
4250 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004251 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004252 cfg->line_outs++;
4253 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004254 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004255 seq = get_defcfg_sequence(def_conf);
4256 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004257 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004258 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004259 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004260 assoc_speaker = assoc;
4261 else if (assoc_speaker != assoc)
4262 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004263 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4264 continue;
4265 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004266 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004267 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004268 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004269 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004270 seq = get_defcfg_sequence(def_conf);
4271 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004272 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4273 continue;
4274 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004275 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004276 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004277 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004278 case AC_JACK_MIC_IN: {
4279 int preferred, alt;
Kailang Yang6ff86a32010-03-19 11:14:36 +01004280 if (loc == AC_JACK_LOC_FRONT ||
4281 (loc & 0x30) == AC_JACK_LOC_INTERNAL) {
Takashi Iwai314634b2006-09-21 11:56:18 +02004282 preferred = AUTO_PIN_FRONT_MIC;
4283 alt = AUTO_PIN_MIC;
4284 } else {
4285 preferred = AUTO_PIN_MIC;
4286 alt = AUTO_PIN_FRONT_MIC;
4287 }
4288 if (!cfg->input_pins[preferred])
4289 cfg->input_pins[preferred] = nid;
4290 else if (!cfg->input_pins[alt])
4291 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004292 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004293 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004294 case AC_JACK_LINE_IN:
4295 if (loc == AC_JACK_LOC_FRONT)
4296 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4297 else
4298 cfg->input_pins[AUTO_PIN_LINE] = nid;
4299 break;
4300 case AC_JACK_CD:
4301 cfg->input_pins[AUTO_PIN_CD] = nid;
4302 break;
4303 case AC_JACK_AUX:
4304 cfg->input_pins[AUTO_PIN_AUX] = nid;
4305 break;
4306 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004307 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004308 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4309 continue;
4310 cfg->dig_out_pins[cfg->dig_outs] = nid;
4311 cfg->dig_out_type[cfg->dig_outs] =
4312 (loc == AC_JACK_LOC_HDMI) ?
4313 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4314 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004315 break;
4316 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004317 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004318 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004319 if (loc == AC_JACK_LOC_HDMI)
4320 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4321 else
4322 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004323 break;
4324 }
4325 }
4326
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004327 /* FIX-UP:
4328 * If no line-out is defined but multiple HPs are found,
4329 * some of them might be the real line-outs.
4330 */
4331 if (!cfg->line_outs && cfg->hp_outs > 1) {
4332 int i = 0;
4333 while (i < cfg->hp_outs) {
4334 /* The real HPs should have the sequence 0x0f */
4335 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4336 i++;
4337 continue;
4338 }
4339 /* Move it to the line-out table */
4340 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4341 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4342 cfg->line_outs++;
4343 cfg->hp_outs--;
4344 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4345 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4346 memmove(sequences_hp + i - 1, sequences_hp + i,
4347 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4348 }
4349 }
4350
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004351 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004352 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4353 cfg->line_outs);
4354 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4355 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004356 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4357 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004358
Takashi Iwaif889fa92007-10-31 15:49:32 +01004359 /* if we have only one mic, make it AUTO_PIN_MIC */
4360 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4361 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4362 cfg->input_pins[AUTO_PIN_MIC] =
4363 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4364 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4365 }
4366 /* ditto for line-in */
4367 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4368 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4369 cfg->input_pins[AUTO_PIN_LINE] =
4370 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4371 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4372 }
4373
Steve Longerbeam81937d32007-05-08 15:33:03 +02004374 /*
4375 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4376 * as a primary output
4377 */
4378 if (!cfg->line_outs) {
4379 if (cfg->speaker_outs) {
4380 cfg->line_outs = cfg->speaker_outs;
4381 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4382 sizeof(cfg->speaker_pins));
4383 cfg->speaker_outs = 0;
4384 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4385 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4386 } else if (cfg->hp_outs) {
4387 cfg->line_outs = cfg->hp_outs;
4388 memcpy(cfg->line_out_pins, cfg->hp_pins,
4389 sizeof(cfg->hp_pins));
4390 cfg->hp_outs = 0;
4391 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4392 cfg->line_out_type = AUTO_PIN_HP_OUT;
4393 }
4394 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004395
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004396 /* Reorder the surround channels
4397 * ALSA sequence is front/surr/clfe/side
4398 * HDA sequence is:
4399 * 4-ch: front/surr => OK as it is
4400 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004401 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004402 */
4403 switch (cfg->line_outs) {
4404 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004405 case 4:
4406 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004407 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004408 cfg->line_out_pins[2] = nid;
4409 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004410 }
4411
Takashi Iwai82bc9552006-03-21 11:24:42 +01004412 /*
4413 * debug prints of the parsed results
4414 */
4415 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4416 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4417 cfg->line_out_pins[2], cfg->line_out_pins[3],
4418 cfg->line_out_pins[4]);
4419 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4420 cfg->speaker_outs, cfg->speaker_pins[0],
4421 cfg->speaker_pins[1], cfg->speaker_pins[2],
4422 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004423 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4424 cfg->hp_outs, cfg->hp_pins[0],
4425 cfg->hp_pins[1], cfg->hp_pins[2],
4426 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004427 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004428 if (cfg->dig_outs)
4429 snd_printd(" dig-out=0x%x/0x%x\n",
4430 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004431 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4432 " cd=0x%x, aux=0x%x\n",
4433 cfg->input_pins[AUTO_PIN_MIC],
4434 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4435 cfg->input_pins[AUTO_PIN_LINE],
4436 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4437 cfg->input_pins[AUTO_PIN_CD],
4438 cfg->input_pins[AUTO_PIN_AUX]);
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004439 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004440 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004441
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004442 return 0;
4443}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004444EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004445
Takashi Iwai4a471b72005-12-07 13:56:29 +01004446/* labels for input pins */
4447const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4448 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4449};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004450EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01004451
4452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453#ifdef CONFIG_PM
4454/*
4455 * power management
4456 */
4457
4458/**
4459 * snd_hda_suspend - suspend the codecs
4460 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 *
4462 * Returns 0 if successful.
4463 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004464int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004466 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Takashi Iwai0ba21762007-04-16 11:29:14 +02004468 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004469#ifdef CONFIG_SND_HDA_POWER_SAVE
4470 if (!codec->power_on)
4471 continue;
4472#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004473 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 }
4475 return 0;
4476}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004477EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479/**
4480 * snd_hda_resume - resume the codecs
4481 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 *
4483 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004484 *
4485 * This fucntion is defined only when POWER_SAVE isn't set.
4486 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 */
4488int snd_hda_resume(struct hda_bus *bus)
4489{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004490 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Takashi Iwai0ba21762007-04-16 11:29:14 +02004492 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004493 if (snd_hda_codec_needs_resume(codec))
4494 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 return 0;
4497}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004498EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004499#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004500
4501/*
4502 * generic arrays
4503 */
4504
Takashi Iwaid5191e52009-11-16 14:58:17 +01004505/**
4506 * snd_array_new - get a new element from the given array
4507 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004508 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004509 * Get a new element from the given array. If it exceeds the
4510 * pre-allocated array size, re-allocate the array.
4511 *
4512 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004513 */
4514void *snd_array_new(struct snd_array *array)
4515{
4516 if (array->used >= array->alloced) {
4517 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004518 void *nlist;
4519 if (snd_BUG_ON(num >= 4096))
4520 return NULL;
4521 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004522 if (!nlist)
4523 return NULL;
4524 if (array->list) {
4525 memcpy(nlist, array->list,
4526 array->elem_size * array->alloced);
4527 kfree(array->list);
4528 }
4529 array->list = nlist;
4530 array->alloced = num;
4531 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004532 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004533}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004534EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004535
Takashi Iwaid5191e52009-11-16 14:58:17 +01004536/**
4537 * snd_array_free - free the given array elements
4538 * @array: the array object
4539 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004540void snd_array_free(struct snd_array *array)
4541{
4542 kfree(array->list);
4543 array->used = 0;
4544 array->alloced = 0;
4545 array->list = NULL;
4546}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004547EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004548
Takashi Iwaid5191e52009-11-16 14:58:17 +01004549/**
4550 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4551 * @pcm: PCM caps bits
4552 * @buf: the string buffer to write
4553 * @buflen: the max buffer length
4554 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004555 * used by hda_proc.c and hda_eld.c
4556 */
4557void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4558{
4559 static unsigned int rates[] = {
4560 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4561 96000, 176400, 192000, 384000
4562 };
4563 int i, j;
4564
4565 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4566 if (pcm & (1 << i))
4567 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4568
4569 buf[j] = '\0'; /* necessary when j == 0 */
4570}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004571EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004572
Takashi Iwaid5191e52009-11-16 14:58:17 +01004573/**
4574 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4575 * @pcm: PCM caps bits
4576 * @buf: the string buffer to write
4577 * @buflen: the max buffer length
4578 *
4579 * used by hda_proc.c and hda_eld.c
4580 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004581void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4582{
4583 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4584 int i, j;
4585
4586 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4587 if (pcm & (AC_SUPPCM_BITS_8 << i))
4588 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4589
4590 buf[j] = '\0'; /* necessary when j == 0 */
4591}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004592EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004593
4594MODULE_DESCRIPTION("HDA codec core");
4595MODULE_LICENSE("GPL");