blob: d9d1c91dfd1b49c78ea7fb99d4dc4ab96e63f19b [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 Iwai5aacc212010-07-30 10:36:29 +0200399 snd_printk(KERN_ERR "hda_codec: "
400 "Too many connections %d for NID 0x%x\n",
401 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100402 return -EINVAL;
403 }
404 conn_list[conns++] = n;
405 }
406 } else {
407 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200408 snd_printk(KERN_ERR "hda_codec: "
409 "Too many connections %d for NID 0x%x\n",
410 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100411 return -EINVAL;
412 }
413 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100415 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 return conns;
418}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100419EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421
422/**
423 * snd_hda_queue_unsol_event - add an unsolicited event to queue
424 * @bus: the BUS
425 * @res: unsolicited event (lower 32bit of RIRB entry)
426 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
427 *
428 * Adds the given event to the queue. The events are processed in
429 * the workqueue asynchronously. Call this function in the interrupt
430 * hanlder when RIRB receives an unsolicited event.
431 *
432 * Returns 0 if successful, or a negative error code.
433 */
434int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
435{
436 struct hda_bus_unsolicited *unsol;
437 unsigned int wp;
438
Takashi Iwai0ba21762007-04-16 11:29:14 +0200439 unsol = bus->unsol;
440 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442
443 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
444 unsol->wp = wp;
445
446 wp <<= 1;
447 unsol->queue[wp] = res;
448 unsol->queue[wp + 1] = res_ex;
449
Takashi Iwai6acaed32009-01-12 10:09:24 +0100450 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 return 0;
453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100454EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800457 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
David Howellsc4028952006-11-22 14:57:56 +0000459static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
David Howellsc4028952006-11-22 14:57:56 +0000461 struct hda_bus_unsolicited *unsol =
462 container_of(work, struct hda_bus_unsolicited, work);
463 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct hda_codec *codec;
465 unsigned int rp, caddr, res;
466
467 while (unsol->rp != unsol->wp) {
468 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
469 unsol->rp = rp;
470 rp <<= 1;
471 res = unsol->queue[rp];
472 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200473 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 continue;
475 codec = bus->caddr_tbl[caddr & 0x0f];
476 if (codec && codec->patch_ops.unsol_event)
477 codec->patch_ops.unsol_event(codec, res);
478 }
479}
480
481/*
482 * initialize unsolicited queue
483 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200484static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct hda_bus_unsolicited *unsol;
487
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100488 if (bus->unsol) /* already initialized */
489 return 0;
490
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200491 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200492 if (!unsol) {
493 snd_printk(KERN_ERR "hda_codec: "
494 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -ENOMEM;
496 }
David Howellsc4028952006-11-22 14:57:56 +0000497 INIT_WORK(&unsol->work, process_unsol_events);
498 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 bus->unsol = unsol;
500 return 0;
501}
502
503/*
504 * destructor
505 */
506static void snd_hda_codec_free(struct hda_codec *codec);
507
508static int snd_hda_bus_free(struct hda_bus *bus)
509{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200510 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Takashi Iwai0ba21762007-04-16 11:29:14 +0200512 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100514 if (bus->workq)
515 flush_workqueue(bus->workq);
516 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200518 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 snd_hda_codec_free(codec);
520 }
521 if (bus->ops.private_free)
522 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100523 if (bus->workq)
524 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 kfree(bus);
526 return 0;
527}
528
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100529static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100532 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return snd_hda_bus_free(bus);
534}
535
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200536#ifdef CONFIG_SND_HDA_HWDEP
537static int snd_hda_bus_dev_register(struct snd_device *device)
538{
539 struct hda_bus *bus = device->device_data;
540 struct hda_codec *codec;
541 list_for_each_entry(codec, &bus->codec_list, list) {
542 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100543 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200544 }
545 return 0;
546}
547#else
548#define snd_hda_bus_dev_register NULL
549#endif
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
552 * snd_hda_bus_new - create a HDA bus
553 * @card: the card entry
554 * @temp: the template for hda_bus information
555 * @busp: the pointer to store the created bus instance
556 *
557 * Returns 0 if successful, or a negative error code.
558 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100559int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200560 const struct hda_bus_template *temp,
561 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct hda_bus *bus;
564 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100565 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200566 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 .dev_free = snd_hda_bus_dev_free,
568 };
569
Takashi Iwaida3cec32008-08-08 17:12:14 +0200570 if (snd_BUG_ON(!temp))
571 return -EINVAL;
572 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (busp)
576 *busp = NULL;
577
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200578 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (bus == NULL) {
580 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
581 return -ENOMEM;
582 }
583
584 bus->card = card;
585 bus->private_data = temp->private_data;
586 bus->pci = temp->pci;
587 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100588 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 bus->ops = temp->ops;
590
Ingo Molnar62932df2006-01-16 16:34:20 +0100591 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 INIT_LIST_HEAD(&bus->codec_list);
593
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100594 snprintf(bus->workq_name, sizeof(bus->workq_name),
595 "hd-audio%d", card->number);
596 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100597 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100598 snd_printk(KERN_ERR "cannot create workqueue %s\n",
599 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100600 kfree(bus);
601 return -ENOMEM;
602 }
603
Takashi Iwai0ba21762007-04-16 11:29:14 +0200604 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
605 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 snd_hda_bus_free(bus);
607 return err;
608 }
609 if (busp)
610 *busp = bus;
611 return 0;
612}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100613EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Takashi Iwai82467612007-07-27 19:15:54 +0200615#ifdef CONFIG_SND_HDA_GENERIC
616#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200617 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200618#else
619#define is_generic_config(codec) 0
620#endif
621
Takashi Iwai645f10c2008-11-28 15:07:37 +0100622#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100623#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
624#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100625#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100626#endif
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/*
629 * find a matching codec preset
630 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200631static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200632find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100634 struct hda_codec_preset_list *tbl;
635 const struct hda_codec_preset *preset;
636 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Takashi Iwai82467612007-07-27 19:15:54 +0200638 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100639 return NULL; /* use the generic parser */
640
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100641 again:
642 mutex_lock(&preset_mutex);
643 list_for_each_entry(tbl, &hda_preset_tables, list) {
644 if (!try_module_get(tbl->owner)) {
645 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
646 continue;
647 }
648 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100650 if (preset->afg && preset->afg != codec->afg)
651 continue;
652 if (preset->mfg && preset->mfg != codec->mfg)
653 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200654 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200656 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200657 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100658 preset->rev == codec->revision_id)) {
659 mutex_unlock(&preset_mutex);
660 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100664 module_put(tbl->owner);
665 }
666 mutex_unlock(&preset_mutex);
667
668 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
669 char name[32];
670 if (!mod_requested)
671 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
672 codec->vendor_id);
673 else
674 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
675 (codec->vendor_id >> 16) & 0xffff);
676 request_module(name);
677 mod_requested++;
678 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 return NULL;
681}
682
683/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200684 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200686static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 const struct hda_vendor_id *c;
689 const char *vendor = NULL;
690 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200691 char tmp[16];
692
693 if (codec->vendor_name)
694 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 for (c = hda_vendor_ids; c->id; c++) {
697 if (c->id == vendor_id) {
698 vendor = c->name;
699 break;
700 }
701 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200702 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 sprintf(tmp, "Generic %04x", vendor_id);
704 vendor = tmp;
705 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200706 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
707 if (!codec->vendor_name)
708 return -ENOMEM;
709
710 get_chip_name:
711 if (codec->chip_name)
712 return 0;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200715 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
716 else {
717 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
718 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
719 }
720 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200721 return -ENOMEM;
722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200726 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100728static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200730 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 hda_nid_t nid;
732
733 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
734 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200735 function_id = snd_hda_param_read(codec, nid,
Pascal de Bruijn234b4342009-03-23 11:15:59 +0100736 AC_PAR_FUNCTION_TYPE) & 0xff;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200737 switch (function_id) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200738 case AC_GRP_AUDIO_FUNCTION:
739 codec->afg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200740 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200741 break;
742 case AC_GRP_MODEM_FUNCTION:
743 codec->mfg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200744 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200745 break;
746 default:
747 break;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100753 * read widget caps for each widget and store in cache
754 */
755static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
756{
757 int i;
758 hda_nid_t nid;
759
760 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
761 &codec->start_nid);
762 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200763 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100764 return -ENOMEM;
765 nid = codec->start_nid;
766 for (i = 0; i < codec->num_nodes; i++, nid++)
767 codec->wcaps[i] = snd_hda_param_read(codec, nid,
768 AC_PAR_AUDIO_WIDGET_CAP);
769 return 0;
770}
771
Takashi Iwai3be14142009-02-20 14:11:16 +0100772/* read all pin default configurations and save codec->init_pins */
773static int read_pin_defaults(struct hda_codec *codec)
774{
775 int i;
776 hda_nid_t nid = codec->start_nid;
777
778 for (i = 0; i < codec->num_nodes; i++, nid++) {
779 struct hda_pincfg *pin;
780 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200781 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100782 if (wid_type != AC_WID_PIN)
783 continue;
784 pin = snd_array_new(&codec->init_pins);
785 if (!pin)
786 return -ENOMEM;
787 pin->nid = nid;
788 pin->cfg = snd_hda_codec_read(codec, nid, 0,
789 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200790 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
791 AC_VERB_GET_PIN_WIDGET_CONTROL,
792 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100793 }
794 return 0;
795}
796
797/* look up the given pin config list and return the item matching with NID */
798static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
799 struct snd_array *array,
800 hda_nid_t nid)
801{
802 int i;
803 for (i = 0; i < array->used; i++) {
804 struct hda_pincfg *pin = snd_array_elem(array, i);
805 if (pin->nid == nid)
806 return pin;
807 }
808 return NULL;
809}
810
811/* write a config value for the given NID */
812static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
813 unsigned int cfg)
814{
815 int i;
816 for (i = 0; i < 4; i++) {
817 snd_hda_codec_write(codec, nid, 0,
818 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
819 cfg & 0xff);
820 cfg >>= 8;
821 }
822}
823
824/* set the current pin config value for the given NID.
825 * the value is cached, and read via snd_hda_codec_get_pincfg()
826 */
827int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
828 hda_nid_t nid, unsigned int cfg)
829{
830 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100831 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100832
Takashi Iwaib82855a2009-12-27 11:24:56 +0100833 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
834 return -EINVAL;
835
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100836 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100837 pin = look_up_pincfg(codec, list, nid);
838 if (!pin) {
839 pin = snd_array_new(list);
840 if (!pin)
841 return -ENOMEM;
842 pin->nid = nid;
843 }
844 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100845
846 /* change only when needed; e.g. if the pincfg is already present
847 * in user_pins[], don't write it
848 */
849 cfg = snd_hda_codec_get_pincfg(codec, nid);
850 if (oldcfg != cfg)
851 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100852 return 0;
853}
854
Takashi Iwaid5191e52009-11-16 14:58:17 +0100855/**
856 * snd_hda_codec_set_pincfg - Override a pin default configuration
857 * @codec: the HDA codec
858 * @nid: NID to set the pin config
859 * @cfg: the pin default config value
860 *
861 * Override a pin default configuration value in the cache.
862 * This value can be read by snd_hda_codec_get_pincfg() in a higher
863 * priority than the real hardware value.
864 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100865int snd_hda_codec_set_pincfg(struct hda_codec *codec,
866 hda_nid_t nid, unsigned int cfg)
867{
Takashi Iwai346ff702009-02-23 09:42:57 +0100868 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100869}
870EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
871
Takashi Iwaid5191e52009-11-16 14:58:17 +0100872/**
873 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
874 * @codec: the HDA codec
875 * @nid: NID to get the pin config
876 *
877 * Get the current pin config value of the given pin NID.
878 * If the pincfg value is cached or overridden via sysfs or driver,
879 * returns the cached value.
880 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100881unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
882{
883 struct hda_pincfg *pin;
884
Takashi Iwai3be14142009-02-20 14:11:16 +0100885#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100886 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100887 if (pin)
888 return pin->cfg;
889#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100890 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
891 if (pin)
892 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100893 pin = look_up_pincfg(codec, &codec->init_pins, nid);
894 if (pin)
895 return pin->cfg;
896 return 0;
897}
898EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
899
900/* restore all current pin configs */
901static void restore_pincfgs(struct hda_codec *codec)
902{
903 int i;
904 for (i = 0; i < codec->init_pins.used; i++) {
905 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
906 set_pincfg(codec, pin->nid,
907 snd_hda_codec_get_pincfg(codec, pin->nid));
908 }
909}
Takashi Iwai54d17402005-11-21 16:33:22 +0100910
Takashi Iwai92ee6162009-12-27 11:18:59 +0100911/**
912 * snd_hda_shutup_pins - Shut up all pins
913 * @codec: the HDA codec
914 *
915 * Clear all pin controls to shup up before suspend for avoiding click noise.
916 * The controls aren't cached so that they can be resumed properly.
917 */
918void snd_hda_shutup_pins(struct hda_codec *codec)
919{
920 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200921 /* don't shut up pins when unloading the driver; otherwise it breaks
922 * the default pin setup at the next load of the driver
923 */
924 if (codec->bus->shutdown)
925 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100926 for (i = 0; i < codec->init_pins.used; i++) {
927 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
928 /* use read here for syncing after issuing each verb */
929 snd_hda_codec_read(codec, pin->nid, 0,
930 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
931 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200932 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100933}
934EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
935
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200936/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
937static void restore_shutup_pins(struct hda_codec *codec)
938{
939 int i;
940 if (!codec->pins_shutup)
941 return;
942 if (codec->bus->shutdown)
943 return;
944 for (i = 0; i < codec->init_pins.used; i++) {
945 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
946 snd_hda_codec_write(codec, pin->nid, 0,
947 AC_VERB_SET_PIN_WIDGET_CONTROL,
948 pin->ctrl);
949 }
950 codec->pins_shutup = 0;
951}
952
Takashi Iwai01751f52007-08-10 16:59:39 +0200953static void init_hda_cache(struct hda_cache_rec *cache,
954 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200955static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200956
Takashi Iwai3be14142009-02-20 14:11:16 +0100957/* restore the initial pin cfgs and release all pincfg lists */
958static void restore_init_pincfgs(struct hda_codec *codec)
959{
Takashi Iwai346ff702009-02-23 09:42:57 +0100960 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100961 * so that only the values in init_pins are restored
962 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100963 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100964#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100965 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100966#endif
967 restore_pincfgs(codec);
968 snd_array_free(&codec->init_pins);
969}
970
Takashi Iwai54d17402005-11-21 16:33:22 +0100971/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * codec destructor
973 */
974static void snd_hda_codec_free(struct hda_codec *codec)
975{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200976 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return;
Takashi Iwai3be14142009-02-20 14:11:16 +0100978 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200979#ifdef CONFIG_SND_HDA_POWER_SAVE
980 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100981 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200982#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200984 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100985 snd_array_free(&codec->nids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 codec->bus->caddr_tbl[codec->addr] = NULL;
987 if (codec->patch_ops.free)
988 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100989 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200990 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200991 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200992 kfree(codec->vendor_name);
993 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200994 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100995 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 kfree(codec);
997}
998
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100999static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1000 unsigned int power_state);
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002/**
1003 * snd_hda_codec_new - create a HDA codec
1004 * @bus: the bus to assign
1005 * @codec_addr: the codec address
1006 * @codecp: the pointer to store the generated codec
1007 *
1008 * Returns 0 if successful, or a negative error code.
1009 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001010int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1011 unsigned int codec_addr,
1012 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001015 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int err;
1017
Takashi Iwaida3cec32008-08-08 17:12:14 +02001018 if (snd_BUG_ON(!bus))
1019 return -EINVAL;
1020 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1021 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001024 snd_printk(KERN_ERR "hda_codec: "
1025 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EBUSY;
1027 }
1028
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001029 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (codec == NULL) {
1031 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1032 return -ENOMEM;
1033 }
1034
1035 codec->bus = bus;
1036 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001037 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001038 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001039 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001040 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001041 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1042 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001043 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001044 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001045 if (codec->bus->modelname) {
1046 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1047 if (!codec->modelname) {
1048 snd_hda_codec_free(codec);
1049 return -ENODEV;
1050 }
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Takashi Iwaicb53c622007-08-10 17:21:45 +02001053#ifdef CONFIG_SND_HDA_POWER_SAVE
1054 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1055 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1056 * the caller has to power down appropriatley after initialization
1057 * phase.
1058 */
1059 hda_keep_power_on(codec);
1060#endif
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 list_add_tail(&codec->list, &bus->codec_list);
1063 bus->caddr_tbl[codec_addr] = codec;
1064
Takashi Iwai0ba21762007-04-16 11:29:14 +02001065 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1066 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001067 if (codec->vendor_id == -1)
1068 /* read again, hopefully the access method was corrected
1069 * in the last read...
1070 */
1071 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1072 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001073 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1074 AC_PAR_SUBSYSTEM_ID);
1075 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1076 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001078 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001079 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001080 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001081 err = -ENODEV;
1082 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
Takashi Iwai3be14142009-02-20 14:11:16 +01001085 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1086 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001087 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001088 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001089 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001090 err = read_pin_defaults(codec);
1091 if (err < 0)
1092 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001093
Takashi Iwai0ba21762007-04-16 11:29:14 +02001094 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001095 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001096 codec->subsystem_id =
1097 snd_hda_codec_read(codec, nid, 0,
1098 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001099 }
1100
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001101 /* power-up all before initialization */
1102 hda_set_power_state(codec,
1103 codec->afg ? codec->afg : codec->mfg,
1104 AC_PWRST_D0);
1105
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001106 snd_hda_codec_proc_new(codec);
1107
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001108 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001109
1110 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1111 codec->subsystem_id, codec->revision_id);
1112 snd_component_add(codec->bus->card, component);
1113
1114 if (codecp)
1115 *codecp = codec;
1116 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001117
1118 error:
1119 snd_hda_codec_free(codec);
1120 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001121}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001122EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001123
Takashi Iwaid5191e52009-11-16 14:58:17 +01001124/**
1125 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1126 * @codec: the HDA codec
1127 *
1128 * Start parsing of the given codec tree and (re-)initialize the whole
1129 * patch instance.
1130 *
1131 * Returns 0 if successful or a negative error code.
1132 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001133int snd_hda_codec_configure(struct hda_codec *codec)
1134{
1135 int err;
1136
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001137 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001138 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001139 err = get_codec_name(codec);
1140 if (err < 0)
1141 return err;
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Takashi Iwai82467612007-07-27 19:15:54 +02001144 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001146 goto patched;
1147 }
Takashi Iwai82467612007-07-27 19:15:54 +02001148 if (codec->preset && codec->preset->patch) {
1149 err = codec->preset->patch(codec);
1150 goto patched;
1151 }
1152
1153 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001154 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001155 if (err < 0)
1156 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001157
1158 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001159 if (!err && codec->patch_ops.unsol_event)
1160 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001161 /* audio codec should override the mixer name */
1162 if (!err && (codec->afg || !*codec->bus->card->mixername))
1163 snprintf(codec->bus->card->mixername,
1164 sizeof(codec->bus->card->mixername),
1165 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001168EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170/**
1171 * snd_hda_codec_setup_stream - set up the codec for streaming
1172 * @codec: the CODEC to set up
1173 * @nid: the NID to set up
1174 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1175 * @channel_id: channel id to pass, zero based.
1176 * @format: stream format.
1177 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001178void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1179 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int channel_id, int format)
1181{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001182 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001183 return;
1184
Takashi Iwai0ba21762007-04-16 11:29:14 +02001185 snd_printdd("hda_codec_setup_stream: "
1186 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 nid, stream_tag, channel_id, format);
1188 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1189 (stream_tag << 4) | channel_id);
1190 msleep(1);
1191 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1192}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001193EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Takashi Iwaid5191e52009-11-16 14:58:17 +01001195/**
1196 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1197 * @codec: the CODEC to clean up
1198 * @nid: the NID to clean up
1199 */
Takashi Iwai888afa12008-03-18 09:57:50 +01001200void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1201{
1202 if (!nid)
1203 return;
1204
1205 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1206 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1207#if 0 /* keep the format */
1208 msleep(1);
1209 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1210#endif
1211}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001212EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214/*
1215 * amp access functions
1216 */
1217
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001218/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001219#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001220#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001221#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1222#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001224#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001227static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001228 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Takashi Iwai01751f52007-08-10 16:59:39 +02001230 memset(cache, 0, sizeof(*cache));
1231 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001232 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001233}
1234
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001235static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001236{
Takashi Iwai603c4012008-07-30 15:01:44 +02001237 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001241static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Takashi Iwai01751f52007-08-10 16:59:39 +02001243 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1244 u16 cur = cache->hash[idx];
1245 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001248 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (info->key == key)
1250 return info;
1251 cur = info->next;
1252 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001253 return NULL;
1254}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001256/* query the hash. allocate an entry if not found. */
1257static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1258 u32 key)
1259{
1260 struct hda_cache_head *info = get_hash(cache, key);
1261 if (!info) {
1262 u16 idx, cur;
1263 /* add a new hash entry */
1264 info = snd_array_new(&cache->buf);
1265 if (!info)
1266 return NULL;
1267 cur = snd_array_index(&cache->buf, info);
1268 info->key = key;
1269 info->val = 0;
1270 idx = key % (u16)ARRAY_SIZE(cache->hash);
1271 info->next = cache->hash[idx];
1272 cache->hash[idx] = cur;
1273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return info;
1275}
1276
Takashi Iwai01751f52007-08-10 16:59:39 +02001277/* query and allocate an amp hash entry */
1278static inline struct hda_amp_info *
1279get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1280{
1281 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1282}
1283
Takashi Iwaid5191e52009-11-16 14:58:17 +01001284/**
1285 * query_amp_caps - query AMP capabilities
1286 * @codec: the HD-auio codec
1287 * @nid: the NID to query
1288 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1289 *
1290 * Query AMP capabilities for the given widget and direction.
1291 * Returns the obtained capability bits.
1292 *
1293 * When cap bits have been already read, this doesn't read again but
1294 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001296u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001298 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Takashi Iwai0ba21762007-04-16 11:29:14 +02001300 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1301 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001303 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001304 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001306 info->amp_caps = snd_hda_param_read(codec, nid,
1307 direction == HDA_OUTPUT ?
1308 AC_PAR_AMP_OUT_CAP :
1309 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001310 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001311 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313 return info->amp_caps;
1314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001315EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Takashi Iwaid5191e52009-11-16 14:58:17 +01001317/**
1318 * snd_hda_override_amp_caps - Override the AMP capabilities
1319 * @codec: the CODEC to clean up
1320 * @nid: the NID to clean up
1321 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1322 * @caps: the capability bits to set
1323 *
1324 * Override the cached AMP caps bits value by the given one.
1325 * This function is useful if the driver needs to adjust the AMP ranges,
1326 * e.g. limit to 0dB, etc.
1327 *
1328 * Returns zero if successful or a negative error code.
1329 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001330int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1331 unsigned int caps)
1332{
1333 struct hda_amp_info *info;
1334
1335 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1336 if (!info)
1337 return -EINVAL;
1338 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001339 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001340 return 0;
1341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001342EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001343
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001344static unsigned int
1345query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1346 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001347{
1348 struct hda_amp_info *info;
1349
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001350 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001351 if (!info)
1352 return 0;
1353 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001354 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001355 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001356 }
1357 return info->amp_caps;
1358}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001359
1360static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1361{
1362 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1363}
1364
Takashi Iwaid5191e52009-11-16 14:58:17 +01001365/**
1366 * snd_hda_query_pin_caps - Query PIN capabilities
1367 * @codec: the HD-auio codec
1368 * @nid: the NID to query
1369 *
1370 * Query PIN capabilities for the given widget.
1371 * Returns the obtained capability bits.
1372 *
1373 * When cap bits have been already read, this doesn't read again but
1374 * returns the cached value.
1375 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001376u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1377{
1378 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1379 read_pin_cap);
1380}
Takashi Iwai1327a322009-03-23 13:07:47 +01001381EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1382
Wu Fengguang864f92b2009-11-18 12:38:02 +08001383/**
1384 * snd_hda_pin_sense - execute pin sense measurement
1385 * @codec: the CODEC to sense
1386 * @nid: the pin NID to sense
1387 *
1388 * Execute necessary pin sense measurement and return its Presence Detect,
1389 * Impedance, ELD Valid etc. status bits.
1390 */
1391u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1392{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001393 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001394
Takashi Iwai729d55b2009-12-25 22:49:01 +01001395 if (!codec->no_trigger_sense) {
1396 pincap = snd_hda_query_pin_caps(codec, nid);
1397 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001398 snd_hda_codec_read(codec, nid, 0,
1399 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001400 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001401 return snd_hda_codec_read(codec, nid, 0,
1402 AC_VERB_GET_PIN_SENSE, 0);
1403}
1404EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1405
1406/**
1407 * snd_hda_jack_detect - query pin Presence Detect status
1408 * @codec: the CODEC to sense
1409 * @nid: the pin NID to sense
1410 *
1411 * Query and return the pin's Presence Detect status.
1412 */
1413int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1414{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001415 u32 sense = snd_hda_pin_sense(codec, nid);
1416 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001417}
1418EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420/*
1421 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001422 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001424static unsigned int get_vol_mute(struct hda_codec *codec,
1425 struct hda_amp_info *info, hda_nid_t nid,
1426 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 u32 val, parm;
1429
Takashi Iwai01751f52007-08-10 16:59:39 +02001430 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001431 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1434 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1435 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001436 val = snd_hda_codec_read(codec, nid, 0,
1437 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001439 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001440 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001444 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001446static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001447 hda_nid_t nid, int ch, int direction, int index,
1448 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 u32 parm;
1451
1452 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1453 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1454 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1455 parm |= val;
1456 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001457 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Takashi Iwaid5191e52009-11-16 14:58:17 +01001460/**
1461 * snd_hda_codec_amp_read - Read AMP value
1462 * @codec: HD-audio codec
1463 * @nid: NID to read the AMP value
1464 * @ch: channel (left=0 or right=1)
1465 * @direction: #HDA_INPUT or #HDA_OUTPUT
1466 * @index: the index value (only for input direction)
1467 *
1468 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 */
Takashi Iwai834be882006-03-01 14:16:17 +01001470int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1471 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001473 struct hda_amp_info *info;
1474 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1475 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001477 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001479EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Takashi Iwaid5191e52009-11-16 14:58:17 +01001481/**
1482 * snd_hda_codec_amp_update - update the AMP value
1483 * @codec: HD-audio codec
1484 * @nid: NID to read the AMP value
1485 * @ch: channel (left=0 or right=1)
1486 * @direction: #HDA_INPUT or #HDA_OUTPUT
1487 * @idx: the index value (only for input direction)
1488 * @mask: bit mask to set
1489 * @val: the bits value to set
1490 *
1491 * Update the AMP value with a bit mask.
1492 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001493 */
Takashi Iwai834be882006-03-01 14:16:17 +01001494int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1495 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001497 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001498
Takashi Iwai0ba21762007-04-16 11:29:14 +02001499 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1500 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001502 if (snd_BUG_ON(mask & ~0xff))
1503 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001504 val &= mask;
1505 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001506 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001508 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 1;
1510}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001511EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Takashi Iwaid5191e52009-11-16 14:58:17 +01001513/**
1514 * snd_hda_codec_amp_stereo - update the AMP stereo values
1515 * @codec: HD-audio codec
1516 * @nid: NID to read the AMP value
1517 * @direction: #HDA_INPUT or #HDA_OUTPUT
1518 * @idx: the index value (only for input direction)
1519 * @mask: bit mask to set
1520 * @val: the bits value to set
1521 *
1522 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1523 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001524 */
1525int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1526 int direction, int idx, int mask, int val)
1527{
1528 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001529
1530 if (snd_BUG_ON(mask & ~0xff))
1531 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001532 for (ch = 0; ch < 2; ch++)
1533 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1534 idx, mask, val);
1535 return ret;
1536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001537EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001538
Takashi Iwaicb53c622007-08-10 17:21:45 +02001539#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001540/**
1541 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1542 * @codec: HD-audio codec
1543 *
1544 * Resume the all amp commands from the cache.
1545 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001546void snd_hda_codec_resume_amp(struct hda_codec *codec)
1547{
Takashi Iwai603c4012008-07-30 15:01:44 +02001548 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001549 int i;
1550
Takashi Iwai603c4012008-07-30 15:01:44 +02001551 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001552 u32 key = buffer->head.key;
1553 hda_nid_t nid;
1554 unsigned int idx, dir, ch;
1555 if (!key)
1556 continue;
1557 nid = key & 0xff;
1558 idx = (key >> 16) & 0xff;
1559 dir = (key >> 24) & 0xff;
1560 for (ch = 0; ch < 2; ch++) {
1561 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1562 continue;
1563 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1564 buffer->vol[ch]);
1565 }
1566 }
1567}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001568EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001569#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001571static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1572 unsigned int ofs)
1573{
1574 u32 caps = query_amp_caps(codec, nid, dir);
1575 /* get num steps */
1576 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1577 if (ofs < caps)
1578 caps -= ofs;
1579 return caps;
1580}
1581
Takashi Iwaid5191e52009-11-16 14:58:17 +01001582/**
1583 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1584 *
1585 * The control element is supposed to have the private_value field
1586 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1587 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001588int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1592 u16 nid = get_amp_nid(kcontrol);
1593 u8 chs = get_amp_channels(kcontrol);
1594 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001595 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001597 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1598 uinfo->count = chs == 3 ? 2 : 1;
1599 uinfo->value.integer.min = 0;
1600 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1601 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001602 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001603 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1604 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return -EINVAL;
1606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return 0;
1608}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001609EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001611
1612static inline unsigned int
1613read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1614 int ch, int dir, int idx, unsigned int ofs)
1615{
1616 unsigned int val;
1617 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1618 val &= HDA_AMP_VOLMASK;
1619 if (val >= ofs)
1620 val -= ofs;
1621 else
1622 val = 0;
1623 return val;
1624}
1625
1626static inline int
1627update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1628 int ch, int dir, int idx, unsigned int ofs,
1629 unsigned int val)
1630{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001631 unsigned int maxval;
1632
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001633 if (val > 0)
1634 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001635 /* ofs = 0: raw max value */
1636 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001637 if (val > maxval)
1638 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001639 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1640 HDA_AMP_VOLMASK, val);
1641}
1642
Takashi Iwaid5191e52009-11-16 14:58:17 +01001643/**
1644 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1645 *
1646 * The control element is supposed to have the private_value field
1647 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1648 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001649int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1650 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1653 hda_nid_t nid = get_amp_nid(kcontrol);
1654 int chs = get_amp_channels(kcontrol);
1655 int dir = get_amp_direction(kcontrol);
1656 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001657 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 long *valp = ucontrol->value.integer.value;
1659
1660 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001661 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001663 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return 0;
1665}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001666EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Takashi Iwaid5191e52009-11-16 14:58:17 +01001668/**
1669 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1670 *
1671 * The control element is supposed to have the private_value field
1672 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1673 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001674int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1675 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
1677 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1678 hda_nid_t nid = get_amp_nid(kcontrol);
1679 int chs = get_amp_channels(kcontrol);
1680 int dir = get_amp_direction(kcontrol);
1681 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001682 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 long *valp = ucontrol->value.integer.value;
1684 int change = 0;
1685
Takashi Iwaicb53c622007-08-10 17:21:45 +02001686 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001687 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001688 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001689 valp++;
1690 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001691 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001692 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001693 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return change;
1695}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001696EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Takashi Iwaid5191e52009-11-16 14:58:17 +01001698/**
1699 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1700 *
1701 * The control element is supposed to have the private_value field
1702 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1703 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001704int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1705 unsigned int size, unsigned int __user *_tlv)
1706{
1707 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1708 hda_nid_t nid = get_amp_nid(kcontrol);
1709 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001710 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001711 u32 caps, val1, val2;
1712
1713 if (size < 4 * sizeof(unsigned int))
1714 return -ENOMEM;
1715 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001716 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1717 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001718 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001719 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001720 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001721 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1722 return -EFAULT;
1723 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1724 return -EFAULT;
1725 if (put_user(val1, _tlv + 2))
1726 return -EFAULT;
1727 if (put_user(val2, _tlv + 3))
1728 return -EFAULT;
1729 return 0;
1730}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001731EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001732
Takashi Iwaid5191e52009-11-16 14:58:17 +01001733/**
1734 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1735 * @codec: HD-audio codec
1736 * @nid: NID of a reference widget
1737 * @dir: #HDA_INPUT or #HDA_OUTPUT
1738 * @tlv: TLV data to be stored, at least 4 elements
1739 *
1740 * Set (static) TLV data for a virtual master volume using the AMP caps
1741 * obtained from the reference NID.
1742 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001743 */
1744void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1745 unsigned int *tlv)
1746{
1747 u32 caps;
1748 int nums, step;
1749
1750 caps = query_amp_caps(codec, nid, dir);
1751 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1752 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1753 step = (step + 1) * 25;
1754 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1755 tlv[1] = 2 * sizeof(unsigned int);
1756 tlv[2] = -nums * step;
1757 tlv[3] = step;
1758}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001759EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001760
1761/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001762static struct snd_kcontrol *
1763_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1764 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001765{
1766 struct snd_ctl_elem_id id;
1767 memset(&id, 0, sizeof(id));
1768 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001769 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001770 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1771 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001772 strcpy(id.name, name);
1773 return snd_ctl_find_id(codec->bus->card, &id);
1774}
1775
Takashi Iwaid5191e52009-11-16 14:58:17 +01001776/**
1777 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1778 * @codec: HD-audio codec
1779 * @name: ctl id name string
1780 *
1781 * Get the control element with the given id string and IFACE_MIXER.
1782 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001783struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1784 const char *name)
1785{
1786 return _snd_hda_find_mixer_ctl(codec, name, 0);
1787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001788EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001789
Takashi Iwaid5191e52009-11-16 14:58:17 +01001790/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001791 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001792 * @codec: HD-audio codec
1793 * @nid: corresponding NID (optional)
1794 * @kctl: the control element to assign
1795 *
1796 * Add the given control element to an array inside the codec instance.
1797 * All control elements belonging to a codec are supposed to be added
1798 * by this function so that a proper clean-up works at the free or
1799 * reconfiguration time.
1800 *
1801 * If non-zero @nid is passed, the NID is assigned to the control element.
1802 * The assignment is shown in the codec proc file.
1803 *
1804 * snd_hda_ctl_add() checks the control subdev id field whether
1805 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001806 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1807 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001808 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001809int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1810 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001811{
1812 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001813 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001814 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001815
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001816 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001817 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001818 if (nid == 0)
1819 nid = get_amp_nid_(kctl->private_value);
1820 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001821 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1822 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001823 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001824 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001825 err = snd_ctl_add(codec->bus->card, kctl);
1826 if (err < 0)
1827 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001828 item = snd_array_new(&codec->mixers);
1829 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001830 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001831 item->kctl = kctl;
1832 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001833 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001834 return 0;
1835}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001836EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001837
Takashi Iwaid5191e52009-11-16 14:58:17 +01001838/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001839 * snd_hda_add_nid - Assign a NID to a control element
1840 * @codec: HD-audio codec
1841 * @nid: corresponding NID (optional)
1842 * @kctl: the control element to assign
1843 * @index: index to kctl
1844 *
1845 * Add the given control element to an array inside the codec instance.
1846 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1847 * NID:KCTL mapping - for example "Capture Source" selector.
1848 */
1849int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1850 unsigned int index, hda_nid_t nid)
1851{
1852 struct hda_nid_item *item;
1853
1854 if (nid > 0) {
1855 item = snd_array_new(&codec->nids);
1856 if (!item)
1857 return -ENOMEM;
1858 item->kctl = kctl;
1859 item->index = index;
1860 item->nid = nid;
1861 return 0;
1862 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01001863 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1864 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001865 return -EINVAL;
1866}
1867EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1868
1869/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001870 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1871 * @codec: HD-audio codec
1872 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001873void snd_hda_ctls_clear(struct hda_codec *codec)
1874{
1875 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001876 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001877 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001878 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001879 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001880 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001881}
1882
Takashi Iwaia65d6292009-02-23 16:57:04 +01001883/* pseudo device locking
1884 * toggle card->shutdown to allow/disallow the device access (as a hack)
1885 */
1886static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001887{
Takashi Iwaia65d6292009-02-23 16:57:04 +01001888 spin_lock(&card->files_lock);
1889 if (card->shutdown) {
1890 spin_unlock(&card->files_lock);
1891 return -EINVAL;
1892 }
1893 card->shutdown = 1;
1894 spin_unlock(&card->files_lock);
1895 return 0;
1896}
1897
1898static void hda_unlock_devices(struct snd_card *card)
1899{
1900 spin_lock(&card->files_lock);
1901 card->shutdown = 0;
1902 spin_unlock(&card->files_lock);
1903}
1904
Takashi Iwaid5191e52009-11-16 14:58:17 +01001905/**
1906 * snd_hda_codec_reset - Clear all objects assigned to the codec
1907 * @codec: HD-audio codec
1908 *
1909 * This frees the all PCM and control elements assigned to the codec, and
1910 * clears the caches and restores the pin default configurations.
1911 *
1912 * When a device is being used, it returns -EBSY. If successfully freed,
1913 * returns zero.
1914 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001915int snd_hda_codec_reset(struct hda_codec *codec)
1916{
1917 struct snd_card *card = codec->bus->card;
1918 int i, pcm;
1919
1920 if (hda_lock_devices(card) < 0)
1921 return -EBUSY;
1922 /* check whether the codec isn't used by any mixer or PCM streams */
1923 if (!list_empty(&card->ctl_files)) {
1924 hda_unlock_devices(card);
1925 return -EBUSY;
1926 }
1927 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1928 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1929 if (!cpcm->pcm)
1930 continue;
1931 if (cpcm->pcm->streams[0].substream_opened ||
1932 cpcm->pcm->streams[1].substream_opened) {
1933 hda_unlock_devices(card);
1934 return -EBUSY;
1935 }
1936 }
1937
1938 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001939
1940#ifdef CONFIG_SND_HDA_POWER_SAVE
1941 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001942 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001943#endif
1944 snd_hda_ctls_clear(codec);
1945 /* relase PCMs */
1946 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001947 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01001948 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001949 clear_bit(codec->pcm_info[i].device,
1950 codec->bus->pcm_dev_bits);
1951 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001952 }
1953 if (codec->patch_ops.free)
1954 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001955 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001956 codec->spec = NULL;
1957 free_hda_cache(&codec->amp_cache);
1958 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001959 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1960 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01001961 /* free only driver_pins so that init_pins + user_pins are restored */
1962 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001963 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001964 codec->num_pcms = 0;
1965 codec->pcm_info = NULL;
1966 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01001967 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1968 codec->slave_dig_outs = NULL;
1969 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001970 module_put(codec->owner);
1971 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001972
1973 /* allow device access again */
1974 hda_unlock_devices(card);
1975 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001976}
1977
Takashi Iwaid5191e52009-11-16 14:58:17 +01001978/**
1979 * snd_hda_add_vmaster - create a virtual master control and add slaves
1980 * @codec: HD-audio codec
1981 * @name: vmaster control name
1982 * @tlv: TLV data (optional)
1983 * @slaves: slave control names (optional)
1984 *
1985 * Create a virtual master control with the given name. The TLV data
1986 * must be either NULL or a valid data.
1987 *
1988 * @slaves is a NULL-terminated array of strings, each of which is a
1989 * slave control name. All controls with these names are assigned to
1990 * the new virtual master control.
1991 *
1992 * This function returns zero if successful or a negative error code.
1993 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01001994int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1995 unsigned int *tlv, const char **slaves)
1996{
1997 struct snd_kcontrol *kctl;
1998 const char **s;
1999 int err;
2000
Takashi Iwai2f085542008-02-22 18:43:50 +01002001 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2002 ;
2003 if (!*s) {
2004 snd_printdd("No slave found for %s\n", name);
2005 return 0;
2006 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002007 kctl = snd_ctl_make_virtual_master(name, tlv);
2008 if (!kctl)
2009 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002010 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002011 if (err < 0)
2012 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002013
Takashi Iwai2134ea42008-01-10 16:53:55 +01002014 for (s = slaves; *s; s++) {
2015 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002016 int i = 0;
2017 for (;;) {
2018 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2019 if (!sctl) {
2020 if (!i)
2021 snd_printdd("Cannot find slave %s, "
2022 "skipped\n", *s);
2023 break;
2024 }
2025 err = snd_ctl_add_slave(kctl, sctl);
2026 if (err < 0)
2027 return err;
2028 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002029 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002030 }
2031 return 0;
2032}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002033EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002034
Takashi Iwaid5191e52009-11-16 14:58:17 +01002035/**
2036 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2037 *
2038 * The control element is supposed to have the private_value field
2039 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2040 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002041int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2042 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 int chs = get_amp_channels(kcontrol);
2045
2046 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2047 uinfo->count = chs == 3 ? 2 : 1;
2048 uinfo->value.integer.min = 0;
2049 uinfo->value.integer.max = 1;
2050 return 0;
2051}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002052EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Takashi Iwaid5191e52009-11-16 14:58:17 +01002054/**
2055 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2056 *
2057 * The control element is supposed to have the private_value field
2058 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2059 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002060int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2061 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2064 hda_nid_t nid = get_amp_nid(kcontrol);
2065 int chs = get_amp_channels(kcontrol);
2066 int dir = get_amp_direction(kcontrol);
2067 int idx = get_amp_index(kcontrol);
2068 long *valp = ucontrol->value.integer.value;
2069
2070 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002071 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002072 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002074 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002075 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
2077}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002078EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Takashi Iwaid5191e52009-11-16 14:58:17 +01002080/**
2081 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2082 *
2083 * The control element is supposed to have the private_value field
2084 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2085 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002086int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2087 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2090 hda_nid_t nid = get_amp_nid(kcontrol);
2091 int chs = get_amp_channels(kcontrol);
2092 int dir = get_amp_direction(kcontrol);
2093 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 long *valp = ucontrol->value.integer.value;
2095 int change = 0;
2096
Takashi Iwaicb53c622007-08-10 17:21:45 +02002097 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002098 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002099 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002100 HDA_AMP_MUTE,
2101 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002102 valp++;
2103 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002104 if (chs & 2)
2105 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002106 HDA_AMP_MUTE,
2107 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002108#ifdef CONFIG_SND_HDA_POWER_SAVE
2109 if (codec->patch_ops.check_power_status)
2110 codec->patch_ops.check_power_status(codec, nid);
2111#endif
2112 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return change;
2114}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002115EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Takashi Iwai67d634c2009-11-16 15:35:59 +01002117#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002118/**
2119 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2120 *
2121 * This function calls snd_hda_enable_beep_device(), which behaves differently
2122 * depending on beep_mode option.
2123 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002124int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2125 struct snd_ctl_elem_value *ucontrol)
2126{
2127 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2128 long *valp = ucontrol->value.integer.value;
2129
2130 snd_hda_enable_beep_device(codec, *valp);
2131 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2132}
2133EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002134#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136/*
Takashi Iwai985be542005-11-02 18:26:49 +01002137 * bound volume controls
2138 *
2139 * bind multiple volumes (# indices, from 0)
2140 */
2141
2142#define AMP_VAL_IDX_SHIFT 19
2143#define AMP_VAL_IDX_MASK (0x0f<<19)
2144
Takashi Iwaid5191e52009-11-16 14:58:17 +01002145/**
2146 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2147 *
2148 * The control element is supposed to have the private_value field
2149 * set up via HDA_BIND_MUTE*() macros.
2150 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002151int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002153{
2154 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2155 unsigned long pval;
2156 int err;
2157
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002158 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002159 pval = kcontrol->private_value;
2160 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2161 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2162 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002163 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002164 return err;
2165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002166EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002167
Takashi Iwaid5191e52009-11-16 14:58:17 +01002168/**
2169 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2170 *
2171 * The control element is supposed to have the private_value field
2172 * set up via HDA_BIND_MUTE*() macros.
2173 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002174int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002176{
2177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2178 unsigned long pval;
2179 int i, indices, err = 0, change = 0;
2180
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002181 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002182 pval = kcontrol->private_value;
2183 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2184 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002185 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2186 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002187 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2188 if (err < 0)
2189 break;
2190 change |= err;
2191 }
2192 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002193 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002194 return err < 0 ? err : change;
2195}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002196EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002197
Takashi Iwaid5191e52009-11-16 14:58:17 +01002198/**
2199 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2200 *
2201 * The control element is supposed to have the private_value field
2202 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002203 */
2204int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2205 struct snd_ctl_elem_info *uinfo)
2206{
2207 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2208 struct hda_bind_ctls *c;
2209 int err;
2210
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002211 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002212 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002213 kcontrol->private_value = *c->values;
2214 err = c->ops->info(kcontrol, uinfo);
2215 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002216 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002217 return err;
2218}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002219EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002220
Takashi Iwaid5191e52009-11-16 14:58:17 +01002221/**
2222 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2223 *
2224 * The control element is supposed to have the private_value field
2225 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2226 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002227int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2229{
2230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2231 struct hda_bind_ctls *c;
2232 int err;
2233
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002234 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002235 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002236 kcontrol->private_value = *c->values;
2237 err = c->ops->get(kcontrol, ucontrol);
2238 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002239 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002240 return err;
2241}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002242EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002243
Takashi Iwaid5191e52009-11-16 14:58:17 +01002244/**
2245 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2246 *
2247 * The control element is supposed to have the private_value field
2248 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2249 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002250int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2251 struct snd_ctl_elem_value *ucontrol)
2252{
2253 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2254 struct hda_bind_ctls *c;
2255 unsigned long *vals;
2256 int err = 0, change = 0;
2257
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002258 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002259 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002260 for (vals = c->values; *vals; vals++) {
2261 kcontrol->private_value = *vals;
2262 err = c->ops->put(kcontrol, ucontrol);
2263 if (err < 0)
2264 break;
2265 change |= err;
2266 }
2267 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002268 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002269 return err < 0 ? err : change;
2270}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002271EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002272
Takashi Iwaid5191e52009-11-16 14:58:17 +01002273/**
2274 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2275 *
2276 * The control element is supposed to have the private_value field
2277 * set up via HDA_BIND_VOL() macro.
2278 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002279int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2280 unsigned int size, unsigned int __user *tlv)
2281{
2282 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2283 struct hda_bind_ctls *c;
2284 int err;
2285
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002286 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002287 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002288 kcontrol->private_value = *c->values;
2289 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2290 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002291 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002292 return err;
2293}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002294EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002295
2296struct hda_ctl_ops snd_hda_bind_vol = {
2297 .info = snd_hda_mixer_amp_volume_info,
2298 .get = snd_hda_mixer_amp_volume_get,
2299 .put = snd_hda_mixer_amp_volume_put,
2300 .tlv = snd_hda_mixer_amp_tlv
2301};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002302EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002303
2304struct hda_ctl_ops snd_hda_bind_sw = {
2305 .info = snd_hda_mixer_amp_switch_info,
2306 .get = snd_hda_mixer_amp_switch_get,
2307 .put = snd_hda_mixer_amp_switch_put,
2308 .tlv = snd_hda_mixer_amp_tlv
2309};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002310EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002311
2312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 * SPDIF out controls
2314 */
2315
Takashi Iwai0ba21762007-04-16 11:29:14 +02002316static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2320 uinfo->count = 1;
2321 return 0;
2322}
2323
Takashi Iwai0ba21762007-04-16 11:29:14 +02002324static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
2327 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2328 IEC958_AES0_NONAUDIO |
2329 IEC958_AES0_CON_EMPHASIS_5015 |
2330 IEC958_AES0_CON_NOT_COPYRIGHT;
2331 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2332 IEC958_AES1_CON_ORIGINAL;
2333 return 0;
2334}
2335
Takashi Iwai0ba21762007-04-16 11:29:14 +02002336static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2337 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2340 IEC958_AES0_NONAUDIO |
2341 IEC958_AES0_PRO_EMPHASIS_5015;
2342 return 0;
2343}
2344
Takashi Iwai0ba21762007-04-16 11:29:14 +02002345static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2346 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
2348 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2349
2350 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2351 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2352 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2353 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2354
2355 return 0;
2356}
2357
2358/* convert from SPDIF status bits to HDA SPDIF bits
2359 * bit 0 (DigEn) is always set zero (to be filled later)
2360 */
2361static unsigned short convert_from_spdif_status(unsigned int sbits)
2362{
2363 unsigned short val = 0;
2364
2365 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002366 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002368 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002370 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2371 IEC958_AES0_PRO_EMPHASIS_5015)
2372 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002374 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2375 IEC958_AES0_CON_EMPHASIS_5015)
2376 val |= AC_DIG1_EMPHASIS;
2377 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2378 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002380 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2382 }
2383 return val;
2384}
2385
2386/* convert to SPDIF status bits from HDA SPDIF bits
2387 */
2388static unsigned int convert_to_spdif_status(unsigned short val)
2389{
2390 unsigned int sbits = 0;
2391
Takashi Iwai0ba21762007-04-16 11:29:14 +02002392 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002394 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 sbits |= IEC958_AES0_PROFESSIONAL;
2396 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002397 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2399 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002400 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002402 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002404 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2406 sbits |= val & (0x7f << 8);
2407 }
2408 return sbits;
2409}
2410
Takashi Iwai2f728532008-09-25 16:32:41 +02002411/* set digital convert verbs both for the given NID and its slaves */
2412static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2413 int verb, int val)
2414{
2415 hda_nid_t *d;
2416
Takashi Iwai9e976972008-11-25 08:17:20 +01002417 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002418 d = codec->slave_dig_outs;
2419 if (!d)
2420 return;
2421 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002422 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002423}
2424
2425static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2426 int dig1, int dig2)
2427{
2428 if (dig1 != -1)
2429 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2430 if (dig2 != -1)
2431 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2432}
2433
Takashi Iwai0ba21762007-04-16 11:29:14 +02002434static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2438 hda_nid_t nid = kcontrol->private_value;
2439 unsigned short val;
2440 int change;
2441
Ingo Molnar62932df2006-01-16 16:34:20 +01002442 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 codec->spdif_status = ucontrol->value.iec958.status[0] |
2444 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2445 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2446 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2447 val = convert_from_spdif_status(codec->spdif_status);
2448 val |= codec->spdif_ctls & 1;
2449 change = codec->spdif_ctls != val;
2450 codec->spdif_ctls = val;
2451
Takashi Iwai2f728532008-09-25 16:32:41 +02002452 if (change)
2453 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Ingo Molnar62932df2006-01-16 16:34:20 +01002455 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return change;
2457}
2458
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002459#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Takashi Iwai0ba21762007-04-16 11:29:14 +02002461static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2465
Takashi Iwai0ba21762007-04-16 11:29:14 +02002466 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return 0;
2468}
2469
Takashi Iwai0ba21762007-04-16 11:29:14 +02002470static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2471 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
2473 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2474 hda_nid_t nid = kcontrol->private_value;
2475 unsigned short val;
2476 int change;
2477
Ingo Molnar62932df2006-01-16 16:34:20 +01002478 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002479 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002481 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002483 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002485 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002486 /* unmute amp switch (if any) */
2487 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002488 (val & AC_DIG1_ENABLE))
2489 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2490 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002492 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return change;
2494}
2495
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002496static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 {
2498 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2499 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002500 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 .info = snd_hda_spdif_mask_info,
2502 .get = snd_hda_spdif_cmask_get,
2503 },
2504 {
2505 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002507 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 .info = snd_hda_spdif_mask_info,
2509 .get = snd_hda_spdif_pmask_get,
2510 },
2511 {
2512 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002513 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 .info = snd_hda_spdif_mask_info,
2515 .get = snd_hda_spdif_default_get,
2516 .put = snd_hda_spdif_default_put,
2517 },
2518 {
2519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002520 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 .info = snd_hda_spdif_out_switch_info,
2522 .get = snd_hda_spdif_out_switch_get,
2523 .put = snd_hda_spdif_out_switch_put,
2524 },
2525 { } /* end */
2526};
2527
Takashi Iwai09f99702008-02-04 12:31:13 +01002528#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530/**
2531 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2532 * @codec: the HDA codec
2533 * @nid: audio out widget NID
2534 *
2535 * Creates controls related with the SPDIF output.
2536 * Called from each patch supporting the SPDIF out.
2537 *
2538 * Returns 0 if successful, or a negative error code.
2539 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002540int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002543 struct snd_kcontrol *kctl;
2544 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002545 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Takashi Iwai09f99702008-02-04 12:31:13 +01002547 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2548 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2549 idx))
2550 break;
2551 }
2552 if (idx >= SPDIF_MAX_IDX) {
2553 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2554 return -EBUSY;
2555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2557 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002558 if (!kctl)
2559 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002560 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002562 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002563 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 return err;
2565 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002566 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002567 snd_hda_codec_read(codec, nid, 0,
2568 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2570 return 0;
2571}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002572EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002575 * SPDIF sharing with analog output
2576 */
2577static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2578 struct snd_ctl_elem_value *ucontrol)
2579{
2580 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2581 ucontrol->value.integer.value[0] = mout->share_spdif;
2582 return 0;
2583}
2584
2585static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_value *ucontrol)
2587{
2588 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2589 mout->share_spdif = !!ucontrol->value.integer.value[0];
2590 return 0;
2591}
2592
2593static struct snd_kcontrol_new spdif_share_sw = {
2594 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2595 .name = "IEC958 Default PCM Playback Switch",
2596 .info = snd_ctl_boolean_mono_info,
2597 .get = spdif_share_sw_get,
2598 .put = spdif_share_sw_put,
2599};
2600
Takashi Iwaid5191e52009-11-16 14:58:17 +01002601/**
2602 * snd_hda_create_spdif_share_sw - create Default PCM switch
2603 * @codec: the HDA codec
2604 * @mout: multi-out instance
2605 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002606int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2607 struct hda_multi_out *mout)
2608{
2609 if (!mout->dig_out_nid)
2610 return 0;
2611 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002612 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2613 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002614}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002615EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002616
2617/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 * SPDIF input
2619 */
2620
2621#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2622
Takashi Iwai0ba21762007-04-16 11:29:14 +02002623static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2624 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
2626 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2627
2628 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2629 return 0;
2630}
2631
Takashi Iwai0ba21762007-04-16 11:29:14 +02002632static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2633 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2636 hda_nid_t nid = kcontrol->private_value;
2637 unsigned int val = !!ucontrol->value.integer.value[0];
2638 int change;
2639
Ingo Molnar62932df2006-01-16 16:34:20 +01002640 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002642 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002644 snd_hda_codec_write_cache(codec, nid, 0,
2645 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002647 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return change;
2649}
2650
Takashi Iwai0ba21762007-04-16 11:29:14 +02002651static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2652 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2655 hda_nid_t nid = kcontrol->private_value;
2656 unsigned short val;
2657 unsigned int sbits;
2658
Andrew Paprocki3982d172007-12-19 12:13:44 +01002659 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 sbits = convert_to_spdif_status(val);
2661 ucontrol->value.iec958.status[0] = sbits;
2662 ucontrol->value.iec958.status[1] = sbits >> 8;
2663 ucontrol->value.iec958.status[2] = sbits >> 16;
2664 ucontrol->value.iec958.status[3] = sbits >> 24;
2665 return 0;
2666}
2667
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002668static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 {
2670 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002671 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 .info = snd_hda_spdif_in_switch_info,
2673 .get = snd_hda_spdif_in_switch_get,
2674 .put = snd_hda_spdif_in_switch_put,
2675 },
2676 {
2677 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2678 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002679 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 .info = snd_hda_spdif_mask_info,
2681 .get = snd_hda_spdif_in_status_get,
2682 },
2683 { } /* end */
2684};
2685
2686/**
2687 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2688 * @codec: the HDA codec
2689 * @nid: audio in widget NID
2690 *
2691 * Creates controls related with the SPDIF input.
2692 * Called from each patch supporting the SPDIF in.
2693 *
2694 * Returns 0 if successful, or a negative error code.
2695 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002696int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
2698 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002699 struct snd_kcontrol *kctl;
2700 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002701 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Takashi Iwai09f99702008-02-04 12:31:13 +01002703 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2704 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2705 idx))
2706 break;
2707 }
2708 if (idx >= SPDIF_MAX_IDX) {
2709 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2710 return -EBUSY;
2711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2713 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002714 if (!kctl)
2715 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002717 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002718 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return err;
2720 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002721 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002722 snd_hda_codec_read(codec, nid, 0,
2723 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002724 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return 0;
2726}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002727EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Takashi Iwaicb53c622007-08-10 17:21:45 +02002729#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002730/*
2731 * command cache
2732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002734/* build a 32bit cache key with the widget id and the command parameter */
2735#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2736#define get_cmd_cache_nid(key) ((key) & 0xff)
2737#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2738
2739/**
2740 * snd_hda_codec_write_cache - send a single command with caching
2741 * @codec: the HDA codec
2742 * @nid: NID to send the command
2743 * @direct: direct flag
2744 * @verb: the verb to send
2745 * @parm: the parameter for the verb
2746 *
2747 * Send a single command without waiting for response.
2748 *
2749 * Returns 0 if successful, or a negative error code.
2750 */
2751int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2752 int direct, unsigned int verb, unsigned int parm)
2753{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002754 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2755 struct hda_cache_head *c;
2756 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002757
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002758 if (err < 0)
2759 return err;
2760 /* parm may contain the verb stuff for get/set amp */
2761 verb = verb | (parm >> 8);
2762 parm &= 0xff;
2763 key = build_cmd_cache_key(nid, verb);
2764 mutex_lock(&codec->bus->cmd_mutex);
2765 c = get_alloc_hash(&codec->cmd_cache, key);
2766 if (c)
2767 c->val = parm;
2768 mutex_unlock(&codec->bus->cmd_mutex);
2769 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002770}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002771EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002772
Takashi Iwaid5191e52009-11-16 14:58:17 +01002773/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002774 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2775 * @codec: the HDA codec
2776 * @nid: NID to send the command
2777 * @direct: direct flag
2778 * @verb: the verb to send
2779 * @parm: the parameter for the verb
2780 *
2781 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2782 * command if the parameter is already identical with the cached value.
2783 * If not, it sends the command and refreshes the cache.
2784 *
2785 * Returns 0 if successful, or a negative error code.
2786 */
2787int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2788 int direct, unsigned int verb, unsigned int parm)
2789{
2790 struct hda_cache_head *c;
2791 u32 key;
2792
2793 /* parm may contain the verb stuff for get/set amp */
2794 verb = verb | (parm >> 8);
2795 parm &= 0xff;
2796 key = build_cmd_cache_key(nid, verb);
2797 mutex_lock(&codec->bus->cmd_mutex);
2798 c = get_hash(&codec->cmd_cache, key);
2799 if (c && c->val == parm) {
2800 mutex_unlock(&codec->bus->cmd_mutex);
2801 return 0;
2802 }
2803 mutex_unlock(&codec->bus->cmd_mutex);
2804 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2805}
2806EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2807
2808/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002809 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2810 * @codec: HD-audio codec
2811 *
2812 * Execute all verbs recorded in the command caches to resume.
2813 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002814void snd_hda_codec_resume_cache(struct hda_codec *codec)
2815{
Takashi Iwai603c4012008-07-30 15:01:44 +02002816 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002817 int i;
2818
Takashi Iwai603c4012008-07-30 15:01:44 +02002819 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002820 u32 key = buffer->key;
2821 if (!key)
2822 continue;
2823 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2824 get_cmd_cache_cmd(key), buffer->val);
2825 }
2826}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002827EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002828
2829/**
2830 * snd_hda_sequence_write_cache - sequence writes with caching
2831 * @codec: the HDA codec
2832 * @seq: VERB array to send
2833 *
2834 * Send the commands sequentially from the given array.
2835 * Thte commands are recorded on cache for power-save and resume.
2836 * The array must be terminated with NID=0.
2837 */
2838void snd_hda_sequence_write_cache(struct hda_codec *codec,
2839 const struct hda_verb *seq)
2840{
2841 for (; seq->nid; seq++)
2842 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2843 seq->param);
2844}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002845EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002846#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002847
Takashi Iwai54d17402005-11-21 16:33:22 +01002848/*
2849 * set power state of the codec
2850 */
2851static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2852 unsigned int power_state)
2853{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002854 hda_nid_t nid;
2855 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002856
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002857 /* this delay seems necessary to avoid click noise at power-down */
2858 if (power_state == AC_PWRST_D3)
2859 msleep(100);
2860 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002861 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002862 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08002863 if (power_state == AC_PWRST_D0 &&
2864 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002865 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002866
Takashi Iwaicb53c622007-08-10 17:21:45 +02002867 nid = codec->start_nid;
2868 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002869 unsigned int wcaps = get_wcaps(codec, nid);
2870 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002871 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002872 if (power_state == AC_PWRST_D3 &&
2873 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002874 unsigned int pincap;
2875 /*
2876 * don't power down the widget if it controls
2877 * eapd and EAPD_BTLENABLE is set.
2878 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01002879 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002880 if (pincap & AC_PINCAP_EAPD) {
2881 int eapd = snd_hda_codec_read(codec,
2882 nid, 0,
2883 AC_VERB_GET_EAPD_BTLENABLE, 0);
2884 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002885 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002886 continue;
2887 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002888 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002889 snd_hda_codec_write(codec, nid, 0,
2890 AC_VERB_SET_POWER_STATE,
2891 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002892 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002893 }
2894
Takashi Iwaicb53c622007-08-10 17:21:45 +02002895 if (power_state == AC_PWRST_D0) {
2896 unsigned long end_time;
2897 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002898 /* wait until the codec reachs to D0 */
2899 end_time = jiffies + msecs_to_jiffies(500);
2900 do {
2901 state = snd_hda_codec_read(codec, fg, 0,
2902 AC_VERB_GET_POWER_STATE, 0);
2903 if (state == power_state)
2904 break;
2905 msleep(1);
2906 } while (time_after_eq(end_time, jiffies));
2907 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002908}
2909
Takashi Iwai11aeff02008-07-30 15:01:46 +02002910#ifdef CONFIG_SND_HDA_HWDEP
2911/* execute additional init verbs */
2912static void hda_exec_init_verbs(struct hda_codec *codec)
2913{
2914 if (codec->init_verbs.list)
2915 snd_hda_sequence_write(codec, codec->init_verbs.list);
2916}
2917#else
2918static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2919#endif
2920
Takashi Iwaicb53c622007-08-10 17:21:45 +02002921#ifdef SND_HDA_NEEDS_RESUME
2922/*
2923 * call suspend and power-down; used both from PM and power-save
2924 */
2925static void hda_call_codec_suspend(struct hda_codec *codec)
2926{
2927 if (codec->patch_ops.suspend)
2928 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2929 hda_set_power_state(codec,
2930 codec->afg ? codec->afg : codec->mfg,
2931 AC_PWRST_D3);
2932#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01002933 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002934 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002935 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002936 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01002937 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002938#endif
2939}
2940
2941/*
2942 * kick up codec; used both from PM and power-save
2943 */
2944static void hda_call_codec_resume(struct hda_codec *codec)
2945{
2946 hda_set_power_state(codec,
2947 codec->afg ? codec->afg : codec->mfg,
2948 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01002949 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02002950 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002951 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002952 if (codec->patch_ops.resume)
2953 codec->patch_ops.resume(codec);
2954 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002955 if (codec->patch_ops.init)
2956 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002957 snd_hda_codec_resume_amp(codec);
2958 snd_hda_codec_resume_cache(codec);
2959 }
2960}
2961#endif /* SND_HDA_NEEDS_RESUME */
2962
Takashi Iwai54d17402005-11-21 16:33:22 +01002963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964/**
2965 * snd_hda_build_controls - build mixer controls
2966 * @bus: the BUS
2967 *
2968 * Creates mixer controls for each codec included in the bus.
2969 *
2970 * Returns 0 if successful, otherwise a negative error code.
2971 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002972int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002974 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Takashi Iwai0ba21762007-04-16 11:29:14 +02002976 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002977 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002978 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01002979 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002980 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002981 err = snd_hda_codec_reset(codec);
2982 if (err < 0) {
2983 printk(KERN_ERR
2984 "hda_codec: cannot revert codec\n");
2985 return err;
2986 }
2987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002989 return 0;
2990}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002991EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002993int snd_hda_codec_build_controls(struct hda_codec *codec)
2994{
2995 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02002996 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002997 /* continue to initialize... */
2998 if (codec->patch_ops.init)
2999 err = codec->patch_ops.init(codec);
3000 if (!err && codec->patch_ops.build_controls)
3001 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003002 if (err < 0)
3003 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return 0;
3005}
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007/*
3008 * stream formats
3009 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003010struct hda_rate_tbl {
3011 unsigned int hz;
3012 unsigned int alsa_bits;
3013 unsigned int hda_fmt;
3014};
3015
3016static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003018
3019 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
3021 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
3022 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
3023 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
3024 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
3025 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
3026 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
3027 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
3028 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
3029 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
3030 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003031#define AC_PAR_PCM_RATE_BITS 11
3032 /* up to bits 10, 384kHZ isn't supported properly */
3033
3034 /* not autodetected value */
3035 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003036
Takashi Iwaibefdf312005-08-22 13:57:55 +02003037 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038};
3039
3040/**
3041 * snd_hda_calc_stream_format - calculate format bitset
3042 * @rate: the sample rate
3043 * @channels: the number of channels
3044 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3045 * @maxbps: the max. bps
3046 *
3047 * Calculate the format bitset from the given rate, channels and th PCM format.
3048 *
3049 * Return zero if invalid.
3050 */
3051unsigned int snd_hda_calc_stream_format(unsigned int rate,
3052 unsigned int channels,
3053 unsigned int format,
3054 unsigned int maxbps)
3055{
3056 int i;
3057 unsigned int val = 0;
3058
Takashi Iwaibefdf312005-08-22 13:57:55 +02003059 for (i = 0; rate_bits[i].hz; i++)
3060 if (rate_bits[i].hz == rate) {
3061 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 break;
3063 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003064 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 snd_printdd("invalid rate %d\n", rate);
3066 return 0;
3067 }
3068
3069 if (channels == 0 || channels > 8) {
3070 snd_printdd("invalid channels %d\n", channels);
3071 return 0;
3072 }
3073 val |= channels - 1;
3074
3075 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003076 case 8:
3077 val |= 0x00;
3078 break;
3079 case 16:
3080 val |= 0x10;
3081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 case 20:
3083 case 24:
3084 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003085 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 val |= 0x40;
3087 else if (maxbps >= 24)
3088 val |= 0x30;
3089 else
3090 val |= 0x20;
3091 break;
3092 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003093 snd_printdd("invalid format width %d\n",
3094 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return 0;
3096 }
3097
3098 return val;
3099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003100EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003102static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3103{
3104 unsigned int val = 0;
3105 if (nid != codec->afg &&
3106 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3107 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3108 if (!val || val == -1)
3109 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3110 if (!val || val == -1)
3111 return 0;
3112 return val;
3113}
3114
3115static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3116{
3117 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3118 get_pcm_param);
3119}
3120
3121static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3122{
3123 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3124 if (!streams || streams == -1)
3125 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3126 if (!streams || streams == -1)
3127 return 0;
3128 return streams;
3129}
3130
3131static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3132{
3133 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3134 get_stream_param);
3135}
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137/**
3138 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3139 * @codec: the HDA codec
3140 * @nid: NID to query
3141 * @ratesp: the pointer to store the detected rate bitflags
3142 * @formatsp: the pointer to store the detected formats
3143 * @bpsp: the pointer to store the detected format widths
3144 *
3145 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3146 * or @bsps argument is ignored.
3147 *
3148 * Returns 0 if successful, otherwise a negative error code.
3149 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003150static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3152{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003153 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003155 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003156 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158 if (ratesp) {
3159 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003160 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003162 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003164 if (rates == 0) {
3165 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3166 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3167 nid, val,
3168 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3169 return -EIO;
3170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 *ratesp = rates;
3172 }
3173
3174 if (formatsp || bpsp) {
3175 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003176 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003178 streams = query_stream_param(codec, nid);
3179 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 bps = 0;
3183 if (streams & AC_SUPFMT_PCM) {
3184 if (val & AC_SUPPCM_BITS_8) {
3185 formats |= SNDRV_PCM_FMTBIT_U8;
3186 bps = 8;
3187 }
3188 if (val & AC_SUPPCM_BITS_16) {
3189 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3190 bps = 16;
3191 }
3192 if (wcaps & AC_WCAP_DIGITAL) {
3193 if (val & AC_SUPPCM_BITS_32)
3194 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3195 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3196 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3197 if (val & AC_SUPPCM_BITS_24)
3198 bps = 24;
3199 else if (val & AC_SUPPCM_BITS_20)
3200 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003201 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3202 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3204 if (val & AC_SUPPCM_BITS_32)
3205 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 else if (val & AC_SUPPCM_BITS_24)
3207 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003208 else if (val & AC_SUPPCM_BITS_20)
3209 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003212 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003214 if (!bps)
3215 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003216 }
3217 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003218 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 /* temporary hack: we have still no proper support
3220 * for the direct AC3 stream...
3221 */
3222 formats |= SNDRV_PCM_FMTBIT_U8;
3223 bps = 8;
3224 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003225 if (formats == 0) {
3226 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3227 "(nid=0x%x, val=0x%x, ovrd=%i, "
3228 "streams=0x%x)\n",
3229 nid, val,
3230 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3231 streams);
3232 return -EIO;
3233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (formatsp)
3235 *formatsp = formats;
3236 if (bpsp)
3237 *bpsp = bps;
3238 }
3239
3240 return 0;
3241}
3242
3243/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003244 * snd_hda_is_supported_format - Check the validity of the format
3245 * @codec: HD-audio codec
3246 * @nid: NID to check
3247 * @format: the HD-audio format value to check
3248 *
3249 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 *
3251 * Returns 1 if supported, 0 if not.
3252 */
3253int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3254 unsigned int format)
3255{
3256 int i;
3257 unsigned int val = 0, rate, stream;
3258
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003259 val = query_pcm_param(codec, nid);
3260 if (!val)
3261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003264 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003265 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (val & (1 << i))
3267 break;
3268 return 0;
3269 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003270 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return 0;
3272
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003273 stream = query_stream_param(codec, nid);
3274 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return 0;
3276
3277 if (stream & AC_SUPFMT_PCM) {
3278 switch (format & 0xf0) {
3279 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003280 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return 0;
3282 break;
3283 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003284 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return 0;
3286 break;
3287 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003288 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 return 0;
3290 break;
3291 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003292 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return 0;
3294 break;
3295 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003296 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return 0;
3298 break;
3299 default:
3300 return 0;
3301 }
3302 } else {
3303 /* FIXME: check for float32 and AC3? */
3304 }
3305
3306 return 1;
3307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003308EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310/*
3311 * PCM stuff
3312 */
3313static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3314 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003315 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
3317 return 0;
3318}
3319
3320static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3321 struct hda_codec *codec,
3322 unsigned int stream_tag,
3323 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003324 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3327 return 0;
3328}
3329
3330static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3331 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003332 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Takashi Iwai888afa12008-03-18 09:57:50 +01003334 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return 0;
3336}
3337
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003338static int set_pcm_default_values(struct hda_codec *codec,
3339 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003341 int err;
3342
Takashi Iwai0ba21762007-04-16 11:29:14 +02003343 /* query support PCM information from the given NID */
3344 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003345 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003346 info->rates ? NULL : &info->rates,
3347 info->formats ? NULL : &info->formats,
3348 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003349 if (err < 0)
3350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
3352 if (info->ops.open == NULL)
3353 info->ops.open = hda_pcm_default_open_close;
3354 if (info->ops.close == NULL)
3355 info->ops.close = hda_pcm_default_open_close;
3356 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003357 if (snd_BUG_ON(!info->nid))
3358 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 info->ops.prepare = hda_pcm_default_prepare;
3360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003362 if (snd_BUG_ON(!info->nid))
3363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 info->ops.cleanup = hda_pcm_default_cleanup;
3365 }
3366 return 0;
3367}
3368
Takashi Iwaid5191e52009-11-16 14:58:17 +01003369/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003370const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3371 "Audio", "SPDIF", "HDMI", "Modem"
3372};
3373
Takashi Iwai176d5332008-07-30 15:01:44 +02003374/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003375 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003376 *
3377 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003378 */
3379static int get_empty_pcm_device(struct hda_bus *bus, int type)
3380{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003381 /* audio device indices; not linear to keep compatibility */
3382 static int audio_idx[HDA_PCM_NTYPES][5] = {
3383 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3384 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003385 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003386 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003387 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003388 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003389
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003390 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003391 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3392 return -EINVAL;
3393 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003394
3395 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3396 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3397 return audio_idx[type][i];
3398
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003399 snd_printk(KERN_WARNING "Too many %s devices\n",
3400 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003401 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003402}
3403
3404/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003405 * attach a new PCM stream
3406 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003407static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003408{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003409 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003410 struct hda_pcm_stream *info;
3411 int stream, err;
3412
Takashi Iwaib91f0802008-11-04 08:43:08 +01003413 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003414 return -EINVAL;
3415 for (stream = 0; stream < 2; stream++) {
3416 info = &pcm->stream[stream];
3417 if (info->substreams) {
3418 err = set_pcm_default_values(codec, info);
3419 if (err < 0)
3420 return err;
3421 }
3422 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003423 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003424}
3425
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003426/* assign all PCMs of the given codec */
3427int snd_hda_codec_build_pcms(struct hda_codec *codec)
3428{
3429 unsigned int pcm;
3430 int err;
3431
3432 if (!codec->num_pcms) {
3433 if (!codec->patch_ops.build_pcms)
3434 return 0;
3435 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003436 if (err < 0) {
3437 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003438 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003439 err = snd_hda_codec_reset(codec);
3440 if (err < 0) {
3441 printk(KERN_ERR
3442 "hda_codec: cannot revert codec\n");
3443 return err;
3444 }
3445 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003446 }
3447 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3448 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3449 int dev;
3450
3451 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003452 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003453
3454 if (!cpcm->pcm) {
3455 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3456 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003457 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003458 cpcm->device = dev;
3459 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003460 if (err < 0) {
3461 printk(KERN_ERR "hda_codec: cannot attach "
3462 "PCM stream %d for codec #%d\n",
3463 dev, codec->addr);
3464 continue; /* no fatal error */
3465 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003466 }
3467 }
3468 return 0;
3469}
3470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471/**
3472 * snd_hda_build_pcms - build PCM information
3473 * @bus: the BUS
3474 *
3475 * Create PCM information for each codec included in the bus.
3476 *
3477 * The build_pcms codec patch is requested to set up codec->num_pcms and
3478 * codec->pcm_info properly. The array is referred by the top-level driver
3479 * to create its PCM instances.
3480 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3481 * callback.
3482 *
3483 * At least, substreams, channels_min and channels_max must be filled for
3484 * each stream. substreams = 0 indicates that the stream doesn't exist.
3485 * When rates and/or formats are zero, the supported values are queried
3486 * from the given nid. The nid is used also by the default ops.prepare
3487 * and ops.cleanup callbacks.
3488 *
3489 * The driver needs to call ops.open in its open callback. Similarly,
3490 * ops.close is supposed to be called in the close callback.
3491 * ops.prepare should be called in the prepare or hw_params callback
3492 * with the proper parameters for set up.
3493 * ops.cleanup should be called in hw_free for clean up of streams.
3494 *
3495 * This function returns 0 if successfull, or a negative error code.
3496 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003497int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003499 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Takashi Iwai0ba21762007-04-16 11:29:14 +02003501 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003502 int err = snd_hda_codec_build_pcms(codec);
3503 if (err < 0)
3504 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
3506 return 0;
3507}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003508EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510/**
3511 * snd_hda_check_board_config - compare the current codec with the config table
3512 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003513 * @num_configs: number of config enums
3514 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 * @tbl: configuration table, terminated by null entries
3516 *
3517 * Compares the modelname or PCI subsystem id of the current codec with the
3518 * given configuration table. If a matching entry is found, returns its
3519 * config value (supposed to be 0 or positive).
3520 *
3521 * If no entries are matching, the function returns a negative value.
3522 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003523int snd_hda_check_board_config(struct hda_codec *codec,
3524 int num_configs, const char **models,
3525 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003527 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003528 int i;
3529 for (i = 0; i < num_configs; i++) {
3530 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003531 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003532 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3533 "selected\n", models[i]);
3534 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 }
3536 }
3537 }
3538
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003539 if (!codec->bus->pci || !tbl)
3540 return -1;
3541
3542 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3543 if (!tbl)
3544 return -1;
3545 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003546#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003547 char tmp[10];
3548 const char *model = NULL;
3549 if (models)
3550 model = models[tbl->value];
3551 if (!model) {
3552 sprintf(tmp, "#%d", tbl->value);
3553 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003555 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3556 "for config %x:%x (%s)\n",
3557 model, tbl->subvendor, tbl->subdevice,
3558 (tbl->name ? tbl->name : "Unknown device"));
3559#endif
3560 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
3562 return -1;
3563}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003564EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003567 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003568 subsystem ID with the
3569 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003570
3571 This is important for Gateway notebooks with SB450 HDA Audio
3572 where the vendor ID of the PCI device is:
3573 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3574 and the vendor/subvendor are found only at the codec.
3575
3576 * @codec: the HDA codec
3577 * @num_configs: number of config enums
3578 * @models: array of model name strings
3579 * @tbl: configuration table, terminated by null entries
3580 *
3581 * Compares the modelname or PCI subsystem id of the current codec with the
3582 * given configuration table. If a matching entry is found, returns its
3583 * config value (supposed to be 0 or positive).
3584 *
3585 * If no entries are matching, the function returns a negative value.
3586 */
3587int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3588 int num_configs, const char **models,
3589 const struct snd_pci_quirk *tbl)
3590{
3591 const struct snd_pci_quirk *q;
3592
3593 /* Search for codec ID */
3594 for (q = tbl; q->subvendor; q++) {
3595 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3596
3597 if (vendorid == codec->subsystem_id)
3598 break;
3599 }
3600
3601 if (!q->subvendor)
3602 return -1;
3603
3604 tbl = q;
3605
3606 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003607#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003608 char tmp[10];
3609 const char *model = NULL;
3610 if (models)
3611 model = models[tbl->value];
3612 if (!model) {
3613 sprintf(tmp, "#%d", tbl->value);
3614 model = tmp;
3615 }
3616 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3617 "for config %x:%x (%s)\n",
3618 model, tbl->subvendor, tbl->subdevice,
3619 (tbl->name ? tbl->name : "Unknown device"));
3620#endif
3621 return tbl->value;
3622 }
3623 return -1;
3624}
3625EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3626
3627/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 * snd_hda_add_new_ctls - create controls from the array
3629 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003630 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 *
3632 * This helper function creates and add new controls in the given array.
3633 * The array must be terminated with an empty entry as terminator.
3634 *
3635 * Returns 0 if successful, or a negative error code.
3636 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003637int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003639 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003642 struct snd_kcontrol *kctl;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003643 if (knew->iface == -1) /* skip this codec private value */
3644 continue;
Takashi Iwai54d17402005-11-21 16:33:22 +01003645 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003646 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003647 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003648 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003649 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003650 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003651 return err;
3652 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003653 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003654 return -ENOMEM;
3655 kctl->id.device = codec->addr;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003656 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003657 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003658 return err;
3659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 }
3661 return 0;
3662}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003663EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Takashi Iwaicb53c622007-08-10 17:21:45 +02003665#ifdef CONFIG_SND_HDA_POWER_SAVE
3666static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3667 unsigned int power_state);
3668
3669static void hda_power_work(struct work_struct *work)
3670{
3671 struct hda_codec *codec =
3672 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003673 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003674
Maxim Levitsky2e492462007-09-03 15:26:57 +02003675 if (!codec->power_on || codec->power_count) {
3676 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003677 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003678 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003679
3680 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003681 if (bus->ops.pm_notify)
3682 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003683}
3684
3685static void hda_keep_power_on(struct hda_codec *codec)
3686{
3687 codec->power_count++;
3688 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003689 codec->power_jiffies = jiffies;
3690}
3691
Takashi Iwaid5191e52009-11-16 14:58:17 +01003692/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003693void snd_hda_update_power_acct(struct hda_codec *codec)
3694{
3695 unsigned long delta = jiffies - codec->power_jiffies;
3696 if (codec->power_on)
3697 codec->power_on_acct += delta;
3698 else
3699 codec->power_off_acct += delta;
3700 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003701}
3702
Takashi Iwaid5191e52009-11-16 14:58:17 +01003703/**
3704 * snd_hda_power_up - Power-up the codec
3705 * @codec: HD-audio codec
3706 *
3707 * Increment the power-up counter and power up the hardware really when
3708 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003709 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003710void snd_hda_power_up(struct hda_codec *codec)
3711{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003712 struct hda_bus *bus = codec->bus;
3713
Takashi Iwaicb53c622007-08-10 17:21:45 +02003714 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003715 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003716 return;
3717
Takashi Iwaia2f63092009-11-11 09:34:25 +01003718 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003719 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003720 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003721 if (bus->ops.pm_notify)
3722 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003723 hda_call_codec_resume(codec);
3724 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003725 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003726}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003727EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003728
3729#define power_save(codec) \
3730 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003731
Takashi Iwaid5191e52009-11-16 14:58:17 +01003732/**
3733 * snd_hda_power_down - Power-down the codec
3734 * @codec: HD-audio codec
3735 *
3736 * Decrement the power-up counter and schedules the power-off work if
3737 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003738 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003739void snd_hda_power_down(struct hda_codec *codec)
3740{
3741 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003742 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003743 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003744 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003745 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003746 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003747 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003748 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003749}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003750EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003751
Takashi Iwaid5191e52009-11-16 14:58:17 +01003752/**
3753 * snd_hda_check_amp_list_power - Check the amp list and update the power
3754 * @codec: HD-audio codec
3755 * @check: the object containing an AMP list and the status
3756 * @nid: NID to check / update
3757 *
3758 * Check whether the given NID is in the amp list. If it's in the list,
3759 * check the current AMP status, and update the the power-status according
3760 * to the mute status.
3761 *
3762 * This function is supposed to be set or called from the check_power_status
3763 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003764 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003765int snd_hda_check_amp_list_power(struct hda_codec *codec,
3766 struct hda_loopback_check *check,
3767 hda_nid_t nid)
3768{
3769 struct hda_amp_list *p;
3770 int ch, v;
3771
3772 if (!check->amplist)
3773 return 0;
3774 for (p = check->amplist; p->nid; p++) {
3775 if (p->nid == nid)
3776 break;
3777 }
3778 if (!p->nid)
3779 return 0; /* nothing changed */
3780
3781 for (p = check->amplist; p->nid; p++) {
3782 for (ch = 0; ch < 2; ch++) {
3783 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3784 p->idx);
3785 if (!(v & HDA_AMP_MUTE) && v > 0) {
3786 if (!check->power_on) {
3787 check->power_on = 1;
3788 snd_hda_power_up(codec);
3789 }
3790 return 1;
3791 }
3792 }
3793 }
3794 if (check->power_on) {
3795 check->power_on = 0;
3796 snd_hda_power_down(codec);
3797 }
3798 return 0;
3799}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003800EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003801#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003803/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003804 * Channel mode helper
3805 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003806
3807/**
3808 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3809 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003810int snd_hda_ch_mode_info(struct hda_codec *codec,
3811 struct snd_ctl_elem_info *uinfo,
3812 const struct hda_channel_mode *chmode,
3813 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003814{
3815 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3816 uinfo->count = 1;
3817 uinfo->value.enumerated.items = num_chmodes;
3818 if (uinfo->value.enumerated.item >= num_chmodes)
3819 uinfo->value.enumerated.item = num_chmodes - 1;
3820 sprintf(uinfo->value.enumerated.name, "%dch",
3821 chmode[uinfo->value.enumerated.item].channels);
3822 return 0;
3823}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003824EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003825
Takashi Iwaid5191e52009-11-16 14:58:17 +01003826/**
3827 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3828 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003829int snd_hda_ch_mode_get(struct hda_codec *codec,
3830 struct snd_ctl_elem_value *ucontrol,
3831 const struct hda_channel_mode *chmode,
3832 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003833 int max_channels)
3834{
3835 int i;
3836
3837 for (i = 0; i < num_chmodes; i++) {
3838 if (max_channels == chmode[i].channels) {
3839 ucontrol->value.enumerated.item[0] = i;
3840 break;
3841 }
3842 }
3843 return 0;
3844}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003845EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003846
Takashi Iwaid5191e52009-11-16 14:58:17 +01003847/**
3848 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3849 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003850int snd_hda_ch_mode_put(struct hda_codec *codec,
3851 struct snd_ctl_elem_value *ucontrol,
3852 const struct hda_channel_mode *chmode,
3853 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003854 int *max_channelsp)
3855{
3856 unsigned int mode;
3857
3858 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01003859 if (mode >= num_chmodes)
3860 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003861 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003862 return 0;
3863 /* change the current channel setting */
3864 *max_channelsp = chmode[mode].channels;
3865 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003866 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003867 return 1;
3868}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003869EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871/*
3872 * input MUX helper
3873 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003874
3875/**
3876 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3877 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003878int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3879 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880{
3881 unsigned int index;
3882
3883 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3884 uinfo->count = 1;
3885 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003886 if (!imux->num_items)
3887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 index = uinfo->value.enumerated.item;
3889 if (index >= imux->num_items)
3890 index = imux->num_items - 1;
3891 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3892 return 0;
3893}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003894EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Takashi Iwaid5191e52009-11-16 14:58:17 +01003896/**
3897 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3898 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003899int snd_hda_input_mux_put(struct hda_codec *codec,
3900 const struct hda_input_mux *imux,
3901 struct snd_ctl_elem_value *ucontrol,
3902 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 unsigned int *cur_val)
3904{
3905 unsigned int idx;
3906
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003907 if (!imux->num_items)
3908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 idx = ucontrol->value.enumerated.item[0];
3910 if (idx >= imux->num_items)
3911 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003912 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003914 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3915 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 *cur_val = idx;
3917 return 1;
3918}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003919EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
3921
3922/*
3923 * Multi-channel / digital-out PCM helper functions
3924 */
3925
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003926/* setup SPDIF output stream */
3927static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3928 unsigned int stream_tag, unsigned int format)
3929{
3930 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02003931 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003932 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02003933 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3934 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003935 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003936 if (codec->slave_dig_outs) {
3937 hda_nid_t *d;
3938 for (d = codec->slave_dig_outs; *d; d++)
3939 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3940 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003941 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003942 /* turn on again (if needed) */
3943 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3944 set_dig_out_convert(codec, nid,
3945 codec->spdif_ctls & 0xff, -1);
3946}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003947
Takashi Iwai2f728532008-09-25 16:32:41 +02003948static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3949{
3950 snd_hda_codec_cleanup_stream(codec, nid);
3951 if (codec->slave_dig_outs) {
3952 hda_nid_t *d;
3953 for (d = codec->slave_dig_outs; *d; d++)
3954 snd_hda_codec_cleanup_stream(codec, *d);
3955 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003956}
3957
Takashi Iwaid5191e52009-11-16 14:58:17 +01003958/**
3959 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
3960 * @bus: HD-audio bus
3961 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003962void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3963{
3964 struct hda_codec *codec;
3965
3966 if (!bus)
3967 return;
3968 list_for_each_entry(codec, &bus->codec_list, list) {
3969#ifdef CONFIG_SND_HDA_POWER_SAVE
3970 if (!codec->power_on)
3971 continue;
3972#endif
3973 if (codec->patch_ops.reboot_notify)
3974 codec->patch_ops.reboot_notify(codec);
3975 }
3976}
Takashi Iwai8f217a22009-11-10 18:26:12 +01003977EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01003978
Takashi Iwaid5191e52009-11-16 14:58:17 +01003979/**
3980 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003982int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3983 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
Ingo Molnar62932df2006-01-16 16:34:20 +01003985 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003986 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3987 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003988 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003990 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 return 0;
3992}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003993EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Takashi Iwaid5191e52009-11-16 14:58:17 +01003995/**
3996 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3997 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003998int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3999 struct hda_multi_out *mout,
4000 unsigned int stream_tag,
4001 unsigned int format,
4002 struct snd_pcm_substream *substream)
4003{
4004 mutex_lock(&codec->spdif_mutex);
4005 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4006 mutex_unlock(&codec->spdif_mutex);
4007 return 0;
4008}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004009EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004010
Takashi Iwaid5191e52009-11-16 14:58:17 +01004011/**
4012 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4013 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004014int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4015 struct hda_multi_out *mout)
4016{
4017 mutex_lock(&codec->spdif_mutex);
4018 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4019 mutex_unlock(&codec->spdif_mutex);
4020 return 0;
4021}
4022EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4023
Takashi Iwaid5191e52009-11-16 14:58:17 +01004024/**
4025 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004027int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4028 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
Ingo Molnar62932df2006-01-16 16:34:20 +01004030 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004032 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 return 0;
4034}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004035EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Takashi Iwaid5191e52009-11-16 14:58:17 +01004037/**
4038 * snd_hda_multi_out_analog_open - open analog outputs
4039 *
4040 * Open analog outputs and set up the hw-constraints.
4041 * If the digital outputs can be opened as slave, open the digital
4042 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004044int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4045 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004046 struct snd_pcm_substream *substream,
4047 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Takashi Iwai9a081602008-02-12 18:37:26 +01004049 struct snd_pcm_runtime *runtime = substream->runtime;
4050 runtime->hw.channels_max = mout->max_channels;
4051 if (mout->dig_out_nid) {
4052 if (!mout->analog_rates) {
4053 mout->analog_rates = hinfo->rates;
4054 mout->analog_formats = hinfo->formats;
4055 mout->analog_maxbps = hinfo->maxbps;
4056 } else {
4057 runtime->hw.rates = mout->analog_rates;
4058 runtime->hw.formats = mout->analog_formats;
4059 hinfo->maxbps = mout->analog_maxbps;
4060 }
4061 if (!mout->spdif_rates) {
4062 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4063 &mout->spdif_rates,
4064 &mout->spdif_formats,
4065 &mout->spdif_maxbps);
4066 }
4067 mutex_lock(&codec->spdif_mutex);
4068 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004069 if ((runtime->hw.rates & mout->spdif_rates) &&
4070 (runtime->hw.formats & mout->spdif_formats)) {
4071 runtime->hw.rates &= mout->spdif_rates;
4072 runtime->hw.formats &= mout->spdif_formats;
4073 if (mout->spdif_maxbps < hinfo->maxbps)
4074 hinfo->maxbps = mout->spdif_maxbps;
4075 } else {
4076 mout->share_spdif = 0;
4077 /* FIXME: need notify? */
4078 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004079 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004080 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4083 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4084}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004085EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Takashi Iwaid5191e52009-11-16 14:58:17 +01004087/**
4088 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4089 *
4090 * Set up the i/o for analog out.
4091 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004093int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4094 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 unsigned int stream_tag,
4096 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004097 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098{
4099 hda_nid_t *nids = mout->dac_nids;
4100 int chs = substream->runtime->channels;
4101 int i;
4102
Ingo Molnar62932df2006-01-16 16:34:20 +01004103 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004104 if (mout->dig_out_nid && mout->share_spdif &&
4105 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004107 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4108 format) &&
4109 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004111 setup_dig_out_stream(codec, mout->dig_out_nid,
4112 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 } else {
4114 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004115 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
4117 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004118 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
4120 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004121 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4122 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004123 if (!mout->no_share_stream &&
4124 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004126 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4127 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004128 /* extra outputs copied from front */
4129 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004130 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004131 snd_hda_codec_setup_stream(codec,
4132 mout->extra_out_nid[i],
4133 stream_tag, 0, format);
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 /* surrounds */
4136 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004137 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004138 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4139 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004140 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004141 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4142 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 }
4144 return 0;
4145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004146EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Takashi Iwaid5191e52009-11-16 14:58:17 +01004148/**
4149 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004151int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4152 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154 hda_nid_t *nids = mout->dac_nids;
4155 int i;
4156
4157 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004158 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004160 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004161 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4162 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004163 snd_hda_codec_cleanup_stream(codec,
4164 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004165 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004167 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 mout->dig_out_used = 0;
4169 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004170 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 return 0;
4172}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004173EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004175/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004176 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004177 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004178
Takashi Iwai12f288b2007-08-02 15:51:59 +02004179static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004180{
4181 for (; *list; list++)
4182 if (*list == nid)
4183 return 1;
4184 return 0;
4185}
4186
Steve Longerbeam81937d32007-05-08 15:33:03 +02004187
4188/*
4189 * Sort an associated group of pins according to their sequence numbers.
4190 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004191static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004192 int num_pins)
4193{
4194 int i, j;
4195 short seq;
4196 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004197
Steve Longerbeam81937d32007-05-08 15:33:03 +02004198 for (i = 0; i < num_pins; i++) {
4199 for (j = i + 1; j < num_pins; j++) {
4200 if (sequences[i] > sequences[j]) {
4201 seq = sequences[i];
4202 sequences[i] = sequences[j];
4203 sequences[j] = seq;
4204 nid = pins[i];
4205 pins[i] = pins[j];
4206 pins[j] = nid;
4207 }
4208 }
4209 }
4210}
4211
4212
Takashi Iwai82bc9552006-03-21 11:24:42 +01004213/*
4214 * Parse all pin widgets and store the useful pin nids to cfg
4215 *
4216 * The number of line-outs or any primary output is stored in line_outs,
4217 * and the corresponding output pins are assigned to line_out_pins[],
4218 * in the order of front, rear, CLFE, side, ...
4219 *
4220 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004221 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004222 * is detected, one of speaker of HP pins is assigned as the primary
4223 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4224 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004225 *
Takashi Iwai82bc9552006-03-21 11:24:42 +01004226 * The analog input pins are assigned to input_pins array.
4227 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4228 * respectively.
4229 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004230int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4231 struct auto_pin_cfg *cfg,
4232 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004233{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004234 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004235 short seq, assoc_line_out, assoc_speaker;
4236 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4237 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004238 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004239
4240 memset(cfg, 0, sizeof(*cfg));
4241
Steve Longerbeam81937d32007-05-08 15:33:03 +02004242 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4243 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004244 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004245 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004246
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004247 end_nid = codec->start_nid + codec->num_nodes;
4248 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004249 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004250 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004251 unsigned int def_conf;
4252 short assoc, loc;
4253
4254 /* read all default configuration for pin complex */
4255 if (wid_type != AC_WID_PIN)
4256 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004257 /* ignore the given nids (e.g. pc-beep returns error) */
4258 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4259 continue;
4260
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004261 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004262 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4263 continue;
4264 loc = get_defcfg_location(def_conf);
4265 switch (get_defcfg_device(def_conf)) {
4266 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004267 seq = get_defcfg_sequence(def_conf);
4268 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004269
4270 if (!(wid_caps & AC_WCAP_STEREO))
4271 if (!cfg->mono_out_pin)
4272 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004273 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004274 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004275 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004276 assoc_line_out = assoc;
4277 else if (assoc_line_out != assoc)
4278 continue;
4279 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4280 continue;
4281 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004282 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004283 cfg->line_outs++;
4284 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004285 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004286 seq = get_defcfg_sequence(def_conf);
4287 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004288 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004289 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004290 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004291 assoc_speaker = assoc;
4292 else if (assoc_speaker != assoc)
4293 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004294 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4295 continue;
4296 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004297 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004298 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004299 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004300 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004301 seq = get_defcfg_sequence(def_conf);
4302 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004303 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4304 continue;
4305 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004306 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004307 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004308 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004309 case AC_JACK_MIC_IN: {
4310 int preferred, alt;
Kailang Yang6ff86a32010-03-19 11:14:36 +01004311 if (loc == AC_JACK_LOC_FRONT ||
4312 (loc & 0x30) == AC_JACK_LOC_INTERNAL) {
Takashi Iwai314634b2006-09-21 11:56:18 +02004313 preferred = AUTO_PIN_FRONT_MIC;
4314 alt = AUTO_PIN_MIC;
4315 } else {
4316 preferred = AUTO_PIN_MIC;
4317 alt = AUTO_PIN_FRONT_MIC;
4318 }
4319 if (!cfg->input_pins[preferred])
4320 cfg->input_pins[preferred] = nid;
4321 else if (!cfg->input_pins[alt])
4322 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004323 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02004324 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004325 case AC_JACK_LINE_IN:
4326 if (loc == AC_JACK_LOC_FRONT)
4327 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4328 else
4329 cfg->input_pins[AUTO_PIN_LINE] = nid;
4330 break;
4331 case AC_JACK_CD:
4332 cfg->input_pins[AUTO_PIN_CD] = nid;
4333 break;
4334 case AC_JACK_AUX:
4335 cfg->input_pins[AUTO_PIN_AUX] = nid;
4336 break;
4337 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004338 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004339 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4340 continue;
4341 cfg->dig_out_pins[cfg->dig_outs] = nid;
4342 cfg->dig_out_type[cfg->dig_outs] =
4343 (loc == AC_JACK_LOC_HDMI) ?
4344 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4345 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004346 break;
4347 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004348 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004349 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004350 if (loc == AC_JACK_LOC_HDMI)
4351 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4352 else
4353 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004354 break;
4355 }
4356 }
4357
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004358 /* FIX-UP:
4359 * If no line-out is defined but multiple HPs are found,
4360 * some of them might be the real line-outs.
4361 */
4362 if (!cfg->line_outs && cfg->hp_outs > 1) {
4363 int i = 0;
4364 while (i < cfg->hp_outs) {
4365 /* The real HPs should have the sequence 0x0f */
4366 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4367 i++;
4368 continue;
4369 }
4370 /* Move it to the line-out table */
4371 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4372 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4373 cfg->line_outs++;
4374 cfg->hp_outs--;
4375 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4376 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4377 memmove(sequences_hp + i - 1, sequences_hp + i,
4378 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4379 }
4380 }
4381
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004382 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004383 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4384 cfg->line_outs);
4385 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4386 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004387 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4388 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004389
Takashi Iwaif889fa92007-10-31 15:49:32 +01004390 /* if we have only one mic, make it AUTO_PIN_MIC */
4391 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4392 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4393 cfg->input_pins[AUTO_PIN_MIC] =
4394 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4395 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4396 }
4397 /* ditto for line-in */
4398 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4399 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4400 cfg->input_pins[AUTO_PIN_LINE] =
4401 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4402 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4403 }
4404
Steve Longerbeam81937d32007-05-08 15:33:03 +02004405 /*
4406 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4407 * as a primary output
4408 */
4409 if (!cfg->line_outs) {
4410 if (cfg->speaker_outs) {
4411 cfg->line_outs = cfg->speaker_outs;
4412 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4413 sizeof(cfg->speaker_pins));
4414 cfg->speaker_outs = 0;
4415 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4416 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4417 } else if (cfg->hp_outs) {
4418 cfg->line_outs = cfg->hp_outs;
4419 memcpy(cfg->line_out_pins, cfg->hp_pins,
4420 sizeof(cfg->hp_pins));
4421 cfg->hp_outs = 0;
4422 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4423 cfg->line_out_type = AUTO_PIN_HP_OUT;
4424 }
4425 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004426
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004427 /* Reorder the surround channels
4428 * ALSA sequence is front/surr/clfe/side
4429 * HDA sequence is:
4430 * 4-ch: front/surr => OK as it is
4431 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004432 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004433 */
4434 switch (cfg->line_outs) {
4435 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004436 case 4:
4437 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004438 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004439 cfg->line_out_pins[2] = nid;
4440 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004441 }
4442
Takashi Iwai82bc9552006-03-21 11:24:42 +01004443 /*
4444 * debug prints of the parsed results
4445 */
4446 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4447 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4448 cfg->line_out_pins[2], cfg->line_out_pins[3],
4449 cfg->line_out_pins[4]);
4450 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4451 cfg->speaker_outs, cfg->speaker_pins[0],
4452 cfg->speaker_pins[1], cfg->speaker_pins[2],
4453 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004454 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4455 cfg->hp_outs, cfg->hp_pins[0],
4456 cfg->hp_pins[1], cfg->hp_pins[2],
4457 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004458 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004459 if (cfg->dig_outs)
4460 snd_printd(" dig-out=0x%x/0x%x\n",
4461 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004462 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4463 " cd=0x%x, aux=0x%x\n",
4464 cfg->input_pins[AUTO_PIN_MIC],
4465 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4466 cfg->input_pins[AUTO_PIN_LINE],
4467 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4468 cfg->input_pins[AUTO_PIN_CD],
4469 cfg->input_pins[AUTO_PIN_AUX]);
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004470 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004471 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004472
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004473 return 0;
4474}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004475EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004476
Takashi Iwai4a471b72005-12-07 13:56:29 +01004477/* labels for input pins */
4478const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4479 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4480};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004481EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01004482
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484#ifdef CONFIG_PM
4485/*
4486 * power management
4487 */
4488
4489/**
4490 * snd_hda_suspend - suspend the codecs
4491 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 *
4493 * Returns 0 if successful.
4494 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004495int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004497 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Takashi Iwai0ba21762007-04-16 11:29:14 +02004499 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004500#ifdef CONFIG_SND_HDA_POWER_SAVE
4501 if (!codec->power_on)
4502 continue;
4503#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004504 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 }
4506 return 0;
4507}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004508EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
4510/**
4511 * snd_hda_resume - resume the codecs
4512 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 *
4514 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004515 *
4516 * This fucntion is defined only when POWER_SAVE isn't set.
4517 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 */
4519int snd_hda_resume(struct hda_bus *bus)
4520{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004521 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Takashi Iwai0ba21762007-04-16 11:29:14 +02004523 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004524 if (snd_hda_codec_needs_resume(codec))
4525 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 return 0;
4528}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004529EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004530#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004531
4532/*
4533 * generic arrays
4534 */
4535
Takashi Iwaid5191e52009-11-16 14:58:17 +01004536/**
4537 * snd_array_new - get a new element from the given array
4538 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004539 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004540 * Get a new element from the given array. If it exceeds the
4541 * pre-allocated array size, re-allocate the array.
4542 *
4543 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004544 */
4545void *snd_array_new(struct snd_array *array)
4546{
4547 if (array->used >= array->alloced) {
4548 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004549 void *nlist;
4550 if (snd_BUG_ON(num >= 4096))
4551 return NULL;
4552 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004553 if (!nlist)
4554 return NULL;
4555 if (array->list) {
4556 memcpy(nlist, array->list,
4557 array->elem_size * array->alloced);
4558 kfree(array->list);
4559 }
4560 array->list = nlist;
4561 array->alloced = num;
4562 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004563 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004564}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004565EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004566
Takashi Iwaid5191e52009-11-16 14:58:17 +01004567/**
4568 * snd_array_free - free the given array elements
4569 * @array: the array object
4570 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004571void snd_array_free(struct snd_array *array)
4572{
4573 kfree(array->list);
4574 array->used = 0;
4575 array->alloced = 0;
4576 array->list = NULL;
4577}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004578EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004579
Takashi Iwaid5191e52009-11-16 14:58:17 +01004580/**
4581 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4582 * @pcm: PCM caps bits
4583 * @buf: the string buffer to write
4584 * @buflen: the max buffer length
4585 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004586 * used by hda_proc.c and hda_eld.c
4587 */
4588void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4589{
4590 static unsigned int rates[] = {
4591 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4592 96000, 176400, 192000, 384000
4593 };
4594 int i, j;
4595
4596 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4597 if (pcm & (1 << i))
4598 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4599
4600 buf[j] = '\0'; /* necessary when j == 0 */
4601}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004602EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004603
Takashi Iwaid5191e52009-11-16 14:58:17 +01004604/**
4605 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4606 * @pcm: PCM caps bits
4607 * @buf: the string buffer to write
4608 * @buflen: the max buffer length
4609 *
4610 * used by hda_proc.c and hda_eld.c
4611 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004612void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4613{
4614 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4615 int i, j;
4616
4617 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4618 if (pcm & (AC_SUPPCM_BITS_8 << i))
4619 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4620
4621 buf[j] = '\0'; /* necessary when j == 0 */
4622}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004623EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004624
4625MODULE_DESCRIPTION("HDA codec core");
4626MODULE_LICENSE("GPL");