blob: c0e83ed0b351460180afdf26c9b3fb4d500a788c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020034#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020035#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * vendor / preset table
39 */
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46/* codec vendor labels */
47static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010048 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020049 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020050 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010051 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010052 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010053 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020054 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010055 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020056 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020058 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020060 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010061 { 0x17e8, "Chrontel" },
62 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000063 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010065 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020066 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 {} /* terminator */
68};
69
Takashi Iwai1289e9e2008-11-27 15:47:11 +010070static DEFINE_MUTEX(preset_mutex);
71static LIST_HEAD(hda_preset_tables);
72
73int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74{
75 mutex_lock(&preset_mutex);
76 list_add_tail(&preset->list, &hda_preset_tables);
77 mutex_unlock(&preset_mutex);
78 return 0;
79}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010080EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010081
82int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83{
84 mutex_lock(&preset_mutex);
85 list_del(&preset->list);
86 mutex_unlock(&preset_mutex);
87 return 0;
88}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010089EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwaicb53c622007-08-10 17:21:45 +020091#ifdef CONFIG_SND_HDA_POWER_SAVE
92static void hda_power_work(struct work_struct *work);
93static void hda_keep_power_on(struct hda_codec *codec);
94#else
95static inline void hda_keep_power_on(struct hda_codec *codec) {}
96#endif
97
Takashi Iwaid5191e52009-11-16 14:58:17 +010098/**
99 * snd_hda_get_jack_location - Give a location string of the jack
100 * @cfg: pin default config value
101 *
102 * Parse the pin default config value and returns the string of the
103 * jack location, e.g. "Rear", "Front", etc.
104 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400105const char *snd_hda_get_jack_location(u32 cfg)
106{
107 static char *bases[7] = {
108 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 };
110 static unsigned char specials_idx[] = {
111 0x07, 0x08,
112 0x17, 0x18, 0x19,
113 0x37, 0x38
114 };
115 static char *specials[] = {
116 "Rear Panel", "Drive Bar",
117 "Riser", "HDMI", "ATAPI",
118 "Mobile-In", "Mobile-Out"
119 };
120 int i;
121 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
122 if ((cfg & 0x0f) < 7)
123 return bases[cfg & 0x0f];
124 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
125 if (cfg == specials_idx[i])
126 return specials[i];
127 }
128 return "UNKNOWN";
129}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100130EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400131
Takashi Iwaid5191e52009-11-16 14:58:17 +0100132/**
133 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
134 * @cfg: pin default config value
135 *
136 * Parse the pin default config value and returns the string of the
137 * jack connectivity, i.e. external or internal connection.
138 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139const char *snd_hda_get_jack_connectivity(u32 cfg)
140{
141 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142
143 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100145EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400146
Takashi Iwaid5191e52009-11-16 14:58:17 +0100147/**
148 * snd_hda_get_jack_type - Give a type string of the jack
149 * @cfg: pin default config value
150 *
151 * Parse the pin default config value and returns the string of the
152 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154const char *snd_hda_get_jack_type(u32 cfg)
155{
156 static char *jack_types[16] = {
157 "Line Out", "Speaker", "HP Out", "CD",
158 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
159 "Line In", "Aux", "Mic", "Telephony",
160 "SPDIF In", "Digitial In", "Reserved", "Other"
161 };
162
163 return jack_types[(cfg & AC_DEFCFG_DEVICE)
164 >> AC_DEFCFG_DEVICE_SHIFT];
165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100166EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400167
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100168/*
169 * Compose a 32bit command word to be sent to the HD-audio controller
170 */
171static inline unsigned int
172make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
173 unsigned int verb, unsigned int parm)
174{
175 u32 val;
176
Takashi Iwai82e1b802009-07-17 12:47:34 +0200177 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
178 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800179 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
180 codec->addr, direct, nid, verb, parm);
181 return ~0;
182 }
183
184 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100185 val |= (u32)direct << 27;
186 val |= (u32)nid << 20;
187 val |= verb << 8;
188 val |= parm;
189 return val;
190}
191
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200192/*
193 * Send and receive a verb
194 */
195static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
196 unsigned int *res)
197{
198 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200199 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200
Wu Fengguang6430aee2009-07-17 16:49:19 +0800201 if (cmd == ~0)
202 return -1;
203
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200204 if (res)
205 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200206 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207 snd_hda_power_up(codec);
208 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200209 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200210 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800211 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 mutex_unlock(&bus->cmd_mutex);
213 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 if (res && *res == -1 && bus->rirb_error) {
215 if (bus->response_reset) {
216 snd_printd("hda_codec: resetting BUS due to "
217 "fatal communication error\n");
218 bus->ops.bus_reset(bus);
219 }
220 goto again;
221 }
222 /* clear reset-flag when the communication gets recovered */
223 if (!err)
224 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200225 return err;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/**
229 * snd_hda_codec_read - send a command and get the response
230 * @codec: the HDA codec
231 * @nid: NID to send the command
232 * @direct: direct flag
233 * @verb: the verb to send
234 * @parm: the parameter for the verb
235 *
236 * Send a single command and read the corresponding response.
237 *
238 * Returns the obtained response value, or -1 for an error.
239 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200240unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
241 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int verb, unsigned int parm)
243{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200244 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700246 if (codec_exec_verb(codec, cmd, &res))
247 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return res;
249}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100250EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252/**
253 * snd_hda_codec_write - send a single command without waiting for response
254 * @codec: the HDA codec
255 * @nid: NID to send the command
256 * @direct: direct flag
257 * @verb: the verb to send
258 * @parm: the parameter for the verb
259 *
260 * Send a single command without waiting for response.
261 *
262 * Returns 0 if successful, or a negative error code.
263 */
264int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
265 unsigned int verb, unsigned int parm)
266{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200267 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100268 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200269 return codec_exec_verb(codec, cmd,
270 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100272EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/**
275 * snd_hda_sequence_write - sequence writes
276 * @codec: the HDA codec
277 * @seq: VERB array to send
278 *
279 * Send the commands sequentially from the given array.
280 * The array must be terminated with NID=0.
281 */
282void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
283{
284 for (; seq->nid; seq++)
285 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100287EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/**
290 * snd_hda_get_sub_nodes - get the range of sub nodes
291 * @codec: the HDA codec
292 * @nid: NID to parse
293 * @start_id: the pointer to store the start NID
294 *
295 * Parse the NID and store the start NID of its sub-nodes.
296 * Returns the number of sub-nodes.
297 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200298int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
299 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 unsigned int parm;
302
303 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200304 if (parm == -1)
305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *start_id = (parm >> 16) & 0x7fff;
307 return (int)(parm & 0x7fff);
308}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100309EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200311/* look up the cached results */
312static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
313{
314 int i, len;
315 for (i = 0; i < array->used; ) {
316 hda_nid_t *p = snd_array_elem(array, i);
317 if (nid == *p)
318 return p;
319 len = p[1];
320 i += len + 2;
321 }
322 return NULL;
323}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200326 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * @codec: the HDA codec
328 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200329 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
331 * Parses the connection list of the given widget and stores the list
332 * of NIDs.
333 *
334 * Returns the number of connections, or a negative error code.
335 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200336int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
337 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200338{
339 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200340 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200341 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200342 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200343 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200344
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200345 again:
346 /* if the connection-list is already cached, read it */
347 p = lookup_conn_list(array, nid);
348 if (p) {
349 if (listp)
350 *listp = p + 2;
351 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200352 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200353 if (snd_BUG_ON(added))
354 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200355
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200356 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200357 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200358 if (len < 0)
359 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200360 err = snd_hda_override_conn_list(codec, nid, len, list);
361 if (err < 0)
362 return err;
363 added = true;
364 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200365}
Takashi Iwaidce20792011-06-24 14:10:28 +0200366EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
367
368/**
369 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * @codec: the HDA codec
371 * @nid: NID to parse
372 * @conn_list: connection list array
373 * @max_conns: max. number of connections to store
374 *
375 * Parses the connection list of the given widget and stores the list
376 * of NIDs.
377 *
378 * Returns the number of connections, or a negative error code.
379 */
380int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200381 hda_nid_t *conn_list, int max_conns)
382{
Takashi Iwaidce20792011-06-24 14:10:28 +0200383 const hda_nid_t *list;
384 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200385
Takashi Iwaidce20792011-06-24 14:10:28 +0200386 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200387 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200388 if (len > max_conns) {
389 snd_printk(KERN_ERR "hda_codec: "
390 "Too many connections %d for NID 0x%x\n",
391 len, nid);
392 return -EINVAL;
393 }
394 memcpy(conn_list, list, len * sizeof(hda_nid_t));
395 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200396}
397EXPORT_SYMBOL_HDA(snd_hda_get_connections);
398
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200399/**
400 * snd_hda_get_raw_connections - copy connection list without cache
401 * @codec: the HDA codec
402 * @nid: NID to parse
403 * @conn_list: connection list array
404 * @max_conns: max. number of connections to store
405 *
406 * Like snd_hda_get_connections(), copy the connection list but without
407 * checking through the connection-list cache.
408 * Currently called only from hda_proc.c, so not exported.
409 */
410int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
411 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100414 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200416 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100417 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Takashi Iwaida3cec32008-08-08 17:12:14 +0200419 if (snd_BUG_ON(!conn_list || max_conns <= 0))
420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200422 wcaps = get_wcaps(codec, nid);
423 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200424 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
425 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
428 if (parm & AC_CLIST_LONG) {
429 /* long form */
430 shift = 16;
431 num_elems = 2;
432 } else {
433 /* short form */
434 shift = 8;
435 num_elems = 4;
436 }
437 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 mask = (1 << (shift-1)) - 1;
439
Takashi Iwai0ba21762007-04-16 11:29:14 +0200440 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0; /* no connection */
442
443 if (conn_len == 1) {
444 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200445 parm = snd_hda_codec_read(codec, nid, 0,
446 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200447 if (parm == -1 && codec->bus->rirb_error)
448 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 conn_list[0] = parm & mask;
450 return 1;
451 }
452
453 /* multi connection */
454 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100455 prev_nid = 0;
456 for (i = 0; i < conn_len; i++) {
457 int range_val;
458 hda_nid_t val, n;
459
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200460 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100461 parm = snd_hda_codec_read(codec, nid, 0,
462 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200463 if (parm == -1 && codec->bus->rirb_error)
464 return -EIO;
465 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200466 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100467 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200468 if (val == 0) {
469 snd_printk(KERN_WARNING "hda_codec: "
470 "invalid CONNECT_LIST verb %x[%i]:%x\n",
471 nid, i, parm);
472 return 0;
473 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100474 parm >>= shift;
475 if (range_val) {
476 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200477 if (!prev_nid || prev_nid >= val) {
478 snd_printk(KERN_WARNING "hda_codec: "
479 "invalid dep_range_val %x:%x\n",
480 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100481 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100483 for (n = prev_nid + 1; n <= val; n++) {
484 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200485 snd_printk(KERN_ERR "hda_codec: "
486 "Too many connections %d for NID 0x%x\n",
487 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100488 return -EINVAL;
489 }
490 conn_list[conns++] = n;
491 }
492 } else {
493 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200494 snd_printk(KERN_ERR "hda_codec: "
495 "Too many connections %d for NID 0x%x\n",
496 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100497 return -EINVAL;
498 }
499 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100501 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 return conns;
504}
505
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200506static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
507{
508 hda_nid_t *p = snd_array_new(array);
509 if (!p)
510 return false;
511 *p = nid;
512 return true;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200516 * snd_hda_override_conn_list - add/modify the connection-list to cache
517 * @codec: the HDA codec
518 * @nid: NID to parse
519 * @len: number of connection list entries
520 * @list: the list of connection entries
521 *
522 * Add or modify the given connection-list to the cache. If the corresponding
523 * cache already exists, invalidate it and append a new one.
524 *
525 * Returns zero or a negative error code.
526 */
527int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
528 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200530 struct snd_array *array = &codec->conn_lists;
531 hda_nid_t *p;
532 int i, old_used;
533
534 p = lookup_conn_list(array, nid);
535 if (p)
536 *p = -1; /* invalidate the old entry */
537
538 old_used = array->used;
539 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
540 goto error_add;
541 for (i = 0; i < len; i++)
542 if (!add_conn_list(array, list[i]))
543 goto error_add;
544 return 0;
545
546 error_add:
547 array->used = old_used;
548 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200550EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
551
552/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200553 * snd_hda_get_conn_index - get the connection index of the given NID
554 * @codec: the HDA codec
555 * @mux: NID containing the list
556 * @nid: NID to select
557 * @recursive: 1 when searching NID recursively, otherwise 0
558 *
559 * Parses the connection list of the widget @mux and checks whether the
560 * widget @nid is present. If it is, return the connection index.
561 * Otherwise it returns -1.
562 */
563int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
564 hda_nid_t nid, int recursive)
565{
566 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
567 int i, nums;
568
569 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
570 for (i = 0; i < nums; i++)
571 if (conn[i] == nid)
572 return i;
573 if (!recursive)
574 return -1;
575 if (recursive > 5) {
576 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
577 return -1;
578 }
579 recursive++;
580 for (i = 0; i < nums; i++)
581 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
582 return i;
583 return -1;
584}
585EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587/**
588 * snd_hda_queue_unsol_event - add an unsolicited event to queue
589 * @bus: the BUS
590 * @res: unsolicited event (lower 32bit of RIRB entry)
591 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
592 *
593 * Adds the given event to the queue. The events are processed in
594 * the workqueue asynchronously. Call this function in the interrupt
595 * hanlder when RIRB receives an unsolicited event.
596 *
597 * Returns 0 if successful, or a negative error code.
598 */
599int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
600{
601 struct hda_bus_unsolicited *unsol;
602 unsigned int wp;
603
Takashi Iwai0ba21762007-04-16 11:29:14 +0200604 unsol = bus->unsol;
605 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return 0;
607
608 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
609 unsol->wp = wp;
610
611 wp <<= 1;
612 unsol->queue[wp] = res;
613 unsol->queue[wp + 1] = res_ex;
614
Takashi Iwai6acaed32009-01-12 10:09:24 +0100615 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 return 0;
618}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100619EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800622 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
David Howellsc4028952006-11-22 14:57:56 +0000624static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
David Howellsc4028952006-11-22 14:57:56 +0000626 struct hda_bus_unsolicited *unsol =
627 container_of(work, struct hda_bus_unsolicited, work);
628 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct hda_codec *codec;
630 unsigned int rp, caddr, res;
631
632 while (unsol->rp != unsol->wp) {
633 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
634 unsol->rp = rp;
635 rp <<= 1;
636 res = unsol->queue[rp];
637 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200638 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 continue;
640 codec = bus->caddr_tbl[caddr & 0x0f];
641 if (codec && codec->patch_ops.unsol_event)
642 codec->patch_ops.unsol_event(codec, res);
643 }
644}
645
646/*
647 * initialize unsolicited queue
648 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200649static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 struct hda_bus_unsolicited *unsol;
652
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100653 if (bus->unsol) /* already initialized */
654 return 0;
655
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200656 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200657 if (!unsol) {
658 snd_printk(KERN_ERR "hda_codec: "
659 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -ENOMEM;
661 }
David Howellsc4028952006-11-22 14:57:56 +0000662 INIT_WORK(&unsol->work, process_unsol_events);
663 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 bus->unsol = unsol;
665 return 0;
666}
667
668/*
669 * destructor
670 */
671static void snd_hda_codec_free(struct hda_codec *codec);
672
673static int snd_hda_bus_free(struct hda_bus *bus)
674{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200675 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Takashi Iwai0ba21762007-04-16 11:29:14 +0200677 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100679 if (bus->workq)
680 flush_workqueue(bus->workq);
681 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200683 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 snd_hda_codec_free(codec);
685 }
686 if (bus->ops.private_free)
687 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100688 if (bus->workq)
689 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 kfree(bus);
691 return 0;
692}
693
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100694static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100697 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return snd_hda_bus_free(bus);
699}
700
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200701#ifdef CONFIG_SND_HDA_HWDEP
702static int snd_hda_bus_dev_register(struct snd_device *device)
703{
704 struct hda_bus *bus = device->device_data;
705 struct hda_codec *codec;
706 list_for_each_entry(codec, &bus->codec_list, list) {
707 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100708 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200709 }
710 return 0;
711}
712#else
713#define snd_hda_bus_dev_register NULL
714#endif
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/**
717 * snd_hda_bus_new - create a HDA bus
718 * @card: the card entry
719 * @temp: the template for hda_bus information
720 * @busp: the pointer to store the created bus instance
721 *
722 * Returns 0 if successful, or a negative error code.
723 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100724int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200725 const struct hda_bus_template *temp,
726 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct hda_bus *bus;
729 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100730 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200731 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 .dev_free = snd_hda_bus_dev_free,
733 };
734
Takashi Iwaida3cec32008-08-08 17:12:14 +0200735 if (snd_BUG_ON(!temp))
736 return -EINVAL;
737 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
738 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (busp)
741 *busp = NULL;
742
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200743 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (bus == NULL) {
745 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
746 return -ENOMEM;
747 }
748
749 bus->card = card;
750 bus->private_data = temp->private_data;
751 bus->pci = temp->pci;
752 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100753 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 bus->ops = temp->ops;
755
Ingo Molnar62932df2006-01-16 16:34:20 +0100756 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200757 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 INIT_LIST_HEAD(&bus->codec_list);
759
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100760 snprintf(bus->workq_name, sizeof(bus->workq_name),
761 "hd-audio%d", card->number);
762 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100763 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100764 snd_printk(KERN_ERR "cannot create workqueue %s\n",
765 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100766 kfree(bus);
767 return -ENOMEM;
768 }
769
Takashi Iwai0ba21762007-04-16 11:29:14 +0200770 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
771 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 snd_hda_bus_free(bus);
773 return err;
774 }
775 if (busp)
776 *busp = bus;
777 return 0;
778}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100779EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Takashi Iwai82467612007-07-27 19:15:54 +0200781#ifdef CONFIG_SND_HDA_GENERIC
782#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200783 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200784#else
785#define is_generic_config(codec) 0
786#endif
787
Takashi Iwai645f10c2008-11-28 15:07:37 +0100788#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100789#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
790#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100791#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100792#endif
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/*
795 * find a matching codec preset
796 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200797static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200798find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100800 struct hda_codec_preset_list *tbl;
801 const struct hda_codec_preset *preset;
802 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Takashi Iwai82467612007-07-27 19:15:54 +0200804 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100805 return NULL; /* use the generic parser */
806
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100807 again:
808 mutex_lock(&preset_mutex);
809 list_for_each_entry(tbl, &hda_preset_tables, list) {
810 if (!try_module_get(tbl->owner)) {
811 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
812 continue;
813 }
814 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100816 if (preset->afg && preset->afg != codec->afg)
817 continue;
818 if (preset->mfg && preset->mfg != codec->mfg)
819 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200820 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200822 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200823 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100824 preset->rev == codec->revision_id)) {
825 mutex_unlock(&preset_mutex);
826 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100830 module_put(tbl->owner);
831 }
832 mutex_unlock(&preset_mutex);
833
834 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
835 char name[32];
836 if (!mod_requested)
837 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
838 codec->vendor_id);
839 else
840 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
841 (codec->vendor_id >> 16) & 0xffff);
842 request_module(name);
843 mod_requested++;
844 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 return NULL;
847}
848
849/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200850 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200852static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 const struct hda_vendor_id *c;
855 const char *vendor = NULL;
856 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200857 char tmp[16];
858
859 if (codec->vendor_name)
860 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 for (c = hda_vendor_ids; c->id; c++) {
863 if (c->id == vendor_id) {
864 vendor = c->name;
865 break;
866 }
867 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200868 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 sprintf(tmp, "Generic %04x", vendor_id);
870 vendor = tmp;
871 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200872 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
873 if (!codec->vendor_name)
874 return -ENOMEM;
875
876 get_chip_name:
877 if (codec->chip_name)
878 return 0;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200881 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
882 else {
883 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
884 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
885 }
886 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200887 return -ENOMEM;
888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200892 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100894static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200896 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 hda_nid_t nid;
898
899 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
900 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200901 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200902 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200903 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200904 case AC_GRP_AUDIO_FUNCTION:
905 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200906 codec->afg_function_id = function_id & 0xff;
907 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200908 break;
909 case AC_GRP_MODEM_FUNCTION:
910 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200911 codec->mfg_function_id = function_id & 0xff;
912 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200913 break;
914 default:
915 break;
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100921 * read widget caps for each widget and store in cache
922 */
923static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
924{
925 int i;
926 hda_nid_t nid;
927
928 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
929 &codec->start_nid);
930 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200931 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100932 return -ENOMEM;
933 nid = codec->start_nid;
934 for (i = 0; i < codec->num_nodes; i++, nid++)
935 codec->wcaps[i] = snd_hda_param_read(codec, nid,
936 AC_PAR_AUDIO_WIDGET_CAP);
937 return 0;
938}
939
Takashi Iwai3be14142009-02-20 14:11:16 +0100940/* read all pin default configurations and save codec->init_pins */
941static int read_pin_defaults(struct hda_codec *codec)
942{
943 int i;
944 hda_nid_t nid = codec->start_nid;
945
946 for (i = 0; i < codec->num_nodes; i++, nid++) {
947 struct hda_pincfg *pin;
948 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200949 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100950 if (wid_type != AC_WID_PIN)
951 continue;
952 pin = snd_array_new(&codec->init_pins);
953 if (!pin)
954 return -ENOMEM;
955 pin->nid = nid;
956 pin->cfg = snd_hda_codec_read(codec, nid, 0,
957 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200958 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
959 AC_VERB_GET_PIN_WIDGET_CONTROL,
960 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100961 }
962 return 0;
963}
964
965/* look up the given pin config list and return the item matching with NID */
966static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
967 struct snd_array *array,
968 hda_nid_t nid)
969{
970 int i;
971 for (i = 0; i < array->used; i++) {
972 struct hda_pincfg *pin = snd_array_elem(array, i);
973 if (pin->nid == nid)
974 return pin;
975 }
976 return NULL;
977}
978
979/* write a config value for the given NID */
980static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
981 unsigned int cfg)
982{
983 int i;
984 for (i = 0; i < 4; i++) {
985 snd_hda_codec_write(codec, nid, 0,
986 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
987 cfg & 0xff);
988 cfg >>= 8;
989 }
990}
991
992/* set the current pin config value for the given NID.
993 * the value is cached, and read via snd_hda_codec_get_pincfg()
994 */
995int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
996 hda_nid_t nid, unsigned int cfg)
997{
998 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100999 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001000
Takashi Iwaib82855a2009-12-27 11:24:56 +01001001 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1002 return -EINVAL;
1003
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001004 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001005 pin = look_up_pincfg(codec, list, nid);
1006 if (!pin) {
1007 pin = snd_array_new(list);
1008 if (!pin)
1009 return -ENOMEM;
1010 pin->nid = nid;
1011 }
1012 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001013
1014 /* change only when needed; e.g. if the pincfg is already present
1015 * in user_pins[], don't write it
1016 */
1017 cfg = snd_hda_codec_get_pincfg(codec, nid);
1018 if (oldcfg != cfg)
1019 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001020 return 0;
1021}
1022
Takashi Iwaid5191e52009-11-16 14:58:17 +01001023/**
1024 * snd_hda_codec_set_pincfg - Override a pin default configuration
1025 * @codec: the HDA codec
1026 * @nid: NID to set the pin config
1027 * @cfg: the pin default config value
1028 *
1029 * Override a pin default configuration value in the cache.
1030 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1031 * priority than the real hardware value.
1032 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001033int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1034 hda_nid_t nid, unsigned int cfg)
1035{
Takashi Iwai346ff702009-02-23 09:42:57 +01001036 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001037}
1038EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1039
Takashi Iwaid5191e52009-11-16 14:58:17 +01001040/**
1041 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1042 * @codec: the HDA codec
1043 * @nid: NID to get the pin config
1044 *
1045 * Get the current pin config value of the given pin NID.
1046 * If the pincfg value is cached or overridden via sysfs or driver,
1047 * returns the cached value.
1048 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001049unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1050{
1051 struct hda_pincfg *pin;
1052
Takashi Iwai3be14142009-02-20 14:11:16 +01001053#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001054 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001055 if (pin)
1056 return pin->cfg;
1057#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001058 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1059 if (pin)
1060 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001061 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1062 if (pin)
1063 return pin->cfg;
1064 return 0;
1065}
1066EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1067
1068/* restore all current pin configs */
1069static void restore_pincfgs(struct hda_codec *codec)
1070{
1071 int i;
1072 for (i = 0; i < codec->init_pins.used; i++) {
1073 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1074 set_pincfg(codec, pin->nid,
1075 snd_hda_codec_get_pincfg(codec, pin->nid));
1076 }
1077}
Takashi Iwai54d17402005-11-21 16:33:22 +01001078
Takashi Iwai92ee6162009-12-27 11:18:59 +01001079/**
1080 * snd_hda_shutup_pins - Shut up all pins
1081 * @codec: the HDA codec
1082 *
1083 * Clear all pin controls to shup up before suspend for avoiding click noise.
1084 * The controls aren't cached so that they can be resumed properly.
1085 */
1086void snd_hda_shutup_pins(struct hda_codec *codec)
1087{
1088 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001089 /* don't shut up pins when unloading the driver; otherwise it breaks
1090 * the default pin setup at the next load of the driver
1091 */
1092 if (codec->bus->shutdown)
1093 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001094 for (i = 0; i < codec->init_pins.used; i++) {
1095 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1096 /* use read here for syncing after issuing each verb */
1097 snd_hda_codec_read(codec, pin->nid, 0,
1098 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1099 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001100 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001101}
1102EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1103
Mike Waychison1c7276c2011-04-20 12:04:36 -07001104#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001105/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1106static void restore_shutup_pins(struct hda_codec *codec)
1107{
1108 int i;
1109 if (!codec->pins_shutup)
1110 return;
1111 if (codec->bus->shutdown)
1112 return;
1113 for (i = 0; i < codec->init_pins.used; i++) {
1114 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1115 snd_hda_codec_write(codec, pin->nid, 0,
1116 AC_VERB_SET_PIN_WIDGET_CONTROL,
1117 pin->ctrl);
1118 }
1119 codec->pins_shutup = 0;
1120}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001121#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001122
Takashi Iwai01751f52007-08-10 16:59:39 +02001123static void init_hda_cache(struct hda_cache_rec *cache,
1124 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001125static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001126
Takashi Iwai3be14142009-02-20 14:11:16 +01001127/* restore the initial pin cfgs and release all pincfg lists */
1128static void restore_init_pincfgs(struct hda_codec *codec)
1129{
Takashi Iwai346ff702009-02-23 09:42:57 +01001130 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001131 * so that only the values in init_pins are restored
1132 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001133 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001134#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001135 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001136#endif
1137 restore_pincfgs(codec);
1138 snd_array_free(&codec->init_pins);
1139}
1140
Takashi Iwai54d17402005-11-21 16:33:22 +01001141/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001142 * audio-converter setup caches
1143 */
1144struct hda_cvt_setup {
1145 hda_nid_t nid;
1146 u8 stream_tag;
1147 u8 channel_id;
1148 u16 format_id;
1149 unsigned char active; /* cvt is currently used */
1150 unsigned char dirty; /* setups should be cleared */
1151};
1152
1153/* get or create a cache entry for the given audio converter NID */
1154static struct hda_cvt_setup *
1155get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1156{
1157 struct hda_cvt_setup *p;
1158 int i;
1159
1160 for (i = 0; i < codec->cvt_setups.used; i++) {
1161 p = snd_array_elem(&codec->cvt_setups, i);
1162 if (p->nid == nid)
1163 return p;
1164 }
1165 p = snd_array_new(&codec->cvt_setups);
1166 if (p)
1167 p->nid = nid;
1168 return p;
1169}
1170
1171/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * codec destructor
1173 */
1174static void snd_hda_codec_free(struct hda_codec *codec)
1175{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001176 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001178 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001179#ifdef CONFIG_SND_HDA_POWER_SAVE
1180 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001181 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001182#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001184 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001185 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001186 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001187 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 codec->bus->caddr_tbl[codec->addr] = NULL;
1189 if (codec->patch_ops.free)
1190 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001191 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001192 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001193 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001194 kfree(codec->vendor_name);
1195 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001196 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001197 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 kfree(codec);
1199}
1200
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001201static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1202 unsigned int power_state);
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/**
1205 * snd_hda_codec_new - create a HDA codec
1206 * @bus: the bus to assign
1207 * @codec_addr: the codec address
1208 * @codecp: the pointer to store the generated codec
1209 *
1210 * Returns 0 if successful, or a negative error code.
1211 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001212int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1213 unsigned int codec_addr,
1214 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001217 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int err;
1219
Takashi Iwaida3cec32008-08-08 17:12:14 +02001220 if (snd_BUG_ON(!bus))
1221 return -EINVAL;
1222 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1223 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001226 snd_printk(KERN_ERR "hda_codec: "
1227 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -EBUSY;
1229 }
1230
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001231 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (codec == NULL) {
1233 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1234 return -ENOMEM;
1235 }
1236
1237 codec->bus = bus;
1238 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001239 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001240 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001241 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001242 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001243 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1244 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001245 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001246 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001247 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001248 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001249 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001250 if (codec->bus->modelname) {
1251 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1252 if (!codec->modelname) {
1253 snd_hda_codec_free(codec);
1254 return -ENODEV;
1255 }
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Takashi Iwaicb53c622007-08-10 17:21:45 +02001258#ifdef CONFIG_SND_HDA_POWER_SAVE
1259 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1260 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1261 * the caller has to power down appropriatley after initialization
1262 * phase.
1263 */
1264 hda_keep_power_on(codec);
1265#endif
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 list_add_tail(&codec->list, &bus->codec_list);
1268 bus->caddr_tbl[codec_addr] = codec;
1269
Takashi Iwai0ba21762007-04-16 11:29:14 +02001270 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1271 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001272 if (codec->vendor_id == -1)
1273 /* read again, hopefully the access method was corrected
1274 * in the last read...
1275 */
1276 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1277 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001278 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1279 AC_PAR_SUBSYSTEM_ID);
1280 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1281 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001283 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001284 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001285 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001286 err = -ENODEV;
1287 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
Takashi Iwai3be14142009-02-20 14:11:16 +01001290 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1291 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001292 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001293 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001294 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001295 err = read_pin_defaults(codec);
1296 if (err < 0)
1297 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001298
Takashi Iwai0ba21762007-04-16 11:29:14 +02001299 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001300 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001301 codec->subsystem_id =
1302 snd_hda_codec_read(codec, nid, 0,
1303 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001304 }
1305
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001306 /* power-up all before initialization */
1307 hda_set_power_state(codec,
1308 codec->afg ? codec->afg : codec->mfg,
1309 AC_PWRST_D0);
1310
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001311 snd_hda_codec_proc_new(codec);
1312
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001313 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001314
1315 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1316 codec->subsystem_id, codec->revision_id);
1317 snd_component_add(codec->bus->card, component);
1318
1319 if (codecp)
1320 *codecp = codec;
1321 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001322
1323 error:
1324 snd_hda_codec_free(codec);
1325 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001326}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001327EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328
Takashi Iwaid5191e52009-11-16 14:58:17 +01001329/**
1330 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1331 * @codec: the HDA codec
1332 *
1333 * Start parsing of the given codec tree and (re-)initialize the whole
1334 * patch instance.
1335 *
1336 * Returns 0 if successful or a negative error code.
1337 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001338int snd_hda_codec_configure(struct hda_codec *codec)
1339{
1340 int err;
1341
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001342 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001343 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001344 err = get_codec_name(codec);
1345 if (err < 0)
1346 return err;
1347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Takashi Iwai82467612007-07-27 19:15:54 +02001349 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001351 goto patched;
1352 }
Takashi Iwai82467612007-07-27 19:15:54 +02001353 if (codec->preset && codec->preset->patch) {
1354 err = codec->preset->patch(codec);
1355 goto patched;
1356 }
1357
1358 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001359 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001360 if (err < 0)
1361 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001362
1363 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001364 if (!err && codec->patch_ops.unsol_event)
1365 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001366 /* audio codec should override the mixer name */
1367 if (!err && (codec->afg || !*codec->bus->card->mixername))
1368 snprintf(codec->bus->card->mixername,
1369 sizeof(codec->bus->card->mixername),
1370 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001371 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001373EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375/**
1376 * snd_hda_codec_setup_stream - set up the codec for streaming
1377 * @codec: the CODEC to set up
1378 * @nid: the NID to set up
1379 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1380 * @channel_id: channel id to pass, zero based.
1381 * @format: stream format.
1382 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001383void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1384 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 int channel_id, int format)
1386{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001387 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001388 struct hda_cvt_setup *p;
1389 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001390 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001391 int i;
1392
Takashi Iwai0ba21762007-04-16 11:29:14 +02001393 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001394 return;
1395
Takashi Iwai0ba21762007-04-16 11:29:14 +02001396 snd_printdd("hda_codec_setup_stream: "
1397 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001399 p = get_hda_cvt_setup(codec, nid);
1400 if (!p)
1401 return;
1402 /* update the stream-id if changed */
1403 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1404 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1405 newval = (stream_tag << 4) | channel_id;
1406 if (oldval != newval)
1407 snd_hda_codec_write(codec, nid, 0,
1408 AC_VERB_SET_CHANNEL_STREAMID,
1409 newval);
1410 p->stream_tag = stream_tag;
1411 p->channel_id = channel_id;
1412 }
1413 /* update the format-id if changed */
1414 if (p->format_id != format) {
1415 oldval = snd_hda_codec_read(codec, nid, 0,
1416 AC_VERB_GET_STREAM_FORMAT, 0);
1417 if (oldval != format) {
1418 msleep(1);
1419 snd_hda_codec_write(codec, nid, 0,
1420 AC_VERB_SET_STREAM_FORMAT,
1421 format);
1422 }
1423 p->format_id = format;
1424 }
1425 p->active = 1;
1426 p->dirty = 0;
1427
1428 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001429 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001430 list_for_each_entry(c, &codec->bus->codec_list, list) {
1431 for (i = 0; i < c->cvt_setups.used; i++) {
1432 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001433 if (!p->active && p->stream_tag == stream_tag &&
1434 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001435 p->dirty = 1;
1436 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001439EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Takashi Iwaif0cea792010-08-13 11:56:53 +02001441static void really_cleanup_stream(struct hda_codec *codec,
1442 struct hda_cvt_setup *q);
1443
Takashi Iwaid5191e52009-11-16 14:58:17 +01001444/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001445 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001446 * @codec: the CODEC to clean up
1447 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001448 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001449 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001450void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1451 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001452{
Takashi Iwaieb541332010-08-06 13:48:11 +02001453 struct hda_cvt_setup *p;
1454
Takashi Iwai888afa12008-03-18 09:57:50 +01001455 if (!nid)
1456 return;
1457
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001458 if (codec->no_sticky_stream)
1459 do_now = 1;
1460
Takashi Iwai888afa12008-03-18 09:57:50 +01001461 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001462 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001463 if (p) {
1464 /* here we just clear the active flag when do_now isn't set;
1465 * actual clean-ups will be done later in
1466 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1467 */
1468 if (do_now)
1469 really_cleanup_stream(codec, p);
1470 else
1471 p->active = 0;
1472 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001473}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001474EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001475
Takashi Iwaieb541332010-08-06 13:48:11 +02001476static void really_cleanup_stream(struct hda_codec *codec,
1477 struct hda_cvt_setup *q)
1478{
1479 hda_nid_t nid = q->nid;
1480 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1481 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1482 memset(q, 0, sizeof(*q));
1483 q->nid = nid;
1484}
1485
1486/* clean up the all conflicting obsolete streams */
1487static void purify_inactive_streams(struct hda_codec *codec)
1488{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001489 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001490 int i;
1491
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001492 list_for_each_entry(c, &codec->bus->codec_list, list) {
1493 for (i = 0; i < c->cvt_setups.used; i++) {
1494 struct hda_cvt_setup *p;
1495 p = snd_array_elem(&c->cvt_setups, i);
1496 if (p->dirty)
1497 really_cleanup_stream(c, p);
1498 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001499 }
1500}
1501
Mike Waychison1c7276c2011-04-20 12:04:36 -07001502#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaieb541332010-08-06 13:48:11 +02001503/* clean up all streams; called from suspend */
1504static void hda_cleanup_all_streams(struct hda_codec *codec)
1505{
1506 int i;
1507
1508 for (i = 0; i < codec->cvt_setups.used; i++) {
1509 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1510 if (p->stream_tag)
1511 really_cleanup_stream(codec, p);
1512 }
1513}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001514#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/*
1517 * amp access functions
1518 */
1519
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001520/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001521#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001522#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001523#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1524#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001526#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001529static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001530 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Takashi Iwai01751f52007-08-10 16:59:39 +02001532 memset(cache, 0, sizeof(*cache));
1533 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001534 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001535}
1536
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001537static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001538{
Takashi Iwai603c4012008-07-30 15:01:44 +02001539 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001543static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Takashi Iwai01751f52007-08-10 16:59:39 +02001545 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1546 u16 cur = cache->hash[idx];
1547 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001550 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (info->key == key)
1552 return info;
1553 cur = info->next;
1554 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001555 return NULL;
1556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001558/* query the hash. allocate an entry if not found. */
1559static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1560 u32 key)
1561{
1562 struct hda_cache_head *info = get_hash(cache, key);
1563 if (!info) {
1564 u16 idx, cur;
1565 /* add a new hash entry */
1566 info = snd_array_new(&cache->buf);
1567 if (!info)
1568 return NULL;
1569 cur = snd_array_index(&cache->buf, info);
1570 info->key = key;
1571 info->val = 0;
1572 idx = key % (u16)ARRAY_SIZE(cache->hash);
1573 info->next = cache->hash[idx];
1574 cache->hash[idx] = cur;
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return info;
1577}
1578
Takashi Iwai01751f52007-08-10 16:59:39 +02001579/* query and allocate an amp hash entry */
1580static inline struct hda_amp_info *
1581get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1582{
1583 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1584}
1585
Takashi Iwaid5191e52009-11-16 14:58:17 +01001586/**
1587 * query_amp_caps - query AMP capabilities
1588 * @codec: the HD-auio codec
1589 * @nid: the NID to query
1590 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1591 *
1592 * Query AMP capabilities for the given widget and direction.
1593 * Returns the obtained capability bits.
1594 *
1595 * When cap bits have been already read, this doesn't read again but
1596 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001598u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001600 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Takashi Iwai0ba21762007-04-16 11:29:14 +02001602 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1603 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001605 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001606 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001608 info->amp_caps = snd_hda_param_read(codec, nid,
1609 direction == HDA_OUTPUT ?
1610 AC_PAR_AMP_OUT_CAP :
1611 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001612 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001613 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615 return info->amp_caps;
1616}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001617EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Takashi Iwaid5191e52009-11-16 14:58:17 +01001619/**
1620 * snd_hda_override_amp_caps - Override the AMP capabilities
1621 * @codec: the CODEC to clean up
1622 * @nid: the NID to clean up
1623 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1624 * @caps: the capability bits to set
1625 *
1626 * Override the cached AMP caps bits value by the given one.
1627 * This function is useful if the driver needs to adjust the AMP ranges,
1628 * e.g. limit to 0dB, etc.
1629 *
1630 * Returns zero if successful or a negative error code.
1631 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001632int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1633 unsigned int caps)
1634{
1635 struct hda_amp_info *info;
1636
1637 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1638 if (!info)
1639 return -EINVAL;
1640 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001641 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001642 return 0;
1643}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001644EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001645
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001646static unsigned int
1647query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1648 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001649{
1650 struct hda_amp_info *info;
1651
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001652 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001653 if (!info)
1654 return 0;
1655 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001656 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001657 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001658 }
1659 return info->amp_caps;
1660}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001661
1662static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1663{
1664 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1665}
1666
Takashi Iwaid5191e52009-11-16 14:58:17 +01001667/**
1668 * snd_hda_query_pin_caps - Query PIN capabilities
1669 * @codec: the HD-auio codec
1670 * @nid: the NID to query
1671 *
1672 * Query PIN capabilities for the given widget.
1673 * Returns the obtained capability bits.
1674 *
1675 * When cap bits have been already read, this doesn't read again but
1676 * returns the cached value.
1677 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001678u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1679{
1680 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1681 read_pin_cap);
1682}
Takashi Iwai1327a322009-03-23 13:07:47 +01001683EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1684
Wu Fengguang864f92b2009-11-18 12:38:02 +08001685/**
1686 * snd_hda_pin_sense - execute pin sense measurement
1687 * @codec: the CODEC to sense
1688 * @nid: the pin NID to sense
1689 *
1690 * Execute necessary pin sense measurement and return its Presence Detect,
1691 * Impedance, ELD Valid etc. status bits.
1692 */
1693u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1694{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001695 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001696
Takashi Iwai729d55b2009-12-25 22:49:01 +01001697 if (!codec->no_trigger_sense) {
1698 pincap = snd_hda_query_pin_caps(codec, nid);
1699 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001700 snd_hda_codec_read(codec, nid, 0,
1701 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001702 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001703 return snd_hda_codec_read(codec, nid, 0,
1704 AC_VERB_GET_PIN_SENSE, 0);
1705}
1706EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1707
1708/**
1709 * snd_hda_jack_detect - query pin Presence Detect status
1710 * @codec: the CODEC to sense
1711 * @nid: the pin NID to sense
1712 *
1713 * Query and return the pin's Presence Detect status.
1714 */
1715int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1716{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001717 u32 sense = snd_hda_pin_sense(codec, nid);
1718 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001719}
1720EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722/*
1723 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001724 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001726static unsigned int get_vol_mute(struct hda_codec *codec,
1727 struct hda_amp_info *info, hda_nid_t nid,
1728 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 u32 val, parm;
1731
Takashi Iwai01751f52007-08-10 16:59:39 +02001732 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001733 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1736 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1737 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001738 val = snd_hda_codec_read(codec, nid, 0,
1739 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001741 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001742 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
1745/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001746 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001748static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001749 hda_nid_t nid, int ch, int direction, int index,
1750 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
1752 u32 parm;
1753
1754 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1755 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1756 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1757 parm |= val;
1758 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001759 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Takashi Iwaid5191e52009-11-16 14:58:17 +01001762/**
1763 * snd_hda_codec_amp_read - Read AMP value
1764 * @codec: HD-audio codec
1765 * @nid: NID to read the AMP value
1766 * @ch: channel (left=0 or right=1)
1767 * @direction: #HDA_INPUT or #HDA_OUTPUT
1768 * @index: the index value (only for input direction)
1769 *
1770 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 */
Takashi Iwai834be882006-03-01 14:16:17 +01001772int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1773 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001775 struct hda_amp_info *info;
1776 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1777 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001779 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001781EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Takashi Iwaid5191e52009-11-16 14:58:17 +01001783/**
1784 * snd_hda_codec_amp_update - update the AMP value
1785 * @codec: HD-audio codec
1786 * @nid: NID to read the AMP value
1787 * @ch: channel (left=0 or right=1)
1788 * @direction: #HDA_INPUT or #HDA_OUTPUT
1789 * @idx: the index value (only for input direction)
1790 * @mask: bit mask to set
1791 * @val: the bits value to set
1792 *
1793 * Update the AMP value with a bit mask.
1794 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001795 */
Takashi Iwai834be882006-03-01 14:16:17 +01001796int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1797 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001799 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001800
Takashi Iwai0ba21762007-04-16 11:29:14 +02001801 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1802 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001804 if (snd_BUG_ON(mask & ~0xff))
1805 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001806 val &= mask;
1807 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001808 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001810 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return 1;
1812}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001813EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Takashi Iwaid5191e52009-11-16 14:58:17 +01001815/**
1816 * snd_hda_codec_amp_stereo - update the AMP stereo values
1817 * @codec: HD-audio codec
1818 * @nid: NID to read the AMP value
1819 * @direction: #HDA_INPUT or #HDA_OUTPUT
1820 * @idx: the index value (only for input direction)
1821 * @mask: bit mask to set
1822 * @val: the bits value to set
1823 *
1824 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1825 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001826 */
1827int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1828 int direction, int idx, int mask, int val)
1829{
1830 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001831
1832 if (snd_BUG_ON(mask & ~0xff))
1833 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001834 for (ch = 0; ch < 2; ch++)
1835 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1836 idx, mask, val);
1837 return ret;
1838}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001839EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001840
Takashi Iwaicb53c622007-08-10 17:21:45 +02001841#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaid5191e52009-11-16 14:58:17 +01001842/**
1843 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1844 * @codec: HD-audio codec
1845 *
1846 * Resume the all amp commands from the cache.
1847 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001848void snd_hda_codec_resume_amp(struct hda_codec *codec)
1849{
Takashi Iwai603c4012008-07-30 15:01:44 +02001850 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001851 int i;
1852
Takashi Iwai603c4012008-07-30 15:01:44 +02001853 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001854 u32 key = buffer->head.key;
1855 hda_nid_t nid;
1856 unsigned int idx, dir, ch;
1857 if (!key)
1858 continue;
1859 nid = key & 0xff;
1860 idx = (key >> 16) & 0xff;
1861 dir = (key >> 24) & 0xff;
1862 for (ch = 0; ch < 2; ch++) {
1863 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1864 continue;
1865 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1866 buffer->vol[ch]);
1867 }
1868 }
1869}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001870EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001871#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001873static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1874 unsigned int ofs)
1875{
1876 u32 caps = query_amp_caps(codec, nid, dir);
1877 /* get num steps */
1878 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1879 if (ofs < caps)
1880 caps -= ofs;
1881 return caps;
1882}
1883
Takashi Iwaid5191e52009-11-16 14:58:17 +01001884/**
1885 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1886 *
1887 * The control element is supposed to have the private_value field
1888 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1889 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001890int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1891 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1894 u16 nid = get_amp_nid(kcontrol);
1895 u8 chs = get_amp_channels(kcontrol);
1896 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001897 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001899 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1900 uinfo->count = chs == 3 ? 2 : 1;
1901 uinfo->value.integer.min = 0;
1902 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1903 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001904 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001905 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1906 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return -EINVAL;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return 0;
1910}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001911EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001913
1914static inline unsigned int
1915read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1916 int ch, int dir, int idx, unsigned int ofs)
1917{
1918 unsigned int val;
1919 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1920 val &= HDA_AMP_VOLMASK;
1921 if (val >= ofs)
1922 val -= ofs;
1923 else
1924 val = 0;
1925 return val;
1926}
1927
1928static inline int
1929update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1930 int ch, int dir, int idx, unsigned int ofs,
1931 unsigned int val)
1932{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001933 unsigned int maxval;
1934
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001935 if (val > 0)
1936 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001937 /* ofs = 0: raw max value */
1938 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001939 if (val > maxval)
1940 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001941 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1942 HDA_AMP_VOLMASK, val);
1943}
1944
Takashi Iwaid5191e52009-11-16 14:58:17 +01001945/**
1946 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1947 *
1948 * The control element is supposed to have the private_value field
1949 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1950 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001951int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1955 hda_nid_t nid = get_amp_nid(kcontrol);
1956 int chs = get_amp_channels(kcontrol);
1957 int dir = get_amp_direction(kcontrol);
1958 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001959 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 long *valp = ucontrol->value.integer.value;
1961
1962 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001963 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001965 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return 0;
1967}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001968EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Takashi Iwaid5191e52009-11-16 14:58:17 +01001970/**
1971 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1972 *
1973 * The control element is supposed to have the private_value field
1974 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1975 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001976int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1977 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1980 hda_nid_t nid = get_amp_nid(kcontrol);
1981 int chs = get_amp_channels(kcontrol);
1982 int dir = get_amp_direction(kcontrol);
1983 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001984 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 long *valp = ucontrol->value.integer.value;
1986 int change = 0;
1987
Takashi Iwaicb53c622007-08-10 17:21:45 +02001988 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001989 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001990 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001991 valp++;
1992 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001993 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001994 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001995 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return change;
1997}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001998EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Takashi Iwaid5191e52009-11-16 14:58:17 +01002000/**
2001 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2002 *
2003 * The control element is supposed to have the private_value field
2004 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2005 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002006int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2007 unsigned int size, unsigned int __user *_tlv)
2008{
2009 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2010 hda_nid_t nid = get_amp_nid(kcontrol);
2011 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002012 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002013 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002014 u32 caps, val1, val2;
2015
2016 if (size < 4 * sizeof(unsigned int))
2017 return -ENOMEM;
2018 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002019 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2020 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002021 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002022 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002023 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002024 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02002025 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002026 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2027 return -EFAULT;
2028 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2029 return -EFAULT;
2030 if (put_user(val1, _tlv + 2))
2031 return -EFAULT;
2032 if (put_user(val2, _tlv + 3))
2033 return -EFAULT;
2034 return 0;
2035}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002036EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002037
Takashi Iwaid5191e52009-11-16 14:58:17 +01002038/**
2039 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2040 * @codec: HD-audio codec
2041 * @nid: NID of a reference widget
2042 * @dir: #HDA_INPUT or #HDA_OUTPUT
2043 * @tlv: TLV data to be stored, at least 4 elements
2044 *
2045 * Set (static) TLV data for a virtual master volume using the AMP caps
2046 * obtained from the reference NID.
2047 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002048 */
2049void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2050 unsigned int *tlv)
2051{
2052 u32 caps;
2053 int nums, step;
2054
2055 caps = query_amp_caps(codec, nid, dir);
2056 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2057 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2058 step = (step + 1) * 25;
2059 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2060 tlv[1] = 2 * sizeof(unsigned int);
2061 tlv[2] = -nums * step;
2062 tlv[3] = step;
2063}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002064EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002065
2066/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002067static struct snd_kcontrol *
2068_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2069 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002070{
2071 struct snd_ctl_elem_id id;
2072 memset(&id, 0, sizeof(id));
2073 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002074 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002075 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2076 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002077 strcpy(id.name, name);
2078 return snd_ctl_find_id(codec->bus->card, &id);
2079}
2080
Takashi Iwaid5191e52009-11-16 14:58:17 +01002081/**
2082 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2083 * @codec: HD-audio codec
2084 * @name: ctl id name string
2085 *
2086 * Get the control element with the given id string and IFACE_MIXER.
2087 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002088struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2089 const char *name)
2090{
2091 return _snd_hda_find_mixer_ctl(codec, name, 0);
2092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002093EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002094
Takashi Iwai1afe2062010-12-23 10:17:52 +01002095static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2096{
2097 int idx;
2098 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2099 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2100 return idx;
2101 }
2102 return -EBUSY;
2103}
2104
Takashi Iwaid5191e52009-11-16 14:58:17 +01002105/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002106 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002107 * @codec: HD-audio codec
2108 * @nid: corresponding NID (optional)
2109 * @kctl: the control element to assign
2110 *
2111 * Add the given control element to an array inside the codec instance.
2112 * All control elements belonging to a codec are supposed to be added
2113 * by this function so that a proper clean-up works at the free or
2114 * reconfiguration time.
2115 *
2116 * If non-zero @nid is passed, the NID is assigned to the control element.
2117 * The assignment is shown in the codec proc file.
2118 *
2119 * snd_hda_ctl_add() checks the control subdev id field whether
2120 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002121 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2122 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002123 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002124int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2125 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002126{
2127 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002128 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002129 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002130
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002131 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002132 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002133 if (nid == 0)
2134 nid = get_amp_nid_(kctl->private_value);
2135 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002136 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2137 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002138 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002139 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002140 err = snd_ctl_add(codec->bus->card, kctl);
2141 if (err < 0)
2142 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002143 item = snd_array_new(&codec->mixers);
2144 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002145 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002146 item->kctl = kctl;
2147 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002148 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002149 return 0;
2150}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002151EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002152
Takashi Iwaid5191e52009-11-16 14:58:17 +01002153/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002154 * snd_hda_add_nid - Assign a NID to a control element
2155 * @codec: HD-audio codec
2156 * @nid: corresponding NID (optional)
2157 * @kctl: the control element to assign
2158 * @index: index to kctl
2159 *
2160 * Add the given control element to an array inside the codec instance.
2161 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2162 * NID:KCTL mapping - for example "Capture Source" selector.
2163 */
2164int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2165 unsigned int index, hda_nid_t nid)
2166{
2167 struct hda_nid_item *item;
2168
2169 if (nid > 0) {
2170 item = snd_array_new(&codec->nids);
2171 if (!item)
2172 return -ENOMEM;
2173 item->kctl = kctl;
2174 item->index = index;
2175 item->nid = nid;
2176 return 0;
2177 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002178 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2179 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002180 return -EINVAL;
2181}
2182EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2183
2184/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002185 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2186 * @codec: HD-audio codec
2187 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002188void snd_hda_ctls_clear(struct hda_codec *codec)
2189{
2190 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002191 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002192 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002193 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002194 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002195 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002196}
2197
Takashi Iwaia65d6292009-02-23 16:57:04 +01002198/* pseudo device locking
2199 * toggle card->shutdown to allow/disallow the device access (as a hack)
2200 */
2201static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002202{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002203 spin_lock(&card->files_lock);
2204 if (card->shutdown) {
2205 spin_unlock(&card->files_lock);
2206 return -EINVAL;
2207 }
2208 card->shutdown = 1;
2209 spin_unlock(&card->files_lock);
2210 return 0;
2211}
2212
2213static void hda_unlock_devices(struct snd_card *card)
2214{
2215 spin_lock(&card->files_lock);
2216 card->shutdown = 0;
2217 spin_unlock(&card->files_lock);
2218}
2219
Takashi Iwaid5191e52009-11-16 14:58:17 +01002220/**
2221 * snd_hda_codec_reset - Clear all objects assigned to the codec
2222 * @codec: HD-audio codec
2223 *
2224 * This frees the all PCM and control elements assigned to the codec, and
2225 * clears the caches and restores the pin default configurations.
2226 *
2227 * When a device is being used, it returns -EBSY. If successfully freed,
2228 * returns zero.
2229 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002230int snd_hda_codec_reset(struct hda_codec *codec)
2231{
2232 struct snd_card *card = codec->bus->card;
2233 int i, pcm;
2234
2235 if (hda_lock_devices(card) < 0)
2236 return -EBUSY;
2237 /* check whether the codec isn't used by any mixer or PCM streams */
2238 if (!list_empty(&card->ctl_files)) {
2239 hda_unlock_devices(card);
2240 return -EBUSY;
2241 }
2242 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2243 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2244 if (!cpcm->pcm)
2245 continue;
2246 if (cpcm->pcm->streams[0].substream_opened ||
2247 cpcm->pcm->streams[1].substream_opened) {
2248 hda_unlock_devices(card);
2249 return -EBUSY;
2250 }
2251 }
2252
2253 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002254
2255#ifdef CONFIG_SND_HDA_POWER_SAVE
2256 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002257 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002258#endif
2259 snd_hda_ctls_clear(codec);
2260 /* relase PCMs */
2261 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002262 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002263 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002264 clear_bit(codec->pcm_info[i].device,
2265 codec->bus->pcm_dev_bits);
2266 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002267 }
2268 if (codec->patch_ops.free)
2269 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002270 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002271 codec->spec = NULL;
2272 free_hda_cache(&codec->amp_cache);
2273 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002274 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2275 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002276 /* free only driver_pins so that init_pins + user_pins are restored */
2277 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002278 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002279 codec->num_pcms = 0;
2280 codec->pcm_info = NULL;
2281 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002282 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2283 codec->slave_dig_outs = NULL;
2284 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002285 module_put(codec->owner);
2286 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002287
2288 /* allow device access again */
2289 hda_unlock_devices(card);
2290 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002291}
2292
Takashi Iwaid5191e52009-11-16 14:58:17 +01002293/**
2294 * snd_hda_add_vmaster - create a virtual master control and add slaves
2295 * @codec: HD-audio codec
2296 * @name: vmaster control name
2297 * @tlv: TLV data (optional)
2298 * @slaves: slave control names (optional)
2299 *
2300 * Create a virtual master control with the given name. The TLV data
2301 * must be either NULL or a valid data.
2302 *
2303 * @slaves is a NULL-terminated array of strings, each of which is a
2304 * slave control name. All controls with these names are assigned to
2305 * the new virtual master control.
2306 *
2307 * This function returns zero if successful or a negative error code.
2308 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002309int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002310 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002311{
2312 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002313 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002314 int err;
2315
Takashi Iwai2f085542008-02-22 18:43:50 +01002316 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2317 ;
2318 if (!*s) {
2319 snd_printdd("No slave found for %s\n", name);
2320 return 0;
2321 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002322 kctl = snd_ctl_make_virtual_master(name, tlv);
2323 if (!kctl)
2324 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002325 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002326 if (err < 0)
2327 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002328
Takashi Iwai2134ea42008-01-10 16:53:55 +01002329 for (s = slaves; *s; s++) {
2330 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002331 int i = 0;
2332 for (;;) {
2333 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2334 if (!sctl) {
2335 if (!i)
2336 snd_printdd("Cannot find slave %s, "
2337 "skipped\n", *s);
2338 break;
2339 }
2340 err = snd_ctl_add_slave(kctl, sctl);
2341 if (err < 0)
2342 return err;
2343 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002344 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002345 }
2346 return 0;
2347}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002348EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002349
Takashi Iwaid5191e52009-11-16 14:58:17 +01002350/**
2351 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2352 *
2353 * The control element is supposed to have the private_value field
2354 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2355 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002356int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 int chs = get_amp_channels(kcontrol);
2360
2361 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2362 uinfo->count = chs == 3 ? 2 : 1;
2363 uinfo->value.integer.min = 0;
2364 uinfo->value.integer.max = 1;
2365 return 0;
2366}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002367EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Takashi Iwaid5191e52009-11-16 14:58:17 +01002369/**
2370 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2371 *
2372 * The control element is supposed to have the private_value field
2373 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2374 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002375int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2376 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
2378 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2379 hda_nid_t nid = get_amp_nid(kcontrol);
2380 int chs = get_amp_channels(kcontrol);
2381 int dir = get_amp_direction(kcontrol);
2382 int idx = get_amp_index(kcontrol);
2383 long *valp = ucontrol->value.integer.value;
2384
2385 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002386 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002387 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002389 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002390 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return 0;
2392}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002393EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Takashi Iwaid5191e52009-11-16 14:58:17 +01002395/**
2396 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2397 *
2398 * The control element is supposed to have the private_value field
2399 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2400 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002401int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
2404 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2405 hda_nid_t nid = get_amp_nid(kcontrol);
2406 int chs = get_amp_channels(kcontrol);
2407 int dir = get_amp_direction(kcontrol);
2408 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 long *valp = ucontrol->value.integer.value;
2410 int change = 0;
2411
Takashi Iwaicb53c622007-08-10 17:21:45 +02002412 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002413 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002414 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002415 HDA_AMP_MUTE,
2416 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002417 valp++;
2418 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002419 if (chs & 2)
2420 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002421 HDA_AMP_MUTE,
2422 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002423 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002424 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return change;
2426}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002427EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Takashi Iwai67d634c2009-11-16 15:35:59 +01002429#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002430/**
2431 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2432 *
2433 * This function calls snd_hda_enable_beep_device(), which behaves differently
2434 * depending on beep_mode option.
2435 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002436int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2437 struct snd_ctl_elem_value *ucontrol)
2438{
2439 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2440 long *valp = ucontrol->value.integer.value;
2441
2442 snd_hda_enable_beep_device(codec, *valp);
2443 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2444}
2445EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002446#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448/*
Takashi Iwai985be542005-11-02 18:26:49 +01002449 * bound volume controls
2450 *
2451 * bind multiple volumes (# indices, from 0)
2452 */
2453
2454#define AMP_VAL_IDX_SHIFT 19
2455#define AMP_VAL_IDX_MASK (0x0f<<19)
2456
Takashi Iwaid5191e52009-11-16 14:58:17 +01002457/**
2458 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2459 *
2460 * The control element is supposed to have the private_value field
2461 * set up via HDA_BIND_MUTE*() macros.
2462 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002463int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002465{
2466 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2467 unsigned long pval;
2468 int err;
2469
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002470 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002471 pval = kcontrol->private_value;
2472 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2473 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2474 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002475 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002476 return err;
2477}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002478EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002479
Takashi Iwaid5191e52009-11-16 14:58:17 +01002480/**
2481 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2482 *
2483 * The control element is supposed to have the private_value field
2484 * set up via HDA_BIND_MUTE*() macros.
2485 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002486int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2487 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002488{
2489 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2490 unsigned long pval;
2491 int i, indices, err = 0, change = 0;
2492
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002493 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002494 pval = kcontrol->private_value;
2495 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2496 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002497 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2498 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002499 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2500 if (err < 0)
2501 break;
2502 change |= err;
2503 }
2504 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002505 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002506 return err < 0 ? err : change;
2507}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002508EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002509
Takashi Iwaid5191e52009-11-16 14:58:17 +01002510/**
2511 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2512 *
2513 * The control element is supposed to have the private_value field
2514 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002515 */
2516int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2517 struct snd_ctl_elem_info *uinfo)
2518{
2519 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2520 struct hda_bind_ctls *c;
2521 int err;
2522
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002523 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002524 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002525 kcontrol->private_value = *c->values;
2526 err = c->ops->info(kcontrol, uinfo);
2527 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002528 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002529 return err;
2530}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002531EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002532
Takashi Iwaid5191e52009-11-16 14:58:17 +01002533/**
2534 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2535 *
2536 * The control element is supposed to have the private_value field
2537 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2538 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002539int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_value *ucontrol)
2541{
2542 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2543 struct hda_bind_ctls *c;
2544 int err;
2545
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002546 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002547 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002548 kcontrol->private_value = *c->values;
2549 err = c->ops->get(kcontrol, ucontrol);
2550 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002551 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002552 return err;
2553}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002554EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002555
Takashi Iwaid5191e52009-11-16 14:58:17 +01002556/**
2557 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2558 *
2559 * The control element is supposed to have the private_value field
2560 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2561 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002562int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2563 struct snd_ctl_elem_value *ucontrol)
2564{
2565 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2566 struct hda_bind_ctls *c;
2567 unsigned long *vals;
2568 int err = 0, change = 0;
2569
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002570 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002571 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002572 for (vals = c->values; *vals; vals++) {
2573 kcontrol->private_value = *vals;
2574 err = c->ops->put(kcontrol, ucontrol);
2575 if (err < 0)
2576 break;
2577 change |= err;
2578 }
2579 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002580 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002581 return err < 0 ? err : change;
2582}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002583EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002584
Takashi Iwaid5191e52009-11-16 14:58:17 +01002585/**
2586 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2587 *
2588 * The control element is supposed to have the private_value field
2589 * set up via HDA_BIND_VOL() macro.
2590 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002591int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2592 unsigned int size, unsigned int __user *tlv)
2593{
2594 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2595 struct hda_bind_ctls *c;
2596 int err;
2597
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002598 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002599 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002600 kcontrol->private_value = *c->values;
2601 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2602 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002603 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002604 return err;
2605}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002606EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002607
2608struct hda_ctl_ops snd_hda_bind_vol = {
2609 .info = snd_hda_mixer_amp_volume_info,
2610 .get = snd_hda_mixer_amp_volume_get,
2611 .put = snd_hda_mixer_amp_volume_put,
2612 .tlv = snd_hda_mixer_amp_tlv
2613};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002614EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002615
2616struct hda_ctl_ops snd_hda_bind_sw = {
2617 .info = snd_hda_mixer_amp_switch_info,
2618 .get = snd_hda_mixer_amp_switch_get,
2619 .put = snd_hda_mixer_amp_switch_put,
2620 .tlv = snd_hda_mixer_amp_tlv
2621};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002622EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002623
2624/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 * SPDIF out controls
2626 */
2627
Takashi Iwai0ba21762007-04-16 11:29:14 +02002628static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2632 uinfo->count = 1;
2633 return 0;
2634}
2635
Takashi Iwai0ba21762007-04-16 11:29:14 +02002636static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2637 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2640 IEC958_AES0_NONAUDIO |
2641 IEC958_AES0_CON_EMPHASIS_5015 |
2642 IEC958_AES0_CON_NOT_COPYRIGHT;
2643 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2644 IEC958_AES1_CON_ORIGINAL;
2645 return 0;
2646}
2647
Takashi Iwai0ba21762007-04-16 11:29:14 +02002648static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2649 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2652 IEC958_AES0_NONAUDIO |
2653 IEC958_AES0_PRO_EMPHASIS_5015;
2654 return 0;
2655}
2656
Takashi Iwai0ba21762007-04-16 11:29:14 +02002657static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002661 int idx = kcontrol->private_value;
2662 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Stephen Warren7c935972011-06-01 11:14:17 -06002664 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2665 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2666 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2667 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 return 0;
2670}
2671
2672/* convert from SPDIF status bits to HDA SPDIF bits
2673 * bit 0 (DigEn) is always set zero (to be filled later)
2674 */
2675static unsigned short convert_from_spdif_status(unsigned int sbits)
2676{
2677 unsigned short val = 0;
2678
2679 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002680 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002682 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002684 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2685 IEC958_AES0_PRO_EMPHASIS_5015)
2686 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002688 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2689 IEC958_AES0_CON_EMPHASIS_5015)
2690 val |= AC_DIG1_EMPHASIS;
2691 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2692 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002694 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2696 }
2697 return val;
2698}
2699
2700/* convert to SPDIF status bits from HDA SPDIF bits
2701 */
2702static unsigned int convert_to_spdif_status(unsigned short val)
2703{
2704 unsigned int sbits = 0;
2705
Takashi Iwai0ba21762007-04-16 11:29:14 +02002706 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002708 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 sbits |= IEC958_AES0_PROFESSIONAL;
2710 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002711 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2713 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002714 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002716 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002718 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2720 sbits |= val & (0x7f << 8);
2721 }
2722 return sbits;
2723}
2724
Takashi Iwai2f728532008-09-25 16:32:41 +02002725/* set digital convert verbs both for the given NID and its slaves */
2726static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2727 int verb, int val)
2728{
Takashi Iwaidda14412011-05-02 11:29:30 +02002729 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002730
Takashi Iwai9e976972008-11-25 08:17:20 +01002731 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002732 d = codec->slave_dig_outs;
2733 if (!d)
2734 return;
2735 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002736 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002737}
2738
2739static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2740 int dig1, int dig2)
2741{
2742 if (dig1 != -1)
2743 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2744 if (dig2 != -1)
2745 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2746}
2747
Takashi Iwai0ba21762007-04-16 11:29:14 +02002748static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2749 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
2751 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002752 int idx = kcontrol->private_value;
2753 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2754 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 unsigned short val;
2756 int change;
2757
Ingo Molnar62932df2006-01-16 16:34:20 +01002758 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002759 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2761 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2762 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002763 val = convert_from_spdif_status(spdif->status);
2764 val |= spdif->ctls & 1;
2765 change = spdif->ctls != val;
2766 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002767 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002768 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002769 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return change;
2771}
2772
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002773#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Takashi Iwai0ba21762007-04-16 11:29:14 +02002775static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2776 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
2778 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002779 int idx = kcontrol->private_value;
2780 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Stephen Warren7c935972011-06-01 11:14:17 -06002782 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return 0;
2784}
2785
Stephen Warren74b654c2011-06-01 11:14:18 -06002786static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2787 int dig1, int dig2)
2788{
2789 set_dig_out_convert(codec, nid, dig1, dig2);
2790 /* unmute amp switch (if any) */
2791 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2792 (dig1 & AC_DIG1_ENABLE))
2793 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2794 HDA_AMP_MUTE, 0);
2795}
2796
Takashi Iwai0ba21762007-04-16 11:29:14 +02002797static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002801 int idx = kcontrol->private_value;
2802 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2803 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 unsigned short val;
2805 int change;
2806
Ingo Molnar62932df2006-01-16 16:34:20 +01002807 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002808 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002810 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002811 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002812 spdif->ctls = val;
2813 if (change && nid != (u16)-1)
2814 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002815 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return change;
2817}
2818
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002819static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 {
2821 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2822 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002823 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 .info = snd_hda_spdif_mask_info,
2825 .get = snd_hda_spdif_cmask_get,
2826 },
2827 {
2828 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2829 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002830 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 .info = snd_hda_spdif_mask_info,
2832 .get = snd_hda_spdif_pmask_get,
2833 },
2834 {
2835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002836 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 .info = snd_hda_spdif_mask_info,
2838 .get = snd_hda_spdif_default_get,
2839 .put = snd_hda_spdif_default_put,
2840 },
2841 {
2842 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002843 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 .info = snd_hda_spdif_out_switch_info,
2845 .get = snd_hda_spdif_out_switch_get,
2846 .put = snd_hda_spdif_out_switch_put,
2847 },
2848 { } /* end */
2849};
2850
2851/**
2852 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2853 * @codec: the HDA codec
2854 * @nid: audio out widget NID
2855 *
2856 * Creates controls related with the SPDIF output.
2857 * Called from each patch supporting the SPDIF out.
2858 *
2859 * Returns 0 if successful, or a negative error code.
2860 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002861int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2862 hda_nid_t associated_nid,
2863 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002866 struct snd_kcontrol *kctl;
2867 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002868 int idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002869 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Takashi Iwai1afe2062010-12-23 10:17:52 +01002871 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2872 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002873 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2874 return -EBUSY;
2875 }
Stephen Warren7c935972011-06-01 11:14:17 -06002876 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2878 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002879 if (!kctl)
2880 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002881 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002882 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002883 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002884 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return err;
2886 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002887 spdif->nid = cvt_nid;
2888 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06002889 AC_VERB_GET_DIGI_CONVERT_1, 0);
2890 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return 0;
2892}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002893EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Stephen Warren7c935972011-06-01 11:14:17 -06002895struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2896 hda_nid_t nid)
2897{
2898 int i;
2899 for (i = 0; i < codec->spdif_out.used; i++) {
2900 struct hda_spdif_out *spdif =
2901 snd_array_elem(&codec->spdif_out, i);
2902 if (spdif->nid == nid)
2903 return spdif;
2904 }
2905 return NULL;
2906}
2907EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2908
Stephen Warren74b654c2011-06-01 11:14:18 -06002909void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2910{
2911 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2912
2913 mutex_lock(&codec->spdif_mutex);
2914 spdif->nid = (u16)-1;
2915 mutex_unlock(&codec->spdif_mutex);
2916}
2917EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2918
2919void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2920{
2921 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2922 unsigned short val;
2923
2924 mutex_lock(&codec->spdif_mutex);
2925 if (spdif->nid != nid) {
2926 spdif->nid = nid;
2927 val = spdif->ctls;
2928 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2929 }
2930 mutex_unlock(&codec->spdif_mutex);
2931}
2932EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002935 * SPDIF sharing with analog output
2936 */
2937static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2938 struct snd_ctl_elem_value *ucontrol)
2939{
2940 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2941 ucontrol->value.integer.value[0] = mout->share_spdif;
2942 return 0;
2943}
2944
2945static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2946 struct snd_ctl_elem_value *ucontrol)
2947{
2948 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2949 mout->share_spdif = !!ucontrol->value.integer.value[0];
2950 return 0;
2951}
2952
2953static struct snd_kcontrol_new spdif_share_sw = {
2954 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2955 .name = "IEC958 Default PCM Playback Switch",
2956 .info = snd_ctl_boolean_mono_info,
2957 .get = spdif_share_sw_get,
2958 .put = spdif_share_sw_put,
2959};
2960
Takashi Iwaid5191e52009-11-16 14:58:17 +01002961/**
2962 * snd_hda_create_spdif_share_sw - create Default PCM switch
2963 * @codec: the HDA codec
2964 * @mout: multi-out instance
2965 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002966int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2967 struct hda_multi_out *mout)
2968{
2969 if (!mout->dig_out_nid)
2970 return 0;
2971 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002972 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2973 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002974}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002975EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002976
2977/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 * SPDIF input
2979 */
2980
2981#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2982
Takashi Iwai0ba21762007-04-16 11:29:14 +02002983static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2984 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2987
2988 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2989 return 0;
2990}
2991
Takashi Iwai0ba21762007-04-16 11:29:14 +02002992static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2993 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2996 hda_nid_t nid = kcontrol->private_value;
2997 unsigned int val = !!ucontrol->value.integer.value[0];
2998 int change;
2999
Ingo Molnar62932df2006-01-16 16:34:20 +01003000 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003002 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003004 snd_hda_codec_write_cache(codec, nid, 0,
3005 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003007 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return change;
3009}
3010
Takashi Iwai0ba21762007-04-16 11:29:14 +02003011static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3012 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
3014 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3015 hda_nid_t nid = kcontrol->private_value;
3016 unsigned short val;
3017 unsigned int sbits;
3018
Andrew Paprocki3982d172007-12-19 12:13:44 +01003019 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 sbits = convert_to_spdif_status(val);
3021 ucontrol->value.iec958.status[0] = sbits;
3022 ucontrol->value.iec958.status[1] = sbits >> 8;
3023 ucontrol->value.iec958.status[2] = sbits >> 16;
3024 ucontrol->value.iec958.status[3] = sbits >> 24;
3025 return 0;
3026}
3027
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003028static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 {
3030 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003031 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 .info = snd_hda_spdif_in_switch_info,
3033 .get = snd_hda_spdif_in_switch_get,
3034 .put = snd_hda_spdif_in_switch_put,
3035 },
3036 {
3037 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3038 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003039 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 .info = snd_hda_spdif_mask_info,
3041 .get = snd_hda_spdif_in_status_get,
3042 },
3043 { } /* end */
3044};
3045
3046/**
3047 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3048 * @codec: the HDA codec
3049 * @nid: audio in widget NID
3050 *
3051 * Creates controls related with the SPDIF input.
3052 * Called from each patch supporting the SPDIF in.
3053 *
3054 * Returns 0 if successful, or a negative error code.
3055 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003056int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003059 struct snd_kcontrol *kctl;
3060 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003061 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Takashi Iwai1afe2062010-12-23 10:17:52 +01003063 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3064 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003065 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3066 return -EBUSY;
3067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3069 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003070 if (!kctl)
3071 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003073 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003074 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return err;
3076 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003077 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003078 snd_hda_codec_read(codec, nid, 0,
3079 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003080 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return 0;
3082}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003083EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Takashi Iwaicb53c622007-08-10 17:21:45 +02003085#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003086/*
3087 * command cache
3088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003090/* build a 32bit cache key with the widget id and the command parameter */
3091#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3092#define get_cmd_cache_nid(key) ((key) & 0xff)
3093#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3094
3095/**
3096 * snd_hda_codec_write_cache - send a single command with caching
3097 * @codec: the HDA codec
3098 * @nid: NID to send the command
3099 * @direct: direct flag
3100 * @verb: the verb to send
3101 * @parm: the parameter for the verb
3102 *
3103 * Send a single command without waiting for response.
3104 *
3105 * Returns 0 if successful, or a negative error code.
3106 */
3107int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3108 int direct, unsigned int verb, unsigned int parm)
3109{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003110 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3111 struct hda_cache_head *c;
3112 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003113
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003114 if (err < 0)
3115 return err;
3116 /* parm may contain the verb stuff for get/set amp */
3117 verb = verb | (parm >> 8);
3118 parm &= 0xff;
3119 key = build_cmd_cache_key(nid, verb);
3120 mutex_lock(&codec->bus->cmd_mutex);
3121 c = get_alloc_hash(&codec->cmd_cache, key);
3122 if (c)
3123 c->val = parm;
3124 mutex_unlock(&codec->bus->cmd_mutex);
3125 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003126}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003127EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003128
Takashi Iwaid5191e52009-11-16 14:58:17 +01003129/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003130 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3131 * @codec: the HDA codec
3132 * @nid: NID to send the command
3133 * @direct: direct flag
3134 * @verb: the verb to send
3135 * @parm: the parameter for the verb
3136 *
3137 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3138 * command if the parameter is already identical with the cached value.
3139 * If not, it sends the command and refreshes the cache.
3140 *
3141 * Returns 0 if successful, or a negative error code.
3142 */
3143int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3144 int direct, unsigned int verb, unsigned int parm)
3145{
3146 struct hda_cache_head *c;
3147 u32 key;
3148
3149 /* parm may contain the verb stuff for get/set amp */
3150 verb = verb | (parm >> 8);
3151 parm &= 0xff;
3152 key = build_cmd_cache_key(nid, verb);
3153 mutex_lock(&codec->bus->cmd_mutex);
3154 c = get_hash(&codec->cmd_cache, key);
3155 if (c && c->val == parm) {
3156 mutex_unlock(&codec->bus->cmd_mutex);
3157 return 0;
3158 }
3159 mutex_unlock(&codec->bus->cmd_mutex);
3160 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3161}
3162EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3163
3164/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003165 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3166 * @codec: HD-audio codec
3167 *
3168 * Execute all verbs recorded in the command caches to resume.
3169 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003170void snd_hda_codec_resume_cache(struct hda_codec *codec)
3171{
Takashi Iwai603c4012008-07-30 15:01:44 +02003172 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003173 int i;
3174
Takashi Iwai603c4012008-07-30 15:01:44 +02003175 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003176 u32 key = buffer->key;
3177 if (!key)
3178 continue;
3179 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3180 get_cmd_cache_cmd(key), buffer->val);
3181 }
3182}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003183EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003184
3185/**
3186 * snd_hda_sequence_write_cache - sequence writes with caching
3187 * @codec: the HDA codec
3188 * @seq: VERB array to send
3189 *
3190 * Send the commands sequentially from the given array.
3191 * Thte commands are recorded on cache for power-save and resume.
3192 * The array must be terminated with NID=0.
3193 */
3194void snd_hda_sequence_write_cache(struct hda_codec *codec,
3195 const struct hda_verb *seq)
3196{
3197 for (; seq->nid; seq++)
3198 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3199 seq->param);
3200}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003201EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003202#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003203
Takashi Iwai54d17402005-11-21 16:33:22 +01003204/*
3205 * set power state of the codec
3206 */
3207static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3208 unsigned int power_state)
3209{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003210 hda_nid_t nid;
3211 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003212
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003213 /* this delay seems necessary to avoid click noise at power-down */
3214 if (power_state == AC_PWRST_D3)
3215 msleep(100);
3216 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003217 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003218 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003219 if (power_state == AC_PWRST_D0 &&
3220 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003221 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003222
Takashi Iwaicb53c622007-08-10 17:21:45 +02003223 nid = codec->start_nid;
3224 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003225 unsigned int wcaps = get_wcaps(codec, nid);
3226 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003227 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003228 if (power_state == AC_PWRST_D3 &&
3229 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003230 unsigned int pincap;
3231 /*
3232 * don't power down the widget if it controls
3233 * eapd and EAPD_BTLENABLE is set.
3234 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003235 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003236 if (pincap & AC_PINCAP_EAPD) {
3237 int eapd = snd_hda_codec_read(codec,
3238 nid, 0,
3239 AC_VERB_GET_EAPD_BTLENABLE, 0);
3240 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003241 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003242 continue;
3243 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003244 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003245 snd_hda_codec_write(codec, nid, 0,
3246 AC_VERB_SET_POWER_STATE,
3247 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003248 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003249 }
3250
Takashi Iwaicb53c622007-08-10 17:21:45 +02003251 if (power_state == AC_PWRST_D0) {
3252 unsigned long end_time;
3253 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003254 /* wait until the codec reachs to D0 */
3255 end_time = jiffies + msecs_to_jiffies(500);
3256 do {
3257 state = snd_hda_codec_read(codec, fg, 0,
3258 AC_VERB_GET_POWER_STATE, 0);
3259 if (state == power_state)
3260 break;
3261 msleep(1);
3262 } while (time_after_eq(end_time, jiffies));
3263 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003264}
3265
Takashi Iwai11aeff02008-07-30 15:01:46 +02003266#ifdef CONFIG_SND_HDA_HWDEP
3267/* execute additional init verbs */
3268static void hda_exec_init_verbs(struct hda_codec *codec)
3269{
3270 if (codec->init_verbs.list)
3271 snd_hda_sequence_write(codec, codec->init_verbs.list);
3272}
3273#else
3274static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3275#endif
3276
Takashi Iwaicb53c622007-08-10 17:21:45 +02003277#ifdef SND_HDA_NEEDS_RESUME
3278/*
3279 * call suspend and power-down; used both from PM and power-save
3280 */
3281static void hda_call_codec_suspend(struct hda_codec *codec)
3282{
3283 if (codec->patch_ops.suspend)
3284 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003285 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003286 hda_set_power_state(codec,
3287 codec->afg ? codec->afg : codec->mfg,
3288 AC_PWRST_D3);
3289#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003290 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003291 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003292 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003293 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003294 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003295#endif
3296}
3297
3298/*
3299 * kick up codec; used both from PM and power-save
3300 */
3301static void hda_call_codec_resume(struct hda_codec *codec)
3302{
3303 hda_set_power_state(codec,
3304 codec->afg ? codec->afg : codec->mfg,
3305 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003306 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003307 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003308 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003309 if (codec->patch_ops.resume)
3310 codec->patch_ops.resume(codec);
3311 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003312 if (codec->patch_ops.init)
3313 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003314 snd_hda_codec_resume_amp(codec);
3315 snd_hda_codec_resume_cache(codec);
3316 }
3317}
3318#endif /* SND_HDA_NEEDS_RESUME */
3319
Takashi Iwai54d17402005-11-21 16:33:22 +01003320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321/**
3322 * snd_hda_build_controls - build mixer controls
3323 * @bus: the BUS
3324 *
3325 * Creates mixer controls for each codec included in the bus.
3326 *
3327 * Returns 0 if successful, otherwise a negative error code.
3328 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003329int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003331 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Takashi Iwai0ba21762007-04-16 11:29:14 +02003333 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003334 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003335 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003336 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003337 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003338 err = snd_hda_codec_reset(codec);
3339 if (err < 0) {
3340 printk(KERN_ERR
3341 "hda_codec: cannot revert codec\n");
3342 return err;
3343 }
3344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003346 return 0;
3347}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003348EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003350int snd_hda_codec_build_controls(struct hda_codec *codec)
3351{
3352 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003353 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003354 /* continue to initialize... */
3355 if (codec->patch_ops.init)
3356 err = codec->patch_ops.init(codec);
3357 if (!err && codec->patch_ops.build_controls)
3358 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003359 if (err < 0)
3360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return 0;
3362}
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364/*
3365 * stream formats
3366 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003367struct hda_rate_tbl {
3368 unsigned int hz;
3369 unsigned int alsa_bits;
3370 unsigned int hda_fmt;
3371};
3372
Takashi Iwai92f10b32010-08-03 14:21:00 +02003373/* rate = base * mult / div */
3374#define HDA_RATE(base, mult, div) \
3375 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3376 (((div) - 1) << AC_FMT_DIV_SHIFT))
3377
Takashi Iwaibefdf312005-08-22 13:57:55 +02003378static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003380
3381 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003382 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3383 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3384 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3385 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3386 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3387 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3388 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3389 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3390 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3391 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3392 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003393#define AC_PAR_PCM_RATE_BITS 11
3394 /* up to bits 10, 384kHZ isn't supported properly */
3395
3396 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003397 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003398
Takashi Iwaibefdf312005-08-22 13:57:55 +02003399 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400};
3401
3402/**
3403 * snd_hda_calc_stream_format - calculate format bitset
3404 * @rate: the sample rate
3405 * @channels: the number of channels
3406 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3407 * @maxbps: the max. bps
3408 *
3409 * Calculate the format bitset from the given rate, channels and th PCM format.
3410 *
3411 * Return zero if invalid.
3412 */
3413unsigned int snd_hda_calc_stream_format(unsigned int rate,
3414 unsigned int channels,
3415 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003416 unsigned int maxbps,
3417 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
3419 int i;
3420 unsigned int val = 0;
3421
Takashi Iwaibefdf312005-08-22 13:57:55 +02003422 for (i = 0; rate_bits[i].hz; i++)
3423 if (rate_bits[i].hz == rate) {
3424 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 break;
3426 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003427 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 snd_printdd("invalid rate %d\n", rate);
3429 return 0;
3430 }
3431
3432 if (channels == 0 || channels > 8) {
3433 snd_printdd("invalid channels %d\n", channels);
3434 return 0;
3435 }
3436 val |= channels - 1;
3437
3438 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003439 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003440 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003441 break;
3442 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003443 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 case 20:
3446 case 24:
3447 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003448 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003449 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003451 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003453 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 break;
3455 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003456 snd_printdd("invalid format width %d\n",
3457 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return 0;
3459 }
3460
Anssi Hannula32c168c2010-08-03 13:28:57 +03003461 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003462 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return val;
3465}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003466EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003468static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3469{
3470 unsigned int val = 0;
3471 if (nid != codec->afg &&
3472 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3473 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3474 if (!val || val == -1)
3475 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3476 if (!val || val == -1)
3477 return 0;
3478 return val;
3479}
3480
3481static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3482{
3483 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3484 get_pcm_param);
3485}
3486
3487static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3488{
3489 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3490 if (!streams || streams == -1)
3491 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3492 if (!streams || streams == -1)
3493 return 0;
3494 return streams;
3495}
3496
3497static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3498{
3499 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3500 get_stream_param);
3501}
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503/**
3504 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3505 * @codec: the HDA codec
3506 * @nid: NID to query
3507 * @ratesp: the pointer to store the detected rate bitflags
3508 * @formatsp: the pointer to store the detected formats
3509 * @bpsp: the pointer to store the detected format widths
3510 *
3511 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3512 * or @bsps argument is ignored.
3513 *
3514 * Returns 0 if successful, otherwise a negative error code.
3515 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003516int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3518{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003519 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003521 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003522 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
3524 if (ratesp) {
3525 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003526 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003528 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003530 if (rates == 0) {
3531 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3532 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3533 nid, val,
3534 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3535 return -EIO;
3536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 *ratesp = rates;
3538 }
3539
3540 if (formatsp || bpsp) {
3541 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003542 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003544 streams = query_stream_param(codec, nid);
3545 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
3548 bps = 0;
3549 if (streams & AC_SUPFMT_PCM) {
3550 if (val & AC_SUPPCM_BITS_8) {
3551 formats |= SNDRV_PCM_FMTBIT_U8;
3552 bps = 8;
3553 }
3554 if (val & AC_SUPPCM_BITS_16) {
3555 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3556 bps = 16;
3557 }
3558 if (wcaps & AC_WCAP_DIGITAL) {
3559 if (val & AC_SUPPCM_BITS_32)
3560 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3561 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3562 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3563 if (val & AC_SUPPCM_BITS_24)
3564 bps = 24;
3565 else if (val & AC_SUPPCM_BITS_20)
3566 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003567 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3568 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3570 if (val & AC_SUPPCM_BITS_32)
3571 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 else if (val & AC_SUPPCM_BITS_24)
3573 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003574 else if (val & AC_SUPPCM_BITS_20)
3575 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
3577 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003578 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003580 if (!bps)
3581 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003582 }
3583 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003584 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 /* temporary hack: we have still no proper support
3586 * for the direct AC3 stream...
3587 */
3588 formats |= SNDRV_PCM_FMTBIT_U8;
3589 bps = 8;
3590 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003591 if (formats == 0) {
3592 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3593 "(nid=0x%x, val=0x%x, ovrd=%i, "
3594 "streams=0x%x)\n",
3595 nid, val,
3596 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3597 streams);
3598 return -EIO;
3599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 if (formatsp)
3601 *formatsp = formats;
3602 if (bpsp)
3603 *bpsp = bps;
3604 }
3605
3606 return 0;
3607}
Stephen Warren384a48d2011-06-01 11:14:21 -06003608EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003611 * snd_hda_is_supported_format - Check the validity of the format
3612 * @codec: HD-audio codec
3613 * @nid: NID to check
3614 * @format: the HD-audio format value to check
3615 *
3616 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 *
3618 * Returns 1 if supported, 0 if not.
3619 */
3620int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3621 unsigned int format)
3622{
3623 int i;
3624 unsigned int val = 0, rate, stream;
3625
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003626 val = query_pcm_param(codec, nid);
3627 if (!val)
3628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003631 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003632 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 if (val & (1 << i))
3634 break;
3635 return 0;
3636 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003637 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return 0;
3639
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003640 stream = query_stream_param(codec, nid);
3641 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 return 0;
3643
3644 if (stream & AC_SUPFMT_PCM) {
3645 switch (format & 0xf0) {
3646 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003647 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return 0;
3649 break;
3650 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003651 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return 0;
3653 break;
3654 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003655 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 return 0;
3657 break;
3658 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003659 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return 0;
3661 break;
3662 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003663 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return 0;
3665 break;
3666 default:
3667 return 0;
3668 }
3669 } else {
3670 /* FIXME: check for float32 and AC3? */
3671 }
3672
3673 return 1;
3674}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003675EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
3677/*
3678 * PCM stuff
3679 */
3680static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3681 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003682 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
3684 return 0;
3685}
3686
3687static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3688 struct hda_codec *codec,
3689 unsigned int stream_tag,
3690 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003691 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
3693 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3694 return 0;
3695}
3696
3697static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3698 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003699 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Takashi Iwai888afa12008-03-18 09:57:50 +01003701 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 return 0;
3703}
3704
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003705static int set_pcm_default_values(struct hda_codec *codec,
3706 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003708 int err;
3709
Takashi Iwai0ba21762007-04-16 11:29:14 +02003710 /* query support PCM information from the given NID */
3711 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003712 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003713 info->rates ? NULL : &info->rates,
3714 info->formats ? NULL : &info->formats,
3715 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003716 if (err < 0)
3717 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
3719 if (info->ops.open == NULL)
3720 info->ops.open = hda_pcm_default_open_close;
3721 if (info->ops.close == NULL)
3722 info->ops.close = hda_pcm_default_open_close;
3723 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003724 if (snd_BUG_ON(!info->nid))
3725 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 info->ops.prepare = hda_pcm_default_prepare;
3727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003729 if (snd_BUG_ON(!info->nid))
3730 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 info->ops.cleanup = hda_pcm_default_cleanup;
3732 }
3733 return 0;
3734}
3735
Takashi Iwaieb541332010-08-06 13:48:11 +02003736/*
3737 * codec prepare/cleanup entries
3738 */
3739int snd_hda_codec_prepare(struct hda_codec *codec,
3740 struct hda_pcm_stream *hinfo,
3741 unsigned int stream,
3742 unsigned int format,
3743 struct snd_pcm_substream *substream)
3744{
3745 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003746 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003747 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3748 if (ret >= 0)
3749 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003750 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003751 return ret;
3752}
3753EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3754
3755void snd_hda_codec_cleanup(struct hda_codec *codec,
3756 struct hda_pcm_stream *hinfo,
3757 struct snd_pcm_substream *substream)
3758{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003759 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003760 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003761 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003762}
3763EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3764
Takashi Iwaid5191e52009-11-16 14:58:17 +01003765/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003766const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3767 "Audio", "SPDIF", "HDMI", "Modem"
3768};
3769
Takashi Iwai176d5332008-07-30 15:01:44 +02003770/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003771 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003772 *
3773 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003774 */
3775static int get_empty_pcm_device(struct hda_bus *bus, int type)
3776{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003777 /* audio device indices; not linear to keep compatibility */
3778 static int audio_idx[HDA_PCM_NTYPES][5] = {
3779 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3780 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003781 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003782 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003783 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003784 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003785
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003786 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003787 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3788 return -EINVAL;
3789 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003790
3791 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3792 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3793 return audio_idx[type][i];
3794
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003795 snd_printk(KERN_WARNING "Too many %s devices\n",
3796 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003797 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003798}
3799
3800/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003801 * attach a new PCM stream
3802 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003803static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003804{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003805 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003806 struct hda_pcm_stream *info;
3807 int stream, err;
3808
Takashi Iwaib91f0802008-11-04 08:43:08 +01003809 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003810 return -EINVAL;
3811 for (stream = 0; stream < 2; stream++) {
3812 info = &pcm->stream[stream];
3813 if (info->substreams) {
3814 err = set_pcm_default_values(codec, info);
3815 if (err < 0)
3816 return err;
3817 }
3818 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003819 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003820}
3821
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003822/* assign all PCMs of the given codec */
3823int snd_hda_codec_build_pcms(struct hda_codec *codec)
3824{
3825 unsigned int pcm;
3826 int err;
3827
3828 if (!codec->num_pcms) {
3829 if (!codec->patch_ops.build_pcms)
3830 return 0;
3831 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003832 if (err < 0) {
3833 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003834 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003835 err = snd_hda_codec_reset(codec);
3836 if (err < 0) {
3837 printk(KERN_ERR
3838 "hda_codec: cannot revert codec\n");
3839 return err;
3840 }
3841 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003842 }
3843 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3844 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3845 int dev;
3846
3847 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003848 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003849
3850 if (!cpcm->pcm) {
3851 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3852 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003853 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003854 cpcm->device = dev;
3855 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003856 if (err < 0) {
3857 printk(KERN_ERR "hda_codec: cannot attach "
3858 "PCM stream %d for codec #%d\n",
3859 dev, codec->addr);
3860 continue; /* no fatal error */
3861 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003862 }
3863 }
3864 return 0;
3865}
3866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867/**
3868 * snd_hda_build_pcms - build PCM information
3869 * @bus: the BUS
3870 *
3871 * Create PCM information for each codec included in the bus.
3872 *
3873 * The build_pcms codec patch is requested to set up codec->num_pcms and
3874 * codec->pcm_info properly. The array is referred by the top-level driver
3875 * to create its PCM instances.
3876 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3877 * callback.
3878 *
3879 * At least, substreams, channels_min and channels_max must be filled for
3880 * each stream. substreams = 0 indicates that the stream doesn't exist.
3881 * When rates and/or formats are zero, the supported values are queried
3882 * from the given nid. The nid is used also by the default ops.prepare
3883 * and ops.cleanup callbacks.
3884 *
3885 * The driver needs to call ops.open in its open callback. Similarly,
3886 * ops.close is supposed to be called in the close callback.
3887 * ops.prepare should be called in the prepare or hw_params callback
3888 * with the proper parameters for set up.
3889 * ops.cleanup should be called in hw_free for clean up of streams.
3890 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003891 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003893int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003895 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Takashi Iwai0ba21762007-04-16 11:29:14 +02003897 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003898 int err = snd_hda_codec_build_pcms(codec);
3899 if (err < 0)
3900 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 }
3902 return 0;
3903}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003904EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906/**
3907 * snd_hda_check_board_config - compare the current codec with the config table
3908 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003909 * @num_configs: number of config enums
3910 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 * @tbl: configuration table, terminated by null entries
3912 *
3913 * Compares the modelname or PCI subsystem id of the current codec with the
3914 * given configuration table. If a matching entry is found, returns its
3915 * config value (supposed to be 0 or positive).
3916 *
3917 * If no entries are matching, the function returns a negative value.
3918 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003919int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003920 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003921 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003923 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003924 int i;
3925 for (i = 0; i < num_configs; i++) {
3926 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003927 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003928 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3929 "selected\n", models[i]);
3930 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
3932 }
3933 }
3934
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003935 if (!codec->bus->pci || !tbl)
3936 return -1;
3937
3938 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3939 if (!tbl)
3940 return -1;
3941 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003942#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003943 char tmp[10];
3944 const char *model = NULL;
3945 if (models)
3946 model = models[tbl->value];
3947 if (!model) {
3948 sprintf(tmp, "#%d", tbl->value);
3949 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003951 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3952 "for config %x:%x (%s)\n",
3953 model, tbl->subvendor, tbl->subdevice,
3954 (tbl->name ? tbl->name : "Unknown device"));
3955#endif
3956 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 }
3958 return -1;
3959}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003960EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
3962/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003963 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003964 subsystem ID with the
3965 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003966
3967 This is important for Gateway notebooks with SB450 HDA Audio
3968 where the vendor ID of the PCI device is:
3969 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3970 and the vendor/subvendor are found only at the codec.
3971
3972 * @codec: the HDA codec
3973 * @num_configs: number of config enums
3974 * @models: array of model name strings
3975 * @tbl: configuration table, terminated by null entries
3976 *
3977 * Compares the modelname or PCI subsystem id of the current codec with the
3978 * given configuration table. If a matching entry is found, returns its
3979 * config value (supposed to be 0 or positive).
3980 *
3981 * If no entries are matching, the function returns a negative value.
3982 */
3983int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003984 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003985 const struct snd_pci_quirk *tbl)
3986{
3987 const struct snd_pci_quirk *q;
3988
3989 /* Search for codec ID */
3990 for (q = tbl; q->subvendor; q++) {
3991 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3992
3993 if (vendorid == codec->subsystem_id)
3994 break;
3995 }
3996
3997 if (!q->subvendor)
3998 return -1;
3999
4000 tbl = q;
4001
4002 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004003#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004004 char tmp[10];
4005 const char *model = NULL;
4006 if (models)
4007 model = models[tbl->value];
4008 if (!model) {
4009 sprintf(tmp, "#%d", tbl->value);
4010 model = tmp;
4011 }
4012 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4013 "for config %x:%x (%s)\n",
4014 model, tbl->subvendor, tbl->subdevice,
4015 (tbl->name ? tbl->name : "Unknown device"));
4016#endif
4017 return tbl->value;
4018 }
4019 return -1;
4020}
4021EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4022
4023/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 * snd_hda_add_new_ctls - create controls from the array
4025 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004026 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 *
4028 * This helper function creates and add new controls in the given array.
4029 * The array must be terminated with an empty entry as terminator.
4030 *
4031 * Returns 0 if successful, or a negative error code.
4032 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004033int snd_hda_add_new_ctls(struct hda_codec *codec,
4034 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004036 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
4038 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004039 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004040 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004041 if (knew->iface == -1) /* skip this codec private value */
4042 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004043 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004044 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004045 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004046 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004047 if (addr > 0)
4048 kctl->id.device = addr;
4049 if (idx > 0)
4050 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004051 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004052 if (!err)
4053 break;
4054 /* try first with another device index corresponding to
4055 * the codec addr; if it still fails (or it's the
4056 * primary codec), then try another control index
4057 */
4058 if (!addr && codec->addr)
4059 addr = codec->addr;
4060 else if (!idx && !knew->index) {
4061 idx = find_empty_mixer_ctl_idx(codec,
4062 knew->name);
4063 if (idx <= 0)
4064 return err;
4065 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004066 return err;
4067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 }
4069 return 0;
4070}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004071EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Takashi Iwaicb53c622007-08-10 17:21:45 +02004073#ifdef CONFIG_SND_HDA_POWER_SAVE
4074static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
4075 unsigned int power_state);
4076
4077static void hda_power_work(struct work_struct *work)
4078{
4079 struct hda_codec *codec =
4080 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004081 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004082
Maxim Levitsky2e492462007-09-03 15:26:57 +02004083 if (!codec->power_on || codec->power_count) {
4084 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004085 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004086 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004087
4088 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004089 if (bus->ops.pm_notify)
4090 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004091}
4092
4093static void hda_keep_power_on(struct hda_codec *codec)
4094{
4095 codec->power_count++;
4096 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004097 codec->power_jiffies = jiffies;
4098}
4099
Takashi Iwaid5191e52009-11-16 14:58:17 +01004100/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004101void snd_hda_update_power_acct(struct hda_codec *codec)
4102{
4103 unsigned long delta = jiffies - codec->power_jiffies;
4104 if (codec->power_on)
4105 codec->power_on_acct += delta;
4106 else
4107 codec->power_off_acct += delta;
4108 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004109}
4110
Takashi Iwaid5191e52009-11-16 14:58:17 +01004111/**
4112 * snd_hda_power_up - Power-up the codec
4113 * @codec: HD-audio codec
4114 *
4115 * Increment the power-up counter and power up the hardware really when
4116 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004117 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004118void snd_hda_power_up(struct hda_codec *codec)
4119{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004120 struct hda_bus *bus = codec->bus;
4121
Takashi Iwaicb53c622007-08-10 17:21:45 +02004122 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004123 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004124 return;
4125
Takashi Iwaia2f63092009-11-11 09:34:25 +01004126 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004127 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004128 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004129 if (bus->ops.pm_notify)
4130 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004131 hda_call_codec_resume(codec);
4132 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004133 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004134}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004135EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004136
4137#define power_save(codec) \
4138 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004139
Takashi Iwaid5191e52009-11-16 14:58:17 +01004140/**
4141 * snd_hda_power_down - Power-down the codec
4142 * @codec: HD-audio codec
4143 *
4144 * Decrement the power-up counter and schedules the power-off work if
4145 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004146 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004147void snd_hda_power_down(struct hda_codec *codec)
4148{
4149 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004150 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004151 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004152 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004153 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004154 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004155 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004156 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004158EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004159
Takashi Iwaid5191e52009-11-16 14:58:17 +01004160/**
4161 * snd_hda_check_amp_list_power - Check the amp list and update the power
4162 * @codec: HD-audio codec
4163 * @check: the object containing an AMP list and the status
4164 * @nid: NID to check / update
4165 *
4166 * Check whether the given NID is in the amp list. If it's in the list,
4167 * check the current AMP status, and update the the power-status according
4168 * to the mute status.
4169 *
4170 * This function is supposed to be set or called from the check_power_status
4171 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004172 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004173int snd_hda_check_amp_list_power(struct hda_codec *codec,
4174 struct hda_loopback_check *check,
4175 hda_nid_t nid)
4176{
Takashi Iwai031024e2011-05-02 11:29:30 +02004177 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004178 int ch, v;
4179
4180 if (!check->amplist)
4181 return 0;
4182 for (p = check->amplist; p->nid; p++) {
4183 if (p->nid == nid)
4184 break;
4185 }
4186 if (!p->nid)
4187 return 0; /* nothing changed */
4188
4189 for (p = check->amplist; p->nid; p++) {
4190 for (ch = 0; ch < 2; ch++) {
4191 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4192 p->idx);
4193 if (!(v & HDA_AMP_MUTE) && v > 0) {
4194 if (!check->power_on) {
4195 check->power_on = 1;
4196 snd_hda_power_up(codec);
4197 }
4198 return 1;
4199 }
4200 }
4201 }
4202 if (check->power_on) {
4203 check->power_on = 0;
4204 snd_hda_power_down(codec);
4205 }
4206 return 0;
4207}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004208EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004209#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004211/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004212 * Channel mode helper
4213 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004214
4215/**
4216 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4217 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004218int snd_hda_ch_mode_info(struct hda_codec *codec,
4219 struct snd_ctl_elem_info *uinfo,
4220 const struct hda_channel_mode *chmode,
4221 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004222{
4223 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4224 uinfo->count = 1;
4225 uinfo->value.enumerated.items = num_chmodes;
4226 if (uinfo->value.enumerated.item >= num_chmodes)
4227 uinfo->value.enumerated.item = num_chmodes - 1;
4228 sprintf(uinfo->value.enumerated.name, "%dch",
4229 chmode[uinfo->value.enumerated.item].channels);
4230 return 0;
4231}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004232EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004233
Takashi Iwaid5191e52009-11-16 14:58:17 +01004234/**
4235 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4236 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004237int snd_hda_ch_mode_get(struct hda_codec *codec,
4238 struct snd_ctl_elem_value *ucontrol,
4239 const struct hda_channel_mode *chmode,
4240 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004241 int max_channels)
4242{
4243 int i;
4244
4245 for (i = 0; i < num_chmodes; i++) {
4246 if (max_channels == chmode[i].channels) {
4247 ucontrol->value.enumerated.item[0] = i;
4248 break;
4249 }
4250 }
4251 return 0;
4252}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004253EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004254
Takashi Iwaid5191e52009-11-16 14:58:17 +01004255/**
4256 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4257 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004258int snd_hda_ch_mode_put(struct hda_codec *codec,
4259 struct snd_ctl_elem_value *ucontrol,
4260 const struct hda_channel_mode *chmode,
4261 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004262 int *max_channelsp)
4263{
4264 unsigned int mode;
4265
4266 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004267 if (mode >= num_chmodes)
4268 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004269 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004270 return 0;
4271 /* change the current channel setting */
4272 *max_channelsp = chmode[mode].channels;
4273 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004274 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004275 return 1;
4276}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004277EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279/*
4280 * input MUX helper
4281 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004282
4283/**
4284 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4285 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004286int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4287 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
4289 unsigned int index;
4290
4291 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4292 uinfo->count = 1;
4293 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004294 if (!imux->num_items)
4295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 index = uinfo->value.enumerated.item;
4297 if (index >= imux->num_items)
4298 index = imux->num_items - 1;
4299 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4300 return 0;
4301}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004302EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Takashi Iwaid5191e52009-11-16 14:58:17 +01004304/**
4305 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4306 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004307int snd_hda_input_mux_put(struct hda_codec *codec,
4308 const struct hda_input_mux *imux,
4309 struct snd_ctl_elem_value *ucontrol,
4310 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 unsigned int *cur_val)
4312{
4313 unsigned int idx;
4314
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004315 if (!imux->num_items)
4316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 idx = ucontrol->value.enumerated.item[0];
4318 if (idx >= imux->num_items)
4319 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004320 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004322 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4323 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 *cur_val = idx;
4325 return 1;
4326}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004327EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329
4330/*
4331 * Multi-channel / digital-out PCM helper functions
4332 */
4333
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004334/* setup SPDIF output stream */
4335static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4336 unsigned int stream_tag, unsigned int format)
4337{
Stephen Warren7c935972011-06-01 11:14:17 -06004338 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4339
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004340 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06004341 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004342 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004343 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004344 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004345 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004346 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004347 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004348 for (d = codec->slave_dig_outs; *d; d++)
4349 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4350 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004351 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004352 /* turn on again (if needed) */
Stephen Warren7c935972011-06-01 11:14:17 -06004353 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004354 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004355 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004356}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004357
Takashi Iwai2f728532008-09-25 16:32:41 +02004358static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4359{
4360 snd_hda_codec_cleanup_stream(codec, nid);
4361 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004362 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004363 for (d = codec->slave_dig_outs; *d; d++)
4364 snd_hda_codec_cleanup_stream(codec, *d);
4365 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004366}
4367
Takashi Iwaid5191e52009-11-16 14:58:17 +01004368/**
4369 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4370 * @bus: HD-audio bus
4371 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004372void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4373{
4374 struct hda_codec *codec;
4375
4376 if (!bus)
4377 return;
4378 list_for_each_entry(codec, &bus->codec_list, list) {
4379#ifdef CONFIG_SND_HDA_POWER_SAVE
4380 if (!codec->power_on)
4381 continue;
4382#endif
4383 if (codec->patch_ops.reboot_notify)
4384 codec->patch_ops.reboot_notify(codec);
4385 }
4386}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004387EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004388
Takashi Iwaid5191e52009-11-16 14:58:17 +01004389/**
4390 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004392int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4393 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
Ingo Molnar62932df2006-01-16 16:34:20 +01004395 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004396 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4397 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004398 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004400 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 return 0;
4402}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004403EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Takashi Iwaid5191e52009-11-16 14:58:17 +01004405/**
4406 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4407 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004408int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4409 struct hda_multi_out *mout,
4410 unsigned int stream_tag,
4411 unsigned int format,
4412 struct snd_pcm_substream *substream)
4413{
4414 mutex_lock(&codec->spdif_mutex);
4415 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4416 mutex_unlock(&codec->spdif_mutex);
4417 return 0;
4418}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004419EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004420
Takashi Iwaid5191e52009-11-16 14:58:17 +01004421/**
4422 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4423 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004424int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4425 struct hda_multi_out *mout)
4426{
4427 mutex_lock(&codec->spdif_mutex);
4428 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4429 mutex_unlock(&codec->spdif_mutex);
4430 return 0;
4431}
4432EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4433
Takashi Iwaid5191e52009-11-16 14:58:17 +01004434/**
4435 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004437int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4438 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439{
Ingo Molnar62932df2006-01-16 16:34:20 +01004440 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004442 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 return 0;
4444}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004445EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Takashi Iwaid5191e52009-11-16 14:58:17 +01004447/**
4448 * snd_hda_multi_out_analog_open - open analog outputs
4449 *
4450 * Open analog outputs and set up the hw-constraints.
4451 * If the digital outputs can be opened as slave, open the digital
4452 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004454int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4455 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004456 struct snd_pcm_substream *substream,
4457 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458{
Takashi Iwai9a081602008-02-12 18:37:26 +01004459 struct snd_pcm_runtime *runtime = substream->runtime;
4460 runtime->hw.channels_max = mout->max_channels;
4461 if (mout->dig_out_nid) {
4462 if (!mout->analog_rates) {
4463 mout->analog_rates = hinfo->rates;
4464 mout->analog_formats = hinfo->formats;
4465 mout->analog_maxbps = hinfo->maxbps;
4466 } else {
4467 runtime->hw.rates = mout->analog_rates;
4468 runtime->hw.formats = mout->analog_formats;
4469 hinfo->maxbps = mout->analog_maxbps;
4470 }
4471 if (!mout->spdif_rates) {
4472 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4473 &mout->spdif_rates,
4474 &mout->spdif_formats,
4475 &mout->spdif_maxbps);
4476 }
4477 mutex_lock(&codec->spdif_mutex);
4478 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004479 if ((runtime->hw.rates & mout->spdif_rates) &&
4480 (runtime->hw.formats & mout->spdif_formats)) {
4481 runtime->hw.rates &= mout->spdif_rates;
4482 runtime->hw.formats &= mout->spdif_formats;
4483 if (mout->spdif_maxbps < hinfo->maxbps)
4484 hinfo->maxbps = mout->spdif_maxbps;
4485 } else {
4486 mout->share_spdif = 0;
4487 /* FIXME: need notify? */
4488 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004489 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004490 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4493 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4494}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004495EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Takashi Iwaid5191e52009-11-16 14:58:17 +01004497/**
4498 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4499 *
4500 * Set up the i/o for analog out.
4501 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004503int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4504 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 unsigned int stream_tag,
4506 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004507 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508{
Takashi Iwaidda14412011-05-02 11:29:30 +02004509 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 int chs = substream->runtime->channels;
Stephen Warren7c935972011-06-01 11:14:17 -06004511 struct hda_spdif_out *spdif =
4512 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 int i;
4514
Ingo Molnar62932df2006-01-16 16:34:20 +01004515 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004516 if (mout->dig_out_nid && mout->share_spdif &&
4517 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004519 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4520 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004521 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004523 setup_dig_out_stream(codec, mout->dig_out_nid,
4524 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 } else {
4526 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004527 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 }
4529 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004530 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
4532 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004533 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4534 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004535 if (!mout->no_share_stream &&
4536 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004538 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4539 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004540 /* extra outputs copied from front */
4541 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004542 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004543 snd_hda_codec_setup_stream(codec,
4544 mout->extra_out_nid[i],
4545 stream_tag, 0, format);
4546
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 /* surrounds */
4548 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004549 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004550 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4551 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004552 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004553 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4554 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
4556 return 0;
4557}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004558EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Takashi Iwaid5191e52009-11-16 14:58:17 +01004560/**
4561 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004563int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4564 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Takashi Iwaidda14412011-05-02 11:29:30 +02004566 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 int i;
4568
4569 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004570 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004572 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004573 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4574 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004575 snd_hda_codec_cleanup_stream(codec,
4576 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004577 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004579 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 mout->dig_out_used = 0;
4581 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004582 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 return 0;
4584}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004585EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004587/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004588 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004589 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004590
Takashi Iwaidda14412011-05-02 11:29:30 +02004591static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004592{
4593 for (; *list; list++)
4594 if (*list == nid)
4595 return 1;
4596 return 0;
4597}
4598
Steve Longerbeam81937d32007-05-08 15:33:03 +02004599
4600/*
4601 * Sort an associated group of pins according to their sequence numbers.
4602 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004603static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004604 int num_pins)
4605{
4606 int i, j;
4607 short seq;
4608 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004609
Steve Longerbeam81937d32007-05-08 15:33:03 +02004610 for (i = 0; i < num_pins; i++) {
4611 for (j = i + 1; j < num_pins; j++) {
4612 if (sequences[i] > sequences[j]) {
4613 seq = sequences[i];
4614 sequences[i] = sequences[j];
4615 sequences[j] = seq;
4616 nid = pins[i];
4617 pins[i] = pins[j];
4618 pins[j] = nid;
4619 }
4620 }
4621 }
4622}
4623
4624
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004625/* add the found input-pin to the cfg->inputs[] table */
4626static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4627 int type)
4628{
4629 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4630 cfg->inputs[cfg->num_inputs].pin = nid;
4631 cfg->inputs[cfg->num_inputs].type = type;
4632 cfg->num_inputs++;
4633 }
4634}
4635
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004636/* sort inputs in the order of AUTO_PIN_* type */
4637static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4638{
4639 int i, j;
4640
4641 for (i = 0; i < cfg->num_inputs; i++) {
4642 for (j = i + 1; j < cfg->num_inputs; j++) {
4643 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4644 struct auto_pin_cfg_item tmp;
4645 tmp = cfg->inputs[i];
4646 cfg->inputs[i] = cfg->inputs[j];
4647 cfg->inputs[j] = tmp;
4648 }
4649 }
4650 }
4651}
4652
Takashi Iwai82bc9552006-03-21 11:24:42 +01004653/*
4654 * Parse all pin widgets and store the useful pin nids to cfg
4655 *
4656 * The number of line-outs or any primary output is stored in line_outs,
4657 * and the corresponding output pins are assigned to line_out_pins[],
4658 * in the order of front, rear, CLFE, side, ...
4659 *
4660 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004661 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004662 * is detected, one of speaker of HP pins is assigned as the primary
4663 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4664 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004665 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004666 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004667 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4668 * respectively.
4669 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004670int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4671 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004672 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004673{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004674 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004675 short seq, assoc_line_out, assoc_speaker;
4676 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4677 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004678 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004679 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004680
4681 memset(cfg, 0, sizeof(*cfg));
4682
Steve Longerbeam81937d32007-05-08 15:33:03 +02004683 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4684 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004685 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004686 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004687
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004688 end_nid = codec->start_nid + codec->num_nodes;
4689 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004690 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004691 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004692 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004693 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004694
4695 /* read all default configuration for pin complex */
4696 if (wid_type != AC_WID_PIN)
4697 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004698 /* ignore the given nids (e.g. pc-beep returns error) */
4699 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4700 continue;
4701
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004702 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004703 conn = get_defcfg_connect(def_conf);
4704 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004705 continue;
4706 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004707 dev = get_defcfg_device(def_conf);
4708
4709 /* workaround for buggy BIOS setups */
4710 if (dev == AC_JACK_LINE_OUT) {
4711 if (conn == AC_JACK_PORT_FIXED)
4712 dev = AC_JACK_SPEAKER;
4713 }
4714
4715 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004716 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004717 seq = get_defcfg_sequence(def_conf);
4718 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004719
4720 if (!(wid_caps & AC_WCAP_STEREO))
4721 if (!cfg->mono_out_pin)
4722 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004723 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004724 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004725 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004726 assoc_line_out = assoc;
4727 else if (assoc_line_out != assoc)
4728 continue;
4729 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4730 continue;
4731 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004732 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004733 cfg->line_outs++;
4734 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004735 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004736 seq = get_defcfg_sequence(def_conf);
4737 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004738 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004739 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004740 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004741 assoc_speaker = assoc;
4742 else if (assoc_speaker != assoc)
4743 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004744 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4745 continue;
4746 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004747 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004748 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004749 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004750 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004751 seq = get_defcfg_sequence(def_conf);
4752 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004753 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4754 continue;
4755 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004756 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004757 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004758 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004759 case AC_JACK_MIC_IN:
4760 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004761 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004762 case AC_JACK_LINE_IN:
4763 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004764 break;
4765 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004766 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004767 break;
4768 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004769 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004770 break;
4771 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004772 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004773 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4774 continue;
4775 cfg->dig_out_pins[cfg->dig_outs] = nid;
4776 cfg->dig_out_type[cfg->dig_outs] =
4777 (loc == AC_JACK_LOC_HDMI) ?
4778 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4779 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004780 break;
4781 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004782 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004783 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004784 if (loc == AC_JACK_LOC_HDMI)
4785 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4786 else
4787 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004788 break;
4789 }
4790 }
4791
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004792 /* FIX-UP:
4793 * If no line-out is defined but multiple HPs are found,
4794 * some of them might be the real line-outs.
4795 */
4796 if (!cfg->line_outs && cfg->hp_outs > 1) {
4797 int i = 0;
4798 while (i < cfg->hp_outs) {
4799 /* The real HPs should have the sequence 0x0f */
4800 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4801 i++;
4802 continue;
4803 }
4804 /* Move it to the line-out table */
4805 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4806 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4807 cfg->line_outs++;
4808 cfg->hp_outs--;
4809 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4810 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004811 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004812 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4813 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004814 memset(cfg->hp_pins + cfg->hp_outs, 0,
4815 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004816 if (!cfg->hp_outs)
4817 cfg->line_out_type = AUTO_PIN_HP_OUT;
4818
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004819 }
4820
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004821 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004822 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4823 cfg->line_outs);
4824 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4825 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004826 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4827 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004828
Steve Longerbeam81937d32007-05-08 15:33:03 +02004829 /*
4830 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4831 * as a primary output
4832 */
4833 if (!cfg->line_outs) {
4834 if (cfg->speaker_outs) {
4835 cfg->line_outs = cfg->speaker_outs;
4836 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4837 sizeof(cfg->speaker_pins));
4838 cfg->speaker_outs = 0;
4839 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4840 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4841 } else if (cfg->hp_outs) {
4842 cfg->line_outs = cfg->hp_outs;
4843 memcpy(cfg->line_out_pins, cfg->hp_pins,
4844 sizeof(cfg->hp_pins));
4845 cfg->hp_outs = 0;
4846 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4847 cfg->line_out_type = AUTO_PIN_HP_OUT;
4848 }
4849 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004850
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004851 /* Reorder the surround channels
4852 * ALSA sequence is front/surr/clfe/side
4853 * HDA sequence is:
4854 * 4-ch: front/surr => OK as it is
4855 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004856 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004857 */
4858 switch (cfg->line_outs) {
4859 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004860 case 4:
4861 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004862 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004863 cfg->line_out_pins[2] = nid;
4864 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004865 }
4866
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004867 sort_autocfg_input_pins(cfg);
4868
Takashi Iwai82bc9552006-03-21 11:24:42 +01004869 /*
4870 * debug prints of the parsed results
4871 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004872 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
Takashi Iwai82bc9552006-03-21 11:24:42 +01004873 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4874 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004875 cfg->line_out_pins[4],
4876 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4877 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4878 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004879 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4880 cfg->speaker_outs, cfg->speaker_pins[0],
4881 cfg->speaker_pins[1], cfg->speaker_pins[2],
4882 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004883 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4884 cfg->hp_outs, cfg->hp_pins[0],
4885 cfg->hp_pins[1], cfg->hp_pins[2],
4886 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004887 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004888 if (cfg->dig_outs)
4889 snd_printd(" dig-out=0x%x/0x%x\n",
4890 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004891 snd_printd(" inputs:");
4892 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004893 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004894 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004895 cfg->inputs[i].pin);
4896 }
4897 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004898 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004899 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004900
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004901 return 0;
4902}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004903EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004904
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004905int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004906{
4907 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004908 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004909 if (conn == AC_JACK_PORT_NONE)
4910 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004911 /* Windows may claim the internal mic to be BOTH, too */
4912 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004913 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004914 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004915 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004916 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004917 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004918 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004919 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004920 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004921 return INPUT_PIN_ATTR_FRONT;
4922 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004923}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004924EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004925
Takashi Iwai990061c2010-09-09 22:08:44 +02004926/**
4927 * hda_get_input_pin_label - Give a label for the given input pin
4928 *
4929 * When check_location is true, the function checks the pin location
4930 * for mic and line-in pins, and set an appropriate prefix like "Front",
4931 * "Rear", "Internal".
4932 */
4933
Takashi Iwai10a20af2010-09-09 16:28:02 +02004934const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4935 int check_location)
4936{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004937 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004938 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004939 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4940 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004941 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004942
Takashi Iwai10a20af2010-09-09 16:28:02 +02004943 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004944
4945 switch (get_defcfg_device(def_conf)) {
4946 case AC_JACK_MIC_IN:
4947 if (!check_location)
4948 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004949 attr = snd_hda_get_input_pin_attr(def_conf);
4950 if (!attr)
4951 return "None";
4952 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004953 case AC_JACK_LINE_IN:
4954 if (!check_location)
4955 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004956 attr = snd_hda_get_input_pin_attr(def_conf);
4957 if (!attr)
4958 return "None";
4959 if (attr == INPUT_PIN_ATTR_DOCK)
4960 return "Dock Line";
4961 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004962 case AC_JACK_AUX:
4963 return "Aux";
4964 case AC_JACK_CD:
4965 return "CD";
4966 case AC_JACK_SPDIF_IN:
4967 return "SPDIF In";
4968 case AC_JACK_DIG_OTHER_IN:
4969 return "Digital In";
4970 default:
4971 return "Misc";
4972 }
4973}
4974EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4975
Takashi Iwaia1c98512010-09-09 21:36:27 +02004976/* Check whether the location prefix needs to be added to the label.
4977 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4978 * have to put "Front" prefix to each label. In such a case, returns false.
4979 */
4980static int check_mic_location_need(struct hda_codec *codec,
4981 const struct auto_pin_cfg *cfg,
4982 int input)
4983{
4984 unsigned int defc;
4985 int i, attr, attr2;
4986
4987 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004988 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004989 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004990 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004991 return 1;
4992
4993 attr = 0;
4994 for (i = 0; i < cfg->num_inputs; i++) {
4995 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004996 attr2 = snd_hda_get_input_pin_attr(defc);
4997 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004998 if (attr && attr != attr2)
4999 return 1; /* different locations found */
5000 attr = attr2;
5001 }
5002 }
5003 return 0;
5004}
5005
Takashi Iwai990061c2010-09-09 22:08:44 +02005006/**
5007 * hda_get_autocfg_input_label - Get a label for the given input
5008 *
5009 * Get a label for the given input pin defined by the autocfg item.
5010 * Unlike hda_get_input_pin_label(), this function checks all inputs
5011 * defined in autocfg and avoids the redundant mic/line prefix as much as
5012 * possible.
5013 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005014const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5015 const struct auto_pin_cfg *cfg,
5016 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005017{
5018 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005019 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005020
Takashi Iwai10a20af2010-09-09 16:28:02 +02005021 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5022 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5023 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005024 if (has_multiple_pins && type == AUTO_PIN_MIC)
5025 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005026 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5027 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005028}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005029EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5030
Takashi Iwai990061c2010-09-09 22:08:44 +02005031/**
5032 * snd_hda_add_imux_item - Add an item to input_mux
5033 *
5034 * When the same label is used already in the existing items, the number
5035 * suffix is appended to the label. This label index number is stored
5036 * to type_idx when non-NULL pointer is given.
5037 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005038int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5039 int index, int *type_idx)
5040{
5041 int i, label_idx = 0;
5042 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5043 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5044 return -EINVAL;
5045 }
5046 for (i = 0; i < imux->num_items; i++) {
5047 if (!strncmp(label, imux->items[i].label, strlen(label)))
5048 label_idx++;
5049 }
5050 if (type_idx)
5051 *type_idx = label_idx;
5052 if (label_idx > 0)
5053 snprintf(imux->items[imux->num_items].label,
5054 sizeof(imux->items[imux->num_items].label),
5055 "%s %d", label, label_idx);
5056 else
5057 strlcpy(imux->items[imux->num_items].label, label,
5058 sizeof(imux->items[imux->num_items].label));
5059 imux->items[imux->num_items].index = index;
5060 imux->num_items++;
5061 return 0;
5062}
5063EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005064
Takashi Iwai4a471b72005-12-07 13:56:29 +01005065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066#ifdef CONFIG_PM
5067/*
5068 * power management
5069 */
5070
5071/**
5072 * snd_hda_suspend - suspend the codecs
5073 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 *
5075 * Returns 0 if successful.
5076 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005077int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005079 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
Takashi Iwai0ba21762007-04-16 11:29:14 +02005081 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02005082#ifdef CONFIG_SND_HDA_POWER_SAVE
5083 if (!codec->power_on)
5084 continue;
5085#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02005086 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 }
5088 return 0;
5089}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005090EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
5092/**
5093 * snd_hda_resume - resume the codecs
5094 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 *
5096 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005097 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005098 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005099 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 */
5101int snd_hda_resume(struct hda_bus *bus)
5102{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005103 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Takashi Iwai0ba21762007-04-16 11:29:14 +02005105 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005106 if (codec->patch_ops.pre_resume)
5107 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005108 if (snd_hda_codec_needs_resume(codec))
5109 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 return 0;
5112}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005113EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005114#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005115
5116/*
5117 * generic arrays
5118 */
5119
Takashi Iwaid5191e52009-11-16 14:58:17 +01005120/**
5121 * snd_array_new - get a new element from the given array
5122 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005123 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005124 * Get a new element from the given array. If it exceeds the
5125 * pre-allocated array size, re-allocate the array.
5126 *
5127 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005128 */
5129void *snd_array_new(struct snd_array *array)
5130{
5131 if (array->used >= array->alloced) {
5132 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005133 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005134 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005135 void *nlist;
5136 if (snd_BUG_ON(num >= 4096))
5137 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005138 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005139 if (!nlist)
5140 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005141 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005142 array->list = nlist;
5143 array->alloced = num;
5144 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005145 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005146}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005147EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005148
Takashi Iwaid5191e52009-11-16 14:58:17 +01005149/**
5150 * snd_array_free - free the given array elements
5151 * @array: the array object
5152 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005153void snd_array_free(struct snd_array *array)
5154{
5155 kfree(array->list);
5156 array->used = 0;
5157 array->alloced = 0;
5158 array->list = NULL;
5159}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005160EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005161
Takashi Iwaid5191e52009-11-16 14:58:17 +01005162/**
5163 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5164 * @pcm: PCM caps bits
5165 * @buf: the string buffer to write
5166 * @buflen: the max buffer length
5167 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005168 * used by hda_proc.c and hda_eld.c
5169 */
5170void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5171{
5172 static unsigned int rates[] = {
5173 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5174 96000, 176400, 192000, 384000
5175 };
5176 int i, j;
5177
5178 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5179 if (pcm & (1 << i))
5180 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5181
5182 buf[j] = '\0'; /* necessary when j == 0 */
5183}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005184EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005185
Takashi Iwaid5191e52009-11-16 14:58:17 +01005186/**
5187 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5188 * @pcm: PCM caps bits
5189 * @buf: the string buffer to write
5190 * @buflen: the max buffer length
5191 *
5192 * used by hda_proc.c and hda_eld.c
5193 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005194void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5195{
5196 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5197 int i, j;
5198
5199 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5200 if (pcm & (AC_SUPPCM_BITS_8 << i))
5201 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5202
5203 buf[j] = '\0'; /* necessary when j == 0 */
5204}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005205EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005206
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005207#ifdef CONFIG_SND_HDA_INPUT_JACK
5208/*
5209 * Input-jack notification support
5210 */
5211struct hda_jack_item {
5212 hda_nid_t nid;
5213 int type;
5214 struct snd_jack *jack;
5215};
5216
5217static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5218 int type)
5219{
5220 switch (type) {
5221 case SND_JACK_HEADPHONE:
5222 return "Headphone";
5223 case SND_JACK_MICROPHONE:
5224 return "Mic";
5225 case SND_JACK_LINEOUT:
5226 return "Line-out";
5227 case SND_JACK_HEADSET:
5228 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005229 case SND_JACK_VIDEOOUT:
5230 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005231 default:
5232 return "Misc";
5233 }
5234}
5235
5236static void hda_free_jack_priv(struct snd_jack *jack)
5237{
5238 struct hda_jack_item *jacks = jack->private_data;
5239 jacks->nid = 0;
5240 jacks->jack = NULL;
5241}
5242
5243int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5244 const char *name)
5245{
5246 struct hda_jack_item *jack;
5247 int err;
5248
5249 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5250 jack = snd_array_new(&codec->jacks);
5251 if (!jack)
5252 return -ENOMEM;
5253
5254 jack->nid = nid;
5255 jack->type = type;
5256 if (!name)
5257 name = get_jack_default_name(codec, nid, type);
5258 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5259 if (err < 0) {
5260 jack->nid = 0;
5261 return err;
5262 }
5263 jack->jack->private_data = jack;
5264 jack->jack->private_free = hda_free_jack_priv;
5265 return 0;
5266}
5267EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5268
5269void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5270{
5271 struct hda_jack_item *jacks = codec->jacks.list;
5272 int i;
5273
5274 if (!jacks)
5275 return;
5276
5277 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5278 unsigned int pin_ctl;
5279 unsigned int present;
5280 int type;
5281
5282 if (jacks->nid != nid)
5283 continue;
5284 present = snd_hda_jack_detect(codec, nid);
5285 type = jacks->type;
5286 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5287 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5288 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5289 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5290 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5291 }
5292 snd_jack_report(jacks->jack, present ? type : 0);
5293 }
5294}
5295EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5296
5297/* free jack instances manually when clearing/reconfiguring */
5298void snd_hda_input_jack_free(struct hda_codec *codec)
5299{
5300 if (!codec->bus->shutdown && codec->jacks.list) {
5301 struct hda_jack_item *jacks = codec->jacks.list;
5302 int i;
5303 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5304 if (jacks->jack)
5305 snd_device_free(codec->bus->card, jacks->jack);
5306 }
5307 }
5308 snd_array_free(&codec->jacks);
5309}
5310EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5311#endif /* CONFIG_SND_HDA_INPUT_JACK */
5312
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005313MODULE_DESCRIPTION("HDA codec core");
5314MODULE_LICENSE("GPL");