blob: d02ea8926e7e7913d00a7a3b0c7b1c722d82d55f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
32#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020033#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020034#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * vendor / preset table
38 */
39
40struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
43};
44
45/* codec vendor labels */
46static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010047 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020048 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020049 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010050 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010051 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010052 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020053 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010054 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020055 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010056 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020057 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020059 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010060 { 0x17e8, "Chrontel" },
61 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000062 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010064 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020065 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 {} /* terminator */
67};
68
Takashi Iwai1289e9e2008-11-27 15:47:11 +010069static DEFINE_MUTEX(preset_mutex);
70static LIST_HEAD(hda_preset_tables);
71
72int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
73{
74 mutex_lock(&preset_mutex);
75 list_add_tail(&preset->list, &hda_preset_tables);
76 mutex_unlock(&preset_mutex);
77 return 0;
78}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010079EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010080
81int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
82{
83 mutex_lock(&preset_mutex);
84 list_del(&preset->list);
85 mutex_unlock(&preset_mutex);
86 return 0;
87}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010088EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwaicb53c622007-08-10 17:21:45 +020090#ifdef CONFIG_SND_HDA_POWER_SAVE
91static void hda_power_work(struct work_struct *work);
92static void hda_keep_power_on(struct hda_codec *codec);
93#else
94static inline void hda_keep_power_on(struct hda_codec *codec) {}
95#endif
96
Takashi Iwaid5191e52009-11-16 14:58:17 +010097/**
98 * snd_hda_get_jack_location - Give a location string of the jack
99 * @cfg: pin default config value
100 *
101 * Parse the pin default config value and returns the string of the
102 * jack location, e.g. "Rear", "Front", etc.
103 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400104const char *snd_hda_get_jack_location(u32 cfg)
105{
106 static char *bases[7] = {
107 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
108 };
109 static unsigned char specials_idx[] = {
110 0x07, 0x08,
111 0x17, 0x18, 0x19,
112 0x37, 0x38
113 };
114 static char *specials[] = {
115 "Rear Panel", "Drive Bar",
116 "Riser", "HDMI", "ATAPI",
117 "Mobile-In", "Mobile-Out"
118 };
119 int i;
120 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
121 if ((cfg & 0x0f) < 7)
122 return bases[cfg & 0x0f];
123 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
124 if (cfg == specials_idx[i])
125 return specials[i];
126 }
127 return "UNKNOWN";
128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100129EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400130
Takashi Iwaid5191e52009-11-16 14:58:17 +0100131/**
132 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
133 * @cfg: pin default config value
134 *
135 * Parse the pin default config value and returns the string of the
136 * jack connectivity, i.e. external or internal connection.
137 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400138const char *snd_hda_get_jack_connectivity(u32 cfg)
139{
140 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
141
142 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100144EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400145
Takashi Iwaid5191e52009-11-16 14:58:17 +0100146/**
147 * snd_hda_get_jack_type - Give a type string of the jack
148 * @cfg: pin default config value
149 *
150 * Parse the pin default config value and returns the string of the
151 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
152 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400153const char *snd_hda_get_jack_type(u32 cfg)
154{
155 static char *jack_types[16] = {
156 "Line Out", "Speaker", "HP Out", "CD",
157 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
158 "Line In", "Aux", "Mic", "Telephony",
159 "SPDIF In", "Digitial In", "Reserved", "Other"
160 };
161
162 return jack_types[(cfg & AC_DEFCFG_DEVICE)
163 >> AC_DEFCFG_DEVICE_SHIFT];
164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100165EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400166
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100167/*
168 * Compose a 32bit command word to be sent to the HD-audio controller
169 */
170static inline unsigned int
171make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
172 unsigned int verb, unsigned int parm)
173{
174 u32 val;
175
Takashi Iwai82e1b802009-07-17 12:47:34 +0200176 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
177 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800178 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
179 codec->addr, direct, nid, verb, parm);
180 return ~0;
181 }
182
183 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184 val |= (u32)direct << 27;
185 val |= (u32)nid << 20;
186 val |= verb << 8;
187 val |= parm;
188 return val;
189}
190
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200191/*
192 * Send and receive a verb
193 */
194static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
195 unsigned int *res)
196{
197 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200198 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200199
Wu Fengguang6430aee2009-07-17 16:49:19 +0800200 if (cmd == ~0)
201 return -1;
202
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200203 if (res)
204 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200205 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206 snd_hda_power_up(codec);
207 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200209 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800210 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200211 mutex_unlock(&bus->cmd_mutex);
212 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200213 if (res && *res == -1 && bus->rirb_error) {
214 if (bus->response_reset) {
215 snd_printd("hda_codec: resetting BUS due to "
216 "fatal communication error\n");
217 bus->ops.bus_reset(bus);
218 }
219 goto again;
220 }
221 /* clear reset-flag when the communication gets recovered */
222 if (!err)
223 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224 return err;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/**
228 * snd_hda_codec_read - send a command and get the response
229 * @codec: the HDA codec
230 * @nid: NID to send the command
231 * @direct: direct flag
232 * @verb: the verb to send
233 * @parm: the parameter for the verb
234 *
235 * Send a single command and read the corresponding response.
236 *
237 * Returns the obtained response value, or -1 for an error.
238 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200239unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
240 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned int verb, unsigned int parm)
242{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200243 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
244 unsigned int res;
245 codec_exec_verb(codec, cmd, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return res;
247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100248EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/**
251 * snd_hda_codec_write - send a single command without waiting for response
252 * @codec: the HDA codec
253 * @nid: NID to send the command
254 * @direct: direct flag
255 * @verb: the verb to send
256 * @parm: the parameter for the verb
257 *
258 * Send a single command without waiting for response.
259 *
260 * Returns 0 if successful, or a negative error code.
261 */
262int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
263 unsigned int verb, unsigned int parm)
264{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200265 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100266 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200267 return codec_exec_verb(codec, cmd,
268 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100270EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/**
273 * snd_hda_sequence_write - sequence writes
274 * @codec: the HDA codec
275 * @seq: VERB array to send
276 *
277 * Send the commands sequentially from the given array.
278 * The array must be terminated with NID=0.
279 */
280void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
281{
282 for (; seq->nid; seq++)
283 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100285EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/**
288 * snd_hda_get_sub_nodes - get the range of sub nodes
289 * @codec: the HDA codec
290 * @nid: NID to parse
291 * @start_id: the pointer to store the start NID
292 *
293 * Parse the NID and store the start NID of its sub-nodes.
294 * Returns the number of sub-nodes.
295 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200296int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
297 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned int parm;
300
301 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200302 if (parm == -1)
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *start_id = (parm >> 16) & 0x7fff;
305 return (int)(parm & 0x7fff);
306}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100307EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
310 * snd_hda_get_connections - get connection list
311 * @codec: the HDA codec
312 * @nid: NID to parse
313 * @conn_list: connection list array
314 * @max_conns: max. number of connections to store
315 *
316 * Parses the connection list of the given widget and stores the list
317 * of NIDs.
318 *
319 * Returns the number of connections, or a negative error code.
320 */
321int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
322 hda_nid_t *conn_list, int max_conns)
323{
324 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100325 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200327 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100328 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Takashi Iwaida3cec32008-08-08 17:12:14 +0200330 if (snd_BUG_ON(!conn_list || max_conns <= 0))
331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200333 wcaps = get_wcaps(codec, nid);
334 if (!(wcaps & AC_WCAP_CONN_LIST) &&
335 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200336 snd_printk(KERN_WARNING "hda_codec: "
337 "connection list not available for 0x%x\n", nid);
338 return -EINVAL;
339 }
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
342 if (parm & AC_CLIST_LONG) {
343 /* long form */
344 shift = 16;
345 num_elems = 2;
346 } else {
347 /* short form */
348 shift = 8;
349 num_elems = 4;
350 }
351 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mask = (1 << (shift-1)) - 1;
353
Takashi Iwai0ba21762007-04-16 11:29:14 +0200354 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0; /* no connection */
356
357 if (conn_len == 1) {
358 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200359 parm = snd_hda_codec_read(codec, nid, 0,
360 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200361 if (parm == -1 && codec->bus->rirb_error)
362 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 conn_list[0] = parm & mask;
364 return 1;
365 }
366
367 /* multi connection */
368 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100369 prev_nid = 0;
370 for (i = 0; i < conn_len; i++) {
371 int range_val;
372 hda_nid_t val, n;
373
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200374 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100375 parm = snd_hda_codec_read(codec, nid, 0,
376 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200377 if (parm == -1 && codec->bus->rirb_error)
378 return -EIO;
379 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200380 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100381 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200382 if (val == 0) {
383 snd_printk(KERN_WARNING "hda_codec: "
384 "invalid CONNECT_LIST verb %x[%i]:%x\n",
385 nid, i, parm);
386 return 0;
387 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100388 parm >>= shift;
389 if (range_val) {
390 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200391 if (!prev_nid || prev_nid >= val) {
392 snd_printk(KERN_WARNING "hda_codec: "
393 "invalid dep_range_val %x:%x\n",
394 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100395 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100397 for (n = prev_nid + 1; n <= val; n++) {
398 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200399 snd_printk(KERN_ERR
400 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100401 return -EINVAL;
402 }
403 conn_list[conns++] = n;
404 }
405 } else {
406 if (conns >= max_conns) {
407 snd_printk(KERN_ERR "Too many connections\n");
408 return -EINVAL;
409 }
410 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100412 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 return conns;
415}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100416EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418
419/**
420 * snd_hda_queue_unsol_event - add an unsolicited event to queue
421 * @bus: the BUS
422 * @res: unsolicited event (lower 32bit of RIRB entry)
423 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
424 *
425 * Adds the given event to the queue. The events are processed in
426 * the workqueue asynchronously. Call this function in the interrupt
427 * hanlder when RIRB receives an unsolicited event.
428 *
429 * Returns 0 if successful, or a negative error code.
430 */
431int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
432{
433 struct hda_bus_unsolicited *unsol;
434 unsigned int wp;
435
Takashi Iwai0ba21762007-04-16 11:29:14 +0200436 unsol = bus->unsol;
437 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439
440 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
441 unsol->wp = wp;
442
443 wp <<= 1;
444 unsol->queue[wp] = res;
445 unsol->queue[wp + 1] = res_ex;
446
Takashi Iwai6acaed32009-01-12 10:09:24 +0100447 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return 0;
450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100451EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800454 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
David Howellsc4028952006-11-22 14:57:56 +0000456static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
David Howellsc4028952006-11-22 14:57:56 +0000458 struct hda_bus_unsolicited *unsol =
459 container_of(work, struct hda_bus_unsolicited, work);
460 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct hda_codec *codec;
462 unsigned int rp, caddr, res;
463
464 while (unsol->rp != unsol->wp) {
465 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
466 unsol->rp = rp;
467 rp <<= 1;
468 res = unsol->queue[rp];
469 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200470 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 continue;
472 codec = bus->caddr_tbl[caddr & 0x0f];
473 if (codec && codec->patch_ops.unsol_event)
474 codec->patch_ops.unsol_event(codec, res);
475 }
476}
477
478/*
479 * initialize unsolicited queue
480 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200481static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct hda_bus_unsolicited *unsol;
484
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100485 if (bus->unsol) /* already initialized */
486 return 0;
487
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200488 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200489 if (!unsol) {
490 snd_printk(KERN_ERR "hda_codec: "
491 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -ENOMEM;
493 }
David Howellsc4028952006-11-22 14:57:56 +0000494 INIT_WORK(&unsol->work, process_unsol_events);
495 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 bus->unsol = unsol;
497 return 0;
498}
499
500/*
501 * destructor
502 */
503static void snd_hda_codec_free(struct hda_codec *codec);
504
505static int snd_hda_bus_free(struct hda_bus *bus)
506{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200507 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Takashi Iwai0ba21762007-04-16 11:29:14 +0200509 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100511 if (bus->workq)
512 flush_workqueue(bus->workq);
513 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200515 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 snd_hda_codec_free(codec);
517 }
518 if (bus->ops.private_free)
519 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100520 if (bus->workq)
521 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 kfree(bus);
523 return 0;
524}
525
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100526static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100529 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return snd_hda_bus_free(bus);
531}
532
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200533#ifdef CONFIG_SND_HDA_HWDEP
534static int snd_hda_bus_dev_register(struct snd_device *device)
535{
536 struct hda_bus *bus = device->device_data;
537 struct hda_codec *codec;
538 list_for_each_entry(codec, &bus->codec_list, list) {
539 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100540 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200541 }
542 return 0;
543}
544#else
545#define snd_hda_bus_dev_register NULL
546#endif
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/**
549 * snd_hda_bus_new - create a HDA bus
550 * @card: the card entry
551 * @temp: the template for hda_bus information
552 * @busp: the pointer to store the created bus instance
553 *
554 * Returns 0 if successful, or a negative error code.
555 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100556int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200557 const struct hda_bus_template *temp,
558 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct hda_bus *bus;
561 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100562 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200563 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .dev_free = snd_hda_bus_dev_free,
565 };
566
Takashi Iwaida3cec32008-08-08 17:12:14 +0200567 if (snd_BUG_ON(!temp))
568 return -EINVAL;
569 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (busp)
573 *busp = NULL;
574
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200575 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (bus == NULL) {
577 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
578 return -ENOMEM;
579 }
580
581 bus->card = card;
582 bus->private_data = temp->private_data;
583 bus->pci = temp->pci;
584 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100585 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 bus->ops = temp->ops;
587
Ingo Molnar62932df2006-01-16 16:34:20 +0100588 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 INIT_LIST_HEAD(&bus->codec_list);
590
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100591 snprintf(bus->workq_name, sizeof(bus->workq_name),
592 "hd-audio%d", card->number);
593 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100594 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100595 snd_printk(KERN_ERR "cannot create workqueue %s\n",
596 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100597 kfree(bus);
598 return -ENOMEM;
599 }
600
Takashi Iwai0ba21762007-04-16 11:29:14 +0200601 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
602 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 snd_hda_bus_free(bus);
604 return err;
605 }
606 if (busp)
607 *busp = bus;
608 return 0;
609}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100610EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Takashi Iwai82467612007-07-27 19:15:54 +0200612#ifdef CONFIG_SND_HDA_GENERIC
613#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200614 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200615#else
616#define is_generic_config(codec) 0
617#endif
618
Takashi Iwai645f10c2008-11-28 15:07:37 +0100619#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100620#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
621#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100622#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100623#endif
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*
626 * find a matching codec preset
627 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200628static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200629find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100631 struct hda_codec_preset_list *tbl;
632 const struct hda_codec_preset *preset;
633 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Takashi Iwai82467612007-07-27 19:15:54 +0200635 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100636 return NULL; /* use the generic parser */
637
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100638 again:
639 mutex_lock(&preset_mutex);
640 list_for_each_entry(tbl, &hda_preset_tables, list) {
641 if (!try_module_get(tbl->owner)) {
642 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
643 continue;
644 }
645 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100647 if (preset->afg && preset->afg != codec->afg)
648 continue;
649 if (preset->mfg && preset->mfg != codec->mfg)
650 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200651 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200653 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200654 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100655 preset->rev == codec->revision_id)) {
656 mutex_unlock(&preset_mutex);
657 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100661 module_put(tbl->owner);
662 }
663 mutex_unlock(&preset_mutex);
664
665 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
666 char name[32];
667 if (!mod_requested)
668 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
669 codec->vendor_id);
670 else
671 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
672 (codec->vendor_id >> 16) & 0xffff);
673 request_module(name);
674 mod_requested++;
675 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 return NULL;
678}
679
680/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200681 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200683static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 const struct hda_vendor_id *c;
686 const char *vendor = NULL;
687 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200688 char tmp[16];
689
690 if (codec->vendor_name)
691 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 for (c = hda_vendor_ids; c->id; c++) {
694 if (c->id == vendor_id) {
695 vendor = c->name;
696 break;
697 }
698 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200699 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 sprintf(tmp, "Generic %04x", vendor_id);
701 vendor = tmp;
702 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200703 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
704 if (!codec->vendor_name)
705 return -ENOMEM;
706
707 get_chip_name:
708 if (codec->chip_name)
709 return 0;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200712 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
713 else {
714 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
715 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
716 }
717 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200718 return -ENOMEM;
719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200723 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100725static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200727 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 hda_nid_t nid;
729
730 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
731 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200732 function_id = snd_hda_param_read(codec, nid,
Pascal de Bruijn234b4342009-03-23 11:15:59 +0100733 AC_PAR_FUNCTION_TYPE) & 0xff;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200734 switch (function_id) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200735 case AC_GRP_AUDIO_FUNCTION:
736 codec->afg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200737 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200738 break;
739 case AC_GRP_MODEM_FUNCTION:
740 codec->mfg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200741 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200742 break;
743 default:
744 break;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100750 * read widget caps for each widget and store in cache
751 */
752static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
753{
754 int i;
755 hda_nid_t nid;
756
757 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
758 &codec->start_nid);
759 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200760 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100761 return -ENOMEM;
762 nid = codec->start_nid;
763 for (i = 0; i < codec->num_nodes; i++, nid++)
764 codec->wcaps[i] = snd_hda_param_read(codec, nid,
765 AC_PAR_AUDIO_WIDGET_CAP);
766 return 0;
767}
768
Takashi Iwai3be14142009-02-20 14:11:16 +0100769/* read all pin default configurations and save codec->init_pins */
770static int read_pin_defaults(struct hda_codec *codec)
771{
772 int i;
773 hda_nid_t nid = codec->start_nid;
774
775 for (i = 0; i < codec->num_nodes; i++, nid++) {
776 struct hda_pincfg *pin;
777 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200778 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100779 if (wid_type != AC_WID_PIN)
780 continue;
781 pin = snd_array_new(&codec->init_pins);
782 if (!pin)
783 return -ENOMEM;
784 pin->nid = nid;
785 pin->cfg = snd_hda_codec_read(codec, nid, 0,
786 AC_VERB_GET_CONFIG_DEFAULT, 0);
787 }
788 return 0;
789}
790
791/* look up the given pin config list and return the item matching with NID */
792static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
793 struct snd_array *array,
794 hda_nid_t nid)
795{
796 int i;
797 for (i = 0; i < array->used; i++) {
798 struct hda_pincfg *pin = snd_array_elem(array, i);
799 if (pin->nid == nid)
800 return pin;
801 }
802 return NULL;
803}
804
805/* write a config value for the given NID */
806static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
807 unsigned int cfg)
808{
809 int i;
810 for (i = 0; i < 4; i++) {
811 snd_hda_codec_write(codec, nid, 0,
812 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
813 cfg & 0xff);
814 cfg >>= 8;
815 }
816}
817
818/* set the current pin config value for the given NID.
819 * the value is cached, and read via snd_hda_codec_get_pincfg()
820 */
821int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
822 hda_nid_t nid, unsigned int cfg)
823{
824 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100825 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100826
Takashi Iwaib82855a2009-12-27 11:24:56 +0100827 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
828 return -EINVAL;
829
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100830 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100831 pin = look_up_pincfg(codec, list, nid);
832 if (!pin) {
833 pin = snd_array_new(list);
834 if (!pin)
835 return -ENOMEM;
836 pin->nid = nid;
837 }
838 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100839
840 /* change only when needed; e.g. if the pincfg is already present
841 * in user_pins[], don't write it
842 */
843 cfg = snd_hda_codec_get_pincfg(codec, nid);
844 if (oldcfg != cfg)
845 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100846 return 0;
847}
848
Takashi Iwaid5191e52009-11-16 14:58:17 +0100849/**
850 * snd_hda_codec_set_pincfg - Override a pin default configuration
851 * @codec: the HDA codec
852 * @nid: NID to set the pin config
853 * @cfg: the pin default config value
854 *
855 * Override a pin default configuration value in the cache.
856 * This value can be read by snd_hda_codec_get_pincfg() in a higher
857 * priority than the real hardware value.
858 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100859int snd_hda_codec_set_pincfg(struct hda_codec *codec,
860 hda_nid_t nid, unsigned int cfg)
861{
Takashi Iwai346ff702009-02-23 09:42:57 +0100862 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100863}
864EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
865
Takashi Iwaid5191e52009-11-16 14:58:17 +0100866/**
867 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
868 * @codec: the HDA codec
869 * @nid: NID to get the pin config
870 *
871 * Get the current pin config value of the given pin NID.
872 * If the pincfg value is cached or overridden via sysfs or driver,
873 * returns the cached value.
874 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100875unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
876{
877 struct hda_pincfg *pin;
878
Takashi Iwai3be14142009-02-20 14:11:16 +0100879#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100880 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100881 if (pin)
882 return pin->cfg;
883#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100884 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
885 if (pin)
886 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100887 pin = look_up_pincfg(codec, &codec->init_pins, nid);
888 if (pin)
889 return pin->cfg;
890 return 0;
891}
892EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
893
894/* restore all current pin configs */
895static void restore_pincfgs(struct hda_codec *codec)
896{
897 int i;
898 for (i = 0; i < codec->init_pins.used; i++) {
899 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
900 set_pincfg(codec, pin->nid,
901 snd_hda_codec_get_pincfg(codec, pin->nid));
902 }
903}
Takashi Iwai54d17402005-11-21 16:33:22 +0100904
Takashi Iwai92ee6162009-12-27 11:18:59 +0100905/**
906 * snd_hda_shutup_pins - Shut up all pins
907 * @codec: the HDA codec
908 *
909 * Clear all pin controls to shup up before suspend for avoiding click noise.
910 * The controls aren't cached so that they can be resumed properly.
911 */
912void snd_hda_shutup_pins(struct hda_codec *codec)
913{
914 int i;
915 for (i = 0; i < codec->init_pins.used; i++) {
916 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
917 /* use read here for syncing after issuing each verb */
918 snd_hda_codec_read(codec, pin->nid, 0,
919 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
920 }
921}
922EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
923
Takashi Iwai01751f52007-08-10 16:59:39 +0200924static void init_hda_cache(struct hda_cache_rec *cache,
925 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200926static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200927
Takashi Iwai3be14142009-02-20 14:11:16 +0100928/* restore the initial pin cfgs and release all pincfg lists */
929static void restore_init_pincfgs(struct hda_codec *codec)
930{
Takashi Iwai346ff702009-02-23 09:42:57 +0100931 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100932 * so that only the values in init_pins are restored
933 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100934 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100935#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100936 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100937#endif
938 restore_pincfgs(codec);
939 snd_array_free(&codec->init_pins);
940}
941
Takashi Iwai54d17402005-11-21 16:33:22 +0100942/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * codec destructor
944 */
945static void snd_hda_codec_free(struct hda_codec *codec)
946{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200947 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return;
Takashi Iwai3be14142009-02-20 14:11:16 +0100949 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200950#ifdef CONFIG_SND_HDA_POWER_SAVE
951 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100952 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200955 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100956 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 codec->bus->caddr_tbl[codec->addr] = NULL;
958 if (codec->patch_ops.free)
959 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100960 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200961 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200962 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200963 kfree(codec->vendor_name);
964 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200965 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100966 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 kfree(codec);
968}
969
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100970static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
971 unsigned int power_state);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/**
974 * snd_hda_codec_new - create a HDA codec
975 * @bus: the bus to assign
976 * @codec_addr: the codec address
977 * @codecp: the pointer to store the generated codec
978 *
979 * Returns 0 if successful, or a negative error code.
980 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100981int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
Takashi Iwaia1e21c92009-06-17 09:33:52 +0200982 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200985 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 int err;
987
Takashi Iwaida3cec32008-08-08 17:12:14 +0200988 if (snd_BUG_ON(!bus))
989 return -EINVAL;
990 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
991 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200994 snd_printk(KERN_ERR "hda_codec: "
995 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return -EBUSY;
997 }
998
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200999 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (codec == NULL) {
1001 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1002 return -ENOMEM;
1003 }
1004
1005 codec->bus = bus;
1006 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001007 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001008 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001009 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001010 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001011 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1012 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001013 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001014 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001015 if (codec->bus->modelname) {
1016 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1017 if (!codec->modelname) {
1018 snd_hda_codec_free(codec);
1019 return -ENODEV;
1020 }
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Takashi Iwaicb53c622007-08-10 17:21:45 +02001023#ifdef CONFIG_SND_HDA_POWER_SAVE
1024 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1025 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1026 * the caller has to power down appropriatley after initialization
1027 * phase.
1028 */
1029 hda_keep_power_on(codec);
1030#endif
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 list_add_tail(&codec->list, &bus->codec_list);
1033 bus->caddr_tbl[codec_addr] = codec;
1034
Takashi Iwai0ba21762007-04-16 11:29:14 +02001035 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1036 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001037 if (codec->vendor_id == -1)
1038 /* read again, hopefully the access method was corrected
1039 * in the last read...
1040 */
1041 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1042 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001043 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1044 AC_PAR_SUBSYSTEM_ID);
1045 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1046 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001048 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001049 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001050 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001051 err = -ENODEV;
1052 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Takashi Iwai3be14142009-02-20 14:11:16 +01001055 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1056 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001057 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001058 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001059 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001060 err = read_pin_defaults(codec);
1061 if (err < 0)
1062 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001063
Takashi Iwai0ba21762007-04-16 11:29:14 +02001064 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001065 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001066 codec->subsystem_id =
1067 snd_hda_codec_read(codec, nid, 0,
1068 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001069 }
1070
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001071 /* power-up all before initialization */
1072 hda_set_power_state(codec,
1073 codec->afg ? codec->afg : codec->mfg,
1074 AC_PWRST_D0);
1075
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001076 snd_hda_codec_proc_new(codec);
1077
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001078 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001079
1080 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1081 codec->subsystem_id, codec->revision_id);
1082 snd_component_add(codec->bus->card, component);
1083
1084 if (codecp)
1085 *codecp = codec;
1086 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001087
1088 error:
1089 snd_hda_codec_free(codec);
1090 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001091}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001092EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001093
Takashi Iwaid5191e52009-11-16 14:58:17 +01001094/**
1095 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1096 * @codec: the HDA codec
1097 *
1098 * Start parsing of the given codec tree and (re-)initialize the whole
1099 * patch instance.
1100 *
1101 * Returns 0 if successful or a negative error code.
1102 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001103int snd_hda_codec_configure(struct hda_codec *codec)
1104{
1105 int err;
1106
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001107 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001108 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001109 err = get_codec_name(codec);
1110 if (err < 0)
1111 return err;
1112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Takashi Iwai82467612007-07-27 19:15:54 +02001114 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001116 goto patched;
1117 }
Takashi Iwai82467612007-07-27 19:15:54 +02001118 if (codec->preset && codec->preset->patch) {
1119 err = codec->preset->patch(codec);
1120 goto patched;
1121 }
1122
1123 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001124 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001125 if (err < 0)
1126 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001127
1128 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001129 if (!err && codec->patch_ops.unsol_event)
1130 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001131 /* audio codec should override the mixer name */
1132 if (!err && (codec->afg || !*codec->bus->card->mixername))
1133 snprintf(codec->bus->card->mixername,
1134 sizeof(codec->bus->card->mixername),
1135 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001136 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001138EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140/**
1141 * snd_hda_codec_setup_stream - set up the codec for streaming
1142 * @codec: the CODEC to set up
1143 * @nid: the NID to set up
1144 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1145 * @channel_id: channel id to pass, zero based.
1146 * @format: stream format.
1147 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001148void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1149 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int channel_id, int format)
1151{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001152 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001153 return;
1154
Takashi Iwai0ba21762007-04-16 11:29:14 +02001155 snd_printdd("hda_codec_setup_stream: "
1156 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 nid, stream_tag, channel_id, format);
1158 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1159 (stream_tag << 4) | channel_id);
1160 msleep(1);
1161 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001163EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Takashi Iwaid5191e52009-11-16 14:58:17 +01001165/**
1166 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1167 * @codec: the CODEC to clean up
1168 * @nid: the NID to clean up
1169 */
Takashi Iwai888afa12008-03-18 09:57:50 +01001170void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1171{
1172 if (!nid)
1173 return;
1174
1175 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1176 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1177#if 0 /* keep the format */
1178 msleep(1);
1179 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1180#endif
1181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001182EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184/*
1185 * amp access functions
1186 */
1187
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001188/* FIXME: more better hash key? */
1189#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001190#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001191#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1192#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001194#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001197static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001198 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Takashi Iwai01751f52007-08-10 16:59:39 +02001200 memset(cache, 0, sizeof(*cache));
1201 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001202 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001203}
1204
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001205static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001206{
Takashi Iwai603c4012008-07-30 15:01:44 +02001207 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +02001211static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1212 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwai01751f52007-08-10 16:59:39 +02001214 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1215 u16 cur = cache->hash[idx];
1216 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001219 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (info->key == key)
1221 return info;
1222 cur = info->next;
1223 }
1224
1225 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +02001226 info = snd_array_new(&cache->buf);
Takashi Iwaic2174292008-11-07 00:23:30 +01001227 if (!info)
1228 return NULL;
Takashi Iwaif43aa022008-11-10 16:24:26 +01001229 cur = snd_array_index(&cache->buf, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +02001231 info->val = 0;
1232 info->next = cache->hash[idx];
1233 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 return info;
1236}
1237
Takashi Iwai01751f52007-08-10 16:59:39 +02001238/* query and allocate an amp hash entry */
1239static inline struct hda_amp_info *
1240get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1241{
1242 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1243}
1244
Takashi Iwaid5191e52009-11-16 14:58:17 +01001245/**
1246 * query_amp_caps - query AMP capabilities
1247 * @codec: the HD-auio codec
1248 * @nid: the NID to query
1249 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1250 *
1251 * Query AMP capabilities for the given widget and direction.
1252 * Returns the obtained capability bits.
1253 *
1254 * When cap bits have been already read, this doesn't read again but
1255 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001257u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001259 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Takashi Iwai0ba21762007-04-16 11:29:14 +02001261 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1262 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001264 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001265 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001267 info->amp_caps = snd_hda_param_read(codec, nid,
1268 direction == HDA_OUTPUT ?
1269 AC_PAR_AMP_OUT_CAP :
1270 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001271 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001272 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274 return info->amp_caps;
1275}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001276EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Takashi Iwaid5191e52009-11-16 14:58:17 +01001278/**
1279 * snd_hda_override_amp_caps - Override the AMP capabilities
1280 * @codec: the CODEC to clean up
1281 * @nid: the NID to clean up
1282 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1283 * @caps: the capability bits to set
1284 *
1285 * Override the cached AMP caps bits value by the given one.
1286 * This function is useful if the driver needs to adjust the AMP ranges,
1287 * e.g. limit to 0dB, etc.
1288 *
1289 * Returns zero if successful or a negative error code.
1290 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001291int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1292 unsigned int caps)
1293{
1294 struct hda_amp_info *info;
1295
1296 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1297 if (!info)
1298 return -EINVAL;
1299 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001300 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001301 return 0;
1302}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001303EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001304
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001305static unsigned int
1306query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1307 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001308{
1309 struct hda_amp_info *info;
1310
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001311 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001312 if (!info)
1313 return 0;
1314 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001315 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001316 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001317 }
1318 return info->amp_caps;
1319}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001320
1321static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1322{
1323 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1324}
1325
Takashi Iwaid5191e52009-11-16 14:58:17 +01001326/**
1327 * snd_hda_query_pin_caps - Query PIN capabilities
1328 * @codec: the HD-auio codec
1329 * @nid: the NID to query
1330 *
1331 * Query PIN capabilities for the given widget.
1332 * Returns the obtained capability bits.
1333 *
1334 * When cap bits have been already read, this doesn't read again but
1335 * returns the cached value.
1336 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001337u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1338{
1339 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1340 read_pin_cap);
1341}
Takashi Iwai1327a322009-03-23 13:07:47 +01001342EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1343
Wu Fengguang864f92b2009-11-18 12:38:02 +08001344/**
1345 * snd_hda_pin_sense - execute pin sense measurement
1346 * @codec: the CODEC to sense
1347 * @nid: the pin NID to sense
1348 *
1349 * Execute necessary pin sense measurement and return its Presence Detect,
1350 * Impedance, ELD Valid etc. status bits.
1351 */
1352u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1353{
1354 u32 pincap = snd_hda_query_pin_caps(codec, nid);
1355
1356 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
1357 snd_hda_codec_read(codec, nid, 0, AC_VERB_SET_PIN_SENSE, 0);
1358
1359 return snd_hda_codec_read(codec, nid, 0,
1360 AC_VERB_GET_PIN_SENSE, 0);
1361}
1362EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1363
1364/**
1365 * snd_hda_jack_detect - query pin Presence Detect status
1366 * @codec: the CODEC to sense
1367 * @nid: the pin NID to sense
1368 *
1369 * Query and return the pin's Presence Detect status.
1370 */
1371int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1372{
1373 u32 sense = snd_hda_pin_sense(codec, nid);
1374 return !!(sense & AC_PINSENSE_PRESENCE);
1375}
1376EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378/*
1379 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001380 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001382static unsigned int get_vol_mute(struct hda_codec *codec,
1383 struct hda_amp_info *info, hda_nid_t nid,
1384 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 u32 val, parm;
1387
Takashi Iwai01751f52007-08-10 16:59:39 +02001388 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001389 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1392 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1393 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001394 val = snd_hda_codec_read(codec, nid, 0,
1395 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001397 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001398 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
1401/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001402 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001404static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001405 hda_nid_t nid, int ch, int direction, int index,
1406 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 u32 parm;
1409
1410 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1411 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1412 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1413 parm |= val;
1414 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001415 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Takashi Iwaid5191e52009-11-16 14:58:17 +01001418/**
1419 * snd_hda_codec_amp_read - Read AMP value
1420 * @codec: HD-audio codec
1421 * @nid: NID to read the AMP value
1422 * @ch: channel (left=0 or right=1)
1423 * @direction: #HDA_INPUT or #HDA_OUTPUT
1424 * @index: the index value (only for input direction)
1425 *
1426 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
Takashi Iwai834be882006-03-01 14:16:17 +01001428int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1429 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001431 struct hda_amp_info *info;
1432 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1433 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001435 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001437EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Takashi Iwaid5191e52009-11-16 14:58:17 +01001439/**
1440 * snd_hda_codec_amp_update - update the AMP value
1441 * @codec: HD-audio codec
1442 * @nid: NID to read the AMP value
1443 * @ch: channel (left=0 or right=1)
1444 * @direction: #HDA_INPUT or #HDA_OUTPUT
1445 * @idx: the index value (only for input direction)
1446 * @mask: bit mask to set
1447 * @val: the bits value to set
1448 *
1449 * Update the AMP value with a bit mask.
1450 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001451 */
Takashi Iwai834be882006-03-01 14:16:17 +01001452int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1453 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001455 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001456
Takashi Iwai0ba21762007-04-16 11:29:14 +02001457 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1458 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001460 val &= mask;
1461 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001462 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001464 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return 1;
1466}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001467EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Takashi Iwaid5191e52009-11-16 14:58:17 +01001469/**
1470 * snd_hda_codec_amp_stereo - update the AMP stereo values
1471 * @codec: HD-audio codec
1472 * @nid: NID to read the AMP value
1473 * @direction: #HDA_INPUT or #HDA_OUTPUT
1474 * @idx: the index value (only for input direction)
1475 * @mask: bit mask to set
1476 * @val: the bits value to set
1477 *
1478 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1479 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001480 */
1481int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1482 int direction, int idx, int mask, int val)
1483{
1484 int ch, ret = 0;
1485 for (ch = 0; ch < 2; ch++)
1486 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1487 idx, mask, val);
1488 return ret;
1489}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001490EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001491
Takashi Iwaicb53c622007-08-10 17:21:45 +02001492#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001493/**
1494 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1495 * @codec: HD-audio codec
1496 *
1497 * Resume the all amp commands from the cache.
1498 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001499void snd_hda_codec_resume_amp(struct hda_codec *codec)
1500{
Takashi Iwai603c4012008-07-30 15:01:44 +02001501 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001502 int i;
1503
Takashi Iwai603c4012008-07-30 15:01:44 +02001504 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001505 u32 key = buffer->head.key;
1506 hda_nid_t nid;
1507 unsigned int idx, dir, ch;
1508 if (!key)
1509 continue;
1510 nid = key & 0xff;
1511 idx = (key >> 16) & 0xff;
1512 dir = (key >> 24) & 0xff;
1513 for (ch = 0; ch < 2; ch++) {
1514 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1515 continue;
1516 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1517 buffer->vol[ch]);
1518 }
1519 }
1520}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001521EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001522#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Takashi Iwaid5191e52009-11-16 14:58:17 +01001524/**
1525 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1526 *
1527 * The control element is supposed to have the private_value field
1528 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1529 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001530int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1531 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1534 u16 nid = get_amp_nid(kcontrol);
1535 u8 chs = get_amp_channels(kcontrol);
1536 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001537 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 u32 caps;
1539
1540 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001541 /* num steps */
1542 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1543 if (!caps) {
1544 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001545 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1546 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return -EINVAL;
1548 }
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001549 if (ofs < caps)
1550 caps -= ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1552 uinfo->count = chs == 3 ? 2 : 1;
1553 uinfo->value.integer.min = 0;
1554 uinfo->value.integer.max = caps;
1555 return 0;
1556}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001557EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001559
1560static inline unsigned int
1561read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1562 int ch, int dir, int idx, unsigned int ofs)
1563{
1564 unsigned int val;
1565 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1566 val &= HDA_AMP_VOLMASK;
1567 if (val >= ofs)
1568 val -= ofs;
1569 else
1570 val = 0;
1571 return val;
1572}
1573
1574static inline int
1575update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1576 int ch, int dir, int idx, unsigned int ofs,
1577 unsigned int val)
1578{
1579 if (val > 0)
1580 val += ofs;
1581 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1582 HDA_AMP_VOLMASK, val);
1583}
1584
Takashi Iwaid5191e52009-11-16 14:58:17 +01001585/**
1586 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1587 *
1588 * The control element is supposed to have the private_value field
1589 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1590 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001591int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1592 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1595 hda_nid_t nid = get_amp_nid(kcontrol);
1596 int chs = get_amp_channels(kcontrol);
1597 int dir = get_amp_direction(kcontrol);
1598 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001599 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 long *valp = ucontrol->value.integer.value;
1601
1602 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001603 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001605 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return 0;
1607}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001608EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Takashi Iwaid5191e52009-11-16 14:58:17 +01001610/**
1611 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1612 *
1613 * The control element is supposed to have the private_value field
1614 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1615 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001616int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1617 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1620 hda_nid_t nid = get_amp_nid(kcontrol);
1621 int chs = get_amp_channels(kcontrol);
1622 int dir = get_amp_direction(kcontrol);
1623 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001624 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 long *valp = ucontrol->value.integer.value;
1626 int change = 0;
1627
Takashi Iwaicb53c622007-08-10 17:21:45 +02001628 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001629 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001630 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001631 valp++;
1632 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001633 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001634 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001635 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return change;
1637}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001638EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Takashi Iwaid5191e52009-11-16 14:58:17 +01001640/**
1641 * snd_hda_mixer_amp_volume_put - TLV 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 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001646int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1647 unsigned int size, unsigned int __user *_tlv)
1648{
1649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1650 hda_nid_t nid = get_amp_nid(kcontrol);
1651 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001652 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001653 u32 caps, val1, val2;
1654
1655 if (size < 4 * sizeof(unsigned int))
1656 return -ENOMEM;
1657 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001658 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1659 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001660 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001661 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001662 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001663 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1664 return -EFAULT;
1665 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1666 return -EFAULT;
1667 if (put_user(val1, _tlv + 2))
1668 return -EFAULT;
1669 if (put_user(val2, _tlv + 3))
1670 return -EFAULT;
1671 return 0;
1672}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001673EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001674
Takashi Iwaid5191e52009-11-16 14:58:17 +01001675/**
1676 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1677 * @codec: HD-audio codec
1678 * @nid: NID of a reference widget
1679 * @dir: #HDA_INPUT or #HDA_OUTPUT
1680 * @tlv: TLV data to be stored, at least 4 elements
1681 *
1682 * Set (static) TLV data for a virtual master volume using the AMP caps
1683 * obtained from the reference NID.
1684 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001685 */
1686void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1687 unsigned int *tlv)
1688{
1689 u32 caps;
1690 int nums, step;
1691
1692 caps = query_amp_caps(codec, nid, dir);
1693 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1694 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1695 step = (step + 1) * 25;
1696 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1697 tlv[1] = 2 * sizeof(unsigned int);
1698 tlv[2] = -nums * step;
1699 tlv[3] = step;
1700}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001701EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001702
1703/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001704static struct snd_kcontrol *
1705_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1706 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001707{
1708 struct snd_ctl_elem_id id;
1709 memset(&id, 0, sizeof(id));
1710 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001711 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001712 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1713 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001714 strcpy(id.name, name);
1715 return snd_ctl_find_id(codec->bus->card, &id);
1716}
1717
Takashi Iwaid5191e52009-11-16 14:58:17 +01001718/**
1719 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1720 * @codec: HD-audio codec
1721 * @name: ctl id name string
1722 *
1723 * Get the control element with the given id string and IFACE_MIXER.
1724 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001725struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1726 const char *name)
1727{
1728 return _snd_hda_find_mixer_ctl(codec, name, 0);
1729}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001730EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001731
Takashi Iwaid5191e52009-11-16 14:58:17 +01001732/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001733 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001734 * @codec: HD-audio codec
1735 * @nid: corresponding NID (optional)
1736 * @kctl: the control element to assign
1737 *
1738 * Add the given control element to an array inside the codec instance.
1739 * All control elements belonging to a codec are supposed to be added
1740 * by this function so that a proper clean-up works at the free or
1741 * reconfiguration time.
1742 *
1743 * If non-zero @nid is passed, the NID is assigned to the control element.
1744 * The assignment is shown in the codec proc file.
1745 *
1746 * snd_hda_ctl_add() checks the control subdev id field whether
1747 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001748 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1749 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001750 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001751int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1752 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001753{
1754 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001755 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001756 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001757
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001758 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001759 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001760 if (nid == 0)
1761 nid = get_amp_nid_(kctl->private_value);
1762 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001763 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1764 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001765 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001766 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001767 err = snd_ctl_add(codec->bus->card, kctl);
1768 if (err < 0)
1769 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001770 item = snd_array_new(&codec->mixers);
1771 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001772 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001773 item->kctl = kctl;
1774 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001775 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001776 return 0;
1777}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001778EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001779
Takashi Iwaid5191e52009-11-16 14:58:17 +01001780/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001781 * snd_hda_add_nid - Assign a NID to a control element
1782 * @codec: HD-audio codec
1783 * @nid: corresponding NID (optional)
1784 * @kctl: the control element to assign
1785 * @index: index to kctl
1786 *
1787 * Add the given control element to an array inside the codec instance.
1788 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1789 * NID:KCTL mapping - for example "Capture Source" selector.
1790 */
1791int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1792 unsigned int index, hda_nid_t nid)
1793{
1794 struct hda_nid_item *item;
1795
1796 if (nid > 0) {
1797 item = snd_array_new(&codec->nids);
1798 if (!item)
1799 return -ENOMEM;
1800 item->kctl = kctl;
1801 item->index = index;
1802 item->nid = nid;
1803 return 0;
1804 }
1805 return -EINVAL;
1806}
1807EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1808
1809/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001810 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1811 * @codec: HD-audio codec
1812 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001813void snd_hda_ctls_clear(struct hda_codec *codec)
1814{
1815 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001816 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001817 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001818 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001819 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001820 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001821}
1822
Takashi Iwaia65d6292009-02-23 16:57:04 +01001823/* pseudo device locking
1824 * toggle card->shutdown to allow/disallow the device access (as a hack)
1825 */
1826static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001827{
Takashi Iwaia65d6292009-02-23 16:57:04 +01001828 spin_lock(&card->files_lock);
1829 if (card->shutdown) {
1830 spin_unlock(&card->files_lock);
1831 return -EINVAL;
1832 }
1833 card->shutdown = 1;
1834 spin_unlock(&card->files_lock);
1835 return 0;
1836}
1837
1838static void hda_unlock_devices(struct snd_card *card)
1839{
1840 spin_lock(&card->files_lock);
1841 card->shutdown = 0;
1842 spin_unlock(&card->files_lock);
1843}
1844
Takashi Iwaid5191e52009-11-16 14:58:17 +01001845/**
1846 * snd_hda_codec_reset - Clear all objects assigned to the codec
1847 * @codec: HD-audio codec
1848 *
1849 * This frees the all PCM and control elements assigned to the codec, and
1850 * clears the caches and restores the pin default configurations.
1851 *
1852 * When a device is being used, it returns -EBSY. If successfully freed,
1853 * returns zero.
1854 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001855int snd_hda_codec_reset(struct hda_codec *codec)
1856{
1857 struct snd_card *card = codec->bus->card;
1858 int i, pcm;
1859
1860 if (hda_lock_devices(card) < 0)
1861 return -EBUSY;
1862 /* check whether the codec isn't used by any mixer or PCM streams */
1863 if (!list_empty(&card->ctl_files)) {
1864 hda_unlock_devices(card);
1865 return -EBUSY;
1866 }
1867 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1868 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1869 if (!cpcm->pcm)
1870 continue;
1871 if (cpcm->pcm->streams[0].substream_opened ||
1872 cpcm->pcm->streams[1].substream_opened) {
1873 hda_unlock_devices(card);
1874 return -EBUSY;
1875 }
1876 }
1877
1878 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001879
1880#ifdef CONFIG_SND_HDA_POWER_SAVE
1881 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001882 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001883#endif
1884 snd_hda_ctls_clear(codec);
1885 /* relase PCMs */
1886 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001887 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01001888 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001889 clear_bit(codec->pcm_info[i].device,
1890 codec->bus->pcm_dev_bits);
1891 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001892 }
1893 if (codec->patch_ops.free)
1894 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001895 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001896 codec->spec = NULL;
1897 free_hda_cache(&codec->amp_cache);
1898 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001899 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1900 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01001901 /* free only driver_pins so that init_pins + user_pins are restored */
1902 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001903 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001904 codec->num_pcms = 0;
1905 codec->pcm_info = NULL;
1906 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01001907 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1908 codec->slave_dig_outs = NULL;
1909 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001910 module_put(codec->owner);
1911 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001912
1913 /* allow device access again */
1914 hda_unlock_devices(card);
1915 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001916}
1917
Takashi Iwaid5191e52009-11-16 14:58:17 +01001918/**
1919 * snd_hda_add_vmaster - create a virtual master control and add slaves
1920 * @codec: HD-audio codec
1921 * @name: vmaster control name
1922 * @tlv: TLV data (optional)
1923 * @slaves: slave control names (optional)
1924 *
1925 * Create a virtual master control with the given name. The TLV data
1926 * must be either NULL or a valid data.
1927 *
1928 * @slaves is a NULL-terminated array of strings, each of which is a
1929 * slave control name. All controls with these names are assigned to
1930 * the new virtual master control.
1931 *
1932 * This function returns zero if successful or a negative error code.
1933 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01001934int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1935 unsigned int *tlv, const char **slaves)
1936{
1937 struct snd_kcontrol *kctl;
1938 const char **s;
1939 int err;
1940
Takashi Iwai2f085542008-02-22 18:43:50 +01001941 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1942 ;
1943 if (!*s) {
1944 snd_printdd("No slave found for %s\n", name);
1945 return 0;
1946 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001947 kctl = snd_ctl_make_virtual_master(name, tlv);
1948 if (!kctl)
1949 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001950 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001951 if (err < 0)
1952 return err;
1953
1954 for (s = slaves; *s; s++) {
1955 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01001956 int i = 0;
1957 for (;;) {
1958 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1959 if (!sctl) {
1960 if (!i)
1961 snd_printdd("Cannot find slave %s, "
1962 "skipped\n", *s);
1963 break;
1964 }
1965 err = snd_ctl_add_slave(kctl, sctl);
1966 if (err < 0)
1967 return err;
1968 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001969 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001970 }
1971 return 0;
1972}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001973EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001974
Takashi Iwaid5191e52009-11-16 14:58:17 +01001975/**
1976 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
1977 *
1978 * The control element is supposed to have the private_value field
1979 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1980 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001981int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1982 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 int chs = get_amp_channels(kcontrol);
1985
1986 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1987 uinfo->count = chs == 3 ? 2 : 1;
1988 uinfo->value.integer.min = 0;
1989 uinfo->value.integer.max = 1;
1990 return 0;
1991}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001992EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Takashi Iwaid5191e52009-11-16 14:58:17 +01001994/**
1995 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
1996 *
1997 * The control element is supposed to have the private_value field
1998 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1999 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002000int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2001 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2004 hda_nid_t nid = get_amp_nid(kcontrol);
2005 int chs = get_amp_channels(kcontrol);
2006 int dir = get_amp_direction(kcontrol);
2007 int idx = get_amp_index(kcontrol);
2008 long *valp = ucontrol->value.integer.value;
2009
2010 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002011 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002012 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002014 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002015 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return 0;
2017}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002018EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Takashi Iwaid5191e52009-11-16 14:58:17 +01002020/**
2021 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2022 *
2023 * The control element is supposed to have the private_value field
2024 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2025 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002026int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2027 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2030 hda_nid_t nid = get_amp_nid(kcontrol);
2031 int chs = get_amp_channels(kcontrol);
2032 int dir = get_amp_direction(kcontrol);
2033 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 long *valp = ucontrol->value.integer.value;
2035 int change = 0;
2036
Takashi Iwaicb53c622007-08-10 17:21:45 +02002037 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002038 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002039 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002040 HDA_AMP_MUTE,
2041 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002042 valp++;
2043 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002044 if (chs & 2)
2045 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002046 HDA_AMP_MUTE,
2047 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002048#ifdef CONFIG_SND_HDA_POWER_SAVE
2049 if (codec->patch_ops.check_power_status)
2050 codec->patch_ops.check_power_status(codec, nid);
2051#endif
2052 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return change;
2054}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002055EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Takashi Iwai67d634c2009-11-16 15:35:59 +01002057#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002058/**
2059 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2060 *
2061 * This function calls snd_hda_enable_beep_device(), which behaves differently
2062 * depending on beep_mode option.
2063 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002064int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2065 struct snd_ctl_elem_value *ucontrol)
2066{
2067 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2068 long *valp = ucontrol->value.integer.value;
2069
2070 snd_hda_enable_beep_device(codec, *valp);
2071 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2072}
2073EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002074#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076/*
Takashi Iwai985be542005-11-02 18:26:49 +01002077 * bound volume controls
2078 *
2079 * bind multiple volumes (# indices, from 0)
2080 */
2081
2082#define AMP_VAL_IDX_SHIFT 19
2083#define AMP_VAL_IDX_MASK (0x0f<<19)
2084
Takashi Iwaid5191e52009-11-16 14:58:17 +01002085/**
2086 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2087 *
2088 * The control element is supposed to have the private_value field
2089 * set up via HDA_BIND_MUTE*() macros.
2090 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002091int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002093{
2094 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2095 unsigned long pval;
2096 int err;
2097
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002098 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002099 pval = kcontrol->private_value;
2100 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2101 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2102 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002103 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002104 return err;
2105}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002106EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002107
Takashi Iwaid5191e52009-11-16 14:58:17 +01002108/**
2109 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2110 *
2111 * The control element is supposed to have the private_value field
2112 * set up via HDA_BIND_MUTE*() macros.
2113 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002114int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002116{
2117 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2118 unsigned long pval;
2119 int i, indices, err = 0, change = 0;
2120
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002121 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002122 pval = kcontrol->private_value;
2123 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2124 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002125 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2126 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002127 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2128 if (err < 0)
2129 break;
2130 change |= err;
2131 }
2132 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002133 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002134 return err < 0 ? err : change;
2135}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002136EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002137
Takashi Iwaid5191e52009-11-16 14:58:17 +01002138/**
2139 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2140 *
2141 * The control element is supposed to have the private_value field
2142 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002143 */
2144int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2145 struct snd_ctl_elem_info *uinfo)
2146{
2147 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2148 struct hda_bind_ctls *c;
2149 int err;
2150
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002151 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002152 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002153 kcontrol->private_value = *c->values;
2154 err = c->ops->info(kcontrol, uinfo);
2155 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002156 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002157 return err;
2158}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002159EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002160
Takashi Iwaid5191e52009-11-16 14:58:17 +01002161/**
2162 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2163 *
2164 * The control element is supposed to have the private_value field
2165 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2166 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002167int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_value *ucontrol)
2169{
2170 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2171 struct hda_bind_ctls *c;
2172 int err;
2173
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002174 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002175 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002176 kcontrol->private_value = *c->values;
2177 err = c->ops->get(kcontrol, ucontrol);
2178 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002179 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002180 return err;
2181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002182EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002183
Takashi Iwaid5191e52009-11-16 14:58:17 +01002184/**
2185 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2186 *
2187 * The control element is supposed to have the private_value field
2188 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2189 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002190int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2191 struct snd_ctl_elem_value *ucontrol)
2192{
2193 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2194 struct hda_bind_ctls *c;
2195 unsigned long *vals;
2196 int err = 0, change = 0;
2197
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002198 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002199 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002200 for (vals = c->values; *vals; vals++) {
2201 kcontrol->private_value = *vals;
2202 err = c->ops->put(kcontrol, ucontrol);
2203 if (err < 0)
2204 break;
2205 change |= err;
2206 }
2207 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002208 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002209 return err < 0 ? err : change;
2210}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002211EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002212
Takashi Iwaid5191e52009-11-16 14:58:17 +01002213/**
2214 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2215 *
2216 * The control element is supposed to have the private_value field
2217 * set up via HDA_BIND_VOL() macro.
2218 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002219int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2220 unsigned int size, unsigned int __user *tlv)
2221{
2222 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2223 struct hda_bind_ctls *c;
2224 int err;
2225
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002226 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002227 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002228 kcontrol->private_value = *c->values;
2229 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2230 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002231 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002232 return err;
2233}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002234EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002235
2236struct hda_ctl_ops snd_hda_bind_vol = {
2237 .info = snd_hda_mixer_amp_volume_info,
2238 .get = snd_hda_mixer_amp_volume_get,
2239 .put = snd_hda_mixer_amp_volume_put,
2240 .tlv = snd_hda_mixer_amp_tlv
2241};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002242EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002243
2244struct hda_ctl_ops snd_hda_bind_sw = {
2245 .info = snd_hda_mixer_amp_switch_info,
2246 .get = snd_hda_mixer_amp_switch_get,
2247 .put = snd_hda_mixer_amp_switch_put,
2248 .tlv = snd_hda_mixer_amp_tlv
2249};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002250EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002251
2252/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 * SPDIF out controls
2254 */
2255
Takashi Iwai0ba21762007-04-16 11:29:14 +02002256static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2257 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2260 uinfo->count = 1;
2261 return 0;
2262}
2263
Takashi Iwai0ba21762007-04-16 11:29:14 +02002264static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2268 IEC958_AES0_NONAUDIO |
2269 IEC958_AES0_CON_EMPHASIS_5015 |
2270 IEC958_AES0_CON_NOT_COPYRIGHT;
2271 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2272 IEC958_AES1_CON_ORIGINAL;
2273 return 0;
2274}
2275
Takashi Iwai0ba21762007-04-16 11:29:14 +02002276static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2277 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2280 IEC958_AES0_NONAUDIO |
2281 IEC958_AES0_PRO_EMPHASIS_5015;
2282 return 0;
2283}
2284
Takashi Iwai0ba21762007-04-16 11:29:14 +02002285static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2289
2290 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2291 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2292 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2293 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2294
2295 return 0;
2296}
2297
2298/* convert from SPDIF status bits to HDA SPDIF bits
2299 * bit 0 (DigEn) is always set zero (to be filled later)
2300 */
2301static unsigned short convert_from_spdif_status(unsigned int sbits)
2302{
2303 unsigned short val = 0;
2304
2305 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002306 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002308 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002310 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2311 IEC958_AES0_PRO_EMPHASIS_5015)
2312 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002314 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2315 IEC958_AES0_CON_EMPHASIS_5015)
2316 val |= AC_DIG1_EMPHASIS;
2317 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2318 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002320 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2322 }
2323 return val;
2324}
2325
2326/* convert to SPDIF status bits from HDA SPDIF bits
2327 */
2328static unsigned int convert_to_spdif_status(unsigned short val)
2329{
2330 unsigned int sbits = 0;
2331
Takashi Iwai0ba21762007-04-16 11:29:14 +02002332 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002334 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 sbits |= IEC958_AES0_PROFESSIONAL;
2336 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002337 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2339 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002340 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002342 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002344 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2346 sbits |= val & (0x7f << 8);
2347 }
2348 return sbits;
2349}
2350
Takashi Iwai2f728532008-09-25 16:32:41 +02002351/* set digital convert verbs both for the given NID and its slaves */
2352static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2353 int verb, int val)
2354{
2355 hda_nid_t *d;
2356
Takashi Iwai9e976972008-11-25 08:17:20 +01002357 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002358 d = codec->slave_dig_outs;
2359 if (!d)
2360 return;
2361 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002362 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002363}
2364
2365static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2366 int dig1, int dig2)
2367{
2368 if (dig1 != -1)
2369 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2370 if (dig2 != -1)
2371 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2372}
2373
Takashi Iwai0ba21762007-04-16 11:29:14 +02002374static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2375 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2378 hda_nid_t nid = kcontrol->private_value;
2379 unsigned short val;
2380 int change;
2381
Ingo Molnar62932df2006-01-16 16:34:20 +01002382 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 codec->spdif_status = ucontrol->value.iec958.status[0] |
2384 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2385 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2386 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2387 val = convert_from_spdif_status(codec->spdif_status);
2388 val |= codec->spdif_ctls & 1;
2389 change = codec->spdif_ctls != val;
2390 codec->spdif_ctls = val;
2391
Takashi Iwai2f728532008-09-25 16:32:41 +02002392 if (change)
2393 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Ingo Molnar62932df2006-01-16 16:34:20 +01002395 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return change;
2397}
2398
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002399#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Takashi Iwai0ba21762007-04-16 11:29:14 +02002401static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
2404 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2405
Takashi Iwai0ba21762007-04-16 11:29:14 +02002406 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return 0;
2408}
2409
Takashi Iwai0ba21762007-04-16 11:29:14 +02002410static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2411 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
2413 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2414 hda_nid_t nid = kcontrol->private_value;
2415 unsigned short val;
2416 int change;
2417
Ingo Molnar62932df2006-01-16 16:34:20 +01002418 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002419 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002421 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002423 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002425 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002426 /* unmute amp switch (if any) */
2427 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002428 (val & AC_DIG1_ENABLE))
2429 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2430 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002432 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return change;
2434}
2435
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002436static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 {
2438 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2439 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2440 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2441 .info = snd_hda_spdif_mask_info,
2442 .get = snd_hda_spdif_cmask_get,
2443 },
2444 {
2445 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2447 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2448 .info = snd_hda_spdif_mask_info,
2449 .get = snd_hda_spdif_pmask_get,
2450 },
2451 {
2452 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2453 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2454 .info = snd_hda_spdif_mask_info,
2455 .get = snd_hda_spdif_default_get,
2456 .put = snd_hda_spdif_default_put,
2457 },
2458 {
2459 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2460 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2461 .info = snd_hda_spdif_out_switch_info,
2462 .get = snd_hda_spdif_out_switch_get,
2463 .put = snd_hda_spdif_out_switch_put,
2464 },
2465 { } /* end */
2466};
2467
Takashi Iwai09f99702008-02-04 12:31:13 +01002468#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470/**
2471 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2472 * @codec: the HDA codec
2473 * @nid: audio out widget NID
2474 *
2475 * Creates controls related with the SPDIF output.
2476 * Called from each patch supporting the SPDIF out.
2477 *
2478 * Returns 0 if successful, or a negative error code.
2479 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002480int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
2482 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002483 struct snd_kcontrol *kctl;
2484 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002485 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Takashi Iwai09f99702008-02-04 12:31:13 +01002487 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2488 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2489 idx))
2490 break;
2491 }
2492 if (idx >= SPDIF_MAX_IDX) {
2493 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2494 return -EBUSY;
2495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2497 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002498 if (!kctl)
2499 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002500 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002502 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002503 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return err;
2505 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002506 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002507 snd_hda_codec_read(codec, nid, 0,
2508 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2510 return 0;
2511}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002512EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002515 * SPDIF sharing with analog output
2516 */
2517static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2518 struct snd_ctl_elem_value *ucontrol)
2519{
2520 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2521 ucontrol->value.integer.value[0] = mout->share_spdif;
2522 return 0;
2523}
2524
2525static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2526 struct snd_ctl_elem_value *ucontrol)
2527{
2528 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2529 mout->share_spdif = !!ucontrol->value.integer.value[0];
2530 return 0;
2531}
2532
2533static struct snd_kcontrol_new spdif_share_sw = {
2534 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2535 .name = "IEC958 Default PCM Playback Switch",
2536 .info = snd_ctl_boolean_mono_info,
2537 .get = spdif_share_sw_get,
2538 .put = spdif_share_sw_put,
2539};
2540
Takashi Iwaid5191e52009-11-16 14:58:17 +01002541/**
2542 * snd_hda_create_spdif_share_sw - create Default PCM switch
2543 * @codec: the HDA codec
2544 * @mout: multi-out instance
2545 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002546int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2547 struct hda_multi_out *mout)
2548{
2549 if (!mout->dig_out_nid)
2550 return 0;
2551 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002552 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2553 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002554}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002555EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002556
2557/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 * SPDIF input
2559 */
2560
2561#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2562
Takashi Iwai0ba21762007-04-16 11:29:14 +02002563static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2567
2568 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2569 return 0;
2570}
2571
Takashi Iwai0ba21762007-04-16 11:29:14 +02002572static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
2575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2576 hda_nid_t nid = kcontrol->private_value;
2577 unsigned int val = !!ucontrol->value.integer.value[0];
2578 int change;
2579
Ingo Molnar62932df2006-01-16 16:34:20 +01002580 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002582 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002584 snd_hda_codec_write_cache(codec, nid, 0,
2585 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002587 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return change;
2589}
2590
Takashi Iwai0ba21762007-04-16 11:29:14 +02002591static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2592 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
2594 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2595 hda_nid_t nid = kcontrol->private_value;
2596 unsigned short val;
2597 unsigned int sbits;
2598
Andrew Paprocki3982d172007-12-19 12:13:44 +01002599 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 sbits = convert_to_spdif_status(val);
2601 ucontrol->value.iec958.status[0] = sbits;
2602 ucontrol->value.iec958.status[1] = sbits >> 8;
2603 ucontrol->value.iec958.status[2] = sbits >> 16;
2604 ucontrol->value.iec958.status[3] = sbits >> 24;
2605 return 0;
2606}
2607
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002608static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 {
2610 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2611 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
2612 .info = snd_hda_spdif_in_switch_info,
2613 .get = snd_hda_spdif_in_switch_get,
2614 .put = snd_hda_spdif_in_switch_put,
2615 },
2616 {
2617 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2618 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2619 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
2620 .info = snd_hda_spdif_mask_info,
2621 .get = snd_hda_spdif_in_status_get,
2622 },
2623 { } /* end */
2624};
2625
2626/**
2627 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2628 * @codec: the HDA codec
2629 * @nid: audio in widget NID
2630 *
2631 * Creates controls related with the SPDIF input.
2632 * Called from each patch supporting the SPDIF in.
2633 *
2634 * Returns 0 if successful, or a negative error code.
2635 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002636int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
2638 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002639 struct snd_kcontrol *kctl;
2640 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002641 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Takashi Iwai09f99702008-02-04 12:31:13 +01002643 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2644 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2645 idx))
2646 break;
2647 }
2648 if (idx >= SPDIF_MAX_IDX) {
2649 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2650 return -EBUSY;
2651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2653 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002654 if (!kctl)
2655 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002657 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002658 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return err;
2660 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002661 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002662 snd_hda_codec_read(codec, nid, 0,
2663 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002664 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return 0;
2666}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002667EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Takashi Iwaicb53c622007-08-10 17:21:45 +02002669#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002670/*
2671 * command cache
2672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002674/* build a 32bit cache key with the widget id and the command parameter */
2675#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2676#define get_cmd_cache_nid(key) ((key) & 0xff)
2677#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2678
2679/**
2680 * snd_hda_codec_write_cache - send a single command with caching
2681 * @codec: the HDA codec
2682 * @nid: NID to send the command
2683 * @direct: direct flag
2684 * @verb: the verb to send
2685 * @parm: the parameter for the verb
2686 *
2687 * Send a single command without waiting for response.
2688 *
2689 * Returns 0 if successful, or a negative error code.
2690 */
2691int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2692 int direct, unsigned int verb, unsigned int parm)
2693{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002694 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2695 struct hda_cache_head *c;
2696 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002697
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002698 if (err < 0)
2699 return err;
2700 /* parm may contain the verb stuff for get/set amp */
2701 verb = verb | (parm >> 8);
2702 parm &= 0xff;
2703 key = build_cmd_cache_key(nid, verb);
2704 mutex_lock(&codec->bus->cmd_mutex);
2705 c = get_alloc_hash(&codec->cmd_cache, key);
2706 if (c)
2707 c->val = parm;
2708 mutex_unlock(&codec->bus->cmd_mutex);
2709 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002710}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002711EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002712
Takashi Iwaid5191e52009-11-16 14:58:17 +01002713/**
2714 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2715 * @codec: HD-audio codec
2716 *
2717 * Execute all verbs recorded in the command caches to resume.
2718 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002719void snd_hda_codec_resume_cache(struct hda_codec *codec)
2720{
Takashi Iwai603c4012008-07-30 15:01:44 +02002721 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002722 int i;
2723
Takashi Iwai603c4012008-07-30 15:01:44 +02002724 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002725 u32 key = buffer->key;
2726 if (!key)
2727 continue;
2728 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2729 get_cmd_cache_cmd(key), buffer->val);
2730 }
2731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002732EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002733
2734/**
2735 * snd_hda_sequence_write_cache - sequence writes with caching
2736 * @codec: the HDA codec
2737 * @seq: VERB array to send
2738 *
2739 * Send the commands sequentially from the given array.
2740 * Thte commands are recorded on cache for power-save and resume.
2741 * The array must be terminated with NID=0.
2742 */
2743void snd_hda_sequence_write_cache(struct hda_codec *codec,
2744 const struct hda_verb *seq)
2745{
2746 for (; seq->nid; seq++)
2747 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2748 seq->param);
2749}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002750EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002751#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002752
Takashi Iwai54d17402005-11-21 16:33:22 +01002753/*
2754 * set power state of the codec
2755 */
2756static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2757 unsigned int power_state)
2758{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002759 hda_nid_t nid;
2760 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002761
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002762 /* this delay seems necessary to avoid click noise at power-down */
2763 if (power_state == AC_PWRST_D3)
2764 msleep(100);
2765 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002766 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002767 /* partial workaround for "azx_get_response timeout" */
2768 if (power_state == AC_PWRST_D0)
2769 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002770
Takashi Iwaicb53c622007-08-10 17:21:45 +02002771 nid = codec->start_nid;
2772 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002773 unsigned int wcaps = get_wcaps(codec, nid);
2774 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002775 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002776 if (power_state == AC_PWRST_D3 &&
2777 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002778 unsigned int pincap;
2779 /*
2780 * don't power down the widget if it controls
2781 * eapd and EAPD_BTLENABLE is set.
2782 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01002783 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002784 if (pincap & AC_PINCAP_EAPD) {
2785 int eapd = snd_hda_codec_read(codec,
2786 nid, 0,
2787 AC_VERB_GET_EAPD_BTLENABLE, 0);
2788 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002789 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002790 continue;
2791 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002792 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002793 snd_hda_codec_write(codec, nid, 0,
2794 AC_VERB_SET_POWER_STATE,
2795 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002796 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002797 }
2798
Takashi Iwaicb53c622007-08-10 17:21:45 +02002799 if (power_state == AC_PWRST_D0) {
2800 unsigned long end_time;
2801 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01002802 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002803 /* wait until the codec reachs to D0 */
2804 end_time = jiffies + msecs_to_jiffies(500);
2805 do {
2806 state = snd_hda_codec_read(codec, fg, 0,
2807 AC_VERB_GET_POWER_STATE, 0);
2808 if (state == power_state)
2809 break;
2810 msleep(1);
2811 } while (time_after_eq(end_time, jiffies));
2812 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002813}
2814
Takashi Iwai11aeff02008-07-30 15:01:46 +02002815#ifdef CONFIG_SND_HDA_HWDEP
2816/* execute additional init verbs */
2817static void hda_exec_init_verbs(struct hda_codec *codec)
2818{
2819 if (codec->init_verbs.list)
2820 snd_hda_sequence_write(codec, codec->init_verbs.list);
2821}
2822#else
2823static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2824#endif
2825
Takashi Iwaicb53c622007-08-10 17:21:45 +02002826#ifdef SND_HDA_NEEDS_RESUME
2827/*
2828 * call suspend and power-down; used both from PM and power-save
2829 */
2830static void hda_call_codec_suspend(struct hda_codec *codec)
2831{
2832 if (codec->patch_ops.suspend)
2833 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2834 hda_set_power_state(codec,
2835 codec->afg ? codec->afg : codec->mfg,
2836 AC_PWRST_D3);
2837#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01002838 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002839 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002840 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002841 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01002842 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002843#endif
2844}
2845
2846/*
2847 * kick up codec; used both from PM and power-save
2848 */
2849static void hda_call_codec_resume(struct hda_codec *codec)
2850{
2851 hda_set_power_state(codec,
2852 codec->afg ? codec->afg : codec->mfg,
2853 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01002854 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwai11aeff02008-07-30 15:01:46 +02002855 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002856 if (codec->patch_ops.resume)
2857 codec->patch_ops.resume(codec);
2858 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002859 if (codec->patch_ops.init)
2860 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002861 snd_hda_codec_resume_amp(codec);
2862 snd_hda_codec_resume_cache(codec);
2863 }
2864}
2865#endif /* SND_HDA_NEEDS_RESUME */
2866
Takashi Iwai54d17402005-11-21 16:33:22 +01002867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868/**
2869 * snd_hda_build_controls - build mixer controls
2870 * @bus: the BUS
2871 *
2872 * Creates mixer controls for each codec included in the bus.
2873 *
2874 * Returns 0 if successful, otherwise a negative error code.
2875 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002876int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002878 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Takashi Iwai0ba21762007-04-16 11:29:14 +02002880 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002881 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002882 if (err < 0) {
2883 printk(KERN_ERR "hda_codec: cannot build controls"
2884 "for #%d (error %d)\n", codec->addr, err);
2885 err = snd_hda_codec_reset(codec);
2886 if (err < 0) {
2887 printk(KERN_ERR
2888 "hda_codec: cannot revert codec\n");
2889 return err;
2890 }
2891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002893 return 0;
2894}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002895EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002897int snd_hda_codec_build_controls(struct hda_codec *codec)
2898{
2899 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02002900 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002901 /* continue to initialize... */
2902 if (codec->patch_ops.init)
2903 err = codec->patch_ops.init(codec);
2904 if (!err && codec->patch_ops.build_controls)
2905 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002906 if (err < 0)
2907 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return 0;
2909}
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911/*
2912 * stream formats
2913 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002914struct hda_rate_tbl {
2915 unsigned int hz;
2916 unsigned int alsa_bits;
2917 unsigned int hda_fmt;
2918};
2919
2920static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002922
2923 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2925 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2926 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2927 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2928 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2929 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2930 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2931 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2932 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2933 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2934 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002935#define AC_PAR_PCM_RATE_BITS 11
2936 /* up to bits 10, 384kHZ isn't supported properly */
2937
2938 /* not autodetected value */
2939 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002940
Takashi Iwaibefdf312005-08-22 13:57:55 +02002941 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942};
2943
2944/**
2945 * snd_hda_calc_stream_format - calculate format bitset
2946 * @rate: the sample rate
2947 * @channels: the number of channels
2948 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2949 * @maxbps: the max. bps
2950 *
2951 * Calculate the format bitset from the given rate, channels and th PCM format.
2952 *
2953 * Return zero if invalid.
2954 */
2955unsigned int snd_hda_calc_stream_format(unsigned int rate,
2956 unsigned int channels,
2957 unsigned int format,
2958 unsigned int maxbps)
2959{
2960 int i;
2961 unsigned int val = 0;
2962
Takashi Iwaibefdf312005-08-22 13:57:55 +02002963 for (i = 0; rate_bits[i].hz; i++)
2964 if (rate_bits[i].hz == rate) {
2965 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 break;
2967 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002968 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 snd_printdd("invalid rate %d\n", rate);
2970 return 0;
2971 }
2972
2973 if (channels == 0 || channels > 8) {
2974 snd_printdd("invalid channels %d\n", channels);
2975 return 0;
2976 }
2977 val |= channels - 1;
2978
2979 switch (snd_pcm_format_width(format)) {
2980 case 8: val |= 0x00; break;
2981 case 16: val |= 0x10; break;
2982 case 20:
2983 case 24:
2984 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02002985 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 val |= 0x40;
2987 else if (maxbps >= 24)
2988 val |= 0x30;
2989 else
2990 val |= 0x20;
2991 break;
2992 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002993 snd_printdd("invalid format width %d\n",
2994 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return 0;
2996 }
2997
2998 return val;
2999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003000EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003002static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3003{
3004 unsigned int val = 0;
3005 if (nid != codec->afg &&
3006 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3007 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3008 if (!val || val == -1)
3009 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3010 if (!val || val == -1)
3011 return 0;
3012 return val;
3013}
3014
3015static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3016{
3017 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3018 get_pcm_param);
3019}
3020
3021static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3022{
3023 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3024 if (!streams || streams == -1)
3025 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3026 if (!streams || streams == -1)
3027 return 0;
3028 return streams;
3029}
3030
3031static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3032{
3033 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3034 get_stream_param);
3035}
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037/**
3038 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3039 * @codec: the HDA codec
3040 * @nid: NID to query
3041 * @ratesp: the pointer to store the detected rate bitflags
3042 * @formatsp: the pointer to store the detected formats
3043 * @bpsp: the pointer to store the detected format widths
3044 *
3045 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3046 * or @bsps argument is ignored.
3047 *
3048 * Returns 0 if successful, otherwise a negative error code.
3049 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003050static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3052{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003053 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003055 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003056 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 if (ratesp) {
3059 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003060 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003062 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003064 if (rates == 0) {
3065 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3066 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3067 nid, val,
3068 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3069 return -EIO;
3070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 *ratesp = rates;
3072 }
3073
3074 if (formatsp || bpsp) {
3075 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003076 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003078 streams = query_stream_param(codec, nid);
3079 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 bps = 0;
3083 if (streams & AC_SUPFMT_PCM) {
3084 if (val & AC_SUPPCM_BITS_8) {
3085 formats |= SNDRV_PCM_FMTBIT_U8;
3086 bps = 8;
3087 }
3088 if (val & AC_SUPPCM_BITS_16) {
3089 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3090 bps = 16;
3091 }
3092 if (wcaps & AC_WCAP_DIGITAL) {
3093 if (val & AC_SUPPCM_BITS_32)
3094 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3095 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3096 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3097 if (val & AC_SUPPCM_BITS_24)
3098 bps = 24;
3099 else if (val & AC_SUPPCM_BITS_20)
3100 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003101 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3102 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3104 if (val & AC_SUPPCM_BITS_32)
3105 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 else if (val & AC_SUPPCM_BITS_24)
3107 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003108 else if (val & AC_SUPPCM_BITS_20)
3109 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
3111 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003112 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003114 if (!bps)
3115 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003116 }
3117 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003118 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /* temporary hack: we have still no proper support
3120 * for the direct AC3 stream...
3121 */
3122 formats |= SNDRV_PCM_FMTBIT_U8;
3123 bps = 8;
3124 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003125 if (formats == 0) {
3126 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3127 "(nid=0x%x, val=0x%x, ovrd=%i, "
3128 "streams=0x%x)\n",
3129 nid, val,
3130 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3131 streams);
3132 return -EIO;
3133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 if (formatsp)
3135 *formatsp = formats;
3136 if (bpsp)
3137 *bpsp = bps;
3138 }
3139
3140 return 0;
3141}
3142
3143/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003144 * snd_hda_is_supported_format - Check the validity of the format
3145 * @codec: HD-audio codec
3146 * @nid: NID to check
3147 * @format: the HD-audio format value to check
3148 *
3149 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 *
3151 * Returns 1 if supported, 0 if not.
3152 */
3153int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3154 unsigned int format)
3155{
3156 int i;
3157 unsigned int val = 0, rate, stream;
3158
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003159 val = query_pcm_param(codec, nid);
3160 if (!val)
3161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
3163 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003164 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003165 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 if (val & (1 << i))
3167 break;
3168 return 0;
3169 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003170 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return 0;
3172
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003173 stream = query_stream_param(codec, nid);
3174 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return 0;
3176
3177 if (stream & AC_SUPFMT_PCM) {
3178 switch (format & 0xf0) {
3179 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003180 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return 0;
3182 break;
3183 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003184 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return 0;
3186 break;
3187 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003188 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return 0;
3190 break;
3191 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003192 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return 0;
3194 break;
3195 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003196 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return 0;
3198 break;
3199 default:
3200 return 0;
3201 }
3202 } else {
3203 /* FIXME: check for float32 and AC3? */
3204 }
3205
3206 return 1;
3207}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003208EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210/*
3211 * PCM stuff
3212 */
3213static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3214 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003215 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
3217 return 0;
3218}
3219
3220static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3221 struct hda_codec *codec,
3222 unsigned int stream_tag,
3223 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003224 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3227 return 0;
3228}
3229
3230static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3231 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003232 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
Takashi Iwai888afa12008-03-18 09:57:50 +01003234 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return 0;
3236}
3237
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003238static int set_pcm_default_values(struct hda_codec *codec,
3239 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003241 int err;
3242
Takashi Iwai0ba21762007-04-16 11:29:14 +02003243 /* query support PCM information from the given NID */
3244 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003245 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003246 info->rates ? NULL : &info->rates,
3247 info->formats ? NULL : &info->formats,
3248 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003249 if (err < 0)
3250 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 }
3252 if (info->ops.open == NULL)
3253 info->ops.open = hda_pcm_default_open_close;
3254 if (info->ops.close == NULL)
3255 info->ops.close = hda_pcm_default_open_close;
3256 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003257 if (snd_BUG_ON(!info->nid))
3258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 info->ops.prepare = hda_pcm_default_prepare;
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003262 if (snd_BUG_ON(!info->nid))
3263 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 info->ops.cleanup = hda_pcm_default_cleanup;
3265 }
3266 return 0;
3267}
3268
Takashi Iwaid5191e52009-11-16 14:58:17 +01003269/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003270const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3271 "Audio", "SPDIF", "HDMI", "Modem"
3272};
3273
Takashi Iwai176d5332008-07-30 15:01:44 +02003274/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003275 * get the empty PCM device number to assign
3276 */
3277static int get_empty_pcm_device(struct hda_bus *bus, int type)
3278{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003279 /* audio device indices; not linear to keep compatibility */
3280 static int audio_idx[HDA_PCM_NTYPES][5] = {
3281 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3282 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003283 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003284 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003285 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003286 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003287
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003288 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003289 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3290 return -EINVAL;
3291 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003292
3293 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3294 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3295 return audio_idx[type][i];
3296
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003297 snd_printk(KERN_WARNING "Too many %s devices\n", snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003298 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003299}
3300
3301/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003302 * attach a new PCM stream
3303 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003304static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003305{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003306 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003307 struct hda_pcm_stream *info;
3308 int stream, err;
3309
Takashi Iwaib91f0802008-11-04 08:43:08 +01003310 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003311 return -EINVAL;
3312 for (stream = 0; stream < 2; stream++) {
3313 info = &pcm->stream[stream];
3314 if (info->substreams) {
3315 err = set_pcm_default_values(codec, info);
3316 if (err < 0)
3317 return err;
3318 }
3319 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003320 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003321}
3322
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003323/* assign all PCMs of the given codec */
3324int snd_hda_codec_build_pcms(struct hda_codec *codec)
3325{
3326 unsigned int pcm;
3327 int err;
3328
3329 if (!codec->num_pcms) {
3330 if (!codec->patch_ops.build_pcms)
3331 return 0;
3332 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003333 if (err < 0) {
3334 printk(KERN_ERR "hda_codec: cannot build PCMs"
3335 "for #%d (error %d)\n", codec->addr, err);
3336 err = snd_hda_codec_reset(codec);
3337 if (err < 0) {
3338 printk(KERN_ERR
3339 "hda_codec: cannot revert codec\n");
3340 return err;
3341 }
3342 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003343 }
3344 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3345 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3346 int dev;
3347
3348 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003349 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003350
3351 if (!cpcm->pcm) {
3352 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3353 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003354 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003355 cpcm->device = dev;
3356 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003357 if (err < 0) {
3358 printk(KERN_ERR "hda_codec: cannot attach "
3359 "PCM stream %d for codec #%d\n",
3360 dev, codec->addr);
3361 continue; /* no fatal error */
3362 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003363 }
3364 }
3365 return 0;
3366}
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368/**
3369 * snd_hda_build_pcms - build PCM information
3370 * @bus: the BUS
3371 *
3372 * Create PCM information for each codec included in the bus.
3373 *
3374 * The build_pcms codec patch is requested to set up codec->num_pcms and
3375 * codec->pcm_info properly. The array is referred by the top-level driver
3376 * to create its PCM instances.
3377 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3378 * callback.
3379 *
3380 * At least, substreams, channels_min and channels_max must be filled for
3381 * each stream. substreams = 0 indicates that the stream doesn't exist.
3382 * When rates and/or formats are zero, the supported values are queried
3383 * from the given nid. The nid is used also by the default ops.prepare
3384 * and ops.cleanup callbacks.
3385 *
3386 * The driver needs to call ops.open in its open callback. Similarly,
3387 * ops.close is supposed to be called in the close callback.
3388 * ops.prepare should be called in the prepare or hw_params callback
3389 * with the proper parameters for set up.
3390 * ops.cleanup should be called in hw_free for clean up of streams.
3391 *
3392 * This function returns 0 if successfull, or a negative error code.
3393 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003394int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003396 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Takashi Iwai0ba21762007-04-16 11:29:14 +02003398 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003399 int err = snd_hda_codec_build_pcms(codec);
3400 if (err < 0)
3401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
3403 return 0;
3404}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003405EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407/**
3408 * snd_hda_check_board_config - compare the current codec with the config table
3409 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003410 * @num_configs: number of config enums
3411 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 * @tbl: configuration table, terminated by null entries
3413 *
3414 * Compares the modelname or PCI subsystem id of the current codec with the
3415 * given configuration table. If a matching entry is found, returns its
3416 * config value (supposed to be 0 or positive).
3417 *
3418 * If no entries are matching, the function returns a negative value.
3419 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003420int snd_hda_check_board_config(struct hda_codec *codec,
3421 int num_configs, const char **models,
3422 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003424 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003425 int i;
3426 for (i = 0; i < num_configs; i++) {
3427 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003428 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003429 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3430 "selected\n", models[i]);
3431 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
3433 }
3434 }
3435
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003436 if (!codec->bus->pci || !tbl)
3437 return -1;
3438
3439 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3440 if (!tbl)
3441 return -1;
3442 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003443#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003444 char tmp[10];
3445 const char *model = NULL;
3446 if (models)
3447 model = models[tbl->value];
3448 if (!model) {
3449 sprintf(tmp, "#%d", tbl->value);
3450 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003452 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3453 "for config %x:%x (%s)\n",
3454 model, tbl->subvendor, tbl->subdevice,
3455 (tbl->name ? tbl->name : "Unknown device"));
3456#endif
3457 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
3459 return -1;
3460}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003461EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
3463/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003464 * snd_hda_check_board_codec_sid_config - compare the current codec
3465 subsystem ID with the
3466 config table
3467
3468 This is important for Gateway notebooks with SB450 HDA Audio
3469 where the vendor ID of the PCI device is:
3470 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3471 and the vendor/subvendor are found only at the codec.
3472
3473 * @codec: the HDA codec
3474 * @num_configs: number of config enums
3475 * @models: array of model name strings
3476 * @tbl: configuration table, terminated by null entries
3477 *
3478 * Compares the modelname or PCI subsystem id of the current codec with the
3479 * given configuration table. If a matching entry is found, returns its
3480 * config value (supposed to be 0 or positive).
3481 *
3482 * If no entries are matching, the function returns a negative value.
3483 */
3484int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3485 int num_configs, const char **models,
3486 const struct snd_pci_quirk *tbl)
3487{
3488 const struct snd_pci_quirk *q;
3489
3490 /* Search for codec ID */
3491 for (q = tbl; q->subvendor; q++) {
3492 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3493
3494 if (vendorid == codec->subsystem_id)
3495 break;
3496 }
3497
3498 if (!q->subvendor)
3499 return -1;
3500
3501 tbl = q;
3502
3503 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003504#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003505 char tmp[10];
3506 const char *model = NULL;
3507 if (models)
3508 model = models[tbl->value];
3509 if (!model) {
3510 sprintf(tmp, "#%d", tbl->value);
3511 model = tmp;
3512 }
3513 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3514 "for config %x:%x (%s)\n",
3515 model, tbl->subvendor, tbl->subdevice,
3516 (tbl->name ? tbl->name : "Unknown device"));
3517#endif
3518 return tbl->value;
3519 }
3520 return -1;
3521}
3522EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3523
3524/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 * snd_hda_add_new_ctls - create controls from the array
3526 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003527 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 *
3529 * This helper function creates and add new controls in the given array.
3530 * The array must be terminated with an empty entry as terminator.
3531 *
3532 * Returns 0 if successful, or a negative error code.
3533 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003534int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003536 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003539 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003540 if (knew->iface == -1) /* skip this codec private value */
3541 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003542 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003543 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003544 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003545 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003546 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003547 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003548 return err;
3549 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003550 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003551 return -ENOMEM;
3552 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003553 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003554 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003555 return err;
3556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 }
3558 return 0;
3559}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003560EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Takashi Iwaicb53c622007-08-10 17:21:45 +02003562#ifdef CONFIG_SND_HDA_POWER_SAVE
3563static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3564 unsigned int power_state);
3565
3566static void hda_power_work(struct work_struct *work)
3567{
3568 struct hda_codec *codec =
3569 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003570 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003571
Maxim Levitsky2e492462007-09-03 15:26:57 +02003572 if (!codec->power_on || codec->power_count) {
3573 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003574 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003575 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003576
3577 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003578 if (bus->ops.pm_notify)
3579 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003580}
3581
3582static void hda_keep_power_on(struct hda_codec *codec)
3583{
3584 codec->power_count++;
3585 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003586 codec->power_jiffies = jiffies;
3587}
3588
Takashi Iwaid5191e52009-11-16 14:58:17 +01003589/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003590void snd_hda_update_power_acct(struct hda_codec *codec)
3591{
3592 unsigned long delta = jiffies - codec->power_jiffies;
3593 if (codec->power_on)
3594 codec->power_on_acct += delta;
3595 else
3596 codec->power_off_acct += delta;
3597 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003598}
3599
Takashi Iwaid5191e52009-11-16 14:58:17 +01003600/**
3601 * snd_hda_power_up - Power-up the codec
3602 * @codec: HD-audio codec
3603 *
3604 * Increment the power-up counter and power up the hardware really when
3605 * not turned on yet.
3606 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003607void snd_hda_power_up(struct hda_codec *codec)
3608{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003609 struct hda_bus *bus = codec->bus;
3610
Takashi Iwaicb53c622007-08-10 17:21:45 +02003611 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003612 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003613 return;
3614
Takashi Iwaia2f63092009-11-11 09:34:25 +01003615 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003616 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003617 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003618 if (bus->ops.pm_notify)
3619 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003620 hda_call_codec_resume(codec);
3621 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003622 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003623}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003624EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003625
3626#define power_save(codec) \
3627 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003628
Takashi Iwaid5191e52009-11-16 14:58:17 +01003629/**
3630 * snd_hda_power_down - Power-down the codec
3631 * @codec: HD-audio codec
3632 *
3633 * Decrement the power-up counter and schedules the power-off work if
3634 * the counter rearches to zero.
3635 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003636void snd_hda_power_down(struct hda_codec *codec)
3637{
3638 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003639 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003640 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003641 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003642 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003643 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003644 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003645 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003646}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003647EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003648
Takashi Iwaid5191e52009-11-16 14:58:17 +01003649/**
3650 * snd_hda_check_amp_list_power - Check the amp list and update the power
3651 * @codec: HD-audio codec
3652 * @check: the object containing an AMP list and the status
3653 * @nid: NID to check / update
3654 *
3655 * Check whether the given NID is in the amp list. If it's in the list,
3656 * check the current AMP status, and update the the power-status according
3657 * to the mute status.
3658 *
3659 * This function is supposed to be set or called from the check_power_status
3660 * patch ops.
3661 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003662int snd_hda_check_amp_list_power(struct hda_codec *codec,
3663 struct hda_loopback_check *check,
3664 hda_nid_t nid)
3665{
3666 struct hda_amp_list *p;
3667 int ch, v;
3668
3669 if (!check->amplist)
3670 return 0;
3671 for (p = check->amplist; p->nid; p++) {
3672 if (p->nid == nid)
3673 break;
3674 }
3675 if (!p->nid)
3676 return 0; /* nothing changed */
3677
3678 for (p = check->amplist; p->nid; p++) {
3679 for (ch = 0; ch < 2; ch++) {
3680 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3681 p->idx);
3682 if (!(v & HDA_AMP_MUTE) && v > 0) {
3683 if (!check->power_on) {
3684 check->power_on = 1;
3685 snd_hda_power_up(codec);
3686 }
3687 return 1;
3688 }
3689 }
3690 }
3691 if (check->power_on) {
3692 check->power_on = 0;
3693 snd_hda_power_down(codec);
3694 }
3695 return 0;
3696}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003697EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003698#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003700/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003701 * Channel mode helper
3702 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003703
3704/**
3705 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3706 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003707int snd_hda_ch_mode_info(struct hda_codec *codec,
3708 struct snd_ctl_elem_info *uinfo,
3709 const struct hda_channel_mode *chmode,
3710 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003711{
3712 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3713 uinfo->count = 1;
3714 uinfo->value.enumerated.items = num_chmodes;
3715 if (uinfo->value.enumerated.item >= num_chmodes)
3716 uinfo->value.enumerated.item = num_chmodes - 1;
3717 sprintf(uinfo->value.enumerated.name, "%dch",
3718 chmode[uinfo->value.enumerated.item].channels);
3719 return 0;
3720}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003721EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003722
Takashi Iwaid5191e52009-11-16 14:58:17 +01003723/**
3724 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3725 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003726int snd_hda_ch_mode_get(struct hda_codec *codec,
3727 struct snd_ctl_elem_value *ucontrol,
3728 const struct hda_channel_mode *chmode,
3729 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003730 int max_channels)
3731{
3732 int i;
3733
3734 for (i = 0; i < num_chmodes; i++) {
3735 if (max_channels == chmode[i].channels) {
3736 ucontrol->value.enumerated.item[0] = i;
3737 break;
3738 }
3739 }
3740 return 0;
3741}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003742EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003743
Takashi Iwaid5191e52009-11-16 14:58:17 +01003744/**
3745 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3746 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003747int snd_hda_ch_mode_put(struct hda_codec *codec,
3748 struct snd_ctl_elem_value *ucontrol,
3749 const struct hda_channel_mode *chmode,
3750 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003751 int *max_channelsp)
3752{
3753 unsigned int mode;
3754
3755 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01003756 if (mode >= num_chmodes)
3757 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003758 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003759 return 0;
3760 /* change the current channel setting */
3761 *max_channelsp = chmode[mode].channels;
3762 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003763 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003764 return 1;
3765}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003766EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768/*
3769 * input MUX helper
3770 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003771
3772/**
3773 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3774 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003775int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3776 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
3778 unsigned int index;
3779
3780 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3781 uinfo->count = 1;
3782 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003783 if (!imux->num_items)
3784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 index = uinfo->value.enumerated.item;
3786 if (index >= imux->num_items)
3787 index = imux->num_items - 1;
3788 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3789 return 0;
3790}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003791EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Takashi Iwaid5191e52009-11-16 14:58:17 +01003793/**
3794 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3795 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003796int snd_hda_input_mux_put(struct hda_codec *codec,
3797 const struct hda_input_mux *imux,
3798 struct snd_ctl_elem_value *ucontrol,
3799 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 unsigned int *cur_val)
3801{
3802 unsigned int idx;
3803
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003804 if (!imux->num_items)
3805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 idx = ucontrol->value.enumerated.item[0];
3807 if (idx >= imux->num_items)
3808 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003809 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003811 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3812 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 *cur_val = idx;
3814 return 1;
3815}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003816EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
3818
3819/*
3820 * Multi-channel / digital-out PCM helper functions
3821 */
3822
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003823/* setup SPDIF output stream */
3824static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3825 unsigned int stream_tag, unsigned int format)
3826{
3827 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02003828 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3829 set_dig_out_convert(codec, nid,
3830 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3831 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003832 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003833 if (codec->slave_dig_outs) {
3834 hda_nid_t *d;
3835 for (d = codec->slave_dig_outs; *d; d++)
3836 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3837 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003838 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003839 /* turn on again (if needed) */
3840 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3841 set_dig_out_convert(codec, nid,
3842 codec->spdif_ctls & 0xff, -1);
3843}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003844
Takashi Iwai2f728532008-09-25 16:32:41 +02003845static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3846{
3847 snd_hda_codec_cleanup_stream(codec, nid);
3848 if (codec->slave_dig_outs) {
3849 hda_nid_t *d;
3850 for (d = codec->slave_dig_outs; *d; d++)
3851 snd_hda_codec_cleanup_stream(codec, *d);
3852 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003853}
3854
Takashi Iwaid5191e52009-11-16 14:58:17 +01003855/**
3856 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
3857 * @bus: HD-audio bus
3858 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003859void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3860{
3861 struct hda_codec *codec;
3862
3863 if (!bus)
3864 return;
3865 list_for_each_entry(codec, &bus->codec_list, list) {
3866#ifdef CONFIG_SND_HDA_POWER_SAVE
3867 if (!codec->power_on)
3868 continue;
3869#endif
3870 if (codec->patch_ops.reboot_notify)
3871 codec->patch_ops.reboot_notify(codec);
3872 }
3873}
Takashi Iwai8f217a22009-11-10 18:26:12 +01003874EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003875
Takashi Iwaid5191e52009-11-16 14:58:17 +01003876/**
3877 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003879int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3880 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
Ingo Molnar62932df2006-01-16 16:34:20 +01003882 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003883 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3884 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003885 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003887 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 return 0;
3889}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003890EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Takashi Iwaid5191e52009-11-16 14:58:17 +01003892/**
3893 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3894 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003895int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3896 struct hda_multi_out *mout,
3897 unsigned int stream_tag,
3898 unsigned int format,
3899 struct snd_pcm_substream *substream)
3900{
3901 mutex_lock(&codec->spdif_mutex);
3902 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3903 mutex_unlock(&codec->spdif_mutex);
3904 return 0;
3905}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003906EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003907
Takashi Iwaid5191e52009-11-16 14:58:17 +01003908/**
3909 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3910 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003911int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3912 struct hda_multi_out *mout)
3913{
3914 mutex_lock(&codec->spdif_mutex);
3915 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3916 mutex_unlock(&codec->spdif_mutex);
3917 return 0;
3918}
3919EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3920
Takashi Iwaid5191e52009-11-16 14:58:17 +01003921/**
3922 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003924int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3925 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
Ingo Molnar62932df2006-01-16 16:34:20 +01003927 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003929 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return 0;
3931}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003932EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Takashi Iwaid5191e52009-11-16 14:58:17 +01003934/**
3935 * snd_hda_multi_out_analog_open - open analog outputs
3936 *
3937 * Open analog outputs and set up the hw-constraints.
3938 * If the digital outputs can be opened as slave, open the digital
3939 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003941int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3942 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003943 struct snd_pcm_substream *substream,
3944 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945{
Takashi Iwai9a081602008-02-12 18:37:26 +01003946 struct snd_pcm_runtime *runtime = substream->runtime;
3947 runtime->hw.channels_max = mout->max_channels;
3948 if (mout->dig_out_nid) {
3949 if (!mout->analog_rates) {
3950 mout->analog_rates = hinfo->rates;
3951 mout->analog_formats = hinfo->formats;
3952 mout->analog_maxbps = hinfo->maxbps;
3953 } else {
3954 runtime->hw.rates = mout->analog_rates;
3955 runtime->hw.formats = mout->analog_formats;
3956 hinfo->maxbps = mout->analog_maxbps;
3957 }
3958 if (!mout->spdif_rates) {
3959 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3960 &mout->spdif_rates,
3961 &mout->spdif_formats,
3962 &mout->spdif_maxbps);
3963 }
3964 mutex_lock(&codec->spdif_mutex);
3965 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02003966 if ((runtime->hw.rates & mout->spdif_rates) &&
3967 (runtime->hw.formats & mout->spdif_formats)) {
3968 runtime->hw.rates &= mout->spdif_rates;
3969 runtime->hw.formats &= mout->spdif_formats;
3970 if (mout->spdif_maxbps < hinfo->maxbps)
3971 hinfo->maxbps = mout->spdif_maxbps;
3972 } else {
3973 mout->share_spdif = 0;
3974 /* FIXME: need notify? */
3975 }
Takashi Iwai9a081602008-02-12 18:37:26 +01003976 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003977 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3980 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3981}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003982EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Takashi Iwaid5191e52009-11-16 14:58:17 +01003984/**
3985 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3986 *
3987 * Set up the i/o for analog out.
3988 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003990int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3991 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 unsigned int stream_tag,
3993 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003994 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
3996 hda_nid_t *nids = mout->dac_nids;
3997 int chs = substream->runtime->channels;
3998 int i;
3999
Ingo Molnar62932df2006-01-16 16:34:20 +01004000 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004001 if (mout->dig_out_nid && mout->share_spdif &&
4002 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004004 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4005 format) &&
4006 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004008 setup_dig_out_stream(codec, mout->dig_out_nid,
4009 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 } else {
4011 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004012 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
4014 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004015 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
4017 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004018 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4019 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004020 if (!mout->no_share_stream &&
4021 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004023 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4024 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004025 /* extra outputs copied from front */
4026 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004027 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004028 snd_hda_codec_setup_stream(codec,
4029 mout->extra_out_nid[i],
4030 stream_tag, 0, format);
4031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 /* surrounds */
4033 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004034 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004035 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4036 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004037 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004038 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4039 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
4041 return 0;
4042}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004043EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Takashi Iwaid5191e52009-11-16 14:58:17 +01004045/**
4046 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004048int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4049 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
4051 hda_nid_t *nids = mout->dac_nids;
4052 int i;
4053
4054 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004055 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004057 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004058 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4059 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004060 snd_hda_codec_cleanup_stream(codec,
4061 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004062 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004064 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 mout->dig_out_used = 0;
4066 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004067 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 return 0;
4069}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004070EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004072/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004073 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004074 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004075
Takashi Iwai12f288b2007-08-02 15:51:59 +02004076static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004077{
4078 for (; *list; list++)
4079 if (*list == nid)
4080 return 1;
4081 return 0;
4082}
4083
Steve Longerbeam81937d32007-05-08 15:33:03 +02004084
4085/*
4086 * Sort an associated group of pins according to their sequence numbers.
4087 */
4088static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
4089 int num_pins)
4090{
4091 int i, j;
4092 short seq;
4093 hda_nid_t nid;
4094
4095 for (i = 0; i < num_pins; i++) {
4096 for (j = i + 1; j < num_pins; j++) {
4097 if (sequences[i] > sequences[j]) {
4098 seq = sequences[i];
4099 sequences[i] = sequences[j];
4100 sequences[j] = seq;
4101 nid = pins[i];
4102 pins[i] = pins[j];
4103 pins[j] = nid;
4104 }
4105 }
4106 }
4107}
4108
4109
Takashi Iwai82bc9552006-03-21 11:24:42 +01004110/*
4111 * Parse all pin widgets and store the useful pin nids to cfg
4112 *
4113 * The number of line-outs or any primary output is stored in line_outs,
4114 * and the corresponding output pins are assigned to line_out_pins[],
4115 * in the order of front, rear, CLFE, side, ...
4116 *
4117 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004118 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004119 * is detected, one of speaker of HP pins is assigned as the primary
4120 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4121 * if any analog output exists.
4122 *
4123 * The analog input pins are assigned to input_pins array.
4124 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4125 * respectively.
4126 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004127int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4128 struct auto_pin_cfg *cfg,
4129 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004130{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004131 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004132 short seq, assoc_line_out, assoc_speaker;
4133 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4134 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004135 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004136
4137 memset(cfg, 0, sizeof(*cfg));
4138
Steve Longerbeam81937d32007-05-08 15:33:03 +02004139 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4140 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004141 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004142 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004143
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004144 end_nid = codec->start_nid + codec->num_nodes;
4145 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004146 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004147 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004148 unsigned int def_conf;
4149 short assoc, loc;
4150
4151 /* read all default configuration for pin complex */
4152 if (wid_type != AC_WID_PIN)
4153 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004154 /* ignore the given nids (e.g. pc-beep returns error) */
4155 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4156 continue;
4157
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004158 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004159 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4160 continue;
4161 loc = get_defcfg_location(def_conf);
4162 switch (get_defcfg_device(def_conf)) {
4163 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004164 seq = get_defcfg_sequence(def_conf);
4165 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004166
4167 if (!(wid_caps & AC_WCAP_STEREO))
4168 if (!cfg->mono_out_pin)
4169 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004170 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004171 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004172 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004173 assoc_line_out = assoc;
4174 else if (assoc_line_out != assoc)
4175 continue;
4176 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4177 continue;
4178 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004179 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004180 cfg->line_outs++;
4181 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004182 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004183 seq = get_defcfg_sequence(def_conf);
4184 assoc = get_defcfg_association(def_conf);
4185 if (! assoc)
4186 continue;
4187 if (! assoc_speaker)
4188 assoc_speaker = assoc;
4189 else if (assoc_speaker != assoc)
4190 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004191 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4192 continue;
4193 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004194 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004195 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004196 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004197 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004198 seq = get_defcfg_sequence(def_conf);
4199 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004200 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4201 continue;
4202 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004203 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004204 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004205 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004206 case AC_JACK_MIC_IN: {
4207 int preferred, alt;
4208 if (loc == AC_JACK_LOC_FRONT) {
4209 preferred = AUTO_PIN_FRONT_MIC;
4210 alt = AUTO_PIN_MIC;
4211 } else {
4212 preferred = AUTO_PIN_MIC;
4213 alt = AUTO_PIN_FRONT_MIC;
4214 }
4215 if (!cfg->input_pins[preferred])
4216 cfg->input_pins[preferred] = nid;
4217 else if (!cfg->input_pins[alt])
4218 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004219 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004220 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004221 case AC_JACK_LINE_IN:
4222 if (loc == AC_JACK_LOC_FRONT)
4223 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4224 else
4225 cfg->input_pins[AUTO_PIN_LINE] = nid;
4226 break;
4227 case AC_JACK_CD:
4228 cfg->input_pins[AUTO_PIN_CD] = nid;
4229 break;
4230 case AC_JACK_AUX:
4231 cfg->input_pins[AUTO_PIN_AUX] = nid;
4232 break;
4233 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004234 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004235 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4236 continue;
4237 cfg->dig_out_pins[cfg->dig_outs] = nid;
4238 cfg->dig_out_type[cfg->dig_outs] =
4239 (loc == AC_JACK_LOC_HDMI) ?
4240 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4241 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004242 break;
4243 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004244 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004245 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004246 if (loc == AC_JACK_LOC_HDMI)
4247 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4248 else
4249 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004250 break;
4251 }
4252 }
4253
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004254 /* FIX-UP:
4255 * If no line-out is defined but multiple HPs are found,
4256 * some of them might be the real line-outs.
4257 */
4258 if (!cfg->line_outs && cfg->hp_outs > 1) {
4259 int i = 0;
4260 while (i < cfg->hp_outs) {
4261 /* The real HPs should have the sequence 0x0f */
4262 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4263 i++;
4264 continue;
4265 }
4266 /* Move it to the line-out table */
4267 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4268 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4269 cfg->line_outs++;
4270 cfg->hp_outs--;
4271 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4272 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4273 memmove(sequences_hp + i - 1, sequences_hp + i,
4274 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4275 }
4276 }
4277
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004278 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004279 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4280 cfg->line_outs);
4281 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4282 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004283 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4284 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02004285
Takashi Iwaif889fa92007-10-31 15:49:32 +01004286 /* if we have only one mic, make it AUTO_PIN_MIC */
4287 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4288 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4289 cfg->input_pins[AUTO_PIN_MIC] =
4290 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4291 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4292 }
4293 /* ditto for line-in */
4294 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4295 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4296 cfg->input_pins[AUTO_PIN_LINE] =
4297 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4298 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4299 }
4300
Steve Longerbeam81937d32007-05-08 15:33:03 +02004301 /*
4302 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4303 * as a primary output
4304 */
4305 if (!cfg->line_outs) {
4306 if (cfg->speaker_outs) {
4307 cfg->line_outs = cfg->speaker_outs;
4308 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4309 sizeof(cfg->speaker_pins));
4310 cfg->speaker_outs = 0;
4311 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4312 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4313 } else if (cfg->hp_outs) {
4314 cfg->line_outs = cfg->hp_outs;
4315 memcpy(cfg->line_out_pins, cfg->hp_pins,
4316 sizeof(cfg->hp_pins));
4317 cfg->hp_outs = 0;
4318 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4319 cfg->line_out_type = AUTO_PIN_HP_OUT;
4320 }
4321 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004322
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004323 /* Reorder the surround channels
4324 * ALSA sequence is front/surr/clfe/side
4325 * HDA sequence is:
4326 * 4-ch: front/surr => OK as it is
4327 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004328 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004329 */
4330 switch (cfg->line_outs) {
4331 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004332 case 4:
4333 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004334 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004335 cfg->line_out_pins[2] = nid;
4336 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004337 }
4338
Takashi Iwai82bc9552006-03-21 11:24:42 +01004339 /*
4340 * debug prints of the parsed results
4341 */
4342 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4343 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4344 cfg->line_out_pins[2], cfg->line_out_pins[3],
4345 cfg->line_out_pins[4]);
4346 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4347 cfg->speaker_outs, cfg->speaker_pins[0],
4348 cfg->speaker_pins[1], cfg->speaker_pins[2],
4349 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004350 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4351 cfg->hp_outs, cfg->hp_pins[0],
4352 cfg->hp_pins[1], cfg->hp_pins[2],
4353 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004354 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004355 if (cfg->dig_outs)
4356 snd_printd(" dig-out=0x%x/0x%x\n",
4357 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004358 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4359 " cd=0x%x, aux=0x%x\n",
4360 cfg->input_pins[AUTO_PIN_MIC],
4361 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4362 cfg->input_pins[AUTO_PIN_LINE],
4363 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4364 cfg->input_pins[AUTO_PIN_CD],
4365 cfg->input_pins[AUTO_PIN_AUX]);
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004366 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004367 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004368
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004369 return 0;
4370}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004371EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004372
Takashi Iwai4a471b72005-12-07 13:56:29 +01004373/* labels for input pins */
4374const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4375 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4376};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004377EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01004378
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380#ifdef CONFIG_PM
4381/*
4382 * power management
4383 */
4384
4385/**
4386 * snd_hda_suspend - suspend the codecs
4387 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 *
4389 * Returns 0 if successful.
4390 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004391int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004393 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Takashi Iwai0ba21762007-04-16 11:29:14 +02004395 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004396#ifdef CONFIG_SND_HDA_POWER_SAVE
4397 if (!codec->power_on)
4398 continue;
4399#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004400 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
4402 return 0;
4403}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004404EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
4406/**
4407 * snd_hda_resume - resume the codecs
4408 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 *
4410 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004411 *
4412 * This fucntion is defined only when POWER_SAVE isn't set.
4413 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 */
4415int snd_hda_resume(struct hda_bus *bus)
4416{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004417 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Takashi Iwai0ba21762007-04-16 11:29:14 +02004419 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004420 if (snd_hda_codec_needs_resume(codec))
4421 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return 0;
4424}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004425EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004426#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004427
4428/*
4429 * generic arrays
4430 */
4431
Takashi Iwaid5191e52009-11-16 14:58:17 +01004432/**
4433 * snd_array_new - get a new element from the given array
4434 * @array: the array object
4435 *
4436 * Get a new element from the given array. If it exceeds the
4437 * pre-allocated array size, re-allocate the array.
4438 *
4439 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004440 */
4441void *snd_array_new(struct snd_array *array)
4442{
4443 if (array->used >= array->alloced) {
4444 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004445 void *nlist;
4446 if (snd_BUG_ON(num >= 4096))
4447 return NULL;
4448 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004449 if (!nlist)
4450 return NULL;
4451 if (array->list) {
4452 memcpy(nlist, array->list,
4453 array->elem_size * array->alloced);
4454 kfree(array->list);
4455 }
4456 array->list = nlist;
4457 array->alloced = num;
4458 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004459 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004460}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004461EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004462
Takashi Iwaid5191e52009-11-16 14:58:17 +01004463/**
4464 * snd_array_free - free the given array elements
4465 * @array: the array object
4466 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004467void snd_array_free(struct snd_array *array)
4468{
4469 kfree(array->list);
4470 array->used = 0;
4471 array->alloced = 0;
4472 array->list = NULL;
4473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004474EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004475
Takashi Iwaid5191e52009-11-16 14:58:17 +01004476/**
4477 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4478 * @pcm: PCM caps bits
4479 * @buf: the string buffer to write
4480 * @buflen: the max buffer length
4481 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004482 * used by hda_proc.c and hda_eld.c
4483 */
4484void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4485{
4486 static unsigned int rates[] = {
4487 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4488 96000, 176400, 192000, 384000
4489 };
4490 int i, j;
4491
4492 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4493 if (pcm & (1 << i))
4494 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4495
4496 buf[j] = '\0'; /* necessary when j == 0 */
4497}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004498EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004499
Takashi Iwaid5191e52009-11-16 14:58:17 +01004500/**
4501 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4502 * @pcm: PCM caps bits
4503 * @buf: the string buffer to write
4504 * @buflen: the max buffer length
4505 *
4506 * used by hda_proc.c and hda_eld.c
4507 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004508void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4509{
4510 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4511 int i, j;
4512
4513 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4514 if (pcm & (AC_SUPPCM_BITS_8 << i))
4515 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4516
4517 buf[j] = '\0'; /* necessary when j == 0 */
4518}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004519EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004520
4521MODULE_DESCRIPTION("HDA codec core");
4522MODULE_LICENSE("GPL");