blob: af989f660cca243c54ad7d35c82ee968f9bec928 [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"
Takashi Iwai28073142007-07-27 18:58:06 +020033#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * vendor / preset table
37 */
38
39struct hda_vendor_id {
40 unsigned int id;
41 const char *name;
42};
43
44/* codec vendor labels */
45static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010046 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020047 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020048 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010049 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010050 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010051 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020052 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010053 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020054 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010055 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020056 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020058 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x17e8, "Chrontel" },
60 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000061 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010063 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020064 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 {} /* terminator */
66};
67
Takashi Iwai1289e9e2008-11-27 15:47:11 +010068static DEFINE_MUTEX(preset_mutex);
69static LIST_HEAD(hda_preset_tables);
70
71int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
72{
73 mutex_lock(&preset_mutex);
74 list_add_tail(&preset->list, &hda_preset_tables);
75 mutex_unlock(&preset_mutex);
76 return 0;
77}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010078EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010079
80int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
81{
82 mutex_lock(&preset_mutex);
83 list_del(&preset->list);
84 mutex_unlock(&preset_mutex);
85 return 0;
86}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010087EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Takashi Iwaicb53c622007-08-10 17:21:45 +020089#ifdef CONFIG_SND_HDA_POWER_SAVE
90static void hda_power_work(struct work_struct *work);
91static void hda_keep_power_on(struct hda_codec *codec);
92#else
93static inline void hda_keep_power_on(struct hda_codec *codec) {}
94#endif
95
Matthew Ranostay50a9f792008-10-25 01:05:45 -040096const char *snd_hda_get_jack_location(u32 cfg)
97{
98 static char *bases[7] = {
99 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
100 };
101 static unsigned char specials_idx[] = {
102 0x07, 0x08,
103 0x17, 0x18, 0x19,
104 0x37, 0x38
105 };
106 static char *specials[] = {
107 "Rear Panel", "Drive Bar",
108 "Riser", "HDMI", "ATAPI",
109 "Mobile-In", "Mobile-Out"
110 };
111 int i;
112 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
113 if ((cfg & 0x0f) < 7)
114 return bases[cfg & 0x0f];
115 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
116 if (cfg == specials_idx[i])
117 return specials[i];
118 }
119 return "UNKNOWN";
120}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100121EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400122
123const char *snd_hda_get_jack_connectivity(u32 cfg)
124{
125 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
126
127 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100129EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400130
131const char *snd_hda_get_jack_type(u32 cfg)
132{
133 static char *jack_types[16] = {
134 "Line Out", "Speaker", "HP Out", "CD",
135 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
136 "Line In", "Aux", "Mic", "Telephony",
137 "SPDIF In", "Digitial In", "Reserved", "Other"
138 };
139
140 return jack_types[(cfg & AC_DEFCFG_DEVICE)
141 >> AC_DEFCFG_DEVICE_SHIFT];
142}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100143EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400144
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100145/*
146 * Compose a 32bit command word to be sent to the HD-audio controller
147 */
148static inline unsigned int
149make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
150 unsigned int verb, unsigned int parm)
151{
152 u32 val;
153
Takashi Iwai82e1b802009-07-17 12:47:34 +0200154 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
155 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800156 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
157 codec->addr, direct, nid, verb, parm);
158 return ~0;
159 }
160
161 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100162 val |= (u32)direct << 27;
163 val |= (u32)nid << 20;
164 val |= verb << 8;
165 val |= parm;
166 return val;
167}
168
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200169/*
170 * Send and receive a verb
171 */
172static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
173 unsigned int *res)
174{
175 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200176 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200177
Wu Fengguang6430aee2009-07-17 16:49:19 +0800178 if (cmd == ~0)
179 return -1;
180
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200181 if (res)
182 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200183 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200184 snd_hda_power_up(codec);
185 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200186 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200187 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800188 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200189 mutex_unlock(&bus->cmd_mutex);
190 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200191 if (res && *res == -1 && bus->rirb_error) {
192 if (bus->response_reset) {
193 snd_printd("hda_codec: resetting BUS due to "
194 "fatal communication error\n");
195 bus->ops.bus_reset(bus);
196 }
197 goto again;
198 }
199 /* clear reset-flag when the communication gets recovered */
200 if (!err)
201 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200202 return err;
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/**
206 * snd_hda_codec_read - send a command and get the response
207 * @codec: the HDA codec
208 * @nid: NID to send the command
209 * @direct: direct flag
210 * @verb: the verb to send
211 * @parm: the parameter for the verb
212 *
213 * Send a single command and read the corresponding response.
214 *
215 * Returns the obtained response value, or -1 for an error.
216 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200217unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
218 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 unsigned int verb, unsigned int parm)
220{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200221 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
222 unsigned int res;
223 codec_exec_verb(codec, cmd, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return res;
225}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100226EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228/**
229 * snd_hda_codec_write - send a single command without waiting for response
230 * @codec: the HDA codec
231 * @nid: NID to send the command
232 * @direct: direct flag
233 * @verb: the verb to send
234 * @parm: the parameter for the verb
235 *
236 * Send a single command without waiting for response.
237 *
238 * Returns 0 if successful, or a negative error code.
239 */
240int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
241 unsigned int verb, unsigned int parm)
242{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200243 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100244 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200245 return codec_exec_verb(codec, cmd,
246 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100248EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/**
251 * snd_hda_sequence_write - sequence writes
252 * @codec: the HDA codec
253 * @seq: VERB array to send
254 *
255 * Send the commands sequentially from the given array.
256 * The array must be terminated with NID=0.
257 */
258void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
259{
260 for (; seq->nid; seq++)
261 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
262}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100263EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265/**
266 * snd_hda_get_sub_nodes - get the range of sub nodes
267 * @codec: the HDA codec
268 * @nid: NID to parse
269 * @start_id: the pointer to store the start NID
270 *
271 * Parse the NID and store the start NID of its sub-nodes.
272 * Returns the number of sub-nodes.
273 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200274int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
275 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 unsigned int parm;
278
279 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200280 if (parm == -1)
281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *start_id = (parm >> 16) & 0x7fff;
283 return (int)(parm & 0x7fff);
284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100285EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/**
288 * snd_hda_get_connections - get connection list
289 * @codec: the HDA codec
290 * @nid: NID to parse
291 * @conn_list: connection list array
292 * @max_conns: max. number of connections to store
293 *
294 * Parses the connection list of the given widget and stores the list
295 * of NIDs.
296 *
297 * Returns the number of connections, or a negative error code.
298 */
299int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
300 hda_nid_t *conn_list, int max_conns)
301{
302 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100303 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200305 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100306 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Takashi Iwaida3cec32008-08-08 17:12:14 +0200308 if (snd_BUG_ON(!conn_list || max_conns <= 0))
309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200311 wcaps = get_wcaps(codec, nid);
312 if (!(wcaps & AC_WCAP_CONN_LIST) &&
313 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200314 snd_printk(KERN_WARNING "hda_codec: "
315 "connection list not available for 0x%x\n", nid);
316 return -EINVAL;
317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
320 if (parm & AC_CLIST_LONG) {
321 /* long form */
322 shift = 16;
323 num_elems = 2;
324 } else {
325 /* short form */
326 shift = 8;
327 num_elems = 4;
328 }
329 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 mask = (1 << (shift-1)) - 1;
331
Takashi Iwai0ba21762007-04-16 11:29:14 +0200332 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0; /* no connection */
334
335 if (conn_len == 1) {
336 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200337 parm = snd_hda_codec_read(codec, nid, 0,
338 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200339 if (parm == -1 && codec->bus->rirb_error)
340 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 conn_list[0] = parm & mask;
342 return 1;
343 }
344
345 /* multi connection */
346 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100347 prev_nid = 0;
348 for (i = 0; i < conn_len; i++) {
349 int range_val;
350 hda_nid_t val, n;
351
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200352 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100353 parm = snd_hda_codec_read(codec, nid, 0,
354 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200355 if (parm == -1 && codec->bus->rirb_error)
356 return -EIO;
357 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200358 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100359 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200360 if (val == 0) {
361 snd_printk(KERN_WARNING "hda_codec: "
362 "invalid CONNECT_LIST verb %x[%i]:%x\n",
363 nid, i, parm);
364 return 0;
365 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100366 parm >>= shift;
367 if (range_val) {
368 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200369 if (!prev_nid || prev_nid >= val) {
370 snd_printk(KERN_WARNING "hda_codec: "
371 "invalid dep_range_val %x:%x\n",
372 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100373 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100375 for (n = prev_nid + 1; n <= val; n++) {
376 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200377 snd_printk(KERN_ERR
378 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100379 return -EINVAL;
380 }
381 conn_list[conns++] = n;
382 }
383 } else {
384 if (conns >= max_conns) {
385 snd_printk(KERN_ERR "Too many connections\n");
386 return -EINVAL;
387 }
388 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100390 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 return conns;
393}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100394EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396
397/**
398 * snd_hda_queue_unsol_event - add an unsolicited event to queue
399 * @bus: the BUS
400 * @res: unsolicited event (lower 32bit of RIRB entry)
401 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
402 *
403 * Adds the given event to the queue. The events are processed in
404 * the workqueue asynchronously. Call this function in the interrupt
405 * hanlder when RIRB receives an unsolicited event.
406 *
407 * Returns 0 if successful, or a negative error code.
408 */
409int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
410{
411 struct hda_bus_unsolicited *unsol;
412 unsigned int wp;
413
Takashi Iwai0ba21762007-04-16 11:29:14 +0200414 unsol = bus->unsol;
415 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
417
418 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
419 unsol->wp = wp;
420
421 wp <<= 1;
422 unsol->queue[wp] = res;
423 unsol->queue[wp + 1] = res_ex;
424
Takashi Iwai6acaed32009-01-12 10:09:24 +0100425 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 return 0;
428}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100429EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800432 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
David Howellsc4028952006-11-22 14:57:56 +0000434static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
David Howellsc4028952006-11-22 14:57:56 +0000436 struct hda_bus_unsolicited *unsol =
437 container_of(work, struct hda_bus_unsolicited, work);
438 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct hda_codec *codec;
440 unsigned int rp, caddr, res;
441
442 while (unsol->rp != unsol->wp) {
443 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
444 unsol->rp = rp;
445 rp <<= 1;
446 res = unsol->queue[rp];
447 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200448 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 continue;
450 codec = bus->caddr_tbl[caddr & 0x0f];
451 if (codec && codec->patch_ops.unsol_event)
452 codec->patch_ops.unsol_event(codec, res);
453 }
454}
455
456/*
457 * initialize unsolicited queue
458 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200459static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct hda_bus_unsolicited *unsol;
462
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100463 if (bus->unsol) /* already initialized */
464 return 0;
465
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200466 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200467 if (!unsol) {
468 snd_printk(KERN_ERR "hda_codec: "
469 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENOMEM;
471 }
David Howellsc4028952006-11-22 14:57:56 +0000472 INIT_WORK(&unsol->work, process_unsol_events);
473 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 bus->unsol = unsol;
475 return 0;
476}
477
478/*
479 * destructor
480 */
481static void snd_hda_codec_free(struct hda_codec *codec);
482
483static int snd_hda_bus_free(struct hda_bus *bus)
484{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200485 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Takashi Iwai0ba21762007-04-16 11:29:14 +0200487 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100489 if (bus->workq)
490 flush_workqueue(bus->workq);
491 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200493 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 snd_hda_codec_free(codec);
495 }
496 if (bus->ops.private_free)
497 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100498 if (bus->workq)
499 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 kfree(bus);
501 return 0;
502}
503
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100504static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100507 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return snd_hda_bus_free(bus);
509}
510
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200511#ifdef CONFIG_SND_HDA_HWDEP
512static int snd_hda_bus_dev_register(struct snd_device *device)
513{
514 struct hda_bus *bus = device->device_data;
515 struct hda_codec *codec;
516 list_for_each_entry(codec, &bus->codec_list, list) {
517 snd_hda_hwdep_add_sysfs(codec);
518 }
519 return 0;
520}
521#else
522#define snd_hda_bus_dev_register NULL
523#endif
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/**
526 * snd_hda_bus_new - create a HDA bus
527 * @card: the card entry
528 * @temp: the template for hda_bus information
529 * @busp: the pointer to store the created bus instance
530 *
531 * Returns 0 if successful, or a negative error code.
532 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100533int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200534 const struct hda_bus_template *temp,
535 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct hda_bus *bus;
538 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100539 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200540 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 .dev_free = snd_hda_bus_dev_free,
542 };
543
Takashi Iwaida3cec32008-08-08 17:12:14 +0200544 if (snd_BUG_ON(!temp))
545 return -EINVAL;
546 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
547 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (busp)
550 *busp = NULL;
551
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200552 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (bus == NULL) {
554 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
555 return -ENOMEM;
556 }
557
558 bus->card = card;
559 bus->private_data = temp->private_data;
560 bus->pci = temp->pci;
561 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100562 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 bus->ops = temp->ops;
564
Ingo Molnar62932df2006-01-16 16:34:20 +0100565 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 INIT_LIST_HEAD(&bus->codec_list);
567
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100568 snprintf(bus->workq_name, sizeof(bus->workq_name),
569 "hd-audio%d", card->number);
570 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100571 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100572 snd_printk(KERN_ERR "cannot create workqueue %s\n",
573 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100574 kfree(bus);
575 return -ENOMEM;
576 }
577
Takashi Iwai0ba21762007-04-16 11:29:14 +0200578 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
579 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 snd_hda_bus_free(bus);
581 return err;
582 }
583 if (busp)
584 *busp = bus;
585 return 0;
586}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100587EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Takashi Iwai82467612007-07-27 19:15:54 +0200589#ifdef CONFIG_SND_HDA_GENERIC
590#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200591 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200592#else
593#define is_generic_config(codec) 0
594#endif
595
Takashi Iwai645f10c2008-11-28 15:07:37 +0100596#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100597#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
598#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100599#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100600#endif
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*
603 * find a matching codec preset
604 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200605static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200606find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100608 struct hda_codec_preset_list *tbl;
609 const struct hda_codec_preset *preset;
610 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Takashi Iwai82467612007-07-27 19:15:54 +0200612 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100613 return NULL; /* use the generic parser */
614
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100615 again:
616 mutex_lock(&preset_mutex);
617 list_for_each_entry(tbl, &hda_preset_tables, list) {
618 if (!try_module_get(tbl->owner)) {
619 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
620 continue;
621 }
622 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100624 if (preset->afg && preset->afg != codec->afg)
625 continue;
626 if (preset->mfg && preset->mfg != codec->mfg)
627 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200628 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200630 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200631 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100632 preset->rev == codec->revision_id)) {
633 mutex_unlock(&preset_mutex);
634 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100638 module_put(tbl->owner);
639 }
640 mutex_unlock(&preset_mutex);
641
642 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
643 char name[32];
644 if (!mod_requested)
645 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
646 codec->vendor_id);
647 else
648 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
649 (codec->vendor_id >> 16) & 0xffff);
650 request_module(name);
651 mod_requested++;
652 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 return NULL;
655}
656
657/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200658 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200660static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 const struct hda_vendor_id *c;
663 const char *vendor = NULL;
664 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200665 char tmp[16];
666
667 if (codec->vendor_name)
668 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 for (c = hda_vendor_ids; c->id; c++) {
671 if (c->id == vendor_id) {
672 vendor = c->name;
673 break;
674 }
675 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200676 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 sprintf(tmp, "Generic %04x", vendor_id);
678 vendor = tmp;
679 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200680 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
681 if (!codec->vendor_name)
682 return -ENOMEM;
683
684 get_chip_name:
685 if (codec->chip_name)
686 return 0;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200689 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
690 else {
691 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
692 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
693 }
694 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200695 return -ENOMEM;
696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200700 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100702static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200704 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 hda_nid_t nid;
706
707 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
708 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200709 function_id = snd_hda_param_read(codec, nid,
Pascal de Bruijn234b4342009-03-23 11:15:59 +0100710 AC_PAR_FUNCTION_TYPE) & 0xff;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200711 switch (function_id) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200712 case AC_GRP_AUDIO_FUNCTION:
713 codec->afg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200714 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200715 break;
716 case AC_GRP_MODEM_FUNCTION:
717 codec->mfg = nid;
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200718 codec->function_id = function_id;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200719 break;
720 default:
721 break;
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100727 * read widget caps for each widget and store in cache
728 */
729static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
730{
731 int i;
732 hda_nid_t nid;
733
734 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
735 &codec->start_nid);
736 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200737 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100738 return -ENOMEM;
739 nid = codec->start_nid;
740 for (i = 0; i < codec->num_nodes; i++, nid++)
741 codec->wcaps[i] = snd_hda_param_read(codec, nid,
742 AC_PAR_AUDIO_WIDGET_CAP);
743 return 0;
744}
745
Takashi Iwai3be14142009-02-20 14:11:16 +0100746/* read all pin default configurations and save codec->init_pins */
747static int read_pin_defaults(struct hda_codec *codec)
748{
749 int i;
750 hda_nid_t nid = codec->start_nid;
751
752 for (i = 0; i < codec->num_nodes; i++, nid++) {
753 struct hda_pincfg *pin;
754 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200755 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100756 if (wid_type != AC_WID_PIN)
757 continue;
758 pin = snd_array_new(&codec->init_pins);
759 if (!pin)
760 return -ENOMEM;
761 pin->nid = nid;
762 pin->cfg = snd_hda_codec_read(codec, nid, 0,
763 AC_VERB_GET_CONFIG_DEFAULT, 0);
764 }
765 return 0;
766}
767
768/* look up the given pin config list and return the item matching with NID */
769static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
770 struct snd_array *array,
771 hda_nid_t nid)
772{
773 int i;
774 for (i = 0; i < array->used; i++) {
775 struct hda_pincfg *pin = snd_array_elem(array, i);
776 if (pin->nid == nid)
777 return pin;
778 }
779 return NULL;
780}
781
782/* write a config value for the given NID */
783static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
784 unsigned int cfg)
785{
786 int i;
787 for (i = 0; i < 4; i++) {
788 snd_hda_codec_write(codec, nid, 0,
789 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
790 cfg & 0xff);
791 cfg >>= 8;
792 }
793}
794
795/* set the current pin config value for the given NID.
796 * the value is cached, and read via snd_hda_codec_get_pincfg()
797 */
798int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
799 hda_nid_t nid, unsigned int cfg)
800{
801 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100802 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100803
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100804 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100805 pin = look_up_pincfg(codec, list, nid);
806 if (!pin) {
807 pin = snd_array_new(list);
808 if (!pin)
809 return -ENOMEM;
810 pin->nid = nid;
811 }
812 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100813
814 /* change only when needed; e.g. if the pincfg is already present
815 * in user_pins[], don't write it
816 */
817 cfg = snd_hda_codec_get_pincfg(codec, nid);
818 if (oldcfg != cfg)
819 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100820 return 0;
821}
822
823int snd_hda_codec_set_pincfg(struct hda_codec *codec,
824 hda_nid_t nid, unsigned int cfg)
825{
Takashi Iwai346ff702009-02-23 09:42:57 +0100826 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100827}
828EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
829
830/* get the current pin config value of the given pin NID */
831unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
832{
833 struct hda_pincfg *pin;
834
Takashi Iwai3be14142009-02-20 14:11:16 +0100835#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100836 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100837 if (pin)
838 return pin->cfg;
839#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100840 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
841 if (pin)
842 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100843 pin = look_up_pincfg(codec, &codec->init_pins, nid);
844 if (pin)
845 return pin->cfg;
846 return 0;
847}
848EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
849
850/* restore all current pin configs */
851static void restore_pincfgs(struct hda_codec *codec)
852{
853 int i;
854 for (i = 0; i < codec->init_pins.used; i++) {
855 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
856 set_pincfg(codec, pin->nid,
857 snd_hda_codec_get_pincfg(codec, pin->nid));
858 }
859}
Takashi Iwai54d17402005-11-21 16:33:22 +0100860
Takashi Iwai01751f52007-08-10 16:59:39 +0200861static void init_hda_cache(struct hda_cache_rec *cache,
862 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200863static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200864
Takashi Iwai3be14142009-02-20 14:11:16 +0100865/* restore the initial pin cfgs and release all pincfg lists */
866static void restore_init_pincfgs(struct hda_codec *codec)
867{
Takashi Iwai346ff702009-02-23 09:42:57 +0100868 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +0100869 * so that only the values in init_pins are restored
870 */
Takashi Iwai346ff702009-02-23 09:42:57 +0100871 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100872#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100873 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100874#endif
875 restore_pincfgs(codec);
876 snd_array_free(&codec->init_pins);
877}
878
Takashi Iwai54d17402005-11-21 16:33:22 +0100879/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * codec destructor
881 */
882static void snd_hda_codec_free(struct hda_codec *codec)
883{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200884 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return;
Takashi Iwai3be14142009-02-20 14:11:16 +0100886 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200887#ifdef CONFIG_SND_HDA_POWER_SAVE
888 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100889 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200890#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200892 snd_array_free(&codec->mixers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 codec->bus->caddr_tbl[codec->addr] = NULL;
894 if (codec->patch_ops.free)
895 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100896 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200897 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200898 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200899 kfree(codec->vendor_name);
900 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200901 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100902 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 kfree(codec);
904}
905
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100906static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
907 unsigned int power_state);
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/**
910 * snd_hda_codec_new - create a HDA codec
911 * @bus: the bus to assign
912 * @codec_addr: the codec address
913 * @codecp: the pointer to store the generated codec
914 *
915 * Returns 0 if successful, or a negative error code.
916 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100917int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
Takashi Iwaia1e21c92009-06-17 09:33:52 +0200918 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200921 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int err;
923
Takashi Iwaida3cec32008-08-08 17:12:14 +0200924 if (snd_BUG_ON(!bus))
925 return -EINVAL;
926 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
927 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200930 snd_printk(KERN_ERR "hda_codec: "
931 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return -EBUSY;
933 }
934
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200935 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (codec == NULL) {
937 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
938 return -ENOMEM;
939 }
940
941 codec->bus = bus;
942 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100943 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +0800944 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200945 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200946 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwaid13bd412008-07-30 15:01:45 +0200947 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +0100948 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +0100949 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200950 if (codec->bus->modelname) {
951 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
952 if (!codec->modelname) {
953 snd_hda_codec_free(codec);
954 return -ENODEV;
955 }
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Takashi Iwaicb53c622007-08-10 17:21:45 +0200958#ifdef CONFIG_SND_HDA_POWER_SAVE
959 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
960 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
961 * the caller has to power down appropriatley after initialization
962 * phase.
963 */
964 hda_keep_power_on(codec);
965#endif
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 list_add_tail(&codec->list, &bus->codec_list);
968 bus->caddr_tbl[codec_addr] = codec;
969
Takashi Iwai0ba21762007-04-16 11:29:14 +0200970 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
971 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100972 if (codec->vendor_id == -1)
973 /* read again, hopefully the access method was corrected
974 * in the last read...
975 */
976 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
977 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200978 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
979 AC_PAR_SUBSYSTEM_ID);
980 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
981 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200983 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200984 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200985 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +0100986 err = -ENODEV;
987 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
Takashi Iwai3be14142009-02-20 14:11:16 +0100990 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
991 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100992 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +0100993 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +0100994 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100995 err = read_pin_defaults(codec);
996 if (err < 0)
997 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +0100998
Takashi Iwai0ba21762007-04-16 11:29:14 +0200999 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001000 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001001 codec->subsystem_id =
1002 snd_hda_codec_read(codec, nid, 0,
1003 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001004 }
1005
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001006 /* power-up all before initialization */
1007 hda_set_power_state(codec,
1008 codec->afg ? codec->afg : codec->mfg,
1009 AC_PWRST_D0);
1010
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001011 snd_hda_codec_proc_new(codec);
1012
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001013 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001014
1015 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1016 codec->subsystem_id, codec->revision_id);
1017 snd_component_add(codec->bus->card, component);
1018
1019 if (codecp)
1020 *codecp = codec;
1021 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001022
1023 error:
1024 snd_hda_codec_free(codec);
1025 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001026}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001027EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001028
1029int snd_hda_codec_configure(struct hda_codec *codec)
1030{
1031 int err;
1032
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001033 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001034 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001035 err = get_codec_name(codec);
1036 if (err < 0)
1037 return err;
1038 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +02001039 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +02001040 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001041 snprintf(codec->bus->card->mixername,
1042 sizeof(codec->bus->card->mixername),
1043 "%s %s", codec->vendor_name, codec->chip_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Takashi Iwai82467612007-07-27 19:15:54 +02001045 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001047 goto patched;
1048 }
Takashi Iwai82467612007-07-27 19:15:54 +02001049 if (codec->preset && codec->preset->patch) {
1050 err = codec->preset->patch(codec);
1051 goto patched;
1052 }
1053
1054 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001055 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001056 if (err < 0)
1057 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001058
1059 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001060 if (!err && codec->patch_ops.unsol_event)
1061 err = init_unsol_queue(codec->bus);
1062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001064EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066/**
1067 * snd_hda_codec_setup_stream - set up the codec for streaming
1068 * @codec: the CODEC to set up
1069 * @nid: the NID to set up
1070 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1071 * @channel_id: channel id to pass, zero based.
1072 * @format: stream format.
1073 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001074void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1075 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 int channel_id, int format)
1077{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001078 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001079 return;
1080
Takashi Iwai0ba21762007-04-16 11:29:14 +02001081 snd_printdd("hda_codec_setup_stream: "
1082 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 nid, stream_tag, channel_id, format);
1084 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1085 (stream_tag << 4) | channel_id);
1086 msleep(1);
1087 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1088}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001089EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Takashi Iwai888afa12008-03-18 09:57:50 +01001091void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1092{
1093 if (!nid)
1094 return;
1095
1096 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1097 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1098#if 0 /* keep the format */
1099 msleep(1);
1100 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1101#endif
1102}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001103EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/*
1106 * amp access functions
1107 */
1108
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001109/* FIXME: more better hash key? */
1110#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001111#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001112#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1113#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001115#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001118static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001119 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Takashi Iwai01751f52007-08-10 16:59:39 +02001121 memset(cache, 0, sizeof(*cache));
1122 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001123 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001124}
1125
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001126static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001127{
Takashi Iwai603c4012008-07-30 15:01:44 +02001128 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +02001132static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1133 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Takashi Iwai01751f52007-08-10 16:59:39 +02001135 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1136 u16 cur = cache->hash[idx];
1137 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001140 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (info->key == key)
1142 return info;
1143 cur = info->next;
1144 }
1145
1146 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +02001147 info = snd_array_new(&cache->buf);
Takashi Iwaic2174292008-11-07 00:23:30 +01001148 if (!info)
1149 return NULL;
Takashi Iwaif43aa022008-11-10 16:24:26 +01001150 cur = snd_array_index(&cache->buf, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +02001152 info->val = 0;
1153 info->next = cache->hash[idx];
1154 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 return info;
1157}
1158
Takashi Iwai01751f52007-08-10 16:59:39 +02001159/* query and allocate an amp hash entry */
1160static inline struct hda_amp_info *
1161get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1162{
1163 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1164}
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166/*
1167 * query AMP capabilities for the given widget and direction
1168 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001169u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001171 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Takashi Iwai0ba21762007-04-16 11:29:14 +02001173 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1174 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001176 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001177 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001179 info->amp_caps = snd_hda_param_read(codec, nid,
1180 direction == HDA_OUTPUT ?
1181 AC_PAR_AMP_OUT_CAP :
1182 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001183 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001184 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 return info->amp_caps;
1187}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001188EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Takashi Iwai897cc182007-05-29 19:01:37 +02001190int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1191 unsigned int caps)
1192{
1193 struct hda_amp_info *info;
1194
1195 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1196 if (!info)
1197 return -EINVAL;
1198 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001199 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001200 return 0;
1201}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001202EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001203
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001204static unsigned int
1205query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1206 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001207{
1208 struct hda_amp_info *info;
1209
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001210 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001211 if (!info)
1212 return 0;
1213 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001214 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001215 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001216 }
1217 return info->amp_caps;
1218}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001219
1220static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1221{
1222 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1223}
1224
1225u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1226{
1227 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1228 read_pin_cap);
1229}
Takashi Iwai1327a322009-03-23 13:07:47 +01001230EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/*
1233 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001234 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001236static unsigned int get_vol_mute(struct hda_codec *codec,
1237 struct hda_amp_info *info, hda_nid_t nid,
1238 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 u32 val, parm;
1241
Takashi Iwai01751f52007-08-10 16:59:39 +02001242 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001243 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1246 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1247 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001248 val = snd_hda_codec_read(codec, nid, 0,
1249 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001251 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001252 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001256 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001258static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001259 hda_nid_t nid, int ch, int direction, int index,
1260 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 u32 parm;
1263
1264 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1265 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1266 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1267 parm |= val;
1268 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001269 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001273 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Takashi Iwai834be882006-03-01 14:16:17 +01001275int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1276 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001278 struct hda_amp_info *info;
1279 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1280 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001282 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001284EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001286/*
1287 * update the AMP value, mask = bit mask to set, val = the value
1288 */
Takashi Iwai834be882006-03-01 14:16:17 +01001289int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1290 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001293
Takashi Iwai0ba21762007-04-16 11:29:14 +02001294 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1295 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001297 val &= mask;
1298 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001299 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001301 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 1;
1303}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001304EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Takashi Iwai47fd8302007-08-10 17:11:07 +02001306/*
1307 * update the AMP stereo with the same mask and value
1308 */
1309int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1310 int direction, int idx, int mask, int val)
1311{
1312 int ch, ret = 0;
1313 for (ch = 0; ch < 2; ch++)
1314 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1315 idx, mask, val);
1316 return ret;
1317}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001318EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001319
Takashi Iwaicb53c622007-08-10 17:21:45 +02001320#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001321/* resume the all amp commands from the cache */
1322void snd_hda_codec_resume_amp(struct hda_codec *codec)
1323{
Takashi Iwai603c4012008-07-30 15:01:44 +02001324 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001325 int i;
1326
Takashi Iwai603c4012008-07-30 15:01:44 +02001327 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001328 u32 key = buffer->head.key;
1329 hda_nid_t nid;
1330 unsigned int idx, dir, ch;
1331 if (!key)
1332 continue;
1333 nid = key & 0xff;
1334 idx = (key >> 16) & 0xff;
1335 dir = (key >> 24) & 0xff;
1336 for (ch = 0; ch < 2; ch++) {
1337 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1338 continue;
1339 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1340 buffer->vol[ch]);
1341 }
1342 }
1343}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001344EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001345#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001348int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1349 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1352 u16 nid = get_amp_nid(kcontrol);
1353 u8 chs = get_amp_channels(kcontrol);
1354 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001355 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 u32 caps;
1357
1358 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001359 /* num steps */
1360 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1361 if (!caps) {
1362 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001363 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1364 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -EINVAL;
1366 }
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001367 if (ofs < caps)
1368 caps -= ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1370 uinfo->count = chs == 3 ? 2 : 1;
1371 uinfo->value.integer.min = 0;
1372 uinfo->value.integer.max = caps;
1373 return 0;
1374}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001375EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001377
1378static inline unsigned int
1379read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1380 int ch, int dir, int idx, unsigned int ofs)
1381{
1382 unsigned int val;
1383 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1384 val &= HDA_AMP_VOLMASK;
1385 if (val >= ofs)
1386 val -= ofs;
1387 else
1388 val = 0;
1389 return val;
1390}
1391
1392static inline int
1393update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1394 int ch, int dir, int idx, unsigned int ofs,
1395 unsigned int val)
1396{
1397 if (val > 0)
1398 val += ofs;
1399 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1400 HDA_AMP_VOLMASK, val);
1401}
1402
Takashi Iwai0ba21762007-04-16 11:29:14 +02001403int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1404 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1407 hda_nid_t nid = get_amp_nid(kcontrol);
1408 int chs = get_amp_channels(kcontrol);
1409 int dir = get_amp_direction(kcontrol);
1410 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001411 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 long *valp = ucontrol->value.integer.value;
1413
1414 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001415 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001417 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return 0;
1419}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001420EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Takashi Iwai0ba21762007-04-16 11:29:14 +02001422int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1423 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1426 hda_nid_t nid = get_amp_nid(kcontrol);
1427 int chs = get_amp_channels(kcontrol);
1428 int dir = get_amp_direction(kcontrol);
1429 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001430 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 long *valp = ucontrol->value.integer.value;
1432 int change = 0;
1433
Takashi Iwaicb53c622007-08-10 17:21:45 +02001434 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001435 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001436 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001437 valp++;
1438 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001439 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001440 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001441 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return change;
1443}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001444EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001446int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1447 unsigned int size, unsigned int __user *_tlv)
1448{
1449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1450 hda_nid_t nid = get_amp_nid(kcontrol);
1451 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001452 unsigned int ofs = get_amp_offset(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001453 u32 caps, val1, val2;
1454
1455 if (size < 4 * sizeof(unsigned int))
1456 return -ENOMEM;
1457 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001458 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1459 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001460 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001461 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001462 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001463 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1464 return -EFAULT;
1465 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1466 return -EFAULT;
1467 if (put_user(val1, _tlv + 2))
1468 return -EFAULT;
1469 if (put_user(val2, _tlv + 3))
1470 return -EFAULT;
1471 return 0;
1472}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001473EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001474
Takashi Iwai2134ea42008-01-10 16:53:55 +01001475/*
1476 * set (static) TLV for virtual master volume; recalculated as max 0dB
1477 */
1478void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1479 unsigned int *tlv)
1480{
1481 u32 caps;
1482 int nums, step;
1483
1484 caps = query_amp_caps(codec, nid, dir);
1485 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1486 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1487 step = (step + 1) * 25;
1488 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1489 tlv[1] = 2 * sizeof(unsigned int);
1490 tlv[2] = -nums * step;
1491 tlv[3] = step;
1492}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001493EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001494
1495/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001496static struct snd_kcontrol *
1497_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1498 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001499{
1500 struct snd_ctl_elem_id id;
1501 memset(&id, 0, sizeof(id));
1502 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001503 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001504 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1505 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001506 strcpy(id.name, name);
1507 return snd_ctl_find_id(codec->bus->card, &id);
1508}
1509
Takashi Iwai09f99702008-02-04 12:31:13 +01001510struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1511 const char *name)
1512{
1513 return _snd_hda_find_mixer_ctl(codec, name, 0);
1514}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001515EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001516
Takashi Iwaid13bd412008-07-30 15:01:45 +02001517/* Add a control element and assign to the codec */
1518int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1519{
1520 int err;
1521 struct snd_kcontrol **knewp;
1522
1523 err = snd_ctl_add(codec->bus->card, kctl);
1524 if (err < 0)
1525 return err;
1526 knewp = snd_array_new(&codec->mixers);
1527 if (!knewp)
1528 return -ENOMEM;
1529 *knewp = kctl;
1530 return 0;
1531}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001532EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001533
1534/* Clear all controls assigned to the given codec */
1535void snd_hda_ctls_clear(struct hda_codec *codec)
1536{
1537 int i;
1538 struct snd_kcontrol **kctls = codec->mixers.list;
1539 for (i = 0; i < codec->mixers.used; i++)
1540 snd_ctl_remove(codec->bus->card, kctls[i]);
1541 snd_array_free(&codec->mixers);
1542}
1543
Takashi Iwaia65d6292009-02-23 16:57:04 +01001544/* pseudo device locking
1545 * toggle card->shutdown to allow/disallow the device access (as a hack)
1546 */
1547static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001548{
Takashi Iwaia65d6292009-02-23 16:57:04 +01001549 spin_lock(&card->files_lock);
1550 if (card->shutdown) {
1551 spin_unlock(&card->files_lock);
1552 return -EINVAL;
1553 }
1554 card->shutdown = 1;
1555 spin_unlock(&card->files_lock);
1556 return 0;
1557}
1558
1559static void hda_unlock_devices(struct snd_card *card)
1560{
1561 spin_lock(&card->files_lock);
1562 card->shutdown = 0;
1563 spin_unlock(&card->files_lock);
1564}
1565
1566int snd_hda_codec_reset(struct hda_codec *codec)
1567{
1568 struct snd_card *card = codec->bus->card;
1569 int i, pcm;
1570
1571 if (hda_lock_devices(card) < 0)
1572 return -EBUSY;
1573 /* check whether the codec isn't used by any mixer or PCM streams */
1574 if (!list_empty(&card->ctl_files)) {
1575 hda_unlock_devices(card);
1576 return -EBUSY;
1577 }
1578 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1579 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1580 if (!cpcm->pcm)
1581 continue;
1582 if (cpcm->pcm->streams[0].substream_opened ||
1583 cpcm->pcm->streams[1].substream_opened) {
1584 hda_unlock_devices(card);
1585 return -EBUSY;
1586 }
1587 }
1588
1589 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001590
1591#ifdef CONFIG_SND_HDA_POWER_SAVE
1592 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001593 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001594#endif
1595 snd_hda_ctls_clear(codec);
1596 /* relase PCMs */
1597 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001598 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01001599 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001600 clear_bit(codec->pcm_info[i].device,
1601 codec->bus->pcm_dev_bits);
1602 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001603 }
1604 if (codec->patch_ops.free)
1605 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001606 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001607 codec->spec = NULL;
1608 free_hda_cache(&codec->amp_cache);
1609 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001610 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1611 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01001612 /* free only driver_pins so that init_pins + user_pins are restored */
1613 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001614 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001615 codec->num_pcms = 0;
1616 codec->pcm_info = NULL;
1617 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01001618 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1619 codec->slave_dig_outs = NULL;
1620 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001621 module_put(codec->owner);
1622 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001623
1624 /* allow device access again */
1625 hda_unlock_devices(card);
1626 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001627}
1628
Takashi Iwai2134ea42008-01-10 16:53:55 +01001629/* create a virtual master control and add slaves */
1630int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1631 unsigned int *tlv, const char **slaves)
1632{
1633 struct snd_kcontrol *kctl;
1634 const char **s;
1635 int err;
1636
Takashi Iwai2f085542008-02-22 18:43:50 +01001637 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1638 ;
1639 if (!*s) {
1640 snd_printdd("No slave found for %s\n", name);
1641 return 0;
1642 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001643 kctl = snd_ctl_make_virtual_master(name, tlv);
1644 if (!kctl)
1645 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001646 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001647 if (err < 0)
1648 return err;
1649
1650 for (s = slaves; *s; s++) {
1651 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01001652 int i = 0;
1653 for (;;) {
1654 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1655 if (!sctl) {
1656 if (!i)
1657 snd_printdd("Cannot find slave %s, "
1658 "skipped\n", *s);
1659 break;
1660 }
1661 err = snd_ctl_add_slave(kctl, sctl);
1662 if (err < 0)
1663 return err;
1664 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001665 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001666 }
1667 return 0;
1668}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001669EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001672int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1673 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 int chs = get_amp_channels(kcontrol);
1676
1677 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1678 uinfo->count = chs == 3 ? 2 : 1;
1679 uinfo->value.integer.min = 0;
1680 uinfo->value.integer.max = 1;
1681 return 0;
1682}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001683EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Takashi Iwai0ba21762007-04-16 11:29:14 +02001685int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1686 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1689 hda_nid_t nid = get_amp_nid(kcontrol);
1690 int chs = get_amp_channels(kcontrol);
1691 int dir = get_amp_direction(kcontrol);
1692 int idx = get_amp_index(kcontrol);
1693 long *valp = ucontrol->value.integer.value;
1694
1695 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001696 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001697 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001699 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001700 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return 0;
1702}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001703EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Takashi Iwai0ba21762007-04-16 11:29:14 +02001705int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1706 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1709 hda_nid_t nid = get_amp_nid(kcontrol);
1710 int chs = get_amp_channels(kcontrol);
1711 int dir = get_amp_direction(kcontrol);
1712 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 long *valp = ucontrol->value.integer.value;
1714 int change = 0;
1715
Takashi Iwaicb53c622007-08-10 17:21:45 +02001716 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001717 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001718 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001719 HDA_AMP_MUTE,
1720 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001721 valp++;
1722 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001723 if (chs & 2)
1724 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001725 HDA_AMP_MUTE,
1726 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001727#ifdef CONFIG_SND_HDA_POWER_SAVE
1728 if (codec->patch_ops.check_power_status)
1729 codec->patch_ops.check_power_status(codec, nid);
1730#endif
1731 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return change;
1733}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001734EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736/*
Takashi Iwai985be542005-11-02 18:26:49 +01001737 * bound volume controls
1738 *
1739 * bind multiple volumes (# indices, from 0)
1740 */
1741
1742#define AMP_VAL_IDX_SHIFT 19
1743#define AMP_VAL_IDX_MASK (0x0f<<19)
1744
Takashi Iwai0ba21762007-04-16 11:29:14 +02001745int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1746 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001747{
1748 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1749 unsigned long pval;
1750 int err;
1751
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001752 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001753 pval = kcontrol->private_value;
1754 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1755 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1756 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001757 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001758 return err;
1759}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001760EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01001761
Takashi Iwai0ba21762007-04-16 11:29:14 +02001762int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001764{
1765 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1766 unsigned long pval;
1767 int i, indices, err = 0, change = 0;
1768
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001769 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001770 pval = kcontrol->private_value;
1771 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1772 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001773 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1774 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001775 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1776 if (err < 0)
1777 break;
1778 change |= err;
1779 }
1780 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001781 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001782 return err < 0 ? err : change;
1783}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001784EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01001785
1786/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001787 * generic bound volume/swtich controls
1788 */
1789int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1790 struct snd_ctl_elem_info *uinfo)
1791{
1792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1793 struct hda_bind_ctls *c;
1794 int err;
1795
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001796 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001797 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001798 kcontrol->private_value = *c->values;
1799 err = c->ops->info(kcontrol, uinfo);
1800 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001801 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02001802 return err;
1803}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001804EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02001805
1806int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1807 struct snd_ctl_elem_value *ucontrol)
1808{
1809 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1810 struct hda_bind_ctls *c;
1811 int err;
1812
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001813 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001814 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001815 kcontrol->private_value = *c->values;
1816 err = c->ops->get(kcontrol, ucontrol);
1817 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001818 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02001819 return err;
1820}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001821EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02001822
1823int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1824 struct snd_ctl_elem_value *ucontrol)
1825{
1826 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1827 struct hda_bind_ctls *c;
1828 unsigned long *vals;
1829 int err = 0, change = 0;
1830
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001831 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001832 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001833 for (vals = c->values; *vals; vals++) {
1834 kcontrol->private_value = *vals;
1835 err = c->ops->put(kcontrol, ucontrol);
1836 if (err < 0)
1837 break;
1838 change |= err;
1839 }
1840 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001841 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02001842 return err < 0 ? err : change;
1843}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001844EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02001845
1846int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1847 unsigned int size, unsigned int __user *tlv)
1848{
1849 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1850 struct hda_bind_ctls *c;
1851 int err;
1852
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001853 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001854 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001855 kcontrol->private_value = *c->values;
1856 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1857 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001858 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02001859 return err;
1860}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001861EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02001862
1863struct hda_ctl_ops snd_hda_bind_vol = {
1864 .info = snd_hda_mixer_amp_volume_info,
1865 .get = snd_hda_mixer_amp_volume_get,
1866 .put = snd_hda_mixer_amp_volume_put,
1867 .tlv = snd_hda_mixer_amp_tlv
1868};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001869EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02001870
1871struct hda_ctl_ops snd_hda_bind_sw = {
1872 .info = snd_hda_mixer_amp_switch_info,
1873 .get = snd_hda_mixer_amp_switch_get,
1874 .put = snd_hda_mixer_amp_switch_put,
1875 .tlv = snd_hda_mixer_amp_tlv
1876};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001877EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02001878
1879/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 * SPDIF out controls
1881 */
1882
Takashi Iwai0ba21762007-04-16 11:29:14 +02001883static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1884 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
1886 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1887 uinfo->count = 1;
1888 return 0;
1889}
1890
Takashi Iwai0ba21762007-04-16 11:29:14 +02001891static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1892 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1895 IEC958_AES0_NONAUDIO |
1896 IEC958_AES0_CON_EMPHASIS_5015 |
1897 IEC958_AES0_CON_NOT_COPYRIGHT;
1898 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1899 IEC958_AES1_CON_ORIGINAL;
1900 return 0;
1901}
1902
Takashi Iwai0ba21762007-04-16 11:29:14 +02001903static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1904 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1907 IEC958_AES0_NONAUDIO |
1908 IEC958_AES0_PRO_EMPHASIS_5015;
1909 return 0;
1910}
1911
Takashi Iwai0ba21762007-04-16 11:29:14 +02001912static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1913 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1916
1917 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1918 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1919 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1920 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1921
1922 return 0;
1923}
1924
1925/* convert from SPDIF status bits to HDA SPDIF bits
1926 * bit 0 (DigEn) is always set zero (to be filled later)
1927 */
1928static unsigned short convert_from_spdif_status(unsigned int sbits)
1929{
1930 unsigned short val = 0;
1931
1932 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001933 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001935 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001937 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1938 IEC958_AES0_PRO_EMPHASIS_5015)
1939 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001941 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1942 IEC958_AES0_CON_EMPHASIS_5015)
1943 val |= AC_DIG1_EMPHASIS;
1944 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1945 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001947 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1949 }
1950 return val;
1951}
1952
1953/* convert to SPDIF status bits from HDA SPDIF bits
1954 */
1955static unsigned int convert_to_spdif_status(unsigned short val)
1956{
1957 unsigned int sbits = 0;
1958
Takashi Iwai0ba21762007-04-16 11:29:14 +02001959 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001961 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 sbits |= IEC958_AES0_PROFESSIONAL;
1963 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001964 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1966 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001967 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001969 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001971 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1973 sbits |= val & (0x7f << 8);
1974 }
1975 return sbits;
1976}
1977
Takashi Iwai2f728532008-09-25 16:32:41 +02001978/* set digital convert verbs both for the given NID and its slaves */
1979static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1980 int verb, int val)
1981{
1982 hda_nid_t *d;
1983
Takashi Iwai9e976972008-11-25 08:17:20 +01001984 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001985 d = codec->slave_dig_outs;
1986 if (!d)
1987 return;
1988 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01001989 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001990}
1991
1992static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1993 int dig1, int dig2)
1994{
1995 if (dig1 != -1)
1996 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1997 if (dig2 != -1)
1998 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1999}
2000
Takashi Iwai0ba21762007-04-16 11:29:14 +02002001static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2005 hda_nid_t nid = kcontrol->private_value;
2006 unsigned short val;
2007 int change;
2008
Ingo Molnar62932df2006-01-16 16:34:20 +01002009 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 codec->spdif_status = ucontrol->value.iec958.status[0] |
2011 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2012 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2013 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2014 val = convert_from_spdif_status(codec->spdif_status);
2015 val |= codec->spdif_ctls & 1;
2016 change = codec->spdif_ctls != val;
2017 codec->spdif_ctls = val;
2018
Takashi Iwai2f728532008-09-25 16:32:41 +02002019 if (change)
2020 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Ingo Molnar62932df2006-01-16 16:34:20 +01002022 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return change;
2024}
2025
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002026#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Takashi Iwai0ba21762007-04-16 11:29:14 +02002028static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2029 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2032
Takashi Iwai0ba21762007-04-16 11:29:14 +02002033 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return 0;
2035}
2036
Takashi Iwai0ba21762007-04-16 11:29:14 +02002037static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2038 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2041 hda_nid_t nid = kcontrol->private_value;
2042 unsigned short val;
2043 int change;
2044
Ingo Molnar62932df2006-01-16 16:34:20 +01002045 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002046 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002048 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002050 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002052 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002053 /* unmute amp switch (if any) */
2054 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002055 (val & AC_DIG1_ENABLE))
2056 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2057 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002059 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return change;
2061}
2062
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002063static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 {
2065 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2066 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2067 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2068 .info = snd_hda_spdif_mask_info,
2069 .get = snd_hda_spdif_cmask_get,
2070 },
2071 {
2072 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2073 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2074 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2075 .info = snd_hda_spdif_mask_info,
2076 .get = snd_hda_spdif_pmask_get,
2077 },
2078 {
2079 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2080 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2081 .info = snd_hda_spdif_mask_info,
2082 .get = snd_hda_spdif_default_get,
2083 .put = snd_hda_spdif_default_put,
2084 },
2085 {
2086 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2087 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2088 .info = snd_hda_spdif_out_switch_info,
2089 .get = snd_hda_spdif_out_switch_get,
2090 .put = snd_hda_spdif_out_switch_put,
2091 },
2092 { } /* end */
2093};
2094
Takashi Iwai09f99702008-02-04 12:31:13 +01002095#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097/**
2098 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2099 * @codec: the HDA codec
2100 * @nid: audio out widget NID
2101 *
2102 * Creates controls related with the SPDIF output.
2103 * Called from each patch supporting the SPDIF out.
2104 *
2105 * Returns 0 if successful, or a negative error code.
2106 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002107int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002110 struct snd_kcontrol *kctl;
2111 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002112 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Takashi Iwai09f99702008-02-04 12:31:13 +01002114 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2115 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2116 idx))
2117 break;
2118 }
2119 if (idx >= SPDIF_MAX_IDX) {
2120 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2121 return -EBUSY;
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2124 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002125 if (!kctl)
2126 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002127 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002129 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002130 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return err;
2132 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002133 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002134 snd_hda_codec_read(codec, nid, 0,
2135 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2137 return 0;
2138}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002139EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002142 * SPDIF sharing with analog output
2143 */
2144static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2145 struct snd_ctl_elem_value *ucontrol)
2146{
2147 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2148 ucontrol->value.integer.value[0] = mout->share_spdif;
2149 return 0;
2150}
2151
2152static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2153 struct snd_ctl_elem_value *ucontrol)
2154{
2155 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2156 mout->share_spdif = !!ucontrol->value.integer.value[0];
2157 return 0;
2158}
2159
2160static struct snd_kcontrol_new spdif_share_sw = {
2161 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2162 .name = "IEC958 Default PCM Playback Switch",
2163 .info = snd_ctl_boolean_mono_info,
2164 .get = spdif_share_sw_get,
2165 .put = spdif_share_sw_put,
2166};
2167
2168int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2169 struct hda_multi_out *mout)
2170{
2171 if (!mout->dig_out_nid)
2172 return 0;
2173 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002174 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01002175 snd_ctl_new1(&spdif_share_sw, mout));
2176}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002177EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002178
2179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 * SPDIF input
2181 */
2182
2183#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2184
Takashi Iwai0ba21762007-04-16 11:29:14 +02002185static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2186 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2189
2190 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2191 return 0;
2192}
2193
Takashi Iwai0ba21762007-04-16 11:29:14 +02002194static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2198 hda_nid_t nid = kcontrol->private_value;
2199 unsigned int val = !!ucontrol->value.integer.value[0];
2200 int change;
2201
Ingo Molnar62932df2006-01-16 16:34:20 +01002202 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002204 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002206 snd_hda_codec_write_cache(codec, nid, 0,
2207 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002209 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return change;
2211}
2212
Takashi Iwai0ba21762007-04-16 11:29:14 +02002213static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2217 hda_nid_t nid = kcontrol->private_value;
2218 unsigned short val;
2219 unsigned int sbits;
2220
Andrew Paprocki3982d172007-12-19 12:13:44 +01002221 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 sbits = convert_to_spdif_status(val);
2223 ucontrol->value.iec958.status[0] = sbits;
2224 ucontrol->value.iec958.status[1] = sbits >> 8;
2225 ucontrol->value.iec958.status[2] = sbits >> 16;
2226 ucontrol->value.iec958.status[3] = sbits >> 24;
2227 return 0;
2228}
2229
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002230static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 {
2232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2233 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
2234 .info = snd_hda_spdif_in_switch_info,
2235 .get = snd_hda_spdif_in_switch_get,
2236 .put = snd_hda_spdif_in_switch_put,
2237 },
2238 {
2239 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2240 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2241 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
2242 .info = snd_hda_spdif_mask_info,
2243 .get = snd_hda_spdif_in_status_get,
2244 },
2245 { } /* end */
2246};
2247
2248/**
2249 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2250 * @codec: the HDA codec
2251 * @nid: audio in widget NID
2252 *
2253 * Creates controls related with the SPDIF input.
2254 * Called from each patch supporting the SPDIF in.
2255 *
2256 * Returns 0 if successful, or a negative error code.
2257 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002258int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002261 struct snd_kcontrol *kctl;
2262 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002263 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Takashi Iwai09f99702008-02-04 12:31:13 +01002265 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2266 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2267 idx))
2268 break;
2269 }
2270 if (idx >= SPDIF_MAX_IDX) {
2271 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2272 return -EBUSY;
2273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2275 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002276 if (!kctl)
2277 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002279 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002280 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return err;
2282 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002283 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002284 snd_hda_codec_read(codec, nid, 0,
2285 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002286 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return 0;
2288}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002289EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Takashi Iwaicb53c622007-08-10 17:21:45 +02002291#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002292/*
2293 * command cache
2294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002296/* build a 32bit cache key with the widget id and the command parameter */
2297#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2298#define get_cmd_cache_nid(key) ((key) & 0xff)
2299#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2300
2301/**
2302 * snd_hda_codec_write_cache - send a single command with caching
2303 * @codec: the HDA codec
2304 * @nid: NID to send the command
2305 * @direct: direct flag
2306 * @verb: the verb to send
2307 * @parm: the parameter for the verb
2308 *
2309 * Send a single command without waiting for response.
2310 *
2311 * Returns 0 if successful, or a negative error code.
2312 */
2313int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2314 int direct, unsigned int verb, unsigned int parm)
2315{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002316 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2317 struct hda_cache_head *c;
2318 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002319
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002320 if (err < 0)
2321 return err;
2322 /* parm may contain the verb stuff for get/set amp */
2323 verb = verb | (parm >> 8);
2324 parm &= 0xff;
2325 key = build_cmd_cache_key(nid, verb);
2326 mutex_lock(&codec->bus->cmd_mutex);
2327 c = get_alloc_hash(&codec->cmd_cache, key);
2328 if (c)
2329 c->val = parm;
2330 mutex_unlock(&codec->bus->cmd_mutex);
2331 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002332}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002333EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002334
2335/* resume the all commands from the cache */
2336void snd_hda_codec_resume_cache(struct hda_codec *codec)
2337{
Takashi Iwai603c4012008-07-30 15:01:44 +02002338 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002339 int i;
2340
Takashi Iwai603c4012008-07-30 15:01:44 +02002341 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002342 u32 key = buffer->key;
2343 if (!key)
2344 continue;
2345 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2346 get_cmd_cache_cmd(key), buffer->val);
2347 }
2348}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002349EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002350
2351/**
2352 * snd_hda_sequence_write_cache - sequence writes with caching
2353 * @codec: the HDA codec
2354 * @seq: VERB array to send
2355 *
2356 * Send the commands sequentially from the given array.
2357 * Thte commands are recorded on cache for power-save and resume.
2358 * The array must be terminated with NID=0.
2359 */
2360void snd_hda_sequence_write_cache(struct hda_codec *codec,
2361 const struct hda_verb *seq)
2362{
2363 for (; seq->nid; seq++)
2364 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2365 seq->param);
2366}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002367EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002368#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002369
Takashi Iwai54d17402005-11-21 16:33:22 +01002370/*
2371 * set power state of the codec
2372 */
2373static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2374 unsigned int power_state)
2375{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002376 hda_nid_t nid;
2377 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002378
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002379 /* this delay seems necessary to avoid click noise at power-down */
2380 if (power_state == AC_PWRST_D3)
2381 msleep(100);
2382 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01002383 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02002384 /* partial workaround for "azx_get_response timeout" */
2385 if (power_state == AC_PWRST_D0)
2386 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01002387
Takashi Iwaicb53c622007-08-10 17:21:45 +02002388 nid = codec->start_nid;
2389 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002390 unsigned int wcaps = get_wcaps(codec, nid);
2391 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02002392 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002393 if (power_state == AC_PWRST_D3 &&
2394 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002395 unsigned int pincap;
2396 /*
2397 * don't power down the widget if it controls
2398 * eapd and EAPD_BTLENABLE is set.
2399 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01002400 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002401 if (pincap & AC_PINCAP_EAPD) {
2402 int eapd = snd_hda_codec_read(codec,
2403 nid, 0,
2404 AC_VERB_GET_EAPD_BTLENABLE, 0);
2405 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02002406 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002407 continue;
2408 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002409 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002410 snd_hda_codec_write(codec, nid, 0,
2411 AC_VERB_SET_POWER_STATE,
2412 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002413 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002414 }
2415
Takashi Iwaicb53c622007-08-10 17:21:45 +02002416 if (power_state == AC_PWRST_D0) {
2417 unsigned long end_time;
2418 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01002419 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002420 /* wait until the codec reachs to D0 */
2421 end_time = jiffies + msecs_to_jiffies(500);
2422 do {
2423 state = snd_hda_codec_read(codec, fg, 0,
2424 AC_VERB_GET_POWER_STATE, 0);
2425 if (state == power_state)
2426 break;
2427 msleep(1);
2428 } while (time_after_eq(end_time, jiffies));
2429 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002430}
2431
Takashi Iwai11aeff02008-07-30 15:01:46 +02002432#ifdef CONFIG_SND_HDA_HWDEP
2433/* execute additional init verbs */
2434static void hda_exec_init_verbs(struct hda_codec *codec)
2435{
2436 if (codec->init_verbs.list)
2437 snd_hda_sequence_write(codec, codec->init_verbs.list);
2438}
2439#else
2440static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2441#endif
2442
Takashi Iwaicb53c622007-08-10 17:21:45 +02002443#ifdef SND_HDA_NEEDS_RESUME
2444/*
2445 * call suspend and power-down; used both from PM and power-save
2446 */
2447static void hda_call_codec_suspend(struct hda_codec *codec)
2448{
2449 if (codec->patch_ops.suspend)
2450 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2451 hda_set_power_state(codec,
2452 codec->afg ? codec->afg : codec->mfg,
2453 AC_PWRST_D3);
2454#ifdef CONFIG_SND_HDA_POWER_SAVE
2455 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002456 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002457 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002458#endif
2459}
2460
2461/*
2462 * kick up codec; used both from PM and power-save
2463 */
2464static void hda_call_codec_resume(struct hda_codec *codec)
2465{
2466 hda_set_power_state(codec,
2467 codec->afg ? codec->afg : codec->mfg,
2468 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01002469 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwai11aeff02008-07-30 15:01:46 +02002470 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002471 if (codec->patch_ops.resume)
2472 codec->patch_ops.resume(codec);
2473 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002474 if (codec->patch_ops.init)
2475 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002476 snd_hda_codec_resume_amp(codec);
2477 snd_hda_codec_resume_cache(codec);
2478 }
2479}
2480#endif /* SND_HDA_NEEDS_RESUME */
2481
Takashi Iwai54d17402005-11-21 16:33:22 +01002482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483/**
2484 * snd_hda_build_controls - build mixer controls
2485 * @bus: the BUS
2486 *
2487 * Creates mixer controls for each codec included in the bus.
2488 *
2489 * Returns 0 if successful, otherwise a negative error code.
2490 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002491int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002493 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Takashi Iwai0ba21762007-04-16 11:29:14 +02002495 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002496 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01002497 if (err < 0) {
2498 printk(KERN_ERR "hda_codec: cannot build controls"
2499 "for #%d (error %d)\n", codec->addr, err);
2500 err = snd_hda_codec_reset(codec);
2501 if (err < 0) {
2502 printk(KERN_ERR
2503 "hda_codec: cannot revert codec\n");
2504 return err;
2505 }
2506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002508 return 0;
2509}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002510EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002512int snd_hda_codec_build_controls(struct hda_codec *codec)
2513{
2514 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02002515 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002516 /* continue to initialize... */
2517 if (codec->patch_ops.init)
2518 err = codec->patch_ops.init(codec);
2519 if (!err && codec->patch_ops.build_controls)
2520 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002521 if (err < 0)
2522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return 0;
2524}
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526/*
2527 * stream formats
2528 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002529struct hda_rate_tbl {
2530 unsigned int hz;
2531 unsigned int alsa_bits;
2532 unsigned int hda_fmt;
2533};
2534
2535static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002537
2538 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2540 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2541 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2542 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2543 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2544 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2545 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2546 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2547 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2548 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2549 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002550#define AC_PAR_PCM_RATE_BITS 11
2551 /* up to bits 10, 384kHZ isn't supported properly */
2552
2553 /* not autodetected value */
2554 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002555
Takashi Iwaibefdf312005-08-22 13:57:55 +02002556 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557};
2558
2559/**
2560 * snd_hda_calc_stream_format - calculate format bitset
2561 * @rate: the sample rate
2562 * @channels: the number of channels
2563 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2564 * @maxbps: the max. bps
2565 *
2566 * Calculate the format bitset from the given rate, channels and th PCM format.
2567 *
2568 * Return zero if invalid.
2569 */
2570unsigned int snd_hda_calc_stream_format(unsigned int rate,
2571 unsigned int channels,
2572 unsigned int format,
2573 unsigned int maxbps)
2574{
2575 int i;
2576 unsigned int val = 0;
2577
Takashi Iwaibefdf312005-08-22 13:57:55 +02002578 for (i = 0; rate_bits[i].hz; i++)
2579 if (rate_bits[i].hz == rate) {
2580 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 break;
2582 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002583 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 snd_printdd("invalid rate %d\n", rate);
2585 return 0;
2586 }
2587
2588 if (channels == 0 || channels > 8) {
2589 snd_printdd("invalid channels %d\n", channels);
2590 return 0;
2591 }
2592 val |= channels - 1;
2593
2594 switch (snd_pcm_format_width(format)) {
2595 case 8: val |= 0x00; break;
2596 case 16: val |= 0x10; break;
2597 case 20:
2598 case 24:
2599 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02002600 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 val |= 0x40;
2602 else if (maxbps >= 24)
2603 val |= 0x30;
2604 else
2605 val |= 0x20;
2606 break;
2607 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002608 snd_printdd("invalid format width %d\n",
2609 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return 0;
2611 }
2612
2613 return val;
2614}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002615EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002617static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2618{
2619 unsigned int val = 0;
2620 if (nid != codec->afg &&
2621 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
2622 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2623 if (!val || val == -1)
2624 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2625 if (!val || val == -1)
2626 return 0;
2627 return val;
2628}
2629
2630static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2631{
2632 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
2633 get_pcm_param);
2634}
2635
2636static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
2637{
2638 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2639 if (!streams || streams == -1)
2640 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2641 if (!streams || streams == -1)
2642 return 0;
2643 return streams;
2644}
2645
2646static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
2647{
2648 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
2649 get_stream_param);
2650}
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652/**
2653 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2654 * @codec: the HDA codec
2655 * @nid: NID to query
2656 * @ratesp: the pointer to store the detected rate bitflags
2657 * @formatsp: the pointer to store the detected formats
2658 * @bpsp: the pointer to store the detected format widths
2659 *
2660 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2661 * or @bsps argument is ignored.
2662 *
2663 * Returns 0 if successful, otherwise a negative error code.
2664 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01002665static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2667{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01002668 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Jaroslav Kyselaee504712009-03-17 14:30:31 +01002670 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002671 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 if (ratesp) {
2674 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002675 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002677 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01002679 if (rates == 0) {
2680 snd_printk(KERN_ERR "hda_codec: rates == 0 "
2681 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
2682 nid, val,
2683 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
2684 return -EIO;
2685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 *ratesp = rates;
2687 }
2688
2689 if (formatsp || bpsp) {
2690 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01002691 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002693 streams = query_stream_param(codec, nid);
2694 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 bps = 0;
2698 if (streams & AC_SUPFMT_PCM) {
2699 if (val & AC_SUPPCM_BITS_8) {
2700 formats |= SNDRV_PCM_FMTBIT_U8;
2701 bps = 8;
2702 }
2703 if (val & AC_SUPPCM_BITS_16) {
2704 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2705 bps = 16;
2706 }
2707 if (wcaps & AC_WCAP_DIGITAL) {
2708 if (val & AC_SUPPCM_BITS_32)
2709 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2710 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2711 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2712 if (val & AC_SUPPCM_BITS_24)
2713 bps = 24;
2714 else if (val & AC_SUPPCM_BITS_20)
2715 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002716 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2717 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2719 if (val & AC_SUPPCM_BITS_32)
2720 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 else if (val & AC_SUPPCM_BITS_24)
2722 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002723 else if (val & AC_SUPPCM_BITS_20)
2724 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02002727 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02002729 if (!bps)
2730 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02002731 }
2732 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002733 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /* temporary hack: we have still no proper support
2735 * for the direct AC3 stream...
2736 */
2737 formats |= SNDRV_PCM_FMTBIT_U8;
2738 bps = 8;
2739 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01002740 if (formats == 0) {
2741 snd_printk(KERN_ERR "hda_codec: formats == 0 "
2742 "(nid=0x%x, val=0x%x, ovrd=%i, "
2743 "streams=0x%x)\n",
2744 nid, val,
2745 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
2746 streams);
2747 return -EIO;
2748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (formatsp)
2750 *formatsp = formats;
2751 if (bpsp)
2752 *bpsp = bps;
2753 }
2754
2755 return 0;
2756}
2757
2758/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002759 * snd_hda_is_supported_format - check whether the given node supports
2760 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 *
2762 * Returns 1 if supported, 0 if not.
2763 */
2764int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2765 unsigned int format)
2766{
2767 int i;
2768 unsigned int val = 0, rate, stream;
2769
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002770 val = query_pcm_param(codec, nid);
2771 if (!val)
2772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002775 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002776 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 if (val & (1 << i))
2778 break;
2779 return 0;
2780 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002781 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return 0;
2783
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002784 stream = query_stream_param(codec, nid);
2785 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return 0;
2787
2788 if (stream & AC_SUPFMT_PCM) {
2789 switch (format & 0xf0) {
2790 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002791 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return 0;
2793 break;
2794 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002795 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return 0;
2797 break;
2798 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002799 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return 0;
2801 break;
2802 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002803 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return 0;
2805 break;
2806 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002807 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return 0;
2809 break;
2810 default:
2811 return 0;
2812 }
2813 } else {
2814 /* FIXME: check for float32 and AC3? */
2815 }
2816
2817 return 1;
2818}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002819EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821/*
2822 * PCM stuff
2823 */
2824static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2825 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002826 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
2828 return 0;
2829}
2830
2831static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2832 struct hda_codec *codec,
2833 unsigned int stream_tag,
2834 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002835 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2838 return 0;
2839}
2840
2841static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2842 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002843 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Takashi Iwai888afa12008-03-18 09:57:50 +01002845 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return 0;
2847}
2848
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002849static int set_pcm_default_values(struct hda_codec *codec,
2850 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01002852 int err;
2853
Takashi Iwai0ba21762007-04-16 11:29:14 +02002854 /* query support PCM information from the given NID */
2855 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01002856 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002857 info->rates ? NULL : &info->rates,
2858 info->formats ? NULL : &info->formats,
2859 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01002860 if (err < 0)
2861 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
2863 if (info->ops.open == NULL)
2864 info->ops.open = hda_pcm_default_open_close;
2865 if (info->ops.close == NULL)
2866 info->ops.close = hda_pcm_default_open_close;
2867 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002868 if (snd_BUG_ON(!info->nid))
2869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 info->ops.prepare = hda_pcm_default_prepare;
2871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002873 if (snd_BUG_ON(!info->nid))
2874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 info->ops.cleanup = hda_pcm_default_cleanup;
2876 }
2877 return 0;
2878}
2879
Takashi Iwai176d5332008-07-30 15:01:44 +02002880/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002881 * get the empty PCM device number to assign
2882 */
2883static int get_empty_pcm_device(struct hda_bus *bus, int type)
2884{
2885 static const char *dev_name[HDA_PCM_NTYPES] = {
2886 "Audio", "SPDIF", "HDMI", "Modem"
2887 };
2888 /* starting device index for each PCM type */
2889 static int dev_idx[HDA_PCM_NTYPES] = {
2890 [HDA_PCM_TYPE_AUDIO] = 0,
2891 [HDA_PCM_TYPE_SPDIF] = 1,
2892 [HDA_PCM_TYPE_HDMI] = 3,
2893 [HDA_PCM_TYPE_MODEM] = 6
2894 };
2895 /* normal audio device indices; not linear to keep compatibility */
2896 static int audio_idx[4] = { 0, 2, 4, 5 };
2897 int i, dev;
2898
2899 switch (type) {
2900 case HDA_PCM_TYPE_AUDIO:
2901 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2902 dev = audio_idx[i];
2903 if (!test_bit(dev, bus->pcm_dev_bits))
Takashi Iwai82ad39f2009-03-03 15:00:35 +01002904 goto ok;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002905 }
Takashi Iwai82ad39f2009-03-03 15:00:35 +01002906 snd_printk(KERN_WARNING "Too many audio devices\n");
2907 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002908 case HDA_PCM_TYPE_SPDIF:
2909 case HDA_PCM_TYPE_HDMI:
2910 case HDA_PCM_TYPE_MODEM:
2911 dev = dev_idx[type];
2912 if (test_bit(dev, bus->pcm_dev_bits)) {
2913 snd_printk(KERN_WARNING "%s already defined\n",
2914 dev_name[type]);
2915 return -EAGAIN;
2916 }
2917 break;
2918 default:
2919 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2920 return -EINVAL;
2921 }
Takashi Iwai82ad39f2009-03-03 15:00:35 +01002922 ok:
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002923 set_bit(dev, bus->pcm_dev_bits);
2924 return dev;
2925}
2926
2927/*
Takashi Iwai176d5332008-07-30 15:01:44 +02002928 * attach a new PCM stream
2929 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002930static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02002931{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002932 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02002933 struct hda_pcm_stream *info;
2934 int stream, err;
2935
Takashi Iwaib91f0802008-11-04 08:43:08 +01002936 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02002937 return -EINVAL;
2938 for (stream = 0; stream < 2; stream++) {
2939 info = &pcm->stream[stream];
2940 if (info->substreams) {
2941 err = set_pcm_default_values(codec, info);
2942 if (err < 0)
2943 return err;
2944 }
2945 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002946 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02002947}
2948
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002949/* assign all PCMs of the given codec */
2950int snd_hda_codec_build_pcms(struct hda_codec *codec)
2951{
2952 unsigned int pcm;
2953 int err;
2954
2955 if (!codec->num_pcms) {
2956 if (!codec->patch_ops.build_pcms)
2957 return 0;
2958 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01002959 if (err < 0) {
2960 printk(KERN_ERR "hda_codec: cannot build PCMs"
2961 "for #%d (error %d)\n", codec->addr, err);
2962 err = snd_hda_codec_reset(codec);
2963 if (err < 0) {
2964 printk(KERN_ERR
2965 "hda_codec: cannot revert codec\n");
2966 return err;
2967 }
2968 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002969 }
2970 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2971 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2972 int dev;
2973
2974 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01002975 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002976
2977 if (!cpcm->pcm) {
2978 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2979 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01002980 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002981 cpcm->device = dev;
2982 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01002983 if (err < 0) {
2984 printk(KERN_ERR "hda_codec: cannot attach "
2985 "PCM stream %d for codec #%d\n",
2986 dev, codec->addr);
2987 continue; /* no fatal error */
2988 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002989 }
2990 }
2991 return 0;
2992}
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994/**
2995 * snd_hda_build_pcms - build PCM information
2996 * @bus: the BUS
2997 *
2998 * Create PCM information for each codec included in the bus.
2999 *
3000 * The build_pcms codec patch is requested to set up codec->num_pcms and
3001 * codec->pcm_info properly. The array is referred by the top-level driver
3002 * to create its PCM instances.
3003 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3004 * callback.
3005 *
3006 * At least, substreams, channels_min and channels_max must be filled for
3007 * each stream. substreams = 0 indicates that the stream doesn't exist.
3008 * When rates and/or formats are zero, the supported values are queried
3009 * from the given nid. The nid is used also by the default ops.prepare
3010 * and ops.cleanup callbacks.
3011 *
3012 * The driver needs to call ops.open in its open callback. Similarly,
3013 * ops.close is supposed to be called in the close callback.
3014 * ops.prepare should be called in the prepare or hw_params callback
3015 * with the proper parameters for set up.
3016 * ops.cleanup should be called in hw_free for clean up of streams.
3017 *
3018 * This function returns 0 if successfull, or a negative error code.
3019 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003020int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003022 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Takashi Iwai0ba21762007-04-16 11:29:14 +02003024 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003025 int err = snd_hda_codec_build_pcms(codec);
3026 if (err < 0)
3027 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029 return 0;
3030}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003031EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033/**
3034 * snd_hda_check_board_config - compare the current codec with the config table
3035 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003036 * @num_configs: number of config enums
3037 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 * @tbl: configuration table, terminated by null entries
3039 *
3040 * Compares the modelname or PCI subsystem id of the current codec with the
3041 * given configuration table. If a matching entry is found, returns its
3042 * config value (supposed to be 0 or positive).
3043 *
3044 * If no entries are matching, the function returns a negative value.
3045 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003046int snd_hda_check_board_config(struct hda_codec *codec,
3047 int num_configs, const char **models,
3048 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003050 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003051 int i;
3052 for (i = 0; i < num_configs; i++) {
3053 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003054 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003055 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3056 "selected\n", models[i]);
3057 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
3059 }
3060 }
3061
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003062 if (!codec->bus->pci || !tbl)
3063 return -1;
3064
3065 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3066 if (!tbl)
3067 return -1;
3068 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003069#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003070 char tmp[10];
3071 const char *model = NULL;
3072 if (models)
3073 model = models[tbl->value];
3074 if (!model) {
3075 sprintf(tmp, "#%d", tbl->value);
3076 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003078 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3079 "for config %x:%x (%s)\n",
3080 model, tbl->subvendor, tbl->subdevice,
3081 (tbl->name ? tbl->name : "Unknown device"));
3082#endif
3083 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
3085 return -1;
3086}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003087EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003090 * snd_hda_check_board_codec_sid_config - compare the current codec
3091 subsystem ID with the
3092 config table
3093
3094 This is important for Gateway notebooks with SB450 HDA Audio
3095 where the vendor ID of the PCI device is:
3096 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3097 and the vendor/subvendor are found only at the codec.
3098
3099 * @codec: the HDA codec
3100 * @num_configs: number of config enums
3101 * @models: array of model name strings
3102 * @tbl: configuration table, terminated by null entries
3103 *
3104 * Compares the modelname or PCI subsystem id of the current codec with the
3105 * given configuration table. If a matching entry is found, returns its
3106 * config value (supposed to be 0 or positive).
3107 *
3108 * If no entries are matching, the function returns a negative value.
3109 */
3110int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3111 int num_configs, const char **models,
3112 const struct snd_pci_quirk *tbl)
3113{
3114 const struct snd_pci_quirk *q;
3115
3116 /* Search for codec ID */
3117 for (q = tbl; q->subvendor; q++) {
3118 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3119
3120 if (vendorid == codec->subsystem_id)
3121 break;
3122 }
3123
3124 if (!q->subvendor)
3125 return -1;
3126
3127 tbl = q;
3128
3129 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003130#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003131 char tmp[10];
3132 const char *model = NULL;
3133 if (models)
3134 model = models[tbl->value];
3135 if (!model) {
3136 sprintf(tmp, "#%d", tbl->value);
3137 model = tmp;
3138 }
3139 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3140 "for config %x:%x (%s)\n",
3141 model, tbl->subvendor, tbl->subdevice,
3142 (tbl->name ? tbl->name : "Unknown device"));
3143#endif
3144 return tbl->value;
3145 }
3146 return -1;
3147}
3148EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3149
3150/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 * snd_hda_add_new_ctls - create controls from the array
3152 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003153 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 *
3155 * This helper function creates and add new controls in the given array.
3156 * The array must be terminated with an empty entry as terminator.
3157 *
3158 * Returns 0 if successful, or a negative error code.
3159 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003160int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003162 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003165 struct snd_kcontrol *kctl;
3166 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003167 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003168 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02003169 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01003170 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003171 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01003172 return err;
3173 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003174 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003175 return -ENOMEM;
3176 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02003177 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003178 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01003179 return err;
3180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 }
3182 return 0;
3183}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003184EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Takashi Iwaicb53c622007-08-10 17:21:45 +02003186#ifdef CONFIG_SND_HDA_POWER_SAVE
3187static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3188 unsigned int power_state);
3189
3190static void hda_power_work(struct work_struct *work)
3191{
3192 struct hda_codec *codec =
3193 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003194 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003195
Maxim Levitsky2e492462007-09-03 15:26:57 +02003196 if (!codec->power_on || codec->power_count) {
3197 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003198 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003199 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003200
3201 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003202 if (bus->ops.pm_notify)
3203 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003204}
3205
3206static void hda_keep_power_on(struct hda_codec *codec)
3207{
3208 codec->power_count++;
3209 codec->power_on = 1;
3210}
3211
3212void snd_hda_power_up(struct hda_codec *codec)
3213{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003214 struct hda_bus *bus = codec->bus;
3215
Takashi Iwaicb53c622007-08-10 17:21:45 +02003216 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003217 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003218 return;
3219
3220 codec->power_on = 1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003221 if (bus->ops.pm_notify)
3222 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003223 hda_call_codec_resume(codec);
3224 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003225 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003226}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003227EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003228
3229#define power_save(codec) \
3230 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003231
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003232#define power_save(codec) \
3233 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3234
Takashi Iwaicb53c622007-08-10 17:21:45 +02003235void snd_hda_power_down(struct hda_codec *codec)
3236{
3237 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003238 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003239 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003240 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003241 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003242 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003243 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003244 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003245}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003246EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003247
3248int snd_hda_check_amp_list_power(struct hda_codec *codec,
3249 struct hda_loopback_check *check,
3250 hda_nid_t nid)
3251{
3252 struct hda_amp_list *p;
3253 int ch, v;
3254
3255 if (!check->amplist)
3256 return 0;
3257 for (p = check->amplist; p->nid; p++) {
3258 if (p->nid == nid)
3259 break;
3260 }
3261 if (!p->nid)
3262 return 0; /* nothing changed */
3263
3264 for (p = check->amplist; p->nid; p++) {
3265 for (ch = 0; ch < 2; ch++) {
3266 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3267 p->idx);
3268 if (!(v & HDA_AMP_MUTE) && v > 0) {
3269 if (!check->power_on) {
3270 check->power_on = 1;
3271 snd_hda_power_up(codec);
3272 }
3273 return 1;
3274 }
3275 }
3276 }
3277 if (check->power_on) {
3278 check->power_on = 0;
3279 snd_hda_power_down(codec);
3280 }
3281 return 0;
3282}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003283EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003284#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003286/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003287 * Channel mode helper
3288 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003289int snd_hda_ch_mode_info(struct hda_codec *codec,
3290 struct snd_ctl_elem_info *uinfo,
3291 const struct hda_channel_mode *chmode,
3292 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003293{
3294 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3295 uinfo->count = 1;
3296 uinfo->value.enumerated.items = num_chmodes;
3297 if (uinfo->value.enumerated.item >= num_chmodes)
3298 uinfo->value.enumerated.item = num_chmodes - 1;
3299 sprintf(uinfo->value.enumerated.name, "%dch",
3300 chmode[uinfo->value.enumerated.item].channels);
3301 return 0;
3302}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003303EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003304
Takashi Iwai0ba21762007-04-16 11:29:14 +02003305int snd_hda_ch_mode_get(struct hda_codec *codec,
3306 struct snd_ctl_elem_value *ucontrol,
3307 const struct hda_channel_mode *chmode,
3308 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003309 int max_channels)
3310{
3311 int i;
3312
3313 for (i = 0; i < num_chmodes; i++) {
3314 if (max_channels == chmode[i].channels) {
3315 ucontrol->value.enumerated.item[0] = i;
3316 break;
3317 }
3318 }
3319 return 0;
3320}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003321EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003322
Takashi Iwai0ba21762007-04-16 11:29:14 +02003323int snd_hda_ch_mode_put(struct hda_codec *codec,
3324 struct snd_ctl_elem_value *ucontrol,
3325 const struct hda_channel_mode *chmode,
3326 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003327 int *max_channelsp)
3328{
3329 unsigned int mode;
3330
3331 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01003332 if (mode >= num_chmodes)
3333 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003334 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003335 return 0;
3336 /* change the current channel setting */
3337 *max_channelsp = chmode[mode].channels;
3338 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003339 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003340 return 1;
3341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003342EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01003343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344/*
3345 * input MUX helper
3346 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003347int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3348 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
3350 unsigned int index;
3351
3352 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3353 uinfo->count = 1;
3354 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003355 if (!imux->num_items)
3356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 index = uinfo->value.enumerated.item;
3358 if (index >= imux->num_items)
3359 index = imux->num_items - 1;
3360 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3361 return 0;
3362}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003363EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Takashi Iwai0ba21762007-04-16 11:29:14 +02003365int snd_hda_input_mux_put(struct hda_codec *codec,
3366 const struct hda_input_mux *imux,
3367 struct snd_ctl_elem_value *ucontrol,
3368 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 unsigned int *cur_val)
3370{
3371 unsigned int idx;
3372
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003373 if (!imux->num_items)
3374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 idx = ucontrol->value.enumerated.item[0];
3376 if (idx >= imux->num_items)
3377 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003378 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003380 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3381 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 *cur_val = idx;
3383 return 1;
3384}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003385EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
3387
3388/*
3389 * Multi-channel / digital-out PCM helper functions
3390 */
3391
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003392/* setup SPDIF output stream */
3393static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3394 unsigned int stream_tag, unsigned int format)
3395{
3396 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02003397 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3398 set_dig_out_convert(codec, nid,
3399 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3400 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003401 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003402 if (codec->slave_dig_outs) {
3403 hda_nid_t *d;
3404 for (d = codec->slave_dig_outs; *d; d++)
3405 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3406 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003407 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003408 /* turn on again (if needed) */
3409 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3410 set_dig_out_convert(codec, nid,
3411 codec->spdif_ctls & 0xff, -1);
3412}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003413
Takashi Iwai2f728532008-09-25 16:32:41 +02003414static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3415{
3416 snd_hda_codec_cleanup_stream(codec, nid);
3417 if (codec->slave_dig_outs) {
3418 hda_nid_t *d;
3419 for (d = codec->slave_dig_outs; *d; d++)
3420 snd_hda_codec_cleanup_stream(codec, *d);
3421 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003422}
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424/*
3425 * open the digital out in the exclusive mode
3426 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003427int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3428 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
Ingo Molnar62932df2006-01-16 16:34:20 +01003430 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003431 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3432 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003433 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003435 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return 0;
3437}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003438EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003440int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3441 struct hda_multi_out *mout,
3442 unsigned int stream_tag,
3443 unsigned int format,
3444 struct snd_pcm_substream *substream)
3445{
3446 mutex_lock(&codec->spdif_mutex);
3447 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3448 mutex_unlock(&codec->spdif_mutex);
3449 return 0;
3450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003451EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003452
Takashi Iwai9411e212009-02-13 11:32:28 +01003453int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3454 struct hda_multi_out *mout)
3455{
3456 mutex_lock(&codec->spdif_mutex);
3457 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3458 mutex_unlock(&codec->spdif_mutex);
3459 return 0;
3460}
3461EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463/*
3464 * release the digital out
3465 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003466int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3467 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Ingo Molnar62932df2006-01-16 16:34:20 +01003469 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003471 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 return 0;
3473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003474EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476/*
3477 * set up more restrictions for analog out
3478 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003479int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3480 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003481 struct snd_pcm_substream *substream,
3482 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Takashi Iwai9a081602008-02-12 18:37:26 +01003484 struct snd_pcm_runtime *runtime = substream->runtime;
3485 runtime->hw.channels_max = mout->max_channels;
3486 if (mout->dig_out_nid) {
3487 if (!mout->analog_rates) {
3488 mout->analog_rates = hinfo->rates;
3489 mout->analog_formats = hinfo->formats;
3490 mout->analog_maxbps = hinfo->maxbps;
3491 } else {
3492 runtime->hw.rates = mout->analog_rates;
3493 runtime->hw.formats = mout->analog_formats;
3494 hinfo->maxbps = mout->analog_maxbps;
3495 }
3496 if (!mout->spdif_rates) {
3497 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3498 &mout->spdif_rates,
3499 &mout->spdif_formats,
3500 &mout->spdif_maxbps);
3501 }
3502 mutex_lock(&codec->spdif_mutex);
3503 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02003504 if ((runtime->hw.rates & mout->spdif_rates) &&
3505 (runtime->hw.formats & mout->spdif_formats)) {
3506 runtime->hw.rates &= mout->spdif_rates;
3507 runtime->hw.formats &= mout->spdif_formats;
3508 if (mout->spdif_maxbps < hinfo->maxbps)
3509 hinfo->maxbps = mout->spdif_maxbps;
3510 } else {
3511 mout->share_spdif = 0;
3512 /* FIXME: need notify? */
3513 }
Takashi Iwai9a081602008-02-12 18:37:26 +01003514 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003515 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3518 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3519}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003520EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522/*
3523 * set up the i/o for analog out
3524 * when the digital out is available, copy the front out to digital out, too.
3525 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003526int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3527 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 unsigned int stream_tag,
3529 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003530 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
3532 hda_nid_t *nids = mout->dac_nids;
3533 int chs = substream->runtime->channels;
3534 int i;
3535
Ingo Molnar62932df2006-01-16 16:34:20 +01003536 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003537 if (mout->dig_out_nid && mout->share_spdif &&
3538 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003540 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3541 format) &&
3542 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003544 setup_dig_out_stream(codec, mout->dig_out_nid,
3545 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 } else {
3547 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003548 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
3550 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003551 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003554 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3555 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003556 if (!mout->no_share_stream &&
3557 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003559 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3560 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003561 /* extra outputs copied from front */
3562 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02003563 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01003564 snd_hda_codec_setup_stream(codec,
3565 mout->extra_out_nid[i],
3566 stream_tag, 0, format);
3567
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 /* surrounds */
3569 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003570 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003571 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3572 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003573 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003574 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3575 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
3577 return 0;
3578}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003579EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581/*
3582 * clean up the setting for analog out
3583 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003584int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3585 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
3587 hda_nid_t *nids = mout->dac_nids;
3588 int i;
3589
3590 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003591 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003593 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003594 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3595 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003596 snd_hda_codec_cleanup_stream(codec,
3597 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003598 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003600 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 mout->dig_out_used = 0;
3602 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003603 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return 0;
3605}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003606EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003608/*
Wu Fengguang6b345002008-10-07 14:21:41 +08003609 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003610 */
Kailang Yangdf694da2005-12-05 19:42:22 +01003611
Takashi Iwai12f288b2007-08-02 15:51:59 +02003612static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01003613{
3614 for (; *list; list++)
3615 if (*list == nid)
3616 return 1;
3617 return 0;
3618}
3619
Steve Longerbeam81937d32007-05-08 15:33:03 +02003620
3621/*
3622 * Sort an associated group of pins according to their sequence numbers.
3623 */
3624static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3625 int num_pins)
3626{
3627 int i, j;
3628 short seq;
3629 hda_nid_t nid;
3630
3631 for (i = 0; i < num_pins; i++) {
3632 for (j = i + 1; j < num_pins; j++) {
3633 if (sequences[i] > sequences[j]) {
3634 seq = sequences[i];
3635 sequences[i] = sequences[j];
3636 sequences[j] = seq;
3637 nid = pins[i];
3638 pins[i] = pins[j];
3639 pins[j] = nid;
3640 }
3641 }
3642 }
3643}
3644
3645
Takashi Iwai82bc9552006-03-21 11:24:42 +01003646/*
3647 * Parse all pin widgets and store the useful pin nids to cfg
3648 *
3649 * The number of line-outs or any primary output is stored in line_outs,
3650 * and the corresponding output pins are assigned to line_out_pins[],
3651 * in the order of front, rear, CLFE, side, ...
3652 *
3653 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003654 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01003655 * is detected, one of speaker of HP pins is assigned as the primary
3656 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3657 * if any analog output exists.
3658 *
3659 * The analog input pins are assigned to input_pins array.
3660 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3661 * respectively.
3662 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003663int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3664 struct auto_pin_cfg *cfg,
3665 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003666{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003667 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003668 short seq, assoc_line_out, assoc_speaker;
3669 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3670 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01003671 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003672
3673 memset(cfg, 0, sizeof(*cfg));
3674
Steve Longerbeam81937d32007-05-08 15:33:03 +02003675 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3676 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01003677 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02003678 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003679
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003680 end_nid = codec->start_nid + codec->num_nodes;
3681 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003682 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02003683 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003684 unsigned int def_conf;
3685 short assoc, loc;
3686
3687 /* read all default configuration for pin complex */
3688 if (wid_type != AC_WID_PIN)
3689 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003690 /* ignore the given nids (e.g. pc-beep returns error) */
3691 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3692 continue;
3693
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01003694 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003695 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3696 continue;
3697 loc = get_defcfg_location(def_conf);
3698 switch (get_defcfg_device(def_conf)) {
3699 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003700 seq = get_defcfg_sequence(def_conf);
3701 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003702
3703 if (!(wid_caps & AC_WCAP_STEREO))
3704 if (!cfg->mono_out_pin)
3705 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003706 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003707 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003708 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003709 assoc_line_out = assoc;
3710 else if (assoc_line_out != assoc)
3711 continue;
3712 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3713 continue;
3714 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003715 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003716 cfg->line_outs++;
3717 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003718 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003719 seq = get_defcfg_sequence(def_conf);
3720 assoc = get_defcfg_association(def_conf);
3721 if (! assoc)
3722 continue;
3723 if (! assoc_speaker)
3724 assoc_speaker = assoc;
3725 else if (assoc_speaker != assoc)
3726 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003727 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3728 continue;
3729 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003730 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003731 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003732 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003733 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003734 seq = get_defcfg_sequence(def_conf);
3735 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003736 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3737 continue;
3738 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003739 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003740 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003741 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003742 case AC_JACK_MIC_IN: {
3743 int preferred, alt;
3744 if (loc == AC_JACK_LOC_FRONT) {
3745 preferred = AUTO_PIN_FRONT_MIC;
3746 alt = AUTO_PIN_MIC;
3747 } else {
3748 preferred = AUTO_PIN_MIC;
3749 alt = AUTO_PIN_FRONT_MIC;
3750 }
3751 if (!cfg->input_pins[preferred])
3752 cfg->input_pins[preferred] = nid;
3753 else if (!cfg->input_pins[alt])
3754 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003755 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003756 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003757 case AC_JACK_LINE_IN:
3758 if (loc == AC_JACK_LOC_FRONT)
3759 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3760 else
3761 cfg->input_pins[AUTO_PIN_LINE] = nid;
3762 break;
3763 case AC_JACK_CD:
3764 cfg->input_pins[AUTO_PIN_CD] = nid;
3765 break;
3766 case AC_JACK_AUX:
3767 cfg->input_pins[AUTO_PIN_AUX] = nid;
3768 break;
3769 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01003770 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01003771 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
3772 continue;
3773 cfg->dig_out_pins[cfg->dig_outs] = nid;
3774 cfg->dig_out_type[cfg->dig_outs] =
3775 (loc == AC_JACK_LOC_HDMI) ?
3776 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
3777 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003778 break;
3779 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01003780 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003781 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01003782 if (loc == AC_JACK_LOC_HDMI)
3783 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
3784 else
3785 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003786 break;
3787 }
3788 }
3789
Takashi Iwai5832fcf82008-02-12 18:30:12 +01003790 /* FIX-UP:
3791 * If no line-out is defined but multiple HPs are found,
3792 * some of them might be the real line-outs.
3793 */
3794 if (!cfg->line_outs && cfg->hp_outs > 1) {
3795 int i = 0;
3796 while (i < cfg->hp_outs) {
3797 /* The real HPs should have the sequence 0x0f */
3798 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3799 i++;
3800 continue;
3801 }
3802 /* Move it to the line-out table */
3803 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3804 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3805 cfg->line_outs++;
3806 cfg->hp_outs--;
3807 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3808 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3809 memmove(sequences_hp + i - 1, sequences_hp + i,
3810 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3811 }
3812 }
3813
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003814 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003815 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3816 cfg->line_outs);
3817 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3818 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003819 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3820 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003821
Takashi Iwaif889fa92007-10-31 15:49:32 +01003822 /* if we have only one mic, make it AUTO_PIN_MIC */
3823 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3824 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3825 cfg->input_pins[AUTO_PIN_MIC] =
3826 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3827 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3828 }
3829 /* ditto for line-in */
3830 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3831 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3832 cfg->input_pins[AUTO_PIN_LINE] =
3833 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3834 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3835 }
3836
Steve Longerbeam81937d32007-05-08 15:33:03 +02003837 /*
3838 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3839 * as a primary output
3840 */
3841 if (!cfg->line_outs) {
3842 if (cfg->speaker_outs) {
3843 cfg->line_outs = cfg->speaker_outs;
3844 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3845 sizeof(cfg->speaker_pins));
3846 cfg->speaker_outs = 0;
3847 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3848 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3849 } else if (cfg->hp_outs) {
3850 cfg->line_outs = cfg->hp_outs;
3851 memcpy(cfg->line_out_pins, cfg->hp_pins,
3852 sizeof(cfg->hp_pins));
3853 cfg->hp_outs = 0;
3854 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3855 cfg->line_out_type = AUTO_PIN_HP_OUT;
3856 }
3857 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003858
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003859 /* Reorder the surround channels
3860 * ALSA sequence is front/surr/clfe/side
3861 * HDA sequence is:
3862 * 4-ch: front/surr => OK as it is
3863 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003864 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003865 */
3866 switch (cfg->line_outs) {
3867 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003868 case 4:
3869 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003870 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003871 cfg->line_out_pins[2] = nid;
3872 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003873 }
3874
Takashi Iwai82bc9552006-03-21 11:24:42 +01003875 /*
3876 * debug prints of the parsed results
3877 */
3878 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3879 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3880 cfg->line_out_pins[2], cfg->line_out_pins[3],
3881 cfg->line_out_pins[4]);
3882 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3883 cfg->speaker_outs, cfg->speaker_pins[0],
3884 cfg->speaker_pins[1], cfg->speaker_pins[2],
3885 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003886 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3887 cfg->hp_outs, cfg->hp_pins[0],
3888 cfg->hp_pins[1], cfg->hp_pins[2],
3889 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003890 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01003891 if (cfg->dig_outs)
3892 snd_printd(" dig-out=0x%x/0x%x\n",
3893 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003894 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3895 " cd=0x%x, aux=0x%x\n",
3896 cfg->input_pins[AUTO_PIN_MIC],
3897 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3898 cfg->input_pins[AUTO_PIN_LINE],
3899 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3900 cfg->input_pins[AUTO_PIN_CD],
3901 cfg->input_pins[AUTO_PIN_AUX]);
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01003902 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01003903 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003904
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003905 return 0;
3906}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003907EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003908
Takashi Iwai4a471b72005-12-07 13:56:29 +01003909/* labels for input pins */
3910const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3911 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3912};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003913EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01003914
3915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916#ifdef CONFIG_PM
3917/*
3918 * power management
3919 */
3920
3921/**
3922 * snd_hda_suspend - suspend the codecs
3923 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 *
3925 * Returns 0 if successful.
3926 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02003927int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003929 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Takashi Iwai0ba21762007-04-16 11:29:14 +02003931 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003932#ifdef CONFIG_SND_HDA_POWER_SAVE
3933 if (!codec->power_on)
3934 continue;
3935#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003936 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 }
3938 return 0;
3939}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003940EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
3942/**
3943 * snd_hda_resume - resume the codecs
3944 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 *
3946 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003947 *
3948 * This fucntion is defined only when POWER_SAVE isn't set.
3949 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 */
3951int snd_hda_resume(struct hda_bus *bus)
3952{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003953 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Takashi Iwai0ba21762007-04-16 11:29:14 +02003955 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003956 if (snd_hda_codec_needs_resume(codec))
3957 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return 0;
3960}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003961EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003962#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02003963
3964/*
3965 * generic arrays
3966 */
3967
3968/* get a new element from the given array
3969 * if it exceeds the pre-allocated array size, re-allocate the array
3970 */
3971void *snd_array_new(struct snd_array *array)
3972{
3973 if (array->used >= array->alloced) {
3974 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01003975 void *nlist;
3976 if (snd_BUG_ON(num >= 4096))
3977 return NULL;
3978 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003979 if (!nlist)
3980 return NULL;
3981 if (array->list) {
3982 memcpy(nlist, array->list,
3983 array->elem_size * array->alloced);
3984 kfree(array->list);
3985 }
3986 array->list = nlist;
3987 array->alloced = num;
3988 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01003989 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003990}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003991EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003992
3993/* free the given array elements */
3994void snd_array_free(struct snd_array *array)
3995{
3996 kfree(array->list);
3997 array->used = 0;
3998 array->alloced = 0;
3999 array->list = NULL;
4000}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004001EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004002
4003/*
4004 * used by hda_proc.c and hda_eld.c
4005 */
4006void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4007{
4008 static unsigned int rates[] = {
4009 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4010 96000, 176400, 192000, 384000
4011 };
4012 int i, j;
4013
4014 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4015 if (pcm & (1 << i))
4016 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4017
4018 buf[j] = '\0'; /* necessary when j == 0 */
4019}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004020EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01004021
4022void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4023{
4024 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4025 int i, j;
4026
4027 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4028 if (pcm & (AC_SUPPCM_BITS_8 << i))
4029 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4030
4031 buf[j] = '\0'; /* necessary when j == 0 */
4032}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004033EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004034
4035MODULE_DESCRIPTION("HDA codec core");
4036MODULE_LICENSE("GPL");