blob: 0e76ac2b2aceefe24ac0037331124c9e8d87eb65 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
32#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020033#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020034#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * vendor / preset table
38 */
39
40struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
43};
44
45/* codec vendor labels */
46static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010047 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020048 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020049 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010050 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010051 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010052 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020053 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010054 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020055 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010056 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020057 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020059 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010060 { 0x17e8, "Chrontel" },
61 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000062 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010064 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020065 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 {} /* terminator */
67};
68
Takashi Iwai1289e9e2008-11-27 15:47:11 +010069static DEFINE_MUTEX(preset_mutex);
70static LIST_HEAD(hda_preset_tables);
71
72int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
73{
74 mutex_lock(&preset_mutex);
75 list_add_tail(&preset->list, &hda_preset_tables);
76 mutex_unlock(&preset_mutex);
77 return 0;
78}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010079EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010080
81int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
82{
83 mutex_lock(&preset_mutex);
84 list_del(&preset->list);
85 mutex_unlock(&preset_mutex);
86 return 0;
87}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010088EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwaicb53c622007-08-10 17:21:45 +020090#ifdef CONFIG_SND_HDA_POWER_SAVE
91static void hda_power_work(struct work_struct *work);
92static void hda_keep_power_on(struct hda_codec *codec);
93#else
94static inline void hda_keep_power_on(struct hda_codec *codec) {}
95#endif
96
Takashi Iwaid5191e52009-11-16 14:58:17 +010097/**
98 * snd_hda_get_jack_location - Give a location string of the jack
99 * @cfg: pin default config value
100 *
101 * Parse the pin default config value and returns the string of the
102 * jack location, e.g. "Rear", "Front", etc.
103 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400104const char *snd_hda_get_jack_location(u32 cfg)
105{
106 static char *bases[7] = {
107 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
108 };
109 static unsigned char specials_idx[] = {
110 0x07, 0x08,
111 0x17, 0x18, 0x19,
112 0x37, 0x38
113 };
114 static char *specials[] = {
115 "Rear Panel", "Drive Bar",
116 "Riser", "HDMI", "ATAPI",
117 "Mobile-In", "Mobile-Out"
118 };
119 int i;
120 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
121 if ((cfg & 0x0f) < 7)
122 return bases[cfg & 0x0f];
123 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
124 if (cfg == specials_idx[i])
125 return specials[i];
126 }
127 return "UNKNOWN";
128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100129EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400130
Takashi Iwaid5191e52009-11-16 14:58:17 +0100131/**
132 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
133 * @cfg: pin default config value
134 *
135 * Parse the pin default config value and returns the string of the
136 * jack connectivity, i.e. external or internal connection.
137 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400138const char *snd_hda_get_jack_connectivity(u32 cfg)
139{
140 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
141
142 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100144EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400145
Takashi Iwaid5191e52009-11-16 14:58:17 +0100146/**
147 * snd_hda_get_jack_type - Give a type string of the jack
148 * @cfg: pin default config value
149 *
150 * Parse the pin default config value and returns the string of the
151 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
152 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400153const char *snd_hda_get_jack_type(u32 cfg)
154{
155 static char *jack_types[16] = {
156 "Line Out", "Speaker", "HP Out", "CD",
157 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
158 "Line In", "Aux", "Mic", "Telephony",
159 "SPDIF In", "Digitial In", "Reserved", "Other"
160 };
161
162 return jack_types[(cfg & AC_DEFCFG_DEVICE)
163 >> AC_DEFCFG_DEVICE_SHIFT];
164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100165EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400166
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100167/*
168 * Compose a 32bit command word to be sent to the HD-audio controller
169 */
170static inline unsigned int
171make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
172 unsigned int verb, unsigned int parm)
173{
174 u32 val;
175
Takashi Iwai82e1b802009-07-17 12:47:34 +0200176 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
177 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800178 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
179 codec->addr, direct, nid, verb, parm);
180 return ~0;
181 }
182
183 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184 val |= (u32)direct << 27;
185 val |= (u32)nid << 20;
186 val |= verb << 8;
187 val |= parm;
188 return val;
189}
190
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200191/*
192 * Send and receive a verb
193 */
194static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
195 unsigned int *res)
196{
197 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200198 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200199
Wu Fengguang6430aee2009-07-17 16:49:19 +0800200 if (cmd == ~0)
201 return -1;
202
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200203 if (res)
204 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200205 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206 snd_hda_power_up(codec);
207 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200209 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800210 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200211 mutex_unlock(&bus->cmd_mutex);
212 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200213 if (res && *res == -1 && bus->rirb_error) {
214 if (bus->response_reset) {
215 snd_printd("hda_codec: resetting BUS due to "
216 "fatal communication error\n");
217 bus->ops.bus_reset(bus);
218 }
219 goto again;
220 }
221 /* clear reset-flag when the communication gets recovered */
222 if (!err)
223 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224 return err;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/**
228 * snd_hda_codec_read - send a command and get the response
229 * @codec: the HDA codec
230 * @nid: NID to send the command
231 * @direct: direct flag
232 * @verb: the verb to send
233 * @parm: the parameter for the verb
234 *
235 * Send a single command and read the corresponding response.
236 *
237 * Returns the obtained response value, or -1 for an error.
238 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200239unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
240 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned int verb, unsigned int parm)
242{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200243 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
244 unsigned int res;
245 codec_exec_verb(codec, cmd, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return res;
247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100248EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/**
251 * snd_hda_codec_write - send a single command without waiting for response
252 * @codec: the HDA codec
253 * @nid: NID to send the command
254 * @direct: direct flag
255 * @verb: the verb to send
256 * @parm: the parameter for the verb
257 *
258 * Send a single command without waiting for response.
259 *
260 * Returns 0 if successful, or a negative error code.
261 */
262int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
263 unsigned int verb, unsigned int parm)
264{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200265 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100266 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200267 return codec_exec_verb(codec, cmd,
268 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100270EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/**
273 * snd_hda_sequence_write - sequence writes
274 * @codec: the HDA codec
275 * @seq: VERB array to send
276 *
277 * Send the commands sequentially from the given array.
278 * The array must be terminated with NID=0.
279 */
280void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
281{
282 for (; seq->nid; seq++)
283 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100285EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/**
288 * snd_hda_get_sub_nodes - get the range of sub nodes
289 * @codec: the HDA codec
290 * @nid: NID to parse
291 * @start_id: the pointer to store the start NID
292 *
293 * Parse the NID and store the start NID of its sub-nodes.
294 * Returns the number of sub-nodes.
295 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200296int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
297 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned int parm;
300
301 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200302 if (parm == -1)
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *start_id = (parm >> 16) & 0x7fff;
305 return (int)(parm & 0x7fff);
306}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100307EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
310 * snd_hda_get_connections - get connection list
311 * @codec: the HDA codec
312 * @nid: NID to parse
313 * @conn_list: connection list array
314 * @max_conns: max. number of connections to store
315 *
316 * Parses the connection list of the given widget and stores the list
317 * of NIDs.
318 *
319 * Returns the number of connections, or a negative error code.
320 */
321int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
322 hda_nid_t *conn_list, int max_conns)
323{
324 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100325 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200327 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100328 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Takashi Iwaida3cec32008-08-08 17:12:14 +0200330 if (snd_BUG_ON(!conn_list || max_conns <= 0))
331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200333 wcaps = get_wcaps(codec, nid);
334 if (!(wcaps & AC_WCAP_CONN_LIST) &&
335 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200336 snd_printk(KERN_WARNING "hda_codec: "
337 "connection list not available for 0x%x\n", nid);
338 return -EINVAL;
339 }
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
342 if (parm & AC_CLIST_LONG) {
343 /* long form */
344 shift = 16;
345 num_elems = 2;
346 } else {
347 /* short form */
348 shift = 8;
349 num_elems = 4;
350 }
351 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mask = (1 << (shift-1)) - 1;
353
Takashi Iwai0ba21762007-04-16 11:29:14 +0200354 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0; /* no connection */
356
357 if (conn_len == 1) {
358 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200359 parm = snd_hda_codec_read(codec, nid, 0,
360 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200361 if (parm == -1 && codec->bus->rirb_error)
362 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 conn_list[0] = parm & mask;
364 return 1;
365 }
366
367 /* multi connection */
368 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100369 prev_nid = 0;
370 for (i = 0; i < conn_len; i++) {
371 int range_val;
372 hda_nid_t val, n;
373
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200374 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100375 parm = snd_hda_codec_read(codec, nid, 0,
376 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200377 if (parm == -1 && codec->bus->rirb_error)
378 return -EIO;
379 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200380 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100381 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200382 if (val == 0) {
383 snd_printk(KERN_WARNING "hda_codec: "
384 "invalid CONNECT_LIST verb %x[%i]:%x\n",
385 nid, i, parm);
386 return 0;
387 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100388 parm >>= shift;
389 if (range_val) {
390 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200391 if (!prev_nid || prev_nid >= val) {
392 snd_printk(KERN_WARNING "hda_codec: "
393 "invalid dep_range_val %x:%x\n",
394 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100395 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100397 for (n = prev_nid + 1; n <= val; n++) {
398 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200399 snd_printk(KERN_ERR
400 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100401 return -EINVAL;
402 }
403 conn_list[conns++] = n;
404 }
405 } else {
406 if (conns >= max_conns) {
407 snd_printk(KERN_ERR "Too many connections\n");
408 return -EINVAL;
409 }
410 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100412 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 return conns;
415}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100416EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418
419/**
420 * snd_hda_queue_unsol_event - add an unsolicited event to queue
421 * @bus: the BUS
422 * @res: unsolicited event (lower 32bit of RIRB entry)
423 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
424 *
425 * Adds the given event to the queue. The events are processed in
426 * the workqueue asynchronously. Call this function in the interrupt
427 * hanlder when RIRB receives an unsolicited event.
428 *
429 * Returns 0 if successful, or a negative error code.
430 */
431int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
432{
433 struct hda_bus_unsolicited *unsol;
434 unsigned int wp;
435
Takashi Iwai0ba21762007-04-16 11:29:14 +0200436 unsol = bus->unsol;
437 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439
440 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
441 unsol->wp = wp;
442
443 wp <<= 1;
444 unsol->queue[wp] = res;
445 unsol->queue[wp + 1] = res_ex;
446
Takashi Iwai6acaed32009-01-12 10:09:24 +0100447 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return 0;
450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100451EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800454 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
David Howellsc4028952006-11-22 14:57:56 +0000456static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
David Howellsc4028952006-11-22 14:57:56 +0000458 struct hda_bus_unsolicited *unsol =
459 container_of(work, struct hda_bus_unsolicited, work);
460 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct hda_codec *codec;
462 unsigned int rp, caddr, res;
463
464 while (unsol->rp != unsol->wp) {
465 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
466 unsol->rp = rp;
467 rp <<= 1;
468 res = unsol->queue[rp];
469 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200470 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 continue;
472 codec = bus->caddr_tbl[caddr & 0x0f];
473 if (codec && codec->patch_ops.unsol_event)
474 codec->patch_ops.unsol_event(codec, res);
475 }
476}
477
478/*
479 * initialize unsolicited queue
480 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200481static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct hda_bus_unsolicited *unsol;
484
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100485 if (bus->unsol) /* already initialized */
486 return 0;
487
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200488 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200489 if (!unsol) {
490 snd_printk(KERN_ERR "hda_codec: "
491 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -ENOMEM;
493 }
David Howellsc4028952006-11-22 14:57:56 +0000494 INIT_WORK(&unsol->work, process_unsol_events);
495 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 bus->unsol = unsol;
497 return 0;
498}
499
500/*
501 * destructor
502 */
503static void snd_hda_codec_free(struct hda_codec *codec);
504
505static int snd_hda_bus_free(struct hda_bus *bus)
506{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200507 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Takashi Iwai0ba21762007-04-16 11:29:14 +0200509 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100511 if (bus->workq)
512 flush_workqueue(bus->workq);
513 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200515 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 snd_hda_codec_free(codec);
517 }
518 if (bus->ops.private_free)
519 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100520 if (bus->workq)
521 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 kfree(bus);
523 return 0;
524}
525
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100526static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100529 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return snd_hda_bus_free(bus);
531}
532
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200533#ifdef CONFIG_SND_HDA_HWDEP
534static int snd_hda_bus_dev_register(struct snd_device *device)
535{
536 struct hda_bus *bus = device->device_data;
537 struct hda_codec *codec;
538 list_for_each_entry(codec, &bus->codec_list, list) {
539 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100540 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200541 }
542 return 0;
543}
544#else
545#define snd_hda_bus_dev_register NULL
546#endif
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/**
549 * snd_hda_bus_new - create a HDA bus
550 * @card: the card entry
551 * @temp: the template for hda_bus information
552 * @busp: the pointer to store the created bus instance
553 *
554 * Returns 0 if successful, or a negative error code.
555 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100556int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200557 const struct hda_bus_template *temp,
558 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct hda_bus *bus;
561 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100562 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200563 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .dev_free = snd_hda_bus_dev_free,
565 };
566
Takashi Iwaida3cec32008-08-08 17:12:14 +0200567 if (snd_BUG_ON(!temp))
568 return -EINVAL;
569 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (busp)
573 *busp = NULL;
574
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200575 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (bus == NULL) {
577 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
578 return -ENOMEM;
579 }
580
581 bus->card = card;
582 bus->private_data = temp->private_data;
583 bus->pci = temp->pci;
584 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100585 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 bus->ops = temp->ops;
587
Ingo Molnar62932df2006-01-16 16:34:20 +0100588 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 INIT_LIST_HEAD(&bus->codec_list);
590
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100591 snprintf(bus->workq_name, sizeof(bus->workq_name),
592 "hd-audio%d", card->number);
593 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100594 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100595 snd_printk(KERN_ERR "cannot create workqueue %s\n",
596 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100597 kfree(bus);
598 return -ENOMEM;
599 }
600
Takashi Iwai0ba21762007-04-16 11:29:14 +0200601 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
602 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 snd_hda_bus_free(bus);
604 return err;
605 }
606 if (busp)
607 *busp = bus;
608 return 0;
609}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100610EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Takashi Iwai82467612007-07-27 19:15:54 +0200612#ifdef CONFIG_SND_HDA_GENERIC
613#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200614 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200615#else
616#define is_generic_config(codec) 0
617#endif
618
Takashi Iwai645f10c2008-11-28 15:07:37 +0100619#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100620#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
621#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100622#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100623#endif
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*
626 * find a matching codec preset
627 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200628static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200629find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100631 struct hda_codec_preset_list *tbl;
632 const struct hda_codec_preset *preset;
633 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Takashi Iwai82467612007-07-27 19:15:54 +0200635 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100636 return NULL; /* use the generic parser */
637
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100638 again:
639 mutex_lock(&preset_mutex);
640 list_for_each_entry(tbl, &hda_preset_tables, list) {
641 if (!try_module_get(tbl->owner)) {
642 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
643 continue;
644 }
645 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100647 if (preset->afg && preset->afg != codec->afg)
648 continue;
649 if (preset->mfg && preset->mfg != codec->mfg)
650 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200651 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200653 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200654 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100655 preset->rev == codec->revision_id)) {
656 mutex_unlock(&preset_mutex);
657 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100661 module_put(tbl->owner);
662 }
663 mutex_unlock(&preset_mutex);
664
665 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
666 char name[32];
667 if (!mod_requested)
668 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
669 codec->vendor_id);
670 else
671 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
672 (codec->vendor_id >> 16) & 0xffff);
673 request_module(name);
674 mod_requested++;
675 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 return NULL;
678}
679
680/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200681 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200683static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 const struct hda_vendor_id *c;
686 const char *vendor = NULL;
687 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200688 char tmp[16];
689
690 if (codec->vendor_name)
691 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 for (c = hda_vendor_ids; c->id; c++) {
694 if (c->id == vendor_id) {
695 vendor = c->name;
696 break;
697 }
698 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200699 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 sprintf(tmp, "Generic %04x", vendor_id);
701 vendor = tmp;
702 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200703 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
704 if (!codec->vendor_name)
705 return -ENOMEM;
706
707 get_chip_name:
708 if (codec->chip_name)
709 return 0;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200712 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
713 else {
714 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
715 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
716 }
717 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200718 return -ENOMEM;
719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200723 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100725static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200727 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 hda_nid_t nid;
729
730 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
731 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200732 function_id = snd_hda_param_read(codec, nid,
Pascal de Bruijn234b4342009-03-23 11:15:59 +0100733 AC_PAR_FUNCTION_TYPE) & 0xff;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200734 switch (function_id) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200735 case AC_GRP_AUDIO_FUNCTION:
736 codec->afg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200737 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200738 break;
739 case AC_GRP_MODEM_FUNCTION:
740 codec->mfg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200741 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200742 break;
743 default:
744 break;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100750 * read widget caps for each widget and store in cache
751 */
752static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
753{
754 int i;
755 hda_nid_t nid;
756
757 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
758 &codec->start_nid);
759 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200760 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100761 return -ENOMEM;
762 nid = codec->start_nid;
763 for (i = 0; i < codec->num_nodes; i++, nid++)
764 codec->wcaps[i] = snd_hda_param_read(codec, nid,
765 AC_PAR_AUDIO_WIDGET_CAP);
766 return 0;
767}
768
Takashi Iwai3be14142009-02-20 14:11:16 +0100769/* read all pin default configurations and save codec->init_pins */
770static int read_pin_defaults(struct hda_codec *codec)
771{
772 int i;
773 hda_nid_t nid = codec->start_nid;
774
775 for (i = 0; i < codec->num_nodes; i++, nid++) {
776 struct hda_pincfg *pin;
777 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200778 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100779 if (wid_type != AC_WID_PIN)
780 continue;
781 pin = snd_array_new(&codec->init_pins);
782 if (!pin)
783 return -ENOMEM;
784 pin->nid = nid;
785 pin->cfg = snd_hda_codec_read(codec, nid, 0,
786 AC_VERB_GET_CONFIG_DEFAULT, 0);
787 }
788 return 0;
789}
790
791/* look up the given pin config list and return the item matching with NID */
792static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
793 struct snd_array *array,
794 hda_nid_t nid)
795{
796 int i;
797 for (i = 0; i < array->used; i++) {
798 struct hda_pincfg *pin = snd_array_elem(array, i);
799 if (pin->nid == nid)
800 return pin;
801 }
802 return NULL;
803}
804
805/* write a config value for the given NID */
806static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
807 unsigned int cfg)
808{
809 int i;
810 for (i = 0; i < 4; i++) {
811 snd_hda_codec_write(codec, nid, 0,
812 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
813 cfg & 0xff);
814 cfg >>= 8;
815 }
816}
817
818/* set the current pin config value for the given NID.
819 * the value is cached, and read via snd_hda_codec_get_pincfg()
820 */
821int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
822 hda_nid_t nid, unsigned int cfg)
823{
824 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100825 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100826
Takashi Iwaib82855a2009-12-27 11:24:56 +0100827 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
828 return -EINVAL;
829
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100830 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100831 pin = look_up_pincfg(codec, list, nid);
832 if (!pin) {
833 pin = snd_array_new(list);
834 if (!pin)
835 return -ENOMEM;
836 pin->nid = nid;
837 }
838 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100839
840 /* change only when needed; e.g. if the pincfg is already present
841 * in user_pins[], don't write it
842 */
843 cfg = snd_hda_codec_get_pincfg(codec, nid);
844 if (oldcfg != cfg)
845 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100846 return 0;
847}
848
Takashi Iwaid5191e52009-11-16 14:58:17 +0100849/**
850 * snd_hda_codec_set_pincfg - Override a pin default configuration
851 * @codec: the HDA codec
852 * @nid: NID to set the pin config
853 * @cfg: the pin default config value
854 *
855 * Override a pin default configuration value in the cache.
856 * This value can be read by snd_hda_codec_get_pincfg() in a higher
857 * priority than the real hardware value.
858 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100859int snd_hda_codec_set_pincfg(struct hda_codec *codec,
860 hda_nid_t nid, unsigned int cfg)
861{
Takashi Iwai346ff702009-02-23 09:42:57 +0100862 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100863}
864EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
865
Takashi Iwaid5191e52009-11-16 14:58:17 +0100866/**
867 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
868 * @codec: the HDA codec
869 * @nid: NID to get the pin config
870 *
871 * Get the current pin config value of the given pin NID.
872 * If the pincfg value is cached or overridden via sysfs or driver,
873 * returns the cached value.
874 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100875unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
876{
877 struct hda_pincfg *pin;
878
Takashi Iwai3be14142009-02-20 14:11:16 +0100879#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100880 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100881 if (pin)
882 return pin->cfg;
883#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100884 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
885 if (pin)
886 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100887 pin = look_up_pincfg(codec, &codec->init_pins, nid);
888 if (pin)
889 return pin->cfg;
890 return 0;
891}
892EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
893
894/* restore all current pin configs */
895static void restore_pincfgs(struct hda_codec *codec)
896{
897 int i;
898 for (i = 0; i < codec->init_pins.used; i++) {
899 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
900 set_pincfg(codec, pin->nid,
901 snd_hda_codec_get_pincfg(codec, pin->nid));
902 }
903}
Takashi Iwai54d17402005-11-21 16:33:22 +0100904
Takashi Iwai92ee6162009-12-27 11:18:59 +0100905/**
906 * snd_hda_shutup_pins - Shut up all pins
907 * @codec: the HDA codec
908 *
909 * Clear all pin controls to shup up before suspend for avoiding click noise.
910 * The controls aren't cached so that they can be resumed properly.
911 */
912void snd_hda_shutup_pins(struct hda_codec *codec)
913{
914 int i;
915 for (i = 0; i < codec->init_pins.used; i++) {
916 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
917 /* use read here for syncing after issuing each verb */
918 snd_hda_codec_read(codec, pin->nid, 0,
919 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
920 }
921}
922EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
923
Takashi Iwai01751f52007-08-10 16:59:39 +0200924static void init_hda_cache(struct hda_cache_rec *cache,
925 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200926static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200927
Takashi Iwai3be14142009-02-20 14:11:16 +0100928/* restore the initial pin cfgs and release all pincfg lists */
929static void restore_init_pincfgs(struct hda_codec *codec)
930{
Takashi Iwai346ff702009-02-23 09:42:57 +0100931 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100932 * so that only the values in init_pins are restored
933 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100934 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100935#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100936 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100937#endif
938 restore_pincfgs(codec);
939 snd_array_free(&codec->init_pins);
940}
941
Takashi Iwai54d17402005-11-21 16:33:22 +0100942/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * codec destructor
944 */
945static void snd_hda_codec_free(struct hda_codec *codec)
946{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200947 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return;
Takashi Iwai3be14142009-02-20 14:11:16 +0100949 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200950#ifdef CONFIG_SND_HDA_POWER_SAVE
951 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100952 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200955 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100956 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 codec->bus->caddr_tbl[codec->addr] = NULL;
958 if (codec->patch_ops.free)
959 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100960 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200961 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200962 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200963 kfree(codec->vendor_name);
964 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200965 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100966 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 kfree(codec);
968}
969
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100970static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
971 unsigned int power_state);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/**
974 * snd_hda_codec_new - create a HDA codec
975 * @bus: the bus to assign
976 * @codec_addr: the codec address
977 * @codecp: the pointer to store the generated codec
978 *
979 * Returns 0 if successful, or a negative error code.
980 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +0100981int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
982 unsigned int codec_addr,
983 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200986 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int err;
988
Takashi Iwaida3cec32008-08-08 17:12:14 +0200989 if (snd_BUG_ON(!bus))
990 return -EINVAL;
991 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
992 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200995 snd_printk(KERN_ERR "hda_codec: "
996 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -EBUSY;
998 }
999
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001000 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (codec == NULL) {
1002 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1003 return -ENOMEM;
1004 }
1005
1006 codec->bus = bus;
1007 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001008 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001009 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001010 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001011 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001012 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1013 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001014 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001015 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001016 if (codec->bus->modelname) {
1017 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1018 if (!codec->modelname) {
1019 snd_hda_codec_free(codec);
1020 return -ENODEV;
1021 }
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Takashi Iwaicb53c622007-08-10 17:21:45 +02001024#ifdef CONFIG_SND_HDA_POWER_SAVE
1025 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1026 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1027 * the caller has to power down appropriatley after initialization
1028 * phase.
1029 */
1030 hda_keep_power_on(codec);
1031#endif
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 list_add_tail(&codec->list, &bus->codec_list);
1034 bus->caddr_tbl[codec_addr] = codec;
1035
Takashi Iwai0ba21762007-04-16 11:29:14 +02001036 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1037 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001038 if (codec->vendor_id == -1)
1039 /* read again, hopefully the access method was corrected
1040 * in the last read...
1041 */
1042 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1043 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001044 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1045 AC_PAR_SUBSYSTEM_ID);
1046 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1047 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001049 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001050 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001051 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001052 err = -ENODEV;
1053 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Takashi Iwai3be14142009-02-20 14:11:16 +01001056 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1057 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001058 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001059 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001060 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001061 err = read_pin_defaults(codec);
1062 if (err < 0)
1063 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001064
Takashi Iwai0ba21762007-04-16 11:29:14 +02001065 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001066 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001067 codec->subsystem_id =
1068 snd_hda_codec_read(codec, nid, 0,
1069 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001070 }
1071
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001072 /* power-up all before initialization */
1073 hda_set_power_state(codec,
1074 codec->afg ? codec->afg : codec->mfg,
1075 AC_PWRST_D0);
1076
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001077 snd_hda_codec_proc_new(codec);
1078
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001079 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001080
1081 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1082 codec->subsystem_id, codec->revision_id);
1083 snd_component_add(codec->bus->card, component);
1084
1085 if (codecp)
1086 *codecp = codec;
1087 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001088
1089 error:
1090 snd_hda_codec_free(codec);
1091 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001093EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001094
Takashi Iwaid5191e52009-11-16 14:58:17 +01001095/**
1096 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1097 * @codec: the HDA codec
1098 *
1099 * Start parsing of the given codec tree and (re-)initialize the whole
1100 * patch instance.
1101 *
1102 * Returns 0 if successful or a negative error code.
1103 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001104int snd_hda_codec_configure(struct hda_codec *codec)
1105{
1106 int err;
1107
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001108 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001109 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001110 err = get_codec_name(codec);
1111 if (err < 0)
1112 return err;
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Takashi Iwai82467612007-07-27 19:15:54 +02001115 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001117 goto patched;
1118 }
Takashi Iwai82467612007-07-27 19:15:54 +02001119 if (codec->preset && codec->preset->patch) {
1120 err = codec->preset->patch(codec);
1121 goto patched;
1122 }
1123
1124 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001125 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001126 if (err < 0)
1127 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001128
1129 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001130 if (!err && codec->patch_ops.unsol_event)
1131 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001132 /* audio codec should override the mixer name */
1133 if (!err && (codec->afg || !*codec->bus->card->mixername))
1134 snprintf(codec->bus->card->mixername,
1135 sizeof(codec->bus->card->mixername),
1136 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001139EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141/**
1142 * snd_hda_codec_setup_stream - set up the codec for streaming
1143 * @codec: the CODEC to set up
1144 * @nid: the NID to set up
1145 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1146 * @channel_id: channel id to pass, zero based.
1147 * @format: stream format.
1148 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001149void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1150 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int channel_id, int format)
1152{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001153 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001154 return;
1155
Takashi Iwai0ba21762007-04-16 11:29:14 +02001156 snd_printdd("hda_codec_setup_stream: "
1157 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 nid, stream_tag, channel_id, format);
1159 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1160 (stream_tag << 4) | channel_id);
1161 msleep(1);
1162 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1163}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001164EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Takashi Iwaid5191e52009-11-16 14:58:17 +01001166/**
1167 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1168 * @codec: the CODEC to clean up
1169 * @nid: the NID to clean up
1170 */
Takashi Iwai888afa12008-03-18 09:57:50 +01001171void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1172{
1173 if (!nid)
1174 return;
1175
1176 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1177 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1178#if 0 /* keep the format */
1179 msleep(1);
1180 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1181#endif
1182}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001183EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185/*
1186 * amp access functions
1187 */
1188
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001189/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001190#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001191#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001192#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1193#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001195#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001198static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001199 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Takashi Iwai01751f52007-08-10 16:59:39 +02001201 memset(cache, 0, sizeof(*cache));
1202 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001203 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001204}
1205
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001206static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001207{
Takashi Iwai603c4012008-07-30 15:01:44 +02001208 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +02001212static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1213 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwai01751f52007-08-10 16:59:39 +02001215 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1216 u16 cur = cache->hash[idx];
1217 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001220 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (info->key == key)
1222 return info;
1223 cur = info->next;
1224 }
1225
1226 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +02001227 info = snd_array_new(&cache->buf);
Takashi Iwaic2174292008-11-07 00:23:30 +01001228 if (!info)
1229 return NULL;
Takashi Iwaif43aa022008-11-10 16:24:26 +01001230 cur = snd_array_index(&cache->buf, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +02001232 info->val = 0;
1233 info->next = cache->hash[idx];
1234 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 return info;
1237}
1238
Takashi Iwai01751f52007-08-10 16:59:39 +02001239/* query and allocate an amp hash entry */
1240static inline struct hda_amp_info *
1241get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1242{
1243 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1244}
1245
Takashi Iwaid5191e52009-11-16 14:58:17 +01001246/**
1247 * query_amp_caps - query AMP capabilities
1248 * @codec: the HD-auio codec
1249 * @nid: the NID to query
1250 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1251 *
1252 * Query AMP capabilities for the given widget and direction.
1253 * Returns the obtained capability bits.
1254 *
1255 * When cap bits have been already read, this doesn't read again but
1256 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001258u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001260 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Takashi Iwai0ba21762007-04-16 11:29:14 +02001262 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1263 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001265 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001266 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001268 info->amp_caps = snd_hda_param_read(codec, nid,
1269 direction == HDA_OUTPUT ?
1270 AC_PAR_AMP_OUT_CAP :
1271 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001272 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001273 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 return info->amp_caps;
1276}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001277EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Takashi Iwaid5191e52009-11-16 14:58:17 +01001279/**
1280 * snd_hda_override_amp_caps - Override the AMP capabilities
1281 * @codec: the CODEC to clean up
1282 * @nid: the NID to clean up
1283 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1284 * @caps: the capability bits to set
1285 *
1286 * Override the cached AMP caps bits value by the given one.
1287 * This function is useful if the driver needs to adjust the AMP ranges,
1288 * e.g. limit to 0dB, etc.
1289 *
1290 * Returns zero if successful or a negative error code.
1291 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001292int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1293 unsigned int caps)
1294{
1295 struct hda_amp_info *info;
1296
1297 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1298 if (!info)
1299 return -EINVAL;
1300 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001301 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001302 return 0;
1303}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001304EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001305
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001306static unsigned int
1307query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1308 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001309{
1310 struct hda_amp_info *info;
1311
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001312 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001313 if (!info)
1314 return 0;
1315 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001316 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001317 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001318 }
1319 return info->amp_caps;
1320}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001321
1322static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1323{
1324 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1325}
1326
Takashi Iwaid5191e52009-11-16 14:58:17 +01001327/**
1328 * snd_hda_query_pin_caps - Query PIN capabilities
1329 * @codec: the HD-auio codec
1330 * @nid: the NID to query
1331 *
1332 * Query PIN capabilities for the given widget.
1333 * Returns the obtained capability bits.
1334 *
1335 * When cap bits have been already read, this doesn't read again but
1336 * returns the cached value.
1337 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001338u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1339{
1340 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1341 read_pin_cap);
1342}
Takashi Iwai1327a322009-03-23 13:07:47 +01001343EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1344
Wu Fengguang864f92b2009-11-18 12:38:02 +08001345/**
1346 * snd_hda_pin_sense - execute pin sense measurement
1347 * @codec: the CODEC to sense
1348 * @nid: the pin NID to sense
1349 *
1350 * Execute necessary pin sense measurement and return its Presence Detect,
1351 * Impedance, ELD Valid etc. status bits.
1352 */
1353u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1354{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001355 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001356
Takashi Iwai729d55b2009-12-25 22:49:01 +01001357 if (!codec->no_trigger_sense) {
1358 pincap = snd_hda_query_pin_caps(codec, nid);
1359 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001360 snd_hda_codec_read(codec, nid, 0,
1361 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001362 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001363 return snd_hda_codec_read(codec, nid, 0,
1364 AC_VERB_GET_PIN_SENSE, 0);
1365}
1366EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1367
1368/**
1369 * snd_hda_jack_detect - query pin Presence Detect status
1370 * @codec: the CODEC to sense
1371 * @nid: the pin NID to sense
1372 *
1373 * Query and return the pin's Presence Detect status.
1374 */
1375int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1376{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001377 u32 sense = snd_hda_pin_sense(codec, nid);
1378 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001379}
1380EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382/*
1383 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001384 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001386static unsigned int get_vol_mute(struct hda_codec *codec,
1387 struct hda_amp_info *info, hda_nid_t nid,
1388 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 u32 val, parm;
1391
Takashi Iwai01751f52007-08-10 16:59:39 +02001392 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001393 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1396 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1397 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001398 val = snd_hda_codec_read(codec, nid, 0,
1399 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001401 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001402 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001406 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001408static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001409 hda_nid_t nid, int ch, int direction, int index,
1410 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 u32 parm;
1413
1414 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1415 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1416 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1417 parm |= val;
1418 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001419 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Takashi Iwaid5191e52009-11-16 14:58:17 +01001422/**
1423 * snd_hda_codec_amp_read - Read AMP value
1424 * @codec: HD-audio codec
1425 * @nid: NID to read the AMP value
1426 * @ch: channel (left=0 or right=1)
1427 * @direction: #HDA_INPUT or #HDA_OUTPUT
1428 * @index: the index value (only for input direction)
1429 *
1430 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
Takashi Iwai834be882006-03-01 14:16:17 +01001432int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1433 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001435 struct hda_amp_info *info;
1436 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1437 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001439 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001441EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Takashi Iwaid5191e52009-11-16 14:58:17 +01001443/**
1444 * snd_hda_codec_amp_update - update the AMP value
1445 * @codec: HD-audio codec
1446 * @nid: NID to read the AMP value
1447 * @ch: channel (left=0 or right=1)
1448 * @direction: #HDA_INPUT or #HDA_OUTPUT
1449 * @idx: the index value (only for input direction)
1450 * @mask: bit mask to set
1451 * @val: the bits value to set
1452 *
1453 * Update the AMP value with a bit mask.
1454 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001455 */
Takashi Iwai834be882006-03-01 14:16:17 +01001456int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1457 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001459 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001460
Takashi Iwai0ba21762007-04-16 11:29:14 +02001461 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1462 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001464 val &= mask;
1465 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001466 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001468 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return 1;
1470}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001471EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Takashi Iwaid5191e52009-11-16 14:58:17 +01001473/**
1474 * snd_hda_codec_amp_stereo - update the AMP stereo values
1475 * @codec: HD-audio codec
1476 * @nid: NID to read the AMP value
1477 * @direction: #HDA_INPUT or #HDA_OUTPUT
1478 * @idx: the index value (only for input direction)
1479 * @mask: bit mask to set
1480 * @val: the bits value to set
1481 *
1482 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1483 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001484 */
1485int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1486 int direction, int idx, int mask, int val)
1487{
1488 int ch, ret = 0;
1489 for (ch = 0; ch < 2; ch++)
1490 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1491 idx, mask, val);
1492 return ret;
1493}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001494EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001495
Takashi Iwaicb53c622007-08-10 17:21:45 +02001496#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001497/**
1498 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1499 * @codec: HD-audio codec
1500 *
1501 * Resume the all amp commands from the cache.
1502 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001503void snd_hda_codec_resume_amp(struct hda_codec *codec)
1504{
Takashi Iwai603c4012008-07-30 15:01:44 +02001505 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001506 int i;
1507
Takashi Iwai603c4012008-07-30 15:01:44 +02001508 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001509 u32 key = buffer->head.key;
1510 hda_nid_t nid;
1511 unsigned int idx, dir, ch;
1512 if (!key)
1513 continue;
1514 nid = key & 0xff;
1515 idx = (key >> 16) & 0xff;
1516 dir = (key >> 24) & 0xff;
1517 for (ch = 0; ch < 2; ch++) {
1518 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1519 continue;
1520 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1521 buffer->vol[ch]);
1522 }
1523 }
1524}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001525EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001526#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Takashi Iwaid5191e52009-11-16 14:58:17 +01001528/**
1529 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1530 *
1531 * The control element is supposed to have the private_value field
1532 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1533 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001534int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1535 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1538 u16 nid = get_amp_nid(kcontrol);
1539 u8 chs = get_amp_channels(kcontrol);
1540 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001541 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 u32 caps;
1543
1544 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001545 /* num steps */
1546 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1547 if (!caps) {
1548 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001549 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1550 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return -EINVAL;
1552 }
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001553 if (ofs < caps)
1554 caps -= ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1556 uinfo->count = chs == 3 ? 2 : 1;
1557 uinfo->value.integer.min = 0;
1558 uinfo->value.integer.max = caps;
1559 return 0;
1560}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001561EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001563
1564static inline unsigned int
1565read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1566 int ch, int dir, int idx, unsigned int ofs)
1567{
1568 unsigned int val;
1569 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1570 val &= HDA_AMP_VOLMASK;
1571 if (val >= ofs)
1572 val -= ofs;
1573 else
1574 val = 0;
1575 return val;
1576}
1577
1578static inline int
1579update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1580 int ch, int dir, int idx, unsigned int ofs,
1581 unsigned int val)
1582{
1583 if (val > 0)
1584 val += ofs;
1585 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1586 HDA_AMP_VOLMASK, val);
1587}
1588
Takashi Iwaid5191e52009-11-16 14:58:17 +01001589/**
1590 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1591 *
1592 * The control element is supposed to have the private_value field
1593 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1594 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001595int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1596 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1599 hda_nid_t nid = get_amp_nid(kcontrol);
1600 int chs = get_amp_channels(kcontrol);
1601 int dir = get_amp_direction(kcontrol);
1602 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001603 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 long *valp = ucontrol->value.integer.value;
1605
1606 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001607 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001609 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return 0;
1611}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001612EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Takashi Iwaid5191e52009-11-16 14:58:17 +01001614/**
1615 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1616 *
1617 * The control element is supposed to have the private_value field
1618 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1619 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001620int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1621 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1624 hda_nid_t nid = get_amp_nid(kcontrol);
1625 int chs = get_amp_channels(kcontrol);
1626 int dir = get_amp_direction(kcontrol);
1627 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001628 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 long *valp = ucontrol->value.integer.value;
1630 int change = 0;
1631
Takashi Iwaicb53c622007-08-10 17:21:45 +02001632 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001633 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001634 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001635 valp++;
1636 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001637 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001638 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001639 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return change;
1641}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001642EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Takashi Iwaid5191e52009-11-16 14:58:17 +01001644/**
1645 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1646 *
1647 * The control element is supposed to have the private_value field
1648 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1649 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001650int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1651 unsigned int size, unsigned int __user *_tlv)
1652{
1653 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1654 hda_nid_t nid = get_amp_nid(kcontrol);
1655 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001656 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001657 u32 caps, val1, val2;
1658
1659 if (size < 4 * sizeof(unsigned int))
1660 return -ENOMEM;
1661 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001662 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1663 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001664 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001665 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001666 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001667 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1668 return -EFAULT;
1669 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1670 return -EFAULT;
1671 if (put_user(val1, _tlv + 2))
1672 return -EFAULT;
1673 if (put_user(val2, _tlv + 3))
1674 return -EFAULT;
1675 return 0;
1676}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001677EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001678
Takashi Iwaid5191e52009-11-16 14:58:17 +01001679/**
1680 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1681 * @codec: HD-audio codec
1682 * @nid: NID of a reference widget
1683 * @dir: #HDA_INPUT or #HDA_OUTPUT
1684 * @tlv: TLV data to be stored, at least 4 elements
1685 *
1686 * Set (static) TLV data for a virtual master volume using the AMP caps
1687 * obtained from the reference NID.
1688 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001689 */
1690void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1691 unsigned int *tlv)
1692{
1693 u32 caps;
1694 int nums, step;
1695
1696 caps = query_amp_caps(codec, nid, dir);
1697 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1698 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1699 step = (step + 1) * 25;
1700 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1701 tlv[1] = 2 * sizeof(unsigned int);
1702 tlv[2] = -nums * step;
1703 tlv[3] = step;
1704}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001705EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001706
1707/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001708static struct snd_kcontrol *
1709_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1710 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001711{
1712 struct snd_ctl_elem_id id;
1713 memset(&id, 0, sizeof(id));
1714 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001715 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001716 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1717 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001718 strcpy(id.name, name);
1719 return snd_ctl_find_id(codec->bus->card, &id);
1720}
1721
Takashi Iwaid5191e52009-11-16 14:58:17 +01001722/**
1723 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1724 * @codec: HD-audio codec
1725 * @name: ctl id name string
1726 *
1727 * Get the control element with the given id string and IFACE_MIXER.
1728 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001729struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1730 const char *name)
1731{
1732 return _snd_hda_find_mixer_ctl(codec, name, 0);
1733}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001734EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001735
Takashi Iwaid5191e52009-11-16 14:58:17 +01001736/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001737 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001738 * @codec: HD-audio codec
1739 * @nid: corresponding NID (optional)
1740 * @kctl: the control element to assign
1741 *
1742 * Add the given control element to an array inside the codec instance.
1743 * All control elements belonging to a codec are supposed to be added
1744 * by this function so that a proper clean-up works at the free or
1745 * reconfiguration time.
1746 *
1747 * If non-zero @nid is passed, the NID is assigned to the control element.
1748 * The assignment is shown in the codec proc file.
1749 *
1750 * snd_hda_ctl_add() checks the control subdev id field whether
1751 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001752 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1753 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001754 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001755int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1756 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001757{
1758 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001759 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001760 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001761
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001762 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001763 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001764 if (nid == 0)
1765 nid = get_amp_nid_(kctl->private_value);
1766 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001767 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1768 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001769 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001770 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001771 err = snd_ctl_add(codec->bus->card, kctl);
1772 if (err < 0)
1773 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001774 item = snd_array_new(&codec->mixers);
1775 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001776 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001777 item->kctl = kctl;
1778 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001779 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001780 return 0;
1781}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001782EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001783
Takashi Iwaid5191e52009-11-16 14:58:17 +01001784/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001785 * snd_hda_add_nid - Assign a NID to a control element
1786 * @codec: HD-audio codec
1787 * @nid: corresponding NID (optional)
1788 * @kctl: the control element to assign
1789 * @index: index to kctl
1790 *
1791 * Add the given control element to an array inside the codec instance.
1792 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1793 * NID:KCTL mapping - for example "Capture Source" selector.
1794 */
1795int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1796 unsigned int index, hda_nid_t nid)
1797{
1798 struct hda_nid_item *item;
1799
1800 if (nid > 0) {
1801 item = snd_array_new(&codec->nids);
1802 if (!item)
1803 return -ENOMEM;
1804 item->kctl = kctl;
1805 item->index = index;
1806 item->nid = nid;
1807 return 0;
1808 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01001809 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1810 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001811 return -EINVAL;
1812}
1813EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1814
1815/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001816 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1817 * @codec: HD-audio codec
1818 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001819void snd_hda_ctls_clear(struct hda_codec *codec)
1820{
1821 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001822 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001823 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001824 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001825 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001826 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001827}
1828
Takashi Iwaia65d6292009-02-23 16:57:04 +01001829/* pseudo device locking
1830 * toggle card->shutdown to allow/disallow the device access (as a hack)
1831 */
1832static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001833{
Takashi Iwaia65d6292009-02-23 16:57:04 +01001834 spin_lock(&card->files_lock);
1835 if (card->shutdown) {
1836 spin_unlock(&card->files_lock);
1837 return -EINVAL;
1838 }
1839 card->shutdown = 1;
1840 spin_unlock(&card->files_lock);
1841 return 0;
1842}
1843
1844static void hda_unlock_devices(struct snd_card *card)
1845{
1846 spin_lock(&card->files_lock);
1847 card->shutdown = 0;
1848 spin_unlock(&card->files_lock);
1849}
1850
Takashi Iwaid5191e52009-11-16 14:58:17 +01001851/**
1852 * snd_hda_codec_reset - Clear all objects assigned to the codec
1853 * @codec: HD-audio codec
1854 *
1855 * This frees the all PCM and control elements assigned to the codec, and
1856 * clears the caches and restores the pin default configurations.
1857 *
1858 * When a device is being used, it returns -EBSY. If successfully freed,
1859 * returns zero.
1860 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001861int snd_hda_codec_reset(struct hda_codec *codec)
1862{
1863 struct snd_card *card = codec->bus->card;
1864 int i, pcm;
1865
1866 if (hda_lock_devices(card) < 0)
1867 return -EBUSY;
1868 /* check whether the codec isn't used by any mixer or PCM streams */
1869 if (!list_empty(&card->ctl_files)) {
1870 hda_unlock_devices(card);
1871 return -EBUSY;
1872 }
1873 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1874 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1875 if (!cpcm->pcm)
1876 continue;
1877 if (cpcm->pcm->streams[0].substream_opened ||
1878 cpcm->pcm->streams[1].substream_opened) {
1879 hda_unlock_devices(card);
1880 return -EBUSY;
1881 }
1882 }
1883
1884 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001885
1886#ifdef CONFIG_SND_HDA_POWER_SAVE
1887 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001888 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001889#endif
1890 snd_hda_ctls_clear(codec);
1891 /* relase PCMs */
1892 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001893 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01001894 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001895 clear_bit(codec->pcm_info[i].device,
1896 codec->bus->pcm_dev_bits);
1897 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001898 }
1899 if (codec->patch_ops.free)
1900 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001901 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001902 codec->spec = NULL;
1903 free_hda_cache(&codec->amp_cache);
1904 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001905 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1906 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01001907 /* free only driver_pins so that init_pins + user_pins are restored */
1908 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001909 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001910 codec->num_pcms = 0;
1911 codec->pcm_info = NULL;
1912 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01001913 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1914 codec->slave_dig_outs = NULL;
1915 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001916 module_put(codec->owner);
1917 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001918
1919 /* allow device access again */
1920 hda_unlock_devices(card);
1921 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001922}
1923
Takashi Iwaid5191e52009-11-16 14:58:17 +01001924/**
1925 * snd_hda_add_vmaster - create a virtual master control and add slaves
1926 * @codec: HD-audio codec
1927 * @name: vmaster control name
1928 * @tlv: TLV data (optional)
1929 * @slaves: slave control names (optional)
1930 *
1931 * Create a virtual master control with the given name. The TLV data
1932 * must be either NULL or a valid data.
1933 *
1934 * @slaves is a NULL-terminated array of strings, each of which is a
1935 * slave control name. All controls with these names are assigned to
1936 * the new virtual master control.
1937 *
1938 * This function returns zero if successful or a negative error code.
1939 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01001940int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1941 unsigned int *tlv, const char **slaves)
1942{
1943 struct snd_kcontrol *kctl;
1944 const char **s;
1945 int err;
1946
Takashi Iwai2f085542008-02-22 18:43:50 +01001947 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1948 ;
1949 if (!*s) {
1950 snd_printdd("No slave found for %s\n", name);
1951 return 0;
1952 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001953 kctl = snd_ctl_make_virtual_master(name, tlv);
1954 if (!kctl)
1955 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001956 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001957 if (err < 0)
1958 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001959
Takashi Iwai2134ea42008-01-10 16:53:55 +01001960 for (s = slaves; *s; s++) {
1961 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01001962 int i = 0;
1963 for (;;) {
1964 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1965 if (!sctl) {
1966 if (!i)
1967 snd_printdd("Cannot find slave %s, "
1968 "skipped\n", *s);
1969 break;
1970 }
1971 err = snd_ctl_add_slave(kctl, sctl);
1972 if (err < 0)
1973 return err;
1974 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001975 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001976 }
1977 return 0;
1978}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001979EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001980
Takashi Iwaid5191e52009-11-16 14:58:17 +01001981/**
1982 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
1983 *
1984 * The control element is supposed to have the private_value field
1985 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1986 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001987int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 int chs = get_amp_channels(kcontrol);
1991
1992 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1993 uinfo->count = chs == 3 ? 2 : 1;
1994 uinfo->value.integer.min = 0;
1995 uinfo->value.integer.max = 1;
1996 return 0;
1997}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001998EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Takashi Iwaid5191e52009-11-16 14:58:17 +01002000/**
2001 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2002 *
2003 * The control element is supposed to have the private_value field
2004 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2005 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002006int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2007 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2010 hda_nid_t nid = get_amp_nid(kcontrol);
2011 int chs = get_amp_channels(kcontrol);
2012 int dir = get_amp_direction(kcontrol);
2013 int idx = get_amp_index(kcontrol);
2014 long *valp = ucontrol->value.integer.value;
2015
2016 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002017 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002018 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002020 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002021 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return 0;
2023}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002024EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Takashi Iwaid5191e52009-11-16 14:58:17 +01002026/**
2027 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2028 *
2029 * The control element is supposed to have the private_value field
2030 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2031 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002032int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2036 hda_nid_t nid = get_amp_nid(kcontrol);
2037 int chs = get_amp_channels(kcontrol);
2038 int dir = get_amp_direction(kcontrol);
2039 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 long *valp = ucontrol->value.integer.value;
2041 int change = 0;
2042
Takashi Iwaicb53c622007-08-10 17:21:45 +02002043 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002044 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002045 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002046 HDA_AMP_MUTE,
2047 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002048 valp++;
2049 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002050 if (chs & 2)
2051 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002052 HDA_AMP_MUTE,
2053 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002054#ifdef CONFIG_SND_HDA_POWER_SAVE
2055 if (codec->patch_ops.check_power_status)
2056 codec->patch_ops.check_power_status(codec, nid);
2057#endif
2058 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return change;
2060}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002061EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Takashi Iwai67d634c2009-11-16 15:35:59 +01002063#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002064/**
2065 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2066 *
2067 * This function calls snd_hda_enable_beep_device(), which behaves differently
2068 * depending on beep_mode option.
2069 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002070int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2072{
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 long *valp = ucontrol->value.integer.value;
2075
2076 snd_hda_enable_beep_device(codec, *valp);
2077 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2078}
2079EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002080#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082/*
Takashi Iwai985be542005-11-02 18:26:49 +01002083 * bound volume controls
2084 *
2085 * bind multiple volumes (# indices, from 0)
2086 */
2087
2088#define AMP_VAL_IDX_SHIFT 19
2089#define AMP_VAL_IDX_MASK (0x0f<<19)
2090
Takashi Iwaid5191e52009-11-16 14:58:17 +01002091/**
2092 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2093 *
2094 * The control element is supposed to have the private_value field
2095 * set up via HDA_BIND_MUTE*() macros.
2096 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002097int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2098 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002099{
2100 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2101 unsigned long pval;
2102 int err;
2103
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002104 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002105 pval = kcontrol->private_value;
2106 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2107 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2108 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002109 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002110 return err;
2111}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002112EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002113
Takashi Iwaid5191e52009-11-16 14:58:17 +01002114/**
2115 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2116 *
2117 * The control element is supposed to have the private_value field
2118 * set up via HDA_BIND_MUTE*() macros.
2119 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002120int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2121 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002122{
2123 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2124 unsigned long pval;
2125 int i, indices, err = 0, change = 0;
2126
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002127 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002128 pval = kcontrol->private_value;
2129 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2130 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002131 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2132 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002133 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2134 if (err < 0)
2135 break;
2136 change |= err;
2137 }
2138 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002139 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002140 return err < 0 ? err : change;
2141}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002142EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002143
Takashi Iwaid5191e52009-11-16 14:58:17 +01002144/**
2145 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2146 *
2147 * The control element is supposed to have the private_value field
2148 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002149 */
2150int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2151 struct snd_ctl_elem_info *uinfo)
2152{
2153 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2154 struct hda_bind_ctls *c;
2155 int err;
2156
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002157 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002158 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002159 kcontrol->private_value = *c->values;
2160 err = c->ops->info(kcontrol, uinfo);
2161 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002162 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002163 return err;
2164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002165EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002166
Takashi Iwaid5191e52009-11-16 14:58:17 +01002167/**
2168 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2169 *
2170 * The control element is supposed to have the private_value field
2171 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2172 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002173int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2175{
2176 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2177 struct hda_bind_ctls *c;
2178 int err;
2179
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002180 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002181 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002182 kcontrol->private_value = *c->values;
2183 err = c->ops->get(kcontrol, ucontrol);
2184 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002185 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002186 return err;
2187}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002188EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002189
Takashi Iwaid5191e52009-11-16 14:58:17 +01002190/**
2191 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2192 *
2193 * The control element is supposed to have the private_value field
2194 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2195 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002196int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2197 struct snd_ctl_elem_value *ucontrol)
2198{
2199 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2200 struct hda_bind_ctls *c;
2201 unsigned long *vals;
2202 int err = 0, change = 0;
2203
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002204 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002205 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002206 for (vals = c->values; *vals; vals++) {
2207 kcontrol->private_value = *vals;
2208 err = c->ops->put(kcontrol, ucontrol);
2209 if (err < 0)
2210 break;
2211 change |= err;
2212 }
2213 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002214 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002215 return err < 0 ? err : change;
2216}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002217EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002218
Takashi Iwaid5191e52009-11-16 14:58:17 +01002219/**
2220 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2221 *
2222 * The control element is supposed to have the private_value field
2223 * set up via HDA_BIND_VOL() macro.
2224 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002225int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2226 unsigned int size, unsigned int __user *tlv)
2227{
2228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2229 struct hda_bind_ctls *c;
2230 int err;
2231
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002232 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002233 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002234 kcontrol->private_value = *c->values;
2235 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2236 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002237 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002238 return err;
2239}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002240EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002241
2242struct hda_ctl_ops snd_hda_bind_vol = {
2243 .info = snd_hda_mixer_amp_volume_info,
2244 .get = snd_hda_mixer_amp_volume_get,
2245 .put = snd_hda_mixer_amp_volume_put,
2246 .tlv = snd_hda_mixer_amp_tlv
2247};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002248EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002249
2250struct hda_ctl_ops snd_hda_bind_sw = {
2251 .info = snd_hda_mixer_amp_switch_info,
2252 .get = snd_hda_mixer_amp_switch_get,
2253 .put = snd_hda_mixer_amp_switch_put,
2254 .tlv = snd_hda_mixer_amp_tlv
2255};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002256EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002257
2258/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 * SPDIF out controls
2260 */
2261
Takashi Iwai0ba21762007-04-16 11:29:14 +02002262static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2266 uinfo->count = 1;
2267 return 0;
2268}
2269
Takashi Iwai0ba21762007-04-16 11:29:14 +02002270static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2271 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2274 IEC958_AES0_NONAUDIO |
2275 IEC958_AES0_CON_EMPHASIS_5015 |
2276 IEC958_AES0_CON_NOT_COPYRIGHT;
2277 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2278 IEC958_AES1_CON_ORIGINAL;
2279 return 0;
2280}
2281
Takashi Iwai0ba21762007-04-16 11:29:14 +02002282static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2283 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
2285 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2286 IEC958_AES0_NONAUDIO |
2287 IEC958_AES0_PRO_EMPHASIS_5015;
2288 return 0;
2289}
2290
Takashi Iwai0ba21762007-04-16 11:29:14 +02002291static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2292 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2295
2296 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2297 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2298 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2299 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2300
2301 return 0;
2302}
2303
2304/* convert from SPDIF status bits to HDA SPDIF bits
2305 * bit 0 (DigEn) is always set zero (to be filled later)
2306 */
2307static unsigned short convert_from_spdif_status(unsigned int sbits)
2308{
2309 unsigned short val = 0;
2310
2311 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002312 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002314 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002316 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2317 IEC958_AES0_PRO_EMPHASIS_5015)
2318 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002320 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2321 IEC958_AES0_CON_EMPHASIS_5015)
2322 val |= AC_DIG1_EMPHASIS;
2323 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2324 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002326 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2328 }
2329 return val;
2330}
2331
2332/* convert to SPDIF status bits from HDA SPDIF bits
2333 */
2334static unsigned int convert_to_spdif_status(unsigned short val)
2335{
2336 unsigned int sbits = 0;
2337
Takashi Iwai0ba21762007-04-16 11:29:14 +02002338 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002340 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 sbits |= IEC958_AES0_PROFESSIONAL;
2342 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002343 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2345 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002346 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002348 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002350 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2352 sbits |= val & (0x7f << 8);
2353 }
2354 return sbits;
2355}
2356
Takashi Iwai2f728532008-09-25 16:32:41 +02002357/* set digital convert verbs both for the given NID and its slaves */
2358static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2359 int verb, int val)
2360{
2361 hda_nid_t *d;
2362
Takashi Iwai9e976972008-11-25 08:17:20 +01002363 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002364 d = codec->slave_dig_outs;
2365 if (!d)
2366 return;
2367 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002368 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002369}
2370
2371static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2372 int dig1, int dig2)
2373{
2374 if (dig1 != -1)
2375 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2376 if (dig2 != -1)
2377 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2378}
2379
Takashi Iwai0ba21762007-04-16 11:29:14 +02002380static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2384 hda_nid_t nid = kcontrol->private_value;
2385 unsigned short val;
2386 int change;
2387
Ingo Molnar62932df2006-01-16 16:34:20 +01002388 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 codec->spdif_status = ucontrol->value.iec958.status[0] |
2390 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2391 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2392 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2393 val = convert_from_spdif_status(codec->spdif_status);
2394 val |= codec->spdif_ctls & 1;
2395 change = codec->spdif_ctls != val;
2396 codec->spdif_ctls = val;
2397
Takashi Iwai2f728532008-09-25 16:32:41 +02002398 if (change)
2399 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Ingo Molnar62932df2006-01-16 16:34:20 +01002401 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return change;
2403}
2404
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002405#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Takashi Iwai0ba21762007-04-16 11:29:14 +02002407static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2408 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
2410 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2411
Takashi Iwai0ba21762007-04-16 11:29:14 +02002412 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return 0;
2414}
2415
Takashi Iwai0ba21762007-04-16 11:29:14 +02002416static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
2419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2420 hda_nid_t nid = kcontrol->private_value;
2421 unsigned short val;
2422 int change;
2423
Ingo Molnar62932df2006-01-16 16:34:20 +01002424 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002425 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002427 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002429 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002431 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002432 /* unmute amp switch (if any) */
2433 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002434 (val & AC_DIG1_ENABLE))
2435 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2436 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002438 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return change;
2440}
2441
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002442static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 {
2444 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2445 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002446 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 .info = snd_hda_spdif_mask_info,
2448 .get = snd_hda_spdif_cmask_get,
2449 },
2450 {
2451 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2452 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002453 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 .info = snd_hda_spdif_mask_info,
2455 .get = snd_hda_spdif_pmask_get,
2456 },
2457 {
2458 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002459 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 .info = snd_hda_spdif_mask_info,
2461 .get = snd_hda_spdif_default_get,
2462 .put = snd_hda_spdif_default_put,
2463 },
2464 {
2465 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002466 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 .info = snd_hda_spdif_out_switch_info,
2468 .get = snd_hda_spdif_out_switch_get,
2469 .put = snd_hda_spdif_out_switch_put,
2470 },
2471 { } /* end */
2472};
2473
Takashi Iwai09f99702008-02-04 12:31:13 +01002474#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476/**
2477 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2478 * @codec: the HDA codec
2479 * @nid: audio out widget NID
2480 *
2481 * Creates controls related with the SPDIF output.
2482 * Called from each patch supporting the SPDIF out.
2483 *
2484 * Returns 0 if successful, or a negative error code.
2485 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002486int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
2488 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002489 struct snd_kcontrol *kctl;
2490 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002491 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Takashi Iwai09f99702008-02-04 12:31:13 +01002493 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2494 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2495 idx))
2496 break;
2497 }
2498 if (idx >= SPDIF_MAX_IDX) {
2499 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2500 return -EBUSY;
2501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2503 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002504 if (!kctl)
2505 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002506 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002508 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002509 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return err;
2511 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002512 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002513 snd_hda_codec_read(codec, nid, 0,
2514 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2516 return 0;
2517}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002518EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002521 * SPDIF sharing with analog output
2522 */
2523static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2524 struct snd_ctl_elem_value *ucontrol)
2525{
2526 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2527 ucontrol->value.integer.value[0] = mout->share_spdif;
2528 return 0;
2529}
2530
2531static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_value *ucontrol)
2533{
2534 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2535 mout->share_spdif = !!ucontrol->value.integer.value[0];
2536 return 0;
2537}
2538
2539static struct snd_kcontrol_new spdif_share_sw = {
2540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2541 .name = "IEC958 Default PCM Playback Switch",
2542 .info = snd_ctl_boolean_mono_info,
2543 .get = spdif_share_sw_get,
2544 .put = spdif_share_sw_put,
2545};
2546
Takashi Iwaid5191e52009-11-16 14:58:17 +01002547/**
2548 * snd_hda_create_spdif_share_sw - create Default PCM switch
2549 * @codec: the HDA codec
2550 * @mout: multi-out instance
2551 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002552int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2553 struct hda_multi_out *mout)
2554{
2555 if (!mout->dig_out_nid)
2556 return 0;
2557 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002558 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2559 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002560}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002561EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002562
2563/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 * SPDIF input
2565 */
2566
2567#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2568
Takashi Iwai0ba21762007-04-16 11:29:14 +02002569static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2573
2574 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2575 return 0;
2576}
2577
Takashi Iwai0ba21762007-04-16 11:29:14 +02002578static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2579 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2582 hda_nid_t nid = kcontrol->private_value;
2583 unsigned int val = !!ucontrol->value.integer.value[0];
2584 int change;
2585
Ingo Molnar62932df2006-01-16 16:34:20 +01002586 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002588 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002590 snd_hda_codec_write_cache(codec, nid, 0,
2591 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002593 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return change;
2595}
2596
Takashi Iwai0ba21762007-04-16 11:29:14 +02002597static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2598 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2601 hda_nid_t nid = kcontrol->private_value;
2602 unsigned short val;
2603 unsigned int sbits;
2604
Andrew Paprocki3982d172007-12-19 12:13:44 +01002605 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 sbits = convert_to_spdif_status(val);
2607 ucontrol->value.iec958.status[0] = sbits;
2608 ucontrol->value.iec958.status[1] = sbits >> 8;
2609 ucontrol->value.iec958.status[2] = sbits >> 16;
2610 ucontrol->value.iec958.status[3] = sbits >> 24;
2611 return 0;
2612}
2613
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002614static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 {
2616 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002617 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 .info = snd_hda_spdif_in_switch_info,
2619 .get = snd_hda_spdif_in_switch_get,
2620 .put = snd_hda_spdif_in_switch_put,
2621 },
2622 {
2623 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2624 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002625 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 .info = snd_hda_spdif_mask_info,
2627 .get = snd_hda_spdif_in_status_get,
2628 },
2629 { } /* end */
2630};
2631
2632/**
2633 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2634 * @codec: the HDA codec
2635 * @nid: audio in widget NID
2636 *
2637 * Creates controls related with the SPDIF input.
2638 * Called from each patch supporting the SPDIF in.
2639 *
2640 * Returns 0 if successful, or a negative error code.
2641 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002642int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
2644 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002645 struct snd_kcontrol *kctl;
2646 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002647 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Takashi Iwai09f99702008-02-04 12:31:13 +01002649 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2650 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2651 idx))
2652 break;
2653 }
2654 if (idx >= SPDIF_MAX_IDX) {
2655 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2656 return -EBUSY;
2657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2659 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002660 if (!kctl)
2661 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002663 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002664 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return err;
2666 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002667 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002668 snd_hda_codec_read(codec, nid, 0,
2669 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002670 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return 0;
2672}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002673EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Takashi Iwaicb53c622007-08-10 17:21:45 +02002675#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002676/*
2677 * command cache
2678 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002680/* build a 32bit cache key with the widget id and the command parameter */
2681#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2682#define get_cmd_cache_nid(key) ((key) & 0xff)
2683#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2684
2685/**
2686 * snd_hda_codec_write_cache - send a single command with caching
2687 * @codec: the HDA codec
2688 * @nid: NID to send the command
2689 * @direct: direct flag
2690 * @verb: the verb to send
2691 * @parm: the parameter for the verb
2692 *
2693 * Send a single command without waiting for response.
2694 *
2695 * Returns 0 if successful, or a negative error code.
2696 */
2697int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2698 int direct, unsigned int verb, unsigned int parm)
2699{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002700 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2701 struct hda_cache_head *c;
2702 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002703
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002704 if (err < 0)
2705 return err;
2706 /* parm may contain the verb stuff for get/set amp */
2707 verb = verb | (parm >> 8);
2708 parm &= 0xff;
2709 key = build_cmd_cache_key(nid, verb);
2710 mutex_lock(&codec->bus->cmd_mutex);
2711 c = get_alloc_hash(&codec->cmd_cache, key);
2712 if (c)
2713 c->val = parm;
2714 mutex_unlock(&codec->bus->cmd_mutex);
2715 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002716}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002717EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002718
Takashi Iwaid5191e52009-11-16 14:58:17 +01002719/**
2720 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2721 * @codec: HD-audio codec
2722 *
2723 * Execute all verbs recorded in the command caches to resume.
2724 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002725void snd_hda_codec_resume_cache(struct hda_codec *codec)
2726{
Takashi Iwai603c4012008-07-30 15:01:44 +02002727 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002728 int i;
2729
Takashi Iwai603c4012008-07-30 15:01:44 +02002730 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002731 u32 key = buffer->key;
2732 if (!key)
2733 continue;
2734 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2735 get_cmd_cache_cmd(key), buffer->val);
2736 }
2737}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002738EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002739
2740/**
2741 * snd_hda_sequence_write_cache - sequence writes with caching
2742 * @codec: the HDA codec
2743 * @seq: VERB array to send
2744 *
2745 * Send the commands sequentially from the given array.
2746 * Thte commands are recorded on cache for power-save and resume.
2747 * The array must be terminated with NID=0.
2748 */
2749void snd_hda_sequence_write_cache(struct hda_codec *codec,
2750 const struct hda_verb *seq)
2751{
2752 for (; seq->nid; seq++)
2753 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2754 seq->param);
2755}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002756EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002757#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002758
Takashi Iwai54d17402005-11-21 16:33:22 +01002759/*
2760 * set power state of the codec
2761 */
2762static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2763 unsigned int power_state)
2764{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002765 hda_nid_t nid;
2766 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002767
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002768 /* this delay seems necessary to avoid click noise at power-down */
2769 if (power_state == AC_PWRST_D3)
2770 msleep(100);
2771 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002772 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002773 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002774 if (power_state == AC_PWRST_D0 &&
2775 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002776 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002777
Takashi Iwaicb53c622007-08-10 17:21:45 +02002778 nid = codec->start_nid;
2779 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002780 unsigned int wcaps = get_wcaps(codec, nid);
2781 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002782 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002783 if (power_state == AC_PWRST_D3 &&
2784 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002785 unsigned int pincap;
2786 /*
2787 * don't power down the widget if it controls
2788 * eapd and EAPD_BTLENABLE is set.
2789 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01002790 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002791 if (pincap & AC_PINCAP_EAPD) {
2792 int eapd = snd_hda_codec_read(codec,
2793 nid, 0,
2794 AC_VERB_GET_EAPD_BTLENABLE, 0);
2795 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002796 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002797 continue;
2798 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002799 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002800 snd_hda_codec_write(codec, nid, 0,
2801 AC_VERB_SET_POWER_STATE,
2802 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002803 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002804 }
2805
Takashi Iwaicb53c622007-08-10 17:21:45 +02002806 if (power_state == AC_PWRST_D0) {
2807 unsigned long end_time;
2808 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002809 /* wait until the codec reachs to D0 */
2810 end_time = jiffies + msecs_to_jiffies(500);
2811 do {
2812 state = snd_hda_codec_read(codec, fg, 0,
2813 AC_VERB_GET_POWER_STATE, 0);
2814 if (state == power_state)
2815 break;
2816 msleep(1);
2817 } while (time_after_eq(end_time, jiffies));
2818 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002819}
2820
Takashi Iwai11aeff02008-07-30 15:01:46 +02002821#ifdef CONFIG_SND_HDA_HWDEP
2822/* execute additional init verbs */
2823static void hda_exec_init_verbs(struct hda_codec *codec)
2824{
2825 if (codec->init_verbs.list)
2826 snd_hda_sequence_write(codec, codec->init_verbs.list);
2827}
2828#else
2829static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2830#endif
2831
Takashi Iwaicb53c622007-08-10 17:21:45 +02002832#ifdef SND_HDA_NEEDS_RESUME
2833/*
2834 * call suspend and power-down; used both from PM and power-save
2835 */
2836static void hda_call_codec_suspend(struct hda_codec *codec)
2837{
2838 if (codec->patch_ops.suspend)
2839 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2840 hda_set_power_state(codec,
2841 codec->afg ? codec->afg : codec->mfg,
2842 AC_PWRST_D3);
2843#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01002844 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002845 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002846 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002847 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01002848 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002849#endif
2850}
2851
2852/*
2853 * kick up codec; used both from PM and power-save
2854 */
2855static void hda_call_codec_resume(struct hda_codec *codec)
2856{
2857 hda_set_power_state(codec,
2858 codec->afg ? codec->afg : codec->mfg,
2859 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01002860 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwai11aeff02008-07-30 15:01:46 +02002861 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002862 if (codec->patch_ops.resume)
2863 codec->patch_ops.resume(codec);
2864 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002865 if (codec->patch_ops.init)
2866 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002867 snd_hda_codec_resume_amp(codec);
2868 snd_hda_codec_resume_cache(codec);
2869 }
2870}
2871#endif /* SND_HDA_NEEDS_RESUME */
2872
Takashi Iwai54d17402005-11-21 16:33:22 +01002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874/**
2875 * snd_hda_build_controls - build mixer controls
2876 * @bus: the BUS
2877 *
2878 * Creates mixer controls for each codec included in the bus.
2879 *
2880 * Returns 0 if successful, otherwise a negative error code.
2881 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002882int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002884 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Takashi Iwai0ba21762007-04-16 11:29:14 +02002886 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002887 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002888 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01002889 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002890 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002891 err = snd_hda_codec_reset(codec);
2892 if (err < 0) {
2893 printk(KERN_ERR
2894 "hda_codec: cannot revert codec\n");
2895 return err;
2896 }
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002899 return 0;
2900}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002901EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002903int snd_hda_codec_build_controls(struct hda_codec *codec)
2904{
2905 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02002906 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002907 /* continue to initialize... */
2908 if (codec->patch_ops.init)
2909 err = codec->patch_ops.init(codec);
2910 if (!err && codec->patch_ops.build_controls)
2911 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002912 if (err < 0)
2913 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return 0;
2915}
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917/*
2918 * stream formats
2919 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002920struct hda_rate_tbl {
2921 unsigned int hz;
2922 unsigned int alsa_bits;
2923 unsigned int hda_fmt;
2924};
2925
2926static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002928
2929 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2931 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2932 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2933 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2934 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2935 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2936 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2937 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2938 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2939 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2940 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002941#define AC_PAR_PCM_RATE_BITS 11
2942 /* up to bits 10, 384kHZ isn't supported properly */
2943
2944 /* not autodetected value */
2945 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002946
Takashi Iwaibefdf312005-08-22 13:57:55 +02002947 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948};
2949
2950/**
2951 * snd_hda_calc_stream_format - calculate format bitset
2952 * @rate: the sample rate
2953 * @channels: the number of channels
2954 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2955 * @maxbps: the max. bps
2956 *
2957 * Calculate the format bitset from the given rate, channels and th PCM format.
2958 *
2959 * Return zero if invalid.
2960 */
2961unsigned int snd_hda_calc_stream_format(unsigned int rate,
2962 unsigned int channels,
2963 unsigned int format,
2964 unsigned int maxbps)
2965{
2966 int i;
2967 unsigned int val = 0;
2968
Takashi Iwaibefdf312005-08-22 13:57:55 +02002969 for (i = 0; rate_bits[i].hz; i++)
2970 if (rate_bits[i].hz == rate) {
2971 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 break;
2973 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002974 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 snd_printdd("invalid rate %d\n", rate);
2976 return 0;
2977 }
2978
2979 if (channels == 0 || channels > 8) {
2980 snd_printdd("invalid channels %d\n", channels);
2981 return 0;
2982 }
2983 val |= channels - 1;
2984
2985 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002986 case 8:
2987 val |= 0x00;
2988 break;
2989 case 16:
2990 val |= 0x10;
2991 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 case 20:
2993 case 24:
2994 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02002995 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 val |= 0x40;
2997 else if (maxbps >= 24)
2998 val |= 0x30;
2999 else
3000 val |= 0x20;
3001 break;
3002 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003003 snd_printdd("invalid format width %d\n",
3004 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return 0;
3006 }
3007
3008 return val;
3009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003010EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003012static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3013{
3014 unsigned int val = 0;
3015 if (nid != codec->afg &&
3016 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3017 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3018 if (!val || val == -1)
3019 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3020 if (!val || val == -1)
3021 return 0;
3022 return val;
3023}
3024
3025static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3026{
3027 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3028 get_pcm_param);
3029}
3030
3031static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3032{
3033 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3034 if (!streams || streams == -1)
3035 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3036 if (!streams || streams == -1)
3037 return 0;
3038 return streams;
3039}
3040
3041static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3042{
3043 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3044 get_stream_param);
3045}
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047/**
3048 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3049 * @codec: the HDA codec
3050 * @nid: NID to query
3051 * @ratesp: the pointer to store the detected rate bitflags
3052 * @formatsp: the pointer to store the detected formats
3053 * @bpsp: the pointer to store the detected format widths
3054 *
3055 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3056 * or @bsps argument is ignored.
3057 *
3058 * Returns 0 if successful, otherwise a negative error code.
3059 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003060static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3062{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003063 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003065 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003066 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 if (ratesp) {
3069 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003070 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003072 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003074 if (rates == 0) {
3075 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3076 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3077 nid, val,
3078 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3079 return -EIO;
3080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 *ratesp = rates;
3082 }
3083
3084 if (formatsp || bpsp) {
3085 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003086 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003088 streams = query_stream_param(codec, nid);
3089 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
3092 bps = 0;
3093 if (streams & AC_SUPFMT_PCM) {
3094 if (val & AC_SUPPCM_BITS_8) {
3095 formats |= SNDRV_PCM_FMTBIT_U8;
3096 bps = 8;
3097 }
3098 if (val & AC_SUPPCM_BITS_16) {
3099 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3100 bps = 16;
3101 }
3102 if (wcaps & AC_WCAP_DIGITAL) {
3103 if (val & AC_SUPPCM_BITS_32)
3104 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3105 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3106 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3107 if (val & AC_SUPPCM_BITS_24)
3108 bps = 24;
3109 else if (val & AC_SUPPCM_BITS_20)
3110 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003111 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3112 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3114 if (val & AC_SUPPCM_BITS_32)
3115 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 else if (val & AC_SUPPCM_BITS_24)
3117 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003118 else if (val & AC_SUPPCM_BITS_20)
3119 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
3121 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003122 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003124 if (!bps)
3125 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003126 }
3127 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003128 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /* temporary hack: we have still no proper support
3130 * for the direct AC3 stream...
3131 */
3132 formats |= SNDRV_PCM_FMTBIT_U8;
3133 bps = 8;
3134 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003135 if (formats == 0) {
3136 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3137 "(nid=0x%x, val=0x%x, ovrd=%i, "
3138 "streams=0x%x)\n",
3139 nid, val,
3140 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3141 streams);
3142 return -EIO;
3143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (formatsp)
3145 *formatsp = formats;
3146 if (bpsp)
3147 *bpsp = bps;
3148 }
3149
3150 return 0;
3151}
3152
3153/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003154 * snd_hda_is_supported_format - Check the validity of the format
3155 * @codec: HD-audio codec
3156 * @nid: NID to check
3157 * @format: the HD-audio format value to check
3158 *
3159 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 *
3161 * Returns 1 if supported, 0 if not.
3162 */
3163int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3164 unsigned int format)
3165{
3166 int i;
3167 unsigned int val = 0, rate, stream;
3168
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003169 val = query_pcm_param(codec, nid);
3170 if (!val)
3171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003174 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003175 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 if (val & (1 << i))
3177 break;
3178 return 0;
3179 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003180 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return 0;
3182
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003183 stream = query_stream_param(codec, nid);
3184 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return 0;
3186
3187 if (stream & AC_SUPFMT_PCM) {
3188 switch (format & 0xf0) {
3189 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003190 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 return 0;
3192 break;
3193 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003194 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 return 0;
3196 break;
3197 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003198 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return 0;
3200 break;
3201 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003202 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return 0;
3204 break;
3205 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003206 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return 0;
3208 break;
3209 default:
3210 return 0;
3211 }
3212 } else {
3213 /* FIXME: check for float32 and AC3? */
3214 }
3215
3216 return 1;
3217}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003218EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
3220/*
3221 * PCM stuff
3222 */
3223static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3224 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003225 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 return 0;
3228}
3229
3230static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3231 struct hda_codec *codec,
3232 unsigned int stream_tag,
3233 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003234 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
3236 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3237 return 0;
3238}
3239
3240static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3241 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003242 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Takashi Iwai888afa12008-03-18 09:57:50 +01003244 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return 0;
3246}
3247
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003248static int set_pcm_default_values(struct hda_codec *codec,
3249 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003251 int err;
3252
Takashi Iwai0ba21762007-04-16 11:29:14 +02003253 /* query support PCM information from the given NID */
3254 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003255 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003256 info->rates ? NULL : &info->rates,
3257 info->formats ? NULL : &info->formats,
3258 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003259 if (err < 0)
3260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
3262 if (info->ops.open == NULL)
3263 info->ops.open = hda_pcm_default_open_close;
3264 if (info->ops.close == NULL)
3265 info->ops.close = hda_pcm_default_open_close;
3266 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003267 if (snd_BUG_ON(!info->nid))
3268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 info->ops.prepare = hda_pcm_default_prepare;
3270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003272 if (snd_BUG_ON(!info->nid))
3273 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 info->ops.cleanup = hda_pcm_default_cleanup;
3275 }
3276 return 0;
3277}
3278
Takashi Iwaid5191e52009-11-16 14:58:17 +01003279/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003280const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3281 "Audio", "SPDIF", "HDMI", "Modem"
3282};
3283
Takashi Iwai176d5332008-07-30 15:01:44 +02003284/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003285 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003286 *
3287 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003288 */
3289static int get_empty_pcm_device(struct hda_bus *bus, int type)
3290{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003291 /* audio device indices; not linear to keep compatibility */
3292 static int audio_idx[HDA_PCM_NTYPES][5] = {
3293 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3294 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003295 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003296 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003297 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003298 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003299
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003300 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003301 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3302 return -EINVAL;
3303 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003304
3305 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3306 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3307 return audio_idx[type][i];
3308
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003309 snd_printk(KERN_WARNING "Too many %s devices\n",
3310 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003311 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003312}
3313
3314/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003315 * attach a new PCM stream
3316 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003317static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003318{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003319 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003320 struct hda_pcm_stream *info;
3321 int stream, err;
3322
Takashi Iwaib91f0802008-11-04 08:43:08 +01003323 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003324 return -EINVAL;
3325 for (stream = 0; stream < 2; stream++) {
3326 info = &pcm->stream[stream];
3327 if (info->substreams) {
3328 err = set_pcm_default_values(codec, info);
3329 if (err < 0)
3330 return err;
3331 }
3332 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003333 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003334}
3335
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003336/* assign all PCMs of the given codec */
3337int snd_hda_codec_build_pcms(struct hda_codec *codec)
3338{
3339 unsigned int pcm;
3340 int err;
3341
3342 if (!codec->num_pcms) {
3343 if (!codec->patch_ops.build_pcms)
3344 return 0;
3345 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003346 if (err < 0) {
3347 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003348 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003349 err = snd_hda_codec_reset(codec);
3350 if (err < 0) {
3351 printk(KERN_ERR
3352 "hda_codec: cannot revert codec\n");
3353 return err;
3354 }
3355 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003356 }
3357 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3358 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3359 int dev;
3360
3361 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003362 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003363
3364 if (!cpcm->pcm) {
3365 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3366 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003367 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003368 cpcm->device = dev;
3369 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003370 if (err < 0) {
3371 printk(KERN_ERR "hda_codec: cannot attach "
3372 "PCM stream %d for codec #%d\n",
3373 dev, codec->addr);
3374 continue; /* no fatal error */
3375 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003376 }
3377 }
3378 return 0;
3379}
3380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381/**
3382 * snd_hda_build_pcms - build PCM information
3383 * @bus: the BUS
3384 *
3385 * Create PCM information for each codec included in the bus.
3386 *
3387 * The build_pcms codec patch is requested to set up codec->num_pcms and
3388 * codec->pcm_info properly. The array is referred by the top-level driver
3389 * to create its PCM instances.
3390 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3391 * callback.
3392 *
3393 * At least, substreams, channels_min and channels_max must be filled for
3394 * each stream. substreams = 0 indicates that the stream doesn't exist.
3395 * When rates and/or formats are zero, the supported values are queried
3396 * from the given nid. The nid is used also by the default ops.prepare
3397 * and ops.cleanup callbacks.
3398 *
3399 * The driver needs to call ops.open in its open callback. Similarly,
3400 * ops.close is supposed to be called in the close callback.
3401 * ops.prepare should be called in the prepare or hw_params callback
3402 * with the proper parameters for set up.
3403 * ops.cleanup should be called in hw_free for clean up of streams.
3404 *
3405 * This function returns 0 if successfull, or a negative error code.
3406 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003407int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003409 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Takashi Iwai0ba21762007-04-16 11:29:14 +02003411 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003412 int err = snd_hda_codec_build_pcms(codec);
3413 if (err < 0)
3414 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
3416 return 0;
3417}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003418EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420/**
3421 * snd_hda_check_board_config - compare the current codec with the config table
3422 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003423 * @num_configs: number of config enums
3424 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 * @tbl: configuration table, terminated by null entries
3426 *
3427 * Compares the modelname or PCI subsystem id of the current codec with the
3428 * given configuration table. If a matching entry is found, returns its
3429 * config value (supposed to be 0 or positive).
3430 *
3431 * If no entries are matching, the function returns a negative value.
3432 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003433int snd_hda_check_board_config(struct hda_codec *codec,
3434 int num_configs, const char **models,
3435 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003437 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003438 int i;
3439 for (i = 0; i < num_configs; i++) {
3440 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003441 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003442 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3443 "selected\n", models[i]);
3444 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
3446 }
3447 }
3448
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003449 if (!codec->bus->pci || !tbl)
3450 return -1;
3451
3452 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3453 if (!tbl)
3454 return -1;
3455 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003456#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003457 char tmp[10];
3458 const char *model = NULL;
3459 if (models)
3460 model = models[tbl->value];
3461 if (!model) {
3462 sprintf(tmp, "#%d", tbl->value);
3463 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003465 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3466 "for config %x:%x (%s)\n",
3467 model, tbl->subvendor, tbl->subdevice,
3468 (tbl->name ? tbl->name : "Unknown device"));
3469#endif
3470 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
3472 return -1;
3473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003474EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003477 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003478 subsystem ID with the
3479 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003480
3481 This is important for Gateway notebooks with SB450 HDA Audio
3482 where the vendor ID of the PCI device is:
3483 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3484 and the vendor/subvendor are found only at the codec.
3485
3486 * @codec: the HDA codec
3487 * @num_configs: number of config enums
3488 * @models: array of model name strings
3489 * @tbl: configuration table, terminated by null entries
3490 *
3491 * Compares the modelname or PCI subsystem id of the current codec with the
3492 * given configuration table. If a matching entry is found, returns its
3493 * config value (supposed to be 0 or positive).
3494 *
3495 * If no entries are matching, the function returns a negative value.
3496 */
3497int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3498 int num_configs, const char **models,
3499 const struct snd_pci_quirk *tbl)
3500{
3501 const struct snd_pci_quirk *q;
3502
3503 /* Search for codec ID */
3504 for (q = tbl; q->subvendor; q++) {
3505 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3506
3507 if (vendorid == codec->subsystem_id)
3508 break;
3509 }
3510
3511 if (!q->subvendor)
3512 return -1;
3513
3514 tbl = q;
3515
3516 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003517#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003518 char tmp[10];
3519 const char *model = NULL;
3520 if (models)
3521 model = models[tbl->value];
3522 if (!model) {
3523 sprintf(tmp, "#%d", tbl->value);
3524 model = tmp;
3525 }
3526 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3527 "for config %x:%x (%s)\n",
3528 model, tbl->subvendor, tbl->subdevice,
3529 (tbl->name ? tbl->name : "Unknown device"));
3530#endif
3531 return tbl->value;
3532 }
3533 return -1;
3534}
3535EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3536
3537/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 * snd_hda_add_new_ctls - create controls from the array
3539 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003540 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 *
3542 * This helper function creates and add new controls in the given array.
3543 * The array must be terminated with an empty entry as terminator.
3544 *
3545 * Returns 0 if successful, or a negative error code.
3546 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003547int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003549 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003552 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003553 if (knew->iface == -1) /* skip this codec private value */
3554 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003555 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003556 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003557 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003558 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003559 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003560 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003561 return err;
3562 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003563 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003564 return -ENOMEM;
3565 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003566 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003567 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003568 return err;
3569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
3571 return 0;
3572}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003573EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Takashi Iwaicb53c622007-08-10 17:21:45 +02003575#ifdef CONFIG_SND_HDA_POWER_SAVE
3576static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3577 unsigned int power_state);
3578
3579static void hda_power_work(struct work_struct *work)
3580{
3581 struct hda_codec *codec =
3582 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003583 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003584
Maxim Levitsky2e492462007-09-03 15:26:57 +02003585 if (!codec->power_on || codec->power_count) {
3586 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003587 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003588 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003589
3590 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003591 if (bus->ops.pm_notify)
3592 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003593}
3594
3595static void hda_keep_power_on(struct hda_codec *codec)
3596{
3597 codec->power_count++;
3598 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003599 codec->power_jiffies = jiffies;
3600}
3601
Takashi Iwaid5191e52009-11-16 14:58:17 +01003602/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003603void snd_hda_update_power_acct(struct hda_codec *codec)
3604{
3605 unsigned long delta = jiffies - codec->power_jiffies;
3606 if (codec->power_on)
3607 codec->power_on_acct += delta;
3608 else
3609 codec->power_off_acct += delta;
3610 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003611}
3612
Takashi Iwaid5191e52009-11-16 14:58:17 +01003613/**
3614 * snd_hda_power_up - Power-up the codec
3615 * @codec: HD-audio codec
3616 *
3617 * Increment the power-up counter and power up the hardware really when
3618 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003619 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003620void snd_hda_power_up(struct hda_codec *codec)
3621{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003622 struct hda_bus *bus = codec->bus;
3623
Takashi Iwaicb53c622007-08-10 17:21:45 +02003624 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003625 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003626 return;
3627
Takashi Iwaia2f63092009-11-11 09:34:25 +01003628 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003629 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003630 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003631 if (bus->ops.pm_notify)
3632 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003633 hda_call_codec_resume(codec);
3634 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003635 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003636}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003637EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003638
3639#define power_save(codec) \
3640 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003641
Takashi Iwaid5191e52009-11-16 14:58:17 +01003642/**
3643 * snd_hda_power_down - Power-down the codec
3644 * @codec: HD-audio codec
3645 *
3646 * Decrement the power-up counter and schedules the power-off work if
3647 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003648 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003649void snd_hda_power_down(struct hda_codec *codec)
3650{
3651 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003652 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003653 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003654 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003655 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003656 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003657 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003658 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003659}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003660EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003661
Takashi Iwaid5191e52009-11-16 14:58:17 +01003662/**
3663 * snd_hda_check_amp_list_power - Check the amp list and update the power
3664 * @codec: HD-audio codec
3665 * @check: the object containing an AMP list and the status
3666 * @nid: NID to check / update
3667 *
3668 * Check whether the given NID is in the amp list. If it's in the list,
3669 * check the current AMP status, and update the the power-status according
3670 * to the mute status.
3671 *
3672 * This function is supposed to be set or called from the check_power_status
3673 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003674 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003675int snd_hda_check_amp_list_power(struct hda_codec *codec,
3676 struct hda_loopback_check *check,
3677 hda_nid_t nid)
3678{
3679 struct hda_amp_list *p;
3680 int ch, v;
3681
3682 if (!check->amplist)
3683 return 0;
3684 for (p = check->amplist; p->nid; p++) {
3685 if (p->nid == nid)
3686 break;
3687 }
3688 if (!p->nid)
3689 return 0; /* nothing changed */
3690
3691 for (p = check->amplist; p->nid; p++) {
3692 for (ch = 0; ch < 2; ch++) {
3693 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3694 p->idx);
3695 if (!(v & HDA_AMP_MUTE) && v > 0) {
3696 if (!check->power_on) {
3697 check->power_on = 1;
3698 snd_hda_power_up(codec);
3699 }
3700 return 1;
3701 }
3702 }
3703 }
3704 if (check->power_on) {
3705 check->power_on = 0;
3706 snd_hda_power_down(codec);
3707 }
3708 return 0;
3709}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003710EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003711#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003713/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003714 * Channel mode helper
3715 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003716
3717/**
3718 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3719 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003720int snd_hda_ch_mode_info(struct hda_codec *codec,
3721 struct snd_ctl_elem_info *uinfo,
3722 const struct hda_channel_mode *chmode,
3723 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003724{
3725 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3726 uinfo->count = 1;
3727 uinfo->value.enumerated.items = num_chmodes;
3728 if (uinfo->value.enumerated.item >= num_chmodes)
3729 uinfo->value.enumerated.item = num_chmodes - 1;
3730 sprintf(uinfo->value.enumerated.name, "%dch",
3731 chmode[uinfo->value.enumerated.item].channels);
3732 return 0;
3733}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003734EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003735
Takashi Iwaid5191e52009-11-16 14:58:17 +01003736/**
3737 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3738 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003739int snd_hda_ch_mode_get(struct hda_codec *codec,
3740 struct snd_ctl_elem_value *ucontrol,
3741 const struct hda_channel_mode *chmode,
3742 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003743 int max_channels)
3744{
3745 int i;
3746
3747 for (i = 0; i < num_chmodes; i++) {
3748 if (max_channels == chmode[i].channels) {
3749 ucontrol->value.enumerated.item[0] = i;
3750 break;
3751 }
3752 }
3753 return 0;
3754}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003755EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003756
Takashi Iwaid5191e52009-11-16 14:58:17 +01003757/**
3758 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3759 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003760int snd_hda_ch_mode_put(struct hda_codec *codec,
3761 struct snd_ctl_elem_value *ucontrol,
3762 const struct hda_channel_mode *chmode,
3763 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003764 int *max_channelsp)
3765{
3766 unsigned int mode;
3767
3768 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01003769 if (mode >= num_chmodes)
3770 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003771 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003772 return 0;
3773 /* change the current channel setting */
3774 *max_channelsp = chmode[mode].channels;
3775 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003776 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003777 return 1;
3778}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003779EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781/*
3782 * input MUX helper
3783 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003784
3785/**
3786 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3787 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003788int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3789 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
3791 unsigned int index;
3792
3793 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3794 uinfo->count = 1;
3795 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003796 if (!imux->num_items)
3797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 index = uinfo->value.enumerated.item;
3799 if (index >= imux->num_items)
3800 index = imux->num_items - 1;
3801 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3802 return 0;
3803}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003804EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Takashi Iwaid5191e52009-11-16 14:58:17 +01003806/**
3807 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3808 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003809int snd_hda_input_mux_put(struct hda_codec *codec,
3810 const struct hda_input_mux *imux,
3811 struct snd_ctl_elem_value *ucontrol,
3812 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 unsigned int *cur_val)
3814{
3815 unsigned int idx;
3816
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003817 if (!imux->num_items)
3818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 idx = ucontrol->value.enumerated.item[0];
3820 if (idx >= imux->num_items)
3821 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003822 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003824 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3825 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 *cur_val = idx;
3827 return 1;
3828}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003829EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831
3832/*
3833 * Multi-channel / digital-out PCM helper functions
3834 */
3835
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003836/* setup SPDIF output stream */
3837static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3838 unsigned int stream_tag, unsigned int format)
3839{
3840 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02003841 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003842 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02003843 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3844 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003845 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003846 if (codec->slave_dig_outs) {
3847 hda_nid_t *d;
3848 for (d = codec->slave_dig_outs; *d; d++)
3849 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3850 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003851 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003852 /* turn on again (if needed) */
3853 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3854 set_dig_out_convert(codec, nid,
3855 codec->spdif_ctls & 0xff, -1);
3856}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003857
Takashi Iwai2f728532008-09-25 16:32:41 +02003858static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3859{
3860 snd_hda_codec_cleanup_stream(codec, nid);
3861 if (codec->slave_dig_outs) {
3862 hda_nid_t *d;
3863 for (d = codec->slave_dig_outs; *d; d++)
3864 snd_hda_codec_cleanup_stream(codec, *d);
3865 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003866}
3867
Takashi Iwaid5191e52009-11-16 14:58:17 +01003868/**
3869 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
3870 * @bus: HD-audio bus
3871 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003872void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3873{
3874 struct hda_codec *codec;
3875
3876 if (!bus)
3877 return;
3878 list_for_each_entry(codec, &bus->codec_list, list) {
3879#ifdef CONFIG_SND_HDA_POWER_SAVE
3880 if (!codec->power_on)
3881 continue;
3882#endif
3883 if (codec->patch_ops.reboot_notify)
3884 codec->patch_ops.reboot_notify(codec);
3885 }
3886}
Takashi Iwai8f217a22009-11-10 18:26:12 +01003887EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003888
Takashi Iwaid5191e52009-11-16 14:58:17 +01003889/**
3890 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003892int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3893 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894{
Ingo Molnar62932df2006-01-16 16:34:20 +01003895 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003896 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3897 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003898 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003900 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return 0;
3902}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003903EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Takashi Iwaid5191e52009-11-16 14:58:17 +01003905/**
3906 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3907 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003908int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3909 struct hda_multi_out *mout,
3910 unsigned int stream_tag,
3911 unsigned int format,
3912 struct snd_pcm_substream *substream)
3913{
3914 mutex_lock(&codec->spdif_mutex);
3915 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3916 mutex_unlock(&codec->spdif_mutex);
3917 return 0;
3918}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003919EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003920
Takashi Iwaid5191e52009-11-16 14:58:17 +01003921/**
3922 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3923 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003924int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3925 struct hda_multi_out *mout)
3926{
3927 mutex_lock(&codec->spdif_mutex);
3928 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3929 mutex_unlock(&codec->spdif_mutex);
3930 return 0;
3931}
3932EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3933
Takashi Iwaid5191e52009-11-16 14:58:17 +01003934/**
3935 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003937int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3938 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
Ingo Molnar62932df2006-01-16 16:34:20 +01003940 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003942 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 return 0;
3944}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003945EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Takashi Iwaid5191e52009-11-16 14:58:17 +01003947/**
3948 * snd_hda_multi_out_analog_open - open analog outputs
3949 *
3950 * Open analog outputs and set up the hw-constraints.
3951 * If the digital outputs can be opened as slave, open the digital
3952 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003954int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3955 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003956 struct snd_pcm_substream *substream,
3957 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958{
Takashi Iwai9a081602008-02-12 18:37:26 +01003959 struct snd_pcm_runtime *runtime = substream->runtime;
3960 runtime->hw.channels_max = mout->max_channels;
3961 if (mout->dig_out_nid) {
3962 if (!mout->analog_rates) {
3963 mout->analog_rates = hinfo->rates;
3964 mout->analog_formats = hinfo->formats;
3965 mout->analog_maxbps = hinfo->maxbps;
3966 } else {
3967 runtime->hw.rates = mout->analog_rates;
3968 runtime->hw.formats = mout->analog_formats;
3969 hinfo->maxbps = mout->analog_maxbps;
3970 }
3971 if (!mout->spdif_rates) {
3972 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3973 &mout->spdif_rates,
3974 &mout->spdif_formats,
3975 &mout->spdif_maxbps);
3976 }
3977 mutex_lock(&codec->spdif_mutex);
3978 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02003979 if ((runtime->hw.rates & mout->spdif_rates) &&
3980 (runtime->hw.formats & mout->spdif_formats)) {
3981 runtime->hw.rates &= mout->spdif_rates;
3982 runtime->hw.formats &= mout->spdif_formats;
3983 if (mout->spdif_maxbps < hinfo->maxbps)
3984 hinfo->maxbps = mout->spdif_maxbps;
3985 } else {
3986 mout->share_spdif = 0;
3987 /* FIXME: need notify? */
3988 }
Takashi Iwai9a081602008-02-12 18:37:26 +01003989 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003990 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3993 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3994}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003995EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Takashi Iwaid5191e52009-11-16 14:58:17 +01003997/**
3998 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3999 *
4000 * Set up the i/o for analog out.
4001 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004003int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4004 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 unsigned int stream_tag,
4006 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004007 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
4009 hda_nid_t *nids = mout->dac_nids;
4010 int chs = substream->runtime->channels;
4011 int i;
4012
Ingo Molnar62932df2006-01-16 16:34:20 +01004013 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004014 if (mout->dig_out_nid && mout->share_spdif &&
4015 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004017 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4018 format) &&
4019 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004021 setup_dig_out_stream(codec, mout->dig_out_nid,
4022 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 } else {
4024 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004025 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 }
4027 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004028 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004031 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4032 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004033 if (!mout->no_share_stream &&
4034 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004036 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4037 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004038 /* extra outputs copied from front */
4039 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004040 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004041 snd_hda_codec_setup_stream(codec,
4042 mout->extra_out_nid[i],
4043 stream_tag, 0, format);
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 /* surrounds */
4046 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004047 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004048 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4049 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004050 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004051 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4052 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 }
4054 return 0;
4055}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004056EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Takashi Iwaid5191e52009-11-16 14:58:17 +01004058/**
4059 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004061int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4062 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
4064 hda_nid_t *nids = mout->dac_nids;
4065 int i;
4066
4067 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004068 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004070 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004071 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4072 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004073 snd_hda_codec_cleanup_stream(codec,
4074 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004075 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004077 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 mout->dig_out_used = 0;
4079 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004080 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 return 0;
4082}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004083EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004085/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004086 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004087 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004088
Takashi Iwai12f288b2007-08-02 15:51:59 +02004089static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004090{
4091 for (; *list; list++)
4092 if (*list == nid)
4093 return 1;
4094 return 0;
4095}
4096
Steve Longerbeam81937d32007-05-08 15:33:03 +02004097
4098/*
4099 * Sort an associated group of pins according to their sequence numbers.
4100 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004101static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004102 int num_pins)
4103{
4104 int i, j;
4105 short seq;
4106 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004107
Steve Longerbeam81937d32007-05-08 15:33:03 +02004108 for (i = 0; i < num_pins; i++) {
4109 for (j = i + 1; j < num_pins; j++) {
4110 if (sequences[i] > sequences[j]) {
4111 seq = sequences[i];
4112 sequences[i] = sequences[j];
4113 sequences[j] = seq;
4114 nid = pins[i];
4115 pins[i] = pins[j];
4116 pins[j] = nid;
4117 }
4118 }
4119 }
4120}
4121
4122
Takashi Iwai82bc9552006-03-21 11:24:42 +01004123/*
4124 * Parse all pin widgets and store the useful pin nids to cfg
4125 *
4126 * The number of line-outs or any primary output is stored in line_outs,
4127 * and the corresponding output pins are assigned to line_out_pins[],
4128 * in the order of front, rear, CLFE, side, ...
4129 *
4130 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004131 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004132 * is detected, one of speaker of HP pins is assigned as the primary
4133 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4134 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004135 *
Takashi Iwai82bc9552006-03-21 11:24:42 +01004136 * The analog input pins are assigned to input_pins array.
4137 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4138 * respectively.
4139 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004140int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4141 struct auto_pin_cfg *cfg,
4142 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004143{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004144 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004145 short seq, assoc_line_out, assoc_speaker;
4146 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4147 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004148 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004149
4150 memset(cfg, 0, sizeof(*cfg));
4151
Steve Longerbeam81937d32007-05-08 15:33:03 +02004152 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4153 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004154 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004155 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004156
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004157 end_nid = codec->start_nid + codec->num_nodes;
4158 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004159 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004160 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004161 unsigned int def_conf;
4162 short assoc, loc;
4163
4164 /* read all default configuration for pin complex */
4165 if (wid_type != AC_WID_PIN)
4166 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004167 /* ignore the given nids (e.g. pc-beep returns error) */
4168 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4169 continue;
4170
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004171 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004172 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4173 continue;
4174 loc = get_defcfg_location(def_conf);
4175 switch (get_defcfg_device(def_conf)) {
4176 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004177 seq = get_defcfg_sequence(def_conf);
4178 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004179
4180 if (!(wid_caps & AC_WCAP_STEREO))
4181 if (!cfg->mono_out_pin)
4182 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004183 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004184 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004185 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004186 assoc_line_out = assoc;
4187 else if (assoc_line_out != assoc)
4188 continue;
4189 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4190 continue;
4191 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004192 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004193 cfg->line_outs++;
4194 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004195 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004196 seq = get_defcfg_sequence(def_conf);
4197 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004198 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004199 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004200 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004201 assoc_speaker = assoc;
4202 else if (assoc_speaker != assoc)
4203 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004204 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4205 continue;
4206 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004207 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004208 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004209 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004210 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004211 seq = get_defcfg_sequence(def_conf);
4212 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004213 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4214 continue;
4215 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004216 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004217 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004218 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004219 case AC_JACK_MIC_IN: {
4220 int preferred, alt;
4221 if (loc == AC_JACK_LOC_FRONT) {
4222 preferred = AUTO_PIN_FRONT_MIC;
4223 alt = AUTO_PIN_MIC;
4224 } else {
4225 preferred = AUTO_PIN_MIC;
4226 alt = AUTO_PIN_FRONT_MIC;
4227 }
4228 if (!cfg->input_pins[preferred])
4229 cfg->input_pins[preferred] = nid;
4230 else if (!cfg->input_pins[alt])
4231 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004232 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004233 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004234 case AC_JACK_LINE_IN:
4235 if (loc == AC_JACK_LOC_FRONT)
4236 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4237 else
4238 cfg->input_pins[AUTO_PIN_LINE] = nid;
4239 break;
4240 case AC_JACK_CD:
4241 cfg->input_pins[AUTO_PIN_CD] = nid;
4242 break;
4243 case AC_JACK_AUX:
4244 cfg->input_pins[AUTO_PIN_AUX] = nid;
4245 break;
4246 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004247 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004248 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4249 continue;
4250 cfg->dig_out_pins[cfg->dig_outs] = nid;
4251 cfg->dig_out_type[cfg->dig_outs] =
4252 (loc == AC_JACK_LOC_HDMI) ?
4253 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4254 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004255 break;
4256 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004257 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004258 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004259 if (loc == AC_JACK_LOC_HDMI)
4260 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4261 else
4262 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004263 break;
4264 }
4265 }
4266
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004267 /* FIX-UP:
4268 * If no line-out is defined but multiple HPs are found,
4269 * some of them might be the real line-outs.
4270 */
4271 if (!cfg->line_outs && cfg->hp_outs > 1) {
4272 int i = 0;
4273 while (i < cfg->hp_outs) {
4274 /* The real HPs should have the sequence 0x0f */
4275 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4276 i++;
4277 continue;
4278 }
4279 /* Move it to the line-out table */
4280 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4281 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4282 cfg->line_outs++;
4283 cfg->hp_outs--;
4284 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4285 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4286 memmove(sequences_hp + i - 1, sequences_hp + i,
4287 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4288 }
4289 }
4290
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004291 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004292 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4293 cfg->line_outs);
4294 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4295 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004296 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4297 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004298
Takashi Iwaif889fa92007-10-31 15:49:32 +01004299 /* if we have only one mic, make it AUTO_PIN_MIC */
4300 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4301 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4302 cfg->input_pins[AUTO_PIN_MIC] =
4303 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4304 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4305 }
4306 /* ditto for line-in */
4307 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4308 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4309 cfg->input_pins[AUTO_PIN_LINE] =
4310 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4311 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4312 }
4313
Steve Longerbeam81937d32007-05-08 15:33:03 +02004314 /*
4315 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4316 * as a primary output
4317 */
4318 if (!cfg->line_outs) {
4319 if (cfg->speaker_outs) {
4320 cfg->line_outs = cfg->speaker_outs;
4321 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4322 sizeof(cfg->speaker_pins));
4323 cfg->speaker_outs = 0;
4324 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4325 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4326 } else if (cfg->hp_outs) {
4327 cfg->line_outs = cfg->hp_outs;
4328 memcpy(cfg->line_out_pins, cfg->hp_pins,
4329 sizeof(cfg->hp_pins));
4330 cfg->hp_outs = 0;
4331 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4332 cfg->line_out_type = AUTO_PIN_HP_OUT;
4333 }
4334 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004335
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004336 /* Reorder the surround channels
4337 * ALSA sequence is front/surr/clfe/side
4338 * HDA sequence is:
4339 * 4-ch: front/surr => OK as it is
4340 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004341 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004342 */
4343 switch (cfg->line_outs) {
4344 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004345 case 4:
4346 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004347 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004348 cfg->line_out_pins[2] = nid;
4349 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004350 }
4351
Takashi Iwai82bc9552006-03-21 11:24:42 +01004352 /*
4353 * debug prints of the parsed results
4354 */
4355 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4356 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4357 cfg->line_out_pins[2], cfg->line_out_pins[3],
4358 cfg->line_out_pins[4]);
4359 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4360 cfg->speaker_outs, cfg->speaker_pins[0],
4361 cfg->speaker_pins[1], cfg->speaker_pins[2],
4362 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004363 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4364 cfg->hp_outs, cfg->hp_pins[0],
4365 cfg->hp_pins[1], cfg->hp_pins[2],
4366 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004367 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004368 if (cfg->dig_outs)
4369 snd_printd(" dig-out=0x%x/0x%x\n",
4370 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004371 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4372 " cd=0x%x, aux=0x%x\n",
4373 cfg->input_pins[AUTO_PIN_MIC],
4374 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4375 cfg->input_pins[AUTO_PIN_LINE],
4376 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4377 cfg->input_pins[AUTO_PIN_CD],
4378 cfg->input_pins[AUTO_PIN_AUX]);
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004379 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004380 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004381
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004382 return 0;
4383}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004384EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004385
Takashi Iwai4a471b72005-12-07 13:56:29 +01004386/* labels for input pins */
4387const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4388 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4389};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004390EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01004391
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393#ifdef CONFIG_PM
4394/*
4395 * power management
4396 */
4397
4398/**
4399 * snd_hda_suspend - suspend the codecs
4400 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 *
4402 * Returns 0 if successful.
4403 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004404int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004406 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Takashi Iwai0ba21762007-04-16 11:29:14 +02004408 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004409#ifdef CONFIG_SND_HDA_POWER_SAVE
4410 if (!codec->power_on)
4411 continue;
4412#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004413 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 }
4415 return 0;
4416}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004417EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
4419/**
4420 * snd_hda_resume - resume the codecs
4421 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 *
4423 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004424 *
4425 * This fucntion is defined only when POWER_SAVE isn't set.
4426 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 */
4428int snd_hda_resume(struct hda_bus *bus)
4429{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004430 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Takashi Iwai0ba21762007-04-16 11:29:14 +02004432 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004433 if (snd_hda_codec_needs_resume(codec))
4434 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return 0;
4437}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004438EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004439#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004440
4441/*
4442 * generic arrays
4443 */
4444
Takashi Iwaid5191e52009-11-16 14:58:17 +01004445/**
4446 * snd_array_new - get a new element from the given array
4447 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004448 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004449 * Get a new element from the given array. If it exceeds the
4450 * pre-allocated array size, re-allocate the array.
4451 *
4452 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004453 */
4454void *snd_array_new(struct snd_array *array)
4455{
4456 if (array->used >= array->alloced) {
4457 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004458 void *nlist;
4459 if (snd_BUG_ON(num >= 4096))
4460 return NULL;
4461 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004462 if (!nlist)
4463 return NULL;
4464 if (array->list) {
4465 memcpy(nlist, array->list,
4466 array->elem_size * array->alloced);
4467 kfree(array->list);
4468 }
4469 array->list = nlist;
4470 array->alloced = num;
4471 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004472 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004474EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004475
Takashi Iwaid5191e52009-11-16 14:58:17 +01004476/**
4477 * snd_array_free - free the given array elements
4478 * @array: the array object
4479 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004480void snd_array_free(struct snd_array *array)
4481{
4482 kfree(array->list);
4483 array->used = 0;
4484 array->alloced = 0;
4485 array->list = NULL;
4486}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004487EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004488
Takashi Iwaid5191e52009-11-16 14:58:17 +01004489/**
4490 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4491 * @pcm: PCM caps bits
4492 * @buf: the string buffer to write
4493 * @buflen: the max buffer length
4494 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004495 * used by hda_proc.c and hda_eld.c
4496 */
4497void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4498{
4499 static unsigned int rates[] = {
4500 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4501 96000, 176400, 192000, 384000
4502 };
4503 int i, j;
4504
4505 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4506 if (pcm & (1 << i))
4507 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4508
4509 buf[j] = '\0'; /* necessary when j == 0 */
4510}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004511EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004512
Takashi Iwaid5191e52009-11-16 14:58:17 +01004513/**
4514 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4515 * @pcm: PCM caps bits
4516 * @buf: the string buffer to write
4517 * @buflen: the max buffer length
4518 *
4519 * used by hda_proc.c and hda_eld.c
4520 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004521void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4522{
4523 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4524 int i, j;
4525
4526 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4527 if (pcm & (AC_SUPPCM_BITS_8 << i))
4528 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4529
4530 buf[j] = '\0'; /* necessary when j == 0 */
4531}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004532EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004533
4534MODULE_DESCRIPTION("HDA codec core");
4535MODULE_LICENSE("GPL");