blob: 67d341fa7d10c3cef3e582621c735b9db6123339 [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 Iwaic8b6bf9b2005-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 Iwaib94d3532008-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 Iwaic8b6bf9b2005-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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 codec->bus->caddr_tbl[codec->addr] = NULL;
1087 if (codec->patch_ops.free)
1088 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001089 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001090 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001091 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001092 kfree(codec->vendor_name);
1093 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001094 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001095 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kfree(codec);
1097}
1098
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001099static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1100 unsigned int power_state);
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/**
1103 * snd_hda_codec_new - create a HDA codec
1104 * @bus: the bus to assign
1105 * @codec_addr: the codec address
1106 * @codecp: the pointer to store the generated codec
1107 *
1108 * Returns 0 if successful, or a negative error code.
1109 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001110int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1111 unsigned int codec_addr,
1112 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001115 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 int err;
1117
Takashi Iwaida3cec32008-08-08 17:12:14 +02001118 if (snd_BUG_ON(!bus))
1119 return -EINVAL;
1120 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1121 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001124 snd_printk(KERN_ERR "hda_codec: "
1125 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return -EBUSY;
1127 }
1128
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001129 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (codec == NULL) {
1131 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1132 return -ENOMEM;
1133 }
1134
1135 codec->bus = bus;
1136 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001137 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001138 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001139 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001140 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001141 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1142 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001143 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001144 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001145 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001146 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001147 if (codec->bus->modelname) {
1148 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1149 if (!codec->modelname) {
1150 snd_hda_codec_free(codec);
1151 return -ENODEV;
1152 }
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Takashi Iwaicb53c622007-08-10 17:21:45 +02001155#ifdef CONFIG_SND_HDA_POWER_SAVE
1156 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1157 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1158 * the caller has to power down appropriatley after initialization
1159 * phase.
1160 */
1161 hda_keep_power_on(codec);
1162#endif
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 list_add_tail(&codec->list, &bus->codec_list);
1165 bus->caddr_tbl[codec_addr] = codec;
1166
Takashi Iwai0ba21762007-04-16 11:29:14 +02001167 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1168 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001169 if (codec->vendor_id == -1)
1170 /* read again, hopefully the access method was corrected
1171 * in the last read...
1172 */
1173 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1174 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001175 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1176 AC_PAR_SUBSYSTEM_ID);
1177 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1178 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001180 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001181 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001182 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001183 err = -ENODEV;
1184 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
Takashi Iwai3be14142009-02-20 14:11:16 +01001187 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1188 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001189 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001190 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001191 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001192 err = read_pin_defaults(codec);
1193 if (err < 0)
1194 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001195
Takashi Iwai0ba21762007-04-16 11:29:14 +02001196 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001197 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001198 codec->subsystem_id =
1199 snd_hda_codec_read(codec, nid, 0,
1200 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001201 }
1202
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001203 /* power-up all before initialization */
1204 hda_set_power_state(codec,
1205 codec->afg ? codec->afg : codec->mfg,
1206 AC_PWRST_D0);
1207
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001208 snd_hda_codec_proc_new(codec);
1209
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001210 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001211
1212 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1213 codec->subsystem_id, codec->revision_id);
1214 snd_component_add(codec->bus->card, component);
1215
1216 if (codecp)
1217 *codecp = codec;
1218 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001219
1220 error:
1221 snd_hda_codec_free(codec);
1222 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001223}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001224EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001225
Takashi Iwaid5191e52009-11-16 14:58:17 +01001226/**
1227 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1228 * @codec: the HDA codec
1229 *
1230 * Start parsing of the given codec tree and (re-)initialize the whole
1231 * patch instance.
1232 *
1233 * Returns 0 if successful or a negative error code.
1234 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001235int snd_hda_codec_configure(struct hda_codec *codec)
1236{
1237 int err;
1238
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001239 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001240 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001241 err = get_codec_name(codec);
1242 if (err < 0)
1243 return err;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Takashi Iwai82467612007-07-27 19:15:54 +02001246 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001248 goto patched;
1249 }
Takashi Iwai82467612007-07-27 19:15:54 +02001250 if (codec->preset && codec->preset->patch) {
1251 err = codec->preset->patch(codec);
1252 goto patched;
1253 }
1254
1255 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001256 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001257 if (err < 0)
1258 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001259
1260 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001261 if (!err && codec->patch_ops.unsol_event)
1262 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001263 /* audio codec should override the mixer name */
1264 if (!err && (codec->afg || !*codec->bus->card->mixername))
1265 snprintf(codec->bus->card->mixername,
1266 sizeof(codec->bus->card->mixername),
1267 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001268 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001270EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272/**
1273 * snd_hda_codec_setup_stream - set up the codec for streaming
1274 * @codec: the CODEC to set up
1275 * @nid: the NID to set up
1276 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1277 * @channel_id: channel id to pass, zero based.
1278 * @format: stream format.
1279 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001280void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1281 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 int channel_id, int format)
1283{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001284 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001285 struct hda_cvt_setup *p;
1286 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001287 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001288 int i;
1289
Takashi Iwai0ba21762007-04-16 11:29:14 +02001290 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001291 return;
1292
Takashi Iwai0ba21762007-04-16 11:29:14 +02001293 snd_printdd("hda_codec_setup_stream: "
1294 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001296 p = get_hda_cvt_setup(codec, nid);
1297 if (!p)
1298 return;
1299 /* update the stream-id if changed */
1300 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1301 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1302 newval = (stream_tag << 4) | channel_id;
1303 if (oldval != newval)
1304 snd_hda_codec_write(codec, nid, 0,
1305 AC_VERB_SET_CHANNEL_STREAMID,
1306 newval);
1307 p->stream_tag = stream_tag;
1308 p->channel_id = channel_id;
1309 }
1310 /* update the format-id if changed */
1311 if (p->format_id != format) {
1312 oldval = snd_hda_codec_read(codec, nid, 0,
1313 AC_VERB_GET_STREAM_FORMAT, 0);
1314 if (oldval != format) {
1315 msleep(1);
1316 snd_hda_codec_write(codec, nid, 0,
1317 AC_VERB_SET_STREAM_FORMAT,
1318 format);
1319 }
1320 p->format_id = format;
1321 }
1322 p->active = 1;
1323 p->dirty = 0;
1324
1325 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001326 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001327 list_for_each_entry(c, &codec->bus->codec_list, list) {
1328 for (i = 0; i < c->cvt_setups.used; i++) {
1329 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001330 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson3fe10cf2012-02-01 12:05:41 +01001331 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001332 p->dirty = 1;
1333 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001336EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Takashi Iwaif0cea792010-08-13 11:56:53 +02001338static void really_cleanup_stream(struct hda_codec *codec,
1339 struct hda_cvt_setup *q);
1340
Takashi Iwaid5191e52009-11-16 14:58:17 +01001341/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001342 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001343 * @codec: the CODEC to clean up
1344 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001345 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001346 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001347void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1348 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001349{
Takashi Iwaieb541332010-08-06 13:48:11 +02001350 struct hda_cvt_setup *p;
1351
Takashi Iwai888afa12008-03-18 09:57:50 +01001352 if (!nid)
1353 return;
1354
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001355 if (codec->no_sticky_stream)
1356 do_now = 1;
1357
Takashi Iwai888afa12008-03-18 09:57:50 +01001358 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001359 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001360 if (p) {
1361 /* here we just clear the active flag when do_now isn't set;
1362 * actual clean-ups will be done later in
1363 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1364 */
1365 if (do_now)
1366 really_cleanup_stream(codec, p);
1367 else
1368 p->active = 0;
1369 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001370}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001371EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001372
Takashi Iwaieb541332010-08-06 13:48:11 +02001373static void really_cleanup_stream(struct hda_codec *codec,
1374 struct hda_cvt_setup *q)
1375{
1376 hda_nid_t nid = q->nid;
1377 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1378 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1379 memset(q, 0, sizeof(*q));
1380 q->nid = nid;
1381}
1382
1383/* clean up the all conflicting obsolete streams */
1384static void purify_inactive_streams(struct hda_codec *codec)
1385{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001386 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001387 int i;
1388
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001389 list_for_each_entry(c, &codec->bus->codec_list, list) {
1390 for (i = 0; i < c->cvt_setups.used; i++) {
1391 struct hda_cvt_setup *p;
1392 p = snd_array_elem(&c->cvt_setups, i);
1393 if (p->dirty)
1394 really_cleanup_stream(c, p);
1395 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001396 }
1397}
1398
Mike Waychison1c7276c2011-04-20 12:04:36 -07001399#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaieb541332010-08-06 13:48:11 +02001400/* clean up all streams; called from suspend */
1401static void hda_cleanup_all_streams(struct hda_codec *codec)
1402{
1403 int i;
1404
1405 for (i = 0; i < codec->cvt_setups.used; i++) {
1406 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1407 if (p->stream_tag)
1408 really_cleanup_stream(codec, p);
1409 }
1410}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001411#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/*
1414 * amp access functions
1415 */
1416
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001417/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001418#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001419#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001420#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1421#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001423#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001426static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001427 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Takashi Iwai01751f52007-08-10 16:59:39 +02001429 memset(cache, 0, sizeof(*cache));
1430 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001431 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001432}
1433
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001434static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001435{
Takashi Iwai603c4012008-07-30 15:01:44 +02001436 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001440static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Takashi Iwai01751f52007-08-10 16:59:39 +02001442 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1443 u16 cur = cache->hash[idx];
1444 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001447 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (info->key == key)
1449 return info;
1450 cur = info->next;
1451 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001452 return NULL;
1453}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001455/* query the hash. allocate an entry if not found. */
1456static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1457 u32 key)
1458{
1459 struct hda_cache_head *info = get_hash(cache, key);
1460 if (!info) {
1461 u16 idx, cur;
1462 /* add a new hash entry */
1463 info = snd_array_new(&cache->buf);
1464 if (!info)
1465 return NULL;
1466 cur = snd_array_index(&cache->buf, info);
1467 info->key = key;
1468 info->val = 0;
1469 idx = key % (u16)ARRAY_SIZE(cache->hash);
1470 info->next = cache->hash[idx];
1471 cache->hash[idx] = cur;
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return info;
1474}
1475
Takashi Iwai01751f52007-08-10 16:59:39 +02001476/* query and allocate an amp hash entry */
1477static inline struct hda_amp_info *
1478get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1479{
1480 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1481}
1482
Takashi Iwaid5191e52009-11-16 14:58:17 +01001483/**
1484 * query_amp_caps - query AMP capabilities
1485 * @codec: the HD-auio codec
1486 * @nid: the NID to query
1487 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1488 *
1489 * Query AMP capabilities for the given widget and direction.
1490 * Returns the obtained capability bits.
1491 *
1492 * When cap bits have been already read, this doesn't read again but
1493 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001495u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001497 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Takashi Iwai0ba21762007-04-16 11:29:14 +02001499 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1500 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001502 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001503 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001505 info->amp_caps = snd_hda_param_read(codec, nid,
1506 direction == HDA_OUTPUT ?
1507 AC_PAR_AMP_OUT_CAP :
1508 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001509 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001510 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 return info->amp_caps;
1513}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001514EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Takashi Iwaid5191e52009-11-16 14:58:17 +01001516/**
1517 * snd_hda_override_amp_caps - Override the AMP capabilities
1518 * @codec: the CODEC to clean up
1519 * @nid: the NID to clean up
1520 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1521 * @caps: the capability bits to set
1522 *
1523 * Override the cached AMP caps bits value by the given one.
1524 * This function is useful if the driver needs to adjust the AMP ranges,
1525 * e.g. limit to 0dB, etc.
1526 *
1527 * Returns zero if successful or a negative error code.
1528 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001529int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1530 unsigned int caps)
1531{
1532 struct hda_amp_info *info;
1533
1534 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1535 if (!info)
1536 return -EINVAL;
1537 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001538 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001539 return 0;
1540}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001541EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001542
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001543static unsigned int
1544query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1545 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001546{
1547 struct hda_amp_info *info;
1548
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001549 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001550 if (!info)
1551 return 0;
1552 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001553 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001554 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001555 }
1556 return info->amp_caps;
1557}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001558
1559static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1560{
1561 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1562}
1563
Takashi Iwaid5191e52009-11-16 14:58:17 +01001564/**
1565 * snd_hda_query_pin_caps - Query PIN capabilities
1566 * @codec: the HD-auio codec
1567 * @nid: the NID to query
1568 *
1569 * Query PIN capabilities for the given widget.
1570 * Returns the obtained capability bits.
1571 *
1572 * When cap bits have been already read, this doesn't read again but
1573 * returns the cached value.
1574 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001575u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1576{
1577 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1578 read_pin_cap);
1579}
Takashi Iwai1327a322009-03-23 13:07:47 +01001580EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1581
Wu Fengguang864f92b2009-11-18 12:38:02 +08001582/**
1583 * snd_hda_pin_sense - execute pin sense measurement
1584 * @codec: the CODEC to sense
1585 * @nid: the pin NID to sense
1586 *
1587 * Execute necessary pin sense measurement and return its Presence Detect,
1588 * Impedance, ELD Valid etc. status bits.
1589 */
1590u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1591{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001592 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001593
Takashi Iwai729d55b2009-12-25 22:49:01 +01001594 if (!codec->no_trigger_sense) {
1595 pincap = snd_hda_query_pin_caps(codec, nid);
1596 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001597 snd_hda_codec_read(codec, nid, 0,
1598 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001599 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001600 return snd_hda_codec_read(codec, nid, 0,
1601 AC_VERB_GET_PIN_SENSE, 0);
1602}
1603EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1604
1605/**
1606 * snd_hda_jack_detect - query pin Presence Detect status
1607 * @codec: the CODEC to sense
1608 * @nid: the pin NID to sense
1609 *
1610 * Query and return the pin's Presence Detect status.
1611 */
1612int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1613{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001614 u32 sense = snd_hda_pin_sense(codec, nid);
1615 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001616}
1617EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619/*
1620 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001621 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001623static unsigned int get_vol_mute(struct hda_codec *codec,
1624 struct hda_amp_info *info, hda_nid_t nid,
1625 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 u32 val, parm;
1628
Takashi Iwai01751f52007-08-10 16:59:39 +02001629 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001630 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1633 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1634 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001635 val = snd_hda_codec_read(codec, nid, 0,
1636 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001638 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001639 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
1642/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001643 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001645static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001646 hda_nid_t nid, int ch, int direction, int index,
1647 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 u32 parm;
1650
1651 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1652 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1653 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1654 parm |= val;
1655 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001656 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Takashi Iwaid5191e52009-11-16 14:58:17 +01001659/**
1660 * snd_hda_codec_amp_read - Read AMP value
1661 * @codec: HD-audio codec
1662 * @nid: NID to read the AMP value
1663 * @ch: channel (left=0 or right=1)
1664 * @direction: #HDA_INPUT or #HDA_OUTPUT
1665 * @index: the index value (only for input direction)
1666 *
1667 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Takashi Iwai834be882006-03-01 14:16:17 +01001669int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1670 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001672 struct hda_amp_info *info;
1673 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1674 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001676 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001678EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Takashi Iwaid5191e52009-11-16 14:58:17 +01001680/**
1681 * snd_hda_codec_amp_update - update the AMP value
1682 * @codec: HD-audio codec
1683 * @nid: NID to read the AMP value
1684 * @ch: channel (left=0 or right=1)
1685 * @direction: #HDA_INPUT or #HDA_OUTPUT
1686 * @idx: the index value (only for input direction)
1687 * @mask: bit mask to set
1688 * @val: the bits value to set
1689 *
1690 * Update the AMP value with a bit mask.
1691 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001692 */
Takashi Iwai834be882006-03-01 14:16:17 +01001693int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1694 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001696 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001697
Takashi Iwai0ba21762007-04-16 11:29:14 +02001698 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1699 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return 0;
Takashi Iwai46712642010-03-29 09:19:38 +02001701 if (snd_BUG_ON(mask & ~0xff))
1702 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001703 val &= mask;
1704 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001705 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001707 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return 1;
1709}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001710EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Takashi Iwaid5191e52009-11-16 14:58:17 +01001712/**
1713 * snd_hda_codec_amp_stereo - update the AMP stereo values
1714 * @codec: HD-audio codec
1715 * @nid: NID to read the AMP value
1716 * @direction: #HDA_INPUT or #HDA_OUTPUT
1717 * @idx: the index value (only for input direction)
1718 * @mask: bit mask to set
1719 * @val: the bits value to set
1720 *
1721 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1722 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001723 */
1724int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1725 int direction, int idx, int mask, int val)
1726{
1727 int ch, ret = 0;
Takashi Iwai46712642010-03-29 09:19:38 +02001728
1729 if (snd_BUG_ON(mask & ~0xff))
1730 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001731 for (ch = 0; ch < 2; ch++)
1732 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1733 idx, mask, val);
1734 return ret;
1735}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001736EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001737
Takashi Iwaicb53c622007-08-10 17:21:45 +02001738#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001739/**
1740 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1741 * @codec: HD-audio codec
1742 *
1743 * Resume the all amp commands from the cache.
1744 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001745void snd_hda_codec_resume_amp(struct hda_codec *codec)
1746{
Takashi Iwai603c4012008-07-30 15:01:44 +02001747 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001748 int i;
1749
Takashi Iwai603c4012008-07-30 15:01:44 +02001750 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001751 u32 key = buffer->head.key;
1752 hda_nid_t nid;
1753 unsigned int idx, dir, ch;
1754 if (!key)
1755 continue;
1756 nid = key & 0xff;
1757 idx = (key >> 16) & 0xff;
1758 dir = (key >> 24) & 0xff;
1759 for (ch = 0; ch < 2; ch++) {
1760 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1761 continue;
1762 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1763 buffer->vol[ch]);
1764 }
1765 }
1766}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001767EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001768#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001770static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1771 unsigned int ofs)
1772{
1773 u32 caps = query_amp_caps(codec, nid, dir);
1774 /* get num steps */
1775 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1776 if (ofs < caps)
1777 caps -= ofs;
1778 return caps;
1779}
1780
Takashi Iwaid5191e52009-11-16 14:58:17 +01001781/**
1782 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1783 *
1784 * The control element is supposed to have the private_value field
1785 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1786 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001787int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1788 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1791 u16 nid = get_amp_nid(kcontrol);
1792 u8 chs = get_amp_channels(kcontrol);
1793 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001794 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001796 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1797 uinfo->count = chs == 3 ? 2 : 1;
1798 uinfo->value.integer.min = 0;
1799 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1800 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001801 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001802 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1803 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return -EINVAL;
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return 0;
1807}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001808EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001810
1811static inline unsigned int
1812read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1813 int ch, int dir, int idx, unsigned int ofs)
1814{
1815 unsigned int val;
1816 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1817 val &= HDA_AMP_VOLMASK;
1818 if (val >= ofs)
1819 val -= ofs;
1820 else
1821 val = 0;
1822 return val;
1823}
1824
1825static inline int
1826update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1827 int ch, int dir, int idx, unsigned int ofs,
1828 unsigned int val)
1829{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001830 unsigned int maxval;
1831
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001832 if (val > 0)
1833 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001834 /* ofs = 0: raw max value */
1835 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001836 if (val > maxval)
1837 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001838 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1839 HDA_AMP_VOLMASK, val);
1840}
1841
Takashi Iwaid5191e52009-11-16 14:58:17 +01001842/**
1843 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1844 *
1845 * The control element is supposed to have the private_value field
1846 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1847 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001848int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1849 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1852 hda_nid_t nid = get_amp_nid(kcontrol);
1853 int chs = get_amp_channels(kcontrol);
1854 int dir = get_amp_direction(kcontrol);
1855 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001856 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 long *valp = ucontrol->value.integer.value;
1858
1859 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001860 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001862 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return 0;
1864}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001865EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Takashi Iwaid5191e52009-11-16 14:58:17 +01001867/**
1868 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1869 *
1870 * The control element is supposed to have the private_value field
1871 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1872 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001873int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1874 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1877 hda_nid_t nid = get_amp_nid(kcontrol);
1878 int chs = get_amp_channels(kcontrol);
1879 int dir = get_amp_direction(kcontrol);
1880 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001881 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 long *valp = ucontrol->value.integer.value;
1883 int change = 0;
1884
Takashi Iwaicb53c622007-08-10 17:21:45 +02001885 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001886 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001887 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001888 valp++;
1889 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001890 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001891 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001892 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return change;
1894}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001895EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Takashi Iwaid5191e52009-11-16 14:58:17 +01001897/**
1898 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1899 *
1900 * The control element is supposed to have the private_value field
1901 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1902 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001903int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1904 unsigned int size, unsigned int __user *_tlv)
1905{
1906 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1907 hda_nid_t nid = get_amp_nid(kcontrol);
1908 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001909 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001910 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001911 u32 caps, val1, val2;
1912
1913 if (size < 4 * sizeof(unsigned int))
1914 return -ENOMEM;
1915 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001916 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1917 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001918 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001919 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001920 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001921 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02001922 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001923 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1924 return -EFAULT;
1925 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1926 return -EFAULT;
1927 if (put_user(val1, _tlv + 2))
1928 return -EFAULT;
1929 if (put_user(val2, _tlv + 3))
1930 return -EFAULT;
1931 return 0;
1932}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001933EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001934
Takashi Iwaid5191e52009-11-16 14:58:17 +01001935/**
1936 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1937 * @codec: HD-audio codec
1938 * @nid: NID of a reference widget
1939 * @dir: #HDA_INPUT or #HDA_OUTPUT
1940 * @tlv: TLV data to be stored, at least 4 elements
1941 *
1942 * Set (static) TLV data for a virtual master volume using the AMP caps
1943 * obtained from the reference NID.
1944 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001945 */
1946void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1947 unsigned int *tlv)
1948{
1949 u32 caps;
1950 int nums, step;
1951
1952 caps = query_amp_caps(codec, nid, dir);
1953 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1954 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1955 step = (step + 1) * 25;
1956 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1957 tlv[1] = 2 * sizeof(unsigned int);
1958 tlv[2] = -nums * step;
1959 tlv[3] = step;
1960}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001961EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001962
1963/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001964static struct snd_kcontrol *
1965_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1966 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001967{
1968 struct snd_ctl_elem_id id;
1969 memset(&id, 0, sizeof(id));
1970 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001971 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001972 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1973 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001974 strcpy(id.name, name);
1975 return snd_ctl_find_id(codec->bus->card, &id);
1976}
1977
Takashi Iwaid5191e52009-11-16 14:58:17 +01001978/**
1979 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1980 * @codec: HD-audio codec
1981 * @name: ctl id name string
1982 *
1983 * Get the control element with the given id string and IFACE_MIXER.
1984 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001985struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1986 const char *name)
1987{
1988 return _snd_hda_find_mixer_ctl(codec, name, 0);
1989}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001990EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001991
Takashi Iwai1afe2062010-12-23 10:17:52 +01001992static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
1993{
1994 int idx;
1995 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
1996 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
1997 return idx;
1998 }
1999 return -EBUSY;
2000}
2001
Takashi Iwaid5191e52009-11-16 14:58:17 +01002002/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002003 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002004 * @codec: HD-audio codec
2005 * @nid: corresponding NID (optional)
2006 * @kctl: the control element to assign
2007 *
2008 * Add the given control element to an array inside the codec instance.
2009 * All control elements belonging to a codec are supposed to be added
2010 * by this function so that a proper clean-up works at the free or
2011 * reconfiguration time.
2012 *
2013 * If non-zero @nid is passed, the NID is assigned to the control element.
2014 * The assignment is shown in the codec proc file.
2015 *
2016 * snd_hda_ctl_add() checks the control subdev id field whether
2017 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002018 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2019 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002020 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002021int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2022 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002023{
2024 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002025 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002026 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002027
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002028 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002029 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002030 if (nid == 0)
2031 nid = get_amp_nid_(kctl->private_value);
2032 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002033 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2034 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002035 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002036 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002037 err = snd_ctl_add(codec->bus->card, kctl);
2038 if (err < 0)
2039 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002040 item = snd_array_new(&codec->mixers);
2041 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002042 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002043 item->kctl = kctl;
2044 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002045 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002046 return 0;
2047}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002048EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002049
Takashi Iwaid5191e52009-11-16 14:58:17 +01002050/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002051 * snd_hda_add_nid - Assign a NID to a control element
2052 * @codec: HD-audio codec
2053 * @nid: corresponding NID (optional)
2054 * @kctl: the control element to assign
2055 * @index: index to kctl
2056 *
2057 * Add the given control element to an array inside the codec instance.
2058 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2059 * NID:KCTL mapping - for example "Capture Source" selector.
2060 */
2061int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2062 unsigned int index, hda_nid_t nid)
2063{
2064 struct hda_nid_item *item;
2065
2066 if (nid > 0) {
2067 item = snd_array_new(&codec->nids);
2068 if (!item)
2069 return -ENOMEM;
2070 item->kctl = kctl;
2071 item->index = index;
2072 item->nid = nid;
2073 return 0;
2074 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002075 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2076 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002077 return -EINVAL;
2078}
2079EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2080
2081/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002082 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2083 * @codec: HD-audio codec
2084 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002085void snd_hda_ctls_clear(struct hda_codec *codec)
2086{
2087 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002088 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002089 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002090 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002091 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002092 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002093}
2094
Takashi Iwaia65d6292009-02-23 16:57:04 +01002095/* pseudo device locking
2096 * toggle card->shutdown to allow/disallow the device access (as a hack)
2097 */
2098static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002099{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002100 spin_lock(&card->files_lock);
2101 if (card->shutdown) {
2102 spin_unlock(&card->files_lock);
2103 return -EINVAL;
2104 }
2105 card->shutdown = 1;
2106 spin_unlock(&card->files_lock);
2107 return 0;
2108}
2109
2110static void hda_unlock_devices(struct snd_card *card)
2111{
2112 spin_lock(&card->files_lock);
2113 card->shutdown = 0;
2114 spin_unlock(&card->files_lock);
2115}
2116
Takashi Iwaid5191e52009-11-16 14:58:17 +01002117/**
2118 * snd_hda_codec_reset - Clear all objects assigned to the codec
2119 * @codec: HD-audio codec
2120 *
2121 * This frees the all PCM and control elements assigned to the codec, and
2122 * clears the caches and restores the pin default configurations.
2123 *
2124 * When a device is being used, it returns -EBSY. If successfully freed,
2125 * returns zero.
2126 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002127int snd_hda_codec_reset(struct hda_codec *codec)
2128{
2129 struct snd_card *card = codec->bus->card;
2130 int i, pcm;
2131
2132 if (hda_lock_devices(card) < 0)
2133 return -EBUSY;
2134 /* check whether the codec isn't used by any mixer or PCM streams */
2135 if (!list_empty(&card->ctl_files)) {
2136 hda_unlock_devices(card);
2137 return -EBUSY;
2138 }
2139 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2140 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2141 if (!cpcm->pcm)
2142 continue;
2143 if (cpcm->pcm->streams[0].substream_opened ||
2144 cpcm->pcm->streams[1].substream_opened) {
2145 hda_unlock_devices(card);
2146 return -EBUSY;
2147 }
2148 }
2149
2150 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002151
2152#ifdef CONFIG_SND_HDA_POWER_SAVE
2153 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002154 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002155#endif
2156 snd_hda_ctls_clear(codec);
2157 /* relase PCMs */
2158 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002159 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002160 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002161 clear_bit(codec->pcm_info[i].device,
2162 codec->bus->pcm_dev_bits);
2163 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002164 }
2165 if (codec->patch_ops.free)
2166 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002167 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002168 codec->spec = NULL;
2169 free_hda_cache(&codec->amp_cache);
2170 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002171 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2172 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002173 /* free only driver_pins so that init_pins + user_pins are restored */
2174 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002175 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002176 codec->num_pcms = 0;
2177 codec->pcm_info = NULL;
2178 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002179 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2180 codec->slave_dig_outs = NULL;
2181 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002182 module_put(codec->owner);
2183 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002184
2185 /* allow device access again */
2186 hda_unlock_devices(card);
2187 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002188}
2189
Takashi Iwaifd6f0752011-11-10 12:28:38 +01002190typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2191
2192/* apply the function to all matching slave ctls in the mixer list */
2193static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2194 map_slave_func_t func, void *data)
2195{
2196 struct hda_nid_item *items;
2197 const char * const *s;
2198 int i, err;
2199
2200 items = codec->mixers.list;
2201 for (i = 0; i < codec->mixers.used; i++) {
2202 struct snd_kcontrol *sctl = items[i].kctl;
2203 if (!sctl || !sctl->id.name ||
2204 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2205 continue;
2206 for (s = slaves; *s; s++) {
2207 if (!strcmp(sctl->id.name, *s)) {
2208 err = func(data, sctl);
2209 if (err)
2210 return err;
2211 break;
2212 }
2213 }
2214 }
2215 return 0;
2216}
2217
2218static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2219{
2220 return 1;
2221}
2222
Takashi Iwaid5191e52009-11-16 14:58:17 +01002223/**
2224 * snd_hda_add_vmaster - create a virtual master control and add slaves
2225 * @codec: HD-audio codec
2226 * @name: vmaster control name
2227 * @tlv: TLV data (optional)
2228 * @slaves: slave control names (optional)
2229 *
2230 * Create a virtual master control with the given name. The TLV data
2231 * must be either NULL or a valid data.
2232 *
2233 * @slaves is a NULL-terminated array of strings, each of which is a
2234 * slave control name. All controls with these names are assigned to
2235 * the new virtual master control.
2236 *
2237 * This function returns zero if successful or a negative error code.
2238 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002239int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002240 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002241{
2242 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002243 int err;
2244
Takashi Iwaifd6f0752011-11-10 12:28:38 +01002245 err = map_slaves(codec, slaves, check_slave_present, NULL);
2246 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002247 snd_printdd("No slave found for %s\n", name);
2248 return 0;
2249 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002250 kctl = snd_ctl_make_virtual_master(name, tlv);
2251 if (!kctl)
2252 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002253 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002254 if (err < 0)
2255 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002256
Takashi Iwaifd6f0752011-11-10 12:28:38 +01002257 err = map_slaves(codec, slaves, (map_slave_func_t)snd_ctl_add_slave,
2258 kctl);
2259 if (err < 0)
2260 return err;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002261 return 0;
2262}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002263EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002264
Takashi Iwaid5191e52009-11-16 14:58:17 +01002265/**
2266 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2267 *
2268 * The control element is supposed to have the private_value field
2269 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2270 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002271int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2272 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 int chs = get_amp_channels(kcontrol);
2275
2276 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2277 uinfo->count = chs == 3 ? 2 : 1;
2278 uinfo->value.integer.min = 0;
2279 uinfo->value.integer.max = 1;
2280 return 0;
2281}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002282EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Takashi Iwaid5191e52009-11-16 14:58:17 +01002284/**
2285 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2286 *
2287 * The control element is supposed to have the private_value field
2288 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2289 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002290int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2291 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2294 hda_nid_t nid = get_amp_nid(kcontrol);
2295 int chs = get_amp_channels(kcontrol);
2296 int dir = get_amp_direction(kcontrol);
2297 int idx = get_amp_index(kcontrol);
2298 long *valp = ucontrol->value.integer.value;
2299
2300 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002301 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002302 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002304 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002305 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
2307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002308EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Takashi Iwaid5191e52009-11-16 14:58:17 +01002310/**
2311 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2312 *
2313 * The control element is supposed to have the private_value field
2314 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2315 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002316int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2320 hda_nid_t nid = get_amp_nid(kcontrol);
2321 int chs = get_amp_channels(kcontrol);
2322 int dir = get_amp_direction(kcontrol);
2323 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 long *valp = ucontrol->value.integer.value;
2325 int change = 0;
2326
Takashi Iwaicb53c622007-08-10 17:21:45 +02002327 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002328 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002329 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002330 HDA_AMP_MUTE,
2331 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002332 valp++;
2333 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002334 if (chs & 2)
2335 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002336 HDA_AMP_MUTE,
2337 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002338 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002339 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return change;
2341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002342EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Takashi Iwai67d634c2009-11-16 15:35:59 +01002344#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002345/**
2346 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2347 *
2348 * This function calls snd_hda_enable_beep_device(), which behaves differently
2349 * depending on beep_mode option.
2350 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002351int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2352 struct snd_ctl_elem_value *ucontrol)
2353{
2354 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2355 long *valp = ucontrol->value.integer.value;
2356
2357 snd_hda_enable_beep_device(codec, *valp);
2358 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2359}
2360EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002361#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/*
Takashi Iwai985be542005-11-02 18:26:49 +01002364 * bound volume controls
2365 *
2366 * bind multiple volumes (# indices, from 0)
2367 */
2368
2369#define AMP_VAL_IDX_SHIFT 19
2370#define AMP_VAL_IDX_MASK (0x0f<<19)
2371
Takashi Iwaid5191e52009-11-16 14:58:17 +01002372/**
2373 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2374 *
2375 * The control element is supposed to have the private_value field
2376 * set up via HDA_BIND_MUTE*() macros.
2377 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002378int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002380{
2381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2382 unsigned long pval;
2383 int err;
2384
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002385 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002386 pval = kcontrol->private_value;
2387 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2388 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2389 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002390 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002391 return err;
2392}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002393EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002394
Takashi Iwaid5191e52009-11-16 14:58:17 +01002395/**
2396 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2397 *
2398 * The control element is supposed to have the private_value field
2399 * set up via HDA_BIND_MUTE*() macros.
2400 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002401int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002403{
2404 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2405 unsigned long pval;
2406 int i, indices, err = 0, change = 0;
2407
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002408 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002409 pval = kcontrol->private_value;
2410 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2411 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002412 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2413 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002414 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2415 if (err < 0)
2416 break;
2417 change |= err;
2418 }
2419 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002420 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002421 return err < 0 ? err : change;
2422}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002423EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002424
Takashi Iwaid5191e52009-11-16 14:58:17 +01002425/**
2426 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2427 *
2428 * The control element is supposed to have the private_value field
2429 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002430 */
2431int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2432 struct snd_ctl_elem_info *uinfo)
2433{
2434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2435 struct hda_bind_ctls *c;
2436 int err;
2437
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002438 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002439 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002440 kcontrol->private_value = *c->values;
2441 err = c->ops->info(kcontrol, uinfo);
2442 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002443 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002444 return err;
2445}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002446EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002447
Takashi Iwaid5191e52009-11-16 14:58:17 +01002448/**
2449 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2450 *
2451 * The control element is supposed to have the private_value field
2452 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2453 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002454int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2455 struct snd_ctl_elem_value *ucontrol)
2456{
2457 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2458 struct hda_bind_ctls *c;
2459 int err;
2460
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002461 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002462 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002463 kcontrol->private_value = *c->values;
2464 err = c->ops->get(kcontrol, ucontrol);
2465 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002466 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002467 return err;
2468}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002469EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002470
Takashi Iwaid5191e52009-11-16 14:58:17 +01002471/**
2472 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2473 *
2474 * The control element is supposed to have the private_value field
2475 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2476 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002477int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2478 struct snd_ctl_elem_value *ucontrol)
2479{
2480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2481 struct hda_bind_ctls *c;
2482 unsigned long *vals;
2483 int err = 0, change = 0;
2484
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002485 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002486 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002487 for (vals = c->values; *vals; vals++) {
2488 kcontrol->private_value = *vals;
2489 err = c->ops->put(kcontrol, ucontrol);
2490 if (err < 0)
2491 break;
2492 change |= err;
2493 }
2494 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002495 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002496 return err < 0 ? err : change;
2497}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002498EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002499
Takashi Iwaid5191e52009-11-16 14:58:17 +01002500/**
2501 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2502 *
2503 * The control element is supposed to have the private_value field
2504 * set up via HDA_BIND_VOL() macro.
2505 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002506int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2507 unsigned int size, unsigned int __user *tlv)
2508{
2509 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2510 struct hda_bind_ctls *c;
2511 int err;
2512
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002513 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002514 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002515 kcontrol->private_value = *c->values;
2516 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2517 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002518 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002519 return err;
2520}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002521EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002522
2523struct hda_ctl_ops snd_hda_bind_vol = {
2524 .info = snd_hda_mixer_amp_volume_info,
2525 .get = snd_hda_mixer_amp_volume_get,
2526 .put = snd_hda_mixer_amp_volume_put,
2527 .tlv = snd_hda_mixer_amp_tlv
2528};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002529EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002530
2531struct hda_ctl_ops snd_hda_bind_sw = {
2532 .info = snd_hda_mixer_amp_switch_info,
2533 .get = snd_hda_mixer_amp_switch_get,
2534 .put = snd_hda_mixer_amp_switch_put,
2535 .tlv = snd_hda_mixer_amp_tlv
2536};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002537EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002538
2539/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 * SPDIF out controls
2541 */
2542
Takashi Iwai0ba21762007-04-16 11:29:14 +02002543static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2544 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
2546 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2547 uinfo->count = 1;
2548 return 0;
2549}
2550
Takashi Iwai0ba21762007-04-16 11:29:14 +02002551static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2552 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2555 IEC958_AES0_NONAUDIO |
2556 IEC958_AES0_CON_EMPHASIS_5015 |
2557 IEC958_AES0_CON_NOT_COPYRIGHT;
2558 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2559 IEC958_AES1_CON_ORIGINAL;
2560 return 0;
2561}
2562
Takashi Iwai0ba21762007-04-16 11:29:14 +02002563static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2567 IEC958_AES0_NONAUDIO |
2568 IEC958_AES0_PRO_EMPHASIS_5015;
2569 return 0;
2570}
2571
Takashi Iwai0ba21762007-04-16 11:29:14 +02002572static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
2575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2576
2577 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2578 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2579 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2580 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2581
2582 return 0;
2583}
2584
2585/* convert from SPDIF status bits to HDA SPDIF bits
2586 * bit 0 (DigEn) is always set zero (to be filled later)
2587 */
2588static unsigned short convert_from_spdif_status(unsigned int sbits)
2589{
2590 unsigned short val = 0;
2591
2592 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002593 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002595 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002597 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2598 IEC958_AES0_PRO_EMPHASIS_5015)
2599 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002601 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2602 IEC958_AES0_CON_EMPHASIS_5015)
2603 val |= AC_DIG1_EMPHASIS;
2604 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2605 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002607 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2609 }
2610 return val;
2611}
2612
2613/* convert to SPDIF status bits from HDA SPDIF bits
2614 */
2615static unsigned int convert_to_spdif_status(unsigned short val)
2616{
2617 unsigned int sbits = 0;
2618
Takashi Iwai0ba21762007-04-16 11:29:14 +02002619 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002621 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 sbits |= IEC958_AES0_PROFESSIONAL;
2623 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002624 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2626 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002627 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002629 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002631 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2633 sbits |= val & (0x7f << 8);
2634 }
2635 return sbits;
2636}
2637
Takashi Iwai2f728532008-09-25 16:32:41 +02002638/* set digital convert verbs both for the given NID and its slaves */
2639static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2640 int verb, int val)
2641{
Takashi Iwaidda14412011-05-02 11:29:30 +02002642 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002643
Takashi Iwai9e976972008-11-25 08:17:20 +01002644 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002645 d = codec->slave_dig_outs;
2646 if (!d)
2647 return;
2648 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002649 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002650}
2651
2652static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2653 int dig1, int dig2)
2654{
2655 if (dig1 != -1)
2656 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2657 if (dig2 != -1)
2658 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2659}
2660
Takashi Iwai0ba21762007-04-16 11:29:14 +02002661static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2662 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
2664 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2665 hda_nid_t nid = kcontrol->private_value;
2666 unsigned short val;
2667 int change;
2668
Ingo Molnar62932df2006-01-16 16:34:20 +01002669 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 codec->spdif_status = ucontrol->value.iec958.status[0] |
2671 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2672 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2673 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2674 val = convert_from_spdif_status(codec->spdif_status);
2675 val |= codec->spdif_ctls & 1;
2676 change = codec->spdif_ctls != val;
2677 codec->spdif_ctls = val;
2678
Takashi Iwai2f728532008-09-25 16:32:41 +02002679 if (change)
2680 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Ingo Molnar62932df2006-01-16 16:34:20 +01002682 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return change;
2684}
2685
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002686#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Takashi Iwai0ba21762007-04-16 11:29:14 +02002688static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2689 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2692
Takashi Iwai0ba21762007-04-16 11:29:14 +02002693 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return 0;
2695}
2696
Takashi Iwai0ba21762007-04-16 11:29:14 +02002697static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2698 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2701 hda_nid_t nid = kcontrol->private_value;
2702 unsigned short val;
2703 int change;
2704
Ingo Molnar62932df2006-01-16 16:34:20 +01002705 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002706 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002708 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002710 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002712 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002713 /* unmute amp switch (if any) */
2714 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002715 (val & AC_DIG1_ENABLE))
2716 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2717 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002719 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return change;
2721}
2722
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002723static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 {
2725 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2726 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002727 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 .info = snd_hda_spdif_mask_info,
2729 .get = snd_hda_spdif_cmask_get,
2730 },
2731 {
2732 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2733 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002734 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 .info = snd_hda_spdif_mask_info,
2736 .get = snd_hda_spdif_pmask_get,
2737 },
2738 {
2739 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002740 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 .info = snd_hda_spdif_mask_info,
2742 .get = snd_hda_spdif_default_get,
2743 .put = snd_hda_spdif_default_put,
2744 },
2745 {
2746 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002747 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 .info = snd_hda_spdif_out_switch_info,
2749 .get = snd_hda_spdif_out_switch_get,
2750 .put = snd_hda_spdif_out_switch_put,
2751 },
2752 { } /* end */
2753};
2754
2755/**
2756 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2757 * @codec: the HDA codec
2758 * @nid: audio out widget NID
2759 *
2760 * Creates controls related with the SPDIF output.
2761 * Called from each patch supporting the SPDIF out.
2762 *
2763 * Returns 0 if successful, or a negative error code.
2764 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002765int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002768 struct snd_kcontrol *kctl;
2769 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002770 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Takashi Iwai1afe2062010-12-23 10:17:52 +01002772 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2773 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002774 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2775 return -EBUSY;
2776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2778 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002779 if (!kctl)
2780 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002781 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002783 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002784 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return err;
2786 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002787 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002788 snd_hda_codec_read(codec, nid, 0,
2789 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2791 return 0;
2792}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002793EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002796 * SPDIF sharing with analog output
2797 */
2798static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2799 struct snd_ctl_elem_value *ucontrol)
2800{
2801 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2802 ucontrol->value.integer.value[0] = mout->share_spdif;
2803 return 0;
2804}
2805
2806static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_value *ucontrol)
2808{
2809 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2810 mout->share_spdif = !!ucontrol->value.integer.value[0];
2811 return 0;
2812}
2813
2814static struct snd_kcontrol_new spdif_share_sw = {
2815 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2816 .name = "IEC958 Default PCM Playback Switch",
2817 .info = snd_ctl_boolean_mono_info,
2818 .get = spdif_share_sw_get,
2819 .put = spdif_share_sw_put,
2820};
2821
Takashi Iwaid5191e52009-11-16 14:58:17 +01002822/**
2823 * snd_hda_create_spdif_share_sw - create Default PCM switch
2824 * @codec: the HDA codec
2825 * @mout: multi-out instance
2826 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002827int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2828 struct hda_multi_out *mout)
2829{
2830 if (!mout->dig_out_nid)
2831 return 0;
2832 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002833 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2834 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002835}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002836EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002837
2838/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 * SPDIF input
2840 */
2841
2842#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2843
Takashi Iwai0ba21762007-04-16 11:29:14 +02002844static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2845 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
2847 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2848
2849 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2850 return 0;
2851}
2852
Takashi Iwai0ba21762007-04-16 11:29:14 +02002853static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2857 hda_nid_t nid = kcontrol->private_value;
2858 unsigned int val = !!ucontrol->value.integer.value[0];
2859 int change;
2860
Ingo Molnar62932df2006-01-16 16:34:20 +01002861 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002863 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002865 snd_hda_codec_write_cache(codec, nid, 0,
2866 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002868 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return change;
2870}
2871
Takashi Iwai0ba21762007-04-16 11:29:14 +02002872static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2876 hda_nid_t nid = kcontrol->private_value;
2877 unsigned short val;
2878 unsigned int sbits;
2879
Andrew Paprocki3982d172007-12-19 12:13:44 +01002880 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 sbits = convert_to_spdif_status(val);
2882 ucontrol->value.iec958.status[0] = sbits;
2883 ucontrol->value.iec958.status[1] = sbits >> 8;
2884 ucontrol->value.iec958.status[2] = sbits >> 16;
2885 ucontrol->value.iec958.status[3] = sbits >> 24;
2886 return 0;
2887}
2888
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002889static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 {
2891 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002892 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 .info = snd_hda_spdif_in_switch_info,
2894 .get = snd_hda_spdif_in_switch_get,
2895 .put = snd_hda_spdif_in_switch_put,
2896 },
2897 {
2898 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2899 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002900 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 .info = snd_hda_spdif_mask_info,
2902 .get = snd_hda_spdif_in_status_get,
2903 },
2904 { } /* end */
2905};
2906
2907/**
2908 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2909 * @codec: the HDA codec
2910 * @nid: audio in widget NID
2911 *
2912 * Creates controls related with the SPDIF input.
2913 * Called from each patch supporting the SPDIF in.
2914 *
2915 * Returns 0 if successful, or a negative error code.
2916 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002917int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
2919 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002920 struct snd_kcontrol *kctl;
2921 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002922 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Takashi Iwai1afe2062010-12-23 10:17:52 +01002924 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
2925 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002926 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2927 return -EBUSY;
2928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2930 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002931 if (!kctl)
2932 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002934 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002935 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return err;
2937 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002938 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002939 snd_hda_codec_read(codec, nid, 0,
2940 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002941 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return 0;
2943}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002944EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Takashi Iwaicb53c622007-08-10 17:21:45 +02002946#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002947/*
2948 * command cache
2949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002951/* build a 32bit cache key with the widget id and the command parameter */
2952#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2953#define get_cmd_cache_nid(key) ((key) & 0xff)
2954#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2955
2956/**
2957 * snd_hda_codec_write_cache - send a single command with caching
2958 * @codec: the HDA codec
2959 * @nid: NID to send the command
2960 * @direct: direct flag
2961 * @verb: the verb to send
2962 * @parm: the parameter for the verb
2963 *
2964 * Send a single command without waiting for response.
2965 *
2966 * Returns 0 if successful, or a negative error code.
2967 */
2968int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2969 int direct, unsigned int verb, unsigned int parm)
2970{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002971 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2972 struct hda_cache_head *c;
2973 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002974
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002975 if (err < 0)
2976 return err;
2977 /* parm may contain the verb stuff for get/set amp */
2978 verb = verb | (parm >> 8);
2979 parm &= 0xff;
2980 key = build_cmd_cache_key(nid, verb);
2981 mutex_lock(&codec->bus->cmd_mutex);
2982 c = get_alloc_hash(&codec->cmd_cache, key);
2983 if (c)
2984 c->val = parm;
2985 mutex_unlock(&codec->bus->cmd_mutex);
2986 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002987}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002988EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002989
Takashi Iwaid5191e52009-11-16 14:58:17 +01002990/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002991 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2992 * @codec: the HDA codec
2993 * @nid: NID to send the command
2994 * @direct: direct flag
2995 * @verb: the verb to send
2996 * @parm: the parameter for the verb
2997 *
2998 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2999 * command if the parameter is already identical with the cached value.
3000 * If not, it sends the command and refreshes the cache.
3001 *
3002 * Returns 0 if successful, or a negative error code.
3003 */
3004int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3005 int direct, unsigned int verb, unsigned int parm)
3006{
3007 struct hda_cache_head *c;
3008 u32 key;
3009
3010 /* parm may contain the verb stuff for get/set amp */
3011 verb = verb | (parm >> 8);
3012 parm &= 0xff;
3013 key = build_cmd_cache_key(nid, verb);
3014 mutex_lock(&codec->bus->cmd_mutex);
3015 c = get_hash(&codec->cmd_cache, key);
3016 if (c && c->val == parm) {
3017 mutex_unlock(&codec->bus->cmd_mutex);
3018 return 0;
3019 }
3020 mutex_unlock(&codec->bus->cmd_mutex);
3021 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3022}
3023EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3024
3025/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003026 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3027 * @codec: HD-audio codec
3028 *
3029 * Execute all verbs recorded in the command caches to resume.
3030 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003031void snd_hda_codec_resume_cache(struct hda_codec *codec)
3032{
Takashi Iwai603c4012008-07-30 15:01:44 +02003033 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003034 int i;
3035
Takashi Iwai603c4012008-07-30 15:01:44 +02003036 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003037 u32 key = buffer->key;
3038 if (!key)
3039 continue;
3040 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3041 get_cmd_cache_cmd(key), buffer->val);
3042 }
3043}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003044EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003045
3046/**
3047 * snd_hda_sequence_write_cache - sequence writes with caching
3048 * @codec: the HDA codec
3049 * @seq: VERB array to send
3050 *
3051 * Send the commands sequentially from the given array.
3052 * Thte commands are recorded on cache for power-save and resume.
3053 * The array must be terminated with NID=0.
3054 */
3055void snd_hda_sequence_write_cache(struct hda_codec *codec,
3056 const struct hda_verb *seq)
3057{
3058 for (; seq->nid; seq++)
3059 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3060 seq->param);
3061}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003062EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003063#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003064
Takashi Iwai54d17402005-11-21 16:33:22 +01003065/*
3066 * set power state of the codec
3067 */
3068static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3069 unsigned int power_state)
3070{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003071 hda_nid_t nid;
3072 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003073
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003074 /* this delay seems necessary to avoid click noise at power-down */
3075 if (power_state == AC_PWRST_D3)
3076 msleep(100);
3077 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003078 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003079 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003080 if (power_state == AC_PWRST_D0 &&
3081 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003082 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003083
Takashi Iwaicb53c622007-08-10 17:21:45 +02003084 nid = codec->start_nid;
3085 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003086 unsigned int wcaps = get_wcaps(codec, nid);
3087 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003088 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003089 if (power_state == AC_PWRST_D3 &&
3090 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003091 unsigned int pincap;
3092 /*
3093 * don't power down the widget if it controls
3094 * eapd and EAPD_BTLENABLE is set.
3095 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003096 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003097 if (pincap & AC_PINCAP_EAPD) {
3098 int eapd = snd_hda_codec_read(codec,
3099 nid, 0,
3100 AC_VERB_GET_EAPD_BTLENABLE, 0);
3101 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003102 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003103 continue;
3104 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003105 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003106 snd_hda_codec_write(codec, nid, 0,
3107 AC_VERB_SET_POWER_STATE,
3108 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003109 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003110 }
3111
Takashi Iwaicb53c622007-08-10 17:21:45 +02003112 if (power_state == AC_PWRST_D0) {
3113 unsigned long end_time;
3114 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003115 /* wait until the codec reachs to D0 */
3116 end_time = jiffies + msecs_to_jiffies(500);
3117 do {
3118 state = snd_hda_codec_read(codec, fg, 0,
3119 AC_VERB_GET_POWER_STATE, 0);
3120 if (state == power_state)
3121 break;
3122 msleep(1);
3123 } while (time_after_eq(end_time, jiffies));
3124 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003125}
3126
Takashi Iwai11aeff02008-07-30 15:01:46 +02003127#ifdef CONFIG_SND_HDA_HWDEP
3128/* execute additional init verbs */
3129static void hda_exec_init_verbs(struct hda_codec *codec)
3130{
3131 if (codec->init_verbs.list)
3132 snd_hda_sequence_write(codec, codec->init_verbs.list);
3133}
3134#else
3135static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3136#endif
3137
Takashi Iwaicb53c622007-08-10 17:21:45 +02003138#ifdef SND_HDA_NEEDS_RESUME
3139/*
3140 * call suspend and power-down; used both from PM and power-save
3141 */
3142static void hda_call_codec_suspend(struct hda_codec *codec)
3143{
3144 if (codec->patch_ops.suspend)
3145 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003146 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003147 hda_set_power_state(codec,
3148 codec->afg ? codec->afg : codec->mfg,
3149 AC_PWRST_D3);
3150#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003151 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003152 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003153 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003154 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003155 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003156#endif
3157}
3158
3159/*
3160 * kick up codec; used both from PM and power-save
3161 */
3162static void hda_call_codec_resume(struct hda_codec *codec)
3163{
3164 hda_set_power_state(codec,
3165 codec->afg ? codec->afg : codec->mfg,
3166 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003167 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003168 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003169 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003170 if (codec->patch_ops.resume)
3171 codec->patch_ops.resume(codec);
3172 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003173 if (codec->patch_ops.init)
3174 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003175 snd_hda_codec_resume_amp(codec);
3176 snd_hda_codec_resume_cache(codec);
3177 }
3178}
3179#endif /* SND_HDA_NEEDS_RESUME */
3180
Takashi Iwai54d17402005-11-21 16:33:22 +01003181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182/**
3183 * snd_hda_build_controls - build mixer controls
3184 * @bus: the BUS
3185 *
3186 * Creates mixer controls for each codec included in the bus.
3187 *
3188 * Returns 0 if successful, otherwise a negative error code.
3189 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003190int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003192 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Takashi Iwai0ba21762007-04-16 11:29:14 +02003194 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003195 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003196 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003197 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003198 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003199 err = snd_hda_codec_reset(codec);
3200 if (err < 0) {
3201 printk(KERN_ERR
3202 "hda_codec: cannot revert codec\n");
3203 return err;
3204 }
3205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003207 return 0;
3208}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003209EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003211int snd_hda_codec_build_controls(struct hda_codec *codec)
3212{
3213 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003214 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003215 /* continue to initialize... */
3216 if (codec->patch_ops.init)
3217 err = codec->patch_ops.init(codec);
3218 if (!err && codec->patch_ops.build_controls)
3219 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003220 if (err < 0)
3221 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return 0;
3223}
3224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225/*
3226 * stream formats
3227 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003228struct hda_rate_tbl {
3229 unsigned int hz;
3230 unsigned int alsa_bits;
3231 unsigned int hda_fmt;
3232};
3233
Takashi Iwai92f10b32010-08-03 14:21:00 +02003234/* rate = base * mult / div */
3235#define HDA_RATE(base, mult, div) \
3236 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3237 (((div) - 1) << AC_FMT_DIV_SHIFT))
3238
Takashi Iwaibefdf312005-08-22 13:57:55 +02003239static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003241
3242 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003243 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3244 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3245 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3246 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3247 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3248 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3249 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3250 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3251 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3252 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3253 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003254#define AC_PAR_PCM_RATE_BITS 11
3255 /* up to bits 10, 384kHZ isn't supported properly */
3256
3257 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003258 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003259
Takashi Iwaibefdf312005-08-22 13:57:55 +02003260 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261};
3262
3263/**
3264 * snd_hda_calc_stream_format - calculate format bitset
3265 * @rate: the sample rate
3266 * @channels: the number of channels
3267 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3268 * @maxbps: the max. bps
3269 *
3270 * Calculate the format bitset from the given rate, channels and th PCM format.
3271 *
3272 * Return zero if invalid.
3273 */
3274unsigned int snd_hda_calc_stream_format(unsigned int rate,
3275 unsigned int channels,
3276 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003277 unsigned int maxbps,
3278 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
3280 int i;
3281 unsigned int val = 0;
3282
Takashi Iwaibefdf312005-08-22 13:57:55 +02003283 for (i = 0; rate_bits[i].hz; i++)
3284 if (rate_bits[i].hz == rate) {
3285 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 break;
3287 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003288 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 snd_printdd("invalid rate %d\n", rate);
3290 return 0;
3291 }
3292
3293 if (channels == 0 || channels > 8) {
3294 snd_printdd("invalid channels %d\n", channels);
3295 return 0;
3296 }
3297 val |= channels - 1;
3298
3299 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003300 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003301 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003302 break;
3303 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003304 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 case 20:
3307 case 24:
3308 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003309 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003310 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003312 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003314 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 break;
3316 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003317 snd_printdd("invalid format width %d\n",
3318 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return 0;
3320 }
3321
Anssi Hannula32c168c2010-08-03 13:28:57 +03003322 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003323 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return val;
3326}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003327EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003329static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3330{
3331 unsigned int val = 0;
3332 if (nid != codec->afg &&
3333 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3334 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3335 if (!val || val == -1)
3336 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3337 if (!val || val == -1)
3338 return 0;
3339 return val;
3340}
3341
3342static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3343{
3344 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3345 get_pcm_param);
3346}
3347
3348static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3349{
3350 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3351 if (!streams || streams == -1)
3352 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3353 if (!streams || streams == -1)
3354 return 0;
3355 return streams;
3356}
3357
3358static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3359{
3360 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3361 get_stream_param);
3362}
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364/**
3365 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3366 * @codec: the HDA codec
3367 * @nid: NID to query
3368 * @ratesp: the pointer to store the detected rate bitflags
3369 * @formatsp: the pointer to store the detected formats
3370 * @bpsp: the pointer to store the detected format widths
3371 *
3372 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3373 * or @bsps argument is ignored.
3374 *
3375 * Returns 0 if successful, otherwise a negative error code.
3376 */
Takashi Iwai986862b2008-11-27 12:40:13 +01003377static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3379{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003380 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003382 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003383 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
3385 if (ratesp) {
3386 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003387 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003389 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003391 if (rates == 0) {
3392 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3393 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3394 nid, val,
3395 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3396 return -EIO;
3397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 *ratesp = rates;
3399 }
3400
3401 if (formatsp || bpsp) {
3402 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003403 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003405 streams = query_stream_param(codec, nid);
3406 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 bps = 0;
3410 if (streams & AC_SUPFMT_PCM) {
3411 if (val & AC_SUPPCM_BITS_8) {
3412 formats |= SNDRV_PCM_FMTBIT_U8;
3413 bps = 8;
3414 }
3415 if (val & AC_SUPPCM_BITS_16) {
3416 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3417 bps = 16;
3418 }
3419 if (wcaps & AC_WCAP_DIGITAL) {
3420 if (val & AC_SUPPCM_BITS_32)
3421 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3422 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3423 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3424 if (val & AC_SUPPCM_BITS_24)
3425 bps = 24;
3426 else if (val & AC_SUPPCM_BITS_20)
3427 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003428 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3429 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3431 if (val & AC_SUPPCM_BITS_32)
3432 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 else if (val & AC_SUPPCM_BITS_24)
3434 bps = 24;
Nicolas Graziano33ef7652006-09-19 14:23:14 +02003435 else if (val & AC_SUPPCM_BITS_20)
3436 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 }
3438 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003439 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003441 if (!bps)
3442 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003443 }
3444 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003445 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 /* temporary hack: we have still no proper support
3447 * for the direct AC3 stream...
3448 */
3449 formats |= SNDRV_PCM_FMTBIT_U8;
3450 bps = 8;
3451 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003452 if (formats == 0) {
3453 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3454 "(nid=0x%x, val=0x%x, ovrd=%i, "
3455 "streams=0x%x)\n",
3456 nid, val,
3457 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3458 streams);
3459 return -EIO;
3460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 if (formatsp)
3462 *formatsp = formats;
3463 if (bpsp)
3464 *bpsp = bps;
3465 }
3466
3467 return 0;
3468}
3469
3470/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003471 * snd_hda_is_supported_format - Check the validity of the format
3472 * @codec: HD-audio codec
3473 * @nid: NID to check
3474 * @format: the HD-audio format value to check
3475 *
3476 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 *
3478 * Returns 1 if supported, 0 if not.
3479 */
3480int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3481 unsigned int format)
3482{
3483 int i;
3484 unsigned int val = 0, rate, stream;
3485
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003486 val = query_pcm_param(codec, nid);
3487 if (!val)
3488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003491 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003492 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 if (val & (1 << i))
3494 break;
3495 return 0;
3496 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003497 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return 0;
3499
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003500 stream = query_stream_param(codec, nid);
3501 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return 0;
3503
3504 if (stream & AC_SUPFMT_PCM) {
3505 switch (format & 0xf0) {
3506 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003507 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return 0;
3509 break;
3510 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003511 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 return 0;
3513 break;
3514 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003515 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 return 0;
3517 break;
3518 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003519 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 return 0;
3521 break;
3522 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003523 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 return 0;
3525 break;
3526 default:
3527 return 0;
3528 }
3529 } else {
3530 /* FIXME: check for float32 and AC3? */
3531 }
3532
3533 return 1;
3534}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003535EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537/*
3538 * PCM stuff
3539 */
3540static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3541 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003542 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
3544 return 0;
3545}
3546
3547static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3548 struct hda_codec *codec,
3549 unsigned int stream_tag,
3550 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003551 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
3553 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3554 return 0;
3555}
3556
3557static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3558 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003559 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
Takashi Iwai888afa12008-03-18 09:57:50 +01003561 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 return 0;
3563}
3564
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003565static int set_pcm_default_values(struct hda_codec *codec,
3566 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003568 int err;
3569
Takashi Iwai0ba21762007-04-16 11:29:14 +02003570 /* query support PCM information from the given NID */
3571 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003572 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003573 info->rates ? NULL : &info->rates,
3574 info->formats ? NULL : &info->formats,
3575 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003576 if (err < 0)
3577 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 }
3579 if (info->ops.open == NULL)
3580 info->ops.open = hda_pcm_default_open_close;
3581 if (info->ops.close == NULL)
3582 info->ops.close = hda_pcm_default_open_close;
3583 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003584 if (snd_BUG_ON(!info->nid))
3585 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 info->ops.prepare = hda_pcm_default_prepare;
3587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003589 if (snd_BUG_ON(!info->nid))
3590 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 info->ops.cleanup = hda_pcm_default_cleanup;
3592 }
3593 return 0;
3594}
3595
Takashi Iwaieb541332010-08-06 13:48:11 +02003596/*
3597 * codec prepare/cleanup entries
3598 */
3599int snd_hda_codec_prepare(struct hda_codec *codec,
3600 struct hda_pcm_stream *hinfo,
3601 unsigned int stream,
3602 unsigned int format,
3603 struct snd_pcm_substream *substream)
3604{
3605 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003606 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003607 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3608 if (ret >= 0)
3609 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003610 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003611 return ret;
3612}
3613EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3614
3615void snd_hda_codec_cleanup(struct hda_codec *codec,
3616 struct hda_pcm_stream *hinfo,
3617 struct snd_pcm_substream *substream)
3618{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003619 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003620 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003621 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003622}
3623EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3624
Takashi Iwaid5191e52009-11-16 14:58:17 +01003625/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003626const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3627 "Audio", "SPDIF", "HDMI", "Modem"
3628};
3629
Takashi Iwai176d5332008-07-30 15:01:44 +02003630/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003631 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003632 *
3633 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003634 */
3635static int get_empty_pcm_device(struct hda_bus *bus, int type)
3636{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003637 /* audio device indices; not linear to keep compatibility */
3638 static int audio_idx[HDA_PCM_NTYPES][5] = {
3639 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3640 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003641 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003642 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003643 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003644 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003645
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003646 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003647 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3648 return -EINVAL;
3649 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003650
3651 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3652 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3653 return audio_idx[type][i];
3654
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003655 snd_printk(KERN_WARNING "Too many %s devices\n",
3656 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003657 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003658}
3659
3660/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003661 * attach a new PCM stream
3662 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003663static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003664{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003665 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003666 struct hda_pcm_stream *info;
3667 int stream, err;
3668
Takashi Iwaib91f0802008-11-04 08:43:08 +01003669 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003670 return -EINVAL;
3671 for (stream = 0; stream < 2; stream++) {
3672 info = &pcm->stream[stream];
3673 if (info->substreams) {
3674 err = set_pcm_default_values(codec, info);
3675 if (err < 0)
3676 return err;
3677 }
3678 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003679 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003680}
3681
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003682/* assign all PCMs of the given codec */
3683int snd_hda_codec_build_pcms(struct hda_codec *codec)
3684{
3685 unsigned int pcm;
3686 int err;
3687
3688 if (!codec->num_pcms) {
3689 if (!codec->patch_ops.build_pcms)
3690 return 0;
3691 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003692 if (err < 0) {
3693 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003694 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003695 err = snd_hda_codec_reset(codec);
3696 if (err < 0) {
3697 printk(KERN_ERR
3698 "hda_codec: cannot revert codec\n");
3699 return err;
3700 }
3701 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003702 }
3703 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3704 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3705 int dev;
3706
3707 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003708 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003709
3710 if (!cpcm->pcm) {
3711 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3712 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003713 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003714 cpcm->device = dev;
3715 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003716 if (err < 0) {
3717 printk(KERN_ERR "hda_codec: cannot attach "
3718 "PCM stream %d for codec #%d\n",
3719 dev, codec->addr);
3720 continue; /* no fatal error */
3721 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003722 }
3723 }
3724 return 0;
3725}
3726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727/**
3728 * snd_hda_build_pcms - build PCM information
3729 * @bus: the BUS
3730 *
3731 * Create PCM information for each codec included in the bus.
3732 *
3733 * The build_pcms codec patch is requested to set up codec->num_pcms and
3734 * codec->pcm_info properly. The array is referred by the top-level driver
3735 * to create its PCM instances.
3736 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3737 * callback.
3738 *
3739 * At least, substreams, channels_min and channels_max must be filled for
3740 * each stream. substreams = 0 indicates that the stream doesn't exist.
3741 * When rates and/or formats are zero, the supported values are queried
3742 * from the given nid. The nid is used also by the default ops.prepare
3743 * and ops.cleanup callbacks.
3744 *
3745 * The driver needs to call ops.open in its open callback. Similarly,
3746 * ops.close is supposed to be called in the close callback.
3747 * ops.prepare should be called in the prepare or hw_params callback
3748 * with the proper parameters for set up.
3749 * ops.cleanup should be called in hw_free for clean up of streams.
3750 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003751 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003753int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003755 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Takashi Iwai0ba21762007-04-16 11:29:14 +02003757 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003758 int err = snd_hda_codec_build_pcms(codec);
3759 if (err < 0)
3760 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 }
3762 return 0;
3763}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003764EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766/**
3767 * snd_hda_check_board_config - compare the current codec with the config table
3768 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003769 * @num_configs: number of config enums
3770 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 * @tbl: configuration table, terminated by null entries
3772 *
3773 * Compares the modelname or PCI subsystem id of the current codec with the
3774 * given configuration table. If a matching entry is found, returns its
3775 * config value (supposed to be 0 or positive).
3776 *
3777 * If no entries are matching, the function returns a negative value.
3778 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003779int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003780 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003781 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003783 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003784 int i;
3785 for (i = 0; i < num_configs; i++) {
3786 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003787 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003788 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3789 "selected\n", models[i]);
3790 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
3792 }
3793 }
3794
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003795 if (!codec->bus->pci || !tbl)
3796 return -1;
3797
3798 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3799 if (!tbl)
3800 return -1;
3801 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003802#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003803 char tmp[10];
3804 const char *model = NULL;
3805 if (models)
3806 model = models[tbl->value];
3807 if (!model) {
3808 sprintf(tmp, "#%d", tbl->value);
3809 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003811 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3812 "for config %x:%x (%s)\n",
3813 model, tbl->subvendor, tbl->subdevice,
3814 (tbl->name ? tbl->name : "Unknown device"));
3815#endif
3816 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
3818 return -1;
3819}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003820EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003823 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003824 subsystem ID with the
3825 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003826
3827 This is important for Gateway notebooks with SB450 HDA Audio
3828 where the vendor ID of the PCI device is:
3829 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3830 and the vendor/subvendor are found only at the codec.
3831
3832 * @codec: the HDA codec
3833 * @num_configs: number of config enums
3834 * @models: array of model name strings
3835 * @tbl: configuration table, terminated by null entries
3836 *
3837 * Compares the modelname or PCI subsystem id of the current codec with the
3838 * given configuration table. If a matching entry is found, returns its
3839 * config value (supposed to be 0 or positive).
3840 *
3841 * If no entries are matching, the function returns a negative value.
3842 */
3843int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003844 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003845 const struct snd_pci_quirk *tbl)
3846{
3847 const struct snd_pci_quirk *q;
3848
3849 /* Search for codec ID */
3850 for (q = tbl; q->subvendor; q++) {
3851 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3852
3853 if (vendorid == codec->subsystem_id)
3854 break;
3855 }
3856
3857 if (!q->subvendor)
3858 return -1;
3859
3860 tbl = q;
3861
3862 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003863#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003864 char tmp[10];
3865 const char *model = NULL;
3866 if (models)
3867 model = models[tbl->value];
3868 if (!model) {
3869 sprintf(tmp, "#%d", tbl->value);
3870 model = tmp;
3871 }
3872 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3873 "for config %x:%x (%s)\n",
3874 model, tbl->subvendor, tbl->subdevice,
3875 (tbl->name ? tbl->name : "Unknown device"));
3876#endif
3877 return tbl->value;
3878 }
3879 return -1;
3880}
3881EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3882
3883/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 * snd_hda_add_new_ctls - create controls from the array
3885 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01003886 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 *
3888 * This helper function creates and add new controls in the given array.
3889 * The array must be terminated with an empty entry as terminator.
3890 *
3891 * Returns 0 if successful, or a negative error code.
3892 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003893int snd_hda_add_new_ctls(struct hda_codec *codec,
3894 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003896 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
3898 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003899 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003900 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003901 if (knew->iface == -1) /* skip this codec private value */
3902 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003903 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003904 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003905 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003906 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003907 if (addr > 0)
3908 kctl->id.device = addr;
3909 if (idx > 0)
3910 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003911 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003912 if (!err)
3913 break;
3914 /* try first with another device index corresponding to
3915 * the codec addr; if it still fails (or it's the
3916 * primary codec), then try another control index
3917 */
3918 if (!addr && codec->addr)
3919 addr = codec->addr;
3920 else if (!idx && !knew->index) {
3921 idx = find_empty_mixer_ctl_idx(codec,
3922 knew->name);
3923 if (idx <= 0)
3924 return err;
3925 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003926 return err;
3927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 }
3929 return 0;
3930}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003931EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Takashi Iwaicb53c622007-08-10 17:21:45 +02003933#ifdef CONFIG_SND_HDA_POWER_SAVE
3934static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3935 unsigned int power_state);
3936
3937static void hda_power_work(struct work_struct *work)
3938{
3939 struct hda_codec *codec =
3940 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003941 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003942
Maxim Levitsky2e492462007-09-03 15:26:57 +02003943 if (!codec->power_on || codec->power_count) {
3944 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003945 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003946 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003947
3948 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003949 if (bus->ops.pm_notify)
3950 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003951}
3952
3953static void hda_keep_power_on(struct hda_codec *codec)
3954{
3955 codec->power_count++;
3956 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003957 codec->power_jiffies = jiffies;
3958}
3959
Takashi Iwaid5191e52009-11-16 14:58:17 +01003960/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003961void snd_hda_update_power_acct(struct hda_codec *codec)
3962{
3963 unsigned long delta = jiffies - codec->power_jiffies;
3964 if (codec->power_on)
3965 codec->power_on_acct += delta;
3966 else
3967 codec->power_off_acct += delta;
3968 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003969}
3970
Takashi Iwaid5191e52009-11-16 14:58:17 +01003971/**
3972 * snd_hda_power_up - Power-up the codec
3973 * @codec: HD-audio codec
3974 *
3975 * Increment the power-up counter and power up the hardware really when
3976 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003977 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003978void snd_hda_power_up(struct hda_codec *codec)
3979{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003980 struct hda_bus *bus = codec->bus;
3981
Takashi Iwaicb53c622007-08-10 17:21:45 +02003982 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003983 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003984 return;
3985
Takashi Iwaia2f63092009-11-11 09:34:25 +01003986 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003987 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003988 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003989 if (bus->ops.pm_notify)
3990 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003991 hda_call_codec_resume(codec);
3992 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003993 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003994}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003995EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003996
3997#define power_save(codec) \
3998 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003999
Takashi Iwaid5191e52009-11-16 14:58:17 +01004000/**
4001 * snd_hda_power_down - Power-down the codec
4002 * @codec: HD-audio codec
4003 *
4004 * Decrement the power-up counter and schedules the power-off work if
4005 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004006 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004007void snd_hda_power_down(struct hda_codec *codec)
4008{
4009 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004010 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004011 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004012 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004013 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004014 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004015 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004016 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004017}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004018EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004019
Takashi Iwaid5191e52009-11-16 14:58:17 +01004020/**
4021 * snd_hda_check_amp_list_power - Check the amp list and update the power
4022 * @codec: HD-audio codec
4023 * @check: the object containing an AMP list and the status
4024 * @nid: NID to check / update
4025 *
4026 * Check whether the given NID is in the amp list. If it's in the list,
4027 * check the current AMP status, and update the the power-status according
4028 * to the mute status.
4029 *
4030 * This function is supposed to be set or called from the check_power_status
4031 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004032 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004033int snd_hda_check_amp_list_power(struct hda_codec *codec,
4034 struct hda_loopback_check *check,
4035 hda_nid_t nid)
4036{
Takashi Iwai031024e2011-05-02 11:29:30 +02004037 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004038 int ch, v;
4039
4040 if (!check->amplist)
4041 return 0;
4042 for (p = check->amplist; p->nid; p++) {
4043 if (p->nid == nid)
4044 break;
4045 }
4046 if (!p->nid)
4047 return 0; /* nothing changed */
4048
4049 for (p = check->amplist; p->nid; p++) {
4050 for (ch = 0; ch < 2; ch++) {
4051 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4052 p->idx);
4053 if (!(v & HDA_AMP_MUTE) && v > 0) {
4054 if (!check->power_on) {
4055 check->power_on = 1;
4056 snd_hda_power_up(codec);
4057 }
4058 return 1;
4059 }
4060 }
4061 }
4062 if (check->power_on) {
4063 check->power_on = 0;
4064 snd_hda_power_down(codec);
4065 }
4066 return 0;
4067}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004068EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004071/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004072 * Channel mode helper
4073 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004074
4075/**
4076 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4077 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004078int snd_hda_ch_mode_info(struct hda_codec *codec,
4079 struct snd_ctl_elem_info *uinfo,
4080 const struct hda_channel_mode *chmode,
4081 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004082{
4083 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4084 uinfo->count = 1;
4085 uinfo->value.enumerated.items = num_chmodes;
4086 if (uinfo->value.enumerated.item >= num_chmodes)
4087 uinfo->value.enumerated.item = num_chmodes - 1;
4088 sprintf(uinfo->value.enumerated.name, "%dch",
4089 chmode[uinfo->value.enumerated.item].channels);
4090 return 0;
4091}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004092EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004093
Takashi Iwaid5191e52009-11-16 14:58:17 +01004094/**
4095 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4096 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004097int snd_hda_ch_mode_get(struct hda_codec *codec,
4098 struct snd_ctl_elem_value *ucontrol,
4099 const struct hda_channel_mode *chmode,
4100 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004101 int max_channels)
4102{
4103 int i;
4104
4105 for (i = 0; i < num_chmodes; i++) {
4106 if (max_channels == chmode[i].channels) {
4107 ucontrol->value.enumerated.item[0] = i;
4108 break;
4109 }
4110 }
4111 return 0;
4112}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004113EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004114
Takashi Iwaid5191e52009-11-16 14:58:17 +01004115/**
4116 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4117 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004118int snd_hda_ch_mode_put(struct hda_codec *codec,
4119 struct snd_ctl_elem_value *ucontrol,
4120 const struct hda_channel_mode *chmode,
4121 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004122 int *max_channelsp)
4123{
4124 unsigned int mode;
4125
4126 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004127 if (mode >= num_chmodes)
4128 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004129 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004130 return 0;
4131 /* change the current channel setting */
4132 *max_channelsp = chmode[mode].channels;
4133 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004134 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004135 return 1;
4136}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004137EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139/*
4140 * input MUX helper
4141 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004142
4143/**
4144 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4145 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004146int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4147 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148{
4149 unsigned int index;
4150
4151 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4152 uinfo->count = 1;
4153 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004154 if (!imux->num_items)
4155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 index = uinfo->value.enumerated.item;
4157 if (index >= imux->num_items)
4158 index = imux->num_items - 1;
4159 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4160 return 0;
4161}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004162EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Takashi Iwaid5191e52009-11-16 14:58:17 +01004164/**
4165 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4166 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004167int snd_hda_input_mux_put(struct hda_codec *codec,
4168 const struct hda_input_mux *imux,
4169 struct snd_ctl_elem_value *ucontrol,
4170 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 unsigned int *cur_val)
4172{
4173 unsigned int idx;
4174
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004175 if (!imux->num_items)
4176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 idx = ucontrol->value.enumerated.item[0];
4178 if (idx >= imux->num_items)
4179 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004180 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004182 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4183 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 *cur_val = idx;
4185 return 1;
4186}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004187EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189
4190/*
4191 * Multi-channel / digital-out PCM helper functions
4192 */
4193
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004194/* setup SPDIF output stream */
4195static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4196 unsigned int stream_tag, unsigned int format)
4197{
4198 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02004199 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004200 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02004201 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4202 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004203 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004204 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004205 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004206 for (d = codec->slave_dig_outs; *d; d++)
4207 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4208 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004209 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004210 /* turn on again (if needed) */
4211 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4212 set_dig_out_convert(codec, nid,
4213 codec->spdif_ctls & 0xff, -1);
4214}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004215
Takashi Iwai2f728532008-09-25 16:32:41 +02004216static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4217{
4218 snd_hda_codec_cleanup_stream(codec, nid);
4219 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004220 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004221 for (d = codec->slave_dig_outs; *d; d++)
4222 snd_hda_codec_cleanup_stream(codec, *d);
4223 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004224}
4225
Takashi Iwaid5191e52009-11-16 14:58:17 +01004226/**
4227 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4228 * @bus: HD-audio bus
4229 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004230void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4231{
4232 struct hda_codec *codec;
4233
4234 if (!bus)
4235 return;
4236 list_for_each_entry(codec, &bus->codec_list, list) {
4237#ifdef CONFIG_SND_HDA_POWER_SAVE
4238 if (!codec->power_on)
4239 continue;
4240#endif
4241 if (codec->patch_ops.reboot_notify)
4242 codec->patch_ops.reboot_notify(codec);
4243 }
4244}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004245EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004246
Takashi Iwaid5191e52009-11-16 14:58:17 +01004247/**
4248 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004250int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4251 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252{
Ingo Molnar62932df2006-01-16 16:34:20 +01004253 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004254 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4255 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004256 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004258 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 return 0;
4260}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004261EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Takashi Iwaid5191e52009-11-16 14:58:17 +01004263/**
4264 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4265 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004266int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4267 struct hda_multi_out *mout,
4268 unsigned int stream_tag,
4269 unsigned int format,
4270 struct snd_pcm_substream *substream)
4271{
4272 mutex_lock(&codec->spdif_mutex);
4273 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4274 mutex_unlock(&codec->spdif_mutex);
4275 return 0;
4276}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004277EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004278
Takashi Iwaid5191e52009-11-16 14:58:17 +01004279/**
4280 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4281 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004282int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4283 struct hda_multi_out *mout)
4284{
4285 mutex_lock(&codec->spdif_mutex);
4286 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4287 mutex_unlock(&codec->spdif_mutex);
4288 return 0;
4289}
4290EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4291
Takashi Iwaid5191e52009-11-16 14:58:17 +01004292/**
4293 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004295int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4296 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
Ingo Molnar62932df2006-01-16 16:34:20 +01004298 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004300 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return 0;
4302}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004303EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Takashi Iwaid5191e52009-11-16 14:58:17 +01004305/**
4306 * snd_hda_multi_out_analog_open - open analog outputs
4307 *
4308 * Open analog outputs and set up the hw-constraints.
4309 * If the digital outputs can be opened as slave, open the digital
4310 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004312int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4313 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004314 struct snd_pcm_substream *substream,
4315 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
Takashi Iwai9a081602008-02-12 18:37:26 +01004317 struct snd_pcm_runtime *runtime = substream->runtime;
4318 runtime->hw.channels_max = mout->max_channels;
4319 if (mout->dig_out_nid) {
4320 if (!mout->analog_rates) {
4321 mout->analog_rates = hinfo->rates;
4322 mout->analog_formats = hinfo->formats;
4323 mout->analog_maxbps = hinfo->maxbps;
4324 } else {
4325 runtime->hw.rates = mout->analog_rates;
4326 runtime->hw.formats = mout->analog_formats;
4327 hinfo->maxbps = mout->analog_maxbps;
4328 }
4329 if (!mout->spdif_rates) {
4330 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4331 &mout->spdif_rates,
4332 &mout->spdif_formats,
4333 &mout->spdif_maxbps);
4334 }
4335 mutex_lock(&codec->spdif_mutex);
4336 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004337 if ((runtime->hw.rates & mout->spdif_rates) &&
4338 (runtime->hw.formats & mout->spdif_formats)) {
4339 runtime->hw.rates &= mout->spdif_rates;
4340 runtime->hw.formats &= mout->spdif_formats;
4341 if (mout->spdif_maxbps < hinfo->maxbps)
4342 hinfo->maxbps = mout->spdif_maxbps;
4343 } else {
4344 mout->share_spdif = 0;
4345 /* FIXME: need notify? */
4346 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004347 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004348 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4351 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4352}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004353EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Takashi Iwaid5191e52009-11-16 14:58:17 +01004355/**
4356 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4357 *
4358 * Set up the i/o for analog out.
4359 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004361int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4362 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 unsigned int stream_tag,
4364 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01004365 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
Takashi Iwaidda14412011-05-02 11:29:30 +02004367 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 int chs = substream->runtime->channels;
4369 int i;
4370
Ingo Molnar62932df2006-01-16 16:34:20 +01004371 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004372 if (mout->dig_out_nid && mout->share_spdif &&
4373 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004375 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4376 format) &&
4377 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004379 setup_dig_out_stream(codec, mout->dig_out_nid,
4380 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 } else {
4382 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004383 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 }
4385 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004386 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004389 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4390 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004391 if (!mout->no_share_stream &&
4392 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004394 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4395 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004396 /* extra outputs copied from front */
4397 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004398 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004399 snd_hda_codec_setup_stream(codec,
4400 mout->extra_out_nid[i],
4401 stream_tag, 0, format);
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 /* surrounds */
4404 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004405 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004406 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4407 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004408 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004409 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4410 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 }
4412 return 0;
4413}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004414EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Takashi Iwaid5191e52009-11-16 14:58:17 +01004416/**
4417 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004419int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4420 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
Takashi Iwaidda14412011-05-02 11:29:30 +02004422 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 int i;
4424
4425 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004426 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004428 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004429 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4430 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004431 snd_hda_codec_cleanup_stream(codec,
4432 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004433 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004435 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 mout->dig_out_used = 0;
4437 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004438 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return 0;
4440}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004441EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004443/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004444 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004445 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004446
Takashi Iwaidda14412011-05-02 11:29:30 +02004447static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004448{
4449 for (; *list; list++)
4450 if (*list == nid)
4451 return 1;
4452 return 0;
4453}
4454
Steve Longerbeam81937d32007-05-08 15:33:03 +02004455
4456/*
4457 * Sort an associated group of pins according to their sequence numbers.
4458 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004459static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004460 int num_pins)
4461{
4462 int i, j;
4463 short seq;
4464 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004465
Steve Longerbeam81937d32007-05-08 15:33:03 +02004466 for (i = 0; i < num_pins; i++) {
4467 for (j = i + 1; j < num_pins; j++) {
4468 if (sequences[i] > sequences[j]) {
4469 seq = sequences[i];
4470 sequences[i] = sequences[j];
4471 sequences[j] = seq;
4472 nid = pins[i];
4473 pins[i] = pins[j];
4474 pins[j] = nid;
4475 }
4476 }
4477 }
4478}
4479
4480
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004481/* add the found input-pin to the cfg->inputs[] table */
4482static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4483 int type)
4484{
4485 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4486 cfg->inputs[cfg->num_inputs].pin = nid;
4487 cfg->inputs[cfg->num_inputs].type = type;
4488 cfg->num_inputs++;
4489 }
4490}
4491
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004492/* sort inputs in the order of AUTO_PIN_* type */
4493static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4494{
4495 int i, j;
4496
4497 for (i = 0; i < cfg->num_inputs; i++) {
4498 for (j = i + 1; j < cfg->num_inputs; j++) {
4499 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4500 struct auto_pin_cfg_item tmp;
4501 tmp = cfg->inputs[i];
4502 cfg->inputs[i] = cfg->inputs[j];
4503 cfg->inputs[j] = tmp;
4504 }
4505 }
4506 }
4507}
4508
Takashi Iwai82bc9552006-03-21 11:24:42 +01004509/*
4510 * Parse all pin widgets and store the useful pin nids to cfg
4511 *
4512 * The number of line-outs or any primary output is stored in line_outs,
4513 * and the corresponding output pins are assigned to line_out_pins[],
4514 * in the order of front, rear, CLFE, side, ...
4515 *
4516 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004517 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004518 * is detected, one of speaker of HP pins is assigned as the primary
4519 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4520 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004521 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004522 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004523 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4524 * respectively.
4525 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004526int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4527 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004528 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004529{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004530 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004531 short seq, assoc_line_out, assoc_speaker;
4532 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4533 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004534 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004535 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004536
4537 memset(cfg, 0, sizeof(*cfg));
4538
Steve Longerbeam81937d32007-05-08 15:33:03 +02004539 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4540 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004541 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004542 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004543
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004544 end_nid = codec->start_nid + codec->num_nodes;
4545 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004546 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004547 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004548 unsigned int def_conf;
4549 short assoc, loc;
4550
4551 /* read all default configuration for pin complex */
4552 if (wid_type != AC_WID_PIN)
4553 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004554 /* ignore the given nids (e.g. pc-beep returns error) */
4555 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4556 continue;
4557
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004558 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004559 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4560 continue;
4561 loc = get_defcfg_location(def_conf);
4562 switch (get_defcfg_device(def_conf)) {
4563 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004564 seq = get_defcfg_sequence(def_conf);
4565 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004566
4567 if (!(wid_caps & AC_WCAP_STEREO))
4568 if (!cfg->mono_out_pin)
4569 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004570 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004571 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004572 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004573 assoc_line_out = assoc;
4574 else if (assoc_line_out != assoc)
4575 continue;
4576 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4577 continue;
4578 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004579 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004580 cfg->line_outs++;
4581 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004582 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004583 seq = get_defcfg_sequence(def_conf);
4584 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004585 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004586 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004587 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004588 assoc_speaker = assoc;
4589 else if (assoc_speaker != assoc)
4590 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004591 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4592 continue;
4593 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004594 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004595 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004596 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004597 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004598 seq = get_defcfg_sequence(def_conf);
4599 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004600 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4601 continue;
4602 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004603 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004604 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004605 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004606 case AC_JACK_MIC_IN:
4607 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004608 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004609 case AC_JACK_LINE_IN:
4610 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004611 break;
4612 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004613 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004614 break;
4615 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004616 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004617 break;
4618 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004619 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004620 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4621 continue;
4622 cfg->dig_out_pins[cfg->dig_outs] = nid;
4623 cfg->dig_out_type[cfg->dig_outs] =
4624 (loc == AC_JACK_LOC_HDMI) ?
4625 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4626 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004627 break;
4628 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004629 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004630 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004631 if (loc == AC_JACK_LOC_HDMI)
4632 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4633 else
4634 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004635 break;
4636 }
4637 }
4638
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004639 /* FIX-UP:
4640 * If no line-out is defined but multiple HPs are found,
4641 * some of them might be the real line-outs.
4642 */
4643 if (!cfg->line_outs && cfg->hp_outs > 1) {
4644 int i = 0;
4645 while (i < cfg->hp_outs) {
4646 /* The real HPs should have the sequence 0x0f */
4647 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4648 i++;
4649 continue;
4650 }
4651 /* Move it to the line-out table */
4652 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4653 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4654 cfg->line_outs++;
4655 cfg->hp_outs--;
4656 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4657 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004658 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004659 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4660 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004661 memset(cfg->hp_pins + cfg->hp_outs, 0,
4662 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004663 if (!cfg->hp_outs)
4664 cfg->line_out_type = AUTO_PIN_HP_OUT;
4665
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004666 }
4667
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004668 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004669 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4670 cfg->line_outs);
4671 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4672 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004673 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4674 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004675
Steve Longerbeam81937d32007-05-08 15:33:03 +02004676 /*
4677 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4678 * as a primary output
4679 */
4680 if (!cfg->line_outs) {
4681 if (cfg->speaker_outs) {
4682 cfg->line_outs = cfg->speaker_outs;
4683 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4684 sizeof(cfg->speaker_pins));
4685 cfg->speaker_outs = 0;
4686 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4687 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4688 } else if (cfg->hp_outs) {
4689 cfg->line_outs = cfg->hp_outs;
4690 memcpy(cfg->line_out_pins, cfg->hp_pins,
4691 sizeof(cfg->hp_pins));
4692 cfg->hp_outs = 0;
4693 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4694 cfg->line_out_type = AUTO_PIN_HP_OUT;
4695 }
4696 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004697
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004698 /* Reorder the surround channels
4699 * ALSA sequence is front/surr/clfe/side
4700 * HDA sequence is:
4701 * 4-ch: front/surr => OK as it is
4702 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004703 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004704 */
4705 switch (cfg->line_outs) {
4706 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004707 case 4:
4708 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004709 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004710 cfg->line_out_pins[2] = nid;
4711 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004712 }
4713
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004714 sort_autocfg_input_pins(cfg);
4715
Takashi Iwai82bc9552006-03-21 11:24:42 +01004716 /*
4717 * debug prints of the parsed results
4718 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004719 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 +01004720 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4721 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004722 cfg->line_out_pins[4],
4723 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4724 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4725 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004726 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4727 cfg->speaker_outs, cfg->speaker_pins[0],
4728 cfg->speaker_pins[1], cfg->speaker_pins[2],
4729 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004730 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4731 cfg->hp_outs, cfg->hp_pins[0],
4732 cfg->hp_pins[1], cfg->hp_pins[2],
4733 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004734 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004735 if (cfg->dig_outs)
4736 snd_printd(" dig-out=0x%x/0x%x\n",
4737 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004738 snd_printd(" inputs:");
4739 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004740 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004741 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004742 cfg->inputs[i].pin);
4743 }
4744 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004745 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004746 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004747
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004748 return 0;
4749}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004750EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004751
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004752int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004753{
4754 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004755 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004756 if (conn == AC_JACK_PORT_NONE)
4757 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004758 /* Windows may claim the internal mic to be BOTH, too */
4759 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004760 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004761 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004762 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004763 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004764 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004765 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004766 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004767 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004768 return INPUT_PIN_ATTR_FRONT;
4769 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004770}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004771EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004772
Takashi Iwai990061c2010-09-09 22:08:44 +02004773/**
4774 * hda_get_input_pin_label - Give a label for the given input pin
4775 *
4776 * When check_location is true, the function checks the pin location
4777 * for mic and line-in pins, and set an appropriate prefix like "Front",
4778 * "Rear", "Internal".
4779 */
4780
Takashi Iwai10a20af2010-09-09 16:28:02 +02004781const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4782 int check_location)
4783{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004784 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004785 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004786 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4787 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004788 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004789
Takashi Iwai10a20af2010-09-09 16:28:02 +02004790 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004791
4792 switch (get_defcfg_device(def_conf)) {
4793 case AC_JACK_MIC_IN:
4794 if (!check_location)
4795 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004796 attr = snd_hda_get_input_pin_attr(def_conf);
4797 if (!attr)
4798 return "None";
4799 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004800 case AC_JACK_LINE_IN:
4801 if (!check_location)
4802 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004803 attr = snd_hda_get_input_pin_attr(def_conf);
4804 if (!attr)
4805 return "None";
4806 if (attr == INPUT_PIN_ATTR_DOCK)
4807 return "Dock Line";
4808 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004809 case AC_JACK_AUX:
4810 return "Aux";
4811 case AC_JACK_CD:
4812 return "CD";
4813 case AC_JACK_SPDIF_IN:
4814 return "SPDIF In";
4815 case AC_JACK_DIG_OTHER_IN:
4816 return "Digital In";
4817 default:
4818 return "Misc";
4819 }
4820}
4821EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4822
Takashi Iwaia1c98512010-09-09 21:36:27 +02004823/* Check whether the location prefix needs to be added to the label.
4824 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4825 * have to put "Front" prefix to each label. In such a case, returns false.
4826 */
4827static int check_mic_location_need(struct hda_codec *codec,
4828 const struct auto_pin_cfg *cfg,
4829 int input)
4830{
4831 unsigned int defc;
4832 int i, attr, attr2;
4833
4834 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004835 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004836 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004837 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004838 return 1;
4839
4840 attr = 0;
4841 for (i = 0; i < cfg->num_inputs; i++) {
4842 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004843 attr2 = snd_hda_get_input_pin_attr(defc);
4844 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004845 if (attr && attr != attr2)
4846 return 1; /* different locations found */
4847 attr = attr2;
4848 }
4849 }
4850 return 0;
4851}
4852
Takashi Iwai990061c2010-09-09 22:08:44 +02004853/**
4854 * hda_get_autocfg_input_label - Get a label for the given input
4855 *
4856 * Get a label for the given input pin defined by the autocfg item.
4857 * Unlike hda_get_input_pin_label(), this function checks all inputs
4858 * defined in autocfg and avoids the redundant mic/line prefix as much as
4859 * possible.
4860 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004861const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4862 const struct auto_pin_cfg *cfg,
4863 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004864{
4865 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004866 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004867
Takashi Iwai10a20af2010-09-09 16:28:02 +02004868 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4869 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4870 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004871 if (has_multiple_pins && type == AUTO_PIN_MIC)
4872 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004873 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4874 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004875}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004876EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4877
Takashi Iwai990061c2010-09-09 22:08:44 +02004878/**
4879 * snd_hda_add_imux_item - Add an item to input_mux
4880 *
4881 * When the same label is used already in the existing items, the number
4882 * suffix is appended to the label. This label index number is stored
4883 * to type_idx when non-NULL pointer is given.
4884 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004885int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4886 int index, int *type_idx)
4887{
4888 int i, label_idx = 0;
4889 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4890 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4891 return -EINVAL;
4892 }
4893 for (i = 0; i < imux->num_items; i++) {
4894 if (!strncmp(label, imux->items[i].label, strlen(label)))
4895 label_idx++;
4896 }
4897 if (type_idx)
4898 *type_idx = label_idx;
4899 if (label_idx > 0)
4900 snprintf(imux->items[imux->num_items].label,
4901 sizeof(imux->items[imux->num_items].label),
4902 "%s %d", label, label_idx);
4903 else
4904 strlcpy(imux->items[imux->num_items].label, label,
4905 sizeof(imux->items[imux->num_items].label));
4906 imux->items[imux->num_items].index = index;
4907 imux->num_items++;
4908 return 0;
4909}
4910EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004911
Takashi Iwai4a471b72005-12-07 13:56:29 +01004912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913#ifdef CONFIG_PM
4914/*
4915 * power management
4916 */
4917
4918/**
4919 * snd_hda_suspend - suspend the codecs
4920 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 *
4922 * Returns 0 if successful.
4923 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004924int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004926 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
Takashi Iwai0ba21762007-04-16 11:29:14 +02004928 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004929#ifdef CONFIG_SND_HDA_POWER_SAVE
4930 if (!codec->power_on)
4931 continue;
4932#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004933 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 }
4935 return 0;
4936}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004937EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
4939/**
4940 * snd_hda_resume - resume the codecs
4941 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 *
4943 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004944 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004945 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004946 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 */
4948int snd_hda_resume(struct hda_bus *bus)
4949{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004950 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Takashi Iwai0ba21762007-04-16 11:29:14 +02004952 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004953 if (snd_hda_codec_needs_resume(codec))
4954 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 return 0;
4957}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004958EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004959#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004960
4961/*
4962 * generic arrays
4963 */
4964
Takashi Iwaid5191e52009-11-16 14:58:17 +01004965/**
4966 * snd_array_new - get a new element from the given array
4967 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004968 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004969 * Get a new element from the given array. If it exceeds the
4970 * pre-allocated array size, re-allocate the array.
4971 *
4972 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004973 */
4974void *snd_array_new(struct snd_array *array)
4975{
4976 if (array->used >= array->alloced) {
4977 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004978 void *nlist;
4979 if (snd_BUG_ON(num >= 4096))
4980 return NULL;
4981 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004982 if (!nlist)
4983 return NULL;
4984 if (array->list) {
4985 memcpy(nlist, array->list,
4986 array->elem_size * array->alloced);
4987 kfree(array->list);
4988 }
4989 array->list = nlist;
4990 array->alloced = num;
4991 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004992 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004993}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004994EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004995
Takashi Iwaid5191e52009-11-16 14:58:17 +01004996/**
4997 * snd_array_free - free the given array elements
4998 * @array: the array object
4999 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005000void snd_array_free(struct snd_array *array)
5001{
5002 kfree(array->list);
5003 array->used = 0;
5004 array->alloced = 0;
5005 array->list = NULL;
5006}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005007EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005008
Takashi Iwaid5191e52009-11-16 14:58:17 +01005009/**
5010 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5011 * @pcm: PCM caps bits
5012 * @buf: the string buffer to write
5013 * @buflen: the max buffer length
5014 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005015 * used by hda_proc.c and hda_eld.c
5016 */
5017void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5018{
5019 static unsigned int rates[] = {
5020 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5021 96000, 176400, 192000, 384000
5022 };
5023 int i, j;
5024
5025 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5026 if (pcm & (1 << i))
5027 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5028
5029 buf[j] = '\0'; /* necessary when j == 0 */
5030}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005031EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005032
Takashi Iwaid5191e52009-11-16 14:58:17 +01005033/**
5034 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5035 * @pcm: PCM caps bits
5036 * @buf: the string buffer to write
5037 * @buflen: the max buffer length
5038 *
5039 * used by hda_proc.c and hda_eld.c
5040 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005041void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5042{
5043 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5044 int i, j;
5045
5046 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5047 if (pcm & (AC_SUPPCM_BITS_8 << i))
5048 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5049
5050 buf[j] = '\0'; /* necessary when j == 0 */
5051}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005052EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005053
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005054#ifdef CONFIG_SND_HDA_INPUT_JACK
5055/*
5056 * Input-jack notification support
5057 */
5058struct hda_jack_item {
5059 hda_nid_t nid;
5060 int type;
5061 struct snd_jack *jack;
5062};
5063
5064static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5065 int type)
5066{
5067 switch (type) {
5068 case SND_JACK_HEADPHONE:
5069 return "Headphone";
5070 case SND_JACK_MICROPHONE:
5071 return "Mic";
5072 case SND_JACK_LINEOUT:
5073 return "Line-out";
5074 case SND_JACK_HEADSET:
5075 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005076 case SND_JACK_VIDEOOUT:
5077 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005078 default:
5079 return "Misc";
5080 }
5081}
5082
5083static void hda_free_jack_priv(struct snd_jack *jack)
5084{
5085 struct hda_jack_item *jacks = jack->private_data;
5086 jacks->nid = 0;
5087 jacks->jack = NULL;
5088}
5089
5090int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5091 const char *name)
5092{
5093 struct hda_jack_item *jack;
5094 int err;
5095
5096 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5097 jack = snd_array_new(&codec->jacks);
5098 if (!jack)
5099 return -ENOMEM;
5100
5101 jack->nid = nid;
5102 jack->type = type;
5103 if (!name)
5104 name = get_jack_default_name(codec, nid, type);
5105 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5106 if (err < 0) {
5107 jack->nid = 0;
5108 return err;
5109 }
5110 jack->jack->private_data = jack;
5111 jack->jack->private_free = hda_free_jack_priv;
5112 return 0;
5113}
5114EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5115
5116void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5117{
5118 struct hda_jack_item *jacks = codec->jacks.list;
5119 int i;
5120
5121 if (!jacks)
5122 return;
5123
5124 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5125 unsigned int pin_ctl;
5126 unsigned int present;
5127 int type;
5128
5129 if (jacks->nid != nid)
5130 continue;
5131 present = snd_hda_jack_detect(codec, nid);
5132 type = jacks->type;
5133 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5134 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5135 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5136 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5137 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5138 }
5139 snd_jack_report(jacks->jack, present ? type : 0);
5140 }
5141}
5142EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5143
5144/* free jack instances manually when clearing/reconfiguring */
5145void snd_hda_input_jack_free(struct hda_codec *codec)
5146{
5147 if (!codec->bus->shutdown && codec->jacks.list) {
5148 struct hda_jack_item *jacks = codec->jacks.list;
5149 int i;
5150 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5151 if (jacks->jack)
5152 snd_device_free(codec->bus->card, jacks->jack);
5153 }
5154 }
5155 snd_array_free(&codec->jacks);
5156}
5157EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5158#endif /* CONFIG_SND_HDA_INPUT_JACK */
5159
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005160MODULE_DESCRIPTION("HDA codec core");
5161MODULE_LICENSE("GPL");