blob: 26c420de91c3c3eeb88777f847ae962ba984df40 [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);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/**
316 * snd_hda_get_connections - get connection list
317 * @codec: the HDA codec
318 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200319 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
321 * Parses the connection list of the given widget and stores the list
322 * of NIDs.
323 *
324 * Returns the number of connections, or a negative error code.
325 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200326int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
327 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200328{
329 struct snd_array *array = &codec->conn_lists;
Andrew Morton5b17b072011-04-14 15:06:13 -0700330 int i, len, old_used;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200331 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200332 hda_nid_t *p;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200333
334 /* look up the cached results */
335 for (i = 0; i < array->used; ) {
Takashi Iwaidce20792011-06-24 14:10:28 +0200336 p = snd_array_elem(array, i);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200337 len = p[1];
Takashi Iwaidce20792011-06-24 14:10:28 +0200338 if (nid == *p) {
339 if (listp)
340 *listp = p + 2;
341 return len;
342 }
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200343 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
Takashi Iwaidce20792011-06-24 14:10:28 +0200358 p = snd_array_elem(array, old_used);
359 if (listp)
360 *listp = p + 2;
361 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200362
363 error_add:
364 array->used = old_used;
365 return -ENOMEM;
366}
Takashi Iwaidce20792011-06-24 14:10:28 +0200367EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
368
369/**
370 * snd_hda_get_connections - copy connection list
371 * @codec: the HDA codec
372 * @nid: NID to parse
373 * @conn_list: connection list array
374 * @max_conns: max. number of connections to store
375 *
376 * Parses the connection list of the given widget and stores the list
377 * of NIDs.
378 *
379 * Returns the number of connections, or a negative error code.
380 */
381int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
382 hda_nid_t *conn_list, int max_conns)
383{
384 const hda_nid_t *list;
385 int len = snd_hda_get_conn_list(codec, nid, &list);
386
387 if (len <= 0)
388 return len;
389 if (len > max_conns) {
390 snd_printk(KERN_ERR "hda_codec: "
391 "Too many connections %d for NID 0x%x\n",
392 len, nid);
393 return -EINVAL;
394 }
395 memcpy(conn_list, list, len * sizeof(hda_nid_t));
396 return len;
397}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200398EXPORT_SYMBOL_HDA(snd_hda_get_connections);
399
400static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
401 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100404 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200406 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100407 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Takashi Iwaida3cec32008-08-08 17:12:14 +0200409 if (snd_BUG_ON(!conn_list || max_conns <= 0))
410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200412 wcaps = get_wcaps(codec, nid);
413 if (!(wcaps & AC_WCAP_CONN_LIST) &&
414 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200415 snd_printk(KERN_WARNING "hda_codec: "
416 "connection list not available for 0x%x\n", nid);
417 return -EINVAL;
418 }
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
421 if (parm & AC_CLIST_LONG) {
422 /* long form */
423 shift = 16;
424 num_elems = 2;
425 } else {
426 /* short form */
427 shift = 8;
428 num_elems = 4;
429 }
430 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 mask = (1 << (shift-1)) - 1;
432
Takashi Iwai0ba21762007-04-16 11:29:14 +0200433 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0; /* no connection */
435
436 if (conn_len == 1) {
437 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200438 parm = snd_hda_codec_read(codec, nid, 0,
439 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200440 if (parm == -1 && codec->bus->rirb_error)
441 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 conn_list[0] = parm & mask;
443 return 1;
444 }
445
446 /* multi connection */
447 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100448 prev_nid = 0;
449 for (i = 0; i < conn_len; i++) {
450 int range_val;
451 hda_nid_t val, n;
452
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200453 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100454 parm = snd_hda_codec_read(codec, nid, 0,
455 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200456 if (parm == -1 && codec->bus->rirb_error)
457 return -EIO;
458 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200459 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100460 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200461 if (val == 0) {
462 snd_printk(KERN_WARNING "hda_codec: "
463 "invalid CONNECT_LIST verb %x[%i]:%x\n",
464 nid, i, parm);
465 return 0;
466 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100467 parm >>= shift;
468 if (range_val) {
469 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200470 if (!prev_nid || prev_nid >= val) {
471 snd_printk(KERN_WARNING "hda_codec: "
472 "invalid dep_range_val %x:%x\n",
473 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100474 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100476 for (n = prev_nid + 1; n <= val; n++) {
477 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200478 snd_printk(KERN_ERR "hda_codec: "
479 "Too many connections %d for NID 0x%x\n",
480 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100481 return -EINVAL;
482 }
483 conn_list[conns++] = n;
484 }
485 } else {
486 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200487 snd_printk(KERN_ERR "hda_codec: "
488 "Too many connections %d for NID 0x%x\n",
489 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 return -EINVAL;
491 }
492 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100494 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 return conns;
497}
498
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200499static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
500{
501 hda_nid_t *p = snd_array_new(array);
502 if (!p)
503 return false;
504 *p = nid;
505 return true;
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/**
509 * snd_hda_queue_unsol_event - add an unsolicited event to queue
510 * @bus: the BUS
511 * @res: unsolicited event (lower 32bit of RIRB entry)
512 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
513 *
514 * Adds the given event to the queue. The events are processed in
515 * the workqueue asynchronously. Call this function in the interrupt
516 * hanlder when RIRB receives an unsolicited event.
517 *
518 * Returns 0 if successful, or a negative error code.
519 */
520int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
521{
522 struct hda_bus_unsolicited *unsol;
523 unsigned int wp;
524
Takashi Iwai0ba21762007-04-16 11:29:14 +0200525 unsol = bus->unsol;
526 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 0;
528
529 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
530 unsol->wp = wp;
531
532 wp <<= 1;
533 unsol->queue[wp] = res;
534 unsol->queue[wp + 1] = res_ex;
535
Takashi Iwai6acaed32009-01-12 10:09:24 +0100536 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 return 0;
539}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100540EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800543 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
David Howellsc4028952006-11-22 14:57:56 +0000545static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
David Howellsc4028952006-11-22 14:57:56 +0000547 struct hda_bus_unsolicited *unsol =
548 container_of(work, struct hda_bus_unsolicited, work);
549 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct hda_codec *codec;
551 unsigned int rp, caddr, res;
552
553 while (unsol->rp != unsol->wp) {
554 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
555 unsol->rp = rp;
556 rp <<= 1;
557 res = unsol->queue[rp];
558 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200559 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 continue;
561 codec = bus->caddr_tbl[caddr & 0x0f];
562 if (codec && codec->patch_ops.unsol_event)
563 codec->patch_ops.unsol_event(codec, res);
564 }
565}
566
567/*
568 * initialize unsolicited queue
569 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200570static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct hda_bus_unsolicited *unsol;
573
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100574 if (bus->unsol) /* already initialized */
575 return 0;
576
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200577 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200578 if (!unsol) {
579 snd_printk(KERN_ERR "hda_codec: "
580 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return -ENOMEM;
582 }
David Howellsc4028952006-11-22 14:57:56 +0000583 INIT_WORK(&unsol->work, process_unsol_events);
584 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 bus->unsol = unsol;
586 return 0;
587}
588
589/*
590 * destructor
591 */
592static void snd_hda_codec_free(struct hda_codec *codec);
593
594static int snd_hda_bus_free(struct hda_bus *bus)
595{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200596 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Takashi Iwai0ba21762007-04-16 11:29:14 +0200598 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100600 if (bus->workq)
601 flush_workqueue(bus->workq);
602 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200604 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 snd_hda_codec_free(codec);
606 }
607 if (bus->ops.private_free)
608 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100609 if (bus->workq)
610 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 kfree(bus);
612 return 0;
613}
614
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100615static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100618 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return snd_hda_bus_free(bus);
620}
621
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200622#ifdef CONFIG_SND_HDA_HWDEP
623static int snd_hda_bus_dev_register(struct snd_device *device)
624{
625 struct hda_bus *bus = device->device_data;
626 struct hda_codec *codec;
627 list_for_each_entry(codec, &bus->codec_list, list) {
628 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100629 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200630 }
631 return 0;
632}
633#else
634#define snd_hda_bus_dev_register NULL
635#endif
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637/**
638 * snd_hda_bus_new - create a HDA bus
639 * @card: the card entry
640 * @temp: the template for hda_bus information
641 * @busp: the pointer to store the created bus instance
642 *
643 * Returns 0 if successful, or a negative error code.
644 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100645int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200646 const struct hda_bus_template *temp,
647 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct hda_bus *bus;
650 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100651 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200652 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 .dev_free = snd_hda_bus_dev_free,
654 };
655
Takashi Iwaida3cec32008-08-08 17:12:14 +0200656 if (snd_BUG_ON(!temp))
657 return -EINVAL;
658 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
659 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (busp)
662 *busp = NULL;
663
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200664 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (bus == NULL) {
666 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
667 return -ENOMEM;
668 }
669
670 bus->card = card;
671 bus->private_data = temp->private_data;
672 bus->pci = temp->pci;
673 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100674 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 bus->ops = temp->ops;
676
Ingo Molnar62932df2006-01-16 16:34:20 +0100677 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200678 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 INIT_LIST_HEAD(&bus->codec_list);
680
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100681 snprintf(bus->workq_name, sizeof(bus->workq_name),
682 "hd-audio%d", card->number);
683 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100684 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100685 snd_printk(KERN_ERR "cannot create workqueue %s\n",
686 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100687 kfree(bus);
688 return -ENOMEM;
689 }
690
Takashi Iwai0ba21762007-04-16 11:29:14 +0200691 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
692 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 snd_hda_bus_free(bus);
694 return err;
695 }
696 if (busp)
697 *busp = bus;
698 return 0;
699}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100700EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Takashi Iwai82467612007-07-27 19:15:54 +0200702#ifdef CONFIG_SND_HDA_GENERIC
703#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200704 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200705#else
706#define is_generic_config(codec) 0
707#endif
708
Takashi Iwai645f10c2008-11-28 15:07:37 +0100709#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100710#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
711#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100712#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100713#endif
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
716 * find a matching codec preset
717 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200718static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200719find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100721 struct hda_codec_preset_list *tbl;
722 const struct hda_codec_preset *preset;
723 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Takashi Iwai82467612007-07-27 19:15:54 +0200725 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100726 return NULL; /* use the generic parser */
727
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100728 again:
729 mutex_lock(&preset_mutex);
730 list_for_each_entry(tbl, &hda_preset_tables, list) {
731 if (!try_module_get(tbl->owner)) {
732 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
733 continue;
734 }
735 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100737 if (preset->afg && preset->afg != codec->afg)
738 continue;
739 if (preset->mfg && preset->mfg != codec->mfg)
740 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200741 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200743 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200744 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100745 preset->rev == codec->revision_id)) {
746 mutex_unlock(&preset_mutex);
747 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100751 module_put(tbl->owner);
752 }
753 mutex_unlock(&preset_mutex);
754
755 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
756 char name[32];
757 if (!mod_requested)
758 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
759 codec->vendor_id);
760 else
761 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
762 (codec->vendor_id >> 16) & 0xffff);
763 request_module(name);
764 mod_requested++;
765 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 return NULL;
768}
769
770/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200771 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200773static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 const struct hda_vendor_id *c;
776 const char *vendor = NULL;
777 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200778 char tmp[16];
779
780 if (codec->vendor_name)
781 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 for (c = hda_vendor_ids; c->id; c++) {
784 if (c->id == vendor_id) {
785 vendor = c->name;
786 break;
787 }
788 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200789 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 sprintf(tmp, "Generic %04x", vendor_id);
791 vendor = tmp;
792 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200793 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
794 if (!codec->vendor_name)
795 return -ENOMEM;
796
797 get_chip_name:
798 if (codec->chip_name)
799 return 0;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200802 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
803 else {
804 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
805 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
806 }
807 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200808 return -ENOMEM;
809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200813 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100815static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200817 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 hda_nid_t nid;
819
820 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
821 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200822 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200823 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200824 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200825 case AC_GRP_AUDIO_FUNCTION:
826 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200827 codec->afg_function_id = function_id & 0xff;
828 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200829 break;
830 case AC_GRP_MODEM_FUNCTION:
831 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200832 codec->mfg_function_id = function_id & 0xff;
833 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200834 break;
835 default:
836 break;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100842 * read widget caps for each widget and store in cache
843 */
844static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
845{
846 int i;
847 hda_nid_t nid;
848
849 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
850 &codec->start_nid);
851 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200852 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100853 return -ENOMEM;
854 nid = codec->start_nid;
855 for (i = 0; i < codec->num_nodes; i++, nid++)
856 codec->wcaps[i] = snd_hda_param_read(codec, nid,
857 AC_PAR_AUDIO_WIDGET_CAP);
858 return 0;
859}
860
Takashi Iwai3be14142009-02-20 14:11:16 +0100861/* read all pin default configurations and save codec->init_pins */
862static int read_pin_defaults(struct hda_codec *codec)
863{
864 int i;
865 hda_nid_t nid = codec->start_nid;
866
867 for (i = 0; i < codec->num_nodes; i++, nid++) {
868 struct hda_pincfg *pin;
869 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200870 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100871 if (wid_type != AC_WID_PIN)
872 continue;
873 pin = snd_array_new(&codec->init_pins);
874 if (!pin)
875 return -ENOMEM;
876 pin->nid = nid;
877 pin->cfg = snd_hda_codec_read(codec, nid, 0,
878 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200879 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
880 AC_VERB_GET_PIN_WIDGET_CONTROL,
881 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100882 }
883 return 0;
884}
885
886/* look up the given pin config list and return the item matching with NID */
887static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
888 struct snd_array *array,
889 hda_nid_t nid)
890{
891 int i;
892 for (i = 0; i < array->used; i++) {
893 struct hda_pincfg *pin = snd_array_elem(array, i);
894 if (pin->nid == nid)
895 return pin;
896 }
897 return NULL;
898}
899
900/* write a config value for the given NID */
901static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
902 unsigned int cfg)
903{
904 int i;
905 for (i = 0; i < 4; i++) {
906 snd_hda_codec_write(codec, nid, 0,
907 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
908 cfg & 0xff);
909 cfg >>= 8;
910 }
911}
912
913/* set the current pin config value for the given NID.
914 * the value is cached, and read via snd_hda_codec_get_pincfg()
915 */
916int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
917 hda_nid_t nid, unsigned int cfg)
918{
919 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100920 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100921
Takashi Iwaib82855a2009-12-27 11:24:56 +0100922 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
923 return -EINVAL;
924
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100925 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100926 pin = look_up_pincfg(codec, list, nid);
927 if (!pin) {
928 pin = snd_array_new(list);
929 if (!pin)
930 return -ENOMEM;
931 pin->nid = nid;
932 }
933 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100934
935 /* change only when needed; e.g. if the pincfg is already present
936 * in user_pins[], don't write it
937 */
938 cfg = snd_hda_codec_get_pincfg(codec, nid);
939 if (oldcfg != cfg)
940 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100941 return 0;
942}
943
Takashi Iwaid5191e52009-11-16 14:58:17 +0100944/**
945 * snd_hda_codec_set_pincfg - Override a pin default configuration
946 * @codec: the HDA codec
947 * @nid: NID to set the pin config
948 * @cfg: the pin default config value
949 *
950 * Override a pin default configuration value in the cache.
951 * This value can be read by snd_hda_codec_get_pincfg() in a higher
952 * priority than the real hardware value.
953 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100954int snd_hda_codec_set_pincfg(struct hda_codec *codec,
955 hda_nid_t nid, unsigned int cfg)
956{
Takashi Iwai346ff702009-02-23 09:42:57 +0100957 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100958}
959EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
960
Takashi Iwaid5191e52009-11-16 14:58:17 +0100961/**
962 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
963 * @codec: the HDA codec
964 * @nid: NID to get the pin config
965 *
966 * Get the current pin config value of the given pin NID.
967 * If the pincfg value is cached or overridden via sysfs or driver,
968 * returns the cached value.
969 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100970unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
971{
972 struct hda_pincfg *pin;
973
Takashi Iwai3be14142009-02-20 14:11:16 +0100974#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100975 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100976 if (pin)
977 return pin->cfg;
978#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100979 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
980 if (pin)
981 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100982 pin = look_up_pincfg(codec, &codec->init_pins, nid);
983 if (pin)
984 return pin->cfg;
985 return 0;
986}
987EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
988
989/* restore all current pin configs */
990static void restore_pincfgs(struct hda_codec *codec)
991{
992 int i;
993 for (i = 0; i < codec->init_pins.used; i++) {
994 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
995 set_pincfg(codec, pin->nid,
996 snd_hda_codec_get_pincfg(codec, pin->nid));
997 }
998}
Takashi Iwai54d17402005-11-21 16:33:22 +0100999
Takashi Iwai92ee6162009-12-27 11:18:59 +01001000/**
1001 * snd_hda_shutup_pins - Shut up all pins
1002 * @codec: the HDA codec
1003 *
1004 * Clear all pin controls to shup up before suspend for avoiding click noise.
1005 * The controls aren't cached so that they can be resumed properly.
1006 */
1007void snd_hda_shutup_pins(struct hda_codec *codec)
1008{
1009 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001010 /* don't shut up pins when unloading the driver; otherwise it breaks
1011 * the default pin setup at the next load of the driver
1012 */
1013 if (codec->bus->shutdown)
1014 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001015 for (i = 0; i < codec->init_pins.used; i++) {
1016 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1017 /* use read here for syncing after issuing each verb */
1018 snd_hda_codec_read(codec, pin->nid, 0,
1019 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1020 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001021 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001022}
1023EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1024
Mike Waychison1c7276c2011-04-20 12:04:36 -07001025#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001026/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1027static void restore_shutup_pins(struct hda_codec *codec)
1028{
1029 int i;
1030 if (!codec->pins_shutup)
1031 return;
1032 if (codec->bus->shutdown)
1033 return;
1034 for (i = 0; i < codec->init_pins.used; i++) {
1035 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1036 snd_hda_codec_write(codec, pin->nid, 0,
1037 AC_VERB_SET_PIN_WIDGET_CONTROL,
1038 pin->ctrl);
1039 }
1040 codec->pins_shutup = 0;
1041}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001042#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001043
Takashi Iwai01751f52007-08-10 16:59:39 +02001044static void init_hda_cache(struct hda_cache_rec *cache,
1045 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001046static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001047
Takashi Iwai3be14142009-02-20 14:11:16 +01001048/* restore the initial pin cfgs and release all pincfg lists */
1049static void restore_init_pincfgs(struct hda_codec *codec)
1050{
Takashi Iwai346ff702009-02-23 09:42:57 +01001051 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001052 * so that only the values in init_pins are restored
1053 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001054 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001055#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001056 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001057#endif
1058 restore_pincfgs(codec);
1059 snd_array_free(&codec->init_pins);
1060}
1061
Takashi Iwai54d17402005-11-21 16:33:22 +01001062/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001063 * audio-converter setup caches
1064 */
1065struct hda_cvt_setup {
1066 hda_nid_t nid;
1067 u8 stream_tag;
1068 u8 channel_id;
1069 u16 format_id;
1070 unsigned char active; /* cvt is currently used */
1071 unsigned char dirty; /* setups should be cleared */
1072};
1073
1074/* get or create a cache entry for the given audio converter NID */
1075static struct hda_cvt_setup *
1076get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1077{
1078 struct hda_cvt_setup *p;
1079 int i;
1080
1081 for (i = 0; i < codec->cvt_setups.used; i++) {
1082 p = snd_array_elem(&codec->cvt_setups, i);
1083 if (p->nid == nid)
1084 return p;
1085 }
1086 p = snd_array_new(&codec->cvt_setups);
1087 if (p)
1088 p->nid = nid;
1089 return p;
1090}
1091
1092/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 * codec destructor
1094 */
1095static void snd_hda_codec_free(struct hda_codec *codec)
1096{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001097 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001099 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001100#ifdef CONFIG_SND_HDA_POWER_SAVE
1101 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001102 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001105 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001106 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001107 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001108 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 codec->bus->caddr_tbl[codec->addr] = NULL;
1110 if (codec->patch_ops.free)
1111 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001112 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001113 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001114 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001115 kfree(codec->vendor_name);
1116 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001117 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001118 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 kfree(codec);
1120}
1121
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001122static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1123 unsigned int power_state);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/**
1126 * snd_hda_codec_new - create a HDA codec
1127 * @bus: the bus to assign
1128 * @codec_addr: the codec address
1129 * @codecp: the pointer to store the generated codec
1130 *
1131 * Returns 0 if successful, or a negative error code.
1132 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001133int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1134 unsigned int codec_addr,
1135 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001138 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int err;
1140
Takashi Iwaida3cec32008-08-08 17:12:14 +02001141 if (snd_BUG_ON(!bus))
1142 return -EINVAL;
1143 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001147 snd_printk(KERN_ERR "hda_codec: "
1148 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return -EBUSY;
1150 }
1151
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001152 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (codec == NULL) {
1154 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1155 return -ENOMEM;
1156 }
1157
1158 codec->bus = bus;
1159 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001160 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001161 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001162 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001163 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001164 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1165 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001166 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001167 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001168 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001169 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001170 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001171 if (codec->bus->modelname) {
1172 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1173 if (!codec->modelname) {
1174 snd_hda_codec_free(codec);
1175 return -ENODEV;
1176 }
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Takashi Iwaicb53c622007-08-10 17:21:45 +02001179#ifdef CONFIG_SND_HDA_POWER_SAVE
1180 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1181 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1182 * the caller has to power down appropriatley after initialization
1183 * phase.
1184 */
1185 hda_keep_power_on(codec);
1186#endif
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 list_add_tail(&codec->list, &bus->codec_list);
1189 bus->caddr_tbl[codec_addr] = codec;
1190
Takashi Iwai0ba21762007-04-16 11:29:14 +02001191 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1192 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001193 if (codec->vendor_id == -1)
1194 /* read again, hopefully the access method was corrected
1195 * in the last read...
1196 */
1197 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1198 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001199 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1200 AC_PAR_SUBSYSTEM_ID);
1201 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1202 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001204 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001205 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001206 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001207 err = -ENODEV;
1208 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
Takashi Iwai3be14142009-02-20 14:11:16 +01001211 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1212 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001213 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001214 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001215 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001216 err = read_pin_defaults(codec);
1217 if (err < 0)
1218 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001219
Takashi Iwai0ba21762007-04-16 11:29:14 +02001220 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001221 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001222 codec->subsystem_id =
1223 snd_hda_codec_read(codec, nid, 0,
1224 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001225 }
1226
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001227 /* power-up all before initialization */
1228 hda_set_power_state(codec,
1229 codec->afg ? codec->afg : codec->mfg,
1230 AC_PWRST_D0);
1231
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001232 snd_hda_codec_proc_new(codec);
1233
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001234 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001235
1236 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1237 codec->subsystem_id, codec->revision_id);
1238 snd_component_add(codec->bus->card, component);
1239
1240 if (codecp)
1241 *codecp = codec;
1242 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001243
1244 error:
1245 snd_hda_codec_free(codec);
1246 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001248EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001249
Takashi Iwaid5191e52009-11-16 14:58:17 +01001250/**
1251 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1252 * @codec: the HDA codec
1253 *
1254 * Start parsing of the given codec tree and (re-)initialize the whole
1255 * patch instance.
1256 *
1257 * Returns 0 if successful or a negative error code.
1258 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001259int snd_hda_codec_configure(struct hda_codec *codec)
1260{
1261 int err;
1262
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001263 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001264 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001265 err = get_codec_name(codec);
1266 if (err < 0)
1267 return err;
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Takashi Iwai82467612007-07-27 19:15:54 +02001270 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001272 goto patched;
1273 }
Takashi Iwai82467612007-07-27 19:15:54 +02001274 if (codec->preset && codec->preset->patch) {
1275 err = codec->preset->patch(codec);
1276 goto patched;
1277 }
1278
1279 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001280 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001281 if (err < 0)
1282 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001283
1284 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001285 if (!err && codec->patch_ops.unsol_event)
1286 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001287 /* audio codec should override the mixer name */
1288 if (!err && (codec->afg || !*codec->bus->card->mixername))
1289 snprintf(codec->bus->card->mixername,
1290 sizeof(codec->bus->card->mixername),
1291 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001294EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296/**
1297 * snd_hda_codec_setup_stream - set up the codec for streaming
1298 * @codec: the CODEC to set up
1299 * @nid: the NID to set up
1300 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1301 * @channel_id: channel id to pass, zero based.
1302 * @format: stream format.
1303 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001304void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1305 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int channel_id, int format)
1307{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001308 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001309 struct hda_cvt_setup *p;
1310 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001311 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001312 int i;
1313
Takashi Iwai0ba21762007-04-16 11:29:14 +02001314 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001315 return;
1316
Takashi Iwai0ba21762007-04-16 11:29:14 +02001317 snd_printdd("hda_codec_setup_stream: "
1318 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001320 p = get_hda_cvt_setup(codec, nid);
1321 if (!p)
1322 return;
1323 /* update the stream-id if changed */
1324 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1325 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1326 newval = (stream_tag << 4) | channel_id;
1327 if (oldval != newval)
1328 snd_hda_codec_write(codec, nid, 0,
1329 AC_VERB_SET_CHANNEL_STREAMID,
1330 newval);
1331 p->stream_tag = stream_tag;
1332 p->channel_id = channel_id;
1333 }
1334 /* update the format-id if changed */
1335 if (p->format_id != format) {
1336 oldval = snd_hda_codec_read(codec, nid, 0,
1337 AC_VERB_GET_STREAM_FORMAT, 0);
1338 if (oldval != format) {
1339 msleep(1);
1340 snd_hda_codec_write(codec, nid, 0,
1341 AC_VERB_SET_STREAM_FORMAT,
1342 format);
1343 }
1344 p->format_id = format;
1345 }
1346 p->active = 1;
1347 p->dirty = 0;
1348
1349 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001350 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001351 list_for_each_entry(c, &codec->bus->codec_list, list) {
1352 for (i = 0; i < c->cvt_setups.used; i++) {
1353 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001354 if (!p->active && p->stream_tag == stream_tag &&
1355 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001356 p->dirty = 1;
1357 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001360EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Takashi Iwaif0cea792010-08-13 11:56:53 +02001362static void really_cleanup_stream(struct hda_codec *codec,
1363 struct hda_cvt_setup *q);
1364
Takashi Iwaid5191e52009-11-16 14:58:17 +01001365/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001366 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001367 * @codec: the CODEC to clean up
1368 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001369 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001370 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001371void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1372 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001373{
Takashi Iwaieb541332010-08-06 13:48:11 +02001374 struct hda_cvt_setup *p;
1375
Takashi Iwai888afa12008-03-18 09:57:50 +01001376 if (!nid)
1377 return;
1378
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001379 if (codec->no_sticky_stream)
1380 do_now = 1;
1381
Takashi Iwai888afa12008-03-18 09:57:50 +01001382 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001383 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001384 if (p) {
1385 /* here we just clear the active flag when do_now isn't set;
1386 * actual clean-ups will be done later in
1387 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1388 */
1389 if (do_now)
1390 really_cleanup_stream(codec, p);
1391 else
1392 p->active = 0;
1393 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001394}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001395EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001396
Takashi Iwaieb541332010-08-06 13:48:11 +02001397static void really_cleanup_stream(struct hda_codec *codec,
1398 struct hda_cvt_setup *q)
1399{
1400 hda_nid_t nid = q->nid;
1401 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1402 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1403 memset(q, 0, sizeof(*q));
1404 q->nid = nid;
1405}
1406
1407/* clean up the all conflicting obsolete streams */
1408static void purify_inactive_streams(struct hda_codec *codec)
1409{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001410 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001411 int i;
1412
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001413 list_for_each_entry(c, &codec->bus->codec_list, list) {
1414 for (i = 0; i < c->cvt_setups.used; i++) {
1415 struct hda_cvt_setup *p;
1416 p = snd_array_elem(&c->cvt_setups, i);
1417 if (p->dirty)
1418 really_cleanup_stream(c, p);
1419 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001420 }
1421}
1422
Mike Waychison1c7276c2011-04-20 12:04:36 -07001423#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaieb541332010-08-06 13:48:11 +02001424/* clean up all streams; called from suspend */
1425static void hda_cleanup_all_streams(struct hda_codec *codec)
1426{
1427 int i;
1428
1429 for (i = 0; i < codec->cvt_setups.used; i++) {
1430 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1431 if (p->stream_tag)
1432 really_cleanup_stream(codec, p);
1433 }
1434}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001435#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*
1438 * amp access functions
1439 */
1440
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001441/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001442#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001443#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001444#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1445#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001447#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001450static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001451 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Takashi Iwai01751f52007-08-10 16:59:39 +02001453 memset(cache, 0, sizeof(*cache));
1454 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001455 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001456}
1457
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001458static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001459{
Takashi Iwai603c4012008-07-30 15:01:44 +02001460 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
1463/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001464static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Takashi Iwai01751f52007-08-10 16:59:39 +02001466 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1467 u16 cur = cache->hash[idx];
1468 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001471 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (info->key == key)
1473 return info;
1474 cur = info->next;
1475 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001476 return NULL;
1477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001479/* query the hash. allocate an entry if not found. */
1480static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1481 u32 key)
1482{
1483 struct hda_cache_head *info = get_hash(cache, key);
1484 if (!info) {
1485 u16 idx, cur;
1486 /* add a new hash entry */
1487 info = snd_array_new(&cache->buf);
1488 if (!info)
1489 return NULL;
1490 cur = snd_array_index(&cache->buf, info);
1491 info->key = key;
1492 info->val = 0;
1493 idx = key % (u16)ARRAY_SIZE(cache->hash);
1494 info->next = cache->hash[idx];
1495 cache->hash[idx] = cur;
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return info;
1498}
1499
Takashi Iwai01751f52007-08-10 16:59:39 +02001500/* query and allocate an amp hash entry */
1501static inline struct hda_amp_info *
1502get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1503{
1504 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1505}
1506
Takashi Iwaid5191e52009-11-16 14:58:17 +01001507/**
1508 * query_amp_caps - query AMP capabilities
1509 * @codec: the HD-auio codec
1510 * @nid: the NID to query
1511 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1512 *
1513 * Query AMP capabilities for the given widget and direction.
1514 * Returns the obtained capability bits.
1515 *
1516 * When cap bits have been already read, this doesn't read again but
1517 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001519u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001521 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Takashi Iwai0ba21762007-04-16 11:29:14 +02001523 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1524 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001526 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001527 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001529 info->amp_caps = snd_hda_param_read(codec, nid,
1530 direction == HDA_OUTPUT ?
1531 AC_PAR_AMP_OUT_CAP :
1532 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001533 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001534 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536 return info->amp_caps;
1537}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001538EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Takashi Iwaid5191e52009-11-16 14:58:17 +01001540/**
1541 * snd_hda_override_amp_caps - Override the AMP capabilities
1542 * @codec: the CODEC to clean up
1543 * @nid: the NID to clean up
1544 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1545 * @caps: the capability bits to set
1546 *
1547 * Override the cached AMP caps bits value by the given one.
1548 * This function is useful if the driver needs to adjust the AMP ranges,
1549 * e.g. limit to 0dB, etc.
1550 *
1551 * Returns zero if successful or a negative error code.
1552 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001553int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1554 unsigned int caps)
1555{
1556 struct hda_amp_info *info;
1557
1558 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1559 if (!info)
1560 return -EINVAL;
1561 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001562 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001563 return 0;
1564}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001565EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001566
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001567static unsigned int
1568query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1569 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001570{
1571 struct hda_amp_info *info;
1572
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001573 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001574 if (!info)
1575 return 0;
1576 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001577 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001578 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001579 }
1580 return info->amp_caps;
1581}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001582
1583static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1584{
1585 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1586}
1587
Takashi Iwaid5191e52009-11-16 14:58:17 +01001588/**
1589 * snd_hda_query_pin_caps - Query PIN capabilities
1590 * @codec: the HD-auio codec
1591 * @nid: the NID to query
1592 *
1593 * Query PIN capabilities for the given widget.
1594 * Returns the obtained capability bits.
1595 *
1596 * When cap bits have been already read, this doesn't read again but
1597 * returns the cached value.
1598 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001599u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1600{
1601 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1602 read_pin_cap);
1603}
Takashi Iwai1327a322009-03-23 13:07:47 +01001604EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1605
Wu Fengguang864f92b2009-11-18 12:38:02 +08001606/**
1607 * snd_hda_pin_sense - execute pin sense measurement
1608 * @codec: the CODEC to sense
1609 * @nid: the pin NID to sense
1610 *
1611 * Execute necessary pin sense measurement and return its Presence Detect,
1612 * Impedance, ELD Valid etc. status bits.
1613 */
1614u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1615{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001616 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001617
Takashi Iwai729d55b2009-12-25 22:49:01 +01001618 if (!codec->no_trigger_sense) {
1619 pincap = snd_hda_query_pin_caps(codec, nid);
1620 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001621 snd_hda_codec_read(codec, nid, 0,
1622 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001623 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001624 return snd_hda_codec_read(codec, nid, 0,
1625 AC_VERB_GET_PIN_SENSE, 0);
1626}
1627EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1628
1629/**
1630 * snd_hda_jack_detect - query pin Presence Detect status
1631 * @codec: the CODEC to sense
1632 * @nid: the pin NID to sense
1633 *
1634 * Query and return the pin's Presence Detect status.
1635 */
1636int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1637{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001638 u32 sense = snd_hda_pin_sense(codec, nid);
1639 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001640}
1641EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643/*
1644 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001645 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001647static unsigned int get_vol_mute(struct hda_codec *codec,
1648 struct hda_amp_info *info, hda_nid_t nid,
1649 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 u32 val, parm;
1652
Takashi Iwai01751f52007-08-10 16:59:39 +02001653 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001654 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1657 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1658 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001659 val = snd_hda_codec_read(codec, nid, 0,
1660 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001662 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001663 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
1666/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001667 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001669static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001670 hda_nid_t nid, int ch, int direction, int index,
1671 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 u32 parm;
1674
1675 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1676 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1677 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1678 parm |= val;
1679 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001680 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Takashi Iwaid5191e52009-11-16 14:58:17 +01001683/**
1684 * snd_hda_codec_amp_read - Read 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 * @index: the index value (only for input direction)
1690 *
1691 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 */
Takashi Iwai834be882006-03-01 14:16:17 +01001693int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1694 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001696 struct hda_amp_info *info;
1697 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1698 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001700 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001702EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Takashi Iwaid5191e52009-11-16 14:58:17 +01001704/**
1705 * snd_hda_codec_amp_update - update the AMP value
1706 * @codec: HD-audio codec
1707 * @nid: NID to read the AMP value
1708 * @ch: channel (left=0 or right=1)
1709 * @direction: #HDA_INPUT or #HDA_OUTPUT
1710 * @idx: the index value (only for input direction)
1711 * @mask: bit mask to set
1712 * @val: the bits value to set
1713 *
1714 * Update the AMP value with a bit mask.
1715 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001716 */
Takashi Iwai834be882006-03-01 14:16:17 +01001717int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1718 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001720 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001721
Takashi Iwai0ba21762007-04-16 11:29:14 +02001722 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1723 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001725 if (snd_BUG_ON(mask & ~0xff))
1726 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001727 val &= mask;
1728 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001729 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001731 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return 1;
1733}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001734EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Takashi Iwaid5191e52009-11-16 14:58:17 +01001736/**
1737 * snd_hda_codec_amp_stereo - update the AMP stereo values
1738 * @codec: HD-audio codec
1739 * @nid: NID to read the AMP value
1740 * @direction: #HDA_INPUT or #HDA_OUTPUT
1741 * @idx: the index value (only for input direction)
1742 * @mask: bit mask to set
1743 * @val: the bits value to set
1744 *
1745 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1746 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001747 */
1748int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1749 int direction, int idx, int mask, int val)
1750{
1751 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001752
1753 if (snd_BUG_ON(mask & ~0xff))
1754 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001755 for (ch = 0; ch < 2; ch++)
1756 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1757 idx, mask, val);
1758 return ret;
1759}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001760EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001761
Takashi Iwaicb53c622007-08-10 17:21:45 +02001762#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001763/**
1764 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1765 * @codec: HD-audio codec
1766 *
1767 * Resume the all amp commands from the cache.
1768 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001769void snd_hda_codec_resume_amp(struct hda_codec *codec)
1770{
Takashi Iwai603c4012008-07-30 15:01:44 +02001771 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001772 int i;
1773
Takashi Iwai603c4012008-07-30 15:01:44 +02001774 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001775 u32 key = buffer->head.key;
1776 hda_nid_t nid;
1777 unsigned int idx, dir, ch;
1778 if (!key)
1779 continue;
1780 nid = key & 0xff;
1781 idx = (key >> 16) & 0xff;
1782 dir = (key >> 24) & 0xff;
1783 for (ch = 0; ch < 2; ch++) {
1784 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1785 continue;
1786 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1787 buffer->vol[ch]);
1788 }
1789 }
1790}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001791EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001792#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001794static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1795 unsigned int ofs)
1796{
1797 u32 caps = query_amp_caps(codec, nid, dir);
1798 /* get num steps */
1799 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1800 if (ofs < caps)
1801 caps -= ofs;
1802 return caps;
1803}
1804
Takashi Iwaid5191e52009-11-16 14:58:17 +01001805/**
1806 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1807 *
1808 * The control element is supposed to have the private_value field
1809 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1810 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001811int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1812 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1815 u16 nid = get_amp_nid(kcontrol);
1816 u8 chs = get_amp_channels(kcontrol);
1817 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001818 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001820 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1821 uinfo->count = chs == 3 ? 2 : 1;
1822 uinfo->value.integer.min = 0;
1823 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1824 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001825 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001826 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1827 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return -EINVAL;
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return 0;
1831}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001832EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001834
1835static inline unsigned int
1836read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1837 int ch, int dir, int idx, unsigned int ofs)
1838{
1839 unsigned int val;
1840 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1841 val &= HDA_AMP_VOLMASK;
1842 if (val >= ofs)
1843 val -= ofs;
1844 else
1845 val = 0;
1846 return val;
1847}
1848
1849static inline int
1850update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1851 int ch, int dir, int idx, unsigned int ofs,
1852 unsigned int val)
1853{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001854 unsigned int maxval;
1855
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001856 if (val > 0)
1857 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001858 /* ofs = 0: raw max value */
1859 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001860 if (val > maxval)
1861 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001862 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1863 HDA_AMP_VOLMASK, val);
1864}
1865
Takashi Iwaid5191e52009-11-16 14:58:17 +01001866/**
1867 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1868 *
1869 * The control element is supposed to have the private_value field
1870 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1871 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001872int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1873 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1876 hda_nid_t nid = get_amp_nid(kcontrol);
1877 int chs = get_amp_channels(kcontrol);
1878 int dir = get_amp_direction(kcontrol);
1879 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001880 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 long *valp = ucontrol->value.integer.value;
1882
1883 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001884 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001886 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return 0;
1888}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001889EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Takashi Iwaid5191e52009-11-16 14:58:17 +01001891/**
1892 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1893 *
1894 * The control element is supposed to have the private_value field
1895 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1896 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001897int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1898 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1901 hda_nid_t nid = get_amp_nid(kcontrol);
1902 int chs = get_amp_channels(kcontrol);
1903 int dir = get_amp_direction(kcontrol);
1904 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001905 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 long *valp = ucontrol->value.integer.value;
1907 int change = 0;
1908
Takashi Iwaicb53c622007-08-10 17:21:45 +02001909 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001910 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001911 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001912 valp++;
1913 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001914 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001915 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001916 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return change;
1918}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001919EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Takashi Iwaid5191e52009-11-16 14:58:17 +01001921/**
1922 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1923 *
1924 * The control element is supposed to have the private_value field
1925 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1926 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001927int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1928 unsigned int size, unsigned int __user *_tlv)
1929{
1930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1931 hda_nid_t nid = get_amp_nid(kcontrol);
1932 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001933 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001934 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001935 u32 caps, val1, val2;
1936
1937 if (size < 4 * sizeof(unsigned int))
1938 return -ENOMEM;
1939 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001940 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1941 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001942 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001943 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001944 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001945 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02001946 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001947 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1948 return -EFAULT;
1949 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1950 return -EFAULT;
1951 if (put_user(val1, _tlv + 2))
1952 return -EFAULT;
1953 if (put_user(val2, _tlv + 3))
1954 return -EFAULT;
1955 return 0;
1956}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001957EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001958
Takashi Iwaid5191e52009-11-16 14:58:17 +01001959/**
1960 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1961 * @codec: HD-audio codec
1962 * @nid: NID of a reference widget
1963 * @dir: #HDA_INPUT or #HDA_OUTPUT
1964 * @tlv: TLV data to be stored, at least 4 elements
1965 *
1966 * Set (static) TLV data for a virtual master volume using the AMP caps
1967 * obtained from the reference NID.
1968 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001969 */
1970void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1971 unsigned int *tlv)
1972{
1973 u32 caps;
1974 int nums, step;
1975
1976 caps = query_amp_caps(codec, nid, dir);
1977 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1978 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1979 step = (step + 1) * 25;
1980 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1981 tlv[1] = 2 * sizeof(unsigned int);
1982 tlv[2] = -nums * step;
1983 tlv[3] = step;
1984}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001985EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001986
1987/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001988static struct snd_kcontrol *
1989_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1990 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001991{
1992 struct snd_ctl_elem_id id;
1993 memset(&id, 0, sizeof(id));
1994 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001995 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001996 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1997 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001998 strcpy(id.name, name);
1999 return snd_ctl_find_id(codec->bus->card, &id);
2000}
2001
Takashi Iwaid5191e52009-11-16 14:58:17 +01002002/**
2003 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2004 * @codec: HD-audio codec
2005 * @name: ctl id name string
2006 *
2007 * Get the control element with the given id string and IFACE_MIXER.
2008 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002009struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2010 const char *name)
2011{
2012 return _snd_hda_find_mixer_ctl(codec, name, 0);
2013}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002014EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002015
Takashi Iwai1afe2062010-12-23 10:17:52 +01002016static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2017{
2018 int idx;
2019 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2020 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2021 return idx;
2022 }
2023 return -EBUSY;
2024}
2025
Takashi Iwaid5191e52009-11-16 14:58:17 +01002026/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002027 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002028 * @codec: HD-audio codec
2029 * @nid: corresponding NID (optional)
2030 * @kctl: the control element to assign
2031 *
2032 * Add the given control element to an array inside the codec instance.
2033 * All control elements belonging to a codec are supposed to be added
2034 * by this function so that a proper clean-up works at the free or
2035 * reconfiguration time.
2036 *
2037 * If non-zero @nid is passed, the NID is assigned to the control element.
2038 * The assignment is shown in the codec proc file.
2039 *
2040 * snd_hda_ctl_add() checks the control subdev id field whether
2041 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002042 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2043 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002044 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002045int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2046 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002047{
2048 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002049 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002050 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002051
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002052 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002053 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002054 if (nid == 0)
2055 nid = get_amp_nid_(kctl->private_value);
2056 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002057 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2058 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002059 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002060 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002061 err = snd_ctl_add(codec->bus->card, kctl);
2062 if (err < 0)
2063 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002064 item = snd_array_new(&codec->mixers);
2065 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002066 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002067 item->kctl = kctl;
2068 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002069 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002070 return 0;
2071}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002072EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002073
Takashi Iwaid5191e52009-11-16 14:58:17 +01002074/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002075 * snd_hda_add_nid - Assign a NID to a control element
2076 * @codec: HD-audio codec
2077 * @nid: corresponding NID (optional)
2078 * @kctl: the control element to assign
2079 * @index: index to kctl
2080 *
2081 * Add the given control element to an array inside the codec instance.
2082 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2083 * NID:KCTL mapping - for example "Capture Source" selector.
2084 */
2085int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2086 unsigned int index, hda_nid_t nid)
2087{
2088 struct hda_nid_item *item;
2089
2090 if (nid > 0) {
2091 item = snd_array_new(&codec->nids);
2092 if (!item)
2093 return -ENOMEM;
2094 item->kctl = kctl;
2095 item->index = index;
2096 item->nid = nid;
2097 return 0;
2098 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002099 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2100 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002101 return -EINVAL;
2102}
2103EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2104
2105/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002106 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2107 * @codec: HD-audio codec
2108 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002109void snd_hda_ctls_clear(struct hda_codec *codec)
2110{
2111 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002112 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002113 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002114 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002115 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002116 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002117}
2118
Takashi Iwaia65d6292009-02-23 16:57:04 +01002119/* pseudo device locking
2120 * toggle card->shutdown to allow/disallow the device access (as a hack)
2121 */
2122static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002123{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002124 spin_lock(&card->files_lock);
2125 if (card->shutdown) {
2126 spin_unlock(&card->files_lock);
2127 return -EINVAL;
2128 }
2129 card->shutdown = 1;
2130 spin_unlock(&card->files_lock);
2131 return 0;
2132}
2133
2134static void hda_unlock_devices(struct snd_card *card)
2135{
2136 spin_lock(&card->files_lock);
2137 card->shutdown = 0;
2138 spin_unlock(&card->files_lock);
2139}
2140
Takashi Iwaid5191e52009-11-16 14:58:17 +01002141/**
2142 * snd_hda_codec_reset - Clear all objects assigned to the codec
2143 * @codec: HD-audio codec
2144 *
2145 * This frees the all PCM and control elements assigned to the codec, and
2146 * clears the caches and restores the pin default configurations.
2147 *
2148 * When a device is being used, it returns -EBSY. If successfully freed,
2149 * returns zero.
2150 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002151int snd_hda_codec_reset(struct hda_codec *codec)
2152{
2153 struct snd_card *card = codec->bus->card;
2154 int i, pcm;
2155
2156 if (hda_lock_devices(card) < 0)
2157 return -EBUSY;
2158 /* check whether the codec isn't used by any mixer or PCM streams */
2159 if (!list_empty(&card->ctl_files)) {
2160 hda_unlock_devices(card);
2161 return -EBUSY;
2162 }
2163 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2164 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2165 if (!cpcm->pcm)
2166 continue;
2167 if (cpcm->pcm->streams[0].substream_opened ||
2168 cpcm->pcm->streams[1].substream_opened) {
2169 hda_unlock_devices(card);
2170 return -EBUSY;
2171 }
2172 }
2173
2174 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002175
2176#ifdef CONFIG_SND_HDA_POWER_SAVE
2177 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002178 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002179#endif
2180 snd_hda_ctls_clear(codec);
2181 /* relase PCMs */
2182 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002183 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002184 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002185 clear_bit(codec->pcm_info[i].device,
2186 codec->bus->pcm_dev_bits);
2187 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002188 }
2189 if (codec->patch_ops.free)
2190 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002191 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002192 codec->spec = NULL;
2193 free_hda_cache(&codec->amp_cache);
2194 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002195 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2196 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002197 /* free only driver_pins so that init_pins + user_pins are restored */
2198 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002199 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002200 codec->num_pcms = 0;
2201 codec->pcm_info = NULL;
2202 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002203 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2204 codec->slave_dig_outs = NULL;
2205 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002206 module_put(codec->owner);
2207 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002208
2209 /* allow device access again */
2210 hda_unlock_devices(card);
2211 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002212}
2213
Takashi Iwaid5191e52009-11-16 14:58:17 +01002214/**
2215 * snd_hda_add_vmaster - create a virtual master control and add slaves
2216 * @codec: HD-audio codec
2217 * @name: vmaster control name
2218 * @tlv: TLV data (optional)
2219 * @slaves: slave control names (optional)
2220 *
2221 * Create a virtual master control with the given name. The TLV data
2222 * must be either NULL or a valid data.
2223 *
2224 * @slaves is a NULL-terminated array of strings, each of which is a
2225 * slave control name. All controls with these names are assigned to
2226 * the new virtual master control.
2227 *
2228 * This function returns zero if successful or a negative error code.
2229 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002230int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002231 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002232{
2233 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002234 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002235 int err;
2236
Takashi Iwai2f085542008-02-22 18:43:50 +01002237 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2238 ;
2239 if (!*s) {
2240 snd_printdd("No slave found for %s\n", name);
2241 return 0;
2242 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002243 kctl = snd_ctl_make_virtual_master(name, tlv);
2244 if (!kctl)
2245 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002246 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002247 if (err < 0)
2248 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002249
Takashi Iwai2134ea42008-01-10 16:53:55 +01002250 for (s = slaves; *s; s++) {
2251 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002252 int i = 0;
2253 for (;;) {
2254 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2255 if (!sctl) {
2256 if (!i)
2257 snd_printdd("Cannot find slave %s, "
2258 "skipped\n", *s);
2259 break;
2260 }
2261 err = snd_ctl_add_slave(kctl, sctl);
2262 if (err < 0)
2263 return err;
2264 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002265 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002266 }
2267 return 0;
2268}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002269EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002270
Takashi Iwaid5191e52009-11-16 14:58:17 +01002271/**
2272 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2273 *
2274 * The control element is supposed to have the private_value field
2275 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2276 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002277int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 int chs = get_amp_channels(kcontrol);
2281
2282 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2283 uinfo->count = chs == 3 ? 2 : 1;
2284 uinfo->value.integer.min = 0;
2285 uinfo->value.integer.max = 1;
2286 return 0;
2287}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002288EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Takashi Iwaid5191e52009-11-16 14:58:17 +01002290/**
2291 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2292 *
2293 * The control element is supposed to have the private_value field
2294 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2295 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002296int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2297 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2300 hda_nid_t nid = get_amp_nid(kcontrol);
2301 int chs = get_amp_channels(kcontrol);
2302 int dir = get_amp_direction(kcontrol);
2303 int idx = get_amp_index(kcontrol);
2304 long *valp = ucontrol->value.integer.value;
2305
2306 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002307 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002308 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002310 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002311 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return 0;
2313}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002314EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Takashi Iwaid5191e52009-11-16 14:58:17 +01002316/**
2317 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2318 *
2319 * The control element is supposed to have the private_value field
2320 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2321 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002322int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2326 hda_nid_t nid = get_amp_nid(kcontrol);
2327 int chs = get_amp_channels(kcontrol);
2328 int dir = get_amp_direction(kcontrol);
2329 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 long *valp = ucontrol->value.integer.value;
2331 int change = 0;
2332
Takashi Iwaicb53c622007-08-10 17:21:45 +02002333 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002334 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002335 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002336 HDA_AMP_MUTE,
2337 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002338 valp++;
2339 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002340 if (chs & 2)
2341 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002342 HDA_AMP_MUTE,
2343 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002344 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002345 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return change;
2347}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002348EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Takashi Iwai67d634c2009-11-16 15:35:59 +01002350#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002351/**
2352 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2353 *
2354 * This function calls snd_hda_enable_beep_device(), which behaves differently
2355 * depending on beep_mode option.
2356 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002357int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2358 struct snd_ctl_elem_value *ucontrol)
2359{
2360 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2361 long *valp = ucontrol->value.integer.value;
2362
2363 snd_hda_enable_beep_device(codec, *valp);
2364 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2365}
2366EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002367#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369/*
Takashi Iwai985be542005-11-02 18:26:49 +01002370 * bound volume controls
2371 *
2372 * bind multiple volumes (# indices, from 0)
2373 */
2374
2375#define AMP_VAL_IDX_SHIFT 19
2376#define AMP_VAL_IDX_MASK (0x0f<<19)
2377
Takashi Iwaid5191e52009-11-16 14:58:17 +01002378/**
2379 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2380 *
2381 * The control element is supposed to have the private_value field
2382 * set up via HDA_BIND_MUTE*() macros.
2383 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002384int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002386{
2387 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2388 unsigned long pval;
2389 int err;
2390
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002391 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002392 pval = kcontrol->private_value;
2393 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2394 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2395 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002396 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002397 return err;
2398}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002399EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002400
Takashi Iwaid5191e52009-11-16 14:58:17 +01002401/**
2402 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2403 *
2404 * The control element is supposed to have the private_value field
2405 * set up via HDA_BIND_MUTE*() macros.
2406 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002407int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2408 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002409{
2410 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2411 unsigned long pval;
2412 int i, indices, err = 0, change = 0;
2413
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002414 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002415 pval = kcontrol->private_value;
2416 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2417 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002418 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2419 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002420 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2421 if (err < 0)
2422 break;
2423 change |= err;
2424 }
2425 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002426 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002427 return err < 0 ? err : change;
2428}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002429EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002430
Takashi Iwaid5191e52009-11-16 14:58:17 +01002431/**
2432 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2433 *
2434 * The control element is supposed to have the private_value field
2435 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002436 */
2437int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2438 struct snd_ctl_elem_info *uinfo)
2439{
2440 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2441 struct hda_bind_ctls *c;
2442 int err;
2443
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002444 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002445 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002446 kcontrol->private_value = *c->values;
2447 err = c->ops->info(kcontrol, uinfo);
2448 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002449 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002450 return err;
2451}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002452EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002453
Takashi Iwaid5191e52009-11-16 14:58:17 +01002454/**
2455 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2456 *
2457 * The control element is supposed to have the private_value field
2458 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2459 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002460int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2461 struct snd_ctl_elem_value *ucontrol)
2462{
2463 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2464 struct hda_bind_ctls *c;
2465 int err;
2466
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002467 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002468 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002469 kcontrol->private_value = *c->values;
2470 err = c->ops->get(kcontrol, ucontrol);
2471 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002472 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002473 return err;
2474}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002475EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002476
Takashi Iwaid5191e52009-11-16 14:58:17 +01002477/**
2478 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2479 *
2480 * The control element is supposed to have the private_value field
2481 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2482 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002483int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2484 struct snd_ctl_elem_value *ucontrol)
2485{
2486 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2487 struct hda_bind_ctls *c;
2488 unsigned long *vals;
2489 int err = 0, change = 0;
2490
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002491 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002492 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002493 for (vals = c->values; *vals; vals++) {
2494 kcontrol->private_value = *vals;
2495 err = c->ops->put(kcontrol, ucontrol);
2496 if (err < 0)
2497 break;
2498 change |= err;
2499 }
2500 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002501 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002502 return err < 0 ? err : change;
2503}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002504EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002505
Takashi Iwaid5191e52009-11-16 14:58:17 +01002506/**
2507 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2508 *
2509 * The control element is supposed to have the private_value field
2510 * set up via HDA_BIND_VOL() macro.
2511 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002512int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2513 unsigned int size, unsigned int __user *tlv)
2514{
2515 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2516 struct hda_bind_ctls *c;
2517 int err;
2518
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002519 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002520 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002521 kcontrol->private_value = *c->values;
2522 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2523 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002524 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002525 return err;
2526}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002527EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002528
2529struct hda_ctl_ops snd_hda_bind_vol = {
2530 .info = snd_hda_mixer_amp_volume_info,
2531 .get = snd_hda_mixer_amp_volume_get,
2532 .put = snd_hda_mixer_amp_volume_put,
2533 .tlv = snd_hda_mixer_amp_tlv
2534};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002535EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002536
2537struct hda_ctl_ops snd_hda_bind_sw = {
2538 .info = snd_hda_mixer_amp_switch_info,
2539 .get = snd_hda_mixer_amp_switch_get,
2540 .put = snd_hda_mixer_amp_switch_put,
2541 .tlv = snd_hda_mixer_amp_tlv
2542};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002543EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002544
2545/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 * SPDIF out controls
2547 */
2548
Takashi Iwai0ba21762007-04-16 11:29:14 +02002549static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2550 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2553 uinfo->count = 1;
2554 return 0;
2555}
2556
Takashi Iwai0ba21762007-04-16 11:29:14 +02002557static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2561 IEC958_AES0_NONAUDIO |
2562 IEC958_AES0_CON_EMPHASIS_5015 |
2563 IEC958_AES0_CON_NOT_COPYRIGHT;
2564 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2565 IEC958_AES1_CON_ORIGINAL;
2566 return 0;
2567}
2568
Takashi Iwai0ba21762007-04-16 11:29:14 +02002569static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2573 IEC958_AES0_NONAUDIO |
2574 IEC958_AES0_PRO_EMPHASIS_5015;
2575 return 0;
2576}
2577
Takashi Iwai0ba21762007-04-16 11:29:14 +02002578static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2579 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002582 int idx = kcontrol->private_value;
2583 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Stephen Warren7c9359762011-06-01 11:14:17 -06002585 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2586 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2587 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2588 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 return 0;
2591}
2592
2593/* convert from SPDIF status bits to HDA SPDIF bits
2594 * bit 0 (DigEn) is always set zero (to be filled later)
2595 */
2596static unsigned short convert_from_spdif_status(unsigned int sbits)
2597{
2598 unsigned short val = 0;
2599
2600 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002601 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002603 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002605 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2606 IEC958_AES0_PRO_EMPHASIS_5015)
2607 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2610 IEC958_AES0_CON_EMPHASIS_5015)
2611 val |= AC_DIG1_EMPHASIS;
2612 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2613 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002615 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2617 }
2618 return val;
2619}
2620
2621/* convert to SPDIF status bits from HDA SPDIF bits
2622 */
2623static unsigned int convert_to_spdif_status(unsigned short val)
2624{
2625 unsigned int sbits = 0;
2626
Takashi Iwai0ba21762007-04-16 11:29:14 +02002627 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002629 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 sbits |= IEC958_AES0_PROFESSIONAL;
2631 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002632 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2634 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002635 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002637 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002639 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2641 sbits |= val & (0x7f << 8);
2642 }
2643 return sbits;
2644}
2645
Takashi Iwai2f728532008-09-25 16:32:41 +02002646/* set digital convert verbs both for the given NID and its slaves */
2647static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2648 int verb, int val)
2649{
Takashi Iwaidda14412011-05-02 11:29:30 +02002650 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002651
Takashi Iwai9e976972008-11-25 08:17:20 +01002652 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002653 d = codec->slave_dig_outs;
2654 if (!d)
2655 return;
2656 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002657 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002658}
2659
2660static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2661 int dig1, int dig2)
2662{
2663 if (dig1 != -1)
2664 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2665 if (dig2 != -1)
2666 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2667}
2668
Takashi Iwai0ba21762007-04-16 11:29:14 +02002669static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002673 int idx = kcontrol->private_value;
2674 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2675 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 unsigned short val;
2677 int change;
2678
Ingo Molnar62932df2006-01-16 16:34:20 +01002679 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002680 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2682 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2683 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002684 val = convert_from_spdif_status(spdif->status);
2685 val |= spdif->ctls & 1;
2686 change = spdif->ctls != val;
2687 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002688 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002689 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002690 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return change;
2692}
2693
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002694#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Takashi Iwai0ba21762007-04-16 11:29:14 +02002696static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2697 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002700 int idx = kcontrol->private_value;
2701 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Stephen Warren7c9359762011-06-01 11:14:17 -06002703 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return 0;
2705}
2706
Stephen Warren74b654c2011-06-01 11:14:18 -06002707static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2708 int dig1, int dig2)
2709{
2710 set_dig_out_convert(codec, nid, dig1, dig2);
2711 /* unmute amp switch (if any) */
2712 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2713 (dig1 & AC_DIG1_ENABLE))
2714 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2715 HDA_AMP_MUTE, 0);
2716}
2717
Takashi Iwai0ba21762007-04-16 11:29:14 +02002718static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2719 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
2721 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002722 int idx = kcontrol->private_value;
2723 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2724 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 unsigned short val;
2726 int change;
2727
Ingo Molnar62932df2006-01-16 16:34:20 +01002728 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002729 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002731 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002732 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002733 spdif->ctls = val;
2734 if (change && nid != (u16)-1)
2735 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002736 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return change;
2738}
2739
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002740static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 {
2742 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002744 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 .info = snd_hda_spdif_mask_info,
2746 .get = snd_hda_spdif_cmask_get,
2747 },
2748 {
2749 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2750 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002751 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 .info = snd_hda_spdif_mask_info,
2753 .get = snd_hda_spdif_pmask_get,
2754 },
2755 {
2756 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002757 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 .info = snd_hda_spdif_mask_info,
2759 .get = snd_hda_spdif_default_get,
2760 .put = snd_hda_spdif_default_put,
2761 },
2762 {
2763 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002764 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 .info = snd_hda_spdif_out_switch_info,
2766 .get = snd_hda_spdif_out_switch_get,
2767 .put = snd_hda_spdif_out_switch_put,
2768 },
2769 { } /* end */
2770};
2771
2772/**
2773 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2774 * @codec: the HDA codec
2775 * @nid: audio out widget NID
2776 *
2777 * Creates controls related with the SPDIF output.
2778 * Called from each patch supporting the SPDIF out.
2779 *
2780 * Returns 0 if successful, or a negative error code.
2781 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002782int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2783 hda_nid_t associated_nid,
2784 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
2786 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002787 struct snd_kcontrol *kctl;
2788 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002789 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002790 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Takashi Iwai1afe2062010-12-23 10:17:52 +01002792 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2793 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002794 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2795 return -EBUSY;
2796 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002797 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2799 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002800 if (!kctl)
2801 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002802 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002803 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002804 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002805 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return err;
2807 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002808 spdif->nid = cvt_nid;
2809 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06002810 AC_VERB_GET_DIGI_CONVERT_1, 0);
2811 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return 0;
2813}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002814EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Stephen Warren7c9359762011-06-01 11:14:17 -06002816struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2817 hda_nid_t nid)
2818{
2819 int i;
2820 for (i = 0; i < codec->spdif_out.used; i++) {
2821 struct hda_spdif_out *spdif =
2822 snd_array_elem(&codec->spdif_out, i);
2823 if (spdif->nid == nid)
2824 return spdif;
2825 }
2826 return NULL;
2827}
2828EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2829
Stephen Warren74b654c2011-06-01 11:14:18 -06002830void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2831{
2832 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2833
2834 mutex_lock(&codec->spdif_mutex);
2835 spdif->nid = (u16)-1;
2836 mutex_unlock(&codec->spdif_mutex);
2837}
2838EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2839
2840void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2841{
2842 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2843 unsigned short val;
2844
2845 mutex_lock(&codec->spdif_mutex);
2846 if (spdif->nid != nid) {
2847 spdif->nid = nid;
2848 val = spdif->ctls;
2849 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2850 }
2851 mutex_unlock(&codec->spdif_mutex);
2852}
2853EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002856 * SPDIF sharing with analog output
2857 */
2858static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2859 struct snd_ctl_elem_value *ucontrol)
2860{
2861 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2862 ucontrol->value.integer.value[0] = mout->share_spdif;
2863 return 0;
2864}
2865
2866static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_value *ucontrol)
2868{
2869 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2870 mout->share_spdif = !!ucontrol->value.integer.value[0];
2871 return 0;
2872}
2873
2874static struct snd_kcontrol_new spdif_share_sw = {
2875 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2876 .name = "IEC958 Default PCM Playback Switch",
2877 .info = snd_ctl_boolean_mono_info,
2878 .get = spdif_share_sw_get,
2879 .put = spdif_share_sw_put,
2880};
2881
Takashi Iwaid5191e52009-11-16 14:58:17 +01002882/**
2883 * snd_hda_create_spdif_share_sw - create Default PCM switch
2884 * @codec: the HDA codec
2885 * @mout: multi-out instance
2886 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002887int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2888 struct hda_multi_out *mout)
2889{
2890 if (!mout->dig_out_nid)
2891 return 0;
2892 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002893 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2894 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002895}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002896EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002897
2898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 * SPDIF input
2900 */
2901
2902#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2903
Takashi Iwai0ba21762007-04-16 11:29:14 +02002904static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2908
2909 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2910 return 0;
2911}
2912
Takashi Iwai0ba21762007-04-16 11:29:14 +02002913static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2917 hda_nid_t nid = kcontrol->private_value;
2918 unsigned int val = !!ucontrol->value.integer.value[0];
2919 int change;
2920
Ingo Molnar62932df2006-01-16 16:34:20 +01002921 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002923 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002925 snd_hda_codec_write_cache(codec, nid, 0,
2926 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002928 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return change;
2930}
2931
Takashi Iwai0ba21762007-04-16 11:29:14 +02002932static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
2935 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2936 hda_nid_t nid = kcontrol->private_value;
2937 unsigned short val;
2938 unsigned int sbits;
2939
Andrew Paprocki3982d172007-12-19 12:13:44 +01002940 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 sbits = convert_to_spdif_status(val);
2942 ucontrol->value.iec958.status[0] = sbits;
2943 ucontrol->value.iec958.status[1] = sbits >> 8;
2944 ucontrol->value.iec958.status[2] = sbits >> 16;
2945 ucontrol->value.iec958.status[3] = sbits >> 24;
2946 return 0;
2947}
2948
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002949static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 {
2951 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002952 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 .info = snd_hda_spdif_in_switch_info,
2954 .get = snd_hda_spdif_in_switch_get,
2955 .put = snd_hda_spdif_in_switch_put,
2956 },
2957 {
2958 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2959 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002960 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 .info = snd_hda_spdif_mask_info,
2962 .get = snd_hda_spdif_in_status_get,
2963 },
2964 { } /* end */
2965};
2966
2967/**
2968 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2969 * @codec: the HDA codec
2970 * @nid: audio in widget NID
2971 *
2972 * Creates controls related with the SPDIF input.
2973 * Called from each patch supporting the SPDIF in.
2974 *
2975 * Returns 0 if successful, or a negative error code.
2976 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002977int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
2979 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002980 struct snd_kcontrol *kctl;
2981 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002982 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Takashi Iwai1afe2062010-12-23 10:17:52 +01002984 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
2985 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002986 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2987 return -EBUSY;
2988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2990 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002991 if (!kctl)
2992 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002994 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002995 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return err;
2997 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002998 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002999 snd_hda_codec_read(codec, nid, 0,
3000 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003001 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return 0;
3003}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003004EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Takashi Iwaicb53c622007-08-10 17:21:45 +02003006#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003007/*
3008 * command cache
3009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003011/* build a 32bit cache key with the widget id and the command parameter */
3012#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3013#define get_cmd_cache_nid(key) ((key) & 0xff)
3014#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3015
3016/**
3017 * snd_hda_codec_write_cache - send a single command with caching
3018 * @codec: the HDA codec
3019 * @nid: NID to send the command
3020 * @direct: direct flag
3021 * @verb: the verb to send
3022 * @parm: the parameter for the verb
3023 *
3024 * Send a single command without waiting for response.
3025 *
3026 * Returns 0 if successful, or a negative error code.
3027 */
3028int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3029 int direct, unsigned int verb, unsigned int parm)
3030{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003031 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3032 struct hda_cache_head *c;
3033 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003034
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003035 if (err < 0)
3036 return err;
3037 /* parm may contain the verb stuff for get/set amp */
3038 verb = verb | (parm >> 8);
3039 parm &= 0xff;
3040 key = build_cmd_cache_key(nid, verb);
3041 mutex_lock(&codec->bus->cmd_mutex);
3042 c = get_alloc_hash(&codec->cmd_cache, key);
3043 if (c)
3044 c->val = parm;
3045 mutex_unlock(&codec->bus->cmd_mutex);
3046 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003047}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003048EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003049
Takashi Iwaid5191e52009-11-16 14:58:17 +01003050/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003051 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3052 * @codec: the HDA codec
3053 * @nid: NID to send the command
3054 * @direct: direct flag
3055 * @verb: the verb to send
3056 * @parm: the parameter for the verb
3057 *
3058 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3059 * command if the parameter is already identical with the cached value.
3060 * If not, it sends the command and refreshes the cache.
3061 *
3062 * Returns 0 if successful, or a negative error code.
3063 */
3064int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3065 int direct, unsigned int verb, unsigned int parm)
3066{
3067 struct hda_cache_head *c;
3068 u32 key;
3069
3070 /* parm may contain the verb stuff for get/set amp */
3071 verb = verb | (parm >> 8);
3072 parm &= 0xff;
3073 key = build_cmd_cache_key(nid, verb);
3074 mutex_lock(&codec->bus->cmd_mutex);
3075 c = get_hash(&codec->cmd_cache, key);
3076 if (c && c->val == parm) {
3077 mutex_unlock(&codec->bus->cmd_mutex);
3078 return 0;
3079 }
3080 mutex_unlock(&codec->bus->cmd_mutex);
3081 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3082}
3083EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3084
3085/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003086 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3087 * @codec: HD-audio codec
3088 *
3089 * Execute all verbs recorded in the command caches to resume.
3090 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003091void snd_hda_codec_resume_cache(struct hda_codec *codec)
3092{
Takashi Iwai603c4012008-07-30 15:01:44 +02003093 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003094 int i;
3095
Takashi Iwai603c4012008-07-30 15:01:44 +02003096 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003097 u32 key = buffer->key;
3098 if (!key)
3099 continue;
3100 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3101 get_cmd_cache_cmd(key), buffer->val);
3102 }
3103}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003104EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003105
3106/**
3107 * snd_hda_sequence_write_cache - sequence writes with caching
3108 * @codec: the HDA codec
3109 * @seq: VERB array to send
3110 *
3111 * Send the commands sequentially from the given array.
3112 * Thte commands are recorded on cache for power-save and resume.
3113 * The array must be terminated with NID=0.
3114 */
3115void snd_hda_sequence_write_cache(struct hda_codec *codec,
3116 const struct hda_verb *seq)
3117{
3118 for (; seq->nid; seq++)
3119 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3120 seq->param);
3121}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003122EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003123#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003124
Takashi Iwai54d17402005-11-21 16:33:22 +01003125/*
3126 * set power state of the codec
3127 */
3128static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3129 unsigned int power_state)
3130{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003131 hda_nid_t nid;
3132 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003133
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003134 /* this delay seems necessary to avoid click noise at power-down */
3135 if (power_state == AC_PWRST_D3)
3136 msleep(100);
3137 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003138 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003139 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003140 if (power_state == AC_PWRST_D0 &&
3141 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003142 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003143
Takashi Iwaicb53c622007-08-10 17:21:45 +02003144 nid = codec->start_nid;
3145 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003146 unsigned int wcaps = get_wcaps(codec, nid);
3147 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003148 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003149 if (power_state == AC_PWRST_D3 &&
3150 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003151 unsigned int pincap;
3152 /*
3153 * don't power down the widget if it controls
3154 * eapd and EAPD_BTLENABLE is set.
3155 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003156 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003157 if (pincap & AC_PINCAP_EAPD) {
3158 int eapd = snd_hda_codec_read(codec,
3159 nid, 0,
3160 AC_VERB_GET_EAPD_BTLENABLE, 0);
3161 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003162 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003163 continue;
3164 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003165 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003166 snd_hda_codec_write(codec, nid, 0,
3167 AC_VERB_SET_POWER_STATE,
3168 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003169 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003170 }
3171
Takashi Iwaicb53c622007-08-10 17:21:45 +02003172 if (power_state == AC_PWRST_D0) {
3173 unsigned long end_time;
3174 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003175 /* wait until the codec reachs to D0 */
3176 end_time = jiffies + msecs_to_jiffies(500);
3177 do {
3178 state = snd_hda_codec_read(codec, fg, 0,
3179 AC_VERB_GET_POWER_STATE, 0);
3180 if (state == power_state)
3181 break;
3182 msleep(1);
3183 } while (time_after_eq(end_time, jiffies));
3184 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003185}
3186
Takashi Iwai11aeff02008-07-30 15:01:46 +02003187#ifdef CONFIG_SND_HDA_HWDEP
3188/* execute additional init verbs */
3189static void hda_exec_init_verbs(struct hda_codec *codec)
3190{
3191 if (codec->init_verbs.list)
3192 snd_hda_sequence_write(codec, codec->init_verbs.list);
3193}
3194#else
3195static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3196#endif
3197
Takashi Iwaicb53c622007-08-10 17:21:45 +02003198#ifdef SND_HDA_NEEDS_RESUME
3199/*
3200 * call suspend and power-down; used both from PM and power-save
3201 */
3202static void hda_call_codec_suspend(struct hda_codec *codec)
3203{
3204 if (codec->patch_ops.suspend)
3205 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003206 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003207 hda_set_power_state(codec,
3208 codec->afg ? codec->afg : codec->mfg,
3209 AC_PWRST_D3);
3210#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003211 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003212 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003213 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003214 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003215 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003216#endif
3217}
3218
3219/*
3220 * kick up codec; used both from PM and power-save
3221 */
3222static void hda_call_codec_resume(struct hda_codec *codec)
3223{
3224 hda_set_power_state(codec,
3225 codec->afg ? codec->afg : codec->mfg,
3226 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003227 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003228 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003229 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003230 if (codec->patch_ops.resume)
3231 codec->patch_ops.resume(codec);
3232 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003233 if (codec->patch_ops.init)
3234 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003235 snd_hda_codec_resume_amp(codec);
3236 snd_hda_codec_resume_cache(codec);
3237 }
3238}
3239#endif /* SND_HDA_NEEDS_RESUME */
3240
Takashi Iwai54d17402005-11-21 16:33:22 +01003241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242/**
3243 * snd_hda_build_controls - build mixer controls
3244 * @bus: the BUS
3245 *
3246 * Creates mixer controls for each codec included in the bus.
3247 *
3248 * Returns 0 if successful, otherwise a negative error code.
3249 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003250int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003252 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Takashi Iwai0ba21762007-04-16 11:29:14 +02003254 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003255 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003256 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003257 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003258 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003259 err = snd_hda_codec_reset(codec);
3260 if (err < 0) {
3261 printk(KERN_ERR
3262 "hda_codec: cannot revert codec\n");
3263 return err;
3264 }
3265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003267 return 0;
3268}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003269EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003271int snd_hda_codec_build_controls(struct hda_codec *codec)
3272{
3273 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003274 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003275 /* continue to initialize... */
3276 if (codec->patch_ops.init)
3277 err = codec->patch_ops.init(codec);
3278 if (!err && codec->patch_ops.build_controls)
3279 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003280 if (err < 0)
3281 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return 0;
3283}
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285/*
3286 * stream formats
3287 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003288struct hda_rate_tbl {
3289 unsigned int hz;
3290 unsigned int alsa_bits;
3291 unsigned int hda_fmt;
3292};
3293
Takashi Iwai92f10b32010-08-03 14:21:00 +02003294/* rate = base * mult / div */
3295#define HDA_RATE(base, mult, div) \
3296 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3297 (((div) - 1) << AC_FMT_DIV_SHIFT))
3298
Takashi Iwaibefdf312005-08-22 13:57:55 +02003299static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003301
3302 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003303 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3304 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3305 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3306 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3307 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3308 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3309 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3310 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3311 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3312 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3313 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003314#define AC_PAR_PCM_RATE_BITS 11
3315 /* up to bits 10, 384kHZ isn't supported properly */
3316
3317 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003318 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003319
Takashi Iwaibefdf312005-08-22 13:57:55 +02003320 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321};
3322
3323/**
3324 * snd_hda_calc_stream_format - calculate format bitset
3325 * @rate: the sample rate
3326 * @channels: the number of channels
3327 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3328 * @maxbps: the max. bps
3329 *
3330 * Calculate the format bitset from the given rate, channels and th PCM format.
3331 *
3332 * Return zero if invalid.
3333 */
3334unsigned int snd_hda_calc_stream_format(unsigned int rate,
3335 unsigned int channels,
3336 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003337 unsigned int maxbps,
3338 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
3340 int i;
3341 unsigned int val = 0;
3342
Takashi Iwaibefdf312005-08-22 13:57:55 +02003343 for (i = 0; rate_bits[i].hz; i++)
3344 if (rate_bits[i].hz == rate) {
3345 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 break;
3347 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003348 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 snd_printdd("invalid rate %d\n", rate);
3350 return 0;
3351 }
3352
3353 if (channels == 0 || channels > 8) {
3354 snd_printdd("invalid channels %d\n", channels);
3355 return 0;
3356 }
3357 val |= channels - 1;
3358
3359 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003360 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003361 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003362 break;
3363 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003364 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 case 20:
3367 case 24:
3368 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003369 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003370 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003372 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003374 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 break;
3376 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003377 snd_printdd("invalid format width %d\n",
3378 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return 0;
3380 }
3381
Anssi Hannula32c168c2010-08-03 13:28:57 +03003382 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003383 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return val;
3386}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003387EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003389static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3390{
3391 unsigned int val = 0;
3392 if (nid != codec->afg &&
3393 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3394 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3395 if (!val || val == -1)
3396 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3397 if (!val || val == -1)
3398 return 0;
3399 return val;
3400}
3401
3402static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3403{
3404 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3405 get_pcm_param);
3406}
3407
3408static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3409{
3410 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3411 if (!streams || streams == -1)
3412 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3413 if (!streams || streams == -1)
3414 return 0;
3415 return streams;
3416}
3417
3418static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3419{
3420 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3421 get_stream_param);
3422}
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424/**
3425 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3426 * @codec: the HDA codec
3427 * @nid: NID to query
3428 * @ratesp: the pointer to store the detected rate bitflags
3429 * @formatsp: the pointer to store the detected formats
3430 * @bpsp: the pointer to store the detected format widths
3431 *
3432 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3433 * or @bsps argument is ignored.
3434 *
3435 * Returns 0 if successful, otherwise a negative error code.
3436 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003437int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3439{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003440 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003442 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003443 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
3445 if (ratesp) {
3446 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003447 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003449 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003451 if (rates == 0) {
3452 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3453 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3454 nid, val,
3455 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3456 return -EIO;
3457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 *ratesp = rates;
3459 }
3460
3461 if (formatsp || bpsp) {
3462 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003463 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003465 streams = query_stream_param(codec, nid);
3466 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
3469 bps = 0;
3470 if (streams & AC_SUPFMT_PCM) {
3471 if (val & AC_SUPPCM_BITS_8) {
3472 formats |= SNDRV_PCM_FMTBIT_U8;
3473 bps = 8;
3474 }
3475 if (val & AC_SUPPCM_BITS_16) {
3476 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3477 bps = 16;
3478 }
3479 if (wcaps & AC_WCAP_DIGITAL) {
3480 if (val & AC_SUPPCM_BITS_32)
3481 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3482 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3483 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3484 if (val & AC_SUPPCM_BITS_24)
3485 bps = 24;
3486 else if (val & AC_SUPPCM_BITS_20)
3487 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003488 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3489 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3491 if (val & AC_SUPPCM_BITS_32)
3492 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 else if (val & AC_SUPPCM_BITS_24)
3494 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003495 else if (val & AC_SUPPCM_BITS_20)
3496 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
3498 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003499 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003501 if (!bps)
3502 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003503 }
3504 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003505 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 /* temporary hack: we have still no proper support
3507 * for the direct AC3 stream...
3508 */
3509 formats |= SNDRV_PCM_FMTBIT_U8;
3510 bps = 8;
3511 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003512 if (formats == 0) {
3513 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3514 "(nid=0x%x, val=0x%x, ovrd=%i, "
3515 "streams=0x%x)\n",
3516 nid, val,
3517 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3518 streams);
3519 return -EIO;
3520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 if (formatsp)
3522 *formatsp = formats;
3523 if (bpsp)
3524 *bpsp = bps;
3525 }
3526
3527 return 0;
3528}
Stephen Warren384a48d2011-06-01 11:14:21 -06003529EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003532 * snd_hda_is_supported_format - Check the validity of the format
3533 * @codec: HD-audio codec
3534 * @nid: NID to check
3535 * @format: the HD-audio format value to check
3536 *
3537 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 *
3539 * Returns 1 if supported, 0 if not.
3540 */
3541int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3542 unsigned int format)
3543{
3544 int i;
3545 unsigned int val = 0, rate, stream;
3546
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003547 val = query_pcm_param(codec, nid);
3548 if (!val)
3549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003552 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003553 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 if (val & (1 << i))
3555 break;
3556 return 0;
3557 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003558 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return 0;
3560
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003561 stream = query_stream_param(codec, nid);
3562 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 return 0;
3564
3565 if (stream & AC_SUPFMT_PCM) {
3566 switch (format & 0xf0) {
3567 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003568 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 return 0;
3570 break;
3571 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003572 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 return 0;
3574 break;
3575 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003576 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 return 0;
3578 break;
3579 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003580 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return 0;
3582 break;
3583 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003584 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return 0;
3586 break;
3587 default:
3588 return 0;
3589 }
3590 } else {
3591 /* FIXME: check for float32 and AC3? */
3592 }
3593
3594 return 1;
3595}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003596EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598/*
3599 * PCM stuff
3600 */
3601static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3602 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003603 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
3605 return 0;
3606}
3607
3608static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3609 struct hda_codec *codec,
3610 unsigned int stream_tag,
3611 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003612 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
3614 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3615 return 0;
3616}
3617
3618static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3619 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003620 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
Takashi Iwai888afa12008-03-18 09:57:50 +01003622 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 return 0;
3624}
3625
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003626static int set_pcm_default_values(struct hda_codec *codec,
3627 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003629 int err;
3630
Takashi Iwai0ba21762007-04-16 11:29:14 +02003631 /* query support PCM information from the given NID */
3632 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003633 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003634 info->rates ? NULL : &info->rates,
3635 info->formats ? NULL : &info->formats,
3636 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003637 if (err < 0)
3638 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
3640 if (info->ops.open == NULL)
3641 info->ops.open = hda_pcm_default_open_close;
3642 if (info->ops.close == NULL)
3643 info->ops.close = hda_pcm_default_open_close;
3644 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003645 if (snd_BUG_ON(!info->nid))
3646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 info->ops.prepare = hda_pcm_default_prepare;
3648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003650 if (snd_BUG_ON(!info->nid))
3651 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 info->ops.cleanup = hda_pcm_default_cleanup;
3653 }
3654 return 0;
3655}
3656
Takashi Iwaieb541332010-08-06 13:48:11 +02003657/*
3658 * codec prepare/cleanup entries
3659 */
3660int snd_hda_codec_prepare(struct hda_codec *codec,
3661 struct hda_pcm_stream *hinfo,
3662 unsigned int stream,
3663 unsigned int format,
3664 struct snd_pcm_substream *substream)
3665{
3666 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003667 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003668 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3669 if (ret >= 0)
3670 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003671 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003672 return ret;
3673}
3674EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3675
3676void snd_hda_codec_cleanup(struct hda_codec *codec,
3677 struct hda_pcm_stream *hinfo,
3678 struct snd_pcm_substream *substream)
3679{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003680 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003681 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003682 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003683}
3684EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3685
Takashi Iwaid5191e52009-11-16 14:58:17 +01003686/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003687const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3688 "Audio", "SPDIF", "HDMI", "Modem"
3689};
3690
Takashi Iwai176d5332008-07-30 15:01:44 +02003691/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003692 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003693 *
3694 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003695 */
3696static int get_empty_pcm_device(struct hda_bus *bus, int type)
3697{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003698 /* audio device indices; not linear to keep compatibility */
3699 static int audio_idx[HDA_PCM_NTYPES][5] = {
3700 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3701 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003702 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003703 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003704 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003705 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003706
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003707 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003708 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3709 return -EINVAL;
3710 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003711
3712 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3713 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3714 return audio_idx[type][i];
3715
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003716 snd_printk(KERN_WARNING "Too many %s devices\n",
3717 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003718 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003719}
3720
3721/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003722 * attach a new PCM stream
3723 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003724static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003725{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003726 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003727 struct hda_pcm_stream *info;
3728 int stream, err;
3729
Takashi Iwaib91f0802008-11-04 08:43:08 +01003730 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003731 return -EINVAL;
3732 for (stream = 0; stream < 2; stream++) {
3733 info = &pcm->stream[stream];
3734 if (info->substreams) {
3735 err = set_pcm_default_values(codec, info);
3736 if (err < 0)
3737 return err;
3738 }
3739 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003740 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003741}
3742
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003743/* assign all PCMs of the given codec */
3744int snd_hda_codec_build_pcms(struct hda_codec *codec)
3745{
3746 unsigned int pcm;
3747 int err;
3748
3749 if (!codec->num_pcms) {
3750 if (!codec->patch_ops.build_pcms)
3751 return 0;
3752 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003753 if (err < 0) {
3754 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003755 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003756 err = snd_hda_codec_reset(codec);
3757 if (err < 0) {
3758 printk(KERN_ERR
3759 "hda_codec: cannot revert codec\n");
3760 return err;
3761 }
3762 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003763 }
3764 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3765 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3766 int dev;
3767
3768 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003769 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003770
3771 if (!cpcm->pcm) {
3772 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3773 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003774 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003775 cpcm->device = dev;
3776 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003777 if (err < 0) {
3778 printk(KERN_ERR "hda_codec: cannot attach "
3779 "PCM stream %d for codec #%d\n",
3780 dev, codec->addr);
3781 continue; /* no fatal error */
3782 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003783 }
3784 }
3785 return 0;
3786}
3787
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788/**
3789 * snd_hda_build_pcms - build PCM information
3790 * @bus: the BUS
3791 *
3792 * Create PCM information for each codec included in the bus.
3793 *
3794 * The build_pcms codec patch is requested to set up codec->num_pcms and
3795 * codec->pcm_info properly. The array is referred by the top-level driver
3796 * to create its PCM instances.
3797 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3798 * callback.
3799 *
3800 * At least, substreams, channels_min and channels_max must be filled for
3801 * each stream. substreams = 0 indicates that the stream doesn't exist.
3802 * When rates and/or formats are zero, the supported values are queried
3803 * from the given nid. The nid is used also by the default ops.prepare
3804 * and ops.cleanup callbacks.
3805 *
3806 * The driver needs to call ops.open in its open callback. Similarly,
3807 * ops.close is supposed to be called in the close callback.
3808 * ops.prepare should be called in the prepare or hw_params callback
3809 * with the proper parameters for set up.
3810 * ops.cleanup should be called in hw_free for clean up of streams.
3811 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003812 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003814int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003816 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Takashi Iwai0ba21762007-04-16 11:29:14 +02003818 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003819 int err = snd_hda_codec_build_pcms(codec);
3820 if (err < 0)
3821 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
3823 return 0;
3824}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003825EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827/**
3828 * snd_hda_check_board_config - compare the current codec with the config table
3829 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003830 * @num_configs: number of config enums
3831 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 * @tbl: configuration table, terminated by null entries
3833 *
3834 * Compares the modelname or PCI subsystem id of the current codec with the
3835 * given configuration table. If a matching entry is found, returns its
3836 * config value (supposed to be 0 or positive).
3837 *
3838 * If no entries are matching, the function returns a negative value.
3839 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003840int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003841 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003842 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003844 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003845 int i;
3846 for (i = 0; i < num_configs; i++) {
3847 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003848 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003849 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3850 "selected\n", models[i]);
3851 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
3853 }
3854 }
3855
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003856 if (!codec->bus->pci || !tbl)
3857 return -1;
3858
3859 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3860 if (!tbl)
3861 return -1;
3862 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003863#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003864 char tmp[10];
3865 const char *model = NULL;
3866 if (models)
3867 model = models[tbl->value];
3868 if (!model) {
3869 sprintf(tmp, "#%d", tbl->value);
3870 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003872 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3873 "for config %x:%x (%s)\n",
3874 model, tbl->subvendor, tbl->subdevice,
3875 (tbl->name ? tbl->name : "Unknown device"));
3876#endif
3877 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 }
3879 return -1;
3880}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003881EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
3883/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003884 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003885 subsystem ID with the
3886 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003887
3888 This is important for Gateway notebooks with SB450 HDA Audio
3889 where the vendor ID of the PCI device is:
3890 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3891 and the vendor/subvendor are found only at the codec.
3892
3893 * @codec: the HDA codec
3894 * @num_configs: number of config enums
3895 * @models: array of model name strings
3896 * @tbl: configuration table, terminated by null entries
3897 *
3898 * Compares the modelname or PCI subsystem id of the current codec with the
3899 * given configuration table. If a matching entry is found, returns its
3900 * config value (supposed to be 0 or positive).
3901 *
3902 * If no entries are matching, the function returns a negative value.
3903 */
3904int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003905 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003906 const struct snd_pci_quirk *tbl)
3907{
3908 const struct snd_pci_quirk *q;
3909
3910 /* Search for codec ID */
3911 for (q = tbl; q->subvendor; q++) {
3912 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3913
3914 if (vendorid == codec->subsystem_id)
3915 break;
3916 }
3917
3918 if (!q->subvendor)
3919 return -1;
3920
3921 tbl = q;
3922
3923 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003924#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003925 char tmp[10];
3926 const char *model = NULL;
3927 if (models)
3928 model = models[tbl->value];
3929 if (!model) {
3930 sprintf(tmp, "#%d", tbl->value);
3931 model = tmp;
3932 }
3933 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3934 "for config %x:%x (%s)\n",
3935 model, tbl->subvendor, tbl->subdevice,
3936 (tbl->name ? tbl->name : "Unknown device"));
3937#endif
3938 return tbl->value;
3939 }
3940 return -1;
3941}
3942EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3943
3944/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 * snd_hda_add_new_ctls - create controls from the array
3946 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003947 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 *
3949 * This helper function creates and add new controls in the given array.
3950 * The array must be terminated with an empty entry as terminator.
3951 *
3952 * Returns 0 if successful, or a negative error code.
3953 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003954int snd_hda_add_new_ctls(struct hda_codec *codec,
3955 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003957 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
3959 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003960 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003961 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003962 if (knew->iface == -1) /* skip this codec private value */
3963 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003964 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003965 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003966 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003967 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003968 if (addr > 0)
3969 kctl->id.device = addr;
3970 if (idx > 0)
3971 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003972 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003973 if (!err)
3974 break;
3975 /* try first with another device index corresponding to
3976 * the codec addr; if it still fails (or it's the
3977 * primary codec), then try another control index
3978 */
3979 if (!addr && codec->addr)
3980 addr = codec->addr;
3981 else if (!idx && !knew->index) {
3982 idx = find_empty_mixer_ctl_idx(codec,
3983 knew->name);
3984 if (idx <= 0)
3985 return err;
3986 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003987 return err;
3988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 }
3990 return 0;
3991}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003992EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Takashi Iwaicb53c622007-08-10 17:21:45 +02003994#ifdef CONFIG_SND_HDA_POWER_SAVE
3995static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3996 unsigned int power_state);
3997
3998static void hda_power_work(struct work_struct *work)
3999{
4000 struct hda_codec *codec =
4001 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004002 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004003
Maxim Levitsky2e492462007-09-03 15:26:57 +02004004 if (!codec->power_on || codec->power_count) {
4005 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004006 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004007 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004008
4009 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004010 if (bus->ops.pm_notify)
4011 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004012}
4013
4014static void hda_keep_power_on(struct hda_codec *codec)
4015{
4016 codec->power_count++;
4017 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004018 codec->power_jiffies = jiffies;
4019}
4020
Takashi Iwaid5191e52009-11-16 14:58:17 +01004021/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004022void snd_hda_update_power_acct(struct hda_codec *codec)
4023{
4024 unsigned long delta = jiffies - codec->power_jiffies;
4025 if (codec->power_on)
4026 codec->power_on_acct += delta;
4027 else
4028 codec->power_off_acct += delta;
4029 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004030}
4031
Takashi Iwaid5191e52009-11-16 14:58:17 +01004032/**
4033 * snd_hda_power_up - Power-up the codec
4034 * @codec: HD-audio codec
4035 *
4036 * Increment the power-up counter and power up the hardware really when
4037 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004038 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004039void snd_hda_power_up(struct hda_codec *codec)
4040{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004041 struct hda_bus *bus = codec->bus;
4042
Takashi Iwaicb53c622007-08-10 17:21:45 +02004043 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004044 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004045 return;
4046
Takashi Iwaia2f63092009-11-11 09:34:25 +01004047 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004048 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004049 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004050 if (bus->ops.pm_notify)
4051 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004052 hda_call_codec_resume(codec);
4053 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004054 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004055}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004056EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004057
4058#define power_save(codec) \
4059 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004060
Takashi Iwaid5191e52009-11-16 14:58:17 +01004061/**
4062 * snd_hda_power_down - Power-down the codec
4063 * @codec: HD-audio codec
4064 *
4065 * Decrement the power-up counter and schedules the power-off work if
4066 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004067 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004068void snd_hda_power_down(struct hda_codec *codec)
4069{
4070 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004071 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004072 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004073 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004074 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004075 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004076 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004077 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004078}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004079EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004080
Takashi Iwaid5191e52009-11-16 14:58:17 +01004081/**
4082 * snd_hda_check_amp_list_power - Check the amp list and update the power
4083 * @codec: HD-audio codec
4084 * @check: the object containing an AMP list and the status
4085 * @nid: NID to check / update
4086 *
4087 * Check whether the given NID is in the amp list. If it's in the list,
4088 * check the current AMP status, and update the the power-status according
4089 * to the mute status.
4090 *
4091 * This function is supposed to be set or called from the check_power_status
4092 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004093 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004094int snd_hda_check_amp_list_power(struct hda_codec *codec,
4095 struct hda_loopback_check *check,
4096 hda_nid_t nid)
4097{
Takashi Iwai031024e2011-05-02 11:29:30 +02004098 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004099 int ch, v;
4100
4101 if (!check->amplist)
4102 return 0;
4103 for (p = check->amplist; p->nid; p++) {
4104 if (p->nid == nid)
4105 break;
4106 }
4107 if (!p->nid)
4108 return 0; /* nothing changed */
4109
4110 for (p = check->amplist; p->nid; p++) {
4111 for (ch = 0; ch < 2; ch++) {
4112 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4113 p->idx);
4114 if (!(v & HDA_AMP_MUTE) && v > 0) {
4115 if (!check->power_on) {
4116 check->power_on = 1;
4117 snd_hda_power_up(codec);
4118 }
4119 return 1;
4120 }
4121 }
4122 }
4123 if (check->power_on) {
4124 check->power_on = 0;
4125 snd_hda_power_down(codec);
4126 }
4127 return 0;
4128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004129EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004132/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004133 * Channel mode helper
4134 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004135
4136/**
4137 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4138 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004139int snd_hda_ch_mode_info(struct hda_codec *codec,
4140 struct snd_ctl_elem_info *uinfo,
4141 const struct hda_channel_mode *chmode,
4142 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004143{
4144 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4145 uinfo->count = 1;
4146 uinfo->value.enumerated.items = num_chmodes;
4147 if (uinfo->value.enumerated.item >= num_chmodes)
4148 uinfo->value.enumerated.item = num_chmodes - 1;
4149 sprintf(uinfo->value.enumerated.name, "%dch",
4150 chmode[uinfo->value.enumerated.item].channels);
4151 return 0;
4152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004153EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004154
Takashi Iwaid5191e52009-11-16 14:58:17 +01004155/**
4156 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4157 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004158int snd_hda_ch_mode_get(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_channels)
4163{
4164 int i;
4165
4166 for (i = 0; i < num_chmodes; i++) {
4167 if (max_channels == chmode[i].channels) {
4168 ucontrol->value.enumerated.item[0] = i;
4169 break;
4170 }
4171 }
4172 return 0;
4173}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004174EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004175
Takashi Iwaid5191e52009-11-16 14:58:17 +01004176/**
4177 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4178 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004179int snd_hda_ch_mode_put(struct hda_codec *codec,
4180 struct snd_ctl_elem_value *ucontrol,
4181 const struct hda_channel_mode *chmode,
4182 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004183 int *max_channelsp)
4184{
4185 unsigned int mode;
4186
4187 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004188 if (mode >= num_chmodes)
4189 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004190 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004191 return 0;
4192 /* change the current channel setting */
4193 *max_channelsp = chmode[mode].channels;
4194 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004195 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004196 return 1;
4197}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004198EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004199
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200/*
4201 * input MUX helper
4202 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004203
4204/**
4205 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4206 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004207int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4208 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
4210 unsigned int index;
4211
4212 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4213 uinfo->count = 1;
4214 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004215 if (!imux->num_items)
4216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 index = uinfo->value.enumerated.item;
4218 if (index >= imux->num_items)
4219 index = imux->num_items - 1;
4220 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4221 return 0;
4222}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004223EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Takashi Iwaid5191e52009-11-16 14:58:17 +01004225/**
4226 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4227 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004228int snd_hda_input_mux_put(struct hda_codec *codec,
4229 const struct hda_input_mux *imux,
4230 struct snd_ctl_elem_value *ucontrol,
4231 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 unsigned int *cur_val)
4233{
4234 unsigned int idx;
4235
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004236 if (!imux->num_items)
4237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 idx = ucontrol->value.enumerated.item[0];
4239 if (idx >= imux->num_items)
4240 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004241 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004243 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4244 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 *cur_val = idx;
4246 return 1;
4247}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004248EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250
4251/*
4252 * Multi-channel / digital-out PCM helper functions
4253 */
4254
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004255/* setup SPDIF output stream */
4256static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4257 unsigned int stream_tag, unsigned int format)
4258{
Stephen Warren7c9359762011-06-01 11:14:17 -06004259 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4260
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004261 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004262 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004263 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004264 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004265 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004266 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004267 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004268 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004269 for (d = codec->slave_dig_outs; *d; d++)
4270 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4271 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004272 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004273 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004274 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004275 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004276 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004277}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004278
Takashi Iwai2f728532008-09-25 16:32:41 +02004279static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4280{
4281 snd_hda_codec_cleanup_stream(codec, nid);
4282 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004283 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004284 for (d = codec->slave_dig_outs; *d; d++)
4285 snd_hda_codec_cleanup_stream(codec, *d);
4286 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004287}
4288
Takashi Iwaid5191e52009-11-16 14:58:17 +01004289/**
4290 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4291 * @bus: HD-audio bus
4292 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004293void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4294{
4295 struct hda_codec *codec;
4296
4297 if (!bus)
4298 return;
4299 list_for_each_entry(codec, &bus->codec_list, list) {
4300#ifdef CONFIG_SND_HDA_POWER_SAVE
4301 if (!codec->power_on)
4302 continue;
4303#endif
4304 if (codec->patch_ops.reboot_notify)
4305 codec->patch_ops.reboot_notify(codec);
4306 }
4307}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004308EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004309
Takashi Iwaid5191e52009-11-16 14:58:17 +01004310/**
4311 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004313int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4314 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
Ingo Molnar62932df2006-01-16 16:34:20 +01004316 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004317 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4318 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004319 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004321 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return 0;
4323}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004324EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Takashi Iwaid5191e52009-11-16 14:58:17 +01004326/**
4327 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4328 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004329int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4330 struct hda_multi_out *mout,
4331 unsigned int stream_tag,
4332 unsigned int format,
4333 struct snd_pcm_substream *substream)
4334{
4335 mutex_lock(&codec->spdif_mutex);
4336 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4337 mutex_unlock(&codec->spdif_mutex);
4338 return 0;
4339}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004340EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004341
Takashi Iwaid5191e52009-11-16 14:58:17 +01004342/**
4343 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4344 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004345int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4346 struct hda_multi_out *mout)
4347{
4348 mutex_lock(&codec->spdif_mutex);
4349 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4350 mutex_unlock(&codec->spdif_mutex);
4351 return 0;
4352}
4353EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4354
Takashi Iwaid5191e52009-11-16 14:58:17 +01004355/**
4356 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004358int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4359 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
Ingo Molnar62932df2006-01-16 16:34:20 +01004361 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004363 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 return 0;
4365}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004366EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Takashi Iwaid5191e52009-11-16 14:58:17 +01004368/**
4369 * snd_hda_multi_out_analog_open - open analog outputs
4370 *
4371 * Open analog outputs and set up the hw-constraints.
4372 * If the digital outputs can be opened as slave, open the digital
4373 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004375int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4376 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004377 struct snd_pcm_substream *substream,
4378 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379{
Takashi Iwai9a081602008-02-12 18:37:26 +01004380 struct snd_pcm_runtime *runtime = substream->runtime;
4381 runtime->hw.channels_max = mout->max_channels;
4382 if (mout->dig_out_nid) {
4383 if (!mout->analog_rates) {
4384 mout->analog_rates = hinfo->rates;
4385 mout->analog_formats = hinfo->formats;
4386 mout->analog_maxbps = hinfo->maxbps;
4387 } else {
4388 runtime->hw.rates = mout->analog_rates;
4389 runtime->hw.formats = mout->analog_formats;
4390 hinfo->maxbps = mout->analog_maxbps;
4391 }
4392 if (!mout->spdif_rates) {
4393 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4394 &mout->spdif_rates,
4395 &mout->spdif_formats,
4396 &mout->spdif_maxbps);
4397 }
4398 mutex_lock(&codec->spdif_mutex);
4399 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004400 if ((runtime->hw.rates & mout->spdif_rates) &&
4401 (runtime->hw.formats & mout->spdif_formats)) {
4402 runtime->hw.rates &= mout->spdif_rates;
4403 runtime->hw.formats &= mout->spdif_formats;
4404 if (mout->spdif_maxbps < hinfo->maxbps)
4405 hinfo->maxbps = mout->spdif_maxbps;
4406 } else {
4407 mout->share_spdif = 0;
4408 /* FIXME: need notify? */
4409 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004410 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004411 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4414 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4415}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004416EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Takashi Iwaid5191e52009-11-16 14:58:17 +01004418/**
4419 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4420 *
4421 * Set up the i/o for analog out.
4422 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004424int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4425 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 unsigned int stream_tag,
4427 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004428 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
Takashi Iwaidda14412011-05-02 11:29:30 +02004430 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004432 struct hda_spdif_out *spdif =
4433 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 int i;
4435
Ingo Molnar62932df2006-01-16 16:34:20 +01004436 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004437 if (mout->dig_out_nid && mout->share_spdif &&
4438 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004440 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4441 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004442 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004444 setup_dig_out_stream(codec, mout->dig_out_nid,
4445 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 } else {
4447 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004448 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 }
4450 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004451 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
4453 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004454 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4455 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004456 if (!mout->no_share_stream &&
4457 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004459 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4460 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004461 /* extra outputs copied from front */
4462 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004463 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004464 snd_hda_codec_setup_stream(codec,
4465 mout->extra_out_nid[i],
4466 stream_tag, 0, format);
4467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 /* surrounds */
4469 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004470 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004471 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4472 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004473 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004474 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4475 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 }
4477 return 0;
4478}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004479EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Takashi Iwaid5191e52009-11-16 14:58:17 +01004481/**
4482 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004484int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4485 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
Takashi Iwaidda14412011-05-02 11:29:30 +02004487 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 int i;
4489
4490 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004491 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004493 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004494 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4495 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004496 snd_hda_codec_cleanup_stream(codec,
4497 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004498 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004500 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 mout->dig_out_used = 0;
4502 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004503 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 return 0;
4505}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004506EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004508/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004509 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004510 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004511
Takashi Iwaidda14412011-05-02 11:29:30 +02004512static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004513{
4514 for (; *list; list++)
4515 if (*list == nid)
4516 return 1;
4517 return 0;
4518}
4519
Steve Longerbeam81937d32007-05-08 15:33:03 +02004520
4521/*
4522 * Sort an associated group of pins according to their sequence numbers.
4523 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004524static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004525 int num_pins)
4526{
4527 int i, j;
4528 short seq;
4529 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004530
Steve Longerbeam81937d32007-05-08 15:33:03 +02004531 for (i = 0; i < num_pins; i++) {
4532 for (j = i + 1; j < num_pins; j++) {
4533 if (sequences[i] > sequences[j]) {
4534 seq = sequences[i];
4535 sequences[i] = sequences[j];
4536 sequences[j] = seq;
4537 nid = pins[i];
4538 pins[i] = pins[j];
4539 pins[j] = nid;
4540 }
4541 }
4542 }
4543}
4544
4545
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004546/* add the found input-pin to the cfg->inputs[] table */
4547static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4548 int type)
4549{
4550 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4551 cfg->inputs[cfg->num_inputs].pin = nid;
4552 cfg->inputs[cfg->num_inputs].type = type;
4553 cfg->num_inputs++;
4554 }
4555}
4556
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004557/* sort inputs in the order of AUTO_PIN_* type */
4558static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4559{
4560 int i, j;
4561
4562 for (i = 0; i < cfg->num_inputs; i++) {
4563 for (j = i + 1; j < cfg->num_inputs; j++) {
4564 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4565 struct auto_pin_cfg_item tmp;
4566 tmp = cfg->inputs[i];
4567 cfg->inputs[i] = cfg->inputs[j];
4568 cfg->inputs[j] = tmp;
4569 }
4570 }
4571 }
4572}
4573
Takashi Iwai82bc9552006-03-21 11:24:42 +01004574/*
4575 * Parse all pin widgets and store the useful pin nids to cfg
4576 *
4577 * The number of line-outs or any primary output is stored in line_outs,
4578 * and the corresponding output pins are assigned to line_out_pins[],
4579 * in the order of front, rear, CLFE, side, ...
4580 *
4581 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004582 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004583 * is detected, one of speaker of HP pins is assigned as the primary
4584 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4585 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004586 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004587 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004588 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4589 * respectively.
4590 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004591int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4592 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004593 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004594{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004595 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004596 short seq, assoc_line_out, assoc_speaker;
4597 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4598 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004599 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004600 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004601
4602 memset(cfg, 0, sizeof(*cfg));
4603
Steve Longerbeam81937d32007-05-08 15:33:03 +02004604 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4605 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004606 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004607 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004608
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004609 end_nid = codec->start_nid + codec->num_nodes;
4610 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004611 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004612 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004613 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004614 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004615
4616 /* read all default configuration for pin complex */
4617 if (wid_type != AC_WID_PIN)
4618 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004619 /* ignore the given nids (e.g. pc-beep returns error) */
4620 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4621 continue;
4622
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004623 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004624 conn = get_defcfg_connect(def_conf);
4625 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004626 continue;
4627 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004628 dev = get_defcfg_device(def_conf);
4629
4630 /* workaround for buggy BIOS setups */
4631 if (dev == AC_JACK_LINE_OUT) {
4632 if (conn == AC_JACK_PORT_FIXED)
4633 dev = AC_JACK_SPEAKER;
4634 }
4635
4636 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004637 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004638 seq = get_defcfg_sequence(def_conf);
4639 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004640
4641 if (!(wid_caps & AC_WCAP_STEREO))
4642 if (!cfg->mono_out_pin)
4643 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004644 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004645 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004646 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004647 assoc_line_out = assoc;
4648 else if (assoc_line_out != assoc)
4649 continue;
4650 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4651 continue;
4652 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004653 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004654 cfg->line_outs++;
4655 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004656 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004657 seq = get_defcfg_sequence(def_conf);
4658 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004659 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004660 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004661 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004662 assoc_speaker = assoc;
4663 else if (assoc_speaker != assoc)
4664 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004665 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4666 continue;
4667 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004668 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004669 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004670 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004671 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004672 seq = get_defcfg_sequence(def_conf);
4673 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004674 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4675 continue;
4676 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004677 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004678 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004679 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004680 case AC_JACK_MIC_IN:
4681 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004682 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004683 case AC_JACK_LINE_IN:
4684 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004685 break;
4686 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004687 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004688 break;
4689 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004690 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004691 break;
4692 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004693 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004694 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4695 continue;
4696 cfg->dig_out_pins[cfg->dig_outs] = nid;
4697 cfg->dig_out_type[cfg->dig_outs] =
4698 (loc == AC_JACK_LOC_HDMI) ?
4699 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4700 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004701 break;
4702 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004703 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004704 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004705 if (loc == AC_JACK_LOC_HDMI)
4706 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4707 else
4708 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004709 break;
4710 }
4711 }
4712
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004713 /* FIX-UP:
4714 * If no line-out is defined but multiple HPs are found,
4715 * some of them might be the real line-outs.
4716 */
4717 if (!cfg->line_outs && cfg->hp_outs > 1) {
4718 int i = 0;
4719 while (i < cfg->hp_outs) {
4720 /* The real HPs should have the sequence 0x0f */
4721 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4722 i++;
4723 continue;
4724 }
4725 /* Move it to the line-out table */
4726 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4727 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4728 cfg->line_outs++;
4729 cfg->hp_outs--;
4730 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4731 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004732 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004733 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4734 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004735 memset(cfg->hp_pins + cfg->hp_outs, 0,
4736 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004737 if (!cfg->hp_outs)
4738 cfg->line_out_type = AUTO_PIN_HP_OUT;
4739
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004740 }
4741
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004742 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004743 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4744 cfg->line_outs);
4745 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4746 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004747 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4748 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004749
Steve Longerbeam81937d32007-05-08 15:33:03 +02004750 /*
4751 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4752 * as a primary output
4753 */
4754 if (!cfg->line_outs) {
4755 if (cfg->speaker_outs) {
4756 cfg->line_outs = cfg->speaker_outs;
4757 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4758 sizeof(cfg->speaker_pins));
4759 cfg->speaker_outs = 0;
4760 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4761 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4762 } else if (cfg->hp_outs) {
4763 cfg->line_outs = cfg->hp_outs;
4764 memcpy(cfg->line_out_pins, cfg->hp_pins,
4765 sizeof(cfg->hp_pins));
4766 cfg->hp_outs = 0;
4767 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4768 cfg->line_out_type = AUTO_PIN_HP_OUT;
4769 }
4770 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004771
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004772 /* Reorder the surround channels
4773 * ALSA sequence is front/surr/clfe/side
4774 * HDA sequence is:
4775 * 4-ch: front/surr => OK as it is
4776 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004777 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004778 */
4779 switch (cfg->line_outs) {
4780 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004781 case 4:
4782 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004783 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004784 cfg->line_out_pins[2] = nid;
4785 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004786 }
4787
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004788 sort_autocfg_input_pins(cfg);
4789
Takashi Iwai82bc9552006-03-21 11:24:42 +01004790 /*
4791 * debug prints of the parsed results
4792 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004793 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 +01004794 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4795 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004796 cfg->line_out_pins[4],
4797 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4798 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4799 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004800 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4801 cfg->speaker_outs, cfg->speaker_pins[0],
4802 cfg->speaker_pins[1], cfg->speaker_pins[2],
4803 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004804 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4805 cfg->hp_outs, cfg->hp_pins[0],
4806 cfg->hp_pins[1], cfg->hp_pins[2],
4807 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004808 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004809 if (cfg->dig_outs)
4810 snd_printd(" dig-out=0x%x/0x%x\n",
4811 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004812 snd_printd(" inputs:");
4813 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004814 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004815 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004816 cfg->inputs[i].pin);
4817 }
4818 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004819 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004820 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004821
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004822 return 0;
4823}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004824EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004825
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004826int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004827{
4828 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004829 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004830 if (conn == AC_JACK_PORT_NONE)
4831 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004832 /* Windows may claim the internal mic to be BOTH, too */
4833 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004834 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004835 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004836 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004837 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004838 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004839 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004840 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004841 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004842 return INPUT_PIN_ATTR_FRONT;
4843 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004844}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004845EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004846
Takashi Iwai990061c2010-09-09 22:08:44 +02004847/**
4848 * hda_get_input_pin_label - Give a label for the given input pin
4849 *
4850 * When check_location is true, the function checks the pin location
4851 * for mic and line-in pins, and set an appropriate prefix like "Front",
4852 * "Rear", "Internal".
4853 */
4854
Takashi Iwai10a20af2010-09-09 16:28:02 +02004855const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4856 int check_location)
4857{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004858 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004859 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004860 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4861 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004862 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004863
Takashi Iwai10a20af2010-09-09 16:28:02 +02004864 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004865
4866 switch (get_defcfg_device(def_conf)) {
4867 case AC_JACK_MIC_IN:
4868 if (!check_location)
4869 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004870 attr = snd_hda_get_input_pin_attr(def_conf);
4871 if (!attr)
4872 return "None";
4873 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004874 case AC_JACK_LINE_IN:
4875 if (!check_location)
4876 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004877 attr = snd_hda_get_input_pin_attr(def_conf);
4878 if (!attr)
4879 return "None";
4880 if (attr == INPUT_PIN_ATTR_DOCK)
4881 return "Dock Line";
4882 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004883 case AC_JACK_AUX:
4884 return "Aux";
4885 case AC_JACK_CD:
4886 return "CD";
4887 case AC_JACK_SPDIF_IN:
4888 return "SPDIF In";
4889 case AC_JACK_DIG_OTHER_IN:
4890 return "Digital In";
4891 default:
4892 return "Misc";
4893 }
4894}
4895EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4896
Takashi Iwaia1c98512010-09-09 21:36:27 +02004897/* Check whether the location prefix needs to be added to the label.
4898 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4899 * have to put "Front" prefix to each label. In such a case, returns false.
4900 */
4901static int check_mic_location_need(struct hda_codec *codec,
4902 const struct auto_pin_cfg *cfg,
4903 int input)
4904{
4905 unsigned int defc;
4906 int i, attr, attr2;
4907
4908 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004909 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004910 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004911 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004912 return 1;
4913
4914 attr = 0;
4915 for (i = 0; i < cfg->num_inputs; i++) {
4916 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004917 attr2 = snd_hda_get_input_pin_attr(defc);
4918 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004919 if (attr && attr != attr2)
4920 return 1; /* different locations found */
4921 attr = attr2;
4922 }
4923 }
4924 return 0;
4925}
4926
Takashi Iwai990061c2010-09-09 22:08:44 +02004927/**
4928 * hda_get_autocfg_input_label - Get a label for the given input
4929 *
4930 * Get a label for the given input pin defined by the autocfg item.
4931 * Unlike hda_get_input_pin_label(), this function checks all inputs
4932 * defined in autocfg and avoids the redundant mic/line prefix as much as
4933 * possible.
4934 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004935const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4936 const struct auto_pin_cfg *cfg,
4937 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004938{
4939 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004940 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004941
Takashi Iwai10a20af2010-09-09 16:28:02 +02004942 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4943 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4944 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004945 if (has_multiple_pins && type == AUTO_PIN_MIC)
4946 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004947 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4948 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004949}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004950EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4951
Takashi Iwai990061c2010-09-09 22:08:44 +02004952/**
4953 * snd_hda_add_imux_item - Add an item to input_mux
4954 *
4955 * When the same label is used already in the existing items, the number
4956 * suffix is appended to the label. This label index number is stored
4957 * to type_idx when non-NULL pointer is given.
4958 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004959int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4960 int index, int *type_idx)
4961{
4962 int i, label_idx = 0;
4963 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4964 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4965 return -EINVAL;
4966 }
4967 for (i = 0; i < imux->num_items; i++) {
4968 if (!strncmp(label, imux->items[i].label, strlen(label)))
4969 label_idx++;
4970 }
4971 if (type_idx)
4972 *type_idx = label_idx;
4973 if (label_idx > 0)
4974 snprintf(imux->items[imux->num_items].label,
4975 sizeof(imux->items[imux->num_items].label),
4976 "%s %d", label, label_idx);
4977 else
4978 strlcpy(imux->items[imux->num_items].label, label,
4979 sizeof(imux->items[imux->num_items].label));
4980 imux->items[imux->num_items].index = index;
4981 imux->num_items++;
4982 return 0;
4983}
4984EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004985
Takashi Iwai4a471b72005-12-07 13:56:29 +01004986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987#ifdef CONFIG_PM
4988/*
4989 * power management
4990 */
4991
4992/**
4993 * snd_hda_suspend - suspend the codecs
4994 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 *
4996 * Returns 0 if successful.
4997 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004998int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005000 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Takashi Iwai0ba21762007-04-16 11:29:14 +02005002 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02005003#ifdef CONFIG_SND_HDA_POWER_SAVE
5004 if (!codec->power_on)
5005 continue;
5006#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02005007 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 }
5009 return 0;
5010}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005011EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
5013/**
5014 * snd_hda_resume - resume the codecs
5015 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 *
5017 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005018 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005019 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005020 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 */
5022int snd_hda_resume(struct hda_bus *bus)
5023{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005024 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
Takashi Iwai0ba21762007-04-16 11:29:14 +02005026 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005027 if (snd_hda_codec_needs_resume(codec))
5028 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 return 0;
5031}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005032EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005033#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005034
5035/*
5036 * generic arrays
5037 */
5038
Takashi Iwaid5191e52009-11-16 14:58:17 +01005039/**
5040 * snd_array_new - get a new element from the given array
5041 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005042 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005043 * Get a new element from the given array. If it exceeds the
5044 * pre-allocated array size, re-allocate the array.
5045 *
5046 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005047 */
5048void *snd_array_new(struct snd_array *array)
5049{
5050 if (array->used >= array->alloced) {
5051 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005052 void *nlist;
5053 if (snd_BUG_ON(num >= 4096))
5054 return NULL;
5055 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005056 if (!nlist)
5057 return NULL;
5058 if (array->list) {
5059 memcpy(nlist, array->list,
5060 array->elem_size * array->alloced);
5061 kfree(array->list);
5062 }
5063 array->list = nlist;
5064 array->alloced = num;
5065 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005066 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005067}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005068EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005069
Takashi Iwaid5191e52009-11-16 14:58:17 +01005070/**
5071 * snd_array_free - free the given array elements
5072 * @array: the array object
5073 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005074void snd_array_free(struct snd_array *array)
5075{
5076 kfree(array->list);
5077 array->used = 0;
5078 array->alloced = 0;
5079 array->list = NULL;
5080}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005081EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005082
Takashi Iwaid5191e52009-11-16 14:58:17 +01005083/**
5084 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5085 * @pcm: PCM caps bits
5086 * @buf: the string buffer to write
5087 * @buflen: the max buffer length
5088 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005089 * used by hda_proc.c and hda_eld.c
5090 */
5091void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5092{
5093 static unsigned int rates[] = {
5094 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5095 96000, 176400, 192000, 384000
5096 };
5097 int i, j;
5098
5099 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5100 if (pcm & (1 << i))
5101 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5102
5103 buf[j] = '\0'; /* necessary when j == 0 */
5104}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005105EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005106
Takashi Iwaid5191e52009-11-16 14:58:17 +01005107/**
5108 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5109 * @pcm: PCM caps bits
5110 * @buf: the string buffer to write
5111 * @buflen: the max buffer length
5112 *
5113 * used by hda_proc.c and hda_eld.c
5114 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005115void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5116{
5117 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5118 int i, j;
5119
5120 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5121 if (pcm & (AC_SUPPCM_BITS_8 << i))
5122 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5123
5124 buf[j] = '\0'; /* necessary when j == 0 */
5125}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005126EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005127
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005128#ifdef CONFIG_SND_HDA_INPUT_JACK
5129/*
5130 * Input-jack notification support
5131 */
5132struct hda_jack_item {
5133 hda_nid_t nid;
5134 int type;
5135 struct snd_jack *jack;
5136};
5137
5138static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5139 int type)
5140{
5141 switch (type) {
5142 case SND_JACK_HEADPHONE:
5143 return "Headphone";
5144 case SND_JACK_MICROPHONE:
5145 return "Mic";
5146 case SND_JACK_LINEOUT:
5147 return "Line-out";
5148 case SND_JACK_HEADSET:
5149 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005150 case SND_JACK_VIDEOOUT:
5151 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005152 default:
5153 return "Misc";
5154 }
5155}
5156
5157static void hda_free_jack_priv(struct snd_jack *jack)
5158{
5159 struct hda_jack_item *jacks = jack->private_data;
5160 jacks->nid = 0;
5161 jacks->jack = NULL;
5162}
5163
5164int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5165 const char *name)
5166{
5167 struct hda_jack_item *jack;
5168 int err;
5169
5170 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5171 jack = snd_array_new(&codec->jacks);
5172 if (!jack)
5173 return -ENOMEM;
5174
5175 jack->nid = nid;
5176 jack->type = type;
5177 if (!name)
5178 name = get_jack_default_name(codec, nid, type);
5179 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5180 if (err < 0) {
5181 jack->nid = 0;
5182 return err;
5183 }
5184 jack->jack->private_data = jack;
5185 jack->jack->private_free = hda_free_jack_priv;
5186 return 0;
5187}
5188EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5189
5190void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5191{
5192 struct hda_jack_item *jacks = codec->jacks.list;
5193 int i;
5194
5195 if (!jacks)
5196 return;
5197
5198 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5199 unsigned int pin_ctl;
5200 unsigned int present;
5201 int type;
5202
5203 if (jacks->nid != nid)
5204 continue;
5205 present = snd_hda_jack_detect(codec, nid);
5206 type = jacks->type;
5207 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5208 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5209 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5210 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5211 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5212 }
5213 snd_jack_report(jacks->jack, present ? type : 0);
5214 }
5215}
5216EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5217
5218/* free jack instances manually when clearing/reconfiguring */
5219void snd_hda_input_jack_free(struct hda_codec *codec)
5220{
5221 if (!codec->bus->shutdown && codec->jacks.list) {
5222 struct hda_jack_item *jacks = codec->jacks.list;
5223 int i;
5224 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5225 if (jacks->jack)
5226 snd_device_free(codec->bus->card, jacks->jack);
5227 }
5228 }
5229 snd_array_free(&codec->jacks);
5230}
5231EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5232#endif /* CONFIG_SND_HDA_INPUT_JACK */
5233
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005234MODULE_DESCRIPTION("HDA codec core");
5235MODULE_LICENSE("GPL");