blob: 3252945f3743a6e7a8f7d633440ff6c7a75c0252 [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,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200733 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200734 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200735 case AC_GRP_AUDIO_FUNCTION:
736 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200737 codec->afg_function_id = function_id & 0xff;
738 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200739 break;
740 case AC_GRP_MODEM_FUNCTION:
741 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200742 codec->mfg_function_id = function_id & 0xff;
743 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200744 break;
745 default:
746 break;
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100752 * read widget caps for each widget and store in cache
753 */
754static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
755{
756 int i;
757 hda_nid_t nid;
758
759 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
760 &codec->start_nid);
761 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200762 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100763 return -ENOMEM;
764 nid = codec->start_nid;
765 for (i = 0; i < codec->num_nodes; i++, nid++)
766 codec->wcaps[i] = snd_hda_param_read(codec, nid,
767 AC_PAR_AUDIO_WIDGET_CAP);
768 return 0;
769}
770
Takashi Iwai3be14142009-02-20 14:11:16 +0100771/* read all pin default configurations and save codec->init_pins */
772static int read_pin_defaults(struct hda_codec *codec)
773{
774 int i;
775 hda_nid_t nid = codec->start_nid;
776
777 for (i = 0; i < codec->num_nodes; i++, nid++) {
778 struct hda_pincfg *pin;
779 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200780 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100781 if (wid_type != AC_WID_PIN)
782 continue;
783 pin = snd_array_new(&codec->init_pins);
784 if (!pin)
785 return -ENOMEM;
786 pin->nid = nid;
787 pin->cfg = snd_hda_codec_read(codec, nid, 0,
788 AC_VERB_GET_CONFIG_DEFAULT, 0);
789 }
790 return 0;
791}
792
793/* look up the given pin config list and return the item matching with NID */
794static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
795 struct snd_array *array,
796 hda_nid_t nid)
797{
798 int i;
799 for (i = 0; i < array->used; i++) {
800 struct hda_pincfg *pin = snd_array_elem(array, i);
801 if (pin->nid == nid)
802 return pin;
803 }
804 return NULL;
805}
806
807/* write a config value for the given NID */
808static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
809 unsigned int cfg)
810{
811 int i;
812 for (i = 0; i < 4; i++) {
813 snd_hda_codec_write(codec, nid, 0,
814 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
815 cfg & 0xff);
816 cfg >>= 8;
817 }
818}
819
820/* set the current pin config value for the given NID.
821 * the value is cached, and read via snd_hda_codec_get_pincfg()
822 */
823int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
824 hda_nid_t nid, unsigned int cfg)
825{
826 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100827 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100828
Takashi Iwaib82855a2009-12-27 11:24:56 +0100829 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
830 return -EINVAL;
831
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100832 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100833 pin = look_up_pincfg(codec, list, nid);
834 if (!pin) {
835 pin = snd_array_new(list);
836 if (!pin)
837 return -ENOMEM;
838 pin->nid = nid;
839 }
840 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100841
842 /* change only when needed; e.g. if the pincfg is already present
843 * in user_pins[], don't write it
844 */
845 cfg = snd_hda_codec_get_pincfg(codec, nid);
846 if (oldcfg != cfg)
847 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100848 return 0;
849}
850
Takashi Iwaid5191e52009-11-16 14:58:17 +0100851/**
852 * snd_hda_codec_set_pincfg - Override a pin default configuration
853 * @codec: the HDA codec
854 * @nid: NID to set the pin config
855 * @cfg: the pin default config value
856 *
857 * Override a pin default configuration value in the cache.
858 * This value can be read by snd_hda_codec_get_pincfg() in a higher
859 * priority than the real hardware value.
860 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100861int snd_hda_codec_set_pincfg(struct hda_codec *codec,
862 hda_nid_t nid, unsigned int cfg)
863{
Takashi Iwai346ff702009-02-23 09:42:57 +0100864 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100865}
866EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
867
Takashi Iwaid5191e52009-11-16 14:58:17 +0100868/**
869 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
870 * @codec: the HDA codec
871 * @nid: NID to get the pin config
872 *
873 * Get the current pin config value of the given pin NID.
874 * If the pincfg value is cached or overridden via sysfs or driver,
875 * returns the cached value.
876 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100877unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
878{
879 struct hda_pincfg *pin;
880
Takashi Iwai3be14142009-02-20 14:11:16 +0100881#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100882 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100883 if (pin)
884 return pin->cfg;
885#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100886 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
887 if (pin)
888 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100889 pin = look_up_pincfg(codec, &codec->init_pins, nid);
890 if (pin)
891 return pin->cfg;
892 return 0;
893}
894EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
895
896/* restore all current pin configs */
897static void restore_pincfgs(struct hda_codec *codec)
898{
899 int i;
900 for (i = 0; i < codec->init_pins.used; i++) {
901 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
902 set_pincfg(codec, pin->nid,
903 snd_hda_codec_get_pincfg(codec, pin->nid));
904 }
905}
Takashi Iwai54d17402005-11-21 16:33:22 +0100906
Takashi Iwai92ee6162009-12-27 11:18:59 +0100907/**
908 * snd_hda_shutup_pins - Shut up all pins
909 * @codec: the HDA codec
910 *
911 * Clear all pin controls to shup up before suspend for avoiding click noise.
912 * The controls aren't cached so that they can be resumed properly.
913 */
914void snd_hda_shutup_pins(struct hda_codec *codec)
915{
916 int i;
917 for (i = 0; i < codec->init_pins.used; i++) {
918 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
919 /* use read here for syncing after issuing each verb */
920 snd_hda_codec_read(codec, pin->nid, 0,
921 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
922 }
923}
924EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
925
Takashi Iwai01751f52007-08-10 16:59:39 +0200926static void init_hda_cache(struct hda_cache_rec *cache,
927 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200928static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200929
Takashi Iwai3be14142009-02-20 14:11:16 +0100930/* restore the initial pin cfgs and release all pincfg lists */
931static void restore_init_pincfgs(struct hda_codec *codec)
932{
Takashi Iwai346ff702009-02-23 09:42:57 +0100933 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100934 * so that only the values in init_pins are restored
935 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100936 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100937#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100938 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100939#endif
940 restore_pincfgs(codec);
941 snd_array_free(&codec->init_pins);
942}
943
Takashi Iwai54d17402005-11-21 16:33:22 +0100944/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * codec destructor
946 */
947static void snd_hda_codec_free(struct hda_codec *codec)
948{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200949 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return;
Takashi Iwai3be14142009-02-20 14:11:16 +0100951 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200952#ifdef CONFIG_SND_HDA_POWER_SAVE
953 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100954 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200955#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200957 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100958 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 codec->bus->caddr_tbl[codec->addr] = NULL;
960 if (codec->patch_ops.free)
961 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100962 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200963 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200964 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200965 kfree(codec->vendor_name);
966 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200967 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100968 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 kfree(codec);
970}
971
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100972static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
973 unsigned int power_state);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/**
976 * snd_hda_codec_new - create a HDA codec
977 * @bus: the bus to assign
978 * @codec_addr: the codec address
979 * @codecp: the pointer to store the generated codec
980 *
981 * Returns 0 if successful, or a negative error code.
982 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +0100983int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
984 unsigned int codec_addr,
985 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200988 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int err;
990
Takashi Iwaida3cec32008-08-08 17:12:14 +0200991 if (snd_BUG_ON(!bus))
992 return -EINVAL;
993 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
994 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200997 snd_printk(KERN_ERR "hda_codec: "
998 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return -EBUSY;
1000 }
1001
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001002 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (codec == NULL) {
1004 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1005 return -ENOMEM;
1006 }
1007
1008 codec->bus = bus;
1009 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001010 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001011 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001012 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001013 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001014 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1015 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001016 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001017 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001018 if (codec->bus->modelname) {
1019 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1020 if (!codec->modelname) {
1021 snd_hda_codec_free(codec);
1022 return -ENODEV;
1023 }
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Takashi Iwaicb53c622007-08-10 17:21:45 +02001026#ifdef CONFIG_SND_HDA_POWER_SAVE
1027 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1028 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1029 * the caller has to power down appropriatley after initialization
1030 * phase.
1031 */
1032 hda_keep_power_on(codec);
1033#endif
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 list_add_tail(&codec->list, &bus->codec_list);
1036 bus->caddr_tbl[codec_addr] = codec;
1037
Takashi Iwai0ba21762007-04-16 11:29:14 +02001038 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1039 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001040 if (codec->vendor_id == -1)
1041 /* read again, hopefully the access method was corrected
1042 * in the last read...
1043 */
1044 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1045 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001046 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1047 AC_PAR_SUBSYSTEM_ID);
1048 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1049 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001051 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001052 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001053 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001054 err = -ENODEV;
1055 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
Takashi Iwai3be14142009-02-20 14:11:16 +01001058 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1059 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001060 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001061 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001062 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001063 err = read_pin_defaults(codec);
1064 if (err < 0)
1065 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001066
Takashi Iwai0ba21762007-04-16 11:29:14 +02001067 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001068 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001069 codec->subsystem_id =
1070 snd_hda_codec_read(codec, nid, 0,
1071 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001072 }
1073
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001074 /* power-up all before initialization */
1075 hda_set_power_state(codec,
1076 codec->afg ? codec->afg : codec->mfg,
1077 AC_PWRST_D0);
1078
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001079 snd_hda_codec_proc_new(codec);
1080
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001081 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001082
1083 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1084 codec->subsystem_id, codec->revision_id);
1085 snd_component_add(codec->bus->card, component);
1086
1087 if (codecp)
1088 *codecp = codec;
1089 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001090
1091 error:
1092 snd_hda_codec_free(codec);
1093 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001094}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001095EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001096
Takashi Iwaid5191e52009-11-16 14:58:17 +01001097/**
1098 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1099 * @codec: the HDA codec
1100 *
1101 * Start parsing of the given codec tree and (re-)initialize the whole
1102 * patch instance.
1103 *
1104 * Returns 0 if successful or a negative error code.
1105 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001106int snd_hda_codec_configure(struct hda_codec *codec)
1107{
1108 int err;
1109
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001110 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001111 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001112 err = get_codec_name(codec);
1113 if (err < 0)
1114 return err;
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Takashi Iwai82467612007-07-27 19:15:54 +02001117 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001119 goto patched;
1120 }
Takashi Iwai82467612007-07-27 19:15:54 +02001121 if (codec->preset && codec->preset->patch) {
1122 err = codec->preset->patch(codec);
1123 goto patched;
1124 }
1125
1126 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001127 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001128 if (err < 0)
1129 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001130
1131 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001132 if (!err && codec->patch_ops.unsol_event)
1133 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001134 /* audio codec should override the mixer name */
1135 if (!err && (codec->afg || !*codec->bus->card->mixername))
1136 snprintf(codec->bus->card->mixername,
1137 sizeof(codec->bus->card->mixername),
1138 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001139 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001141EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143/**
1144 * snd_hda_codec_setup_stream - set up the codec for streaming
1145 * @codec: the CODEC to set up
1146 * @nid: the NID to set up
1147 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1148 * @channel_id: channel id to pass, zero based.
1149 * @format: stream format.
1150 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001151void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1152 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 int channel_id, int format)
1154{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001155 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001156 return;
1157
Takashi Iwai0ba21762007-04-16 11:29:14 +02001158 snd_printdd("hda_codec_setup_stream: "
1159 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 nid, stream_tag, channel_id, format);
1161 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1162 (stream_tag << 4) | channel_id);
1163 msleep(1);
1164 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001166EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Takashi Iwaid5191e52009-11-16 14:58:17 +01001168/**
1169 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1170 * @codec: the CODEC to clean up
1171 * @nid: the NID to clean up
1172 */
Takashi Iwai888afa12008-03-18 09:57:50 +01001173void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1174{
1175 if (!nid)
1176 return;
1177
1178 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1179 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1180#if 0 /* keep the format */
1181 msleep(1);
1182 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1183#endif
1184}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001185EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/*
1188 * amp access functions
1189 */
1190
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001191/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001192#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001193#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001194#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1195#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001197#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001200static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001201 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Takashi Iwai01751f52007-08-10 16:59:39 +02001203 memset(cache, 0, sizeof(*cache));
1204 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001205 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001206}
1207
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001208static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001209{
Takashi Iwai603c4012008-07-30 15:01:44 +02001210 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001214static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Takashi Iwai01751f52007-08-10 16:59:39 +02001216 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1217 u16 cur = cache->hash[idx];
1218 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001221 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (info->key == key)
1223 return info;
1224 cur = info->next;
1225 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001226 return NULL;
1227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001229/* query the hash. allocate an entry if not found. */
1230static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1231 u32 key)
1232{
1233 struct hda_cache_head *info = get_hash(cache, key);
1234 if (!info) {
1235 u16 idx, cur;
1236 /* add a new hash entry */
1237 info = snd_array_new(&cache->buf);
1238 if (!info)
1239 return NULL;
1240 cur = snd_array_index(&cache->buf, info);
1241 info->key = key;
1242 info->val = 0;
1243 idx = key % (u16)ARRAY_SIZE(cache->hash);
1244 info->next = cache->hash[idx];
1245 cache->hash[idx] = cur;
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return info;
1248}
1249
Takashi Iwai01751f52007-08-10 16:59:39 +02001250/* query and allocate an amp hash entry */
1251static inline struct hda_amp_info *
1252get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1253{
1254 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1255}
1256
Takashi Iwaid5191e52009-11-16 14:58:17 +01001257/**
1258 * query_amp_caps - query AMP capabilities
1259 * @codec: the HD-auio codec
1260 * @nid: the NID to query
1261 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1262 *
1263 * Query AMP capabilities for the given widget and direction.
1264 * Returns the obtained capability bits.
1265 *
1266 * When cap bits have been already read, this doesn't read again but
1267 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001269u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001271 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Takashi Iwai0ba21762007-04-16 11:29:14 +02001273 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1274 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001276 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001277 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001279 info->amp_caps = snd_hda_param_read(codec, nid,
1280 direction == HDA_OUTPUT ?
1281 AC_PAR_AMP_OUT_CAP :
1282 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001283 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001284 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 return info->amp_caps;
1287}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001288EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Takashi Iwaid5191e52009-11-16 14:58:17 +01001290/**
1291 * snd_hda_override_amp_caps - Override the AMP capabilities
1292 * @codec: the CODEC to clean up
1293 * @nid: the NID to clean up
1294 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1295 * @caps: the capability bits to set
1296 *
1297 * Override the cached AMP caps bits value by the given one.
1298 * This function is useful if the driver needs to adjust the AMP ranges,
1299 * e.g. limit to 0dB, etc.
1300 *
1301 * Returns zero if successful or a negative error code.
1302 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001303int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1304 unsigned int caps)
1305{
1306 struct hda_amp_info *info;
1307
1308 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1309 if (!info)
1310 return -EINVAL;
1311 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001312 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001313 return 0;
1314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001315EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001316
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001317static unsigned int
1318query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1319 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001320{
1321 struct hda_amp_info *info;
1322
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001323 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001324 if (!info)
1325 return 0;
1326 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001327 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001328 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001329 }
1330 return info->amp_caps;
1331}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001332
1333static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1334{
1335 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1336}
1337
Takashi Iwaid5191e52009-11-16 14:58:17 +01001338/**
1339 * snd_hda_query_pin_caps - Query PIN capabilities
1340 * @codec: the HD-auio codec
1341 * @nid: the NID to query
1342 *
1343 * Query PIN capabilities for the given widget.
1344 * Returns the obtained capability bits.
1345 *
1346 * When cap bits have been already read, this doesn't read again but
1347 * returns the cached value.
1348 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001349u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1350{
1351 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1352 read_pin_cap);
1353}
Takashi Iwai1327a322009-03-23 13:07:47 +01001354EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1355
Wu Fengguang864f92b2009-11-18 12:38:02 +08001356/**
1357 * snd_hda_pin_sense - execute pin sense measurement
1358 * @codec: the CODEC to sense
1359 * @nid: the pin NID to sense
1360 *
1361 * Execute necessary pin sense measurement and return its Presence Detect,
1362 * Impedance, ELD Valid etc. status bits.
1363 */
1364u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1365{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001366 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001367
Takashi Iwai729d55b2009-12-25 22:49:01 +01001368 if (!codec->no_trigger_sense) {
1369 pincap = snd_hda_query_pin_caps(codec, nid);
1370 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001371 snd_hda_codec_read(codec, nid, 0,
1372 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001373 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001374 return snd_hda_codec_read(codec, nid, 0,
1375 AC_VERB_GET_PIN_SENSE, 0);
1376}
1377EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1378
1379/**
1380 * snd_hda_jack_detect - query pin Presence Detect status
1381 * @codec: the CODEC to sense
1382 * @nid: the pin NID to sense
1383 *
1384 * Query and return the pin's Presence Detect status.
1385 */
1386int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1387{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001388 u32 sense = snd_hda_pin_sense(codec, nid);
1389 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001390}
1391EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393/*
1394 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001395 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001397static unsigned int get_vol_mute(struct hda_codec *codec,
1398 struct hda_amp_info *info, hda_nid_t nid,
1399 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 u32 val, parm;
1402
Takashi Iwai01751f52007-08-10 16:59:39 +02001403 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001404 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1407 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1408 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001409 val = snd_hda_codec_read(codec, nid, 0,
1410 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001412 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001413 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001417 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001419static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001420 hda_nid_t nid, int ch, int direction, int index,
1421 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 u32 parm;
1424
1425 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1426 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1427 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1428 parm |= val;
1429 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001430 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Takashi Iwaid5191e52009-11-16 14:58:17 +01001433/**
1434 * snd_hda_codec_amp_read - Read AMP value
1435 * @codec: HD-audio codec
1436 * @nid: NID to read the AMP value
1437 * @ch: channel (left=0 or right=1)
1438 * @direction: #HDA_INPUT or #HDA_OUTPUT
1439 * @index: the index value (only for input direction)
1440 *
1441 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
Takashi Iwai834be882006-03-01 14:16:17 +01001443int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1444 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001446 struct hda_amp_info *info;
1447 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1448 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001450 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001452EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Takashi Iwaid5191e52009-11-16 14:58:17 +01001454/**
1455 * snd_hda_codec_amp_update - update the AMP value
1456 * @codec: HD-audio codec
1457 * @nid: NID to read the AMP value
1458 * @ch: channel (left=0 or right=1)
1459 * @direction: #HDA_INPUT or #HDA_OUTPUT
1460 * @idx: the index value (only for input direction)
1461 * @mask: bit mask to set
1462 * @val: the bits value to set
1463 *
1464 * Update the AMP value with a bit mask.
1465 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001466 */
Takashi Iwai834be882006-03-01 14:16:17 +01001467int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1468 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001470 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001471
Takashi Iwai0ba21762007-04-16 11:29:14 +02001472 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1473 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001475 if (snd_BUG_ON(mask & ~0xff))
1476 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001477 val &= mask;
1478 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001479 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001481 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return 1;
1483}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001484EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Takashi Iwaid5191e52009-11-16 14:58:17 +01001486/**
1487 * snd_hda_codec_amp_stereo - update the AMP stereo values
1488 * @codec: HD-audio codec
1489 * @nid: NID to read the AMP value
1490 * @direction: #HDA_INPUT or #HDA_OUTPUT
1491 * @idx: the index value (only for input direction)
1492 * @mask: bit mask to set
1493 * @val: the bits value to set
1494 *
1495 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1496 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001497 */
1498int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1499 int direction, int idx, int mask, int val)
1500{
1501 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001502
1503 if (snd_BUG_ON(mask & ~0xff))
1504 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001505 for (ch = 0; ch < 2; ch++)
1506 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1507 idx, mask, val);
1508 return ret;
1509}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001510EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001511
Takashi Iwaicb53c622007-08-10 17:21:45 +02001512#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001513/**
1514 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1515 * @codec: HD-audio codec
1516 *
1517 * Resume the all amp commands from the cache.
1518 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001519void snd_hda_codec_resume_amp(struct hda_codec *codec)
1520{
Takashi Iwai603c4012008-07-30 15:01:44 +02001521 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001522 int i;
1523
Takashi Iwai603c4012008-07-30 15:01:44 +02001524 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001525 u32 key = buffer->head.key;
1526 hda_nid_t nid;
1527 unsigned int idx, dir, ch;
1528 if (!key)
1529 continue;
1530 nid = key & 0xff;
1531 idx = (key >> 16) & 0xff;
1532 dir = (key >> 24) & 0xff;
1533 for (ch = 0; ch < 2; ch++) {
1534 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1535 continue;
1536 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1537 buffer->vol[ch]);
1538 }
1539 }
1540}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001541EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001542#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Takashi Iwaid5191e52009-11-16 14:58:17 +01001544/**
1545 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1546 *
1547 * The control element is supposed to have the private_value field
1548 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1549 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001550int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1551 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1554 u16 nid = get_amp_nid(kcontrol);
1555 u8 chs = get_amp_channels(kcontrol);
1556 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001557 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 u32 caps;
1559
1560 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001561 /* num steps */
1562 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1563 if (!caps) {
1564 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001565 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1566 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return -EINVAL;
1568 }
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001569 if (ofs < caps)
1570 caps -= ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1572 uinfo->count = chs == 3 ? 2 : 1;
1573 uinfo->value.integer.min = 0;
1574 uinfo->value.integer.max = caps;
1575 return 0;
1576}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001577EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001579
1580static inline unsigned int
1581read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1582 int ch, int dir, int idx, unsigned int ofs)
1583{
1584 unsigned int val;
1585 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1586 val &= HDA_AMP_VOLMASK;
1587 if (val >= ofs)
1588 val -= ofs;
1589 else
1590 val = 0;
1591 return val;
1592}
1593
1594static inline int
1595update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1596 int ch, int dir, int idx, unsigned int ofs,
1597 unsigned int val)
1598{
1599 if (val > 0)
1600 val += ofs;
1601 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1602 HDA_AMP_VOLMASK, val);
1603}
1604
Takashi Iwaid5191e52009-11-16 14:58:17 +01001605/**
1606 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1607 *
1608 * The control element is supposed to have the private_value field
1609 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1610 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001611int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1612 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1615 hda_nid_t nid = get_amp_nid(kcontrol);
1616 int chs = get_amp_channels(kcontrol);
1617 int dir = get_amp_direction(kcontrol);
1618 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001619 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 long *valp = ucontrol->value.integer.value;
1621
1622 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001623 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001625 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
1627}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001628EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Takashi Iwaid5191e52009-11-16 14:58:17 +01001630/**
1631 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1632 *
1633 * The control element is supposed to have the private_value field
1634 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1635 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001636int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1637 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1640 hda_nid_t nid = get_amp_nid(kcontrol);
1641 int chs = get_amp_channels(kcontrol);
1642 int dir = get_amp_direction(kcontrol);
1643 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001644 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 long *valp = ucontrol->value.integer.value;
1646 int change = 0;
1647
Takashi Iwaicb53c622007-08-10 17:21:45 +02001648 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001649 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001650 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001651 valp++;
1652 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001653 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001654 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001655 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return change;
1657}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001658EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Takashi Iwaid5191e52009-11-16 14:58:17 +01001660/**
1661 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1662 *
1663 * The control element is supposed to have the private_value field
1664 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1665 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001666int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1667 unsigned int size, unsigned int __user *_tlv)
1668{
1669 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1670 hda_nid_t nid = get_amp_nid(kcontrol);
1671 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001672 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001673 u32 caps, val1, val2;
1674
1675 if (size < 4 * sizeof(unsigned int))
1676 return -ENOMEM;
1677 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001678 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1679 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001680 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001681 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001682 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001683 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1684 return -EFAULT;
1685 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1686 return -EFAULT;
1687 if (put_user(val1, _tlv + 2))
1688 return -EFAULT;
1689 if (put_user(val2, _tlv + 3))
1690 return -EFAULT;
1691 return 0;
1692}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001693EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001694
Takashi Iwaid5191e52009-11-16 14:58:17 +01001695/**
1696 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1697 * @codec: HD-audio codec
1698 * @nid: NID of a reference widget
1699 * @dir: #HDA_INPUT or #HDA_OUTPUT
1700 * @tlv: TLV data to be stored, at least 4 elements
1701 *
1702 * Set (static) TLV data for a virtual master volume using the AMP caps
1703 * obtained from the reference NID.
1704 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001705 */
1706void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1707 unsigned int *tlv)
1708{
1709 u32 caps;
1710 int nums, step;
1711
1712 caps = query_amp_caps(codec, nid, dir);
1713 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1714 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1715 step = (step + 1) * 25;
1716 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1717 tlv[1] = 2 * sizeof(unsigned int);
1718 tlv[2] = -nums * step;
1719 tlv[3] = step;
1720}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001721EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001722
1723/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001724static struct snd_kcontrol *
1725_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1726 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001727{
1728 struct snd_ctl_elem_id id;
1729 memset(&id, 0, sizeof(id));
1730 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001731 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001732 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1733 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001734 strcpy(id.name, name);
1735 return snd_ctl_find_id(codec->bus->card, &id);
1736}
1737
Takashi Iwaid5191e52009-11-16 14:58:17 +01001738/**
1739 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1740 * @codec: HD-audio codec
1741 * @name: ctl id name string
1742 *
1743 * Get the control element with the given id string and IFACE_MIXER.
1744 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001745struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1746 const char *name)
1747{
1748 return _snd_hda_find_mixer_ctl(codec, name, 0);
1749}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001750EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001751
Takashi Iwaid5191e52009-11-16 14:58:17 +01001752/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001753 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001754 * @codec: HD-audio codec
1755 * @nid: corresponding NID (optional)
1756 * @kctl: the control element to assign
1757 *
1758 * Add the given control element to an array inside the codec instance.
1759 * All control elements belonging to a codec are supposed to be added
1760 * by this function so that a proper clean-up works at the free or
1761 * reconfiguration time.
1762 *
1763 * If non-zero @nid is passed, the NID is assigned to the control element.
1764 * The assignment is shown in the codec proc file.
1765 *
1766 * snd_hda_ctl_add() checks the control subdev id field whether
1767 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001768 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1769 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001770 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001771int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1772 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001773{
1774 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001775 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001776 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001777
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001778 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001779 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001780 if (nid == 0)
1781 nid = get_amp_nid_(kctl->private_value);
1782 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001783 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1784 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001785 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001786 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001787 err = snd_ctl_add(codec->bus->card, kctl);
1788 if (err < 0)
1789 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001790 item = snd_array_new(&codec->mixers);
1791 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001792 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001793 item->kctl = kctl;
1794 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001795 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001796 return 0;
1797}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001798EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001799
Takashi Iwaid5191e52009-11-16 14:58:17 +01001800/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001801 * snd_hda_add_nid - Assign a NID to a control element
1802 * @codec: HD-audio codec
1803 * @nid: corresponding NID (optional)
1804 * @kctl: the control element to assign
1805 * @index: index to kctl
1806 *
1807 * Add the given control element to an array inside the codec instance.
1808 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1809 * NID:KCTL mapping - for example "Capture Source" selector.
1810 */
1811int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1812 unsigned int index, hda_nid_t nid)
1813{
1814 struct hda_nid_item *item;
1815
1816 if (nid > 0) {
1817 item = snd_array_new(&codec->nids);
1818 if (!item)
1819 return -ENOMEM;
1820 item->kctl = kctl;
1821 item->index = index;
1822 item->nid = nid;
1823 return 0;
1824 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01001825 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1826 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001827 return -EINVAL;
1828}
1829EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1830
1831/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001832 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1833 * @codec: HD-audio codec
1834 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001835void snd_hda_ctls_clear(struct hda_codec *codec)
1836{
1837 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001838 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001839 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001840 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001841 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001842 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001843}
1844
Takashi Iwaia65d6292009-02-23 16:57:04 +01001845/* pseudo device locking
1846 * toggle card->shutdown to allow/disallow the device access (as a hack)
1847 */
1848static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001849{
Takashi Iwaia65d6292009-02-23 16:57:04 +01001850 spin_lock(&card->files_lock);
1851 if (card->shutdown) {
1852 spin_unlock(&card->files_lock);
1853 return -EINVAL;
1854 }
1855 card->shutdown = 1;
1856 spin_unlock(&card->files_lock);
1857 return 0;
1858}
1859
1860static void hda_unlock_devices(struct snd_card *card)
1861{
1862 spin_lock(&card->files_lock);
1863 card->shutdown = 0;
1864 spin_unlock(&card->files_lock);
1865}
1866
Takashi Iwaid5191e52009-11-16 14:58:17 +01001867/**
1868 * snd_hda_codec_reset - Clear all objects assigned to the codec
1869 * @codec: HD-audio codec
1870 *
1871 * This frees the all PCM and control elements assigned to the codec, and
1872 * clears the caches and restores the pin default configurations.
1873 *
1874 * When a device is being used, it returns -EBSY. If successfully freed,
1875 * returns zero.
1876 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001877int snd_hda_codec_reset(struct hda_codec *codec)
1878{
1879 struct snd_card *card = codec->bus->card;
1880 int i, pcm;
1881
1882 if (hda_lock_devices(card) < 0)
1883 return -EBUSY;
1884 /* check whether the codec isn't used by any mixer or PCM streams */
1885 if (!list_empty(&card->ctl_files)) {
1886 hda_unlock_devices(card);
1887 return -EBUSY;
1888 }
1889 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1890 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1891 if (!cpcm->pcm)
1892 continue;
1893 if (cpcm->pcm->streams[0].substream_opened ||
1894 cpcm->pcm->streams[1].substream_opened) {
1895 hda_unlock_devices(card);
1896 return -EBUSY;
1897 }
1898 }
1899
1900 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001901
1902#ifdef CONFIG_SND_HDA_POWER_SAVE
1903 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001904 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001905#endif
1906 snd_hda_ctls_clear(codec);
1907 /* relase PCMs */
1908 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001909 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01001910 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001911 clear_bit(codec->pcm_info[i].device,
1912 codec->bus->pcm_dev_bits);
1913 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001914 }
1915 if (codec->patch_ops.free)
1916 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001917 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001918 codec->spec = NULL;
1919 free_hda_cache(&codec->amp_cache);
1920 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001921 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1922 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01001923 /* free only driver_pins so that init_pins + user_pins are restored */
1924 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001925 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001926 codec->num_pcms = 0;
1927 codec->pcm_info = NULL;
1928 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01001929 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1930 codec->slave_dig_outs = NULL;
1931 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001932 module_put(codec->owner);
1933 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001934
1935 /* allow device access again */
1936 hda_unlock_devices(card);
1937 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001938}
1939
Takashi Iwaid5191e52009-11-16 14:58:17 +01001940/**
1941 * snd_hda_add_vmaster - create a virtual master control and add slaves
1942 * @codec: HD-audio codec
1943 * @name: vmaster control name
1944 * @tlv: TLV data (optional)
1945 * @slaves: slave control names (optional)
1946 *
1947 * Create a virtual master control with the given name. The TLV data
1948 * must be either NULL or a valid data.
1949 *
1950 * @slaves is a NULL-terminated array of strings, each of which is a
1951 * slave control name. All controls with these names are assigned to
1952 * the new virtual master control.
1953 *
1954 * This function returns zero if successful or a negative error code.
1955 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01001956int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1957 unsigned int *tlv, const char **slaves)
1958{
1959 struct snd_kcontrol *kctl;
1960 const char **s;
1961 int err;
1962
Takashi Iwai2f085542008-02-22 18:43:50 +01001963 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1964 ;
1965 if (!*s) {
1966 snd_printdd("No slave found for %s\n", name);
1967 return 0;
1968 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001969 kctl = snd_ctl_make_virtual_master(name, tlv);
1970 if (!kctl)
1971 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001972 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001973 if (err < 0)
1974 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001975
Takashi Iwai2134ea42008-01-10 16:53:55 +01001976 for (s = slaves; *s; s++) {
1977 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01001978 int i = 0;
1979 for (;;) {
1980 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1981 if (!sctl) {
1982 if (!i)
1983 snd_printdd("Cannot find slave %s, "
1984 "skipped\n", *s);
1985 break;
1986 }
1987 err = snd_ctl_add_slave(kctl, sctl);
1988 if (err < 0)
1989 return err;
1990 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001991 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001992 }
1993 return 0;
1994}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001995EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001996
Takashi Iwaid5191e52009-11-16 14:58:17 +01001997/**
1998 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
1999 *
2000 * The control element is supposed to have the private_value field
2001 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2002 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002003int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 int chs = get_amp_channels(kcontrol);
2007
2008 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2009 uinfo->count = chs == 3 ? 2 : 1;
2010 uinfo->value.integer.min = 0;
2011 uinfo->value.integer.max = 1;
2012 return 0;
2013}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002014EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Takashi Iwaid5191e52009-11-16 14:58:17 +01002016/**
2017 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2018 *
2019 * The control element is supposed to have the private_value field
2020 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2021 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002022int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2023 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2026 hda_nid_t nid = get_amp_nid(kcontrol);
2027 int chs = get_amp_channels(kcontrol);
2028 int dir = get_amp_direction(kcontrol);
2029 int idx = get_amp_index(kcontrol);
2030 long *valp = ucontrol->value.integer.value;
2031
2032 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002033 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002034 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002036 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002037 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return 0;
2039}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002040EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Takashi Iwaid5191e52009-11-16 14:58:17 +01002042/**
2043 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2044 *
2045 * The control element is supposed to have the private_value field
2046 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2047 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002048int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2049 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2052 hda_nid_t nid = get_amp_nid(kcontrol);
2053 int chs = get_amp_channels(kcontrol);
2054 int dir = get_amp_direction(kcontrol);
2055 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 long *valp = ucontrol->value.integer.value;
2057 int change = 0;
2058
Takashi Iwaicb53c622007-08-10 17:21:45 +02002059 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002060 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002061 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002062 HDA_AMP_MUTE,
2063 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002064 valp++;
2065 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002066 if (chs & 2)
2067 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002068 HDA_AMP_MUTE,
2069 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002070#ifdef CONFIG_SND_HDA_POWER_SAVE
2071 if (codec->patch_ops.check_power_status)
2072 codec->patch_ops.check_power_status(codec, nid);
2073#endif
2074 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return change;
2076}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002077EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Takashi Iwai67d634c2009-11-16 15:35:59 +01002079#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002080/**
2081 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2082 *
2083 * This function calls snd_hda_enable_beep_device(), which behaves differently
2084 * depending on beep_mode option.
2085 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002086int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2087 struct snd_ctl_elem_value *ucontrol)
2088{
2089 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2090 long *valp = ucontrol->value.integer.value;
2091
2092 snd_hda_enable_beep_device(codec, *valp);
2093 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2094}
2095EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002096#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098/*
Takashi Iwai985be542005-11-02 18:26:49 +01002099 * bound volume controls
2100 *
2101 * bind multiple volumes (# indices, from 0)
2102 */
2103
2104#define AMP_VAL_IDX_SHIFT 19
2105#define AMP_VAL_IDX_MASK (0x0f<<19)
2106
Takashi Iwaid5191e52009-11-16 14:58:17 +01002107/**
2108 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2109 *
2110 * The control element is supposed to have the private_value field
2111 * set up via HDA_BIND_MUTE*() macros.
2112 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002113int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002115{
2116 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2117 unsigned long pval;
2118 int err;
2119
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002120 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002121 pval = kcontrol->private_value;
2122 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2123 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2124 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002125 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002126 return err;
2127}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002128EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002129
Takashi Iwaid5191e52009-11-16 14:58:17 +01002130/**
2131 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2132 *
2133 * The control element is supposed to have the private_value field
2134 * set up via HDA_BIND_MUTE*() macros.
2135 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002136int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2137 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002138{
2139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2140 unsigned long pval;
2141 int i, indices, err = 0, change = 0;
2142
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002143 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002144 pval = kcontrol->private_value;
2145 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2146 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002147 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2148 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002149 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2150 if (err < 0)
2151 break;
2152 change |= err;
2153 }
2154 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002155 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002156 return err < 0 ? err : change;
2157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002158EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002159
Takashi Iwaid5191e52009-11-16 14:58:17 +01002160/**
2161 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2162 *
2163 * The control element is supposed to have the private_value field
2164 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002165 */
2166int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2167 struct snd_ctl_elem_info *uinfo)
2168{
2169 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2170 struct hda_bind_ctls *c;
2171 int err;
2172
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002173 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002174 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002175 kcontrol->private_value = *c->values;
2176 err = c->ops->info(kcontrol, uinfo);
2177 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002178 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002179 return err;
2180}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002181EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002182
Takashi Iwaid5191e52009-11-16 14:58:17 +01002183/**
2184 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2185 *
2186 * The control element is supposed to have the private_value field
2187 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2188 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002189int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2190 struct snd_ctl_elem_value *ucontrol)
2191{
2192 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2193 struct hda_bind_ctls *c;
2194 int err;
2195
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002196 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002197 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002198 kcontrol->private_value = *c->values;
2199 err = c->ops->get(kcontrol, ucontrol);
2200 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002201 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002202 return err;
2203}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002204EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002205
Takashi Iwaid5191e52009-11-16 14:58:17 +01002206/**
2207 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2208 *
2209 * The control element is supposed to have the private_value field
2210 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2211 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002212int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_value *ucontrol)
2214{
2215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2216 struct hda_bind_ctls *c;
2217 unsigned long *vals;
2218 int err = 0, change = 0;
2219
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002220 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002221 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002222 for (vals = c->values; *vals; vals++) {
2223 kcontrol->private_value = *vals;
2224 err = c->ops->put(kcontrol, ucontrol);
2225 if (err < 0)
2226 break;
2227 change |= err;
2228 }
2229 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002230 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002231 return err < 0 ? err : change;
2232}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002233EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002234
Takashi Iwaid5191e52009-11-16 14:58:17 +01002235/**
2236 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2237 *
2238 * The control element is supposed to have the private_value field
2239 * set up via HDA_BIND_VOL() macro.
2240 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002241int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2242 unsigned int size, unsigned int __user *tlv)
2243{
2244 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2245 struct hda_bind_ctls *c;
2246 int err;
2247
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002248 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002249 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002250 kcontrol->private_value = *c->values;
2251 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2252 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002253 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002254 return err;
2255}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002256EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002257
2258struct hda_ctl_ops snd_hda_bind_vol = {
2259 .info = snd_hda_mixer_amp_volume_info,
2260 .get = snd_hda_mixer_amp_volume_get,
2261 .put = snd_hda_mixer_amp_volume_put,
2262 .tlv = snd_hda_mixer_amp_tlv
2263};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002264EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002265
2266struct hda_ctl_ops snd_hda_bind_sw = {
2267 .info = snd_hda_mixer_amp_switch_info,
2268 .get = snd_hda_mixer_amp_switch_get,
2269 .put = snd_hda_mixer_amp_switch_put,
2270 .tlv = snd_hda_mixer_amp_tlv
2271};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002272EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002273
2274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * SPDIF out controls
2276 */
2277
Takashi Iwai0ba21762007-04-16 11:29:14 +02002278static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2279 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2282 uinfo->count = 1;
2283 return 0;
2284}
2285
Takashi Iwai0ba21762007-04-16 11:29:14 +02002286static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
2289 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2290 IEC958_AES0_NONAUDIO |
2291 IEC958_AES0_CON_EMPHASIS_5015 |
2292 IEC958_AES0_CON_NOT_COPYRIGHT;
2293 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2294 IEC958_AES1_CON_ORIGINAL;
2295 return 0;
2296}
2297
Takashi Iwai0ba21762007-04-16 11:29:14 +02002298static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2302 IEC958_AES0_NONAUDIO |
2303 IEC958_AES0_PRO_EMPHASIS_5015;
2304 return 0;
2305}
2306
Takashi Iwai0ba21762007-04-16 11:29:14 +02002307static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2308 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
2310 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2311
2312 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2313 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2314 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2315 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2316
2317 return 0;
2318}
2319
2320/* convert from SPDIF status bits to HDA SPDIF bits
2321 * bit 0 (DigEn) is always set zero (to be filled later)
2322 */
2323static unsigned short convert_from_spdif_status(unsigned int sbits)
2324{
2325 unsigned short val = 0;
2326
2327 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002328 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002330 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002332 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2333 IEC958_AES0_PRO_EMPHASIS_5015)
2334 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002336 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2337 IEC958_AES0_CON_EMPHASIS_5015)
2338 val |= AC_DIG1_EMPHASIS;
2339 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2340 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002342 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2344 }
2345 return val;
2346}
2347
2348/* convert to SPDIF status bits from HDA SPDIF bits
2349 */
2350static unsigned int convert_to_spdif_status(unsigned short val)
2351{
2352 unsigned int sbits = 0;
2353
Takashi Iwai0ba21762007-04-16 11:29:14 +02002354 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002356 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 sbits |= IEC958_AES0_PROFESSIONAL;
2358 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002359 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2361 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002362 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002364 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002366 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2368 sbits |= val & (0x7f << 8);
2369 }
2370 return sbits;
2371}
2372
Takashi Iwai2f728532008-09-25 16:32:41 +02002373/* set digital convert verbs both for the given NID and its slaves */
2374static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2375 int verb, int val)
2376{
2377 hda_nid_t *d;
2378
Takashi Iwai9e976972008-11-25 08:17:20 +01002379 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002380 d = codec->slave_dig_outs;
2381 if (!d)
2382 return;
2383 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002384 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002385}
2386
2387static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2388 int dig1, int dig2)
2389{
2390 if (dig1 != -1)
2391 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2392 if (dig2 != -1)
2393 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2394}
2395
Takashi Iwai0ba21762007-04-16 11:29:14 +02002396static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2397 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
2399 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2400 hda_nid_t nid = kcontrol->private_value;
2401 unsigned short val;
2402 int change;
2403
Ingo Molnar62932df2006-01-16 16:34:20 +01002404 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 codec->spdif_status = ucontrol->value.iec958.status[0] |
2406 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2407 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2408 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2409 val = convert_from_spdif_status(codec->spdif_status);
2410 val |= codec->spdif_ctls & 1;
2411 change = codec->spdif_ctls != val;
2412 codec->spdif_ctls = val;
2413
Takashi Iwai2f728532008-09-25 16:32:41 +02002414 if (change)
2415 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Ingo Molnar62932df2006-01-16 16:34:20 +01002417 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return change;
2419}
2420
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002421#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Takashi Iwai0ba21762007-04-16 11:29:14 +02002423static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2427
Takashi Iwai0ba21762007-04-16 11:29:14 +02002428 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return 0;
2430}
2431
Takashi Iwai0ba21762007-04-16 11:29:14 +02002432static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2436 hda_nid_t nid = kcontrol->private_value;
2437 unsigned short val;
2438 int change;
2439
Ingo Molnar62932df2006-01-16 16:34:20 +01002440 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002441 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002443 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002445 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002447 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002448 /* unmute amp switch (if any) */
2449 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002450 (val & AC_DIG1_ENABLE))
2451 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2452 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002454 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return change;
2456}
2457
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002458static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 {
2460 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2461 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002462 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 .info = snd_hda_spdif_mask_info,
2464 .get = snd_hda_spdif_cmask_get,
2465 },
2466 {
2467 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2468 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002469 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 .info = snd_hda_spdif_mask_info,
2471 .get = snd_hda_spdif_pmask_get,
2472 },
2473 {
2474 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002475 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .info = snd_hda_spdif_mask_info,
2477 .get = snd_hda_spdif_default_get,
2478 .put = snd_hda_spdif_default_put,
2479 },
2480 {
2481 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002482 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 .info = snd_hda_spdif_out_switch_info,
2484 .get = snd_hda_spdif_out_switch_get,
2485 .put = snd_hda_spdif_out_switch_put,
2486 },
2487 { } /* end */
2488};
2489
Takashi Iwai09f99702008-02-04 12:31:13 +01002490#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492/**
2493 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2494 * @codec: the HDA codec
2495 * @nid: audio out widget NID
2496 *
2497 * Creates controls related with the SPDIF output.
2498 * Called from each patch supporting the SPDIF out.
2499 *
2500 * Returns 0 if successful, or a negative error code.
2501 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002502int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002505 struct snd_kcontrol *kctl;
2506 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002507 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Takashi Iwai09f99702008-02-04 12:31:13 +01002509 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2510 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2511 idx))
2512 break;
2513 }
2514 if (idx >= SPDIF_MAX_IDX) {
2515 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2516 return -EBUSY;
2517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2519 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002520 if (!kctl)
2521 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002522 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002524 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002525 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return err;
2527 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002528 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002529 snd_hda_codec_read(codec, nid, 0,
2530 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2532 return 0;
2533}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002534EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002537 * SPDIF sharing with analog output
2538 */
2539static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_value *ucontrol)
2541{
2542 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2543 ucontrol->value.integer.value[0] = mout->share_spdif;
2544 return 0;
2545}
2546
2547static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2548 struct snd_ctl_elem_value *ucontrol)
2549{
2550 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2551 mout->share_spdif = !!ucontrol->value.integer.value[0];
2552 return 0;
2553}
2554
2555static struct snd_kcontrol_new spdif_share_sw = {
2556 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2557 .name = "IEC958 Default PCM Playback Switch",
2558 .info = snd_ctl_boolean_mono_info,
2559 .get = spdif_share_sw_get,
2560 .put = spdif_share_sw_put,
2561};
2562
Takashi Iwaid5191e52009-11-16 14:58:17 +01002563/**
2564 * snd_hda_create_spdif_share_sw - create Default PCM switch
2565 * @codec: the HDA codec
2566 * @mout: multi-out instance
2567 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002568int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2569 struct hda_multi_out *mout)
2570{
2571 if (!mout->dig_out_nid)
2572 return 0;
2573 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002574 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2575 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002576}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002577EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002578
2579/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 * SPDIF input
2581 */
2582
2583#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2584
Takashi Iwai0ba21762007-04-16 11:29:14 +02002585static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2589
2590 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2591 return 0;
2592}
2593
Takashi Iwai0ba21762007-04-16 11:29:14 +02002594static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2595 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
2597 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2598 hda_nid_t nid = kcontrol->private_value;
2599 unsigned int val = !!ucontrol->value.integer.value[0];
2600 int change;
2601
Ingo Molnar62932df2006-01-16 16:34:20 +01002602 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002604 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002606 snd_hda_codec_write_cache(codec, nid, 0,
2607 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002609 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return change;
2611}
2612
Takashi Iwai0ba21762007-04-16 11:29:14 +02002613static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2614 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
2616 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2617 hda_nid_t nid = kcontrol->private_value;
2618 unsigned short val;
2619 unsigned int sbits;
2620
Andrew Paprocki3982d172007-12-19 12:13:44 +01002621 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 sbits = convert_to_spdif_status(val);
2623 ucontrol->value.iec958.status[0] = sbits;
2624 ucontrol->value.iec958.status[1] = sbits >> 8;
2625 ucontrol->value.iec958.status[2] = sbits >> 16;
2626 ucontrol->value.iec958.status[3] = sbits >> 24;
2627 return 0;
2628}
2629
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002630static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 {
2632 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002633 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 .info = snd_hda_spdif_in_switch_info,
2635 .get = snd_hda_spdif_in_switch_get,
2636 .put = snd_hda_spdif_in_switch_put,
2637 },
2638 {
2639 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2640 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002641 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 .info = snd_hda_spdif_mask_info,
2643 .get = snd_hda_spdif_in_status_get,
2644 },
2645 { } /* end */
2646};
2647
2648/**
2649 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2650 * @codec: the HDA codec
2651 * @nid: audio in widget NID
2652 *
2653 * Creates controls related with the SPDIF input.
2654 * Called from each patch supporting the SPDIF in.
2655 *
2656 * Returns 0 if successful, or a negative error code.
2657 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002658int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002661 struct snd_kcontrol *kctl;
2662 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002663 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Takashi Iwai09f99702008-02-04 12:31:13 +01002665 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2666 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2667 idx))
2668 break;
2669 }
2670 if (idx >= SPDIF_MAX_IDX) {
2671 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2672 return -EBUSY;
2673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2675 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002676 if (!kctl)
2677 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002679 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002680 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return err;
2682 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002683 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002684 snd_hda_codec_read(codec, nid, 0,
2685 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002686 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return 0;
2688}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002689EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Takashi Iwaicb53c622007-08-10 17:21:45 +02002691#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002692/*
2693 * command cache
2694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002696/* build a 32bit cache key with the widget id and the command parameter */
2697#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2698#define get_cmd_cache_nid(key) ((key) & 0xff)
2699#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2700
2701/**
2702 * snd_hda_codec_write_cache - send a single command with caching
2703 * @codec: the HDA codec
2704 * @nid: NID to send the command
2705 * @direct: direct flag
2706 * @verb: the verb to send
2707 * @parm: the parameter for the verb
2708 *
2709 * Send a single command without waiting for response.
2710 *
2711 * Returns 0 if successful, or a negative error code.
2712 */
2713int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2714 int direct, unsigned int verb, unsigned int parm)
2715{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002716 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2717 struct hda_cache_head *c;
2718 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002719
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002720 if (err < 0)
2721 return err;
2722 /* parm may contain the verb stuff for get/set amp */
2723 verb = verb | (parm >> 8);
2724 parm &= 0xff;
2725 key = build_cmd_cache_key(nid, verb);
2726 mutex_lock(&codec->bus->cmd_mutex);
2727 c = get_alloc_hash(&codec->cmd_cache, key);
2728 if (c)
2729 c->val = parm;
2730 mutex_unlock(&codec->bus->cmd_mutex);
2731 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002732}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002733EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002734
Takashi Iwaid5191e52009-11-16 14:58:17 +01002735/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002736 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2737 * @codec: the HDA codec
2738 * @nid: NID to send the command
2739 * @direct: direct flag
2740 * @verb: the verb to send
2741 * @parm: the parameter for the verb
2742 *
2743 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2744 * command if the parameter is already identical with the cached value.
2745 * If not, it sends the command and refreshes the cache.
2746 *
2747 * Returns 0 if successful, or a negative error code.
2748 */
2749int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2750 int direct, unsigned int verb, unsigned int parm)
2751{
2752 struct hda_cache_head *c;
2753 u32 key;
2754
2755 /* parm may contain the verb stuff for get/set amp */
2756 verb = verb | (parm >> 8);
2757 parm &= 0xff;
2758 key = build_cmd_cache_key(nid, verb);
2759 mutex_lock(&codec->bus->cmd_mutex);
2760 c = get_hash(&codec->cmd_cache, key);
2761 if (c && c->val == parm) {
2762 mutex_unlock(&codec->bus->cmd_mutex);
2763 return 0;
2764 }
2765 mutex_unlock(&codec->bus->cmd_mutex);
2766 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2767}
2768EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2769
2770/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002771 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2772 * @codec: HD-audio codec
2773 *
2774 * Execute all verbs recorded in the command caches to resume.
2775 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002776void snd_hda_codec_resume_cache(struct hda_codec *codec)
2777{
Takashi Iwai603c4012008-07-30 15:01:44 +02002778 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002779 int i;
2780
Takashi Iwai603c4012008-07-30 15:01:44 +02002781 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002782 u32 key = buffer->key;
2783 if (!key)
2784 continue;
2785 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2786 get_cmd_cache_cmd(key), buffer->val);
2787 }
2788}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002789EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002790
2791/**
2792 * snd_hda_sequence_write_cache - sequence writes with caching
2793 * @codec: the HDA codec
2794 * @seq: VERB array to send
2795 *
2796 * Send the commands sequentially from the given array.
2797 * Thte commands are recorded on cache for power-save and resume.
2798 * The array must be terminated with NID=0.
2799 */
2800void snd_hda_sequence_write_cache(struct hda_codec *codec,
2801 const struct hda_verb *seq)
2802{
2803 for (; seq->nid; seq++)
2804 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2805 seq->param);
2806}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002807EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002808#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002809
Takashi Iwai54d17402005-11-21 16:33:22 +01002810/*
2811 * set power state of the codec
2812 */
2813static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2814 unsigned int power_state)
2815{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002816 hda_nid_t nid;
2817 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002818
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002819 /* this delay seems necessary to avoid click noise at power-down */
2820 if (power_state == AC_PWRST_D3)
2821 msleep(100);
2822 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002823 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002824 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002825 if (power_state == AC_PWRST_D0 &&
2826 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002827 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002828
Takashi Iwaicb53c622007-08-10 17:21:45 +02002829 nid = codec->start_nid;
2830 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002831 unsigned int wcaps = get_wcaps(codec, nid);
2832 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002833 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002834 if (power_state == AC_PWRST_D3 &&
2835 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002836 unsigned int pincap;
2837 /*
2838 * don't power down the widget if it controls
2839 * eapd and EAPD_BTLENABLE is set.
2840 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01002841 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002842 if (pincap & AC_PINCAP_EAPD) {
2843 int eapd = snd_hda_codec_read(codec,
2844 nid, 0,
2845 AC_VERB_GET_EAPD_BTLENABLE, 0);
2846 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002847 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002848 continue;
2849 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002850 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002851 snd_hda_codec_write(codec, nid, 0,
2852 AC_VERB_SET_POWER_STATE,
2853 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002854 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002855 }
2856
Takashi Iwaicb53c622007-08-10 17:21:45 +02002857 if (power_state == AC_PWRST_D0) {
2858 unsigned long end_time;
2859 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002860 /* wait until the codec reachs to D0 */
2861 end_time = jiffies + msecs_to_jiffies(500);
2862 do {
2863 state = snd_hda_codec_read(codec, fg, 0,
2864 AC_VERB_GET_POWER_STATE, 0);
2865 if (state == power_state)
2866 break;
2867 msleep(1);
2868 } while (time_after_eq(end_time, jiffies));
2869 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002870}
2871
Takashi Iwai11aeff02008-07-30 15:01:46 +02002872#ifdef CONFIG_SND_HDA_HWDEP
2873/* execute additional init verbs */
2874static void hda_exec_init_verbs(struct hda_codec *codec)
2875{
2876 if (codec->init_verbs.list)
2877 snd_hda_sequence_write(codec, codec->init_verbs.list);
2878}
2879#else
2880static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2881#endif
2882
Takashi Iwaicb53c622007-08-10 17:21:45 +02002883#ifdef SND_HDA_NEEDS_RESUME
2884/*
2885 * call suspend and power-down; used both from PM and power-save
2886 */
2887static void hda_call_codec_suspend(struct hda_codec *codec)
2888{
2889 if (codec->patch_ops.suspend)
2890 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2891 hda_set_power_state(codec,
2892 codec->afg ? codec->afg : codec->mfg,
2893 AC_PWRST_D3);
2894#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01002895 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002896 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002897 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002898 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01002899 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002900#endif
2901}
2902
2903/*
2904 * kick up codec; used both from PM and power-save
2905 */
2906static void hda_call_codec_resume(struct hda_codec *codec)
2907{
2908 hda_set_power_state(codec,
2909 codec->afg ? codec->afg : codec->mfg,
2910 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01002911 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwai11aeff02008-07-30 15:01:46 +02002912 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002913 if (codec->patch_ops.resume)
2914 codec->patch_ops.resume(codec);
2915 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002916 if (codec->patch_ops.init)
2917 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002918 snd_hda_codec_resume_amp(codec);
2919 snd_hda_codec_resume_cache(codec);
2920 }
2921}
2922#endif /* SND_HDA_NEEDS_RESUME */
2923
Takashi Iwai54d17402005-11-21 16:33:22 +01002924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925/**
2926 * snd_hda_build_controls - build mixer controls
2927 * @bus: the BUS
2928 *
2929 * Creates mixer controls for each codec included in the bus.
2930 *
2931 * Returns 0 if successful, otherwise a negative error code.
2932 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002933int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002935 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Takashi Iwai0ba21762007-04-16 11:29:14 +02002937 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002938 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002939 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01002940 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002941 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002942 err = snd_hda_codec_reset(codec);
2943 if (err < 0) {
2944 printk(KERN_ERR
2945 "hda_codec: cannot revert codec\n");
2946 return err;
2947 }
2948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002950 return 0;
2951}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002952EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002954int snd_hda_codec_build_controls(struct hda_codec *codec)
2955{
2956 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02002957 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002958 /* continue to initialize... */
2959 if (codec->patch_ops.init)
2960 err = codec->patch_ops.init(codec);
2961 if (!err && codec->patch_ops.build_controls)
2962 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002963 if (err < 0)
2964 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 return 0;
2966}
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968/*
2969 * stream formats
2970 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002971struct hda_rate_tbl {
2972 unsigned int hz;
2973 unsigned int alsa_bits;
2974 unsigned int hda_fmt;
2975};
2976
2977static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002979
2980 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2982 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2983 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2984 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2985 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2986 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2987 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2988 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2989 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2990 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2991 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002992#define AC_PAR_PCM_RATE_BITS 11
2993 /* up to bits 10, 384kHZ isn't supported properly */
2994
2995 /* not autodetected value */
2996 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002997
Takashi Iwaibefdf312005-08-22 13:57:55 +02002998 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999};
3000
3001/**
3002 * snd_hda_calc_stream_format - calculate format bitset
3003 * @rate: the sample rate
3004 * @channels: the number of channels
3005 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3006 * @maxbps: the max. bps
3007 *
3008 * Calculate the format bitset from the given rate, channels and th PCM format.
3009 *
3010 * Return zero if invalid.
3011 */
3012unsigned int snd_hda_calc_stream_format(unsigned int rate,
3013 unsigned int channels,
3014 unsigned int format,
3015 unsigned int maxbps)
3016{
3017 int i;
3018 unsigned int val = 0;
3019
Takashi Iwaibefdf312005-08-22 13:57:55 +02003020 for (i = 0; rate_bits[i].hz; i++)
3021 if (rate_bits[i].hz == rate) {
3022 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 break;
3024 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003025 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 snd_printdd("invalid rate %d\n", rate);
3027 return 0;
3028 }
3029
3030 if (channels == 0 || channels > 8) {
3031 snd_printdd("invalid channels %d\n", channels);
3032 return 0;
3033 }
3034 val |= channels - 1;
3035
3036 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003037 case 8:
3038 val |= 0x00;
3039 break;
3040 case 16:
3041 val |= 0x10;
3042 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 case 20:
3044 case 24:
3045 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003046 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 val |= 0x40;
3048 else if (maxbps >= 24)
3049 val |= 0x30;
3050 else
3051 val |= 0x20;
3052 break;
3053 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003054 snd_printdd("invalid format width %d\n",
3055 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return 0;
3057 }
3058
3059 return val;
3060}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003061EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003063static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3064{
3065 unsigned int val = 0;
3066 if (nid != codec->afg &&
3067 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3068 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3069 if (!val || val == -1)
3070 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3071 if (!val || val == -1)
3072 return 0;
3073 return val;
3074}
3075
3076static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3077{
3078 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3079 get_pcm_param);
3080}
3081
3082static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3083{
3084 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3085 if (!streams || streams == -1)
3086 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3087 if (!streams || streams == -1)
3088 return 0;
3089 return streams;
3090}
3091
3092static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3093{
3094 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3095 get_stream_param);
3096}
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098/**
3099 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3100 * @codec: the HDA codec
3101 * @nid: NID to query
3102 * @ratesp: the pointer to store the detected rate bitflags
3103 * @formatsp: the pointer to store the detected formats
3104 * @bpsp: the pointer to store the detected format widths
3105 *
3106 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3107 * or @bsps argument is ignored.
3108 *
3109 * Returns 0 if successful, otherwise a negative error code.
3110 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003111static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3113{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003114 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003116 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003117 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 if (ratesp) {
3120 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003121 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003123 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003125 if (rates == 0) {
3126 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3127 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3128 nid, val,
3129 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3130 return -EIO;
3131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 *ratesp = rates;
3133 }
3134
3135 if (formatsp || bpsp) {
3136 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003137 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003139 streams = query_stream_param(codec, nid);
3140 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
3143 bps = 0;
3144 if (streams & AC_SUPFMT_PCM) {
3145 if (val & AC_SUPPCM_BITS_8) {
3146 formats |= SNDRV_PCM_FMTBIT_U8;
3147 bps = 8;
3148 }
3149 if (val & AC_SUPPCM_BITS_16) {
3150 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3151 bps = 16;
3152 }
3153 if (wcaps & AC_WCAP_DIGITAL) {
3154 if (val & AC_SUPPCM_BITS_32)
3155 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3156 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3157 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3158 if (val & AC_SUPPCM_BITS_24)
3159 bps = 24;
3160 else if (val & AC_SUPPCM_BITS_20)
3161 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003162 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3163 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3165 if (val & AC_SUPPCM_BITS_32)
3166 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 else if (val & AC_SUPPCM_BITS_24)
3168 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003169 else if (val & AC_SUPPCM_BITS_20)
3170 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
3172 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003173 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003175 if (!bps)
3176 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003177 }
3178 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003179 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 /* temporary hack: we have still no proper support
3181 * for the direct AC3 stream...
3182 */
3183 formats |= SNDRV_PCM_FMTBIT_U8;
3184 bps = 8;
3185 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003186 if (formats == 0) {
3187 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3188 "(nid=0x%x, val=0x%x, ovrd=%i, "
3189 "streams=0x%x)\n",
3190 nid, val,
3191 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3192 streams);
3193 return -EIO;
3194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 if (formatsp)
3196 *formatsp = formats;
3197 if (bpsp)
3198 *bpsp = bps;
3199 }
3200
3201 return 0;
3202}
3203
3204/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003205 * snd_hda_is_supported_format - Check the validity of the format
3206 * @codec: HD-audio codec
3207 * @nid: NID to check
3208 * @format: the HD-audio format value to check
3209 *
3210 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 *
3212 * Returns 1 if supported, 0 if not.
3213 */
3214int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3215 unsigned int format)
3216{
3217 int i;
3218 unsigned int val = 0, rate, stream;
3219
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003220 val = query_pcm_param(codec, nid);
3221 if (!val)
3222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003225 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003226 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (val & (1 << i))
3228 break;
3229 return 0;
3230 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003231 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return 0;
3233
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003234 stream = query_stream_param(codec, nid);
3235 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return 0;
3237
3238 if (stream & AC_SUPFMT_PCM) {
3239 switch (format & 0xf0) {
3240 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003241 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return 0;
3243 break;
3244 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003245 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return 0;
3247 break;
3248 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003249 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return 0;
3251 break;
3252 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003253 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return 0;
3255 break;
3256 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003257 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return 0;
3259 break;
3260 default:
3261 return 0;
3262 }
3263 } else {
3264 /* FIXME: check for float32 and AC3? */
3265 }
3266
3267 return 1;
3268}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003269EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271/*
3272 * PCM stuff
3273 */
3274static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3275 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003276 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 return 0;
3279}
3280
3281static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3282 struct hda_codec *codec,
3283 unsigned int stream_tag,
3284 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003285 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
3287 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3288 return 0;
3289}
3290
3291static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3292 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003293 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
Takashi Iwai888afa12008-03-18 09:57:50 +01003295 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return 0;
3297}
3298
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003299static int set_pcm_default_values(struct hda_codec *codec,
3300 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003302 int err;
3303
Takashi Iwai0ba21762007-04-16 11:29:14 +02003304 /* query support PCM information from the given NID */
3305 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003306 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003307 info->rates ? NULL : &info->rates,
3308 info->formats ? NULL : &info->formats,
3309 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003310 if (err < 0)
3311 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 }
3313 if (info->ops.open == NULL)
3314 info->ops.open = hda_pcm_default_open_close;
3315 if (info->ops.close == NULL)
3316 info->ops.close = hda_pcm_default_open_close;
3317 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003318 if (snd_BUG_ON(!info->nid))
3319 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 info->ops.prepare = hda_pcm_default_prepare;
3321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003323 if (snd_BUG_ON(!info->nid))
3324 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 info->ops.cleanup = hda_pcm_default_cleanup;
3326 }
3327 return 0;
3328}
3329
Takashi Iwaid5191e52009-11-16 14:58:17 +01003330/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003331const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3332 "Audio", "SPDIF", "HDMI", "Modem"
3333};
3334
Takashi Iwai176d5332008-07-30 15:01:44 +02003335/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003336 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003337 *
3338 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003339 */
3340static int get_empty_pcm_device(struct hda_bus *bus, int type)
3341{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003342 /* audio device indices; not linear to keep compatibility */
3343 static int audio_idx[HDA_PCM_NTYPES][5] = {
3344 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3345 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003346 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003347 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003348 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003349 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003350
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003351 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003352 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3353 return -EINVAL;
3354 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003355
3356 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3357 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3358 return audio_idx[type][i];
3359
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003360 snd_printk(KERN_WARNING "Too many %s devices\n",
3361 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003362 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003363}
3364
3365/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003366 * attach a new PCM stream
3367 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003368static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003369{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003370 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003371 struct hda_pcm_stream *info;
3372 int stream, err;
3373
Takashi Iwaib91f0802008-11-04 08:43:08 +01003374 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003375 return -EINVAL;
3376 for (stream = 0; stream < 2; stream++) {
3377 info = &pcm->stream[stream];
3378 if (info->substreams) {
3379 err = set_pcm_default_values(codec, info);
3380 if (err < 0)
3381 return err;
3382 }
3383 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003384 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003385}
3386
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003387/* assign all PCMs of the given codec */
3388int snd_hda_codec_build_pcms(struct hda_codec *codec)
3389{
3390 unsigned int pcm;
3391 int err;
3392
3393 if (!codec->num_pcms) {
3394 if (!codec->patch_ops.build_pcms)
3395 return 0;
3396 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003397 if (err < 0) {
3398 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003399 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003400 err = snd_hda_codec_reset(codec);
3401 if (err < 0) {
3402 printk(KERN_ERR
3403 "hda_codec: cannot revert codec\n");
3404 return err;
3405 }
3406 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003407 }
3408 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3409 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3410 int dev;
3411
3412 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003413 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003414
3415 if (!cpcm->pcm) {
3416 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3417 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003418 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003419 cpcm->device = dev;
3420 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003421 if (err < 0) {
3422 printk(KERN_ERR "hda_codec: cannot attach "
3423 "PCM stream %d for codec #%d\n",
3424 dev, codec->addr);
3425 continue; /* no fatal error */
3426 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003427 }
3428 }
3429 return 0;
3430}
3431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432/**
3433 * snd_hda_build_pcms - build PCM information
3434 * @bus: the BUS
3435 *
3436 * Create PCM information for each codec included in the bus.
3437 *
3438 * The build_pcms codec patch is requested to set up codec->num_pcms and
3439 * codec->pcm_info properly. The array is referred by the top-level driver
3440 * to create its PCM instances.
3441 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3442 * callback.
3443 *
3444 * At least, substreams, channels_min and channels_max must be filled for
3445 * each stream. substreams = 0 indicates that the stream doesn't exist.
3446 * When rates and/or formats are zero, the supported values are queried
3447 * from the given nid. The nid is used also by the default ops.prepare
3448 * and ops.cleanup callbacks.
3449 *
3450 * The driver needs to call ops.open in its open callback. Similarly,
3451 * ops.close is supposed to be called in the close callback.
3452 * ops.prepare should be called in the prepare or hw_params callback
3453 * with the proper parameters for set up.
3454 * ops.cleanup should be called in hw_free for clean up of streams.
3455 *
3456 * This function returns 0 if successfull, or a negative error code.
3457 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003458int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003460 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Takashi Iwai0ba21762007-04-16 11:29:14 +02003462 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003463 int err = snd_hda_codec_build_pcms(codec);
3464 if (err < 0)
3465 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 }
3467 return 0;
3468}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003469EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471/**
3472 * snd_hda_check_board_config - compare the current codec with the config table
3473 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003474 * @num_configs: number of config enums
3475 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 * @tbl: configuration table, terminated by null entries
3477 *
3478 * Compares the modelname or PCI subsystem id of the current codec with the
3479 * given configuration table. If a matching entry is found, returns its
3480 * config value (supposed to be 0 or positive).
3481 *
3482 * If no entries are matching, the function returns a negative value.
3483 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003484int snd_hda_check_board_config(struct hda_codec *codec,
3485 int num_configs, const char **models,
3486 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003488 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003489 int i;
3490 for (i = 0; i < num_configs; i++) {
3491 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003492 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003493 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3494 "selected\n", models[i]);
3495 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
3497 }
3498 }
3499
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003500 if (!codec->bus->pci || !tbl)
3501 return -1;
3502
3503 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3504 if (!tbl)
3505 return -1;
3506 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003507#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003508 char tmp[10];
3509 const char *model = NULL;
3510 if (models)
3511 model = models[tbl->value];
3512 if (!model) {
3513 sprintf(tmp, "#%d", tbl->value);
3514 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003516 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3517 "for config %x:%x (%s)\n",
3518 model, tbl->subvendor, tbl->subdevice,
3519 (tbl->name ? tbl->name : "Unknown device"));
3520#endif
3521 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 }
3523 return -1;
3524}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003525EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003528 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003529 subsystem ID with the
3530 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003531
3532 This is important for Gateway notebooks with SB450 HDA Audio
3533 where the vendor ID of the PCI device is:
3534 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3535 and the vendor/subvendor are found only at the codec.
3536
3537 * @codec: the HDA codec
3538 * @num_configs: number of config enums
3539 * @models: array of model name strings
3540 * @tbl: configuration table, terminated by null entries
3541 *
3542 * Compares the modelname or PCI subsystem id of the current codec with the
3543 * given configuration table. If a matching entry is found, returns its
3544 * config value (supposed to be 0 or positive).
3545 *
3546 * If no entries are matching, the function returns a negative value.
3547 */
3548int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3549 int num_configs, const char **models,
3550 const struct snd_pci_quirk *tbl)
3551{
3552 const struct snd_pci_quirk *q;
3553
3554 /* Search for codec ID */
3555 for (q = tbl; q->subvendor; q++) {
3556 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3557
3558 if (vendorid == codec->subsystem_id)
3559 break;
3560 }
3561
3562 if (!q->subvendor)
3563 return -1;
3564
3565 tbl = q;
3566
3567 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003568#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003569 char tmp[10];
3570 const char *model = NULL;
3571 if (models)
3572 model = models[tbl->value];
3573 if (!model) {
3574 sprintf(tmp, "#%d", tbl->value);
3575 model = tmp;
3576 }
3577 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3578 "for config %x:%x (%s)\n",
3579 model, tbl->subvendor, tbl->subdevice,
3580 (tbl->name ? tbl->name : "Unknown device"));
3581#endif
3582 return tbl->value;
3583 }
3584 return -1;
3585}
3586EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3587
3588/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 * snd_hda_add_new_ctls - create controls from the array
3590 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003591 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 *
3593 * This helper function creates and add new controls in the given array.
3594 * The array must be terminated with an empty entry as terminator.
3595 *
3596 * Returns 0 if successful, or a negative error code.
3597 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003598int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003600 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003603 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003604 if (knew->iface == -1) /* skip this codec private value */
3605 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003606 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003607 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003608 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003609 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003610 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003611 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003612 return err;
3613 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003614 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003615 return -ENOMEM;
3616 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003617 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003618 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003619 return err;
3620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 }
3622 return 0;
3623}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003624EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
Takashi Iwaicb53c622007-08-10 17:21:45 +02003626#ifdef CONFIG_SND_HDA_POWER_SAVE
3627static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3628 unsigned int power_state);
3629
3630static void hda_power_work(struct work_struct *work)
3631{
3632 struct hda_codec *codec =
3633 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003634 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003635
Maxim Levitsky2e492462007-09-03 15:26:57 +02003636 if (!codec->power_on || codec->power_count) {
3637 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003638 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003639 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003640
3641 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003642 if (bus->ops.pm_notify)
3643 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003644}
3645
3646static void hda_keep_power_on(struct hda_codec *codec)
3647{
3648 codec->power_count++;
3649 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003650 codec->power_jiffies = jiffies;
3651}
3652
Takashi Iwaid5191e52009-11-16 14:58:17 +01003653/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003654void snd_hda_update_power_acct(struct hda_codec *codec)
3655{
3656 unsigned long delta = jiffies - codec->power_jiffies;
3657 if (codec->power_on)
3658 codec->power_on_acct += delta;
3659 else
3660 codec->power_off_acct += delta;
3661 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003662}
3663
Takashi Iwaid5191e52009-11-16 14:58:17 +01003664/**
3665 * snd_hda_power_up - Power-up the codec
3666 * @codec: HD-audio codec
3667 *
3668 * Increment the power-up counter and power up the hardware really when
3669 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003670 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003671void snd_hda_power_up(struct hda_codec *codec)
3672{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003673 struct hda_bus *bus = codec->bus;
3674
Takashi Iwaicb53c622007-08-10 17:21:45 +02003675 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003676 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003677 return;
3678
Takashi Iwaia2f63092009-11-11 09:34:25 +01003679 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003680 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003681 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003682 if (bus->ops.pm_notify)
3683 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003684 hda_call_codec_resume(codec);
3685 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003686 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003687}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003688EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003689
3690#define power_save(codec) \
3691 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003692
Takashi Iwaid5191e52009-11-16 14:58:17 +01003693/**
3694 * snd_hda_power_down - Power-down the codec
3695 * @codec: HD-audio codec
3696 *
3697 * Decrement the power-up counter and schedules the power-off work if
3698 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003699 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003700void snd_hda_power_down(struct hda_codec *codec)
3701{
3702 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003703 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003704 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003705 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003706 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003707 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003708 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003709 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003710}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003711EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003712
Takashi Iwaid5191e52009-11-16 14:58:17 +01003713/**
3714 * snd_hda_check_amp_list_power - Check the amp list and update the power
3715 * @codec: HD-audio codec
3716 * @check: the object containing an AMP list and the status
3717 * @nid: NID to check / update
3718 *
3719 * Check whether the given NID is in the amp list. If it's in the list,
3720 * check the current AMP status, and update the the power-status according
3721 * to the mute status.
3722 *
3723 * This function is supposed to be set or called from the check_power_status
3724 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003725 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003726int snd_hda_check_amp_list_power(struct hda_codec *codec,
3727 struct hda_loopback_check *check,
3728 hda_nid_t nid)
3729{
3730 struct hda_amp_list *p;
3731 int ch, v;
3732
3733 if (!check->amplist)
3734 return 0;
3735 for (p = check->amplist; p->nid; p++) {
3736 if (p->nid == nid)
3737 break;
3738 }
3739 if (!p->nid)
3740 return 0; /* nothing changed */
3741
3742 for (p = check->amplist; p->nid; p++) {
3743 for (ch = 0; ch < 2; ch++) {
3744 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3745 p->idx);
3746 if (!(v & HDA_AMP_MUTE) && v > 0) {
3747 if (!check->power_on) {
3748 check->power_on = 1;
3749 snd_hda_power_up(codec);
3750 }
3751 return 1;
3752 }
3753 }
3754 }
3755 if (check->power_on) {
3756 check->power_on = 0;
3757 snd_hda_power_down(codec);
3758 }
3759 return 0;
3760}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003761EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003762#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003764/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003765 * Channel mode helper
3766 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003767
3768/**
3769 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3770 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003771int snd_hda_ch_mode_info(struct hda_codec *codec,
3772 struct snd_ctl_elem_info *uinfo,
3773 const struct hda_channel_mode *chmode,
3774 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003775{
3776 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3777 uinfo->count = 1;
3778 uinfo->value.enumerated.items = num_chmodes;
3779 if (uinfo->value.enumerated.item >= num_chmodes)
3780 uinfo->value.enumerated.item = num_chmodes - 1;
3781 sprintf(uinfo->value.enumerated.name, "%dch",
3782 chmode[uinfo->value.enumerated.item].channels);
3783 return 0;
3784}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003785EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003786
Takashi Iwaid5191e52009-11-16 14:58:17 +01003787/**
3788 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3789 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003790int snd_hda_ch_mode_get(struct hda_codec *codec,
3791 struct snd_ctl_elem_value *ucontrol,
3792 const struct hda_channel_mode *chmode,
3793 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003794 int max_channels)
3795{
3796 int i;
3797
3798 for (i = 0; i < num_chmodes; i++) {
3799 if (max_channels == chmode[i].channels) {
3800 ucontrol->value.enumerated.item[0] = i;
3801 break;
3802 }
3803 }
3804 return 0;
3805}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003806EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003807
Takashi Iwaid5191e52009-11-16 14:58:17 +01003808/**
3809 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3810 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003811int snd_hda_ch_mode_put(struct hda_codec *codec,
3812 struct snd_ctl_elem_value *ucontrol,
3813 const struct hda_channel_mode *chmode,
3814 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003815 int *max_channelsp)
3816{
3817 unsigned int mode;
3818
3819 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01003820 if (mode >= num_chmodes)
3821 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003822 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003823 return 0;
3824 /* change the current channel setting */
3825 *max_channelsp = chmode[mode].channels;
3826 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003827 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003828 return 1;
3829}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003830EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832/*
3833 * input MUX helper
3834 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003835
3836/**
3837 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3838 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003839int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3840 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841{
3842 unsigned int index;
3843
3844 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3845 uinfo->count = 1;
3846 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003847 if (!imux->num_items)
3848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 index = uinfo->value.enumerated.item;
3850 if (index >= imux->num_items)
3851 index = imux->num_items - 1;
3852 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3853 return 0;
3854}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003855EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Takashi Iwaid5191e52009-11-16 14:58:17 +01003857/**
3858 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3859 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003860int snd_hda_input_mux_put(struct hda_codec *codec,
3861 const struct hda_input_mux *imux,
3862 struct snd_ctl_elem_value *ucontrol,
3863 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 unsigned int *cur_val)
3865{
3866 unsigned int idx;
3867
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003868 if (!imux->num_items)
3869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 idx = ucontrol->value.enumerated.item[0];
3871 if (idx >= imux->num_items)
3872 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003873 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003875 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3876 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 *cur_val = idx;
3878 return 1;
3879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003880EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882
3883/*
3884 * Multi-channel / digital-out PCM helper functions
3885 */
3886
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003887/* setup SPDIF output stream */
3888static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3889 unsigned int stream_tag, unsigned int format)
3890{
3891 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02003892 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003893 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02003894 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3895 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003896 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003897 if (codec->slave_dig_outs) {
3898 hda_nid_t *d;
3899 for (d = codec->slave_dig_outs; *d; d++)
3900 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3901 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003902 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003903 /* turn on again (if needed) */
3904 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3905 set_dig_out_convert(codec, nid,
3906 codec->spdif_ctls & 0xff, -1);
3907}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003908
Takashi Iwai2f728532008-09-25 16:32:41 +02003909static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3910{
3911 snd_hda_codec_cleanup_stream(codec, nid);
3912 if (codec->slave_dig_outs) {
3913 hda_nid_t *d;
3914 for (d = codec->slave_dig_outs; *d; d++)
3915 snd_hda_codec_cleanup_stream(codec, *d);
3916 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003917}
3918
Takashi Iwaid5191e52009-11-16 14:58:17 +01003919/**
3920 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
3921 * @bus: HD-audio bus
3922 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003923void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3924{
3925 struct hda_codec *codec;
3926
3927 if (!bus)
3928 return;
3929 list_for_each_entry(codec, &bus->codec_list, list) {
3930#ifdef CONFIG_SND_HDA_POWER_SAVE
3931 if (!codec->power_on)
3932 continue;
3933#endif
3934 if (codec->patch_ops.reboot_notify)
3935 codec->patch_ops.reboot_notify(codec);
3936 }
3937}
Takashi Iwai8f217a22009-11-10 18:26:12 +01003938EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003939
Takashi Iwaid5191e52009-11-16 14:58:17 +01003940/**
3941 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003943int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3944 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945{
Ingo Molnar62932df2006-01-16 16:34:20 +01003946 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003947 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3948 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003949 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003951 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 return 0;
3953}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003954EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Takashi Iwaid5191e52009-11-16 14:58:17 +01003956/**
3957 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3958 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003959int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3960 struct hda_multi_out *mout,
3961 unsigned int stream_tag,
3962 unsigned int format,
3963 struct snd_pcm_substream *substream)
3964{
3965 mutex_lock(&codec->spdif_mutex);
3966 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3967 mutex_unlock(&codec->spdif_mutex);
3968 return 0;
3969}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003970EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003971
Takashi Iwaid5191e52009-11-16 14:58:17 +01003972/**
3973 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3974 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003975int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3976 struct hda_multi_out *mout)
3977{
3978 mutex_lock(&codec->spdif_mutex);
3979 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3980 mutex_unlock(&codec->spdif_mutex);
3981 return 0;
3982}
3983EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3984
Takashi Iwaid5191e52009-11-16 14:58:17 +01003985/**
3986 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003988int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3989 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
Ingo Molnar62932df2006-01-16 16:34:20 +01003991 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003993 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return 0;
3995}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003996EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Takashi Iwaid5191e52009-11-16 14:58:17 +01003998/**
3999 * snd_hda_multi_out_analog_open - open analog outputs
4000 *
4001 * Open analog outputs and set up the hw-constraints.
4002 * If the digital outputs can be opened as slave, open the digital
4003 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004005int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4006 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004007 struct snd_pcm_substream *substream,
4008 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Takashi Iwai9a081602008-02-12 18:37:26 +01004010 struct snd_pcm_runtime *runtime = substream->runtime;
4011 runtime->hw.channels_max = mout->max_channels;
4012 if (mout->dig_out_nid) {
4013 if (!mout->analog_rates) {
4014 mout->analog_rates = hinfo->rates;
4015 mout->analog_formats = hinfo->formats;
4016 mout->analog_maxbps = hinfo->maxbps;
4017 } else {
4018 runtime->hw.rates = mout->analog_rates;
4019 runtime->hw.formats = mout->analog_formats;
4020 hinfo->maxbps = mout->analog_maxbps;
4021 }
4022 if (!mout->spdif_rates) {
4023 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4024 &mout->spdif_rates,
4025 &mout->spdif_formats,
4026 &mout->spdif_maxbps);
4027 }
4028 mutex_lock(&codec->spdif_mutex);
4029 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004030 if ((runtime->hw.rates & mout->spdif_rates) &&
4031 (runtime->hw.formats & mout->spdif_formats)) {
4032 runtime->hw.rates &= mout->spdif_rates;
4033 runtime->hw.formats &= mout->spdif_formats;
4034 if (mout->spdif_maxbps < hinfo->maxbps)
4035 hinfo->maxbps = mout->spdif_maxbps;
4036 } else {
4037 mout->share_spdif = 0;
4038 /* FIXME: need notify? */
4039 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004040 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004041 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4044 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4045}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004046EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Takashi Iwaid5191e52009-11-16 14:58:17 +01004048/**
4049 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4050 *
4051 * Set up the i/o for analog out.
4052 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004054int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4055 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 unsigned int stream_tag,
4057 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004058 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
4060 hda_nid_t *nids = mout->dac_nids;
4061 int chs = substream->runtime->channels;
4062 int i;
4063
Ingo Molnar62932df2006-01-16 16:34:20 +01004064 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004065 if (mout->dig_out_nid && mout->share_spdif &&
4066 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004068 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4069 format) &&
4070 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004072 setup_dig_out_stream(codec, mout->dig_out_nid,
4073 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 } else {
4075 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004076 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 }
4078 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004079 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
4081 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004082 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4083 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004084 if (!mout->no_share_stream &&
4085 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004087 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4088 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004089 /* extra outputs copied from front */
4090 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004091 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004092 snd_hda_codec_setup_stream(codec,
4093 mout->extra_out_nid[i],
4094 stream_tag, 0, format);
4095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 /* surrounds */
4097 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004098 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004099 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4100 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004101 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004102 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4103 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
4105 return 0;
4106}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004107EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Takashi Iwaid5191e52009-11-16 14:58:17 +01004109/**
4110 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004112int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4113 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
4115 hda_nid_t *nids = mout->dac_nids;
4116 int i;
4117
4118 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004119 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004121 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004122 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4123 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004124 snd_hda_codec_cleanup_stream(codec,
4125 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004126 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004128 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 mout->dig_out_used = 0;
4130 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004131 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 return 0;
4133}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004134EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004136/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004137 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004138 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004139
Takashi Iwai12f288b2007-08-02 15:51:59 +02004140static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004141{
4142 for (; *list; list++)
4143 if (*list == nid)
4144 return 1;
4145 return 0;
4146}
4147
Steve Longerbeam81937d32007-05-08 15:33:03 +02004148
4149/*
4150 * Sort an associated group of pins according to their sequence numbers.
4151 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004152static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004153 int num_pins)
4154{
4155 int i, j;
4156 short seq;
4157 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004158
Steve Longerbeam81937d32007-05-08 15:33:03 +02004159 for (i = 0; i < num_pins; i++) {
4160 for (j = i + 1; j < num_pins; j++) {
4161 if (sequences[i] > sequences[j]) {
4162 seq = sequences[i];
4163 sequences[i] = sequences[j];
4164 sequences[j] = seq;
4165 nid = pins[i];
4166 pins[i] = pins[j];
4167 pins[j] = nid;
4168 }
4169 }
4170 }
4171}
4172
4173
Takashi Iwai82bc9552006-03-21 11:24:42 +01004174/*
4175 * Parse all pin widgets and store the useful pin nids to cfg
4176 *
4177 * The number of line-outs or any primary output is stored in line_outs,
4178 * and the corresponding output pins are assigned to line_out_pins[],
4179 * in the order of front, rear, CLFE, side, ...
4180 *
4181 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004182 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004183 * is detected, one of speaker of HP pins is assigned as the primary
4184 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4185 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004186 *
Takashi Iwai82bc9552006-03-21 11:24:42 +01004187 * The analog input pins are assigned to input_pins array.
4188 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4189 * respectively.
4190 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004191int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4192 struct auto_pin_cfg *cfg,
4193 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004194{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004195 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004196 short seq, assoc_line_out, assoc_speaker;
4197 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4198 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004199 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004200
4201 memset(cfg, 0, sizeof(*cfg));
4202
Steve Longerbeam81937d32007-05-08 15:33:03 +02004203 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4204 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004205 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004206 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004207
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004208 end_nid = codec->start_nid + codec->num_nodes;
4209 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004210 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004211 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004212 unsigned int def_conf;
4213 short assoc, loc;
4214
4215 /* read all default configuration for pin complex */
4216 if (wid_type != AC_WID_PIN)
4217 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004218 /* ignore the given nids (e.g. pc-beep returns error) */
4219 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4220 continue;
4221
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004222 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004223 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4224 continue;
4225 loc = get_defcfg_location(def_conf);
4226 switch (get_defcfg_device(def_conf)) {
4227 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004228 seq = get_defcfg_sequence(def_conf);
4229 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004230
4231 if (!(wid_caps & AC_WCAP_STEREO))
4232 if (!cfg->mono_out_pin)
4233 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004234 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004235 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004236 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004237 assoc_line_out = assoc;
4238 else if (assoc_line_out != assoc)
4239 continue;
4240 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4241 continue;
4242 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004243 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004244 cfg->line_outs++;
4245 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004246 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004247 seq = get_defcfg_sequence(def_conf);
4248 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004249 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004250 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004251 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004252 assoc_speaker = assoc;
4253 else if (assoc_speaker != assoc)
4254 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004255 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4256 continue;
4257 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004258 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004259 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004260 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004261 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004262 seq = get_defcfg_sequence(def_conf);
4263 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004264 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4265 continue;
4266 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004267 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004268 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004269 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004270 case AC_JACK_MIC_IN: {
4271 int preferred, alt;
Kailang Yang6ff86a32010-03-19 11:14:36 +01004272 if (loc == AC_JACK_LOC_FRONT ||
4273 (loc & 0x30) == AC_JACK_LOC_INTERNAL) {
Takashi Iwai314634b2006-09-21 11:56:18 +02004274 preferred = AUTO_PIN_FRONT_MIC;
4275 alt = AUTO_PIN_MIC;
4276 } else {
4277 preferred = AUTO_PIN_MIC;
4278 alt = AUTO_PIN_FRONT_MIC;
4279 }
4280 if (!cfg->input_pins[preferred])
4281 cfg->input_pins[preferred] = nid;
4282 else if (!cfg->input_pins[alt])
4283 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004284 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004285 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004286 case AC_JACK_LINE_IN:
4287 if (loc == AC_JACK_LOC_FRONT)
4288 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4289 else
4290 cfg->input_pins[AUTO_PIN_LINE] = nid;
4291 break;
4292 case AC_JACK_CD:
4293 cfg->input_pins[AUTO_PIN_CD] = nid;
4294 break;
4295 case AC_JACK_AUX:
4296 cfg->input_pins[AUTO_PIN_AUX] = nid;
4297 break;
4298 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004299 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004300 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4301 continue;
4302 cfg->dig_out_pins[cfg->dig_outs] = nid;
4303 cfg->dig_out_type[cfg->dig_outs] =
4304 (loc == AC_JACK_LOC_HDMI) ?
4305 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4306 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004307 break;
4308 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004309 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004310 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004311 if (loc == AC_JACK_LOC_HDMI)
4312 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4313 else
4314 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004315 break;
4316 }
4317 }
4318
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004319 /* FIX-UP:
4320 * If no line-out is defined but multiple HPs are found,
4321 * some of them might be the real line-outs.
4322 */
4323 if (!cfg->line_outs && cfg->hp_outs > 1) {
4324 int i = 0;
4325 while (i < cfg->hp_outs) {
4326 /* The real HPs should have the sequence 0x0f */
4327 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4328 i++;
4329 continue;
4330 }
4331 /* Move it to the line-out table */
4332 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4333 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4334 cfg->line_outs++;
4335 cfg->hp_outs--;
4336 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4337 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4338 memmove(sequences_hp + i - 1, sequences_hp + i,
4339 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4340 }
4341 }
4342
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004343 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004344 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4345 cfg->line_outs);
4346 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4347 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004348 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4349 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004350
Takashi Iwaif889fa92007-10-31 15:49:32 +01004351 /* if we have only one mic, make it AUTO_PIN_MIC */
4352 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4353 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4354 cfg->input_pins[AUTO_PIN_MIC] =
4355 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4356 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4357 }
4358 /* ditto for line-in */
4359 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4360 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4361 cfg->input_pins[AUTO_PIN_LINE] =
4362 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4363 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4364 }
4365
Steve Longerbeam81937d32007-05-08 15:33:03 +02004366 /*
4367 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4368 * as a primary output
4369 */
4370 if (!cfg->line_outs) {
4371 if (cfg->speaker_outs) {
4372 cfg->line_outs = cfg->speaker_outs;
4373 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4374 sizeof(cfg->speaker_pins));
4375 cfg->speaker_outs = 0;
4376 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4377 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4378 } else if (cfg->hp_outs) {
4379 cfg->line_outs = cfg->hp_outs;
4380 memcpy(cfg->line_out_pins, cfg->hp_pins,
4381 sizeof(cfg->hp_pins));
4382 cfg->hp_outs = 0;
4383 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4384 cfg->line_out_type = AUTO_PIN_HP_OUT;
4385 }
4386 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004387
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004388 /* Reorder the surround channels
4389 * ALSA sequence is front/surr/clfe/side
4390 * HDA sequence is:
4391 * 4-ch: front/surr => OK as it is
4392 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004393 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004394 */
4395 switch (cfg->line_outs) {
4396 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004397 case 4:
4398 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004399 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004400 cfg->line_out_pins[2] = nid;
4401 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004402 }
4403
Takashi Iwai82bc9552006-03-21 11:24:42 +01004404 /*
4405 * debug prints of the parsed results
4406 */
4407 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4408 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4409 cfg->line_out_pins[2], cfg->line_out_pins[3],
4410 cfg->line_out_pins[4]);
4411 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4412 cfg->speaker_outs, cfg->speaker_pins[0],
4413 cfg->speaker_pins[1], cfg->speaker_pins[2],
4414 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004415 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4416 cfg->hp_outs, cfg->hp_pins[0],
4417 cfg->hp_pins[1], cfg->hp_pins[2],
4418 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004419 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004420 if (cfg->dig_outs)
4421 snd_printd(" dig-out=0x%x/0x%x\n",
4422 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004423 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4424 " cd=0x%x, aux=0x%x\n",
4425 cfg->input_pins[AUTO_PIN_MIC],
4426 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4427 cfg->input_pins[AUTO_PIN_LINE],
4428 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4429 cfg->input_pins[AUTO_PIN_CD],
4430 cfg->input_pins[AUTO_PIN_AUX]);
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004431 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004432 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004433
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004434 return 0;
4435}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004436EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004437
Takashi Iwai4a471b72005-12-07 13:56:29 +01004438/* labels for input pins */
4439const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4440 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4441};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004442EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01004443
4444
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445#ifdef CONFIG_PM
4446/*
4447 * power management
4448 */
4449
4450/**
4451 * snd_hda_suspend - suspend the codecs
4452 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 *
4454 * Returns 0 if successful.
4455 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004456int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004458 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Takashi Iwai0ba21762007-04-16 11:29:14 +02004460 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004461#ifdef CONFIG_SND_HDA_POWER_SAVE
4462 if (!codec->power_on)
4463 continue;
4464#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004465 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 }
4467 return 0;
4468}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004469EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471/**
4472 * snd_hda_resume - resume the codecs
4473 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 *
4475 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004476 *
4477 * This fucntion is defined only when POWER_SAVE isn't set.
4478 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 */
4480int snd_hda_resume(struct hda_bus *bus)
4481{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004482 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Takashi Iwai0ba21762007-04-16 11:29:14 +02004484 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004485 if (snd_hda_codec_needs_resume(codec))
4486 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return 0;
4489}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004490EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004491#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004492
4493/*
4494 * generic arrays
4495 */
4496
Takashi Iwaid5191e52009-11-16 14:58:17 +01004497/**
4498 * snd_array_new - get a new element from the given array
4499 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004500 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004501 * Get a new element from the given array. If it exceeds the
4502 * pre-allocated array size, re-allocate the array.
4503 *
4504 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004505 */
4506void *snd_array_new(struct snd_array *array)
4507{
4508 if (array->used >= array->alloced) {
4509 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004510 void *nlist;
4511 if (snd_BUG_ON(num >= 4096))
4512 return NULL;
4513 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004514 if (!nlist)
4515 return NULL;
4516 if (array->list) {
4517 memcpy(nlist, array->list,
4518 array->elem_size * array->alloced);
4519 kfree(array->list);
4520 }
4521 array->list = nlist;
4522 array->alloced = num;
4523 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004524 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004525}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004526EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004527
Takashi Iwaid5191e52009-11-16 14:58:17 +01004528/**
4529 * snd_array_free - free the given array elements
4530 * @array: the array object
4531 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004532void snd_array_free(struct snd_array *array)
4533{
4534 kfree(array->list);
4535 array->used = 0;
4536 array->alloced = 0;
4537 array->list = NULL;
4538}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004539EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004540
Takashi Iwaid5191e52009-11-16 14:58:17 +01004541/**
4542 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4543 * @pcm: PCM caps bits
4544 * @buf: the string buffer to write
4545 * @buflen: the max buffer length
4546 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004547 * used by hda_proc.c and hda_eld.c
4548 */
4549void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4550{
4551 static unsigned int rates[] = {
4552 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4553 96000, 176400, 192000, 384000
4554 };
4555 int i, j;
4556
4557 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4558 if (pcm & (1 << i))
4559 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4560
4561 buf[j] = '\0'; /* necessary when j == 0 */
4562}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004563EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004564
Takashi Iwaid5191e52009-11-16 14:58:17 +01004565/**
4566 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4567 * @pcm: PCM caps bits
4568 * @buf: the string buffer to write
4569 * @buflen: the max buffer length
4570 *
4571 * used by hda_proc.c and hda_eld.c
4572 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004573void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4574{
4575 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4576 int i, j;
4577
4578 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4579 if (pcm & (AC_SUPPCM_BITS_8 << i))
4580 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4581
4582 buf[j] = '\0'; /* necessary when j == 0 */
4583}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004584EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004585
4586MODULE_DESCRIPTION("HDA codec core");
4587MODULE_LICENSE("GPL");