blob: e5c3484388f0ba31eed3460c02c727eea37f9339 [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);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200787 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
788 AC_VERB_GET_PIN_WIDGET_CONTROL,
789 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100790 }
791 return 0;
792}
793
794/* look up the given pin config list and return the item matching with NID */
795static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
796 struct snd_array *array,
797 hda_nid_t nid)
798{
799 int i;
800 for (i = 0; i < array->used; i++) {
801 struct hda_pincfg *pin = snd_array_elem(array, i);
802 if (pin->nid == nid)
803 return pin;
804 }
805 return NULL;
806}
807
808/* write a config value for the given NID */
809static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
810 unsigned int cfg)
811{
812 int i;
813 for (i = 0; i < 4; i++) {
814 snd_hda_codec_write(codec, nid, 0,
815 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
816 cfg & 0xff);
817 cfg >>= 8;
818 }
819}
820
821/* set the current pin config value for the given NID.
822 * the value is cached, and read via snd_hda_codec_get_pincfg()
823 */
824int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
825 hda_nid_t nid, unsigned int cfg)
826{
827 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100828 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100829
Takashi Iwaib82855a2009-12-27 11:24:56 +0100830 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
831 return -EINVAL;
832
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100833 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100834 pin = look_up_pincfg(codec, list, nid);
835 if (!pin) {
836 pin = snd_array_new(list);
837 if (!pin)
838 return -ENOMEM;
839 pin->nid = nid;
840 }
841 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100842
843 /* change only when needed; e.g. if the pincfg is already present
844 * in user_pins[], don't write it
845 */
846 cfg = snd_hda_codec_get_pincfg(codec, nid);
847 if (oldcfg != cfg)
848 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100849 return 0;
850}
851
Takashi Iwaid5191e52009-11-16 14:58:17 +0100852/**
853 * snd_hda_codec_set_pincfg - Override a pin default configuration
854 * @codec: the HDA codec
855 * @nid: NID to set the pin config
856 * @cfg: the pin default config value
857 *
858 * Override a pin default configuration value in the cache.
859 * This value can be read by snd_hda_codec_get_pincfg() in a higher
860 * priority than the real hardware value.
861 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100862int snd_hda_codec_set_pincfg(struct hda_codec *codec,
863 hda_nid_t nid, unsigned int cfg)
864{
Takashi Iwai346ff702009-02-23 09:42:57 +0100865 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100866}
867EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
868
Takashi Iwaid5191e52009-11-16 14:58:17 +0100869/**
870 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
871 * @codec: the HDA codec
872 * @nid: NID to get the pin config
873 *
874 * Get the current pin config value of the given pin NID.
875 * If the pincfg value is cached or overridden via sysfs or driver,
876 * returns the cached value.
877 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100878unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
879{
880 struct hda_pincfg *pin;
881
Takashi Iwai3be14142009-02-20 14:11:16 +0100882#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100883 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100884 if (pin)
885 return pin->cfg;
886#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100887 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
888 if (pin)
889 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100890 pin = look_up_pincfg(codec, &codec->init_pins, nid);
891 if (pin)
892 return pin->cfg;
893 return 0;
894}
895EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
896
897/* restore all current pin configs */
898static void restore_pincfgs(struct hda_codec *codec)
899{
900 int i;
901 for (i = 0; i < codec->init_pins.used; i++) {
902 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
903 set_pincfg(codec, pin->nid,
904 snd_hda_codec_get_pincfg(codec, pin->nid));
905 }
906}
Takashi Iwai54d17402005-11-21 16:33:22 +0100907
Takashi Iwai92ee6162009-12-27 11:18:59 +0100908/**
909 * snd_hda_shutup_pins - Shut up all pins
910 * @codec: the HDA codec
911 *
912 * Clear all pin controls to shup up before suspend for avoiding click noise.
913 * The controls aren't cached so that they can be resumed properly.
914 */
915void snd_hda_shutup_pins(struct hda_codec *codec)
916{
917 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200918 /* don't shut up pins when unloading the driver; otherwise it breaks
919 * the default pin setup at the next load of the driver
920 */
921 if (codec->bus->shutdown)
922 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100923 for (i = 0; i < codec->init_pins.used; i++) {
924 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
925 /* use read here for syncing after issuing each verb */
926 snd_hda_codec_read(codec, pin->nid, 0,
927 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
928 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200929 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100930}
931EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
932
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200933/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
934static void restore_shutup_pins(struct hda_codec *codec)
935{
936 int i;
937 if (!codec->pins_shutup)
938 return;
939 if (codec->bus->shutdown)
940 return;
941 for (i = 0; i < codec->init_pins.used; i++) {
942 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
943 snd_hda_codec_write(codec, pin->nid, 0,
944 AC_VERB_SET_PIN_WIDGET_CONTROL,
945 pin->ctrl);
946 }
947 codec->pins_shutup = 0;
948}
949
Takashi Iwai01751f52007-08-10 16:59:39 +0200950static void init_hda_cache(struct hda_cache_rec *cache,
951 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200952static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200953
Takashi Iwai3be14142009-02-20 14:11:16 +0100954/* restore the initial pin cfgs and release all pincfg lists */
955static void restore_init_pincfgs(struct hda_codec *codec)
956{
Takashi Iwai346ff702009-02-23 09:42:57 +0100957 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100958 * so that only the values in init_pins are restored
959 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100960 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100961#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100962 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100963#endif
964 restore_pincfgs(codec);
965 snd_array_free(&codec->init_pins);
966}
967
Takashi Iwai54d17402005-11-21 16:33:22 +0100968/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * codec destructor
970 */
971static void snd_hda_codec_free(struct hda_codec *codec)
972{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200973 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return;
Takashi Iwai3be14142009-02-20 14:11:16 +0100975 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200976#ifdef CONFIG_SND_HDA_POWER_SAVE
977 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100978 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200979#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200981 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100982 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 codec->bus->caddr_tbl[codec->addr] = NULL;
984 if (codec->patch_ops.free)
985 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100986 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200987 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200988 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200989 kfree(codec->vendor_name);
990 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200991 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100992 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 kfree(codec);
994}
995
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100996static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
997 unsigned int power_state);
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/**
1000 * snd_hda_codec_new - create a HDA codec
1001 * @bus: the bus to assign
1002 * @codec_addr: the codec address
1003 * @codecp: the pointer to store the generated codec
1004 *
1005 * Returns 0 if successful, or a negative error code.
1006 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001007int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1008 unsigned int codec_addr,
1009 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001012 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int err;
1014
Takashi Iwaida3cec32008-08-08 17:12:14 +02001015 if (snd_BUG_ON(!bus))
1016 return -EINVAL;
1017 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1018 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001021 snd_printk(KERN_ERR "hda_codec: "
1022 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return -EBUSY;
1024 }
1025
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001026 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (codec == NULL) {
1028 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1029 return -ENOMEM;
1030 }
1031
1032 codec->bus = bus;
1033 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001034 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001035 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001036 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001037 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001038 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1039 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001040 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001041 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001042 if (codec->bus->modelname) {
1043 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1044 if (!codec->modelname) {
1045 snd_hda_codec_free(codec);
1046 return -ENODEV;
1047 }
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Takashi Iwaicb53c622007-08-10 17:21:45 +02001050#ifdef CONFIG_SND_HDA_POWER_SAVE
1051 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1052 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1053 * the caller has to power down appropriatley after initialization
1054 * phase.
1055 */
1056 hda_keep_power_on(codec);
1057#endif
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 list_add_tail(&codec->list, &bus->codec_list);
1060 bus->caddr_tbl[codec_addr] = codec;
1061
Takashi Iwai0ba21762007-04-16 11:29:14 +02001062 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1063 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001064 if (codec->vendor_id == -1)
1065 /* read again, hopefully the access method was corrected
1066 * in the last read...
1067 */
1068 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1069 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001070 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1071 AC_PAR_SUBSYSTEM_ID);
1072 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1073 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001075 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001076 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001077 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001078 err = -ENODEV;
1079 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
Takashi Iwai3be14142009-02-20 14:11:16 +01001082 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1083 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001084 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001085 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001086 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001087 err = read_pin_defaults(codec);
1088 if (err < 0)
1089 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001090
Takashi Iwai0ba21762007-04-16 11:29:14 +02001091 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001092 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001093 codec->subsystem_id =
1094 snd_hda_codec_read(codec, nid, 0,
1095 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001096 }
1097
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001098 /* power-up all before initialization */
1099 hda_set_power_state(codec,
1100 codec->afg ? codec->afg : codec->mfg,
1101 AC_PWRST_D0);
1102
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001103 snd_hda_codec_proc_new(codec);
1104
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001105 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001106
1107 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1108 codec->subsystem_id, codec->revision_id);
1109 snd_component_add(codec->bus->card, component);
1110
1111 if (codecp)
1112 *codecp = codec;
1113 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001114
1115 error:
1116 snd_hda_codec_free(codec);
1117 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001119EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001120
Takashi Iwaid5191e52009-11-16 14:58:17 +01001121/**
1122 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1123 * @codec: the HDA codec
1124 *
1125 * Start parsing of the given codec tree and (re-)initialize the whole
1126 * patch instance.
1127 *
1128 * Returns 0 if successful or a negative error code.
1129 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001130int snd_hda_codec_configure(struct hda_codec *codec)
1131{
1132 int err;
1133
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001134 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001135 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001136 err = get_codec_name(codec);
1137 if (err < 0)
1138 return err;
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Takashi Iwai82467612007-07-27 19:15:54 +02001141 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001143 goto patched;
1144 }
Takashi Iwai82467612007-07-27 19:15:54 +02001145 if (codec->preset && codec->preset->patch) {
1146 err = codec->preset->patch(codec);
1147 goto patched;
1148 }
1149
1150 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001151 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001152 if (err < 0)
1153 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001154
1155 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001156 if (!err && codec->patch_ops.unsol_event)
1157 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001158 /* audio codec should override the mixer name */
1159 if (!err && (codec->afg || !*codec->bus->card->mixername))
1160 snprintf(codec->bus->card->mixername,
1161 sizeof(codec->bus->card->mixername),
1162 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001165EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167/**
1168 * snd_hda_codec_setup_stream - set up the codec for streaming
1169 * @codec: the CODEC to set up
1170 * @nid: the NID to set up
1171 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1172 * @channel_id: channel id to pass, zero based.
1173 * @format: stream format.
1174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001175void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1176 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 int channel_id, int format)
1178{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001179 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001180 return;
1181
Takashi Iwai0ba21762007-04-16 11:29:14 +02001182 snd_printdd("hda_codec_setup_stream: "
1183 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 nid, stream_tag, channel_id, format);
1185 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1186 (stream_tag << 4) | channel_id);
1187 msleep(1);
1188 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1189}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001190EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Takashi Iwaid5191e52009-11-16 14:58:17 +01001192/**
1193 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1194 * @codec: the CODEC to clean up
1195 * @nid: the NID to clean up
1196 */
Takashi Iwai888afa12008-03-18 09:57:50 +01001197void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1198{
1199 if (!nid)
1200 return;
1201
1202 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1203 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1204#if 0 /* keep the format */
1205 msleep(1);
1206 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1207#endif
1208}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001209EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211/*
1212 * amp access functions
1213 */
1214
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001215/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001216#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001217#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001218#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1219#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001221#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001224static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001225 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Takashi Iwai01751f52007-08-10 16:59:39 +02001227 memset(cache, 0, sizeof(*cache));
1228 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001229 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001230}
1231
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001232static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001233{
Takashi Iwai603c4012008-07-30 15:01:44 +02001234 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001238static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Takashi Iwai01751f52007-08-10 16:59:39 +02001240 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1241 u16 cur = cache->hash[idx];
1242 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001245 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (info->key == key)
1247 return info;
1248 cur = info->next;
1249 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001250 return NULL;
1251}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001253/* query the hash. allocate an entry if not found. */
1254static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1255 u32 key)
1256{
1257 struct hda_cache_head *info = get_hash(cache, key);
1258 if (!info) {
1259 u16 idx, cur;
1260 /* add a new hash entry */
1261 info = snd_array_new(&cache->buf);
1262 if (!info)
1263 return NULL;
1264 cur = snd_array_index(&cache->buf, info);
1265 info->key = key;
1266 info->val = 0;
1267 idx = key % (u16)ARRAY_SIZE(cache->hash);
1268 info->next = cache->hash[idx];
1269 cache->hash[idx] = cur;
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return info;
1272}
1273
Takashi Iwai01751f52007-08-10 16:59:39 +02001274/* query and allocate an amp hash entry */
1275static inline struct hda_amp_info *
1276get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1277{
1278 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1279}
1280
Takashi Iwaid5191e52009-11-16 14:58:17 +01001281/**
1282 * query_amp_caps - query AMP capabilities
1283 * @codec: the HD-auio codec
1284 * @nid: the NID to query
1285 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1286 *
1287 * Query AMP capabilities for the given widget and direction.
1288 * Returns the obtained capability bits.
1289 *
1290 * When cap bits have been already read, this doesn't read again but
1291 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001293u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001295 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Takashi Iwai0ba21762007-04-16 11:29:14 +02001297 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1298 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001300 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001301 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001303 info->amp_caps = snd_hda_param_read(codec, nid,
1304 direction == HDA_OUTPUT ?
1305 AC_PAR_AMP_OUT_CAP :
1306 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001307 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001308 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310 return info->amp_caps;
1311}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001312EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Takashi Iwaid5191e52009-11-16 14:58:17 +01001314/**
1315 * snd_hda_override_amp_caps - Override the AMP capabilities
1316 * @codec: the CODEC to clean up
1317 * @nid: the NID to clean up
1318 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1319 * @caps: the capability bits to set
1320 *
1321 * Override the cached AMP caps bits value by the given one.
1322 * This function is useful if the driver needs to adjust the AMP ranges,
1323 * e.g. limit to 0dB, etc.
1324 *
1325 * Returns zero if successful or a negative error code.
1326 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001327int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1328 unsigned int caps)
1329{
1330 struct hda_amp_info *info;
1331
1332 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1333 if (!info)
1334 return -EINVAL;
1335 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001336 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001337 return 0;
1338}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001339EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001340
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001341static unsigned int
1342query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1343 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001344{
1345 struct hda_amp_info *info;
1346
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001347 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001348 if (!info)
1349 return 0;
1350 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001351 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001352 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001353 }
1354 return info->amp_caps;
1355}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001356
1357static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1358{
1359 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1360}
1361
Takashi Iwaid5191e52009-11-16 14:58:17 +01001362/**
1363 * snd_hda_query_pin_caps - Query PIN capabilities
1364 * @codec: the HD-auio codec
1365 * @nid: the NID to query
1366 *
1367 * Query PIN capabilities for the given widget.
1368 * Returns the obtained capability bits.
1369 *
1370 * When cap bits have been already read, this doesn't read again but
1371 * returns the cached value.
1372 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001373u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1374{
1375 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1376 read_pin_cap);
1377}
Takashi Iwai1327a322009-03-23 13:07:47 +01001378EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1379
Wu Fengguang864f92b2009-11-18 12:38:02 +08001380/**
1381 * snd_hda_pin_sense - execute pin sense measurement
1382 * @codec: the CODEC to sense
1383 * @nid: the pin NID to sense
1384 *
1385 * Execute necessary pin sense measurement and return its Presence Detect,
1386 * Impedance, ELD Valid etc. status bits.
1387 */
1388u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1389{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001390 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001391
Takashi Iwai729d55b2009-12-25 22:49:01 +01001392 if (!codec->no_trigger_sense) {
1393 pincap = snd_hda_query_pin_caps(codec, nid);
1394 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001395 snd_hda_codec_read(codec, nid, 0,
1396 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001397 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001398 return snd_hda_codec_read(codec, nid, 0,
1399 AC_VERB_GET_PIN_SENSE, 0);
1400}
1401EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1402
1403/**
1404 * snd_hda_jack_detect - query pin Presence Detect status
1405 * @codec: the CODEC to sense
1406 * @nid: the pin NID to sense
1407 *
1408 * Query and return the pin's Presence Detect status.
1409 */
1410int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1411{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001412 u32 sense = snd_hda_pin_sense(codec, nid);
1413 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001414}
1415EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417/*
1418 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001419 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001421static unsigned int get_vol_mute(struct hda_codec *codec,
1422 struct hda_amp_info *info, hda_nid_t nid,
1423 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 u32 val, parm;
1426
Takashi Iwai01751f52007-08-10 16:59:39 +02001427 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001428 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1431 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1432 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001433 val = snd_hda_codec_read(codec, nid, 0,
1434 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001436 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001437 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
1440/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001441 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001443static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001444 hda_nid_t nid, int ch, int direction, int index,
1445 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 u32 parm;
1448
1449 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1450 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1451 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1452 parm |= val;
1453 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001454 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Takashi Iwaid5191e52009-11-16 14:58:17 +01001457/**
1458 * snd_hda_codec_amp_read - Read AMP value
1459 * @codec: HD-audio codec
1460 * @nid: NID to read the AMP value
1461 * @ch: channel (left=0 or right=1)
1462 * @direction: #HDA_INPUT or #HDA_OUTPUT
1463 * @index: the index value (only for input direction)
1464 *
1465 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
Takashi Iwai834be882006-03-01 14:16:17 +01001467int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1468 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001470 struct hda_amp_info *info;
1471 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1472 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001474 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001476EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Takashi Iwaid5191e52009-11-16 14:58:17 +01001478/**
1479 * snd_hda_codec_amp_update - update the AMP value
1480 * @codec: HD-audio codec
1481 * @nid: NID to read the AMP value
1482 * @ch: channel (left=0 or right=1)
1483 * @direction: #HDA_INPUT or #HDA_OUTPUT
1484 * @idx: the index value (only for input direction)
1485 * @mask: bit mask to set
1486 * @val: the bits value to set
1487 *
1488 * Update the AMP value with a bit mask.
1489 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001490 */
Takashi Iwai834be882006-03-01 14:16:17 +01001491int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1492 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001494 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001495
Takashi Iwai0ba21762007-04-16 11:29:14 +02001496 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1497 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001499 if (snd_BUG_ON(mask & ~0xff))
1500 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001501 val &= mask;
1502 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001503 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001505 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 1;
1507}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001508EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Takashi Iwaid5191e52009-11-16 14:58:17 +01001510/**
1511 * snd_hda_codec_amp_stereo - update the AMP stereo values
1512 * @codec: HD-audio codec
1513 * @nid: NID to read the AMP value
1514 * @direction: #HDA_INPUT or #HDA_OUTPUT
1515 * @idx: the index value (only for input direction)
1516 * @mask: bit mask to set
1517 * @val: the bits value to set
1518 *
1519 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1520 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001521 */
1522int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1523 int direction, int idx, int mask, int val)
1524{
1525 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001526
1527 if (snd_BUG_ON(mask & ~0xff))
1528 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001529 for (ch = 0; ch < 2; ch++)
1530 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1531 idx, mask, val);
1532 return ret;
1533}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001534EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001535
Takashi Iwaicb53c622007-08-10 17:21:45 +02001536#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001537/**
1538 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1539 * @codec: HD-audio codec
1540 *
1541 * Resume the all amp commands from the cache.
1542 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001543void snd_hda_codec_resume_amp(struct hda_codec *codec)
1544{
Takashi Iwai603c4012008-07-30 15:01:44 +02001545 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001546 int i;
1547
Takashi Iwai603c4012008-07-30 15:01:44 +02001548 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001549 u32 key = buffer->head.key;
1550 hda_nid_t nid;
1551 unsigned int idx, dir, ch;
1552 if (!key)
1553 continue;
1554 nid = key & 0xff;
1555 idx = (key >> 16) & 0xff;
1556 dir = (key >> 24) & 0xff;
1557 for (ch = 0; ch < 2; ch++) {
1558 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1559 continue;
1560 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1561 buffer->vol[ch]);
1562 }
1563 }
1564}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001565EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001566#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001568static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1569 unsigned int ofs)
1570{
1571 u32 caps = query_amp_caps(codec, nid, dir);
1572 /* get num steps */
1573 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1574 if (ofs < caps)
1575 caps -= ofs;
1576 return caps;
1577}
1578
Takashi Iwaid5191e52009-11-16 14:58:17 +01001579/**
1580 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1581 *
1582 * The control element is supposed to have the private_value field
1583 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1584 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001585int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1586 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1589 u16 nid = get_amp_nid(kcontrol);
1590 u8 chs = get_amp_channels(kcontrol);
1591 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001592 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001594 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1595 uinfo->count = chs == 3 ? 2 : 1;
1596 uinfo->value.integer.min = 0;
1597 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1598 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001599 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001600 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1601 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return -EINVAL;
1603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return 0;
1605}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001606EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001608
1609static inline unsigned int
1610read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1611 int ch, int dir, int idx, unsigned int ofs)
1612{
1613 unsigned int val;
1614 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1615 val &= HDA_AMP_VOLMASK;
1616 if (val >= ofs)
1617 val -= ofs;
1618 else
1619 val = 0;
1620 return val;
1621}
1622
1623static inline int
1624update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1625 int ch, int dir, int idx, unsigned int ofs,
1626 unsigned int val)
1627{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001628 unsigned int maxval;
1629
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001630 if (val > 0)
1631 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001632 /* ofs = 0: raw max value */
1633 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001634 if (val > maxval)
1635 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001636 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1637 HDA_AMP_VOLMASK, val);
1638}
1639
Takashi Iwaid5191e52009-11-16 14:58:17 +01001640/**
1641 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1642 *
1643 * The control element is supposed to have the private_value field
1644 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1645 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001646int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1650 hda_nid_t nid = get_amp_nid(kcontrol);
1651 int chs = get_amp_channels(kcontrol);
1652 int dir = get_amp_direction(kcontrol);
1653 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001654 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 long *valp = ucontrol->value.integer.value;
1656
1657 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001658 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001660 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return 0;
1662}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001663EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Takashi Iwaid5191e52009-11-16 14:58:17 +01001665/**
1666 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1667 *
1668 * The control element is supposed to have the private_value field
1669 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1670 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001671int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1672 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1675 hda_nid_t nid = get_amp_nid(kcontrol);
1676 int chs = get_amp_channels(kcontrol);
1677 int dir = get_amp_direction(kcontrol);
1678 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001679 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 long *valp = ucontrol->value.integer.value;
1681 int change = 0;
1682
Takashi Iwaicb53c622007-08-10 17:21:45 +02001683 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001684 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001685 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001686 valp++;
1687 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001688 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001689 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001690 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return change;
1692}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001693EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Takashi Iwaid5191e52009-11-16 14:58:17 +01001695/**
1696 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1697 *
1698 * The control element is supposed to have the private_value field
1699 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1700 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001701int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1702 unsigned int size, unsigned int __user *_tlv)
1703{
1704 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1705 hda_nid_t nid = get_amp_nid(kcontrol);
1706 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001707 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001708 u32 caps, val1, val2;
1709
1710 if (size < 4 * sizeof(unsigned int))
1711 return -ENOMEM;
1712 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001713 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1714 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001715 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001716 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001717 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001718 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1719 return -EFAULT;
1720 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1721 return -EFAULT;
1722 if (put_user(val1, _tlv + 2))
1723 return -EFAULT;
1724 if (put_user(val2, _tlv + 3))
1725 return -EFAULT;
1726 return 0;
1727}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001728EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001729
Takashi Iwaid5191e52009-11-16 14:58:17 +01001730/**
1731 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1732 * @codec: HD-audio codec
1733 * @nid: NID of a reference widget
1734 * @dir: #HDA_INPUT or #HDA_OUTPUT
1735 * @tlv: TLV data to be stored, at least 4 elements
1736 *
1737 * Set (static) TLV data for a virtual master volume using the AMP caps
1738 * obtained from the reference NID.
1739 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001740 */
1741void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1742 unsigned int *tlv)
1743{
1744 u32 caps;
1745 int nums, step;
1746
1747 caps = query_amp_caps(codec, nid, dir);
1748 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1749 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1750 step = (step + 1) * 25;
1751 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1752 tlv[1] = 2 * sizeof(unsigned int);
1753 tlv[2] = -nums * step;
1754 tlv[3] = step;
1755}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001756EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001757
1758/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001759static struct snd_kcontrol *
1760_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1761 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001762{
1763 struct snd_ctl_elem_id id;
1764 memset(&id, 0, sizeof(id));
1765 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001766 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001767 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1768 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001769 strcpy(id.name, name);
1770 return snd_ctl_find_id(codec->bus->card, &id);
1771}
1772
Takashi Iwaid5191e52009-11-16 14:58:17 +01001773/**
1774 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1775 * @codec: HD-audio codec
1776 * @name: ctl id name string
1777 *
1778 * Get the control element with the given id string and IFACE_MIXER.
1779 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001780struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1781 const char *name)
1782{
1783 return _snd_hda_find_mixer_ctl(codec, name, 0);
1784}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001785EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001786
Takashi Iwaid5191e52009-11-16 14:58:17 +01001787/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001788 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001789 * @codec: HD-audio codec
1790 * @nid: corresponding NID (optional)
1791 * @kctl: the control element to assign
1792 *
1793 * Add the given control element to an array inside the codec instance.
1794 * All control elements belonging to a codec are supposed to be added
1795 * by this function so that a proper clean-up works at the free or
1796 * reconfiguration time.
1797 *
1798 * If non-zero @nid is passed, the NID is assigned to the control element.
1799 * The assignment is shown in the codec proc file.
1800 *
1801 * snd_hda_ctl_add() checks the control subdev id field whether
1802 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001803 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1804 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001805 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001806int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1807 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001808{
1809 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001810 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001811 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001812
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001813 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001814 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001815 if (nid == 0)
1816 nid = get_amp_nid_(kctl->private_value);
1817 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001818 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1819 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001820 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001821 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001822 err = snd_ctl_add(codec->bus->card, kctl);
1823 if (err < 0)
1824 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001825 item = snd_array_new(&codec->mixers);
1826 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001827 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001828 item->kctl = kctl;
1829 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001830 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001831 return 0;
1832}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001833EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001834
Takashi Iwaid5191e52009-11-16 14:58:17 +01001835/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001836 * snd_hda_add_nid - Assign a NID to a control element
1837 * @codec: HD-audio codec
1838 * @nid: corresponding NID (optional)
1839 * @kctl: the control element to assign
1840 * @index: index to kctl
1841 *
1842 * Add the given control element to an array inside the codec instance.
1843 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1844 * NID:KCTL mapping - for example "Capture Source" selector.
1845 */
1846int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1847 unsigned int index, hda_nid_t nid)
1848{
1849 struct hda_nid_item *item;
1850
1851 if (nid > 0) {
1852 item = snd_array_new(&codec->nids);
1853 if (!item)
1854 return -ENOMEM;
1855 item->kctl = kctl;
1856 item->index = index;
1857 item->nid = nid;
1858 return 0;
1859 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01001860 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1861 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001862 return -EINVAL;
1863}
1864EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1865
1866/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001867 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1868 * @codec: HD-audio codec
1869 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001870void snd_hda_ctls_clear(struct hda_codec *codec)
1871{
1872 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001873 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001874 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001875 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001876 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001877 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001878}
1879
Takashi Iwaia65d6292009-02-23 16:57:04 +01001880/* pseudo device locking
1881 * toggle card->shutdown to allow/disallow the device access (as a hack)
1882 */
1883static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001884{
Takashi Iwaia65d6292009-02-23 16:57:04 +01001885 spin_lock(&card->files_lock);
1886 if (card->shutdown) {
1887 spin_unlock(&card->files_lock);
1888 return -EINVAL;
1889 }
1890 card->shutdown = 1;
1891 spin_unlock(&card->files_lock);
1892 return 0;
1893}
1894
1895static void hda_unlock_devices(struct snd_card *card)
1896{
1897 spin_lock(&card->files_lock);
1898 card->shutdown = 0;
1899 spin_unlock(&card->files_lock);
1900}
1901
Takashi Iwaid5191e52009-11-16 14:58:17 +01001902/**
1903 * snd_hda_codec_reset - Clear all objects assigned to the codec
1904 * @codec: HD-audio codec
1905 *
1906 * This frees the all PCM and control elements assigned to the codec, and
1907 * clears the caches and restores the pin default configurations.
1908 *
1909 * When a device is being used, it returns -EBSY. If successfully freed,
1910 * returns zero.
1911 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001912int snd_hda_codec_reset(struct hda_codec *codec)
1913{
1914 struct snd_card *card = codec->bus->card;
1915 int i, pcm;
1916
1917 if (hda_lock_devices(card) < 0)
1918 return -EBUSY;
1919 /* check whether the codec isn't used by any mixer or PCM streams */
1920 if (!list_empty(&card->ctl_files)) {
1921 hda_unlock_devices(card);
1922 return -EBUSY;
1923 }
1924 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1925 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1926 if (!cpcm->pcm)
1927 continue;
1928 if (cpcm->pcm->streams[0].substream_opened ||
1929 cpcm->pcm->streams[1].substream_opened) {
1930 hda_unlock_devices(card);
1931 return -EBUSY;
1932 }
1933 }
1934
1935 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001936
1937#ifdef CONFIG_SND_HDA_POWER_SAVE
1938 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001939 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001940#endif
1941 snd_hda_ctls_clear(codec);
1942 /* relase PCMs */
1943 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001944 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01001945 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001946 clear_bit(codec->pcm_info[i].device,
1947 codec->bus->pcm_dev_bits);
1948 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001949 }
1950 if (codec->patch_ops.free)
1951 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001952 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001953 codec->spec = NULL;
1954 free_hda_cache(&codec->amp_cache);
1955 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001956 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1957 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01001958 /* free only driver_pins so that init_pins + user_pins are restored */
1959 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001960 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001961 codec->num_pcms = 0;
1962 codec->pcm_info = NULL;
1963 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01001964 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1965 codec->slave_dig_outs = NULL;
1966 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001967 module_put(codec->owner);
1968 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001969
1970 /* allow device access again */
1971 hda_unlock_devices(card);
1972 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001973}
1974
Takashi Iwaid5191e52009-11-16 14:58:17 +01001975/**
1976 * snd_hda_add_vmaster - create a virtual master control and add slaves
1977 * @codec: HD-audio codec
1978 * @name: vmaster control name
1979 * @tlv: TLV data (optional)
1980 * @slaves: slave control names (optional)
1981 *
1982 * Create a virtual master control with the given name. The TLV data
1983 * must be either NULL or a valid data.
1984 *
1985 * @slaves is a NULL-terminated array of strings, each of which is a
1986 * slave control name. All controls with these names are assigned to
1987 * the new virtual master control.
1988 *
1989 * This function returns zero if successful or a negative error code.
1990 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01001991int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1992 unsigned int *tlv, const char **slaves)
1993{
1994 struct snd_kcontrol *kctl;
1995 const char **s;
1996 int err;
1997
Takashi Iwai2f085542008-02-22 18:43:50 +01001998 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1999 ;
2000 if (!*s) {
2001 snd_printdd("No slave found for %s\n", name);
2002 return 0;
2003 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002004 kctl = snd_ctl_make_virtual_master(name, tlv);
2005 if (!kctl)
2006 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002007 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002008 if (err < 0)
2009 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002010
Takashi Iwai2134ea42008-01-10 16:53:55 +01002011 for (s = slaves; *s; s++) {
2012 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002013 int i = 0;
2014 for (;;) {
2015 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2016 if (!sctl) {
2017 if (!i)
2018 snd_printdd("Cannot find slave %s, "
2019 "skipped\n", *s);
2020 break;
2021 }
2022 err = snd_ctl_add_slave(kctl, sctl);
2023 if (err < 0)
2024 return err;
2025 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002026 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002027 }
2028 return 0;
2029}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002030EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002031
Takashi Iwaid5191e52009-11-16 14:58:17 +01002032/**
2033 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2034 *
2035 * The control element is supposed to have the private_value field
2036 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2037 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002038int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2039 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 int chs = get_amp_channels(kcontrol);
2042
2043 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2044 uinfo->count = chs == 3 ? 2 : 1;
2045 uinfo->value.integer.min = 0;
2046 uinfo->value.integer.max = 1;
2047 return 0;
2048}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002049EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Takashi Iwaid5191e52009-11-16 14:58:17 +01002051/**
2052 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2053 *
2054 * The control element is supposed to have the private_value field
2055 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2056 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002057int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2058 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2061 hda_nid_t nid = get_amp_nid(kcontrol);
2062 int chs = get_amp_channels(kcontrol);
2063 int dir = get_amp_direction(kcontrol);
2064 int idx = get_amp_index(kcontrol);
2065 long *valp = ucontrol->value.integer.value;
2066
2067 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002068 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002069 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002071 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002072 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return 0;
2074}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002075EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Takashi Iwaid5191e52009-11-16 14:58:17 +01002077/**
2078 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2079 *
2080 * The control element is supposed to have the private_value field
2081 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2082 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002083int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2084 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2087 hda_nid_t nid = get_amp_nid(kcontrol);
2088 int chs = get_amp_channels(kcontrol);
2089 int dir = get_amp_direction(kcontrol);
2090 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 long *valp = ucontrol->value.integer.value;
2092 int change = 0;
2093
Takashi Iwaicb53c622007-08-10 17:21:45 +02002094 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002095 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002096 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002097 HDA_AMP_MUTE,
2098 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002099 valp++;
2100 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002101 if (chs & 2)
2102 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002103 HDA_AMP_MUTE,
2104 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002105#ifdef CONFIG_SND_HDA_POWER_SAVE
2106 if (codec->patch_ops.check_power_status)
2107 codec->patch_ops.check_power_status(codec, nid);
2108#endif
2109 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return change;
2111}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002112EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Takashi Iwai67d634c2009-11-16 15:35:59 +01002114#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002115/**
2116 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2117 *
2118 * This function calls snd_hda_enable_beep_device(), which behaves differently
2119 * depending on beep_mode option.
2120 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002121int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2122 struct snd_ctl_elem_value *ucontrol)
2123{
2124 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2125 long *valp = ucontrol->value.integer.value;
2126
2127 snd_hda_enable_beep_device(codec, *valp);
2128 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2129}
2130EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002131#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133/*
Takashi Iwai985be542005-11-02 18:26:49 +01002134 * bound volume controls
2135 *
2136 * bind multiple volumes (# indices, from 0)
2137 */
2138
2139#define AMP_VAL_IDX_SHIFT 19
2140#define AMP_VAL_IDX_MASK (0x0f<<19)
2141
Takashi Iwaid5191e52009-11-16 14:58:17 +01002142/**
2143 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2144 *
2145 * The control element is supposed to have the private_value field
2146 * set up via HDA_BIND_MUTE*() macros.
2147 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002148int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2149 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002150{
2151 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2152 unsigned long pval;
2153 int err;
2154
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002155 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002156 pval = kcontrol->private_value;
2157 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2158 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2159 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002160 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002161 return err;
2162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002163EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002164
Takashi Iwaid5191e52009-11-16 14:58:17 +01002165/**
2166 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2167 *
2168 * The control element is supposed to have the private_value field
2169 * set up via HDA_BIND_MUTE*() macros.
2170 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002171int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2172 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002173{
2174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2175 unsigned long pval;
2176 int i, indices, err = 0, change = 0;
2177
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002178 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002179 pval = kcontrol->private_value;
2180 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2181 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002182 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2183 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002184 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2185 if (err < 0)
2186 break;
2187 change |= err;
2188 }
2189 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002190 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002191 return err < 0 ? err : change;
2192}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002193EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002194
Takashi Iwaid5191e52009-11-16 14:58:17 +01002195/**
2196 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2197 *
2198 * The control element is supposed to have the private_value field
2199 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002200 */
2201int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_info *uinfo)
2203{
2204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2205 struct hda_bind_ctls *c;
2206 int err;
2207
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002208 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002209 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002210 kcontrol->private_value = *c->values;
2211 err = c->ops->info(kcontrol, uinfo);
2212 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002213 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002214 return err;
2215}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002216EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002217
Takashi Iwaid5191e52009-11-16 14:58:17 +01002218/**
2219 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2220 *
2221 * The control element is supposed to have the private_value field
2222 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2223 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002224int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2225 struct snd_ctl_elem_value *ucontrol)
2226{
2227 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2228 struct hda_bind_ctls *c;
2229 int err;
2230
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002231 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002232 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002233 kcontrol->private_value = *c->values;
2234 err = c->ops->get(kcontrol, ucontrol);
2235 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002236 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002237 return err;
2238}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002239EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002240
Takashi Iwaid5191e52009-11-16 14:58:17 +01002241/**
2242 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2243 *
2244 * The control element is supposed to have the private_value field
2245 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2246 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002247int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_value *ucontrol)
2249{
2250 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2251 struct hda_bind_ctls *c;
2252 unsigned long *vals;
2253 int err = 0, change = 0;
2254
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002255 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002256 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002257 for (vals = c->values; *vals; vals++) {
2258 kcontrol->private_value = *vals;
2259 err = c->ops->put(kcontrol, ucontrol);
2260 if (err < 0)
2261 break;
2262 change |= err;
2263 }
2264 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002265 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002266 return err < 0 ? err : change;
2267}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002268EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002269
Takashi Iwaid5191e52009-11-16 14:58:17 +01002270/**
2271 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2272 *
2273 * The control element is supposed to have the private_value field
2274 * set up via HDA_BIND_VOL() macro.
2275 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002276int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2277 unsigned int size, unsigned int __user *tlv)
2278{
2279 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2280 struct hda_bind_ctls *c;
2281 int err;
2282
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002283 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002284 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002285 kcontrol->private_value = *c->values;
2286 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2287 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002288 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002289 return err;
2290}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002291EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002292
2293struct hda_ctl_ops snd_hda_bind_vol = {
2294 .info = snd_hda_mixer_amp_volume_info,
2295 .get = snd_hda_mixer_amp_volume_get,
2296 .put = snd_hda_mixer_amp_volume_put,
2297 .tlv = snd_hda_mixer_amp_tlv
2298};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002299EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002300
2301struct hda_ctl_ops snd_hda_bind_sw = {
2302 .info = snd_hda_mixer_amp_switch_info,
2303 .get = snd_hda_mixer_amp_switch_get,
2304 .put = snd_hda_mixer_amp_switch_put,
2305 .tlv = snd_hda_mixer_amp_tlv
2306};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002307EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002308
2309/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 * SPDIF out controls
2311 */
2312
Takashi Iwai0ba21762007-04-16 11:29:14 +02002313static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2314 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2317 uinfo->count = 1;
2318 return 0;
2319}
2320
Takashi Iwai0ba21762007-04-16 11:29:14 +02002321static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2322 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
2324 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2325 IEC958_AES0_NONAUDIO |
2326 IEC958_AES0_CON_EMPHASIS_5015 |
2327 IEC958_AES0_CON_NOT_COPYRIGHT;
2328 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2329 IEC958_AES1_CON_ORIGINAL;
2330 return 0;
2331}
2332
Takashi Iwai0ba21762007-04-16 11:29:14 +02002333static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2337 IEC958_AES0_NONAUDIO |
2338 IEC958_AES0_PRO_EMPHASIS_5015;
2339 return 0;
2340}
2341
Takashi Iwai0ba21762007-04-16 11:29:14 +02002342static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2343 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2346
2347 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2348 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2349 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2350 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2351
2352 return 0;
2353}
2354
2355/* convert from SPDIF status bits to HDA SPDIF bits
2356 * bit 0 (DigEn) is always set zero (to be filled later)
2357 */
2358static unsigned short convert_from_spdif_status(unsigned int sbits)
2359{
2360 unsigned short val = 0;
2361
2362 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002363 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002365 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002367 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2368 IEC958_AES0_PRO_EMPHASIS_5015)
2369 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002371 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2372 IEC958_AES0_CON_EMPHASIS_5015)
2373 val |= AC_DIG1_EMPHASIS;
2374 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2375 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002377 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2379 }
2380 return val;
2381}
2382
2383/* convert to SPDIF status bits from HDA SPDIF bits
2384 */
2385static unsigned int convert_to_spdif_status(unsigned short val)
2386{
2387 unsigned int sbits = 0;
2388
Takashi Iwai0ba21762007-04-16 11:29:14 +02002389 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002391 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 sbits |= IEC958_AES0_PROFESSIONAL;
2393 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002394 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2396 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002397 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002399 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002401 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2403 sbits |= val & (0x7f << 8);
2404 }
2405 return sbits;
2406}
2407
Takashi Iwai2f728532008-09-25 16:32:41 +02002408/* set digital convert verbs both for the given NID and its slaves */
2409static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2410 int verb, int val)
2411{
2412 hda_nid_t *d;
2413
Takashi Iwai9e976972008-11-25 08:17:20 +01002414 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002415 d = codec->slave_dig_outs;
2416 if (!d)
2417 return;
2418 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002419 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002420}
2421
2422static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2423 int dig1, int dig2)
2424{
2425 if (dig1 != -1)
2426 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2427 if (dig2 != -1)
2428 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2429}
2430
Takashi Iwai0ba21762007-04-16 11:29:14 +02002431static int snd_hda_spdif_default_put(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 hda_nid_t nid = kcontrol->private_value;
2436 unsigned short val;
2437 int change;
2438
Ingo Molnar62932df2006-01-16 16:34:20 +01002439 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 codec->spdif_status = ucontrol->value.iec958.status[0] |
2441 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2442 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2443 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2444 val = convert_from_spdif_status(codec->spdif_status);
2445 val |= codec->spdif_ctls & 1;
2446 change = codec->spdif_ctls != val;
2447 codec->spdif_ctls = val;
2448
Takashi Iwai2f728532008-09-25 16:32:41 +02002449 if (change)
2450 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Ingo Molnar62932df2006-01-16 16:34:20 +01002452 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return change;
2454}
2455
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002456#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Takashi Iwai0ba21762007-04-16 11:29:14 +02002458static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2462
Takashi Iwai0ba21762007-04-16 11:29:14 +02002463 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return 0;
2465}
2466
Takashi Iwai0ba21762007-04-16 11:29:14 +02002467static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2468 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2471 hda_nid_t nid = kcontrol->private_value;
2472 unsigned short val;
2473 int change;
2474
Ingo Molnar62932df2006-01-16 16:34:20 +01002475 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002476 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002478 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002480 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002482 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002483 /* unmute amp switch (if any) */
2484 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002485 (val & AC_DIG1_ENABLE))
2486 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2487 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002489 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return change;
2491}
2492
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002493static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 {
2495 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2496 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002497 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 .info = snd_hda_spdif_mask_info,
2499 .get = snd_hda_spdif_cmask_get,
2500 },
2501 {
2502 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2503 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002504 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 .info = snd_hda_spdif_mask_info,
2506 .get = snd_hda_spdif_pmask_get,
2507 },
2508 {
2509 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002510 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 .info = snd_hda_spdif_mask_info,
2512 .get = snd_hda_spdif_default_get,
2513 .put = snd_hda_spdif_default_put,
2514 },
2515 {
2516 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002517 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 .info = snd_hda_spdif_out_switch_info,
2519 .get = snd_hda_spdif_out_switch_get,
2520 .put = snd_hda_spdif_out_switch_put,
2521 },
2522 { } /* end */
2523};
2524
Takashi Iwai09f99702008-02-04 12:31:13 +01002525#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527/**
2528 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2529 * @codec: the HDA codec
2530 * @nid: audio out widget NID
2531 *
2532 * Creates controls related with the SPDIF output.
2533 * Called from each patch supporting the SPDIF out.
2534 *
2535 * Returns 0 if successful, or a negative error code.
2536 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002537int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
2539 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002540 struct snd_kcontrol *kctl;
2541 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002542 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Takashi Iwai09f99702008-02-04 12:31:13 +01002544 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2545 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2546 idx))
2547 break;
2548 }
2549 if (idx >= SPDIF_MAX_IDX) {
2550 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2551 return -EBUSY;
2552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2554 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002555 if (!kctl)
2556 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002557 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002559 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002560 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return err;
2562 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002563 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002564 snd_hda_codec_read(codec, nid, 0,
2565 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2567 return 0;
2568}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002569EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002572 * SPDIF sharing with analog output
2573 */
2574static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2575 struct snd_ctl_elem_value *ucontrol)
2576{
2577 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2578 ucontrol->value.integer.value[0] = mout->share_spdif;
2579 return 0;
2580}
2581
2582static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2583 struct snd_ctl_elem_value *ucontrol)
2584{
2585 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2586 mout->share_spdif = !!ucontrol->value.integer.value[0];
2587 return 0;
2588}
2589
2590static struct snd_kcontrol_new spdif_share_sw = {
2591 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2592 .name = "IEC958 Default PCM Playback Switch",
2593 .info = snd_ctl_boolean_mono_info,
2594 .get = spdif_share_sw_get,
2595 .put = spdif_share_sw_put,
2596};
2597
Takashi Iwaid5191e52009-11-16 14:58:17 +01002598/**
2599 * snd_hda_create_spdif_share_sw - create Default PCM switch
2600 * @codec: the HDA codec
2601 * @mout: multi-out instance
2602 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002603int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2604 struct hda_multi_out *mout)
2605{
2606 if (!mout->dig_out_nid)
2607 return 0;
2608 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002609 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2610 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002611}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002612EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002613
2614/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 * SPDIF input
2616 */
2617
2618#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2619
Takashi Iwai0ba21762007-04-16 11:29:14 +02002620static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
2623 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2624
2625 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2626 return 0;
2627}
2628
Takashi Iwai0ba21762007-04-16 11:29:14 +02002629static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2630 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
2632 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2633 hda_nid_t nid = kcontrol->private_value;
2634 unsigned int val = !!ucontrol->value.integer.value[0];
2635 int change;
2636
Ingo Molnar62932df2006-01-16 16:34:20 +01002637 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002639 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002641 snd_hda_codec_write_cache(codec, nid, 0,
2642 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002644 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return change;
2646}
2647
Takashi Iwai0ba21762007-04-16 11:29:14 +02002648static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2649 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2652 hda_nid_t nid = kcontrol->private_value;
2653 unsigned short val;
2654 unsigned int sbits;
2655
Andrew Paprocki3982d172007-12-19 12:13:44 +01002656 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 sbits = convert_to_spdif_status(val);
2658 ucontrol->value.iec958.status[0] = sbits;
2659 ucontrol->value.iec958.status[1] = sbits >> 8;
2660 ucontrol->value.iec958.status[2] = sbits >> 16;
2661 ucontrol->value.iec958.status[3] = sbits >> 24;
2662 return 0;
2663}
2664
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002665static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 {
2667 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002668 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 .info = snd_hda_spdif_in_switch_info,
2670 .get = snd_hda_spdif_in_switch_get,
2671 .put = snd_hda_spdif_in_switch_put,
2672 },
2673 {
2674 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2675 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002676 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 .info = snd_hda_spdif_mask_info,
2678 .get = snd_hda_spdif_in_status_get,
2679 },
2680 { } /* end */
2681};
2682
2683/**
2684 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2685 * @codec: the HDA codec
2686 * @nid: audio in widget NID
2687 *
2688 * Creates controls related with the SPDIF input.
2689 * Called from each patch supporting the SPDIF in.
2690 *
2691 * Returns 0 if successful, or a negative error code.
2692 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002693int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
2695 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002696 struct snd_kcontrol *kctl;
2697 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002698 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Takashi Iwai09f99702008-02-04 12:31:13 +01002700 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2701 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2702 idx))
2703 break;
2704 }
2705 if (idx >= SPDIF_MAX_IDX) {
2706 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2707 return -EBUSY;
2708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2710 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002711 if (!kctl)
2712 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002714 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002715 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return err;
2717 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002718 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002719 snd_hda_codec_read(codec, nid, 0,
2720 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002721 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return 0;
2723}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002724EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Takashi Iwaicb53c622007-08-10 17:21:45 +02002726#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002727/*
2728 * command cache
2729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002731/* build a 32bit cache key with the widget id and the command parameter */
2732#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2733#define get_cmd_cache_nid(key) ((key) & 0xff)
2734#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2735
2736/**
2737 * snd_hda_codec_write_cache - send a single command with caching
2738 * @codec: the HDA codec
2739 * @nid: NID to send the command
2740 * @direct: direct flag
2741 * @verb: the verb to send
2742 * @parm: the parameter for the verb
2743 *
2744 * Send a single command without waiting for response.
2745 *
2746 * Returns 0 if successful, or a negative error code.
2747 */
2748int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2749 int direct, unsigned int verb, unsigned int parm)
2750{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002751 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2752 struct hda_cache_head *c;
2753 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002754
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002755 if (err < 0)
2756 return err;
2757 /* parm may contain the verb stuff for get/set amp */
2758 verb = verb | (parm >> 8);
2759 parm &= 0xff;
2760 key = build_cmd_cache_key(nid, verb);
2761 mutex_lock(&codec->bus->cmd_mutex);
2762 c = get_alloc_hash(&codec->cmd_cache, key);
2763 if (c)
2764 c->val = parm;
2765 mutex_unlock(&codec->bus->cmd_mutex);
2766 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002767}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002768EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002769
Takashi Iwaid5191e52009-11-16 14:58:17 +01002770/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002771 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2772 * @codec: the HDA codec
2773 * @nid: NID to send the command
2774 * @direct: direct flag
2775 * @verb: the verb to send
2776 * @parm: the parameter for the verb
2777 *
2778 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2779 * command if the parameter is already identical with the cached value.
2780 * If not, it sends the command and refreshes the cache.
2781 *
2782 * Returns 0 if successful, or a negative error code.
2783 */
2784int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2785 int direct, unsigned int verb, unsigned int parm)
2786{
2787 struct hda_cache_head *c;
2788 u32 key;
2789
2790 /* parm may contain the verb stuff for get/set amp */
2791 verb = verb | (parm >> 8);
2792 parm &= 0xff;
2793 key = build_cmd_cache_key(nid, verb);
2794 mutex_lock(&codec->bus->cmd_mutex);
2795 c = get_hash(&codec->cmd_cache, key);
2796 if (c && c->val == parm) {
2797 mutex_unlock(&codec->bus->cmd_mutex);
2798 return 0;
2799 }
2800 mutex_unlock(&codec->bus->cmd_mutex);
2801 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2802}
2803EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2804
2805/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002806 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2807 * @codec: HD-audio codec
2808 *
2809 * Execute all verbs recorded in the command caches to resume.
2810 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002811void snd_hda_codec_resume_cache(struct hda_codec *codec)
2812{
Takashi Iwai603c4012008-07-30 15:01:44 +02002813 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002814 int i;
2815
Takashi Iwai603c4012008-07-30 15:01:44 +02002816 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002817 u32 key = buffer->key;
2818 if (!key)
2819 continue;
2820 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2821 get_cmd_cache_cmd(key), buffer->val);
2822 }
2823}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002824EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002825
2826/**
2827 * snd_hda_sequence_write_cache - sequence writes with caching
2828 * @codec: the HDA codec
2829 * @seq: VERB array to send
2830 *
2831 * Send the commands sequentially from the given array.
2832 * Thte commands are recorded on cache for power-save and resume.
2833 * The array must be terminated with NID=0.
2834 */
2835void snd_hda_sequence_write_cache(struct hda_codec *codec,
2836 const struct hda_verb *seq)
2837{
2838 for (; seq->nid; seq++)
2839 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2840 seq->param);
2841}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002842EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002843#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002844
Takashi Iwai54d17402005-11-21 16:33:22 +01002845/*
2846 * set power state of the codec
2847 */
2848static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2849 unsigned int power_state)
2850{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002851 hda_nid_t nid;
2852 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002853
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002854 /* this delay seems necessary to avoid click noise at power-down */
2855 if (power_state == AC_PWRST_D3)
2856 msleep(100);
2857 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002858 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002859 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002860 if (power_state == AC_PWRST_D0 &&
2861 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002862 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002863
Takashi Iwaicb53c622007-08-10 17:21:45 +02002864 nid = codec->start_nid;
2865 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002866 unsigned int wcaps = get_wcaps(codec, nid);
2867 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002868 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002869 if (power_state == AC_PWRST_D3 &&
2870 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002871 unsigned int pincap;
2872 /*
2873 * don't power down the widget if it controls
2874 * eapd and EAPD_BTLENABLE is set.
2875 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01002876 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002877 if (pincap & AC_PINCAP_EAPD) {
2878 int eapd = snd_hda_codec_read(codec,
2879 nid, 0,
2880 AC_VERB_GET_EAPD_BTLENABLE, 0);
2881 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002882 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002883 continue;
2884 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002885 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002886 snd_hda_codec_write(codec, nid, 0,
2887 AC_VERB_SET_POWER_STATE,
2888 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002889 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002890 }
2891
Takashi Iwaicb53c622007-08-10 17:21:45 +02002892 if (power_state == AC_PWRST_D0) {
2893 unsigned long end_time;
2894 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002895 /* wait until the codec reachs to D0 */
2896 end_time = jiffies + msecs_to_jiffies(500);
2897 do {
2898 state = snd_hda_codec_read(codec, fg, 0,
2899 AC_VERB_GET_POWER_STATE, 0);
2900 if (state == power_state)
2901 break;
2902 msleep(1);
2903 } while (time_after_eq(end_time, jiffies));
2904 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002905}
2906
Takashi Iwai11aeff02008-07-30 15:01:46 +02002907#ifdef CONFIG_SND_HDA_HWDEP
2908/* execute additional init verbs */
2909static void hda_exec_init_verbs(struct hda_codec *codec)
2910{
2911 if (codec->init_verbs.list)
2912 snd_hda_sequence_write(codec, codec->init_verbs.list);
2913}
2914#else
2915static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2916#endif
2917
Takashi Iwaicb53c622007-08-10 17:21:45 +02002918#ifdef SND_HDA_NEEDS_RESUME
2919/*
2920 * call suspend and power-down; used both from PM and power-save
2921 */
2922static void hda_call_codec_suspend(struct hda_codec *codec)
2923{
2924 if (codec->patch_ops.suspend)
2925 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2926 hda_set_power_state(codec,
2927 codec->afg ? codec->afg : codec->mfg,
2928 AC_PWRST_D3);
2929#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01002930 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002931 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002932 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002933 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01002934 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002935#endif
2936}
2937
2938/*
2939 * kick up codec; used both from PM and power-save
2940 */
2941static void hda_call_codec_resume(struct hda_codec *codec)
2942{
2943 hda_set_power_state(codec,
2944 codec->afg ? codec->afg : codec->mfg,
2945 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01002946 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02002947 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002948 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002949 if (codec->patch_ops.resume)
2950 codec->patch_ops.resume(codec);
2951 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002952 if (codec->patch_ops.init)
2953 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002954 snd_hda_codec_resume_amp(codec);
2955 snd_hda_codec_resume_cache(codec);
2956 }
2957}
2958#endif /* SND_HDA_NEEDS_RESUME */
2959
Takashi Iwai54d17402005-11-21 16:33:22 +01002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961/**
2962 * snd_hda_build_controls - build mixer controls
2963 * @bus: the BUS
2964 *
2965 * Creates mixer controls for each codec included in the bus.
2966 *
2967 * Returns 0 if successful, otherwise a negative error code.
2968 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002969int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002971 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Takashi Iwai0ba21762007-04-16 11:29:14 +02002973 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002974 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002975 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01002976 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002977 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002978 err = snd_hda_codec_reset(codec);
2979 if (err < 0) {
2980 printk(KERN_ERR
2981 "hda_codec: cannot revert codec\n");
2982 return err;
2983 }
2984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002986 return 0;
2987}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002988EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002990int snd_hda_codec_build_controls(struct hda_codec *codec)
2991{
2992 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02002993 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002994 /* continue to initialize... */
2995 if (codec->patch_ops.init)
2996 err = codec->patch_ops.init(codec);
2997 if (!err && codec->patch_ops.build_controls)
2998 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002999 if (err < 0)
3000 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return 0;
3002}
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004/*
3005 * stream formats
3006 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003007struct hda_rate_tbl {
3008 unsigned int hz;
3009 unsigned int alsa_bits;
3010 unsigned int hda_fmt;
3011};
3012
3013static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003015
3016 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
3018 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
3019 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
3020 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
3021 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
3022 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
3023 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
3024 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
3025 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
3026 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
3027 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003028#define AC_PAR_PCM_RATE_BITS 11
3029 /* up to bits 10, 384kHZ isn't supported properly */
3030
3031 /* not autodetected value */
3032 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003033
Takashi Iwaibefdf312005-08-22 13:57:55 +02003034 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035};
3036
3037/**
3038 * snd_hda_calc_stream_format - calculate format bitset
3039 * @rate: the sample rate
3040 * @channels: the number of channels
3041 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3042 * @maxbps: the max. bps
3043 *
3044 * Calculate the format bitset from the given rate, channels and th PCM format.
3045 *
3046 * Return zero if invalid.
3047 */
3048unsigned int snd_hda_calc_stream_format(unsigned int rate,
3049 unsigned int channels,
3050 unsigned int format,
3051 unsigned int maxbps)
3052{
3053 int i;
3054 unsigned int val = 0;
3055
Takashi Iwaibefdf312005-08-22 13:57:55 +02003056 for (i = 0; rate_bits[i].hz; i++)
3057 if (rate_bits[i].hz == rate) {
3058 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 break;
3060 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003061 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 snd_printdd("invalid rate %d\n", rate);
3063 return 0;
3064 }
3065
3066 if (channels == 0 || channels > 8) {
3067 snd_printdd("invalid channels %d\n", channels);
3068 return 0;
3069 }
3070 val |= channels - 1;
3071
3072 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003073 case 8:
3074 val |= 0x00;
3075 break;
3076 case 16:
3077 val |= 0x10;
3078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 case 20:
3080 case 24:
3081 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003082 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 val |= 0x40;
3084 else if (maxbps >= 24)
3085 val |= 0x30;
3086 else
3087 val |= 0x20;
3088 break;
3089 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003090 snd_printdd("invalid format width %d\n",
3091 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return 0;
3093 }
3094
3095 return val;
3096}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003097EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003099static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3100{
3101 unsigned int val = 0;
3102 if (nid != codec->afg &&
3103 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3104 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3105 if (!val || val == -1)
3106 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3107 if (!val || val == -1)
3108 return 0;
3109 return val;
3110}
3111
3112static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3113{
3114 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3115 get_pcm_param);
3116}
3117
3118static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3119{
3120 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3121 if (!streams || streams == -1)
3122 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3123 if (!streams || streams == -1)
3124 return 0;
3125 return streams;
3126}
3127
3128static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3129{
3130 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3131 get_stream_param);
3132}
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134/**
3135 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3136 * @codec: the HDA codec
3137 * @nid: NID to query
3138 * @ratesp: the pointer to store the detected rate bitflags
3139 * @formatsp: the pointer to store the detected formats
3140 * @bpsp: the pointer to store the detected format widths
3141 *
3142 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3143 * or @bsps argument is ignored.
3144 *
3145 * Returns 0 if successful, otherwise a negative error code.
3146 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003147static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3149{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003150 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003152 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003153 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155 if (ratesp) {
3156 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003157 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003159 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003161 if (rates == 0) {
3162 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3163 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3164 nid, val,
3165 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3166 return -EIO;
3167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 *ratesp = rates;
3169 }
3170
3171 if (formatsp || bpsp) {
3172 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003173 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003175 streams = query_stream_param(codec, nid);
3176 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 bps = 0;
3180 if (streams & AC_SUPFMT_PCM) {
3181 if (val & AC_SUPPCM_BITS_8) {
3182 formats |= SNDRV_PCM_FMTBIT_U8;
3183 bps = 8;
3184 }
3185 if (val & AC_SUPPCM_BITS_16) {
3186 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3187 bps = 16;
3188 }
3189 if (wcaps & AC_WCAP_DIGITAL) {
3190 if (val & AC_SUPPCM_BITS_32)
3191 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3192 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3193 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3194 if (val & AC_SUPPCM_BITS_24)
3195 bps = 24;
3196 else if (val & AC_SUPPCM_BITS_20)
3197 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003198 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3199 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3201 if (val & AC_SUPPCM_BITS_32)
3202 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 else if (val & AC_SUPPCM_BITS_24)
3204 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003205 else if (val & AC_SUPPCM_BITS_20)
3206 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 }
3208 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003209 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003211 if (!bps)
3212 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003213 }
3214 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003215 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 /* temporary hack: we have still no proper support
3217 * for the direct AC3 stream...
3218 */
3219 formats |= SNDRV_PCM_FMTBIT_U8;
3220 bps = 8;
3221 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003222 if (formats == 0) {
3223 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3224 "(nid=0x%x, val=0x%x, ovrd=%i, "
3225 "streams=0x%x)\n",
3226 nid, val,
3227 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3228 streams);
3229 return -EIO;
3230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 if (formatsp)
3232 *formatsp = formats;
3233 if (bpsp)
3234 *bpsp = bps;
3235 }
3236
3237 return 0;
3238}
3239
3240/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003241 * snd_hda_is_supported_format - Check the validity of the format
3242 * @codec: HD-audio codec
3243 * @nid: NID to check
3244 * @format: the HD-audio format value to check
3245 *
3246 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 *
3248 * Returns 1 if supported, 0 if not.
3249 */
3250int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3251 unsigned int format)
3252{
3253 int i;
3254 unsigned int val = 0, rate, stream;
3255
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003256 val = query_pcm_param(codec, nid);
3257 if (!val)
3258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003261 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003262 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (val & (1 << i))
3264 break;
3265 return 0;
3266 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003267 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 return 0;
3269
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003270 stream = query_stream_param(codec, nid);
3271 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return 0;
3273
3274 if (stream & AC_SUPFMT_PCM) {
3275 switch (format & 0xf0) {
3276 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003277 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return 0;
3279 break;
3280 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003281 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return 0;
3283 break;
3284 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003285 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return 0;
3287 break;
3288 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003289 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return 0;
3291 break;
3292 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003293 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return 0;
3295 break;
3296 default:
3297 return 0;
3298 }
3299 } else {
3300 /* FIXME: check for float32 and AC3? */
3301 }
3302
3303 return 1;
3304}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003305EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307/*
3308 * PCM stuff
3309 */
3310static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3311 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003312 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
3314 return 0;
3315}
3316
3317static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3318 struct hda_codec *codec,
3319 unsigned int stream_tag,
3320 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003321 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3324 return 0;
3325}
3326
3327static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3328 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003329 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Takashi Iwai888afa12008-03-18 09:57:50 +01003331 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 return 0;
3333}
3334
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003335static int set_pcm_default_values(struct hda_codec *codec,
3336 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003338 int err;
3339
Takashi Iwai0ba21762007-04-16 11:29:14 +02003340 /* query support PCM information from the given NID */
3341 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003342 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003343 info->rates ? NULL : &info->rates,
3344 info->formats ? NULL : &info->formats,
3345 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003346 if (err < 0)
3347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 }
3349 if (info->ops.open == NULL)
3350 info->ops.open = hda_pcm_default_open_close;
3351 if (info->ops.close == NULL)
3352 info->ops.close = hda_pcm_default_open_close;
3353 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003354 if (snd_BUG_ON(!info->nid))
3355 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 info->ops.prepare = hda_pcm_default_prepare;
3357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003359 if (snd_BUG_ON(!info->nid))
3360 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 info->ops.cleanup = hda_pcm_default_cleanup;
3362 }
3363 return 0;
3364}
3365
Takashi Iwaid5191e52009-11-16 14:58:17 +01003366/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003367const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3368 "Audio", "SPDIF", "HDMI", "Modem"
3369};
3370
Takashi Iwai176d5332008-07-30 15:01:44 +02003371/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003372 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003373 *
3374 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003375 */
3376static int get_empty_pcm_device(struct hda_bus *bus, int type)
3377{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003378 /* audio device indices; not linear to keep compatibility */
3379 static int audio_idx[HDA_PCM_NTYPES][5] = {
3380 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3381 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003382 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003383 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003384 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003385 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003386
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003387 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003388 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3389 return -EINVAL;
3390 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003391
3392 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3393 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3394 return audio_idx[type][i];
3395
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003396 snd_printk(KERN_WARNING "Too many %s devices\n",
3397 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003398 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003399}
3400
3401/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003402 * attach a new PCM stream
3403 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003404static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003405{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003406 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003407 struct hda_pcm_stream *info;
3408 int stream, err;
3409
Takashi Iwaib91f0802008-11-04 08:43:08 +01003410 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003411 return -EINVAL;
3412 for (stream = 0; stream < 2; stream++) {
3413 info = &pcm->stream[stream];
3414 if (info->substreams) {
3415 err = set_pcm_default_values(codec, info);
3416 if (err < 0)
3417 return err;
3418 }
3419 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003420 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003421}
3422
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003423/* assign all PCMs of the given codec */
3424int snd_hda_codec_build_pcms(struct hda_codec *codec)
3425{
3426 unsigned int pcm;
3427 int err;
3428
3429 if (!codec->num_pcms) {
3430 if (!codec->patch_ops.build_pcms)
3431 return 0;
3432 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003433 if (err < 0) {
3434 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003435 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003436 err = snd_hda_codec_reset(codec);
3437 if (err < 0) {
3438 printk(KERN_ERR
3439 "hda_codec: cannot revert codec\n");
3440 return err;
3441 }
3442 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003443 }
3444 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3445 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3446 int dev;
3447
3448 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003449 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003450
3451 if (!cpcm->pcm) {
3452 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3453 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003454 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003455 cpcm->device = dev;
3456 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003457 if (err < 0) {
3458 printk(KERN_ERR "hda_codec: cannot attach "
3459 "PCM stream %d for codec #%d\n",
3460 dev, codec->addr);
3461 continue; /* no fatal error */
3462 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003463 }
3464 }
3465 return 0;
3466}
3467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468/**
3469 * snd_hda_build_pcms - build PCM information
3470 * @bus: the BUS
3471 *
3472 * Create PCM information for each codec included in the bus.
3473 *
3474 * The build_pcms codec patch is requested to set up codec->num_pcms and
3475 * codec->pcm_info properly. The array is referred by the top-level driver
3476 * to create its PCM instances.
3477 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3478 * callback.
3479 *
3480 * At least, substreams, channels_min and channels_max must be filled for
3481 * each stream. substreams = 0 indicates that the stream doesn't exist.
3482 * When rates and/or formats are zero, the supported values are queried
3483 * from the given nid. The nid is used also by the default ops.prepare
3484 * and ops.cleanup callbacks.
3485 *
3486 * The driver needs to call ops.open in its open callback. Similarly,
3487 * ops.close is supposed to be called in the close callback.
3488 * ops.prepare should be called in the prepare or hw_params callback
3489 * with the proper parameters for set up.
3490 * ops.cleanup should be called in hw_free for clean up of streams.
3491 *
3492 * This function returns 0 if successfull, or a negative error code.
3493 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003494int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003496 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Takashi Iwai0ba21762007-04-16 11:29:14 +02003498 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003499 int err = snd_hda_codec_build_pcms(codec);
3500 if (err < 0)
3501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
3503 return 0;
3504}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003505EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507/**
3508 * snd_hda_check_board_config - compare the current codec with the config table
3509 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003510 * @num_configs: number of config enums
3511 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 * @tbl: configuration table, terminated by null entries
3513 *
3514 * Compares the modelname or PCI subsystem id of the current codec with the
3515 * given configuration table. If a matching entry is found, returns its
3516 * config value (supposed to be 0 or positive).
3517 *
3518 * If no entries are matching, the function returns a negative value.
3519 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003520int snd_hda_check_board_config(struct hda_codec *codec,
3521 int num_configs, const char **models,
3522 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003524 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003525 int i;
3526 for (i = 0; i < num_configs; i++) {
3527 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003528 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003529 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3530 "selected\n", models[i]);
3531 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
3533 }
3534 }
3535
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003536 if (!codec->bus->pci || !tbl)
3537 return -1;
3538
3539 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3540 if (!tbl)
3541 return -1;
3542 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003543#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003544 char tmp[10];
3545 const char *model = NULL;
3546 if (models)
3547 model = models[tbl->value];
3548 if (!model) {
3549 sprintf(tmp, "#%d", tbl->value);
3550 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003552 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3553 "for config %x:%x (%s)\n",
3554 model, tbl->subvendor, tbl->subdevice,
3555 (tbl->name ? tbl->name : "Unknown device"));
3556#endif
3557 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
3559 return -1;
3560}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003561EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
3563/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003564 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003565 subsystem ID with the
3566 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003567
3568 This is important for Gateway notebooks with SB450 HDA Audio
3569 where the vendor ID of the PCI device is:
3570 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3571 and the vendor/subvendor are found only at the codec.
3572
3573 * @codec: the HDA codec
3574 * @num_configs: number of config enums
3575 * @models: array of model name strings
3576 * @tbl: configuration table, terminated by null entries
3577 *
3578 * Compares the modelname or PCI subsystem id of the current codec with the
3579 * given configuration table. If a matching entry is found, returns its
3580 * config value (supposed to be 0 or positive).
3581 *
3582 * If no entries are matching, the function returns a negative value.
3583 */
3584int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3585 int num_configs, const char **models,
3586 const struct snd_pci_quirk *tbl)
3587{
3588 const struct snd_pci_quirk *q;
3589
3590 /* Search for codec ID */
3591 for (q = tbl; q->subvendor; q++) {
3592 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3593
3594 if (vendorid == codec->subsystem_id)
3595 break;
3596 }
3597
3598 if (!q->subvendor)
3599 return -1;
3600
3601 tbl = q;
3602
3603 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003604#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003605 char tmp[10];
3606 const char *model = NULL;
3607 if (models)
3608 model = models[tbl->value];
3609 if (!model) {
3610 sprintf(tmp, "#%d", tbl->value);
3611 model = tmp;
3612 }
3613 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3614 "for config %x:%x (%s)\n",
3615 model, tbl->subvendor, tbl->subdevice,
3616 (tbl->name ? tbl->name : "Unknown device"));
3617#endif
3618 return tbl->value;
3619 }
3620 return -1;
3621}
3622EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3623
3624/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 * snd_hda_add_new_ctls - create controls from the array
3626 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003627 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 *
3629 * This helper function creates and add new controls in the given array.
3630 * The array must be terminated with an empty entry as terminator.
3631 *
3632 * Returns 0 if successful, or a negative error code.
3633 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003634int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003636 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003639 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003640 if (knew->iface == -1) /* skip this codec private value */
3641 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003642 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003643 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003644 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003645 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003646 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003647 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003648 return err;
3649 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003650 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003651 return -ENOMEM;
3652 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003653 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003654 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003655 return err;
3656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
3658 return 0;
3659}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003660EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Takashi Iwaicb53c622007-08-10 17:21:45 +02003662#ifdef CONFIG_SND_HDA_POWER_SAVE
3663static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3664 unsigned int power_state);
3665
3666static void hda_power_work(struct work_struct *work)
3667{
3668 struct hda_codec *codec =
3669 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003670 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003671
Maxim Levitsky2e492462007-09-03 15:26:57 +02003672 if (!codec->power_on || codec->power_count) {
3673 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003674 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003675 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003676
3677 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003678 if (bus->ops.pm_notify)
3679 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003680}
3681
3682static void hda_keep_power_on(struct hda_codec *codec)
3683{
3684 codec->power_count++;
3685 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003686 codec->power_jiffies = jiffies;
3687}
3688
Takashi Iwaid5191e52009-11-16 14:58:17 +01003689/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003690void snd_hda_update_power_acct(struct hda_codec *codec)
3691{
3692 unsigned long delta = jiffies - codec->power_jiffies;
3693 if (codec->power_on)
3694 codec->power_on_acct += delta;
3695 else
3696 codec->power_off_acct += delta;
3697 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003698}
3699
Takashi Iwaid5191e52009-11-16 14:58:17 +01003700/**
3701 * snd_hda_power_up - Power-up the codec
3702 * @codec: HD-audio codec
3703 *
3704 * Increment the power-up counter and power up the hardware really when
3705 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003706 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003707void snd_hda_power_up(struct hda_codec *codec)
3708{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003709 struct hda_bus *bus = codec->bus;
3710
Takashi Iwaicb53c622007-08-10 17:21:45 +02003711 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003712 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003713 return;
3714
Takashi Iwaia2f63092009-11-11 09:34:25 +01003715 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003716 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003717 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003718 if (bus->ops.pm_notify)
3719 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003720 hda_call_codec_resume(codec);
3721 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003722 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003723}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003724EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003725
3726#define power_save(codec) \
3727 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003728
Takashi Iwaid5191e52009-11-16 14:58:17 +01003729/**
3730 * snd_hda_power_down - Power-down the codec
3731 * @codec: HD-audio codec
3732 *
3733 * Decrement the power-up counter and schedules the power-off work if
3734 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003735 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003736void snd_hda_power_down(struct hda_codec *codec)
3737{
3738 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003739 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003740 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003741 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003742 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003743 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003744 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003745 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003746}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003747EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003748
Takashi Iwaid5191e52009-11-16 14:58:17 +01003749/**
3750 * snd_hda_check_amp_list_power - Check the amp list and update the power
3751 * @codec: HD-audio codec
3752 * @check: the object containing an AMP list and the status
3753 * @nid: NID to check / update
3754 *
3755 * Check whether the given NID is in the amp list. If it's in the list,
3756 * check the current AMP status, and update the the power-status according
3757 * to the mute status.
3758 *
3759 * This function is supposed to be set or called from the check_power_status
3760 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003761 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003762int snd_hda_check_amp_list_power(struct hda_codec *codec,
3763 struct hda_loopback_check *check,
3764 hda_nid_t nid)
3765{
3766 struct hda_amp_list *p;
3767 int ch, v;
3768
3769 if (!check->amplist)
3770 return 0;
3771 for (p = check->amplist; p->nid; p++) {
3772 if (p->nid == nid)
3773 break;
3774 }
3775 if (!p->nid)
3776 return 0; /* nothing changed */
3777
3778 for (p = check->amplist; p->nid; p++) {
3779 for (ch = 0; ch < 2; ch++) {
3780 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3781 p->idx);
3782 if (!(v & HDA_AMP_MUTE) && v > 0) {
3783 if (!check->power_on) {
3784 check->power_on = 1;
3785 snd_hda_power_up(codec);
3786 }
3787 return 1;
3788 }
3789 }
3790 }
3791 if (check->power_on) {
3792 check->power_on = 0;
3793 snd_hda_power_down(codec);
3794 }
3795 return 0;
3796}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003797EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003798#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003800/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003801 * Channel mode helper
3802 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003803
3804/**
3805 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3806 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003807int snd_hda_ch_mode_info(struct hda_codec *codec,
3808 struct snd_ctl_elem_info *uinfo,
3809 const struct hda_channel_mode *chmode,
3810 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003811{
3812 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3813 uinfo->count = 1;
3814 uinfo->value.enumerated.items = num_chmodes;
3815 if (uinfo->value.enumerated.item >= num_chmodes)
3816 uinfo->value.enumerated.item = num_chmodes - 1;
3817 sprintf(uinfo->value.enumerated.name, "%dch",
3818 chmode[uinfo->value.enumerated.item].channels);
3819 return 0;
3820}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003821EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003822
Takashi Iwaid5191e52009-11-16 14:58:17 +01003823/**
3824 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3825 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003826int snd_hda_ch_mode_get(struct hda_codec *codec,
3827 struct snd_ctl_elem_value *ucontrol,
3828 const struct hda_channel_mode *chmode,
3829 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003830 int max_channels)
3831{
3832 int i;
3833
3834 for (i = 0; i < num_chmodes; i++) {
3835 if (max_channels == chmode[i].channels) {
3836 ucontrol->value.enumerated.item[0] = i;
3837 break;
3838 }
3839 }
3840 return 0;
3841}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003842EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003843
Takashi Iwaid5191e52009-11-16 14:58:17 +01003844/**
3845 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3846 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003847int snd_hda_ch_mode_put(struct hda_codec *codec,
3848 struct snd_ctl_elem_value *ucontrol,
3849 const struct hda_channel_mode *chmode,
3850 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003851 int *max_channelsp)
3852{
3853 unsigned int mode;
3854
3855 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01003856 if (mode >= num_chmodes)
3857 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003858 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003859 return 0;
3860 /* change the current channel setting */
3861 *max_channelsp = chmode[mode].channels;
3862 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003863 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003864 return 1;
3865}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003866EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868/*
3869 * input MUX helper
3870 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003871
3872/**
3873 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3874 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003875int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3876 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
3878 unsigned int index;
3879
3880 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3881 uinfo->count = 1;
3882 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003883 if (!imux->num_items)
3884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 index = uinfo->value.enumerated.item;
3886 if (index >= imux->num_items)
3887 index = imux->num_items - 1;
3888 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3889 return 0;
3890}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003891EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Takashi Iwaid5191e52009-11-16 14:58:17 +01003893/**
3894 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3895 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003896int snd_hda_input_mux_put(struct hda_codec *codec,
3897 const struct hda_input_mux *imux,
3898 struct snd_ctl_elem_value *ucontrol,
3899 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 unsigned int *cur_val)
3901{
3902 unsigned int idx;
3903
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003904 if (!imux->num_items)
3905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 idx = ucontrol->value.enumerated.item[0];
3907 if (idx >= imux->num_items)
3908 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003909 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003911 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3912 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 *cur_val = idx;
3914 return 1;
3915}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003916EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
3918
3919/*
3920 * Multi-channel / digital-out PCM helper functions
3921 */
3922
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003923/* setup SPDIF output stream */
3924static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3925 unsigned int stream_tag, unsigned int format)
3926{
3927 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02003928 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003929 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02003930 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3931 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003932 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003933 if (codec->slave_dig_outs) {
3934 hda_nid_t *d;
3935 for (d = codec->slave_dig_outs; *d; d++)
3936 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3937 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003938 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003939 /* turn on again (if needed) */
3940 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3941 set_dig_out_convert(codec, nid,
3942 codec->spdif_ctls & 0xff, -1);
3943}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003944
Takashi Iwai2f728532008-09-25 16:32:41 +02003945static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3946{
3947 snd_hda_codec_cleanup_stream(codec, nid);
3948 if (codec->slave_dig_outs) {
3949 hda_nid_t *d;
3950 for (d = codec->slave_dig_outs; *d; d++)
3951 snd_hda_codec_cleanup_stream(codec, *d);
3952 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003953}
3954
Takashi Iwaid5191e52009-11-16 14:58:17 +01003955/**
3956 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
3957 * @bus: HD-audio bus
3958 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003959void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3960{
3961 struct hda_codec *codec;
3962
3963 if (!bus)
3964 return;
3965 list_for_each_entry(codec, &bus->codec_list, list) {
3966#ifdef CONFIG_SND_HDA_POWER_SAVE
3967 if (!codec->power_on)
3968 continue;
3969#endif
3970 if (codec->patch_ops.reboot_notify)
3971 codec->patch_ops.reboot_notify(codec);
3972 }
3973}
Takashi Iwai8f217a22009-11-10 18:26:12 +01003974EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003975
Takashi Iwaid5191e52009-11-16 14:58:17 +01003976/**
3977 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003979int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3980 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
Ingo Molnar62932df2006-01-16 16:34:20 +01003982 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003983 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3984 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003985 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003987 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return 0;
3989}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003990EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Takashi Iwaid5191e52009-11-16 14:58:17 +01003992/**
3993 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3994 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003995int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3996 struct hda_multi_out *mout,
3997 unsigned int stream_tag,
3998 unsigned int format,
3999 struct snd_pcm_substream *substream)
4000{
4001 mutex_lock(&codec->spdif_mutex);
4002 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4003 mutex_unlock(&codec->spdif_mutex);
4004 return 0;
4005}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004006EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004007
Takashi Iwaid5191e52009-11-16 14:58:17 +01004008/**
4009 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4010 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004011int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4012 struct hda_multi_out *mout)
4013{
4014 mutex_lock(&codec->spdif_mutex);
4015 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4016 mutex_unlock(&codec->spdif_mutex);
4017 return 0;
4018}
4019EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4020
Takashi Iwaid5191e52009-11-16 14:58:17 +01004021/**
4022 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004024int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4025 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Ingo Molnar62932df2006-01-16 16:34:20 +01004027 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004029 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return 0;
4031}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004032EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Takashi Iwaid5191e52009-11-16 14:58:17 +01004034/**
4035 * snd_hda_multi_out_analog_open - open analog outputs
4036 *
4037 * Open analog outputs and set up the hw-constraints.
4038 * If the digital outputs can be opened as slave, open the digital
4039 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004041int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4042 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004043 struct snd_pcm_substream *substream,
4044 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
Takashi Iwai9a081602008-02-12 18:37:26 +01004046 struct snd_pcm_runtime *runtime = substream->runtime;
4047 runtime->hw.channels_max = mout->max_channels;
4048 if (mout->dig_out_nid) {
4049 if (!mout->analog_rates) {
4050 mout->analog_rates = hinfo->rates;
4051 mout->analog_formats = hinfo->formats;
4052 mout->analog_maxbps = hinfo->maxbps;
4053 } else {
4054 runtime->hw.rates = mout->analog_rates;
4055 runtime->hw.formats = mout->analog_formats;
4056 hinfo->maxbps = mout->analog_maxbps;
4057 }
4058 if (!mout->spdif_rates) {
4059 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4060 &mout->spdif_rates,
4061 &mout->spdif_formats,
4062 &mout->spdif_maxbps);
4063 }
4064 mutex_lock(&codec->spdif_mutex);
4065 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004066 if ((runtime->hw.rates & mout->spdif_rates) &&
4067 (runtime->hw.formats & mout->spdif_formats)) {
4068 runtime->hw.rates &= mout->spdif_rates;
4069 runtime->hw.formats &= mout->spdif_formats;
4070 if (mout->spdif_maxbps < hinfo->maxbps)
4071 hinfo->maxbps = mout->spdif_maxbps;
4072 } else {
4073 mout->share_spdif = 0;
4074 /* FIXME: need notify? */
4075 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004076 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004077 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4080 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4081}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004082EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Takashi Iwaid5191e52009-11-16 14:58:17 +01004084/**
4085 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4086 *
4087 * Set up the i/o for analog out.
4088 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004090int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4091 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 unsigned int stream_tag,
4093 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004094 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095{
4096 hda_nid_t *nids = mout->dac_nids;
4097 int chs = substream->runtime->channels;
4098 int i;
4099
Ingo Molnar62932df2006-01-16 16:34:20 +01004100 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004101 if (mout->dig_out_nid && mout->share_spdif &&
4102 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004104 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4105 format) &&
4106 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004108 setup_dig_out_stream(codec, mout->dig_out_nid,
4109 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 } else {
4111 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004112 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 }
4114 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004115 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
4117 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004118 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4119 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004120 if (!mout->no_share_stream &&
4121 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004123 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4124 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004125 /* extra outputs copied from front */
4126 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004127 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004128 snd_hda_codec_setup_stream(codec,
4129 mout->extra_out_nid[i],
4130 stream_tag, 0, format);
4131
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 /* surrounds */
4133 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004134 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004135 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4136 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004137 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004138 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4139 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 }
4141 return 0;
4142}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004143EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
Takashi Iwaid5191e52009-11-16 14:58:17 +01004145/**
4146 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004148int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4149 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
4151 hda_nid_t *nids = mout->dac_nids;
4152 int i;
4153
4154 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004155 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004157 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004158 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4159 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004160 snd_hda_codec_cleanup_stream(codec,
4161 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004162 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004164 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 mout->dig_out_used = 0;
4166 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004167 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 return 0;
4169}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004170EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004172/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004173 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004174 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004175
Takashi Iwai12f288b2007-08-02 15:51:59 +02004176static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004177{
4178 for (; *list; list++)
4179 if (*list == nid)
4180 return 1;
4181 return 0;
4182}
4183
Steve Longerbeam81937d32007-05-08 15:33:03 +02004184
4185/*
4186 * Sort an associated group of pins according to their sequence numbers.
4187 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004188static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004189 int num_pins)
4190{
4191 int i, j;
4192 short seq;
4193 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004194
Steve Longerbeam81937d32007-05-08 15:33:03 +02004195 for (i = 0; i < num_pins; i++) {
4196 for (j = i + 1; j < num_pins; j++) {
4197 if (sequences[i] > sequences[j]) {
4198 seq = sequences[i];
4199 sequences[i] = sequences[j];
4200 sequences[j] = seq;
4201 nid = pins[i];
4202 pins[i] = pins[j];
4203 pins[j] = nid;
4204 }
4205 }
4206 }
4207}
4208
4209
Takashi Iwai82bc9552006-03-21 11:24:42 +01004210/*
4211 * Parse all pin widgets and store the useful pin nids to cfg
4212 *
4213 * The number of line-outs or any primary output is stored in line_outs,
4214 * and the corresponding output pins are assigned to line_out_pins[],
4215 * in the order of front, rear, CLFE, side, ...
4216 *
4217 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004218 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004219 * is detected, one of speaker of HP pins is assigned as the primary
4220 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4221 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004222 *
Takashi Iwai82bc9552006-03-21 11:24:42 +01004223 * The analog input pins are assigned to input_pins array.
4224 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4225 * respectively.
4226 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004227int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4228 struct auto_pin_cfg *cfg,
4229 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004230{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004231 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004232 short seq, assoc_line_out, assoc_speaker;
4233 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4234 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004235 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004236
4237 memset(cfg, 0, sizeof(*cfg));
4238
Steve Longerbeam81937d32007-05-08 15:33:03 +02004239 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4240 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004241 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004242 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004243
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004244 end_nid = codec->start_nid + codec->num_nodes;
4245 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004246 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004247 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004248 unsigned int def_conf;
4249 short assoc, loc;
4250
4251 /* read all default configuration for pin complex */
4252 if (wid_type != AC_WID_PIN)
4253 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004254 /* ignore the given nids (e.g. pc-beep returns error) */
4255 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4256 continue;
4257
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004258 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004259 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4260 continue;
4261 loc = get_defcfg_location(def_conf);
4262 switch (get_defcfg_device(def_conf)) {
4263 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004264 seq = get_defcfg_sequence(def_conf);
4265 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004266
4267 if (!(wid_caps & AC_WCAP_STEREO))
4268 if (!cfg->mono_out_pin)
4269 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004270 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004271 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004272 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004273 assoc_line_out = assoc;
4274 else if (assoc_line_out != assoc)
4275 continue;
4276 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4277 continue;
4278 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004279 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004280 cfg->line_outs++;
4281 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004282 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004283 seq = get_defcfg_sequence(def_conf);
4284 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004285 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004286 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004287 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004288 assoc_speaker = assoc;
4289 else if (assoc_speaker != assoc)
4290 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004291 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4292 continue;
4293 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004294 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004295 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004296 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004297 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004298 seq = get_defcfg_sequence(def_conf);
4299 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004300 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4301 continue;
4302 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004303 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004304 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004305 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004306 case AC_JACK_MIC_IN: {
4307 int preferred, alt;
Kailang Yang6ff86a32010-03-19 11:14:36 +01004308 if (loc == AC_JACK_LOC_FRONT ||
4309 (loc & 0x30) == AC_JACK_LOC_INTERNAL) {
Takashi Iwai314634b2006-09-21 11:56:18 +02004310 preferred = AUTO_PIN_FRONT_MIC;
4311 alt = AUTO_PIN_MIC;
4312 } else {
4313 preferred = AUTO_PIN_MIC;
4314 alt = AUTO_PIN_FRONT_MIC;
4315 }
4316 if (!cfg->input_pins[preferred])
4317 cfg->input_pins[preferred] = nid;
4318 else if (!cfg->input_pins[alt])
4319 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004320 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004321 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004322 case AC_JACK_LINE_IN:
4323 if (loc == AC_JACK_LOC_FRONT)
4324 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4325 else
4326 cfg->input_pins[AUTO_PIN_LINE] = nid;
4327 break;
4328 case AC_JACK_CD:
4329 cfg->input_pins[AUTO_PIN_CD] = nid;
4330 break;
4331 case AC_JACK_AUX:
4332 cfg->input_pins[AUTO_PIN_AUX] = nid;
4333 break;
4334 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004335 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004336 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4337 continue;
4338 cfg->dig_out_pins[cfg->dig_outs] = nid;
4339 cfg->dig_out_type[cfg->dig_outs] =
4340 (loc == AC_JACK_LOC_HDMI) ?
4341 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4342 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004343 break;
4344 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004345 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004346 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004347 if (loc == AC_JACK_LOC_HDMI)
4348 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4349 else
4350 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004351 break;
4352 }
4353 }
4354
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004355 /* FIX-UP:
4356 * If no line-out is defined but multiple HPs are found,
4357 * some of them might be the real line-outs.
4358 */
4359 if (!cfg->line_outs && cfg->hp_outs > 1) {
4360 int i = 0;
4361 while (i < cfg->hp_outs) {
4362 /* The real HPs should have the sequence 0x0f */
4363 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4364 i++;
4365 continue;
4366 }
4367 /* Move it to the line-out table */
4368 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4369 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4370 cfg->line_outs++;
4371 cfg->hp_outs--;
4372 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4373 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4374 memmove(sequences_hp + i - 1, sequences_hp + i,
4375 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4376 }
4377 }
4378
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004379 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004380 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4381 cfg->line_outs);
4382 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4383 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004384 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4385 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004386
Takashi Iwaif889fa92007-10-31 15:49:32 +01004387 /* if we have only one mic, make it AUTO_PIN_MIC */
4388 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4389 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4390 cfg->input_pins[AUTO_PIN_MIC] =
4391 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4392 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4393 }
4394 /* ditto for line-in */
4395 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4396 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4397 cfg->input_pins[AUTO_PIN_LINE] =
4398 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4399 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4400 }
4401
Steve Longerbeam81937d32007-05-08 15:33:03 +02004402 /*
4403 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4404 * as a primary output
4405 */
4406 if (!cfg->line_outs) {
4407 if (cfg->speaker_outs) {
4408 cfg->line_outs = cfg->speaker_outs;
4409 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4410 sizeof(cfg->speaker_pins));
4411 cfg->speaker_outs = 0;
4412 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4413 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4414 } else if (cfg->hp_outs) {
4415 cfg->line_outs = cfg->hp_outs;
4416 memcpy(cfg->line_out_pins, cfg->hp_pins,
4417 sizeof(cfg->hp_pins));
4418 cfg->hp_outs = 0;
4419 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4420 cfg->line_out_type = AUTO_PIN_HP_OUT;
4421 }
4422 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004423
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004424 /* Reorder the surround channels
4425 * ALSA sequence is front/surr/clfe/side
4426 * HDA sequence is:
4427 * 4-ch: front/surr => OK as it is
4428 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004429 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004430 */
4431 switch (cfg->line_outs) {
4432 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004433 case 4:
4434 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004435 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004436 cfg->line_out_pins[2] = nid;
4437 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004438 }
4439
Takashi Iwai82bc9552006-03-21 11:24:42 +01004440 /*
4441 * debug prints of the parsed results
4442 */
4443 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4444 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4445 cfg->line_out_pins[2], cfg->line_out_pins[3],
4446 cfg->line_out_pins[4]);
4447 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4448 cfg->speaker_outs, cfg->speaker_pins[0],
4449 cfg->speaker_pins[1], cfg->speaker_pins[2],
4450 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004451 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4452 cfg->hp_outs, cfg->hp_pins[0],
4453 cfg->hp_pins[1], cfg->hp_pins[2],
4454 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004455 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004456 if (cfg->dig_outs)
4457 snd_printd(" dig-out=0x%x/0x%x\n",
4458 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004459 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4460 " cd=0x%x, aux=0x%x\n",
4461 cfg->input_pins[AUTO_PIN_MIC],
4462 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4463 cfg->input_pins[AUTO_PIN_LINE],
4464 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4465 cfg->input_pins[AUTO_PIN_CD],
4466 cfg->input_pins[AUTO_PIN_AUX]);
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004467 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004468 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004469
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004470 return 0;
4471}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004472EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004473
Takashi Iwai4a471b72005-12-07 13:56:29 +01004474/* labels for input pins */
4475const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4476 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4477};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004478EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01004479
4480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481#ifdef CONFIG_PM
4482/*
4483 * power management
4484 */
4485
4486/**
4487 * snd_hda_suspend - suspend the codecs
4488 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 *
4490 * Returns 0 if successful.
4491 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004492int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004494 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Takashi Iwai0ba21762007-04-16 11:29:14 +02004496 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004497#ifdef CONFIG_SND_HDA_POWER_SAVE
4498 if (!codec->power_on)
4499 continue;
4500#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004501 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 }
4503 return 0;
4504}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004505EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
4507/**
4508 * snd_hda_resume - resume the codecs
4509 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 *
4511 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004512 *
4513 * This fucntion is defined only when POWER_SAVE isn't set.
4514 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 */
4516int snd_hda_resume(struct hda_bus *bus)
4517{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004518 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Takashi Iwai0ba21762007-04-16 11:29:14 +02004520 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004521 if (snd_hda_codec_needs_resume(codec))
4522 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 return 0;
4525}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004526EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004527#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004528
4529/*
4530 * generic arrays
4531 */
4532
Takashi Iwaid5191e52009-11-16 14:58:17 +01004533/**
4534 * snd_array_new - get a new element from the given array
4535 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004536 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004537 * Get a new element from the given array. If it exceeds the
4538 * pre-allocated array size, re-allocate the array.
4539 *
4540 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004541 */
4542void *snd_array_new(struct snd_array *array)
4543{
4544 if (array->used >= array->alloced) {
4545 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004546 void *nlist;
4547 if (snd_BUG_ON(num >= 4096))
4548 return NULL;
4549 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004550 if (!nlist)
4551 return NULL;
4552 if (array->list) {
4553 memcpy(nlist, array->list,
4554 array->elem_size * array->alloced);
4555 kfree(array->list);
4556 }
4557 array->list = nlist;
4558 array->alloced = num;
4559 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004560 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004561}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004562EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004563
Takashi Iwaid5191e52009-11-16 14:58:17 +01004564/**
4565 * snd_array_free - free the given array elements
4566 * @array: the array object
4567 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004568void snd_array_free(struct snd_array *array)
4569{
4570 kfree(array->list);
4571 array->used = 0;
4572 array->alloced = 0;
4573 array->list = NULL;
4574}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004575EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004576
Takashi Iwaid5191e52009-11-16 14:58:17 +01004577/**
4578 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4579 * @pcm: PCM caps bits
4580 * @buf: the string buffer to write
4581 * @buflen: the max buffer length
4582 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004583 * used by hda_proc.c and hda_eld.c
4584 */
4585void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4586{
4587 static unsigned int rates[] = {
4588 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4589 96000, 176400, 192000, 384000
4590 };
4591 int i, j;
4592
4593 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4594 if (pcm & (1 << i))
4595 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4596
4597 buf[j] = '\0'; /* necessary when j == 0 */
4598}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004599EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004600
Takashi Iwaid5191e52009-11-16 14:58:17 +01004601/**
4602 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4603 * @pcm: PCM caps bits
4604 * @buf: the string buffer to write
4605 * @buflen: the max buffer length
4606 *
4607 * used by hda_proc.c and hda_eld.c
4608 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004609void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4610{
4611 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4612 int i, j;
4613
4614 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4615 if (pcm & (AC_SUPPCM_BITS_8 << i))
4616 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4617
4618 buf[j] = '\0'; /* necessary when j == 0 */
4619}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004620EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004621
4622MODULE_DESCRIPTION("HDA codec core");
4623MODULE_LICENSE("GPL");