blob: 056cd9ade1fb41dbc43140a64e4a2fb196558171 [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);
Takashi Iwaie581f3d2011-07-26 10:19:20 +020094#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaicb53c622007-08-10 17:21:45 +020095#else
96static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +020097#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020098#endif
99
Takashi Iwaid5191e52009-11-16 14:58:17 +0100100/**
101 * snd_hda_get_jack_location - Give a location string of the jack
102 * @cfg: pin default config value
103 *
104 * Parse the pin default config value and returns the string of the
105 * jack location, e.g. "Rear", "Front", etc.
106 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400107const char *snd_hda_get_jack_location(u32 cfg)
108{
109 static char *bases[7] = {
110 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
111 };
112 static unsigned char specials_idx[] = {
113 0x07, 0x08,
114 0x17, 0x18, 0x19,
115 0x37, 0x38
116 };
117 static char *specials[] = {
118 "Rear Panel", "Drive Bar",
119 "Riser", "HDMI", "ATAPI",
120 "Mobile-In", "Mobile-Out"
121 };
122 int i;
123 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
124 if ((cfg & 0x0f) < 7)
125 return bases[cfg & 0x0f];
126 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
127 if (cfg == specials_idx[i])
128 return specials[i];
129 }
130 return "UNKNOWN";
131}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100132EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400133
Takashi Iwaid5191e52009-11-16 14:58:17 +0100134/**
135 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
136 * @cfg: pin default config value
137 *
138 * Parse the pin default config value and returns the string of the
139 * jack connectivity, i.e. external or internal connection.
140 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400141const char *snd_hda_get_jack_connectivity(u32 cfg)
142{
143 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
144
145 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
146}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100147EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400148
Takashi Iwaid5191e52009-11-16 14:58:17 +0100149/**
150 * snd_hda_get_jack_type - Give a type string of the jack
151 * @cfg: pin default config value
152 *
153 * Parse the pin default config value and returns the string of the
154 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
155 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400156const char *snd_hda_get_jack_type(u32 cfg)
157{
158 static char *jack_types[16] = {
159 "Line Out", "Speaker", "HP Out", "CD",
160 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
161 "Line In", "Aux", "Mic", "Telephony",
162 "SPDIF In", "Digitial In", "Reserved", "Other"
163 };
164
165 return jack_types[(cfg & AC_DEFCFG_DEVICE)
166 >> AC_DEFCFG_DEVICE_SHIFT];
167}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100168EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400169
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100170/*
171 * Compose a 32bit command word to be sent to the HD-audio controller
172 */
173static inline unsigned int
174make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
175 unsigned int verb, unsigned int parm)
176{
177 u32 val;
178
Takashi Iwai82e1b802009-07-17 12:47:34 +0200179 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
180 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800181 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
182 codec->addr, direct, nid, verb, parm);
183 return ~0;
184 }
185
186 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100187 val |= (u32)direct << 27;
188 val |= (u32)nid << 20;
189 val |= verb << 8;
190 val |= parm;
191 return val;
192}
193
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200194/*
195 * Send and receive a verb
196 */
197static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
198 unsigned int *res)
199{
200 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200201 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200202
Wu Fengguang6430aee2009-07-17 16:49:19 +0800203 if (cmd == ~0)
204 return -1;
205
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206 if (res)
207 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200208 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200209 snd_hda_power_up(codec);
210 mutex_lock(&bus->cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200211 err = bus->ops.command(bus, cmd);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200212 if (!err && res)
Wu Fengguangdeadff12009-08-01 18:45:16 +0800213 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200214 mutex_unlock(&bus->cmd_mutex);
215 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200216 if (res && *res == -1 && bus->rirb_error) {
217 if (bus->response_reset) {
218 snd_printd("hda_codec: resetting BUS due to "
219 "fatal communication error\n");
220 bus->ops.bus_reset(bus);
221 }
222 goto again;
223 }
224 /* clear reset-flag when the communication gets recovered */
225 if (!err)
226 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200227 return err;
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/**
231 * snd_hda_codec_read - send a command and get the response
232 * @codec: the HDA codec
233 * @nid: NID to send the command
234 * @direct: direct flag
235 * @verb: the verb to send
236 * @parm: the parameter for the verb
237 *
238 * Send a single command and read the corresponding response.
239 *
240 * Returns the obtained response value, or -1 for an error.
241 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200242unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
243 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 unsigned int verb, unsigned int parm)
245{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200246 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
247 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700248 if (codec_exec_verb(codec, cmd, &res))
249 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return res;
251}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100252EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254/**
255 * snd_hda_codec_write - send a single command without waiting for response
256 * @codec: the HDA codec
257 * @nid: NID to send the command
258 * @direct: direct flag
259 * @verb: the verb to send
260 * @parm: the parameter for the verb
261 *
262 * Send a single command without waiting for response.
263 *
264 * Returns 0 if successful, or a negative error code.
265 */
266int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
267 unsigned int verb, unsigned int parm)
268{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200269 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100270 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200271 return codec_exec_verb(codec, cmd,
272 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100274EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/**
277 * snd_hda_sequence_write - sequence writes
278 * @codec: the HDA codec
279 * @seq: VERB array to send
280 *
281 * Send the commands sequentially from the given array.
282 * The array must be terminated with NID=0.
283 */
284void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
285{
286 for (; seq->nid; seq++)
287 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
288}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100289EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/**
292 * snd_hda_get_sub_nodes - get the range of sub nodes
293 * @codec: the HDA codec
294 * @nid: NID to parse
295 * @start_id: the pointer to store the start NID
296 *
297 * Parse the NID and store the start NID of its sub-nodes.
298 * Returns the number of sub-nodes.
299 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200300int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
301 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 unsigned int parm;
304
305 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200306 if (parm == -1)
307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 *start_id = (parm >> 16) & 0x7fff;
309 return (int)(parm & 0x7fff);
310}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100311EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200313/* look up the cached results */
314static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
315{
316 int i, len;
317 for (i = 0; i < array->used; ) {
318 hda_nid_t *p = snd_array_elem(array, i);
319 if (nid == *p)
320 return p;
321 len = p[1];
322 i += len + 2;
323 }
324 return NULL;
325}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200328 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * @codec: the HDA codec
330 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200331 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
333 * Parses the connection list of the given widget and stores the list
334 * of NIDs.
335 *
336 * Returns the number of connections, or a negative error code.
337 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200338int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
339 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200340{
341 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200342 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200343 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200344 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200345 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200346
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200347 again:
348 /* if the connection-list is already cached, read it */
349 p = lookup_conn_list(array, nid);
350 if (p) {
351 if (listp)
352 *listp = p + 2;
353 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200354 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355 if (snd_BUG_ON(added))
356 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200357
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200358 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200359 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200360 if (len < 0)
361 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200362 err = snd_hda_override_conn_list(codec, nid, len, list);
363 if (err < 0)
364 return err;
365 added = true;
366 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200367}
Takashi Iwaidce20792011-06-24 14:10:28 +0200368EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
369
370/**
371 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * @codec: the HDA codec
373 * @nid: NID to parse
374 * @conn_list: connection list array
375 * @max_conns: max. number of connections to store
376 *
377 * Parses the connection list of the given widget and stores the list
378 * of NIDs.
379 *
380 * Returns the number of connections, or a negative error code.
381 */
382int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200383 hda_nid_t *conn_list, int max_conns)
384{
Takashi Iwaidce20792011-06-24 14:10:28 +0200385 const hda_nid_t *list;
386 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200387
Takashi Iwaidce20792011-06-24 14:10:28 +0200388 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200389 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200390 if (len > max_conns) {
391 snd_printk(KERN_ERR "hda_codec: "
392 "Too many connections %d for NID 0x%x\n",
393 len, nid);
394 return -EINVAL;
395 }
396 memcpy(conn_list, list, len * sizeof(hda_nid_t));
397 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200398}
399EXPORT_SYMBOL_HDA(snd_hda_get_connections);
400
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200401/**
402 * snd_hda_get_raw_connections - copy connection list without cache
403 * @codec: the HDA codec
404 * @nid: NID to parse
405 * @conn_list: connection list array
406 * @max_conns: max. number of connections to store
407 *
408 * Like snd_hda_get_connections(), copy the connection list but without
409 * checking through the connection-list cache.
410 * Currently called only from hda_proc.c, so not exported.
411 */
412int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
413 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100416 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200418 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100419 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Takashi Iwaida3cec32008-08-08 17:12:14 +0200421 if (snd_BUG_ON(!conn_list || max_conns <= 0))
422 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200424 wcaps = get_wcaps(codec, nid);
425 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200426 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
427 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
430 if (parm & AC_CLIST_LONG) {
431 /* long form */
432 shift = 16;
433 num_elems = 2;
434 } else {
435 /* short form */
436 shift = 8;
437 num_elems = 4;
438 }
439 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 mask = (1 << (shift-1)) - 1;
441
Takashi Iwai0ba21762007-04-16 11:29:14 +0200442 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0; /* no connection */
444
445 if (conn_len == 1) {
446 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200447 parm = snd_hda_codec_read(codec, nid, 0,
448 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200449 if (parm == -1 && codec->bus->rirb_error)
450 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 conn_list[0] = parm & mask;
452 return 1;
453 }
454
455 /* multi connection */
456 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100457 prev_nid = 0;
458 for (i = 0; i < conn_len; i++) {
459 int range_val;
460 hda_nid_t val, n;
461
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200462 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100463 parm = snd_hda_codec_read(codec, nid, 0,
464 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200465 if (parm == -1 && codec->bus->rirb_error)
466 return -EIO;
467 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200468 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100469 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200470 if (val == 0) {
471 snd_printk(KERN_WARNING "hda_codec: "
472 "invalid CONNECT_LIST verb %x[%i]:%x\n",
473 nid, i, parm);
474 return 0;
475 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100476 parm >>= shift;
477 if (range_val) {
478 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200479 if (!prev_nid || prev_nid >= val) {
480 snd_printk(KERN_WARNING "hda_codec: "
481 "invalid dep_range_val %x:%x\n",
482 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100483 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100485 for (n = prev_nid + 1; n <= val; n++) {
486 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200487 snd_printk(KERN_ERR "hda_codec: "
488 "Too many connections %d for NID 0x%x\n",
489 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 return -EINVAL;
491 }
492 conn_list[conns++] = n;
493 }
494 } else {
495 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200496 snd_printk(KERN_ERR "hda_codec: "
497 "Too many connections %d for NID 0x%x\n",
498 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100499 return -EINVAL;
500 }
501 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100503 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 return conns;
506}
507
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200508static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
509{
510 hda_nid_t *p = snd_array_new(array);
511 if (!p)
512 return false;
513 *p = nid;
514 return true;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200518 * snd_hda_override_conn_list - add/modify the connection-list to cache
519 * @codec: the HDA codec
520 * @nid: NID to parse
521 * @len: number of connection list entries
522 * @list: the list of connection entries
523 *
524 * Add or modify the given connection-list to the cache. If the corresponding
525 * cache already exists, invalidate it and append a new one.
526 *
527 * Returns zero or a negative error code.
528 */
529int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
530 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200532 struct snd_array *array = &codec->conn_lists;
533 hda_nid_t *p;
534 int i, old_used;
535
536 p = lookup_conn_list(array, nid);
537 if (p)
538 *p = -1; /* invalidate the old entry */
539
540 old_used = array->used;
541 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
542 goto error_add;
543 for (i = 0; i < len; i++)
544 if (!add_conn_list(array, list[i]))
545 goto error_add;
546 return 0;
547
548 error_add:
549 array->used = old_used;
550 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200552EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
553
554/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200555 * snd_hda_get_conn_index - get the connection index of the given NID
556 * @codec: the HDA codec
557 * @mux: NID containing the list
558 * @nid: NID to select
559 * @recursive: 1 when searching NID recursively, otherwise 0
560 *
561 * Parses the connection list of the widget @mux and checks whether the
562 * widget @nid is present. If it is, return the connection index.
563 * Otherwise it returns -1.
564 */
565int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
566 hda_nid_t nid, int recursive)
567{
568 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
569 int i, nums;
570
571 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
572 for (i = 0; i < nums; i++)
573 if (conn[i] == nid)
574 return i;
575 if (!recursive)
576 return -1;
577 if (recursive > 5) {
578 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
579 return -1;
580 }
581 recursive++;
582 for (i = 0; i < nums; i++)
583 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
584 return i;
585 return -1;
586}
587EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/**
590 * snd_hda_queue_unsol_event - add an unsolicited event to queue
591 * @bus: the BUS
592 * @res: unsolicited event (lower 32bit of RIRB entry)
593 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
594 *
595 * Adds the given event to the queue. The events are processed in
596 * the workqueue asynchronously. Call this function in the interrupt
597 * hanlder when RIRB receives an unsolicited event.
598 *
599 * Returns 0 if successful, or a negative error code.
600 */
601int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
602{
603 struct hda_bus_unsolicited *unsol;
604 unsigned int wp;
605
Takashi Iwai0ba21762007-04-16 11:29:14 +0200606 unsol = bus->unsol;
607 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 0;
609
610 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
611 unsol->wp = wp;
612
613 wp <<= 1;
614 unsol->queue[wp] = res;
615 unsol->queue[wp + 1] = res_ex;
616
Takashi Iwai6acaed32009-01-12 10:09:24 +0100617 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 return 0;
620}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100621EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800624 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
David Howellsc4028952006-11-22 14:57:56 +0000626static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
David Howellsc4028952006-11-22 14:57:56 +0000628 struct hda_bus_unsolicited *unsol =
629 container_of(work, struct hda_bus_unsolicited, work);
630 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct hda_codec *codec;
632 unsigned int rp, caddr, res;
633
634 while (unsol->rp != unsol->wp) {
635 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
636 unsol->rp = rp;
637 rp <<= 1;
638 res = unsol->queue[rp];
639 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200640 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 continue;
642 codec = bus->caddr_tbl[caddr & 0x0f];
643 if (codec && codec->patch_ops.unsol_event)
644 codec->patch_ops.unsol_event(codec, res);
645 }
646}
647
648/*
649 * initialize unsolicited queue
650 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200651static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct hda_bus_unsolicited *unsol;
654
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100655 if (bus->unsol) /* already initialized */
656 return 0;
657
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200658 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200659 if (!unsol) {
660 snd_printk(KERN_ERR "hda_codec: "
661 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -ENOMEM;
663 }
David Howellsc4028952006-11-22 14:57:56 +0000664 INIT_WORK(&unsol->work, process_unsol_events);
665 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 bus->unsol = unsol;
667 return 0;
668}
669
670/*
671 * destructor
672 */
673static void snd_hda_codec_free(struct hda_codec *codec);
674
675static int snd_hda_bus_free(struct hda_bus *bus)
676{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200677 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Takashi Iwai0ba21762007-04-16 11:29:14 +0200679 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100681 if (bus->workq)
682 flush_workqueue(bus->workq);
683 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200685 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 snd_hda_codec_free(codec);
687 }
688 if (bus->ops.private_free)
689 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100690 if (bus->workq)
691 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 kfree(bus);
693 return 0;
694}
695
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100696static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100699 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return snd_hda_bus_free(bus);
701}
702
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200703#ifdef CONFIG_SND_HDA_HWDEP
704static int snd_hda_bus_dev_register(struct snd_device *device)
705{
706 struct hda_bus *bus = device->device_data;
707 struct hda_codec *codec;
708 list_for_each_entry(codec, &bus->codec_list, list) {
709 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100710 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200711 }
712 return 0;
713}
714#else
715#define snd_hda_bus_dev_register NULL
716#endif
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718/**
719 * snd_hda_bus_new - create a HDA bus
720 * @card: the card entry
721 * @temp: the template for hda_bus information
722 * @busp: the pointer to store the created bus instance
723 *
724 * Returns 0 if successful, or a negative error code.
725 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100726int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200727 const struct hda_bus_template *temp,
728 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct hda_bus *bus;
731 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100732 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200733 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 .dev_free = snd_hda_bus_dev_free,
735 };
736
Takashi Iwaida3cec32008-08-08 17:12:14 +0200737 if (snd_BUG_ON(!temp))
738 return -EINVAL;
739 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
740 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (busp)
743 *busp = NULL;
744
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200745 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (bus == NULL) {
747 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
748 return -ENOMEM;
749 }
750
751 bus->card = card;
752 bus->private_data = temp->private_data;
753 bus->pci = temp->pci;
754 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100755 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 bus->ops = temp->ops;
757
Ingo Molnar62932df2006-01-16 16:34:20 +0100758 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200759 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 INIT_LIST_HEAD(&bus->codec_list);
761
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100762 snprintf(bus->workq_name, sizeof(bus->workq_name),
763 "hd-audio%d", card->number);
764 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100765 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100766 snd_printk(KERN_ERR "cannot create workqueue %s\n",
767 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100768 kfree(bus);
769 return -ENOMEM;
770 }
771
Takashi Iwai0ba21762007-04-16 11:29:14 +0200772 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
773 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 snd_hda_bus_free(bus);
775 return err;
776 }
777 if (busp)
778 *busp = bus;
779 return 0;
780}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100781EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Takashi Iwai82467612007-07-27 19:15:54 +0200783#ifdef CONFIG_SND_HDA_GENERIC
784#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200785 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200786#else
787#define is_generic_config(codec) 0
788#endif
789
Takashi Iwai645f10c2008-11-28 15:07:37 +0100790#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100791#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
792#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100793#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100794#endif
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/*
797 * find a matching codec preset
798 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200799static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200800find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100802 struct hda_codec_preset_list *tbl;
803 const struct hda_codec_preset *preset;
804 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Takashi Iwai82467612007-07-27 19:15:54 +0200806 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100807 return NULL; /* use the generic parser */
808
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809 again:
810 mutex_lock(&preset_mutex);
811 list_for_each_entry(tbl, &hda_preset_tables, list) {
812 if (!try_module_get(tbl->owner)) {
813 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
814 continue;
815 }
816 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100818 if (preset->afg && preset->afg != codec->afg)
819 continue;
820 if (preset->mfg && preset->mfg != codec->mfg)
821 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200822 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200824 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200825 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100826 preset->rev == codec->revision_id)) {
827 mutex_unlock(&preset_mutex);
828 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100832 module_put(tbl->owner);
833 }
834 mutex_unlock(&preset_mutex);
835
836 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
837 char name[32];
838 if (!mod_requested)
839 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
840 codec->vendor_id);
841 else
842 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
843 (codec->vendor_id >> 16) & 0xffff);
844 request_module(name);
845 mod_requested++;
846 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848 return NULL;
849}
850
851/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200852 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200854static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 const struct hda_vendor_id *c;
857 const char *vendor = NULL;
858 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200859 char tmp[16];
860
861 if (codec->vendor_name)
862 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 for (c = hda_vendor_ids; c->id; c++) {
865 if (c->id == vendor_id) {
866 vendor = c->name;
867 break;
868 }
869 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200870 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 sprintf(tmp, "Generic %04x", vendor_id);
872 vendor = tmp;
873 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200874 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
875 if (!codec->vendor_name)
876 return -ENOMEM;
877
878 get_chip_name:
879 if (codec->chip_name)
880 return 0;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200883 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
884 else {
885 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
886 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
887 }
888 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200889 return -ENOMEM;
890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200894 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100896static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200898 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 hda_nid_t nid;
900
901 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
902 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200903 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200904 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200905 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200906 case AC_GRP_AUDIO_FUNCTION:
907 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200908 codec->afg_function_id = function_id & 0xff;
909 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200910 break;
911 case AC_GRP_MODEM_FUNCTION:
912 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200913 codec->mfg_function_id = function_id & 0xff;
914 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200915 break;
916 default:
917 break;
918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100923 * read widget caps for each widget and store in cache
924 */
925static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
926{
927 int i;
928 hda_nid_t nid;
929
930 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
931 &codec->start_nid);
932 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200933 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100934 return -ENOMEM;
935 nid = codec->start_nid;
936 for (i = 0; i < codec->num_nodes; i++, nid++)
937 codec->wcaps[i] = snd_hda_param_read(codec, nid,
938 AC_PAR_AUDIO_WIDGET_CAP);
939 return 0;
940}
941
Takashi Iwai3be14142009-02-20 14:11:16 +0100942/* read all pin default configurations and save codec->init_pins */
943static int read_pin_defaults(struct hda_codec *codec)
944{
945 int i;
946 hda_nid_t nid = codec->start_nid;
947
948 for (i = 0; i < codec->num_nodes; i++, nid++) {
949 struct hda_pincfg *pin;
950 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200951 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100952 if (wid_type != AC_WID_PIN)
953 continue;
954 pin = snd_array_new(&codec->init_pins);
955 if (!pin)
956 return -ENOMEM;
957 pin->nid = nid;
958 pin->cfg = snd_hda_codec_read(codec, nid, 0,
959 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200960 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
961 AC_VERB_GET_PIN_WIDGET_CONTROL,
962 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100963 }
964 return 0;
965}
966
967/* look up the given pin config list and return the item matching with NID */
968static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
969 struct snd_array *array,
970 hda_nid_t nid)
971{
972 int i;
973 for (i = 0; i < array->used; i++) {
974 struct hda_pincfg *pin = snd_array_elem(array, i);
975 if (pin->nid == nid)
976 return pin;
977 }
978 return NULL;
979}
980
981/* write a config value for the given NID */
982static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
983 unsigned int cfg)
984{
985 int i;
986 for (i = 0; i < 4; i++) {
987 snd_hda_codec_write(codec, nid, 0,
988 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
989 cfg & 0xff);
990 cfg >>= 8;
991 }
992}
993
994/* set the current pin config value for the given NID.
995 * the value is cached, and read via snd_hda_codec_get_pincfg()
996 */
997int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
998 hda_nid_t nid, unsigned int cfg)
999{
1000 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001001 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001002
Takashi Iwaib82855a2009-12-27 11:24:56 +01001003 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1004 return -EINVAL;
1005
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001006 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001007 pin = look_up_pincfg(codec, list, nid);
1008 if (!pin) {
1009 pin = snd_array_new(list);
1010 if (!pin)
1011 return -ENOMEM;
1012 pin->nid = nid;
1013 }
1014 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001015
1016 /* change only when needed; e.g. if the pincfg is already present
1017 * in user_pins[], don't write it
1018 */
1019 cfg = snd_hda_codec_get_pincfg(codec, nid);
1020 if (oldcfg != cfg)
1021 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001022 return 0;
1023}
1024
Takashi Iwaid5191e52009-11-16 14:58:17 +01001025/**
1026 * snd_hda_codec_set_pincfg - Override a pin default configuration
1027 * @codec: the HDA codec
1028 * @nid: NID to set the pin config
1029 * @cfg: the pin default config value
1030 *
1031 * Override a pin default configuration value in the cache.
1032 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1033 * priority than the real hardware value.
1034 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001035int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1036 hda_nid_t nid, unsigned int cfg)
1037{
Takashi Iwai346ff702009-02-23 09:42:57 +01001038 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001039}
1040EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1041
Takashi Iwaid5191e52009-11-16 14:58:17 +01001042/**
1043 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1044 * @codec: the HDA codec
1045 * @nid: NID to get the pin config
1046 *
1047 * Get the current pin config value of the given pin NID.
1048 * If the pincfg value is cached or overridden via sysfs or driver,
1049 * returns the cached value.
1050 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001051unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1052{
1053 struct hda_pincfg *pin;
1054
Takashi Iwai3be14142009-02-20 14:11:16 +01001055#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001056 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001057 if (pin)
1058 return pin->cfg;
1059#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001060 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1061 if (pin)
1062 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001063 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1064 if (pin)
1065 return pin->cfg;
1066 return 0;
1067}
1068EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1069
1070/* restore all current pin configs */
1071static void restore_pincfgs(struct hda_codec *codec)
1072{
1073 int i;
1074 for (i = 0; i < codec->init_pins.used; i++) {
1075 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1076 set_pincfg(codec, pin->nid,
1077 snd_hda_codec_get_pincfg(codec, pin->nid));
1078 }
1079}
Takashi Iwai54d17402005-11-21 16:33:22 +01001080
Takashi Iwai92ee6162009-12-27 11:18:59 +01001081/**
1082 * snd_hda_shutup_pins - Shut up all pins
1083 * @codec: the HDA codec
1084 *
1085 * Clear all pin controls to shup up before suspend for avoiding click noise.
1086 * The controls aren't cached so that they can be resumed properly.
1087 */
1088void snd_hda_shutup_pins(struct hda_codec *codec)
1089{
1090 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001091 /* don't shut up pins when unloading the driver; otherwise it breaks
1092 * the default pin setup at the next load of the driver
1093 */
1094 if (codec->bus->shutdown)
1095 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001096 for (i = 0; i < codec->init_pins.used; i++) {
1097 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1098 /* use read here for syncing after issuing each verb */
1099 snd_hda_codec_read(codec, pin->nid, 0,
1100 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1101 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001102 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001103}
1104EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1105
Takashi Iwai2a439522011-07-26 09:52:50 +02001106#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001107/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1108static void restore_shutup_pins(struct hda_codec *codec)
1109{
1110 int i;
1111 if (!codec->pins_shutup)
1112 return;
1113 if (codec->bus->shutdown)
1114 return;
1115 for (i = 0; i < codec->init_pins.used; i++) {
1116 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1117 snd_hda_codec_write(codec, pin->nid, 0,
1118 AC_VERB_SET_PIN_WIDGET_CONTROL,
1119 pin->ctrl);
1120 }
1121 codec->pins_shutup = 0;
1122}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001123#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001124
Takashi Iwai01751f52007-08-10 16:59:39 +02001125static void init_hda_cache(struct hda_cache_rec *cache,
1126 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001127static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001128
Takashi Iwai3be14142009-02-20 14:11:16 +01001129/* restore the initial pin cfgs and release all pincfg lists */
1130static void restore_init_pincfgs(struct hda_codec *codec)
1131{
Takashi Iwai346ff702009-02-23 09:42:57 +01001132 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001133 * so that only the values in init_pins are restored
1134 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001135 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001136#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001137 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001138#endif
1139 restore_pincfgs(codec);
1140 snd_array_free(&codec->init_pins);
1141}
1142
Takashi Iwai54d17402005-11-21 16:33:22 +01001143/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001144 * audio-converter setup caches
1145 */
1146struct hda_cvt_setup {
1147 hda_nid_t nid;
1148 u8 stream_tag;
1149 u8 channel_id;
1150 u16 format_id;
1151 unsigned char active; /* cvt is currently used */
1152 unsigned char dirty; /* setups should be cleared */
1153};
1154
1155/* get or create a cache entry for the given audio converter NID */
1156static struct hda_cvt_setup *
1157get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1158{
1159 struct hda_cvt_setup *p;
1160 int i;
1161
1162 for (i = 0; i < codec->cvt_setups.used; i++) {
1163 p = snd_array_elem(&codec->cvt_setups, i);
1164 if (p->nid == nid)
1165 return p;
1166 }
1167 p = snd_array_new(&codec->cvt_setups);
1168 if (p)
1169 p->nid = nid;
1170 return p;
1171}
1172
1173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 * codec destructor
1175 */
1176static void snd_hda_codec_free(struct hda_codec *codec)
1177{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001178 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001180 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001181#ifdef CONFIG_SND_HDA_POWER_SAVE
1182 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001183 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001186 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001187 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001188 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001189 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 codec->bus->caddr_tbl[codec->addr] = NULL;
1191 if (codec->patch_ops.free)
1192 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001193 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001194 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001195 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001196 kfree(codec->vendor_name);
1197 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001198 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001199 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 kfree(codec);
1201}
1202
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001203static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1204 unsigned int power_state);
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206/**
1207 * snd_hda_codec_new - create a HDA codec
1208 * @bus: the bus to assign
1209 * @codec_addr: the codec address
1210 * @codecp: the pointer to store the generated codec
1211 *
1212 * Returns 0 if successful, or a negative error code.
1213 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001214int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1215 unsigned int codec_addr,
1216 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001219 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int err;
1221
Takashi Iwaida3cec32008-08-08 17:12:14 +02001222 if (snd_BUG_ON(!bus))
1223 return -EINVAL;
1224 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001228 snd_printk(KERN_ERR "hda_codec: "
1229 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -EBUSY;
1231 }
1232
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001233 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (codec == NULL) {
1235 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1236 return -ENOMEM;
1237 }
1238
1239 codec->bus = bus;
1240 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001241 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001242 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001243 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001244 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001245 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1246 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001247 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001248 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001249 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001250 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001251 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001252 if (codec->bus->modelname) {
1253 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1254 if (!codec->modelname) {
1255 snd_hda_codec_free(codec);
1256 return -ENODEV;
1257 }
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Takashi Iwaicb53c622007-08-10 17:21:45 +02001260#ifdef CONFIG_SND_HDA_POWER_SAVE
1261 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1262 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1263 * the caller has to power down appropriatley after initialization
1264 * phase.
1265 */
1266 hda_keep_power_on(codec);
1267#endif
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 list_add_tail(&codec->list, &bus->codec_list);
1270 bus->caddr_tbl[codec_addr] = codec;
1271
Takashi Iwai0ba21762007-04-16 11:29:14 +02001272 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1273 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001274 if (codec->vendor_id == -1)
1275 /* read again, hopefully the access method was corrected
1276 * in the last read...
1277 */
1278 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1279 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001280 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1281 AC_PAR_SUBSYSTEM_ID);
1282 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1283 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001285 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001286 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001287 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001288 err = -ENODEV;
1289 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291
Takashi Iwai3be14142009-02-20 14:11:16 +01001292 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1293 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001294 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001295 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001296 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001297 err = read_pin_defaults(codec);
1298 if (err < 0)
1299 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001300
Takashi Iwai0ba21762007-04-16 11:29:14 +02001301 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001302 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001303 codec->subsystem_id =
1304 snd_hda_codec_read(codec, nid, 0,
1305 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001306 }
1307
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001308 /* power-up all before initialization */
1309 hda_set_power_state(codec,
1310 codec->afg ? codec->afg : codec->mfg,
1311 AC_PWRST_D0);
1312
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001313 snd_hda_codec_proc_new(codec);
1314
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001315 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001316
1317 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1318 codec->subsystem_id, codec->revision_id);
1319 snd_component_add(codec->bus->card, component);
1320
1321 if (codecp)
1322 *codecp = codec;
1323 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001324
1325 error:
1326 snd_hda_codec_free(codec);
1327 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001329EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330
Takashi Iwaid5191e52009-11-16 14:58:17 +01001331/**
1332 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1333 * @codec: the HDA codec
1334 *
1335 * Start parsing of the given codec tree and (re-)initialize the whole
1336 * patch instance.
1337 *
1338 * Returns 0 if successful or a negative error code.
1339 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001340int snd_hda_codec_configure(struct hda_codec *codec)
1341{
1342 int err;
1343
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001344 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001345 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001346 err = get_codec_name(codec);
1347 if (err < 0)
1348 return err;
1349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Takashi Iwai82467612007-07-27 19:15:54 +02001351 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001353 goto patched;
1354 }
Takashi Iwai82467612007-07-27 19:15:54 +02001355 if (codec->preset && codec->preset->patch) {
1356 err = codec->preset->patch(codec);
1357 goto patched;
1358 }
1359
1360 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001361 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001362 if (err < 0)
1363 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001364
1365 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001366 if (!err && codec->patch_ops.unsol_event)
1367 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001368 /* audio codec should override the mixer name */
1369 if (!err && (codec->afg || !*codec->bus->card->mixername))
1370 snprintf(codec->bus->card->mixername,
1371 sizeof(codec->bus->card->mixername),
1372 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001373 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001375EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377/**
1378 * snd_hda_codec_setup_stream - set up the codec for streaming
1379 * @codec: the CODEC to set up
1380 * @nid: the NID to set up
1381 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1382 * @channel_id: channel id to pass, zero based.
1383 * @format: stream format.
1384 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001385void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1386 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 int channel_id, int format)
1388{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001389 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001390 struct hda_cvt_setup *p;
1391 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001392 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001393 int i;
1394
Takashi Iwai0ba21762007-04-16 11:29:14 +02001395 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001396 return;
1397
Takashi Iwai0ba21762007-04-16 11:29:14 +02001398 snd_printdd("hda_codec_setup_stream: "
1399 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001401 p = get_hda_cvt_setup(codec, nid);
1402 if (!p)
1403 return;
1404 /* update the stream-id if changed */
1405 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1406 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1407 newval = (stream_tag << 4) | channel_id;
1408 if (oldval != newval)
1409 snd_hda_codec_write(codec, nid, 0,
1410 AC_VERB_SET_CHANNEL_STREAMID,
1411 newval);
1412 p->stream_tag = stream_tag;
1413 p->channel_id = channel_id;
1414 }
1415 /* update the format-id if changed */
1416 if (p->format_id != format) {
1417 oldval = snd_hda_codec_read(codec, nid, 0,
1418 AC_VERB_GET_STREAM_FORMAT, 0);
1419 if (oldval != format) {
1420 msleep(1);
1421 snd_hda_codec_write(codec, nid, 0,
1422 AC_VERB_SET_STREAM_FORMAT,
1423 format);
1424 }
1425 p->format_id = format;
1426 }
1427 p->active = 1;
1428 p->dirty = 0;
1429
1430 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001431 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001432 list_for_each_entry(c, &codec->bus->codec_list, list) {
1433 for (i = 0; i < c->cvt_setups.used; i++) {
1434 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001435 if (!p->active && p->stream_tag == stream_tag &&
1436 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001437 p->dirty = 1;
1438 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001441EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Takashi Iwaif0cea792010-08-13 11:56:53 +02001443static void really_cleanup_stream(struct hda_codec *codec,
1444 struct hda_cvt_setup *q);
1445
Takashi Iwaid5191e52009-11-16 14:58:17 +01001446/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001447 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001448 * @codec: the CODEC to clean up
1449 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001450 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001451 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001452void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1453 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001454{
Takashi Iwaieb541332010-08-06 13:48:11 +02001455 struct hda_cvt_setup *p;
1456
Takashi Iwai888afa12008-03-18 09:57:50 +01001457 if (!nid)
1458 return;
1459
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001460 if (codec->no_sticky_stream)
1461 do_now = 1;
1462
Takashi Iwai888afa12008-03-18 09:57:50 +01001463 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001464 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001465 if (p) {
1466 /* here we just clear the active flag when do_now isn't set;
1467 * actual clean-ups will be done later in
1468 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1469 */
1470 if (do_now)
1471 really_cleanup_stream(codec, p);
1472 else
1473 p->active = 0;
1474 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001475}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001476EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001477
Takashi Iwaieb541332010-08-06 13:48:11 +02001478static void really_cleanup_stream(struct hda_codec *codec,
1479 struct hda_cvt_setup *q)
1480{
1481 hda_nid_t nid = q->nid;
1482 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1483 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1484 memset(q, 0, sizeof(*q));
1485 q->nid = nid;
1486}
1487
1488/* clean up the all conflicting obsolete streams */
1489static void purify_inactive_streams(struct hda_codec *codec)
1490{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001491 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001492 int i;
1493
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001494 list_for_each_entry(c, &codec->bus->codec_list, list) {
1495 for (i = 0; i < c->cvt_setups.used; i++) {
1496 struct hda_cvt_setup *p;
1497 p = snd_array_elem(&c->cvt_setups, i);
1498 if (p->dirty)
1499 really_cleanup_stream(c, p);
1500 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001501 }
1502}
1503
Takashi Iwai2a439522011-07-26 09:52:50 +02001504#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001505/* clean up all streams; called from suspend */
1506static void hda_cleanup_all_streams(struct hda_codec *codec)
1507{
1508 int i;
1509
1510 for (i = 0; i < codec->cvt_setups.used; i++) {
1511 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1512 if (p->stream_tag)
1513 really_cleanup_stream(codec, p);
1514 }
1515}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001516#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518/*
1519 * amp access functions
1520 */
1521
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001522/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001523#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001524#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001525#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1526#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001528#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001531static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001532 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Takashi Iwai01751f52007-08-10 16:59:39 +02001534 memset(cache, 0, sizeof(*cache));
1535 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001536 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001537}
1538
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001539static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001540{
Takashi Iwai603c4012008-07-30 15:01:44 +02001541 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001545static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Takashi Iwai01751f52007-08-10 16:59:39 +02001547 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1548 u16 cur = cache->hash[idx];
1549 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001552 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (info->key == key)
1554 return info;
1555 cur = info->next;
1556 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001557 return NULL;
1558}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001560/* query the hash. allocate an entry if not found. */
1561static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1562 u32 key)
1563{
1564 struct hda_cache_head *info = get_hash(cache, key);
1565 if (!info) {
1566 u16 idx, cur;
1567 /* add a new hash entry */
1568 info = snd_array_new(&cache->buf);
1569 if (!info)
1570 return NULL;
1571 cur = snd_array_index(&cache->buf, info);
1572 info->key = key;
1573 info->val = 0;
1574 idx = key % (u16)ARRAY_SIZE(cache->hash);
1575 info->next = cache->hash[idx];
1576 cache->hash[idx] = cur;
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return info;
1579}
1580
Takashi Iwai01751f52007-08-10 16:59:39 +02001581/* query and allocate an amp hash entry */
1582static inline struct hda_amp_info *
1583get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1584{
1585 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1586}
1587
Takashi Iwaid5191e52009-11-16 14:58:17 +01001588/**
1589 * query_amp_caps - query AMP capabilities
1590 * @codec: the HD-auio codec
1591 * @nid: the NID to query
1592 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1593 *
1594 * Query AMP capabilities for the given widget and direction.
1595 * Returns the obtained capability bits.
1596 *
1597 * When cap bits have been already read, this doesn't read again but
1598 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001600u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001602 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Takashi Iwai0ba21762007-04-16 11:29:14 +02001604 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1605 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001607 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001608 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001610 info->amp_caps = snd_hda_param_read(codec, nid,
1611 direction == HDA_OUTPUT ?
1612 AC_PAR_AMP_OUT_CAP :
1613 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001614 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001615 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 return info->amp_caps;
1618}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001619EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Takashi Iwaid5191e52009-11-16 14:58:17 +01001621/**
1622 * snd_hda_override_amp_caps - Override the AMP capabilities
1623 * @codec: the CODEC to clean up
1624 * @nid: the NID to clean up
1625 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1626 * @caps: the capability bits to set
1627 *
1628 * Override the cached AMP caps bits value by the given one.
1629 * This function is useful if the driver needs to adjust the AMP ranges,
1630 * e.g. limit to 0dB, etc.
1631 *
1632 * Returns zero if successful or a negative error code.
1633 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001634int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1635 unsigned int caps)
1636{
1637 struct hda_amp_info *info;
1638
1639 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1640 if (!info)
1641 return -EINVAL;
1642 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001643 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001644 return 0;
1645}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001646EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001647
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001648static unsigned int
1649query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1650 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001651{
1652 struct hda_amp_info *info;
1653
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001654 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001655 if (!info)
1656 return 0;
1657 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001658 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001659 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001660 }
1661 return info->amp_caps;
1662}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001663
1664static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1665{
1666 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1667}
1668
Takashi Iwaid5191e52009-11-16 14:58:17 +01001669/**
1670 * snd_hda_query_pin_caps - Query PIN capabilities
1671 * @codec: the HD-auio codec
1672 * @nid: the NID to query
1673 *
1674 * Query PIN capabilities for the given widget.
1675 * Returns the obtained capability bits.
1676 *
1677 * When cap bits have been already read, this doesn't read again but
1678 * returns the cached value.
1679 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001680u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1681{
1682 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1683 read_pin_cap);
1684}
Takashi Iwai1327a322009-03-23 13:07:47 +01001685EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1686
Wu Fengguang864f92b2009-11-18 12:38:02 +08001687/**
1688 * snd_hda_pin_sense - execute pin sense measurement
1689 * @codec: the CODEC to sense
1690 * @nid: the pin NID to sense
1691 *
1692 * Execute necessary pin sense measurement and return its Presence Detect,
1693 * Impedance, ELD Valid etc. status bits.
1694 */
1695u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1696{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001697 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001698
Takashi Iwai729d55b2009-12-25 22:49:01 +01001699 if (!codec->no_trigger_sense) {
1700 pincap = snd_hda_query_pin_caps(codec, nid);
1701 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001702 snd_hda_codec_read(codec, nid, 0,
1703 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001704 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001705 return snd_hda_codec_read(codec, nid, 0,
1706 AC_VERB_GET_PIN_SENSE, 0);
1707}
1708EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1709
1710/**
1711 * snd_hda_jack_detect - query pin Presence Detect status
1712 * @codec: the CODEC to sense
1713 * @nid: the pin NID to sense
1714 *
1715 * Query and return the pin's Presence Detect status.
1716 */
1717int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1718{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001719 u32 sense = snd_hda_pin_sense(codec, nid);
1720 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001721}
1722EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724/*
1725 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001726 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001728static unsigned int get_vol_mute(struct hda_codec *codec,
1729 struct hda_amp_info *info, hda_nid_t nid,
1730 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 u32 val, parm;
1733
Takashi Iwai01751f52007-08-10 16:59:39 +02001734 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001735 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1738 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1739 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001740 val = snd_hda_codec_read(codec, nid, 0,
1741 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001743 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001744 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001748 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001750static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001751 hda_nid_t nid, int ch, int direction, int index,
1752 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
1754 u32 parm;
1755
1756 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1757 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1758 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1759 parm |= val;
1760 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001761 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Takashi Iwaid5191e52009-11-16 14:58:17 +01001764/**
1765 * snd_hda_codec_amp_read - Read AMP value
1766 * @codec: HD-audio codec
1767 * @nid: NID to read the AMP value
1768 * @ch: channel (left=0 or right=1)
1769 * @direction: #HDA_INPUT or #HDA_OUTPUT
1770 * @index: the index value (only for input direction)
1771 *
1772 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
Takashi Iwai834be882006-03-01 14:16:17 +01001774int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1775 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001777 struct hda_amp_info *info;
1778 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1779 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001781 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001783EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Takashi Iwaid5191e52009-11-16 14:58:17 +01001785/**
1786 * snd_hda_codec_amp_update - update the AMP value
1787 * @codec: HD-audio codec
1788 * @nid: NID to read the AMP value
1789 * @ch: channel (left=0 or right=1)
1790 * @direction: #HDA_INPUT or #HDA_OUTPUT
1791 * @idx: the index value (only for input direction)
1792 * @mask: bit mask to set
1793 * @val: the bits value to set
1794 *
1795 * Update the AMP value with a bit mask.
1796 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001797 */
Takashi Iwai834be882006-03-01 14:16:17 +01001798int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1799 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001801 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001802
Takashi Iwai0ba21762007-04-16 11:29:14 +02001803 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1804 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001806 if (snd_BUG_ON(mask & ~0xff))
1807 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001808 val &= mask;
1809 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001810 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001812 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return 1;
1814}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001815EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Takashi Iwaid5191e52009-11-16 14:58:17 +01001817/**
1818 * snd_hda_codec_amp_stereo - update the AMP stereo values
1819 * @codec: HD-audio codec
1820 * @nid: NID to read the AMP value
1821 * @direction: #HDA_INPUT or #HDA_OUTPUT
1822 * @idx: the index value (only for input direction)
1823 * @mask: bit mask to set
1824 * @val: the bits value to set
1825 *
1826 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1827 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001828 */
1829int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1830 int direction, int idx, int mask, int val)
1831{
1832 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001833
1834 if (snd_BUG_ON(mask & ~0xff))
1835 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001836 for (ch = 0; ch < 2; ch++)
1837 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1838 idx, mask, val);
1839 return ret;
1840}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001841EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001842
Takashi Iwai2a439522011-07-26 09:52:50 +02001843#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001844/**
1845 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1846 * @codec: HD-audio codec
1847 *
1848 * Resume the all amp commands from the cache.
1849 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001850void snd_hda_codec_resume_amp(struct hda_codec *codec)
1851{
Takashi Iwai603c4012008-07-30 15:01:44 +02001852 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001853 int i;
1854
Takashi Iwai603c4012008-07-30 15:01:44 +02001855 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001856 u32 key = buffer->head.key;
1857 hda_nid_t nid;
1858 unsigned int idx, dir, ch;
1859 if (!key)
1860 continue;
1861 nid = key & 0xff;
1862 idx = (key >> 16) & 0xff;
1863 dir = (key >> 24) & 0xff;
1864 for (ch = 0; ch < 2; ch++) {
1865 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1866 continue;
1867 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1868 buffer->vol[ch]);
1869 }
1870 }
1871}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001872EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001873#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001875static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1876 unsigned int ofs)
1877{
1878 u32 caps = query_amp_caps(codec, nid, dir);
1879 /* get num steps */
1880 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1881 if (ofs < caps)
1882 caps -= ofs;
1883 return caps;
1884}
1885
Takashi Iwaid5191e52009-11-16 14:58:17 +01001886/**
1887 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1888 *
1889 * The control element is supposed to have the private_value field
1890 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1891 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001892int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1893 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1896 u16 nid = get_amp_nid(kcontrol);
1897 u8 chs = get_amp_channels(kcontrol);
1898 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001899 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001901 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1902 uinfo->count = chs == 3 ? 2 : 1;
1903 uinfo->value.integer.min = 0;
1904 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1905 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001906 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001907 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1908 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return -EINVAL;
1910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return 0;
1912}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001913EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001915
1916static inline unsigned int
1917read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1918 int ch, int dir, int idx, unsigned int ofs)
1919{
1920 unsigned int val;
1921 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1922 val &= HDA_AMP_VOLMASK;
1923 if (val >= ofs)
1924 val -= ofs;
1925 else
1926 val = 0;
1927 return val;
1928}
1929
1930static inline int
1931update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1932 int ch, int dir, int idx, unsigned int ofs,
1933 unsigned int val)
1934{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001935 unsigned int maxval;
1936
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001937 if (val > 0)
1938 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001939 /* ofs = 0: raw max value */
1940 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001941 if (val > maxval)
1942 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001943 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1944 HDA_AMP_VOLMASK, val);
1945}
1946
Takashi Iwaid5191e52009-11-16 14:58:17 +01001947/**
1948 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1949 *
1950 * The control element is supposed to have the private_value field
1951 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1952 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001953int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1954 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1957 hda_nid_t nid = get_amp_nid(kcontrol);
1958 int chs = get_amp_channels(kcontrol);
1959 int dir = get_amp_direction(kcontrol);
1960 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001961 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 long *valp = ucontrol->value.integer.value;
1963
1964 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001965 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001967 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return 0;
1969}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001970EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Takashi Iwaid5191e52009-11-16 14:58:17 +01001972/**
1973 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1974 *
1975 * The control element is supposed to have the private_value field
1976 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1977 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001978int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1979 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1982 hda_nid_t nid = get_amp_nid(kcontrol);
1983 int chs = get_amp_channels(kcontrol);
1984 int dir = get_amp_direction(kcontrol);
1985 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001986 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 long *valp = ucontrol->value.integer.value;
1988 int change = 0;
1989
Takashi Iwaicb53c622007-08-10 17:21:45 +02001990 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001991 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001992 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001993 valp++;
1994 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001995 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001996 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001997 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return change;
1999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002000EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Takashi Iwaid5191e52009-11-16 14:58:17 +01002002/**
2003 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2004 *
2005 * The control element is supposed to have the private_value field
2006 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2007 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002008int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2009 unsigned int size, unsigned int __user *_tlv)
2010{
2011 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2012 hda_nid_t nid = get_amp_nid(kcontrol);
2013 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002014 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002015 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002016 u32 caps, val1, val2;
2017
2018 if (size < 4 * sizeof(unsigned int))
2019 return -ENOMEM;
2020 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002021 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2022 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002023 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002024 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002025 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002026 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02002027 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002028 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2029 return -EFAULT;
2030 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2031 return -EFAULT;
2032 if (put_user(val1, _tlv + 2))
2033 return -EFAULT;
2034 if (put_user(val2, _tlv + 3))
2035 return -EFAULT;
2036 return 0;
2037}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002038EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002039
Takashi Iwaid5191e52009-11-16 14:58:17 +01002040/**
2041 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2042 * @codec: HD-audio codec
2043 * @nid: NID of a reference widget
2044 * @dir: #HDA_INPUT or #HDA_OUTPUT
2045 * @tlv: TLV data to be stored, at least 4 elements
2046 *
2047 * Set (static) TLV data for a virtual master volume using the AMP caps
2048 * obtained from the reference NID.
2049 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002050 */
2051void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2052 unsigned int *tlv)
2053{
2054 u32 caps;
2055 int nums, step;
2056
2057 caps = query_amp_caps(codec, nid, dir);
2058 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2059 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2060 step = (step + 1) * 25;
2061 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2062 tlv[1] = 2 * sizeof(unsigned int);
2063 tlv[2] = -nums * step;
2064 tlv[3] = step;
2065}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002066EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002067
2068/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002069static struct snd_kcontrol *
2070_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2071 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002072{
2073 struct snd_ctl_elem_id id;
2074 memset(&id, 0, sizeof(id));
2075 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002076 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002077 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2078 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002079 strcpy(id.name, name);
2080 return snd_ctl_find_id(codec->bus->card, &id);
2081}
2082
Takashi Iwaid5191e52009-11-16 14:58:17 +01002083/**
2084 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2085 * @codec: HD-audio codec
2086 * @name: ctl id name string
2087 *
2088 * Get the control element with the given id string and IFACE_MIXER.
2089 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002090struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2091 const char *name)
2092{
2093 return _snd_hda_find_mixer_ctl(codec, name, 0);
2094}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002095EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002096
Takashi Iwai1afe2062010-12-23 10:17:52 +01002097static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2098{
2099 int idx;
2100 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2101 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2102 return idx;
2103 }
2104 return -EBUSY;
2105}
2106
Takashi Iwaid5191e52009-11-16 14:58:17 +01002107/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002108 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002109 * @codec: HD-audio codec
2110 * @nid: corresponding NID (optional)
2111 * @kctl: the control element to assign
2112 *
2113 * Add the given control element to an array inside the codec instance.
2114 * All control elements belonging to a codec are supposed to be added
2115 * by this function so that a proper clean-up works at the free or
2116 * reconfiguration time.
2117 *
2118 * If non-zero @nid is passed, the NID is assigned to the control element.
2119 * The assignment is shown in the codec proc file.
2120 *
2121 * snd_hda_ctl_add() checks the control subdev id field whether
2122 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002123 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2124 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002125 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002126int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2127 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002128{
2129 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002130 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002131 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002132
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002133 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002134 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002135 if (nid == 0)
2136 nid = get_amp_nid_(kctl->private_value);
2137 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002138 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2139 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002140 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002141 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002142 err = snd_ctl_add(codec->bus->card, kctl);
2143 if (err < 0)
2144 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002145 item = snd_array_new(&codec->mixers);
2146 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002147 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002148 item->kctl = kctl;
2149 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002150 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002151 return 0;
2152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002153EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002154
Takashi Iwaid5191e52009-11-16 14:58:17 +01002155/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002156 * snd_hda_add_nid - Assign a NID to a control element
2157 * @codec: HD-audio codec
2158 * @nid: corresponding NID (optional)
2159 * @kctl: the control element to assign
2160 * @index: index to kctl
2161 *
2162 * Add the given control element to an array inside the codec instance.
2163 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2164 * NID:KCTL mapping - for example "Capture Source" selector.
2165 */
2166int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2167 unsigned int index, hda_nid_t nid)
2168{
2169 struct hda_nid_item *item;
2170
2171 if (nid > 0) {
2172 item = snd_array_new(&codec->nids);
2173 if (!item)
2174 return -ENOMEM;
2175 item->kctl = kctl;
2176 item->index = index;
2177 item->nid = nid;
2178 return 0;
2179 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002180 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2181 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002182 return -EINVAL;
2183}
2184EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2185
2186/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002187 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2188 * @codec: HD-audio codec
2189 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002190void snd_hda_ctls_clear(struct hda_codec *codec)
2191{
2192 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002193 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002194 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002195 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002196 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002197 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002198}
2199
Takashi Iwaia65d6292009-02-23 16:57:04 +01002200/* pseudo device locking
2201 * toggle card->shutdown to allow/disallow the device access (as a hack)
2202 */
2203static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002204{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002205 spin_lock(&card->files_lock);
2206 if (card->shutdown) {
2207 spin_unlock(&card->files_lock);
2208 return -EINVAL;
2209 }
2210 card->shutdown = 1;
2211 spin_unlock(&card->files_lock);
2212 return 0;
2213}
2214
2215static void hda_unlock_devices(struct snd_card *card)
2216{
2217 spin_lock(&card->files_lock);
2218 card->shutdown = 0;
2219 spin_unlock(&card->files_lock);
2220}
2221
Takashi Iwaid5191e52009-11-16 14:58:17 +01002222/**
2223 * snd_hda_codec_reset - Clear all objects assigned to the codec
2224 * @codec: HD-audio codec
2225 *
2226 * This frees the all PCM and control elements assigned to the codec, and
2227 * clears the caches and restores the pin default configurations.
2228 *
2229 * When a device is being used, it returns -EBSY. If successfully freed,
2230 * returns zero.
2231 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002232int snd_hda_codec_reset(struct hda_codec *codec)
2233{
2234 struct snd_card *card = codec->bus->card;
2235 int i, pcm;
2236
2237 if (hda_lock_devices(card) < 0)
2238 return -EBUSY;
2239 /* check whether the codec isn't used by any mixer or PCM streams */
2240 if (!list_empty(&card->ctl_files)) {
2241 hda_unlock_devices(card);
2242 return -EBUSY;
2243 }
2244 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2245 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2246 if (!cpcm->pcm)
2247 continue;
2248 if (cpcm->pcm->streams[0].substream_opened ||
2249 cpcm->pcm->streams[1].substream_opened) {
2250 hda_unlock_devices(card);
2251 return -EBUSY;
2252 }
2253 }
2254
2255 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002256
2257#ifdef CONFIG_SND_HDA_POWER_SAVE
2258 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002259 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002260#endif
2261 snd_hda_ctls_clear(codec);
2262 /* relase PCMs */
2263 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002264 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002265 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002266 clear_bit(codec->pcm_info[i].device,
2267 codec->bus->pcm_dev_bits);
2268 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002269 }
2270 if (codec->patch_ops.free)
2271 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002272 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002273 codec->spec = NULL;
2274 free_hda_cache(&codec->amp_cache);
2275 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002276 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2277 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002278 /* free only driver_pins so that init_pins + user_pins are restored */
2279 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002280 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002281 codec->num_pcms = 0;
2282 codec->pcm_info = NULL;
2283 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002284 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2285 codec->slave_dig_outs = NULL;
2286 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002287 module_put(codec->owner);
2288 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002289
2290 /* allow device access again */
2291 hda_unlock_devices(card);
2292 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002293}
2294
Takashi Iwaid5191e52009-11-16 14:58:17 +01002295/**
2296 * snd_hda_add_vmaster - create a virtual master control and add slaves
2297 * @codec: HD-audio codec
2298 * @name: vmaster control name
2299 * @tlv: TLV data (optional)
2300 * @slaves: slave control names (optional)
2301 *
2302 * Create a virtual master control with the given name. The TLV data
2303 * must be either NULL or a valid data.
2304 *
2305 * @slaves is a NULL-terminated array of strings, each of which is a
2306 * slave control name. All controls with these names are assigned to
2307 * the new virtual master control.
2308 *
2309 * This function returns zero if successful or a negative error code.
2310 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002311int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002312 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002313{
2314 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002315 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002316 int err;
2317
Takashi Iwai2f085542008-02-22 18:43:50 +01002318 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2319 ;
2320 if (!*s) {
2321 snd_printdd("No slave found for %s\n", name);
2322 return 0;
2323 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002324 kctl = snd_ctl_make_virtual_master(name, tlv);
2325 if (!kctl)
2326 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002327 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002328 if (err < 0)
2329 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002330
Takashi Iwai2134ea42008-01-10 16:53:55 +01002331 for (s = slaves; *s; s++) {
2332 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002333 int i = 0;
2334 for (;;) {
2335 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2336 if (!sctl) {
2337 if (!i)
2338 snd_printdd("Cannot find slave %s, "
2339 "skipped\n", *s);
2340 break;
2341 }
2342 err = snd_ctl_add_slave(kctl, sctl);
2343 if (err < 0)
2344 return err;
2345 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002346 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002347 }
2348 return 0;
2349}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002350EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002351
Takashi Iwaid5191e52009-11-16 14:58:17 +01002352/**
2353 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2354 *
2355 * The control element is supposed to have the private_value field
2356 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2357 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002358int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2359 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 int chs = get_amp_channels(kcontrol);
2362
2363 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2364 uinfo->count = chs == 3 ? 2 : 1;
2365 uinfo->value.integer.min = 0;
2366 uinfo->value.integer.max = 1;
2367 return 0;
2368}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002369EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Takashi Iwaid5191e52009-11-16 14:58:17 +01002371/**
2372 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2373 *
2374 * The control element is supposed to have the private_value field
2375 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2376 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002377int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
2380 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2381 hda_nid_t nid = get_amp_nid(kcontrol);
2382 int chs = get_amp_channels(kcontrol);
2383 int dir = get_amp_direction(kcontrol);
2384 int idx = get_amp_index(kcontrol);
2385 long *valp = ucontrol->value.integer.value;
2386
2387 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002388 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002389 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002391 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002392 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return 0;
2394}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002395EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Takashi Iwaid5191e52009-11-16 14:58:17 +01002397/**
2398 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2399 *
2400 * The control element is supposed to have the private_value field
2401 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2402 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002403int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2407 hda_nid_t nid = get_amp_nid(kcontrol);
2408 int chs = get_amp_channels(kcontrol);
2409 int dir = get_amp_direction(kcontrol);
2410 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 long *valp = ucontrol->value.integer.value;
2412 int change = 0;
2413
Takashi Iwaicb53c622007-08-10 17:21:45 +02002414 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002415 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002416 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002417 HDA_AMP_MUTE,
2418 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002419 valp++;
2420 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002421 if (chs & 2)
2422 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002423 HDA_AMP_MUTE,
2424 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002425 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002426 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return change;
2428}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002429EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Takashi Iwai67d634c2009-11-16 15:35:59 +01002431#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002432/**
2433 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2434 *
2435 * This function calls snd_hda_enable_beep_device(), which behaves differently
2436 * depending on beep_mode option.
2437 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002438int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
2440{
2441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2442 long *valp = ucontrol->value.integer.value;
2443
2444 snd_hda_enable_beep_device(codec, *valp);
2445 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2446}
2447EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002448#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450/*
Takashi Iwai985be542005-11-02 18:26:49 +01002451 * bound volume controls
2452 *
2453 * bind multiple volumes (# indices, from 0)
2454 */
2455
2456#define AMP_VAL_IDX_SHIFT 19
2457#define AMP_VAL_IDX_MASK (0x0f<<19)
2458
Takashi Iwaid5191e52009-11-16 14:58:17 +01002459/**
2460 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2461 *
2462 * The control element is supposed to have the private_value field
2463 * set up via HDA_BIND_MUTE*() macros.
2464 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002465int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2466 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002467{
2468 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2469 unsigned long pval;
2470 int err;
2471
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002472 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002473 pval = kcontrol->private_value;
2474 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2475 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2476 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002477 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002478 return err;
2479}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002480EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002481
Takashi Iwaid5191e52009-11-16 14:58:17 +01002482/**
2483 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2484 *
2485 * The control element is supposed to have the private_value field
2486 * set up via HDA_BIND_MUTE*() macros.
2487 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002488int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002490{
2491 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2492 unsigned long pval;
2493 int i, indices, err = 0, change = 0;
2494
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002495 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002496 pval = kcontrol->private_value;
2497 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2498 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002499 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2500 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002501 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2502 if (err < 0)
2503 break;
2504 change |= err;
2505 }
2506 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002507 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002508 return err < 0 ? err : change;
2509}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002510EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002511
Takashi Iwaid5191e52009-11-16 14:58:17 +01002512/**
2513 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2514 *
2515 * The control element is supposed to have the private_value field
2516 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002517 */
2518int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2519 struct snd_ctl_elem_info *uinfo)
2520{
2521 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2522 struct hda_bind_ctls *c;
2523 int err;
2524
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002525 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002526 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002527 kcontrol->private_value = *c->values;
2528 err = c->ops->info(kcontrol, uinfo);
2529 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002530 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002531 return err;
2532}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002533EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002534
Takashi Iwaid5191e52009-11-16 14:58:17 +01002535/**
2536 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2537 *
2538 * The control element is supposed to have the private_value field
2539 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2540 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002541int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_value *ucontrol)
2543{
2544 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2545 struct hda_bind_ctls *c;
2546 int err;
2547
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002548 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002549 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002550 kcontrol->private_value = *c->values;
2551 err = c->ops->get(kcontrol, ucontrol);
2552 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002553 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002554 return err;
2555}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002556EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002557
Takashi Iwaid5191e52009-11-16 14:58:17 +01002558/**
2559 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2560 *
2561 * The control element is supposed to have the private_value field
2562 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2563 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002564int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2565 struct snd_ctl_elem_value *ucontrol)
2566{
2567 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2568 struct hda_bind_ctls *c;
2569 unsigned long *vals;
2570 int err = 0, change = 0;
2571
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002572 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002573 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002574 for (vals = c->values; *vals; vals++) {
2575 kcontrol->private_value = *vals;
2576 err = c->ops->put(kcontrol, ucontrol);
2577 if (err < 0)
2578 break;
2579 change |= err;
2580 }
2581 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002582 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002583 return err < 0 ? err : change;
2584}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002585EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002586
Takashi Iwaid5191e52009-11-16 14:58:17 +01002587/**
2588 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2589 *
2590 * The control element is supposed to have the private_value field
2591 * set up via HDA_BIND_VOL() macro.
2592 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002593int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2594 unsigned int size, unsigned int __user *tlv)
2595{
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 struct hda_bind_ctls *c;
2598 int err;
2599
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002600 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002601 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002602 kcontrol->private_value = *c->values;
2603 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2604 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002605 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002606 return err;
2607}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002608EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002609
2610struct hda_ctl_ops snd_hda_bind_vol = {
2611 .info = snd_hda_mixer_amp_volume_info,
2612 .get = snd_hda_mixer_amp_volume_get,
2613 .put = snd_hda_mixer_amp_volume_put,
2614 .tlv = snd_hda_mixer_amp_tlv
2615};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002616EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002617
2618struct hda_ctl_ops snd_hda_bind_sw = {
2619 .info = snd_hda_mixer_amp_switch_info,
2620 .get = snd_hda_mixer_amp_switch_get,
2621 .put = snd_hda_mixer_amp_switch_put,
2622 .tlv = snd_hda_mixer_amp_tlv
2623};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002624EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002625
2626/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 * SPDIF out controls
2628 */
2629
Takashi Iwai0ba21762007-04-16 11:29:14 +02002630static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2631 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2634 uinfo->count = 1;
2635 return 0;
2636}
2637
Takashi Iwai0ba21762007-04-16 11:29:14 +02002638static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2639 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
2641 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2642 IEC958_AES0_NONAUDIO |
2643 IEC958_AES0_CON_EMPHASIS_5015 |
2644 IEC958_AES0_CON_NOT_COPYRIGHT;
2645 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2646 IEC958_AES1_CON_ORIGINAL;
2647 return 0;
2648}
2649
Takashi Iwai0ba21762007-04-16 11:29:14 +02002650static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2651 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
2653 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2654 IEC958_AES0_NONAUDIO |
2655 IEC958_AES0_PRO_EMPHASIS_5015;
2656 return 0;
2657}
2658
Takashi Iwai0ba21762007-04-16 11:29:14 +02002659static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002663 int idx = kcontrol->private_value;
2664 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Stephen Warren7c9359762011-06-01 11:14:17 -06002666 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2667 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2668 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2669 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 return 0;
2672}
2673
2674/* convert from SPDIF status bits to HDA SPDIF bits
2675 * bit 0 (DigEn) is always set zero (to be filled later)
2676 */
2677static unsigned short convert_from_spdif_status(unsigned int sbits)
2678{
2679 unsigned short val = 0;
2680
2681 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002682 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002684 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002686 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2687 IEC958_AES0_PRO_EMPHASIS_5015)
2688 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002690 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2691 IEC958_AES0_CON_EMPHASIS_5015)
2692 val |= AC_DIG1_EMPHASIS;
2693 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2694 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002696 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2698 }
2699 return val;
2700}
2701
2702/* convert to SPDIF status bits from HDA SPDIF bits
2703 */
2704static unsigned int convert_to_spdif_status(unsigned short val)
2705{
2706 unsigned int sbits = 0;
2707
Takashi Iwai0ba21762007-04-16 11:29:14 +02002708 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002710 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 sbits |= IEC958_AES0_PROFESSIONAL;
2712 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002713 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2715 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002716 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002718 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002720 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2722 sbits |= val & (0x7f << 8);
2723 }
2724 return sbits;
2725}
2726
Takashi Iwai2f728532008-09-25 16:32:41 +02002727/* set digital convert verbs both for the given NID and its slaves */
2728static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2729 int verb, int val)
2730{
Takashi Iwaidda14412011-05-02 11:29:30 +02002731 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002732
Takashi Iwai9e976972008-11-25 08:17:20 +01002733 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002734 d = codec->slave_dig_outs;
2735 if (!d)
2736 return;
2737 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002738 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002739}
2740
2741static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2742 int dig1, int dig2)
2743{
2744 if (dig1 != -1)
2745 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2746 if (dig2 != -1)
2747 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2748}
2749
Takashi Iwai0ba21762007-04-16 11:29:14 +02002750static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2751 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
2753 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002754 int idx = kcontrol->private_value;
2755 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2756 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 unsigned short val;
2758 int change;
2759
Ingo Molnar62932df2006-01-16 16:34:20 +01002760 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002761 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2763 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2764 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002765 val = convert_from_spdif_status(spdif->status);
2766 val |= spdif->ctls & 1;
2767 change = spdif->ctls != val;
2768 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002769 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002770 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002771 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return change;
2773}
2774
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002775#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Takashi Iwai0ba21762007-04-16 11:29:14 +02002777static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2778 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
2780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002781 int idx = kcontrol->private_value;
2782 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Stephen Warren7c9359762011-06-01 11:14:17 -06002784 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return 0;
2786}
2787
Stephen Warren74b654c2011-06-01 11:14:18 -06002788static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2789 int dig1, int dig2)
2790{
2791 set_dig_out_convert(codec, nid, dig1, dig2);
2792 /* unmute amp switch (if any) */
2793 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2794 (dig1 & AC_DIG1_ENABLE))
2795 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2796 HDA_AMP_MUTE, 0);
2797}
2798
Takashi Iwai0ba21762007-04-16 11:29:14 +02002799static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2800 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
2802 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002803 int idx = kcontrol->private_value;
2804 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2805 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 unsigned short val;
2807 int change;
2808
Ingo Molnar62932df2006-01-16 16:34:20 +01002809 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002810 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002812 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002813 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002814 spdif->ctls = val;
2815 if (change && nid != (u16)-1)
2816 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002817 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return change;
2819}
2820
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002821static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 {
2823 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2824 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002825 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 .info = snd_hda_spdif_mask_info,
2827 .get = snd_hda_spdif_cmask_get,
2828 },
2829 {
2830 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002832 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 .info = snd_hda_spdif_mask_info,
2834 .get = snd_hda_spdif_pmask_get,
2835 },
2836 {
2837 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002838 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 .info = snd_hda_spdif_mask_info,
2840 .get = snd_hda_spdif_default_get,
2841 .put = snd_hda_spdif_default_put,
2842 },
2843 {
2844 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002845 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 .info = snd_hda_spdif_out_switch_info,
2847 .get = snd_hda_spdif_out_switch_get,
2848 .put = snd_hda_spdif_out_switch_put,
2849 },
2850 { } /* end */
2851};
2852
2853/**
2854 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2855 * @codec: the HDA codec
2856 * @nid: audio out widget NID
2857 *
2858 * Creates controls related with the SPDIF output.
2859 * Called from each patch supporting the SPDIF out.
2860 *
2861 * Returns 0 if successful, or a negative error code.
2862 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002863int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2864 hda_nid_t associated_nid,
2865 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002868 struct snd_kcontrol *kctl;
2869 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002870 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002871 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Takashi Iwai1afe2062010-12-23 10:17:52 +01002873 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2874 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002875 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2876 return -EBUSY;
2877 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002878 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2880 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002881 if (!kctl)
2882 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002883 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002884 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002885 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002886 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return err;
2888 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002889 spdif->nid = cvt_nid;
2890 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06002891 AC_VERB_GET_DIGI_CONVERT_1, 0);
2892 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return 0;
2894}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002895EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Stephen Warren7c9359762011-06-01 11:14:17 -06002897struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2898 hda_nid_t nid)
2899{
2900 int i;
2901 for (i = 0; i < codec->spdif_out.used; i++) {
2902 struct hda_spdif_out *spdif =
2903 snd_array_elem(&codec->spdif_out, i);
2904 if (spdif->nid == nid)
2905 return spdif;
2906 }
2907 return NULL;
2908}
2909EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2910
Stephen Warren74b654c2011-06-01 11:14:18 -06002911void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2912{
2913 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2914
2915 mutex_lock(&codec->spdif_mutex);
2916 spdif->nid = (u16)-1;
2917 mutex_unlock(&codec->spdif_mutex);
2918}
2919EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2920
2921void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2922{
2923 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2924 unsigned short val;
2925
2926 mutex_lock(&codec->spdif_mutex);
2927 if (spdif->nid != nid) {
2928 spdif->nid = nid;
2929 val = spdif->ctls;
2930 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2931 }
2932 mutex_unlock(&codec->spdif_mutex);
2933}
2934EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002937 * SPDIF sharing with analog output
2938 */
2939static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2940 struct snd_ctl_elem_value *ucontrol)
2941{
2942 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2943 ucontrol->value.integer.value[0] = mout->share_spdif;
2944 return 0;
2945}
2946
2947static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2948 struct snd_ctl_elem_value *ucontrol)
2949{
2950 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2951 mout->share_spdif = !!ucontrol->value.integer.value[0];
2952 return 0;
2953}
2954
2955static struct snd_kcontrol_new spdif_share_sw = {
2956 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2957 .name = "IEC958 Default PCM Playback Switch",
2958 .info = snd_ctl_boolean_mono_info,
2959 .get = spdif_share_sw_get,
2960 .put = spdif_share_sw_put,
2961};
2962
Takashi Iwaid5191e52009-11-16 14:58:17 +01002963/**
2964 * snd_hda_create_spdif_share_sw - create Default PCM switch
2965 * @codec: the HDA codec
2966 * @mout: multi-out instance
2967 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002968int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2969 struct hda_multi_out *mout)
2970{
2971 if (!mout->dig_out_nid)
2972 return 0;
2973 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002974 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2975 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002976}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002977EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002978
2979/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 * SPDIF input
2981 */
2982
2983#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2984
Takashi Iwai0ba21762007-04-16 11:29:14 +02002985static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2989
2990 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2991 return 0;
2992}
2993
Takashi Iwai0ba21762007-04-16 11:29:14 +02002994static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2995 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
2997 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2998 hda_nid_t nid = kcontrol->private_value;
2999 unsigned int val = !!ucontrol->value.integer.value[0];
3000 int change;
3001
Ingo Molnar62932df2006-01-16 16:34:20 +01003002 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003004 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003006 snd_hda_codec_write_cache(codec, nid, 0,
3007 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003009 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return change;
3011}
3012
Takashi Iwai0ba21762007-04-16 11:29:14 +02003013static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3014 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
3016 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3017 hda_nid_t nid = kcontrol->private_value;
3018 unsigned short val;
3019 unsigned int sbits;
3020
Andrew Paprocki3982d172007-12-19 12:13:44 +01003021 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 sbits = convert_to_spdif_status(val);
3023 ucontrol->value.iec958.status[0] = sbits;
3024 ucontrol->value.iec958.status[1] = sbits >> 8;
3025 ucontrol->value.iec958.status[2] = sbits >> 16;
3026 ucontrol->value.iec958.status[3] = sbits >> 24;
3027 return 0;
3028}
3029
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003030static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 {
3032 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003033 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 .info = snd_hda_spdif_in_switch_info,
3035 .get = snd_hda_spdif_in_switch_get,
3036 .put = snd_hda_spdif_in_switch_put,
3037 },
3038 {
3039 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3040 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003041 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 .info = snd_hda_spdif_mask_info,
3043 .get = snd_hda_spdif_in_status_get,
3044 },
3045 { } /* end */
3046};
3047
3048/**
3049 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3050 * @codec: the HDA codec
3051 * @nid: audio in widget NID
3052 *
3053 * Creates controls related with the SPDIF input.
3054 * Called from each patch supporting the SPDIF in.
3055 *
3056 * Returns 0 if successful, or a negative error code.
3057 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003058int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
3060 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003061 struct snd_kcontrol *kctl;
3062 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003063 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Takashi Iwai1afe2062010-12-23 10:17:52 +01003065 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3066 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003067 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3068 return -EBUSY;
3069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3071 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003072 if (!kctl)
3073 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003075 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003076 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return err;
3078 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003079 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003080 snd_hda_codec_read(codec, nid, 0,
3081 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003082 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return 0;
3084}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003085EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Takashi Iwai2a439522011-07-26 09:52:50 +02003087#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003088/*
3089 * command cache
3090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003092/* build a 32bit cache key with the widget id and the command parameter */
3093#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3094#define get_cmd_cache_nid(key) ((key) & 0xff)
3095#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3096
3097/**
3098 * snd_hda_codec_write_cache - send a single command with caching
3099 * @codec: the HDA codec
3100 * @nid: NID to send the command
3101 * @direct: direct flag
3102 * @verb: the verb to send
3103 * @parm: the parameter for the verb
3104 *
3105 * Send a single command without waiting for response.
3106 *
3107 * Returns 0 if successful, or a negative error code.
3108 */
3109int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3110 int direct, unsigned int verb, unsigned int parm)
3111{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003112 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3113 struct hda_cache_head *c;
3114 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003115
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003116 if (err < 0)
3117 return err;
3118 /* parm may contain the verb stuff for get/set amp */
3119 verb = verb | (parm >> 8);
3120 parm &= 0xff;
3121 key = build_cmd_cache_key(nid, verb);
3122 mutex_lock(&codec->bus->cmd_mutex);
3123 c = get_alloc_hash(&codec->cmd_cache, key);
3124 if (c)
3125 c->val = parm;
3126 mutex_unlock(&codec->bus->cmd_mutex);
3127 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003129EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003130
Takashi Iwaid5191e52009-11-16 14:58:17 +01003131/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003132 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3133 * @codec: the HDA codec
3134 * @nid: NID to send the command
3135 * @direct: direct flag
3136 * @verb: the verb to send
3137 * @parm: the parameter for the verb
3138 *
3139 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3140 * command if the parameter is already identical with the cached value.
3141 * If not, it sends the command and refreshes the cache.
3142 *
3143 * Returns 0 if successful, or a negative error code.
3144 */
3145int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3146 int direct, unsigned int verb, unsigned int parm)
3147{
3148 struct hda_cache_head *c;
3149 u32 key;
3150
3151 /* parm may contain the verb stuff for get/set amp */
3152 verb = verb | (parm >> 8);
3153 parm &= 0xff;
3154 key = build_cmd_cache_key(nid, verb);
3155 mutex_lock(&codec->bus->cmd_mutex);
3156 c = get_hash(&codec->cmd_cache, key);
3157 if (c && c->val == parm) {
3158 mutex_unlock(&codec->bus->cmd_mutex);
3159 return 0;
3160 }
3161 mutex_unlock(&codec->bus->cmd_mutex);
3162 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3163}
3164EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3165
3166/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003167 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3168 * @codec: HD-audio codec
3169 *
3170 * Execute all verbs recorded in the command caches to resume.
3171 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003172void snd_hda_codec_resume_cache(struct hda_codec *codec)
3173{
Takashi Iwai603c4012008-07-30 15:01:44 +02003174 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003175 int i;
3176
Takashi Iwai603c4012008-07-30 15:01:44 +02003177 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003178 u32 key = buffer->key;
3179 if (!key)
3180 continue;
3181 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3182 get_cmd_cache_cmd(key), buffer->val);
3183 }
3184}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003185EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003186
3187/**
3188 * snd_hda_sequence_write_cache - sequence writes with caching
3189 * @codec: the HDA codec
3190 * @seq: VERB array to send
3191 *
3192 * Send the commands sequentially from the given array.
3193 * Thte commands are recorded on cache for power-save and resume.
3194 * The array must be terminated with NID=0.
3195 */
3196void snd_hda_sequence_write_cache(struct hda_codec *codec,
3197 const struct hda_verb *seq)
3198{
3199 for (; seq->nid; seq++)
3200 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3201 seq->param);
3202}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003203EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003204#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003205
Takashi Iwai54d17402005-11-21 16:33:22 +01003206/*
3207 * set power state of the codec
3208 */
3209static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3210 unsigned int power_state)
3211{
Takashi Iwaicb53c622007-08-10 17:21:45 +02003212 hda_nid_t nid;
3213 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003214
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003215 /* this delay seems necessary to avoid click noise at power-down */
3216 if (power_state == AC_PWRST_D3)
3217 msleep(100);
3218 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai54d17402005-11-21 16:33:22 +01003219 power_state);
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003220 /* partial workaround for "azx_get_response timeout" */
Zhang, Ruidd2b4a72010-02-24 09:38:49 +08003221 if (power_state == AC_PWRST_D0 &&
3222 (codec->vendor_id & 0xffff0000) == 0x14f10000)
Takashi Iwai05ff7e12009-07-22 12:39:24 +02003223 msleep(10);
Takashi Iwai54d17402005-11-21 16:33:22 +01003224
Takashi Iwaicb53c622007-08-10 17:21:45 +02003225 nid = codec->start_nid;
3226 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003227 unsigned int wcaps = get_wcaps(codec, nid);
3228 if (wcaps & AC_WCAP_POWER) {
Takashi Iwaia22d5432009-07-27 12:54:26 +02003229 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003230 if (power_state == AC_PWRST_D3 &&
3231 wid_type == AC_WID_PIN) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003232 unsigned int pincap;
3233 /*
3234 * don't power down the widget if it controls
3235 * eapd and EAPD_BTLENABLE is set.
3236 */
Takashi Iwai14bafe32009-03-23 16:35:39 +01003237 pincap = snd_hda_query_pin_caps(codec, nid);
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003238 if (pincap & AC_PINCAP_EAPD) {
3239 int eapd = snd_hda_codec_read(codec,
3240 nid, 0,
3241 AC_VERB_GET_EAPD_BTLENABLE, 0);
3242 eapd &= 0x02;
Takashi Iwaia3b48c82009-04-21 13:37:29 +02003243 if (eapd)
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003244 continue;
3245 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003246 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003247 snd_hda_codec_write(codec, nid, 0,
3248 AC_VERB_SET_POWER_STATE,
3249 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003250 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003251 }
3252
Takashi Iwaicb53c622007-08-10 17:21:45 +02003253 if (power_state == AC_PWRST_D0) {
3254 unsigned long end_time;
3255 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003256 /* wait until the codec reachs to D0 */
3257 end_time = jiffies + msecs_to_jiffies(500);
3258 do {
3259 state = snd_hda_codec_read(codec, fg, 0,
3260 AC_VERB_GET_POWER_STATE, 0);
3261 if (state == power_state)
3262 break;
3263 msleep(1);
3264 } while (time_after_eq(end_time, jiffies));
3265 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003266}
3267
Takashi Iwai11aeff02008-07-30 15:01:46 +02003268#ifdef CONFIG_SND_HDA_HWDEP
3269/* execute additional init verbs */
3270static void hda_exec_init_verbs(struct hda_codec *codec)
3271{
3272 if (codec->init_verbs.list)
3273 snd_hda_sequence_write(codec, codec->init_verbs.list);
3274}
3275#else
3276static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3277#endif
3278
Takashi Iwai2a439522011-07-26 09:52:50 +02003279#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003280/*
3281 * call suspend and power-down; used both from PM and power-save
3282 */
3283static void hda_call_codec_suspend(struct hda_codec *codec)
3284{
3285 if (codec->patch_ops.suspend)
3286 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003287 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003288 hda_set_power_state(codec,
3289 codec->afg ? codec->afg : codec->mfg,
3290 AC_PWRST_D3);
3291#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003292 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003293 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003294 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003295 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003296 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003297#endif
3298}
3299
3300/*
3301 * kick up codec; used both from PM and power-save
3302 */
3303static void hda_call_codec_resume(struct hda_codec *codec)
3304{
3305 hda_set_power_state(codec,
3306 codec->afg ? codec->afg : codec->mfg,
3307 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003308 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003309 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003310 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003311 if (codec->patch_ops.resume)
3312 codec->patch_ops.resume(codec);
3313 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003314 if (codec->patch_ops.init)
3315 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003316 snd_hda_codec_resume_amp(codec);
3317 snd_hda_codec_resume_cache(codec);
3318 }
3319}
Takashi Iwai2a439522011-07-26 09:52:50 +02003320#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003321
Takashi Iwai54d17402005-11-21 16:33:22 +01003322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323/**
3324 * snd_hda_build_controls - build mixer controls
3325 * @bus: the BUS
3326 *
3327 * Creates mixer controls for each codec included in the bus.
3328 *
3329 * Returns 0 if successful, otherwise a negative error code.
3330 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003331int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003333 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Takashi Iwai0ba21762007-04-16 11:29:14 +02003335 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003336 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003337 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003338 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003339 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003340 err = snd_hda_codec_reset(codec);
3341 if (err < 0) {
3342 printk(KERN_ERR
3343 "hda_codec: cannot revert codec\n");
3344 return err;
3345 }
3346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003348 return 0;
3349}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003350EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003352int snd_hda_codec_build_controls(struct hda_codec *codec)
3353{
3354 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003355 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003356 /* continue to initialize... */
3357 if (codec->patch_ops.init)
3358 err = codec->patch_ops.init(codec);
3359 if (!err && codec->patch_ops.build_controls)
3360 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003361 if (err < 0)
3362 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return 0;
3364}
3365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366/*
3367 * stream formats
3368 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003369struct hda_rate_tbl {
3370 unsigned int hz;
3371 unsigned int alsa_bits;
3372 unsigned int hda_fmt;
3373};
3374
Takashi Iwai92f10b32010-08-03 14:21:00 +02003375/* rate = base * mult / div */
3376#define HDA_RATE(base, mult, div) \
3377 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3378 (((div) - 1) << AC_FMT_DIV_SHIFT))
3379
Takashi Iwaibefdf312005-08-22 13:57:55 +02003380static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003382
3383 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003384 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3385 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3386 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3387 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3388 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3389 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3390 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3391 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3392 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3393 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3394 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003395#define AC_PAR_PCM_RATE_BITS 11
3396 /* up to bits 10, 384kHZ isn't supported properly */
3397
3398 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003399 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003400
Takashi Iwaibefdf312005-08-22 13:57:55 +02003401 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402};
3403
3404/**
3405 * snd_hda_calc_stream_format - calculate format bitset
3406 * @rate: the sample rate
3407 * @channels: the number of channels
3408 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3409 * @maxbps: the max. bps
3410 *
3411 * Calculate the format bitset from the given rate, channels and th PCM format.
3412 *
3413 * Return zero if invalid.
3414 */
3415unsigned int snd_hda_calc_stream_format(unsigned int rate,
3416 unsigned int channels,
3417 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003418 unsigned int maxbps,
3419 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
3421 int i;
3422 unsigned int val = 0;
3423
Takashi Iwaibefdf312005-08-22 13:57:55 +02003424 for (i = 0; rate_bits[i].hz; i++)
3425 if (rate_bits[i].hz == rate) {
3426 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 break;
3428 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003429 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 snd_printdd("invalid rate %d\n", rate);
3431 return 0;
3432 }
3433
3434 if (channels == 0 || channels > 8) {
3435 snd_printdd("invalid channels %d\n", channels);
3436 return 0;
3437 }
3438 val |= channels - 1;
3439
3440 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003441 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003442 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003443 break;
3444 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003445 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 case 20:
3448 case 24:
3449 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003450 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003451 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003453 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003455 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 break;
3457 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003458 snd_printdd("invalid format width %d\n",
3459 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 return 0;
3461 }
3462
Anssi Hannula32c168c2010-08-03 13:28:57 +03003463 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003464 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return val;
3467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003468EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003470static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3471{
3472 unsigned int val = 0;
3473 if (nid != codec->afg &&
3474 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3475 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3476 if (!val || val == -1)
3477 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3478 if (!val || val == -1)
3479 return 0;
3480 return val;
3481}
3482
3483static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3484{
3485 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3486 get_pcm_param);
3487}
3488
3489static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3490{
3491 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3492 if (!streams || streams == -1)
3493 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3494 if (!streams || streams == -1)
3495 return 0;
3496 return streams;
3497}
3498
3499static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3500{
3501 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3502 get_stream_param);
3503}
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505/**
3506 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3507 * @codec: the HDA codec
3508 * @nid: NID to query
3509 * @ratesp: the pointer to store the detected rate bitflags
3510 * @formatsp: the pointer to store the detected formats
3511 * @bpsp: the pointer to store the detected format widths
3512 *
3513 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3514 * or @bsps argument is ignored.
3515 *
3516 * Returns 0 if successful, otherwise a negative error code.
3517 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003518int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3520{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003521 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003523 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003524 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
3526 if (ratesp) {
3527 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003528 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003530 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003532 if (rates == 0) {
3533 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3534 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3535 nid, val,
3536 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3537 return -EIO;
3538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 *ratesp = rates;
3540 }
3541
3542 if (formatsp || bpsp) {
3543 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003544 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003546 streams = query_stream_param(codec, nid);
3547 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
3550 bps = 0;
3551 if (streams & AC_SUPFMT_PCM) {
3552 if (val & AC_SUPPCM_BITS_8) {
3553 formats |= SNDRV_PCM_FMTBIT_U8;
3554 bps = 8;
3555 }
3556 if (val & AC_SUPPCM_BITS_16) {
3557 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3558 bps = 16;
3559 }
3560 if (wcaps & AC_WCAP_DIGITAL) {
3561 if (val & AC_SUPPCM_BITS_32)
3562 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3563 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3564 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3565 if (val & AC_SUPPCM_BITS_24)
3566 bps = 24;
3567 else if (val & AC_SUPPCM_BITS_20)
3568 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003569 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3570 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3572 if (val & AC_SUPPCM_BITS_32)
3573 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 else if (val & AC_SUPPCM_BITS_24)
3575 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003576 else if (val & AC_SUPPCM_BITS_20)
3577 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 }
3579 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003580 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003582 if (!bps)
3583 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003584 }
3585 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003586 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 /* temporary hack: we have still no proper support
3588 * for the direct AC3 stream...
3589 */
3590 formats |= SNDRV_PCM_FMTBIT_U8;
3591 bps = 8;
3592 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003593 if (formats == 0) {
3594 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3595 "(nid=0x%x, val=0x%x, ovrd=%i, "
3596 "streams=0x%x)\n",
3597 nid, val,
3598 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3599 streams);
3600 return -EIO;
3601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 if (formatsp)
3603 *formatsp = formats;
3604 if (bpsp)
3605 *bpsp = bps;
3606 }
3607
3608 return 0;
3609}
Stephen Warren384a48d2011-06-01 11:14:21 -06003610EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
3612/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003613 * snd_hda_is_supported_format - Check the validity of the format
3614 * @codec: HD-audio codec
3615 * @nid: NID to check
3616 * @format: the HD-audio format value to check
3617 *
3618 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 *
3620 * Returns 1 if supported, 0 if not.
3621 */
3622int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3623 unsigned int format)
3624{
3625 int i;
3626 unsigned int val = 0, rate, stream;
3627
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003628 val = query_pcm_param(codec, nid);
3629 if (!val)
3630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003633 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003634 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 if (val & (1 << i))
3636 break;
3637 return 0;
3638 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003639 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return 0;
3641
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003642 stream = query_stream_param(codec, nid);
3643 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return 0;
3645
3646 if (stream & AC_SUPFMT_PCM) {
3647 switch (format & 0xf0) {
3648 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003649 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return 0;
3651 break;
3652 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003653 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 return 0;
3655 break;
3656 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003657 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return 0;
3659 break;
3660 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003661 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 return 0;
3663 break;
3664 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003665 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 return 0;
3667 break;
3668 default:
3669 return 0;
3670 }
3671 } else {
3672 /* FIXME: check for float32 and AC3? */
3673 }
3674
3675 return 1;
3676}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003677EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
3679/*
3680 * PCM stuff
3681 */
3682static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3683 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003684 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
3686 return 0;
3687}
3688
3689static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3690 struct hda_codec *codec,
3691 unsigned int stream_tag,
3692 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003693 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
3695 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3696 return 0;
3697}
3698
3699static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3700 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003701 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
Takashi Iwai888afa12008-03-18 09:57:50 +01003703 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return 0;
3705}
3706
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003707static int set_pcm_default_values(struct hda_codec *codec,
3708 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003710 int err;
3711
Takashi Iwai0ba21762007-04-16 11:29:14 +02003712 /* query support PCM information from the given NID */
3713 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003714 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003715 info->rates ? NULL : &info->rates,
3716 info->formats ? NULL : &info->formats,
3717 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003718 if (err < 0)
3719 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
3721 if (info->ops.open == NULL)
3722 info->ops.open = hda_pcm_default_open_close;
3723 if (info->ops.close == NULL)
3724 info->ops.close = hda_pcm_default_open_close;
3725 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003726 if (snd_BUG_ON(!info->nid))
3727 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 info->ops.prepare = hda_pcm_default_prepare;
3729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003731 if (snd_BUG_ON(!info->nid))
3732 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 info->ops.cleanup = hda_pcm_default_cleanup;
3734 }
3735 return 0;
3736}
3737
Takashi Iwaieb541332010-08-06 13:48:11 +02003738/*
3739 * codec prepare/cleanup entries
3740 */
3741int snd_hda_codec_prepare(struct hda_codec *codec,
3742 struct hda_pcm_stream *hinfo,
3743 unsigned int stream,
3744 unsigned int format,
3745 struct snd_pcm_substream *substream)
3746{
3747 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003748 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003749 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3750 if (ret >= 0)
3751 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003752 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003753 return ret;
3754}
3755EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3756
3757void snd_hda_codec_cleanup(struct hda_codec *codec,
3758 struct hda_pcm_stream *hinfo,
3759 struct snd_pcm_substream *substream)
3760{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003761 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003762 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003763 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003764}
3765EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3766
Takashi Iwaid5191e52009-11-16 14:58:17 +01003767/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003768const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3769 "Audio", "SPDIF", "HDMI", "Modem"
3770};
3771
Takashi Iwai176d5332008-07-30 15:01:44 +02003772/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003773 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003774 *
3775 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003776 */
3777static int get_empty_pcm_device(struct hda_bus *bus, int type)
3778{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003779 /* audio device indices; not linear to keep compatibility */
3780 static int audio_idx[HDA_PCM_NTYPES][5] = {
3781 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3782 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003783 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003784 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003785 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003786 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003787
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003788 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003789 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3790 return -EINVAL;
3791 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003792
3793 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3794 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3795 return audio_idx[type][i];
3796
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003797 snd_printk(KERN_WARNING "Too many %s devices\n",
3798 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003799 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003800}
3801
3802/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003803 * attach a new PCM stream
3804 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003805static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003806{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003807 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003808 struct hda_pcm_stream *info;
3809 int stream, err;
3810
Takashi Iwaib91f0802008-11-04 08:43:08 +01003811 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003812 return -EINVAL;
3813 for (stream = 0; stream < 2; stream++) {
3814 info = &pcm->stream[stream];
3815 if (info->substreams) {
3816 err = set_pcm_default_values(codec, info);
3817 if (err < 0)
3818 return err;
3819 }
3820 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003821 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003822}
3823
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003824/* assign all PCMs of the given codec */
3825int snd_hda_codec_build_pcms(struct hda_codec *codec)
3826{
3827 unsigned int pcm;
3828 int err;
3829
3830 if (!codec->num_pcms) {
3831 if (!codec->patch_ops.build_pcms)
3832 return 0;
3833 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003834 if (err < 0) {
3835 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003836 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003837 err = snd_hda_codec_reset(codec);
3838 if (err < 0) {
3839 printk(KERN_ERR
3840 "hda_codec: cannot revert codec\n");
3841 return err;
3842 }
3843 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003844 }
3845 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3846 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3847 int dev;
3848
3849 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003850 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003851
3852 if (!cpcm->pcm) {
3853 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3854 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003855 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003856 cpcm->device = dev;
3857 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003858 if (err < 0) {
3859 printk(KERN_ERR "hda_codec: cannot attach "
3860 "PCM stream %d for codec #%d\n",
3861 dev, codec->addr);
3862 continue; /* no fatal error */
3863 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003864 }
3865 }
3866 return 0;
3867}
3868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869/**
3870 * snd_hda_build_pcms - build PCM information
3871 * @bus: the BUS
3872 *
3873 * Create PCM information for each codec included in the bus.
3874 *
3875 * The build_pcms codec patch is requested to set up codec->num_pcms and
3876 * codec->pcm_info properly. The array is referred by the top-level driver
3877 * to create its PCM instances.
3878 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3879 * callback.
3880 *
3881 * At least, substreams, channels_min and channels_max must be filled for
3882 * each stream. substreams = 0 indicates that the stream doesn't exist.
3883 * When rates and/or formats are zero, the supported values are queried
3884 * from the given nid. The nid is used also by the default ops.prepare
3885 * and ops.cleanup callbacks.
3886 *
3887 * The driver needs to call ops.open in its open callback. Similarly,
3888 * ops.close is supposed to be called in the close callback.
3889 * ops.prepare should be called in the prepare or hw_params callback
3890 * with the proper parameters for set up.
3891 * ops.cleanup should be called in hw_free for clean up of streams.
3892 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003893 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003895int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003897 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Takashi Iwai0ba21762007-04-16 11:29:14 +02003899 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003900 int err = snd_hda_codec_build_pcms(codec);
3901 if (err < 0)
3902 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 }
3904 return 0;
3905}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003906EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908/**
3909 * snd_hda_check_board_config - compare the current codec with the config table
3910 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003911 * @num_configs: number of config enums
3912 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 * @tbl: configuration table, terminated by null entries
3914 *
3915 * Compares the modelname or PCI subsystem id of the current codec with the
3916 * given configuration table. If a matching entry is found, returns its
3917 * config value (supposed to be 0 or positive).
3918 *
3919 * If no entries are matching, the function returns a negative value.
3920 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003921int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003922 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003923 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003925 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003926 int i;
3927 for (i = 0; i < num_configs; i++) {
3928 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003929 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003930 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3931 "selected\n", models[i]);
3932 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
3934 }
3935 }
3936
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003937 if (!codec->bus->pci || !tbl)
3938 return -1;
3939
3940 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3941 if (!tbl)
3942 return -1;
3943 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003944#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003945 char tmp[10];
3946 const char *model = NULL;
3947 if (models)
3948 model = models[tbl->value];
3949 if (!model) {
3950 sprintf(tmp, "#%d", tbl->value);
3951 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003953 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3954 "for config %x:%x (%s)\n",
3955 model, tbl->subvendor, tbl->subdevice,
3956 (tbl->name ? tbl->name : "Unknown device"));
3957#endif
3958 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 }
3960 return -1;
3961}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003962EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003965 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003966 subsystem ID with the
3967 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003968
3969 This is important for Gateway notebooks with SB450 HDA Audio
3970 where the vendor ID of the PCI device is:
3971 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3972 and the vendor/subvendor are found only at the codec.
3973
3974 * @codec: the HDA codec
3975 * @num_configs: number of config enums
3976 * @models: array of model name strings
3977 * @tbl: configuration table, terminated by null entries
3978 *
3979 * Compares the modelname or PCI subsystem id of the current codec with the
3980 * given configuration table. If a matching entry is found, returns its
3981 * config value (supposed to be 0 or positive).
3982 *
3983 * If no entries are matching, the function returns a negative value.
3984 */
3985int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003986 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003987 const struct snd_pci_quirk *tbl)
3988{
3989 const struct snd_pci_quirk *q;
3990
3991 /* Search for codec ID */
3992 for (q = tbl; q->subvendor; q++) {
3993 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3994
3995 if (vendorid == codec->subsystem_id)
3996 break;
3997 }
3998
3999 if (!q->subvendor)
4000 return -1;
4001
4002 tbl = q;
4003
4004 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004005#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004006 char tmp[10];
4007 const char *model = NULL;
4008 if (models)
4009 model = models[tbl->value];
4010 if (!model) {
4011 sprintf(tmp, "#%d", tbl->value);
4012 model = tmp;
4013 }
4014 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4015 "for config %x:%x (%s)\n",
4016 model, tbl->subvendor, tbl->subdevice,
4017 (tbl->name ? tbl->name : "Unknown device"));
4018#endif
4019 return tbl->value;
4020 }
4021 return -1;
4022}
4023EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4024
4025/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 * snd_hda_add_new_ctls - create controls from the array
4027 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004028 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 *
4030 * This helper function creates and add new controls in the given array.
4031 * The array must be terminated with an empty entry as terminator.
4032 *
4033 * Returns 0 if successful, or a negative error code.
4034 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004035int snd_hda_add_new_ctls(struct hda_codec *codec,
4036 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004038 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
4040 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004041 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004042 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004043 if (knew->iface == -1) /* skip this codec private value */
4044 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004045 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004046 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004047 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004048 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004049 if (addr > 0)
4050 kctl->id.device = addr;
4051 if (idx > 0)
4052 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004053 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004054 if (!err)
4055 break;
4056 /* try first with another device index corresponding to
4057 * the codec addr; if it still fails (or it's the
4058 * primary codec), then try another control index
4059 */
4060 if (!addr && codec->addr)
4061 addr = codec->addr;
4062 else if (!idx && !knew->index) {
4063 idx = find_empty_mixer_ctl_idx(codec,
4064 knew->name);
4065 if (idx <= 0)
4066 return err;
4067 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004068 return err;
4069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 }
4071 return 0;
4072}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004073EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Takashi Iwaicb53c622007-08-10 17:21:45 +02004075#ifdef CONFIG_SND_HDA_POWER_SAVE
4076static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
4077 unsigned int power_state);
4078
4079static void hda_power_work(struct work_struct *work)
4080{
4081 struct hda_codec *codec =
4082 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004083 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004084
Maxim Levitsky2e492462007-09-03 15:26:57 +02004085 if (!codec->power_on || codec->power_count) {
4086 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004087 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004088 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004089
4090 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004091 if (bus->ops.pm_notify)
4092 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004093}
4094
4095static void hda_keep_power_on(struct hda_codec *codec)
4096{
4097 codec->power_count++;
4098 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004099 codec->power_jiffies = jiffies;
4100}
4101
Takashi Iwaid5191e52009-11-16 14:58:17 +01004102/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004103void snd_hda_update_power_acct(struct hda_codec *codec)
4104{
4105 unsigned long delta = jiffies - codec->power_jiffies;
4106 if (codec->power_on)
4107 codec->power_on_acct += delta;
4108 else
4109 codec->power_off_acct += delta;
4110 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004111}
4112
Takashi Iwaid5191e52009-11-16 14:58:17 +01004113/**
4114 * snd_hda_power_up - Power-up the codec
4115 * @codec: HD-audio codec
4116 *
4117 * Increment the power-up counter and power up the hardware really when
4118 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004119 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004120void snd_hda_power_up(struct hda_codec *codec)
4121{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004122 struct hda_bus *bus = codec->bus;
4123
Takashi Iwaicb53c622007-08-10 17:21:45 +02004124 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004125 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004126 return;
4127
Takashi Iwaia2f63092009-11-11 09:34:25 +01004128 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004129 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004130 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004131 if (bus->ops.pm_notify)
4132 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004133 hda_call_codec_resume(codec);
4134 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004135 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004136}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004137EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004138
4139#define power_save(codec) \
4140 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004141
Takashi Iwaid5191e52009-11-16 14:58:17 +01004142/**
4143 * snd_hda_power_down - Power-down the codec
4144 * @codec: HD-audio codec
4145 *
4146 * Decrement the power-up counter and schedules the power-off work if
4147 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004148 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004149void snd_hda_power_down(struct hda_codec *codec)
4150{
4151 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004152 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004153 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004154 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004155 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004156 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004157 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004158 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004159}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004160EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004161
Takashi Iwaid5191e52009-11-16 14:58:17 +01004162/**
4163 * snd_hda_check_amp_list_power - Check the amp list and update the power
4164 * @codec: HD-audio codec
4165 * @check: the object containing an AMP list and the status
4166 * @nid: NID to check / update
4167 *
4168 * Check whether the given NID is in the amp list. If it's in the list,
4169 * check the current AMP status, and update the the power-status according
4170 * to the mute status.
4171 *
4172 * This function is supposed to be set or called from the check_power_status
4173 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004174 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004175int snd_hda_check_amp_list_power(struct hda_codec *codec,
4176 struct hda_loopback_check *check,
4177 hda_nid_t nid)
4178{
Takashi Iwai031024e2011-05-02 11:29:30 +02004179 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004180 int ch, v;
4181
4182 if (!check->amplist)
4183 return 0;
4184 for (p = check->amplist; p->nid; p++) {
4185 if (p->nid == nid)
4186 break;
4187 }
4188 if (!p->nid)
4189 return 0; /* nothing changed */
4190
4191 for (p = check->amplist; p->nid; p++) {
4192 for (ch = 0; ch < 2; ch++) {
4193 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4194 p->idx);
4195 if (!(v & HDA_AMP_MUTE) && v > 0) {
4196 if (!check->power_on) {
4197 check->power_on = 1;
4198 snd_hda_power_up(codec);
4199 }
4200 return 1;
4201 }
4202 }
4203 }
4204 if (check->power_on) {
4205 check->power_on = 0;
4206 snd_hda_power_down(codec);
4207 }
4208 return 0;
4209}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004210EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004213/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004214 * Channel mode helper
4215 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004216
4217/**
4218 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4219 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004220int snd_hda_ch_mode_info(struct hda_codec *codec,
4221 struct snd_ctl_elem_info *uinfo,
4222 const struct hda_channel_mode *chmode,
4223 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004224{
4225 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4226 uinfo->count = 1;
4227 uinfo->value.enumerated.items = num_chmodes;
4228 if (uinfo->value.enumerated.item >= num_chmodes)
4229 uinfo->value.enumerated.item = num_chmodes - 1;
4230 sprintf(uinfo->value.enumerated.name, "%dch",
4231 chmode[uinfo->value.enumerated.item].channels);
4232 return 0;
4233}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004234EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004235
Takashi Iwaid5191e52009-11-16 14:58:17 +01004236/**
4237 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4238 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004239int snd_hda_ch_mode_get(struct hda_codec *codec,
4240 struct snd_ctl_elem_value *ucontrol,
4241 const struct hda_channel_mode *chmode,
4242 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004243 int max_channels)
4244{
4245 int i;
4246
4247 for (i = 0; i < num_chmodes; i++) {
4248 if (max_channels == chmode[i].channels) {
4249 ucontrol->value.enumerated.item[0] = i;
4250 break;
4251 }
4252 }
4253 return 0;
4254}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004255EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004256
Takashi Iwaid5191e52009-11-16 14:58:17 +01004257/**
4258 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4259 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004260int snd_hda_ch_mode_put(struct hda_codec *codec,
4261 struct snd_ctl_elem_value *ucontrol,
4262 const struct hda_channel_mode *chmode,
4263 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004264 int *max_channelsp)
4265{
4266 unsigned int mode;
4267
4268 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004269 if (mode >= num_chmodes)
4270 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004271 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004272 return 0;
4273 /* change the current channel setting */
4274 *max_channelsp = chmode[mode].channels;
4275 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004276 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004277 return 1;
4278}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004279EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281/*
4282 * input MUX helper
4283 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004284
4285/**
4286 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4287 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004288int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4289 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
4291 unsigned int index;
4292
4293 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4294 uinfo->count = 1;
4295 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004296 if (!imux->num_items)
4297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 index = uinfo->value.enumerated.item;
4299 if (index >= imux->num_items)
4300 index = imux->num_items - 1;
4301 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4302 return 0;
4303}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004304EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Takashi Iwaid5191e52009-11-16 14:58:17 +01004306/**
4307 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4308 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004309int snd_hda_input_mux_put(struct hda_codec *codec,
4310 const struct hda_input_mux *imux,
4311 struct snd_ctl_elem_value *ucontrol,
4312 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 unsigned int *cur_val)
4314{
4315 unsigned int idx;
4316
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004317 if (!imux->num_items)
4318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 idx = ucontrol->value.enumerated.item[0];
4320 if (idx >= imux->num_items)
4321 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004322 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004324 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4325 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 *cur_val = idx;
4327 return 1;
4328}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004329EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331
4332/*
4333 * Multi-channel / digital-out PCM helper functions
4334 */
4335
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004336/* setup SPDIF output stream */
4337static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4338 unsigned int stream_tag, unsigned int format)
4339{
Stephen Warren7c9359762011-06-01 11:14:17 -06004340 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4341
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004342 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004343 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004344 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004345 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004346 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004347 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004348 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004349 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004350 for (d = codec->slave_dig_outs; *d; d++)
4351 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4352 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004353 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004354 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004355 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004356 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004357 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004358}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004359
Takashi Iwai2f728532008-09-25 16:32:41 +02004360static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4361{
4362 snd_hda_codec_cleanup_stream(codec, nid);
4363 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004364 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004365 for (d = codec->slave_dig_outs; *d; d++)
4366 snd_hda_codec_cleanup_stream(codec, *d);
4367 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004368}
4369
Takashi Iwaid5191e52009-11-16 14:58:17 +01004370/**
4371 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4372 * @bus: HD-audio bus
4373 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004374void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4375{
4376 struct hda_codec *codec;
4377
4378 if (!bus)
4379 return;
4380 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004381 if (hda_codec_is_power_on(codec) &&
4382 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004383 codec->patch_ops.reboot_notify(codec);
4384 }
4385}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004386EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004387
Takashi Iwaid5191e52009-11-16 14:58:17 +01004388/**
4389 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004391int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4392 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393{
Ingo Molnar62932df2006-01-16 16:34:20 +01004394 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004395 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4396 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004397 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004399 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 return 0;
4401}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004402EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Takashi Iwaid5191e52009-11-16 14:58:17 +01004404/**
4405 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4406 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004407int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4408 struct hda_multi_out *mout,
4409 unsigned int stream_tag,
4410 unsigned int format,
4411 struct snd_pcm_substream *substream)
4412{
4413 mutex_lock(&codec->spdif_mutex);
4414 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4415 mutex_unlock(&codec->spdif_mutex);
4416 return 0;
4417}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004418EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004419
Takashi Iwaid5191e52009-11-16 14:58:17 +01004420/**
4421 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4422 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004423int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4424 struct hda_multi_out *mout)
4425{
4426 mutex_lock(&codec->spdif_mutex);
4427 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4428 mutex_unlock(&codec->spdif_mutex);
4429 return 0;
4430}
4431EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4432
Takashi Iwaid5191e52009-11-16 14:58:17 +01004433/**
4434 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004436int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4437 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
Ingo Molnar62932df2006-01-16 16:34:20 +01004439 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004441 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 return 0;
4443}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004444EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
Takashi Iwaid5191e52009-11-16 14:58:17 +01004446/**
4447 * snd_hda_multi_out_analog_open - open analog outputs
4448 *
4449 * Open analog outputs and set up the hw-constraints.
4450 * If the digital outputs can be opened as slave, open the digital
4451 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004453int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4454 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004455 struct snd_pcm_substream *substream,
4456 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
Takashi Iwai9a081602008-02-12 18:37:26 +01004458 struct snd_pcm_runtime *runtime = substream->runtime;
4459 runtime->hw.channels_max = mout->max_channels;
4460 if (mout->dig_out_nid) {
4461 if (!mout->analog_rates) {
4462 mout->analog_rates = hinfo->rates;
4463 mout->analog_formats = hinfo->formats;
4464 mout->analog_maxbps = hinfo->maxbps;
4465 } else {
4466 runtime->hw.rates = mout->analog_rates;
4467 runtime->hw.formats = mout->analog_formats;
4468 hinfo->maxbps = mout->analog_maxbps;
4469 }
4470 if (!mout->spdif_rates) {
4471 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4472 &mout->spdif_rates,
4473 &mout->spdif_formats,
4474 &mout->spdif_maxbps);
4475 }
4476 mutex_lock(&codec->spdif_mutex);
4477 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004478 if ((runtime->hw.rates & mout->spdif_rates) &&
4479 (runtime->hw.formats & mout->spdif_formats)) {
4480 runtime->hw.rates &= mout->spdif_rates;
4481 runtime->hw.formats &= mout->spdif_formats;
4482 if (mout->spdif_maxbps < hinfo->maxbps)
4483 hinfo->maxbps = mout->spdif_maxbps;
4484 } else {
4485 mout->share_spdif = 0;
4486 /* FIXME: need notify? */
4487 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004488 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004489 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4492 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4493}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004494EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Takashi Iwaid5191e52009-11-16 14:58:17 +01004496/**
4497 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4498 *
4499 * Set up the i/o for analog out.
4500 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004502int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4503 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 unsigned int stream_tag,
4505 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004506 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Takashi Iwaidda14412011-05-02 11:29:30 +02004508 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004510 struct hda_spdif_out *spdif =
4511 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 int i;
4513
Ingo Molnar62932df2006-01-16 16:34:20 +01004514 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004515 if (mout->dig_out_nid && mout->share_spdif &&
4516 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004518 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4519 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004520 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004522 setup_dig_out_stream(codec, mout->dig_out_nid,
4523 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 } else {
4525 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004526 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 }
4528 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004529 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
4531 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004532 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4533 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004534 if (!mout->no_share_stream &&
4535 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004537 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4538 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004539 /* extra outputs copied from front */
4540 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004541 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004542 snd_hda_codec_setup_stream(codec,
4543 mout->extra_out_nid[i],
4544 stream_tag, 0, format);
4545
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 /* surrounds */
4547 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004548 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004549 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4550 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004551 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004552 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4553 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 }
4555 return 0;
4556}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004557EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
Takashi Iwaid5191e52009-11-16 14:58:17 +01004559/**
4560 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004562int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4563 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564{
Takashi Iwaidda14412011-05-02 11:29:30 +02004565 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 int i;
4567
4568 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004569 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004571 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004572 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4573 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004574 snd_hda_codec_cleanup_stream(codec,
4575 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004576 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004578 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 mout->dig_out_used = 0;
4580 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004581 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 return 0;
4583}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004584EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004586/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004587 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004588 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004589
Takashi Iwaidda14412011-05-02 11:29:30 +02004590static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004591{
4592 for (; *list; list++)
4593 if (*list == nid)
4594 return 1;
4595 return 0;
4596}
4597
Steve Longerbeam81937d32007-05-08 15:33:03 +02004598
4599/*
4600 * Sort an associated group of pins according to their sequence numbers.
4601 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004602static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004603 int num_pins)
4604{
4605 int i, j;
4606 short seq;
4607 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004608
Steve Longerbeam81937d32007-05-08 15:33:03 +02004609 for (i = 0; i < num_pins; i++) {
4610 for (j = i + 1; j < num_pins; j++) {
4611 if (sequences[i] > sequences[j]) {
4612 seq = sequences[i];
4613 sequences[i] = sequences[j];
4614 sequences[j] = seq;
4615 nid = pins[i];
4616 pins[i] = pins[j];
4617 pins[j] = nid;
4618 }
4619 }
4620 }
4621}
4622
4623
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004624/* add the found input-pin to the cfg->inputs[] table */
4625static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4626 int type)
4627{
4628 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4629 cfg->inputs[cfg->num_inputs].pin = nid;
4630 cfg->inputs[cfg->num_inputs].type = type;
4631 cfg->num_inputs++;
4632 }
4633}
4634
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004635/* sort inputs in the order of AUTO_PIN_* type */
4636static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4637{
4638 int i, j;
4639
4640 for (i = 0; i < cfg->num_inputs; i++) {
4641 for (j = i + 1; j < cfg->num_inputs; j++) {
4642 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4643 struct auto_pin_cfg_item tmp;
4644 tmp = cfg->inputs[i];
4645 cfg->inputs[i] = cfg->inputs[j];
4646 cfg->inputs[j] = tmp;
4647 }
4648 }
4649 }
4650}
4651
Takashi Iwai82bc9552006-03-21 11:24:42 +01004652/*
4653 * Parse all pin widgets and store the useful pin nids to cfg
4654 *
4655 * The number of line-outs or any primary output is stored in line_outs,
4656 * and the corresponding output pins are assigned to line_out_pins[],
4657 * in the order of front, rear, CLFE, side, ...
4658 *
4659 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004660 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004661 * is detected, one of speaker of HP pins is assigned as the primary
4662 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4663 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004664 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004665 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004666 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4667 * respectively.
4668 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004669int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4670 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004671 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004672{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004673 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004674 short seq, assoc_line_out, assoc_speaker;
4675 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4676 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004677 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004678 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004679
4680 memset(cfg, 0, sizeof(*cfg));
4681
Steve Longerbeam81937d32007-05-08 15:33:03 +02004682 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4683 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004684 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004685 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004686
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004687 end_nid = codec->start_nid + codec->num_nodes;
4688 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004689 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004690 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004691 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004692 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004693
4694 /* read all default configuration for pin complex */
4695 if (wid_type != AC_WID_PIN)
4696 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004697 /* ignore the given nids (e.g. pc-beep returns error) */
4698 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4699 continue;
4700
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004701 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004702 conn = get_defcfg_connect(def_conf);
4703 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004704 continue;
4705 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004706 dev = get_defcfg_device(def_conf);
4707
4708 /* workaround for buggy BIOS setups */
4709 if (dev == AC_JACK_LINE_OUT) {
4710 if (conn == AC_JACK_PORT_FIXED)
4711 dev = AC_JACK_SPEAKER;
4712 }
4713
4714 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004715 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004716 seq = get_defcfg_sequence(def_conf);
4717 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004718
4719 if (!(wid_caps & AC_WCAP_STEREO))
4720 if (!cfg->mono_out_pin)
4721 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004722 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004723 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004724 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004725 assoc_line_out = assoc;
4726 else if (assoc_line_out != assoc)
4727 continue;
4728 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4729 continue;
4730 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004731 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004732 cfg->line_outs++;
4733 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004734 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004735 seq = get_defcfg_sequence(def_conf);
4736 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004737 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004738 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004739 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004740 assoc_speaker = assoc;
4741 else if (assoc_speaker != assoc)
4742 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004743 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4744 continue;
4745 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004746 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004747 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004748 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004749 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004750 seq = get_defcfg_sequence(def_conf);
4751 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004752 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4753 continue;
4754 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004755 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004756 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004757 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004758 case AC_JACK_MIC_IN:
4759 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004760 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004761 case AC_JACK_LINE_IN:
4762 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004763 break;
4764 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004765 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004766 break;
4767 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004768 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004769 break;
4770 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004771 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004772 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4773 continue;
4774 cfg->dig_out_pins[cfg->dig_outs] = nid;
4775 cfg->dig_out_type[cfg->dig_outs] =
4776 (loc == AC_JACK_LOC_HDMI) ?
4777 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4778 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004779 break;
4780 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004781 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004782 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004783 if (loc == AC_JACK_LOC_HDMI)
4784 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4785 else
4786 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004787 break;
4788 }
4789 }
4790
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004791 /* FIX-UP:
4792 * If no line-out is defined but multiple HPs are found,
4793 * some of them might be the real line-outs.
4794 */
4795 if (!cfg->line_outs && cfg->hp_outs > 1) {
4796 int i = 0;
4797 while (i < cfg->hp_outs) {
4798 /* The real HPs should have the sequence 0x0f */
4799 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4800 i++;
4801 continue;
4802 }
4803 /* Move it to the line-out table */
4804 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4805 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4806 cfg->line_outs++;
4807 cfg->hp_outs--;
4808 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4809 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004810 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004811 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4812 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004813 memset(cfg->hp_pins + cfg->hp_outs, 0,
4814 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004815 if (!cfg->hp_outs)
4816 cfg->line_out_type = AUTO_PIN_HP_OUT;
4817
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004818 }
4819
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004820 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004821 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4822 cfg->line_outs);
4823 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4824 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004825 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4826 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004827
Steve Longerbeam81937d32007-05-08 15:33:03 +02004828 /*
4829 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4830 * as a primary output
4831 */
4832 if (!cfg->line_outs) {
4833 if (cfg->speaker_outs) {
4834 cfg->line_outs = cfg->speaker_outs;
4835 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4836 sizeof(cfg->speaker_pins));
4837 cfg->speaker_outs = 0;
4838 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4839 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4840 } else if (cfg->hp_outs) {
4841 cfg->line_outs = cfg->hp_outs;
4842 memcpy(cfg->line_out_pins, cfg->hp_pins,
4843 sizeof(cfg->hp_pins));
4844 cfg->hp_outs = 0;
4845 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4846 cfg->line_out_type = AUTO_PIN_HP_OUT;
4847 }
4848 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004849
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004850 /* Reorder the surround channels
4851 * ALSA sequence is front/surr/clfe/side
4852 * HDA sequence is:
4853 * 4-ch: front/surr => OK as it is
4854 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004855 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004856 */
4857 switch (cfg->line_outs) {
4858 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004859 case 4:
4860 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004861 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004862 cfg->line_out_pins[2] = nid;
4863 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004864 }
4865
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004866 sort_autocfg_input_pins(cfg);
4867
Takashi Iwai82bc9552006-03-21 11:24:42 +01004868 /*
4869 * debug prints of the parsed results
4870 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004871 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 +01004872 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4873 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004874 cfg->line_out_pins[4],
4875 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4876 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4877 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004878 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4879 cfg->speaker_outs, cfg->speaker_pins[0],
4880 cfg->speaker_pins[1], cfg->speaker_pins[2],
4881 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004882 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4883 cfg->hp_outs, cfg->hp_pins[0],
4884 cfg->hp_pins[1], cfg->hp_pins[2],
4885 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004886 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004887 if (cfg->dig_outs)
4888 snd_printd(" dig-out=0x%x/0x%x\n",
4889 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004890 snd_printd(" inputs:");
4891 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004892 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004893 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004894 cfg->inputs[i].pin);
4895 }
4896 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004897 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004898 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004899
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004900 return 0;
4901}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004902EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004903
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004904int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004905{
4906 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004907 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004908 if (conn == AC_JACK_PORT_NONE)
4909 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004910 /* Windows may claim the internal mic to be BOTH, too */
4911 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004912 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004913 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004914 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004915 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004916 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004917 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004918 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004919 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004920 return INPUT_PIN_ATTR_FRONT;
4921 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004922}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004923EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004924
Takashi Iwai990061c2010-09-09 22:08:44 +02004925/**
4926 * hda_get_input_pin_label - Give a label for the given input pin
4927 *
4928 * When check_location is true, the function checks the pin location
4929 * for mic and line-in pins, and set an appropriate prefix like "Front",
4930 * "Rear", "Internal".
4931 */
4932
Takashi Iwai10a20af2010-09-09 16:28:02 +02004933const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4934 int check_location)
4935{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004936 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004937 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004938 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4939 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004940 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004941
Takashi Iwai10a20af2010-09-09 16:28:02 +02004942 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004943
4944 switch (get_defcfg_device(def_conf)) {
4945 case AC_JACK_MIC_IN:
4946 if (!check_location)
4947 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004948 attr = snd_hda_get_input_pin_attr(def_conf);
4949 if (!attr)
4950 return "None";
4951 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004952 case AC_JACK_LINE_IN:
4953 if (!check_location)
4954 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004955 attr = snd_hda_get_input_pin_attr(def_conf);
4956 if (!attr)
4957 return "None";
4958 if (attr == INPUT_PIN_ATTR_DOCK)
4959 return "Dock Line";
4960 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004961 case AC_JACK_AUX:
4962 return "Aux";
4963 case AC_JACK_CD:
4964 return "CD";
4965 case AC_JACK_SPDIF_IN:
4966 return "SPDIF In";
4967 case AC_JACK_DIG_OTHER_IN:
4968 return "Digital In";
4969 default:
4970 return "Misc";
4971 }
4972}
4973EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4974
Takashi Iwaia1c98512010-09-09 21:36:27 +02004975/* Check whether the location prefix needs to be added to the label.
4976 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4977 * have to put "Front" prefix to each label. In such a case, returns false.
4978 */
4979static int check_mic_location_need(struct hda_codec *codec,
4980 const struct auto_pin_cfg *cfg,
4981 int input)
4982{
4983 unsigned int defc;
4984 int i, attr, attr2;
4985
4986 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004987 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004988 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004989 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004990 return 1;
4991
4992 attr = 0;
4993 for (i = 0; i < cfg->num_inputs; i++) {
4994 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004995 attr2 = snd_hda_get_input_pin_attr(defc);
4996 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004997 if (attr && attr != attr2)
4998 return 1; /* different locations found */
4999 attr = attr2;
5000 }
5001 }
5002 return 0;
5003}
5004
Takashi Iwai990061c2010-09-09 22:08:44 +02005005/**
5006 * hda_get_autocfg_input_label - Get a label for the given input
5007 *
5008 * Get a label for the given input pin defined by the autocfg item.
5009 * Unlike hda_get_input_pin_label(), this function checks all inputs
5010 * defined in autocfg and avoids the redundant mic/line prefix as much as
5011 * possible.
5012 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005013const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5014 const struct auto_pin_cfg *cfg,
5015 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005016{
5017 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005018 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005019
Takashi Iwai10a20af2010-09-09 16:28:02 +02005020 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5021 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5022 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005023 if (has_multiple_pins && type == AUTO_PIN_MIC)
5024 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005025 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5026 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005027}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005028EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5029
Takashi Iwai990061c2010-09-09 22:08:44 +02005030/**
5031 * snd_hda_add_imux_item - Add an item to input_mux
5032 *
5033 * When the same label is used already in the existing items, the number
5034 * suffix is appended to the label. This label index number is stored
5035 * to type_idx when non-NULL pointer is given.
5036 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005037int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5038 int index, int *type_idx)
5039{
5040 int i, label_idx = 0;
5041 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5042 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5043 return -EINVAL;
5044 }
5045 for (i = 0; i < imux->num_items; i++) {
5046 if (!strncmp(label, imux->items[i].label, strlen(label)))
5047 label_idx++;
5048 }
5049 if (type_idx)
5050 *type_idx = label_idx;
5051 if (label_idx > 0)
5052 snprintf(imux->items[imux->num_items].label,
5053 sizeof(imux->items[imux->num_items].label),
5054 "%s %d", label, label_idx);
5055 else
5056 strlcpy(imux->items[imux->num_items].label, label,
5057 sizeof(imux->items[imux->num_items].label));
5058 imux->items[imux->num_items].index = index;
5059 imux->num_items++;
5060 return 0;
5061}
5062EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005063
Takashi Iwai4a471b72005-12-07 13:56:29 +01005064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065#ifdef CONFIG_PM
5066/*
5067 * power management
5068 */
5069
5070/**
5071 * snd_hda_suspend - suspend the codecs
5072 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 *
5074 * Returns 0 if successful.
5075 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005076int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005078 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Takashi Iwai0ba21762007-04-16 11:29:14 +02005080 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005081 if (hda_codec_is_power_on(codec))
5082 hda_call_codec_suspend(codec);
5083 if (codec->patch_ops.post_suspend)
5084 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 }
5086 return 0;
5087}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005088EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
5090/**
5091 * snd_hda_resume - resume the codecs
5092 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 *
5094 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005095 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005096 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005097 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 */
5099int snd_hda_resume(struct hda_bus *bus)
5100{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005101 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
Takashi Iwai0ba21762007-04-16 11:29:14 +02005103 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005104 if (codec->patch_ops.pre_resume)
5105 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005106 if (snd_hda_codec_needs_resume(codec))
5107 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 return 0;
5110}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005111EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005112#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005113
5114/*
5115 * generic arrays
5116 */
5117
Takashi Iwaid5191e52009-11-16 14:58:17 +01005118/**
5119 * snd_array_new - get a new element from the given array
5120 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005121 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005122 * Get a new element from the given array. If it exceeds the
5123 * pre-allocated array size, re-allocate the array.
5124 *
5125 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005126 */
5127void *snd_array_new(struct snd_array *array)
5128{
5129 if (array->used >= array->alloced) {
5130 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005131 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005132 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005133 void *nlist;
5134 if (snd_BUG_ON(num >= 4096))
5135 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005136 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005137 if (!nlist)
5138 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005139 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005140 array->list = nlist;
5141 array->alloced = num;
5142 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005143 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005145EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005146
Takashi Iwaid5191e52009-11-16 14:58:17 +01005147/**
5148 * snd_array_free - free the given array elements
5149 * @array: the array object
5150 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005151void snd_array_free(struct snd_array *array)
5152{
5153 kfree(array->list);
5154 array->used = 0;
5155 array->alloced = 0;
5156 array->list = NULL;
5157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005158EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005159
Takashi Iwaid5191e52009-11-16 14:58:17 +01005160/**
5161 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5162 * @pcm: PCM caps bits
5163 * @buf: the string buffer to write
5164 * @buflen: the max buffer length
5165 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005166 * used by hda_proc.c and hda_eld.c
5167 */
5168void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5169{
5170 static unsigned int rates[] = {
5171 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5172 96000, 176400, 192000, 384000
5173 };
5174 int i, j;
5175
5176 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5177 if (pcm & (1 << i))
5178 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5179
5180 buf[j] = '\0'; /* necessary when j == 0 */
5181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005182EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005183
Takashi Iwaid5191e52009-11-16 14:58:17 +01005184/**
5185 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5186 * @pcm: PCM caps bits
5187 * @buf: the string buffer to write
5188 * @buflen: the max buffer length
5189 *
5190 * used by hda_proc.c and hda_eld.c
5191 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005192void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5193{
5194 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5195 int i, j;
5196
5197 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5198 if (pcm & (AC_SUPPCM_BITS_8 << i))
5199 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5200
5201 buf[j] = '\0'; /* necessary when j == 0 */
5202}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005203EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005204
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005205#ifdef CONFIG_SND_HDA_INPUT_JACK
5206/*
5207 * Input-jack notification support
5208 */
5209struct hda_jack_item {
5210 hda_nid_t nid;
5211 int type;
5212 struct snd_jack *jack;
5213};
5214
5215static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5216 int type)
5217{
5218 switch (type) {
5219 case SND_JACK_HEADPHONE:
5220 return "Headphone";
5221 case SND_JACK_MICROPHONE:
5222 return "Mic";
5223 case SND_JACK_LINEOUT:
5224 return "Line-out";
5225 case SND_JACK_HEADSET:
5226 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005227 case SND_JACK_VIDEOOUT:
5228 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005229 default:
5230 return "Misc";
5231 }
5232}
5233
5234static void hda_free_jack_priv(struct snd_jack *jack)
5235{
5236 struct hda_jack_item *jacks = jack->private_data;
5237 jacks->nid = 0;
5238 jacks->jack = NULL;
5239}
5240
5241int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5242 const char *name)
5243{
5244 struct hda_jack_item *jack;
5245 int err;
5246
5247 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5248 jack = snd_array_new(&codec->jacks);
5249 if (!jack)
5250 return -ENOMEM;
5251
5252 jack->nid = nid;
5253 jack->type = type;
5254 if (!name)
5255 name = get_jack_default_name(codec, nid, type);
5256 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5257 if (err < 0) {
5258 jack->nid = 0;
5259 return err;
5260 }
5261 jack->jack->private_data = jack;
5262 jack->jack->private_free = hda_free_jack_priv;
5263 return 0;
5264}
5265EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5266
5267void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5268{
5269 struct hda_jack_item *jacks = codec->jacks.list;
5270 int i;
5271
5272 if (!jacks)
5273 return;
5274
5275 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5276 unsigned int pin_ctl;
5277 unsigned int present;
5278 int type;
5279
5280 if (jacks->nid != nid)
5281 continue;
5282 present = snd_hda_jack_detect(codec, nid);
5283 type = jacks->type;
5284 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5285 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5286 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5287 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5288 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5289 }
5290 snd_jack_report(jacks->jack, present ? type : 0);
5291 }
5292}
5293EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5294
5295/* free jack instances manually when clearing/reconfiguring */
5296void snd_hda_input_jack_free(struct hda_codec *codec)
5297{
5298 if (!codec->bus->shutdown && codec->jacks.list) {
5299 struct hda_jack_item *jacks = codec->jacks.list;
5300 int i;
5301 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5302 if (jacks->jack)
5303 snd_device_free(codec->bus->card, jacks->jack);
5304 }
5305 }
5306 snd_array_free(&codec->jacks);
5307}
5308EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5309#endif /* CONFIG_SND_HDA_INPUT_JACK */
5310
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005311MODULE_DESCRIPTION("HDA codec core");
5312MODULE_LICENSE("GPL");