blob: a2388fc23e391b51a6528ed317b66d78e2a00275 [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>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020034#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020035#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * vendor / preset table
39 */
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46/* codec vendor labels */
47static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010048 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020049 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020050 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010051 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010052 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010053 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020054 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010055 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020056 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020058 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020060 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010061 { 0x17e8, "Chrontel" },
62 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000063 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010065 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020066 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 {} /* terminator */
68};
69
Takashi Iwai1289e9e2008-11-27 15:47:11 +010070static DEFINE_MUTEX(preset_mutex);
71static LIST_HEAD(hda_preset_tables);
72
73int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74{
75 mutex_lock(&preset_mutex);
76 list_add_tail(&preset->list, &hda_preset_tables);
77 mutex_unlock(&preset_mutex);
78 return 0;
79}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010080EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010081
82int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83{
84 mutex_lock(&preset_mutex);
85 list_del(&preset->list);
86 mutex_unlock(&preset_mutex);
87 return 0;
88}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010089EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwaicb53c622007-08-10 17:21:45 +020091#ifdef CONFIG_SND_HDA_POWER_SAVE
92static void hda_power_work(struct work_struct *work);
93static void hda_keep_power_on(struct hda_codec *codec);
94#else
95static inline void hda_keep_power_on(struct hda_codec *codec) {}
96#endif
97
Takashi Iwaid5191e52009-11-16 14:58:17 +010098/**
99 * snd_hda_get_jack_location - Give a location string of the jack
100 * @cfg: pin default config value
101 *
102 * Parse the pin default config value and returns the string of the
103 * jack location, e.g. "Rear", "Front", etc.
104 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400105const char *snd_hda_get_jack_location(u32 cfg)
106{
107 static char *bases[7] = {
108 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 };
110 static unsigned char specials_idx[] = {
111 0x07, 0x08,
112 0x17, 0x18, 0x19,
113 0x37, 0x38
114 };
115 static char *specials[] = {
116 "Rear Panel", "Drive Bar",
117 "Riser", "HDMI", "ATAPI",
118 "Mobile-In", "Mobile-Out"
119 };
120 int i;
121 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
122 if ((cfg & 0x0f) < 7)
123 return bases[cfg & 0x0f];
124 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
125 if (cfg == specials_idx[i])
126 return specials[i];
127 }
128 return "UNKNOWN";
129}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100130EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400131
Takashi Iwaid5191e52009-11-16 14:58:17 +0100132/**
133 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
134 * @cfg: pin default config value
135 *
136 * Parse the pin default config value and returns the string of the
137 * jack connectivity, i.e. external or internal connection.
138 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139const char *snd_hda_get_jack_connectivity(u32 cfg)
140{
141 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142
143 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100145EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400146
Takashi Iwaid5191e52009-11-16 14:58:17 +0100147/**
148 * snd_hda_get_jack_type - Give a type string of the jack
149 * @cfg: pin default config value
150 *
151 * Parse the pin default config value and returns the string of the
152 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154const char *snd_hda_get_jack_type(u32 cfg)
155{
156 static char *jack_types[16] = {
157 "Line Out", "Speaker", "HP Out", "CD",
158 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
159 "Line In", "Aux", "Mic", "Telephony",
160 "SPDIF In", "Digitial In", "Reserved", "Other"
161 };
162
163 return jack_types[(cfg & AC_DEFCFG_DEVICE)
164 >> AC_DEFCFG_DEVICE_SHIFT];
165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100166EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400167
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100168/*
169 * Compose a 32bit command word to be sent to the HD-audio controller
170 */
171static inline unsigned int
172make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
173 unsigned int verb, unsigned int parm)
174{
175 u32 val;
176
Takashi Iwai82e1b802009-07-17 12:47:34 +0200177 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
178 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800179 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
180 codec->addr, direct, nid, verb, parm);
181 return ~0;
182 }
183
184 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100185 val |= (u32)direct << 27;
186 val |= (u32)nid << 20;
187 val |= verb << 8;
188 val |= parm;
189 return val;
190}
191
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200192/*
193 * Send and receive a verb
194 */
195static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
196 unsigned int *res)
197{
198 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200199 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200
Wu Fengguang6430aee2009-07-17 16:49:19 +0800201 if (cmd == ~0)
202 return -1;
203
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200204 if (res)
205 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200206 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207 snd_hda_power_up(codec);
208 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200209 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200210 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800211 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 mutex_unlock(&bus->cmd_mutex);
213 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 if (res && *res == -1 && bus->rirb_error) {
215 if (bus->response_reset) {
216 snd_printd("hda_codec: resetting BUS due to "
217 "fatal communication error\n");
218 bus->ops.bus_reset(bus);
219 }
220 goto again;
221 }
222 /* clear reset-flag when the communication gets recovered */
223 if (!err)
224 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200225 return err;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/**
229 * snd_hda_codec_read - send a command and get the 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 and read the corresponding response.
237 *
238 * Returns the obtained response value, or -1 for an error.
239 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200240unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
241 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int verb, unsigned int parm)
243{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200244 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700246 if (codec_exec_verb(codec, cmd, &res))
247 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return res;
249}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100250EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252/**
253 * snd_hda_codec_write - send a single command without waiting for response
254 * @codec: the HDA codec
255 * @nid: NID to send the command
256 * @direct: direct flag
257 * @verb: the verb to send
258 * @parm: the parameter for the verb
259 *
260 * Send a single command without waiting for response.
261 *
262 * Returns 0 if successful, or a negative error code.
263 */
264int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
265 unsigned int verb, unsigned int parm)
266{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200267 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100268 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200269 return codec_exec_verb(codec, cmd,
270 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100272EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/**
275 * snd_hda_sequence_write - sequence writes
276 * @codec: the HDA codec
277 * @seq: VERB array to send
278 *
279 * Send the commands sequentially from the given array.
280 * The array must be terminated with NID=0.
281 */
282void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
283{
284 for (; seq->nid; seq++)
285 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100287EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/**
290 * snd_hda_get_sub_nodes - get the range of sub nodes
291 * @codec: the HDA codec
292 * @nid: NID to parse
293 * @start_id: the pointer to store the start NID
294 *
295 * Parse the NID and store the start NID of its sub-nodes.
296 * Returns the number of sub-nodes.
297 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200298int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
299 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 unsigned int parm;
302
303 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200304 if (parm == -1)
305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *start_id = (parm >> 16) & 0x7fff;
307 return (int)(parm & 0x7fff);
308}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100309EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200311static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
312 hda_nid_t *conn_list, int max_conns);
313static bool add_conn_list(struct snd_array *array, hda_nid_t nid);
314static int copy_conn_list(hda_nid_t nid, hda_nid_t *dst, int max_dst,
315 hda_nid_t *src, int len);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/**
318 * snd_hda_get_connections - get connection list
319 * @codec: the HDA codec
320 * @nid: NID to parse
321 * @conn_list: connection list array
322 * @max_conns: max. number of connections to store
323 *
324 * Parses the connection list of the given widget and stores the list
325 * of NIDs.
326 *
327 * Returns the number of connections, or a negative error code.
328 */
329int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200330 hda_nid_t *conn_list, int max_conns)
331{
332 struct snd_array *array = &codec->conn_lists;
Andrew Morton5b17b072011-04-14 15:06:13 -0700333 int i, len, old_used;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200334 hda_nid_t list[HDA_MAX_CONNECTIONS];
335
336 /* look up the cached results */
337 for (i = 0; i < array->used; ) {
338 hda_nid_t *p = snd_array_elem(array, i);
339 len = p[1];
340 if (nid == *p)
341 return copy_conn_list(nid, conn_list, max_conns,
342 p + 2, len);
343 i += len + 2;
344 }
345
346 len = _hda_get_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
347 if (len < 0)
348 return len;
349
350 /* add to the cache */
351 old_used = array->used;
352 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
353 goto error_add;
354 for (i = 0; i < len; i++)
355 if (!add_conn_list(array, list[i]))
356 goto error_add;
357
358 return copy_conn_list(nid, conn_list, max_conns, list, len);
359
360 error_add:
361 array->used = old_used;
362 return -ENOMEM;
363}
364EXPORT_SYMBOL_HDA(snd_hda_get_connections);
365
366static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
367 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100370 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200372 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100373 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Takashi Iwaida3cec32008-08-08 17:12:14 +0200375 if (snd_BUG_ON(!conn_list || max_conns <= 0))
376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200378 wcaps = get_wcaps(codec, nid);
379 if (!(wcaps & AC_WCAP_CONN_LIST) &&
380 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200381 snd_printk(KERN_WARNING "hda_codec: "
382 "connection list not available for 0x%x\n", nid);
383 return -EINVAL;
384 }
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
387 if (parm & AC_CLIST_LONG) {
388 /* long form */
389 shift = 16;
390 num_elems = 2;
391 } else {
392 /* short form */
393 shift = 8;
394 num_elems = 4;
395 }
396 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 mask = (1 << (shift-1)) - 1;
398
Takashi Iwai0ba21762007-04-16 11:29:14 +0200399 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0; /* no connection */
401
402 if (conn_len == 1) {
403 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200404 parm = snd_hda_codec_read(codec, nid, 0,
405 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200406 if (parm == -1 && codec->bus->rirb_error)
407 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 conn_list[0] = parm & mask;
409 return 1;
410 }
411
412 /* multi connection */
413 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100414 prev_nid = 0;
415 for (i = 0; i < conn_len; i++) {
416 int range_val;
417 hda_nid_t val, n;
418
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200419 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100420 parm = snd_hda_codec_read(codec, nid, 0,
421 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200422 if (parm == -1 && codec->bus->rirb_error)
423 return -EIO;
424 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200425 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200427 if (val == 0) {
428 snd_printk(KERN_WARNING "hda_codec: "
429 "invalid CONNECT_LIST verb %x[%i]:%x\n",
430 nid, i, parm);
431 return 0;
432 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100433 parm >>= shift;
434 if (range_val) {
435 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200436 if (!prev_nid || prev_nid >= val) {
437 snd_printk(KERN_WARNING "hda_codec: "
438 "invalid dep_range_val %x:%x\n",
439 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100440 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100442 for (n = prev_nid + 1; n <= val; n++) {
443 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200444 snd_printk(KERN_ERR "hda_codec: "
445 "Too many connections %d for NID 0x%x\n",
446 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100447 return -EINVAL;
448 }
449 conn_list[conns++] = n;
450 }
451 } else {
452 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200453 snd_printk(KERN_ERR "hda_codec: "
454 "Too many connections %d for NID 0x%x\n",
455 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100456 return -EINVAL;
457 }
458 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100460 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 return conns;
463}
464
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200465static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
466{
467 hda_nid_t *p = snd_array_new(array);
468 if (!p)
469 return false;
470 *p = nid;
471 return true;
472}
473
474static int copy_conn_list(hda_nid_t nid, hda_nid_t *dst, int max_dst,
475 hda_nid_t *src, int len)
476{
477 if (len > max_dst) {
478 snd_printk(KERN_ERR "hda_codec: "
479 "Too many connections %d for NID 0x%x\n",
480 len, nid);
481 return -EINVAL;
482 }
483 memcpy(dst, src, len * sizeof(hda_nid_t));
484 return len;
485}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487/**
488 * snd_hda_queue_unsol_event - add an unsolicited event to queue
489 * @bus: the BUS
490 * @res: unsolicited event (lower 32bit of RIRB entry)
491 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
492 *
493 * Adds the given event to the queue. The events are processed in
494 * the workqueue asynchronously. Call this function in the interrupt
495 * hanlder when RIRB receives an unsolicited event.
496 *
497 * Returns 0 if successful, or a negative error code.
498 */
499int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
500{
501 struct hda_bus_unsolicited *unsol;
502 unsigned int wp;
503
Takashi Iwai0ba21762007-04-16 11:29:14 +0200504 unsol = bus->unsol;
505 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 0;
507
508 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
509 unsol->wp = wp;
510
511 wp <<= 1;
512 unsol->queue[wp] = res;
513 unsol->queue[wp + 1] = res_ex;
514
Takashi Iwai6acaed32009-01-12 10:09:24 +0100515 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 return 0;
518}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100519EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800522 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
David Howellsc4028952006-11-22 14:57:56 +0000524static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
David Howellsc4028952006-11-22 14:57:56 +0000526 struct hda_bus_unsolicited *unsol =
527 container_of(work, struct hda_bus_unsolicited, work);
528 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct hda_codec *codec;
530 unsigned int rp, caddr, res;
531
532 while (unsol->rp != unsol->wp) {
533 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
534 unsol->rp = rp;
535 rp <<= 1;
536 res = unsol->queue[rp];
537 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200538 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 continue;
540 codec = bus->caddr_tbl[caddr & 0x0f];
541 if (codec && codec->patch_ops.unsol_event)
542 codec->patch_ops.unsol_event(codec, res);
543 }
544}
545
546/*
547 * initialize unsolicited queue
548 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200549static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct hda_bus_unsolicited *unsol;
552
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100553 if (bus->unsol) /* already initialized */
554 return 0;
555
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200556 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200557 if (!unsol) {
558 snd_printk(KERN_ERR "hda_codec: "
559 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return -ENOMEM;
561 }
David Howellsc4028952006-11-22 14:57:56 +0000562 INIT_WORK(&unsol->work, process_unsol_events);
563 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 bus->unsol = unsol;
565 return 0;
566}
567
568/*
569 * destructor
570 */
571static void snd_hda_codec_free(struct hda_codec *codec);
572
573static int snd_hda_bus_free(struct hda_bus *bus)
574{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200575 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Takashi Iwai0ba21762007-04-16 11:29:14 +0200577 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100579 if (bus->workq)
580 flush_workqueue(bus->workq);
581 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200583 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 snd_hda_codec_free(codec);
585 }
586 if (bus->ops.private_free)
587 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100588 if (bus->workq)
589 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 kfree(bus);
591 return 0;
592}
593
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100594static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100597 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return snd_hda_bus_free(bus);
599}
600
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200601#ifdef CONFIG_SND_HDA_HWDEP
602static int snd_hda_bus_dev_register(struct snd_device *device)
603{
604 struct hda_bus *bus = device->device_data;
605 struct hda_codec *codec;
606 list_for_each_entry(codec, &bus->codec_list, list) {
607 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100608 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200609 }
610 return 0;
611}
612#else
613#define snd_hda_bus_dev_register NULL
614#endif
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/**
617 * snd_hda_bus_new - create a HDA bus
618 * @card: the card entry
619 * @temp: the template for hda_bus information
620 * @busp: the pointer to store the created bus instance
621 *
622 * Returns 0 if successful, or a negative error code.
623 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100624int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200625 const struct hda_bus_template *temp,
626 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 struct hda_bus *bus;
629 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100630 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200631 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 .dev_free = snd_hda_bus_dev_free,
633 };
634
Takashi Iwaida3cec32008-08-08 17:12:14 +0200635 if (snd_BUG_ON(!temp))
636 return -EINVAL;
637 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
638 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if (busp)
641 *busp = NULL;
642
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200643 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (bus == NULL) {
645 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
646 return -ENOMEM;
647 }
648
649 bus->card = card;
650 bus->private_data = temp->private_data;
651 bus->pci = temp->pci;
652 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100653 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 bus->ops = temp->ops;
655
Ingo Molnar62932df2006-01-16 16:34:20 +0100656 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200657 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 INIT_LIST_HEAD(&bus->codec_list);
659
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100660 snprintf(bus->workq_name, sizeof(bus->workq_name),
661 "hd-audio%d", card->number);
662 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100663 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100664 snd_printk(KERN_ERR "cannot create workqueue %s\n",
665 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100666 kfree(bus);
667 return -ENOMEM;
668 }
669
Takashi Iwai0ba21762007-04-16 11:29:14 +0200670 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
671 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 snd_hda_bus_free(bus);
673 return err;
674 }
675 if (busp)
676 *busp = bus;
677 return 0;
678}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100679EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Takashi Iwai82467612007-07-27 19:15:54 +0200681#ifdef CONFIG_SND_HDA_GENERIC
682#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200683 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200684#else
685#define is_generic_config(codec) 0
686#endif
687
Takashi Iwai645f10c2008-11-28 15:07:37 +0100688#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100689#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
690#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100691#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100692#endif
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/*
695 * find a matching codec preset
696 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200697static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200698find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100700 struct hda_codec_preset_list *tbl;
701 const struct hda_codec_preset *preset;
702 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Takashi Iwai82467612007-07-27 19:15:54 +0200704 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100705 return NULL; /* use the generic parser */
706
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100707 again:
708 mutex_lock(&preset_mutex);
709 list_for_each_entry(tbl, &hda_preset_tables, list) {
710 if (!try_module_get(tbl->owner)) {
711 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
712 continue;
713 }
714 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100716 if (preset->afg && preset->afg != codec->afg)
717 continue;
718 if (preset->mfg && preset->mfg != codec->mfg)
719 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200720 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200722 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200723 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100724 preset->rev == codec->revision_id)) {
725 mutex_unlock(&preset_mutex);
726 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100730 module_put(tbl->owner);
731 }
732 mutex_unlock(&preset_mutex);
733
734 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
735 char name[32];
736 if (!mod_requested)
737 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
738 codec->vendor_id);
739 else
740 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
741 (codec->vendor_id >> 16) & 0xffff);
742 request_module(name);
743 mod_requested++;
744 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 return NULL;
747}
748
749/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200750 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200752static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 const struct hda_vendor_id *c;
755 const char *vendor = NULL;
756 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200757 char tmp[16];
758
759 if (codec->vendor_name)
760 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 for (c = hda_vendor_ids; c->id; c++) {
763 if (c->id == vendor_id) {
764 vendor = c->name;
765 break;
766 }
767 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200768 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 sprintf(tmp, "Generic %04x", vendor_id);
770 vendor = tmp;
771 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200772 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
773 if (!codec->vendor_name)
774 return -ENOMEM;
775
776 get_chip_name:
777 if (codec->chip_name)
778 return 0;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200781 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
782 else {
783 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
784 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
785 }
786 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200787 return -ENOMEM;
788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200792 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100794static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200796 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 hda_nid_t nid;
798
799 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
800 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200801 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200802 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200803 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200804 case AC_GRP_AUDIO_FUNCTION:
805 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200806 codec->afg_function_id = function_id & 0xff;
807 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200808 break;
809 case AC_GRP_MODEM_FUNCTION:
810 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200811 codec->mfg_function_id = function_id & 0xff;
812 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200813 break;
814 default:
815 break;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100821 * read widget caps for each widget and store in cache
822 */
823static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
824{
825 int i;
826 hda_nid_t nid;
827
828 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
829 &codec->start_nid);
830 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200831 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100832 return -ENOMEM;
833 nid = codec->start_nid;
834 for (i = 0; i < codec->num_nodes; i++, nid++)
835 codec->wcaps[i] = snd_hda_param_read(codec, nid,
836 AC_PAR_AUDIO_WIDGET_CAP);
837 return 0;
838}
839
Takashi Iwai3be14142009-02-20 14:11:16 +0100840/* read all pin default configurations and save codec->init_pins */
841static int read_pin_defaults(struct hda_codec *codec)
842{
843 int i;
844 hda_nid_t nid = codec->start_nid;
845
846 for (i = 0; i < codec->num_nodes; i++, nid++) {
847 struct hda_pincfg *pin;
848 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200849 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100850 if (wid_type != AC_WID_PIN)
851 continue;
852 pin = snd_array_new(&codec->init_pins);
853 if (!pin)
854 return -ENOMEM;
855 pin->nid = nid;
856 pin->cfg = snd_hda_codec_read(codec, nid, 0,
857 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200858 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
859 AC_VERB_GET_PIN_WIDGET_CONTROL,
860 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100861 }
862 return 0;
863}
864
865/* look up the given pin config list and return the item matching with NID */
866static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
867 struct snd_array *array,
868 hda_nid_t nid)
869{
870 int i;
871 for (i = 0; i < array->used; i++) {
872 struct hda_pincfg *pin = snd_array_elem(array, i);
873 if (pin->nid == nid)
874 return pin;
875 }
876 return NULL;
877}
878
879/* write a config value for the given NID */
880static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
881 unsigned int cfg)
882{
883 int i;
884 for (i = 0; i < 4; i++) {
885 snd_hda_codec_write(codec, nid, 0,
886 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
887 cfg & 0xff);
888 cfg >>= 8;
889 }
890}
891
892/* set the current pin config value for the given NID.
893 * the value is cached, and read via snd_hda_codec_get_pincfg()
894 */
895int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
896 hda_nid_t nid, unsigned int cfg)
897{
898 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100899 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100900
Takashi Iwaib82855a2009-12-27 11:24:56 +0100901 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
902 return -EINVAL;
903
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100904 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100905 pin = look_up_pincfg(codec, list, nid);
906 if (!pin) {
907 pin = snd_array_new(list);
908 if (!pin)
909 return -ENOMEM;
910 pin->nid = nid;
911 }
912 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100913
914 /* change only when needed; e.g. if the pincfg is already present
915 * in user_pins[], don't write it
916 */
917 cfg = snd_hda_codec_get_pincfg(codec, nid);
918 if (oldcfg != cfg)
919 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100920 return 0;
921}
922
Takashi Iwaid5191e52009-11-16 14:58:17 +0100923/**
924 * snd_hda_codec_set_pincfg - Override a pin default configuration
925 * @codec: the HDA codec
926 * @nid: NID to set the pin config
927 * @cfg: the pin default config value
928 *
929 * Override a pin default configuration value in the cache.
930 * This value can be read by snd_hda_codec_get_pincfg() in a higher
931 * priority than the real hardware value.
932 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100933int snd_hda_codec_set_pincfg(struct hda_codec *codec,
934 hda_nid_t nid, unsigned int cfg)
935{
Takashi Iwai346ff702009-02-23 09:42:57 +0100936 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100937}
938EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
939
Takashi Iwaid5191e52009-11-16 14:58:17 +0100940/**
941 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
942 * @codec: the HDA codec
943 * @nid: NID to get the pin config
944 *
945 * Get the current pin config value of the given pin NID.
946 * If the pincfg value is cached or overridden via sysfs or driver,
947 * returns the cached value.
948 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100949unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
950{
951 struct hda_pincfg *pin;
952
Takashi Iwai3be14142009-02-20 14:11:16 +0100953#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100954 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100955 if (pin)
956 return pin->cfg;
957#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100958 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
959 if (pin)
960 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100961 pin = look_up_pincfg(codec, &codec->init_pins, nid);
962 if (pin)
963 return pin->cfg;
964 return 0;
965}
966EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
967
968/* restore all current pin configs */
969static void restore_pincfgs(struct hda_codec *codec)
970{
971 int i;
972 for (i = 0; i < codec->init_pins.used; i++) {
973 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
974 set_pincfg(codec, pin->nid,
975 snd_hda_codec_get_pincfg(codec, pin->nid));
976 }
977}
Takashi Iwai54d17402005-11-21 16:33:22 +0100978
Takashi Iwai92ee6162009-12-27 11:18:59 +0100979/**
980 * snd_hda_shutup_pins - Shut up all pins
981 * @codec: the HDA codec
982 *
983 * Clear all pin controls to shup up before suspend for avoiding click noise.
984 * The controls aren't cached so that they can be resumed properly.
985 */
986void snd_hda_shutup_pins(struct hda_codec *codec)
987{
988 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200989 /* don't shut up pins when unloading the driver; otherwise it breaks
990 * the default pin setup at the next load of the driver
991 */
992 if (codec->bus->shutdown)
993 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100994 for (i = 0; i < codec->init_pins.used; i++) {
995 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
996 /* use read here for syncing after issuing each verb */
997 snd_hda_codec_read(codec, pin->nid, 0,
998 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
999 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001000 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001001}
1002EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1003
Mike Waychison1c7276c2011-04-20 12:04:36 -07001004#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001005/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1006static void restore_shutup_pins(struct hda_codec *codec)
1007{
1008 int i;
1009 if (!codec->pins_shutup)
1010 return;
1011 if (codec->bus->shutdown)
1012 return;
1013 for (i = 0; i < codec->init_pins.used; i++) {
1014 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1015 snd_hda_codec_write(codec, pin->nid, 0,
1016 AC_VERB_SET_PIN_WIDGET_CONTROL,
1017 pin->ctrl);
1018 }
1019 codec->pins_shutup = 0;
1020}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001021#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001022
Takashi Iwai01751f52007-08-10 16:59:39 +02001023static void init_hda_cache(struct hda_cache_rec *cache,
1024 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001025static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001026
Takashi Iwai3be14142009-02-20 14:11:16 +01001027/* restore the initial pin cfgs and release all pincfg lists */
1028static void restore_init_pincfgs(struct hda_codec *codec)
1029{
Takashi Iwai346ff702009-02-23 09:42:57 +01001030 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001031 * so that only the values in init_pins are restored
1032 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001033 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001034#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001035 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001036#endif
1037 restore_pincfgs(codec);
1038 snd_array_free(&codec->init_pins);
1039}
1040
Takashi Iwai54d17402005-11-21 16:33:22 +01001041/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001042 * audio-converter setup caches
1043 */
1044struct hda_cvt_setup {
1045 hda_nid_t nid;
1046 u8 stream_tag;
1047 u8 channel_id;
1048 u16 format_id;
1049 unsigned char active; /* cvt is currently used */
1050 unsigned char dirty; /* setups should be cleared */
1051};
1052
1053/* get or create a cache entry for the given audio converter NID */
1054static struct hda_cvt_setup *
1055get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1056{
1057 struct hda_cvt_setup *p;
1058 int i;
1059
1060 for (i = 0; i < codec->cvt_setups.used; i++) {
1061 p = snd_array_elem(&codec->cvt_setups, i);
1062 if (p->nid == nid)
1063 return p;
1064 }
1065 p = snd_array_new(&codec->cvt_setups);
1066 if (p)
1067 p->nid = nid;
1068 return p;
1069}
1070
1071/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 * codec destructor
1073 */
1074static void snd_hda_codec_free(struct hda_codec *codec)
1075{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001076 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001078 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001079#ifdef CONFIG_SND_HDA_POWER_SAVE
1080 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001081 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001084 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001085 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001086 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001087 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 codec->bus->caddr_tbl[codec->addr] = NULL;
1089 if (codec->patch_ops.free)
1090 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001091 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001092 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001093 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001094 kfree(codec->vendor_name);
1095 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001096 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001097 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 kfree(codec);
1099}
1100
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001101static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1102 unsigned int power_state);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/**
1105 * snd_hda_codec_new - create a HDA codec
1106 * @bus: the bus to assign
1107 * @codec_addr: the codec address
1108 * @codecp: the pointer to store the generated codec
1109 *
1110 * Returns 0 if successful, or a negative error code.
1111 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001112int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1113 unsigned int codec_addr,
1114 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001117 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int err;
1119
Takashi Iwaida3cec32008-08-08 17:12:14 +02001120 if (snd_BUG_ON(!bus))
1121 return -EINVAL;
1122 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1123 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001126 snd_printk(KERN_ERR "hda_codec: "
1127 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -EBUSY;
1129 }
1130
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001131 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (codec == NULL) {
1133 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1134 return -ENOMEM;
1135 }
1136
1137 codec->bus = bus;
1138 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001139 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001140 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001141 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001142 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001143 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1144 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001145 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001146 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001147 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001148 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001149 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001150 if (codec->bus->modelname) {
1151 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1152 if (!codec->modelname) {
1153 snd_hda_codec_free(codec);
1154 return -ENODEV;
1155 }
1156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Takashi Iwaicb53c622007-08-10 17:21:45 +02001158#ifdef CONFIG_SND_HDA_POWER_SAVE
1159 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1160 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1161 * the caller has to power down appropriatley after initialization
1162 * phase.
1163 */
1164 hda_keep_power_on(codec);
1165#endif
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 list_add_tail(&codec->list, &bus->codec_list);
1168 bus->caddr_tbl[codec_addr] = codec;
1169
Takashi Iwai0ba21762007-04-16 11:29:14 +02001170 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1171 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001172 if (codec->vendor_id == -1)
1173 /* read again, hopefully the access method was corrected
1174 * in the last read...
1175 */
1176 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1177 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001178 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1179 AC_PAR_SUBSYSTEM_ID);
1180 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1181 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001183 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001184 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001185 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001186 err = -ENODEV;
1187 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Takashi Iwai3be14142009-02-20 14:11:16 +01001190 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1191 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001192 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001193 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001194 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001195 err = read_pin_defaults(codec);
1196 if (err < 0)
1197 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001198
Takashi Iwai0ba21762007-04-16 11:29:14 +02001199 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001200 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001201 codec->subsystem_id =
1202 snd_hda_codec_read(codec, nid, 0,
1203 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001204 }
1205
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001206 /* power-up all before initialization */
1207 hda_set_power_state(codec,
1208 codec->afg ? codec->afg : codec->mfg,
1209 AC_PWRST_D0);
1210
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001211 snd_hda_codec_proc_new(codec);
1212
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001213 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001214
1215 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1216 codec->subsystem_id, codec->revision_id);
1217 snd_component_add(codec->bus->card, component);
1218
1219 if (codecp)
1220 *codecp = codec;
1221 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001222
1223 error:
1224 snd_hda_codec_free(codec);
1225 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001226}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001227EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001228
Takashi Iwaid5191e52009-11-16 14:58:17 +01001229/**
1230 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1231 * @codec: the HDA codec
1232 *
1233 * Start parsing of the given codec tree and (re-)initialize the whole
1234 * patch instance.
1235 *
1236 * Returns 0 if successful or a negative error code.
1237 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001238int snd_hda_codec_configure(struct hda_codec *codec)
1239{
1240 int err;
1241
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001242 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001243 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001244 err = get_codec_name(codec);
1245 if (err < 0)
1246 return err;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Takashi Iwai82467612007-07-27 19:15:54 +02001249 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001251 goto patched;
1252 }
Takashi Iwai82467612007-07-27 19:15:54 +02001253 if (codec->preset && codec->preset->patch) {
1254 err = codec->preset->patch(codec);
1255 goto patched;
1256 }
1257
1258 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001259 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001260 if (err < 0)
1261 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001262
1263 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001264 if (!err && codec->patch_ops.unsol_event)
1265 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001266 /* audio codec should override the mixer name */
1267 if (!err && (codec->afg || !*codec->bus->card->mixername))
1268 snprintf(codec->bus->card->mixername,
1269 sizeof(codec->bus->card->mixername),
1270 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001271 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001273EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275/**
1276 * snd_hda_codec_setup_stream - set up the codec for streaming
1277 * @codec: the CODEC to set up
1278 * @nid: the NID to set up
1279 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1280 * @channel_id: channel id to pass, zero based.
1281 * @format: stream format.
1282 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001283void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1284 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int channel_id, int format)
1286{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001287 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001288 struct hda_cvt_setup *p;
1289 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001290 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001291 int i;
1292
Takashi Iwai0ba21762007-04-16 11:29:14 +02001293 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001294 return;
1295
Takashi Iwai0ba21762007-04-16 11:29:14 +02001296 snd_printdd("hda_codec_setup_stream: "
1297 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001299 p = get_hda_cvt_setup(codec, nid);
1300 if (!p)
1301 return;
1302 /* update the stream-id if changed */
1303 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1304 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1305 newval = (stream_tag << 4) | channel_id;
1306 if (oldval != newval)
1307 snd_hda_codec_write(codec, nid, 0,
1308 AC_VERB_SET_CHANNEL_STREAMID,
1309 newval);
1310 p->stream_tag = stream_tag;
1311 p->channel_id = channel_id;
1312 }
1313 /* update the format-id if changed */
1314 if (p->format_id != format) {
1315 oldval = snd_hda_codec_read(codec, nid, 0,
1316 AC_VERB_GET_STREAM_FORMAT, 0);
1317 if (oldval != format) {
1318 msleep(1);
1319 snd_hda_codec_write(codec, nid, 0,
1320 AC_VERB_SET_STREAM_FORMAT,
1321 format);
1322 }
1323 p->format_id = format;
1324 }
1325 p->active = 1;
1326 p->dirty = 0;
1327
1328 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001329 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001330 list_for_each_entry(c, &codec->bus->codec_list, list) {
1331 for (i = 0; i < c->cvt_setups.used; i++) {
1332 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001333 if (!p->active && p->stream_tag == stream_tag &&
1334 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001335 p->dirty = 1;
1336 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001339EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Takashi Iwaif0cea792010-08-13 11:56:53 +02001341static void really_cleanup_stream(struct hda_codec *codec,
1342 struct hda_cvt_setup *q);
1343
Takashi Iwaid5191e52009-11-16 14:58:17 +01001344/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001345 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001346 * @codec: the CODEC to clean up
1347 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001348 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001349 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001350void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1351 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001352{
Takashi Iwaieb541332010-08-06 13:48:11 +02001353 struct hda_cvt_setup *p;
1354
Takashi Iwai888afa12008-03-18 09:57:50 +01001355 if (!nid)
1356 return;
1357
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001358 if (codec->no_sticky_stream)
1359 do_now = 1;
1360
Takashi Iwai888afa12008-03-18 09:57:50 +01001361 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001362 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001363 if (p) {
1364 /* here we just clear the active flag when do_now isn't set;
1365 * actual clean-ups will be done later in
1366 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1367 */
1368 if (do_now)
1369 really_cleanup_stream(codec, p);
1370 else
1371 p->active = 0;
1372 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001373}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001374EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001375
Takashi Iwaieb541332010-08-06 13:48:11 +02001376static void really_cleanup_stream(struct hda_codec *codec,
1377 struct hda_cvt_setup *q)
1378{
1379 hda_nid_t nid = q->nid;
1380 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1381 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1382 memset(q, 0, sizeof(*q));
1383 q->nid = nid;
1384}
1385
1386/* clean up the all conflicting obsolete streams */
1387static void purify_inactive_streams(struct hda_codec *codec)
1388{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001389 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001390 int i;
1391
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001392 list_for_each_entry(c, &codec->bus->codec_list, list) {
1393 for (i = 0; i < c->cvt_setups.used; i++) {
1394 struct hda_cvt_setup *p;
1395 p = snd_array_elem(&c->cvt_setups, i);
1396 if (p->dirty)
1397 really_cleanup_stream(c, p);
1398 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001399 }
1400}
1401
Mike Waychison1c7276c2011-04-20 12:04:36 -07001402#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaieb541332010-08-06 13:48:11 +02001403/* clean up all streams; called from suspend */
1404static void hda_cleanup_all_streams(struct hda_codec *codec)
1405{
1406 int i;
1407
1408 for (i = 0; i < codec->cvt_setups.used; i++) {
1409 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1410 if (p->stream_tag)
1411 really_cleanup_stream(codec, p);
1412 }
1413}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001414#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416/*
1417 * amp access functions
1418 */
1419
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001420/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001421#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001422#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001423#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1424#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001426#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001429static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001430 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Takashi Iwai01751f52007-08-10 16:59:39 +02001432 memset(cache, 0, sizeof(*cache));
1433 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001434 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001435}
1436
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001437static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001438{
Takashi Iwai603c4012008-07-30 15:01:44 +02001439 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
1442/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001443static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Takashi Iwai01751f52007-08-10 16:59:39 +02001445 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1446 u16 cur = cache->hash[idx];
1447 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001450 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (info->key == key)
1452 return info;
1453 cur = info->next;
1454 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001455 return NULL;
1456}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001458/* query the hash. allocate an entry if not found. */
1459static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1460 u32 key)
1461{
1462 struct hda_cache_head *info = get_hash(cache, key);
1463 if (!info) {
1464 u16 idx, cur;
1465 /* add a new hash entry */
1466 info = snd_array_new(&cache->buf);
1467 if (!info)
1468 return NULL;
1469 cur = snd_array_index(&cache->buf, info);
1470 info->key = key;
1471 info->val = 0;
1472 idx = key % (u16)ARRAY_SIZE(cache->hash);
1473 info->next = cache->hash[idx];
1474 cache->hash[idx] = cur;
1475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return info;
1477}
1478
Takashi Iwai01751f52007-08-10 16:59:39 +02001479/* query and allocate an amp hash entry */
1480static inline struct hda_amp_info *
1481get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1482{
1483 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1484}
1485
Takashi Iwaid5191e52009-11-16 14:58:17 +01001486/**
1487 * query_amp_caps - query AMP capabilities
1488 * @codec: the HD-auio codec
1489 * @nid: the NID to query
1490 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1491 *
1492 * Query AMP capabilities for the given widget and direction.
1493 * Returns the obtained capability bits.
1494 *
1495 * When cap bits have been already read, this doesn't read again but
1496 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001498u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001500 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Takashi Iwai0ba21762007-04-16 11:29:14 +02001502 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1503 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001505 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001506 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001508 info->amp_caps = snd_hda_param_read(codec, nid,
1509 direction == HDA_OUTPUT ?
1510 AC_PAR_AMP_OUT_CAP :
1511 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001512 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001513 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515 return info->amp_caps;
1516}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001517EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Takashi Iwaid5191e52009-11-16 14:58:17 +01001519/**
1520 * snd_hda_override_amp_caps - Override the AMP capabilities
1521 * @codec: the CODEC to clean up
1522 * @nid: the NID to clean up
1523 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1524 * @caps: the capability bits to set
1525 *
1526 * Override the cached AMP caps bits value by the given one.
1527 * This function is useful if the driver needs to adjust the AMP ranges,
1528 * e.g. limit to 0dB, etc.
1529 *
1530 * Returns zero if successful or a negative error code.
1531 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001532int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1533 unsigned int caps)
1534{
1535 struct hda_amp_info *info;
1536
1537 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1538 if (!info)
1539 return -EINVAL;
1540 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001541 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001542 return 0;
1543}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001544EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001545
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001546static unsigned int
1547query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1548 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001549{
1550 struct hda_amp_info *info;
1551
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001552 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001553 if (!info)
1554 return 0;
1555 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001556 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001557 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001558 }
1559 return info->amp_caps;
1560}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001561
1562static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1563{
1564 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1565}
1566
Takashi Iwaid5191e52009-11-16 14:58:17 +01001567/**
1568 * snd_hda_query_pin_caps - Query PIN capabilities
1569 * @codec: the HD-auio codec
1570 * @nid: the NID to query
1571 *
1572 * Query PIN capabilities for the given widget.
1573 * Returns the obtained capability bits.
1574 *
1575 * When cap bits have been already read, this doesn't read again but
1576 * returns the cached value.
1577 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001578u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1579{
1580 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1581 read_pin_cap);
1582}
Takashi Iwai1327a322009-03-23 13:07:47 +01001583EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1584
Wu Fengguang864f92b2009-11-18 12:38:02 +08001585/**
1586 * snd_hda_pin_sense - execute pin sense measurement
1587 * @codec: the CODEC to sense
1588 * @nid: the pin NID to sense
1589 *
1590 * Execute necessary pin sense measurement and return its Presence Detect,
1591 * Impedance, ELD Valid etc. status bits.
1592 */
1593u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1594{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001595 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001596
Takashi Iwai729d55b2009-12-25 22:49:01 +01001597 if (!codec->no_trigger_sense) {
1598 pincap = snd_hda_query_pin_caps(codec, nid);
1599 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001600 snd_hda_codec_read(codec, nid, 0,
1601 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001602 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001603 return snd_hda_codec_read(codec, nid, 0,
1604 AC_VERB_GET_PIN_SENSE, 0);
1605}
1606EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1607
1608/**
1609 * snd_hda_jack_detect - query pin Presence Detect status
1610 * @codec: the CODEC to sense
1611 * @nid: the pin NID to sense
1612 *
1613 * Query and return the pin's Presence Detect status.
1614 */
1615int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1616{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001617 u32 sense = snd_hda_pin_sense(codec, nid);
1618 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001619}
1620EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/*
1623 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001624 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001626static unsigned int get_vol_mute(struct hda_codec *codec,
1627 struct hda_amp_info *info, hda_nid_t nid,
1628 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
1630 u32 val, parm;
1631
Takashi Iwai01751f52007-08-10 16:59:39 +02001632 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001633 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1636 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1637 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001638 val = snd_hda_codec_read(codec, nid, 0,
1639 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001641 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001642 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
1645/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001646 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001648static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001649 hda_nid_t nid, int ch, int direction, int index,
1650 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 u32 parm;
1653
1654 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1655 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1656 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1657 parm |= val;
1658 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001659 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Takashi Iwaid5191e52009-11-16 14:58:17 +01001662/**
1663 * snd_hda_codec_amp_read - Read AMP value
1664 * @codec: HD-audio codec
1665 * @nid: NID to read the AMP value
1666 * @ch: channel (left=0 or right=1)
1667 * @direction: #HDA_INPUT or #HDA_OUTPUT
1668 * @index: the index value (only for input direction)
1669 *
1670 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 */
Takashi Iwai834be882006-03-01 14:16:17 +01001672int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1673 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001675 struct hda_amp_info *info;
1676 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1677 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001679 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001681EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Takashi Iwaid5191e52009-11-16 14:58:17 +01001683/**
1684 * snd_hda_codec_amp_update - update the AMP value
1685 * @codec: HD-audio codec
1686 * @nid: NID to read the AMP value
1687 * @ch: channel (left=0 or right=1)
1688 * @direction: #HDA_INPUT or #HDA_OUTPUT
1689 * @idx: the index value (only for input direction)
1690 * @mask: bit mask to set
1691 * @val: the bits value to set
1692 *
1693 * Update the AMP value with a bit mask.
1694 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001695 */
Takashi Iwai834be882006-03-01 14:16:17 +01001696int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1697 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001699 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001700
Takashi Iwai0ba21762007-04-16 11:29:14 +02001701 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1702 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001704 if (snd_BUG_ON(mask & ~0xff))
1705 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001706 val &= mask;
1707 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001708 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001710 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return 1;
1712}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001713EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Takashi Iwaid5191e52009-11-16 14:58:17 +01001715/**
1716 * snd_hda_codec_amp_stereo - update the AMP stereo values
1717 * @codec: HD-audio codec
1718 * @nid: NID to read the AMP value
1719 * @direction: #HDA_INPUT or #HDA_OUTPUT
1720 * @idx: the index value (only for input direction)
1721 * @mask: bit mask to set
1722 * @val: the bits value to set
1723 *
1724 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1725 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001726 */
1727int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1728 int direction, int idx, int mask, int val)
1729{
1730 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001731
1732 if (snd_BUG_ON(mask & ~0xff))
1733 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001734 for (ch = 0; ch < 2; ch++)
1735 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1736 idx, mask, val);
1737 return ret;
1738}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001739EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001740
Takashi Iwaicb53c622007-08-10 17:21:45 +02001741#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001742/**
1743 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1744 * @codec: HD-audio codec
1745 *
1746 * Resume the all amp commands from the cache.
1747 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001748void snd_hda_codec_resume_amp(struct hda_codec *codec)
1749{
Takashi Iwai603c4012008-07-30 15:01:44 +02001750 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001751 int i;
1752
Takashi Iwai603c4012008-07-30 15:01:44 +02001753 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001754 u32 key = buffer->head.key;
1755 hda_nid_t nid;
1756 unsigned int idx, dir, ch;
1757 if (!key)
1758 continue;
1759 nid = key & 0xff;
1760 idx = (key >> 16) & 0xff;
1761 dir = (key >> 24) & 0xff;
1762 for (ch = 0; ch < 2; ch++) {
1763 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1764 continue;
1765 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1766 buffer->vol[ch]);
1767 }
1768 }
1769}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001770EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001771#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001773static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1774 unsigned int ofs)
1775{
1776 u32 caps = query_amp_caps(codec, nid, dir);
1777 /* get num steps */
1778 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1779 if (ofs < caps)
1780 caps -= ofs;
1781 return caps;
1782}
1783
Takashi Iwaid5191e52009-11-16 14:58:17 +01001784/**
1785 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1786 *
1787 * The control element is supposed to have the private_value field
1788 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1789 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001790int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1791 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1794 u16 nid = get_amp_nid(kcontrol);
1795 u8 chs = get_amp_channels(kcontrol);
1796 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001797 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001799 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1800 uinfo->count = chs == 3 ? 2 : 1;
1801 uinfo->value.integer.min = 0;
1802 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1803 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001804 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001805 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1806 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return -EINVAL;
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return 0;
1810}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001811EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001813
1814static inline unsigned int
1815read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1816 int ch, int dir, int idx, unsigned int ofs)
1817{
1818 unsigned int val;
1819 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1820 val &= HDA_AMP_VOLMASK;
1821 if (val >= ofs)
1822 val -= ofs;
1823 else
1824 val = 0;
1825 return val;
1826}
1827
1828static inline int
1829update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1830 int ch, int dir, int idx, unsigned int ofs,
1831 unsigned int val)
1832{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001833 unsigned int maxval;
1834
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001835 if (val > 0)
1836 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001837 /* ofs = 0: raw max value */
1838 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001839 if (val > maxval)
1840 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001841 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1842 HDA_AMP_VOLMASK, val);
1843}
1844
Takashi Iwaid5191e52009-11-16 14:58:17 +01001845/**
1846 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1847 *
1848 * The control element is supposed to have the private_value field
1849 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1850 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001851int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1852 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1855 hda_nid_t nid = get_amp_nid(kcontrol);
1856 int chs = get_amp_channels(kcontrol);
1857 int dir = get_amp_direction(kcontrol);
1858 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001859 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 long *valp = ucontrol->value.integer.value;
1861
1862 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001863 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001865 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return 0;
1867}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001868EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Takashi Iwaid5191e52009-11-16 14:58:17 +01001870/**
1871 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1872 *
1873 * The control element is supposed to have the private_value field
1874 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1875 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001876int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1877 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1880 hda_nid_t nid = get_amp_nid(kcontrol);
1881 int chs = get_amp_channels(kcontrol);
1882 int dir = get_amp_direction(kcontrol);
1883 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001884 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 long *valp = ucontrol->value.integer.value;
1886 int change = 0;
1887
Takashi Iwaicb53c622007-08-10 17:21:45 +02001888 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001889 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001890 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001891 valp++;
1892 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001893 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001894 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001895 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return change;
1897}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001898EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Takashi Iwaid5191e52009-11-16 14:58:17 +01001900/**
1901 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1902 *
1903 * The control element is supposed to have the private_value field
1904 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1905 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001906int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1907 unsigned int size, unsigned int __user *_tlv)
1908{
1909 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1910 hda_nid_t nid = get_amp_nid(kcontrol);
1911 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001912 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001913 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001914 u32 caps, val1, val2;
1915
1916 if (size < 4 * sizeof(unsigned int))
1917 return -ENOMEM;
1918 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001919 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1920 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001921 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001922 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001923 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001924 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02001925 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001926 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1927 return -EFAULT;
1928 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1929 return -EFAULT;
1930 if (put_user(val1, _tlv + 2))
1931 return -EFAULT;
1932 if (put_user(val2, _tlv + 3))
1933 return -EFAULT;
1934 return 0;
1935}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001936EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001937
Takashi Iwaid5191e52009-11-16 14:58:17 +01001938/**
1939 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1940 * @codec: HD-audio codec
1941 * @nid: NID of a reference widget
1942 * @dir: #HDA_INPUT or #HDA_OUTPUT
1943 * @tlv: TLV data to be stored, at least 4 elements
1944 *
1945 * Set (static) TLV data for a virtual master volume using the AMP caps
1946 * obtained from the reference NID.
1947 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001948 */
1949void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1950 unsigned int *tlv)
1951{
1952 u32 caps;
1953 int nums, step;
1954
1955 caps = query_amp_caps(codec, nid, dir);
1956 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1957 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1958 step = (step + 1) * 25;
1959 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1960 tlv[1] = 2 * sizeof(unsigned int);
1961 tlv[2] = -nums * step;
1962 tlv[3] = step;
1963}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001964EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001965
1966/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001967static struct snd_kcontrol *
1968_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1969 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001970{
1971 struct snd_ctl_elem_id id;
1972 memset(&id, 0, sizeof(id));
1973 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001974 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001975 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1976 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001977 strcpy(id.name, name);
1978 return snd_ctl_find_id(codec->bus->card, &id);
1979}
1980
Takashi Iwaid5191e52009-11-16 14:58:17 +01001981/**
1982 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1983 * @codec: HD-audio codec
1984 * @name: ctl id name string
1985 *
1986 * Get the control element with the given id string and IFACE_MIXER.
1987 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001988struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1989 const char *name)
1990{
1991 return _snd_hda_find_mixer_ctl(codec, name, 0);
1992}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001993EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001994
Takashi Iwai1afe2062010-12-23 10:17:52 +01001995static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
1996{
1997 int idx;
1998 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
1999 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2000 return idx;
2001 }
2002 return -EBUSY;
2003}
2004
Takashi Iwaid5191e52009-11-16 14:58:17 +01002005/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002006 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002007 * @codec: HD-audio codec
2008 * @nid: corresponding NID (optional)
2009 * @kctl: the control element to assign
2010 *
2011 * Add the given control element to an array inside the codec instance.
2012 * All control elements belonging to a codec are supposed to be added
2013 * by this function so that a proper clean-up works at the free or
2014 * reconfiguration time.
2015 *
2016 * If non-zero @nid is passed, the NID is assigned to the control element.
2017 * The assignment is shown in the codec proc file.
2018 *
2019 * snd_hda_ctl_add() checks the control subdev id field whether
2020 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002021 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2022 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002023 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002024int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2025 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002026{
2027 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002028 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002029 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002030
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002031 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002032 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002033 if (nid == 0)
2034 nid = get_amp_nid_(kctl->private_value);
2035 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002036 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2037 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002038 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002039 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002040 err = snd_ctl_add(codec->bus->card, kctl);
2041 if (err < 0)
2042 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002043 item = snd_array_new(&codec->mixers);
2044 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002045 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002046 item->kctl = kctl;
2047 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002048 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002049 return 0;
2050}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002051EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002052
Takashi Iwaid5191e52009-11-16 14:58:17 +01002053/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002054 * snd_hda_add_nid - Assign a NID to a control element
2055 * @codec: HD-audio codec
2056 * @nid: corresponding NID (optional)
2057 * @kctl: the control element to assign
2058 * @index: index to kctl
2059 *
2060 * Add the given control element to an array inside the codec instance.
2061 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2062 * NID:KCTL mapping - for example "Capture Source" selector.
2063 */
2064int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2065 unsigned int index, hda_nid_t nid)
2066{
2067 struct hda_nid_item *item;
2068
2069 if (nid > 0) {
2070 item = snd_array_new(&codec->nids);
2071 if (!item)
2072 return -ENOMEM;
2073 item->kctl = kctl;
2074 item->index = index;
2075 item->nid = nid;
2076 return 0;
2077 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002078 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2079 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002080 return -EINVAL;
2081}
2082EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2083
2084/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002085 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2086 * @codec: HD-audio codec
2087 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002088void snd_hda_ctls_clear(struct hda_codec *codec)
2089{
2090 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002091 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002092 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002093 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002094 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002095 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002096}
2097
Takashi Iwaia65d6292009-02-23 16:57:04 +01002098/* pseudo device locking
2099 * toggle card->shutdown to allow/disallow the device access (as a hack)
2100 */
2101static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002102{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002103 spin_lock(&card->files_lock);
2104 if (card->shutdown) {
2105 spin_unlock(&card->files_lock);
2106 return -EINVAL;
2107 }
2108 card->shutdown = 1;
2109 spin_unlock(&card->files_lock);
2110 return 0;
2111}
2112
2113static void hda_unlock_devices(struct snd_card *card)
2114{
2115 spin_lock(&card->files_lock);
2116 card->shutdown = 0;
2117 spin_unlock(&card->files_lock);
2118}
2119
Takashi Iwaid5191e52009-11-16 14:58:17 +01002120/**
2121 * snd_hda_codec_reset - Clear all objects assigned to the codec
2122 * @codec: HD-audio codec
2123 *
2124 * This frees the all PCM and control elements assigned to the codec, and
2125 * clears the caches and restores the pin default configurations.
2126 *
2127 * When a device is being used, it returns -EBSY. If successfully freed,
2128 * returns zero.
2129 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002130int snd_hda_codec_reset(struct hda_codec *codec)
2131{
2132 struct snd_card *card = codec->bus->card;
2133 int i, pcm;
2134
2135 if (hda_lock_devices(card) < 0)
2136 return -EBUSY;
2137 /* check whether the codec isn't used by any mixer or PCM streams */
2138 if (!list_empty(&card->ctl_files)) {
2139 hda_unlock_devices(card);
2140 return -EBUSY;
2141 }
2142 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2143 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2144 if (!cpcm->pcm)
2145 continue;
2146 if (cpcm->pcm->streams[0].substream_opened ||
2147 cpcm->pcm->streams[1].substream_opened) {
2148 hda_unlock_devices(card);
2149 return -EBUSY;
2150 }
2151 }
2152
2153 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002154
2155#ifdef CONFIG_SND_HDA_POWER_SAVE
2156 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002157 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002158#endif
2159 snd_hda_ctls_clear(codec);
2160 /* relase PCMs */
2161 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002162 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002163 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002164 clear_bit(codec->pcm_info[i].device,
2165 codec->bus->pcm_dev_bits);
2166 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002167 }
2168 if (codec->patch_ops.free)
2169 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002170 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002171 codec->spec = NULL;
2172 free_hda_cache(&codec->amp_cache);
2173 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002174 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2175 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002176 /* free only driver_pins so that init_pins + user_pins are restored */
2177 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002178 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002179 codec->num_pcms = 0;
2180 codec->pcm_info = NULL;
2181 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002182 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2183 codec->slave_dig_outs = NULL;
2184 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002185 module_put(codec->owner);
2186 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002187
2188 /* allow device access again */
2189 hda_unlock_devices(card);
2190 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002191}
2192
Takashi Iwaid5191e52009-11-16 14:58:17 +01002193/**
2194 * snd_hda_add_vmaster - create a virtual master control and add slaves
2195 * @codec: HD-audio codec
2196 * @name: vmaster control name
2197 * @tlv: TLV data (optional)
2198 * @slaves: slave control names (optional)
2199 *
2200 * Create a virtual master control with the given name. The TLV data
2201 * must be either NULL or a valid data.
2202 *
2203 * @slaves is a NULL-terminated array of strings, each of which is a
2204 * slave control name. All controls with these names are assigned to
2205 * the new virtual master control.
2206 *
2207 * This function returns zero if successful or a negative error code.
2208 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002209int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002210 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002211{
2212 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002213 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002214 int err;
2215
Takashi Iwai2f085542008-02-22 18:43:50 +01002216 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2217 ;
2218 if (!*s) {
2219 snd_printdd("No slave found for %s\n", name);
2220 return 0;
2221 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002222 kctl = snd_ctl_make_virtual_master(name, tlv);
2223 if (!kctl)
2224 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002225 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002226 if (err < 0)
2227 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002228
Takashi Iwai2134ea42008-01-10 16:53:55 +01002229 for (s = slaves; *s; s++) {
2230 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002231 int i = 0;
2232 for (;;) {
2233 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2234 if (!sctl) {
2235 if (!i)
2236 snd_printdd("Cannot find slave %s, "
2237 "skipped\n", *s);
2238 break;
2239 }
2240 err = snd_ctl_add_slave(kctl, sctl);
2241 if (err < 0)
2242 return err;
2243 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002244 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002245 }
2246 return 0;
2247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002248EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002249
Takashi Iwaid5191e52009-11-16 14:58:17 +01002250/**
2251 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2252 *
2253 * The control element is supposed to have the private_value field
2254 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2255 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002256int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2257 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 int chs = get_amp_channels(kcontrol);
2260
2261 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2262 uinfo->count = chs == 3 ? 2 : 1;
2263 uinfo->value.integer.min = 0;
2264 uinfo->value.integer.max = 1;
2265 return 0;
2266}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002267EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Takashi Iwaid5191e52009-11-16 14:58:17 +01002269/**
2270 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2271 *
2272 * The control element is supposed to have the private_value field
2273 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2274 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002275int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2276 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
2278 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2279 hda_nid_t nid = get_amp_nid(kcontrol);
2280 int chs = get_amp_channels(kcontrol);
2281 int dir = get_amp_direction(kcontrol);
2282 int idx = get_amp_index(kcontrol);
2283 long *valp = ucontrol->value.integer.value;
2284
2285 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002286 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002287 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002289 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002290 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return 0;
2292}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002293EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Takashi Iwaid5191e52009-11-16 14:58:17 +01002295/**
2296 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2297 *
2298 * The control element is supposed to have the private_value field
2299 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2300 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002301int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
2304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2305 hda_nid_t nid = get_amp_nid(kcontrol);
2306 int chs = get_amp_channels(kcontrol);
2307 int dir = get_amp_direction(kcontrol);
2308 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 long *valp = ucontrol->value.integer.value;
2310 int change = 0;
2311
Takashi Iwaicb53c622007-08-10 17:21:45 +02002312 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002313 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002314 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002315 HDA_AMP_MUTE,
2316 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002317 valp++;
2318 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002319 if (chs & 2)
2320 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002321 HDA_AMP_MUTE,
2322 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002323 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002324 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return change;
2326}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002327EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Takashi Iwai67d634c2009-11-16 15:35:59 +01002329#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002330/**
2331 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2332 *
2333 * This function calls snd_hda_enable_beep_device(), which behaves differently
2334 * depending on beep_mode option.
2335 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002336int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2337 struct snd_ctl_elem_value *ucontrol)
2338{
2339 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2340 long *valp = ucontrol->value.integer.value;
2341
2342 snd_hda_enable_beep_device(codec, *valp);
2343 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2344}
2345EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002346#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/*
Takashi Iwai985be542005-11-02 18:26:49 +01002349 * bound volume controls
2350 *
2351 * bind multiple volumes (# indices, from 0)
2352 */
2353
2354#define AMP_VAL_IDX_SHIFT 19
2355#define AMP_VAL_IDX_MASK (0x0f<<19)
2356
Takashi Iwaid5191e52009-11-16 14:58:17 +01002357/**
2358 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2359 *
2360 * The control element is supposed to have the private_value field
2361 * set up via HDA_BIND_MUTE*() macros.
2362 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002363int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2364 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002365{
2366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2367 unsigned long pval;
2368 int err;
2369
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002370 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002371 pval = kcontrol->private_value;
2372 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2373 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2374 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002375 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002376 return err;
2377}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002378EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002379
Takashi Iwaid5191e52009-11-16 14:58:17 +01002380/**
2381 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2382 *
2383 * The control element is supposed to have the private_value field
2384 * set up via HDA_BIND_MUTE*() macros.
2385 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002386int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2387 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002388{
2389 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2390 unsigned long pval;
2391 int i, indices, err = 0, change = 0;
2392
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002393 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002394 pval = kcontrol->private_value;
2395 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2396 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002397 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2398 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002399 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2400 if (err < 0)
2401 break;
2402 change |= err;
2403 }
2404 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002405 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002406 return err < 0 ? err : change;
2407}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002408EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002409
Takashi Iwaid5191e52009-11-16 14:58:17 +01002410/**
2411 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2412 *
2413 * The control element is supposed to have the private_value field
2414 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002415 */
2416int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_info *uinfo)
2418{
2419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2420 struct hda_bind_ctls *c;
2421 int err;
2422
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002423 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002424 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002425 kcontrol->private_value = *c->values;
2426 err = c->ops->info(kcontrol, uinfo);
2427 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002428 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002429 return err;
2430}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002431EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002432
Takashi Iwaid5191e52009-11-16 14:58:17 +01002433/**
2434 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2435 *
2436 * The control element is supposed to have the private_value field
2437 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2438 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002439int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2440 struct snd_ctl_elem_value *ucontrol)
2441{
2442 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2443 struct hda_bind_ctls *c;
2444 int err;
2445
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002446 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002447 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002448 kcontrol->private_value = *c->values;
2449 err = c->ops->get(kcontrol, ucontrol);
2450 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002451 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002452 return err;
2453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002454EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002455
Takashi Iwaid5191e52009-11-16 14:58:17 +01002456/**
2457 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2458 *
2459 * The control element is supposed to have the private_value field
2460 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2461 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002462int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2463 struct snd_ctl_elem_value *ucontrol)
2464{
2465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2466 struct hda_bind_ctls *c;
2467 unsigned long *vals;
2468 int err = 0, change = 0;
2469
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002470 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002471 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002472 for (vals = c->values; *vals; vals++) {
2473 kcontrol->private_value = *vals;
2474 err = c->ops->put(kcontrol, ucontrol);
2475 if (err < 0)
2476 break;
2477 change |= err;
2478 }
2479 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002480 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002481 return err < 0 ? err : change;
2482}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002483EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002484
Takashi Iwaid5191e52009-11-16 14:58:17 +01002485/**
2486 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2487 *
2488 * The control element is supposed to have the private_value field
2489 * set up via HDA_BIND_VOL() macro.
2490 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002491int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2492 unsigned int size, unsigned int __user *tlv)
2493{
2494 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2495 struct hda_bind_ctls *c;
2496 int err;
2497
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002498 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002499 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002500 kcontrol->private_value = *c->values;
2501 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2502 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002503 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002504 return err;
2505}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002506EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002507
2508struct hda_ctl_ops snd_hda_bind_vol = {
2509 .info = snd_hda_mixer_amp_volume_info,
2510 .get = snd_hda_mixer_amp_volume_get,
2511 .put = snd_hda_mixer_amp_volume_put,
2512 .tlv = snd_hda_mixer_amp_tlv
2513};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002514EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002515
2516struct hda_ctl_ops snd_hda_bind_sw = {
2517 .info = snd_hda_mixer_amp_switch_info,
2518 .get = snd_hda_mixer_amp_switch_get,
2519 .put = snd_hda_mixer_amp_switch_put,
2520 .tlv = snd_hda_mixer_amp_tlv
2521};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002522EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002523
2524/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 * SPDIF out controls
2526 */
2527
Takashi Iwai0ba21762007-04-16 11:29:14 +02002528static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2532 uinfo->count = 1;
2533 return 0;
2534}
2535
Takashi Iwai0ba21762007-04-16 11:29:14 +02002536static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
2539 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2540 IEC958_AES0_NONAUDIO |
2541 IEC958_AES0_CON_EMPHASIS_5015 |
2542 IEC958_AES0_CON_NOT_COPYRIGHT;
2543 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2544 IEC958_AES1_CON_ORIGINAL;
2545 return 0;
2546}
2547
Takashi Iwai0ba21762007-04-16 11:29:14 +02002548static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2549 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
2551 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2552 IEC958_AES0_NONAUDIO |
2553 IEC958_AES0_PRO_EMPHASIS_5015;
2554 return 0;
2555}
2556
Takashi Iwai0ba21762007-04-16 11:29:14 +02002557static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002561 int idx = kcontrol->private_value;
2562 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Stephen Warren7c9359762011-06-01 11:14:17 -06002564 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2565 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2566 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2567 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 return 0;
2570}
2571
2572/* convert from SPDIF status bits to HDA SPDIF bits
2573 * bit 0 (DigEn) is always set zero (to be filled later)
2574 */
2575static unsigned short convert_from_spdif_status(unsigned int sbits)
2576{
2577 unsigned short val = 0;
2578
2579 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002580 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002582 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002584 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2585 IEC958_AES0_PRO_EMPHASIS_5015)
2586 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002588 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2589 IEC958_AES0_CON_EMPHASIS_5015)
2590 val |= AC_DIG1_EMPHASIS;
2591 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2592 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002594 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2596 }
2597 return val;
2598}
2599
2600/* convert to SPDIF status bits from HDA SPDIF bits
2601 */
2602static unsigned int convert_to_spdif_status(unsigned short val)
2603{
2604 unsigned int sbits = 0;
2605
Takashi Iwai0ba21762007-04-16 11:29:14 +02002606 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002608 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 sbits |= IEC958_AES0_PROFESSIONAL;
2610 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002611 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2613 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002614 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002616 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002618 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2620 sbits |= val & (0x7f << 8);
2621 }
2622 return sbits;
2623}
2624
Takashi Iwai2f728532008-09-25 16:32:41 +02002625/* set digital convert verbs both for the given NID and its slaves */
2626static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2627 int verb, int val)
2628{
Takashi Iwaidda14412011-05-02 11:29:30 +02002629 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002630
Takashi Iwai9e976972008-11-25 08:17:20 +01002631 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002632 d = codec->slave_dig_outs;
2633 if (!d)
2634 return;
2635 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002636 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002637}
2638
2639static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2640 int dig1, int dig2)
2641{
2642 if (dig1 != -1)
2643 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2644 if (dig2 != -1)
2645 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2646}
2647
Takashi Iwai0ba21762007-04-16 11:29:14 +02002648static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2649 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002652 int idx = kcontrol->private_value;
2653 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2654 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 unsigned short val;
2656 int change;
2657
Ingo Molnar62932df2006-01-16 16:34:20 +01002658 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002659 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2661 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2662 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002663 val = convert_from_spdif_status(spdif->status);
2664 val |= spdif->ctls & 1;
2665 change = spdif->ctls != val;
2666 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002667 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002668 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002669 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return change;
2671}
2672
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002673#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Takashi Iwai0ba21762007-04-16 11:29:14 +02002675static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2676 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
2678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002679 int idx = kcontrol->private_value;
2680 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Stephen Warren7c9359762011-06-01 11:14:17 -06002682 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return 0;
2684}
2685
Stephen Warren74b654c2011-06-01 11:14:18 -06002686static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2687 int dig1, int dig2)
2688{
2689 set_dig_out_convert(codec, nid, dig1, dig2);
2690 /* unmute amp switch (if any) */
2691 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2692 (dig1 & AC_DIG1_ENABLE))
2693 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2694 HDA_AMP_MUTE, 0);
2695}
2696
Takashi Iwai0ba21762007-04-16 11:29:14 +02002697static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2698 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002701 int idx = kcontrol->private_value;
2702 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2703 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 unsigned short val;
2705 int change;
2706
Ingo Molnar62932df2006-01-16 16:34:20 +01002707 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002708 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002710 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002711 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002712 spdif->ctls = val;
2713 if (change && nid != (u16)-1)
2714 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002715 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return change;
2717}
2718
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002719static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 {
2721 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2722 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002723 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 .info = snd_hda_spdif_mask_info,
2725 .get = snd_hda_spdif_cmask_get,
2726 },
2727 {
2728 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2729 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002730 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 .info = snd_hda_spdif_mask_info,
2732 .get = snd_hda_spdif_pmask_get,
2733 },
2734 {
2735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002736 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 .info = snd_hda_spdif_mask_info,
2738 .get = snd_hda_spdif_default_get,
2739 .put = snd_hda_spdif_default_put,
2740 },
2741 {
2742 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002743 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 .info = snd_hda_spdif_out_switch_info,
2745 .get = snd_hda_spdif_out_switch_get,
2746 .put = snd_hda_spdif_out_switch_put,
2747 },
2748 { } /* end */
2749};
2750
2751/**
2752 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2753 * @codec: the HDA codec
2754 * @nid: audio out widget NID
2755 *
2756 * Creates controls related with the SPDIF output.
2757 * Called from each patch supporting the SPDIF out.
2758 *
2759 * Returns 0 if successful, or a negative error code.
2760 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002761int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2762 hda_nid_t associated_nid,
2763 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002766 struct snd_kcontrol *kctl;
2767 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002768 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002769 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Takashi Iwai1afe2062010-12-23 10:17:52 +01002771 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2772 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002773 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2774 return -EBUSY;
2775 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002776 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2778 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002779 if (!kctl)
2780 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002781 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002782 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002783 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002784 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return err;
2786 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002787 spdif->nid = cvt_nid;
2788 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06002789 AC_VERB_GET_DIGI_CONVERT_1, 0);
2790 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return 0;
2792}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002793EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Stephen Warren7c9359762011-06-01 11:14:17 -06002795struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2796 hda_nid_t nid)
2797{
2798 int i;
2799 for (i = 0; i < codec->spdif_out.used; i++) {
2800 struct hda_spdif_out *spdif =
2801 snd_array_elem(&codec->spdif_out, i);
2802 if (spdif->nid == nid)
2803 return spdif;
2804 }
2805 return NULL;
2806}
2807EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2808
Stephen Warren74b654c2011-06-01 11:14:18 -06002809void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2810{
2811 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2812
2813 mutex_lock(&codec->spdif_mutex);
2814 spdif->nid = (u16)-1;
2815 mutex_unlock(&codec->spdif_mutex);
2816}
2817EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2818
2819void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2820{
2821 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2822 unsigned short val;
2823
2824 mutex_lock(&codec->spdif_mutex);
2825 if (spdif->nid != nid) {
2826 spdif->nid = nid;
2827 val = spdif->ctls;
2828 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2829 }
2830 mutex_unlock(&codec->spdif_mutex);
2831}
2832EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002835 * SPDIF sharing with analog output
2836 */
2837static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2838 struct snd_ctl_elem_value *ucontrol)
2839{
2840 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2841 ucontrol->value.integer.value[0] = mout->share_spdif;
2842 return 0;
2843}
2844
2845static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
2847{
2848 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2849 mout->share_spdif = !!ucontrol->value.integer.value[0];
2850 return 0;
2851}
2852
2853static struct snd_kcontrol_new spdif_share_sw = {
2854 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2855 .name = "IEC958 Default PCM Playback Switch",
2856 .info = snd_ctl_boolean_mono_info,
2857 .get = spdif_share_sw_get,
2858 .put = spdif_share_sw_put,
2859};
2860
Takashi Iwaid5191e52009-11-16 14:58:17 +01002861/**
2862 * snd_hda_create_spdif_share_sw - create Default PCM switch
2863 * @codec: the HDA codec
2864 * @mout: multi-out instance
2865 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002866int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2867 struct hda_multi_out *mout)
2868{
2869 if (!mout->dig_out_nid)
2870 return 0;
2871 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002872 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2873 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002874}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002875EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002876
2877/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 * SPDIF input
2879 */
2880
2881#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2882
Takashi Iwai0ba21762007-04-16 11:29:14 +02002883static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2884 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2887
2888 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2889 return 0;
2890}
2891
Takashi Iwai0ba21762007-04-16 11:29:14 +02002892static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2893 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
2895 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2896 hda_nid_t nid = kcontrol->private_value;
2897 unsigned int val = !!ucontrol->value.integer.value[0];
2898 int change;
2899
Ingo Molnar62932df2006-01-16 16:34:20 +01002900 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002902 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002904 snd_hda_codec_write_cache(codec, nid, 0,
2905 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002907 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return change;
2909}
2910
Takashi Iwai0ba21762007-04-16 11:29:14 +02002911static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2912 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2915 hda_nid_t nid = kcontrol->private_value;
2916 unsigned short val;
2917 unsigned int sbits;
2918
Andrew Paprocki3982d172007-12-19 12:13:44 +01002919 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 sbits = convert_to_spdif_status(val);
2921 ucontrol->value.iec958.status[0] = sbits;
2922 ucontrol->value.iec958.status[1] = sbits >> 8;
2923 ucontrol->value.iec958.status[2] = sbits >> 16;
2924 ucontrol->value.iec958.status[3] = sbits >> 24;
2925 return 0;
2926}
2927
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002928static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 {
2930 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002931 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 .info = snd_hda_spdif_in_switch_info,
2933 .get = snd_hda_spdif_in_switch_get,
2934 .put = snd_hda_spdif_in_switch_put,
2935 },
2936 {
2937 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2938 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002939 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 .info = snd_hda_spdif_mask_info,
2941 .get = snd_hda_spdif_in_status_get,
2942 },
2943 { } /* end */
2944};
2945
2946/**
2947 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2948 * @codec: the HDA codec
2949 * @nid: audio in widget NID
2950 *
2951 * Creates controls related with the SPDIF input.
2952 * Called from each patch supporting the SPDIF in.
2953 *
2954 * Returns 0 if successful, or a negative error code.
2955 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002956int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
2958 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002959 struct snd_kcontrol *kctl;
2960 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002961 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Takashi Iwai1afe2062010-12-23 10:17:52 +01002963 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
2964 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002965 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2966 return -EBUSY;
2967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2969 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002970 if (!kctl)
2971 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002973 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002974 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return err;
2976 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002977 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002978 snd_hda_codec_read(codec, nid, 0,
2979 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002980 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return 0;
2982}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002983EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Takashi Iwaicb53c622007-08-10 17:21:45 +02002985#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002986/*
2987 * command cache
2988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002990/* build a 32bit cache key with the widget id and the command parameter */
2991#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2992#define get_cmd_cache_nid(key) ((key) & 0xff)
2993#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2994
2995/**
2996 * snd_hda_codec_write_cache - send a single command with caching
2997 * @codec: the HDA codec
2998 * @nid: NID to send the command
2999 * @direct: direct flag
3000 * @verb: the verb to send
3001 * @parm: the parameter for the verb
3002 *
3003 * Send a single command without waiting for response.
3004 *
3005 * Returns 0 if successful, or a negative error code.
3006 */
3007int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3008 int direct, unsigned int verb, unsigned int parm)
3009{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003010 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3011 struct hda_cache_head *c;
3012 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003013
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003014 if (err < 0)
3015 return err;
3016 /* parm may contain the verb stuff for get/set amp */
3017 verb = verb | (parm >> 8);
3018 parm &= 0xff;
3019 key = build_cmd_cache_key(nid, verb);
3020 mutex_lock(&codec->bus->cmd_mutex);
3021 c = get_alloc_hash(&codec->cmd_cache, key);
3022 if (c)
3023 c->val = parm;
3024 mutex_unlock(&codec->bus->cmd_mutex);
3025 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003026}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003027EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003028
Takashi Iwaid5191e52009-11-16 14:58:17 +01003029/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003030 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3031 * @codec: the HDA codec
3032 * @nid: NID to send the command
3033 * @direct: direct flag
3034 * @verb: the verb to send
3035 * @parm: the parameter for the verb
3036 *
3037 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3038 * command if the parameter is already identical with the cached value.
3039 * If not, it sends the command and refreshes the cache.
3040 *
3041 * Returns 0 if successful, or a negative error code.
3042 */
3043int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3044 int direct, unsigned int verb, unsigned int parm)
3045{
3046 struct hda_cache_head *c;
3047 u32 key;
3048
3049 /* parm may contain the verb stuff for get/set amp */
3050 verb = verb | (parm >> 8);
3051 parm &= 0xff;
3052 key = build_cmd_cache_key(nid, verb);
3053 mutex_lock(&codec->bus->cmd_mutex);
3054 c = get_hash(&codec->cmd_cache, key);
3055 if (c && c->val == parm) {
3056 mutex_unlock(&codec->bus->cmd_mutex);
3057 return 0;
3058 }
3059 mutex_unlock(&codec->bus->cmd_mutex);
3060 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3061}
3062EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3063
3064/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003065 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3066 * @codec: HD-audio codec
3067 *
3068 * Execute all verbs recorded in the command caches to resume.
3069 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003070void snd_hda_codec_resume_cache(struct hda_codec *codec)
3071{
Takashi Iwai603c4012008-07-30 15:01:44 +02003072 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003073 int i;
3074
Takashi Iwai603c4012008-07-30 15:01:44 +02003075 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003076 u32 key = buffer->key;
3077 if (!key)
3078 continue;
3079 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3080 get_cmd_cache_cmd(key), buffer->val);
3081 }
3082}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003083EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003084
3085/**
3086 * snd_hda_sequence_write_cache - sequence writes with caching
3087 * @codec: the HDA codec
3088 * @seq: VERB array to send
3089 *
3090 * Send the commands sequentially from the given array.
3091 * Thte commands are recorded on cache for power-save and resume.
3092 * The array must be terminated with NID=0.
3093 */
3094void snd_hda_sequence_write_cache(struct hda_codec *codec,
3095 const struct hda_verb *seq)
3096{
3097 for (; seq->nid; seq++)
3098 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3099 seq->param);
3100}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003101EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003102#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003103
Takashi Iwai54d17402005-11-21 16:33:22 +01003104/*
3105 * set power state of the codec
3106 */
3107static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3108 unsigned int power_state)
3109{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003110 hda_nid_t nid;
3111 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003112
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003113 /* this delay seems necessary to avoid click noise at power-down */
3114 if (power_state == AC_PWRST_D3)
3115 msleep(100);
3116 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003117 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003118 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003119 if (power_state == AC_PWRST_D0 &&
3120 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003121 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003122
Takashi Iwaicb53c622007-08-10 17:21:45 +02003123 nid = codec->start_nid;
3124 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003125 unsigned int wcaps = get_wcaps(codec, nid);
3126 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003127 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003128 if (power_state == AC_PWRST_D3 &&
3129 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003130 unsigned int pincap;
3131 /*
3132 * don't power down the widget if it controls
3133 * eapd and EAPD_BTLENABLE is set.
3134 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003135 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003136 if (pincap & AC_PINCAP_EAPD) {
3137 int eapd = snd_hda_codec_read(codec,
3138 nid, 0,
3139 AC_VERB_GET_EAPD_BTLENABLE, 0);
3140 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003141 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003142 continue;
3143 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003144 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003145 snd_hda_codec_write(codec, nid, 0,
3146 AC_VERB_SET_POWER_STATE,
3147 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003148 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003149 }
3150
Takashi Iwaicb53c622007-08-10 17:21:45 +02003151 if (power_state == AC_PWRST_D0) {
3152 unsigned long end_time;
3153 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003154 /* wait until the codec reachs to D0 */
3155 end_time = jiffies + msecs_to_jiffies(500);
3156 do {
3157 state = snd_hda_codec_read(codec, fg, 0,
3158 AC_VERB_GET_POWER_STATE, 0);
3159 if (state == power_state)
3160 break;
3161 msleep(1);
3162 } while (time_after_eq(end_time, jiffies));
3163 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003164}
3165
Takashi Iwai11aeff02008-07-30 15:01:46 +02003166#ifdef CONFIG_SND_HDA_HWDEP
3167/* execute additional init verbs */
3168static void hda_exec_init_verbs(struct hda_codec *codec)
3169{
3170 if (codec->init_verbs.list)
3171 snd_hda_sequence_write(codec, codec->init_verbs.list);
3172}
3173#else
3174static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3175#endif
3176
Takashi Iwaicb53c622007-08-10 17:21:45 +02003177#ifdef SND_HDA_NEEDS_RESUME
3178/*
3179 * call suspend and power-down; used both from PM and power-save
3180 */
3181static void hda_call_codec_suspend(struct hda_codec *codec)
3182{
3183 if (codec->patch_ops.suspend)
3184 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003185 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003186 hda_set_power_state(codec,
3187 codec->afg ? codec->afg : codec->mfg,
3188 AC_PWRST_D3);
3189#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003190 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003191 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003192 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003193 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003194 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003195#endif
3196}
3197
3198/*
3199 * kick up codec; used both from PM and power-save
3200 */
3201static void hda_call_codec_resume(struct hda_codec *codec)
3202{
3203 hda_set_power_state(codec,
3204 codec->afg ? codec->afg : codec->mfg,
3205 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003206 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003207 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003208 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003209 if (codec->patch_ops.resume)
3210 codec->patch_ops.resume(codec);
3211 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003212 if (codec->patch_ops.init)
3213 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003214 snd_hda_codec_resume_amp(codec);
3215 snd_hda_codec_resume_cache(codec);
3216 }
3217}
3218#endif /* SND_HDA_NEEDS_RESUME */
3219
Takashi Iwai54d17402005-11-21 16:33:22 +01003220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221/**
3222 * snd_hda_build_controls - build mixer controls
3223 * @bus: the BUS
3224 *
3225 * Creates mixer controls for each codec included in the bus.
3226 *
3227 * Returns 0 if successful, otherwise a negative error code.
3228 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003229int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003231 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Takashi Iwai0ba21762007-04-16 11:29:14 +02003233 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003234 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003235 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003236 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003237 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003238 err = snd_hda_codec_reset(codec);
3239 if (err < 0) {
3240 printk(KERN_ERR
3241 "hda_codec: cannot revert codec\n");
3242 return err;
3243 }
3244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003246 return 0;
3247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003248EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003250int snd_hda_codec_build_controls(struct hda_codec *codec)
3251{
3252 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003253 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003254 /* continue to initialize... */
3255 if (codec->patch_ops.init)
3256 err = codec->patch_ops.init(codec);
3257 if (!err && codec->patch_ops.build_controls)
3258 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003259 if (err < 0)
3260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return 0;
3262}
3263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264/*
3265 * stream formats
3266 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003267struct hda_rate_tbl {
3268 unsigned int hz;
3269 unsigned int alsa_bits;
3270 unsigned int hda_fmt;
3271};
3272
Takashi Iwai92f10b32010-08-03 14:21:00 +02003273/* rate = base * mult / div */
3274#define HDA_RATE(base, mult, div) \
3275 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3276 (((div) - 1) << AC_FMT_DIV_SHIFT))
3277
Takashi Iwaibefdf312005-08-22 13:57:55 +02003278static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003280
3281 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003282 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3283 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3284 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3285 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3286 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3287 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3288 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3289 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3290 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3291 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3292 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003293#define AC_PAR_PCM_RATE_BITS 11
3294 /* up to bits 10, 384kHZ isn't supported properly */
3295
3296 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003297 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003298
Takashi Iwaibefdf312005-08-22 13:57:55 +02003299 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300};
3301
3302/**
3303 * snd_hda_calc_stream_format - calculate format bitset
3304 * @rate: the sample rate
3305 * @channels: the number of channels
3306 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3307 * @maxbps: the max. bps
3308 *
3309 * Calculate the format bitset from the given rate, channels and th PCM format.
3310 *
3311 * Return zero if invalid.
3312 */
3313unsigned int snd_hda_calc_stream_format(unsigned int rate,
3314 unsigned int channels,
3315 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003316 unsigned int maxbps,
3317 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
3319 int i;
3320 unsigned int val = 0;
3321
Takashi Iwaibefdf312005-08-22 13:57:55 +02003322 for (i = 0; rate_bits[i].hz; i++)
3323 if (rate_bits[i].hz == rate) {
3324 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 break;
3326 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003327 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 snd_printdd("invalid rate %d\n", rate);
3329 return 0;
3330 }
3331
3332 if (channels == 0 || channels > 8) {
3333 snd_printdd("invalid channels %d\n", channels);
3334 return 0;
3335 }
3336 val |= channels - 1;
3337
3338 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003339 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003340 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003341 break;
3342 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003343 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 case 20:
3346 case 24:
3347 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003348 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003349 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003351 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003353 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 break;
3355 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003356 snd_printdd("invalid format width %d\n",
3357 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 return 0;
3359 }
3360
Anssi Hannula32c168c2010-08-03 13:28:57 +03003361 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003362 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return val;
3365}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003366EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003368static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3369{
3370 unsigned int val = 0;
3371 if (nid != codec->afg &&
3372 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3373 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3374 if (!val || val == -1)
3375 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3376 if (!val || val == -1)
3377 return 0;
3378 return val;
3379}
3380
3381static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3382{
3383 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3384 get_pcm_param);
3385}
3386
3387static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3388{
3389 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3390 if (!streams || streams == -1)
3391 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3392 if (!streams || streams == -1)
3393 return 0;
3394 return streams;
3395}
3396
3397static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3398{
3399 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3400 get_stream_param);
3401}
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403/**
3404 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3405 * @codec: the HDA codec
3406 * @nid: NID to query
3407 * @ratesp: the pointer to store the detected rate bitflags
3408 * @formatsp: the pointer to store the detected formats
3409 * @bpsp: the pointer to store the detected format widths
3410 *
3411 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3412 * or @bsps argument is ignored.
3413 *
3414 * Returns 0 if successful, otherwise a negative error code.
3415 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003416int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3418{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003419 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003421 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003422 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 if (ratesp) {
3425 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003426 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003428 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003430 if (rates == 0) {
3431 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3432 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3433 nid, val,
3434 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3435 return -EIO;
3436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 *ratesp = rates;
3438 }
3439
3440 if (formatsp || bpsp) {
3441 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003442 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003444 streams = query_stream_param(codec, nid);
3445 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
3448 bps = 0;
3449 if (streams & AC_SUPFMT_PCM) {
3450 if (val & AC_SUPPCM_BITS_8) {
3451 formats |= SNDRV_PCM_FMTBIT_U8;
3452 bps = 8;
3453 }
3454 if (val & AC_SUPPCM_BITS_16) {
3455 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3456 bps = 16;
3457 }
3458 if (wcaps & AC_WCAP_DIGITAL) {
3459 if (val & AC_SUPPCM_BITS_32)
3460 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3461 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3462 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3463 if (val & AC_SUPPCM_BITS_24)
3464 bps = 24;
3465 else if (val & AC_SUPPCM_BITS_20)
3466 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003467 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3468 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3470 if (val & AC_SUPPCM_BITS_32)
3471 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 else if (val & AC_SUPPCM_BITS_24)
3473 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003474 else if (val & AC_SUPPCM_BITS_20)
3475 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 }
3477 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003478 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003480 if (!bps)
3481 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003482 }
3483 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003484 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 /* temporary hack: we have still no proper support
3486 * for the direct AC3 stream...
3487 */
3488 formats |= SNDRV_PCM_FMTBIT_U8;
3489 bps = 8;
3490 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003491 if (formats == 0) {
3492 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3493 "(nid=0x%x, val=0x%x, ovrd=%i, "
3494 "streams=0x%x)\n",
3495 nid, val,
3496 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3497 streams);
3498 return -EIO;
3499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 if (formatsp)
3501 *formatsp = formats;
3502 if (bpsp)
3503 *bpsp = bps;
3504 }
3505
3506 return 0;
3507}
Stephen Warren384a48d2011-06-01 11:14:21 -06003508EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003511 * snd_hda_is_supported_format - Check the validity of the format
3512 * @codec: HD-audio codec
3513 * @nid: NID to check
3514 * @format: the HD-audio format value to check
3515 *
3516 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 *
3518 * Returns 1 if supported, 0 if not.
3519 */
3520int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3521 unsigned int format)
3522{
3523 int i;
3524 unsigned int val = 0, rate, stream;
3525
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003526 val = query_pcm_param(codec, nid);
3527 if (!val)
3528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
3530 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003531 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003532 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 if (val & (1 << i))
3534 break;
3535 return 0;
3536 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003537 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 return 0;
3539
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003540 stream = query_stream_param(codec, nid);
3541 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 return 0;
3543
3544 if (stream & AC_SUPFMT_PCM) {
3545 switch (format & 0xf0) {
3546 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003547 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return 0;
3549 break;
3550 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003551 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return 0;
3553 break;
3554 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003555 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return 0;
3557 break;
3558 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003559 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return 0;
3561 break;
3562 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003563 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return 0;
3565 break;
3566 default:
3567 return 0;
3568 }
3569 } else {
3570 /* FIXME: check for float32 and AC3? */
3571 }
3572
3573 return 1;
3574}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003575EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577/*
3578 * PCM stuff
3579 */
3580static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3581 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003582 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
3584 return 0;
3585}
3586
3587static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3588 struct hda_codec *codec,
3589 unsigned int stream_tag,
3590 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003591 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3594 return 0;
3595}
3596
3597static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3598 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003599 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
Takashi Iwai888afa12008-03-18 09:57:50 +01003601 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 return 0;
3603}
3604
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003605static int set_pcm_default_values(struct hda_codec *codec,
3606 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003608 int err;
3609
Takashi Iwai0ba21762007-04-16 11:29:14 +02003610 /* query support PCM information from the given NID */
3611 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003612 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003613 info->rates ? NULL : &info->rates,
3614 info->formats ? NULL : &info->formats,
3615 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003616 if (err < 0)
3617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 }
3619 if (info->ops.open == NULL)
3620 info->ops.open = hda_pcm_default_open_close;
3621 if (info->ops.close == NULL)
3622 info->ops.close = hda_pcm_default_open_close;
3623 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003624 if (snd_BUG_ON(!info->nid))
3625 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 info->ops.prepare = hda_pcm_default_prepare;
3627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003629 if (snd_BUG_ON(!info->nid))
3630 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 info->ops.cleanup = hda_pcm_default_cleanup;
3632 }
3633 return 0;
3634}
3635
Takashi Iwaieb541332010-08-06 13:48:11 +02003636/*
3637 * codec prepare/cleanup entries
3638 */
3639int snd_hda_codec_prepare(struct hda_codec *codec,
3640 struct hda_pcm_stream *hinfo,
3641 unsigned int stream,
3642 unsigned int format,
3643 struct snd_pcm_substream *substream)
3644{
3645 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003646 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003647 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3648 if (ret >= 0)
3649 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003650 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003651 return ret;
3652}
3653EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3654
3655void snd_hda_codec_cleanup(struct hda_codec *codec,
3656 struct hda_pcm_stream *hinfo,
3657 struct snd_pcm_substream *substream)
3658{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003659 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003660 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003661 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003662}
3663EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3664
Takashi Iwaid5191e52009-11-16 14:58:17 +01003665/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003666const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3667 "Audio", "SPDIF", "HDMI", "Modem"
3668};
3669
Takashi Iwai176d5332008-07-30 15:01:44 +02003670/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003671 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003672 *
3673 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003674 */
3675static int get_empty_pcm_device(struct hda_bus *bus, int type)
3676{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003677 /* audio device indices; not linear to keep compatibility */
3678 static int audio_idx[HDA_PCM_NTYPES][5] = {
3679 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3680 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003681 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003682 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003683 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003684 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003685
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003686 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003687 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3688 return -EINVAL;
3689 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003690
3691 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3692 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3693 return audio_idx[type][i];
3694
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003695 snd_printk(KERN_WARNING "Too many %s devices\n",
3696 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003697 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003698}
3699
3700/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003701 * attach a new PCM stream
3702 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003703static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003704{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003705 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003706 struct hda_pcm_stream *info;
3707 int stream, err;
3708
Takashi Iwaib91f0802008-11-04 08:43:08 +01003709 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003710 return -EINVAL;
3711 for (stream = 0; stream < 2; stream++) {
3712 info = &pcm->stream[stream];
3713 if (info->substreams) {
3714 err = set_pcm_default_values(codec, info);
3715 if (err < 0)
3716 return err;
3717 }
3718 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003719 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003720}
3721
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003722/* assign all PCMs of the given codec */
3723int snd_hda_codec_build_pcms(struct hda_codec *codec)
3724{
3725 unsigned int pcm;
3726 int err;
3727
3728 if (!codec->num_pcms) {
3729 if (!codec->patch_ops.build_pcms)
3730 return 0;
3731 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003732 if (err < 0) {
3733 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003734 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003735 err = snd_hda_codec_reset(codec);
3736 if (err < 0) {
3737 printk(KERN_ERR
3738 "hda_codec: cannot revert codec\n");
3739 return err;
3740 }
3741 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003742 }
3743 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3744 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3745 int dev;
3746
3747 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003748 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003749
3750 if (!cpcm->pcm) {
3751 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3752 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003753 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003754 cpcm->device = dev;
3755 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003756 if (err < 0) {
3757 printk(KERN_ERR "hda_codec: cannot attach "
3758 "PCM stream %d for codec #%d\n",
3759 dev, codec->addr);
3760 continue; /* no fatal error */
3761 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003762 }
3763 }
3764 return 0;
3765}
3766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767/**
3768 * snd_hda_build_pcms - build PCM information
3769 * @bus: the BUS
3770 *
3771 * Create PCM information for each codec included in the bus.
3772 *
3773 * The build_pcms codec patch is requested to set up codec->num_pcms and
3774 * codec->pcm_info properly. The array is referred by the top-level driver
3775 * to create its PCM instances.
3776 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3777 * callback.
3778 *
3779 * At least, substreams, channels_min and channels_max must be filled for
3780 * each stream. substreams = 0 indicates that the stream doesn't exist.
3781 * When rates and/or formats are zero, the supported values are queried
3782 * from the given nid. The nid is used also by the default ops.prepare
3783 * and ops.cleanup callbacks.
3784 *
3785 * The driver needs to call ops.open in its open callback. Similarly,
3786 * ops.close is supposed to be called in the close callback.
3787 * ops.prepare should be called in the prepare or hw_params callback
3788 * with the proper parameters for set up.
3789 * ops.cleanup should be called in hw_free for clean up of streams.
3790 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003791 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003793int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003795 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Takashi Iwai0ba21762007-04-16 11:29:14 +02003797 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003798 int err = snd_hda_codec_build_pcms(codec);
3799 if (err < 0)
3800 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
3802 return 0;
3803}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003804EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806/**
3807 * snd_hda_check_board_config - compare the current codec with the config table
3808 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003809 * @num_configs: number of config enums
3810 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 * @tbl: configuration table, terminated by null entries
3812 *
3813 * Compares the modelname or PCI subsystem id of the current codec with the
3814 * given configuration table. If a matching entry is found, returns its
3815 * config value (supposed to be 0 or positive).
3816 *
3817 * If no entries are matching, the function returns a negative value.
3818 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003819int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003820 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003821 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003823 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003824 int i;
3825 for (i = 0; i < num_configs; i++) {
3826 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003827 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003828 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3829 "selected\n", models[i]);
3830 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 }
3832 }
3833 }
3834
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003835 if (!codec->bus->pci || !tbl)
3836 return -1;
3837
3838 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3839 if (!tbl)
3840 return -1;
3841 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003842#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003843 char tmp[10];
3844 const char *model = NULL;
3845 if (models)
3846 model = models[tbl->value];
3847 if (!model) {
3848 sprintf(tmp, "#%d", tbl->value);
3849 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003851 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3852 "for config %x:%x (%s)\n",
3853 model, tbl->subvendor, tbl->subdevice,
3854 (tbl->name ? tbl->name : "Unknown device"));
3855#endif
3856 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 }
3858 return -1;
3859}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003860EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003863 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003864 subsystem ID with the
3865 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003866
3867 This is important for Gateway notebooks with SB450 HDA Audio
3868 where the vendor ID of the PCI device is:
3869 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3870 and the vendor/subvendor are found only at the codec.
3871
3872 * @codec: the HDA codec
3873 * @num_configs: number of config enums
3874 * @models: array of model name strings
3875 * @tbl: configuration table, terminated by null entries
3876 *
3877 * Compares the modelname or PCI subsystem id of the current codec with the
3878 * given configuration table. If a matching entry is found, returns its
3879 * config value (supposed to be 0 or positive).
3880 *
3881 * If no entries are matching, the function returns a negative value.
3882 */
3883int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003884 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003885 const struct snd_pci_quirk *tbl)
3886{
3887 const struct snd_pci_quirk *q;
3888
3889 /* Search for codec ID */
3890 for (q = tbl; q->subvendor; q++) {
3891 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3892
3893 if (vendorid == codec->subsystem_id)
3894 break;
3895 }
3896
3897 if (!q->subvendor)
3898 return -1;
3899
3900 tbl = q;
3901
3902 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003903#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003904 char tmp[10];
3905 const char *model = NULL;
3906 if (models)
3907 model = models[tbl->value];
3908 if (!model) {
3909 sprintf(tmp, "#%d", tbl->value);
3910 model = tmp;
3911 }
3912 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3913 "for config %x:%x (%s)\n",
3914 model, tbl->subvendor, tbl->subdevice,
3915 (tbl->name ? tbl->name : "Unknown device"));
3916#endif
3917 return tbl->value;
3918 }
3919 return -1;
3920}
3921EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3922
3923/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 * snd_hda_add_new_ctls - create controls from the array
3925 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003926 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 *
3928 * This helper function creates and add new controls in the given array.
3929 * The array must be terminated with an empty entry as terminator.
3930 *
3931 * Returns 0 if successful, or a negative error code.
3932 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003933int snd_hda_add_new_ctls(struct hda_codec *codec,
3934 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003936 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
3938 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003939 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003940 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003941 if (knew->iface == -1) /* skip this codec private value */
3942 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003943 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003944 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003945 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003946 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003947 if (addr > 0)
3948 kctl->id.device = addr;
3949 if (idx > 0)
3950 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003951 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003952 if (!err)
3953 break;
3954 /* try first with another device index corresponding to
3955 * the codec addr; if it still fails (or it's the
3956 * primary codec), then try another control index
3957 */
3958 if (!addr && codec->addr)
3959 addr = codec->addr;
3960 else if (!idx && !knew->index) {
3961 idx = find_empty_mixer_ctl_idx(codec,
3962 knew->name);
3963 if (idx <= 0)
3964 return err;
3965 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003966 return err;
3967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
3969 return 0;
3970}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003971EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
Takashi Iwaicb53c622007-08-10 17:21:45 +02003973#ifdef CONFIG_SND_HDA_POWER_SAVE
3974static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3975 unsigned int power_state);
3976
3977static void hda_power_work(struct work_struct *work)
3978{
3979 struct hda_codec *codec =
3980 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003981 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003982
Maxim Levitsky2e492462007-09-03 15:26:57 +02003983 if (!codec->power_on || codec->power_count) {
3984 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003985 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003986 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003987
3988 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003989 if (bus->ops.pm_notify)
3990 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003991}
3992
3993static void hda_keep_power_on(struct hda_codec *codec)
3994{
3995 codec->power_count++;
3996 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003997 codec->power_jiffies = jiffies;
3998}
3999
Takashi Iwaid5191e52009-11-16 14:58:17 +01004000/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004001void snd_hda_update_power_acct(struct hda_codec *codec)
4002{
4003 unsigned long delta = jiffies - codec->power_jiffies;
4004 if (codec->power_on)
4005 codec->power_on_acct += delta;
4006 else
4007 codec->power_off_acct += delta;
4008 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004009}
4010
Takashi Iwaid5191e52009-11-16 14:58:17 +01004011/**
4012 * snd_hda_power_up - Power-up the codec
4013 * @codec: HD-audio codec
4014 *
4015 * Increment the power-up counter and power up the hardware really when
4016 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004017 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004018void snd_hda_power_up(struct hda_codec *codec)
4019{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004020 struct hda_bus *bus = codec->bus;
4021
Takashi Iwaicb53c622007-08-10 17:21:45 +02004022 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004023 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004024 return;
4025
Takashi Iwaia2f63092009-11-11 09:34:25 +01004026 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004027 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004028 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004029 if (bus->ops.pm_notify)
4030 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004031 hda_call_codec_resume(codec);
4032 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004033 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004034}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004035EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004036
4037#define power_save(codec) \
4038 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004039
Takashi Iwaid5191e52009-11-16 14:58:17 +01004040/**
4041 * snd_hda_power_down - Power-down the codec
4042 * @codec: HD-audio codec
4043 *
4044 * Decrement the power-up counter and schedules the power-off work if
4045 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004046 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004047void snd_hda_power_down(struct hda_codec *codec)
4048{
4049 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004050 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004051 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004052 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004053 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004054 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004055 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004056 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004057}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004058EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004059
Takashi Iwaid5191e52009-11-16 14:58:17 +01004060/**
4061 * snd_hda_check_amp_list_power - Check the amp list and update the power
4062 * @codec: HD-audio codec
4063 * @check: the object containing an AMP list and the status
4064 * @nid: NID to check / update
4065 *
4066 * Check whether the given NID is in the amp list. If it's in the list,
4067 * check the current AMP status, and update the the power-status according
4068 * to the mute status.
4069 *
4070 * This function is supposed to be set or called from the check_power_status
4071 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004072 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004073int snd_hda_check_amp_list_power(struct hda_codec *codec,
4074 struct hda_loopback_check *check,
4075 hda_nid_t nid)
4076{
Takashi Iwai031024e2011-05-02 11:29:30 +02004077 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004078 int ch, v;
4079
4080 if (!check->amplist)
4081 return 0;
4082 for (p = check->amplist; p->nid; p++) {
4083 if (p->nid == nid)
4084 break;
4085 }
4086 if (!p->nid)
4087 return 0; /* nothing changed */
4088
4089 for (p = check->amplist; p->nid; p++) {
4090 for (ch = 0; ch < 2; ch++) {
4091 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4092 p->idx);
4093 if (!(v & HDA_AMP_MUTE) && v > 0) {
4094 if (!check->power_on) {
4095 check->power_on = 1;
4096 snd_hda_power_up(codec);
4097 }
4098 return 1;
4099 }
4100 }
4101 }
4102 if (check->power_on) {
4103 check->power_on = 0;
4104 snd_hda_power_down(codec);
4105 }
4106 return 0;
4107}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004108EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004111/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004112 * Channel mode helper
4113 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004114
4115/**
4116 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4117 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004118int snd_hda_ch_mode_info(struct hda_codec *codec,
4119 struct snd_ctl_elem_info *uinfo,
4120 const struct hda_channel_mode *chmode,
4121 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004122{
4123 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4124 uinfo->count = 1;
4125 uinfo->value.enumerated.items = num_chmodes;
4126 if (uinfo->value.enumerated.item >= num_chmodes)
4127 uinfo->value.enumerated.item = num_chmodes - 1;
4128 sprintf(uinfo->value.enumerated.name, "%dch",
4129 chmode[uinfo->value.enumerated.item].channels);
4130 return 0;
4131}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004132EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004133
Takashi Iwaid5191e52009-11-16 14:58:17 +01004134/**
4135 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4136 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004137int snd_hda_ch_mode_get(struct hda_codec *codec,
4138 struct snd_ctl_elem_value *ucontrol,
4139 const struct hda_channel_mode *chmode,
4140 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004141 int max_channels)
4142{
4143 int i;
4144
4145 for (i = 0; i < num_chmodes; i++) {
4146 if (max_channels == chmode[i].channels) {
4147 ucontrol->value.enumerated.item[0] = i;
4148 break;
4149 }
4150 }
4151 return 0;
4152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004153EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004154
Takashi Iwaid5191e52009-11-16 14:58:17 +01004155/**
4156 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4157 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004158int snd_hda_ch_mode_put(struct hda_codec *codec,
4159 struct snd_ctl_elem_value *ucontrol,
4160 const struct hda_channel_mode *chmode,
4161 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004162 int *max_channelsp)
4163{
4164 unsigned int mode;
4165
4166 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004167 if (mode >= num_chmodes)
4168 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004169 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004170 return 0;
4171 /* change the current channel setting */
4172 *max_channelsp = chmode[mode].channels;
4173 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004174 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004175 return 1;
4176}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004177EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179/*
4180 * input MUX helper
4181 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004182
4183/**
4184 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4185 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004186int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4187 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188{
4189 unsigned int index;
4190
4191 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4192 uinfo->count = 1;
4193 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004194 if (!imux->num_items)
4195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 index = uinfo->value.enumerated.item;
4197 if (index >= imux->num_items)
4198 index = imux->num_items - 1;
4199 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4200 return 0;
4201}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004202EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Takashi Iwaid5191e52009-11-16 14:58:17 +01004204/**
4205 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4206 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004207int snd_hda_input_mux_put(struct hda_codec *codec,
4208 const struct hda_input_mux *imux,
4209 struct snd_ctl_elem_value *ucontrol,
4210 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 unsigned int *cur_val)
4212{
4213 unsigned int idx;
4214
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004215 if (!imux->num_items)
4216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 idx = ucontrol->value.enumerated.item[0];
4218 if (idx >= imux->num_items)
4219 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004220 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004222 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4223 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 *cur_val = idx;
4225 return 1;
4226}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004227EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229
4230/*
4231 * Multi-channel / digital-out PCM helper functions
4232 */
4233
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004234/* setup SPDIF output stream */
4235static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4236 unsigned int stream_tag, unsigned int format)
4237{
Stephen Warren7c9359762011-06-01 11:14:17 -06004238 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4239
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004240 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004241 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004242 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004243 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004244 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004245 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004246 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004247 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004248 for (d = codec->slave_dig_outs; *d; d++)
4249 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4250 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004251 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004252 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004253 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004254 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004255 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004256}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004257
Takashi Iwai2f728532008-09-25 16:32:41 +02004258static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4259{
4260 snd_hda_codec_cleanup_stream(codec, nid);
4261 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004262 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004263 for (d = codec->slave_dig_outs; *d; d++)
4264 snd_hda_codec_cleanup_stream(codec, *d);
4265 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004266}
4267
Takashi Iwaid5191e52009-11-16 14:58:17 +01004268/**
4269 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4270 * @bus: HD-audio bus
4271 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004272void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4273{
4274 struct hda_codec *codec;
4275
4276 if (!bus)
4277 return;
4278 list_for_each_entry(codec, &bus->codec_list, list) {
4279#ifdef CONFIG_SND_HDA_POWER_SAVE
4280 if (!codec->power_on)
4281 continue;
4282#endif
4283 if (codec->patch_ops.reboot_notify)
4284 codec->patch_ops.reboot_notify(codec);
4285 }
4286}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004287EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004288
Takashi Iwaid5191e52009-11-16 14:58:17 +01004289/**
4290 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004292int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4293 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Ingo Molnar62932df2006-01-16 16:34:20 +01004295 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004296 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4297 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004298 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004300 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return 0;
4302}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004303EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Takashi Iwaid5191e52009-11-16 14:58:17 +01004305/**
4306 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4307 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004308int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4309 struct hda_multi_out *mout,
4310 unsigned int stream_tag,
4311 unsigned int format,
4312 struct snd_pcm_substream *substream)
4313{
4314 mutex_lock(&codec->spdif_mutex);
4315 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4316 mutex_unlock(&codec->spdif_mutex);
4317 return 0;
4318}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004319EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004320
Takashi Iwaid5191e52009-11-16 14:58:17 +01004321/**
4322 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4323 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004324int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4325 struct hda_multi_out *mout)
4326{
4327 mutex_lock(&codec->spdif_mutex);
4328 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4329 mutex_unlock(&codec->spdif_mutex);
4330 return 0;
4331}
4332EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4333
Takashi Iwaid5191e52009-11-16 14:58:17 +01004334/**
4335 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004337int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4338 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339{
Ingo Molnar62932df2006-01-16 16:34:20 +01004340 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004342 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return 0;
4344}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004345EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Takashi Iwaid5191e52009-11-16 14:58:17 +01004347/**
4348 * snd_hda_multi_out_analog_open - open analog outputs
4349 *
4350 * Open analog outputs and set up the hw-constraints.
4351 * If the digital outputs can be opened as slave, open the digital
4352 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004354int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4355 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004356 struct snd_pcm_substream *substream,
4357 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358{
Takashi Iwai9a081602008-02-12 18:37:26 +01004359 struct snd_pcm_runtime *runtime = substream->runtime;
4360 runtime->hw.channels_max = mout->max_channels;
4361 if (mout->dig_out_nid) {
4362 if (!mout->analog_rates) {
4363 mout->analog_rates = hinfo->rates;
4364 mout->analog_formats = hinfo->formats;
4365 mout->analog_maxbps = hinfo->maxbps;
4366 } else {
4367 runtime->hw.rates = mout->analog_rates;
4368 runtime->hw.formats = mout->analog_formats;
4369 hinfo->maxbps = mout->analog_maxbps;
4370 }
4371 if (!mout->spdif_rates) {
4372 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4373 &mout->spdif_rates,
4374 &mout->spdif_formats,
4375 &mout->spdif_maxbps);
4376 }
4377 mutex_lock(&codec->spdif_mutex);
4378 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004379 if ((runtime->hw.rates & mout->spdif_rates) &&
4380 (runtime->hw.formats & mout->spdif_formats)) {
4381 runtime->hw.rates &= mout->spdif_rates;
4382 runtime->hw.formats &= mout->spdif_formats;
4383 if (mout->spdif_maxbps < hinfo->maxbps)
4384 hinfo->maxbps = mout->spdif_maxbps;
4385 } else {
4386 mout->share_spdif = 0;
4387 /* FIXME: need notify? */
4388 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004389 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004390 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4393 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4394}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004395EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Takashi Iwaid5191e52009-11-16 14:58:17 +01004397/**
4398 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4399 *
4400 * Set up the i/o for analog out.
4401 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004403int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4404 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 unsigned int stream_tag,
4406 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004407 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Takashi Iwaidda14412011-05-02 11:29:30 +02004409 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004411 struct hda_spdif_out *spdif =
4412 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 int i;
4414
Ingo Molnar62932df2006-01-16 16:34:20 +01004415 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004416 if (mout->dig_out_nid && mout->share_spdif &&
4417 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004419 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4420 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004421 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004423 setup_dig_out_stream(codec, mout->dig_out_nid,
4424 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 } else {
4426 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004427 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 }
4429 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004430 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004433 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4434 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004435 if (!mout->no_share_stream &&
4436 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004438 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4439 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004440 /* extra outputs copied from front */
4441 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004442 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004443 snd_hda_codec_setup_stream(codec,
4444 mout->extra_out_nid[i],
4445 stream_tag, 0, format);
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 /* surrounds */
4448 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004449 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004450 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4451 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004452 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004453 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4454 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 }
4456 return 0;
4457}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004458EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Takashi Iwaid5191e52009-11-16 14:58:17 +01004460/**
4461 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004463int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4464 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
Takashi Iwaidda14412011-05-02 11:29:30 +02004466 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 int i;
4468
4469 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004470 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004472 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004473 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4474 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004475 snd_hda_codec_cleanup_stream(codec,
4476 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004477 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004479 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 mout->dig_out_used = 0;
4481 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004482 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 return 0;
4484}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004485EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004487/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004488 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004489 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004490
Takashi Iwaidda14412011-05-02 11:29:30 +02004491static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004492{
4493 for (; *list; list++)
4494 if (*list == nid)
4495 return 1;
4496 return 0;
4497}
4498
Steve Longerbeam81937d32007-05-08 15:33:03 +02004499
4500/*
4501 * Sort an associated group of pins according to their sequence numbers.
4502 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004503static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004504 int num_pins)
4505{
4506 int i, j;
4507 short seq;
4508 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004509
Steve Longerbeam81937d32007-05-08 15:33:03 +02004510 for (i = 0; i < num_pins; i++) {
4511 for (j = i + 1; j < num_pins; j++) {
4512 if (sequences[i] > sequences[j]) {
4513 seq = sequences[i];
4514 sequences[i] = sequences[j];
4515 sequences[j] = seq;
4516 nid = pins[i];
4517 pins[i] = pins[j];
4518 pins[j] = nid;
4519 }
4520 }
4521 }
4522}
4523
4524
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004525/* add the found input-pin to the cfg->inputs[] table */
4526static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4527 int type)
4528{
4529 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4530 cfg->inputs[cfg->num_inputs].pin = nid;
4531 cfg->inputs[cfg->num_inputs].type = type;
4532 cfg->num_inputs++;
4533 }
4534}
4535
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004536/* sort inputs in the order of AUTO_PIN_* type */
4537static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4538{
4539 int i, j;
4540
4541 for (i = 0; i < cfg->num_inputs; i++) {
4542 for (j = i + 1; j < cfg->num_inputs; j++) {
4543 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4544 struct auto_pin_cfg_item tmp;
4545 tmp = cfg->inputs[i];
4546 cfg->inputs[i] = cfg->inputs[j];
4547 cfg->inputs[j] = tmp;
4548 }
4549 }
4550 }
4551}
4552
Takashi Iwai82bc9552006-03-21 11:24:42 +01004553/*
4554 * Parse all pin widgets and store the useful pin nids to cfg
4555 *
4556 * The number of line-outs or any primary output is stored in line_outs,
4557 * and the corresponding output pins are assigned to line_out_pins[],
4558 * in the order of front, rear, CLFE, side, ...
4559 *
4560 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004561 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004562 * is detected, one of speaker of HP pins is assigned as the primary
4563 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4564 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004565 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004566 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004567 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4568 * respectively.
4569 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004570int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4571 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004572 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004573{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004574 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004575 short seq, assoc_line_out, assoc_speaker;
4576 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4577 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004578 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004579 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004580
4581 memset(cfg, 0, sizeof(*cfg));
4582
Steve Longerbeam81937d32007-05-08 15:33:03 +02004583 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4584 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004585 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004586 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004587
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004588 end_nid = codec->start_nid + codec->num_nodes;
4589 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004590 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004591 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004592 unsigned int def_conf;
4593 short assoc, loc;
4594
4595 /* read all default configuration for pin complex */
4596 if (wid_type != AC_WID_PIN)
4597 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004598 /* ignore the given nids (e.g. pc-beep returns error) */
4599 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4600 continue;
4601
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004602 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004603 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4604 continue;
4605 loc = get_defcfg_location(def_conf);
4606 switch (get_defcfg_device(def_conf)) {
4607 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004608 seq = get_defcfg_sequence(def_conf);
4609 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004610
4611 if (!(wid_caps & AC_WCAP_STEREO))
4612 if (!cfg->mono_out_pin)
4613 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004614 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004615 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004616 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004617 assoc_line_out = assoc;
4618 else if (assoc_line_out != assoc)
4619 continue;
4620 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4621 continue;
4622 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004623 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004624 cfg->line_outs++;
4625 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004626 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004627 seq = get_defcfg_sequence(def_conf);
4628 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004629 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004630 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004631 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004632 assoc_speaker = assoc;
4633 else if (assoc_speaker != assoc)
4634 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004635 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4636 continue;
4637 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004638 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004639 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004640 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004641 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004642 seq = get_defcfg_sequence(def_conf);
4643 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004644 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4645 continue;
4646 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004647 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004648 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004649 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004650 case AC_JACK_MIC_IN:
4651 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004652 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004653 case AC_JACK_LINE_IN:
4654 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004655 break;
4656 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004657 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004658 break;
4659 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004660 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004661 break;
4662 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004663 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004664 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4665 continue;
4666 cfg->dig_out_pins[cfg->dig_outs] = nid;
4667 cfg->dig_out_type[cfg->dig_outs] =
4668 (loc == AC_JACK_LOC_HDMI) ?
4669 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4670 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004671 break;
4672 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004673 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004674 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004675 if (loc == AC_JACK_LOC_HDMI)
4676 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4677 else
4678 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004679 break;
4680 }
4681 }
4682
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004683 /* FIX-UP:
4684 * If no line-out is defined but multiple HPs are found,
4685 * some of them might be the real line-outs.
4686 */
4687 if (!cfg->line_outs && cfg->hp_outs > 1) {
4688 int i = 0;
4689 while (i < cfg->hp_outs) {
4690 /* The real HPs should have the sequence 0x0f */
4691 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4692 i++;
4693 continue;
4694 }
4695 /* Move it to the line-out table */
4696 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4697 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4698 cfg->line_outs++;
4699 cfg->hp_outs--;
4700 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4701 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004702 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004703 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4704 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004705 memset(cfg->hp_pins + cfg->hp_outs, 0,
4706 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004707 if (!cfg->hp_outs)
4708 cfg->line_out_type = AUTO_PIN_HP_OUT;
4709
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004710 }
4711
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004712 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004713 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4714 cfg->line_outs);
4715 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4716 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004717 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4718 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004719
Steve Longerbeam81937d32007-05-08 15:33:03 +02004720 /*
4721 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4722 * as a primary output
4723 */
4724 if (!cfg->line_outs) {
4725 if (cfg->speaker_outs) {
4726 cfg->line_outs = cfg->speaker_outs;
4727 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4728 sizeof(cfg->speaker_pins));
4729 cfg->speaker_outs = 0;
4730 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4731 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4732 } else if (cfg->hp_outs) {
4733 cfg->line_outs = cfg->hp_outs;
4734 memcpy(cfg->line_out_pins, cfg->hp_pins,
4735 sizeof(cfg->hp_pins));
4736 cfg->hp_outs = 0;
4737 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4738 cfg->line_out_type = AUTO_PIN_HP_OUT;
4739 }
4740 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004741
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004742 /* Reorder the surround channels
4743 * ALSA sequence is front/surr/clfe/side
4744 * HDA sequence is:
4745 * 4-ch: front/surr => OK as it is
4746 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004747 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004748 */
4749 switch (cfg->line_outs) {
4750 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004751 case 4:
4752 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004753 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004754 cfg->line_out_pins[2] = nid;
4755 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004756 }
4757
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004758 sort_autocfg_input_pins(cfg);
4759
Takashi Iwai82bc9552006-03-21 11:24:42 +01004760 /*
4761 * debug prints of the parsed results
4762 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004763 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
Takashi Iwai82bc9552006-03-21 11:24:42 +01004764 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4765 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004766 cfg->line_out_pins[4],
4767 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4768 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4769 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004770 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4771 cfg->speaker_outs, cfg->speaker_pins[0],
4772 cfg->speaker_pins[1], cfg->speaker_pins[2],
4773 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004774 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4775 cfg->hp_outs, cfg->hp_pins[0],
4776 cfg->hp_pins[1], cfg->hp_pins[2],
4777 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004778 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004779 if (cfg->dig_outs)
4780 snd_printd(" dig-out=0x%x/0x%x\n",
4781 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004782 snd_printd(" inputs:");
4783 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004784 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004785 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004786 cfg->inputs[i].pin);
4787 }
4788 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004789 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004790 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004791
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004792 return 0;
4793}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004794EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004795
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004796int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004797{
4798 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004799 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004800 if (conn == AC_JACK_PORT_NONE)
4801 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004802 /* Windows may claim the internal mic to be BOTH, too */
4803 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004804 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004805 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004806 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004807 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004808 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004809 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004810 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004811 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004812 return INPUT_PIN_ATTR_FRONT;
4813 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004814}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004815EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004816
Takashi Iwai990061c2010-09-09 22:08:44 +02004817/**
4818 * hda_get_input_pin_label - Give a label for the given input pin
4819 *
4820 * When check_location is true, the function checks the pin location
4821 * for mic and line-in pins, and set an appropriate prefix like "Front",
4822 * "Rear", "Internal".
4823 */
4824
Takashi Iwai10a20af2010-09-09 16:28:02 +02004825const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4826 int check_location)
4827{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004828 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004829 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004830 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4831 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004832 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004833
Takashi Iwai10a20af2010-09-09 16:28:02 +02004834 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004835
4836 switch (get_defcfg_device(def_conf)) {
4837 case AC_JACK_MIC_IN:
4838 if (!check_location)
4839 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004840 attr = snd_hda_get_input_pin_attr(def_conf);
4841 if (!attr)
4842 return "None";
4843 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004844 case AC_JACK_LINE_IN:
4845 if (!check_location)
4846 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004847 attr = snd_hda_get_input_pin_attr(def_conf);
4848 if (!attr)
4849 return "None";
4850 if (attr == INPUT_PIN_ATTR_DOCK)
4851 return "Dock Line";
4852 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004853 case AC_JACK_AUX:
4854 return "Aux";
4855 case AC_JACK_CD:
4856 return "CD";
4857 case AC_JACK_SPDIF_IN:
4858 return "SPDIF In";
4859 case AC_JACK_DIG_OTHER_IN:
4860 return "Digital In";
4861 default:
4862 return "Misc";
4863 }
4864}
4865EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4866
Takashi Iwaia1c98512010-09-09 21:36:27 +02004867/* Check whether the location prefix needs to be added to the label.
4868 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4869 * have to put "Front" prefix to each label. In such a case, returns false.
4870 */
4871static int check_mic_location_need(struct hda_codec *codec,
4872 const struct auto_pin_cfg *cfg,
4873 int input)
4874{
4875 unsigned int defc;
4876 int i, attr, attr2;
4877
4878 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004879 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004880 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004881 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004882 return 1;
4883
4884 attr = 0;
4885 for (i = 0; i < cfg->num_inputs; i++) {
4886 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004887 attr2 = snd_hda_get_input_pin_attr(defc);
4888 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004889 if (attr && attr != attr2)
4890 return 1; /* different locations found */
4891 attr = attr2;
4892 }
4893 }
4894 return 0;
4895}
4896
Takashi Iwai990061c2010-09-09 22:08:44 +02004897/**
4898 * hda_get_autocfg_input_label - Get a label for the given input
4899 *
4900 * Get a label for the given input pin defined by the autocfg item.
4901 * Unlike hda_get_input_pin_label(), this function checks all inputs
4902 * defined in autocfg and avoids the redundant mic/line prefix as much as
4903 * possible.
4904 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004905const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4906 const struct auto_pin_cfg *cfg,
4907 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004908{
4909 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004910 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004911
Takashi Iwai10a20af2010-09-09 16:28:02 +02004912 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4913 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4914 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004915 if (has_multiple_pins && type == AUTO_PIN_MIC)
4916 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004917 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4918 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004919}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004920EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4921
Takashi Iwai990061c2010-09-09 22:08:44 +02004922/**
4923 * snd_hda_add_imux_item - Add an item to input_mux
4924 *
4925 * When the same label is used already in the existing items, the number
4926 * suffix is appended to the label. This label index number is stored
4927 * to type_idx when non-NULL pointer is given.
4928 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004929int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4930 int index, int *type_idx)
4931{
4932 int i, label_idx = 0;
4933 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4934 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4935 return -EINVAL;
4936 }
4937 for (i = 0; i < imux->num_items; i++) {
4938 if (!strncmp(label, imux->items[i].label, strlen(label)))
4939 label_idx++;
4940 }
4941 if (type_idx)
4942 *type_idx = label_idx;
4943 if (label_idx > 0)
4944 snprintf(imux->items[imux->num_items].label,
4945 sizeof(imux->items[imux->num_items].label),
4946 "%s %d", label, label_idx);
4947 else
4948 strlcpy(imux->items[imux->num_items].label, label,
4949 sizeof(imux->items[imux->num_items].label));
4950 imux->items[imux->num_items].index = index;
4951 imux->num_items++;
4952 return 0;
4953}
4954EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004955
Takashi Iwai4a471b72005-12-07 13:56:29 +01004956
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957#ifdef CONFIG_PM
4958/*
4959 * power management
4960 */
4961
4962/**
4963 * snd_hda_suspend - suspend the codecs
4964 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 *
4966 * Returns 0 if successful.
4967 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004968int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004970 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
Takashi Iwai0ba21762007-04-16 11:29:14 +02004972 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004973#ifdef CONFIG_SND_HDA_POWER_SAVE
4974 if (!codec->power_on)
4975 continue;
4976#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004977 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 }
4979 return 0;
4980}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004981EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983/**
4984 * snd_hda_resume - resume the codecs
4985 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 *
4987 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004988 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004989 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004990 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 */
4992int snd_hda_resume(struct hda_bus *bus)
4993{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004994 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Takashi Iwai0ba21762007-04-16 11:29:14 +02004996 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004997 if (snd_hda_codec_needs_resume(codec))
4998 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 return 0;
5001}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005002EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005003#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005004
5005/*
5006 * generic arrays
5007 */
5008
Takashi Iwaid5191e52009-11-16 14:58:17 +01005009/**
5010 * snd_array_new - get a new element from the given array
5011 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005012 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005013 * Get a new element from the given array. If it exceeds the
5014 * pre-allocated array size, re-allocate the array.
5015 *
5016 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005017 */
5018void *snd_array_new(struct snd_array *array)
5019{
5020 if (array->used >= array->alloced) {
5021 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005022 void *nlist;
5023 if (snd_BUG_ON(num >= 4096))
5024 return NULL;
5025 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005026 if (!nlist)
5027 return NULL;
5028 if (array->list) {
5029 memcpy(nlist, array->list,
5030 array->elem_size * array->alloced);
5031 kfree(array->list);
5032 }
5033 array->list = nlist;
5034 array->alloced = num;
5035 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005036 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005037}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005038EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005039
Takashi Iwaid5191e52009-11-16 14:58:17 +01005040/**
5041 * snd_array_free - free the given array elements
5042 * @array: the array object
5043 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005044void snd_array_free(struct snd_array *array)
5045{
5046 kfree(array->list);
5047 array->used = 0;
5048 array->alloced = 0;
5049 array->list = NULL;
5050}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005051EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005052
Takashi Iwaid5191e52009-11-16 14:58:17 +01005053/**
5054 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5055 * @pcm: PCM caps bits
5056 * @buf: the string buffer to write
5057 * @buflen: the max buffer length
5058 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005059 * used by hda_proc.c and hda_eld.c
5060 */
5061void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5062{
5063 static unsigned int rates[] = {
5064 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5065 96000, 176400, 192000, 384000
5066 };
5067 int i, j;
5068
5069 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5070 if (pcm & (1 << i))
5071 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5072
5073 buf[j] = '\0'; /* necessary when j == 0 */
5074}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005075EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005076
Takashi Iwaid5191e52009-11-16 14:58:17 +01005077/**
5078 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5079 * @pcm: PCM caps bits
5080 * @buf: the string buffer to write
5081 * @buflen: the max buffer length
5082 *
5083 * used by hda_proc.c and hda_eld.c
5084 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005085void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5086{
5087 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5088 int i, j;
5089
5090 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5091 if (pcm & (AC_SUPPCM_BITS_8 << i))
5092 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5093
5094 buf[j] = '\0'; /* necessary when j == 0 */
5095}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005096EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005097
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005098#ifdef CONFIG_SND_HDA_INPUT_JACK
5099/*
5100 * Input-jack notification support
5101 */
5102struct hda_jack_item {
5103 hda_nid_t nid;
5104 int type;
5105 struct snd_jack *jack;
5106};
5107
5108static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5109 int type)
5110{
5111 switch (type) {
5112 case SND_JACK_HEADPHONE:
5113 return "Headphone";
5114 case SND_JACK_MICROPHONE:
5115 return "Mic";
5116 case SND_JACK_LINEOUT:
5117 return "Line-out";
5118 case SND_JACK_HEADSET:
5119 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005120 case SND_JACK_VIDEOOUT:
5121 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005122 default:
5123 return "Misc";
5124 }
5125}
5126
5127static void hda_free_jack_priv(struct snd_jack *jack)
5128{
5129 struct hda_jack_item *jacks = jack->private_data;
5130 jacks->nid = 0;
5131 jacks->jack = NULL;
5132}
5133
5134int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5135 const char *name)
5136{
5137 struct hda_jack_item *jack;
5138 int err;
5139
5140 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5141 jack = snd_array_new(&codec->jacks);
5142 if (!jack)
5143 return -ENOMEM;
5144
5145 jack->nid = nid;
5146 jack->type = type;
5147 if (!name)
5148 name = get_jack_default_name(codec, nid, type);
5149 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5150 if (err < 0) {
5151 jack->nid = 0;
5152 return err;
5153 }
5154 jack->jack->private_data = jack;
5155 jack->jack->private_free = hda_free_jack_priv;
5156 return 0;
5157}
5158EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5159
5160void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5161{
5162 struct hda_jack_item *jacks = codec->jacks.list;
5163 int i;
5164
5165 if (!jacks)
5166 return;
5167
5168 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5169 unsigned int pin_ctl;
5170 unsigned int present;
5171 int type;
5172
5173 if (jacks->nid != nid)
5174 continue;
5175 present = snd_hda_jack_detect(codec, nid);
5176 type = jacks->type;
5177 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5178 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5179 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5180 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5181 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5182 }
5183 snd_jack_report(jacks->jack, present ? type : 0);
5184 }
5185}
5186EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5187
5188/* free jack instances manually when clearing/reconfiguring */
5189void snd_hda_input_jack_free(struct hda_codec *codec)
5190{
5191 if (!codec->bus->shutdown && codec->jacks.list) {
5192 struct hda_jack_item *jacks = codec->jacks.list;
5193 int i;
5194 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5195 if (jacks->jack)
5196 snd_device_free(codec->bus->card, jacks->jack);
5197 }
5198 }
5199 snd_array_free(&codec->jacks);
5200}
5201EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5202#endif /* CONFIG_SND_HDA_INPUT_JACK */
5203
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005204MODULE_DESCRIPTION("HDA codec core");
5205MODULE_LICENSE("GPL");