blob: 11ead159abd9d2561f899a4715740fea1350fec9 [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;
332 int i, j, len, old_used;
333 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
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001003/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1004static void restore_shutup_pins(struct hda_codec *codec)
1005{
1006 int i;
1007 if (!codec->pins_shutup)
1008 return;
1009 if (codec->bus->shutdown)
1010 return;
1011 for (i = 0; i < codec->init_pins.used; i++) {
1012 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1013 snd_hda_codec_write(codec, pin->nid, 0,
1014 AC_VERB_SET_PIN_WIDGET_CONTROL,
1015 pin->ctrl);
1016 }
1017 codec->pins_shutup = 0;
1018}
1019
Takashi Iwai01751f52007-08-10 16:59:39 +02001020static void init_hda_cache(struct hda_cache_rec *cache,
1021 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001022static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001023
Takashi Iwai3be14142009-02-20 14:11:16 +01001024/* restore the initial pin cfgs and release all pincfg lists */
1025static void restore_init_pincfgs(struct hda_codec *codec)
1026{
Takashi Iwai346ff702009-02-23 09:42:57 +01001027 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001028 * so that only the values in init_pins are restored
1029 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001030 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001031#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001032 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001033#endif
1034 restore_pincfgs(codec);
1035 snd_array_free(&codec->init_pins);
1036}
1037
Takashi Iwai54d17402005-11-21 16:33:22 +01001038/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001039 * audio-converter setup caches
1040 */
1041struct hda_cvt_setup {
1042 hda_nid_t nid;
1043 u8 stream_tag;
1044 u8 channel_id;
1045 u16 format_id;
1046 unsigned char active; /* cvt is currently used */
1047 unsigned char dirty; /* setups should be cleared */
1048};
1049
1050/* get or create a cache entry for the given audio converter NID */
1051static struct hda_cvt_setup *
1052get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1053{
1054 struct hda_cvt_setup *p;
1055 int i;
1056
1057 for (i = 0; i < codec->cvt_setups.used; i++) {
1058 p = snd_array_elem(&codec->cvt_setups, i);
1059 if (p->nid == nid)
1060 return p;
1061 }
1062 p = snd_array_new(&codec->cvt_setups);
1063 if (p)
1064 p->nid = nid;
1065 return p;
1066}
1067
1068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * codec destructor
1070 */
1071static void snd_hda_codec_free(struct hda_codec *codec)
1072{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001073 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001075 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001076#ifdef CONFIG_SND_HDA_POWER_SAVE
1077 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001078 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001079#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001081 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001082 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001083 snd_array_free(&codec->conn_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 codec->bus->caddr_tbl[codec->addr] = NULL;
1085 if (codec->patch_ops.free)
1086 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001087 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001088 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001089 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001090 kfree(codec->vendor_name);
1091 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001092 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001093 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 kfree(codec);
1095}
1096
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001097static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1098 unsigned int power_state);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100/**
1101 * snd_hda_codec_new - create a HDA codec
1102 * @bus: the bus to assign
1103 * @codec_addr: the codec address
1104 * @codecp: the pointer to store the generated codec
1105 *
1106 * Returns 0 if successful, or a negative error code.
1107 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001108int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1109 unsigned int codec_addr,
1110 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001113 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int err;
1115
Takashi Iwaida3cec32008-08-08 17:12:14 +02001116 if (snd_BUG_ON(!bus))
1117 return -EINVAL;
1118 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001122 snd_printk(KERN_ERR "hda_codec: "
1123 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return -EBUSY;
1125 }
1126
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001127 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (codec == NULL) {
1129 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1130 return -ENOMEM;
1131 }
1132
1133 codec->bus = bus;
1134 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001135 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001136 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001137 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001138 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001139 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1140 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001141 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001142 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001143 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001144 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001145 if (codec->bus->modelname) {
1146 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1147 if (!codec->modelname) {
1148 snd_hda_codec_free(codec);
1149 return -ENODEV;
1150 }
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Takashi Iwaicb53c622007-08-10 17:21:45 +02001153#ifdef CONFIG_SND_HDA_POWER_SAVE
1154 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1155 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1156 * the caller has to power down appropriatley after initialization
1157 * phase.
1158 */
1159 hda_keep_power_on(codec);
1160#endif
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 list_add_tail(&codec->list, &bus->codec_list);
1163 bus->caddr_tbl[codec_addr] = codec;
1164
Takashi Iwai0ba21762007-04-16 11:29:14 +02001165 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1166 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001167 if (codec->vendor_id == -1)
1168 /* read again, hopefully the access method was corrected
1169 * in the last read...
1170 */
1171 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1172 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001173 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1174 AC_PAR_SUBSYSTEM_ID);
1175 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1176 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001178 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001179 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001180 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001181 err = -ENODEV;
1182 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184
Takashi Iwai3be14142009-02-20 14:11:16 +01001185 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1186 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001187 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001188 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001189 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001190 err = read_pin_defaults(codec);
1191 if (err < 0)
1192 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001193
Takashi Iwai0ba21762007-04-16 11:29:14 +02001194 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001195 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001196 codec->subsystem_id =
1197 snd_hda_codec_read(codec, nid, 0,
1198 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001199 }
1200
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001201 /* power-up all before initialization */
1202 hda_set_power_state(codec,
1203 codec->afg ? codec->afg : codec->mfg,
1204 AC_PWRST_D0);
1205
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001206 snd_hda_codec_proc_new(codec);
1207
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001208 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001209
1210 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1211 codec->subsystem_id, codec->revision_id);
1212 snd_component_add(codec->bus->card, component);
1213
1214 if (codecp)
1215 *codecp = codec;
1216 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001217
1218 error:
1219 snd_hda_codec_free(codec);
1220 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001221}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001222EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001223
Takashi Iwaid5191e52009-11-16 14:58:17 +01001224/**
1225 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1226 * @codec: the HDA codec
1227 *
1228 * Start parsing of the given codec tree and (re-)initialize the whole
1229 * patch instance.
1230 *
1231 * Returns 0 if successful or a negative error code.
1232 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001233int snd_hda_codec_configure(struct hda_codec *codec)
1234{
1235 int err;
1236
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001237 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001238 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001239 err = get_codec_name(codec);
1240 if (err < 0)
1241 return err;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Takashi Iwai82467612007-07-27 19:15:54 +02001244 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001246 goto patched;
1247 }
Takashi Iwai82467612007-07-27 19:15:54 +02001248 if (codec->preset && codec->preset->patch) {
1249 err = codec->preset->patch(codec);
1250 goto patched;
1251 }
1252
1253 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001254 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001255 if (err < 0)
1256 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001257
1258 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001259 if (!err && codec->patch_ops.unsol_event)
1260 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001261 /* audio codec should override the mixer name */
1262 if (!err && (codec->afg || !*codec->bus->card->mixername))
1263 snprintf(codec->bus->card->mixername,
1264 sizeof(codec->bus->card->mixername),
1265 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001268EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270/**
1271 * snd_hda_codec_setup_stream - set up the codec for streaming
1272 * @codec: the CODEC to set up
1273 * @nid: the NID to set up
1274 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1275 * @channel_id: channel id to pass, zero based.
1276 * @format: stream format.
1277 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001278void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1279 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int channel_id, int format)
1281{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001282 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001283 struct hda_cvt_setup *p;
1284 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001285 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001286 int i;
1287
Takashi Iwai0ba21762007-04-16 11:29:14 +02001288 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001289 return;
1290
Takashi Iwai0ba21762007-04-16 11:29:14 +02001291 snd_printdd("hda_codec_setup_stream: "
1292 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001294 p = get_hda_cvt_setup(codec, nid);
1295 if (!p)
1296 return;
1297 /* update the stream-id if changed */
1298 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1299 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1300 newval = (stream_tag << 4) | channel_id;
1301 if (oldval != newval)
1302 snd_hda_codec_write(codec, nid, 0,
1303 AC_VERB_SET_CHANNEL_STREAMID,
1304 newval);
1305 p->stream_tag = stream_tag;
1306 p->channel_id = channel_id;
1307 }
1308 /* update the format-id if changed */
1309 if (p->format_id != format) {
1310 oldval = snd_hda_codec_read(codec, nid, 0,
1311 AC_VERB_GET_STREAM_FORMAT, 0);
1312 if (oldval != format) {
1313 msleep(1);
1314 snd_hda_codec_write(codec, nid, 0,
1315 AC_VERB_SET_STREAM_FORMAT,
1316 format);
1317 }
1318 p->format_id = format;
1319 }
1320 p->active = 1;
1321 p->dirty = 0;
1322
1323 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001324 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001325 list_for_each_entry(c, &codec->bus->codec_list, list) {
1326 for (i = 0; i < c->cvt_setups.used; i++) {
1327 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001328 if (!p->active && p->stream_tag == stream_tag &&
1329 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001330 p->dirty = 1;
1331 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001334EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Takashi Iwaif0cea792010-08-13 11:56:53 +02001336static void really_cleanup_stream(struct hda_codec *codec,
1337 struct hda_cvt_setup *q);
1338
Takashi Iwaid5191e52009-11-16 14:58:17 +01001339/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001340 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001341 * @codec: the CODEC to clean up
1342 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001343 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001344 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001345void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1346 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001347{
Takashi Iwaieb541332010-08-06 13:48:11 +02001348 struct hda_cvt_setup *p;
1349
Takashi Iwai888afa12008-03-18 09:57:50 +01001350 if (!nid)
1351 return;
1352
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001353 if (codec->no_sticky_stream)
1354 do_now = 1;
1355
Takashi Iwai888afa12008-03-18 09:57:50 +01001356 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001357 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001358 if (p) {
1359 /* here we just clear the active flag when do_now isn't set;
1360 * actual clean-ups will be done later in
1361 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1362 */
1363 if (do_now)
1364 really_cleanup_stream(codec, p);
1365 else
1366 p->active = 0;
1367 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001368}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001369EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001370
Takashi Iwaieb541332010-08-06 13:48:11 +02001371static void really_cleanup_stream(struct hda_codec *codec,
1372 struct hda_cvt_setup *q)
1373{
1374 hda_nid_t nid = q->nid;
1375 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1376 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1377 memset(q, 0, sizeof(*q));
1378 q->nid = nid;
1379}
1380
1381/* clean up the all conflicting obsolete streams */
1382static void purify_inactive_streams(struct hda_codec *codec)
1383{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001384 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001385 int i;
1386
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001387 list_for_each_entry(c, &codec->bus->codec_list, list) {
1388 for (i = 0; i < c->cvt_setups.used; i++) {
1389 struct hda_cvt_setup *p;
1390 p = snd_array_elem(&c->cvt_setups, i);
1391 if (p->dirty)
1392 really_cleanup_stream(c, p);
1393 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001394 }
1395}
1396
1397/* clean up all streams; called from suspend */
1398static void hda_cleanup_all_streams(struct hda_codec *codec)
1399{
1400 int i;
1401
1402 for (i = 0; i < codec->cvt_setups.used; i++) {
1403 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1404 if (p->stream_tag)
1405 really_cleanup_stream(codec, p);
1406 }
1407}
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/*
1410 * amp access functions
1411 */
1412
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001413/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001414#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001415#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001416#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1417#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001419#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001422static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001423 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Takashi Iwai01751f52007-08-10 16:59:39 +02001425 memset(cache, 0, sizeof(*cache));
1426 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001427 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001428}
1429
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001430static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001431{
Takashi Iwai603c4012008-07-30 15:01:44 +02001432 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001436static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Takashi Iwai01751f52007-08-10 16:59:39 +02001438 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1439 u16 cur = cache->hash[idx];
1440 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001443 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (info->key == key)
1445 return info;
1446 cur = info->next;
1447 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001448 return NULL;
1449}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001451/* query the hash. allocate an entry if not found. */
1452static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1453 u32 key)
1454{
1455 struct hda_cache_head *info = get_hash(cache, key);
1456 if (!info) {
1457 u16 idx, cur;
1458 /* add a new hash entry */
1459 info = snd_array_new(&cache->buf);
1460 if (!info)
1461 return NULL;
1462 cur = snd_array_index(&cache->buf, info);
1463 info->key = key;
1464 info->val = 0;
1465 idx = key % (u16)ARRAY_SIZE(cache->hash);
1466 info->next = cache->hash[idx];
1467 cache->hash[idx] = cur;
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return info;
1470}
1471
Takashi Iwai01751f52007-08-10 16:59:39 +02001472/* query and allocate an amp hash entry */
1473static inline struct hda_amp_info *
1474get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1475{
1476 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1477}
1478
Takashi Iwaid5191e52009-11-16 14:58:17 +01001479/**
1480 * query_amp_caps - query AMP capabilities
1481 * @codec: the HD-auio codec
1482 * @nid: the NID to query
1483 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1484 *
1485 * Query AMP capabilities for the given widget and direction.
1486 * Returns the obtained capability bits.
1487 *
1488 * When cap bits have been already read, this doesn't read again but
1489 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001491u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001493 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Takashi Iwai0ba21762007-04-16 11:29:14 +02001495 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1496 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001498 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001499 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001501 info->amp_caps = snd_hda_param_read(codec, nid,
1502 direction == HDA_OUTPUT ?
1503 AC_PAR_AMP_OUT_CAP :
1504 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001505 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001506 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508 return info->amp_caps;
1509}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001510EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Takashi Iwaid5191e52009-11-16 14:58:17 +01001512/**
1513 * snd_hda_override_amp_caps - Override the AMP capabilities
1514 * @codec: the CODEC to clean up
1515 * @nid: the NID to clean up
1516 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1517 * @caps: the capability bits to set
1518 *
1519 * Override the cached AMP caps bits value by the given one.
1520 * This function is useful if the driver needs to adjust the AMP ranges,
1521 * e.g. limit to 0dB, etc.
1522 *
1523 * Returns zero if successful or a negative error code.
1524 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001525int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1526 unsigned int caps)
1527{
1528 struct hda_amp_info *info;
1529
1530 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1531 if (!info)
1532 return -EINVAL;
1533 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001534 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001535 return 0;
1536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001537EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001538
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001539static unsigned int
1540query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1541 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001542{
1543 struct hda_amp_info *info;
1544
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001545 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001546 if (!info)
1547 return 0;
1548 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001549 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001550 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001551 }
1552 return info->amp_caps;
1553}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001554
1555static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1556{
1557 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1558}
1559
Takashi Iwaid5191e52009-11-16 14:58:17 +01001560/**
1561 * snd_hda_query_pin_caps - Query PIN capabilities
1562 * @codec: the HD-auio codec
1563 * @nid: the NID to query
1564 *
1565 * Query PIN capabilities for the given widget.
1566 * Returns the obtained capability bits.
1567 *
1568 * When cap bits have been already read, this doesn't read again but
1569 * returns the cached value.
1570 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001571u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1572{
1573 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1574 read_pin_cap);
1575}
Takashi Iwai1327a322009-03-23 13:07:47 +01001576EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1577
Wu Fengguang864f92b2009-11-18 12:38:02 +08001578/**
1579 * snd_hda_pin_sense - execute pin sense measurement
1580 * @codec: the CODEC to sense
1581 * @nid: the pin NID to sense
1582 *
1583 * Execute necessary pin sense measurement and return its Presence Detect,
1584 * Impedance, ELD Valid etc. status bits.
1585 */
1586u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1587{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001588 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001589
Takashi Iwai729d55b2009-12-25 22:49:01 +01001590 if (!codec->no_trigger_sense) {
1591 pincap = snd_hda_query_pin_caps(codec, nid);
1592 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001593 snd_hda_codec_read(codec, nid, 0,
1594 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001595 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001596 return snd_hda_codec_read(codec, nid, 0,
1597 AC_VERB_GET_PIN_SENSE, 0);
1598}
1599EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1600
1601/**
1602 * snd_hda_jack_detect - query pin Presence Detect status
1603 * @codec: the CODEC to sense
1604 * @nid: the pin NID to sense
1605 *
1606 * Query and return the pin's Presence Detect status.
1607 */
1608int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1609{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001610 u32 sense = snd_hda_pin_sense(codec, nid);
1611 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001612}
1613EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615/*
1616 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001617 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001619static unsigned int get_vol_mute(struct hda_codec *codec,
1620 struct hda_amp_info *info, hda_nid_t nid,
1621 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 u32 val, parm;
1624
Takashi Iwai01751f52007-08-10 16:59:39 +02001625 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001626 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1629 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1630 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001631 val = snd_hda_codec_read(codec, nid, 0,
1632 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001634 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001635 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001639 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001641static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001642 hda_nid_t nid, int ch, int direction, int index,
1643 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 u32 parm;
1646
1647 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1648 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1649 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1650 parm |= val;
1651 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001652 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
Takashi Iwaid5191e52009-11-16 14:58:17 +01001655/**
1656 * snd_hda_codec_amp_read - Read AMP value
1657 * @codec: HD-audio codec
1658 * @nid: NID to read the AMP value
1659 * @ch: channel (left=0 or right=1)
1660 * @direction: #HDA_INPUT or #HDA_OUTPUT
1661 * @index: the index value (only for input direction)
1662 *
1663 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 */
Takashi Iwai834be882006-03-01 14:16:17 +01001665int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1666 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001668 struct hda_amp_info *info;
1669 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1670 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001672 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001674EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Takashi Iwaid5191e52009-11-16 14:58:17 +01001676/**
1677 * snd_hda_codec_amp_update - update the AMP value
1678 * @codec: HD-audio codec
1679 * @nid: NID to read the AMP value
1680 * @ch: channel (left=0 or right=1)
1681 * @direction: #HDA_INPUT or #HDA_OUTPUT
1682 * @idx: the index value (only for input direction)
1683 * @mask: bit mask to set
1684 * @val: the bits value to set
1685 *
1686 * Update the AMP value with a bit mask.
1687 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001688 */
Takashi Iwai834be882006-03-01 14:16:17 +01001689int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1690 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001692 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001693
Takashi Iwai0ba21762007-04-16 11:29:14 +02001694 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1695 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001697 if (snd_BUG_ON(mask & ~0xff))
1698 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001699 val &= mask;
1700 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001701 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001703 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 1;
1705}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001706EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Takashi Iwaid5191e52009-11-16 14:58:17 +01001708/**
1709 * snd_hda_codec_amp_stereo - update the AMP stereo values
1710 * @codec: HD-audio codec
1711 * @nid: NID to read the AMP value
1712 * @direction: #HDA_INPUT or #HDA_OUTPUT
1713 * @idx: the index value (only for input direction)
1714 * @mask: bit mask to set
1715 * @val: the bits value to set
1716 *
1717 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1718 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001719 */
1720int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1721 int direction, int idx, int mask, int val)
1722{
1723 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001724
1725 if (snd_BUG_ON(mask & ~0xff))
1726 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001727 for (ch = 0; ch < 2; ch++)
1728 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1729 idx, mask, val);
1730 return ret;
1731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001732EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001733
Takashi Iwaicb53c622007-08-10 17:21:45 +02001734#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001735/**
1736 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1737 * @codec: HD-audio codec
1738 *
1739 * Resume the all amp commands from the cache.
1740 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001741void snd_hda_codec_resume_amp(struct hda_codec *codec)
1742{
Takashi Iwai603c4012008-07-30 15:01:44 +02001743 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001744 int i;
1745
Takashi Iwai603c4012008-07-30 15:01:44 +02001746 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001747 u32 key = buffer->head.key;
1748 hda_nid_t nid;
1749 unsigned int idx, dir, ch;
1750 if (!key)
1751 continue;
1752 nid = key & 0xff;
1753 idx = (key >> 16) & 0xff;
1754 dir = (key >> 24) & 0xff;
1755 for (ch = 0; ch < 2; ch++) {
1756 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1757 continue;
1758 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1759 buffer->vol[ch]);
1760 }
1761 }
1762}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001763EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001764#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001766static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1767 unsigned int ofs)
1768{
1769 u32 caps = query_amp_caps(codec, nid, dir);
1770 /* get num steps */
1771 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1772 if (ofs < caps)
1773 caps -= ofs;
1774 return caps;
1775}
1776
Takashi Iwaid5191e52009-11-16 14:58:17 +01001777/**
1778 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1779 *
1780 * The control element is supposed to have the private_value field
1781 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1782 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001783int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1784 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1787 u16 nid = get_amp_nid(kcontrol);
1788 u8 chs = get_amp_channels(kcontrol);
1789 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001790 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001792 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1793 uinfo->count = chs == 3 ? 2 : 1;
1794 uinfo->value.integer.min = 0;
1795 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1796 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001797 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001798 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1799 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return -EINVAL;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return 0;
1803}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001804EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001806
1807static inline unsigned int
1808read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1809 int ch, int dir, int idx, unsigned int ofs)
1810{
1811 unsigned int val;
1812 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1813 val &= HDA_AMP_VOLMASK;
1814 if (val >= ofs)
1815 val -= ofs;
1816 else
1817 val = 0;
1818 return val;
1819}
1820
1821static inline int
1822update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1823 int ch, int dir, int idx, unsigned int ofs,
1824 unsigned int val)
1825{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001826 unsigned int maxval;
1827
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001828 if (val > 0)
1829 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001830 /* ofs = 0: raw max value */
1831 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001832 if (val > maxval)
1833 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001834 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1835 HDA_AMP_VOLMASK, val);
1836}
1837
Takashi Iwaid5191e52009-11-16 14:58:17 +01001838/**
1839 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1840 *
1841 * The control element is supposed to have the private_value field
1842 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1843 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001844int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1845 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1848 hda_nid_t nid = get_amp_nid(kcontrol);
1849 int chs = get_amp_channels(kcontrol);
1850 int dir = get_amp_direction(kcontrol);
1851 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001852 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 long *valp = ucontrol->value.integer.value;
1854
1855 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001856 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001858 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return 0;
1860}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001861EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Takashi Iwaid5191e52009-11-16 14:58:17 +01001863/**
1864 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1865 *
1866 * The control element is supposed to have the private_value field
1867 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1868 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001869int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1870 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1873 hda_nid_t nid = get_amp_nid(kcontrol);
1874 int chs = get_amp_channels(kcontrol);
1875 int dir = get_amp_direction(kcontrol);
1876 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001877 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 long *valp = ucontrol->value.integer.value;
1879 int change = 0;
1880
Takashi Iwaicb53c622007-08-10 17:21:45 +02001881 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001882 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001883 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001884 valp++;
1885 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001886 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001887 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001888 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return change;
1890}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001891EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Takashi Iwaid5191e52009-11-16 14:58:17 +01001893/**
1894 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1895 *
1896 * The control element is supposed to have the private_value field
1897 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1898 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001899int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1900 unsigned int size, unsigned int __user *_tlv)
1901{
1902 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1903 hda_nid_t nid = get_amp_nid(kcontrol);
1904 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001905 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001906 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001907 u32 caps, val1, val2;
1908
1909 if (size < 4 * sizeof(unsigned int))
1910 return -ENOMEM;
1911 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001912 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1913 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001914 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001915 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001916 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001917 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02001918 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001919 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1920 return -EFAULT;
1921 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1922 return -EFAULT;
1923 if (put_user(val1, _tlv + 2))
1924 return -EFAULT;
1925 if (put_user(val2, _tlv + 3))
1926 return -EFAULT;
1927 return 0;
1928}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001929EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001930
Takashi Iwaid5191e52009-11-16 14:58:17 +01001931/**
1932 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1933 * @codec: HD-audio codec
1934 * @nid: NID of a reference widget
1935 * @dir: #HDA_INPUT or #HDA_OUTPUT
1936 * @tlv: TLV data to be stored, at least 4 elements
1937 *
1938 * Set (static) TLV data for a virtual master volume using the AMP caps
1939 * obtained from the reference NID.
1940 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001941 */
1942void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1943 unsigned int *tlv)
1944{
1945 u32 caps;
1946 int nums, step;
1947
1948 caps = query_amp_caps(codec, nid, dir);
1949 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1950 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1951 step = (step + 1) * 25;
1952 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1953 tlv[1] = 2 * sizeof(unsigned int);
1954 tlv[2] = -nums * step;
1955 tlv[3] = step;
1956}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001957EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001958
1959/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001960static struct snd_kcontrol *
1961_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1962 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001963{
1964 struct snd_ctl_elem_id id;
1965 memset(&id, 0, sizeof(id));
1966 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001967 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001968 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1969 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001970 strcpy(id.name, name);
1971 return snd_ctl_find_id(codec->bus->card, &id);
1972}
1973
Takashi Iwaid5191e52009-11-16 14:58:17 +01001974/**
1975 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1976 * @codec: HD-audio codec
1977 * @name: ctl id name string
1978 *
1979 * Get the control element with the given id string and IFACE_MIXER.
1980 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001981struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1982 const char *name)
1983{
1984 return _snd_hda_find_mixer_ctl(codec, name, 0);
1985}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001986EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001987
Takashi Iwai1afe2062010-12-23 10:17:52 +01001988static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
1989{
1990 int idx;
1991 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
1992 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
1993 return idx;
1994 }
1995 return -EBUSY;
1996}
1997
Takashi Iwaid5191e52009-11-16 14:58:17 +01001998/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001999 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002000 * @codec: HD-audio codec
2001 * @nid: corresponding NID (optional)
2002 * @kctl: the control element to assign
2003 *
2004 * Add the given control element to an array inside the codec instance.
2005 * All control elements belonging to a codec are supposed to be added
2006 * by this function so that a proper clean-up works at the free or
2007 * reconfiguration time.
2008 *
2009 * If non-zero @nid is passed, the NID is assigned to the control element.
2010 * The assignment is shown in the codec proc file.
2011 *
2012 * snd_hda_ctl_add() checks the control subdev id field whether
2013 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002014 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2015 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002016 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002017int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2018 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002019{
2020 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002021 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002022 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002023
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002024 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002025 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002026 if (nid == 0)
2027 nid = get_amp_nid_(kctl->private_value);
2028 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002029 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2030 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002031 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002032 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002033 err = snd_ctl_add(codec->bus->card, kctl);
2034 if (err < 0)
2035 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002036 item = snd_array_new(&codec->mixers);
2037 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002038 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002039 item->kctl = kctl;
2040 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002041 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002042 return 0;
2043}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002044EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002045
Takashi Iwaid5191e52009-11-16 14:58:17 +01002046/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002047 * snd_hda_add_nid - Assign a NID to a control element
2048 * @codec: HD-audio codec
2049 * @nid: corresponding NID (optional)
2050 * @kctl: the control element to assign
2051 * @index: index to kctl
2052 *
2053 * Add the given control element to an array inside the codec instance.
2054 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2055 * NID:KCTL mapping - for example "Capture Source" selector.
2056 */
2057int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2058 unsigned int index, hda_nid_t nid)
2059{
2060 struct hda_nid_item *item;
2061
2062 if (nid > 0) {
2063 item = snd_array_new(&codec->nids);
2064 if (!item)
2065 return -ENOMEM;
2066 item->kctl = kctl;
2067 item->index = index;
2068 item->nid = nid;
2069 return 0;
2070 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002071 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2072 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002073 return -EINVAL;
2074}
2075EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2076
2077/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002078 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2079 * @codec: HD-audio codec
2080 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002081void snd_hda_ctls_clear(struct hda_codec *codec)
2082{
2083 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002084 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002085 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002086 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002087 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002088 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002089}
2090
Takashi Iwaia65d6292009-02-23 16:57:04 +01002091/* pseudo device locking
2092 * toggle card->shutdown to allow/disallow the device access (as a hack)
2093 */
2094static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002095{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002096 spin_lock(&card->files_lock);
2097 if (card->shutdown) {
2098 spin_unlock(&card->files_lock);
2099 return -EINVAL;
2100 }
2101 card->shutdown = 1;
2102 spin_unlock(&card->files_lock);
2103 return 0;
2104}
2105
2106static void hda_unlock_devices(struct snd_card *card)
2107{
2108 spin_lock(&card->files_lock);
2109 card->shutdown = 0;
2110 spin_unlock(&card->files_lock);
2111}
2112
Takashi Iwaid5191e52009-11-16 14:58:17 +01002113/**
2114 * snd_hda_codec_reset - Clear all objects assigned to the codec
2115 * @codec: HD-audio codec
2116 *
2117 * This frees the all PCM and control elements assigned to the codec, and
2118 * clears the caches and restores the pin default configurations.
2119 *
2120 * When a device is being used, it returns -EBSY. If successfully freed,
2121 * returns zero.
2122 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002123int snd_hda_codec_reset(struct hda_codec *codec)
2124{
2125 struct snd_card *card = codec->bus->card;
2126 int i, pcm;
2127
2128 if (hda_lock_devices(card) < 0)
2129 return -EBUSY;
2130 /* check whether the codec isn't used by any mixer or PCM streams */
2131 if (!list_empty(&card->ctl_files)) {
2132 hda_unlock_devices(card);
2133 return -EBUSY;
2134 }
2135 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2136 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2137 if (!cpcm->pcm)
2138 continue;
2139 if (cpcm->pcm->streams[0].substream_opened ||
2140 cpcm->pcm->streams[1].substream_opened) {
2141 hda_unlock_devices(card);
2142 return -EBUSY;
2143 }
2144 }
2145
2146 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002147
2148#ifdef CONFIG_SND_HDA_POWER_SAVE
2149 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002150 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002151#endif
2152 snd_hda_ctls_clear(codec);
2153 /* relase PCMs */
2154 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002155 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002156 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002157 clear_bit(codec->pcm_info[i].device,
2158 codec->bus->pcm_dev_bits);
2159 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002160 }
2161 if (codec->patch_ops.free)
2162 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002163 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002164 codec->spec = NULL;
2165 free_hda_cache(&codec->amp_cache);
2166 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002167 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2168 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002169 /* free only driver_pins so that init_pins + user_pins are restored */
2170 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002171 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002172 codec->num_pcms = 0;
2173 codec->pcm_info = NULL;
2174 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002175 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2176 codec->slave_dig_outs = NULL;
2177 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002178 module_put(codec->owner);
2179 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002180
2181 /* allow device access again */
2182 hda_unlock_devices(card);
2183 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002184}
2185
Takashi Iwaid5191e52009-11-16 14:58:17 +01002186/**
2187 * snd_hda_add_vmaster - create a virtual master control and add slaves
2188 * @codec: HD-audio codec
2189 * @name: vmaster control name
2190 * @tlv: TLV data (optional)
2191 * @slaves: slave control names (optional)
2192 *
2193 * Create a virtual master control with the given name. The TLV data
2194 * must be either NULL or a valid data.
2195 *
2196 * @slaves is a NULL-terminated array of strings, each of which is a
2197 * slave control name. All controls with these names are assigned to
2198 * the new virtual master control.
2199 *
2200 * This function returns zero if successful or a negative error code.
2201 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002202int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002203 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002204{
2205 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002206 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002207 int err;
2208
Takashi Iwai2f085542008-02-22 18:43:50 +01002209 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2210 ;
2211 if (!*s) {
2212 snd_printdd("No slave found for %s\n", name);
2213 return 0;
2214 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002215 kctl = snd_ctl_make_virtual_master(name, tlv);
2216 if (!kctl)
2217 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002218 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002219 if (err < 0)
2220 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002221
Takashi Iwai2134ea42008-01-10 16:53:55 +01002222 for (s = slaves; *s; s++) {
2223 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002224 int i = 0;
2225 for (;;) {
2226 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2227 if (!sctl) {
2228 if (!i)
2229 snd_printdd("Cannot find slave %s, "
2230 "skipped\n", *s);
2231 break;
2232 }
2233 err = snd_ctl_add_slave(kctl, sctl);
2234 if (err < 0)
2235 return err;
2236 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002237 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002238 }
2239 return 0;
2240}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002241EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002242
Takashi Iwaid5191e52009-11-16 14:58:17 +01002243/**
2244 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2245 *
2246 * The control element is supposed to have the private_value field
2247 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2248 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002249int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2250 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 int chs = get_amp_channels(kcontrol);
2253
2254 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2255 uinfo->count = chs == 3 ? 2 : 1;
2256 uinfo->value.integer.min = 0;
2257 uinfo->value.integer.max = 1;
2258 return 0;
2259}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002260EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Takashi Iwaid5191e52009-11-16 14:58:17 +01002262/**
2263 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2264 *
2265 * The control element is supposed to have the private_value field
2266 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2267 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002268int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2269 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2272 hda_nid_t nid = get_amp_nid(kcontrol);
2273 int chs = get_amp_channels(kcontrol);
2274 int dir = get_amp_direction(kcontrol);
2275 int idx = get_amp_index(kcontrol);
2276 long *valp = ucontrol->value.integer.value;
2277
2278 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002279 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002280 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002282 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002283 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return 0;
2285}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002286EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Takashi Iwaid5191e52009-11-16 14:58:17 +01002288/**
2289 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2290 *
2291 * The control element is supposed to have the private_value field
2292 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2293 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002294int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2295 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2298 hda_nid_t nid = get_amp_nid(kcontrol);
2299 int chs = get_amp_channels(kcontrol);
2300 int dir = get_amp_direction(kcontrol);
2301 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 long *valp = ucontrol->value.integer.value;
2303 int change = 0;
2304
Takashi Iwaicb53c622007-08-10 17:21:45 +02002305 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002306 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002307 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002308 HDA_AMP_MUTE,
2309 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002310 valp++;
2311 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002312 if (chs & 2)
2313 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002314 HDA_AMP_MUTE,
2315 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002316 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002317 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return change;
2319}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002320EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Takashi Iwai67d634c2009-11-16 15:35:59 +01002322#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002323/**
2324 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2325 *
2326 * This function calls snd_hda_enable_beep_device(), which behaves differently
2327 * depending on beep_mode option.
2328 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002329int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_value *ucontrol)
2331{
2332 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2333 long *valp = ucontrol->value.integer.value;
2334
2335 snd_hda_enable_beep_device(codec, *valp);
2336 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2337}
2338EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002339#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341/*
Takashi Iwai985be542005-11-02 18:26:49 +01002342 * bound volume controls
2343 *
2344 * bind multiple volumes (# indices, from 0)
2345 */
2346
2347#define AMP_VAL_IDX_SHIFT 19
2348#define AMP_VAL_IDX_MASK (0x0f<<19)
2349
Takashi Iwaid5191e52009-11-16 14:58:17 +01002350/**
2351 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2352 *
2353 * The control element is supposed to have the private_value field
2354 * set up via HDA_BIND_MUTE*() macros.
2355 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002356int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002358{
2359 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2360 unsigned long pval;
2361 int err;
2362
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002363 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002364 pval = kcontrol->private_value;
2365 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2366 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2367 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002368 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002369 return err;
2370}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002371EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002372
Takashi Iwaid5191e52009-11-16 14:58:17 +01002373/**
2374 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2375 *
2376 * The control element is supposed to have the private_value field
2377 * set up via HDA_BIND_MUTE*() macros.
2378 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002379int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2380 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002381{
2382 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2383 unsigned long pval;
2384 int i, indices, err = 0, change = 0;
2385
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002386 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002387 pval = kcontrol->private_value;
2388 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2389 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002390 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2391 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002392 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2393 if (err < 0)
2394 break;
2395 change |= err;
2396 }
2397 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002398 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002399 return err < 0 ? err : change;
2400}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002401EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002402
Takashi Iwaid5191e52009-11-16 14:58:17 +01002403/**
2404 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2405 *
2406 * The control element is supposed to have the private_value field
2407 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002408 */
2409int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2410 struct snd_ctl_elem_info *uinfo)
2411{
2412 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2413 struct hda_bind_ctls *c;
2414 int err;
2415
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002416 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002417 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002418 kcontrol->private_value = *c->values;
2419 err = c->ops->info(kcontrol, uinfo);
2420 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002421 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002422 return err;
2423}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002424EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002425
Takashi Iwaid5191e52009-11-16 14:58:17 +01002426/**
2427 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2428 *
2429 * The control element is supposed to have the private_value field
2430 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2431 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002432int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
2434{
2435 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2436 struct hda_bind_ctls *c;
2437 int err;
2438
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002439 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002440 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002441 kcontrol->private_value = *c->values;
2442 err = c->ops->get(kcontrol, ucontrol);
2443 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002444 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002445 return err;
2446}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002447EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002448
Takashi Iwaid5191e52009-11-16 14:58:17 +01002449/**
2450 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2451 *
2452 * The control element is supposed to have the private_value field
2453 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2454 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002455int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
2457{
2458 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2459 struct hda_bind_ctls *c;
2460 unsigned long *vals;
2461 int err = 0, change = 0;
2462
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002463 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002464 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002465 for (vals = c->values; *vals; vals++) {
2466 kcontrol->private_value = *vals;
2467 err = c->ops->put(kcontrol, ucontrol);
2468 if (err < 0)
2469 break;
2470 change |= err;
2471 }
2472 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002473 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002474 return err < 0 ? err : change;
2475}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002476EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002477
Takashi Iwaid5191e52009-11-16 14:58:17 +01002478/**
2479 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2480 *
2481 * The control element is supposed to have the private_value field
2482 * set up via HDA_BIND_VOL() macro.
2483 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002484int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2485 unsigned int size, unsigned int __user *tlv)
2486{
2487 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2488 struct hda_bind_ctls *c;
2489 int err;
2490
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002491 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002492 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002493 kcontrol->private_value = *c->values;
2494 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2495 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002496 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002497 return err;
2498}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002499EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002500
2501struct hda_ctl_ops snd_hda_bind_vol = {
2502 .info = snd_hda_mixer_amp_volume_info,
2503 .get = snd_hda_mixer_amp_volume_get,
2504 .put = snd_hda_mixer_amp_volume_put,
2505 .tlv = snd_hda_mixer_amp_tlv
2506};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002507EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002508
2509struct hda_ctl_ops snd_hda_bind_sw = {
2510 .info = snd_hda_mixer_amp_switch_info,
2511 .get = snd_hda_mixer_amp_switch_get,
2512 .put = snd_hda_mixer_amp_switch_put,
2513 .tlv = snd_hda_mixer_amp_tlv
2514};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002515EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002516
2517/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 * SPDIF out controls
2519 */
2520
Takashi Iwai0ba21762007-04-16 11:29:14 +02002521static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2522 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2525 uinfo->count = 1;
2526 return 0;
2527}
2528
Takashi Iwai0ba21762007-04-16 11:29:14 +02002529static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2530 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2533 IEC958_AES0_NONAUDIO |
2534 IEC958_AES0_CON_EMPHASIS_5015 |
2535 IEC958_AES0_CON_NOT_COPYRIGHT;
2536 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2537 IEC958_AES1_CON_ORIGINAL;
2538 return 0;
2539}
2540
Takashi Iwai0ba21762007-04-16 11:29:14 +02002541static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
2544 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2545 IEC958_AES0_NONAUDIO |
2546 IEC958_AES0_PRO_EMPHASIS_5015;
2547 return 0;
2548}
2549
Takashi Iwai0ba21762007-04-16 11:29:14 +02002550static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2551 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
2553 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2554
2555 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2556 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2557 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2558 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2559
2560 return 0;
2561}
2562
2563/* convert from SPDIF status bits to HDA SPDIF bits
2564 * bit 0 (DigEn) is always set zero (to be filled later)
2565 */
2566static unsigned short convert_from_spdif_status(unsigned int sbits)
2567{
2568 unsigned short val = 0;
2569
2570 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002571 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002573 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002575 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2576 IEC958_AES0_PRO_EMPHASIS_5015)
2577 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002579 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2580 IEC958_AES0_CON_EMPHASIS_5015)
2581 val |= AC_DIG1_EMPHASIS;
2582 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2583 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002585 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2587 }
2588 return val;
2589}
2590
2591/* convert to SPDIF status bits from HDA SPDIF bits
2592 */
2593static unsigned int convert_to_spdif_status(unsigned short val)
2594{
2595 unsigned int sbits = 0;
2596
Takashi Iwai0ba21762007-04-16 11:29:14 +02002597 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002599 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 sbits |= IEC958_AES0_PROFESSIONAL;
2601 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002602 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2604 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002605 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002607 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2611 sbits |= val & (0x7f << 8);
2612 }
2613 return sbits;
2614}
2615
Takashi Iwai2f728532008-09-25 16:32:41 +02002616/* set digital convert verbs both for the given NID and its slaves */
2617static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2618 int verb, int val)
2619{
2620 hda_nid_t *d;
2621
Takashi Iwai9e976972008-11-25 08:17:20 +01002622 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002623 d = codec->slave_dig_outs;
2624 if (!d)
2625 return;
2626 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002627 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002628}
2629
2630static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2631 int dig1, int dig2)
2632{
2633 if (dig1 != -1)
2634 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2635 if (dig2 != -1)
2636 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2637}
2638
Takashi Iwai0ba21762007-04-16 11:29:14 +02002639static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2640 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
2642 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2643 hda_nid_t nid = kcontrol->private_value;
2644 unsigned short val;
2645 int change;
2646
Ingo Molnar62932df2006-01-16 16:34:20 +01002647 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 codec->spdif_status = ucontrol->value.iec958.status[0] |
2649 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2650 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2651 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2652 val = convert_from_spdif_status(codec->spdif_status);
2653 val |= codec->spdif_ctls & 1;
2654 change = codec->spdif_ctls != val;
2655 codec->spdif_ctls = val;
2656
Takashi Iwai2f728532008-09-25 16:32:41 +02002657 if (change)
2658 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Ingo Molnar62932df2006-01-16 16:34:20 +01002660 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return change;
2662}
2663
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002664#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Takashi Iwai0ba21762007-04-16 11:29:14 +02002666static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2667 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2670
Takashi Iwai0ba21762007-04-16 11:29:14 +02002671 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return 0;
2673}
2674
Takashi Iwai0ba21762007-04-16 11:29:14 +02002675static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2676 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
2678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2679 hda_nid_t nid = kcontrol->private_value;
2680 unsigned short val;
2681 int change;
2682
Ingo Molnar62932df2006-01-16 16:34:20 +01002683 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002684 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002686 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002688 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02002690 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002691 /* unmute amp switch (if any) */
2692 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02002693 (val & AC_DIG1_ENABLE))
2694 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2695 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002697 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return change;
2699}
2700
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002701static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 {
2703 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2704 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002705 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 .info = snd_hda_spdif_mask_info,
2707 .get = snd_hda_spdif_cmask_get,
2708 },
2709 {
2710 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2711 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002712 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 .info = snd_hda_spdif_mask_info,
2714 .get = snd_hda_spdif_pmask_get,
2715 },
2716 {
2717 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002718 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 .info = snd_hda_spdif_mask_info,
2720 .get = snd_hda_spdif_default_get,
2721 .put = snd_hda_spdif_default_put,
2722 },
2723 {
2724 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002725 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 .info = snd_hda_spdif_out_switch_info,
2727 .get = snd_hda_spdif_out_switch_get,
2728 .put = snd_hda_spdif_out_switch_put,
2729 },
2730 { } /* end */
2731};
2732
2733/**
2734 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2735 * @codec: the HDA codec
2736 * @nid: audio out widget NID
2737 *
2738 * Creates controls related with the SPDIF output.
2739 * Called from each patch supporting the SPDIF out.
2740 *
2741 * Returns 0 if successful, or a negative error code.
2742 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002743int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002746 struct snd_kcontrol *kctl;
2747 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002748 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Takashi Iwai1afe2062010-12-23 10:17:52 +01002750 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2751 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002752 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2753 return -EBUSY;
2754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2756 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002757 if (!kctl)
2758 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002759 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002761 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002762 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return err;
2764 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002765 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002766 snd_hda_codec_read(codec, nid, 0,
2767 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2769 return 0;
2770}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002771EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002774 * SPDIF sharing with analog output
2775 */
2776static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_value *ucontrol)
2778{
2779 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2780 ucontrol->value.integer.value[0] = mout->share_spdif;
2781 return 0;
2782}
2783
2784static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2785 struct snd_ctl_elem_value *ucontrol)
2786{
2787 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2788 mout->share_spdif = !!ucontrol->value.integer.value[0];
2789 return 0;
2790}
2791
2792static struct snd_kcontrol_new spdif_share_sw = {
2793 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2794 .name = "IEC958 Default PCM Playback Switch",
2795 .info = snd_ctl_boolean_mono_info,
2796 .get = spdif_share_sw_get,
2797 .put = spdif_share_sw_put,
2798};
2799
Takashi Iwaid5191e52009-11-16 14:58:17 +01002800/**
2801 * snd_hda_create_spdif_share_sw - create Default PCM switch
2802 * @codec: the HDA codec
2803 * @mout: multi-out instance
2804 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002805int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2806 struct hda_multi_out *mout)
2807{
2808 if (!mout->dig_out_nid)
2809 return 0;
2810 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002811 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2812 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002813}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002814EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002815
2816/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 * SPDIF input
2818 */
2819
2820#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2821
Takashi Iwai0ba21762007-04-16 11:29:14 +02002822static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2823 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
2825 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2826
2827 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2828 return 0;
2829}
2830
Takashi Iwai0ba21762007-04-16 11:29:14 +02002831static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2832 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
2834 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2835 hda_nid_t nid = kcontrol->private_value;
2836 unsigned int val = !!ucontrol->value.integer.value[0];
2837 int change;
2838
Ingo Molnar62932df2006-01-16 16:34:20 +01002839 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002841 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002843 snd_hda_codec_write_cache(codec, nid, 0,
2844 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002846 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return change;
2848}
2849
Takashi Iwai0ba21762007-04-16 11:29:14 +02002850static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2851 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
2853 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2854 hda_nid_t nid = kcontrol->private_value;
2855 unsigned short val;
2856 unsigned int sbits;
2857
Andrew Paprocki3982d172007-12-19 12:13:44 +01002858 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 sbits = convert_to_spdif_status(val);
2860 ucontrol->value.iec958.status[0] = sbits;
2861 ucontrol->value.iec958.status[1] = sbits >> 8;
2862 ucontrol->value.iec958.status[2] = sbits >> 16;
2863 ucontrol->value.iec958.status[3] = sbits >> 24;
2864 return 0;
2865}
2866
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002867static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 {
2869 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002870 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 .info = snd_hda_spdif_in_switch_info,
2872 .get = snd_hda_spdif_in_switch_get,
2873 .put = snd_hda_spdif_in_switch_put,
2874 },
2875 {
2876 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2877 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002878 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 .info = snd_hda_spdif_mask_info,
2880 .get = snd_hda_spdif_in_status_get,
2881 },
2882 { } /* end */
2883};
2884
2885/**
2886 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2887 * @codec: the HDA codec
2888 * @nid: audio in widget NID
2889 *
2890 * Creates controls related with the SPDIF input.
2891 * Called from each patch supporting the SPDIF in.
2892 *
2893 * Returns 0 if successful, or a negative error code.
2894 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002895int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
2897 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002898 struct snd_kcontrol *kctl;
2899 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002900 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Takashi Iwai1afe2062010-12-23 10:17:52 +01002902 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
2903 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002904 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2905 return -EBUSY;
2906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2908 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002909 if (!kctl)
2910 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002912 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002913 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return err;
2915 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002916 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002917 snd_hda_codec_read(codec, nid, 0,
2918 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002919 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return 0;
2921}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002922EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Takashi Iwaicb53c622007-08-10 17:21:45 +02002924#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002925/*
2926 * command cache
2927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002929/* build a 32bit cache key with the widget id and the command parameter */
2930#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2931#define get_cmd_cache_nid(key) ((key) & 0xff)
2932#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2933
2934/**
2935 * snd_hda_codec_write_cache - send a single command with caching
2936 * @codec: the HDA codec
2937 * @nid: NID to send the command
2938 * @direct: direct flag
2939 * @verb: the verb to send
2940 * @parm: the parameter for the verb
2941 *
2942 * Send a single command without waiting for response.
2943 *
2944 * Returns 0 if successful, or a negative error code.
2945 */
2946int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2947 int direct, unsigned int verb, unsigned int parm)
2948{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002949 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2950 struct hda_cache_head *c;
2951 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002952
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02002953 if (err < 0)
2954 return err;
2955 /* parm may contain the verb stuff for get/set amp */
2956 verb = verb | (parm >> 8);
2957 parm &= 0xff;
2958 key = build_cmd_cache_key(nid, verb);
2959 mutex_lock(&codec->bus->cmd_mutex);
2960 c = get_alloc_hash(&codec->cmd_cache, key);
2961 if (c)
2962 c->val = parm;
2963 mutex_unlock(&codec->bus->cmd_mutex);
2964 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002965}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002966EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002967
Takashi Iwaid5191e52009-11-16 14:58:17 +01002968/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02002969 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2970 * @codec: the HDA codec
2971 * @nid: NID to send the command
2972 * @direct: direct flag
2973 * @verb: the verb to send
2974 * @parm: the parameter for the verb
2975 *
2976 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2977 * command if the parameter is already identical with the cached value.
2978 * If not, it sends the command and refreshes the cache.
2979 *
2980 * Returns 0 if successful, or a negative error code.
2981 */
2982int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2983 int direct, unsigned int verb, unsigned int parm)
2984{
2985 struct hda_cache_head *c;
2986 u32 key;
2987
2988 /* parm may contain the verb stuff for get/set amp */
2989 verb = verb | (parm >> 8);
2990 parm &= 0xff;
2991 key = build_cmd_cache_key(nid, verb);
2992 mutex_lock(&codec->bus->cmd_mutex);
2993 c = get_hash(&codec->cmd_cache, key);
2994 if (c && c->val == parm) {
2995 mutex_unlock(&codec->bus->cmd_mutex);
2996 return 0;
2997 }
2998 mutex_unlock(&codec->bus->cmd_mutex);
2999 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3000}
3001EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3002
3003/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003004 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3005 * @codec: HD-audio codec
3006 *
3007 * Execute all verbs recorded in the command caches to resume.
3008 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003009void snd_hda_codec_resume_cache(struct hda_codec *codec)
3010{
Takashi Iwai603c4012008-07-30 15:01:44 +02003011 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003012 int i;
3013
Takashi Iwai603c4012008-07-30 15:01:44 +02003014 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003015 u32 key = buffer->key;
3016 if (!key)
3017 continue;
3018 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3019 get_cmd_cache_cmd(key), buffer->val);
3020 }
3021}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003022EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003023
3024/**
3025 * snd_hda_sequence_write_cache - sequence writes with caching
3026 * @codec: the HDA codec
3027 * @seq: VERB array to send
3028 *
3029 * Send the commands sequentially from the given array.
3030 * Thte commands are recorded on cache for power-save and resume.
3031 * The array must be terminated with NID=0.
3032 */
3033void snd_hda_sequence_write_cache(struct hda_codec *codec,
3034 const struct hda_verb *seq)
3035{
3036 for (; seq->nid; seq++)
3037 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3038 seq->param);
3039}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003040EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003041#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003042
Takashi Iwai54d17402005-11-21 16:33:22 +01003043/*
3044 * set power state of the codec
3045 */
3046static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3047 unsigned int power_state)
3048{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003049 hda_nid_t nid;
3050 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003051
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003052 /* this delay seems necessary to avoid click noise at power-down */
3053 if (power_state == AC_PWRST_D3)
3054 msleep(100);
3055 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003056 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003057 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003058 if (power_state == AC_PWRST_D0 &&
3059 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003060 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003061
Takashi Iwaicb53c622007-08-10 17:21:45 +02003062 nid = codec->start_nid;
3063 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003064 unsigned int wcaps = get_wcaps(codec, nid);
3065 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003066 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003067 if (power_state == AC_PWRST_D3 &&
3068 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003069 unsigned int pincap;
3070 /*
3071 * don't power down the widget if it controls
3072 * eapd and EAPD_BTLENABLE is set.
3073 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003074 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003075 if (pincap & AC_PINCAP_EAPD) {
3076 int eapd = snd_hda_codec_read(codec,
3077 nid, 0,
3078 AC_VERB_GET_EAPD_BTLENABLE, 0);
3079 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003080 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003081 continue;
3082 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003083 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003084 snd_hda_codec_write(codec, nid, 0,
3085 AC_VERB_SET_POWER_STATE,
3086 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003087 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003088 }
3089
Takashi Iwaicb53c622007-08-10 17:21:45 +02003090 if (power_state == AC_PWRST_D0) {
3091 unsigned long end_time;
3092 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003093 /* wait until the codec reachs to D0 */
3094 end_time = jiffies + msecs_to_jiffies(500);
3095 do {
3096 state = snd_hda_codec_read(codec, fg, 0,
3097 AC_VERB_GET_POWER_STATE, 0);
3098 if (state == power_state)
3099 break;
3100 msleep(1);
3101 } while (time_after_eq(end_time, jiffies));
3102 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003103}
3104
Takashi Iwai11aeff02008-07-30 15:01:46 +02003105#ifdef CONFIG_SND_HDA_HWDEP
3106/* execute additional init verbs */
3107static void hda_exec_init_verbs(struct hda_codec *codec)
3108{
3109 if (codec->init_verbs.list)
3110 snd_hda_sequence_write(codec, codec->init_verbs.list);
3111}
3112#else
3113static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3114#endif
3115
Takashi Iwaicb53c622007-08-10 17:21:45 +02003116#ifdef SND_HDA_NEEDS_RESUME
3117/*
3118 * call suspend and power-down; used both from PM and power-save
3119 */
3120static void hda_call_codec_suspend(struct hda_codec *codec)
3121{
3122 if (codec->patch_ops.suspend)
3123 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003124 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003125 hda_set_power_state(codec,
3126 codec->afg ? codec->afg : codec->mfg,
3127 AC_PWRST_D3);
3128#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003129 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003130 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003131 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003132 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003133 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003134#endif
3135}
3136
3137/*
3138 * kick up codec; used both from PM and power-save
3139 */
3140static void hda_call_codec_resume(struct hda_codec *codec)
3141{
3142 hda_set_power_state(codec,
3143 codec->afg ? codec->afg : codec->mfg,
3144 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003145 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003146 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003147 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003148 if (codec->patch_ops.resume)
3149 codec->patch_ops.resume(codec);
3150 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003151 if (codec->patch_ops.init)
3152 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003153 snd_hda_codec_resume_amp(codec);
3154 snd_hda_codec_resume_cache(codec);
3155 }
3156}
3157#endif /* SND_HDA_NEEDS_RESUME */
3158
Takashi Iwai54d17402005-11-21 16:33:22 +01003159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160/**
3161 * snd_hda_build_controls - build mixer controls
3162 * @bus: the BUS
3163 *
3164 * Creates mixer controls for each codec included in the bus.
3165 *
3166 * Returns 0 if successful, otherwise a negative error code.
3167 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003168int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003170 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Takashi Iwai0ba21762007-04-16 11:29:14 +02003172 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003173 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003174 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003175 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003176 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003177 err = snd_hda_codec_reset(codec);
3178 if (err < 0) {
3179 printk(KERN_ERR
3180 "hda_codec: cannot revert codec\n");
3181 return err;
3182 }
3183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003185 return 0;
3186}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003187EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003189int snd_hda_codec_build_controls(struct hda_codec *codec)
3190{
3191 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003192 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003193 /* continue to initialize... */
3194 if (codec->patch_ops.init)
3195 err = codec->patch_ops.init(codec);
3196 if (!err && codec->patch_ops.build_controls)
3197 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003198 if (err < 0)
3199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 return 0;
3201}
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203/*
3204 * stream formats
3205 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003206struct hda_rate_tbl {
3207 unsigned int hz;
3208 unsigned int alsa_bits;
3209 unsigned int hda_fmt;
3210};
3211
Takashi Iwai92f10b32010-08-03 14:21:00 +02003212/* rate = base * mult / div */
3213#define HDA_RATE(base, mult, div) \
3214 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3215 (((div) - 1) << AC_FMT_DIV_SHIFT))
3216
Takashi Iwaibefdf312005-08-22 13:57:55 +02003217static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003219
3220 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003221 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3222 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3223 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3224 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3225 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3226 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3227 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3228 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3229 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3230 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3231 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003232#define AC_PAR_PCM_RATE_BITS 11
3233 /* up to bits 10, 384kHZ isn't supported properly */
3234
3235 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003236 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003237
Takashi Iwaibefdf312005-08-22 13:57:55 +02003238 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239};
3240
3241/**
3242 * snd_hda_calc_stream_format - calculate format bitset
3243 * @rate: the sample rate
3244 * @channels: the number of channels
3245 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3246 * @maxbps: the max. bps
3247 *
3248 * Calculate the format bitset from the given rate, channels and th PCM format.
3249 *
3250 * Return zero if invalid.
3251 */
3252unsigned int snd_hda_calc_stream_format(unsigned int rate,
3253 unsigned int channels,
3254 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003255 unsigned int maxbps,
3256 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
3258 int i;
3259 unsigned int val = 0;
3260
Takashi Iwaibefdf312005-08-22 13:57:55 +02003261 for (i = 0; rate_bits[i].hz; i++)
3262 if (rate_bits[i].hz == rate) {
3263 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 break;
3265 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003266 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 snd_printdd("invalid rate %d\n", rate);
3268 return 0;
3269 }
3270
3271 if (channels == 0 || channels > 8) {
3272 snd_printdd("invalid channels %d\n", channels);
3273 return 0;
3274 }
3275 val |= channels - 1;
3276
3277 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003278 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003279 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003280 break;
3281 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003282 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 case 20:
3285 case 24:
3286 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003287 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003288 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003290 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003292 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 break;
3294 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003295 snd_printdd("invalid format width %d\n",
3296 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return 0;
3298 }
3299
Anssi Hannula32c168c2010-08-03 13:28:57 +03003300 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003301 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return val;
3304}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003305EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003307static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3308{
3309 unsigned int val = 0;
3310 if (nid != codec->afg &&
3311 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3312 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3313 if (!val || val == -1)
3314 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3315 if (!val || val == -1)
3316 return 0;
3317 return val;
3318}
3319
3320static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3321{
3322 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3323 get_pcm_param);
3324}
3325
3326static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3327{
3328 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3329 if (!streams || streams == -1)
3330 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3331 if (!streams || streams == -1)
3332 return 0;
3333 return streams;
3334}
3335
3336static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3337{
3338 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3339 get_stream_param);
3340}
3341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342/**
3343 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3344 * @codec: the HDA codec
3345 * @nid: NID to query
3346 * @ratesp: the pointer to store the detected rate bitflags
3347 * @formatsp: the pointer to store the detected formats
3348 * @bpsp: the pointer to store the detected format widths
3349 *
3350 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3351 * or @bsps argument is ignored.
3352 *
3353 * Returns 0 if successful, otherwise a negative error code.
3354 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01003355static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3357{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003358 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003360 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003361 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 if (ratesp) {
3364 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003365 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003367 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003369 if (rates == 0) {
3370 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3371 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3372 nid, val,
3373 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3374 return -EIO;
3375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 *ratesp = rates;
3377 }
3378
3379 if (formatsp || bpsp) {
3380 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003381 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003383 streams = query_stream_param(codec, nid);
3384 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
3387 bps = 0;
3388 if (streams & AC_SUPFMT_PCM) {
3389 if (val & AC_SUPPCM_BITS_8) {
3390 formats |= SNDRV_PCM_FMTBIT_U8;
3391 bps = 8;
3392 }
3393 if (val & AC_SUPPCM_BITS_16) {
3394 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3395 bps = 16;
3396 }
3397 if (wcaps & AC_WCAP_DIGITAL) {
3398 if (val & AC_SUPPCM_BITS_32)
3399 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3400 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3401 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3402 if (val & AC_SUPPCM_BITS_24)
3403 bps = 24;
3404 else if (val & AC_SUPPCM_BITS_20)
3405 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003406 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3407 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3409 if (val & AC_SUPPCM_BITS_32)
3410 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 else if (val & AC_SUPPCM_BITS_24)
3412 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003413 else if (val & AC_SUPPCM_BITS_20)
3414 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
3416 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003417 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003419 if (!bps)
3420 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003421 }
3422 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003423 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 /* temporary hack: we have still no proper support
3425 * for the direct AC3 stream...
3426 */
3427 formats |= SNDRV_PCM_FMTBIT_U8;
3428 bps = 8;
3429 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003430 if (formats == 0) {
3431 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3432 "(nid=0x%x, val=0x%x, ovrd=%i, "
3433 "streams=0x%x)\n",
3434 nid, val,
3435 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3436 streams);
3437 return -EIO;
3438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 if (formatsp)
3440 *formatsp = formats;
3441 if (bpsp)
3442 *bpsp = bps;
3443 }
3444
3445 return 0;
3446}
3447
3448/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003449 * snd_hda_is_supported_format - Check the validity of the format
3450 * @codec: HD-audio codec
3451 * @nid: NID to check
3452 * @format: the HD-audio format value to check
3453 *
3454 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 *
3456 * Returns 1 if supported, 0 if not.
3457 */
3458int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3459 unsigned int format)
3460{
3461 int i;
3462 unsigned int val = 0, rate, stream;
3463
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003464 val = query_pcm_param(codec, nid);
3465 if (!val)
3466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
3468 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003469 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003470 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 if (val & (1 << i))
3472 break;
3473 return 0;
3474 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003475 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 return 0;
3477
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003478 stream = query_stream_param(codec, nid);
3479 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return 0;
3481
3482 if (stream & AC_SUPFMT_PCM) {
3483 switch (format & 0xf0) {
3484 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003485 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return 0;
3487 break;
3488 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003489 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 return 0;
3491 break;
3492 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003493 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return 0;
3495 break;
3496 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003497 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return 0;
3499 break;
3500 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003501 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return 0;
3503 break;
3504 default:
3505 return 0;
3506 }
3507 } else {
3508 /* FIXME: check for float32 and AC3? */
3509 }
3510
3511 return 1;
3512}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003513EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
3515/*
3516 * PCM stuff
3517 */
3518static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3519 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003520 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
3522 return 0;
3523}
3524
3525static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3526 struct hda_codec *codec,
3527 unsigned int stream_tag,
3528 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003529 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
3531 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3532 return 0;
3533}
3534
3535static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3536 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003537 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Takashi Iwai888afa12008-03-18 09:57:50 +01003539 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 return 0;
3541}
3542
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003543static int set_pcm_default_values(struct hda_codec *codec,
3544 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003546 int err;
3547
Takashi Iwai0ba21762007-04-16 11:29:14 +02003548 /* query support PCM information from the given NID */
3549 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003550 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003551 info->rates ? NULL : &info->rates,
3552 info->formats ? NULL : &info->formats,
3553 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003554 if (err < 0)
3555 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 }
3557 if (info->ops.open == NULL)
3558 info->ops.open = hda_pcm_default_open_close;
3559 if (info->ops.close == NULL)
3560 info->ops.close = hda_pcm_default_open_close;
3561 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003562 if (snd_BUG_ON(!info->nid))
3563 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 info->ops.prepare = hda_pcm_default_prepare;
3565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003567 if (snd_BUG_ON(!info->nid))
3568 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 info->ops.cleanup = hda_pcm_default_cleanup;
3570 }
3571 return 0;
3572}
3573
Takashi Iwaieb541332010-08-06 13:48:11 +02003574/*
3575 * codec prepare/cleanup entries
3576 */
3577int snd_hda_codec_prepare(struct hda_codec *codec,
3578 struct hda_pcm_stream *hinfo,
3579 unsigned int stream,
3580 unsigned int format,
3581 struct snd_pcm_substream *substream)
3582{
3583 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003584 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003585 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3586 if (ret >= 0)
3587 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003588 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003589 return ret;
3590}
3591EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3592
3593void snd_hda_codec_cleanup(struct hda_codec *codec,
3594 struct hda_pcm_stream *hinfo,
3595 struct snd_pcm_substream *substream)
3596{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003597 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003598 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003599 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003600}
3601EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3602
Takashi Iwaid5191e52009-11-16 14:58:17 +01003603/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003604const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3605 "Audio", "SPDIF", "HDMI", "Modem"
3606};
3607
Takashi Iwai176d5332008-07-30 15:01:44 +02003608/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003609 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003610 *
3611 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003612 */
3613static int get_empty_pcm_device(struct hda_bus *bus, int type)
3614{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003615 /* audio device indices; not linear to keep compatibility */
3616 static int audio_idx[HDA_PCM_NTYPES][5] = {
3617 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3618 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003619 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003620 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003621 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003622 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003623
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003624 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003625 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3626 return -EINVAL;
3627 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003628
3629 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3630 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3631 return audio_idx[type][i];
3632
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003633 snd_printk(KERN_WARNING "Too many %s devices\n",
3634 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003635 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003636}
3637
3638/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003639 * attach a new PCM stream
3640 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003641static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003642{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003643 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003644 struct hda_pcm_stream *info;
3645 int stream, err;
3646
Takashi Iwaib91f0802008-11-04 08:43:08 +01003647 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003648 return -EINVAL;
3649 for (stream = 0; stream < 2; stream++) {
3650 info = &pcm->stream[stream];
3651 if (info->substreams) {
3652 err = set_pcm_default_values(codec, info);
3653 if (err < 0)
3654 return err;
3655 }
3656 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003657 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003658}
3659
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003660/* assign all PCMs of the given codec */
3661int snd_hda_codec_build_pcms(struct hda_codec *codec)
3662{
3663 unsigned int pcm;
3664 int err;
3665
3666 if (!codec->num_pcms) {
3667 if (!codec->patch_ops.build_pcms)
3668 return 0;
3669 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003670 if (err < 0) {
3671 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003672 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003673 err = snd_hda_codec_reset(codec);
3674 if (err < 0) {
3675 printk(KERN_ERR
3676 "hda_codec: cannot revert codec\n");
3677 return err;
3678 }
3679 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003680 }
3681 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3682 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3683 int dev;
3684
3685 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003686 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003687
3688 if (!cpcm->pcm) {
3689 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3690 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003691 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003692 cpcm->device = dev;
3693 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003694 if (err < 0) {
3695 printk(KERN_ERR "hda_codec: cannot attach "
3696 "PCM stream %d for codec #%d\n",
3697 dev, codec->addr);
3698 continue; /* no fatal error */
3699 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003700 }
3701 }
3702 return 0;
3703}
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705/**
3706 * snd_hda_build_pcms - build PCM information
3707 * @bus: the BUS
3708 *
3709 * Create PCM information for each codec included in the bus.
3710 *
3711 * The build_pcms codec patch is requested to set up codec->num_pcms and
3712 * codec->pcm_info properly. The array is referred by the top-level driver
3713 * to create its PCM instances.
3714 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3715 * callback.
3716 *
3717 * At least, substreams, channels_min and channels_max must be filled for
3718 * each stream. substreams = 0 indicates that the stream doesn't exist.
3719 * When rates and/or formats are zero, the supported values are queried
3720 * from the given nid. The nid is used also by the default ops.prepare
3721 * and ops.cleanup callbacks.
3722 *
3723 * The driver needs to call ops.open in its open callback. Similarly,
3724 * ops.close is supposed to be called in the close callback.
3725 * ops.prepare should be called in the prepare or hw_params callback
3726 * with the proper parameters for set up.
3727 * ops.cleanup should be called in hw_free for clean up of streams.
3728 *
3729 * This function returns 0 if successfull, or a negative error code.
3730 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003731int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003733 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Takashi Iwai0ba21762007-04-16 11:29:14 +02003735 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003736 int err = snd_hda_codec_build_pcms(codec);
3737 if (err < 0)
3738 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 }
3740 return 0;
3741}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003742EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744/**
3745 * snd_hda_check_board_config - compare the current codec with the config table
3746 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003747 * @num_configs: number of config enums
3748 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 * @tbl: configuration table, terminated by null entries
3750 *
3751 * Compares the modelname or PCI subsystem id of the current codec with the
3752 * given configuration table. If a matching entry is found, returns its
3753 * config value (supposed to be 0 or positive).
3754 *
3755 * If no entries are matching, the function returns a negative value.
3756 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003757int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003758 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003759 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003761 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003762 int i;
3763 for (i = 0; i < num_configs; i++) {
3764 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003765 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003766 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3767 "selected\n", models[i]);
3768 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 }
3770 }
3771 }
3772
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003773 if (!codec->bus->pci || !tbl)
3774 return -1;
3775
3776 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3777 if (!tbl)
3778 return -1;
3779 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003780#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003781 char tmp[10];
3782 const char *model = NULL;
3783 if (models)
3784 model = models[tbl->value];
3785 if (!model) {
3786 sprintf(tmp, "#%d", tbl->value);
3787 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003789 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3790 "for config %x:%x (%s)\n",
3791 model, tbl->subvendor, tbl->subdevice,
3792 (tbl->name ? tbl->name : "Unknown device"));
3793#endif
3794 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 }
3796 return -1;
3797}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003798EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
3800/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003801 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003802 subsystem ID with the
3803 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003804
3805 This is important for Gateway notebooks with SB450 HDA Audio
3806 where the vendor ID of the PCI device is:
3807 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3808 and the vendor/subvendor are found only at the codec.
3809
3810 * @codec: the HDA codec
3811 * @num_configs: number of config enums
3812 * @models: array of model name strings
3813 * @tbl: configuration table, terminated by null entries
3814 *
3815 * Compares the modelname or PCI subsystem id of the current codec with the
3816 * given configuration table. If a matching entry is found, returns its
3817 * config value (supposed to be 0 or positive).
3818 *
3819 * If no entries are matching, the function returns a negative value.
3820 */
3821int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003822 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003823 const struct snd_pci_quirk *tbl)
3824{
3825 const struct snd_pci_quirk *q;
3826
3827 /* Search for codec ID */
3828 for (q = tbl; q->subvendor; q++) {
3829 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3830
3831 if (vendorid == codec->subsystem_id)
3832 break;
3833 }
3834
3835 if (!q->subvendor)
3836 return -1;
3837
3838 tbl = q;
3839
3840 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02003841#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003842 char tmp[10];
3843 const char *model = NULL;
3844 if (models)
3845 model = models[tbl->value];
3846 if (!model) {
3847 sprintf(tmp, "#%d", tbl->value);
3848 model = tmp;
3849 }
3850 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3851 "for config %x:%x (%s)\n",
3852 model, tbl->subvendor, tbl->subdevice,
3853 (tbl->name ? tbl->name : "Unknown device"));
3854#endif
3855 return tbl->value;
3856 }
3857 return -1;
3858}
3859EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3860
3861/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 * snd_hda_add_new_ctls - create controls from the array
3863 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003864 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 *
3866 * This helper function creates and add new controls in the given array.
3867 * The array must be terminated with an empty entry as terminator.
3868 *
3869 * Returns 0 if successful, or a negative error code.
3870 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003871int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003873 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
3875 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003876 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003877 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003878 if (knew->iface == -1) /* skip this codec private value */
3879 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003880 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003881 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003882 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003883 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003884 if (addr > 0)
3885 kctl->id.device = addr;
3886 if (idx > 0)
3887 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003888 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003889 if (!err)
3890 break;
3891 /* try first with another device index corresponding to
3892 * the codec addr; if it still fails (or it's the
3893 * primary codec), then try another control index
3894 */
3895 if (!addr && codec->addr)
3896 addr = codec->addr;
3897 else if (!idx && !knew->index) {
3898 idx = find_empty_mixer_ctl_idx(codec,
3899 knew->name);
3900 if (idx <= 0)
3901 return err;
3902 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003903 return err;
3904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 }
3906 return 0;
3907}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003908EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Takashi Iwaicb53c622007-08-10 17:21:45 +02003910#ifdef CONFIG_SND_HDA_POWER_SAVE
3911static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3912 unsigned int power_state);
3913
3914static void hda_power_work(struct work_struct *work)
3915{
3916 struct hda_codec *codec =
3917 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003918 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003919
Maxim Levitsky2e492462007-09-03 15:26:57 +02003920 if (!codec->power_on || codec->power_count) {
3921 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003922 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02003923 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003924
3925 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003926 if (bus->ops.pm_notify)
3927 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003928}
3929
3930static void hda_keep_power_on(struct hda_codec *codec)
3931{
3932 codec->power_count++;
3933 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003934 codec->power_jiffies = jiffies;
3935}
3936
Takashi Iwaid5191e52009-11-16 14:58:17 +01003937/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01003938void snd_hda_update_power_acct(struct hda_codec *codec)
3939{
3940 unsigned long delta = jiffies - codec->power_jiffies;
3941 if (codec->power_on)
3942 codec->power_on_acct += delta;
3943 else
3944 codec->power_off_acct += delta;
3945 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003946}
3947
Takashi Iwaid5191e52009-11-16 14:58:17 +01003948/**
3949 * snd_hda_power_up - Power-up the codec
3950 * @codec: HD-audio codec
3951 *
3952 * Increment the power-up counter and power up the hardware really when
3953 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003954 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003955void snd_hda_power_up(struct hda_codec *codec)
3956{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003957 struct hda_bus *bus = codec->bus;
3958
Takashi Iwaicb53c622007-08-10 17:21:45 +02003959 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02003960 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003961 return;
3962
Takashi Iwaia2f63092009-11-11 09:34:25 +01003963 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003964 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003965 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003966 if (bus->ops.pm_notify)
3967 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003968 hda_call_codec_resume(codec);
3969 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02003970 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003971}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003972EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003973
3974#define power_save(codec) \
3975 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003976
Takashi Iwaid5191e52009-11-16 14:58:17 +01003977/**
3978 * snd_hda_power_down - Power-down the codec
3979 * @codec: HD-audio codec
3980 *
3981 * Decrement the power-up counter and schedules the power-off work if
3982 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003983 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003984void snd_hda_power_down(struct hda_codec *codec)
3985{
3986 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02003987 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003988 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003989 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02003990 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01003991 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01003992 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02003993 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02003994}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003995EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003996
Takashi Iwaid5191e52009-11-16 14:58:17 +01003997/**
3998 * snd_hda_check_amp_list_power - Check the amp list and update the power
3999 * @codec: HD-audio codec
4000 * @check: the object containing an AMP list and the status
4001 * @nid: NID to check / update
4002 *
4003 * Check whether the given NID is in the amp list. If it's in the list,
4004 * check the current AMP status, and update the the power-status according
4005 * to the mute status.
4006 *
4007 * This function is supposed to be set or called from the check_power_status
4008 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004009 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004010int snd_hda_check_amp_list_power(struct hda_codec *codec,
4011 struct hda_loopback_check *check,
4012 hda_nid_t nid)
4013{
4014 struct hda_amp_list *p;
4015 int ch, v;
4016
4017 if (!check->amplist)
4018 return 0;
4019 for (p = check->amplist; p->nid; p++) {
4020 if (p->nid == nid)
4021 break;
4022 }
4023 if (!p->nid)
4024 return 0; /* nothing changed */
4025
4026 for (p = check->amplist; p->nid; p++) {
4027 for (ch = 0; ch < 2; ch++) {
4028 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4029 p->idx);
4030 if (!(v & HDA_AMP_MUTE) && v > 0) {
4031 if (!check->power_on) {
4032 check->power_on = 1;
4033 snd_hda_power_up(codec);
4034 }
4035 return 1;
4036 }
4037 }
4038 }
4039 if (check->power_on) {
4040 check->power_on = 0;
4041 snd_hda_power_down(codec);
4042 }
4043 return 0;
4044}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004045EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004046#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004048/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004049 * Channel mode helper
4050 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004051
4052/**
4053 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4054 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004055int snd_hda_ch_mode_info(struct hda_codec *codec,
4056 struct snd_ctl_elem_info *uinfo,
4057 const struct hda_channel_mode *chmode,
4058 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004059{
4060 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4061 uinfo->count = 1;
4062 uinfo->value.enumerated.items = num_chmodes;
4063 if (uinfo->value.enumerated.item >= num_chmodes)
4064 uinfo->value.enumerated.item = num_chmodes - 1;
4065 sprintf(uinfo->value.enumerated.name, "%dch",
4066 chmode[uinfo->value.enumerated.item].channels);
4067 return 0;
4068}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004069EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004070
Takashi Iwaid5191e52009-11-16 14:58:17 +01004071/**
4072 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4073 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004074int snd_hda_ch_mode_get(struct hda_codec *codec,
4075 struct snd_ctl_elem_value *ucontrol,
4076 const struct hda_channel_mode *chmode,
4077 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004078 int max_channels)
4079{
4080 int i;
4081
4082 for (i = 0; i < num_chmodes; i++) {
4083 if (max_channels == chmode[i].channels) {
4084 ucontrol->value.enumerated.item[0] = i;
4085 break;
4086 }
4087 }
4088 return 0;
4089}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004090EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004091
Takashi Iwaid5191e52009-11-16 14:58:17 +01004092/**
4093 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4094 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004095int snd_hda_ch_mode_put(struct hda_codec *codec,
4096 struct snd_ctl_elem_value *ucontrol,
4097 const struct hda_channel_mode *chmode,
4098 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004099 int *max_channelsp)
4100{
4101 unsigned int mode;
4102
4103 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004104 if (mode >= num_chmodes)
4105 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004106 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004107 return 0;
4108 /* change the current channel setting */
4109 *max_channelsp = chmode[mode].channels;
4110 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004111 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004112 return 1;
4113}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004114EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116/*
4117 * input MUX helper
4118 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004119
4120/**
4121 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4122 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004123int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4124 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
4126 unsigned int index;
4127
4128 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4129 uinfo->count = 1;
4130 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004131 if (!imux->num_items)
4132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 index = uinfo->value.enumerated.item;
4134 if (index >= imux->num_items)
4135 index = imux->num_items - 1;
4136 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4137 return 0;
4138}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004139EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Takashi Iwaid5191e52009-11-16 14:58:17 +01004141/**
4142 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4143 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004144int snd_hda_input_mux_put(struct hda_codec *codec,
4145 const struct hda_input_mux *imux,
4146 struct snd_ctl_elem_value *ucontrol,
4147 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 unsigned int *cur_val)
4149{
4150 unsigned int idx;
4151
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004152 if (!imux->num_items)
4153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 idx = ucontrol->value.enumerated.item[0];
4155 if (idx >= imux->num_items)
4156 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004157 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004159 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4160 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 *cur_val = idx;
4162 return 1;
4163}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004164EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
4166
4167/*
4168 * Multi-channel / digital-out PCM helper functions
4169 */
4170
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004171/* setup SPDIF output stream */
4172static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4173 unsigned int stream_tag, unsigned int format)
4174{
4175 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02004176 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004177 set_dig_out_convert(codec, nid,
Takashi Iwai2f728532008-09-25 16:32:41 +02004178 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4179 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004180 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004181 if (codec->slave_dig_outs) {
4182 hda_nid_t *d;
4183 for (d = codec->slave_dig_outs; *d; d++)
4184 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4185 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004186 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004187 /* turn on again (if needed) */
4188 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4189 set_dig_out_convert(codec, nid,
4190 codec->spdif_ctls & 0xff, -1);
4191}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004192
Takashi Iwai2f728532008-09-25 16:32:41 +02004193static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4194{
4195 snd_hda_codec_cleanup_stream(codec, nid);
4196 if (codec->slave_dig_outs) {
4197 hda_nid_t *d;
4198 for (d = codec->slave_dig_outs; *d; d++)
4199 snd_hda_codec_cleanup_stream(codec, *d);
4200 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004201}
4202
Takashi Iwaid5191e52009-11-16 14:58:17 +01004203/**
4204 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4205 * @bus: HD-audio bus
4206 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004207void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4208{
4209 struct hda_codec *codec;
4210
4211 if (!bus)
4212 return;
4213 list_for_each_entry(codec, &bus->codec_list, list) {
4214#ifdef CONFIG_SND_HDA_POWER_SAVE
4215 if (!codec->power_on)
4216 continue;
4217#endif
4218 if (codec->patch_ops.reboot_notify)
4219 codec->patch_ops.reboot_notify(codec);
4220 }
4221}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004222EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004223
Takashi Iwaid5191e52009-11-16 14:58:17 +01004224/**
4225 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004227int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4228 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
Ingo Molnar62932df2006-01-16 16:34:20 +01004230 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004231 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4232 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004233 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004235 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 return 0;
4237}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004238EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Takashi Iwaid5191e52009-11-16 14:58:17 +01004240/**
4241 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4242 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004243int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4244 struct hda_multi_out *mout,
4245 unsigned int stream_tag,
4246 unsigned int format,
4247 struct snd_pcm_substream *substream)
4248{
4249 mutex_lock(&codec->spdif_mutex);
4250 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4251 mutex_unlock(&codec->spdif_mutex);
4252 return 0;
4253}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004254EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004255
Takashi Iwaid5191e52009-11-16 14:58:17 +01004256/**
4257 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4258 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004259int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4260 struct hda_multi_out *mout)
4261{
4262 mutex_lock(&codec->spdif_mutex);
4263 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4264 mutex_unlock(&codec->spdif_mutex);
4265 return 0;
4266}
4267EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4268
Takashi Iwaid5191e52009-11-16 14:58:17 +01004269/**
4270 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004272int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4273 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
Ingo Molnar62932df2006-01-16 16:34:20 +01004275 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004277 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 return 0;
4279}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004280EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Takashi Iwaid5191e52009-11-16 14:58:17 +01004282/**
4283 * snd_hda_multi_out_analog_open - open analog outputs
4284 *
4285 * Open analog outputs and set up the hw-constraints.
4286 * If the digital outputs can be opened as slave, open the digital
4287 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004289int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4290 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004291 struct snd_pcm_substream *substream,
4292 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293{
Takashi Iwai9a081602008-02-12 18:37:26 +01004294 struct snd_pcm_runtime *runtime = substream->runtime;
4295 runtime->hw.channels_max = mout->max_channels;
4296 if (mout->dig_out_nid) {
4297 if (!mout->analog_rates) {
4298 mout->analog_rates = hinfo->rates;
4299 mout->analog_formats = hinfo->formats;
4300 mout->analog_maxbps = hinfo->maxbps;
4301 } else {
4302 runtime->hw.rates = mout->analog_rates;
4303 runtime->hw.formats = mout->analog_formats;
4304 hinfo->maxbps = mout->analog_maxbps;
4305 }
4306 if (!mout->spdif_rates) {
4307 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4308 &mout->spdif_rates,
4309 &mout->spdif_formats,
4310 &mout->spdif_maxbps);
4311 }
4312 mutex_lock(&codec->spdif_mutex);
4313 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004314 if ((runtime->hw.rates & mout->spdif_rates) &&
4315 (runtime->hw.formats & mout->spdif_formats)) {
4316 runtime->hw.rates &= mout->spdif_rates;
4317 runtime->hw.formats &= mout->spdif_formats;
4318 if (mout->spdif_maxbps < hinfo->maxbps)
4319 hinfo->maxbps = mout->spdif_maxbps;
4320 } else {
4321 mout->share_spdif = 0;
4322 /* FIXME: need notify? */
4323 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004324 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004325 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4328 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4329}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004330EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
Takashi Iwaid5191e52009-11-16 14:58:17 +01004332/**
4333 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4334 *
4335 * Set up the i/o for analog out.
4336 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004338int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4339 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 unsigned int stream_tag,
4341 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004342 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343{
4344 hda_nid_t *nids = mout->dac_nids;
4345 int chs = substream->runtime->channels;
4346 int i;
4347
Ingo Molnar62932df2006-01-16 16:34:20 +01004348 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004349 if (mout->dig_out_nid && mout->share_spdif &&
4350 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004352 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4353 format) &&
4354 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004356 setup_dig_out_stream(codec, mout->dig_out_nid,
4357 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 } else {
4359 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004360 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
4362 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004363 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
4365 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004366 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4367 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004368 if (!mout->no_share_stream &&
4369 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004371 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4372 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004373 /* extra outputs copied from front */
4374 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004375 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004376 snd_hda_codec_setup_stream(codec,
4377 mout->extra_out_nid[i],
4378 stream_tag, 0, format);
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 /* surrounds */
4381 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004382 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004383 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4384 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004385 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004386 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4387 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
4389 return 0;
4390}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004391EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Takashi Iwaid5191e52009-11-16 14:58:17 +01004393/**
4394 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004396int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4397 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
4399 hda_nid_t *nids = mout->dac_nids;
4400 int i;
4401
4402 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004403 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004405 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004406 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4407 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004408 snd_hda_codec_cleanup_stream(codec,
4409 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004410 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004412 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 mout->dig_out_used = 0;
4414 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004415 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return 0;
4417}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004418EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004420/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004421 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004422 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004423
Takashi Iwai12f288b2007-08-02 15:51:59 +02004424static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004425{
4426 for (; *list; list++)
4427 if (*list == nid)
4428 return 1;
4429 return 0;
4430}
4431
Steve Longerbeam81937d32007-05-08 15:33:03 +02004432
4433/*
4434 * Sort an associated group of pins according to their sequence numbers.
4435 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004436static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004437 int num_pins)
4438{
4439 int i, j;
4440 short seq;
4441 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004442
Steve Longerbeam81937d32007-05-08 15:33:03 +02004443 for (i = 0; i < num_pins; i++) {
4444 for (j = i + 1; j < num_pins; j++) {
4445 if (sequences[i] > sequences[j]) {
4446 seq = sequences[i];
4447 sequences[i] = sequences[j];
4448 sequences[j] = seq;
4449 nid = pins[i];
4450 pins[i] = pins[j];
4451 pins[j] = nid;
4452 }
4453 }
4454 }
4455}
4456
4457
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004458/* add the found input-pin to the cfg->inputs[] table */
4459static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4460 int type)
4461{
4462 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4463 cfg->inputs[cfg->num_inputs].pin = nid;
4464 cfg->inputs[cfg->num_inputs].type = type;
4465 cfg->num_inputs++;
4466 }
4467}
4468
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004469/* sort inputs in the order of AUTO_PIN_* type */
4470static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4471{
4472 int i, j;
4473
4474 for (i = 0; i < cfg->num_inputs; i++) {
4475 for (j = i + 1; j < cfg->num_inputs; j++) {
4476 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4477 struct auto_pin_cfg_item tmp;
4478 tmp = cfg->inputs[i];
4479 cfg->inputs[i] = cfg->inputs[j];
4480 cfg->inputs[j] = tmp;
4481 }
4482 }
4483 }
4484}
4485
Takashi Iwai82bc9552006-03-21 11:24:42 +01004486/*
4487 * Parse all pin widgets and store the useful pin nids to cfg
4488 *
4489 * The number of line-outs or any primary output is stored in line_outs,
4490 * and the corresponding output pins are assigned to line_out_pins[],
4491 * in the order of front, rear, CLFE, side, ...
4492 *
4493 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004494 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004495 * is detected, one of speaker of HP pins is assigned as the primary
4496 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4497 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004498 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004499 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004500 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4501 * respectively.
4502 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004503int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4504 struct auto_pin_cfg *cfg,
4505 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004506{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004507 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004508 short seq, assoc_line_out, assoc_speaker;
4509 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4510 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004511 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004512 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004513
4514 memset(cfg, 0, sizeof(*cfg));
4515
Steve Longerbeam81937d32007-05-08 15:33:03 +02004516 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4517 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004518 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004519 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004520
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004521 end_nid = codec->start_nid + codec->num_nodes;
4522 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004523 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004524 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004525 unsigned int def_conf;
4526 short assoc, loc;
4527
4528 /* read all default configuration for pin complex */
4529 if (wid_type != AC_WID_PIN)
4530 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004531 /* ignore the given nids (e.g. pc-beep returns error) */
4532 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4533 continue;
4534
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004535 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004536 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4537 continue;
4538 loc = get_defcfg_location(def_conf);
4539 switch (get_defcfg_device(def_conf)) {
4540 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004541 seq = get_defcfg_sequence(def_conf);
4542 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004543
4544 if (!(wid_caps & AC_WCAP_STEREO))
4545 if (!cfg->mono_out_pin)
4546 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004547 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004548 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004549 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004550 assoc_line_out = assoc;
4551 else if (assoc_line_out != assoc)
4552 continue;
4553 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4554 continue;
4555 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004556 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004557 cfg->line_outs++;
4558 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004559 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004560 seq = get_defcfg_sequence(def_conf);
4561 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004562 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004563 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004564 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004565 assoc_speaker = assoc;
4566 else if (assoc_speaker != assoc)
4567 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004568 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4569 continue;
4570 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004571 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004572 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004573 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004574 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004575 seq = get_defcfg_sequence(def_conf);
4576 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004577 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4578 continue;
4579 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004580 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004581 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004582 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004583 case AC_JACK_MIC_IN:
4584 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004585 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004586 case AC_JACK_LINE_IN:
4587 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004588 break;
4589 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004590 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004591 break;
4592 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004593 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004594 break;
4595 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004596 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004597 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4598 continue;
4599 cfg->dig_out_pins[cfg->dig_outs] = nid;
4600 cfg->dig_out_type[cfg->dig_outs] =
4601 (loc == AC_JACK_LOC_HDMI) ?
4602 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4603 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004604 break;
4605 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004606 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004607 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004608 if (loc == AC_JACK_LOC_HDMI)
4609 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4610 else
4611 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004612 break;
4613 }
4614 }
4615
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004616 /* FIX-UP:
4617 * If no line-out is defined but multiple HPs are found,
4618 * some of them might be the real line-outs.
4619 */
4620 if (!cfg->line_outs && cfg->hp_outs > 1) {
4621 int i = 0;
4622 while (i < cfg->hp_outs) {
4623 /* The real HPs should have the sequence 0x0f */
4624 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4625 i++;
4626 continue;
4627 }
4628 /* Move it to the line-out table */
4629 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4630 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4631 cfg->line_outs++;
4632 cfg->hp_outs--;
4633 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4634 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004635 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004636 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4637 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004638 memset(cfg->hp_pins + cfg->hp_outs, 0,
4639 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004640 if (!cfg->hp_outs)
4641 cfg->line_out_type = AUTO_PIN_HP_OUT;
4642
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004643 }
4644
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004645 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004646 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4647 cfg->line_outs);
4648 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4649 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004650 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4651 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004652
Steve Longerbeam81937d32007-05-08 15:33:03 +02004653 /*
4654 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4655 * as a primary output
4656 */
4657 if (!cfg->line_outs) {
4658 if (cfg->speaker_outs) {
4659 cfg->line_outs = cfg->speaker_outs;
4660 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4661 sizeof(cfg->speaker_pins));
4662 cfg->speaker_outs = 0;
4663 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4664 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4665 } else if (cfg->hp_outs) {
4666 cfg->line_outs = cfg->hp_outs;
4667 memcpy(cfg->line_out_pins, cfg->hp_pins,
4668 sizeof(cfg->hp_pins));
4669 cfg->hp_outs = 0;
4670 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4671 cfg->line_out_type = AUTO_PIN_HP_OUT;
4672 }
4673 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004674
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004675 /* Reorder the surround channels
4676 * ALSA sequence is front/surr/clfe/side
4677 * HDA sequence is:
4678 * 4-ch: front/surr => OK as it is
4679 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004680 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004681 */
4682 switch (cfg->line_outs) {
4683 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004684 case 4:
4685 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004686 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004687 cfg->line_out_pins[2] = nid;
4688 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004689 }
4690
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004691 sort_autocfg_input_pins(cfg);
4692
Takashi Iwai82bc9552006-03-21 11:24:42 +01004693 /*
4694 * debug prints of the parsed results
4695 */
4696 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4697 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4698 cfg->line_out_pins[2], cfg->line_out_pins[3],
4699 cfg->line_out_pins[4]);
4700 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4701 cfg->speaker_outs, cfg->speaker_pins[0],
4702 cfg->speaker_pins[1], cfg->speaker_pins[2],
4703 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004704 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4705 cfg->hp_outs, cfg->hp_pins[0],
4706 cfg->hp_pins[1], cfg->hp_pins[2],
4707 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004708 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004709 if (cfg->dig_outs)
4710 snd_printd(" dig-out=0x%x/0x%x\n",
4711 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004712 snd_printd(" inputs:");
4713 for (i = 0; i < cfg->num_inputs; i++) {
4714 snd_printdd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004715 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004716 cfg->inputs[i].pin);
4717 }
4718 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004719 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004720 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004721
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004722 return 0;
4723}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004724EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004725
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004726int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004727{
4728 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004729 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004730 if (conn == AC_JACK_PORT_NONE)
4731 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004732 /* Windows may claim the internal mic to be BOTH, too */
4733 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004734 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004735 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004736 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004737 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004738 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004739 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004740 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004741 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004742 return INPUT_PIN_ATTR_FRONT;
4743 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004744}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004745EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004746
Takashi Iwai990061c2010-09-09 22:08:44 +02004747/**
4748 * hda_get_input_pin_label - Give a label for the given input pin
4749 *
4750 * When check_location is true, the function checks the pin location
4751 * for mic and line-in pins, and set an appropriate prefix like "Front",
4752 * "Rear", "Internal".
4753 */
4754
Takashi Iwai10a20af2010-09-09 16:28:02 +02004755const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4756 int check_location)
4757{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004758 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004759 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004760 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4761 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004762 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004763
Takashi Iwai10a20af2010-09-09 16:28:02 +02004764 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004765
4766 switch (get_defcfg_device(def_conf)) {
4767 case AC_JACK_MIC_IN:
4768 if (!check_location)
4769 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004770 attr = snd_hda_get_input_pin_attr(def_conf);
4771 if (!attr)
4772 return "None";
4773 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004774 case AC_JACK_LINE_IN:
4775 if (!check_location)
4776 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004777 attr = snd_hda_get_input_pin_attr(def_conf);
4778 if (!attr)
4779 return "None";
4780 if (attr == INPUT_PIN_ATTR_DOCK)
4781 return "Dock Line";
4782 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004783 case AC_JACK_AUX:
4784 return "Aux";
4785 case AC_JACK_CD:
4786 return "CD";
4787 case AC_JACK_SPDIF_IN:
4788 return "SPDIF In";
4789 case AC_JACK_DIG_OTHER_IN:
4790 return "Digital In";
4791 default:
4792 return "Misc";
4793 }
4794}
4795EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4796
Takashi Iwaia1c98512010-09-09 21:36:27 +02004797/* Check whether the location prefix needs to be added to the label.
4798 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4799 * have to put "Front" prefix to each label. In such a case, returns false.
4800 */
4801static int check_mic_location_need(struct hda_codec *codec,
4802 const struct auto_pin_cfg *cfg,
4803 int input)
4804{
4805 unsigned int defc;
4806 int i, attr, attr2;
4807
4808 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004809 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004810 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004811 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004812 return 1;
4813
4814 attr = 0;
4815 for (i = 0; i < cfg->num_inputs; i++) {
4816 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004817 attr2 = snd_hda_get_input_pin_attr(defc);
4818 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004819 if (attr && attr != attr2)
4820 return 1; /* different locations found */
4821 attr = attr2;
4822 }
4823 }
4824 return 0;
4825}
4826
Takashi Iwai990061c2010-09-09 22:08:44 +02004827/**
4828 * hda_get_autocfg_input_label - Get a label for the given input
4829 *
4830 * Get a label for the given input pin defined by the autocfg item.
4831 * Unlike hda_get_input_pin_label(), this function checks all inputs
4832 * defined in autocfg and avoids the redundant mic/line prefix as much as
4833 * possible.
4834 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004835const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4836 const struct auto_pin_cfg *cfg,
4837 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004838{
4839 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02004840 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004841
Takashi Iwai10a20af2010-09-09 16:28:02 +02004842 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4843 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4844 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004845 if (has_multiple_pins && type == AUTO_PIN_MIC)
4846 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004847 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4848 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004849}
Takashi Iwai10a20af2010-09-09 16:28:02 +02004850EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4851
Takashi Iwai990061c2010-09-09 22:08:44 +02004852/**
4853 * snd_hda_add_imux_item - Add an item to input_mux
4854 *
4855 * When the same label is used already in the existing items, the number
4856 * suffix is appended to the label. This label index number is stored
4857 * to type_idx when non-NULL pointer is given.
4858 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004859int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4860 int index, int *type_idx)
4861{
4862 int i, label_idx = 0;
4863 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4864 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4865 return -EINVAL;
4866 }
4867 for (i = 0; i < imux->num_items; i++) {
4868 if (!strncmp(label, imux->items[i].label, strlen(label)))
4869 label_idx++;
4870 }
4871 if (type_idx)
4872 *type_idx = label_idx;
4873 if (label_idx > 0)
4874 snprintf(imux->items[imux->num_items].label,
4875 sizeof(imux->items[imux->num_items].label),
4876 "%s %d", label, label_idx);
4877 else
4878 strlcpy(imux->items[imux->num_items].label, label,
4879 sizeof(imux->items[imux->num_items].label));
4880 imux->items[imux->num_items].index = index;
4881 imux->num_items++;
4882 return 0;
4883}
4884EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004885
Takashi Iwai4a471b72005-12-07 13:56:29 +01004886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887#ifdef CONFIG_PM
4888/*
4889 * power management
4890 */
4891
4892/**
4893 * snd_hda_suspend - suspend the codecs
4894 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 *
4896 * Returns 0 if successful.
4897 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004898int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004900 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
Takashi Iwai0ba21762007-04-16 11:29:14 +02004902 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02004903#ifdef CONFIG_SND_HDA_POWER_SAVE
4904 if (!codec->power_on)
4905 continue;
4906#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02004907 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 }
4909 return 0;
4910}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004911EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
4913/**
4914 * snd_hda_resume - resume the codecs
4915 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 *
4917 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004918 *
4919 * This fucntion is defined only when POWER_SAVE isn't set.
4920 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 */
4922int snd_hda_resume(struct hda_bus *bus)
4923{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004924 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Takashi Iwai0ba21762007-04-16 11:29:14 +02004926 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02004927 if (snd_hda_codec_needs_resume(codec))
4928 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 return 0;
4931}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004932EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004933#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004934
4935/*
4936 * generic arrays
4937 */
4938
Takashi Iwaid5191e52009-11-16 14:58:17 +01004939/**
4940 * snd_array_new - get a new element from the given array
4941 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004942 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004943 * Get a new element from the given array. If it exceeds the
4944 * pre-allocated array size, re-allocate the array.
4945 *
4946 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004947 */
4948void *snd_array_new(struct snd_array *array)
4949{
4950 if (array->used >= array->alloced) {
4951 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01004952 void *nlist;
4953 if (snd_BUG_ON(num >= 4096))
4954 return NULL;
4955 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004956 if (!nlist)
4957 return NULL;
4958 if (array->list) {
4959 memcpy(nlist, array->list,
4960 array->elem_size * array->alloced);
4961 kfree(array->list);
4962 }
4963 array->list = nlist;
4964 array->alloced = num;
4965 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01004966 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004967}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004968EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004969
Takashi Iwaid5191e52009-11-16 14:58:17 +01004970/**
4971 * snd_array_free - free the given array elements
4972 * @array: the array object
4973 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004974void snd_array_free(struct snd_array *array)
4975{
4976 kfree(array->list);
4977 array->used = 0;
4978 array->alloced = 0;
4979 array->list = NULL;
4980}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004981EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01004982
Takashi Iwaid5191e52009-11-16 14:58:17 +01004983/**
4984 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4985 * @pcm: PCM caps bits
4986 * @buf: the string buffer to write
4987 * @buflen: the max buffer length
4988 *
Takashi Iwaib2022262008-11-21 21:24:03 +01004989 * used by hda_proc.c and hda_eld.c
4990 */
4991void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4992{
4993 static unsigned int rates[] = {
4994 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4995 96000, 176400, 192000, 384000
4996 };
4997 int i, j;
4998
4999 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5000 if (pcm & (1 << i))
5001 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5002
5003 buf[j] = '\0'; /* necessary when j == 0 */
5004}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005005EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005006
Takashi Iwaid5191e52009-11-16 14:58:17 +01005007/**
5008 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5009 * @pcm: PCM caps bits
5010 * @buf: the string buffer to write
5011 * @buflen: the max buffer length
5012 *
5013 * used by hda_proc.c and hda_eld.c
5014 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005015void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5016{
5017 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5018 int i, j;
5019
5020 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5021 if (pcm & (AC_SUPPCM_BITS_8 << i))
5022 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5023
5024 buf[j] = '\0'; /* necessary when j == 0 */
5025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005026EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005027
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005028#ifdef CONFIG_SND_HDA_INPUT_JACK
5029/*
5030 * Input-jack notification support
5031 */
5032struct hda_jack_item {
5033 hda_nid_t nid;
5034 int type;
5035 struct snd_jack *jack;
5036};
5037
5038static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5039 int type)
5040{
5041 switch (type) {
5042 case SND_JACK_HEADPHONE:
5043 return "Headphone";
5044 case SND_JACK_MICROPHONE:
5045 return "Mic";
5046 case SND_JACK_LINEOUT:
5047 return "Line-out";
5048 case SND_JACK_HEADSET:
5049 return "Headset";
5050 default:
5051 return "Misc";
5052 }
5053}
5054
5055static void hda_free_jack_priv(struct snd_jack *jack)
5056{
5057 struct hda_jack_item *jacks = jack->private_data;
5058 jacks->nid = 0;
5059 jacks->jack = NULL;
5060}
5061
5062int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5063 const char *name)
5064{
5065 struct hda_jack_item *jack;
5066 int err;
5067
5068 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5069 jack = snd_array_new(&codec->jacks);
5070 if (!jack)
5071 return -ENOMEM;
5072
5073 jack->nid = nid;
5074 jack->type = type;
5075 if (!name)
5076 name = get_jack_default_name(codec, nid, type);
5077 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5078 if (err < 0) {
5079 jack->nid = 0;
5080 return err;
5081 }
5082 jack->jack->private_data = jack;
5083 jack->jack->private_free = hda_free_jack_priv;
5084 return 0;
5085}
5086EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5087
5088void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5089{
5090 struct hda_jack_item *jacks = codec->jacks.list;
5091 int i;
5092
5093 if (!jacks)
5094 return;
5095
5096 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5097 unsigned int pin_ctl;
5098 unsigned int present;
5099 int type;
5100
5101 if (jacks->nid != nid)
5102 continue;
5103 present = snd_hda_jack_detect(codec, nid);
5104 type = jacks->type;
5105 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5106 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5107 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5108 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5109 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5110 }
5111 snd_jack_report(jacks->jack, present ? type : 0);
5112 }
5113}
5114EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5115
5116/* free jack instances manually when clearing/reconfiguring */
5117void snd_hda_input_jack_free(struct hda_codec *codec)
5118{
5119 if (!codec->bus->shutdown && codec->jacks.list) {
5120 struct hda_jack_item *jacks = codec->jacks.list;
5121 int i;
5122 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5123 if (jacks->jack)
5124 snd_device_free(codec->bus->card, jacks->jack);
5125 }
5126 }
5127 snd_array_free(&codec->jacks);
5128}
5129EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5130#endif /* CONFIG_SND_HDA_INPUT_JACK */
5131
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005132MODULE_DESCRIPTION("HDA codec core");
5133MODULE_LICENSE("GPL");