blob: e17e2998d333c7a692963c82c362b24cb3fedd24 [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;
246 codec_exec_verb(codec, cmd, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return res;
248}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100249EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/**
252 * snd_hda_codec_write - send a single command without waiting for response
253 * @codec: the HDA codec
254 * @nid: NID to send the command
255 * @direct: direct flag
256 * @verb: the verb to send
257 * @parm: the parameter for the verb
258 *
259 * Send a single command without waiting for response.
260 *
261 * Returns 0 if successful, or a negative error code.
262 */
263int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
264 unsigned int verb, unsigned int parm)
265{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200266 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100267 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200268 return codec_exec_verb(codec, cmd,
269 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100271EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/**
274 * snd_hda_sequence_write - sequence writes
275 * @codec: the HDA codec
276 * @seq: VERB array to send
277 *
278 * Send the commands sequentially from the given array.
279 * The array must be terminated with NID=0.
280 */
281void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
282{
283 for (; seq->nid; seq++)
284 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
285}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100286EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/**
289 * snd_hda_get_sub_nodes - get the range of sub nodes
290 * @codec: the HDA codec
291 * @nid: NID to parse
292 * @start_id: the pointer to store the start NID
293 *
294 * Parse the NID and store the start NID of its sub-nodes.
295 * Returns the number of sub-nodes.
296 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200297int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
298 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 unsigned int parm;
301
302 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200303 if (parm == -1)
304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *start_id = (parm >> 16) & 0x7fff;
306 return (int)(parm & 0x7fff);
307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100308EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200310static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
311 hda_nid_t *conn_list, int max_conns);
312static bool add_conn_list(struct snd_array *array, hda_nid_t nid);
313static int copy_conn_list(hda_nid_t nid, hda_nid_t *dst, int max_dst,
314 hda_nid_t *src, int len);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/**
317 * snd_hda_get_connections - get connection list
318 * @codec: the HDA codec
319 * @nid: NID to parse
320 * @conn_list: connection list array
321 * @max_conns: max. number of connections to store
322 *
323 * Parses the connection list of the given widget and stores the list
324 * of NIDs.
325 *
326 * Returns the number of connections, or a negative error code.
327 */
328int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200329 hda_nid_t *conn_list, int max_conns)
330{
331 struct snd_array *array = &codec->conn_lists;
Andrew Morton5b17b072011-04-14 15:06:13 -0700332 int i, len, old_used;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200333 hda_nid_t list[HDA_MAX_CONNECTIONS];
334
335 /* look up the cached results */
336 for (i = 0; i < array->used; ) {
337 hda_nid_t *p = snd_array_elem(array, i);
338 len = p[1];
339 if (nid == *p)
340 return copy_conn_list(nid, conn_list, max_conns,
341 p + 2, len);
342 i += len + 2;
343 }
344
345 len = _hda_get_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
346 if (len < 0)
347 return len;
348
349 /* add to the cache */
350 old_used = array->used;
351 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
352 goto error_add;
353 for (i = 0; i < len; i++)
354 if (!add_conn_list(array, list[i]))
355 goto error_add;
356
357 return copy_conn_list(nid, conn_list, max_conns, list, len);
358
359 error_add:
360 array->used = old_used;
361 return -ENOMEM;
362}
363EXPORT_SYMBOL_HDA(snd_hda_get_connections);
364
365static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
366 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100369 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200371 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100372 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Takashi Iwaida3cec32008-08-08 17:12:14 +0200374 if (snd_BUG_ON(!conn_list || max_conns <= 0))
375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200377 wcaps = get_wcaps(codec, nid);
378 if (!(wcaps & AC_WCAP_CONN_LIST) &&
379 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200380 snd_printk(KERN_WARNING "hda_codec: "
381 "connection list not available for 0x%x\n", nid);
382 return -EINVAL;
383 }
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
386 if (parm & AC_CLIST_LONG) {
387 /* long form */
388 shift = 16;
389 num_elems = 2;
390 } else {
391 /* short form */
392 shift = 8;
393 num_elems = 4;
394 }
395 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 mask = (1 << (shift-1)) - 1;
397
Takashi Iwai0ba21762007-04-16 11:29:14 +0200398 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0; /* no connection */
400
401 if (conn_len == 1) {
402 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200403 parm = snd_hda_codec_read(codec, nid, 0,
404 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200405 if (parm == -1 && codec->bus->rirb_error)
406 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 conn_list[0] = parm & mask;
408 return 1;
409 }
410
411 /* multi connection */
412 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100413 prev_nid = 0;
414 for (i = 0; i < conn_len; i++) {
415 int range_val;
416 hda_nid_t val, n;
417
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200418 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100419 parm = snd_hda_codec_read(codec, nid, 0,
420 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200421 if (parm == -1 && codec->bus->rirb_error)
422 return -EIO;
423 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200424 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100425 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200426 if (val == 0) {
427 snd_printk(KERN_WARNING "hda_codec: "
428 "invalid CONNECT_LIST verb %x[%i]:%x\n",
429 nid, i, parm);
430 return 0;
431 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100432 parm >>= shift;
433 if (range_val) {
434 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200435 if (!prev_nid || prev_nid >= val) {
436 snd_printk(KERN_WARNING "hda_codec: "
437 "invalid dep_range_val %x:%x\n",
438 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100439 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100441 for (n = prev_nid + 1; n <= val; n++) {
442 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200443 snd_printk(KERN_ERR "hda_codec: "
444 "Too many connections %d for NID 0x%x\n",
445 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100446 return -EINVAL;
447 }
448 conn_list[conns++] = n;
449 }
450 } else {
451 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200452 snd_printk(KERN_ERR "hda_codec: "
453 "Too many connections %d for NID 0x%x\n",
454 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100455 return -EINVAL;
456 }
457 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100459 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 return conns;
462}
463
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200464static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
465{
466 hda_nid_t *p = snd_array_new(array);
467 if (!p)
468 return false;
469 *p = nid;
470 return true;
471}
472
473static int copy_conn_list(hda_nid_t nid, hda_nid_t *dst, int max_dst,
474 hda_nid_t *src, int len)
475{
476 if (len > max_dst) {
477 snd_printk(KERN_ERR "hda_codec: "
478 "Too many connections %d for NID 0x%x\n",
479 len, nid);
480 return -EINVAL;
481 }
482 memcpy(dst, src, len * sizeof(hda_nid_t));
483 return len;
484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486/**
487 * snd_hda_queue_unsol_event - add an unsolicited event to queue
488 * @bus: the BUS
489 * @res: unsolicited event (lower 32bit of RIRB entry)
490 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
491 *
492 * Adds the given event to the queue. The events are processed in
493 * the workqueue asynchronously. Call this function in the interrupt
494 * hanlder when RIRB receives an unsolicited event.
495 *
496 * Returns 0 if successful, or a negative error code.
497 */
498int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
499{
500 struct hda_bus_unsolicited *unsol;
501 unsigned int wp;
502
Takashi Iwai0ba21762007-04-16 11:29:14 +0200503 unsol = bus->unsol;
504 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506
507 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
508 unsol->wp = wp;
509
510 wp <<= 1;
511 unsol->queue[wp] = res;
512 unsol->queue[wp + 1] = res_ex;
513
Takashi Iwai6acaed32009-01-12 10:09:24 +0100514 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 return 0;
517}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100518EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800521 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
David Howellsc4028952006-11-22 14:57:56 +0000523static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
David Howellsc4028952006-11-22 14:57:56 +0000525 struct hda_bus_unsolicited *unsol =
526 container_of(work, struct hda_bus_unsolicited, work);
527 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct hda_codec *codec;
529 unsigned int rp, caddr, res;
530
531 while (unsol->rp != unsol->wp) {
532 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
533 unsol->rp = rp;
534 rp <<= 1;
535 res = unsol->queue[rp];
536 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200537 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 continue;
539 codec = bus->caddr_tbl[caddr & 0x0f];
540 if (codec && codec->patch_ops.unsol_event)
541 codec->patch_ops.unsol_event(codec, res);
542 }
543}
544
545/*
546 * initialize unsolicited queue
547 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200548static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct hda_bus_unsolicited *unsol;
551
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100552 if (bus->unsol) /* already initialized */
553 return 0;
554
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200555 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200556 if (!unsol) {
557 snd_printk(KERN_ERR "hda_codec: "
558 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -ENOMEM;
560 }
David Howellsc4028952006-11-22 14:57:56 +0000561 INIT_WORK(&unsol->work, process_unsol_events);
562 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 bus->unsol = unsol;
564 return 0;
565}
566
567/*
568 * destructor
569 */
570static void snd_hda_codec_free(struct hda_codec *codec);
571
572static int snd_hda_bus_free(struct hda_bus *bus)
573{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200574 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Takashi Iwai0ba21762007-04-16 11:29:14 +0200576 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100578 if (bus->workq)
579 flush_workqueue(bus->workq);
580 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200582 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 snd_hda_codec_free(codec);
584 }
585 if (bus->ops.private_free)
586 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100587 if (bus->workq)
588 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 kfree(bus);
590 return 0;
591}
592
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100593static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100596 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return snd_hda_bus_free(bus);
598}
599
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200600#ifdef CONFIG_SND_HDA_HWDEP
601static int snd_hda_bus_dev_register(struct snd_device *device)
602{
603 struct hda_bus *bus = device->device_data;
604 struct hda_codec *codec;
605 list_for_each_entry(codec, &bus->codec_list, list) {
606 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100607 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200608 }
609 return 0;
610}
611#else
612#define snd_hda_bus_dev_register NULL
613#endif
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/**
616 * snd_hda_bus_new - create a HDA bus
617 * @card: the card entry
618 * @temp: the template for hda_bus information
619 * @busp: the pointer to store the created bus instance
620 *
621 * Returns 0 if successful, or a negative error code.
622 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100623int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200624 const struct hda_bus_template *temp,
625 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct hda_bus *bus;
628 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100629 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200630 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 .dev_free = snd_hda_bus_dev_free,
632 };
633
Takashi Iwaida3cec32008-08-08 17:12:14 +0200634 if (snd_BUG_ON(!temp))
635 return -EINVAL;
636 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
637 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (busp)
640 *busp = NULL;
641
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200642 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (bus == NULL) {
644 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
645 return -ENOMEM;
646 }
647
648 bus->card = card;
649 bus->private_data = temp->private_data;
650 bus->pci = temp->pci;
651 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100652 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 bus->ops = temp->ops;
654
Ingo Molnar62932df2006-01-16 16:34:20 +0100655 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200656 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 INIT_LIST_HEAD(&bus->codec_list);
658
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100659 snprintf(bus->workq_name, sizeof(bus->workq_name),
660 "hd-audio%d", card->number);
661 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100662 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100663 snd_printk(KERN_ERR "cannot create workqueue %s\n",
664 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100665 kfree(bus);
666 return -ENOMEM;
667 }
668
Takashi Iwai0ba21762007-04-16 11:29:14 +0200669 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
670 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 snd_hda_bus_free(bus);
672 return err;
673 }
674 if (busp)
675 *busp = bus;
676 return 0;
677}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100678EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Takashi Iwai82467612007-07-27 19:15:54 +0200680#ifdef CONFIG_SND_HDA_GENERIC
681#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200682 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200683#else
684#define is_generic_config(codec) 0
685#endif
686
Takashi Iwai645f10c2008-11-28 15:07:37 +0100687#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100688#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
689#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100690#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100691#endif
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/*
694 * find a matching codec preset
695 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200696static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200697find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100699 struct hda_codec_preset_list *tbl;
700 const struct hda_codec_preset *preset;
701 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Takashi Iwai82467612007-07-27 19:15:54 +0200703 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100704 return NULL; /* use the generic parser */
705
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100706 again:
707 mutex_lock(&preset_mutex);
708 list_for_each_entry(tbl, &hda_preset_tables, list) {
709 if (!try_module_get(tbl->owner)) {
710 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
711 continue;
712 }
713 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100715 if (preset->afg && preset->afg != codec->afg)
716 continue;
717 if (preset->mfg && preset->mfg != codec->mfg)
718 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200719 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200721 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200722 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100723 preset->rev == codec->revision_id)) {
724 mutex_unlock(&preset_mutex);
725 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100729 module_put(tbl->owner);
730 }
731 mutex_unlock(&preset_mutex);
732
733 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
734 char name[32];
735 if (!mod_requested)
736 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
737 codec->vendor_id);
738 else
739 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
740 (codec->vendor_id >> 16) & 0xffff);
741 request_module(name);
742 mod_requested++;
743 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 return NULL;
746}
747
748/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200749 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200751static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 const struct hda_vendor_id *c;
754 const char *vendor = NULL;
755 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200756 char tmp[16];
757
758 if (codec->vendor_name)
759 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 for (c = hda_vendor_ids; c->id; c++) {
762 if (c->id == vendor_id) {
763 vendor = c->name;
764 break;
765 }
766 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200767 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 sprintf(tmp, "Generic %04x", vendor_id);
769 vendor = tmp;
770 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200771 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
772 if (!codec->vendor_name)
773 return -ENOMEM;
774
775 get_chip_name:
776 if (codec->chip_name)
777 return 0;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200780 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
781 else {
782 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
783 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
784 }
785 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200786 return -ENOMEM;
787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200791 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100793static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200795 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 hda_nid_t nid;
797
798 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
799 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200800 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200801 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200802 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200803 case AC_GRP_AUDIO_FUNCTION:
804 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200805 codec->afg_function_id = function_id & 0xff;
806 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200807 break;
808 case AC_GRP_MODEM_FUNCTION:
809 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200810 codec->mfg_function_id = function_id & 0xff;
811 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200812 break;
813 default:
814 break;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100820 * read widget caps for each widget and store in cache
821 */
822static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
823{
824 int i;
825 hda_nid_t nid;
826
827 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
828 &codec->start_nid);
829 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200830 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100831 return -ENOMEM;
832 nid = codec->start_nid;
833 for (i = 0; i < codec->num_nodes; i++, nid++)
834 codec->wcaps[i] = snd_hda_param_read(codec, nid,
835 AC_PAR_AUDIO_WIDGET_CAP);
836 return 0;
837}
838
Takashi Iwai3be14142009-02-20 14:11:16 +0100839/* read all pin default configurations and save codec->init_pins */
840static int read_pin_defaults(struct hda_codec *codec)
841{
842 int i;
843 hda_nid_t nid = codec->start_nid;
844
845 for (i = 0; i < codec->num_nodes; i++, nid++) {
846 struct hda_pincfg *pin;
847 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200848 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100849 if (wid_type != AC_WID_PIN)
850 continue;
851 pin = snd_array_new(&codec->init_pins);
852 if (!pin)
853 return -ENOMEM;
854 pin->nid = nid;
855 pin->cfg = snd_hda_codec_read(codec, nid, 0,
856 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200857 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
858 AC_VERB_GET_PIN_WIDGET_CONTROL,
859 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100860 }
861 return 0;
862}
863
864/* look up the given pin config list and return the item matching with NID */
865static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
866 struct snd_array *array,
867 hda_nid_t nid)
868{
869 int i;
870 for (i = 0; i < array->used; i++) {
871 struct hda_pincfg *pin = snd_array_elem(array, i);
872 if (pin->nid == nid)
873 return pin;
874 }
875 return NULL;
876}
877
878/* write a config value for the given NID */
879static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
880 unsigned int cfg)
881{
882 int i;
883 for (i = 0; i < 4; i++) {
884 snd_hda_codec_write(codec, nid, 0,
885 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
886 cfg & 0xff);
887 cfg >>= 8;
888 }
889}
890
891/* set the current pin config value for the given NID.
892 * the value is cached, and read via snd_hda_codec_get_pincfg()
893 */
894int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
895 hda_nid_t nid, unsigned int cfg)
896{
897 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100898 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100899
Takashi Iwaib82855a2009-12-27 11:24:56 +0100900 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
901 return -EINVAL;
902
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100903 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100904 pin = look_up_pincfg(codec, list, nid);
905 if (!pin) {
906 pin = snd_array_new(list);
907 if (!pin)
908 return -ENOMEM;
909 pin->nid = nid;
910 }
911 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100912
913 /* change only when needed; e.g. if the pincfg is already present
914 * in user_pins[], don't write it
915 */
916 cfg = snd_hda_codec_get_pincfg(codec, nid);
917 if (oldcfg != cfg)
918 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100919 return 0;
920}
921
Takashi Iwaid5191e52009-11-16 14:58:17 +0100922/**
923 * snd_hda_codec_set_pincfg - Override a pin default configuration
924 * @codec: the HDA codec
925 * @nid: NID to set the pin config
926 * @cfg: the pin default config value
927 *
928 * Override a pin default configuration value in the cache.
929 * This value can be read by snd_hda_codec_get_pincfg() in a higher
930 * priority than the real hardware value.
931 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100932int snd_hda_codec_set_pincfg(struct hda_codec *codec,
933 hda_nid_t nid, unsigned int cfg)
934{
Takashi Iwai346ff702009-02-23 09:42:57 +0100935 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100936}
937EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
938
Takashi Iwaid5191e52009-11-16 14:58:17 +0100939/**
940 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
941 * @codec: the HDA codec
942 * @nid: NID to get the pin config
943 *
944 * Get the current pin config value of the given pin NID.
945 * If the pincfg value is cached or overridden via sysfs or driver,
946 * returns the cached value.
947 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100948unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
949{
950 struct hda_pincfg *pin;
951
Takashi Iwai3be14142009-02-20 14:11:16 +0100952#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100953 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100954 if (pin)
955 return pin->cfg;
956#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100957 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
958 if (pin)
959 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100960 pin = look_up_pincfg(codec, &codec->init_pins, nid);
961 if (pin)
962 return pin->cfg;
963 return 0;
964}
965EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
966
967/* restore all current pin configs */
968static void restore_pincfgs(struct hda_codec *codec)
969{
970 int i;
971 for (i = 0; i < codec->init_pins.used; i++) {
972 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
973 set_pincfg(codec, pin->nid,
974 snd_hda_codec_get_pincfg(codec, pin->nid));
975 }
976}
Takashi Iwai54d17402005-11-21 16:33:22 +0100977
Takashi Iwai92ee6162009-12-27 11:18:59 +0100978/**
979 * snd_hda_shutup_pins - Shut up all pins
980 * @codec: the HDA codec
981 *
982 * Clear all pin controls to shup up before suspend for avoiding click noise.
983 * The controls aren't cached so that they can be resumed properly.
984 */
985void snd_hda_shutup_pins(struct hda_codec *codec)
986{
987 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200988 /* don't shut up pins when unloading the driver; otherwise it breaks
989 * the default pin setup at the next load of the driver
990 */
991 if (codec->bus->shutdown)
992 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100993 for (i = 0; i < codec->init_pins.used; i++) {
994 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
995 /* use read here for syncing after issuing each verb */
996 snd_hda_codec_read(codec, pin->nid, 0,
997 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
998 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200999 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001000}
1001EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1002
Mike Waychison1c7276c2011-04-20 12:04:36 -07001003#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001004/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1005static void restore_shutup_pins(struct hda_codec *codec)
1006{
1007 int i;
1008 if (!codec->pins_shutup)
1009 return;
1010 if (codec->bus->shutdown)
1011 return;
1012 for (i = 0; i < codec->init_pins.used; i++) {
1013 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1014 snd_hda_codec_write(codec, pin->nid, 0,
1015 AC_VERB_SET_PIN_WIDGET_CONTROL,
1016 pin->ctrl);
1017 }
1018 codec->pins_shutup = 0;
1019}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001020#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001021
Takashi Iwai01751f52007-08-10 16:59:39 +02001022static void init_hda_cache(struct hda_cache_rec *cache,
1023 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001024static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001025
Takashi Iwai3be14142009-02-20 14:11:16 +01001026/* restore the initial pin cfgs and release all pincfg lists */
1027static void restore_init_pincfgs(struct hda_codec *codec)
1028{
Takashi Iwai346ff702009-02-23 09:42:57 +01001029 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001030 * so that only the values in init_pins are restored
1031 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001032 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001033#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001034 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001035#endif
1036 restore_pincfgs(codec);
1037 snd_array_free(&codec->init_pins);
1038}
1039
Takashi Iwai54d17402005-11-21 16:33:22 +01001040/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001041 * audio-converter setup caches
1042 */
1043struct hda_cvt_setup {
1044 hda_nid_t nid;
1045 u8 stream_tag;
1046 u8 channel_id;
1047 u16 format_id;
1048 unsigned char active; /* cvt is currently used */
1049 unsigned char dirty; /* setups should be cleared */
1050};
1051
1052/* get or create a cache entry for the given audio converter NID */
1053static struct hda_cvt_setup *
1054get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1055{
1056 struct hda_cvt_setup *p;
1057 int i;
1058
1059 for (i = 0; i < codec->cvt_setups.used; i++) {
1060 p = snd_array_elem(&codec->cvt_setups, i);
1061 if (p->nid == nid)
1062 return p;
1063 }
1064 p = snd_array_new(&codec->cvt_setups);
1065 if (p)
1066 p->nid = nid;
1067 return p;
1068}
1069
1070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * codec destructor
1072 */
1073static void snd_hda_codec_free(struct hda_codec *codec)
1074{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001075 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001077 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001078#ifdef CONFIG_SND_HDA_POWER_SAVE
1079 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001080 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001081#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001083 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001084 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001085 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001086 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 codec->bus->caddr_tbl[codec->addr] = NULL;
1088 if (codec->patch_ops.free)
1089 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001090 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001091 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001092 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001093 kfree(codec->vendor_name);
1094 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001095 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001096 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 kfree(codec);
1098}
1099
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001100static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1101 unsigned int power_state);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/**
1104 * snd_hda_codec_new - create a HDA codec
1105 * @bus: the bus to assign
1106 * @codec_addr: the codec address
1107 * @codecp: the pointer to store the generated codec
1108 *
1109 * Returns 0 if successful, or a negative error code.
1110 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001111int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1112 unsigned int codec_addr,
1113 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001116 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int err;
1118
Takashi Iwaida3cec32008-08-08 17:12:14 +02001119 if (snd_BUG_ON(!bus))
1120 return -EINVAL;
1121 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1122 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001125 snd_printk(KERN_ERR "hda_codec: "
1126 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return -EBUSY;
1128 }
1129
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001130 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (codec == NULL) {
1132 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1133 return -ENOMEM;
1134 }
1135
1136 codec->bus = bus;
1137 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001138 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001139 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001140 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001141 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001142 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1143 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001144 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001145 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001146 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001147 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001148 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001149 if (codec->bus->modelname) {
1150 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1151 if (!codec->modelname) {
1152 snd_hda_codec_free(codec);
1153 return -ENODEV;
1154 }
1155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Takashi Iwaicb53c622007-08-10 17:21:45 +02001157#ifdef CONFIG_SND_HDA_POWER_SAVE
1158 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1159 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1160 * the caller has to power down appropriatley after initialization
1161 * phase.
1162 */
1163 hda_keep_power_on(codec);
1164#endif
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 list_add_tail(&codec->list, &bus->codec_list);
1167 bus->caddr_tbl[codec_addr] = codec;
1168
Takashi Iwai0ba21762007-04-16 11:29:14 +02001169 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1170 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001171 if (codec->vendor_id == -1)
1172 /* read again, hopefully the access method was corrected
1173 * in the last read...
1174 */
1175 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1176 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001177 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1178 AC_PAR_SUBSYSTEM_ID);
1179 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1180 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001182 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001183 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001184 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001185 err = -ENODEV;
1186 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
Takashi Iwai3be14142009-02-20 14:11:16 +01001189 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1190 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001191 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001192 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001193 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001194 err = read_pin_defaults(codec);
1195 if (err < 0)
1196 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001197
Takashi Iwai0ba21762007-04-16 11:29:14 +02001198 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001199 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001200 codec->subsystem_id =
1201 snd_hda_codec_read(codec, nid, 0,
1202 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001203 }
1204
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001205 /* power-up all before initialization */
1206 hda_set_power_state(codec,
1207 codec->afg ? codec->afg : codec->mfg,
1208 AC_PWRST_D0);
1209
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001210 snd_hda_codec_proc_new(codec);
1211
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001212 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001213
1214 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1215 codec->subsystem_id, codec->revision_id);
1216 snd_component_add(codec->bus->card, component);
1217
1218 if (codecp)
1219 *codecp = codec;
1220 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001221
1222 error:
1223 snd_hda_codec_free(codec);
1224 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001225}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001226EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001227
Takashi Iwaid5191e52009-11-16 14:58:17 +01001228/**
1229 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1230 * @codec: the HDA codec
1231 *
1232 * Start parsing of the given codec tree and (re-)initialize the whole
1233 * patch instance.
1234 *
1235 * Returns 0 if successful or a negative error code.
1236 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001237int snd_hda_codec_configure(struct hda_codec *codec)
1238{
1239 int err;
1240
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001241 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001242 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001243 err = get_codec_name(codec);
1244 if (err < 0)
1245 return err;
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Takashi Iwai82467612007-07-27 19:15:54 +02001248 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001250 goto patched;
1251 }
Takashi Iwai82467612007-07-27 19:15:54 +02001252 if (codec->preset && codec->preset->patch) {
1253 err = codec->preset->patch(codec);
1254 goto patched;
1255 }
1256
1257 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001258 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001259 if (err < 0)
1260 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001261
1262 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001263 if (!err && codec->patch_ops.unsol_event)
1264 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001265 /* audio codec should override the mixer name */
1266 if (!err && (codec->afg || !*codec->bus->card->mixername))
1267 snprintf(codec->bus->card->mixername,
1268 sizeof(codec->bus->card->mixername),
1269 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001272EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274/**
1275 * snd_hda_codec_setup_stream - set up the codec for streaming
1276 * @codec: the CODEC to set up
1277 * @nid: the NID to set up
1278 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1279 * @channel_id: channel id to pass, zero based.
1280 * @format: stream format.
1281 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001282void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1283 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int channel_id, int format)
1285{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001286 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001287 struct hda_cvt_setup *p;
1288 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001289 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001290 int i;
1291
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001293 return;
1294
Takashi Iwai0ba21762007-04-16 11:29:14 +02001295 snd_printdd("hda_codec_setup_stream: "
1296 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001298 p = get_hda_cvt_setup(codec, nid);
1299 if (!p)
1300 return;
1301 /* update the stream-id if changed */
1302 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1303 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1304 newval = (stream_tag << 4) | channel_id;
1305 if (oldval != newval)
1306 snd_hda_codec_write(codec, nid, 0,
1307 AC_VERB_SET_CHANNEL_STREAMID,
1308 newval);
1309 p->stream_tag = stream_tag;
1310 p->channel_id = channel_id;
1311 }
1312 /* update the format-id if changed */
1313 if (p->format_id != format) {
1314 oldval = snd_hda_codec_read(codec, nid, 0,
1315 AC_VERB_GET_STREAM_FORMAT, 0);
1316 if (oldval != format) {
1317 msleep(1);
1318 snd_hda_codec_write(codec, nid, 0,
1319 AC_VERB_SET_STREAM_FORMAT,
1320 format);
1321 }
1322 p->format_id = format;
1323 }
1324 p->active = 1;
1325 p->dirty = 0;
1326
1327 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001328 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001329 list_for_each_entry(c, &codec->bus->codec_list, list) {
1330 for (i = 0; i < c->cvt_setups.used; i++) {
1331 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001332 if (!p->active && p->stream_tag == stream_tag &&
1333 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001334 p->dirty = 1;
1335 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001338EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Takashi Iwaif0cea792010-08-13 11:56:53 +02001340static void really_cleanup_stream(struct hda_codec *codec,
1341 struct hda_cvt_setup *q);
1342
Takashi Iwaid5191e52009-11-16 14:58:17 +01001343/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001344 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001345 * @codec: the CODEC to clean up
1346 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001347 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001348 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001349void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1350 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001351{
Takashi Iwaieb541332010-08-06 13:48:11 +02001352 struct hda_cvt_setup *p;
1353
Takashi Iwai888afa12008-03-18 09:57:50 +01001354 if (!nid)
1355 return;
1356
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001357 if (codec->no_sticky_stream)
1358 do_now = 1;
1359
Takashi Iwai888afa12008-03-18 09:57:50 +01001360 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001361 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001362 if (p) {
1363 /* here we just clear the active flag when do_now isn't set;
1364 * actual clean-ups will be done later in
1365 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1366 */
1367 if (do_now)
1368 really_cleanup_stream(codec, p);
1369 else
1370 p->active = 0;
1371 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001372}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001373EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001374
Takashi Iwaieb541332010-08-06 13:48:11 +02001375static void really_cleanup_stream(struct hda_codec *codec,
1376 struct hda_cvt_setup *q)
1377{
1378 hda_nid_t nid = q->nid;
1379 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1380 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1381 memset(q, 0, sizeof(*q));
1382 q->nid = nid;
1383}
1384
1385/* clean up the all conflicting obsolete streams */
1386static void purify_inactive_streams(struct hda_codec *codec)
1387{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001388 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001389 int i;
1390
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001391 list_for_each_entry(c, &codec->bus->codec_list, list) {
1392 for (i = 0; i < c->cvt_setups.used; i++) {
1393 struct hda_cvt_setup *p;
1394 p = snd_array_elem(&c->cvt_setups, i);
1395 if (p->dirty)
1396 really_cleanup_stream(c, p);
1397 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001398 }
1399}
1400
Mike Waychison1c7276c2011-04-20 12:04:36 -07001401#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaieb541332010-08-06 13:48:11 +02001402/* clean up all streams; called from suspend */
1403static void hda_cleanup_all_streams(struct hda_codec *codec)
1404{
1405 int i;
1406
1407 for (i = 0; i < codec->cvt_setups.used; i++) {
1408 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1409 if (p->stream_tag)
1410 really_cleanup_stream(codec, p);
1411 }
1412}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001413#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415/*
1416 * amp access functions
1417 */
1418
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001419/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001420#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001421#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001422#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1423#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001425#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001428static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001429 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Takashi Iwai01751f52007-08-10 16:59:39 +02001431 memset(cache, 0, sizeof(*cache));
1432 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001433 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001434}
1435
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001436static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001437{
Takashi Iwai603c4012008-07-30 15:01:44 +02001438 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
1441/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001442static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Takashi Iwai01751f52007-08-10 16:59:39 +02001444 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1445 u16 cur = cache->hash[idx];
1446 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001449 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (info->key == key)
1451 return info;
1452 cur = info->next;
1453 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001454 return NULL;
1455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001457/* query the hash. allocate an entry if not found. */
1458static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1459 u32 key)
1460{
1461 struct hda_cache_head *info = get_hash(cache, key);
1462 if (!info) {
1463 u16 idx, cur;
1464 /* add a new hash entry */
1465 info = snd_array_new(&cache->buf);
1466 if (!info)
1467 return NULL;
1468 cur = snd_array_index(&cache->buf, info);
1469 info->key = key;
1470 info->val = 0;
1471 idx = key % (u16)ARRAY_SIZE(cache->hash);
1472 info->next = cache->hash[idx];
1473 cache->hash[idx] = cur;
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return info;
1476}
1477
Takashi Iwai01751f52007-08-10 16:59:39 +02001478/* query and allocate an amp hash entry */
1479static inline struct hda_amp_info *
1480get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1481{
1482 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1483}
1484
Takashi Iwaid5191e52009-11-16 14:58:17 +01001485/**
1486 * query_amp_caps - query AMP capabilities
1487 * @codec: the HD-auio codec
1488 * @nid: the NID to query
1489 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1490 *
1491 * Query AMP capabilities for the given widget and direction.
1492 * Returns the obtained capability bits.
1493 *
1494 * When cap bits have been already read, this doesn't read again but
1495 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001497u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001499 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Takashi Iwai0ba21762007-04-16 11:29:14 +02001501 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1502 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001504 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001505 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001507 info->amp_caps = snd_hda_param_read(codec, nid,
1508 direction == HDA_OUTPUT ?
1509 AC_PAR_AMP_OUT_CAP :
1510 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001511 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001512 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 return info->amp_caps;
1515}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001516EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Takashi Iwaid5191e52009-11-16 14:58:17 +01001518/**
1519 * snd_hda_override_amp_caps - Override the AMP capabilities
1520 * @codec: the CODEC to clean up
1521 * @nid: the NID to clean up
1522 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1523 * @caps: the capability bits to set
1524 *
1525 * Override the cached AMP caps bits value by the given one.
1526 * This function is useful if the driver needs to adjust the AMP ranges,
1527 * e.g. limit to 0dB, etc.
1528 *
1529 * Returns zero if successful or a negative error code.
1530 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001531int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1532 unsigned int caps)
1533{
1534 struct hda_amp_info *info;
1535
1536 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1537 if (!info)
1538 return -EINVAL;
1539 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001540 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001541 return 0;
1542}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001543EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001544
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001545static unsigned int
1546query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1547 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001548{
1549 struct hda_amp_info *info;
1550
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001551 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001552 if (!info)
1553 return 0;
1554 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001555 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001556 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001557 }
1558 return info->amp_caps;
1559}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001560
1561static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1562{
1563 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1564}
1565
Takashi Iwaid5191e52009-11-16 14:58:17 +01001566/**
1567 * snd_hda_query_pin_caps - Query PIN capabilities
1568 * @codec: the HD-auio codec
1569 * @nid: the NID to query
1570 *
1571 * Query PIN capabilities for the given widget.
1572 * Returns the obtained capability bits.
1573 *
1574 * When cap bits have been already read, this doesn't read again but
1575 * returns the cached value.
1576 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001577u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1578{
1579 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1580 read_pin_cap);
1581}
Takashi Iwai1327a322009-03-23 13:07:47 +01001582EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1583
Wu Fengguang864f92b2009-11-18 12:38:02 +08001584/**
1585 * snd_hda_pin_sense - execute pin sense measurement
1586 * @codec: the CODEC to sense
1587 * @nid: the pin NID to sense
1588 *
1589 * Execute necessary pin sense measurement and return its Presence Detect,
1590 * Impedance, ELD Valid etc. status bits.
1591 */
1592u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1593{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001594 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001595
Takashi Iwai729d55b2009-12-25 22:49:01 +01001596 if (!codec->no_trigger_sense) {
1597 pincap = snd_hda_query_pin_caps(codec, nid);
1598 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001599 snd_hda_codec_read(codec, nid, 0,
1600 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001601 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001602 return snd_hda_codec_read(codec, nid, 0,
1603 AC_VERB_GET_PIN_SENSE, 0);
1604}
1605EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1606
1607/**
1608 * snd_hda_jack_detect - query pin Presence Detect status
1609 * @codec: the CODEC to sense
1610 * @nid: the pin NID to sense
1611 *
1612 * Query and return the pin's Presence Detect status.
1613 */
1614int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1615{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001616 u32 sense = snd_hda_pin_sense(codec, nid);
1617 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001618}
1619EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/*
1622 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001623 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001625static unsigned int get_vol_mute(struct hda_codec *codec,
1626 struct hda_amp_info *info, hda_nid_t nid,
1627 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 u32 val, parm;
1630
Takashi Iwai01751f52007-08-10 16:59:39 +02001631 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001632 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1635 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1636 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001637 val = snd_hda_codec_read(codec, nid, 0,
1638 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001640 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001641 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
1644/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001645 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001647static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001648 hda_nid_t nid, int ch, int direction, int index,
1649 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 u32 parm;
1652
1653 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1654 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1655 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1656 parm |= val;
1657 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001658 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
Takashi Iwaid5191e52009-11-16 14:58:17 +01001661/**
1662 * snd_hda_codec_amp_read - Read AMP value
1663 * @codec: HD-audio codec
1664 * @nid: NID to read the AMP value
1665 * @ch: channel (left=0 or right=1)
1666 * @direction: #HDA_INPUT or #HDA_OUTPUT
1667 * @index: the index value (only for input direction)
1668 *
1669 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
Takashi Iwai834be882006-03-01 14:16:17 +01001671int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1672 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001674 struct hda_amp_info *info;
1675 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1676 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001678 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001680EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Takashi Iwaid5191e52009-11-16 14:58:17 +01001682/**
1683 * snd_hda_codec_amp_update - update the AMP value
1684 * @codec: HD-audio codec
1685 * @nid: NID to read the AMP value
1686 * @ch: channel (left=0 or right=1)
1687 * @direction: #HDA_INPUT or #HDA_OUTPUT
1688 * @idx: the index value (only for input direction)
1689 * @mask: bit mask to set
1690 * @val: the bits value to set
1691 *
1692 * Update the AMP value with a bit mask.
1693 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001694 */
Takashi Iwai834be882006-03-01 14:16:17 +01001695int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1696 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001698 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001699
Takashi Iwai0ba21762007-04-16 11:29:14 +02001700 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1701 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001703 if (snd_BUG_ON(mask & ~0xff))
1704 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001705 val &= mask;
1706 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001707 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001709 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return 1;
1711}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001712EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Takashi Iwaid5191e52009-11-16 14:58:17 +01001714/**
1715 * snd_hda_codec_amp_stereo - update the AMP stereo values
1716 * @codec: HD-audio codec
1717 * @nid: NID to read the AMP value
1718 * @direction: #HDA_INPUT or #HDA_OUTPUT
1719 * @idx: the index value (only for input direction)
1720 * @mask: bit mask to set
1721 * @val: the bits value to set
1722 *
1723 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1724 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001725 */
1726int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1727 int direction, int idx, int mask, int val)
1728{
1729 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001730
1731 if (snd_BUG_ON(mask & ~0xff))
1732 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001733 for (ch = 0; ch < 2; ch++)
1734 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1735 idx, mask, val);
1736 return ret;
1737}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001738EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001739
Takashi Iwaicb53c622007-08-10 17:21:45 +02001740#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001741/**
1742 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1743 * @codec: HD-audio codec
1744 *
1745 * Resume the all amp commands from the cache.
1746 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001747void snd_hda_codec_resume_amp(struct hda_codec *codec)
1748{
Takashi Iwai603c4012008-07-30 15:01:44 +02001749 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001750 int i;
1751
Takashi Iwai603c4012008-07-30 15:01:44 +02001752 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001753 u32 key = buffer->head.key;
1754 hda_nid_t nid;
1755 unsigned int idx, dir, ch;
1756 if (!key)
1757 continue;
1758 nid = key & 0xff;
1759 idx = (key >> 16) & 0xff;
1760 dir = (key >> 24) & 0xff;
1761 for (ch = 0; ch < 2; ch++) {
1762 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1763 continue;
1764 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1765 buffer->vol[ch]);
1766 }
1767 }
1768}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001769EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001770#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001772static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1773 unsigned int ofs)
1774{
1775 u32 caps = query_amp_caps(codec, nid, dir);
1776 /* get num steps */
1777 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1778 if (ofs < caps)
1779 caps -= ofs;
1780 return caps;
1781}
1782
Takashi Iwaid5191e52009-11-16 14:58:17 +01001783/**
1784 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1785 *
1786 * The control element is supposed to have the private_value field
1787 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1788 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001789int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1790 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1793 u16 nid = get_amp_nid(kcontrol);
1794 u8 chs = get_amp_channels(kcontrol);
1795 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001796 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001798 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1799 uinfo->count = chs == 3 ? 2 : 1;
1800 uinfo->value.integer.min = 0;
1801 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1802 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001803 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001804 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1805 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return -EINVAL;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return 0;
1809}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001810EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001812
1813static inline unsigned int
1814read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1815 int ch, int dir, int idx, unsigned int ofs)
1816{
1817 unsigned int val;
1818 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1819 val &= HDA_AMP_VOLMASK;
1820 if (val >= ofs)
1821 val -= ofs;
1822 else
1823 val = 0;
1824 return val;
1825}
1826
1827static inline int
1828update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1829 int ch, int dir, int idx, unsigned int ofs,
1830 unsigned int val)
1831{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001832 unsigned int maxval;
1833
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001834 if (val > 0)
1835 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001836 /* ofs = 0: raw max value */
1837 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001838 if (val > maxval)
1839 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001840 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1841 HDA_AMP_VOLMASK, val);
1842}
1843
Takashi Iwaid5191e52009-11-16 14:58:17 +01001844/**
1845 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1846 *
1847 * The control element is supposed to have the private_value field
1848 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1849 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001850int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1851 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1854 hda_nid_t nid = get_amp_nid(kcontrol);
1855 int chs = get_amp_channels(kcontrol);
1856 int dir = get_amp_direction(kcontrol);
1857 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001858 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 long *valp = ucontrol->value.integer.value;
1860
1861 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001862 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001864 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return 0;
1866}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001867EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Takashi Iwaid5191e52009-11-16 14:58:17 +01001869/**
1870 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1871 *
1872 * The control element is supposed to have the private_value field
1873 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1874 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001875int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1876 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
1878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1879 hda_nid_t nid = get_amp_nid(kcontrol);
1880 int chs = get_amp_channels(kcontrol);
1881 int dir = get_amp_direction(kcontrol);
1882 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001883 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 long *valp = ucontrol->value.integer.value;
1885 int change = 0;
1886
Takashi Iwaicb53c622007-08-10 17:21:45 +02001887 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001888 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001889 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001890 valp++;
1891 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001892 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001893 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001894 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return change;
1896}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001897EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Takashi Iwaid5191e52009-11-16 14:58:17 +01001899/**
1900 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1901 *
1902 * The control element is supposed to have the private_value field
1903 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1904 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001905int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1906 unsigned int size, unsigned int __user *_tlv)
1907{
1908 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1909 hda_nid_t nid = get_amp_nid(kcontrol);
1910 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001911 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001912 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001913 u32 caps, val1, val2;
1914
1915 if (size < 4 * sizeof(unsigned int))
1916 return -ENOMEM;
1917 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001918 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1919 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001920 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001921 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001922 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001923 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02001924 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001925 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1926 return -EFAULT;
1927 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1928 return -EFAULT;
1929 if (put_user(val1, _tlv + 2))
1930 return -EFAULT;
1931 if (put_user(val2, _tlv + 3))
1932 return -EFAULT;
1933 return 0;
1934}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001935EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001936
Takashi Iwaid5191e52009-11-16 14:58:17 +01001937/**
1938 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1939 * @codec: HD-audio codec
1940 * @nid: NID of a reference widget
1941 * @dir: #HDA_INPUT or #HDA_OUTPUT
1942 * @tlv: TLV data to be stored, at least 4 elements
1943 *
1944 * Set (static) TLV data for a virtual master volume using the AMP caps
1945 * obtained from the reference NID.
1946 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001947 */
1948void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1949 unsigned int *tlv)
1950{
1951 u32 caps;
1952 int nums, step;
1953
1954 caps = query_amp_caps(codec, nid, dir);
1955 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1956 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1957 step = (step + 1) * 25;
1958 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1959 tlv[1] = 2 * sizeof(unsigned int);
1960 tlv[2] = -nums * step;
1961 tlv[3] = step;
1962}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001963EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001964
1965/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001966static struct snd_kcontrol *
1967_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1968 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001969{
1970 struct snd_ctl_elem_id id;
1971 memset(&id, 0, sizeof(id));
1972 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001973 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001974 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1975 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001976 strcpy(id.name, name);
1977 return snd_ctl_find_id(codec->bus->card, &id);
1978}
1979
Takashi Iwaid5191e52009-11-16 14:58:17 +01001980/**
1981 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1982 * @codec: HD-audio codec
1983 * @name: ctl id name string
1984 *
1985 * Get the control element with the given id string and IFACE_MIXER.
1986 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001987struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1988 const char *name)
1989{
1990 return _snd_hda_find_mixer_ctl(codec, name, 0);
1991}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001992EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001993
Takashi Iwai1afe2062010-12-23 10:17:52 +01001994static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
1995{
1996 int idx;
1997 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
1998 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
1999 return idx;
2000 }
2001 return -EBUSY;
2002}
2003
Takashi Iwaid5191e52009-11-16 14:58:17 +01002004/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002005 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002006 * @codec: HD-audio codec
2007 * @nid: corresponding NID (optional)
2008 * @kctl: the control element to assign
2009 *
2010 * Add the given control element to an array inside the codec instance.
2011 * All control elements belonging to a codec are supposed to be added
2012 * by this function so that a proper clean-up works at the free or
2013 * reconfiguration time.
2014 *
2015 * If non-zero @nid is passed, the NID is assigned to the control element.
2016 * The assignment is shown in the codec proc file.
2017 *
2018 * snd_hda_ctl_add() checks the control subdev id field whether
2019 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002020 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2021 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002022 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002023int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2024 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002025{
2026 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002027 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002028 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002029
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002030 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002031 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002032 if (nid == 0)
2033 nid = get_amp_nid_(kctl->private_value);
2034 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002035 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2036 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002037 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002038 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002039 err = snd_ctl_add(codec->bus->card, kctl);
2040 if (err < 0)
2041 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002042 item = snd_array_new(&codec->mixers);
2043 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002044 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002045 item->kctl = kctl;
2046 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002047 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002048 return 0;
2049}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002050EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002051
Takashi Iwaid5191e52009-11-16 14:58:17 +01002052/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002053 * snd_hda_add_nid - Assign a NID to a control element
2054 * @codec: HD-audio codec
2055 * @nid: corresponding NID (optional)
2056 * @kctl: the control element to assign
2057 * @index: index to kctl
2058 *
2059 * Add the given control element to an array inside the codec instance.
2060 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2061 * NID:KCTL mapping - for example "Capture Source" selector.
2062 */
2063int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2064 unsigned int index, hda_nid_t nid)
2065{
2066 struct hda_nid_item *item;
2067
2068 if (nid > 0) {
2069 item = snd_array_new(&codec->nids);
2070 if (!item)
2071 return -ENOMEM;
2072 item->kctl = kctl;
2073 item->index = index;
2074 item->nid = nid;
2075 return 0;
2076 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002077 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2078 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002079 return -EINVAL;
2080}
2081EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2082
2083/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002084 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2085 * @codec: HD-audio codec
2086 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002087void snd_hda_ctls_clear(struct hda_codec *codec)
2088{
2089 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002090 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002091 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002092 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002093 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002094 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002095}
2096
Takashi Iwaia65d6292009-02-23 16:57:04 +01002097/* pseudo device locking
2098 * toggle card->shutdown to allow/disallow the device access (as a hack)
2099 */
2100static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002101{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002102 spin_lock(&card->files_lock);
2103 if (card->shutdown) {
2104 spin_unlock(&card->files_lock);
2105 return -EINVAL;
2106 }
2107 card->shutdown = 1;
2108 spin_unlock(&card->files_lock);
2109 return 0;
2110}
2111
2112static void hda_unlock_devices(struct snd_card *card)
2113{
2114 spin_lock(&card->files_lock);
2115 card->shutdown = 0;
2116 spin_unlock(&card->files_lock);
2117}
2118
Takashi Iwaid5191e52009-11-16 14:58:17 +01002119/**
2120 * snd_hda_codec_reset - Clear all objects assigned to the codec
2121 * @codec: HD-audio codec
2122 *
2123 * This frees the all PCM and control elements assigned to the codec, and
2124 * clears the caches and restores the pin default configurations.
2125 *
2126 * When a device is being used, it returns -EBSY. If successfully freed,
2127 * returns zero.
2128 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002129int snd_hda_codec_reset(struct hda_codec *codec)
2130{
2131 struct snd_card *card = codec->bus->card;
2132 int i, pcm;
2133
2134 if (hda_lock_devices(card) < 0)
2135 return -EBUSY;
2136 /* check whether the codec isn't used by any mixer or PCM streams */
2137 if (!list_empty(&card->ctl_files)) {
2138 hda_unlock_devices(card);
2139 return -EBUSY;
2140 }
2141 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2142 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2143 if (!cpcm->pcm)
2144 continue;
2145 if (cpcm->pcm->streams[0].substream_opened ||
2146 cpcm->pcm->streams[1].substream_opened) {
2147 hda_unlock_devices(card);
2148 return -EBUSY;
2149 }
2150 }
2151
2152 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002153
2154#ifdef CONFIG_SND_HDA_POWER_SAVE
2155 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002156 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002157#endif
2158 snd_hda_ctls_clear(codec);
2159 /* relase PCMs */
2160 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002161 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002162 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002163 clear_bit(codec->pcm_info[i].device,
2164 codec->bus->pcm_dev_bits);
2165 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002166 }
2167 if (codec->patch_ops.free)
2168 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002169 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002170 codec->spec = NULL;
2171 free_hda_cache(&codec->amp_cache);
2172 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002173 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2174 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002175 /* free only driver_pins so that init_pins + user_pins are restored */
2176 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002177 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002178 codec->num_pcms = 0;
2179 codec->pcm_info = NULL;
2180 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002181 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2182 codec->slave_dig_outs = NULL;
2183 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002184 module_put(codec->owner);
2185 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002186
2187 /* allow device access again */
2188 hda_unlock_devices(card);
2189 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002190}
2191
Takashi Iwaid5191e52009-11-16 14:58:17 +01002192/**
2193 * snd_hda_add_vmaster - create a virtual master control and add slaves
2194 * @codec: HD-audio codec
2195 * @name: vmaster control name
2196 * @tlv: TLV data (optional)
2197 * @slaves: slave control names (optional)
2198 *
2199 * Create a virtual master control with the given name. The TLV data
2200 * must be either NULL or a valid data.
2201 *
2202 * @slaves is a NULL-terminated array of strings, each of which is a
2203 * slave control name. All controls with these names are assigned to
2204 * the new virtual master control.
2205 *
2206 * This function returns zero if successful or a negative error code.
2207 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002208int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002209 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002210{
2211 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002212 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002213 int err;
2214
Takashi Iwai2f085542008-02-22 18:43:50 +01002215 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2216 ;
2217 if (!*s) {
2218 snd_printdd("No slave found for %s\n", name);
2219 return 0;
2220 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002221 kctl = snd_ctl_make_virtual_master(name, tlv);
2222 if (!kctl)
2223 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002224 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002225 if (err < 0)
2226 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002227
Takashi Iwai2134ea42008-01-10 16:53:55 +01002228 for (s = slaves; *s; s++) {
2229 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002230 int i = 0;
2231 for (;;) {
2232 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2233 if (!sctl) {
2234 if (!i)
2235 snd_printdd("Cannot find slave %s, "
2236 "skipped\n", *s);
2237 break;
2238 }
2239 err = snd_ctl_add_slave(kctl, sctl);
2240 if (err < 0)
2241 return err;
2242 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002243 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002244 }
2245 return 0;
2246}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002247EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002248
Takashi Iwaid5191e52009-11-16 14:58:17 +01002249/**
2250 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2251 *
2252 * The control element is supposed to have the private_value field
2253 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2254 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002255int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2256 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 int chs = get_amp_channels(kcontrol);
2259
2260 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2261 uinfo->count = chs == 3 ? 2 : 1;
2262 uinfo->value.integer.min = 0;
2263 uinfo->value.integer.max = 1;
2264 return 0;
2265}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002266EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Takashi Iwaid5191e52009-11-16 14:58:17 +01002268/**
2269 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2270 *
2271 * The control element is supposed to have the private_value field
2272 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2273 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002274int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2275 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2278 hda_nid_t nid = get_amp_nid(kcontrol);
2279 int chs = get_amp_channels(kcontrol);
2280 int dir = get_amp_direction(kcontrol);
2281 int idx = get_amp_index(kcontrol);
2282 long *valp = ucontrol->value.integer.value;
2283
2284 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002285 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002286 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002288 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002289 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return 0;
2291}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002292EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Takashi Iwaid5191e52009-11-16 14:58:17 +01002294/**
2295 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2296 *
2297 * The control element is supposed to have the private_value field
2298 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2299 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002300int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2301 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2304 hda_nid_t nid = get_amp_nid(kcontrol);
2305 int chs = get_amp_channels(kcontrol);
2306 int dir = get_amp_direction(kcontrol);
2307 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 long *valp = ucontrol->value.integer.value;
2309 int change = 0;
2310
Takashi Iwaicb53c622007-08-10 17:21:45 +02002311 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002312 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002313 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002314 HDA_AMP_MUTE,
2315 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002316 valp++;
2317 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002318 if (chs & 2)
2319 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002320 HDA_AMP_MUTE,
2321 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002322 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002323 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return change;
2325}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002326EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Takashi Iwai67d634c2009-11-16 15:35:59 +01002328#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002329/**
2330 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2331 *
2332 * This function calls snd_hda_enable_beep_device(), which behaves differently
2333 * depending on beep_mode option.
2334 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002335int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_value *ucontrol)
2337{
2338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2339 long *valp = ucontrol->value.integer.value;
2340
2341 snd_hda_enable_beep_device(codec, *valp);
2342 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2343}
2344EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002345#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347/*
Takashi Iwai985be542005-11-02 18:26:49 +01002348 * bound volume controls
2349 *
2350 * bind multiple volumes (# indices, from 0)
2351 */
2352
2353#define AMP_VAL_IDX_SHIFT 19
2354#define AMP_VAL_IDX_MASK (0x0f<<19)
2355
Takashi Iwaid5191e52009-11-16 14:58:17 +01002356/**
2357 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2358 *
2359 * The control element is supposed to have the private_value field
2360 * set up via HDA_BIND_MUTE*() macros.
2361 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002362int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2363 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002364{
2365 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2366 unsigned long pval;
2367 int err;
2368
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002369 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002370 pval = kcontrol->private_value;
2371 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2372 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2373 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002374 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002375 return err;
2376}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002377EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002378
Takashi Iwaid5191e52009-11-16 14:58:17 +01002379/**
2380 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2381 *
2382 * The control element is supposed to have the private_value field
2383 * set up via HDA_BIND_MUTE*() macros.
2384 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002385int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002387{
2388 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2389 unsigned long pval;
2390 int i, indices, err = 0, change = 0;
2391
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002392 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002393 pval = kcontrol->private_value;
2394 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2395 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002396 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2397 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002398 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2399 if (err < 0)
2400 break;
2401 change |= err;
2402 }
2403 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002404 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002405 return err < 0 ? err : change;
2406}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002407EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002408
Takashi Iwaid5191e52009-11-16 14:58:17 +01002409/**
2410 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2411 *
2412 * The control element is supposed to have the private_value field
2413 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002414 */
2415int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_info *uinfo)
2417{
2418 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2419 struct hda_bind_ctls *c;
2420 int err;
2421
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002422 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002423 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002424 kcontrol->private_value = *c->values;
2425 err = c->ops->info(kcontrol, uinfo);
2426 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002427 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002428 return err;
2429}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002430EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002431
Takashi Iwaid5191e52009-11-16 14:58:17 +01002432/**
2433 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2434 *
2435 * The control element is supposed to have the private_value field
2436 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2437 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002438int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
2440{
2441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2442 struct hda_bind_ctls *c;
2443 int err;
2444
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002445 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002446 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002447 kcontrol->private_value = *c->values;
2448 err = c->ops->get(kcontrol, ucontrol);
2449 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002450 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002451 return err;
2452}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002453EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002454
Takashi Iwaid5191e52009-11-16 14:58:17 +01002455/**
2456 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2457 *
2458 * The control element is supposed to have the private_value field
2459 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2460 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002461int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_value *ucontrol)
2463{
2464 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2465 struct hda_bind_ctls *c;
2466 unsigned long *vals;
2467 int err = 0, change = 0;
2468
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002469 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002470 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002471 for (vals = c->values; *vals; vals++) {
2472 kcontrol->private_value = *vals;
2473 err = c->ops->put(kcontrol, ucontrol);
2474 if (err < 0)
2475 break;
2476 change |= err;
2477 }
2478 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002479 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002480 return err < 0 ? err : change;
2481}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002482EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002483
Takashi Iwaid5191e52009-11-16 14:58:17 +01002484/**
2485 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2486 *
2487 * The control element is supposed to have the private_value field
2488 * set up via HDA_BIND_VOL() macro.
2489 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002490int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2491 unsigned int size, unsigned int __user *tlv)
2492{
2493 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2494 struct hda_bind_ctls *c;
2495 int err;
2496
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002497 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002498 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002499 kcontrol->private_value = *c->values;
2500 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2501 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002502 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002503 return err;
2504}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002505EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002506
2507struct hda_ctl_ops snd_hda_bind_vol = {
2508 .info = snd_hda_mixer_amp_volume_info,
2509 .get = snd_hda_mixer_amp_volume_get,
2510 .put = snd_hda_mixer_amp_volume_put,
2511 .tlv = snd_hda_mixer_amp_tlv
2512};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002513EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002514
2515struct hda_ctl_ops snd_hda_bind_sw = {
2516 .info = snd_hda_mixer_amp_switch_info,
2517 .get = snd_hda_mixer_amp_switch_get,
2518 .put = snd_hda_mixer_amp_switch_put,
2519 .tlv = snd_hda_mixer_amp_tlv
2520};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002521EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002522
2523/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 * SPDIF out controls
2525 */
2526
Takashi Iwai0ba21762007-04-16 11:29:14 +02002527static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2531 uinfo->count = 1;
2532 return 0;
2533}
2534
Takashi Iwai0ba21762007-04-16 11:29:14 +02002535static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2536 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
2538 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2539 IEC958_AES0_NONAUDIO |
2540 IEC958_AES0_CON_EMPHASIS_5015 |
2541 IEC958_AES0_CON_NOT_COPYRIGHT;
2542 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2543 IEC958_AES1_CON_ORIGINAL;
2544 return 0;
2545}
2546
Takashi Iwai0ba21762007-04-16 11:29:14 +02002547static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2548 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2551 IEC958_AES0_NONAUDIO |
2552 IEC958_AES0_PRO_EMPHASIS_5015;
2553 return 0;
2554}
2555
Takashi Iwai0ba21762007-04-16 11:29:14 +02002556static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2557 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
2559 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002560 int idx = kcontrol->private_value;
2561 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Stephen Warren7c9359762011-06-01 11:14:17 -06002563 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2564 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2565 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2566 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 return 0;
2569}
2570
2571/* convert from SPDIF status bits to HDA SPDIF bits
2572 * bit 0 (DigEn) is always set zero (to be filled later)
2573 */
2574static unsigned short convert_from_spdif_status(unsigned int sbits)
2575{
2576 unsigned short val = 0;
2577
2578 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002579 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002581 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002583 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2584 IEC958_AES0_PRO_EMPHASIS_5015)
2585 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002587 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2588 IEC958_AES0_CON_EMPHASIS_5015)
2589 val |= AC_DIG1_EMPHASIS;
2590 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2591 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002593 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2595 }
2596 return val;
2597}
2598
2599/* convert to SPDIF status bits from HDA SPDIF bits
2600 */
2601static unsigned int convert_to_spdif_status(unsigned short val)
2602{
2603 unsigned int sbits = 0;
2604
Takashi Iwai0ba21762007-04-16 11:29:14 +02002605 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002607 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 sbits |= IEC958_AES0_PROFESSIONAL;
2609 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002610 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2612 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002613 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002615 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002617 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2619 sbits |= val & (0x7f << 8);
2620 }
2621 return sbits;
2622}
2623
Takashi Iwai2f728532008-09-25 16:32:41 +02002624/* set digital convert verbs both for the given NID and its slaves */
2625static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2626 int verb, int val)
2627{
Takashi Iwaidda14412011-05-02 11:29:30 +02002628 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002629
Takashi Iwai9e976972008-11-25 08:17:20 +01002630 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002631 d = codec->slave_dig_outs;
2632 if (!d)
2633 return;
2634 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002635 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002636}
2637
2638static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2639 int dig1, int dig2)
2640{
2641 if (dig1 != -1)
2642 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2643 if (dig2 != -1)
2644 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2645}
2646
Takashi Iwai0ba21762007-04-16 11:29:14 +02002647static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2648 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
2650 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002651 int idx = kcontrol->private_value;
2652 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2653 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 unsigned short val;
2655 int change;
2656
Ingo Molnar62932df2006-01-16 16:34:20 +01002657 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002658 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2660 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2661 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002662 val = convert_from_spdif_status(spdif->status);
2663 val |= spdif->ctls & 1;
2664 change = spdif->ctls != val;
2665 spdif->ctls = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Takashi Iwai2f728532008-09-25 16:32:41 +02002667 if (change)
2668 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Ingo Molnar62932df2006-01-16 16:34:20 +01002670 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return change;
2672}
2673
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002674#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Takashi Iwai0ba21762007-04-16 11:29:14 +02002676static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2677 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
2679 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002680 int idx = kcontrol->private_value;
2681 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Stephen Warren7c9359762011-06-01 11:14:17 -06002683 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return 0;
2685}
2686
Takashi Iwai0ba21762007-04-16 11:29:14 +02002687static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2688 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
2690 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002691 int idx = kcontrol->private_value;
2692 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2693 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 unsigned short val;
2695 int change;
2696
Ingo Molnar62932df2006-01-16 16:34:20 +01002697 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002698 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002700 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002701 change = spdif->ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002702 if (change) {
Stephen Warren7c9359762011-06-01 11:14:17 -06002703 spdif->ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002704 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002705 /* unmute amp switch (if any) */
2706 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002707 (val & AC_DIG1_ENABLE))
2708 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2709 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002711 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return change;
2713}
2714
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002715static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 {
2717 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2718 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002719 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 .info = snd_hda_spdif_mask_info,
2721 .get = snd_hda_spdif_cmask_get,
2722 },
2723 {
2724 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2725 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002726 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 .info = snd_hda_spdif_mask_info,
2728 .get = snd_hda_spdif_pmask_get,
2729 },
2730 {
2731 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002732 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 .info = snd_hda_spdif_mask_info,
2734 .get = snd_hda_spdif_default_get,
2735 .put = snd_hda_spdif_default_put,
2736 },
2737 {
2738 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002739 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 .info = snd_hda_spdif_out_switch_info,
2741 .get = snd_hda_spdif_out_switch_get,
2742 .put = snd_hda_spdif_out_switch_put,
2743 },
2744 { } /* end */
2745};
2746
2747/**
2748 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2749 * @codec: the HDA codec
2750 * @nid: audio out widget NID
2751 *
2752 * Creates controls related with the SPDIF output.
2753 * Called from each patch supporting the SPDIF out.
2754 *
2755 * Returns 0 if successful, or a negative error code.
2756 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002757int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002760 struct snd_kcontrol *kctl;
2761 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002762 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002763 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Takashi Iwai1afe2062010-12-23 10:17:52 +01002765 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2766 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002767 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2768 return -EBUSY;
2769 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002770 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2772 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002773 if (!kctl)
2774 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002775 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002776 kctl->private_value = codec->spdif_out.used - 1;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002777 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002778 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return err;
2780 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002781 spdif->nid = nid;
2782 spdif->ctls = snd_hda_codec_read(codec, nid, 0,
2783 AC_VERB_GET_DIGI_CONVERT_1, 0);
2784 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return 0;
2786}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002787EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Stephen Warren7c9359762011-06-01 11:14:17 -06002789struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2790 hda_nid_t nid)
2791{
2792 int i;
2793 for (i = 0; i < codec->spdif_out.used; i++) {
2794 struct hda_spdif_out *spdif =
2795 snd_array_elem(&codec->spdif_out, i);
2796 if (spdif->nid == nid)
2797 return spdif;
2798 }
2799 return NULL;
2800}
2801EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002804 * SPDIF sharing with analog output
2805 */
2806static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_value *ucontrol)
2808{
2809 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2810 ucontrol->value.integer.value[0] = mout->share_spdif;
2811 return 0;
2812}
2813
2814static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2815 struct snd_ctl_elem_value *ucontrol)
2816{
2817 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2818 mout->share_spdif = !!ucontrol->value.integer.value[0];
2819 return 0;
2820}
2821
2822static struct snd_kcontrol_new spdif_share_sw = {
2823 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2824 .name = "IEC958 Default PCM Playback Switch",
2825 .info = snd_ctl_boolean_mono_info,
2826 .get = spdif_share_sw_get,
2827 .put = spdif_share_sw_put,
2828};
2829
Takashi Iwaid5191e52009-11-16 14:58:17 +01002830/**
2831 * snd_hda_create_spdif_share_sw - create Default PCM switch
2832 * @codec: the HDA codec
2833 * @mout: multi-out instance
2834 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002835int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2836 struct hda_multi_out *mout)
2837{
2838 if (!mout->dig_out_nid)
2839 return 0;
2840 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002841 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2842 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002843}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002844EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002845
2846/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 * SPDIF input
2848 */
2849
2850#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2851
Takashi Iwai0ba21762007-04-16 11:29:14 +02002852static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2853 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2856
2857 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2858 return 0;
2859}
2860
Takashi Iwai0ba21762007-04-16 11:29:14 +02002861static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2865 hda_nid_t nid = kcontrol->private_value;
2866 unsigned int val = !!ucontrol->value.integer.value[0];
2867 int change;
2868
Ingo Molnar62932df2006-01-16 16:34:20 +01002869 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002871 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002873 snd_hda_codec_write_cache(codec, nid, 0,
2874 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002876 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return change;
2878}
2879
Takashi Iwai0ba21762007-04-16 11:29:14 +02002880static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2884 hda_nid_t nid = kcontrol->private_value;
2885 unsigned short val;
2886 unsigned int sbits;
2887
Andrew Paprocki3982d172007-12-19 12:13:44 +01002888 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 sbits = convert_to_spdif_status(val);
2890 ucontrol->value.iec958.status[0] = sbits;
2891 ucontrol->value.iec958.status[1] = sbits >> 8;
2892 ucontrol->value.iec958.status[2] = sbits >> 16;
2893 ucontrol->value.iec958.status[3] = sbits >> 24;
2894 return 0;
2895}
2896
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002897static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 {
2899 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002900 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 .info = snd_hda_spdif_in_switch_info,
2902 .get = snd_hda_spdif_in_switch_get,
2903 .put = snd_hda_spdif_in_switch_put,
2904 },
2905 {
2906 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2907 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002908 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 .info = snd_hda_spdif_mask_info,
2910 .get = snd_hda_spdif_in_status_get,
2911 },
2912 { } /* end */
2913};
2914
2915/**
2916 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2917 * @codec: the HDA codec
2918 * @nid: audio in widget NID
2919 *
2920 * Creates controls related with the SPDIF input.
2921 * Called from each patch supporting the SPDIF in.
2922 *
2923 * Returns 0 if successful, or a negative error code.
2924 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002925int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
2927 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002928 struct snd_kcontrol *kctl;
2929 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002930 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Takashi Iwai1afe2062010-12-23 10:17:52 +01002932 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
2933 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002934 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2935 return -EBUSY;
2936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2938 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002939 if (!kctl)
2940 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002942 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002943 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return err;
2945 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002946 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002947 snd_hda_codec_read(codec, nid, 0,
2948 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002949 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return 0;
2951}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002952EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Takashi Iwaicb53c622007-08-10 17:21:45 +02002954#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002955/*
2956 * command cache
2957 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002959/* build a 32bit cache key with the widget id and the command parameter */
2960#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2961#define get_cmd_cache_nid(key) ((key) & 0xff)
2962#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2963
2964/**
2965 * snd_hda_codec_write_cache - send a single command with caching
2966 * @codec: the HDA codec
2967 * @nid: NID to send the command
2968 * @direct: direct flag
2969 * @verb: the verb to send
2970 * @parm: the parameter for the verb
2971 *
2972 * Send a single command without waiting for response.
2973 *
2974 * Returns 0 if successful, or a negative error code.
2975 */
2976int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2977 int direct, unsigned int verb, unsigned int parm)
2978{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002979 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2980 struct hda_cache_head *c;
2981 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002982
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002983 if (err < 0)
2984 return err;
2985 /* parm may contain the verb stuff for get/set amp */
2986 verb = verb | (parm >> 8);
2987 parm &= 0xff;
2988 key = build_cmd_cache_key(nid, verb);
2989 mutex_lock(&codec->bus->cmd_mutex);
2990 c = get_alloc_hash(&codec->cmd_cache, key);
2991 if (c)
2992 c->val = parm;
2993 mutex_unlock(&codec->bus->cmd_mutex);
2994 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002995}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002996EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002997
Takashi Iwaid5191e52009-11-16 14:58:17 +01002998/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002999 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3000 * @codec: the HDA codec
3001 * @nid: NID to send the command
3002 * @direct: direct flag
3003 * @verb: the verb to send
3004 * @parm: the parameter for the verb
3005 *
3006 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3007 * command if the parameter is already identical with the cached value.
3008 * If not, it sends the command and refreshes the cache.
3009 *
3010 * Returns 0 if successful, or a negative error code.
3011 */
3012int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3013 int direct, unsigned int verb, unsigned int parm)
3014{
3015 struct hda_cache_head *c;
3016 u32 key;
3017
3018 /* parm may contain the verb stuff for get/set amp */
3019 verb = verb | (parm >> 8);
3020 parm &= 0xff;
3021 key = build_cmd_cache_key(nid, verb);
3022 mutex_lock(&codec->bus->cmd_mutex);
3023 c = get_hash(&codec->cmd_cache, key);
3024 if (c && c->val == parm) {
3025 mutex_unlock(&codec->bus->cmd_mutex);
3026 return 0;
3027 }
3028 mutex_unlock(&codec->bus->cmd_mutex);
3029 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3030}
3031EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3032
3033/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003034 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3035 * @codec: HD-audio codec
3036 *
3037 * Execute all verbs recorded in the command caches to resume.
3038 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003039void snd_hda_codec_resume_cache(struct hda_codec *codec)
3040{
Takashi Iwai603c4012008-07-30 15:01:44 +02003041 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003042 int i;
3043
Takashi Iwai603c4012008-07-30 15:01:44 +02003044 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003045 u32 key = buffer->key;
3046 if (!key)
3047 continue;
3048 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3049 get_cmd_cache_cmd(key), buffer->val);
3050 }
3051}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003052EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003053
3054/**
3055 * snd_hda_sequence_write_cache - sequence writes with caching
3056 * @codec: the HDA codec
3057 * @seq: VERB array to send
3058 *
3059 * Send the commands sequentially from the given array.
3060 * Thte commands are recorded on cache for power-save and resume.
3061 * The array must be terminated with NID=0.
3062 */
3063void snd_hda_sequence_write_cache(struct hda_codec *codec,
3064 const struct hda_verb *seq)
3065{
3066 for (; seq->nid; seq++)
3067 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3068 seq->param);
3069}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003070EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003071#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003072
Takashi Iwai54d17402005-11-21 16:33:22 +01003073/*
3074 * set power state of the codec
3075 */
3076static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3077 unsigned int power_state)
3078{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003079 hda_nid_t nid;
3080 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003081
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003082 /* this delay seems necessary to avoid click noise at power-down */
3083 if (power_state == AC_PWRST_D3)
3084 msleep(100);
3085 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003086 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003087 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003088 if (power_state == AC_PWRST_D0 &&
3089 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003090 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003091
Takashi Iwaicb53c622007-08-10 17:21:45 +02003092 nid = codec->start_nid;
3093 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003094 unsigned int wcaps = get_wcaps(codec, nid);
3095 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003096 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003097 if (power_state == AC_PWRST_D3 &&
3098 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003099 unsigned int pincap;
3100 /*
3101 * don't power down the widget if it controls
3102 * eapd and EAPD_BTLENABLE is set.
3103 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003104 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003105 if (pincap & AC_PINCAP_EAPD) {
3106 int eapd = snd_hda_codec_read(codec,
3107 nid, 0,
3108 AC_VERB_GET_EAPD_BTLENABLE, 0);
3109 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003110 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003111 continue;
3112 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003113 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003114 snd_hda_codec_write(codec, nid, 0,
3115 AC_VERB_SET_POWER_STATE,
3116 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003117 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003118 }
3119
Takashi Iwaicb53c622007-08-10 17:21:45 +02003120 if (power_state == AC_PWRST_D0) {
3121 unsigned long end_time;
3122 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003123 /* wait until the codec reachs to D0 */
3124 end_time = jiffies + msecs_to_jiffies(500);
3125 do {
3126 state = snd_hda_codec_read(codec, fg, 0,
3127 AC_VERB_GET_POWER_STATE, 0);
3128 if (state == power_state)
3129 break;
3130 msleep(1);
3131 } while (time_after_eq(end_time, jiffies));
3132 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003133}
3134
Takashi Iwai11aeff02008-07-30 15:01:46 +02003135#ifdef CONFIG_SND_HDA_HWDEP
3136/* execute additional init verbs */
3137static void hda_exec_init_verbs(struct hda_codec *codec)
3138{
3139 if (codec->init_verbs.list)
3140 snd_hda_sequence_write(codec, codec->init_verbs.list);
3141}
3142#else
3143static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3144#endif
3145
Takashi Iwaicb53c622007-08-10 17:21:45 +02003146#ifdef SND_HDA_NEEDS_RESUME
3147/*
3148 * call suspend and power-down; used both from PM and power-save
3149 */
3150static void hda_call_codec_suspend(struct hda_codec *codec)
3151{
3152 if (codec->patch_ops.suspend)
3153 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003154 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003155 hda_set_power_state(codec,
3156 codec->afg ? codec->afg : codec->mfg,
3157 AC_PWRST_D3);
3158#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003159 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003160 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003161 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003162 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003163 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003164#endif
3165}
3166
3167/*
3168 * kick up codec; used both from PM and power-save
3169 */
3170static void hda_call_codec_resume(struct hda_codec *codec)
3171{
3172 hda_set_power_state(codec,
3173 codec->afg ? codec->afg : codec->mfg,
3174 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003175 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003176 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003177 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003178 if (codec->patch_ops.resume)
3179 codec->patch_ops.resume(codec);
3180 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003181 if (codec->patch_ops.init)
3182 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003183 snd_hda_codec_resume_amp(codec);
3184 snd_hda_codec_resume_cache(codec);
3185 }
3186}
3187#endif /* SND_HDA_NEEDS_RESUME */
3188
Takashi Iwai54d17402005-11-21 16:33:22 +01003189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190/**
3191 * snd_hda_build_controls - build mixer controls
3192 * @bus: the BUS
3193 *
3194 * Creates mixer controls for each codec included in the bus.
3195 *
3196 * Returns 0 if successful, otherwise a negative error code.
3197 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003198int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003200 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Takashi Iwai0ba21762007-04-16 11:29:14 +02003202 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003203 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003204 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003205 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003206 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003207 err = snd_hda_codec_reset(codec);
3208 if (err < 0) {
3209 printk(KERN_ERR
3210 "hda_codec: cannot revert codec\n");
3211 return err;
3212 }
3213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003215 return 0;
3216}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003217EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003219int snd_hda_codec_build_controls(struct hda_codec *codec)
3220{
3221 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003222 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003223 /* continue to initialize... */
3224 if (codec->patch_ops.init)
3225 err = codec->patch_ops.init(codec);
3226 if (!err && codec->patch_ops.build_controls)
3227 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003228 if (err < 0)
3229 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return 0;
3231}
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233/*
3234 * stream formats
3235 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003236struct hda_rate_tbl {
3237 unsigned int hz;
3238 unsigned int alsa_bits;
3239 unsigned int hda_fmt;
3240};
3241
Takashi Iwai92f10b32010-08-03 14:21:00 +02003242/* rate = base * mult / div */
3243#define HDA_RATE(base, mult, div) \
3244 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3245 (((div) - 1) << AC_FMT_DIV_SHIFT))
3246
Takashi Iwaibefdf312005-08-22 13:57:55 +02003247static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003249
3250 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003251 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3252 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3253 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3254 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3255 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3256 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3257 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3258 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3259 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3260 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3261 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003262#define AC_PAR_PCM_RATE_BITS 11
3263 /* up to bits 10, 384kHZ isn't supported properly */
3264
3265 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003266 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003267
Takashi Iwaibefdf312005-08-22 13:57:55 +02003268 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269};
3270
3271/**
3272 * snd_hda_calc_stream_format - calculate format bitset
3273 * @rate: the sample rate
3274 * @channels: the number of channels
3275 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3276 * @maxbps: the max. bps
3277 *
3278 * Calculate the format bitset from the given rate, channels and th PCM format.
3279 *
3280 * Return zero if invalid.
3281 */
3282unsigned int snd_hda_calc_stream_format(unsigned int rate,
3283 unsigned int channels,
3284 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003285 unsigned int maxbps,
3286 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
3288 int i;
3289 unsigned int val = 0;
3290
Takashi Iwaibefdf312005-08-22 13:57:55 +02003291 for (i = 0; rate_bits[i].hz; i++)
3292 if (rate_bits[i].hz == rate) {
3293 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 break;
3295 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003296 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 snd_printdd("invalid rate %d\n", rate);
3298 return 0;
3299 }
3300
3301 if (channels == 0 || channels > 8) {
3302 snd_printdd("invalid channels %d\n", channels);
3303 return 0;
3304 }
3305 val |= channels - 1;
3306
3307 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003308 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003309 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003310 break;
3311 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003312 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003313 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 case 20:
3315 case 24:
3316 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003317 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003318 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003320 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003322 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 break;
3324 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003325 snd_printdd("invalid format width %d\n",
3326 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 return 0;
3328 }
3329
Anssi Hannula32c168c2010-08-03 13:28:57 +03003330 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003331 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 return val;
3334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003335EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003337static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3338{
3339 unsigned int val = 0;
3340 if (nid != codec->afg &&
3341 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3342 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3343 if (!val || val == -1)
3344 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3345 if (!val || val == -1)
3346 return 0;
3347 return val;
3348}
3349
3350static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3351{
3352 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3353 get_pcm_param);
3354}
3355
3356static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3357{
3358 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3359 if (!streams || streams == -1)
3360 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3361 if (!streams || streams == -1)
3362 return 0;
3363 return streams;
3364}
3365
3366static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3367{
3368 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3369 get_stream_param);
3370}
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372/**
3373 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3374 * @codec: the HDA codec
3375 * @nid: NID to query
3376 * @ratesp: the pointer to store the detected rate bitflags
3377 * @formatsp: the pointer to store the detected formats
3378 * @bpsp: the pointer to store the detected format widths
3379 *
3380 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3381 * or @bsps argument is ignored.
3382 *
3383 * Returns 0 if successful, otherwise a negative error code.
3384 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003385static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3387{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003388 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003390 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003391 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 if (ratesp) {
3394 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003395 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003397 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003399 if (rates == 0) {
3400 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3401 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3402 nid, val,
3403 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3404 return -EIO;
3405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 *ratesp = rates;
3407 }
3408
3409 if (formatsp || bpsp) {
3410 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003411 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003413 streams = query_stream_param(codec, nid);
3414 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
3417 bps = 0;
3418 if (streams & AC_SUPFMT_PCM) {
3419 if (val & AC_SUPPCM_BITS_8) {
3420 formats |= SNDRV_PCM_FMTBIT_U8;
3421 bps = 8;
3422 }
3423 if (val & AC_SUPPCM_BITS_16) {
3424 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3425 bps = 16;
3426 }
3427 if (wcaps & AC_WCAP_DIGITAL) {
3428 if (val & AC_SUPPCM_BITS_32)
3429 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3430 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3431 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3432 if (val & AC_SUPPCM_BITS_24)
3433 bps = 24;
3434 else if (val & AC_SUPPCM_BITS_20)
3435 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003436 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3437 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3439 if (val & AC_SUPPCM_BITS_32)
3440 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 else if (val & AC_SUPPCM_BITS_24)
3442 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003443 else if (val & AC_SUPPCM_BITS_20)
3444 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
3446 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003447 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003449 if (!bps)
3450 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003451 }
3452 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003453 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 /* temporary hack: we have still no proper support
3455 * for the direct AC3 stream...
3456 */
3457 formats |= SNDRV_PCM_FMTBIT_U8;
3458 bps = 8;
3459 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003460 if (formats == 0) {
3461 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3462 "(nid=0x%x, val=0x%x, ovrd=%i, "
3463 "streams=0x%x)\n",
3464 nid, val,
3465 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3466 streams);
3467 return -EIO;
3468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (formatsp)
3470 *formatsp = formats;
3471 if (bpsp)
3472 *bpsp = bps;
3473 }
3474
3475 return 0;
3476}
3477
3478/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003479 * snd_hda_is_supported_format - Check the validity of the format
3480 * @codec: HD-audio codec
3481 * @nid: NID to check
3482 * @format: the HD-audio format value to check
3483 *
3484 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 *
3486 * Returns 1 if supported, 0 if not.
3487 */
3488int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3489 unsigned int format)
3490{
3491 int i;
3492 unsigned int val = 0, rate, stream;
3493
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003494 val = query_pcm_param(codec, nid);
3495 if (!val)
3496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
3498 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003499 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003500 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (val & (1 << i))
3502 break;
3503 return 0;
3504 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003505 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 return 0;
3507
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003508 stream = query_stream_param(codec, nid);
3509 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return 0;
3511
3512 if (stream & AC_SUPFMT_PCM) {
3513 switch (format & 0xf0) {
3514 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003515 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 return 0;
3517 break;
3518 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003519 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 return 0;
3521 break;
3522 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003523 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 return 0;
3525 break;
3526 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003527 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return 0;
3529 break;
3530 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003531 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 return 0;
3533 break;
3534 default:
3535 return 0;
3536 }
3537 } else {
3538 /* FIXME: check for float32 and AC3? */
3539 }
3540
3541 return 1;
3542}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003543EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545/*
3546 * PCM stuff
3547 */
3548static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3549 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003550 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
3552 return 0;
3553}
3554
3555static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3556 struct hda_codec *codec,
3557 unsigned int stream_tag,
3558 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003559 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
3561 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3562 return 0;
3563}
3564
3565static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3566 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003567 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
Takashi Iwai888afa12008-03-18 09:57:50 +01003569 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return 0;
3571}
3572
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003573static int set_pcm_default_values(struct hda_codec *codec,
3574 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003576 int err;
3577
Takashi Iwai0ba21762007-04-16 11:29:14 +02003578 /* query support PCM information from the given NID */
3579 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003580 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003581 info->rates ? NULL : &info->rates,
3582 info->formats ? NULL : &info->formats,
3583 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003584 if (err < 0)
3585 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 }
3587 if (info->ops.open == NULL)
3588 info->ops.open = hda_pcm_default_open_close;
3589 if (info->ops.close == NULL)
3590 info->ops.close = hda_pcm_default_open_close;
3591 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003592 if (snd_BUG_ON(!info->nid))
3593 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 info->ops.prepare = hda_pcm_default_prepare;
3595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003597 if (snd_BUG_ON(!info->nid))
3598 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 info->ops.cleanup = hda_pcm_default_cleanup;
3600 }
3601 return 0;
3602}
3603
Takashi Iwaieb541332010-08-06 13:48:11 +02003604/*
3605 * codec prepare/cleanup entries
3606 */
3607int snd_hda_codec_prepare(struct hda_codec *codec,
3608 struct hda_pcm_stream *hinfo,
3609 unsigned int stream,
3610 unsigned int format,
3611 struct snd_pcm_substream *substream)
3612{
3613 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003614 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003615 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3616 if (ret >= 0)
3617 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003618 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003619 return ret;
3620}
3621EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3622
3623void snd_hda_codec_cleanup(struct hda_codec *codec,
3624 struct hda_pcm_stream *hinfo,
3625 struct snd_pcm_substream *substream)
3626{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003627 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003628 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003629 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003630}
3631EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3632
Takashi Iwaid5191e52009-11-16 14:58:17 +01003633/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003634const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3635 "Audio", "SPDIF", "HDMI", "Modem"
3636};
3637
Takashi Iwai176d5332008-07-30 15:01:44 +02003638/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003639 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003640 *
3641 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003642 */
3643static int get_empty_pcm_device(struct hda_bus *bus, int type)
3644{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003645 /* audio device indices; not linear to keep compatibility */
3646 static int audio_idx[HDA_PCM_NTYPES][5] = {
3647 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3648 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003649 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003650 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003651 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003652 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003653
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003654 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003655 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3656 return -EINVAL;
3657 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003658
3659 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3660 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3661 return audio_idx[type][i];
3662
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003663 snd_printk(KERN_WARNING "Too many %s devices\n",
3664 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003665 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003666}
3667
3668/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003669 * attach a new PCM stream
3670 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003671static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003672{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003673 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003674 struct hda_pcm_stream *info;
3675 int stream, err;
3676
Takashi Iwaib91f0802008-11-04 08:43:08 +01003677 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003678 return -EINVAL;
3679 for (stream = 0; stream < 2; stream++) {
3680 info = &pcm->stream[stream];
3681 if (info->substreams) {
3682 err = set_pcm_default_values(codec, info);
3683 if (err < 0)
3684 return err;
3685 }
3686 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003687 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003688}
3689
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003690/* assign all PCMs of the given codec */
3691int snd_hda_codec_build_pcms(struct hda_codec *codec)
3692{
3693 unsigned int pcm;
3694 int err;
3695
3696 if (!codec->num_pcms) {
3697 if (!codec->patch_ops.build_pcms)
3698 return 0;
3699 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003700 if (err < 0) {
3701 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003702 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003703 err = snd_hda_codec_reset(codec);
3704 if (err < 0) {
3705 printk(KERN_ERR
3706 "hda_codec: cannot revert codec\n");
3707 return err;
3708 }
3709 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003710 }
3711 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3712 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3713 int dev;
3714
3715 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003716 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003717
3718 if (!cpcm->pcm) {
3719 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3720 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003721 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003722 cpcm->device = dev;
3723 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003724 if (err < 0) {
3725 printk(KERN_ERR "hda_codec: cannot attach "
3726 "PCM stream %d for codec #%d\n",
3727 dev, codec->addr);
3728 continue; /* no fatal error */
3729 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003730 }
3731 }
3732 return 0;
3733}
3734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735/**
3736 * snd_hda_build_pcms - build PCM information
3737 * @bus: the BUS
3738 *
3739 * Create PCM information for each codec included in the bus.
3740 *
3741 * The build_pcms codec patch is requested to set up codec->num_pcms and
3742 * codec->pcm_info properly. The array is referred by the top-level driver
3743 * to create its PCM instances.
3744 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3745 * callback.
3746 *
3747 * At least, substreams, channels_min and channels_max must be filled for
3748 * each stream. substreams = 0 indicates that the stream doesn't exist.
3749 * When rates and/or formats are zero, the supported values are queried
3750 * from the given nid. The nid is used also by the default ops.prepare
3751 * and ops.cleanup callbacks.
3752 *
3753 * The driver needs to call ops.open in its open callback. Similarly,
3754 * ops.close is supposed to be called in the close callback.
3755 * ops.prepare should be called in the prepare or hw_params callback
3756 * with the proper parameters for set up.
3757 * ops.cleanup should be called in hw_free for clean up of streams.
3758 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003759 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003761int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003763 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Takashi Iwai0ba21762007-04-16 11:29:14 +02003765 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003766 int err = snd_hda_codec_build_pcms(codec);
3767 if (err < 0)
3768 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 }
3770 return 0;
3771}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003772EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774/**
3775 * snd_hda_check_board_config - compare the current codec with the config table
3776 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003777 * @num_configs: number of config enums
3778 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 * @tbl: configuration table, terminated by null entries
3780 *
3781 * Compares the modelname or PCI subsystem id of the current codec with the
3782 * given configuration table. If a matching entry is found, returns its
3783 * config value (supposed to be 0 or positive).
3784 *
3785 * If no entries are matching, the function returns a negative value.
3786 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003787int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003788 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003789 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003791 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003792 int i;
3793 for (i = 0; i < num_configs; i++) {
3794 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003795 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003796 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3797 "selected\n", models[i]);
3798 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 }
3800 }
3801 }
3802
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003803 if (!codec->bus->pci || !tbl)
3804 return -1;
3805
3806 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3807 if (!tbl)
3808 return -1;
3809 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003810#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003811 char tmp[10];
3812 const char *model = NULL;
3813 if (models)
3814 model = models[tbl->value];
3815 if (!model) {
3816 sprintf(tmp, "#%d", tbl->value);
3817 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003819 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3820 "for config %x:%x (%s)\n",
3821 model, tbl->subvendor, tbl->subdevice,
3822 (tbl->name ? tbl->name : "Unknown device"));
3823#endif
3824 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
3826 return -1;
3827}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003828EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003831 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003832 subsystem ID with the
3833 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003834
3835 This is important for Gateway notebooks with SB450 HDA Audio
3836 where the vendor ID of the PCI device is:
3837 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3838 and the vendor/subvendor are found only at the codec.
3839
3840 * @codec: the HDA codec
3841 * @num_configs: number of config enums
3842 * @models: array of model name strings
3843 * @tbl: configuration table, terminated by null entries
3844 *
3845 * Compares the modelname or PCI subsystem id of the current codec with the
3846 * given configuration table. If a matching entry is found, returns its
3847 * config value (supposed to be 0 or positive).
3848 *
3849 * If no entries are matching, the function returns a negative value.
3850 */
3851int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003852 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003853 const struct snd_pci_quirk *tbl)
3854{
3855 const struct snd_pci_quirk *q;
3856
3857 /* Search for codec ID */
3858 for (q = tbl; q->subvendor; q++) {
3859 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3860
3861 if (vendorid == codec->subsystem_id)
3862 break;
3863 }
3864
3865 if (!q->subvendor)
3866 return -1;
3867
3868 tbl = q;
3869
3870 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003871#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003872 char tmp[10];
3873 const char *model = NULL;
3874 if (models)
3875 model = models[tbl->value];
3876 if (!model) {
3877 sprintf(tmp, "#%d", tbl->value);
3878 model = tmp;
3879 }
3880 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3881 "for config %x:%x (%s)\n",
3882 model, tbl->subvendor, tbl->subdevice,
3883 (tbl->name ? tbl->name : "Unknown device"));
3884#endif
3885 return tbl->value;
3886 }
3887 return -1;
3888}
3889EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3890
3891/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 * snd_hda_add_new_ctls - create controls from the array
3893 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003894 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 *
3896 * This helper function creates and add new controls in the given array.
3897 * The array must be terminated with an empty entry as terminator.
3898 *
3899 * Returns 0 if successful, or a negative error code.
3900 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003901int snd_hda_add_new_ctls(struct hda_codec *codec,
3902 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003904 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
3906 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003907 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003908 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003909 if (knew->iface == -1) /* skip this codec private value */
3910 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003911 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003912 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003913 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003914 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003915 if (addr > 0)
3916 kctl->id.device = addr;
3917 if (idx > 0)
3918 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003919 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003920 if (!err)
3921 break;
3922 /* try first with another device index corresponding to
3923 * the codec addr; if it still fails (or it's the
3924 * primary codec), then try another control index
3925 */
3926 if (!addr && codec->addr)
3927 addr = codec->addr;
3928 else if (!idx && !knew->index) {
3929 idx = find_empty_mixer_ctl_idx(codec,
3930 knew->name);
3931 if (idx <= 0)
3932 return err;
3933 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003934 return err;
3935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 }
3937 return 0;
3938}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003939EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Takashi Iwaicb53c622007-08-10 17:21:45 +02003941#ifdef CONFIG_SND_HDA_POWER_SAVE
3942static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3943 unsigned int power_state);
3944
3945static void hda_power_work(struct work_struct *work)
3946{
3947 struct hda_codec *codec =
3948 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003949 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003950
Maxim Levitsky2e492462007-09-03 15:26:57 +02003951 if (!codec->power_on || codec->power_count) {
3952 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003953 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003954 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003955
3956 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003957 if (bus->ops.pm_notify)
3958 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003959}
3960
3961static void hda_keep_power_on(struct hda_codec *codec)
3962{
3963 codec->power_count++;
3964 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003965 codec->power_jiffies = jiffies;
3966}
3967
Takashi Iwaid5191e52009-11-16 14:58:17 +01003968/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003969void snd_hda_update_power_acct(struct hda_codec *codec)
3970{
3971 unsigned long delta = jiffies - codec->power_jiffies;
3972 if (codec->power_on)
3973 codec->power_on_acct += delta;
3974 else
3975 codec->power_off_acct += delta;
3976 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003977}
3978
Takashi Iwaid5191e52009-11-16 14:58:17 +01003979/**
3980 * snd_hda_power_up - Power-up the codec
3981 * @codec: HD-audio codec
3982 *
3983 * Increment the power-up counter and power up the hardware really when
3984 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003985 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003986void snd_hda_power_up(struct hda_codec *codec)
3987{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003988 struct hda_bus *bus = codec->bus;
3989
Takashi Iwaicb53c622007-08-10 17:21:45 +02003990 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003991 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003992 return;
3993
Takashi Iwaia2f63092009-11-11 09:34:25 +01003994 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003995 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003996 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003997 if (bus->ops.pm_notify)
3998 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003999 hda_call_codec_resume(codec);
4000 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004001 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004002}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004003EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004004
4005#define power_save(codec) \
4006 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004007
Takashi Iwaid5191e52009-11-16 14:58:17 +01004008/**
4009 * snd_hda_power_down - Power-down the codec
4010 * @codec: HD-audio codec
4011 *
4012 * Decrement the power-up counter and schedules the power-off work if
4013 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004014 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004015void snd_hda_power_down(struct hda_codec *codec)
4016{
4017 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004018 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004019 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004020 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004021 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004022 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004023 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004024 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004026EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004027
Takashi Iwaid5191e52009-11-16 14:58:17 +01004028/**
4029 * snd_hda_check_amp_list_power - Check the amp list and update the power
4030 * @codec: HD-audio codec
4031 * @check: the object containing an AMP list and the status
4032 * @nid: NID to check / update
4033 *
4034 * Check whether the given NID is in the amp list. If it's in the list,
4035 * check the current AMP status, and update the the power-status according
4036 * to the mute status.
4037 *
4038 * This function is supposed to be set or called from the check_power_status
4039 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004040 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004041int snd_hda_check_amp_list_power(struct hda_codec *codec,
4042 struct hda_loopback_check *check,
4043 hda_nid_t nid)
4044{
Takashi Iwai031024e2011-05-02 11:29:30 +02004045 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004046 int ch, v;
4047
4048 if (!check->amplist)
4049 return 0;
4050 for (p = check->amplist; p->nid; p++) {
4051 if (p->nid == nid)
4052 break;
4053 }
4054 if (!p->nid)
4055 return 0; /* nothing changed */
4056
4057 for (p = check->amplist; p->nid; p++) {
4058 for (ch = 0; ch < 2; ch++) {
4059 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4060 p->idx);
4061 if (!(v & HDA_AMP_MUTE) && v > 0) {
4062 if (!check->power_on) {
4063 check->power_on = 1;
4064 snd_hda_power_up(codec);
4065 }
4066 return 1;
4067 }
4068 }
4069 }
4070 if (check->power_on) {
4071 check->power_on = 0;
4072 snd_hda_power_down(codec);
4073 }
4074 return 0;
4075}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004076EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004077#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004079/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004080 * Channel mode helper
4081 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004082
4083/**
4084 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4085 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004086int snd_hda_ch_mode_info(struct hda_codec *codec,
4087 struct snd_ctl_elem_info *uinfo,
4088 const struct hda_channel_mode *chmode,
4089 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004090{
4091 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4092 uinfo->count = 1;
4093 uinfo->value.enumerated.items = num_chmodes;
4094 if (uinfo->value.enumerated.item >= num_chmodes)
4095 uinfo->value.enumerated.item = num_chmodes - 1;
4096 sprintf(uinfo->value.enumerated.name, "%dch",
4097 chmode[uinfo->value.enumerated.item].channels);
4098 return 0;
4099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004100EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004101
Takashi Iwaid5191e52009-11-16 14:58:17 +01004102/**
4103 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4104 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004105int snd_hda_ch_mode_get(struct hda_codec *codec,
4106 struct snd_ctl_elem_value *ucontrol,
4107 const struct hda_channel_mode *chmode,
4108 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004109 int max_channels)
4110{
4111 int i;
4112
4113 for (i = 0; i < num_chmodes; i++) {
4114 if (max_channels == chmode[i].channels) {
4115 ucontrol->value.enumerated.item[0] = i;
4116 break;
4117 }
4118 }
4119 return 0;
4120}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004121EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004122
Takashi Iwaid5191e52009-11-16 14:58:17 +01004123/**
4124 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4125 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004126int snd_hda_ch_mode_put(struct hda_codec *codec,
4127 struct snd_ctl_elem_value *ucontrol,
4128 const struct hda_channel_mode *chmode,
4129 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004130 int *max_channelsp)
4131{
4132 unsigned int mode;
4133
4134 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004135 if (mode >= num_chmodes)
4136 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004137 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004138 return 0;
4139 /* change the current channel setting */
4140 *max_channelsp = chmode[mode].channels;
4141 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004142 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004143 return 1;
4144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004145EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147/*
4148 * input MUX helper
4149 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004150
4151/**
4152 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4153 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004154int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4155 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
4157 unsigned int index;
4158
4159 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4160 uinfo->count = 1;
4161 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004162 if (!imux->num_items)
4163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 index = uinfo->value.enumerated.item;
4165 if (index >= imux->num_items)
4166 index = imux->num_items - 1;
4167 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4168 return 0;
4169}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004170EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Takashi Iwaid5191e52009-11-16 14:58:17 +01004172/**
4173 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004175int snd_hda_input_mux_put(struct hda_codec *codec,
4176 const struct hda_input_mux *imux,
4177 struct snd_ctl_elem_value *ucontrol,
4178 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 unsigned int *cur_val)
4180{
4181 unsigned int idx;
4182
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004183 if (!imux->num_items)
4184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 idx = ucontrol->value.enumerated.item[0];
4186 if (idx >= imux->num_items)
4187 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004188 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004190 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4191 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 *cur_val = idx;
4193 return 1;
4194}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004195EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
4197
4198/*
4199 * Multi-channel / digital-out PCM helper functions
4200 */
4201
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004202/* setup SPDIF output stream */
4203static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4204 unsigned int stream_tag, unsigned int format)
4205{
Stephen Warren7c9359762011-06-01 11:14:17 -06004206 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4207
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004208 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004209 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004210 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004211 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004212 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004213 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004214 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004215 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004216 for (d = codec->slave_dig_outs; *d; d++)
4217 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4218 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004219 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004220 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004221 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004222 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004223 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004224}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004225
Takashi Iwai2f728532008-09-25 16:32:41 +02004226static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4227{
4228 snd_hda_codec_cleanup_stream(codec, nid);
4229 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004230 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004231 for (d = codec->slave_dig_outs; *d; d++)
4232 snd_hda_codec_cleanup_stream(codec, *d);
4233 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004234}
4235
Takashi Iwaid5191e52009-11-16 14:58:17 +01004236/**
4237 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4238 * @bus: HD-audio bus
4239 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004240void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4241{
4242 struct hda_codec *codec;
4243
4244 if (!bus)
4245 return;
4246 list_for_each_entry(codec, &bus->codec_list, list) {
4247#ifdef CONFIG_SND_HDA_POWER_SAVE
4248 if (!codec->power_on)
4249 continue;
4250#endif
4251 if (codec->patch_ops.reboot_notify)
4252 codec->patch_ops.reboot_notify(codec);
4253 }
4254}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004255EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004256
Takashi Iwaid5191e52009-11-16 14:58:17 +01004257/**
4258 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004260int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4261 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
Ingo Molnar62932df2006-01-16 16:34:20 +01004263 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004264 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4265 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004266 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004268 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 return 0;
4270}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004271EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
Takashi Iwaid5191e52009-11-16 14:58:17 +01004273/**
4274 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4275 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004276int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4277 struct hda_multi_out *mout,
4278 unsigned int stream_tag,
4279 unsigned int format,
4280 struct snd_pcm_substream *substream)
4281{
4282 mutex_lock(&codec->spdif_mutex);
4283 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4284 mutex_unlock(&codec->spdif_mutex);
4285 return 0;
4286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004287EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004288
Takashi Iwaid5191e52009-11-16 14:58:17 +01004289/**
4290 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4291 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004292int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4293 struct hda_multi_out *mout)
4294{
4295 mutex_lock(&codec->spdif_mutex);
4296 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4297 mutex_unlock(&codec->spdif_mutex);
4298 return 0;
4299}
4300EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4301
Takashi Iwaid5191e52009-11-16 14:58:17 +01004302/**
4303 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004305int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4306 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
Ingo Molnar62932df2006-01-16 16:34:20 +01004308 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004310 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 return 0;
4312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004313EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Takashi Iwaid5191e52009-11-16 14:58:17 +01004315/**
4316 * snd_hda_multi_out_analog_open - open analog outputs
4317 *
4318 * Open analog outputs and set up the hw-constraints.
4319 * If the digital outputs can be opened as slave, open the digital
4320 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004322int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4323 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004324 struct snd_pcm_substream *substream,
4325 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
Takashi Iwai9a081602008-02-12 18:37:26 +01004327 struct snd_pcm_runtime *runtime = substream->runtime;
4328 runtime->hw.channels_max = mout->max_channels;
4329 if (mout->dig_out_nid) {
4330 if (!mout->analog_rates) {
4331 mout->analog_rates = hinfo->rates;
4332 mout->analog_formats = hinfo->formats;
4333 mout->analog_maxbps = hinfo->maxbps;
4334 } else {
4335 runtime->hw.rates = mout->analog_rates;
4336 runtime->hw.formats = mout->analog_formats;
4337 hinfo->maxbps = mout->analog_maxbps;
4338 }
4339 if (!mout->spdif_rates) {
4340 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4341 &mout->spdif_rates,
4342 &mout->spdif_formats,
4343 &mout->spdif_maxbps);
4344 }
4345 mutex_lock(&codec->spdif_mutex);
4346 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004347 if ((runtime->hw.rates & mout->spdif_rates) &&
4348 (runtime->hw.formats & mout->spdif_formats)) {
4349 runtime->hw.rates &= mout->spdif_rates;
4350 runtime->hw.formats &= mout->spdif_formats;
4351 if (mout->spdif_maxbps < hinfo->maxbps)
4352 hinfo->maxbps = mout->spdif_maxbps;
4353 } else {
4354 mout->share_spdif = 0;
4355 /* FIXME: need notify? */
4356 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004357 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004358 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4361 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4362}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004363EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Takashi Iwaid5191e52009-11-16 14:58:17 +01004365/**
4366 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4367 *
4368 * Set up the i/o for analog out.
4369 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004371int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4372 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 unsigned int stream_tag,
4374 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004375 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
Takashi Iwaidda14412011-05-02 11:29:30 +02004377 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004379 struct hda_spdif_out *spdif =
4380 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 int i;
4382
Ingo Molnar62932df2006-01-16 16:34:20 +01004383 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004384 if (mout->dig_out_nid && mout->share_spdif &&
4385 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004387 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4388 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004389 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004391 setup_dig_out_stream(codec, mout->dig_out_nid,
4392 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 } else {
4394 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004395 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 }
4397 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004398 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
4400 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004401 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4402 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004403 if (!mout->no_share_stream &&
4404 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004406 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4407 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004408 /* extra outputs copied from front */
4409 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004410 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004411 snd_hda_codec_setup_stream(codec,
4412 mout->extra_out_nid[i],
4413 stream_tag, 0, format);
4414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 /* surrounds */
4416 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004417 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004418 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4419 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004420 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004421 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4422 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 }
4424 return 0;
4425}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004426EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Takashi Iwaid5191e52009-11-16 14:58:17 +01004428/**
4429 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004431int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4432 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
Takashi Iwaidda14412011-05-02 11:29:30 +02004434 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 int i;
4436
4437 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004438 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004440 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004441 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4442 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004443 snd_hda_codec_cleanup_stream(codec,
4444 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004445 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004447 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 mout->dig_out_used = 0;
4449 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004450 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 return 0;
4452}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004453EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004455/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004456 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004457 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004458
Takashi Iwaidda14412011-05-02 11:29:30 +02004459static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004460{
4461 for (; *list; list++)
4462 if (*list == nid)
4463 return 1;
4464 return 0;
4465}
4466
Steve Longerbeam81937d32007-05-08 15:33:03 +02004467
4468/*
4469 * Sort an associated group of pins according to their sequence numbers.
4470 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004471static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004472 int num_pins)
4473{
4474 int i, j;
4475 short seq;
4476 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004477
Steve Longerbeam81937d32007-05-08 15:33:03 +02004478 for (i = 0; i < num_pins; i++) {
4479 for (j = i + 1; j < num_pins; j++) {
4480 if (sequences[i] > sequences[j]) {
4481 seq = sequences[i];
4482 sequences[i] = sequences[j];
4483 sequences[j] = seq;
4484 nid = pins[i];
4485 pins[i] = pins[j];
4486 pins[j] = nid;
4487 }
4488 }
4489 }
4490}
4491
4492
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004493/* add the found input-pin to the cfg->inputs[] table */
4494static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4495 int type)
4496{
4497 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4498 cfg->inputs[cfg->num_inputs].pin = nid;
4499 cfg->inputs[cfg->num_inputs].type = type;
4500 cfg->num_inputs++;
4501 }
4502}
4503
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004504/* sort inputs in the order of AUTO_PIN_* type */
4505static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4506{
4507 int i, j;
4508
4509 for (i = 0; i < cfg->num_inputs; i++) {
4510 for (j = i + 1; j < cfg->num_inputs; j++) {
4511 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4512 struct auto_pin_cfg_item tmp;
4513 tmp = cfg->inputs[i];
4514 cfg->inputs[i] = cfg->inputs[j];
4515 cfg->inputs[j] = tmp;
4516 }
4517 }
4518 }
4519}
4520
Takashi Iwai82bc9552006-03-21 11:24:42 +01004521/*
4522 * Parse all pin widgets and store the useful pin nids to cfg
4523 *
4524 * The number of line-outs or any primary output is stored in line_outs,
4525 * and the corresponding output pins are assigned to line_out_pins[],
4526 * in the order of front, rear, CLFE, side, ...
4527 *
4528 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004529 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004530 * is detected, one of speaker of HP pins is assigned as the primary
4531 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4532 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004533 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004534 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004535 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4536 * respectively.
4537 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004538int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4539 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004540 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004541{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004542 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004543 short seq, assoc_line_out, assoc_speaker;
4544 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4545 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004546 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004547 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004548
4549 memset(cfg, 0, sizeof(*cfg));
4550
Steve Longerbeam81937d32007-05-08 15:33:03 +02004551 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4552 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004553 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004554 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004555
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004556 end_nid = codec->start_nid + codec->num_nodes;
4557 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004558 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004559 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004560 unsigned int def_conf;
4561 short assoc, loc;
4562
4563 /* read all default configuration for pin complex */
4564 if (wid_type != AC_WID_PIN)
4565 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004566 /* ignore the given nids (e.g. pc-beep returns error) */
4567 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4568 continue;
4569
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004570 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004571 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4572 continue;
4573 loc = get_defcfg_location(def_conf);
4574 switch (get_defcfg_device(def_conf)) {
4575 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004576 seq = get_defcfg_sequence(def_conf);
4577 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004578
4579 if (!(wid_caps & AC_WCAP_STEREO))
4580 if (!cfg->mono_out_pin)
4581 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004582 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004583 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004584 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004585 assoc_line_out = assoc;
4586 else if (assoc_line_out != assoc)
4587 continue;
4588 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4589 continue;
4590 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004591 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004592 cfg->line_outs++;
4593 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004594 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004595 seq = get_defcfg_sequence(def_conf);
4596 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004597 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004598 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004599 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004600 assoc_speaker = assoc;
4601 else if (assoc_speaker != assoc)
4602 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004603 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4604 continue;
4605 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004606 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004607 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004608 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004609 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004610 seq = get_defcfg_sequence(def_conf);
4611 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004612 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4613 continue;
4614 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004615 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004616 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004617 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004618 case AC_JACK_MIC_IN:
4619 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004620 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004621 case AC_JACK_LINE_IN:
4622 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004623 break;
4624 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004625 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004626 break;
4627 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004628 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004629 break;
4630 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004631 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004632 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4633 continue;
4634 cfg->dig_out_pins[cfg->dig_outs] = nid;
4635 cfg->dig_out_type[cfg->dig_outs] =
4636 (loc == AC_JACK_LOC_HDMI) ?
4637 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4638 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004639 break;
4640 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004641 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004642 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004643 if (loc == AC_JACK_LOC_HDMI)
4644 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4645 else
4646 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004647 break;
4648 }
4649 }
4650
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004651 /* FIX-UP:
4652 * If no line-out is defined but multiple HPs are found,
4653 * some of them might be the real line-outs.
4654 */
4655 if (!cfg->line_outs && cfg->hp_outs > 1) {
4656 int i = 0;
4657 while (i < cfg->hp_outs) {
4658 /* The real HPs should have the sequence 0x0f */
4659 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4660 i++;
4661 continue;
4662 }
4663 /* Move it to the line-out table */
4664 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4665 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4666 cfg->line_outs++;
4667 cfg->hp_outs--;
4668 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4669 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004670 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004671 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4672 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004673 memset(cfg->hp_pins + cfg->hp_outs, 0,
4674 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004675 if (!cfg->hp_outs)
4676 cfg->line_out_type = AUTO_PIN_HP_OUT;
4677
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004678 }
4679
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004680 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004681 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4682 cfg->line_outs);
4683 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4684 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004685 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4686 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004687
Steve Longerbeam81937d32007-05-08 15:33:03 +02004688 /*
4689 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4690 * as a primary output
4691 */
4692 if (!cfg->line_outs) {
4693 if (cfg->speaker_outs) {
4694 cfg->line_outs = cfg->speaker_outs;
4695 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4696 sizeof(cfg->speaker_pins));
4697 cfg->speaker_outs = 0;
4698 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4699 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4700 } else if (cfg->hp_outs) {
4701 cfg->line_outs = cfg->hp_outs;
4702 memcpy(cfg->line_out_pins, cfg->hp_pins,
4703 sizeof(cfg->hp_pins));
4704 cfg->hp_outs = 0;
4705 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4706 cfg->line_out_type = AUTO_PIN_HP_OUT;
4707 }
4708 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004709
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004710 /* Reorder the surround channels
4711 * ALSA sequence is front/surr/clfe/side
4712 * HDA sequence is:
4713 * 4-ch: front/surr => OK as it is
4714 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004715 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004716 */
4717 switch (cfg->line_outs) {
4718 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004719 case 4:
4720 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004721 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004722 cfg->line_out_pins[2] = nid;
4723 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004724 }
4725
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004726 sort_autocfg_input_pins(cfg);
4727
Takashi Iwai82bc9552006-03-21 11:24:42 +01004728 /*
4729 * debug prints of the parsed results
4730 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004731 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 +01004732 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4733 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004734 cfg->line_out_pins[4],
4735 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4736 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4737 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004738 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4739 cfg->speaker_outs, cfg->speaker_pins[0],
4740 cfg->speaker_pins[1], cfg->speaker_pins[2],
4741 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004742 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4743 cfg->hp_outs, cfg->hp_pins[0],
4744 cfg->hp_pins[1], cfg->hp_pins[2],
4745 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004746 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004747 if (cfg->dig_outs)
4748 snd_printd(" dig-out=0x%x/0x%x\n",
4749 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004750 snd_printd(" inputs:");
4751 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004752 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004753 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004754 cfg->inputs[i].pin);
4755 }
4756 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004757 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004758 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004759
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004760 return 0;
4761}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004762EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004763
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004764int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004765{
4766 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004767 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004768 if (conn == AC_JACK_PORT_NONE)
4769 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004770 /* Windows may claim the internal mic to be BOTH, too */
4771 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004772 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004773 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004774 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004775 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004776 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004777 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004778 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004779 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004780 return INPUT_PIN_ATTR_FRONT;
4781 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004782}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004783EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004784
Takashi Iwai990061c2010-09-09 22:08:44 +02004785/**
4786 * hda_get_input_pin_label - Give a label for the given input pin
4787 *
4788 * When check_location is true, the function checks the pin location
4789 * for mic and line-in pins, and set an appropriate prefix like "Front",
4790 * "Rear", "Internal".
4791 */
4792
Takashi Iwai10a20af2010-09-09 16:28:02 +02004793const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4794 int check_location)
4795{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004796 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004797 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004798 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4799 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004800 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004801
Takashi Iwai10a20af2010-09-09 16:28:02 +02004802 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004803
4804 switch (get_defcfg_device(def_conf)) {
4805 case AC_JACK_MIC_IN:
4806 if (!check_location)
4807 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004808 attr = snd_hda_get_input_pin_attr(def_conf);
4809 if (!attr)
4810 return "None";
4811 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004812 case AC_JACK_LINE_IN:
4813 if (!check_location)
4814 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004815 attr = snd_hda_get_input_pin_attr(def_conf);
4816 if (!attr)
4817 return "None";
4818 if (attr == INPUT_PIN_ATTR_DOCK)
4819 return "Dock Line";
4820 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004821 case AC_JACK_AUX:
4822 return "Aux";
4823 case AC_JACK_CD:
4824 return "CD";
4825 case AC_JACK_SPDIF_IN:
4826 return "SPDIF In";
4827 case AC_JACK_DIG_OTHER_IN:
4828 return "Digital In";
4829 default:
4830 return "Misc";
4831 }
4832}
4833EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4834
Takashi Iwaia1c98512010-09-09 21:36:27 +02004835/* Check whether the location prefix needs to be added to the label.
4836 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4837 * have to put "Front" prefix to each label. In such a case, returns false.
4838 */
4839static int check_mic_location_need(struct hda_codec *codec,
4840 const struct auto_pin_cfg *cfg,
4841 int input)
4842{
4843 unsigned int defc;
4844 int i, attr, attr2;
4845
4846 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004847 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004848 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004849 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004850 return 1;
4851
4852 attr = 0;
4853 for (i = 0; i < cfg->num_inputs; i++) {
4854 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004855 attr2 = snd_hda_get_input_pin_attr(defc);
4856 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004857 if (attr && attr != attr2)
4858 return 1; /* different locations found */
4859 attr = attr2;
4860 }
4861 }
4862 return 0;
4863}
4864
Takashi Iwai990061c2010-09-09 22:08:44 +02004865/**
4866 * hda_get_autocfg_input_label - Get a label for the given input
4867 *
4868 * Get a label for the given input pin defined by the autocfg item.
4869 * Unlike hda_get_input_pin_label(), this function checks all inputs
4870 * defined in autocfg and avoids the redundant mic/line prefix as much as
4871 * possible.
4872 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004873const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4874 const struct auto_pin_cfg *cfg,
4875 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004876{
4877 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004878 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004879
Takashi Iwai10a20af2010-09-09 16:28:02 +02004880 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4881 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4882 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004883 if (has_multiple_pins && type == AUTO_PIN_MIC)
4884 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004885 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4886 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004887}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004888EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4889
Takashi Iwai990061c2010-09-09 22:08:44 +02004890/**
4891 * snd_hda_add_imux_item - Add an item to input_mux
4892 *
4893 * When the same label is used already in the existing items, the number
4894 * suffix is appended to the label. This label index number is stored
4895 * to type_idx when non-NULL pointer is given.
4896 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004897int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4898 int index, int *type_idx)
4899{
4900 int i, label_idx = 0;
4901 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4902 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4903 return -EINVAL;
4904 }
4905 for (i = 0; i < imux->num_items; i++) {
4906 if (!strncmp(label, imux->items[i].label, strlen(label)))
4907 label_idx++;
4908 }
4909 if (type_idx)
4910 *type_idx = label_idx;
4911 if (label_idx > 0)
4912 snprintf(imux->items[imux->num_items].label,
4913 sizeof(imux->items[imux->num_items].label),
4914 "%s %d", label, label_idx);
4915 else
4916 strlcpy(imux->items[imux->num_items].label, label,
4917 sizeof(imux->items[imux->num_items].label));
4918 imux->items[imux->num_items].index = index;
4919 imux->num_items++;
4920 return 0;
4921}
4922EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004923
Takashi Iwai4a471b72005-12-07 13:56:29 +01004924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925#ifdef CONFIG_PM
4926/*
4927 * power management
4928 */
4929
4930/**
4931 * snd_hda_suspend - suspend the codecs
4932 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 *
4934 * Returns 0 if successful.
4935 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004936int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004938 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Takashi Iwai0ba21762007-04-16 11:29:14 +02004940 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004941#ifdef CONFIG_SND_HDA_POWER_SAVE
4942 if (!codec->power_on)
4943 continue;
4944#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004945 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 }
4947 return 0;
4948}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004949EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
4951/**
4952 * snd_hda_resume - resume the codecs
4953 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 *
4955 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004956 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004957 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004958 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 */
4960int snd_hda_resume(struct hda_bus *bus)
4961{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004962 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
Takashi Iwai0ba21762007-04-16 11:29:14 +02004964 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004965 if (snd_hda_codec_needs_resume(codec))
4966 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 return 0;
4969}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004970EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004971#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004972
4973/*
4974 * generic arrays
4975 */
4976
Takashi Iwaid5191e52009-11-16 14:58:17 +01004977/**
4978 * snd_array_new - get a new element from the given array
4979 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004980 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004981 * Get a new element from the given array. If it exceeds the
4982 * pre-allocated array size, re-allocate the array.
4983 *
4984 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004985 */
4986void *snd_array_new(struct snd_array *array)
4987{
4988 if (array->used >= array->alloced) {
4989 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004990 void *nlist;
4991 if (snd_BUG_ON(num >= 4096))
4992 return NULL;
4993 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004994 if (!nlist)
4995 return NULL;
4996 if (array->list) {
4997 memcpy(nlist, array->list,
4998 array->elem_size * array->alloced);
4999 kfree(array->list);
5000 }
5001 array->list = nlist;
5002 array->alloced = num;
5003 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005004 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005005}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005006EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005007
Takashi Iwaid5191e52009-11-16 14:58:17 +01005008/**
5009 * snd_array_free - free the given array elements
5010 * @array: the array object
5011 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005012void snd_array_free(struct snd_array *array)
5013{
5014 kfree(array->list);
5015 array->used = 0;
5016 array->alloced = 0;
5017 array->list = NULL;
5018}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005019EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005020
Takashi Iwaid5191e52009-11-16 14:58:17 +01005021/**
5022 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5023 * @pcm: PCM caps bits
5024 * @buf: the string buffer to write
5025 * @buflen: the max buffer length
5026 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005027 * used by hda_proc.c and hda_eld.c
5028 */
5029void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5030{
5031 static unsigned int rates[] = {
5032 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5033 96000, 176400, 192000, 384000
5034 };
5035 int i, j;
5036
5037 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5038 if (pcm & (1 << i))
5039 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5040
5041 buf[j] = '\0'; /* necessary when j == 0 */
5042}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005043EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005044
Takashi Iwaid5191e52009-11-16 14:58:17 +01005045/**
5046 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5047 * @pcm: PCM caps bits
5048 * @buf: the string buffer to write
5049 * @buflen: the max buffer length
5050 *
5051 * used by hda_proc.c and hda_eld.c
5052 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005053void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5054{
5055 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5056 int i, j;
5057
5058 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5059 if (pcm & (AC_SUPPCM_BITS_8 << i))
5060 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5061
5062 buf[j] = '\0'; /* necessary when j == 0 */
5063}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005064EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005065
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005066#ifdef CONFIG_SND_HDA_INPUT_JACK
5067/*
5068 * Input-jack notification support
5069 */
5070struct hda_jack_item {
5071 hda_nid_t nid;
5072 int type;
5073 struct snd_jack *jack;
5074};
5075
5076static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5077 int type)
5078{
5079 switch (type) {
5080 case SND_JACK_HEADPHONE:
5081 return "Headphone";
5082 case SND_JACK_MICROPHONE:
5083 return "Mic";
5084 case SND_JACK_LINEOUT:
5085 return "Line-out";
5086 case SND_JACK_HEADSET:
5087 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005088 case SND_JACK_VIDEOOUT:
5089 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005090 default:
5091 return "Misc";
5092 }
5093}
5094
5095static void hda_free_jack_priv(struct snd_jack *jack)
5096{
5097 struct hda_jack_item *jacks = jack->private_data;
5098 jacks->nid = 0;
5099 jacks->jack = NULL;
5100}
5101
5102int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5103 const char *name)
5104{
5105 struct hda_jack_item *jack;
5106 int err;
5107
5108 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5109 jack = snd_array_new(&codec->jacks);
5110 if (!jack)
5111 return -ENOMEM;
5112
5113 jack->nid = nid;
5114 jack->type = type;
5115 if (!name)
5116 name = get_jack_default_name(codec, nid, type);
5117 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5118 if (err < 0) {
5119 jack->nid = 0;
5120 return err;
5121 }
5122 jack->jack->private_data = jack;
5123 jack->jack->private_free = hda_free_jack_priv;
5124 return 0;
5125}
5126EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5127
5128void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5129{
5130 struct hda_jack_item *jacks = codec->jacks.list;
5131 int i;
5132
5133 if (!jacks)
5134 return;
5135
5136 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5137 unsigned int pin_ctl;
5138 unsigned int present;
5139 int type;
5140
5141 if (jacks->nid != nid)
5142 continue;
5143 present = snd_hda_jack_detect(codec, nid);
5144 type = jacks->type;
5145 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5146 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5147 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5148 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5149 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5150 }
5151 snd_jack_report(jacks->jack, present ? type : 0);
5152 }
5153}
5154EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5155
5156/* free jack instances manually when clearing/reconfiguring */
5157void snd_hda_input_jack_free(struct hda_codec *codec)
5158{
5159 if (!codec->bus->shutdown && codec->jacks.list) {
5160 struct hda_jack_item *jacks = codec->jacks.list;
5161 int i;
5162 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5163 if (jacks->jack)
5164 snd_device_free(codec->bus->card, jacks->jack);
5165 }
5166 }
5167 snd_array_free(&codec->jacks);
5168}
5169EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5170#endif /* CONFIG_SND_HDA_INPUT_JACK */
5171
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005172MODULE_DESCRIPTION("HDA codec core");
5173MODULE_LICENSE("GPL");