blob: a3d638c8c1fd96a407c64b36ee2098d60e087bea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
32#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020033#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020034#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * vendor / preset table
38 */
39
40struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
43};
44
45/* codec vendor labels */
46static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010047 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020048 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020049 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010050 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010051 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010052 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020053 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010054 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020055 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010056 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020057 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020059 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010060 { 0x17e8, "Chrontel" },
61 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000062 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010064 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020065 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 {} /* terminator */
67};
68
Takashi Iwai1289e9e2008-11-27 15:47:11 +010069static DEFINE_MUTEX(preset_mutex);
70static LIST_HEAD(hda_preset_tables);
71
72int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
73{
74 mutex_lock(&preset_mutex);
75 list_add_tail(&preset->list, &hda_preset_tables);
76 mutex_unlock(&preset_mutex);
77 return 0;
78}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010079EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010080
81int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
82{
83 mutex_lock(&preset_mutex);
84 list_del(&preset->list);
85 mutex_unlock(&preset_mutex);
86 return 0;
87}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010088EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwaicb53c622007-08-10 17:21:45 +020090#ifdef CONFIG_SND_HDA_POWER_SAVE
91static void hda_power_work(struct work_struct *work);
92static void hda_keep_power_on(struct hda_codec *codec);
93#else
94static inline void hda_keep_power_on(struct hda_codec *codec) {}
95#endif
96
Takashi Iwaid5191e52009-11-16 14:58:17 +010097/**
98 * snd_hda_get_jack_location - Give a location string of the jack
99 * @cfg: pin default config value
100 *
101 * Parse the pin default config value and returns the string of the
102 * jack location, e.g. "Rear", "Front", etc.
103 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400104const char *snd_hda_get_jack_location(u32 cfg)
105{
106 static char *bases[7] = {
107 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
108 };
109 static unsigned char specials_idx[] = {
110 0x07, 0x08,
111 0x17, 0x18, 0x19,
112 0x37, 0x38
113 };
114 static char *specials[] = {
115 "Rear Panel", "Drive Bar",
116 "Riser", "HDMI", "ATAPI",
117 "Mobile-In", "Mobile-Out"
118 };
119 int i;
120 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
121 if ((cfg & 0x0f) < 7)
122 return bases[cfg & 0x0f];
123 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
124 if (cfg == specials_idx[i])
125 return specials[i];
126 }
127 return "UNKNOWN";
128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100129EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400130
Takashi Iwaid5191e52009-11-16 14:58:17 +0100131/**
132 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
133 * @cfg: pin default config value
134 *
135 * Parse the pin default config value and returns the string of the
136 * jack connectivity, i.e. external or internal connection.
137 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400138const char *snd_hda_get_jack_connectivity(u32 cfg)
139{
140 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
141
142 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100144EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400145
Takashi Iwaid5191e52009-11-16 14:58:17 +0100146/**
147 * snd_hda_get_jack_type - Give a type string of the jack
148 * @cfg: pin default config value
149 *
150 * Parse the pin default config value and returns the string of the
151 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
152 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400153const char *snd_hda_get_jack_type(u32 cfg)
154{
155 static char *jack_types[16] = {
156 "Line Out", "Speaker", "HP Out", "CD",
157 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
158 "Line In", "Aux", "Mic", "Telephony",
159 "SPDIF In", "Digitial In", "Reserved", "Other"
160 };
161
162 return jack_types[(cfg & AC_DEFCFG_DEVICE)
163 >> AC_DEFCFG_DEVICE_SHIFT];
164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100165EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400166
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100167/*
168 * Compose a 32bit command word to be sent to the HD-audio controller
169 */
170static inline unsigned int
171make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
172 unsigned int verb, unsigned int parm)
173{
174 u32 val;
175
Takashi Iwai82e1b802009-07-17 12:47:34 +0200176 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
177 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800178 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
179 codec->addr, direct, nid, verb, parm);
180 return ~0;
181 }
182
183 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184 val |= (u32)direct << 27;
185 val |= (u32)nid << 20;
186 val |= verb << 8;
187 val |= parm;
188 return val;
189}
190
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200191/*
192 * Send and receive a verb
193 */
194static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
195 unsigned int *res)
196{
197 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200198 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200199
Wu Fengguang6430aee2009-07-17 16:49:19 +0800200 if (cmd == ~0)
201 return -1;
202
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200203 if (res)
204 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200205 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206 snd_hda_power_up(codec);
207 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200209 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800210 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200211 mutex_unlock(&bus->cmd_mutex);
212 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200213 if (res && *res == -1 && bus->rirb_error) {
214 if (bus->response_reset) {
215 snd_printd("hda_codec: resetting BUS due to "
216 "fatal communication error\n");
217 bus->ops.bus_reset(bus);
218 }
219 goto again;
220 }
221 /* clear reset-flag when the communication gets recovered */
222 if (!err)
223 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224 return err;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/**
228 * snd_hda_codec_read - send a command and get the response
229 * @codec: the HDA codec
230 * @nid: NID to send the command
231 * @direct: direct flag
232 * @verb: the verb to send
233 * @parm: the parameter for the verb
234 *
235 * Send a single command and read the corresponding response.
236 *
237 * Returns the obtained response value, or -1 for an error.
238 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200239unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
240 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned int verb, unsigned int parm)
242{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200243 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
244 unsigned int res;
245 codec_exec_verb(codec, cmd, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return res;
247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100248EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/**
251 * snd_hda_codec_write - send a single command without waiting for response
252 * @codec: the HDA codec
253 * @nid: NID to send the command
254 * @direct: direct flag
255 * @verb: the verb to send
256 * @parm: the parameter for the verb
257 *
258 * Send a single command without waiting for response.
259 *
260 * Returns 0 if successful, or a negative error code.
261 */
262int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
263 unsigned int verb, unsigned int parm)
264{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200265 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100266 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200267 return codec_exec_verb(codec, cmd,
268 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100270EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/**
273 * snd_hda_sequence_write - sequence writes
274 * @codec: the HDA codec
275 * @seq: VERB array to send
276 *
277 * Send the commands sequentially from the given array.
278 * The array must be terminated with NID=0.
279 */
280void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
281{
282 for (; seq->nid; seq++)
283 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100285EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/**
288 * snd_hda_get_sub_nodes - get the range of sub nodes
289 * @codec: the HDA codec
290 * @nid: NID to parse
291 * @start_id: the pointer to store the start NID
292 *
293 * Parse the NID and store the start NID of its sub-nodes.
294 * Returns the number of sub-nodes.
295 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200296int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
297 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned int parm;
300
301 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200302 if (parm == -1)
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *start_id = (parm >> 16) & 0x7fff;
305 return (int)(parm & 0x7fff);
306}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100307EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
310 * snd_hda_get_connections - get connection list
311 * @codec: the HDA codec
312 * @nid: NID to parse
313 * @conn_list: connection list array
314 * @max_conns: max. number of connections to store
315 *
316 * Parses the connection list of the given widget and stores the list
317 * of NIDs.
318 *
319 * Returns the number of connections, or a negative error code.
320 */
321int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
322 hda_nid_t *conn_list, int max_conns)
323{
324 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100325 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200327 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100328 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Takashi Iwaida3cec32008-08-08 17:12:14 +0200330 if (snd_BUG_ON(!conn_list || max_conns <= 0))
331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200333 wcaps = get_wcaps(codec, nid);
334 if (!(wcaps & AC_WCAP_CONN_LIST) &&
335 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200336 snd_printk(KERN_WARNING "hda_codec: "
337 "connection list not available for 0x%x\n", nid);
338 return -EINVAL;
339 }
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
342 if (parm & AC_CLIST_LONG) {
343 /* long form */
344 shift = 16;
345 num_elems = 2;
346 } else {
347 /* short form */
348 shift = 8;
349 num_elems = 4;
350 }
351 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mask = (1 << (shift-1)) - 1;
353
Takashi Iwai0ba21762007-04-16 11:29:14 +0200354 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0; /* no connection */
356
357 if (conn_len == 1) {
358 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200359 parm = snd_hda_codec_read(codec, nid, 0,
360 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200361 if (parm == -1 && codec->bus->rirb_error)
362 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 conn_list[0] = parm & mask;
364 return 1;
365 }
366
367 /* multi connection */
368 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100369 prev_nid = 0;
370 for (i = 0; i < conn_len; i++) {
371 int range_val;
372 hda_nid_t val, n;
373
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200374 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100375 parm = snd_hda_codec_read(codec, nid, 0,
376 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200377 if (parm == -1 && codec->bus->rirb_error)
378 return -EIO;
379 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200380 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100381 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200382 if (val == 0) {
383 snd_printk(KERN_WARNING "hda_codec: "
384 "invalid CONNECT_LIST verb %x[%i]:%x\n",
385 nid, i, parm);
386 return 0;
387 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100388 parm >>= shift;
389 if (range_val) {
390 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200391 if (!prev_nid || prev_nid >= val) {
392 snd_printk(KERN_WARNING "hda_codec: "
393 "invalid dep_range_val %x:%x\n",
394 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100395 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100397 for (n = prev_nid + 1; n <= val; n++) {
398 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200399 snd_printk(KERN_ERR
400 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100401 return -EINVAL;
402 }
403 conn_list[conns++] = n;
404 }
405 } else {
406 if (conns >= max_conns) {
407 snd_printk(KERN_ERR "Too many connections\n");
408 return -EINVAL;
409 }
410 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100412 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 return conns;
415}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100416EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418
419/**
420 * snd_hda_queue_unsol_event - add an unsolicited event to queue
421 * @bus: the BUS
422 * @res: unsolicited event (lower 32bit of RIRB entry)
423 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
424 *
425 * Adds the given event to the queue. The events are processed in
426 * the workqueue asynchronously. Call this function in the interrupt
427 * hanlder when RIRB receives an unsolicited event.
428 *
429 * Returns 0 if successful, or a negative error code.
430 */
431int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
432{
433 struct hda_bus_unsolicited *unsol;
434 unsigned int wp;
435
Takashi Iwai0ba21762007-04-16 11:29:14 +0200436 unsol = bus->unsol;
437 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439
440 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
441 unsol->wp = wp;
442
443 wp <<= 1;
444 unsol->queue[wp] = res;
445 unsol->queue[wp + 1] = res_ex;
446
Takashi Iwai6acaed32009-01-12 10:09:24 +0100447 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return 0;
450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100451EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800454 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
David Howellsc4028952006-11-22 14:57:56 +0000456static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
David Howellsc4028952006-11-22 14:57:56 +0000458 struct hda_bus_unsolicited *unsol =
459 container_of(work, struct hda_bus_unsolicited, work);
460 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct hda_codec *codec;
462 unsigned int rp, caddr, res;
463
464 while (unsol->rp != unsol->wp) {
465 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
466 unsol->rp = rp;
467 rp <<= 1;
468 res = unsol->queue[rp];
469 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200470 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 continue;
472 codec = bus->caddr_tbl[caddr & 0x0f];
473 if (codec && codec->patch_ops.unsol_event)
474 codec->patch_ops.unsol_event(codec, res);
475 }
476}
477
478/*
479 * initialize unsolicited queue
480 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200481static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct hda_bus_unsolicited *unsol;
484
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100485 if (bus->unsol) /* already initialized */
486 return 0;
487
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200488 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200489 if (!unsol) {
490 snd_printk(KERN_ERR "hda_codec: "
491 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -ENOMEM;
493 }
David Howellsc4028952006-11-22 14:57:56 +0000494 INIT_WORK(&unsol->work, process_unsol_events);
495 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 bus->unsol = unsol;
497 return 0;
498}
499
500/*
501 * destructor
502 */
503static void snd_hda_codec_free(struct hda_codec *codec);
504
505static int snd_hda_bus_free(struct hda_bus *bus)
506{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200507 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Takashi Iwai0ba21762007-04-16 11:29:14 +0200509 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100511 if (bus->workq)
512 flush_workqueue(bus->workq);
513 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200515 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 snd_hda_codec_free(codec);
517 }
518 if (bus->ops.private_free)
519 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100520 if (bus->workq)
521 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 kfree(bus);
523 return 0;
524}
525
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100526static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100529 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return snd_hda_bus_free(bus);
531}
532
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200533#ifdef CONFIG_SND_HDA_HWDEP
534static int snd_hda_bus_dev_register(struct snd_device *device)
535{
536 struct hda_bus *bus = device->device_data;
537 struct hda_codec *codec;
538 list_for_each_entry(codec, &bus->codec_list, list) {
539 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100540 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200541 }
542 return 0;
543}
544#else
545#define snd_hda_bus_dev_register NULL
546#endif
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/**
549 * snd_hda_bus_new - create a HDA bus
550 * @card: the card entry
551 * @temp: the template for hda_bus information
552 * @busp: the pointer to store the created bus instance
553 *
554 * Returns 0 if successful, or a negative error code.
555 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100556int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200557 const struct hda_bus_template *temp,
558 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct hda_bus *bus;
561 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100562 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200563 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .dev_free = snd_hda_bus_dev_free,
565 };
566
Takashi Iwaida3cec32008-08-08 17:12:14 +0200567 if (snd_BUG_ON(!temp))
568 return -EINVAL;
569 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (busp)
573 *busp = NULL;
574
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200575 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (bus == NULL) {
577 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
578 return -ENOMEM;
579 }
580
581 bus->card = card;
582 bus->private_data = temp->private_data;
583 bus->pci = temp->pci;
584 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100585 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 bus->ops = temp->ops;
587
Ingo Molnar62932df2006-01-16 16:34:20 +0100588 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 INIT_LIST_HEAD(&bus->codec_list);
590
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100591 snprintf(bus->workq_name, sizeof(bus->workq_name),
592 "hd-audio%d", card->number);
593 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100594 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100595 snd_printk(KERN_ERR "cannot create workqueue %s\n",
596 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100597 kfree(bus);
598 return -ENOMEM;
599 }
600
Takashi Iwai0ba21762007-04-16 11:29:14 +0200601 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
602 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 snd_hda_bus_free(bus);
604 return err;
605 }
606 if (busp)
607 *busp = bus;
608 return 0;
609}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100610EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Takashi Iwai82467612007-07-27 19:15:54 +0200612#ifdef CONFIG_SND_HDA_GENERIC
613#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200614 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200615#else
616#define is_generic_config(codec) 0
617#endif
618
Takashi Iwai645f10c2008-11-28 15:07:37 +0100619#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100620#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
621#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100622#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100623#endif
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*
626 * find a matching codec preset
627 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200628static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200629find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100631 struct hda_codec_preset_list *tbl;
632 const struct hda_codec_preset *preset;
633 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Takashi Iwai82467612007-07-27 19:15:54 +0200635 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100636 return NULL; /* use the generic parser */
637
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100638 again:
639 mutex_lock(&preset_mutex);
640 list_for_each_entry(tbl, &hda_preset_tables, list) {
641 if (!try_module_get(tbl->owner)) {
642 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
643 continue;
644 }
645 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100647 if (preset->afg && preset->afg != codec->afg)
648 continue;
649 if (preset->mfg && preset->mfg != codec->mfg)
650 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200651 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200653 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200654 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100655 preset->rev == codec->revision_id)) {
656 mutex_unlock(&preset_mutex);
657 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100661 module_put(tbl->owner);
662 }
663 mutex_unlock(&preset_mutex);
664
665 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
666 char name[32];
667 if (!mod_requested)
668 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
669 codec->vendor_id);
670 else
671 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
672 (codec->vendor_id >> 16) & 0xffff);
673 request_module(name);
674 mod_requested++;
675 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 return NULL;
678}
679
680/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200681 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200683static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 const struct hda_vendor_id *c;
686 const char *vendor = NULL;
687 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200688 char tmp[16];
689
690 if (codec->vendor_name)
691 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 for (c = hda_vendor_ids; c->id; c++) {
694 if (c->id == vendor_id) {
695 vendor = c->name;
696 break;
697 }
698 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200699 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 sprintf(tmp, "Generic %04x", vendor_id);
701 vendor = tmp;
702 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200703 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
704 if (!codec->vendor_name)
705 return -ENOMEM;
706
707 get_chip_name:
708 if (codec->chip_name)
709 return 0;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200712 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
713 else {
714 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
715 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
716 }
717 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200718 return -ENOMEM;
719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200723 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100725static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200727 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 hda_nid_t nid;
729
730 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
731 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200732 function_id = snd_hda_param_read(codec, nid,
Pascal de Bruijn234b4342009-03-23 11:15:59 +0100733 AC_PAR_FUNCTION_TYPE) & 0xff;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200734 switch (function_id) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200735 case AC_GRP_AUDIO_FUNCTION:
736 codec->afg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200737 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200738 break;
739 case AC_GRP_MODEM_FUNCTION:
740 codec->mfg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200741 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200742 break;
743 default:
744 break;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100750 * read widget caps for each widget and store in cache
751 */
752static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
753{
754 int i;
755 hda_nid_t nid;
756
757 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
758 &codec->start_nid);
759 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200760 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100761 return -ENOMEM;
762 nid = codec->start_nid;
763 for (i = 0; i < codec->num_nodes; i++, nid++)
764 codec->wcaps[i] = snd_hda_param_read(codec, nid,
765 AC_PAR_AUDIO_WIDGET_CAP);
766 return 0;
767}
768
Takashi Iwai3be14142009-02-20 14:11:16 +0100769/* read all pin default configurations and save codec->init_pins */
770static int read_pin_defaults(struct hda_codec *codec)
771{
772 int i;
773 hda_nid_t nid = codec->start_nid;
774
775 for (i = 0; i < codec->num_nodes; i++, nid++) {
776 struct hda_pincfg *pin;
777 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200778 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100779 if (wid_type != AC_WID_PIN)
780 continue;
781 pin = snd_array_new(&codec->init_pins);
782 if (!pin)
783 return -ENOMEM;
784 pin->nid = nid;
785 pin->cfg = snd_hda_codec_read(codec, nid, 0,
786 AC_VERB_GET_CONFIG_DEFAULT, 0);
787 }
788 return 0;
789}
790
791/* look up the given pin config list and return the item matching with NID */
792static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
793 struct snd_array *array,
794 hda_nid_t nid)
795{
796 int i;
797 for (i = 0; i < array->used; i++) {
798 struct hda_pincfg *pin = snd_array_elem(array, i);
799 if (pin->nid == nid)
800 return pin;
801 }
802 return NULL;
803}
804
805/* write a config value for the given NID */
806static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
807 unsigned int cfg)
808{
809 int i;
810 for (i = 0; i < 4; i++) {
811 snd_hda_codec_write(codec, nid, 0,
812 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
813 cfg & 0xff);
814 cfg >>= 8;
815 }
816}
817
818/* set the current pin config value for the given NID.
819 * the value is cached, and read via snd_hda_codec_get_pincfg()
820 */
821int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
822 hda_nid_t nid, unsigned int cfg)
823{
824 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100825 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100826
Takashi Iwaib82855a2009-12-27 11:24:56 +0100827 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
828 return -EINVAL;
829
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100830 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100831 pin = look_up_pincfg(codec, list, nid);
832 if (!pin) {
833 pin = snd_array_new(list);
834 if (!pin)
835 return -ENOMEM;
836 pin->nid = nid;
837 }
838 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100839
840 /* change only when needed; e.g. if the pincfg is already present
841 * in user_pins[], don't write it
842 */
843 cfg = snd_hda_codec_get_pincfg(codec, nid);
844 if (oldcfg != cfg)
845 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100846 return 0;
847}
848
Takashi Iwaid5191e52009-11-16 14:58:17 +0100849/**
850 * snd_hda_codec_set_pincfg - Override a pin default configuration
851 * @codec: the HDA codec
852 * @nid: NID to set the pin config
853 * @cfg: the pin default config value
854 *
855 * Override a pin default configuration value in the cache.
856 * This value can be read by snd_hda_codec_get_pincfg() in a higher
857 * priority than the real hardware value.
858 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100859int snd_hda_codec_set_pincfg(struct hda_codec *codec,
860 hda_nid_t nid, unsigned int cfg)
861{
Takashi Iwai346ff702009-02-23 09:42:57 +0100862 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100863}
864EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
865
Takashi Iwaid5191e52009-11-16 14:58:17 +0100866/**
867 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
868 * @codec: the HDA codec
869 * @nid: NID to get the pin config
870 *
871 * Get the current pin config value of the given pin NID.
872 * If the pincfg value is cached or overridden via sysfs or driver,
873 * returns the cached value.
874 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100875unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
876{
877 struct hda_pincfg *pin;
878
Takashi Iwai3be14142009-02-20 14:11:16 +0100879#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100880 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100881 if (pin)
882 return pin->cfg;
883#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100884 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
885 if (pin)
886 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100887 pin = look_up_pincfg(codec, &codec->init_pins, nid);
888 if (pin)
889 return pin->cfg;
890 return 0;
891}
892EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
893
894/* restore all current pin configs */
895static void restore_pincfgs(struct hda_codec *codec)
896{
897 int i;
898 for (i = 0; i < codec->init_pins.used; i++) {
899 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
900 set_pincfg(codec, pin->nid,
901 snd_hda_codec_get_pincfg(codec, pin->nid));
902 }
903}
Takashi Iwai54d17402005-11-21 16:33:22 +0100904
Takashi Iwai92ee6162009-12-27 11:18:59 +0100905/**
906 * snd_hda_shutup_pins - Shut up all pins
907 * @codec: the HDA codec
908 *
909 * Clear all pin controls to shup up before suspend for avoiding click noise.
910 * The controls aren't cached so that they can be resumed properly.
911 */
912void snd_hda_shutup_pins(struct hda_codec *codec)
913{
914 int i;
915 for (i = 0; i < codec->init_pins.used; i++) {
916 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
917 /* use read here for syncing after issuing each verb */
918 snd_hda_codec_read(codec, pin->nid, 0,
919 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
920 }
921}
922EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
923
Takashi Iwai01751f52007-08-10 16:59:39 +0200924static void init_hda_cache(struct hda_cache_rec *cache,
925 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200926static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200927
Takashi Iwai3be14142009-02-20 14:11:16 +0100928/* restore the initial pin cfgs and release all pincfg lists */
929static void restore_init_pincfgs(struct hda_codec *codec)
930{
Takashi Iwai346ff702009-02-23 09:42:57 +0100931 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100932 * so that only the values in init_pins are restored
933 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100934 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100935#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100936 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100937#endif
938 restore_pincfgs(codec);
939 snd_array_free(&codec->init_pins);
940}
941
Takashi Iwai54d17402005-11-21 16:33:22 +0100942/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * codec destructor
944 */
945static void snd_hda_codec_free(struct hda_codec *codec)
946{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200947 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return;
Takashi Iwai3be14142009-02-20 14:11:16 +0100949 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200950#ifdef CONFIG_SND_HDA_POWER_SAVE
951 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100952 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200955 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100956 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 codec->bus->caddr_tbl[codec->addr] = NULL;
958 if (codec->patch_ops.free)
959 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100960 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200961 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200962 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200963 kfree(codec->vendor_name);
964 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200965 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100966 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 kfree(codec);
968}
969
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100970static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
971 unsigned int power_state);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/**
974 * snd_hda_codec_new - create a HDA codec
975 * @bus: the bus to assign
976 * @codec_addr: the codec address
977 * @codecp: the pointer to store the generated codec
978 *
979 * Returns 0 if successful, or a negative error code.
980 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +0100981int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
982 unsigned int codec_addr,
983 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200986 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int err;
988
Takashi Iwaida3cec32008-08-08 17:12:14 +0200989 if (snd_BUG_ON(!bus))
990 return -EINVAL;
991 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
992 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200995 snd_printk(KERN_ERR "hda_codec: "
996 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -EBUSY;
998 }
999
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001000 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (codec == NULL) {
1002 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1003 return -ENOMEM;
1004 }
1005
1006 codec->bus = bus;
1007 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001008 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001009 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001010 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001011 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001012 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1013 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001014 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001015 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001016 if (codec->bus->modelname) {
1017 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1018 if (!codec->modelname) {
1019 snd_hda_codec_free(codec);
1020 return -ENODEV;
1021 }
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Takashi Iwaicb53c622007-08-10 17:21:45 +02001024#ifdef CONFIG_SND_HDA_POWER_SAVE
1025 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1026 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1027 * the caller has to power down appropriatley after initialization
1028 * phase.
1029 */
1030 hda_keep_power_on(codec);
1031#endif
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 list_add_tail(&codec->list, &bus->codec_list);
1034 bus->caddr_tbl[codec_addr] = codec;
1035
Takashi Iwai0ba21762007-04-16 11:29:14 +02001036 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1037 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001038 if (codec->vendor_id == -1)
1039 /* read again, hopefully the access method was corrected
1040 * in the last read...
1041 */
1042 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1043 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001044 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1045 AC_PAR_SUBSYSTEM_ID);
1046 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1047 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001049 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001050 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001051 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001052 err = -ENODEV;
1053 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Takashi Iwai3be14142009-02-20 14:11:16 +01001056 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1057 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001058 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001059 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001060 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001061 err = read_pin_defaults(codec);
1062 if (err < 0)
1063 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001064
Takashi Iwai0ba21762007-04-16 11:29:14 +02001065 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001066 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001067 codec->subsystem_id =
1068 snd_hda_codec_read(codec, nid, 0,
1069 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001070 }
1071
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001072 /* power-up all before initialization */
1073 hda_set_power_state(codec,
1074 codec->afg ? codec->afg : codec->mfg,
1075 AC_PWRST_D0);
1076
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001077 snd_hda_codec_proc_new(codec);
1078
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001079 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001080
1081 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1082 codec->subsystem_id, codec->revision_id);
1083 snd_component_add(codec->bus->card, component);
1084
1085 if (codecp)
1086 *codecp = codec;
1087 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001088
1089 error:
1090 snd_hda_codec_free(codec);
1091 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001093EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001094
Takashi Iwaid5191e52009-11-16 14:58:17 +01001095/**
1096 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1097 * @codec: the HDA codec
1098 *
1099 * Start parsing of the given codec tree and (re-)initialize the whole
1100 * patch instance.
1101 *
1102 * Returns 0 if successful or a negative error code.
1103 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001104int snd_hda_codec_configure(struct hda_codec *codec)
1105{
1106 int err;
1107
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001108 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001109 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001110 err = get_codec_name(codec);
1111 if (err < 0)
1112 return err;
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Takashi Iwai82467612007-07-27 19:15:54 +02001115 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001117 goto patched;
1118 }
Takashi Iwai82467612007-07-27 19:15:54 +02001119 if (codec->preset && codec->preset->patch) {
1120 err = codec->preset->patch(codec);
1121 goto patched;
1122 }
1123
1124 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001125 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001126 if (err < 0)
1127 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001128
1129 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001130 if (!err && codec->patch_ops.unsol_event)
1131 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001132 /* audio codec should override the mixer name */
1133 if (!err && (codec->afg || !*codec->bus->card->mixername))
1134 snprintf(codec->bus->card->mixername,
1135 sizeof(codec->bus->card->mixername),
1136 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001139EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141/**
1142 * snd_hda_codec_setup_stream - set up the codec for streaming
1143 * @codec: the CODEC to set up
1144 * @nid: the NID to set up
1145 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1146 * @channel_id: channel id to pass, zero based.
1147 * @format: stream format.
1148 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001149void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1150 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int channel_id, int format)
1152{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001153 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001154 return;
1155
Takashi Iwai0ba21762007-04-16 11:29:14 +02001156 snd_printdd("hda_codec_setup_stream: "
1157 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 nid, stream_tag, channel_id, format);
1159 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1160 (stream_tag << 4) | channel_id);
1161 msleep(1);
1162 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1163}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001164EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Takashi Iwaid5191e52009-11-16 14:58:17 +01001166/**
1167 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1168 * @codec: the CODEC to clean up
1169 * @nid: the NID to clean up
1170 */
Takashi Iwai888afa12008-03-18 09:57:50 +01001171void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1172{
1173 if (!nid)
1174 return;
1175
1176 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1177 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1178#if 0 /* keep the format */
1179 msleep(1);
1180 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1181#endif
1182}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001183EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185/*
1186 * amp access functions
1187 */
1188
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001189/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001190#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001191#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001192#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1193#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001195#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001198static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001199 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Takashi Iwai01751f52007-08-10 16:59:39 +02001201 memset(cache, 0, sizeof(*cache));
1202 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001203 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001204}
1205
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001206static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001207{
Takashi Iwai603c4012008-07-30 15:01:44 +02001208 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001212static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwai01751f52007-08-10 16:59:39 +02001214 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1215 u16 cur = cache->hash[idx];
1216 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001219 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (info->key == key)
1221 return info;
1222 cur = info->next;
1223 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001224 return NULL;
1225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001227/* query the hash. allocate an entry if not found. */
1228static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1229 u32 key)
1230{
1231 struct hda_cache_head *info = get_hash(cache, key);
1232 if (!info) {
1233 u16 idx, cur;
1234 /* add a new hash entry */
1235 info = snd_array_new(&cache->buf);
1236 if (!info)
1237 return NULL;
1238 cur = snd_array_index(&cache->buf, info);
1239 info->key = key;
1240 info->val = 0;
1241 idx = key % (u16)ARRAY_SIZE(cache->hash);
1242 info->next = cache->hash[idx];
1243 cache->hash[idx] = cur;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return info;
1246}
1247
Takashi Iwai01751f52007-08-10 16:59:39 +02001248/* query and allocate an amp hash entry */
1249static inline struct hda_amp_info *
1250get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1251{
1252 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1253}
1254
Takashi Iwaid5191e52009-11-16 14:58:17 +01001255/**
1256 * query_amp_caps - query AMP capabilities
1257 * @codec: the HD-auio codec
1258 * @nid: the NID to query
1259 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1260 *
1261 * Query AMP capabilities for the given widget and direction.
1262 * Returns the obtained capability bits.
1263 *
1264 * When cap bits have been already read, this doesn't read again but
1265 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001267u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001269 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Takashi Iwai0ba21762007-04-16 11:29:14 +02001271 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1272 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001274 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001275 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001277 info->amp_caps = snd_hda_param_read(codec, nid,
1278 direction == HDA_OUTPUT ?
1279 AC_PAR_AMP_OUT_CAP :
1280 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001281 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001282 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 return info->amp_caps;
1285}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001286EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Takashi Iwaid5191e52009-11-16 14:58:17 +01001288/**
1289 * snd_hda_override_amp_caps - Override the AMP capabilities
1290 * @codec: the CODEC to clean up
1291 * @nid: the NID to clean up
1292 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1293 * @caps: the capability bits to set
1294 *
1295 * Override the cached AMP caps bits value by the given one.
1296 * This function is useful if the driver needs to adjust the AMP ranges,
1297 * e.g. limit to 0dB, etc.
1298 *
1299 * Returns zero if successful or a negative error code.
1300 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001301int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1302 unsigned int caps)
1303{
1304 struct hda_amp_info *info;
1305
1306 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1307 if (!info)
1308 return -EINVAL;
1309 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001310 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001311 return 0;
1312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001313EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001314
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001315static unsigned int
1316query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1317 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001318{
1319 struct hda_amp_info *info;
1320
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001321 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001322 if (!info)
1323 return 0;
1324 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001325 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001326 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001327 }
1328 return info->amp_caps;
1329}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001330
1331static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1332{
1333 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1334}
1335
Takashi Iwaid5191e52009-11-16 14:58:17 +01001336/**
1337 * snd_hda_query_pin_caps - Query PIN capabilities
1338 * @codec: the HD-auio codec
1339 * @nid: the NID to query
1340 *
1341 * Query PIN capabilities for the given widget.
1342 * Returns the obtained capability bits.
1343 *
1344 * When cap bits have been already read, this doesn't read again but
1345 * returns the cached value.
1346 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001347u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1348{
1349 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1350 read_pin_cap);
1351}
Takashi Iwai1327a322009-03-23 13:07:47 +01001352EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1353
Wu Fengguang864f92b2009-11-18 12:38:02 +08001354/**
1355 * snd_hda_pin_sense - execute pin sense measurement
1356 * @codec: the CODEC to sense
1357 * @nid: the pin NID to sense
1358 *
1359 * Execute necessary pin sense measurement and return its Presence Detect,
1360 * Impedance, ELD Valid etc. status bits.
1361 */
1362u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1363{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001364 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001365
Takashi Iwai729d55b2009-12-25 22:49:01 +01001366 if (!codec->no_trigger_sense) {
1367 pincap = snd_hda_query_pin_caps(codec, nid);
1368 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001369 snd_hda_codec_read(codec, nid, 0,
1370 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001371 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001372 return snd_hda_codec_read(codec, nid, 0,
1373 AC_VERB_GET_PIN_SENSE, 0);
1374}
1375EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1376
1377/**
1378 * snd_hda_jack_detect - query pin Presence Detect status
1379 * @codec: the CODEC to sense
1380 * @nid: the pin NID to sense
1381 *
1382 * Query and return the pin's Presence Detect status.
1383 */
1384int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1385{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001386 u32 sense = snd_hda_pin_sense(codec, nid);
1387 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001388}
1389EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391/*
1392 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001393 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001395static unsigned int get_vol_mute(struct hda_codec *codec,
1396 struct hda_amp_info *info, hda_nid_t nid,
1397 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 u32 val, parm;
1400
Takashi Iwai01751f52007-08-10 16:59:39 +02001401 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001402 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1405 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1406 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001407 val = snd_hda_codec_read(codec, nid, 0,
1408 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001410 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001411 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
1414/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001415 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001417static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 hda_nid_t nid, int ch, int direction, int index,
1419 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 u32 parm;
1422
1423 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1424 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1425 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1426 parm |= val;
1427 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001428 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Takashi Iwaid5191e52009-11-16 14:58:17 +01001431/**
1432 * snd_hda_codec_amp_read - Read AMP value
1433 * @codec: HD-audio codec
1434 * @nid: NID to read the AMP value
1435 * @ch: channel (left=0 or right=1)
1436 * @direction: #HDA_INPUT or #HDA_OUTPUT
1437 * @index: the index value (only for input direction)
1438 *
1439 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
Takashi Iwai834be882006-03-01 14:16:17 +01001441int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1442 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001444 struct hda_amp_info *info;
1445 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1446 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001448 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001450EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Takashi Iwaid5191e52009-11-16 14:58:17 +01001452/**
1453 * snd_hda_codec_amp_update - update the AMP value
1454 * @codec: HD-audio codec
1455 * @nid: NID to read the AMP value
1456 * @ch: channel (left=0 or right=1)
1457 * @direction: #HDA_INPUT or #HDA_OUTPUT
1458 * @idx: the index value (only for input direction)
1459 * @mask: bit mask to set
1460 * @val: the bits value to set
1461 *
1462 * Update the AMP value with a bit mask.
1463 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001464 */
Takashi Iwai834be882006-03-01 14:16:17 +01001465int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1466 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001468 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001469
Takashi Iwai0ba21762007-04-16 11:29:14 +02001470 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1471 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001473 if (snd_BUG_ON(mask & ~0xff))
1474 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001475 val &= mask;
1476 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001477 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001479 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 1;
1481}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001482EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Takashi Iwaid5191e52009-11-16 14:58:17 +01001484/**
1485 * snd_hda_codec_amp_stereo - update the AMP stereo values
1486 * @codec: HD-audio codec
1487 * @nid: NID to read the AMP value
1488 * @direction: #HDA_INPUT or #HDA_OUTPUT
1489 * @idx: the index value (only for input direction)
1490 * @mask: bit mask to set
1491 * @val: the bits value to set
1492 *
1493 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1494 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001495 */
1496int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1497 int direction, int idx, int mask, int val)
1498{
1499 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001500
1501 if (snd_BUG_ON(mask & ~0xff))
1502 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001503 for (ch = 0; ch < 2; ch++)
1504 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1505 idx, mask, val);
1506 return ret;
1507}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001508EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001509
Takashi Iwaicb53c622007-08-10 17:21:45 +02001510#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001511/**
1512 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1513 * @codec: HD-audio codec
1514 *
1515 * Resume the all amp commands from the cache.
1516 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001517void snd_hda_codec_resume_amp(struct hda_codec *codec)
1518{
Takashi Iwai603c4012008-07-30 15:01:44 +02001519 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001520 int i;
1521
Takashi Iwai603c4012008-07-30 15:01:44 +02001522 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001523 u32 key = buffer->head.key;
1524 hda_nid_t nid;
1525 unsigned int idx, dir, ch;
1526 if (!key)
1527 continue;
1528 nid = key & 0xff;
1529 idx = (key >> 16) & 0xff;
1530 dir = (key >> 24) & 0xff;
1531 for (ch = 0; ch < 2; ch++) {
1532 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1533 continue;
1534 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1535 buffer->vol[ch]);
1536 }
1537 }
1538}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001539EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001540#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Takashi Iwaid5191e52009-11-16 14:58:17 +01001542/**
1543 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1544 *
1545 * The control element is supposed to have the private_value field
1546 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1547 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001548int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1549 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1552 u16 nid = get_amp_nid(kcontrol);
1553 u8 chs = get_amp_channels(kcontrol);
1554 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001555 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 u32 caps;
1557
1558 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001559 /* num steps */
1560 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1561 if (!caps) {
1562 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001563 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1564 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return -EINVAL;
1566 }
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001567 if (ofs < caps)
1568 caps -= ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1570 uinfo->count = chs == 3 ? 2 : 1;
1571 uinfo->value.integer.min = 0;
1572 uinfo->value.integer.max = caps;
1573 return 0;
1574}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001575EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001577
1578static inline unsigned int
1579read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1580 int ch, int dir, int idx, unsigned int ofs)
1581{
1582 unsigned int val;
1583 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1584 val &= HDA_AMP_VOLMASK;
1585 if (val >= ofs)
1586 val -= ofs;
1587 else
1588 val = 0;
1589 return val;
1590}
1591
1592static inline int
1593update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1594 int ch, int dir, int idx, unsigned int ofs,
1595 unsigned int val)
1596{
1597 if (val > 0)
1598 val += ofs;
1599 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1600 HDA_AMP_VOLMASK, val);
1601}
1602
Takashi Iwaid5191e52009-11-16 14:58:17 +01001603/**
1604 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1605 *
1606 * The control element is supposed to have the private_value field
1607 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1608 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001609int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1610 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1613 hda_nid_t nid = get_amp_nid(kcontrol);
1614 int chs = get_amp_channels(kcontrol);
1615 int dir = get_amp_direction(kcontrol);
1616 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001617 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 long *valp = ucontrol->value.integer.value;
1619
1620 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001621 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001623 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return 0;
1625}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001626EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Takashi Iwaid5191e52009-11-16 14:58:17 +01001628/**
1629 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1630 *
1631 * The control element is supposed to have the private_value field
1632 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1633 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001634int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1635 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1638 hda_nid_t nid = get_amp_nid(kcontrol);
1639 int chs = get_amp_channels(kcontrol);
1640 int dir = get_amp_direction(kcontrol);
1641 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001642 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 long *valp = ucontrol->value.integer.value;
1644 int change = 0;
1645
Takashi Iwaicb53c622007-08-10 17:21:45 +02001646 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001647 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001648 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001649 valp++;
1650 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001651 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001652 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001653 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return change;
1655}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001656EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Takashi Iwaid5191e52009-11-16 14:58:17 +01001658/**
1659 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1660 *
1661 * The control element is supposed to have the private_value field
1662 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1663 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001664int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1665 unsigned int size, unsigned int __user *_tlv)
1666{
1667 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1668 hda_nid_t nid = get_amp_nid(kcontrol);
1669 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001670 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001671 u32 caps, val1, val2;
1672
1673 if (size < 4 * sizeof(unsigned int))
1674 return -ENOMEM;
1675 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001676 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1677 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001678 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001679 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001680 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001681 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1682 return -EFAULT;
1683 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1684 return -EFAULT;
1685 if (put_user(val1, _tlv + 2))
1686 return -EFAULT;
1687 if (put_user(val2, _tlv + 3))
1688 return -EFAULT;
1689 return 0;
1690}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001691EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001692
Takashi Iwaid5191e52009-11-16 14:58:17 +01001693/**
1694 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1695 * @codec: HD-audio codec
1696 * @nid: NID of a reference widget
1697 * @dir: #HDA_INPUT or #HDA_OUTPUT
1698 * @tlv: TLV data to be stored, at least 4 elements
1699 *
1700 * Set (static) TLV data for a virtual master volume using the AMP caps
1701 * obtained from the reference NID.
1702 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001703 */
1704void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1705 unsigned int *tlv)
1706{
1707 u32 caps;
1708 int nums, step;
1709
1710 caps = query_amp_caps(codec, nid, dir);
1711 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1712 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1713 step = (step + 1) * 25;
1714 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1715 tlv[1] = 2 * sizeof(unsigned int);
1716 tlv[2] = -nums * step;
1717 tlv[3] = step;
1718}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001719EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001720
1721/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001722static struct snd_kcontrol *
1723_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1724 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001725{
1726 struct snd_ctl_elem_id id;
1727 memset(&id, 0, sizeof(id));
1728 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001729 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001730 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1731 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001732 strcpy(id.name, name);
1733 return snd_ctl_find_id(codec->bus->card, &id);
1734}
1735
Takashi Iwaid5191e52009-11-16 14:58:17 +01001736/**
1737 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1738 * @codec: HD-audio codec
1739 * @name: ctl id name string
1740 *
1741 * Get the control element with the given id string and IFACE_MIXER.
1742 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001743struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1744 const char *name)
1745{
1746 return _snd_hda_find_mixer_ctl(codec, name, 0);
1747}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001748EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001749
Takashi Iwaid5191e52009-11-16 14:58:17 +01001750/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001751 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001752 * @codec: HD-audio codec
1753 * @nid: corresponding NID (optional)
1754 * @kctl: the control element to assign
1755 *
1756 * Add the given control element to an array inside the codec instance.
1757 * All control elements belonging to a codec are supposed to be added
1758 * by this function so that a proper clean-up works at the free or
1759 * reconfiguration time.
1760 *
1761 * If non-zero @nid is passed, the NID is assigned to the control element.
1762 * The assignment is shown in the codec proc file.
1763 *
1764 * snd_hda_ctl_add() checks the control subdev id field whether
1765 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001766 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1767 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001768 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001769int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1770 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001771{
1772 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001773 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001774 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001775
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001776 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001777 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001778 if (nid == 0)
1779 nid = get_amp_nid_(kctl->private_value);
1780 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001781 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1782 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001783 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001784 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001785 err = snd_ctl_add(codec->bus->card, kctl);
1786 if (err < 0)
1787 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001788 item = snd_array_new(&codec->mixers);
1789 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001790 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001791 item->kctl = kctl;
1792 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001793 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001794 return 0;
1795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001796EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001797
Takashi Iwaid5191e52009-11-16 14:58:17 +01001798/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001799 * snd_hda_add_nid - Assign a NID to a control element
1800 * @codec: HD-audio codec
1801 * @nid: corresponding NID (optional)
1802 * @kctl: the control element to assign
1803 * @index: index to kctl
1804 *
1805 * Add the given control element to an array inside the codec instance.
1806 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1807 * NID:KCTL mapping - for example "Capture Source" selector.
1808 */
1809int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1810 unsigned int index, hda_nid_t nid)
1811{
1812 struct hda_nid_item *item;
1813
1814 if (nid > 0) {
1815 item = snd_array_new(&codec->nids);
1816 if (!item)
1817 return -ENOMEM;
1818 item->kctl = kctl;
1819 item->index = index;
1820 item->nid = nid;
1821 return 0;
1822 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01001823 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1824 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001825 return -EINVAL;
1826}
1827EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1828
1829/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001830 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1831 * @codec: HD-audio codec
1832 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001833void snd_hda_ctls_clear(struct hda_codec *codec)
1834{
1835 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001836 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001837 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001838 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001839 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001840 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001841}
1842
Takashi Iwaia65d6292009-02-23 16:57:04 +01001843/* pseudo device locking
1844 * toggle card->shutdown to allow/disallow the device access (as a hack)
1845 */
1846static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001847{
Takashi Iwaia65d6292009-02-23 16:57:04 +01001848 spin_lock(&card->files_lock);
1849 if (card->shutdown) {
1850 spin_unlock(&card->files_lock);
1851 return -EINVAL;
1852 }
1853 card->shutdown = 1;
1854 spin_unlock(&card->files_lock);
1855 return 0;
1856}
1857
1858static void hda_unlock_devices(struct snd_card *card)
1859{
1860 spin_lock(&card->files_lock);
1861 card->shutdown = 0;
1862 spin_unlock(&card->files_lock);
1863}
1864
Takashi Iwaid5191e52009-11-16 14:58:17 +01001865/**
1866 * snd_hda_codec_reset - Clear all objects assigned to the codec
1867 * @codec: HD-audio codec
1868 *
1869 * This frees the all PCM and control elements assigned to the codec, and
1870 * clears the caches and restores the pin default configurations.
1871 *
1872 * When a device is being used, it returns -EBSY. If successfully freed,
1873 * returns zero.
1874 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001875int snd_hda_codec_reset(struct hda_codec *codec)
1876{
1877 struct snd_card *card = codec->bus->card;
1878 int i, pcm;
1879
1880 if (hda_lock_devices(card) < 0)
1881 return -EBUSY;
1882 /* check whether the codec isn't used by any mixer or PCM streams */
1883 if (!list_empty(&card->ctl_files)) {
1884 hda_unlock_devices(card);
1885 return -EBUSY;
1886 }
1887 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1888 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1889 if (!cpcm->pcm)
1890 continue;
1891 if (cpcm->pcm->streams[0].substream_opened ||
1892 cpcm->pcm->streams[1].substream_opened) {
1893 hda_unlock_devices(card);
1894 return -EBUSY;
1895 }
1896 }
1897
1898 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001899
1900#ifdef CONFIG_SND_HDA_POWER_SAVE
1901 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001902 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001903#endif
1904 snd_hda_ctls_clear(codec);
1905 /* relase PCMs */
1906 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001907 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01001908 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001909 clear_bit(codec->pcm_info[i].device,
1910 codec->bus->pcm_dev_bits);
1911 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001912 }
1913 if (codec->patch_ops.free)
1914 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001915 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001916 codec->spec = NULL;
1917 free_hda_cache(&codec->amp_cache);
1918 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001919 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1920 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01001921 /* free only driver_pins so that init_pins + user_pins are restored */
1922 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001923 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001924 codec->num_pcms = 0;
1925 codec->pcm_info = NULL;
1926 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01001927 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1928 codec->slave_dig_outs = NULL;
1929 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001930 module_put(codec->owner);
1931 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001932
1933 /* allow device access again */
1934 hda_unlock_devices(card);
1935 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001936}
1937
Takashi Iwaid5191e52009-11-16 14:58:17 +01001938/**
1939 * snd_hda_add_vmaster - create a virtual master control and add slaves
1940 * @codec: HD-audio codec
1941 * @name: vmaster control name
1942 * @tlv: TLV data (optional)
1943 * @slaves: slave control names (optional)
1944 *
1945 * Create a virtual master control with the given name. The TLV data
1946 * must be either NULL or a valid data.
1947 *
1948 * @slaves is a NULL-terminated array of strings, each of which is a
1949 * slave control name. All controls with these names are assigned to
1950 * the new virtual master control.
1951 *
1952 * This function returns zero if successful or a negative error code.
1953 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01001954int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1955 unsigned int *tlv, const char **slaves)
1956{
1957 struct snd_kcontrol *kctl;
1958 const char **s;
1959 int err;
1960
Takashi Iwai2f085542008-02-22 18:43:50 +01001961 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1962 ;
1963 if (!*s) {
1964 snd_printdd("No slave found for %s\n", name);
1965 return 0;
1966 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001967 kctl = snd_ctl_make_virtual_master(name, tlv);
1968 if (!kctl)
1969 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001970 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001971 if (err < 0)
1972 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001973
Takashi Iwai2134ea42008-01-10 16:53:55 +01001974 for (s = slaves; *s; s++) {
1975 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01001976 int i = 0;
1977 for (;;) {
1978 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1979 if (!sctl) {
1980 if (!i)
1981 snd_printdd("Cannot find slave %s, "
1982 "skipped\n", *s);
1983 break;
1984 }
1985 err = snd_ctl_add_slave(kctl, sctl);
1986 if (err < 0)
1987 return err;
1988 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001989 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001990 }
1991 return 0;
1992}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001993EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001994
Takashi Iwaid5191e52009-11-16 14:58:17 +01001995/**
1996 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
1997 *
1998 * The control element is supposed to have the private_value field
1999 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2000 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002001int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 int chs = get_amp_channels(kcontrol);
2005
2006 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2007 uinfo->count = chs == 3 ? 2 : 1;
2008 uinfo->value.integer.min = 0;
2009 uinfo->value.integer.max = 1;
2010 return 0;
2011}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002012EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Takashi Iwaid5191e52009-11-16 14:58:17 +01002014/**
2015 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2016 *
2017 * The control element is supposed to have the private_value field
2018 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2019 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002020int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2021 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2024 hda_nid_t nid = get_amp_nid(kcontrol);
2025 int chs = get_amp_channels(kcontrol);
2026 int dir = get_amp_direction(kcontrol);
2027 int idx = get_amp_index(kcontrol);
2028 long *valp = ucontrol->value.integer.value;
2029
2030 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002031 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002032 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002034 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002035 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return 0;
2037}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002038EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Takashi Iwaid5191e52009-11-16 14:58:17 +01002040/**
2041 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2042 *
2043 * The control element is supposed to have the private_value field
2044 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2045 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002046int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2050 hda_nid_t nid = get_amp_nid(kcontrol);
2051 int chs = get_amp_channels(kcontrol);
2052 int dir = get_amp_direction(kcontrol);
2053 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 long *valp = ucontrol->value.integer.value;
2055 int change = 0;
2056
Takashi Iwaicb53c622007-08-10 17:21:45 +02002057 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002058 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002059 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002060 HDA_AMP_MUTE,
2061 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002062 valp++;
2063 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002064 if (chs & 2)
2065 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002066 HDA_AMP_MUTE,
2067 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002068#ifdef CONFIG_SND_HDA_POWER_SAVE
2069 if (codec->patch_ops.check_power_status)
2070 codec->patch_ops.check_power_status(codec, nid);
2071#endif
2072 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return change;
2074}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002075EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Takashi Iwai67d634c2009-11-16 15:35:59 +01002077#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002078/**
2079 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2080 *
2081 * This function calls snd_hda_enable_beep_device(), which behaves differently
2082 * depending on beep_mode option.
2083 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002084int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2085 struct snd_ctl_elem_value *ucontrol)
2086{
2087 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2088 long *valp = ucontrol->value.integer.value;
2089
2090 snd_hda_enable_beep_device(codec, *valp);
2091 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2092}
2093EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002094#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096/*
Takashi Iwai985be542005-11-02 18:26:49 +01002097 * bound volume controls
2098 *
2099 * bind multiple volumes (# indices, from 0)
2100 */
2101
2102#define AMP_VAL_IDX_SHIFT 19
2103#define AMP_VAL_IDX_MASK (0x0f<<19)
2104
Takashi Iwaid5191e52009-11-16 14:58:17 +01002105/**
2106 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2107 *
2108 * The control element is supposed to have the private_value field
2109 * set up via HDA_BIND_MUTE*() macros.
2110 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002111int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2112 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002113{
2114 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2115 unsigned long pval;
2116 int err;
2117
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002118 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002119 pval = kcontrol->private_value;
2120 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2121 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2122 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002123 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002124 return err;
2125}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002126EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002127
Takashi Iwaid5191e52009-11-16 14:58:17 +01002128/**
2129 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2130 *
2131 * The control element is supposed to have the private_value field
2132 * set up via HDA_BIND_MUTE*() macros.
2133 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002134int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2135 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002136{
2137 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2138 unsigned long pval;
2139 int i, indices, err = 0, change = 0;
2140
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002141 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002142 pval = kcontrol->private_value;
2143 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2144 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002145 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2146 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002147 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2148 if (err < 0)
2149 break;
2150 change |= err;
2151 }
2152 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002153 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002154 return err < 0 ? err : change;
2155}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002156EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002157
Takashi Iwaid5191e52009-11-16 14:58:17 +01002158/**
2159 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2160 *
2161 * The control element is supposed to have the private_value field
2162 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002163 */
2164int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2165 struct snd_ctl_elem_info *uinfo)
2166{
2167 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2168 struct hda_bind_ctls *c;
2169 int err;
2170
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002171 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002172 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002173 kcontrol->private_value = *c->values;
2174 err = c->ops->info(kcontrol, uinfo);
2175 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002176 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002177 return err;
2178}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002179EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002180
Takashi Iwaid5191e52009-11-16 14:58:17 +01002181/**
2182 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2183 *
2184 * The control element is supposed to have the private_value field
2185 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2186 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002187int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2188 struct snd_ctl_elem_value *ucontrol)
2189{
2190 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2191 struct hda_bind_ctls *c;
2192 int err;
2193
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002194 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002195 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002196 kcontrol->private_value = *c->values;
2197 err = c->ops->get(kcontrol, ucontrol);
2198 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002199 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002200 return err;
2201}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002202EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002203
Takashi Iwaid5191e52009-11-16 14:58:17 +01002204/**
2205 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2206 *
2207 * The control element is supposed to have the private_value field
2208 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2209 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002210int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_value *ucontrol)
2212{
2213 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2214 struct hda_bind_ctls *c;
2215 unsigned long *vals;
2216 int err = 0, change = 0;
2217
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002218 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002219 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002220 for (vals = c->values; *vals; vals++) {
2221 kcontrol->private_value = *vals;
2222 err = c->ops->put(kcontrol, ucontrol);
2223 if (err < 0)
2224 break;
2225 change |= err;
2226 }
2227 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002228 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002229 return err < 0 ? err : change;
2230}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002231EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002232
Takashi Iwaid5191e52009-11-16 14:58:17 +01002233/**
2234 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2235 *
2236 * The control element is supposed to have the private_value field
2237 * set up via HDA_BIND_VOL() macro.
2238 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002239int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2240 unsigned int size, unsigned int __user *tlv)
2241{
2242 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2243 struct hda_bind_ctls *c;
2244 int err;
2245
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002246 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002247 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002248 kcontrol->private_value = *c->values;
2249 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2250 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002251 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002252 return err;
2253}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002254EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002255
2256struct hda_ctl_ops snd_hda_bind_vol = {
2257 .info = snd_hda_mixer_amp_volume_info,
2258 .get = snd_hda_mixer_amp_volume_get,
2259 .put = snd_hda_mixer_amp_volume_put,
2260 .tlv = snd_hda_mixer_amp_tlv
2261};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002262EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002263
2264struct hda_ctl_ops snd_hda_bind_sw = {
2265 .info = snd_hda_mixer_amp_switch_info,
2266 .get = snd_hda_mixer_amp_switch_get,
2267 .put = snd_hda_mixer_amp_switch_put,
2268 .tlv = snd_hda_mixer_amp_tlv
2269};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002270EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002271
2272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 * SPDIF out controls
2274 */
2275
Takashi Iwai0ba21762007-04-16 11:29:14 +02002276static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2277 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2280 uinfo->count = 1;
2281 return 0;
2282}
2283
Takashi Iwai0ba21762007-04-16 11:29:14 +02002284static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2285 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2288 IEC958_AES0_NONAUDIO |
2289 IEC958_AES0_CON_EMPHASIS_5015 |
2290 IEC958_AES0_CON_NOT_COPYRIGHT;
2291 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2292 IEC958_AES1_CON_ORIGINAL;
2293 return 0;
2294}
2295
Takashi Iwai0ba21762007-04-16 11:29:14 +02002296static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2297 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2300 IEC958_AES0_NONAUDIO |
2301 IEC958_AES0_PRO_EMPHASIS_5015;
2302 return 0;
2303}
2304
Takashi Iwai0ba21762007-04-16 11:29:14 +02002305static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2306 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2309
2310 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2311 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2312 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2313 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2314
2315 return 0;
2316}
2317
2318/* convert from SPDIF status bits to HDA SPDIF bits
2319 * bit 0 (DigEn) is always set zero (to be filled later)
2320 */
2321static unsigned short convert_from_spdif_status(unsigned int sbits)
2322{
2323 unsigned short val = 0;
2324
2325 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002326 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002328 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002330 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2331 IEC958_AES0_PRO_EMPHASIS_5015)
2332 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002334 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2335 IEC958_AES0_CON_EMPHASIS_5015)
2336 val |= AC_DIG1_EMPHASIS;
2337 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2338 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002340 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2342 }
2343 return val;
2344}
2345
2346/* convert to SPDIF status bits from HDA SPDIF bits
2347 */
2348static unsigned int convert_to_spdif_status(unsigned short val)
2349{
2350 unsigned int sbits = 0;
2351
Takashi Iwai0ba21762007-04-16 11:29:14 +02002352 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002354 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 sbits |= IEC958_AES0_PROFESSIONAL;
2356 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002357 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2359 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002360 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002362 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002364 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2366 sbits |= val & (0x7f << 8);
2367 }
2368 return sbits;
2369}
2370
Takashi Iwai2f728532008-09-25 16:32:41 +02002371/* set digital convert verbs both for the given NID and its slaves */
2372static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2373 int verb, int val)
2374{
2375 hda_nid_t *d;
2376
Takashi Iwai9e976972008-11-25 08:17:20 +01002377 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002378 d = codec->slave_dig_outs;
2379 if (!d)
2380 return;
2381 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002382 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002383}
2384
2385static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2386 int dig1, int dig2)
2387{
2388 if (dig1 != -1)
2389 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2390 if (dig2 != -1)
2391 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2392}
2393
Takashi Iwai0ba21762007-04-16 11:29:14 +02002394static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2398 hda_nid_t nid = kcontrol->private_value;
2399 unsigned short val;
2400 int change;
2401
Ingo Molnar62932df2006-01-16 16:34:20 +01002402 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 codec->spdif_status = ucontrol->value.iec958.status[0] |
2404 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2405 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2406 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2407 val = convert_from_spdif_status(codec->spdif_status);
2408 val |= codec->spdif_ctls & 1;
2409 change = codec->spdif_ctls != val;
2410 codec->spdif_ctls = val;
2411
Takashi Iwai2f728532008-09-25 16:32:41 +02002412 if (change)
2413 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Ingo Molnar62932df2006-01-16 16:34:20 +01002415 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return change;
2417}
2418
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002419#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Takashi Iwai0ba21762007-04-16 11:29:14 +02002421static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2425
Takashi Iwai0ba21762007-04-16 11:29:14 +02002426 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return 0;
2428}
2429
Takashi Iwai0ba21762007-04-16 11:29:14 +02002430static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2431 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2434 hda_nid_t nid = kcontrol->private_value;
2435 unsigned short val;
2436 int change;
2437
Ingo Molnar62932df2006-01-16 16:34:20 +01002438 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002439 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002441 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002443 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002445 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002446 /* unmute amp switch (if any) */
2447 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002448 (val & AC_DIG1_ENABLE))
2449 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2450 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002452 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return change;
2454}
2455
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002456static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 {
2458 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2459 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002460 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 .info = snd_hda_spdif_mask_info,
2462 .get = snd_hda_spdif_cmask_get,
2463 },
2464 {
2465 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2466 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002467 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 .info = snd_hda_spdif_mask_info,
2469 .get = snd_hda_spdif_pmask_get,
2470 },
2471 {
2472 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002473 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 .info = snd_hda_spdif_mask_info,
2475 .get = snd_hda_spdif_default_get,
2476 .put = snd_hda_spdif_default_put,
2477 },
2478 {
2479 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002480 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 .info = snd_hda_spdif_out_switch_info,
2482 .get = snd_hda_spdif_out_switch_get,
2483 .put = snd_hda_spdif_out_switch_put,
2484 },
2485 { } /* end */
2486};
2487
Takashi Iwai09f99702008-02-04 12:31:13 +01002488#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490/**
2491 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2492 * @codec: the HDA codec
2493 * @nid: audio out widget NID
2494 *
2495 * Creates controls related with the SPDIF output.
2496 * Called from each patch supporting the SPDIF out.
2497 *
2498 * Returns 0 if successful, or a negative error code.
2499 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002500int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002503 struct snd_kcontrol *kctl;
2504 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002505 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Takashi Iwai09f99702008-02-04 12:31:13 +01002507 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2508 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2509 idx))
2510 break;
2511 }
2512 if (idx >= SPDIF_MAX_IDX) {
2513 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2514 return -EBUSY;
2515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2517 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002518 if (!kctl)
2519 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002520 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002522 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002523 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return err;
2525 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002526 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002527 snd_hda_codec_read(codec, nid, 0,
2528 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2530 return 0;
2531}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002532EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002535 * SPDIF sharing with analog output
2536 */
2537static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2538 struct snd_ctl_elem_value *ucontrol)
2539{
2540 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2541 ucontrol->value.integer.value[0] = mout->share_spdif;
2542 return 0;
2543}
2544
2545static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
2547{
2548 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2549 mout->share_spdif = !!ucontrol->value.integer.value[0];
2550 return 0;
2551}
2552
2553static struct snd_kcontrol_new spdif_share_sw = {
2554 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2555 .name = "IEC958 Default PCM Playback Switch",
2556 .info = snd_ctl_boolean_mono_info,
2557 .get = spdif_share_sw_get,
2558 .put = spdif_share_sw_put,
2559};
2560
Takashi Iwaid5191e52009-11-16 14:58:17 +01002561/**
2562 * snd_hda_create_spdif_share_sw - create Default PCM switch
2563 * @codec: the HDA codec
2564 * @mout: multi-out instance
2565 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002566int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2567 struct hda_multi_out *mout)
2568{
2569 if (!mout->dig_out_nid)
2570 return 0;
2571 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002572 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2573 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002574}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002575EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002576
2577/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 * SPDIF input
2579 */
2580
2581#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2582
Takashi Iwai0ba21762007-04-16 11:29:14 +02002583static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2587
2588 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2589 return 0;
2590}
2591
Takashi Iwai0ba21762007-04-16 11:29:14 +02002592static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2593 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
2595 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2596 hda_nid_t nid = kcontrol->private_value;
2597 unsigned int val = !!ucontrol->value.integer.value[0];
2598 int change;
2599
Ingo Molnar62932df2006-01-16 16:34:20 +01002600 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002602 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002604 snd_hda_codec_write_cache(codec, nid, 0,
2605 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002607 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return change;
2609}
2610
Takashi Iwai0ba21762007-04-16 11:29:14 +02002611static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2612 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
2614 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2615 hda_nid_t nid = kcontrol->private_value;
2616 unsigned short val;
2617 unsigned int sbits;
2618
Andrew Paprocki3982d172007-12-19 12:13:44 +01002619 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 sbits = convert_to_spdif_status(val);
2621 ucontrol->value.iec958.status[0] = sbits;
2622 ucontrol->value.iec958.status[1] = sbits >> 8;
2623 ucontrol->value.iec958.status[2] = sbits >> 16;
2624 ucontrol->value.iec958.status[3] = sbits >> 24;
2625 return 0;
2626}
2627
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002628static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 {
2630 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002631 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 .info = snd_hda_spdif_in_switch_info,
2633 .get = snd_hda_spdif_in_switch_get,
2634 .put = snd_hda_spdif_in_switch_put,
2635 },
2636 {
2637 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2638 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002639 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 .info = snd_hda_spdif_mask_info,
2641 .get = snd_hda_spdif_in_status_get,
2642 },
2643 { } /* end */
2644};
2645
2646/**
2647 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2648 * @codec: the HDA codec
2649 * @nid: audio in widget NID
2650 *
2651 * Creates controls related with the SPDIF input.
2652 * Called from each patch supporting the SPDIF in.
2653 *
2654 * Returns 0 if successful, or a negative error code.
2655 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002656int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
2658 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002659 struct snd_kcontrol *kctl;
2660 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002661 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Takashi Iwai09f99702008-02-04 12:31:13 +01002663 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2664 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2665 idx))
2666 break;
2667 }
2668 if (idx >= SPDIF_MAX_IDX) {
2669 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2670 return -EBUSY;
2671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2673 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002674 if (!kctl)
2675 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002677 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002678 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return err;
2680 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002681 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002682 snd_hda_codec_read(codec, nid, 0,
2683 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002684 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 return 0;
2686}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002687EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Takashi Iwaicb53c622007-08-10 17:21:45 +02002689#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002690/*
2691 * command cache
2692 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002694/* build a 32bit cache key with the widget id and the command parameter */
2695#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2696#define get_cmd_cache_nid(key) ((key) & 0xff)
2697#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2698
2699/**
2700 * snd_hda_codec_write_cache - send a single command with caching
2701 * @codec: the HDA codec
2702 * @nid: NID to send the command
2703 * @direct: direct flag
2704 * @verb: the verb to send
2705 * @parm: the parameter for the verb
2706 *
2707 * Send a single command without waiting for response.
2708 *
2709 * Returns 0 if successful, or a negative error code.
2710 */
2711int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2712 int direct, unsigned int verb, unsigned int parm)
2713{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002714 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2715 struct hda_cache_head *c;
2716 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002717
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002718 if (err < 0)
2719 return err;
2720 /* parm may contain the verb stuff for get/set amp */
2721 verb = verb | (parm >> 8);
2722 parm &= 0xff;
2723 key = build_cmd_cache_key(nid, verb);
2724 mutex_lock(&codec->bus->cmd_mutex);
2725 c = get_alloc_hash(&codec->cmd_cache, key);
2726 if (c)
2727 c->val = parm;
2728 mutex_unlock(&codec->bus->cmd_mutex);
2729 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002730}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002731EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002732
Takashi Iwaid5191e52009-11-16 14:58:17 +01002733/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002734 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2735 * @codec: the HDA codec
2736 * @nid: NID to send the command
2737 * @direct: direct flag
2738 * @verb: the verb to send
2739 * @parm: the parameter for the verb
2740 *
2741 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2742 * command if the parameter is already identical with the cached value.
2743 * If not, it sends the command and refreshes the cache.
2744 *
2745 * Returns 0 if successful, or a negative error code.
2746 */
2747int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2748 int direct, unsigned int verb, unsigned int parm)
2749{
2750 struct hda_cache_head *c;
2751 u32 key;
2752
2753 /* parm may contain the verb stuff for get/set amp */
2754 verb = verb | (parm >> 8);
2755 parm &= 0xff;
2756 key = build_cmd_cache_key(nid, verb);
2757 mutex_lock(&codec->bus->cmd_mutex);
2758 c = get_hash(&codec->cmd_cache, key);
2759 if (c && c->val == parm) {
2760 mutex_unlock(&codec->bus->cmd_mutex);
2761 return 0;
2762 }
2763 mutex_unlock(&codec->bus->cmd_mutex);
2764 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2765}
2766EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2767
2768/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002769 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2770 * @codec: HD-audio codec
2771 *
2772 * Execute all verbs recorded in the command caches to resume.
2773 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002774void snd_hda_codec_resume_cache(struct hda_codec *codec)
2775{
Takashi Iwai603c4012008-07-30 15:01:44 +02002776 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002777 int i;
2778
Takashi Iwai603c4012008-07-30 15:01:44 +02002779 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002780 u32 key = buffer->key;
2781 if (!key)
2782 continue;
2783 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2784 get_cmd_cache_cmd(key), buffer->val);
2785 }
2786}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002787EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002788
2789/**
2790 * snd_hda_sequence_write_cache - sequence writes with caching
2791 * @codec: the HDA codec
2792 * @seq: VERB array to send
2793 *
2794 * Send the commands sequentially from the given array.
2795 * Thte commands are recorded on cache for power-save and resume.
2796 * The array must be terminated with NID=0.
2797 */
2798void snd_hda_sequence_write_cache(struct hda_codec *codec,
2799 const struct hda_verb *seq)
2800{
2801 for (; seq->nid; seq++)
2802 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2803 seq->param);
2804}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002805EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002806#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002807
Takashi Iwai54d17402005-11-21 16:33:22 +01002808/*
2809 * set power state of the codec
2810 */
2811static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2812 unsigned int power_state)
2813{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002814 hda_nid_t nid;
2815 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002816
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002817 /* this delay seems necessary to avoid click noise at power-down */
2818 if (power_state == AC_PWRST_D3)
2819 msleep(100);
2820 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002821 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002822 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002823 if (power_state == AC_PWRST_D0 &&
2824 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002825 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002826
Takashi Iwaicb53c622007-08-10 17:21:45 +02002827 nid = codec->start_nid;
2828 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002829 unsigned int wcaps = get_wcaps(codec, nid);
2830 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002831 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002832 if (power_state == AC_PWRST_D3 &&
2833 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002834 unsigned int pincap;
2835 /*
2836 * don't power down the widget if it controls
2837 * eapd and EAPD_BTLENABLE is set.
2838 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01002839 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002840 if (pincap & AC_PINCAP_EAPD) {
2841 int eapd = snd_hda_codec_read(codec,
2842 nid, 0,
2843 AC_VERB_GET_EAPD_BTLENABLE, 0);
2844 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002845 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002846 continue;
2847 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002848 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002849 snd_hda_codec_write(codec, nid, 0,
2850 AC_VERB_SET_POWER_STATE,
2851 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002852 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002853 }
2854
Takashi Iwaicb53c622007-08-10 17:21:45 +02002855 if (power_state == AC_PWRST_D0) {
2856 unsigned long end_time;
2857 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002858 /* wait until the codec reachs to D0 */
2859 end_time = jiffies + msecs_to_jiffies(500);
2860 do {
2861 state = snd_hda_codec_read(codec, fg, 0,
2862 AC_VERB_GET_POWER_STATE, 0);
2863 if (state == power_state)
2864 break;
2865 msleep(1);
2866 } while (time_after_eq(end_time, jiffies));
2867 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002868}
2869
Takashi Iwai11aeff02008-07-30 15:01:46 +02002870#ifdef CONFIG_SND_HDA_HWDEP
2871/* execute additional init verbs */
2872static void hda_exec_init_verbs(struct hda_codec *codec)
2873{
2874 if (codec->init_verbs.list)
2875 snd_hda_sequence_write(codec, codec->init_verbs.list);
2876}
2877#else
2878static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2879#endif
2880
Takashi Iwaicb53c622007-08-10 17:21:45 +02002881#ifdef SND_HDA_NEEDS_RESUME
2882/*
2883 * call suspend and power-down; used both from PM and power-save
2884 */
2885static void hda_call_codec_suspend(struct hda_codec *codec)
2886{
2887 if (codec->patch_ops.suspend)
2888 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2889 hda_set_power_state(codec,
2890 codec->afg ? codec->afg : codec->mfg,
2891 AC_PWRST_D3);
2892#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01002893 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002894 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002895 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002896 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01002897 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002898#endif
2899}
2900
2901/*
2902 * kick up codec; used both from PM and power-save
2903 */
2904static void hda_call_codec_resume(struct hda_codec *codec)
2905{
2906 hda_set_power_state(codec,
2907 codec->afg ? codec->afg : codec->mfg,
2908 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01002909 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwai11aeff02008-07-30 15:01:46 +02002910 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002911 if (codec->patch_ops.resume)
2912 codec->patch_ops.resume(codec);
2913 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002914 if (codec->patch_ops.init)
2915 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002916 snd_hda_codec_resume_amp(codec);
2917 snd_hda_codec_resume_cache(codec);
2918 }
2919}
2920#endif /* SND_HDA_NEEDS_RESUME */
2921
Takashi Iwai54d17402005-11-21 16:33:22 +01002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923/**
2924 * snd_hda_build_controls - build mixer controls
2925 * @bus: the BUS
2926 *
2927 * Creates mixer controls for each codec included in the bus.
2928 *
2929 * Returns 0 if successful, otherwise a negative error code.
2930 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002931int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002933 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Takashi Iwai0ba21762007-04-16 11:29:14 +02002935 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002936 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002937 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01002938 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002939 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002940 err = snd_hda_codec_reset(codec);
2941 if (err < 0) {
2942 printk(KERN_ERR
2943 "hda_codec: cannot revert codec\n");
2944 return err;
2945 }
2946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002948 return 0;
2949}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002950EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002952int snd_hda_codec_build_controls(struct hda_codec *codec)
2953{
2954 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02002955 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002956 /* continue to initialize... */
2957 if (codec->patch_ops.init)
2958 err = codec->patch_ops.init(codec);
2959 if (!err && codec->patch_ops.build_controls)
2960 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002961 if (err < 0)
2962 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return 0;
2964}
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966/*
2967 * stream formats
2968 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002969struct hda_rate_tbl {
2970 unsigned int hz;
2971 unsigned int alsa_bits;
2972 unsigned int hda_fmt;
2973};
2974
2975static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002977
2978 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2980 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2981 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2982 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2983 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2984 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2985 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2986 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2987 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2988 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2989 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002990#define AC_PAR_PCM_RATE_BITS 11
2991 /* up to bits 10, 384kHZ isn't supported properly */
2992
2993 /* not autodetected value */
2994 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002995
Takashi Iwaibefdf312005-08-22 13:57:55 +02002996 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997};
2998
2999/**
3000 * snd_hda_calc_stream_format - calculate format bitset
3001 * @rate: the sample rate
3002 * @channels: the number of channels
3003 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3004 * @maxbps: the max. bps
3005 *
3006 * Calculate the format bitset from the given rate, channels and th PCM format.
3007 *
3008 * Return zero if invalid.
3009 */
3010unsigned int snd_hda_calc_stream_format(unsigned int rate,
3011 unsigned int channels,
3012 unsigned int format,
3013 unsigned int maxbps)
3014{
3015 int i;
3016 unsigned int val = 0;
3017
Takashi Iwaibefdf312005-08-22 13:57:55 +02003018 for (i = 0; rate_bits[i].hz; i++)
3019 if (rate_bits[i].hz == rate) {
3020 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 break;
3022 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003023 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 snd_printdd("invalid rate %d\n", rate);
3025 return 0;
3026 }
3027
3028 if (channels == 0 || channels > 8) {
3029 snd_printdd("invalid channels %d\n", channels);
3030 return 0;
3031 }
3032 val |= channels - 1;
3033
3034 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003035 case 8:
3036 val |= 0x00;
3037 break;
3038 case 16:
3039 val |= 0x10;
3040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 case 20:
3042 case 24:
3043 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003044 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 val |= 0x40;
3046 else if (maxbps >= 24)
3047 val |= 0x30;
3048 else
3049 val |= 0x20;
3050 break;
3051 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003052 snd_printdd("invalid format width %d\n",
3053 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return 0;
3055 }
3056
3057 return val;
3058}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003059EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003061static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3062{
3063 unsigned int val = 0;
3064 if (nid != codec->afg &&
3065 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3066 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3067 if (!val || val == -1)
3068 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3069 if (!val || val == -1)
3070 return 0;
3071 return val;
3072}
3073
3074static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3075{
3076 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3077 get_pcm_param);
3078}
3079
3080static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3081{
3082 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3083 if (!streams || streams == -1)
3084 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3085 if (!streams || streams == -1)
3086 return 0;
3087 return streams;
3088}
3089
3090static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3091{
3092 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3093 get_stream_param);
3094}
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096/**
3097 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3098 * @codec: the HDA codec
3099 * @nid: NID to query
3100 * @ratesp: the pointer to store the detected rate bitflags
3101 * @formatsp: the pointer to store the detected formats
3102 * @bpsp: the pointer to store the detected format widths
3103 *
3104 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3105 * or @bsps argument is ignored.
3106 *
3107 * Returns 0 if successful, otherwise a negative error code.
3108 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003109static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3111{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003112 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003114 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003115 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 if (ratesp) {
3118 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003119 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003121 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003123 if (rates == 0) {
3124 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3125 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3126 nid, val,
3127 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3128 return -EIO;
3129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 *ratesp = rates;
3131 }
3132
3133 if (formatsp || bpsp) {
3134 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003135 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003137 streams = query_stream_param(codec, nid);
3138 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 bps = 0;
3142 if (streams & AC_SUPFMT_PCM) {
3143 if (val & AC_SUPPCM_BITS_8) {
3144 formats |= SNDRV_PCM_FMTBIT_U8;
3145 bps = 8;
3146 }
3147 if (val & AC_SUPPCM_BITS_16) {
3148 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3149 bps = 16;
3150 }
3151 if (wcaps & AC_WCAP_DIGITAL) {
3152 if (val & AC_SUPPCM_BITS_32)
3153 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3154 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3155 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3156 if (val & AC_SUPPCM_BITS_24)
3157 bps = 24;
3158 else if (val & AC_SUPPCM_BITS_20)
3159 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003160 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3161 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3163 if (val & AC_SUPPCM_BITS_32)
3164 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 else if (val & AC_SUPPCM_BITS_24)
3166 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003167 else if (val & AC_SUPPCM_BITS_20)
3168 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
3170 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003171 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003173 if (!bps)
3174 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003175 }
3176 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003177 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 /* temporary hack: we have still no proper support
3179 * for the direct AC3 stream...
3180 */
3181 formats |= SNDRV_PCM_FMTBIT_U8;
3182 bps = 8;
3183 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003184 if (formats == 0) {
3185 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3186 "(nid=0x%x, val=0x%x, ovrd=%i, "
3187 "streams=0x%x)\n",
3188 nid, val,
3189 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3190 streams);
3191 return -EIO;
3192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (formatsp)
3194 *formatsp = formats;
3195 if (bpsp)
3196 *bpsp = bps;
3197 }
3198
3199 return 0;
3200}
3201
3202/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003203 * snd_hda_is_supported_format - Check the validity of the format
3204 * @codec: HD-audio codec
3205 * @nid: NID to check
3206 * @format: the HD-audio format value to check
3207 *
3208 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 *
3210 * Returns 1 if supported, 0 if not.
3211 */
3212int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3213 unsigned int format)
3214{
3215 int i;
3216 unsigned int val = 0, rate, stream;
3217
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003218 val = query_pcm_param(codec, nid);
3219 if (!val)
3220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003223 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003224 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 if (val & (1 << i))
3226 break;
3227 return 0;
3228 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003229 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return 0;
3231
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003232 stream = query_stream_param(codec, nid);
3233 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return 0;
3235
3236 if (stream & AC_SUPFMT_PCM) {
3237 switch (format & 0xf0) {
3238 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003239 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return 0;
3241 break;
3242 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003243 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return 0;
3245 break;
3246 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003247 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return 0;
3249 break;
3250 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003251 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return 0;
3253 break;
3254 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003255 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return 0;
3257 break;
3258 default:
3259 return 0;
3260 }
3261 } else {
3262 /* FIXME: check for float32 and AC3? */
3263 }
3264
3265 return 1;
3266}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003267EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269/*
3270 * PCM stuff
3271 */
3272static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3273 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003274 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
3276 return 0;
3277}
3278
3279static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3280 struct hda_codec *codec,
3281 unsigned int stream_tag,
3282 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003283 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
3285 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3286 return 0;
3287}
3288
3289static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3290 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003291 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Takashi Iwai888afa12008-03-18 09:57:50 +01003293 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return 0;
3295}
3296
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003297static int set_pcm_default_values(struct hda_codec *codec,
3298 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003300 int err;
3301
Takashi Iwai0ba21762007-04-16 11:29:14 +02003302 /* query support PCM information from the given NID */
3303 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003304 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003305 info->rates ? NULL : &info->rates,
3306 info->formats ? NULL : &info->formats,
3307 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003308 if (err < 0)
3309 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
3311 if (info->ops.open == NULL)
3312 info->ops.open = hda_pcm_default_open_close;
3313 if (info->ops.close == NULL)
3314 info->ops.close = hda_pcm_default_open_close;
3315 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003316 if (snd_BUG_ON(!info->nid))
3317 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 info->ops.prepare = hda_pcm_default_prepare;
3319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003321 if (snd_BUG_ON(!info->nid))
3322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 info->ops.cleanup = hda_pcm_default_cleanup;
3324 }
3325 return 0;
3326}
3327
Takashi Iwaid5191e52009-11-16 14:58:17 +01003328/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003329const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3330 "Audio", "SPDIF", "HDMI", "Modem"
3331};
3332
Takashi Iwai176d5332008-07-30 15:01:44 +02003333/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003334 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003335 *
3336 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003337 */
3338static int get_empty_pcm_device(struct hda_bus *bus, int type)
3339{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003340 /* audio device indices; not linear to keep compatibility */
3341 static int audio_idx[HDA_PCM_NTYPES][5] = {
3342 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3343 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003344 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003345 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003346 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003347 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003348
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003349 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003350 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3351 return -EINVAL;
3352 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003353
3354 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3355 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3356 return audio_idx[type][i];
3357
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003358 snd_printk(KERN_WARNING "Too many %s devices\n",
3359 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003360 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003361}
3362
3363/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003364 * attach a new PCM stream
3365 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003366static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003367{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003368 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003369 struct hda_pcm_stream *info;
3370 int stream, err;
3371
Takashi Iwaib91f0802008-11-04 08:43:08 +01003372 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003373 return -EINVAL;
3374 for (stream = 0; stream < 2; stream++) {
3375 info = &pcm->stream[stream];
3376 if (info->substreams) {
3377 err = set_pcm_default_values(codec, info);
3378 if (err < 0)
3379 return err;
3380 }
3381 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003382 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003383}
3384
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003385/* assign all PCMs of the given codec */
3386int snd_hda_codec_build_pcms(struct hda_codec *codec)
3387{
3388 unsigned int pcm;
3389 int err;
3390
3391 if (!codec->num_pcms) {
3392 if (!codec->patch_ops.build_pcms)
3393 return 0;
3394 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003395 if (err < 0) {
3396 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003397 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003398 err = snd_hda_codec_reset(codec);
3399 if (err < 0) {
3400 printk(KERN_ERR
3401 "hda_codec: cannot revert codec\n");
3402 return err;
3403 }
3404 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003405 }
3406 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3407 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3408 int dev;
3409
3410 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003411 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003412
3413 if (!cpcm->pcm) {
3414 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3415 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003416 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003417 cpcm->device = dev;
3418 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003419 if (err < 0) {
3420 printk(KERN_ERR "hda_codec: cannot attach "
3421 "PCM stream %d for codec #%d\n",
3422 dev, codec->addr);
3423 continue; /* no fatal error */
3424 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003425 }
3426 }
3427 return 0;
3428}
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430/**
3431 * snd_hda_build_pcms - build PCM information
3432 * @bus: the BUS
3433 *
3434 * Create PCM information for each codec included in the bus.
3435 *
3436 * The build_pcms codec patch is requested to set up codec->num_pcms and
3437 * codec->pcm_info properly. The array is referred by the top-level driver
3438 * to create its PCM instances.
3439 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3440 * callback.
3441 *
3442 * At least, substreams, channels_min and channels_max must be filled for
3443 * each stream. substreams = 0 indicates that the stream doesn't exist.
3444 * When rates and/or formats are zero, the supported values are queried
3445 * from the given nid. The nid is used also by the default ops.prepare
3446 * and ops.cleanup callbacks.
3447 *
3448 * The driver needs to call ops.open in its open callback. Similarly,
3449 * ops.close is supposed to be called in the close callback.
3450 * ops.prepare should be called in the prepare or hw_params callback
3451 * with the proper parameters for set up.
3452 * ops.cleanup should be called in hw_free for clean up of streams.
3453 *
3454 * This function returns 0 if successfull, or a negative error code.
3455 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003456int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003458 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Takashi Iwai0ba21762007-04-16 11:29:14 +02003460 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003461 int err = snd_hda_codec_build_pcms(codec);
3462 if (err < 0)
3463 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
3465 return 0;
3466}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003467EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469/**
3470 * snd_hda_check_board_config - compare the current codec with the config table
3471 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003472 * @num_configs: number of config enums
3473 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 * @tbl: configuration table, terminated by null entries
3475 *
3476 * Compares the modelname or PCI subsystem id of the current codec with the
3477 * given configuration table. If a matching entry is found, returns its
3478 * config value (supposed to be 0 or positive).
3479 *
3480 * If no entries are matching, the function returns a negative value.
3481 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003482int snd_hda_check_board_config(struct hda_codec *codec,
3483 int num_configs, const char **models,
3484 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003486 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003487 int i;
3488 for (i = 0; i < num_configs; i++) {
3489 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003490 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003491 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3492 "selected\n", models[i]);
3493 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
3495 }
3496 }
3497
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003498 if (!codec->bus->pci || !tbl)
3499 return -1;
3500
3501 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3502 if (!tbl)
3503 return -1;
3504 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003505#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003506 char tmp[10];
3507 const char *model = NULL;
3508 if (models)
3509 model = models[tbl->value];
3510 if (!model) {
3511 sprintf(tmp, "#%d", tbl->value);
3512 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003514 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3515 "for config %x:%x (%s)\n",
3516 model, tbl->subvendor, tbl->subdevice,
3517 (tbl->name ? tbl->name : "Unknown device"));
3518#endif
3519 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
3521 return -1;
3522}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003523EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003526 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003527 subsystem ID with the
3528 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003529
3530 This is important for Gateway notebooks with SB450 HDA Audio
3531 where the vendor ID of the PCI device is:
3532 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3533 and the vendor/subvendor are found only at the codec.
3534
3535 * @codec: the HDA codec
3536 * @num_configs: number of config enums
3537 * @models: array of model name strings
3538 * @tbl: configuration table, terminated by null entries
3539 *
3540 * Compares the modelname or PCI subsystem id of the current codec with the
3541 * given configuration table. If a matching entry is found, returns its
3542 * config value (supposed to be 0 or positive).
3543 *
3544 * If no entries are matching, the function returns a negative value.
3545 */
3546int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3547 int num_configs, const char **models,
3548 const struct snd_pci_quirk *tbl)
3549{
3550 const struct snd_pci_quirk *q;
3551
3552 /* Search for codec ID */
3553 for (q = tbl; q->subvendor; q++) {
3554 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3555
3556 if (vendorid == codec->subsystem_id)
3557 break;
3558 }
3559
3560 if (!q->subvendor)
3561 return -1;
3562
3563 tbl = q;
3564
3565 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003566#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003567 char tmp[10];
3568 const char *model = NULL;
3569 if (models)
3570 model = models[tbl->value];
3571 if (!model) {
3572 sprintf(tmp, "#%d", tbl->value);
3573 model = tmp;
3574 }
3575 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3576 "for config %x:%x (%s)\n",
3577 model, tbl->subvendor, tbl->subdevice,
3578 (tbl->name ? tbl->name : "Unknown device"));
3579#endif
3580 return tbl->value;
3581 }
3582 return -1;
3583}
3584EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3585
3586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 * snd_hda_add_new_ctls - create controls from the array
3588 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003589 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 *
3591 * This helper function creates and add new controls in the given array.
3592 * The array must be terminated with an empty entry as terminator.
3593 *
3594 * Returns 0 if successful, or a negative error code.
3595 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003596int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003598 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
3600 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003601 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003602 if (knew->iface == -1) /* skip this codec private value */
3603 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003604 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003605 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003606 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003607 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003608 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003609 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003610 return err;
3611 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003612 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003613 return -ENOMEM;
3614 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003615 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003616 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003617 return err;
3618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
3620 return 0;
3621}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003622EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Takashi Iwaicb53c622007-08-10 17:21:45 +02003624#ifdef CONFIG_SND_HDA_POWER_SAVE
3625static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3626 unsigned int power_state);
3627
3628static void hda_power_work(struct work_struct *work)
3629{
3630 struct hda_codec *codec =
3631 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003632 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003633
Maxim Levitsky2e492462007-09-03 15:26:57 +02003634 if (!codec->power_on || codec->power_count) {
3635 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003636 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003637 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003638
3639 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003640 if (bus->ops.pm_notify)
3641 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003642}
3643
3644static void hda_keep_power_on(struct hda_codec *codec)
3645{
3646 codec->power_count++;
3647 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003648 codec->power_jiffies = jiffies;
3649}
3650
Takashi Iwaid5191e52009-11-16 14:58:17 +01003651/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003652void snd_hda_update_power_acct(struct hda_codec *codec)
3653{
3654 unsigned long delta = jiffies - codec->power_jiffies;
3655 if (codec->power_on)
3656 codec->power_on_acct += delta;
3657 else
3658 codec->power_off_acct += delta;
3659 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003660}
3661
Takashi Iwaid5191e52009-11-16 14:58:17 +01003662/**
3663 * snd_hda_power_up - Power-up the codec
3664 * @codec: HD-audio codec
3665 *
3666 * Increment the power-up counter and power up the hardware really when
3667 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003668 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003669void snd_hda_power_up(struct hda_codec *codec)
3670{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003671 struct hda_bus *bus = codec->bus;
3672
Takashi Iwaicb53c622007-08-10 17:21:45 +02003673 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003674 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003675 return;
3676
Takashi Iwaia2f63092009-11-11 09:34:25 +01003677 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003678 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003679 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003680 if (bus->ops.pm_notify)
3681 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003682 hda_call_codec_resume(codec);
3683 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003684 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003685}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003686EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003687
3688#define power_save(codec) \
3689 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003690
Takashi Iwaid5191e52009-11-16 14:58:17 +01003691/**
3692 * snd_hda_power_down - Power-down the codec
3693 * @codec: HD-audio codec
3694 *
3695 * Decrement the power-up counter and schedules the power-off work if
3696 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003697 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003698void snd_hda_power_down(struct hda_codec *codec)
3699{
3700 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003701 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003702 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003703 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003704 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003705 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003706 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003707 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003708}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003709EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003710
Takashi Iwaid5191e52009-11-16 14:58:17 +01003711/**
3712 * snd_hda_check_amp_list_power - Check the amp list and update the power
3713 * @codec: HD-audio codec
3714 * @check: the object containing an AMP list and the status
3715 * @nid: NID to check / update
3716 *
3717 * Check whether the given NID is in the amp list. If it's in the list,
3718 * check the current AMP status, and update the the power-status according
3719 * to the mute status.
3720 *
3721 * This function is supposed to be set or called from the check_power_status
3722 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003723 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003724int snd_hda_check_amp_list_power(struct hda_codec *codec,
3725 struct hda_loopback_check *check,
3726 hda_nid_t nid)
3727{
3728 struct hda_amp_list *p;
3729 int ch, v;
3730
3731 if (!check->amplist)
3732 return 0;
3733 for (p = check->amplist; p->nid; p++) {
3734 if (p->nid == nid)
3735 break;
3736 }
3737 if (!p->nid)
3738 return 0; /* nothing changed */
3739
3740 for (p = check->amplist; p->nid; p++) {
3741 for (ch = 0; ch < 2; ch++) {
3742 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3743 p->idx);
3744 if (!(v & HDA_AMP_MUTE) && v > 0) {
3745 if (!check->power_on) {
3746 check->power_on = 1;
3747 snd_hda_power_up(codec);
3748 }
3749 return 1;
3750 }
3751 }
3752 }
3753 if (check->power_on) {
3754 check->power_on = 0;
3755 snd_hda_power_down(codec);
3756 }
3757 return 0;
3758}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003759EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003762/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003763 * Channel mode helper
3764 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003765
3766/**
3767 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3768 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003769int snd_hda_ch_mode_info(struct hda_codec *codec,
3770 struct snd_ctl_elem_info *uinfo,
3771 const struct hda_channel_mode *chmode,
3772 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003773{
3774 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3775 uinfo->count = 1;
3776 uinfo->value.enumerated.items = num_chmodes;
3777 if (uinfo->value.enumerated.item >= num_chmodes)
3778 uinfo->value.enumerated.item = num_chmodes - 1;
3779 sprintf(uinfo->value.enumerated.name, "%dch",
3780 chmode[uinfo->value.enumerated.item].channels);
3781 return 0;
3782}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003783EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003784
Takashi Iwaid5191e52009-11-16 14:58:17 +01003785/**
3786 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3787 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003788int snd_hda_ch_mode_get(struct hda_codec *codec,
3789 struct snd_ctl_elem_value *ucontrol,
3790 const struct hda_channel_mode *chmode,
3791 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003792 int max_channels)
3793{
3794 int i;
3795
3796 for (i = 0; i < num_chmodes; i++) {
3797 if (max_channels == chmode[i].channels) {
3798 ucontrol->value.enumerated.item[0] = i;
3799 break;
3800 }
3801 }
3802 return 0;
3803}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003804EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003805
Takashi Iwaid5191e52009-11-16 14:58:17 +01003806/**
3807 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3808 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003809int snd_hda_ch_mode_put(struct hda_codec *codec,
3810 struct snd_ctl_elem_value *ucontrol,
3811 const struct hda_channel_mode *chmode,
3812 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003813 int *max_channelsp)
3814{
3815 unsigned int mode;
3816
3817 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01003818 if (mode >= num_chmodes)
3819 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003820 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003821 return 0;
3822 /* change the current channel setting */
3823 *max_channelsp = chmode[mode].channels;
3824 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003825 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003826 return 1;
3827}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003828EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830/*
3831 * input MUX helper
3832 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003833
3834/**
3835 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3836 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003837int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3838 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
3840 unsigned int index;
3841
3842 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3843 uinfo->count = 1;
3844 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003845 if (!imux->num_items)
3846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 index = uinfo->value.enumerated.item;
3848 if (index >= imux->num_items)
3849 index = imux->num_items - 1;
3850 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3851 return 0;
3852}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003853EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
Takashi Iwaid5191e52009-11-16 14:58:17 +01003855/**
3856 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3857 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003858int snd_hda_input_mux_put(struct hda_codec *codec,
3859 const struct hda_input_mux *imux,
3860 struct snd_ctl_elem_value *ucontrol,
3861 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 unsigned int *cur_val)
3863{
3864 unsigned int idx;
3865
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003866 if (!imux->num_items)
3867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 idx = ucontrol->value.enumerated.item[0];
3869 if (idx >= imux->num_items)
3870 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003871 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003873 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3874 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 *cur_val = idx;
3876 return 1;
3877}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003878EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
3880
3881/*
3882 * Multi-channel / digital-out PCM helper functions
3883 */
3884
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003885/* setup SPDIF output stream */
3886static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3887 unsigned int stream_tag, unsigned int format)
3888{
3889 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02003890 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003891 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02003892 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3893 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003894 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003895 if (codec->slave_dig_outs) {
3896 hda_nid_t *d;
3897 for (d = codec->slave_dig_outs; *d; d++)
3898 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3899 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003900 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003901 /* turn on again (if needed) */
3902 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3903 set_dig_out_convert(codec, nid,
3904 codec->spdif_ctls & 0xff, -1);
3905}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003906
Takashi Iwai2f728532008-09-25 16:32:41 +02003907static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3908{
3909 snd_hda_codec_cleanup_stream(codec, nid);
3910 if (codec->slave_dig_outs) {
3911 hda_nid_t *d;
3912 for (d = codec->slave_dig_outs; *d; d++)
3913 snd_hda_codec_cleanup_stream(codec, *d);
3914 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003915}
3916
Takashi Iwaid5191e52009-11-16 14:58:17 +01003917/**
3918 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
3919 * @bus: HD-audio bus
3920 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003921void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3922{
3923 struct hda_codec *codec;
3924
3925 if (!bus)
3926 return;
3927 list_for_each_entry(codec, &bus->codec_list, list) {
3928#ifdef CONFIG_SND_HDA_POWER_SAVE
3929 if (!codec->power_on)
3930 continue;
3931#endif
3932 if (codec->patch_ops.reboot_notify)
3933 codec->patch_ops.reboot_notify(codec);
3934 }
3935}
Takashi Iwai8f217a22009-11-10 18:26:12 +01003936EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003937
Takashi Iwaid5191e52009-11-16 14:58:17 +01003938/**
3939 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003941int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3942 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943{
Ingo Molnar62932df2006-01-16 16:34:20 +01003944 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003945 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3946 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003947 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003949 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 return 0;
3951}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003952EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
Takashi Iwaid5191e52009-11-16 14:58:17 +01003954/**
3955 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3956 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003957int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3958 struct hda_multi_out *mout,
3959 unsigned int stream_tag,
3960 unsigned int format,
3961 struct snd_pcm_substream *substream)
3962{
3963 mutex_lock(&codec->spdif_mutex);
3964 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3965 mutex_unlock(&codec->spdif_mutex);
3966 return 0;
3967}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003968EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003969
Takashi Iwaid5191e52009-11-16 14:58:17 +01003970/**
3971 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3972 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003973int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3974 struct hda_multi_out *mout)
3975{
3976 mutex_lock(&codec->spdif_mutex);
3977 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3978 mutex_unlock(&codec->spdif_mutex);
3979 return 0;
3980}
3981EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3982
Takashi Iwaid5191e52009-11-16 14:58:17 +01003983/**
3984 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003986int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3987 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Ingo Molnar62932df2006-01-16 16:34:20 +01003989 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003991 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 return 0;
3993}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003994EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Takashi Iwaid5191e52009-11-16 14:58:17 +01003996/**
3997 * snd_hda_multi_out_analog_open - open analog outputs
3998 *
3999 * Open analog outputs and set up the hw-constraints.
4000 * If the digital outputs can be opened as slave, open the digital
4001 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004003int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4004 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004005 struct snd_pcm_substream *substream,
4006 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
Takashi Iwai9a081602008-02-12 18:37:26 +01004008 struct snd_pcm_runtime *runtime = substream->runtime;
4009 runtime->hw.channels_max = mout->max_channels;
4010 if (mout->dig_out_nid) {
4011 if (!mout->analog_rates) {
4012 mout->analog_rates = hinfo->rates;
4013 mout->analog_formats = hinfo->formats;
4014 mout->analog_maxbps = hinfo->maxbps;
4015 } else {
4016 runtime->hw.rates = mout->analog_rates;
4017 runtime->hw.formats = mout->analog_formats;
4018 hinfo->maxbps = mout->analog_maxbps;
4019 }
4020 if (!mout->spdif_rates) {
4021 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4022 &mout->spdif_rates,
4023 &mout->spdif_formats,
4024 &mout->spdif_maxbps);
4025 }
4026 mutex_lock(&codec->spdif_mutex);
4027 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004028 if ((runtime->hw.rates & mout->spdif_rates) &&
4029 (runtime->hw.formats & mout->spdif_formats)) {
4030 runtime->hw.rates &= mout->spdif_rates;
4031 runtime->hw.formats &= mout->spdif_formats;
4032 if (mout->spdif_maxbps < hinfo->maxbps)
4033 hinfo->maxbps = mout->spdif_maxbps;
4034 } else {
4035 mout->share_spdif = 0;
4036 /* FIXME: need notify? */
4037 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004038 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004039 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4042 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4043}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004044EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Takashi Iwaid5191e52009-11-16 14:58:17 +01004046/**
4047 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4048 *
4049 * Set up the i/o for analog out.
4050 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004052int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4053 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 unsigned int stream_tag,
4055 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004056 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
4058 hda_nid_t *nids = mout->dac_nids;
4059 int chs = substream->runtime->channels;
4060 int i;
4061
Ingo Molnar62932df2006-01-16 16:34:20 +01004062 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004063 if (mout->dig_out_nid && mout->share_spdif &&
4064 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004066 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4067 format) &&
4068 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004070 setup_dig_out_stream(codec, mout->dig_out_nid,
4071 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 } else {
4073 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004074 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
4076 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004077 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004080 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4081 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004082 if (!mout->no_share_stream &&
4083 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004085 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4086 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004087 /* extra outputs copied from front */
4088 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004089 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004090 snd_hda_codec_setup_stream(codec,
4091 mout->extra_out_nid[i],
4092 stream_tag, 0, format);
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 /* surrounds */
4095 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004096 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004097 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4098 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004099 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004100 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4101 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 }
4103 return 0;
4104}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004105EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Takashi Iwaid5191e52009-11-16 14:58:17 +01004107/**
4108 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004110int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4111 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 hda_nid_t *nids = mout->dac_nids;
4114 int i;
4115
4116 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004117 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004119 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004120 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4121 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004122 snd_hda_codec_cleanup_stream(codec,
4123 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004124 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004126 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 mout->dig_out_used = 0;
4128 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004129 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 return 0;
4131}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004132EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004134/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004135 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004136 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004137
Takashi Iwai12f288b2007-08-02 15:51:59 +02004138static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004139{
4140 for (; *list; list++)
4141 if (*list == nid)
4142 return 1;
4143 return 0;
4144}
4145
Steve Longerbeam81937d32007-05-08 15:33:03 +02004146
4147/*
4148 * Sort an associated group of pins according to their sequence numbers.
4149 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004150static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004151 int num_pins)
4152{
4153 int i, j;
4154 short seq;
4155 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004156
Steve Longerbeam81937d32007-05-08 15:33:03 +02004157 for (i = 0; i < num_pins; i++) {
4158 for (j = i + 1; j < num_pins; j++) {
4159 if (sequences[i] > sequences[j]) {
4160 seq = sequences[i];
4161 sequences[i] = sequences[j];
4162 sequences[j] = seq;
4163 nid = pins[i];
4164 pins[i] = pins[j];
4165 pins[j] = nid;
4166 }
4167 }
4168 }
4169}
4170
4171
Takashi Iwai82bc9552006-03-21 11:24:42 +01004172/*
4173 * Parse all pin widgets and store the useful pin nids to cfg
4174 *
4175 * The number of line-outs or any primary output is stored in line_outs,
4176 * and the corresponding output pins are assigned to line_out_pins[],
4177 * in the order of front, rear, CLFE, side, ...
4178 *
4179 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004180 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004181 * is detected, one of speaker of HP pins is assigned as the primary
4182 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4183 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004184 *
Takashi Iwai82bc9552006-03-21 11:24:42 +01004185 * The analog input pins are assigned to input_pins array.
4186 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4187 * respectively.
4188 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004189int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4190 struct auto_pin_cfg *cfg,
4191 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004192{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004193 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004194 short seq, assoc_line_out, assoc_speaker;
4195 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4196 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004197 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004198
4199 memset(cfg, 0, sizeof(*cfg));
4200
Steve Longerbeam81937d32007-05-08 15:33:03 +02004201 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4202 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004203 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004204 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004205
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004206 end_nid = codec->start_nid + codec->num_nodes;
4207 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004208 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004209 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004210 unsigned int def_conf;
4211 short assoc, loc;
4212
4213 /* read all default configuration for pin complex */
4214 if (wid_type != AC_WID_PIN)
4215 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004216 /* ignore the given nids (e.g. pc-beep returns error) */
4217 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4218 continue;
4219
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004220 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004221 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4222 continue;
4223 loc = get_defcfg_location(def_conf);
4224 switch (get_defcfg_device(def_conf)) {
4225 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004226 seq = get_defcfg_sequence(def_conf);
4227 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004228
4229 if (!(wid_caps & AC_WCAP_STEREO))
4230 if (!cfg->mono_out_pin)
4231 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004232 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004233 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004234 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004235 assoc_line_out = assoc;
4236 else if (assoc_line_out != assoc)
4237 continue;
4238 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4239 continue;
4240 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004241 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004242 cfg->line_outs++;
4243 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004244 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004245 seq = get_defcfg_sequence(def_conf);
4246 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004247 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004248 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004249 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004250 assoc_speaker = assoc;
4251 else if (assoc_speaker != assoc)
4252 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004253 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4254 continue;
4255 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004256 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004257 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004258 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004259 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004260 seq = get_defcfg_sequence(def_conf);
4261 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004262 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4263 continue;
4264 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004265 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004266 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004267 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004268 case AC_JACK_MIC_IN: {
4269 int preferred, alt;
Kailang Yang6ff86a32010-03-19 11:14:36 +01004270 if (loc == AC_JACK_LOC_FRONT ||
4271 (loc & 0x30) == AC_JACK_LOC_INTERNAL) {
Takashi Iwai314634b2006-09-21 11:56:18 +02004272 preferred = AUTO_PIN_FRONT_MIC;
4273 alt = AUTO_PIN_MIC;
4274 } else {
4275 preferred = AUTO_PIN_MIC;
4276 alt = AUTO_PIN_FRONT_MIC;
4277 }
4278 if (!cfg->input_pins[preferred])
4279 cfg->input_pins[preferred] = nid;
4280 else if (!cfg->input_pins[alt])
4281 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004282 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004283 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004284 case AC_JACK_LINE_IN:
4285 if (loc == AC_JACK_LOC_FRONT)
4286 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4287 else
4288 cfg->input_pins[AUTO_PIN_LINE] = nid;
4289 break;
4290 case AC_JACK_CD:
4291 cfg->input_pins[AUTO_PIN_CD] = nid;
4292 break;
4293 case AC_JACK_AUX:
4294 cfg->input_pins[AUTO_PIN_AUX] = nid;
4295 break;
4296 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004297 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004298 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4299 continue;
4300 cfg->dig_out_pins[cfg->dig_outs] = nid;
4301 cfg->dig_out_type[cfg->dig_outs] =
4302 (loc == AC_JACK_LOC_HDMI) ?
4303 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4304 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004305 break;
4306 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004307 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004308 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004309 if (loc == AC_JACK_LOC_HDMI)
4310 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4311 else
4312 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004313 break;
4314 }
4315 }
4316
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004317 /* FIX-UP:
4318 * If no line-out is defined but multiple HPs are found,
4319 * some of them might be the real line-outs.
4320 */
4321 if (!cfg->line_outs && cfg->hp_outs > 1) {
4322 int i = 0;
4323 while (i < cfg->hp_outs) {
4324 /* The real HPs should have the sequence 0x0f */
4325 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4326 i++;
4327 continue;
4328 }
4329 /* Move it to the line-out table */
4330 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4331 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4332 cfg->line_outs++;
4333 cfg->hp_outs--;
4334 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4335 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4336 memmove(sequences_hp + i - 1, sequences_hp + i,
4337 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4338 }
4339 }
4340
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004341 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004342 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4343 cfg->line_outs);
4344 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4345 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004346 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4347 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004348
Takashi Iwaif889fa92007-10-31 15:49:32 +01004349 /* if we have only one mic, make it AUTO_PIN_MIC */
4350 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4351 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4352 cfg->input_pins[AUTO_PIN_MIC] =
4353 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4354 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4355 }
4356 /* ditto for line-in */
4357 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4358 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4359 cfg->input_pins[AUTO_PIN_LINE] =
4360 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4361 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4362 }
4363
Steve Longerbeam81937d32007-05-08 15:33:03 +02004364 /*
4365 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4366 * as a primary output
4367 */
4368 if (!cfg->line_outs) {
4369 if (cfg->speaker_outs) {
4370 cfg->line_outs = cfg->speaker_outs;
4371 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4372 sizeof(cfg->speaker_pins));
4373 cfg->speaker_outs = 0;
4374 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4375 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4376 } else if (cfg->hp_outs) {
4377 cfg->line_outs = cfg->hp_outs;
4378 memcpy(cfg->line_out_pins, cfg->hp_pins,
4379 sizeof(cfg->hp_pins));
4380 cfg->hp_outs = 0;
4381 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4382 cfg->line_out_type = AUTO_PIN_HP_OUT;
4383 }
4384 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004385
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004386 /* Reorder the surround channels
4387 * ALSA sequence is front/surr/clfe/side
4388 * HDA sequence is:
4389 * 4-ch: front/surr => OK as it is
4390 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004391 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004392 */
4393 switch (cfg->line_outs) {
4394 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004395 case 4:
4396 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004397 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004398 cfg->line_out_pins[2] = nid;
4399 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004400 }
4401
Takashi Iwai82bc9552006-03-21 11:24:42 +01004402 /*
4403 * debug prints of the parsed results
4404 */
4405 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4406 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4407 cfg->line_out_pins[2], cfg->line_out_pins[3],
4408 cfg->line_out_pins[4]);
4409 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4410 cfg->speaker_outs, cfg->speaker_pins[0],
4411 cfg->speaker_pins[1], cfg->speaker_pins[2],
4412 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004413 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4414 cfg->hp_outs, cfg->hp_pins[0],
4415 cfg->hp_pins[1], cfg->hp_pins[2],
4416 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004417 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004418 if (cfg->dig_outs)
4419 snd_printd(" dig-out=0x%x/0x%x\n",
4420 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004421 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4422 " cd=0x%x, aux=0x%x\n",
4423 cfg->input_pins[AUTO_PIN_MIC],
4424 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4425 cfg->input_pins[AUTO_PIN_LINE],
4426 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4427 cfg->input_pins[AUTO_PIN_CD],
4428 cfg->input_pins[AUTO_PIN_AUX]);
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004429 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004430 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004431
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004432 return 0;
4433}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004434EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004435
Takashi Iwai4a471b72005-12-07 13:56:29 +01004436/* labels for input pins */
4437const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4438 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4439};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004440EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01004441
4442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443#ifdef CONFIG_PM
4444/*
4445 * power management
4446 */
4447
4448/**
4449 * snd_hda_suspend - suspend the codecs
4450 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 *
4452 * Returns 0 if successful.
4453 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004454int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004456 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Takashi Iwai0ba21762007-04-16 11:29:14 +02004458 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004459#ifdef CONFIG_SND_HDA_POWER_SAVE
4460 if (!codec->power_on)
4461 continue;
4462#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004463 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 }
4465 return 0;
4466}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004467EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
4469/**
4470 * snd_hda_resume - resume the codecs
4471 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 *
4473 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004474 *
4475 * This fucntion is defined only when POWER_SAVE isn't set.
4476 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 */
4478int snd_hda_resume(struct hda_bus *bus)
4479{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004480 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Takashi Iwai0ba21762007-04-16 11:29:14 +02004482 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004483 if (snd_hda_codec_needs_resume(codec))
4484 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 return 0;
4487}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004488EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004489#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004490
4491/*
4492 * generic arrays
4493 */
4494
Takashi Iwaid5191e52009-11-16 14:58:17 +01004495/**
4496 * snd_array_new - get a new element from the given array
4497 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004498 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004499 * Get a new element from the given array. If it exceeds the
4500 * pre-allocated array size, re-allocate the array.
4501 *
4502 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004503 */
4504void *snd_array_new(struct snd_array *array)
4505{
4506 if (array->used >= array->alloced) {
4507 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004508 void *nlist;
4509 if (snd_BUG_ON(num >= 4096))
4510 return NULL;
4511 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004512 if (!nlist)
4513 return NULL;
4514 if (array->list) {
4515 memcpy(nlist, array->list,
4516 array->elem_size * array->alloced);
4517 kfree(array->list);
4518 }
4519 array->list = nlist;
4520 array->alloced = num;
4521 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004522 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004523}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004524EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004525
Takashi Iwaid5191e52009-11-16 14:58:17 +01004526/**
4527 * snd_array_free - free the given array elements
4528 * @array: the array object
4529 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004530void snd_array_free(struct snd_array *array)
4531{
4532 kfree(array->list);
4533 array->used = 0;
4534 array->alloced = 0;
4535 array->list = NULL;
4536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004537EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004538
Takashi Iwaid5191e52009-11-16 14:58:17 +01004539/**
4540 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4541 * @pcm: PCM caps bits
4542 * @buf: the string buffer to write
4543 * @buflen: the max buffer length
4544 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004545 * used by hda_proc.c and hda_eld.c
4546 */
4547void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4548{
4549 static unsigned int rates[] = {
4550 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4551 96000, 176400, 192000, 384000
4552 };
4553 int i, j;
4554
4555 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4556 if (pcm & (1 << i))
4557 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4558
4559 buf[j] = '\0'; /* necessary when j == 0 */
4560}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004561EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004562
Takashi Iwaid5191e52009-11-16 14:58:17 +01004563/**
4564 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4565 * @pcm: PCM caps bits
4566 * @buf: the string buffer to write
4567 * @buflen: the max buffer length
4568 *
4569 * used by hda_proc.c and hda_eld.c
4570 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004571void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4572{
4573 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4574 int i, j;
4575
4576 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4577 if (pcm & (AC_SUPPCM_BITS_8 << i))
4578 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4579
4580 buf[j] = '\0'; /* necessary when j == 0 */
4581}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004582EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004583
4584MODULE_DESCRIPTION("HDA codec core");
4585MODULE_LICENSE("GPL");