blob: 45b4a8d70e085a36849f32d971e0d987b6ded638 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020034#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020035#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * vendor / preset table
39 */
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46/* codec vendor labels */
47static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010048 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020049 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020050 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010051 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010052 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010053 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020054 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010055 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020056 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020058 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020060 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010061 { 0x17e8, "Chrontel" },
62 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000063 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010065 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020066 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 {} /* terminator */
68};
69
Takashi Iwai1289e9e2008-11-27 15:47:11 +010070static DEFINE_MUTEX(preset_mutex);
71static LIST_HEAD(hda_preset_tables);
72
73int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74{
75 mutex_lock(&preset_mutex);
76 list_add_tail(&preset->list, &hda_preset_tables);
77 mutex_unlock(&preset_mutex);
78 return 0;
79}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010080EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010081
82int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83{
84 mutex_lock(&preset_mutex);
85 list_del(&preset->list);
86 mutex_unlock(&preset_mutex);
87 return 0;
88}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010089EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwaicb53c622007-08-10 17:21:45 +020091#ifdef CONFIG_SND_HDA_POWER_SAVE
92static void hda_power_work(struct work_struct *work);
93static void hda_keep_power_on(struct hda_codec *codec);
94#else
95static inline void hda_keep_power_on(struct hda_codec *codec) {}
96#endif
97
Takashi Iwaid5191e52009-11-16 14:58:17 +010098/**
99 * snd_hda_get_jack_location - Give a location string of the jack
100 * @cfg: pin default config value
101 *
102 * Parse the pin default config value and returns the string of the
103 * jack location, e.g. "Rear", "Front", etc.
104 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400105const char *snd_hda_get_jack_location(u32 cfg)
106{
107 static char *bases[7] = {
108 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 };
110 static unsigned char specials_idx[] = {
111 0x07, 0x08,
112 0x17, 0x18, 0x19,
113 0x37, 0x38
114 };
115 static char *specials[] = {
116 "Rear Panel", "Drive Bar",
117 "Riser", "HDMI", "ATAPI",
118 "Mobile-In", "Mobile-Out"
119 };
120 int i;
121 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
122 if ((cfg & 0x0f) < 7)
123 return bases[cfg & 0x0f];
124 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
125 if (cfg == specials_idx[i])
126 return specials[i];
127 }
128 return "UNKNOWN";
129}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100130EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400131
Takashi Iwaid5191e52009-11-16 14:58:17 +0100132/**
133 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
134 * @cfg: pin default config value
135 *
136 * Parse the pin default config value and returns the string of the
137 * jack connectivity, i.e. external or internal connection.
138 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139const char *snd_hda_get_jack_connectivity(u32 cfg)
140{
141 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142
143 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100145EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400146
Takashi Iwaid5191e52009-11-16 14:58:17 +0100147/**
148 * snd_hda_get_jack_type - Give a type string of the jack
149 * @cfg: pin default config value
150 *
151 * Parse the pin default config value and returns the string of the
152 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154const char *snd_hda_get_jack_type(u32 cfg)
155{
156 static char *jack_types[16] = {
157 "Line Out", "Speaker", "HP Out", "CD",
158 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
159 "Line In", "Aux", "Mic", "Telephony",
160 "SPDIF In", "Digitial In", "Reserved", "Other"
161 };
162
163 return jack_types[(cfg & AC_DEFCFG_DEVICE)
164 >> AC_DEFCFG_DEVICE_SHIFT];
165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100166EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400167
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100168/*
169 * Compose a 32bit command word to be sent to the HD-audio controller
170 */
171static inline unsigned int
172make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
173 unsigned int verb, unsigned int parm)
174{
175 u32 val;
176
Takashi Iwai82e1b802009-07-17 12:47:34 +0200177 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
178 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800179 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
180 codec->addr, direct, nid, verb, parm);
181 return ~0;
182 }
183
184 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100185 val |= (u32)direct << 27;
186 val |= (u32)nid << 20;
187 val |= verb << 8;
188 val |= parm;
189 return val;
190}
191
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200192/*
193 * Send and receive a verb
194 */
195static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
196 unsigned int *res)
197{
198 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200199 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200
Wu Fengguang6430aee2009-07-17 16:49:19 +0800201 if (cmd == ~0)
202 return -1;
203
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200204 if (res)
205 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200206 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207 snd_hda_power_up(codec);
208 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200209 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200210 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800211 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 mutex_unlock(&bus->cmd_mutex);
213 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 if (res && *res == -1 && bus->rirb_error) {
215 if (bus->response_reset) {
216 snd_printd("hda_codec: resetting BUS due to "
217 "fatal communication error\n");
218 bus->ops.bus_reset(bus);
219 }
220 goto again;
221 }
222 /* clear reset-flag when the communication gets recovered */
223 if (!err)
224 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200225 return err;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/**
229 * snd_hda_codec_read - send a command and get the response
230 * @codec: the HDA codec
231 * @nid: NID to send the command
232 * @direct: direct flag
233 * @verb: the verb to send
234 * @parm: the parameter for the verb
235 *
236 * Send a single command and read the corresponding response.
237 *
238 * Returns the obtained response value, or -1 for an error.
239 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200240unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
241 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int verb, unsigned int parm)
243{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200244 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 unsigned int res;
246 codec_exec_verb(codec, cmd, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return res;
248}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100249EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/**
252 * snd_hda_codec_write - send a single command without waiting for response
253 * @codec: the HDA codec
254 * @nid: NID to send the command
255 * @direct: direct flag
256 * @verb: the verb to send
257 * @parm: the parameter for the verb
258 *
259 * Send a single command without waiting for response.
260 *
261 * Returns 0 if successful, or a negative error code.
262 */
263int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
264 unsigned int verb, unsigned int parm)
265{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200266 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100267 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200268 return codec_exec_verb(codec, cmd,
269 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100271EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/**
274 * snd_hda_sequence_write - sequence writes
275 * @codec: the HDA codec
276 * @seq: VERB array to send
277 *
278 * Send the commands sequentially from the given array.
279 * The array must be terminated with NID=0.
280 */
281void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
282{
283 for (; seq->nid; seq++)
284 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
285}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100286EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/**
289 * snd_hda_get_sub_nodes - get the range of sub nodes
290 * @codec: the HDA codec
291 * @nid: NID to parse
292 * @start_id: the pointer to store the start NID
293 *
294 * Parse the NID and store the start NID of its sub-nodes.
295 * Returns the number of sub-nodes.
296 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200297int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
298 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 unsigned int parm;
301
302 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200303 if (parm == -1)
304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *start_id = (parm >> 16) & 0x7fff;
306 return (int)(parm & 0x7fff);
307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100308EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200310static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
311 hda_nid_t *conn_list, int max_conns);
312static bool add_conn_list(struct snd_array *array, hda_nid_t nid);
313static int copy_conn_list(hda_nid_t nid, hda_nid_t *dst, int max_dst,
314 hda_nid_t *src, int len);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/**
317 * snd_hda_get_connections - get connection list
318 * @codec: the HDA codec
319 * @nid: NID to parse
320 * @conn_list: connection list array
321 * @max_conns: max. number of connections to store
322 *
323 * Parses the connection list of the given widget and stores the list
324 * of NIDs.
325 *
326 * Returns the number of connections, or a negative error code.
327 */
328int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200329 hda_nid_t *conn_list, int max_conns)
330{
331 struct snd_array *array = &codec->conn_lists;
Andrew Morton5b17b072011-04-14 15:06:13 -0700332 int i, len, old_used;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200333 hda_nid_t list[HDA_MAX_CONNECTIONS];
334
335 /* look up the cached results */
336 for (i = 0; i < array->used; ) {
337 hda_nid_t *p = snd_array_elem(array, i);
338 len = p[1];
339 if (nid == *p)
340 return copy_conn_list(nid, conn_list, max_conns,
341 p + 2, len);
342 i += len + 2;
343 }
344
345 len = _hda_get_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
346 if (len < 0)
347 return len;
348
349 /* add to the cache */
350 old_used = array->used;
351 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
352 goto error_add;
353 for (i = 0; i < len; i++)
354 if (!add_conn_list(array, list[i]))
355 goto error_add;
356
357 return copy_conn_list(nid, conn_list, max_conns, list, len);
358
359 error_add:
360 array->used = old_used;
361 return -ENOMEM;
362}
363EXPORT_SYMBOL_HDA(snd_hda_get_connections);
364
365static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
366 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100369 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200371 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100372 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Takashi Iwaida3cec32008-08-08 17:12:14 +0200374 if (snd_BUG_ON(!conn_list || max_conns <= 0))
375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200377 wcaps = get_wcaps(codec, nid);
378 if (!(wcaps & AC_WCAP_CONN_LIST) &&
379 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200380 snd_printk(KERN_WARNING "hda_codec: "
381 "connection list not available for 0x%x\n", nid);
382 return -EINVAL;
383 }
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
386 if (parm & AC_CLIST_LONG) {
387 /* long form */
388 shift = 16;
389 num_elems = 2;
390 } else {
391 /* short form */
392 shift = 8;
393 num_elems = 4;
394 }
395 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 mask = (1 << (shift-1)) - 1;
397
Takashi Iwai0ba21762007-04-16 11:29:14 +0200398 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0; /* no connection */
400
401 if (conn_len == 1) {
402 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200403 parm = snd_hda_codec_read(codec, nid, 0,
404 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200405 if (parm == -1 && codec->bus->rirb_error)
406 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 conn_list[0] = parm & mask;
408 return 1;
409 }
410
411 /* multi connection */
412 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100413 prev_nid = 0;
414 for (i = 0; i < conn_len; i++) {
415 int range_val;
416 hda_nid_t val, n;
417
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200418 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100419 parm = snd_hda_codec_read(codec, nid, 0,
420 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200421 if (parm == -1 && codec->bus->rirb_error)
422 return -EIO;
423 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200424 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100425 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200426 if (val == 0) {
427 snd_printk(KERN_WARNING "hda_codec: "
428 "invalid CONNECT_LIST verb %x[%i]:%x\n",
429 nid, i, parm);
430 return 0;
431 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100432 parm >>= shift;
433 if (range_val) {
434 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200435 if (!prev_nid || prev_nid >= val) {
436 snd_printk(KERN_WARNING "hda_codec: "
437 "invalid dep_range_val %x:%x\n",
438 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100439 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100441 for (n = prev_nid + 1; n <= val; n++) {
442 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200443 snd_printk(KERN_ERR "hda_codec: "
444 "Too many connections %d for NID 0x%x\n",
445 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100446 return -EINVAL;
447 }
448 conn_list[conns++] = n;
449 }
450 } else {
451 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200452 snd_printk(KERN_ERR "hda_codec: "
453 "Too many connections %d for NID 0x%x\n",
454 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100455 return -EINVAL;
456 }
457 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100459 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 return conns;
462}
463
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200464static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
465{
466 hda_nid_t *p = snd_array_new(array);
467 if (!p)
468 return false;
469 *p = nid;
470 return true;
471}
472
473static int copy_conn_list(hda_nid_t nid, hda_nid_t *dst, int max_dst,
474 hda_nid_t *src, int len)
475{
476 if (len > max_dst) {
477 snd_printk(KERN_ERR "hda_codec: "
478 "Too many connections %d for NID 0x%x\n",
479 len, nid);
480 return -EINVAL;
481 }
482 memcpy(dst, src, len * sizeof(hda_nid_t));
483 return len;
484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486/**
487 * snd_hda_queue_unsol_event - add an unsolicited event to queue
488 * @bus: the BUS
489 * @res: unsolicited event (lower 32bit of RIRB entry)
490 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
491 *
492 * Adds the given event to the queue. The events are processed in
493 * the workqueue asynchronously. Call this function in the interrupt
494 * hanlder when RIRB receives an unsolicited event.
495 *
496 * Returns 0 if successful, or a negative error code.
497 */
498int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
499{
500 struct hda_bus_unsolicited *unsol;
501 unsigned int wp;
502
Takashi Iwai0ba21762007-04-16 11:29:14 +0200503 unsol = bus->unsol;
504 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506
507 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
508 unsol->wp = wp;
509
510 wp <<= 1;
511 unsol->queue[wp] = res;
512 unsol->queue[wp + 1] = res_ex;
513
Takashi Iwai6acaed32009-01-12 10:09:24 +0100514 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 return 0;
517}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100518EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800521 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
David Howellsc4028952006-11-22 14:57:56 +0000523static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
David Howellsc4028952006-11-22 14:57:56 +0000525 struct hda_bus_unsolicited *unsol =
526 container_of(work, struct hda_bus_unsolicited, work);
527 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct hda_codec *codec;
529 unsigned int rp, caddr, res;
530
531 while (unsol->rp != unsol->wp) {
532 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
533 unsol->rp = rp;
534 rp <<= 1;
535 res = unsol->queue[rp];
536 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200537 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 continue;
539 codec = bus->caddr_tbl[caddr & 0x0f];
540 if (codec && codec->patch_ops.unsol_event)
541 codec->patch_ops.unsol_event(codec, res);
542 }
543}
544
545/*
546 * initialize unsolicited queue
547 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200548static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct hda_bus_unsolicited *unsol;
551
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100552 if (bus->unsol) /* already initialized */
553 return 0;
554
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200555 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200556 if (!unsol) {
557 snd_printk(KERN_ERR "hda_codec: "
558 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -ENOMEM;
560 }
David Howellsc4028952006-11-22 14:57:56 +0000561 INIT_WORK(&unsol->work, process_unsol_events);
562 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 bus->unsol = unsol;
564 return 0;
565}
566
567/*
568 * destructor
569 */
570static void snd_hda_codec_free(struct hda_codec *codec);
571
572static int snd_hda_bus_free(struct hda_bus *bus)
573{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200574 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Takashi Iwai0ba21762007-04-16 11:29:14 +0200576 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100578 if (bus->workq)
579 flush_workqueue(bus->workq);
580 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200582 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 snd_hda_codec_free(codec);
584 }
585 if (bus->ops.private_free)
586 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100587 if (bus->workq)
588 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 kfree(bus);
590 return 0;
591}
592
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100593static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100596 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return snd_hda_bus_free(bus);
598}
599
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200600#ifdef CONFIG_SND_HDA_HWDEP
601static int snd_hda_bus_dev_register(struct snd_device *device)
602{
603 struct hda_bus *bus = device->device_data;
604 struct hda_codec *codec;
605 list_for_each_entry(codec, &bus->codec_list, list) {
606 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100607 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200608 }
609 return 0;
610}
611#else
612#define snd_hda_bus_dev_register NULL
613#endif
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/**
616 * snd_hda_bus_new - create a HDA bus
617 * @card: the card entry
618 * @temp: the template for hda_bus information
619 * @busp: the pointer to store the created bus instance
620 *
621 * Returns 0 if successful, or a negative error code.
622 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100623int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200624 const struct hda_bus_template *temp,
625 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct hda_bus *bus;
628 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100629 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200630 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 .dev_free = snd_hda_bus_dev_free,
632 };
633
Takashi Iwaida3cec32008-08-08 17:12:14 +0200634 if (snd_BUG_ON(!temp))
635 return -EINVAL;
636 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
637 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (busp)
640 *busp = NULL;
641
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200642 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (bus == NULL) {
644 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
645 return -ENOMEM;
646 }
647
648 bus->card = card;
649 bus->private_data = temp->private_data;
650 bus->pci = temp->pci;
651 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100652 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 bus->ops = temp->ops;
654
Ingo Molnar62932df2006-01-16 16:34:20 +0100655 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200656 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 INIT_LIST_HEAD(&bus->codec_list);
658
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100659 snprintf(bus->workq_name, sizeof(bus->workq_name),
660 "hd-audio%d", card->number);
661 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100662 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100663 snd_printk(KERN_ERR "cannot create workqueue %s\n",
664 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100665 kfree(bus);
666 return -ENOMEM;
667 }
668
Takashi Iwai0ba21762007-04-16 11:29:14 +0200669 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
670 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 snd_hda_bus_free(bus);
672 return err;
673 }
674 if (busp)
675 *busp = bus;
676 return 0;
677}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100678EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Takashi Iwai82467612007-07-27 19:15:54 +0200680#ifdef CONFIG_SND_HDA_GENERIC
681#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200682 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200683#else
684#define is_generic_config(codec) 0
685#endif
686
Takashi Iwai645f10c2008-11-28 15:07:37 +0100687#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100688#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
689#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100690#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100691#endif
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/*
694 * find a matching codec preset
695 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200696static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200697find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100699 struct hda_codec_preset_list *tbl;
700 const struct hda_codec_preset *preset;
701 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Takashi Iwai82467612007-07-27 19:15:54 +0200703 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100704 return NULL; /* use the generic parser */
705
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100706 again:
707 mutex_lock(&preset_mutex);
708 list_for_each_entry(tbl, &hda_preset_tables, list) {
709 if (!try_module_get(tbl->owner)) {
710 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
711 continue;
712 }
713 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100715 if (preset->afg && preset->afg != codec->afg)
716 continue;
717 if (preset->mfg && preset->mfg != codec->mfg)
718 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200719 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200721 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200722 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100723 preset->rev == codec->revision_id)) {
724 mutex_unlock(&preset_mutex);
725 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100729 module_put(tbl->owner);
730 }
731 mutex_unlock(&preset_mutex);
732
733 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
734 char name[32];
735 if (!mod_requested)
736 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
737 codec->vendor_id);
738 else
739 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
740 (codec->vendor_id >> 16) & 0xffff);
741 request_module(name);
742 mod_requested++;
743 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 return NULL;
746}
747
748/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200749 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200751static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 const struct hda_vendor_id *c;
754 const char *vendor = NULL;
755 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200756 char tmp[16];
757
758 if (codec->vendor_name)
759 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 for (c = hda_vendor_ids; c->id; c++) {
762 if (c->id == vendor_id) {
763 vendor = c->name;
764 break;
765 }
766 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200767 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 sprintf(tmp, "Generic %04x", vendor_id);
769 vendor = tmp;
770 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200771 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
772 if (!codec->vendor_name)
773 return -ENOMEM;
774
775 get_chip_name:
776 if (codec->chip_name)
777 return 0;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200780 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
781 else {
782 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
783 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
784 }
785 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200786 return -ENOMEM;
787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200791 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100793static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200795 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 hda_nid_t nid;
797
798 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
799 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200800 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200801 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200802 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200803 case AC_GRP_AUDIO_FUNCTION:
804 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200805 codec->afg_function_id = function_id & 0xff;
806 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200807 break;
808 case AC_GRP_MODEM_FUNCTION:
809 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200810 codec->mfg_function_id = function_id & 0xff;
811 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200812 break;
813 default:
814 break;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100820 * read widget caps for each widget and store in cache
821 */
822static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
823{
824 int i;
825 hda_nid_t nid;
826
827 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
828 &codec->start_nid);
829 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200830 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100831 return -ENOMEM;
832 nid = codec->start_nid;
833 for (i = 0; i < codec->num_nodes; i++, nid++)
834 codec->wcaps[i] = snd_hda_param_read(codec, nid,
835 AC_PAR_AUDIO_WIDGET_CAP);
836 return 0;
837}
838
Takashi Iwai3be14142009-02-20 14:11:16 +0100839/* read all pin default configurations and save codec->init_pins */
840static int read_pin_defaults(struct hda_codec *codec)
841{
842 int i;
843 hda_nid_t nid = codec->start_nid;
844
845 for (i = 0; i < codec->num_nodes; i++, nid++) {
846 struct hda_pincfg *pin;
847 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200848 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100849 if (wid_type != AC_WID_PIN)
850 continue;
851 pin = snd_array_new(&codec->init_pins);
852 if (!pin)
853 return -ENOMEM;
854 pin->nid = nid;
855 pin->cfg = snd_hda_codec_read(codec, nid, 0,
856 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200857 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
858 AC_VERB_GET_PIN_WIDGET_CONTROL,
859 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100860 }
861 return 0;
862}
863
864/* look up the given pin config list and return the item matching with NID */
865static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
866 struct snd_array *array,
867 hda_nid_t nid)
868{
869 int i;
870 for (i = 0; i < array->used; i++) {
871 struct hda_pincfg *pin = snd_array_elem(array, i);
872 if (pin->nid == nid)
873 return pin;
874 }
875 return NULL;
876}
877
878/* write a config value for the given NID */
879static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
880 unsigned int cfg)
881{
882 int i;
883 for (i = 0; i < 4; i++) {
884 snd_hda_codec_write(codec, nid, 0,
885 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
886 cfg & 0xff);
887 cfg >>= 8;
888 }
889}
890
891/* set the current pin config value for the given NID.
892 * the value is cached, and read via snd_hda_codec_get_pincfg()
893 */
894int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
895 hda_nid_t nid, unsigned int cfg)
896{
897 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100898 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100899
Takashi Iwaib82855a2009-12-27 11:24:56 +0100900 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
901 return -EINVAL;
902
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100903 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100904 pin = look_up_pincfg(codec, list, nid);
905 if (!pin) {
906 pin = snd_array_new(list);
907 if (!pin)
908 return -ENOMEM;
909 pin->nid = nid;
910 }
911 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100912
913 /* change only when needed; e.g. if the pincfg is already present
914 * in user_pins[], don't write it
915 */
916 cfg = snd_hda_codec_get_pincfg(codec, nid);
917 if (oldcfg != cfg)
918 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100919 return 0;
920}
921
Takashi Iwaid5191e52009-11-16 14:58:17 +0100922/**
923 * snd_hda_codec_set_pincfg - Override a pin default configuration
924 * @codec: the HDA codec
925 * @nid: NID to set the pin config
926 * @cfg: the pin default config value
927 *
928 * Override a pin default configuration value in the cache.
929 * This value can be read by snd_hda_codec_get_pincfg() in a higher
930 * priority than the real hardware value.
931 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100932int snd_hda_codec_set_pincfg(struct hda_codec *codec,
933 hda_nid_t nid, unsigned int cfg)
934{
Takashi Iwai346ff702009-02-23 09:42:57 +0100935 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100936}
937EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
938
Takashi Iwaid5191e52009-11-16 14:58:17 +0100939/**
940 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
941 * @codec: the HDA codec
942 * @nid: NID to get the pin config
943 *
944 * Get the current pin config value of the given pin NID.
945 * If the pincfg value is cached or overridden via sysfs or driver,
946 * returns the cached value.
947 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100948unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
949{
950 struct hda_pincfg *pin;
951
Takashi Iwai3be14142009-02-20 14:11:16 +0100952#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +0100953 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +0100954 if (pin)
955 return pin->cfg;
956#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100957 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
958 if (pin)
959 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100960 pin = look_up_pincfg(codec, &codec->init_pins, nid);
961 if (pin)
962 return pin->cfg;
963 return 0;
964}
965EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
966
967/* restore all current pin configs */
968static void restore_pincfgs(struct hda_codec *codec)
969{
970 int i;
971 for (i = 0; i < codec->init_pins.used; i++) {
972 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
973 set_pincfg(codec, pin->nid,
974 snd_hda_codec_get_pincfg(codec, pin->nid));
975 }
976}
Takashi Iwai54d17402005-11-21 16:33:22 +0100977
Takashi Iwai92ee6162009-12-27 11:18:59 +0100978/**
979 * snd_hda_shutup_pins - Shut up all pins
980 * @codec: the HDA codec
981 *
982 * Clear all pin controls to shup up before suspend for avoiding click noise.
983 * The controls aren't cached so that they can be resumed properly.
984 */
985void snd_hda_shutup_pins(struct hda_codec *codec)
986{
987 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200988 /* don't shut up pins when unloading the driver; otherwise it breaks
989 * the default pin setup at the next load of the driver
990 */
991 if (codec->bus->shutdown)
992 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100993 for (i = 0; i < codec->init_pins.used; i++) {
994 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
995 /* use read here for syncing after issuing each verb */
996 snd_hda_codec_read(codec, pin->nid, 0,
997 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
998 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200999 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001000}
1001EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1002
Mike Waychison1c7276c2011-04-20 12:04:36 -07001003#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001004/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1005static void restore_shutup_pins(struct hda_codec *codec)
1006{
1007 int i;
1008 if (!codec->pins_shutup)
1009 return;
1010 if (codec->bus->shutdown)
1011 return;
1012 for (i = 0; i < codec->init_pins.used; i++) {
1013 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1014 snd_hda_codec_write(codec, pin->nid, 0,
1015 AC_VERB_SET_PIN_WIDGET_CONTROL,
1016 pin->ctrl);
1017 }
1018 codec->pins_shutup = 0;
1019}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001020#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001021
Takashi Iwai01751f52007-08-10 16:59:39 +02001022static void init_hda_cache(struct hda_cache_rec *cache,
1023 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001024static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001025
Takashi Iwai3be14142009-02-20 14:11:16 +01001026/* restore the initial pin cfgs and release all pincfg lists */
1027static void restore_init_pincfgs(struct hda_codec *codec)
1028{
Takashi Iwai346ff702009-02-23 09:42:57 +01001029 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001030 * so that only the values in init_pins are restored
1031 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001032 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001033#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001034 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001035#endif
1036 restore_pincfgs(codec);
1037 snd_array_free(&codec->init_pins);
1038}
1039
Takashi Iwai54d17402005-11-21 16:33:22 +01001040/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001041 * audio-converter setup caches
1042 */
1043struct hda_cvt_setup {
1044 hda_nid_t nid;
1045 u8 stream_tag;
1046 u8 channel_id;
1047 u16 format_id;
1048 unsigned char active; /* cvt is currently used */
1049 unsigned char dirty; /* setups should be cleared */
1050};
1051
1052/* get or create a cache entry for the given audio converter NID */
1053static struct hda_cvt_setup *
1054get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1055{
1056 struct hda_cvt_setup *p;
1057 int i;
1058
1059 for (i = 0; i < codec->cvt_setups.used; i++) {
1060 p = snd_array_elem(&codec->cvt_setups, i);
1061 if (p->nid == nid)
1062 return p;
1063 }
1064 p = snd_array_new(&codec->cvt_setups);
1065 if (p)
1066 p->nid = nid;
1067 return p;
1068}
1069
1070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * codec destructor
1072 */
1073static void snd_hda_codec_free(struct hda_codec *codec)
1074{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001075 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001077 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001078#ifdef CONFIG_SND_HDA_POWER_SAVE
1079 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001080 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001081#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001083 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001084 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001085 snd_array_free(&codec->conn_lists);
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 &&
1331 get_wcaps_type(get_wcaps(codec, 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 Iwaia68d5a542010-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 Iwaia68d5a542010-03-30 18:03:44 +02001452 return NULL;
1453}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Takashi Iwaia68d5a542010-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 Iwai467126462010-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 Iwai467126462010-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 Iwaid5191e52009-11-16 14:58:17 +01002190/**
2191 * snd_hda_add_vmaster - create a virtual master control and add slaves
2192 * @codec: HD-audio codec
2193 * @name: vmaster control name
2194 * @tlv: TLV data (optional)
2195 * @slaves: slave control names (optional)
2196 *
2197 * Create a virtual master control with the given name. The TLV data
2198 * must be either NULL or a valid data.
2199 *
2200 * @slaves is a NULL-terminated array of strings, each of which is a
2201 * slave control name. All controls with these names are assigned to
2202 * the new virtual master control.
2203 *
2204 * This function returns zero if successful or a negative error code.
2205 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002206int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002207 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002208{
2209 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002210 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002211 int err;
2212
Takashi Iwai2f085542008-02-22 18:43:50 +01002213 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2214 ;
2215 if (!*s) {
2216 snd_printdd("No slave found for %s\n", name);
2217 return 0;
2218 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002219 kctl = snd_ctl_make_virtual_master(name, tlv);
2220 if (!kctl)
2221 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002222 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002223 if (err < 0)
2224 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002225
Takashi Iwai2134ea42008-01-10 16:53:55 +01002226 for (s = slaves; *s; s++) {
2227 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002228 int i = 0;
2229 for (;;) {
2230 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2231 if (!sctl) {
2232 if (!i)
2233 snd_printdd("Cannot find slave %s, "
2234 "skipped\n", *s);
2235 break;
2236 }
2237 err = snd_ctl_add_slave(kctl, sctl);
2238 if (err < 0)
2239 return err;
2240 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002241 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002242 }
2243 return 0;
2244}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002245EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002246
Takashi Iwaid5191e52009-11-16 14:58:17 +01002247/**
2248 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2249 *
2250 * The control element is supposed to have the private_value field
2251 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2252 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002253int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
2256 int chs = get_amp_channels(kcontrol);
2257
2258 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2259 uinfo->count = chs == 3 ? 2 : 1;
2260 uinfo->value.integer.min = 0;
2261 uinfo->value.integer.max = 1;
2262 return 0;
2263}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002264EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Takashi Iwaid5191e52009-11-16 14:58:17 +01002266/**
2267 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2268 *
2269 * The control element is supposed to have the private_value field
2270 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2271 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002272int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2273 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2276 hda_nid_t nid = get_amp_nid(kcontrol);
2277 int chs = get_amp_channels(kcontrol);
2278 int dir = get_amp_direction(kcontrol);
2279 int idx = get_amp_index(kcontrol);
2280 long *valp = ucontrol->value.integer.value;
2281
2282 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002283 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002284 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002286 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002287 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return 0;
2289}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002290EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Takashi Iwaid5191e52009-11-16 14:58:17 +01002292/**
2293 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2294 *
2295 * The control element is supposed to have the private_value field
2296 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2297 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002298int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2302 hda_nid_t nid = get_amp_nid(kcontrol);
2303 int chs = get_amp_channels(kcontrol);
2304 int dir = get_amp_direction(kcontrol);
2305 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 long *valp = ucontrol->value.integer.value;
2307 int change = 0;
2308
Takashi Iwaicb53c622007-08-10 17:21:45 +02002309 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002310 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002311 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002312 HDA_AMP_MUTE,
2313 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002314 valp++;
2315 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002316 if (chs & 2)
2317 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002318 HDA_AMP_MUTE,
2319 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002320 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002321 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return change;
2323}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002324EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Takashi Iwai67d634c2009-11-16 15:35:59 +01002326#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002327/**
2328 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2329 *
2330 * This function calls snd_hda_enable_beep_device(), which behaves differently
2331 * depending on beep_mode option.
2332 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002333int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2335{
2336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2337 long *valp = ucontrol->value.integer.value;
2338
2339 snd_hda_enable_beep_device(codec, *valp);
2340 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2341}
2342EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002343#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/*
Takashi Iwai985be542005-11-02 18:26:49 +01002346 * bound volume controls
2347 *
2348 * bind multiple volumes (# indices, from 0)
2349 */
2350
2351#define AMP_VAL_IDX_SHIFT 19
2352#define AMP_VAL_IDX_MASK (0x0f<<19)
2353
Takashi Iwaid5191e52009-11-16 14:58:17 +01002354/**
2355 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2356 *
2357 * The control element is supposed to have the private_value field
2358 * set up via HDA_BIND_MUTE*() macros.
2359 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002360int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2361 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002362{
2363 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2364 unsigned long pval;
2365 int err;
2366
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002367 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002368 pval = kcontrol->private_value;
2369 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2370 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2371 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002372 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002373 return err;
2374}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002375EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002376
Takashi Iwaid5191e52009-11-16 14:58:17 +01002377/**
2378 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2379 *
2380 * The control element is supposed to have the private_value field
2381 * set up via HDA_BIND_MUTE*() macros.
2382 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002383int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2384 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002385{
2386 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2387 unsigned long pval;
2388 int i, indices, err = 0, change = 0;
2389
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002390 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002391 pval = kcontrol->private_value;
2392 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2393 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002394 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2395 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002396 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2397 if (err < 0)
2398 break;
2399 change |= err;
2400 }
2401 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002402 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002403 return err < 0 ? err : change;
2404}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002405EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002406
Takashi Iwaid5191e52009-11-16 14:58:17 +01002407/**
2408 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2409 *
2410 * The control element is supposed to have the private_value field
2411 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002412 */
2413int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2414 struct snd_ctl_elem_info *uinfo)
2415{
2416 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2417 struct hda_bind_ctls *c;
2418 int err;
2419
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002420 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002421 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002422 kcontrol->private_value = *c->values;
2423 err = c->ops->info(kcontrol, uinfo);
2424 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002425 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002426 return err;
2427}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002428EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002429
Takashi Iwaid5191e52009-11-16 14:58:17 +01002430/**
2431 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2432 *
2433 * The control element is supposed to have the private_value field
2434 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2435 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002436int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2437 struct snd_ctl_elem_value *ucontrol)
2438{
2439 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2440 struct hda_bind_ctls *c;
2441 int err;
2442
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002443 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002444 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002445 kcontrol->private_value = *c->values;
2446 err = c->ops->get(kcontrol, ucontrol);
2447 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002448 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002449 return err;
2450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002451EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002452
Takashi Iwaid5191e52009-11-16 14:58:17 +01002453/**
2454 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2455 *
2456 * The control element is supposed to have the private_value field
2457 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2458 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002459int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2460 struct snd_ctl_elem_value *ucontrol)
2461{
2462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2463 struct hda_bind_ctls *c;
2464 unsigned long *vals;
2465 int err = 0, change = 0;
2466
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002467 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002468 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002469 for (vals = c->values; *vals; vals++) {
2470 kcontrol->private_value = *vals;
2471 err = c->ops->put(kcontrol, ucontrol);
2472 if (err < 0)
2473 break;
2474 change |= err;
2475 }
2476 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002477 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002478 return err < 0 ? err : change;
2479}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002480EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002481
Takashi Iwaid5191e52009-11-16 14:58:17 +01002482/**
2483 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2484 *
2485 * The control element is supposed to have the private_value field
2486 * set up via HDA_BIND_VOL() macro.
2487 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002488int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2489 unsigned int size, unsigned int __user *tlv)
2490{
2491 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2492 struct hda_bind_ctls *c;
2493 int err;
2494
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002495 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002496 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002497 kcontrol->private_value = *c->values;
2498 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2499 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002500 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002501 return err;
2502}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002503EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002504
2505struct hda_ctl_ops snd_hda_bind_vol = {
2506 .info = snd_hda_mixer_amp_volume_info,
2507 .get = snd_hda_mixer_amp_volume_get,
2508 .put = snd_hda_mixer_amp_volume_put,
2509 .tlv = snd_hda_mixer_amp_tlv
2510};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002511EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002512
2513struct hda_ctl_ops snd_hda_bind_sw = {
2514 .info = snd_hda_mixer_amp_switch_info,
2515 .get = snd_hda_mixer_amp_switch_get,
2516 .put = snd_hda_mixer_amp_switch_put,
2517 .tlv = snd_hda_mixer_amp_tlv
2518};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002519EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002520
2521/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 * SPDIF out controls
2523 */
2524
Takashi Iwai0ba21762007-04-16 11:29:14 +02002525static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2526 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
2528 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2529 uinfo->count = 1;
2530 return 0;
2531}
2532
Takashi Iwai0ba21762007-04-16 11:29:14 +02002533static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2534 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
2536 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2537 IEC958_AES0_NONAUDIO |
2538 IEC958_AES0_CON_EMPHASIS_5015 |
2539 IEC958_AES0_CON_NOT_COPYRIGHT;
2540 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2541 IEC958_AES1_CON_ORIGINAL;
2542 return 0;
2543}
2544
Takashi Iwai0ba21762007-04-16 11:29:14 +02002545static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2549 IEC958_AES0_NONAUDIO |
2550 IEC958_AES0_PRO_EMPHASIS_5015;
2551 return 0;
2552}
2553
Takashi Iwai0ba21762007-04-16 11:29:14 +02002554static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
2557 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2558
2559 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2560 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2561 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2562 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2563
2564 return 0;
2565}
2566
2567/* convert from SPDIF status bits to HDA SPDIF bits
2568 * bit 0 (DigEn) is always set zero (to be filled later)
2569 */
2570static unsigned short convert_from_spdif_status(unsigned int sbits)
2571{
2572 unsigned short val = 0;
2573
2574 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002575 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002577 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002579 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2580 IEC958_AES0_PRO_EMPHASIS_5015)
2581 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002583 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2584 IEC958_AES0_CON_EMPHASIS_5015)
2585 val |= AC_DIG1_EMPHASIS;
2586 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2587 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002589 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2591 }
2592 return val;
2593}
2594
2595/* convert to SPDIF status bits from HDA SPDIF bits
2596 */
2597static unsigned int convert_to_spdif_status(unsigned short val)
2598{
2599 unsigned int sbits = 0;
2600
Takashi Iwai0ba21762007-04-16 11:29:14 +02002601 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002603 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 sbits |= IEC958_AES0_PROFESSIONAL;
2605 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002606 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2608 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002611 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002613 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2615 sbits |= val & (0x7f << 8);
2616 }
2617 return sbits;
2618}
2619
Takashi Iwai2f728532008-09-25 16:32:41 +02002620/* set digital convert verbs both for the given NID and its slaves */
2621static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2622 int verb, int val)
2623{
Takashi Iwaidda14412011-05-02 11:29:30 +02002624 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002625
Takashi Iwai9e976972008-11-25 08:17:20 +01002626 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002627 d = codec->slave_dig_outs;
2628 if (!d)
2629 return;
2630 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002631 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002632}
2633
2634static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2635 int dig1, int dig2)
2636{
2637 if (dig1 != -1)
2638 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2639 if (dig2 != -1)
2640 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2641}
2642
Takashi Iwai0ba21762007-04-16 11:29:14 +02002643static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2644 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
2646 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2647 hda_nid_t nid = kcontrol->private_value;
2648 unsigned short val;
2649 int change;
2650
Ingo Molnar62932df2006-01-16 16:34:20 +01002651 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 codec->spdif_status = ucontrol->value.iec958.status[0] |
2653 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2654 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2655 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2656 val = convert_from_spdif_status(codec->spdif_status);
2657 val |= codec->spdif_ctls & 1;
2658 change = codec->spdif_ctls != val;
2659 codec->spdif_ctls = val;
2660
Takashi Iwai2f728532008-09-25 16:32:41 +02002661 if (change)
2662 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Ingo Molnar62932df2006-01-16 16:34:20 +01002664 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return change;
2666}
2667
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002668#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Takashi Iwai0ba21762007-04-16 11:29:14 +02002670static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2671 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2674
Takashi Iwai0ba21762007-04-16 11:29:14 +02002675 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 return 0;
2677}
2678
Takashi Iwai0ba21762007-04-16 11:29:14 +02002679static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2683 hda_nid_t nid = kcontrol->private_value;
2684 unsigned short val;
2685 int change;
2686
Ingo Molnar62932df2006-01-16 16:34:20 +01002687 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002688 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002690 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002692 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002694 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002695 /* unmute amp switch (if any) */
2696 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002697 (val & AC_DIG1_ENABLE))
2698 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2699 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002701 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 return change;
2703}
2704
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002705static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 {
2707 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2708 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002709 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 .info = snd_hda_spdif_mask_info,
2711 .get = snd_hda_spdif_cmask_get,
2712 },
2713 {
2714 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2715 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002716 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 .info = snd_hda_spdif_mask_info,
2718 .get = snd_hda_spdif_pmask_get,
2719 },
2720 {
2721 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002722 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 .info = snd_hda_spdif_mask_info,
2724 .get = snd_hda_spdif_default_get,
2725 .put = snd_hda_spdif_default_put,
2726 },
2727 {
2728 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002729 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 .info = snd_hda_spdif_out_switch_info,
2731 .get = snd_hda_spdif_out_switch_get,
2732 .put = snd_hda_spdif_out_switch_put,
2733 },
2734 { } /* end */
2735};
2736
2737/**
2738 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2739 * @codec: the HDA codec
2740 * @nid: audio out widget NID
2741 *
2742 * Creates controls related with the SPDIF output.
2743 * Called from each patch supporting the SPDIF out.
2744 *
2745 * Returns 0 if successful, or a negative error code.
2746 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002747int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002750 struct snd_kcontrol *kctl;
2751 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002752 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Takashi Iwai1afe2062010-12-23 10:17:52 +01002754 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2755 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002756 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2757 return -EBUSY;
2758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2760 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002761 if (!kctl)
2762 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002763 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002765 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002766 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return err;
2768 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002769 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002770 snd_hda_codec_read(codec, nid, 0,
2771 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2773 return 0;
2774}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002775EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002778 * SPDIF sharing with analog output
2779 */
2780static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2781 struct snd_ctl_elem_value *ucontrol)
2782{
2783 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2784 ucontrol->value.integer.value[0] = mout->share_spdif;
2785 return 0;
2786}
2787
2788static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2789 struct snd_ctl_elem_value *ucontrol)
2790{
2791 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2792 mout->share_spdif = !!ucontrol->value.integer.value[0];
2793 return 0;
2794}
2795
2796static struct snd_kcontrol_new spdif_share_sw = {
2797 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2798 .name = "IEC958 Default PCM Playback Switch",
2799 .info = snd_ctl_boolean_mono_info,
2800 .get = spdif_share_sw_get,
2801 .put = spdif_share_sw_put,
2802};
2803
Takashi Iwaid5191e52009-11-16 14:58:17 +01002804/**
2805 * snd_hda_create_spdif_share_sw - create Default PCM switch
2806 * @codec: the HDA codec
2807 * @mout: multi-out instance
2808 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002809int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2810 struct hda_multi_out *mout)
2811{
2812 if (!mout->dig_out_nid)
2813 return 0;
2814 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002815 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2816 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002817}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002818EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002819
2820/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 * SPDIF input
2822 */
2823
2824#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2825
Takashi Iwai0ba21762007-04-16 11:29:14 +02002826static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2830
2831 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2832 return 0;
2833}
2834
Takashi Iwai0ba21762007-04-16 11:29:14 +02002835static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
2838 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2839 hda_nid_t nid = kcontrol->private_value;
2840 unsigned int val = !!ucontrol->value.integer.value[0];
2841 int change;
2842
Ingo Molnar62932df2006-01-16 16:34:20 +01002843 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002845 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002847 snd_hda_codec_write_cache(codec, nid, 0,
2848 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002850 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return change;
2852}
2853
Takashi Iwai0ba21762007-04-16 11:29:14 +02002854static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2855 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
2857 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2858 hda_nid_t nid = kcontrol->private_value;
2859 unsigned short val;
2860 unsigned int sbits;
2861
Andrew Paprocki3982d172007-12-19 12:13:44 +01002862 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 sbits = convert_to_spdif_status(val);
2864 ucontrol->value.iec958.status[0] = sbits;
2865 ucontrol->value.iec958.status[1] = sbits >> 8;
2866 ucontrol->value.iec958.status[2] = sbits >> 16;
2867 ucontrol->value.iec958.status[3] = sbits >> 24;
2868 return 0;
2869}
2870
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002871static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 {
2873 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002874 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 .info = snd_hda_spdif_in_switch_info,
2876 .get = snd_hda_spdif_in_switch_get,
2877 .put = snd_hda_spdif_in_switch_put,
2878 },
2879 {
2880 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2881 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002882 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 .info = snd_hda_spdif_mask_info,
2884 .get = snd_hda_spdif_in_status_get,
2885 },
2886 { } /* end */
2887};
2888
2889/**
2890 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2891 * @codec: the HDA codec
2892 * @nid: audio in widget NID
2893 *
2894 * Creates controls related with the SPDIF input.
2895 * Called from each patch supporting the SPDIF in.
2896 *
2897 * Returns 0 if successful, or a negative error code.
2898 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002899int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
2901 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002902 struct snd_kcontrol *kctl;
2903 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002904 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Takashi Iwai1afe2062010-12-23 10:17:52 +01002906 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
2907 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002908 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2909 return -EBUSY;
2910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2912 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002913 if (!kctl)
2914 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002916 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002917 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return err;
2919 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002920 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002921 snd_hda_codec_read(codec, nid, 0,
2922 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002923 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return 0;
2925}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002926EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Takashi Iwaicb53c622007-08-10 17:21:45 +02002928#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002929/*
2930 * command cache
2931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002933/* build a 32bit cache key with the widget id and the command parameter */
2934#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2935#define get_cmd_cache_nid(key) ((key) & 0xff)
2936#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2937
2938/**
2939 * snd_hda_codec_write_cache - send a single command with caching
2940 * @codec: the HDA codec
2941 * @nid: NID to send the command
2942 * @direct: direct flag
2943 * @verb: the verb to send
2944 * @parm: the parameter for the verb
2945 *
2946 * Send a single command without waiting for response.
2947 *
2948 * Returns 0 if successful, or a negative error code.
2949 */
2950int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2951 int direct, unsigned int verb, unsigned int parm)
2952{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002953 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2954 struct hda_cache_head *c;
2955 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002956
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002957 if (err < 0)
2958 return err;
2959 /* parm may contain the verb stuff for get/set amp */
2960 verb = verb | (parm >> 8);
2961 parm &= 0xff;
2962 key = build_cmd_cache_key(nid, verb);
2963 mutex_lock(&codec->bus->cmd_mutex);
2964 c = get_alloc_hash(&codec->cmd_cache, key);
2965 if (c)
2966 c->val = parm;
2967 mutex_unlock(&codec->bus->cmd_mutex);
2968 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002969}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002970EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002971
Takashi Iwaid5191e52009-11-16 14:58:17 +01002972/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02002973 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2974 * @codec: the HDA codec
2975 * @nid: NID to send the command
2976 * @direct: direct flag
2977 * @verb: the verb to send
2978 * @parm: the parameter for the verb
2979 *
2980 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2981 * command if the parameter is already identical with the cached value.
2982 * If not, it sends the command and refreshes the cache.
2983 *
2984 * Returns 0 if successful, or a negative error code.
2985 */
2986int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2987 int direct, unsigned int verb, unsigned int parm)
2988{
2989 struct hda_cache_head *c;
2990 u32 key;
2991
2992 /* parm may contain the verb stuff for get/set amp */
2993 verb = verb | (parm >> 8);
2994 parm &= 0xff;
2995 key = build_cmd_cache_key(nid, verb);
2996 mutex_lock(&codec->bus->cmd_mutex);
2997 c = get_hash(&codec->cmd_cache, key);
2998 if (c && c->val == parm) {
2999 mutex_unlock(&codec->bus->cmd_mutex);
3000 return 0;
3001 }
3002 mutex_unlock(&codec->bus->cmd_mutex);
3003 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3004}
3005EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3006
3007/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003008 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3009 * @codec: HD-audio codec
3010 *
3011 * Execute all verbs recorded in the command caches to resume.
3012 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003013void snd_hda_codec_resume_cache(struct hda_codec *codec)
3014{
Takashi Iwai603c4012008-07-30 15:01:44 +02003015 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003016 int i;
3017
Takashi Iwai603c4012008-07-30 15:01:44 +02003018 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003019 u32 key = buffer->key;
3020 if (!key)
3021 continue;
3022 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3023 get_cmd_cache_cmd(key), buffer->val);
3024 }
3025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003026EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003027
3028/**
3029 * snd_hda_sequence_write_cache - sequence writes with caching
3030 * @codec: the HDA codec
3031 * @seq: VERB array to send
3032 *
3033 * Send the commands sequentially from the given array.
3034 * Thte commands are recorded on cache for power-save and resume.
3035 * The array must be terminated with NID=0.
3036 */
3037void snd_hda_sequence_write_cache(struct hda_codec *codec,
3038 const struct hda_verb *seq)
3039{
3040 for (; seq->nid; seq++)
3041 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3042 seq->param);
3043}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003044EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003045#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003046
Takashi Iwai54d17402005-11-21 16:33:22 +01003047/*
3048 * set power state of the codec
3049 */
3050static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3051 unsigned int power_state)
3052{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003053 hda_nid_t nid;
3054 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003055
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003056 /* this delay seems necessary to avoid click noise at power-down */
3057 if (power_state == AC_PWRST_D3)
3058 msleep(100);
3059 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003060 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003061 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003062 if (power_state == AC_PWRST_D0 &&
3063 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003064 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003065
Takashi Iwaicb53c622007-08-10 17:21:45 +02003066 nid = codec->start_nid;
3067 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003068 unsigned int wcaps = get_wcaps(codec, nid);
3069 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003070 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003071 if (power_state == AC_PWRST_D3 &&
3072 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003073 unsigned int pincap;
3074 /*
3075 * don't power down the widget if it controls
3076 * eapd and EAPD_BTLENABLE is set.
3077 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003078 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003079 if (pincap & AC_PINCAP_EAPD) {
3080 int eapd = snd_hda_codec_read(codec,
3081 nid, 0,
3082 AC_VERB_GET_EAPD_BTLENABLE, 0);
3083 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003084 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003085 continue;
3086 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003087 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003088 snd_hda_codec_write(codec, nid, 0,
3089 AC_VERB_SET_POWER_STATE,
3090 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003091 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003092 }
3093
Takashi Iwaicb53c622007-08-10 17:21:45 +02003094 if (power_state == AC_PWRST_D0) {
3095 unsigned long end_time;
3096 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003097 /* wait until the codec reachs to D0 */
3098 end_time = jiffies + msecs_to_jiffies(500);
3099 do {
3100 state = snd_hda_codec_read(codec, fg, 0,
3101 AC_VERB_GET_POWER_STATE, 0);
3102 if (state == power_state)
3103 break;
3104 msleep(1);
3105 } while (time_after_eq(end_time, jiffies));
3106 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003107}
3108
Takashi Iwai11aeff02008-07-30 15:01:46 +02003109#ifdef CONFIG_SND_HDA_HWDEP
3110/* execute additional init verbs */
3111static void hda_exec_init_verbs(struct hda_codec *codec)
3112{
3113 if (codec->init_verbs.list)
3114 snd_hda_sequence_write(codec, codec->init_verbs.list);
3115}
3116#else
3117static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3118#endif
3119
Takashi Iwaicb53c622007-08-10 17:21:45 +02003120#ifdef SND_HDA_NEEDS_RESUME
3121/*
3122 * call suspend and power-down; used both from PM and power-save
3123 */
3124static void hda_call_codec_suspend(struct hda_codec *codec)
3125{
3126 if (codec->patch_ops.suspend)
3127 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003128 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003129 hda_set_power_state(codec,
3130 codec->afg ? codec->afg : codec->mfg,
3131 AC_PWRST_D3);
3132#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003133 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003134 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003135 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003136 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003137 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003138#endif
3139}
3140
3141/*
3142 * kick up codec; used both from PM and power-save
3143 */
3144static void hda_call_codec_resume(struct hda_codec *codec)
3145{
3146 hda_set_power_state(codec,
3147 codec->afg ? codec->afg : codec->mfg,
3148 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003149 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003150 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003151 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003152 if (codec->patch_ops.resume)
3153 codec->patch_ops.resume(codec);
3154 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003155 if (codec->patch_ops.init)
3156 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003157 snd_hda_codec_resume_amp(codec);
3158 snd_hda_codec_resume_cache(codec);
3159 }
3160}
3161#endif /* SND_HDA_NEEDS_RESUME */
3162
Takashi Iwai54d17402005-11-21 16:33:22 +01003163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164/**
3165 * snd_hda_build_controls - build mixer controls
3166 * @bus: the BUS
3167 *
3168 * Creates mixer controls for each codec included in the bus.
3169 *
3170 * Returns 0 if successful, otherwise a negative error code.
3171 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003172int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003174 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Takashi Iwai0ba21762007-04-16 11:29:14 +02003176 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003177 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003178 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003179 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003180 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003181 err = snd_hda_codec_reset(codec);
3182 if (err < 0) {
3183 printk(KERN_ERR
3184 "hda_codec: cannot revert codec\n");
3185 return err;
3186 }
3187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003189 return 0;
3190}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003191EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003193int snd_hda_codec_build_controls(struct hda_codec *codec)
3194{
3195 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003196 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003197 /* continue to initialize... */
3198 if (codec->patch_ops.init)
3199 err = codec->patch_ops.init(codec);
3200 if (!err && codec->patch_ops.build_controls)
3201 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003202 if (err < 0)
3203 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return 0;
3205}
3206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207/*
3208 * stream formats
3209 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003210struct hda_rate_tbl {
3211 unsigned int hz;
3212 unsigned int alsa_bits;
3213 unsigned int hda_fmt;
3214};
3215
Takashi Iwai92f10b32010-08-03 14:21:00 +02003216/* rate = base * mult / div */
3217#define HDA_RATE(base, mult, div) \
3218 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3219 (((div) - 1) << AC_FMT_DIV_SHIFT))
3220
Takashi Iwaibefdf312005-08-22 13:57:55 +02003221static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003223
3224 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003225 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3226 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3227 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3228 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3229 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3230 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3231 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3232 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3233 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3234 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3235 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003236#define AC_PAR_PCM_RATE_BITS 11
3237 /* up to bits 10, 384kHZ isn't supported properly */
3238
3239 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003240 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003241
Takashi Iwaibefdf312005-08-22 13:57:55 +02003242 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243};
3244
3245/**
3246 * snd_hda_calc_stream_format - calculate format bitset
3247 * @rate: the sample rate
3248 * @channels: the number of channels
3249 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3250 * @maxbps: the max. bps
3251 *
3252 * Calculate the format bitset from the given rate, channels and th PCM format.
3253 *
3254 * Return zero if invalid.
3255 */
3256unsigned int snd_hda_calc_stream_format(unsigned int rate,
3257 unsigned int channels,
3258 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003259 unsigned int maxbps,
3260 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
3262 int i;
3263 unsigned int val = 0;
3264
Takashi Iwaibefdf312005-08-22 13:57:55 +02003265 for (i = 0; rate_bits[i].hz; i++)
3266 if (rate_bits[i].hz == rate) {
3267 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 break;
3269 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003270 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 snd_printdd("invalid rate %d\n", rate);
3272 return 0;
3273 }
3274
3275 if (channels == 0 || channels > 8) {
3276 snd_printdd("invalid channels %d\n", channels);
3277 return 0;
3278 }
3279 val |= channels - 1;
3280
3281 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003282 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003283 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003284 break;
3285 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003286 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 case 20:
3289 case 24:
3290 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003291 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003292 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003294 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003296 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 break;
3298 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003299 snd_printdd("invalid format width %d\n",
3300 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return 0;
3302 }
3303
Anssi Hannula32c168c2010-08-03 13:28:57 +03003304 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003305 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return val;
3308}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003309EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003311static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3312{
3313 unsigned int val = 0;
3314 if (nid != codec->afg &&
3315 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3316 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3317 if (!val || val == -1)
3318 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3319 if (!val || val == -1)
3320 return 0;
3321 return val;
3322}
3323
3324static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3325{
3326 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3327 get_pcm_param);
3328}
3329
3330static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3331{
3332 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3333 if (!streams || streams == -1)
3334 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3335 if (!streams || streams == -1)
3336 return 0;
3337 return streams;
3338}
3339
3340static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3341{
3342 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3343 get_stream_param);
3344}
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346/**
3347 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3348 * @codec: the HDA codec
3349 * @nid: NID to query
3350 * @ratesp: the pointer to store the detected rate bitflags
3351 * @formatsp: the pointer to store the detected formats
3352 * @bpsp: the pointer to store the detected format widths
3353 *
3354 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3355 * or @bsps argument is ignored.
3356 *
3357 * Returns 0 if successful, otherwise a negative error code.
3358 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003359static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3361{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003362 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003364 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003365 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
3367 if (ratesp) {
3368 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003369 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003371 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003373 if (rates == 0) {
3374 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3375 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3376 nid, val,
3377 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3378 return -EIO;
3379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 *ratesp = rates;
3381 }
3382
3383 if (formatsp || bpsp) {
3384 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003385 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003387 streams = query_stream_param(codec, nid);
3388 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
3391 bps = 0;
3392 if (streams & AC_SUPFMT_PCM) {
3393 if (val & AC_SUPPCM_BITS_8) {
3394 formats |= SNDRV_PCM_FMTBIT_U8;
3395 bps = 8;
3396 }
3397 if (val & AC_SUPPCM_BITS_16) {
3398 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3399 bps = 16;
3400 }
3401 if (wcaps & AC_WCAP_DIGITAL) {
3402 if (val & AC_SUPPCM_BITS_32)
3403 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3404 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3405 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3406 if (val & AC_SUPPCM_BITS_24)
3407 bps = 24;
3408 else if (val & AC_SUPPCM_BITS_20)
3409 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003410 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3411 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3413 if (val & AC_SUPPCM_BITS_32)
3414 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 else if (val & AC_SUPPCM_BITS_24)
3416 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003417 else if (val & AC_SUPPCM_BITS_20)
3418 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 }
3420 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003421 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003423 if (!bps)
3424 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003425 }
3426 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003427 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 /* temporary hack: we have still no proper support
3429 * for the direct AC3 stream...
3430 */
3431 formats |= SNDRV_PCM_FMTBIT_U8;
3432 bps = 8;
3433 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003434 if (formats == 0) {
3435 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3436 "(nid=0x%x, val=0x%x, ovrd=%i, "
3437 "streams=0x%x)\n",
3438 nid, val,
3439 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3440 streams);
3441 return -EIO;
3442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (formatsp)
3444 *formatsp = formats;
3445 if (bpsp)
3446 *bpsp = bps;
3447 }
3448
3449 return 0;
3450}
3451
3452/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003453 * snd_hda_is_supported_format - Check the validity of the format
3454 * @codec: HD-audio codec
3455 * @nid: NID to check
3456 * @format: the HD-audio format value to check
3457 *
3458 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 *
3460 * Returns 1 if supported, 0 if not.
3461 */
3462int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3463 unsigned int format)
3464{
3465 int i;
3466 unsigned int val = 0, rate, stream;
3467
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003468 val = query_pcm_param(codec, nid);
3469 if (!val)
3470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003473 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003474 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 if (val & (1 << i))
3476 break;
3477 return 0;
3478 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003479 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return 0;
3481
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003482 stream = query_stream_param(codec, nid);
3483 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return 0;
3485
3486 if (stream & AC_SUPFMT_PCM) {
3487 switch (format & 0xf0) {
3488 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003489 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 return 0;
3491 break;
3492 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003493 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return 0;
3495 break;
3496 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003497 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return 0;
3499 break;
3500 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003501 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return 0;
3503 break;
3504 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003505 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 return 0;
3507 break;
3508 default:
3509 return 0;
3510 }
3511 } else {
3512 /* FIXME: check for float32 and AC3? */
3513 }
3514
3515 return 1;
3516}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003517EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
3519/*
3520 * PCM stuff
3521 */
3522static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3523 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003524 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
3526 return 0;
3527}
3528
3529static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3530 struct hda_codec *codec,
3531 unsigned int stream_tag,
3532 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003533 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
3535 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3536 return 0;
3537}
3538
3539static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3540 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003541 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
Takashi Iwai888afa12008-03-18 09:57:50 +01003543 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 return 0;
3545}
3546
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003547static int set_pcm_default_values(struct hda_codec *codec,
3548 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003550 int err;
3551
Takashi Iwai0ba21762007-04-16 11:29:14 +02003552 /* query support PCM information from the given NID */
3553 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003554 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003555 info->rates ? NULL : &info->rates,
3556 info->formats ? NULL : &info->formats,
3557 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003558 if (err < 0)
3559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 }
3561 if (info->ops.open == NULL)
3562 info->ops.open = hda_pcm_default_open_close;
3563 if (info->ops.close == NULL)
3564 info->ops.close = hda_pcm_default_open_close;
3565 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003566 if (snd_BUG_ON(!info->nid))
3567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 info->ops.prepare = hda_pcm_default_prepare;
3569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003571 if (snd_BUG_ON(!info->nid))
3572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 info->ops.cleanup = hda_pcm_default_cleanup;
3574 }
3575 return 0;
3576}
3577
Takashi Iwaieb541332010-08-06 13:48:11 +02003578/*
3579 * codec prepare/cleanup entries
3580 */
3581int snd_hda_codec_prepare(struct hda_codec *codec,
3582 struct hda_pcm_stream *hinfo,
3583 unsigned int stream,
3584 unsigned int format,
3585 struct snd_pcm_substream *substream)
3586{
3587 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003588 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003589 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3590 if (ret >= 0)
3591 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003592 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003593 return ret;
3594}
3595EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3596
3597void snd_hda_codec_cleanup(struct hda_codec *codec,
3598 struct hda_pcm_stream *hinfo,
3599 struct snd_pcm_substream *substream)
3600{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003601 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003602 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003603 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003604}
3605EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3606
Takashi Iwaid5191e52009-11-16 14:58:17 +01003607/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003608const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3609 "Audio", "SPDIF", "HDMI", "Modem"
3610};
3611
Takashi Iwai176d5332008-07-30 15:01:44 +02003612/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003613 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003614 *
3615 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003616 */
3617static int get_empty_pcm_device(struct hda_bus *bus, int type)
3618{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003619 /* audio device indices; not linear to keep compatibility */
3620 static int audio_idx[HDA_PCM_NTYPES][5] = {
3621 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3622 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003623 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003624 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003625 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003626 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003627
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003628 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003629 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3630 return -EINVAL;
3631 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003632
3633 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3634 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3635 return audio_idx[type][i];
3636
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003637 snd_printk(KERN_WARNING "Too many %s devices\n",
3638 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003639 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003640}
3641
3642/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003643 * attach a new PCM stream
3644 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003645static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003646{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003647 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003648 struct hda_pcm_stream *info;
3649 int stream, err;
3650
Takashi Iwaib91f0802008-11-04 08:43:08 +01003651 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003652 return -EINVAL;
3653 for (stream = 0; stream < 2; stream++) {
3654 info = &pcm->stream[stream];
3655 if (info->substreams) {
3656 err = set_pcm_default_values(codec, info);
3657 if (err < 0)
3658 return err;
3659 }
3660 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003661 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003662}
3663
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003664/* assign all PCMs of the given codec */
3665int snd_hda_codec_build_pcms(struct hda_codec *codec)
3666{
3667 unsigned int pcm;
3668 int err;
3669
3670 if (!codec->num_pcms) {
3671 if (!codec->patch_ops.build_pcms)
3672 return 0;
3673 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003674 if (err < 0) {
3675 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003676 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003677 err = snd_hda_codec_reset(codec);
3678 if (err < 0) {
3679 printk(KERN_ERR
3680 "hda_codec: cannot revert codec\n");
3681 return err;
3682 }
3683 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003684 }
3685 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3686 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3687 int dev;
3688
3689 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003690 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003691
3692 if (!cpcm->pcm) {
3693 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3694 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003695 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003696 cpcm->device = dev;
3697 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003698 if (err < 0) {
3699 printk(KERN_ERR "hda_codec: cannot attach "
3700 "PCM stream %d for codec #%d\n",
3701 dev, codec->addr);
3702 continue; /* no fatal error */
3703 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003704 }
3705 }
3706 return 0;
3707}
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709/**
3710 * snd_hda_build_pcms - build PCM information
3711 * @bus: the BUS
3712 *
3713 * Create PCM information for each codec included in the bus.
3714 *
3715 * The build_pcms codec patch is requested to set up codec->num_pcms and
3716 * codec->pcm_info properly. The array is referred by the top-level driver
3717 * to create its PCM instances.
3718 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3719 * callback.
3720 *
3721 * At least, substreams, channels_min and channels_max must be filled for
3722 * each stream. substreams = 0 indicates that the stream doesn't exist.
3723 * When rates and/or formats are zero, the supported values are queried
3724 * from the given nid. The nid is used also by the default ops.prepare
3725 * and ops.cleanup callbacks.
3726 *
3727 * The driver needs to call ops.open in its open callback. Similarly,
3728 * ops.close is supposed to be called in the close callback.
3729 * ops.prepare should be called in the prepare or hw_params callback
3730 * with the proper parameters for set up.
3731 * ops.cleanup should be called in hw_free for clean up of streams.
3732 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003733 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003735int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003737 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Takashi Iwai0ba21762007-04-16 11:29:14 +02003739 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003740 int err = snd_hda_codec_build_pcms(codec);
3741 if (err < 0)
3742 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 }
3744 return 0;
3745}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003746EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748/**
3749 * snd_hda_check_board_config - compare the current codec with the config table
3750 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003751 * @num_configs: number of config enums
3752 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 * @tbl: configuration table, terminated by null entries
3754 *
3755 * Compares the modelname or PCI subsystem id of the current codec with the
3756 * given configuration table. If a matching entry is found, returns its
3757 * config value (supposed to be 0 or positive).
3758 *
3759 * If no entries are matching, the function returns a negative value.
3760 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003761int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003762 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003763 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003765 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003766 int i;
3767 for (i = 0; i < num_configs; i++) {
3768 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003769 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003770 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3771 "selected\n", models[i]);
3772 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
3774 }
3775 }
3776
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003777 if (!codec->bus->pci || !tbl)
3778 return -1;
3779
3780 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3781 if (!tbl)
3782 return -1;
3783 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003784#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003785 char tmp[10];
3786 const char *model = NULL;
3787 if (models)
3788 model = models[tbl->value];
3789 if (!model) {
3790 sprintf(tmp, "#%d", tbl->value);
3791 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003793 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3794 "for config %x:%x (%s)\n",
3795 model, tbl->subvendor, tbl->subdevice,
3796 (tbl->name ? tbl->name : "Unknown device"));
3797#endif
3798 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 }
3800 return -1;
3801}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003802EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
3804/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003805 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003806 subsystem ID with the
3807 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003808
3809 This is important for Gateway notebooks with SB450 HDA Audio
3810 where the vendor ID of the PCI device is:
3811 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3812 and the vendor/subvendor are found only at the codec.
3813
3814 * @codec: the HDA codec
3815 * @num_configs: number of config enums
3816 * @models: array of model name strings
3817 * @tbl: configuration table, terminated by null entries
3818 *
3819 * Compares the modelname or PCI subsystem id of the current codec with the
3820 * given configuration table. If a matching entry is found, returns its
3821 * config value (supposed to be 0 or positive).
3822 *
3823 * If no entries are matching, the function returns a negative value.
3824 */
3825int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003826 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003827 const struct snd_pci_quirk *tbl)
3828{
3829 const struct snd_pci_quirk *q;
3830
3831 /* Search for codec ID */
3832 for (q = tbl; q->subvendor; q++) {
3833 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3834
3835 if (vendorid == codec->subsystem_id)
3836 break;
3837 }
3838
3839 if (!q->subvendor)
3840 return -1;
3841
3842 tbl = q;
3843
3844 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003845#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003846 char tmp[10];
3847 const char *model = NULL;
3848 if (models)
3849 model = models[tbl->value];
3850 if (!model) {
3851 sprintf(tmp, "#%d", tbl->value);
3852 model = tmp;
3853 }
3854 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3855 "for config %x:%x (%s)\n",
3856 model, tbl->subvendor, tbl->subdevice,
3857 (tbl->name ? tbl->name : "Unknown device"));
3858#endif
3859 return tbl->value;
3860 }
3861 return -1;
3862}
3863EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3864
3865/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 * snd_hda_add_new_ctls - create controls from the array
3867 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003868 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 *
3870 * This helper function creates and add new controls in the given array.
3871 * The array must be terminated with an empty entry as terminator.
3872 *
3873 * Returns 0 if successful, or a negative error code.
3874 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003875int snd_hda_add_new_ctls(struct hda_codec *codec,
3876 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003878 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
3880 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003881 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003882 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003883 if (knew->iface == -1) /* skip this codec private value */
3884 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003885 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003886 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003887 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003888 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003889 if (addr > 0)
3890 kctl->id.device = addr;
3891 if (idx > 0)
3892 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003893 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003894 if (!err)
3895 break;
3896 /* try first with another device index corresponding to
3897 * the codec addr; if it still fails (or it's the
3898 * primary codec), then try another control index
3899 */
3900 if (!addr && codec->addr)
3901 addr = codec->addr;
3902 else if (!idx && !knew->index) {
3903 idx = find_empty_mixer_ctl_idx(codec,
3904 knew->name);
3905 if (idx <= 0)
3906 return err;
3907 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003908 return err;
3909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911 return 0;
3912}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003913EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Takashi Iwaicb53c622007-08-10 17:21:45 +02003915#ifdef CONFIG_SND_HDA_POWER_SAVE
3916static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3917 unsigned int power_state);
3918
3919static void hda_power_work(struct work_struct *work)
3920{
3921 struct hda_codec *codec =
3922 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003923 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003924
Maxim Levitsky2e492462007-09-03 15:26:57 +02003925 if (!codec->power_on || codec->power_count) {
3926 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003927 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003928 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003929
3930 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003931 if (bus->ops.pm_notify)
3932 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003933}
3934
3935static void hda_keep_power_on(struct hda_codec *codec)
3936{
3937 codec->power_count++;
3938 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003939 codec->power_jiffies = jiffies;
3940}
3941
Takashi Iwaid5191e52009-11-16 14:58:17 +01003942/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003943void snd_hda_update_power_acct(struct hda_codec *codec)
3944{
3945 unsigned long delta = jiffies - codec->power_jiffies;
3946 if (codec->power_on)
3947 codec->power_on_acct += delta;
3948 else
3949 codec->power_off_acct += delta;
3950 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003951}
3952
Takashi Iwaid5191e52009-11-16 14:58:17 +01003953/**
3954 * snd_hda_power_up - Power-up the codec
3955 * @codec: HD-audio codec
3956 *
3957 * Increment the power-up counter and power up the hardware really when
3958 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003959 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003960void snd_hda_power_up(struct hda_codec *codec)
3961{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003962 struct hda_bus *bus = codec->bus;
3963
Takashi Iwaicb53c622007-08-10 17:21:45 +02003964 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003965 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003966 return;
3967
Takashi Iwaia2f63092009-11-11 09:34:25 +01003968 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003969 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003970 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003971 if (bus->ops.pm_notify)
3972 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003973 hda_call_codec_resume(codec);
3974 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003975 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003976}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003977EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003978
3979#define power_save(codec) \
3980 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003981
Takashi Iwaid5191e52009-11-16 14:58:17 +01003982/**
3983 * snd_hda_power_down - Power-down the codec
3984 * @codec: HD-audio codec
3985 *
3986 * Decrement the power-up counter and schedules the power-off work if
3987 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003988 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003989void snd_hda_power_down(struct hda_codec *codec)
3990{
3991 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003992 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003993 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003994 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003995 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003996 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003997 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003998 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004000EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004001
Takashi Iwaid5191e52009-11-16 14:58:17 +01004002/**
4003 * snd_hda_check_amp_list_power - Check the amp list and update the power
4004 * @codec: HD-audio codec
4005 * @check: the object containing an AMP list and the status
4006 * @nid: NID to check / update
4007 *
4008 * Check whether the given NID is in the amp list. If it's in the list,
4009 * check the current AMP status, and update the the power-status according
4010 * to the mute status.
4011 *
4012 * This function is supposed to be set or called from the check_power_status
4013 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004014 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004015int snd_hda_check_amp_list_power(struct hda_codec *codec,
4016 struct hda_loopback_check *check,
4017 hda_nid_t nid)
4018{
Takashi Iwai031024e2011-05-02 11:29:30 +02004019 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004020 int ch, v;
4021
4022 if (!check->amplist)
4023 return 0;
4024 for (p = check->amplist; p->nid; p++) {
4025 if (p->nid == nid)
4026 break;
4027 }
4028 if (!p->nid)
4029 return 0; /* nothing changed */
4030
4031 for (p = check->amplist; p->nid; p++) {
4032 for (ch = 0; ch < 2; ch++) {
4033 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4034 p->idx);
4035 if (!(v & HDA_AMP_MUTE) && v > 0) {
4036 if (!check->power_on) {
4037 check->power_on = 1;
4038 snd_hda_power_up(codec);
4039 }
4040 return 1;
4041 }
4042 }
4043 }
4044 if (check->power_on) {
4045 check->power_on = 0;
4046 snd_hda_power_down(codec);
4047 }
4048 return 0;
4049}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004050EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004051#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004053/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004054 * Channel mode helper
4055 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004056
4057/**
4058 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4059 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004060int snd_hda_ch_mode_info(struct hda_codec *codec,
4061 struct snd_ctl_elem_info *uinfo,
4062 const struct hda_channel_mode *chmode,
4063 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004064{
4065 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4066 uinfo->count = 1;
4067 uinfo->value.enumerated.items = num_chmodes;
4068 if (uinfo->value.enumerated.item >= num_chmodes)
4069 uinfo->value.enumerated.item = num_chmodes - 1;
4070 sprintf(uinfo->value.enumerated.name, "%dch",
4071 chmode[uinfo->value.enumerated.item].channels);
4072 return 0;
4073}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004074EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004075
Takashi Iwaid5191e52009-11-16 14:58:17 +01004076/**
4077 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4078 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004079int snd_hda_ch_mode_get(struct hda_codec *codec,
4080 struct snd_ctl_elem_value *ucontrol,
4081 const struct hda_channel_mode *chmode,
4082 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004083 int max_channels)
4084{
4085 int i;
4086
4087 for (i = 0; i < num_chmodes; i++) {
4088 if (max_channels == chmode[i].channels) {
4089 ucontrol->value.enumerated.item[0] = i;
4090 break;
4091 }
4092 }
4093 return 0;
4094}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004095EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004096
Takashi Iwaid5191e52009-11-16 14:58:17 +01004097/**
4098 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4099 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004100int snd_hda_ch_mode_put(struct hda_codec *codec,
4101 struct snd_ctl_elem_value *ucontrol,
4102 const struct hda_channel_mode *chmode,
4103 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004104 int *max_channelsp)
4105{
4106 unsigned int mode;
4107
4108 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004109 if (mode >= num_chmodes)
4110 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004111 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004112 return 0;
4113 /* change the current channel setting */
4114 *max_channelsp = chmode[mode].channels;
4115 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004116 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004117 return 1;
4118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004119EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004120
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121/*
4122 * input MUX helper
4123 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004124
4125/**
4126 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4127 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004128int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4129 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
4131 unsigned int index;
4132
4133 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4134 uinfo->count = 1;
4135 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004136 if (!imux->num_items)
4137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 index = uinfo->value.enumerated.item;
4139 if (index >= imux->num_items)
4140 index = imux->num_items - 1;
4141 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4142 return 0;
4143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004144EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Takashi Iwaid5191e52009-11-16 14:58:17 +01004146/**
4147 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4148 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004149int snd_hda_input_mux_put(struct hda_codec *codec,
4150 const struct hda_input_mux *imux,
4151 struct snd_ctl_elem_value *ucontrol,
4152 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 unsigned int *cur_val)
4154{
4155 unsigned int idx;
4156
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004157 if (!imux->num_items)
4158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 idx = ucontrol->value.enumerated.item[0];
4160 if (idx >= imux->num_items)
4161 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004162 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004164 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4165 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 *cur_val = idx;
4167 return 1;
4168}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004169EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
4171
4172/*
4173 * Multi-channel / digital-out PCM helper functions
4174 */
4175
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004176/* setup SPDIF output stream */
4177static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4178 unsigned int stream_tag, unsigned int format)
4179{
4180 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02004181 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004182 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02004183 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4184 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004185 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004186 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004187 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004188 for (d = codec->slave_dig_outs; *d; d++)
4189 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4190 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004191 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004192 /* turn on again (if needed) */
4193 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4194 set_dig_out_convert(codec, nid,
4195 codec->spdif_ctls & 0xff, -1);
4196}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004197
Takashi Iwai2f728532008-09-25 16:32:41 +02004198static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4199{
4200 snd_hda_codec_cleanup_stream(codec, nid);
4201 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004202 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004203 for (d = codec->slave_dig_outs; *d; d++)
4204 snd_hda_codec_cleanup_stream(codec, *d);
4205 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004206}
4207
Takashi Iwaid5191e52009-11-16 14:58:17 +01004208/**
4209 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4210 * @bus: HD-audio bus
4211 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004212void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4213{
4214 struct hda_codec *codec;
4215
4216 if (!bus)
4217 return;
4218 list_for_each_entry(codec, &bus->codec_list, list) {
4219#ifdef CONFIG_SND_HDA_POWER_SAVE
4220 if (!codec->power_on)
4221 continue;
4222#endif
4223 if (codec->patch_ops.reboot_notify)
4224 codec->patch_ops.reboot_notify(codec);
4225 }
4226}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004227EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004228
Takashi Iwaid5191e52009-11-16 14:58:17 +01004229/**
4230 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004232int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4233 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234{
Ingo Molnar62932df2006-01-16 16:34:20 +01004235 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004236 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4237 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004238 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004240 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return 0;
4242}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004243EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Takashi Iwaid5191e52009-11-16 14:58:17 +01004245/**
4246 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4247 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004248int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4249 struct hda_multi_out *mout,
4250 unsigned int stream_tag,
4251 unsigned int format,
4252 struct snd_pcm_substream *substream)
4253{
4254 mutex_lock(&codec->spdif_mutex);
4255 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4256 mutex_unlock(&codec->spdif_mutex);
4257 return 0;
4258}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004259EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004260
Takashi Iwaid5191e52009-11-16 14:58:17 +01004261/**
4262 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4263 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004264int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4265 struct hda_multi_out *mout)
4266{
4267 mutex_lock(&codec->spdif_mutex);
4268 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4269 mutex_unlock(&codec->spdif_mutex);
4270 return 0;
4271}
4272EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4273
Takashi Iwaid5191e52009-11-16 14:58:17 +01004274/**
4275 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004277int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4278 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
Ingo Molnar62932df2006-01-16 16:34:20 +01004280 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004282 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return 0;
4284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004285EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Takashi Iwaid5191e52009-11-16 14:58:17 +01004287/**
4288 * snd_hda_multi_out_analog_open - open analog outputs
4289 *
4290 * Open analog outputs and set up the hw-constraints.
4291 * If the digital outputs can be opened as slave, open the digital
4292 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004294int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4295 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004296 struct snd_pcm_substream *substream,
4297 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298{
Takashi Iwai9a081602008-02-12 18:37:26 +01004299 struct snd_pcm_runtime *runtime = substream->runtime;
4300 runtime->hw.channels_max = mout->max_channels;
4301 if (mout->dig_out_nid) {
4302 if (!mout->analog_rates) {
4303 mout->analog_rates = hinfo->rates;
4304 mout->analog_formats = hinfo->formats;
4305 mout->analog_maxbps = hinfo->maxbps;
4306 } else {
4307 runtime->hw.rates = mout->analog_rates;
4308 runtime->hw.formats = mout->analog_formats;
4309 hinfo->maxbps = mout->analog_maxbps;
4310 }
4311 if (!mout->spdif_rates) {
4312 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4313 &mout->spdif_rates,
4314 &mout->spdif_formats,
4315 &mout->spdif_maxbps);
4316 }
4317 mutex_lock(&codec->spdif_mutex);
4318 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004319 if ((runtime->hw.rates & mout->spdif_rates) &&
4320 (runtime->hw.formats & mout->spdif_formats)) {
4321 runtime->hw.rates &= mout->spdif_rates;
4322 runtime->hw.formats &= mout->spdif_formats;
4323 if (mout->spdif_maxbps < hinfo->maxbps)
4324 hinfo->maxbps = mout->spdif_maxbps;
4325 } else {
4326 mout->share_spdif = 0;
4327 /* FIXME: need notify? */
4328 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004329 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004330 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4333 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004335EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Takashi Iwaid5191e52009-11-16 14:58:17 +01004337/**
4338 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4339 *
4340 * Set up the i/o for analog out.
4341 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004343int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4344 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 unsigned int stream_tag,
4346 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004347 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
Takashi Iwaidda14412011-05-02 11:29:30 +02004349 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 int chs = substream->runtime->channels;
4351 int i;
4352
Ingo Molnar62932df2006-01-16 16:34:20 +01004353 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004354 if (mout->dig_out_nid && mout->share_spdif &&
4355 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004357 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4358 format) &&
4359 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004361 setup_dig_out_stream(codec, mout->dig_out_nid,
4362 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 } else {
4364 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004365 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 }
4367 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004368 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
4370 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004371 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4372 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004373 if (!mout->no_share_stream &&
4374 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004376 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4377 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004378 /* extra outputs copied from front */
4379 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004380 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004381 snd_hda_codec_setup_stream(codec,
4382 mout->extra_out_nid[i],
4383 stream_tag, 0, format);
4384
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 /* surrounds */
4386 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004387 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004388 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4389 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004390 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004391 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4392 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 }
4394 return 0;
4395}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004396EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Takashi Iwaid5191e52009-11-16 14:58:17 +01004398/**
4399 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004401int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4402 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Takashi Iwaidda14412011-05-02 11:29:30 +02004404 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 int i;
4406
4407 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004408 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004410 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004411 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4412 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004413 snd_hda_codec_cleanup_stream(codec,
4414 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004415 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004417 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 mout->dig_out_used = 0;
4419 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004420 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 return 0;
4422}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004423EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004425/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004426 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004427 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004428
Takashi Iwaidda14412011-05-02 11:29:30 +02004429static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004430{
4431 for (; *list; list++)
4432 if (*list == nid)
4433 return 1;
4434 return 0;
4435}
4436
Steve Longerbeam81937d32007-05-08 15:33:03 +02004437
4438/*
4439 * Sort an associated group of pins according to their sequence numbers.
4440 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004441static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004442 int num_pins)
4443{
4444 int i, j;
4445 short seq;
4446 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004447
Steve Longerbeam81937d32007-05-08 15:33:03 +02004448 for (i = 0; i < num_pins; i++) {
4449 for (j = i + 1; j < num_pins; j++) {
4450 if (sequences[i] > sequences[j]) {
4451 seq = sequences[i];
4452 sequences[i] = sequences[j];
4453 sequences[j] = seq;
4454 nid = pins[i];
4455 pins[i] = pins[j];
4456 pins[j] = nid;
4457 }
4458 }
4459 }
4460}
4461
4462
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004463/* add the found input-pin to the cfg->inputs[] table */
4464static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4465 int type)
4466{
4467 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4468 cfg->inputs[cfg->num_inputs].pin = nid;
4469 cfg->inputs[cfg->num_inputs].type = type;
4470 cfg->num_inputs++;
4471 }
4472}
4473
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004474/* sort inputs in the order of AUTO_PIN_* type */
4475static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4476{
4477 int i, j;
4478
4479 for (i = 0; i < cfg->num_inputs; i++) {
4480 for (j = i + 1; j < cfg->num_inputs; j++) {
4481 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4482 struct auto_pin_cfg_item tmp;
4483 tmp = cfg->inputs[i];
4484 cfg->inputs[i] = cfg->inputs[j];
4485 cfg->inputs[j] = tmp;
4486 }
4487 }
4488 }
4489}
4490
Takashi Iwai82bc9552006-03-21 11:24:42 +01004491/*
4492 * Parse all pin widgets and store the useful pin nids to cfg
4493 *
4494 * The number of line-outs or any primary output is stored in line_outs,
4495 * and the corresponding output pins are assigned to line_out_pins[],
4496 * in the order of front, rear, CLFE, side, ...
4497 *
4498 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004499 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004500 * is detected, one of speaker of HP pins is assigned as the primary
4501 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4502 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004503 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004504 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004505 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4506 * respectively.
4507 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004508int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4509 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004510 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004511{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004512 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004513 short seq, assoc_line_out, assoc_speaker;
4514 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4515 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004516 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004517 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004518
4519 memset(cfg, 0, sizeof(*cfg));
4520
Steve Longerbeam81937d32007-05-08 15:33:03 +02004521 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4522 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004523 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004524 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004525
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004526 end_nid = codec->start_nid + codec->num_nodes;
4527 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004528 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004529 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004530 unsigned int def_conf;
4531 short assoc, loc;
4532
4533 /* read all default configuration for pin complex */
4534 if (wid_type != AC_WID_PIN)
4535 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004536 /* ignore the given nids (e.g. pc-beep returns error) */
4537 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4538 continue;
4539
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004540 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004541 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4542 continue;
4543 loc = get_defcfg_location(def_conf);
4544 switch (get_defcfg_device(def_conf)) {
4545 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004546 seq = get_defcfg_sequence(def_conf);
4547 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004548
4549 if (!(wid_caps & AC_WCAP_STEREO))
4550 if (!cfg->mono_out_pin)
4551 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004552 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004553 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004554 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004555 assoc_line_out = assoc;
4556 else if (assoc_line_out != assoc)
4557 continue;
4558 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4559 continue;
4560 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004561 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004562 cfg->line_outs++;
4563 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004564 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004565 seq = get_defcfg_sequence(def_conf);
4566 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004567 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004568 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004569 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004570 assoc_speaker = assoc;
4571 else if (assoc_speaker != assoc)
4572 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004573 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4574 continue;
4575 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004576 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004577 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004578 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004579 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004580 seq = get_defcfg_sequence(def_conf);
4581 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004582 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4583 continue;
4584 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004585 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004586 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004587 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004588 case AC_JACK_MIC_IN:
4589 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004590 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004591 case AC_JACK_LINE_IN:
4592 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004593 break;
4594 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004595 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004596 break;
4597 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004598 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004599 break;
4600 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004601 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004602 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4603 continue;
4604 cfg->dig_out_pins[cfg->dig_outs] = nid;
4605 cfg->dig_out_type[cfg->dig_outs] =
4606 (loc == AC_JACK_LOC_HDMI) ?
4607 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4608 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004609 break;
4610 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004611 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004612 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004613 if (loc == AC_JACK_LOC_HDMI)
4614 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4615 else
4616 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004617 break;
4618 }
4619 }
4620
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004621 /* FIX-UP:
4622 * If no line-out is defined but multiple HPs are found,
4623 * some of them might be the real line-outs.
4624 */
4625 if (!cfg->line_outs && cfg->hp_outs > 1) {
4626 int i = 0;
4627 while (i < cfg->hp_outs) {
4628 /* The real HPs should have the sequence 0x0f */
4629 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4630 i++;
4631 continue;
4632 }
4633 /* Move it to the line-out table */
4634 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4635 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4636 cfg->line_outs++;
4637 cfg->hp_outs--;
4638 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4639 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004640 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004641 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4642 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004643 memset(cfg->hp_pins + cfg->hp_outs, 0,
4644 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004645 if (!cfg->hp_outs)
4646 cfg->line_out_type = AUTO_PIN_HP_OUT;
4647
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004648 }
4649
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004650 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004651 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4652 cfg->line_outs);
4653 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4654 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004655 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4656 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004657
Steve Longerbeam81937d32007-05-08 15:33:03 +02004658 /*
4659 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4660 * as a primary output
4661 */
4662 if (!cfg->line_outs) {
4663 if (cfg->speaker_outs) {
4664 cfg->line_outs = cfg->speaker_outs;
4665 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4666 sizeof(cfg->speaker_pins));
4667 cfg->speaker_outs = 0;
4668 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4669 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4670 } else if (cfg->hp_outs) {
4671 cfg->line_outs = cfg->hp_outs;
4672 memcpy(cfg->line_out_pins, cfg->hp_pins,
4673 sizeof(cfg->hp_pins));
4674 cfg->hp_outs = 0;
4675 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4676 cfg->line_out_type = AUTO_PIN_HP_OUT;
4677 }
4678 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004679
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004680 /* Reorder the surround channels
4681 * ALSA sequence is front/surr/clfe/side
4682 * HDA sequence is:
4683 * 4-ch: front/surr => OK as it is
4684 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004685 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004686 */
4687 switch (cfg->line_outs) {
4688 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004689 case 4:
4690 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004691 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004692 cfg->line_out_pins[2] = nid;
4693 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004694 }
4695
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004696 sort_autocfg_input_pins(cfg);
4697
Takashi Iwai82bc9552006-03-21 11:24:42 +01004698 /*
4699 * debug prints of the parsed results
4700 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004701 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 +01004702 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4703 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004704 cfg->line_out_pins[4],
4705 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4706 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4707 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004708 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4709 cfg->speaker_outs, cfg->speaker_pins[0],
4710 cfg->speaker_pins[1], cfg->speaker_pins[2],
4711 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004712 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4713 cfg->hp_outs, cfg->hp_pins[0],
4714 cfg->hp_pins[1], cfg->hp_pins[2],
4715 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004716 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004717 if (cfg->dig_outs)
4718 snd_printd(" dig-out=0x%x/0x%x\n",
4719 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004720 snd_printd(" inputs:");
4721 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004722 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004723 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004724 cfg->inputs[i].pin);
4725 }
4726 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004727 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004728 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004729
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004730 return 0;
4731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004732EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004733
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004734int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004735{
4736 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004737 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004738 if (conn == AC_JACK_PORT_NONE)
4739 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004740 /* Windows may claim the internal mic to be BOTH, too */
4741 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004742 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004743 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004744 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004745 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004746 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004747 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004748 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004749 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004750 return INPUT_PIN_ATTR_FRONT;
4751 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004752}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004753EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004754
Takashi Iwai990061c2010-09-09 22:08:44 +02004755/**
4756 * hda_get_input_pin_label - Give a label for the given input pin
4757 *
4758 * When check_location is true, the function checks the pin location
4759 * for mic and line-in pins, and set an appropriate prefix like "Front",
4760 * "Rear", "Internal".
4761 */
4762
Takashi Iwai10a20af2010-09-09 16:28:02 +02004763const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4764 int check_location)
4765{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004766 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004767 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004768 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4769 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004770 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004771
Takashi Iwai10a20af2010-09-09 16:28:02 +02004772 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004773
4774 switch (get_defcfg_device(def_conf)) {
4775 case AC_JACK_MIC_IN:
4776 if (!check_location)
4777 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004778 attr = snd_hda_get_input_pin_attr(def_conf);
4779 if (!attr)
4780 return "None";
4781 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004782 case AC_JACK_LINE_IN:
4783 if (!check_location)
4784 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004785 attr = snd_hda_get_input_pin_attr(def_conf);
4786 if (!attr)
4787 return "None";
4788 if (attr == INPUT_PIN_ATTR_DOCK)
4789 return "Dock Line";
4790 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004791 case AC_JACK_AUX:
4792 return "Aux";
4793 case AC_JACK_CD:
4794 return "CD";
4795 case AC_JACK_SPDIF_IN:
4796 return "SPDIF In";
4797 case AC_JACK_DIG_OTHER_IN:
4798 return "Digital In";
4799 default:
4800 return "Misc";
4801 }
4802}
4803EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4804
Takashi Iwaia1c98512010-09-09 21:36:27 +02004805/* Check whether the location prefix needs to be added to the label.
4806 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4807 * have to put "Front" prefix to each label. In such a case, returns false.
4808 */
4809static int check_mic_location_need(struct hda_codec *codec,
4810 const struct auto_pin_cfg *cfg,
4811 int input)
4812{
4813 unsigned int defc;
4814 int i, attr, attr2;
4815
4816 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004817 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004818 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004819 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004820 return 1;
4821
4822 attr = 0;
4823 for (i = 0; i < cfg->num_inputs; i++) {
4824 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004825 attr2 = snd_hda_get_input_pin_attr(defc);
4826 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004827 if (attr && attr != attr2)
4828 return 1; /* different locations found */
4829 attr = attr2;
4830 }
4831 }
4832 return 0;
4833}
4834
Takashi Iwai990061c2010-09-09 22:08:44 +02004835/**
4836 * hda_get_autocfg_input_label - Get a label for the given input
4837 *
4838 * Get a label for the given input pin defined by the autocfg item.
4839 * Unlike hda_get_input_pin_label(), this function checks all inputs
4840 * defined in autocfg and avoids the redundant mic/line prefix as much as
4841 * possible.
4842 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004843const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4844 const struct auto_pin_cfg *cfg,
4845 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004846{
4847 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004848 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004849
Takashi Iwai10a20af2010-09-09 16:28:02 +02004850 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4851 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4852 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004853 if (has_multiple_pins && type == AUTO_PIN_MIC)
4854 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004855 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4856 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004857}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004858EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4859
Takashi Iwai990061c2010-09-09 22:08:44 +02004860/**
4861 * snd_hda_add_imux_item - Add an item to input_mux
4862 *
4863 * When the same label is used already in the existing items, the number
4864 * suffix is appended to the label. This label index number is stored
4865 * to type_idx when non-NULL pointer is given.
4866 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004867int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4868 int index, int *type_idx)
4869{
4870 int i, label_idx = 0;
4871 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4872 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4873 return -EINVAL;
4874 }
4875 for (i = 0; i < imux->num_items; i++) {
4876 if (!strncmp(label, imux->items[i].label, strlen(label)))
4877 label_idx++;
4878 }
4879 if (type_idx)
4880 *type_idx = label_idx;
4881 if (label_idx > 0)
4882 snprintf(imux->items[imux->num_items].label,
4883 sizeof(imux->items[imux->num_items].label),
4884 "%s %d", label, label_idx);
4885 else
4886 strlcpy(imux->items[imux->num_items].label, label,
4887 sizeof(imux->items[imux->num_items].label));
4888 imux->items[imux->num_items].index = index;
4889 imux->num_items++;
4890 return 0;
4891}
4892EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004893
Takashi Iwai4a471b72005-12-07 13:56:29 +01004894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895#ifdef CONFIG_PM
4896/*
4897 * power management
4898 */
4899
4900/**
4901 * snd_hda_suspend - suspend the codecs
4902 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 *
4904 * Returns 0 if successful.
4905 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004906int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004908 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Takashi Iwai0ba21762007-04-16 11:29:14 +02004910 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004911#ifdef CONFIG_SND_HDA_POWER_SAVE
4912 if (!codec->power_on)
4913 continue;
4914#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004915 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 }
4917 return 0;
4918}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004919EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
4921/**
4922 * snd_hda_resume - resume the codecs
4923 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 *
4925 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004926 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004927 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004928 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 */
4930int snd_hda_resume(struct hda_bus *bus)
4931{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004932 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Takashi Iwai0ba21762007-04-16 11:29:14 +02004934 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004935 if (snd_hda_codec_needs_resume(codec))
4936 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 return 0;
4939}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004940EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004941#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004942
4943/*
4944 * generic arrays
4945 */
4946
Takashi Iwaid5191e52009-11-16 14:58:17 +01004947/**
4948 * snd_array_new - get a new element from the given array
4949 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004950 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004951 * Get a new element from the given array. If it exceeds the
4952 * pre-allocated array size, re-allocate the array.
4953 *
4954 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004955 */
4956void *snd_array_new(struct snd_array *array)
4957{
4958 if (array->used >= array->alloced) {
4959 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004960 void *nlist;
4961 if (snd_BUG_ON(num >= 4096))
4962 return NULL;
4963 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004964 if (!nlist)
4965 return NULL;
4966 if (array->list) {
4967 memcpy(nlist, array->list,
4968 array->elem_size * array->alloced);
4969 kfree(array->list);
4970 }
4971 array->list = nlist;
4972 array->alloced = num;
4973 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004974 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004975}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004976EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004977
Takashi Iwaid5191e52009-11-16 14:58:17 +01004978/**
4979 * snd_array_free - free the given array elements
4980 * @array: the array object
4981 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004982void snd_array_free(struct snd_array *array)
4983{
4984 kfree(array->list);
4985 array->used = 0;
4986 array->alloced = 0;
4987 array->list = NULL;
4988}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004989EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004990
Takashi Iwaid5191e52009-11-16 14:58:17 +01004991/**
4992 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4993 * @pcm: PCM caps bits
4994 * @buf: the string buffer to write
4995 * @buflen: the max buffer length
4996 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004997 * used by hda_proc.c and hda_eld.c
4998 */
4999void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5000{
5001 static unsigned int rates[] = {
5002 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5003 96000, 176400, 192000, 384000
5004 };
5005 int i, j;
5006
5007 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5008 if (pcm & (1 << i))
5009 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5010
5011 buf[j] = '\0'; /* necessary when j == 0 */
5012}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005013EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005014
Takashi Iwaid5191e52009-11-16 14:58:17 +01005015/**
5016 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5017 * @pcm: PCM caps bits
5018 * @buf: the string buffer to write
5019 * @buflen: the max buffer length
5020 *
5021 * used by hda_proc.c and hda_eld.c
5022 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005023void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5024{
5025 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5026 int i, j;
5027
5028 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5029 if (pcm & (AC_SUPPCM_BITS_8 << i))
5030 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5031
5032 buf[j] = '\0'; /* necessary when j == 0 */
5033}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005034EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005035
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005036#ifdef CONFIG_SND_HDA_INPUT_JACK
5037/*
5038 * Input-jack notification support
5039 */
5040struct hda_jack_item {
5041 hda_nid_t nid;
5042 int type;
5043 struct snd_jack *jack;
5044};
5045
5046static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5047 int type)
5048{
5049 switch (type) {
5050 case SND_JACK_HEADPHONE:
5051 return "Headphone";
5052 case SND_JACK_MICROPHONE:
5053 return "Mic";
5054 case SND_JACK_LINEOUT:
5055 return "Line-out";
5056 case SND_JACK_HEADSET:
5057 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005058 case SND_JACK_VIDEOOUT:
5059 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005060 default:
5061 return "Misc";
5062 }
5063}
5064
5065static void hda_free_jack_priv(struct snd_jack *jack)
5066{
5067 struct hda_jack_item *jacks = jack->private_data;
5068 jacks->nid = 0;
5069 jacks->jack = NULL;
5070}
5071
5072int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5073 const char *name)
5074{
5075 struct hda_jack_item *jack;
5076 int err;
5077
5078 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5079 jack = snd_array_new(&codec->jacks);
5080 if (!jack)
5081 return -ENOMEM;
5082
5083 jack->nid = nid;
5084 jack->type = type;
5085 if (!name)
5086 name = get_jack_default_name(codec, nid, type);
5087 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5088 if (err < 0) {
5089 jack->nid = 0;
5090 return err;
5091 }
5092 jack->jack->private_data = jack;
5093 jack->jack->private_free = hda_free_jack_priv;
5094 return 0;
5095}
5096EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5097
5098void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5099{
5100 struct hda_jack_item *jacks = codec->jacks.list;
5101 int i;
5102
5103 if (!jacks)
5104 return;
5105
5106 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5107 unsigned int pin_ctl;
5108 unsigned int present;
5109 int type;
5110
5111 if (jacks->nid != nid)
5112 continue;
5113 present = snd_hda_jack_detect(codec, nid);
5114 type = jacks->type;
5115 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5116 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5117 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5118 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5119 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5120 }
5121 snd_jack_report(jacks->jack, present ? type : 0);
5122 }
5123}
5124EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5125
5126/* free jack instances manually when clearing/reconfiguring */
5127void snd_hda_input_jack_free(struct hda_codec *codec)
5128{
5129 if (!codec->bus->shutdown && codec->jacks.list) {
5130 struct hda_jack_item *jacks = codec->jacks.list;
5131 int i;
5132 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5133 if (jacks->jack)
5134 snd_device_free(codec->bus->card, jacks->jack);
5135 }
5136 }
5137 snd_array_free(&codec->jacks);
5138}
5139EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5140#endif /* CONFIG_SND_HDA_INPUT_JACK */
5141
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005142MODULE_DESCRIPTION("HDA codec core");
5143MODULE_LICENSE("GPL");